ircomm_tty.c 38 KB

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