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