generic_serial.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. tty_wakeup(tty);
  301. func_exit ();
  302. }
  303. void gs_flush_chars(struct tty_struct * tty)
  304. {
  305. struct gs_port *port;
  306. func_enter ();
  307. if (!tty) return;
  308. port = tty->driver_data;
  309. if (!port) return;
  310. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  311. !port->xmit_buf) {
  312. func_exit ();
  313. return;
  314. }
  315. /* Beats me -- REW */
  316. port->flags |= GS_TX_INTEN;
  317. port->rd->enable_tx_interrupts (port);
  318. func_exit ();
  319. }
  320. void gs_stop(struct tty_struct * tty)
  321. {
  322. struct gs_port *port;
  323. func_enter ();
  324. if (!tty) return;
  325. port = tty->driver_data;
  326. if (!port) return;
  327. if (port->xmit_cnt &&
  328. port->xmit_buf &&
  329. (port->flags & GS_TX_INTEN) ) {
  330. port->flags &= ~GS_TX_INTEN;
  331. port->rd->disable_tx_interrupts (port);
  332. }
  333. func_exit ();
  334. }
  335. void gs_start(struct tty_struct * tty)
  336. {
  337. struct gs_port *port;
  338. if (!tty) return;
  339. port = tty->driver_data;
  340. if (!port) return;
  341. if (port->xmit_cnt &&
  342. port->xmit_buf &&
  343. !(port->flags & GS_TX_INTEN) ) {
  344. port->flags |= GS_TX_INTEN;
  345. port->rd->enable_tx_interrupts (port);
  346. }
  347. func_exit ();
  348. }
  349. static void gs_shutdown_port (struct gs_port *port)
  350. {
  351. unsigned long flags;
  352. func_enter();
  353. if (!port) return;
  354. if (!(port->flags & ASYNC_INITIALIZED))
  355. return;
  356. spin_lock_irqsave(&port->driver_lock, flags);
  357. if (port->xmit_buf) {
  358. free_page((unsigned long) port->xmit_buf);
  359. port->xmit_buf = NULL;
  360. }
  361. if (port->tty)
  362. set_bit(TTY_IO_ERROR, &port->tty->flags);
  363. port->rd->shutdown_port (port);
  364. port->flags &= ~ASYNC_INITIALIZED;
  365. spin_unlock_irqrestore(&port->driver_lock, flags);
  366. func_exit();
  367. }
  368. void gs_hangup(struct tty_struct *tty)
  369. {
  370. struct gs_port *port;
  371. func_enter ();
  372. if (!tty) return;
  373. port = tty->driver_data;
  374. tty = port->tty;
  375. if (!tty)
  376. return;
  377. gs_shutdown_port (port);
  378. port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
  379. port->tty = NULL;
  380. port->count = 0;
  381. wake_up_interruptible(&port->open_wait);
  382. func_exit ();
  383. }
  384. int gs_block_til_ready(void *port_, struct file * filp)
  385. {
  386. struct gs_port *port = port_;
  387. DECLARE_WAITQUEUE(wait, current);
  388. int retval;
  389. int do_clocal = 0;
  390. int CD;
  391. struct tty_struct *tty;
  392. unsigned long flags;
  393. func_enter ();
  394. if (!port) return 0;
  395. tty = port->tty;
  396. if (!tty) return 0;
  397. gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
  398. /*
  399. * If the device is in the middle of being closed, then block
  400. * until it's done, and then try again.
  401. */
  402. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  403. interruptible_sleep_on(&port->close_wait);
  404. if (port->flags & ASYNC_HUP_NOTIFY)
  405. return -EAGAIN;
  406. else
  407. return -ERESTARTSYS;
  408. }
  409. gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
  410. /*
  411. * If non-blocking mode is set, or the port is not enabled,
  412. * then make the check up front and then exit.
  413. */
  414. if ((filp->f_flags & O_NONBLOCK) ||
  415. (tty->flags & (1 << TTY_IO_ERROR))) {
  416. port->flags |= ASYNC_NORMAL_ACTIVE;
  417. return 0;
  418. }
  419. gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
  420. if (C_CLOCAL(tty))
  421. do_clocal = 1;
  422. /*
  423. * Block waiting for the carrier detect and the line to become
  424. * free (i.e., not in use by the callout). While we are in
  425. * this loop, port->count is dropped by one, so that
  426. * rs_close() knows when to free things. We restore it upon
  427. * exit, either normal or abnormal.
  428. */
  429. retval = 0;
  430. add_wait_queue(&port->open_wait, &wait);
  431. gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
  432. spin_lock_irqsave(&port->driver_lock, flags);
  433. if (!tty_hung_up_p(filp)) {
  434. port->count--;
  435. }
  436. spin_unlock_irqrestore(&port->driver_lock, flags);
  437. port->blocked_open++;
  438. while (1) {
  439. CD = port->rd->get_CD (port);
  440. gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
  441. set_current_state (TASK_INTERRUPTIBLE);
  442. if (tty_hung_up_p(filp) ||
  443. !(port->flags & ASYNC_INITIALIZED)) {
  444. if (port->flags & ASYNC_HUP_NOTIFY)
  445. retval = -EAGAIN;
  446. else
  447. retval = -ERESTARTSYS;
  448. break;
  449. }
  450. if (!(port->flags & ASYNC_CLOSING) &&
  451. (do_clocal || CD))
  452. break;
  453. gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
  454. (int)signal_pending (current), *(long*)(&current->blocked));
  455. if (signal_pending(current)) {
  456. retval = -ERESTARTSYS;
  457. break;
  458. }
  459. schedule();
  460. }
  461. gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
  462. port->blocked_open);
  463. set_current_state (TASK_RUNNING);
  464. remove_wait_queue(&port->open_wait, &wait);
  465. if (!tty_hung_up_p(filp)) {
  466. port->count++;
  467. }
  468. port->blocked_open--;
  469. if (retval)
  470. return retval;
  471. port->flags |= ASYNC_NORMAL_ACTIVE;
  472. func_exit ();
  473. return 0;
  474. }
  475. void gs_close(struct tty_struct * tty, struct file * filp)
  476. {
  477. unsigned long flags;
  478. struct gs_port *port;
  479. func_enter ();
  480. if (!tty) return;
  481. port = (struct gs_port *) tty->driver_data;
  482. if (!port) return;
  483. if (!port->tty) {
  484. /* This seems to happen when this is called from vhangup. */
  485. gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
  486. port->tty = tty;
  487. }
  488. spin_lock_irqsave(&port->driver_lock, flags);
  489. if (tty_hung_up_p(filp)) {
  490. spin_unlock_irqrestore(&port->driver_lock, flags);
  491. if (port->rd->hungup)
  492. port->rd->hungup (port);
  493. func_exit ();
  494. return;
  495. }
  496. if ((tty->count == 1) && (port->count != 1)) {
  497. printk(KERN_ERR "gs: gs_close port %p: bad port count;"
  498. " tty->count is 1, port count is %d\n", port, port->count);
  499. port->count = 1;
  500. }
  501. if (--port->count < 0) {
  502. printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->count);
  503. port->count = 0;
  504. }
  505. if (port->count) {
  506. gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->count);
  507. spin_unlock_irqrestore(&port->driver_lock, flags);
  508. func_exit ();
  509. return;
  510. }
  511. port->flags |= ASYNC_CLOSING;
  512. /*
  513. * Now we wait for the transmit buffer to clear; and we notify
  514. * the line discipline to only process XON/XOFF characters.
  515. */
  516. tty->closing = 1;
  517. /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  518. tty_wait_until_sent(tty, port->closing_wait); */
  519. /*
  520. * At this point we stop accepting input. To do this, we
  521. * disable the receive line status interrupts, and tell the
  522. * interrupt driver to stop checking the data ready bit in the
  523. * line status register.
  524. */
  525. port->rd->disable_rx_interrupts (port);
  526. spin_unlock_irqrestore(&port->driver_lock, flags);
  527. /* close has no way of returning "EINTR", so discard return value */
  528. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  529. gs_wait_tx_flushed (port, port->closing_wait);
  530. port->flags &= ~GS_ACTIVE;
  531. if (tty->driver->flush_buffer)
  532. tty->driver->flush_buffer(tty);
  533. tty_ldisc_flush(tty);
  534. tty->closing = 0;
  535. port->event = 0;
  536. port->rd->close (port);
  537. port->rd->shutdown_port (port);
  538. port->tty = NULL;
  539. if (port->blocked_open) {
  540. if (port->close_delay) {
  541. spin_unlock_irqrestore(&port->driver_lock, flags);
  542. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  543. spin_lock_irqsave(&port->driver_lock, flags);
  544. }
  545. wake_up_interruptible(&port->open_wait);
  546. }
  547. port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
  548. wake_up_interruptible(&port->close_wait);
  549. func_exit ();
  550. }
  551. void gs_set_termios (struct tty_struct * tty,
  552. struct ktermios * old_termios)
  553. {
  554. struct gs_port *port;
  555. int baudrate, tmp, rv;
  556. struct ktermios *tiosp;
  557. func_enter();
  558. if (!tty) return;
  559. port = tty->driver_data;
  560. if (!port) return;
  561. if (!port->tty) {
  562. /* This seems to happen when this is called after gs_close. */
  563. gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->tty is NULL\n");
  564. port->tty = tty;
  565. }
  566. tiosp = tty->termios;
  567. if (gs_debug & GS_DEBUG_TERMIOS) {
  568. gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
  569. }
  570. /* This is an optimization that is only allowed for dumb cards */
  571. /* Smart cards require knowledge of iflags and oflags too: that
  572. might change hardware cooking mode.... */
  573. if (old_termios) {
  574. if( (tiosp->c_iflag == old_termios->c_iflag)
  575. && (tiosp->c_oflag == old_termios->c_oflag)
  576. && (tiosp->c_cflag == old_termios->c_cflag)
  577. && (tiosp->c_lflag == old_termios->c_lflag)
  578. && (tiosp->c_line == old_termios->c_line)
  579. && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
  580. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
  581. return /* 0 */;
  582. }
  583. } else
  584. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
  585. "no optimization\n");
  586. if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
  587. if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
  588. if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
  589. if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
  590. if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
  591. if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
  592. if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
  593. }
  594. baudrate = tty_get_baud_rate(tty);
  595. if ((tiosp->c_cflag & CBAUD) == B38400) {
  596. if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  597. baudrate = 57600;
  598. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  599. baudrate = 115200;
  600. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  601. baudrate = 230400;
  602. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  603. baudrate = 460800;
  604. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  605. baudrate = (port->baud_base / port->custom_divisor);
  606. }
  607. /* I recommend using THIS instead of the mess in termios (and
  608. duplicating the above code). Next we should create a clean
  609. interface towards this variable. If your card supports arbitrary
  610. baud rates, (e.g. CD1400 or 16550 based cards) then everything
  611. will be very easy..... */
  612. port->baud = baudrate;
  613. /* Two timer ticks seems enough to wakeup something like SLIP driver */
  614. /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
  615. tmp = (baudrate / 10 / HZ) * 2;
  616. if (tmp < 0) tmp = 0;
  617. if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
  618. port->wakeup_chars = tmp;
  619. /* We should really wait for the characters to be all sent before
  620. changing the settings. -- CAL */
  621. rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
  622. if (rv < 0) return /* rv */;
  623. rv = port->rd->set_real_termios(port);
  624. if (rv < 0) return /* rv */;
  625. if ((!old_termios ||
  626. (old_termios->c_cflag & CRTSCTS)) &&
  627. !( tiosp->c_cflag & CRTSCTS)) {
  628. tty->stopped = 0;
  629. gs_start(tty);
  630. }
  631. #ifdef tytso_patch_94Nov25_1726
  632. /* This "makes sense", Why is it commented out? */
  633. if (!(old_termios->c_cflag & CLOCAL) &&
  634. (tty->termios->c_cflag & CLOCAL))
  635. wake_up_interruptible(&port->gs.open_wait);
  636. #endif
  637. func_exit();
  638. return /* 0 */;
  639. }
  640. /* Must be called with interrupts enabled */
  641. int gs_init_port(struct gs_port *port)
  642. {
  643. unsigned long flags;
  644. func_enter ();
  645. if (port->flags & ASYNC_INITIALIZED) {
  646. func_exit ();
  647. return 0;
  648. }
  649. if (!port->xmit_buf) {
  650. /* We may sleep in get_zeroed_page() */
  651. unsigned long tmp;
  652. tmp = get_zeroed_page(GFP_KERNEL);
  653. spin_lock_irqsave (&port->driver_lock, flags);
  654. if (port->xmit_buf)
  655. free_page (tmp);
  656. else
  657. port->xmit_buf = (unsigned char *) tmp;
  658. spin_unlock_irqrestore(&port->driver_lock, flags);
  659. if (!port->xmit_buf) {
  660. func_exit ();
  661. return -ENOMEM;
  662. }
  663. }
  664. spin_lock_irqsave (&port->driver_lock, flags);
  665. if (port->tty)
  666. clear_bit(TTY_IO_ERROR, &port->tty->flags);
  667. mutex_init(&port->port_write_mutex);
  668. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  669. spin_unlock_irqrestore(&port->driver_lock, flags);
  670. gs_set_termios(port->tty, NULL);
  671. spin_lock_irqsave (&port->driver_lock, flags);
  672. port->flags |= ASYNC_INITIALIZED;
  673. port->flags &= ~GS_TX_INTEN;
  674. spin_unlock_irqrestore(&port->driver_lock, flags);
  675. func_exit ();
  676. return 0;
  677. }
  678. int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
  679. {
  680. struct serial_struct sio;
  681. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  682. return(-EFAULT);
  683. if (!capable(CAP_SYS_ADMIN)) {
  684. if ((sio.baud_base != port->baud_base) ||
  685. (sio.close_delay != port->close_delay) ||
  686. ((sio.flags & ~ASYNC_USR_MASK) !=
  687. (port->flags & ~ASYNC_USR_MASK)))
  688. return(-EPERM);
  689. }
  690. port->flags = (port->flags & ~ASYNC_USR_MASK) |
  691. (sio.flags & ASYNC_USR_MASK);
  692. port->baud_base = sio.baud_base;
  693. port->close_delay = sio.close_delay;
  694. port->closing_wait = sio.closing_wait;
  695. port->custom_divisor = sio.custom_divisor;
  696. gs_set_termios (port->tty, NULL);
  697. return 0;
  698. }
  699. /*****************************************************************************/
  700. /*
  701. * Generate the serial struct info.
  702. */
  703. int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
  704. {
  705. struct serial_struct sio;
  706. memset(&sio, 0, sizeof(struct serial_struct));
  707. sio.flags = port->flags;
  708. sio.baud_base = port->baud_base;
  709. sio.close_delay = port->close_delay;
  710. sio.closing_wait = port->closing_wait;
  711. sio.custom_divisor = port->custom_divisor;
  712. sio.hub6 = 0;
  713. /* If you want you can override these. */
  714. sio.type = PORT_UNKNOWN;
  715. sio.xmit_fifo_size = -1;
  716. sio.line = -1;
  717. sio.port = -1;
  718. sio.irq = -1;
  719. if (port->rd->getserial)
  720. port->rd->getserial (port, &sio);
  721. if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
  722. return -EFAULT;
  723. return 0;
  724. }
  725. void gs_got_break(struct gs_port *port)
  726. {
  727. func_enter ();
  728. tty_insert_flip_char(port->tty, 0, TTY_BREAK);
  729. tty_schedule_flip(port->tty);
  730. if (port->flags & ASYNC_SAK) {
  731. do_SAK (port->tty);
  732. }
  733. func_exit ();
  734. }
  735. EXPORT_SYMBOL(gs_put_char);
  736. EXPORT_SYMBOL(gs_write);
  737. EXPORT_SYMBOL(gs_write_room);
  738. EXPORT_SYMBOL(gs_chars_in_buffer);
  739. EXPORT_SYMBOL(gs_flush_buffer);
  740. EXPORT_SYMBOL(gs_flush_chars);
  741. EXPORT_SYMBOL(gs_stop);
  742. EXPORT_SYMBOL(gs_start);
  743. EXPORT_SYMBOL(gs_hangup);
  744. EXPORT_SYMBOL(gs_block_til_ready);
  745. EXPORT_SYMBOL(gs_close);
  746. EXPORT_SYMBOL(gs_set_termios);
  747. EXPORT_SYMBOL(gs_init_port);
  748. EXPORT_SYMBOL(gs_setserial);
  749. EXPORT_SYMBOL(gs_getserial);
  750. EXPORT_SYMBOL(gs_got_break);
  751. MODULE_LICENSE("GPL");