1 /* This is the serial hardware link layer (HLL) for the Gigaset 307x isdn
2 * DECT base (aka Sinus 45 isdn) using the RS232 DECT data module M101,
3 * written as a line discipline.
5 * =====================================================================
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 * =====================================================================
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/platform_device.h>
18 #include <linux/tty.h>
19 #include <linux/completion.h>
20 #include <linux/slab.h>
22 /* Version Information */
23 #define DRIVER_AUTHOR "Tilman Schmidt"
24 #define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101"
26 #define GIGASET_MINORS 1
27 #define GIGASET_MINOR 0
28 #define GIGASET_MODULENAME "ser_gigaset"
29 #define GIGASET_DEVNAME "ttyGS"
31 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
32 #define IF_WRITEBUF 264
34 MODULE_AUTHOR(DRIVER_AUTHOR);
35 MODULE_DESCRIPTION(DRIVER_DESC);
36 MODULE_LICENSE("GPL");
37 MODULE_ALIAS_LDISC(N_GIGASET_M101);
39 static int startmode = SM_ISDN;
40 module_param(startmode, int, S_IRUGO);
41 MODULE_PARM_DESC(startmode, "initial operation mode");
42 static int cidmode = 1;
43 module_param(cidmode, int, S_IRUGO);
44 MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
46 static struct gigaset_driver *driver;
48 struct ser_cardstate {
49 struct platform_device dev;
50 struct tty_struct *tty;
52 struct completion dead_cmp;
55 static struct platform_driver device_driver = {
57 .name = GIGASET_MODULENAME,
61 static void flush_send_queue(struct cardstate *);
63 /* transmit data from current open skb
64 * result: number of bytes sent or error code < 0
66 static int write_modem(struct cardstate *cs)
68 struct tty_struct *tty = cs->hw.ser->tty;
69 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
70 struct sk_buff *skb = bcs->tx_skb;
71 int sent = -EOPNOTSUPP;
73 if (!tty || !tty->driver || !skb)
77 dev_kfree_skb_any(skb);
82 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
84 sent = tty->ops->write(tty, skb->data, skb->len);
85 gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
93 /* skb sent completely */
94 gigaset_skb_sent(bcs, skb);
96 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
98 dev_kfree_skb_any(skb);
105 * transmit first queued command buffer
106 * result: number of bytes sent or error code < 0
108 static int send_cb(struct cardstate *cs)
110 struct tty_struct *tty = cs->hw.ser->tty;
111 struct cmdbuf_t *cb, *tcb;
115 if (!tty || !tty->driver)
120 return 0; /* nothing to do */
123 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
124 sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
127 gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
128 flush_send_queue(cs);
133 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
134 sent, cb->len, cs->cmdbytes);
137 while (cb && !cb->len) {
138 spin_lock_irqsave(&cs->cmdlock, flags);
139 cs->cmdbytes -= cs->curlen;
141 cs->cmdbuf = cb = cb->next;
144 cs->curlen = cb->len;
146 cs->lastcmdbuf = NULL;
149 spin_unlock_irqrestore(&cs->cmdlock, flags);
151 if (tcb->wake_tasklet)
152 tasklet_schedule(tcb->wake_tasklet);
160 * If there is already a skb opened, put data to the transfer buffer
161 * by calling "write_modem".
162 * Otherwise take a new skb out of the queue.
164 static void gigaset_modem_fill(unsigned long data)
166 struct cardstate *cs = (struct cardstate *) data;
167 struct bc_state *bcs;
168 struct sk_buff *nextskb;
172 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
177 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
181 /* no skb is being sent; send command if any */
183 gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
185 /* something sent or error */
188 /* no command to send; get skb */
189 nextskb = skb_dequeue(&bcs->squeue);
191 /* no skb either, nothing to do */
193 bcs->tx_skb = nextskb;
195 gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
196 (unsigned long) bcs->tx_skb);
200 gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
201 if (write_modem(cs) < 0)
202 gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
206 * throw away all data queued for sending
208 static void flush_send_queue(struct cardstate *cs)
215 spin_lock_irqsave(&cs->cmdlock, flags);
216 while ((cb = cs->cmdbuf) != NULL) {
217 cs->cmdbuf = cb->next;
218 if (cb->wake_tasklet)
219 tasklet_schedule(cb->wake_tasklet);
222 cs->cmdbuf = cs->lastcmdbuf = NULL;
223 cs->cmdbytes = cs->curlen = 0;
224 spin_unlock_irqrestore(&cs->cmdlock, flags);
228 dev_kfree_skb_any(cs->bcs->tx_skb);
229 while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
230 dev_kfree_skb_any(skb);
234 /* Gigaset Driver Interface */
235 /* ======================== */
238 * queue an AT command string for transmission to the Gigaset device
240 * cs controller state structure
241 * buf buffer containing the string to send
242 * len number of characters to send
243 * wake_tasklet tasklet to run when transmission is complete, or NULL
245 * number of bytes queued, or error code < 0
247 static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
248 int len, struct tasklet_struct *wake_tasklet)
253 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
254 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
255 "CMD Transmit", len, buf);
260 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
262 dev_err(cs->dev, "%s: out of memory!\n", __func__);
266 memcpy(cb->buf, buf, len);
270 cb->wake_tasklet = wake_tasklet;
272 spin_lock_irqsave(&cs->cmdlock, flags);
273 cb->prev = cs->lastcmdbuf;
275 cs->lastcmdbuf->next = cb;
282 spin_unlock_irqrestore(&cs->cmdlock, flags);
284 spin_lock_irqsave(&cs->lock, flags);
286 tasklet_schedule(&cs->write_tasklet);
287 spin_unlock_irqrestore(&cs->lock, flags);
292 * tty_driver.write_room interface routine
293 * return number of characters the driver will accept to be written
295 * controller state structure
297 * number of characters
299 static int gigaset_write_room(struct cardstate *cs)
303 bytes = cs->cmdbytes;
304 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
308 * tty_driver.chars_in_buffer interface routine
309 * return number of characters waiting to be sent
311 * controller state structure
313 * number of characters
315 static int gigaset_chars_in_buffer(struct cardstate *cs)
321 * implementation of ioctl(GIGASET_BRKCHARS)
323 * controller state structure
325 * -EINVAL (unimplemented function)
327 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
329 /* not implemented */
335 * Called by "do_action" in ev-layer.c
337 static int gigaset_init_bchannel(struct bc_state *bcs)
339 /* nothing to do for M10x */
340 gigaset_bchannel_up(bcs);
346 * Called by "do_action" in ev-layer.c
348 static int gigaset_close_bchannel(struct bc_state *bcs)
350 /* nothing to do for M10x */
351 gigaset_bchannel_down(bcs);
356 * Set up B channel structure
357 * This is called by "gigaset_initcs" in common.c
359 static int gigaset_initbcshw(struct bc_state *bcs)
367 * Free B channel structure
368 * Called by "gigaset_freebcs" in common.c
370 static int gigaset_freebcshw(struct bc_state *bcs)
377 * Reinitialize B channel structure
378 * This is called by "bcs_reinit" in common.c
380 static void gigaset_reinitbcshw(struct bc_state *bcs)
382 /* nothing to do for M10x */
386 * Free hardware specific device data
387 * This will be called by "gigaset_freecs" in common.c
389 static void gigaset_freecshw(struct cardstate *cs)
391 tasklet_kill(&cs->write_tasklet);
394 dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
395 platform_device_unregister(&cs->hw.ser->dev);
400 static void gigaset_device_release(struct device *dev)
402 struct platform_device *pdev = to_platform_device(dev);
404 /* adapted from platform_device_release() in drivers/base/platform.c */
405 kfree(dev->platform_data);
406 kfree(pdev->resource);
410 * Set up hardware specific device data
411 * This is called by "gigaset_initcs" in common.c
413 static int gigaset_initcshw(struct cardstate *cs)
416 struct ser_cardstate *scs;
418 scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
420 pr_err("out of memory\n");
425 cs->hw.ser->dev.name = GIGASET_MODULENAME;
426 cs->hw.ser->dev.id = cs->minor_index;
427 cs->hw.ser->dev.dev.release = gigaset_device_release;
428 rc = platform_device_register(&cs->hw.ser->dev);
430 pr_err("error %d registering platform device\n", rc);
435 dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
437 tasklet_init(&cs->write_tasklet,
438 gigaset_modem_fill, (unsigned long) cs);
443 * set modem control lines
445 * card state structure
446 * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
447 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
448 * and by "if_lock" and "if_termios" in interface.c
450 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
453 struct tty_struct *tty = cs->hw.ser->tty;
454 unsigned int set, clear;
456 if (!tty || !tty->driver || !tty->ops->tiocmset)
458 set = new_state & ~old_state;
459 clear = old_state & ~new_state;
462 gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
463 return tty->ops->tiocmset(tty, NULL, set, clear);
466 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
471 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
476 static const struct gigaset_ops ops = {
479 gigaset_chars_in_buffer,
481 gigaset_init_bchannel,
482 gigaset_close_bchannel,
488 gigaset_set_modem_ctrl,
490 gigaset_set_line_ctrl,
491 gigaset_m10x_send_skb, /* asyncdata.c */
492 gigaset_m10x_input, /* asyncdata.c */
496 /* Line Discipline Interface */
497 /* ========================= */
499 /* helper functions for cardstate refcounting */
500 static struct cardstate *cs_get(struct tty_struct *tty)
502 struct cardstate *cs = tty->disc_data;
504 if (!cs || !cs->hw.ser) {
505 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
508 atomic_inc(&cs->hw.ser->refcnt);
512 static void cs_put(struct cardstate *cs)
514 if (atomic_dec_and_test(&cs->hw.ser->refcnt))
515 complete(&cs->hw.ser->dead_cmp);
519 * Called by the tty driver when the line discipline is pushed onto the tty.
520 * Called in process context.
523 gigaset_tty_open(struct tty_struct *tty)
525 struct cardstate *cs;
527 gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
529 pr_info(DRIVER_DESC "\n");
532 pr_err("%s: no driver structure\n", __func__);
536 /* allocate memory for our device state and intialize it */
537 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
541 cs->dev = &cs->hw.ser->dev.dev;
542 cs->hw.ser->tty = tty;
543 atomic_set(&cs->hw.ser->refcnt, 1);
544 init_completion(&cs->hw.ser->dead_cmp);
548 /* OK.. Initialization of the datastructures and the HW is done.. Now
549 * startup system and notify the LL that we are ready to run
551 if (startmode == SM_LOCKED)
552 cs->mstate = MS_LOCKED;
553 if (!gigaset_start(cs)) {
554 tasklet_kill(&cs->write_tasklet);
558 gig_dbg(DEBUG_INIT, "Startup of HLL done");
562 gig_dbg(DEBUG_INIT, "Startup of HLL failed");
563 tty->disc_data = NULL;
569 * Called by the tty driver when the line discipline is removed.
570 * Called from process context.
573 gigaset_tty_close(struct tty_struct *tty)
575 struct cardstate *cs = tty->disc_data;
577 gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
580 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
584 /* prevent other callers from entering ldisc methods */
585 tty->disc_data = NULL;
588 pr_err("%s: no hw cardstate\n", __func__);
590 /* wait for running methods to finish */
591 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
592 wait_for_completion(&cs->hw.ser->dead_cmp);
595 /* stop operations */
597 tasklet_kill(&cs->write_tasklet);
598 flush_send_queue(cs);
602 gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
606 * Called by the tty driver when the tty line is hung up.
607 * Wait for I/O to driver to complete and unregister ISDN device.
608 * This is already done by the close routine, so just call that.
609 * Called from process context.
611 static int gigaset_tty_hangup(struct tty_struct *tty)
613 gigaset_tty_close(tty);
619 * Unused, received data goes only to the Gigaset driver.
622 gigaset_tty_read(struct tty_struct *tty, struct file *file,
623 unsigned char __user *buf, size_t count)
630 * Unused, transmit data comes only from the Gigaset driver.
633 gigaset_tty_write(struct tty_struct *tty, struct file *file,
634 const unsigned char *buf, size_t count)
641 * Called in process context only.
642 * May be re-entered by multiple ioctl calling threads.
645 gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
646 unsigned int cmd, unsigned long arg)
648 struct cardstate *cs = cs_get(tty);
650 int __user *p = (int __user *)arg;
658 /* unused, always return zero */
660 rc = put_user(val, p);
664 /* flush our buffers and the serial port's buffer */
667 /* no own input buffer to flush */
671 flush_send_queue(cs);
677 /* pass through to underlying serial device */
678 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
686 * Called by the tty driver when a block of data has been received.
687 * Will not be re-entered while running but other ldisc functions
688 * may be called in parallel.
689 * Can be called from hard interrupt level as well as soft interrupt
693 * buf buffer containing received characters
694 * cflags buffer containing error flags for received characters (ignored)
695 * count number of received characters
698 gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
699 char *cflags, int count)
701 struct cardstate *cs = cs_get(tty);
702 unsigned tail, head, n;
703 struct inbuf_t *inbuf;
709 dev_err(cs->dev, "%s: no inbuf\n", __func__);
716 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
720 /* possible buffer wraparound */
721 n = min_t(unsigned, count, RBUFSIZE - tail);
722 memcpy(inbuf->data + tail, buf, n);
723 tail = (tail + n) % RBUFSIZE;
729 /* tail < head and some data left */
733 "inbuf overflow, discarding %d bytes\n",
737 memcpy(inbuf->data + tail, buf, count);
741 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
744 /* Everything was received .. Push data into handler */
745 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
746 gigaset_schedule_event(cs);
751 * Called by the tty driver when there's room for more data to send.
754 gigaset_tty_wakeup(struct tty_struct *tty)
756 struct cardstate *cs = cs_get(tty);
758 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
761 tasklet_schedule(&cs->write_tasklet);
765 static struct tty_ldisc_ops gigaset_ldisc = {
766 .owner = THIS_MODULE,
767 .magic = TTY_LDISC_MAGIC,
768 .name = "ser_gigaset",
769 .open = gigaset_tty_open,
770 .close = gigaset_tty_close,
771 .hangup = gigaset_tty_hangup,
772 .read = gigaset_tty_read,
773 .write = gigaset_tty_write,
774 .ioctl = gigaset_tty_ioctl,
775 .receive_buf = gigaset_tty_receive,
776 .write_wakeup = gigaset_tty_wakeup,
780 /* Initialization / Shutdown */
781 /* ========================= */
783 static int __init ser_gigaset_init(void)
787 gig_dbg(DEBUG_INIT, "%s", __func__);
788 rc = platform_driver_register(&device_driver);
790 pr_err("error %d registering platform driver\n", rc);
794 /* allocate memory for our driver state and intialize it */
795 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
796 GIGASET_MODULENAME, GIGASET_DEVNAME,
801 rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
803 pr_err("error %d registering line discipline\n", rc);
811 gigaset_freedriver(driver);
814 platform_driver_unregister(&device_driver);
818 static void __exit ser_gigaset_exit(void)
822 gig_dbg(DEBUG_INIT, "%s", __func__);
825 gigaset_freedriver(driver);
829 rc = tty_unregister_ldisc(N_GIGASET_M101);
831 pr_err("error %d unregistering line discipline\n", rc);
833 platform_driver_unregister(&device_driver);
836 module_init(ser_gigaset_init);
837 module_exit(ser_gigaset_exit);