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