generic_serial.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:26k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <asm/semaphore.h>
  28. #include <asm/uaccess.h>
  29. #define DEBUG 
  30. static char *                  tmp_buf; 
  31. static DECLARE_MUTEX(tmp_buf_sem);
  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 %sn", __FUNCTION__)
  39. #define func_exit()  gs_dprintk (GS_DEBUG_FLOW, "gs: exit  %sn", __FUNCTION__)
  40. #if NEW_WRITE_LOCKING
  41. #define DECL      /* Nothing */
  42. #define LOCKIT    down (& port->port_write_sem);
  43. #define RELEASEIT up (&port->port_write_sem);
  44. #else
  45. #define DECL      unsigned long flags;
  46. #define LOCKIT    save_flags (flags);cli ()
  47. #define RELEASEIT restore_flags (flags)
  48. #endif
  49. #define RS_EVENT_WRITE_WAKEUP 1
  50. MODULE_PARM(gs_debug, "i");
  51. void gs_put_char(struct tty_struct * tty, unsigned char ch)
  52. {
  53. struct gs_port *port;
  54. DECL
  55. func_enter (); 
  56. if (!tty) return;
  57. port = tty->driver_data;
  58. if (!port) return;
  59. if (! (port->flags & ASYNC_INITIALIZED)) return;
  60. /* Take a lock on the serial tranmit buffer! */
  61. LOCKIT;
  62. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
  63. /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
  64. RELEASEIT;
  65. return;
  66. }
  67. port->xmit_buf[port->xmit_head++] = ch;
  68. port->xmit_head &= SERIAL_XMIT_SIZE - 1;
  69. port->xmit_cnt++;  /* Characters in buffer */
  70. RELEASEIT;
  71. func_exit ();
  72. }
  73. #ifdef NEW_WRITE_LOCKING
  74. /*
  75. > Problems to take into account are:
  76. >       -1- Interrupts that empty part of the buffer.
  77. >       -2- page faults on the access to userspace. 
  78. >       -3- Other processes that are also trying to do a "write". 
  79. */
  80. int gs_write(struct tty_struct * tty, int from_user, 
  81.                     const unsigned char *buf, int count)
  82. {
  83. struct gs_port *port;
  84. int c, total = 0;
  85. int t;
  86. func_enter ();
  87. if (!tty) return 0;
  88. port = tty->driver;
  89. if (!port) return 0;
  90. if (! (port->flags & ASYNC_INITIALIZED))
  91. return 0;
  92. /* get exclusive "write" access to this port (problem 3) */
  93. /* This is not a spinlock because we can have a disk access (page 
  94.  fault) in copy_from_user */
  95. down (& port->port_write_sem);
  96. while (1) {
  97. c = count;
  98.  
  99. /* This is safe because we "OWN" the "head". Noone else can 
  100.    change the "head": we own the port_write_sem. */
  101. /* Don't overrun the end of the buffer */
  102. t = SERIAL_XMIT_SIZE - port->xmit_head;
  103. if (t < c) c = t;
  104.  
  105. /* This is safe because the xmit_cnt can only decrease. This 
  106.    would increase "t", so we might copy too little chars. */
  107. /* Don't copy past the "head" of the buffer */
  108. t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
  109. if (t < c) c = t;
  110.  
  111. /* Can't copy more? break out! */
  112. if (c <= 0) break;
  113. if (from_user)
  114.                        if (copy_from_user (port->xmit_buf + port->xmit_head, 
  115.                                            buf, c)) {
  116.                                up (& port->port_write_sem);
  117.                                return -EFAULT;
  118.                        }
  119. else
  120. memcpy         (port->xmit_buf + port->xmit_head, buf, c);
  121. port -> xmit_cnt += c;
  122. port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
  123. buf += c;
  124. count -= c;
  125. total += c;
  126. }
  127. up (& port->port_write_sem);
  128. gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %sn", 
  129.             (port->flags & GS_TX_INTEN)?"enabled": "disabled"); 
  130. if (port->xmit_cnt && 
  131.     !tty->stopped && 
  132.     !tty->hw_stopped &&
  133.     !(port->flags & GS_TX_INTEN)) {
  134. port->flags |= GS_TX_INTEN;
  135. port->rd->enable_tx_interrupts (port);
  136. }
  137. func_exit ();
  138. return total;
  139. }
  140. #else
  141. /*
  142. > Problems to take into account are:
  143. >       -1- Interrupts that empty part of the buffer.
  144. >       -2- page faults on the access to userspace. 
  145. >       -3- Other processes that are also trying to do a "write". 
  146. */
  147. int gs_write(struct tty_struct * tty, int from_user, 
  148.                     const unsigned char *buf, int count)
  149. {
  150. struct gs_port *port;
  151. int c, total = 0;
  152. int t;
  153. unsigned long flags;
  154. func_enter ();
  155. /* The standard serial driver returns 0 in this case. 
  156.    That sounds to me as "No error, I just didn't get to writing any
  157.    bytes. Feel free to try again." 
  158.    The "official" way to write n bytes from buf is:
  159.  for (nwritten = 0;nwritten < n;nwritten += rv) {
  160.  rv = write (fd, buf+nwritten, n-nwritten);
  161.  if (rv < 0) break; // Error: bail out. //
  162.  } 
  163.    which will loop endlessly in this case. The manual page for write
  164.    agrees with me. In practise almost everybody writes 
  165.    "write (fd, buf,n);" but some people might have had to deal with 
  166.    incomplete writes in the past and correctly implemented it by now... 
  167.  */
  168. if (!tty) return -EIO;
  169. port = tty->driver_data;
  170. if (!port || !port->xmit_buf || !tmp_buf)
  171. return -EIO;
  172. save_flags(flags);
  173. if (from_user) {
  174. down(&tmp_buf_sem);
  175. while (1) {
  176. c = count;
  177. /* Note: This part can be done without
  178.  * interrupt routine protection since
  179.  * the interrupt routines may only modify
  180.  * shared variables in safe ways, in the worst
  181.  * case causing us to loop twice in the code
  182.  * below. See comments below. */ 
  183. /* Don't overrun the end of the buffer */
  184. t = SERIAL_XMIT_SIZE - port->xmit_head;
  185. if (t < c) c = t;
  186.  
  187. /* This is safe because the xmit_cnt can only decrease. This 
  188.    would increase "t", so we might copy too little chars. */
  189. /* Don't copy past the "head" of the buffer */
  190. t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
  191. if (t < c) c = t;  
  192. /* Can't copy more? break out! */
  193. if (c <= 0) break;
  194. c -= copy_from_user(tmp_buf, buf, c);
  195. if (!c) {
  196. if (!total)
  197. total = -EFAULT;
  198. break;
  199. }
  200. cli();
  201. t = SERIAL_XMIT_SIZE - port->xmit_head;
  202. if (t < c) c = t;
  203. t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
  204. if (t < c) c = t;
  205. memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
  206. port->xmit_head = ((port->xmit_head + c) &
  207.                    (SERIAL_XMIT_SIZE-1));
  208. port->xmit_cnt += c;
  209. restore_flags(flags);
  210. buf += c;
  211. count -= c;
  212. total += c;
  213. }
  214. up(&tmp_buf_sem);
  215. } else {
  216. while (1) {
  217. cli();
  218. c = count;
  219. /* This is safe because we "OWN" the "head". Noone else can 
  220.    change the "head": we own the port_write_sem. */
  221. /* Don't overrun the end of the buffer */
  222. t = SERIAL_XMIT_SIZE - port->xmit_head;
  223. if (t < c) c = t;
  224.  
  225. /* This is safe because the xmit_cnt can only decrease. This 
  226.    would increase "t", so we might copy too little chars. */
  227. /* Don't copy past the "head" of the buffer */
  228. t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
  229. if (t < c) c = t;
  230.  
  231. /* Can't copy more? break out! */
  232. if (c <= 0) {
  233. restore_flags(flags);
  234. break;
  235. }
  236. memcpy(port->xmit_buf + port->xmit_head, buf, c);
  237. port->xmit_head = ((port->xmit_head + c) &
  238.                    (SERIAL_XMIT_SIZE-1));
  239. port->xmit_cnt += c;
  240. restore_flags(flags);
  241. buf += c;
  242. count -= c;
  243. total += c;
  244. }
  245. }
  246. if (port->xmit_cnt && 
  247.     !tty->stopped && 
  248.     !tty->hw_stopped &&
  249.     !(port->flags & GS_TX_INTEN)) {
  250. port->flags |= GS_TX_INTEN;
  251. port->rd->enable_tx_interrupts (port);
  252. }
  253. func_exit ();
  254. return total;
  255. }
  256. #endif
  257. int gs_write_room(struct tty_struct * tty)
  258. {
  259. struct gs_port *port = tty->driver_data;
  260. int ret;
  261. func_enter ();
  262. ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  263. if (ret < 0)
  264. ret = 0;
  265. func_exit ();
  266. return ret;
  267. }
  268. int gs_chars_in_buffer(struct tty_struct *tty)
  269. {
  270. struct gs_port *port = tty->driver_data;
  271. func_enter ();
  272. func_exit ();
  273. return port->xmit_cnt;
  274. }
  275. int gs_real_chars_in_buffer(struct tty_struct *tty)
  276. {
  277. struct gs_port *port;
  278. func_enter ();
  279. if (!tty) return 0;
  280. port = tty->driver_data;
  281. if (!port->rd) return 0;
  282. if (!port->rd->chars_in_buffer) return 0;
  283. func_exit ();
  284. return port->xmit_cnt + port->rd->chars_in_buffer (port);
  285. }
  286. static int gs_wait_tx_flushed (void * ptr, int timeout) 
  287. {
  288. struct gs_port *port = ptr;
  289. long end_jiffies;
  290. int jiffies_to_transmit, charsleft = 0, rv = 0;
  291. int rcib;
  292. func_enter();
  293. gs_dprintk (GS_DEBUG_FLUSH, "port=%p.n", port);
  294. if (port) {
  295. gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.n", 
  296. port->xmit_cnt, port->xmit_buf, port->tty);
  297. }
  298. if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
  299. gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.n");
  300. func_exit();
  301. return -EINVAL;  /* This is an error which we don't know how to handle. */
  302. }
  303. rcib = gs_real_chars_in_buffer(port->tty);
  304. if(rcib <= 0) {
  305. gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.n");
  306. func_exit();
  307. return rv;
  308. }
  309. /* stop trying: now + twice the time it would normally take +  seconds */
  310. if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
  311. end_jiffies  = jiffies; 
  312. if (timeout !=  MAX_SCHEDULE_TIMEOUT)
  313. end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
  314. end_jiffies += timeout;
  315. gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).n", 
  316.     jiffies, end_jiffies, end_jiffies-jiffies); 
  317. /* the expression is actually jiffies < end_jiffies, but that won't
  318.    work around the wraparound. Tricky eh? */
  319. while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
  320.         time_after (end_jiffies, jiffies)) {
  321. /* Units check: 
  322.    chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
  323.    check! */
  324. charsleft += 16; /* Allow 16 chars more to be transmitted ... */
  325. jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
  326. /*                                ^^^ Round up.... */
  327. if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
  328. gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
  329.     "(%d chars).n", jiffies_to_transmit, charsleft); 
  330. set_current_state (TASK_INTERRUPTIBLE);
  331. schedule_timeout(jiffies_to_transmit);
  332. if (signal_pending (current)) {
  333. gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: "); 
  334. rv = -EINTR;
  335. break;
  336. }
  337. }
  338. gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.n", charsleft); 
  339. set_current_state (TASK_RUNNING);
  340. func_exit();
  341. return rv;
  342. }
  343. void gs_flush_buffer(struct tty_struct *tty)
  344. {
  345. struct gs_port *port;
  346. unsigned long flags;
  347. func_enter ();
  348. if (!tty) return;
  349. port = tty->driver_data;
  350. if (!port) return;
  351. /* XXX Would the write semaphore do? */
  352. save_flags(flags); cli();
  353. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  354. restore_flags(flags);
  355. wake_up_interruptible(&tty->write_wait);
  356. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  357.     tty->ldisc.write_wakeup)
  358. (tty->ldisc.write_wakeup)(tty);
  359. func_exit ();
  360. }
  361. void gs_flush_chars(struct tty_struct * tty)
  362. {
  363. struct gs_port *port;
  364. func_enter ();
  365. if (!tty) return;
  366. port = tty->driver_data;
  367. if (!port) return;
  368. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  369.     !port->xmit_buf) {
  370. func_exit ();
  371. return;
  372. }
  373. /* Beats me -- REW */
  374. port->flags |= GS_TX_INTEN;
  375. port->rd->enable_tx_interrupts (port);
  376. func_exit ();
  377. }
  378. void gs_stop(struct tty_struct * tty)
  379. {
  380. struct gs_port *port;
  381. func_enter ();
  382. if (!tty) return;
  383. port = tty->driver_data;
  384. if (!port) return;
  385. if (port->xmit_cnt && 
  386.     port->xmit_buf && 
  387.     (port->flags & GS_TX_INTEN) ) {
  388. port->flags &= ~GS_TX_INTEN;
  389. port->rd->disable_tx_interrupts (port);
  390. }
  391. func_exit ();
  392. }
  393. void gs_start(struct tty_struct * tty)
  394. {
  395. struct gs_port *port;
  396. if (!tty) return;
  397. port = tty->driver_data;
  398. if (!port) return;
  399. if (port->xmit_cnt && 
  400.     port->xmit_buf && 
  401.     !(port->flags & GS_TX_INTEN) ) {
  402. port->flags |= GS_TX_INTEN;
  403. port->rd->enable_tx_interrupts (port);
  404. }
  405. func_exit ();
  406. }
  407. void gs_shutdown_port (struct gs_port *port)
  408. {
  409. unsigned long flags;
  410. func_enter();
  411. if (!port) return;
  412. if (!(port->flags & ASYNC_INITIALIZED))
  413. return;
  414. save_flags (flags);
  415. cli ();
  416. if (port->xmit_buf) {
  417. free_page((unsigned long) port->xmit_buf);
  418. port->xmit_buf = 0;
  419. }
  420. if (port->tty)
  421. set_bit(TTY_IO_ERROR, &port->tty->flags);
  422. port->rd->shutdown_port (port);
  423. port->flags &= ~ASYNC_INITIALIZED;
  424. restore_flags (flags);
  425. func_exit();
  426. }
  427. void gs_hangup(struct tty_struct *tty)
  428. {
  429. struct gs_port   *port;
  430. func_enter ();
  431. if (!tty) return;
  432. port = tty->driver_data;
  433. tty = port->tty;
  434. if (!tty) 
  435. return;
  436. gs_shutdown_port (port);
  437. port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE |GS_ACTIVE);
  438. port->tty = NULL;
  439. port->count = 0;
  440. wake_up_interruptible(&port->open_wait);
  441. func_exit ();
  442. }
  443. void gs_do_softint(void *private_)
  444. {
  445. struct gs_port *port = private_;
  446. struct tty_struct *tty;
  447. func_enter ();
  448. if (!port) return;
  449. tty = port->tty;
  450. if (!tty) return;
  451. if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
  452. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  453.     tty->ldisc.write_wakeup)
  454. (tty->ldisc.write_wakeup)(tty);
  455. wake_up_interruptible(&tty->write_wait);
  456. }
  457. func_exit ();
  458. }
  459. int gs_block_til_ready(void *port_, struct file * filp)
  460. {
  461. struct gs_port *port = port_;
  462. DECLARE_WAITQUEUE(wait, current);
  463. int    retval;
  464. int    do_clocal = 0;
  465. int    CD;
  466. struct tty_struct *tty;
  467. unsigned long flags;
  468. func_enter ();
  469. if (!port) return 0;
  470. tty = port->tty;
  471. if (!tty) return 0;
  472. gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.n"); 
  473. /*
  474.  * If the device is in the middle of being closed, then block
  475.  * until it's done, and then try again.
  476.  */
  477. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  478. interruptible_sleep_on(&port->close_wait);
  479. if (port->flags & ASYNC_HUP_NOTIFY)
  480. return -EAGAIN;
  481. else
  482. return -ERESTARTSYS;
  483. }
  484. gs_dprintk (GS_DEBUG_BTR, "after hung upn"); 
  485. /*
  486.  * If this is a callout device, then just make sure the normal
  487.  * device isn't being used.
  488.  */
  489. if (tty->driver.subtype == GS_TYPE_CALLOUT) {
  490. if (port->flags & ASYNC_NORMAL_ACTIVE)
  491. return -EBUSY;
  492. if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
  493.     (port->flags & ASYNC_SESSION_LOCKOUT) &&
  494.     (port->session != current->session))
  495. return -EBUSY;
  496. if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
  497.     (port->flags & ASYNC_PGRP_LOCKOUT) &&
  498.     (port->pgrp != current->pgrp))
  499. return -EBUSY;
  500. port->flags |= ASYNC_CALLOUT_ACTIVE;
  501. return 0;
  502. }
  503. gs_dprintk (GS_DEBUG_BTR, "after subtypen");
  504. /*
  505.  * If non-blocking mode is set, or the port is not enabled,
  506.  * then make the check up front and then exit.
  507.  */
  508. if ((filp->f_flags & O_NONBLOCK) ||
  509.     (tty->flags & (1 << TTY_IO_ERROR))) {
  510. if (port->flags & ASYNC_CALLOUT_ACTIVE)
  511. return -EBUSY;
  512. port->flags |= ASYNC_NORMAL_ACTIVE;
  513. return 0;
  514. }
  515. gs_dprintk (GS_DEBUG_BTR, "after nonblockn"); 
  516.  
  517. if (port->flags & ASYNC_CALLOUT_ACTIVE) {
  518. if (port->normal_termios.c_cflag & CLOCAL) 
  519. do_clocal = 1;
  520. } else {
  521. if (C_CLOCAL(tty))
  522. do_clocal = 1;
  523. }
  524. /*
  525.  * Block waiting for the carrier detect and the line to become
  526.  * free (i.e., not in use by the callout).  While we are in
  527.  * this loop, port->count is dropped by one, so that
  528.  * rs_close() knows when to free things.  We restore it upon
  529.  * exit, either normal or abnormal.
  530.  */
  531. retval = 0;
  532. add_wait_queue(&port->open_wait, &wait);
  533. gs_dprintk (GS_DEBUG_BTR, "after add waitq.n"); 
  534. save_flags(flags);
  535. cli();
  536. if (!tty_hung_up_p(filp))
  537. port->count--;
  538. restore_flags(flags);
  539. port->blocked_open++;
  540. while (1) {
  541. CD = port->rd->get_CD (port);
  542. gs_dprintk (GS_DEBUG_BTR, "CD is now %d.n", CD);
  543. set_current_state (TASK_INTERRUPTIBLE);
  544. if (tty_hung_up_p(filp) ||
  545.     !(port->flags & ASYNC_INITIALIZED)) {
  546. if (port->flags & ASYNC_HUP_NOTIFY)
  547. retval = -EAGAIN;
  548. else
  549. retval = -ERESTARTSYS;
  550. break;
  551. }
  552. if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
  553.     !(port->flags & ASYNC_CLOSING) &&
  554.     (do_clocal || CD))
  555. break;
  556. gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)n", 
  557. (int)signal_pending (current), *(long*)(&current->blocked)); 
  558. if (signal_pending(current)) {
  559. retval = -ERESTARTSYS;
  560. break;
  561. }
  562. schedule();
  563. }
  564. gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)n",
  565.     port->blocked_open);
  566. set_current_state (TASK_RUNNING);
  567. remove_wait_queue(&port->open_wait, &wait);
  568. if (!tty_hung_up_p(filp))
  569. port->count++;
  570. port->blocked_open--;
  571. if (retval)
  572. return retval;
  573. port->flags |= ASYNC_NORMAL_ACTIVE;
  574. func_exit ();
  575. return 0;
  576. }  
  577. void gs_close(struct tty_struct * tty, struct file * filp)
  578. {
  579. unsigned long flags;
  580. struct gs_port *port;
  581. func_enter ();
  582. if (!tty) return;
  583. port = (struct gs_port *) tty->driver_data;
  584. if (!port) return;
  585. if (!port->tty) {
  586. /* This seems to happen when this is called from vhangup. */
  587. gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULLn");
  588. port->tty = tty;
  589. }
  590. save_flags(flags); cli();
  591. if (tty_hung_up_p(filp)) {
  592. restore_flags(flags);
  593. port->rd->hungup (port);
  594. func_exit ();
  595. return;
  596. }
  597. if ((tty->count == 1) && (port->count != 1)) {
  598. printk(KERN_ERR "gs: gs_close: bad port count;"
  599.        " tty->count is 1, port count is %dn", port->count);
  600. port->count = 1;
  601. }
  602. if (--port->count < 0) {
  603. printk(KERN_ERR "gs: gs_close: bad port count: %dn", port->count);
  604. port->count = 0;
  605. }
  606. if (port->count) {
  607. gs_dprintk(GS_DEBUG_CLOSE, "gs_close: count: %dn", port->count);
  608. restore_flags(flags);
  609. func_exit ();
  610. return;
  611. }
  612. port->flags |= ASYNC_CLOSING;
  613. /*
  614.  * Save the termios structure, since this port may have
  615.  * separate termios for callout and dialin.
  616.  */
  617. if (port->flags & ASYNC_NORMAL_ACTIVE)
  618. port->normal_termios = *tty->termios;
  619. if (port->flags & ASYNC_CALLOUT_ACTIVE)
  620. port->callout_termios = *tty->termios;
  621. /*
  622.  * Now we wait for the transmit buffer to clear; and we notify 
  623.  * the line discipline to only process XON/XOFF characters.
  624.  */
  625. tty->closing = 1;
  626. /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  627.    tty_wait_until_sent(tty, port->closing_wait); */
  628. /*
  629.  * At this point we stop accepting input.  To do this, we
  630.  * disable the receive line status interrupts, and tell the
  631.  * interrupt driver to stop checking the data ready bit in the
  632.  * line status register.
  633.  */
  634. port->rd->disable_rx_interrupts (port);
  635. /* close has no way of returning "EINTR", so discard return value */
  636. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  637. gs_wait_tx_flushed (port, port->closing_wait); 
  638. port->flags &= ~GS_ACTIVE;
  639. if (tty->driver.flush_buffer)
  640. tty->driver.flush_buffer(tty);
  641. if (tty->ldisc.flush_buffer)
  642. tty->ldisc.flush_buffer(tty);
  643. tty->closing = 0;
  644. port->event = 0;
  645. port->rd->close (port);
  646. port->rd->shutdown_port (port);
  647. port->tty = 0;
  648. if (port->blocked_open) {
  649. if (port->close_delay) {
  650. set_current_state (TASK_INTERRUPTIBLE);
  651. schedule_timeout(port->close_delay);
  652. }
  653. wake_up_interruptible(&port->open_wait);
  654. }
  655. port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
  656.                  ASYNC_CLOSING | ASYNC_INITIALIZED);
  657. wake_up_interruptible(&port->close_wait);
  658. restore_flags(flags);
  659. func_exit ();
  660. }
  661. static unsigned int     gs_baudrates[] = {
  662.   0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  663.   9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
  664. };
  665. void gs_set_termios (struct tty_struct * tty, 
  666.                      struct termios * old_termios)
  667. {
  668. struct gs_port *port;
  669. int baudrate, tmp, rv;
  670. struct termios *tiosp;
  671. func_enter();
  672. if (!tty) return;
  673. port = tty->driver_data;
  674. if (!port) return;
  675. tiosp = tty->termios;
  676. if (gs_debug & GS_DEBUG_TERMIOS) {
  677. gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):n", tiosp);
  678. }
  679. #if 0
  680. /* This is an optimization that is only allowed for dumb cards */
  681. /* Smart cards require knowledge of iflags and oflags too: that 
  682.    might change hardware cooking mode.... */
  683. #endif
  684. if (old_termios) {
  685. if(   (tiosp->c_iflag == old_termios->c_iflag)
  686.    && (tiosp->c_oflag == old_termios->c_oflag)
  687.    && (tiosp->c_cflag == old_termios->c_cflag)
  688.    && (tiosp->c_lflag == old_termios->c_lflag)
  689.    && (tiosp->c_line  == old_termios->c_line)
  690.    && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
  691. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized awayn");
  692. return /* 0 */;
  693. }
  694. } else 
  695. gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
  696.            "no optimizationn");
  697. if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
  698. if(tiosp->c_iflag != old_termios->c_iflag)  printk("c_iflag changedn");
  699. if(tiosp->c_oflag != old_termios->c_oflag)  printk("c_oflag changedn");
  700. if(tiosp->c_cflag != old_termios->c_cflag)  printk("c_cflag changedn");
  701. if(tiosp->c_lflag != old_termios->c_lflag)  printk("c_lflag changedn");
  702. if(tiosp->c_line  != old_termios->c_line)   printk("c_line changedn");
  703. if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changedn");
  704. }
  705. baudrate = tiosp->c_cflag & CBAUD;
  706. if (baudrate & CBAUDEX) {
  707. baudrate &= ~CBAUDEX;
  708. if ((baudrate < 1) || (baudrate > 4))
  709. tiosp->c_cflag &= ~CBAUDEX;
  710. else
  711. baudrate += 15;
  712. }
  713. baudrate = gs_baudrates[baudrate];
  714. if ((tiosp->c_cflag & CBAUD) == B38400) {
  715. if (     (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  716. baudrate = 57600;
  717. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  718. baudrate = 115200;
  719. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  720. baudrate = 230400;
  721. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  722. baudrate = 460800;
  723. else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  724. baudrate = (port->baud_base / port->custom_divisor);
  725. }
  726. /* I recommend using THIS instead of the mess in termios (and
  727.    duplicating the above code). Next we should create a clean
  728.    interface towards this variable. If your card supports arbitrary
  729.    baud rates, (e.g. CD1400 or 16550 based cards) then everything
  730.    will be very easy..... */
  731. port->baud = baudrate;
  732. /* Two timer ticks seems enough to wakeup something like SLIP driver */
  733. /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
  734. tmp = (baudrate / 10 / HZ) * 2;  
  735. if (tmp <                 0) tmp = 0;
  736. if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
  737. port->wakeup_chars = tmp;
  738. /* We should really wait for the characters to be all sent before
  739.    changing the settings. -- CAL */
  740. rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
  741. if (rv < 0) return /* rv */;
  742. rv = port->rd->set_real_termios(port);
  743. if (rv < 0) return /* rv */;
  744. if ((!old_termios || 
  745.      (old_termios->c_cflag & CRTSCTS)) &&
  746.     !(      tiosp->c_cflag & CRTSCTS)) {
  747. tty->stopped = 0;
  748. gs_start(tty);
  749. }
  750. #ifdef tytso_patch_94Nov25_1726
  751. /* This "makes sense", Why is it commented out? */
  752. if (!(old_termios->c_cflag & CLOCAL) &&
  753.     (tty->termios->c_cflag & CLOCAL))
  754. wake_up_interruptible(&info->open_wait);
  755. #endif
  756. func_exit();
  757. return /* 0 */;
  758. }
  759. /* Must be called with interrupts enabled */
  760. int gs_init_port(struct gs_port *port)
  761. {
  762. unsigned long flags;
  763. unsigned long page;
  764. save_flags (flags);
  765. if (!tmp_buf) {
  766. page = get_free_page(GFP_KERNEL);
  767. cli (); /* Don't expect this to make a difference. */ 
  768. if (tmp_buf)
  769. free_page(page);
  770. else
  771. tmp_buf = (unsigned char *) page;
  772. restore_flags (flags);
  773. if (!tmp_buf) {
  774. return -ENOMEM;
  775. }
  776. }
  777. if (port->flags & ASYNC_INITIALIZED)
  778. return 0;
  779. if (!port->xmit_buf) {
  780. /* We may sleep in get_free_page() */
  781. unsigned long tmp;
  782. tmp = get_free_page(GFP_KERNEL);
  783. /* Spinlock? */
  784. cli ();
  785. if (port->xmit_buf) 
  786. free_page (tmp);
  787. else
  788. port->xmit_buf = (unsigned char *) tmp;
  789. restore_flags (flags);
  790. if (!port->xmit_buf)
  791. return -ENOMEM;
  792. }
  793. cli();
  794. if (port->tty) 
  795. clear_bit(TTY_IO_ERROR, &port->tty->flags);
  796. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  797. gs_set_termios(port->tty, NULL);
  798. port->flags |= ASYNC_INITIALIZED;
  799. port->flags &= ~GS_TX_INTEN;
  800. restore_flags(flags);
  801. return 0;
  802. }
  803. int gs_setserial(struct gs_port *port, struct serial_struct *sp)
  804. {
  805. struct serial_struct sio;
  806. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  807. return(-EFAULT);
  808. if (!capable(CAP_SYS_ADMIN)) {
  809. if ((sio.baud_base != port->baud_base) ||
  810.     (sio.close_delay != port->close_delay) ||
  811.     ((sio.flags & ~ASYNC_USR_MASK) !=
  812.      (port->flags & ~ASYNC_USR_MASK)))
  813. return(-EPERM);
  814. port->flags = (port->flags & ~ASYNC_USR_MASK) |
  815. (sio.flags & ASYNC_USR_MASK);
  816.   
  817. port->baud_base = sio.baud_base;
  818. port->close_delay = sio.close_delay;
  819. port->closing_wait = sio.closing_wait;
  820. port->custom_divisor = sio.custom_divisor;
  821. gs_set_termios (port->tty, NULL);
  822. return 0;
  823. }
  824. /*****************************************************************************/
  825. /*
  826.  *      Generate the serial struct info.
  827.  */
  828. int gs_getserial(struct gs_port *port, struct serial_struct *sp)
  829. {
  830. struct serial_struct    sio;
  831. memset(&sio, 0, sizeof(struct serial_struct));
  832. sio.flags = port->flags;
  833. sio.baud_base = port->baud_base;
  834. sio.close_delay = port->close_delay;
  835. sio.closing_wait = port->closing_wait;
  836. sio.custom_divisor = port->custom_divisor;
  837. sio.hub6 = 0;
  838. /* If you want you can override these. */
  839. sio.type = PORT_UNKNOWN;
  840. sio.xmit_fifo_size = -1;
  841. sio.line = -1;
  842. sio.port = -1;
  843. sio.irq = -1;
  844. if (port->rd->getserial)
  845. port->rd->getserial (port, &sio);
  846. if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
  847. return -EFAULT;
  848. return 0;
  849. }
  850. void gs_got_break(struct gs_port *port)
  851. {
  852. if (port->flags & ASYNC_SAK) {
  853. do_SAK (port->tty);
  854. }
  855. *(port->tty->flip.flag_buf_ptr) = TTY_BREAK;
  856. port->tty->flip.flag_buf_ptr++;
  857. port->tty->flip.char_buf_ptr++;
  858. port->tty->flip.count++;
  859. }
  860. EXPORT_SYMBOL(gs_put_char);
  861. EXPORT_SYMBOL(gs_write);
  862. EXPORT_SYMBOL(gs_write_room);
  863. EXPORT_SYMBOL(gs_chars_in_buffer);
  864. EXPORT_SYMBOL(gs_flush_buffer);
  865. EXPORT_SYMBOL(gs_flush_chars);
  866. EXPORT_SYMBOL(gs_stop);
  867. EXPORT_SYMBOL(gs_start);
  868. EXPORT_SYMBOL(gs_hangup);
  869. EXPORT_SYMBOL(gs_do_softint);
  870. EXPORT_SYMBOL(gs_block_til_ready);
  871. EXPORT_SYMBOL(gs_close);
  872. EXPORT_SYMBOL(gs_set_termios);
  873. EXPORT_SYMBOL(gs_init_port);
  874. EXPORT_SYMBOL(gs_setserial);
  875. EXPORT_SYMBOL(gs_getserial);
  876. EXPORT_SYMBOL(gs_got_break);
  877. MODULE_LICENSE("GPL");