| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /********************************************************************* | 
|  | 2 | * | 
|  | 3 | * Filename:      irtty-sir.c | 
|  | 4 | * Version:       2.0 | 
|  | 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> | 
|  | 13 | * | 
|  | 14 | *     Copyright (c) 1998-2000 Dag Brattli, | 
|  | 15 | *     Copyright (c) 2002 Martin Diehl, | 
|  | 16 | *     All Rights Reserved. | 
|  | 17 | * | 
|  | 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. | 
|  | 22 | * | 
| Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 23 | *     Neither Dag Brattli nor University of Tromsø admit liability nor | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | *     provide warranty for any of this software. This material is | 
|  | 25 | *     provided "AS-IS" and at no charge. | 
|  | 26 | * | 
|  | 27 | ********************************************************************/ | 
|  | 28 |  | 
|  | 29 | #include <linux/module.h> | 
|  | 30 | #include <linux/kernel.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/tty.h> | 
|  | 33 | #include <linux/init.h> | 
|  | 34 | #include <asm/uaccess.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/delay.h> | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 36 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | #include <net/irda/irda.h> | 
|  | 39 | #include <net/irda/irda_device.h> | 
|  | 40 |  | 
|  | 41 | #include "sir-dev.h" | 
|  | 42 | #include "irtty-sir.h" | 
|  | 43 |  | 
|  | 44 | static int qos_mtt_bits = 0x03;      /* 5 ms or more */ | 
|  | 45 |  | 
|  | 46 | module_param(qos_mtt_bits, int, 0); | 
|  | 47 | MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time"); | 
|  | 48 |  | 
|  | 49 | /* ------------------------------------------------------- */ | 
|  | 50 |  | 
|  | 51 | /* device configuration callbacks always invoked with irda-thread context */ | 
|  | 52 |  | 
|  | 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() | 
|  | 58 | */ | 
|  | 59 |  | 
|  | 60 | static int irtty_chars_in_buffer(struct sir_dev *dev) | 
|  | 61 | { | 
|  | 62 | struct sirtty_cb *priv = dev->priv; | 
|  | 63 |  | 
|  | 64 | IRDA_ASSERT(priv != NULL, return -1;); | 
|  | 65 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;); | 
|  | 66 |  | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 67 | return tty_chars_in_buffer(priv->tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
|  | 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 | 
|  | 73 | * | 
|  | 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 | 
|  | 76 | * | 
|  | 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. | 
|  | 80 | * | 
|  | 81 | * I really wish all serial drivers would provide | 
|  | 82 | * correct implementation of wait_until_sent() | 
|  | 83 | */ | 
|  | 84 |  | 
|  | 85 | #define USBSERIAL_TX_DONE_DELAY	60 | 
|  | 86 |  | 
|  | 87 | static void irtty_wait_until_sent(struct sir_dev *dev) | 
|  | 88 | { | 
|  | 89 | struct sirtty_cb *priv = dev->priv; | 
|  | 90 | struct tty_struct *tty; | 
|  | 91 |  | 
|  | 92 | IRDA_ASSERT(priv != NULL, return;); | 
|  | 93 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;); | 
|  | 94 |  | 
|  | 95 | tty = priv->tty; | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 96 | if (tty->ops->wait_until_sent) { | 
|  | 97 | tty->ops->wait_until_sent(tty, msecs_to_jiffies(100)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } | 
|  | 99 | else { | 
|  | 100 | msleep(USBSERIAL_TX_DONE_DELAY); | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | /* | 
|  | 105 | *  Function irtty_change_speed (dev, speed) | 
|  | 106 | * | 
|  | 107 | *    Change the speed of the serial port. | 
|  | 108 | * | 
|  | 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. | 
|  | 112 | */ | 
|  | 113 |  | 
|  | 114 | static int irtty_change_speed(struct sir_dev *dev, unsigned speed) | 
|  | 115 | { | 
|  | 116 | struct sirtty_cb *priv = dev->priv; | 
|  | 117 | struct tty_struct *tty; | 
| Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 118 | struct ktermios old_termios; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | int cflag; | 
|  | 120 |  | 
|  | 121 | IRDA_ASSERT(priv != NULL, return -1;); | 
|  | 122 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;); | 
|  | 123 |  | 
|  | 124 | tty = priv->tty; | 
|  | 125 |  | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 126 | mutex_lock(&tty->termios_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | old_termios = *(tty->termios); | 
|  | 128 | cflag = tty->termios->c_cflag; | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 129 | tty_encode_baud_rate(tty, speed, speed); | 
|  | 130 | if (tty->ops->set_termios) | 
|  | 131 | tty->ops->set_termios(tty, &old_termios); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | priv->io.speed = speed; | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 133 | mutex_unlock(&tty->termios_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 |  | 
|  | 135 | return 0; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | /* | 
|  | 139 | * Function irtty_set_dtr_rts (dev, dtr, rts) | 
|  | 140 | * | 
|  | 141 | *    This function can be used by dongles etc. to set or reset the status | 
|  | 142 | *    of the dtr and rts lines | 
|  | 143 | */ | 
|  | 144 |  | 
|  | 145 | static int irtty_set_dtr_rts(struct sir_dev *dev, int dtr, int rts) | 
|  | 146 | { | 
|  | 147 | struct sirtty_cb *priv = dev->priv; | 
|  | 148 | int set = 0; | 
|  | 149 | int clear = 0; | 
|  | 150 |  | 
|  | 151 | IRDA_ASSERT(priv != NULL, return -1;); | 
|  | 152 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;); | 
|  | 153 |  | 
|  | 154 | if (rts) | 
|  | 155 | set |= TIOCM_RTS; | 
|  | 156 | else | 
|  | 157 | clear |= TIOCM_RTS; | 
|  | 158 | if (dtr) | 
|  | 159 | set |= TIOCM_DTR; | 
|  | 160 | else | 
|  | 161 | clear |= TIOCM_DTR; | 
|  | 162 |  | 
|  | 163 | /* | 
|  | 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 | 
|  | 168 | */ | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 169 | IRDA_ASSERT(priv->tty->ops->tiocmset != NULL, return -1;); | 
| Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 170 | priv->tty->ops->tiocmset(priv->tty, set, clear); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 |  | 
|  | 172 | return 0; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /* ------------------------------------------------------- */ | 
|  | 176 |  | 
|  | 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. | 
|  | 180 | */ | 
|  | 181 |  | 
|  | 182 | static int irtty_do_write(struct sir_dev *dev, const unsigned char *ptr, size_t len) | 
|  | 183 | { | 
|  | 184 | struct sirtty_cb *priv = dev->priv; | 
|  | 185 | struct tty_struct *tty; | 
|  | 186 | int writelen; | 
|  | 187 |  | 
|  | 188 | IRDA_ASSERT(priv != NULL, return -1;); | 
|  | 189 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;); | 
|  | 190 |  | 
|  | 191 | tty = priv->tty; | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 192 | if (!tty->ops->write) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return 0; | 
| Alan Cox | 8a1ec21 | 2008-12-05 22:31:52 -0800 | [diff] [blame] | 194 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 195 | writelen = tty_write_room(tty); | 
|  | 196 | if (writelen > len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | writelen = len; | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 198 | return tty->ops->write(tty, ptr, writelen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
|  | 201 | /* ------------------------------------------------------- */ | 
|  | 202 |  | 
|  | 203 | /* irda line discipline callbacks */ | 
|  | 204 |  | 
|  | 205 | /* | 
|  | 206 | *  Function irtty_receive_buf( tty, cp, count) | 
|  | 207 | * | 
|  | 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 | 
|  | 211 | *    further processing | 
|  | 212 | * | 
|  | 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 | 
|  | 217 | */ | 
|  | 218 |  | 
| Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 219 | static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp, | 
|  | 220 | char *fp, int count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { | 
|  | 222 | struct sir_dev *dev; | 
|  | 223 | struct sirtty_cb *priv = tty->disc_data; | 
|  | 224 | int	i; | 
|  | 225 |  | 
| Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 226 | IRDA_ASSERT(priv != NULL, return;); | 
|  | 227 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 |  | 
|  | 229 | if (unlikely(count==0))		/* yes, this happens */ | 
| Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 230 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 |  | 
|  | 232 | dev = priv->dev; | 
|  | 233 | if (!dev) { | 
| Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 234 | IRDA_WARNING("%s(), not ready yet!\n", __func__); | 
| Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 235 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
|  | 238 | for (i = 0; i < count; i++) { | 
|  | 239 | /* | 
|  | 240 | *  Characters received with a parity error, etc? | 
|  | 241 | */ | 
|  | 242 | if (fp && *fp++) { | 
|  | 243 | IRDA_DEBUG(0, "Framing or parity error!\n"); | 
|  | 244 | sirdev_receive(dev, NULL, 0);	/* notify sir_dev (updating stats) */ | 
| Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 245 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | sirdev_receive(dev, cp, count); | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * Function irtty_write_wakeup (tty) | 
|  | 254 | * | 
|  | 255 | *    Called by the driver when there's room for more data.  If we have | 
|  | 256 | *    more packets to send, we send them here. | 
|  | 257 | * | 
|  | 258 | */ | 
|  | 259 | static void irtty_write_wakeup(struct tty_struct *tty) | 
|  | 260 | { | 
|  | 261 | struct sirtty_cb *priv = tty->disc_data; | 
|  | 262 |  | 
|  | 263 | IRDA_ASSERT(priv != NULL, return;); | 
|  | 264 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;); | 
|  | 265 |  | 
| Alan Cox | 8a1ec21 | 2008-12-05 22:31:52 -0800 | [diff] [blame] | 266 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (priv->dev) | 
|  | 268 | sirdev_write_complete(priv->dev); | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | /* ------------------------------------------------------- */ | 
|  | 272 |  | 
|  | 273 | /* | 
|  | 274 | * Function irtty_stop_receiver (tty, stop) | 
|  | 275 | * | 
|  | 276 | */ | 
|  | 277 |  | 
|  | 278 | static inline void irtty_stop_receiver(struct tty_struct *tty, int stop) | 
|  | 279 | { | 
| Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 280 | struct ktermios old_termios; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | int cflag; | 
|  | 282 |  | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 283 | mutex_lock(&tty->termios_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | old_termios = *(tty->termios); | 
|  | 285 | cflag = tty->termios->c_cflag; | 
|  | 286 |  | 
|  | 287 | if (stop) | 
|  | 288 | cflag &= ~CREAD; | 
|  | 289 | else | 
|  | 290 | cflag |= CREAD; | 
|  | 291 |  | 
|  | 292 | tty->termios->c_cflag = cflag; | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 293 | if (tty->ops->set_termios) | 
|  | 294 | tty->ops->set_termios(tty, &old_termios); | 
|  | 295 | mutex_unlock(&tty->termios_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
|  | 298 | /*****************************************************************/ | 
|  | 299 |  | 
|  | 300 | /* serialize ldisc open/close with sir_dev */ | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 301 | static DEFINE_MUTEX(irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 |  | 
|  | 303 | /* notifier from sir_dev when irda% device gets opened (ifup) */ | 
|  | 304 |  | 
|  | 305 | static int irtty_start_dev(struct sir_dev *dev) | 
|  | 306 | { | 
|  | 307 | struct sirtty_cb *priv; | 
|  | 308 | struct tty_struct *tty; | 
|  | 309 |  | 
|  | 310 | /* serialize with ldisc open/close */ | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 311 | mutex_lock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 |  | 
|  | 313 | priv = dev->priv; | 
|  | 314 | if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) { | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 315 | mutex_unlock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return -ESTALE; | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | tty = priv->tty; | 
|  | 320 |  | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 321 | if (tty->ops->start) | 
|  | 322 | tty->ops->start(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | /* Make sure we can receive more data */ | 
|  | 324 | irtty_stop_receiver(tty, FALSE); | 
|  | 325 |  | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 326 | mutex_unlock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return 0; | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | /* notifier from sir_dev when irda% device gets closed (ifdown) */ | 
|  | 331 |  | 
|  | 332 | static int irtty_stop_dev(struct sir_dev *dev) | 
|  | 333 | { | 
|  | 334 | struct sirtty_cb *priv; | 
|  | 335 | struct tty_struct *tty; | 
|  | 336 |  | 
|  | 337 | /* serialize with ldisc open/close */ | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 338 | mutex_lock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 |  | 
|  | 340 | priv = dev->priv; | 
|  | 341 | if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) { | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 342 | mutex_unlock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return -ESTALE; | 
|  | 344 | } | 
|  | 345 |  | 
|  | 346 | tty = priv->tty; | 
|  | 347 |  | 
|  | 348 | /* Make sure we don't receive more data */ | 
|  | 349 | irtty_stop_receiver(tty, TRUE); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 350 | if (tty->ops->stop) | 
|  | 351 | tty->ops->stop(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 |  | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 353 | mutex_unlock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 |  | 
|  | 355 | return 0; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | /* ------------------------------------------------------- */ | 
|  | 359 |  | 
|  | 360 | static struct sir_driver sir_tty_drv = { | 
|  | 361 | .owner			= THIS_MODULE, | 
|  | 362 | .driver_name		= "sir_tty", | 
|  | 363 | .start_dev		= irtty_start_dev, | 
|  | 364 | .stop_dev		= irtty_stop_dev, | 
|  | 365 | .do_write		= irtty_do_write, | 
|  | 366 | .chars_in_buffer	= irtty_chars_in_buffer, | 
|  | 367 | .wait_until_sent	= irtty_wait_until_sent, | 
|  | 368 | .set_speed		= irtty_change_speed, | 
|  | 369 | .set_dtr_rts		= irtty_set_dtr_rts, | 
|  | 370 | }; | 
|  | 371 |  | 
|  | 372 | /* ------------------------------------------------------- */ | 
|  | 373 |  | 
|  | 374 | /* | 
|  | 375 | * Function irtty_ioctl (tty, file, cmd, arg) | 
|  | 376 | * | 
|  | 377 | *     The Swiss army knife of system calls :-) | 
|  | 378 | * | 
|  | 379 | */ | 
|  | 380 | static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) | 
|  | 381 | { | 
|  | 382 | struct irtty_info { char name[6]; } info; | 
|  | 383 | struct sir_dev *dev; | 
|  | 384 | struct sirtty_cb *priv = tty->disc_data; | 
|  | 385 | int err = 0; | 
|  | 386 |  | 
|  | 387 | IRDA_ASSERT(priv != NULL, return -ENODEV;); | 
|  | 388 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;); | 
|  | 389 |  | 
| Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 390 | IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __func__, cmd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 |  | 
|  | 392 | dev = priv->dev; | 
|  | 393 | IRDA_ASSERT(dev != NULL, return -1;); | 
|  | 394 |  | 
|  | 395 | switch (cmd) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | case IRTTY_IOCTDONGLE: | 
|  | 397 | /* this call blocks for completion */ | 
|  | 398 | err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg); | 
|  | 399 | break; | 
|  | 400 |  | 
|  | 401 | case IRTTY_IOCGET: | 
|  | 402 | IRDA_ASSERT(dev->netdev != NULL, return -1;); | 
|  | 403 |  | 
|  | 404 | memset(&info, 0, sizeof(info)); | 
|  | 405 | strncpy(info.name, dev->netdev->name, sizeof(info.name)-1); | 
|  | 406 |  | 
|  | 407 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) | 
|  | 408 | err = -EFAULT; | 
|  | 409 | break; | 
|  | 410 | default: | 
| Alan Cox | d012753 | 2007-11-07 01:27:34 -0800 | [diff] [blame] | 411 | err = tty_mode_ioctl(tty, file, cmd, arg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | break; | 
|  | 413 | } | 
|  | 414 | return err; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 |  | 
|  | 418 | /* | 
|  | 419 | *  Function irtty_open(tty) | 
|  | 420 | * | 
|  | 421 | *    This function is called by the TTY module when the IrDA line | 
|  | 422 | *    discipline is called for.  Because we are sure the tty line exists, | 
|  | 423 | *    we only have to link it to a free IrDA channel. | 
|  | 424 | */ | 
|  | 425 | static int irtty_open(struct tty_struct *tty) | 
|  | 426 | { | 
|  | 427 | struct sir_dev *dev; | 
|  | 428 | struct sirtty_cb *priv; | 
|  | 429 | int ret = 0; | 
|  | 430 |  | 
|  | 431 | /* Module stuff handled via irda_ldisc.owner - Jean II */ | 
|  | 432 |  | 
|  | 433 | /* First make sure we're not already connected. */ | 
|  | 434 | if (tty->disc_data != NULL) { | 
|  | 435 | priv = tty->disc_data; | 
|  | 436 | if (priv && priv->magic == IRTTY_MAGIC) { | 
|  | 437 | ret = -EEXIST; | 
|  | 438 | goto out; | 
|  | 439 | } | 
|  | 440 | tty->disc_data = NULL;		/* ### */ | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 | /* stop the underlying  driver */ | 
|  | 444 | irtty_stop_receiver(tty, TRUE); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 445 | if (tty->ops->stop) | 
|  | 446 | tty->ops->stop(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 |  | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 448 | tty_driver_flush_buffer(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 |  | 
|  | 450 | /* apply mtt override */ | 
|  | 451 | sir_tty_drv.qos_mtt_bits = qos_mtt_bits; | 
|  | 452 |  | 
|  | 453 | /* get a sir device instance for this driver */ | 
|  | 454 | dev = sirdev_get_instance(&sir_tty_drv, tty->name); | 
|  | 455 | if (!dev) { | 
|  | 456 | ret = -ENODEV; | 
|  | 457 | goto out; | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | /* allocate private device info block */ | 
| Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 461 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (!priv) | 
|  | 463 | goto out_put; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 |  | 
|  | 465 | priv->magic = IRTTY_MAGIC; | 
|  | 466 | priv->tty = tty; | 
|  | 467 | priv->dev = dev; | 
|  | 468 |  | 
|  | 469 | /* serialize with start_dev - in case we were racing with ifup */ | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 470 | mutex_lock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 |  | 
|  | 472 | dev->priv = priv; | 
|  | 473 | tty->disc_data = priv; | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 474 | tty->receive_room = 65536; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 |  | 
| Arjan van de Ven | d4ccd08 | 2006-03-20 22:32:53 -0800 | [diff] [blame] | 476 | mutex_unlock(&irtty_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 |  | 
| Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 478 | IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __func__, tty->name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 |  | 
|  | 480 | return 0; | 
|  | 481 |  | 
|  | 482 | out_put: | 
|  | 483 | sirdev_put_instance(dev); | 
|  | 484 | out: | 
|  | 485 | return ret; | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | /* | 
|  | 489 | *  Function irtty_close (tty) | 
|  | 490 | * | 
|  | 491 | *    Close down a IrDA channel. This means flushing out any pending queues, | 
|  | 492 | *    and then restoring the TTY line discipline to what it was before it got | 
|  | 493 | *    hooked to IrDA (which usually is TTY again). | 
|  | 494 | */ | 
|  | 495 | static void irtty_close(struct tty_struct *tty) | 
|  | 496 | { | 
|  | 497 | struct sirtty_cb *priv = tty->disc_data; | 
|  | 498 |  | 
|  | 499 | IRDA_ASSERT(priv != NULL, return;); | 
|  | 500 | IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;); | 
|  | 501 |  | 
|  | 502 | /* Hm, with a dongle attached the dongle driver wants | 
|  | 503 | * to close the dongle - which requires the use of | 
|  | 504 | * some tty write and/or termios or ioctl operations. | 
|  | 505 | * Are we allowed to call those when already requested | 
|  | 506 | * to shutdown the ldisc? | 
|  | 507 | * If not, we should somehow mark the dev being staled. | 
|  | 508 | * Question remains, how to close the dongle in this case... | 
|  | 509 | * For now let's assume we are granted to issue tty driver calls | 
|  | 510 | * until we return here from the ldisc close. I'm just wondering | 
|  | 511 | * how this behaves with hotpluggable serial hardware like | 
|  | 512 | * rs232-pcmcia card or usb-serial... | 
|  | 513 | * | 
|  | 514 | * priv->tty = NULL?; | 
|  | 515 | */ | 
|  | 516 |  | 
|  | 517 | /* we are dead now */ | 
|  | 518 | tty->disc_data = NULL; | 
|  | 519 |  | 
|  | 520 | sirdev_put_instance(priv->dev); | 
|  | 521 |  | 
|  | 522 | /* Stop tty */ | 
|  | 523 | irtty_stop_receiver(tty, TRUE); | 
| Alan Cox | 8a1ec21 | 2008-12-05 22:31:52 -0800 | [diff] [blame] | 524 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 525 | if (tty->ops->stop) | 
|  | 526 | tty->ops->stop(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 |  | 
|  | 528 | kfree(priv); | 
|  | 529 |  | 
| Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 530 | IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __func__, tty->name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } | 
|  | 532 |  | 
|  | 533 | /* ------------------------------------------------------- */ | 
|  | 534 |  | 
| Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 535 | static struct tty_ldisc_ops irda_ldisc = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | .magic		= TTY_LDISC_MAGIC, | 
|  | 537 | .name		= "irda", | 
|  | 538 | .flags		= 0, | 
|  | 539 | .open		= irtty_open, | 
|  | 540 | .close		= irtty_close, | 
|  | 541 | .read		= NULL, | 
|  | 542 | .write		= NULL, | 
|  | 543 | .ioctl		= irtty_ioctl, | 
|  | 544 | .poll		= NULL, | 
|  | 545 | .receive_buf	= irtty_receive_buf, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | .write_wakeup	= irtty_write_wakeup, | 
|  | 547 | .owner		= THIS_MODULE, | 
|  | 548 | }; | 
|  | 549 |  | 
|  | 550 | /* ------------------------------------------------------- */ | 
|  | 551 |  | 
|  | 552 | static int __init irtty_sir_init(void) | 
|  | 553 | { | 
|  | 554 | int err; | 
|  | 555 |  | 
|  | 556 | if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0) | 
|  | 557 | IRDA_ERROR("IrDA: can't register line discipline (err = %d)\n", | 
|  | 558 | err); | 
|  | 559 | return err; | 
|  | 560 | } | 
|  | 561 |  | 
|  | 562 | static void __exit irtty_sir_cleanup(void) | 
|  | 563 | { | 
|  | 564 | int err; | 
|  | 565 |  | 
| Alexey Dobriyan | 64ccd71 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 566 | if ((err = tty_unregister_ldisc(N_IRDA))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | IRDA_ERROR("%s(), can't unregister line discipline (err = %d)\n", | 
| Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 568 | __func__, err); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } | 
|  | 570 | } | 
|  | 571 |  | 
|  | 572 | module_init(irtty_sir_init); | 
|  | 573 | module_exit(irtty_sir_cleanup); | 
|  | 574 |  | 
|  | 575 | MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>"); | 
|  | 576 | MODULE_DESCRIPTION("IrDA TTY device driver"); | 
|  | 577 | MODULE_ALIAS_LDISC(N_IRDA); | 
|  | 578 | MODULE_LICENSE("GPL"); | 
|  | 579 |  |