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