Debian Codegraph @

drivers/dahdi/dahdi-sysfs-chan.c

raw symbols SCIP
1 /* dahdi-sysfs-chan.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/version.h>
24
25 #define DAHDI_PRINK_MACROS_USE_debug
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/ctype.h>
30 #include <dahdi/kernel.h>
31 #include "dahdi.h"
32 #include "dahdi-sysfs.h"
33
34 /* shortcuts, for code readability */
35 #define MAKE_DAHDI_DEV(num, name) \
36 device_create(dahdi_class, NULL, MKDEV(DAHDI_MAJOR, num), \
37 NULL, "%s", name)
38
39 #define DEL_DAHDI_DEV(num) \
40 device_destroy(dahdi_class, MKDEV(DAHDI_MAJOR, num))
41
42 static struct class *dahdi_class;
43
44 static dev_t dahdi_channels_devt; /*!< Device number of first channel */
45 static struct cdev dahdi_channels_cdev; /*!< Channels chardev's */
46
47 /*
48 * Flags to remember what initializations already
49 * succeeded.
50 */
51 static struct {
52 u32 channel_driver:1;
53 u32 channels_bus:1;
54 u32 cdev:1;
55 } should_cleanup;
56
57 #define chan_attr(field, format_string) \
58 static BUS_ATTR_READER(field##_show, dev, buf) \
59 { \
60 struct dahdi_chan *chan; \
61 \
62 chan = dev_to_chan(dev); \
63 return sprintf(buf, format_string, chan->field); \
64 }
65
66 chan_attr(name, "%s\n");
67 chan_attr(channo, "%d\n");
68 chan_attr(chanpos, "%d\n");
69 chan_attr(blocksize, "%d\n");
70 #ifdef OPTIMIZE_CHANMUTE
71 chan_attr(chanmute, "%d\n");
72 #endif
73
74 static BUS_ATTR_READER(sigcap_show, dev, buf)
75 {
76 struct dahdi_chan *chan;
77 int len = 0;
78 int i;
79 uint sigtypes[] = {
80 DAHDI_SIG_FXSLS,
81 DAHDI_SIG_FXSGS,
82 DAHDI_SIG_FXSKS,
83 DAHDI_SIG_FXOLS,
84 DAHDI_SIG_FXOGS,
85 DAHDI_SIG_FXOKS,
86 DAHDI_SIG_EM,
87 DAHDI_SIG_CLEAR,
88 DAHDI_SIG_HDLCRAW,
89 DAHDI_SIG_HDLCFCS,
90 DAHDI_SIG_HDLCNET,
91 DAHDI_SIG_SLAVE,
92 DAHDI_SIG_SF,
93 DAHDI_SIG_CAS,
94 DAHDI_SIG_EM_E1,
95 DAHDI_SIG_DACS_RBS,
96 DAHDI_SIG_HARDHDLC,
97 DAHDI_SIG_MTP2,
98 };
99 chan = dev_to_chan(dev);
100
101 for (i = 0; i < ARRAY_SIZE(sigtypes); i++) {
102 uint x = chan->sigcap & sigtypes[i];
103 if (x == sigtypes[i])
104 len += sprintf(buf + len, "%s ", sigstr(x));
105 }
106 while (len > 0 && isspace(buf[len - 1])) /* trim */
107 len--;
108 len += sprintf(buf + len, "\n");
109 return len;
110 }
111
112 static BUS_ATTR_READER(sig_show, dev, buf)
113 {
114 struct dahdi_chan *chan;
115
116 chan = dev_to_chan(dev);
117 return sprintf(buf, "%s\n", sigstr(chan->sig));
118 }
119
120 static BUS_ATTR_READER(in_use_show, dev, buf)
121 {
122 struct dahdi_chan *chan;
123
124 chan = dev_to_chan(dev);
125 return sprintf(buf, "%d\n", test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags));
126 }
127
128 static BUS_ATTR_READER(alarms_show, dev, buf)
129 {
130 struct dahdi_chan *chan;
131 int len;
132
133 chan = dev_to_chan(dev);
134 len = fill_alarm_string(buf, PAGE_SIZE, chan->chan_alarms);
135 buf[len++] = '\n';
136 return len;
137 }
138
139 static BUS_ATTR_READER(ec_factory_show, dev, buf)
140 {
141 struct dahdi_chan *chan;
142 int len = 0;
143
144 chan = dev_to_chan(dev);
145 if (chan->ec_factory)
146 len += sprintf(buf, "%s", chan->ec_factory->get_name(chan));
147 buf[len++] = '\n';
148 return len;
149 }
150
151 static BUS_ATTR_READER(ec_state_show, dev, buf)
152 {
153 struct dahdi_chan *chan;
154 int len = 0;
155
156 chan = dev_to_chan(dev);
157 if (chan->ec_factory)
158 len += sprintf(buf, "%sACTIVE", (chan->ec_state) ? "" : "IN");
159 buf[len++] = '\n';
160 return len;
161 }
162
163 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
164 static struct device_attribute chan_dev_attrs[] = {
165 __ATTR_RO(name),
166 __ATTR_RO(channo),
167 __ATTR_RO(chanpos),
168 __ATTR_RO(sig),
169 __ATTR_RO(sigcap),
170 __ATTR_RO(alarms),
171 __ATTR_RO(ec_factory),
172 __ATTR_RO(ec_state),
173 __ATTR_RO(blocksize),
174 #ifdef OPTIMIZE_CHANMUTE
175 __ATTR_RO(chanmute),
176 #endif
177 __ATTR_RO(in_use),
178 __ATTR_NULL,
179 };
180 #else
181 static DEVICE_ATTR_RO(name);
182 static DEVICE_ATTR_RO(channo);
183 static DEVICE_ATTR_RO(chanpos);
184 static DEVICE_ATTR_RO(sig);
185 static DEVICE_ATTR_RO(sigcap);
186 static DEVICE_ATTR_RO(alarms);
187 static DEVICE_ATTR_RO(ec_factory);
188 static DEVICE_ATTR_RO(ec_state);
189 static DEVICE_ATTR_RO(blocksize);
190 #ifdef OPTIMIZE_CHANMUTE
191 static DEVICE_ATTR_RO(chanmute);
192 #endif
193 static DEVICE_ATTR_RO(in_use);
194
195 static struct attribute *chan_dev_attrs[] = {
196 &dev_attr_name.attr,
197 &dev_attr_channo.attr,
198 &dev_attr_chanpos.attr,
199 &dev_attr_sig.attr,
200 &dev_attr_sigcap.attr,
201 &dev_attr_alarms.attr,
202 &dev_attr_ec_factory.attr,
203 &dev_attr_ec_state.attr,
204 &dev_attr_blocksize.attr,
205 #ifdef OPTIMIZE_CHANMUTE
206 &dev_attr_chanmute.attr,
207 #endif
208 &dev_attr_in_use.attr,
209 NULL,
210 };
211 ATTRIBUTE_GROUPS(chan_dev);
212 #endif
213
214 static void chan_release(struct device *dev)
215 {
216 struct dahdi_chan *chan;
217
218 BUG_ON(!dev);
219 chan = dev_to_chan(dev);
220 chan_dbg(DEVICES, chan, "SYSFS\n");
221 }
222
223 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
224 static int chan_match(struct device *dev, const struct device_driver *driver)
225 #else
226 static int chan_match(struct device *dev, struct device_driver *driver)
227 #endif /* LINUX_VERSION_CODE */
228 {
229 struct dahdi_chan *chan;
230
231 chan = dev_to_chan(dev);
232 chan_dbg(DEVICES, chan, "SYSFS\n");
233 return 1;
234 }
235
236 static struct bus_type chan_bus_type = {
237 .name = "dahdi_channels",
238 .match = chan_match,
239 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
240 .dev_attrs = chan_dev_attrs,
241 #else
242 .dev_groups = chan_dev_groups,
243 #endif
244 };
245
246 static int chan_probe(struct device *dev)
247 {
248 struct dahdi_chan *chan;
249
250 chan = dev_to_chan(dev);
251 chan_dbg(DEVICES, chan, "SYSFS\n");
252 return 0;
253 }
254
255 static int chan_remove(struct device *dev)
256 {
257 struct dahdi_chan *chan;
258
259 chan = dev_to_chan(dev);
260 chan_dbg(DEVICES, chan, "SYSFS\n");
261 return 0;
262 }
263
264 static struct device_driver chan_driver = {
265 .name = "dahdi",
266 .bus = &chan_bus_type,
267 #ifndef OLD_HOTPLUG_SUPPORT
268 .owner = THIS_MODULE,
269 #endif
270 .probe = chan_probe,
271 .remove = chan_remove
272 };
273
274 int chan_sysfs_create(struct dahdi_chan *chan)
275 {
276 struct device *dev;
277 struct dahdi_span *span;
278 int res;
279 dev_t devt;
280
281 chan_dbg(DEVICES, chan, "Creating channel %d\n", chan->channo);
282 if (test_bit(DAHDI_FLAGBIT_DEVFILE, &chan->flags))
283 return 0;
284 span = chan->span;
285 devt = MKDEV(MAJOR(dahdi_channels_devt), chan->channo);
286 dev = &chan->chan_device;
287 memset(dev, 0, sizeof(*dev));
288 dev->devt = devt;
289 dev->bus = &chan_bus_type;
290 dev->parent = span->span_device;
291 /*
292 * FIXME: the name cannot be longer than KOBJ_NAME_LEN
293 */
294 dev_set_name(dev, "dahdi!chan!%03d!%03d", span->spanno, chan->chanpos);
295 dev_set_drvdata(dev, chan);
296 dev->release = chan_release;
297 res = device_register(dev);
298 if (res) {
299 chan_err(chan, "%s: device_register failed: %d\n",
300 __func__, res);
301 put_device(dev);
302 return res;
303 }
304 set_bit(DAHDI_FLAGBIT_DEVFILE, &chan->flags);
305 return 0;
306 }
307
308 void chan_sysfs_remove(struct dahdi_chan *chan)
309 {
310 struct device *dev = &chan->chan_device;
311
312 chan_dbg(DEVICES, chan, "Destroying channel %d\n", chan->channo);
313 if (!dev_get_drvdata(dev))
314 return;
315 if (!test_bit(DAHDI_FLAGBIT_DEVFILE, &chan->flags))
316 return;
317 dev = &chan->chan_device;
318 BUG_ON(dev_get_drvdata(dev) != chan);
319 device_unregister(dev);
320 /* FIXME: should have been done earlier in dahdi_chan_unreg */
321 chan->channo = -1;
322 clear_bit(DAHDI_FLAGBIT_DEVFILE, &chan->flags);
323 }
324
325 /*
326 * Used by dahdi_transcode.c
327 */
328 int dahdi_register_chardev(struct dahdi_chardev *dev)
329 {
330 static const char *DAHDI_STRING = "dahdi!";
331 char *udevname;
332
333 udevname = kzalloc(strlen(dev->name) + sizeof(DAHDI_STRING) + 1,
334 GFP_KERNEL);
335 if (!udevname)
336 return -ENOMEM;
337
338 strcpy(udevname, DAHDI_STRING);
339 strcat(udevname, dev->name);
340 MAKE_DAHDI_DEV(dev->minor, udevname);
341 kfree(udevname);
342 return 0;
343 }
344 EXPORT_SYMBOL(dahdi_register_chardev);
345
346 /*
347 * Used by dahdi_transcode.c
348 */
349 int dahdi_unregister_chardev(struct dahdi_chardev *dev)
350 {
351 DEL_DAHDI_DEV(dev->minor);
352 return 0;
353 }
354 EXPORT_SYMBOL(dahdi_unregister_chardev);
355
356 /*--------- Sysfs Device handling ----*/
357
358 /*
359 * Describe fixed device files and maintain their
360 * pointer so fixed_devfiles_remove() can always be called
361 * and work cleanly
362 */
363 static struct {
364 int minor;
365 char *name;
366 void *dev; /* FIXME: wrong type because of old kernels */
367 } fixed_minors[] = {
368 { DAHDI_CTL, "dahdi!ctl", },
369 { DAHDI_TIMER, "dahdi!timer", },
370 { DAHDI_CHANNEL, "dahdi!channel",},
371 { DAHDI_PSEUDO, "dahdi!pseudo", },
372 };
373
374 /*
375 * Removes /dev/dahdi/{ctl,timer,channel,pseudo}
376 *
377 * It is safe to call it during initialization error handling,
378 * as it skips non existing objects.
379 */
380 static void fixed_devfiles_remove(void)
381 {
382 int i;
383
384 if (!dahdi_class)
385 return;
386 for (i = 0; i < ARRAY_SIZE(fixed_minors); i++) {
387 void *d = fixed_minors[i].dev;
388 if (d && !IS_ERR(d)) {
389 dahdi_dbg(DEVICES, "Removing fixed device file %s\n",
390 fixed_minors[i].name);
391 DEL_DAHDI_DEV(fixed_minors[i].minor);
392 }
393 }
394 }
395
396 /*
397 * Creates /dev/dahdi/{ctl,timer,channel,pseudo}
398 */
399 static int fixed_devfiles_create(void)
400 {
401 int i;
402 int res = 0;
403
404 if (!dahdi_class) {
405 dahdi_err("%s: dahdi_class is not initialized yet!\n",
406 __func__);
407 res = -ENODEV;
408 goto cleanup;
409 }
410 for (i = 0; i < ARRAY_SIZE(fixed_minors); i++) {
411 char *name = fixed_minors[i].name;
412 int minor = fixed_minors[i].minor;
413 void *dummy;
414
415 dahdi_dbg(DEVICES, "Making fixed device file %s\n", name);
416 dummy = (void *)MAKE_DAHDI_DEV(minor, name);
417 if (IS_ERR(dummy)) {
418 int res = PTR_ERR(dummy);
419
420 dahdi_err("%s: failed (%d: %s). Error: %d\n",
421 __func__, minor, name, res);
422 goto cleanup;
423 }
424 fixed_minors[i].dev = dummy;
425 }
426 return 0;
427 cleanup:
428 fixed_devfiles_remove();
429 return res;
430 }
431
432 /*
433 * Called during driver unload and while handling any error during
434 * driver load.
435 * Always clean any (and only) objects that were initialized (invariant)
436 */
437 static void sysfs_channels_cleanup(void)
438 {
439 if (should_cleanup.cdev) {
440 dahdi_dbg(DEVICES, "removing channels cdev\n");
441 cdev_del(&dahdi_channels_cdev);
442 should_cleanup.cdev = 0;
443 }
444 if (dahdi_channels_devt) {
445 dahdi_dbg(DEVICES, "unregistering chrdev_region\n");
446 unregister_chrdev_region(dahdi_channels_devt,
447 DAHDI_MAX_CHANNELS);
448 }
449
450 fixed_devfiles_remove();
451 if (dahdi_class) {
452 dahdi_dbg(DEVICES, "Destroying DAHDI class:\n");
453 class_destroy(dahdi_class);
454 dahdi_class = NULL;
455 }
456 if (should_cleanup.channel_driver) {
457 dahdi_dbg(DEVICES, "Removing channel driver\n");
458 driver_unregister(&chan_driver);
459 should_cleanup.channel_driver = 0;
460 }
461 if (should_cleanup.channels_bus) {
462 dahdi_dbg(DEVICES, "Removing channels bus\n");
463 bus_unregister(&chan_bus_type);
464 should_cleanup.channels_bus = 0;
465 }
466 }
467
468 int __init dahdi_sysfs_chan_init(const struct file_operations *fops)
469 {
470 int res = 0;
471
472 dahdi_dbg(DEVICES, "Registering channels bus\n");
473 res = bus_register(&chan_bus_type);
474 if (res) {
475 dahdi_err("%s: bus_register(%s) failed. Error number %d\n",
476 __func__, chan_bus_type.name, res);
477 goto cleanup;
478 }
479 should_cleanup.channels_bus = 1;
480
481 dahdi_dbg(DEVICES, "Registering channel driver\n");
482 res = driver_register(&chan_driver);
483 if (res) {
484 dahdi_err("%s: driver_register(%s) failed. Error number %d",
485 __func__, chan_driver.name, res);
486 goto cleanup;
487 }
488 should_cleanup.channel_driver = 1;
489
490 /* see linux kernel commit 1aaba11da9aa7d7d6b52a74d45b31cac118295a1 */
491 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
492 dahdi_class = class_create("dahdi");
493 #else
494 dahdi_class = class_create(THIS_MODULE, "dahdi");
495 #endif
496 if (IS_ERR(dahdi_class)) {
497 res = PTR_ERR(dahdi_class);
498 dahdi_err("%s: class_create(dahi_chan) failed. Error: %d\n",
499 __func__, res);
500 goto cleanup;
501 }
502 res = fixed_devfiles_create();
503 if (res)
504 goto cleanup;
505 dahdi_dbg(DEVICES, "allocating chrdev_region\n");
506 res = alloc_chrdev_region(&dahdi_channels_devt,
507 0,
508 DAHDI_MAX_CHANNELS,
509 "dahdi_channels");
510 if (res) {
511 dahdi_err("%s: Failed allocating chrdev for %d channels (%d)",
512 __func__, DAHDI_MAX_CHANNELS, res);
513 goto cleanup;
514 }
515 dahdi_dbg(DEVICES, "adding channels cdev\n");
516 cdev_init(&dahdi_channels_cdev, fops);
517 res = cdev_add(&dahdi_channels_cdev, dahdi_channels_devt,
518 DAHDI_MAX_CHANNELS);
519 if (res) {
520 dahdi_err("%s: cdev_add() failed (%d)", __func__, res);
521 goto cleanup;
522 }
523 should_cleanup.cdev = 1;
524 return 0;
525 cleanup:
526 sysfs_channels_cleanup();
527 return res;
528 }
529
530 void dahdi_sysfs_chan_exit(void)
531 {
532 sysfs_channels_cleanup();
533 }