ircomm_tty.c 40 KB

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