ircomm_tty.c 38 KB

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