ircomm_tty.c 39 KB

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