generic_serial.c 24 KB

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