Debian Codegraph @

drivers/dahdi/dahdi-sysfs.c

raw
1 /* dahdi-sysfs.c
2 *
3 * Copyright (C) 2011-2012, Xorcom
4 * Copyright (C) 2011-2012, Digium, Inc.
5 *
6 * All rights reserved.
7 *
8 */
9
10 /*
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/version.h>
25 #define DAHDI_PRINK_MACROS_USE_debug
26 #include <dahdi/kernel.h>
27 #include <linux/device.h>
28 #include <linux/slab.h>
29 #include <linux/ctype.h>
30
31 #include "dahdi.h"
32 #include "dahdi-sysfs.h"
33
34
35 static char *initdir;
36 module_param(initdir, charp, 0444);
37 MODULE_PARM_DESC(initdir,
38 "deprecated, should use <tools_rootdir>/usr/share/dahdi");
39
40 static char *tools_rootdir;
41 module_param(tools_rootdir, charp, 0444);
42 MODULE_PARM_DESC(tools_rootdir,
43 "root directory of all tools paths (default /)");
44
45 static int span_match(struct device *dev, struct device_driver *driver)
46 {
47 return 1;
48 }
49
50 static inline struct dahdi_span *dev_to_span(struct device *dev)
51 {
52 return dev_get_drvdata(dev);
53 }
54
55 #define SPAN_VAR_BLOCK \
56 do { \
57 DAHDI_ADD_UEVENT_VAR("DAHDI_TOOLS_ROOTDIR=%s", tools_rootdir); \
58 DAHDI_ADD_UEVENT_VAR("DAHDI_INIT_DIR=%s/%s", tools_rootdir, \
59 initdir); \
60 DAHDI_ADD_UEVENT_VAR("SPAN_NUM=%d", span->spanno); \
61 DAHDI_ADD_UEVENT_VAR("SPAN_NAME=%s", span->name); \
62 } while (0)
63
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
65 #define DAHDI_ADD_UEVENT_VAR(fmt, val...) \
66 do { \
67 int err = add_uevent_var(envp, num_envp, &i, \
68 buffer, buffer_size, &len, \
69 fmt, val); \
70 if (err) \
71 return err; \
72 } while (0)
73
74 static int span_uevent(struct device *dev, char **envp, int num_envp,
75 char *buffer, int buffer_size)
76 {
77 struct dahdi_span *span;
78 int i = 0;
79 int len = 0;
80
81 if (!dev)
82 return -ENODEV;
83
84 span = dev_to_span(dev);
85 if (!span)
86 return -ENODEV;
87
88 dahdi_dbg(GENERAL, "SYFS dev_name=%s span=%s\n",
89 dev_name(dev), span->name);
90 SPAN_VAR_BLOCK;
91 envp[i] = NULL;
92 return 0;
93 }
94
95 #else
96 #define DAHDI_ADD_UEVENT_VAR(fmt, val...) \
97 do { \
98 int err = add_uevent_var(kenv, fmt, val); \
99 if (err) \
100 return err; \
101 } while (0)
102
103 static int span_uevent(struct device *dev, struct kobj_uevent_env *kenv)
104 {
105 struct dahdi_span *span;
106
107 if (!dev)
108 return -ENODEV;
109 span = dev_to_span(dev);
110 if (!span)
111 return -ENODEV;
112 dahdi_dbg(GENERAL, "SYFS dev_name=%s span=%s\n",
113 dev_name(dev), span->name);
114 SPAN_VAR_BLOCK;
115 return 0;
116 }
117
118 #endif
119
120 #define span_attr(field, format_string) \
121 static BUS_ATTR_READER(field##_show, dev, buf) \
122 { \
123 struct dahdi_span *span; \
124 \
125 span = dev_to_span(dev); \
126 return sprintf(buf, format_string, span->field); \
127 }
128
129 span_attr(name, "%s\n");
130 span_attr(desc, "%s\n");
131 span_attr(alarms, "0x%x\n");
132 span_attr(lbo, "%d\n");
133 span_attr(syncsrc, "%d\n");
134
135 static BUS_ATTR_READER(spantype_show, dev, buf)
136 {
137 struct dahdi_span *span;
138
139 span = dev_to_span(dev);
140 return sprintf(buf, "%s\n", dahdi_spantype2str(span->spantype));
141 }
142
143 static BUS_ATTR_READER(local_spanno_show, dev, buf)
144 {
145 struct dahdi_span *span;
146
147 span = dev_to_span(dev);
148 return sprintf(buf, "%d\n", local_spanno(span));
149 }
150
151 static BUS_ATTR_READER(is_digital_show, dev, buf)
152 {
153 struct dahdi_span *span;
154
155 span = dev_to_span(dev);
156 return sprintf(buf, "%d\n", dahdi_is_digital_span(span));
157 }
158
159 static BUS_ATTR_READER(is_sync_master_show, dev, buf)
160 {
161 struct dahdi_span *span;
162
163 span = dev_to_span(dev);
164 return sprintf(buf, "%d\n", dahdi_is_sync_master(span));
165 }
166
167 static BUS_ATTR_READER(basechan_show, dev, buf)
168 {
169 struct dahdi_span *span;
170
171 span = dev_to_span(dev);
172 if (!span->channels)
173 return -ENODEV;
174 return sprintf(buf, "%d\n", span->chans[0]->channo);
175 }
176
177 static BUS_ATTR_READER(channels_show, dev, buf)
178 {
179 struct dahdi_span *span;
180
181 span = dev_to_span(dev);
182 return sprintf(buf, "%d\n", span->channels);
183 }
184
185 static BUS_ATTR_READER(lineconfig_show, dev, buf)
186 {
187 struct dahdi_span *span;
188 int len = 0;
189
190 span = dev_to_span(dev);
191 len += lineconfig_str(span->lineconfig, buf, 20);
192 len += sprintf(buf + len, "\n");
193 return len;
194 }
195
196 static BUS_ATTR_READER(linecompat_show, dev, buf)
197 {
198 struct dahdi_span *span;
199 int bit;
200 int len = 0;
201
202 span = dev_to_span(dev);
203 for (bit = 4; bit <= 12; bit++) {
204 if (span->linecompat & (1 << bit)) {
205 const char *name = dahdi_lineconfig_bit_name(bit);
206 if (name)
207 len += sprintf(buf + len, "%s ", name);
208 }
209 }
210 /* chomp */
211 while (len > 0 && isspace(buf[len - 1]))
212 buf[--len] = '\0';
213 len += sprintf(buf + len, "\n");
214 return len;
215 }
216
217 static struct device_attribute span_dev_attrs[] = {
218 __ATTR_RO(name),
219 __ATTR_RO(desc),
220 __ATTR_RO(spantype),
221 __ATTR_RO(local_spanno),
222 __ATTR_RO(alarms),
223 __ATTR_RO(lbo),
224 __ATTR_RO(syncsrc),
225 __ATTR_RO(is_digital),
226 __ATTR_RO(is_sync_master),
227 __ATTR_RO(basechan),
228 __ATTR_RO(channels),
229 __ATTR_RO(lineconfig),
230 __ATTR_RO(linecompat),
231 __ATTR_NULL,
232 };
233
234 static ssize_t master_span_show(struct device_driver *driver, char *buf)
235 {
236 struct dahdi_span *s = get_master_span();
237
238 return snprintf(buf, PAGE_SIZE, "%d\n", (s) ? s->spanno : 0);
239 }
240
241 static ssize_t master_span_store(struct device_driver *driver, const char *buf,
242 size_t count)
243 {
244 int spanno;
245
246 if (sscanf(buf, "%d", &spanno) != 1) {
247 module_printk(KERN_ERR, "non-numeric input '%s'\n", buf);
248 return -EINVAL;
249 }
250 set_master_span(spanno);
251 return count;
252 }
253
254 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
255 static struct driver_attribute dahdi_attrs[] = {
256 __ATTR(master_span, S_IRUGO | S_IWUSR, master_span_show,
257 master_span_store),
258 __ATTR_NULL,
259 };
260 #else
261 static DRIVER_ATTR_RW(master_span);
262 static struct attribute *dahdi_attrs[] = {
263 &driver_attr_master_span.attr,
264 NULL,
265 };
266 ATTRIBUTE_GROUPS(dahdi);
267 #endif
268
269 static struct bus_type spans_bus_type = {
270 .name = "dahdi_spans",
271 .match = span_match,
272 .uevent = span_uevent,
273 .dev_attrs = span_dev_attrs,
274 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
275 .drv_attrs = dahdi_attrs,
276 #else
277 .drv_groups = dahdi_groups,
278 #endif
279 };
280
281 static int span_probe(struct device *dev)
282 {
283 struct dahdi_span *span;
284
285 span = dev_to_span(dev);
286 span_dbg(DEVICES, span, "\n");
287 return 0;
288 }
289
290 static int span_remove(struct device *dev)
291 {
292 struct dahdi_span *span;
293
294 span = dev_to_span(dev);
295 span_dbg(DEVICES, span, "\n");
296 return 0;
297 }
298
299 static struct device_driver dahdi_driver = {
300 .name = "generic_lowlevel",
301 .bus = &spans_bus_type,
302 .probe = span_probe,
303 .remove = span_remove,
304 .owner = THIS_MODULE
305 };
306
307 static void span_uevent_send(struct dahdi_span *span, enum kobject_action act)
308 {
309 struct kobject *kobj;
310
311 kobj = &span->span_device->kobj;
312 span_dbg(DEVICES, span, "SYFS dev_name=%s action=%d\n",
313 dev_name(span->span_device), act);
314 kobject_uevent(kobj, act);
315 }
316
317 static void span_release(struct device *dev)
318 {
319 dahdi_dbg(DEVICES, "%s: %s\n", __func__, dev_name(dev));
320 }
321
322 void span_sysfs_remove(struct dahdi_span *span)
323 {
324 struct device *span_device;
325 int x;
326
327 span_dbg(DEVICES, span, "\n");
328 span_device = span->span_device;
329
330 if (!span_device)
331 return;
332
333 for (x = 0; x < span->channels; x++)
334 chan_sysfs_remove(span->chans[x]);
335 if (!dev_get_drvdata(span_device))
336 return;
337
338 /* Grab an extra reference to the device since we'll still want it
339 * after we've unregistered it */
340
341 get_device(span_device);
342 span_uevent_send(span, KOBJ_OFFLINE);
343 sysfs_remove_link(&span_device->kobj, "ddev");
344 device_unregister(span->span_device);
345 dev_set_drvdata(span_device, NULL);
346 span_device->parent = NULL;
347 put_device(span_device);
348 memset(&span->span_device, 0, sizeof(span->span_device));
349 kfree(span->span_device);
350 span->span_device = NULL;
351 }
352
353 int span_sysfs_create(struct dahdi_span *span)
354 {
355 struct device *span_device;
356 int res = 0;
357 int x;
358
359 if (span->span_device) {
360 WARN_ON(1);
361 return -EEXIST;
362 }
363
364 span->span_device = kzalloc(sizeof(*span->span_device), GFP_KERNEL);
365 if (!span->span_device)
366 return -ENOMEM;
367
368 span_device = span->span_device;
369 span_dbg(DEVICES, span, "\n");
370
371 span_device->bus = &spans_bus_type;
372 span_device->parent = &span->parent->dev;
373 dev_set_name(span_device, "span-%d", span->spanno);
374 dev_set_drvdata(span_device, span);
375 span_device->release = span_release;
376 res = device_register(span_device);
377 if (res) {
378 span_err(span, "%s: device_register failed: %d\n", __func__,
379 res);
380 kfree(span->span_device);
381 span->span_device = NULL;
382 goto cleanup;
383 }
384 res = sysfs_create_link(&span_device->kobj, &span_device->parent->kobj,
385 "ddev");
386 if (res) {
387 span_err(span, "%s: sysfs_create_link failed: %d\n", __func__,
388 res);
389 kfree(span->span_device);
390 span->span_device = NULL;
391 goto cleanup;
392 }
393
394 for (x = 0; x < span->channels; x++) {
395 res = chan_sysfs_create(span->chans[x]);
396 if (res)
397 goto cleanup;
398 }
399 return 0;
400
401 cleanup:
402 span_sysfs_remove(span);
403 return res;
404 }
405
406 /* Only used to flag that the device exists: */
407 static struct {
408 unsigned int clean_dahdi_driver:1;
409 unsigned int clean_span_bus_type:1;
410 unsigned int clean_device_bus:1;
411 unsigned int clean_chardev:1;
412 } should_cleanup;
413
414 static inline struct dahdi_device *to_ddev(struct device *dev)
415 {
416 return container_of(dev, struct dahdi_device, dev);
417 }
418
419 #define DEVICE_VAR_BLOCK \
420 do { \
421 DAHDI_ADD_UEVENT_VAR("DAHDI_TOOLS_ROOTDIR=%s", tools_rootdir); \
422 DAHDI_ADD_UEVENT_VAR("DAHDI_INIT_DIR=%s/%s", tools_rootdir, \
423 initdir); \
424 DAHDI_ADD_UEVENT_VAR("DAHDI_DEVICE_HWID=%s", \
425 ddev->hardware_id); \
426 DAHDI_ADD_UEVENT_VAR("DAHDI_DEVICE_LOCATION=%s", \
427 ddev->location); \
428 } while (0)
429
430 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
431 #define DAHDI_ADD_UEVENT_VAR(fmt, val...) \
432 do { \
433 int err = add_uevent_var(envp, num_envp, &i, \
434 buffer, buffer_size, &len, \
435 fmt, val); \
436 if (err) \
437 return err; \
438 } while (0)
439
440 static int device_uevent(struct device *dev, char **envp, int num_envp,
441 char *buffer, int buffer_size)
442 {
443 struct dahdi_device *ddev;
444 int i = 0;
445 int len = 0;
446
447 if (!dev)
448 return -ENODEV;
449
450 ddev = to_ddev(dev);
451 if (!ddev)
452 return -ENODEV;
453
454 dahdi_dbg(GENERAL, "SYFS dev_name=%s\n", dev_name(dev));
455 DEVICE_VAR_BLOCK;
456 envp[i] = NULL;
457 return 0;
458 }
459
460 #else
461 #define DAHDI_ADD_UEVENT_VAR(fmt, val...) \
462 do { \
463 int err = add_uevent_var(kenv, fmt, val); \
464 if (err) \
465 return err; \
466 } while (0)
467
468 static int device_uevent(struct device *dev, struct kobj_uevent_env *kenv)
469 {
470 struct dahdi_device *ddev;
471
472 if (!dev)
473 return -ENODEV;
474 ddev = to_ddev(dev);
475 if (!ddev)
476 return -ENODEV;
477 dahdi_dbg(GENERAL, "SYFS dev_name=%s\n", dev_name(dev));
478 DEVICE_VAR_BLOCK;
479 return 0;
480 }
481
482 #endif
483
484 static ssize_t
485 dahdi_device_manufacturer_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487 {
488 struct dahdi_device *ddev = to_ddev(dev);
489 return sprintf(buf, "%s\n", ddev->manufacturer);
490 }
491
492 static ssize_t
493 dahdi_device_type_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495 {
496 struct dahdi_device *ddev = to_ddev(dev);
497 return sprintf(buf, "%s\n", ddev->devicetype);
498 }
499
500 static ssize_t
501 dahdi_device_span_count_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503 {
504 struct dahdi_device *ddev = to_ddev(dev);
505 unsigned int count = 0;
506 struct list_head *pos;
507
508 list_for_each(pos, &ddev->spans)
509 ++count;
510
511 return sprintf(buf, "%d\n", count);
512 }
513
514 static ssize_t
515 dahdi_device_hardware_id_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
517 {
518 struct dahdi_device *ddev = to_ddev(dev);
519
520 return sprintf(buf, "%s\n",
521 (ddev->hardware_id) ? ddev->hardware_id : "");
522 }
523
524 static ssize_t
525 dahdi_device_location_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527 {
528 struct dahdi_device *ddev = to_ddev(dev);
529
530 return sprintf(buf, "%s\n",
531 (ddev->location) ? ddev->location : "");
532 }
533
534 static ssize_t
535 dahdi_device_auto_assign(struct device *dev, struct device_attribute *attr,
536 const char *buf, size_t count)
537 {
538 struct dahdi_device *ddev = to_ddev(dev);
539 dahdi_assign_device_spans(ddev);
540 return count;
541 }
542
543 static ssize_t
544 dahdi_device_assign_span(struct device *dev, struct device_attribute *attr,
545 const char *buf, size_t count)
546 {
547 int ret;
548 struct dahdi_span *span;
549 unsigned int local_span_number;
550 unsigned int desired_spanno;
551 unsigned int desired_basechanno;
552 struct dahdi_device *const ddev = to_ddev(dev);
553
554 ret = sscanf(buf, "%u:%u:%u", &local_span_number, &desired_spanno,
555 &desired_basechanno);
556 if (ret != 3) {
557 dev_notice(dev, "bad input (should be <num>:<num>:<num>)\n");
558 return -EINVAL;
559 }
560
561 if (desired_spanno && !desired_basechanno) {
562 dev_notice(dev, "Must set span number AND base chan number\n");
563 return -EINVAL;
564 }
565
566 list_for_each_entry(span, &ddev->spans, device_node) {
567 if (local_span_number == local_spanno(span)) {
568 ret = dahdi_assign_span(span, desired_spanno,
569 desired_basechanno, 1);
570 return (ret) ? ret : count;
571 }
572 }
573 dev_notice(dev, "no match for local span number %d\n",
574 local_span_number);
575 return -EINVAL;
576 }
577
578 static ssize_t
579 dahdi_device_unassign_span(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
581 {
582 int ret;
583 unsigned int local_span_number;
584 struct dahdi_span *span;
585 struct dahdi_device *const ddev = to_ddev(dev);
586
587 ret = sscanf(buf, "%u", &local_span_number);
588 if (ret != 1)
589 return -EINVAL;
590
591 ret = -ENODEV;
592 list_for_each_entry(span, &ddev->spans, device_node) {
593 if (local_span_number == local_spanno(span))
594 ret = dahdi_unassign_span(span);
595 }
596 if (-ENODEV == ret) {
597 if (printk_ratelimit()) {
598 dev_info(dev, "'%d' is an invalid local span number.\n",
599 local_span_number);
600 }
601 return -EINVAL;
602 }
603 return (ret < 0) ? ret : count;
604 }
605
606 static ssize_t
607 dahdi_spantype_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
609 {
610 struct dahdi_device *ddev = to_ddev(dev);
611 int count = 0;
612 ssize_t total = 0;
613 struct dahdi_span *span;
614
615 /* TODO: Make sure this doesn't overflow the page. */
616 list_for_each_entry(span, &ddev->spans, device_node) {
617 count = sprintf(buf, "%d:%s\n",
618 local_spanno(span), dahdi_spantype2str(span->spantype));
619 buf += count;
620 total += count;
621 }
622
623 return total;
624 }
625
626 static ssize_t
627 dahdi_spantype_store(struct device *dev, struct device_attribute *attr,
628 const char *buf, size_t count)
629 {
630 struct dahdi_device *const ddev = to_ddev(dev);
631 int ret;
632 struct dahdi_span *span = NULL;
633 struct dahdi_span *cur;
634 unsigned int local_span_number;
635 char spantype_name[80];
636 enum spantypes spantype;
637
638 ret = sscanf(buf, "%u:%70s", &local_span_number, spantype_name);
639 if (ret != 2) {
640 dev_err(&ddev->dev, "Wrong input format: '%s'\n", buf);
641 return -EINVAL;
642 }
643 spantype = dahdi_str2spantype(spantype_name);
644 if (spantype == SPANTYPE_INVALID) {
645 dev_err(&ddev->dev, "Invalid spantype: '%s'\n", buf);
646 return -EINVAL;
647 }
648
649 list_for_each_entry(cur, &ddev->spans, device_node) {
650 if (local_spanno(cur) == local_span_number) {
651 span = cur;
652 break;
653 }
654 }
655
656 if (!span || (local_spanno(span) != local_span_number)) {
657 module_printk(KERN_WARNING,
658 "%d is not a valid local span number "
659 "for this device.\n", local_span_number);
660 return -EINVAL;
661 }
662
663 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags)) {
664 module_printk(KERN_WARNING, "Span %s is already assigned.\n",
665 span->name);
666 return -EINVAL;
667 }
668
669
670 if (!span->ops->set_spantype) {
671 module_printk(KERN_WARNING, "Span %s does not support "
672 "setting type.\n", span->name);
673 return -EINVAL;
674 }
675
676 ret = span->ops->set_spantype(span, spantype);
677 return (ret < 0) ? ret : count;
678 }
679
680 static ssize_t
681 dahdi_registration_time_show(struct device *dev,
682 struct device_attribute *attr, char *buf)
683 {
684 struct dahdi_device *ddev = to_ddev(dev);
685 int count = 0;
686
687 count += sprintf(buf, "%010ld.%09ld\n",
688 ddev->registration_time.tv_sec,
689 ddev->registration_time.tv_nsec);
690 return count;
691 }
692
693 static struct device_attribute dahdi_device_attrs[] = {
694 __ATTR(manufacturer, S_IRUGO, dahdi_device_manufacturer_show, NULL),
695 __ATTR(type, S_IRUGO, dahdi_device_type_show, NULL),
696 __ATTR(span_count, S_IRUGO, dahdi_device_span_count_show, NULL),
697 __ATTR(hardware_id, S_IRUGO, dahdi_device_hardware_id_show, NULL),
698 __ATTR(location, S_IRUGO, dahdi_device_location_show, NULL),
699 __ATTR(auto_assign, S_IWUSR, NULL, dahdi_device_auto_assign),
700 __ATTR(assign_span, S_IWUSR, NULL, dahdi_device_assign_span),
701 __ATTR(unassign_span, S_IWUSR, NULL, dahdi_device_unassign_span),
702 __ATTR(spantype, S_IWUSR | S_IRUGO, dahdi_spantype_show,
703 dahdi_spantype_store),
704 __ATTR(registration_time, S_IRUGO, dahdi_registration_time_show, NULL),
705 __ATTR_NULL,
706 };
707
708 static struct bus_type dahdi_device_bus = {
709 .name = "dahdi_devices",
710 .uevent = device_uevent,
711 .dev_attrs = dahdi_device_attrs,
712 };
713
714 static void dahdi_sysfs_cleanup(void)
715 {
716 dahdi_dbg(DEVICES, "SYSFS\n");
717 if (should_cleanup.clean_dahdi_driver) {
718 dahdi_dbg(DEVICES, "Unregister driver\n");
719 driver_unregister(&dahdi_driver);
720 should_cleanup.clean_dahdi_driver = 0;
721 }
722 if (should_cleanup.clean_span_bus_type) {
723 dahdi_dbg(DEVICES, "Unregister span bus type\n");
724 bus_unregister(&spans_bus_type);
725 should_cleanup.clean_span_bus_type = 0;
726 }
727 dahdi_sysfs_chan_exit();
728 if (should_cleanup.clean_chardev) {
729 dahdi_dbg(DEVICES, "Unregister character device\n");
730 unregister_chrdev(DAHDI_MAJOR, "dahdi");
731 should_cleanup.clean_chardev = 0;
732 }
733
734 if (should_cleanup.clean_device_bus) {
735 dahdi_dbg(DEVICES, "Unregister DAHDI device bus\n");
736 bus_unregister(&dahdi_device_bus);
737 should_cleanup.clean_device_bus = 0;
738 }
739 }
740
741 static void dahdi_device_release(struct device *dev)
742 {
743 struct dahdi_device *ddev = container_of(dev, struct dahdi_device, dev);
744 kfree(ddev);
745 }
746
747 /**
748 * dahdi_sysfs_add_device - Add the dahdi_device into the sysfs hierarchy.
749 * @ddev: The device to add.
750 * @parent: The physical device that is implementing this device.
751 *
752 * By adding the dahdi_device to the sysfs hierarchy user space can control
753 * how spans are numbered.
754 *
755 */
756 int dahdi_sysfs_add_device(struct dahdi_device *ddev, struct device *parent)
757 {
758 int ret;
759 struct device *const dev = &ddev->dev;
760 const char *dn;
761
762 dev->parent = parent;
763 dev->bus = &dahdi_device_bus;
764 dn = dev_name(dev);
765 if (!dn || !*dn) {
766 /* Invent default name based on parent */
767 if (!parent)
768 return -EINVAL;
769 dev_set_name(dev, "%s:%s", parent->bus->name, dev_name(parent));
770 }
771 ret = device_add(dev);
772 return ret;
773 }
774
775 void dahdi_sysfs_init_device(struct dahdi_device *ddev)
776 {
777 device_initialize(&ddev->dev);
778 ddev->dev.release = dahdi_device_release;
779 }
780
781 void dahdi_sysfs_unregister_device(struct dahdi_device *ddev)
782 {
783 device_del(&ddev->dev);
784 }
785
786 int __init dahdi_sysfs_init(const struct file_operations *dahdi_fops)
787 {
788 int res = 0;
789
790 dahdi_dbg(DEVICES, "Registering DAHDI device bus\n");
791
792 /* Handle dahdi-tools paths (for udev environment) */
793 if (tools_rootdir && initdir) {
794 dahdi_err("Cannot use tools-rootdir and initdir parameters simultaneously\n");
795 return -EINVAL;
796 }
797 if (initdir)
798 pr_notice("dahdi: initdir is depracated -- prefer using \"tools_rootdir\" parameter\n");
799 else
800 initdir = "/usr/share/dahdi";
801 if (!tools_rootdir)
802 tools_rootdir = "";
803
804 res = bus_register(&dahdi_device_bus);
805 if (res)
806 return res;
807 should_cleanup.clean_device_bus = 1;
808
809 dahdi_dbg(DEVICES,
810 "Registering character device (major=%d)\n", DAHDI_MAJOR);
811 res = register_chrdev(DAHDI_MAJOR, "dahdi", dahdi_fops);
812 if (res) {
813 module_printk(KERN_ERR,
814 "Unable to register DAHDI character device "
815 "handler on %d\n", DAHDI_MAJOR);
816 return res;
817 }
818 should_cleanup.clean_chardev = 1;
819
820 res = dahdi_sysfs_chan_init(dahdi_fops);
821 if (res)
822 goto cleanup;
823
824 res = bus_register(&spans_bus_type);
825 if (res) {
826 dahdi_err("%s: bus_register(%s) failed. Error number %d",
827 __func__, spans_bus_type.name, res);
828 goto cleanup;
829 }
830 should_cleanup.clean_span_bus_type = 1;
831
832 res = driver_register(&dahdi_driver);
833 if (res) {
834 dahdi_err("%s: driver_register(%s) failed. Error number %d",
835 __func__, dahdi_driver.name, res);
836 goto cleanup;
837 }
838 should_cleanup.clean_dahdi_driver = 1;
839
840 module_printk(KERN_INFO, "Telephony Interface Registered on major %d\n",
841 DAHDI_MAJOR);
842 return 0;
843
844 cleanup:
845 dahdi_sysfs_cleanup();
846 return res;
847 }
848
849 void dahdi_sysfs_exit(void)
850 {
851 dahdi_sysfs_cleanup();
852 }