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