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