1/*
2 * Copyright 2003 Digi International (www.digi.com)
3 *	Scott H Kilau <Scott_Kilau at digi dot com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE.  See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 *
20 *	NOTE TO LINUX KERNEL HACKERS:  DO NOT REFORMAT THIS CODE!
21 *
22 *	This is shared code between Digi's CVS archive and the
23 *	Linux Kernel sources.
24 *	Changing the source just for reformatting needlessly breaks
25 *	our CVS diff history.
26 *
27 *	Send any bug fixes/changes to:  Eng.Linux at digi dot com.
28 *	Thank you.
29 */
30
31/************************************************************************
32 *
33 * This file implements the tty driver functionality for the
34 * Neo and ClassicBoard PCI based product lines.
35 *
36 ************************************************************************
37 *
38 */
39
40#include <linux/kernel.h>
41#include <linux/sched.h>	/* For jiffies, task states */
42#include <linux/interrupt.h>	/* For tasklet and interrupt structs/defines */
43#include <linux/module.h>
44#include <linux/ctype.h>
45#include <linux/tty.h>
46#include <linux/tty_flip.h>
47#include <linux/serial_reg.h>
48#include <linux/slab.h>
49#include <linux/delay.h>	/* For udelay */
50#include <linux/uaccess.h>	/* For copy_from_user/copy_to_user */
51#include <linux/pci.h>
52
53#include "dgnc_driver.h"
54#include "dgnc_tty.h"
55#include "dgnc_types.h"
56#include "dgnc_neo.h"
57#include "dgnc_cls.h"
58#include "dpacompat.h"
59#include "dgnc_sysfs.h"
60#include "dgnc_utils.h"
61
62#define init_MUTEX(sem)	 sema_init(sem, 1)
63#define DECLARE_MUTEX(name)     \
64	struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
65
66/*
67 * internal variables
68 */
69static struct dgnc_board	*dgnc_BoardsByMajor[256];
70static unsigned char		*dgnc_TmpWriteBuf;
71static DECLARE_MUTEX(dgnc_TmpWriteSem);
72
73/*
74 * Default transparent print information.
75 */
76static struct digi_t dgnc_digi_init = {
77	.digi_flags =	DIGI_COOK,	/* Flags			*/
78	.digi_maxcps =	100,		/* Max CPS			*/
79	.digi_maxchar =	50,		/* Max chars in print queue	*/
80	.digi_bufsize =	100,		/* Printer buffer size		*/
81	.digi_onlen =	4,		/* size of printer on string	*/
82	.digi_offlen =	4,		/* size of printer off string	*/
83	.digi_onstr =	"\033[5i",	/* ANSI printer on string ]	*/
84	.digi_offstr =	"\033[4i",	/* ANSI printer off string ]	*/
85	.digi_term =	"ansi"		/* default terminal type	*/
86};
87
88
89/*
90 * Define a local default termios struct. All ports will be created
91 * with this termios initially.
92 *
93 * This defines a raw port at 9600 baud, 8 data bits, no parity,
94 * 1 stop bit.
95 */
96static struct ktermios DgncDefaultTermios = {
97	.c_iflag =	(DEFAULT_IFLAGS),	/* iflags */
98	.c_oflag =	(DEFAULT_OFLAGS),	/* oflags */
99	.c_cflag =	(DEFAULT_CFLAGS),	/* cflags */
100	.c_lflag =	(DEFAULT_LFLAGS),	/* lflags */
101	.c_cc =		INIT_C_CC,
102	.c_line =	0,
103};
104
105
106/* Our function prototypes */
107static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
108static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
109static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
110static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
111static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
112static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
113static int dgnc_tty_write_room(struct tty_struct *tty);
114static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
115static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
116static void dgnc_tty_start(struct tty_struct *tty);
117static void dgnc_tty_stop(struct tty_struct *tty);
118static void dgnc_tty_throttle(struct tty_struct *tty);
119static void dgnc_tty_unthrottle(struct tty_struct *tty);
120static void dgnc_tty_flush_chars(struct tty_struct *tty);
121static void dgnc_tty_flush_buffer(struct tty_struct *tty);
122static void dgnc_tty_hangup(struct tty_struct *tty);
123static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
124static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value);
125static int dgnc_tty_tiocmget(struct tty_struct *tty);
126static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
127static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
128static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
129static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
130static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
131static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
132
133
134static const struct tty_operations dgnc_tty_ops = {
135	.open = dgnc_tty_open,
136	.close = dgnc_tty_close,
137	.write = dgnc_tty_write,
138	.write_room = dgnc_tty_write_room,
139	.flush_buffer = dgnc_tty_flush_buffer,
140	.chars_in_buffer = dgnc_tty_chars_in_buffer,
141	.flush_chars = dgnc_tty_flush_chars,
142	.ioctl = dgnc_tty_ioctl,
143	.set_termios = dgnc_tty_set_termios,
144	.stop = dgnc_tty_stop,
145	.start = dgnc_tty_start,
146	.throttle = dgnc_tty_throttle,
147	.unthrottle = dgnc_tty_unthrottle,
148	.hangup = dgnc_tty_hangup,
149	.put_char = dgnc_tty_put_char,
150	.tiocmget = dgnc_tty_tiocmget,
151	.tiocmset = dgnc_tty_tiocmset,
152	.break_ctl = dgnc_tty_send_break,
153	.wait_until_sent = dgnc_tty_wait_until_sent,
154	.send_xchar = dgnc_tty_send_xchar
155};
156
157/************************************************************************
158 *
159 * TTY Initialization/Cleanup Functions
160 *
161 ************************************************************************/
162
163/*
164 * dgnc_tty_preinit()
165 *
166 * Initialize any global tty related data before we download any boards.
167 */
168int dgnc_tty_preinit(void)
169{
170	/*
171	 * Allocate a buffer for doing the copy from user space to
172	 * kernel space in dgnc_write().  We only use one buffer and
173	 * control access to it with a semaphore.  If we are paging, we
174	 * are already in trouble so one buffer won't hurt much anyway.
175	 *
176	 * We are okay to sleep in the malloc, as this routine
177	 * is only called during module load, (not in interrupt context),
178	 * and with no locks held.
179	 */
180	dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
181
182	if (!dgnc_TmpWriteBuf)
183		return -ENOMEM;
184
185	return 0;
186}
187
188
189/*
190 * dgnc_tty_register()
191 *
192 * Init the tty subsystem for this board.
193 */
194int dgnc_tty_register(struct dgnc_board *brd)
195{
196	int rc = 0;
197
198	brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
199
200	snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
201
202	brd->SerialDriver.name = brd->SerialName;
203	brd->SerialDriver.name_base = 0;
204	brd->SerialDriver.major = 0;
205	brd->SerialDriver.minor_start = 0;
206	brd->SerialDriver.num = brd->maxports;
207	brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
208	brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
209	brd->SerialDriver.init_termios = DgncDefaultTermios;
210	brd->SerialDriver.driver_name = DRVSTR;
211	brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
212
213	/*
214	 * The kernel wants space to store pointers to
215	 * tty_struct's and termios's.
216	 */
217	brd->SerialDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.ttys), GFP_KERNEL);
218	if (!brd->SerialDriver.ttys)
219		return -ENOMEM;
220
221	kref_init(&brd->SerialDriver.kref);
222	brd->SerialDriver.termios = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.termios), GFP_KERNEL);
223	if (!brd->SerialDriver.termios)
224		return -ENOMEM;
225
226	/*
227	 * Entry points for driver.  Called by the kernel from
228	 * tty_io.c and n_tty.c.
229	 */
230	tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
231
232	if (!brd->dgnc_Major_Serial_Registered) {
233		/* Register tty devices */
234		rc = tty_register_driver(&brd->SerialDriver);
235		if (rc < 0) {
236			APR(("Can't register tty device (%d)\n", rc));
237			return rc;
238		}
239		brd->dgnc_Major_Serial_Registered = TRUE;
240	}
241
242	/*
243	 * If we're doing transparent print, we have to do all of the above
244	 * again, separately so we don't get the LD confused about what major
245	 * we are when we get into the dgnc_tty_open() routine.
246	 */
247	brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
248	snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
249
250	brd->PrintDriver.name = brd->PrintName;
251	brd->PrintDriver.name_base = 0;
252	brd->PrintDriver.major = brd->SerialDriver.major;
253	brd->PrintDriver.minor_start = 0x80;
254	brd->PrintDriver.num = brd->maxports;
255	brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
256	brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
257	brd->PrintDriver.init_termios = DgncDefaultTermios;
258	brd->PrintDriver.driver_name = DRVSTR;
259	brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
260
261	/*
262	 * The kernel wants space to store pointers to
263	 * tty_struct's and termios's.  Must be separated from
264	 * the Serial Driver so we don't get confused
265	 */
266	brd->PrintDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.ttys), GFP_KERNEL);
267	if (!brd->PrintDriver.ttys)
268		return -ENOMEM;
269	kref_init(&brd->PrintDriver.kref);
270	brd->PrintDriver.termios = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.termios), GFP_KERNEL);
271	if (!brd->PrintDriver.termios)
272		return -ENOMEM;
273
274	/*
275	 * Entry points for driver.  Called by the kernel from
276	 * tty_io.c and n_tty.c.
277	 */
278	tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
279
280	if (!brd->dgnc_Major_TransparentPrint_Registered) {
281		/* Register Transparent Print devices */
282		rc = tty_register_driver(&brd->PrintDriver);
283		if (rc < 0) {
284			APR(("Can't register Transparent Print device (%d)\n", rc));
285			return rc;
286		}
287		brd->dgnc_Major_TransparentPrint_Registered = TRUE;
288	}
289
290	dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
291	brd->dgnc_Serial_Major = brd->SerialDriver.major;
292	brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
293
294	return rc;
295}
296
297
298/*
299 * dgnc_tty_init()
300 *
301 * Init the tty subsystem.  Called once per board after board has been
302 * downloaded and init'ed.
303 */
304int dgnc_tty_init(struct dgnc_board *brd)
305{
306	int i;
307	void __iomem *vaddr;
308	struct channel_t *ch;
309
310	if (!brd)
311		return -ENXIO;
312
313	/*
314	 * Initialize board structure elements.
315	 */
316
317	vaddr = brd->re_map_membase;
318
319	brd->nasync = brd->maxports;
320
321	/*
322	 * Allocate channel memory that might not have been allocated
323	 * when the driver was first loaded.
324	 */
325	for (i = 0; i < brd->nasync; i++) {
326		if (!brd->channels[i]) {
327
328			/*
329			 * Okay to malloc with GFP_KERNEL, we are not at
330			 * interrupt context, and there are no locks held.
331			 */
332			brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
333		}
334	}
335
336	ch = brd->channels[0];
337	vaddr = brd->re_map_membase;
338
339	/* Set up channel variables */
340	for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
341
342		if (!brd->channels[i])
343			continue;
344
345		spin_lock_init(&ch->ch_lock);
346
347		/* Store all our magic numbers */
348		ch->magic = DGNC_CHANNEL_MAGIC;
349		ch->ch_tun.magic = DGNC_UNIT_MAGIC;
350		ch->ch_tun.un_ch = ch;
351		ch->ch_tun.un_type = DGNC_SERIAL;
352		ch->ch_tun.un_dev = i;
353
354		ch->ch_pun.magic = DGNC_UNIT_MAGIC;
355		ch->ch_pun.un_ch = ch;
356		ch->ch_pun.un_type = DGNC_PRINT;
357		ch->ch_pun.un_dev = i + 128;
358
359		if (brd->bd_uart_offset == 0x200)
360			ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
361		else
362			ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
363
364		ch->ch_bd = brd;
365		ch->ch_portnum = i;
366		ch->ch_digi = dgnc_digi_init;
367
368		/* .25 second delay */
369		ch->ch_close_delay = 250;
370
371		init_waitqueue_head(&ch->ch_flags_wait);
372		init_waitqueue_head(&ch->ch_tun.un_flags_wait);
373		init_waitqueue_head(&ch->ch_pun.un_flags_wait);
374		init_waitqueue_head(&ch->ch_sniff_wait);
375
376		{
377			struct device *classp;
378
379			classp = tty_register_device(&brd->SerialDriver, i,
380				&(ch->ch_bd->pdev->dev));
381			ch->ch_tun.un_sysfs = classp;
382			dgnc_create_tty_sysfs(&ch->ch_tun, classp);
383
384			classp = tty_register_device(&brd->PrintDriver, i,
385				&(ch->ch_bd->pdev->dev));
386			ch->ch_pun.un_sysfs = classp;
387			dgnc_create_tty_sysfs(&ch->ch_pun, classp);
388		}
389
390	}
391
392	return 0;
393}
394
395
396/*
397 * dgnc_tty_post_uninit()
398 *
399 * UnInitialize any global tty related data.
400 */
401void dgnc_tty_post_uninit(void)
402{
403	kfree(dgnc_TmpWriteBuf);
404	dgnc_TmpWriteBuf = NULL;
405}
406
407
408/*
409 * dgnc_tty_uninit()
410 *
411 * Uninitialize the TTY portion of this driver.  Free all memory and
412 * resources.
413 */
414void dgnc_tty_uninit(struct dgnc_board *brd)
415{
416	int i = 0;
417
418	if (brd->dgnc_Major_Serial_Registered) {
419		dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
420		brd->dgnc_Serial_Major = 0;
421		for (i = 0; i < brd->nasync; i++) {
422			dgnc_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
423			tty_unregister_device(&brd->SerialDriver, i);
424		}
425		tty_unregister_driver(&brd->SerialDriver);
426		brd->dgnc_Major_Serial_Registered = FALSE;
427	}
428
429	if (brd->dgnc_Major_TransparentPrint_Registered) {
430		dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
431		brd->dgnc_TransparentPrint_Major = 0;
432		for (i = 0; i < brd->nasync; i++) {
433			dgnc_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
434			tty_unregister_device(&brd->PrintDriver, i);
435		}
436		tty_unregister_driver(&brd->PrintDriver);
437		brd->dgnc_Major_TransparentPrint_Registered = FALSE;
438	}
439
440	kfree(brd->SerialDriver.ttys);
441	brd->SerialDriver.ttys = NULL;
442	kfree(brd->PrintDriver.ttys);
443	brd->PrintDriver.ttys = NULL;
444}
445
446
447#define TMPBUFLEN (1024)
448
449/*
450 * dgnc_sniff - Dump data out to the "sniff" buffer if the
451 * proc sniff file is opened...
452 */
453void dgnc_sniff_nowait_nolock(struct channel_t *ch, unsigned char *text, unsigned char *buf, int len)
454{
455	struct timeval tv;
456	int n;
457	int r;
458	int nbuf;
459	int i;
460	int tmpbuflen;
461	char *tmpbuf;
462	char *p;
463	int too_much_data;
464
465	tmpbuf = kzalloc(TMPBUFLEN, GFP_ATOMIC);
466	if (!tmpbuf)
467		return;
468	p = tmpbuf;
469
470	/* Leave if sniff not open */
471	if (!(ch->ch_sniff_flags & SNIFF_OPEN))
472		goto exit;
473
474	do_gettimeofday(&tv);
475
476	/* Create our header for data dump */
477	p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
478	tmpbuflen = p - tmpbuf;
479
480	do {
481		too_much_data = 0;
482
483		for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
484			p += sprintf(p, "%02x ", *buf);
485			buf++;
486			tmpbuflen = p - tmpbuf;
487		}
488
489		if (tmpbuflen < (TMPBUFLEN - 4)) {
490			if (i > 0)
491				p += sprintf(p - 1, "%s\n", ">");
492			else
493				p += sprintf(p, "%s\n", ">");
494		} else {
495			too_much_data = 1;
496			len -= i;
497		}
498
499		nbuf = strlen(tmpbuf);
500		p = tmpbuf;
501
502		/*
503		 *  Loop while data remains.
504		 */
505		while (nbuf > 0 && ch->ch_sniff_buf) {
506			/*
507			 *  Determine the amount of available space left in the
508			 *  buffer.  If there's none, wait until some appears.
509			 */
510			n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) & SNIFF_MASK;
511
512			/*
513			 * If there is no space left to write to in our sniff buffer,
514			 * we have no choice but to drop the data.
515			 * We *cannot* sleep here waiting for space, because this
516			 * function was probably called by the interrupt/timer routines!
517			 */
518			if (n == 0)
519				goto exit;
520
521			/*
522			 * Copy as much data as will fit.
523			 */
524
525			if (n > nbuf)
526				n = nbuf;
527
528			r = SNIFF_MAX - ch->ch_sniff_in;
529
530			if (r <= n) {
531				memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
532
533				n -= r;
534				ch->ch_sniff_in = 0;
535				p += r;
536				nbuf -= r;
537			}
538
539			memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
540
541			ch->ch_sniff_in += n;
542			p += n;
543			nbuf -= n;
544
545			/*
546			 *  Wakeup any thread waiting for data
547			 */
548			if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
549				ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
550				wake_up_interruptible(&ch->ch_sniff_wait);
551			}
552		}
553
554		/*
555		 * If the user sent us too much data to push into our tmpbuf,
556		 * we need to keep looping around on all the data.
557		 */
558		if (too_much_data) {
559			p = tmpbuf;
560			tmpbuflen = 0;
561		}
562
563	} while (too_much_data);
564
565exit:
566	kfree(tmpbuf);
567}
568
569
570/*=======================================================================
571 *
572 *	dgnc_wmove - Write data to transmit queue.
573 *
574 *		ch	- Pointer to channel structure.
575 *		buf	- Poiter to characters to be moved.
576 *		n	- Number of characters to move.
577 *
578 *=======================================================================*/
579static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
580{
581	int	remain;
582	uint	head;
583
584	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
585		return;
586
587	head = ch->ch_w_head & WQUEUEMASK;
588
589	/*
590	 * If the write wraps over the top of the circular buffer,
591	 * move the portion up to the wrap point, and reset the
592	 * pointers to the bottom.
593	 */
594	remain = WQUEUESIZE - head;
595
596	if (n >= remain) {
597		n -= remain;
598		memcpy(ch->ch_wqueue + head, buf, remain);
599		head = 0;
600		buf += remain;
601	}
602
603	if (n > 0) {
604		/*
605		 * Move rest of data.
606		 */
607		remain = n;
608		memcpy(ch->ch_wqueue + head, buf, remain);
609		head += remain;
610	}
611
612	head &= WQUEUEMASK;
613	ch->ch_w_head = head;
614}
615
616
617
618
619/*=======================================================================
620 *
621 *      dgnc_input - Process received data.
622 *
623 *	      ch      - Pointer to channel structure.
624 *
625 *=======================================================================*/
626void dgnc_input(struct channel_t *ch)
627{
628	struct dgnc_board *bd;
629	struct tty_struct *tp;
630	struct tty_ldisc *ld;
631	uint	rmask;
632	ushort	head;
633	ushort	tail;
634	int	data_len;
635	unsigned long flags;
636	int flip_len;
637	int len = 0;
638	int n = 0;
639	int s = 0;
640	int i = 0;
641
642	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
643		return;
644
645	tp = ch->ch_tun.un_tty;
646
647	bd = ch->ch_bd;
648	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
649		return;
650
651	spin_lock_irqsave(&ch->ch_lock, flags);
652
653	/*
654	 *      Figure the number of characters in the buffer.
655	 *      Exit immediately if none.
656	 */
657	rmask = RQUEUEMASK;
658	head = ch->ch_r_head & rmask;
659	tail = ch->ch_r_tail & rmask;
660	data_len = (head - tail) & rmask;
661
662	if (data_len == 0) {
663		spin_unlock_irqrestore(&ch->ch_lock, flags);
664		return;
665	}
666
667	/*
668	 * If the device is not open, or CREAD is off,
669	 * flush input data and return immediately.
670	 */
671	if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
672	    !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
673
674		ch->ch_r_head = tail;
675
676		/* Force queue flow control to be released, if needed */
677		dgnc_check_queue_flow_control(ch);
678
679		spin_unlock_irqrestore(&ch->ch_lock, flags);
680		return;
681	}
682
683	/*
684	 * If we are throttled, simply don't read any data.
685	 */
686	if (ch->ch_flags & CH_FORCED_STOPI) {
687		spin_unlock_irqrestore(&ch->ch_lock, flags);
688		return;
689	}
690
691	flip_len = TTY_FLIPBUF_SIZE;
692
693	/* Chop down the length, if needed */
694	len = min(data_len, flip_len);
695	len = min(len, (N_TTY_BUF_SIZE - 1));
696
697	ld = tty_ldisc_ref(tp);
698
699#ifdef TTY_DONT_FLIP
700	/*
701	 * If the DONT_FLIP flag is on, don't flush our buffer, and act
702	 * like the ld doesn't have any space to put the data right now.
703	 */
704	if (test_bit(TTY_DONT_FLIP, &tp->flags))
705		len = 0;
706#endif
707
708	/*
709	 * If we were unable to get a reference to the ld,
710	 * don't flush our buffer, and act like the ld doesn't
711	 * have any space to put the data right now.
712	 */
713	if (!ld) {
714		len = 0;
715	} else {
716		/*
717		 * If ld doesn't have a pointer to a receive_buf function,
718		 * flush the data, then act like the ld doesn't have any
719		 * space to put the data right now.
720		 */
721		if (!ld->ops->receive_buf) {
722			ch->ch_r_head = ch->ch_r_tail;
723			len = 0;
724		}
725	}
726
727	if (len <= 0) {
728		spin_unlock_irqrestore(&ch->ch_lock, flags);
729		if (ld)
730			tty_ldisc_deref(ld);
731		return;
732	}
733
734	/*
735	 * The tty layer in the kernel has changed in 2.6.16+.
736	 *
737	 * The flip buffers in the tty structure are no longer exposed,
738	 * and probably will be going away eventually.
739	 *
740	 * If we are completely raw, we don't need to go through a lot
741	 * of the tty layers that exist.
742	 * In this case, we take the shortest and fastest route we
743	 * can to relay the data to the user.
744	 *
745	 * On the other hand, if we are not raw, we need to go through
746	 * the new 2.6.16+ tty layer, which has its API more well defined.
747	 */
748	len = tty_buffer_request_room(tp->port, len);
749	n = len;
750
751	/*
752	 * n now contains the most amount of data we can copy,
753	 * bounded either by how much the Linux tty layer can handle,
754	 * or the amount of data the card actually has pending...
755	 */
756	while (n) {
757		s = ((head >= tail) ? head : RQUEUESIZE) - tail;
758		s = min(s, n);
759
760		if (s <= 0)
761			break;
762
763		/*
764		 * If conditions are such that ld needs to see all
765		 * UART errors, we will have to walk each character
766		 * and error byte and send them to the buffer one at
767		 * a time.
768		 */
769		if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
770			for (i = 0; i < s; i++) {
771				if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
772					tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
773				else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
774					tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
775				else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
776					tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
777				else
778					tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
779			}
780		} else {
781			tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
782		}
783
784		dgnc_sniff_nowait_nolock(ch, "USER READ", ch->ch_rqueue + tail, s);
785
786		tail += s;
787		n -= s;
788		/* Flip queue if needed */
789		tail &= rmask;
790	}
791
792	ch->ch_r_tail = tail & rmask;
793	ch->ch_e_tail = tail & rmask;
794	dgnc_check_queue_flow_control(ch);
795	spin_unlock_irqrestore(&ch->ch_lock, flags);
796
797	/* Tell the tty layer its okay to "eat" the data now */
798	tty_flip_buffer_push(tp->port);
799
800	if (ld)
801		tty_ldisc_deref(ld);
802}
803
804
805/************************************************************************
806 * Determines when CARRIER changes state and takes appropriate
807 * action.
808 ************************************************************************/
809void dgnc_carrier(struct channel_t *ch)
810{
811	struct dgnc_board *bd;
812
813	int virt_carrier = 0;
814	int phys_carrier = 0;
815
816	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
817		return;
818
819	bd = ch->ch_bd;
820
821	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
822		return;
823
824	if (ch->ch_mistat & UART_MSR_DCD)
825		phys_carrier = 1;
826
827	if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
828		virt_carrier = 1;
829
830	if (ch->ch_c_cflag & CLOCAL)
831		virt_carrier = 1;
832
833	/*
834	 * Test for a VIRTUAL carrier transition to HIGH.
835	 */
836	if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
837
838		/*
839		 * When carrier rises, wake any threads waiting
840		 * for carrier in the open routine.
841		 */
842
843		if (waitqueue_active(&(ch->ch_flags_wait)))
844			wake_up_interruptible(&ch->ch_flags_wait);
845	}
846
847	/*
848	 * Test for a PHYSICAL carrier transition to HIGH.
849	 */
850	if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
851
852		/*
853		 * When carrier rises, wake any threads waiting
854		 * for carrier in the open routine.
855		 */
856
857		if (waitqueue_active(&(ch->ch_flags_wait)))
858			wake_up_interruptible(&ch->ch_flags_wait);
859	}
860
861	/*
862	 *  Test for a PHYSICAL transition to low, so long as we aren't
863	 *  currently ignoring physical transitions (which is what "virtual
864	 *  carrier" indicates).
865	 *
866	 *  The transition of the virtual carrier to low really doesn't
867	 *  matter... it really only means "ignore carrier state", not
868	 *  "make pretend that carrier is there".
869	 */
870	if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
871	    (phys_carrier == 0)) {
872
873		/*
874		 *   When carrier drops:
875		 *
876		 *   Drop carrier on all open units.
877		 *
878		 *   Flush queues, waking up any task waiting in the
879		 *   line discipline.
880		 *
881		 *   Send a hangup to the control terminal.
882		 *
883		 *   Enable all select calls.
884		 */
885		if (waitqueue_active(&(ch->ch_flags_wait)))
886			wake_up_interruptible(&ch->ch_flags_wait);
887
888		if (ch->ch_tun.un_open_count > 0)
889			tty_hangup(ch->ch_tun.un_tty);
890
891		if (ch->ch_pun.un_open_count > 0)
892			tty_hangup(ch->ch_pun.un_tty);
893	}
894
895	/*
896	 *  Make sure that our cached values reflect the current reality.
897	 */
898	if (virt_carrier == 1)
899		ch->ch_flags |= CH_FCAR;
900	else
901		ch->ch_flags &= ~CH_FCAR;
902
903	if (phys_carrier == 1)
904		ch->ch_flags |= CH_CD;
905	else
906		ch->ch_flags &= ~CH_CD;
907}
908
909/*
910 *  Assign the custom baud rate to the channel structure
911 */
912static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
913{
914	int testdiv;
915	int testrate_high;
916	int testrate_low;
917	int deltahigh;
918	int deltalow;
919
920	if (newrate <= 0) {
921		ch->ch_custom_speed = 0;
922		return;
923	}
924
925	/*
926	 *  Since the divisor is stored in a 16-bit integer, we make sure
927	 *  we don't allow any rates smaller than a 16-bit integer would allow.
928	 *  And of course, rates above the dividend won't fly.
929	 */
930	if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
931		newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
932
933	if (newrate && newrate > ch->ch_bd->bd_dividend)
934		newrate = ch->ch_bd->bd_dividend;
935
936	if (newrate > 0) {
937		testdiv = ch->ch_bd->bd_dividend / newrate;
938
939		/*
940		 *  If we try to figure out what rate the board would use
941		 *  with the test divisor, it will be either equal or higher
942		 *  than the requested baud rate.  If we then determine the
943		 *  rate with a divisor one higher, we will get the next lower
944		 *  supported rate below the requested.
945		 */
946		testrate_high = ch->ch_bd->bd_dividend / testdiv;
947		testrate_low  = ch->ch_bd->bd_dividend / (testdiv + 1);
948
949		/*
950		 *  If the rate for the requested divisor is correct, just
951		 *  use it and be done.
952		 */
953		if (testrate_high != newrate) {
954			/*
955			 *  Otherwise, pick the rate that is closer (i.e. whichever rate
956			 *  has a smaller delta).
957			 */
958			deltahigh = testrate_high - newrate;
959			deltalow = newrate - testrate_low;
960
961			if (deltahigh < deltalow)
962				newrate = testrate_high;
963			else
964				newrate = testrate_low;
965		}
966	}
967
968	ch->ch_custom_speed = newrate;
969}
970
971
972void dgnc_check_queue_flow_control(struct channel_t *ch)
973{
974	int qleft = 0;
975
976	/* Store how much space we have left in the queue */
977	qleft = ch->ch_r_tail - ch->ch_r_head - 1;
978	if (qleft < 0)
979		qleft += RQUEUEMASK + 1;
980
981	/*
982	 * Check to see if we should enforce flow control on our queue because
983	 * the ld (or user) isn't reading data out of our queue fast enuf.
984	 *
985	 * NOTE: This is done based on what the current flow control of the
986	 * port is set for.
987	 *
988	 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
989	 *	This will cause the UART's FIFO to back up, and force
990	 *	the RTS signal to be dropped.
991	 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
992	 *	the other side, in hopes it will stop sending data to us.
993	 * 3) NONE - Nothing we can do.  We will simply drop any extra data
994	 *	that gets sent into us when the queue fills up.
995	 */
996	if (qleft < 256) {
997		/* HWFLOW */
998		if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
999			if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
1000				ch->ch_bd->bd_ops->disable_receiver(ch);
1001				ch->ch_flags |= (CH_RECEIVER_OFF);
1002			}
1003		}
1004		/* SWFLOW */
1005		else if (ch->ch_c_iflag & IXOFF) {
1006			if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
1007				ch->ch_bd->bd_ops->send_stop_character(ch);
1008				ch->ch_stops_sent++;
1009			}
1010		}
1011		/* No FLOW */
1012		else {
1013			/* Empty... Can't do anything about the impending overflow... */
1014		}
1015	}
1016
1017	/*
1018	 * Check to see if we should unenforce flow control because
1019	 * ld (or user) finally read enuf data out of our queue.
1020	 *
1021	 * NOTE: This is done based on what the current flow control of the
1022	 * port is set for.
1023	 *
1024	 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1025	 *	This will cause the UART's FIFO to raise RTS back up,
1026	 *	which will allow the other side to start sending data again.
1027	 * 2) SWFLOW (IXOFF) - Send a start character to
1028	 *	the other side, so it will start sending data to us again.
1029	 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1030	 *	other side, we don't need to do anything now.
1031	 */
1032	if (qleft > (RQUEUESIZE / 2)) {
1033		/* HWFLOW */
1034		if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
1035			if (ch->ch_flags & CH_RECEIVER_OFF) {
1036				ch->ch_bd->bd_ops->enable_receiver(ch);
1037				ch->ch_flags &= ~(CH_RECEIVER_OFF);
1038			}
1039		}
1040		/* SWFLOW */
1041		else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
1042			ch->ch_stops_sent = 0;
1043			ch->ch_bd->bd_ops->send_start_character(ch);
1044		}
1045		/* No FLOW */
1046		else {
1047			/* Nothing needed. */
1048		}
1049	}
1050}
1051
1052
1053void dgnc_wakeup_writes(struct channel_t *ch)
1054{
1055	int qlen = 0;
1056	unsigned long flags;
1057
1058	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1059		return;
1060
1061	spin_lock_irqsave(&ch->ch_lock, flags);
1062
1063	/*
1064	 * If channel now has space, wake up anyone waiting on the condition.
1065	 */
1066	qlen = ch->ch_w_head - ch->ch_w_tail;
1067	if (qlen < 0)
1068		qlen += WQUEUESIZE;
1069
1070	if (qlen >= (WQUEUESIZE - 256)) {
1071		spin_unlock_irqrestore(&ch->ch_lock, flags);
1072		return;
1073	}
1074
1075	if (ch->ch_tun.un_flags & UN_ISOPEN) {
1076		if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1077			ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
1078			spin_unlock_irqrestore(&ch->ch_lock, flags);
1079			(ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1080			spin_lock_irqsave(&ch->ch_lock, flags);
1081		}
1082
1083		wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1084
1085		/*
1086		 * If unit is set to wait until empty, check to make sure
1087		 * the queue AND FIFO are both empty.
1088		 */
1089		if (ch->ch_tun.un_flags & UN_EMPTY) {
1090			if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1091				ch->ch_tun.un_flags &= ~(UN_EMPTY);
1092
1093				/*
1094				 * If RTS Toggle mode is on, whenever
1095				 * the queue and UART is empty, keep RTS low.
1096				 */
1097				if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1098					ch->ch_mostat &= ~(UART_MCR_RTS);
1099					ch->ch_bd->bd_ops->assert_modem_signals(ch);
1100				}
1101
1102				/*
1103				 * If DTR Toggle mode is on, whenever
1104				 * the queue and UART is empty, keep DTR low.
1105				 */
1106				if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1107					ch->ch_mostat &= ~(UART_MCR_DTR);
1108					ch->ch_bd->bd_ops->assert_modem_signals(ch);
1109				}
1110			}
1111		}
1112
1113		wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1114	}
1115
1116	if (ch->ch_pun.un_flags & UN_ISOPEN) {
1117		if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1118			ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
1119			spin_unlock_irqrestore(&ch->ch_lock, flags);
1120			(ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1121			spin_lock_irqsave(&ch->ch_lock, flags);
1122		}
1123
1124		wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1125
1126		/*
1127		 * If unit is set to wait until empty, check to make sure
1128		 * the queue AND FIFO are both empty.
1129		 */
1130		if (ch->ch_pun.un_flags & UN_EMPTY) {
1131			if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
1132				ch->ch_pun.un_flags &= ~(UN_EMPTY);
1133		}
1134
1135		wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1136	}
1137
1138	spin_unlock_irqrestore(&ch->ch_lock, flags);
1139}
1140
1141
1142
1143/************************************************************************
1144 *
1145 * TTY Entry points and helper functions
1146 *
1147 ************************************************************************/
1148
1149/*
1150 * dgnc_tty_open()
1151 *
1152 */
1153static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1154{
1155	struct dgnc_board	*brd;
1156	struct channel_t *ch;
1157	struct un_t	*un;
1158	uint		major = 0;
1159	uint		minor = 0;
1160	int		rc = 0;
1161	unsigned long flags;
1162
1163	rc = 0;
1164
1165	major = MAJOR(tty_devnum(tty));
1166	minor = MINOR(tty_devnum(tty));
1167
1168	if (major > 255)
1169		return -ENXIO;
1170
1171	/* Get board pointer from our array of majors we have allocated */
1172	brd = dgnc_BoardsByMajor[major];
1173	if (!brd)
1174		return -ENXIO;
1175
1176	/*
1177	 * If board is not yet up to a state of READY, go to
1178	 * sleep waiting for it to happen or they cancel the open.
1179	 */
1180	rc = wait_event_interruptible(brd->state_wait,
1181		(brd->state & BOARD_READY));
1182
1183	if (rc)
1184		return rc;
1185
1186	spin_lock_irqsave(&brd->bd_lock, flags);
1187
1188	/* If opened device is greater than our number of ports, bail. */
1189	if (PORT_NUM(minor) > brd->nasync) {
1190		spin_unlock_irqrestore(&brd->bd_lock, flags);
1191		return -ENXIO;
1192	}
1193
1194	ch = brd->channels[PORT_NUM(minor)];
1195	if (!ch) {
1196		spin_unlock_irqrestore(&brd->bd_lock, flags);
1197		return -ENXIO;
1198	}
1199
1200	/* Drop board lock */
1201	spin_unlock_irqrestore(&brd->bd_lock, flags);
1202
1203	/* Grab channel lock */
1204	spin_lock_irqsave(&ch->ch_lock, flags);
1205
1206	/* Figure out our type */
1207	if (!IS_PRINT(minor)) {
1208		un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1209		un->un_type = DGNC_SERIAL;
1210	} else if (IS_PRINT(minor)) {
1211		un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1212		un->un_type = DGNC_PRINT;
1213	} else {
1214		spin_unlock_irqrestore(&ch->ch_lock, flags);
1215		return -ENXIO;
1216	}
1217
1218	/*
1219	 * If the port is still in a previous open, and in a state
1220	 * where we simply cannot safely keep going, wait until the
1221	 * state clears.
1222	 */
1223	spin_unlock_irqrestore(&ch->ch_lock, flags);
1224
1225	rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1226
1227	/* If ret is non-zero, user ctrl-c'ed us */
1228	if (rc)
1229		return -EINTR;
1230
1231	/*
1232	 * If either unit is in the middle of the fragile part of close,
1233	 * we just cannot touch the channel safely.
1234	 * Go to sleep, knowing that when the channel can be
1235	 * touched safely, the close routine will signal the
1236	 * ch_flags_wait to wake us back up.
1237	 */
1238	rc = wait_event_interruptible(ch->ch_flags_wait,
1239		(((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1240
1241	/* If ret is non-zero, user ctrl-c'ed us */
1242	if (rc)
1243		return -EINTR;
1244
1245	spin_lock_irqsave(&ch->ch_lock, flags);
1246
1247
1248	/* Store our unit into driver_data, so we always have it available. */
1249	tty->driver_data = un;
1250
1251
1252	/*
1253	 * Initialize tty's
1254	 */
1255	if (!(un->un_flags & UN_ISOPEN)) {
1256		/* Store important variables. */
1257		un->un_tty     = tty;
1258
1259		/* Maybe do something here to the TTY struct as well? */
1260	}
1261
1262
1263	/*
1264	 * Allocate channel buffers for read/write/error.
1265	 * Set flag, so we don't get trounced on.
1266	 */
1267	ch->ch_flags |= (CH_OPENING);
1268
1269	/* Drop locks, as malloc with GFP_KERNEL can sleep */
1270	spin_unlock_irqrestore(&ch->ch_lock, flags);
1271
1272	if (!ch->ch_rqueue)
1273		ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1274	if (!ch->ch_equeue)
1275		ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1276	if (!ch->ch_wqueue)
1277		ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1278
1279	spin_lock_irqsave(&ch->ch_lock, flags);
1280
1281	ch->ch_flags &= ~(CH_OPENING);
1282	wake_up_interruptible(&ch->ch_flags_wait);
1283
1284	/*
1285	 * Initialize if neither terminal or printer is open.
1286	 */
1287	if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1288
1289		/*
1290		 * Flush input queues.
1291		 */
1292		ch->ch_r_head = 0;
1293		ch->ch_r_tail = 0;
1294		ch->ch_e_head = 0;
1295		ch->ch_e_tail = 0;
1296		ch->ch_w_head = 0;
1297		ch->ch_w_tail = 0;
1298
1299		brd->bd_ops->flush_uart_write(ch);
1300		brd->bd_ops->flush_uart_read(ch);
1301
1302		ch->ch_flags = 0;
1303		ch->ch_cached_lsr = 0;
1304		ch->ch_stop_sending_break = 0;
1305		ch->ch_stops_sent = 0;
1306
1307		ch->ch_c_cflag   = tty->termios.c_cflag;
1308		ch->ch_c_iflag   = tty->termios.c_iflag;
1309		ch->ch_c_oflag   = tty->termios.c_oflag;
1310		ch->ch_c_lflag   = tty->termios.c_lflag;
1311		ch->ch_startc = tty->termios.c_cc[VSTART];
1312		ch->ch_stopc  = tty->termios.c_cc[VSTOP];
1313
1314		/*
1315		 * Bring up RTS and DTR...
1316		 * Also handle RTS or DTR toggle if set.
1317		 */
1318		if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1319			ch->ch_mostat |= (UART_MCR_RTS);
1320		if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1321			ch->ch_mostat |= (UART_MCR_DTR);
1322
1323		/* Tell UART to init itself */
1324		brd->bd_ops->uart_init(ch);
1325	}
1326
1327	/*
1328	 * Run param in case we changed anything
1329	 */
1330	brd->bd_ops->param(tty);
1331
1332	dgnc_carrier(ch);
1333
1334	/*
1335	 * follow protocol for opening port
1336	 */
1337
1338	spin_unlock_irqrestore(&ch->ch_lock, flags);
1339
1340	rc = dgnc_block_til_ready(tty, file, ch);
1341
1342	/* No going back now, increment our unit and channel counters */
1343	spin_lock_irqsave(&ch->ch_lock, flags);
1344	ch->ch_open_count++;
1345	un->un_open_count++;
1346	un->un_flags |= (UN_ISOPEN);
1347	spin_unlock_irqrestore(&ch->ch_lock, flags);
1348
1349	return rc;
1350}
1351
1352
1353/*
1354 * dgnc_block_til_ready()
1355 *
1356 * Wait for DCD, if needed.
1357 */
1358static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1359{
1360	int retval = 0;
1361	struct un_t *un = NULL;
1362	unsigned long flags;
1363	uint	old_flags = 0;
1364	int	sleep_on_un_flags = 0;
1365
1366	if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC)
1367		return -ENXIO;
1368
1369	un = tty->driver_data;
1370	if (!un || un->magic != DGNC_UNIT_MAGIC)
1371		return -ENXIO;
1372
1373	spin_lock_irqsave(&ch->ch_lock, flags);
1374
1375	ch->ch_wopen++;
1376
1377	/* Loop forever */
1378	while (1) {
1379
1380		sleep_on_un_flags = 0;
1381
1382		/*
1383		 * If board has failed somehow during our sleep, bail with error.
1384		 */
1385		if (ch->ch_bd->state == BOARD_FAILED) {
1386			retval = -ENXIO;
1387			break;
1388		}
1389
1390		/* If tty was hung up, break out of loop and set error. */
1391		if (tty_hung_up_p(file)) {
1392			retval = -EAGAIN;
1393			break;
1394		}
1395
1396		/*
1397		 * If either unit is in the middle of the fragile part of close,
1398		 * we just cannot touch the channel safely.
1399		 * Go back to sleep, knowing that when the channel can be
1400		 * touched safely, the close routine will signal the
1401		 * ch_wait_flags to wake us back up.
1402		 */
1403		if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1404
1405			/*
1406			 * Our conditions to leave cleanly and happily:
1407			 * 1) NONBLOCKING on the tty is set.
1408			 * 2) CLOCAL is set.
1409			 * 3) DCD (fake or real) is active.
1410			 */
1411
1412			if (file->f_flags & O_NONBLOCK)
1413				break;
1414
1415			if (tty->flags & (1 << TTY_IO_ERROR)) {
1416				retval = -EIO;
1417				break;
1418			}
1419
1420			if (ch->ch_flags & CH_CD)
1421				break;
1422
1423			if (ch->ch_flags & CH_FCAR)
1424				break;
1425		} else {
1426			sleep_on_un_flags = 1;
1427		}
1428
1429		/*
1430		 * If there is a signal pending, the user probably
1431		 * interrupted (ctrl-c) us.
1432		 * Leave loop with error set.
1433		 */
1434		if (signal_pending(current)) {
1435			retval = -ERESTARTSYS;
1436			break;
1437		}
1438
1439		/*
1440		 * Store the flags before we let go of channel lock
1441		 */
1442		if (sleep_on_un_flags)
1443			old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1444		else
1445			old_flags = ch->ch_flags;
1446
1447		/*
1448		 * Let go of channel lock before calling schedule.
1449		 * Our poller will get any FEP events and wake us up when DCD
1450		 * eventually goes active.
1451		 */
1452
1453		spin_unlock_irqrestore(&ch->ch_lock, flags);
1454
1455		/*
1456		 * Wait for something in the flags to change from the current value.
1457		 */
1458		if (sleep_on_un_flags)
1459			retval = wait_event_interruptible(un->un_flags_wait,
1460				(old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1461		else
1462			retval = wait_event_interruptible(ch->ch_flags_wait,
1463				(old_flags != ch->ch_flags));
1464
1465		/*
1466		 * We got woken up for some reason.
1467		 * Before looping around, grab our channel lock.
1468		 */
1469		spin_lock_irqsave(&ch->ch_lock, flags);
1470	}
1471
1472	ch->ch_wopen--;
1473
1474	spin_unlock_irqrestore(&ch->ch_lock, flags);
1475
1476	if (retval)
1477		return retval;
1478
1479	return 0;
1480}
1481
1482
1483/*
1484 * dgnc_tty_hangup()
1485 *
1486 * Hangup the port.  Like a close, but don't wait for output to drain.
1487 */
1488static void dgnc_tty_hangup(struct tty_struct *tty)
1489{
1490	struct un_t	*un;
1491
1492	if (!tty || tty->magic != TTY_MAGIC)
1493		return;
1494
1495	un = tty->driver_data;
1496	if (!un || un->magic != DGNC_UNIT_MAGIC)
1497		return;
1498
1499	/* flush the transmit queues */
1500	dgnc_tty_flush_buffer(tty);
1501
1502}
1503
1504
1505/*
1506 * dgnc_tty_close()
1507 *
1508 */
1509static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1510{
1511	struct ktermios *ts;
1512	struct dgnc_board *bd;
1513	struct channel_t *ch;
1514	struct un_t *un;
1515	unsigned long flags;
1516	int rc = 0;
1517
1518	if (!tty || tty->magic != TTY_MAGIC)
1519		return;
1520
1521	un = tty->driver_data;
1522	if (!un || un->magic != DGNC_UNIT_MAGIC)
1523		return;
1524
1525	ch = un->un_ch;
1526	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1527		return;
1528
1529	bd = ch->ch_bd;
1530	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1531		return;
1532
1533	ts = &tty->termios;
1534
1535	spin_lock_irqsave(&ch->ch_lock, flags);
1536
1537	/*
1538	 * Determine if this is the last close or not - and if we agree about
1539	 * which type of close it is with the Line Discipline
1540	 */
1541	if ((tty->count == 1) && (un->un_open_count != 1)) {
1542		/*
1543		 * Uh, oh.  tty->count is 1, which means that the tty
1544		 * structure will be freed.  un_open_count should always
1545		 * be one in these conditions.  If it's greater than
1546		 * one, we've got real problems, since it means the
1547		 * serial port won't be shutdown.
1548		 */
1549		APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1550		un->un_open_count = 1;
1551	}
1552
1553	if (un->un_open_count)
1554		un->un_open_count--;
1555	else
1556		APR(("bad serial port open count of %d\n", un->un_open_count));
1557
1558	ch->ch_open_count--;
1559
1560	if (ch->ch_open_count && un->un_open_count) {
1561		spin_unlock_irqrestore(&ch->ch_lock, flags);
1562		return;
1563	}
1564
1565	/* OK, its the last close on the unit */
1566	un->un_flags |= UN_CLOSING;
1567
1568	tty->closing = 1;
1569
1570
1571	/*
1572	 * Only officially close channel if count is 0 and
1573	 * DIGI_PRINTER bit is not set.
1574	 */
1575	if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1576
1577		ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1578
1579		/*
1580		 * turn off print device when closing print device.
1581		 */
1582		if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1583			dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1584				(int) ch->ch_digi.digi_offlen);
1585			ch->ch_flags &= ~CH_PRON;
1586		}
1587
1588		spin_unlock_irqrestore(&ch->ch_lock, flags);
1589		/* wait for output to drain */
1590		/* This will also return if we take an interrupt */
1591
1592		rc = bd->bd_ops->drain(tty, 0);
1593
1594		dgnc_tty_flush_buffer(tty);
1595		tty_ldisc_flush(tty);
1596
1597		spin_lock_irqsave(&ch->ch_lock, flags);
1598
1599		tty->closing = 0;
1600
1601		/*
1602		 * If we have HUPCL set, lower DTR and RTS
1603		 */
1604		if (ch->ch_c_cflag & HUPCL) {
1605
1606			/* Drop RTS/DTR */
1607			ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1608			bd->bd_ops->assert_modem_signals(ch);
1609
1610			/*
1611			 * Go to sleep to ensure RTS/DTR
1612			 * have been dropped for modems to see it.
1613			 */
1614			if (ch->ch_close_delay) {
1615				spin_unlock_irqrestore(&ch->ch_lock,
1616						       flags);
1617				dgnc_ms_sleep(ch->ch_close_delay);
1618				spin_lock_irqsave(&ch->ch_lock, flags);
1619			}
1620		}
1621
1622		ch->ch_old_baud = 0;
1623
1624		/* Turn off UART interrupts for this port */
1625		ch->ch_bd->bd_ops->uart_off(ch);
1626	} else {
1627		/*
1628		 * turn off print device when closing print device.
1629		 */
1630		if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1631			dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1632				(int) ch->ch_digi.digi_offlen);
1633			ch->ch_flags &= ~CH_PRON;
1634		}
1635	}
1636
1637	un->un_tty = NULL;
1638	un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1639
1640	wake_up_interruptible(&ch->ch_flags_wait);
1641	wake_up_interruptible(&un->un_flags_wait);
1642
1643	spin_unlock_irqrestore(&ch->ch_lock, flags);
1644}
1645
1646
1647/*
1648 * dgnc_tty_chars_in_buffer()
1649 *
1650 * Return number of characters that have not been transmitted yet.
1651 *
1652 * This routine is used by the line discipline to determine if there
1653 * is data waiting to be transmitted/drained/flushed or not.
1654 */
1655static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1656{
1657	struct channel_t *ch = NULL;
1658	struct un_t *un = NULL;
1659	ushort thead;
1660	ushort ttail;
1661	uint tmask;
1662	uint chars = 0;
1663	unsigned long flags;
1664
1665	if (tty == NULL)
1666		return 0;
1667
1668	un = tty->driver_data;
1669	if (!un || un->magic != DGNC_UNIT_MAGIC)
1670		return 0;
1671
1672	ch = un->un_ch;
1673	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1674		return 0;
1675
1676	spin_lock_irqsave(&ch->ch_lock, flags);
1677
1678	tmask = WQUEUEMASK;
1679	thead = ch->ch_w_head & tmask;
1680	ttail = ch->ch_w_tail & tmask;
1681
1682	spin_unlock_irqrestore(&ch->ch_lock, flags);
1683
1684	if (ttail == thead) {
1685		chars = 0;
1686	} else {
1687		if (thead >= ttail)
1688			chars = thead - ttail;
1689		else
1690			chars = thead - ttail + WQUEUESIZE;
1691	}
1692
1693	return chars;
1694}
1695
1696
1697/*
1698 * dgnc_maxcps_room
1699 *
1700 * Reduces bytes_available to the max number of characters
1701 * that can be sent currently given the maxcps value, and
1702 * returns the new bytes_available.  This only affects printer
1703 * output.
1704 */
1705static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1706{
1707	struct channel_t *ch = NULL;
1708	struct un_t *un = NULL;
1709
1710	if (!tty)
1711		return bytes_available;
1712
1713	un = tty->driver_data;
1714	if (!un || un->magic != DGNC_UNIT_MAGIC)
1715		return bytes_available;
1716
1717	ch = un->un_ch;
1718	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1719		return bytes_available;
1720
1721	/*
1722	 * If its not the Transparent print device, return
1723	 * the full data amount.
1724	 */
1725	if (un->un_type != DGNC_PRINT)
1726		return bytes_available;
1727
1728	if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1729		int cps_limit = 0;
1730		unsigned long current_time = jiffies;
1731		unsigned long buffer_time = current_time +
1732			(HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1733
1734		if (ch->ch_cpstime < current_time) {
1735			/* buffer is empty */
1736			ch->ch_cpstime = current_time;	    /* reset ch_cpstime */
1737			cps_limit = ch->ch_digi.digi_bufsize;
1738		} else if (ch->ch_cpstime < buffer_time) {
1739			/* still room in the buffer */
1740			cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1741		} else {
1742			/* no room in the buffer */
1743			cps_limit = 0;
1744		}
1745
1746		bytes_available = min(cps_limit, bytes_available);
1747	}
1748
1749	return bytes_available;
1750}
1751
1752
1753/*
1754 * dgnc_tty_write_room()
1755 *
1756 * Return space available in Tx buffer
1757 */
1758static int dgnc_tty_write_room(struct tty_struct *tty)
1759{
1760	struct channel_t *ch = NULL;
1761	struct un_t *un = NULL;
1762	ushort head;
1763	ushort tail;
1764	ushort tmask;
1765	int ret = 0;
1766	unsigned long flags;
1767
1768	if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1769		return 0;
1770
1771	un = tty->driver_data;
1772	if (!un || un->magic != DGNC_UNIT_MAGIC)
1773		return 0;
1774
1775	ch = un->un_ch;
1776	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1777		return 0;
1778
1779	spin_lock_irqsave(&ch->ch_lock, flags);
1780
1781	tmask = WQUEUEMASK;
1782	head = (ch->ch_w_head) & tmask;
1783	tail = (ch->ch_w_tail) & tmask;
1784
1785	ret = tail - head - 1;
1786	if (ret < 0)
1787		ret += WQUEUESIZE;
1788
1789	/* Limit printer to maxcps */
1790	ret = dgnc_maxcps_room(tty, ret);
1791
1792	/*
1793	 * If we are printer device, leave space for
1794	 * possibly both the on and off strings.
1795	 */
1796	if (un->un_type == DGNC_PRINT) {
1797		if (!(ch->ch_flags & CH_PRON))
1798			ret -= ch->ch_digi.digi_onlen;
1799		ret -= ch->ch_digi.digi_offlen;
1800	} else {
1801		if (ch->ch_flags & CH_PRON)
1802			ret -= ch->ch_digi.digi_offlen;
1803	}
1804
1805	if (ret < 0)
1806		ret = 0;
1807
1808	spin_unlock_irqrestore(&ch->ch_lock, flags);
1809
1810	return ret;
1811}
1812
1813
1814/*
1815 * dgnc_tty_put_char()
1816 *
1817 * Put a character into ch->ch_buf
1818 *
1819 *      - used by the line discipline for OPOST processing
1820 */
1821static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1822{
1823	/*
1824	 * Simply call tty_write.
1825	 */
1826	dgnc_tty_write(tty, &c, 1);
1827	return 1;
1828}
1829
1830
1831/*
1832 * dgnc_tty_write()
1833 *
1834 * Take data from the user or kernel and send it out to the FEP.
1835 * In here exists all the Transparent Print magic as well.
1836 */
1837static int dgnc_tty_write(struct tty_struct *tty,
1838		const unsigned char *buf, int count)
1839{
1840	struct channel_t *ch = NULL;
1841	struct un_t *un = NULL;
1842	int bufcount = 0, n = 0;
1843	int orig_count = 0;
1844	unsigned long flags;
1845	ushort head;
1846	ushort tail;
1847	ushort tmask;
1848	uint remain;
1849	int from_user = 0;
1850
1851	if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1852		return 0;
1853
1854	un = tty->driver_data;
1855	if (!un || un->magic != DGNC_UNIT_MAGIC)
1856		return 0;
1857
1858	ch = un->un_ch;
1859	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1860		return 0;
1861
1862	if (!count)
1863		return 0;
1864
1865	/*
1866	 * Store original amount of characters passed in.
1867	 * This helps to figure out if we should ask the FEP
1868	 * to send us an event when it has more space available.
1869	 */
1870	orig_count = count;
1871
1872	spin_lock_irqsave(&ch->ch_lock, flags);
1873
1874	/* Get our space available for the channel from the board */
1875	tmask = WQUEUEMASK;
1876	head = (ch->ch_w_head) & tmask;
1877	tail = (ch->ch_w_tail) & tmask;
1878
1879	bufcount = tail - head - 1;
1880	if (bufcount < 0)
1881		bufcount += WQUEUESIZE;
1882
1883	/*
1884	 * Limit printer output to maxcps overall, with bursts allowed
1885	 * up to bufsize characters.
1886	 */
1887	bufcount = dgnc_maxcps_room(tty, bufcount);
1888
1889	/*
1890	 * Take minimum of what the user wants to send, and the
1891	 * space available in the FEP buffer.
1892	 */
1893	count = min(count, bufcount);
1894
1895	/*
1896	 * Bail if no space left.
1897	 */
1898	if (count <= 0) {
1899		spin_unlock_irqrestore(&ch->ch_lock, flags);
1900		return 0;
1901	}
1902
1903	/*
1904	 * Output the printer ON string, if we are in terminal mode, but
1905	 * need to be in printer mode.
1906	 */
1907	if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1908		dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1909		    (int) ch->ch_digi.digi_onlen);
1910		head = (ch->ch_w_head) & tmask;
1911		ch->ch_flags |= CH_PRON;
1912	}
1913
1914	/*
1915	 * On the other hand, output the printer OFF string, if we are
1916	 * currently in printer mode, but need to output to the terminal.
1917	 */
1918	if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1919		dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1920			(int) ch->ch_digi.digi_offlen);
1921		head = (ch->ch_w_head) & tmask;
1922		ch->ch_flags &= ~CH_PRON;
1923	}
1924
1925	/*
1926	 * If there is nothing left to copy, or I can't handle any more data, leave.
1927	 */
1928	if (count <= 0) {
1929		spin_unlock_irqrestore(&ch->ch_lock, flags);
1930		return 0;
1931	}
1932
1933	if (from_user) {
1934
1935		count = min(count, WRITEBUFLEN);
1936
1937		spin_unlock_irqrestore(&ch->ch_lock, flags);
1938
1939		/*
1940		 * If data is coming from user space, copy it into a temporary
1941		 * buffer so we don't get swapped out while doing the copy to
1942		 * the board.
1943		 */
1944		/* we're allowed to block if it's from_user */
1945		if (down_interruptible(&dgnc_TmpWriteSem))
1946			return -EINTR;
1947
1948		/*
1949		 * copy_from_user() returns the number
1950		 * of bytes that could *NOT* be copied.
1951		 */
1952		count -= copy_from_user(dgnc_TmpWriteBuf, (const unsigned char __user *) buf, count);
1953
1954		if (!count) {
1955			up(&dgnc_TmpWriteSem);
1956			return -EFAULT;
1957		}
1958
1959		spin_lock_irqsave(&ch->ch_lock, flags);
1960
1961		buf = dgnc_TmpWriteBuf;
1962
1963	}
1964
1965	n = count;
1966
1967	/*
1968	 * If the write wraps over the top of the circular buffer,
1969	 * move the portion up to the wrap point, and reset the
1970	 * pointers to the bottom.
1971	 */
1972	remain = WQUEUESIZE - head;
1973
1974	if (n >= remain) {
1975		n -= remain;
1976		memcpy(ch->ch_wqueue + head, buf, remain);
1977		dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1978		head = 0;
1979		buf += remain;
1980	}
1981
1982	if (n > 0) {
1983		/*
1984		 * Move rest of data.
1985		 */
1986		remain = n;
1987		memcpy(ch->ch_wqueue + head, buf, remain);
1988		dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1989		head += remain;
1990	}
1991
1992	if (count) {
1993		head &= tmask;
1994		ch->ch_w_head = head;
1995	}
1996
1997	/* Update printer buffer empty time. */
1998	if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
1999	    && (ch->ch_digi.digi_bufsize > 0)) {
2000		ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2001	}
2002
2003	if (from_user) {
2004		spin_unlock_irqrestore(&ch->ch_lock, flags);
2005		up(&dgnc_TmpWriteSem);
2006	} else {
2007		spin_unlock_irqrestore(&ch->ch_lock, flags);
2008	}
2009
2010	if (count) {
2011		/*
2012		 * Channel lock is grabbed and then released
2013		 * inside this routine.
2014		 */
2015		ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2016	}
2017
2018	return count;
2019}
2020
2021
2022/*
2023 * Return modem signals to ld.
2024 */
2025
2026static int dgnc_tty_tiocmget(struct tty_struct *tty)
2027{
2028	struct channel_t *ch;
2029	struct un_t *un;
2030	int result = -EIO;
2031	unsigned char mstat = 0;
2032	unsigned long flags;
2033
2034	if (!tty || tty->magic != TTY_MAGIC)
2035		return result;
2036
2037	un = tty->driver_data;
2038	if (!un || un->magic != DGNC_UNIT_MAGIC)
2039		return result;
2040
2041	ch = un->un_ch;
2042	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2043		return result;
2044
2045	spin_lock_irqsave(&ch->ch_lock, flags);
2046
2047	mstat = (ch->ch_mostat | ch->ch_mistat);
2048
2049	spin_unlock_irqrestore(&ch->ch_lock, flags);
2050
2051	result = 0;
2052
2053	if (mstat & UART_MCR_DTR)
2054		result |= TIOCM_DTR;
2055	if (mstat & UART_MCR_RTS)
2056		result |= TIOCM_RTS;
2057	if (mstat & UART_MSR_CTS)
2058		result |= TIOCM_CTS;
2059	if (mstat & UART_MSR_DSR)
2060		result |= TIOCM_DSR;
2061	if (mstat & UART_MSR_RI)
2062		result |= TIOCM_RI;
2063	if (mstat & UART_MSR_DCD)
2064		result |= TIOCM_CD;
2065
2066	return result;
2067}
2068
2069
2070/*
2071 * dgnc_tty_tiocmset()
2072 *
2073 * Set modem signals, called by ld.
2074 */
2075
2076static int dgnc_tty_tiocmset(struct tty_struct *tty,
2077		unsigned int set, unsigned int clear)
2078{
2079	struct dgnc_board *bd;
2080	struct channel_t *ch;
2081	struct un_t *un;
2082	int ret = -EIO;
2083	unsigned long flags;
2084
2085	if (!tty || tty->magic != TTY_MAGIC)
2086		return ret;
2087
2088	un = tty->driver_data;
2089	if (!un || un->magic != DGNC_UNIT_MAGIC)
2090		return ret;
2091
2092	ch = un->un_ch;
2093	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2094		return ret;
2095
2096	bd = ch->ch_bd;
2097	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2098		return ret;
2099
2100	spin_lock_irqsave(&ch->ch_lock, flags);
2101
2102	if (set & TIOCM_RTS)
2103		ch->ch_mostat |= UART_MCR_RTS;
2104
2105	if (set & TIOCM_DTR)
2106		ch->ch_mostat |= UART_MCR_DTR;
2107
2108	if (clear & TIOCM_RTS)
2109		ch->ch_mostat &= ~(UART_MCR_RTS);
2110
2111	if (clear & TIOCM_DTR)
2112		ch->ch_mostat &= ~(UART_MCR_DTR);
2113
2114	ch->ch_bd->bd_ops->assert_modem_signals(ch);
2115
2116	spin_unlock_irqrestore(&ch->ch_lock, flags);
2117
2118	return 0;
2119}
2120
2121
2122/*
2123 * dgnc_tty_send_break()
2124 *
2125 * Send a Break, called by ld.
2126 */
2127static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2128{
2129	struct dgnc_board *bd;
2130	struct channel_t *ch;
2131	struct un_t *un;
2132	int ret = -EIO;
2133	unsigned long flags;
2134
2135	if (!tty || tty->magic != TTY_MAGIC)
2136		return ret;
2137
2138	un = tty->driver_data;
2139	if (!un || un->magic != DGNC_UNIT_MAGIC)
2140		return ret;
2141
2142	ch = un->un_ch;
2143	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2144		return ret;
2145
2146	bd = ch->ch_bd;
2147	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2148		return ret;
2149
2150	switch (msec) {
2151	case -1:
2152		msec = 0xFFFF;
2153		break;
2154	case 0:
2155		msec = 0;
2156		break;
2157	default:
2158		break;
2159	}
2160
2161	spin_lock_irqsave(&ch->ch_lock, flags);
2162
2163	ch->ch_bd->bd_ops->send_break(ch, msec);
2164
2165	spin_unlock_irqrestore(&ch->ch_lock, flags);
2166
2167	return 0;
2168
2169}
2170
2171
2172/*
2173 * dgnc_tty_wait_until_sent()
2174 *
2175 * wait until data has been transmitted, called by ld.
2176 */
2177static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2178{
2179	struct dgnc_board *bd;
2180	struct channel_t *ch;
2181	struct un_t *un;
2182	int rc;
2183
2184	if (!tty || tty->magic != TTY_MAGIC)
2185		return;
2186
2187	un = tty->driver_data;
2188	if (!un || un->magic != DGNC_UNIT_MAGIC)
2189		return;
2190
2191	ch = un->un_ch;
2192	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2193		return;
2194
2195	bd = ch->ch_bd;
2196	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2197		return;
2198
2199	rc = bd->bd_ops->drain(tty, 0);
2200}
2201
2202
2203/*
2204 * dgnc_send_xchar()
2205 *
2206 * send a high priority character, called by ld.
2207 */
2208static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2209{
2210	struct dgnc_board *bd;
2211	struct channel_t *ch;
2212	struct un_t *un;
2213	unsigned long flags;
2214
2215	if (!tty || tty->magic != TTY_MAGIC)
2216		return;
2217
2218	un = tty->driver_data;
2219	if (!un || un->magic != DGNC_UNIT_MAGIC)
2220		return;
2221
2222	ch = un->un_ch;
2223	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2224		return;
2225
2226	bd = ch->ch_bd;
2227	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2228		return;
2229
2230	dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2231
2232	spin_lock_irqsave(&ch->ch_lock, flags);
2233	bd->bd_ops->send_immediate_char(ch, c);
2234	spin_unlock_irqrestore(&ch->ch_lock, flags);
2235
2236	dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2237}
2238
2239
2240
2241
2242/*
2243 * Return modem signals to ld.
2244 */
2245static inline int dgnc_get_mstat(struct channel_t *ch)
2246{
2247	unsigned char mstat;
2248	int result = -EIO;
2249	unsigned long flags;
2250
2251	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2252		return -ENXIO;
2253
2254	spin_lock_irqsave(&ch->ch_lock, flags);
2255
2256	mstat = (ch->ch_mostat | ch->ch_mistat);
2257
2258	spin_unlock_irqrestore(&ch->ch_lock, flags);
2259
2260	result = 0;
2261
2262	if (mstat & UART_MCR_DTR)
2263		result |= TIOCM_DTR;
2264	if (mstat & UART_MCR_RTS)
2265		result |= TIOCM_RTS;
2266	if (mstat & UART_MSR_CTS)
2267		result |= TIOCM_CTS;
2268	if (mstat & UART_MSR_DSR)
2269		result |= TIOCM_DSR;
2270	if (mstat & UART_MSR_RI)
2271		result |= TIOCM_RI;
2272	if (mstat & UART_MSR_DCD)
2273		result |= TIOCM_CD;
2274
2275	return result;
2276}
2277
2278
2279
2280/*
2281 * Return modem signals to ld.
2282 */
2283static int dgnc_get_modem_info(struct channel_t *ch, unsigned int  __user *value)
2284{
2285	int result;
2286
2287	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2288		return -ENXIO;
2289
2290	result = dgnc_get_mstat(ch);
2291
2292	if (result < 0)
2293		return -ENXIO;
2294
2295	return put_user(result, value);
2296}
2297
2298
2299/*
2300 * dgnc_set_modem_info()
2301 *
2302 * Set modem signals, called by ld.
2303 */
2304static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2305{
2306	struct dgnc_board *bd;
2307	struct channel_t *ch;
2308	struct un_t *un;
2309	int ret = -ENXIO;
2310	unsigned int arg = 0;
2311	unsigned long flags;
2312
2313	if (!tty || tty->magic != TTY_MAGIC)
2314		return ret;
2315
2316	un = tty->driver_data;
2317	if (!un || un->magic != DGNC_UNIT_MAGIC)
2318		return ret;
2319
2320	ch = un->un_ch;
2321	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2322		return ret;
2323
2324	bd = ch->ch_bd;
2325	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2326		return ret;
2327
2328	ret = 0;
2329
2330	ret = get_user(arg, value);
2331	if (ret)
2332		return ret;
2333
2334	switch (command) {
2335	case TIOCMBIS:
2336		if (arg & TIOCM_RTS)
2337			ch->ch_mostat |= UART_MCR_RTS;
2338
2339		if (arg & TIOCM_DTR)
2340			ch->ch_mostat |= UART_MCR_DTR;
2341
2342		break;
2343
2344	case TIOCMBIC:
2345		if (arg & TIOCM_RTS)
2346			ch->ch_mostat &= ~(UART_MCR_RTS);
2347
2348		if (arg & TIOCM_DTR)
2349			ch->ch_mostat &= ~(UART_MCR_DTR);
2350
2351		break;
2352
2353	case TIOCMSET:
2354
2355		if (arg & TIOCM_RTS)
2356			ch->ch_mostat |= UART_MCR_RTS;
2357		else
2358			ch->ch_mostat &= ~(UART_MCR_RTS);
2359
2360		if (arg & TIOCM_DTR)
2361			ch->ch_mostat |= UART_MCR_DTR;
2362		else
2363			ch->ch_mostat &= ~(UART_MCR_DTR);
2364
2365		break;
2366
2367	default:
2368		return -EINVAL;
2369	}
2370
2371	spin_lock_irqsave(&ch->ch_lock, flags);
2372
2373	ch->ch_bd->bd_ops->assert_modem_signals(ch);
2374
2375	spin_unlock_irqrestore(&ch->ch_lock, flags);
2376
2377	return 0;
2378}
2379
2380
2381/*
2382 * dgnc_tty_digigeta()
2383 *
2384 * Ioctl to get the information for ditty.
2385 *
2386 *
2387 *
2388 */
2389static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2390{
2391	struct channel_t *ch;
2392	struct un_t *un;
2393	struct digi_t tmp;
2394	unsigned long flags;
2395
2396	if (!retinfo)
2397		return -EFAULT;
2398
2399	if (!tty || tty->magic != TTY_MAGIC)
2400		return -EFAULT;
2401
2402	un = tty->driver_data;
2403	if (!un || un->magic != DGNC_UNIT_MAGIC)
2404		return -EFAULT;
2405
2406	ch = un->un_ch;
2407	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2408		return -EFAULT;
2409
2410	memset(&tmp, 0, sizeof(tmp));
2411
2412	spin_lock_irqsave(&ch->ch_lock, flags);
2413	memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2414	spin_unlock_irqrestore(&ch->ch_lock, flags);
2415
2416	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2417		return -EFAULT;
2418
2419	return 0;
2420}
2421
2422
2423/*
2424 * dgnc_tty_digiseta()
2425 *
2426 * Ioctl to set the information for ditty.
2427 *
2428 *
2429 *
2430 */
2431static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2432{
2433	struct dgnc_board *bd;
2434	struct channel_t *ch;
2435	struct un_t *un;
2436	struct digi_t new_digi;
2437	unsigned long flags;
2438
2439	if (!tty || tty->magic != TTY_MAGIC)
2440		return -EFAULT;
2441
2442	un = tty->driver_data;
2443	if (!un || un->magic != DGNC_UNIT_MAGIC)
2444		return -EFAULT;
2445
2446	ch = un->un_ch;
2447	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2448		return -EFAULT;
2449
2450	bd = ch->ch_bd;
2451	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2452		return -EFAULT;
2453
2454	if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2455		return -EFAULT;
2456
2457	spin_lock_irqsave(&ch->ch_lock, flags);
2458
2459	/*
2460	 * Handle transistions to and from RTS Toggle.
2461	 */
2462	if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2463		ch->ch_mostat &= ~(UART_MCR_RTS);
2464	if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2465		ch->ch_mostat |= (UART_MCR_RTS);
2466
2467	/*
2468	 * Handle transistions to and from DTR Toggle.
2469	 */
2470	if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2471		ch->ch_mostat &= ~(UART_MCR_DTR);
2472	if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2473		ch->ch_mostat |= (UART_MCR_DTR);
2474
2475	memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2476
2477	if (ch->ch_digi.digi_maxcps < 1)
2478		ch->ch_digi.digi_maxcps = 1;
2479
2480	if (ch->ch_digi.digi_maxcps > 10000)
2481		ch->ch_digi.digi_maxcps = 10000;
2482
2483	if (ch->ch_digi.digi_bufsize < 10)
2484		ch->ch_digi.digi_bufsize = 10;
2485
2486	if (ch->ch_digi.digi_maxchar < 1)
2487		ch->ch_digi.digi_maxchar = 1;
2488
2489	if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2490		ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2491
2492	if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2493		ch->ch_digi.digi_onlen = DIGI_PLEN;
2494
2495	if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2496		ch->ch_digi.digi_offlen = DIGI_PLEN;
2497
2498	ch->ch_bd->bd_ops->param(tty);
2499
2500	spin_unlock_irqrestore(&ch->ch_lock, flags);
2501
2502	return 0;
2503}
2504
2505
2506/*
2507 * dgnc_set_termios()
2508 */
2509static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2510{
2511	struct dgnc_board *bd;
2512	struct channel_t *ch;
2513	struct un_t *un;
2514	unsigned long flags;
2515
2516	if (!tty || tty->magic != TTY_MAGIC)
2517		return;
2518
2519	un = tty->driver_data;
2520	if (!un || un->magic != DGNC_UNIT_MAGIC)
2521		return;
2522
2523	ch = un->un_ch;
2524	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2525		return;
2526
2527	bd = ch->ch_bd;
2528	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2529		return;
2530
2531	spin_lock_irqsave(&ch->ch_lock, flags);
2532
2533	ch->ch_c_cflag   = tty->termios.c_cflag;
2534	ch->ch_c_iflag   = tty->termios.c_iflag;
2535	ch->ch_c_oflag   = tty->termios.c_oflag;
2536	ch->ch_c_lflag   = tty->termios.c_lflag;
2537	ch->ch_startc = tty->termios.c_cc[VSTART];
2538	ch->ch_stopc  = tty->termios.c_cc[VSTOP];
2539
2540	ch->ch_bd->bd_ops->param(tty);
2541	dgnc_carrier(ch);
2542
2543	spin_unlock_irqrestore(&ch->ch_lock, flags);
2544}
2545
2546
2547static void dgnc_tty_throttle(struct tty_struct *tty)
2548{
2549	struct channel_t *ch;
2550	struct un_t *un;
2551	unsigned long flags;
2552
2553	if (!tty || tty->magic != TTY_MAGIC)
2554		return;
2555
2556	un = tty->driver_data;
2557	if (!un || un->magic != DGNC_UNIT_MAGIC)
2558		return;
2559
2560	ch = un->un_ch;
2561	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2562		return;
2563
2564	spin_lock_irqsave(&ch->ch_lock, flags);
2565
2566	ch->ch_flags |= (CH_FORCED_STOPI);
2567
2568	spin_unlock_irqrestore(&ch->ch_lock, flags);
2569}
2570
2571
2572static void dgnc_tty_unthrottle(struct tty_struct *tty)
2573{
2574	struct channel_t *ch;
2575	struct un_t *un;
2576	unsigned long flags;
2577
2578	if (!tty || tty->magic != TTY_MAGIC)
2579		return;
2580
2581	un = tty->driver_data;
2582	if (!un || un->magic != DGNC_UNIT_MAGIC)
2583		return;
2584
2585	ch = un->un_ch;
2586	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2587		return;
2588
2589	spin_lock_irqsave(&ch->ch_lock, flags);
2590
2591	ch->ch_flags &= ~(CH_FORCED_STOPI);
2592
2593	spin_unlock_irqrestore(&ch->ch_lock, flags);
2594}
2595
2596
2597static void dgnc_tty_start(struct tty_struct *tty)
2598{
2599	struct dgnc_board *bd;
2600	struct channel_t *ch;
2601	struct un_t *un;
2602	unsigned long flags;
2603
2604	if (!tty || tty->magic != TTY_MAGIC)
2605		return;
2606
2607	un = tty->driver_data;
2608	if (!un || un->magic != DGNC_UNIT_MAGIC)
2609		return;
2610
2611	ch = un->un_ch;
2612	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2613		return;
2614
2615	bd = ch->ch_bd;
2616	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2617		return;
2618
2619	spin_lock_irqsave(&ch->ch_lock, flags);
2620
2621	ch->ch_flags &= ~(CH_FORCED_STOP);
2622
2623	spin_unlock_irqrestore(&ch->ch_lock, flags);
2624}
2625
2626
2627static void dgnc_tty_stop(struct tty_struct *tty)
2628{
2629	struct dgnc_board *bd;
2630	struct channel_t *ch;
2631	struct un_t *un;
2632	unsigned long flags;
2633
2634	if (!tty || tty->magic != TTY_MAGIC)
2635		return;
2636
2637	un = tty->driver_data;
2638	if (!un || un->magic != DGNC_UNIT_MAGIC)
2639		return;
2640
2641	ch = un->un_ch;
2642	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2643		return;
2644
2645	bd = ch->ch_bd;
2646	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2647		return;
2648
2649	spin_lock_irqsave(&ch->ch_lock, flags);
2650
2651	ch->ch_flags |= (CH_FORCED_STOP);
2652
2653	spin_unlock_irqrestore(&ch->ch_lock, flags);
2654}
2655
2656
2657/*
2658 * dgnc_tty_flush_chars()
2659 *
2660 * Flush the cook buffer
2661 *
2662 * Note to self, and any other poor souls who venture here:
2663 *
2664 * flush in this case DOES NOT mean dispose of the data.
2665 * instead, it means "stop buffering and send it if you
2666 * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
2667 *
2668 * It is also always called in interrupt context - JAR 8-Sept-99
2669 */
2670static void dgnc_tty_flush_chars(struct tty_struct *tty)
2671{
2672	struct dgnc_board *bd;
2673	struct channel_t *ch;
2674	struct un_t *un;
2675	unsigned long flags;
2676
2677	if (!tty || tty->magic != TTY_MAGIC)
2678		return;
2679
2680	un = tty->driver_data;
2681	if (!un || un->magic != DGNC_UNIT_MAGIC)
2682		return;
2683
2684	ch = un->un_ch;
2685	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2686		return;
2687
2688	bd = ch->ch_bd;
2689	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2690		return;
2691
2692	spin_lock_irqsave(&ch->ch_lock, flags);
2693
2694	/* Do something maybe here */
2695
2696	spin_unlock_irqrestore(&ch->ch_lock, flags);
2697}
2698
2699
2700
2701/*
2702 * dgnc_tty_flush_buffer()
2703 *
2704 * Flush Tx buffer (make in == out)
2705 */
2706static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2707{
2708	struct channel_t *ch;
2709	struct un_t *un;
2710	unsigned long flags;
2711
2712	if (!tty || tty->magic != TTY_MAGIC)
2713		return;
2714
2715	un = tty->driver_data;
2716	if (!un || un->magic != DGNC_UNIT_MAGIC)
2717		return;
2718
2719	ch = un->un_ch;
2720	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2721		return;
2722
2723	spin_lock_irqsave(&ch->ch_lock, flags);
2724
2725	ch->ch_flags &= ~CH_STOP;
2726
2727	/* Flush our write queue */
2728	ch->ch_w_head = ch->ch_w_tail;
2729
2730	/* Flush UARTs transmit FIFO */
2731	ch->ch_bd->bd_ops->flush_uart_write(ch);
2732
2733	if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2734		ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2735		wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2736	}
2737	if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2738		ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2739		wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2740	}
2741
2742	spin_unlock_irqrestore(&ch->ch_lock, flags);
2743}
2744
2745
2746
2747/*****************************************************************************
2748 *
2749 * The IOCTL function and all of its helpers
2750 *
2751 *****************************************************************************/
2752
2753/*
2754 * dgnc_tty_ioctl()
2755 *
2756 * The usual assortment of ioctl's
2757 */
2758static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2759		unsigned long arg)
2760{
2761	struct dgnc_board *bd;
2762	struct channel_t *ch;
2763	struct un_t *un;
2764	int rc;
2765	unsigned long flags;
2766	void __user *uarg = (void __user *) arg;
2767
2768	if (!tty || tty->magic != TTY_MAGIC)
2769		return -ENODEV;
2770
2771	un = tty->driver_data;
2772	if (!un || un->magic != DGNC_UNIT_MAGIC)
2773		return -ENODEV;
2774
2775	ch = un->un_ch;
2776	if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2777		return -ENODEV;
2778
2779	bd = ch->ch_bd;
2780	if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2781		return -ENODEV;
2782
2783	spin_lock_irqsave(&ch->ch_lock, flags);
2784
2785	if (un->un_open_count <= 0) {
2786		spin_unlock_irqrestore(&ch->ch_lock, flags);
2787		return -EIO;
2788	}
2789
2790	switch (cmd) {
2791
2792	/* Here are all the standard ioctl's that we MUST implement */
2793
2794	case TCSBRK:
2795		/*
2796		 * TCSBRK is SVID version: non-zero arg --> no break
2797		 * this behaviour is exploited by tcdrain().
2798		 *
2799		 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2800		 * between 0.25 and 0.5 seconds so we'll ask for something
2801		 * in the middle: 0.375 seconds.
2802		 */
2803		rc = tty_check_change(tty);
2804		spin_unlock_irqrestore(&ch->ch_lock, flags);
2805		if (rc)
2806			return rc;
2807
2808		rc = ch->ch_bd->bd_ops->drain(tty, 0);
2809
2810		if (rc)
2811			return -EINTR;
2812
2813		spin_lock_irqsave(&ch->ch_lock, flags);
2814
2815		if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2816			ch->ch_bd->bd_ops->send_break(ch, 250);
2817
2818		spin_unlock_irqrestore(&ch->ch_lock, flags);
2819
2820		return 0;
2821
2822
2823	case TCSBRKP:
2824		/* support for POSIX tcsendbreak()
2825		 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2826		 * between 0.25 and 0.5 seconds so we'll ask for something
2827		 * in the middle: 0.375 seconds.
2828		 */
2829		rc = tty_check_change(tty);
2830		spin_unlock_irqrestore(&ch->ch_lock, flags);
2831		if (rc)
2832			return rc;
2833
2834		rc = ch->ch_bd->bd_ops->drain(tty, 0);
2835		if (rc)
2836			return -EINTR;
2837
2838		spin_lock_irqsave(&ch->ch_lock, flags);
2839
2840		ch->ch_bd->bd_ops->send_break(ch, 250);
2841
2842		spin_unlock_irqrestore(&ch->ch_lock, flags);
2843
2844		return 0;
2845
2846	case TIOCSBRK:
2847		rc = tty_check_change(tty);
2848		spin_unlock_irqrestore(&ch->ch_lock, flags);
2849		if (rc)
2850			return rc;
2851
2852		rc = ch->ch_bd->bd_ops->drain(tty, 0);
2853		if (rc)
2854			return -EINTR;
2855
2856		spin_lock_irqsave(&ch->ch_lock, flags);
2857
2858		ch->ch_bd->bd_ops->send_break(ch, 250);
2859
2860		spin_unlock_irqrestore(&ch->ch_lock, flags);
2861
2862		return 0;
2863
2864	case TIOCCBRK:
2865		/* Do Nothing */
2866		spin_unlock_irqrestore(&ch->ch_lock, flags);
2867		return 0;
2868
2869	case TIOCGSOFTCAR:
2870
2871		spin_unlock_irqrestore(&ch->ch_lock, flags);
2872
2873		rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
2874		return rc;
2875
2876	case TIOCSSOFTCAR:
2877
2878		spin_unlock_irqrestore(&ch->ch_lock, flags);
2879		rc = get_user(arg, (unsigned long __user *) arg);
2880		if (rc)
2881			return rc;
2882
2883		spin_lock_irqsave(&ch->ch_lock, flags);
2884		tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
2885		ch->ch_bd->bd_ops->param(tty);
2886		spin_unlock_irqrestore(&ch->ch_lock, flags);
2887
2888		return 0;
2889
2890	case TIOCMGET:
2891		spin_unlock_irqrestore(&ch->ch_lock, flags);
2892		return dgnc_get_modem_info(ch, uarg);
2893
2894	case TIOCMBIS:
2895	case TIOCMBIC:
2896	case TIOCMSET:
2897		spin_unlock_irqrestore(&ch->ch_lock, flags);
2898		return dgnc_set_modem_info(tty, cmd, uarg);
2899
2900		/*
2901		 * Here are any additional ioctl's that we want to implement
2902		 */
2903
2904	case TCFLSH:
2905		/*
2906		 * The linux tty driver doesn't have a flush
2907		 * input routine for the driver, assuming all backed
2908		 * up data is in the line disc. buffers.  However,
2909		 * we all know that's not the case.  Here, we
2910		 * act on the ioctl, but then lie and say we didn't
2911		 * so the line discipline will process the flush
2912		 * also.
2913		 */
2914		rc = tty_check_change(tty);
2915		if (rc) {
2916			spin_unlock_irqrestore(&ch->ch_lock, flags);
2917			return rc;
2918		}
2919
2920		if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2921			ch->ch_r_head = ch->ch_r_tail;
2922			ch->ch_bd->bd_ops->flush_uart_read(ch);
2923			/* Force queue flow control to be released, if needed */
2924			dgnc_check_queue_flow_control(ch);
2925		}
2926
2927		if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2928			if (!(un->un_type == DGNC_PRINT)) {
2929				ch->ch_w_head = ch->ch_w_tail;
2930				ch->ch_bd->bd_ops->flush_uart_write(ch);
2931
2932				if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2933					ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2934					wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2935				}
2936
2937				if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2938					ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2939					wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2940				}
2941
2942			}
2943		}
2944
2945		/* pretend we didn't recognize this IOCTL */
2946		spin_unlock_irqrestore(&ch->ch_lock, flags);
2947		return -ENOIOCTLCMD;
2948	case TCSETSF:
2949	case TCSETSW:
2950		/*
2951		 * The linux tty driver doesn't have a flush
2952		 * input routine for the driver, assuming all backed
2953		 * up data is in the line disc. buffers.  However,
2954		 * we all know that's not the case.  Here, we
2955		 * act on the ioctl, but then lie and say we didn't
2956		 * so the line discipline will process the flush
2957		 * also.
2958		 */
2959		if (cmd == TCSETSF) {
2960			/* flush rx */
2961			ch->ch_flags &= ~CH_STOP;
2962			ch->ch_r_head = ch->ch_r_tail;
2963			ch->ch_bd->bd_ops->flush_uart_read(ch);
2964			/* Force queue flow control to be released, if needed */
2965			dgnc_check_queue_flow_control(ch);
2966		}
2967
2968		/* now wait for all the output to drain */
2969		spin_unlock_irqrestore(&ch->ch_lock, flags);
2970		rc = ch->ch_bd->bd_ops->drain(tty, 0);
2971		if (rc)
2972			return -EINTR;
2973
2974		/* pretend we didn't recognize this */
2975		return -ENOIOCTLCMD;
2976
2977	case TCSETAW:
2978
2979		spin_unlock_irqrestore(&ch->ch_lock, flags);
2980		rc = ch->ch_bd->bd_ops->drain(tty, 0);
2981		if (rc)
2982			return -EINTR;
2983
2984		/* pretend we didn't recognize this */
2985		return -ENOIOCTLCMD;
2986
2987	case TCXONC:
2988		spin_unlock_irqrestore(&ch->ch_lock, flags);
2989		/* Make the ld do it */
2990		return -ENOIOCTLCMD;
2991
2992	case DIGI_GETA:
2993		/* get information for ditty */
2994		spin_unlock_irqrestore(&ch->ch_lock, flags);
2995		return dgnc_tty_digigeta(tty, uarg);
2996
2997	case DIGI_SETAW:
2998	case DIGI_SETAF:
2999
3000		/* set information for ditty */
3001		if (cmd == (DIGI_SETAW)) {
3002
3003			spin_unlock_irqrestore(&ch->ch_lock, flags);
3004			rc = ch->ch_bd->bd_ops->drain(tty, 0);
3005
3006			if (rc)
3007				return -EINTR;
3008
3009			spin_lock_irqsave(&ch->ch_lock, flags);
3010		} else {
3011			tty_ldisc_flush(tty);
3012		}
3013		/* fall thru */
3014
3015	case DIGI_SETA:
3016		spin_unlock_irqrestore(&ch->ch_lock, flags);
3017		return dgnc_tty_digiseta(tty, uarg);
3018
3019	case DIGI_LOOPBACK:
3020		{
3021			uint loopback = 0;
3022			/* Let go of locks when accessing user space, could sleep */
3023			spin_unlock_irqrestore(&ch->ch_lock, flags);
3024			rc = get_user(loopback, (unsigned int __user *) arg);
3025			if (rc)
3026				return rc;
3027			spin_lock_irqsave(&ch->ch_lock, flags);
3028
3029			/* Enable/disable internal loopback for this port */
3030			if (loopback)
3031				ch->ch_flags |= CH_LOOPBACK;
3032			else
3033				ch->ch_flags &= ~(CH_LOOPBACK);
3034
3035			ch->ch_bd->bd_ops->param(tty);
3036			spin_unlock_irqrestore(&ch->ch_lock, flags);
3037			return 0;
3038		}
3039
3040	case DIGI_GETCUSTOMBAUD:
3041		spin_unlock_irqrestore(&ch->ch_lock, flags);
3042		rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
3043		return rc;
3044
3045	case DIGI_SETCUSTOMBAUD:
3046	{
3047		int new_rate;
3048		/* Let go of locks when accessing user space, could sleep */
3049		spin_unlock_irqrestore(&ch->ch_lock, flags);
3050		rc = get_user(new_rate, (int __user *) arg);
3051		if (rc)
3052			return rc;
3053		spin_lock_irqsave(&ch->ch_lock, flags);
3054		dgnc_set_custom_speed(ch, new_rate);
3055		ch->ch_bd->bd_ops->param(tty);
3056		spin_unlock_irqrestore(&ch->ch_lock, flags);
3057		return 0;
3058	}
3059
3060	/*
3061	 * This ioctl allows insertion of a character into the front
3062	 * of any pending data to be transmitted.
3063	 *
3064	 * This ioctl is to satify the "Send Character Immediate"
3065	 * call that the RealPort protocol spec requires.
3066	 */
3067	case DIGI_REALPORT_SENDIMMEDIATE:
3068	{
3069		unsigned char c;
3070
3071		spin_unlock_irqrestore(&ch->ch_lock, flags);
3072		rc = get_user(c, (unsigned char __user *) arg);
3073		if (rc)
3074			return rc;
3075		spin_lock_irqsave(&ch->ch_lock, flags);
3076		ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3077		spin_unlock_irqrestore(&ch->ch_lock, flags);
3078		return 0;
3079	}
3080
3081	/*
3082	 * This ioctl returns all the current counts for the port.
3083	 *
3084	 * This ioctl is to satify the "Line Error Counters"
3085	 * call that the RealPort protocol spec requires.
3086	 */
3087	case DIGI_REALPORT_GETCOUNTERS:
3088	{
3089		struct digi_getcounter buf;
3090
3091		buf.norun = ch->ch_err_overrun;
3092		buf.noflow = 0;  	/* The driver doesn't keep this stat */
3093		buf.nframe = ch->ch_err_frame;
3094		buf.nparity = ch->ch_err_parity;
3095		buf.nbreak = ch->ch_err_break;
3096		buf.rbytes = ch->ch_rxcount;
3097		buf.tbytes = ch->ch_txcount;
3098
3099		spin_unlock_irqrestore(&ch->ch_lock, flags);
3100
3101		if (copy_to_user(uarg, &buf, sizeof(buf)))
3102			return -EFAULT;
3103
3104		return 0;
3105	}
3106
3107	/*
3108	 * This ioctl returns all current events.
3109	 *
3110	 * This ioctl is to satify the "Event Reporting"
3111	 * call that the RealPort protocol spec requires.
3112	 */
3113	case DIGI_REALPORT_GETEVENTS:
3114	{
3115		unsigned int events = 0;
3116
3117		/* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3118		if (ch->ch_flags & CH_BREAK_SENDING)
3119			events |= EV_TXB;
3120		if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
3121			events |= (EV_OPU | EV_OPS);
3122
3123		if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI))
3124			events |= (EV_IPU | EV_IPS);
3125
3126		spin_unlock_irqrestore(&ch->ch_lock, flags);
3127		rc = put_user(events, (unsigned int __user *) arg);
3128		return rc;
3129	}
3130
3131	/*
3132	 * This ioctl returns TOUT and TIN counters based
3133	 * upon the values passed in by the RealPort Server.
3134	 * It also passes back whether the UART Transmitter is
3135	 * empty as well.
3136	 */
3137	case DIGI_REALPORT_GETBUFFERS:
3138	{
3139		struct digi_getbuffer buf;
3140		int tdist;
3141		int count;
3142
3143		spin_unlock_irqrestore(&ch->ch_lock, flags);
3144
3145		/*
3146		 * Get data from user first.
3147		 */
3148		if (copy_from_user(&buf, uarg, sizeof(buf)))
3149			return -EFAULT;
3150
3151		spin_lock_irqsave(&ch->ch_lock, flags);
3152
3153		/*
3154		 * Figure out how much data is in our RX and TX queues.
3155		 */
3156		buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3157		buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3158
3159		/*
3160		 * Is the UART empty? Add that value to whats in our TX queue.
3161		 */
3162		count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3163
3164		/*
3165		 * Figure out how much data the RealPort Server believes should
3166		 * be in our TX queue.
3167		 */
3168		tdist = (buf.tIn - buf.tOut) & 0xffff;
3169
3170		/*
3171		 * If we have more data than the RealPort Server believes we
3172		 * should have, reduce our count to its amount.
3173		 *
3174		 * This count difference CAN happen because the Linux LD can
3175		 * insert more characters into our queue for OPOST processing
3176		 * that the RealPort Server doesn't know about.
3177		 */
3178		if (buf.txbuf > tdist)
3179			buf.txbuf = tdist;
3180
3181		/*
3182		 * Report whether our queue and UART TX are completely empty.
3183		 */
3184		if (count)
3185			buf.txdone = 0;
3186		else
3187			buf.txdone = 1;
3188
3189		spin_unlock_irqrestore(&ch->ch_lock, flags);
3190
3191		if (copy_to_user(uarg, &buf, sizeof(buf)))
3192			return -EFAULT;
3193
3194		return 0;
3195	}
3196	default:
3197		spin_unlock_irqrestore(&ch->ch_lock, flags);
3198
3199		return -ENOIOCTLCMD;
3200	}
3201}
3202