ircomm_tty.c 39 KB

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