generic_serial.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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 mutex_lock(& port->port_write_mutex);
  46. #define RELEASEIT mutex_unlock(&port->port_write_mutex);
  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. mutex_lock(& port->port_write_mutex);
  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_mutex. */
  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. mutex_unlock(& port->port_write_mutex);
  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_mutex. */
  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. /* This is an optimization that is only allowed for dumb cards */
  577. /* Smart cards require knowledge of iflags and oflags too: that
  578. might change hardware cooking mode.... */
  579. if (old_termios) {
  580. if( (tiosp->c_iflag == old_termios->c_iflag)
  581. && (tiosp->c_oflag == old_termios->c_oflag)
  582. && (tiosp->c_cflag == old_termios->c_cflag)
  583. && (tiosp->c_lflag == old_termios->c_lflag)
  584. && (tiosp->c_line == old_termios->c_line)
  585. && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
  586. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
  587. return /* 0 */;
  588. }
  589. } else
  590. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
  591. "no optimization\n");
  592. if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
  593. if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
  594. if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
  595. if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
  596. if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
  597. if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
  598. if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
  599. }
  600. baudrate = tty_get_baud_rate(tty);
  601. baudrate = gs_baudrates[baudrate];
  602. if ((tiosp->c_cflag & CBAUD) == B38400) {
  603. if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  604. baudrate = 57600;
  605. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  606. baudrate = 115200;
  607. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  608. baudrate = 230400;
  609. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  610. baudrate = 460800;
  611. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  612. baudrate = (port->baud_base / port->custom_divisor);
  613. }
  614. /* I recommend using THIS instead of the mess in termios (and
  615. duplicating the above code). Next we should create a clean
  616. interface towards this variable. If your card supports arbitrary
  617. baud rates, (e.g. CD1400 or 16550 based cards) then everything
  618. will be very easy..... */
  619. port->baud = baudrate;
  620. /* Two timer ticks seems enough to wakeup something like SLIP driver */
  621. /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
  622. tmp = (baudrate / 10 / HZ) * 2;
  623. if (tmp < 0) tmp = 0;
  624. if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
  625. port->wakeup_chars = tmp;
  626. /* We should really wait for the characters to be all sent before
  627. changing the settings. -- CAL */
  628. rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
  629. if (rv < 0) return /* rv */;
  630. rv = port->rd->set_real_termios(port);
  631. if (rv < 0) return /* rv */;
  632. if ((!old_termios ||
  633. (old_termios->c_cflag & CRTSCTS)) &&
  634. !( tiosp->c_cflag & CRTSCTS)) {
  635. tty->stopped = 0;
  636. gs_start(tty);
  637. }
  638. #ifdef tytso_patch_94Nov25_1726
  639. /* This "makes sense", Why is it commented out? */
  640. if (!(old_termios->c_cflag & CLOCAL) &&
  641. (tty->termios->c_cflag & CLOCAL))
  642. wake_up_interruptible(&port->gs.open_wait);
  643. #endif
  644. func_exit();
  645. return /* 0 */;
  646. }
  647. /* Must be called with interrupts enabled */
  648. int gs_init_port(struct gs_port *port)
  649. {
  650. unsigned long flags;
  651. unsigned long page;
  652. func_enter ();
  653. if (!tmp_buf) {
  654. page = get_zeroed_page(GFP_KERNEL);
  655. spin_lock_irqsave (&port->driver_lock, flags); /* Don't expect this to make a difference. */
  656. if (tmp_buf)
  657. free_page(page);
  658. else
  659. tmp_buf = (unsigned char *) page;
  660. spin_unlock_irqrestore (&port->driver_lock, flags);
  661. if (!tmp_buf) {
  662. func_exit ();
  663. return -ENOMEM;
  664. }
  665. }
  666. if (port->flags & ASYNC_INITIALIZED) {
  667. func_exit ();
  668. return 0;
  669. }
  670. if (!port->xmit_buf) {
  671. /* We may sleep in get_zeroed_page() */
  672. unsigned long tmp;
  673. tmp = get_zeroed_page(GFP_KERNEL);
  674. spin_lock_irqsave (&port->driver_lock, flags);
  675. if (port->xmit_buf)
  676. free_page (tmp);
  677. else
  678. port->xmit_buf = (unsigned char *) tmp;
  679. spin_unlock_irqrestore(&port->driver_lock, flags);
  680. if (!port->xmit_buf) {
  681. func_exit ();
  682. return -ENOMEM;
  683. }
  684. }
  685. spin_lock_irqsave (&port->driver_lock, flags);
  686. if (port->tty)
  687. clear_bit(TTY_IO_ERROR, &port->tty->flags);
  688. mutex_init(&port->port_write_mutex);
  689. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  690. spin_unlock_irqrestore(&port->driver_lock, flags);
  691. gs_set_termios(port->tty, NULL);
  692. spin_lock_irqsave (&port->driver_lock, flags);
  693. port->flags |= ASYNC_INITIALIZED;
  694. port->flags &= ~GS_TX_INTEN;
  695. spin_unlock_irqrestore(&port->driver_lock, flags);
  696. func_exit ();
  697. return 0;
  698. }
  699. int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
  700. {
  701. struct serial_struct sio;
  702. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  703. return(-EFAULT);
  704. if (!capable(CAP_SYS_ADMIN)) {
  705. if ((sio.baud_base != port->baud_base) ||
  706. (sio.close_delay != port->close_delay) ||
  707. ((sio.flags & ~ASYNC_USR_MASK) !=
  708. (port->flags & ~ASYNC_USR_MASK)))
  709. return(-EPERM);
  710. }
  711. port->flags = (port->flags & ~ASYNC_USR_MASK) |
  712. (sio.flags & ASYNC_USR_MASK);
  713. port->baud_base = sio.baud_base;
  714. port->close_delay = sio.close_delay;
  715. port->closing_wait = sio.closing_wait;
  716. port->custom_divisor = sio.custom_divisor;
  717. gs_set_termios (port->tty, NULL);
  718. return 0;
  719. }
  720. /*****************************************************************************/
  721. /*
  722. * Generate the serial struct info.
  723. */
  724. int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
  725. {
  726. struct serial_struct sio;
  727. memset(&sio, 0, sizeof(struct serial_struct));
  728. sio.flags = port->flags;
  729. sio.baud_base = port->baud_base;
  730. sio.close_delay = port->close_delay;
  731. sio.closing_wait = port->closing_wait;
  732. sio.custom_divisor = port->custom_divisor;
  733. sio.hub6 = 0;
  734. /* If you want you can override these. */
  735. sio.type = PORT_UNKNOWN;
  736. sio.xmit_fifo_size = -1;
  737. sio.line = -1;
  738. sio.port = -1;
  739. sio.irq = -1;
  740. if (port->rd->getserial)
  741. port->rd->getserial (port, &sio);
  742. if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
  743. return -EFAULT;
  744. return 0;
  745. }
  746. void gs_got_break(struct gs_port *port)
  747. {
  748. func_enter ();
  749. tty_insert_flip_char(port->tty, 0, TTY_BREAK);
  750. tty_schedule_flip(port->tty);
  751. if (port->flags & ASYNC_SAK) {
  752. do_SAK (port->tty);
  753. }
  754. func_exit ();
  755. }
  756. EXPORT_SYMBOL(gs_put_char);
  757. EXPORT_SYMBOL(gs_write);
  758. EXPORT_SYMBOL(gs_write_room);
  759. EXPORT_SYMBOL(gs_chars_in_buffer);
  760. EXPORT_SYMBOL(gs_flush_buffer);
  761. EXPORT_SYMBOL(gs_flush_chars);
  762. EXPORT_SYMBOL(gs_stop);
  763. EXPORT_SYMBOL(gs_start);
  764. EXPORT_SYMBOL(gs_hangup);
  765. EXPORT_SYMBOL(gs_block_til_ready);
  766. EXPORT_SYMBOL(gs_close);
  767. EXPORT_SYMBOL(gs_set_termios);
  768. EXPORT_SYMBOL(gs_init_port);
  769. EXPORT_SYMBOL(gs_setserial);
  770. EXPORT_SYMBOL(gs_getserial);
  771. EXPORT_SYMBOL(gs_got_break);
  772. MODULE_LICENSE("GPL");