ircomm_tty.c 38 KB

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