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