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