2 * CTC / ESCON network driver, tty interface.
4 * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
5 * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/serial_reg.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.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)
104 struct tty_struct *tty;
106 DBF_TEXT(trace, 5, __FUNCTION__);
107 if ((tty = info->tty)) {
108 if (info->mcr & UART_MCR_RTS) {
110 tty_insert_flip_string(tty, skb->data, len);
111 tty_flip_buffer_push(tty);
119 /* ctc_tty_readmodem() is called periodically from within timer-interrupt.
120 * It tries getting received data from the receive queue an stuff it into
121 * the tty's flip-buffer.
124 ctc_tty_readmodem(ctc_tty_info *info)
127 struct tty_struct *tty;
129 DBF_TEXT(trace, 5, __FUNCTION__);
130 if ((tty = info->tty)) {
131 if (info->mcr & UART_MCR_RTS) {
134 if ((skb = skb_dequeue(&info->rx_queue))) {
136 tty_insert_flip_string(tty, skb->data, len);
138 tty_flip_buffer_push(tty);
140 skb_queue_head(&info->rx_queue, skb);
143 ret = !skb_queue_empty(&info->rx_queue);
152 ctc_tty_setcarrier(struct net_device *netdev, int on)
156 DBF_TEXT(trace, 4, __FUNCTION__);
157 if ((!driver) || ctc_tty_shuttingdown)
159 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
160 if (driver->info[i].netdev == netdev) {
161 ctc_tty_info *info = &driver->info[i];
163 info->msr |= UART_MSR_DCD;
165 info->msr &= ~UART_MSR_DCD;
166 if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
167 tty_hangup(info->tty);
172 ctc_tty_netif_rx(struct sk_buff *skb)
175 ctc_tty_info *info = NULL;
177 DBF_TEXT(trace, 5, __FUNCTION__);
180 if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
184 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
185 if (driver->info[i].netdev == skb->dev) {
186 info = &driver->info[i];
197 if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
201 skb_pull(skb, sizeof(__u32));
203 i = *((int *)skb->data);
204 skb_pull(skb, sizeof(info->mcr));
205 if (i & UART_MCR_RTS) {
206 info->msr |= UART_MSR_CTS;
207 if (info->flags & CTC_ASYNC_CTS_FLOW)
208 info->tty->hw_stopped = 0;
210 info->msr &= ~UART_MSR_CTS;
211 if (info->flags & CTC_ASYNC_CTS_FLOW)
212 info->tty->hw_stopped = 1;
214 if (i & UART_MCR_DTR)
215 info->msr |= UART_MSR_DSR;
217 info->msr &= ~UART_MSR_DSR;
222 /* Try to deliver directly via tty-flip-buf if queue is empty */
223 if (skb_queue_empty(&info->rx_queue))
224 if (ctc_tty_try_read(info, skb))
226 /* Direct deliver failed or queue wasn't empty.
227 * Queue up for later dequeueing via timer-irq.
229 skb_queue_tail(&info->rx_queue, skb);
230 /* Schedule dequeuing */
231 tasklet_schedule(&info->tasklet);
235 ctc_tty_tint(ctc_tty_info * info)
237 struct sk_buff *skb = skb_dequeue(&info->tx_queue);
238 int stopped = (info->tty->hw_stopped || info->tty->stopped);
242 DBF_TEXT(trace, 4, __FUNCTION__);
248 if (info->flags & CTC_ASYNC_TX_LINESTAT) {
249 int skb_res = info->netdev->hard_header_len +
250 sizeof(info->mcr) + sizeof(__u32);
251 /* If we must update line status,
252 * create an empty dummy skb and insert it.
255 skb_queue_head(&info->tx_queue, skb);
257 skb = dev_alloc_skb(skb_res);
260 "ctc_tty: Out of memory in %s%d tint\n",
261 CTC_TTY_NAME, info->line);
264 skb_reserve(skb, skb_res);
271 skb_queue_head(&info->tx_queue, skb);
276 printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
278 printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
280 memcpy(skb_push(skb, sizeof(info->mcr)), &info->mcr, sizeof(info->mcr));
281 memcpy(skb_push(skb, sizeof(__u32)), &ctc_tty_magic, sizeof(__u32));
282 rc = info->netdev->hard_start_xmit(skb, info->netdev);
284 skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
286 skb_queue_head(&info->tx_queue, skb);
290 struct tty_struct *tty = info->tty;
292 info->flags &= ~CTC_ASYNC_TX_LINESTAT;
297 return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
300 /************************************************************
304 * mostly "stolen" from original Linux-serial.c and friends.
306 ************************************************************/
309 ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
311 #ifdef MODEM_PARANOIA_CHECK
313 printk(KERN_WARNING "ctc_tty: null info_struct for %s in %s\n",
317 if (info->magic != CTC_ASYNC_MAGIC) {
318 printk(KERN_WARNING "ctc_tty: bad magic for info struct %s in %s\n",
327 ctc_tty_inject(ctc_tty_info *info, char c)
332 DBF_TEXT(trace, 4, __FUNCTION__);
333 if (ctc_tty_shuttingdown)
335 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
337 skb = dev_alloc_skb(skb_res);
340 "ctc_tty: Out of memory in %s%d tx_inject\n",
341 CTC_TTY_NAME, info->line);
344 skb_reserve(skb, skb_res);
345 *(skb_put(skb, 1)) = c;
346 skb_queue_head(&info->tx_queue, skb);
347 tasklet_schedule(&info->tasklet);
351 ctc_tty_transmit_status(ctc_tty_info *info)
353 DBF_TEXT(trace, 5, __FUNCTION__);
354 if (ctc_tty_shuttingdown)
356 info->flags |= CTC_ASYNC_TX_LINESTAT;
357 tasklet_schedule(&info->tasklet);
361 ctc_tty_change_speed(ctc_tty_info * info)
367 DBF_TEXT(trace, 3, __FUNCTION__);
368 if (!info->tty || !info->tty->termios)
370 cflag = info->tty->termios->c_cflag;
372 quot = i = cflag & CBAUD;
376 info->tty->termios->c_cflag &= ~CBAUDEX;
381 info->mcr |= UART_MCR_DTR;
382 info->mcr |= UART_MCR_RTS;
383 ctc_tty_transmit_status(info);
385 info->mcr &= ~UART_MCR_DTR;
386 info->mcr &= ~UART_MCR_RTS;
387 ctc_tty_transmit_status(info);
391 /* CTS flow control flag and modem status interrupts */
392 if (cflag & CRTSCTS) {
393 info->flags |= CTC_ASYNC_CTS_FLOW;
395 info->flags &= ~CTC_ASYNC_CTS_FLOW;
397 info->flags &= ~CTC_ASYNC_CHECK_CD;
399 info->flags |= CTC_ASYNC_CHECK_CD;
404 ctc_tty_startup(ctc_tty_info * info)
406 DBF_TEXT(trace, 3, __FUNCTION__);
407 if (info->flags & CTC_ASYNC_INITIALIZED)
409 #ifdef CTC_DEBUG_MODEM_OPEN
410 printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
413 * Now, initialize the UART
415 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
417 clear_bit(TTY_IO_ERROR, &info->tty->flags);
419 * and set the speed of the serial port
421 ctc_tty_change_speed(info);
423 info->flags |= CTC_ASYNC_INITIALIZED;
424 if (!(info->flags & CTC_ASYNC_NETDEV_OPEN))
425 info->netdev->open(info->netdev);
426 info->flags |= CTC_ASYNC_NETDEV_OPEN;
431 ctc_tty_stopdev(unsigned long data)
433 ctc_tty_info *info = (ctc_tty_info *)data;
435 if ((!info) || (!info->netdev) ||
436 (info->flags & CTC_ASYNC_INITIALIZED))
438 info->netdev->stop(info->netdev);
439 info->flags &= ~CTC_ASYNC_NETDEV_OPEN;
443 * This routine will shutdown a serial port; interrupts are disabled, and
444 * DTR is dropped if the hangup on close termio flag is on.
447 ctc_tty_shutdown(ctc_tty_info * info)
449 DBF_TEXT(trace, 3, __FUNCTION__);
450 if (!(info->flags & CTC_ASYNC_INITIALIZED))
452 #ifdef CTC_DEBUG_MODEM_OPEN
453 printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
455 info->msr &= ~UART_MSR_RI;
456 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
457 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
459 set_bit(TTY_IO_ERROR, &info->tty->flags);
460 mod_timer(&info->stoptimer, jiffies + (10 * HZ));
461 skb_queue_purge(&info->tx_queue);
462 skb_queue_purge(&info->rx_queue);
463 info->flags &= ~CTC_ASYNC_INITIALIZED;
466 /* ctc_tty_write() is the main send-routine. It is called from the upper
467 * levels within the kernel to perform sending data. Depending on the
468 * online-flag it either directs output to the at-command-interpreter or
469 * to the lower level. Additional tasks done here:
470 * - If online, check for escape-sequence (+++)
471 * - If sending audio-data, call ctc_tty_DLEdown() to parse DLE-codes.
472 * - If receiving audio-data, call ctc_tty_end_vrx() to abort if needed.
473 * - If dialing, abort dial.
476 ctc_tty_write(struct tty_struct *tty, const u_char * buf, int count)
480 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
482 DBF_TEXT(trace, 5, __FUNCTION__);
483 if (ctc_tty_shuttingdown)
485 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write"))
497 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
501 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
503 skb = dev_alloc_skb(skb_res + c);
506 "ctc_tty: Out of memory in %s%d write\n",
507 CTC_TTY_NAME, info->line);
510 skb_reserve(skb, skb_res);
511 memcpy(skb_put(skb, c), buf, c);
512 skb_queue_tail(&info->tx_queue, skb);
517 if (!skb_queue_empty(&info->tx_queue)) {
518 info->lsr &= ~UART_LSR_TEMT;
519 tasklet_schedule(&info->tasklet);
522 DBF_TEXT(trace, 6, __FUNCTION__);
527 ctc_tty_write_room(struct tty_struct *tty)
529 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
531 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write_room"))
533 return CTC_TTY_XMIT_SIZE;
537 ctc_tty_chars_in_buffer(struct tty_struct *tty)
539 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
541 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_chars_in_buffer"))
547 ctc_tty_flush_buffer(struct tty_struct *tty)
552 DBF_TEXT(trace, 4, __FUNCTION__);
555 spin_lock_irqsave(&ctc_tty_lock, flags);
556 info = (ctc_tty_info *) tty->driver_data;
557 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_buffer")) {
558 spin_unlock_irqrestore(&ctc_tty_lock, flags);
561 skb_queue_purge(&info->tx_queue);
562 info->lsr |= UART_LSR_TEMT;
563 spin_unlock_irqrestore(&ctc_tty_lock, flags);
564 wake_up_interruptible(&tty->write_wait);
567 DBF_TEXT_(trace, 2, "ex: %s ", __FUNCTION__);
572 ctc_tty_flush_chars(struct tty_struct *tty)
574 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
576 DBF_TEXT(trace, 4, __FUNCTION__);
577 if (ctc_tty_shuttingdown)
579 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
581 if (tty->stopped || tty->hw_stopped || skb_queue_empty(&info->tx_queue))
583 tasklet_schedule(&info->tasklet);
587 * ------------------------------------------------------------
590 * This routine is called by the upper-layer tty layer to signal that
591 * incoming characters should be throttled.
592 * ------------------------------------------------------------
595 ctc_tty_throttle(struct tty_struct *tty)
597 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
599 DBF_TEXT(trace, 4, __FUNCTION__);
600 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_throttle"))
602 info->mcr &= ~UART_MCR_RTS;
604 ctc_tty_inject(info, STOP_CHAR(tty));
605 ctc_tty_transmit_status(info);
609 ctc_tty_unthrottle(struct tty_struct *tty)
611 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
613 DBF_TEXT(trace, 4, __FUNCTION__);
614 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_unthrottle"))
616 info->mcr |= UART_MCR_RTS;
618 ctc_tty_inject(info, START_CHAR(tty));
619 ctc_tty_transmit_status(info);
623 * ------------------------------------------------------------
624 * ctc_tty_ioctl() and friends
625 * ------------------------------------------------------------
629 * ctc_tty_get_lsr_info - get line status register info
631 * Purpose: Let user call ioctl() to get info when the UART physically
632 * is emptied. On bus types like RS485, the transmitter must
633 * release the bus after transmitting. This must be done when
634 * the transmit shift register is empty, not be done when the
635 * transmit holding register is empty. This functionality
636 * allows RS485 driver to be written in user space.
639 ctc_tty_get_lsr_info(ctc_tty_info * info, uint __user *value)
645 DBF_TEXT(trace, 4, __FUNCTION__);
646 spin_lock_irqsave(&ctc_tty_lock, flags);
648 spin_unlock_irqrestore(&ctc_tty_lock, flags);
649 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
650 put_user(result, value);
655 static int ctc_tty_tiocmget(struct tty_struct *tty, struct file *file)
657 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
663 DBF_TEXT(trace, 4, __FUNCTION__);
664 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
666 if (tty->flags & (1 << TTY_IO_ERROR))
670 spin_lock_irqsave(&ctc_tty_lock, flags);
672 spin_unlock_irqrestore(&ctc_tty_lock, flags);
673 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
674 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
675 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
676 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
677 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
678 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
683 ctc_tty_tiocmset(struct tty_struct *tty, struct file *file,
684 unsigned int set, unsigned int clear)
686 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
688 DBF_TEXT(trace, 4, __FUNCTION__);
689 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
691 if (tty->flags & (1 << TTY_IO_ERROR))
695 info->mcr |= UART_MCR_RTS;
697 info->mcr |= UART_MCR_DTR;
699 if (clear & TIOCM_RTS)
700 info->mcr &= ~UART_MCR_RTS;
701 if (clear & TIOCM_DTR)
702 info->mcr &= ~UART_MCR_DTR;
704 if ((set | clear) & (TIOCM_RTS|TIOCM_DTR))
705 ctc_tty_transmit_status(info);
710 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
713 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
717 DBF_TEXT(trace, 4, __FUNCTION__);
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 __user *) 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 __user *) 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 if (access_ok(VERIFY_WRITE, (void __user *) arg, sizeof(uint)))
766 return ctc_tty_get_lsr_info(info, (uint __user *) arg);
770 #ifdef CTC_DEBUG_MODEM_IOCTL
771 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
772 CTC_TTY_NAME, info->line);
780 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
782 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
783 unsigned int cflag = tty->termios->c_cflag;
785 DBF_TEXT(trace, 4, __FUNCTION__);
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);
823 DBF_TEXT(trace, 4, __FUNCTION__);
825 * If the device is in the middle of being closed, then block
826 * until it's done, and then try again.
828 if (tty_hung_up_p(filp) ||
829 (info->flags & CTC_ASYNC_CLOSING)) {
830 if (info->flags & CTC_ASYNC_CLOSING)
831 wait_event(info->close_wait,
832 !(info->flags & CTC_ASYNC_CLOSING));
833 #ifdef MODEM_DO_RESTART
834 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
843 * If non-blocking mode is set, then make the check up front
846 if ((filp->f_flags & O_NONBLOCK) ||
847 (tty->flags & (1 << TTY_IO_ERROR))) {
848 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
851 if (tty->termios->c_cflag & CLOCAL)
854 * Block waiting for the carrier detect and the line to become
855 * free (i.e., not in use by the callout). While we are in
856 * this loop, info->count is dropped by one, so that
857 * ctc_tty_close() knows when to free things. We restore it upon
858 * exit, either normal or abnormal.
861 add_wait_queue(&info->open_wait, &wait);
862 #ifdef CTC_DEBUG_MODEM_OPEN
863 printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
864 CTC_TTY_NAME, info->line, info->count);
866 spin_lock_irqsave(&ctc_tty_lock, flags);
867 if (!(tty_hung_up_p(filp)))
869 spin_unlock_irqrestore(&ctc_tty_lock, flags);
870 info->blocked_open++;
872 set_current_state(TASK_INTERRUPTIBLE);
873 if (tty_hung_up_p(filp) ||
874 !(info->flags & CTC_ASYNC_INITIALIZED)) {
875 #ifdef MODEM_DO_RESTART
876 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
879 retval = -ERESTARTSYS;
885 if (!(info->flags & CTC_ASYNC_CLOSING) &&
886 (do_clocal || (info->msr & UART_MSR_DCD))) {
889 if (signal_pending(current)) {
890 retval = -ERESTARTSYS;
893 #ifdef CTC_DEBUG_MODEM_OPEN
894 printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
895 CTC_TTY_NAME, info->line, info->count);
899 current->state = TASK_RUNNING;
900 remove_wait_queue(&info->open_wait, &wait);
901 if (!tty_hung_up_p(filp))
903 info->blocked_open--;
904 #ifdef CTC_DEBUG_MODEM_OPEN
905 printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
906 CTC_TTY_NAME, info->line, info->count);
910 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
915 * This routine is called whenever a serial port is opened. It
916 * enables interrupts for a serial port, linking in its async structure into
917 * the IRQ chain. It also performs the serial-specific
918 * initialization for the tty structure.
921 ctc_tty_open(struct tty_struct *tty, struct file *filp)
924 unsigned long saveflags;
928 DBF_TEXT(trace, 3, __FUNCTION__);
930 if (line < 0 || line > CTC_TTY_MAX_DEVICES)
932 info = &driver->info[line];
933 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
937 #ifdef CTC_DEBUG_MODEM_OPEN
938 printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
941 spin_lock_irqsave(&ctc_tty_lock, saveflags);
943 tty->driver_data = info;
945 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
947 * Start up serial port
949 retval = ctc_tty_startup(info);
951 #ifdef CTC_DEBUG_MODEM_OPEN
952 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
956 retval = ctc_tty_block_til_ready(tty, filp, info);
958 #ifdef CTC_DEBUG_MODEM_OPEN
959 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
963 #ifdef CTC_DEBUG_MODEM_OPEN
964 printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
970 ctc_tty_close(struct tty_struct *tty, struct file *filp)
972 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
975 DBF_TEXT(trace, 3, __FUNCTION__);
976 if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
978 spin_lock_irqsave(&ctc_tty_lock, flags);
979 if (tty_hung_up_p(filp)) {
980 spin_unlock_irqrestore(&ctc_tty_lock, flags);
981 #ifdef CTC_DEBUG_MODEM_OPEN
982 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
986 if ((tty->count == 1) && (info->count != 1)) {
988 * Uh, oh. tty->count is 1, which means that the tty
989 * structure will be freed. Info->count should always
990 * be one in these conditions. If it's greater than
991 * one, we've got real problems, since it means the
992 * serial port won't be shutdown.
994 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
995 "info->count is %d\n", info->count);
998 if (--info->count < 0) {
999 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
1000 CTC_TTY_NAME, info->line, info->count);
1004 local_irq_restore(flags);
1005 #ifdef CTC_DEBUG_MODEM_OPEN
1006 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1010 info->flags |= CTC_ASYNC_CLOSING;
1013 * At this point we stop accepting input. To do this, we
1014 * disable the receive line status interrupts, and tell the
1015 * interrupt driver to stop checking the data ready bit in the
1016 * line status register.
1018 if (info->flags & CTC_ASYNC_INITIALIZED) {
1019 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
1021 * Before we drop DTR, make sure the UART transmitter
1022 * has completely drained; this is especially
1023 * important if there is a transmit FIFO!
1025 timeout = jiffies + HZ;
1026 while (!(info->lsr & UART_LSR_TEMT)) {
1027 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1029 spin_lock_irqsave(&ctc_tty_lock, flags);
1030 if (time_after(jiffies,timeout))
1034 ctc_tty_shutdown(info);
1035 if (tty->driver->flush_buffer) {
1036 skb_queue_purge(&info->tx_queue);
1037 info->lsr |= UART_LSR_TEMT;
1039 tty_ldisc_flush(tty);
1042 if (info->blocked_open) {
1043 msleep_interruptible(500);
1044 wake_up_interruptible(&info->open_wait);
1046 info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
1047 wake_up_interruptible(&info->close_wait);
1048 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1049 #ifdef CTC_DEBUG_MODEM_OPEN
1050 printk(KERN_DEBUG "ctc_tty_close normal exit\n");
1055 * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1058 ctc_tty_hangup(struct tty_struct *tty)
1060 ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1061 unsigned long saveflags;
1062 DBF_TEXT(trace, 3, __FUNCTION__);
1063 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
1065 ctc_tty_shutdown(info);
1067 info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1068 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1070 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1071 wake_up_interruptible(&info->open_wait);
1076 * For all online tty's, try sending data to
1080 ctc_tty_task(unsigned long arg)
1082 ctc_tty_info *info = (void *)arg;
1083 unsigned long saveflags;
1086 DBF_TEXT(trace, 3, __FUNCTION__);
1087 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1088 if ((!ctc_tty_shuttingdown) && info) {
1089 again = ctc_tty_tint(info);
1091 info->lsr |= UART_LSR_TEMT;
1092 again |= ctc_tty_readmodem(info);
1094 tasklet_schedule(&info->tasklet);
1097 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1100 static struct tty_operations ctc_ops = {
1101 .open = ctc_tty_open,
1102 .close = ctc_tty_close,
1103 .write = ctc_tty_write,
1104 .flush_chars = ctc_tty_flush_chars,
1105 .write_room = ctc_tty_write_room,
1106 .chars_in_buffer = ctc_tty_chars_in_buffer,
1107 .flush_buffer = ctc_tty_flush_buffer,
1108 .ioctl = ctc_tty_ioctl,
1109 .throttle = ctc_tty_throttle,
1110 .unthrottle = ctc_tty_unthrottle,
1111 .set_termios = ctc_tty_set_termios,
1112 .hangup = ctc_tty_hangup,
1113 .tiocmget = ctc_tty_tiocmget,
1114 .tiocmset = ctc_tty_tiocmset,
1122 struct tty_driver *device;
1124 DBF_TEXT(trace, 2, __FUNCTION__);
1125 driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1126 if (driver == NULL) {
1127 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1130 memset(driver, 0, sizeof(ctc_tty_driver));
1131 device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
1134 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1138 device->devfs_name = "ctc/" CTC_TTY_NAME;
1139 device->name = CTC_TTY_NAME;
1140 device->major = CTC_TTY_MAJOR;
1141 device->minor_start = 0;
1142 device->type = TTY_DRIVER_TYPE_SERIAL;
1143 device->subtype = SERIAL_TYPE_NORMAL;
1144 device->init_termios = tty_std_termios;
1145 device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1146 device->flags = TTY_DRIVER_REAL_RAW;
1147 device->driver_name = "ctc_tty",
1148 tty_set_operations(device, &ctc_ops);
1149 if (tty_register_driver(device)) {
1150 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1151 put_tty_driver(device);
1155 driver->ctc_tty_device = device;
1156 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1157 info = &driver->info[i];
1158 init_MUTEX(&info->write_sem);
1159 tasklet_init(&info->tasklet, ctc_tty_task,
1160 (unsigned long) info);
1161 info->magic = CTC_ASYNC_MAGIC;
1165 info->blocked_open = 0;
1166 init_waitqueue_head(&info->open_wait);
1167 init_waitqueue_head(&info->close_wait);
1168 skb_queue_head_init(&info->tx_queue);
1169 skb_queue_head_init(&info->rx_queue);
1170 init_timer(&info->stoptimer);
1171 info->stoptimer.function = ctc_tty_stopdev;
1172 info->stoptimer.data = (unsigned long)info;
1173 info->mcr = UART_MCR_RTS;
1179 ctc_tty_register_netdev(struct net_device *dev) {
1184 DBF_TEXT(trace, 2, __FUNCTION__);
1185 if ((!dev) || (!dev->name)) {
1187 "ctc_tty_register_netdev called "
1188 "with NULL dev or NULL dev-name\n");
1193 * If the name is a format string the caller wants us to
1194 * do a name allocation : format string must end with %d
1196 if (strchr(dev->name, '%'))
1198 int err = dev_alloc_name(dev, dev->name); // dev->name is changed by this
1200 printk(KERN_DEBUG "dev_alloc returned error %d\n", err);
1206 for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1207 ttynum = simple_strtoul(p, &err, 0);
1208 if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1211 "ctc_tty_register_netdev called "
1212 "with number in name '%s'\n", dev->name);
1215 if (driver->info[ttynum].netdev) {
1217 "ctc_tty_register_netdev called "
1218 "for already registered device '%s'\n",
1222 driver->info[ttynum].netdev = dev;
1227 ctc_tty_unregister_netdev(struct net_device *dev) {
1229 unsigned long saveflags;
1230 ctc_tty_info *info = NULL;
1232 DBF_TEXT(trace, 2, __FUNCTION__);
1233 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1234 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
1235 if (driver->info[i].netdev == dev) {
1236 info = &driver->info[i];
1240 info->netdev = NULL;
1241 skb_queue_purge(&info->tx_queue);
1242 skb_queue_purge(&info->rx_queue);
1244 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1248 ctc_tty_cleanup(void) {
1249 unsigned long saveflags;
1251 DBF_TEXT(trace, 2, __FUNCTION__);
1252 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1253 ctc_tty_shuttingdown = 1;
1254 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1255 tty_unregister_driver(driver->ctc_tty_device);
1256 put_tty_driver(driver->ctc_tty_device);