ircomm_tty.c 38 KB

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