Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[pandora-kernel.git] / drivers / char / ip2 / ip2main.c
1 /*
2 *
3 *   (c) 1999 by Computone Corporation
4 *
5 ********************************************************************************
6 *
7 *   PACKAGE:     Linux tty Device Driver for IntelliPort family of multiport
8 *                serial I/O controllers.
9 *
10 *   DESCRIPTION: Mainline code for the device driver
11 *
12 *******************************************************************************/
13 // ToDo:
14 //
15 // Fix the immediate DSS_NOW problem.
16 // Work over the channel stats return logic in ip2_ipl_ioctl so they
17 //      make sense for all 256 possible channels and so the user space
18 //      utilities will compile and work properly.
19 //
20 // Done:
21 //
22 // 1.2.14       /\/\|=mhw=|\/\/
23 // Added bounds checking to ip2_ipl_ioctl to avoid potential terroristic acts.
24 // Changed the definition of ip2trace to be more consistent with kernel style
25 //      Thanks to Andreas Dilger <adilger@turbolabs.com> for these updates
26 //
27 // 1.2.13       /\/\|=mhw=|\/\/
28 // DEVFS: Renamed ttf/{n} to tts/F{n} and cuf/{n} to cua/F{n} to conform
29 //      to agreed devfs serial device naming convention.
30 //
31 // 1.2.12       /\/\|=mhw=|\/\/
32 // Cleaned up some remove queue cut and paste errors
33 //
34 // 1.2.11       /\/\|=mhw=|\/\/
35 // Clean up potential NULL pointer dereferences
36 // Clean up devfs registration
37 // Add kernel command line parsing for io and irq
38 //      Compile defaults for io and irq are now set in ip2.c not ip2.h!
39 // Reworked poll_only hack for explicit parameter setting
40 //      You must now EXPLICITLY set poll_only = 1 or set all irqs to 0
41 // Merged ip2_loadmain and old_ip2_init
42 // Converted all instances of interruptible_sleep_on into queue calls
43 //      Most of these had no race conditions but better to clean up now
44 //
45 // 1.2.10       /\/\|=mhw=|\/\/
46 // Fixed the bottom half interrupt handler and enabled USE_IQI
47 //      to split the interrupt handler into a formal top-half / bottom-half
48 // Fixed timing window on high speed processors that queued messages to
49 //      the outbound mail fifo faster than the board could handle.
50 //
51 // 1.2.9
52 // Four box EX was barfing on >128k kmalloc, made structure smaller by
53 // reducing output buffer size
54 //
55 // 1.2.8
56 // Device file system support (MHW)
57 //
58 // 1.2.7 
59 // Fixed
60 // Reload of ip2 without unloading ip2main hangs system on cat of /proc/modules
61 //
62 // 1.2.6
63 //Fixes DCD problems
64 //      DCD was not reported when CLOCAL was set on call to TIOCMGET
65 //
66 //Enhancements:
67 //      TIOCMGET requests and waits for status return
68 //      No DSS interrupts enabled except for DCD when needed
69 //
70 // For internal use only
71 //
72 //#define IP2DEBUG_INIT
73 //#define IP2DEBUG_OPEN
74 //#define IP2DEBUG_WRITE
75 //#define IP2DEBUG_READ
76 //#define IP2DEBUG_IOCTL
77 //#define IP2DEBUG_IPL
78
79 //#define IP2DEBUG_TRACE
80 //#define DEBUG_FIFO
81
82 /************/
83 /* Includes */
84 /************/
85
86 #include <linux/ctype.h>
87 #include <linux/string.h>
88 #include <linux/fcntl.h>
89 #include <linux/errno.h>
90 #include <linux/module.h>
91 #include <linux/signal.h>
92 #include <linux/sched.h>
93 #include <linux/timer.h>
94 #include <linux/interrupt.h>
95 #include <linux/pci.h>
96 #include <linux/mm.h>
97 #include <linux/slab.h>
98 #include <linux/major.h>
99 #include <linux/wait.h>
100 #include <linux/device.h>
101
102 #include <linux/tty.h>
103 #include <linux/tty_flip.h>
104 #include <linux/termios.h>
105 #include <linux/tty_driver.h>
106 #include <linux/serial.h>
107 #include <linux/ptrace.h>
108 #include <linux/ioport.h>
109
110 #include <linux/cdk.h>
111 #include <linux/comstats.h>
112 #include <linux/delay.h>
113 #include <linux/bitops.h>
114
115 #include <asm/system.h>
116 #include <asm/io.h>
117 #include <asm/irq.h>
118
119 #include <linux/vmalloc.h>
120 #include <linux/init.h>
121
122 #include <asm/uaccess.h>
123
124 #include "ip2types.h"
125 #include "ip2trace.h"
126 #include "ip2ioctl.h"
127 #include "ip2.h"
128 #include "i2ellis.h"
129 #include "i2lib.h"
130
131 /*****************
132  * /proc/ip2mem  *
133  *****************/
134
135 #include <linux/proc_fs.h>
136 #include <linux/seq_file.h>
137
138 static const struct file_operations ip2mem_proc_fops;
139 static int ip2_read_proc(char *, char **, off_t, int, int *, void * );
140
141 /********************/
142 /* Type Definitions */
143 /********************/
144
145 /*************/
146 /* Constants */
147 /*************/
148
149 /* String constants to identify ourselves */
150 static char *pcName    = "Computone IntelliPort Plus multiport driver";
151 static char *pcVersion = "1.2.14";
152
153 /* String constants for port names */
154 static char *pcDriver_name   = "ip2";
155 static char *pcIpl               = "ip2ipl";
156
157 // cheezy kludge or genius - you decide?
158 int ip2_loadmain(int *, int *, unsigned char *, int);
159 static unsigned char *Fip_firmware;
160 static int Fip_firmware_size;
161
162 /***********************/
163 /* Function Prototypes */
164 /***********************/
165
166 /* Global module entry functions */
167
168 /* Private (static) functions */
169 static int  ip2_open(PTTY, struct file *);
170 static void ip2_close(PTTY, struct file *);
171 static int  ip2_write(PTTY, const unsigned char *, int);
172 static int  ip2_putchar(PTTY, unsigned char);
173 static void ip2_flush_chars(PTTY);
174 static int  ip2_write_room(PTTY);
175 static int  ip2_chars_in_buf(PTTY);
176 static void ip2_flush_buffer(PTTY);
177 static int  ip2_ioctl(PTTY, struct file *, UINT, ULONG);
178 static void ip2_set_termios(PTTY, struct ktermios *);
179 static void ip2_set_line_discipline(PTTY);
180 static void ip2_throttle(PTTY);
181 static void ip2_unthrottle(PTTY);
182 static void ip2_stop(PTTY);
183 static void ip2_start(PTTY);
184 static void ip2_hangup(PTTY);
185 static int  ip2_tiocmget(struct tty_struct *tty, struct file *file);
186 static int  ip2_tiocmset(struct tty_struct *tty, struct file *file,
187                          unsigned int set, unsigned int clear);
188
189 static void set_irq(int, int);
190 static void ip2_interrupt_bh(struct work_struct *work);
191 static irqreturn_t ip2_interrupt(int irq, void *dev_id);
192 static void ip2_poll(unsigned long arg);
193 static inline void service_all_boards(void);
194 static void do_input(struct work_struct *);
195 static void do_status(struct work_struct *);
196
197 static void ip2_wait_until_sent(PTTY,int);
198
199 static void set_params (i2ChanStrPtr, struct ktermios *);
200 static int get_serial_info(i2ChanStrPtr, struct serial_struct __user *);
201 static int set_serial_info(i2ChanStrPtr, struct serial_struct __user *);
202
203 static ssize_t ip2_ipl_read(struct file *, char __user *, size_t, loff_t *);
204 static ssize_t ip2_ipl_write(struct file *, const char __user *, size_t, loff_t *);
205 static int ip2_ipl_ioctl(struct inode *, struct file *, UINT, ULONG);
206 static int ip2_ipl_open(struct inode *, struct file *);
207
208 static int DumpTraceBuffer(char __user *, int);
209 static int DumpFifoBuffer( char __user *, int);
210
211 static void ip2_init_board(int);
212 static unsigned short find_eisa_board(int);
213
214 /***************/
215 /* Static Data */
216 /***************/
217
218 static struct tty_driver *ip2_tty_driver;
219
220 /* Here, then is a table of board pointers which the interrupt routine should
221  * scan through to determine who it must service.
222  */
223 static unsigned short i2nBoards; // Number of boards here
224
225 static i2eBordStrPtr i2BoardPtrTable[IP2_MAX_BOARDS];
226
227 static i2ChanStrPtr  DevTable[IP2_MAX_PORTS];
228 //DevTableMem just used to save addresses for kfree
229 static void  *DevTableMem[IP2_MAX_BOARDS];
230
231 /* This is the driver descriptor for the ip2ipl device, which is used to
232  * download the loadware to the boards.
233  */
234 static const struct file_operations ip2_ipl = {
235         .owner          = THIS_MODULE,
236         .read           = ip2_ipl_read,
237         .write          = ip2_ipl_write,
238         .ioctl          = ip2_ipl_ioctl,
239         .open           = ip2_ipl_open,
240 }; 
241
242 static unsigned long irq_counter = 0;
243 static unsigned long bh_counter = 0;
244
245 // Use immediate queue to service interrupts
246 #define USE_IQI
247 //#define USE_IQ        // PCI&2.2 needs work
248
249 /* The timer_list entry for our poll routine. If interrupt operation is not
250  * selected, the board is serviced periodically to see if anything needs doing.
251  */
252 #define  POLL_TIMEOUT   (jiffies + 1)
253 static DEFINE_TIMER(PollTimer, ip2_poll, 0, 0);
254 static char  TimerOn;
255
256 #ifdef IP2DEBUG_TRACE
257 /* Trace (debug) buffer data */
258 #define TRACEMAX  1000
259 static unsigned long tracebuf[TRACEMAX];
260 static int tracestuff;
261 static int tracestrip;
262 static int tracewrap;
263 #endif
264
265 /**********/
266 /* Macros */
267 /**********/
268
269 #if defined(MODULE) && defined(IP2DEBUG_OPEN)
270 #define DBG_CNT(s) printk(KERN_DEBUG "(%s): [%x] refc=%d, ttyc=%d, modc=%x -> %s\n", \
271                     tty->name,(pCh->flags),ip2_tty_driver->refcount, \
272                     tty->count,/*GET_USE_COUNT(module)*/0,s)
273 #else
274 #define DBG_CNT(s)
275 #endif
276
277 /********/
278 /* Code */
279 /********/
280
281 #include "i2ellis.c"    /* Extremely low-level interface services */
282 #include "i2cmd.c"      /* Standard loadware command definitions */
283 #include "i2lib.c"      /* High level interface services */
284
285 /* Configuration area for modprobe */
286
287 MODULE_AUTHOR("Doug McNash");
288 MODULE_DESCRIPTION("Computone IntelliPort Plus Driver");
289
290 static int poll_only = 0;
291
292 static int Eisa_irq;
293 static int Eisa_slot;
294
295 static int iindx;
296 static char rirqs[IP2_MAX_BOARDS];
297 static int Valid_Irqs[] = { 3, 4, 5, 7, 10, 11, 12, 15, 0};
298
299 /* for sysfs class support */
300 static struct class *ip2_class;
301
302 // Some functions to keep track of what irq's we have
303
304 static int
305 is_valid_irq(int irq)
306 {
307         int *i = Valid_Irqs;
308         
309         while ((*i != 0) && (*i != irq)) {
310                 i++;
311         }
312         return (*i);
313 }
314
315 static void
316 mark_requested_irq( char irq )
317 {
318         rirqs[iindx++] = irq;
319 }
320
321 #ifdef MODULE
322 static int
323 clear_requested_irq( char irq )
324 {
325         int i;
326         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
327                 if (rirqs[i] == irq) {
328                         rirqs[i] = 0;
329                         return 1;
330                 }
331         }
332         return 0;
333 }
334 #endif
335
336 static int
337 have_requested_irq( char irq )
338 {
339         // array init to zeros so 0 irq will not be requested as a side effect
340         int i;
341         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
342                 if (rirqs[i] == irq)
343                         return 1;
344         }
345         return 0;
346 }
347
348 /******************************************************************************/
349 /* Function:   init_module()                                                  */
350 /* Parameters: None                                                           */
351 /* Returns:    Success (0)                                                    */
352 /*                                                                            */
353 /* Description:                                                               */
354 /* This is a required entry point for an installable module. It simply calls  */
355 /* the driver initialisation function and returns what it returns.            */
356 /******************************************************************************/
357 #ifdef MODULE
358 static int __init
359 ip2_init_module(void)
360 {
361 #ifdef IP2DEBUG_INIT
362         printk (KERN_DEBUG "Loading module ...\n" );
363 #endif
364     return 0;
365 }
366 module_init(ip2_init_module);
367 #endif /* MODULE */
368
369 /******************************************************************************/
370 /* Function:   cleanup_module()                                               */
371 /* Parameters: None                                                           */
372 /* Returns:    Nothing                                                        */
373 /*                                                                            */
374 /* Description:                                                               */
375 /* This is a required entry point for an installable module. It has to return */
376 /* the device and the driver to a passive state. It should not be necessary   */
377 /* to reset the board fully, especially as the loadware is downloaded         */
378 /* externally rather than in the driver. We just want to disable the board    */
379 /* and clear the loadware to a reset state. To allow this there has to be a   */
380 /* way to detect whether the board has the loadware running at init time to   */
381 /* handle subsequent installations of the driver. All memory allocated by the */
382 /* driver should be returned since it may be unloaded from memory.            */
383 /******************************************************************************/
384 #ifdef MODULE
385 void __exit
386 ip2_cleanup_module(void)
387 {
388         int err;
389         int i;
390
391 #ifdef IP2DEBUG_INIT
392         printk (KERN_DEBUG "Unloading %s: version %s\n", pcName, pcVersion );
393 #endif
394         /* Stop poll timer if we had one. */
395         if ( TimerOn ) {
396                 del_timer ( &PollTimer );
397                 TimerOn = 0;
398         }
399
400         /* Reset the boards we have. */
401         for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
402                 if ( i2BoardPtrTable[i] ) {
403                         iiReset( i2BoardPtrTable[i] );
404                 }
405         }
406
407         /* The following is done at most once, if any boards were installed. */
408         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
409                 if ( i2BoardPtrTable[i] ) {
410                         iiResetDelay( i2BoardPtrTable[i] );
411                         /* free io addresses and Tibet */
412                         release_region( ip2config.addr[i], 8 );
413                         device_destroy(ip2_class, MKDEV(IP2_IPL_MAJOR, 4 * i));
414                         device_destroy(ip2_class, MKDEV(IP2_IPL_MAJOR, 4 * i + 1));
415                 }
416                 /* Disable and remove interrupt handler. */
417                 if ( (ip2config.irq[i] > 0) && have_requested_irq(ip2config.irq[i]) ) { 
418                         free_irq ( ip2config.irq[i], (void *)&pcName);
419                         clear_requested_irq( ip2config.irq[i]);
420                 }
421         }
422         class_destroy(ip2_class);
423         if ( ( err = tty_unregister_driver ( ip2_tty_driver ) ) ) {
424                 printk(KERN_ERR "IP2: failed to unregister tty driver (%d)\n", err);
425         }
426         put_tty_driver(ip2_tty_driver);
427         unregister_chrdev(IP2_IPL_MAJOR, pcIpl);
428         remove_proc_entry("ip2mem", NULL);
429
430         // free memory
431         for (i = 0; i < IP2_MAX_BOARDS; i++) {
432                 void *pB;
433 #ifdef CONFIG_PCI
434                 if (ip2config.type[i] == PCI && ip2config.pci_dev[i]) {
435                         pci_disable_device(ip2config.pci_dev[i]);
436                         pci_dev_put(ip2config.pci_dev[i]);
437                         ip2config.pci_dev[i] = NULL;
438                 }
439 #endif
440                 if ((pB = i2BoardPtrTable[i]) != 0 ) {
441                         kfree ( pB );
442                         i2BoardPtrTable[i] = NULL;
443                 }
444                 if ((DevTableMem[i]) != NULL ) {
445                         kfree ( DevTableMem[i]  );
446                         DevTableMem[i] = NULL;
447                 }
448         }
449
450         /* Cleanup the iiEllis subsystem. */
451         iiEllisCleanup();
452 #ifdef IP2DEBUG_INIT
453         printk (KERN_DEBUG "IP2 Unloaded\n" );
454 #endif
455 }
456 module_exit(ip2_cleanup_module);
457 #endif /* MODULE */
458
459 static const struct tty_operations ip2_ops = {
460         .open            = ip2_open,
461         .close           = ip2_close,
462         .write           = ip2_write,
463         .put_char        = ip2_putchar,
464         .flush_chars     = ip2_flush_chars,
465         .write_room      = ip2_write_room,
466         .chars_in_buffer = ip2_chars_in_buf,
467         .flush_buffer    = ip2_flush_buffer,
468         .ioctl           = ip2_ioctl,
469         .throttle        = ip2_throttle,
470         .unthrottle      = ip2_unthrottle,
471         .set_termios     = ip2_set_termios,
472         .set_ldisc       = ip2_set_line_discipline,
473         .stop            = ip2_stop,
474         .start           = ip2_start,
475         .hangup          = ip2_hangup,
476         .read_proc       = ip2_read_proc,
477         .tiocmget        = ip2_tiocmget,
478         .tiocmset        = ip2_tiocmset,
479 };
480
481 /******************************************************************************/
482 /* Function:   ip2_loadmain()                                                 */
483 /* Parameters: irq, io from command line of insmod et. al.                    */
484 /*              pointer to fip firmware and firmware size for boards          */
485 /* Returns:    Success (0)                                                    */
486 /*                                                                            */
487 /* Description:                                                               */
488 /* This was the required entry point for all drivers (now in ip2.c)           */
489 /* It performs all                                                            */
490 /* initialisation of the devices and driver structures, and registers itself  */
491 /* with the relevant kernel modules.                                          */
492 /******************************************************************************/
493 /* IRQF_DISABLED - if set blocks all interrupts else only this line */
494 /* IRQF_SHARED    - for shared irq PCI or maybe EISA only */
495 /* SA_RANDOM   - can be source for cert. random number generators */
496 #define IP2_SA_FLAGS    0
497
498 int
499 ip2_loadmain(int *iop, int *irqp, unsigned char *firmware, int firmsize) 
500 {
501         int i, j, box;
502         int err = 0;
503         static int loaded;
504         i2eBordStrPtr pB = NULL;
505         int rc = -1;
506         static struct pci_dev *pci_dev_i = NULL;
507
508         ip2trace (ITRC_NO_PORT, ITRC_INIT, ITRC_ENTER, 0 );
509
510         /* process command line arguments to modprobe or
511                 insmod i.e. iop & irqp */
512         /* irqp and iop should ALWAYS be specified now...  But we check
513                 them individually just to be sure, anyways... */
514         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
515                 if (iop) {
516                         ip2config.addr[i] = iop[i];
517                         if (irqp) {
518                                 if( irqp[i] >= 0 ) {
519                                         ip2config.irq[i] = irqp[i];
520                                 } else {
521                                         ip2config.irq[i] = 0;
522                                 }
523         // This is a little bit of a hack.  If poll_only=1 on command
524         // line back in ip2.c OR all IRQs on all specified boards are
525         // explicitly set to 0, then drop to poll only mode and override
526         // PCI or EISA interrupts.  This superceeds the old hack of
527         // triggering if all interrupts were zero (like da default).
528         // Still a hack but less prone to random acts of terrorism.
529         //
530         // What we really should do, now that the IRQ default is set
531         // to -1, is to use 0 as a hard coded, do not probe.
532         //
533         //      /\/\|=mhw=|\/\/
534                                 poll_only |= irqp[i];
535                         }
536                 }
537         }
538         poll_only = !poll_only;
539
540         Fip_firmware = firmware;
541         Fip_firmware_size = firmsize;
542
543         /* Announce our presence */
544         printk( KERN_INFO "%s version %s\n", pcName, pcVersion );
545
546         // ip2 can be unloaded and reloaded for no good reason
547         // we can't let that happen here or bad things happen
548         // second load hoses board but not system - fixme later
549         if (loaded) {
550                 printk( KERN_INFO "Still loaded\n" );
551                 return 0;
552         }
553         loaded++;
554
555         ip2_tty_driver = alloc_tty_driver(IP2_MAX_PORTS);
556         if (!ip2_tty_driver)
557                 return -ENOMEM;
558
559         /* Initialise the iiEllis subsystem. */
560         iiEllisInit();
561
562         /* Initialize arrays. */
563         memset( i2BoardPtrTable, 0, sizeof i2BoardPtrTable );
564         memset( DevTable, 0, sizeof DevTable );
565
566         /* Initialise all the boards we can find (up to the maximum). */
567         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
568                 switch ( ip2config.addr[i] ) { 
569                 case 0: /* skip this slot even if card is present */
570                         break;
571                 default: /* ISA */
572                    /* ISA address must be specified */
573                         if ( (ip2config.addr[i] < 0x100) || (ip2config.addr[i] > 0x3f8) ) {
574                                 printk ( KERN_ERR "IP2: Bad ISA board %d address %x\n",
575                                                          i, ip2config.addr[i] );
576                                 ip2config.addr[i] = 0;
577                         } else {
578                                 ip2config.type[i] = ISA;
579
580                                 /* Check for valid irq argument, set for polling if invalid */
581                                 if (ip2config.irq[i] && !is_valid_irq(ip2config.irq[i])) {
582                                         printk(KERN_ERR "IP2: Bad IRQ(%d) specified\n",ip2config.irq[i]);
583                                         ip2config.irq[i] = 0;// 0 is polling and is valid in that sense
584                                 }
585                         }
586                         break;
587                 case PCI:
588 #ifdef CONFIG_PCI
589                         {
590                                 int status;
591
592                                 pci_dev_i = pci_get_device(PCI_VENDOR_ID_COMPUTONE,
593                                                           PCI_DEVICE_ID_COMPUTONE_IP2EX, pci_dev_i);
594                                 if (pci_dev_i != NULL) {
595                                         unsigned int addr;
596
597                                         if (pci_enable_device(pci_dev_i)) {
598                                                 printk( KERN_ERR "IP2: can't enable PCI device at %s\n",
599                                                         pci_name(pci_dev_i));
600                                                 break;
601                                         }
602                                         ip2config.type[i] = PCI;
603                                         ip2config.pci_dev[i] = pci_dev_get(pci_dev_i);
604                                         status =
605                                         pci_read_config_dword(pci_dev_i, PCI_BASE_ADDRESS_1, &addr);
606                                         if ( addr & 1 ) {
607                                                 ip2config.addr[i]=(USHORT)(addr&0xfffe);
608                                         } else {
609                                                 printk( KERN_ERR "IP2: PCI I/O address error\n");
610                                         }
611
612 //              If the PCI BIOS assigned it, lets try and use it.  If we
613 //              can't acquire it or it screws up, deal with it then.
614
615 //                                      if (!is_valid_irq(pci_irq)) {
616 //                                              printk( KERN_ERR "IP2: Bad PCI BIOS IRQ(%d)\n",pci_irq);
617 //                                              pci_irq = 0;
618 //                                      }
619                                         ip2config.irq[i] = pci_dev_i->irq;
620                                 } else {        // ann error
621                                         ip2config.addr[i] = 0;
622                                         printk(KERN_ERR "IP2: PCI board %d not found\n", i);
623                                 } 
624                         }
625 #else
626                         printk( KERN_ERR "IP2: PCI card specified but PCI support not\n");
627                         printk( KERN_ERR "IP2: configured in this kernel.\n");
628                         printk( KERN_ERR "IP2: Recompile kernel with CONFIG_PCI defined!\n");
629 #endif /* CONFIG_PCI */
630                         break;
631                 case EISA:
632                         if ( (ip2config.addr[i] = find_eisa_board( Eisa_slot + 1 )) != 0) {
633                                 /* Eisa_irq set as side effect, boo */
634                                 ip2config.type[i] = EISA;
635                         } 
636                         ip2config.irq[i] = Eisa_irq;
637                         break;
638                 }       /* switch */
639         }       /* for */
640         if (pci_dev_i)
641                 pci_dev_put(pci_dev_i);
642
643         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
644                 if ( ip2config.addr[i] ) {
645                         pB = kzalloc(sizeof(i2eBordStr), GFP_KERNEL);
646                         if (pB) {
647                                 i2BoardPtrTable[i] = pB;
648                                 iiSetAddress( pB, ip2config.addr[i], ii2DelayTimer );
649                                 iiReset( pB );
650                         } else {
651                                 printk(KERN_ERR "IP2: board memory allocation error\n");
652                         }
653                 }
654         }
655         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
656                 if ( ( pB = i2BoardPtrTable[i] ) != NULL ) {
657                         iiResetDelay( pB );
658                         break;
659                 }
660         }
661         for ( i = 0; i < IP2_MAX_BOARDS; ++i ) {
662                 if ( i2BoardPtrTable[i] != NULL ) {
663                         ip2_init_board( i );
664                 }
665         }
666
667         ip2trace (ITRC_NO_PORT, ITRC_INIT, 2, 0 );
668
669         ip2_tty_driver->owner               = THIS_MODULE;
670         ip2_tty_driver->name                 = "ttyF";
671         ip2_tty_driver->driver_name          = pcDriver_name;
672         ip2_tty_driver->major                = IP2_TTY_MAJOR;
673         ip2_tty_driver->minor_start          = 0;
674         ip2_tty_driver->type                 = TTY_DRIVER_TYPE_SERIAL;
675         ip2_tty_driver->subtype              = SERIAL_TYPE_NORMAL;
676         ip2_tty_driver->init_termios         = tty_std_termios;
677         ip2_tty_driver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
678         ip2_tty_driver->flags                = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
679         tty_set_operations(ip2_tty_driver, &ip2_ops);
680
681         ip2trace (ITRC_NO_PORT, ITRC_INIT, 3, 0 );
682
683         /* Register the tty devices. */
684         if ( ( err = tty_register_driver ( ip2_tty_driver ) ) ) {
685                 printk(KERN_ERR "IP2: failed to register tty driver (%d)\n", err);
686                 put_tty_driver(ip2_tty_driver);
687                 return -EINVAL;
688         } else
689         /* Register the IPL driver. */
690         if ( ( err = register_chrdev ( IP2_IPL_MAJOR, pcIpl, &ip2_ipl ) ) ) {
691                 printk(KERN_ERR "IP2: failed to register IPL device (%d)\n", err );
692         } else {
693                 /* create the sysfs class */
694                 ip2_class = class_create(THIS_MODULE, "ip2");
695                 if (IS_ERR(ip2_class)) {
696                         err = PTR_ERR(ip2_class);
697                         goto out_chrdev;        
698                 }
699         }
700         /* Register the read_procmem thing */
701         if (!proc_create("ip2mem",0,NULL,&ip2mem_proc_fops)) {
702                 printk(KERN_ERR "IP2: failed to register read_procmem\n");
703         } else {
704
705         ip2trace (ITRC_NO_PORT, ITRC_INIT, 4, 0 );
706                 /* Register the interrupt handler or poll handler, depending upon the
707                  * specified interrupt.
708                  */
709
710                 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
711                         if ( 0 == ip2config.addr[i] ) {
712                                 continue;
713                         }
714
715                         if ( NULL != ( pB = i2BoardPtrTable[i] ) ) {
716                                 device_create(ip2_class, NULL,
717                                                 MKDEV(IP2_IPL_MAJOR, 4 * i),
718                                                 "ipl%d", i);
719                                 device_create(ip2_class, NULL,
720                                                 MKDEV(IP2_IPL_MAJOR, 4 * i + 1),
721                                                 "stat%d", i);
722
723                             for ( box = 0; box < ABS_MAX_BOXES; ++box )
724                             {
725                                 for ( j = 0; j < ABS_BIGGEST_BOX; ++j )
726                                 {
727                                     if ( pB->i2eChannelMap[box] & (1 << j) )
728                                     {
729                                         tty_register_device(ip2_tty_driver,
730                                             j + ABS_BIGGEST_BOX *
731                                                     (box+i*ABS_MAX_BOXES), NULL);
732                                     }
733                                 }
734                             }
735                         }
736
737                         if (poll_only) {
738 //              Poll only forces driver to only use polling and
739 //              to ignore the probed PCI or EISA interrupts.
740                                 ip2config.irq[i] = CIR_POLL;
741                         }
742                         if ( ip2config.irq[i] == CIR_POLL ) {
743 retry:
744                                 if (!TimerOn) {
745                                         PollTimer.expires = POLL_TIMEOUT;
746                                         add_timer ( &PollTimer );
747                                         TimerOn = 1;
748                                         printk( KERN_INFO "IP2: polling\n");
749                                 }
750                         } else {
751                                 if (have_requested_irq(ip2config.irq[i]))
752                                         continue;
753                                 rc = request_irq( ip2config.irq[i], ip2_interrupt,
754                                         IP2_SA_FLAGS | (ip2config.type[i] == PCI ? IRQF_SHARED : 0),
755                                         pcName, i2BoardPtrTable[i]);
756                                 if (rc) {
757                                         printk(KERN_ERR "IP2: an request_irq failed: error %d\n",rc);
758                                         ip2config.irq[i] = CIR_POLL;
759                                         printk( KERN_INFO "IP2: Polling %ld/sec.\n",
760                                                         (POLL_TIMEOUT - jiffies));
761                                         goto retry;
762                                 } 
763                                 mark_requested_irq(ip2config.irq[i]);
764                                 /* Initialise the interrupt handler bottom half (aka slih). */
765                         }
766                 }
767                 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
768                         if ( i2BoardPtrTable[i] ) {
769                                 set_irq( i, ip2config.irq[i] ); /* set and enable board interrupt */
770                         }
771                 }
772         }
773         ip2trace (ITRC_NO_PORT, ITRC_INIT, ITRC_RETURN, 0 );
774         goto out;
775
776 out_chrdev:
777         unregister_chrdev(IP2_IPL_MAJOR, "ip2");
778 out:
779         return err;
780 }
781
782 EXPORT_SYMBOL(ip2_loadmain);
783
784 /******************************************************************************/
785 /* Function:   ip2_init_board()                                               */
786 /* Parameters: Index of board in configuration structure                      */
787 /* Returns:    Success (0)                                                    */
788 /*                                                                            */
789 /* Description:                                                               */
790 /* This function initializes the specified board. The loadware is copied to   */
791 /* the board, the channel structures are initialized, and the board details   */
792 /* are reported on the console.                                               */
793 /******************************************************************************/
794 static void
795 ip2_init_board( int boardnum )
796 {
797         int i;
798         int nports = 0, nboxes = 0;
799         i2ChanStrPtr pCh;
800         i2eBordStrPtr pB = i2BoardPtrTable[boardnum];
801
802         if ( !iiInitialize ( pB ) ) {
803                 printk ( KERN_ERR "IP2: Failed to initialize board at 0x%x, error %d\n",
804                          pB->i2eBase, pB->i2eError );
805                 goto err_initialize;
806         }
807         printk(KERN_INFO "IP2: Board %d: addr=0x%x irq=%d\n", boardnum + 1,
808                ip2config.addr[boardnum], ip2config.irq[boardnum] );
809
810         if (!request_region( ip2config.addr[boardnum], 8, pcName )) {
811                 printk(KERN_ERR "IP2: bad addr=0x%x\n", ip2config.addr[boardnum]);
812                 goto err_initialize;
813         }
814
815         if ( iiDownloadAll ( pB, (loadHdrStrPtr)Fip_firmware, 1, Fip_firmware_size )
816             != II_DOWN_GOOD ) {
817                 printk ( KERN_ERR "IP2: failed to download loadware\n" );
818                 goto err_release_region;
819         } else {
820                 printk ( KERN_INFO "IP2: fv=%d.%d.%d lv=%d.%d.%d\n",
821                          pB->i2ePom.e.porVersion,
822                          pB->i2ePom.e.porRevision,
823                          pB->i2ePom.e.porSubRev, pB->i2eLVersion,
824                          pB->i2eLRevision, pB->i2eLSub );
825         }
826
827         switch ( pB->i2ePom.e.porID & ~POR_ID_RESERVED ) {
828
829         default:
830                 printk( KERN_ERR "IP2: Unknown board type, ID = %x\n",
831                                 pB->i2ePom.e.porID );
832                 nports = 0;
833                 goto err_release_region;
834                 break;
835
836         case POR_ID_II_4: /* IntelliPort-II, ISA-4 (4xRJ45) */
837                 printk ( KERN_INFO "IP2: ISA-4\n" );
838                 nports = 4;
839                 break;
840
841         case POR_ID_II_8: /* IntelliPort-II, 8-port using standard brick. */
842                 printk ( KERN_INFO "IP2: ISA-8 std\n" );
843                 nports = 8;
844                 break;
845
846         case POR_ID_II_8R: /* IntelliPort-II, 8-port using RJ11's (no CTS) */
847                 printk ( KERN_INFO "IP2: ISA-8 RJ11\n" );
848                 nports = 8;
849                 break;
850
851         case POR_ID_FIIEX: /* IntelliPort IIEX */
852         {
853                 int portnum = IP2_PORTS_PER_BOARD * boardnum;
854                 int            box;
855
856                 for( box = 0; box < ABS_MAX_BOXES; ++box ) {
857                         if ( pB->i2eChannelMap[box] != 0 ) {
858                                 ++nboxes;
859                         }
860                         for( i = 0; i < ABS_BIGGEST_BOX; ++i ) {
861                                 if ( pB->i2eChannelMap[box] & 1<< i ) {
862                                         ++nports;
863                                 }
864                         }
865                 }
866                 DevTableMem[boardnum] = pCh =
867                         kmalloc( sizeof(i2ChanStr) * nports, GFP_KERNEL );
868                 if ( !pCh ) {
869                         printk ( KERN_ERR "IP2: (i2_init_channel:) Out of memory.\n");
870                         goto err_release_region;
871                 }
872                 if ( !i2InitChannels( pB, nports, pCh ) ) {
873                         printk(KERN_ERR "IP2: i2InitChannels failed: %d\n",pB->i2eError);
874                         kfree ( pCh );
875                         goto err_release_region;
876                 }
877                 pB->i2eChannelPtr = &DevTable[portnum];
878                 pB->i2eChannelCnt = ABS_MOST_PORTS;
879
880                 for( box = 0; box < ABS_MAX_BOXES; ++box, portnum += ABS_BIGGEST_BOX ) {
881                         for( i = 0; i < ABS_BIGGEST_BOX; ++i ) {
882                                 if ( pB->i2eChannelMap[box] & (1 << i) ) {
883                                         DevTable[portnum + i] = pCh;
884                                         pCh->port_index = portnum + i;
885                                         pCh++;
886                                 }
887                         }
888                 }
889                 printk(KERN_INFO "IP2: EX box=%d ports=%d %d bit\n",
890                         nboxes, nports, pB->i2eDataWidth16 ? 16 : 8 );
891                 }
892                 goto ex_exit;
893         }
894         DevTableMem[boardnum] = pCh =
895                 kmalloc ( sizeof (i2ChanStr) * nports, GFP_KERNEL );
896         if ( !pCh ) {
897                 printk ( KERN_ERR "IP2: (i2_init_channel:) Out of memory.\n");
898                 goto err_release_region;
899         }
900         pB->i2eChannelPtr = pCh;
901         pB->i2eChannelCnt = nports;
902         if ( !i2InitChannels( pB, nports, pCh ) ) {
903                 printk(KERN_ERR "IP2: i2InitChannels failed: %d\n",pB->i2eError);
904                 kfree ( pCh );
905                 goto err_release_region;
906         }
907         pB->i2eChannelPtr = &DevTable[IP2_PORTS_PER_BOARD * boardnum];
908
909         for( i = 0; i < pB->i2eChannelCnt; ++i ) {
910                 DevTable[IP2_PORTS_PER_BOARD * boardnum + i] = pCh;
911                 pCh->port_index = (IP2_PORTS_PER_BOARD * boardnum) + i;
912                 pCh++;
913         }
914 ex_exit:
915         INIT_WORK(&pB->tqueue_interrupt, ip2_interrupt_bh);
916         return;
917
918 err_release_region:
919         release_region(ip2config.addr[boardnum], 8);
920 err_initialize:
921         kfree ( pB );
922         i2BoardPtrTable[boardnum] = NULL;
923         return;
924 }
925
926 /******************************************************************************/
927 /* Function:   find_eisa_board ( int start_slot )                             */
928 /* Parameters: First slot to check                                            */
929 /* Returns:    Address of EISA IntelliPort II controller                      */
930 /*                                                                            */
931 /* Description:                                                               */
932 /* This function searches for an EISA IntelliPort controller, starting        */
933 /* from the specified slot number. If the motherboard is not identified as an */
934 /* EISA motherboard, or no valid board ID is selected it returns 0. Otherwise */
935 /* it returns the base address of the controller.                             */
936 /******************************************************************************/
937 static unsigned short
938 find_eisa_board( int start_slot )
939 {
940         int i, j;
941         unsigned int idm = 0;
942         unsigned int idp = 0;
943         unsigned int base = 0;
944         unsigned int value;
945         int setup_address;
946         int setup_irq;
947         int ismine = 0;
948
949         /*
950          * First a check for an EISA motherboard, which we do by comparing the
951          * EISA ID registers for the system board and the first couple of slots.
952          * No slot ID should match the system board ID, but on an ISA or PCI
953          * machine the odds are that an empty bus will return similar values for
954          * each slot.
955          */
956         i = 0x0c80;
957         value = (inb(i) << 24) + (inb(i+1) << 16) + (inb(i+2) << 8) + inb(i+3);
958         for( i = 0x1c80; i <= 0x4c80; i += 0x1000 ) {
959                 j = (inb(i)<<24)+(inb(i+1)<<16)+(inb(i+2)<<8)+inb(i+3);
960                 if ( value == j )
961                         return 0;
962         }
963
964         /*
965          * OK, so we are inclined to believe that this is an EISA machine. Find
966          * an IntelliPort controller.
967          */
968         for( i = start_slot; i < 16; i++ ) {
969                 base = i << 12;
970                 idm = (inb(base + 0xc80) << 8) | (inb(base + 0xc81) & 0xff);
971                 idp = (inb(base + 0xc82) << 8) | (inb(base + 0xc83) & 0xff);
972                 ismine = 0;
973                 if ( idm == 0x0e8e ) {
974                         if ( idp == 0x0281 || idp == 0x0218 ) {
975                                 ismine = 1;
976                         } else if ( idp == 0x0282 || idp == 0x0283 ) {
977                                 ismine = 3;     /* Can do edge-trigger */
978                         }
979                         if ( ismine ) {
980                                 Eisa_slot = i;
981                                 break;
982                         }
983                 }
984         }
985         if ( !ismine )
986                 return 0;
987
988         /* It's some sort of EISA card, but at what address is it configured? */
989
990         setup_address = base + 0xc88;
991         value = inb(base + 0xc86);
992         setup_irq = (value & 8) ? Valid_Irqs[value & 7] : 0;
993
994         if ( (ismine & 2) && !(value & 0x10) ) {
995                 ismine = 1;     /* Could be edging, but not */
996         }
997
998         if ( Eisa_irq == 0 ) {
999                 Eisa_irq = setup_irq;
1000         } else if ( Eisa_irq != setup_irq ) {
1001                 printk ( KERN_ERR "IP2: EISA irq mismatch between EISA controllers\n" );
1002         }
1003
1004 #ifdef IP2DEBUG_INIT
1005 printk(KERN_DEBUG "Computone EISA board in slot %d, I.D. 0x%x%x, Address 0x%x",
1006                base >> 12, idm, idp, setup_address);
1007         if ( Eisa_irq ) {
1008                 printk(KERN_DEBUG ", Interrupt %d %s\n",
1009                        setup_irq, (ismine & 2) ? "(edge)" : "(level)");
1010         } else {
1011                 printk(KERN_DEBUG ", (polled)\n");
1012         }
1013 #endif
1014         return setup_address;
1015 }
1016
1017 /******************************************************************************/
1018 /* Function:   set_irq()                                                      */
1019 /* Parameters: index to board in board table                                  */
1020 /*             IRQ to use                                                     */
1021 /* Returns:    Success (0)                                                    */
1022 /*                                                                            */
1023 /* Description:                                                               */
1024 /******************************************************************************/
1025 static void
1026 set_irq( int boardnum, int boardIrq )
1027 {
1028         unsigned char tempCommand[16];
1029         i2eBordStrPtr pB = i2BoardPtrTable[boardnum];
1030         unsigned long flags;
1031
1032         /*
1033          * Notify the boards they may generate interrupts. This is done by
1034          * sending an in-line command to channel 0 on each board. This is why
1035          * the channels have to be defined already. For each board, if the
1036          * interrupt has never been defined, we must do so NOW, directly, since
1037          * board will not send flow control or even give an interrupt until this
1038          * is done.  If polling we must send 0 as the interrupt parameter.
1039          */
1040
1041         // We will get an interrupt here at the end of this function
1042
1043         iiDisableMailIrq(pB);
1044
1045         /* We build up the entire packet header. */
1046         CHANNEL_OF(tempCommand) = 0;
1047         PTYPE_OF(tempCommand) = PTYPE_INLINE;
1048         CMD_COUNT_OF(tempCommand) = 2;
1049         (CMD_OF(tempCommand))[0] = CMDVALUE_IRQ;
1050         (CMD_OF(tempCommand))[1] = boardIrq;
1051         /*
1052          * Write to FIFO; don't bother to adjust fifo capacity for this, since
1053          * board will respond almost immediately after SendMail hit.
1054          */
1055         write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1056         iiWriteBuf(pB, tempCommand, 4);
1057         write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1058         pB->i2eUsingIrq = boardIrq;
1059         pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
1060
1061         /* Need to update number of boards before you enable mailbox int */
1062         ++i2nBoards;
1063
1064         CHANNEL_OF(tempCommand) = 0;
1065         PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1066         CMD_COUNT_OF(tempCommand) = 6;
1067         (CMD_OF(tempCommand))[0] = 88;  // SILO
1068         (CMD_OF(tempCommand))[1] = 64;  // chars
1069         (CMD_OF(tempCommand))[2] = 32;  // ms
1070
1071         (CMD_OF(tempCommand))[3] = 28;  // MAX_BLOCK
1072         (CMD_OF(tempCommand))[4] = 64;  // chars
1073
1074         (CMD_OF(tempCommand))[5] = 87;  // HW_TEST
1075         write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1076         iiWriteBuf(pB, tempCommand, 8);
1077         write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1078
1079         CHANNEL_OF(tempCommand) = 0;
1080         PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1081         CMD_COUNT_OF(tempCommand) = 1;
1082         (CMD_OF(tempCommand))[0] = 84;  /* get BOX_IDS */
1083         iiWriteBuf(pB, tempCommand, 3);
1084
1085 #ifdef XXX
1086         // enable heartbeat for test porpoises
1087         CHANNEL_OF(tempCommand) = 0;
1088         PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1089         CMD_COUNT_OF(tempCommand) = 2;
1090         (CMD_OF(tempCommand))[0] = 44;  /* get ping */
1091         (CMD_OF(tempCommand))[1] = 200; /* 200 ms */
1092         write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1093         iiWriteBuf(pB, tempCommand, 4);
1094         write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1095 #endif
1096
1097         iiEnableMailIrq(pB);
1098         iiSendPendingMail(pB);
1099 }
1100
1101 /******************************************************************************/
1102 /* Interrupt Handler Section                                                  */
1103 /******************************************************************************/
1104
1105 static inline void
1106 service_all_boards(void)
1107 {
1108         int i;
1109         i2eBordStrPtr  pB;
1110
1111         /* Service every board on the list */
1112         for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
1113                 pB = i2BoardPtrTable[i];
1114                 if ( pB ) {
1115                         i2ServiceBoard( pB );
1116                 }
1117         }
1118 }
1119
1120
1121 /******************************************************************************/
1122 /* Function:   ip2_interrupt_bh(work)                                         */
1123 /* Parameters: work - pointer to the board structure                          */
1124 /* Returns:    Nothing                                                        */
1125 /*                                                                            */
1126 /* Description:                                                               */
1127 /*      Service the board in a bottom half interrupt handler and then         */
1128 /*      reenable the board's interrupts if it has an IRQ number               */
1129 /*                                                                            */
1130 /******************************************************************************/
1131 static void
1132 ip2_interrupt_bh(struct work_struct *work)
1133 {
1134         i2eBordStrPtr pB = container_of(work, i2eBordStr, tqueue_interrupt);
1135 //      pB better well be set or we have a problem!  We can only get
1136 //      here from the IMMEDIATE queue.  Here, we process the boards.
1137 //      Checking pB doesn't cost much and it saves us from the sanity checkers.
1138
1139         bh_counter++; 
1140
1141         if ( pB ) {
1142                 i2ServiceBoard( pB );
1143                 if( pB->i2eUsingIrq ) {
1144 //                      Re-enable his interrupts
1145                         iiEnableMailIrq(pB);
1146                 }
1147         }
1148 }
1149
1150
1151 /******************************************************************************/
1152 /* Function:   ip2_interrupt(int irq, void *dev_id)    */
1153 /* Parameters: irq - interrupt number                                         */
1154 /*             pointer to optional device ID structure                        */
1155 /* Returns:    Nothing                                                        */
1156 /*                                                                            */
1157 /* Description:                                                               */
1158 /*                                                                            */
1159 /*      Our task here is simply to identify each board which needs servicing. */
1160 /*      If we are queuing then, queue it to be serviced, and disable its irq  */
1161 /*      mask otherwise process the board directly.                            */
1162 /*                                                                            */
1163 /*      We could queue by IRQ but that just complicates things on both ends   */
1164 /*      with very little gain in performance (how many instructions does      */
1165 /*      it take to iterate on the immediate queue).                           */
1166 /*                                                                            */
1167 /*                                                                            */
1168 /******************************************************************************/
1169 static void
1170 ip2_irq_work(i2eBordStrPtr pB)
1171 {
1172 #ifdef USE_IQI
1173         if (NO_MAIL_HERE != ( pB->i2eStartMail = iiGetMail(pB))) {
1174 //              Disable his interrupt (will be enabled when serviced)
1175 //              This is mostly to protect from reentrancy.
1176                 iiDisableMailIrq(pB);
1177
1178 //              Park the board on the immediate queue for processing.
1179                 schedule_work(&pB->tqueue_interrupt);
1180
1181 //              Make sure the immediate queue is flagged to fire.
1182         }
1183 #else
1184
1185 //      We are using immediate servicing here.  This sucks and can
1186 //      cause all sorts of havoc with ppp and others.  The failsafe
1187 //      check on iiSendPendingMail could also throw a hairball.
1188
1189         i2ServiceBoard( pB );
1190
1191 #endif /* USE_IQI */
1192 }
1193
1194 static void
1195 ip2_polled_interrupt(void)
1196 {
1197         int i;
1198         i2eBordStrPtr  pB;
1199         const int irq = 0;
1200
1201         ip2trace (ITRC_NO_PORT, ITRC_INTR, 99, 1, irq );
1202
1203         /* Service just the boards on the list using this irq */
1204         for( i = 0; i < i2nBoards; ++i ) {
1205                 pB = i2BoardPtrTable[i];
1206
1207 //              Only process those boards which match our IRQ.
1208 //                      IRQ = 0 for polled boards, we won't poll "IRQ" boards
1209
1210                 if ( pB && (pB->i2eUsingIrq == irq) ) {
1211                         ip2_irq_work(pB);
1212                 }
1213         }
1214
1215         ++irq_counter;
1216
1217         ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1218 }
1219
1220 static irqreturn_t
1221 ip2_interrupt(int irq, void *dev_id)
1222 {
1223         i2eBordStrPtr pB = dev_id;
1224
1225         ip2trace (ITRC_NO_PORT, ITRC_INTR, 99, 1, pB->i2eUsingIrq );
1226
1227         ip2_irq_work(pB);
1228
1229         ++irq_counter;
1230
1231         ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1232         return IRQ_HANDLED;
1233 }
1234
1235 /******************************************************************************/
1236 /* Function:   ip2_poll(unsigned long arg)                                    */
1237 /* Parameters: ?                                                              */
1238 /* Returns:    Nothing                                                        */
1239 /*                                                                            */
1240 /* Description:                                                               */
1241 /* This function calls the library routine i2ServiceBoard for each board in   */
1242 /* the board table. This is used instead of the interrupt routine when polled */
1243 /* mode is specified.                                                         */
1244 /******************************************************************************/
1245 static void
1246 ip2_poll(unsigned long arg)
1247 {
1248         ip2trace (ITRC_NO_PORT, ITRC_INTR, 100, 0 );
1249
1250         TimerOn = 0; // it's the truth but not checked in service
1251
1252         // Just polled boards, IRQ = 0 will hit all non-interrupt boards.
1253         // It will NOT poll boards handled by hard interrupts.
1254         // The issue of queued BH interrupts is handled in ip2_interrupt().
1255         ip2_polled_interrupt();
1256
1257         PollTimer.expires = POLL_TIMEOUT;
1258         add_timer( &PollTimer );
1259         TimerOn = 1;
1260
1261         ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1262 }
1263
1264 static void do_input(struct work_struct *work)
1265 {
1266         i2ChanStrPtr pCh = container_of(work, i2ChanStr, tqueue_input);
1267         unsigned long flags;
1268
1269         ip2trace(CHANN, ITRC_INPUT, 21, 0 );
1270
1271         // Data input
1272         if ( pCh->pTTY != NULL ) {
1273                 read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
1274                 if (!pCh->throttled && (pCh->Ibuf_stuff != pCh->Ibuf_strip)) {
1275                         read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
1276                         i2Input( pCh );
1277                 } else
1278                         read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
1279         } else {
1280                 ip2trace(CHANN, ITRC_INPUT, 22, 0 );
1281
1282                 i2InputFlush( pCh );
1283         }
1284 }
1285
1286 // code duplicated from n_tty (ldisc)
1287 static inline void  isig(int sig, struct tty_struct *tty, int flush)
1288 {
1289         if (tty->pgrp)
1290                 kill_pgrp(tty->pgrp, sig, 1);
1291         if (flush || !L_NOFLSH(tty)) {
1292                 if ( tty->ldisc.flush_buffer )  
1293                         tty->ldisc.flush_buffer(tty);
1294                 i2InputFlush( tty->driver_data );
1295         }
1296 }
1297
1298 static void do_status(struct work_struct *work)
1299 {
1300         i2ChanStrPtr pCh = container_of(work, i2ChanStr, tqueue_status);
1301         int status;
1302
1303         status =  i2GetStatus( pCh, (I2_BRK|I2_PAR|I2_FRA|I2_OVR) );
1304
1305         ip2trace (CHANN, ITRC_STATUS, 21, 1, status );
1306
1307         if (pCh->pTTY && (status & (I2_BRK|I2_PAR|I2_FRA|I2_OVR)) ) {
1308                 if ( (status & I2_BRK) ) {
1309                         // code duplicated from n_tty (ldisc)
1310                         if (I_IGNBRK(pCh->pTTY))
1311                                 goto skip_this;
1312                         if (I_BRKINT(pCh->pTTY)) {
1313                                 isig(SIGINT, pCh->pTTY, 1);
1314                                 goto skip_this;
1315                         }
1316                         wake_up_interruptible(&pCh->pTTY->read_wait);
1317                 }
1318 #ifdef NEVER_HAPPENS_AS_SETUP_XXX
1319         // and can't work because we don't know the_char
1320         // as the_char is reported on a separate path
1321         // The intelligent board does this stuff as setup
1322         {
1323         char brkf = TTY_NORMAL;
1324         unsigned char brkc = '\0';
1325         unsigned char tmp;
1326                 if ( (status & I2_BRK) ) {
1327                         brkf = TTY_BREAK;
1328                         brkc = '\0';
1329                 } 
1330                 else if (status & I2_PAR) {
1331                         brkf = TTY_PARITY;
1332                         brkc = the_char;
1333                 } else if (status & I2_FRA) {
1334                         brkf = TTY_FRAME;
1335                         brkc = the_char;
1336                 } else if (status & I2_OVR) {
1337                         brkf = TTY_OVERRUN;
1338                         brkc = the_char;
1339                 }
1340                 tmp = pCh->pTTY->real_raw;
1341                 pCh->pTTY->real_raw = 0;
1342                 pCh->pTTY->ldisc.receive_buf( pCh->pTTY, &brkc, &brkf, 1 );
1343                 pCh->pTTY->real_raw = tmp;
1344         }
1345 #endif /* NEVER_HAPPENS_AS_SETUP_XXX */
1346         }
1347 skip_this:
1348
1349         if ( status & (I2_DDCD | I2_DDSR | I2_DCTS | I2_DRI) ) {
1350                 wake_up_interruptible(&pCh->delta_msr_wait);
1351
1352                 if ( (pCh->flags & ASYNC_CHECK_CD) && (status & I2_DDCD) ) {
1353                         if ( status & I2_DCD ) {
1354                                 if ( pCh->wopen ) {
1355                                         wake_up_interruptible ( &pCh->open_wait );
1356                                 }
1357                         } else {
1358                                 if (pCh->pTTY &&  (!(pCh->pTTY->termios->c_cflag & CLOCAL)) ) {
1359                                         tty_hangup( pCh->pTTY );
1360                                 }
1361                         }
1362                 }
1363         }
1364
1365         ip2trace (CHANN, ITRC_STATUS, 26, 0 );
1366 }
1367
1368 /******************************************************************************/
1369 /* Device Open/Close/Ioctl Entry Point Section                                */
1370 /******************************************************************************/
1371
1372 /******************************************************************************/
1373 /* Function:   open_sanity_check()                                            */
1374 /* Parameters: Pointer to tty structure                                       */
1375 /*             Pointer to file structure                                      */
1376 /* Returns:    Success or failure                                             */
1377 /*                                                                            */
1378 /* Description:                                                               */
1379 /* Verifies the structure magic numbers and cross links.                      */
1380 /******************************************************************************/
1381 #ifdef IP2DEBUG_OPEN
1382 static void 
1383 open_sanity_check( i2ChanStrPtr pCh, i2eBordStrPtr pBrd )
1384 {
1385         if ( pBrd->i2eValid != I2E_MAGIC ) {
1386                 printk(KERN_ERR "IP2: invalid board structure\n" );
1387         } else if ( pBrd != pCh->pMyBord ) {
1388                 printk(KERN_ERR "IP2: board structure pointer mismatch (%p)\n",
1389                          pCh->pMyBord );
1390         } else if ( pBrd->i2eChannelCnt < pCh->port_index ) {
1391                 printk(KERN_ERR "IP2: bad device index (%d)\n", pCh->port_index );
1392         } else if (&((i2ChanStrPtr)pBrd->i2eChannelPtr)[pCh->port_index] != pCh) {
1393         } else {
1394                 printk(KERN_INFO "IP2: all pointers check out!\n" );
1395         }
1396 }
1397 #endif
1398
1399
1400 /******************************************************************************/
1401 /* Function:   ip2_open()                                                     */
1402 /* Parameters: Pointer to tty structure                                       */
1403 /*             Pointer to file structure                                      */
1404 /* Returns:    Success or failure                                             */
1405 /*                                                                            */
1406 /* Description: (MANDATORY)                                                   */
1407 /* A successful device open has to run a gauntlet of checks before it         */
1408 /* completes. After some sanity checking and pointer setup, the function      */
1409 /* blocks until all conditions are satisfied. It then initialises the port to */
1410 /* the default characteristics and returns.                                   */
1411 /******************************************************************************/
1412 static int
1413 ip2_open( PTTY tty, struct file *pFile )
1414 {
1415         wait_queue_t wait;
1416         int rc = 0;
1417         int do_clocal = 0;
1418         i2ChanStrPtr  pCh = DevTable[tty->index];
1419
1420         ip2trace (tty->index, ITRC_OPEN, ITRC_ENTER, 0 );
1421
1422         if ( pCh == NULL ) {
1423                 return -ENODEV;
1424         }
1425         /* Setup pointer links in device and tty structures */
1426         pCh->pTTY = tty;
1427         tty->driver_data = pCh;
1428
1429 #ifdef IP2DEBUG_OPEN
1430         printk(KERN_DEBUG \
1431                         "IP2:open(tty=%p,pFile=%p):dev=%s,ch=%d,idx=%d\n",
1432                tty, pFile, tty->name, pCh->infl.hd.i2sChannel, pCh->port_index);
1433         open_sanity_check ( pCh, pCh->pMyBord );
1434 #endif
1435
1436         i2QueueCommands(PTYPE_INLINE, pCh, 100, 3, CMD_DTRUP,CMD_RTSUP,CMD_DCD_REP);
1437         pCh->dataSetOut |= (I2_DTR | I2_RTS);
1438         serviceOutgoingFifo( pCh->pMyBord );
1439
1440         /* Block here until the port is ready (per serial and istallion) */
1441         /*
1442          * 1. If the port is in the middle of closing wait for the completion
1443          *    and then return the appropriate error.
1444          */
1445         init_waitqueue_entry(&wait, current);
1446         add_wait_queue(&pCh->close_wait, &wait);
1447         set_current_state( TASK_INTERRUPTIBLE );
1448
1449         if ( tty_hung_up_p(pFile) || ( pCh->flags & ASYNC_CLOSING )) {
1450                 if ( pCh->flags & ASYNC_CLOSING ) {
1451                         schedule();
1452                 }
1453                 if ( tty_hung_up_p(pFile) ) {
1454                         set_current_state( TASK_RUNNING );
1455                         remove_wait_queue(&pCh->close_wait, &wait);
1456                         return( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS;
1457                 }
1458         }
1459         set_current_state( TASK_RUNNING );
1460         remove_wait_queue(&pCh->close_wait, &wait);
1461
1462         /*
1463          * 3. Handle a non-blocking open of a normal port.
1464          */
1465         if ( (pFile->f_flags & O_NONBLOCK) || (tty->flags & (1<<TTY_IO_ERROR) )) {
1466                 pCh->flags |= ASYNC_NORMAL_ACTIVE;
1467                 goto noblock;
1468         }
1469         /*
1470          * 4. Now loop waiting for the port to be free and carrier present
1471          *    (if required).
1472          */
1473         if ( tty->termios->c_cflag & CLOCAL )
1474                 do_clocal = 1;
1475
1476 #ifdef IP2DEBUG_OPEN
1477         printk(KERN_DEBUG "OpenBlock: do_clocal = %d\n", do_clocal);
1478 #endif
1479
1480         ++pCh->wopen;
1481
1482         init_waitqueue_entry(&wait, current);
1483         add_wait_queue(&pCh->open_wait, &wait);
1484
1485         for(;;) {
1486                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 2, CMD_DTRUP, CMD_RTSUP);
1487                 pCh->dataSetOut |= (I2_DTR | I2_RTS);
1488                 set_current_state( TASK_INTERRUPTIBLE );
1489                 serviceOutgoingFifo( pCh->pMyBord );
1490                 if ( tty_hung_up_p(pFile) ) {
1491                         set_current_state( TASK_RUNNING );
1492                         remove_wait_queue(&pCh->open_wait, &wait);
1493                         return ( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EBUSY : -ERESTARTSYS;
1494                 }
1495                 if (!(pCh->flags & ASYNC_CLOSING) && 
1496                                 (do_clocal || (pCh->dataSetIn & I2_DCD) )) {
1497                         rc = 0;
1498                         break;
1499                 }
1500
1501 #ifdef IP2DEBUG_OPEN
1502                 printk(KERN_DEBUG "ASYNC_CLOSING = %s\n",
1503                         (pCh->flags & ASYNC_CLOSING)?"True":"False");
1504                 printk(KERN_DEBUG "OpenBlock: waiting for CD or signal\n");
1505 #endif
1506                 ip2trace (CHANN, ITRC_OPEN, 3, 2, 0,
1507                                 (pCh->flags & ASYNC_CLOSING) );
1508                 /* check for signal */
1509                 if (signal_pending(current)) {
1510                         rc = (( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS);
1511                         break;
1512                 }
1513                 schedule();
1514         }
1515         set_current_state( TASK_RUNNING );
1516         remove_wait_queue(&pCh->open_wait, &wait);
1517
1518         --pCh->wopen; //why count?
1519
1520         ip2trace (CHANN, ITRC_OPEN, 4, 0 );
1521
1522         if (rc != 0 ) {
1523                 return rc;
1524         }
1525         pCh->flags |= ASYNC_NORMAL_ACTIVE;
1526
1527 noblock:
1528
1529         /* first open - Assign termios structure to port */
1530         if ( tty->count == 1 ) {
1531                 i2QueueCommands(PTYPE_INLINE, pCh, 0, 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
1532                 /* Now we must send the termios settings to the loadware */
1533                 set_params( pCh, NULL );
1534         }
1535
1536         /*
1537          * Now set any i2lib options. These may go away if the i2lib code ends
1538          * up rolled into the mainline.
1539          */
1540         pCh->channelOptions |= CO_NBLOCK_WRITE;
1541
1542 #ifdef IP2DEBUG_OPEN
1543         printk (KERN_DEBUG "IP2: open completed\n" );
1544 #endif
1545         serviceOutgoingFifo( pCh->pMyBord );
1546
1547         ip2trace (CHANN, ITRC_OPEN, ITRC_RETURN, 0 );
1548
1549         return 0;
1550 }
1551
1552 /******************************************************************************/
1553 /* Function:   ip2_close()                                                    */
1554 /* Parameters: Pointer to tty structure                                       */
1555 /*             Pointer to file structure                                      */
1556 /* Returns:    Nothing                                                        */
1557 /*                                                                            */
1558 /* Description:                                                               */
1559 /*                                                                            */
1560 /*                                                                            */
1561 /******************************************************************************/
1562 static void
1563 ip2_close( PTTY tty, struct file *pFile )
1564 {
1565         i2ChanStrPtr  pCh = tty->driver_data;
1566
1567         if ( !pCh ) {
1568                 return;
1569         }
1570
1571         ip2trace (CHANN, ITRC_CLOSE, ITRC_ENTER, 0 );
1572
1573 #ifdef IP2DEBUG_OPEN
1574         printk(KERN_DEBUG "IP2:close %s:\n",tty->name);
1575 #endif
1576
1577         if ( tty_hung_up_p ( pFile ) ) {
1578
1579                 ip2trace (CHANN, ITRC_CLOSE, 2, 1, 2 );
1580
1581                 return;
1582         }
1583         if ( tty->count > 1 ) { /* not the last close */
1584
1585                 ip2trace (CHANN, ITRC_CLOSE, 2, 1, 3 );
1586
1587                 return;
1588         }
1589         pCh->flags |= ASYNC_CLOSING;    // last close actually
1590
1591         tty->closing = 1;
1592
1593         if (pCh->ClosingWaitTime != ASYNC_CLOSING_WAIT_NONE) {
1594                 /*
1595                  * Before we drop DTR, make sure the transmitter has completely drained.
1596                  * This uses an timeout, after which the close
1597                  * completes.
1598                  */
1599                 ip2_wait_until_sent(tty, pCh->ClosingWaitTime );
1600         }
1601         /*
1602          * At this point we stop accepting input. Here we flush the channel
1603          * input buffer which will allow the board to send up more data. Any
1604          * additional input is tossed at interrupt/poll time.
1605          */
1606         i2InputFlush( pCh );
1607
1608         /* disable DSS reporting */
1609         i2QueueCommands(PTYPE_INLINE, pCh, 100, 4,
1610                                 CMD_DCD_NREP, CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
1611         if ( !tty || (tty->termios->c_cflag & HUPCL) ) {
1612                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 2, CMD_RTSDN, CMD_DTRDN);
1613                 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
1614                 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
1615         }
1616
1617         serviceOutgoingFifo ( pCh->pMyBord );
1618
1619         tty_ldisc_flush(tty);
1620         tty_driver_flush_buffer(tty);
1621         tty->closing = 0;
1622         
1623         pCh->pTTY = NULL;
1624
1625         if (pCh->wopen) {
1626                 if (pCh->ClosingDelay) {
1627                         msleep_interruptible(jiffies_to_msecs(pCh->ClosingDelay));
1628                 }
1629                 wake_up_interruptible(&pCh->open_wait);
1630         }
1631
1632         pCh->flags &=~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1633         wake_up_interruptible(&pCh->close_wait);
1634
1635 #ifdef IP2DEBUG_OPEN
1636         DBG_CNT("ip2_close: after wakeups--");
1637 #endif
1638
1639
1640         ip2trace (CHANN, ITRC_CLOSE, ITRC_RETURN, 1, 1 );
1641
1642         return;
1643 }
1644
1645 /******************************************************************************/
1646 /* Function:   ip2_hangup()                                                   */
1647 /* Parameters: Pointer to tty structure                                       */
1648 /* Returns:    Nothing                                                        */
1649 /*                                                                            */
1650 /* Description:                                                               */
1651 /*                                                                            */
1652 /*                                                                            */
1653 /******************************************************************************/
1654 static void
1655 ip2_hangup ( PTTY tty )
1656 {
1657         i2ChanStrPtr  pCh = tty->driver_data;
1658
1659         if( !pCh ) {
1660                 return;
1661         }
1662
1663         ip2trace (CHANN, ITRC_HANGUP, ITRC_ENTER, 0 );
1664
1665         ip2_flush_buffer(tty);
1666
1667         /* disable DSS reporting */
1668
1669         i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_DCD_NREP);
1670         i2QueueCommands(PTYPE_INLINE, pCh, 0, 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
1671         if ( (tty->termios->c_cflag & HUPCL) ) {
1672                 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 2, CMD_RTSDN, CMD_DTRDN);
1673                 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
1674                 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
1675         }
1676         i2QueueCommands(PTYPE_INLINE, pCh, 1, 3, 
1677                                 CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
1678         serviceOutgoingFifo ( pCh->pMyBord );
1679
1680         wake_up_interruptible ( &pCh->delta_msr_wait );
1681
1682         pCh->flags &= ~ASYNC_NORMAL_ACTIVE;
1683         pCh->pTTY = NULL;
1684         wake_up_interruptible ( &pCh->open_wait );
1685
1686         ip2trace (CHANN, ITRC_HANGUP, ITRC_RETURN, 0 );
1687 }
1688
1689 /******************************************************************************/
1690 /******************************************************************************/
1691 /* Device Output Section                                                      */
1692 /******************************************************************************/
1693 /******************************************************************************/
1694
1695 /******************************************************************************/
1696 /* Function:   ip2_write()                                                    */
1697 /* Parameters: Pointer to tty structure                                       */
1698 /*             Flag denoting data is in user (1) or kernel (0) space          */
1699 /*             Pointer to data                                                */
1700 /*             Number of bytes to write                                       */
1701 /* Returns:    Number of bytes actually written                               */
1702 /*                                                                            */
1703 /* Description: (MANDATORY)                                                   */
1704 /*                                                                            */
1705 /*                                                                            */
1706 /******************************************************************************/
1707 static int
1708 ip2_write( PTTY tty, const unsigned char *pData, int count)
1709 {
1710         i2ChanStrPtr  pCh = tty->driver_data;
1711         int bytesSent = 0;
1712         unsigned long flags;
1713
1714         ip2trace (CHANN, ITRC_WRITE, ITRC_ENTER, 2, count, -1 );
1715
1716         /* Flush out any buffered data left over from ip2_putchar() calls. */
1717         ip2_flush_chars( tty );
1718
1719         /* This is the actual move bit. Make sure it does what we need!!!!! */
1720         write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1721         bytesSent = i2Output( pCh, pData, count);
1722         write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1723
1724         ip2trace (CHANN, ITRC_WRITE, ITRC_RETURN, 1, bytesSent );
1725
1726         return bytesSent > 0 ? bytesSent : 0;
1727 }
1728
1729 /******************************************************************************/
1730 /* Function:   ip2_putchar()                                                  */
1731 /* Parameters: Pointer to tty structure                                       */
1732 /*             Character to write                                             */
1733 /* Returns:    Nothing                                                        */
1734 /*                                                                            */
1735 /* Description:                                                               */
1736 /*                                                                            */
1737 /*                                                                            */
1738 /******************************************************************************/
1739 static int
1740 ip2_putchar( PTTY tty, unsigned char ch )
1741 {
1742         i2ChanStrPtr  pCh = tty->driver_data;
1743         unsigned long flags;
1744
1745 //      ip2trace (CHANN, ITRC_PUTC, ITRC_ENTER, 1, ch );
1746
1747         write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1748         pCh->Pbuf[pCh->Pbuf_stuff++] = ch;
1749         if ( pCh->Pbuf_stuff == sizeof pCh->Pbuf ) {
1750                 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1751                 ip2_flush_chars( tty );
1752         } else
1753                 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1754         return 1;
1755
1756 //      ip2trace (CHANN, ITRC_PUTC, ITRC_RETURN, 1, ch );
1757 }
1758
1759 /******************************************************************************/
1760 /* Function:   ip2_flush_chars()                                              */
1761 /* Parameters: Pointer to tty structure                                       */
1762 /* Returns:    Nothing                                                        */
1763 /*                                                                            */
1764 /* Description:                                                               */
1765 /*                                                                            */
1766 /******************************************************************************/
1767 static void
1768 ip2_flush_chars( PTTY tty )
1769 {
1770         int   strip;
1771         i2ChanStrPtr  pCh = tty->driver_data;
1772         unsigned long flags;
1773
1774         write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1775         if ( pCh->Pbuf_stuff ) {
1776
1777 //              ip2trace (CHANN, ITRC_PUTC, 10, 1, strip );
1778
1779                 //
1780                 // We may need to restart i2Output if it does not fullfill this request
1781                 //
1782                 strip = i2Output( pCh, pCh->Pbuf, pCh->Pbuf_stuff);
1783                 if ( strip != pCh->Pbuf_stuff ) {
1784                         memmove( pCh->Pbuf, &pCh->Pbuf[strip], pCh->Pbuf_stuff - strip );
1785                 }
1786                 pCh->Pbuf_stuff -= strip;
1787         }
1788         write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1789 }
1790
1791 /******************************************************************************/
1792 /* Function:   ip2_write_room()                                               */
1793 /* Parameters: Pointer to tty structure                                       */
1794 /* Returns:    Number of bytes that the driver can accept                     */
1795 /*                                                                            */
1796 /* Description:                                                               */
1797 /*                                                                            */
1798 /******************************************************************************/
1799 static int
1800 ip2_write_room ( PTTY tty )
1801 {
1802         int bytesFree;
1803         i2ChanStrPtr  pCh = tty->driver_data;
1804         unsigned long flags;
1805
1806         read_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1807         bytesFree = i2OutputFree( pCh ) - pCh->Pbuf_stuff;
1808         read_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1809
1810         ip2trace (CHANN, ITRC_WRITE, 11, 1, bytesFree );
1811
1812         return ((bytesFree > 0) ? bytesFree : 0);
1813 }
1814
1815 /******************************************************************************/
1816 /* Function:   ip2_chars_in_buf()                                             */
1817 /* Parameters: Pointer to tty structure                                       */
1818 /* Returns:    Number of bytes queued for transmission                        */
1819 /*                                                                            */
1820 /* Description:                                                               */
1821 /*                                                                            */
1822 /*                                                                            */
1823 /******************************************************************************/
1824 static int
1825 ip2_chars_in_buf ( PTTY tty )
1826 {
1827         i2ChanStrPtr  pCh = tty->driver_data;
1828         int rc;
1829         unsigned long flags;
1830
1831         ip2trace (CHANN, ITRC_WRITE, 12, 1, pCh->Obuf_char_count + pCh->Pbuf_stuff );
1832
1833 #ifdef IP2DEBUG_WRITE
1834         printk (KERN_DEBUG "IP2: chars in buffer = %d (%d,%d)\n",
1835                                  pCh->Obuf_char_count + pCh->Pbuf_stuff,
1836                                  pCh->Obuf_char_count, pCh->Pbuf_stuff );
1837 #endif
1838         read_lock_irqsave(&pCh->Obuf_spinlock, flags);
1839         rc =  pCh->Obuf_char_count;
1840         read_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1841         read_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1842         rc +=  pCh->Pbuf_stuff;
1843         read_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1844         return rc;
1845 }
1846
1847 /******************************************************************************/
1848 /* Function:   ip2_flush_buffer()                                             */
1849 /* Parameters: Pointer to tty structure                                       */
1850 /* Returns:    Nothing                                                        */
1851 /*                                                                            */
1852 /* Description:                                                               */
1853 /*                                                                            */
1854 /*                                                                            */
1855 /******************************************************************************/
1856 static void
1857 ip2_flush_buffer( PTTY tty )
1858 {
1859         i2ChanStrPtr  pCh = tty->driver_data;
1860         unsigned long flags;
1861
1862         ip2trace (CHANN, ITRC_FLUSH, ITRC_ENTER, 0 );
1863
1864 #ifdef IP2DEBUG_WRITE
1865         printk (KERN_DEBUG "IP2: flush buffer\n" );
1866 #endif
1867         write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1868         pCh->Pbuf_stuff = 0;
1869         write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1870         i2FlushOutput( pCh );
1871         ip2_owake(tty);
1872
1873         ip2trace (CHANN, ITRC_FLUSH, ITRC_RETURN, 0 );
1874
1875 }
1876
1877 /******************************************************************************/
1878 /* Function:   ip2_wait_until_sent()                                          */
1879 /* Parameters: Pointer to tty structure                                       */
1880 /*             Timeout for wait.                                              */
1881 /* Returns:    Nothing                                                        */
1882 /*                                                                            */
1883 /* Description:                                                               */
1884 /* This function is used in place of the normal tty_wait_until_sent, which    */
1885 /* only waits for the driver buffers to be empty (or rather, those buffers    */
1886 /* reported by chars_in_buffer) which doesn't work for IP2 due to the         */
1887 /* indeterminate number of bytes buffered on the board.                       */
1888 /******************************************************************************/
1889 static void
1890 ip2_wait_until_sent ( PTTY tty, int timeout )
1891 {
1892         int i = jiffies;
1893         i2ChanStrPtr  pCh = tty->driver_data;
1894
1895         tty_wait_until_sent(tty, timeout );
1896         if ( (i = timeout - (jiffies -i)) > 0)
1897                 i2DrainOutput( pCh, i );
1898 }
1899
1900 /******************************************************************************/
1901 /******************************************************************************/
1902 /* Device Input Section                                                       */
1903 /******************************************************************************/
1904 /******************************************************************************/
1905
1906 /******************************************************************************/
1907 /* Function:   ip2_throttle()                                                 */
1908 /* Parameters: Pointer to tty structure                                       */
1909 /* Returns:    Nothing                                                        */
1910 /*                                                                            */
1911 /* Description:                                                               */
1912 /*                                                                            */
1913 /*                                                                            */
1914 /******************************************************************************/
1915 static void
1916 ip2_throttle ( PTTY tty )
1917 {
1918         i2ChanStrPtr  pCh = tty->driver_data;
1919
1920 #ifdef IP2DEBUG_READ
1921         printk (KERN_DEBUG "IP2: throttle\n" );
1922 #endif
1923         /*
1924          * Signal the poll/interrupt handlers not to forward incoming data to
1925          * the line discipline. This will cause the buffers to fill up in the
1926          * library and thus cause the library routines to send the flow control
1927          * stuff.
1928          */
1929         pCh->throttled = 1;
1930 }
1931
1932 /******************************************************************************/
1933 /* Function:   ip2_unthrottle()                                               */
1934 /* Parameters: Pointer to tty structure                                       */
1935 /* Returns:    Nothing                                                        */
1936 /*                                                                            */
1937 /* Description:                                                               */
1938 /*                                                                            */
1939 /*                                                                            */
1940 /******************************************************************************/
1941 static void
1942 ip2_unthrottle ( PTTY tty )
1943 {
1944         i2ChanStrPtr  pCh = tty->driver_data;
1945         unsigned long flags;
1946
1947 #ifdef IP2DEBUG_READ
1948         printk (KERN_DEBUG "IP2: unthrottle\n" );
1949 #endif
1950
1951         /* Pass incoming data up to the line discipline again. */
1952         pCh->throttled = 0;
1953         i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_RESUME);
1954         serviceOutgoingFifo( pCh->pMyBord );
1955         read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
1956         if ( pCh->Ibuf_stuff != pCh->Ibuf_strip ) {
1957                 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
1958 #ifdef IP2DEBUG_READ
1959                 printk (KERN_DEBUG "i2Input called from unthrottle\n" );
1960 #endif
1961                 i2Input( pCh );
1962         } else
1963                 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
1964 }
1965
1966 static void
1967 ip2_start ( PTTY tty )
1968 {
1969         i2ChanStrPtr  pCh = DevTable[tty->index];
1970
1971         i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_RESUME);
1972         i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_UNSUSPEND);
1973         i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_RESUME);
1974 #ifdef IP2DEBUG_WRITE
1975         printk (KERN_DEBUG "IP2: start tx\n" );
1976 #endif
1977 }
1978
1979 static void
1980 ip2_stop ( PTTY tty )
1981 {
1982         i2ChanStrPtr  pCh = DevTable[tty->index];
1983
1984         i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_SUSPEND);
1985 #ifdef IP2DEBUG_WRITE
1986         printk (KERN_DEBUG "IP2: stop tx\n" );
1987 #endif
1988 }
1989
1990 /******************************************************************************/
1991 /* Device Ioctl Section                                                       */
1992 /******************************************************************************/
1993
1994 static int ip2_tiocmget(struct tty_struct *tty, struct file *file)
1995 {
1996         i2ChanStrPtr pCh = DevTable[tty->index];
1997 #ifdef  ENABLE_DSSNOW
1998         wait_queue_t wait;
1999 #endif
2000
2001         if (pCh == NULL)
2002                 return -ENODEV;
2003
2004 /*
2005         FIXME - the following code is causing a NULL pointer dereference in
2006         2.3.51 in an interrupt handler.  It's suppose to prompt the board
2007         to return the DSS signal status immediately.  Why doesn't it do
2008         the same thing in 2.2.14?
2009 */
2010
2011 /*      This thing is still busted in the 1.2.12 driver on 2.4.x
2012         and even hoses the serial console so the oops can be trapped.
2013                 /\/\|=mhw=|\/\/                 */
2014
2015 #ifdef  ENABLE_DSSNOW
2016         i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DSS_NOW);
2017
2018         init_waitqueue_entry(&wait, current);
2019         add_wait_queue(&pCh->dss_now_wait, &wait);
2020         set_current_state( TASK_INTERRUPTIBLE );
2021
2022         serviceOutgoingFifo( pCh->pMyBord );
2023
2024         schedule();
2025
2026         set_current_state( TASK_RUNNING );
2027         remove_wait_queue(&pCh->dss_now_wait, &wait);
2028
2029         if (signal_pending(current)) {
2030                 return -EINTR;
2031         }
2032 #endif
2033         return  ((pCh->dataSetOut & I2_RTS) ? TIOCM_RTS : 0)
2034               | ((pCh->dataSetOut & I2_DTR) ? TIOCM_DTR : 0)
2035               | ((pCh->dataSetIn  & I2_DCD) ? TIOCM_CAR : 0)
2036               | ((pCh->dataSetIn  & I2_RI)  ? TIOCM_RNG : 0)
2037               | ((pCh->dataSetIn  & I2_DSR) ? TIOCM_DSR : 0)
2038               | ((pCh->dataSetIn  & I2_CTS) ? TIOCM_CTS : 0);
2039 }
2040
2041 static int ip2_tiocmset(struct tty_struct *tty, struct file *file,
2042                         unsigned int set, unsigned int clear)
2043 {
2044         i2ChanStrPtr pCh = DevTable[tty->index];
2045
2046         if (pCh == NULL)
2047                 return -ENODEV;
2048
2049         if (set & TIOCM_RTS) {
2050                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_RTSUP);
2051                 pCh->dataSetOut |= I2_RTS;
2052         }
2053         if (set & TIOCM_DTR) {
2054                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DTRUP);
2055                 pCh->dataSetOut |= I2_DTR;
2056         }
2057
2058         if (clear & TIOCM_RTS) {
2059                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_RTSDN);
2060                 pCh->dataSetOut &= ~I2_RTS;
2061         }
2062         if (clear & TIOCM_DTR) {
2063                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DTRDN);
2064                 pCh->dataSetOut &= ~I2_DTR;
2065         }
2066         serviceOutgoingFifo( pCh->pMyBord );
2067         return 0;
2068 }
2069
2070 /******************************************************************************/
2071 /* Function:   ip2_ioctl()                                                    */
2072 /* Parameters: Pointer to tty structure                                       */
2073 /*             Pointer to file structure                                      */
2074 /*             Command                                                        */
2075 /*             Argument                                                       */
2076 /* Returns:    Success or failure                                             */
2077 /*                                                                            */
2078 /* Description:                                                               */
2079 /*                                                                            */
2080 /*                                                                            */
2081 /******************************************************************************/
2082 static int
2083 ip2_ioctl ( PTTY tty, struct file *pFile, UINT cmd, ULONG arg )
2084 {
2085         wait_queue_t wait;
2086         i2ChanStrPtr pCh = DevTable[tty->index];
2087         i2eBordStrPtr pB;
2088         struct async_icount cprev, cnow;        /* kernel counter temps */
2089         struct serial_icounter_struct __user *p_cuser;
2090         int rc = 0;
2091         unsigned long flags;
2092         void __user *argp = (void __user *)arg;
2093
2094         if ( pCh == NULL )
2095                 return -ENODEV;
2096
2097         pB = pCh->pMyBord;
2098
2099         ip2trace (CHANN, ITRC_IOCTL, ITRC_ENTER, 2, cmd, arg );
2100
2101 #ifdef IP2DEBUG_IOCTL
2102         printk(KERN_DEBUG "IP2: ioctl cmd (%x), arg (%lx)\n", cmd, arg );
2103 #endif
2104
2105         switch(cmd) {
2106         case TIOCGSERIAL:
2107
2108                 ip2trace (CHANN, ITRC_IOCTL, 2, 1, rc );
2109
2110                 rc = get_serial_info(pCh, argp);
2111                 if (rc)
2112                         return rc;
2113                 break;
2114
2115         case TIOCSSERIAL:
2116
2117                 ip2trace (CHANN, ITRC_IOCTL, 3, 1, rc );
2118
2119                 rc = set_serial_info(pCh, argp);
2120                 if (rc)
2121                         return rc;
2122                 break;
2123
2124         case TCXONC:
2125                 rc = tty_check_change(tty);
2126                 if (rc)
2127                         return rc;
2128                 switch (arg) {
2129                 case TCOOFF:
2130                         //return  -ENOIOCTLCMD;
2131                         break;
2132                 case TCOON:
2133                         //return  -ENOIOCTLCMD;
2134                         break;
2135                 case TCIOFF:
2136                         if (STOP_CHAR(tty) != __DISABLED_CHAR) {
2137                                 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 1,
2138                                                 CMD_XMIT_NOW(STOP_CHAR(tty)));
2139                         }
2140                         break;
2141                 case TCION:
2142                         if (START_CHAR(tty) != __DISABLED_CHAR) {
2143                                 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 1,
2144                                                 CMD_XMIT_NOW(START_CHAR(tty)));
2145                         }
2146                         break;
2147                 default:
2148                         return -EINVAL;
2149                 }
2150                 return 0;
2151
2152         case TCSBRK:   /* SVID version: non-zero arg --> no break */
2153                 rc = tty_check_change(tty);
2154
2155                 ip2trace (CHANN, ITRC_IOCTL, 4, 1, rc );
2156
2157                 if (!rc) {
2158                         ip2_wait_until_sent(tty,0);
2159                         if (!arg) {
2160                                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_SEND_BRK(250));
2161                                 serviceOutgoingFifo( pCh->pMyBord );
2162                         }
2163                 }
2164                 break;
2165
2166         case TCSBRKP:  /* support for POSIX tcsendbreak() */
2167                 rc = tty_check_change(tty);
2168
2169                 ip2trace (CHANN, ITRC_IOCTL, 5, 1, rc );
2170
2171                 if (!rc) {
2172                         ip2_wait_until_sent(tty,0);
2173                         i2QueueCommands(PTYPE_INLINE, pCh, 100, 1,
2174                                 CMD_SEND_BRK(arg ? arg*100 : 250));
2175                         serviceOutgoingFifo ( pCh->pMyBord );   
2176                 }
2177                 break;
2178
2179         case TIOCGSOFTCAR:
2180
2181                 ip2trace (CHANN, ITRC_IOCTL, 6, 1, rc );
2182
2183                         rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
2184                 if (rc) 
2185                         return rc;
2186         break;
2187
2188         case TIOCSSOFTCAR:
2189
2190                 ip2trace (CHANN, ITRC_IOCTL, 7, 1, rc );
2191
2192                 rc = get_user(arg,(unsigned long __user *) argp);
2193                 if (rc) 
2194                         return rc;
2195                 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL)
2196                                          | (arg ? CLOCAL : 0));
2197                 
2198                 break;
2199
2200         /*
2201          * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change - mask
2202          * passed in arg for lines of interest (use |'ed TIOCM_RNG/DSR/CD/CTS
2203          * for masking). Caller should use TIOCGICOUNT to see which one it was
2204          */
2205         case TIOCMIWAIT:
2206                 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
2207                 cprev = pCh->icount;     /* note the counters on entry */
2208                 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
2209                 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 4, 
2210                                                 CMD_DCD_REP, CMD_CTS_REP, CMD_DSR_REP, CMD_RI_REP);
2211                 init_waitqueue_entry(&wait, current);
2212                 add_wait_queue(&pCh->delta_msr_wait, &wait);
2213                 set_current_state( TASK_INTERRUPTIBLE );
2214
2215                 serviceOutgoingFifo( pCh->pMyBord );
2216                 for(;;) {
2217                         ip2trace (CHANN, ITRC_IOCTL, 10, 0 );
2218
2219                         schedule();
2220
2221                         ip2trace (CHANN, ITRC_IOCTL, 11, 0 );
2222
2223                         /* see if a signal did it */
2224                         if (signal_pending(current)) {
2225                                 rc = -ERESTARTSYS;
2226                                 break;
2227                         }
2228                         write_lock_irqsave(&pB->read_fifo_spinlock, flags);
2229                         cnow = pCh->icount; /* atomic copy */
2230                         write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
2231                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2232                                 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2233                                 rc =  -EIO; /* no change => rc */
2234                                 break;
2235                         }
2236                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2237                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2238                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
2239                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2240                                 rc =  0;
2241                                 break;
2242                         }
2243                         cprev = cnow;
2244                 }
2245                 set_current_state( TASK_RUNNING );
2246                 remove_wait_queue(&pCh->delta_msr_wait, &wait);
2247
2248                 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 3, 
2249                                                  CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
2250                 if ( ! (pCh->flags      & ASYNC_CHECK_CD)) {
2251                         i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DCD_NREP);
2252                 }
2253                 serviceOutgoingFifo( pCh->pMyBord );
2254                 return rc;
2255                 break;
2256
2257         /*
2258          * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2259          * Return: write counters to the user passed counter struct
2260          * NB: both 1->0 and 0->1 transitions are counted except for RI where
2261          * only 0->1 is counted. The controller is quite capable of counting
2262          * both, but this done to preserve compatibility with the standard
2263          * serial driver.
2264          */
2265         case TIOCGICOUNT:
2266                 ip2trace (CHANN, ITRC_IOCTL, 11, 1, rc );
2267
2268                 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
2269                 cnow = pCh->icount;
2270                 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
2271                 p_cuser = argp;
2272                 rc = put_user(cnow.cts, &p_cuser->cts);
2273                 rc = put_user(cnow.dsr, &p_cuser->dsr);
2274                 rc = put_user(cnow.rng, &p_cuser->rng);
2275                 rc = put_user(cnow.dcd, &p_cuser->dcd);
2276                 rc = put_user(cnow.rx, &p_cuser->rx);
2277                 rc = put_user(cnow.tx, &p_cuser->tx);
2278                 rc = put_user(cnow.frame, &p_cuser->frame);
2279                 rc = put_user(cnow.overrun, &p_cuser->overrun);
2280                 rc = put_user(cnow.parity, &p_cuser->parity);
2281                 rc = put_user(cnow.brk, &p_cuser->brk);
2282                 rc = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
2283                 break;
2284
2285         /*
2286          * The rest are not supported by this driver. By returning -ENOIOCTLCMD they
2287          * will be passed to the line discipline for it to handle.
2288          */
2289         case TIOCSERCONFIG:
2290         case TIOCSERGWILD:
2291         case TIOCSERGETLSR:
2292         case TIOCSERSWILD:
2293         case TIOCSERGSTRUCT:
2294         case TIOCSERGETMULTI:
2295         case TIOCSERSETMULTI:
2296
2297         default:
2298                 ip2trace (CHANN, ITRC_IOCTL, 12, 0 );
2299
2300                 rc =  -ENOIOCTLCMD;
2301                 break;
2302         }
2303
2304         ip2trace (CHANN, ITRC_IOCTL, ITRC_RETURN, 0 );
2305
2306         return rc;
2307 }
2308
2309 /******************************************************************************/
2310 /* Function:   GetSerialInfo()                                                */
2311 /* Parameters: Pointer to channel structure                                   */
2312 /*             Pointer to old termios structure                               */
2313 /* Returns:    Nothing                                                        */
2314 /*                                                                            */
2315 /* Description:                                                               */
2316 /* This is to support the setserial command, and requires processing of the   */
2317 /* standard Linux serial structure.                                           */
2318 /******************************************************************************/
2319 static int
2320 get_serial_info ( i2ChanStrPtr pCh, struct serial_struct __user *retinfo )
2321 {
2322         struct serial_struct tmp;
2323
2324         memset ( &tmp, 0, sizeof(tmp) );
2325         tmp.type = pCh->pMyBord->channelBtypes.bid_value[(pCh->port_index & (IP2_PORTS_PER_BOARD-1))/16];
2326         if (BID_HAS_654(tmp.type)) {
2327                 tmp.type = PORT_16650;
2328         } else {
2329                 tmp.type = PORT_CIRRUS;
2330         }
2331         tmp.line = pCh->port_index;
2332         tmp.port = pCh->pMyBord->i2eBase;
2333         tmp.irq  = ip2config.irq[pCh->port_index/64];
2334         tmp.flags = pCh->flags;
2335         tmp.baud_base = pCh->BaudBase;
2336         tmp.close_delay = pCh->ClosingDelay;
2337         tmp.closing_wait = pCh->ClosingWaitTime;
2338         tmp.custom_divisor = pCh->BaudDivisor;
2339         return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
2340 }
2341
2342 /******************************************************************************/
2343 /* Function:   SetSerialInfo()                                                */
2344 /* Parameters: Pointer to channel structure                                   */
2345 /*             Pointer to old termios structure                               */
2346 /* Returns:    Nothing                                                        */
2347 /*                                                                            */
2348 /* Description:                                                               */
2349 /* This function provides support for setserial, which uses the TIOCSSERIAL   */
2350 /* ioctl. Not all setserial parameters are relevant. If the user attempts to  */
2351 /* change the IRQ, address or type of the port the ioctl fails.               */
2352 /******************************************************************************/
2353 static int
2354 set_serial_info( i2ChanStrPtr pCh, struct serial_struct __user *new_info )
2355 {
2356         struct serial_struct ns;
2357         int   old_flags, old_baud_divisor;
2358
2359         if (copy_from_user(&ns, new_info, sizeof (ns)))
2360                 return -EFAULT;
2361
2362         /*
2363          * We don't allow setserial to change IRQ, board address, type or baud
2364          * base. Also line nunber as such is meaningless but we use it for our
2365          * array index so it is fixed also.
2366          */
2367         if ( (ns.irq        != ip2config.irq[pCh->port_index])
2368             || ((int) ns.port      != ((int) (pCh->pMyBord->i2eBase)))
2369             || (ns.baud_base != pCh->BaudBase)
2370             || (ns.line      != pCh->port_index) ) {
2371                 return -EINVAL;
2372         }
2373
2374         old_flags = pCh->flags;
2375         old_baud_divisor = pCh->BaudDivisor;
2376
2377         if ( !capable(CAP_SYS_ADMIN) ) {
2378                 if ( ( ns.close_delay != pCh->ClosingDelay ) ||
2379                     ( (ns.flags & ~ASYNC_USR_MASK) !=
2380                       (pCh->flags & ~ASYNC_USR_MASK) ) ) {
2381                         return -EPERM;
2382                 }
2383
2384                 pCh->flags = (pCh->flags & ~ASYNC_USR_MASK) |
2385                                (ns.flags & ASYNC_USR_MASK);
2386                 pCh->BaudDivisor = ns.custom_divisor;
2387         } else {
2388                 pCh->flags = (pCh->flags & ~ASYNC_FLAGS) |
2389                                (ns.flags & ASYNC_FLAGS);
2390                 pCh->BaudDivisor = ns.custom_divisor;
2391                 pCh->ClosingDelay = ns.close_delay * HZ/100;
2392                 pCh->ClosingWaitTime = ns.closing_wait * HZ/100;
2393         }
2394
2395         if ( ( (old_flags & ASYNC_SPD_MASK) != (pCh->flags & ASYNC_SPD_MASK) )
2396             || (old_baud_divisor != pCh->BaudDivisor) ) {
2397                 // Invalidate speed and reset parameters
2398                 set_params( pCh, NULL );
2399         }
2400
2401         return 0;
2402 }
2403
2404 /******************************************************************************/
2405 /* Function:   ip2_set_termios()                                              */
2406 /* Parameters: Pointer to tty structure                                       */
2407 /*             Pointer to old termios structure                               */
2408 /* Returns:    Nothing                                                        */
2409 /*                                                                            */
2410 /* Description:                                                               */
2411 /*                                                                            */
2412 /*                                                                            */
2413 /******************************************************************************/
2414 static void
2415 ip2_set_termios( PTTY tty, struct ktermios *old_termios )
2416 {
2417         i2ChanStrPtr pCh = (i2ChanStrPtr)tty->driver_data;
2418
2419 #ifdef IP2DEBUG_IOCTL
2420         printk (KERN_DEBUG "IP2: set termios %p\n", old_termios );
2421 #endif
2422
2423         set_params( pCh, old_termios );
2424 }
2425
2426 /******************************************************************************/
2427 /* Function:   ip2_set_line_discipline()                                      */
2428 /* Parameters: Pointer to tty structure                                       */
2429 /* Returns:    Nothing                                                        */
2430 /*                                                                            */
2431 /* Description:  Does nothing                                                 */
2432 /*                                                                            */
2433 /*                                                                            */
2434 /******************************************************************************/
2435 static void
2436 ip2_set_line_discipline ( PTTY tty )
2437 {
2438 #ifdef IP2DEBUG_IOCTL
2439         printk (KERN_DEBUG "IP2: set line discipline\n" );
2440 #endif
2441
2442         ip2trace (((i2ChanStrPtr)tty->driver_data)->port_index, ITRC_IOCTL, 16, 0 );
2443
2444 }
2445
2446 /******************************************************************************/
2447 /* Function:   SetLine Characteristics()                                      */
2448 /* Parameters: Pointer to channel structure                                   */
2449 /* Returns:    Nothing                                                        */
2450 /*                                                                            */
2451 /* Description:                                                               */
2452 /* This routine is called to update the channel structure with the new line   */
2453 /* characteristics, and send the appropriate commands to the board when they  */
2454 /* change.                                                                    */
2455 /******************************************************************************/
2456 static void
2457 set_params( i2ChanStrPtr pCh, struct ktermios *o_tios )
2458 {
2459         tcflag_t cflag, iflag, lflag;
2460         char stop_char, start_char;
2461         struct ktermios dummy;
2462
2463         lflag = pCh->pTTY->termios->c_lflag;
2464         cflag = pCh->pTTY->termios->c_cflag;
2465         iflag = pCh->pTTY->termios->c_iflag;
2466
2467         if (o_tios == NULL) {
2468                 dummy.c_lflag = ~lflag;
2469                 dummy.c_cflag = ~cflag;
2470                 dummy.c_iflag = ~iflag;
2471                 o_tios = &dummy;
2472         }
2473
2474         {
2475                 switch ( cflag & CBAUD ) {
2476                 case B0:
2477                         i2QueueCommands( PTYPE_BYPASS, pCh, 100, 2, CMD_RTSDN, CMD_DTRDN);
2478                         pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
2479                         i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
2480                         pCh->pTTY->termios->c_cflag |= (CBAUD & o_tios->c_cflag);
2481                         goto service_it;
2482                         break;
2483                 case B38400:
2484                         /*
2485                          * This is the speed that is overloaded with all the other high
2486                          * speeds, depending upon the flag settings.
2487                          */
2488                         if ( ( pCh->flags & ASYNC_SPD_MASK ) == ASYNC_SPD_HI ) {
2489                                 pCh->speed = CBR_57600;
2490                         } else if ( (pCh->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI ) {
2491                                 pCh->speed = CBR_115200;
2492                         } else if ( (pCh->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST ) {
2493                                 pCh->speed = CBR_C1;
2494                         } else {
2495                                 pCh->speed = CBR_38400;
2496                         }
2497                         break;
2498                 case B50:      pCh->speed = CBR_50;      break;
2499                 case B75:      pCh->speed = CBR_75;      break;
2500                 case B110:     pCh->speed = CBR_110;     break;
2501                 case B134:     pCh->speed = CBR_134;     break;
2502                 case B150:     pCh->speed = CBR_150;     break;
2503                 case B200:     pCh->speed = CBR_200;     break;
2504                 case B300:     pCh->speed = CBR_300;     break;
2505                 case B600:     pCh->speed = CBR_600;     break;
2506                 case B1200:    pCh->speed = CBR_1200;    break;
2507                 case B1800:    pCh->speed = CBR_1800;    break;
2508                 case B2400:    pCh->speed = CBR_2400;    break;
2509                 case B4800:    pCh->speed = CBR_4800;    break;
2510                 case B9600:    pCh->speed = CBR_9600;    break;
2511                 case B19200:   pCh->speed = CBR_19200;   break;
2512                 case B57600:   pCh->speed = CBR_57600;   break;
2513                 case B115200:  pCh->speed = CBR_115200;  break;
2514                 case B153600:  pCh->speed = CBR_153600;  break;
2515                 case B230400:  pCh->speed = CBR_230400;  break;
2516                 case B307200:  pCh->speed = CBR_307200;  break;
2517                 case B460800:  pCh->speed = CBR_460800;  break;
2518                 case B921600:  pCh->speed = CBR_921600;  break;
2519                 default:       pCh->speed = CBR_9600;    break;
2520                 }
2521                 if ( pCh->speed == CBR_C1 ) {
2522                         // Process the custom speed parameters.
2523                         int bps = pCh->BaudBase / pCh->BaudDivisor;
2524                         if ( bps == 921600 ) {
2525                                 pCh->speed = CBR_921600;
2526                         } else {
2527                                 bps = bps/10;
2528                                 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_BAUD_DEF1(bps) );
2529                         }
2530                 }
2531                 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_SETBAUD(pCh->speed));
2532                 
2533                 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 2, CMD_DTRUP, CMD_RTSUP);
2534                 pCh->dataSetOut |= (I2_DTR | I2_RTS);
2535         }
2536         if ( (CSTOPB & cflag) ^ (CSTOPB & o_tios->c_cflag)) 
2537         {
2538                 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1, 
2539                         CMD_SETSTOP( ( cflag & CSTOPB ) ? CST_2 : CST_1));
2540         }
2541         if (((PARENB|PARODD) & cflag) ^ ((PARENB|PARODD) & o_tios->c_cflag)) 
2542         {
2543                 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1,
2544                         CMD_SETPAR( 
2545                                 (cflag & PARENB ?  (cflag & PARODD ? CSP_OD : CSP_EV) : CSP_NP)
2546                         )
2547                 );
2548         }
2549         /* byte size and parity */
2550         if ( (CSIZE & cflag)^(CSIZE & o_tios->c_cflag)) 
2551         {
2552                 int datasize;
2553                 switch ( cflag & CSIZE ) {
2554                 case CS5: datasize = CSZ_5; break;
2555                 case CS6: datasize = CSZ_6; break;
2556                 case CS7: datasize = CSZ_7; break;
2557                 case CS8: datasize = CSZ_8; break;
2558                 default:  datasize = CSZ_5; break;      /* as per serial.c */
2559                 }
2560                 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1, CMD_SETBITS(datasize) );
2561         }
2562         /* Process CTS flow control flag setting */
2563         if ( (cflag & CRTSCTS) ) {
2564                 i2QueueCommands(PTYPE_INLINE, pCh, 100,
2565                                                 2, CMD_CTSFL_ENAB, CMD_RTSFL_ENAB);
2566         } else {
2567                 i2QueueCommands(PTYPE_INLINE, pCh, 100,
2568                                                 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
2569         }
2570         //
2571         // Process XON/XOFF flow control flags settings
2572         //
2573         stop_char = STOP_CHAR(pCh->pTTY);
2574         start_char = START_CHAR(pCh->pTTY);
2575
2576         //////////// can't be \000
2577         if (stop_char == __DISABLED_CHAR ) 
2578         {
2579                 stop_char = ~__DISABLED_CHAR; 
2580         }
2581         if (start_char == __DISABLED_CHAR ) 
2582         {
2583                 start_char = ~__DISABLED_CHAR;
2584         }
2585         /////////////////////////////////
2586
2587         if ( o_tios->c_cc[VSTART] != start_char ) 
2588         {
2589                 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DEF_IXON(start_char));
2590                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DEF_OXON(start_char));
2591         }
2592         if ( o_tios->c_cc[VSTOP] != stop_char ) 
2593         {
2594                  i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DEF_IXOFF(stop_char));
2595                  i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DEF_OXOFF(stop_char));
2596         }
2597         if (stop_char == __DISABLED_CHAR ) 
2598         {
2599                 stop_char = ~__DISABLED_CHAR;  //TEST123
2600                 goto no_xoff;
2601         }
2602         if ((iflag & (IXOFF))^(o_tios->c_iflag & (IXOFF))) 
2603         {
2604                 if ( iflag & IXOFF ) {  // Enable XOFF output flow control
2605                         i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_OXON_OPT(COX_XON));
2606                 } else {        // Disable XOFF output flow control
2607 no_xoff:
2608                         i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_OXON_OPT(COX_NONE));
2609                 }
2610         }
2611         if (start_char == __DISABLED_CHAR ) 
2612         {
2613                 goto no_xon;
2614         }
2615         if ((iflag & (IXON|IXANY)) ^ (o_tios->c_iflag & (IXON|IXANY))) 
2616         {
2617                 if ( iflag & IXON ) {
2618                         if ( iflag & IXANY ) { // Enable XON/XANY output flow control
2619                                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_XANY));
2620                         } else { // Enable XON output flow control
2621                                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_XON));
2622                         }
2623                 } else { // Disable XON output flow control
2624 no_xon:
2625                         i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_NONE));
2626                 }
2627         }
2628         if ( (iflag & ISTRIP) ^ ( o_tios->c_iflag & (ISTRIP)) ) 
2629         {
2630                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, 
2631                                 CMD_ISTRIP_OPT((iflag & ISTRIP ? 1 : 0)));
2632         }
2633         if ( (iflag & INPCK) ^ ( o_tios->c_iflag & (INPCK)) ) 
2634         {
2635                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, 
2636                                 CMD_PARCHK((iflag & INPCK) ? CPK_ENAB : CPK_DSAB));
2637         }
2638
2639         if ( (iflag & (IGNBRK|PARMRK|BRKINT|IGNPAR)) 
2640                         ^       ( o_tios->c_iflag & (IGNBRK|PARMRK|BRKINT|IGNPAR)) ) 
2641         {
2642                 char brkrpt = 0;
2643                 char parrpt = 0;
2644
2645                 if ( iflag & IGNBRK ) { /* Ignore breaks altogether */
2646                         /* Ignore breaks altogether */
2647                         i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_BRK_NREP);
2648                 } else {
2649                         if ( iflag & BRKINT ) {
2650                                 if ( iflag & PARMRK ) {
2651                                         brkrpt = 0x0a;  // exception an inline triple
2652                                 } else {
2653                                         brkrpt = 0x1a;  // exception and NULL
2654                                 }
2655                                 brkrpt |= 0x04; // flush input
2656                         } else {
2657                                 if ( iflag & PARMRK ) {
2658                                         brkrpt = 0x0b;  //POSIX triple \0377 \0 \0
2659                                 } else {
2660                                         brkrpt = 0x01;  // Null only
2661                                 }
2662                         }
2663                         i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_BRK_REP(brkrpt));
2664                 } 
2665
2666                 if (iflag & IGNPAR) {
2667                         parrpt = 0x20;
2668                                                                                                         /* would be 2 for not cirrus bug */
2669                                                                                                         /* would be 0x20 cept for cirrus bug */
2670                 } else {
2671                         if ( iflag & PARMRK ) {
2672                                 /*
2673                                  * Replace error characters with 3-byte sequence (\0377,\0,char)
2674                                  */
2675                                 parrpt = 0x04 ;
2676                                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_ISTRIP_OPT((char)0));
2677                         } else {
2678                                 parrpt = 0x03;
2679                         } 
2680                 }
2681                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_SET_ERROR(parrpt));
2682         }
2683         if (cflag & CLOCAL) {
2684                 // Status reporting fails for DCD if this is off
2685                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DCD_NREP);
2686                 pCh->flags &= ~ASYNC_CHECK_CD;
2687         } else {
2688                 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DCD_REP);
2689                 pCh->flags      |= ASYNC_CHECK_CD;
2690         }
2691
2692 service_it:
2693         i2DrainOutput( pCh, 100 );              
2694 }
2695
2696 /******************************************************************************/
2697 /* IPL Device Section                                                         */
2698 /******************************************************************************/
2699
2700 /******************************************************************************/
2701 /* Function:   ip2_ipl_read()                                                  */
2702 /* Parameters: Pointer to device inode                                        */
2703 /*             Pointer to file structure                                      */
2704 /*             Pointer to data                                                */
2705 /*             Number of bytes to read                                        */
2706 /* Returns:    Success or failure                                             */
2707 /*                                                                            */
2708 /* Description:   Ugly                                                        */
2709 /*                                                                            */
2710 /*                                                                            */
2711 /******************************************************************************/
2712
2713 static 
2714 ssize_t
2715 ip2_ipl_read(struct file *pFile, char __user *pData, size_t count, loff_t *off )
2716 {
2717         unsigned int minor = iminor(pFile->f_path.dentry->d_inode);
2718         int rc = 0;
2719
2720 #ifdef IP2DEBUG_IPL
2721         printk (KERN_DEBUG "IP2IPL: read %p, %d bytes\n", pData, count );
2722 #endif
2723
2724         switch( minor ) {
2725         case 0:     // IPL device
2726                 rc = -EINVAL;
2727                 break;
2728         case 1:     // Status dump
2729                 rc = -EINVAL;
2730                 break;
2731         case 2:     // Ping device
2732                 rc = -EINVAL;
2733                 break;
2734         case 3:     // Trace device
2735                 rc = DumpTraceBuffer ( pData, count );
2736                 break;
2737         case 4:     // Trace device
2738                 rc = DumpFifoBuffer ( pData, count );
2739                 break;
2740         default:
2741                 rc = -ENODEV;
2742                 break;
2743         }
2744         return rc;
2745 }
2746
2747 static int
2748 DumpFifoBuffer ( char __user *pData, int count )
2749 {
2750 #ifdef DEBUG_FIFO
2751         int rc;
2752         rc = copy_to_user(pData, DBGBuf, count);
2753
2754         printk(KERN_DEBUG "Last index %d\n", I );
2755
2756         return count;
2757 #endif  /* DEBUG_FIFO */
2758         return 0;
2759 }
2760
2761 static int
2762 DumpTraceBuffer ( char __user *pData, int count )
2763 {
2764 #ifdef IP2DEBUG_TRACE
2765         int rc;
2766         int dumpcount;
2767         int chunk;
2768         int *pIndex = (int __user *)pData;
2769
2770         if ( count < (sizeof(int) * 6) ) {
2771                 return -EIO;
2772         }
2773         rc = put_user(tracewrap, pIndex );
2774         rc = put_user(TRACEMAX, ++pIndex );
2775         rc = put_user(tracestrip, ++pIndex );
2776         rc = put_user(tracestuff, ++pIndex );
2777         pData += sizeof(int) * 6;
2778         count -= sizeof(int) * 6;
2779
2780         dumpcount = tracestuff - tracestrip;
2781         if ( dumpcount < 0 ) {
2782                 dumpcount += TRACEMAX;
2783         }
2784         if ( dumpcount > count ) {
2785                 dumpcount = count;
2786         }
2787         chunk = TRACEMAX - tracestrip;
2788         if ( dumpcount > chunk ) {
2789                 rc = copy_to_user(pData, &tracebuf[tracestrip],
2790                               chunk * sizeof(tracebuf[0]) );
2791                 pData += chunk * sizeof(tracebuf[0]);
2792                 tracestrip = 0;
2793                 chunk = dumpcount - chunk;
2794         } else {
2795                 chunk = dumpcount;
2796         }
2797         rc = copy_to_user(pData, &tracebuf[tracestrip],
2798                       chunk * sizeof(tracebuf[0]) );
2799         tracestrip += chunk;
2800         tracewrap = 0;
2801
2802         rc = put_user(tracestrip, ++pIndex );
2803         rc = put_user(tracestuff, ++pIndex );
2804
2805         return dumpcount;
2806 #else
2807         return 0;
2808 #endif
2809 }
2810
2811 /******************************************************************************/
2812 /* Function:   ip2_ipl_write()                                                 */
2813 /* Parameters:                                                                */
2814 /*             Pointer to file structure                                      */
2815 /*             Pointer to data                                                */
2816 /*             Number of bytes to write                                       */
2817 /* Returns:    Success or failure                                             */
2818 /*                                                                            */
2819 /* Description:                                                               */
2820 /*                                                                            */
2821 /*                                                                            */
2822 /******************************************************************************/
2823 static ssize_t
2824 ip2_ipl_write(struct file *pFile, const char __user *pData, size_t count, loff_t *off)
2825 {
2826 #ifdef IP2DEBUG_IPL
2827         printk (KERN_DEBUG "IP2IPL: write %p, %d bytes\n", pData, count );
2828 #endif
2829         return 0;
2830 }
2831
2832 /******************************************************************************/
2833 /* Function:   ip2_ipl_ioctl()                                                */
2834 /* Parameters: Pointer to device inode                                        */
2835 /*             Pointer to file structure                                      */
2836 /*             Command                                                        */
2837 /*             Argument                                                       */
2838 /* Returns:    Success or failure                                             */
2839 /*                                                                            */
2840 /* Description:                                                               */
2841 /*                                                                            */
2842 /*                                                                            */
2843 /******************************************************************************/
2844 static int
2845 ip2_ipl_ioctl ( struct inode *pInode, struct file *pFile, UINT cmd, ULONG arg )
2846 {
2847         unsigned int iplminor = iminor(pInode);
2848         int rc = 0;
2849         void __user *argp = (void __user *)arg;
2850         ULONG __user *pIndex = argp;
2851         i2eBordStrPtr pB = i2BoardPtrTable[iplminor / 4];
2852         i2ChanStrPtr pCh;
2853
2854 #ifdef IP2DEBUG_IPL
2855         printk (KERN_DEBUG "IP2IPL: ioctl cmd %d, arg %ld\n", cmd, arg );
2856 #endif
2857
2858         switch ( iplminor ) {
2859         case 0:     // IPL device
2860                 rc = -EINVAL;
2861                 break;
2862         case 1:     // Status dump
2863         case 5:
2864         case 9:
2865         case 13:
2866                 switch ( cmd ) {
2867                 case 64:        /* Driver - ip2stat */
2868                         rc = put_user(ip2_tty_driver->refcount, pIndex++ );
2869                         rc = put_user(irq_counter, pIndex++  );
2870                         rc = put_user(bh_counter, pIndex++  );
2871                         break;
2872
2873                 case 65:        /* Board  - ip2stat */
2874                         if ( pB ) {
2875                                 rc = copy_to_user(argp, pB, sizeof(i2eBordStr));
2876                                 rc = put_user(inb(pB->i2eStatus),
2877                                         (ULONG __user *)(arg + (ULONG)(&pB->i2eStatus) - (ULONG)pB ) );
2878                         } else {
2879                                 rc = -ENODEV;
2880                         }
2881                         break;
2882
2883                 default:
2884                         if (cmd < IP2_MAX_PORTS) {
2885                                 pCh = DevTable[cmd];
2886                                 if ( pCh )
2887                                 {
2888                                         rc = copy_to_user(argp, pCh, sizeof(i2ChanStr));
2889                                 } else {
2890                                         rc = -ENODEV;
2891                                 }
2892                         } else {
2893                                 rc = -EINVAL;
2894                         }
2895                 }
2896                 break;
2897
2898         case 2:     // Ping device
2899                 rc = -EINVAL;
2900                 break;
2901         case 3:     // Trace device
2902                 /*
2903                  * akpm: This used to write a whole bunch of function addresses
2904                  * to userspace, which generated lots of put_user() warnings.
2905                  * I killed it all.  Just return "success" and don't do
2906                  * anything.
2907                  */
2908                 if (cmd == 1)
2909                         rc = 0;
2910                 else
2911                         rc = -EINVAL;
2912                 break;
2913
2914         default:
2915                 rc = -ENODEV;
2916                 break;
2917         }
2918         return rc;
2919 }
2920
2921 /******************************************************************************/
2922 /* Function:   ip2_ipl_open()                                                 */
2923 /* Parameters: Pointer to device inode                                        */
2924 /*             Pointer to file structure                                      */
2925 /* Returns:    Success or failure                                             */
2926 /*                                                                            */
2927 /* Description:                                                               */
2928 /*                                                                            */
2929 /*                                                                            */
2930 /******************************************************************************/
2931 static int
2932 ip2_ipl_open( struct inode *pInode, struct file *pFile )
2933 {
2934         unsigned int iplminor = iminor(pInode);
2935         i2eBordStrPtr pB;
2936         i2ChanStrPtr  pCh;
2937
2938 #ifdef IP2DEBUG_IPL
2939         printk (KERN_DEBUG "IP2IPL: open\n" );
2940 #endif
2941
2942         switch(iplminor) {
2943         // These are the IPL devices
2944         case 0:
2945         case 4:
2946         case 8:
2947         case 12:
2948                 break;
2949
2950         // These are the status devices
2951         case 1:
2952         case 5:
2953         case 9:
2954         case 13:
2955                 break;
2956
2957         // These are the debug devices
2958         case 2:
2959         case 6:
2960         case 10:
2961         case 14:
2962                 pB = i2BoardPtrTable[iplminor / 4];
2963                 pCh = (i2ChanStrPtr) pB->i2eChannelPtr;
2964                 break;
2965
2966         // This is the trace device
2967         case 3:
2968                 break;
2969         }
2970         return 0;
2971 }
2972
2973 static int
2974 proc_ip2mem_show(struct seq_file *m, void *v)
2975 {
2976         i2eBordStrPtr  pB;
2977         i2ChanStrPtr  pCh;
2978         PTTY tty;
2979         int i;
2980
2981 #define FMTLINE "%3d: 0x%08x 0x%08x 0%011o 0%011o\n"
2982 #define FMTLIN2 "     0x%04x 0x%04x tx flow 0x%x\n"
2983 #define FMTLIN3 "     0x%04x 0x%04x rc flow\n"
2984
2985         seq_printf(m,"\n");
2986
2987         for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
2988                 pB = i2BoardPtrTable[i];
2989                 if ( pB ) {
2990                         seq_printf(m,"board %d:\n",i);
2991                         seq_printf(m,"\tFifo rem: %d mty: %x outM %x\n",
2992                                 pB->i2eFifoRemains,pB->i2eWaitingForEmptyFifo,pB->i2eOutMailWaiting);
2993                 }
2994         }
2995
2996         seq_printf(m,"#: tty flags, port flags,     cflags,     iflags\n");
2997         for (i=0; i < IP2_MAX_PORTS; i++) {
2998                 pCh = DevTable[i];
2999                 if (pCh) {
3000                         tty = pCh->pTTY;
3001                         if (tty && tty->count) {
3002                                 seq_printf(m,FMTLINE,i,(int)tty->flags,pCh->flags,
3003                                                                         tty->termios->c_cflag,tty->termios->c_iflag);
3004
3005                                 seq_printf(m,FMTLIN2,
3006                                                 pCh->outfl.asof,pCh->outfl.room,pCh->channelNeeds);
3007                                 seq_printf(m,FMTLIN3,pCh->infl.asof,pCh->infl.room);
3008                         }
3009                 }
3010         }
3011         return 0;
3012 }
3013
3014 static int proc_ip2mem_open(struct inode *inode, struct file *file)
3015 {
3016         return single_open(file, proc_ip2mem_show, NULL);
3017 }
3018
3019 static const struct file_operations ip2mem_proc_fops = {
3020         .owner          = THIS_MODULE,
3021         .open           = proc_ip2mem_open,
3022         .read           = seq_read,
3023         .llseek         = seq_lseek,
3024         .release        = single_release,
3025 };
3026
3027 /*
3028  * This is the handler for /proc/tty/driver/ip2
3029  *
3030  * This stretch of code has been largely plagerized from at least three
3031  * different sources including ip2mkdev.c and a couple of other drivers.
3032  * The bugs are all mine.  :-)  =mhw=
3033  */
3034 static int ip2_read_proc(char *page, char **start, off_t off,
3035                                 int count, int *eof, void *data)
3036 {
3037         int     i, j, box;
3038         int     len = 0;
3039         int     boxes = 0;
3040         int     ports = 0;
3041         int     tports = 0;
3042         off_t   begin = 0;
3043         i2eBordStrPtr  pB;
3044
3045         len += sprintf(page, "ip2info: 1.0 driver: %s\n", pcVersion );
3046         len += sprintf(page+len, "Driver: SMajor=%d CMajor=%d IMajor=%d MaxBoards=%d MaxBoxes=%d MaxPorts=%d\n",
3047                         IP2_TTY_MAJOR, IP2_CALLOUT_MAJOR, IP2_IPL_MAJOR,
3048                         IP2_MAX_BOARDS, ABS_MAX_BOXES, ABS_BIGGEST_BOX);
3049
3050         for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
3051                 /* This need to be reset for a board by board count... */
3052                 boxes = 0;
3053                 pB = i2BoardPtrTable[i];
3054                 if( pB ) {
3055                         switch( pB->i2ePom.e.porID & ~POR_ID_RESERVED ) 
3056                         {
3057                         case POR_ID_FIIEX:
3058                                 len += sprintf( page+len, "Board %d: EX ports=", i );
3059                                 for( box = 0; box < ABS_MAX_BOXES; ++box )
3060                                 {
3061                                         ports = 0;
3062
3063                                         if( pB->i2eChannelMap[box] != 0 ) ++boxes;
3064                                         for( j = 0; j < ABS_BIGGEST_BOX; ++j ) 
3065                                         {
3066                                                 if( pB->i2eChannelMap[box] & 1<< j ) {
3067                                                         ++ports;
3068                                                 }
3069                                         }
3070                                         len += sprintf( page+len, "%d,", ports );
3071                                         tports += ports;
3072                                 }
3073
3074                                 --len;  /* Backup over that last comma */
3075
3076                                 len += sprintf( page+len, " boxes=%d width=%d", boxes, pB->i2eDataWidth16 ? 16 : 8 );
3077                                 break;
3078
3079                         case POR_ID_II_4:
3080                                 len += sprintf(page+len, "Board %d: ISA-4 ports=4 boxes=1", i );
3081                                 tports = ports = 4;
3082                                 break;
3083
3084                         case POR_ID_II_8:
3085                                 len += sprintf(page+len, "Board %d: ISA-8-std ports=8 boxes=1", i );
3086                                 tports = ports = 8;
3087                                 break;
3088
3089                         case POR_ID_II_8R:
3090                                 len += sprintf(page+len, "Board %d: ISA-8-RJ11 ports=8 boxes=1", i );
3091                                 tports = ports = 8;
3092                                 break;
3093
3094                         default:
3095                                 len += sprintf(page+len, "Board %d: unknown", i );
3096                                 /* Don't try and probe for minor numbers */
3097                                 tports = ports = 0;
3098                         }
3099
3100                 } else {
3101                         /* Don't try and probe for minor numbers */
3102                         len += sprintf(page+len, "Board %d: vacant", i );
3103                         tports = ports = 0;
3104                 }
3105
3106                 if( tports ) {
3107                         len += sprintf(page+len, " minors=" );
3108
3109                         for ( box = 0; box < ABS_MAX_BOXES; ++box )
3110                         {
3111                                 for ( j = 0; j < ABS_BIGGEST_BOX; ++j )
3112                                 {
3113                                         if ( pB->i2eChannelMap[box] & (1 << j) )
3114                                         {
3115                                                 len += sprintf (page+len,"%d,",
3116                                                         j + ABS_BIGGEST_BOX *
3117                                                         (box+i*ABS_MAX_BOXES));
3118                                         }
3119                                 }
3120                         }
3121
3122                         page[ len - 1 ] = '\n'; /* Overwrite that last comma */
3123                 } else {
3124                         len += sprintf (page+len,"\n" );
3125                 }
3126
3127                 if (len+begin > off+count)
3128                         break;
3129                 if (len+begin < off) {
3130                         begin += len;
3131                         len = 0;
3132                 }
3133         }
3134
3135         if (i >= IP2_MAX_BOARDS)
3136                 *eof = 1;
3137         if (off >= len+begin)
3138                 return 0;
3139
3140         *start = page + (off-begin);
3141         return ((count < begin+len-off) ? count : begin+len-off);
3142  }
3143  
3144 /******************************************************************************/
3145 /* Function:   ip2trace()                                                     */
3146 /* Parameters: Value to add to trace buffer                                   */
3147 /* Returns:    Nothing                                                        */
3148 /*                                                                            */
3149 /* Description:                                                               */
3150 /*                                                                            */
3151 /*                                                                            */
3152 /******************************************************************************/
3153 #ifdef IP2DEBUG_TRACE
3154 void
3155 ip2trace (unsigned short pn, unsigned char cat, unsigned char label, unsigned long codes, ...)
3156 {
3157         long flags;
3158         unsigned long *pCode = &codes;
3159         union ip2breadcrumb bc;
3160         i2ChanStrPtr  pCh;
3161
3162
3163         tracebuf[tracestuff++] = jiffies;
3164         if ( tracestuff == TRACEMAX ) {
3165                 tracestuff = 0;
3166         }
3167         if ( tracestuff == tracestrip ) {
3168                 if ( ++tracestrip == TRACEMAX ) {
3169                         tracestrip = 0;
3170                 }
3171                 ++tracewrap;
3172         }
3173
3174         bc.hdr.port  = 0xff & pn;
3175         bc.hdr.cat   = cat;
3176         bc.hdr.codes = (unsigned char)( codes & 0xff );
3177         bc.hdr.label = label;
3178         tracebuf[tracestuff++] = bc.value;
3179
3180         for (;;) {
3181                 if ( tracestuff == TRACEMAX ) {
3182                         tracestuff = 0;
3183                 }
3184                 if ( tracestuff == tracestrip ) {
3185                         if ( ++tracestrip == TRACEMAX ) {
3186                                 tracestrip = 0;
3187                         }
3188                         ++tracewrap;
3189                 }
3190
3191                 if ( !codes-- )
3192                         break;
3193
3194                 tracebuf[tracestuff++] = *++pCode;
3195         }
3196 }
3197 #endif
3198
3199
3200 MODULE_LICENSE("GPL");
3201
3202 static struct pci_device_id ip2main_pci_tbl[] __devinitdata = {
3203         { PCI_DEVICE(PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_IP2EX) },
3204         { }
3205 };
3206
3207 MODULE_DEVICE_TABLE(pci, ip2main_pci_tbl);