Debian Codegraph @

drivers/dahdi/dahdi-sysfs.c

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