generic_serial.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * generic_serial.c
  3. *
  4. * Copyright (C) 1998/1999 R.E.Wolff@BitWizard.nl
  5. *
  6. * written for the SX serial driver.
  7. * Contains the code that should be shared over all the serial drivers.
  8. *
  9. * Credit for the idea to do it this way might go to Alan Cox.
  10. *
  11. *
  12. * Version 0.1 -- December, 1998. Initial version.
  13. * Version 0.2 -- March, 1999. Some more routines. Bugfixes. Etc.
  14. * Version 0.5 -- August, 1999. Some more fixes. Reformat for Linus.
  15. *
  16. * BitWizard is actively maintaining this file. We sometimes find
  17. * that someone submitted changes to this file. We really appreciate
  18. * your help, but please submit changes through us. We're doing our
  19. * best to be responsive. -- REW
  20. * */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/tty.h>
  24. #include <linux/serial.h>
  25. #include <linux/mm.h>
  26. #include <linux/generic_serial.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/tty_flip.h>
  29. #include <linux/delay.h>
  30. #include <asm/semaphore.h>
  31. #include <asm/uaccess.h>
  32. #define DEBUG
  33. static char * tmp_buf;
  34. static DECLARE_MUTEX(tmp_buf_sem);
  35. static int gs_debug;
  36. #ifdef DEBUG
  37. #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
  38. #else
  39. #define gs_dprintk(f, str...) /* nothing */
  40. #endif
  41. #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __FUNCTION__)
  42. #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit %s\n", __FUNCTION__)
  43. #define NEW_WRITE_LOCKING 1
  44. #if NEW_WRITE_LOCKING
  45. #define DECL /* Nothing */
  46. #define LOCKIT down (& port->port_write_sem);
  47. #define RELEASEIT up (&port->port_write_sem);
  48. #else
  49. #define DECL unsigned long flags;
  50. #define LOCKIT save_flags (flags);cli ()
  51. #define RELEASEIT restore_flags (flags)
  52. #endif
  53. #define RS_EVENT_WRITE_WAKEUP 1
  54. module_param(gs_debug, int, 0644);
  55. void gs_put_char(struct tty_struct * tty, unsigned char ch)
  56. {
  57. struct gs_port *port;
  58. DECL
  59. func_enter ();
  60. if (!tty) return;
  61. port = tty->driver_data;
  62. if (!port) return;
  63. if (! (port->flags & ASYNC_INITIALIZED)) return;
  64. /* Take a lock on the serial tranmit buffer! */
  65. LOCKIT;
  66. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
  67. /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
  68. RELEASEIT;
  69. return;
  70. }
  71. port->xmit_buf[port->xmit_head++] = ch;
  72. port->xmit_head &= SERIAL_XMIT_SIZE - 1;
  73. port->xmit_cnt++; /* Characters in buffer */
  74. RELEASEIT;
  75. func_exit ();
  76. }
  77. #ifdef NEW_WRITE_LOCKING
  78. /*
  79. > Problems to take into account are:
  80. > -1- Interrupts that empty part of the buffer.
  81. > -2- page faults on the access to userspace.
  82. > -3- Other processes that are also trying to do a "write".
  83. */
  84. int gs_write(struct tty_struct * tty,
  85. const unsigned char *buf, int count)
  86. {
  87. struct gs_port *port;
  88. int c, total = 0;
  89. int t;
  90. func_enter ();
  91. if (!tty) return 0;
  92. port = tty->driver_data;
  93. if (!port) return 0;
  94. if (! (port->flags & ASYNC_INITIALIZED))
  95. return 0;
  96. /* get exclusive "write" access to this port (problem 3) */
  97. /* This is not a spinlock because we can have a disk access (page
  98. fault) in copy_from_user */
  99. down (& port->port_write_sem);
  100. while (1) {
  101. c = count;
  102. /* This is safe because we "OWN" the "head". Noone else can
  103. change the "head": we own the port_write_sem. */
  104. /* Don't overrun the end of the buffer */
  105. t = SERIAL_XMIT_SIZE - port->xmit_head;
  106. if (t < c) c = t;
  107. /* This is safe because the xmit_cnt can only decrease. This
  108. would increase "t", so we might copy too little chars. */
  109. /* Don't copy past the "head" of the buffer */
  110. t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
  111. if (t < c) c = t;
  112. /* Can't copy more? break out! */
  113. if (c <= 0) break;
  114. memcpy (port->xmit_buf + port->xmit_head, buf, c);
  115. port -> xmit_cnt += c;
  116. port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
  117. buf += c;
  118. count -= c;
  119. total += c;
  120. }
  121. up (& port->port_write_sem);
  122. gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
  123. (port->flags & GS_TX_INTEN)?"enabled": "disabled");
  124. if (port->xmit_cnt &&
  125. !tty->stopped &&
  126. !tty->hw_stopped &&
  127. !(port->flags & GS_TX_INTEN)) {
  128. port->flags |= GS_TX_INTEN;
  129. port->rd->enable_tx_interrupts (port);
  130. }
  131. func_exit ();
  132. return total;
  133. }
  134. #else
  135. /*
  136. > Problems to take into account are:
  137. > -1- Interrupts that empty part of the buffer.
  138. > -2- page faults on the access to userspace.
  139. > -3- Other processes that are also trying to do a "write".
  140. */
  141. int gs_write(struct tty_struct * tty,
  142. const unsigned char *buf, int count)
  143. {
  144. struct gs_port *port;
  145. int c, total = 0;
  146. int t;
  147. unsigned long flags;
  148. func_enter ();
  149. /* The standard serial driver returns 0 in this case.
  150. That sounds to me as "No error, I just didn't get to writing any
  151. bytes. Feel free to try again."
  152. The "official" way to write n bytes from buf is:
  153. for (nwritten = 0;nwritten < n;nwritten += rv) {
  154. rv = write (fd, buf+nwritten, n-nwritten);
  155. if (rv < 0) break; // Error: bail out. //
  156. }
  157. which will loop endlessly in this case. The manual page for write
  158. agrees with me. In practise almost everybody writes
  159. "write (fd, buf,n);" but some people might have had to deal with
  160. incomplete writes in the past and correctly implemented it by now...
  161. */
  162. if (!tty) return -EIO;
  163. port = tty->driver_data;
  164. if (!port || !port->xmit_buf || !tmp_buf)
  165. return -EIO;
  166. local_save_flags(flags);
  167. while (1) {
  168. cli();
  169. c = count;
  170. /* This is safe because we "OWN" the "head". Noone else can
  171. change the "head": we own the port_write_sem. */
  172. /* Don't overrun the end of the buffer */
  173. t = SERIAL_XMIT_SIZE - port->xmit_head;
  174. if (t < c) c = t;
  175. /* This is safe because the xmit_cnt can only decrease. This
  176. would increase "t", so we might copy too little chars. */
  177. /* Don't copy past the "head" of the buffer */
  178. t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
  179. if (t < c) c = t;
  180. /* Can't copy more? break out! */
  181. if (c <= 0) {
  182. local_restore_flags(flags);
  183. break;
  184. }
  185. memcpy(port->xmit_buf + port->xmit_head, buf, c);
  186. port->xmit_head = ((port->xmit_head + c) &
  187. (SERIAL_XMIT_SIZE-1));
  188. port->xmit_cnt += c;
  189. local_restore_flags(flags);
  190. buf += c;
  191. count -= c;
  192. total += c;
  193. }
  194. if (port->xmit_cnt &&
  195. !tty->stopped &&
  196. !tty->hw_stopped &&
  197. !(port->flags & GS_TX_INTEN)) {
  198. port->flags |= GS_TX_INTEN;
  199. port->rd->enable_tx_interrupts (port);
  200. }
  201. func_exit ();
  202. return total;
  203. }
  204. #endif
  205. int gs_write_room(struct tty_struct * tty)
  206. {
  207. struct gs_port *port = tty->driver_data;
  208. int ret;
  209. func_enter ();
  210. ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  211. if (ret < 0)
  212. ret = 0;
  213. func_exit ();
  214. return ret;
  215. }
  216. int gs_chars_in_buffer(struct tty_struct *tty)
  217. {
  218. struct gs_port *port = tty->driver_data;
  219. func_enter ();
  220. func_exit ();
  221. return port->xmit_cnt;
  222. }
  223. static int gs_real_chars_in_buffer(struct tty_struct *tty)
  224. {
  225. struct gs_port *port;
  226. func_enter ();
  227. if (!tty) return 0;
  228. port = tty->driver_data;
  229. if (!port->rd) return 0;
  230. if (!port->rd->chars_in_buffer) return 0;
  231. func_exit ();
  232. return port->xmit_cnt + port->rd->chars_in_buffer (port);
  233. }
  234. static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
  235. {
  236. struct gs_port *port = ptr;
  237. unsigned long end_jiffies;
  238. int jiffies_to_transmit, charsleft = 0, rv = 0;
  239. int rcib;
  240. func_enter();
  241. gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
  242. if (port) {
  243. gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
  244. port->xmit_cnt, port->xmit_buf, port->tty);
  245. }
  246. if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
  247. gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
  248. func_exit();
  249. return -EINVAL; /* This is an error which we don't know how to handle. */
  250. }
  251. rcib = gs_real_chars_in_buffer(port->tty);
  252. if(rcib <= 0) {
  253. gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
  254. func_exit();
  255. return rv;
  256. }
  257. /* stop trying: now + twice the time it would normally take + seconds */
  258. if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
  259. end_jiffies = jiffies;
  260. if (timeout != MAX_SCHEDULE_TIMEOUT)
  261. end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
  262. end_jiffies += timeout;
  263. gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
  264. jiffies, end_jiffies, end_jiffies-jiffies);
  265. /* the expression is actually jiffies < end_jiffies, but that won't
  266. work around the wraparound. Tricky eh? */
  267. while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
  268. time_after (end_jiffies, jiffies)) {
  269. /* Units check:
  270. chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
  271. check! */
  272. charsleft += 16; /* Allow 16 chars more to be transmitted ... */
  273. jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
  274. /* ^^^ Round up.... */
  275. if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
  276. gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
  277. "(%d chars).\n", jiffies_to_transmit, charsleft);
  278. msleep_interruptible(jiffies_to_msecs(jiffies_to_transmit));
  279. if (signal_pending (current)) {
  280. gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
  281. rv = -EINTR;
  282. break;
  283. }
  284. }
  285. gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
  286. set_current_state (TASK_RUNNING);
  287. func_exit();
  288. return rv;
  289. }
  290. void gs_flush_buffer(struct tty_struct *tty)
  291. {
  292. struct gs_port *port;
  293. unsigned long flags;
  294. func_enter ();
  295. if (!tty) return;
  296. port = tty->driver_data;
  297. if (!port) return;
  298. /* XXX Would the write semaphore do? */
  299. spin_lock_irqsave (&port->driver_lock, flags);
  300. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  301. spin_unlock_irqrestore (&port->driver_lock, flags);
  302. wake_up_interruptible(&tty->write_wait);
  303. tty_wakeup(tty);
  304. func_exit ();
  305. }
  306. void gs_flush_chars(struct tty_struct * tty)
  307. {
  308. struct gs_port *port;
  309. func_enter ();
  310. if (!tty) return;
  311. port = tty->driver_data;
  312. if (!port) return;
  313. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  314. !port->xmit_buf) {
  315. func_exit ();
  316. return;
  317. }
  318. /* Beats me -- REW */
  319. port->flags |= GS_TX_INTEN;
  320. port->rd->enable_tx_interrupts (port);
  321. func_exit ();
  322. }
  323. void gs_stop(struct tty_struct * tty)
  324. {
  325. struct gs_port *port;
  326. func_enter ();
  327. if (!tty) return;
  328. port = tty->driver_data;
  329. if (!port) return;
  330. if (port->xmit_cnt &&
  331. port->xmit_buf &&
  332. (port->flags & GS_TX_INTEN) ) {
  333. port->flags &= ~GS_TX_INTEN;
  334. port->rd->disable_tx_interrupts (port);
  335. }
  336. func_exit ();
  337. }
  338. void gs_start(struct tty_struct * tty)
  339. {
  340. struct gs_port *port;
  341. if (!tty) return;
  342. port = tty->driver_data;
  343. if (!port) return;
  344. if (port->xmit_cnt &&
  345. port->xmit_buf &&
  346. !(port->flags & GS_TX_INTEN) ) {
  347. port->flags |= GS_TX_INTEN;
  348. port->rd->enable_tx_interrupts (port);
  349. }
  350. func_exit ();
  351. }
  352. static void gs_shutdown_port (struct gs_port *port)
  353. {
  354. unsigned long flags;
  355. func_enter();
  356. if (!port) return;
  357. if (!(port->flags & ASYNC_INITIALIZED))
  358. return;
  359. spin_lock_irqsave(&port->driver_lock, flags);
  360. if (port->xmit_buf) {
  361. free_page((unsigned long) port->xmit_buf);
  362. port->xmit_buf = NULL;
  363. }
  364. if (port->tty)
  365. set_bit(TTY_IO_ERROR, &port->tty->flags);
  366. port->rd->shutdown_port (port);
  367. port->flags &= ~ASYNC_INITIALIZED;
  368. spin_unlock_irqrestore(&port->driver_lock, flags);
  369. func_exit();
  370. }
  371. void gs_hangup(struct tty_struct *tty)
  372. {
  373. struct gs_port *port;
  374. func_enter ();
  375. if (!tty) return;
  376. port = tty->driver_data;
  377. tty = port->tty;
  378. if (!tty)
  379. return;
  380. gs_shutdown_port (port);
  381. port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
  382. port->tty = NULL;
  383. port->count = 0;
  384. wake_up_interruptible(&port->open_wait);
  385. func_exit ();
  386. }
  387. int gs_block_til_ready(void *port_, struct file * filp)
  388. {
  389. struct gs_port *port = port_;
  390. DECLARE_WAITQUEUE(wait, current);
  391. int retval;
  392. int do_clocal = 0;
  393. int CD;
  394. struct tty_struct *tty;
  395. unsigned long flags;
  396. func_enter ();
  397. if (!port) return 0;
  398. tty = port->tty;
  399. if (!tty) return 0;
  400. gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
  401. /*
  402. * If the device is in the middle of being closed, then block
  403. * until it's done, and then try again.
  404. */
  405. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  406. interruptible_sleep_on(&port->close_wait);
  407. if (port->flags & ASYNC_HUP_NOTIFY)
  408. return -EAGAIN;
  409. else
  410. return -ERESTARTSYS;
  411. }
  412. gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
  413. /*
  414. * If non-blocking mode is set, or the port is not enabled,
  415. * then make the check up front and then exit.
  416. */
  417. if ((filp->f_flags & O_NONBLOCK) ||
  418. (tty->flags & (1 << TTY_IO_ERROR))) {
  419. port->flags |= ASYNC_NORMAL_ACTIVE;
  420. return 0;
  421. }
  422. gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
  423. if (C_CLOCAL(tty))
  424. do_clocal = 1;
  425. /*
  426. * Block waiting for the carrier detect and the line to become
  427. * free (i.e., not in use by the callout). While we are in
  428. * this loop, port->count is dropped by one, so that
  429. * rs_close() knows when to free things. We restore it upon
  430. * exit, either normal or abnormal.
  431. */
  432. retval = 0;
  433. add_wait_queue(&port->open_wait, &wait);
  434. gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
  435. spin_lock_irqsave(&port->driver_lock, flags);
  436. if (!tty_hung_up_p(filp)) {
  437. port->count--;
  438. }
  439. spin_unlock_irqrestore(&port->driver_lock, flags);
  440. port->blocked_open++;
  441. while (1) {
  442. CD = port->rd->get_CD (port);
  443. gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
  444. set_current_state (TASK_INTERRUPTIBLE);
  445. if (tty_hung_up_p(filp) ||
  446. !(port->flags & ASYNC_INITIALIZED)) {
  447. if (port->flags & ASYNC_HUP_NOTIFY)
  448. retval = -EAGAIN;
  449. else
  450. retval = -ERESTARTSYS;
  451. break;
  452. }
  453. if (!(port->flags & ASYNC_CLOSING) &&
  454. (do_clocal || CD))
  455. break;
  456. gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
  457. (int)signal_pending (current), *(long*)(&current->blocked));
  458. if (signal_pending(current)) {
  459. retval = -ERESTARTSYS;
  460. break;
  461. }
  462. schedule();
  463. }
  464. gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
  465. port->blocked_open);
  466. set_current_state (TASK_RUNNING);
  467. remove_wait_queue(&port->open_wait, &wait);
  468. if (!tty_hung_up_p(filp)) {
  469. port->count++;
  470. }
  471. port->blocked_open--;
  472. if (retval)
  473. return retval;
  474. port->flags |= ASYNC_NORMAL_ACTIVE;
  475. func_exit ();
  476. return 0;
  477. }
  478. void gs_close(struct tty_struct * tty, struct file * filp)
  479. {
  480. unsigned long flags;
  481. struct gs_port *port;
  482. func_enter ();
  483. if (!tty) return;
  484. port = (struct gs_port *) tty->driver_data;
  485. if (!port) return;
  486. if (!port->tty) {
  487. /* This seems to happen when this is called from vhangup. */
  488. gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
  489. port->tty = tty;
  490. }
  491. spin_lock_irqsave(&port->driver_lock, flags);
  492. if (tty_hung_up_p(filp)) {
  493. spin_unlock_irqrestore(&port->driver_lock, flags);
  494. if (port->rd->hungup)
  495. port->rd->hungup (port);
  496. func_exit ();
  497. return;
  498. }
  499. if ((tty->count == 1) && (port->count != 1)) {
  500. printk(KERN_ERR "gs: gs_close port %p: bad port count;"
  501. " tty->count is 1, port count is %d\n", port, port->count);
  502. port->count = 1;
  503. }
  504. if (--port->count < 0) {
  505. printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->count);
  506. port->count = 0;
  507. }
  508. if (port->count) {
  509. gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->count);
  510. spin_unlock_irqrestore(&port->driver_lock, flags);
  511. func_exit ();
  512. return;
  513. }
  514. port->flags |= ASYNC_CLOSING;
  515. /*
  516. * Now we wait for the transmit buffer to clear; and we notify
  517. * the line discipline to only process XON/XOFF characters.
  518. */
  519. tty->closing = 1;
  520. /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  521. tty_wait_until_sent(tty, port->closing_wait); */
  522. /*
  523. * At this point we stop accepting input. To do this, we
  524. * disable the receive line status interrupts, and tell the
  525. * interrupt driver to stop checking the data ready bit in the
  526. * line status register.
  527. */
  528. port->rd->disable_rx_interrupts (port);
  529. spin_unlock_irqrestore(&port->driver_lock, flags);
  530. /* close has no way of returning "EINTR", so discard return value */
  531. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  532. gs_wait_tx_flushed (port, port->closing_wait);
  533. port->flags &= ~GS_ACTIVE;
  534. if (tty->driver->flush_buffer)
  535. tty->driver->flush_buffer(tty);
  536. tty_ldisc_flush(tty);
  537. tty->closing = 0;
  538. port->event = 0;
  539. port->rd->close (port);
  540. port->rd->shutdown_port (port);
  541. port->tty = NULL;
  542. if (port->blocked_open) {
  543. if (port->close_delay) {
  544. spin_unlock_irqrestore(&port->driver_lock, flags);
  545. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  546. spin_lock_irqsave(&port->driver_lock, flags);
  547. }
  548. wake_up_interruptible(&port->open_wait);
  549. }
  550. port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
  551. wake_up_interruptible(&port->close_wait);
  552. func_exit ();
  553. }
  554. static unsigned int gs_baudrates[] = {
  555. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  556. 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
  557. };
  558. void gs_set_termios (struct tty_struct * tty,
  559. struct termios * old_termios)
  560. {
  561. struct gs_port *port;
  562. int baudrate, tmp, rv;
  563. struct termios *tiosp;
  564. func_enter();
  565. if (!tty) return;
  566. port = tty->driver_data;
  567. if (!port) return;
  568. if (!port->tty) {
  569. /* This seems to happen when this is called after gs_close. */
  570. gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->tty is NULL\n");
  571. port->tty = tty;
  572. }
  573. tiosp = tty->termios;
  574. if (gs_debug & GS_DEBUG_TERMIOS) {
  575. gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
  576. }
  577. #if 0
  578. /* This is an optimization that is only allowed for dumb cards */
  579. /* Smart cards require knowledge of iflags and oflags too: that
  580. might change hardware cooking mode.... */
  581. #endif
  582. if (old_termios) {
  583. if( (tiosp->c_iflag == old_termios->c_iflag)
  584. && (tiosp->c_oflag == old_termios->c_oflag)
  585. && (tiosp->c_cflag == old_termios->c_cflag)
  586. && (tiosp->c_lflag == old_termios->c_lflag)
  587. && (tiosp->c_line == old_termios->c_line)
  588. && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
  589. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
  590. return /* 0 */;
  591. }
  592. } else
  593. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
  594. "no optimization\n");
  595. if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
  596. if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
  597. if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
  598. if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
  599. if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
  600. if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
  601. if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
  602. }
  603. baudrate = tiosp->c_cflag & CBAUD;
  604. if (baudrate & CBAUDEX) {
  605. baudrate &= ~CBAUDEX;
  606. if ((baudrate < 1) || (baudrate > 4))
  607. tiosp->c_cflag &= ~CBAUDEX;
  608. else
  609. baudrate += 15;
  610. }
  611. baudrate = gs_baudrates[baudrate];
  612. if ((tiosp->c_cflag & CBAUD) == B38400) {
  613. if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  614. baudrate = 57600;
  615. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  616. baudrate = 115200;
  617. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  618. baudrate = 230400;
  619. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  620. baudrate = 460800;
  621. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  622. baudrate = (port->baud_base / port->custom_divisor);
  623. }
  624. /* I recommend using THIS instead of the mess in termios (and
  625. duplicating the above code). Next we should create a clean
  626. interface towards this variable. If your card supports arbitrary
  627. baud rates, (e.g. CD1400 or 16550 based cards) then everything
  628. will be very easy..... */
  629. port->baud = baudrate;
  630. /* Two timer ticks seems enough to wakeup something like SLIP driver */
  631. /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
  632. tmp = (baudrate / 10 / HZ) * 2;
  633. if (tmp < 0) tmp = 0;
  634. if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
  635. port->wakeup_chars = tmp;
  636. /* We should really wait for the characters to be all sent before
  637. changing the settings. -- CAL */
  638. rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
  639. if (rv < 0) return /* rv */;
  640. rv = port->rd->set_real_termios(port);
  641. if (rv < 0) return /* rv */;
  642. if ((!old_termios ||
  643. (old_termios->c_cflag & CRTSCTS)) &&
  644. !( tiosp->c_cflag & CRTSCTS)) {
  645. tty->stopped = 0;
  646. gs_start(tty);
  647. }
  648. #ifdef tytso_patch_94Nov25_1726
  649. /* This "makes sense", Why is it commented out? */
  650. if (!(old_termios->c_cflag & CLOCAL) &&
  651. (tty->termios->c_cflag & CLOCAL))
  652. wake_up_interruptible(&port->gs.open_wait);
  653. #endif
  654. func_exit();
  655. return /* 0 */;
  656. }
  657. /* Must be called with interrupts enabled */
  658. int gs_init_port(struct gs_port *port)
  659. {
  660. unsigned long flags;
  661. unsigned long page;
  662. func_enter ();
  663. if (!tmp_buf) {
  664. page = get_zeroed_page(GFP_KERNEL);
  665. spin_lock_irqsave (&port->driver_lock, flags); /* Don't expect this to make a difference. */
  666. if (tmp_buf)
  667. free_page(page);
  668. else
  669. tmp_buf = (unsigned char *) page;
  670. spin_unlock_irqrestore (&port->driver_lock, flags);
  671. if (!tmp_buf) {
  672. func_exit ();
  673. return -ENOMEM;
  674. }
  675. }
  676. if (port->flags & ASYNC_INITIALIZED) {
  677. func_exit ();
  678. return 0;
  679. }
  680. if (!port->xmit_buf) {
  681. /* We may sleep in get_zeroed_page() */
  682. unsigned long tmp;
  683. tmp = get_zeroed_page(GFP_KERNEL);
  684. spin_lock_irqsave (&port->driver_lock, flags);
  685. if (port->xmit_buf)
  686. free_page (tmp);
  687. else
  688. port->xmit_buf = (unsigned char *) tmp;
  689. spin_unlock_irqrestore(&port->driver_lock, flags);
  690. if (!port->xmit_buf) {
  691. func_exit ();
  692. return -ENOMEM;
  693. }
  694. }
  695. spin_lock_irqsave (&port->driver_lock, flags);
  696. if (port->tty)
  697. clear_bit(TTY_IO_ERROR, &port->tty->flags);
  698. init_MUTEX(&port->port_write_sem);
  699. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  700. spin_unlock_irqrestore(&port->driver_lock, flags);
  701. gs_set_termios(port->tty, NULL);
  702. spin_lock_irqsave (&port->driver_lock, flags);
  703. port->flags |= ASYNC_INITIALIZED;
  704. port->flags &= ~GS_TX_INTEN;
  705. spin_unlock_irqrestore(&port->driver_lock, flags);
  706. func_exit ();
  707. return 0;
  708. }
  709. int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
  710. {
  711. struct serial_struct sio;
  712. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  713. return(-EFAULT);
  714. if (!capable(CAP_SYS_ADMIN)) {
  715. if ((sio.baud_base != port->baud_base) ||
  716. (sio.close_delay != port->close_delay) ||
  717. ((sio.flags & ~ASYNC_USR_MASK) !=
  718. (port->flags & ~ASYNC_USR_MASK)))
  719. return(-EPERM);
  720. }
  721. port->flags = (port->flags & ~ASYNC_USR_MASK) |
  722. (sio.flags & ASYNC_USR_MASK);
  723. port->baud_base = sio.baud_base;
  724. port->close_delay = sio.close_delay;
  725. port->closing_wait = sio.closing_wait;
  726. port->custom_divisor = sio.custom_divisor;
  727. gs_set_termios (port->tty, NULL);
  728. return 0;
  729. }
  730. /*****************************************************************************/
  731. /*
  732. * Generate the serial struct info.
  733. */
  734. int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
  735. {
  736. struct serial_struct sio;
  737. memset(&sio, 0, sizeof(struct serial_struct));
  738. sio.flags = port->flags;
  739. sio.baud_base = port->baud_base;
  740. sio.close_delay = port->close_delay;
  741. sio.closing_wait = port->closing_wait;
  742. sio.custom_divisor = port->custom_divisor;
  743. sio.hub6 = 0;
  744. /* If you want you can override these. */
  745. sio.type = PORT_UNKNOWN;
  746. sio.xmit_fifo_size = -1;
  747. sio.line = -1;
  748. sio.port = -1;
  749. sio.irq = -1;
  750. if (port->rd->getserial)
  751. port->rd->getserial (port, &sio);
  752. if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
  753. return -EFAULT;
  754. return 0;
  755. }
  756. void gs_got_break(struct gs_port *port)
  757. {
  758. func_enter ();
  759. tty_insert_flip_char(port->tty, 0, TTY_BREAK);
  760. tty_schedule_flip(port->tty);
  761. if (port->flags & ASYNC_SAK) {
  762. do_SAK (port->tty);
  763. }
  764. func_exit ();
  765. }
  766. EXPORT_SYMBOL(gs_put_char);
  767. EXPORT_SYMBOL(gs_write);
  768. EXPORT_SYMBOL(gs_write_room);
  769. EXPORT_SYMBOL(gs_chars_in_buffer);
  770. EXPORT_SYMBOL(gs_flush_buffer);
  771. EXPORT_SYMBOL(gs_flush_chars);
  772. EXPORT_SYMBOL(gs_stop);
  773. EXPORT_SYMBOL(gs_start);
  774. EXPORT_SYMBOL(gs_hangup);
  775. EXPORT_SYMBOL(gs_block_til_ready);
  776. EXPORT_SYMBOL(gs_close);
  777. EXPORT_SYMBOL(gs_set_termios);
  778. EXPORT_SYMBOL(gs_init_port);
  779. EXPORT_SYMBOL(gs_setserial);
  780. EXPORT_SYMBOL(gs_getserial);
  781. EXPORT_SYMBOL(gs_got_break);
  782. MODULE_LICENSE("GPL");