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