ircomm_tty.c 38 KB

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