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