2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
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)
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
31 /************************************************************************
33 * This file implements the tty driver functionality for the
34 * Neo and ClassicBoard PCI based product lines.
36 ************************************************************************
40 #include <linux/kernel.h>
41 #include <linux/sched.h> /* For jiffies, task states */
42 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
43 #include <linux/module.h>
44 #include <linux/ctype.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial_reg.h>
48 #include <linux/slab.h>
49 #include <linux/delay.h> /* For udelay */
50 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
51 #include <linux/pci.h>
53 #include "dgnc_driver.h"
55 #include "dgnc_types.h"
58 #include "dpacompat.h"
59 #include "dgnc_sysfs.h"
60 #include "dgnc_utils.h"
62 #define init_MUTEX(sem) sema_init(sem, 1)
63 #define DECLARE_MUTEX(name) \
64 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
69 static struct dgnc_board
*dgnc_BoardsByMajor
[256];
70 static unsigned char *dgnc_TmpWriteBuf
;
71 static DECLARE_MUTEX(dgnc_TmpWriteSem
);
74 * Default transparent print information.
76 static struct digi_t dgnc_digi_init
= {
77 .digi_flags
= DIGI_COOK
, /* Flags */
78 .digi_maxcps
= 100, /* Max CPS */
79 .digi_maxchar
= 50, /* Max chars in print queue */
80 .digi_bufsize
= 100, /* Printer buffer size */
81 .digi_onlen
= 4, /* size of printer on string */
82 .digi_offlen
= 4, /* size of printer off string */
83 .digi_onstr
= "\033[5i", /* ANSI printer on string ] */
84 .digi_offstr
= "\033[4i", /* ANSI printer off string ] */
85 .digi_term
= "ansi" /* default terminal type */
90 * Define a local default termios struct. All ports will be created
91 * with this termios initially.
93 * This defines a raw port at 9600 baud, 8 data bits, no parity,
96 static struct ktermios DgncDefaultTermios
= {
97 .c_iflag
= (DEFAULT_IFLAGS
), /* iflags */
98 .c_oflag
= (DEFAULT_OFLAGS
), /* oflags */
99 .c_cflag
= (DEFAULT_CFLAGS
), /* cflags */
100 .c_lflag
= (DEFAULT_LFLAGS
), /* lflags */
106 /* Our function prototypes */
107 static int dgnc_tty_open(struct tty_struct
*tty
, struct file
*file
);
108 static void dgnc_tty_close(struct tty_struct
*tty
, struct file
*file
);
109 static int dgnc_block_til_ready(struct tty_struct
*tty
, struct file
*file
, struct channel_t
*ch
);
110 static int dgnc_tty_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
);
111 static int dgnc_tty_digigeta(struct tty_struct
*tty
, struct digi_t __user
*retinfo
);
112 static int dgnc_tty_digiseta(struct tty_struct
*tty
, struct digi_t __user
*new_info
);
113 static int dgnc_tty_write_room(struct tty_struct
*tty
);
114 static int dgnc_tty_put_char(struct tty_struct
*tty
, unsigned char c
);
115 static int dgnc_tty_chars_in_buffer(struct tty_struct
*tty
);
116 static void dgnc_tty_start(struct tty_struct
*tty
);
117 static void dgnc_tty_stop(struct tty_struct
*tty
);
118 static void dgnc_tty_throttle(struct tty_struct
*tty
);
119 static void dgnc_tty_unthrottle(struct tty_struct
*tty
);
120 static void dgnc_tty_flush_chars(struct tty_struct
*tty
);
121 static void dgnc_tty_flush_buffer(struct tty_struct
*tty
);
122 static void dgnc_tty_hangup(struct tty_struct
*tty
);
123 static int dgnc_set_modem_info(struct tty_struct
*tty
, unsigned int command
, unsigned int __user
*value
);
124 static int dgnc_get_modem_info(struct channel_t
*ch
, unsigned int __user
*value
);
125 static int dgnc_tty_tiocmget(struct tty_struct
*tty
);
126 static int dgnc_tty_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
);
127 static int dgnc_tty_send_break(struct tty_struct
*tty
, int msec
);
128 static void dgnc_tty_wait_until_sent(struct tty_struct
*tty
, int timeout
);
129 static int dgnc_tty_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
);
130 static void dgnc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
);
131 static void dgnc_tty_send_xchar(struct tty_struct
*tty
, char ch
);
134 static const struct tty_operations dgnc_tty_ops
= {
135 .open
= dgnc_tty_open
,
136 .close
= dgnc_tty_close
,
137 .write
= dgnc_tty_write
,
138 .write_room
= dgnc_tty_write_room
,
139 .flush_buffer
= dgnc_tty_flush_buffer
,
140 .chars_in_buffer
= dgnc_tty_chars_in_buffer
,
141 .flush_chars
= dgnc_tty_flush_chars
,
142 .ioctl
= dgnc_tty_ioctl
,
143 .set_termios
= dgnc_tty_set_termios
,
144 .stop
= dgnc_tty_stop
,
145 .start
= dgnc_tty_start
,
146 .throttle
= dgnc_tty_throttle
,
147 .unthrottle
= dgnc_tty_unthrottle
,
148 .hangup
= dgnc_tty_hangup
,
149 .put_char
= dgnc_tty_put_char
,
150 .tiocmget
= dgnc_tty_tiocmget
,
151 .tiocmset
= dgnc_tty_tiocmset
,
152 .break_ctl
= dgnc_tty_send_break
,
153 .wait_until_sent
= dgnc_tty_wait_until_sent
,
154 .send_xchar
= dgnc_tty_send_xchar
157 /************************************************************************
159 * TTY Initialization/Cleanup Functions
161 ************************************************************************/
166 * Initialize any global tty related data before we download any boards.
168 int dgnc_tty_preinit(void)
171 * Allocate a buffer for doing the copy from user space to
172 * kernel space in dgnc_write(). We only use one buffer and
173 * control access to it with a semaphore. If we are paging, we
174 * are already in trouble so one buffer won't hurt much anyway.
176 * We are okay to sleep in the malloc, as this routine
177 * is only called during module load, (not in interrupt context),
178 * and with no locks held.
180 dgnc_TmpWriteBuf
= kmalloc(WRITEBUFLEN
, GFP_KERNEL
);
182 if (!dgnc_TmpWriteBuf
)
190 * dgnc_tty_register()
192 * Init the tty subsystem for this board.
194 int dgnc_tty_register(struct dgnc_board
*brd
)
198 brd
->SerialDriver
.magic
= TTY_DRIVER_MAGIC
;
200 snprintf(brd
->SerialName
, MAXTTYNAMELEN
, "tty_dgnc_%d_", brd
->boardnum
);
202 brd
->SerialDriver
.name
= brd
->SerialName
;
203 brd
->SerialDriver
.name_base
= 0;
204 brd
->SerialDriver
.major
= 0;
205 brd
->SerialDriver
.minor_start
= 0;
206 brd
->SerialDriver
.num
= brd
->maxports
;
207 brd
->SerialDriver
.type
= TTY_DRIVER_TYPE_SERIAL
;
208 brd
->SerialDriver
.subtype
= SERIAL_TYPE_NORMAL
;
209 brd
->SerialDriver
.init_termios
= DgncDefaultTermios
;
210 brd
->SerialDriver
.driver_name
= DRVSTR
;
211 brd
->SerialDriver
.flags
= (TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
| TTY_DRIVER_HARDWARE_BREAK
);
214 * The kernel wants space to store pointers to
215 * tty_struct's and termios's.
217 brd
->SerialDriver
.ttys
= kcalloc(brd
->maxports
, sizeof(*brd
->SerialDriver
.ttys
), GFP_KERNEL
);
218 if (!brd
->SerialDriver
.ttys
)
221 kref_init(&brd
->SerialDriver
.kref
);
222 brd
->SerialDriver
.termios
= kcalloc(brd
->maxports
, sizeof(*brd
->SerialDriver
.termios
), GFP_KERNEL
);
223 if (!brd
->SerialDriver
.termios
)
227 * Entry points for driver. Called by the kernel from
228 * tty_io.c and n_tty.c.
230 tty_set_operations(&brd
->SerialDriver
, &dgnc_tty_ops
);
232 if (!brd
->dgnc_Major_Serial_Registered
) {
233 /* Register tty devices */
234 rc
= tty_register_driver(&brd
->SerialDriver
);
236 APR(("Can't register tty device (%d)\n", rc
));
239 brd
->dgnc_Major_Serial_Registered
= TRUE
;
243 * If we're doing transparent print, we have to do all of the above
244 * again, separately so we don't get the LD confused about what major
245 * we are when we get into the dgnc_tty_open() routine.
247 brd
->PrintDriver
.magic
= TTY_DRIVER_MAGIC
;
248 snprintf(brd
->PrintName
, MAXTTYNAMELEN
, "pr_dgnc_%d_", brd
->boardnum
);
250 brd
->PrintDriver
.name
= brd
->PrintName
;
251 brd
->PrintDriver
.name_base
= 0;
252 brd
->PrintDriver
.major
= brd
->SerialDriver
.major
;
253 brd
->PrintDriver
.minor_start
= 0x80;
254 brd
->PrintDriver
.num
= brd
->maxports
;
255 brd
->PrintDriver
.type
= TTY_DRIVER_TYPE_SERIAL
;
256 brd
->PrintDriver
.subtype
= SERIAL_TYPE_NORMAL
;
257 brd
->PrintDriver
.init_termios
= DgncDefaultTermios
;
258 brd
->PrintDriver
.driver_name
= DRVSTR
;
259 brd
->PrintDriver
.flags
= (TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
| TTY_DRIVER_HARDWARE_BREAK
);
262 * The kernel wants space to store pointers to
263 * tty_struct's and termios's. Must be separated from
264 * the Serial Driver so we don't get confused
266 brd
->PrintDriver
.ttys
= kcalloc(brd
->maxports
, sizeof(*brd
->PrintDriver
.ttys
), GFP_KERNEL
);
267 if (!brd
->PrintDriver
.ttys
)
269 kref_init(&brd
->PrintDriver
.kref
);
270 brd
->PrintDriver
.termios
= kcalloc(brd
->maxports
, sizeof(*brd
->PrintDriver
.termios
), GFP_KERNEL
);
271 if (!brd
->PrintDriver
.termios
)
275 * Entry points for driver. Called by the kernel from
276 * tty_io.c and n_tty.c.
278 tty_set_operations(&brd
->PrintDriver
, &dgnc_tty_ops
);
280 if (!brd
->dgnc_Major_TransparentPrint_Registered
) {
281 /* Register Transparent Print devices */
282 rc
= tty_register_driver(&brd
->PrintDriver
);
284 APR(("Can't register Transparent Print device (%d)\n", rc
));
287 brd
->dgnc_Major_TransparentPrint_Registered
= TRUE
;
290 dgnc_BoardsByMajor
[brd
->SerialDriver
.major
] = brd
;
291 brd
->dgnc_Serial_Major
= brd
->SerialDriver
.major
;
292 brd
->dgnc_TransparentPrint_Major
= brd
->PrintDriver
.major
;
301 * Init the tty subsystem. Called once per board after board has been
302 * downloaded and init'ed.
304 int dgnc_tty_init(struct dgnc_board
*brd
)
308 struct channel_t
*ch
;
314 * Initialize board structure elements.
317 vaddr
= brd
->re_map_membase
;
319 brd
->nasync
= brd
->maxports
;
322 * Allocate channel memory that might not have been allocated
323 * when the driver was first loaded.
325 for (i
= 0; i
< brd
->nasync
; i
++) {
326 if (!brd
->channels
[i
]) {
329 * Okay to malloc with GFP_KERNEL, we are not at
330 * interrupt context, and there are no locks held.
332 brd
->channels
[i
] = kzalloc(sizeof(*brd
->channels
[i
]), GFP_KERNEL
);
336 ch
= brd
->channels
[0];
337 vaddr
= brd
->re_map_membase
;
339 /* Set up channel variables */
340 for (i
= 0; i
< brd
->nasync
; i
++, ch
= brd
->channels
[i
]) {
342 if (!brd
->channels
[i
])
345 spin_lock_init(&ch
->ch_lock
);
347 /* Store all our magic numbers */
348 ch
->magic
= DGNC_CHANNEL_MAGIC
;
349 ch
->ch_tun
.magic
= DGNC_UNIT_MAGIC
;
350 ch
->ch_tun
.un_ch
= ch
;
351 ch
->ch_tun
.un_type
= DGNC_SERIAL
;
352 ch
->ch_tun
.un_dev
= i
;
354 ch
->ch_pun
.magic
= DGNC_UNIT_MAGIC
;
355 ch
->ch_pun
.un_ch
= ch
;
356 ch
->ch_pun
.un_type
= DGNC_PRINT
;
357 ch
->ch_pun
.un_dev
= i
+ 128;
359 if (brd
->bd_uart_offset
== 0x200)
360 ch
->ch_neo_uart
= vaddr
+ (brd
->bd_uart_offset
* i
);
362 ch
->ch_cls_uart
= vaddr
+ (brd
->bd_uart_offset
* i
);
366 ch
->ch_digi
= dgnc_digi_init
;
368 /* .25 second delay */
369 ch
->ch_close_delay
= 250;
371 init_waitqueue_head(&ch
->ch_flags_wait
);
372 init_waitqueue_head(&ch
->ch_tun
.un_flags_wait
);
373 init_waitqueue_head(&ch
->ch_pun
.un_flags_wait
);
374 init_waitqueue_head(&ch
->ch_sniff_wait
);
377 struct device
*classp
;
379 classp
= tty_register_device(&brd
->SerialDriver
, i
,
380 &(ch
->ch_bd
->pdev
->dev
));
381 ch
->ch_tun
.un_sysfs
= classp
;
382 dgnc_create_tty_sysfs(&ch
->ch_tun
, classp
);
384 classp
= tty_register_device(&brd
->PrintDriver
, i
,
385 &(ch
->ch_bd
->pdev
->dev
));
386 ch
->ch_pun
.un_sysfs
= classp
;
387 dgnc_create_tty_sysfs(&ch
->ch_pun
, classp
);
397 * dgnc_tty_post_uninit()
399 * UnInitialize any global tty related data.
401 void dgnc_tty_post_uninit(void)
403 kfree(dgnc_TmpWriteBuf
);
404 dgnc_TmpWriteBuf
= NULL
;
411 * Uninitialize the TTY portion of this driver. Free all memory and
414 void dgnc_tty_uninit(struct dgnc_board
*brd
)
418 if (brd
->dgnc_Major_Serial_Registered
) {
419 dgnc_BoardsByMajor
[brd
->SerialDriver
.major
] = NULL
;
420 brd
->dgnc_Serial_Major
= 0;
421 for (i
= 0; i
< brd
->nasync
; i
++) {
422 dgnc_remove_tty_sysfs(brd
->channels
[i
]->ch_tun
.un_sysfs
);
423 tty_unregister_device(&brd
->SerialDriver
, i
);
425 tty_unregister_driver(&brd
->SerialDriver
);
426 brd
->dgnc_Major_Serial_Registered
= FALSE
;
429 if (brd
->dgnc_Major_TransparentPrint_Registered
) {
430 dgnc_BoardsByMajor
[brd
->PrintDriver
.major
] = NULL
;
431 brd
->dgnc_TransparentPrint_Major
= 0;
432 for (i
= 0; i
< brd
->nasync
; i
++) {
433 dgnc_remove_tty_sysfs(brd
->channels
[i
]->ch_pun
.un_sysfs
);
434 tty_unregister_device(&brd
->PrintDriver
, i
);
436 tty_unregister_driver(&brd
->PrintDriver
);
437 brd
->dgnc_Major_TransparentPrint_Registered
= FALSE
;
440 kfree(brd
->SerialDriver
.ttys
);
441 brd
->SerialDriver
.ttys
= NULL
;
442 kfree(brd
->PrintDriver
.ttys
);
443 brd
->PrintDriver
.ttys
= NULL
;
447 #define TMPBUFLEN (1024)
450 * dgnc_sniff - Dump data out to the "sniff" buffer if the
451 * proc sniff file is opened...
453 void dgnc_sniff_nowait_nolock(struct channel_t
*ch
, unsigned char *text
, unsigned char *buf
, int len
)
465 tmpbuf
= kzalloc(TMPBUFLEN
, GFP_ATOMIC
);
470 /* Leave if sniff not open */
471 if (!(ch
->ch_sniff_flags
& SNIFF_OPEN
))
474 do_gettimeofday(&tv
);
476 /* Create our header for data dump */
477 p
+= sprintf(p
, "<%ld %ld><%s><", tv
.tv_sec
, tv
.tv_usec
, text
);
478 tmpbuflen
= p
- tmpbuf
;
483 for (i
= 0; i
< len
&& tmpbuflen
< (TMPBUFLEN
- 4); i
++) {
484 p
+= sprintf(p
, "%02x ", *buf
);
486 tmpbuflen
= p
- tmpbuf
;
489 if (tmpbuflen
< (TMPBUFLEN
- 4)) {
491 p
+= sprintf(p
- 1, "%s\n", ">");
493 p
+= sprintf(p
, "%s\n", ">");
499 nbuf
= strlen(tmpbuf
);
503 * Loop while data remains.
505 while (nbuf
> 0 && ch
->ch_sniff_buf
) {
507 * Determine the amount of available space left in the
508 * buffer. If there's none, wait until some appears.
510 n
= (ch
->ch_sniff_out
- ch
->ch_sniff_in
- 1) & SNIFF_MASK
;
513 * If there is no space left to write to in our sniff buffer,
514 * we have no choice but to drop the data.
515 * We *cannot* sleep here waiting for space, because this
516 * function was probably called by the interrupt/timer routines!
522 * Copy as much data as will fit.
528 r
= SNIFF_MAX
- ch
->ch_sniff_in
;
531 memcpy(ch
->ch_sniff_buf
+ ch
->ch_sniff_in
, p
, r
);
539 memcpy(ch
->ch_sniff_buf
+ ch
->ch_sniff_in
, p
, n
);
541 ch
->ch_sniff_in
+= n
;
546 * Wakeup any thread waiting for data
548 if (ch
->ch_sniff_flags
& SNIFF_WAIT_DATA
) {
549 ch
->ch_sniff_flags
&= ~SNIFF_WAIT_DATA
;
550 wake_up_interruptible(&ch
->ch_sniff_wait
);
555 * If the user sent us too much data to push into our tmpbuf,
556 * we need to keep looping around on all the data.
563 } while (too_much_data
);
570 /*=======================================================================
572 * dgnc_wmove - Write data to transmit queue.
574 * ch - Pointer to channel structure.
575 * buf - Poiter to characters to be moved.
576 * n - Number of characters to move.
578 *=======================================================================*/
579 static void dgnc_wmove(struct channel_t
*ch
, char *buf
, uint n
)
584 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
587 head
= ch
->ch_w_head
& WQUEUEMASK
;
590 * If the write wraps over the top of the circular buffer,
591 * move the portion up to the wrap point, and reset the
592 * pointers to the bottom.
594 remain
= WQUEUESIZE
- head
;
598 memcpy(ch
->ch_wqueue
+ head
, buf
, remain
);
608 memcpy(ch
->ch_wqueue
+ head
, buf
, remain
);
613 ch
->ch_w_head
= head
;
619 /*=======================================================================
621 * dgnc_input - Process received data.
623 * ch - Pointer to channel structure.
625 *=======================================================================*/
626 void dgnc_input(struct channel_t
*ch
)
628 struct dgnc_board
*bd
;
629 struct tty_struct
*tp
;
630 struct tty_ldisc
*ld
;
642 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
645 tp
= ch
->ch_tun
.un_tty
;
648 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
651 spin_lock_irqsave(&ch
->ch_lock
, flags
);
654 * Figure the number of characters in the buffer.
655 * Exit immediately if none.
658 head
= ch
->ch_r_head
& rmask
;
659 tail
= ch
->ch_r_tail
& rmask
;
660 data_len
= (head
- tail
) & rmask
;
663 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
668 * If the device is not open, or CREAD is off,
669 * flush input data and return immediately.
671 if (!tp
|| (tp
->magic
!= TTY_MAGIC
) || !(ch
->ch_tun
.un_flags
& UN_ISOPEN
) ||
672 !(tp
->termios
.c_cflag
& CREAD
) || (ch
->ch_tun
.un_flags
& UN_CLOSING
)) {
674 ch
->ch_r_head
= tail
;
676 /* Force queue flow control to be released, if needed */
677 dgnc_check_queue_flow_control(ch
);
679 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
684 * If we are throttled, simply don't read any data.
686 if (ch
->ch_flags
& CH_FORCED_STOPI
) {
687 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
691 flip_len
= TTY_FLIPBUF_SIZE
;
693 /* Chop down the length, if needed */
694 len
= min(data_len
, flip_len
);
695 len
= min(len
, (N_TTY_BUF_SIZE
- 1));
697 ld
= tty_ldisc_ref(tp
);
701 * If the DONT_FLIP flag is on, don't flush our buffer, and act
702 * like the ld doesn't have any space to put the data right now.
704 if (test_bit(TTY_DONT_FLIP
, &tp
->flags
))
709 * If we were unable to get a reference to the ld,
710 * don't flush our buffer, and act like the ld doesn't
711 * have any space to put the data right now.
717 * If ld doesn't have a pointer to a receive_buf function,
718 * flush the data, then act like the ld doesn't have any
719 * space to put the data right now.
721 if (!ld
->ops
->receive_buf
) {
722 ch
->ch_r_head
= ch
->ch_r_tail
;
728 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
735 * The tty layer in the kernel has changed in 2.6.16+.
737 * The flip buffers in the tty structure are no longer exposed,
738 * and probably will be going away eventually.
740 * If we are completely raw, we don't need to go through a lot
741 * of the tty layers that exist.
742 * In this case, we take the shortest and fastest route we
743 * can to relay the data to the user.
745 * On the other hand, if we are not raw, we need to go through
746 * the new 2.6.16+ tty layer, which has its API more well defined.
748 len
= tty_buffer_request_room(tp
->port
, len
);
752 * n now contains the most amount of data we can copy,
753 * bounded either by how much the Linux tty layer can handle,
754 * or the amount of data the card actually has pending...
757 s
= ((head
>= tail
) ? head
: RQUEUESIZE
) - tail
;
764 * If conditions are such that ld needs to see all
765 * UART errors, we will have to walk each character
766 * and error byte and send them to the buffer one at
769 if (I_PARMRK(tp
) || I_BRKINT(tp
) || I_INPCK(tp
)) {
770 for (i
= 0; i
< s
; i
++) {
771 if (*(ch
->ch_equeue
+ tail
+ i
) & UART_LSR_BI
)
772 tty_insert_flip_char(tp
->port
, *(ch
->ch_rqueue
+ tail
+ i
), TTY_BREAK
);
773 else if (*(ch
->ch_equeue
+ tail
+ i
) & UART_LSR_PE
)
774 tty_insert_flip_char(tp
->port
, *(ch
->ch_rqueue
+ tail
+ i
), TTY_PARITY
);
775 else if (*(ch
->ch_equeue
+ tail
+ i
) & UART_LSR_FE
)
776 tty_insert_flip_char(tp
->port
, *(ch
->ch_rqueue
+ tail
+ i
), TTY_FRAME
);
778 tty_insert_flip_char(tp
->port
, *(ch
->ch_rqueue
+ tail
+ i
), TTY_NORMAL
);
781 tty_insert_flip_string(tp
->port
, ch
->ch_rqueue
+ tail
, s
);
784 dgnc_sniff_nowait_nolock(ch
, "USER READ", ch
->ch_rqueue
+ tail
, s
);
788 /* Flip queue if needed */
792 ch
->ch_r_tail
= tail
& rmask
;
793 ch
->ch_e_tail
= tail
& rmask
;
794 dgnc_check_queue_flow_control(ch
);
795 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
797 /* Tell the tty layer its okay to "eat" the data now */
798 tty_flip_buffer_push(tp
->port
);
805 /************************************************************************
806 * Determines when CARRIER changes state and takes appropriate
808 ************************************************************************/
809 void dgnc_carrier(struct channel_t
*ch
)
811 struct dgnc_board
*bd
;
813 int virt_carrier
= 0;
814 int phys_carrier
= 0;
816 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
821 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
824 if (ch
->ch_mistat
& UART_MSR_DCD
)
827 if (ch
->ch_digi
.digi_flags
& DIGI_FORCEDCD
)
830 if (ch
->ch_c_cflag
& CLOCAL
)
834 * Test for a VIRTUAL carrier transition to HIGH.
836 if (((ch
->ch_flags
& CH_FCAR
) == 0) && (virt_carrier
== 1)) {
839 * When carrier rises, wake any threads waiting
840 * for carrier in the open routine.
843 if (waitqueue_active(&(ch
->ch_flags_wait
)))
844 wake_up_interruptible(&ch
->ch_flags_wait
);
848 * Test for a PHYSICAL carrier transition to HIGH.
850 if (((ch
->ch_flags
& CH_CD
) == 0) && (phys_carrier
== 1)) {
853 * When carrier rises, wake any threads waiting
854 * for carrier in the open routine.
857 if (waitqueue_active(&(ch
->ch_flags_wait
)))
858 wake_up_interruptible(&ch
->ch_flags_wait
);
862 * Test for a PHYSICAL transition to low, so long as we aren't
863 * currently ignoring physical transitions (which is what "virtual
864 * carrier" indicates).
866 * The transition of the virtual carrier to low really doesn't
867 * matter... it really only means "ignore carrier state", not
868 * "make pretend that carrier is there".
870 if ((virt_carrier
== 0) && ((ch
->ch_flags
& CH_CD
) != 0) &&
871 (phys_carrier
== 0)) {
874 * When carrier drops:
876 * Drop carrier on all open units.
878 * Flush queues, waking up any task waiting in the
881 * Send a hangup to the control terminal.
883 * Enable all select calls.
885 if (waitqueue_active(&(ch
->ch_flags_wait
)))
886 wake_up_interruptible(&ch
->ch_flags_wait
);
888 if (ch
->ch_tun
.un_open_count
> 0)
889 tty_hangup(ch
->ch_tun
.un_tty
);
891 if (ch
->ch_pun
.un_open_count
> 0)
892 tty_hangup(ch
->ch_pun
.un_tty
);
896 * Make sure that our cached values reflect the current reality.
898 if (virt_carrier
== 1)
899 ch
->ch_flags
|= CH_FCAR
;
901 ch
->ch_flags
&= ~CH_FCAR
;
903 if (phys_carrier
== 1)
904 ch
->ch_flags
|= CH_CD
;
906 ch
->ch_flags
&= ~CH_CD
;
910 * Assign the custom baud rate to the channel structure
912 static void dgnc_set_custom_speed(struct channel_t
*ch
, uint newrate
)
921 ch
->ch_custom_speed
= 0;
926 * Since the divisor is stored in a 16-bit integer, we make sure
927 * we don't allow any rates smaller than a 16-bit integer would allow.
928 * And of course, rates above the dividend won't fly.
930 if (newrate
&& newrate
< ((ch
->ch_bd
->bd_dividend
/ 0xFFFF) + 1))
931 newrate
= ((ch
->ch_bd
->bd_dividend
/ 0xFFFF) + 1);
933 if (newrate
&& newrate
> ch
->ch_bd
->bd_dividend
)
934 newrate
= ch
->ch_bd
->bd_dividend
;
937 testdiv
= ch
->ch_bd
->bd_dividend
/ newrate
;
940 * If we try to figure out what rate the board would use
941 * with the test divisor, it will be either equal or higher
942 * than the requested baud rate. If we then determine the
943 * rate with a divisor one higher, we will get the next lower
944 * supported rate below the requested.
946 testrate_high
= ch
->ch_bd
->bd_dividend
/ testdiv
;
947 testrate_low
= ch
->ch_bd
->bd_dividend
/ (testdiv
+ 1);
950 * If the rate for the requested divisor is correct, just
951 * use it and be done.
953 if (testrate_high
!= newrate
) {
955 * Otherwise, pick the rate that is closer (i.e. whichever rate
956 * has a smaller delta).
958 deltahigh
= testrate_high
- newrate
;
959 deltalow
= newrate
- testrate_low
;
961 if (deltahigh
< deltalow
)
962 newrate
= testrate_high
;
964 newrate
= testrate_low
;
968 ch
->ch_custom_speed
= newrate
;
972 void dgnc_check_queue_flow_control(struct channel_t
*ch
)
976 /* Store how much space we have left in the queue */
977 qleft
= ch
->ch_r_tail
- ch
->ch_r_head
- 1;
979 qleft
+= RQUEUEMASK
+ 1;
982 * Check to see if we should enforce flow control on our queue because
983 * the ld (or user) isn't reading data out of our queue fast enuf.
985 * NOTE: This is done based on what the current flow control of the
988 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
989 * This will cause the UART's FIFO to back up, and force
990 * the RTS signal to be dropped.
991 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
992 * the other side, in hopes it will stop sending data to us.
993 * 3) NONE - Nothing we can do. We will simply drop any extra data
994 * that gets sent into us when the queue fills up.
998 if (ch
->ch_digi
.digi_flags
& CTSPACE
|| ch
->ch_c_cflag
& CRTSCTS
) {
999 if (!(ch
->ch_flags
& CH_RECEIVER_OFF
)) {
1000 ch
->ch_bd
->bd_ops
->disable_receiver(ch
);
1001 ch
->ch_flags
|= (CH_RECEIVER_OFF
);
1005 else if (ch
->ch_c_iflag
& IXOFF
) {
1006 if (ch
->ch_stops_sent
<= MAX_STOPS_SENT
) {
1007 ch
->ch_bd
->bd_ops
->send_stop_character(ch
);
1008 ch
->ch_stops_sent
++;
1013 /* Empty... Can't do anything about the impending overflow... */
1018 * Check to see if we should unenforce flow control because
1019 * ld (or user) finally read enuf data out of our queue.
1021 * NOTE: This is done based on what the current flow control of the
1024 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1025 * This will cause the UART's FIFO to raise RTS back up,
1026 * which will allow the other side to start sending data again.
1027 * 2) SWFLOW (IXOFF) - Send a start character to
1028 * the other side, so it will start sending data to us again.
1029 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1030 * other side, we don't need to do anything now.
1032 if (qleft
> (RQUEUESIZE
/ 2)) {
1034 if (ch
->ch_digi
.digi_flags
& RTSPACE
|| ch
->ch_c_cflag
& CRTSCTS
) {
1035 if (ch
->ch_flags
& CH_RECEIVER_OFF
) {
1036 ch
->ch_bd
->bd_ops
->enable_receiver(ch
);
1037 ch
->ch_flags
&= ~(CH_RECEIVER_OFF
);
1041 else if (ch
->ch_c_iflag
& IXOFF
&& ch
->ch_stops_sent
) {
1042 ch
->ch_stops_sent
= 0;
1043 ch
->ch_bd
->bd_ops
->send_start_character(ch
);
1047 /* Nothing needed. */
1053 void dgnc_wakeup_writes(struct channel_t
*ch
)
1056 unsigned long flags
;
1058 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
1061 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1064 * If channel now has space, wake up anyone waiting on the condition.
1066 qlen
= ch
->ch_w_head
- ch
->ch_w_tail
;
1070 if (qlen
>= (WQUEUESIZE
- 256)) {
1071 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1075 if (ch
->ch_tun
.un_flags
& UN_ISOPEN
) {
1076 if ((ch
->ch_tun
.un_tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
1077 ch
->ch_tun
.un_tty
->ldisc
->ops
->write_wakeup
) {
1078 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1079 (ch
->ch_tun
.un_tty
->ldisc
->ops
->write_wakeup
)(ch
->ch_tun
.un_tty
);
1080 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1083 wake_up_interruptible(&ch
->ch_tun
.un_tty
->write_wait
);
1086 * If unit is set to wait until empty, check to make sure
1087 * the queue AND FIFO are both empty.
1089 if (ch
->ch_tun
.un_flags
& UN_EMPTY
) {
1090 if ((qlen
== 0) && (ch
->ch_bd
->bd_ops
->get_uart_bytes_left(ch
) == 0)) {
1091 ch
->ch_tun
.un_flags
&= ~(UN_EMPTY
);
1094 * If RTS Toggle mode is on, whenever
1095 * the queue and UART is empty, keep RTS low.
1097 if (ch
->ch_digi
.digi_flags
& DIGI_RTS_TOGGLE
) {
1098 ch
->ch_mostat
&= ~(UART_MCR_RTS
);
1099 ch
->ch_bd
->bd_ops
->assert_modem_signals(ch
);
1103 * If DTR Toggle mode is on, whenever
1104 * the queue and UART is empty, keep DTR low.
1106 if (ch
->ch_digi
.digi_flags
& DIGI_DTR_TOGGLE
) {
1107 ch
->ch_mostat
&= ~(UART_MCR_DTR
);
1108 ch
->ch_bd
->bd_ops
->assert_modem_signals(ch
);
1113 wake_up_interruptible(&ch
->ch_tun
.un_flags_wait
);
1116 if (ch
->ch_pun
.un_flags
& UN_ISOPEN
) {
1117 if ((ch
->ch_pun
.un_tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
1118 ch
->ch_pun
.un_tty
->ldisc
->ops
->write_wakeup
) {
1119 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1120 (ch
->ch_pun
.un_tty
->ldisc
->ops
->write_wakeup
)(ch
->ch_pun
.un_tty
);
1121 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1124 wake_up_interruptible(&ch
->ch_pun
.un_tty
->write_wait
);
1127 * If unit is set to wait until empty, check to make sure
1128 * the queue AND FIFO are both empty.
1130 if (ch
->ch_pun
.un_flags
& UN_EMPTY
) {
1131 if ((qlen
== 0) && (ch
->ch_bd
->bd_ops
->get_uart_bytes_left(ch
) == 0))
1132 ch
->ch_pun
.un_flags
&= ~(UN_EMPTY
);
1135 wake_up_interruptible(&ch
->ch_pun
.un_flags_wait
);
1138 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1143 /************************************************************************
1145 * TTY Entry points and helper functions
1147 ************************************************************************/
1153 static int dgnc_tty_open(struct tty_struct
*tty
, struct file
*file
)
1155 struct dgnc_board
*brd
;
1156 struct channel_t
*ch
;
1161 unsigned long flags
;
1165 major
= MAJOR(tty_devnum(tty
));
1166 minor
= MINOR(tty_devnum(tty
));
1171 /* Get board pointer from our array of majors we have allocated */
1172 brd
= dgnc_BoardsByMajor
[major
];
1177 * If board is not yet up to a state of READY, go to
1178 * sleep waiting for it to happen or they cancel the open.
1180 rc
= wait_event_interruptible(brd
->state_wait
,
1181 (brd
->state
& BOARD_READY
));
1186 spin_lock_irqsave(&brd
->bd_lock
, flags
);
1188 /* If opened device is greater than our number of ports, bail. */
1189 if (PORT_NUM(minor
) > brd
->nasync
) {
1190 spin_unlock_irqrestore(&brd
->bd_lock
, flags
);
1194 ch
= brd
->channels
[PORT_NUM(minor
)];
1196 spin_unlock_irqrestore(&brd
->bd_lock
, flags
);
1200 /* Drop board lock */
1201 spin_unlock_irqrestore(&brd
->bd_lock
, flags
);
1203 /* Grab channel lock */
1204 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1206 /* Figure out our type */
1207 if (!IS_PRINT(minor
)) {
1208 un
= &brd
->channels
[PORT_NUM(minor
)]->ch_tun
;
1209 un
->un_type
= DGNC_SERIAL
;
1210 } else if (IS_PRINT(minor
)) {
1211 un
= &brd
->channels
[PORT_NUM(minor
)]->ch_pun
;
1212 un
->un_type
= DGNC_PRINT
;
1214 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1219 * If the port is still in a previous open, and in a state
1220 * where we simply cannot safely keep going, wait until the
1223 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1225 rc
= wait_event_interruptible(ch
->ch_flags_wait
, ((ch
->ch_flags
& CH_OPENING
) == 0));
1227 /* If ret is non-zero, user ctrl-c'ed us */
1232 * If either unit is in the middle of the fragile part of close,
1233 * we just cannot touch the channel safely.
1234 * Go to sleep, knowing that when the channel can be
1235 * touched safely, the close routine will signal the
1236 * ch_flags_wait to wake us back up.
1238 rc
= wait_event_interruptible(ch
->ch_flags_wait
,
1239 (((ch
->ch_tun
.un_flags
| ch
->ch_pun
.un_flags
) & UN_CLOSING
) == 0));
1241 /* If ret is non-zero, user ctrl-c'ed us */
1245 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1248 /* Store our unit into driver_data, so we always have it available. */
1249 tty
->driver_data
= un
;
1255 if (!(un
->un_flags
& UN_ISOPEN
)) {
1256 /* Store important variables. */
1259 /* Maybe do something here to the TTY struct as well? */
1264 * Allocate channel buffers for read/write/error.
1265 * Set flag, so we don't get trounced on.
1267 ch
->ch_flags
|= (CH_OPENING
);
1269 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1270 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1273 ch
->ch_rqueue
= kzalloc(RQUEUESIZE
, GFP_KERNEL
);
1275 ch
->ch_equeue
= kzalloc(EQUEUESIZE
, GFP_KERNEL
);
1277 ch
->ch_wqueue
= kzalloc(WQUEUESIZE
, GFP_KERNEL
);
1279 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1281 ch
->ch_flags
&= ~(CH_OPENING
);
1282 wake_up_interruptible(&ch
->ch_flags_wait
);
1285 * Initialize if neither terminal or printer is open.
1287 if (!((ch
->ch_tun
.un_flags
| ch
->ch_pun
.un_flags
) & UN_ISOPEN
)) {
1290 * Flush input queues.
1299 brd
->bd_ops
->flush_uart_write(ch
);
1300 brd
->bd_ops
->flush_uart_read(ch
);
1303 ch
->ch_cached_lsr
= 0;
1304 ch
->ch_stop_sending_break
= 0;
1305 ch
->ch_stops_sent
= 0;
1307 ch
->ch_c_cflag
= tty
->termios
.c_cflag
;
1308 ch
->ch_c_iflag
= tty
->termios
.c_iflag
;
1309 ch
->ch_c_oflag
= tty
->termios
.c_oflag
;
1310 ch
->ch_c_lflag
= tty
->termios
.c_lflag
;
1311 ch
->ch_startc
= tty
->termios
.c_cc
[VSTART
];
1312 ch
->ch_stopc
= tty
->termios
.c_cc
[VSTOP
];
1315 * Bring up RTS and DTR...
1316 * Also handle RTS or DTR toggle if set.
1318 if (!(ch
->ch_digi
.digi_flags
& DIGI_RTS_TOGGLE
))
1319 ch
->ch_mostat
|= (UART_MCR_RTS
);
1320 if (!(ch
->ch_digi
.digi_flags
& DIGI_DTR_TOGGLE
))
1321 ch
->ch_mostat
|= (UART_MCR_DTR
);
1323 /* Tell UART to init itself */
1324 brd
->bd_ops
->uart_init(ch
);
1328 * Run param in case we changed anything
1330 brd
->bd_ops
->param(tty
);
1335 * follow protocol for opening port
1338 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1340 rc
= dgnc_block_til_ready(tty
, file
, ch
);
1342 /* No going back now, increment our unit and channel counters */
1343 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1344 ch
->ch_open_count
++;
1345 un
->un_open_count
++;
1346 un
->un_flags
|= (UN_ISOPEN
);
1347 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1354 * dgnc_block_til_ready()
1356 * Wait for DCD, if needed.
1358 static int dgnc_block_til_ready(struct tty_struct
*tty
, struct file
*file
, struct channel_t
*ch
)
1361 struct un_t
*un
= NULL
;
1362 unsigned long flags
;
1364 int sleep_on_un_flags
= 0;
1366 if (!tty
|| tty
->magic
!= TTY_MAGIC
|| !file
|| !ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
1369 un
= tty
->driver_data
;
1370 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
1373 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1380 sleep_on_un_flags
= 0;
1383 * If board has failed somehow during our sleep, bail with error.
1385 if (ch
->ch_bd
->state
== BOARD_FAILED
) {
1390 /* If tty was hung up, break out of loop and set error. */
1391 if (tty_hung_up_p(file
)) {
1397 * If either unit is in the middle of the fragile part of close,
1398 * we just cannot touch the channel safely.
1399 * Go back to sleep, knowing that when the channel can be
1400 * touched safely, the close routine will signal the
1401 * ch_wait_flags to wake us back up.
1403 if (!((ch
->ch_tun
.un_flags
| ch
->ch_pun
.un_flags
) & UN_CLOSING
)) {
1406 * Our conditions to leave cleanly and happily:
1407 * 1) NONBLOCKING on the tty is set.
1409 * 3) DCD (fake or real) is active.
1412 if (file
->f_flags
& O_NONBLOCK
)
1415 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1420 if (ch
->ch_flags
& CH_CD
)
1423 if (ch
->ch_flags
& CH_FCAR
)
1426 sleep_on_un_flags
= 1;
1430 * If there is a signal pending, the user probably
1431 * interrupted (ctrl-c) us.
1432 * Leave loop with error set.
1434 if (signal_pending(current
)) {
1435 retval
= -ERESTARTSYS
;
1440 * Store the flags before we let go of channel lock
1442 if (sleep_on_un_flags
)
1443 old_flags
= ch
->ch_tun
.un_flags
| ch
->ch_pun
.un_flags
;
1445 old_flags
= ch
->ch_flags
;
1448 * Let go of channel lock before calling schedule.
1449 * Our poller will get any FEP events and wake us up when DCD
1450 * eventually goes active.
1453 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1456 * Wait for something in the flags to change from the current value.
1458 if (sleep_on_un_flags
)
1459 retval
= wait_event_interruptible(un
->un_flags_wait
,
1460 (old_flags
!= (ch
->ch_tun
.un_flags
| ch
->ch_pun
.un_flags
)));
1462 retval
= wait_event_interruptible(ch
->ch_flags_wait
,
1463 (old_flags
!= ch
->ch_flags
));
1466 * We got woken up for some reason.
1467 * Before looping around, grab our channel lock.
1469 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1474 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1486 * Hangup the port. Like a close, but don't wait for output to drain.
1488 static void dgnc_tty_hangup(struct tty_struct
*tty
)
1492 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
1495 un
= tty
->driver_data
;
1496 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
1499 /* flush the transmit queues */
1500 dgnc_tty_flush_buffer(tty
);
1509 static void dgnc_tty_close(struct tty_struct
*tty
, struct file
*file
)
1511 struct ktermios
*ts
;
1512 struct dgnc_board
*bd
;
1513 struct channel_t
*ch
;
1515 unsigned long flags
;
1518 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
1521 un
= tty
->driver_data
;
1522 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
1526 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
1530 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
1535 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1538 * Determine if this is the last close or not - and if we agree about
1539 * which type of close it is with the Line Discipline
1541 if ((tty
->count
== 1) && (un
->un_open_count
!= 1)) {
1543 * Uh, oh. tty->count is 1, which means that the tty
1544 * structure will be freed. un_open_count should always
1545 * be one in these conditions. If it's greater than
1546 * one, we've got real problems, since it means the
1547 * serial port won't be shutdown.
1549 APR(("tty->count is 1, un open count is %d\n", un
->un_open_count
));
1550 un
->un_open_count
= 1;
1553 if (un
->un_open_count
)
1554 un
->un_open_count
--;
1556 APR(("bad serial port open count of %d\n", un
->un_open_count
));
1558 ch
->ch_open_count
--;
1560 if (ch
->ch_open_count
&& un
->un_open_count
) {
1561 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1565 /* OK, its the last close on the unit */
1566 un
->un_flags
|= UN_CLOSING
;
1572 * Only officially close channel if count is 0 and
1573 * DIGI_PRINTER bit is not set.
1575 if ((ch
->ch_open_count
== 0) && !(ch
->ch_digi
.digi_flags
& DIGI_PRINTER
)) {
1577 ch
->ch_flags
&= ~(CH_STOPI
| CH_FORCED_STOPI
);
1580 * turn off print device when closing print device.
1582 if ((un
->un_type
== DGNC_PRINT
) && (ch
->ch_flags
& CH_PRON
)) {
1583 dgnc_wmove(ch
, ch
->ch_digi
.digi_offstr
,
1584 (int) ch
->ch_digi
.digi_offlen
);
1585 ch
->ch_flags
&= ~CH_PRON
;
1588 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1589 /* wait for output to drain */
1590 /* This will also return if we take an interrupt */
1592 rc
= bd
->bd_ops
->drain(tty
, 0);
1594 dgnc_tty_flush_buffer(tty
);
1595 tty_ldisc_flush(tty
);
1597 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1602 * If we have HUPCL set, lower DTR and RTS
1604 if (ch
->ch_c_cflag
& HUPCL
) {
1607 ch
->ch_mostat
&= ~(UART_MCR_DTR
| UART_MCR_RTS
);
1608 bd
->bd_ops
->assert_modem_signals(ch
);
1611 * Go to sleep to ensure RTS/DTR
1612 * have been dropped for modems to see it.
1614 if (ch
->ch_close_delay
) {
1615 spin_unlock_irqrestore(&ch
->ch_lock
,
1617 dgnc_ms_sleep(ch
->ch_close_delay
);
1618 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1622 ch
->ch_old_baud
= 0;
1624 /* Turn off UART interrupts for this port */
1625 ch
->ch_bd
->bd_ops
->uart_off(ch
);
1628 * turn off print device when closing print device.
1630 if ((un
->un_type
== DGNC_PRINT
) && (ch
->ch_flags
& CH_PRON
)) {
1631 dgnc_wmove(ch
, ch
->ch_digi
.digi_offstr
,
1632 (int) ch
->ch_digi
.digi_offlen
);
1633 ch
->ch_flags
&= ~CH_PRON
;
1638 un
->un_flags
&= ~(UN_ISOPEN
| UN_CLOSING
);
1640 wake_up_interruptible(&ch
->ch_flags_wait
);
1641 wake_up_interruptible(&un
->un_flags_wait
);
1643 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1648 * dgnc_tty_chars_in_buffer()
1650 * Return number of characters that have not been transmitted yet.
1652 * This routine is used by the line discipline to determine if there
1653 * is data waiting to be transmitted/drained/flushed or not.
1655 static int dgnc_tty_chars_in_buffer(struct tty_struct
*tty
)
1657 struct channel_t
*ch
= NULL
;
1658 struct un_t
*un
= NULL
;
1663 unsigned long flags
;
1668 un
= tty
->driver_data
;
1669 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
1673 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
1676 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1679 thead
= ch
->ch_w_head
& tmask
;
1680 ttail
= ch
->ch_w_tail
& tmask
;
1682 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1684 if (ttail
== thead
) {
1688 chars
= thead
- ttail
;
1690 chars
= thead
- ttail
+ WQUEUESIZE
;
1700 * Reduces bytes_available to the max number of characters
1701 * that can be sent currently given the maxcps value, and
1702 * returns the new bytes_available. This only affects printer
1705 static int dgnc_maxcps_room(struct tty_struct
*tty
, int bytes_available
)
1707 struct channel_t
*ch
= NULL
;
1708 struct un_t
*un
= NULL
;
1711 return bytes_available
;
1713 un
= tty
->driver_data
;
1714 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
1715 return bytes_available
;
1718 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
1719 return bytes_available
;
1722 * If its not the Transparent print device, return
1723 * the full data amount.
1725 if (un
->un_type
!= DGNC_PRINT
)
1726 return bytes_available
;
1728 if (ch
->ch_digi
.digi_maxcps
> 0 && ch
->ch_digi
.digi_bufsize
> 0) {
1730 unsigned long current_time
= jiffies
;
1731 unsigned long buffer_time
= current_time
+
1732 (HZ
* ch
->ch_digi
.digi_bufsize
) / ch
->ch_digi
.digi_maxcps
;
1734 if (ch
->ch_cpstime
< current_time
) {
1735 /* buffer is empty */
1736 ch
->ch_cpstime
= current_time
; /* reset ch_cpstime */
1737 cps_limit
= ch
->ch_digi
.digi_bufsize
;
1738 } else if (ch
->ch_cpstime
< buffer_time
) {
1739 /* still room in the buffer */
1740 cps_limit
= ((buffer_time
- ch
->ch_cpstime
) * ch
->ch_digi
.digi_maxcps
) / HZ
;
1742 /* no room in the buffer */
1746 bytes_available
= min(cps_limit
, bytes_available
);
1749 return bytes_available
;
1754 * dgnc_tty_write_room()
1756 * Return space available in Tx buffer
1758 static int dgnc_tty_write_room(struct tty_struct
*tty
)
1760 struct channel_t
*ch
= NULL
;
1761 struct un_t
*un
= NULL
;
1766 unsigned long flags
;
1768 if (tty
== NULL
|| dgnc_TmpWriteBuf
== NULL
)
1771 un
= tty
->driver_data
;
1772 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
1776 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
1779 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1782 head
= (ch
->ch_w_head
) & tmask
;
1783 tail
= (ch
->ch_w_tail
) & tmask
;
1785 ret
= tail
- head
- 1;
1789 /* Limit printer to maxcps */
1790 ret
= dgnc_maxcps_room(tty
, ret
);
1793 * If we are printer device, leave space for
1794 * possibly both the on and off strings.
1796 if (un
->un_type
== DGNC_PRINT
) {
1797 if (!(ch
->ch_flags
& CH_PRON
))
1798 ret
-= ch
->ch_digi
.digi_onlen
;
1799 ret
-= ch
->ch_digi
.digi_offlen
;
1801 if (ch
->ch_flags
& CH_PRON
)
1802 ret
-= ch
->ch_digi
.digi_offlen
;
1808 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1815 * dgnc_tty_put_char()
1817 * Put a character into ch->ch_buf
1819 * - used by the line discipline for OPOST processing
1821 static int dgnc_tty_put_char(struct tty_struct
*tty
, unsigned char c
)
1824 * Simply call tty_write.
1826 dgnc_tty_write(tty
, &c
, 1);
1834 * Take data from the user or kernel and send it out to the FEP.
1835 * In here exists all the Transparent Print magic as well.
1837 static int dgnc_tty_write(struct tty_struct
*tty
,
1838 const unsigned char *buf
, int count
)
1840 struct channel_t
*ch
= NULL
;
1841 struct un_t
*un
= NULL
;
1842 int bufcount
= 0, n
= 0;
1844 unsigned long flags
;
1851 if (tty
== NULL
|| dgnc_TmpWriteBuf
== NULL
)
1854 un
= tty
->driver_data
;
1855 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
1859 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
1866 * Store original amount of characters passed in.
1867 * This helps to figure out if we should ask the FEP
1868 * to send us an event when it has more space available.
1872 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1874 /* Get our space available for the channel from the board */
1876 head
= (ch
->ch_w_head
) & tmask
;
1877 tail
= (ch
->ch_w_tail
) & tmask
;
1879 bufcount
= tail
- head
- 1;
1881 bufcount
+= WQUEUESIZE
;
1884 * Limit printer output to maxcps overall, with bursts allowed
1885 * up to bufsize characters.
1887 bufcount
= dgnc_maxcps_room(tty
, bufcount
);
1890 * Take minimum of what the user wants to send, and the
1891 * space available in the FEP buffer.
1893 count
= min(count
, bufcount
);
1896 * Bail if no space left.
1899 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1904 * Output the printer ON string, if we are in terminal mode, but
1905 * need to be in printer mode.
1907 if ((un
->un_type
== DGNC_PRINT
) && !(ch
->ch_flags
& CH_PRON
)) {
1908 dgnc_wmove(ch
, ch
->ch_digi
.digi_onstr
,
1909 (int) ch
->ch_digi
.digi_onlen
);
1910 head
= (ch
->ch_w_head
) & tmask
;
1911 ch
->ch_flags
|= CH_PRON
;
1915 * On the other hand, output the printer OFF string, if we are
1916 * currently in printer mode, but need to output to the terminal.
1918 if ((un
->un_type
!= DGNC_PRINT
) && (ch
->ch_flags
& CH_PRON
)) {
1919 dgnc_wmove(ch
, ch
->ch_digi
.digi_offstr
,
1920 (int) ch
->ch_digi
.digi_offlen
);
1921 head
= (ch
->ch_w_head
) & tmask
;
1922 ch
->ch_flags
&= ~CH_PRON
;
1926 * If there is nothing left to copy, or I can't handle any more data, leave.
1929 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1935 count
= min(count
, WRITEBUFLEN
);
1937 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
1940 * If data is coming from user space, copy it into a temporary
1941 * buffer so we don't get swapped out while doing the copy to
1944 /* we're allowed to block if it's from_user */
1945 if (down_interruptible(&dgnc_TmpWriteSem
))
1949 * copy_from_user() returns the number
1950 * of bytes that could *NOT* be copied.
1952 count
-= copy_from_user(dgnc_TmpWriteBuf
, (const unsigned char __user
*) buf
, count
);
1955 up(&dgnc_TmpWriteSem
);
1959 spin_lock_irqsave(&ch
->ch_lock
, flags
);
1961 buf
= dgnc_TmpWriteBuf
;
1968 * If the write wraps over the top of the circular buffer,
1969 * move the portion up to the wrap point, and reset the
1970 * pointers to the bottom.
1972 remain
= WQUEUESIZE
- head
;
1976 memcpy(ch
->ch_wqueue
+ head
, buf
, remain
);
1977 dgnc_sniff_nowait_nolock(ch
, "USER WRITE", ch
->ch_wqueue
+ head
, remain
);
1984 * Move rest of data.
1987 memcpy(ch
->ch_wqueue
+ head
, buf
, remain
);
1988 dgnc_sniff_nowait_nolock(ch
, "USER WRITE", ch
->ch_wqueue
+ head
, remain
);
1994 ch
->ch_w_head
= head
;
1997 /* Update printer buffer empty time. */
1998 if ((un
->un_type
== DGNC_PRINT
) && (ch
->ch_digi
.digi_maxcps
> 0)
1999 && (ch
->ch_digi
.digi_bufsize
> 0)) {
2000 ch
->ch_cpstime
+= (HZ
* count
) / ch
->ch_digi
.digi_maxcps
;
2004 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2005 up(&dgnc_TmpWriteSem
);
2007 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2012 * Channel lock is grabbed and then released
2013 * inside this routine.
2015 ch
->ch_bd
->bd_ops
->copy_data_from_queue_to_uart(ch
);
2023 * Return modem signals to ld.
2026 static int dgnc_tty_tiocmget(struct tty_struct
*tty
)
2028 struct channel_t
*ch
;
2031 unsigned char mstat
= 0;
2032 unsigned long flags
;
2034 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2037 un
= tty
->driver_data
;
2038 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2042 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2045 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2047 mstat
= (ch
->ch_mostat
| ch
->ch_mistat
);
2049 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2053 if (mstat
& UART_MCR_DTR
)
2054 result
|= TIOCM_DTR
;
2055 if (mstat
& UART_MCR_RTS
)
2056 result
|= TIOCM_RTS
;
2057 if (mstat
& UART_MSR_CTS
)
2058 result
|= TIOCM_CTS
;
2059 if (mstat
& UART_MSR_DSR
)
2060 result
|= TIOCM_DSR
;
2061 if (mstat
& UART_MSR_RI
)
2063 if (mstat
& UART_MSR_DCD
)
2071 * dgnc_tty_tiocmset()
2073 * Set modem signals, called by ld.
2076 static int dgnc_tty_tiocmset(struct tty_struct
*tty
,
2077 unsigned int set
, unsigned int clear
)
2079 struct dgnc_board
*bd
;
2080 struct channel_t
*ch
;
2083 unsigned long flags
;
2085 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2088 un
= tty
->driver_data
;
2089 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2093 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2097 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2100 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2102 if (set
& TIOCM_RTS
)
2103 ch
->ch_mostat
|= UART_MCR_RTS
;
2105 if (set
& TIOCM_DTR
)
2106 ch
->ch_mostat
|= UART_MCR_DTR
;
2108 if (clear
& TIOCM_RTS
)
2109 ch
->ch_mostat
&= ~(UART_MCR_RTS
);
2111 if (clear
& TIOCM_DTR
)
2112 ch
->ch_mostat
&= ~(UART_MCR_DTR
);
2114 ch
->ch_bd
->bd_ops
->assert_modem_signals(ch
);
2116 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2123 * dgnc_tty_send_break()
2125 * Send a Break, called by ld.
2127 static int dgnc_tty_send_break(struct tty_struct
*tty
, int msec
)
2129 struct dgnc_board
*bd
;
2130 struct channel_t
*ch
;
2133 unsigned long flags
;
2135 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2138 un
= tty
->driver_data
;
2139 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2143 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2147 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2161 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2163 ch
->ch_bd
->bd_ops
->send_break(ch
, msec
);
2165 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2173 * dgnc_tty_wait_until_sent()
2175 * wait until data has been transmitted, called by ld.
2177 static void dgnc_tty_wait_until_sent(struct tty_struct
*tty
, int timeout
)
2179 struct dgnc_board
*bd
;
2180 struct channel_t
*ch
;
2184 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2187 un
= tty
->driver_data
;
2188 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2192 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2196 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2199 rc
= bd
->bd_ops
->drain(tty
, 0);
2206 * send a high priority character, called by ld.
2208 static void dgnc_tty_send_xchar(struct tty_struct
*tty
, char c
)
2210 struct dgnc_board
*bd
;
2211 struct channel_t
*ch
;
2213 unsigned long flags
;
2215 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2218 un
= tty
->driver_data
;
2219 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2223 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2227 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2230 dev_dbg(tty
->dev
, "dgnc_tty_send_xchar start\n");
2232 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2233 bd
->bd_ops
->send_immediate_char(ch
, c
);
2234 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2236 dev_dbg(tty
->dev
, "dgnc_tty_send_xchar finish\n");
2243 * Return modem signals to ld.
2245 static inline int dgnc_get_mstat(struct channel_t
*ch
)
2247 unsigned char mstat
;
2249 unsigned long flags
;
2251 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2254 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2256 mstat
= (ch
->ch_mostat
| ch
->ch_mistat
);
2258 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2262 if (mstat
& UART_MCR_DTR
)
2263 result
|= TIOCM_DTR
;
2264 if (mstat
& UART_MCR_RTS
)
2265 result
|= TIOCM_RTS
;
2266 if (mstat
& UART_MSR_CTS
)
2267 result
|= TIOCM_CTS
;
2268 if (mstat
& UART_MSR_DSR
)
2269 result
|= TIOCM_DSR
;
2270 if (mstat
& UART_MSR_RI
)
2272 if (mstat
& UART_MSR_DCD
)
2281 * Return modem signals to ld.
2283 static int dgnc_get_modem_info(struct channel_t
*ch
, unsigned int __user
*value
)
2287 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2290 result
= dgnc_get_mstat(ch
);
2295 return put_user(result
, value
);
2300 * dgnc_set_modem_info()
2302 * Set modem signals, called by ld.
2304 static int dgnc_set_modem_info(struct tty_struct
*tty
, unsigned int command
, unsigned int __user
*value
)
2306 struct dgnc_board
*bd
;
2307 struct channel_t
*ch
;
2310 unsigned int arg
= 0;
2311 unsigned long flags
;
2313 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2316 un
= tty
->driver_data
;
2317 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2321 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2325 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2330 ret
= get_user(arg
, value
);
2336 if (arg
& TIOCM_RTS
)
2337 ch
->ch_mostat
|= UART_MCR_RTS
;
2339 if (arg
& TIOCM_DTR
)
2340 ch
->ch_mostat
|= UART_MCR_DTR
;
2345 if (arg
& TIOCM_RTS
)
2346 ch
->ch_mostat
&= ~(UART_MCR_RTS
);
2348 if (arg
& TIOCM_DTR
)
2349 ch
->ch_mostat
&= ~(UART_MCR_DTR
);
2355 if (arg
& TIOCM_RTS
)
2356 ch
->ch_mostat
|= UART_MCR_RTS
;
2358 ch
->ch_mostat
&= ~(UART_MCR_RTS
);
2360 if (arg
& TIOCM_DTR
)
2361 ch
->ch_mostat
|= UART_MCR_DTR
;
2363 ch
->ch_mostat
&= ~(UART_MCR_DTR
);
2371 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2373 ch
->ch_bd
->bd_ops
->assert_modem_signals(ch
);
2375 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2382 * dgnc_tty_digigeta()
2384 * Ioctl to get the information for ditty.
2389 static int dgnc_tty_digigeta(struct tty_struct
*tty
, struct digi_t __user
*retinfo
)
2391 struct channel_t
*ch
;
2394 unsigned long flags
;
2399 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2402 un
= tty
->driver_data
;
2403 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2407 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2410 memset(&tmp
, 0, sizeof(tmp
));
2412 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2413 memcpy(&tmp
, &ch
->ch_digi
, sizeof(tmp
));
2414 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2416 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2424 * dgnc_tty_digiseta()
2426 * Ioctl to set the information for ditty.
2431 static int dgnc_tty_digiseta(struct tty_struct
*tty
, struct digi_t __user
*new_info
)
2433 struct dgnc_board
*bd
;
2434 struct channel_t
*ch
;
2436 struct digi_t new_digi
;
2437 unsigned long flags
;
2439 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2442 un
= tty
->driver_data
;
2443 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2447 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2451 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2454 if (copy_from_user(&new_digi
, new_info
, sizeof(new_digi
)))
2457 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2460 * Handle transistions to and from RTS Toggle.
2462 if (!(ch
->ch_digi
.digi_flags
& DIGI_RTS_TOGGLE
) && (new_digi
.digi_flags
& DIGI_RTS_TOGGLE
))
2463 ch
->ch_mostat
&= ~(UART_MCR_RTS
);
2464 if ((ch
->ch_digi
.digi_flags
& DIGI_RTS_TOGGLE
) && !(new_digi
.digi_flags
& DIGI_RTS_TOGGLE
))
2465 ch
->ch_mostat
|= (UART_MCR_RTS
);
2468 * Handle transistions to and from DTR Toggle.
2470 if (!(ch
->ch_digi
.digi_flags
& DIGI_DTR_TOGGLE
) && (new_digi
.digi_flags
& DIGI_DTR_TOGGLE
))
2471 ch
->ch_mostat
&= ~(UART_MCR_DTR
);
2472 if ((ch
->ch_digi
.digi_flags
& DIGI_DTR_TOGGLE
) && !(new_digi
.digi_flags
& DIGI_DTR_TOGGLE
))
2473 ch
->ch_mostat
|= (UART_MCR_DTR
);
2475 memcpy(&ch
->ch_digi
, &new_digi
, sizeof(new_digi
));
2477 if (ch
->ch_digi
.digi_maxcps
< 1)
2478 ch
->ch_digi
.digi_maxcps
= 1;
2480 if (ch
->ch_digi
.digi_maxcps
> 10000)
2481 ch
->ch_digi
.digi_maxcps
= 10000;
2483 if (ch
->ch_digi
.digi_bufsize
< 10)
2484 ch
->ch_digi
.digi_bufsize
= 10;
2486 if (ch
->ch_digi
.digi_maxchar
< 1)
2487 ch
->ch_digi
.digi_maxchar
= 1;
2489 if (ch
->ch_digi
.digi_maxchar
> ch
->ch_digi
.digi_bufsize
)
2490 ch
->ch_digi
.digi_maxchar
= ch
->ch_digi
.digi_bufsize
;
2492 if (ch
->ch_digi
.digi_onlen
> DIGI_PLEN
)
2493 ch
->ch_digi
.digi_onlen
= DIGI_PLEN
;
2495 if (ch
->ch_digi
.digi_offlen
> DIGI_PLEN
)
2496 ch
->ch_digi
.digi_offlen
= DIGI_PLEN
;
2498 ch
->ch_bd
->bd_ops
->param(tty
);
2500 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2507 * dgnc_set_termios()
2509 static void dgnc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
2511 struct dgnc_board
*bd
;
2512 struct channel_t
*ch
;
2514 unsigned long flags
;
2516 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2519 un
= tty
->driver_data
;
2520 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2524 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2528 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2531 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2533 ch
->ch_c_cflag
= tty
->termios
.c_cflag
;
2534 ch
->ch_c_iflag
= tty
->termios
.c_iflag
;
2535 ch
->ch_c_oflag
= tty
->termios
.c_oflag
;
2536 ch
->ch_c_lflag
= tty
->termios
.c_lflag
;
2537 ch
->ch_startc
= tty
->termios
.c_cc
[VSTART
];
2538 ch
->ch_stopc
= tty
->termios
.c_cc
[VSTOP
];
2540 ch
->ch_bd
->bd_ops
->param(tty
);
2543 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2547 static void dgnc_tty_throttle(struct tty_struct
*tty
)
2549 struct channel_t
*ch
;
2551 unsigned long flags
;
2553 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2556 un
= tty
->driver_data
;
2557 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2561 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2564 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2566 ch
->ch_flags
|= (CH_FORCED_STOPI
);
2568 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2572 static void dgnc_tty_unthrottle(struct tty_struct
*tty
)
2574 struct channel_t
*ch
;
2576 unsigned long flags
;
2578 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2581 un
= tty
->driver_data
;
2582 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2586 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2589 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2591 ch
->ch_flags
&= ~(CH_FORCED_STOPI
);
2593 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2597 static void dgnc_tty_start(struct tty_struct
*tty
)
2599 struct dgnc_board
*bd
;
2600 struct channel_t
*ch
;
2602 unsigned long flags
;
2604 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2607 un
= tty
->driver_data
;
2608 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2612 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2616 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2619 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2621 ch
->ch_flags
&= ~(CH_FORCED_STOP
);
2623 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2627 static void dgnc_tty_stop(struct tty_struct
*tty
)
2629 struct dgnc_board
*bd
;
2630 struct channel_t
*ch
;
2632 unsigned long flags
;
2634 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2637 un
= tty
->driver_data
;
2638 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2642 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2646 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2649 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2651 ch
->ch_flags
|= (CH_FORCED_STOP
);
2653 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2658 * dgnc_tty_flush_chars()
2660 * Flush the cook buffer
2662 * Note to self, and any other poor souls who venture here:
2664 * flush in this case DOES NOT mean dispose of the data.
2665 * instead, it means "stop buffering and send it if you
2666 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2668 * It is also always called in interrupt context - JAR 8-Sept-99
2670 static void dgnc_tty_flush_chars(struct tty_struct
*tty
)
2672 struct dgnc_board
*bd
;
2673 struct channel_t
*ch
;
2675 unsigned long flags
;
2677 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2680 un
= tty
->driver_data
;
2681 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2685 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2689 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2692 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2694 /* Do something maybe here */
2696 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2702 * dgnc_tty_flush_buffer()
2704 * Flush Tx buffer (make in == out)
2706 static void dgnc_tty_flush_buffer(struct tty_struct
*tty
)
2708 struct channel_t
*ch
;
2710 unsigned long flags
;
2712 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2715 un
= tty
->driver_data
;
2716 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2720 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2723 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2725 ch
->ch_flags
&= ~CH_STOP
;
2727 /* Flush our write queue */
2728 ch
->ch_w_head
= ch
->ch_w_tail
;
2730 /* Flush UARTs transmit FIFO */
2731 ch
->ch_bd
->bd_ops
->flush_uart_write(ch
);
2733 if (ch
->ch_tun
.un_flags
& (UN_LOW
|UN_EMPTY
)) {
2734 ch
->ch_tun
.un_flags
&= ~(UN_LOW
|UN_EMPTY
);
2735 wake_up_interruptible(&ch
->ch_tun
.un_flags_wait
);
2737 if (ch
->ch_pun
.un_flags
& (UN_LOW
|UN_EMPTY
)) {
2738 ch
->ch_pun
.un_flags
&= ~(UN_LOW
|UN_EMPTY
);
2739 wake_up_interruptible(&ch
->ch_pun
.un_flags_wait
);
2742 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2747 /*****************************************************************************
2749 * The IOCTL function and all of its helpers
2751 *****************************************************************************/
2756 * The usual assortment of ioctl's
2758 static int dgnc_tty_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
2761 struct dgnc_board
*bd
;
2762 struct channel_t
*ch
;
2765 unsigned long flags
;
2766 void __user
*uarg
= (void __user
*) arg
;
2768 if (!tty
|| tty
->magic
!= TTY_MAGIC
)
2771 un
= tty
->driver_data
;
2772 if (!un
|| un
->magic
!= DGNC_UNIT_MAGIC
)
2776 if (!ch
|| ch
->magic
!= DGNC_CHANNEL_MAGIC
)
2780 if (!bd
|| bd
->magic
!= DGNC_BOARD_MAGIC
)
2783 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2785 if (un
->un_open_count
<= 0) {
2786 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2792 /* Here are all the standard ioctl's that we MUST implement */
2796 * TCSBRK is SVID version: non-zero arg --> no break
2797 * this behaviour is exploited by tcdrain().
2799 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2800 * between 0.25 and 0.5 seconds so we'll ask for something
2801 * in the middle: 0.375 seconds.
2803 rc
= tty_check_change(tty
);
2804 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2808 rc
= ch
->ch_bd
->bd_ops
->drain(tty
, 0);
2813 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2815 if (((cmd
== TCSBRK
) && (!arg
)) || (cmd
== TCSBRKP
))
2816 ch
->ch_bd
->bd_ops
->send_break(ch
, 250);
2818 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2824 /* support for POSIX tcsendbreak()
2825 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2826 * between 0.25 and 0.5 seconds so we'll ask for something
2827 * in the middle: 0.375 seconds.
2829 rc
= tty_check_change(tty
);
2830 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2834 rc
= ch
->ch_bd
->bd_ops
->drain(tty
, 0);
2838 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2840 ch
->ch_bd
->bd_ops
->send_break(ch
, 250);
2842 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2847 rc
= tty_check_change(tty
);
2848 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2852 rc
= ch
->ch_bd
->bd_ops
->drain(tty
, 0);
2856 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2858 ch
->ch_bd
->bd_ops
->send_break(ch
, 250);
2860 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2866 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2871 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2873 rc
= put_user(C_CLOCAL(tty
) ? 1 : 0, (unsigned long __user
*) arg
);
2878 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2879 rc
= get_user(arg
, (unsigned long __user
*) arg
);
2883 spin_lock_irqsave(&ch
->ch_lock
, flags
);
2884 tty
->termios
.c_cflag
= ((tty
->termios
.c_cflag
& ~CLOCAL
) | (arg
? CLOCAL
: 0));
2885 ch
->ch_bd
->bd_ops
->param(tty
);
2886 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2891 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2892 return dgnc_get_modem_info(ch
, uarg
);
2897 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2898 return dgnc_set_modem_info(tty
, cmd
, uarg
);
2901 * Here are any additional ioctl's that we want to implement
2906 * The linux tty driver doesn't have a flush
2907 * input routine for the driver, assuming all backed
2908 * up data is in the line disc. buffers. However,
2909 * we all know that's not the case. Here, we
2910 * act on the ioctl, but then lie and say we didn't
2911 * so the line discipline will process the flush
2914 rc
= tty_check_change(tty
);
2916 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2920 if ((arg
== TCIFLUSH
) || (arg
== TCIOFLUSH
)) {
2921 ch
->ch_r_head
= ch
->ch_r_tail
;
2922 ch
->ch_bd
->bd_ops
->flush_uart_read(ch
);
2923 /* Force queue flow control to be released, if needed */
2924 dgnc_check_queue_flow_control(ch
);
2927 if ((arg
== TCOFLUSH
) || (arg
== TCIOFLUSH
)) {
2928 if (!(un
->un_type
== DGNC_PRINT
)) {
2929 ch
->ch_w_head
= ch
->ch_w_tail
;
2930 ch
->ch_bd
->bd_ops
->flush_uart_write(ch
);
2932 if (ch
->ch_tun
.un_flags
& (UN_LOW
|UN_EMPTY
)) {
2933 ch
->ch_tun
.un_flags
&= ~(UN_LOW
|UN_EMPTY
);
2934 wake_up_interruptible(&ch
->ch_tun
.un_flags_wait
);
2937 if (ch
->ch_pun
.un_flags
& (UN_LOW
|UN_EMPTY
)) {
2938 ch
->ch_pun
.un_flags
&= ~(UN_LOW
|UN_EMPTY
);
2939 wake_up_interruptible(&ch
->ch_pun
.un_flags_wait
);
2945 /* pretend we didn't recognize this IOCTL */
2946 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2947 return -ENOIOCTLCMD
;
2951 * The linux tty driver doesn't have a flush
2952 * input routine for the driver, assuming all backed
2953 * up data is in the line disc. buffers. However,
2954 * we all know that's not the case. Here, we
2955 * act on the ioctl, but then lie and say we didn't
2956 * so the line discipline will process the flush
2959 if (cmd
== TCSETSF
) {
2961 ch
->ch_flags
&= ~CH_STOP
;
2962 ch
->ch_r_head
= ch
->ch_r_tail
;
2963 ch
->ch_bd
->bd_ops
->flush_uart_read(ch
);
2964 /* Force queue flow control to be released, if needed */
2965 dgnc_check_queue_flow_control(ch
);
2968 /* now wait for all the output to drain */
2969 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2970 rc
= ch
->ch_bd
->bd_ops
->drain(tty
, 0);
2974 /* pretend we didn't recognize this */
2975 return -ENOIOCTLCMD
;
2979 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2980 rc
= ch
->ch_bd
->bd_ops
->drain(tty
, 0);
2984 /* pretend we didn't recognize this */
2985 return -ENOIOCTLCMD
;
2988 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2989 /* Make the ld do it */
2990 return -ENOIOCTLCMD
;
2993 /* get information for ditty */
2994 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
2995 return dgnc_tty_digigeta(tty
, uarg
);
3000 /* set information for ditty */
3001 if (cmd
== (DIGI_SETAW
)) {
3003 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3004 rc
= ch
->ch_bd
->bd_ops
->drain(tty
, 0);
3009 spin_lock_irqsave(&ch
->ch_lock
, flags
);
3011 tty_ldisc_flush(tty
);
3016 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3017 return dgnc_tty_digiseta(tty
, uarg
);
3022 /* Let go of locks when accessing user space, could sleep */
3023 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3024 rc
= get_user(loopback
, (unsigned int __user
*) arg
);
3027 spin_lock_irqsave(&ch
->ch_lock
, flags
);
3029 /* Enable/disable internal loopback for this port */
3031 ch
->ch_flags
|= CH_LOOPBACK
;
3033 ch
->ch_flags
&= ~(CH_LOOPBACK
);
3035 ch
->ch_bd
->bd_ops
->param(tty
);
3036 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3040 case DIGI_GETCUSTOMBAUD
:
3041 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3042 rc
= put_user(ch
->ch_custom_speed
, (unsigned int __user
*) arg
);
3045 case DIGI_SETCUSTOMBAUD
:
3048 /* Let go of locks when accessing user space, could sleep */
3049 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3050 rc
= get_user(new_rate
, (int __user
*) arg
);
3053 spin_lock_irqsave(&ch
->ch_lock
, flags
);
3054 dgnc_set_custom_speed(ch
, new_rate
);
3055 ch
->ch_bd
->bd_ops
->param(tty
);
3056 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3061 * This ioctl allows insertion of a character into the front
3062 * of any pending data to be transmitted.
3064 * This ioctl is to satify the "Send Character Immediate"
3065 * call that the RealPort protocol spec requires.
3067 case DIGI_REALPORT_SENDIMMEDIATE
:
3071 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3072 rc
= get_user(c
, (unsigned char __user
*) arg
);
3075 spin_lock_irqsave(&ch
->ch_lock
, flags
);
3076 ch
->ch_bd
->bd_ops
->send_immediate_char(ch
, c
);
3077 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3082 * This ioctl returns all the current counts for the port.
3084 * This ioctl is to satify the "Line Error Counters"
3085 * call that the RealPort protocol spec requires.
3087 case DIGI_REALPORT_GETCOUNTERS
:
3089 struct digi_getcounter buf
;
3091 buf
.norun
= ch
->ch_err_overrun
;
3092 buf
.noflow
= 0; /* The driver doesn't keep this stat */
3093 buf
.nframe
= ch
->ch_err_frame
;
3094 buf
.nparity
= ch
->ch_err_parity
;
3095 buf
.nbreak
= ch
->ch_err_break
;
3096 buf
.rbytes
= ch
->ch_rxcount
;
3097 buf
.tbytes
= ch
->ch_txcount
;
3099 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3101 if (copy_to_user(uarg
, &buf
, sizeof(buf
)))
3108 * This ioctl returns all current events.
3110 * This ioctl is to satify the "Event Reporting"
3111 * call that the RealPort protocol spec requires.
3113 case DIGI_REALPORT_GETEVENTS
:
3115 unsigned int events
= 0;
3117 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3118 if (ch
->ch_flags
& CH_BREAK_SENDING
)
3120 if ((ch
->ch_flags
& CH_STOP
) || (ch
->ch_flags
& CH_FORCED_STOP
))
3121 events
|= (EV_OPU
| EV_OPS
);
3123 if ((ch
->ch_flags
& CH_STOPI
) || (ch
->ch_flags
& CH_FORCED_STOPI
))
3124 events
|= (EV_IPU
| EV_IPS
);
3126 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3127 rc
= put_user(events
, (unsigned int __user
*) arg
);
3132 * This ioctl returns TOUT and TIN counters based
3133 * upon the values passed in by the RealPort Server.
3134 * It also passes back whether the UART Transmitter is
3137 case DIGI_REALPORT_GETBUFFERS
:
3139 struct digi_getbuffer buf
;
3143 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3146 * Get data from user first.
3148 if (copy_from_user(&buf
, uarg
, sizeof(buf
)))
3151 spin_lock_irqsave(&ch
->ch_lock
, flags
);
3154 * Figure out how much data is in our RX and TX queues.
3156 buf
.rxbuf
= (ch
->ch_r_head
- ch
->ch_r_tail
) & RQUEUEMASK
;
3157 buf
.txbuf
= (ch
->ch_w_head
- ch
->ch_w_tail
) & WQUEUEMASK
;
3160 * Is the UART empty? Add that value to whats in our TX queue.
3162 count
= buf
.txbuf
+ ch
->ch_bd
->bd_ops
->get_uart_bytes_left(ch
);
3165 * Figure out how much data the RealPort Server believes should
3166 * be in our TX queue.
3168 tdist
= (buf
.tIn
- buf
.tOut
) & 0xffff;
3171 * If we have more data than the RealPort Server believes we
3172 * should have, reduce our count to its amount.
3174 * This count difference CAN happen because the Linux LD can
3175 * insert more characters into our queue for OPOST processing
3176 * that the RealPort Server doesn't know about.
3178 if (buf
.txbuf
> tdist
)
3182 * Report whether our queue and UART TX are completely empty.
3189 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3191 if (copy_to_user(uarg
, &buf
, sizeof(buf
)))
3197 spin_unlock_irqrestore(&ch
->ch_lock
, flags
);
3199 return -ENOIOCTLCMD
;