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