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