generic_serial.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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/uaccess.h>
  31. #define DEBUG
  32. static int gs_debug;
  33. #ifdef DEBUG
  34. #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
  35. #else
  36. #define gs_dprintk(f, str...) /* nothing */
  37. #endif
  38. #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __FUNCTION__)
  39. #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit %s\n", __FUNCTION__)
  40. #define RS_EVENT_WRITE_WAKEUP 1
  41. module_param(gs_debug, int, 0644);
  42. void gs_put_char(struct tty_struct * tty, unsigned char ch)
  43. {
  44. struct gs_port *port;
  45. func_enter ();
  46. if (!tty) return;
  47. port = tty->driver_data;
  48. if (!port) return;
  49. if (! (port->flags & ASYNC_INITIALIZED)) return;
  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;
  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. }
  63. /*
  64. > Problems to take into account are:
  65. > -1- Interrupts that empty part of the buffer.
  66. > -2- page faults on the access to userspace.
  67. > -3- Other processes that are also trying to do a "write".
  68. */
  69. int gs_write(struct tty_struct * tty,
  70. const unsigned char *buf, int count)
  71. {
  72. struct gs_port *port;
  73. int c, total = 0;
  74. int t;
  75. func_enter ();
  76. if (!tty) return 0;
  77. port = tty->driver_data;
  78. if (!port) return 0;
  79. if (! (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->flags & GS_TX_INTEN)?"enabled": "disabled");
  109. if (port->xmit_cnt &&
  110. !tty->stopped &&
  111. !tty->hw_stopped &&
  112. !(port->flags & GS_TX_INTEN)) {
  113. 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. if (!tty) return 0;
  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->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->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->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. if (!tty) return;
  210. port = tty->driver_data;
  211. if (!port) return;
  212. /* XXX Would the write semaphore do? */
  213. spin_lock_irqsave (&port->driver_lock, flags);
  214. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  215. spin_unlock_irqrestore (&port->driver_lock, flags);
  216. tty_wakeup(tty);
  217. func_exit ();
  218. }
  219. void gs_flush_chars(struct tty_struct * tty)
  220. {
  221. struct gs_port *port;
  222. func_enter ();
  223. if (!tty) return;
  224. port = tty->driver_data;
  225. if (!port) return;
  226. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  227. !port->xmit_buf) {
  228. func_exit ();
  229. return;
  230. }
  231. /* Beats me -- REW */
  232. port->flags |= GS_TX_INTEN;
  233. port->rd->enable_tx_interrupts (port);
  234. func_exit ();
  235. }
  236. void gs_stop(struct tty_struct * tty)
  237. {
  238. struct gs_port *port;
  239. func_enter ();
  240. if (!tty) return;
  241. port = tty->driver_data;
  242. if (!port) return;
  243. if (port->xmit_cnt &&
  244. port->xmit_buf &&
  245. (port->flags & GS_TX_INTEN) ) {
  246. port->flags &= ~GS_TX_INTEN;
  247. port->rd->disable_tx_interrupts (port);
  248. }
  249. func_exit ();
  250. }
  251. void gs_start(struct tty_struct * tty)
  252. {
  253. struct gs_port *port;
  254. if (!tty) return;
  255. port = tty->driver_data;
  256. if (!port) return;
  257. if (port->xmit_cnt &&
  258. port->xmit_buf &&
  259. !(port->flags & GS_TX_INTEN) ) {
  260. port->flags |= GS_TX_INTEN;
  261. port->rd->enable_tx_interrupts (port);
  262. }
  263. func_exit ();
  264. }
  265. static void gs_shutdown_port (struct gs_port *port)
  266. {
  267. unsigned long flags;
  268. func_enter();
  269. if (!port) return;
  270. if (!(port->flags & ASYNC_INITIALIZED))
  271. return;
  272. spin_lock_irqsave(&port->driver_lock, flags);
  273. if (port->xmit_buf) {
  274. free_page((unsigned long) port->xmit_buf);
  275. port->xmit_buf = NULL;
  276. }
  277. if (port->tty)
  278. set_bit(TTY_IO_ERROR, &port->tty->flags);
  279. port->rd->shutdown_port (port);
  280. port->flags &= ~ASYNC_INITIALIZED;
  281. spin_unlock_irqrestore(&port->driver_lock, flags);
  282. func_exit();
  283. }
  284. void gs_hangup(struct tty_struct *tty)
  285. {
  286. struct gs_port *port;
  287. func_enter ();
  288. if (!tty) return;
  289. port = tty->driver_data;
  290. tty = port->tty;
  291. if (!tty)
  292. return;
  293. gs_shutdown_port (port);
  294. port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
  295. port->tty = NULL;
  296. port->count = 0;
  297. wake_up_interruptible(&port->open_wait);
  298. func_exit ();
  299. }
  300. int gs_block_til_ready(void *port_, struct file * filp)
  301. {
  302. struct gs_port *port = port_;
  303. DECLARE_WAITQUEUE(wait, current);
  304. int retval;
  305. int do_clocal = 0;
  306. int CD;
  307. struct tty_struct *tty;
  308. unsigned long flags;
  309. func_enter ();
  310. if (!port) return 0;
  311. tty = port->tty;
  312. if (!tty) return 0;
  313. gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
  314. /*
  315. * If the device is in the middle of being closed, then block
  316. * until it's done, and then try again.
  317. */
  318. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  319. interruptible_sleep_on(&port->close_wait);
  320. if (port->flags & ASYNC_HUP_NOTIFY)
  321. return -EAGAIN;
  322. else
  323. return -ERESTARTSYS;
  324. }
  325. gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
  326. /*
  327. * If non-blocking mode is set, or the port is not enabled,
  328. * then make the check up front and then exit.
  329. */
  330. if ((filp->f_flags & O_NONBLOCK) ||
  331. (tty->flags & (1 << TTY_IO_ERROR))) {
  332. port->flags |= ASYNC_NORMAL_ACTIVE;
  333. return 0;
  334. }
  335. gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
  336. if (C_CLOCAL(tty))
  337. do_clocal = 1;
  338. /*
  339. * Block waiting for the carrier detect and the line to become
  340. * free (i.e., not in use by the callout). While we are in
  341. * this loop, port->count is dropped by one, so that
  342. * rs_close() knows when to free things. We restore it upon
  343. * exit, either normal or abnormal.
  344. */
  345. retval = 0;
  346. add_wait_queue(&port->open_wait, &wait);
  347. gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
  348. spin_lock_irqsave(&port->driver_lock, flags);
  349. if (!tty_hung_up_p(filp)) {
  350. port->count--;
  351. }
  352. spin_unlock_irqrestore(&port->driver_lock, flags);
  353. port->blocked_open++;
  354. while (1) {
  355. CD = port->rd->get_CD (port);
  356. gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
  357. set_current_state (TASK_INTERRUPTIBLE);
  358. if (tty_hung_up_p(filp) ||
  359. !(port->flags & ASYNC_INITIALIZED)) {
  360. if (port->flags & ASYNC_HUP_NOTIFY)
  361. retval = -EAGAIN;
  362. else
  363. retval = -ERESTARTSYS;
  364. break;
  365. }
  366. if (!(port->flags & ASYNC_CLOSING) &&
  367. (do_clocal || CD))
  368. break;
  369. gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
  370. (int)signal_pending (current), *(long*)(&current->blocked));
  371. if (signal_pending(current)) {
  372. retval = -ERESTARTSYS;
  373. break;
  374. }
  375. schedule();
  376. }
  377. gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
  378. port->blocked_open);
  379. set_current_state (TASK_RUNNING);
  380. remove_wait_queue(&port->open_wait, &wait);
  381. if (!tty_hung_up_p(filp)) {
  382. port->count++;
  383. }
  384. port->blocked_open--;
  385. if (retval)
  386. return retval;
  387. port->flags |= ASYNC_NORMAL_ACTIVE;
  388. func_exit ();
  389. return 0;
  390. }
  391. void gs_close(struct tty_struct * tty, struct file * filp)
  392. {
  393. unsigned long flags;
  394. struct gs_port *port;
  395. func_enter ();
  396. if (!tty) return;
  397. port = (struct gs_port *) tty->driver_data;
  398. if (!port) return;
  399. if (!port->tty) {
  400. /* This seems to happen when this is called from vhangup. */
  401. gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
  402. port->tty = tty;
  403. }
  404. spin_lock_irqsave(&port->driver_lock, flags);
  405. if (tty_hung_up_p(filp)) {
  406. spin_unlock_irqrestore(&port->driver_lock, flags);
  407. if (port->rd->hungup)
  408. port->rd->hungup (port);
  409. func_exit ();
  410. return;
  411. }
  412. if ((tty->count == 1) && (port->count != 1)) {
  413. printk(KERN_ERR "gs: gs_close port %p: bad port count;"
  414. " tty->count is 1, port count is %d\n", port, port->count);
  415. port->count = 1;
  416. }
  417. if (--port->count < 0) {
  418. printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->count);
  419. port->count = 0;
  420. }
  421. if (port->count) {
  422. gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->count);
  423. spin_unlock_irqrestore(&port->driver_lock, flags);
  424. func_exit ();
  425. return;
  426. }
  427. port->flags |= ASYNC_CLOSING;
  428. /*
  429. * Now we wait for the transmit buffer to clear; and we notify
  430. * the line discipline to only process XON/XOFF characters.
  431. */
  432. tty->closing = 1;
  433. /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  434. tty_wait_until_sent(tty, port->closing_wait); */
  435. /*
  436. * At this point we stop accepting input. To do this, we
  437. * disable the receive line status interrupts, and tell the
  438. * interrupt driver to stop checking the data ready bit in the
  439. * line status register.
  440. */
  441. port->rd->disable_rx_interrupts (port);
  442. spin_unlock_irqrestore(&port->driver_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->flags &= ~GS_ACTIVE;
  447. if (tty->driver->flush_buffer)
  448. tty->driver->flush_buffer(tty);
  449. tty_ldisc_flush(tty);
  450. tty->closing = 0;
  451. port->event = 0;
  452. port->rd->close (port);
  453. port->rd->shutdown_port (port);
  454. port->tty = NULL;
  455. if (port->blocked_open) {
  456. if (port->close_delay) {
  457. spin_unlock_irqrestore(&port->driver_lock, flags);
  458. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  459. spin_lock_irqsave(&port->driver_lock, flags);
  460. }
  461. wake_up_interruptible(&port->open_wait);
  462. }
  463. port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
  464. wake_up_interruptible(&port->close_wait);
  465. func_exit ();
  466. }
  467. void gs_set_termios (struct tty_struct * tty,
  468. struct ktermios * old_termios)
  469. {
  470. struct gs_port *port;
  471. int baudrate, tmp, rv;
  472. struct ktermios *tiosp;
  473. func_enter();
  474. if (!tty) return;
  475. port = tty->driver_data;
  476. if (!port) return;
  477. if (!port->tty) {
  478. /* This seems to happen when this is called after gs_close. */
  479. gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->tty is NULL\n");
  480. port->tty = tty;
  481. }
  482. tiosp = tty->termios;
  483. if (gs_debug & GS_DEBUG_TERMIOS) {
  484. gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
  485. }
  486. if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
  487. if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
  488. if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
  489. if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
  490. if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
  491. if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
  492. if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
  493. }
  494. baudrate = tty_get_baud_rate(tty);
  495. if ((tiosp->c_cflag & CBAUD) == B38400) {
  496. if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  497. baudrate = 57600;
  498. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  499. baudrate = 115200;
  500. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  501. baudrate = 230400;
  502. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  503. baudrate = 460800;
  504. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  505. baudrate = (port->baud_base / port->custom_divisor);
  506. }
  507. /* I recommend using THIS instead of the mess in termios (and
  508. duplicating the above code). Next we should create a clean
  509. interface towards this variable. If your card supports arbitrary
  510. baud rates, (e.g. CD1400 or 16550 based cards) then everything
  511. will be very easy..... */
  512. port->baud = baudrate;
  513. /* Two timer ticks seems enough to wakeup something like SLIP driver */
  514. /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
  515. tmp = (baudrate / 10 / HZ) * 2;
  516. if (tmp < 0) tmp = 0;
  517. if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
  518. port->wakeup_chars = tmp;
  519. /* We should really wait for the characters to be all sent before
  520. changing the settings. -- CAL */
  521. rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
  522. if (rv < 0) return /* rv */;
  523. rv = port->rd->set_real_termios(port);
  524. if (rv < 0) return /* rv */;
  525. if ((!old_termios ||
  526. (old_termios->c_cflag & CRTSCTS)) &&
  527. !( tiosp->c_cflag & CRTSCTS)) {
  528. tty->stopped = 0;
  529. gs_start(tty);
  530. }
  531. #ifdef tytso_patch_94Nov25_1726
  532. /* This "makes sense", Why is it commented out? */
  533. if (!(old_termios->c_cflag & CLOCAL) &&
  534. (tty->termios->c_cflag & CLOCAL))
  535. wake_up_interruptible(&port->gs.open_wait);
  536. #endif
  537. func_exit();
  538. return /* 0 */;
  539. }
  540. /* Must be called with interrupts enabled */
  541. int gs_init_port(struct gs_port *port)
  542. {
  543. unsigned long flags;
  544. func_enter ();
  545. if (port->flags & ASYNC_INITIALIZED) {
  546. func_exit ();
  547. return 0;
  548. }
  549. if (!port->xmit_buf) {
  550. /* We may sleep in get_zeroed_page() */
  551. unsigned long tmp;
  552. tmp = get_zeroed_page(GFP_KERNEL);
  553. spin_lock_irqsave (&port->driver_lock, flags);
  554. if (port->xmit_buf)
  555. free_page (tmp);
  556. else
  557. port->xmit_buf = (unsigned char *) tmp;
  558. spin_unlock_irqrestore(&port->driver_lock, flags);
  559. if (!port->xmit_buf) {
  560. func_exit ();
  561. return -ENOMEM;
  562. }
  563. }
  564. spin_lock_irqsave (&port->driver_lock, flags);
  565. if (port->tty)
  566. clear_bit(TTY_IO_ERROR, &port->tty->flags);
  567. mutex_init(&port->port_write_mutex);
  568. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  569. spin_unlock_irqrestore(&port->driver_lock, flags);
  570. gs_set_termios(port->tty, NULL);
  571. spin_lock_irqsave (&port->driver_lock, flags);
  572. port->flags |= ASYNC_INITIALIZED;
  573. port->flags &= ~GS_TX_INTEN;
  574. spin_unlock_irqrestore(&port->driver_lock, flags);
  575. func_exit ();
  576. return 0;
  577. }
  578. int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
  579. {
  580. struct serial_struct sio;
  581. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  582. return(-EFAULT);
  583. if (!capable(CAP_SYS_ADMIN)) {
  584. if ((sio.baud_base != port->baud_base) ||
  585. (sio.close_delay != port->close_delay) ||
  586. ((sio.flags & ~ASYNC_USR_MASK) !=
  587. (port->flags & ~ASYNC_USR_MASK)))
  588. return(-EPERM);
  589. }
  590. port->flags = (port->flags & ~ASYNC_USR_MASK) |
  591. (sio.flags & ASYNC_USR_MASK);
  592. port->baud_base = sio.baud_base;
  593. port->close_delay = sio.close_delay;
  594. port->closing_wait = sio.closing_wait;
  595. port->custom_divisor = sio.custom_divisor;
  596. gs_set_termios (port->tty, NULL);
  597. return 0;
  598. }
  599. /*****************************************************************************/
  600. /*
  601. * Generate the serial struct info.
  602. */
  603. int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
  604. {
  605. struct serial_struct sio;
  606. memset(&sio, 0, sizeof(struct serial_struct));
  607. sio.flags = port->flags;
  608. sio.baud_base = port->baud_base;
  609. sio.close_delay = port->close_delay;
  610. sio.closing_wait = port->closing_wait;
  611. sio.custom_divisor = port->custom_divisor;
  612. sio.hub6 = 0;
  613. /* If you want you can override these. */
  614. sio.type = PORT_UNKNOWN;
  615. sio.xmit_fifo_size = -1;
  616. sio.line = -1;
  617. sio.port = -1;
  618. sio.irq = -1;
  619. if (port->rd->getserial)
  620. port->rd->getserial (port, &sio);
  621. if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
  622. return -EFAULT;
  623. return 0;
  624. }
  625. void gs_got_break(struct gs_port *port)
  626. {
  627. func_enter ();
  628. tty_insert_flip_char(port->tty, 0, TTY_BREAK);
  629. tty_schedule_flip(port->tty);
  630. if (port->flags & ASYNC_SAK) {
  631. do_SAK (port->tty);
  632. }
  633. func_exit ();
  634. }
  635. EXPORT_SYMBOL(gs_put_char);
  636. EXPORT_SYMBOL(gs_write);
  637. EXPORT_SYMBOL(gs_write_room);
  638. EXPORT_SYMBOL(gs_chars_in_buffer);
  639. EXPORT_SYMBOL(gs_flush_buffer);
  640. EXPORT_SYMBOL(gs_flush_chars);
  641. EXPORT_SYMBOL(gs_stop);
  642. EXPORT_SYMBOL(gs_start);
  643. EXPORT_SYMBOL(gs_hangup);
  644. EXPORT_SYMBOL(gs_block_til_ready);
  645. EXPORT_SYMBOL(gs_close);
  646. EXPORT_SYMBOL(gs_set_termios);
  647. EXPORT_SYMBOL(gs_init_port);
  648. EXPORT_SYMBOL(gs_setserial);
  649. EXPORT_SYMBOL(gs_getserial);
  650. EXPORT_SYMBOL(gs_got_break);
  651. MODULE_LICENSE("GPL");