generic_serial.c 23 KB

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