2 * $Id: ctctty.c,v 1.21 2004/07/02 16:31:22 ptiedem Exp $
4 * CTC / ESCON network driver, tty interface.
6 * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/tty.h>
28 #include <linux/serial_reg.h>
29 #include <linux/interrupt.h>
30 #include <asm/uaccess.h>
31 #include <linux/devfs_fs_kernel.h>
35 #define CTC_TTY_MAJOR 43
36 #define CTC_TTY_MAX_DEVICES 64
38 #define CTC_ASYNC_MAGIC 0x49344C01 /* for paranoia-checking */
39 #define CTC_ASYNC_INITIALIZED 0x80000000 /* port was initialized */
40 #define CTC_ASYNC_NORMAL_ACTIVE 0x20000000 /* Normal device active */
41 #define CTC_ASYNC_CLOSING 0x08000000 /* Serial port is closing */
42 #define CTC_ASYNC_CTS_FLOW 0x04000000 /* Do CTS flow control */
43 #define CTC_ASYNC_CHECK_CD 0x02000000 /* i.e., CLOCAL */
44 #define CTC_ASYNC_HUP_NOTIFY 0x0001 /* Notify tty on hangups/closes */
45 #define CTC_ASYNC_NETDEV_OPEN 0x0002 /* Underlying netdev is open */
46 #define CTC_ASYNC_TX_LINESTAT 0x0004 /* Must send line status */
47 #define CTC_ASYNC_SPLIT_TERMIOS 0x0008 /* Sep. termios for dialin/out */
48 #define CTC_TTY_XMIT_SIZE 1024 /* Default bufsize for write */
49 #define CTC_SERIAL_XMIT_MAX 4000 /* Maximum bufsize for write */
51 /* Private data (similar to async_struct in <linux/serial.h>) */
54 int flags; /* defined in tty.h */
55 int mcr; /* Modem control register */
56 int msr; /* Modem status register */
57 int lsr; /* Line status register */
59 int count; /* # of fd on device */
60 int blocked_open; /* # of blocked opens */
61 struct net_device *netdev;
62 struct sk_buff_head tx_queue; /* transmit queue */
63 struct sk_buff_head rx_queue; /* receive queue */
64 struct tty_struct *tty; /* Pointer to corresponding tty */
65 wait_queue_head_t open_wait;
66 wait_queue_head_t close_wait;
67 struct semaphore write_sem;
68 struct tasklet_struct tasklet;
69 struct timer_list stoptimer;
72 /* Description of one CTC-tty */
74 struct tty_driver *ctc_tty_device; /* tty-device */
75 ctc_tty_info info[CTC_TTY_MAX_DEVICES]; /* Private data */
78 static ctc_tty_driver *driver;
80 /* Leave this unchanged unless you know what you do! */
81 #define MODEM_PARANOIA_CHECK
82 #define MODEM_DO_RESTART
84 #define CTC_TTY_NAME "ctctty"
86 static __u32 ctc_tty_magic = CTC_ASYNC_MAGIC;
87 static int ctc_tty_shuttingdown = 0;
89 static spinlock_t ctc_tty_lock;
91 /* ctc_tty_try_read() is called from within ctc_tty_rcv_skb()
92 * to stuff incoming data directly into a tty's flip-buffer. If the
93 * flip buffer is full, the packet gets queued up.
97 * 0 = Failure, data has to be buffered and later processed by
98 * ctc_tty_readmodem().
101 ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
105 struct tty_struct *tty;
107 DBF_TEXT(trace, 2, __FUNCTION__);
108 if ((tty = info->tty)) {
109 if (info->mcr & UART_MCR_RTS) {
110 c = TTY_FLIPBUF_SIZE - tty->flip.count;
113 memcpy(tty->flip.char_buf_ptr, skb->data, len);
114 memset(tty->flip.flag_buf_ptr, 0, len);
115 tty->flip.count += len;
116 tty->flip.char_buf_ptr += len;
117 tty->flip.flag_buf_ptr += len;
118 tty_flip_buffer_push(tty);
127 /* ctc_tty_readmodem() is called periodically from within timer-interrupt.
128 * It tries getting received data from the receive queue an stuff it into
129 * the tty's flip-buffer.
132 ctc_tty_readmodem(ctc_tty_info *info)
135 struct tty_struct *tty;
137 DBF_TEXT(trace, 2, __FUNCTION__);
138 if ((tty = info->tty)) {
139 if (info->mcr & UART_MCR_RTS) {
140 int c = TTY_FLIPBUF_SIZE - tty->flip.count;
143 if ((c > 0) && (skb = skb_dequeue(&info->rx_queue))) {
147 memcpy(tty->flip.char_buf_ptr, skb->data, len);
149 memset(tty->flip.flag_buf_ptr, 0, len);
150 tty->flip.count += len;
151 tty->flip.char_buf_ptr += len;
152 tty->flip.flag_buf_ptr += len;
153 tty_flip_buffer_push(tty);
155 skb_queue_head(&info->rx_queue, skb);
158 ret = skb_queue_len(&info->rx_queue);
167 ctc_tty_setcarrier(struct net_device *netdev, int on)
171 DBF_TEXT(trace, 2, __FUNCTION__);
172 if ((!driver) || ctc_tty_shuttingdown)
174 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
175 if (driver->info[i].netdev == netdev) {
176 ctc_tty_info *info = &driver->info[i];
178 info->msr |= UART_MSR_DCD;
180 info->msr &= ~UART_MSR_DCD;
181 if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
182 tty_hangup(info->tty);
187 ctc_tty_netif_rx(struct sk_buff *skb)
190 ctc_tty_info *info = NULL;
192 DBF_TEXT(trace, 2, __FUNCTION__);
195 if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
199 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
200 if (driver->info[i].netdev == skb->dev) {
201 info = &driver->info[i];
212 if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
216 skb_pull(skb, sizeof(__u32));
218 i = *((int *)skb->data);
219 skb_pull(skb, sizeof(info->mcr));
220 if (i & UART_MCR_RTS) {
221 info->msr |= UART_MSR_CTS;
222 if (info->flags & CTC_ASYNC_CTS_FLOW)
223 info->tty->hw_stopped = 0;
225 info->msr &= ~UART_MSR_CTS;
226 if (info->flags & CTC_ASYNC_CTS_FLOW)
227 info->tty->hw_stopped = 1;
229 if (i & UART_MCR_DTR)
230 info->msr |= UART_MSR_DSR;
232 info->msr &= ~UART_MSR_DSR;
237 /* Try to deliver directly via tty-flip-buf if queue is empty */
238 if (skb_queue_empty(&info->rx_queue))
239 if (ctc_tty_try_read(info, skb))
241 /* Direct deliver failed or queue wasn't empty.
242 * Queue up for later dequeueing via timer-irq.
244 skb_queue_tail(&info->rx_queue, skb);
245 /* Schedule dequeuing */
246 tasklet_schedule(&info->tasklet);
250 ctc_tty_tint(ctc_tty_info * info)
252 struct sk_buff *skb = skb_dequeue(&info->tx_queue);
253 int stopped = (info->tty->hw_stopped || info->tty->stopped);
257 DBF_TEXT(trace, 2, __FUNCTION__);
263 if (info->flags & CTC_ASYNC_TX_LINESTAT) {
264 int skb_res = info->netdev->hard_header_len +
265 sizeof(info->mcr) + sizeof(__u32);
266 /* If we must update line status,
267 * create an empty dummy skb and insert it.
270 skb_queue_head(&info->tx_queue, skb);
272 skb = dev_alloc_skb(skb_res);
275 "ctc_tty: Out of memory in %s%d tint\n",
276 CTC_TTY_NAME, info->line);
279 skb_reserve(skb, skb_res);
286 skb_queue_head(&info->tx_queue, skb);
291 printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
293 printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
295 memcpy(skb_push(skb, sizeof(info->mcr)), &info->mcr, sizeof(info->mcr));
296 memcpy(skb_push(skb, sizeof(__u32)), &ctc_tty_magic, sizeof(__u32));
297 rc = info->netdev->hard_start_xmit(skb, info->netdev);
299 skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
301 skb_queue_head(&info->tx_queue, skb);
305 struct tty_struct *tty = info->tty;
307 info->flags &= ~CTC_ASYNC_TX_LINESTAT;
309 if (wake && (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
310 tty->ldisc.write_wakeup)
311 (tty->ldisc.write_wakeup)(tty);
312 wake_up_interruptible(&tty->write_wait);
315 return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
318 /************************************************************
322 * mostly "stolen" from original Linux-serial.c and friends.
324 ************************************************************/
327 ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
329 #ifdef MODEM_PARANOIA_CHECK
331 printk(KERN_WARNING "ctc_tty: null info_struct for %s in %s\n",
335 if (info->magic != CTC_ASYNC_MAGIC) {
336 printk(KERN_WARNING "ctc_tty: bad magic for info struct %s in %s\n",
345 ctc_tty_inject(ctc_tty_info *info, char c)
350 DBF_TEXT(trace, 2, __FUNCTION__);
351 if (ctc_tty_shuttingdown)
353 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
355 skb = dev_alloc_skb(skb_res);
358 "ctc_tty: Out of memory in %s%d tx_inject\n",
359 CTC_TTY_NAME, info->line);
362 skb_reserve(skb, skb_res);
363 *(skb_put(skb, 1)) = c;
364 skb_queue_head(&info->tx_queue, skb);
365 tasklet_schedule(&info->tasklet);
369 ctc_tty_transmit_status(ctc_tty_info *info)
371 DBF_TEXT(trace, 2, __FUNCTION__);
372 if (ctc_tty_shuttingdown)
374 info->flags |= CTC_ASYNC_TX_LINESTAT;
375 tasklet_schedule(&info->tasklet);
379 ctc_tty_change_speed(ctc_tty_info * info)
385 DBF_TEXT(trace, 2, __FUNCTION__);
386 if (!info->tty || !info->tty->termios)
388 cflag = info->tty->termios->c_cflag;
390 quot = i = cflag & CBAUD;
394 info->tty->termios->c_cflag &= ~CBAUDEX;
399 info->mcr |= UART_MCR_DTR;
400 info->mcr |= UART_MCR_RTS;
401 ctc_tty_transmit_status(info);
403 info->mcr &= ~UART_MCR_DTR;
404 info->mcr &= ~UART_MCR_RTS;
405 ctc_tty_transmit_status(info);
409 /* CTS flow control flag and modem status interrupts */
410 if (cflag & CRTSCTS) {
411 info->flags |= CTC_ASYNC_CTS_FLOW;
413 info->flags &= ~CTC_ASYNC_CTS_FLOW;
415 info->flags &= ~CTC_ASYNC_CHECK_CD;
417 info->flags |= CTC_ASYNC_CHECK_CD;
422 ctc_tty_startup(ctc_tty_info * info)
424 DBF_TEXT(trace, 2, __FUNCTION__);
425 if (info->flags & CTC_ASYNC_INITIALIZED)
427 #ifdef CTC_DEBUG_MODEM_OPEN
428 printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
431 * Now, initialize the UART
433 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
435 clear_bit(TTY_IO_ERROR, &info->tty->flags);
437 * and set the speed of the serial port
439 ctc_tty_change_speed(info);
441 info->flags |= CTC_ASYNC_INITIALIZED;
442 if (!(info->flags & CTC_ASYNC_NETDEV_OPEN))
443 info->netdev->open(info->netdev);
444 info->flags |= CTC_ASYNC_NETDEV_OPEN;
449 ctc_tty_stopdev(unsigned long data)
451 ctc_tty_info *info = (ctc_tty_info *)data;
453 if ((!info) || (!info->netdev) ||
454 (info->flags & CTC_ASYNC_INITIALIZED))
456 info->netdev->stop(info->netdev);
457 info->flags &= ~CTC_ASYNC_NETDEV_OPEN;
461 * This routine will shutdown a serial port; interrupts are disabled, and
462 * DTR is dropped if the hangup on close termio flag is on.
465 ctc_tty_shutdown(ctc_tty_info * info)
467 DBF_TEXT(trace, 2, __FUNCTION__);
468 if (!(info->flags & CTC_ASYNC_INITIALIZED))
470 #ifdef CTC_DEBUG_MODEM_OPEN
471 printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
473 info->msr &= ~UART_MSR_RI;
474 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
475 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
477 set_bit(TTY_IO_ERROR, &info->tty->flags);
478 mod_timer(&info->stoptimer, jiffies + (10 * HZ));
479 skb_queue_purge(&info->tx_queue);
480 skb_queue_purge(&info->rx_queue);
481 info->flags &= ~CTC_ASYNC_INITIALIZED;
484 /* ctc_tty_write() is the main send-routine. It is called from the upper
485 * levels within the kernel to perform sending data. Depending on the
486 * online-flag it either directs output to the at-command-interpreter or
487 * to the lower level. Additional tasks done here:
488 * - If online, check for escape-sequence (+++)
489 * - If sending audio-data, call ctc_tty_DLEdown() to parse DLE-codes.
490 * - If receiving audio-data, call ctc_tty_end_vrx() to abort if needed.
491 * - If dialing, abort dial.
494 ctc_tty_write(struct tty_struct *tty, int from_user, const u_char * buf, int count)
498 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
500 DBF_TEXT(trace, 2, __FUNCTION__);
501 if (ctc_tty_shuttingdown)
503 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write"))
512 down(&info->write_sem);
517 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
521 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
523 skb = dev_alloc_skb(skb_res + c);
526 "ctc_tty: Out of memory in %s%d write\n",
527 CTC_TTY_NAME, info->line);
530 skb_reserve(skb, skb_res);
532 copy_from_user(skb_put(skb, c),
533 (const u_char __user *)buf, c);
535 memcpy(skb_put(skb, c), buf, c);
536 skb_queue_tail(&info->tx_queue, skb);
541 if (skb_queue_len(&info->tx_queue)) {
542 info->lsr &= ~UART_LSR_TEMT;
543 tasklet_schedule(&info->tasklet);
546 up(&info->write_sem);
548 DBF_TEXT(trace, 6, __FUNCTION__);
553 ctc_tty_write_room(struct tty_struct *tty)
555 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
557 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write_room"))
559 return CTC_TTY_XMIT_SIZE;
563 ctc_tty_chars_in_buffer(struct tty_struct *tty)
565 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
567 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_chars_in_buffer"))
573 ctc_tty_flush_buffer(struct tty_struct *tty)
578 DBF_TEXT(trace, 2, __FUNCTION__);
581 spin_lock_irqsave(&ctc_tty_lock, flags);
582 info = (ctc_tty_info *) tty->driver_data;
583 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_buffer")) {
584 spin_unlock_irqrestore(&ctc_tty_lock, flags);
587 skb_queue_purge(&info->tx_queue);
588 info->lsr |= UART_LSR_TEMT;
589 spin_unlock_irqrestore(&ctc_tty_lock, flags);
590 wake_up_interruptible(&tty->write_wait);
591 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
592 tty->ldisc.write_wakeup)
593 (tty->ldisc.write_wakeup) (tty);
595 DBF_TEXT_(trace, 2, "ex: %s ", __FUNCTION__);
600 ctc_tty_flush_chars(struct tty_struct *tty)
602 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
604 if (ctc_tty_shuttingdown)
606 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
608 if (tty->stopped || tty->hw_stopped || (!skb_queue_len(&info->tx_queue)))
610 tasklet_schedule(&info->tasklet);
614 * ------------------------------------------------------------
617 * This routine is called by the upper-layer tty layer to signal that
618 * incoming characters should be throttled.
619 * ------------------------------------------------------------
622 ctc_tty_throttle(struct tty_struct *tty)
624 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
626 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_throttle"))
628 info->mcr &= ~UART_MCR_RTS;
630 ctc_tty_inject(info, STOP_CHAR(tty));
631 ctc_tty_transmit_status(info);
635 ctc_tty_unthrottle(struct tty_struct *tty)
637 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
639 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_unthrottle"))
641 info->mcr |= UART_MCR_RTS;
643 ctc_tty_inject(info, START_CHAR(tty));
644 ctc_tty_transmit_status(info);
648 * ------------------------------------------------------------
649 * ctc_tty_ioctl() and friends
650 * ------------------------------------------------------------
654 * ctc_tty_get_lsr_info - get line status register info
656 * Purpose: Let user call ioctl() to get info when the UART physically
657 * is emptied. On bus types like RS485, the transmitter must
658 * release the bus after transmitting. This must be done when
659 * the transmit shift register is empty, not be done when the
660 * transmit holding register is empty. This functionality
661 * allows RS485 driver to be written in user space.
664 ctc_tty_get_lsr_info(ctc_tty_info * info, uint __user *value)
670 spin_lock_irqsave(&ctc_tty_lock, flags);
672 spin_unlock_irqrestore(&ctc_tty_lock, flags);
673 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
674 put_user(result, value);
679 static int ctc_tty_tiocmget(struct tty_struct *tty, struct file *file)
681 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
687 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
689 if (tty->flags & (1 << TTY_IO_ERROR))
693 spin_lock_irqsave(&ctc_tty_lock, flags);
695 spin_unlock_irqrestore(&ctc_tty_lock, flags);
696 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
697 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
698 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
699 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
700 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
701 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
706 ctc_tty_tiocmset(struct tty_struct *tty, struct file *file,
707 unsigned int set, unsigned int clear)
709 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
711 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
713 if (tty->flags & (1 << TTY_IO_ERROR))
717 info->mcr |= UART_MCR_RTS;
719 info->mcr |= UART_MCR_DTR;
721 if (clear & TIOCM_RTS)
722 info->mcr &= ~UART_MCR_RTS;
723 if (clear & TIOCM_DTR)
724 info->mcr &= ~UART_MCR_DTR;
726 if ((set | clear) & (TIOCM_RTS|TIOCM_DTR))
727 ctc_tty_transmit_status(info);
732 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
735 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
739 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
741 if (tty->flags & (1 << TTY_IO_ERROR))
744 case TCSBRK: /* SVID version: non-zero arg --> no break */
745 #ifdef CTC_DEBUG_MODEM_IOCTL
746 printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
748 retval = tty_check_change(tty);
751 tty_wait_until_sent(tty, 0);
753 case TCSBRKP: /* support for POSIX tcsendbreak() */
754 #ifdef CTC_DEBUG_MODEM_IOCTL
755 printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
757 retval = tty_check_change(tty);
760 tty_wait_until_sent(tty, 0);
763 #ifdef CTC_DEBUG_MODEM_IOCTL
764 printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
767 error = put_user(C_CLOCAL(tty) ? 1 : 0, (ulong __user *) arg);
770 #ifdef CTC_DEBUG_MODEM_IOCTL
771 printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
774 error = get_user(arg, (ulong __user *) arg);
777 tty->termios->c_cflag =
778 ((tty->termios->c_cflag & ~CLOCAL) |
781 case TIOCSERGETLSR: /* Get line status register */
782 #ifdef CTC_DEBUG_MODEM_IOCTL
783 printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
786 error = verify_area(VERIFY_WRITE, (void __user *) arg, sizeof(uint));
790 return ctc_tty_get_lsr_info(info, (uint __user *) arg);
792 #ifdef CTC_DEBUG_MODEM_IOCTL
793 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
794 CTC_TTY_NAME, info->line);
802 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
804 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
805 unsigned int cflag = tty->termios->c_cflag;
806 ctc_tty_change_speed(info);
808 /* Handle transition to B0 */
809 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
810 info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
811 ctc_tty_transmit_status(info);
814 /* Handle transition from B0 to other */
815 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
816 info->mcr |= UART_MCR_DTR;
817 if (!(tty->termios->c_cflag & CRTSCTS) ||
818 !test_bit(TTY_THROTTLED, &tty->flags)) {
819 info->mcr |= UART_MCR_RTS;
821 ctc_tty_transmit_status(info);
824 /* Handle turning off CRTSCTS */
825 if ((old_termios->c_cflag & CRTSCTS) &&
826 !(tty->termios->c_cflag & CRTSCTS))
831 * ------------------------------------------------------------
832 * ctc_tty_open() and friends
833 * ------------------------------------------------------------
836 ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
838 DECLARE_WAITQUEUE(wait, NULL);
844 * If the device is in the middle of being closed, then block
845 * until it's done, and then try again.
847 if (tty_hung_up_p(filp) ||
848 (info->flags & CTC_ASYNC_CLOSING)) {
849 if (info->flags & CTC_ASYNC_CLOSING)
850 wait_event(info->close_wait,
851 !(info->flags & CTC_ASYNC_CLOSING));
852 #ifdef MODEM_DO_RESTART
853 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
862 * If non-blocking mode is set, then make the check up front
865 if ((filp->f_flags & O_NONBLOCK) ||
866 (tty->flags & (1 << TTY_IO_ERROR))) {
867 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
870 if (tty->termios->c_cflag & CLOCAL)
873 * Block waiting for the carrier detect and the line to become
874 * free (i.e., not in use by the callout). While we are in
875 * this loop, info->count is dropped by one, so that
876 * ctc_tty_close() knows when to free things. We restore it upon
877 * exit, either normal or abnormal.
880 add_wait_queue(&info->open_wait, &wait);
881 #ifdef CTC_DEBUG_MODEM_OPEN
882 printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
883 CTC_TTY_NAME, info->line, info->count);
885 spin_lock_irqsave(&ctc_tty_lock, flags);
886 if (!(tty_hung_up_p(filp)))
888 spin_unlock_irqrestore(&ctc_tty_lock, flags);
889 info->blocked_open++;
891 set_current_state(TASK_INTERRUPTIBLE);
892 if (tty_hung_up_p(filp) ||
893 !(info->flags & CTC_ASYNC_INITIALIZED)) {
894 #ifdef MODEM_DO_RESTART
895 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
898 retval = -ERESTARTSYS;
904 if (!(info->flags & CTC_ASYNC_CLOSING) &&
905 (do_clocal || (info->msr & UART_MSR_DCD))) {
908 if (signal_pending(current)) {
909 retval = -ERESTARTSYS;
912 #ifdef CTC_DEBUG_MODEM_OPEN
913 printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
914 CTC_TTY_NAME, info->line, info->count);
918 current->state = TASK_RUNNING;
919 remove_wait_queue(&info->open_wait, &wait);
920 if (!tty_hung_up_p(filp))
922 info->blocked_open--;
923 #ifdef CTC_DEBUG_MODEM_OPEN
924 printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
925 CTC_TTY_NAME, info->line, info->count);
929 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
934 * This routine is called whenever a serial port is opened. It
935 * enables interrupts for a serial port, linking in its async structure into
936 * the IRQ chain. It also performs the serial-specific
937 * initialization for the tty structure.
940 ctc_tty_open(struct tty_struct *tty, struct file *filp)
943 unsigned long saveflags;
948 if (line < 0 || line > CTC_TTY_MAX_DEVICES)
950 info = &driver->info[line];
951 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
955 #ifdef CTC_DEBUG_MODEM_OPEN
956 printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
959 spin_lock_irqsave(&ctc_tty_lock, saveflags);
961 tty->driver_data = info;
963 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
965 * Start up serial port
967 retval = ctc_tty_startup(info);
969 #ifdef CTC_DEBUG_MODEM_OPEN
970 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
974 retval = ctc_tty_block_til_ready(tty, filp, info);
976 #ifdef CTC_DEBUG_MODEM_OPEN
977 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
981 #ifdef CTC_DEBUG_MODEM_OPEN
982 printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
988 ctc_tty_close(struct tty_struct *tty, struct file *filp)
990 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
994 if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
996 spin_lock_irqsave(&ctc_tty_lock, flags);
997 if (tty_hung_up_p(filp)) {
998 spin_unlock_irqrestore(&ctc_tty_lock, flags);
999 #ifdef CTC_DEBUG_MODEM_OPEN
1000 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
1004 if ((tty->count == 1) && (info->count != 1)) {
1006 * Uh, oh. tty->count is 1, which means that the tty
1007 * structure will be freed. Info->count should always
1008 * be one in these conditions. If it's greater than
1009 * one, we've got real problems, since it means the
1010 * serial port won't be shutdown.
1012 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
1013 "info->count is %d\n", info->count);
1016 if (--info->count < 0) {
1017 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
1018 CTC_TTY_NAME, info->line, info->count);
1022 local_irq_restore(flags);
1023 #ifdef CTC_DEBUG_MODEM_OPEN
1024 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1028 info->flags |= CTC_ASYNC_CLOSING;
1031 * At this point we stop accepting input. To do this, we
1032 * disable the receive line status interrupts, and tell the
1033 * interrupt driver to stop checking the data ready bit in the
1034 * line status register.
1036 if (info->flags & CTC_ASYNC_INITIALIZED) {
1037 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
1039 * Before we drop DTR, make sure the UART transmitter
1040 * has completely drained; this is especially
1041 * important if there is a transmit FIFO!
1043 timeout = jiffies + HZ;
1044 while (!(info->lsr & UART_LSR_TEMT)) {
1045 set_current_state(TASK_INTERRUPTIBLE);
1046 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1047 schedule_timeout(HZ/2);
1048 spin_lock_irqsave(&ctc_tty_lock, flags);
1049 if (time_after(jiffies,timeout))
1053 ctc_tty_shutdown(info);
1054 if (tty->driver->flush_buffer) {
1055 skb_queue_purge(&info->tx_queue);
1056 info->lsr |= UART_LSR_TEMT;
1058 if (tty->ldisc.flush_buffer)
1059 tty->ldisc.flush_buffer(tty);
1062 if (info->blocked_open) {
1063 set_current_state(TASK_INTERRUPTIBLE);
1064 schedule_timeout(HZ/2);
1065 wake_up_interruptible(&info->open_wait);
1067 info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
1068 wake_up_interruptible(&info->close_wait);
1069 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1070 #ifdef CTC_DEBUG_MODEM_OPEN
1071 printk(KERN_DEBUG "ctc_tty_close normal exit\n");
1076 * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1079 ctc_tty_hangup(struct tty_struct *tty)
1081 ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1082 unsigned long saveflags;
1083 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
1085 ctc_tty_shutdown(info);
1087 info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1088 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1090 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1091 wake_up_interruptible(&info->open_wait);
1096 * For all online tty's, try sending data to
1100 ctc_tty_task(unsigned long arg)
1102 ctc_tty_info *info = (void *)arg;
1103 unsigned long saveflags;
1106 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1107 if ((!ctc_tty_shuttingdown) && info) {
1108 again = ctc_tty_tint(info);
1110 info->lsr |= UART_LSR_TEMT;
1111 again |= ctc_tty_readmodem(info);
1113 tasklet_schedule(&info->tasklet);
1116 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1119 static struct tty_operations ctc_ops = {
1120 .open = ctc_tty_open,
1121 .close = ctc_tty_close,
1122 .write = ctc_tty_write,
1123 .flush_chars = ctc_tty_flush_chars,
1124 .write_room = ctc_tty_write_room,
1125 .chars_in_buffer = ctc_tty_chars_in_buffer,
1126 .flush_buffer = ctc_tty_flush_buffer,
1127 .ioctl = ctc_tty_ioctl,
1128 .throttle = ctc_tty_throttle,
1129 .unthrottle = ctc_tty_unthrottle,
1130 .set_termios = ctc_tty_set_termios,
1131 .hangup = ctc_tty_hangup,
1132 .tiocmget = ctc_tty_tiocmget,
1133 .tiocmset = ctc_tty_tiocmset,
1141 struct tty_driver *device;
1143 driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1144 if (driver == NULL) {
1145 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1148 memset(driver, 0, sizeof(ctc_tty_driver));
1149 device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
1152 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1156 device->devfs_name = "ctc/" CTC_TTY_NAME;
1157 device->name = CTC_TTY_NAME;
1158 device->major = CTC_TTY_MAJOR;
1159 device->minor_start = 0;
1160 device->type = TTY_DRIVER_TYPE_SERIAL;
1161 device->subtype = SERIAL_TYPE_NORMAL;
1162 device->init_termios = tty_std_termios;
1163 device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1164 device->flags = TTY_DRIVER_REAL_RAW;
1165 device->driver_name = "ctc_tty",
1166 tty_set_operations(device, &ctc_ops);
1167 if (tty_register_driver(device)) {
1168 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1169 put_tty_driver(device);
1173 driver->ctc_tty_device = device;
1174 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1175 info = &driver->info[i];
1176 init_MUTEX(&info->write_sem);
1177 tasklet_init(&info->tasklet, ctc_tty_task,
1178 (unsigned long) info);
1179 info->magic = CTC_ASYNC_MAGIC;
1183 info->blocked_open = 0;
1184 init_waitqueue_head(&info->open_wait);
1185 init_waitqueue_head(&info->close_wait);
1186 skb_queue_head_init(&info->tx_queue);
1187 skb_queue_head_init(&info->rx_queue);
1188 init_timer(&info->stoptimer);
1189 info->stoptimer.function = ctc_tty_stopdev;
1190 info->stoptimer.data = (unsigned long)info;
1191 info->mcr = UART_MCR_RTS;
1197 ctc_tty_register_netdev(struct net_device *dev) {
1202 if ((!dev) || (!dev->name)) {
1204 "ctc_tty_register_netdev called "
1205 "with NULL dev or NULL dev-name\n");
1210 * If the name is a format string the caller wants us to
1211 * do a name allocation : format string must end with %d
1213 if (strchr(dev->name, '%'))
1215 int err = dev_alloc_name(dev, dev->name); // dev->name is changed by this
1217 printk(KERN_DEBUG "dev_alloc returned error %d\n", err);
1223 for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1224 ttynum = simple_strtoul(p, &err, 0);
1225 if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1228 "ctc_tty_register_netdev called "
1229 "with number in name '%s'\n", dev->name);
1232 if (driver->info[ttynum].netdev) {
1234 "ctc_tty_register_netdev called "
1235 "for already registered device '%s'\n",
1239 driver->info[ttynum].netdev = dev;
1244 ctc_tty_unregister_netdev(struct net_device *dev) {
1246 unsigned long saveflags;
1247 ctc_tty_info *info = NULL;
1249 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1250 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
1251 if (driver->info[i].netdev == dev) {
1252 info = &driver->info[i];
1256 info->netdev = NULL;
1257 skb_queue_purge(&info->tx_queue);
1258 skb_queue_purge(&info->rx_queue);
1260 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1264 ctc_tty_cleanup(void) {
1265 unsigned long saveflags;
1267 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1268 ctc_tty_shuttingdown = 1;
1269 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1270 tty_unregister_driver(driver->ctc_tty_device);
1271 put_tty_driver(driver->ctc_tty_device);