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