irtty-sir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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/smp_lock.h>
  34. #include <linux/delay.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 priv->tty->driver->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->driver->wait_until_sent) {
  80. lock_kernel();
  81. tty->driver->wait_until_sent(tty, msecs_to_jiffies(100));
  82. unlock_kernel();
  83. }
  84. else {
  85. msleep(USBSERIAL_TX_DONE_DELAY);
  86. }
  87. }
  88. /*
  89. * Function irtty_change_speed (dev, speed)
  90. *
  91. * Change the speed of the serial port.
  92. *
  93. * This may sleep in set_termios (usbserial driver f.e.) and must
  94. * not be called from interrupt/timer/tasklet therefore.
  95. * All such invocations are deferred to kIrDAd now so we can sleep there.
  96. */
  97. static int irtty_change_speed(struct sir_dev *dev, unsigned speed)
  98. {
  99. struct sirtty_cb *priv = dev->priv;
  100. struct tty_struct *tty;
  101. struct termios old_termios;
  102. int cflag;
  103. IRDA_ASSERT(priv != NULL, return -1;);
  104. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
  105. tty = priv->tty;
  106. lock_kernel();
  107. old_termios = *(tty->termios);
  108. cflag = tty->termios->c_cflag;
  109. cflag &= ~CBAUD;
  110. IRDA_DEBUG(2, "%s(), Setting speed to %d\n", __FUNCTION__, speed);
  111. switch (speed) {
  112. case 1200:
  113. cflag |= B1200;
  114. break;
  115. case 2400:
  116. cflag |= B2400;
  117. break;
  118. case 4800:
  119. cflag |= B4800;
  120. break;
  121. case 19200:
  122. cflag |= B19200;
  123. break;
  124. case 38400:
  125. cflag |= B38400;
  126. break;
  127. case 57600:
  128. cflag |= B57600;
  129. break;
  130. case 115200:
  131. cflag |= B115200;
  132. break;
  133. case 9600:
  134. default:
  135. cflag |= B9600;
  136. break;
  137. }
  138. tty->termios->c_cflag = cflag;
  139. if (tty->driver->set_termios)
  140. tty->driver->set_termios(tty, &old_termios);
  141. unlock_kernel();
  142. priv->io.speed = speed;
  143. return 0;
  144. }
  145. /*
  146. * Function irtty_set_dtr_rts (dev, dtr, rts)
  147. *
  148. * This function can be used by dongles etc. to set or reset the status
  149. * of the dtr and rts lines
  150. */
  151. static int irtty_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
  152. {
  153. struct sirtty_cb *priv = dev->priv;
  154. int set = 0;
  155. int clear = 0;
  156. IRDA_ASSERT(priv != NULL, return -1;);
  157. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
  158. if (rts)
  159. set |= TIOCM_RTS;
  160. else
  161. clear |= TIOCM_RTS;
  162. if (dtr)
  163. set |= TIOCM_DTR;
  164. else
  165. clear |= TIOCM_DTR;
  166. /*
  167. * We can't use ioctl() because it expects a non-null file structure,
  168. * and we don't have that here.
  169. * This function is not yet defined for all tty driver, so
  170. * let's be careful... Jean II
  171. */
  172. IRDA_ASSERT(priv->tty->driver->tiocmset != NULL, return -1;);
  173. priv->tty->driver->tiocmset(priv->tty, NULL, set, clear);
  174. return 0;
  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. 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. IRDA_ASSERT(priv != NULL, return -1;);
  187. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
  188. tty = priv->tty;
  189. if (!tty->driver->write)
  190. return 0;
  191. tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
  192. if (tty->driver->write_room) {
  193. writelen = tty->driver->write_room(tty);
  194. if (writelen > len)
  195. writelen = len;
  196. }
  197. else
  198. writelen = len;
  199. return tty->driver->write(tty, ptr, writelen);
  200. }
  201. /* ------------------------------------------------------- */
  202. /* irda line discipline callbacks */
  203. /*
  204. * Function irtty_receive_buf( tty, cp, count)
  205. *
  206. * Handle the 'receiver data ready' interrupt. This function is called
  207. * by the 'tty_io' module in the kernel when a block of IrDA data has
  208. * been received, which can now be decapsulated and delivered for
  209. * further processing
  210. *
  211. * calling context depends on underlying driver and tty->low_latency!
  212. * for example (low_latency: 1 / 0):
  213. * serial.c: uart-interrupt / softint
  214. * usbserial: urb-complete-interrupt / softint
  215. */
  216. static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  217. char *fp, int count)
  218. {
  219. struct sir_dev *dev;
  220. struct sirtty_cb *priv = tty->disc_data;
  221. int i;
  222. IRDA_ASSERT(priv != NULL, return;);
  223. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  224. if (unlikely(count==0)) /* yes, this happens */
  225. return;
  226. dev = priv->dev;
  227. if (!dev) {
  228. IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__);
  229. return;
  230. }
  231. for (i = 0; i < count; i++) {
  232. /*
  233. * Characters received with a parity error, etc?
  234. */
  235. if (fp && *fp++) {
  236. IRDA_DEBUG(0, "Framing or parity error!\n");
  237. sirdev_receive(dev, NULL, 0); /* notify sir_dev (updating stats) */
  238. return;
  239. }
  240. }
  241. sirdev_receive(dev, cp, count);
  242. }
  243. /*
  244. * Function irtty_write_wakeup (tty)
  245. *
  246. * Called by the driver when there's room for more data. If we have
  247. * more packets to send, we send them here.
  248. *
  249. */
  250. static void irtty_write_wakeup(struct tty_struct *tty)
  251. {
  252. struct sirtty_cb *priv = tty->disc_data;
  253. IRDA_ASSERT(priv != NULL, return;);
  254. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  255. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  256. if (priv->dev)
  257. sirdev_write_complete(priv->dev);
  258. }
  259. /* ------------------------------------------------------- */
  260. /*
  261. * Function irtty_stop_receiver (tty, stop)
  262. *
  263. */
  264. static inline void irtty_stop_receiver(struct tty_struct *tty, int stop)
  265. {
  266. struct termios old_termios;
  267. int cflag;
  268. lock_kernel();
  269. old_termios = *(tty->termios);
  270. cflag = tty->termios->c_cflag;
  271. if (stop)
  272. cflag &= ~CREAD;
  273. else
  274. cflag |= CREAD;
  275. tty->termios->c_cflag = cflag;
  276. if (tty->driver->set_termios)
  277. tty->driver->set_termios(tty, &old_termios);
  278. unlock_kernel();
  279. }
  280. /*****************************************************************/
  281. /* serialize ldisc open/close with sir_dev */
  282. static DECLARE_MUTEX(irtty_sem);
  283. /* notifier from sir_dev when irda% device gets opened (ifup) */
  284. static int irtty_start_dev(struct sir_dev *dev)
  285. {
  286. struct sirtty_cb *priv;
  287. struct tty_struct *tty;
  288. /* serialize with ldisc open/close */
  289. down(&irtty_sem);
  290. priv = dev->priv;
  291. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  292. up(&irtty_sem);
  293. return -ESTALE;
  294. }
  295. tty = priv->tty;
  296. if (tty->driver->start)
  297. tty->driver->start(tty);
  298. /* Make sure we can receive more data */
  299. irtty_stop_receiver(tty, FALSE);
  300. up(&irtty_sem);
  301. return 0;
  302. }
  303. /* notifier from sir_dev when irda% device gets closed (ifdown) */
  304. static int irtty_stop_dev(struct sir_dev *dev)
  305. {
  306. struct sirtty_cb *priv;
  307. struct tty_struct *tty;
  308. /* serialize with ldisc open/close */
  309. down(&irtty_sem);
  310. priv = dev->priv;
  311. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  312. up(&irtty_sem);
  313. return -ESTALE;
  314. }
  315. tty = priv->tty;
  316. /* Make sure we don't receive more data */
  317. irtty_stop_receiver(tty, TRUE);
  318. if (tty->driver->stop)
  319. tty->driver->stop(tty);
  320. up(&irtty_sem);
  321. return 0;
  322. }
  323. /* ------------------------------------------------------- */
  324. static struct sir_driver sir_tty_drv = {
  325. .owner = THIS_MODULE,
  326. .driver_name = "sir_tty",
  327. .start_dev = irtty_start_dev,
  328. .stop_dev = irtty_stop_dev,
  329. .do_write = irtty_do_write,
  330. .chars_in_buffer = irtty_chars_in_buffer,
  331. .wait_until_sent = irtty_wait_until_sent,
  332. .set_speed = irtty_change_speed,
  333. .set_dtr_rts = irtty_set_dtr_rts,
  334. };
  335. /* ------------------------------------------------------- */
  336. /*
  337. * Function irtty_ioctl (tty, file, cmd, arg)
  338. *
  339. * The Swiss army knife of system calls :-)
  340. *
  341. */
  342. static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  343. {
  344. struct irtty_info { char name[6]; } info;
  345. struct sir_dev *dev;
  346. struct sirtty_cb *priv = tty->disc_data;
  347. int err = 0;
  348. IRDA_ASSERT(priv != NULL, return -ENODEV;);
  349. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;);
  350. IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __FUNCTION__, cmd);
  351. dev = priv->dev;
  352. IRDA_ASSERT(dev != NULL, return -1;);
  353. switch (cmd) {
  354. case TCGETS:
  355. case TCGETA:
  356. err = n_tty_ioctl(tty, file, cmd, arg);
  357. break;
  358. case IRTTY_IOCTDONGLE:
  359. /* this call blocks for completion */
  360. err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg);
  361. break;
  362. case IRTTY_IOCGET:
  363. IRDA_ASSERT(dev->netdev != NULL, return -1;);
  364. memset(&info, 0, sizeof(info));
  365. strncpy(info.name, dev->netdev->name, sizeof(info.name)-1);
  366. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  367. err = -EFAULT;
  368. break;
  369. default:
  370. err = -ENOIOCTLCMD;
  371. break;
  372. }
  373. return err;
  374. }
  375. /*
  376. * Function irtty_open(tty)
  377. *
  378. * This function is called by the TTY module when the IrDA line
  379. * discipline is called for. Because we are sure the tty line exists,
  380. * we only have to link it to a free IrDA channel.
  381. */
  382. static int irtty_open(struct tty_struct *tty)
  383. {
  384. struct sir_dev *dev;
  385. struct sirtty_cb *priv;
  386. int ret = 0;
  387. /* Module stuff handled via irda_ldisc.owner - Jean II */
  388. /* First make sure we're not already connected. */
  389. if (tty->disc_data != NULL) {
  390. priv = tty->disc_data;
  391. if (priv && priv->magic == IRTTY_MAGIC) {
  392. ret = -EEXIST;
  393. goto out;
  394. }
  395. tty->disc_data = NULL; /* ### */
  396. }
  397. /* stop the underlying driver */
  398. irtty_stop_receiver(tty, TRUE);
  399. if (tty->driver->stop)
  400. tty->driver->stop(tty);
  401. if (tty->driver->flush_buffer)
  402. tty->driver->flush_buffer(tty);
  403. /* apply mtt override */
  404. sir_tty_drv.qos_mtt_bits = qos_mtt_bits;
  405. /* get a sir device instance for this driver */
  406. dev = sirdev_get_instance(&sir_tty_drv, tty->name);
  407. if (!dev) {
  408. ret = -ENODEV;
  409. goto out;
  410. }
  411. /* allocate private device info block */
  412. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  413. if (!priv)
  414. goto out_put;
  415. memset(priv, 0, sizeof(*priv));
  416. priv->magic = IRTTY_MAGIC;
  417. priv->tty = tty;
  418. priv->dev = dev;
  419. /* serialize with start_dev - in case we were racing with ifup */
  420. down(&irtty_sem);
  421. dev->priv = priv;
  422. tty->disc_data = priv;
  423. tty->receive_room = 65536;
  424. up(&irtty_sem);
  425. IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __FUNCTION__, tty->name);
  426. return 0;
  427. out_put:
  428. sirdev_put_instance(dev);
  429. out:
  430. return ret;
  431. }
  432. /*
  433. * Function irtty_close (tty)
  434. *
  435. * Close down a IrDA channel. This means flushing out any pending queues,
  436. * and then restoring the TTY line discipline to what it was before it got
  437. * hooked to IrDA (which usually is TTY again).
  438. */
  439. static void irtty_close(struct tty_struct *tty)
  440. {
  441. struct sirtty_cb *priv = tty->disc_data;
  442. IRDA_ASSERT(priv != NULL, return;);
  443. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  444. /* Hm, with a dongle attached the dongle driver wants
  445. * to close the dongle - which requires the use of
  446. * some tty write and/or termios or ioctl operations.
  447. * Are we allowed to call those when already requested
  448. * to shutdown the ldisc?
  449. * If not, we should somehow mark the dev being staled.
  450. * Question remains, how to close the dongle in this case...
  451. * For now let's assume we are granted to issue tty driver calls
  452. * until we return here from the ldisc close. I'm just wondering
  453. * how this behaves with hotpluggable serial hardware like
  454. * rs232-pcmcia card or usb-serial...
  455. *
  456. * priv->tty = NULL?;
  457. */
  458. /* we are dead now */
  459. tty->disc_data = NULL;
  460. sirdev_put_instance(priv->dev);
  461. /* Stop tty */
  462. irtty_stop_receiver(tty, TRUE);
  463. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  464. if (tty->driver->stop)
  465. tty->driver->stop(tty);
  466. kfree(priv);
  467. IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __FUNCTION__, tty->name);
  468. }
  469. /* ------------------------------------------------------- */
  470. static struct tty_ldisc irda_ldisc = {
  471. .magic = TTY_LDISC_MAGIC,
  472. .name = "irda",
  473. .flags = 0,
  474. .open = irtty_open,
  475. .close = irtty_close,
  476. .read = NULL,
  477. .write = NULL,
  478. .ioctl = irtty_ioctl,
  479. .poll = NULL,
  480. .receive_buf = irtty_receive_buf,
  481. .write_wakeup = irtty_write_wakeup,
  482. .owner = THIS_MODULE,
  483. };
  484. /* ------------------------------------------------------- */
  485. static int __init irtty_sir_init(void)
  486. {
  487. int err;
  488. if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0)
  489. IRDA_ERROR("IrDA: can't register line discipline (err = %d)\n",
  490. err);
  491. return err;
  492. }
  493. static void __exit irtty_sir_cleanup(void)
  494. {
  495. int err;
  496. if ((err = tty_unregister_ldisc(N_IRDA))) {
  497. IRDA_ERROR("%s(), can't unregister line discipline (err = %d)\n",
  498. __FUNCTION__, err);
  499. }
  500. }
  501. module_init(irtty_sir_init);
  502. module_exit(irtty_sir_cleanup);
  503. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  504. MODULE_DESCRIPTION("IrDA TTY device driver");
  505. MODULE_ALIAS_LDISC(N_IRDA);
  506. MODULE_LICENSE("GPL");