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