irtty-sir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. *
  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.
  26. *
  27. ********************************************************************/
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/tty.h>
  32. #include <linux/init.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/delay.h>
  35. #include <linux/mutex.h>
  36. #include <net/irda/irda.h>
  37. #include <net/irda/irda_device.h>
  38. #include "sir-dev.h"
  39. #include "irtty-sir.h"
  40. static int qos_mtt_bits = 0x03; /* 5 ms or more */
  41. module_param(qos_mtt_bits, int, 0);
  42. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  43. /* ------------------------------------------------------- */
  44. /* device configuration callbacks always invoked with irda-thread context */
  45. /* find out, how many chars we have in buffers below us
  46. * this is allowed to lie, i.e. return less chars than we
  47. * actually have. The returned value is used to determine
  48. * how long the irdathread should wait before doing the
  49. * real blocking wait_until_sent()
  50. */
  51. static int irtty_chars_in_buffer(struct sir_dev *dev)
  52. {
  53. struct sirtty_cb *priv = dev->priv;
  54. IRDA_ASSERT(priv != NULL, return -1;);
  55. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
  56. return tty_chars_in_buffer(priv->tty);
  57. }
  58. /* Wait (sleep) until underlaying hardware finished transmission
  59. * i.e. hardware buffers are drained
  60. * this must block and not return before all characters are really sent
  61. *
  62. * If the tty sits on top of a 16550A-like uart, there are typically
  63. * up to 16 bytes in the fifo - f.e. 9600 bps 8N1 needs 16.7 msec
  64. *
  65. * With usbserial the uart-fifo is basically replaced by the converter's
  66. * outgoing endpoint buffer, which can usually hold 64 bytes (at least).
  67. * With pl2303 it appears we are safe with 60msec here.
  68. *
  69. * I really wish all serial drivers would provide
  70. * correct implementation of wait_until_sent()
  71. */
  72. #define USBSERIAL_TX_DONE_DELAY 60
  73. static void irtty_wait_until_sent(struct sir_dev *dev)
  74. {
  75. struct sirtty_cb *priv = dev->priv;
  76. struct tty_struct *tty;
  77. IRDA_ASSERT(priv != NULL, return;);
  78. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  79. tty = priv->tty;
  80. if (tty->ops->wait_until_sent) {
  81. tty->ops->wait_until_sent(tty, msecs_to_jiffies(100));
  82. }
  83. else {
  84. msleep(USBSERIAL_TX_DONE_DELAY);
  85. }
  86. }
  87. /*
  88. * Function irtty_change_speed (dev, speed)
  89. *
  90. * Change the speed of the serial port.
  91. *
  92. * This may sleep in set_termios (usbserial driver f.e.) and must
  93. * not be called from interrupt/timer/tasklet therefore.
  94. * All such invocations are deferred to kIrDAd now so we can sleep there.
  95. */
  96. static int irtty_change_speed(struct sir_dev *dev, unsigned speed)
  97. {
  98. struct sirtty_cb *priv = dev->priv;
  99. struct tty_struct *tty;
  100. struct ktermios old_termios;
  101. int cflag;
  102. IRDA_ASSERT(priv != NULL, return -1;);
  103. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
  104. tty = priv->tty;
  105. down_write(&tty->termios_rwsem);
  106. old_termios = tty->termios;
  107. cflag = tty->termios.c_cflag;
  108. tty_encode_baud_rate(tty, speed, speed);
  109. if (tty->ops->set_termios)
  110. tty->ops->set_termios(tty, &old_termios);
  111. priv->io.speed = speed;
  112. up_write(&tty->termios_rwsem);
  113. return 0;
  114. }
  115. /*
  116. * Function irtty_set_dtr_rts (dev, dtr, rts)
  117. *
  118. * This function can be used by dongles etc. to set or reset the status
  119. * of the dtr and rts lines
  120. */
  121. static int irtty_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
  122. {
  123. struct sirtty_cb *priv = dev->priv;
  124. int set = 0;
  125. int clear = 0;
  126. IRDA_ASSERT(priv != NULL, return -1;);
  127. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
  128. if (rts)
  129. set |= TIOCM_RTS;
  130. else
  131. clear |= TIOCM_RTS;
  132. if (dtr)
  133. set |= TIOCM_DTR;
  134. else
  135. clear |= TIOCM_DTR;
  136. /*
  137. * We can't use ioctl() because it expects a non-null file structure,
  138. * and we don't have that here.
  139. * This function is not yet defined for all tty driver, so
  140. * let's be careful... Jean II
  141. */
  142. IRDA_ASSERT(priv->tty->ops->tiocmset != NULL, return -1;);
  143. priv->tty->ops->tiocmset(priv->tty, set, clear);
  144. return 0;
  145. }
  146. /* ------------------------------------------------------- */
  147. /* called from sir_dev when there is more data to send
  148. * context is either netdev->hard_xmit or some transmit-completion bh
  149. * i.e. we are under spinlock here and must not sleep.
  150. */
  151. static int irtty_do_write(struct sir_dev *dev, const unsigned char *ptr, size_t len)
  152. {
  153. struct sirtty_cb *priv = dev->priv;
  154. struct tty_struct *tty;
  155. int writelen;
  156. IRDA_ASSERT(priv != NULL, return -1;);
  157. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
  158. tty = priv->tty;
  159. if (!tty->ops->write)
  160. return 0;
  161. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  162. writelen = tty_write_room(tty);
  163. if (writelen > len)
  164. writelen = len;
  165. return tty->ops->write(tty, ptr, writelen);
  166. }
  167. /* ------------------------------------------------------- */
  168. /* irda line discipline callbacks */
  169. /*
  170. * Function irtty_receive_buf( tty, cp, count)
  171. *
  172. * Handle the 'receiver data ready' interrupt. This function is called
  173. * by the 'tty_io' module in the kernel when a block of IrDA data has
  174. * been received, which can now be decapsulated and delivered for
  175. * further processing
  176. *
  177. * calling context depends on underlying driver and tty->port->low_latency!
  178. * for example (low_latency: 1 / 0):
  179. * serial.c: uart-interrupt / softint
  180. * usbserial: urb-complete-interrupt / softint
  181. */
  182. static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  183. char *fp, int count)
  184. {
  185. struct sir_dev *dev;
  186. struct sirtty_cb *priv = tty->disc_data;
  187. int i;
  188. IRDA_ASSERT(priv != NULL, return;);
  189. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  190. if (unlikely(count==0)) /* yes, this happens */
  191. return;
  192. dev = priv->dev;
  193. if (!dev) {
  194. net_warn_ratelimited("%s(), not ready yet!\n", __func__);
  195. return;
  196. }
  197. for (i = 0; i < count; i++) {
  198. /*
  199. * Characters received with a parity error, etc?
  200. */
  201. if (fp && *fp++) {
  202. pr_debug("Framing or parity error!\n");
  203. sirdev_receive(dev, NULL, 0); /* notify sir_dev (updating stats) */
  204. return;
  205. }
  206. }
  207. sirdev_receive(dev, cp, count);
  208. }
  209. /*
  210. * Function irtty_write_wakeup (tty)
  211. *
  212. * Called by the driver when there's room for more data. If we have
  213. * more packets to send, we send them here.
  214. *
  215. */
  216. static void irtty_write_wakeup(struct tty_struct *tty)
  217. {
  218. struct sirtty_cb *priv = tty->disc_data;
  219. IRDA_ASSERT(priv != NULL, return;);
  220. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  221. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  222. if (priv->dev)
  223. sirdev_write_complete(priv->dev);
  224. }
  225. /* ------------------------------------------------------- */
  226. /*
  227. * Function irtty_stop_receiver (tty, stop)
  228. *
  229. */
  230. static inline void irtty_stop_receiver(struct tty_struct *tty, int stop)
  231. {
  232. struct ktermios old_termios;
  233. int cflag;
  234. down_write(&tty->termios_rwsem);
  235. old_termios = tty->termios;
  236. cflag = tty->termios.c_cflag;
  237. if (stop)
  238. cflag &= ~CREAD;
  239. else
  240. cflag |= CREAD;
  241. tty->termios.c_cflag = cflag;
  242. if (tty->ops->set_termios)
  243. tty->ops->set_termios(tty, &old_termios);
  244. up_write(&tty->termios_rwsem);
  245. }
  246. /*****************************************************************/
  247. /* serialize ldisc open/close with sir_dev */
  248. static DEFINE_MUTEX(irtty_mutex);
  249. /* notifier from sir_dev when irda% device gets opened (ifup) */
  250. static int irtty_start_dev(struct sir_dev *dev)
  251. {
  252. struct sirtty_cb *priv;
  253. struct tty_struct *tty;
  254. /* serialize with ldisc open/close */
  255. mutex_lock(&irtty_mutex);
  256. priv = dev->priv;
  257. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  258. mutex_unlock(&irtty_mutex);
  259. return -ESTALE;
  260. }
  261. tty = priv->tty;
  262. if (tty->ops->start)
  263. tty->ops->start(tty);
  264. /* Make sure we can receive more data */
  265. irtty_stop_receiver(tty, FALSE);
  266. mutex_unlock(&irtty_mutex);
  267. return 0;
  268. }
  269. /* notifier from sir_dev when irda% device gets closed (ifdown) */
  270. static int irtty_stop_dev(struct sir_dev *dev)
  271. {
  272. struct sirtty_cb *priv;
  273. struct tty_struct *tty;
  274. /* serialize with ldisc open/close */
  275. mutex_lock(&irtty_mutex);
  276. priv = dev->priv;
  277. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  278. mutex_unlock(&irtty_mutex);
  279. return -ESTALE;
  280. }
  281. tty = priv->tty;
  282. /* Make sure we don't receive more data */
  283. irtty_stop_receiver(tty, TRUE);
  284. if (tty->ops->stop)
  285. tty->ops->stop(tty);
  286. mutex_unlock(&irtty_mutex);
  287. return 0;
  288. }
  289. /* ------------------------------------------------------- */
  290. static struct sir_driver sir_tty_drv = {
  291. .owner = THIS_MODULE,
  292. .driver_name = "sir_tty",
  293. .start_dev = irtty_start_dev,
  294. .stop_dev = irtty_stop_dev,
  295. .do_write = irtty_do_write,
  296. .chars_in_buffer = irtty_chars_in_buffer,
  297. .wait_until_sent = irtty_wait_until_sent,
  298. .set_speed = irtty_change_speed,
  299. .set_dtr_rts = irtty_set_dtr_rts,
  300. };
  301. /* ------------------------------------------------------- */
  302. /*
  303. * Function irtty_ioctl (tty, file, cmd, arg)
  304. *
  305. * The Swiss army knife of system calls :-)
  306. *
  307. */
  308. static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  309. {
  310. struct irtty_info { char name[6]; } info;
  311. struct sir_dev *dev;
  312. struct sirtty_cb *priv = tty->disc_data;
  313. int err = 0;
  314. IRDA_ASSERT(priv != NULL, return -ENODEV;);
  315. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;);
  316. pr_debug("%s(cmd=0x%X)\n", __func__, cmd);
  317. dev = priv->dev;
  318. IRDA_ASSERT(dev != NULL, return -1;);
  319. switch (cmd) {
  320. case IRTTY_IOCTDONGLE:
  321. /* this call blocks for completion */
  322. err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg);
  323. break;
  324. case IRTTY_IOCGET:
  325. IRDA_ASSERT(dev->netdev != NULL, return -1;);
  326. memset(&info, 0, sizeof(info));
  327. strncpy(info.name, dev->netdev->name, sizeof(info.name)-1);
  328. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  329. err = -EFAULT;
  330. break;
  331. default:
  332. err = tty_mode_ioctl(tty, file, cmd, arg);
  333. break;
  334. }
  335. return err;
  336. }
  337. /*
  338. * Function irtty_open(tty)
  339. *
  340. * This function is called by the TTY module when the IrDA line
  341. * discipline is called for. Because we are sure the tty line exists,
  342. * we only have to link it to a free IrDA channel.
  343. */
  344. static int irtty_open(struct tty_struct *tty)
  345. {
  346. struct sir_dev *dev;
  347. struct sirtty_cb *priv;
  348. int ret = 0;
  349. /* Module stuff handled via irda_ldisc.owner - Jean II */
  350. /* stop the underlying driver */
  351. irtty_stop_receiver(tty, TRUE);
  352. if (tty->ops->stop)
  353. tty->ops->stop(tty);
  354. tty_driver_flush_buffer(tty);
  355. /* apply mtt override */
  356. sir_tty_drv.qos_mtt_bits = qos_mtt_bits;
  357. /* get a sir device instance for this driver */
  358. dev = sirdev_get_instance(&sir_tty_drv, tty->name);
  359. if (!dev) {
  360. ret = -ENODEV;
  361. goto out;
  362. }
  363. /* allocate private device info block */
  364. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  365. if (!priv) {
  366. ret = -ENOMEM;
  367. goto out_put;
  368. }
  369. priv->magic = IRTTY_MAGIC;
  370. priv->tty = tty;
  371. priv->dev = dev;
  372. /* serialize with start_dev - in case we were racing with ifup */
  373. mutex_lock(&irtty_mutex);
  374. dev->priv = priv;
  375. tty->disc_data = priv;
  376. tty->receive_room = 65536;
  377. mutex_unlock(&irtty_mutex);
  378. pr_debug("%s - %s: irda line discipline opened\n", __func__, tty->name);
  379. return 0;
  380. out_put:
  381. sirdev_put_instance(dev);
  382. out:
  383. return ret;
  384. }
  385. /*
  386. * Function irtty_close (tty)
  387. *
  388. * Close down a IrDA channel. This means flushing out any pending queues,
  389. * and then restoring the TTY line discipline to what it was before it got
  390. * hooked to IrDA (which usually is TTY again).
  391. */
  392. static void irtty_close(struct tty_struct *tty)
  393. {
  394. struct sirtty_cb *priv = tty->disc_data;
  395. IRDA_ASSERT(priv != NULL, return;);
  396. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  397. /* Hm, with a dongle attached the dongle driver wants
  398. * to close the dongle - which requires the use of
  399. * some tty write and/or termios or ioctl operations.
  400. * Are we allowed to call those when already requested
  401. * to shutdown the ldisc?
  402. * If not, we should somehow mark the dev being staled.
  403. * Question remains, how to close the dongle in this case...
  404. * For now let's assume we are granted to issue tty driver calls
  405. * until we return here from the ldisc close. I'm just wondering
  406. * how this behaves with hotpluggable serial hardware like
  407. * rs232-pcmcia card or usb-serial...
  408. *
  409. * priv->tty = NULL?;
  410. */
  411. /* we are dead now */
  412. tty->disc_data = NULL;
  413. sirdev_put_instance(priv->dev);
  414. /* Stop tty */
  415. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  416. if (tty->ops->stop)
  417. tty->ops->stop(tty);
  418. kfree(priv);
  419. pr_debug("%s - %s: irda line discipline closed\n", __func__, tty->name);
  420. }
  421. /* ------------------------------------------------------- */
  422. static struct tty_ldisc_ops irda_ldisc = {
  423. .magic = TTY_LDISC_MAGIC,
  424. .name = "irda",
  425. .flags = 0,
  426. .open = irtty_open,
  427. .close = irtty_close,
  428. .read = NULL,
  429. .write = NULL,
  430. .ioctl = irtty_ioctl,
  431. .poll = NULL,
  432. .receive_buf = irtty_receive_buf,
  433. .write_wakeup = irtty_write_wakeup,
  434. .owner = THIS_MODULE,
  435. };
  436. /* ------------------------------------------------------- */
  437. static int __init irtty_sir_init(void)
  438. {
  439. int err;
  440. if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0)
  441. net_err_ratelimited("IrDA: can't register line discipline (err = %d)\n",
  442. err);
  443. return err;
  444. }
  445. static void __exit irtty_sir_cleanup(void)
  446. {
  447. int err;
  448. if ((err = tty_unregister_ldisc(N_IRDA))) {
  449. net_err_ratelimited("%s(), can't unregister line discipline (err = %d)\n",
  450. __func__, err);
  451. }
  452. }
  453. module_init(irtty_sir_init);
  454. module_exit(irtty_sir_cleanup);
  455. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  456. MODULE_DESCRIPTION("IrDA TTY device driver");
  457. MODULE_ALIAS_LDISC(N_IRDA);
  458. MODULE_LICENSE("GPL");