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