2 * $Id: ctctty.c,v 1.17 2004/03/31 17:06:34 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>
34 #define CTC_TTY_MAJOR 43
35 #define CTC_TTY_MAX_DEVICES 64
37 #define CTC_ASYNC_MAGIC 0x49344C01 /* for paranoia-checking */
38 #define CTC_ASYNC_INITIALIZED 0x80000000 /* port was initialized */
39 #define CTC_ASYNC_NORMAL_ACTIVE 0x20000000 /* Normal device active */
40 #define CTC_ASYNC_CLOSING 0x08000000 /* Serial port is closing */
41 #define CTC_ASYNC_CTS_FLOW 0x04000000 /* Do CTS flow control */
42 #define CTC_ASYNC_CHECK_CD 0x02000000 /* i.e., CLOCAL */
43 #define CTC_ASYNC_HUP_NOTIFY 0x0001 /* Notify tty on hangups/closes */
44 #define CTC_ASYNC_NETDEV_OPEN 0x0002 /* Underlying netdev is open */
45 #define CTC_ASYNC_TX_LINESTAT 0x0004 /* Must send line status */
46 #define CTC_ASYNC_SPLIT_TERMIOS 0x0008 /* Sep. termios for dialin/out */
47 #define CTC_TTY_XMIT_SIZE 1024 /* Default bufsize for write */
48 #define CTC_SERIAL_XMIT_MAX 4000 /* Maximum bufsize for write */
50 /* Private data (similar to async_struct in <linux/serial.h>) */
53 int flags; /* defined in tty.h */
54 int mcr; /* Modem control register */
55 int msr; /* Modem status register */
56 int lsr; /* Line status register */
58 int count; /* # of fd on device */
59 int blocked_open; /* # of blocked opens */
60 struct net_device *netdev;
61 struct sk_buff_head tx_queue; /* transmit queue */
62 struct sk_buff_head rx_queue; /* receive queue */
63 struct tty_struct *tty; /* Pointer to corresponding tty */
64 wait_queue_head_t open_wait;
65 wait_queue_head_t close_wait;
66 struct semaphore write_sem;
67 struct tasklet_struct tasklet;
68 struct timer_list stoptimer;
71 /* Description of one CTC-tty */
73 struct tty_driver *ctc_tty_device; /* tty-device */
74 ctc_tty_info info[CTC_TTY_MAX_DEVICES]; /* Private data */
77 static ctc_tty_driver *driver;
79 /* Leave this unchanged unless you know what you do! */
80 #define MODEM_PARANOIA_CHECK
81 #define MODEM_DO_RESTART
83 #define CTC_TTY_NAME "ctctty"
85 static __u32 ctc_tty_magic = CTC_ASYNC_MAGIC;
86 static int ctc_tty_shuttingdown = 0;
88 static spinlock_t ctc_tty_lock;
90 /* ctc_tty_try_read() is called from within ctc_tty_rcv_skb()
91 * to stuff incoming data directly into a tty's flip-buffer. If the
92 * flip buffer is full, the packet gets queued up.
96 * 0 = Failure, data has to be buffered and later processed by
97 * ctc_tty_readmodem().
100 ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
104 struct tty_struct *tty;
106 if ((tty = info->tty)) {
107 if (info->mcr & UART_MCR_RTS) {
108 c = TTY_FLIPBUF_SIZE - tty->flip.count;
111 memcpy(tty->flip.char_buf_ptr, skb->data, len);
112 memset(tty->flip.flag_buf_ptr, 0, len);
113 tty->flip.count += len;
114 tty->flip.char_buf_ptr += len;
115 tty->flip.flag_buf_ptr += len;
116 tty_flip_buffer_push(tty);
125 /* ctc_tty_readmodem() is called periodically from within timer-interrupt.
126 * It tries getting received data from the receive queue an stuff it into
127 * the tty's flip-buffer.
130 ctc_tty_readmodem(ctc_tty_info *info)
133 struct tty_struct *tty;
135 if ((tty = info->tty)) {
136 if (info->mcr & UART_MCR_RTS) {
137 int c = TTY_FLIPBUF_SIZE - tty->flip.count;
140 if ((c > 0) && (skb = skb_dequeue(&info->rx_queue))) {
144 memcpy(tty->flip.char_buf_ptr, skb->data, len);
146 memset(tty->flip.flag_buf_ptr, 0, len);
147 tty->flip.count += len;
148 tty->flip.char_buf_ptr += len;
149 tty->flip.flag_buf_ptr += len;
150 tty_flip_buffer_push(tty);
152 skb_queue_head(&info->rx_queue, skb);
155 ret = skb_queue_len(&info->rx_queue);
164 ctc_tty_setcarrier(struct net_device *netdev, int on)
168 if ((!driver) || ctc_tty_shuttingdown)
170 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
171 if (driver->info[i].netdev == netdev) {
172 ctc_tty_info *info = &driver->info[i];
174 info->msr |= UART_MSR_DCD;
176 info->msr &= ~UART_MSR_DCD;
177 if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
178 tty_hangup(info->tty);
183 ctc_tty_netif_rx(struct sk_buff *skb)
186 ctc_tty_info *info = NULL;
190 if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
194 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
195 if (driver->info[i].netdev == skb->dev) {
196 info = &driver->info[i];
207 if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
211 skb_pull(skb, sizeof(__u32));
213 i = *((int *)skb->data);
214 skb_pull(skb, sizeof(info->mcr));
215 if (i & UART_MCR_RTS) {
216 info->msr |= UART_MSR_CTS;
217 if (info->flags & CTC_ASYNC_CTS_FLOW)
218 info->tty->hw_stopped = 0;
220 info->msr &= ~UART_MSR_CTS;
221 if (info->flags & CTC_ASYNC_CTS_FLOW)
222 info->tty->hw_stopped = 1;
224 if (i & UART_MCR_DTR)
225 info->msr |= UART_MSR_DSR;
227 info->msr &= ~UART_MSR_DSR;
232 /* Try to deliver directly via tty-flip-buf if queue is empty */
233 if (skb_queue_empty(&info->rx_queue))
234 if (ctc_tty_try_read(info, skb))
236 /* Direct deliver failed or queue wasn't empty.
237 * Queue up for later dequeueing via timer-irq.
239 skb_queue_tail(&info->rx_queue, skb);
240 /* Schedule dequeuing */
241 tasklet_schedule(&info->tasklet);
245 ctc_tty_tint(ctc_tty_info * info)
247 struct sk_buff *skb = skb_dequeue(&info->tx_queue);
248 int stopped = (info->tty->hw_stopped || info->tty->stopped);
257 if (info->flags & CTC_ASYNC_TX_LINESTAT) {
258 int skb_res = info->netdev->hard_header_len +
259 sizeof(info->mcr) + sizeof(__u32);
260 /* If we must update line status,
261 * create an empty dummy skb and insert it.
264 skb_queue_head(&info->tx_queue, skb);
266 skb = dev_alloc_skb(skb_res);
269 "ctc_tty: Out of memory in %s%d tint\n",
270 CTC_TTY_NAME, info->line);
273 skb_reserve(skb, skb_res);
280 skb_queue_head(&info->tx_queue, skb);
285 printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
287 printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
289 memcpy(skb_push(skb, sizeof(info->mcr)), &info->mcr, sizeof(info->mcr));
290 memcpy(skb_push(skb, sizeof(__u32)), &ctc_tty_magic, sizeof(__u32));
291 rc = info->netdev->hard_start_xmit(skb, info->netdev);
293 skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
295 skb_queue_head(&info->tx_queue, skb);
299 struct tty_struct *tty = info->tty;
301 info->flags &= ~CTC_ASYNC_TX_LINESTAT;
303 if (wake && (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
304 tty->ldisc.write_wakeup)
305 (tty->ldisc.write_wakeup)(tty);
306 wake_up_interruptible(&tty->write_wait);
309 return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
312 /************************************************************
316 * mostly "stolen" from original Linux-serial.c and friends.
318 ************************************************************/
321 ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
323 #ifdef MODEM_PARANOIA_CHECK
325 printk(KERN_WARNING "ctc_tty: null info_struct for %s in %s\n",
329 if (info->magic != CTC_ASYNC_MAGIC) {
330 printk(KERN_WARNING "ctc_tty: bad magic for info struct %s in %s\n",
339 ctc_tty_inject(ctc_tty_info *info, char c)
344 if (ctc_tty_shuttingdown)
346 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
348 skb = dev_alloc_skb(skb_res);
351 "ctc_tty: Out of memory in %s%d tx_inject\n",
352 CTC_TTY_NAME, info->line);
355 skb_reserve(skb, skb_res);
356 *(skb_put(skb, 1)) = c;
357 skb_queue_head(&info->tx_queue, skb);
358 tasklet_schedule(&info->tasklet);
362 ctc_tty_transmit_status(ctc_tty_info *info)
364 if (ctc_tty_shuttingdown)
366 info->flags |= CTC_ASYNC_TX_LINESTAT;
367 tasklet_schedule(&info->tasklet);
371 ctc_tty_change_speed(ctc_tty_info * info)
377 if (!info->tty || !info->tty->termios)
379 cflag = info->tty->termios->c_cflag;
381 quot = i = cflag & CBAUD;
385 info->tty->termios->c_cflag &= ~CBAUDEX;
390 info->mcr |= UART_MCR_DTR;
391 info->mcr |= UART_MCR_RTS;
392 ctc_tty_transmit_status(info);
394 info->mcr &= ~UART_MCR_DTR;
395 info->mcr &= ~UART_MCR_RTS;
396 ctc_tty_transmit_status(info);
400 /* CTS flow control flag and modem status interrupts */
401 if (cflag & CRTSCTS) {
402 info->flags |= CTC_ASYNC_CTS_FLOW;
404 info->flags &= ~CTC_ASYNC_CTS_FLOW;
406 info->flags &= ~CTC_ASYNC_CHECK_CD;
408 info->flags |= CTC_ASYNC_CHECK_CD;
413 ctc_tty_startup(ctc_tty_info * info)
415 if (info->flags & CTC_ASYNC_INITIALIZED)
417 #ifdef CTC_DEBUG_MODEM_OPEN
418 printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
421 * Now, initialize the UART
423 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
425 clear_bit(TTY_IO_ERROR, &info->tty->flags);
427 * and set the speed of the serial port
429 ctc_tty_change_speed(info);
431 info->flags |= CTC_ASYNC_INITIALIZED;
432 if (!(info->flags & CTC_ASYNC_NETDEV_OPEN))
433 info->netdev->open(info->netdev);
434 info->flags |= CTC_ASYNC_NETDEV_OPEN;
439 ctc_tty_stopdev(unsigned long data)
441 ctc_tty_info *info = (ctc_tty_info *)data;
443 if ((!info) || (!info->netdev) ||
444 (info->flags & CTC_ASYNC_INITIALIZED))
446 info->netdev->stop(info->netdev);
447 info->flags &= ~CTC_ASYNC_NETDEV_OPEN;
451 * This routine will shutdown a serial port; interrupts are disabled, and
452 * DTR is dropped if the hangup on close termio flag is on.
455 ctc_tty_shutdown(ctc_tty_info * info)
457 if (!(info->flags & CTC_ASYNC_INITIALIZED))
459 #ifdef CTC_DEBUG_MODEM_OPEN
460 printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
462 info->msr &= ~UART_MSR_RI;
463 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
464 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
466 set_bit(TTY_IO_ERROR, &info->tty->flags);
467 mod_timer(&info->stoptimer, jiffies + (10 * HZ));
468 skb_queue_purge(&info->tx_queue);
469 skb_queue_purge(&info->rx_queue);
470 info->flags &= ~CTC_ASYNC_INITIALIZED;
473 /* ctc_tty_write() is the main send-routine. It is called from the upper
474 * levels within the kernel to perform sending data. Depending on the
475 * online-flag it either directs output to the at-command-interpreter or
476 * to the lower level. Additional tasks done here:
477 * - If online, check for escape-sequence (+++)
478 * - If sending audio-data, call ctc_tty_DLEdown() to parse DLE-codes.
479 * - If receiving audio-data, call ctc_tty_end_vrx() to abort if needed.
480 * - If dialing, abort dial.
483 ctc_tty_write(struct tty_struct *tty, int from_user, const u_char * buf, int count)
487 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
489 if (ctc_tty_shuttingdown)
491 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write"))
498 down(&info->write_sem);
503 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
507 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
509 skb = dev_alloc_skb(skb_res + c);
512 "ctc_tty: Out of memory in %s%d write\n",
513 CTC_TTY_NAME, info->line);
516 skb_reserve(skb, skb_res);
518 copy_from_user(skb_put(skb, c), buf, c);
520 memcpy(skb_put(skb, c), buf, c);
521 skb_queue_tail(&info->tx_queue, skb);
526 if (skb_queue_len(&info->tx_queue)) {
527 info->lsr &= ~UART_LSR_TEMT;
528 tasklet_schedule(&info->tasklet);
531 up(&info->write_sem);
536 ctc_tty_write_room(struct tty_struct *tty)
538 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
540 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write_room"))
542 return CTC_TTY_XMIT_SIZE;
546 ctc_tty_chars_in_buffer(struct tty_struct *tty)
548 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
550 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_chars_in_buffer"))
556 ctc_tty_flush_buffer(struct tty_struct *tty)
563 spin_lock_irqsave(&ctc_tty_lock, flags);
564 info = (ctc_tty_info *) tty->driver_data;
565 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_buffer")) {
566 spin_unlock_irqrestore(&ctc_tty_lock, flags);
569 skb_queue_purge(&info->tx_queue);
570 info->lsr |= UART_LSR_TEMT;
571 spin_unlock_irqrestore(&ctc_tty_lock, flags);
572 wake_up_interruptible(&tty->write_wait);
573 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
574 tty->ldisc.write_wakeup)
575 (tty->ldisc.write_wakeup) (tty);
579 ctc_tty_flush_chars(struct tty_struct *tty)
581 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
583 if (ctc_tty_shuttingdown)
585 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
587 if (tty->stopped || tty->hw_stopped || (!skb_queue_len(&info->tx_queue)))
589 tasklet_schedule(&info->tasklet);
593 * ------------------------------------------------------------
596 * This routine is called by the upper-layer tty layer to signal that
597 * incoming characters should be throttled.
598 * ------------------------------------------------------------
601 ctc_tty_throttle(struct tty_struct *tty)
603 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
605 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_throttle"))
607 info->mcr &= ~UART_MCR_RTS;
609 ctc_tty_inject(info, STOP_CHAR(tty));
610 ctc_tty_transmit_status(info);
614 ctc_tty_unthrottle(struct tty_struct *tty)
616 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
618 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_unthrottle"))
620 info->mcr |= UART_MCR_RTS;
622 ctc_tty_inject(info, START_CHAR(tty));
623 ctc_tty_transmit_status(info);
627 * ------------------------------------------------------------
628 * ctc_tty_ioctl() and friends
629 * ------------------------------------------------------------
633 * ctc_tty_get_lsr_info - get line status register info
635 * Purpose: Let user call ioctl() to get info when the UART physically
636 * is emptied. On bus types like RS485, the transmitter must
637 * release the bus after transmitting. This must be done when
638 * the transmit shift register is empty, not be done when the
639 * transmit holding register is empty. This functionality
640 * allows RS485 driver to be written in user space.
643 ctc_tty_get_lsr_info(ctc_tty_info * info, uint * value)
649 spin_lock_irqsave(&ctc_tty_lock, flags);
651 spin_unlock_irqrestore(&ctc_tty_lock, flags);
652 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
653 put_user(result, (uint *) value);
658 static int ctc_tty_tiocmget(struct tty_struct *tty, struct file *file)
660 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
666 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
668 if (tty->flags & (1 << TTY_IO_ERROR))
672 spin_lock_irqsave(&ctc_tty_lock, flags);
674 spin_unlock_irqrestore(&ctc_tty_lock, flags);
675 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
676 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
677 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
678 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
679 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
680 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
685 ctc_tty_tiocmset(struct tty_struct *tty, struct file *file,
686 unsigned int set, unsigned int clear)
688 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
690 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
692 if (tty->flags & (1 << TTY_IO_ERROR))
696 info->mcr |= UART_MCR_RTS;
698 info->mcr |= UART_MCR_DTR;
700 if (clear & TIOCM_RTS)
701 info->mcr &= ~UART_MCR_RTS;
702 if (clear & TIOCM_DTR)
703 info->mcr &= ~UART_MCR_DTR;
705 if ((set | clear) & (TIOCM_RTS|TIOCM_DTR))
706 ctc_tty_transmit_status(info);
711 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
714 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
718 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
720 if (tty->flags & (1 << TTY_IO_ERROR))
723 case TCSBRK: /* SVID version: non-zero arg --> no break */
724 #ifdef CTC_DEBUG_MODEM_IOCTL
725 printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
727 retval = tty_check_change(tty);
730 tty_wait_until_sent(tty, 0);
732 case TCSBRKP: /* support for POSIX tcsendbreak() */
733 #ifdef CTC_DEBUG_MODEM_IOCTL
734 printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
736 retval = tty_check_change(tty);
739 tty_wait_until_sent(tty, 0);
742 #ifdef CTC_DEBUG_MODEM_IOCTL
743 printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
746 error = put_user(C_CLOCAL(tty) ? 1 : 0, (ulong *) arg);
749 #ifdef CTC_DEBUG_MODEM_IOCTL
750 printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
753 error = get_user(arg, (ulong *) arg);
756 tty->termios->c_cflag =
757 ((tty->termios->c_cflag & ~CLOCAL) |
760 case TIOCSERGETLSR: /* Get line status register */
761 #ifdef CTC_DEBUG_MODEM_IOCTL
762 printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
765 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(uint));
769 return ctc_tty_get_lsr_info(info, (uint *) arg);
771 #ifdef CTC_DEBUG_MODEM_IOCTL
772 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
773 CTC_TTY_NAME, info->line);
781 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
783 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
784 unsigned int cflag = tty->termios->c_cflag;
786 ctc_tty_change_speed(info);
788 /* Handle transition to B0 */
789 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
790 info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
791 ctc_tty_transmit_status(info);
794 /* Handle transition from B0 to other */
795 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
796 info->mcr |= UART_MCR_DTR;
797 if (!(tty->termios->c_cflag & CRTSCTS) ||
798 !test_bit(TTY_THROTTLED, &tty->flags)) {
799 info->mcr |= UART_MCR_RTS;
801 ctc_tty_transmit_status(info);
804 /* Handle turning off CRTSCTS */
805 if ((old_termios->c_cflag & CRTSCTS) &&
806 !(tty->termios->c_cflag & CRTSCTS))
811 * ------------------------------------------------------------
812 * ctc_tty_open() and friends
813 * ------------------------------------------------------------
816 ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
818 DECLARE_WAITQUEUE(wait, NULL);
824 * If the device is in the middle of being closed, then block
825 * until it's done, and then try again.
827 if (tty_hung_up_p(filp) ||
828 (info->flags & CTC_ASYNC_CLOSING)) {
829 if (info->flags & CTC_ASYNC_CLOSING)
830 wait_event(info->close_wait,
831 !(info->flags & CTC_ASYNC_CLOSING));
832 #ifdef MODEM_DO_RESTART
833 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
842 * If non-blocking mode is set, then make the check up front
845 if ((filp->f_flags & O_NONBLOCK) ||
846 (tty->flags & (1 << TTY_IO_ERROR))) {
847 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
850 if (tty->termios->c_cflag & CLOCAL)
853 * Block waiting for the carrier detect and the line to become
854 * free (i.e., not in use by the callout). While we are in
855 * this loop, info->count is dropped by one, so that
856 * ctc_tty_close() knows when to free things. We restore it upon
857 * exit, either normal or abnormal.
860 add_wait_queue(&info->open_wait, &wait);
861 #ifdef CTC_DEBUG_MODEM_OPEN
862 printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
863 CTC_TTY_NAME, info->line, info->count);
865 spin_lock_irqsave(&ctc_tty_lock, flags);
866 if (!(tty_hung_up_p(filp)))
868 spin_unlock_irqrestore(&ctc_tty_lock, flags);
869 info->blocked_open++;
871 set_current_state(TASK_INTERRUPTIBLE);
872 if (tty_hung_up_p(filp) ||
873 !(info->flags & CTC_ASYNC_INITIALIZED)) {
874 #ifdef MODEM_DO_RESTART
875 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
878 retval = -ERESTARTSYS;
884 if (!(info->flags & CTC_ASYNC_CLOSING) &&
885 (do_clocal || (info->msr & UART_MSR_DCD))) {
888 if (signal_pending(current)) {
889 retval = -ERESTARTSYS;
892 #ifdef CTC_DEBUG_MODEM_OPEN
893 printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
894 CTC_TTY_NAME, info->line, info->count);
898 current->state = TASK_RUNNING;
899 remove_wait_queue(&info->open_wait, &wait);
900 if (!tty_hung_up_p(filp))
902 info->blocked_open--;
903 #ifdef CTC_DEBUG_MODEM_OPEN
904 printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
905 CTC_TTY_NAME, info->line, info->count);
909 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
914 * This routine is called whenever a serial port is opened. It
915 * enables interrupts for a serial port, linking in its async structure into
916 * the IRQ chain. It also performs the serial-specific
917 * initialization for the tty structure.
920 ctc_tty_open(struct tty_struct *tty, struct file *filp)
923 unsigned long saveflags;
928 if (line < 0 || line > CTC_TTY_MAX_DEVICES)
930 info = &driver->info[line];
931 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
935 #ifdef CTC_DEBUG_MODEM_OPEN
936 printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
939 spin_lock_irqsave(&ctc_tty_lock, saveflags);
941 tty->driver_data = info;
943 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
945 * Start up serial port
947 retval = ctc_tty_startup(info);
949 #ifdef CTC_DEBUG_MODEM_OPEN
950 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
954 retval = ctc_tty_block_til_ready(tty, filp, info);
956 #ifdef CTC_DEBUG_MODEM_OPEN
957 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
961 #ifdef CTC_DEBUG_MODEM_OPEN
962 printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
968 ctc_tty_close(struct tty_struct *tty, struct file *filp)
970 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
974 if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
976 spin_lock_irqsave(&ctc_tty_lock, flags);
977 if (tty_hung_up_p(filp)) {
978 spin_unlock_irqrestore(&ctc_tty_lock, flags);
979 #ifdef CTC_DEBUG_MODEM_OPEN
980 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
984 if ((tty->count == 1) && (info->count != 1)) {
986 * Uh, oh. tty->count is 1, which means that the tty
987 * structure will be freed. Info->count should always
988 * be one in these conditions. If it's greater than
989 * one, we've got real problems, since it means the
990 * serial port won't be shutdown.
992 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
993 "info->count is %d\n", info->count);
996 if (--info->count < 0) {
997 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
998 CTC_TTY_NAME, info->line, info->count);
1002 local_irq_restore(flags);
1003 #ifdef CTC_DEBUG_MODEM_OPEN
1004 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1008 info->flags |= CTC_ASYNC_CLOSING;
1011 * At this point we stop accepting input. To do this, we
1012 * disable the receive line status interrupts, and tell the
1013 * interrupt driver to stop checking the data ready bit in the
1014 * line status register.
1016 if (info->flags & CTC_ASYNC_INITIALIZED) {
1017 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
1019 * Before we drop DTR, make sure the UART transmitter
1020 * has completely drained; this is especially
1021 * important if there is a transmit FIFO!
1023 timeout = jiffies + HZ;
1024 while (!(info->lsr & UART_LSR_TEMT)) {
1025 set_current_state(TASK_INTERRUPTIBLE);
1026 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1027 schedule_timeout(HZ/2);
1028 spin_lock_irqsave(&ctc_tty_lock, flags);
1029 if (time_after(jiffies,timeout))
1033 ctc_tty_shutdown(info);
1034 if (tty->driver->flush_buffer)
1035 tty->driver->flush_buffer(tty);
1036 if (tty->ldisc.flush_buffer)
1037 tty->ldisc.flush_buffer(tty);
1040 if (info->blocked_open) {
1041 set_current_state(TASK_INTERRUPTIBLE);
1042 schedule_timeout(HZ/2);
1043 wake_up_interruptible(&info->open_wait);
1045 info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
1046 wake_up_interruptible(&info->close_wait);
1047 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1048 #ifdef CTC_DEBUG_MODEM_OPEN
1049 printk(KERN_DEBUG "ctc_tty_close normal exit\n");
1054 * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1057 ctc_tty_hangup(struct tty_struct *tty)
1059 ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1060 unsigned long saveflags;
1062 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
1064 ctc_tty_shutdown(info);
1066 info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1067 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1069 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1070 wake_up_interruptible(&info->open_wait);
1075 * For all online tty's, try sending data to
1079 ctc_tty_task(unsigned long arg)
1081 ctc_tty_info *info = (void *)arg;
1082 unsigned long saveflags;
1085 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1086 if ((!ctc_tty_shuttingdown) && info) {
1087 again = ctc_tty_tint(info);
1089 info->lsr |= UART_LSR_TEMT;
1090 again |= ctc_tty_readmodem(info);
1092 tasklet_schedule(&info->tasklet);
1095 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1098 static struct tty_operations ctc_ops = {
1099 .open = ctc_tty_open,
1100 .close = ctc_tty_close,
1101 .write = ctc_tty_write,
1102 .flush_chars = ctc_tty_flush_chars,
1103 .write_room = ctc_tty_write_room,
1104 .chars_in_buffer = ctc_tty_chars_in_buffer,
1105 .flush_buffer = ctc_tty_flush_buffer,
1106 .ioctl = ctc_tty_ioctl,
1107 .throttle = ctc_tty_throttle,
1108 .unthrottle = ctc_tty_unthrottle,
1109 .set_termios = ctc_tty_set_termios,
1110 .hangup = ctc_tty_hangup,
1111 .tiocmget = ctc_tty_tiocmget,
1112 .tiocmset = ctc_tty_tiocmset,
1120 struct tty_driver *device;
1122 driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1123 if (driver == NULL) {
1124 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1127 memset(driver, 0, sizeof(ctc_tty_driver));
1128 device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
1131 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1135 device->devfs_name = "ctc/" CTC_TTY_NAME;
1136 device->name = CTC_TTY_NAME;
1137 device->major = CTC_TTY_MAJOR;
1138 device->minor_start = 0;
1139 device->type = TTY_DRIVER_TYPE_SERIAL;
1140 device->subtype = SERIAL_TYPE_NORMAL;
1141 device->init_termios = tty_std_termios;
1142 device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1143 device->flags = TTY_DRIVER_REAL_RAW;
1144 device->driver_name = "ctc_tty",
1145 tty_set_operations(device, &ctc_ops);
1146 if (tty_register_driver(device)) {
1147 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1148 put_tty_driver(device);
1152 driver->ctc_tty_device = device;
1153 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1154 info = &driver->info[i];
1155 init_MUTEX(&info->write_sem);
1156 tasklet_init(&info->tasklet, ctc_tty_task,
1157 (unsigned long) info);
1158 info->magic = CTC_ASYNC_MAGIC;
1162 info->blocked_open = 0;
1163 init_waitqueue_head(&info->open_wait);
1164 init_waitqueue_head(&info->close_wait);
1165 skb_queue_head_init(&info->tx_queue);
1166 skb_queue_head_init(&info->rx_queue);
1167 init_timer(&info->stoptimer);
1168 info->stoptimer.function = ctc_tty_stopdev;
1169 info->stoptimer.data = (unsigned long)info;
1170 info->mcr = UART_MCR_RTS;
1176 ctc_tty_register_netdev(struct net_device *dev) {
1181 if ((!dev) || (!dev->name)) {
1183 "ctc_tty_register_netdev called "
1184 "with NULL dev or NULL dev-name\n");
1187 for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1188 ttynum = simple_strtoul(p, &err, 0);
1189 if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1192 "ctc_tty_register_netdev called "
1193 "with number in name '%s'\n", dev->name);
1196 if (driver->info[ttynum].netdev) {
1198 "ctc_tty_register_netdev called "
1199 "for already registered device '%s'\n",
1203 driver->info[ttynum].netdev = dev;
1208 ctc_tty_unregister_netdev(struct net_device *dev) {
1210 unsigned long saveflags;
1211 ctc_tty_info *info = NULL;
1213 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1214 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
1215 if (driver->info[i].netdev == dev) {
1216 info = &driver->info[i];
1220 info->netdev = NULL;
1221 skb_queue_purge(&info->tx_queue);
1222 skb_queue_purge(&info->rx_queue);
1224 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1228 ctc_tty_cleanup(void) {
1229 unsigned long saveflags;
1231 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1232 ctc_tty_shuttingdown = 1;
1233 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1234 tty_unregister_driver(driver->ctc_tty_device);
1235 put_tty_driver(driver->ctc_tty_device);