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