2 * $Id: ctctty.c,v 1.11 2003/05/14 15:27:54 felfert 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 #define __NO_VERSION__
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/tty.h>
29 #include <linux/serial_reg.h>
30 #include <linux/interrupt.h>
31 #include <asm/uaccess.h>
32 #ifdef CONFIG_DEVFS_FS
33 # include <linux/devfs_fs_kernel.h>
37 #if LINUX_VERSION_CODE < 0x020212
38 typedef struct wait_queue wait_queue_t;
39 typedef struct wait_queue *wait_queue_head_t;
40 #define DECLARE_WAITQUEUE(wait, current) \
41 struct wait_queue wait = { current, NULL }
42 #define init_waitqueue_head(x) *(x)=NULL
43 #define __set_current_state(state_value) \
44 do { current->state = state_value; } while (0)
46 #define set_current_state(state_value) \
47 do { __set_current_state(state_value); mb(); } while (0)
49 #define set_current_state(state_value) __set_current_state(state_value)
51 #define init_MUTEX(x) *(x)=MUTEX
54 #define CTC_TTY_MAJOR 43
55 #define CTC_TTY_MAX_DEVICES 64
57 #define CTC_ASYNC_MAGIC 0x49344C01 /* for paranoia-checking */
58 #define CTC_ASYNC_INITIALIZED 0x80000000 /* port was initialized */
59 #define CTC_ASYNC_NORMAL_ACTIVE 0x20000000 /* Normal device active */
60 #define CTC_ASYNC_CLOSING 0x08000000 /* Serial port is closing */
61 #define CTC_ASYNC_CTS_FLOW 0x04000000 /* Do CTS flow control */
62 #define CTC_ASYNC_CHECK_CD 0x02000000 /* i.e., CLOCAL */
63 #define CTC_ASYNC_HUP_NOTIFY 0x0001 /* Notify tty on hangups/closes */
64 #define CTC_ASYNC_NETDEV_OPEN 0x0002 /* Underlying netdev is open */
65 #define CTC_ASYNC_TX_LINESTAT 0x0004 /* Must send line status */
66 #define CTC_ASYNC_SPLIT_TERMIOS 0x0008 /* Sep. termios for dialin/out */
67 #define CTC_TTY_XMIT_SIZE 1024 /* Default bufsize for write */
68 #define CTC_SERIAL_XMIT_MAX 4000 /* Maximum bufsize for write */
69 #define CTC_SERIAL_TYPE_NORMAL 1
71 /* Private data (similar to async_struct in <linux/serial.h>) */
74 int flags; /* defined in tty.h */
75 int mcr; /* Modem control register */
76 int msr; /* Modem status register */
77 int lsr; /* Line status register */
79 int count; /* # of fd on device */
80 int blocked_open; /* # of blocked opens */
82 struct sk_buff_head tx_queue; /* transmit queue */
83 struct sk_buff_head rx_queue; /* receive queue */
84 struct tty_struct *tty; /* Pointer to corresponding tty */
85 struct termios normal_termios; /* For saving termios structs */
86 wait_queue_head_t open_wait;
87 wait_queue_head_t close_wait;
88 struct semaphore write_sem;
90 struct timer_list stoptimer;
91 struct timer_list flowtimer;
94 /* Description of one CTC-tty */
96 int refcount; /* Number of opens */
97 struct tty_driver ctc_tty_device; /* tty-device */
98 struct tty_struct *modem_table[CTC_TTY_MAX_DEVICES];
99 struct termios *modem_termios[CTC_TTY_MAX_DEVICES];
100 struct termios *modem_termios_locked[CTC_TTY_MAX_DEVICES];
101 ctc_tty_info info[CTC_TTY_MAX_DEVICES]; /* Private data */
104 static ctc_tty_driver *driver;
106 /* Leave this unchanged unless you know what you do! */
107 #define MODEM_PARANOIA_CHECK
108 #define MODEM_DO_RESTART
110 #define CTC_TTY_NAME "ctctty"
112 #ifdef CONFIG_DEVFS_FS
113 static char *ctc_ttyname = "ctc/" CTC_TTY_NAME "%d";
115 static char *ctc_ttyname = CTC_TTY_NAME;
118 char *ctc_tty_revision = "$Revision: 1.11 $";
120 static __u32 ctc_tty_magic = CTC_ASYNC_MAGIC;
121 static int ctc_tty_shuttingdown = 0;
123 static spinlock_t ctc_tty_lock;
125 /* ctc_tty_try_read() is called from within ctc_tty_rcv_skb()
126 * to stuff incoming data directly into a tty's flip-buffer. If the
127 * flip buffer is full, the packet gets queued up.
131 * 0 = Failure, data has to be buffered and later processed by
132 * ctc_tty_readmodem().
135 ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
139 struct tty_struct *tty;
141 if ((tty = info->tty)) {
142 if (info->mcr & UART_MCR_RTS) {
143 c = TTY_FLIPBUF_SIZE - tty->flip.count;
146 memcpy(tty->flip.char_buf_ptr, skb->data, len);
147 memset(tty->flip.flag_buf_ptr, 0, len);
148 tty->flip.count += len;
149 tty->flip.char_buf_ptr += len;
150 tty->flip.flag_buf_ptr += len;
151 tty_flip_buffer_push(tty);
160 /* ctc_tty_readmodem() is called periodically from within timer-interrupt.
161 * It tries getting received data from the receive queue an stuff it into
162 * the tty's flip-buffer.
165 ctc_tty_readmodem(ctc_tty_info *info)
168 struct tty_struct *tty;
171 if (!(tty = info->tty))
174 /* If the upper layer is flow blocked or just
175 * has no room for data we schedule a timer to
176 * try again later - wilder
178 c = TTY_FLIPBUF_SIZE - tty->flip.count;
179 if ( !(info->mcr & UART_MCR_RTS) || (c <= 0) ) {
180 /* can't do any work now, wake up later */
181 mod_timer(&info->flowtimer, jiffies+(HZ/100) );
185 if ((skb = skb_dequeue(&info->rx_queue))) {
189 memcpy(tty->flip.char_buf_ptr, skb->data, len);
191 memset(tty->flip.flag_buf_ptr, 0, len);
192 tty->flip.count += len;
193 tty->flip.char_buf_ptr += len;
194 tty->flip.flag_buf_ptr += len;
195 tty_flip_buffer_push(tty);
198 skb_queue_head(&info->rx_queue, skb);
204 return skb_queue_len(&info->rx_queue);
208 ctc_tty_setcarrier(net_device *netdev, int on)
212 if ((!driver) || ctc_tty_shuttingdown)
214 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
215 if (driver->info[i].netdev == netdev) {
216 ctc_tty_info *info = &driver->info[i];
218 info->msr |= UART_MSR_DCD;
220 info->msr &= ~UART_MSR_DCD;
221 if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
222 tty_hangup(info->tty);
227 ctc_tty_netif_rx(struct sk_buff *skb)
230 ctc_tty_info *info = NULL;
234 if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
238 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
239 if (driver->info[i].netdev == skb->dev) {
240 info = &driver->info[i];
247 if ( !(info->tty) ) {
256 if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
260 skb_pull(skb, sizeof(__u32));
262 i = *((__u32 *)skb->data);
263 skb_pull(skb, sizeof(info->mcr));
264 if (i & UART_MCR_RTS) {
265 info->msr |= UART_MSR_CTS;
266 if (info->flags & CTC_ASYNC_CTS_FLOW)
267 info->tty->hw_stopped = 0;
269 info->msr &= ~UART_MSR_CTS;
270 if (info->flags & CTC_ASYNC_CTS_FLOW)
271 info->tty->hw_stopped = 1;
273 if (i & UART_MCR_DTR)
274 info->msr |= UART_MSR_DSR;
276 info->msr &= ~UART_MSR_DSR;
281 /* Try to deliver directly via tty-flip-buf if queue is empty */
282 if (skb_queue_empty(&info->rx_queue))
283 if (ctc_tty_try_read(info, skb))
285 /* Direct deliver failed or queue wasn't empty.
286 * Queue up for later dequeueing via timer-irq.
288 if (skb_queue_len(&info->rx_queue) < 50)
289 skb_queue_tail(&info->rx_queue, skb);
292 printk(KERN_DEBUG "ctctty: RX overrun\n");
294 /* Schedule dequeuing */
295 queue_task(&info->tq, &tq_immediate);
296 mark_bh(IMMEDIATE_BH);
300 ctc_tty_tint(ctc_tty_info * info)
302 struct sk_buff *skb = skb_dequeue(&info->tx_queue);
303 int stopped = (info->tty->hw_stopped || info->tty->stopped);
312 if (info->flags & CTC_ASYNC_TX_LINESTAT) {
313 int skb_res = info->netdev->hard_header_len +
314 sizeof(info->mcr) + sizeof(__u32);
315 /* If we must update line status,
316 * create an empty dummy skb and insert it.
319 skb_queue_head(&info->tx_queue, skb);
321 skb = dev_alloc_skb(skb_res);
324 "ctc_tty: Out of memory in %s%d tint\n",
325 CTC_TTY_NAME, info->line);
328 skb_reserve(skb, skb_res);
335 skb_queue_head(&info->tx_queue, skb);
340 printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
342 printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
344 memcpy(skb_push(skb, sizeof(info->mcr)), &info->mcr, sizeof(info->mcr));
345 memcpy(skb_push(skb, sizeof(__u32)), &ctc_tty_magic, sizeof(__u32));
346 rc = info->netdev->hard_start_xmit(skb, info->netdev);
348 skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
350 skb_queue_head(&info->tx_queue, skb);
354 /* The connection is not up yet, try again in one second. - wilder */
356 mod_timer(&info->flowtimer, jiffies+(HZ) );
361 struct tty_struct *tty = info->tty;
363 info->flags &= ~CTC_ASYNC_TX_LINESTAT;
367 wake_up_interruptible(&tty->write_wait);
370 return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
373 /************************************************************
377 * mostly "stolen" from original Linux-serial.c and friends.
379 ************************************************************/
382 ctc_tty_paranoia_check(ctc_tty_info * info, kdev_t device, const char *routine)
384 #ifdef MODEM_PARANOIA_CHECK
386 printk(KERN_WARNING "ctc_tty: null info_struct for (%d, %d) in %s\n",
387 MAJOR(device), MINOR(device), routine);
390 if (info->magic != CTC_ASYNC_MAGIC) {
391 printk(KERN_WARNING "ctc_tty: bad magic for info struct (%d, %d) in %s\n",
392 MAJOR(device), MINOR(device), routine);
400 ctc_tty_inject(ctc_tty_info *info, char c)
405 if (ctc_tty_shuttingdown)
407 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
409 skb = dev_alloc_skb(skb_res);
412 "ctc_tty: Out of memory in %s%d tx_inject\n",
413 CTC_TTY_NAME, info->line);
416 skb_reserve(skb, skb_res);
417 *(skb_put(skb, 1)) = c;
418 skb_queue_head(&info->tx_queue, skb);
419 queue_task(&info->tq, &tq_immediate);
420 mark_bh(IMMEDIATE_BH);
424 ctc_tty_transmit_status(ctc_tty_info *info)
426 if (ctc_tty_shuttingdown)
428 info->flags |= CTC_ASYNC_TX_LINESTAT;
429 queue_task(&info->tq, &tq_immediate);
430 mark_bh(IMMEDIATE_BH);
434 ctc_tty_change_speed(ctc_tty_info * info)
440 if (!info->tty || !info->tty->termios)
442 cflag = info->tty->termios->c_cflag;
444 quot = i = cflag & CBAUD;
448 info->tty->termios->c_cflag &= ~CBAUDEX;
453 info->mcr |= UART_MCR_DTR;
454 info->mcr |= UART_MCR_RTS;
455 ctc_tty_transmit_status(info);
457 info->mcr &= ~UART_MCR_DTR;
458 info->mcr &= ~UART_MCR_RTS;
459 ctc_tty_transmit_status(info);
463 /* CTS flow control flag and modem status interrupts */
464 if (cflag & CRTSCTS) {
465 info->flags |= CTC_ASYNC_CTS_FLOW;
467 info->flags &= ~CTC_ASYNC_CTS_FLOW;
469 info->flags &= ~CTC_ASYNC_CHECK_CD;
471 info->flags |= CTC_ASYNC_CHECK_CD;
476 ctc_tty_startup(ctc_tty_info * info)
478 if (info->flags & CTC_ASYNC_INITIALIZED)
480 #ifdef CTC_DEBUG_MODEM_OPEN
481 printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
484 * Now, initialize the UART
486 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
488 clear_bit(TTY_IO_ERROR, &info->tty->flags);
490 * and set the speed of the serial port
492 ctc_tty_change_speed(info);
494 info->flags |= CTC_ASYNC_INITIALIZED;
495 if (!(info->flags & CTC_ASYNC_NETDEV_OPEN))
496 info->netdev->open(info->netdev);
497 info->flags |= CTC_ASYNC_NETDEV_OPEN;
502 ctc_tty_stopdev(unsigned long data)
504 ctc_tty_info *info = (ctc_tty_info *)data;
506 if ((!info) || (!info->netdev) ||
507 (info->flags & CTC_ASYNC_INITIALIZED))
509 info->netdev->stop(info->netdev);
510 info->flags &= ~CTC_ASYNC_NETDEV_OPEN;
513 /* Run from the timer queue when we are flow blocked
514 * to kick start the bottom half - wilder */
516 ctc_tty_startupbh(unsigned long data)
518 ctc_tty_info *info = (ctc_tty_info *)data;
519 if (( !ctc_tty_shuttingdown) && info) {
520 queue_task(&info->tq, &tq_immediate);
521 mark_bh(IMMEDIATE_BH);
526 * This routine will shutdown a serial port; interrupts are disabled, and
527 * DTR is dropped if the hangup on close termio flag is on.
530 ctc_tty_shutdown(ctc_tty_info * info)
532 if (!(info->flags & CTC_ASYNC_INITIALIZED))
534 #ifdef CTC_DEBUG_MODEM_OPEN
535 printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
537 info->msr &= ~UART_MSR_RI;
538 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
539 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
541 set_bit(TTY_IO_ERROR, &info->tty->flags);
542 mod_timer(&info->stoptimer, jiffies + (10 * HZ));
543 skb_queue_purge(&info->tx_queue);
544 skb_queue_purge(&info->rx_queue);
545 info->flags &= ~CTC_ASYNC_INITIALIZED;
548 /* ctc_tty_write() is the main send-routine. It is called from the upper
549 * levels within the kernel to perform sending data. Depending on the
550 * online-flag it either directs output to the at-command-interpreter or
551 * to the lower level. Additional tasks done here:
552 * - If online, check for escape-sequence (+++)
553 * - If sending audio-data, call ctc_tty_DLEdown() to parse DLE-codes.
554 * - If receiving audio-data, call ctc_tty_end_vrx() to abort if needed.
555 * - If dialing, abort dial.
558 ctc_tty_write(struct tty_struct *tty, int from_user, const u_char * buf, int count)
562 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
564 if (ctc_tty_shuttingdown)
566 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_write"))
573 down(&info->write_sem);
578 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
582 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
584 skb = dev_alloc_skb(skb_res + c);
587 "ctc_tty: Out of memory in %s%d write\n",
588 CTC_TTY_NAME, info->line);
591 skb_reserve(skb, skb_res);
593 copy_from_user(skb_put(skb, c), buf, c);
595 memcpy(skb_put(skb, c), buf, c);
596 skb_queue_tail(&info->tx_queue, skb);
601 if (skb_queue_len(&info->tx_queue)) {
602 info->lsr &= ~UART_LSR_TEMT;
603 queue_task(&info->tq, &tq_immediate);
604 mark_bh(IMMEDIATE_BH);
607 up(&info->write_sem);
612 ctc_tty_write_room(struct tty_struct *tty)
614 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
616 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_write_room"))
620 if (skb_queue_len(&info->tx_queue) > 10 ) {
624 return CTC_TTY_XMIT_SIZE;
628 ctc_tty_chars_in_buffer(struct tty_struct *tty)
630 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
632 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_chars_in_buffer"))
638 ctc_tty_flush_buffer(struct tty_struct *tty)
646 restore_flags(flags);
649 info = (ctc_tty_info *) tty->driver_data;
650 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_flush_buffer")) {
651 restore_flags(flags);
654 skb_queue_purge(&info->tx_queue);
655 info->lsr |= UART_LSR_TEMT;
656 restore_flags(flags);
661 ctc_tty_flush_chars(struct tty_struct *tty)
663 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
665 if (ctc_tty_shuttingdown)
667 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_flush_chars"))
669 if (tty->stopped || tty->hw_stopped || (!skb_queue_len(&info->tx_queue)))
671 queue_task(&info->tq, &tq_immediate);
672 mark_bh(IMMEDIATE_BH);
676 * ------------------------------------------------------------
679 * This routine is called by the upper-layer tty layer to signal that
680 * incoming characters should be throttled.
681 * ------------------------------------------------------------
684 ctc_tty_throttle(struct tty_struct *tty)
686 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
688 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_throttle"))
690 info->mcr &= ~UART_MCR_RTS;
692 ctc_tty_inject(info, STOP_CHAR(tty));
693 ctc_tty_transmit_status(info);
697 ctc_tty_unthrottle(struct tty_struct *tty)
699 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
701 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_unthrottle"))
703 info->mcr |= UART_MCR_RTS;
705 ctc_tty_inject(info, START_CHAR(tty));
706 ctc_tty_transmit_status(info);
710 * ------------------------------------------------------------
711 * ctc_tty_ioctl() and friends
712 * ------------------------------------------------------------
716 * ctc_tty_get_lsr_info - get line status register info
718 * Purpose: Let user call ioctl() to get info when the UART physically
719 * is emptied. On bus types like RS485, the transmitter must
720 * release the bus after transmitting. This must be done when
721 * the transmit shift register is empty, not be done when the
722 * transmit holding register is empty. This functionality
723 * allows RS485 driver to be written in user space.
726 ctc_tty_get_lsr_info(ctc_tty_info * info, uint * value)
735 restore_flags(flags);
736 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
737 put_user(result, (uint *) value);
743 ctc_tty_get_ctc_tty_info(ctc_tty_info * info, uint * value)
754 restore_flags(flags);
755 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
756 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
757 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
758 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
759 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
760 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
761 put_user(result, (uint *) value);
766 ctc_tty_set_ctc_tty_info(ctc_tty_info * info, uint cmd, uint * value)
769 int old_mcr = info->mcr & (UART_MCR_RTS | UART_MCR_DTR);
771 get_user(arg, (uint *) value);
774 #ifdef CTC_DEBUG_MODEM_IOCTL
775 printk(KERN_DEBUG "%s%d ioctl TIOCMBIS\n", CTC_TTY_NAME,
779 info->mcr |= UART_MCR_RTS;
781 info->mcr |= UART_MCR_DTR;
784 #ifdef CTC_DEBUG_MODEM_IOCTL
785 printk(KERN_DEBUG "%s%d ioctl TIOCMBIC\n", CTC_TTY_NAME,
789 info->mcr &= ~UART_MCR_RTS;
791 info->mcr &= ~UART_MCR_DTR;
794 #ifdef CTC_DEBUG_MODEM_IOCTL
795 printk(KERN_DEBUG "%s%d ioctl TIOCMSET\n", CTC_TTY_NAME,
798 info->mcr = ((info->mcr & ~(UART_MCR_RTS | UART_MCR_DTR))
799 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
800 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
805 if ((info->mcr & (UART_MCR_RTS | UART_MCR_DTR)) != old_mcr)
806 ctc_tty_transmit_status(info);
811 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
814 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
818 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_ioctl"))
820 if (tty->flags & (1 << TTY_IO_ERROR))
823 case TCSBRK: /* SVID version: non-zero arg --> no break */
824 #ifdef CTC_DEBUG_MODEM_IOCTL
825 printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
827 retval = tty_check_change(tty);
830 tty_wait_until_sent(tty, 0);
832 case TCSBRKP: /* support for POSIX tcsendbreak() */
833 #ifdef CTC_DEBUG_MODEM_IOCTL
834 printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
836 retval = tty_check_change(tty);
839 tty_wait_until_sent(tty, 0);
842 #ifdef CTC_DEBUG_MODEM_IOCTL
843 printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
846 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long));
849 put_user(C_CLOCAL(tty) ? 1 : 0, (ulong *) arg);
852 #ifdef CTC_DEBUG_MODEM_IOCTL
853 printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
856 error = verify_area(VERIFY_READ, (void *) arg, sizeof(long));
859 get_user(arg, (ulong *) arg);
860 tty->termios->c_cflag =
861 ((tty->termios->c_cflag & ~CLOCAL) |
865 #ifdef CTC_DEBUG_MODEM_IOCTL
866 printk(KERN_DEBUG "%s%d ioctl TIOCMGET\n", CTC_TTY_NAME,
869 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(uint));
872 return ctc_tty_get_ctc_tty_info(info, (uint *) arg);
876 error = verify_area(VERIFY_READ, (void *) arg, sizeof(uint));
879 return ctc_tty_set_ctc_tty_info(info, cmd, (uint *) arg);
880 case TIOCSERGETLSR: /* Get line status register */
881 #ifdef CTC_DEBUG_MODEM_IOCTL
882 printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
885 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(uint));
889 return ctc_tty_get_lsr_info(info, (uint *) arg);
891 #ifdef CTC_DEBUG_MODEM_IOCTL
892 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
893 CTC_TTY_NAME, info->line);
901 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
903 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
904 unsigned int cflag = tty->termios->c_cflag;
906 ctc_tty_change_speed(info);
908 /* Handle transition to B0 */
909 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
910 info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
911 ctc_tty_transmit_status(info);
914 /* Handle transition from B0 to other */
915 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
916 info->mcr |= UART_MCR_DTR;
917 if (!(tty->termios->c_cflag & CRTSCTS) ||
918 !test_bit(TTY_THROTTLED, &tty->flags)) {
919 info->mcr |= UART_MCR_RTS;
921 ctc_tty_transmit_status(info);
924 /* Handle turning off CRTSCTS */
925 if ((old_termios->c_cflag & CRTSCTS) &&
926 !(tty->termios->c_cflag & CRTSCTS))
931 * ------------------------------------------------------------
932 * ctc_tty_open() and friends
933 * ------------------------------------------------------------
936 ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
938 DECLARE_WAITQUEUE(wait, NULL);
944 * If the device is in the middle of being closed, then block
945 * until it's done, and then try again.
947 if (tty_hung_up_p(filp) ||
948 (info->flags & CTC_ASYNC_CLOSING)) {
949 if (info->flags & CTC_ASYNC_CLOSING)
950 interruptible_sleep_on(&info->close_wait);
951 #ifdef MODEM_DO_RESTART
952 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
961 * If non-blocking mode is set, then make the check up front
964 if ((filp->f_flags & O_NONBLOCK) ||
965 (tty->flags & (1 << TTY_IO_ERROR))) {
966 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
969 if (tty->termios->c_cflag & CLOCAL)
972 * Block waiting for the carrier detect and the line to become
973 * free (i.e., not in use by the callout). While we are in
974 * this loop, info->count is dropped by one, so that
975 * ctc_tty_close() knows when to free things. We restore it upon
976 * exit, either normal or abnormal.
979 add_wait_queue(&info->open_wait, &wait);
980 #ifdef CTC_DEBUG_MODEM_OPEN
981 printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
982 CTC_TTY_NAME, info->line, info->count);
986 if (!(tty_hung_up_p(filp)))
988 restore_flags(flags);
989 info->blocked_open++;
991 set_current_state(TASK_INTERRUPTIBLE);
992 if (tty_hung_up_p(filp) ||
993 !(info->flags & CTC_ASYNC_INITIALIZED)) {
994 #ifdef MODEM_DO_RESTART
995 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
998 retval = -ERESTARTSYS;
1004 if (!(info->flags & CTC_ASYNC_CLOSING) &&
1005 (do_clocal || (info->msr & UART_MSR_DCD))) {
1008 if (signal_pending(current)) {
1009 retval = -ERESTARTSYS;
1012 #ifdef CTC_DEBUG_MODEM_OPEN
1013 printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
1014 CTC_TTY_NAME, info->line, info->count);
1018 current->state = TASK_RUNNING;
1019 remove_wait_queue(&info->open_wait, &wait);
1020 if (!tty_hung_up_p(filp))
1022 info->blocked_open--;
1023 #ifdef CTC_DEBUG_MODEM_OPEN
1024 printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
1025 CTC_TTY_NAME, info->line, info->count);
1029 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
1034 * This routine is called whenever a serial port is opened. It
1035 * enables interrupts for a serial port, linking in its async structure into
1036 * the IRQ chain. It also performs the serial-specific
1037 * initialization for the tty structure.
1040 ctc_tty_open(struct tty_struct *tty, struct file *filp)
1043 unsigned long saveflags;
1047 line = MINOR(tty->device) - tty->driver.minor_start;
1048 if (line < 0 || line > CTC_TTY_MAX_DEVICES)
1050 info = &driver->info[line];
1051 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_open"))
1055 #ifdef CTC_DEBUG_MODEM_OPEN
1056 printk(KERN_DEBUG "ctc_tty_open %s%d, count = %d\n", tty->driver.name,
1057 info->line, info->count);
1059 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1061 tty->driver_data = info;
1063 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1065 * Start up serial port
1067 retval = ctc_tty_startup(info);
1069 #ifdef CTC_DEBUG_MODEM_OPEN
1070 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
1074 retval = ctc_tty_block_til_ready(tty, filp, info);
1076 #ifdef CTC_DEBUG_MODEM_OPEN
1077 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
1081 if ((info->count == 1) && (info->flags & CTC_ASYNC_SPLIT_TERMIOS)) {
1082 *tty->termios = info->normal_termios;
1083 ctc_tty_change_speed(info);
1085 #ifdef CTC_DEBUG_MODEM_OPEN
1086 printk(KERN_DEBUG "ctc_tty_open %s%d successful...\n", CTC_TTY_NAME, info->line);
1092 ctc_tty_close(struct tty_struct *tty, struct file *filp)
1094 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
1095 unsigned long saveflags;
1099 if (!info || ctc_tty_paranoia_check(info, tty->device, "ctc_tty_close"))
1103 if (tty_hung_up_p(filp)) {
1104 restore_flags(flags);
1105 #ifdef CTC_DEBUG_MODEM_OPEN
1106 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
1110 if ((tty->count == 1) && (info->count != 1)) {
1112 * Uh, oh. tty->count is 1, which means that the tty
1113 * structure will be freed. Info->count should always
1114 * be one in these conditions. If it's greater than
1115 * one, we've got real problems, since it means the
1116 * serial port won't be shutdown.
1118 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
1119 "info->count is %d\n", info->count);
1122 if (--info->count < 0) {
1123 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
1124 CTC_TTY_NAME, info->line, info->count);
1128 restore_flags(flags);
1129 #ifdef CTC_DEBUG_MODEM_OPEN
1130 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1134 info->flags |= CTC_ASYNC_CLOSING;
1136 * Save the termios structure, since this port may have
1137 * separate termios for callout and dialin.
1139 if (info->flags & CTC_ASYNC_NORMAL_ACTIVE)
1140 info->normal_termios = *tty->termios;
1144 * At this point we stop accepting input. To do this, we
1145 * disable the receive line status interrupts, and tell the
1146 * interrupt driver to stop checking the data ready bit in the
1147 * line status register.
1149 if (info->flags & CTC_ASYNC_INITIALIZED) {
1150 tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
1152 * Before we drop DTR, make sure the UART transmitter
1153 * has completely drained; this is especially
1154 * important if there is a transmit FIFO!
1156 timeout = jiffies + HZ;
1157 while (!(info->lsr & UART_LSR_TEMT)) {
1158 set_current_state(TASK_INTERRUPTIBLE);
1159 schedule_timeout(20);
1160 if (time_after(jiffies,timeout))
1164 ctc_tty_shutdown(info);
1165 if (tty->driver.flush_buffer)
1166 tty->driver.flush_buffer(tty);
1167 tty_ldisc_flush(tty);
1168 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1170 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1172 if (info->blocked_open) {
1173 set_current_state(TASK_INTERRUPTIBLE);
1174 schedule_timeout(50);
1175 wake_up_interruptible(&info->open_wait);
1177 info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
1178 wake_up_interruptible(&info->close_wait);
1179 restore_flags(flags);
1180 #ifdef CTC_DEBUG_MODEM_OPEN
1181 printk(KERN_DEBUG "ctc_tty_close normal exit\n");
1186 * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1189 ctc_tty_hangup(struct tty_struct *tty)
1191 ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1192 unsigned long saveflags;
1194 if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_hangup"))
1196 ctc_tty_shutdown(info);
1198 info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1199 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1201 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1202 wake_up_interruptible(&info->open_wait);
1207 * For all online tty's, try sending data to
1211 ctc_tty_task(ctc_tty_info *info)
1213 unsigned long saveflags;
1216 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1217 if ((!ctc_tty_shuttingdown) && info) {
1218 again = ctc_tty_tint(info);
1220 info->lsr |= UART_LSR_TEMT;
1221 again |= ctc_tty_readmodem(info);
1223 queue_task(&info->tq, &tq_immediate);
1224 mark_bh(IMMEDIATE_BH);
1227 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1235 struct tty_driver *device;
1237 driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1238 if (driver == NULL) {
1239 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1242 memset(driver, 0, sizeof(ctc_tty_driver));
1243 device = &driver->ctc_tty_device;
1245 device->magic = TTY_DRIVER_MAGIC;
1246 device->name = ctc_ttyname;
1247 device->major = CTC_TTY_MAJOR;
1248 device->minor_start = 0;
1249 device->num = CTC_TTY_MAX_DEVICES;
1250 device->type = TTY_DRIVER_TYPE_SERIAL;
1251 device->subtype = CTC_SERIAL_TYPE_NORMAL;
1252 device->init_termios = tty_std_termios;
1253 device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1254 device->flags = TTY_DRIVER_REAL_RAW;
1255 device->refcount = &driver->refcount;
1256 device->table = driver->modem_table;
1257 device->termios = driver->modem_termios;
1258 device->termios_locked = driver->modem_termios_locked;
1259 device->open = ctc_tty_open;
1260 device->close = ctc_tty_close;
1261 device->write = ctc_tty_write;
1262 device->put_char = NULL;
1263 device->flush_chars = ctc_tty_flush_chars;
1264 device->write_room = ctc_tty_write_room;
1265 device->chars_in_buffer = ctc_tty_chars_in_buffer;
1266 device->flush_buffer = ctc_tty_flush_buffer;
1267 device->ioctl = ctc_tty_ioctl;
1268 device->throttle = ctc_tty_throttle;
1269 device->unthrottle = ctc_tty_unthrottle;
1270 device->set_termios = ctc_tty_set_termios;
1271 device->stop = NULL;
1272 device->start = NULL;
1273 device->hangup = ctc_tty_hangup;
1274 device->driver_name = "ctc_tty";
1276 if (tty_register_driver(device)) {
1277 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1281 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1282 info = &driver->info[i];
1283 init_MUTEX(&info->write_sem);
1284 #if LINUX_VERSION_CODE >= 0x020400
1285 INIT_LIST_HEAD(&info->tq.list);
1287 info->tq.next = NULL;
1290 info->tq.routine = (void *)(void *)ctc_tty_task;
1291 info->tq.data = info;
1292 info->magic = CTC_ASYNC_MAGIC;
1296 info->blocked_open = 0;
1297 info->normal_termios = device->init_termios;
1298 init_waitqueue_head(&info->open_wait);
1299 init_waitqueue_head(&info->close_wait);
1300 skb_queue_head_init(&info->tx_queue);
1301 skb_queue_head_init(&info->rx_queue);
1302 init_timer(&info->stoptimer);
1303 info->stoptimer.function = ctc_tty_stopdev;
1304 info->stoptimer.data = (unsigned long)info;
1305 init_timer(&info->flowtimer);
1306 info->flowtimer.function = ctc_tty_startupbh;
1307 info->flowtimer.data = (unsigned long)info;
1308 info->mcr = UART_MCR_RTS;
1314 ctc_tty_register_netdev(net_device *dev) {
1319 if ((!dev) || (!dev->name)) {
1321 "ctc_tty_register_netdev called "
1322 "with NULL dev or NULL dev-name\n");
1325 for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1326 ttynum = simple_strtoul(p, &err, 0);
1327 if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1330 "ctc_tty_register_netdev called "
1331 "with number in name '%s'\n", dev->name);
1334 if (driver->info[ttynum].netdev) {
1336 "ctc_tty_register_netdev called "
1337 "for already registered device '%s'\n",
1341 driver->info[ttynum].netdev = dev;
1346 ctc_tty_unregister_netdev(net_device *dev) {
1348 unsigned long saveflags;
1349 ctc_tty_info *info = NULL;
1351 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1352 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
1353 if (driver->info[i].netdev == dev) {
1354 info = &driver->info[i];
1358 info->netdev = NULL;
1359 skb_queue_purge(&info->tx_queue);
1360 skb_queue_purge(&info->rx_queue);
1362 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1366 ctc_tty_cleanup(int final) {
1367 unsigned long saveflags;
1369 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1370 ctc_tty_shuttingdown = 1;
1377 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
1378 driver->info[i].tq.routine = NULL;
1379 tty_unregister_driver(&driver->ctc_tty_device);
1381 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);