irtty-sir.c 15 KB

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