patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / s390 / net / ctctty.c
1 /*
2  * $Id: ctctty.c,v 1.17 2004/03/31 17:06:34 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
34 #define CTC_TTY_MAJOR       43
35 #define CTC_TTY_MAX_DEVICES 64
36
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    */
49
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;
70
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;
76
77 static ctc_tty_driver *driver;
78
79 /* Leave this unchanged unless you know what you do! */
80 #define MODEM_PARANOIA_CHECK
81 #define MODEM_DO_RESTART
82
83 #define CTC_TTY_NAME "ctctty"
84
85 static __u32 ctc_tty_magic = CTC_ASYNC_MAGIC;
86 static int ctc_tty_shuttingdown = 0;
87
88 static spinlock_t ctc_tty_lock;
89
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.
93  *
94  * Return:
95  *  1 = Success
96  *  0 = Failure, data has to be buffered and later processed by
97  *      ctc_tty_readmodem().
98  */
99 static int
100 ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
101 {
102         int c;
103         int len;
104         struct tty_struct *tty;
105
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;
119                         }
120                 }
121         }
122         return 0;
123 }
124
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.
128  */
129 static int
130 ctc_tty_readmodem(ctc_tty_info *info)
131 {
132         int ret = 1;
133         struct tty_struct *tty;
134
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;
139                         
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);
156                                 }
157                         }
158                 }
159         }
160         return ret;
161 }
162
163 void
164 ctc_tty_setcarrier(struct net_device *netdev, int on)
165 {
166         int i;
167
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);
179                 }
180 }
181
182 void
183 ctc_tty_netif_rx(struct sk_buff *skb)
184 {
185         int i;
186         ctc_tty_info *info = NULL;
187
188         if (!skb)
189                 return;
190         if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
191                 dev_kfree_skb(skb);
192                 return;
193         }
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;
198                 }
199         if (!info) {
200                 dev_kfree_skb(skb);
201                 return;
202         }
203         if (skb->len < 6) {
204                 dev_kfree_skb(skb);
205                 return;
206         }
207         if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
208                 dev_kfree_skb(skb);
209                 return;
210         }
211         skb_pull(skb, sizeof(__u32));
212
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;
223         }
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;
231         }
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.
238          */
239         skb_queue_tail(&info->rx_queue, skb);
240         /* Schedule dequeuing */
241         tasklet_schedule(&info->tasklet);
242 }
243
244 static int
245 ctc_tty_tint(ctc_tty_info * info)
246 {
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;
251
252         if (!info->netdev) {
253                 if (skb)
254                         kfree_skb(skb);
255                 return 0;
256         }
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.
262                  */
263                 if (skb)
264                         skb_queue_head(&info->tx_queue, skb);
265
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;
272                 }
273                 skb_reserve(skb, skb_res);
274                 stopped = 0;
275                 wake = 0;
276         }
277         if (!skb)
278                 return 0;
279         if (stopped) {
280                 skb_queue_head(&info->tx_queue, skb);
281                 return 1;
282         }
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;
300
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);
307                 }
308         }
309         return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
310 }
311
312 /************************************************************
313  *
314  * Modem-functions
315  *
316  * mostly "stolen" from original Linux-serial.c and friends.
317  *
318  ************************************************************/
319
320 static inline int
321 ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
322 {
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;
328         }
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;
333         }
334 #endif
335         return 0;
336 }
337
338 static void
339 ctc_tty_inject(ctc_tty_info *info, char c)
340 {
341         int skb_res;
342         struct sk_buff *skb;
343         
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;
354         }
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);
359 }
360
361 static void
362 ctc_tty_transmit_status(ctc_tty_info *info)
363 {
364         if (ctc_tty_shuttingdown)
365                 return;
366         info->flags |= CTC_ASYNC_TX_LINESTAT;
367         tasklet_schedule(&info->tasklet);
368 }
369
370 static void
371 ctc_tty_change_speed(ctc_tty_info * info)
372 {
373         unsigned int cflag;
374         unsigned int quot;
375         int i;
376
377         if (!info->tty || !info->tty->termios)
378                 return;
379         cflag = info->tty->termios->c_cflag;
380
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;
388         }
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;
398         }
399
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;
409         }
410 }
411
412 static int
413 ctc_tty_startup(ctc_tty_info * info)
414 {
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
420         /*
421          * Now, initialize the UART
422          */
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);
426         /*
427          * and set the speed of the serial port
428          */
429         ctc_tty_change_speed(info);
430
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;
436 }
437
438 static void
439 ctc_tty_stopdev(unsigned long data)
440 {
441         ctc_tty_info *info = (ctc_tty_info *)data;
442
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;
448 }
449
450 /*
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.
453  */
454 static void
455 ctc_tty_shutdown(ctc_tty_info * info)
456 {
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;
471 }
472
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.
481  */
482 static int
483 ctc_tty_write(struct tty_struct *tty, int from_user, const u_char * buf, int count)
484 {
485         int c;
486         int total = 0;
487         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
488
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;
502
503                 c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
504                 if (c <= 0)
505                         break;
506                 
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;
515                 }
516                 skb_reserve(skb, skb_res);
517                 if (from_user)
518                         copy_from_user(skb_put(skb, c),
519                                         (const u_char __user *)buf, c);
520                 else
521                         memcpy(skb_put(skb, c), buf, c);
522                 skb_queue_tail(&info->tx_queue, skb);
523                 buf += c;
524                 total += c;
525                 count -= c;
526         }
527         if (skb_queue_len(&info->tx_queue)) {
528                 info->lsr &= ~UART_LSR_TEMT;
529                 tasklet_schedule(&info->tasklet);
530         }
531         if (from_user)
532                 up(&info->write_sem);
533         return total;
534 }
535
536 static int
537 ctc_tty_write_room(struct tty_struct *tty)
538 {
539         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
540
541         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write_room"))
542                 return 0;
543         return CTC_TTY_XMIT_SIZE;
544 }
545
546 static int
547 ctc_tty_chars_in_buffer(struct tty_struct *tty)
548 {
549         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
550
551         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_chars_in_buffer"))
552                 return 0;
553         return 0;
554 }
555
556 static void
557 ctc_tty_flush_buffer(struct tty_struct *tty)
558 {
559         ctc_tty_info *info;
560         unsigned long flags;
561
562         if (!tty)
563                 return;
564         spin_lock_irqsave(&ctc_tty_lock, flags);
565         info = (ctc_tty_info *) tty->driver_data;
566         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_buffer")) {
567                 spin_unlock_irqrestore(&ctc_tty_lock, flags);
568                 return;
569         }
570         skb_queue_purge(&info->tx_queue);
571         info->lsr |= UART_LSR_TEMT;
572         spin_unlock_irqrestore(&ctc_tty_lock, flags);
573         wake_up_interruptible(&tty->write_wait);
574         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
575             tty->ldisc.write_wakeup)
576                 (tty->ldisc.write_wakeup) (tty);
577 }
578
579 static void
580 ctc_tty_flush_chars(struct tty_struct *tty)
581 {
582         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
583
584         if (ctc_tty_shuttingdown)
585                 return;
586         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
587                 return;
588         if (tty->stopped || tty->hw_stopped || (!skb_queue_len(&info->tx_queue)))
589                 return;
590         tasklet_schedule(&info->tasklet);
591 }
592
593 /*
594  * ------------------------------------------------------------
595  * ctc_tty_throttle()
596  *
597  * This routine is called by the upper-layer tty layer to signal that
598  * incoming characters should be throttled.
599  * ------------------------------------------------------------
600  */
601 static void
602 ctc_tty_throttle(struct tty_struct *tty)
603 {
604         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
605
606         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_throttle"))
607                 return;
608         info->mcr &= ~UART_MCR_RTS;
609         if (I_IXOFF(tty))
610                 ctc_tty_inject(info, STOP_CHAR(tty));
611         ctc_tty_transmit_status(info);
612 }
613
614 static void
615 ctc_tty_unthrottle(struct tty_struct *tty)
616 {
617         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
618
619         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_unthrottle"))
620                 return;
621         info->mcr |= UART_MCR_RTS;
622         if (I_IXOFF(tty))
623                 ctc_tty_inject(info, START_CHAR(tty));
624         ctc_tty_transmit_status(info);
625 }
626
627 /*
628  * ------------------------------------------------------------
629  * ctc_tty_ioctl() and friends
630  * ------------------------------------------------------------
631  */
632
633 /*
634  * ctc_tty_get_lsr_info - get line status register info
635  *
636  * Purpose: Let user call ioctl() to get info when the UART physically
637  *          is emptied.  On bus types like RS485, the transmitter must
638  *          release the bus after transmitting. This must be done when
639  *          the transmit shift register is empty, not be done when the
640  *          transmit holding register is empty.  This functionality
641  *          allows RS485 driver to be written in user space.
642  */
643 static int
644 ctc_tty_get_lsr_info(ctc_tty_info * info, uint __user *value)
645 {
646         u_char status;
647         uint result;
648         ulong flags;
649
650         spin_lock_irqsave(&ctc_tty_lock, flags);
651         status = info->lsr;
652         spin_unlock_irqrestore(&ctc_tty_lock, flags);
653         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
654         put_user(result, value);
655         return 0;
656 }
657
658
659 static int ctc_tty_tiocmget(struct tty_struct *tty, struct file *file)
660 {
661         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
662         u_char control,
663          status;
664         uint result;
665         ulong flags;
666
667         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
668                 return -ENODEV;
669         if (tty->flags & (1 << TTY_IO_ERROR))
670                 return -EIO;
671
672         control = info->mcr;
673         spin_lock_irqsave(&ctc_tty_lock, flags);
674         status = info->msr;
675         spin_unlock_irqrestore(&ctc_tty_lock, flags);
676         result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
677             | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
678             | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
679             | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
680             | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
681             | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
682         return result;
683 }
684
685 static int
686 ctc_tty_tiocmset(struct tty_struct *tty, struct file *file,
687                  unsigned int set, unsigned int clear)
688 {
689         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
690
691         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
692                 return -ENODEV;
693         if (tty->flags & (1 << TTY_IO_ERROR))
694                 return -EIO;
695
696         if (set & TIOCM_RTS)
697                 info->mcr |= UART_MCR_RTS;
698         if (set & TIOCM_DTR)
699                 info->mcr |= UART_MCR_DTR;
700
701         if (clear & TIOCM_RTS)
702                 info->mcr &= ~UART_MCR_RTS;
703         if (clear & TIOCM_DTR)
704                 info->mcr &= ~UART_MCR_DTR;
705
706         if ((set | clear) & (TIOCM_RTS|TIOCM_DTR))
707                 ctc_tty_transmit_status(info);
708         return 0;
709 }
710
711 static int
712 ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
713                uint cmd, ulong arg)
714 {
715         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
716         int error;
717         int retval;
718
719         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
720                 return -ENODEV;
721         if (tty->flags & (1 << TTY_IO_ERROR))
722                 return -EIO;
723         switch (cmd) {
724                 case TCSBRK:   /* SVID version: non-zero arg --> no break */
725 #ifdef CTC_DEBUG_MODEM_IOCTL
726                         printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
727 #endif
728                         retval = tty_check_change(tty);
729                         if (retval)
730                                 return retval;
731                         tty_wait_until_sent(tty, 0);
732                         return 0;
733                 case TCSBRKP:  /* support for POSIX tcsendbreak() */
734 #ifdef CTC_DEBUG_MODEM_IOCTL
735                         printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
736 #endif
737                         retval = tty_check_change(tty);
738                         if (retval)
739                                 return retval;
740                         tty_wait_until_sent(tty, 0);
741                         return 0;
742                 case TIOCGSOFTCAR:
743 #ifdef CTC_DEBUG_MODEM_IOCTL
744                         printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
745                                info->line);
746 #endif
747                         error = put_user(C_CLOCAL(tty) ? 1 : 0, (ulong __user *) arg);
748                         return error;
749                 case TIOCSSOFTCAR:
750 #ifdef CTC_DEBUG_MODEM_IOCTL
751                         printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
752                                info->line);
753 #endif
754                         error = get_user(arg, (ulong __user *) arg);
755                         if (error)
756                                 return error;
757                         tty->termios->c_cflag =
758                             ((tty->termios->c_cflag & ~CLOCAL) |
759                              (arg ? CLOCAL : 0));
760                         return 0;
761                 case TIOCSERGETLSR:     /* Get line status register */
762 #ifdef CTC_DEBUG_MODEM_IOCTL
763                         printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
764                                info->line);
765 #endif
766                         error = verify_area(VERIFY_WRITE, (void __user *) arg, sizeof(uint));
767                         if (error)
768                                 return error;
769                         else
770                                 return ctc_tty_get_lsr_info(info, (uint __user *) arg);
771                 default:
772 #ifdef CTC_DEBUG_MODEM_IOCTL
773                         printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
774                                CTC_TTY_NAME, info->line);
775 #endif
776                         return -ENOIOCTLCMD;
777         }
778         return 0;
779 }
780
781 static void
782 ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
783 {
784         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
785         unsigned int cflag = tty->termios->c_cflag;
786
787         ctc_tty_change_speed(info);
788
789         /* Handle transition to B0 */
790         if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
791                 info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
792                 ctc_tty_transmit_status(info);
793         }
794
795         /* Handle transition from B0 to other */
796         if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
797                 info->mcr |= UART_MCR_DTR;
798                 if (!(tty->termios->c_cflag & CRTSCTS) ||
799                     !test_bit(TTY_THROTTLED, &tty->flags)) {
800                         info->mcr |= UART_MCR_RTS;
801                 }
802                 ctc_tty_transmit_status(info);
803         }
804
805         /* Handle turning off CRTSCTS */
806         if ((old_termios->c_cflag & CRTSCTS) &&
807             !(tty->termios->c_cflag & CRTSCTS))
808                 tty->hw_stopped = 0;
809 }
810
811 /*
812  * ------------------------------------------------------------
813  * ctc_tty_open() and friends
814  * ------------------------------------------------------------
815  */
816 static int
817 ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
818 {
819         DECLARE_WAITQUEUE(wait, NULL);
820         int do_clocal = 0;
821         unsigned long flags;
822         int retval;
823
824         /*
825          * If the device is in the middle of being closed, then block
826          * until it's done, and then try again.
827          */
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)
835                         return -EAGAIN;
836                 else
837                         return -ERESTARTSYS;
838 #else
839                 return -EAGAIN;
840 #endif
841         }
842         /*
843          * If non-blocking mode is set, then make the check up front
844          * and then exit.
845          */
846         if ((filp->f_flags & O_NONBLOCK) ||
847             (tty->flags & (1 << TTY_IO_ERROR))) {
848                 info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
849                 return 0;
850         }
851         if (tty->termios->c_cflag & CLOCAL)
852                 do_clocal = 1;
853         /*
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.
859          */
860         retval = 0;
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);
865 #endif
866         spin_lock_irqsave(&ctc_tty_lock, flags);
867         if (!(tty_hung_up_p(filp)))
868                 info->count--;
869         spin_unlock_irqrestore(&ctc_tty_lock, flags);
870         info->blocked_open++;
871         while (1) {
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)
877                                 retval = -EAGAIN;
878                         else
879                                 retval = -ERESTARTSYS;
880 #else
881                         retval = -EAGAIN;
882 #endif
883                         break;
884                 }
885                 if (!(info->flags & CTC_ASYNC_CLOSING) &&
886                     (do_clocal || (info->msr & UART_MSR_DCD))) {
887                         break;
888                 }
889                 if (signal_pending(current)) {
890                         retval = -ERESTARTSYS;
891                         break;
892                 }
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);
896 #endif
897                 schedule();
898         }
899         current->state = TASK_RUNNING;
900         remove_wait_queue(&info->open_wait, &wait);
901         if (!tty_hung_up_p(filp))
902                 info->count++;
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);
907 #endif
908         if (retval)
909                 return retval;
910         info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
911         return 0;
912 }
913
914 /*
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.
919  */
920 static int
921 ctc_tty_open(struct tty_struct *tty, struct file *filp)
922 {
923         ctc_tty_info *info;
924         unsigned long saveflags;
925         int retval,
926          line;
927
928         line = tty->index;
929         if (line < 0 || line > CTC_TTY_MAX_DEVICES)
930                 return -ENODEV;
931         info = &driver->info[line];
932         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
933                 return -ENODEV;
934         if (!info->netdev)
935                 return -ENODEV;
936 #ifdef CTC_DEBUG_MODEM_OPEN
937         printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
938                info->count);
939 #endif
940         spin_lock_irqsave(&ctc_tty_lock, saveflags);
941         info->count++;
942         tty->driver_data = info;
943         info->tty = tty;
944         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
945         /*
946          * Start up serial port
947          */
948         retval = ctc_tty_startup(info);
949         if (retval) {
950 #ifdef CTC_DEBUG_MODEM_OPEN
951                 printk(KERN_DEBUG "ctc_tty_open return after startup\n");
952 #endif
953                 return retval;
954         }
955         retval = ctc_tty_block_til_ready(tty, filp, info);
956         if (retval) {
957 #ifdef CTC_DEBUG_MODEM_OPEN
958                 printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
959 #endif
960                 return retval;
961         }
962 #ifdef CTC_DEBUG_MODEM_OPEN
963         printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
964 #endif
965         return 0;
966 }
967
968 static void
969 ctc_tty_close(struct tty_struct *tty, struct file *filp)
970 {
971         ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
972         ulong flags;
973         ulong timeout;
974
975         if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
976                 return;
977         spin_lock_irqsave(&ctc_tty_lock, flags);
978         if (tty_hung_up_p(filp)) {
979                 spin_unlock_irqrestore(&ctc_tty_lock, flags);
980 #ifdef CTC_DEBUG_MODEM_OPEN
981                 printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
982 #endif
983                 return;
984         }
985         if ((tty->count == 1) && (info->count != 1)) {
986                 /*
987                  * Uh, oh.  tty->count is 1, which means that the tty
988                  * structure will be freed.  Info->count should always
989                  * be one in these conditions.  If it's greater than
990                  * one, we've got real problems, since it means the
991                  * serial port won't be shutdown.
992                  */
993                 printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
994                        "info->count is %d\n", info->count);
995                 info->count = 1;
996         }
997         if (--info->count < 0) {
998                 printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
999                        CTC_TTY_NAME, info->line, info->count);
1000                 info->count = 0;
1001         }
1002         if (info->count) {
1003                 local_irq_restore(flags);
1004 #ifdef CTC_DEBUG_MODEM_OPEN
1005                 printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
1006 #endif
1007                 return;
1008         }
1009         info->flags |= CTC_ASYNC_CLOSING;
1010         tty->closing = 1;
1011         /*
1012          * At this point we stop accepting input.  To do this, we
1013          * disable the receive line status interrupts, and tell the
1014          * interrupt driver to stop checking the data ready bit in the
1015          * line status register.
1016          */
1017         if (info->flags & CTC_ASYNC_INITIALIZED) {
1018                 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
1019                 /*
1020                  * Before we drop DTR, make sure the UART transmitter
1021                  * has completely drained; this is especially
1022                  * important if there is a transmit FIFO!
1023                  */
1024                 timeout = jiffies + HZ;
1025                 while (!(info->lsr & UART_LSR_TEMT)) {
1026                         set_current_state(TASK_INTERRUPTIBLE);
1027                         spin_unlock_irqrestore(&ctc_tty_lock, flags);
1028                         schedule_timeout(HZ/2);
1029                         spin_lock_irqsave(&ctc_tty_lock, flags);
1030                         if (time_after(jiffies,timeout))
1031                                 break;
1032                 }
1033         }
1034         ctc_tty_shutdown(info);
1035         if (tty->driver->flush_buffer)
1036                 tty->driver->flush_buffer(tty);
1037         if (tty->ldisc.flush_buffer)
1038                 tty->ldisc.flush_buffer(tty);
1039         info->tty = 0;
1040         tty->closing = 0;
1041         if (info->blocked_open) {
1042                 set_current_state(TASK_INTERRUPTIBLE);
1043                 schedule_timeout(HZ/2);
1044                 wake_up_interruptible(&info->open_wait);
1045         }
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");
1051 #endif
1052 }
1053
1054 /*
1055  * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
1056  */
1057 static void
1058 ctc_tty_hangup(struct tty_struct *tty)
1059 {
1060         ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
1061         unsigned long saveflags;
1062
1063         if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
1064                 return;
1065         ctc_tty_shutdown(info);
1066         info->count = 0;
1067         info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
1068         spin_lock_irqsave(&ctc_tty_lock, saveflags);
1069         info->tty = 0;
1070         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1071         wake_up_interruptible(&info->open_wait);
1072 }
1073
1074
1075 /*
1076  * For all online tty's, try sending data to
1077  * the lower levels.
1078  */
1079 static void
1080 ctc_tty_task(unsigned long arg)
1081 {
1082         ctc_tty_info *info = (void *)arg;
1083         unsigned long saveflags;
1084         int again;
1085
1086         spin_lock_irqsave(&ctc_tty_lock, saveflags);
1087         if ((!ctc_tty_shuttingdown) && info) {
1088                 again = ctc_tty_tint(info);
1089                 if (!again)
1090                         info->lsr |= UART_LSR_TEMT;
1091                 again |= ctc_tty_readmodem(info);
1092                 if (again) {
1093                         tasklet_schedule(&info->tasklet);
1094                 }
1095         }
1096         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1097 }
1098
1099 static struct tty_operations ctc_ops = {
1100         .open = ctc_tty_open,
1101         .close = ctc_tty_close,
1102         .write = ctc_tty_write,
1103         .flush_chars = ctc_tty_flush_chars,
1104         .write_room = ctc_tty_write_room,
1105         .chars_in_buffer = ctc_tty_chars_in_buffer,
1106         .flush_buffer = ctc_tty_flush_buffer,
1107         .ioctl = ctc_tty_ioctl,
1108         .throttle = ctc_tty_throttle,
1109         .unthrottle = ctc_tty_unthrottle,
1110         .set_termios = ctc_tty_set_termios,
1111         .hangup = ctc_tty_hangup,
1112         .tiocmget = ctc_tty_tiocmget,
1113         .tiocmset = ctc_tty_tiocmset,
1114 };
1115
1116 int
1117 ctc_tty_init(void)
1118 {
1119         int i;
1120         ctc_tty_info *info;
1121         struct tty_driver *device;
1122
1123         driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
1124         if (driver == NULL) {
1125                 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1126                 return -ENOMEM;
1127         }
1128         memset(driver, 0, sizeof(ctc_tty_driver));
1129         device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
1130         if (!device) {
1131                 kfree(driver);
1132                 printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
1133                 return -ENOMEM;
1134         }
1135
1136         device->devfs_name = "ctc/" CTC_TTY_NAME;
1137         device->name = CTC_TTY_NAME;
1138         device->major = CTC_TTY_MAJOR;
1139         device->minor_start = 0;
1140         device->type = TTY_DRIVER_TYPE_SERIAL;
1141         device->subtype = SERIAL_TYPE_NORMAL;
1142         device->init_termios = tty_std_termios;
1143         device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1144         device->flags = TTY_DRIVER_REAL_RAW;
1145         device->driver_name = "ctc_tty",
1146         tty_set_operations(device, &ctc_ops);
1147         if (tty_register_driver(device)) {
1148                 printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
1149                 put_tty_driver(device);
1150                 kfree(driver);
1151                 return -1;
1152         }
1153         driver->ctc_tty_device = device;
1154         for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
1155                 info = &driver->info[i];
1156                 init_MUTEX(&info->write_sem);
1157                 tasklet_init(&info->tasklet, ctc_tty_task,
1158                                 (unsigned long) info);
1159                 info->magic = CTC_ASYNC_MAGIC;
1160                 info->line = i;
1161                 info->tty = 0;
1162                 info->count = 0;
1163                 info->blocked_open = 0;
1164                 init_waitqueue_head(&info->open_wait);
1165                 init_waitqueue_head(&info->close_wait);
1166                 skb_queue_head_init(&info->tx_queue);
1167                 skb_queue_head_init(&info->rx_queue);
1168                 init_timer(&info->stoptimer);
1169                 info->stoptimer.function = ctc_tty_stopdev;
1170                 info->stoptimer.data = (unsigned long)info;
1171                 info->mcr = UART_MCR_RTS;
1172         }
1173         return 0;
1174 }
1175
1176 int
1177 ctc_tty_register_netdev(struct net_device *dev) {
1178         int ttynum;
1179         char *err;
1180         char *p;
1181
1182         if ((!dev) || (!dev->name)) {
1183                 printk(KERN_WARNING
1184                        "ctc_tty_register_netdev called "
1185                        "with NULL dev or NULL dev-name\n");
1186                 return -1;
1187         }
1188         for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
1189         ttynum = simple_strtoul(p, &err, 0);
1190         if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
1191             (err && *err)) {
1192                 printk(KERN_WARNING
1193                        "ctc_tty_register_netdev called "
1194                        "with number in name '%s'\n", dev->name);
1195                 return -1;
1196         }
1197         if (driver->info[ttynum].netdev) {
1198                 printk(KERN_WARNING
1199                        "ctc_tty_register_netdev called "
1200                        "for already registered device '%s'\n",
1201                        dev->name);
1202                 return -1;
1203         }
1204         driver->info[ttynum].netdev = dev;
1205         return 0;
1206 }
1207
1208 void
1209 ctc_tty_unregister_netdev(struct net_device *dev) {
1210         int i;
1211         unsigned long saveflags;
1212         ctc_tty_info *info = NULL;
1213
1214         spin_lock_irqsave(&ctc_tty_lock, saveflags);
1215         for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
1216                 if (driver->info[i].netdev == dev) {
1217                         info = &driver->info[i];
1218                         break;
1219                 }
1220         if (info) {
1221                 info->netdev = NULL;
1222                 skb_queue_purge(&info->tx_queue);
1223                 skb_queue_purge(&info->rx_queue);
1224         }
1225         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1226 }
1227
1228 void
1229 ctc_tty_cleanup(void) {
1230         unsigned long saveflags;
1231         
1232         spin_lock_irqsave(&ctc_tty_lock, saveflags);
1233         ctc_tty_shuttingdown = 1;
1234         spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
1235         tty_unregister_driver(driver->ctc_tty_device);
1236         put_tty_driver(driver->ctc_tty_device);
1237         kfree(driver);
1238         driver = NULL;
1239 }