1
2
3
4
5
6
7
8
9
10
11
12
13#include "gigaset.h"
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/platform_device.h>
18#include <linux/tty.h>
19#include <linux/poll.h>
20
21
22#define DRIVER_AUTHOR "Tilman Schmidt"
23#define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101"
24
25#define GIGASET_MINORS 1
26#define GIGASET_MINOR 0
27#define GIGASET_MODULENAME "ser_gigaset"
28#define GIGASET_DEVNAME "ttyGS"
29
30
31#define IF_WRITEBUF 264
32
33MODULE_AUTHOR(DRIVER_AUTHOR);
34MODULE_DESCRIPTION(DRIVER_DESC);
35MODULE_LICENSE("GPL");
36MODULE_ALIAS_LDISC(N_GIGASET_M101);
37
38static int startmode = SM_ISDN;
39module_param(startmode, int, S_IRUGO);
40MODULE_PARM_DESC(startmode, "initial operation mode");
41static int cidmode = 1;
42module_param(cidmode, int, S_IRUGO);
43MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
44
45static struct gigaset_driver *driver;
46
47struct ser_cardstate {
48 struct platform_device dev;
49 struct tty_struct *tty;
50 atomic_t refcnt;
51 struct mutex dead_mutex;
52};
53
54static struct platform_driver device_driver = {
55 .driver = {
56 .name = GIGASET_MODULENAME,
57 },
58};
59
60static void flush_send_queue(struct cardstate *);
61
62
63
64
65static int write_modem(struct cardstate *cs)
66{
67 struct tty_struct *tty = cs->hw.ser->tty;
68 struct bc_state *bcs = &cs->bcs[0];
69 struct sk_buff *skb = bcs->tx_skb;
70 int sent;
71
72 if (!tty || !tty->driver || !skb)
73 return -EFAULT;
74
75 if (!skb->len) {
76 dev_kfree_skb_any(skb);
77 bcs->tx_skb = NULL;
78 return -EINVAL;
79 }
80
81 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
82 sent = tty->driver->write(tty, skb->data, skb->len);
83 gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
84 if (sent < 0) {
85
86 flush_send_queue(cs);
87 return sent;
88 }
89 skb_pull(skb, sent);
90 if (!skb->len) {
91
92 gigaset_skb_sent(bcs, skb);
93
94 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
95 (unsigned long) skb);
96 dev_kfree_skb_any(skb);
97 bcs->tx_skb = NULL;
98 }
99 return sent;
100}
101
102
103
104
105
106static int send_cb(struct cardstate *cs)
107{
108 struct tty_struct *tty = cs->hw.ser->tty;
109 struct cmdbuf_t *cb, *tcb;
110 unsigned long flags;
111 int sent = 0;
112
113 if (!tty || !tty->driver)
114 return -EFAULT;
115
116 cb = cs->cmdbuf;
117 if (!cb)
118 return 0;
119
120 if (cb->len) {
121 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
122 sent = tty->driver->write(tty, cb->buf + cb->offset, cb->len);
123 if (sent < 0) {
124
125 gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
126 flush_send_queue(cs);
127 return sent;
128 }
129 cb->offset += sent;
130 cb->len -= sent;
131 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
132 sent, cb->len, cs->cmdbytes);
133 }
134
135 while (cb && !cb->len) {
136 spin_lock_irqsave(&cs->cmdlock, flags);
137 cs->cmdbytes -= cs->curlen;
138 tcb = cb;
139 cs->cmdbuf = cb = cb->next;
140 if (cb) {
141 cb->prev = NULL;
142 cs->curlen = cb->len;
143 } else {
144 cs->lastcmdbuf = NULL;
145 cs->curlen = 0;
146 }
147 spin_unlock_irqrestore(&cs->cmdlock, flags);
148
149 if (tcb->wake_tasklet)
150 tasklet_schedule(tcb->wake_tasklet);
151 kfree(tcb);
152 }
153 return sent;
154}
155
156
157
158
159
160
161
162static void gigaset_modem_fill(unsigned long data)
163{
164 struct cardstate *cs = (struct cardstate *) data;
165 struct bc_state *bcs;
166 int sent = 0;
167
168 if (!cs || !(bcs = cs->bcs)) {
169 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
170 return;
171 }
172 if (!bcs->tx_skb) {
173
174 sent = send_cb(cs);
175 gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
176 if (sent)
177
178 return;
179
180
181 if (!(bcs->tx_skb = skb_dequeue(&bcs->squeue)))
182
183 return;
184
185 gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
186 (unsigned long) bcs->tx_skb);
187 }
188
189
190 gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
191 if (write_modem(cs) < 0)
192 gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
193}
194
195
196
197
198static void flush_send_queue(struct cardstate *cs)
199{
200 struct sk_buff *skb;
201 struct cmdbuf_t *cb;
202 unsigned long flags;
203
204
205 spin_lock_irqsave(&cs->cmdlock, flags);
206 while ((cb = cs->cmdbuf) != NULL) {
207 cs->cmdbuf = cb->next;
208 if (cb->wake_tasklet)
209 tasklet_schedule(cb->wake_tasklet);
210 kfree(cb);
211 }
212 cs->cmdbuf = cs->lastcmdbuf = NULL;
213 cs->cmdbytes = cs->curlen = 0;
214 spin_unlock_irqrestore(&cs->cmdlock, flags);
215
216
217 if (cs->bcs->tx_skb)
218 dev_kfree_skb_any(cs->bcs->tx_skb);
219 while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
220 dev_kfree_skb_any(skb);
221}
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
238 int len, struct tasklet_struct *wake_tasklet)
239{
240 struct cmdbuf_t *cb;
241 unsigned long flags;
242
243 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
244 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
245 "CMD Transmit", len, buf);
246
247 if (len <= 0)
248 return 0;
249
250 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
251 dev_err(cs->dev, "%s: out of memory!\n", __func__);
252 return -ENOMEM;
253 }
254
255 memcpy(cb->buf, buf, len);
256 cb->len = len;
257 cb->offset = 0;
258 cb->next = NULL;
259 cb->wake_tasklet = wake_tasklet;
260
261 spin_lock_irqsave(&cs->cmdlock, flags);
262 cb->prev = cs->lastcmdbuf;
263 if (cs->lastcmdbuf)
264 cs->lastcmdbuf->next = cb;
265 else {
266 cs->cmdbuf = cb;
267 cs->curlen = len;
268 }
269 cs->cmdbytes += len;
270 cs->lastcmdbuf = cb;
271 spin_unlock_irqrestore(&cs->cmdlock, flags);
272
273 spin_lock_irqsave(&cs->lock, flags);
274 if (cs->connected)
275 tasklet_schedule(&cs->write_tasklet);
276 spin_unlock_irqrestore(&cs->lock, flags);
277 return len;
278}
279
280
281
282
283
284
285
286
287
288static int gigaset_write_room(struct cardstate *cs)
289{
290 unsigned bytes;
291
292 bytes = cs->cmdbytes;
293 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
294}
295
296
297
298
299
300
301
302
303
304static int gigaset_chars_in_buffer(struct cardstate *cs)
305{
306 return cs->cmdbytes;
307}
308
309
310
311
312
313
314
315
316static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
317{
318
319 return -EINVAL;
320}
321
322
323
324
325
326static int gigaset_init_bchannel(struct bc_state *bcs)
327{
328
329 gigaset_bchannel_up(bcs);
330 return 0;
331}
332
333
334
335
336
337static int gigaset_close_bchannel(struct bc_state *bcs)
338{
339
340 gigaset_bchannel_down(bcs);
341 return 0;
342}
343
344
345
346
347
348static int gigaset_initbcshw(struct bc_state *bcs)
349{
350
351 bcs->hw.ser = NULL;
352 return 1;
353}
354
355
356
357
358
359static int gigaset_freebcshw(struct bc_state *bcs)
360{
361
362 return 1;
363}
364
365
366
367
368
369static void gigaset_reinitbcshw(struct bc_state *bcs)
370{
371
372}
373
374
375
376
377
378static void gigaset_freecshw(struct cardstate *cs)
379{
380 tasklet_kill(&cs->write_tasklet);
381 if (!cs->hw.ser)
382 return;
383 dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
384 platform_device_unregister(&cs->hw.ser->dev);
385 kfree(cs->hw.ser);
386 cs->hw.ser = NULL;
387}
388
389static void gigaset_device_release(struct device *dev)
390{
391 struct platform_device *pdev =
392 container_of(dev, struct platform_device, dev);
393
394
395
396 kfree(dev->platform_data);
397 kfree(pdev->resource);
398}
399
400
401
402
403
404static int gigaset_initcshw(struct cardstate *cs)
405{
406 int rc;
407
408 if (!(cs->hw.ser = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL))) {
409 err("%s: out of memory!", __func__);
410 return 0;
411 }
412
413 cs->hw.ser->dev.name = GIGASET_MODULENAME;
414 cs->hw.ser->dev.id = cs->minor_index;
415 cs->hw.ser->dev.dev.release = gigaset_device_release;
416 if ((rc = platform_device_register(&cs->hw.ser->dev)) != 0) {
417 err("error %d registering platform device", rc);
418 kfree(cs->hw.ser);
419 cs->hw.ser = NULL;
420 return 0;
421 }
422 dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
423
424 tasklet_init(&cs->write_tasklet,
425 &gigaset_modem_fill, (unsigned long) cs);
426 return 1;
427}
428
429
430
431
432
433
434
435
436
437static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state, unsigned new_state)
438{
439 struct tty_struct *tty = cs->hw.ser->tty;
440 unsigned int set, clear;
441
442 if (!tty || !tty->driver || !tty->driver->tiocmset)
443 return -EFAULT;
444 set = new_state & ~old_state;
445 clear = old_state & ~new_state;
446 if (!set && !clear)
447 return 0;
448 gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
449 return tty->driver->tiocmset(tty, NULL, set, clear);
450}
451
452static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
453{
454 return -EINVAL;
455}
456
457static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
458{
459 return -EINVAL;
460}
461
462static const struct gigaset_ops ops = {
463 gigaset_write_cmd,
464 gigaset_write_room,
465 gigaset_chars_in_buffer,
466 gigaset_brkchars,
467 gigaset_init_bchannel,
468 gigaset_close_bchannel,
469 gigaset_initbcshw,
470 gigaset_freebcshw,
471 gigaset_reinitbcshw,
472 gigaset_initcshw,
473 gigaset_freecshw,
474 gigaset_set_modem_ctrl,
475 gigaset_baud_rate,
476 gigaset_set_line_ctrl,
477 gigaset_m10x_send_skb,
478 gigaset_m10x_input,
479};
480
481
482
483
484
485
486static struct cardstate *cs_get(struct tty_struct *tty)
487{
488 struct cardstate *cs = tty->disc_data;
489
490 if (!cs || !cs->hw.ser) {
491 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
492 return NULL;
493 }
494 atomic_inc(&cs->hw.ser->refcnt);
495 return cs;
496}
497
498static void cs_put(struct cardstate *cs)
499{
500 if (atomic_dec_and_test(&cs->hw.ser->refcnt))
501 mutex_unlock(&cs->hw.ser->dead_mutex);
502}
503
504
505
506
507
508static int
509gigaset_tty_open(struct tty_struct *tty)
510{
511 struct cardstate *cs;
512
513 gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
514
515 info(DRIVER_AUTHOR);
516 info(DRIVER_DESC);
517
518 if (!driver) {
519 err("%s: no driver structure", __func__);
520 return -ENODEV;
521 }
522
523
524 if (!(cs = gigaset_initcs(driver, 1, 1, 0, cidmode,
525 GIGASET_MODULENAME)))
526 goto error;
527
528 cs->dev = &cs->hw.ser->dev.dev;
529 cs->hw.ser->tty = tty;
530 mutex_init(&cs->hw.ser->dead_mutex);
531 atomic_set(&cs->hw.ser->refcnt, 1);
532
533 tty->disc_data = cs;
534
535
536
537
538 if (startmode == SM_LOCKED)
539 atomic_set(&cs->mstate, MS_LOCKED);
540 if (!gigaset_start(cs)) {
541 tasklet_kill(&cs->write_tasklet);
542 goto error;
543 }
544
545 gig_dbg(DEBUG_INIT, "Startup of HLL done");
546 mutex_lock(&cs->hw.ser->dead_mutex);
547 return 0;
548
549error:
550 gig_dbg(DEBUG_INIT, "Startup of HLL failed");
551 tty->disc_data = NULL;
552 gigaset_freecs(cs);
553 return -ENODEV;
554}
555
556
557
558
559
560static void
561gigaset_tty_close(struct tty_struct *tty)
562{
563 struct cardstate *cs = tty->disc_data;
564
565 gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
566
567 if (!cs) {
568 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
569 return;
570 }
571
572
573 tty->disc_data = NULL;
574
575 if (!cs->hw.ser)
576 err("%s: no hw cardstate", __func__);
577 else {
578
579 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
580 mutex_lock(&cs->hw.ser->dead_mutex);
581 }
582
583
584 gigaset_stop(cs);
585 tasklet_kill(&cs->write_tasklet);
586 flush_send_queue(cs);
587 cs->dev = NULL;
588 gigaset_freecs(cs);
589
590 gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
591}
592
593
594
595
596
597
598
599static int gigaset_tty_hangup(struct tty_struct *tty)
600{
601 gigaset_tty_close(tty);
602 return 0;
603}
604
605
606
607
608
609static ssize_t
610gigaset_tty_read(struct tty_struct *tty, struct file *file,
611 unsigned char __user *buf, size_t count)
612{
613 return -EAGAIN;
614}
615
616
617
618
619
620static ssize_t
621gigaset_tty_write(struct tty_struct *tty, struct file *file,
622 const unsigned char *buf, size_t count)
623{
624 return -EAGAIN;
625}
626
627
628
629
630
631
632static int
633gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
634 unsigned int cmd, unsigned long arg)
635{
636 struct cardstate *cs = cs_get(tty);
637 int rc, val;
638 int __user *p = (int __user *)arg;
639
640 if (!cs)
641 return -ENXIO;
642
643 switch (cmd) {
644 case TCGETS:
645 case TCGETA:
646
647 rc = n_tty_ioctl(tty, file, cmd, arg);
648 break;
649
650 case TCFLSH:
651
652 switch (arg) {
653 case TCIFLUSH:
654
655 break;
656 case TCIOFLUSH:
657 case TCOFLUSH:
658 flush_send_queue(cs);
659 break;
660 }
661
662 rc = n_tty_ioctl(tty, file, cmd, arg);
663 break;
664
665 case FIONREAD:
666
667 val = 0;
668 rc = put_user(val, p);
669 break;
670
671 default:
672 rc = -ENOIOCTLCMD;
673 }
674
675 cs_put(cs);
676 return rc;
677}
678
679
680
681
682
683static unsigned int
684gigaset_tty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
685{
686 return 0;
687}
688
689
690
691
692
693
694
695
696
697
698
699
700
701static void
702gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
703 char *cflags, int count)
704{
705 struct cardstate *cs = cs_get(tty);
706 unsigned tail, head, n;
707 struct inbuf_t *inbuf;
708
709 if (!cs)
710 return;
711 if (!(inbuf = cs->inbuf)) {
712 dev_err(cs->dev, "%s: no inbuf\n", __func__);
713 cs_put(cs);
714 return;
715 }
716
717 tail = atomic_read(&inbuf->tail);
718 head = atomic_read(&inbuf->head);
719 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
720 head, tail, count);
721
722 if (head <= tail) {
723
724 n = min_t(unsigned, count, RBUFSIZE - tail);
725 memcpy(inbuf->data + tail, buf, n);
726 tail = (tail + n) % RBUFSIZE;
727 buf += n;
728 count -= n;
729 }
730
731 if (count > 0) {
732
733 n = head - tail - 1;
734 if (count > n) {
735 dev_err(cs->dev,
736 "inbuf overflow, discarding %d bytes\n",
737 count - n);
738 count = n;
739 }
740 memcpy(inbuf->data + tail, buf, count);
741 tail += count;
742 }
743
744 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
745 atomic_set(&inbuf->tail, tail);
746
747
748 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
749 gigaset_schedule_event(cs);
750 cs_put(cs);
751}
752
753
754
755
756static void
757gigaset_tty_wakeup(struct tty_struct *tty)
758{
759 struct cardstate *cs = cs_get(tty);
760
761 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
762 if (!cs)
763 return;
764 tasklet_schedule(&cs->write_tasklet);
765 cs_put(cs);
766}
767
768static struct tty_ldisc gigaset_ldisc = {
769 .owner = THIS_MODULE,
770 .magic = TTY_LDISC_MAGIC,
771 .name = "ser_gigaset",
772 .open = gigaset_tty_open,
773 .close = gigaset_tty_close,
774 .hangup = gigaset_tty_hangup,
775 .read = gigaset_tty_read,
776 .write = gigaset_tty_write,
777 .ioctl = gigaset_tty_ioctl,
778 .poll = gigaset_tty_poll,
779 .receive_buf = gigaset_tty_receive,
780 .write_wakeup = gigaset_tty_wakeup,
781};
782
783
784
785
786
787static int __init ser_gigaset_init(void)
788{
789 int rc;
790
791 gig_dbg(DEBUG_INIT, "%s", __func__);
792 if ((rc = platform_driver_register(&device_driver)) != 0) {
793 err("error %d registering platform driver", rc);
794 return rc;
795 }
796
797
798 if (!(driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
799 GIGASET_MODULENAME, GIGASET_DEVNAME,
800 &ops, THIS_MODULE)))
801 goto error;
802
803 if ((rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc)) != 0) {
804 err("error %d registering line discipline", rc);
805 goto error;
806 }
807
808 return 0;
809
810error:
811 if (driver) {
812 gigaset_freedriver(driver);
813 driver = NULL;
814 }
815 platform_driver_unregister(&device_driver);
816 return rc;
817}
818
819static void __exit ser_gigaset_exit(void)
820{
821 int rc;
822
823 gig_dbg(DEBUG_INIT, "%s", __func__);
824
825 if (driver) {
826 gigaset_freedriver(driver);
827 driver = NULL;
828 }
829
830 if ((rc = tty_unregister_ldisc(N_GIGASET_M101)) != 0)
831 err("error %d unregistering line discipline", rc);
832
833 platform_driver_unregister(&device_driver);
834}
835
836module_init(ser_gigaset_init);
837module_exit(ser_gigaset_exit);
838