Merge tag 'clk-for-linus-3.14-part2' of git://git.linaro.org/people/mike.turquette...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / dgap / dgap_tty.c
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  *
20  *      NOTE TO LINUX KERNEL HACKERS:  DO NOT REFORMAT THIS CODE!
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  *
27  *      Send any bug fixes/changes to:  Eng.Linux at digi dot com.
28  *      Thank you.
29  */
30
31 /************************************************************************
32  *
33  * This file implements the tty driver functionality for the
34  * FEP5 based product lines.
35  *
36  ************************************************************************
37  *
38  * $Id: dgap_tty.c,v 1.3 2011/06/23 12:11:31 markh Exp $
39  */
40
41 #include <linux/kernel.h>
42 #include <linux/version.h>
43 #include <linux/sched.h>        /* For jiffies, task states */
44 #include <linux/interrupt.h>    /* For tasklet and interrupt structs/defines */
45 #include <linux/module.h>
46 #include <linux/ctype.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/serial_reg.h>
50 #include <linux/slab.h>
51 #include <linux/delay.h>        /* For udelay */
52 #include <asm/uaccess.h>        /* For copy_from_user/copy_to_user */
53 #include <asm/io.h>             /* For read[bwl]/write[bwl] */
54 #include <linux/pci.h>
55
56 #include "dgap_driver.h"
57 #include "dgap_tty.h"
58 #include "dgap_types.h"
59 #include "dgap_fep5.h"
60 #include "dgap_parse.h"
61 #include "dgap_conf.h"
62 #include "dgap_sysfs.h"
63
64 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
65 #define init_MUTEX(sem)         sema_init(sem, 1)
66 #define DECLARE_MUTEX(name)     \
67         struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
68 #endif
69
70 /*
71  * internal variables
72  */
73 static struct board_t   *dgap_BoardsByMajor[256];
74 static uchar            *dgap_TmpWriteBuf = NULL;
75 static DECLARE_MUTEX(dgap_TmpWriteSem);
76
77 /*
78  * Default transparent print information.
79  */
80 static struct digi_t dgap_digi_init = {
81         .digi_flags =   DIGI_COOK,      /* Flags                        */
82         .digi_maxcps =  100,            /* Max CPS                      */
83         .digi_maxchar = 50,             /* Max chars in print queue     */
84         .digi_bufsize = 100,            /* Printer buffer size          */
85         .digi_onlen =   4,              /* size of printer on string    */
86         .digi_offlen =  4,              /* size of printer off string   */
87         .digi_onstr =   "\033[5i",      /* ANSI printer on string ]     */
88         .digi_offstr =  "\033[4i",      /* ANSI printer off string ]    */
89         .digi_term =    "ansi"          /* default terminal type        */
90 };
91
92
93 /*
94  * Define a local default termios struct. All ports will be created
95  * with this termios initially.
96  *
97  * This defines a raw port at 9600 baud, 8 data bits, no parity,
98  * 1 stop bit.
99  */
100
101 static struct ktermios DgapDefaultTermios =
102 {
103         .c_iflag =      (DEFAULT_IFLAGS),       /* iflags */
104         .c_oflag =      (DEFAULT_OFLAGS),       /* oflags */
105         .c_cflag =      (DEFAULT_CFLAGS),       /* cflags */
106         .c_lflag =      (DEFAULT_LFLAGS),       /* lflags */
107         .c_cc =         INIT_C_CC,
108         .c_line =       0,
109 };
110
111 /* Our function prototypes */
112 static int dgap_tty_open(struct tty_struct *tty, struct file *file);
113 static void dgap_tty_close(struct tty_struct *tty, struct file *file);
114 static int dgap_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
115 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
116 static int dgap_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
117 static int dgap_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
118 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo);
119 static int dgap_tty_digisetedelay(struct tty_struct *tty, int __user *new_info);
120 static int dgap_tty_write_room(struct tty_struct* tty);
121 static int dgap_tty_chars_in_buffer(struct tty_struct* tty);
122 static void dgap_tty_start(struct tty_struct *tty);
123 static void dgap_tty_stop(struct tty_struct *tty);
124 static void dgap_tty_throttle(struct tty_struct *tty);
125 static void dgap_tty_unthrottle(struct tty_struct *tty);
126 static void dgap_tty_flush_chars(struct tty_struct *tty);
127 static void dgap_tty_flush_buffer(struct tty_struct *tty);
128 static void dgap_tty_hangup(struct tty_struct *tty);
129 static int dgap_wait_for_drain(struct tty_struct *tty);
130 static int dgap_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
131 static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value);
132 static int dgap_tty_digisetcustombaud(struct tty_struct *tty, int __user *new_info);
133 static int dgap_tty_digigetcustombaud(struct tty_struct *tty, int __user *retinfo);
134 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
135 static int dgap_tty_tiocmget(struct tty_struct *tty);
136 static int dgap_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
137 #else
138 static int dgap_tty_tiocmget(struct tty_struct *tty, struct file *file);
139 static int dgap_tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
140 #endif
141 static int dgap_tty_send_break(struct tty_struct *tty, int msec);
142 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout);
143 static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
144 static void dgap_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
145 static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c);
146 static void dgap_tty_send_xchar(struct tty_struct *tty, char ch);
147
148 static const struct tty_operations dgap_tty_ops = {
149         .open = dgap_tty_open,
150         .close = dgap_tty_close,
151         .write = dgap_tty_write,
152         .write_room = dgap_tty_write_room,
153         .flush_buffer = dgap_tty_flush_buffer,
154         .chars_in_buffer = dgap_tty_chars_in_buffer,
155         .flush_chars = dgap_tty_flush_chars,
156         .ioctl = dgap_tty_ioctl,
157         .set_termios = dgap_tty_set_termios,
158         .stop = dgap_tty_stop,
159         .start = dgap_tty_start,
160         .throttle = dgap_tty_throttle,
161         .unthrottle = dgap_tty_unthrottle,
162         .hangup = dgap_tty_hangup,
163         .put_char = dgap_tty_put_char,
164         .tiocmget = dgap_tty_tiocmget,
165         .tiocmset = dgap_tty_tiocmset,
166         .break_ctl = dgap_tty_send_break,
167         .wait_until_sent = dgap_tty_wait_until_sent,
168         .send_xchar = dgap_tty_send_xchar
169 };
170
171
172
173
174
175 /************************************************************************
176  *
177  * TTY Initialization/Cleanup Functions
178  *
179  ************************************************************************/
180
181 /*
182  * dgap_tty_preinit()
183  *
184  * Initialize any global tty related data before we download any boards.
185  */
186 int dgap_tty_preinit(void)
187 {
188         unsigned long flags;
189
190         DGAP_LOCK(dgap_global_lock, flags);
191
192         /*
193          * Allocate a buffer for doing the copy from user space to
194          * kernel space in dgap_input().  We only use one buffer and
195          * control access to it with a semaphore.  If we are paging, we
196          * are already in trouble so one buffer won't hurt much anyway.
197          */
198         dgap_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_ATOMIC);
199
200         if (!dgap_TmpWriteBuf) {
201                 DGAP_UNLOCK(dgap_global_lock, flags);
202                 DPR_INIT(("unable to allocate tmp write buf"));
203                 return (-ENOMEM);
204         }
205
206         DGAP_UNLOCK(dgap_global_lock, flags);
207         return(0);
208 }
209
210
211 /*
212  * dgap_tty_register()
213  *
214  * Init the tty subsystem for this board.
215  */
216 int dgap_tty_register(struct board_t *brd)
217 {
218         int rc = 0;
219
220         DPR_INIT(("tty_register start"));
221
222         brd->SerialDriver = alloc_tty_driver(MAXPORTS);
223
224         snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgap_%d_", brd->boardnum);
225         brd->SerialDriver->name = brd->SerialName;
226         brd->SerialDriver->name_base = 0;
227         brd->SerialDriver->major = 0;
228         brd->SerialDriver->minor_start = 0;
229         brd->SerialDriver->type = TTY_DRIVER_TYPE_SERIAL;
230         brd->SerialDriver->subtype = SERIAL_TYPE_NORMAL;
231         brd->SerialDriver->init_termios = DgapDefaultTermios;
232         brd->SerialDriver->driver_name = DRVSTR;
233         brd->SerialDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
234
235         /* The kernel wants space to store pointers to tty_structs */
236         brd->SerialDriver->ttys = kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
237         if (!brd->SerialDriver->ttys)
238                 return(-ENOMEM);
239
240 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
241         brd->SerialDriver->refcount = brd->TtyRefCnt;
242 #endif
243
244         /*
245          * Entry points for driver.  Called by the kernel from
246          * tty_io.c and n_tty.c.
247          */
248         tty_set_operations(brd->SerialDriver, &dgap_tty_ops);
249
250         /*
251          * If we're doing transparent print, we have to do all of the above
252          * again, separately so we don't get the LD confused about what major
253          * we are when we get into the dgap_tty_open() routine.
254          */
255         brd->PrintDriver = alloc_tty_driver(MAXPORTS);
256
257         snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgap_%d_", brd->boardnum);
258         brd->PrintDriver->name = brd->PrintName;
259         brd->PrintDriver->name_base = 0;
260         brd->PrintDriver->major = 0;
261         brd->PrintDriver->minor_start = 0;
262         brd->PrintDriver->type = TTY_DRIVER_TYPE_SERIAL;
263         brd->PrintDriver->subtype = SERIAL_TYPE_NORMAL;
264         brd->PrintDriver->init_termios = DgapDefaultTermios;
265         brd->PrintDriver->driver_name = DRVSTR;
266         brd->PrintDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
267
268         /* The kernel wants space to store pointers to tty_structs */
269         brd->PrintDriver->ttys = kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
270         if (!brd->PrintDriver->ttys)
271                 return(-ENOMEM);
272
273 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
274         brd->PrintDriver->refcount = brd->TtyRefCnt;
275 #endif
276
277         /*
278          * Entry points for driver.  Called by the kernel from
279          * tty_io.c and n_tty.c.
280          */
281         tty_set_operations(brd->PrintDriver, &dgap_tty_ops);
282
283         if (!brd->dgap_Major_Serial_Registered) {
284                 /* Register tty devices */
285                 rc = tty_register_driver(brd->SerialDriver);
286                 if (rc < 0) {
287                         APR(("Can't register tty device (%d)\n", rc));
288                         return(rc);
289                 }
290                 brd->dgap_Major_Serial_Registered = TRUE;
291                 dgap_BoardsByMajor[brd->SerialDriver->major] = brd;
292                 brd->dgap_Serial_Major = brd->SerialDriver->major;
293         }
294
295         if (!brd->dgap_Major_TransparentPrint_Registered) {
296                 /* Register Transparent Print devices */
297                 rc = tty_register_driver(brd->PrintDriver);
298                 if (rc < 0) {
299                         APR(("Can't register Transparent Print device (%d)\n", rc));
300                         return(rc);
301                 }
302                 brd->dgap_Major_TransparentPrint_Registered = TRUE;
303                 dgap_BoardsByMajor[brd->PrintDriver->major] = brd;
304                 brd->dgap_TransparentPrint_Major = brd->PrintDriver->major;
305         }
306
307         DPR_INIT(("DGAP REGISTER TTY: MAJORS: %d %d\n", brd->SerialDriver->major,
308                 brd->PrintDriver->major));
309
310         return (rc);
311 }
312
313
314 /*
315  * dgap_tty_init()
316  *
317  * Init the tty subsystem.  Called once per board after board has been
318  * downloaded and init'ed.
319  */
320 int dgap_tty_init(struct board_t *brd)
321 {
322         int i;
323         int tlw;
324         uint true_count = 0;
325         uchar *vaddr;
326         uchar modem = 0;
327         struct channel_t *ch;
328         struct bs_t *bs;
329         struct cm_t *cm;
330
331         if (!brd)
332                 return (-ENXIO);
333
334         DPR_INIT(("dgap_tty_init start\n"));
335
336         /*
337          * Initialize board structure elements.
338          */
339
340         vaddr = brd->re_map_membase;
341         true_count = readw((vaddr + NCHAN));
342
343         brd->nasync = dgap_config_get_number_of_ports(brd);
344
345         if (!brd->nasync) {
346                 brd->nasync = brd->maxports;
347         }
348
349         if (brd->nasync > brd->maxports) {
350                 brd->nasync = brd->maxports;
351         }
352
353         if (true_count != brd->nasync) {
354                 if ((brd->type == PPCM) && (true_count == 64)) {
355                         APR(("***WARNING**** %s configured for %d ports, has %d ports.\nPlease make SURE the EBI cable running from the card\nto each EM module is plugged into EBI IN!\n",
356                                 brd->name, brd->nasync, true_count));
357                 }
358                 else if ((brd->type == PPCM) && (true_count == 0)) {
359                         APR(("***WARNING**** %s configured for %d ports, has %d ports.\nPlease make SURE the EBI cable running from the card\nto each EM module is plugged into EBI IN!\n",
360                                 brd->name, brd->nasync, true_count));
361                 }
362                 else {
363                         APR(("***WARNING**** %s configured for %d ports, has %d ports.\n",
364                                 brd->name, brd->nasync, true_count));
365                 }
366
367                 brd->nasync = true_count;
368
369                 /* If no ports, don't bother going any further */
370                 if (!brd->nasync) {
371                         brd->state = BOARD_FAILED;
372                         brd->dpastatus = BD_NOFEP;
373                         return(-ENXIO);
374                 }
375         }
376
377         /*
378          * Allocate channel memory that might not have been allocated
379          * when the driver was first loaded.
380          */
381         for (i = 0; i < brd->nasync; i++) {
382                 if (!brd->channels[i]) {
383                         brd->channels[i] = kzalloc(sizeof(struct channel_t), GFP_ATOMIC);
384                         if (!brd->channels[i]) {
385                                 DPR_CORE(("%s:%d Unable to allocate memory for channel struct\n",
386                                     __FILE__, __LINE__));
387                         }
388                 }
389         }
390
391         ch = brd->channels[0];
392         vaddr = brd->re_map_membase;
393
394         bs = (struct bs_t *) ((ulong) vaddr + CHANBUF);
395         cm = (struct cm_t *) ((ulong) vaddr + CMDBUF);
396
397         brd->bd_bs = bs;
398
399         /* Set up channel variables */
400         for (i = 0; i < brd->nasync; i++, ch = brd->channels[i], bs++) {
401
402                 if (!brd->channels[i])
403                         continue;
404
405                 DGAP_SPINLOCK_INIT(ch->ch_lock);
406
407                 /* Store all our magic numbers */
408                 ch->magic = DGAP_CHANNEL_MAGIC;
409                 ch->ch_tun.magic = DGAP_UNIT_MAGIC;
410                 ch->ch_tun.un_type = DGAP_SERIAL;
411                 ch->ch_tun.un_ch = ch;
412                 ch->ch_tun.un_dev = i;
413
414                 ch->ch_pun.magic = DGAP_UNIT_MAGIC;
415                 ch->ch_pun.un_type = DGAP_PRINT;
416                 ch->ch_pun.un_ch = ch;
417                 ch->ch_pun.un_dev = i;
418
419                 ch->ch_vaddr = vaddr;
420                 ch->ch_bs = bs;
421                 ch->ch_cm = cm;
422                 ch->ch_bd = brd;
423                 ch->ch_portnum = i;
424                 ch->ch_digi = dgap_digi_init;
425
426                 /*
427                  * Set up digi dsr and dcd bits based on altpin flag.
428                  */
429                 if (dgap_config_get_altpin(brd)) {
430                         ch->ch_dsr      = DM_CD;
431                         ch->ch_cd       = DM_DSR;
432                         ch->ch_digi.digi_flags |= DIGI_ALTPIN;
433                 }
434                 else {
435                         ch->ch_cd       = DM_CD;
436                         ch->ch_dsr      = DM_DSR;
437                 }
438
439                 ch->ch_taddr = vaddr + ((ch->ch_bs->tx_seg) << 4);
440                 ch->ch_raddr = vaddr + ((ch->ch_bs->rx_seg) << 4);
441                 ch->ch_tx_win = 0;
442                 ch->ch_rx_win = 0;
443                 ch->ch_tsize = readw(&(ch->ch_bs->tx_max)) + 1;
444                 ch->ch_rsize = readw(&(ch->ch_bs->rx_max)) + 1;
445                 ch->ch_tstart = 0;
446                 ch->ch_rstart = 0;
447
448                 /* .25 second delay */
449                 ch->ch_close_delay = 250;
450
451                 /*
452                  * Set queue water marks, interrupt mask,
453                  * and general tty parameters.
454                  */
455                 ch->ch_tlw = tlw = ch->ch_tsize >= 2000 ? ((ch->ch_tsize * 5) / 8) : ch->ch_tsize / 2;
456
457                 dgap_cmdw(ch, STLOW, tlw, 0);
458
459                 dgap_cmdw(ch, SRLOW, ch->ch_rsize / 2, 0);
460
461                 dgap_cmdw(ch, SRHIGH, 7 * ch->ch_rsize / 8, 0);
462
463                 ch->ch_mistat = readb(&(ch->ch_bs->m_stat));
464
465                 init_waitqueue_head(&ch->ch_flags_wait);
466                 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
467                 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
468                 init_waitqueue_head(&ch->ch_sniff_wait);
469
470                 /* Turn on all modem interrupts for now */
471                 modem = (DM_CD | DM_DSR | DM_CTS | DM_RI);
472                 writeb(modem, &(ch->ch_bs->m_int));
473
474                 /*
475                  * Set edelay to 0 if interrupts are turned on,
476                  * otherwise set edelay to the usual 100.
477                  */
478                 if (brd->intr_used)
479                         writew(0, &(ch->ch_bs->edelay));
480                 else
481                         writew(100, &(ch->ch_bs->edelay));
482
483                 writeb(1, &(ch->ch_bs->idata));
484         }
485
486
487         DPR_INIT(("dgap_tty_init finish\n"));
488
489         return (0);
490 }
491
492
493 /*
494  * dgap_tty_post_uninit()
495  *
496  * UnInitialize any global tty related data.
497  */
498 void dgap_tty_post_uninit(void)
499 {
500         kfree(dgap_TmpWriteBuf);
501         dgap_TmpWriteBuf = NULL;
502 }
503
504
505 /*
506  * dgap_tty_uninit()
507  *
508  * Uninitialize the TTY portion of this driver.  Free all memory and
509  * resources.
510  */
511 void dgap_tty_uninit(struct board_t *brd)
512 {
513         int i = 0;
514
515         if (brd->dgap_Major_Serial_Registered) {
516                 dgap_BoardsByMajor[brd->SerialDriver->major] = NULL;
517                 brd->dgap_Serial_Major = 0;
518                 for (i = 0; i < brd->nasync; i++) {
519                         dgap_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
520                         tty_unregister_device(brd->SerialDriver, i);
521                 }
522                 tty_unregister_driver(brd->SerialDriver);
523                 kfree(brd->SerialDriver->ttys);
524                 brd->SerialDriver->ttys = NULL;
525                 put_tty_driver(brd->SerialDriver);
526                 brd->dgap_Major_Serial_Registered = FALSE;
527         }
528
529         if (brd->dgap_Major_TransparentPrint_Registered) {
530                 dgap_BoardsByMajor[brd->PrintDriver->major] = NULL;
531                 brd->dgap_TransparentPrint_Major = 0;
532                 for (i = 0; i < brd->nasync; i++) {
533                         dgap_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
534                         tty_unregister_device(brd->PrintDriver, i);
535                 }
536                 tty_unregister_driver(brd->PrintDriver);
537                 kfree(brd->PrintDriver->ttys);
538                 brd->PrintDriver->ttys = NULL;
539                 put_tty_driver(brd->PrintDriver);
540                 brd->dgap_Major_TransparentPrint_Registered = FALSE;
541         }
542 }
543
544
545 #define TMPBUFLEN (1024)
546
547 /*
548  * dgap_sniff - Dump data out to the "sniff" buffer if the
549  * proc sniff file is opened...
550  */
551 static void dgap_sniff_nowait_nolock(struct channel_t *ch, uchar *text, uchar *buf, int len)
552 {
553         struct timeval tv;
554         int n;
555         int r;
556         int nbuf;
557         int i;
558         int tmpbuflen;
559         char tmpbuf[TMPBUFLEN];
560         char *p = tmpbuf;
561         int too_much_data;
562
563         /* Leave if sniff not open */
564         if (!(ch->ch_sniff_flags & SNIFF_OPEN))
565                 return;
566
567         do_gettimeofday(&tv);
568
569         /* Create our header for data dump */
570         p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
571         tmpbuflen = p - tmpbuf;
572
573         do {
574                 too_much_data = 0;
575
576                 for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
577                         p += sprintf(p, "%02x ", *buf);
578                         buf++;
579                         tmpbuflen = p - tmpbuf;
580                 }
581
582                 if (tmpbuflen < (TMPBUFLEN - 4)) {
583                         if (i > 0)
584                                 p += sprintf(p - 1, "%s\n", ">");
585                         else
586                                 p += sprintf(p, "%s\n", ">");
587                 } else {
588                         too_much_data = 1;
589                         len -= i;
590                 }
591
592                 nbuf = strlen(tmpbuf);
593                 p = tmpbuf;
594
595                 /*
596                  *  Loop while data remains.
597                  */
598                 while (nbuf > 0 && ch->ch_sniff_buf) {
599                         /*
600                          *  Determine the amount of available space left in the
601                          *  buffer.  If there's none, wait until some appears.
602                          */
603                         n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) & SNIFF_MASK;
604
605                         /*
606                          * If there is no space left to write to in our sniff buffer,
607                          * we have no choice but to drop the data.
608                          * We *cannot* sleep here waiting for space, because this
609                          * function was probably called by the interrupt/timer routines!
610                          */
611                         if (n == 0) {
612                                 return;
613                         }
614
615                         /*
616                          * Copy as much data as will fit.
617                          */
618
619                         if (n > nbuf)
620                                 n = nbuf;
621
622                         r = SNIFF_MAX - ch->ch_sniff_in;
623
624                         if (r <= n) {
625                                 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
626
627                                 n -= r;
628                                 ch->ch_sniff_in = 0;
629                                 p += r;
630                                 nbuf -= r;
631                         }
632
633                         memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
634
635                         ch->ch_sniff_in += n;
636                         p += n;
637                         nbuf -= n;
638
639                         /*
640                          *  Wakeup any thread waiting for data
641                          */
642                         if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
643                                 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
644                                 wake_up_interruptible(&ch->ch_sniff_wait);
645                         }
646                 }
647
648                 /*
649                  * If the user sent us too much data to push into our tmpbuf,
650                  * we need to keep looping around on all the data.
651                  */
652                 if (too_much_data) {
653                         p = tmpbuf;
654                         tmpbuflen = 0;
655                 }
656
657         } while (too_much_data);
658 }
659
660
661 /*=======================================================================
662  *
663  *      dgap_input - Process received data.
664  *
665  *              ch      - Pointer to channel structure.
666  *
667  *=======================================================================*/
668
669 void dgap_input(struct channel_t *ch)
670 {
671         struct board_t *bd;
672         struct bs_t     *bs;
673         struct tty_struct *tp;
674         struct tty_ldisc *ld;
675         uint    rmask;
676         uint    head;
677         uint    tail;
678         int     data_len;
679         ulong   lock_flags;
680         ulong   lock_flags2;
681         int flip_len;
682         int len = 0;
683         int n = 0;
684         uchar *buf;
685         uchar tmpchar;
686         int s = 0;
687
688         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
689                 return;
690
691         tp = ch->ch_tun.un_tty;
692
693         bs  = ch->ch_bs;
694         if (!bs) {
695                 return;
696         }
697
698         bd = ch->ch_bd;
699         if(!bd || bd->magic != DGAP_BOARD_MAGIC)
700                 return;
701
702         DPR_READ(("dgap_input start\n"));
703
704         DGAP_LOCK(bd->bd_lock, lock_flags);
705         DGAP_LOCK(ch->ch_lock, lock_flags2);
706
707         /*
708          *      Figure the number of characters in the buffer.
709          *      Exit immediately if none.
710          */
711
712         rmask = ch->ch_rsize - 1;
713
714         head = readw(&(bs->rx_head));
715         head &= rmask;
716         tail = readw(&(bs->rx_tail));
717         tail &= rmask;
718
719         data_len = (head - tail) & rmask;
720
721         if (data_len == 0) {
722                 writeb(1, &(bs->idata));
723                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
724                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
725                 DPR_READ(("No data on port %d\n", ch->ch_portnum));
726                 return;
727         }
728
729         /*
730          * If the device is not open, or CREAD is off, flush
731          * input data and return immediately.
732          */
733         if ((bd->state != BOARD_READY) || !tp  || (tp->magic != TTY_MAGIC) ||
734             !(ch->ch_tun.un_flags & UN_ISOPEN) || !(tp->termios.c_cflag & CREAD) ||
735             (ch->ch_tun.un_flags & UN_CLOSING)) {
736
737                 DPR_READ(("input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum));
738                 DPR_READ(("input. tp: %p tp->magic: %x MAGIC:%x ch flags: %x\n",
739                         tp, tp ? tp->magic : 0, TTY_MAGIC, ch->ch_tun.un_flags));
740                 writew(head, &(bs->rx_tail));
741                 writeb(1, &(bs->idata));
742                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
743                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
744                 return;
745         }
746
747         /*
748          * If we are throttled, simply don't read any data.
749          */
750         if (ch->ch_flags & CH_RXBLOCK) {
751                 writeb(1, &(bs->idata));
752                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
753                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
754                 DPR_READ(("Port %d throttled, not reading any data. head: %x tail: %x\n",
755                         ch->ch_portnum, head, tail));
756                 return;
757         }
758
759         /*
760          *      Ignore oruns.
761          */
762         tmpchar = readb(&(bs->orun));
763         if (tmpchar) {
764                 ch->ch_err_overrun++;
765                 writeb(0, &(bs->orun));
766         }
767
768         DPR_READ(("dgap_input start 2\n"));
769
770         /* Decide how much data we can send into the tty layer */
771         flip_len = TTY_FLIPBUF_SIZE;
772
773         /* Chop down the length, if needed */
774         len = min(data_len, flip_len);
775         len = min(len, (N_TTY_BUF_SIZE - 1));
776
777         ld = tty_ldisc_ref(tp);
778
779 #ifdef TTY_DONT_FLIP
780         /*
781          * If the DONT_FLIP flag is on, don't flush our buffer, and act
782          * like the ld doesn't have any space to put the data right now.
783          */
784         if (test_bit(TTY_DONT_FLIP, &tp->flags))
785                 len = 0;
786 #endif
787
788         /*
789          * If we were unable to get a reference to the ld,
790          * don't flush our buffer, and act like the ld doesn't
791          * have any space to put the data right now.
792          */
793         if (!ld) {
794                 len = 0;
795         } else {
796                 /*
797                  * If ld doesn't have a pointer to a receive_buf function,
798                  * flush the data, then act like the ld doesn't have any
799                  * space to put the data right now.
800                  */
801                 if (!ld->ops->receive_buf) {
802                         writew(head, &(bs->rx_tail));
803                         len = 0;
804                 }
805         }
806
807         if (len <= 0) {
808                 writeb(1, &(bs->idata));
809                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
810                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
811                 DPR_READ(("dgap_input 1 - finish\n"));
812                 if (ld)
813                         tty_ldisc_deref(ld);
814                 return;
815         }
816
817         buf = ch->ch_bd->flipbuf;
818         n = len;
819
820         /*
821          * n now contains the most amount of data we can copy,
822          * bounded either by our buffer size or the amount
823          * of data the card actually has pending...
824          */
825         while (n) {
826
827                 s = ((head >= tail) ? head : ch->ch_rsize) - tail;
828                 s = min(s, n);
829
830                 if (s <= 0)
831                         break;
832
833                 memcpy_fromio(buf, (char *) ch->ch_raddr + tail, s);
834                 dgap_sniff_nowait_nolock(ch, "USER READ", buf, s);
835
836                 tail += s;
837                 buf += s;
838
839                 n -= s;
840                 /* Flip queue if needed */
841                 tail &= rmask;
842         }
843
844         writew(tail, &(bs->rx_tail));
845         writeb(1, &(bs->idata));
846         ch->ch_rxcount += len;
847
848         /*
849          * If we are completely raw, we don't need to go through a lot
850          * of the tty layers that exist.
851          * In this case, we take the shortest and fastest route we
852          * can to relay the data to the user.
853          *
854          * On the other hand, if we are not raw, we need to go through
855          * the tty layer, which has its API more well defined.
856          */
857         if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
858                 dgap_parity_scan(ch, ch->ch_bd->flipbuf, ch->ch_bd->flipflagbuf, &len);
859
860                 len = tty_buffer_request_room(tp->port, len);
861                 tty_insert_flip_string_flags(tp->port, ch->ch_bd->flipbuf,
862                         ch->ch_bd->flipflagbuf, len);
863         }
864         else {
865                 len = tty_buffer_request_room(tp->port, len);
866                 tty_insert_flip_string(tp->port, ch->ch_bd->flipbuf, len);
867         }
868
869         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
870         DGAP_UNLOCK(bd->bd_lock, lock_flags);
871
872         /* Tell the tty layer its okay to "eat" the data now */
873         tty_flip_buffer_push(tp->port);
874
875         if (ld)
876                 tty_ldisc_deref(ld);
877
878         DPR_READ(("dgap_input - finish\n"));
879 }
880
881
882 /************************************************************************
883  * Determines when CARRIER changes state and takes appropriate
884  * action.
885  ************************************************************************/
886 void dgap_carrier(struct channel_t *ch)
887 {
888         struct board_t *bd;
889
890         int virt_carrier = 0;
891         int phys_carrier = 0;
892
893         DPR_CARR(("dgap_carrier called...\n"));
894
895         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
896                 return;
897
898         bd = ch->ch_bd;
899
900         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
901                 return;
902
903         /* Make sure altpin is always set correctly */
904         if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
905                 ch->ch_dsr      = DM_CD;
906                 ch->ch_cd       = DM_DSR;
907         }
908         else {
909                 ch->ch_dsr      = DM_DSR;
910                 ch->ch_cd       = DM_CD;
911         }
912
913         if (ch->ch_mistat & D_CD(ch)) {
914                 DPR_CARR(("mistat: %x  D_CD: %x\n", ch->ch_mistat, D_CD(ch)));
915                 phys_carrier = 1;
916         }
917
918         if (ch->ch_digi.digi_flags & DIGI_FORCEDCD) {
919                 virt_carrier = 1;
920         }
921
922         if (ch->ch_c_cflag & CLOCAL) {
923                 virt_carrier = 1;
924         }
925
926
927         DPR_CARR(("DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier));
928
929         /*
930          * Test for a VIRTUAL carrier transition to HIGH.
931          */
932         if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
933
934                 /*
935                  * When carrier rises, wake any threads waiting
936                  * for carrier in the open routine.
937                  */
938
939                 DPR_CARR(("carrier: virt DCD rose\n"));
940
941                 if (waitqueue_active(&(ch->ch_flags_wait)))
942                         wake_up_interruptible(&ch->ch_flags_wait);
943         }
944
945         /*
946          * Test for a PHYSICAL carrier transition to HIGH.
947          */
948         if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
949
950                 /*
951                  * When carrier rises, wake any threads waiting
952                  * for carrier in the open routine.
953                  */
954
955                 DPR_CARR(("carrier: physical DCD rose\n"));
956
957                 if (waitqueue_active(&(ch->ch_flags_wait)))
958                         wake_up_interruptible(&ch->ch_flags_wait);
959         }
960
961         /*
962          *  Test for a PHYSICAL transition to low, so long as we aren't
963          *  currently ignoring physical transitions (which is what "virtual
964          *  carrier" indicates).
965          *
966          *  The transition of the virtual carrier to low really doesn't
967          *  matter... it really only means "ignore carrier state", not
968          *  "make pretend that carrier is there".
969          */
970         if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
971             (phys_carrier == 0))
972         {
973
974                 /*
975                  *   When carrier drops:
976                  *
977                  *   Drop carrier on all open units.
978                  *
979                  *   Flush queues, waking up any task waiting in the
980                  *   line discipline.
981                  *
982                  *   Send a hangup to the control terminal.
983                  *
984                  *   Enable all select calls.
985                  */
986                 if (waitqueue_active(&(ch->ch_flags_wait)))
987                         wake_up_interruptible(&ch->ch_flags_wait);
988
989                 if (ch->ch_tun.un_open_count > 0) {
990                         DPR_CARR(("Sending tty hangup\n"));
991                         tty_hangup(ch->ch_tun.un_tty);
992                 }
993
994                 if (ch->ch_pun.un_open_count > 0) {
995                         DPR_CARR(("Sending pr hangup\n"));
996                         tty_hangup(ch->ch_pun.un_tty);
997                 }
998         }
999
1000         /*
1001          *  Make sure that our cached values reflect the current reality.
1002          */
1003         if (virt_carrier == 1)
1004                 ch->ch_flags |= CH_FCAR;
1005         else
1006                 ch->ch_flags &= ~CH_FCAR;
1007
1008         if (phys_carrier == 1)
1009                 ch->ch_flags |= CH_CD;
1010         else
1011                 ch->ch_flags &= ~CH_CD;
1012 }
1013
1014
1015 /************************************************************************
1016  *
1017  * TTY Entry points and helper functions
1018  *
1019  ************************************************************************/
1020
1021 /*
1022  * dgap_tty_open()
1023  *
1024  */
1025 static int dgap_tty_open(struct tty_struct *tty, struct file *file)
1026 {
1027         struct board_t  *brd;
1028         struct channel_t *ch;
1029         struct un_t     *un;
1030         struct bs_t     *bs;
1031         uint            major = 0;
1032         uint            minor = 0;
1033         int             rc = 0;
1034         ulong           lock_flags;
1035         ulong           lock_flags2;
1036         u16             head;
1037
1038         rc = 0;
1039
1040         major = MAJOR(tty_devnum(tty));
1041         minor = MINOR(tty_devnum(tty));
1042
1043         if (major > 255) {
1044                 return -ENXIO;
1045         }
1046
1047         /* Get board pointer from our array of majors we have allocated */
1048         brd = dgap_BoardsByMajor[major];
1049         if (!brd) {
1050                 return -ENXIO;
1051         }
1052
1053         /*
1054          * If board is not yet up to a state of READY, go to
1055          * sleep waiting for it to happen or they cancel the open.
1056          */
1057         rc = wait_event_interruptible(brd->state_wait,
1058                 (brd->state & BOARD_READY));
1059
1060         if (rc) {
1061                 return rc;
1062         }
1063
1064         DGAP_LOCK(brd->bd_lock, lock_flags);
1065
1066         /* The wait above should guarantee this cannot happen */
1067         if (brd->state != BOARD_READY) {
1068                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
1069                 return -ENXIO;
1070         }
1071
1072         /* If opened device is greater than our number of ports, bail. */
1073         if (MINOR(tty_devnum(tty)) > brd->nasync) {
1074                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
1075                 return -ENXIO;
1076         }
1077
1078         ch = brd->channels[minor];
1079         if (!ch) {
1080                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
1081                 return -ENXIO;
1082         }
1083
1084         /* Grab channel lock */
1085         DGAP_LOCK(ch->ch_lock, lock_flags2);
1086
1087         /* Figure out our type */
1088         if (major == brd->dgap_Serial_Major) {
1089                 un = &brd->channels[minor]->ch_tun;
1090                 un->un_type = DGAP_SERIAL;
1091         }
1092         else if (major == brd->dgap_TransparentPrint_Major) {
1093                 un = &brd->channels[minor]->ch_pun;
1094                 un->un_type = DGAP_PRINT;
1095         }
1096         else {
1097                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1098                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
1099                 DPR_OPEN(("%d Unknown TYPE!\n", __LINE__));
1100                 return -ENXIO;
1101         }
1102
1103         /* Store our unit into driver_data, so we always have it available. */
1104         tty->driver_data = un;
1105
1106         DPR_OPEN(("Open called. MAJOR: %d MINOR:%d unit: %p NAME: %s\n",
1107                 MAJOR(tty_devnum(tty)), MINOR(tty_devnum(tty)), un, brd->name));
1108
1109         /*
1110          * Error if channel info pointer is NULL.
1111          */
1112         bs = ch->ch_bs;
1113         if (!bs) {
1114                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1115                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
1116                 DPR_OPEN(("%d BS is 0!\n", __LINE__));
1117                 return -ENXIO;
1118         }
1119
1120         DPR_OPEN(("%d: tflag=%x  pflag=%x\n", __LINE__, ch->ch_tun.un_flags, ch->ch_pun.un_flags));
1121
1122         /*
1123          * Initialize tty's
1124          */
1125         if (!(un->un_flags & UN_ISOPEN)) {
1126                 /* Store important variables. */
1127                 un->un_tty     = tty;
1128
1129                 /* Maybe do something here to the TTY struct as well? */
1130         }
1131
1132         /*
1133          * Initialize if neither terminal or printer is open.
1134          */
1135         if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1136
1137                 DPR_OPEN(("dgap_open: initializing channel in open...\n"));
1138
1139                 ch->ch_mforce = 0;
1140                 ch->ch_mval = 0;
1141
1142                 /*
1143                  * Flush input queue.
1144                  */
1145                 head = readw(&(bs->rx_head));
1146                 writew(head, &(bs->rx_tail));
1147
1148                 ch->ch_flags = 0;
1149                 ch->pscan_state = 0;
1150                 ch->pscan_savechar = 0;
1151
1152                 ch->ch_c_cflag   = tty->termios.c_cflag;
1153                 ch->ch_c_iflag   = tty->termios.c_iflag;
1154                 ch->ch_c_oflag   = tty->termios.c_oflag;
1155                 ch->ch_c_lflag   = tty->termios.c_lflag;
1156                 ch->ch_startc = tty->termios.c_cc[VSTART];
1157                 ch->ch_stopc  = tty->termios.c_cc[VSTOP];
1158
1159                 /* TODO: flush our TTY struct here? */
1160         }
1161
1162         dgap_carrier(ch);
1163         /*
1164          * Run param in case we changed anything
1165          */
1166         dgap_param(tty);
1167
1168         /*
1169          * follow protocol for opening port
1170          */
1171
1172         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1173         DGAP_UNLOCK(brd->bd_lock, lock_flags);
1174
1175         rc = dgap_block_til_ready(tty, file, ch);
1176
1177         if (!un->un_tty) {
1178                 return -ENODEV;
1179         }
1180
1181         if (rc) {
1182                 DPR_OPEN(("dgap_tty_open returning after dgap_block_til_ready "
1183                         "with %d\n", rc));
1184         }
1185
1186         /* No going back now, increment our unit and channel counters */
1187         DGAP_LOCK(ch->ch_lock, lock_flags);
1188         ch->ch_open_count++;
1189         un->un_open_count++;
1190         un->un_flags |= (UN_ISOPEN);
1191         DGAP_UNLOCK(ch->ch_lock, lock_flags);
1192
1193         DPR_OPEN(("dgap_tty_open finished\n"));
1194         return (rc);
1195 }
1196
1197
1198 /*
1199  * dgap_block_til_ready()
1200  *
1201  * Wait for DCD, if needed.
1202  */
1203 static int dgap_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1204 {
1205         int retval = 0;
1206         struct un_t *un = NULL;
1207         ulong   lock_flags;
1208         uint    old_flags = 0;
1209         int sleep_on_un_flags = 0;
1210
1211         if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGAP_CHANNEL_MAGIC) {
1212                 return (-ENXIO);
1213         }
1214
1215         un = tty->driver_data;
1216         if (!un || un->magic != DGAP_UNIT_MAGIC) {
1217                 return (-ENXIO);
1218         }
1219
1220         DPR_OPEN(("dgap_block_til_ready - before block.\n"));
1221
1222         DGAP_LOCK(ch->ch_lock, lock_flags);
1223
1224         ch->ch_wopen++;
1225
1226         /* Loop forever */
1227         while (1) {
1228
1229                 sleep_on_un_flags = 0;
1230
1231                 /*
1232                  * If board has failed somehow during our sleep, bail with error.
1233                  */
1234                 if (ch->ch_bd->state == BOARD_FAILED) {
1235                         retval = -ENXIO;
1236                         break;
1237                 }
1238
1239                 /* If tty was hung up, break out of loop and set error. */
1240                 if (tty_hung_up_p(file)) {
1241                         retval = -EAGAIN;
1242                         break;
1243                 }
1244
1245                 /*
1246                  * If either unit is in the middle of the fragile part of close,
1247                  * we just cannot touch the channel safely.
1248                  * Go back to sleep, knowing that when the channel can be
1249                  * touched safely, the close routine will signal the
1250                  * ch_wait_flags to wake us back up.
1251                  */
1252                 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1253
1254                         /*
1255                          * Our conditions to leave cleanly and happily:
1256                          * 1) NONBLOCKING on the tty is set.
1257                          * 2) CLOCAL is set.
1258                          * 3) DCD (fake or real) is active.
1259                          */
1260
1261                         if (file->f_flags & O_NONBLOCK) {
1262                                 break;
1263                         }
1264
1265                         if (tty->flags & (1 << TTY_IO_ERROR)) {
1266                                 break;
1267                         }
1268
1269                         if (ch->ch_flags & CH_CD) {
1270                                 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1271                                 break;
1272                         }
1273
1274                         if (ch->ch_flags & CH_FCAR) {
1275                                 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1276                                 break;
1277                         }
1278                 }
1279                 else {
1280                         sleep_on_un_flags = 1;
1281                 }
1282
1283                 /*
1284                  * If there is a signal pending, the user probably
1285                  * interrupted (ctrl-c) us.
1286                  * Leave loop with error set.
1287                  */
1288                 if (signal_pending(current)) {
1289                         DPR_OPEN(("%d: signal pending...\n", __LINE__));
1290                         retval = -ERESTARTSYS;
1291                         break;
1292                 }
1293
1294                 DPR_OPEN(("dgap_block_til_ready - blocking.\n"));
1295
1296                 /*
1297                  * Store the flags before we let go of channel lock
1298                  */
1299                 if (sleep_on_un_flags)
1300                         old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1301                 else
1302                         old_flags = ch->ch_flags;
1303
1304                 /*
1305                  * Let go of channel lock before calling schedule.
1306                  * Our poller will get any FEP events and wake us up when DCD
1307                  * eventually goes active.
1308                  */
1309
1310                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1311
1312                 DPR_OPEN(("Going to sleep on %s flags...\n",
1313                         (sleep_on_un_flags ? "un" : "ch")));
1314
1315                 /*
1316                  * Wait for something in the flags to change from the current value.
1317                  */
1318                 if (sleep_on_un_flags) {
1319                         retval = wait_event_interruptible(un->un_flags_wait,
1320                                 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1321                 }
1322                 else {
1323                         retval = wait_event_interruptible(ch->ch_flags_wait,
1324                                 (old_flags != ch->ch_flags));
1325                 }
1326
1327                 DPR_OPEN(("After sleep... retval: %x\n", retval));
1328
1329                 /*
1330                  * We got woken up for some reason.
1331                  * Before looping around, grab our channel lock.
1332                  */
1333                 DGAP_LOCK(ch->ch_lock, lock_flags);
1334         }
1335
1336         ch->ch_wopen--;
1337
1338         DGAP_UNLOCK(ch->ch_lock, lock_flags);
1339
1340         DPR_OPEN(("dgap_block_til_ready - after blocking.\n"));
1341
1342         if (retval) {
1343                 DPR_OPEN(("dgap_block_til_ready - done. error. retval: %x\n", retval));
1344                 return(retval);
1345         }
1346
1347         DPR_OPEN(("dgap_block_til_ready - done no error. jiffies: %lu\n", jiffies));
1348
1349         return(0);
1350 }
1351
1352
1353 /*
1354  * dgap_tty_hangup()
1355  *
1356  * Hangup the port.  Like a close, but don't wait for output to drain.
1357  */
1358 static void dgap_tty_hangup(struct tty_struct *tty)
1359 {
1360         struct board_t  *bd;
1361         struct channel_t *ch;
1362         struct un_t     *un;
1363
1364         if (!tty || tty->magic != TTY_MAGIC)
1365                 return;
1366
1367         un = tty->driver_data;
1368         if (!un || un->magic != DGAP_UNIT_MAGIC)
1369                 return;
1370
1371         ch = un->un_ch;
1372         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1373                 return;
1374
1375         bd = ch->ch_bd;
1376         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1377                 return;
1378
1379         DPR_CLOSE(("dgap_hangup called. ch->ch_open_count: %d un->un_open_count: %d\n",
1380                 ch->ch_open_count, un->un_open_count));
1381
1382         /* flush the transmit queues */
1383         dgap_tty_flush_buffer(tty);
1384
1385         DPR_CLOSE(("dgap_hangup finished. ch->ch_open_count: %d un->un_open_count: %d\n",
1386                 ch->ch_open_count, un->un_open_count));
1387 }
1388
1389
1390
1391 /*
1392  * dgap_tty_close()
1393  *
1394  */
1395 static void dgap_tty_close(struct tty_struct *tty, struct file *file)
1396 {
1397         struct ktermios *ts;
1398         struct board_t *bd;
1399         struct channel_t *ch;
1400         struct un_t *un;
1401         ulong lock_flags;
1402         int rc = 0;
1403
1404         if (!tty || tty->magic != TTY_MAGIC)
1405                 return;
1406
1407         un = tty->driver_data;
1408         if (!un || un->magic != DGAP_UNIT_MAGIC)
1409                 return;
1410
1411         ch = un->un_ch;
1412         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1413                 return;
1414
1415         bd = ch->ch_bd;
1416         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1417                 return;
1418
1419         ts = &tty->termios;
1420
1421         DPR_CLOSE(("Close called\n"));
1422
1423         DGAP_LOCK(ch->ch_lock, lock_flags);
1424
1425         /*
1426          * Determine if this is the last close or not - and if we agree about
1427          * which type of close it is with the Line Discipline
1428          */
1429         if ((tty->count == 1) && (un->un_open_count != 1)) {
1430                 /*
1431                  * Uh, oh.  tty->count is 1, which means that the tty
1432                  * structure will be freed.  un_open_count should always
1433                  * be one in these conditions.  If it's greater than
1434                  * one, we've got real problems, since it means the
1435                  * serial port won't be shutdown.
1436                  */
1437                 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1438                 un->un_open_count = 1;
1439         }
1440
1441         if (--un->un_open_count < 0) {
1442                 APR(("bad serial port open count of %d\n", un->un_open_count));
1443                 un->un_open_count = 0;
1444         }
1445
1446         ch->ch_open_count--;
1447
1448         if (ch->ch_open_count && un->un_open_count) {
1449                 DPR_CLOSE(("dgap_tty_close: not last close ch: %d un:%d\n",
1450                         ch->ch_open_count, un->un_open_count));
1451
1452                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1453                 return;
1454         }
1455
1456         /* OK, its the last close on the unit */
1457         DPR_CLOSE(("dgap_tty_close - last close on unit procedures\n"));
1458
1459         un->un_flags |= UN_CLOSING;
1460
1461         tty->closing = 1;
1462
1463         /*
1464          * Only officially close channel if count is 0 and
1465          * DIGI_PRINTER bit is not set.
1466          */
1467         if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1468
1469                 ch->ch_flags &= ~(CH_RXBLOCK);
1470
1471                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1472
1473                 /* wait for output to drain */
1474                 /* This will also return if we take an interrupt */
1475
1476                 DPR_CLOSE(("Calling wait_for_drain\n"));
1477                 rc = dgap_wait_for_drain(tty);
1478                 DPR_CLOSE(("After calling wait_for_drain\n"));
1479
1480                 if (rc) {
1481                         DPR_BASIC(("dgap_tty_close - bad return: %d ", rc));
1482                 }
1483
1484                 dgap_tty_flush_buffer(tty);
1485                 tty_ldisc_flush(tty);
1486
1487                 DGAP_LOCK(ch->ch_lock, lock_flags);
1488
1489                 tty->closing = 0;
1490
1491                 /*
1492                  * If we have HUPCL set, lower DTR and RTS
1493                  */
1494                 if (ch->ch_c_cflag & HUPCL ) {
1495                         DPR_CLOSE(("Close. HUPCL set, dropping DTR/RTS\n"));
1496                         ch->ch_mostat &= ~(D_RTS(ch)|D_DTR(ch));
1497                         dgap_cmdb( ch, SMODEM, 0, D_DTR(ch)|D_RTS(ch), 0 );
1498
1499                         /*
1500                          * Go to sleep to ensure RTS/DTR
1501                          * have been dropped for modems to see it.
1502                          */
1503                         if (ch->ch_close_delay) {
1504                                 DPR_CLOSE(("Close. Sleeping for RTS/DTR drop\n"));
1505
1506                                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1507                                 dgap_ms_sleep(ch->ch_close_delay);
1508                                 DGAP_LOCK(ch->ch_lock, lock_flags);
1509
1510                                 DPR_CLOSE(("Close. After sleeping for RTS/DTR drop\n"));
1511                         }
1512                 }
1513
1514                 ch->pscan_state = 0;
1515                 ch->pscan_savechar = 0;
1516                 ch->ch_baud_info = 0;
1517
1518         }
1519
1520         /*
1521          * turn off print device when closing print device.
1522          */
1523         if ((un->un_type == DGAP_PRINT)  && (ch->ch_flags & CH_PRON) ) {
1524                 dgap_wmove(ch, ch->ch_digi.digi_offstr,
1525                         (int) ch->ch_digi.digi_offlen);
1526                 ch->ch_flags &= ~CH_PRON;
1527         }
1528
1529         un->un_tty = NULL;
1530         un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1531         tty->driver_data = NULL;
1532
1533         DPR_CLOSE(("Close. Doing wakeups\n"));
1534         wake_up_interruptible(&ch->ch_flags_wait);
1535         wake_up_interruptible(&un->un_flags_wait);
1536
1537         DGAP_UNLOCK(ch->ch_lock, lock_flags);
1538
1539         DPR_BASIC(("dgap_tty_close - complete\n"));
1540 }
1541
1542
1543 /*
1544  * dgap_tty_chars_in_buffer()
1545  *
1546  * Return number of characters that have not been transmitted yet.
1547  *
1548  * This routine is used by the line discipline to determine if there
1549  * is data waiting to be transmitted/drained/flushed or not.
1550  */
1551 static int dgap_tty_chars_in_buffer(struct tty_struct *tty)
1552 {
1553         struct board_t *bd = NULL;
1554         struct channel_t *ch = NULL;
1555         struct un_t *un = NULL;
1556         struct bs_t *bs = NULL;
1557         uchar tbusy;
1558         uint chars = 0;
1559         u16 thead, ttail, tmask, chead, ctail;
1560         ulong   lock_flags = 0;
1561         ulong   lock_flags2 = 0;
1562
1563         if (tty == NULL)
1564                 return(0);
1565
1566         un = tty->driver_data;
1567         if (!un || un->magic != DGAP_UNIT_MAGIC)
1568                 return (0);
1569
1570         ch = un->un_ch;
1571         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1572                 return (0);
1573
1574         bd = ch->ch_bd;
1575         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1576                 return (0);
1577
1578         bs = ch->ch_bs;
1579         if (!bs)
1580                 return (0);
1581
1582         DGAP_LOCK(bd->bd_lock, lock_flags);
1583         DGAP_LOCK(ch->ch_lock, lock_flags2);
1584
1585         tmask = (ch->ch_tsize - 1);
1586
1587         /* Get Transmit queue pointers */
1588         thead = readw(&(bs->tx_head)) & tmask;
1589         ttail = readw(&(bs->tx_tail)) & tmask;
1590
1591         /* Get tbusy flag */
1592         tbusy = readb(&(bs->tbusy));
1593
1594         /* Get Command queue pointers */
1595         chead = readw(&(ch->ch_cm->cm_head));
1596         ctail = readw(&(ch->ch_cm->cm_tail));
1597
1598         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1599         DGAP_UNLOCK(bd->bd_lock, lock_flags);
1600
1601         /*
1602          * The only way we know for sure if there is no pending
1603          * data left to be transferred, is if:
1604          * 1) Transmit head and tail are equal (empty).
1605          * 2) Command queue head and tail are equal (empty).
1606          * 3) The "TBUSY" flag is 0. (Transmitter not busy).
1607          */
1608
1609         if ((ttail == thead) && (tbusy == 0) && (chead == ctail)) {
1610                 chars = 0;
1611         }
1612         else {
1613                 if (thead >= ttail)
1614                         chars = thead - ttail;
1615                 else
1616                         chars = thead - ttail + ch->ch_tsize;
1617                 /*
1618                  * Fudge factor here.
1619                  * If chars is zero, we know that the command queue had
1620                  * something in it or tbusy was set.  Because we cannot
1621                  * be sure if there is still some data to be transmitted,
1622                  * lets lie, and tell ld we have 1 byte left.
1623                  */
1624                 if (chars == 0) {
1625                         /*
1626                          * If TBUSY is still set, and our tx buffers are empty,
1627                          * force the firmware to send me another wakeup after
1628                          * TBUSY has been cleared.
1629                          */
1630                         if (tbusy != 0) {
1631                                 DGAP_LOCK(ch->ch_lock, lock_flags);
1632                                 un->un_flags |= UN_EMPTY;
1633                                 writeb(1, &(bs->iempty));
1634                                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1635                         }
1636                         chars = 1;
1637                 }
1638         }
1639
1640         DPR_WRITE(("dgap_tty_chars_in_buffer. Port: %x - %d (head: %d tail: %d tsize: %d)\n",
1641                 ch->ch_portnum, chars, thead, ttail, ch->ch_tsize));
1642         return(chars);
1643 }
1644
1645
1646 static int dgap_wait_for_drain(struct tty_struct *tty)
1647 {
1648         struct channel_t *ch;
1649         struct un_t *un;
1650         struct bs_t *bs;
1651         int ret = -EIO;
1652         uint count = 1;
1653         ulong   lock_flags = 0;
1654
1655         if (!tty || tty->magic != TTY_MAGIC)
1656                 return ret;
1657
1658         un = tty->driver_data;
1659         if (!un || un->magic != DGAP_UNIT_MAGIC)
1660                 return ret;
1661
1662         ch = un->un_ch;
1663         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1664                 return ret;
1665
1666         bs = ch->ch_bs;
1667         if (!bs)
1668                 return ret;
1669
1670         ret = 0;
1671
1672         DPR_DRAIN(("dgap_wait_for_drain start\n"));
1673
1674         /* Loop until data is drained */
1675         while (count != 0) {
1676
1677                 count = dgap_tty_chars_in_buffer(tty);
1678
1679                 if (count == 0)
1680                         break;
1681
1682                 /* Set flag waiting for drain */
1683                 DGAP_LOCK(ch->ch_lock, lock_flags);
1684                 un->un_flags |= UN_EMPTY;
1685                 writeb(1, &(bs->iempty));
1686                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1687
1688                 /* Go to sleep till we get woken up */
1689                 ret = wait_event_interruptible(un->un_flags_wait, ((un->un_flags & UN_EMPTY) == 0));
1690                 /* If ret is non-zero, user ctrl-c'ed us */
1691                 if (ret) {
1692                         break;
1693                 }
1694         }
1695
1696         DGAP_LOCK(ch->ch_lock, lock_flags);
1697         un->un_flags &= ~(UN_EMPTY);
1698         DGAP_UNLOCK(ch->ch_lock, lock_flags);
1699
1700         DPR_DRAIN(("dgap_wait_for_drain finish\n"));
1701         return (ret);
1702 }
1703
1704
1705 /*
1706  * dgap_maxcps_room
1707  *
1708  * Reduces bytes_available to the max number of characters
1709  * that can be sent currently given the maxcps value, and
1710  * returns the new bytes_available.  This only affects printer
1711  * output.
1712  */
1713 static int dgap_maxcps_room(struct tty_struct *tty, int bytes_available)
1714 {
1715         struct channel_t *ch = NULL;
1716         struct un_t *un = NULL;
1717
1718         if (tty == NULL)
1719                 return (bytes_available);
1720
1721         un = tty->driver_data;
1722         if (!un || un->magic != DGAP_UNIT_MAGIC)
1723                 return (bytes_available);
1724
1725         ch = un->un_ch;
1726         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1727                 return (bytes_available);
1728
1729         /*
1730          * If its not the Transparent print device, return
1731          * the full data amount.
1732          */
1733         if (un->un_type != DGAP_PRINT)
1734                 return (bytes_available);
1735
1736         if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0 ) {
1737                 int cps_limit = 0;
1738                 unsigned long current_time = jiffies;
1739                 unsigned long buffer_time = current_time +
1740                         (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1741
1742                 if (ch->ch_cpstime < current_time) {
1743                         /* buffer is empty */
1744                         ch->ch_cpstime = current_time;            /* reset ch_cpstime */
1745                         cps_limit = ch->ch_digi.digi_bufsize;
1746                 }
1747                 else if (ch->ch_cpstime < buffer_time) {
1748                         /* still room in the buffer */
1749                         cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1750                 }
1751                 else {
1752                         /* no room in the buffer */
1753                         cps_limit = 0;
1754                 }
1755
1756                 bytes_available = min(cps_limit, bytes_available);
1757         }
1758
1759         return (bytes_available);
1760 }
1761
1762
1763 static inline void dgap_set_firmware_event(struct un_t *un, unsigned int event)
1764 {
1765         struct channel_t *ch = NULL;
1766         struct bs_t *bs = NULL;
1767
1768         if (!un || un->magic != DGAP_UNIT_MAGIC)
1769                 return;
1770         ch = un->un_ch;
1771         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1772                 return;
1773         bs = ch->ch_bs;
1774         if (!bs)
1775                 return;
1776
1777         if ((event & UN_LOW) != 0) {
1778                 if ((un->un_flags & UN_LOW) == 0) {
1779                         un->un_flags |= UN_LOW;
1780                         writeb(1, &(bs->ilow));
1781                 }
1782         }
1783         if ((event & UN_LOW) != 0) {
1784                 if ((un->un_flags & UN_EMPTY) == 0) {
1785                         un->un_flags |= UN_EMPTY;
1786                         writeb(1, &(bs->iempty));
1787                 }
1788         }
1789 }
1790
1791
1792 /*
1793  * dgap_tty_write_room()
1794  *
1795  * Return space available in Tx buffer
1796  */
1797 static int dgap_tty_write_room(struct tty_struct *tty)
1798 {
1799         struct channel_t *ch = NULL;
1800         struct un_t *un = NULL;
1801         struct bs_t *bs = NULL;
1802         u16 head, tail, tmask;
1803         int ret = 0;
1804         ulong   lock_flags = 0;
1805
1806         if (tty == NULL || dgap_TmpWriteBuf == NULL)
1807                 return(0);
1808
1809         un = tty->driver_data;
1810         if (!un || un->magic != DGAP_UNIT_MAGIC)
1811                 return (0);
1812
1813         ch = un->un_ch;
1814         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1815                 return (0);
1816
1817         bs = ch->ch_bs;
1818         if (!bs)
1819                 return (0);
1820
1821         DGAP_LOCK(ch->ch_lock, lock_flags);
1822
1823         tmask = ch->ch_tsize - 1;
1824         head = readw(&(bs->tx_head)) & tmask;
1825         tail = readw(&(bs->tx_tail)) & tmask;
1826
1827         if ((ret = tail - head - 1) < 0)
1828                 ret += ch->ch_tsize;
1829
1830         /* Limit printer to maxcps */
1831         ret = dgap_maxcps_room(tty, ret);
1832
1833         /*
1834          * If we are printer device, leave space for
1835          * possibly both the on and off strings.
1836          */
1837         if (un->un_type == DGAP_PRINT) {
1838                 if (!(ch->ch_flags & CH_PRON))
1839                         ret -= ch->ch_digi.digi_onlen;
1840                 ret -= ch->ch_digi.digi_offlen;
1841         }
1842         else {
1843                 if (ch->ch_flags & CH_PRON)
1844                         ret -= ch->ch_digi.digi_offlen;
1845         }
1846
1847         if (ret < 0)
1848                 ret = 0;
1849
1850         /*
1851          * Schedule FEP to wake us up if needed.
1852          *
1853          * TODO:  This might be overkill...
1854          * Do we really need to schedule callbacks from the FEP
1855          * in every case?  Can we get smarter based on ret?
1856          */
1857         dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
1858         DGAP_UNLOCK(ch->ch_lock, lock_flags);
1859
1860         DPR_WRITE(("dgap_tty_write_room - %d tail: %d head: %d\n", ret, tail, head));
1861
1862         return(ret);
1863 }
1864
1865
1866 /*
1867  * dgap_tty_put_char()
1868  *
1869  * Put a character into ch->ch_buf
1870  *
1871  *      - used by the line discipline for OPOST processing
1872  */
1873 static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c)
1874 {
1875         /*
1876          * Simply call tty_write.
1877          */
1878         DPR_WRITE(("dgap_tty_put_char called\n"));
1879         dgap_tty_write(tty, &c, 1);
1880         return 1;
1881 }
1882
1883
1884 /*
1885  * dgap_tty_write()
1886  *
1887  * Take data from the user or kernel and send it out to the FEP.
1888  * In here exists all the Transparent Print magic as well.
1889  */
1890 static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1891 {
1892         struct channel_t *ch = NULL;
1893         struct un_t *un = NULL;
1894         struct bs_t *bs = NULL;
1895         char *vaddr = NULL;
1896         u16 head, tail, tmask, remain;
1897         int bufcount = 0, n = 0;
1898         int orig_count = 0;
1899         ulong lock_flags;
1900         int from_user = 0;
1901
1902         if (tty == NULL || dgap_TmpWriteBuf == NULL)
1903                 return(0);
1904
1905         un = tty->driver_data;
1906         if (!un || un->magic != DGAP_UNIT_MAGIC)
1907                 return (0);
1908
1909         ch = un->un_ch;
1910         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1911                 return(0);
1912
1913         bs = ch->ch_bs;
1914         if (!bs)
1915                 return(0);
1916
1917         if (!count)
1918                 return(0);
1919
1920         DPR_WRITE(("dgap_tty_write: Port: %x tty=%p user=%d len=%d\n",
1921                 ch->ch_portnum, tty, from_user, count));
1922
1923         /*
1924          * Store original amount of characters passed in.
1925          * This helps to figure out if we should ask the FEP
1926          * to send us an event when it has more space available.
1927          */
1928         orig_count = count;
1929
1930         DGAP_LOCK(ch->ch_lock, lock_flags);
1931
1932         /* Get our space available for the channel from the board */
1933         tmask = ch->ch_tsize - 1;
1934         head = readw(&(bs->tx_head)) & tmask;
1935         tail = readw(&(bs->tx_tail)) & tmask;
1936
1937         if ((bufcount = tail - head - 1) < 0)
1938                 bufcount += ch->ch_tsize;
1939
1940         DPR_WRITE(("%d: bufcount: %x count: %x tail: %x head: %x tmask: %x\n",
1941                 __LINE__, bufcount, count, tail, head, tmask));
1942
1943         /*
1944          * Limit printer output to maxcps overall, with bursts allowed
1945          * up to bufsize characters.
1946          */
1947         bufcount = dgap_maxcps_room(tty, bufcount);
1948
1949         /*
1950          * Take minimum of what the user wants to send, and the
1951          * space available in the FEP buffer.
1952          */
1953         count = min(count, bufcount);
1954
1955         /*
1956          * Bail if no space left.
1957          */
1958         if (count <= 0) {
1959                 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
1960                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1961                 return(0);
1962         }
1963
1964         /*
1965          * Output the printer ON string, if we are in terminal mode, but
1966          * need to be in printer mode.
1967          */
1968         if ((un->un_type == DGAP_PRINT) && !(ch->ch_flags & CH_PRON)) {
1969                 dgap_wmove(ch, ch->ch_digi.digi_onstr,
1970                     (int) ch->ch_digi.digi_onlen);
1971                 head = readw(&(bs->tx_head)) & tmask;
1972                 ch->ch_flags |= CH_PRON;
1973         }
1974
1975         /*
1976          * On the other hand, output the printer OFF string, if we are
1977          * currently in printer mode, but need to output to the terminal.
1978          */
1979         if ((un->un_type != DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
1980                 dgap_wmove(ch, ch->ch_digi.digi_offstr,
1981                         (int) ch->ch_digi.digi_offlen);
1982                 head = readw(&(bs->tx_head)) & tmask;
1983                 ch->ch_flags &= ~CH_PRON;
1984         }
1985
1986         /*
1987          * If there is nothing left to copy, or I can't handle any more data, leave.
1988          */
1989         if (count <= 0) {
1990                 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
1991                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
1992                 return(0);
1993         }
1994
1995         if (from_user) {
1996
1997                 count = min(count, WRITEBUFLEN);
1998
1999                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2000
2001                 /*
2002                  * If data is coming from user space, copy it into a temporary
2003                  * buffer so we don't get swapped out while doing the copy to
2004                  * the board.
2005                  */
2006                 /* we're allowed to block if it's from_user */
2007                 if (down_interruptible(&dgap_TmpWriteSem)) {
2008                         return (-EINTR);
2009                 }
2010
2011                 if (copy_from_user(dgap_TmpWriteBuf, (const uchar __user *) buf, count)) {
2012                         up(&dgap_TmpWriteSem);
2013                         printk("Write: Copy from user failed!\n");
2014                         return -EFAULT;
2015                 }
2016
2017                 DGAP_LOCK(ch->ch_lock, lock_flags);
2018
2019                 buf = dgap_TmpWriteBuf;
2020         }
2021
2022         n = count;
2023
2024         /*
2025          * If the write wraps over the top of the circular buffer,
2026          * move the portion up to the wrap point, and reset the
2027          * pointers to the bottom.
2028          */
2029         remain = ch->ch_tstart + ch->ch_tsize - head;
2030
2031         if (n >= remain) {
2032                 n -= remain;
2033                 vaddr = ch->ch_taddr + head;
2034
2035                 memcpy_toio(vaddr, (uchar *) buf, remain);
2036                 dgap_sniff_nowait_nolock(ch, "USER WRITE", (uchar *) buf, remain);
2037
2038                 head = ch->ch_tstart;
2039                 buf += remain;
2040         }
2041
2042         if (n > 0) {
2043
2044                 /*
2045                  * Move rest of data.
2046                  */
2047                 vaddr = ch->ch_taddr + head;
2048                 remain = n;
2049
2050                 memcpy_toio(vaddr, (uchar *) buf, remain);
2051                 dgap_sniff_nowait_nolock(ch, "USER WRITE", (uchar *) buf, remain);
2052
2053                 head += remain;
2054
2055         }
2056
2057         if (count) {
2058                 ch->ch_txcount += count;
2059                 head &= tmask;
2060                 writew(head, &(bs->tx_head));
2061         }
2062
2063
2064         dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
2065
2066         /*
2067          * If this is the print device, and the
2068          * printer is still on, we need to turn it
2069          * off before going idle.  If the buffer is
2070          * non-empty, wait until it goes empty.
2071          * Otherwise turn it off right now.
2072          */
2073         if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
2074                 tail = readw(&(bs->tx_tail)) & tmask;
2075
2076                 if (tail != head) {
2077                         un->un_flags |= UN_EMPTY;
2078                         writeb(1, &(bs->iempty));
2079                 }
2080                 else {
2081                         dgap_wmove(ch, ch->ch_digi.digi_offstr,
2082                                 (int) ch->ch_digi.digi_offlen);
2083                         head = readw(&(bs->tx_head)) & tmask;
2084                         ch->ch_flags &= ~CH_PRON;
2085                 }
2086         }
2087
2088         /* Update printer buffer empty time. */
2089         if ((un->un_type == DGAP_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2090             && (ch->ch_digi.digi_bufsize > 0)) {
2091                 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2092         }
2093
2094         if (from_user) {
2095                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2096                 up(&dgap_TmpWriteSem);
2097         }
2098         else {
2099                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2100         }
2101
2102         DPR_WRITE(("Write finished - Write %d bytes of %d.\n", count, orig_count));
2103
2104         return (count);
2105 }
2106
2107
2108
2109 /*
2110  * Return modem signals to ld.
2111  */
2112 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2113 static int dgap_tty_tiocmget(struct tty_struct *tty)
2114 #else
2115 static int dgap_tty_tiocmget(struct tty_struct *tty, struct file *file)
2116 #endif
2117 {
2118         struct channel_t *ch;
2119         struct un_t *un;
2120         int result = -EIO;
2121         uchar mstat = 0;
2122         ulong lock_flags;
2123
2124         if (!tty || tty->magic != TTY_MAGIC)
2125                 return result;
2126
2127         un = tty->driver_data;
2128         if (!un || un->magic != DGAP_UNIT_MAGIC)
2129                 return result;
2130
2131         ch = un->un_ch;
2132         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2133                 return result;
2134
2135         DPR_IOCTL(("dgap_tty_tiocmget start\n"));
2136
2137         DGAP_LOCK(ch->ch_lock, lock_flags);
2138
2139         mstat = readb(&(ch->ch_bs->m_stat));
2140         /* Append any outbound signals that might be pending... */
2141         mstat |= ch->ch_mostat;
2142
2143         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2144
2145         result = 0;
2146
2147         if (mstat & D_DTR(ch))
2148                 result |= TIOCM_DTR;
2149         if (mstat & D_RTS(ch))
2150                 result |= TIOCM_RTS;
2151         if (mstat & D_CTS(ch))
2152                 result |= TIOCM_CTS;
2153         if (mstat & D_DSR(ch))
2154                 result |= TIOCM_DSR;
2155         if (mstat & D_RI(ch))
2156                 result |= TIOCM_RI;
2157         if (mstat & D_CD(ch))
2158                 result |= TIOCM_CD;
2159
2160         DPR_IOCTL(("dgap_tty_tiocmget finish\n"));
2161
2162         return result;
2163 }
2164
2165
2166 /*
2167  * dgap_tty_tiocmset()
2168  *
2169  * Set modem signals, called by ld.
2170  */
2171 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2172 static int dgap_tty_tiocmset(struct tty_struct *tty,
2173                 unsigned int set, unsigned int clear)
2174 #else
2175 static int dgap_tty_tiocmset(struct tty_struct *tty, struct file *file,
2176                 unsigned int set, unsigned int clear)
2177 #endif
2178 {
2179         struct board_t *bd;
2180         struct channel_t *ch;
2181         struct un_t *un;
2182         int ret = -EIO;
2183         ulong lock_flags;
2184         ulong lock_flags2;
2185
2186         if (!tty || tty->magic != TTY_MAGIC)
2187                 return ret;
2188
2189         un = tty->driver_data;
2190         if (!un || un->magic != DGAP_UNIT_MAGIC)
2191                 return ret;
2192
2193         ch = un->un_ch;
2194         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2195                 return ret;
2196
2197         bd = ch->ch_bd;
2198         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2199                 return ret;
2200
2201         DPR_IOCTL(("dgap_tty_tiocmset start\n"));
2202
2203         DGAP_LOCK(bd->bd_lock, lock_flags);
2204         DGAP_LOCK(ch->ch_lock, lock_flags2);
2205
2206         if (set & TIOCM_RTS) {
2207                 ch->ch_mforce |= D_RTS(ch);
2208                 ch->ch_mval   |= D_RTS(ch);
2209         }
2210
2211         if (set & TIOCM_DTR) {
2212                 ch->ch_mforce |= D_DTR(ch);
2213                 ch->ch_mval   |= D_DTR(ch);
2214         }
2215
2216         if (clear & TIOCM_RTS) {
2217                 ch->ch_mforce |= D_RTS(ch);
2218                 ch->ch_mval   &= ~(D_RTS(ch));
2219         }
2220
2221         if (clear & TIOCM_DTR) {
2222                 ch->ch_mforce |= D_DTR(ch);
2223                 ch->ch_mval   &= ~(D_DTR(ch));
2224         }
2225
2226         dgap_param(tty);
2227
2228         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2229         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2230
2231         DPR_IOCTL(("dgap_tty_tiocmset finish\n"));
2232
2233         return (0);
2234 }
2235
2236
2237
2238 /*
2239  * dgap_tty_send_break()
2240  *
2241  * Send a Break, called by ld.
2242  */
2243 static int dgap_tty_send_break(struct tty_struct *tty, int msec)
2244 {
2245         struct board_t *bd;
2246         struct channel_t *ch;
2247         struct un_t *un;
2248         int ret = -EIO;
2249         ulong lock_flags;
2250         ulong lock_flags2;
2251
2252         if (!tty || tty->magic != TTY_MAGIC)
2253                 return ret;
2254
2255         un = tty->driver_data;
2256         if (!un || un->magic != DGAP_UNIT_MAGIC)
2257                 return ret;
2258
2259         ch = un->un_ch;
2260         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2261                 return ret;
2262
2263         bd = ch->ch_bd;
2264         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2265                 return ret;
2266
2267         switch (msec) {
2268         case -1:
2269                 msec = 0xFFFF;
2270                 break;
2271         case 0:
2272                 msec = 1;
2273                 break;
2274         default:
2275                 msec /= 10;
2276                 break;
2277         }
2278
2279         DPR_IOCTL(("dgap_tty_send_break start 1.  %lx\n", jiffies));
2280
2281         DGAP_LOCK(bd->bd_lock, lock_flags);
2282         DGAP_LOCK(ch->ch_lock, lock_flags2);
2283 #if 0
2284         dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
2285 #endif
2286         dgap_cmdw(ch, SBREAK, (u16) msec, 0);
2287
2288         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2289         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2290
2291         DPR_IOCTL(("dgap_tty_send_break finish\n"));
2292
2293         return (0);
2294 }
2295
2296
2297
2298
2299 /*
2300  * dgap_tty_wait_until_sent()
2301  *
2302  * wait until data has been transmitted, called by ld.
2303  */
2304 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2305 {
2306         int rc;
2307         rc = dgap_wait_for_drain(tty);
2308         if (rc) {
2309                 DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
2310                 return;
2311         }
2312         return;
2313 }
2314
2315
2316
2317 /*
2318  * dgap_send_xchar()
2319  *
2320  * send a high priority character, called by ld.
2321  */
2322 static void dgap_tty_send_xchar(struct tty_struct *tty, char c)
2323 {
2324         struct board_t *bd;
2325         struct channel_t *ch;
2326         struct un_t *un;
2327         ulong lock_flags;
2328         ulong lock_flags2;
2329
2330         if (!tty || tty->magic != TTY_MAGIC)
2331                 return;
2332
2333         un = tty->driver_data;
2334         if (!un || un->magic != DGAP_UNIT_MAGIC)
2335                 return;
2336
2337         ch = un->un_ch;
2338         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2339                 return;
2340
2341         bd = ch->ch_bd;
2342         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2343                 return;
2344
2345         DPR_IOCTL(("dgap_tty_send_xchar start 1.  %lx\n", jiffies));
2346
2347         DGAP_LOCK(bd->bd_lock, lock_flags);
2348         DGAP_LOCK(ch->ch_lock, lock_flags2);
2349
2350         /*
2351          * This is technically what we should do.
2352          * However, the NIST tests specifically want
2353          * to see each XON or XOFF character that it
2354          * sends, so lets just send each character
2355          * by hand...
2356          */
2357 #if 0
2358         if (c == STOP_CHAR(tty)) {
2359                 dgap_cmdw(ch, RPAUSE, 0, 0);
2360         }
2361         else if (c == START_CHAR(tty)) {
2362                 dgap_cmdw(ch, RRESUME, 0, 0);
2363         }
2364         else {
2365                 dgap_wmove(ch, &c, 1);
2366         }
2367 #else
2368         dgap_wmove(ch, &c, 1);
2369 #endif
2370
2371         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2372         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2373
2374         DPR_IOCTL(("dgap_tty_send_xchar finish\n"));
2375
2376         return;
2377 }
2378
2379
2380
2381
2382 /*
2383  * Return modem signals to ld.
2384  */
2385 static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2386 {
2387         int result = 0;
2388         uchar mstat = 0;
2389         ulong lock_flags;
2390         int rc = 0;
2391
2392         DPR_IOCTL(("dgap_get_modem_info start\n"));
2393
2394         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2395                 return(-ENXIO);
2396
2397         DGAP_LOCK(ch->ch_lock, lock_flags);
2398
2399         mstat = readb(&(ch->ch_bs->m_stat));
2400         /* Append any outbound signals that might be pending... */
2401         mstat |= ch->ch_mostat;
2402
2403         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2404
2405         result = 0;
2406
2407         if (mstat & D_DTR(ch))
2408                 result |= TIOCM_DTR;
2409         if (mstat & D_RTS(ch))
2410                 result |= TIOCM_RTS;
2411         if (mstat & D_CTS(ch))
2412                 result |= TIOCM_CTS;
2413         if (mstat & D_DSR(ch))
2414                 result |= TIOCM_DSR;
2415         if (mstat & D_RI(ch))
2416                 result |= TIOCM_RI;
2417         if (mstat & D_CD(ch))
2418                 result |= TIOCM_CD;
2419
2420         rc = put_user(result, value);
2421
2422         DPR_IOCTL(("dgap_get_modem_info finish\n"));
2423         return(rc);
2424 }
2425
2426
2427 /*
2428  * dgap_set_modem_info()
2429  *
2430  * Set modem signals, called by ld.
2431  */
2432 static int dgap_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2433 {
2434         struct board_t *bd;
2435         struct channel_t *ch;
2436         struct un_t *un;
2437         int ret = -ENXIO;
2438         unsigned int arg = 0;
2439         ulong lock_flags;
2440         ulong lock_flags2;
2441
2442         if (!tty || tty->magic != TTY_MAGIC)
2443                 return ret;
2444
2445         un = tty->driver_data;
2446         if (!un || un->magic != DGAP_UNIT_MAGIC)
2447                 return ret;
2448
2449         ch = un->un_ch;
2450         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2451                 return ret;
2452
2453         bd = ch->ch_bd;
2454         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2455                 return ret;
2456
2457         DPR_IOCTL(("dgap_set_modem_info() start\n"));
2458
2459         ret = get_user(arg, value);
2460         if (ret) {
2461                 DPR_IOCTL(("dgap_set_modem_info %d ret: %x. finished.\n", __LINE__, ret));
2462                 return(ret);
2463         }
2464
2465         DPR_IOCTL(("dgap_set_modem_info: command: %x arg: %x\n", command, arg));
2466
2467         switch (command) {
2468         case TIOCMBIS:
2469                 if (arg & TIOCM_RTS) {
2470                         ch->ch_mforce |= D_RTS(ch);
2471                         ch->ch_mval   |= D_RTS(ch);
2472                 }
2473
2474                 if (arg & TIOCM_DTR) {
2475                         ch->ch_mforce |= D_DTR(ch);
2476                         ch->ch_mval   |= D_DTR(ch);
2477                 }
2478
2479                 break;
2480
2481         case TIOCMBIC:
2482                 if (arg & TIOCM_RTS) {
2483                         ch->ch_mforce |= D_RTS(ch);
2484                         ch->ch_mval   &= ~(D_RTS(ch));
2485                 }
2486
2487                 if (arg & TIOCM_DTR) {
2488                         ch->ch_mforce |= D_DTR(ch);
2489                         ch->ch_mval   &= ~(D_DTR(ch));
2490                 }
2491
2492                 break;
2493
2494         case TIOCMSET:
2495                 ch->ch_mforce = D_DTR(ch)|D_RTS(ch);
2496
2497                 if (arg & TIOCM_RTS) {
2498                         ch->ch_mval |= D_RTS(ch);
2499                 }
2500                 else {
2501                         ch->ch_mval &= ~(D_RTS(ch));
2502                 }
2503
2504                 if (arg & TIOCM_DTR) {
2505                         ch->ch_mval |= (D_DTR(ch));
2506                 }
2507                 else {
2508                         ch->ch_mval &= ~(D_DTR(ch));
2509                 }
2510
2511                 break;
2512
2513         default:
2514                 return(-EINVAL);
2515         }
2516
2517         DGAP_LOCK(bd->bd_lock, lock_flags);
2518         DGAP_LOCK(ch->ch_lock, lock_flags2);
2519
2520         dgap_param(tty);
2521
2522         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2523         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2524
2525         DPR_IOCTL(("dgap_set_modem_info finish\n"));
2526
2527         return (0);
2528 }
2529
2530
2531 /*
2532  * dgap_tty_digigeta()
2533  *
2534  * Ioctl to get the information for ditty.
2535  *
2536  *
2537  *
2538  */
2539 static int dgap_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2540 {
2541         struct channel_t *ch;
2542         struct un_t *un;
2543         struct digi_t tmp;
2544         ulong lock_flags;
2545
2546         if (!retinfo)
2547                 return (-EFAULT);
2548
2549         if (!tty || tty->magic != TTY_MAGIC)
2550                 return (-EFAULT);
2551
2552         un = tty->driver_data;
2553         if (!un || un->magic != DGAP_UNIT_MAGIC)
2554                 return (-EFAULT);
2555
2556         ch = un->un_ch;
2557         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2558                 return (-EFAULT);
2559
2560         memset(&tmp, 0, sizeof(tmp));
2561
2562         DGAP_LOCK(ch->ch_lock, lock_flags);
2563         memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2564         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2565
2566         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2567                 return (-EFAULT);
2568
2569         return (0);
2570 }
2571
2572
2573 /*
2574  * dgap_tty_digiseta()
2575  *
2576  * Ioctl to set the information for ditty.
2577  *
2578  *
2579  *
2580  */
2581 static int dgap_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2582 {
2583         struct board_t *bd;
2584         struct channel_t *ch;
2585         struct un_t *un;
2586         struct digi_t new_digi;
2587         ulong   lock_flags = 0;
2588         unsigned long lock_flags2;
2589
2590         DPR_IOCTL(("DIGI_SETA start\n"));
2591
2592         if (!tty || tty->magic != TTY_MAGIC)
2593                 return (-EFAULT);
2594
2595         un = tty->driver_data;
2596         if (!un || un->magic != DGAP_UNIT_MAGIC)
2597                 return (-EFAULT);
2598
2599         ch = un->un_ch;
2600         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2601                 return (-EFAULT);
2602
2603         bd = ch->ch_bd;
2604         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2605                 return (-EFAULT);
2606
2607         if (copy_from_user(&new_digi, new_info, sizeof(struct digi_t))) {
2608                 DPR_IOCTL(("DIGI_SETA failed copy_from_user\n"));
2609                 return(-EFAULT);
2610         }
2611
2612         DGAP_LOCK(bd->bd_lock, lock_flags);
2613         DGAP_LOCK(ch->ch_lock, lock_flags2);
2614
2615         memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
2616
2617         if (ch->ch_digi.digi_maxcps < 1)
2618                 ch->ch_digi.digi_maxcps = 1;
2619
2620         if (ch->ch_digi.digi_maxcps > 10000)
2621                 ch->ch_digi.digi_maxcps = 10000;
2622
2623         if (ch->ch_digi.digi_bufsize < 10)
2624                 ch->ch_digi.digi_bufsize = 10;
2625
2626         if (ch->ch_digi.digi_maxchar < 1)
2627                 ch->ch_digi.digi_maxchar = 1;
2628
2629         if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2630                 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2631
2632         if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2633                 ch->ch_digi.digi_onlen = DIGI_PLEN;
2634
2635         if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2636                 ch->ch_digi.digi_offlen = DIGI_PLEN;
2637
2638         dgap_param(tty);
2639
2640         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2641         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2642
2643         DPR_IOCTL(("DIGI_SETA finish\n"));
2644
2645         return(0);
2646 }
2647
2648
2649 /*
2650  * dgap_tty_digigetedelay()
2651  *
2652  * Ioctl to get the current edelay setting.
2653  *
2654  *
2655  *
2656  */
2657 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo)
2658 {
2659         struct channel_t *ch;
2660         struct un_t *un;
2661         int tmp;
2662         ulong lock_flags;
2663
2664         if (!retinfo)
2665                 return (-EFAULT);
2666
2667         if (!tty || tty->magic != TTY_MAGIC)
2668                 return (-EFAULT);
2669
2670         un = tty->driver_data;
2671         if (!un || un->magic != DGAP_UNIT_MAGIC)
2672                 return (-EFAULT);
2673
2674         ch = un->un_ch;
2675         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2676                 return (-EFAULT);
2677
2678         memset(&tmp, 0, sizeof(tmp));
2679
2680         DGAP_LOCK(ch->ch_lock, lock_flags);
2681         tmp = readw(&(ch->ch_bs->edelay));
2682         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2683
2684         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2685                 return (-EFAULT);
2686
2687         return (0);
2688 }
2689
2690
2691 /*
2692  * dgap_tty_digisetedelay()
2693  *
2694  * Ioctl to set the EDELAY setting
2695  *
2696  */
2697 static int dgap_tty_digisetedelay(struct tty_struct *tty, int __user *new_info)
2698 {
2699         struct board_t *bd;
2700         struct channel_t *ch;
2701         struct un_t *un;
2702         int new_digi;
2703         ulong lock_flags;
2704         ulong lock_flags2;
2705
2706         DPR_IOCTL(("DIGI_SETA start\n"));
2707
2708         if (!tty || tty->magic != TTY_MAGIC)
2709                 return (-EFAULT);
2710
2711         un = tty->driver_data;
2712         if (!un || un->magic != DGAP_UNIT_MAGIC)
2713                 return (-EFAULT);
2714
2715         ch = un->un_ch;
2716         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2717                 return (-EFAULT);
2718
2719         bd = ch->ch_bd;
2720         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2721                 return (-EFAULT);
2722
2723         if (copy_from_user(&new_digi, new_info, sizeof(int))) {
2724                 DPR_IOCTL(("DIGI_SETEDELAY failed copy_from_user\n"));
2725                 return(-EFAULT);
2726         }
2727
2728         DGAP_LOCK(bd->bd_lock, lock_flags);
2729         DGAP_LOCK(ch->ch_lock, lock_flags2);
2730
2731         writew((u16) new_digi, &(ch->ch_bs->edelay));
2732
2733         dgap_param(tty);
2734
2735         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2736         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2737
2738         DPR_IOCTL(("DIGI_SETA finish\n"));
2739
2740         return(0);
2741 }
2742
2743
2744 /*
2745  * dgap_tty_digigetcustombaud()
2746  *
2747  * Ioctl to get the current custom baud rate setting.
2748  */
2749 static int dgap_tty_digigetcustombaud(struct tty_struct *tty, int __user *retinfo)
2750 {
2751         struct channel_t *ch;
2752         struct un_t *un;
2753         int tmp;
2754         ulong lock_flags;
2755
2756         if (!retinfo)
2757                 return (-EFAULT);
2758
2759         if (!tty || tty->magic != TTY_MAGIC)
2760                 return (-EFAULT);
2761
2762         un = tty->driver_data;
2763         if (!un || un->magic != DGAP_UNIT_MAGIC)
2764                 return (-EFAULT);
2765
2766         ch = un->un_ch;
2767         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2768                 return (-EFAULT);
2769
2770         memset(&tmp, 0, sizeof(tmp));
2771
2772         DGAP_LOCK(ch->ch_lock, lock_flags);
2773         tmp = dgap_get_custom_baud(ch);
2774         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2775
2776         DPR_IOCTL(("DIGI_GETCUSTOMBAUD. Returning %d\n", tmp));
2777
2778         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2779                 return (-EFAULT);
2780
2781         return (0);
2782 }
2783
2784
2785 /*
2786  * dgap_tty_digisetcustombaud()
2787  *
2788  * Ioctl to set the custom baud rate setting
2789  */
2790 static int dgap_tty_digisetcustombaud(struct tty_struct *tty, int __user *new_info)
2791 {
2792         struct board_t *bd;
2793         struct channel_t *ch;
2794         struct un_t *un;
2795         uint new_rate;
2796         ulong lock_flags;
2797         ulong lock_flags2;
2798
2799         DPR_IOCTL(("DIGI_SETCUSTOMBAUD start\n"));
2800
2801         if (!tty || tty->magic != TTY_MAGIC)
2802                 return (-EFAULT);
2803
2804         un = tty->driver_data;
2805         if (!un || un->magic != DGAP_UNIT_MAGIC)
2806                 return (-EFAULT);
2807
2808         ch = un->un_ch;
2809         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2810                 return (-EFAULT);
2811
2812         bd = ch->ch_bd;
2813         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2814                 return (-EFAULT);
2815
2816
2817         if (copy_from_user(&new_rate, new_info, sizeof(unsigned int))) {
2818                 DPR_IOCTL(("DIGI_SETCUSTOMBAUD failed copy_from_user\n"));
2819                 return(-EFAULT);
2820         }
2821
2822         if (bd->bd_flags & BD_FEP5PLUS) {
2823
2824                 DPR_IOCTL(("DIGI_SETCUSTOMBAUD. Setting %d\n", new_rate));
2825
2826                 DGAP_LOCK(bd->bd_lock, lock_flags);
2827                 DGAP_LOCK(ch->ch_lock, lock_flags2);
2828
2829                 ch->ch_custom_speed = new_rate;
2830
2831                 dgap_param(tty);
2832
2833                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2834                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
2835         }
2836
2837         DPR_IOCTL(("DIGI_SETCUSTOMBAUD finish\n"));
2838
2839         return(0);
2840 }
2841
2842
2843 /*
2844  * dgap_set_termios()
2845  */
2846 static void dgap_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2847 {
2848         struct board_t *bd;
2849         struct channel_t *ch;
2850         struct un_t *un;
2851         unsigned long lock_flags;
2852         unsigned long lock_flags2;
2853
2854         if (!tty || tty->magic != TTY_MAGIC)
2855                 return;
2856
2857         un = tty->driver_data;
2858         if (!un || un->magic != DGAP_UNIT_MAGIC)
2859                 return;
2860
2861         ch = un->un_ch;
2862         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2863                 return;
2864
2865         bd = ch->ch_bd;
2866         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2867                 return;
2868
2869         DGAP_LOCK(bd->bd_lock, lock_flags);
2870         DGAP_LOCK(ch->ch_lock, lock_flags2);
2871
2872         ch->ch_c_cflag   = tty->termios.c_cflag;
2873         ch->ch_c_iflag   = tty->termios.c_iflag;
2874         ch->ch_c_oflag   = tty->termios.c_oflag;
2875         ch->ch_c_lflag   = tty->termios.c_lflag;
2876         ch->ch_startc    = tty->termios.c_cc[VSTART];
2877         ch->ch_stopc     = tty->termios.c_cc[VSTOP];
2878
2879         dgap_carrier(ch);
2880         dgap_param(tty);
2881
2882         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2883         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2884 }
2885
2886
2887 static void dgap_tty_throttle(struct tty_struct *tty)
2888 {
2889         struct board_t *bd;
2890         struct channel_t *ch;
2891         struct un_t *un;
2892         ulong   lock_flags;
2893         ulong   lock_flags2;
2894
2895         if (!tty || tty->magic != TTY_MAGIC)
2896                 return;
2897
2898         un = tty->driver_data;
2899         if (!un || un->magic != DGAP_UNIT_MAGIC)
2900                 return;
2901
2902         ch = un->un_ch;
2903         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2904                 return;
2905
2906         bd = ch->ch_bd;
2907         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2908                 return;
2909
2910         DPR_IOCTL(("dgap_tty_throttle start\n"));
2911
2912         DGAP_LOCK(bd->bd_lock, lock_flags);
2913         DGAP_LOCK(ch->ch_lock, lock_flags2);
2914
2915         ch->ch_flags |= (CH_RXBLOCK);
2916 #if 1
2917         dgap_cmdw(ch, RPAUSE, 0, 0);
2918 #endif
2919
2920         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2921         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2922
2923         DPR_IOCTL(("dgap_tty_throttle finish\n"));
2924 }
2925
2926
2927 static void dgap_tty_unthrottle(struct tty_struct *tty)
2928 {
2929         struct board_t *bd;
2930         struct channel_t *ch;
2931         struct un_t *un;
2932         ulong   lock_flags;
2933         ulong   lock_flags2;
2934
2935         if (!tty || tty->magic != TTY_MAGIC)
2936                 return;
2937
2938         un = tty->driver_data;
2939         if (!un || un->magic != DGAP_UNIT_MAGIC)
2940                 return;
2941
2942         ch = un->un_ch;
2943         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2944                 return;
2945
2946         bd = ch->ch_bd;
2947         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2948                 return;
2949
2950         DPR_IOCTL(("dgap_tty_unthrottle start\n"));
2951
2952         DGAP_LOCK(bd->bd_lock, lock_flags);
2953         DGAP_LOCK(ch->ch_lock, lock_flags2);
2954
2955         ch->ch_flags &= ~(CH_RXBLOCK);
2956
2957 #if 1
2958         dgap_cmdw(ch, RRESUME, 0, 0);
2959 #endif
2960
2961         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2962         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2963
2964         DPR_IOCTL(("dgap_tty_unthrottle finish\n"));
2965 }
2966
2967
2968 static void dgap_tty_start(struct tty_struct *tty)
2969 {
2970         struct board_t *bd;
2971         struct channel_t *ch;
2972         struct un_t *un;
2973         ulong   lock_flags;
2974         ulong   lock_flags2;
2975
2976         if (!tty || tty->magic != TTY_MAGIC)
2977                 return;
2978
2979         un = tty->driver_data;
2980         if (!un || un->magic != DGAP_UNIT_MAGIC)
2981                 return;
2982
2983         ch = un->un_ch;
2984         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2985                 return;
2986
2987         bd = ch->ch_bd;
2988         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2989                 return;
2990
2991         DPR_IOCTL(("dgap_tty_start start\n"));
2992
2993         DGAP_LOCK(bd->bd_lock, lock_flags);
2994         DGAP_LOCK(ch->ch_lock, lock_flags2);
2995
2996         dgap_cmdw(ch, RESUMETX, 0, 0);
2997
2998         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2999         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3000
3001         DPR_IOCTL(("dgap_tty_start finish\n"));
3002 }
3003
3004
3005 static void dgap_tty_stop(struct tty_struct *tty)
3006 {
3007         struct board_t *bd;
3008         struct channel_t *ch;
3009         struct un_t *un;
3010         ulong   lock_flags;
3011         ulong   lock_flags2;
3012
3013         if (!tty || tty->magic != TTY_MAGIC)
3014                 return;
3015
3016         un = tty->driver_data;
3017         if (!un || un->magic != DGAP_UNIT_MAGIC)
3018                 return;
3019
3020         ch = un->un_ch;
3021         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3022                 return;
3023
3024         bd = ch->ch_bd;
3025         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3026                 return;
3027
3028         DPR_IOCTL(("dgap_tty_stop start\n"));
3029
3030         DGAP_LOCK(bd->bd_lock, lock_flags);
3031         DGAP_LOCK(ch->ch_lock, lock_flags2);
3032
3033         dgap_cmdw(ch, PAUSETX, 0, 0);
3034
3035         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3036         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3037
3038         DPR_IOCTL(("dgap_tty_stop finish\n"));
3039 }
3040
3041
3042 /*
3043  * dgap_tty_flush_chars()
3044  *
3045  * Flush the cook buffer
3046  *
3047  * Note to self, and any other poor souls who venture here:
3048  *
3049  * flush in this case DOES NOT mean dispose of the data.
3050  * instead, it means "stop buffering and send it if you
3051  * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
3052  *
3053  * It is also always called in interrupt context - JAR 8-Sept-99
3054  */
3055 static void dgap_tty_flush_chars(struct tty_struct *tty)
3056 {
3057         struct board_t *bd;
3058         struct channel_t *ch;
3059         struct un_t *un;
3060         ulong   lock_flags;
3061         ulong   lock_flags2;
3062
3063         if (!tty || tty->magic != TTY_MAGIC)
3064                 return;
3065
3066         un = tty->driver_data;
3067         if (!un || un->magic != DGAP_UNIT_MAGIC)
3068                 return;
3069
3070         ch = un->un_ch;
3071         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3072                 return;
3073
3074         bd = ch->ch_bd;
3075         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3076                 return;
3077
3078         DPR_IOCTL(("dgap_tty_flush_chars start\n"));
3079
3080         DGAP_LOCK(bd->bd_lock, lock_flags);
3081         DGAP_LOCK(ch->ch_lock, lock_flags2);
3082
3083         /* TODO: Do something here */
3084
3085         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3086         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3087
3088         DPR_IOCTL(("dgap_tty_flush_chars finish\n"));
3089 }
3090
3091
3092
3093 /*
3094  * dgap_tty_flush_buffer()
3095  *
3096  * Flush Tx buffer (make in == out)
3097  */
3098 static void dgap_tty_flush_buffer(struct tty_struct *tty)
3099 {
3100         struct board_t *bd;
3101         struct channel_t *ch;
3102         struct un_t *un;
3103         ulong   lock_flags;
3104         ulong   lock_flags2;
3105         u16     head = 0;
3106
3107         if (!tty || tty->magic != TTY_MAGIC)
3108                 return;
3109
3110         un = tty->driver_data;
3111         if (!un || un->magic != DGAP_UNIT_MAGIC)
3112                 return;
3113
3114         ch = un->un_ch;
3115         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3116                 return;
3117
3118         bd = ch->ch_bd;
3119         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3120                 return;
3121
3122         DPR_IOCTL(("dgap_tty_flush_buffer on port: %d start\n", ch->ch_portnum));
3123
3124         DGAP_LOCK(bd->bd_lock, lock_flags);
3125         DGAP_LOCK(ch->ch_lock, lock_flags2);
3126
3127         ch->ch_flags &= ~CH_STOP;
3128         head = readw(&(ch->ch_bs->tx_head));
3129         dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
3130         dgap_cmdw(ch, RESUMETX, 0, 0);
3131         if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3132                 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3133                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3134         }
3135         if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3136                 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3137                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3138         }
3139
3140         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3141         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3142         if (waitqueue_active(&tty->write_wait))
3143                 wake_up_interruptible(&tty->write_wait);
3144         tty_wakeup(tty);
3145
3146         DPR_IOCTL(("dgap_tty_flush_buffer finish\n"));
3147 }
3148
3149
3150
3151 /*****************************************************************************
3152  *
3153  * The IOCTL function and all of its helpers
3154  *
3155  *****************************************************************************/
3156
3157 /*
3158  * dgap_tty_ioctl()
3159  *
3160  * The usual assortment of ioctl's
3161  */
3162 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
3163                 unsigned long arg)
3164 {
3165         struct board_t *bd;
3166         struct channel_t *ch;
3167         struct un_t *un;
3168         int rc;
3169         u16     head = 0;
3170         ulong   lock_flags = 0;
3171         ulong   lock_flags2 = 0;
3172         void __user *uarg = (void __user *) arg;
3173
3174         if (!tty || tty->magic != TTY_MAGIC)
3175                 return (-ENODEV);
3176
3177         un = tty->driver_data;
3178         if (!un || un->magic != DGAP_UNIT_MAGIC)
3179                 return (-ENODEV);
3180
3181         ch = un->un_ch;
3182         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3183                 return (-ENODEV);
3184
3185         bd = ch->ch_bd;
3186         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3187                 return (-ENODEV);
3188
3189         DPR_IOCTL(("dgap_tty_ioctl start on port %d - cmd %s (%x), arg %lx\n",
3190                 ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
3191
3192         DGAP_LOCK(bd->bd_lock, lock_flags);
3193         DGAP_LOCK(ch->ch_lock, lock_flags2);
3194
3195         if (un->un_open_count <= 0) {
3196                 DPR_BASIC(("dgap_tty_ioctl - unit not open.\n"));
3197                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3198                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3199                 return(-EIO);
3200         }
3201
3202         switch (cmd) {
3203
3204         /* Here are all the standard ioctl's that we MUST implement */
3205
3206         case TCSBRK:
3207                 /*
3208                  * TCSBRK is SVID version: non-zero arg --> no break
3209                  * this behaviour is exploited by tcdrain().
3210                  *
3211                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3212                  * between 0.25 and 0.5 seconds so we'll ask for something
3213                  * in the middle: 0.375 seconds.
3214                  */
3215                 rc = tty_check_change(tty);
3216                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3217                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3218                 if (rc) {
3219                         return(rc);
3220                 }
3221
3222                 rc = dgap_wait_for_drain(tty);
3223
3224                 if (rc) {
3225                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
3226                         return(-EINTR);
3227                 }
3228
3229                 DGAP_LOCK(bd->bd_lock, lock_flags);
3230                 DGAP_LOCK(ch->ch_lock, lock_flags2);
3231
3232                 if(((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
3233                         dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3234                 }
3235
3236                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3237                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3238
3239                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3240                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
3241
3242                 return(0);
3243
3244
3245         case TCSBRKP:
3246                 /* support for POSIX tcsendbreak()
3247
3248                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3249                  * between 0.25 and 0.5 seconds so we'll ask for something
3250                  * in the middle: 0.375 seconds.
3251                  */
3252                 rc = tty_check_change(tty);
3253                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3254                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3255                 if (rc) {
3256                         return(rc);
3257                 }
3258
3259                 rc = dgap_wait_for_drain(tty);
3260                 if (rc) {
3261                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
3262                         return(-EINTR);
3263                 }
3264
3265                 DGAP_LOCK(bd->bd_lock, lock_flags);
3266                 DGAP_LOCK(ch->ch_lock, lock_flags2);
3267
3268                 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3269
3270                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3271                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3272
3273                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3274                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
3275
3276                 return(0);
3277
3278         case TIOCSBRK:
3279                 /*
3280                  * FEP5 doesn't support turning on a break unconditionally.
3281                  * The FEP5 device will stop sending a break automatically
3282                  * after the specified time value that was sent when turning on
3283                  * the break.
3284                  */
3285                 rc = tty_check_change(tty);
3286                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3287                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3288                 if (rc) {
3289                         return(rc);
3290                 }
3291
3292                 rc = dgap_wait_for_drain(tty);
3293                 if (rc) {
3294                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
3295                         return(-EINTR);
3296                 }
3297
3298                 DGAP_LOCK(bd->bd_lock, lock_flags);
3299                 DGAP_LOCK(ch->ch_lock, lock_flags2);
3300
3301                 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3302
3303                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3304                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3305
3306                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3307                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
3308
3309                 return 0;
3310
3311         case TIOCCBRK:
3312                 /*
3313                  * FEP5 doesn't support turning off a break unconditionally.
3314                  * The FEP5 device will stop sending a break automatically
3315                  * after the specified time value that was sent when turning on
3316                  * the break.
3317                  */
3318                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3319                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3320                 return 0;
3321
3322         case TIOCGSOFTCAR:
3323
3324                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3325                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3326
3327                 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
3328                 return(rc);
3329
3330         case TIOCSSOFTCAR:
3331                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3332                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3333
3334                 rc = get_user(arg, (unsigned long __user *) arg);
3335                 if (rc)
3336                         return(rc);
3337
3338                 DGAP_LOCK(bd->bd_lock, lock_flags);
3339                 DGAP_LOCK(ch->ch_lock, lock_flags2);
3340                 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
3341                 dgap_param(tty);
3342                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3343                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3344
3345                 return(0);
3346
3347         case TIOCMGET:
3348                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3349                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3350                 return(dgap_get_modem_info(ch, uarg));
3351
3352         case TIOCMBIS:
3353         case TIOCMBIC:
3354         case TIOCMSET:
3355                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3356                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3357                 return(dgap_set_modem_info(tty, cmd, uarg));
3358
3359                 /*
3360                  * Here are any additional ioctl's that we want to implement
3361                  */
3362
3363         case TCFLSH:
3364                 /*
3365                  * The linux tty driver doesn't have a flush
3366                  * input routine for the driver, assuming all backed
3367                  * up data is in the line disc. buffers.  However,
3368                  * we all know that's not the case.  Here, we
3369                  * act on the ioctl, but then lie and say we didn't
3370                  * so the line discipline will process the flush
3371                  * also.
3372                  */
3373                 rc = tty_check_change(tty);
3374                 if (rc) {
3375                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3376                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3377                         return(rc);
3378                 }
3379
3380                 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
3381                         if (!(un->un_type == DGAP_PRINT)) {
3382                                 head = readw(&(ch->ch_bs->rx_head));
3383                                 writew(head, &(ch->ch_bs->rx_tail));
3384                                 writeb(0, &(ch->ch_bs->orun));
3385                         }
3386                 }
3387
3388                 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
3389                         ch->ch_flags &= ~CH_STOP;
3390                         head = readw(&(ch->ch_bs->tx_head));
3391                         dgap_cmdw(ch, FLUSHTX, (u16) head, 0 );
3392                         dgap_cmdw(ch, RESUMETX, 0, 0);
3393                         if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3394                                 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3395                                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3396                         }
3397                         if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3398                                 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3399                                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3400                         }
3401                         if (waitqueue_active(&tty->write_wait))
3402                                 wake_up_interruptible(&tty->write_wait);
3403
3404                         /* Can't hold any locks when calling tty_wakeup! */
3405                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3406                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3407                         tty_wakeup(tty);
3408                         DGAP_LOCK(bd->bd_lock, lock_flags);
3409                         DGAP_LOCK(ch->ch_lock, lock_flags2);
3410                 }
3411
3412                 /* pretend we didn't recognize this IOCTL */
3413                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3414                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3415
3416                 DPR_IOCTL(("dgap_tty_ioctl (LINE:%d) finish on port %d - cmd %s (%x), arg %lx\n",
3417                         __LINE__, ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
3418
3419                 return(-ENOIOCTLCMD);
3420
3421         case TCSETSF:
3422         case TCSETSW:
3423                 /*
3424                  * The linux tty driver doesn't have a flush
3425                  * input routine for the driver, assuming all backed
3426                  * up data is in the line disc. buffers.  However,
3427                  * we all know that's not the case.  Here, we
3428                  * act on the ioctl, but then lie and say we didn't
3429                  * so the line discipline will process the flush
3430                  * also.
3431                  */
3432                 if (cmd == TCSETSF) {
3433                         /* flush rx */
3434                         ch->ch_flags &= ~CH_STOP;
3435                         head = readw(&(ch->ch_bs->rx_head));
3436                         writew(head, &(ch->ch_bs->rx_tail));
3437                 }
3438
3439                 /* now wait for all the output to drain */
3440                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3441                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3442                 rc = dgap_wait_for_drain(tty);
3443                 if (rc) {
3444                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
3445                         return(-EINTR);
3446                 }
3447
3448                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3449                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
3450
3451                 /* pretend we didn't recognize this */
3452                 return(-ENOIOCTLCMD);
3453
3454         case TCSETAW:
3455
3456                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3457                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3458                 rc = dgap_wait_for_drain(tty);
3459                 if (rc) {
3460                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
3461                         return(-EINTR);
3462                 }
3463
3464                 /* pretend we didn't recognize this */
3465                 return(-ENOIOCTLCMD);
3466
3467         case TCXONC:
3468                 /*
3469                  * The Linux Line Discipline (LD) would do this for us if we
3470                  * let it, but we have the special firmware options to do this
3471                  * the "right way" regardless of hardware or software flow
3472                  * control so we'll do it outselves instead of letting the LD
3473                  * do it.
3474                  */
3475                 rc = tty_check_change(tty);
3476                 if (rc) {
3477                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3478                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3479                         return(rc);
3480                 }
3481
3482                 DPR_IOCTL(("dgap_ioctl - in TCXONC - %d\n", cmd));
3483                 switch (arg) {
3484
3485                 case TCOON:
3486                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3487                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3488                         dgap_tty_start(tty);
3489                         return(0);
3490                 case TCOOFF:
3491                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3492                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3493                         dgap_tty_stop(tty);
3494                         return(0);
3495                 case TCION:
3496                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3497                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3498                         /* Make the ld do it */
3499                         return(-ENOIOCTLCMD);
3500                 case TCIOFF:
3501                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3502                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3503                         /* Make the ld do it */
3504                         return(-ENOIOCTLCMD);
3505                 default:
3506                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3507                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3508                         return(-EINVAL);
3509                 }
3510
3511         case DIGI_GETA:
3512                 /* get information for ditty */
3513                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3514                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3515                 return(dgap_tty_digigeta(tty, uarg));
3516
3517         case DIGI_SETAW:
3518         case DIGI_SETAF:
3519
3520                 /* set information for ditty */
3521                 if (cmd == (DIGI_SETAW)) {
3522
3523                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3524                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3525                         rc = dgap_wait_for_drain(tty);
3526                         if (rc) {
3527                                 DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
3528                                 return(-EINTR);
3529                         }
3530                         DGAP_LOCK(bd->bd_lock, lock_flags);
3531                         DGAP_LOCK(ch->ch_lock, lock_flags2);
3532                 }
3533                 else {
3534                         tty_ldisc_flush(tty);
3535                 }
3536                 /* fall thru */
3537
3538         case DIGI_SETA:
3539                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3540                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3541                 return(dgap_tty_digiseta(tty, uarg));
3542
3543         case DIGI_GEDELAY:
3544                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3545                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3546                 return(dgap_tty_digigetedelay(tty, uarg));
3547
3548         case DIGI_SEDELAY:
3549                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3550                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3551                 return(dgap_tty_digisetedelay(tty, uarg));
3552
3553         case DIGI_GETCUSTOMBAUD:
3554                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3555                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3556                 return(dgap_tty_digigetcustombaud(tty, uarg));
3557
3558         case DIGI_SETCUSTOMBAUD:
3559                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3560                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3561                 return(dgap_tty_digisetcustombaud(tty, uarg));
3562
3563         case DIGI_RESET_PORT:
3564                 dgap_firmware_reset_port(ch);
3565                 dgap_param(tty);
3566                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3567                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3568                 return 0;
3569
3570         default:
3571                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3572                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3573
3574                 DPR_IOCTL(("dgap_tty_ioctl - in default\n"));
3575                 DPR_IOCTL(("dgap_tty_ioctl end - cmd %s (%x), arg %lx\n",
3576                         dgap_ioctl_name(cmd), cmd, arg));
3577
3578                 return(-ENOIOCTLCMD);
3579         }
3580 }