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

Linux/Unix编程

开发平台:

Unix_Linux

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