1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* This is the serial hardware link layer (HLL) for the Gigaset 307x isdn
3 * DECT base (aka Sinus 45 isdn) using the RS232 DECT data module M101,
4 * written as a line discipline.
6 * =====================================================================
7 * =====================================================================
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/platform_device.h>
14 #include <linux/completion.h>
16 /* Version Information */
17 #define DRIVER_AUTHOR "Tilman Schmidt"
18 #define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101"
20 #define GIGASET_MINORS 1
21 #define GIGASET_MINOR 0
22 #define GIGASET_MODULENAME "ser_gigaset"
23 #define GIGASET_DEVNAME "ttyGS"
25 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
26 #define IF_WRITEBUF 264
28 MODULE_AUTHOR(DRIVER_AUTHOR);
29 MODULE_DESCRIPTION(DRIVER_DESC);
30 MODULE_LICENSE("GPL");
31 MODULE_ALIAS_LDISC(N_GIGASET_M101);
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");
40 static struct gigaset_driver *driver;
42 struct ser_cardstate {
43 struct platform_device dev;
44 struct tty_struct *tty;
46 struct completion dead_cmp;
49 static struct platform_driver device_driver = {
51 .name = GIGASET_MODULENAME,
55 static void flush_send_queue(struct cardstate *);
57 /* transmit data from current open skb
58 * result: number of bytes sent or error code < 0
60 static int write_modem(struct cardstate *cs)
62 struct tty_struct *tty = cs->hw.ser->tty;
63 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
64 struct sk_buff *skb = bcs->tx_skb;
65 int sent = -EOPNOTSUPP;
67 WARN_ON(!tty || !tty->ops || !skb);
70 dev_kfree_skb_any(skb);
75 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
77 sent = tty->ops->write(tty, skb->data, skb->len);
78 gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
86 /* skb sent completely */
87 gigaset_skb_sent(bcs, skb);
89 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
91 dev_kfree_skb_any(skb);
98 * transmit first queued command buffer
99 * result: number of bytes sent or error code < 0
101 static int send_cb(struct cardstate *cs)
103 struct tty_struct *tty = cs->hw.ser->tty;
104 struct cmdbuf_t *cb, *tcb;
108 WARN_ON(!tty || !tty->ops);
112 return 0; /* nothing to do */
115 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
116 sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
119 gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
120 flush_send_queue(cs);
125 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
126 sent, cb->len, cs->cmdbytes);
129 while (cb && !cb->len) {
130 spin_lock_irqsave(&cs->cmdlock, flags);
131 cs->cmdbytes -= cs->curlen;
133 cs->cmdbuf = cb = cb->next;
136 cs->curlen = cb->len;
138 cs->lastcmdbuf = NULL;
141 spin_unlock_irqrestore(&cs->cmdlock, flags);
143 if (tcb->wake_tasklet)
144 tasklet_schedule(tcb->wake_tasklet);
152 * If there is already a skb opened, put data to the transfer buffer
153 * by calling "write_modem".
154 * Otherwise take a new skb out of the queue.
156 static void gigaset_modem_fill(unsigned long data)
158 struct cardstate *cs = (struct cardstate *) data;
159 struct bc_state *bcs;
160 struct sk_buff *nextskb;
164 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
169 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
173 /* no skb is being sent; send command if any */
175 gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
177 /* something sent or error */
180 /* no command to send; get skb */
181 nextskb = skb_dequeue(&bcs->squeue);
183 /* no skb either, nothing to do */
185 bcs->tx_skb = nextskb;
187 gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
188 (unsigned long) bcs->tx_skb);
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__);
198 * throw away all data queued for sending
200 static void flush_send_queue(struct cardstate *cs)
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);
214 cs->cmdbuf = cs->lastcmdbuf = NULL;
215 cs->cmdbytes = cs->curlen = 0;
216 spin_unlock_irqrestore(&cs->cmdlock, flags);
220 dev_kfree_skb_any(cs->bcs->tx_skb);
221 while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
222 dev_kfree_skb_any(skb);
226 /* Gigaset Driver Interface */
227 /* ======================== */
230 * queue an AT command string for transmission to the Gigaset device
232 * cs controller state structure
233 * buf buffer containing the string to send
234 * len number of characters to send
235 * wake_tasklet tasklet to run when transmission is complete, or NULL
237 * number of bytes queued, or error code < 0
239 static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
243 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
244 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
245 "CMD Transmit", cb->len, cb->buf);
247 spin_lock_irqsave(&cs->cmdlock, flags);
248 cb->prev = cs->lastcmdbuf;
250 cs->lastcmdbuf->next = cb;
253 cs->curlen = cb->len;
255 cs->cmdbytes += cb->len;
257 spin_unlock_irqrestore(&cs->cmdlock, flags);
259 spin_lock_irqsave(&cs->lock, flags);
261 tasklet_schedule(&cs->write_tasklet);
262 spin_unlock_irqrestore(&cs->lock, flags);
267 * tty_driver.write_room interface routine
268 * return number of characters the driver will accept to be written
270 * controller state structure
272 * number of characters
274 static int gigaset_write_room(struct cardstate *cs)
278 bytes = cs->cmdbytes;
279 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
283 * tty_driver.chars_in_buffer interface routine
284 * return number of characters waiting to be sent
286 * controller state structure
288 * number of characters
290 static int gigaset_chars_in_buffer(struct cardstate *cs)
296 * implementation of ioctl(GIGASET_BRKCHARS)
298 * controller state structure
300 * -EINVAL (unimplemented function)
302 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
304 /* not implemented */
310 * Called by "do_action" in ev-layer.c
312 static int gigaset_init_bchannel(struct bc_state *bcs)
314 /* nothing to do for M10x */
315 gigaset_bchannel_up(bcs);
321 * Called by "do_action" in ev-layer.c
323 static int gigaset_close_bchannel(struct bc_state *bcs)
325 /* nothing to do for M10x */
326 gigaset_bchannel_down(bcs);
331 * Set up B channel structure
332 * This is called by "gigaset_initcs" in common.c
334 static int gigaset_initbcshw(struct bc_state *bcs)
342 * Free B channel structure
343 * Called by "gigaset_freebcs" in common.c
345 static void gigaset_freebcshw(struct bc_state *bcs)
351 * Reinitialize B channel structure
352 * This is called by "bcs_reinit" in common.c
354 static void gigaset_reinitbcshw(struct bc_state *bcs)
356 /* nothing to do for M10x */
360 * Free hardware specific device data
361 * This will be called by "gigaset_freecs" in common.c
363 static void gigaset_freecshw(struct cardstate *cs)
365 tasklet_kill(&cs->write_tasklet);
368 platform_device_unregister(&cs->hw.ser->dev);
371 static void gigaset_device_release(struct device *dev)
373 kfree(container_of(dev, struct ser_cardstate, dev.dev));
377 * Set up hardware specific device data
378 * This is called by "gigaset_initcs" in common.c
380 static int gigaset_initcshw(struct cardstate *cs)
383 struct ser_cardstate *scs;
385 scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
387 pr_err("out of memory\n");
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);
397 pr_err("error %d registering platform device\n", rc);
403 tasklet_init(&cs->write_tasklet,
404 gigaset_modem_fill, (unsigned long) cs);
409 * set modem control lines
411 * card state structure
412 * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
413 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
414 * and by "if_lock" and "if_termios" in interface.c
416 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
419 struct tty_struct *tty = cs->hw.ser->tty;
420 unsigned int set, clear;
422 WARN_ON(!tty || !tty->ops);
423 /* tiocmset is an optional tty driver method */
424 if (!tty->ops->tiocmset)
426 set = new_state & ~old_state;
427 clear = old_state & ~new_state;
430 gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
431 return tty->ops->tiocmset(tty, set, clear);
434 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
439 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
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, /* asyncdata.c */
460 .handle_input = gigaset_m10x_input, /* asyncdata.c */
464 /* Line Discipline Interface */
465 /* ========================= */
467 /* helper functions for cardstate refcounting */
468 static struct cardstate *cs_get(struct tty_struct *tty)
470 struct cardstate *cs = tty->disc_data;
472 if (!cs || !cs->hw.ser) {
473 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
476 atomic_inc(&cs->hw.ser->refcnt);
480 static void cs_put(struct cardstate *cs)
482 if (atomic_dec_and_test(&cs->hw.ser->refcnt))
483 complete(&cs->hw.ser->dead_cmp);
487 * Called by the tty driver when the line discipline is pushed onto the tty.
488 * Called in process context.
491 gigaset_tty_open(struct tty_struct *tty)
493 struct cardstate *cs;
496 gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
498 pr_info(DRIVER_DESC "\n");
501 pr_err("%s: no driver structure\n", __func__);
505 /* allocate memory for our device state and initialize it */
506 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
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);
518 /* Set the amount of data we're willing to receive per call
519 * from the hardware driver to half of the input buffer size
520 * to leave some reserve.
521 * Note: We don't do flow control towards the hardware driver.
522 * If more data is received than will fit into the input buffer,
523 * it will be dropped and an error will be logged. This should
524 * never happen as the device is slow and the buffer size ample.
526 tty->receive_room = RBUFSIZE/2;
528 /* OK.. Initialization of the datastructures and the HW is done.. Now
529 * startup system and notify the LL that we are ready to run
531 if (startmode == SM_LOCKED)
532 cs->mstate = MS_LOCKED;
533 rc = gigaset_start(cs);
535 tasklet_kill(&cs->write_tasklet);
539 gig_dbg(DEBUG_INIT, "Startup of HLL done");
543 gig_dbg(DEBUG_INIT, "Startup of HLL failed");
544 tty->disc_data = NULL;
550 * Called by the tty driver when the line discipline is removed.
551 * Called from process context.
554 gigaset_tty_close(struct tty_struct *tty)
556 struct cardstate *cs = tty->disc_data;
558 gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
561 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
565 /* prevent other callers from entering ldisc methods */
566 tty->disc_data = NULL;
569 pr_err("%s: no hw cardstate\n", __func__);
571 /* wait for running methods to finish */
572 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
573 wait_for_completion(&cs->hw.ser->dead_cmp);
576 /* stop operations */
578 tasklet_kill(&cs->write_tasklet);
579 flush_send_queue(cs);
583 gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
587 * Called by the tty driver when the tty line is hung up.
588 * Wait for I/O to driver to complete and unregister ISDN device.
589 * This is already done by the close routine, so just call that.
590 * Called from process context.
592 static int gigaset_tty_hangup(struct tty_struct *tty)
594 gigaset_tty_close(tty);
600 * Called in process context only.
601 * May be re-entered by multiple ioctl calling threads.
604 gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
605 unsigned int cmd, unsigned long arg)
607 struct cardstate *cs = cs_get(tty);
609 int __user *p = (int __user *)arg;
617 /* unused, always return zero */
619 rc = put_user(val, p);
623 /* flush our buffers and the serial port's buffer */
626 /* no own input buffer to flush */
630 flush_send_queue(cs);
636 /* pass through to underlying serial device */
637 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
645 * Called by the tty driver when a block of data has been received.
646 * Will not be re-entered while running but other ldisc functions
647 * may be called in parallel.
648 * Can be called from hard interrupt level as well as soft interrupt
652 * buf buffer containing received characters
653 * cflags buffer containing error flags for received characters (ignored)
654 * count number of received characters
657 gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
658 char *cflags, int count)
660 struct cardstate *cs = cs_get(tty);
661 unsigned tail, head, n;
662 struct inbuf_t *inbuf;
668 dev_err(cs->dev, "%s: no inbuf\n", __func__);
675 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
679 /* possible buffer wraparound */
680 n = min_t(unsigned, count, RBUFSIZE - tail);
681 memcpy(inbuf->data + tail, buf, n);
682 tail = (tail + n) % RBUFSIZE;
688 /* tail < head and some data left */
692 "inbuf overflow, discarding %d bytes\n",
696 memcpy(inbuf->data + tail, buf, count);
700 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
703 /* Everything was received .. Push data into handler */
704 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
705 gigaset_schedule_event(cs);
710 * Called by the tty driver when there's room for more data to send.
713 gigaset_tty_wakeup(struct tty_struct *tty)
715 struct cardstate *cs = cs_get(tty);
717 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
720 tasklet_schedule(&cs->write_tasklet);
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,
737 /* Initialization / Shutdown */
738 /* ========================= */
740 static int __init ser_gigaset_init(void)
744 gig_dbg(DEBUG_INIT, "%s", __func__);
745 rc = platform_driver_register(&device_driver);
747 pr_err("error %d registering platform driver\n", rc);
751 /* allocate memory for our driver state and initialize it */
752 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
753 GIGASET_MODULENAME, GIGASET_DEVNAME,
760 rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
762 pr_err("error %d registering line discipline\n", rc);
770 gigaset_freedriver(driver);
773 platform_driver_unregister(&device_driver);
777 static void __exit ser_gigaset_exit(void)
781 gig_dbg(DEBUG_INIT, "%s", __func__);
784 gigaset_freedriver(driver);
788 rc = tty_unregister_ldisc(N_GIGASET_M101);
790 pr_err("error %d unregistering line discipline\n", rc);
792 platform_driver_unregister(&device_driver);
795 module_init(ser_gigaset_init);
796 module_exit(ser_gigaset_exit);