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>
21 /* Version Information */
22 #define DRIVER_AUTHOR "Tilman Schmidt"
23 #define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101"
25 #define GIGASET_MINORS 1
26 #define GIGASET_MINOR 0
27 #define GIGASET_MODULENAME "ser_gigaset"
28 #define GIGASET_DEVNAME "ttyGS"
30 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
31 #define IF_WRITEBUF 264
33 MODULE_AUTHOR(DRIVER_AUTHOR);
34 MODULE_DESCRIPTION(DRIVER_DESC);
35 MODULE_LICENSE("GPL");
36 MODULE_ALIAS_LDISC(N_GIGASET_M101);
38 static int startmode = SM_ISDN;
39 module_param(startmode, int, S_IRUGO);
40 MODULE_PARM_DESC(startmode, "initial operation mode");
41 static int cidmode = 1;
42 module_param(cidmode, int, S_IRUGO);
43 MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
45 static struct gigaset_driver *driver;
47 struct ser_cardstate {
48 struct platform_device dev;
49 struct tty_struct *tty;
51 struct completion dead_cmp;
54 static struct platform_driver device_driver = {
56 .name = GIGASET_MODULENAME,
60 static void flush_send_queue(struct cardstate *);
62 /* transmit data from current open skb
63 * result: number of bytes sent or error code < 0
65 static int write_modem(struct cardstate *cs)
67 struct tty_struct *tty = cs->hw.ser->tty;
68 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
69 struct sk_buff *skb = bcs->tx_skb;
70 int sent = -EOPNOTSUPP;
72 if (!tty || !tty->driver || !skb)
76 dev_kfree_skb_any(skb);
81 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
83 sent = tty->ops->write(tty, skb->data, skb->len);
84 gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
92 /* skb sent completely */
93 gigaset_skb_sent(bcs, skb);
95 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
97 dev_kfree_skb_any(skb);
104 * transmit first queued command buffer
105 * result: number of bytes sent or error code < 0
107 static int send_cb(struct cardstate *cs)
109 struct tty_struct *tty = cs->hw.ser->tty;
110 struct cmdbuf_t *cb, *tcb;
114 if (!tty || !tty->driver)
119 return 0; /* nothing to do */
122 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
123 sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
126 gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
127 flush_send_queue(cs);
132 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
133 sent, cb->len, cs->cmdbytes);
136 while (cb && !cb->len) {
137 spin_lock_irqsave(&cs->cmdlock, flags);
138 cs->cmdbytes -= cs->curlen;
140 cs->cmdbuf = cb = cb->next;
143 cs->curlen = cb->len;
145 cs->lastcmdbuf = NULL;
148 spin_unlock_irqrestore(&cs->cmdlock, flags);
150 if (tcb->wake_tasklet)
151 tasklet_schedule(tcb->wake_tasklet);
159 * If there is already a skb opened, put data to the transfer buffer
160 * by calling "write_modem".
161 * Otherwise take a new skb out of the queue.
163 static void gigaset_modem_fill(unsigned long data)
165 struct cardstate *cs = (struct cardstate *) data;
166 struct bc_state *bcs;
167 struct sk_buff *nextskb;
171 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
176 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
180 /* no skb is being sent; send command if any */
182 gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
184 /* something sent or error */
187 /* no command to send; get skb */
188 nextskb = skb_dequeue(&bcs->squeue);
190 /* no skb either, nothing to do */
192 bcs->tx_skb = nextskb;
194 gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
195 (unsigned long) bcs->tx_skb);
199 gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
200 if (write_modem(cs) < 0)
201 gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
205 * throw away all data queued for sending
207 static void flush_send_queue(struct cardstate *cs)
214 spin_lock_irqsave(&cs->cmdlock, flags);
215 while ((cb = cs->cmdbuf) != NULL) {
216 cs->cmdbuf = cb->next;
217 if (cb->wake_tasklet)
218 tasklet_schedule(cb->wake_tasklet);
221 cs->cmdbuf = cs->lastcmdbuf = NULL;
222 cs->cmdbytes = cs->curlen = 0;
223 spin_unlock_irqrestore(&cs->cmdlock, flags);
227 dev_kfree_skb_any(cs->bcs->tx_skb);
228 while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
229 dev_kfree_skb_any(skb);
233 /* Gigaset Driver Interface */
234 /* ======================== */
237 * queue an AT command string for transmission to the Gigaset device
239 * cs controller state structure
240 * buf buffer containing the string to send
241 * len number of characters to send
242 * wake_tasklet tasklet to run when transmission is complete, or NULL
244 * number of bytes queued, or error code < 0
246 static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
247 int len, struct tasklet_struct *wake_tasklet)
252 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
253 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
254 "CMD Transmit", len, buf);
259 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
261 dev_err(cs->dev, "%s: out of memory!\n", __func__);
265 memcpy(cb->buf, buf, len);
269 cb->wake_tasklet = wake_tasklet;
271 spin_lock_irqsave(&cs->cmdlock, flags);
272 cb->prev = cs->lastcmdbuf;
274 cs->lastcmdbuf->next = cb;
281 spin_unlock_irqrestore(&cs->cmdlock, flags);
283 spin_lock_irqsave(&cs->lock, flags);
285 tasklet_schedule(&cs->write_tasklet);
286 spin_unlock_irqrestore(&cs->lock, flags);
291 * tty_driver.write_room interface routine
292 * return number of characters the driver will accept to be written
294 * controller state structure
296 * number of characters
298 static int gigaset_write_room(struct cardstate *cs)
302 bytes = cs->cmdbytes;
303 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
307 * tty_driver.chars_in_buffer interface routine
308 * return number of characters waiting to be sent
310 * controller state structure
312 * number of characters
314 static int gigaset_chars_in_buffer(struct cardstate *cs)
320 * implementation of ioctl(GIGASET_BRKCHARS)
322 * controller state structure
324 * -EINVAL (unimplemented function)
326 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
328 /* not implemented */
334 * Called by "do_action" in ev-layer.c
336 static int gigaset_init_bchannel(struct bc_state *bcs)
338 /* nothing to do for M10x */
339 gigaset_bchannel_up(bcs);
345 * Called by "do_action" in ev-layer.c
347 static int gigaset_close_bchannel(struct bc_state *bcs)
349 /* nothing to do for M10x */
350 gigaset_bchannel_down(bcs);
355 * Set up B channel structure
356 * This is called by "gigaset_initcs" in common.c
358 static int gigaset_initbcshw(struct bc_state *bcs)
366 * Free B channel structure
367 * Called by "gigaset_freebcs" in common.c
369 static int gigaset_freebcshw(struct bc_state *bcs)
376 * Reinitialize B channel structure
377 * This is called by "bcs_reinit" in common.c
379 static void gigaset_reinitbcshw(struct bc_state *bcs)
381 /* nothing to do for M10x */
385 * Free hardware specific device data
386 * This will be called by "gigaset_freecs" in common.c
388 static void gigaset_freecshw(struct cardstate *cs)
390 tasklet_kill(&cs->write_tasklet);
393 dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
394 platform_device_unregister(&cs->hw.ser->dev);
399 static void gigaset_device_release(struct device *dev)
401 struct platform_device *pdev = to_platform_device(dev);
403 /* adapted from platform_device_release() in drivers/base/platform.c */
404 kfree(dev->platform_data);
405 kfree(pdev->resource);
409 * Set up hardware specific device data
410 * This is called by "gigaset_initcs" in common.c
412 static int gigaset_initcshw(struct cardstate *cs)
415 struct ser_cardstate *scs;
417 scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
419 pr_err("out of memory\n");
424 cs->hw.ser->dev.name = GIGASET_MODULENAME;
425 cs->hw.ser->dev.id = cs->minor_index;
426 cs->hw.ser->dev.dev.release = gigaset_device_release;
427 rc = platform_device_register(&cs->hw.ser->dev);
429 pr_err("error %d registering platform device\n", rc);
434 dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
436 tasklet_init(&cs->write_tasklet,
437 gigaset_modem_fill, (unsigned long) cs);
442 * set modem control lines
444 * card state structure
445 * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
446 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
447 * and by "if_lock" and "if_termios" in interface.c
449 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
452 struct tty_struct *tty = cs->hw.ser->tty;
453 unsigned int set, clear;
455 if (!tty || !tty->driver || !tty->ops->tiocmset)
457 set = new_state & ~old_state;
458 clear = old_state & ~new_state;
461 gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
462 return tty->ops->tiocmset(tty, NULL, set, clear);
465 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
470 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
475 static const struct gigaset_ops ops = {
478 gigaset_chars_in_buffer,
480 gigaset_init_bchannel,
481 gigaset_close_bchannel,
487 gigaset_set_modem_ctrl,
489 gigaset_set_line_ctrl,
490 gigaset_m10x_send_skb, /* asyncdata.c */
491 gigaset_m10x_input, /* asyncdata.c */
495 /* Line Discipline Interface */
496 /* ========================= */
498 /* helper functions for cardstate refcounting */
499 static struct cardstate *cs_get(struct tty_struct *tty)
501 struct cardstate *cs = tty->disc_data;
503 if (!cs || !cs->hw.ser) {
504 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
507 atomic_inc(&cs->hw.ser->refcnt);
511 static void cs_put(struct cardstate *cs)
513 if (atomic_dec_and_test(&cs->hw.ser->refcnt))
514 complete(&cs->hw.ser->dead_cmp);
518 * Called by the tty driver when the line discipline is pushed onto the tty.
519 * Called in process context.
522 gigaset_tty_open(struct tty_struct *tty)
524 struct cardstate *cs;
526 gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
528 pr_info(DRIVER_DESC "\n");
531 pr_err("%s: no driver structure\n", __func__);
535 /* allocate memory for our device state and intialize it */
536 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
540 cs->dev = &cs->hw.ser->dev.dev;
541 cs->hw.ser->tty = tty;
542 atomic_set(&cs->hw.ser->refcnt, 1);
543 init_completion(&cs->hw.ser->dead_cmp);
547 /* OK.. Initialization of the datastructures and the HW is done.. Now
548 * startup system and notify the LL that we are ready to run
550 if (startmode == SM_LOCKED)
551 cs->mstate = MS_LOCKED;
552 if (!gigaset_start(cs)) {
553 tasklet_kill(&cs->write_tasklet);
557 gig_dbg(DEBUG_INIT, "Startup of HLL done");
561 gig_dbg(DEBUG_INIT, "Startup of HLL failed");
562 tty->disc_data = NULL;
568 * Called by the tty driver when the line discipline is removed.
569 * Called from process context.
572 gigaset_tty_close(struct tty_struct *tty)
574 struct cardstate *cs = tty->disc_data;
576 gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
579 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
583 /* prevent other callers from entering ldisc methods */
584 tty->disc_data = NULL;
587 pr_err("%s: no hw cardstate\n", __func__);
589 /* wait for running methods to finish */
590 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
591 wait_for_completion(&cs->hw.ser->dead_cmp);
594 /* stop operations */
596 tasklet_kill(&cs->write_tasklet);
597 flush_send_queue(cs);
601 gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
605 * Called by the tty driver when the tty line is hung up.
606 * Wait for I/O to driver to complete and unregister ISDN device.
607 * This is already done by the close routine, so just call that.
608 * Called from process context.
610 static int gigaset_tty_hangup(struct tty_struct *tty)
612 gigaset_tty_close(tty);
618 * Unused, received data goes only to the Gigaset driver.
621 gigaset_tty_read(struct tty_struct *tty, struct file *file,
622 unsigned char __user *buf, size_t count)
629 * Unused, transmit data comes only from the Gigaset driver.
632 gigaset_tty_write(struct tty_struct *tty, struct file *file,
633 const unsigned char *buf, size_t count)
640 * Called in process context only.
641 * May be re-entered by multiple ioctl calling threads.
644 gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
645 unsigned int cmd, unsigned long arg)
647 struct cardstate *cs = cs_get(tty);
649 int __user *p = (int __user *)arg;
657 /* unused, always return zero */
659 rc = put_user(val, p);
663 /* flush our buffers and the serial port's buffer */
666 /* no own input buffer to flush */
670 flush_send_queue(cs);
676 /* pass through to underlying serial device */
677 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
685 * Called by the tty driver when a block of data has been received.
686 * Will not be re-entered while running but other ldisc functions
687 * may be called in parallel.
688 * Can be called from hard interrupt level as well as soft interrupt
692 * buf buffer containing received characters
693 * cflags buffer containing error flags for received characters (ignored)
694 * count number of received characters
697 gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
698 char *cflags, int count)
700 struct cardstate *cs = cs_get(tty);
701 unsigned tail, head, n;
702 struct inbuf_t *inbuf;
708 dev_err(cs->dev, "%s: no inbuf\n", __func__);
715 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
719 /* possible buffer wraparound */
720 n = min_t(unsigned, count, RBUFSIZE - tail);
721 memcpy(inbuf->data + tail, buf, n);
722 tail = (tail + n) % RBUFSIZE;
728 /* tail < head and some data left */
732 "inbuf overflow, discarding %d bytes\n",
736 memcpy(inbuf->data + tail, buf, count);
740 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
743 /* Everything was received .. Push data into handler */
744 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
745 gigaset_schedule_event(cs);
750 * Called by the tty driver when there's room for more data to send.
753 gigaset_tty_wakeup(struct tty_struct *tty)
755 struct cardstate *cs = cs_get(tty);
757 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
760 tasklet_schedule(&cs->write_tasklet);
764 static struct tty_ldisc_ops gigaset_ldisc = {
765 .owner = THIS_MODULE,
766 .magic = TTY_LDISC_MAGIC,
767 .name = "ser_gigaset",
768 .open = gigaset_tty_open,
769 .close = gigaset_tty_close,
770 .hangup = gigaset_tty_hangup,
771 .read = gigaset_tty_read,
772 .write = gigaset_tty_write,
773 .ioctl = gigaset_tty_ioctl,
774 .receive_buf = gigaset_tty_receive,
775 .write_wakeup = gigaset_tty_wakeup,
779 /* Initialization / Shutdown */
780 /* ========================= */
782 static int __init ser_gigaset_init(void)
786 gig_dbg(DEBUG_INIT, "%s", __func__);
787 rc = platform_driver_register(&device_driver);
789 pr_err("error %d registering platform driver\n", rc);
793 /* allocate memory for our driver state and intialize it */
794 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
795 GIGASET_MODULENAME, GIGASET_DEVNAME,
800 rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
802 pr_err("error %d registering line discipline\n", rc);
810 gigaset_freedriver(driver);
813 platform_driver_unregister(&device_driver);
817 static void __exit ser_gigaset_exit(void)
821 gig_dbg(DEBUG_INIT, "%s", __func__);
824 gigaset_freedriver(driver);
828 rc = tty_unregister_ldisc(N_GIGASET_M101);
830 pr_err("error %d unregistering line discipline\n", rc);
832 platform_driver_unregister(&device_driver);
835 module_init(ser_gigaset_init);
836 module_exit(ser_gigaset_exit);