2 * $Id: ctctty.c,v 1.29 2005/04/05 08:50:44 mschwide 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 <linux/delay.h>
31 #include <asm/uaccess.h>
32 #include <linux/devfs_fs_kernel.h>
36 #define CTC_TTY_MAJOR 43
37 #define CTC_TTY_MAX_DEVICES 64
39 #define CTC_ASYNC_MAGIC 0x49344C01 /* for paranoia-checking */
40 #define CTC_ASYNC_INITIALIZED 0x80000000 /* port was initialized */
41 #define CTC_ASYNC_NORMAL_ACTIVE 0x20000000 /* Normal device active */
42 #define CTC_ASYNC_CLOSING 0x08000000 /* Serial port is closing */
43 #define CTC_ASYNC_CTS_FLOW 0x04000000 /* Do CTS flow control */
44 #define CTC_ASYNC_CHECK_CD 0x02000000 /* i.e., CLOCAL */
45 #define CTC_ASYNC_HUP_NOTIFY 0x0001 /* Notify tty on hangups/closes */
46 #define CTC_ASYNC_NETDEV_OPEN 0x0002 /* Underlying netdev is open */
47 #define CTC_ASYNC_TX_LINESTAT 0x0004 /* Must send line status */
48 #define CTC_ASYNC_SPLIT_TERMIOS 0x0008 /* Sep. termios for dialin/out */
49 #define CTC_TTY_XMIT_SIZE 1024 /* Default bufsize for write */
50 #define CTC_SERIAL_XMIT_MAX 4000 /* Maximum bufsize for write */
52 /* Private data (similar to async_struct in <linux/serial.h>) */
55 int flags; /* defined in tty.h */
56 int mcr; /* Modem control register */
57 int msr; /* Modem status register */
58 int lsr; /* Line status register */
60 int count; /* # of fd on device */
61 int blocked_open; /* # of blocked opens */
62 struct net_device *netdev;
63 struct sk_buff_head tx_queue; /* transmit queue */
64 struct sk_buff_head rx_queue; /* receive queue */
65 struct tty_struct *tty; /* Pointer to corresponding tty */
66 wait_queue_head_t open_wait;
67 wait_queue_head_t close_wait;
68 struct semaphore write_sem;
69 struct tasklet_struct tasklet;
70 struct timer_list stoptimer;
73 /* Description of one CTC-tty */
75 struct tty_driver *ctc_tty_device; /* tty-device */
76 ctc_tty_info info[CTC_TTY_MAX_DEVICES]; /* Private data */
79 static ctc_tty_driver *driver;
81 /* Leave this unchanged unless you know what you do! */
82 #define MODEM_PARANOIA_CHECK
83 #define MODEM_DO_RESTART
85 #define CTC_TTY_NAME "ctctty"
87 static __u32 ctc_tty_magic = CTC_ASYNC_MAGIC;
88 static int ctc_tty_shuttingdown = 0;
90 static spinlock_t ctc_tty_lock;
92 /* ctc_tty_try_read() is called from within ctc_tty_rcv_skb()
93 * to stuff incoming data directly into a tty's flip-buffer. If the
94 * flip buffer is full, the packet gets queued up.
98 * 0 = Failure, data has to be buffered and later processed by
99 * ctc_tty_readmodem().
102 ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
106 struct tty_struct *tty;
108 DBF_TEXT(trace, 5, __FUNCTION__);
109 if ((tty = info->tty)) {
110 if (info->mcr & UART_MCR_RTS) {
111 c = TTY_FLIPBUF_SIZE - tty->flip.count;
114 memcpy(tty->flip.char_buf_ptr, skb->data, len);
115 memset(tty->flip.flag_buf_ptr, 0, len);
116 tty->flip.count += len;
117 tty->flip.char_buf_ptr += len;
118 tty->flip.flag_buf_ptr += len;
119 tty_flip_buffer_push(tty);
128 /* ctc_tty_readmodem() is called periodically from within timer-interrupt.
129 * It tries getting received data from the receive queue an stuff it into
130 * the tty's flip-buffer.
133 ctc_tty_readmodem(ctc_tty_info *info)
136 struct tty_struct *tty;
138 DBF_TEXT(trace, 5, __FUNCTION__);
139 if ((tty = info->tty)) {
140 if (info->mcr & UART_MCR_RTS) {
141 int c = TTY_FLIPBUF_SIZE - tty->flip.count;
144 if ((c > 0) && (skb = skb_dequeue(&info->rx_queue))) {
148 memcpy(tty->flip.char_buf_ptr, skb->data, len);
150 memset(tty->flip.flag_buf_ptr, 0, len);
151 tty->flip.count += len;
152 tty->flip.char_buf_ptr += len;
153 tty->flip.flag_buf_ptr += len;
154 tty_flip_buffer_push(tty);
156 skb_queue_head(&info->rx_queue, skb);
159 ret = skb_queue_len(&info->rx_queue);
168 ctc_tty_setcarrier(struct net_device *netdev, int on)
172 DBF_TEXT(trace, 4, __FUNCTION__);
173 if ((!driver) || ctc_tty_shuttingdown)
175 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
176 if (driver->info[i].netdev == netdev) {
177 ctc_tty_info *info = &driver->info[i];
179 info->msr |= UART_MSR_DCD;
181 info->msr &= ~UART_MSR_DCD;
182 if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
183 tty_hangup(info->tty);
188 ctc_tty_netif_rx(struct sk_buff *skb)
191 ctc_tty_info *info = NULL;
193 DBF_TEXT(trace, 5, __FUNCTION__);
196 if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
200 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
201 if (driver->info[i].netdev == skb->dev) {
202 info = &driver->info[i];
213 if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
217 skb_pull(skb, sizeof(__u32));
219 i = *((int *)skb->data);
220 skb_pull(skb, sizeof(info->mcr));
221 if (i & UART_MCR_RTS) {
222 info->msr |= UART_MSR_CTS;
223 if (info->flags & CTC_ASYNC_CTS_FLOW)
224 info->tty->hw_stopped = 0;
226 info->msr &= ~UART_MSR_CTS;
227 if (info->flags & CTC_ASYNC_CTS_FLOW)
228 info->tty->hw_stopped = 1;
230 if (i & UART_MCR_DTR)
231 info->msr |= UART_MSR_DSR;
233 info->msr &= ~UART_MSR_DSR;
238 /* Try to deliver directly via tty-flip-buf if queue is empty */
239 if (skb_queue_empty(&info->rx_queue))
240 if (ctc_tty_try_read(info, skb))
242 /* Direct deliver failed or queue wasn't empty.
243 * Queue up for later dequeueing via timer-irq.
245 skb_queue_tail(&info->rx_queue, skb);
246 /* Schedule dequeuing */
247 tasklet_schedule(&info->tasklet);
251 ctc_tty_tint(ctc_tty_info * info)
253 struct sk_buff *skb = skb_dequeue(&info->tx_queue);
254 int stopped = (info->tty->hw_stopped || info->tty->stopped);
258 DBF_TEXT(trace, 4, __FUNCTION__);
264 if (info->flags & CTC_ASYNC_TX_LINESTAT) {
265 int skb_res = info->netdev->hard_header_len +
266 sizeof(info->mcr) + sizeof(__u32);
267 /* If we must update line status,
268 * create an empty dummy skb and insert it.
271 skb_queue_head(&info->tx_queue, skb);
273 skb = dev_alloc_skb(skb_res);
276 "ctc_tty: Out of memory in %s%d tint\n",
277 CTC_TTY_NAME, info->line);
280 skb_reserve(skb, skb_res);
287 skb_queue_head(&info->tx_queue, skb);
292 printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
294 printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
296 memcpy(skb_push(skb, sizeof(info->mcr)), &info->mcr, sizeof(info->mcr));
297 memcpy(skb_push(skb, sizeof(__u32)), &ctc_tty_magic, sizeof(__u32));
298 rc = info->netdev->hard_start_xmit(skb, info->netdev);
300 skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
302 skb_queue_head(&info->tx_queue, skb);
306 struct tty_struct *tty = info->tty;
308 info->flags &= ~CTC_ASYNC_TX_LINESTAT;
313 return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
316 /************************************************************
320 * mostly "stolen" from original Linux-serial.c and friends.
322 ************************************************************/
325 ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
327 #ifdef MODEM_PARANOIA_CHECK
329 printk(KERN_WARNING "ctc_tty: null info_struct for %s in %s\n",
333 if (info->magic != CTC_ASYNC_MAGIC) {
334 printk(KERN_WARNING "ctc_tty: bad magic for info struct %s in %s\n",
343 ctc_tty_inject(ctc_tty_info *info, char c)
348 DBF_TEXT(trace, 4, __FUNCTION__);
349 if (ctc_tty_shuttingdown)
351 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
353 skb = dev_alloc_skb(skb_res);
356 "ctc_tty: Out of memory in %s%d tx_inject\n",
357 CTC_TTY_NAME, info->line);
360 skb_reserve(skb, skb_res);
361 *(skb_put(skb, 1)) = c;
362 skb_queue_head(&info->tx_queue, skb);
363 tasklet_schedule(&info->tasklet);
367 ctc_tty_transmit_status(ctc_tty_info *info)
369 DBF_TEXT(trace, 5, __FUNCTION__);
370 if (ctc_tty_shuttingdown)
372 info->flags |= CTC_ASYNC_TX_LINESTAT;
373 tasklet_schedule(&info->tasklet);
377 ctc_tty_change_speed(ctc_tty_info * info)
383 DBF_TEXT(trace, 3, __FUNCTION__);
384 if (!info->tty || !info->tty->termios)
386 cflag = info->tty->termios->c_cflag;
388 quot = i = cflag & CBAUD;
392 info->tty->termios->c_cflag &= ~CBAUDEX;
397 info->mcr |= UART_MCR_DTR;
398 info->mcr |= UART_MCR_RTS;
399 ctc_tty_transmit_status(info);
401 info->mcr &= ~UART_MCR_DTR;
402 info->mcr &= ~UART_MCR_RTS;
403 ctc_tty_transmit_status(info);
407 /* CTS flow control flag and modem status interrupts */
408 if (cflag & CRTSCTS) {
409 info->flags |= CTC_ASYNC_CTS_FLOW;
411 info->flags &= ~CTC_ASYNC_CTS_FLOW;
413 info->flags &= ~CTC_ASYNC_CHECK_CD;
415 info->flags |= CTC_ASYNC_CHECK_CD;
420 ctc_tty_startup(ctc_tty_info * info)
422 DBF_TEXT(trace, 3, __FUNCTION__);
423 if (info->flags & CTC_ASYNC_INITIALIZED)
425 #ifdef CTC_DEBUG_MODEM_OPEN
426 printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
429 * Now, initialize the UART
431 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
433 clear_bit(TTY_IO_ERROR, &info->tty->flags);
435 * and set the speed of the serial port
437 ctc_tty_change_speed(info);
439 info->flags |= CTC_ASYNC_INITIALIZED;
440 if (!(info->flags & CTC_ASYNC_NETDEV_OPEN))
441 info->netdev->open(info->netdev);
442 info->flags |= CTC_ASYNC_NETDEV_OPEN;
447 ctc_tty_stopdev(unsigned long data)
449 ctc_tty_info *info = (ctc_tty_info *)data;
451 if ((!info) || (!info->netdev) ||
452 (info->flags & CTC_ASYNC_INITIALIZED))
454 info->netdev->stop(info->netdev);
455 info->flags &= ~CTC_ASYNC_NETDEV_OPEN;
459 * This routine will shutdown a serial port; interrupts are disabled, and
460 * DTR is dropped if the hangup on close termio flag is on.
463 ctc_tty_shutdown(ctc_tty_info * info)
465 DBF_TEXT(trace, 3, __FUNCTION__);
466 if (!(info->flags & CTC_ASYNC_INITIALIZED))
468 #ifdef CTC_DEBUG_MODEM_OPEN
469 printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
471 info->msr &= ~UART_MSR_RI;
472 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
473 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
475 set_bit(TTY_IO_ERROR, &info->tty->flags);
476 mod_timer(&info->stoptimer, jiffies + (10 * HZ));
477 skb_queue_purge(&info->tx_queue);
478 skb_queue_purge(&info->rx_queue);
479 info->flags &= ~CTC_ASYNC_INITIALIZED;
482 /* ctc_tty_write() is the main send-routine. It is called from the upper
483 * levels within the kernel to perform sending data. Depending on the
484 * online-flag it either directs output to the at-command-interpreter or
485 * to the lower level. Additional tasks done here:
486 * - If online, check for escape-sequence (+++)
487 * - If sending audio-data, call ctc_tty_DLEdown() to parse DLE-codes.
488 * - If receiving audio-data, call ctc_tty_end_vrx() to abort if needed.
489 * - If dialing, abort dial.
492 ctc_tty_write(struct tty_struct *tty, const u_char * buf, int count)
496 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
498 DBF_TEXT(trace, 5, __FUNCTION__);
499 if (ctc_tty_shuttingdown)
501 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write"))
513 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
517 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
519 skb = dev_alloc_skb(skb_res + c);
522 "ctc_tty: Out of memory in %s%d write\n",
523 CTC_TTY_NAME, info->line);
526 skb_reserve(skb, skb_res);
527 memcpy(skb_put(skb, c), buf, c);
528 skb_queue_tail(&info->tx_queue, skb);
533 if (skb_queue_len(&info->tx_queue)) {
534 info->lsr &= ~UART_LSR_TEMT;
535 tasklet_schedule(&info->tasklet);
538 DBF_TEXT(trace, 6, __FUNCTION__);
543 ctc_tty_write_room(struct tty_struct *tty)
545 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
547 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write_room"))
549 return CTC_TTY_XMIT_SIZE;
553 ctc_tty_chars_in_buffer(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_chars_in_buffer"))
563 ctc_tty_flush_buffer(struct tty_struct *tty)
568 DBF_TEXT(trace, 4, __FUNCTION__);
571 spin_lock_irqsave(&ctc_tty_lock, flags);
572 info = (ctc_tty_info *) tty->driver_data;
573 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_buffer")) {
574 spin_unlock_irqrestore(&ctc_tty_lock, flags);
577 skb_queue_purge(&info->tx_queue);
578 info->lsr |= UART_LSR_TEMT;
579 spin_unlock_irqrestore(&ctc_tty_lock, flags);
580 wake_up_interruptible(&tty->write_wait);
583 DBF_TEXT_(trace, 2, "ex: %s ", __FUNCTION__);
588 ctc_tty_flush_chars(struct tty_struct *tty)
590 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
592 DBF_TEXT(trace, 4, __FUNCTION__);
593 if (ctc_tty_shuttingdown)
595 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
597 if (tty->stopped || tty->hw_stopped || (!skb_queue_len(&info->tx_queue)))
599 tasklet_schedule(&info->tasklet);
603 * ------------------------------------------------------------
606 * This routine is called by the upper-layer tty layer to signal that
607 * incoming characters should be throttled.
608 * ------------------------------------------------------------
611 ctc_tty_throttle(struct tty_struct *tty)
613 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
615 DBF_TEXT(trace, 4, __FUNCTION__);
616 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_throttle"))
618 info->mcr &= ~UART_MCR_RTS;
620 ctc_tty_inject(info, STOP_CHAR(tty));
621 ctc_tty_transmit_status(info);
625 ctc_tty_unthrottle(struct tty_struct *tty)
627 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
629 DBF_TEXT(trace, 4, __FUNCTION__);
630 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_unthrottle"))
632 info->mcr |= UART_MCR_RTS;
634 ctc_tty_inject(info, START_CHAR(tty));
635 ctc_tty_transmit_status(info);
639 * ------------------------------------------------------------
640 * ctc_tty_ioctl() and friends
641 * ------------------------------------------------------------
645 * ctc_tty_get_lsr_info - get line status register info
647 * Purpose: Let user call ioctl() to get info when the UART physically
648 * is emptied. On bus types like RS485, the transmitter must
649 * release the bus after transmitting. This must be done when
650 * the transmit shift register is empty, not be done when the
651 * transmit holding register is empty. This functionality
652 * allows RS485 driver to be written in user space.
655 ctc_tty_get_lsr_info(ctc_tty_info * info, uint __user *value)
661 DBF_TEXT(trace, 4, __FUNCTION__);
662 spin_lock_irqsave(&ctc_tty_lock, flags);
664 spin_unlock_irqrestore(&ctc_tty_lock, flags);
665 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
666 put_user(result, value);
671 static int ctc_tty_tiocmget(struct tty_struct *tty, struct file *file)
673 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
679 DBF_TEXT(trace, 4, __FUNCTION__);
680 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
682 if (tty->flags & (1 << TTY_IO_ERROR))
686 spin_lock_irqsave(&ctc_tty_lock, flags);
688 spin_unlock_irqrestore(&ctc_tty_lock, flags);
689 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
690 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
691 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
692 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
693 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
694 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
699 ctc_tty_tiocmset(struct tty_struct *tty, struct file *file,
700 unsigned int set, unsigned int clear)
702 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
704 DBF_TEXT(trace, 4, __FUNCTION__);
705 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
707 if (tty->flags & (1 << TTY_IO_ERROR))
711 info->mcr |= UART_MCR_RTS;
713 info->mcr |= UART_MCR_DTR;
715 if (clear & TIOCM_RTS)
716 info->mcr &= ~UART_MCR_RTS;
717 if (clear & TIOCM_DTR)
718 info->mcr &= ~UART_MCR_DTR;
720 if ((set | clear) & (TIOCM_RTS|TIOCM_DTR))
721 ctc_tty_transmit_status(info);
726 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
729 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
733 DBF_TEXT(trace, 4, __FUNCTION__);
734 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
736 if (tty->flags & (1 << TTY_IO_ERROR))
739 case TCSBRK: /* SVID version: non-zero arg --> no break */
740 #ifdef CTC_DEBUG_MODEM_IOCTL
741 printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
743 retval = tty_check_change(tty);
746 tty_wait_until_sent(tty, 0);
748 case TCSBRKP: /* support for POSIX tcsendbreak() */
749 #ifdef CTC_DEBUG_MODEM_IOCTL
750 printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
752 retval = tty_check_change(tty);
755 tty_wait_until_sent(tty, 0);
758 #ifdef CTC_DEBUG_MODEM_IOCTL
759 printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
762 error = put_user(C_CLOCAL(tty) ? 1 : 0, (ulong __user *) arg);
765 #ifdef CTC_DEBUG_MODEM_IOCTL
766 printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
769 error = get_user(arg, (ulong __user *) arg);
772 tty->termios->c_cflag =
773 ((tty->termios->c_cflag & ~CLOCAL) |
776 case TIOCSERGETLSR: /* Get line status register */
777 #ifdef CTC_DEBUG_MODEM_IOCTL
778 printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
781 if (access_ok(VERIFY_WRITE, (void __user *) arg, sizeof(uint)))
782 return ctc_tty_get_lsr_info(info, (uint __user *) arg);
786 #ifdef CTC_DEBUG_MODEM_IOCTL
787 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
788 CTC_TTY_NAME, info->line);
796 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
798 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
799 unsigned int cflag = tty->termios->c_cflag;
801 DBF_TEXT(trace, 4, __FUNCTION__);
802 ctc_tty_change_speed(info);
804 /* Handle transition to B0 */
805 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
806 info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
807 ctc_tty_transmit_status(info);
810 /* Handle transition from B0 to other */
811 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
812 info->mcr |= UART_MCR_DTR;
813 if (!(tty->termios->c_cflag & CRTSCTS) ||
814 !test_bit(TTY_THROTTLED, &tty->flags)) {
815 info->mcr |= UART_MCR_RTS;
817 ctc_tty_transmit_status(info);
820 /* Handle turning off CRTSCTS */
821 if ((old_termios->c_cflag & CRTSCTS) &&
822 !(tty->termios->c_cflag & CRTSCTS))
827 * ------------------------------------------------------------
828 * ctc_tty_open() and friends
829 * ------------------------------------------------------------
832 ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
834 DECLARE_WAITQUEUE(wait, NULL);
839 DBF_TEXT(trace, 4, __FUNCTION__);
841 * If the device is in the middle of being closed, then block
842 * until it's done, and then try again.
844 if (tty_hung_up_p(filp) ||
845 (info->flags & CTC_ASYNC_CLOSING)) {
846 if (info->flags & CTC_ASYNC_CLOSING)
847 wait_event(info->close_wait,
848 !(info->flags & CTC_ASYNC_CLOSING));
849 #ifdef MODEM_DO_RESTART
850 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
859 * If non-blocking mode is set, then make the check up front
862 if ((filp->f_flags & O_NONBLOCK) ||
863 (tty->flags & (1 << TTY_IO_ERROR))) {
864 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
867 if (tty->termios->c_cflag & CLOCAL)
870 * Block waiting for the carrier detect and the line to become
871 * free (i.e., not in use by the callout). While we are in
872 * this loop, info->count is dropped by one, so that
873 * ctc_tty_close() knows when to free things. We restore it upon
874 * exit, either normal or abnormal.
877 add_wait_queue(&info->open_wait, &wait);
878 #ifdef CTC_DEBUG_MODEM_OPEN
879 printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
880 CTC_TTY_NAME, info->line, info->count);
882 spin_lock_irqsave(&ctc_tty_lock, flags);
883 if (!(tty_hung_up_p(filp)))
885 spin_unlock_irqrestore(&ctc_tty_lock, flags);
886 info->blocked_open++;
888 set_current_state(TASK_INTERRUPTIBLE);
889 if (tty_hung_up_p(filp) ||
890 !(info->flags & CTC_ASYNC_INITIALIZED)) {
891 #ifdef MODEM_DO_RESTART
892 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
895 retval = -ERESTARTSYS;
901 if (!(info->flags & CTC_ASYNC_CLOSING) &&
902 (do_clocal || (info->msr & UART_MSR_DCD))) {
905 if (signal_pending(current)) {
906 retval = -ERESTARTSYS;
909 #ifdef CTC_DEBUG_MODEM_OPEN
910 printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
911 CTC_TTY_NAME, info->line, info->count);
915 current->state = TASK_RUNNING;
916 remove_wait_queue(&info->open_wait, &wait);
917 if (!tty_hung_up_p(filp))
919 info->blocked_open--;
920 #ifdef CTC_DEBUG_MODEM_OPEN
921 printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
922 CTC_TTY_NAME, info->line, info->count);
926 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
931 * This routine is called whenever a serial port is opened. It
932 * enables interrupts for a serial port, linking in its async structure into
933 * the IRQ chain. It also performs the serial-specific
934 * initialization for the tty structure.
937 ctc_tty_open(struct tty_struct *tty, struct file *filp)
940 unsigned long saveflags;
944 DBF_TEXT(trace, 3, __FUNCTION__);
946 if (line < 0 || line > CTC_TTY_MAX_DEVICES)
948 info = &driver->info[line];
949 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
953 #ifdef CTC_DEBUG_MODEM_OPEN
954 printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
957 spin_lock_irqsave(&ctc_tty_lock, saveflags);
959 tty->driver_data = info;
961 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
963 * Start up serial port
965 retval = ctc_tty_startup(info);
967 #ifdef CTC_DEBUG_MODEM_OPEN
968 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
972 retval = ctc_tty_block_til_ready(tty, filp, info);
974 #ifdef CTC_DEBUG_MODEM_OPEN
975 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
979 #ifdef CTC_DEBUG_MODEM_OPEN
980 printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
986 ctc_tty_close(struct tty_struct *tty, struct file *filp)
988 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
991 DBF_TEXT(trace, 3, __FUNCTION__);
992 if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
994 spin_lock_irqsave(&ctc_tty_lock, flags);
995 if (tty_hung_up_p(filp)) {
996 spin_unlock_irqrestore(&ctc_tty_lock, flags);
997 #ifdef CTC_DEBUG_MODEM_OPEN
998 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
1002 if ((tty->count == 1) && (info->count != 1)) {
1004 * Uh, oh. tty->count is 1, which means that the tty
1005 * structure will be freed. Info->count should always
1006 * be one in these conditions. If it's greater than
1007 * one, we've got real problems, since it means the
1008 * serial port won't be shutdown.
1010 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
1011 "info->count is %d\n", info->count);
1014 if (--info->count < 0) {
1015 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
1016 CTC_TTY_NAME, info->line, info->count);
1020 local_irq_restore(flags);
1021 #ifdef CTC_DEBUG_MODEM_OPEN
1022 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1026 info->flags |= CTC_ASYNC_CLOSING;
1029 * At this point we stop accepting input. To do this, we
1030 * disable the receive line status interrupts, and tell the
1031 * interrupt driver to stop checking the data ready bit in the
1032 * line status register.
1034 if (info->flags & CTC_ASYNC_INITIALIZED) {
1035 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
1037 * Before we drop DTR, make sure the UART transmitter
1038 * has completely drained; this is especially
1039 * important if there is a transmit FIFO!
1041 timeout = jiffies + HZ;
1042 while (!(info->lsr & UART_LSR_TEMT)) {
1043 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1045 spin_lock_irqsave(&ctc_tty_lock, flags);
1046 if (time_after(jiffies,timeout))
1050 ctc_tty_shutdown(info);
1051 if (tty->driver->flush_buffer) {
1052 skb_queue_purge(&info->tx_queue);
1053 info->lsr |= UART_LSR_TEMT;
1055 tty_ldisc_flush(tty);
1058 if (info->blocked_open) {
1059 msleep_interruptible(500);
1060 wake_up_interruptible(&info->open_wait);
1062 info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
1063 wake_up_interruptible(&info->close_wait);
1064 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1065 #ifdef CTC_DEBUG_MODEM_OPEN
1066 printk(KERN_DEBUG "ctc_tty_close normal exit\n");
1071 * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1074 ctc_tty_hangup(struct tty_struct *tty)
1076 ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1077 unsigned long saveflags;
1078 DBF_TEXT(trace, 3, __FUNCTION__);
1079 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
1081 ctc_tty_shutdown(info);
1083 info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1084 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1086 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1087 wake_up_interruptible(&info->open_wait);
1092 * For all online tty's, try sending data to
1096 ctc_tty_task(unsigned long arg)
1098 ctc_tty_info *info = (void *)arg;
1099 unsigned long saveflags;
1102 DBF_TEXT(trace, 3, __FUNCTION__);
1103 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1104 if ((!ctc_tty_shuttingdown) && info) {
1105 again = ctc_tty_tint(info);
1107 info->lsr |= UART_LSR_TEMT;
1108 again |= ctc_tty_readmodem(info);
1110 tasklet_schedule(&info->tasklet);
1113 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1116 static struct tty_operations ctc_ops = {
1117 .open = ctc_tty_open,
1118 .close = ctc_tty_close,
1119 .write = ctc_tty_write,
1120 .flush_chars = ctc_tty_flush_chars,
1121 .write_room = ctc_tty_write_room,
1122 .chars_in_buffer = ctc_tty_chars_in_buffer,
1123 .flush_buffer = ctc_tty_flush_buffer,
1124 .ioctl = ctc_tty_ioctl,
1125 .throttle = ctc_tty_throttle,
1126 .unthrottle = ctc_tty_unthrottle,
1127 .set_termios = ctc_tty_set_termios,
1128 .hangup = ctc_tty_hangup,
1129 .tiocmget = ctc_tty_tiocmget,
1130 .tiocmset = ctc_tty_tiocmset,
1138 struct tty_driver *device;
1140 DBF_TEXT(trace, 2, __FUNCTION__);
1141 driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1142 if (driver == NULL) {
1143 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1146 memset(driver, 0, sizeof(ctc_tty_driver));
1147 device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
1150 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1154 device->devfs_name = "ctc/" CTC_TTY_NAME;
1155 device->name = CTC_TTY_NAME;
1156 device->major = CTC_TTY_MAJOR;
1157 device->minor_start = 0;
1158 device->type = TTY_DRIVER_TYPE_SERIAL;
1159 device->subtype = SERIAL_TYPE_NORMAL;
1160 device->init_termios = tty_std_termios;
1161 device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1162 device->flags = TTY_DRIVER_REAL_RAW;
1163 device->driver_name = "ctc_tty",
1164 tty_set_operations(device, &ctc_ops);
1165 if (tty_register_driver(device)) {
1166 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1167 put_tty_driver(device);
1171 driver->ctc_tty_device = device;
1172 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1173 info = &driver->info[i];
1174 init_MUTEX(&info->write_sem);
1175 tasklet_init(&info->tasklet, ctc_tty_task,
1176 (unsigned long) info);
1177 info->magic = CTC_ASYNC_MAGIC;
1181 info->blocked_open = 0;
1182 init_waitqueue_head(&info->open_wait);
1183 init_waitqueue_head(&info->close_wait);
1184 skb_queue_head_init(&info->tx_queue);
1185 skb_queue_head_init(&info->rx_queue);
1186 init_timer(&info->stoptimer);
1187 info->stoptimer.function = ctc_tty_stopdev;
1188 info->stoptimer.data = (unsigned long)info;
1189 info->mcr = UART_MCR_RTS;
1195 ctc_tty_register_netdev(struct net_device *dev) {
1200 DBF_TEXT(trace, 2, __FUNCTION__);
1201 if ((!dev) || (!dev->name)) {
1203 "ctc_tty_register_netdev called "
1204 "with NULL dev or NULL dev-name\n");
1209 * If the name is a format string the caller wants us to
1210 * do a name allocation : format string must end with %d
1212 if (strchr(dev->name, '%'))
1214 int err = dev_alloc_name(dev, dev->name); // dev->name is changed by this
1216 printk(KERN_DEBUG "dev_alloc returned error %d\n", err);
1222 for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1223 ttynum = simple_strtoul(p, &err, 0);
1224 if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1227 "ctc_tty_register_netdev called "
1228 "with number in name '%s'\n", dev->name);
1231 if (driver->info[ttynum].netdev) {
1233 "ctc_tty_register_netdev called "
1234 "for already registered device '%s'\n",
1238 driver->info[ttynum].netdev = dev;
1243 ctc_tty_unregister_netdev(struct net_device *dev) {
1245 unsigned long saveflags;
1246 ctc_tty_info *info = NULL;
1248 DBF_TEXT(trace, 2, __FUNCTION__);
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 DBF_TEXT(trace, 2, __FUNCTION__);
1268 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1269 ctc_tty_shuttingdown = 1;
1270 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1271 tty_unregister_driver(driver->ctc_tty_device);
1272 put_tty_driver(driver->ctc_tty_device);