irtty-sir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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_receive_room (tty)
  245. *
  246. * Used by the TTY to find out how much data we can receive at a time
  247. *
  248. */
  249. static int irtty_receive_room(struct tty_struct *tty)
  250. {
  251. struct sirtty_cb *priv = tty->disc_data;
  252. IRDA_ASSERT(priv != NULL, return 0;);
  253. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return 0;);
  254. return 65536; /* We can handle an infinite amount of data. :-) */
  255. }
  256. /*
  257. * Function irtty_write_wakeup (tty)
  258. *
  259. * Called by the driver when there's room for more data. If we have
  260. * more packets to send, we send them here.
  261. *
  262. */
  263. static void irtty_write_wakeup(struct tty_struct *tty)
  264. {
  265. struct sirtty_cb *priv = tty->disc_data;
  266. IRDA_ASSERT(priv != NULL, return;);
  267. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  268. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  269. if (priv->dev)
  270. sirdev_write_complete(priv->dev);
  271. }
  272. /* ------------------------------------------------------- */
  273. /*
  274. * Function irtty_stop_receiver (tty, stop)
  275. *
  276. */
  277. static inline void irtty_stop_receiver(struct tty_struct *tty, int stop)
  278. {
  279. struct termios old_termios;
  280. int cflag;
  281. lock_kernel();
  282. old_termios = *(tty->termios);
  283. cflag = tty->termios->c_cflag;
  284. if (stop)
  285. cflag &= ~CREAD;
  286. else
  287. cflag |= CREAD;
  288. tty->termios->c_cflag = cflag;
  289. if (tty->driver->set_termios)
  290. tty->driver->set_termios(tty, &old_termios);
  291. unlock_kernel();
  292. }
  293. /*****************************************************************/
  294. /* serialize ldisc open/close with sir_dev */
  295. static DECLARE_MUTEX(irtty_sem);
  296. /* notifier from sir_dev when irda% device gets opened (ifup) */
  297. static int irtty_start_dev(struct sir_dev *dev)
  298. {
  299. struct sirtty_cb *priv;
  300. struct tty_struct *tty;
  301. /* serialize with ldisc open/close */
  302. down(&irtty_sem);
  303. priv = dev->priv;
  304. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  305. up(&irtty_sem);
  306. return -ESTALE;
  307. }
  308. tty = priv->tty;
  309. if (tty->driver->start)
  310. tty->driver->start(tty);
  311. /* Make sure we can receive more data */
  312. irtty_stop_receiver(tty, FALSE);
  313. up(&irtty_sem);
  314. return 0;
  315. }
  316. /* notifier from sir_dev when irda% device gets closed (ifdown) */
  317. static int irtty_stop_dev(struct sir_dev *dev)
  318. {
  319. struct sirtty_cb *priv;
  320. struct tty_struct *tty;
  321. /* serialize with ldisc open/close */
  322. down(&irtty_sem);
  323. priv = dev->priv;
  324. if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
  325. up(&irtty_sem);
  326. return -ESTALE;
  327. }
  328. tty = priv->tty;
  329. /* Make sure we don't receive more data */
  330. irtty_stop_receiver(tty, TRUE);
  331. if (tty->driver->stop)
  332. tty->driver->stop(tty);
  333. up(&irtty_sem);
  334. return 0;
  335. }
  336. /* ------------------------------------------------------- */
  337. static struct sir_driver sir_tty_drv = {
  338. .owner = THIS_MODULE,
  339. .driver_name = "sir_tty",
  340. .start_dev = irtty_start_dev,
  341. .stop_dev = irtty_stop_dev,
  342. .do_write = irtty_do_write,
  343. .chars_in_buffer = irtty_chars_in_buffer,
  344. .wait_until_sent = irtty_wait_until_sent,
  345. .set_speed = irtty_change_speed,
  346. .set_dtr_rts = irtty_set_dtr_rts,
  347. };
  348. /* ------------------------------------------------------- */
  349. /*
  350. * Function irtty_ioctl (tty, file, cmd, arg)
  351. *
  352. * The Swiss army knife of system calls :-)
  353. *
  354. */
  355. static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  356. {
  357. struct irtty_info { char name[6]; } info;
  358. struct sir_dev *dev;
  359. struct sirtty_cb *priv = tty->disc_data;
  360. int err = 0;
  361. IRDA_ASSERT(priv != NULL, return -ENODEV;);
  362. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;);
  363. IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __FUNCTION__, cmd);
  364. dev = priv->dev;
  365. IRDA_ASSERT(dev != NULL, return -1;);
  366. switch (cmd) {
  367. case TCGETS:
  368. case TCGETA:
  369. err = n_tty_ioctl(tty, file, cmd, arg);
  370. break;
  371. case IRTTY_IOCTDONGLE:
  372. /* this call blocks for completion */
  373. err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg);
  374. break;
  375. case IRTTY_IOCGET:
  376. IRDA_ASSERT(dev->netdev != NULL, return -1;);
  377. memset(&info, 0, sizeof(info));
  378. strncpy(info.name, dev->netdev->name, sizeof(info.name)-1);
  379. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  380. err = -EFAULT;
  381. break;
  382. default:
  383. err = -ENOIOCTLCMD;
  384. break;
  385. }
  386. return err;
  387. }
  388. /*
  389. * Function irtty_open(tty)
  390. *
  391. * This function is called by the TTY module when the IrDA line
  392. * discipline is called for. Because we are sure the tty line exists,
  393. * we only have to link it to a free IrDA channel.
  394. */
  395. static int irtty_open(struct tty_struct *tty)
  396. {
  397. struct sir_dev *dev;
  398. struct sirtty_cb *priv;
  399. int ret = 0;
  400. /* Module stuff handled via irda_ldisc.owner - Jean II */
  401. /* First make sure we're not already connected. */
  402. if (tty->disc_data != NULL) {
  403. priv = tty->disc_data;
  404. if (priv && priv->magic == IRTTY_MAGIC) {
  405. ret = -EEXIST;
  406. goto out;
  407. }
  408. tty->disc_data = NULL; /* ### */
  409. }
  410. /* stop the underlying driver */
  411. irtty_stop_receiver(tty, TRUE);
  412. if (tty->driver->stop)
  413. tty->driver->stop(tty);
  414. if (tty->driver->flush_buffer)
  415. tty->driver->flush_buffer(tty);
  416. /* apply mtt override */
  417. sir_tty_drv.qos_mtt_bits = qos_mtt_bits;
  418. /* get a sir device instance for this driver */
  419. dev = sirdev_get_instance(&sir_tty_drv, tty->name);
  420. if (!dev) {
  421. ret = -ENODEV;
  422. goto out;
  423. }
  424. /* allocate private device info block */
  425. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  426. if (!priv)
  427. goto out_put;
  428. memset(priv, 0, sizeof(*priv));
  429. priv->magic = IRTTY_MAGIC;
  430. priv->tty = tty;
  431. priv->dev = dev;
  432. /* serialize with start_dev - in case we were racing with ifup */
  433. down(&irtty_sem);
  434. dev->priv = priv;
  435. tty->disc_data = priv;
  436. up(&irtty_sem);
  437. IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __FUNCTION__, tty->name);
  438. return 0;
  439. out_put:
  440. sirdev_put_instance(dev);
  441. out:
  442. return ret;
  443. }
  444. /*
  445. * Function irtty_close (tty)
  446. *
  447. * Close down a IrDA channel. This means flushing out any pending queues,
  448. * and then restoring the TTY line discipline to what it was before it got
  449. * hooked to IrDA (which usually is TTY again).
  450. */
  451. static void irtty_close(struct tty_struct *tty)
  452. {
  453. struct sirtty_cb *priv = tty->disc_data;
  454. IRDA_ASSERT(priv != NULL, return;);
  455. IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
  456. /* Hm, with a dongle attached the dongle driver wants
  457. * to close the dongle - which requires the use of
  458. * some tty write and/or termios or ioctl operations.
  459. * Are we allowed to call those when already requested
  460. * to shutdown the ldisc?
  461. * If not, we should somehow mark the dev being staled.
  462. * Question remains, how to close the dongle in this case...
  463. * For now let's assume we are granted to issue tty driver calls
  464. * until we return here from the ldisc close. I'm just wondering
  465. * how this behaves with hotpluggable serial hardware like
  466. * rs232-pcmcia card or usb-serial...
  467. *
  468. * priv->tty = NULL?;
  469. */
  470. /* we are dead now */
  471. tty->disc_data = NULL;
  472. sirdev_put_instance(priv->dev);
  473. /* Stop tty */
  474. irtty_stop_receiver(tty, TRUE);
  475. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  476. if (tty->driver->stop)
  477. tty->driver->stop(tty);
  478. kfree(priv);
  479. IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __FUNCTION__, tty->name);
  480. }
  481. /* ------------------------------------------------------- */
  482. static struct tty_ldisc irda_ldisc = {
  483. .magic = TTY_LDISC_MAGIC,
  484. .name = "irda",
  485. .flags = 0,
  486. .open = irtty_open,
  487. .close = irtty_close,
  488. .read = NULL,
  489. .write = NULL,
  490. .ioctl = irtty_ioctl,
  491. .poll = NULL,
  492. .receive_buf = irtty_receive_buf,
  493. .receive_room = irtty_receive_room,
  494. .write_wakeup = irtty_write_wakeup,
  495. .owner = THIS_MODULE,
  496. };
  497. /* ------------------------------------------------------- */
  498. static int __init irtty_sir_init(void)
  499. {
  500. int err;
  501. if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0)
  502. IRDA_ERROR("IrDA: can't register line discipline (err = %d)\n",
  503. err);
  504. return err;
  505. }
  506. static void __exit irtty_sir_cleanup(void)
  507. {
  508. int err;
  509. if ((err = tty_unregister_ldisc(N_IRDA))) {
  510. IRDA_ERROR("%s(), can't unregister line discipline (err = %d)\n",
  511. __FUNCTION__, err);
  512. }
  513. }
  514. module_init(irtty_sir_init);
  515. module_exit(irtty_sir_cleanup);
  516. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  517. MODULE_DESCRIPTION("IrDA TTY device driver");
  518. MODULE_ALIAS_LDISC(N_IRDA);
  519. MODULE_LICENSE("GPL");