]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/dgnc/dgnc_tty.c
staging: dgnc: Fix sleeping under spinlock bug
[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 */
1296 ch->ch_r_head = ch->ch_r_tail = 0;
1297 ch->ch_e_head = ch->ch_e_tail = 0;
1298 ch->ch_w_head = ch->ch_w_tail = 0;
1299
1300 brd->bd_ops->flush_uart_write(ch);
1301 brd->bd_ops->flush_uart_read(ch);
1302
1303 ch->ch_flags = 0;
1304 ch->ch_cached_lsr = 0;
1305 ch->ch_stop_sending_break = 0;
1306 ch->ch_stops_sent = 0;
1307
22e3de76
LL
1308 ch->ch_c_cflag = tty->termios.c_cflag;
1309 ch->ch_c_iflag = tty->termios.c_iflag;
1310 ch->ch_c_oflag = tty->termios.c_oflag;
1311 ch->ch_c_lflag = tty->termios.c_lflag;
1312 ch->ch_startc = tty->termios.c_cc[VSTART];
1313 ch->ch_stopc = tty->termios.c_cc[VSTOP];
0b99d589
LL
1314
1315 /*
1316 * Bring up RTS and DTR...
1317 * Also handle RTS or DTR toggle if set.
1318 */
1319 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1320 ch->ch_mostat |= (UART_MCR_RTS);
1321 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1322 ch->ch_mostat |= (UART_MCR_DTR);
1323
1324 /* Tell UART to init itself */
1325 brd->bd_ops->uart_init(ch);
1326 }
1327
1328 /*
1329 * Run param in case we changed anything
1330 */
1331 brd->bd_ops->param(tty);
1332
1333 dgnc_carrier(ch);
1334
8b07d521 1335 /*
0b99d589
LL
1336 * follow protocol for opening port
1337 */
1338
1339 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1340
1341 rc = dgnc_block_til_ready(tty, file, ch);
1342
0b99d589
LL
1343 /* No going back now, increment our unit and channel counters */
1344 DGNC_LOCK(ch->ch_lock, lock_flags);
1345 ch->ch_open_count++;
1346 un->un_open_count++;
1347 un->un_flags |= (UN_ISOPEN);
1348 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1349
8f90ef80 1350 return rc;
0b99d589
LL
1351}
1352
1353
8b07d521 1354/*
0b99d589
LL
1355 * dgnc_block_til_ready()
1356 *
1357 * Wait for DCD, if needed.
1358 */
1359static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
8b07d521 1360{
0b99d589
LL
1361 int retval = 0;
1362 struct un_t *un = NULL;
1363 ulong lock_flags;
1364 uint old_flags = 0;
1365 int sleep_on_un_flags = 0;
1366
1367 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC) {
8f90ef80 1368 return -ENXIO;
0b99d589
LL
1369 }
1370
1371 un = tty->driver_data;
50667c67 1372 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 1373 return -ENXIO;
0b99d589 1374
0b99d589
LL
1375 DGNC_LOCK(ch->ch_lock, lock_flags);
1376
1377 ch->ch_wopen++;
1378
1379 /* Loop forever */
1380 while (1) {
1381
1382 sleep_on_un_flags = 0;
1383
1384 /*
1385 * If board has failed somehow during our sleep, bail with error.
1386 */
1387 if (ch->ch_bd->state == BOARD_FAILED) {
1388 retval = -ENXIO;
1389 break;
1390 }
1391
1392 /* If tty was hung up, break out of loop and set error. */
1393 if (tty_hung_up_p(file)) {
1394 retval = -EAGAIN;
1395 break;
1396 }
1397
1398 /*
1399 * If either unit is in the middle of the fragile part of close,
1400 * we just cannot touch the channel safely.
1401 * Go back to sleep, knowing that when the channel can be
8b07d521 1402 * touched safely, the close routine will signal the
0b99d589
LL
1403 * ch_wait_flags to wake us back up.
1404 */
1405 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1406
1407 /*
1408 * Our conditions to leave cleanly and happily:
1409 * 1) NONBLOCKING on the tty is set.
1410 * 2) CLOCAL is set.
1411 * 3) DCD (fake or real) is active.
1412 */
1413
50667c67 1414 if (file->f_flags & O_NONBLOCK)
0b99d589 1415 break;
0b99d589
LL
1416
1417 if (tty->flags & (1 << TTY_IO_ERROR)) {
1418 retval = -EIO;
1419 break;
1420 }
1421
f1e51745 1422 if (ch->ch_flags & CH_CD)
0b99d589 1423 break;
0b99d589 1424
f1e51745 1425 if (ch->ch_flags & CH_FCAR)
0b99d589 1426 break;
2000c581 1427 } else {
0b99d589
LL
1428 sleep_on_un_flags = 1;
1429 }
1430
1431 /*
1432 * If there is a signal pending, the user probably
1433 * interrupted (ctrl-c) us.
1434 * Leave loop with error set.
1435 */
1436 if (signal_pending(current)) {
0b99d589
LL
1437 retval = -ERESTARTSYS;
1438 break;
1439 }
1440
0b99d589
LL
1441 /*
1442 * Store the flags before we let go of channel lock
1443 */
1444 if (sleep_on_un_flags)
1445 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1446 else
1447 old_flags = ch->ch_flags;
1448
1449 /*
1450 * Let go of channel lock before calling schedule.
1451 * Our poller will get any FEP events and wake us up when DCD
1452 * eventually goes active.
1453 */
1454
1455 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1456
0b99d589
LL
1457 /*
1458 * Wait for something in the flags to change from the current value.
1459 */
50667c67 1460 if (sleep_on_un_flags)
0b99d589
LL
1461 retval = wait_event_interruptible(un->un_flags_wait,
1462 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
50667c67 1463 else
0b99d589
LL
1464 retval = wait_event_interruptible(ch->ch_flags_wait,
1465 (old_flags != ch->ch_flags));
0b99d589 1466
0b99d589
LL
1467 /*
1468 * We got woken up for some reason.
1469 * Before looping around, grab our channel lock.
1470 */
1471 DGNC_LOCK(ch->ch_lock, lock_flags);
1472 }
1473
1474 ch->ch_wopen--;
1475
1476 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1477
f1e51745 1478 if (retval)
8f90ef80 1479 return retval;
0b99d589 1480
8f90ef80 1481 return 0;
0b99d589
LL
1482}
1483
1484
1485/*
1486 * dgnc_tty_hangup()
1487 *
1488 * Hangup the port. Like a close, but don't wait for output to drain.
8b07d521 1489 */
0b99d589
LL
1490static void dgnc_tty_hangup(struct tty_struct *tty)
1491{
1492 struct un_t *un;
1493
1494 if (!tty || tty->magic != TTY_MAGIC)
1495 return;
1496
1497 un = tty->driver_data;
1498 if (!un || un->magic != DGNC_UNIT_MAGIC)
1499 return;
1500
0b99d589
LL
1501 /* flush the transmit queues */
1502 dgnc_tty_flush_buffer(tty);
1503
0b99d589
LL
1504}
1505
1506
1507/*
1508 * dgnc_tty_close()
1509 *
1510 */
1511static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1512{
1513 struct ktermios *ts;
03425f55 1514 struct dgnc_board *bd;
0b99d589
LL
1515 struct channel_t *ch;
1516 struct un_t *un;
1517 ulong lock_flags;
1518 int rc = 0;
1519
1520 if (!tty || tty->magic != TTY_MAGIC)
1521 return;
1522
1523 un = tty->driver_data;
1524 if (!un || un->magic != DGNC_UNIT_MAGIC)
1525 return;
1526
1527 ch = un->un_ch;
1528 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1529 return;
1530
1531 bd = ch->ch_bd;
1532 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1533 return;
1534
5857a0b5 1535 ts = &tty->termios;
0b99d589 1536
0b99d589
LL
1537 DGNC_LOCK(ch->ch_lock, lock_flags);
1538
1539 /*
1540 * Determine if this is the last close or not - and if we agree about
1541 * which type of close it is with the Line Discipline
1542 */
1543 if ((tty->count == 1) && (un->un_open_count != 1)) {
1544 /*
1545 * Uh, oh. tty->count is 1, which means that the tty
1546 * structure will be freed. un_open_count should always
1547 * be one in these conditions. If it's greater than
1548 * one, we've got real problems, since it means the
1549 * serial port won't be shutdown.
1550 */
1551 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1552 un->un_open_count = 1;
8b07d521 1553 }
0b99d589 1554
3098e514
DY
1555 if (un->un_open_count)
1556 un->un_open_count--;
1557 else
0b99d589 1558 APR(("bad serial port open count of %d\n", un->un_open_count));
0b99d589
LL
1559
1560 ch->ch_open_count--;
1561
1562 if (ch->ch_open_count && un->un_open_count) {
0b99d589 1563 DGNC_UNLOCK(ch->ch_lock, lock_flags);
90d2a471
LL
1564 return;
1565 }
0b99d589
LL
1566
1567 /* OK, its the last close on the unit */
0b99d589
LL
1568 un->un_flags |= UN_CLOSING;
1569
1570 tty->closing = 1;
1571
1572
1573 /*
1574 * Only officially close channel if count is 0 and
90d2a471 1575 * DIGI_PRINTER bit is not set.
0b99d589
LL
1576 */
1577 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1578
1579 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1580
1581 /*
1582 * turn off print device when closing print device.
1583 */
9cdf838b 1584 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
0b99d589
LL
1585 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1586 (int) ch->ch_digi.digi_offlen);
1587 ch->ch_flags &= ~CH_PRON;
1588 }
1589
1590 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1591 /* wait for output to drain */
1592 /* This will also return if we take an interrupt */
1593
0b99d589
LL
1594 rc = bd->bd_ops->drain(tty, 0);
1595
0b99d589
LL
1596 dgnc_tty_flush_buffer(tty);
1597 tty_ldisc_flush(tty);
1598
1599 DGNC_LOCK(ch->ch_lock, lock_flags);
1600
1601 tty->closing = 0;
1602
1603 /*
1604 * If we have HUPCL set, lower DTR and RTS
1605 */
1606 if (ch->ch_c_cflag & HUPCL) {
0b99d589
LL
1607
1608 /* Drop RTS/DTR */
1609 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1610 bd->bd_ops->assert_modem_signals(ch);
1611
1612 /*
8b07d521 1613 * Go to sleep to ensure RTS/DTR
0b99d589
LL
1614 * have been dropped for modems to see it.
1615 */
1616 if (ch->ch_close_delay) {
0b99d589
LL
1617 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1618 dgnc_ms_sleep(ch->ch_close_delay);
1619 DGNC_LOCK(ch->ch_lock, lock_flags);
0b99d589
LL
1620 }
1621 }
1622
1623 ch->ch_old_baud = 0;
1624
1625 /* Turn off UART interrupts for this port */
1626 ch->ch_bd->bd_ops->uart_off(ch);
2000c581 1627 } else {
0b99d589
LL
1628 /*
1629 * turn off print device when closing print device.
1630 */
9cdf838b 1631 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
0b99d589
LL
1632 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1633 (int) ch->ch_digi.digi_offlen);
1634 ch->ch_flags &= ~CH_PRON;
1635 }
1636 }
1637
1638 un->un_tty = NULL;
1639 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1640
0b99d589
LL
1641 wake_up_interruptible(&ch->ch_flags_wait);
1642 wake_up_interruptible(&un->un_flags_wait);
1643
1644 DGNC_UNLOCK(ch->ch_lock, lock_flags);
0b99d589
LL
1645}
1646
1647
1648/*
1649 * dgnc_tty_chars_in_buffer()
1650 *
1651 * Return number of characters that have not been transmitted yet.
1652 *
1653 * This routine is used by the line discipline to determine if there
1654 * is data waiting to be transmitted/drained/flushed or not.
1655 */
1656static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1657{
1658 struct channel_t *ch = NULL;
1659 struct un_t *un = NULL;
1660 ushort thead;
1661 ushort ttail;
1662 uint tmask;
1663 uint chars = 0;
1664 ulong lock_flags = 0;
1665
1666 if (tty == NULL)
8f90ef80 1667 return 0;
0b99d589
LL
1668
1669 un = tty->driver_data;
1670 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 1671 return 0;
0b99d589
LL
1672
1673 ch = un->un_ch;
1674 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 1675 return 0;
0b99d589
LL
1676
1677 DGNC_LOCK(ch->ch_lock, lock_flags);
1678
1679 tmask = WQUEUEMASK;
1680 thead = ch->ch_w_head & tmask;
1681 ttail = ch->ch_w_tail & tmask;
1682
1683 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1684
1685 if (ttail == thead) {
1686 chars = 0;
1687 } else {
1688 if (thead >= ttail)
1689 chars = thead - ttail;
1690 else
1691 chars = thead - ttail + WQUEUESIZE;
1692 }
1693
8f90ef80 1694 return chars;
0b99d589
LL
1695}
1696
1697
8b07d521 1698/*
0b99d589
LL
1699 * dgnc_maxcps_room
1700 *
1701 * Reduces bytes_available to the max number of characters
1702 * that can be sent currently given the maxcps value, and
1703 * returns the new bytes_available. This only affects printer
1704 * output.
8b07d521 1705 */
0b99d589
LL
1706static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1707{
1708 struct channel_t *ch = NULL;
1709 struct un_t *un = NULL;
1710
1711 if (!tty)
8f90ef80 1712 return bytes_available;
0b99d589
LL
1713
1714 un = tty->driver_data;
1715 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 1716 return bytes_available;
0b99d589
LL
1717
1718 ch = un->un_ch;
1719 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 1720 return bytes_available;
0b99d589
LL
1721
1722 /*
1723 * If its not the Transparent print device, return
1724 * the full data amount.
1725 */
1726 if (un->un_type != DGNC_PRINT)
8f90ef80 1727 return bytes_available;
0b99d589 1728
9cdf838b 1729 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
0b99d589
LL
1730 int cps_limit = 0;
1731 unsigned long current_time = jiffies;
1732 unsigned long buffer_time = current_time +
1733 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1734
1735 if (ch->ch_cpstime < current_time) {
1736 /* buffer is empty */
90d2a471 1737 ch->ch_cpstime = current_time; /* reset ch_cpstime */
0b99d589 1738 cps_limit = ch->ch_digi.digi_bufsize;
2000c581 1739 } else if (ch->ch_cpstime < buffer_time) {
0b99d589
LL
1740 /* still room in the buffer */
1741 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
2000c581 1742 } else {
0b99d589 1743 /* no room in the buffer */
8b07d521 1744 cps_limit = 0;
0b99d589
LL
1745 }
1746
1747 bytes_available = min(cps_limit, bytes_available);
1748 }
1749
8f90ef80 1750 return bytes_available;
0b99d589
LL
1751}
1752
1753
1754/*
1755 * dgnc_tty_write_room()
1756 *
1757 * Return space available in Tx buffer
8b07d521 1758 */
0b99d589
LL
1759static int dgnc_tty_write_room(struct tty_struct *tty)
1760{
1761 struct channel_t *ch = NULL;
1762 struct un_t *un = NULL;
1763 ushort head;
1764 ushort tail;
1765 ushort tmask;
1766 int ret = 0;
1767 ulong lock_flags = 0;
1768
1769 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
8f90ef80 1770 return 0;
0b99d589
LL
1771
1772 un = tty->driver_data;
1773 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 1774 return 0;
0b99d589
LL
1775
1776 ch = un->un_ch;
1777 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 1778 return 0;
0b99d589
LL
1779
1780 DGNC_LOCK(ch->ch_lock, lock_flags);
1781
1782 tmask = WQUEUEMASK;
1783 head = (ch->ch_w_head) & tmask;
1784 tail = (ch->ch_w_tail) & tmask;
1785
f7c851d4
CP
1786 ret = tail - head - 1;
1787 if (ret < 0)
0b99d589
LL
1788 ret += WQUEUESIZE;
1789
1790 /* Limit printer to maxcps */
1791 ret = dgnc_maxcps_room(tty, ret);
1792
1793 /*
8b07d521 1794 * If we are printer device, leave space for
0b99d589
LL
1795 * possibly both the on and off strings.
1796 */
1797 if (un->un_type == DGNC_PRINT) {
1798 if (!(ch->ch_flags & CH_PRON))
1799 ret -= ch->ch_digi.digi_onlen;
1800 ret -= ch->ch_digi.digi_offlen;
2000c581 1801 } else {
0b99d589
LL
1802 if (ch->ch_flags & CH_PRON)
1803 ret -= ch->ch_digi.digi_offlen;
1804 }
1805
1806 if (ret < 0)
1807 ret = 0;
1808
1809 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8b07d521 1810
8f90ef80 1811 return ret;
0b99d589
LL
1812}
1813
1814
1815/*
1816 * dgnc_tty_put_char()
1817 *
1818 * Put a character into ch->ch_buf
8b07d521 1819 *
0b99d589
LL
1820 * - used by the line discipline for OPOST processing
1821 */
1822static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1823{
1824 /*
1825 * Simply call tty_write.
1826 */
0b99d589
LL
1827 dgnc_tty_write(tty, &c, 1);
1828 return 1;
1829}
1830
1831
1832/*
1833 * dgnc_tty_write()
1834 *
1835 * Take data from the user or kernel and send it out to the FEP.
1836 * In here exists all the Transparent Print magic as well.
1837 */
1838static int dgnc_tty_write(struct tty_struct *tty,
1839 const unsigned char *buf, int count)
1840{
1841 struct channel_t *ch = NULL;
1842 struct un_t *un = NULL;
1843 int bufcount = 0, n = 0;
1844 int orig_count = 0;
1845 ulong lock_flags;
1846 ushort head;
1847 ushort tail;
1848 ushort tmask;
1849 uint remain;
1850 int from_user = 0;
1851
1852 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
8f90ef80 1853 return 0;
0b99d589
LL
1854
1855 un = tty->driver_data;
1856 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 1857 return 0;
0b99d589
LL
1858
1859 ch = un->un_ch;
1860 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 1861 return 0;
0b99d589
LL
1862
1863 if (!count)
8f90ef80 1864 return 0;
0b99d589 1865
0b99d589
LL
1866 /*
1867 * Store original amount of characters passed in.
1868 * This helps to figure out if we should ask the FEP
1869 * to send us an event when it has more space available.
1870 */
1871 orig_count = count;
1872
1873 DGNC_LOCK(ch->ch_lock, lock_flags);
1874
1875 /* Get our space available for the channel from the board */
1876 tmask = WQUEUEMASK;
1877 head = (ch->ch_w_head) & tmask;
1878 tail = (ch->ch_w_tail) & tmask;
1879
f7c851d4
CP
1880 bufcount = tail - head - 1;
1881 if (bufcount < 0)
0b99d589
LL
1882 bufcount += WQUEUESIZE;
1883
0b99d589
LL
1884 /*
1885 * Limit printer output to maxcps overall, with bursts allowed
1886 * up to bufsize characters.
1887 */
1888 bufcount = dgnc_maxcps_room(tty, bufcount);
1889
1890 /*
1891 * Take minimum of what the user wants to send, and the
1892 * space available in the FEP buffer.
1893 */
1894 count = min(count, bufcount);
1895
1896 /*
1897 * Bail if no space left.
1898 */
1899 if (count <= 0) {
1900 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 1901 return 0;
0b99d589
LL
1902 }
1903
1904 /*
1905 * Output the printer ON string, if we are in terminal mode, but
1906 * need to be in printer mode.
1907 */
1908 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1909 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1910 (int) ch->ch_digi.digi_onlen);
1911 head = (ch->ch_w_head) & tmask;
1912 ch->ch_flags |= CH_PRON;
1913 }
1914
1915 /*
1916 * On the other hand, output the printer OFF string, if we are
1917 * currently in printer mode, but need to output to the terminal.
1918 */
1919 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1920 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1921 (int) ch->ch_digi.digi_offlen);
1922 head = (ch->ch_w_head) & tmask;
1923 ch->ch_flags &= ~CH_PRON;
1924 }
1925
1926 /*
1927 * If there is nothing left to copy, or I can't handle any more data, leave.
8b07d521 1928 */
0b99d589
LL
1929 if (count <= 0) {
1930 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 1931 return 0;
0b99d589
LL
1932 }
1933
1934 if (from_user) {
1935
1936 count = min(count, WRITEBUFLEN);
1937
1938 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1939
1940 /*
1941 * If data is coming from user space, copy it into a temporary
1942 * buffer so we don't get swapped out while doing the copy to
1943 * the board.
1944 */
1945 /* we're allowed to block if it's from_user */
50667c67 1946 if (down_interruptible(&dgnc_TmpWriteSem))
8f90ef80 1947 return -EINTR;
0b99d589
LL
1948
1949 /*
1950 * copy_from_user() returns the number
1951 * of bytes that could *NOT* be copied.
1952 */
1953 count -= copy_from_user(dgnc_TmpWriteBuf, (const uchar __user *) buf, count);
1954
1955 if (!count) {
1956 up(&dgnc_TmpWriteSem);
8f90ef80 1957 return -EFAULT;
0b99d589
LL
1958 }
1959
1960 DGNC_LOCK(ch->ch_lock, lock_flags);
1961
1962 buf = dgnc_TmpWriteBuf;
1963
1964 }
1965
1966 n = count;
1967
1968 /*
1969 * If the write wraps over the top of the circular buffer,
1970 * move the portion up to the wrap point, and reset the
1971 * pointers to the bottom.
1972 */
1973 remain = WQUEUESIZE - head;
1974
1975 if (n >= remain) {
1976 n -= remain;
1977 memcpy(ch->ch_wqueue + head, buf, remain);
1978 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1979 head = 0;
1980 buf += remain;
1981 }
1982
1983 if (n > 0) {
1984 /*
1985 * Move rest of data.
1986 */
1987 remain = n;
1988 memcpy(ch->ch_wqueue + head, buf, remain);
1989 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1990 head += remain;
1991 }
1992
1993 if (count) {
1994 head &= tmask;
1995 ch->ch_w_head = head;
1996 }
1997
0b99d589
LL
1998 /* Update printer buffer empty time. */
1999 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2000 && (ch->ch_digi.digi_bufsize > 0)) {
90d2a471 2001 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
0b99d589
LL
2002 }
2003
2004 if (from_user) {
2005 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2006 up(&dgnc_TmpWriteSem);
2007 } else {
2008 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2009 }
2010
0b99d589
LL
2011 if (count) {
2012 /*
2013 * Channel lock is grabbed and then released
2014 * inside this routine.
2015 */
2016 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2017 }
2018
8f90ef80 2019 return count;
0b99d589
LL
2020}
2021
2022
2023/*
2024 * Return modem signals to ld.
2025 */
b74c7461 2026
0b99d589 2027static int dgnc_tty_tiocmget(struct tty_struct *tty)
0b99d589
LL
2028{
2029 struct channel_t *ch;
2030 struct un_t *un;
2031 int result = -EIO;
2032 uchar mstat = 0;
2033 ulong lock_flags;
2034
2035 if (!tty || tty->magic != TTY_MAGIC)
2036 return result;
2037
2038 un = tty->driver_data;
2039 if (!un || un->magic != DGNC_UNIT_MAGIC)
2040 return result;
2041
2042 ch = un->un_ch;
2043 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2044 return result;
2045
0b99d589
LL
2046 DGNC_LOCK(ch->ch_lock, lock_flags);
2047
2048 mstat = (ch->ch_mostat | ch->ch_mistat);
2049
2050 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2051
2052 result = 0;
2053
2054 if (mstat & UART_MCR_DTR)
2055 result |= TIOCM_DTR;
2056 if (mstat & UART_MCR_RTS)
2057 result |= TIOCM_RTS;
2058 if (mstat & UART_MSR_CTS)
2059 result |= TIOCM_CTS;
2060 if (mstat & UART_MSR_DSR)
2061 result |= TIOCM_DSR;
2062 if (mstat & UART_MSR_RI)
2063 result |= TIOCM_RI;
2064 if (mstat & UART_MSR_DCD)
2065 result |= TIOCM_CD;
2066
0b99d589
LL
2067 return result;
2068}
2069
2070
2071/*
2072 * dgnc_tty_tiocmset()
2073 *
2074 * Set modem signals, called by ld.
2075 */
b74c7461 2076
0b99d589 2077static int dgnc_tty_tiocmset(struct tty_struct *tty,
90d2a471 2078 unsigned int set, unsigned int clear)
0b99d589 2079{
03425f55 2080 struct dgnc_board *bd;
0b99d589
LL
2081 struct channel_t *ch;
2082 struct un_t *un;
2083 int ret = -EIO;
2084 ulong lock_flags;
2085
2086 if (!tty || tty->magic != TTY_MAGIC)
2087 return ret;
2088
2089 un = tty->driver_data;
2090 if (!un || un->magic != DGNC_UNIT_MAGIC)
2091 return ret;
2092
2093 ch = un->un_ch;
2094 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2095 return ret;
2096
2097 bd = ch->ch_bd;
2098 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2099 return ret;
2100
0b99d589
LL
2101 DGNC_LOCK(ch->ch_lock, lock_flags);
2102
50667c67 2103 if (set & TIOCM_RTS)
0b99d589 2104 ch->ch_mostat |= UART_MCR_RTS;
0b99d589 2105
50667c67 2106 if (set & TIOCM_DTR)
0b99d589 2107 ch->ch_mostat |= UART_MCR_DTR;
0b99d589 2108
50667c67 2109 if (clear & TIOCM_RTS)
0b99d589 2110 ch->ch_mostat &= ~(UART_MCR_RTS);
0b99d589 2111
50667c67 2112 if (clear & TIOCM_DTR)
0b99d589 2113 ch->ch_mostat &= ~(UART_MCR_DTR);
0b99d589
LL
2114
2115 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2116
2117 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2118
8f90ef80 2119 return 0;
0b99d589
LL
2120}
2121
2122
2123/*
2124 * dgnc_tty_send_break()
2125 *
2126 * Send a Break, called by ld.
2127 */
2128static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2129{
03425f55 2130 struct dgnc_board *bd;
0b99d589
LL
2131 struct channel_t *ch;
2132 struct un_t *un;
2133 int ret = -EIO;
2134 ulong lock_flags;
2135
2136 if (!tty || tty->magic != TTY_MAGIC)
2137 return ret;
2138
2139 un = tty->driver_data;
2140 if (!un || un->magic != DGNC_UNIT_MAGIC)
2141 return ret;
2142
2143 ch = un->un_ch;
2144 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2145 return ret;
2146
2147 bd = ch->ch_bd;
2148 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2149 return ret;
2150
2151 switch (msec) {
2152 case -1:
2153 msec = 0xFFFF;
2154 break;
2155 case 0:
2156 msec = 0;
2157 break;
2158 default:
2159 break;
2160 }
2161
0b99d589
LL
2162 DGNC_LOCK(ch->ch_lock, lock_flags);
2163
2164 ch->ch_bd->bd_ops->send_break(ch, msec);
2165
2166 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2167
8f90ef80 2168 return 0;
0b99d589
LL
2169
2170}
2171
2172
2173/*
2174 * dgnc_tty_wait_until_sent()
2175 *
2176 * wait until data has been transmitted, called by ld.
2177 */
2178static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2179{
03425f55 2180 struct dgnc_board *bd;
0b99d589
LL
2181 struct channel_t *ch;
2182 struct un_t *un;
2183 int rc;
2184
2185 if (!tty || tty->magic != TTY_MAGIC)
2186 return;
2187
2188 un = tty->driver_data;
2189 if (!un || un->magic != DGNC_UNIT_MAGIC)
2190 return;
2191
2192 ch = un->un_ch;
2193 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2194 return;
2195
2196 bd = ch->ch_bd;
2197 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2198 return;
2199
2200 rc = bd->bd_ops->drain(tty, 0);
f1e51745 2201
0b99d589 2202 return;
8b07d521 2203}
0b99d589
LL
2204
2205
2206/*
2207 * dgnc_send_xchar()
2208 *
2209 * send a high priority character, called by ld.
2210 */
2211static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2212{
03425f55 2213 struct dgnc_board *bd;
0b99d589
LL
2214 struct channel_t *ch;
2215 struct un_t *un;
2216 ulong lock_flags;
2217
2218 if (!tty || tty->magic != TTY_MAGIC)
2219 return;
2220
2221 un = tty->driver_data;
2222 if (!un || un->magic != DGNC_UNIT_MAGIC)
2223 return;
2224
2225 ch = un->un_ch;
2226 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2227 return;
2228
2229 bd = ch->ch_bd;
2230 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2231 return;
2232
0b99d589
LL
2233 printk("dgnc_tty_send_xchar start\n");
2234
2235 DGNC_LOCK(ch->ch_lock, lock_flags);
2236 bd->bd_ops->send_immediate_char(ch, c);
2237 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2238
0b99d589
LL
2239 printk("dgnc_tty_send_xchar finish\n");
2240 return;
8b07d521 2241}
0b99d589
LL
2242
2243
2244
2245
2246/*
2247 * Return modem signals to ld.
2248 */
2249static inline int dgnc_get_mstat(struct channel_t *ch)
2250{
2251 unsigned char mstat;
2252 int result = -EIO;
2253 ulong lock_flags;
2254
0b99d589 2255 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 2256 return -ENXIO;
0b99d589
LL
2257
2258 DGNC_LOCK(ch->ch_lock, lock_flags);
2259
2260 mstat = (ch->ch_mostat | ch->ch_mistat);
2261
2262 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2263
2264 result = 0;
2265
2266 if (mstat & UART_MCR_DTR)
2267 result |= TIOCM_DTR;
2268 if (mstat & UART_MCR_RTS)
2269 result |= TIOCM_RTS;
2270 if (mstat & UART_MSR_CTS)
2271 result |= TIOCM_CTS;
2272 if (mstat & UART_MSR_DSR)
2273 result |= TIOCM_DSR;
2274 if (mstat & UART_MSR_RI)
2275 result |= TIOCM_RI;
2276 if (mstat & UART_MSR_DCD)
2277 result |= TIOCM_CD;
2278
8f90ef80 2279 return result;
0b99d589
LL
2280}
2281
2282
2283
2284/*
2285 * Return modem signals to ld.
2286 */
2287static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2288{
2289 int result;
2290 int rc;
2291
0b99d589 2292 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 2293 return -ENXIO;
0b99d589
LL
2294
2295 result = dgnc_get_mstat(ch);
2296
2297 if (result < 0)
8f90ef80 2298 return -ENXIO;
0b99d589
LL
2299
2300 rc = put_user(result, value);
2301
8f90ef80 2302 return rc;
0b99d589
LL
2303}
2304
2305
2306/*
2307 * dgnc_set_modem_info()
2308 *
2309 * Set modem signals, called by ld.
2310 */
2311static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2312{
03425f55 2313 struct dgnc_board *bd;
0b99d589
LL
2314 struct channel_t *ch;
2315 struct un_t *un;
2316 int ret = -ENXIO;
2317 unsigned int arg = 0;
2318 ulong lock_flags;
2319
2320 if (!tty || tty->magic != TTY_MAGIC)
2321 return ret;
2322
2323 un = tty->driver_data;
2324 if (!un || un->magic != DGNC_UNIT_MAGIC)
2325 return ret;
2326
2327 ch = un->un_ch;
2328 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2329 return ret;
2330
2331 bd = ch->ch_bd;
2332 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2333 return ret;
2334
2335 ret = 0;
2336
0b99d589
LL
2337 ret = get_user(arg, value);
2338 if (ret)
8f90ef80 2339 return ret;
0b99d589
LL
2340
2341 switch (command) {
2342 case TIOCMBIS:
50667c67 2343 if (arg & TIOCM_RTS)
0b99d589 2344 ch->ch_mostat |= UART_MCR_RTS;
0b99d589 2345
50667c67 2346 if (arg & TIOCM_DTR)
0b99d589 2347 ch->ch_mostat |= UART_MCR_DTR;
0b99d589
LL
2348
2349 break;
2350
2351 case TIOCMBIC:
50667c67 2352 if (arg & TIOCM_RTS)
0b99d589 2353 ch->ch_mostat &= ~(UART_MCR_RTS);
0b99d589 2354
50667c67 2355 if (arg & TIOCM_DTR)
0b99d589 2356 ch->ch_mostat &= ~(UART_MCR_DTR);
0b99d589
LL
2357
2358 break;
2359
90d2a471 2360 case TIOCMSET:
0b99d589 2361
50667c67 2362 if (arg & TIOCM_RTS)
0b99d589 2363 ch->ch_mostat |= UART_MCR_RTS;
50667c67 2364 else
0b99d589 2365 ch->ch_mostat &= ~(UART_MCR_RTS);
0b99d589 2366
50667c67 2367 if (arg & TIOCM_DTR)
0b99d589 2368 ch->ch_mostat |= UART_MCR_DTR;
50667c67 2369 else
0b99d589 2370 ch->ch_mostat &= ~(UART_MCR_DTR);
0b99d589
LL
2371
2372 break;
2373
2374 default:
8f90ef80 2375 return -EINVAL;
0b99d589
LL
2376 }
2377
2378 DGNC_LOCK(ch->ch_lock, lock_flags);
2379
2380 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2381
2382 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2383
8f90ef80 2384 return 0;
0b99d589
LL
2385}
2386
2387
2388/*
8b07d521 2389 * dgnc_tty_digigeta()
0b99d589
LL
2390 *
2391 * Ioctl to get the information for ditty.
2392 *
2393 *
2394 *
2395 */
2396static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2397{
2398 struct channel_t *ch;
2399 struct un_t *un;
2400 struct digi_t tmp;
2401 ulong lock_flags;
2402
2403 if (!retinfo)
8f90ef80 2404 return -EFAULT;
0b99d589
LL
2405
2406 if (!tty || tty->magic != TTY_MAGIC)
8f90ef80 2407 return -EFAULT;
0b99d589
LL
2408
2409 un = tty->driver_data;
2410 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 2411 return -EFAULT;
0b99d589
LL
2412
2413 ch = un->un_ch;
2414 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 2415 return -EFAULT;
0b99d589
LL
2416
2417 memset(&tmp, 0, sizeof(tmp));
2418
2419 DGNC_LOCK(ch->ch_lock, lock_flags);
2420 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2421 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2422
2423 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
8f90ef80 2424 return -EFAULT;
0b99d589 2425
8f90ef80 2426 return 0;
0b99d589
LL
2427}
2428
2429
2430/*
8b07d521 2431 * dgnc_tty_digiseta()
0b99d589
LL
2432 *
2433 * Ioctl to set the information for ditty.
2434 *
2435 *
2436 *
2437 */
2438static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2439{
03425f55 2440 struct dgnc_board *bd;
0b99d589
LL
2441 struct channel_t *ch;
2442 struct un_t *un;
2443 struct digi_t new_digi;
2444 ulong lock_flags;
2445
0b99d589 2446 if (!tty || tty->magic != TTY_MAGIC)
8f90ef80 2447 return -EFAULT;
0b99d589
LL
2448
2449 un = tty->driver_data;
2450 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 2451 return -EFAULT;
0b99d589
LL
2452
2453 ch = un->un_ch;
2454 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 2455 return -EFAULT;
0b99d589
LL
2456
2457 bd = ch->ch_bd;
2458 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
8f90ef80 2459 return -EFAULT;
0b99d589 2460
f1e51745 2461 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
8f90ef80 2462 return -EFAULT;
0b99d589
LL
2463
2464 DGNC_LOCK(ch->ch_lock, lock_flags);
2465
2466 /*
2467 * Handle transistions to and from RTS Toggle.
2468 */
2469 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2470 ch->ch_mostat &= ~(UART_MCR_RTS);
2471 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2472 ch->ch_mostat |= (UART_MCR_RTS);
2473
2474 /*
2475 * Handle transistions to and from DTR Toggle.
2476 */
2477 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2478 ch->ch_mostat &= ~(UART_MCR_DTR);
2479 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2480 ch->ch_mostat |= (UART_MCR_DTR);
2481
a07bf39a 2482 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
0b99d589 2483
8b07d521 2484 if (ch->ch_digi.digi_maxcps < 1)
0b99d589
LL
2485 ch->ch_digi.digi_maxcps = 1;
2486
8b07d521 2487 if (ch->ch_digi.digi_maxcps > 10000)
0b99d589
LL
2488 ch->ch_digi.digi_maxcps = 10000;
2489
2490 if (ch->ch_digi.digi_bufsize < 10)
2491 ch->ch_digi.digi_bufsize = 10;
2492
2493 if (ch->ch_digi.digi_maxchar < 1)
2494 ch->ch_digi.digi_maxchar = 1;
2495
2496 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2497 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2498
2499 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2500 ch->ch_digi.digi_onlen = DIGI_PLEN;
2501
2502 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2503 ch->ch_digi.digi_offlen = DIGI_PLEN;
2504
2505 ch->ch_bd->bd_ops->param(tty);
2506
2507 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2508
8f90ef80 2509 return 0;
0b99d589
LL
2510}
2511
2512
2513/*
2514 * dgnc_set_termios()
2515 */
2516static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2517{
03425f55 2518 struct dgnc_board *bd;
0b99d589
LL
2519 struct channel_t *ch;
2520 struct un_t *un;
2521 unsigned long lock_flags;
2522
2523 if (!tty || tty->magic != TTY_MAGIC)
2524 return;
2525
2526 un = tty->driver_data;
2527 if (!un || un->magic != DGNC_UNIT_MAGIC)
2528 return;
2529
2530 ch = un->un_ch;
2531 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2532 return;
2533
2534 bd = ch->ch_bd;
2535 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2536 return;
2537
2538 DGNC_LOCK(ch->ch_lock, lock_flags);
2539
22e3de76
LL
2540 ch->ch_c_cflag = tty->termios.c_cflag;
2541 ch->ch_c_iflag = tty->termios.c_iflag;
2542 ch->ch_c_oflag = tty->termios.c_oflag;
2543 ch->ch_c_lflag = tty->termios.c_lflag;
2544 ch->ch_startc = tty->termios.c_cc[VSTART];
2545 ch->ch_stopc = tty->termios.c_cc[VSTOP];
0b99d589
LL
2546
2547 ch->ch_bd->bd_ops->param(tty);
2548 dgnc_carrier(ch);
2549
2550 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2551}
2552
2553
2554static void dgnc_tty_throttle(struct tty_struct *tty)
2555{
2556 struct channel_t *ch;
2557 struct un_t *un;
2558 ulong lock_flags = 0;
2559
2560 if (!tty || tty->magic != TTY_MAGIC)
2561 return;
2562
2563 un = tty->driver_data;
2564 if (!un || un->magic != DGNC_UNIT_MAGIC)
2565 return;
8b07d521 2566
90d2a471
LL
2567 ch = un->un_ch;
2568 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2569 return;
0b99d589 2570
0b99d589
LL
2571 DGNC_LOCK(ch->ch_lock, lock_flags);
2572
2573 ch->ch_flags |= (CH_FORCED_STOPI);
2574
2575 DGNC_UNLOCK(ch->ch_lock, lock_flags);
0b99d589
LL
2576}
2577
2578
2579static void dgnc_tty_unthrottle(struct tty_struct *tty)
2580{
2581 struct channel_t *ch;
2582 struct un_t *un;
2583 ulong lock_flags;
2584
2585 if (!tty || tty->magic != TTY_MAGIC)
2586 return;
2587
2588 un = tty->driver_data;
2589 if (!un || un->magic != DGNC_UNIT_MAGIC)
2590 return;
8b07d521 2591
90d2a471
LL
2592 ch = un->un_ch;
2593 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2594 return;
0b99d589 2595
0b99d589
LL
2596 DGNC_LOCK(ch->ch_lock, lock_flags);
2597
2598 ch->ch_flags &= ~(CH_FORCED_STOPI);
2599
2600 DGNC_UNLOCK(ch->ch_lock, lock_flags);
0b99d589
LL
2601}
2602
2603
2604static void dgnc_tty_start(struct tty_struct *tty)
2605{
03425f55 2606 struct dgnc_board *bd;
0b99d589
LL
2607 struct channel_t *ch;
2608 struct un_t *un;
2609 ulong lock_flags;
2610
2611 if (!tty || tty->magic != TTY_MAGIC)
2612 return;
2613
2614 un = tty->driver_data;
2615 if (!un || un->magic != DGNC_UNIT_MAGIC)
2616 return;
8b07d521 2617
90d2a471
LL
2618 ch = un->un_ch;
2619 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2620 return;
0b99d589
LL
2621
2622 bd = ch->ch_bd;
2623 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2624 return;
2625
0b99d589
LL
2626 DGNC_LOCK(ch->ch_lock, lock_flags);
2627
2628 ch->ch_flags &= ~(CH_FORCED_STOP);
2629
2630 DGNC_UNLOCK(ch->ch_lock, lock_flags);
0b99d589
LL
2631}
2632
2633
2634static void dgnc_tty_stop(struct tty_struct *tty)
2635{
03425f55 2636 struct dgnc_board *bd;
0b99d589
LL
2637 struct channel_t *ch;
2638 struct un_t *un;
2639 ulong lock_flags;
2640
2641 if (!tty || tty->magic != TTY_MAGIC)
2642 return;
2643
2644 un = tty->driver_data;
2645 if (!un || un->magic != DGNC_UNIT_MAGIC)
2646 return;
8b07d521 2647
90d2a471
LL
2648 ch = un->un_ch;
2649 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2650 return;
0b99d589
LL
2651
2652 bd = ch->ch_bd;
2653 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2654 return;
2655
0b99d589
LL
2656 DGNC_LOCK(ch->ch_lock, lock_flags);
2657
2658 ch->ch_flags |= (CH_FORCED_STOP);
2659
2660 DGNC_UNLOCK(ch->ch_lock, lock_flags);
0b99d589
LL
2661}
2662
2663
8b07d521 2664/*
0b99d589
LL
2665 * dgnc_tty_flush_chars()
2666 *
2667 * Flush the cook buffer
2668 *
2669 * Note to self, and any other poor souls who venture here:
2670 *
2671 * flush in this case DOES NOT mean dispose of the data.
2672 * instead, it means "stop buffering and send it if you
2673 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2674 *
2675 * It is also always called in interrupt context - JAR 8-Sept-99
2676 */
2677static void dgnc_tty_flush_chars(struct tty_struct *tty)
2678{
03425f55 2679 struct dgnc_board *bd;
0b99d589
LL
2680 struct channel_t *ch;
2681 struct un_t *un;
2682 ulong lock_flags;
2683
2684 if (!tty || tty->magic != TTY_MAGIC)
2685 return;
2686
2687 un = tty->driver_data;
2688 if (!un || un->magic != DGNC_UNIT_MAGIC)
2689 return;
8b07d521 2690
90d2a471
LL
2691 ch = un->un_ch;
2692 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2693 return;
0b99d589
LL
2694
2695 bd = ch->ch_bd;
2696 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2697 return;
2698
0b99d589
LL
2699 DGNC_LOCK(ch->ch_lock, lock_flags);
2700
2701 /* Do something maybe here */
2702
2703 DGNC_UNLOCK(ch->ch_lock, lock_flags);
0b99d589
LL
2704}
2705
2706
2707
2708/*
2709 * dgnc_tty_flush_buffer()
8b07d521 2710 *
0b99d589
LL
2711 * Flush Tx buffer (make in == out)
2712 */
2713static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2714{
2715 struct channel_t *ch;
2716 struct un_t *un;
2717 ulong lock_flags;
2718
2719 if (!tty || tty->magic != TTY_MAGIC)
2720 return;
2721
2722 un = tty->driver_data;
2723 if (!un || un->magic != DGNC_UNIT_MAGIC)
2724 return;
8b07d521 2725
90d2a471
LL
2726 ch = un->un_ch;
2727 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2728 return;
0b99d589 2729
0b99d589
LL
2730 DGNC_LOCK(ch->ch_lock, lock_flags);
2731
2732 ch->ch_flags &= ~CH_STOP;
2733
2734 /* Flush our write queue */
2735 ch->ch_w_head = ch->ch_w_tail;
2736
2737 /* Flush UARTs transmit FIFO */
2738 ch->ch_bd->bd_ops->flush_uart_write(ch);
2739
2740 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2741 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2742 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2743 }
2744 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2745 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2746 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2747 }
2748
2749 DGNC_UNLOCK(ch->ch_lock, lock_flags);
0b99d589
LL
2750}
2751
2752
2753
2754/*****************************************************************************
2755 *
2756 * The IOCTL function and all of its helpers
2757 *
2758 *****************************************************************************/
8b07d521 2759
0b99d589
LL
2760/*
2761 * dgnc_tty_ioctl()
2762 *
2763 * The usual assortment of ioctl's
2764 */
a31cefa2 2765static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
0b99d589
LL
2766 unsigned long arg)
2767{
03425f55 2768 struct dgnc_board *bd;
0b99d589
LL
2769 struct channel_t *ch;
2770 struct un_t *un;
2771 int rc;
2772 ulong lock_flags;
2773 void __user *uarg = (void __user *) arg;
2774
2775 if (!tty || tty->magic != TTY_MAGIC)
8f90ef80 2776 return -ENODEV;
0b99d589
LL
2777
2778 un = tty->driver_data;
2779 if (!un || un->magic != DGNC_UNIT_MAGIC)
8f90ef80 2780 return -ENODEV;
0b99d589
LL
2781
2782 ch = un->un_ch;
2783 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
8f90ef80 2784 return -ENODEV;
0b99d589
LL
2785
2786 bd = ch->ch_bd;
2787 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
8f90ef80 2788 return -ENODEV;
0b99d589 2789
0b99d589
LL
2790 DGNC_LOCK(ch->ch_lock, lock_flags);
2791
2792 if (un->un_open_count <= 0) {
0b99d589 2793 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 2794 return -EIO;
0b99d589
LL
2795 }
2796
2797 switch (cmd) {
2798
2799 /* Here are all the standard ioctl's that we MUST implement */
2800
2801 case TCSBRK:
2802 /*
8b07d521 2803 * TCSBRK is SVID version: non-zero arg --> no break
0b99d589
LL
2804 * this behaviour is exploited by tcdrain().
2805 *
2806 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2807 * between 0.25 and 0.5 seconds so we'll ask for something
2808 * in the middle: 0.375 seconds.
2809 */
2810 rc = tty_check_change(tty);
2811 DGNC_UNLOCK(ch->ch_lock, lock_flags);
50667c67 2812 if (rc)
8f90ef80 2813 return rc;
0b99d589
LL
2814
2815 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2816
f1e51745 2817 if (rc)
8f90ef80 2818 return -EINTR;
0b99d589
LL
2819
2820 DGNC_LOCK(ch->ch_lock, lock_flags);
2821
a82477c3 2822 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
0b99d589
LL
2823 ch->ch_bd->bd_ops->send_break(ch, 250);
2824 }
2825
2826 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2827
8f90ef80 2828 return 0;
0b99d589
LL
2829
2830
2831 case TCSBRKP:
90d2a471 2832 /* support for POSIX tcsendbreak()
0b99d589
LL
2833 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2834 * between 0.25 and 0.5 seconds so we'll ask for something
2835 * in the middle: 0.375 seconds.
2836 */
2837 rc = tty_check_change(tty);
2838 DGNC_UNLOCK(ch->ch_lock, lock_flags);
50667c67 2839 if (rc)
8f90ef80 2840 return rc;
0b99d589
LL
2841
2842 rc = ch->ch_bd->bd_ops->drain(tty, 0);
f1e51745 2843 if (rc)
8f90ef80 2844 return -EINTR;
0b99d589
LL
2845
2846 DGNC_LOCK(ch->ch_lock, lock_flags);
2847
2848 ch->ch_bd->bd_ops->send_break(ch, 250);
2849
2850 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2851
8f90ef80 2852 return 0;
0b99d589
LL
2853
2854 case TIOCSBRK:
2855 rc = tty_check_change(tty);
2856 DGNC_UNLOCK(ch->ch_lock, lock_flags);
50667c67 2857 if (rc)
8f90ef80 2858 return rc;
0b99d589
LL
2859
2860 rc = ch->ch_bd->bd_ops->drain(tty, 0);
f1e51745 2861 if (rc)
8f90ef80 2862 return -EINTR;
0b99d589
LL
2863
2864 DGNC_LOCK(ch->ch_lock, lock_flags);
2865
2866 ch->ch_bd->bd_ops->send_break(ch, 250);
2867
2868 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2869
8f90ef80 2870 return 0;
0b99d589
LL
2871
2872 case TIOCCBRK:
2873 /* Do Nothing */
2874 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2875 return 0;
2876
2877 case TIOCGSOFTCAR:
2878
2879 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2880
2881 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
8f90ef80 2882 return rc;
0b99d589
LL
2883
2884 case TIOCSSOFTCAR:
2885
2886 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2887 rc = get_user(arg, (unsigned long __user *) arg);
2888 if (rc)
8f90ef80 2889 return rc;
0b99d589
LL
2890
2891 DGNC_LOCK(ch->ch_lock, lock_flags);
22e3de76 2892 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
0b99d589
LL
2893 ch->ch_bd->bd_ops->param(tty);
2894 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2895
8f90ef80 2896 return 0;
8b07d521 2897
0b99d589
LL
2898 case TIOCMGET:
2899 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 2900 return dgnc_get_modem_info(ch, uarg);
0b99d589
LL
2901
2902 case TIOCMBIS:
2903 case TIOCMBIC:
2904 case TIOCMSET:
2905 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 2906 return dgnc_set_modem_info(tty, cmd, uarg);
0b99d589
LL
2907
2908 /*
2909 * Here are any additional ioctl's that we want to implement
2910 */
8b07d521
LL
2911
2912 case TCFLSH:
0b99d589
LL
2913 /*
2914 * The linux tty driver doesn't have a flush
2915 * input routine for the driver, assuming all backed
2916 * up data is in the line disc. buffers. However,
2917 * we all know that's not the case. Here, we
2918 * act on the ioctl, but then lie and say we didn't
2919 * so the line discipline will process the flush
2920 * also.
8b07d521 2921 */
0b99d589
LL
2922 rc = tty_check_change(tty);
2923 if (rc) {
2924 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 2925 return rc;
0b99d589
LL
2926 }
2927
2928 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2929 ch->ch_r_head = ch->ch_r_tail;
2930 ch->ch_bd->bd_ops->flush_uart_read(ch);
2931 /* Force queue flow control to be released, if needed */
2932 dgnc_check_queue_flow_control(ch);
2933 }
2934
2935 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2936 if (!(un->un_type == DGNC_PRINT)) {
2937 ch->ch_w_head = ch->ch_w_tail;
2938 ch->ch_bd->bd_ops->flush_uart_write(ch);
2939
2940 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2941 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2942 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2943 }
2944
2945 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2946 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2947 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2948 }
2949
2950 }
2951 }
2952
8b07d521 2953 /* pretend we didn't recognize this IOCTL */
0b99d589 2954 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 2955 return -ENOIOCTLCMD;
0b99d589
LL
2956 case TCSETSF:
2957 case TCSETSW:
2958 /*
2959 * The linux tty driver doesn't have a flush
2960 * input routine for the driver, assuming all backed
2961 * up data is in the line disc. buffers. However,
2962 * we all know that's not the case. Here, we
2963 * act on the ioctl, but then lie and say we didn't
2964 * so the line discipline will process the flush
2965 * also.
2966 */
2967 if (cmd == TCSETSF) {
2968 /* flush rx */
2969 ch->ch_flags &= ~CH_STOP;
2970 ch->ch_r_head = ch->ch_r_tail;
2971 ch->ch_bd->bd_ops->flush_uart_read(ch);
2972 /* Force queue flow control to be released, if needed */
2973 dgnc_check_queue_flow_control(ch);
2974 }
2975
2976 /* now wait for all the output to drain */
2977 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2978 rc = ch->ch_bd->bd_ops->drain(tty, 0);
f1e51745 2979 if (rc)
8f90ef80 2980 return -EINTR;
0b99d589
LL
2981
2982 /* pretend we didn't recognize this */
8f90ef80 2983 return -ENOIOCTLCMD;
0b99d589
LL
2984
2985 case TCSETAW:
2986
2987 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2988 rc = ch->ch_bd->bd_ops->drain(tty, 0);
f1e51745 2989 if (rc)
8f90ef80 2990 return -EINTR;
0b99d589
LL
2991
2992 /* pretend we didn't recognize this */
8f90ef80 2993 return -ENOIOCTLCMD;
0b99d589
LL
2994
2995 case TCXONC:
2996 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2997 /* Make the ld do it */
8f90ef80 2998 return -ENOIOCTLCMD;
0b99d589
LL
2999
3000 case DIGI_GETA:
3001 /* get information for ditty */
3002 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 3003 return dgnc_tty_digigeta(tty, uarg);
0b99d589
LL
3004
3005 case DIGI_SETAW:
3006 case DIGI_SETAF:
3007
3008 /* set information for ditty */
3009 if (cmd == (DIGI_SETAW)) {
3010
3011 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3012 rc = ch->ch_bd->bd_ops->drain(tty, 0);
f1e51745
SL
3013
3014 if (rc)
8f90ef80 3015 return -EINTR;
f1e51745 3016
0b99d589 3017 DGNC_LOCK(ch->ch_lock, lock_flags);
2000c581 3018 } else {
0b99d589
LL
3019 tty_ldisc_flush(tty);
3020 }
3021 /* fall thru */
3022
3023 case DIGI_SETA:
3024 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 3025 return dgnc_tty_digiseta(tty, uarg);
8b07d521 3026
0b99d589
LL
3027 case DIGI_LOOPBACK:
3028 {
3029 uint loopback = 0;
3030 /* Let go of locks when accessing user space, could sleep */
3031 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3032 rc = get_user(loopback, (unsigned int __user *) arg);
3033 if (rc)
8f90ef80 3034 return rc;
0b99d589
LL
3035 DGNC_LOCK(ch->ch_lock, lock_flags);
3036
8b07d521 3037 /* Enable/disable internal loopback for this port */
0b99d589
LL
3038 if (loopback)
3039 ch->ch_flags |= CH_LOOPBACK;
3040 else
3041 ch->ch_flags &= ~(CH_LOOPBACK);
3042
3043 ch->ch_bd->bd_ops->param(tty);
3044 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 3045 return 0;
90d2a471 3046 }
0b99d589
LL
3047
3048 case DIGI_GETCUSTOMBAUD:
3049 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3050 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
8f90ef80 3051 return rc;
0b99d589
LL
3052
3053 case DIGI_SETCUSTOMBAUD:
3054 {
93c76c9c 3055 int new_rate;
0b99d589
LL
3056 /* Let go of locks when accessing user space, could sleep */
3057 DGNC_UNLOCK(ch->ch_lock, lock_flags);
93c76c9c 3058 rc = get_user(new_rate, (int __user *) arg);
0b99d589 3059 if (rc)
8f90ef80 3060 return rc;
0b99d589
LL
3061 DGNC_LOCK(ch->ch_lock, lock_flags);
3062 dgnc_set_custom_speed(ch, new_rate);
3063 ch->ch_bd->bd_ops->param(tty);
3064 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 3065 return 0;
90d2a471 3066 }
0b99d589
LL
3067
3068 /*
3069 * This ioctl allows insertion of a character into the front
3070 * of any pending data to be transmitted.
3071 *
3072 * This ioctl is to satify the "Send Character Immediate"
3073 * call that the RealPort protocol spec requires.
3074 */
3075 case DIGI_REALPORT_SENDIMMEDIATE:
3076 {
3077 unsigned char c;
3078 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3079 rc = get_user(c, (unsigned char __user *) arg);
3080 if (rc)
8f90ef80 3081 return rc;
0b99d589
LL
3082 DGNC_LOCK(ch->ch_lock, lock_flags);
3083 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3084 DGNC_UNLOCK(ch->ch_lock, lock_flags);
8f90ef80 3085 return 0;
0b99d589
LL
3086 }
3087
3088 /*
3089 * This ioctl returns all the current counts for the port.
3090 *
3091 * This ioctl is to satify the "Line Error Counters"
3092 * call that the RealPort protocol spec requires.
3093 */
3094 case DIGI_REALPORT_GETCOUNTERS:
3095 {
3096 struct digi_getcounter buf;
3097
3098 buf.norun = ch->ch_err_overrun;
3099 buf.noflow = 0; /* The driver doesn't keep this stat */
3100 buf.nframe = ch->ch_err_frame;
3101 buf.nparity = ch->ch_err_parity;
3102 buf.nbreak = ch->ch_err_break;
3103 buf.rbytes = ch->ch_rxcount;
3104 buf.tbytes = ch->ch_txcount;
3105
3106 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3107
50667c67 3108 if (copy_to_user(uarg, &buf, sizeof(buf)))
8f90ef80 3109 return -EFAULT;
50667c67 3110
8f90ef80 3111 return 0;
0b99d589
LL
3112 }
3113
3114 /*
3115 * This ioctl returns all current events.
3116 *
3117 * This ioctl is to satify the "Event Reporting"
3118 * call that the RealPort protocol spec requires.
90d2a471 3119 */
0b99d589
LL
3120 case DIGI_REALPORT_GETEVENTS:
3121 {
3122 unsigned int events = 0;
3123
3124 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3125 if (ch->ch_flags & CH_BREAK_SENDING)
3126 events |= EV_TXB;
50667c67 3127 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
0b99d589 3128 events |= (EV_OPU | EV_OPS);
50667c67 3129
0b99d589
LL
3130 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) {
3131 events |= (EV_IPU | EV_IPS);
3132 }
3133
3134 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3135 rc = put_user(events, (unsigned int __user *) arg);
8f90ef80 3136 return rc;
0b99d589
LL
3137 }
3138
3139 /*
3140 * This ioctl returns TOUT and TIN counters based
3141 * upon the values passed in by the RealPort Server.
3142 * It also passes back whether the UART Transmitter is
3143 * empty as well.
90d2a471 3144 */
0b99d589
LL
3145 case DIGI_REALPORT_GETBUFFERS:
3146 {
3147 struct digi_getbuffer buf;
3148 int tdist;
3149 int count;
3150
3151 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3152
3153 /*
3154 * Get data from user first.
3155 */
50667c67 3156 if (copy_from_user(&buf, uarg, sizeof(buf)))
8f90ef80 3157 return -EFAULT;
0b99d589
LL
3158
3159 DGNC_LOCK(ch->ch_lock, lock_flags);
3160
3161 /*
3162 * Figure out how much data is in our RX and TX queues.
3163 */
3164 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3165 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3166
3167 /*
3168 * Is the UART empty? Add that value to whats in our TX queue.
3169 */
3170 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3171
3172 /*
3173 * Figure out how much data the RealPort Server believes should
3174 * be in our TX queue.
3175 */
3176 tdist = (buf.tIn - buf.tOut) & 0xffff;
3177
3178 /*
3179 * If we have more data than the RealPort Server believes we
3180 * should have, reduce our count to its amount.
3181 *
3182 * This count difference CAN happen because the Linux LD can
3183 * insert more characters into our queue for OPOST processing
3184 * that the RealPort Server doesn't know about.
3185 */
50667c67 3186 if (buf.txbuf > tdist)
0b99d589 3187 buf.txbuf = tdist;
0b99d589
LL
3188
3189 /*
3190 * Report whether our queue and UART TX are completely empty.
3191 */
50667c67 3192 if (count)
0b99d589 3193 buf.txdone = 0;
50667c67 3194 else
0b99d589 3195 buf.txdone = 1;
0b99d589
LL
3196
3197 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3198
50667c67 3199 if (copy_to_user(uarg, &buf, sizeof(buf)))
8f90ef80 3200 return -EFAULT;
50667c67 3201
8f90ef80 3202 return 0;
0b99d589
LL
3203 }
3204 default:
3205 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3206
8f90ef80 3207 return -ENOIOCTLCMD;
0b99d589 3208 }
0b99d589 3209}