- port, cleanup Emulab ICMP Ping Of Death (IPOD) patch from 2.4.x
[linux-2.6.git] / drivers / s390 / net / ctctty.c
1 /*
2  * $Id: ctctty.c,v 1.21 2004/07/02 16:31:22 ptiedem Exp $
3  *
4  * CTC / ESCON network driver, tty interface.
5  *
6  * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
8  *
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.
13  *
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.
18  *
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.
22  *
23  */
24
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"
33 #include "ctcdbug.h"
34
35 #define CTC_TTY_MAJOR       43
36 #define CTC_TTY_MAX_DEVICES 64
37
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    */
50
51 /* Private data (similar to async_struct in <linux/serial.h>) */
52 typedef struct {
53   int                   magic;
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           */
58   int                   line;
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;
70 } ctc_tty_info;
71
72 /* Description of one CTC-tty */
73 typedef struct {
74   struct tty_driver  *ctc_tty_device;              /* tty-device             */
75   ctc_tty_info       info[CTC_TTY_MAX_DEVICES];    /* Private data           */
76 } ctc_tty_driver;
77
78 static ctc_tty_driver *driver;
79
80 /* Leave this unchanged unless you know what you do! */
81 #define MODEM_PARANOIA_CHECK
82 #define MODEM_DO_RESTART
83
84 #define CTC_TTY_NAME "ctctty"
85
86 static __u32 ctc_tty_magic = CTC_ASYNC_MAGIC;
87 static int ctc_tty_shuttingdown = 0;
88
89 static spinlock_t ctc_tty_lock;
90
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.
94  *
95  * Return:
96  *  1 = Success
97  *  0 = Failure, data has to be buffered and later processed by
98  *      ctc_tty_readmodem().
99  */
100 static int
101 ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
102 {
103         int c;
104         int len;
105         struct tty_struct *tty;
106
107         DBF_TEXT(trace, 2, __FUNCTION__);
108         if ((tty = info->tty)) {
109                 if (info->mcr & UART_MCR_RTS) {
110                         c = TTY_FLIPBUF_SIZE - tty->flip.count;
111                         len = skb->len;
112                         if (c >= len) {
113                                 memcpy(tty->flip.char_buf_ptr, skb->data, len);
114                                 memset(tty->flip.flag_buf_ptr, 0, len);
115                                 tty->flip.count += len;
116                                 tty->flip.char_buf_ptr += len;
117                                 tty->flip.flag_buf_ptr += len;
118                                 tty_flip_buffer_push(tty);
119                                 kfree_skb(skb);
120                                 return 1;
121                         }
122                 }
123         }
124         return 0;
125 }
126
127 /* ctc_tty_readmodem() is called periodically from within timer-interrupt.
128  * It tries getting received data from the receive queue an stuff it into
129  * the tty's flip-buffer.
130  */
131 static int
132 ctc_tty_readmodem(ctc_tty_info *info)
133 {
134         int ret = 1;
135         struct tty_struct *tty;
136
137         DBF_TEXT(trace, 2, __FUNCTION__);
138         if ((tty = info->tty)) {
139                 if (info->mcr & UART_MCR_RTS) {
140                         int c = TTY_FLIPBUF_SIZE - tty->flip.count;
141                         struct sk_buff *skb;
142                         
143                         if ((c > 0) && (skb = skb_dequeue(&info->rx_queue))) {
144                                 int len = skb->len;
145                                 if (len > c)
146                                         len = c;
147                                 memcpy(tty->flip.char_buf_ptr, skb->data, len);
148                                 skb_pull(skb, len);
149                                 memset(tty->flip.flag_buf_ptr, 0, len);
150                                 tty->flip.count += len;
151                                 tty->flip.char_buf_ptr += len;
152                                 tty->flip.flag_buf_ptr += len;
153                                 tty_flip_buffer_push(tty);
154                                 if (skb->len > 0)
155                                         skb_queue_head(&info->rx_queue, skb);
156                                 else {
157                                         kfree_skb(skb);
158                                         ret = skb_queue_len(&info->rx_queue);
159                                 }
160                         }
161                 }
162         }
163         return ret;
164 }
165
166 void
167 ctc_tty_setcarrier(struct net_device *netdev, int on)
168 {
169         int i;
170
171         DBF_TEXT(trace, 2, __FUNCTION__);
172         if ((!driver) || ctc_tty_shuttingdown)
173                 return;
174         for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
175                 if (driver->info[i].netdev == netdev) {
176                         ctc_tty_info *info = &driver->info[i];
177                         if (on)
178                                 info->msr |= UART_MSR_DCD;
179                         else
180                                 info->msr &= ~UART_MSR_DCD;
181                         if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
182                                 tty_hangup(info->tty);
183                 }
184 }
185
186 void
187 ctc_tty_netif_rx(struct sk_buff *skb)
188 {
189         int i;
190         ctc_tty_info *info = NULL;
191
192         DBF_TEXT(trace, 2, __FUNCTION__);
193         if (!skb)
194                 return;
195         if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
196                 dev_kfree_skb(skb);
197                 return;
198         }
199         for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
200                 if (driver->info[i].netdev == skb->dev) {
201                         info = &driver->info[i];
202                         break;
203                 }
204         if (!info) {
205                 dev_kfree_skb(skb);
206                 return;
207         }
208         if (skb->len < 6) {
209                 dev_kfree_skb(skb);
210                 return;
211         }
212         if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
213                 dev_kfree_skb(skb);
214                 return;
215         }
216         skb_pull(skb, sizeof(__u32));
217
218         i = *((int *)skb->data);
219         skb_pull(skb, sizeof(info->mcr));
220         if (i & UART_MCR_RTS) {
221                 info->msr |= UART_MSR_CTS;
222                 if (info->flags & CTC_ASYNC_CTS_FLOW)
223                         info->tty->hw_stopped = 0;
224         } else {
225                 info->msr &= ~UART_MSR_CTS;
226                 if (info->flags & CTC_ASYNC_CTS_FLOW)
227                         info->tty->hw_stopped = 1;
228         }
229         if (i & UART_MCR_DTR)
230                 info->msr |= UART_MSR_DSR;
231         else
232                 info->msr &= ~UART_MSR_DSR;
233         if (skb->len <= 0) {
234                 kfree_skb(skb);
235                 return;
236         }
237         /* Try to deliver directly via tty-flip-buf if queue is empty */
238         if (skb_queue_empty(&info->rx_queue))
239                 if (ctc_tty_try_read(info, skb))
240                         return;
241         /* Direct deliver failed or queue wasn't empty.
242          * Queue up for later dequeueing via timer-irq.
243          */
244         skb_queue_tail(&info->rx_queue, skb);
245         /* Schedule dequeuing */
246         tasklet_schedule(&info->tasklet);
247 }
248
249 static int
250 ctc_tty_tint(ctc_tty_info * info)
251 {
252         struct sk_buff *skb = skb_dequeue(&info->tx_queue);
253         int stopped = (info->tty->hw_stopped || info->tty->stopped);
254         int wake = 1;
255         int rc;
256
257         DBF_TEXT(trace, 2, __FUNCTION__);
258         if (!info->netdev) {
259                 if (skb)
260                         kfree_skb(skb);
261                 return 0;
262         }
263         if (info->flags & CTC_ASYNC_TX_LINESTAT) {
264                 int skb_res = info->netdev->hard_header_len +
265                         sizeof(info->mcr) + sizeof(__u32);
266                 /* If we must update line status,
267                  * create an empty dummy skb and insert it.
268                  */
269                 if (skb)
270                         skb_queue_head(&info->tx_queue, skb);
271
272                 skb = dev_alloc_skb(skb_res);
273                 if (!skb) {
274                         printk(KERN_WARNING
275                                "ctc_tty: Out of memory in %s%d tint\n",
276                                CTC_TTY_NAME, info->line);
277                         return 1;
278                 }
279                 skb_reserve(skb, skb_res);
280                 stopped = 0;
281                 wake = 0;
282         }
283         if (!skb)
284                 return 0;
285         if (stopped) {
286                 skb_queue_head(&info->tx_queue, skb);
287                 return 1;
288         }
289 #if 0
290         if (skb->len > 0)
291                 printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
292         else
293                 printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
294 #endif
295         memcpy(skb_push(skb, sizeof(info->mcr)), &info->mcr, sizeof(info->mcr));
296         memcpy(skb_push(skb, sizeof(__u32)), &ctc_tty_magic, sizeof(__u32));
297         rc = info->netdev->hard_start_xmit(skb, info->netdev);
298         if (rc) {
299                 skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
300                 if (skb->len > 0)
301                         skb_queue_head(&info->tx_queue, skb);
302                 else
303                         kfree_skb(skb);
304         } else {
305                 struct tty_struct *tty = info->tty;
306
307                 info->flags &= ~CTC_ASYNC_TX_LINESTAT;
308                 if (tty) {
309                         if (wake && (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
310                             tty->ldisc.write_wakeup)
311                                 (tty->ldisc.write_wakeup)(tty);
312                         wake_up_interruptible(&tty->write_wait);
313                 }
314         }
315         return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
316 }
317
318 /************************************************************
319  *
320  * Modem-functions
321  *
322  * mostly "stolen" from original Linux-serial.c and friends.
323  *
324  ************************************************************/
325
326 static inline int
327 ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
328 {
329 #ifdef MODEM_PARANOIA_CHECK
330         if (!info) {
331                 printk(KERN_WARNING "ctc_tty: null info_struct for %s in %s\n",
332                        name, routine);
333                 return 1;
334         }
335         if (info->magic != CTC_ASYNC_MAGIC) {
336                 printk(KERN_WARNING "ctc_tty: bad magic for info struct %s in %s\n",
337                        name, routine);
338                 return 1;
339         }
340 #endif
341         return 0;
342 }
343
344 static void
345 ctc_tty_inject(ctc_tty_info *info, char c)
346 {
347         int skb_res;
348         struct sk_buff *skb;
349         
350         DBF_TEXT(trace, 2, __FUNCTION__);
351         if (ctc_tty_shuttingdown)
352                 return;
353         skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
354                 sizeof(__u32) + 1;
355         skb = dev_alloc_skb(skb_res);
356         if (!skb) {
357                 printk(KERN_WARNING
358                        "ctc_tty: Out of memory in %s%d tx_inject\n",
359                        CTC_TTY_NAME, info->line);
360                 return;
361         }
362         skb_reserve(skb, skb_res);
363         *(skb_put(skb, 1)) = c;
364         skb_queue_head(&info->tx_queue, skb);
365         tasklet_schedule(&info->tasklet);
366 }
367
368 static void
369 ctc_tty_transmit_status(ctc_tty_info *info)
370 {
371         DBF_TEXT(trace, 2, __FUNCTION__);
372         if (ctc_tty_shuttingdown)
373                 return;
374         info->flags |= CTC_ASYNC_TX_LINESTAT;
375         tasklet_schedule(&info->tasklet);
376 }
377
378 static void
379 ctc_tty_change_speed(ctc_tty_info * info)
380 {
381         unsigned int cflag;
382         unsigned int quot;
383         int i;
384
385         DBF_TEXT(trace, 2, __FUNCTION__);
386         if (!info->tty || !info->tty->termios)
387                 return;
388         cflag = info->tty->termios->c_cflag;
389
390         quot = i = cflag & CBAUD;
391         if (i & CBAUDEX) {
392                 i &= ~CBAUDEX;
393                 if (i < 1 || i > 2)
394                         info->tty->termios->c_cflag &= ~CBAUDEX;
395                 else
396                         i += 15;
397         }
398         if (quot) {
399                 info->mcr |= UART_MCR_DTR;
400                 info->mcr |= UART_MCR_RTS;
401                 ctc_tty_transmit_status(info);
402         } else {
403                 info->mcr &= ~UART_MCR_DTR;
404                 info->mcr &= ~UART_MCR_RTS;
405                 ctc_tty_transmit_status(info);
406                 return;
407         }
408
409         /* CTS flow control flag and modem status interrupts */
410         if (cflag & CRTSCTS) {
411                 info->flags |= CTC_ASYNC_CTS_FLOW;
412         } else
413                 info->flags &= ~CTC_ASYNC_CTS_FLOW;
414         if (cflag & CLOCAL)
415                 info->flags &= ~CTC_ASYNC_CHECK_CD;
416         else {
417                 info->flags |= CTC_ASYNC_CHECK_CD;
418         }
419 }
420
421 static int
422 ctc_tty_startup(ctc_tty_info * info)
423 {
424         DBF_TEXT(trace, 2, __FUNCTION__);
425         if (info->flags & CTC_ASYNC_INITIALIZED)
426                 return 0;
427 #ifdef CTC_DEBUG_MODEM_OPEN
428         printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
429 #endif
430         /*
431          * Now, initialize the UART
432          */
433         info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
434         if (info->tty)
435                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
436         /*
437          * and set the speed of the serial port
438          */
439         ctc_tty_change_speed(info);
440
441         info->flags |= CTC_ASYNC_INITIALIZED;
442         if (!(info->flags & CTC_ASYNC_NETDEV_OPEN))
443                 info->netdev->open(info->netdev);
444         info->flags |= CTC_ASYNC_NETDEV_OPEN;
445         return 0;
446 }
447
448 static void
449 ctc_tty_stopdev(unsigned long data)
450 {
451         ctc_tty_info *info = (ctc_tty_info *)data;
452
453         if ((!info) || (!info->netdev) ||
454             (info->flags & CTC_ASYNC_INITIALIZED))
455                 return;
456         info->netdev->stop(info->netdev);
457         info->flags &= ~CTC_ASYNC_NETDEV_OPEN;
458 }
459
460 /*
461  * This routine will shutdown a serial port; interrupts are disabled, and
462  * DTR is dropped if the hangup on close termio flag is on.
463  */
464 static void
465 ctc_tty_shutdown(ctc_tty_info * info)
466 {
467         DBF_TEXT(trace, 2, __FUNCTION__);
468         if (!(info->flags & CTC_ASYNC_INITIALIZED))
469                 return;
470 #ifdef CTC_DEBUG_MODEM_OPEN
471         printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
472 #endif
473         info->msr &= ~UART_MSR_RI;
474         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
475                 info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
476         if (info->tty)
477                 set_bit(TTY_IO_ERROR, &info->tty->flags);
478         mod_timer(&info->stoptimer, jiffies + (10 * HZ));
479         skb_queue_purge(&info->tx_queue);
480         skb_queue_purge(&info->rx_queue);
481         info->flags &= ~CTC_ASYNC_INITIALIZED;
482 }
483
484 /* ctc_tty_write() is the main send-routine. It is called from the upper
485  * levels within the kernel to perform sending data. Depending on the
486  * online-flag it either directs output to the at-command-interpreter or
487  * to the lower level. Additional tasks done here:
488  *  - If online, check for escape-sequence (+++)
489  *  - If sending audio-data, call ctc_tty_DLEdown() to parse DLE-codes.
490  *  - If receiving audio-data, call ctc_tty_end_vrx() to abort if needed.
491  *  - If dialing, abort dial.
492  */
493 static int
494 ctc_tty_write(struct tty_struct *tty, int from_user, const u_char * buf, int count)
495 {
496         int c;
497         int total = 0;
498         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
499
500         DBF_TEXT(trace, 2, __FUNCTION__);
501         if (ctc_tty_shuttingdown)
502                 goto ex;
503         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write"))
504                 goto ex;
505         if (!tty)
506                 goto ex;
507         if (!info->netdev) {
508                 total = -ENODEV;
509                 goto ex;
510         }
511         if (from_user)
512                 down(&info->write_sem);
513         while (1) {
514                 struct sk_buff *skb;
515                 int skb_res;
516
517                 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
518                 if (c <= 0)
519                         break;
520                 
521                 skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
522                         + sizeof(__u32);
523                 skb = dev_alloc_skb(skb_res + c);
524                 if (!skb) {
525                         printk(KERN_WARNING
526                                "ctc_tty: Out of memory in %s%d write\n",
527                                CTC_TTY_NAME, info->line);
528                         break;
529                 }
530                 skb_reserve(skb, skb_res);
531                 if (from_user)
532                         copy_from_user(skb_put(skb, c),
533                                         (const u_char __user *)buf, c);
534                 else
535                         memcpy(skb_put(skb, c), buf, c);
536                 skb_queue_tail(&info->tx_queue, skb);
537                 buf += c;
538                 total += c;
539                 count -= c;
540         }
541         if (skb_queue_len(&info->tx_queue)) {
542                 info->lsr &= ~UART_LSR_TEMT;
543                 tasklet_schedule(&info->tasklet);
544         }
545         if (from_user)
546                 up(&info->write_sem);
547 ex:
548         DBF_TEXT(trace, 6, __FUNCTION__);
549         return total;
550 }
551
552 static int
553 ctc_tty_write_room(struct tty_struct *tty)
554 {
555         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
556
557         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write_room"))
558                 return 0;
559         return CTC_TTY_XMIT_SIZE;
560 }
561
562 static int
563 ctc_tty_chars_in_buffer(struct tty_struct *tty)
564 {
565         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
566
567         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_chars_in_buffer"))
568                 return 0;
569         return 0;
570 }
571
572 static void
573 ctc_tty_flush_buffer(struct tty_struct *tty)
574 {
575         ctc_tty_info *info;
576         unsigned long flags;
577
578         DBF_TEXT(trace, 2, __FUNCTION__);
579         if (!tty)
580                 goto ex;
581         spin_lock_irqsave(&ctc_tty_lock, flags);
582         info = (ctc_tty_info *) tty->driver_data;
583         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_buffer")) {
584                 spin_unlock_irqrestore(&ctc_tty_lock, flags);
585                 goto ex;
586         }
587         skb_queue_purge(&info->tx_queue);
588         info->lsr |= UART_LSR_TEMT;
589         spin_unlock_irqrestore(&ctc_tty_lock, flags);
590         wake_up_interruptible(&tty->write_wait);
591         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
592             tty->ldisc.write_wakeup)
593                 (tty->ldisc.write_wakeup) (tty);
594 ex:
595         DBF_TEXT_(trace, 2, "ex: %s ", __FUNCTION__);
596         return;
597 }
598
599 static void
600 ctc_tty_flush_chars(struct tty_struct *tty)
601 {
602         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
603
604         if (ctc_tty_shuttingdown)
605                 return;
606         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
607                 return;
608         if (tty->stopped || tty->hw_stopped || (!skb_queue_len(&info->tx_queue)))
609                 return;
610         tasklet_schedule(&info->tasklet);
611 }
612
613 /*
614  * ------------------------------------------------------------
615  * ctc_tty_throttle()
616  *
617  * This routine is called by the upper-layer tty layer to signal that
618  * incoming characters should be throttled.
619  * ------------------------------------------------------------
620  */
621 static void
622 ctc_tty_throttle(struct tty_struct *tty)
623 {
624         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
625
626         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_throttle"))
627                 return;
628         info->mcr &= ~UART_MCR_RTS;
629         if (I_IXOFF(tty))
630                 ctc_tty_inject(info, STOP_CHAR(tty));
631         ctc_tty_transmit_status(info);
632 }
633
634 static void
635 ctc_tty_unthrottle(struct tty_struct *tty)
636 {
637         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
638
639         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_unthrottle"))
640                 return;
641         info->mcr |= UART_MCR_RTS;
642         if (I_IXOFF(tty))
643                 ctc_tty_inject(info, START_CHAR(tty));
644         ctc_tty_transmit_status(info);
645 }
646
647 /*
648  * ------------------------------------------------------------
649  * ctc_tty_ioctl() and friends
650  * ------------------------------------------------------------
651  */
652
653 /*
654  * ctc_tty_get_lsr_info - get line status register info
655  *
656  * Purpose: Let user call ioctl() to get info when the UART physically
657  *          is emptied.  On bus types like RS485, the transmitter must
658  *          release the bus after transmitting. This must be done when
659  *          the transmit shift register is empty, not be done when the
660  *          transmit holding register is empty.  This functionality
661  *          allows RS485 driver to be written in user space.
662  */
663 static int
664 ctc_tty_get_lsr_info(ctc_tty_info * info, uint __user *value)
665 {
666         u_char status;
667         uint result;
668         ulong flags;
669
670         spin_lock_irqsave(&ctc_tty_lock, flags);
671         status = info->lsr;
672         spin_unlock_irqrestore(&ctc_tty_lock, flags);
673         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
674         put_user(result, value);
675         return 0;
676 }
677
678
679 static int ctc_tty_tiocmget(struct tty_struct *tty, struct file *file)
680 {
681         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
682         u_char control,
683          status;
684         uint result;
685         ulong flags;
686
687         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
688                 return -ENODEV;
689         if (tty->flags & (1 << TTY_IO_ERROR))
690                 return -EIO;
691
692         control = info->mcr;
693         spin_lock_irqsave(&ctc_tty_lock, flags);
694         status = info->msr;
695         spin_unlock_irqrestore(&ctc_tty_lock, flags);
696         result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
697             | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
698             | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
699             | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
700             | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
701             | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
702         return result;
703 }
704
705 static int
706 ctc_tty_tiocmset(struct tty_struct *tty, struct file *file,
707                  unsigned int set, unsigned int clear)
708 {
709         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
710
711         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
712                 return -ENODEV;
713         if (tty->flags & (1 << TTY_IO_ERROR))
714                 return -EIO;
715
716         if (set & TIOCM_RTS)
717                 info->mcr |= UART_MCR_RTS;
718         if (set & TIOCM_DTR)
719                 info->mcr |= UART_MCR_DTR;
720
721         if (clear & TIOCM_RTS)
722                 info->mcr &= ~UART_MCR_RTS;
723         if (clear & TIOCM_DTR)
724                 info->mcr &= ~UART_MCR_DTR;
725
726         if ((set | clear) & (TIOCM_RTS|TIOCM_DTR))
727                 ctc_tty_transmit_status(info);
728         return 0;
729 }
730
731 static int
732 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
733                uint cmd, ulong arg)
734 {
735         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
736         int error;
737         int retval;
738
739         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
740                 return -ENODEV;
741         if (tty->flags & (1 << TTY_IO_ERROR))
742                 return -EIO;
743         switch (cmd) {
744                 case TCSBRK:   /* SVID version: non-zero arg --> no break */
745 #ifdef CTC_DEBUG_MODEM_IOCTL
746                         printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
747 #endif
748                         retval = tty_check_change(tty);
749                         if (retval)
750                                 return retval;
751                         tty_wait_until_sent(tty, 0);
752                         return 0;
753                 case TCSBRKP:  /* support for POSIX tcsendbreak() */
754 #ifdef CTC_DEBUG_MODEM_IOCTL
755                         printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
756 #endif
757                         retval = tty_check_change(tty);
758                         if (retval)
759                                 return retval;
760                         tty_wait_until_sent(tty, 0);
761                         return 0;
762                 case TIOCGSOFTCAR:
763 #ifdef CTC_DEBUG_MODEM_IOCTL
764                         printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
765                                info->line);
766 #endif
767                         error = put_user(C_CLOCAL(tty) ? 1 : 0, (ulong __user *) arg);
768                         return error;
769                 case TIOCSSOFTCAR:
770 #ifdef CTC_DEBUG_MODEM_IOCTL
771                         printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
772                                info->line);
773 #endif
774                         error = get_user(arg, (ulong __user *) arg);
775                         if (error)
776                                 return error;
777                         tty->termios->c_cflag =
778                             ((tty->termios->c_cflag & ~CLOCAL) |
779                              (arg ? CLOCAL : 0));
780                         return 0;
781                 case TIOCSERGETLSR:     /* Get line status register */
782 #ifdef CTC_DEBUG_MODEM_IOCTL
783                         printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
784                                info->line);
785 #endif
786                         error = verify_area(VERIFY_WRITE, (void __user *) arg, sizeof(uint));
787                         if (error)
788                                 return error;
789                         else
790                                 return ctc_tty_get_lsr_info(info, (uint __user *) arg);
791                 default:
792 #ifdef CTC_DEBUG_MODEM_IOCTL
793                         printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
794                                CTC_TTY_NAME, info->line);
795 #endif
796                         return -ENOIOCTLCMD;
797         }
798         return 0;
799 }
800
801 static void
802 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
803 {
804         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
805         unsigned int cflag = tty->termios->c_cflag;
806         ctc_tty_change_speed(info);
807
808         /* Handle transition to B0 */
809         if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
810                 info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
811                 ctc_tty_transmit_status(info);
812         }
813
814         /* Handle transition from B0 to other */
815         if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
816                 info->mcr |= UART_MCR_DTR;
817                 if (!(tty->termios->c_cflag & CRTSCTS) ||
818                     !test_bit(TTY_THROTTLED, &tty->flags)) {
819                         info->mcr |= UART_MCR_RTS;
820                 }
821                 ctc_tty_transmit_status(info);
822         }
823
824         /* Handle turning off CRTSCTS */
825         if ((old_termios->c_cflag & CRTSCTS) &&
826             !(tty->termios->c_cflag & CRTSCTS))
827                 tty->hw_stopped = 0;
828 }
829
830 /*
831  * ------------------------------------------------------------
832  * ctc_tty_open() and friends
833  * ------------------------------------------------------------
834  */
835 static int
836 ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
837 {
838         DECLARE_WAITQUEUE(wait, NULL);
839         int do_clocal = 0;
840         unsigned long flags;
841         int retval;
842
843         /*
844          * If the device is in the middle of being closed, then block
845          * until it's done, and then try again.
846          */
847         if (tty_hung_up_p(filp) ||
848             (info->flags & CTC_ASYNC_CLOSING)) {
849                 if (info->flags & CTC_ASYNC_CLOSING)
850                         wait_event(info->close_wait, 
851                                    !(info->flags & CTC_ASYNC_CLOSING));
852 #ifdef MODEM_DO_RESTART
853                 if (info->flags & CTC_ASYNC_HUP_NOTIFY)
854                         return -EAGAIN;
855                 else
856                         return -ERESTARTSYS;
857 #else
858                 return -EAGAIN;
859 #endif
860         }
861         /*
862          * If non-blocking mode is set, then make the check up front
863          * and then exit.
864          */
865         if ((filp->f_flags & O_NONBLOCK) ||
866             (tty->flags & (1 << TTY_IO_ERROR))) {
867                 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
868                 return 0;
869         }
870         if (tty->termios->c_cflag & CLOCAL)
871                 do_clocal = 1;
872         /*
873          * Block waiting for the carrier detect and the line to become
874          * free (i.e., not in use by the callout).  While we are in
875          * this loop, info->count is dropped by one, so that
876          * ctc_tty_close() knows when to free things.  We restore it upon
877          * exit, either normal or abnormal.
878          */
879         retval = 0;
880         add_wait_queue(&info->open_wait, &wait);
881 #ifdef CTC_DEBUG_MODEM_OPEN
882         printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
883                CTC_TTY_NAME, info->line, info->count);
884 #endif
885         spin_lock_irqsave(&ctc_tty_lock, flags);
886         if (!(tty_hung_up_p(filp)))
887                 info->count--;
888         spin_unlock_irqrestore(&ctc_tty_lock, flags);
889         info->blocked_open++;
890         while (1) {
891                 set_current_state(TASK_INTERRUPTIBLE);
892                 if (tty_hung_up_p(filp) ||
893                     !(info->flags & CTC_ASYNC_INITIALIZED)) {
894 #ifdef MODEM_DO_RESTART
895                         if (info->flags & CTC_ASYNC_HUP_NOTIFY)
896                                 retval = -EAGAIN;
897                         else
898                                 retval = -ERESTARTSYS;
899 #else
900                         retval = -EAGAIN;
901 #endif
902                         break;
903                 }
904                 if (!(info->flags & CTC_ASYNC_CLOSING) &&
905                     (do_clocal || (info->msr & UART_MSR_DCD))) {
906                         break;
907                 }
908                 if (signal_pending(current)) {
909                         retval = -ERESTARTSYS;
910                         break;
911                 }
912 #ifdef CTC_DEBUG_MODEM_OPEN
913                 printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
914                        CTC_TTY_NAME, info->line, info->count);
915 #endif
916                 schedule();
917         }
918         current->state = TASK_RUNNING;
919         remove_wait_queue(&info->open_wait, &wait);
920         if (!tty_hung_up_p(filp))
921                 info->count++;
922         info->blocked_open--;
923 #ifdef CTC_DEBUG_MODEM_OPEN
924         printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
925                CTC_TTY_NAME, info->line, info->count);
926 #endif
927         if (retval)
928                 return retval;
929         info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
930         return 0;
931 }
932
933 /*
934  * This routine is called whenever a serial port is opened.  It
935  * enables interrupts for a serial port, linking in its async structure into
936  * the IRQ chain.   It also performs the serial-specific
937  * initialization for the tty structure.
938  */
939 static int
940 ctc_tty_open(struct tty_struct *tty, struct file *filp)
941 {
942         ctc_tty_info *info;
943         unsigned long saveflags;
944         int retval,
945          line;
946
947         line = tty->index;
948         if (line < 0 || line > CTC_TTY_MAX_DEVICES)
949                 return -ENODEV;
950         info = &driver->info[line];
951         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
952                 return -ENODEV;
953         if (!info->netdev)
954                 return -ENODEV;
955 #ifdef CTC_DEBUG_MODEM_OPEN
956         printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
957                info->count);
958 #endif
959         spin_lock_irqsave(&ctc_tty_lock, saveflags);
960         info->count++;
961         tty->driver_data = info;
962         info->tty = tty;
963         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
964         /*
965          * Start up serial port
966          */
967         retval = ctc_tty_startup(info);
968         if (retval) {
969 #ifdef CTC_DEBUG_MODEM_OPEN
970                 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
971 #endif
972                 return retval;
973         }
974         retval = ctc_tty_block_til_ready(tty, filp, info);
975         if (retval) {
976 #ifdef CTC_DEBUG_MODEM_OPEN
977                 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
978 #endif
979                 return retval;
980         }
981 #ifdef CTC_DEBUG_MODEM_OPEN
982         printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
983 #endif
984         return 0;
985 }
986
987 static void
988 ctc_tty_close(struct tty_struct *tty, struct file *filp)
989 {
990         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
991         ulong flags;
992         ulong timeout;
993
994         if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
995                 return;
996         spin_lock_irqsave(&ctc_tty_lock, flags);
997         if (tty_hung_up_p(filp)) {
998                 spin_unlock_irqrestore(&ctc_tty_lock, flags);
999 #ifdef CTC_DEBUG_MODEM_OPEN
1000                 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
1001 #endif
1002                 return;
1003         }
1004         if ((tty->count == 1) && (info->count != 1)) {
1005                 /*
1006                  * Uh, oh.  tty->count is 1, which means that the tty
1007                  * structure will be freed.  Info->count should always
1008                  * be one in these conditions.  If it's greater than
1009                  * one, we've got real problems, since it means the
1010                  * serial port won't be shutdown.
1011                  */
1012                 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
1013                        "info->count is %d\n", info->count);
1014                 info->count = 1;
1015         }
1016         if (--info->count < 0) {
1017                 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
1018                        CTC_TTY_NAME, info->line, info->count);
1019                 info->count = 0;
1020         }
1021         if (info->count) {
1022                 local_irq_restore(flags);
1023 #ifdef CTC_DEBUG_MODEM_OPEN
1024                 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1025 #endif
1026                 return;
1027         }
1028         info->flags |= CTC_ASYNC_CLOSING;
1029         tty->closing = 1;
1030         /*
1031          * At this point we stop accepting input.  To do this, we
1032          * disable the receive line status interrupts, and tell the
1033          * interrupt driver to stop checking the data ready bit in the
1034          * line status register.
1035          */
1036         if (info->flags & CTC_ASYNC_INITIALIZED) {
1037                 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
1038                 /*
1039                  * Before we drop DTR, make sure the UART transmitter
1040                  * has completely drained; this is especially
1041                  * important if there is a transmit FIFO!
1042                  */
1043                 timeout = jiffies + HZ;
1044                 while (!(info->lsr & UART_LSR_TEMT)) {
1045                         set_current_state(TASK_INTERRUPTIBLE);
1046                         spin_unlock_irqrestore(&ctc_tty_lock, flags);
1047                         schedule_timeout(HZ/2);
1048                         spin_lock_irqsave(&ctc_tty_lock, flags);
1049                         if (time_after(jiffies,timeout))
1050                                 break;
1051                 }
1052         }
1053         ctc_tty_shutdown(info);
1054         if (tty->driver->flush_buffer) {
1055                 skb_queue_purge(&info->tx_queue);
1056                 info->lsr |= UART_LSR_TEMT;
1057         }
1058         if (tty->ldisc.flush_buffer)
1059                 tty->ldisc.flush_buffer(tty);
1060         info->tty = 0;
1061         tty->closing = 0;
1062         if (info->blocked_open) {
1063                 set_current_state(TASK_INTERRUPTIBLE);
1064                 schedule_timeout(HZ/2);
1065                 wake_up_interruptible(&info->open_wait);
1066         }
1067         info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
1068         wake_up_interruptible(&info->close_wait);
1069         spin_unlock_irqrestore(&ctc_tty_lock, flags);
1070 #ifdef CTC_DEBUG_MODEM_OPEN
1071         printk(KERN_DEBUG "ctc_tty_close normal exit\n");
1072 #endif
1073 }
1074
1075 /*
1076  * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1077  */
1078 static void
1079 ctc_tty_hangup(struct tty_struct *tty)
1080 {
1081         ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1082         unsigned long saveflags;
1083         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
1084                 return;
1085         ctc_tty_shutdown(info);
1086         info->count = 0;
1087         info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1088         spin_lock_irqsave(&ctc_tty_lock, saveflags);
1089         info->tty = 0;
1090         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1091         wake_up_interruptible(&info->open_wait);
1092 }
1093
1094
1095 /*
1096  * For all online tty's, try sending data to
1097  * the lower levels.
1098  */
1099 static void
1100 ctc_tty_task(unsigned long arg)
1101 {
1102         ctc_tty_info *info = (void *)arg;
1103         unsigned long saveflags;
1104         int again;
1105
1106         spin_lock_irqsave(&ctc_tty_lock, saveflags);
1107         if ((!ctc_tty_shuttingdown) && info) {
1108                 again = ctc_tty_tint(info);
1109                 if (!again)
1110                         info->lsr |= UART_LSR_TEMT;
1111                 again |= ctc_tty_readmodem(info);
1112                 if (again) {
1113                         tasklet_schedule(&info->tasklet);
1114                 }
1115         }
1116         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1117 }
1118
1119 static struct tty_operations ctc_ops = {
1120         .open = ctc_tty_open,
1121         .close = ctc_tty_close,
1122         .write = ctc_tty_write,
1123         .flush_chars = ctc_tty_flush_chars,
1124         .write_room = ctc_tty_write_room,
1125         .chars_in_buffer = ctc_tty_chars_in_buffer,
1126         .flush_buffer = ctc_tty_flush_buffer,
1127         .ioctl = ctc_tty_ioctl,
1128         .throttle = ctc_tty_throttle,
1129         .unthrottle = ctc_tty_unthrottle,
1130         .set_termios = ctc_tty_set_termios,
1131         .hangup = ctc_tty_hangup,
1132         .tiocmget = ctc_tty_tiocmget,
1133         .tiocmset = ctc_tty_tiocmset,
1134 };
1135
1136 int
1137 ctc_tty_init(void)
1138 {
1139         int i;
1140         ctc_tty_info *info;
1141         struct tty_driver *device;
1142
1143         driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1144         if (driver == NULL) {
1145                 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1146                 return -ENOMEM;
1147         }
1148         memset(driver, 0, sizeof(ctc_tty_driver));
1149         device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
1150         if (!device) {
1151                 kfree(driver);
1152                 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1153                 return -ENOMEM;
1154         }
1155
1156         device->devfs_name = "ctc/" CTC_TTY_NAME;
1157         device->name = CTC_TTY_NAME;
1158         device->major = CTC_TTY_MAJOR;
1159         device->minor_start = 0;
1160         device->type = TTY_DRIVER_TYPE_SERIAL;
1161         device->subtype = SERIAL_TYPE_NORMAL;
1162         device->init_termios = tty_std_termios;
1163         device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1164         device->flags = TTY_DRIVER_REAL_RAW;
1165         device->driver_name = "ctc_tty",
1166         tty_set_operations(device, &ctc_ops);
1167         if (tty_register_driver(device)) {
1168                 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1169                 put_tty_driver(device);
1170                 kfree(driver);
1171                 return -1;
1172         }
1173         driver->ctc_tty_device = device;
1174         for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1175                 info = &driver->info[i];
1176                 init_MUTEX(&info->write_sem);
1177                 tasklet_init(&info->tasklet, ctc_tty_task,
1178                                 (unsigned long) info);
1179                 info->magic = CTC_ASYNC_MAGIC;
1180                 info->line = i;
1181                 info->tty = 0;
1182                 info->count = 0;
1183                 info->blocked_open = 0;
1184                 init_waitqueue_head(&info->open_wait);
1185                 init_waitqueue_head(&info->close_wait);
1186                 skb_queue_head_init(&info->tx_queue);
1187                 skb_queue_head_init(&info->rx_queue);
1188                 init_timer(&info->stoptimer);
1189                 info->stoptimer.function = ctc_tty_stopdev;
1190                 info->stoptimer.data = (unsigned long)info;
1191                 info->mcr = UART_MCR_RTS;
1192         }
1193         return 0;
1194 }
1195
1196 int
1197 ctc_tty_register_netdev(struct net_device *dev) {
1198         int ttynum;
1199         char *err;
1200         char *p;
1201
1202         if ((!dev) || (!dev->name)) {
1203                 printk(KERN_WARNING
1204                        "ctc_tty_register_netdev called "
1205                        "with NULL dev or NULL dev-name\n");
1206                 return -1;
1207         }
1208
1209         /*
1210          *      If the name is a format string the caller wants us to
1211          *      do a name allocation : format string must end with %d
1212          */
1213         if (strchr(dev->name, '%'))
1214         {
1215                 int err = dev_alloc_name(dev, dev->name);       // dev->name is changed by this
1216                 if (err < 0) {
1217                         printk(KERN_DEBUG "dev_alloc returned error %d\n", err);
1218                         return err;
1219                 }
1220
1221         }
1222
1223         for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1224         ttynum = simple_strtoul(p, &err, 0);
1225         if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1226             (err && *err)) {
1227                 printk(KERN_WARNING
1228                        "ctc_tty_register_netdev called "
1229                        "with number in name '%s'\n", dev->name);
1230                 return -1;
1231         }
1232         if (driver->info[ttynum].netdev) {
1233                 printk(KERN_WARNING
1234                        "ctc_tty_register_netdev called "
1235                        "for already registered device '%s'\n",
1236                        dev->name);
1237                 return -1;
1238         }
1239         driver->info[ttynum].netdev = dev;
1240         return 0;
1241 }
1242
1243 void
1244 ctc_tty_unregister_netdev(struct net_device *dev) {
1245         int i;
1246         unsigned long saveflags;
1247         ctc_tty_info *info = NULL;
1248
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];
1253                         break;
1254                 }
1255         if (info) {
1256                 info->netdev = NULL;
1257                 skb_queue_purge(&info->tx_queue);
1258                 skb_queue_purge(&info->rx_queue);
1259         }
1260         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1261 }
1262
1263 void
1264 ctc_tty_cleanup(void) {
1265         unsigned long saveflags;
1266         
1267         spin_lock_irqsave(&ctc_tty_lock, saveflags);
1268         ctc_tty_shuttingdown = 1;
1269         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1270         tty_unregister_driver(driver->ctc_tty_device);
1271         put_tty_driver(driver->ctc_tty_device);
1272         kfree(driver);
1273         driver = NULL;
1274 }