ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / rio / rio_linux.c
1
2 /* rio_linux.c -- Linux driver for the Specialix RIO series cards. 
3  *
4  *
5  *   (C) 1999 R.E.Wolff@BitWizard.nl
6  *
7  * Specialix pays for the development and support of this driver.
8  * Please DO contact support@specialix.co.uk if you require
9  * support. But please read the documentation (rio.txt) first.
10  *
11  *
12  *
13  *      This program is free software; you can redistribute it and/or
14  *      modify it under the terms of the GNU General Public License as
15  *      published by the Free Software Foundation; either version 2 of
16  *      the License, or (at your option) any later version.
17  *
18  *      This program is distributed in the hope that it will be
19  *      useful, but WITHOUT ANY WARRANTY; without even the implied
20  *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21  *      PURPOSE.  See the GNU General Public License for more details.
22  *
23  *      You should have received a copy of the GNU General Public
24  *      License along with this program; if not, write to the Free
25  *      Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
26  *      USA.
27  *
28  * Revision history:
29  * $Log: rio.c,v $
30  * Revision 1.1  1999/07/11 10:13:54  wolff
31  * Initial revision
32  *
33  * */
34
35
36 #define RCS_ID "$Id: rio.c,v 1.1 1999/07/11 10:13:54 wolff Exp wolff $"
37 #define RCS_REV "$Revision: 1.1 $"
38
39
40 #include <linux/module.h>
41 #include <linux/config.h> 
42 #include <linux/kdev_t.h>
43 #include <asm/io.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/ioport.h>
47 #include <linux/interrupt.h>
48 #include <linux/errno.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
51 #include <linux/mm.h>
52 #include <linux/serial.h>
53 #include <linux/fcntl.h>
54 #include <linux/major.h>
55 #include <linux/delay.h>
56 #include <linux/pci.h>
57 #include <linux/slab.h>
58 #include <linux/miscdevice.h>
59 #include <linux/init.h>
60
61 #include <linux/generic_serial.h>
62 #include <asm/uaccess.h>
63
64 #if BITS_PER_LONG != 32
65 #  error FIXME: this driver only works on 32-bit platforms
66 #endif
67
68 #include "linux_compat.h"
69 #include "typdef.h"
70 #include "pkt.h"
71 #include "daemon.h"
72 #include "rio.h"
73 #include "riospace.h"
74 #include "top.h"
75 #include "cmdpkt.h"
76 #include "map.h"
77 #include "riotypes.h"
78 #include "rup.h"
79 #include "port.h"
80 #include "riodrvr.h"
81 #include "rioinfo.h"
82 #include "func.h"
83 #include "errors.h"
84 #include "pci.h"
85
86 #include "parmmap.h"
87 #include "unixrup.h"
88 #include "board.h"
89 #include "host.h"
90 #include "error.h"
91 #include "phb.h"
92 #include "link.h"
93 #include "cmdblk.h"
94 #include "route.h"
95 #include "control.h"
96 #include "cirrus.h"
97 #include "rioioctl.h"
98 #include "param.h"
99 #include "list.h"
100 #include "sam.h"
101 #include "protsts.h"
102 #include "rioboard.h"
103
104
105 #include "rio_linux.h"
106
107 /* I don't think that this driver can handle more than 512 ports on
108 one machine.  Specialix specifies max 4 boards in one machine. I don't
109 know why. If you want to try anyway you'll have to increase the number
110 of boards in rio.h.  You'll have to allocate more majors if you need
111 more than 512 ports.... */
112
113 #ifndef RIO_NORMAL_MAJOR0
114 /* This allows overriding on the compiler commandline, or in a "major.h" 
115    include or something like that */
116 #define RIO_NORMAL_MAJOR0  154
117 #define RIO_NORMAL_MAJOR1  156
118 #endif
119
120 #ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
121 #define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
122 #endif
123
124 #ifndef RIO_WINDOW_LEN 
125 #define RIO_WINDOW_LEN 0x10000
126 #endif
127
128
129 /* Configurable options: 
130    (Don't be too sure that it'll work if you toggle them) */
131
132 /* Am I paranoid or not ? ;-) */
133 #undef RIO_PARANOIA_CHECK
134
135
136 /* 20 -> 2000 per second. The card should rate-limit interrupts at 1000
137    Hz, but it is user configurable. I don't recommend going above 1000
138    Hz. The interrupt ratelimit might trigger if the interrupt is
139    shared with a very active other device. 
140    undef this if you want to disable the check....
141 */
142 #define IRQ_RATE_LIMIT 200
143
144 #if 0
145 /* Not implemented */
146 /* 
147  * The following defines are mostly for testing purposes. But if you need
148  * some nice reporting in your syslog, you can define them also.
149  */
150 #define RIO_REPORT_FIFO
151 #define RIO_REPORT_OVERRUN
152 #endif 
153
154
155 /* These constants are derived from SCO Source */
156 static struct Conf
157 RIOConf =
158 {
159   /* locator */         "RIO Config here",
160   /* startuptime */     HZ*2,           /* how long to wait for card to run */
161   /* slowcook */        0,              /* TRUE -> always use line disc. */
162   /* intrpolltime */    1,              /* The frequency of OUR polls */
163   /* breakinterval */   25,             /* x10 mS XXX: units seem to be 1ms not 10! -- REW*/
164   /* timer */           10,             /* mS */
165   /* RtaLoadBase */     0x7000,
166   /* HostLoadBase */    0x7C00,
167   /* XpHz */            5,              /* number of Xprint hits per second */
168   /* XpCps */           120,            /* Xprint characters per second */
169   /* XpOn */            "\033d#",       /* start Xprint for a wyse 60 */
170   /* XpOff */           "\024",         /* end Xprint for a wyse 60 */
171   /* MaxXpCps */        2000,           /* highest Xprint speed */
172   /* MinXpCps */        10,             /* slowest Xprint speed */
173   /* SpinCmds */        1,              /* non-zero for mega fast boots */
174   /* First Addr */      0x0A0000,       /* First address to look at */
175   /* Last Addr */       0xFF0000,       /* Last address looked at */
176   /* BufferSize */      1024,           /* Bytes per port of buffering */
177   /* LowWater */        256,            /* how much data left before wakeup */
178   /* LineLength */      80,             /* how wide is the console? */
179   /* CmdTimeout */      HZ,             /* how long a close command may take */
180 };
181
182
183
184
185 /* Function prototypes */
186
187 static void rio_disable_tx_interrupts (void * ptr); 
188 static void rio_enable_tx_interrupts (void * ptr); 
189 static void rio_disable_rx_interrupts (void * ptr); 
190 static void rio_enable_rx_interrupts (void * ptr); 
191 static int  rio_get_CD (void * ptr); 
192 static void rio_shutdown_port (void * ptr);
193 static int  rio_set_real_termios (void  *ptr);
194 static void rio_hungup (void  *ptr);
195 static void rio_close (void  *ptr);
196 static int rio_chars_in_buffer (void * ptr);
197 static int rio_fw_ioctl (struct inode *inode, struct file *filp,
198                          unsigned int cmd, unsigned long arg);
199 static int rio_init_drivers(void);
200
201 void my_hd (void *addr, int len);
202
203 static struct tty_driver *rio_driver, *rio_driver2;
204
205 /* The name "p" is a bit non-descript. But that's what the rio-lynxos
206 sources use all over the place. */
207 struct rio_info *p;
208
209 /* struct rio_board boards[RIO_HOSTS]; */
210 struct rio_port *rio_ports;
211
212 int rio_initialized;
213 int rio_nports;
214 int rio_debug;
215
216
217 /* You can have the driver poll your card. 
218     - Set rio_poll to 1 to poll every timer tick (10ms on Intel). 
219       This is used when the card cannot use an interrupt for some reason.
220 */
221 int rio_poll = 1;
222
223
224 /* These are the only open spaces in my computer. Yours may have more
225    or less.... */
226 int rio_probe_addrs[]= {0xc0000, 0xd0000, 0xe0000};
227
228 #define NR_RIO_ADDRS (sizeof(rio_probe_addrs)/sizeof (int))
229
230
231 /* Set the mask to all-ones. This alas, only supports 32 interrupts. 
232    Some architectures may need more. -- Changed to LONG to
233    support up to 64 bits on 64bit architectures. -- REW 20/06/99 */
234 long rio_irqmask = -1;
235
236 MODULE_AUTHOR("Rogier Wolff <R.E.Wolff@bitwizard.nl>, Patrick van de Lageweg <patrick@bitwizard.nl>");
237 MODULE_DESCRIPTION("RIO driver");
238 MODULE_LICENSE("GPL");
239 MODULE_PARM(rio_poll, "i");
240 MODULE_PARM(rio_debug, "i");
241 MODULE_PARM(rio_irqmask, "i");
242
243 static struct real_driver rio_real_driver = {
244   rio_disable_tx_interrupts,
245   rio_enable_tx_interrupts,
246   rio_disable_rx_interrupts,
247   rio_enable_rx_interrupts,
248   rio_get_CD,
249   rio_shutdown_port, 
250   rio_set_real_termios, 
251   rio_chars_in_buffer,
252   rio_close,
253   rio_hungup,
254   NULL
255 };
256
257 /* 
258  *  Firmware loader driver specific routines
259  *
260  */
261
262 static struct file_operations rio_fw_fops = {
263         .owner          = THIS_MODULE,
264         .ioctl          = rio_fw_ioctl,
265 };
266
267 struct miscdevice rio_fw_device = {
268         RIOCTL_MISC_MINOR, "rioctl", &rio_fw_fops
269 };
270
271
272
273
274
275 #ifdef RIO_PARANOIA_CHECK
276
277 /* This doesn't work. Who's paranoid around here? Not me! */
278
279 static inline int rio_paranoia_check(struct rio_port const * port,
280                                     char *name, const char *routine)
281 {
282
283   static const char *badmagic =
284     KERN_ERR "rio: Warning: bad rio port magic number for device %s in %s\n";
285   static const char *badinfo =
286     KERN_ERR "rio: Warning: null rio port for device %s in %s\n";
287  
288   if (!port) {
289     printk (badinfo, name, routine);
290     return 1;
291   }
292   if (port->magic != RIO_MAGIC) {
293     printk (badmagic, name, routine);
294     return 1;
295   }
296
297   return 0;
298 }
299 #else
300 #define rio_paranoia_check(a,b,c) 0
301 #endif
302
303
304 #ifdef DEBUG
305 void my_hd (void *ad, int len)
306 {
307   int i, j, ch;
308   unsigned char *addr = ad;
309   
310   for (i=0;i<len;i+=16) {
311     rio_dprintk (RIO_DEBUG_PARAM, "%08x ", (int) addr+i);
312     for (j=0;j<16;j++) {
313       rio_dprintk (RIO_DEBUG_PARAM, "%02x %s", addr[j+i], (j==7)?" ":"");
314     }
315     for (j=0;j<16;j++) {
316       ch = addr[j+i];
317       rio_dprintk (RIO_DEBUG_PARAM, "%c", (ch < 0x20)?'.':((ch > 0x7f)?'.':ch));
318     }
319     rio_dprintk (RIO_DEBUG_PARAM, "\n");
320   }
321 }
322 #else
323 #define my_hd(ad,len) do{/* nothing*/ } while (0)
324 #endif
325
326
327 /* Delay a number of jiffies, allowing a signal to interrupt */ 
328 int RIODelay (struct Port *PortP, int njiffies)
329 {
330   func_enter ();
331
332   rio_dprintk (RIO_DEBUG_DELAY, "delaying %d jiffies\n", njiffies);  
333   current->state = TASK_INTERRUPTIBLE;
334   schedule_timeout(njiffies);
335   func_exit();
336
337   if (signal_pending(current))
338     return RIO_FAIL;
339   else
340     return !RIO_FAIL;
341 }
342
343
344 /* Delay a number of jiffies, disallowing a signal to interrupt */ 
345 int RIODelay_ni (struct Port *PortP, int njiffies)
346 {
347   func_enter ();
348
349   rio_dprintk (RIO_DEBUG_DELAY, "delaying %d jiffies (ni)\n", njiffies);  
350   current->state = TASK_UNINTERRUPTIBLE;
351   schedule_timeout(njiffies);
352   func_exit();
353   return !RIO_FAIL;
354 }
355
356
357 int rio_minor(struct tty_struct *tty)
358 {
359         return tty->index + (tty->driver == rio_driver) ? 0 : 256;
360 }
361
362
363 int rio_ismodem(struct tty_struct *tty)
364 {
365         return 1;
366 }
367
368
369 void rio_udelay (int usecs)
370 {
371   udelay (usecs);
372 }
373
374 static int rio_set_real_termios (void *ptr)
375 {
376   int rv, modem;
377   struct tty_struct *tty;
378   func_enter();
379
380   tty = ((struct Port *)ptr)->gs.tty;
381
382   modem = rio_ismodem(tty);
383
384   rv = RIOParam( (struct Port *) ptr, CONFIG, modem, 1);
385
386   func_exit ();
387
388   return rv;
389 }
390
391
392 void rio_reset_interrupt (struct Host *HostP)
393 {
394   func_enter();
395
396   switch( HostP->Type ) {
397   case RIO_AT:
398   case RIO_MCA:
399   case RIO_PCI:
400     WBYTE(HostP->ResetInt , 0xff);
401   }
402
403   func_exit();
404 }
405
406
407 static irqreturn_t rio_interrupt (int irq, void *ptr, struct pt_regs *regs)
408 {
409   struct Host *HostP;
410   func_enter ();
411
412   HostP = (struct Host*)ptr; /* &p->RIOHosts[(long)ptr]; */
413   rio_dprintk (RIO_DEBUG_IFLOW, "rio: enter rio_interrupt (%d/%d)\n", 
414                irq, HostP->Ivec); 
415
416   /* AAargh! The order in which to do these things is essential and
417      not trivial. 
418      
419      - Rate limit goes before "recursive". Otherwise a series of
420        recursive calls will hang the machine in the interrupt routine. 
421
422      - hardware twiddling goes before "recursive". Otherwise when we
423        poll the card, and a recursive interrupt happens, we won't
424        ack the card, so it might keep on interrupting us. (especially
425        level sensitive interrupt systems like PCI).
426
427      - Rate limit goes before hardware twiddling. Otherwise we won't
428        catch a card that has gone bonkers.
429
430      - The "initialized" test goes after the hardware twiddling. Otherwise
431        the card will stick us in the interrupt routine again.
432
433      - The initialized test goes before recursive. 
434   */
435
436
437
438 #ifdef IRQ_RATE_LIMIT
439   /* Aaargh! I'm ashamed. This costs more lines-of-code than the
440      actual interrupt routine!. (Well, used to when I wrote that comment) */
441   {
442     static int lastjif;
443     static int nintr=0;
444
445     if (lastjif == jiffies) {
446       if (++nintr > IRQ_RATE_LIMIT) {
447         free_irq (HostP->Ivec, ptr);
448         printk (KERN_ERR "rio: Too many interrupts. Turning off interrupt %d.\n", 
449                 HostP->Ivec);
450       }
451     } else {
452       lastjif = jiffies;
453       nintr = 0;
454     }
455   }
456 #endif
457   rio_dprintk (RIO_DEBUG_IFLOW, "rio: We've have noticed the interrupt\n"); 
458   if (HostP->Ivec == irq) {
459     /* Tell the card we've noticed the interrupt. */
460     rio_reset_interrupt (HostP);
461   }
462
463   if ((HostP->Flags & RUN_STATE) != RC_RUNNING)
464         return IRQ_HANDLED;
465
466   if (test_and_set_bit (RIO_BOARD_INTR_LOCK, &HostP->locks)) {
467     printk (KERN_ERR "Recursive interrupt! (host %d/irq%d)\n", 
468             (int) ptr, HostP->Ivec);
469     return IRQ_HANDLED;
470   }
471
472   RIOServiceHost(p, HostP, irq);
473
474   rio_dprintk ( RIO_DEBUG_IFLOW, "riointr() doing host %d type %d\n", 
475                 (int) ptr, HostP->Type);
476
477   clear_bit (RIO_BOARD_INTR_LOCK, &HostP->locks);
478   rio_dprintk (RIO_DEBUG_IFLOW, "rio: exit rio_interrupt (%d/%d)\n", 
479                irq, HostP->Ivec); 
480   func_exit ();
481   return IRQ_HANDLED;
482 }
483
484
485 static void rio_pollfunc (unsigned long data)
486 {
487   func_enter ();
488
489   rio_interrupt (0, &p->RIOHosts[data], NULL);
490   p->RIOHosts[data].timer.expires = jiffies + rio_poll;
491   add_timer (&p->RIOHosts[data].timer);
492
493   func_exit ();
494 }
495
496
497 /* ********************************************************************** *
498  *                Here are the routines that actually                     *
499  *              interface with the generic_serial driver                  *
500  * ********************************************************************** */
501
502 /* Ehhm. I don't know how to fiddle with interrupts on the Specialix 
503    cards. ....   Hmm. Ok I figured it out. You don't.  -- REW */
504
505 static void rio_disable_tx_interrupts (void * ptr) 
506 {
507   func_enter();
508
509   /*  port->gs.flags &= ~GS_TX_INTEN; */
510
511   func_exit();
512 }
513
514
515 static void rio_enable_tx_interrupts (void * ptr) 
516 {
517   struct Port *PortP = ptr;
518   /* int hn; */
519
520   func_enter();
521
522   /* hn = PortP->HostP - p->RIOHosts;
523
524      rio_dprintk (RIO_DEBUG_TTY, "Pushing host %d\n", hn);
525      rio_interrupt (-1,(void *) hn, NULL); */
526
527   RIOTxEnable((char *) PortP);
528
529   /* 
530    * In general we cannot count on "tx empty" interrupts, although
531    * the interrupt routine seems to be able to tell the difference. 
532    */
533   PortP->gs.flags &= ~GS_TX_INTEN;
534
535   func_exit();
536 }
537
538
539 static void rio_disable_rx_interrupts (void * ptr) 
540 {
541   func_enter();
542   func_exit();
543 }
544
545 static void rio_enable_rx_interrupts (void * ptr) 
546 {
547   /*  struct rio_port *port = ptr; */
548   func_enter();
549   func_exit();
550 }
551
552
553 /* Jeez. Isn't this simple?  */
554 static int rio_get_CD (void * ptr) 
555 {
556   struct Port *PortP = ptr;
557   int rv;
558
559   func_enter();
560   rv = (PortP->ModemState & MSVR1_CD) != 0;
561
562   rio_dprintk (RIO_DEBUG_INIT, "Getting CD status: %d\n", rv);
563   
564   func_exit();  
565   return rv;
566 }
567
568
569 /* Jeez. Isn't this simple? Actually, we can sync with the actual port
570    by just pushing stuff into the queue going to the port... */
571 static int rio_chars_in_buffer (void * ptr) 
572 {
573   func_enter();
574
575   func_exit();  
576   return 0;
577 }
578
579
580 /* Nothing special here... */
581 static void rio_shutdown_port (void * ptr) 
582 {
583   struct Port *PortP;
584
585   func_enter();
586
587   PortP = (struct Port *)ptr;
588   PortP->gs.tty = NULL;
589 #if 0
590   port->gs.flags &= ~ GS_ACTIVE;
591   if (!port->gs.tty) {
592     rio_dprintk (RIO_DBUG_TTY, "No tty.\n");
593     return;
594   }
595   if (!port->gs.tty->termios) {
596     rio_dprintk (RIO_DEBUG_TTY, "No termios.\n");
597     return;
598   }
599   if (port->gs.tty->termios->c_cflag & HUPCL) {
600     rio_setsignals (port, 0, 0);
601   }
602 #endif
603
604   func_exit();
605 }
606
607
608 /* I haven't the foggiest why the decrement use count has to happen
609    here. The whole linux serial drivers stuff needs to be redesigned.
610    My guess is that this is a hack to minimize the impact of a bug
611    elsewhere. Thinking about it some more. (try it sometime) Try
612    running minicom on a serial port that is driven by a modularized
613    driver. Have the modem hangup. Then remove the driver module. Then
614    exit minicom.  I expect an "oops".  -- REW */
615 static void rio_hungup (void *ptr)
616 {
617   struct Port *PortP;
618
619   func_enter();
620   
621   PortP = (struct Port *)ptr;
622   PortP->gs.tty = NULL;
623
624   func_exit ();
625 }
626
627
628 /* The standard serial_close would become shorter if you'd wrap it like
629    this. 
630    rs_close (...){save_flags;cli;real_close();dec_use_count;restore_flags;}
631  */
632 static void rio_close (void *ptr)
633 {
634   struct Port *PortP;
635
636   func_enter ();
637
638   PortP = (struct Port *)ptr;
639
640   riotclose (ptr);
641
642   if(PortP->gs.count) {
643     printk (KERN_ERR "WARNING port count:%d\n", PortP->gs.count);
644     PortP->gs.count = 0; 
645   }                
646
647   PortP->gs.tty = NULL;
648   func_exit ();
649 }
650
651
652
653 static int rio_fw_ioctl (struct inode *inode, struct file *filp,
654                          unsigned int cmd, unsigned long arg)
655 {
656   int rc = 0;
657   func_enter();
658
659   /* The "dev" argument isn't used. */
660   rc = riocontrol (p, 0, cmd, (void *)arg, capable(CAP_SYS_ADMIN));
661
662   func_exit ();
663   return rc;
664 }
665
666 extern int RIOShortCommand(struct rio_info *p, struct Port *PortP,
667                int command, int len, int arg);
668
669 static int rio_ioctl (struct tty_struct * tty, struct file * filp, 
670                      unsigned int cmd, unsigned long arg)
671 {
672   int rc;
673   struct Port *PortP;
674   int ival;
675
676   func_enter();
677
678   PortP = (struct Port *)tty->driver_data;
679
680   rc  = 0;
681   switch (cmd) {
682 #if 0
683   case TIOCGSOFTCAR:
684     rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
685                   (unsigned int *) arg);
686     break;
687 #endif
688   case TIOCSSOFTCAR:
689     if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
690       tty->termios->c_cflag =
691         (tty->termios->c_cflag & ~CLOCAL) |
692         (ival ? CLOCAL : 0);
693     }
694     break;
695   case TIOCGSERIAL:
696     if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
697                           sizeof(struct serial_struct))) == 0)
698       rc = gs_getserial(&PortP->gs, (struct serial_struct *) arg);
699     break;
700   case TCSBRK:
701     if ( PortP->State & RIO_DELETED ) {
702       rio_dprintk (RIO_DEBUG_TTY, "BREAK on deleted RTA\n");
703       rc = -EIO;      
704     } else {
705       if (RIOShortCommand(p, PortP, SBREAK, 2, 250) == RIO_FAIL) {
706          rio_dprintk (RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
707          rc = -EIO;
708       }          
709     }
710     break;
711   case TCSBRKP:
712     if ( PortP->State & RIO_DELETED ) {
713       rio_dprintk (RIO_DEBUG_TTY, "BREAK on deleted RTA\n");
714       rc = -EIO;      
715     } else {
716       int l;
717       l = arg?arg*100:250;
718       if (l > 255) l = 255;
719       if (RIOShortCommand(p, PortP, SBREAK, 2, arg?arg*100:250) == RIO_FAIL) {
720          rio_dprintk (RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
721          rc = -EIO;
722       }          
723     }
724     break;
725   case TIOCSSERIAL:
726     if ((rc = verify_area(VERIFY_READ, (void *) arg,
727                           sizeof(struct serial_struct))) == 0)
728       rc = gs_setserial(&PortP->gs, (struct serial_struct *) arg);
729     break;
730 #if 0
731   /*
732    * note: these IOCTLs no longer reach here.  Use
733    * tiocmset/tiocmget driver methods instead.  The
734    * #if 0 disablement predates this comment.
735    */
736   case TIOCMGET:
737     if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
738                           sizeof(unsigned int))) == 0) {
739       ival = rio_getsignals(port);
740       put_user(ival, (unsigned int *) arg);
741     }
742     break;
743   case TIOCMBIS:
744     if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
745       rio_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
746                            ((ival & TIOCM_RTS) ? 1 : -1));
747     }
748     break;
749   case TIOCMBIC:
750     if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
751       rio_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
752                            ((ival & TIOCM_RTS) ? 0 : -1));
753     }
754     break;
755   case TIOCMSET:
756     if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
757       rio_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
758                            ((ival & TIOCM_RTS) ? 1 : 0));
759     }
760     break;
761 #endif
762   default:
763     rc = -ENOIOCTLCMD;
764     break;
765   }
766   func_exit();
767   return rc;
768 }
769
770
771 /* The throttle/unthrottle scheme for the Specialix card is different
772  * from other drivers and deserves some explanation. 
773  * The Specialix hardware takes care of XON/XOFF
774  * and CTS/RTS flow control itself.  This means that all we have to
775  * do when signalled by the upper tty layer to throttle/unthrottle is
776  * to make a note of it here.  When we come to read characters from the
777  * rx buffers on the card (rio_receive_chars()) we look to see if the
778  * upper layer can accept more (as noted here in rio_rx_throt[]). 
779  * If it can't we simply don't remove chars from the cards buffer. 
780  * When the tty layer can accept chars, we again note that here and when
781  * rio_receive_chars() is called it will remove them from the cards buffer.
782  * The card will notice that a ports buffer has drained below some low
783  * water mark and will unflow control the line itself, using whatever
784  * flow control scheme is in use for that port. -- Simon Allen
785  */
786
787 static void rio_throttle (struct tty_struct * tty)
788 {
789   struct Port *port = (struct Port *)tty->driver_data;
790   
791   func_enter();
792   /* If the port is using any type of input flow
793    * control then throttle the port.
794    */
795
796   if((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) ) {
797     port->State |= RIO_THROTTLE_RX;
798   }
799
800   func_exit();
801 }
802
803
804 static void rio_unthrottle (struct tty_struct * tty)
805 {
806   struct Port *port = (struct Port *)tty->driver_data;
807
808   func_enter();
809   /* Always unthrottle even if flow control is not enabled on
810    * this port in case we disabled flow control while the port
811    * was throttled
812    */
813
814   port->State &= ~RIO_THROTTLE_RX;
815
816   func_exit();
817   return;
818 }
819
820
821
822
823
824 /* ********************************************************************** *
825  *                    Here are the initialization routines.               *
826  * ********************************************************************** */
827
828
829 struct vpd_prom *get_VPD_PROM (struct Host *hp)
830 {
831   static struct vpd_prom vpdp;
832   char *p;
833   int i;
834
835   func_enter();
836   rio_dprintk (RIO_DEBUG_PROBE, "Going to verify vpd prom at %p.\n", 
837               hp->Caddr + RIO_VPD_ROM);
838
839   p = (char *) &vpdp;
840   for (i=0;i< sizeof (struct vpd_prom);i++)
841     *p++ = readb (hp->Caddr+RIO_VPD_ROM + i*2);
842       /* read_rio_byte (hp, RIO_VPD_ROM + i*2); */
843
844   /* Terminate the identifier string. 
845      *** requires one extra byte in struct vpd_prom *** */
846   *p++=0; 
847
848   if (rio_debug & RIO_DEBUG_PROBE)
849     my_hd ((char *)&vpdp, 0x20);
850   
851   func_exit();
852
853   return &vpdp;
854 }
855
856 static struct tty_operations rio_ops = {
857         .open  = riotopen,
858         .close = gs_close,
859         .write = gs_write,
860         .put_char = gs_put_char,
861         .flush_chars = gs_flush_chars,
862         .write_room = gs_write_room,
863         .chars_in_buffer = gs_chars_in_buffer,
864         .flush_buffer = gs_flush_buffer,
865         .ioctl = rio_ioctl,
866         .throttle = rio_throttle,
867         .unthrottle = rio_unthrottle,
868         .set_termios = gs_set_termios,
869         .stop = gs_stop,
870         .start = gs_start,
871         .hangup = gs_hangup,
872 };
873
874 static int rio_init_drivers(void)
875 {
876         int error = -ENOMEM;
877
878         rio_driver = alloc_tty_driver(256);
879         if (!rio_driver)
880                 goto out;
881         rio_driver2 = alloc_tty_driver(256);
882         if (!rio_driver2)
883                 goto out1;
884
885         func_enter();
886
887         rio_driver->owner = THIS_MODULE;
888         rio_driver->driver_name = "specialix_rio";
889         rio_driver->name = "ttySR";
890         rio_driver->major = RIO_NORMAL_MAJOR0;
891         rio_driver->type = TTY_DRIVER_TYPE_SERIAL;
892         rio_driver->subtype = SERIAL_TYPE_NORMAL;
893         rio_driver->init_termios = tty_std_termios;
894         rio_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
895         rio_driver->flags = TTY_DRIVER_REAL_RAW;
896         tty_set_operations(rio_driver, &rio_ops);
897
898         rio_driver2->owner = THIS_MODULE;
899         rio_driver2->driver_name = "specialix_rio";
900         rio_driver2->name = "ttySR";
901         rio_driver2->major = RIO_NORMAL_MAJOR1;
902         rio_driver2->type = TTY_DRIVER_TYPE_SERIAL;
903         rio_driver2->subtype = SERIAL_TYPE_NORMAL;
904         rio_driver2->init_termios = tty_std_termios;
905         rio_driver2->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
906         rio_driver2->flags = TTY_DRIVER_REAL_RAW;
907         tty_set_operations(rio_driver2, &rio_ops);
908
909         rio_dprintk (RIO_DEBUG_INIT, "set_termios = %p\n", gs_set_termios);
910
911         if ((error = tty_register_driver(rio_driver)))
912                 goto out2;
913         if ((error = tty_register_driver(rio_driver2)))
914                 goto out3;
915         func_exit();
916         return 0;
917 out3:
918         tty_unregister_driver(rio_driver);
919 out2:
920         put_tty_driver(rio_driver2);
921 out1:
922         put_tty_driver(rio_driver);
923 out:
924         printk(KERN_ERR "rio: Couldn't register a rio driver, error = %d\n",
925              error);
926         return 1;
927 }
928
929
930 static void * ckmalloc (int size)
931 {
932   void *p;
933
934   p = kmalloc(size, GFP_KERNEL);
935   if (p) 
936     memset(p, 0, size);
937   return p;
938 }
939
940
941
942 static int rio_init_datastructures (void)
943 {
944   int i;
945   struct Port *port;
946   func_enter();
947
948   /* Many drivers statically allocate the maximum number of ports
949      There is no reason not to allocate them dynamically. Is there? -- REW */
950   /* However, the RIO driver allows users to configure their first
951      RTA as the ports numbered 504-511. We therefore need to allocate 
952      the whole range. :-(   -- REW */
953   
954 #define RI_SZ   sizeof(struct rio_info)
955 #define HOST_SZ sizeof(struct Host)
956 #define PORT_SZ sizeof(struct Port *)
957 #define TMIO_SZ sizeof(struct termios *)
958   rio_dprintk (RIO_DEBUG_INIT, "getting : %d %d %d %d %d bytes\n", 
959                RI_SZ, 
960                RIO_HOSTS * HOST_SZ,
961                RIO_PORTS * PORT_SZ,
962                RIO_PORTS * TMIO_SZ,
963                RIO_PORTS * TMIO_SZ);
964   
965   if (!(p                  = ckmalloc (              RI_SZ))) goto free0;
966   if (!(p->RIOHosts        = ckmalloc (RIO_HOSTS * HOST_SZ))) goto free1;
967   if (!(p->RIOPortp        = ckmalloc (RIO_PORTS * PORT_SZ))) goto free2;
968   p->RIOConf = RIOConf;
969   rio_dprintk (RIO_DEBUG_INIT, "Got : %p %p %p\n", 
970                p, p->RIOHosts, p->RIOPortp);
971
972 #if 1
973   for (i = 0; i < RIO_PORTS; i++) {
974     port = p->RIOPortp[i] = ckmalloc (sizeof (struct Port));
975     if (!port) {
976       goto free6;
977     }
978     rio_dprintk (RIO_DEBUG_INIT, "initing port %d (%d)\n", i, port->Mapped);
979     port->PortNum = i;
980     port->gs.magic = RIO_MAGIC;
981     port->gs.close_delay = HZ/2;
982     port->gs.closing_wait = 30 * HZ;
983     port->gs.rd = &rio_real_driver;
984     port->portSem = SPIN_LOCK_UNLOCKED;
985     /*
986      * Initializing wait queue
987      */
988     init_waitqueue_head(&port->gs.open_wait);
989     init_waitqueue_head(&port->gs.close_wait);
990   }
991 #else
992   /* We could postpone initializing them to when they are configured. */
993 #endif
994
995
996   
997   if (rio_debug & RIO_DEBUG_INIT) {
998     my_hd (&rio_real_driver, sizeof (rio_real_driver));
999   }
1000
1001   
1002   func_exit();
1003   return 0;
1004
1005  free6:for (i--;i>=0;i--)
1006         kfree (p->RIOPortp[i]);
1007 /*free5:
1008  free4:
1009  free3:*/kfree (p->RIOPortp);
1010  free2:kfree (p->RIOHosts);
1011  free1:
1012   rio_dprintk (RIO_DEBUG_INIT, "Not enough memory! %p %p %p\n", 
1013                        p, p->RIOHosts, p->RIOPortp);
1014   kfree(p);                   
1015  free0:
1016   return -ENOMEM;
1017 }
1018
1019 static void  __exit rio_release_drivers(void)
1020 {
1021   func_enter();
1022   tty_unregister_driver(rio_driver2);
1023   tty_unregister_driver(rio_driver);
1024   put_tty_driver(rio_driver2);
1025   put_tty_driver(rio_driver);
1026   func_exit();
1027 }
1028
1029
1030 #ifdef CONFIG_PCI
1031  /* This was written for SX, but applies to RIO too...
1032     (including bugs....)
1033
1034     There is another bit besides Bit 17. Turning that bit off
1035     (on boards shipped with the fix in the eeprom) results in a 
1036     hang on the next access to the card. 
1037  */
1038
1039  /******************************************************** 
1040  * Setting bit 17 in the CNTRL register of the PLX 9050  * 
1041  * chip forces a retry on writes while a read is pending.*
1042  * This is to prevent the card locking up on Intel Xeon  *
1043  * multiprocessor systems with the NX chipset.    -- NV  *
1044  ********************************************************/
1045
1046 /* Newer cards are produced with this bit set from the configuration
1047    EEprom.  As the bit is read/write for the CPU, we can fix it here,
1048    if we detect that it isn't set correctly. -- REW */
1049
1050 void fix_rio_pci (struct pci_dev *pdev)
1051 {
1052   unsigned int hwbase;
1053   unsigned long rebase;
1054   unsigned int t;
1055
1056 #define CNTRL_REG_OFFSET        0x50
1057 #define CNTRL_REG_GOODVALUE     0x18260000
1058
1059   pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
1060   hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
1061   rebase =  (ulong) ioremap(hwbase, 0x80);
1062   t = readl (rebase + CNTRL_REG_OFFSET);
1063   if (t != CNTRL_REG_GOODVALUE) {
1064     printk (KERN_DEBUG "rio: performing cntrl reg fix: %08x -> %08x\n", 
1065             t, CNTRL_REG_GOODVALUE); 
1066     writel (CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);  
1067   }
1068   iounmap((char*) rebase);
1069 }
1070 #endif
1071
1072
1073 static int __init rio_init(void) 
1074 {
1075   int found = 0;
1076   int i;
1077   struct Host *hp;
1078   int retval;
1079   struct vpd_prom *vpdp;
1080   int okboard;
1081
1082 #ifdef CONFIG_PCI
1083   struct pci_dev *pdev = NULL;
1084   unsigned int tint;
1085   unsigned short tshort;
1086 #endif
1087
1088   func_enter();
1089   rio_dprintk (RIO_DEBUG_INIT, "Initing rio module... (rio_debug=%d)\n", 
1090                rio_debug);
1091
1092   if (abs ((long) (&rio_debug) - rio_debug) < 0x10000) {
1093     printk (KERN_WARNING "rio: rio_debug is an address, instead of a value. "
1094             "Assuming -1. Was %x/%p.\n", rio_debug, &rio_debug);
1095     rio_debug=-1;
1096   }
1097
1098   if (misc_register(&rio_fw_device) < 0) {
1099     printk(KERN_ERR "RIO: Unable to register firmware loader driver.\n");
1100     return -EIO;
1101   }
1102
1103   retval = rio_init_datastructures ();
1104   if (retval < 0) {
1105     misc_deregister(&rio_fw_device);
1106     return retval;
1107   }
1108
1109 #ifdef CONFIG_PCI
1110     /* First look for the JET devices: */
1111     while ((pdev = pci_find_device (PCI_VENDOR_ID_SPECIALIX, 
1112                                     PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, 
1113                                     pdev))) {
1114        if (pci_enable_device(pdev)) continue;
1115
1116       /* Specialix has a whole bunch of cards with
1117          0x2000 as the device ID. They say its because
1118          the standard requires it. Stupid standard. */
1119       /* It seems that reading a word doesn't work reliably on 2.0.
1120          Also, reading a non-aligned dword doesn't work. So we read the
1121          whole dword at 0x2c and extract the word at 0x2e (SUBSYSTEM_ID)
1122          ourselves */
1123       /* I don't know why the define doesn't work, constant 0x2c does --REW */ 
1124       pci_read_config_dword (pdev, 0x2c, &tint);
1125       tshort = (tint >> 16) & 0xffff;
1126       rio_dprintk (RIO_DEBUG_PROBE, "Got a specialix card: %x.\n", tint);
1127       if (tshort != 0x0100) {
1128         rio_dprintk (RIO_DEBUG_PROBE, "But it's not a RIO card (%d)...\n", 
1129                     tshort);
1130         continue;
1131       }
1132       rio_dprintk (RIO_DEBUG_PROBE, "cp1\n");
1133
1134       pci_read_config_dword(pdev, PCI_BASE_ADDRESS_2, &tint);
1135
1136       hp = &p->RIOHosts[p->RIONumHosts];
1137       hp->PaddrP =  tint & PCI_BASE_ADDRESS_MEM_MASK;
1138       hp->Ivec = pdev->irq;
1139       if (((1 << hp->Ivec) & rio_irqmask) == 0)
1140               hp->Ivec = 0;
1141       hp->CardP = (struct DpRam *)
1142       hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
1143       hp->Type  = RIO_PCI;
1144       hp->Copy  = rio_pcicopy; 
1145       hp->Mode  = RIO_PCI_BOOT_FROM_RAM;
1146       hp->HostLock = SPIN_LOCK_UNLOCKED;
1147       rio_reset_interrupt (hp);
1148       rio_start_card_running (hp);
1149
1150       rio_dprintk (RIO_DEBUG_PROBE, "Going to test it (%p/%p).\n",
1151                    (void *)p->RIOHosts[p->RIONumHosts].PaddrP,
1152                    p->RIOHosts[p->RIONumHosts].Caddr);
1153       if (RIOBoardTest( p->RIOHosts[p->RIONumHosts].PaddrP,
1154                         p->RIOHosts[p->RIONumHosts].Caddr, 
1155                         RIO_PCI, 0 ) == RIO_SUCCESS) {
1156               rio_dprintk (RIO_DEBUG_INIT, "Done RIOBoardTest\n");
1157               WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt, 0xff);
1158               p->RIOHosts[p->RIONumHosts].UniqueNum  =
1159                       ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[0]) &0xFF)<< 0)|
1160                       ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[1]) &0xFF)<< 8)|
1161                       ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[2]) &0xFF)<<16)|
1162                       ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[3]) &0xFF)<<24);
1163               rio_dprintk (RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n",
1164                            p->RIOHosts[p->RIONumHosts].UniqueNum);
1165               
1166               fix_rio_pci (pdev);
1167               p->RIOLastPCISearch = RIO_SUCCESS;
1168               p->RIONumHosts++;
1169               found++;
1170       } else {
1171               iounmap((char*) (p->RIOHosts[p->RIONumHosts].Caddr));
1172       }
1173     }
1174     
1175     /* Then look for the older PCI card.... : */
1176
1177   /* These older PCI cards have problems (only byte-mode access is
1178      supported), which makes them a bit awkward to support. 
1179      They also have problems sharing interrupts. Be careful. 
1180      (The driver now refuses to share interrupts for these
1181      cards. This should be sufficient).
1182   */
1183
1184     /* Then look for the older RIO/PCI devices: */
1185     while ((pdev = pci_find_device (PCI_VENDOR_ID_SPECIALIX, 
1186                                     PCI_DEVICE_ID_SPECIALIX_RIO, 
1187                                     pdev))) {
1188        if (pci_enable_device(pdev)) continue;
1189
1190 #ifdef CONFIG_RIO_OLDPCI
1191       pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &tint);
1192
1193       hp = &p->RIOHosts[p->RIONumHosts];
1194       hp->PaddrP =  tint & PCI_BASE_ADDRESS_MEM_MASK;
1195       hp->Ivec = pdev->irq;
1196       if (((1 << hp->Ivec) & rio_irqmask) == 0) 
1197         hp->Ivec = 0;
1198       hp->Ivec |= 0x8000; /* Mark as non-sharable */
1199       hp->CardP = (struct DpRam *)
1200       hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
1201       hp->Type  = RIO_PCI;
1202       hp->Copy  = rio_pcicopy;
1203       hp->Mode  = RIO_PCI_BOOT_FROM_RAM;
1204       hp->HostLock = SPIN_LOCK_UNLOCKED;
1205
1206       rio_dprintk (RIO_DEBUG_PROBE, "Ivec: %x\n", hp->Ivec);
1207       rio_dprintk (RIO_DEBUG_PROBE, "Mode: %x\n", hp->Mode);
1208
1209       rio_reset_interrupt (hp);
1210       rio_start_card_running (hp);
1211        rio_dprintk (RIO_DEBUG_PROBE, "Going to test it (%p/%p).\n",
1212                    (void *)p->RIOHosts[p->RIONumHosts].PaddrP,
1213                    p->RIOHosts[p->RIONumHosts].Caddr);
1214       if (RIOBoardTest( p->RIOHosts[p->RIONumHosts].PaddrP,
1215                         p->RIOHosts[p->RIONumHosts].Caddr, 
1216                         RIO_PCI, 0 ) == RIO_SUCCESS) {
1217         WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt, 0xff);
1218         p->RIOHosts[p->RIONumHosts].UniqueNum  =
1219           ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[0]) &0xFF)<< 0)|
1220           ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[1]) &0xFF)<< 8)|
1221           ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[2]) &0xFF)<<16)|
1222           ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[3]) &0xFF)<<24);
1223         rio_dprintk (RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n",
1224                    p->RIOHosts[p->RIONumHosts].UniqueNum);
1225
1226         p->RIOLastPCISearch = RIO_SUCCESS;
1227         p->RIONumHosts++;
1228         found++;
1229       } else {
1230         iounmap((char*) (p->RIOHosts[p->RIONumHosts].Caddr));
1231       }
1232 #else
1233       printk (KERN_ERR "Found an older RIO PCI card, but the driver is not "
1234               "compiled to support it.\n");
1235 #endif
1236     }
1237 #endif /* PCI */
1238
1239   /* Now probe for ISA cards... */
1240   for (i=0;i<NR_RIO_ADDRS;i++) {
1241     hp = &p->RIOHosts[p->RIONumHosts];
1242     hp->PaddrP = rio_probe_addrs[i];
1243     /* There was something about the IRQs of these cards. 'Forget what.--REW */
1244     hp->Ivec = 0;
1245     hp->CardP = (struct DpRam *)
1246     hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
1247     hp->Type = RIO_AT;
1248     hp->Copy = rio_pcicopy; /* AT card PCI???? - PVDL
1249                              * -- YES! this is now a normal copy. Only the 
1250                              * old PCI card uses the special PCI copy. 
1251                              * Moreover, the ISA card will work with the 
1252                              * special PCI copy anyway. -- REW */
1253     hp->Mode = 0;
1254     hp->HostLock = SPIN_LOCK_UNLOCKED;
1255
1256     vpdp = get_VPD_PROM (hp);
1257     rio_dprintk (RIO_DEBUG_PROBE, "Got VPD ROM\n");
1258     okboard = 0;
1259     if ((strncmp (vpdp->identifier, RIO_ISA_IDENT, 16) == 0) ||
1260         (strncmp (vpdp->identifier, RIO_ISA2_IDENT, 16) == 0) ||
1261         (strncmp (vpdp->identifier, RIO_ISA3_IDENT, 16) == 0)) {
1262       /* Board is present... */
1263       if (RIOBoardTest (hp->PaddrP, 
1264                         hp->Caddr, RIO_AT, 0) == RIO_SUCCESS) {
1265         /* ... and feeling fine!!!! */
1266         rio_dprintk (RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n",
1267                    p->RIOHosts[p->RIONumHosts].UniqueNum);
1268         if (RIOAssignAT(p, hp->PaddrP, hp->Caddr, 0)) {        
1269           rio_dprintk (RIO_DEBUG_PROBE, "Hmm Tested ok, host%d uniqid = %x.\n",
1270                        p->RIONumHosts, 
1271                        p->RIOHosts[p->RIONumHosts-1].UniqueNum);
1272           okboard++;
1273           found++;
1274         }
1275       }
1276
1277     if (!okboard)
1278       iounmap ((char*) (hp->Caddr));
1279     }
1280   }
1281
1282
1283   for (i=0;i<p->RIONumHosts;i++) {
1284     hp = &p->RIOHosts[i];
1285     if (hp->Ivec) {
1286       int mode = SA_SHIRQ;
1287       if (hp->Ivec & 0x8000) {mode = 0; hp->Ivec &= 0x7fff;}
1288       rio_dprintk (RIO_DEBUG_INIT, "Requesting interrupt hp: %p rio_interrupt: %d Mode: %x\n", hp,hp->Ivec, hp->Mode);
1289       retval = request_irq (hp->Ivec, rio_interrupt, mode, "rio", hp);
1290       rio_dprintk (RIO_DEBUG_INIT, "Return value from request_irq: %d\n", retval);
1291       if (retval) {
1292               printk(KERN_ERR "rio: Cannot allocate irq %d.\n", hp->Ivec);
1293               hp->Ivec = 0;
1294       }
1295       rio_dprintk (RIO_DEBUG_INIT, "Got irq %d.\n", hp->Ivec);
1296       if (hp->Ivec != 0){
1297               rio_dprintk (RIO_DEBUG_INIT, "Enabling interrupts on rio card.\n"); 
1298               hp->Mode |= RIO_PCI_INT_ENABLE;
1299       } else
1300               hp->Mode &= !RIO_PCI_INT_ENABLE;
1301       rio_dprintk (RIO_DEBUG_INIT, "New Mode: %x\n", hp->Mode);
1302       rio_start_card_running (hp);
1303     }
1304     /* Init the timer "always" to make sure that it can safely be 
1305        deleted when we unload... */
1306
1307     init_timer (&hp->timer);
1308     if (!hp->Ivec) {
1309       rio_dprintk (RIO_DEBUG_INIT, "Starting polling at %dj intervals.\n", 
1310                    rio_poll);
1311       hp->timer.data = i;
1312       hp->timer.function = rio_pollfunc;
1313       hp->timer.expires = jiffies + rio_poll;
1314       add_timer (&hp->timer);
1315     }
1316   }
1317
1318   if (found) {
1319     rio_dprintk (RIO_DEBUG_INIT, "rio: total of %d boards detected.\n", found);
1320     rio_init_drivers ();
1321   } else {
1322     /* deregister the misc device we created earlier */
1323     misc_deregister(&rio_fw_device);
1324   }
1325
1326   func_exit();
1327   return found?0:-EIO;
1328 }
1329
1330
1331 static void __exit rio_exit (void)
1332 {
1333   int i; 
1334   struct Host *hp;
1335   
1336   func_enter();
1337
1338   for (i=0,hp=p->RIOHosts;i<p->RIONumHosts;i++, hp++) {
1339     RIOHostReset (hp->Type, hp->CardP, hp->Slot);
1340     if (hp->Ivec) {
1341       free_irq (hp->Ivec, hp);
1342       rio_dprintk (RIO_DEBUG_INIT, "freed irq %d.\n", hp->Ivec);
1343     }
1344     /* It is safe/allowed to del_timer a non-active timer */
1345     del_timer (&hp->timer);
1346   }
1347
1348   if (misc_deregister(&rio_fw_device) < 0) {
1349     printk (KERN_INFO "rio: couldn't deregister control-device\n");
1350   }
1351
1352
1353   rio_dprintk (RIO_DEBUG_CLEANUP, "Cleaning up drivers\n");
1354
1355   rio_release_drivers ();
1356
1357   /* Release dynamically allocated memory */
1358   kfree (p->RIOPortp);
1359   kfree (p->RIOHosts);
1360   kfree (p);
1361
1362   func_exit();
1363 }
1364
1365 module_init(rio_init);
1366 module_exit(rio_exit);
1367
1368 /*
1369  * Anybody who knows why this doesn't work for me, please tell me -- REW.
1370  * Snatched from scsi.c (fixed one spelling error):
1371  * Overrides for Emacs so that we follow Linus' tabbing style.
1372  * Emacs will notice this stuff at the end of the file and automatically
1373  * adjust the settings for this buffer only.  This must remain at the end
1374  * of the file.
1375  * ---------------------------------------------------------------------------
1376  * Local Variables:
1377  * c-indent-level: 4
1378  * c-brace-imaginary-offset: 0
1379  * c-brace-offset: -4
1380  * c-argdecl-indent: 4
1381  * c-label-offset: -4
1382  * c-continued-statement-offset: 4
1383  * c-continued-brace-offset: 0
1384  * indent-tabs-mode: nil
1385  * tab-width: 8
1386  * End:
1387  */
1388