Debian Codegraph @

drivers/dahdi/trunkdev/trunkdev.c

raw symbols SCIP
1 /* DAHDI Virtual Trunk device driver.
2 * (C) 2022 by Harald Welte <[email protected]>
3 * for Osmocom retronetworking project
4 *
5 */
6
7 /* This virtual trunk device is a new character device. Contrary to
8 the per-channel devices, this is a per-span device for virtual E1
9 spans. Userspace reads and writes 32-byte E1 frames on it.
10
11 ioctl's are added to create and delete such virtual trunk devices
12 at runtime. Each virtual trunk device is limited to one span.
13
14 Opening trunkdev is similar to opening channels via /dev/dahd/chanw
15 works: All users open the same generic device node but then issue
16 an ioctl to specify which (named) trundev they actually want to open.
17
18 This avoids having to dynamically register minor device ids, creating
19 device nodes, etc.
20
21 The trunkdev is in RED + LOS alarm after creation. Only once a userspace
22 process opens the E1 frame side, those alarms cease.
23 */
24
25
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/kmod.h>
31 #include <linux/wait.h>
32 #include <linux/spinlock.h>
33
34 #include <dahdi/kernel.h>
35
36 #include "frame_fifo.h"
37 #include "../dahdi.h"
38
39 #define TRUNKDEV_FMT "trunkdev/%s/0"
40
41 #define ALL_RY_ALARMS (DAHDI_ALARM_RED | DAHDI_ALARM_YELLOW | \
42 DAHDI_ALARM_LFA | DAHDI_ALARM_LMFA)
43
44 struct dahdi_trunkdev {
45 /* pointer to 'file' _if_ somebody has opened us via DAHDI_TRUNKDEV_OPEM */
46 struct file *file;
47
48 /* the two E1 frame FIFOs between trunk-device and DAHDI code */
49 struct frame_fifo from_trunk;
50 struct frame_fifo to_trunk;
51
52 /* wait-queue for blocking read on trunk device */
53 wait_queue_head_t waitq;
54
55 struct {
56 struct dahdi_device *dev;
57 struct dahdi_span span;
58 struct dahdi_chan *chans[31];
59 /* [next rx byte] offset into chan[i]->readchunk */
60 unsigned int readchunk_idx;
61 /* [next tx byte] offset into chan[i]->writechunk */
62 unsigned int writechunk_idx;
63 } dahdi;
64 };
65
66 /* process one incoming E1 frame (32 bytes; one for each TS) */
67 static void _span_demux_one_frame(struct dahdi_trunkdev *td, const uint8_t *data)
68 {
69 struct dahdi_span *dspan = &td->dahdi.span;
70 unsigned int i;
71
72 /* TODO: TS0 handling */
73
74 /* de-multiplex one byte to each channel */
75 for (i = 0; i < ARRAY_SIZE(td->dahdi.chans); i++) {
76 struct dahdi_chan *chan = td->dahdi.chans[i];
77 chan->readchunk[td->dahdi.readchunk_idx] = data[1+i];
78 }
79 td->dahdi.readchunk_idx++;
80
81 /* DAHDI_CHUNKSIZE is 8, meaning every channel (timeslot) wants data for 8
82 * bytes (PCM samples) every time _dahdi_receive() is called */
83 if (td->dahdi.readchunk_idx == DAHDI_CHUNKSIZE) {
84 dahdi_receive(dspan);
85 td->dahdi.readchunk_idx = 0;
86 }
87 }
88
89 /* multiplex one E1 frame (32 bytes) and write it to 'out' */
90 static void _span_mux_one_frame(struct dahdi_trunkdev *td, uint8_t *out)
91 {
92 struct dahdi_span *dspan = &td->dahdi.span;
93 unsigned int i;
94
95 out[0] = 0; /* TODO: TS0 handling */
96
97 /* multiplex one byte of each channel into a frame */
98 for (i = 0; i < ARRAY_SIZE(td->dahdi.chans); i++) {
99 struct dahdi_chan *chan = td->dahdi.chans[i];
100 out[1+i] = chan->writechunk[td->dahdi.writechunk_idx];
101 }
102 td->dahdi.writechunk_idx++;
103
104 /* DAHDI_CHUNKSIZE is 8, meaning every channel (timeslot) has data for 8
105 * bytes (PCM samples) every time _dahdi_transmit() is called */
106 if (td->dahdi.writechunk_idx == DAHDI_CHUNKSIZE) {
107 dahdi_transmit(dspan);
108 td->dahdi.writechunk_idx = 0;
109 }
110 }
111
112
113
114
115 /*************************************************************************
116 * DAHDI driver integration
117 *************************************************************************/
118
119 /* configuration of the span itself */
120 static int td_d_spanconfig(struct file *file, struct dahdi_span *span,
121 struct dahdi_lineconfig *lc)
122 {
123 struct dahdi_trunkdev *td = container_of(span, struct dahdi_trunkdev, dahdi.span);
124 unsigned int i;
125
126 if (lc->sync < 0)
127 lc->sync = 0;
128 if (lc->sync > 1) {
129 // TODO: log
130 lc->sync = 0;
131 }
132
133 /* (re-)set some sane defaults */
134 for (i = 0; i < span->channels; i++) {
135 struct dahdi_chan *const chan = td->dahdi.chans[i];
136 chan->sigcap = DAHDI_SIG_CLEAR | DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
137 }
138
139 return 0;
140 }
141
142 /* configuration of a single channel within the span */
143 static int td_d_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
144 {
145 return 0;
146 }
147
148 /* start the span */
149 static int td_d_startup(struct file *file, struct dahdi_span *span)
150 {
151 //struct dahdi_trunkdev *td = container_of(span, struct dahdi_trunkdev, dahdi.span);
152 /* TODO: set alarms if trunkdev not open? */
153 return 0;
154 }
155
156 /* shut down / stop the span */
157 static int td_d_shutdown(struct dahdi_span *span)
158 {
159 //struct dahdi_trunkdev *td = container_of(span, struct dahdi_trunkdev, dahdi.span);
160 /* TODO: should trunkdev continue to read/write even if the dahdi side is shutdown */
161 return 0;
162 }
163
164 static const char *maint2str(int cmd)
165 {
166 switch (cmd) {
167 case DAHDI_MAINT_NONE: return "NONE";
168 case DAHDI_MAINT_LOCALLOOP: return "LOCALLOOP";
169 case DAHDI_MAINT_NETWORKPAYLOADLOOP: return "NETWORKPAYLOADLOOP";
170 default: return "UNKNOWN";
171 }
172 }
173
174 static int td_d_maint(struct dahdi_span *span, int cmd)
175 {
176 struct dahdi_trunkdev *td = container_of(span, struct dahdi_trunkdev, dahdi.span);
177
178 switch (cmd) {
179 case DAHDI_MAINT_NONE:
180 case DAHDI_MAINT_LOCALLOOP:
181 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
182 /* we don't need to do anything, as span->maintstat is set by the core,
183 * just acknowledge those supported modes here */
184 dev_info(&td->dahdi.dev->dev, "Setting maintenance mode to: %s\n", maint2str(cmd));
185 break;
186 default:
187 return -ENOSYS;
188 }
189 return 0;
190 }
191
192
193 /* called in hard_irq context with chan_lock held */
194 static void td_d_sync_tick(struct dahdi_span *span, int is_master)
195 {
196 struct dahdi_trunkdev *td = container_of(span, struct dahdi_trunkdev, dahdi.span);
197 unsigned int i;
198
199 /* we must have some other clock source than this span, due to the way
200 * how DAHDI works internally: This very sync_tick function is called when the
201 * master device receives data via _dahdi_receive(). As we are calling
202 * _dahdi_receive() via _span_demux_one_frame(), this would lead to re-entrancy */
203 BUG_ON(is_master);
204
205 /* trigger transmission + reception of E1 frames */
206 for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
207 uint8_t frame[32];
208 int rc;
209
210 switch (span->maintstat) {
211 case DAHDI_MAINT_NONE:
212 /* Trunk -> DAHDI direction */
213 rc = frame_fifo_out(&td->from_trunk, frame);
214 if (rc >= 0)
215 _span_demux_one_frame(td, frame);
216
217 /* Trunk <- DAHDI direction */
218 _span_mux_one_frame(td, frame);
219 frame_fifo_in(&td->to_trunk, frame);
220 break;
221 case DAHDI_MAINT_LOCALLOOP:
222 /* Loop frame from DAHDI back to itself */
223 _span_mux_one_frame(td, frame);
224 _span_demux_one_frame(td, frame);
225 break;
226 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
227 /* Loop frame from trunkdev back to itself */
228 rc = frame_fifo_out(&td->from_trunk, frame);
229 if (rc >= 0)
230 frame_fifo_in(&td->to_trunk, frame);
231 break;
232 }
233 }
234 /* wake up any processes doing blocking read or waiting in poll */
235 wake_up_interruptible(&td->waitq);
236 }
237
238 static const struct dahdi_span_ops trunkdev_span_ops = {
239 .owner = THIS_MODULE,
240 .spanconfig = td_d_spanconfig,
241 .chanconfig = td_d_chanconfig,
242 .startup = td_d_startup,
243 .shutdown = td_d_shutdown,
244 .maint = td_d_maint,
245 .sync_tick = td_d_sync_tick,
246 };
247
248
249 static void trunkdev_free_channels(struct dahdi_trunkdev *td)
250 {
251 unsigned int i;
252 for (i = 0; i < ARRAY_SIZE(td->dahdi.chans); i++) {
253 kfree(td->dahdi.chans[i]);
254 td->dahdi.chans[i] = NULL;
255 }
256 }
257
258 static int trunkdev_alloc_channels(struct dahdi_trunkdev *td)
259 {
260 unsigned int i;
261
262 for (i = 0; i < ARRAY_SIZE(td->dahdi.chans); i++) {
263 struct dahdi_chan *chan;
264 kfree(td->dahdi.chans[i]);
265 chan = td->dahdi.chans[i] = kzalloc(sizeof(*chan), GFP_KERNEL);
266 if (!chan) {
267 trunkdev_free_channels(td);
268 return -ENOMEM;
269 }
270 chan->pvt = td;
271 snprintf(chan->name, sizeof(chan->name)-1, "%s/%u",
272 td->dahdi.span.name, i+1);
273 chan->chanpos = i+1;
274 chan->sigcap = DAHDI_SIG_CLEAR | DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
275 }
276 return 0;
277 }
278
279 static int trunkdev_create(const char *name)
280 {
281 struct dahdi_trunkdev *td;
282 struct dahdi_device *ddev;
283 struct dahdi_span *dspan;
284 int ret = -EINVAL;
285 int rc;
286
287 td = kzalloc(sizeof(*td), GFP_KERNEL);
288 if (!td)
289 return -ENOMEM;
290
291 frame_fifo_init(&td->from_trunk, 0, NULL, td);
292 frame_fifo_init(&td->to_trunk, 0, NULL, td);
293 init_waitqueue_head(&td->waitq);
294
295 td->dahdi.dev = dahdi_create_device();
296 if (!td->dahdi.dev) {
297 printk(KERN_ERR "Error in dahdi_create_device()\b");
298 goto error;
299 }
300 ddev = td->dahdi.dev;
301 dev_set_name(&ddev->dev, "trunkdev_%s", name);
302 ddev->manufacturer = "Osmocom";
303 ddev->devicetype = "Virtual Trunk";
304 ddev->hardware_id = "none";
305 //ddev->location = FIXME;
306
307 dspan = &td->dahdi.span;
308 snprintf(dspan->name, sizeof(dspan->name), TRUNKDEV_FMT, name);
309 snprintf(dspan->desc, sizeof(dspan->desc), "Virtual trunk %s span 0", name);
310 dspan->channels = 31;
311 dspan->spantype = SPANTYPE_DIGITAL_E1;
312 dspan->deflaw = DAHDI_LAW_ALAW;
313 dspan->linecompat = DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
314 dspan->alarms = DAHDI_ALARM_RED | DAHDI_ALARM_LOS;
315 dspan->chans = td->dahdi.chans;
316 dspan->flags = 0;
317 dspan->offset = 0;
318 dspan->ops = &trunkdev_span_ops;
319 dspan->cannot_provide_timing = 1;
320
321 rc = trunkdev_alloc_channels(td);
322 if (rc) {
323 ret = -ENOMEM;
324 goto err_free_tdev;
325 }
326
327 list_add_tail(&dspan->device_node, &ddev->spans);
328
329 rc = dahdi_register_device(td->dahdi.dev, NULL);
330 if (rc) {
331 printk(KERN_ERR "Error in dahdi_register_device()\n");
332 goto err_free_chans;
333 }
334
335 return dspan->spanno;
336
337 err_free_chans:
338 trunkdev_free_channels(td);
339 err_free_tdev:
340 dahdi_free_device(td->dahdi.dev);
341 error:
342 kfree(td);
343 return ret;
344 }
345
346 static void trunkdev_free(struct dahdi_trunkdev *td)
347 {
348 if (td->file)
349 td->file->private_data = NULL;
350
351 /* will in turn call td_d_shutdown() */
352 dahdi_unregister_device(td->dahdi.dev);
353 trunkdev_free_channels(td);
354 kfree(td);
355 }
356
357
358 /*************************************************************************
359 * /dev/dahdi/trunkdev character device handling for a full trunk
360 *************************************************************************/
361
362 /* user has opened a trunkdev device */
363 static int dahdi_trunkdev_open(struct inode *inode, struct file *file)
364 {
365 const struct file_operations *original_fops;
366
367 printk(KERN_DEBUG "%s\n", __func__);
368
369 /* use our own file operations for this file, not the generic dahdi ones */
370 original_fops = file->f_op;
371 file->f_op = dahdi_trunkdev_fops;
372 file->private_data = NULL;
373 /* Under normal operation, this releases the reference on the DAHDI
374 * module that was created when the file was opened. dahdi_open is
375 * responsible for taking a reference out on this module before
376 * calling this function. */
377 module_put(original_fops->owner);
378
379 /* nothing else to do here, we wait for ioctls() to create new trunkdev
380 * or to associate the file handle with existing ones */
381 return 0;
382 }
383
384 /* user has closed the trunkdev device */
385 static int dahdi_trunkdev_release(struct inode *inode, struct file *file)
386 {
387 struct dahdi_trunkdev *td = file->private_data;
388 struct dahdi_span *dspan;
389 unsigned long flags;
390
391 printk(KERN_DEBUG "%s\n", __func__);
392
393 if (unlikely(!td))
394 return -ENXIO;
395
396 dspan = &td->dahdi.span;
397 /* signal RED alarm once the trunkdev user is gone */
398 spin_lock_irqsave(&dspan->lock, flags);
399 dspan->alarms &= ~ALL_RY_ALARMS;
400 dspan->alarms |= DAHDI_ALARM_RED | DAHDI_ALARM_LOS;
401 dahdi_alarm_notify(dspan);
402 spin_unlock_irqrestore(&dspan->lock, flags);
403
404 /* drop the reference taken during DAHDI_TRUNKDEV_OPEN */
405 put_span(dspan);
406
407 td->file = NULL;
408 file->private_data = NULL;
409
410 return 0;
411 }
412
413 static long dahdi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long data)
414 {
415 struct dahdi_trunkdev_create create;
416 struct dahdi_trunkdev_delete delete;
417 struct dahdi_trunkdev_open open;
418 char name[40];
419 struct dahdi_trunkdev *td;
420 struct dahdi_span *span;
421 unsigned long flags;
422 int res;
423
424 printk(KERN_DEBUG "%s(cmd=%u)\n", __func__, cmd);
425
426 switch (cmd) {
427 case DAHDI_TRUNKDEV_CREATE: /* create an new trunkdev/span */
428 printk(KERN_DEBUG "%s(cmd=DAHDI_TRUNKDEV_CREATE)\n", __func__);
429 if (copy_from_user(&create, (__user const void *) data, sizeof(create)))
430 return -EFAULT;
431 res = trunkdev_create(create.name);
432 if (res < 0)
433 return res;
434 create.spanno = res;
435 if (copy_to_user((__user void *) data, &create, sizeof(create))) {
436 trunkdev_free(td);
437 return -EFAULT;
438 }
439 /* increase module reference count to avoid module unload until all
440 * trunkdev have been deleted again */
441 if (!try_module_get(THIS_MODULE)) {
442 trunkdev_free(td);
443 return -ENXIO;
444 }
445 return 0;
446
447 case DAHDI_TRUNKDEV_DELETE: /* destroy an existing trunkdev/span */
448 printk(KERN_DEBUG "%s(cmd=DAHDI_TRUNKDEV_DELETE)\n", __func__);
449 if (copy_from_user(&delete, (__user const void *) data, sizeof(delete)))
450 return -EFAULT;
451 snprintf(name, sizeof(name), TRUNKDEV_FMT, delete.name);
452 span = dahdi_span_find_by_name_and_get(name);
453 if (!span)
454 return -ENODEV;
455 if (span->ops != &trunkdev_span_ops) {
456 /* the span with this name is not a trunkdev span */
457 put_span(span);
458 return -EINVAL;
459 }
460 td = container_of(span, struct dahdi_trunkdev, dahdi.span);
461 if (td->file) {
462 /* some other file already opened this span */
463 put_span(span);
464 return -EBUSY;
465 }
466 /* FIXME: some kind of mutex required? */
467 /* release the reference we obtained above before free'ing underlying memory */
468 put_span(span);
469 trunkdev_free(td);
470 /* drop the reference we hold from TRUNKDEV_CREATE time */
471 module_put(THIS_MODULE);
472 return 0;
473
474 case DAHDI_TRUNKDEV_OPEN: /* open a trunkdev/span */
475 printk(KERN_DEBUG "%s(cmd=DAHDI_TRUNKDEV_OPEN)\n", __func__);
476 if (unlikely(file->private_data))
477 return -EBUSY;
478 if (copy_from_user(&open, (__user const void *) data, sizeof(open)))
479 return -EFAULT;
480 snprintf(name, sizeof(name), TRUNKDEV_FMT, open.name);
481 span = dahdi_span_find_by_name_and_get(name);
482 if (!span)
483 return -ENODEV;
484 if (span->ops != &trunkdev_span_ops) {
485 /* the span with this name is not a trunkdev span */
486 put_span(span);
487 return -EINVAL;
488 }
489 td = container_of(span, struct dahdi_trunkdev, dahdi.span);
490 if (td->file) {
491 /* some other file already opened this span */
492 put_span(span);
493 return -EBUSY;
494 }
495 /* FIXME: some kind of mutex required? */
496 file->private_data = td;
497 td->file = file;
498 /* remove all alarms */
499 spin_lock_irqsave(&span->lock, flags);
500 span->alarms &= ~(ALL_RY_ALARMS|DAHDI_ALARM_LOS);
501 dahdi_alarm_notify(span);
502 spin_unlock_irqrestore(&span->lock, flags);
503 /* FIFO should be full at this point, let's flush it as now we actually
504 * have a receiver */
505 frame_fifo_flush(&td->to_trunk);
506
507 open.spanno = span->spanno;
508 if (copy_to_user((__user void *) data, &open, sizeof(open)))
509 return -EFAULT;
510 return 0;
511 default:
512 printk(KERN_ERR "%s(invalid cmd=0x%x)\n", __func__, cmd);
513 return -EINVAL;
514 }
515 }
516
517 static ssize_t dahdi_trunkdev_read(struct file *file, char __user *usrbuf,
518 size_t count, loff_t *ppos)
519 {
520 struct dahdi_trunkdev *td = file->private_data;
521 size_t copied = 0;
522
523 if (unlikely(!td))
524 return -ENODEV;
525
526 if (unlikely(count < 1))
527 return -EINVAL;
528
529 if (unlikely(count % BYTES_PER_FRAME))
530 return -EINVAL;
531
532 while (copied < count) {
533 uint8_t frame[BYTES_PER_FRAME];
534 int rc;
535
536 rc = frame_fifo_out(&td->to_trunk, frame);
537 if (rc < 0) {
538 /* insufficient data available */
539 if (file->f_flags & O_NONBLOCK) {
540 if (copied == 0)
541 return -EAGAIN;
542 else
543 return copied;
544 }
545 /* blocking wait for frames in FIFO or for device disappearing */
546 rc = wait_event_interruptible(td->waitq,
547 (frame_fifo_frames(&td->to_trunk) || !file->private_data));
548 if (rc)
549 return rc;
550 /* device has disappeared */
551 if (!file->private_data)
552 return -ENODEV;
553 }
554 if (copy_to_user(usrbuf + copied, frame, sizeof(frame)))
555 return -EFAULT;
556 copied += sizeof(frame);
557 }
558
559 return copied;
560 }
561
562 static ssize_t dahdi_trunkdev_write(struct file *file, const char __user *usrbuf, size_t count, loff_t *ppos)
563 {
564 struct dahdi_trunkdev *td = file->private_data;
565 size_t copied = 0;
566
567 if (unlikely(!td))
568 return -ENODEV;
569
570 if (unlikely(count < 1))
571 return -EINVAL;
572
573 if (unlikely(count % BYTES_PER_FRAME))
574 return -EINVAL;
575
576 while (copied < count) {
577 uint8_t frame[BYTES_PER_FRAME];
578
579 if (copy_from_user(frame, usrbuf + copied, sizeof(frame)))
580 return -EFAULT;
581
582 frame_fifo_in(&td->from_trunk, frame);
583 copied += sizeof(frame);
584 /* TODO: we could detect overflow and start blocking? */
585 }
586
587 return copied;
588 }
589
590 static unsigned int dahdi_trunkdev_poll(struct file *file, struct poll_table_struct *wait_table)
591 {
592 struct dahdi_trunkdev *td = file->private_data;
593 unsigned int ret = 0;
594
595 if (unlikely(!td))
596 return POLLERR | POLLHUP | POLLRDHUP | POLLNVAL | POLLPRI;
597
598 poll_wait(file, &td->waitq, wait_table);
599
600 if (frame_fifo_frames(&td->to_trunk))
601 ret |= POLLIN | POLLRDNORM;
602
603 /* we currently always allow write to the fifo, as we don't detect overflows yet */
604 ret |= POLLOUT | POLLWRNORM;
605
606 return ret;
607 }
608
609 static const struct file_operations __dahdi_trunkdev_fops = {
610 .owner = THIS_MODULE,
611 .open = dahdi_trunkdev_open,
612 .release = dahdi_trunkdev_release,
613 .unlocked_ioctl = dahdi_unlocked_ioctl,
614 .read = dahdi_trunkdev_read,
615 .write = dahdi_trunkdev_write,
616 .poll = dahdi_trunkdev_poll,
617 };
618
619 static struct dahdi_chardev trunkdev_chardev = {
620 .name = "trunkdev",
621 .minor = DAHDI_TRUNKDEV,
622 };
623
624 static int __init dahdi_trunkdev_init(void)
625 {
626 int res;
627
628 if (dahdi_trunkdev_fops) {
629 printk(KERN_WARNING "dahdi_trunkdev_fops already set.\n");
630 return -EBUSY;
631 }
632
633 if ((res = dahdi_register_chardev(&trunkdev_chardev)))
634 return res;
635
636 dahdi_trunkdev_fops = (struct file_operations *) &__dahdi_trunkdev_fops;
637
638 printk(KERN_INFO "%s: Loaded.\n", THIS_MODULE->name);
639 return 0;
640 }
641
642 static void __exit dahdi_trunkdev_cleanup(void)
643 {
644 dahdi_unregister_chardev(&trunkdev_chardev);
645 dahdi_trunkdev_fops = NULL;
646 printk(KERN_INFO "%s: Unloaded.\n", THIS_MODULE->name);
647 }
648
649 MODULE_DESCRIPTION("DAHDI virtual trunk device");
650 MODULE_AUTHOR("Harald Welte <[email protected]>");
651 MODULE_LICENSE("GPL");
652
653 module_init(dahdi_trunkdev_init);
654 module_exit(dahdi_trunkdev_cleanup);