1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2003 Digi International (www.digi.com)
4  *	Scott H Kilau <Scott_Kilau at digi dot com>
5  */
6 
7 /*
8  * This file implements the tty driver functionality for the
9  * Neo and ClassicBoard PCI based product lines.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/sched/signal.h>	/* For jiffies, task states, etc. */
14 #include <linux/interrupt.h>	/* For tasklet and interrupt structs/defines */
15 #include <linux/module.h>
16 #include <linux/ctype.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/types.h>
20 #include <linux/serial_reg.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>	/* For udelay */
23 #include <linux/uaccess.h>	/* For copy_from_user/copy_to_user */
24 #include <linux/pci.h>
25 #include "dgnc_driver.h"
26 #include "dgnc_tty.h"
27 #include "dgnc_cls.h"
28 
29 /* Default transparent print information. */
30 
31 static const struct digi_t dgnc_digi_init = {
32 	.digi_flags =	DIGI_COOK,	/* Flags */
33 	.digi_maxcps =	100,		/* Max CPS */
34 	.digi_maxchar =	50,		/* Max chars in print queue */
35 	.digi_bufsize =	100,		/* Printer buffer size */
36 	.digi_onlen =	4,		/* size of printer on string */
37 	.digi_offlen =	4,		/* size of printer off string */
38 	.digi_onstr =	"\033[5i",	/* ANSI printer on string ] */
39 	.digi_offstr =	"\033[4i",	/* ANSI printer off string ] */
40 	.digi_term =	"ansi"		/* default terminal type */
41 };
42 
43 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
44 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
45 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
46 				struct channel_t *ch);
47 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
48 			  unsigned long arg);
49 static int dgnc_tty_digigeta(struct tty_struct *tty,
50 			     struct digi_t __user *retinfo);
51 static int dgnc_tty_digiseta(struct tty_struct *tty,
52 			     struct digi_t __user *new_info);
53 static int dgnc_tty_write_room(struct tty_struct *tty);
54 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
55 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
56 static void dgnc_tty_start(struct tty_struct *tty);
57 static void dgnc_tty_stop(struct tty_struct *tty);
58 static void dgnc_tty_throttle(struct tty_struct *tty);
59 static void dgnc_tty_unthrottle(struct tty_struct *tty);
60 static void dgnc_tty_flush_chars(struct tty_struct *tty);
61 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
62 static void dgnc_tty_hangup(struct tty_struct *tty);
63 static int dgnc_set_modem_info(struct channel_t *ch, unsigned int command,
64 			       unsigned int __user *value);
65 static int dgnc_get_modem_info(struct channel_t *ch,
66 			       unsigned int __user *value);
67 static int dgnc_tty_tiocmget(struct tty_struct *tty);
68 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
69 			     unsigned int clear);
70 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
71 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
72 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
73 			  int count);
74 static void dgnc_tty_set_termios(struct tty_struct *tty,
75 				 struct ktermios *old_termios);
76 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
77 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char line);
78 static void dgnc_wake_up_unit(struct un_t *unit);
79 
80 static const struct tty_operations dgnc_tty_ops = {
81 	.open = dgnc_tty_open,
82 	.close = dgnc_tty_close,
83 	.write = dgnc_tty_write,
84 	.write_room = dgnc_tty_write_room,
85 	.flush_buffer = dgnc_tty_flush_buffer,
86 	.chars_in_buffer = dgnc_tty_chars_in_buffer,
87 	.flush_chars = dgnc_tty_flush_chars,
88 	.ioctl = dgnc_tty_ioctl,
89 	.set_termios = dgnc_tty_set_termios,
90 	.stop = dgnc_tty_stop,
91 	.start = dgnc_tty_start,
92 	.throttle = dgnc_tty_throttle,
93 	.unthrottle = dgnc_tty_unthrottle,
94 	.hangup = dgnc_tty_hangup,
95 	.put_char = dgnc_tty_put_char,
96 	.tiocmget = dgnc_tty_tiocmget,
97 	.tiocmset = dgnc_tty_tiocmset,
98 	.break_ctl = dgnc_tty_send_break,
99 	.wait_until_sent = dgnc_tty_wait_until_sent,
100 	.send_xchar = dgnc_tty_send_xchar
101 };
102 
103 /* TTY Initialization/Cleanup Functions */
104 
dgnc_tty_create(char * serial_name,uint maxports,int major,int minor)105 static struct tty_driver *dgnc_tty_create(char *serial_name, uint maxports,
106 					  int major, int minor)
107 {
108 	int rc;
109 	struct tty_driver *drv;
110 
111 	drv = tty_alloc_driver(maxports,
112 			       TTY_DRIVER_REAL_RAW |
113 			       TTY_DRIVER_DYNAMIC_DEV |
114 			       TTY_DRIVER_HARDWARE_BREAK);
115 	if (IS_ERR(drv))
116 		return drv;
117 
118 	drv->name = serial_name;
119 	drv->name_base = 0;
120 	drv->major = major;
121 	drv->minor_start = minor;
122 	drv->type = TTY_DRIVER_TYPE_SERIAL;
123 	drv->subtype = SERIAL_TYPE_NORMAL;
124 	drv->init_termios = tty_std_termios;
125 	drv->init_termios.c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL);
126 	drv->init_termios.c_ispeed = 9600;
127 	drv->init_termios.c_ospeed = 9600;
128 	drv->driver_name = DRVSTR;
129 	/*
130 	 * Entry points for driver.  Called by the kernel from
131 	 * tty_io.c and n_tty.c.
132 	 */
133 	tty_set_operations(drv, &dgnc_tty_ops);
134 	rc = tty_register_driver(drv);
135 	if (rc < 0) {
136 		put_tty_driver(drv);
137 		return ERR_PTR(rc);
138 	}
139 	return drv;
140 }
141 
dgnc_tty_free(struct tty_driver * drv)142 static void dgnc_tty_free(struct tty_driver *drv)
143 {
144 	tty_unregister_driver(drv);
145 	put_tty_driver(drv);
146 }
147 
148 /**
149  * dgnc_tty_register() - Init the tty subsystem for this board.
150  */
dgnc_tty_register(struct dgnc_board * brd)151 int dgnc_tty_register(struct dgnc_board *brd)
152 {
153 	int rc;
154 
155 	snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgnc_%d_",
156 		 brd->boardnum);
157 
158 	brd->serial_driver = dgnc_tty_create(brd->serial_name,
159 					     brd->maxports, 0, 0);
160 	if (IS_ERR(brd->serial_driver)) {
161 		rc = PTR_ERR(brd->serial_driver);
162 		dev_dbg(&brd->pdev->dev, "Can't register tty device (%d)\n",
163 			rc);
164 		return rc;
165 	}
166 
167 	snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
168 	brd->print_driver = dgnc_tty_create(brd->print_name, brd->maxports,
169 					    0x80,
170 					    brd->serial_driver->major);
171 	if (IS_ERR(brd->print_driver)) {
172 		rc = PTR_ERR(brd->print_driver);
173 		dev_dbg(&brd->pdev->dev,
174 			"Can't register Transparent Print device(%d)\n", rc);
175 		dgnc_tty_free(brd->serial_driver);
176 		return rc;
177 	}
178 	return 0;
179 }
180 
dgnc_tty_unregister(struct dgnc_board * brd)181 void dgnc_tty_unregister(struct dgnc_board *brd)
182 {
183 	dgnc_tty_free(brd->print_driver);
184 	dgnc_tty_free(brd->serial_driver);
185 }
186 
187 /**
188  * dgnc_tty_init() - Initialize the tty subsystem.
189  *
190  * Called once per board after board has been downloaded and initialized.
191  */
dgnc_tty_init(struct dgnc_board * brd)192 int dgnc_tty_init(struct dgnc_board *brd)
193 {
194 	int i;
195 	int rc;
196 	void __iomem *vaddr;
197 	struct channel_t *ch;
198 
199 	if (!brd)
200 		return -ENXIO;
201 
202 	/* Initialize board structure elements. */
203 
204 	vaddr = brd->re_map_membase;
205 
206 	brd->nasync = brd->maxports;
207 
208 	for (i = 0; i < brd->nasync; i++) {
209 		brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
210 					   GFP_KERNEL);
211 		if (!brd->channels[i]) {
212 			rc = -ENOMEM;
213 			goto err_free_channels;
214 		}
215 	}
216 
217 	ch = brd->channels[0];
218 	vaddr = brd->re_map_membase;
219 
220 	/* Set up channel variables */
221 	for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
222 		spin_lock_init(&ch->ch_lock);
223 
224 		ch->ch_tun.un_ch = ch;
225 		ch->ch_tun.un_type = DGNC_SERIAL;
226 		ch->ch_tun.un_dev = i;
227 
228 		ch->ch_pun.un_ch = ch;
229 		ch->ch_pun.un_type = DGNC_PRINT;
230 		ch->ch_pun.un_dev = i + 128;
231 
232 		ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
233 
234 		ch->ch_bd = brd;
235 		ch->ch_portnum = i;
236 		ch->ch_digi = dgnc_digi_init;
237 
238 		/* .25 second delay */
239 		ch->ch_close_delay = 250;
240 
241 		init_waitqueue_head(&ch->ch_flags_wait);
242 		init_waitqueue_head(&ch->ch_tun.un_flags_wait);
243 		init_waitqueue_head(&ch->ch_pun.un_flags_wait);
244 
245 		{
246 			struct device *classp;
247 
248 			classp = tty_register_device(brd->serial_driver, i,
249 						     &ch->ch_bd->pdev->dev);
250 			ch->ch_tun.un_sysfs = classp;
251 
252 			classp = tty_register_device(brd->print_driver, i,
253 						     &ch->ch_bd->pdev->dev);
254 			ch->ch_pun.un_sysfs = classp;
255 		}
256 	}
257 
258 	return 0;
259 
260 err_free_channels:
261 	for (i = i - 1; i >= 0; --i) {
262 		kfree(brd->channels[i]);
263 		brd->channels[i] = NULL;
264 	}
265 
266 	return rc;
267 }
268 
269 /**
270  * dgnc_cleanup_tty() - Cleanup driver.
271  *
272  * Uninitialize the TTY portion of this driver.  Free all memory and
273  * resources.
274  */
dgnc_cleanup_tty(struct dgnc_board * brd)275 void dgnc_cleanup_tty(struct dgnc_board *brd)
276 {
277 	int i = 0;
278 
279 	for (i = 0; i < brd->nasync; i++)
280 		tty_unregister_device(brd->serial_driver, i);
281 
282 	tty_unregister_driver(brd->serial_driver);
283 
284 	for (i = 0; i < brd->nasync; i++)
285 		tty_unregister_device(brd->print_driver, i);
286 
287 	tty_unregister_driver(brd->print_driver);
288 
289 	put_tty_driver(brd->serial_driver);
290 	put_tty_driver(brd->print_driver);
291 }
292 
293 /**
294  * dgnc_wmove() - Write data to transmit queue.
295  * @ch: Pointer to channel structure.
296  * @buf: Pointer to characters to be moved.
297  * @n: Number of characters to move.
298  */
dgnc_wmove(struct channel_t * ch,char * buf,uint n)299 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
300 {
301 	int	remain;
302 	uint	head;
303 
304 	if (!ch)
305 		return;
306 
307 	head = ch->ch_w_head & WQUEUEMASK;
308 
309 	/*
310 	 * If the write wraps over the top of the circular buffer,
311 	 * move the portion up to the wrap point, and reset the
312 	 * pointers to the bottom.
313 	 */
314 	remain = WQUEUESIZE - head;
315 
316 	if (n >= remain) {
317 		n -= remain;
318 		memcpy(ch->ch_wqueue + head, buf, remain);
319 		head = 0;
320 		buf += remain;
321 	}
322 
323 	if (n > 0) {
324 		/* Move rest of data. */
325 		remain = n;
326 		memcpy(ch->ch_wqueue + head, buf, remain);
327 		head += remain;
328 	}
329 
330 	head &= WQUEUEMASK;
331 	ch->ch_w_head = head;
332 }
333 
334 /**
335  * dgnc_input() - Process received data.
336  * @ch: Pointer to channel structure.
337  */
dgnc_input(struct channel_t * ch)338 void dgnc_input(struct channel_t *ch)
339 {
340 	struct dgnc_board *bd;
341 	struct tty_struct *tp;
342 	struct tty_ldisc *ld = NULL;
343 	uint	rmask;
344 	ushort	head;
345 	ushort	tail;
346 	int	data_len;
347 	unsigned long flags;
348 	int flip_len;
349 	int len = 0;
350 	int n = 0;
351 	int s = 0;
352 	int i = 0;
353 
354 	if (!ch)
355 		return;
356 
357 	tp = ch->ch_tun.un_tty;
358 
359 	bd = ch->ch_bd;
360 	if (!bd)
361 		return;
362 
363 	spin_lock_irqsave(&ch->ch_lock, flags);
364 
365 	rmask = RQUEUEMASK;
366 	head = ch->ch_r_head & rmask;
367 	tail = ch->ch_r_tail & rmask;
368 	data_len = (head - tail) & rmask;
369 
370 	if (data_len == 0)
371 		goto exit_unlock;
372 
373 	/*
374 	 * If the device is not open, or CREAD is off,
375 	 * flush input data and return immediately.
376 	 */
377 	if (!tp ||
378 	    !(ch->ch_tun.un_flags & UN_ISOPEN) ||
379 	    !C_CREAD(tp) ||
380 	    (ch->ch_tun.un_flags & UN_CLOSING)) {
381 		ch->ch_r_head = tail;
382 
383 		/* Force queue flow control to be released, if needed */
384 		dgnc_check_queue_flow_control(ch);
385 
386 		goto exit_unlock;
387 	}
388 
389 	if (ch->ch_flags & CH_FORCED_STOPI)
390 		goto exit_unlock;
391 
392 	flip_len = TTY_FLIPBUF_SIZE;
393 
394 	len = min(data_len, flip_len);
395 	len = min(len, (N_TTY_BUF_SIZE - 1));
396 
397 	ld = tty_ldisc_ref(tp);
398 	if (!ld) {
399 		len = 0;
400 	} else {
401 		if (!ld->ops->receive_buf) {
402 			ch->ch_r_head = ch->ch_r_tail;
403 			len = 0;
404 		}
405 	}
406 
407 	if (len <= 0)
408 		goto exit_unlock;
409 
410 	/*
411 	 * The tty layer in the kernel has changed in 2.6.16+.
412 	 *
413 	 * The flip buffers in the tty structure are no longer exposed,
414 	 * and probably will be going away eventually.
415 	 *
416 	 * If we are completely raw, we don't need to go through a lot
417 	 * of the tty layers that exist.
418 	 * In this case, we take the shortest and fastest route we
419 	 * can to relay the data to the user.
420 	 *
421 	 * On the other hand, if we are not raw, we need to go through
422 	 * the new 2.6.16+ tty layer, which has its API more well defined.
423 	 */
424 	len = tty_buffer_request_room(tp->port, len);
425 	n = len;
426 
427 	/*
428 	 * n now contains the most amount of data we can copy,
429 	 * bounded either by how much the Linux tty layer can handle,
430 	 * or the amount of data the card actually has pending...
431 	 */
432 	while (n) {
433 		unsigned char *ch_pos = ch->ch_equeue + tail;
434 
435 		s = ((head >= tail) ? head : RQUEUESIZE) - tail;
436 		s = min(s, n);
437 
438 		if (s <= 0)
439 			break;
440 
441 		/*
442 		 * If conditions are such that ld needs to see all
443 		 * UART errors, we will have to walk each character
444 		 * and error byte and send them to the buffer one at
445 		 * a time.
446 		 */
447 		if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
448 			for (i = 0; i < s; i++) {
449 				unsigned char ch = *(ch_pos + i);
450 				char flag = TTY_NORMAL;
451 
452 				if (ch & UART_LSR_BI)
453 					flag = TTY_BREAK;
454 				else if (ch & UART_LSR_PE)
455 					flag = TTY_PARITY;
456 				else if (ch & UART_LSR_FE)
457 					flag = TTY_FRAME;
458 
459 				tty_insert_flip_char(tp->port, ch, flag);
460 			}
461 		} else {
462 			tty_insert_flip_string(tp->port, ch_pos, s);
463 		}
464 
465 		tail += s;
466 		n -= s;
467 		/* Flip queue if needed */
468 		tail &= rmask;
469 	}
470 
471 	ch->ch_r_tail = tail & rmask;
472 	ch->ch_e_tail = tail & rmask;
473 	dgnc_check_queue_flow_control(ch);
474 	spin_unlock_irqrestore(&ch->ch_lock, flags);
475 
476 	/* Tell the tty layer its okay to "eat" the data now */
477 	tty_flip_buffer_push(tp->port);
478 
479 	if (ld)
480 		tty_ldisc_deref(ld);
481 	return;
482 
483 exit_unlock:
484 	spin_unlock_irqrestore(&ch->ch_lock, flags);
485 	if (ld)
486 		tty_ldisc_deref(ld);
487 }
488 
489 /**
490  * dgnc_carrier()
491  *
492  * Determines when CARRIER changes state and takes appropriate
493  * action.
494  */
dgnc_carrier(struct channel_t * ch)495 void dgnc_carrier(struct channel_t *ch)
496 {
497 	int virt_carrier = 0;
498 	int phys_carrier = 0;
499 
500 	if (!ch)
501 		return;
502 
503 	if (ch->ch_mistat & UART_MSR_DCD)
504 		phys_carrier = 1;
505 
506 	if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
507 		virt_carrier = 1;
508 
509 	if (ch->ch_c_cflag & CLOCAL)
510 		virt_carrier = 1;
511 
512 	/* Test for a VIRTUAL carrier transition to HIGH. */
513 
514 	if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
515 		/*
516 		 * When carrier rises, wake any threads waiting
517 		 * for carrier in the open routine.
518 		 */
519 		if (waitqueue_active(&ch->ch_flags_wait))
520 			wake_up_interruptible(&ch->ch_flags_wait);
521 	}
522 
523 	/* Test for a PHYSICAL carrier transition to HIGH. */
524 
525 	if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
526 		/*
527 		 * When carrier rises, wake any threads waiting
528 		 * for carrier in the open routine.
529 		 */
530 		if (waitqueue_active(&ch->ch_flags_wait))
531 			wake_up_interruptible(&ch->ch_flags_wait);
532 	}
533 
534 	/*
535 	 *  Test for a PHYSICAL transition to low, so long as we aren't
536 	 *  currently ignoring physical transitions (which is what "virtual
537 	 *  carrier" indicates).
538 	 *
539 	 *  The transition of the virtual carrier to low really doesn't
540 	 *  matter... it really only means "ignore carrier state", not
541 	 *  "make pretend that carrier is there".
542 	 */
543 	if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
544 	    (phys_carrier == 0)) {
545 		/*
546 		 *   When carrier drops:
547 		 *
548 		 *   Drop carrier on all open units.
549 		 *
550 		 *   Flush queues, waking up any task waiting in the
551 		 *   line discipline.
552 		 *
553 		 *   Send a hangup to the control terminal.
554 		 *
555 		 *   Enable all select calls.
556 		 */
557 		if (waitqueue_active(&ch->ch_flags_wait))
558 			wake_up_interruptible(&ch->ch_flags_wait);
559 
560 		if (ch->ch_tun.un_open_count > 0)
561 			tty_hangup(ch->ch_tun.un_tty);
562 
563 		if (ch->ch_pun.un_open_count > 0)
564 			tty_hangup(ch->ch_pun.un_tty);
565 	}
566 
567 	/*  Make sure that our cached values reflect the current reality. */
568 
569 	if (virt_carrier == 1)
570 		ch->ch_flags |= CH_FCAR;
571 	else
572 		ch->ch_flags &= ~CH_FCAR;
573 
574 	if (phys_carrier == 1)
575 		ch->ch_flags |= CH_CD;
576 	else
577 		ch->ch_flags &= ~CH_CD;
578 }
579 
580 /*  Assign the custom baud rate to the channel structure */
dgnc_set_custom_speed(struct channel_t * ch,uint newrate)581 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
582 {
583 	int testdiv;
584 	int testrate_high;
585 	int testrate_low;
586 	int deltahigh;
587 	int deltalow;
588 
589 	if (newrate <= 0) {
590 		ch->ch_custom_speed = 0;
591 		return;
592 	}
593 
594 	/*
595 	 *  Since the divisor is stored in a 16-bit integer, we make sure
596 	 *  we don't allow any rates smaller than a 16-bit integer would allow.
597 	 *  And of course, rates above the dividend won't fly.
598 	 */
599 	if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
600 		newrate = (ch->ch_bd->bd_dividend / 0xFFFF) + 1;
601 
602 	if (newrate && newrate > ch->ch_bd->bd_dividend)
603 		newrate = ch->ch_bd->bd_dividend;
604 
605 	if (newrate > 0) {
606 		testdiv = ch->ch_bd->bd_dividend / newrate;
607 
608 		/*
609 		 *  If we try to figure out what rate the board would use
610 		 *  with the test divisor, it will be either equal or higher
611 		 *  than the requested baud rate.  If we then determine the
612 		 *  rate with a divisor one higher, we will get the next lower
613 		 *  supported rate below the requested.
614 		 */
615 		testrate_high = ch->ch_bd->bd_dividend / testdiv;
616 		testrate_low  = ch->ch_bd->bd_dividend / (testdiv + 1);
617 
618 		/*
619 		 *  If the rate for the requested divisor is correct, just
620 		 *  use it and be done.
621 		 */
622 		if (testrate_high != newrate) {
623 			/*
624 			 *  Otherwise, pick the rate that is closer
625 			 *  (i.e. whichever rate has a smaller delta).
626 			 */
627 			deltahigh = testrate_high - newrate;
628 			deltalow = newrate - testrate_low;
629 
630 			if (deltahigh < deltalow)
631 				newrate = testrate_high;
632 			else
633 				newrate = testrate_low;
634 		}
635 	}
636 
637 	ch->ch_custom_speed = newrate;
638 }
639 
dgnc_check_queue_flow_control(struct channel_t * ch)640 void dgnc_check_queue_flow_control(struct channel_t *ch)
641 {
642 	int qleft;
643 
644 	qleft = ch->ch_r_tail - ch->ch_r_head - 1;
645 	if (qleft < 0)
646 		qleft += RQUEUEMASK + 1;
647 
648 	/*
649 	 * Check to see if we should enforce flow control on our queue because
650 	 * the ld (or user) isn't reading data out of our queue fast enuf.
651 	 *
652 	 * NOTE: This is done based on what the current flow control of the
653 	 * port is set for.
654 	 *
655 	 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
656 	 *	This will cause the UART's FIFO to back up, and force
657 	 *	the RTS signal to be dropped.
658 	 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
659 	 *	the other side, in hopes it will stop sending data to us.
660 	 * 3) NONE - Nothing we can do.  We will simply drop any extra data
661 	 *	that gets sent into us when the queue fills up.
662 	 */
663 	if (qleft < 256) {
664 		/* HWFLOW */
665 		if (ch->ch_digi.digi_flags & CTSPACE ||
666 		    ch->ch_c_cflag & CRTSCTS) {
667 			if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
668 				ch->ch_bd->bd_ops->disable_receiver(ch);
669 				ch->ch_flags |= (CH_RECEIVER_OFF);
670 			}
671 		}
672 		/* SWFLOW */
673 		else if (ch->ch_c_iflag & IXOFF) {
674 			if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
675 				ch->ch_bd->bd_ops->send_stop_character(ch);
676 				ch->ch_stops_sent++;
677 			}
678 		}
679 	}
680 
681 	/*
682 	 * Check to see if we should unenforce flow control because
683 	 * ld (or user) finally read enuf data out of our queue.
684 	 *
685 	 * NOTE: This is done based on what the current flow control of the
686 	 * port is set for.
687 	 *
688 	 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
689 	 *	This will cause the UART's FIFO to raise RTS back up,
690 	 *	which will allow the other side to start sending data again.
691 	 * 2) SWFLOW (IXOFF) - Send a start character to
692 	 *	the other side, so it will start sending data to us again.
693 	 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
694 	 *	other side, we don't need to do anything now.
695 	 */
696 	if (qleft > (RQUEUESIZE / 2)) {
697 		/* HWFLOW */
698 		if (ch->ch_digi.digi_flags & RTSPACE ||
699 		    ch->ch_c_cflag & CRTSCTS) {
700 			if (ch->ch_flags & CH_RECEIVER_OFF) {
701 				ch->ch_bd->bd_ops->enable_receiver(ch);
702 				ch->ch_flags &= ~(CH_RECEIVER_OFF);
703 			}
704 		}
705 		/* SWFLOW */
706 		else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
707 			ch->ch_stops_sent = 0;
708 			ch->ch_bd->bd_ops->send_start_character(ch);
709 		}
710 	}
711 }
712 
dgnc_set_signal_low(struct channel_t * ch,const unsigned char sig)713 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char sig)
714 {
715 	ch->ch_mostat &= ~(sig);
716 	ch->ch_bd->bd_ops->assert_modem_signals(ch);
717 }
718 
dgnc_wakeup_writes(struct channel_t * ch)719 void dgnc_wakeup_writes(struct channel_t *ch)
720 {
721 	int qlen = 0;
722 	unsigned long flags;
723 
724 	if (!ch)
725 		return;
726 
727 	spin_lock_irqsave(&ch->ch_lock, flags);
728 
729 	/* If channel now has space, wake up anyone waiting on the condition. */
730 
731 	qlen = ch->ch_w_head - ch->ch_w_tail;
732 	if (qlen < 0)
733 		qlen += WQUEUESIZE;
734 
735 	if (qlen >= (WQUEUESIZE - 256)) {
736 		spin_unlock_irqrestore(&ch->ch_lock, flags);
737 		return;
738 	}
739 
740 	if (ch->ch_tun.un_flags & UN_ISOPEN) {
741 		tty_wakeup(ch->ch_tun.un_tty);
742 
743 		/*
744 		 * If unit is set to wait until empty, check to make sure
745 		 * the queue AND FIFO are both empty.
746 		 */
747 		if (ch->ch_tun.un_flags & UN_EMPTY) {
748 			if ((qlen == 0) &&
749 			    (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
750 				ch->ch_tun.un_flags &= ~(UN_EMPTY);
751 
752 				/*
753 				 * If RTS Toggle mode is on, whenever
754 				 * the queue and UART is empty, keep RTS low.
755 				 */
756 				if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
757 					dgnc_set_signal_low(ch, UART_MCR_RTS);
758 
759 				/*
760 				 * If DTR Toggle mode is on, whenever
761 				 * the queue and UART is empty, keep DTR low.
762 				 */
763 				if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
764 					dgnc_set_signal_low(ch, UART_MCR_DTR);
765 			}
766 		}
767 
768 		wake_up_interruptible(&ch->ch_tun.un_flags_wait);
769 	}
770 
771 	if (ch->ch_pun.un_flags & UN_ISOPEN) {
772 		tty_wakeup(ch->ch_pun.un_tty);
773 
774 		/*
775 		 * If unit is set to wait until empty, check to make sure
776 		 * the queue AND FIFO are both empty.
777 		 */
778 		if (ch->ch_pun.un_flags & UN_EMPTY) {
779 			if ((qlen == 0) &&
780 			    (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
781 				ch->ch_pun.un_flags &= ~(UN_EMPTY);
782 		}
783 
784 		wake_up_interruptible(&ch->ch_pun.un_flags_wait);
785 	}
786 
787 	spin_unlock_irqrestore(&ch->ch_lock, flags);
788 }
789 
find_board_by_major(unsigned int major)790 static struct dgnc_board *find_board_by_major(unsigned int major)
791 {
792 	int i;
793 
794 	for (i = 0; i < MAXBOARDS; i++) {
795 		struct dgnc_board *brd = dgnc_board[i];
796 
797 		if (!brd)
798 			return NULL;
799 
800 		if (major == brd->serial_driver->major ||
801 		    major == brd->print_driver->major)
802 			return brd;
803 	}
804 
805 	return NULL;
806 }
807 
808 /* TTY Entry points and helper functions */
809 
dgnc_tty_open(struct tty_struct * tty,struct file * file)810 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
811 {
812 	struct dgnc_board	*brd;
813 	struct channel_t *ch;
814 	struct un_t	*un;
815 	uint		major = 0;
816 	uint		minor = 0;
817 	int		rc = 0;
818 	unsigned long flags;
819 
820 	rc = 0;
821 
822 	major = MAJOR(tty_devnum(tty));
823 	minor = MINOR(tty_devnum(tty));
824 
825 	if (major > 255)
826 		return -ENXIO;
827 
828 	brd = find_board_by_major(major);
829 	if (!brd)
830 		return -ENXIO;
831 
832 	rc = wait_event_interruptible(brd->state_wait,
833 				      (brd->state & BOARD_READY));
834 	if (rc)
835 		return rc;
836 
837 	spin_lock_irqsave(&brd->bd_lock, flags);
838 
839 	if (PORT_NUM(minor) >= brd->nasync) {
840 		rc = -ENXIO;
841 		goto err_brd_unlock;
842 	}
843 
844 	ch = brd->channels[PORT_NUM(minor)];
845 	if (!ch) {
846 		rc = -ENXIO;
847 		goto err_brd_unlock;
848 	}
849 
850 	spin_unlock_irqrestore(&brd->bd_lock, flags);
851 
852 	spin_lock_irqsave(&ch->ch_lock, flags);
853 
854 	/* Figure out our type */
855 	if (!IS_PRINT(minor)) {
856 		un = &brd->channels[PORT_NUM(minor)]->ch_tun;
857 		un->un_type = DGNC_SERIAL;
858 	} else if (IS_PRINT(minor)) {
859 		un = &brd->channels[PORT_NUM(minor)]->ch_pun;
860 		un->un_type = DGNC_PRINT;
861 	} else {
862 		rc = -ENXIO;
863 		goto err_ch_unlock;
864 	}
865 
866 	/*
867 	 * If the port is still in a previous open, and in a state
868 	 * where we simply cannot safely keep going, wait until the
869 	 * state clears.
870 	 */
871 	spin_unlock_irqrestore(&ch->ch_lock, flags);
872 
873 	rc = wait_event_interruptible(ch->ch_flags_wait,
874 				      ((ch->ch_flags & CH_OPENING) == 0));
875 	/* If ret is non-zero, user ctrl-c'ed us */
876 	if (rc)
877 		return -EINTR;
878 
879 	/*
880 	 * If either unit is in the middle of the fragile part of close,
881 	 * we just cannot touch the channel safely.
882 	 * Go to sleep, knowing that when the channel can be
883 	 * touched safely, the close routine will signal the
884 	 * ch_flags_wait to wake us back up.
885 	 */
886 	rc = wait_event_interruptible(ch->ch_flags_wait,
887 				      !((ch->ch_tun.un_flags |
888 				      ch->ch_pun.un_flags) & UN_CLOSING));
889 	/* If ret is non-zero, user ctrl-c'ed us */
890 	if (rc)
891 		return -EINTR;
892 
893 	spin_lock_irqsave(&ch->ch_lock, flags);
894 
895 	tty->driver_data = un;
896 
897 	/* Initialize tty's */
898 
899 	if (!(un->un_flags & UN_ISOPEN)) {
900 		un->un_tty = tty;
901 
902 		/* Maybe do something here to the TTY struct as well? */
903 	}
904 
905 	/*
906 	 * Allocate channel buffers for read/write/error.
907 	 * Set flag, so we don't get trounced on.
908 	 */
909 	ch->ch_flags |= (CH_OPENING);
910 
911 	spin_unlock_irqrestore(&ch->ch_lock, flags);
912 
913 	if (!ch->ch_rqueue)
914 		ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
915 	if (!ch->ch_equeue)
916 		ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
917 	if (!ch->ch_wqueue)
918 		ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
919 
920 	if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
921 		kfree(ch->ch_rqueue);
922 		kfree(ch->ch_equeue);
923 		kfree(ch->ch_wqueue);
924 		return -ENOMEM;
925 	}
926 
927 	spin_lock_irqsave(&ch->ch_lock, flags);
928 
929 	ch->ch_flags &= ~(CH_OPENING);
930 	wake_up_interruptible(&ch->ch_flags_wait);
931 
932 	/* Initialize if neither terminal or printer is open. */
933 
934 	if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
935 		/* Flush input queues. */
936 		ch->ch_r_head = 0;
937 		ch->ch_r_tail = 0;
938 		ch->ch_e_head = 0;
939 		ch->ch_e_tail = 0;
940 		ch->ch_w_head = 0;
941 		ch->ch_w_tail = 0;
942 
943 		brd->bd_ops->flush_uart_write(ch);
944 		brd->bd_ops->flush_uart_read(ch);
945 
946 		ch->ch_flags = 0;
947 		ch->ch_cached_lsr = 0;
948 		ch->ch_stop_sending_break = 0;
949 		ch->ch_stops_sent = 0;
950 
951 		ch->ch_c_cflag   = tty->termios.c_cflag;
952 		ch->ch_c_iflag   = tty->termios.c_iflag;
953 		ch->ch_c_oflag   = tty->termios.c_oflag;
954 		ch->ch_c_lflag   = tty->termios.c_lflag;
955 		ch->ch_startc = tty->termios.c_cc[VSTART];
956 		ch->ch_stopc  = tty->termios.c_cc[VSTOP];
957 
958 		/*
959 		 * Bring up RTS and DTR...
960 		 * Also handle RTS or DTR toggle if set.
961 		 */
962 		if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
963 			ch->ch_mostat |= (UART_MCR_RTS);
964 		if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
965 			ch->ch_mostat |= (UART_MCR_DTR);
966 
967 		/* Tell UART to init itself */
968 		brd->bd_ops->uart_init(ch);
969 	}
970 
971 	brd->bd_ops->param(tty);
972 
973 	dgnc_carrier(ch);
974 
975 	spin_unlock_irqrestore(&ch->ch_lock, flags);
976 
977 	rc = dgnc_block_til_ready(tty, file, ch);
978 
979 	spin_lock_irqsave(&ch->ch_lock, flags);
980 	ch->ch_open_count++;
981 	un->un_open_count++;
982 	un->un_flags |= (UN_ISOPEN);
983 	spin_unlock_irqrestore(&ch->ch_lock, flags);
984 
985 	return rc;
986 
987 err_brd_unlock:
988 	spin_unlock_irqrestore(&brd->bd_lock, flags);
989 
990 	return rc;
991 err_ch_unlock:
992 	spin_unlock_irqrestore(&ch->ch_lock, flags);
993 
994 	return rc;
995 }
996 
997 /* Wait for DCD, if needed. */
dgnc_block_til_ready(struct tty_struct * tty,struct file * file,struct channel_t * ch)998 static int dgnc_block_til_ready(struct tty_struct *tty,
999 				struct file *file,
1000 				struct channel_t *ch)
1001 {
1002 	int rc = 0;
1003 	struct un_t *un = tty->driver_data;
1004 	unsigned long flags;
1005 	uint	old_flags = 0;
1006 	int	sleep_on_un_flags = 0;
1007 
1008 	if (!file)
1009 		return -ENXIO;
1010 
1011 	spin_lock_irqsave(&ch->ch_lock, flags);
1012 
1013 	ch->ch_wopen++;
1014 
1015 	while (1) {
1016 		sleep_on_un_flags = 0;
1017 
1018 		if (ch->ch_bd->state == BOARD_FAILED) {
1019 			rc = -ENXIO;
1020 			break;
1021 		}
1022 
1023 		if (tty_hung_up_p(file)) {
1024 			rc = -EAGAIN;
1025 			break;
1026 		}
1027 
1028 		/*
1029 		 * If either unit is in the middle of the fragile part of close,
1030 		 * we just cannot touch the channel safely.
1031 		 * Go back to sleep, knowing that when the channel can be
1032 		 * touched safely, the close routine will signal the
1033 		 * ch_wait_flags to wake us back up.
1034 		 */
1035 		if (!((ch->ch_tun.un_flags |
1036 		    ch->ch_pun.un_flags) &
1037 		    UN_CLOSING)) {
1038 			/*
1039 			 * Our conditions to leave cleanly and happily:
1040 			 * 1) NONBLOCKING on the tty is set.
1041 			 * 2) CLOCAL is set.
1042 			 * 3) DCD (fake or real) is active.
1043 			 */
1044 
1045 			if (file->f_flags & O_NONBLOCK)
1046 				break;
1047 
1048 			if (tty_io_error(tty)) {
1049 				rc = -EIO;
1050 				break;
1051 			}
1052 
1053 			if (ch->ch_flags & CH_CD)
1054 				break;
1055 
1056 			if (ch->ch_flags & CH_FCAR)
1057 				break;
1058 		} else {
1059 			sleep_on_un_flags = 1;
1060 		}
1061 
1062 		/*
1063 		 * If there is a signal pending, the user probably
1064 		 * interrupted (ctrl-c) us.
1065 		 */
1066 		if (signal_pending(current)) {
1067 			rc = -ERESTARTSYS;
1068 			break;
1069 		}
1070 
1071 		if (sleep_on_un_flags)
1072 			old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1073 		else
1074 			old_flags = ch->ch_flags;
1075 
1076 		/*
1077 		 * Let go of channel lock before calling schedule.
1078 		 * Our poller will get any FEP events and wake us up when DCD
1079 		 * eventually goes active.
1080 		 */
1081 
1082 		spin_unlock_irqrestore(&ch->ch_lock, flags);
1083 
1084 		/*
1085 		 * Wait for something in the flags to change
1086 		 * from the current value.
1087 		 */
1088 		if (sleep_on_un_flags)
1089 			rc = wait_event_interruptible
1090 				(un->un_flags_wait,
1091 				 (old_flags != (ch->ch_tun.un_flags |
1092 						ch->ch_pun.un_flags)));
1093 		else
1094 			rc = wait_event_interruptible(
1095 					ch->ch_flags_wait,
1096 					(old_flags != ch->ch_flags));
1097 
1098 		/*
1099 		 * We got woken up for some reason.
1100 		 * Before looping around, grab our channel lock.
1101 		 */
1102 		spin_lock_irqsave(&ch->ch_lock, flags);
1103 	}
1104 
1105 	ch->ch_wopen--;
1106 
1107 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1108 
1109 	return rc;
1110 }
1111 
1112 /* Hangup the port.  Like a close, but don't wait for output to drain. */
dgnc_tty_hangup(struct tty_struct * tty)1113 static void dgnc_tty_hangup(struct tty_struct *tty)
1114 {
1115 	if (!tty)
1116 		return;
1117 
1118 	/* flush the transmit queues */
1119 	dgnc_tty_flush_buffer(tty);
1120 }
1121 
dgnc_tty_close(struct tty_struct * tty,struct file * file)1122 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1123 {
1124 	struct dgnc_board *bd;
1125 	struct channel_t *ch;
1126 	struct un_t *un;
1127 	unsigned long flags;
1128 
1129 	if (!tty)
1130 		return;
1131 
1132 	un = tty->driver_data;
1133 	if (!un)
1134 		return;
1135 
1136 	ch = un->un_ch;
1137 	if (!ch)
1138 		return;
1139 
1140 	bd = ch->ch_bd;
1141 	if (!bd)
1142 		return;
1143 
1144 	spin_lock_irqsave(&ch->ch_lock, flags);
1145 
1146 	/*
1147 	 * Determine if this is the last close or not - and if we agree about
1148 	 * which type of close it is with the Line Discipline
1149 	 */
1150 	if ((tty->count == 1) && (un->un_open_count != 1)) {
1151 		/*
1152 		 * Uh, oh.  tty->count is 1, which means that the tty
1153 		 * structure will be freed.  un_open_count should always
1154 		 * be one in these conditions.  If it's greater than
1155 		 * one, we've got real problems, since it means the
1156 		 * serial port won't be shutdown.
1157 		 */
1158 		dev_dbg(tty->dev,
1159 			"tty->count is 1, un open count is %d\n",
1160 			un->un_open_count);
1161 		un->un_open_count = 1;
1162 	}
1163 
1164 	if (un->un_open_count)
1165 		un->un_open_count--;
1166 	else
1167 		dev_dbg(tty->dev,
1168 			"bad serial port open count of %d\n",
1169 			un->un_open_count);
1170 
1171 	ch->ch_open_count--;
1172 
1173 	if (ch->ch_open_count && un->un_open_count) {
1174 		spin_unlock_irqrestore(&ch->ch_lock, flags);
1175 		return;
1176 	}
1177 
1178 	/* OK, its the last close on the unit */
1179 	un->un_flags |= UN_CLOSING;
1180 
1181 	tty->closing = 1;
1182 
1183 	/*
1184 	 * Only officially close channel if count is 0 and
1185 	 * DIGI_PRINTER bit is not set.
1186 	 */
1187 	if ((ch->ch_open_count == 0) &&
1188 	    !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1189 		ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1190 
1191 		/* turn off print device when closing print device. */
1192 
1193 		if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1194 			dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1195 				   (int)ch->ch_digi.digi_offlen);
1196 			ch->ch_flags &= ~CH_PRON;
1197 		}
1198 
1199 		spin_unlock_irqrestore(&ch->ch_lock, flags);
1200 		/* wait for output to drain */
1201 		/* This will also return if we take an interrupt */
1202 
1203 		bd->bd_ops->drain(tty, 0);
1204 
1205 		dgnc_tty_flush_buffer(tty);
1206 		tty_ldisc_flush(tty);
1207 
1208 		spin_lock_irqsave(&ch->ch_lock, flags);
1209 
1210 		tty->closing = 0;
1211 
1212 		/* If we have HUPCL set, lower DTR and RTS */
1213 
1214 		if (ch->ch_c_cflag & HUPCL) {
1215 			/* Drop RTS/DTR */
1216 			ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1217 			bd->bd_ops->assert_modem_signals(ch);
1218 
1219 			/*
1220 			 * Go to sleep to ensure RTS/DTR
1221 			 * have been dropped for modems to see it.
1222 			 */
1223 			if (ch->ch_close_delay) {
1224 				spin_unlock_irqrestore(&ch->ch_lock,
1225 						       flags);
1226 				msleep_interruptible(ch->ch_close_delay);
1227 				spin_lock_irqsave(&ch->ch_lock, flags);
1228 			}
1229 		}
1230 
1231 		ch->ch_old_baud = 0;
1232 
1233 		/* Turn off UART interrupts for this port */
1234 		ch->ch_bd->bd_ops->uart_off(ch);
1235 	} else {
1236 		/* turn off print device when closing print device. */
1237 
1238 		if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1239 			dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1240 				   (int)ch->ch_digi.digi_offlen);
1241 			ch->ch_flags &= ~CH_PRON;
1242 		}
1243 	}
1244 
1245 	un->un_tty = NULL;
1246 	un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1247 
1248 	wake_up_interruptible(&ch->ch_flags_wait);
1249 	wake_up_interruptible(&un->un_flags_wait);
1250 
1251 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1252 }
1253 
1254 /*
1255  * Return number of characters that have not been transmitted yet.
1256  *
1257  * This routine is used by the line discipline to determine if there
1258  * is data waiting to be transmitted/drained/flushed or not.
1259  */
dgnc_tty_chars_in_buffer(struct tty_struct * tty)1260 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1261 {
1262 	struct channel_t *ch = NULL;
1263 	struct un_t *un = NULL;
1264 	ushort thead;
1265 	ushort ttail;
1266 	uint tmask;
1267 	uint chars;
1268 	unsigned long flags;
1269 
1270 	if (!tty)
1271 		return 0;
1272 
1273 	un = tty->driver_data;
1274 	if (!un)
1275 		return 0;
1276 
1277 	ch = un->un_ch;
1278 	if (!ch)
1279 		return 0;
1280 
1281 	spin_lock_irqsave(&ch->ch_lock, flags);
1282 
1283 	tmask = WQUEUEMASK;
1284 	thead = ch->ch_w_head & tmask;
1285 	ttail = ch->ch_w_tail & tmask;
1286 
1287 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1288 
1289 	if (ttail == thead)
1290 		chars = 0;
1291 	else if (thead > ttail)
1292 		chars = thead - ttail;
1293 	else
1294 		chars = thead - ttail + WQUEUESIZE;
1295 
1296 	return chars;
1297 }
1298 
1299 /*
1300  * Reduces bytes_available to the max number of characters
1301  * that can be sent currently given the maxcps value, and
1302  * returns the new bytes_available.  This only affects printer
1303  * output.
1304  */
dgnc_maxcps_room(struct channel_t * ch,int bytes_available)1305 static int dgnc_maxcps_room(struct channel_t *ch, int bytes_available)
1306 {
1307 	int rc = bytes_available;
1308 
1309 	if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1310 		int cps_limit = 0;
1311 		unsigned long current_time = jiffies;
1312 		unsigned long buffer_time = current_time +
1313 			(HZ * ch->ch_digi.digi_bufsize) /
1314 			ch->ch_digi.digi_maxcps;
1315 
1316 		if (ch->ch_cpstime < current_time) {
1317 			/* buffer is empty */
1318 			ch->ch_cpstime = current_time;	/* reset ch_cpstime */
1319 			cps_limit = ch->ch_digi.digi_bufsize;
1320 		} else if (ch->ch_cpstime < buffer_time) {
1321 			/* still room in the buffer */
1322 			cps_limit = ((buffer_time - ch->ch_cpstime) *
1323 					ch->ch_digi.digi_maxcps) / HZ;
1324 		} else {
1325 			/* no room in the buffer */
1326 			cps_limit = 0;
1327 		}
1328 
1329 		rc = min(cps_limit, bytes_available);
1330 	}
1331 
1332 	return rc;
1333 }
1334 
1335 /* Return room available in Tx buffer */
dgnc_tty_write_room(struct tty_struct * tty)1336 static int dgnc_tty_write_room(struct tty_struct *tty)
1337 {
1338 	struct channel_t *ch = NULL;
1339 	struct un_t *un = NULL;
1340 	ushort head;
1341 	ushort tail;
1342 	ushort tmask;
1343 	int room = 0;
1344 	unsigned long flags;
1345 
1346 	if (!tty)
1347 		return 0;
1348 
1349 	un = tty->driver_data;
1350 	if (!un)
1351 		return 0;
1352 
1353 	ch = un->un_ch;
1354 	if (!ch)
1355 		return 0;
1356 
1357 	spin_lock_irqsave(&ch->ch_lock, flags);
1358 
1359 	tmask = WQUEUEMASK;
1360 	head = (ch->ch_w_head) & tmask;
1361 	tail = (ch->ch_w_tail) & tmask;
1362 
1363 	room = tail - head - 1;
1364 	if (room < 0)
1365 		room += WQUEUESIZE;
1366 
1367 	/* Limit printer to maxcps */
1368 	if (un->un_type != DGNC_PRINT)
1369 		room = dgnc_maxcps_room(ch, room);
1370 
1371 	/*
1372 	 * If we are printer device, leave room for
1373 	 * possibly both the on and off strings.
1374 	 */
1375 	if (un->un_type == DGNC_PRINT) {
1376 		if (!(ch->ch_flags & CH_PRON))
1377 			room -= ch->ch_digi.digi_onlen;
1378 		room -= ch->ch_digi.digi_offlen;
1379 	} else {
1380 		if (ch->ch_flags & CH_PRON)
1381 			room -= ch->ch_digi.digi_offlen;
1382 	}
1383 
1384 	if (room < 0)
1385 		room = 0;
1386 
1387 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1388 	return room;
1389 }
1390 
1391 /*
1392  * Put a character into ch->ch_buf
1393  * Used by the line discipline for OPOST processing
1394  */
dgnc_tty_put_char(struct tty_struct * tty,unsigned char c)1395 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1396 {
1397 	dgnc_tty_write(tty, &c, 1);
1398 	return 1;
1399 }
1400 
1401 /*
1402  * Take data from the user or kernel and send it out to the FEP.
1403  * In here exists all the Transparent Print magic as well.
1404  */
dgnc_tty_write(struct tty_struct * tty,const unsigned char * buf,int count)1405 static int dgnc_tty_write(struct tty_struct *tty,
1406 			  const unsigned char *buf, int count)
1407 {
1408 	struct channel_t *ch = NULL;
1409 	struct un_t *un = NULL;
1410 	int bufcount = 0, n = 0;
1411 	unsigned long flags;
1412 	ushort head;
1413 	ushort tail;
1414 	ushort tmask;
1415 	uint remain;
1416 
1417 	if (!tty)
1418 		return 0;
1419 
1420 	un = tty->driver_data;
1421 	if (!un)
1422 		return 0;
1423 
1424 	ch = un->un_ch;
1425 	if (!ch)
1426 		return 0;
1427 
1428 	if (!count)
1429 		return 0;
1430 
1431 	/*
1432 	 * Store original amount of characters passed in.
1433 	 * This helps to figure out if we should ask the FEP
1434 	 * to send us an event when it has more space available.
1435 	 */
1436 
1437 	spin_lock_irqsave(&ch->ch_lock, flags);
1438 
1439 	tmask = WQUEUEMASK;
1440 	head = (ch->ch_w_head) & tmask;
1441 	tail = (ch->ch_w_tail) & tmask;
1442 
1443 	bufcount = tail - head - 1;
1444 	if (bufcount < 0)
1445 		bufcount += WQUEUESIZE;
1446 
1447 	/*
1448 	 * Limit printer output to maxcps overall, with bursts allowed
1449 	 * up to bufsize characters.
1450 	 */
1451 	if (un->un_type != DGNC_PRINT)
1452 		bufcount = dgnc_maxcps_room(ch, bufcount);
1453 
1454 	count = min(count, bufcount);
1455 	if (count <= 0)
1456 		goto exit_retry;
1457 
1458 	/*
1459 	 * Output the printer ON string, if we are in terminal mode, but
1460 	 * need to be in printer mode.
1461 	 */
1462 	if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1463 		dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1464 			   (int)ch->ch_digi.digi_onlen);
1465 		head = (ch->ch_w_head) & tmask;
1466 		ch->ch_flags |= CH_PRON;
1467 	}
1468 
1469 	/*
1470 	 * On the other hand, output the printer OFF string, if we are
1471 	 * currently in printer mode, but need to output to the terminal.
1472 	 */
1473 	if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1474 		dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1475 			   (int)ch->ch_digi.digi_offlen);
1476 		head = (ch->ch_w_head) & tmask;
1477 		ch->ch_flags &= ~CH_PRON;
1478 	}
1479 
1480 	n = count;
1481 
1482 	/*
1483 	 * If the write wraps over the top of the circular buffer,
1484 	 * move the portion up to the wrap point, and reset the
1485 	 * pointers to the bottom.
1486 	 */
1487 	remain = WQUEUESIZE - head;
1488 
1489 	if (n >= remain) {
1490 		n -= remain;
1491 		memcpy(ch->ch_wqueue + head, buf, remain);
1492 		head = 0;
1493 		buf += remain;
1494 	}
1495 
1496 	if (n > 0) {
1497 		/* Move rest of data. */
1498 		remain = n;
1499 		memcpy(ch->ch_wqueue + head, buf, remain);
1500 		head += remain;
1501 	}
1502 
1503 	if (count) {
1504 		head &= tmask;
1505 		ch->ch_w_head = head;
1506 	}
1507 
1508 	/* Update printer buffer empty time. */
1509 	if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1510 	    (ch->ch_digi.digi_bufsize > 0)) {
1511 		ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1512 	}
1513 
1514 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1515 
1516 	if (count)
1517 		ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1518 
1519 	return count;
1520 
1521 exit_retry:
1522 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1523 
1524 	return 0;
1525 }
1526 
1527 /* Return modem signals to ld. */
dgnc_tty_tiocmget(struct tty_struct * tty)1528 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1529 {
1530 	struct channel_t *ch;
1531 	struct un_t *un;
1532 	int rc;
1533 	unsigned char mstat = 0;
1534 	unsigned long flags;
1535 
1536 	if (!tty)
1537 		return -EIO;
1538 
1539 	un = tty->driver_data;
1540 	if (!un)
1541 		return -EIO;
1542 
1543 	ch = un->un_ch;
1544 	if (!ch)
1545 		return -EIO;
1546 
1547 	spin_lock_irqsave(&ch->ch_lock, flags);
1548 
1549 	mstat = ch->ch_mostat | ch->ch_mistat;
1550 
1551 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1552 
1553 	rc = 0;
1554 
1555 	if (mstat & UART_MCR_DTR)
1556 		rc |= TIOCM_DTR;
1557 	if (mstat & UART_MCR_RTS)
1558 		rc |= TIOCM_RTS;
1559 	if (mstat & UART_MSR_CTS)
1560 		rc |= TIOCM_CTS;
1561 	if (mstat & UART_MSR_DSR)
1562 		rc |= TIOCM_DSR;
1563 	if (mstat & UART_MSR_RI)
1564 		rc |= TIOCM_RI;
1565 	if (mstat & UART_MSR_DCD)
1566 		rc |= TIOCM_CD;
1567 
1568 	return rc;
1569 }
1570 
1571 /* Set modem signals, called by ld. */
dgnc_tty_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)1572 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1573 			     unsigned int set, unsigned int clear)
1574 {
1575 	struct dgnc_board *bd;
1576 	struct channel_t *ch;
1577 	struct un_t *un;
1578 	unsigned long flags;
1579 
1580 	if (!tty)
1581 		return -EIO;
1582 
1583 	un = tty->driver_data;
1584 	if (!un)
1585 		return -EIO;
1586 
1587 	ch = un->un_ch;
1588 	if (!ch)
1589 		return -EIO;
1590 
1591 	bd = ch->ch_bd;
1592 	if (!bd)
1593 		return -EIO;
1594 
1595 	spin_lock_irqsave(&ch->ch_lock, flags);
1596 
1597 	if (set & TIOCM_RTS)
1598 		ch->ch_mostat |= UART_MCR_RTS;
1599 
1600 	if (set & TIOCM_DTR)
1601 		ch->ch_mostat |= UART_MCR_DTR;
1602 
1603 	if (clear & TIOCM_RTS)
1604 		ch->ch_mostat &= ~(UART_MCR_RTS);
1605 
1606 	if (clear & TIOCM_DTR)
1607 		ch->ch_mostat &= ~(UART_MCR_DTR);
1608 
1609 	bd->bd_ops->assert_modem_signals(ch);
1610 
1611 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1612 
1613 	return 0;
1614 }
1615 
1616 /* Send a Break, called by ld. */
dgnc_tty_send_break(struct tty_struct * tty,int msec)1617 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1618 {
1619 	struct dgnc_board *bd;
1620 	struct channel_t *ch;
1621 	struct un_t *un;
1622 	unsigned long flags;
1623 
1624 	if (!tty)
1625 		return -EIO;
1626 
1627 	un = tty->driver_data;
1628 	if (!un)
1629 		return -EIO;
1630 
1631 	ch = un->un_ch;
1632 	if (!ch)
1633 		return -EIO;
1634 
1635 	bd = ch->ch_bd;
1636 	if (!bd)
1637 		return -EIO;
1638 
1639 	if (msec < 0)
1640 		msec = 0xFFFF;
1641 
1642 	spin_lock_irqsave(&ch->ch_lock, flags);
1643 
1644 	bd->bd_ops->send_break(ch, msec);
1645 
1646 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1647 
1648 	return 0;
1649 }
1650 
1651 /* wait until data has been transmitted, called by ld. */
dgnc_tty_wait_until_sent(struct tty_struct * tty,int timeout)1652 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1653 {
1654 	struct dgnc_board *bd;
1655 	struct channel_t *ch;
1656 	struct un_t *un;
1657 
1658 	if (!tty)
1659 		return;
1660 
1661 	un = tty->driver_data;
1662 	if (!un)
1663 		return;
1664 
1665 	ch = un->un_ch;
1666 	if (!ch)
1667 		return;
1668 
1669 	bd = ch->ch_bd;
1670 	if (!bd)
1671 		return;
1672 
1673 	bd->bd_ops->drain(tty, 0);
1674 }
1675 
1676 /* send a high priority character, called by ld. */
dgnc_tty_send_xchar(struct tty_struct * tty,char c)1677 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1678 {
1679 	struct dgnc_board *bd;
1680 	struct channel_t *ch;
1681 	struct un_t *un;
1682 	unsigned long flags;
1683 
1684 	if (!tty)
1685 		return;
1686 
1687 	un = tty->driver_data;
1688 	if (!un)
1689 		return;
1690 
1691 	ch = un->un_ch;
1692 	if (!ch)
1693 		return;
1694 
1695 	bd = ch->ch_bd;
1696 	if (!bd)
1697 		return;
1698 
1699 	spin_lock_irqsave(&ch->ch_lock, flags);
1700 	bd->bd_ops->send_immediate_char(ch, c);
1701 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1702 }
1703 
1704 /* Return modem signals to ld. */
dgnc_get_mstat(struct channel_t * ch)1705 static inline int dgnc_get_mstat(struct channel_t *ch)
1706 {
1707 	unsigned char mstat;
1708 	unsigned long flags;
1709 	int rc;
1710 
1711 	if (!ch)
1712 		return -ENXIO;
1713 
1714 	spin_lock_irqsave(&ch->ch_lock, flags);
1715 
1716 	mstat = ch->ch_mostat | ch->ch_mistat;
1717 
1718 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1719 
1720 	rc = 0;
1721 
1722 	if (mstat & UART_MCR_DTR)
1723 		rc |= TIOCM_DTR;
1724 	if (mstat & UART_MCR_RTS)
1725 		rc |= TIOCM_RTS;
1726 	if (mstat & UART_MSR_CTS)
1727 		rc |= TIOCM_CTS;
1728 	if (mstat & UART_MSR_DSR)
1729 		rc |= TIOCM_DSR;
1730 	if (mstat & UART_MSR_RI)
1731 		rc |= TIOCM_RI;
1732 	if (mstat & UART_MSR_DCD)
1733 		rc |= TIOCM_CD;
1734 
1735 	return rc;
1736 }
1737 
1738 /* Return modem signals to ld. */
dgnc_get_modem_info(struct channel_t * ch,unsigned int __user * value)1739 static int dgnc_get_modem_info(struct channel_t *ch,
1740 			       unsigned int  __user *value)
1741 {
1742 	return put_user(dgnc_get_mstat(ch), value);
1743 }
1744 
1745 /* Set modem signals, called by ld. */
dgnc_set_modem_info(struct channel_t * ch,unsigned int command,unsigned int __user * value)1746 static int dgnc_set_modem_info(struct channel_t *ch,
1747 			       unsigned int command,
1748 			       unsigned int __user *value)
1749 {
1750 	int rc;
1751 	unsigned int arg = 0;
1752 	unsigned long flags;
1753 
1754 	rc = get_user(arg, value);
1755 	if (rc)
1756 		return rc;
1757 
1758 	switch (command) {
1759 	case TIOCMBIS:
1760 		if (arg & TIOCM_RTS)
1761 			ch->ch_mostat |= UART_MCR_RTS;
1762 
1763 		if (arg & TIOCM_DTR)
1764 			ch->ch_mostat |= UART_MCR_DTR;
1765 
1766 		break;
1767 
1768 	case TIOCMBIC:
1769 		if (arg & TIOCM_RTS)
1770 			ch->ch_mostat &= ~(UART_MCR_RTS);
1771 
1772 		if (arg & TIOCM_DTR)
1773 			ch->ch_mostat &= ~(UART_MCR_DTR);
1774 
1775 		break;
1776 
1777 	case TIOCMSET:
1778 
1779 		if (arg & TIOCM_RTS)
1780 			ch->ch_mostat |= UART_MCR_RTS;
1781 		else
1782 			ch->ch_mostat &= ~(UART_MCR_RTS);
1783 
1784 		if (arg & TIOCM_DTR)
1785 			ch->ch_mostat |= UART_MCR_DTR;
1786 		else
1787 			ch->ch_mostat &= ~(UART_MCR_DTR);
1788 
1789 		break;
1790 
1791 	default:
1792 		return -EINVAL;
1793 	}
1794 
1795 	spin_lock_irqsave(&ch->ch_lock, flags);
1796 
1797 	ch->ch_bd->bd_ops->assert_modem_signals(ch);
1798 
1799 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1800 
1801 	return 0;
1802 }
1803 
1804 /* Ioctl to get the information for ditty. */
dgnc_tty_digigeta(struct tty_struct * tty,struct digi_t __user * retinfo)1805 static int dgnc_tty_digigeta(struct tty_struct *tty,
1806 			     struct digi_t __user *retinfo)
1807 {
1808 	struct channel_t *ch;
1809 	struct un_t *un;
1810 	struct digi_t tmp;
1811 	unsigned long flags;
1812 
1813 	if (!retinfo)
1814 		return -EFAULT;
1815 
1816 	if (!tty)
1817 		return -EFAULT;
1818 
1819 	un = tty->driver_data;
1820 	if (!un)
1821 		return -EFAULT;
1822 
1823 	ch = un->un_ch;
1824 	if (!ch)
1825 		return -EFAULT;
1826 
1827 	memset(&tmp, 0, sizeof(tmp));
1828 
1829 	spin_lock_irqsave(&ch->ch_lock, flags);
1830 	memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
1831 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1832 
1833 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1834 		return -EFAULT;
1835 
1836 	return 0;
1837 }
1838 
1839 /* Ioctl to set the information for ditty. */
dgnc_tty_digiseta(struct tty_struct * tty,struct digi_t __user * new_info)1840 static int dgnc_tty_digiseta(struct tty_struct *tty,
1841 			     struct digi_t __user *new_info)
1842 {
1843 	struct dgnc_board *bd;
1844 	struct channel_t *ch;
1845 	struct un_t *un;
1846 	struct digi_t new_digi;
1847 	unsigned long flags;
1848 
1849 	if (!tty)
1850 		return -EFAULT;
1851 
1852 	un = tty->driver_data;
1853 	if (!un)
1854 		return -EFAULT;
1855 
1856 	ch = un->un_ch;
1857 	if (!ch)
1858 		return -EFAULT;
1859 
1860 	bd = ch->ch_bd;
1861 	if (!bd)
1862 		return -EFAULT;
1863 
1864 	if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
1865 		return -EFAULT;
1866 
1867 	spin_lock_irqsave(&ch->ch_lock, flags);
1868 
1869 	/* Handle transitions to and from RTS Toggle. */
1870 
1871 	if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1872 	    (new_digi.digi_flags & DIGI_RTS_TOGGLE))
1873 		ch->ch_mostat &= ~(UART_MCR_RTS);
1874 	if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1875 	    !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
1876 		ch->ch_mostat |= (UART_MCR_RTS);
1877 
1878 	/* Handle transitions to and from DTR Toggle. */
1879 
1880 	if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1881 	    (new_digi.digi_flags & DIGI_DTR_TOGGLE))
1882 		ch->ch_mostat &= ~(UART_MCR_DTR);
1883 	if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1884 	    !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
1885 		ch->ch_mostat |= (UART_MCR_DTR);
1886 
1887 	memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
1888 
1889 	if (ch->ch_digi.digi_maxcps < 1)
1890 		ch->ch_digi.digi_maxcps = 1;
1891 
1892 	if (ch->ch_digi.digi_maxcps > 10000)
1893 		ch->ch_digi.digi_maxcps = 10000;
1894 
1895 	if (ch->ch_digi.digi_bufsize < 10)
1896 		ch->ch_digi.digi_bufsize = 10;
1897 
1898 	if (ch->ch_digi.digi_maxchar < 1)
1899 		ch->ch_digi.digi_maxchar = 1;
1900 
1901 	if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
1902 		ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
1903 
1904 	if (ch->ch_digi.digi_onlen > DIGI_PLEN)
1905 		ch->ch_digi.digi_onlen = DIGI_PLEN;
1906 
1907 	if (ch->ch_digi.digi_offlen > DIGI_PLEN)
1908 		ch->ch_digi.digi_offlen = DIGI_PLEN;
1909 
1910 	bd->bd_ops->param(tty);
1911 
1912 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1913 
1914 	return 0;
1915 }
1916 
dgnc_tty_set_termios(struct tty_struct * tty,struct ktermios * old_termios)1917 static void dgnc_tty_set_termios(struct tty_struct *tty,
1918 				 struct ktermios *old_termios)
1919 {
1920 	struct dgnc_board *bd;
1921 	struct channel_t *ch;
1922 	struct un_t *un;
1923 	unsigned long flags;
1924 
1925 	if (!tty)
1926 		return;
1927 
1928 	un = tty->driver_data;
1929 	if (!un)
1930 		return;
1931 
1932 	ch = un->un_ch;
1933 	if (!ch)
1934 		return;
1935 
1936 	bd = ch->ch_bd;
1937 	if (!bd)
1938 		return;
1939 
1940 	spin_lock_irqsave(&ch->ch_lock, flags);
1941 
1942 	ch->ch_c_cflag   = tty->termios.c_cflag;
1943 	ch->ch_c_iflag   = tty->termios.c_iflag;
1944 	ch->ch_c_oflag   = tty->termios.c_oflag;
1945 	ch->ch_c_lflag   = tty->termios.c_lflag;
1946 	ch->ch_startc = tty->termios.c_cc[VSTART];
1947 	ch->ch_stopc  = tty->termios.c_cc[VSTOP];
1948 
1949 	bd->bd_ops->param(tty);
1950 	dgnc_carrier(ch);
1951 
1952 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1953 }
1954 
dgnc_tty_throttle(struct tty_struct * tty)1955 static void dgnc_tty_throttle(struct tty_struct *tty)
1956 {
1957 	struct channel_t *ch;
1958 	struct un_t *un;
1959 	unsigned long flags;
1960 
1961 	if (!tty)
1962 		return;
1963 
1964 	un = tty->driver_data;
1965 	if (!un)
1966 		return;
1967 
1968 	ch = un->un_ch;
1969 	if (!ch)
1970 		return;
1971 
1972 	spin_lock_irqsave(&ch->ch_lock, flags);
1973 
1974 	ch->ch_flags |= (CH_FORCED_STOPI);
1975 
1976 	spin_unlock_irqrestore(&ch->ch_lock, flags);
1977 }
1978 
dgnc_tty_unthrottle(struct tty_struct * tty)1979 static void dgnc_tty_unthrottle(struct tty_struct *tty)
1980 {
1981 	struct channel_t *ch;
1982 	struct un_t *un;
1983 	unsigned long flags;
1984 
1985 	if (!tty)
1986 		return;
1987 
1988 	un = tty->driver_data;
1989 	if (!un)
1990 		return;
1991 
1992 	ch = un->un_ch;
1993 	if (!ch)
1994 		return;
1995 
1996 	spin_lock_irqsave(&ch->ch_lock, flags);
1997 
1998 	ch->ch_flags &= ~(CH_FORCED_STOPI);
1999 
2000 	spin_unlock_irqrestore(&ch->ch_lock, flags);
2001 }
2002 
dgnc_tty_start(struct tty_struct * tty)2003 static void dgnc_tty_start(struct tty_struct *tty)
2004 {
2005 	struct dgnc_board *bd;
2006 	struct channel_t *ch;
2007 	struct un_t *un;
2008 	unsigned long flags;
2009 
2010 	if (!tty)
2011 		return;
2012 
2013 	un = tty->driver_data;
2014 	if (!un)
2015 		return;
2016 
2017 	ch = un->un_ch;
2018 	if (!ch)
2019 		return;
2020 
2021 	bd = ch->ch_bd;
2022 	if (!bd)
2023 		return;
2024 
2025 	spin_lock_irqsave(&ch->ch_lock, flags);
2026 
2027 	ch->ch_flags &= ~(CH_FORCED_STOP);
2028 
2029 	spin_unlock_irqrestore(&ch->ch_lock, flags);
2030 }
2031 
dgnc_tty_stop(struct tty_struct * tty)2032 static void dgnc_tty_stop(struct tty_struct *tty)
2033 {
2034 	struct dgnc_board *bd;
2035 	struct channel_t *ch;
2036 	struct un_t *un;
2037 	unsigned long flags;
2038 
2039 	if (!tty)
2040 		return;
2041 
2042 	un = tty->driver_data;
2043 	if (!un)
2044 		return;
2045 
2046 	ch = un->un_ch;
2047 	if (!ch)
2048 		return;
2049 
2050 	bd = ch->ch_bd;
2051 	if (!bd)
2052 		return;
2053 
2054 	spin_lock_irqsave(&ch->ch_lock, flags);
2055 
2056 	ch->ch_flags |= (CH_FORCED_STOP);
2057 
2058 	spin_unlock_irqrestore(&ch->ch_lock, flags);
2059 }
2060 
2061 /*
2062  * Flush the cook buffer
2063  *
2064  * Note to self, and any other poor souls who venture here:
2065  *
2066  * flush in this case DOES NOT mean dispose of the data.
2067  * instead, it means "stop buffering and send it if you
2068  * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
2069  *
2070  * It is also always called in interrupt context - JAR 8-Sept-99
2071  */
dgnc_tty_flush_chars(struct tty_struct * tty)2072 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2073 {
2074 	struct dgnc_board *bd;
2075 	struct channel_t *ch;
2076 	struct un_t *un;
2077 	unsigned long flags;
2078 
2079 	if (!tty)
2080 		return;
2081 
2082 	un = tty->driver_data;
2083 	if (!un)
2084 		return;
2085 
2086 	ch = un->un_ch;
2087 	if (!ch)
2088 		return;
2089 
2090 	bd = ch->ch_bd;
2091 	if (!bd)
2092 		return;
2093 
2094 	spin_lock_irqsave(&ch->ch_lock, flags);
2095 
2096 	/* Do something maybe here */
2097 
2098 	spin_unlock_irqrestore(&ch->ch_lock, flags);
2099 }
2100 
2101 /* Flush Tx buffer (make in == out) */
dgnc_tty_flush_buffer(struct tty_struct * tty)2102 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2103 {
2104 	struct channel_t *ch;
2105 	struct un_t *un;
2106 	unsigned long flags;
2107 
2108 	if (!tty)
2109 		return;
2110 
2111 	un = tty->driver_data;
2112 	if (!un)
2113 		return;
2114 
2115 	ch = un->un_ch;
2116 	if (!ch)
2117 		return;
2118 
2119 	spin_lock_irqsave(&ch->ch_lock, flags);
2120 
2121 	ch->ch_flags &= ~CH_STOP;
2122 
2123 	/* Flush our write queue */
2124 	ch->ch_w_head = ch->ch_w_tail;
2125 
2126 	/* Flush UARTs transmit FIFO */
2127 	ch->ch_bd->bd_ops->flush_uart_write(ch);
2128 
2129 	if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2130 		ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2131 		wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2132 	}
2133 	if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2134 		ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2135 		wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2136 	}
2137 
2138 	spin_unlock_irqrestore(&ch->ch_lock, flags);
2139 }
2140 
2141 /* Wakes up processes waiting in the unit's (teminal/printer) wait queue */
dgnc_wake_up_unit(struct un_t * unit)2142 static void dgnc_wake_up_unit(struct un_t *unit)
2143 {
2144 	unit->un_flags &= ~(UN_LOW | UN_EMPTY);
2145 	wake_up_interruptible(&unit->un_flags_wait);
2146 }
2147 
2148 /* The IOCTL function and all of its helpers */
2149 
2150 /* The usual assortment of ioctl's */
dgnc_tty_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)2151 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2152 			  unsigned long arg)
2153 {
2154 	struct dgnc_board *bd;
2155 	struct board_ops *ch_bd_ops;
2156 	struct channel_t *ch;
2157 	struct un_t *un;
2158 	int rc;
2159 	unsigned long flags;
2160 	void __user *uarg = (void __user *)arg;
2161 
2162 	if (!tty)
2163 		return -ENODEV;
2164 
2165 	un = tty->driver_data;
2166 	if (!un)
2167 		return -ENODEV;
2168 
2169 	ch = un->un_ch;
2170 	if (!ch)
2171 		return -ENODEV;
2172 
2173 	bd = ch->ch_bd;
2174 	if (!bd)
2175 		return -ENODEV;
2176 
2177 	ch_bd_ops = bd->bd_ops;
2178 
2179 	spin_lock_irqsave(&ch->ch_lock, flags);
2180 
2181 	if (un->un_open_count <= 0) {
2182 		rc = -EIO;
2183 		goto err_unlock;
2184 	}
2185 
2186 	switch (cmd) {
2187 	/* Here are all the standard ioctl's that we MUST implement */
2188 
2189 	case TCSBRK:
2190 		/*
2191 		 * TCSBRK is SVID version: non-zero arg --> no break
2192 		 * this behaviour is exploited by tcdrain().
2193 		 *
2194 		 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2195 		 * between 0.25 and 0.5 seconds so we'll ask for something
2196 		 * in the middle: 0.375 seconds.
2197 		 */
2198 		rc = tty_check_change(tty);
2199 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2200 		if (rc)
2201 			return rc;
2202 
2203 		rc = ch_bd_ops->drain(tty, 0);
2204 		if (rc)
2205 			return -EINTR;
2206 
2207 		spin_lock_irqsave(&ch->ch_lock, flags);
2208 
2209 		if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2210 			ch_bd_ops->send_break(ch, 250);
2211 
2212 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2213 
2214 		return 0;
2215 
2216 	case TCSBRKP:
2217 		/*
2218 		 * support for POSIX tcsendbreak()
2219 		 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2220 		 * between 0.25 and 0.5 seconds so we'll ask for something
2221 		 * in the middle: 0.375 seconds.
2222 		 */
2223 		rc = tty_check_change(tty);
2224 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2225 		if (rc)
2226 			return rc;
2227 
2228 		rc = ch_bd_ops->drain(tty, 0);
2229 		if (rc)
2230 			return -EINTR;
2231 
2232 		spin_lock_irqsave(&ch->ch_lock, flags);
2233 
2234 		ch_bd_ops->send_break(ch, 250);
2235 
2236 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2237 
2238 		return 0;
2239 
2240 	case TIOCSBRK:
2241 		rc = tty_check_change(tty);
2242 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2243 		if (rc)
2244 			return rc;
2245 
2246 		rc = ch_bd_ops->drain(tty, 0);
2247 		if (rc)
2248 			return -EINTR;
2249 
2250 		spin_lock_irqsave(&ch->ch_lock, flags);
2251 
2252 		ch_bd_ops->send_break(ch, 250);
2253 
2254 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2255 
2256 		return 0;
2257 
2258 	case TIOCCBRK:
2259 		/* Do Nothing */
2260 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2261 		return 0;
2262 
2263 	case TIOCGSOFTCAR:
2264 
2265 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2266 
2267 		return put_user(C_CLOCAL(tty) ? 1 : 0,
2268 				(unsigned long __user *)arg);
2269 
2270 	case TIOCSSOFTCAR:
2271 
2272 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2273 		rc = get_user(arg, (unsigned long __user *)arg);
2274 		if (rc)
2275 			return rc;
2276 
2277 		spin_lock_irqsave(&ch->ch_lock, flags);
2278 		tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2279 				       (arg ? CLOCAL : 0));
2280 		ch_bd_ops->param(tty);
2281 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2282 
2283 		return 0;
2284 
2285 	case TIOCMGET:
2286 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2287 		return dgnc_get_modem_info(ch, uarg);
2288 
2289 	case TIOCMBIS:
2290 	case TIOCMBIC:
2291 	case TIOCMSET:
2292 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2293 		return dgnc_set_modem_info(ch, cmd, uarg);
2294 
2295 		/* Here are any additional ioctl's that we want to implement */
2296 
2297 	case TCFLSH:
2298 		/*
2299 		 * The linux tty driver doesn't have a flush
2300 		 * input routine for the driver, assuming all backed
2301 		 * up data is in the line disc. buffers.  However,
2302 		 * we all know that's not the case.  Here, we
2303 		 * act on the ioctl, but then lie and say we didn't
2304 		 * so the line discipline will process the flush
2305 		 * also.
2306 		 */
2307 		rc = tty_check_change(tty);
2308 		if (rc)
2309 			goto err_unlock;
2310 
2311 		if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2312 			ch->ch_r_head = ch->ch_r_tail;
2313 			ch_bd_ops->flush_uart_read(ch);
2314 			/* Force queue flow control to be released, if needed */
2315 			dgnc_check_queue_flow_control(ch);
2316 		}
2317 
2318 		if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2319 			if (!(un->un_type == DGNC_PRINT)) {
2320 				ch->ch_w_head = ch->ch_w_tail;
2321 				ch_bd_ops->flush_uart_write(ch);
2322 
2323 				if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY))
2324 					dgnc_wake_up_unit(&ch->ch_tun);
2325 
2326 				if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY))
2327 					dgnc_wake_up_unit(&ch->ch_pun);
2328 			}
2329 		}
2330 
2331 		/* pretend we didn't recognize this IOCTL */
2332 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2333 		return -ENOIOCTLCMD;
2334 	case TCSETSF:
2335 	case TCSETSW:
2336 		/*
2337 		 * The linux tty driver doesn't have a flush
2338 		 * input routine for the driver, assuming all backed
2339 		 * up data is in the line disc. buffers.  However,
2340 		 * we all know that's not the case.  Here, we
2341 		 * act on the ioctl, but then lie and say we didn't
2342 		 * so the line discipline will process the flush
2343 		 * also.
2344 		 */
2345 		if (cmd == TCSETSF) {
2346 			/* flush rx */
2347 			ch->ch_flags &= ~CH_STOP;
2348 			ch->ch_r_head = ch->ch_r_tail;
2349 			ch_bd_ops->flush_uart_read(ch);
2350 			/* Force queue flow control to be released, if needed */
2351 			dgnc_check_queue_flow_control(ch);
2352 		}
2353 
2354 		/* now wait for all the output to drain */
2355 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2356 		rc = ch_bd_ops->drain(tty, 0);
2357 		if (rc)
2358 			return -EINTR;
2359 
2360 		/* pretend we didn't recognize this */
2361 		return -ENOIOCTLCMD;
2362 
2363 	case TCSETAW:
2364 
2365 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2366 		rc = ch_bd_ops->drain(tty, 0);
2367 		if (rc)
2368 			return -EINTR;
2369 
2370 		/* pretend we didn't recognize this */
2371 		return -ENOIOCTLCMD;
2372 
2373 	case TCXONC:
2374 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2375 		/* Make the ld do it */
2376 		return -ENOIOCTLCMD;
2377 
2378 	case DIGI_GETA:
2379 		/* get information for ditty */
2380 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2381 		return dgnc_tty_digigeta(tty, uarg);
2382 
2383 	case DIGI_SETAW:
2384 	case DIGI_SETAF:
2385 
2386 		/* set information for ditty */
2387 		if (cmd == (DIGI_SETAW)) {
2388 			spin_unlock_irqrestore(&ch->ch_lock, flags);
2389 			rc = ch_bd_ops->drain(tty, 0);
2390 			if (rc)
2391 				return -EINTR;
2392 
2393 			spin_lock_irqsave(&ch->ch_lock, flags);
2394 		} else {
2395 			tty_ldisc_flush(tty);
2396 		}
2397 		/* fall thru */
2398 
2399 	case DIGI_SETA:
2400 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2401 		return dgnc_tty_digiseta(tty, uarg);
2402 
2403 	case DIGI_LOOPBACK:
2404 		{
2405 			uint loopback = 0;
2406 			/*
2407 			 * Let go of locks when accessing user space,
2408 			 * could sleep
2409 			 */
2410 			spin_unlock_irqrestore(&ch->ch_lock, flags);
2411 			rc = get_user(loopback, (unsigned int __user *)arg);
2412 			if (rc)
2413 				return rc;
2414 			spin_lock_irqsave(&ch->ch_lock, flags);
2415 
2416 			/* Enable/disable internal loopback for this port */
2417 			if (loopback)
2418 				ch->ch_flags |= CH_LOOPBACK;
2419 			else
2420 				ch->ch_flags &= ~(CH_LOOPBACK);
2421 
2422 			ch_bd_ops->param(tty);
2423 			spin_unlock_irqrestore(&ch->ch_lock, flags);
2424 			return 0;
2425 		}
2426 
2427 	case DIGI_GETCUSTOMBAUD:
2428 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2429 		return put_user(ch->ch_custom_speed,
2430 				(unsigned int __user *)arg);
2431 
2432 	case DIGI_SETCUSTOMBAUD:
2433 	{
2434 		int new_rate;
2435 
2436 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2437 		rc = get_user(new_rate, (int __user *)arg);
2438 		if (rc)
2439 			return rc;
2440 		spin_lock_irqsave(&ch->ch_lock, flags);
2441 		dgnc_set_custom_speed(ch, new_rate);
2442 		ch_bd_ops->param(tty);
2443 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2444 		return 0;
2445 	}
2446 
2447 	/*
2448 	 * This ioctl allows insertion of a character into the front
2449 	 * of any pending data to be transmitted.
2450 	 *
2451 	 * This ioctl is to satisfy the "Send Character Immediate"
2452 	 * call that the RealPort protocol spec requires.
2453 	 */
2454 	case DIGI_REALPORT_SENDIMMEDIATE:
2455 	{
2456 		unsigned char c;
2457 
2458 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2459 		rc = get_user(c, (unsigned char __user *)arg);
2460 		if (rc)
2461 			return rc;
2462 		spin_lock_irqsave(&ch->ch_lock, flags);
2463 		ch_bd_ops->send_immediate_char(ch, c);
2464 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2465 		return 0;
2466 	}
2467 
2468 	/*
2469 	 * This ioctl returns all the current counts for the port.
2470 	 *
2471 	 * This ioctl is to satisfy the "Line Error Counters"
2472 	 * call that the RealPort protocol spec requires.
2473 	 */
2474 	case DIGI_REALPORT_GETCOUNTERS:
2475 	{
2476 		struct digi_getcounter buf;
2477 
2478 		buf.norun = ch->ch_err_overrun;
2479 		buf.noflow = 0;		/* The driver doesn't keep this stat */
2480 		buf.nframe = ch->ch_err_frame;
2481 		buf.nparity = ch->ch_err_parity;
2482 		buf.nbreak = ch->ch_err_break;
2483 		buf.rbytes = ch->ch_rxcount;
2484 		buf.tbytes = ch->ch_txcount;
2485 
2486 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2487 
2488 		if (copy_to_user(uarg, &buf, sizeof(buf)))
2489 			return -EFAULT;
2490 
2491 		return 0;
2492 	}
2493 
2494 	/*
2495 	 * This ioctl returns all current events.
2496 	 *
2497 	 * This ioctl is to satisfy the "Event Reporting"
2498 	 * call that the RealPort protocol spec requires.
2499 	 */
2500 	case DIGI_REALPORT_GETEVENTS:
2501 	{
2502 		unsigned int events = 0;
2503 
2504 		/* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2505 		if (ch->ch_flags & CH_BREAK_SENDING)
2506 			events |= EV_TXB;
2507 		if ((ch->ch_flags & CH_STOP) ||
2508 		    (ch->ch_flags & CH_FORCED_STOP))
2509 			events |= (EV_OPU | EV_OPS);
2510 
2511 		if ((ch->ch_flags & CH_STOPI) ||
2512 		    (ch->ch_flags & CH_FORCED_STOPI))
2513 			events |= (EV_IPU | EV_IPS);
2514 
2515 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2516 		return put_user(events, (unsigned int __user *)arg);
2517 	}
2518 
2519 	/*
2520 	 * This ioctl returns TOUT and TIN counters based
2521 	 * upon the values passed in by the RealPort Server.
2522 	 * It also passes back whether the UART Transmitter is
2523 	 * empty as well.
2524 	 */
2525 	case DIGI_REALPORT_GETBUFFERS:
2526 	{
2527 		struct digi_getbuffer buf;
2528 		int tdist;
2529 		int count;
2530 
2531 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2532 
2533 		if (copy_from_user(&buf, uarg, sizeof(buf)))
2534 			return -EFAULT;
2535 
2536 		spin_lock_irqsave(&ch->ch_lock, flags);
2537 
2538 		/* Figure out how much data is in our RX and TX queues. */
2539 
2540 		buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2541 		buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2542 
2543 		/*
2544 		 * Is the UART empty?
2545 		 * Add that value to whats in our TX queue.
2546 		 */
2547 
2548 		count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2549 
2550 		/*
2551 		 * Figure out how much data the RealPort Server believes should
2552 		 * be in our TX queue.
2553 		 */
2554 		tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2555 
2556 		/*
2557 		 * If we have more data than the RealPort Server believes we
2558 		 * should have, reduce our count to its amount.
2559 		 *
2560 		 * This count difference CAN happen because the Linux LD can
2561 		 * insert more characters into our queue for OPOST processing
2562 		 * that the RealPort Server doesn't know about.
2563 		 */
2564 		if (buf.txbuf > tdist)
2565 			buf.txbuf = tdist;
2566 
2567 		/* Report whether our queue and UART TX are completely empty. */
2568 
2569 		if (count)
2570 			buf.txdone = 0;
2571 		else
2572 			buf.txdone = 1;
2573 
2574 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2575 
2576 		if (copy_to_user(uarg, &buf, sizeof(buf)))
2577 			return -EFAULT;
2578 
2579 		return 0;
2580 	}
2581 	default:
2582 		spin_unlock_irqrestore(&ch->ch_lock, flags);
2583 
2584 		return -ENOIOCTLCMD;
2585 	}
2586 err_unlock:
2587 	spin_unlock_irqrestore(&ch->ch_lock, flags);
2588 
2589 	return rc;
2590 }
2591