Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / s390 / net / ctctty.c
blobd8bad5ad7e253186b328d8ffa93bbd30df0b4946
1 /*
2 * $Id: ctctty.c,v 1.12 2003/06/17 11:36: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)
12 * any later version.
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>
32 #include "ctctty.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>) */
51 typedef struct {
52 int magic;
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 */
57 int line;
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;
69 } ctc_tty_info;
71 /* Description of one CTC-tty */
72 typedef struct {
73 struct tty_driver *ctc_tty_device; /* tty-device */
74 ctc_tty_info info[CTC_TTY_MAX_DEVICES]; /* Private data */
75 } ctc_tty_driver;
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.
94 * Return:
95 * 1 = Success
96 * 0 = Failure, data has to be buffered and later processed by
97 * ctc_tty_readmodem().
99 static int
100 ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
102 int c;
103 int len;
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;
109 len = skb->len;
110 if (c >= len) {
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);
117 kfree_skb(skb);
118 return 1;
122 return 0;
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.
129 static int
130 ctc_tty_readmodem(ctc_tty_info *info)
132 int ret = 1;
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;
138 struct sk_buff *skb;
140 if ((c > 0) && (skb = skb_dequeue(&info->rx_queue))) {
141 int len = skb->len;
142 if (len > c)
143 len = c;
144 memcpy(tty->flip.char_buf_ptr, skb->data, len);
145 skb_pull(skb, 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);
151 if (skb->len > 0)
152 skb_queue_head(&info->rx_queue, skb);
153 else {
154 kfree_skb(skb);
155 ret = skb_queue_len(&info->rx_queue);
160 return ret;
163 void
164 ctc_tty_setcarrier(struct net_device *netdev, int on)
166 int i;
168 if ((!driver) || ctc_tty_shuttingdown)
169 return;
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];
173 if (on)
174 info->msr |= UART_MSR_DCD;
175 else
176 info->msr &= ~UART_MSR_DCD;
177 if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
178 tty_hangup(info->tty);
182 void
183 ctc_tty_netif_rx(struct sk_buff *skb)
185 int i;
186 ctc_tty_info *info = NULL;
188 if (!skb)
189 return;
190 if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
191 dev_kfree_skb(skb);
192 return;
194 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
195 if (driver->info[i].netdev == skb->dev) {
196 info = &driver->info[i];
197 break;
199 if (!info) {
200 dev_kfree_skb(skb);
201 return;
203 if (skb->len < 6) {
204 dev_kfree_skb(skb);
205 return;
207 if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
208 dev_kfree_skb(skb);
209 return;
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;
219 } else {
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;
226 else
227 info->msr &= ~UART_MSR_DSR;
228 if (skb->len <= 0) {
229 kfree_skb(skb);
230 return;
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))
235 return;
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);
244 static int
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);
249 int wake = 1;
250 int rc;
252 if (!info->netdev) {
253 if (skb)
254 kfree_skb(skb);
255 return 0;
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.
263 if (skb)
264 skb_queue_head(&info->tx_queue, skb);
266 skb = dev_alloc_skb(skb_res);
267 if (!skb) {
268 printk(KERN_WARNING
269 "ctc_tty: Out of memory in %s%d tint\n",
270 CTC_TTY_NAME, info->line);
271 return 1;
273 skb_reserve(skb, skb_res);
274 stopped = 0;
275 wake = 0;
277 if (!skb)
278 return 0;
279 if (stopped) {
280 skb_queue_head(&info->tx_queue, skb);
281 return 1;
283 #if 0
284 if (skb->len > 0)
285 printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
286 else
287 printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
288 #endif
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);
292 if (rc) {
293 skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
294 if (skb->len > 0)
295 skb_queue_head(&info->tx_queue, skb);
296 else
297 kfree_skb(skb);
298 } else {
299 struct tty_struct *tty = info->tty;
301 info->flags &= ~CTC_ASYNC_TX_LINESTAT;
302 if (tty) {
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 /************************************************************
314 * Modem-functions
316 * mostly "stolen" from original Linux-serial.c and friends.
318 ************************************************************/
320 static inline int
321 ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
323 #ifdef MODEM_PARANOIA_CHECK
324 if (!info) {
325 printk(KERN_WARNING "ctc_tty: null info_struct for %s in %s\n",
326 name, routine);
327 return 1;
329 if (info->magic != CTC_ASYNC_MAGIC) {
330 printk(KERN_WARNING "ctc_tty: bad magic for info struct %s in %s\n",
331 name, routine);
332 return 1;
334 #endif
335 return 0;
338 static void
339 ctc_tty_inject(ctc_tty_info *info, char c)
341 int skb_res;
342 struct sk_buff *skb;
344 if (ctc_tty_shuttingdown)
345 return;
346 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
347 sizeof(__u32) + 1;
348 skb = dev_alloc_skb(skb_res);
349 if (!skb) {
350 printk(KERN_WARNING
351 "ctc_tty: Out of memory in %s%d tx_inject\n",
352 CTC_TTY_NAME, info->line);
353 return;
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);
361 static void
362 ctc_tty_transmit_status(ctc_tty_info *info)
364 if (ctc_tty_shuttingdown)
365 return;
366 info->flags |= CTC_ASYNC_TX_LINESTAT;
367 tasklet_schedule(&info->tasklet);
370 static void
371 ctc_tty_change_speed(ctc_tty_info * info)
373 unsigned int cflag;
374 unsigned int quot;
375 int i;
377 if (!info->tty || !info->tty->termios)
378 return;
379 cflag = info->tty->termios->c_cflag;
381 quot = i = cflag & CBAUD;
382 if (i & CBAUDEX) {
383 i &= ~CBAUDEX;
384 if (i < 1 || i > 2)
385 info->tty->termios->c_cflag &= ~CBAUDEX;
386 else
387 i += 15;
389 if (quot) {
390 info->mcr |= UART_MCR_DTR;
391 info->mcr |= UART_MCR_RTS;
392 ctc_tty_transmit_status(info);
393 } else {
394 info->mcr &= ~UART_MCR_DTR;
395 info->mcr &= ~UART_MCR_RTS;
396 ctc_tty_transmit_status(info);
397 return;
400 /* CTS flow control flag and modem status interrupts */
401 if (cflag & CRTSCTS) {
402 info->flags |= CTC_ASYNC_CTS_FLOW;
403 } else
404 info->flags &= ~CTC_ASYNC_CTS_FLOW;
405 if (cflag & CLOCAL)
406 info->flags &= ~CTC_ASYNC_CHECK_CD;
407 else {
408 info->flags |= CTC_ASYNC_CHECK_CD;
412 static int
413 ctc_tty_startup(ctc_tty_info * info)
415 if (info->flags & CTC_ASYNC_INITIALIZED)
416 return 0;
417 #ifdef CTC_DEBUG_MODEM_OPEN
418 printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
419 #endif
421 * Now, initialize the UART
423 info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
424 if (info->tty)
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;
435 return 0;
438 static void
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))
445 return;
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.
454 static void
455 ctc_tty_shutdown(ctc_tty_info * info)
457 if (!(info->flags & CTC_ASYNC_INITIALIZED))
458 return;
459 #ifdef CTC_DEBUG_MODEM_OPEN
460 printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
461 #endif
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);
465 if (info->tty)
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.
482 static int
483 ctc_tty_write(struct tty_struct *tty, int from_user, const u_char * buf, int count)
485 int c;
486 int total = 0;
487 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
489 if (ctc_tty_shuttingdown)
490 return 0;
491 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write"))
492 return 0;
493 if (!tty)
494 return 0;
495 if (!info->netdev)
496 return -ENODEV;
497 if (from_user)
498 down(&info->write_sem);
499 while (1) {
500 struct sk_buff *skb;
501 int skb_res;
503 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
504 if (c <= 0)
505 break;
507 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
508 + sizeof(__u32);
509 skb = dev_alloc_skb(skb_res + c);
510 if (!skb) {
511 printk(KERN_WARNING
512 "ctc_tty: Out of memory in %s%d write\n",
513 CTC_TTY_NAME, info->line);
514 break;
516 skb_reserve(skb, skb_res);
517 if (from_user)
518 copy_from_user(skb_put(skb, c), buf, c);
519 else
520 memcpy(skb_put(skb, c), buf, c);
521 skb_queue_tail(&info->tx_queue, skb);
522 buf += c;
523 total += c;
524 count -= c;
526 if (skb_queue_len(&info->tx_queue)) {
527 info->lsr &= ~UART_LSR_TEMT;
528 tasklet_schedule(&info->tasklet);
530 if (from_user)
531 up(&info->write_sem);
532 return total;
535 static int
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"))
541 return 0;
542 return CTC_TTY_XMIT_SIZE;
545 static int
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"))
551 return 0;
552 return 0;
555 static void
556 ctc_tty_flush_buffer(struct tty_struct *tty)
558 ctc_tty_info *info;
559 unsigned long flags;
561 if (!tty)
562 return;
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);
567 return;
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);
578 static void
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)
584 return;
585 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
586 return;
587 if (tty->stopped || tty->hw_stopped || (!skb_queue_len(&info->tx_queue)))
588 return;
589 tasklet_schedule(&info->tasklet);
593 * ------------------------------------------------------------
594 * ctc_tty_throttle()
596 * This routine is called by the upper-layer tty layer to signal that
597 * incoming characters should be throttled.
598 * ------------------------------------------------------------
600 static void
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"))
606 return;
607 info->mcr &= ~UART_MCR_RTS;
608 if (I_IXOFF(tty))
609 ctc_tty_inject(info, STOP_CHAR(tty));
610 ctc_tty_transmit_status(info);
613 static void
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"))
619 return;
620 info->mcr |= UART_MCR_RTS;
621 if (I_IXOFF(tty))
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.
642 static int
643 ctc_tty_get_lsr_info(ctc_tty_info * info, uint * value)
645 u_char status;
646 uint result;
647 ulong flags;
649 spin_lock_irqsave(&ctc_tty_lock, flags);
650 status = info->lsr;
651 spin_unlock_irqrestore(&ctc_tty_lock, flags);
652 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
653 put_user(result, (uint *) value);
654 return 0;
658 static int
659 ctc_tty_get_ctc_tty_info(ctc_tty_info * info, uint * value)
661 u_char control,
662 status;
663 uint result;
664 ulong flags;
666 control = info->mcr;
667 spin_lock_irqsave(&ctc_tty_lock, flags);
668 status = info->msr;
669 spin_unlock_irqrestore(&ctc_tty_lock, flags);
670 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
671 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
672 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
673 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
674 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
675 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
676 put_user(result, (uint *) value);
677 return 0;
680 static int
681 ctc_tty_set_ctc_tty_info(ctc_tty_info * info, uint cmd, uint * value)
683 uint arg;
684 int old_mcr = info->mcr & (UART_MCR_RTS | UART_MCR_DTR);
686 get_user(arg, (uint *) value);
687 switch (cmd) {
688 case TIOCMBIS:
689 #ifdef CTC_DEBUG_MODEM_IOCTL
690 printk(KERN_DEBUG "%s%d ioctl TIOCMBIS\n", CTC_TTY_NAME,
691 info->line);
692 #endif
693 if (arg & TIOCM_RTS)
694 info->mcr |= UART_MCR_RTS;
695 if (arg & TIOCM_DTR)
696 info->mcr |= UART_MCR_DTR;
697 break;
698 case TIOCMBIC:
699 #ifdef CTC_DEBUG_MODEM_IOCTL
700 printk(KERN_DEBUG "%s%d ioctl TIOCMBIC\n", CTC_TTY_NAME,
701 info->line);
702 #endif
703 if (arg & TIOCM_RTS)
704 info->mcr &= ~UART_MCR_RTS;
705 if (arg & TIOCM_DTR)
706 info->mcr &= ~UART_MCR_DTR;
707 break;
708 case TIOCMSET:
709 #ifdef CTC_DEBUG_MODEM_IOCTL
710 printk(KERN_DEBUG "%s%d ioctl TIOCMSET\n", CTC_TTY_NAME,
711 info->line);
712 #endif
713 info->mcr = ((info->mcr & ~(UART_MCR_RTS | UART_MCR_DTR))
714 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
715 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
716 break;
717 default:
718 return -EINVAL;
720 if ((info->mcr & (UART_MCR_RTS | UART_MCR_DTR)) != old_mcr)
721 ctc_tty_transmit_status(info);
722 return 0;
725 static int
726 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
727 uint cmd, ulong arg)
729 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
730 int error;
731 int retval;
733 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
734 return -ENODEV;
735 if (tty->flags & (1 << TTY_IO_ERROR))
736 return -EIO;
737 switch (cmd) {
738 case TCSBRK: /* SVID version: non-zero arg --> no break */
739 #ifdef CTC_DEBUG_MODEM_IOCTL
740 printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
741 #endif
742 retval = tty_check_change(tty);
743 if (retval)
744 return retval;
745 tty_wait_until_sent(tty, 0);
746 return 0;
747 case TCSBRKP: /* support for POSIX tcsendbreak() */
748 #ifdef CTC_DEBUG_MODEM_IOCTL
749 printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
750 #endif
751 retval = tty_check_change(tty);
752 if (retval)
753 return retval;
754 tty_wait_until_sent(tty, 0);
755 return 0;
756 case TIOCGSOFTCAR:
757 #ifdef CTC_DEBUG_MODEM_IOCTL
758 printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
759 info->line);
760 #endif
761 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long));
762 if (error)
763 return error;
764 put_user(C_CLOCAL(tty) ? 1 : 0, (ulong *) arg);
765 return 0;
766 case TIOCSSOFTCAR:
767 #ifdef CTC_DEBUG_MODEM_IOCTL
768 printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
769 info->line);
770 #endif
771 error = verify_area(VERIFY_READ, (void *) arg, sizeof(long));
772 if (error)
773 return error;
774 get_user(arg, (ulong *) arg);
775 tty->termios->c_cflag =
776 ((tty->termios->c_cflag & ~CLOCAL) |
777 (arg ? CLOCAL : 0));
778 return 0;
779 case TIOCMGET:
780 #ifdef CTC_DEBUG_MODEM_IOCTL
781 printk(KERN_DEBUG "%s%d ioctl TIOCMGET\n", CTC_TTY_NAME,
782 info->line);
783 #endif
784 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(uint));
785 if (error)
786 return error;
787 return ctc_tty_get_ctc_tty_info(info, (uint *) arg);
788 case TIOCMBIS:
789 case TIOCMBIC:
790 case TIOCMSET:
791 error = verify_area(VERIFY_READ, (void *) arg, sizeof(uint));
792 if (error)
793 return error;
794 return ctc_tty_set_ctc_tty_info(info, cmd, (uint *) arg);
795 case TIOCSERGETLSR: /* Get line status register */
796 #ifdef CTC_DEBUG_MODEM_IOCTL
797 printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
798 info->line);
799 #endif
800 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(uint));
801 if (error)
802 return error;
803 else
804 return ctc_tty_get_lsr_info(info, (uint *) arg);
805 default:
806 #ifdef CTC_DEBUG_MODEM_IOCTL
807 printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
808 CTC_TTY_NAME, info->line);
809 #endif
810 return -ENOIOCTLCMD;
812 return 0;
815 static void
816 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
818 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
819 unsigned int cflag = tty->termios->c_cflag;
821 ctc_tty_change_speed(info);
823 /* Handle transition to B0 */
824 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
825 info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
826 ctc_tty_transmit_status(info);
829 /* Handle transition from B0 to other */
830 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
831 info->mcr |= UART_MCR_DTR;
832 if (!(tty->termios->c_cflag & CRTSCTS) ||
833 !test_bit(TTY_THROTTLED, &tty->flags)) {
834 info->mcr |= UART_MCR_RTS;
836 ctc_tty_transmit_status(info);
839 /* Handle turning off CRTSCTS */
840 if ((old_termios->c_cflag & CRTSCTS) &&
841 !(tty->termios->c_cflag & CRTSCTS))
842 tty->hw_stopped = 0;
846 * ------------------------------------------------------------
847 * ctc_tty_open() and friends
848 * ------------------------------------------------------------
850 static int
851 ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
853 DECLARE_WAITQUEUE(wait, NULL);
854 int do_clocal = 0;
855 unsigned long flags;
856 int retval;
859 * If the device is in the middle of being closed, then block
860 * until it's done, and then try again.
862 if (tty_hung_up_p(filp) ||
863 (info->flags & CTC_ASYNC_CLOSING)) {
864 if (info->flags & CTC_ASYNC_CLOSING)
865 wait_event(info->close_wait,
866 !(info->flags & CTC_ASYNC_CLOSING));
867 #ifdef MODEM_DO_RESTART
868 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
869 return -EAGAIN;
870 else
871 return -ERESTARTSYS;
872 #else
873 return -EAGAIN;
874 #endif
877 * If non-blocking mode is set, then make the check up front
878 * and then exit.
880 if ((filp->f_flags & O_NONBLOCK) ||
881 (tty->flags & (1 << TTY_IO_ERROR))) {
882 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
883 return 0;
885 if (tty->termios->c_cflag & CLOCAL)
886 do_clocal = 1;
888 * Block waiting for the carrier detect and the line to become
889 * free (i.e., not in use by the callout). While we are in
890 * this loop, info->count is dropped by one, so that
891 * ctc_tty_close() knows when to free things. We restore it upon
892 * exit, either normal or abnormal.
894 retval = 0;
895 add_wait_queue(&info->open_wait, &wait);
896 #ifdef CTC_DEBUG_MODEM_OPEN
897 printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
898 CTC_TTY_NAME, info->line, info->count);
899 #endif
900 spin_lock_irqsave(&ctc_tty_lock, flags);
901 if (!(tty_hung_up_p(filp)))
902 info->count--;
903 spin_unlock_irqrestore(&ctc_tty_lock, flags);
904 info->blocked_open++;
905 while (1) {
906 set_current_state(TASK_INTERRUPTIBLE);
907 if (tty_hung_up_p(filp) ||
908 !(info->flags & CTC_ASYNC_INITIALIZED)) {
909 #ifdef MODEM_DO_RESTART
910 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
911 retval = -EAGAIN;
912 else
913 retval = -ERESTARTSYS;
914 #else
915 retval = -EAGAIN;
916 #endif
917 break;
919 if (!(info->flags & CTC_ASYNC_CLOSING) &&
920 (do_clocal || (info->msr & UART_MSR_DCD))) {
921 break;
923 if (signal_pending(current)) {
924 retval = -ERESTARTSYS;
925 break;
927 #ifdef CTC_DEBUG_MODEM_OPEN
928 printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
929 CTC_TTY_NAME, info->line, info->count);
930 #endif
931 schedule();
933 current->state = TASK_RUNNING;
934 remove_wait_queue(&info->open_wait, &wait);
935 if (!tty_hung_up_p(filp))
936 info->count++;
937 info->blocked_open--;
938 #ifdef CTC_DEBUG_MODEM_OPEN
939 printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
940 CTC_TTY_NAME, info->line, info->count);
941 #endif
942 if (retval)
943 return retval;
944 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
945 return 0;
949 * This routine is called whenever a serial port is opened. It
950 * enables interrupts for a serial port, linking in its async structure into
951 * the IRQ chain. It also performs the serial-specific
952 * initialization for the tty structure.
954 static int
955 ctc_tty_open(struct tty_struct *tty, struct file *filp)
957 ctc_tty_info *info;
958 unsigned long saveflags;
959 int retval,
960 line;
962 line = tty->index;
963 if (line < 0 || line > CTC_TTY_MAX_DEVICES)
964 return -ENODEV;
965 info = &driver->info[line];
966 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
967 return -ENODEV;
968 if (!info->netdev)
969 return -ENODEV;
970 #ifdef CTC_DEBUG_MODEM_OPEN
971 printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
972 info->count);
973 #endif
974 spin_lock_irqsave(&ctc_tty_lock, saveflags);
975 info->count++;
976 tty->driver_data = info;
977 info->tty = tty;
978 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
980 * Start up serial port
982 retval = ctc_tty_startup(info);
983 if (retval) {
984 #ifdef CTC_DEBUG_MODEM_OPEN
985 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
986 #endif
987 return retval;
989 retval = ctc_tty_block_til_ready(tty, filp, info);
990 if (retval) {
991 #ifdef CTC_DEBUG_MODEM_OPEN
992 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
993 #endif
994 return retval;
996 #ifdef CTC_DEBUG_MODEM_OPEN
997 printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
998 #endif
999 return 0;
1002 static void
1003 ctc_tty_close(struct tty_struct *tty, struct file *filp)
1005 ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
1006 ulong flags;
1007 ulong timeout;
1009 if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
1010 return;
1011 spin_lock_irqsave(&ctc_tty_lock, flags);
1012 if (tty_hung_up_p(filp)) {
1013 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1014 #ifdef CTC_DEBUG_MODEM_OPEN
1015 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
1016 #endif
1017 return;
1019 if ((tty->count == 1) && (info->count != 1)) {
1021 * Uh, oh. tty->count is 1, which means that the tty
1022 * structure will be freed. Info->count should always
1023 * be one in these conditions. If it's greater than
1024 * one, we've got real problems, since it means the
1025 * serial port won't be shutdown.
1027 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
1028 "info->count is %d\n", info->count);
1029 info->count = 1;
1031 if (--info->count < 0) {
1032 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
1033 CTC_TTY_NAME, info->line, info->count);
1034 info->count = 0;
1036 if (info->count) {
1037 local_irq_restore(flags);
1038 #ifdef CTC_DEBUG_MODEM_OPEN
1039 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1040 #endif
1041 return;
1043 info->flags |= CTC_ASYNC_CLOSING;
1044 tty->closing = 1;
1046 * At this point we stop accepting input. To do this, we
1047 * disable the receive line status interrupts, and tell the
1048 * interrupt driver to stop checking the data ready bit in the
1049 * line status register.
1051 if (info->flags & CTC_ASYNC_INITIALIZED) {
1052 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
1054 * Before we drop DTR, make sure the UART transmitter
1055 * has completely drained; this is especially
1056 * important if there is a transmit FIFO!
1058 timeout = jiffies + HZ;
1059 while (!(info->lsr & UART_LSR_TEMT)) {
1060 set_current_state(TASK_INTERRUPTIBLE);
1061 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1062 schedule_timeout(HZ/2);
1063 spin_lock_irqsave(&ctc_tty_lock, flags);
1064 if (time_after(jiffies,timeout))
1065 break;
1068 ctc_tty_shutdown(info);
1069 if (tty->driver->flush_buffer)
1070 tty->driver->flush_buffer(tty);
1071 if (tty->ldisc.flush_buffer)
1072 tty->ldisc.flush_buffer(tty);
1073 info->tty = 0;
1074 tty->closing = 0;
1075 if (info->blocked_open) {
1076 set_current_state(TASK_INTERRUPTIBLE);
1077 schedule_timeout(HZ/2);
1078 wake_up_interruptible(&info->open_wait);
1080 info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
1081 wake_up_interruptible(&info->close_wait);
1082 spin_unlock_irqrestore(&ctc_tty_lock, flags);
1083 #ifdef CTC_DEBUG_MODEM_OPEN
1084 printk(KERN_DEBUG "ctc_tty_close normal exit\n");
1085 #endif
1089 * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1091 static void
1092 ctc_tty_hangup(struct tty_struct *tty)
1094 ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1095 unsigned long saveflags;
1097 if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
1098 return;
1099 ctc_tty_shutdown(info);
1100 info->count = 0;
1101 info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1102 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1103 info->tty = 0;
1104 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1105 wake_up_interruptible(&info->open_wait);
1110 * For all online tty's, try sending data to
1111 * the lower levels.
1113 static void
1114 ctc_tty_task(unsigned long arg)
1116 ctc_tty_info *info = (void *)arg;
1117 unsigned long saveflags;
1118 int again;
1120 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1121 if ((!ctc_tty_shuttingdown) && info) {
1122 again = ctc_tty_tint(info);
1123 if (!again)
1124 info->lsr |= UART_LSR_TEMT;
1125 again |= ctc_tty_readmodem(info);
1126 if (again) {
1127 tasklet_schedule(&info->tasklet);
1130 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1133 static struct tty_operations ctc_ops = {
1134 .open = ctc_tty_open,
1135 .close = ctc_tty_close,
1136 .write = ctc_tty_write,
1137 .flush_chars = ctc_tty_flush_chars,
1138 .write_room = ctc_tty_write_room,
1139 .chars_in_buffer = ctc_tty_chars_in_buffer,
1140 .flush_buffer = ctc_tty_flush_buffer,
1141 .ioctl = ctc_tty_ioctl,
1142 .throttle = ctc_tty_throttle,
1143 .unthrottle = ctc_tty_unthrottle,
1144 .set_termios = ctc_tty_set_termios,
1145 .hangup = ctc_tty_hangup,
1149 ctc_tty_init(void)
1151 int i;
1152 ctc_tty_info *info;
1153 struct tty_driver *device;
1155 driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1156 if (driver == NULL) {
1157 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1158 return -ENOMEM;
1160 memset(driver, 0, sizeof(ctc_tty_driver));
1161 device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
1162 if (!device) {
1163 kfree(driver);
1164 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1165 return -ENOMEM;
1168 device->devfs_name = "ctc/" CTC_TTY_NAME;
1169 device->name = CTC_TTY_NAME;
1170 device->major = CTC_TTY_MAJOR;
1171 device->minor_start = 0;
1172 device->type = TTY_DRIVER_TYPE_SERIAL;
1173 device->subtype = SERIAL_TYPE_NORMAL;
1174 device->init_termios = tty_std_termios;
1175 device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1176 device->flags = TTY_DRIVER_REAL_RAW;
1177 device->driver_name = "ctc_tty",
1178 tty_set_operations(device, &ctc_ops);
1179 if (tty_register_driver(device)) {
1180 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1181 put_tty_driver(device);
1182 kfree(driver);
1183 return -1;
1185 driver->ctc_tty_device = device;
1186 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1187 info = &driver->info[i];
1188 init_MUTEX(&info->write_sem);
1189 tasklet_init(&info->tasklet, ctc_tty_task,
1190 (unsigned long) info);
1191 info->magic = CTC_ASYNC_MAGIC;
1192 info->line = i;
1193 info->tty = 0;
1194 info->count = 0;
1195 info->blocked_open = 0;
1196 init_waitqueue_head(&info->open_wait);
1197 init_waitqueue_head(&info->close_wait);
1198 skb_queue_head_init(&info->tx_queue);
1199 skb_queue_head_init(&info->rx_queue);
1200 init_timer(&info->stoptimer);
1201 info->stoptimer.function = ctc_tty_stopdev;
1202 info->stoptimer.data = (unsigned long)info;
1203 info->mcr = UART_MCR_RTS;
1205 return 0;
1209 ctc_tty_register_netdev(struct net_device *dev) {
1210 int ttynum;
1211 char *err;
1212 char *p;
1214 if ((!dev) || (!dev->name)) {
1215 printk(KERN_WARNING
1216 "ctc_tty_register_netdev called "
1217 "with NULL dev or NULL dev-name\n");
1218 return -1;
1220 for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1221 ttynum = simple_strtoul(p, &err, 0);
1222 if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1223 (err && *err)) {
1224 printk(KERN_WARNING
1225 "ctc_tty_register_netdev called "
1226 "with number in name '%s'\n", dev->name);
1227 return -1;
1229 if (driver->info[ttynum].netdev) {
1230 printk(KERN_WARNING
1231 "ctc_tty_register_netdev called "
1232 "for already registered device '%s'\n",
1233 dev->name);
1234 return -1;
1236 driver->info[ttynum].netdev = dev;
1237 return 0;
1240 void
1241 ctc_tty_unregister_netdev(struct net_device *dev) {
1242 int i;
1243 unsigned long saveflags;
1244 ctc_tty_info *info = NULL;
1246 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1247 for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
1248 if (driver->info[i].netdev == dev) {
1249 info = &driver->info[i];
1250 break;
1252 if (info) {
1253 info->netdev = NULL;
1254 skb_queue_purge(&info->tx_queue);
1255 skb_queue_purge(&info->rx_queue);
1257 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1260 void
1261 ctc_tty_cleanup(void) {
1262 unsigned long saveflags;
1264 spin_lock_irqsave(&ctc_tty_lock, saveflags);
1265 ctc_tty_shuttingdown = 1;
1266 tty_unregister_driver(driver->ctc_tty_device);
1267 kfree(driver);
1268 put_tty_driver(driver->ctc_tty_device);
1269 driver = NULL;
1270 spin_unlock_irqrestore(&ctc_tty_lock, saveflags);