1 /*********************************************************************
3 * Filename: irtty-sir.c
5 * Description: IrDA line discipline implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Dec 9 21:18:38 1997
9 * Modified at: Sun Oct 27 22:13:30 2002
10 * Modified by: Martin Diehl <mad@mdiehl.de>
11 * Sources: slip.c by Laurence Culhane, <loz@holmes.demon.co.uk>
12 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
14 * Copyright (c) 1998-2000 Dag Brattli,
15 * Copyright (c) 2002 Martin Diehl,
16 * All Rights Reserved.
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
23 * Neither Dag Brattli nor University of Tromsø admit liability nor
24 * provide warranty for any of this software. This material is
25 * provided "AS-IS" and at no charge.
27 ********************************************************************/
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/tty.h>
32 #include <linux/init.h>
33 #include <asm/uaccess.h>
34 #include <linux/smp_lock.h>
35 #include <linux/delay.h>
37 #include <net/irda/irda.h>
38 #include <net/irda/irda_device.h>
41 #include "irtty-sir.h"
43 static int qos_mtt_bits
= 0x03; /* 5 ms or more */
45 module_param(qos_mtt_bits
, int, 0);
46 MODULE_PARM_DESC(qos_mtt_bits
, "Minimum Turn Time");
48 /* ------------------------------------------------------- */
50 /* device configuration callbacks always invoked with irda-thread context */
52 /* find out, how many chars we have in buffers below us
53 * this is allowed to lie, i.e. return less chars than we
54 * actually have. The returned value is used to determine
55 * how long the irdathread should wait before doing the
56 * real blocking wait_until_sent()
59 static int irtty_chars_in_buffer(struct sir_dev
*dev
)
61 struct sirtty_cb
*priv
= dev
->priv
;
63 IRDA_ASSERT(priv
!= NULL
, return -1;);
64 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
66 return priv
->tty
->driver
->chars_in_buffer(priv
->tty
);
69 /* Wait (sleep) until underlaying hardware finished transmission
70 * i.e. hardware buffers are drained
71 * this must block and not return before all characters are really sent
73 * If the tty sits on top of a 16550A-like uart, there are typically
74 * up to 16 bytes in the fifo - f.e. 9600 bps 8N1 needs 16.7 msec
76 * With usbserial the uart-fifo is basically replaced by the converter's
77 * outgoing endpoint buffer, which can usually hold 64 bytes (at least).
78 * With pl2303 it appears we are safe with 60msec here.
80 * I really wish all serial drivers would provide
81 * correct implementation of wait_until_sent()
84 #define USBSERIAL_TX_DONE_DELAY 60
86 static void irtty_wait_until_sent(struct sir_dev
*dev
)
88 struct sirtty_cb
*priv
= dev
->priv
;
89 struct tty_struct
*tty
;
91 IRDA_ASSERT(priv
!= NULL
, return;);
92 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return;);
95 if (tty
->driver
->wait_until_sent
) {
97 tty
->driver
->wait_until_sent(tty
, msecs_to_jiffies(100));
101 msleep(USBSERIAL_TX_DONE_DELAY
);
106 * Function irtty_change_speed (dev, speed)
108 * Change the speed of the serial port.
110 * This may sleep in set_termios (usbserial driver f.e.) and must
111 * not be called from interrupt/timer/tasklet therefore.
112 * All such invocations are deferred to kIrDAd now so we can sleep there.
115 static int irtty_change_speed(struct sir_dev
*dev
, unsigned speed
)
117 struct sirtty_cb
*priv
= dev
->priv
;
118 struct tty_struct
*tty
;
119 struct termios old_termios
;
122 IRDA_ASSERT(priv
!= NULL
, return -1;);
123 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
128 old_termios
= *(tty
->termios
);
129 cflag
= tty
->termios
->c_cflag
;
133 IRDA_DEBUG(2, "%s(), Setting speed to %d\n", __FUNCTION__
, speed
);
163 tty
->termios
->c_cflag
= cflag
;
164 if (tty
->driver
->set_termios
)
165 tty
->driver
->set_termios(tty
, &old_termios
);
168 priv
->io
.speed
= speed
;
174 * Function irtty_set_dtr_rts (dev, dtr, rts)
176 * This function can be used by dongles etc. to set or reset the status
177 * of the dtr and rts lines
180 static int irtty_set_dtr_rts(struct sir_dev
*dev
, int dtr
, int rts
)
182 struct sirtty_cb
*priv
= dev
->priv
;
186 IRDA_ASSERT(priv
!= NULL
, return -1;);
187 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
199 * We can't use ioctl() because it expects a non-null file structure,
200 * and we don't have that here.
201 * This function is not yet defined for all tty driver, so
202 * let's be careful... Jean II
204 IRDA_ASSERT(priv
->tty
->driver
->tiocmset
!= NULL
, return -1;);
205 priv
->tty
->driver
->tiocmset(priv
->tty
, NULL
, set
, clear
);
210 /* ------------------------------------------------------- */
212 /* called from sir_dev when there is more data to send
213 * context is either netdev->hard_xmit or some transmit-completion bh
214 * i.e. we are under spinlock here and must not sleep.
217 static int irtty_do_write(struct sir_dev
*dev
, const unsigned char *ptr
, size_t len
)
219 struct sirtty_cb
*priv
= dev
->priv
;
220 struct tty_struct
*tty
;
223 IRDA_ASSERT(priv
!= NULL
, return -1;);
224 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
227 if (!tty
->driver
->write
)
229 tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
230 if (tty
->driver
->write_room
) {
231 writelen
= tty
->driver
->write_room(tty
);
237 return tty
->driver
->write(tty
, ptr
, writelen
);
240 /* ------------------------------------------------------- */
242 /* irda line discipline callbacks */
245 * Function irtty_receive_buf( tty, cp, count)
247 * Handle the 'receiver data ready' interrupt. This function is called
248 * by the 'tty_io' module in the kernel when a block of IrDA data has
249 * been received, which can now be decapsulated and delivered for
252 * calling context depends on underlying driver and tty->low_latency!
253 * for example (low_latency: 1 / 0):
254 * serial.c: uart-interrupt / softint
255 * usbserial: urb-complete-interrupt / softint
258 static void irtty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
262 struct sirtty_cb
*priv
= tty
->disc_data
;
265 IRDA_ASSERT(priv
!= NULL
, return;);
266 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return;);
268 if (unlikely(count
==0)) /* yes, this happens */
273 IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__
);
277 for (i
= 0; i
< count
; i
++) {
279 * Characters received with a parity error, etc?
282 IRDA_DEBUG(0, "Framing or parity error!\n");
283 sirdev_receive(dev
, NULL
, 0); /* notify sir_dev (updating stats) */
288 sirdev_receive(dev
, cp
, count
);
292 * Function irtty_receive_room (tty)
294 * Used by the TTY to find out how much data we can receive at a time
297 static int irtty_receive_room(struct tty_struct
*tty
)
299 struct sirtty_cb
*priv
= tty
->disc_data
;
301 IRDA_ASSERT(priv
!= NULL
, return 0;);
302 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return 0;);
304 return 65536; /* We can handle an infinite amount of data. :-) */
308 * Function irtty_write_wakeup (tty)
310 * Called by the driver when there's room for more data. If we have
311 * more packets to send, we send them here.
314 static void irtty_write_wakeup(struct tty_struct
*tty
)
316 struct sirtty_cb
*priv
= tty
->disc_data
;
318 IRDA_ASSERT(priv
!= NULL
, return;);
319 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return;);
321 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
324 sirdev_write_complete(priv
->dev
);
327 /* ------------------------------------------------------- */
330 * Function irtty_stop_receiver (tty, stop)
334 static inline void irtty_stop_receiver(struct tty_struct
*tty
, int stop
)
336 struct termios old_termios
;
340 old_termios
= *(tty
->termios
);
341 cflag
= tty
->termios
->c_cflag
;
348 tty
->termios
->c_cflag
= cflag
;
349 if (tty
->driver
->set_termios
)
350 tty
->driver
->set_termios(tty
, &old_termios
);
354 /*****************************************************************/
356 /* serialize ldisc open/close with sir_dev */
357 static DECLARE_MUTEX(irtty_sem
);
359 /* notifier from sir_dev when irda% device gets opened (ifup) */
361 static int irtty_start_dev(struct sir_dev
*dev
)
363 struct sirtty_cb
*priv
;
364 struct tty_struct
*tty
;
366 /* serialize with ldisc open/close */
370 if (unlikely(!priv
|| priv
->magic
!=IRTTY_MAGIC
)) {
377 if (tty
->driver
->start
)
378 tty
->driver
->start(tty
);
379 /* Make sure we can receive more data */
380 irtty_stop_receiver(tty
, FALSE
);
386 /* notifier from sir_dev when irda% device gets closed (ifdown) */
388 static int irtty_stop_dev(struct sir_dev
*dev
)
390 struct sirtty_cb
*priv
;
391 struct tty_struct
*tty
;
393 /* serialize with ldisc open/close */
397 if (unlikely(!priv
|| priv
->magic
!=IRTTY_MAGIC
)) {
404 /* Make sure we don't receive more data */
405 irtty_stop_receiver(tty
, TRUE
);
406 if (tty
->driver
->stop
)
407 tty
->driver
->stop(tty
);
414 /* ------------------------------------------------------- */
416 static struct sir_driver sir_tty_drv
= {
417 .owner
= THIS_MODULE
,
418 .driver_name
= "sir_tty",
419 .start_dev
= irtty_start_dev
,
420 .stop_dev
= irtty_stop_dev
,
421 .do_write
= irtty_do_write
,
422 .chars_in_buffer
= irtty_chars_in_buffer
,
423 .wait_until_sent
= irtty_wait_until_sent
,
424 .set_speed
= irtty_change_speed
,
425 .set_dtr_rts
= irtty_set_dtr_rts
,
428 /* ------------------------------------------------------- */
431 * Function irtty_ioctl (tty, file, cmd, arg)
433 * The Swiss army knife of system calls :-)
436 static int irtty_ioctl(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
438 struct irtty_info
{ char name
[6]; } info
;
440 struct sirtty_cb
*priv
= tty
->disc_data
;
443 IRDA_ASSERT(priv
!= NULL
, return -ENODEV
;);
444 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -EBADR
;);
446 IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __FUNCTION__
, cmd
);
449 IRDA_ASSERT(dev
!= NULL
, return -1;);
454 err
= n_tty_ioctl(tty
, file
, cmd
, arg
);
457 case IRTTY_IOCTDONGLE
:
458 /* this call blocks for completion */
459 err
= sirdev_set_dongle(dev
, (IRDA_DONGLE
) arg
);
463 IRDA_ASSERT(dev
->netdev
!= NULL
, return -1;);
465 memset(&info
, 0, sizeof(info
));
466 strncpy(info
.name
, dev
->netdev
->name
, sizeof(info
.name
)-1);
468 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
480 * Function irtty_open(tty)
482 * This function is called by the TTY module when the IrDA line
483 * discipline is called for. Because we are sure the tty line exists,
484 * we only have to link it to a free IrDA channel.
486 static int irtty_open(struct tty_struct
*tty
)
489 struct sirtty_cb
*priv
;
492 /* Module stuff handled via irda_ldisc.owner - Jean II */
494 /* First make sure we're not already connected. */
495 if (tty
->disc_data
!= NULL
) {
496 priv
= tty
->disc_data
;
497 if (priv
&& priv
->magic
== IRTTY_MAGIC
) {
501 tty
->disc_data
= NULL
; /* ### */
504 /* stop the underlying driver */
505 irtty_stop_receiver(tty
, TRUE
);
506 if (tty
->driver
->stop
)
507 tty
->driver
->stop(tty
);
509 if (tty
->driver
->flush_buffer
)
510 tty
->driver
->flush_buffer(tty
);
512 /* apply mtt override */
513 sir_tty_drv
.qos_mtt_bits
= qos_mtt_bits
;
515 /* get a sir device instance for this driver */
516 dev
= sirdev_get_instance(&sir_tty_drv
, tty
->name
);
522 /* allocate private device info block */
523 priv
= kmalloc(sizeof(*priv
), GFP_KERNEL
);
526 memset(priv
, 0, sizeof(*priv
));
528 priv
->magic
= IRTTY_MAGIC
;
532 /* serialize with start_dev - in case we were racing with ifup */
536 tty
->disc_data
= priv
;
540 IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __FUNCTION__
, tty
->name
);
545 sirdev_put_instance(dev
);
551 * Function irtty_close (tty)
553 * Close down a IrDA channel. This means flushing out any pending queues,
554 * and then restoring the TTY line discipline to what it was before it got
555 * hooked to IrDA (which usually is TTY again).
557 static void irtty_close(struct tty_struct
*tty
)
559 struct sirtty_cb
*priv
= tty
->disc_data
;
561 IRDA_ASSERT(priv
!= NULL
, return;);
562 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return;);
564 /* Hm, with a dongle attached the dongle driver wants
565 * to close the dongle - which requires the use of
566 * some tty write and/or termios or ioctl operations.
567 * Are we allowed to call those when already requested
568 * to shutdown the ldisc?
569 * If not, we should somehow mark the dev being staled.
570 * Question remains, how to close the dongle in this case...
571 * For now let's assume we are granted to issue tty driver calls
572 * until we return here from the ldisc close. I'm just wondering
573 * how this behaves with hotpluggable serial hardware like
574 * rs232-pcmcia card or usb-serial...
579 /* we are dead now */
580 tty
->disc_data
= NULL
;
582 sirdev_put_instance(priv
->dev
);
585 irtty_stop_receiver(tty
, TRUE
);
586 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
587 if (tty
->driver
->stop
)
588 tty
->driver
->stop(tty
);
592 IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __FUNCTION__
, tty
->name
);
595 /* ------------------------------------------------------- */
597 static struct tty_ldisc irda_ldisc
= {
598 .magic
= TTY_LDISC_MAGIC
,
602 .close
= irtty_close
,
605 .ioctl
= irtty_ioctl
,
607 .receive_buf
= irtty_receive_buf
,
608 .receive_room
= irtty_receive_room
,
609 .write_wakeup
= irtty_write_wakeup
,
610 .owner
= THIS_MODULE
,
613 /* ------------------------------------------------------- */
615 static int __init
irtty_sir_init(void)
619 if ((err
= tty_register_ldisc(N_IRDA
, &irda_ldisc
)) != 0)
620 IRDA_ERROR("IrDA: can't register line discipline (err = %d)\n",
625 static void __exit
irtty_sir_cleanup(void)
629 if ((err
= tty_register_ldisc(N_IRDA
, NULL
))) {
630 IRDA_ERROR("%s(), can't unregister line discipline (err = %d)\n",
635 module_init(irtty_sir_init
);
636 module_exit(irtty_sir_cleanup
);
638 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
639 MODULE_DESCRIPTION("IrDA TTY device driver");
640 MODULE_ALIAS_LDISC(N_IRDA
);
641 MODULE_LICENSE("GPL");