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