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