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