generic_serial.c 20 KB

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