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