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/poll.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 mutex dead_mutex
;
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
;
72 if (!tty
|| !tty
->driver
|| !skb
)
76 dev_kfree_skb_any(skb
);
81 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
82 sent
= tty
->driver
->write(tty
, skb
->data
, skb
->len
);
83 gig_dbg(DEBUG_OUTPUT
, "write_modem: sent %d", sent
);
91 /* skb sent completely */
92 gigaset_skb_sent(bcs
, skb
);
94 gig_dbg(DEBUG_INTR
, "kfree skb (Adr: %lx)!",
96 dev_kfree_skb_any(skb
);
103 * transmit first queued command buffer
104 * result: number of bytes sent or error code < 0
106 static int send_cb(struct cardstate
*cs
)
108 struct tty_struct
*tty
= cs
->hw
.ser
->tty
;
109 struct cmdbuf_t
*cb
, *tcb
;
113 if (!tty
|| !tty
->driver
)
118 return 0; /* nothing to do */
121 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
122 sent
= tty
->driver
->write(tty
, cb
->buf
+ cb
->offset
, cb
->len
);
125 gig_dbg(DEBUG_OUTPUT
, "send_cb: write error %d", sent
);
126 flush_send_queue(cs
);
131 gig_dbg(DEBUG_OUTPUT
, "send_cb: sent %d, left %u, queued %u",
132 sent
, cb
->len
, cs
->cmdbytes
);
135 while (cb
&& !cb
->len
) {
136 spin_lock_irqsave(&cs
->cmdlock
, flags
);
137 cs
->cmdbytes
-= cs
->curlen
;
139 cs
->cmdbuf
= cb
= cb
->next
;
142 cs
->curlen
= cb
->len
;
144 cs
->lastcmdbuf
= NULL
;
147 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
149 if (tcb
->wake_tasklet
)
150 tasklet_schedule(tcb
->wake_tasklet
);
158 * If there is already a skb opened, put data to the transfer buffer
159 * by calling "write_modem".
160 * Otherwise take a new skb out of the queue.
162 static void gigaset_modem_fill(unsigned long data
)
164 struct cardstate
*cs
= (struct cardstate
*) data
;
165 struct bc_state
*bcs
;
168 if (!cs
|| !(bcs
= cs
->bcs
)) {
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 if (!(bcs
->tx_skb
= skb_dequeue(&bcs
->squeue
)))
182 /* no skb either, nothing to do */
185 gig_dbg(DEBUG_INTR
, "Dequeued skb (Adr: %lx)",
186 (unsigned long) bcs
->tx_skb
);
190 gig_dbg(DEBUG_OUTPUT
, "%s: tx_skb", __func__
);
191 if (write_modem(cs
) < 0)
192 gig_dbg(DEBUG_OUTPUT
, "%s: write_modem failed", __func__
);
196 * throw away all data queued for sending
198 static void flush_send_queue(struct cardstate
*cs
)
205 spin_lock_irqsave(&cs
->cmdlock
, flags
);
206 while ((cb
= cs
->cmdbuf
) != NULL
) {
207 cs
->cmdbuf
= cb
->next
;
208 if (cb
->wake_tasklet
)
209 tasklet_schedule(cb
->wake_tasklet
);
212 cs
->cmdbuf
= cs
->lastcmdbuf
= NULL
;
213 cs
->cmdbytes
= cs
->curlen
= 0;
214 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
218 dev_kfree_skb_any(cs
->bcs
->tx_skb
);
219 while ((skb
= skb_dequeue(&cs
->bcs
->squeue
)) != NULL
)
220 dev_kfree_skb_any(skb
);
224 /* Gigaset Driver Interface */
225 /* ======================== */
228 * queue an AT command string for transmission to the Gigaset device
230 * cs controller state structure
231 * buf buffer containing the string to send
232 * len number of characters to send
233 * wake_tasklet tasklet to run when transmission is complete, or NULL
235 * number of bytes queued, or error code < 0
237 static int gigaset_write_cmd(struct cardstate
*cs
, const unsigned char *buf
,
238 int len
, struct tasklet_struct
*wake_tasklet
)
243 gigaset_dbg_buffer(atomic_read(&cs
->mstate
) != MS_LOCKED
?
244 DEBUG_TRANSCMD
: DEBUG_LOCKCMD
,
245 "CMD Transmit", len
, buf
);
250 if (!(cb
= kmalloc(sizeof(struct cmdbuf_t
) + len
, GFP_ATOMIC
))) {
251 dev_err(cs
->dev
, "%s: out of memory!\n", __func__
);
255 memcpy(cb
->buf
, buf
, len
);
259 cb
->wake_tasklet
= wake_tasklet
;
261 spin_lock_irqsave(&cs
->cmdlock
, flags
);
262 cb
->prev
= cs
->lastcmdbuf
;
264 cs
->lastcmdbuf
->next
= cb
;
271 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
273 spin_lock_irqsave(&cs
->lock
, flags
);
275 tasklet_schedule(&cs
->write_tasklet
);
276 spin_unlock_irqrestore(&cs
->lock
, flags
);
281 * tty_driver.write_room interface routine
282 * return number of characters the driver will accept to be written
284 * controller state structure
286 * number of characters
288 static int gigaset_write_room(struct cardstate
*cs
)
292 bytes
= cs
->cmdbytes
;
293 return bytes
< IF_WRITEBUF
? IF_WRITEBUF
- bytes
: 0;
297 * tty_driver.chars_in_buffer interface routine
298 * return number of characters waiting to be sent
300 * controller state structure
302 * number of characters
304 static int gigaset_chars_in_buffer(struct cardstate
*cs
)
310 * implementation of ioctl(GIGASET_BRKCHARS)
312 * controller state structure
314 * -EINVAL (unimplemented function)
316 static int gigaset_brkchars(struct cardstate
*cs
, const unsigned char buf
[6])
318 /* not implemented */
324 * Called by "do_action" in ev-layer.c
326 static int gigaset_init_bchannel(struct bc_state
*bcs
)
328 /* nothing to do for M10x */
329 gigaset_bchannel_up(bcs
);
335 * Called by "do_action" in ev-layer.c
337 static int gigaset_close_bchannel(struct bc_state
*bcs
)
339 /* nothing to do for M10x */
340 gigaset_bchannel_down(bcs
);
345 * Set up B channel structure
346 * This is called by "gigaset_initcs" in common.c
348 static int gigaset_initbcshw(struct bc_state
*bcs
)
356 * Free B channel structure
357 * Called by "gigaset_freebcs" in common.c
359 static int gigaset_freebcshw(struct bc_state
*bcs
)
366 * Reinitialize B channel structure
367 * This is called by "bcs_reinit" in common.c
369 static void gigaset_reinitbcshw(struct bc_state
*bcs
)
371 /* nothing to do for M10x */
375 * Free hardware specific device data
376 * This will be called by "gigaset_freecs" in common.c
378 static void gigaset_freecshw(struct cardstate
*cs
)
380 tasklet_kill(&cs
->write_tasklet
);
383 dev_set_drvdata(&cs
->hw
.ser
->dev
.dev
, NULL
);
384 platform_device_unregister(&cs
->hw
.ser
->dev
);
389 static void gigaset_device_release(struct device
*dev
)
391 struct platform_device
*pdev
=
392 container_of(dev
, struct platform_device
, dev
);
394 /* adapted from platform_device_release() in drivers/base/platform.c */
395 //FIXME is this actually necessary?
396 kfree(dev
->platform_data
);
397 kfree(pdev
->resource
);
401 * Set up hardware specific device data
402 * This is called by "gigaset_initcs" in common.c
404 static int gigaset_initcshw(struct cardstate
*cs
)
408 if (!(cs
->hw
.ser
= kzalloc(sizeof(struct ser_cardstate
), GFP_KERNEL
))) {
409 err("%s: out of memory!", __func__
);
413 cs
->hw
.ser
->dev
.name
= GIGASET_MODULENAME
;
414 cs
->hw
.ser
->dev
.id
= cs
->minor_index
;
415 cs
->hw
.ser
->dev
.dev
.release
= gigaset_device_release
;
416 if ((rc
= platform_device_register(&cs
->hw
.ser
->dev
)) != 0) {
417 err("error %d registering platform device", rc
);
422 dev_set_drvdata(&cs
->hw
.ser
->dev
.dev
, cs
);
424 tasklet_init(&cs
->write_tasklet
,
425 &gigaset_modem_fill
, (unsigned long) cs
);
430 * set modem control lines
432 * card state structure
433 * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
434 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
435 * and by "if_lock" and "if_termios" in interface.c
437 static int gigaset_set_modem_ctrl(struct cardstate
*cs
, unsigned old_state
, unsigned new_state
)
439 struct tty_struct
*tty
= cs
->hw
.ser
->tty
;
440 unsigned int set
, clear
;
442 if (!tty
|| !tty
->driver
|| !tty
->driver
->tiocmset
)
444 set
= new_state
& ~old_state
;
445 clear
= old_state
& ~new_state
;
448 gig_dbg(DEBUG_IF
, "tiocmset set %x clear %x", set
, clear
);
449 return tty
->driver
->tiocmset(tty
, NULL
, set
, clear
);
452 static int gigaset_baud_rate(struct cardstate
*cs
, unsigned cflag
)
457 static int gigaset_set_line_ctrl(struct cardstate
*cs
, unsigned cflag
)
462 static struct gigaset_ops ops
= {
465 gigaset_chars_in_buffer
,
467 gigaset_init_bchannel
,
468 gigaset_close_bchannel
,
474 gigaset_set_modem_ctrl
,
476 gigaset_set_line_ctrl
,
477 gigaset_m10x_send_skb
, /* asyncdata.c */
478 gigaset_m10x_input
, /* asyncdata.c */
482 /* Line Discipline Interface */
483 /* ========================= */
485 /* helper functions for cardstate refcounting */
486 static struct cardstate
*cs_get(struct tty_struct
*tty
)
488 struct cardstate
*cs
= tty
->disc_data
;
490 if (!cs
|| !cs
->hw
.ser
) {
491 gig_dbg(DEBUG_ANY
, "%s: no cardstate", __func__
);
494 atomic_inc(&cs
->hw
.ser
->refcnt
);
498 static void cs_put(struct cardstate
*cs
)
500 if (atomic_dec_and_test(&cs
->hw
.ser
->refcnt
))
501 mutex_unlock(&cs
->hw
.ser
->dead_mutex
);
505 * Called by the tty driver when the line discipline is pushed onto the tty.
506 * Called in process context.
509 gigaset_tty_open(struct tty_struct
*tty
)
511 struct cardstate
*cs
;
513 gig_dbg(DEBUG_INIT
, "Starting HLL for Gigaset M101");
519 err("%s: no driver structure", __func__
);
523 /* allocate memory for our device state and intialize it */
524 if (!(cs
= gigaset_initcs(driver
, 1, 1, 0, cidmode
,
525 GIGASET_MODULENAME
)))
528 cs
->dev
= &cs
->hw
.ser
->dev
.dev
;
529 cs
->hw
.ser
->tty
= tty
;
530 mutex_init(&cs
->hw
.ser
->dead_mutex
);
531 atomic_set(&cs
->hw
.ser
->refcnt
, 1);
535 /* OK.. Initialization of the datastructures and the HW is done.. Now
536 * startup system and notify the LL that we are ready to run
538 if (startmode
== SM_LOCKED
)
539 atomic_set(&cs
->mstate
, MS_LOCKED
);
540 if (!gigaset_start(cs
)) {
541 tasklet_kill(&cs
->write_tasklet
);
545 gig_dbg(DEBUG_INIT
, "Startup of HLL done");
546 mutex_lock(&cs
->hw
.ser
->dead_mutex
);
550 gig_dbg(DEBUG_INIT
, "Startup of HLL failed");
551 tty
->disc_data
= NULL
;
557 * Called by the tty driver when the line discipline is removed.
558 * Called from process context.
561 gigaset_tty_close(struct tty_struct
*tty
)
563 struct cardstate
*cs
= tty
->disc_data
;
565 gig_dbg(DEBUG_INIT
, "Stopping HLL for Gigaset M101");
568 gig_dbg(DEBUG_INIT
, "%s: no cardstate", __func__
);
572 /* prevent other callers from entering ldisc methods */
573 tty
->disc_data
= NULL
;
576 err("%s: no hw cardstate", __func__
);
578 /* wait for running methods to finish */
579 if (!atomic_dec_and_test(&cs
->hw
.ser
->refcnt
))
580 mutex_lock(&cs
->hw
.ser
->dead_mutex
);
583 /* stop operations */
585 tasklet_kill(&cs
->write_tasklet
);
586 flush_send_queue(cs
);
590 gig_dbg(DEBUG_INIT
, "Shutdown of HLL done");
594 * Called by the tty driver when the tty line is hung up.
595 * Wait for I/O to driver to complete and unregister ISDN device.
596 * This is already done by the close routine, so just call that.
597 * Called from process context.
599 static int gigaset_tty_hangup(struct tty_struct
*tty
)
601 gigaset_tty_close(tty
);
607 * Unused, received data goes only to the Gigaset driver.
610 gigaset_tty_read(struct tty_struct
*tty
, struct file
*file
,
611 unsigned char __user
*buf
, size_t count
)
618 * Unused, transmit data comes only from the Gigaset driver.
621 gigaset_tty_write(struct tty_struct
*tty
, struct file
*file
,
622 const unsigned char *buf
, size_t count
)
629 * Called in process context only.
630 * May be re-entered by multiple ioctl calling threads.
633 gigaset_tty_ioctl(struct tty_struct
*tty
, struct file
*file
,
634 unsigned int cmd
, unsigned long arg
)
636 struct cardstate
*cs
= cs_get(tty
);
638 int __user
*p
= (int __user
*)arg
;
646 /* pass through to underlying serial device */
647 rc
= n_tty_ioctl(tty
, file
, cmd
, arg
);
651 /* flush our buffers and the serial port's buffer */
654 /* no own input buffer to flush */
658 flush_send_queue(cs
);
661 /* flush the serial port's buffer */
662 rc
= n_tty_ioctl(tty
, file
, cmd
, arg
);
666 /* unused, always return zero */
668 rc
= put_user(val
, p
);
681 * Unused, always return zero.
684 gigaset_tty_poll(struct tty_struct
*tty
, struct file
*file
, poll_table
*wait
)
690 * Called by the tty driver when a block of data has been received.
691 * Will not be re-entered while running but other ldisc functions
692 * may be called in parallel.
693 * Can be called from hard interrupt level as well as soft interrupt
697 * buf buffer containing received characters
698 * cflags buffer containing error flags for received characters (ignored)
699 * count number of received characters
702 gigaset_tty_receive(struct tty_struct
*tty
, const unsigned char *buf
,
703 char *cflags
, int count
)
705 struct cardstate
*cs
= cs_get(tty
);
706 unsigned tail
, head
, n
;
707 struct inbuf_t
*inbuf
;
711 if (!(inbuf
= cs
->inbuf
)) {
712 dev_err(cs
->dev
, "%s: no inbuf\n", __func__
);
717 tail
= atomic_read(&inbuf
->tail
);
718 head
= atomic_read(&inbuf
->head
);
719 gig_dbg(DEBUG_INTR
, "buffer state: %u -> %u, receive %u bytes",
723 /* possible buffer wraparound */
724 n
= min_t(unsigned, count
, RBUFSIZE
- tail
);
725 memcpy(inbuf
->data
+ tail
, buf
, n
);
726 tail
= (tail
+ n
) % RBUFSIZE
;
732 /* tail < head and some data left */
736 "inbuf overflow, discarding %d bytes\n",
740 memcpy(inbuf
->data
+ tail
, buf
, count
);
744 gig_dbg(DEBUG_INTR
, "setting tail to %u", tail
);
745 atomic_set(&inbuf
->tail
, tail
);
747 /* Everything was received .. Push data into handler */
748 gig_dbg(DEBUG_INTR
, "%s-->BH", __func__
);
749 gigaset_schedule_event(cs
);
754 * Called by the tty driver when there's room for more data to send.
757 gigaset_tty_wakeup(struct tty_struct
*tty
)
759 struct cardstate
*cs
= cs_get(tty
);
761 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
764 tasklet_schedule(&cs
->write_tasklet
);
768 static struct tty_ldisc gigaset_ldisc
= {
769 .owner
= THIS_MODULE
,
770 .magic
= TTY_LDISC_MAGIC
,
771 .name
= "ser_gigaset",
772 .open
= gigaset_tty_open
,
773 .close
= gigaset_tty_close
,
774 .hangup
= gigaset_tty_hangup
,
775 .read
= gigaset_tty_read
,
776 .write
= gigaset_tty_write
,
777 .ioctl
= gigaset_tty_ioctl
,
778 .poll
= gigaset_tty_poll
,
779 .receive_buf
= gigaset_tty_receive
,
780 .write_wakeup
= gigaset_tty_wakeup
,
784 /* Initialization / Shutdown */
785 /* ========================= */
787 static int __init
ser_gigaset_init(void)
791 gig_dbg(DEBUG_INIT
, "%s", __func__
);
792 if ((rc
= platform_driver_register(&device_driver
)) != 0) {
793 err("error %d registering platform driver", rc
);
797 /* allocate memory for our driver state and intialize it */
798 if (!(driver
= gigaset_initdriver(GIGASET_MINOR
, GIGASET_MINORS
,
799 GIGASET_MODULENAME
, GIGASET_DEVNAME
,
803 if ((rc
= tty_register_ldisc(N_GIGASET_M101
, &gigaset_ldisc
)) != 0) {
804 err("error %d registering line discipline", rc
);
812 gigaset_freedriver(driver
);
815 platform_driver_unregister(&device_driver
);
819 static void __exit
ser_gigaset_exit(void)
823 gig_dbg(DEBUG_INIT
, "%s", __func__
);
826 gigaset_freedriver(driver
);
830 if ((rc
= tty_unregister_ldisc(N_GIGASET_M101
)) != 0)
831 err("error %d unregistering line discipline", rc
);
833 platform_driver_unregister(&device_driver
);
836 module_init(ser_gigaset_init
);
837 module_exit(ser_gigaset_exit
);