irtty-sir.c 14 KB

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