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