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