generic_serial.c 20 KB

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