generic_serial.c 20 KB

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