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