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