ircomm_tty.c 38 KB

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