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/slab.h>
32 #include <linux/tty.h>
33 #include <linux/init.h>
34 #include <asm/uaccess.h>
35 #include <linux/delay.h>
36 #include <linux/mutex.h>
38 #include <net/irda/irda.h>
39 #include <net/irda/irda_device.h>
42 #include "irtty-sir.h"
44 static int qos_mtt_bits
= 0x03; /* 5 ms or more */
46 module_param(qos_mtt_bits
, int, 0);
47 MODULE_PARM_DESC(qos_mtt_bits
, "Minimum Turn Time");
49 /* ------------------------------------------------------- */
51 /* device configuration callbacks always invoked with irda-thread context */
53 /* find out, how many chars we have in buffers below us
54 * this is allowed to lie, i.e. return less chars than we
55 * actually have. The returned value is used to determine
56 * how long the irdathread should wait before doing the
57 * real blocking wait_until_sent()
60 static int irtty_chars_in_buffer(struct sir_dev
*dev
)
62 struct sirtty_cb
*priv
= dev
->priv
;
64 IRDA_ASSERT(priv
!= NULL
, return -1;);
65 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
67 return tty_chars_in_buffer(priv
->tty
);
70 /* Wait (sleep) until underlaying hardware finished transmission
71 * i.e. hardware buffers are drained
72 * this must block and not return before all characters are really sent
74 * If the tty sits on top of a 16550A-like uart, there are typically
75 * up to 16 bytes in the fifo - f.e. 9600 bps 8N1 needs 16.7 msec
77 * With usbserial the uart-fifo is basically replaced by the converter's
78 * outgoing endpoint buffer, which can usually hold 64 bytes (at least).
79 * With pl2303 it appears we are safe with 60msec here.
81 * I really wish all serial drivers would provide
82 * correct implementation of wait_until_sent()
85 #define USBSERIAL_TX_DONE_DELAY 60
87 static void irtty_wait_until_sent(struct sir_dev
*dev
)
89 struct sirtty_cb
*priv
= dev
->priv
;
90 struct tty_struct
*tty
;
92 IRDA_ASSERT(priv
!= NULL
, return;);
93 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return;);
96 if (tty
->ops
->wait_until_sent
) {
97 tty
->ops
->wait_until_sent(tty
, msecs_to_jiffies(100));
100 msleep(USBSERIAL_TX_DONE_DELAY
);
105 * Function irtty_change_speed (dev, speed)
107 * Change the speed of the serial port.
109 * This may sleep in set_termios (usbserial driver f.e.) and must
110 * not be called from interrupt/timer/tasklet therefore.
111 * All such invocations are deferred to kIrDAd now so we can sleep there.
114 static int irtty_change_speed(struct sir_dev
*dev
, unsigned speed
)
116 struct sirtty_cb
*priv
= dev
->priv
;
117 struct tty_struct
*tty
;
118 struct ktermios old_termios
;
121 IRDA_ASSERT(priv
!= NULL
, return -1;);
122 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
126 mutex_lock(&tty
->termios_mutex
);
127 old_termios
= *(tty
->termios
);
128 cflag
= tty
->termios
->c_cflag
;
129 tty_encode_baud_rate(tty
, speed
, speed
);
130 if (tty
->ops
->set_termios
)
131 tty
->ops
->set_termios(tty
, &old_termios
);
132 priv
->io
.speed
= speed
;
133 mutex_unlock(&tty
->termios_mutex
);
139 * Function irtty_set_dtr_rts (dev, dtr, rts)
141 * This function can be used by dongles etc. to set or reset the status
142 * of the dtr and rts lines
145 static int irtty_set_dtr_rts(struct sir_dev
*dev
, int dtr
, int rts
)
147 struct sirtty_cb
*priv
= dev
->priv
;
151 IRDA_ASSERT(priv
!= NULL
, return -1;);
152 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
164 * We can't use ioctl() because it expects a non-null file structure,
165 * and we don't have that here.
166 * This function is not yet defined for all tty driver, so
167 * let's be careful... Jean II
169 IRDA_ASSERT(priv
->tty
->ops
->tiocmset
!= NULL
, return -1;);
170 priv
->tty
->ops
->tiocmset(priv
->tty
, set
, clear
);
175 /* ------------------------------------------------------- */
177 /* called from sir_dev when there is more data to send
178 * context is either netdev->hard_xmit or some transmit-completion bh
179 * i.e. we are under spinlock here and must not sleep.
182 static int irtty_do_write(struct sir_dev
*dev
, const unsigned char *ptr
, size_t len
)
184 struct sirtty_cb
*priv
= dev
->priv
;
185 struct tty_struct
*tty
;
188 IRDA_ASSERT(priv
!= NULL
, return -1;);
189 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -1;);
192 if (!tty
->ops
->write
)
194 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
195 writelen
= tty_write_room(tty
);
198 return tty
->ops
->write(tty
, ptr
, writelen
);
201 /* ------------------------------------------------------- */
203 /* irda line discipline callbacks */
206 * Function irtty_receive_buf( tty, cp, count)
208 * Handle the 'receiver data ready' interrupt. This function is called
209 * by the 'tty_io' module in the kernel when a block of IrDA data has
210 * been received, which can now be decapsulated and delivered for
213 * calling context depends on underlying driver and tty->low_latency!
214 * for example (low_latency: 1 / 0):
215 * serial.c: uart-interrupt / softint
216 * usbserial: urb-complete-interrupt / softint
219 static unsigned int irtty_receive_buf(struct tty_struct
*tty
,
220 const unsigned char *cp
, char *fp
, int count
)
223 struct sirtty_cb
*priv
= tty
->disc_data
;
226 IRDA_ASSERT(priv
!= NULL
, return -ENODEV
;);
227 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -EINVAL
;);
229 if (unlikely(count
==0)) /* yes, this happens */
234 IRDA_WARNING("%s(), not ready yet!\n", __func__
);
238 for (i
= 0; i
< count
; i
++) {
240 * Characters received with a parity error, etc?
243 IRDA_DEBUG(0, "Framing or parity error!\n");
244 sirdev_receive(dev
, NULL
, 0); /* notify sir_dev (updating stats) */
249 sirdev_receive(dev
, cp
, count
);
255 * Function irtty_write_wakeup (tty)
257 * Called by the driver when there's room for more data. If we have
258 * more packets to send, we send them here.
261 static void irtty_write_wakeup(struct tty_struct
*tty
)
263 struct sirtty_cb
*priv
= tty
->disc_data
;
265 IRDA_ASSERT(priv
!= NULL
, return;);
266 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return;);
268 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
270 sirdev_write_complete(priv
->dev
);
273 /* ------------------------------------------------------- */
276 * Function irtty_stop_receiver (tty, stop)
280 static inline void irtty_stop_receiver(struct tty_struct
*tty
, int stop
)
282 struct ktermios old_termios
;
285 mutex_lock(&tty
->termios_mutex
);
286 old_termios
= *(tty
->termios
);
287 cflag
= tty
->termios
->c_cflag
;
294 tty
->termios
->c_cflag
= cflag
;
295 if (tty
->ops
->set_termios
)
296 tty
->ops
->set_termios(tty
, &old_termios
);
297 mutex_unlock(&tty
->termios_mutex
);
300 /*****************************************************************/
302 /* serialize ldisc open/close with sir_dev */
303 static DEFINE_MUTEX(irtty_mutex
);
305 /* notifier from sir_dev when irda% device gets opened (ifup) */
307 static int irtty_start_dev(struct sir_dev
*dev
)
309 struct sirtty_cb
*priv
;
310 struct tty_struct
*tty
;
312 /* serialize with ldisc open/close */
313 mutex_lock(&irtty_mutex
);
316 if (unlikely(!priv
|| priv
->magic
!=IRTTY_MAGIC
)) {
317 mutex_unlock(&irtty_mutex
);
324 tty
->ops
->start(tty
);
325 /* Make sure we can receive more data */
326 irtty_stop_receiver(tty
, FALSE
);
328 mutex_unlock(&irtty_mutex
);
332 /* notifier from sir_dev when irda% device gets closed (ifdown) */
334 static int irtty_stop_dev(struct sir_dev
*dev
)
336 struct sirtty_cb
*priv
;
337 struct tty_struct
*tty
;
339 /* serialize with ldisc open/close */
340 mutex_lock(&irtty_mutex
);
343 if (unlikely(!priv
|| priv
->magic
!=IRTTY_MAGIC
)) {
344 mutex_unlock(&irtty_mutex
);
350 /* Make sure we don't receive more data */
351 irtty_stop_receiver(tty
, TRUE
);
355 mutex_unlock(&irtty_mutex
);
360 /* ------------------------------------------------------- */
362 static struct sir_driver sir_tty_drv
= {
363 .owner
= THIS_MODULE
,
364 .driver_name
= "sir_tty",
365 .start_dev
= irtty_start_dev
,
366 .stop_dev
= irtty_stop_dev
,
367 .do_write
= irtty_do_write
,
368 .chars_in_buffer
= irtty_chars_in_buffer
,
369 .wait_until_sent
= irtty_wait_until_sent
,
370 .set_speed
= irtty_change_speed
,
371 .set_dtr_rts
= irtty_set_dtr_rts
,
374 /* ------------------------------------------------------- */
377 * Function irtty_ioctl (tty, file, cmd, arg)
379 * The Swiss army knife of system calls :-)
382 static int irtty_ioctl(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
384 struct irtty_info
{ char name
[6]; } info
;
386 struct sirtty_cb
*priv
= tty
->disc_data
;
389 IRDA_ASSERT(priv
!= NULL
, return -ENODEV
;);
390 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return -EBADR
;);
392 IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __func__
, cmd
);
395 IRDA_ASSERT(dev
!= NULL
, return -1;);
398 case IRTTY_IOCTDONGLE
:
399 /* this call blocks for completion */
400 err
= sirdev_set_dongle(dev
, (IRDA_DONGLE
) arg
);
404 IRDA_ASSERT(dev
->netdev
!= NULL
, return -1;);
406 memset(&info
, 0, sizeof(info
));
407 strncpy(info
.name
, dev
->netdev
->name
, sizeof(info
.name
)-1);
409 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
413 err
= tty_mode_ioctl(tty
, file
, cmd
, arg
);
421 * Function irtty_open(tty)
423 * This function is called by the TTY module when the IrDA line
424 * discipline is called for. Because we are sure the tty line exists,
425 * we only have to link it to a free IrDA channel.
427 static int irtty_open(struct tty_struct
*tty
)
430 struct sirtty_cb
*priv
;
433 /* Module stuff handled via irda_ldisc.owner - Jean II */
435 /* First make sure we're not already connected. */
436 if (tty
->disc_data
!= NULL
) {
437 priv
= tty
->disc_data
;
438 if (priv
&& priv
->magic
== IRTTY_MAGIC
) {
442 tty
->disc_data
= NULL
; /* ### */
445 /* stop the underlying driver */
446 irtty_stop_receiver(tty
, TRUE
);
450 tty_driver_flush_buffer(tty
);
452 /* apply mtt override */
453 sir_tty_drv
.qos_mtt_bits
= qos_mtt_bits
;
455 /* get a sir device instance for this driver */
456 dev
= sirdev_get_instance(&sir_tty_drv
, tty
->name
);
462 /* allocate private device info block */
463 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
467 priv
->magic
= IRTTY_MAGIC
;
471 /* serialize with start_dev - in case we were racing with ifup */
472 mutex_lock(&irtty_mutex
);
475 tty
->disc_data
= priv
;
476 tty
->receive_room
= 65536;
478 mutex_unlock(&irtty_mutex
);
480 IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __func__
, tty
->name
);
485 sirdev_put_instance(dev
);
491 * Function irtty_close (tty)
493 * Close down a IrDA channel. This means flushing out any pending queues,
494 * and then restoring the TTY line discipline to what it was before it got
495 * hooked to IrDA (which usually is TTY again).
497 static void irtty_close(struct tty_struct
*tty
)
499 struct sirtty_cb
*priv
= tty
->disc_data
;
501 IRDA_ASSERT(priv
!= NULL
, return;);
502 IRDA_ASSERT(priv
->magic
== IRTTY_MAGIC
, return;);
504 /* Hm, with a dongle attached the dongle driver wants
505 * to close the dongle - which requires the use of
506 * some tty write and/or termios or ioctl operations.
507 * Are we allowed to call those when already requested
508 * to shutdown the ldisc?
509 * If not, we should somehow mark the dev being staled.
510 * Question remains, how to close the dongle in this case...
511 * For now let's assume we are granted to issue tty driver calls
512 * until we return here from the ldisc close. I'm just wondering
513 * how this behaves with hotpluggable serial hardware like
514 * rs232-pcmcia card or usb-serial...
519 /* we are dead now */
520 tty
->disc_data
= NULL
;
522 sirdev_put_instance(priv
->dev
);
525 irtty_stop_receiver(tty
, TRUE
);
526 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
532 IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __func__
, tty
->name
);
535 /* ------------------------------------------------------- */
537 static struct tty_ldisc_ops irda_ldisc
= {
538 .magic
= TTY_LDISC_MAGIC
,
542 .close
= irtty_close
,
545 .ioctl
= irtty_ioctl
,
547 .receive_buf
= irtty_receive_buf
,
548 .write_wakeup
= irtty_write_wakeup
,
549 .owner
= THIS_MODULE
,
552 /* ------------------------------------------------------- */
554 static int __init
irtty_sir_init(void)
558 if ((err
= tty_register_ldisc(N_IRDA
, &irda_ldisc
)) != 0)
559 IRDA_ERROR("IrDA: can't register line discipline (err = %d)\n",
564 static void __exit
irtty_sir_cleanup(void)
568 if ((err
= tty_unregister_ldisc(N_IRDA
))) {
569 IRDA_ERROR("%s(), can't unregister line discipline (err = %d)\n",
574 module_init(irtty_sir_init
);
575 module_exit(irtty_sir_cleanup
);
577 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
578 MODULE_DESCRIPTION("IrDA TTY device driver");
579 MODULE_ALIAS_LDISC(N_IRDA
);
580 MODULE_LICENSE("GPL");