irtty-sir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. mutex_lock(&tty->termios_mutex);
  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. mutex_unlock(&tty->termios_mutex);
  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->low_latency!
  178. * for example (low_latency: 1 / 0):
  179. * serial.c: uart-interrupt / softint
  180. * usbserial: urb-complete-interrupt / softint
  181. */
  182. static unsigned int irtty_receive_buf(struct tty_struct *tty,
  183. const unsigned char *cp, 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 -ENODEV;);
  189. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EINVAL;);
  190. if (unlikely(count==0)) /* yes, this happens */
  191. return 0;
  192. dev = priv->dev;
  193. if (!dev) {
  194. IRDA_WARNING("%s(), not ready yet!\n", __func__);
  195. return -ENODEV;
  196. }
  197. for (i = 0; i < count; i++) {
  198. /*
  199. * Characters received with a parity error, etc?
  200. */
  201. if (fp && *fp++) {
  202. IRDA_DEBUG(0, "Framing or parity error!\n");
  203. sirdev_receive(dev, NULL, 0); /* notify sir_dev (updating stats) */
  204. return -EINVAL;
  205. }
  206. }
  207. sirdev_receive(dev, cp, count);
  208. return count;
  209. }
  210. /*
  211. * Function irtty_write_wakeup (tty)
  212. *
  213. * Called by the driver when there's room for more data. If we have
  214. * more packets to send, we send them here.
  215. *
  216. */
  217. static void irtty_write_wakeup(struct tty_struct *tty)
  218. {
  219. struct sirtty_cb *priv = tty->disc_data;
  220. IRDA_ASSERT(priv != NULL, return;);
  221. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  222. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  223. if (priv->dev)
  224. sirdev_write_complete(priv->dev);
  225. }
  226. /* ------------------------------------------------------- */
  227. /*
  228. * Function irtty_stop_receiver (tty, stop)
  229. *
  230. */
  231. static inline void irtty_stop_receiver(struct tty_struct *tty, int stop)
  232. {
  233. struct ktermios old_termios;
  234. int cflag;
  235. mutex_lock(&tty->termios_mutex);
  236. old_termios = *(tty->termios);
  237. cflag = tty->termios->c_cflag;
  238. if (stop)
  239. cflag &= ~CREAD;
  240. else
  241. cflag |= CREAD;
  242. tty->termios->c_cflag = cflag;
  243. if (tty->ops->set_termios)
  244. tty->ops->set_termios(tty, &old_termios);
  245. mutex_unlock(&tty->termios_mutex);
  246. }
  247. /*****************************************************************/
  248. /* serialize ldisc open/close with sir_dev */
  249. static DEFINE_MUTEX(irtty_mutex);
  250. /* notifier from sir_dev when irda% device gets opened (ifup) */
  251. static int irtty_start_dev(struct sir_dev *dev)
  252. {
  253. struct sirtty_cb *priv;
  254. struct tty_struct *tty;
  255. /* serialize with ldisc open/close */
  256. mutex_lock(&irtty_mutex);
  257. priv = dev->priv;
  258. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  259. mutex_unlock(&irtty_mutex);
  260. return -ESTALE;
  261. }
  262. tty = priv->tty;
  263. if (tty->ops->start)
  264. tty->ops->start(tty);
  265. /* Make sure we can receive more data */
  266. irtty_stop_receiver(tty, FALSE);
  267. mutex_unlock(&irtty_mutex);
  268. return 0;
  269. }
  270. /* notifier from sir_dev when irda% device gets closed (ifdown) */
  271. static int irtty_stop_dev(struct sir_dev *dev)
  272. {
  273. struct sirtty_cb *priv;
  274. struct tty_struct *tty;
  275. /* serialize with ldisc open/close */
  276. mutex_lock(&irtty_mutex);
  277. priv = dev->priv;
  278. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  279. mutex_unlock(&irtty_mutex);
  280. return -ESTALE;
  281. }
  282. tty = priv->tty;
  283. /* Make sure we don't receive more data */
  284. irtty_stop_receiver(tty, TRUE);
  285. if (tty->ops->stop)
  286. tty->ops->stop(tty);
  287. mutex_unlock(&irtty_mutex);
  288. return 0;
  289. }
  290. /* ------------------------------------------------------- */
  291. static struct sir_driver sir_tty_drv = {
  292. .owner = THIS_MODULE,
  293. .driver_name = "sir_tty",
  294. .start_dev = irtty_start_dev,
  295. .stop_dev = irtty_stop_dev,
  296. .do_write = irtty_do_write,
  297. .chars_in_buffer = irtty_chars_in_buffer,
  298. .wait_until_sent = irtty_wait_until_sent,
  299. .set_speed = irtty_change_speed,
  300. .set_dtr_rts = irtty_set_dtr_rts,
  301. };
  302. /* ------------------------------------------------------- */
  303. /*
  304. * Function irtty_ioctl (tty, file, cmd, arg)
  305. *
  306. * The Swiss army knife of system calls :-)
  307. *
  308. */
  309. static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  310. {
  311. struct irtty_info { char name[6]; } info;
  312. struct sir_dev *dev;
  313. struct sirtty_cb *priv = tty->disc_data;
  314. int err = 0;
  315. IRDA_ASSERT(priv != NULL, return -ENODEV;);
  316. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;);
  317. IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __func__, cmd);
  318. dev = priv->dev;
  319. IRDA_ASSERT(dev != NULL, return -1;);
  320. switch (cmd) {
  321. case IRTTY_IOCTDONGLE:
  322. /* this call blocks for completion */
  323. err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg);
  324. break;
  325. case IRTTY_IOCGET:
  326. IRDA_ASSERT(dev->netdev != NULL, return -1;);
  327. memset(&info, 0, sizeof(info));
  328. strncpy(info.name, dev->netdev->name, sizeof(info.name)-1);
  329. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  330. err = -EFAULT;
  331. break;
  332. default:
  333. err = tty_mode_ioctl(tty, file, cmd, arg);
  334. break;
  335. }
  336. return err;
  337. }
  338. /*
  339. * Function irtty_open(tty)
  340. *
  341. * This function is called by the TTY module when the IrDA line
  342. * discipline is called for. Because we are sure the tty line exists,
  343. * we only have to link it to a free IrDA channel.
  344. */
  345. static int irtty_open(struct tty_struct *tty)
  346. {
  347. struct sir_dev *dev;
  348. struct sirtty_cb *priv;
  349. int ret = 0;
  350. /* Module stuff handled via irda_ldisc.owner - Jean II */
  351. /* First make sure we're not already connected. */
  352. if (tty->disc_data != NULL) {
  353. priv = tty->disc_data;
  354. if (priv && priv->magic == IRTTY_MAGIC) {
  355. ret = -EEXIST;
  356. goto out;
  357. }
  358. tty->disc_data = NULL; /* ### */
  359. }
  360. /* stop the underlying driver */
  361. irtty_stop_receiver(tty, TRUE);
  362. if (tty->ops->stop)
  363. tty->ops->stop(tty);
  364. tty_driver_flush_buffer(tty);
  365. /* apply mtt override */
  366. sir_tty_drv.qos_mtt_bits = qos_mtt_bits;
  367. /* get a sir device instance for this driver */
  368. dev = sirdev_get_instance(&sir_tty_drv, tty->name);
  369. if (!dev) {
  370. ret = -ENODEV;
  371. goto out;
  372. }
  373. /* allocate private device info block */
  374. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  375. if (!priv)
  376. goto out_put;
  377. priv->magic = IRTTY_MAGIC;
  378. priv->tty = tty;
  379. priv->dev = dev;
  380. /* serialize with start_dev - in case we were racing with ifup */
  381. mutex_lock(&irtty_mutex);
  382. dev->priv = priv;
  383. tty->disc_data = priv;
  384. tty->receive_room = 65536;
  385. mutex_unlock(&irtty_mutex);
  386. IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __func__, tty->name);
  387. return 0;
  388. out_put:
  389. sirdev_put_instance(dev);
  390. out:
  391. return ret;
  392. }
  393. /*
  394. * Function irtty_close (tty)
  395. *
  396. * Close down a IrDA channel. This means flushing out any pending queues,
  397. * and then restoring the TTY line discipline to what it was before it got
  398. * hooked to IrDA (which usually is TTY again).
  399. */
  400. static void irtty_close(struct tty_struct *tty)
  401. {
  402. struct sirtty_cb *priv = tty->disc_data;
  403. IRDA_ASSERT(priv != NULL, return;);
  404. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  405. /* Hm, with a dongle attached the dongle driver wants
  406. * to close the dongle - which requires the use of
  407. * some tty write and/or termios or ioctl operations.
  408. * Are we allowed to call those when already requested
  409. * to shutdown the ldisc?
  410. * If not, we should somehow mark the dev being staled.
  411. * Question remains, how to close the dongle in this case...
  412. * For now let's assume we are granted to issue tty driver calls
  413. * until we return here from the ldisc close. I'm just wondering
  414. * how this behaves with hotpluggable serial hardware like
  415. * rs232-pcmcia card or usb-serial...
  416. *
  417. * priv->tty = NULL?;
  418. */
  419. /* we are dead now */
  420. tty->disc_data = NULL;
  421. sirdev_put_instance(priv->dev);
  422. /* Stop tty */
  423. irtty_stop_receiver(tty, TRUE);
  424. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  425. if (tty->ops->stop)
  426. tty->ops->stop(tty);
  427. kfree(priv);
  428. IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __func__, tty->name);
  429. }
  430. /* ------------------------------------------------------- */
  431. static struct tty_ldisc_ops irda_ldisc = {
  432. .magic = TTY_LDISC_MAGIC,
  433. .name = "irda",
  434. .flags = 0,
  435. .open = irtty_open,
  436. .close = irtty_close,
  437. .read = NULL,
  438. .write = NULL,
  439. .ioctl = irtty_ioctl,
  440. .poll = NULL,
  441. .receive_buf = irtty_receive_buf,
  442. .write_wakeup = irtty_write_wakeup,
  443. .owner = THIS_MODULE,
  444. };
  445. /* ------------------------------------------------------- */
  446. static int __init irtty_sir_init(void)
  447. {
  448. int err;
  449. if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0)
  450. IRDA_ERROR("IrDA: can't register line discipline (err = %d)\n",
  451. err);
  452. return err;
  453. }
  454. static void __exit irtty_sir_cleanup(void)
  455. {
  456. int err;
  457. if ((err = tty_unregister_ldisc(N_IRDA))) {
  458. IRDA_ERROR("%s(), can't unregister line discipline (err = %d)\n",
  459. __func__, err);
  460. }
  461. }
  462. module_init(irtty_sir_init);
  463. module_exit(irtty_sir_cleanup);
  464. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  465. MODULE_DESCRIPTION("IrDA TTY device driver");
  466. MODULE_ALIAS_LDISC(N_IRDA);
  467. MODULE_LICENSE("GPL");