ircomm_tty.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /*********************************************************************
  2. *
  3. * Filename: ircomm_tty.c
  4. * Version: 1.0
  5. * Description: IrCOMM serial TTY driver
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Jun 6 21:00:56 1999
  9. * Modified at: Wed Feb 23 00:09:02 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Sources: serial.c and previous IrCOMM work by Takahide Higuchi
  12. *
  13. * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. *
  31. ********************************************************************/
  32. #include <linux/init.h>
  33. #include <linux/module.h>
  34. #include <linux/fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/termios.h>
  39. #include <linux/tty.h>
  40. #include <linux/tty_flip.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/device.h> /* for MODULE_ALIAS_CHARDEV_MAJOR */
  43. #include <asm/uaccess.h>
  44. #include <net/irda/irda.h>
  45. #include <net/irda/irmod.h>
  46. #include <net/irda/ircomm_core.h>
  47. #include <net/irda/ircomm_param.h>
  48. #include <net/irda/ircomm_tty_attach.h>
  49. #include <net/irda/ircomm_tty.h>
  50. static int ircomm_tty_install(struct tty_driver *driver,
  51. struct tty_struct *tty);
  52. static int ircomm_tty_open(struct tty_struct *tty, struct file *filp);
  53. static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
  54. static int ircomm_tty_write(struct tty_struct * tty,
  55. const unsigned char *buf, int count);
  56. static int ircomm_tty_write_room(struct tty_struct *tty);
  57. static void ircomm_tty_throttle(struct tty_struct *tty);
  58. static void ircomm_tty_unthrottle(struct tty_struct *tty);
  59. static int ircomm_tty_chars_in_buffer(struct tty_struct *tty);
  60. static void ircomm_tty_flush_buffer(struct tty_struct *tty);
  61. static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
  62. static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
  63. static void ircomm_tty_hangup(struct tty_struct *tty);
  64. static void ircomm_tty_do_softint(struct work_struct *work);
  65. static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
  66. static void ircomm_tty_stop(struct tty_struct *tty);
  67. static int ircomm_tty_data_indication(void *instance, void *sap,
  68. struct sk_buff *skb);
  69. static int ircomm_tty_control_indication(void *instance, void *sap,
  70. struct sk_buff *skb);
  71. static void ircomm_tty_flow_indication(void *instance, void *sap,
  72. LOCAL_FLOW cmd);
  73. #ifdef CONFIG_PROC_FS
  74. static const struct file_operations ircomm_tty_proc_fops;
  75. #endif /* CONFIG_PROC_FS */
  76. static struct tty_driver *driver;
  77. static hashbin_t *ircomm_tty = NULL;
  78. static const struct tty_operations ops = {
  79. .install = ircomm_tty_install,
  80. .open = ircomm_tty_open,
  81. .close = ircomm_tty_close,
  82. .write = ircomm_tty_write,
  83. .write_room = ircomm_tty_write_room,
  84. .chars_in_buffer = ircomm_tty_chars_in_buffer,
  85. .flush_buffer = ircomm_tty_flush_buffer,
  86. .ioctl = ircomm_tty_ioctl, /* ircomm_tty_ioctl.c */
  87. .tiocmget = ircomm_tty_tiocmget, /* ircomm_tty_ioctl.c */
  88. .tiocmset = ircomm_tty_tiocmset, /* ircomm_tty_ioctl.c */
  89. .throttle = ircomm_tty_throttle,
  90. .unthrottle = ircomm_tty_unthrottle,
  91. .send_xchar = ircomm_tty_send_xchar,
  92. .set_termios = ircomm_tty_set_termios,
  93. .stop = ircomm_tty_stop,
  94. .start = ircomm_tty_start,
  95. .hangup = ircomm_tty_hangup,
  96. .wait_until_sent = ircomm_tty_wait_until_sent,
  97. #ifdef CONFIG_PROC_FS
  98. .proc_fops = &ircomm_tty_proc_fops,
  99. #endif /* CONFIG_PROC_FS */
  100. };
  101. static void ircomm_port_raise_dtr_rts(struct tty_port *port, int raise)
  102. {
  103. struct ircomm_tty_cb *self = container_of(port, struct ircomm_tty_cb,
  104. port);
  105. /*
  106. * Here, we use to lock those two guys, but as ircomm_param_request()
  107. * does it itself, I don't see the point (and I see the deadlock).
  108. * Jean II
  109. */
  110. if (raise)
  111. self->settings.dte |= IRCOMM_RTS | IRCOMM_DTR;
  112. else
  113. self->settings.dte &= ~(IRCOMM_RTS | IRCOMM_DTR);
  114. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  115. }
  116. static int ircomm_port_carrier_raised(struct tty_port *port)
  117. {
  118. struct ircomm_tty_cb *self = container_of(port, struct ircomm_tty_cb,
  119. port);
  120. return self->settings.dce & IRCOMM_CD;
  121. }
  122. static const struct tty_port_operations ircomm_port_ops = {
  123. .dtr_rts = ircomm_port_raise_dtr_rts,
  124. .carrier_raised = ircomm_port_carrier_raised,
  125. };
  126. /*
  127. * Function ircomm_tty_init()
  128. *
  129. * Init IrCOMM TTY layer/driver
  130. *
  131. */
  132. static int __init ircomm_tty_init(void)
  133. {
  134. driver = alloc_tty_driver(IRCOMM_TTY_PORTS);
  135. if (!driver)
  136. return -ENOMEM;
  137. ircomm_tty = hashbin_new(HB_LOCK);
  138. if (ircomm_tty == NULL) {
  139. IRDA_ERROR("%s(), can't allocate hashbin!\n", __func__);
  140. put_tty_driver(driver);
  141. return -ENOMEM;
  142. }
  143. driver->driver_name = "ircomm";
  144. driver->name = "ircomm";
  145. driver->major = IRCOMM_TTY_MAJOR;
  146. driver->minor_start = IRCOMM_TTY_MINOR;
  147. driver->type = TTY_DRIVER_TYPE_SERIAL;
  148. driver->subtype = SERIAL_TYPE_NORMAL;
  149. driver->init_termios = tty_std_termios;
  150. driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  151. driver->flags = TTY_DRIVER_REAL_RAW;
  152. tty_set_operations(driver, &ops);
  153. if (tty_register_driver(driver)) {
  154. IRDA_ERROR("%s(): Couldn't register serial driver\n",
  155. __func__);
  156. put_tty_driver(driver);
  157. return -1;
  158. }
  159. return 0;
  160. }
  161. static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
  162. {
  163. IRDA_DEBUG(0, "%s()\n", __func__ );
  164. IRDA_ASSERT(self != NULL, return;);
  165. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  166. ircomm_tty_shutdown(self);
  167. self->magic = 0;
  168. kfree(self);
  169. }
  170. /*
  171. * Function ircomm_tty_cleanup ()
  172. *
  173. * Remove IrCOMM TTY layer/driver
  174. *
  175. */
  176. static void __exit ircomm_tty_cleanup(void)
  177. {
  178. int ret;
  179. IRDA_DEBUG(4, "%s()\n", __func__ );
  180. ret = tty_unregister_driver(driver);
  181. if (ret) {
  182. IRDA_ERROR("%s(), failed to unregister driver\n",
  183. __func__);
  184. return;
  185. }
  186. hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
  187. put_tty_driver(driver);
  188. }
  189. /*
  190. * Function ircomm_startup (self)
  191. *
  192. *
  193. *
  194. */
  195. static int ircomm_tty_startup(struct ircomm_tty_cb *self)
  196. {
  197. notify_t notify;
  198. int ret = -ENODEV;
  199. IRDA_DEBUG(2, "%s()\n", __func__ );
  200. IRDA_ASSERT(self != NULL, return -1;);
  201. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  202. /* Check if already open */
  203. if (test_and_set_bit(ASYNCB_INITIALIZED, &self->port.flags)) {
  204. IRDA_DEBUG(2, "%s(), already open so break out!\n", __func__ );
  205. return 0;
  206. }
  207. /* Register with IrCOMM */
  208. irda_notify_init(&notify);
  209. /* These callbacks we must handle ourselves */
  210. notify.data_indication = ircomm_tty_data_indication;
  211. notify.udata_indication = ircomm_tty_control_indication;
  212. notify.flow_indication = ircomm_tty_flow_indication;
  213. /* Use the ircomm_tty interface for these ones */
  214. notify.disconnect_indication = ircomm_tty_disconnect_indication;
  215. notify.connect_confirm = ircomm_tty_connect_confirm;
  216. notify.connect_indication = ircomm_tty_connect_indication;
  217. strlcpy(notify.name, "ircomm_tty", sizeof(notify.name));
  218. notify.instance = self;
  219. if (!self->ircomm) {
  220. self->ircomm = ircomm_open(&notify, self->service_type,
  221. self->line);
  222. }
  223. if (!self->ircomm)
  224. goto err;
  225. self->slsap_sel = self->ircomm->slsap_sel;
  226. /* Connect IrCOMM link with remote device */
  227. ret = ircomm_tty_attach_cable(self);
  228. if (ret < 0) {
  229. IRDA_ERROR("%s(), error attaching cable!\n", __func__);
  230. goto err;
  231. }
  232. return 0;
  233. err:
  234. clear_bit(ASYNCB_INITIALIZED, &self->port.flags);
  235. return ret;
  236. }
  237. /*
  238. * Function ircomm_block_til_ready (self, filp)
  239. *
  240. *
  241. *
  242. */
  243. static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
  244. struct tty_struct *tty, struct file *filp)
  245. {
  246. struct tty_port *port = &self->port;
  247. DECLARE_WAITQUEUE(wait, current);
  248. int retval;
  249. int do_clocal = 0, extra_count = 0;
  250. unsigned long flags;
  251. IRDA_DEBUG(2, "%s()\n", __func__ );
  252. /*
  253. * If non-blocking mode is set, or the port is not enabled,
  254. * then make the check up front and then exit.
  255. */
  256. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  257. /* nonblock mode is set or port is not enabled */
  258. port->flags |= ASYNC_NORMAL_ACTIVE;
  259. IRDA_DEBUG(1, "%s(), O_NONBLOCK requested!\n", __func__ );
  260. return 0;
  261. }
  262. if (tty->termios.c_cflag & CLOCAL) {
  263. IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
  264. do_clocal = 1;
  265. }
  266. /* Wait for carrier detect and the line to become
  267. * free (i.e., not in use by the callout). While we are in
  268. * this loop, port->count is dropped by one, so that
  269. * mgsl_close() knows when to free things. We restore it upon
  270. * exit, either normal or abnormal.
  271. */
  272. retval = 0;
  273. add_wait_queue(&port->open_wait, &wait);
  274. IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
  275. __FILE__, __LINE__, tty->driver->name, port->count);
  276. spin_lock_irqsave(&port->lock, flags);
  277. if (!tty_hung_up_p(filp)) {
  278. extra_count = 1;
  279. port->count--;
  280. }
  281. spin_unlock_irqrestore(&port->lock, flags);
  282. port->blocked_open++;
  283. while (1) {
  284. if (tty->termios.c_cflag & CBAUD)
  285. tty_port_raise_dtr_rts(port);
  286. current->state = TASK_INTERRUPTIBLE;
  287. if (tty_hung_up_p(filp) ||
  288. !test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  289. retval = (port->flags & ASYNC_HUP_NOTIFY) ?
  290. -EAGAIN : -ERESTARTSYS;
  291. break;
  292. }
  293. /*
  294. * Check if link is ready now. Even if CLOCAL is
  295. * specified, we cannot return before the IrCOMM link is
  296. * ready
  297. */
  298. if (!test_bit(ASYNCB_CLOSING, &port->flags) &&
  299. (do_clocal || tty_port_carrier_raised(port)) &&
  300. self->state == IRCOMM_TTY_READY)
  301. {
  302. break;
  303. }
  304. if (signal_pending(current)) {
  305. retval = -ERESTARTSYS;
  306. break;
  307. }
  308. IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
  309. __FILE__, __LINE__, tty->driver->name, port->count);
  310. schedule();
  311. }
  312. __set_current_state(TASK_RUNNING);
  313. remove_wait_queue(&port->open_wait, &wait);
  314. if (extra_count) {
  315. /* ++ is not atomic, so this should be protected - Jean II */
  316. spin_lock_irqsave(&port->lock, flags);
  317. port->count++;
  318. spin_unlock_irqrestore(&port->lock, flags);
  319. }
  320. port->blocked_open--;
  321. IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
  322. __FILE__, __LINE__, tty->driver->name, port->count);
  323. if (!retval)
  324. port->flags |= ASYNC_NORMAL_ACTIVE;
  325. return retval;
  326. }
  327. static int ircomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  328. {
  329. struct ircomm_tty_cb *self;
  330. unsigned int line = tty->index;
  331. /* Check if instance already exists */
  332. self = hashbin_lock_find(ircomm_tty, line, NULL);
  333. if (!self) {
  334. /* No, so make new instance */
  335. self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
  336. if (self == NULL) {
  337. IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
  338. return -ENOMEM;
  339. }
  340. tty_port_init(&self->port);
  341. self->port.ops = &ircomm_port_ops;
  342. self->magic = IRCOMM_TTY_MAGIC;
  343. self->flow = FLOW_STOP;
  344. self->line = line;
  345. INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
  346. self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
  347. self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
  348. /* Init some important stuff */
  349. init_timer(&self->watchdog_timer);
  350. spin_lock_init(&self->spinlock);
  351. /*
  352. * Force TTY into raw mode by default which is usually what
  353. * we want for IrCOMM and IrLPT. This way applications will
  354. * not have to twiddle with printcap etc.
  355. *
  356. * Note this is completely usafe and doesn't work properly
  357. */
  358. tty->termios.c_iflag = 0;
  359. tty->termios.c_oflag = 0;
  360. /* Insert into hash */
  361. hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
  362. }
  363. tty->driver_data = self;
  364. return tty_port_install(&self->port, driver, tty);
  365. }
  366. /*
  367. * Function ircomm_tty_open (tty, filp)
  368. *
  369. * This routine is called when a particular tty device is opened. This
  370. * routine is mandatory; if this routine is not filled in, the attempted
  371. * open will fail with ENODEV.
  372. */
  373. static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
  374. {
  375. struct ircomm_tty_cb *self = tty->driver_data;
  376. unsigned long flags;
  377. int ret;
  378. IRDA_DEBUG(2, "%s()\n", __func__ );
  379. /* ++ is not atomic, so this should be protected - Jean II */
  380. spin_lock_irqsave(&self->port.lock, flags);
  381. self->port.count++;
  382. spin_unlock_irqrestore(&self->port.lock, flags);
  383. tty_port_tty_set(&self->port, tty);
  384. IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
  385. self->line, self->port.count);
  386. /* Not really used by us, but lets do it anyway */
  387. tty->low_latency = (self->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  388. /*
  389. * If the port is the middle of closing, bail out now
  390. */
  391. if (tty_hung_up_p(filp) ||
  392. test_bit(ASYNCB_CLOSING, &self->port.flags)) {
  393. /* Hm, why are we blocking on ASYNC_CLOSING if we
  394. * do return -EAGAIN/-ERESTARTSYS below anyway?
  395. * IMHO it's either not needed in the first place
  396. * or for some reason we need to make sure the async
  397. * closing has been finished - if so, wouldn't we
  398. * probably better sleep uninterruptible?
  399. */
  400. if (wait_event_interruptible(self->port.close_wait,
  401. !test_bit(ASYNCB_CLOSING, &self->port.flags))) {
  402. IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
  403. __func__);
  404. return -ERESTARTSYS;
  405. }
  406. #ifdef SERIAL_DO_RESTART
  407. return (self->port.flags & ASYNC_HUP_NOTIFY) ?
  408. -EAGAIN : -ERESTARTSYS;
  409. #else
  410. return -EAGAIN;
  411. #endif
  412. }
  413. /* Check if this is a "normal" ircomm device, or an irlpt device */
  414. if (self->line < 0x10) {
  415. self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
  416. self->settings.service_type = IRCOMM_9_WIRE; /* 9 wire as default */
  417. /* Jan Kiszka -> add DSR/RI -> Conform to IrCOMM spec */
  418. self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI; /* Default line settings */
  419. IRDA_DEBUG(2, "%s(), IrCOMM device\n", __func__ );
  420. } else {
  421. IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
  422. self->service_type = IRCOMM_3_WIRE_RAW;
  423. self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
  424. }
  425. ret = ircomm_tty_startup(self);
  426. if (ret)
  427. return ret;
  428. ret = ircomm_tty_block_til_ready(self, tty, filp);
  429. if (ret) {
  430. IRDA_DEBUG(2,
  431. "%s(), returning after block_til_ready with %d\n", __func__ ,
  432. ret);
  433. return ret;
  434. }
  435. return 0;
  436. }
  437. /*
  438. * Function ircomm_tty_close (tty, filp)
  439. *
  440. * This routine is called when a particular tty device is closed.
  441. *
  442. */
  443. static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
  444. {
  445. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  446. struct tty_port *port = &self->port;
  447. IRDA_DEBUG(0, "%s()\n", __func__ );
  448. IRDA_ASSERT(self != NULL, return;);
  449. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  450. if (tty_port_close_start(port, tty, filp) == 0)
  451. return;
  452. ircomm_tty_shutdown(self);
  453. tty_driver_flush_buffer(tty);
  454. tty_port_close_end(port, tty);
  455. tty_port_tty_set(port, NULL);
  456. }
  457. /*
  458. * Function ircomm_tty_flush_buffer (tty)
  459. *
  460. *
  461. *
  462. */
  463. static void ircomm_tty_flush_buffer(struct tty_struct *tty)
  464. {
  465. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  466. IRDA_ASSERT(self != NULL, return;);
  467. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  468. /*
  469. * Let do_softint() do this to avoid race condition with
  470. * do_softint() ;-)
  471. */
  472. schedule_work(&self->tqueue);
  473. }
  474. /*
  475. * Function ircomm_tty_do_softint (work)
  476. *
  477. * We use this routine to give the write wakeup to the user at at a
  478. * safe time (as fast as possible after write have completed). This
  479. * can be compared to the Tx interrupt.
  480. */
  481. static void ircomm_tty_do_softint(struct work_struct *work)
  482. {
  483. struct ircomm_tty_cb *self =
  484. container_of(work, struct ircomm_tty_cb, tqueue);
  485. struct tty_struct *tty;
  486. unsigned long flags;
  487. struct sk_buff *skb, *ctrl_skb;
  488. IRDA_DEBUG(2, "%s()\n", __func__ );
  489. if (!self || self->magic != IRCOMM_TTY_MAGIC)
  490. return;
  491. tty = tty_port_tty_get(&self->port);
  492. if (!tty)
  493. return;
  494. /* Unlink control buffer */
  495. spin_lock_irqsave(&self->spinlock, flags);
  496. ctrl_skb = self->ctrl_skb;
  497. self->ctrl_skb = NULL;
  498. spin_unlock_irqrestore(&self->spinlock, flags);
  499. /* Flush control buffer if any */
  500. if(ctrl_skb) {
  501. if(self->flow == FLOW_START)
  502. ircomm_control_request(self->ircomm, ctrl_skb);
  503. /* Drop reference count - see ircomm_ttp_data_request(). */
  504. dev_kfree_skb(ctrl_skb);
  505. }
  506. if (tty->hw_stopped)
  507. goto put;
  508. /* Unlink transmit buffer */
  509. spin_lock_irqsave(&self->spinlock, flags);
  510. skb = self->tx_skb;
  511. self->tx_skb = NULL;
  512. spin_unlock_irqrestore(&self->spinlock, flags);
  513. /* Flush transmit buffer if any */
  514. if (skb) {
  515. ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
  516. /* Drop reference count - see ircomm_ttp_data_request(). */
  517. dev_kfree_skb(skb);
  518. }
  519. /* Check if user (still) wants to be waken up */
  520. tty_wakeup(tty);
  521. put:
  522. tty_kref_put(tty);
  523. }
  524. /*
  525. * Function ircomm_tty_write (tty, buf, count)
  526. *
  527. * This routine is called by the kernel to write a series of characters
  528. * to the tty device. The characters may come from user space or kernel
  529. * space. This routine will return the number of characters actually
  530. * accepted for writing. This routine is mandatory.
  531. */
  532. static int ircomm_tty_write(struct tty_struct *tty,
  533. const unsigned char *buf, int count)
  534. {
  535. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  536. unsigned long flags;
  537. struct sk_buff *skb;
  538. int tailroom = 0;
  539. int len = 0;
  540. int size;
  541. IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
  542. tty->hw_stopped);
  543. IRDA_ASSERT(self != NULL, return -1;);
  544. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  545. /* We may receive packets from the TTY even before we have finished
  546. * our setup. Not cool.
  547. * The problem is that we don't know the final header and data size
  548. * to create the proper skb, so any skb we would create would have
  549. * bogus header and data size, so need care.
  550. * We use a bogus header size to safely detect this condition.
  551. * Another problem is that hw_stopped was set to 0 way before it
  552. * should be, so we would drop this skb. It should now be fixed.
  553. * One option is to not accept data until we are properly setup.
  554. * But, I suspect that when it happens, the ppp line discipline
  555. * just "drops" the data, which might screw up connect scripts.
  556. * The second option is to create a "safe skb", with large header
  557. * and small size (see ircomm_tty_open() for values).
  558. * We just need to make sure that when the real values get filled,
  559. * we don't mess up the original "safe skb" (see tx_data_size).
  560. * Jean II */
  561. if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
  562. IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
  563. #ifdef IRCOMM_NO_TX_BEFORE_INIT
  564. /* We didn't consume anything, TTY will retry */
  565. return 0;
  566. #endif
  567. }
  568. if (count < 1)
  569. return 0;
  570. /* Protect our manipulation of self->tx_skb and related */
  571. spin_lock_irqsave(&self->spinlock, flags);
  572. /* Fetch current transmit buffer */
  573. skb = self->tx_skb;
  574. /*
  575. * Send out all the data we get, possibly as multiple fragmented
  576. * frames, but this will only happen if the data is larger than the
  577. * max data size. The normal case however is just the opposite, and
  578. * this function may be called multiple times, and will then actually
  579. * defragment the data and send it out as one packet as soon as
  580. * possible, but at a safer point in time
  581. */
  582. while (count) {
  583. size = count;
  584. /* Adjust data size to the max data size */
  585. if (size > self->max_data_size)
  586. size = self->max_data_size;
  587. /*
  588. * Do we already have a buffer ready for transmit, or do
  589. * we need to allocate a new frame
  590. */
  591. if (skb) {
  592. /*
  593. * Any room for more data at the end of the current
  594. * transmit buffer? Cannot use skb_tailroom, since
  595. * dev_alloc_skb gives us a larger skb than we
  596. * requested
  597. * Note : use tx_data_size, because max_data_size
  598. * may have changed and we don't want to overwrite
  599. * the skb. - Jean II
  600. */
  601. if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
  602. /* Adjust data to tailroom */
  603. if (size > tailroom)
  604. size = tailroom;
  605. } else {
  606. /*
  607. * Current transmit frame is full, so break
  608. * out, so we can send it as soon as possible
  609. */
  610. break;
  611. }
  612. } else {
  613. /* Prepare a full sized frame */
  614. skb = alloc_skb(self->max_data_size+
  615. self->max_header_size,
  616. GFP_ATOMIC);
  617. if (!skb) {
  618. spin_unlock_irqrestore(&self->spinlock, flags);
  619. return -ENOBUFS;
  620. }
  621. skb_reserve(skb, self->max_header_size);
  622. self->tx_skb = skb;
  623. /* Remember skb size because max_data_size may
  624. * change later on - Jean II */
  625. self->tx_data_size = self->max_data_size;
  626. }
  627. /* Copy data */
  628. memcpy(skb_put(skb,size), buf + len, size);
  629. count -= size;
  630. len += size;
  631. }
  632. spin_unlock_irqrestore(&self->spinlock, flags);
  633. /*
  634. * Schedule a new thread which will transmit the frame as soon
  635. * as possible, but at a safe point in time. We do this so the
  636. * "user" can give us data multiple times, as PPP does (because of
  637. * its 256 byte tx buffer). We will then defragment and send out
  638. * all this data as one single packet.
  639. */
  640. schedule_work(&self->tqueue);
  641. return len;
  642. }
  643. /*
  644. * Function ircomm_tty_write_room (tty)
  645. *
  646. * This routine returns the numbers of characters the tty driver will
  647. * accept for queuing to be written. This number is subject to change as
  648. * output buffers get emptied, or if the output flow control is acted.
  649. */
  650. static int ircomm_tty_write_room(struct tty_struct *tty)
  651. {
  652. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  653. unsigned long flags;
  654. int ret;
  655. IRDA_ASSERT(self != NULL, return -1;);
  656. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  657. #ifdef IRCOMM_NO_TX_BEFORE_INIT
  658. /* max_header_size tells us if the channel is initialised or not. */
  659. if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
  660. /* Don't bother us yet */
  661. return 0;
  662. #endif
  663. /* Check if we are allowed to transmit any data.
  664. * hw_stopped is the regular flow control.
  665. * Jean II */
  666. if (tty->hw_stopped)
  667. ret = 0;
  668. else {
  669. spin_lock_irqsave(&self->spinlock, flags);
  670. if (self->tx_skb)
  671. ret = self->tx_data_size - self->tx_skb->len;
  672. else
  673. ret = self->max_data_size;
  674. spin_unlock_irqrestore(&self->spinlock, flags);
  675. }
  676. IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
  677. return ret;
  678. }
  679. /*
  680. * Function ircomm_tty_wait_until_sent (tty, timeout)
  681. *
  682. * This routine waits until the device has written out all of the
  683. * characters in its transmitter FIFO.
  684. */
  685. static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
  686. {
  687. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  688. unsigned long orig_jiffies, poll_time;
  689. unsigned long flags;
  690. IRDA_DEBUG(2, "%s()\n", __func__ );
  691. IRDA_ASSERT(self != NULL, return;);
  692. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  693. orig_jiffies = jiffies;
  694. /* Set poll time to 200 ms */
  695. poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
  696. spin_lock_irqsave(&self->spinlock, flags);
  697. while (self->tx_skb && self->tx_skb->len) {
  698. spin_unlock_irqrestore(&self->spinlock, flags);
  699. schedule_timeout_interruptible(poll_time);
  700. spin_lock_irqsave(&self->spinlock, flags);
  701. if (signal_pending(current))
  702. break;
  703. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  704. break;
  705. }
  706. spin_unlock_irqrestore(&self->spinlock, flags);
  707. current->state = TASK_RUNNING;
  708. }
  709. /*
  710. * Function ircomm_tty_throttle (tty)
  711. *
  712. * This routine notifies the tty driver that input buffers for the line
  713. * discipline are close to full, and it should somehow signal that no
  714. * more characters should be sent to the tty.
  715. */
  716. static void ircomm_tty_throttle(struct tty_struct *tty)
  717. {
  718. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  719. IRDA_DEBUG(2, "%s()\n", __func__ );
  720. IRDA_ASSERT(self != NULL, return;);
  721. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  722. /* Software flow control? */
  723. if (I_IXOFF(tty))
  724. ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
  725. /* Hardware flow control? */
  726. if (tty->termios.c_cflag & CRTSCTS) {
  727. self->settings.dte &= ~IRCOMM_RTS;
  728. self->settings.dte |= IRCOMM_DELTA_RTS;
  729. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  730. }
  731. ircomm_flow_request(self->ircomm, FLOW_STOP);
  732. }
  733. /*
  734. * Function ircomm_tty_unthrottle (tty)
  735. *
  736. * This routine notifies the tty drivers that it should signals that
  737. * characters can now be sent to the tty without fear of overrunning the
  738. * input buffers of the line disciplines.
  739. */
  740. static void ircomm_tty_unthrottle(struct tty_struct *tty)
  741. {
  742. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  743. IRDA_DEBUG(2, "%s()\n", __func__ );
  744. IRDA_ASSERT(self != NULL, return;);
  745. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  746. /* Using software flow control? */
  747. if (I_IXOFF(tty)) {
  748. ircomm_tty_send_xchar(tty, START_CHAR(tty));
  749. }
  750. /* Using hardware flow control? */
  751. if (tty->termios.c_cflag & CRTSCTS) {
  752. self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
  753. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  754. IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
  755. }
  756. ircomm_flow_request(self->ircomm, FLOW_START);
  757. }
  758. /*
  759. * Function ircomm_tty_chars_in_buffer (tty)
  760. *
  761. * Indicates if there are any data in the buffer
  762. *
  763. */
  764. static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
  765. {
  766. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  767. unsigned long flags;
  768. int len = 0;
  769. IRDA_ASSERT(self != NULL, return -1;);
  770. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  771. spin_lock_irqsave(&self->spinlock, flags);
  772. if (self->tx_skb)
  773. len = self->tx_skb->len;
  774. spin_unlock_irqrestore(&self->spinlock, flags);
  775. return len;
  776. }
  777. static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
  778. {
  779. unsigned long flags;
  780. IRDA_ASSERT(self != NULL, return;);
  781. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  782. IRDA_DEBUG(0, "%s()\n", __func__ );
  783. if (!test_and_clear_bit(ASYNCB_INITIALIZED, &self->port.flags))
  784. return;
  785. ircomm_tty_detach_cable(self);
  786. spin_lock_irqsave(&self->spinlock, flags);
  787. del_timer(&self->watchdog_timer);
  788. /* Free parameter buffer */
  789. if (self->ctrl_skb) {
  790. dev_kfree_skb(self->ctrl_skb);
  791. self->ctrl_skb = NULL;
  792. }
  793. /* Free transmit buffer */
  794. if (self->tx_skb) {
  795. dev_kfree_skb(self->tx_skb);
  796. self->tx_skb = NULL;
  797. }
  798. if (self->ircomm) {
  799. ircomm_close(self->ircomm);
  800. self->ircomm = NULL;
  801. }
  802. spin_unlock_irqrestore(&self->spinlock, flags);
  803. }
  804. /*
  805. * Function ircomm_tty_hangup (tty)
  806. *
  807. * This routine notifies the tty driver that it should hangup the tty
  808. * device.
  809. *
  810. */
  811. static void ircomm_tty_hangup(struct tty_struct *tty)
  812. {
  813. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  814. struct tty_port *port = &self->port;
  815. unsigned long flags;
  816. IRDA_DEBUG(0, "%s()\n", __func__ );
  817. IRDA_ASSERT(self != NULL, return;);
  818. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  819. /* ircomm_tty_flush_buffer(tty); */
  820. ircomm_tty_shutdown(self);
  821. spin_lock_irqsave(&port->lock, flags);
  822. port->flags &= ~ASYNC_NORMAL_ACTIVE;
  823. if (port->tty) {
  824. set_bit(TTY_IO_ERROR, &port->tty->flags);
  825. tty_kref_put(port->tty);
  826. }
  827. port->tty = NULL;
  828. port->count = 0;
  829. spin_unlock_irqrestore(&port->lock, flags);
  830. wake_up_interruptible(&port->open_wait);
  831. }
  832. /*
  833. * Function ircomm_tty_send_xchar (tty, ch)
  834. *
  835. * This routine is used to send a high-priority XON/XOFF character to
  836. * the device.
  837. */
  838. static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
  839. {
  840. IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
  841. }
  842. /*
  843. * Function ircomm_tty_start (tty)
  844. *
  845. * This routine notifies the tty driver that it resume sending
  846. * characters to the tty device.
  847. */
  848. void ircomm_tty_start(struct tty_struct *tty)
  849. {
  850. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  851. ircomm_flow_request(self->ircomm, FLOW_START);
  852. }
  853. /*
  854. * Function ircomm_tty_stop (tty)
  855. *
  856. * This routine notifies the tty driver that it should stop outputting
  857. * characters to the tty device.
  858. */
  859. static void ircomm_tty_stop(struct tty_struct *tty)
  860. {
  861. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  862. IRDA_ASSERT(self != NULL, return;);
  863. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  864. ircomm_flow_request(self->ircomm, FLOW_STOP);
  865. }
  866. /*
  867. * Function ircomm_check_modem_status (self)
  868. *
  869. * Check for any changes in the DCE's line settings. This function should
  870. * be called whenever the dce parameter settings changes, to update the
  871. * flow control settings and other things
  872. */
  873. void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
  874. {
  875. struct tty_struct *tty;
  876. int status;
  877. IRDA_DEBUG(0, "%s()\n", __func__ );
  878. IRDA_ASSERT(self != NULL, return;);
  879. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  880. tty = tty_port_tty_get(&self->port);
  881. status = self->settings.dce;
  882. if (status & IRCOMM_DCE_DELTA_ANY) {
  883. /*wake_up_interruptible(&self->delta_msr_wait);*/
  884. }
  885. if ((self->port.flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
  886. IRDA_DEBUG(2,
  887. "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
  888. (status & IRCOMM_CD) ? "on" : "off");
  889. if (status & IRCOMM_CD) {
  890. wake_up_interruptible(&self->port.open_wait);
  891. } else {
  892. IRDA_DEBUG(2,
  893. "%s(), Doing serial hangup..\n", __func__ );
  894. if (tty)
  895. tty_hangup(tty);
  896. /* Hangup will remote the tty, so better break out */
  897. goto put;
  898. }
  899. }
  900. if (tty && tty_port_cts_enabled(&self->port)) {
  901. if (tty->hw_stopped) {
  902. if (status & IRCOMM_CTS) {
  903. IRDA_DEBUG(2,
  904. "%s(), CTS tx start...\n", __func__ );
  905. tty->hw_stopped = 0;
  906. /* Wake up processes blocked on open */
  907. wake_up_interruptible(&self->port.open_wait);
  908. schedule_work(&self->tqueue);
  909. goto put;
  910. }
  911. } else {
  912. if (!(status & IRCOMM_CTS)) {
  913. IRDA_DEBUG(2,
  914. "%s(), CTS tx stop...\n", __func__ );
  915. tty->hw_stopped = 1;
  916. }
  917. }
  918. }
  919. put:
  920. tty_kref_put(tty);
  921. }
  922. /*
  923. * Function ircomm_tty_data_indication (instance, sap, skb)
  924. *
  925. * Handle incoming data, and deliver it to the line discipline
  926. *
  927. */
  928. static int ircomm_tty_data_indication(void *instance, void *sap,
  929. struct sk_buff *skb)
  930. {
  931. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
  932. struct tty_struct *tty;
  933. IRDA_DEBUG(2, "%s()\n", __func__ );
  934. IRDA_ASSERT(self != NULL, return -1;);
  935. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  936. IRDA_ASSERT(skb != NULL, return -1;);
  937. tty = tty_port_tty_get(&self->port);
  938. if (!tty) {
  939. IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
  940. return 0;
  941. }
  942. /*
  943. * If we receive data when hardware is stopped then something is wrong.
  944. * We try to poll the peers line settings to check if we are up todate.
  945. * Devices like WinCE can do this, and since they don't send any
  946. * params, we can just as well declare the hardware for running.
  947. */
  948. if (tty->hw_stopped && (self->flow == FLOW_START)) {
  949. IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
  950. ircomm_param_request(self, IRCOMM_POLL, TRUE);
  951. /* We can just as well declare the hardware for running */
  952. ircomm_tty_send_initial_parameters(self);
  953. ircomm_tty_link_established(self);
  954. }
  955. /*
  956. * Use flip buffer functions since the code may be called from interrupt
  957. * context
  958. */
  959. tty_insert_flip_string(tty, skb->data, skb->len);
  960. tty_flip_buffer_push(tty);
  961. tty_kref_put(tty);
  962. /* No need to kfree_skb - see ircomm_ttp_data_indication() */
  963. return 0;
  964. }
  965. /*
  966. * Function ircomm_tty_control_indication (instance, sap, skb)
  967. *
  968. * Parse all incoming parameters (easy!)
  969. *
  970. */
  971. static int ircomm_tty_control_indication(void *instance, void *sap,
  972. struct sk_buff *skb)
  973. {
  974. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
  975. int clen;
  976. IRDA_DEBUG(4, "%s()\n", __func__ );
  977. IRDA_ASSERT(self != NULL, return -1;);
  978. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  979. IRDA_ASSERT(skb != NULL, return -1;);
  980. clen = skb->data[0];
  981. irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
  982. &ircomm_param_info);
  983. /* No need to kfree_skb - see ircomm_control_indication() */
  984. return 0;
  985. }
  986. /*
  987. * Function ircomm_tty_flow_indication (instance, sap, cmd)
  988. *
  989. * This function is called by IrTTP when it wants us to slow down the
  990. * transmission of data. We just mark the hardware as stopped, and wait
  991. * for IrTTP to notify us that things are OK again.
  992. */
  993. static void ircomm_tty_flow_indication(void *instance, void *sap,
  994. LOCAL_FLOW cmd)
  995. {
  996. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
  997. struct tty_struct *tty;
  998. IRDA_ASSERT(self != NULL, return;);
  999. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
  1000. tty = tty_port_tty_get(&self->port);
  1001. switch (cmd) {
  1002. case FLOW_START:
  1003. IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
  1004. if (tty)
  1005. tty->hw_stopped = 0;
  1006. /* ircomm_tty_do_softint will take care of the rest */
  1007. schedule_work(&self->tqueue);
  1008. break;
  1009. default: /* If we get here, something is very wrong, better stop */
  1010. case FLOW_STOP:
  1011. IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
  1012. if (tty)
  1013. tty->hw_stopped = 1;
  1014. break;
  1015. }
  1016. tty_kref_put(tty);
  1017. self->flow = cmd;
  1018. }
  1019. #ifdef CONFIG_PROC_FS
  1020. static void ircomm_tty_line_info(struct ircomm_tty_cb *self, struct seq_file *m)
  1021. {
  1022. struct tty_struct *tty;
  1023. char sep;
  1024. seq_printf(m, "State: %s\n", ircomm_tty_state[self->state]);
  1025. seq_puts(m, "Service type: ");
  1026. if (self->service_type & IRCOMM_9_WIRE)
  1027. seq_puts(m, "9_WIRE");
  1028. else if (self->service_type & IRCOMM_3_WIRE)
  1029. seq_puts(m, "3_WIRE");
  1030. else if (self->service_type & IRCOMM_3_WIRE_RAW)
  1031. seq_puts(m, "3_WIRE_RAW");
  1032. else
  1033. seq_puts(m, "No common service type!\n");
  1034. seq_putc(m, '\n');
  1035. seq_printf(m, "Port name: %s\n", self->settings.port_name);
  1036. seq_printf(m, "DTE status:");
  1037. sep = ' ';
  1038. if (self->settings.dte & IRCOMM_RTS) {
  1039. seq_printf(m, "%cRTS", sep);
  1040. sep = '|';
  1041. }
  1042. if (self->settings.dte & IRCOMM_DTR) {
  1043. seq_printf(m, "%cDTR", sep);
  1044. sep = '|';
  1045. }
  1046. seq_putc(m, '\n');
  1047. seq_puts(m, "DCE status:");
  1048. sep = ' ';
  1049. if (self->settings.dce & IRCOMM_CTS) {
  1050. seq_printf(m, "%cCTS", sep);
  1051. sep = '|';
  1052. }
  1053. if (self->settings.dce & IRCOMM_DSR) {
  1054. seq_printf(m, "%cDSR", sep);
  1055. sep = '|';
  1056. }
  1057. if (self->settings.dce & IRCOMM_CD) {
  1058. seq_printf(m, "%cCD", sep);
  1059. sep = '|';
  1060. }
  1061. if (self->settings.dce & IRCOMM_RI) {
  1062. seq_printf(m, "%cRI", sep);
  1063. sep = '|';
  1064. }
  1065. seq_putc(m, '\n');
  1066. seq_puts(m, "Configuration: ");
  1067. if (!self->settings.null_modem)
  1068. seq_puts(m, "DTE <-> DCE\n");
  1069. else
  1070. seq_puts(m, "DTE <-> DTE (null modem emulation)\n");
  1071. seq_printf(m, "Data rate: %d\n", self->settings.data_rate);
  1072. seq_puts(m, "Flow control:");
  1073. sep = ' ';
  1074. if (self->settings.flow_control & IRCOMM_XON_XOFF_IN) {
  1075. seq_printf(m, "%cXON_XOFF_IN", sep);
  1076. sep = '|';
  1077. }
  1078. if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT) {
  1079. seq_printf(m, "%cXON_XOFF_OUT", sep);
  1080. sep = '|';
  1081. }
  1082. if (self->settings.flow_control & IRCOMM_RTS_CTS_IN) {
  1083. seq_printf(m, "%cRTS_CTS_IN", sep);
  1084. sep = '|';
  1085. }
  1086. if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT) {
  1087. seq_printf(m, "%cRTS_CTS_OUT", sep);
  1088. sep = '|';
  1089. }
  1090. if (self->settings.flow_control & IRCOMM_DSR_DTR_IN) {
  1091. seq_printf(m, "%cDSR_DTR_IN", sep);
  1092. sep = '|';
  1093. }
  1094. if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT) {
  1095. seq_printf(m, "%cDSR_DTR_OUT", sep);
  1096. sep = '|';
  1097. }
  1098. if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN) {
  1099. seq_printf(m, "%cENQ_ACK_IN", sep);
  1100. sep = '|';
  1101. }
  1102. if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT) {
  1103. seq_printf(m, "%cENQ_ACK_OUT", sep);
  1104. sep = '|';
  1105. }
  1106. seq_putc(m, '\n');
  1107. seq_puts(m, "Flags:");
  1108. sep = ' ';
  1109. if (tty_port_cts_enabled(&self->port)) {
  1110. seq_printf(m, "%cASYNC_CTS_FLOW", sep);
  1111. sep = '|';
  1112. }
  1113. if (self->port.flags & ASYNC_CHECK_CD) {
  1114. seq_printf(m, "%cASYNC_CHECK_CD", sep);
  1115. sep = '|';
  1116. }
  1117. if (self->port.flags & ASYNC_INITIALIZED) {
  1118. seq_printf(m, "%cASYNC_INITIALIZED", sep);
  1119. sep = '|';
  1120. }
  1121. if (self->port.flags & ASYNC_LOW_LATENCY) {
  1122. seq_printf(m, "%cASYNC_LOW_LATENCY", sep);
  1123. sep = '|';
  1124. }
  1125. if (self->port.flags & ASYNC_CLOSING) {
  1126. seq_printf(m, "%cASYNC_CLOSING", sep);
  1127. sep = '|';
  1128. }
  1129. if (self->port.flags & ASYNC_NORMAL_ACTIVE) {
  1130. seq_printf(m, "%cASYNC_NORMAL_ACTIVE", sep);
  1131. sep = '|';
  1132. }
  1133. seq_putc(m, '\n');
  1134. seq_printf(m, "Role: %s\n", self->client ? "client" : "server");
  1135. seq_printf(m, "Open count: %d\n", self->port.count);
  1136. seq_printf(m, "Max data size: %d\n", self->max_data_size);
  1137. seq_printf(m, "Max header size: %d\n", self->max_header_size);
  1138. tty = tty_port_tty_get(&self->port);
  1139. if (tty) {
  1140. seq_printf(m, "Hardware: %s\n",
  1141. tty->hw_stopped ? "Stopped" : "Running");
  1142. tty_kref_put(tty);
  1143. }
  1144. }
  1145. static int ircomm_tty_proc_show(struct seq_file *m, void *v)
  1146. {
  1147. struct ircomm_tty_cb *self;
  1148. unsigned long flags;
  1149. spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
  1150. self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
  1151. while (self != NULL) {
  1152. if (self->magic != IRCOMM_TTY_MAGIC)
  1153. break;
  1154. ircomm_tty_line_info(self, m);
  1155. self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
  1156. }
  1157. spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
  1158. return 0;
  1159. }
  1160. static int ircomm_tty_proc_open(struct inode *inode, struct file *file)
  1161. {
  1162. return single_open(file, ircomm_tty_proc_show, NULL);
  1163. }
  1164. static const struct file_operations ircomm_tty_proc_fops = {
  1165. .owner = THIS_MODULE,
  1166. .open = ircomm_tty_proc_open,
  1167. .read = seq_read,
  1168. .llseek = seq_lseek,
  1169. .release = single_release,
  1170. };
  1171. #endif /* CONFIG_PROC_FS */
  1172. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  1173. MODULE_DESCRIPTION("IrCOMM serial TTY driver");
  1174. MODULE_LICENSE("GPL");
  1175. MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
  1176. module_init(ircomm_tty_init);
  1177. module_exit(ircomm_tty_cleanup);