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