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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  drivers/char/serial_txx927.c
  3.  *  driver for TX[34]927 SIO
  4.  *
  5.  * Copyright 2001 MontaVista Software Inc.
  6.  * Author: MontaVista Software, Inc. 
  7.  *                ahennessy@mvista.com
  8.  *
  9.  * Based on drivers/char/serial.c
  10.  *
  11.  * Copyright (C) 2000-2001 Toshiba Corporation
  12.  *
  13.  * This program is free software; you can redistribute  it and/or modify it
  14.  * under  the terms of  the GNU General  Public License as published by the
  15.  * Free Software Foundation;  either version 2 of the  License, or (at your
  16.  * option) any later version.
  17.  *
  18.  * THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
  19.  * WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
  21.  * NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
  24.  * USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  *
  29.  * You should have received a copy of the  GNU General Public License along
  30.  * with this program; if not, write  to the Free Software Foundation, Inc.,
  31.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  32.  */
  33. #define SERIAL_DO_RESTART
  34. /* Set of debugging defines */
  35. #undef SERIAL_DEBUG_INTR
  36. #undef SERIAL_DEBUG_OPEN
  37. #undef SERIAL_DEBUG_FLOW
  38. #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  39. #undef SERIAL_DEBUG_PCI
  40. #undef SERIAL_DEBUG_AUTOCONF   
  41. #ifdef MODULE
  42. #undef CONFIG_TXX927_SERIAL_CONSOLE
  43. #endif                               
  44. #define CONFIG_SERIAL_RSA
  45. #define RS_STROBE_TIME (10*HZ)
  46. #define RS_ISR_PASS_LIMIT 256     
  47. /*
  48.  * End of serial driver configuration section.
  49.  */
  50. #include <linux/module.h>    
  51. #include <linux/config.h>
  52. #include <linux/module.h>
  53. #include <linux/errno.h>
  54. #include <linux/signal.h>
  55. #include <linux/sched.h>
  56. #include <linux/timer.h>
  57. #include <linux/interrupt.h>
  58. #include <linux/tty.h>
  59. #include <linux/tty_flip.h>
  60. #include <linux/serial.h>
  61. #include <linux/circ_buf.h>
  62. #include <linux/serial_reg.h>
  63. #include <linux/major.h>
  64. #include <linux/string.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/ptrace.h>
  67. #include <linux/ioport.h>
  68. #include <linux/mm.h>
  69. #include <linux/slab.h>
  70. #include <linux/init.h>
  71. #include <linux/serialP.h>
  72. #include <linux/delay.h>
  73. #ifdef CONFIG_TXX927_SERIAL_CONSOLE
  74. #include <linux/console.h>
  75. #endif
  76. #ifdef CONFIG_MAGIC_SYSRQ
  77. #include <linux/sysrq.h>
  78. #endif            
  79. #include <asm/system.h>
  80. #include <asm/serial.h>
  81. #include <asm/io.h>
  82. #include <asm/irq.h>
  83. #include <asm/uaccess.h>
  84. #include <asm/bitops.h>
  85. #include <asm/jmr3927/txx927.h>
  86. #include <asm/bootinfo.h>
  87. #ifdef CONFIG_TOSHIBA_JMR3927
  88. #include <asm/jmr3927/jmr3927.h>
  89. #endif
  90. #define _INLINE_ inline
  91. #ifdef CONFIG_MAC_SERIAL
  92. #define SERIAL_DEV_OFFSET 2
  93. #else
  94. #define SERIAL_DEV_OFFSET 0
  95. #endif
  96. static char *serial_name = "TXx927 Serial driver";
  97. static char *serial_version = "0.02";
  98. static DECLARE_TASK_QUEUE(tq_serial);
  99. static struct tty_driver serial_driver, callout_driver;
  100. static int serial_refcount;
  101. static struct timer_list serial_timer;
  102. extern unsigned long get_txx927_uart_baud(void);
  103. /* serial subtype definitions */
  104. #ifndef SERIAL_TYPE_NORMAL
  105. #define SERIAL_TYPE_NORMAL      1
  106. #define SERIAL_TYPE_CALLOUT     2
  107. #endif                          
  108. /* number of characters left in xmit buffer before we ask for more */
  109. #define WAKEUP_CHARS 256
  110. /*
  111.  * IRQ_timeout - How long the timeout should be for each IRQ
  112.  *  should be after the IRQ has been active.
  113.  */
  114. static struct async_struct *IRQ_ports[NR_IRQS];
  115. static int IRQ_timeout[NR_IRQS];
  116. #ifdef CONFIG_TXX927_SERIAL_CONSOLE
  117. static struct console sercons;
  118. #endif
  119. #if defined(CONFIG_TXX927_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  120. static unsigned long break_pressed; /* break, really ... */
  121. #endif                                                        
  122. static void change_speed(struct async_struct *info, struct termios *old);
  123. static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
  124. #ifndef PREPARE_FUNC
  125. #define PREPARE_FUNC(dev)  (dev->prepare)
  126. #define ACTIVATE_FUNC(dev)  (dev->activate)
  127. #define DEACTIVATE_FUNC(dev)  (dev->deactivate)
  128. #endif
  129. #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
  130. #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
  131. #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %sn", 
  132.  kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
  133. #else
  134. #define DBG_CNT(s)
  135. #endif                                     
  136. #define SERIAL_DRIVER_NAME "TXx927SIO"
  137. #ifdef CONFIG_SERIAL
  138. /* "ttyS","cua" is used for standard serial driver */
  139. #define TXX927_TTY_NAME "ttySC"
  140. #define TXX927_TTY_MINOR_START (64 + 16) /* ttySC0(80), ttySC1(81) */
  141. #define TXX927_CU_NAME "cuac"
  142. #define TXX927_SERIAL_BH TXX927SERIAL_BH
  143. #else
  144. /* acts like standard serial driver */
  145. #define TXX927_TTY_NAME "ttyS"
  146. #define TXX927_TTY_MINOR_START 64
  147. #define TXX927_CU_NAME "cua"
  148. #define TXX927_SERIAL_BH SERIAL_BH
  149. #endif
  150. #define TXX927_TTY_MAJOR TTY_MAJOR
  151. #define TXX927_TTYAUX_MAJOR TTYAUX_MAJOR
  152. #define ASYNC_HAVE_CTS_LINE ASYNC_BOOT_AUTOCONF /* reuse */
  153. static struct serial_state rs_table[RS_TABLE_SIZE] = {
  154. SERIAL_PORT_DFNS /* Defined in serial.h */
  155. };
  156. #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
  157. static struct tty_struct *serial_table[NR_PORTS];
  158. static struct termios *serial_termios[NR_PORTS];
  159. static struct termios *serial_termios_locked[NR_PORTS];
  160. #ifndef MIN
  161. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  162. #endif
  163. /*
  164.  * tmp_buf is used as a temporary buffer by serial_write.  We need to
  165.  * lock it in case the copy_from_user blocks while swapping in a page,
  166.  * and some other program tries to do a serial write at the same time.
  167.  * Since the lock will only come under contention when the system is
  168.  * swapping and available memory is low, it makes sense to share one
  169.  * buffer across all the serial ports, since it significantly saves
  170.  * memory if large numbers of serial ports are open.
  171.  */
  172. static unsigned char *tmp_buf;
  173. #ifdef DECLARE_MUTEX
  174. static DECLARE_MUTEX(tmp_buf_sem);
  175. #else
  176. static struct semaphore tmp_buf_sem = MUTEX;
  177. #endif                                 
  178. static inline int serial_paranoia_check(struct async_struct *info,
  179. kdev_t device, const char *routine)
  180. {
  181. #ifdef SERIAL_PARANOIA_CHECK
  182. static const char *badmagic =
  183. "Warning: bad magic number for serial struct (%s) in %sn";
  184. static const char *badinfo =
  185. "Warning: null async_struct for (%s) in %sn";
  186. if (!info) {
  187. printk(badinfo, kdevname(device), routine);
  188. return 1;
  189. }
  190. if (info->magic != SERIAL_MAGIC) {
  191. printk(badmagic, kdevname(device), routine);
  192. return 1;
  193. }
  194. #endif
  195. return 0;
  196. }
  197. static inline struct txx927_sio_reg *sio_reg(struct async_struct *info)
  198. {
  199. return (struct txx927_sio_reg *)info->port;
  200. }
  201. /*
  202.  * Wait for transmitter & holding register to empty
  203.  */
  204. static inline void wait_for_xmitr(struct async_struct *info)
  205. {
  206. unsigned int tmout = 1000000;
  207. do {
  208. if (--tmout == 0) break;
  209. } while (!(sio_reg(info)->cisr & TXx927_SICISR_TXALS));
  210. }
  211. /*
  212.  * ------------------------------------------------------------
  213.  * rs_stop() and rs_start()
  214.  *
  215.  * This routines are called before setting or resetting tty->stopped.
  216.  * They enable or disable transmitter interrupts, as necessary.
  217.  * ------------------------------------------------------------
  218.  */
  219. static void rs_stop(struct tty_struct *tty)
  220. {
  221. struct async_struct *info = (struct async_struct *)tty->driver_data;
  222. unsigned long flags;
  223. if (serial_paranoia_check(info, tty->device, "rs_stop"))
  224. return;
  225. save_flags(flags); cli();
  226. if (info->IER & UART_IER_THRI) {
  227. info->IER &= ~UART_IER_THRI;
  228.         sio_reg(info)->dicr &= ~TXx927_SIDICR_TIE;
  229. }
  230. restore_flags(flags);
  231. }
  232. static void rs_start(struct tty_struct *tty)
  233. {
  234. struct async_struct *info = (struct async_struct *)tty->driver_data;
  235. unsigned long flags;
  236. if (serial_paranoia_check(info, tty->device, "rs_start"))
  237. return;
  238. save_flags(flags); cli();
  239. if (info->xmit.head != info->xmit.tail
  240.     && info->xmit.buf
  241.     && !(info->IER & UART_IER_THRI)) {
  242. info->IER |= UART_IER_THRI;
  243.         sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
  244. }
  245. restore_flags(flags);
  246. }
  247. /*
  248.  * ----------------------------------------------------------------------
  249.  *
  250.  * Here starts the interrupt handling routines.  All of the following
  251.  * subroutines are declared as inline and are folded into
  252.  * rs_interrupt().  They were separated out for readability's sake.
  253.  *
  254.  * Note: rs_interrupt() is a "fast" interrupt, which means that it
  255.  * runs with interrupts turned off.  People who may want to modify
  256.  * rs_interrupt() should try to keep the interrupt handler as fast as
  257.  * possible.  After you are done making modifications, it is not a bad
  258.  * idea to do:
  259.  * 
  260.  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  261.  *
  262.  * and look at the resulting assemble code in serial.s.
  263.  *
  264.  *  - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  265.  * -----------------------------------------------------------------------
  266.  */
  267. /*
  268.  * This routine is used by the interrupt handler to schedule
  269.  * processing in the software interrupt portion of the driver.
  270.  */
  271. static _INLINE_ void rs_sched_event(struct async_struct *info,
  272.   int event)
  273. {
  274. info->event |= 1 << event;
  275. queue_task(&info->tqueue, &tq_serial);
  276. mark_bh(TXX927_SERIAL_BH);
  277. }
  278. static _INLINE_ void receive_chars(struct async_struct *info,
  279.  int *status)
  280. {
  281. struct tty_struct *tty = info->tty;
  282. unsigned char ch;
  283. int ignored = 0;
  284. struct async_icount *icount;
  285. icount = &info->state->icount;
  286. do {
  287. ch = sio_reg(info)->rfifo;
  288. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  289. break;
  290. *tty->flip.char_buf_ptr = ch;
  291. icount->rx++;
  292. #ifdef SERIAL_DEBUG_INTR
  293. printk("DR%02x:%02x...", ch, *status);
  294. #endif
  295. *tty->flip.flag_buf_ptr = 0;
  296. if (*status & (TXx927_SIDISR_UBRK | TXx927_SIDISR_UPER |
  297.        TXx927_SIDISR_UFER | TXx927_SIDISR_UOER)) {
  298. /*
  299.  * For statistics only
  300.  */
  301. if (*status & TXx927_SIDISR_UBRK) {
  302. *status &= ~(TXx927_SIDISR_UFER | TXx927_SIDISR_UPER);
  303. icount->brk++;
  304. } else if (*status & TXx927_SIDISR_UPER)
  305. icount->parity++;
  306. else if (*status & TXx927_SIDISR_UFER)
  307. icount->frame++;
  308. if (*status & TXx927_SIDISR_UOER)
  309. icount->overrun++;
  310. /*
  311.  * Now check to see if character should be
  312.  * ignored, and mask off conditions which
  313.  * should be ignored.
  314.  */
  315. if (*status & info->ignore_status_mask) {
  316. if (++ignored > 100)
  317. break;
  318. goto ignore_char;
  319. }
  320. *status &= info->read_status_mask;
  321. if (*status & (TXx927_SIDISR_UBRK)) {
  322. #ifdef SERIAL_DEBUG_INTR
  323. printk("handling break....");
  324. #endif
  325. *tty->flip.flag_buf_ptr = TTY_BREAK;
  326. if (info->flags & ASYNC_SAK)
  327. do_SAK(tty);
  328. } else if (*status & TXx927_SIDISR_UPER)
  329. *tty->flip.flag_buf_ptr = TTY_PARITY;
  330. else if (*status & TXx927_SIDISR_UFER)
  331. *tty->flip.flag_buf_ptr = TTY_FRAME;
  332. if (*status & TXx927_SIDISR_UOER) {
  333. /*
  334.  * Overrun is special, since it's
  335.  * reported immediately, and doesn't
  336.  * affect the current character
  337.  */
  338. if (tty->flip.count < TTY_FLIPBUF_SIZE) {
  339. tty->flip.count++;
  340. tty->flip.flag_buf_ptr++;
  341. tty->flip.char_buf_ptr++;
  342. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  343. }
  344. }
  345. }
  346. tty->flip.flag_buf_ptr++;
  347. tty->flip.char_buf_ptr++;
  348. tty->flip.count++;
  349. ignore_char:
  350. *status = sio_reg(info)->disr;
  351. } while (!(*status & TXx927_SIDISR_UVALID));
  352. tty_flip_buffer_push(tty);
  353. }
  354. static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
  355. {
  356. int count;
  357. wait_for_xmitr(info);
  358. if (info->x_char) {
  359. sio_reg(info)->tfifo = info->x_char;
  360. info->state->icount.tx++;
  361. info->x_char = 0;
  362. if (intr_done)
  363. *intr_done = 0;
  364. return;
  365. }
  366.  
  367. if (info->xmit.head == info->xmit.tail
  368.     || info->tty->stopped
  369.     || info->tty->hw_stopped) {
  370. sio_reg(info)->dicr &= ~TXx927_SIDICR_TIE;
  371. return;
  372. }
  373. count = info->xmit_fifo_size;
  374. do {
  375. sio_reg(info)->tfifo = info->xmit.buf[info->xmit.tail++];
  376. info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
  377. info->state->icount.tx++;
  378. if (info->xmit.head == info->xmit.tail)
  379. break;
  380. } while (--count > 0);
  381. if (CIRC_CNT(info->xmit.head,
  382.      info->xmit.tail,
  383.      SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
  384. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  385. #ifdef SERIAL_DEBUG_INTR
  386. printk("THRE...");
  387. #endif
  388. if (intr_done)
  389. *intr_done = 0;
  390. if (info->xmit.head == info->xmit.tail) {
  391. sio_reg(info)->dicr &= ~TXx927_SIDICR_TIE;
  392. }
  393. }
  394. static _INLINE_ void check_modem_status(struct async_struct *info)
  395. {
  396. /* RTS/CTS are controled by HW. (if possible) */
  397. }
  398. /*
  399.  * This is the serial driver's interrupt routine for a single port
  400.  */
  401. static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
  402. {
  403. int status;
  404. int pass_counter = 0;
  405. struct async_struct * info;
  406. #ifdef SERIAL_DEBUG_INTR
  407. printk("rs_interrupt_single(%d)...", irq);
  408. #endif
  409. info = IRQ_ports[irq];
  410. if (!info || !info->tty)
  411. return;
  412. do {
  413. status = sio_reg(info)->disr;
  414. #ifdef SERIAL_DEBUG_INTR
  415. printk("status = %x...", status);
  416. #endif
  417. if (!(sio_reg(info)->dicr & TXx927_SIDICR_TIE))
  418. status &= ~TXx927_SIDISR_TDIS;
  419. if (!(status & (TXx927_SIDISR_TDIS | TXx927_SIDISR_RDIS | TXx927_SIDISR_TOUT)))
  420. break;
  421. if (status & TXx927_SIDISR_RDIS)
  422. receive_chars(info, &status);
  423. check_modem_status(info);
  424. if (status & TXx927_SIDISR_TDIS)
  425. transmit_chars(info, 0);
  426. /* Clear TX/RX Int. Status */
  427. sio_reg(info)->disr &= ~(TXx927_SIDISR_TDIS | TXx927_SIDISR_RDIS | TXx927_SIDISR_TOUT);
  428. if (pass_counter++ > RS_ISR_PASS_LIMIT) {
  429. #ifdef SERIAL_DEBUG_INTR
  430. printk("rs_single loop break.n");
  431. #endif
  432. break;
  433. }
  434. } while (1);
  435. info->last_active = jiffies;
  436. #ifdef SERIAL_DEBUG_INTR
  437. printk("end.n");
  438. #endif
  439. }
  440. /*
  441.  * -------------------------------------------------------------------
  442.  * Here ends the serial interrupt routines.
  443.  * -------------------------------------------------------------------
  444.  */
  445. /*
  446.  * This routine is used to handle the "bottom half" processing for the
  447.  * serial driver, known also the "software interrupt" processing.
  448.  * This processing is done at the kernel interrupt level, after the
  449.  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
  450.  * is where time-consuming activities which can not be done in the
  451.  * interrupt driver proper are done; the interrupt driver schedules
  452.  * them using rs_sched_event(), and they get done here.
  453.  */
  454. static void do_serial_bh(void)
  455. {
  456. run_task_queue(&tq_serial);
  457. }
  458. static void do_softint(void *private_)
  459. {
  460. struct async_struct *info = (struct async_struct *) private_;
  461. struct tty_struct *tty;
  462. tty = info->tty;
  463. if (!tty)
  464. return;
  465. if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
  466. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  467.     tty->ldisc.write_wakeup)
  468. (tty->ldisc.write_wakeup)(tty);
  469. wake_up_interruptible(&tty->write_wait);
  470. #ifdef SERIAL_HAVE_POLL_WAIT
  471. wake_up_interruptible(&tty->poll_wait);
  472. #endif                                                    
  473. }
  474. }
  475. /*
  476.  * This subroutine is called when the RS_TIMER goes off.  It is used
  477.  * by the serial driver to handle ports that do not have an interrupt
  478.  * (irq=0).  This doesn't work very well for 16450's, but gives barely
  479.  * passable results for a 16550A.  (Although at the expense of much
  480.  * CPU overhead).
  481.  */
  482. static void rs_timer(unsigned long dummy)
  483. {
  484. static unsigned long last_strobe;
  485. struct async_struct *info;
  486. unsigned int i;
  487. unsigned long flags;
  488. if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
  489. for (i=0; i < NR_IRQS; i++) {
  490. info = IRQ_ports[i];
  491. if (!info)
  492. continue;
  493. save_flags(flags); cli();
  494. rs_interrupt_single(i, NULL, NULL);
  495. restore_flags(flags);
  496. }
  497. }
  498. last_strobe = jiffies;
  499. mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
  500. #if 0
  501. if (IRQ_ports[0]) {
  502. save_flags(flags); cli();
  503. rs_interrupt_single(0, NULL, NULL);
  504. restore_flags(flags);
  505. mod_timer(&serial_timer, jiffies + IRQ_timeout[0]);
  506. }
  507. #endif
  508. }
  509. /*
  510.  * ---------------------------------------------------------------
  511.  * Low level utility subroutines for the serial driver:  routines to
  512.  * figure out the appropriate timeout for an interrupt chain, routines
  513.  * to initialize and startup a serial port, and routines to shutdown a
  514.  * serial port.  Useful stuff like that.
  515.  * ---------------------------------------------------------------
  516.  */
  517. /*
  518.  * This routine figures out the correct timeout for a particular IRQ.
  519.  * It uses the smallest timeout of all of the serial ports in a
  520.  * particular interrupt chain.  Now only used for IRQ 0....
  521.  */
  522. static void figure_IRQ_timeout(int irq)
  523. {
  524. struct async_struct *info;
  525. int timeout = 60*HZ; /* 60 seconds === a long time :-) */
  526. info = IRQ_ports[irq];
  527. if (!info) {
  528. IRQ_timeout[irq] = 60*HZ;
  529. return;
  530. }
  531. while (info) {
  532. if (info->timeout < timeout)
  533. timeout = info->timeout;
  534. info = info->next_port;
  535. }
  536. if (!irq)
  537. timeout = timeout / 2;
  538. IRQ_timeout[irq] = timeout ? timeout : 1;
  539. }
  540. static int startup(struct async_struct * info)
  541. {
  542. unsigned long flags;
  543. int retval=0;
  544. struct serial_state *state= info->state;
  545. unsigned long page;
  546. page = get_zeroed_page(GFP_KERNEL);
  547. if (!page)
  548. return -ENOMEM;
  549. save_flags(flags); cli();
  550. if (info->flags & ASYNC_INITIALIZED) {
  551. free_page(page);
  552. goto errout;
  553. }
  554. if (!state->port) {
  555. if (info->tty)
  556. set_bit(TTY_IO_ERROR, &info->tty->flags);
  557. free_page(page);
  558. goto errout;
  559. }
  560. if (info->xmit.buf)
  561. free_page(page);
  562. else
  563. info->xmit.buf = (unsigned char *) page;
  564. #ifdef SERIAL_DEBUG_OPEN
  565. printk("starting up ttys%d (irq %d)...", info->line, state->irq);
  566. #endif
  567. /*
  568.  * Clear the FIFO buffers and disable them
  569.  * (they will be reenabled in change_speed())
  570.  */
  571. sio_reg(info)->fcr |= TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
  572. TXx927_SIFCR_FRSTE;
  573. /* clear reset */
  574. sio_reg(info)->fcr &= ~(TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
  575. TXx927_SIFCR_FRSTE);
  576. /*
  577.  * Allocate the IRQ if necessary
  578.  */
  579. if (state->irq && (!IRQ_ports[state->irq] ||
  580.   !IRQ_ports[state->irq]->next_port)) {
  581. if (IRQ_ports[state->irq]) {
  582. retval = -EBUSY;
  583. goto errout;
  584. }
  585. retval = request_irq(state->irq, rs_interrupt_single,
  586.      SA_INTERRUPT,
  587.      "txx927serial", NULL);
  588. if (retval) {
  589. if (capable(CAP_SYS_ADMIN)) {
  590. if (info->tty)
  591. set_bit(TTY_IO_ERROR,
  592. &info->tty->flags);
  593. retval = 0;
  594. }
  595. goto errout;
  596. }
  597. }
  598. /*
  599.  * Insert serial port into IRQ chain.
  600.  */
  601. info->prev_port = 0;
  602. info->next_port = IRQ_ports[state->irq];
  603. if (info->next_port)
  604. info->next_port->prev_port = info;
  605. IRQ_ports[state->irq] = info;
  606. figure_IRQ_timeout(state->irq);
  607. /*
  608.  * Clear the interrupt registers.
  609.  */
  610. sio_reg(info)->disr = 0;
  611. /*
  612.  * Now, initialize the UART 
  613.  */
  614. /* HW RTS/CTS control */
  615. if (state->flags & ASYNC_HAVE_CTS_LINE)
  616. sio_reg(info)->flcr = TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES |
  617. TXx927_SIFLCR_RTSTL_MAX /* 15 */;
  618. /* Enable RX/TX */
  619. sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
  620. /*
  621.  * Finally, enable interrupts
  622.  */
  623. sio_reg(info)->dicr = TXx927_SIDICR_RIE;
  624. if (info->tty)
  625. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  626. info->xmit.head = info->xmit.tail = 0;
  627. /*
  628.  * Set up the tty->alt_speed kludge
  629.  */
  630. if (info->tty) {
  631. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  632. info->tty->alt_speed = 57600;
  633. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  634. info->tty->alt_speed = 115200;
  635. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  636. info->tty->alt_speed = 230400;
  637. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  638. info->tty->alt_speed = 460800;
  639. }
  640. /*
  641.  * and set the speed of the serial port
  642.  */
  643. change_speed(info, 0);
  644. info->flags |= ASYNC_INITIALIZED;
  645. restore_flags(flags);
  646. return 0;
  647. errout:
  648. restore_flags(flags);
  649. return retval;
  650. }
  651. /*
  652.  * This routine will shutdown a serial port; interrupts are disabled, and
  653.  * DTR is dropped if the hangup on close termio flag is on.
  654.  */
  655. static void shutdown(struct async_struct * info)
  656. {
  657. unsigned long flags;
  658. struct serial_state *state;
  659. int retval;
  660. if (!(info->flags & ASYNC_INITIALIZED))
  661. return;
  662. state = info->state;
  663. #ifdef SERIAL_DEBUG_OPEN
  664. printk("Shutting down serial port %d (irq %d)....", info->line,
  665.        state->irq);
  666. #endif
  667. save_flags(flags); cli(); /* Disable interrupts */
  668. /*
  669.  * First unlink the serial port from the IRQ chain...
  670.  */
  671. if (info->next_port)
  672. info->next_port->prev_port = info->prev_port;
  673. if (info->prev_port)
  674. info->prev_port->next_port = info->next_port;
  675. else
  676. IRQ_ports[state->irq] = info->next_port;
  677. figure_IRQ_timeout(state->irq);
  678. /*
  679.  * Free the IRQ, if necessary
  680.  */
  681. if (state->irq && (!IRQ_ports[state->irq] ||
  682.   !IRQ_ports[state->irq]->next_port)) {
  683. if (IRQ_ports[state->irq]) {
  684. free_irq(state->irq, NULL);
  685. retval = request_irq(state->irq, rs_interrupt_single,
  686.      SA_INTERRUPT, "txx927serial", NULL);
  687. if (retval)
  688. printk(KERN_WARNING "txx927serial shutdown: request_irq: error %d"
  689.        "  Couldn't reacquire IRQ.n", retval);
  690. } else
  691. free_irq(state->irq, NULL);
  692. }
  693. if (info->xmit.buf) {
  694. free_page((unsigned long) info->xmit.buf);
  695. info->xmit.buf = 0;
  696. }
  697. sio_reg(info)->dicr = 0; /* disable all intrs */
  698. /* disable break condition */
  699. sio_reg(info)->flcr &= ~TXx927_SIFLCR_TBRK;
  700. if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
  701. /* drop RTS */
  702. sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
  703. /* TXx927-SIO can not control DTR... */
  704. }
  705. /* reset FIFO's */
  706. sio_reg(info)->fcr |= TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
  707. TXx927_SIFCR_FRSTE;
  708. /* clear reset */
  709. sio_reg(info)->fcr &= ~(TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
  710. TXx927_SIFCR_FRSTE);
  711. /* DON'T disable Rx/Tx here, ie. DON'T set either
  712.  * TXx927_SIFLCR_RSDE or TXx927_SIFLCR_TSDE in flcr
  713.  */
  714. if (info->tty)
  715. set_bit(TTY_IO_ERROR, &info->tty->flags);
  716. info->flags &= ~ASYNC_INITIALIZED;
  717. restore_flags(flags);
  718. }
  719. /*
  720.  * This routine is called to set the UART divisor registers to match
  721.  * the specified baud rate for a serial port.
  722.  */
  723. static void change_speed(struct async_struct *info,
  724.  struct termios *old_termios)
  725. {
  726. int quot = 0, baud_base, baud;
  727. unsigned cflag, cval;
  728. int bits;
  729. unsigned long flags;
  730. if (!info->tty || !info->tty->termios)
  731. return;
  732. cflag = info->tty->termios->c_cflag;
  733. if (!info->port)
  734. return;
  735. cval = sio_reg(info)->lcr;
  736. /* byte size and parity */
  737. cval &= ~TXx927_SILCR_UMODE_MASK;
  738. switch (cflag & CSIZE) {
  739. case CS7:
  740. cval |= TXx927_SILCR_UMODE_7BIT;
  741. bits = 9;
  742. break;
  743. case CS5: /* not supported */
  744. case CS6: /* not supported */
  745. case CS8:
  746. default:
  747. cval |= TXx927_SILCR_UMODE_8BIT;
  748. bits = 10;
  749. break;
  750. }
  751. cval &= ~TXx927_SILCR_USBL_MASK;
  752. if (cflag & CSTOPB) {
  753. cval |= TXx927_SILCR_USBL_2BIT;
  754. bits++;
  755. } else {
  756. cval |= TXx927_SILCR_USBL_1BIT;
  757. }
  758. cval &= ~(TXx927_SILCR_UPEN|TXx927_SILCR_UEPS);
  759. if (cflag & PARENB) {
  760. cval |= TXx927_SILCR_UPEN;
  761. bits++;
  762. }
  763. if (!(cflag & PARODD))
  764. cval |= TXx927_SILCR_UEPS;
  765. /* Determine divisor based on baud rate */
  766. baud = tty_get_baud_rate(info->tty);
  767. if (!baud)
  768. baud = 9600; /* B0 transition handled in rs_set_termios */
  769. baud_base = info->state->baud_base;
  770. quot = (baud_base + baud / 2) / baud;
  771. /* If the quotient is zero refuse the change */
  772. if (!quot && old_termios) {
  773. info->tty->termios->c_cflag &= ~CBAUD;
  774. info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
  775. baud = tty_get_baud_rate(info->tty);
  776. if (!baud)
  777. baud = 9600;
  778. quot = (baud_base + baud / 2) / baud;
  779. }
  780. /* As a last resort, if the quotient is zero, default to 9600 bps */
  781. if (!quot)
  782. quot = (baud_base + 9600 / 2) / 9600;
  783. info->quot = quot;
  784. info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
  785. info->timeout += HZ/50; /* Add .02 seconds of slop */
  786. /* CTS flow control flag */
  787. if (cflag & CRTSCTS) {
  788. info->flags |= ASYNC_CTS_FLOW;
  789. if (info->state->flags & ASYNC_HAVE_CTS_LINE)
  790. sio_reg(info)->flcr = TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES |
  791. TXx927_SIFLCR_RTSTL_MAX /* 15 */;
  792. } else {
  793. info->flags &= ~ASYNC_CTS_FLOW;
  794. sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES | TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
  795. }
  796. /*
  797.  * Set up parity check flag
  798.  */
  799. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  800. info->read_status_mask = TXx927_SIDISR_UOER |
  801. TXx927_SIDISR_TDIS | TXx927_SIDISR_RDIS;
  802. if (I_INPCK(info->tty))
  803. info->read_status_mask |= TXx927_SIDISR_UFER | TXx927_SIDISR_UPER;
  804. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  805. info->read_status_mask |= TXx927_SIDISR_UBRK;
  806. /*
  807.  * Characters to ignore
  808.  */
  809. info->ignore_status_mask = 0;
  810. if (I_IGNPAR(info->tty))
  811. info->ignore_status_mask |= TXx927_SIDISR_UPER | TXx927_SIDISR_UFER;
  812. if (I_IGNBRK(info->tty)) {
  813. info->ignore_status_mask |= TXx927_SIDISR_UBRK;
  814. /*
  815.  * If we're ignore parity and break indicators, ignore 
  816.  * overruns too.  (For real raw support).
  817.  */
  818. if (I_IGNPAR(info->tty))
  819. info->ignore_status_mask |= TXx927_SIDISR_UOER;
  820. }
  821. /*
  822.  * !!! ignore all characters if CREAD is not set
  823.  */
  824. if ((cflag & CREAD) == 0)
  825. info->ignore_status_mask |= TXx927_SIDISR_RDIS;
  826. save_flags(flags); cli();
  827. sio_reg(info)->lcr = cval | TXx927_SILCR_SCS_IMCLK_BG;
  828. sio_reg(info)->bgr = quot | TXx927_SIBGR_BCLK_T0;
  829. restore_flags(flags);
  830. }
  831. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  832. {
  833. struct async_struct *info = (struct async_struct *)tty->driver_data;
  834. unsigned long flags;
  835. if (serial_paranoia_check(info, tty->device, "rs_put_char"))
  836. return;                                          
  837. if (!tty || !info->xmit.buf)
  838. return;
  839. save_flags(flags); cli();
  840. if (CIRC_SPACE(info->xmit.head,
  841.        info->xmit.tail,
  842.        SERIAL_XMIT_SIZE) == 0) {
  843. restore_flags(flags);
  844. return;
  845. }
  846. info->xmit.buf[info->xmit.head++] = ch;
  847. info->xmit.head &= SERIAL_XMIT_SIZE-1;
  848. restore_flags(flags);
  849. }
  850. static void rs_flush_chars(struct tty_struct *tty)
  851. {
  852. struct async_struct *info = (struct async_struct *)tty->driver_data;
  853. unsigned long flags;
  854. if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
  855. return;                                                  
  856. if (info->xmit.head == info->xmit.tail
  857.     || tty->stopped
  858.     || tty->hw_stopped
  859.     || !info->xmit.buf)
  860. return;
  861. save_flags(flags); cli();
  862. sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
  863. restore_flags(flags);
  864. }
  865. static int rs_write(struct tty_struct * tty, int from_user,
  866.     const unsigned char *buf, int count)
  867. {
  868. int c, ret = 0;
  869. struct async_struct *info = (struct async_struct *)tty->driver_data;
  870. unsigned long flags;
  871. if (serial_paranoia_check(info, tty->device, "rs_write"))
  872. return 0;
  873. if (!tty || !info->xmit.buf || !tmp_buf)
  874. return 0;
  875. save_flags(flags);
  876. if (from_user) {
  877. down(&tmp_buf_sem);
  878. while (1) {
  879. int c1;
  880. c = CIRC_SPACE_TO_END(info->xmit.head,
  881.       info->xmit.tail,
  882.       SERIAL_XMIT_SIZE);
  883. if (count < c)
  884. c = count;
  885. if (c <= 0)
  886. break;
  887. c -= copy_from_user(tmp_buf, buf, c);
  888. if (!c) {
  889. if (!ret)
  890. ret = -EFAULT;
  891. break;
  892. }
  893. cli();
  894. c1 = CIRC_SPACE_TO_END(info->xmit.head,
  895.        info->xmit.tail,
  896.        SERIAL_XMIT_SIZE);
  897. if (c1 < c)
  898. c = c1;
  899. memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
  900. info->xmit.head = ((info->xmit.head + c) &
  901.    (SERIAL_XMIT_SIZE-1));
  902. restore_flags(flags);
  903. buf += c;
  904. count -= c;
  905. ret += c;
  906. }
  907. up(&tmp_buf_sem);
  908. } else {
  909. cli();
  910. while (1) {
  911. c = CIRC_SPACE_TO_END(info->xmit.head,
  912.       info->xmit.tail,
  913.       SERIAL_XMIT_SIZE);
  914. if (count < c)
  915. c = count;
  916. if (c <= 0) {
  917. break;
  918. }
  919. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  920. info->xmit.head = ((info->xmit.head + c) &
  921.    (SERIAL_XMIT_SIZE-1));
  922. buf += c;
  923. count -= c;
  924. ret += c;
  925. }
  926. restore_flags(flags);
  927. }
  928. if (info->xmit.head != info->xmit.tail
  929.     && !tty->stopped
  930.     && !tty->hw_stopped
  931.     && !(info->IER & UART_IER_THRI)) 
  932. sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
  933. return ret;
  934. }
  935. static int rs_write_room(struct tty_struct *tty)
  936. {
  937. struct async_struct *info = (struct async_struct *)tty->driver_data;
  938. if (serial_paranoia_check(info, tty->device, "rs_write_room"))
  939.         return 0;                                           
  940. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  941. }
  942. static int rs_chars_in_buffer(struct tty_struct *tty)
  943. {
  944. struct async_struct *info = (struct async_struct *)tty->driver_data;
  945. if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
  946. return 0;                                                     
  947. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  948. }
  949. static void rs_flush_buffer(struct tty_struct *tty)
  950. {
  951. struct async_struct *info = (struct async_struct *)tty->driver_data;
  952. unsigned long flags;
  953. if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
  954. return;
  955. save_flags(flags); cli();
  956. info->xmit.head = info->xmit.tail = 0;
  957. restore_flags(flags);
  958. wake_up_interruptible(&tty->write_wait);
  959. #ifdef SERIAL_HAVE_POLL_WAIT
  960. wake_up_interruptible(&tty->poll_wait);
  961. #endif
  962. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  963.     tty->ldisc.write_wakeup)
  964. (tty->ldisc.write_wakeup)(tty);
  965. }
  966. /*
  967.  * This function is used to send a high-priority XON/XOFF character to
  968.  * the device
  969.  */
  970. static void rs_send_xchar(struct tty_struct *tty, char ch)
  971. {
  972. struct async_struct *info = (struct async_struct *)tty->driver_data;
  973. if (serial_paranoia_check(info, tty->device, "rs_send_char"))
  974. return;                                        
  975. info->x_char = ch;
  976. if (ch) {
  977. /* Make sure transmit interrupts are on */
  978. sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
  979. }
  980. }
  981. /*
  982.  * ------------------------------------------------------------
  983.  * rs_throttle()
  984.  * 
  985.  * This routine is called by the upper-layer tty layer to signal that
  986.  * incoming characters should be throttled.
  987.  * ------------------------------------------------------------
  988.  */
  989. static void rs_throttle(struct tty_struct * tty)
  990. {
  991. struct async_struct *info = (struct async_struct *)tty->driver_data;
  992. unsigned long flags;
  993. #ifdef SERIAL_DEBUG_THROTTLE
  994. char buf[64];
  995. printk("throttle %s: %d....n", tty_name(tty, buf),
  996.        tty->ldisc.chars_in_buffer(tty));
  997. #endif
  998. if (serial_paranoia_check(info, tty->device, "rs_throttle"))
  999. return;                                              
  1000. if (I_IXOFF(tty))
  1001. rs_send_xchar(tty, STOP_CHAR(tty));
  1002. if (tty->termios->c_cflag & CRTSCTS) {
  1003. save_flags(flags); cli();
  1004. /* drop RTS */
  1005. sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
  1006. restore_flags(flags);
  1007. }
  1008. }
  1009. static void rs_unthrottle(struct tty_struct * tty)
  1010. {
  1011. struct async_struct *info = (struct async_struct *)tty->driver_data;
  1012. unsigned long flags;
  1013. #ifdef SERIAL_DEBUG_THROTTLE
  1014. char buf[64];
  1015. printk("unthrottle %s: %d....n", tty_name(tty, buf),
  1016.        tty->ldisc.chars_in_buffer(tty));
  1017. #endif
  1018. if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
  1019. return;                                              
  1020. if (I_IXOFF(tty)) {
  1021. if (info->x_char)
  1022. info->x_char = 0;
  1023. else
  1024. rs_send_xchar(tty, START_CHAR(tty));
  1025. }
  1026. if (tty->termios->c_cflag & CRTSCTS) {
  1027. save_flags(flags); cli();
  1028. sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
  1029. restore_flags(flags);
  1030. }
  1031. }
  1032. /*
  1033.  * ------------------------------------------------------------
  1034.  * rs_ioctl() and friends
  1035.  * ------------------------------------------------------------
  1036.  */
  1037. static int get_modem_info(struct async_struct * info, unsigned int *value)
  1038. {
  1039. unsigned int result;
  1040. unsigned long flags;
  1041. save_flags(flags); cli();
  1042. result =  ((sio_reg(info)->flcr & TXx927_SIFLCR_RTSSC) ? 0 : TIOCM_RTS)
  1043. | ((sio_reg(info)->cisr & TXx927_SICISR_CTSS) ? 0 : TIOCM_CTS);
  1044. restore_flags(flags);
  1045. return put_user(result,value);
  1046. }
  1047. static int set_modem_info(struct async_struct * info, unsigned int cmd,
  1048.   unsigned int *value)
  1049. {
  1050. int error;
  1051. unsigned int arg;
  1052. unsigned long flags;
  1053. error = get_user(arg, value);
  1054. if (error)
  1055. return error;
  1056. save_flags(flags); cli();
  1057. switch (cmd) {
  1058. case TIOCMBIS: 
  1059. if (arg & TIOCM_RTS)
  1060. sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
  1061. break;
  1062. case TIOCMBIC:
  1063. if (arg & TIOCM_RTS)
  1064. sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
  1065. break;
  1066. case TIOCMSET:
  1067. sio_reg(info)->flcr =
  1068. (sio_reg(info)->flcr & ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE)) |
  1069. ((arg & TIOCM_RTS) ? 0 : TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
  1070. break;
  1071. default:
  1072. error = -EINVAL;
  1073. }
  1074. restore_flags(flags);
  1075. return error;
  1076. }
  1077. /*
  1078.  * rs_break() --- routine which turns the break handling on or off
  1079.  */
  1080. static void rs_break(struct tty_struct *tty, int break_state)
  1081. {
  1082. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1083. unsigned long flags;
  1084. if (serial_paranoia_check(info, tty->device, "rs_break"))
  1085. return;                                             
  1086. if (!info->port)
  1087. return;
  1088. save_flags(flags); cli();
  1089. if (break_state == -1)
  1090. sio_reg(info)->flcr |= TXx927_SIFLCR_TBRK;
  1091. else
  1092. sio_reg(info)->flcr &= ~TXx927_SIFLCR_TBRK;
  1093. restore_flags(flags);
  1094. }
  1095. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1096.     unsigned int cmd, unsigned long arg)
  1097. {
  1098. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1099. if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
  1100. return -ENODEV;                                
  1101. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1102.     (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1103.     (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1104. if (tty->flags & (1 << TTY_IO_ERROR))
  1105.     return -EIO;
  1106. }
  1107. switch (cmd) {
  1108. case TIOCMGET:
  1109. return get_modem_info(info, (unsigned int *) arg);
  1110. case TIOCMBIS:
  1111. case TIOCMBIC:
  1112. case TIOCMSET:
  1113. return set_modem_info(info, cmd, (unsigned int *) arg);
  1114. return 0;
  1115. case TIOCGSERIAL:
  1116. printk("TIOCGSERIALn");
  1117. return 0;
  1118. case TIOCSSERIAL:
  1119. printk("TIOCSSERIALn");
  1120. return 0;
  1121. case TIOCSERCONFIG:
  1122. printk("TIOCSERCONFIGn");
  1123. return 0;
  1124. case TIOCSERGETLSR: /* Get line status register */
  1125. printk("TIOCSERGETLSRn");
  1126. return 0;
  1127. case TIOCSERGSTRUCT:
  1128. printk("TIOCSERGSTRUCTn");
  1129. return 0;
  1130. /*
  1131.  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1132.  * - mask passed in arg for lines of interest
  1133.    *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1134.  * Caller should use TIOCGICOUNT to see which one it was
  1135.  */
  1136. case TIOCMIWAIT:
  1137. printk("TIOCMIWAITn");
  1138. return 0;
  1139. /* 
  1140.  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1141.  * Return: write counters to the user passed counter struct
  1142.  * NB: both 1->0 and 0->1 transitions are counted except for
  1143.  *     RI where only 0->1 is counted.
  1144.  */
  1145. case TIOCGICOUNT:
  1146. printk("TIOCGICOUNTn");
  1147. return 0;
  1148. case TIOCSERGWILD:
  1149. case TIOCSERSWILD:
  1150. /* "setserial -W" is called in Debian boot */
  1151. printk ("TIOCSER?WILD ioctl obsolete, ignored.n");
  1152. return 0;
  1153. default:
  1154. return -ENOIOCTLCMD;
  1155. }
  1156. return 0;
  1157. }
  1158. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1159. {
  1160. struct async_struct *info = (struct async_struct *)tty->driver_data;
  1161. unsigned long flags;
  1162. unsigned int cflag = tty->termios->c_cflag;
  1163. if (   (cflag == old_termios->c_cflag)
  1164.     && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
  1165. == RELEVANT_IFLAG(old_termios->c_iflag)))
  1166.   return;
  1167. change_speed(info, old_termios);
  1168. /* Handle transition to B0 status */
  1169. if ((old_termios->c_cflag & CBAUD) &&
  1170.     !(cflag & CBAUD)) {
  1171. save_flags(flags); cli();
  1172. sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
  1173. restore_flags(flags);
  1174. }
  1175. /* Handle transition away from B0 status */
  1176. if (!(old_termios->c_cflag & CBAUD) &&
  1177.     (cflag & CBAUD)) {
  1178. if (!(tty->termios->c_cflag & CRTSCTS) || 
  1179.     !test_bit(TTY_THROTTLED, &tty->flags)) {
  1180. save_flags(flags); cli();
  1181. sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
  1182. restore_flags(flags);
  1183. }
  1184. }
  1185. /* Handle turning off CRTSCTS */
  1186. if ((old_termios->c_cflag & CRTSCTS) &&
  1187.     !(tty->termios->c_cflag & CRTSCTS)) {
  1188. tty->hw_stopped = 0;
  1189. rs_start(tty);
  1190. }
  1191. }
  1192. /*
  1193.  * ------------------------------------------------------------
  1194.  * rs_close()
  1195.  * 
  1196.  * This routine is called when the serial port gets closed.  First, we
  1197.  * wait for the last remaining data to be sent.  Then, we unlink its
  1198.  * async structure from the interrupt chain if necessary, and we free
  1199.  * that IRQ if nothing is left in the chain.
  1200.  * ------------------------------------------------------------
  1201.  */
  1202. static void rs_close(struct tty_struct *tty, struct file * filp)
  1203. {
  1204. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1205. struct serial_state *state;
  1206. unsigned long flags;
  1207. if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
  1208. return;
  1209. state = info->state;
  1210. save_flags(flags); cli();
  1211. if (tty_hung_up_p(filp)) {
  1212. restore_flags(flags);
  1213. return;
  1214. }
  1215. #ifdef SERIAL_DEBUG_OPEN
  1216. printk("rs_close ttys%d, count = %dn", info->line, state->count);
  1217. #endif
  1218. if ((tty->count == 1) && (state->count != 1)) {
  1219. /*
  1220.  * Uh, oh.  tty->count is 1, which means that the tty
  1221.  * structure will be freed.  state->count should always
  1222.  * be one in these conditions.  If it's greater than
  1223.  * one, we've got real problems, since it means the
  1224.  * serial port won't be shutdown.
  1225.  */
  1226. printk(KERN_WARNING "rs_close: bad serial port count; tty->count is 1, "
  1227.        "state->count is %dn", state->count);
  1228. state->count = 1;
  1229. }
  1230. if (--state->count < 0) {
  1231. printk(KERN_WARNING "rs_close: bad serial port count for ttys%d: %dn",
  1232.        info->line, state->count);
  1233. state->count = 0;
  1234. }
  1235. if (state->count) {
  1236. restore_flags(flags);
  1237. return;
  1238. }
  1239. info->flags |= ASYNC_CLOSING;
  1240. /*
  1241.  * Save the termios structure, since this port may have
  1242.  * separate termios for callout and dialin.
  1243.  */
  1244. if (info->flags & ASYNC_NORMAL_ACTIVE)
  1245. info->state->normal_termios = *tty->termios;
  1246. if (info->flags & ASYNC_CALLOUT_ACTIVE)
  1247. info->state->callout_termios = *tty->termios;
  1248. /*
  1249.  * Now we wait for the transmit buffer to clear; and we notify 
  1250.  * the line discipline to only process XON/XOFF characters.
  1251.  */
  1252. tty->closing = 1;
  1253. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1254. tty_wait_until_sent(tty, info->closing_wait);
  1255. /*
  1256.  * At this point we stop accepting input.  To do this, we
  1257.  * disable the receive line status interrupts, and tell the
  1258.  * interrupt driver to stop checking the data ready bit in the
  1259.  * line status register.
  1260.  */
  1261. info->read_status_mask &= ~TXx927_SIDISR_RDIS;
  1262. if (info->flags & ASYNC_INITIALIZED) {
  1263. #if 0
  1264. sio_reg(info)->dicr &= ~TXx927_SIDICR_RIE;
  1265. #endif
  1266. /*
  1267.  * Before we drop DTR, make sure the UART transmitter
  1268.  * has completely drained; this is especially
  1269.  * important if there is a transmit FIFO!
  1270.  */
  1271. rs_wait_until_sent(tty, info->timeout);
  1272. }
  1273. shutdown(info);
  1274. if (tty->driver.flush_buffer)
  1275. tty->driver.flush_buffer(tty);
  1276. if (tty->ldisc.flush_buffer)
  1277. tty->ldisc.flush_buffer(tty);
  1278. tty->closing = 0;
  1279. info->event = 0;
  1280. info->tty = 0;
  1281. if (info->blocked_open) {
  1282. if (info->close_delay) {
  1283. current->state = TASK_INTERRUPTIBLE;
  1284. schedule_timeout(info->close_delay);
  1285. }
  1286. wake_up_interruptible(&info->open_wait);
  1287. }
  1288. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
  1289.  ASYNC_CLOSING);
  1290. wake_up_interruptible(&info->close_wait);
  1291. restore_flags(flags);
  1292. }
  1293. /*
  1294.  * rs_wait_until_sent() --- wait until the transmitter is empty
  1295.  */
  1296. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1297. {
  1298. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1299. unsigned long orig_jiffies, char_time;
  1300. int cisr;
  1301. if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
  1302. return;                                            
  1303. if (info->xmit_fifo_size == 0)
  1304. return; /* Just in case.... */
  1305. orig_jiffies = jiffies;
  1306. /*
  1307.  * Set the check interval to be 1/5 of the estimated time to
  1308.  * send a single character, and make it at least 1.  The check
  1309.  * interval should also be less than the timeout.
  1310.  * 
  1311.  * Note: we have to use pretty tight timings here to satisfy
  1312.  * the NIST-PCTS.
  1313.  */
  1314. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1315. char_time = char_time / 5;
  1316. if (char_time == 0)
  1317. char_time = 1;
  1318. if (timeout)
  1319.   char_time = MIN(char_time, timeout);
  1320. /*
  1321.  * If the transmitter hasn't cleared in twice the approximate
  1322.  * amount of time to send the entire FIFO, it probably won't
  1323.  * ever clear.  This assumes the UART isn't doing flow
  1324.  * control, which is currently the case.  Hence, if it ever
  1325.  * takes longer than info->timeout, this is probably due to a
  1326.  * UART bug of some kind.  So, we clamp the timeout parameter at
  1327.  * 2*info->timeout.
  1328.  */
  1329. if (!timeout || timeout > 2*info->timeout)
  1330. timeout = 2*info->timeout;
  1331. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1332. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1333. printk("jiff=%lu...", jiffies);
  1334. #endif
  1335. while (!((cisr = sio_reg(info)->cisr) & TXx927_SICISR_TXALS)) {
  1336. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1337. printk("cisr = %d (jiff=%lu)...", cisr, jiffies);
  1338. #endif
  1339. current->state = TASK_INTERRUPTIBLE;
  1340. current->counter = 0; /* make us low-priority */
  1341. schedule_timeout(char_time);
  1342. if (signal_pending(current))
  1343. break;
  1344. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1345. break;
  1346. }
  1347. current->state = TASK_RUNNING;
  1348. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1349. printk("cisr = %d (jiff=%lu)...donen", cisr, jiffies);
  1350. #endif
  1351. }
  1352. /*
  1353.  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1354.  */
  1355. static void rs_hangup(struct tty_struct *tty)
  1356. {
  1357. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1358. struct serial_state *state = info->state;
  1359. if (serial_paranoia_check(info, tty->device, "rs_hangup"))
  1360. return;                                           
  1361. state = info->state;
  1362. rs_flush_buffer(tty);
  1363. shutdown(info);
  1364. info->event = 0;
  1365. state->count = 0;
  1366. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
  1367. info->tty = 0;
  1368. wake_up_interruptible(&info->open_wait);
  1369. }
  1370. /*
  1371.  * ------------------------------------------------------------
  1372.  * rs_open() and friends
  1373.  * ------------------------------------------------------------
  1374.  */
  1375. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1376.    struct async_struct *info)
  1377. {
  1378. DECLARE_WAITQUEUE(wait, current);
  1379. struct serial_state *state = info->state;
  1380. int retval;
  1381. int do_clocal = 0, extra_count = 0;
  1382. unsigned long flags;
  1383. /*
  1384.  * If the device is in the middle of being closed, then block
  1385.  * until it's done, and then try again.
  1386.  */
  1387. if (tty_hung_up_p(filp) ||
  1388.     (info->flags & ASYNC_CLOSING)) {
  1389. if (info->flags & ASYNC_CLOSING)
  1390. interruptible_sleep_on(&info->close_wait);
  1391. #ifdef SERIAL_DO_RESTART
  1392. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1393. -EAGAIN : -ERESTARTSYS);
  1394. #else
  1395. return -EAGAIN;
  1396. #endif
  1397. }
  1398. /*
  1399.  * If this is a callout device, then just make sure the normal
  1400.  * device isn't being used.
  1401.  */
  1402. if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
  1403. if (info->flags & ASYNC_NORMAL_ACTIVE)
  1404. return -EBUSY;
  1405. if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1406.     (info->flags & ASYNC_SESSION_LOCKOUT) &&
  1407.     (info->session != current->session))
  1408.     return -EBUSY;
  1409. if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1410.     (info->flags & ASYNC_PGRP_LOCKOUT) &&
  1411.     (info->pgrp != current->pgrp))
  1412.     return -EBUSY;
  1413. info->flags |= ASYNC_CALLOUT_ACTIVE;
  1414. return 0;
  1415. }
  1416. /*
  1417.  * If non-blocking mode is set, or the port is not enabled,
  1418.  * then make the check up front and then exit.
  1419.  */
  1420. if ((filp->f_flags & O_NONBLOCK) ||
  1421.     (tty->flags & (1 << TTY_IO_ERROR))) {
  1422. if (info->flags & ASYNC_CALLOUT_ACTIVE)
  1423. return -EBUSY;
  1424. info->flags |= ASYNC_NORMAL_ACTIVE;
  1425. return 0;
  1426. }
  1427. if (info->flags & ASYNC_CALLOUT_ACTIVE) {
  1428. if (state->normal_termios.c_cflag & CLOCAL)
  1429. do_clocal = 1;
  1430. } else {
  1431. if (tty->termios->c_cflag & CLOCAL)
  1432. do_clocal = 1;
  1433. }
  1434. /*
  1435.  * Block waiting for the carrier detect and the line to become
  1436.  * free (i.e., not in use by the callout).  While we are in
  1437.  * this loop, state->count is dropped by one, so that
  1438.  * rs_close() knows when to free things.  We restore it upon
  1439.  * exit, either normal or abnormal.
  1440.  */
  1441. retval = 0;
  1442. add_wait_queue(&info->open_wait, &wait);
  1443. #ifdef SERIAL_DEBUG_OPEN
  1444. printk("block_til_ready before block: ttys%d, count = %dn",
  1445.        state->line, state->count);
  1446. #endif
  1447. save_flags(flags); cli();
  1448. if (!tty_hung_up_p(filp)) {
  1449. extra_count = 1;
  1450. state->count--;
  1451. }
  1452. restore_flags(flags);
  1453. info->blocked_open++;
  1454. while (1) {
  1455. save_flags(flags); cli();
  1456. if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1457.     (tty->termios->c_cflag & CBAUD))
  1458. sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
  1459. restore_flags(flags);
  1460. current->state = TASK_INTERRUPTIBLE;
  1461. if (tty_hung_up_p(filp) ||
  1462.     !(info->flags & ASYNC_INITIALIZED)) {
  1463. #ifdef SERIAL_DO_RESTART
  1464. if (info->flags & ASYNC_HUP_NOTIFY)
  1465. retval = -EAGAIN;
  1466. else
  1467. retval = -ERESTARTSYS;
  1468. #else
  1469. retval = -EAGAIN;
  1470. #endif
  1471. break;
  1472. }
  1473. if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1474.     !(info->flags & ASYNC_CLOSING))
  1475. break;
  1476. if (signal_pending(current)) {
  1477. retval = -ERESTARTSYS;
  1478. break;
  1479. }
  1480. #ifdef SERIAL_DEBUG_OPEN
  1481. printk("block_til_ready blocking: ttys%d, count = %dn",
  1482.        info->line, state->count);
  1483. #endif
  1484. schedule();
  1485. }
  1486. current->state = TASK_RUNNING;
  1487. remove_wait_queue(&info->open_wait, &wait);
  1488. if (extra_count)
  1489. state->count++;
  1490. info->blocked_open--;
  1491. #ifdef SERIAL_DEBUG_OPEN
  1492. printk("block_til_ready after blocking: ttys%d, count = %dn",
  1493.        info->line, state->count);
  1494. #endif
  1495. if (retval)
  1496. return retval;
  1497. info->flags |= ASYNC_NORMAL_ACTIVE;
  1498. return 0;
  1499. }
  1500. static int get_async_struct(int line, struct async_struct **ret_info)
  1501. {
  1502. struct async_struct *info;
  1503. struct serial_state *sstate;
  1504. #ifdef REMOTE_DEBUG
  1505. if (kdb_port_info.state && line == kdb_port_info.line)
  1506. return -ENODEV;
  1507. #endif
  1508. sstate = rs_table + line;
  1509. sstate->count++;
  1510. if (sstate->info) {
  1511. *ret_info = sstate->info;
  1512. return 0;
  1513. }
  1514. info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
  1515. if (!info) {
  1516. sstate->count--;
  1517. return -ENOMEM;
  1518. }
  1519. memset(info, 0, sizeof(struct async_struct));
  1520. init_waitqueue_head(&info->open_wait);
  1521. init_waitqueue_head(&info->close_wait);
  1522. init_waitqueue_head(&info->delta_msr_wait);
  1523. info->magic = SERIAL_MAGIC;
  1524. info->port = sstate->port;
  1525. info->flags = sstate->flags;
  1526. info->io_type = sstate->io_type;
  1527. info->xmit_fifo_size = sstate->xmit_fifo_size;
  1528. info->line = line;
  1529. info->tqueue.routine = do_softint;
  1530. info->tqueue.data = info;
  1531. info->state = sstate;
  1532. if (sstate->info) {
  1533. kfree(info);
  1534. *ret_info = sstate->info;
  1535. return 0;
  1536. }
  1537. *ret_info = sstate->info = info;
  1538. return 0;
  1539. }
  1540. /*
  1541.  * This routine is called whenever a serial port is opened.  It
  1542.  * enables interrupts for a serial port, linking in its async structure into
  1543.  * the IRQ chain.   It also performs the serial-specific
  1544.  * initialization for the tty structure.
  1545.  */
  1546. static int rs_open(struct tty_struct *tty, struct file * filp)
  1547. {
  1548. struct async_struct *info;
  1549. int  retval, line;
  1550. unsigned long page;
  1551. line = MINOR(tty->device) - tty->driver.minor_start;
  1552. if ((line < 0) || (line >= NR_PORTS)) {
  1553. return -ENODEV;
  1554. }
  1555. retval = get_async_struct(line, &info);
  1556. if (retval) {
  1557. return retval;
  1558. }
  1559. tty->driver_data = info;
  1560. info->tty = tty;
  1561. #ifdef SERIAL_DEBUG_OPEN
  1562. printk("rs_open %s%d, count = %dn", tty->driver.name, info->line,
  1563.        info->state->count);
  1564. #endif
  1565. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1566. if (!tmp_buf) {
  1567. page = get_free_page(GFP_KERNEL);
  1568. if (!page) {
  1569. return -ENOMEM;
  1570. }
  1571. if (tmp_buf)
  1572. free_page(page);
  1573. else
  1574. tmp_buf = (unsigned char *) page;
  1575. }
  1576. /*
  1577.  * If the port is the middle of closing, bail out now
  1578.  */
  1579. if (tty_hung_up_p(filp) ||
  1580.     (info->flags & ASYNC_CLOSING)) {
  1581. if (info->flags & ASYNC_CLOSING)
  1582. interruptible_sleep_on(&info->close_wait);
  1583. #ifdef SERIAL_DO_RESTART
  1584. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1585. -EAGAIN : -ERESTARTSYS);
  1586. #else
  1587. return -EAGAIN;
  1588. #endif
  1589. }
  1590. /*
  1591.  * Start up serial port
  1592.  */
  1593. retval = startup(info);
  1594. if (retval) {
  1595. return retval;
  1596. }
  1597. retval = block_til_ready(tty, filp, info);
  1598. if (retval) {
  1599. #ifdef SERIAL_DEBUG_OPEN
  1600. printk("rs_open returning after block_til_ready with %dn",
  1601.        retval);
  1602. #endif
  1603. return retval;
  1604. }
  1605. if ((info->state->count == 1) &&
  1606.     (info->flags & ASYNC_SPLIT_TERMIOS)) {
  1607. if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
  1608. *tty->termios = info->state->normal_termios;
  1609. else 
  1610. *tty->termios = info->state->callout_termios;
  1611. change_speed(info, 0);
  1612. }
  1613. #ifdef CONFIG_TXX927_SERIAL_CONSOLE
  1614. if (sercons.cflag && sercons.index == line) {
  1615. tty->termios->c_cflag = sercons.cflag;
  1616. sercons.cflag = 0;
  1617. change_speed(info, 0);
  1618. }
  1619. #endif
  1620. info->session = current->session;
  1621. info->pgrp = current->pgrp;
  1622. #ifdef SERIAL_DEBUG_OPEN
  1623. printk("rs_open ttys%d successful...", info->line);
  1624. #endif
  1625. return 0;
  1626. }
  1627. /*
  1628.  * /proc fs routines....
  1629.  */
  1630. static inline int line_info(char *buf, struct serial_state *state)
  1631. {
  1632. struct async_struct *info = state->info, scr_info;
  1633. char stat_buf[30];
  1634. int ret;
  1635. unsigned long flags;
  1636. ret = sprintf(buf, "%d: uart:%s port:%lX irq:%d",
  1637.       state->line, SERIAL_DRIVER_NAME, 
  1638.       state->port, state->irq);
  1639. if (!state->port) {
  1640. ret += sprintf(buf+ret, "n");
  1641. return ret;
  1642. }
  1643. /*
  1644.  * Figure out the current RS-232 lines
  1645.  */
  1646. if (!info) {
  1647. info = &scr_info; /* This is just for serial_{in,out} */
  1648. info->magic = SERIAL_MAGIC;
  1649. info->port = state->port;
  1650. info->flags = state->flags;
  1651. info->quot = 0;
  1652. info->tty = 0;
  1653. }
  1654. stat_buf[0] = 0;
  1655. stat_buf[1] = 0;
  1656. save_flags(flags); cli();
  1657. if (!(sio_reg(info)->flcr & TXx927_SIFLCR_RTSSC))
  1658. strcat(stat_buf, "|RTS");
  1659. if (!(sio_reg(info)->cisr & TXx927_SICISR_CTSS))
  1660. strcat(stat_buf, "|CTS");
  1661. restore_flags(flags); 
  1662. if (info->quot) {
  1663. ret += sprintf(buf+ret, " baud:%d",
  1664.        state->baud_base / info->quot);
  1665. }
  1666. ret += sprintf(buf+ret, " tx:%d rx:%d",
  1667.       state->icount.tx, state->icount.rx);
  1668. if (state->icount.frame)
  1669. ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
  1670. if (state->icount.parity)
  1671. ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
  1672. if (state->icount.brk)
  1673. ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
  1674. if (state->icount.overrun)
  1675. ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
  1676. /*
  1677.  * Last thing is the RS-232 status lines
  1678.  */
  1679. ret += sprintf(buf+ret, " %sn", stat_buf+1);
  1680. return ret;
  1681. }
  1682. static int rs_read_proc(char *page, char **start, off_t off, int count,
  1683.  int *eof, void *data)
  1684. {
  1685. int i, len = 0, l;
  1686. off_t begin = 0;
  1687. len += sprintf(page, "serinfo:1.0 driver:%sn", serial_version);
  1688. for (i = 0; i < NR_PORTS && len < 4000; i++) {
  1689. l = line_info(page + len, &rs_table[i]);
  1690. len += l;
  1691. if (len+begin > off+count)
  1692. goto done;
  1693. if (len+begin < off) {
  1694. begin += len;
  1695. len = 0;
  1696. }
  1697. }
  1698. *eof = 1;
  1699. done:
  1700. if (off >= len+begin)
  1701. return 0;
  1702. *start = page + (begin-off);
  1703. return ((count < begin+len-off) ? count : begin+len-off);
  1704. }
  1705. /*
  1706.  * ---------------------------------------------------------------------
  1707.  * rs_init() and friends
  1708.  *
  1709.  * rs_init() is called at boot-time to initialize the serial driver.
  1710.  * ---------------------------------------------------------------------
  1711.  */
  1712. /*
  1713.  * This routine prints out the appropriate serial driver version
  1714.  * number, and identifies which options were configured into this
  1715.  * driver.
  1716.  */
  1717. static _INLINE_ void show_serial_version(void)
  1718. {
  1719.   printk(KERN_INFO "%s version %sn", serial_name, serial_version);
  1720. }
  1721. /*
  1722.  * The serial driver boot-time initialization code!
  1723.  */
  1724. static int __init rs_init(void)
  1725. {
  1726. int i;
  1727. struct serial_state * state;
  1728. if (rs_table[0].port == 0)
  1729. return -ENODEV;
  1730. init_bh(TXX927_SERIAL_BH, do_serial_bh);
  1731. init_timer(&serial_timer);
  1732. serial_timer.function = rs_timer;
  1733. mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
  1734. for (i = 0; i < NR_IRQS; i++) {
  1735. IRQ_ports[i] = 0;
  1736. IRQ_timeout[i] = 0;
  1737. }
  1738. #ifdef CONFIG_TXX927_SERIAL_CONSOLE
  1739. /*
  1740.  * The interrupt of the serial console port
  1741.  * can't be shared.
  1742.  */
  1743. if (sercons.flags & CON_CONSDEV) {
  1744. for(i = 0; i < NR_PORTS; i++)
  1745. if (i != sercons.index &&
  1746.     rs_table[i].irq == rs_table[sercons.index].irq)
  1747. rs_table[i].irq = 0;
  1748. }
  1749. #endif
  1750. show_serial_version();
  1751. /* Initialize the tty_driver structure */
  1752. memset(&serial_driver, 0, sizeof(struct tty_driver));
  1753. serial_driver.magic = TTY_DRIVER_MAGIC;
  1754. serial_driver.driver_name = "txx927serial";
  1755. #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
  1756. serial_driver.name = "tts/%d";
  1757. #else
  1758. serial_driver.name = "ttyS";
  1759. #endif
  1760. serial_driver.major = TXX927_TTY_MAJOR;
  1761. serial_driver.minor_start = TXX927_TTY_MINOR_START + SERIAL_DEV_OFFSET;
  1762. serial_driver.num = NR_PORTS;
  1763. serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
  1764. serial_driver.subtype = SERIAL_TYPE_NORMAL;
  1765. serial_driver.init_termios = tty_std_termios;
  1766. serial_driver.init_termios.c_cflag =
  1767. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1768. serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
  1769. serial_driver.refcount = &serial_refcount;
  1770. serial_driver.table = serial_table;
  1771. serial_driver.termios = serial_termios;
  1772. serial_driver.termios_locked = serial_termios_locked;
  1773. serial_driver.open = rs_open;
  1774. serial_driver.close = rs_close;
  1775. serial_driver.write = rs_write;
  1776. serial_driver.put_char = rs_put_char;
  1777. serial_driver.flush_chars = rs_flush_chars;
  1778. serial_driver.write_room = rs_write_room;
  1779. serial_driver.chars_in_buffer = rs_chars_in_buffer;
  1780. serial_driver.flush_buffer = rs_flush_buffer;
  1781. serial_driver.ioctl = rs_ioctl;
  1782. serial_driver.throttle = rs_throttle;
  1783. serial_driver.unthrottle = rs_unthrottle;
  1784. serial_driver.send_xchar = rs_send_xchar;
  1785. serial_driver.set_termios = rs_set_termios;
  1786. serial_driver.stop = rs_stop;
  1787. serial_driver.start = rs_start;
  1788. serial_driver.hangup = rs_hangup;
  1789. serial_driver.break_ctl = rs_break;
  1790. serial_driver.wait_until_sent = rs_wait_until_sent;
  1791. serial_driver.read_proc = rs_read_proc;
  1792. /*
  1793.  * The callout device is just like normal device except for
  1794.  * major number and the subtype code.
  1795.  */
  1796. callout_driver = serial_driver;
  1797. #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
  1798.         callout_driver.name = "cua/%d";
  1799. #else
  1800. callout_driver.name = "cua";
  1801. #endif                               
  1802. callout_driver.major = TTYAUX_MAJOR;
  1803. callout_driver.subtype = SERIAL_TYPE_CALLOUT;
  1804. callout_driver.read_proc = 0;
  1805. callout_driver.proc_entry = 0;
  1806. if (tty_register_driver(&serial_driver)){
  1807. panic("Couldn't register serial drivern");
  1808. }
  1809. if (tty_register_driver(&callout_driver)) {
  1810. panic("Couldn't register callout drivern");
  1811. }
  1812. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  1813. state->magic = SSTATE_MAGIC;
  1814. state->line = i;
  1815. state->type = PORT_UNKNOWN;
  1816. state->custom_divisor = 0;
  1817. state->close_delay = 5*HZ/10;
  1818. state->closing_wait = 30*HZ;
  1819. state->callout_termios = callout_driver.init_termios;
  1820. state->normal_termios = serial_driver.init_termios;
  1821. state->icount.cts = state->icount.dsr = 
  1822. state->icount.rng = state->icount.dcd = 0;
  1823. state->icount.rx = state->icount.tx = 0;
  1824. state->icount.frame = state->icount.parity = 0;
  1825. state->icount.overrun = state->icount.brk = 0;
  1826. state->irq = irq_cannonicalize(state->irq);
  1827. state->xmit_fifo_size = TXx927_SIO_TX_FIFO;
  1828. if (state->hub6)
  1829. state->io_type = SERIAL_IO_HUB6;
  1830. if (state->port) {
  1831. continue;                                
  1832. }
  1833. }
  1834. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  1835.         if (state->type == PORT_UNKNOWN) {
  1836.        continue;
  1837. }                                        
  1838. printk(KERN_INFO "%s%02d at 0x%04lx (irq = %d) is a %sn",
  1839.        TXX927_TTY_NAME,
  1840.        state->line,
  1841.        state->port, state->irq,
  1842.        SERIAL_DRIVER_NAME);
  1843. tty_register_devfs(&serial_driver, 0,
  1844.                 serial_driver.minor_start + state->line);
  1845. tty_register_devfs(&callout_driver, 0,
  1846.         callout_driver.minor_start + state->line); 
  1847. }
  1848. return 0;
  1849. }
  1850. static void __exit rs_fini(void) 
  1851. {
  1852. unsigned long flags;
  1853. int e1, e2;
  1854. int i;
  1855. struct async_struct *info;
  1856. del_timer_sync(&serial_timer);
  1857. save_flags(flags); cli();
  1858.         remove_bh(TXX927_SERIAL_BH);
  1859. if ((e1 = tty_unregister_driver(&serial_driver)))
  1860. printk(KERN_WARNING "serial: failed to unregister serial driver (%d)n",
  1861.        e1);
  1862. if ((e2 = tty_unregister_driver(&callout_driver)))
  1863. printk(KERN_WARNING "serial: failed to unregister callout driver (%d)n", 
  1864.        e2);
  1865. restore_flags(flags);
  1866. for (i = 0; i < NR_PORTS; i++) {
  1867. if ((info = rs_table[i].info)) {
  1868. rs_table[i].info = NULL;
  1869. kfree(info);
  1870. }
  1871. }
  1872. if (tmp_buf) {
  1873. unsigned long pg = (unsigned long) tmp_buf;
  1874. tmp_buf = NULL;
  1875. free_page(pg);
  1876. }
  1877. }
  1878. module_init(rs_init);
  1879. module_exit(rs_fini);
  1880. MODULE_DESCRIPTION("TXX927 serial driver");
  1881. /*
  1882.  * ------------------------------------------------------------
  1883.  * Serial console driver
  1884.  * ------------------------------------------------------------
  1885.  */
  1886. #ifdef CONFIG_TXX927_SERIAL_CONSOLE
  1887. static struct async_struct async_sercons;
  1888. /*
  1889.  * Print a string to the serial port trying not to disturb
  1890.  * any possible real use of the port...
  1891.  */
  1892. static void serial_console_write(struct console *co, const char *s,
  1893. unsigned count)
  1894. {
  1895. static struct async_struct *info = &async_sercons;
  1896. int ier;
  1897. unsigned i;
  1898. /*
  1899.  * First save the IER then disable the interrupts
  1900.  */
  1901. ier = sio_reg(info)->dicr;
  1902. sio_reg(info)->dicr = 0;
  1903. /*
  1904.  * Now, do each character
  1905.  */
  1906. for (i = 0; i < count; i++, s++) {
  1907. wait_for_xmitr(info);
  1908. /*
  1909.  * Send the character out.
  1910.  * If a LF, also do CR...
  1911.  */
  1912. sio_reg(info)->tfifo = *s;
  1913. if (*s == 10) {
  1914. wait_for_xmitr(info);
  1915. sio_reg(info)->tfifo = 13;
  1916. }
  1917. }
  1918. /*
  1919.  * Finally, Wait for transmitter & holding register to empty
  1920.  *  and restore the IER
  1921.  */
  1922. wait_for_xmitr(info);
  1923. sio_reg(info)->dicr = ier;
  1924. }
  1925. static kdev_t serial_console_device(struct console *c)
  1926. {
  1927. return MKDEV(TXX927_TTY_MAJOR, TXX927_TTY_MINOR_START + c->index);
  1928. }
  1929. /*
  1930.  * Setup initial baud/bits/parity. We do two things here:
  1931.  * - construct a cflag setting for the first rs_open()
  1932.  * - initialize the serial port
  1933.  * Return non-zero if we didn't find a serial port.
  1934.  */
  1935. static int serial_console_setup(struct console *co, char *options)
  1936. {
  1937. static struct async_struct *info;
  1938. struct serial_state *state;
  1939. unsigned cval;
  1940. int baud = 9600;
  1941. int bits = 8;
  1942. int parity = 'n';
  1943. int cflag = CREAD | HUPCL | CLOCAL;
  1944. int quot = 0;
  1945. char *s;
  1946. if (co->index < 0 || co->index >= NR_PORTS) {
  1947. return -1;
  1948. }
  1949. if (options) {
  1950. baud = simple_strtoul(options, NULL, 10);
  1951. s = options;
  1952. while(*s >= '0' && *s <= '9')
  1953. s++;
  1954. if (*s) parity = *s++;
  1955. if (*s) bits   = *s - '0';
  1956. }
  1957. /*
  1958.  * Now construct a cflag setting.
  1959.  */
  1960. switch(baud) {
  1961. case 1200:
  1962. cflag |= B1200;
  1963. break;
  1964. case 2400:
  1965. cflag |= B2400;
  1966. break;
  1967. case 4800:
  1968. cflag |= B4800;
  1969. break;
  1970. case 19200:
  1971. cflag |= B19200;
  1972. break;
  1973. case 38400:
  1974. cflag |= B38400;
  1975. break;
  1976. case 57600:
  1977. cflag |= B57600;
  1978. break;
  1979. case 115200:
  1980. cflag |= B115200;
  1981. break;
  1982. case 9600:
  1983. default:
  1984. cflag |= B9600;
  1985. break;
  1986. }
  1987. switch(bits) {
  1988. case 7:
  1989. cflag |= CS7;
  1990. break;
  1991. default:
  1992. case 8:
  1993. cflag |= CS8;
  1994. break;
  1995. }
  1996. switch(parity) {
  1997. case 'o': case 'O':
  1998. cflag |= PARODD;
  1999. break;
  2000. case 'e': case 'E':
  2001. cflag |= PARENB;
  2002. break;
  2003. }
  2004. co->cflag = cflag;
  2005. /*
  2006.  * Divisor, bytesize and parity
  2007.  */
  2008. state = rs_table + co->index;
  2009. info = &async_sercons;
  2010. info->magic = SERIAL_MAGIC;
  2011. info->state = state;
  2012. info->port = state->port;
  2013. info->flags = state->flags;
  2014. info->io_type = state->io_type;
  2015. info->iomem_base = state->iomem_base;
  2016. info->iomem_reg_shift = state->iomem_reg_shift;
  2017. quot = state->baud_base / baud;
  2018. switch (cflag & CSIZE) {
  2019. case CS7: cval = TXx927_SILCR_UMODE_7BIT; break;
  2020. default:
  2021. case CS8: cval = TXx927_SILCR_UMODE_8BIT; break;
  2022. }
  2023. if (cflag & CSTOPB)
  2024. cval |= TXx927_SILCR_USBL_2BIT;
  2025. else
  2026. cval |= TXx927_SILCR_USBL_1BIT;
  2027. if (cflag & PARENB)
  2028. cval |= TXx927_SILCR_UPEN;
  2029. if (!(cflag & PARODD))
  2030. cval |= TXx927_SILCR_UEPS;
  2031. /*
  2032.  * Disable UART interrupts, set DTR and RTS high
  2033.  * and set speed.
  2034.  */
  2035. sio_reg(info)->dicr = 0;
  2036. sio_reg(info)->lcr = cval | TXx927_SILCR_SCS_IMCLK_BG;
  2037. sio_reg(info)->bgr = quot | TXx927_SIBGR_BCLK_T0;
  2038. /* HW RTS/CTS control */
  2039. if (info->flags & ASYNC_HAVE_CTS_LINE)
  2040. sio_reg(info)->flcr = TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES |
  2041. TXx927_SIFLCR_RTSTL_MAX /* 15 */;
  2042. /* Enable RX/TX */
  2043. sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
  2044. return 0;
  2045. }
  2046. static struct console sercons = {
  2047. name:           TXX927_TTY_NAME,
  2048. write:          serial_console_write,
  2049. device:         serial_console_device,
  2050. setup:         serial_console_setup,
  2051. flags:         CON_PRINTBUFFER,
  2052. index:         -1,
  2053. };
  2054. /*
  2055.  * Register console.
  2056.  */
  2057. void __init txx927_console_init(void)
  2058. {
  2059. register_console(&sercons);
  2060. }
  2061. #endif