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