This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / parport / parport_pc.c
1 /* Low-level parallel-port routines for 8255-based PC-style hardware.
2  * 
3  * Authors: Phil Blundell <Philip.Blundell@pobox.com>
4  *          Tim Waugh <tim@cyberelk.demon.co.uk>
5  *          Jose Renau <renau@acm.org>
6  *          David Campbell <campbell@torque.net>
7  *          Andrea Arcangeli
8  *
9  * based on work by Grant Guenther <grant@torque.net> and Phil Blundell.
10  *
11  * Cleaned up include files - Russell King <linux@arm.uk.linux.org>
12  * DMA support - Bert De Jonghe <bert@sophis.be>
13  * Many ECP bugs fixed.  Fred Barnes & Jamie Lokier, 1999
14  * More PCI support now conditional on CONFIG_PCI, 03/2001, Paul G. 
15  * Various hacks, Fred Barnes, 04/2001
16  * Updated probing logic - Adam Belay <ambx1@neo.rr.com>
17  */
18
19 /* This driver should work with any hardware that is broadly compatible
20  * with that in the IBM PC.  This applies to the majority of integrated
21  * I/O chipsets that are commonly available.  The expected register
22  * layout is:
23  *
24  *      base+0          data
25  *      base+1          status
26  *      base+2          control
27  *
28  * In addition, there are some optional registers:
29  *
30  *      base+3          EPP address
31  *      base+4          EPP data
32  *      base+0x400      ECP config A
33  *      base+0x401      ECP config B
34  *      base+0x402      ECP control
35  *
36  * All registers are 8 bits wide and read/write.  If your hardware differs
37  * only in register addresses (eg because your registers are on 32-bit
38  * word boundaries) then you can alter the constants in parport_pc.h to
39  * accommodate this.
40  *
41  * Note that the ECP registers may not start at offset 0x400 for PCI cards,
42  * but rather will start at port->base_hi.
43  */
44
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/sched.h>
49 #include <linux/delay.h>
50 #include <linux/errno.h>
51 #include <linux/interrupt.h>
52 #include <linux/ioport.h>
53 #include <linux/kernel.h>
54 #include <linux/slab.h>
55 #include <linux/pci.h>
56 #include <linux/pnp.h>
57 #include <linux/sysctl.h>
58
59 #include <asm/io.h>
60 #include <asm/dma.h>
61 #include <asm/uaccess.h>
62
63 #include <linux/parport.h>
64 #include <linux/parport_pc.h>
65 #include <asm/parport.h>
66
67 #define PARPORT_PC_MAX_PORTS PARPORT_MAX
68
69 /* ECR modes */
70 #define ECR_SPP 00
71 #define ECR_PS2 01
72 #define ECR_PPF 02
73 #define ECR_ECP 03
74 #define ECR_EPP 04
75 #define ECR_VND 05
76 #define ECR_TST 06
77 #define ECR_CNF 07
78 #define ECR_MODE_MASK 0xe0
79 #define ECR_WRITE(p,v) frob_econtrol((p),0xff,(v))
80
81 #undef DEBUG
82
83 #ifdef DEBUG
84 #define DPRINTK  printk
85 #else
86 #define DPRINTK(stuff...)
87 #endif
88
89
90 #define NR_SUPERIOS 3
91 static struct superio_struct {  /* For Super-IO chips autodetection */
92         int io;
93         int irq;
94         int dma;
95 } superios[NR_SUPERIOS] __devinitdata = { {0,},};
96
97 static int user_specified;
98 #if defined(CONFIG_PARPORT_PC_SUPERIO) || \
99        (defined(CONFIG_PARPORT_1284) && defined(CONFIG_PARPORT_PC_FIFO))
100 static int verbose_probing;
101 #endif
102 static int pci_registered_parport;
103 static int pnp_registered_parport;
104
105 /* frob_control, but for ECR */
106 static void frob_econtrol (struct parport *pb, unsigned char m,
107                            unsigned char v)
108 {
109         unsigned char ectr = 0;
110
111         if (m != 0xff)
112                 ectr = inb (ECONTROL (pb));
113
114         DPRINTK (KERN_DEBUG "frob_econtrol(%02x,%02x): %02x -> %02x\n",
115                 m, v, ectr, (ectr & ~m) ^ v);
116
117         outb ((ectr & ~m) ^ v, ECONTROL (pb));
118 }
119
120 static __inline__ void frob_set_mode (struct parport *p, int mode)
121 {
122         frob_econtrol (p, ECR_MODE_MASK, mode << 5);
123 }
124
125 #ifdef CONFIG_PARPORT_PC_FIFO
126 /* Safely change the mode bits in the ECR 
127    Returns:
128             0    : Success
129            -EBUSY: Could not drain FIFO in some finite amount of time,
130                    mode not changed!
131  */
132 static int change_mode(struct parport *p, int m)
133 {
134         const struct parport_pc_private *priv = p->physport->private_data;
135         unsigned char oecr;
136         int mode;
137
138         DPRINTK(KERN_INFO "parport change_mode ECP-ISA to mode 0x%02x\n",m);
139
140         if (!priv->ecr) {
141                 printk (KERN_DEBUG "change_mode: but there's no ECR!\n");
142                 return 0;
143         }
144
145         /* Bits <7:5> contain the mode. */
146         oecr = inb (ECONTROL (p));
147         mode = (oecr >> 5) & 0x7;
148         if (mode == m) return 0;
149
150         if (mode >= 2 && !(priv->ctr & 0x20)) {
151                 /* This mode resets the FIFO, so we may
152                  * have to wait for it to drain first. */
153                 unsigned long expire = jiffies + p->physport->cad->timeout;
154                 int counter;
155                 switch (mode) {
156                 case ECR_PPF: /* Parallel Port FIFO mode */
157                 case ECR_ECP: /* ECP Parallel Port mode */
158                         /* Busy wait for 200us */
159                         for (counter = 0; counter < 40; counter++) {
160                                 if (inb (ECONTROL (p)) & 0x01)
161                                         break;
162                                 if (signal_pending (current)) break;
163                                 udelay (5);
164                         }
165
166                         /* Poll slowly. */
167                         while (!(inb (ECONTROL (p)) & 0x01)) {
168                                 if (time_after_eq (jiffies, expire))
169                                         /* The FIFO is stuck. */
170                                         return -EBUSY;
171                                 __set_current_state (TASK_INTERRUPTIBLE);
172                                 schedule_timeout ((HZ + 99) / 100);
173                                 if (signal_pending (current))
174                                         break;
175                         }
176                 }
177         }
178
179         if (mode >= 2 && m >= 2) {
180                 /* We have to go through mode 001 */
181                 oecr &= ~(7 << 5);
182                 oecr |= ECR_PS2 << 5;
183                 ECR_WRITE (p, oecr);
184         }
185
186         /* Set the mode. */
187         oecr &= ~(7 << 5);
188         oecr |= m << 5;
189         ECR_WRITE (p, oecr);
190         return 0;
191 }
192
193 #ifdef CONFIG_PARPORT_1284
194 /* Find FIFO lossage; FIFO is reset */
195 static int get_fifo_residue (struct parport *p)
196 {
197         int residue;
198         int cnfga;
199         const struct parport_pc_private *priv = p->physport->private_data;
200
201         /* Adjust for the contents of the FIFO. */
202         for (residue = priv->fifo_depth; ; residue--) {
203                 if (inb (ECONTROL (p)) & 0x2)
204                                 /* Full up. */
205                         break;
206
207                 outb (0, FIFO (p));
208         }
209
210         printk (KERN_DEBUG "%s: %d PWords were left in FIFO\n", p->name,
211                 residue);
212
213         /* Reset the FIFO. */
214         frob_set_mode (p, ECR_PS2);
215
216         /* Now change to config mode and clean up. FIXME */
217         frob_set_mode (p, ECR_CNF);
218         cnfga = inb (CONFIGA (p));
219         printk (KERN_DEBUG "%s: cnfgA contains 0x%02x\n", p->name, cnfga);
220
221         if (!(cnfga & (1<<2))) {
222                 printk (KERN_DEBUG "%s: Accounting for extra byte\n", p->name);
223                 residue++;
224         }
225
226         /* Don't care about partial PWords until support is added for
227          * PWord != 1 byte. */
228
229         /* Back to PS2 mode. */
230         frob_set_mode (p, ECR_PS2);
231
232         DPRINTK (KERN_DEBUG "*** get_fifo_residue: done residue collecting (ecr = 0x%2.2x)\n", inb (ECONTROL (p)));
233         return residue;
234 }
235 #endif /* IEEE 1284 support */
236 #endif /* FIFO support */
237
238 /*
239  * Clear TIMEOUT BIT in EPP MODE
240  *
241  * This is also used in SPP detection.
242  */
243 static int clear_epp_timeout(struct parport *pb)
244 {
245         unsigned char r;
246
247         if (!(parport_pc_read_status(pb) & 0x01))
248                 return 1;
249
250         /* To clear timeout some chips require double read */
251         parport_pc_read_status(pb);
252         r = parport_pc_read_status(pb);
253         outb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
254         outb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
255         r = parport_pc_read_status(pb);
256
257         return !(r & 0x01);
258 }
259
260 /*
261  * Access functions.
262  *
263  * Most of these aren't static because they may be used by the
264  * parport_xxx_yyy macros.  extern __inline__ versions of several
265  * of these are in parport_pc.h.
266  */
267
268 static irqreturn_t parport_pc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
269 {
270         parport_generic_irq(irq, (struct parport *) dev_id, regs);
271         /* FIXME! Was it really ours? */
272         return IRQ_HANDLED;
273 }
274
275 void parport_pc_init_state(struct pardevice *dev, struct parport_state *s)
276 {
277         s->u.pc.ctr = 0xc;
278         if (dev->irq_func &&
279             dev->port->irq != PARPORT_IRQ_NONE)
280                 /* Set ackIntEn */
281                 s->u.pc.ctr |= 0x10;
282
283         s->u.pc.ecr = 0x34; /* NetMos chip can cause problems 0x24;
284                              * D.Gruszka VScom */
285 }
286
287 void parport_pc_save_state(struct parport *p, struct parport_state *s)
288 {
289         const struct parport_pc_private *priv = p->physport->private_data;
290         s->u.pc.ctr = priv->ctr;
291         if (priv->ecr)
292                 s->u.pc.ecr = inb (ECONTROL (p));
293 }
294
295 void parport_pc_restore_state(struct parport *p, struct parport_state *s)
296 {
297         struct parport_pc_private *priv = p->physport->private_data;
298         register unsigned char c = s->u.pc.ctr & priv->ctr_writable;
299         outb (c, CONTROL (p));
300         priv->ctr = c;
301         if (priv->ecr)
302                 ECR_WRITE (p, s->u.pc.ecr);
303 }
304
305 #ifdef CONFIG_PARPORT_1284
306 static size_t parport_pc_epp_read_data (struct parport *port, void *buf,
307                                         size_t length, int flags)
308 {
309         size_t got = 0;
310
311         if (flags & PARPORT_W91284PIC) {
312                 unsigned char status;
313                 size_t left = length;
314
315                 /* use knowledge about data lines..:
316                  *  nFault is 0 if there is at least 1 byte in the Warp's FIFO
317                  *  pError is 1 if there are 16 bytes in the Warp's FIFO
318                  */
319                 status = inb (STATUS (port));
320
321                 while (!(status & 0x08) && (got < length)) {
322                         if ((left >= 16) && (status & 0x20) && !(status & 0x08)) {
323                                 /* can grab 16 bytes from warp fifo */
324                                 if (!((long)buf & 0x03)) {
325                                         insl (EPPDATA (port), buf, 4);
326                                 } else {
327                                         insb (EPPDATA (port), buf, 16);
328                                 }
329                                 buf += 16;
330                                 got += 16;
331                                 left -= 16;
332                         } else {
333                                 /* grab single byte from the warp fifo */
334                                 *((char *)buf) = inb (EPPDATA (port));
335                                 buf++;
336                                 got++;
337                                 left--;
338                         }
339                         status = inb (STATUS (port));
340                         if (status & 0x01) {
341                                 /* EPP timeout should never occur... */
342                                 printk (KERN_DEBUG "%s: EPP timeout occurred while talking to "
343                                         "w91284pic (should not have done)\n", port->name);
344                                 clear_epp_timeout (port);
345                         }
346                 }
347                 return got;
348         }
349         if ((flags & PARPORT_EPP_FAST) && (length > 1)) {
350                 if (!(((long)buf | length) & 0x03)) {
351                         insl (EPPDATA (port), buf, (length >> 2));
352                 } else {
353                         insb (EPPDATA (port), buf, length);
354                 }
355                 if (inb (STATUS (port)) & 0x01) {
356                         clear_epp_timeout (port);
357                         return -EIO;
358                 }
359                 return length;
360         }
361         for (; got < length; got++) {
362                 *((char*)buf) = inb (EPPDATA(port));
363                 buf++;
364                 if (inb (STATUS (port)) & 0x01) {
365                         /* EPP timeout */
366                         clear_epp_timeout (port);
367                         break;
368                 }
369         }
370
371         return got;
372 }
373
374 static size_t parport_pc_epp_write_data (struct parport *port, const void *buf,
375                                          size_t length, int flags)
376 {
377         size_t written = 0;
378
379         if ((flags & PARPORT_EPP_FAST) && (length > 1)) {
380                 if (!(((long)buf | length) & 0x03)) {
381                         outsl (EPPDATA (port), buf, (length >> 2));
382                 } else {
383                         outsb (EPPDATA (port), buf, length);
384                 }
385                 if (inb (STATUS (port)) & 0x01) {
386                         clear_epp_timeout (port);
387                         return -EIO;
388                 }
389                 return length;
390         }
391         for (; written < length; written++) {
392                 outb (*((char*)buf), EPPDATA(port));
393                 buf++;
394                 if (inb (STATUS(port)) & 0x01) {
395                         clear_epp_timeout (port);
396                         break;
397                 }
398         }
399
400         return written;
401 }
402
403 static size_t parport_pc_epp_read_addr (struct parport *port, void *buf,
404                                         size_t length, int flags)
405 {
406         size_t got = 0;
407
408         if ((flags & PARPORT_EPP_FAST) && (length > 1)) {
409                 insb (EPPADDR (port), buf, length);
410                 if (inb (STATUS (port)) & 0x01) {
411                         clear_epp_timeout (port);
412                         return -EIO;
413                 }
414                 return length;
415         }
416         for (; got < length; got++) {
417                 *((char*)buf) = inb (EPPADDR (port));
418                 buf++;
419                 if (inb (STATUS (port)) & 0x01) {
420                         clear_epp_timeout (port);
421                         break;
422                 }
423         }
424
425         return got;
426 }
427
428 static size_t parport_pc_epp_write_addr (struct parport *port,
429                                          const void *buf, size_t length,
430                                          int flags)
431 {
432         size_t written = 0;
433
434         if ((flags & PARPORT_EPP_FAST) && (length > 1)) {
435                 outsb (EPPADDR (port), buf, length);
436                 if (inb (STATUS (port)) & 0x01) {
437                         clear_epp_timeout (port);
438                         return -EIO;
439                 }
440                 return length;
441         }
442         for (; written < length; written++) {
443                 outb (*((char*)buf), EPPADDR (port));
444                 buf++;
445                 if (inb (STATUS (port)) & 0x01) {
446                         clear_epp_timeout (port);
447                         break;
448                 }
449         }
450
451         return written;
452 }
453
454 static size_t parport_pc_ecpepp_read_data (struct parport *port, void *buf,
455                                            size_t length, int flags)
456 {
457         size_t got;
458
459         frob_set_mode (port, ECR_EPP);
460         parport_pc_data_reverse (port);
461         parport_pc_write_control (port, 0x4);
462         got = parport_pc_epp_read_data (port, buf, length, flags);
463         frob_set_mode (port, ECR_PS2);
464
465         return got;
466 }
467
468 static size_t parport_pc_ecpepp_write_data (struct parport *port,
469                                             const void *buf, size_t length,
470                                             int flags)
471 {
472         size_t written;
473
474         frob_set_mode (port, ECR_EPP);
475         parport_pc_write_control (port, 0x4);
476         parport_pc_data_forward (port);
477         written = parport_pc_epp_write_data (port, buf, length, flags);
478         frob_set_mode (port, ECR_PS2);
479
480         return written;
481 }
482
483 static size_t parport_pc_ecpepp_read_addr (struct parport *port, void *buf,
484                                            size_t length, int flags)
485 {
486         size_t got;
487
488         frob_set_mode (port, ECR_EPP);
489         parport_pc_data_reverse (port);
490         parport_pc_write_control (port, 0x4);
491         got = parport_pc_epp_read_addr (port, buf, length, flags);
492         frob_set_mode (port, ECR_PS2);
493
494         return got;
495 }
496
497 static size_t parport_pc_ecpepp_write_addr (struct parport *port,
498                                             const void *buf, size_t length,
499                                             int flags)
500 {
501         size_t written;
502
503         frob_set_mode (port, ECR_EPP);
504         parport_pc_write_control (port, 0x4);
505         parport_pc_data_forward (port);
506         written = parport_pc_epp_write_addr (port, buf, length, flags);
507         frob_set_mode (port, ECR_PS2);
508
509         return written;
510 }
511 #endif /* IEEE 1284 support */
512
513 #ifdef CONFIG_PARPORT_PC_FIFO
514 static size_t parport_pc_fifo_write_block_pio (struct parport *port,
515                                                const void *buf, size_t length)
516 {
517         int ret = 0;
518         const unsigned char *bufp = buf;
519         size_t left = length;
520         unsigned long expire = jiffies + port->physport->cad->timeout;
521         const int fifo = FIFO (port);
522         int poll_for = 8; /* 80 usecs */
523         const struct parport_pc_private *priv = port->physport->private_data;
524         const int fifo_depth = priv->fifo_depth;
525
526         port = port->physport;
527
528         /* We don't want to be interrupted every character. */
529         parport_pc_disable_irq (port);
530         /* set nErrIntrEn and serviceIntr */
531         frob_econtrol (port, (1<<4) | (1<<2), (1<<4) | (1<<2));
532
533         /* Forward mode. */
534         parport_pc_data_forward (port); /* Must be in PS2 mode */
535
536         while (left) {
537                 unsigned char byte;
538                 unsigned char ecrval = inb (ECONTROL (port));
539                 int i = 0;
540
541                 if (need_resched() && time_before (jiffies, expire))
542                         /* Can't yield the port. */
543                         schedule ();
544
545                 /* Anyone else waiting for the port? */
546                 if (port->waithead) {
547                         printk (KERN_DEBUG "Somebody wants the port\n");
548                         break;
549                 }
550
551                 if (ecrval & 0x02) {
552                         /* FIFO is full. Wait for interrupt. */
553
554                         /* Clear serviceIntr */
555                         ECR_WRITE (port, ecrval & ~(1<<2));
556                 false_alarm:
557                         ret = parport_wait_event (port, HZ);
558                         if (ret < 0) break;
559                         ret = 0;
560                         if (!time_before (jiffies, expire)) {
561                                 /* Timed out. */
562                                 printk (KERN_DEBUG "FIFO write timed out\n");
563                                 break;
564                         }
565                         ecrval = inb (ECONTROL (port));
566                         if (!(ecrval & (1<<2))) {
567                                 if (need_resched() &&
568                                     time_before (jiffies, expire))
569                                         schedule ();
570
571                                 goto false_alarm;
572                         }
573
574                         continue;
575                 }
576
577                 /* Can't fail now. */
578                 expire = jiffies + port->cad->timeout;
579
580         poll:
581                 if (signal_pending (current))
582                         break;
583
584                 if (ecrval & 0x01) {
585                         /* FIFO is empty. Blast it full. */
586                         const int n = left < fifo_depth ? left : fifo_depth;
587                         outsb (fifo, bufp, n);
588                         bufp += n;
589                         left -= n;
590
591                         /* Adjust the poll time. */
592                         if (i < (poll_for - 2)) poll_for--;
593                         continue;
594                 } else if (i++ < poll_for) {
595                         udelay (10);
596                         ecrval = inb (ECONTROL (port));
597                         goto poll;
598                 }
599
600                 /* Half-full (call me an optimist) */
601                 byte = *bufp++;
602                 outb (byte, fifo);
603                 left--;
604         }
605
606 dump_parport_state ("leave fifo_write_block_pio", port);
607         return length - left;
608 }
609
610 static size_t parport_pc_fifo_write_block_dma (struct parport *port,
611                                                const void *buf, size_t length)
612 {
613         int ret = 0;
614         unsigned long dmaflag;
615         size_t left = length;
616         const struct parport_pc_private *priv = port->physport->private_data;
617         dma_addr_t dma_addr, dma_handle;
618         size_t maxlen = 0x10000; /* max 64k per DMA transfer */
619         unsigned long start = (unsigned long) buf;
620         unsigned long end = (unsigned long) buf + length - 1;
621
622 dump_parport_state ("enter fifo_write_block_dma", port);
623         if (end < MAX_DMA_ADDRESS) {
624                 /* If it would cross a 64k boundary, cap it at the end. */
625                 if ((start ^ end) & ~0xffffUL)
626                         maxlen = 0x10000 - (start & 0xffff);
627
628                 dma_addr = dma_handle = pci_map_single(priv->dev, (void *)buf, length,
629                                                        PCI_DMA_TODEVICE);
630         } else {
631                 /* above 16 MB we use a bounce buffer as ISA-DMA is not possible */
632                 maxlen   = PAGE_SIZE;          /* sizeof(priv->dma_buf) */
633                 dma_addr = priv->dma_handle;
634                 dma_handle = 0;
635         }
636
637         port = port->physport;
638
639         /* We don't want to be interrupted every character. */
640         parport_pc_disable_irq (port);
641         /* set nErrIntrEn and serviceIntr */
642         frob_econtrol (port, (1<<4) | (1<<2), (1<<4) | (1<<2));
643
644         /* Forward mode. */
645         parport_pc_data_forward (port); /* Must be in PS2 mode */
646
647         while (left) {
648                 unsigned long expire = jiffies + port->physport->cad->timeout;
649
650                 size_t count = left;
651
652                 if (count > maxlen)
653                         count = maxlen;
654
655                 if (!dma_handle)   /* bounce buffer ! */
656                         memcpy(priv->dma_buf, buf, count);
657
658                 dmaflag = claim_dma_lock();
659                 disable_dma(port->dma);
660                 clear_dma_ff(port->dma);
661                 set_dma_mode(port->dma, DMA_MODE_WRITE);
662                 set_dma_addr(port->dma, dma_addr);
663                 set_dma_count(port->dma, count);
664
665                 /* Set DMA mode */
666                 frob_econtrol (port, 1<<3, 1<<3);
667
668                 /* Clear serviceIntr */
669                 frob_econtrol (port, 1<<2, 0);
670
671                 enable_dma(port->dma);
672                 release_dma_lock(dmaflag);
673
674                 /* assume DMA will be successful */
675                 left -= count;
676                 buf  += count;
677                 if (dma_handle) dma_addr += count;
678
679                 /* Wait for interrupt. */
680         false_alarm:
681                 ret = parport_wait_event (port, HZ);
682                 if (ret < 0) break;
683                 ret = 0;
684                 if (!time_before (jiffies, expire)) {
685                         /* Timed out. */
686                         printk (KERN_DEBUG "DMA write timed out\n");
687                         break;
688                 }
689                 /* Is serviceIntr set? */
690                 if (!(inb (ECONTROL (port)) & (1<<2))) {
691                         cond_resched();
692
693                         goto false_alarm;
694                 }
695
696                 dmaflag = claim_dma_lock();
697                 disable_dma(port->dma);
698                 clear_dma_ff(port->dma);
699                 count = get_dma_residue(port->dma);
700                 release_dma_lock(dmaflag);
701
702                 cond_resched(); /* Can't yield the port. */
703
704                 /* Anyone else waiting for the port? */
705                 if (port->waithead) {
706                         printk (KERN_DEBUG "Somebody wants the port\n");
707                         break;
708                 }
709
710                 /* update for possible DMA residue ! */
711                 buf  -= count;
712                 left += count;
713                 if (dma_handle) dma_addr -= count;
714         }
715
716         /* Maybe got here through break, so adjust for DMA residue! */
717         dmaflag = claim_dma_lock();
718         disable_dma(port->dma);
719         clear_dma_ff(port->dma);
720         left += get_dma_residue(port->dma);
721         release_dma_lock(dmaflag);
722
723         /* Turn off DMA mode */
724         frob_econtrol (port, 1<<3, 0);
725         
726         if (dma_handle)
727                 pci_unmap_single(priv->dev, dma_handle, length, PCI_DMA_TODEVICE);
728
729 dump_parport_state ("leave fifo_write_block_dma", port);
730         return length - left;
731 }
732
733 /* Parallel Port FIFO mode (ECP chipsets) */
734 size_t parport_pc_compat_write_block_pio (struct parport *port,
735                                           const void *buf, size_t length,
736                                           int flags)
737 {
738         size_t written;
739         int r;
740         unsigned long expire;
741         const struct parport_pc_private *priv = port->physport->private_data;
742
743         /* Special case: a timeout of zero means we cannot call schedule().
744          * Also if O_NONBLOCK is set then use the default implementation. */
745         if (port->physport->cad->timeout <= PARPORT_INACTIVITY_O_NONBLOCK)
746                 return parport_ieee1284_write_compat (port, buf,
747                                                       length, flags);
748
749         /* Set up parallel port FIFO mode.*/
750         parport_pc_data_forward (port); /* Must be in PS2 mode */
751         parport_pc_frob_control (port, PARPORT_CONTROL_STROBE, 0);
752         r = change_mode (port, ECR_PPF); /* Parallel port FIFO */
753         if (r)  printk (KERN_DEBUG "%s: Warning change_mode ECR_PPF failed\n", port->name);
754
755         port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
756
757         /* Write the data to the FIFO. */
758         if (port->dma != PARPORT_DMA_NONE)
759                 written = parport_pc_fifo_write_block_dma (port, buf, length);
760         else
761                 written = parport_pc_fifo_write_block_pio (port, buf, length);
762
763         /* Finish up. */
764         /* For some hardware we don't want to touch the mode until
765          * the FIFO is empty, so allow 4 seconds for each position
766          * in the fifo.
767          */
768         expire = jiffies + (priv->fifo_depth * HZ * 4);
769         do {
770                 /* Wait for the FIFO to empty */
771                 r = change_mode (port, ECR_PS2);
772                 if (r != -EBUSY) {
773                         break;
774                 }
775         } while (time_before (jiffies, expire));
776         if (r == -EBUSY) {
777
778                 printk (KERN_DEBUG "%s: FIFO is stuck\n", port->name);
779
780                 /* Prevent further data transfer. */
781                 frob_set_mode (port, ECR_TST);
782
783                 /* Adjust for the contents of the FIFO. */
784                 for (written -= priv->fifo_depth; ; written++) {
785                         if (inb (ECONTROL (port)) & 0x2) {
786                                 /* Full up. */
787                                 break;
788                         }
789                         outb (0, FIFO (port));
790                 }
791
792                 /* Reset the FIFO and return to PS2 mode. */
793                 frob_set_mode (port, ECR_PS2);
794         }
795
796         r = parport_wait_peripheral (port,
797                                      PARPORT_STATUS_BUSY,
798                                      PARPORT_STATUS_BUSY);
799         if (r)
800                 printk (KERN_DEBUG
801                         "%s: BUSY timeout (%d) in compat_write_block_pio\n", 
802                         port->name, r);
803
804         port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
805
806         return written;
807 }
808
809 /* ECP */
810 #ifdef CONFIG_PARPORT_1284
811 size_t parport_pc_ecp_write_block_pio (struct parport *port,
812                                        const void *buf, size_t length,
813                                        int flags)
814 {
815         size_t written;
816         int r;
817         unsigned long expire;
818         const struct parport_pc_private *priv = port->physport->private_data;
819
820         /* Special case: a timeout of zero means we cannot call schedule().
821          * Also if O_NONBLOCK is set then use the default implementation. */
822         if (port->physport->cad->timeout <= PARPORT_INACTIVITY_O_NONBLOCK)
823                 return parport_ieee1284_ecp_write_data (port, buf,
824                                                         length, flags);
825
826         /* Switch to forward mode if necessary. */
827         if (port->physport->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
828                 /* Event 47: Set nInit high. */
829                 parport_frob_control (port,
830                                       PARPORT_CONTROL_INIT
831                                       | PARPORT_CONTROL_AUTOFD,
832                                       PARPORT_CONTROL_INIT
833                                       | PARPORT_CONTROL_AUTOFD);
834
835                 /* Event 49: PError goes high. */
836                 r = parport_wait_peripheral (port,
837                                              PARPORT_STATUS_PAPEROUT,
838                                              PARPORT_STATUS_PAPEROUT);
839                 if (r) {
840                         printk (KERN_DEBUG "%s: PError timeout (%d) "
841                                 "in ecp_write_block_pio\n", port->name, r);
842                 }
843         }
844
845         /* Set up ECP parallel port mode.*/
846         parport_pc_data_forward (port); /* Must be in PS2 mode */
847         parport_pc_frob_control (port,
848                                  PARPORT_CONTROL_STROBE |
849                                  PARPORT_CONTROL_AUTOFD,
850                                  0);
851         r = change_mode (port, ECR_ECP); /* ECP FIFO */
852         if (r) printk (KERN_DEBUG "%s: Warning change_mode ECR_ECP failed\n", port->name);
853         port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
854
855         /* Write the data to the FIFO. */
856         if (port->dma != PARPORT_DMA_NONE)
857                 written = parport_pc_fifo_write_block_dma (port, buf, length);
858         else
859                 written = parport_pc_fifo_write_block_pio (port, buf, length);
860
861         /* Finish up. */
862         /* For some hardware we don't want to touch the mode until
863          * the FIFO is empty, so allow 4 seconds for each position
864          * in the fifo.
865          */
866         expire = jiffies + (priv->fifo_depth * (HZ * 4));
867         do {
868                 /* Wait for the FIFO to empty */
869                 r = change_mode (port, ECR_PS2);
870                 if (r != -EBUSY) {
871                         break;
872                 }
873         } while (time_before (jiffies, expire));
874         if (r == -EBUSY) {
875
876                 printk (KERN_DEBUG "%s: FIFO is stuck\n", port->name);
877
878                 /* Prevent further data transfer. */
879                 frob_set_mode (port, ECR_TST);
880
881                 /* Adjust for the contents of the FIFO. */
882                 for (written -= priv->fifo_depth; ; written++) {
883                         if (inb (ECONTROL (port)) & 0x2) {
884                                 /* Full up. */
885                                 break;
886                         }
887                         outb (0, FIFO (port));
888                 }
889
890                 /* Reset the FIFO and return to PS2 mode. */
891                 frob_set_mode (port, ECR_PS2);
892
893                 /* Host transfer recovery. */
894                 parport_pc_data_reverse (port); /* Must be in PS2 mode */
895                 udelay (5);
896                 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
897                 r = parport_wait_peripheral (port, PARPORT_STATUS_PAPEROUT, 0);
898                 if (r)
899                         printk (KERN_DEBUG "%s: PE,1 timeout (%d) "
900                                 "in ecp_write_block_pio\n", port->name, r);
901
902                 parport_frob_control (port,
903                                       PARPORT_CONTROL_INIT,
904                                       PARPORT_CONTROL_INIT);
905                 r = parport_wait_peripheral (port,
906                                              PARPORT_STATUS_PAPEROUT,
907                                              PARPORT_STATUS_PAPEROUT);
908                 if (r)
909                         printk (KERN_DEBUG "%s: PE,2 timeout (%d) "
910                                 "in ecp_write_block_pio\n", port->name, r);
911         }
912
913         r = parport_wait_peripheral (port,
914                                      PARPORT_STATUS_BUSY, 
915                                      PARPORT_STATUS_BUSY);
916         if(r)
917                 printk (KERN_DEBUG
918                         "%s: BUSY timeout (%d) in ecp_write_block_pio\n",
919                         port->name, r);
920
921         port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
922
923         return written;
924 }
925
926 size_t parport_pc_ecp_read_block_pio (struct parport *port,
927                                       void *buf, size_t length, int flags)
928 {
929         size_t left = length;
930         size_t fifofull;
931         int r;
932         const int fifo = FIFO(port);
933         const struct parport_pc_private *priv = port->physport->private_data;
934         const int fifo_depth = priv->fifo_depth;
935         char *bufp = buf;
936
937         port = port->physport;
938 DPRINTK (KERN_DEBUG "parport_pc: parport_pc_ecp_read_block_pio\n");
939 dump_parport_state ("enter fcn", port);
940
941         /* Special case: a timeout of zero means we cannot call schedule().
942          * Also if O_NONBLOCK is set then use the default implementation. */
943         if (port->cad->timeout <= PARPORT_INACTIVITY_O_NONBLOCK)
944                 return parport_ieee1284_ecp_read_data (port, buf,
945                                                        length, flags);
946
947         if (port->ieee1284.mode == IEEE1284_MODE_ECPRLE) {
948                 /* If the peripheral is allowed to send RLE compressed
949                  * data, it is possible for a byte to expand to 128
950                  * bytes in the FIFO. */
951                 fifofull = 128;
952         } else {
953                 fifofull = fifo_depth;
954         }
955
956         /* If the caller wants less than a full FIFO's worth of data,
957          * go through software emulation.  Otherwise we may have to throw
958          * away data. */
959         if (length < fifofull)
960                 return parport_ieee1284_ecp_read_data (port, buf,
961                                                        length, flags);
962
963         if (port->ieee1284.phase != IEEE1284_PH_REV_IDLE) {
964                 /* change to reverse-idle phase (must be in forward-idle) */
965
966                 /* Event 38: Set nAutoFd low (also make sure nStrobe is high) */
967                 parport_frob_control (port,
968                                       PARPORT_CONTROL_AUTOFD
969                                       | PARPORT_CONTROL_STROBE,
970                                       PARPORT_CONTROL_AUTOFD);
971                 parport_pc_data_reverse (port); /* Must be in PS2 mode */
972                 udelay (5);
973                 /* Event 39: Set nInit low to initiate bus reversal */
974                 parport_frob_control (port,
975                                       PARPORT_CONTROL_INIT,
976                                       0);
977                 /* Event 40: Wait for  nAckReverse (PError) to go low */
978                 r = parport_wait_peripheral (port, PARPORT_STATUS_PAPEROUT, 0);
979                 if (r) {
980                         printk (KERN_DEBUG "%s: PE timeout Event 40 (%d) "
981                                 "in ecp_read_block_pio\n", port->name, r);
982                         return 0;
983                 }
984         }
985
986         /* Set up ECP FIFO mode.*/
987 /*      parport_pc_frob_control (port,
988                                  PARPORT_CONTROL_STROBE |
989                                  PARPORT_CONTROL_AUTOFD,
990                                  PARPORT_CONTROL_AUTOFD); */
991         r = change_mode (port, ECR_ECP); /* ECP FIFO */
992         if (r) printk (KERN_DEBUG "%s: Warning change_mode ECR_ECP failed\n", port->name);
993
994         port->ieee1284.phase = IEEE1284_PH_REV_DATA;
995
996         /* the first byte must be collected manually */
997 dump_parport_state ("pre 43", port);
998         /* Event 43: Wait for nAck to go low */
999         r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
1000         if (r) {
1001                 /* timed out while reading -- no data */
1002                 printk (KERN_DEBUG "PIO read timed out (initial byte)\n");
1003                 goto out_no_data;
1004         }
1005         /* read byte */
1006         *bufp++ = inb (DATA (port));
1007         left--;
1008 dump_parport_state ("43-44", port);
1009         /* Event 44: nAutoFd (HostAck) goes high to acknowledge */
1010         parport_pc_frob_control (port,
1011                                  PARPORT_CONTROL_AUTOFD,
1012                                  0);
1013 dump_parport_state ("pre 45", port);
1014         /* Event 45: Wait for nAck to go high */
1015 /*      r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, PARPORT_STATUS_ACK); */
1016 dump_parport_state ("post 45", port);
1017 r = 0;
1018         if (r) {
1019                 /* timed out while waiting for peripheral to respond to ack */
1020                 printk (KERN_DEBUG "ECP PIO read timed out (waiting for nAck)\n");
1021
1022                 /* keep hold of the byte we've got already */
1023                 goto out_no_data;
1024         }
1025         /* Event 46: nAutoFd (HostAck) goes low to accept more data */
1026         parport_pc_frob_control (port,
1027                                  PARPORT_CONTROL_AUTOFD,
1028                                  PARPORT_CONTROL_AUTOFD);
1029
1030
1031 dump_parport_state ("rev idle", port);
1032         /* Do the transfer. */
1033         while (left > fifofull) {
1034                 int ret;
1035                 unsigned long expire = jiffies + port->cad->timeout;
1036                 unsigned char ecrval = inb (ECONTROL (port));
1037
1038                 if (need_resched() && time_before (jiffies, expire))
1039                         /* Can't yield the port. */
1040                         schedule ();
1041
1042                 /* At this point, the FIFO may already be full. In
1043                  * that case ECP is already holding back the
1044                  * peripheral (assuming proper design) with a delayed
1045                  * handshake.  Work fast to avoid a peripheral
1046                  * timeout.  */
1047
1048                 if (ecrval & 0x01) {
1049                         /* FIFO is empty. Wait for interrupt. */
1050 dump_parport_state ("FIFO empty", port);
1051
1052                         /* Anyone else waiting for the port? */
1053                         if (port->waithead) {
1054                                 printk (KERN_DEBUG "Somebody wants the port\n");
1055                                 break;
1056                         }
1057
1058                         /* Clear serviceIntr */
1059                         ECR_WRITE (port, ecrval & ~(1<<2));
1060                 false_alarm:
1061 dump_parport_state ("waiting", port);
1062                         ret = parport_wait_event (port, HZ);
1063 DPRINTK (KERN_DEBUG "parport_wait_event returned %d\n", ret);
1064                         if (ret < 0)
1065                                 break;
1066                         ret = 0;
1067                         if (!time_before (jiffies, expire)) {
1068                                 /* Timed out. */
1069 dump_parport_state ("timeout", port);
1070                                 printk (KERN_DEBUG "PIO read timed out\n");
1071                                 break;
1072                         }
1073                         ecrval = inb (ECONTROL (port));
1074                         if (!(ecrval & (1<<2))) {
1075                                 if (need_resched() &&
1076                                     time_before (jiffies, expire)) {
1077                                         schedule ();
1078                                 }
1079                                 goto false_alarm;
1080                         }
1081
1082                         /* Depending on how the FIFO threshold was
1083                          * set, how long interrupt service took, and
1084                          * how fast the peripheral is, we might be
1085                          * lucky and have a just filled FIFO. */
1086                         continue;
1087                 }
1088
1089                 if (ecrval & 0x02) {
1090                         /* FIFO is full. */
1091 dump_parport_state ("FIFO full", port);
1092                         insb (fifo, bufp, fifo_depth);
1093                         bufp += fifo_depth;
1094                         left -= fifo_depth;
1095                         continue;
1096                 }
1097
1098 DPRINTK (KERN_DEBUG "*** ecp_read_block_pio: reading one byte from the FIFO\n");
1099
1100                 /* FIFO not filled.  We will cycle this loop for a while
1101                  * and either the peripheral will fill it faster,
1102                  * tripping a fast empty with insb, or we empty it. */
1103                 *bufp++ = inb (fifo);
1104                 left--;
1105         }
1106
1107         /* scoop up anything left in the FIFO */
1108         while (left && !(inb (ECONTROL (port) & 0x01))) {
1109                 *bufp++ = inb (fifo);
1110                 left--;
1111         }
1112
1113         port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
1114 dump_parport_state ("rev idle2", port);
1115
1116 out_no_data:
1117
1118         /* Go to forward idle mode to shut the peripheral up (event 47). */
1119         parport_frob_control (port, PARPORT_CONTROL_INIT, PARPORT_CONTROL_INIT);
1120
1121         /* event 49: PError goes high */
1122         r = parport_wait_peripheral (port,
1123                                      PARPORT_STATUS_PAPEROUT,
1124                                      PARPORT_STATUS_PAPEROUT);
1125         if (r) {
1126                 printk (KERN_DEBUG
1127                         "%s: PE timeout FWDIDLE (%d) in ecp_read_block_pio\n",
1128                         port->name, r);
1129         }
1130
1131         port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
1132
1133         /* Finish up. */
1134         {
1135                 int lost = get_fifo_residue (port);
1136                 if (lost)
1137                         /* Shouldn't happen with compliant peripherals. */
1138                         printk (KERN_DEBUG "%s: DATA LOSS (%d bytes)!\n",
1139                                 port->name, lost);
1140         }
1141
1142 dump_parport_state ("fwd idle", port);
1143         return length - left;
1144 }
1145
1146 #endif /* IEEE 1284 support */
1147 #endif /* Allowed to use FIFO/DMA */
1148
1149
1150 /*
1151  *      ******************************************
1152  *      INITIALISATION AND MODULE STUFF BELOW HERE
1153  *      ******************************************
1154  */
1155
1156 /* GCC is not inlining extern inline function later overwriten to non-inline,
1157    so we use outlined_ variants here.  */
1158 struct parport_operations parport_pc_ops = 
1159 {
1160         .write_data     = parport_pc_write_data,
1161         .read_data      = parport_pc_read_data,
1162
1163         .write_control  = parport_pc_write_control,
1164         .read_control   = parport_pc_read_control,
1165         .frob_control   = parport_pc_frob_control,
1166
1167         .read_status    = parport_pc_read_status,
1168
1169         .enable_irq     = parport_pc_enable_irq,
1170         .disable_irq    = parport_pc_disable_irq,
1171
1172         .data_forward   = parport_pc_data_forward,
1173         .data_reverse   = parport_pc_data_reverse,
1174
1175         .init_state     = parport_pc_init_state,
1176         .save_state     = parport_pc_save_state,
1177         .restore_state  = parport_pc_restore_state,
1178
1179         .epp_write_data = parport_ieee1284_epp_write_data,
1180         .epp_read_data  = parport_ieee1284_epp_read_data,
1181         .epp_write_addr = parport_ieee1284_epp_write_addr,
1182         .epp_read_addr  = parport_ieee1284_epp_read_addr,
1183
1184         .ecp_write_data = parport_ieee1284_ecp_write_data,
1185         .ecp_read_data  = parport_ieee1284_ecp_read_data,
1186         .ecp_write_addr = parport_ieee1284_ecp_write_addr,
1187
1188         .compat_write_data      = parport_ieee1284_write_compat,
1189         .nibble_read_data       = parport_ieee1284_read_nibble,
1190         .byte_read_data         = parport_ieee1284_read_byte,
1191
1192         .owner          = THIS_MODULE,
1193 };
1194
1195 #ifdef CONFIG_PARPORT_PC_SUPERIO
1196 /* Super-IO chipset detection, Winbond, SMSC */
1197 static void __devinit show_parconfig_smsc37c669(int io, int key)
1198 {
1199         int cr1,cr4,cra,cr23,cr26,cr27,i=0;
1200         static const char *modes[]={ "SPP and Bidirectional (PS/2)",    
1201                                      "EPP and SPP",
1202                                      "ECP",
1203                                      "ECP and EPP" };
1204
1205         outb(key,io);
1206         outb(key,io);
1207         outb(1,io);
1208         cr1=inb(io+1);
1209         outb(4,io);
1210         cr4=inb(io+1);
1211         outb(0x0a,io);
1212         cra=inb(io+1);
1213         outb(0x23,io);
1214         cr23=inb(io+1);
1215         outb(0x26,io);
1216         cr26=inb(io+1);
1217         outb(0x27,io);
1218         cr27=inb(io+1);
1219         outb(0xaa,io);
1220
1221         if (verbose_probing) {
1222                 printk (KERN_INFO "SMSC 37c669 LPT Config: cr_1=0x%02x, 4=0x%02x, "
1223                         "A=0x%2x, 23=0x%02x, 26=0x%02x, 27=0x%02x\n",
1224                         cr1,cr4,cra,cr23,cr26,cr27);
1225                 
1226                 /* The documentation calls DMA and IRQ-Lines by letters, so
1227                    the board maker can/will wire them
1228                    appropriately/randomly...  G=reserved H=IDE-irq, */
1229                 printk (KERN_INFO "SMSC LPT Config: io=0x%04x, irq=%c, dma=%c, "
1230                         "fifo threshold=%d\n", cr23*4,
1231                         (cr27 &0x0f) ? 'A'-1+(cr27 &0x0f): '-',
1232                         (cr26 &0x0f) ? 'A'-1+(cr26 &0x0f): '-', cra & 0x0f);
1233                 printk(KERN_INFO "SMSC LPT Config: enabled=%s power=%s\n",
1234                        (cr23*4 >=0x100) ?"yes":"no", (cr1 & 4) ? "yes" : "no");
1235                 printk(KERN_INFO "SMSC LPT Config: Port mode=%s, EPP version =%s\n",
1236                        (cr1 & 0x08 ) ? "Standard mode only (SPP)" : modes[cr4 & 0x03], 
1237                        (cr4 & 0x40) ? "1.7" : "1.9");
1238         }
1239                 
1240         /* Heuristics !  BIOS setup for this mainboard device limits
1241            the choices to standard settings, i.e. io-address and IRQ
1242            are related, however DMA can be 1 or 3, assume DMA_A=DMA1,
1243            DMA_C=DMA3 (this is true e.g. for TYAN 1564D Tomcat IV) */
1244         if(cr23*4 >=0x100) { /* if active */
1245                 while((superios[i].io!= 0) && (i<NR_SUPERIOS))
1246                         i++;
1247                 if(i==NR_SUPERIOS)
1248                         printk(KERN_INFO "Super-IO: too many chips!\n");
1249                 else {
1250                         int d;
1251                         switch (cr23*4) {
1252                                 case 0x3bc:
1253                                         superios[i].io = 0x3bc;
1254                                         superios[i].irq = 7;
1255                                         break;
1256                                 case 0x378:
1257                                         superios[i].io = 0x378;
1258                                         superios[i].irq = 7;
1259                                         break;
1260                                 case 0x278:
1261                                         superios[i].io = 0x278;
1262                                         superios[i].irq = 5;
1263                         }
1264                         d=(cr26 &0x0f);
1265                         if((d==1) || (d==3)) 
1266                                 superios[i].dma= d;
1267                         else
1268                                 superios[i].dma= PARPORT_DMA_NONE;
1269                 }
1270         }
1271 }
1272
1273
1274 static void __devinit show_parconfig_winbond(int io, int key)
1275 {
1276         int cr30,cr60,cr61,cr70,cr74,crf0,i=0;
1277         static const char *modes[] = {
1278                 "Standard (SPP) and Bidirectional(PS/2)", /* 0 */
1279                 "EPP-1.9 and SPP",
1280                 "ECP",
1281                 "ECP and EPP-1.9",
1282                 "Standard (SPP)",
1283                 "EPP-1.7 and SPP",              /* 5 */
1284                 "undefined!",
1285                 "ECP and EPP-1.7" };
1286         static char *irqtypes[] = { "pulsed low, high-Z", "follows nACK" };
1287                 
1288         /* The registers are called compatible-PnP because the
1289            register layout is modelled after ISA-PnP, the access
1290            method is just another ... */
1291         outb(key,io);
1292         outb(key,io);
1293         outb(0x07,io);   /* Register 7: Select Logical Device */
1294         outb(0x01,io+1); /* LD1 is Parallel Port */
1295         outb(0x30,io);
1296         cr30=inb(io+1);
1297         outb(0x60,io);
1298         cr60=inb(io+1);
1299         outb(0x61,io);
1300         cr61=inb(io+1);
1301         outb(0x70,io);
1302         cr70=inb(io+1);
1303         outb(0x74,io);
1304         cr74=inb(io+1);
1305         outb(0xf0,io);
1306         crf0=inb(io+1);
1307         outb(0xaa,io);
1308
1309         if (verbose_probing) {
1310                 printk(KERN_INFO "Winbond LPT Config: cr_30=%02x 60,61=%02x%02x "
1311                        "70=%02x 74=%02x, f0=%02x\n", cr30,cr60,cr61,cr70,cr74,crf0);
1312                 printk(KERN_INFO "Winbond LPT Config: active=%s, io=0x%02x%02x irq=%d, ", 
1313                        (cr30 & 0x01) ? "yes":"no", cr60,cr61,cr70&0x0f );
1314                 if ((cr74 & 0x07) > 3)
1315                         printk("dma=none\n");
1316                 else
1317                         printk("dma=%d\n",cr74 & 0x07);
1318                 printk(KERN_INFO "Winbond LPT Config: irqtype=%s, ECP fifo threshold=%d\n",
1319                        irqtypes[crf0>>7], (crf0>>3)&0x0f);
1320                 printk(KERN_INFO "Winbond LPT Config: Port mode=%s\n", modes[crf0 & 0x07]);
1321         }
1322
1323         if(cr30 & 0x01) { /* the settings can be interrogated later ... */
1324                 while((superios[i].io!= 0) && (i<NR_SUPERIOS))
1325                         i++;
1326                 if(i==NR_SUPERIOS) 
1327                         printk(KERN_INFO "Super-IO: too many chips!\n");
1328                 else {
1329                         superios[i].io = (cr60<<8)|cr61;
1330                         superios[i].irq = cr70&0x0f;
1331                         superios[i].dma = (((cr74 & 0x07) > 3) ?
1332                                            PARPORT_DMA_NONE : (cr74 & 0x07));
1333                 }
1334         }
1335 }
1336
1337 static void __devinit decode_winbond(int efer, int key, int devid, int devrev, int oldid)
1338 {
1339         const char *type = "unknown";
1340         int id,progif=2;
1341
1342         if (devid == devrev)
1343                 /* simple heuristics, we happened to read some
1344                    non-winbond register */
1345                 return;
1346
1347         id=(devid<<8) | devrev;
1348
1349         /* Values are from public data sheets pdf files, I can just
1350            confirm 83977TF is correct :-) */
1351         if      (id == 0x9771) type="83977F/AF";
1352         else if (id == 0x9773) type="83977TF / SMSC 97w33x/97w34x";
1353         else if (id == 0x9774) type="83977ATF";
1354         else if ((id & ~0x0f) == 0x5270) type="83977CTF / SMSC 97w36x";
1355         else if ((id & ~0x0f) == 0x52f0) type="83977EF / SMSC 97w35x";
1356         else if ((id & ~0x0f) == 0x5210) type="83627";
1357         else if ((id & ~0x0f) == 0x6010) type="83697HF";
1358         else if ((oldid &0x0f ) == 0x0a) { type="83877F"; progif=1;}
1359         else if ((oldid &0x0f ) == 0x0b) { type="83877AF"; progif=1;}
1360         else if ((oldid &0x0f ) == 0x0c) { type="83877TF"; progif=1;}
1361         else if ((oldid &0x0f ) == 0x0d) { type="83877ATF"; progif=1;}
1362         else progif=0;
1363
1364         if (verbose_probing)
1365                 printk(KERN_INFO "Winbond chip at EFER=0x%x key=0x%02x "
1366                        "devid=%02x devrev=%02x oldid=%02x type=%s\n", 
1367                        efer, key, devid, devrev, oldid, type);
1368
1369         if (progif == 2)
1370                 show_parconfig_winbond(efer,key);
1371 }
1372
1373 static void __devinit decode_smsc(int efer, int key, int devid, int devrev)
1374 {
1375         const char *type = "unknown";
1376         void (*func)(int io, int key);
1377         int id;
1378
1379         if (devid == devrev)
1380                 /* simple heuristics, we happened to read some
1381                    non-smsc register */
1382                 return;
1383
1384         func=NULL;
1385         id=(devid<<8) | devrev;
1386
1387         if      (id==0x0302) {type="37c669"; func=show_parconfig_smsc37c669;}
1388         else if (id==0x6582) type="37c665IR";
1389         else if (devid==0x65) type="37c665GT";
1390         else if (devid==0x66) type="37c666GT";
1391
1392         if (verbose_probing)
1393                 printk(KERN_INFO "SMSC chip at EFER=0x%x "
1394                        "key=0x%02x devid=%02x devrev=%02x type=%s\n",
1395                        efer, key, devid, devrev, type);
1396
1397         if (func)
1398                 func(efer,key);
1399 }
1400
1401
1402 static void __devinit winbond_check(int io, int key)
1403 {
1404         int devid,devrev,oldid,x_devid,x_devrev,x_oldid;
1405
1406         if (!request_region(io, 3, __FUNCTION__))
1407                 return;
1408
1409         /* First probe without key */
1410         outb(0x20,io);
1411         x_devid=inb(io+1);
1412         outb(0x21,io);
1413         x_devrev=inb(io+1);
1414         outb(0x09,io);
1415         x_oldid=inb(io+1);
1416
1417         outb(key,io);
1418         outb(key,io);     /* Write Magic Sequence to EFER, extended
1419                              funtion enable register */
1420         outb(0x20,io);    /* Write EFIR, extended function index register */
1421         devid=inb(io+1);  /* Read EFDR, extended function data register */
1422         outb(0x21,io);
1423         devrev=inb(io+1);
1424         outb(0x09,io);
1425         oldid=inb(io+1);
1426         outb(0xaa,io);    /* Magic Seal */
1427
1428         if ((x_devid == devid) && (x_devrev == devrev) && (x_oldid == oldid))
1429                 goto out; /* protection against false positives */
1430
1431         decode_winbond(io,key,devid,devrev,oldid);
1432 out:
1433         release_region(io, 3);
1434 }
1435
1436 static void __devinit winbond_check2(int io,int key)
1437 {
1438         int devid,devrev,oldid,x_devid,x_devrev,x_oldid;
1439
1440         if (!request_region(io, 3, __FUNCTION__))
1441                 return;
1442
1443         /* First probe without the key */
1444         outb(0x20,io+2);
1445         x_devid=inb(io+2);
1446         outb(0x21,io+1);
1447         x_devrev=inb(io+2);
1448         outb(0x09,io+1);
1449         x_oldid=inb(io+2);
1450
1451         outb(key,io);     /* Write Magic Byte to EFER, extended
1452                              funtion enable register */
1453         outb(0x20,io+2);  /* Write EFIR, extended function index register */
1454         devid=inb(io+2);  /* Read EFDR, extended function data register */
1455         outb(0x21,io+1);
1456         devrev=inb(io+2);
1457         outb(0x09,io+1);
1458         oldid=inb(io+2);
1459         outb(0xaa,io);    /* Magic Seal */
1460
1461         if ((x_devid == devid) && (x_devrev == devrev) && (x_oldid == oldid))
1462                 goto out; /* protection against false positives */
1463
1464         decode_winbond(io,key,devid,devrev,oldid);
1465 out:
1466         release_region(io, 3);
1467 }
1468
1469 static void __devinit smsc_check(int io, int key)
1470 {
1471         int id,rev,oldid,oldrev,x_id,x_rev,x_oldid,x_oldrev;
1472
1473         if (!request_region(io, 3, __FUNCTION__))
1474                 return;
1475
1476         /* First probe without the key */
1477         outb(0x0d,io);
1478         x_oldid=inb(io+1);
1479         outb(0x0e,io);
1480         x_oldrev=inb(io+1);
1481         outb(0x20,io);
1482         x_id=inb(io+1);
1483         outb(0x21,io);
1484         x_rev=inb(io+1);
1485
1486         outb(key,io);
1487         outb(key,io);     /* Write Magic Sequence to EFER, extended
1488                              funtion enable register */
1489         outb(0x0d,io);    /* Write EFIR, extended function index register */
1490         oldid=inb(io+1);  /* Read EFDR, extended function data register */
1491         outb(0x0e,io);
1492         oldrev=inb(io+1);
1493         outb(0x20,io);
1494         id=inb(io+1);
1495         outb(0x21,io);
1496         rev=inb(io+1);
1497         outb(0xaa,io);    /* Magic Seal */
1498
1499         if ((x_id == id) && (x_oldrev == oldrev) &&
1500             (x_oldid == oldid) && (x_rev == rev))
1501                 goto out; /* protection against false positives */
1502
1503         decode_smsc(io,key,oldid,oldrev);
1504 out:
1505         release_region(io, 3);
1506 }
1507
1508
1509 static void __devinit detect_and_report_winbond (void)
1510
1511         if (verbose_probing)
1512                 printk(KERN_DEBUG "Winbond Super-IO detection, now testing ports 3F0,370,250,4E,2E ...\n");
1513         winbond_check(0x3f0,0x87);
1514         winbond_check(0x370,0x87);
1515         winbond_check(0x2e ,0x87);
1516         winbond_check(0x4e ,0x87);
1517         winbond_check(0x3f0,0x86);
1518         winbond_check2(0x250,0x88); 
1519         winbond_check2(0x250,0x89);
1520 }
1521
1522 static void __devinit detect_and_report_smsc (void)
1523 {
1524         if (verbose_probing)
1525                 printk(KERN_DEBUG "SMSC Super-IO detection, now testing Ports 2F0, 370 ...\n");
1526         smsc_check(0x3f0,0x55);
1527         smsc_check(0x370,0x55);
1528         smsc_check(0x3f0,0x44);
1529         smsc_check(0x370,0x44);
1530 }
1531 #endif /* CONFIG_PARPORT_PC_SUPERIO */
1532
1533 static int __devinit get_superio_dma (struct parport *p)
1534 {
1535         int i=0;
1536         while( (superios[i].io != p->base) && (i<NR_SUPERIOS))
1537                 i++;
1538         if (i!=NR_SUPERIOS)
1539                 return superios[i].dma;
1540         return PARPORT_DMA_NONE;
1541 }
1542
1543 static int __devinit get_superio_irq (struct parport *p)
1544 {
1545         int i=0;
1546         while( (superios[i].io != p->base) && (i<NR_SUPERIOS))
1547                 i++;
1548         if (i!=NR_SUPERIOS)
1549                 return superios[i].irq;
1550         return PARPORT_IRQ_NONE;
1551 }
1552         
1553
1554 /* --- Mode detection ------------------------------------- */
1555
1556 /*
1557  * Checks for port existence, all ports support SPP MODE
1558  * Returns: 
1559  *         0           :  No parallel port at this address
1560  *  PARPORT_MODE_PCSPP :  SPP port detected 
1561  *                        (if the user specified an ioport himself,
1562  *                         this shall always be the case!)
1563  *
1564  */
1565 static int __devinit parport_SPP_supported(struct parport *pb)
1566 {
1567         unsigned char r, w;
1568
1569         /*
1570          * first clear an eventually pending EPP timeout 
1571          * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
1572          * that does not even respond to SPP cycles if an EPP
1573          * timeout is pending
1574          */
1575         clear_epp_timeout(pb);
1576
1577         /* Do a simple read-write test to make sure the port exists. */
1578         w = 0xc;
1579         outb (w, CONTROL (pb));
1580
1581         /* Is there a control register that we can read from?  Some
1582          * ports don't allow reads, so read_control just returns a
1583          * software copy. Some ports _do_ allow reads, so bypass the
1584          * software copy here.  In addition, some bits aren't
1585          * writable. */
1586         r = inb (CONTROL (pb));
1587         if ((r & 0xf) == w) {
1588                 w = 0xe;
1589                 outb (w, CONTROL (pb));
1590                 r = inb (CONTROL (pb));
1591                 outb (0xc, CONTROL (pb));
1592                 if ((r & 0xf) == w)
1593                         return PARPORT_MODE_PCSPP;
1594         }
1595
1596         if (user_specified)
1597                 /* That didn't work, but the user thinks there's a
1598                  * port here. */
1599                 printk (KERN_INFO "parport 0x%lx (WARNING): CTR: "
1600                         "wrote 0x%02x, read 0x%02x\n", pb->base, w, r);
1601
1602         /* Try the data register.  The data lines aren't tri-stated at
1603          * this stage, so we expect back what we wrote. */
1604         w = 0xaa;
1605         parport_pc_write_data (pb, w);
1606         r = parport_pc_read_data (pb);
1607         if (r == w) {
1608                 w = 0x55;
1609                 parport_pc_write_data (pb, w);
1610                 r = parport_pc_read_data (pb);
1611                 if (r == w)
1612                         return PARPORT_MODE_PCSPP;
1613         }
1614
1615         if (user_specified) {
1616                 /* Didn't work, but the user is convinced this is the
1617                  * place. */
1618                 printk (KERN_INFO "parport 0x%lx (WARNING): DATA: "
1619                         "wrote 0x%02x, read 0x%02x\n", pb->base, w, r);
1620                 printk (KERN_INFO "parport 0x%lx: You gave this address, "
1621                         "but there is probably no parallel port there!\n",
1622                         pb->base);
1623         }
1624
1625         /* It's possible that we can't read the control register or
1626          * the data register.  In that case just believe the user. */
1627         if (user_specified)
1628                 return PARPORT_MODE_PCSPP;
1629
1630         return 0;
1631 }
1632
1633 /* Check for ECR
1634  *
1635  * Old style XT ports alias io ports every 0x400, hence accessing ECR
1636  * on these cards actually accesses the CTR.
1637  *
1638  * Modern cards don't do this but reading from ECR will return 0xff
1639  * regardless of what is written here if the card does NOT support
1640  * ECP.
1641  *
1642  * We first check to see if ECR is the same as CTR.  If not, the low
1643  * two bits of ECR aren't writable, so we check by writing ECR and
1644  * reading it back to see if it's what we expect.
1645  */
1646 static int __devinit parport_ECR_present(struct parport *pb)
1647 {
1648         struct parport_pc_private *priv = pb->private_data;
1649         unsigned char r = 0xc;
1650
1651         outb (r, CONTROL (pb));
1652         if ((inb (ECONTROL (pb)) & 0x3) == (r & 0x3)) {
1653                 outb (r ^ 0x2, CONTROL (pb)); /* Toggle bit 1 */
1654
1655                 r = inb (CONTROL (pb));
1656                 if ((inb (ECONTROL (pb)) & 0x2) == (r & 0x2))
1657                         goto no_reg; /* Sure that no ECR register exists */
1658         }
1659         
1660         if ((inb (ECONTROL (pb)) & 0x3 ) != 0x1)
1661                 goto no_reg;
1662
1663         ECR_WRITE (pb, 0x34);
1664         if (inb (ECONTROL (pb)) != 0x35)
1665                 goto no_reg;
1666
1667         priv->ecr = 1;
1668         outb (0xc, CONTROL (pb));
1669         
1670         /* Go to mode 000 */
1671         frob_set_mode (pb, ECR_SPP);
1672
1673         return 1;
1674
1675  no_reg:
1676         outb (0xc, CONTROL (pb));
1677         return 0; 
1678 }
1679
1680 #ifdef CONFIG_PARPORT_1284
1681 /* Detect PS/2 support.
1682  *
1683  * Bit 5 (0x20) sets the PS/2 data direction; setting this high
1684  * allows us to read data from the data lines.  In theory we would get back
1685  * 0xff but any peripheral attached to the port may drag some or all of the
1686  * lines down to zero.  So if we get back anything that isn't the contents
1687  * of the data register we deem PS/2 support to be present. 
1688  *
1689  * Some SPP ports have "half PS/2" ability - you can't turn off the line
1690  * drivers, but an external peripheral with sufficiently beefy drivers of
1691  * its own can overpower them and assert its own levels onto the bus, from
1692  * where they can then be read back as normal.  Ports with this property
1693  * and the right type of device attached are likely to fail the SPP test,
1694  * (as they will appear to have stuck bits) and so the fact that they might
1695  * be misdetected here is rather academic. 
1696  */
1697
1698 static int __devinit parport_PS2_supported(struct parport *pb)
1699 {
1700         int ok = 0;
1701   
1702         clear_epp_timeout(pb);
1703
1704         /* try to tri-state the buffer */
1705         parport_pc_data_reverse (pb);
1706         
1707         parport_pc_write_data(pb, 0x55);
1708         if (parport_pc_read_data(pb) != 0x55) ok++;
1709
1710         parport_pc_write_data(pb, 0xaa);
1711         if (parport_pc_read_data(pb) != 0xaa) ok++;
1712
1713         /* cancel input mode */
1714         parport_pc_data_forward (pb);
1715
1716         if (ok) {
1717                 pb->modes |= PARPORT_MODE_TRISTATE;
1718         } else {
1719                 struct parport_pc_private *priv = pb->private_data;
1720                 priv->ctr_writable &= ~0x20;
1721         }
1722
1723         return ok;
1724 }
1725
1726 #ifdef CONFIG_PARPORT_PC_FIFO
1727 static int __devinit parport_ECP_supported(struct parport *pb)
1728 {
1729         int i;
1730         int config, configb;
1731         int pword;
1732         struct parport_pc_private *priv = pb->private_data;
1733         /* Translate ECP intrLine to ISA irq value */   
1734         static const int intrline[]= { 0, 7, 9, 10, 11, 14, 15, 5 }; 
1735
1736         /* If there is no ECR, we have no hope of supporting ECP. */
1737         if (!priv->ecr)
1738                 return 0;
1739
1740         /* Find out FIFO depth */
1741         ECR_WRITE (pb, ECR_SPP << 5); /* Reset FIFO */
1742         ECR_WRITE (pb, ECR_TST << 5); /* TEST FIFO */
1743         for (i=0; i < 1024 && !(inb (ECONTROL (pb)) & 0x02); i++)
1744                 outb (0xaa, FIFO (pb));
1745
1746         /*
1747          * Using LGS chipset it uses ECR register, but
1748          * it doesn't support ECP or FIFO MODE
1749          */
1750         if (i == 1024) {
1751                 ECR_WRITE (pb, ECR_SPP << 5);
1752                 return 0;
1753         }
1754
1755         priv->fifo_depth = i;
1756         if (verbose_probing)
1757                 printk (KERN_DEBUG "0x%lx: FIFO is %d bytes\n", pb->base, i);
1758
1759         /* Find out writeIntrThreshold */
1760         frob_econtrol (pb, 1<<2, 1<<2);
1761         frob_econtrol (pb, 1<<2, 0);
1762         for (i = 1; i <= priv->fifo_depth; i++) {
1763                 inb (FIFO (pb));
1764                 udelay (50);
1765                 if (inb (ECONTROL (pb)) & (1<<2))
1766                         break;
1767         }
1768
1769         if (i <= priv->fifo_depth) {
1770                 if (verbose_probing)
1771                         printk (KERN_DEBUG "0x%lx: writeIntrThreshold is %d\n",
1772                                 pb->base, i);
1773         } else
1774                 /* Number of bytes we know we can write if we get an
1775                    interrupt. */
1776                 i = 0;
1777
1778         priv->writeIntrThreshold = i;
1779
1780         /* Find out readIntrThreshold */
1781         frob_set_mode (pb, ECR_PS2); /* Reset FIFO and enable PS2 */
1782         parport_pc_data_reverse (pb); /* Must be in PS2 mode */
1783         frob_set_mode (pb, ECR_TST); /* Test FIFO */
1784         frob_econtrol (pb, 1<<2, 1<<2);
1785         frob_econtrol (pb, 1<<2, 0);
1786         for (i = 1; i <= priv->fifo_depth; i++) {
1787                 outb (0xaa, FIFO (pb));
1788                 if (inb (ECONTROL (pb)) & (1<<2))
1789                         break;
1790         }
1791
1792         if (i <= priv->fifo_depth) {
1793                 if (verbose_probing)
1794                         printk (KERN_INFO "0x%lx: readIntrThreshold is %d\n",
1795                                 pb->base, i);
1796         } else
1797                 /* Number of bytes we can read if we get an interrupt. */
1798                 i = 0;
1799
1800         priv->readIntrThreshold = i;
1801
1802         ECR_WRITE (pb, ECR_SPP << 5); /* Reset FIFO */
1803         ECR_WRITE (pb, 0xf4); /* Configuration mode */
1804         config = inb (CONFIGA (pb));
1805         pword = (config >> 4) & 0x7;
1806         switch (pword) {
1807         case 0:
1808                 pword = 2;
1809                 printk (KERN_WARNING "0x%lx: Unsupported pword size!\n",
1810                         pb->base);
1811                 break;
1812         case 2:
1813                 pword = 4;
1814                 printk (KERN_WARNING "0x%lx: Unsupported pword size!\n",
1815                         pb->base);
1816                 break;
1817         default:
1818                 printk (KERN_WARNING "0x%lx: Unknown implementation ID\n",
1819                         pb->base);
1820                 /* Assume 1 */
1821         case 1:
1822                 pword = 1;
1823         }
1824         priv->pword = pword;
1825
1826         if (verbose_probing) {
1827                 printk (KERN_DEBUG "0x%lx: PWord is %d bits\n", pb->base, 8 * pword);
1828                 
1829                 printk (KERN_DEBUG "0x%lx: Interrupts are ISA-%s\n", pb->base,
1830                         config & 0x80 ? "Level" : "Pulses");
1831
1832                 configb = inb (CONFIGB (pb));
1833                 printk (KERN_DEBUG "0x%lx: ECP port cfgA=0x%02x cfgB=0x%02x\n",
1834                         pb->base, config, configb);
1835                 printk (KERN_DEBUG "0x%lx: ECP settings irq=", pb->base);
1836                 if ((configb >>3) & 0x07)
1837                         printk("%d",intrline[(configb >>3) & 0x07]);
1838                 else
1839                         printk("<none or set by other means>");
1840                 printk (" dma=");
1841                 if( (configb & 0x03 ) == 0x00)
1842                         printk("<none or set by other means>\n");
1843                 else
1844                         printk("%d\n",configb & 0x07);
1845         }
1846
1847         /* Go back to mode 000 */
1848         frob_set_mode (pb, ECR_SPP);
1849
1850         return 1;
1851 }
1852 #endif
1853
1854 static int __devinit parport_ECPPS2_supported(struct parport *pb)
1855 {
1856         const struct parport_pc_private *priv = pb->private_data;
1857         int result;
1858         unsigned char oecr;
1859
1860         if (!priv->ecr)
1861                 return 0;
1862
1863         oecr = inb (ECONTROL (pb));
1864         ECR_WRITE (pb, ECR_PS2 << 5);
1865         result = parport_PS2_supported(pb);
1866         ECR_WRITE (pb, oecr);
1867         return result;
1868 }
1869
1870 /* EPP mode detection  */
1871
1872 static int __devinit parport_EPP_supported(struct parport *pb)
1873 {
1874         const struct parport_pc_private *priv = pb->private_data;
1875
1876         /*
1877          * Theory:
1878          *      Bit 0 of STR is the EPP timeout bit, this bit is 0
1879          *      when EPP is possible and is set high when an EPP timeout
1880          *      occurs (EPP uses the HALT line to stop the CPU while it does
1881          *      the byte transfer, an EPP timeout occurs if the attached
1882          *      device fails to respond after 10 micro seconds).
1883          *
1884          *      This bit is cleared by either reading it (National Semi)
1885          *      or writing a 1 to the bit (SMC, UMC, WinBond), others ???
1886          *      This bit is always high in non EPP modes.
1887          */
1888
1889         /* If EPP timeout bit clear then EPP available */
1890         if (!clear_epp_timeout(pb)) {
1891                 return 0;  /* No way to clear timeout */
1892         }
1893
1894         /* Check for Intel bug. */
1895         if (priv->ecr) {
1896                 unsigned char i;
1897                 for (i = 0x00; i < 0x80; i += 0x20) {
1898                         ECR_WRITE (pb, i);
1899                         if (clear_epp_timeout (pb)) {
1900                                 /* Phony EPP in ECP. */
1901                                 return 0;
1902                         }
1903                 }
1904         }
1905
1906         pb->modes |= PARPORT_MODE_EPP;
1907
1908         /* Set up access functions to use EPP hardware. */
1909         pb->ops->epp_read_data = parport_pc_epp_read_data;
1910         pb->ops->epp_write_data = parport_pc_epp_write_data;
1911         pb->ops->epp_read_addr = parport_pc_epp_read_addr;
1912         pb->ops->epp_write_addr = parport_pc_epp_write_addr;
1913
1914         return 1;
1915 }
1916
1917 static int __devinit parport_ECPEPP_supported(struct parport *pb)
1918 {
1919         struct parport_pc_private *priv = pb->private_data;
1920         int result;
1921         unsigned char oecr;
1922
1923         if (!priv->ecr) {
1924                 return 0;
1925         }
1926
1927         oecr = inb (ECONTROL (pb));
1928         /* Search for SMC style EPP+ECP mode */
1929         ECR_WRITE (pb, 0x80);
1930         outb (0x04, CONTROL (pb));
1931         result = parport_EPP_supported(pb);
1932
1933         ECR_WRITE (pb, oecr);
1934
1935         if (result) {
1936                 /* Set up access functions to use ECP+EPP hardware. */
1937                 pb->ops->epp_read_data = parport_pc_ecpepp_read_data;
1938                 pb->ops->epp_write_data = parport_pc_ecpepp_write_data;
1939                 pb->ops->epp_read_addr = parport_pc_ecpepp_read_addr;
1940                 pb->ops->epp_write_addr = parport_pc_ecpepp_write_addr;
1941         }
1942
1943         return result;
1944 }
1945
1946 #else /* No IEEE 1284 support */
1947
1948 /* Don't bother probing for modes we know we won't use. */
1949 static int __devinit parport_PS2_supported(struct parport *pb) { return 0; }
1950 #ifdef CONFIG_PARPORT_PC_FIFO
1951 static int __devinit parport_ECP_supported(struct parport *pb) { return 0; }
1952 #endif
1953 static int __devinit parport_EPP_supported(struct parport *pb) { return 0; }
1954 static int __devinit parport_ECPEPP_supported(struct parport *pb){return 0;}
1955 static int __devinit parport_ECPPS2_supported(struct parport *pb){return 0;}
1956
1957 #endif /* No IEEE 1284 support */
1958
1959 /* --- IRQ detection -------------------------------------- */
1960
1961 /* Only if supports ECP mode */
1962 static int __devinit programmable_irq_support(struct parport *pb)
1963 {
1964         int irq, intrLine;
1965         unsigned char oecr = inb (ECONTROL (pb));
1966         static const int lookup[8] = {
1967                 PARPORT_IRQ_NONE, 7, 9, 10, 11, 14, 15, 5
1968         };
1969
1970         ECR_WRITE (pb, ECR_CNF << 5); /* Configuration MODE */
1971
1972         intrLine = (inb (CONFIGB (pb)) >> 3) & 0x07;
1973         irq = lookup[intrLine];
1974
1975         ECR_WRITE (pb, oecr);
1976         return irq;
1977 }
1978
1979 static int __devinit irq_probe_ECP(struct parport *pb)
1980 {
1981         int i;
1982         unsigned long irqs;
1983
1984         irqs = probe_irq_on();
1985                 
1986         ECR_WRITE (pb, ECR_SPP << 5); /* Reset FIFO */
1987         ECR_WRITE (pb, (ECR_TST << 5) | 0x04);
1988         ECR_WRITE (pb, ECR_TST << 5);
1989
1990         /* If Full FIFO sure that writeIntrThreshold is generated */
1991         for (i=0; i < 1024 && !(inb (ECONTROL (pb)) & 0x02) ; i++) 
1992                 outb (0xaa, FIFO (pb));
1993                 
1994         pb->irq = probe_irq_off(irqs);
1995         ECR_WRITE (pb, ECR_SPP << 5);
1996
1997         if (pb->irq <= 0)
1998                 pb->irq = PARPORT_IRQ_NONE;
1999
2000         return pb->irq;
2001 }
2002
2003 /*
2004  * This detection seems that only works in National Semiconductors
2005  * This doesn't work in SMC, LGS, and Winbond 
2006  */
2007 static int __devinit irq_probe_EPP(struct parport *pb)
2008 {
2009 #ifndef ADVANCED_DETECT
2010         return PARPORT_IRQ_NONE;
2011 #else
2012         int irqs;
2013         unsigned char oecr;
2014
2015         if (pb->modes & PARPORT_MODE_PCECR)
2016                 oecr = inb (ECONTROL (pb));
2017
2018         irqs = probe_irq_on();
2019
2020         if (pb->modes & PARPORT_MODE_PCECR)
2021                 frob_econtrol (pb, 0x10, 0x10);
2022         
2023         clear_epp_timeout(pb);
2024         parport_pc_frob_control (pb, 0x20, 0x20);
2025         parport_pc_frob_control (pb, 0x10, 0x10);
2026         clear_epp_timeout(pb);
2027
2028         /* Device isn't expecting an EPP read
2029          * and generates an IRQ.
2030          */
2031         parport_pc_read_epp(pb);
2032         udelay(20);
2033
2034         pb->irq = probe_irq_off (irqs);
2035         if (pb->modes & PARPORT_MODE_PCECR)
2036                 ECR_WRITE (pb, oecr);
2037         parport_pc_write_control(pb, 0xc);
2038
2039         if (pb->irq <= 0)
2040                 pb->irq = PARPORT_IRQ_NONE;
2041
2042         return pb->irq;
2043 #endif /* Advanced detection */
2044 }
2045
2046 static int __devinit irq_probe_SPP(struct parport *pb)
2047 {
2048         /* Don't even try to do this. */
2049         return PARPORT_IRQ_NONE;
2050 }
2051
2052 /* We will attempt to share interrupt requests since other devices
2053  * such as sound cards and network cards seem to like using the
2054  * printer IRQs.
2055  *
2056  * When ECP is available we can autoprobe for IRQs.
2057  * NOTE: If we can autoprobe it, we can register the IRQ.
2058  */
2059 static int __devinit parport_irq_probe(struct parport *pb)
2060 {
2061         struct parport_pc_private *priv = pb->private_data;
2062
2063         if (priv->ecr) {
2064                 pb->irq = programmable_irq_support(pb);
2065
2066                 if (pb->irq == PARPORT_IRQ_NONE)
2067                         pb->irq = irq_probe_ECP(pb);
2068         }
2069
2070         if ((pb->irq == PARPORT_IRQ_NONE) && priv->ecr &&
2071             (pb->modes & PARPORT_MODE_EPP))
2072                 pb->irq = irq_probe_EPP(pb);
2073
2074         clear_epp_timeout(pb);
2075
2076         if (pb->irq == PARPORT_IRQ_NONE && (pb->modes & PARPORT_MODE_EPP))
2077                 pb->irq = irq_probe_EPP(pb);
2078
2079         clear_epp_timeout(pb);
2080
2081         if (pb->irq == PARPORT_IRQ_NONE)
2082                 pb->irq = irq_probe_SPP(pb);
2083
2084         if (pb->irq == PARPORT_IRQ_NONE)
2085                 pb->irq = get_superio_irq(pb);
2086
2087         return pb->irq;
2088 }
2089
2090 /* --- DMA detection -------------------------------------- */
2091
2092 /* Only if chipset conforms to ECP ISA Interface Standard */
2093 static int __devinit programmable_dma_support (struct parport *p)
2094 {
2095         unsigned char oecr = inb (ECONTROL (p));
2096         int dma;
2097
2098         frob_set_mode (p, ECR_CNF);
2099         
2100         dma = inb (CONFIGB(p)) & 0x07;
2101         /* 000: Indicates jumpered 8-bit DMA if read-only.
2102            100: Indicates jumpered 16-bit DMA if read-only. */
2103         if ((dma & 0x03) == 0)
2104                 dma = PARPORT_DMA_NONE;
2105
2106         ECR_WRITE (p, oecr);
2107         return dma;
2108 }
2109
2110 static int __devinit parport_dma_probe (struct parport *p)
2111 {
2112         const struct parport_pc_private *priv = p->private_data;
2113         if (priv->ecr)
2114                 p->dma = programmable_dma_support(p); /* ask ECP chipset first */
2115         if (p->dma == PARPORT_DMA_NONE) {
2116                 /* ask known Super-IO chips proper, although these
2117                    claim ECP compatible, some don't report their DMA
2118                    conforming to ECP standards */
2119                 p->dma = get_superio_dma(p);
2120         }
2121
2122         return p->dma;
2123 }
2124
2125 /* --- Initialisation code -------------------------------- */
2126
2127 static LIST_HEAD(ports_list);
2128 static spinlock_t ports_lock = SPIN_LOCK_UNLOCKED;
2129
2130 struct parport *parport_pc_probe_port (unsigned long int base,
2131                                        unsigned long int base_hi,
2132                                        int irq, int dma,
2133                                        struct pci_dev *dev)
2134 {
2135         struct parport_pc_private *priv;
2136         struct parport_operations *ops;
2137         struct parport *p;
2138         int probedirq = PARPORT_IRQ_NONE;
2139         struct resource *base_res;
2140         struct resource *ECR_res = NULL;
2141         struct resource *EPP_res = NULL;
2142
2143         ops = kmalloc(sizeof (struct parport_operations), GFP_KERNEL);
2144         if (!ops)
2145                 goto out1;
2146
2147         priv = kmalloc (sizeof (struct parport_pc_private), GFP_KERNEL);
2148         if (!priv)
2149                 goto out2;
2150
2151         /* a misnomer, actually - it's allocate and reserve parport number */
2152         p = parport_register_port(base, irq, dma, ops);
2153         if (!p)
2154                 goto out3;
2155
2156         base_res = request_region(base, 3, p->name);
2157         if (!base_res)
2158                 goto out4;
2159
2160         memcpy(ops, &parport_pc_ops, sizeof (struct parport_operations));
2161         priv->ctr = 0xc;
2162         priv->ctr_writable = ~0x10;
2163         priv->ecr = 0;
2164         priv->fifo_depth = 0;
2165         priv->dma_buf = NULL;
2166         priv->dma_handle = 0;
2167         priv->dev = dev;
2168         INIT_LIST_HEAD(&priv->list);
2169         priv->port = p;
2170         p->base_hi = base_hi;
2171         p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
2172         p->private_data = priv;
2173
2174         if (base_hi) {
2175                 ECR_res = request_region(base_hi, 3, p->name);
2176                 if (ECR_res)
2177                         parport_ECR_present(p);
2178         }
2179
2180         if (base != 0x3bc) {
2181                 EPP_res = request_region(base+0x3, 5, p->name);
2182                 if (EPP_res)
2183                         if (!parport_EPP_supported(p))
2184                                 parport_ECPEPP_supported(p);
2185         }
2186         if (!parport_SPP_supported (p))
2187                 /* No port. */
2188                 goto out5;
2189         if (priv->ecr)
2190                 parport_ECPPS2_supported(p);
2191         else
2192                 parport_PS2_supported(p);
2193
2194         p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
2195
2196         printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
2197         if (p->base_hi && priv->ecr)
2198                 printk(" (0x%lx)", p->base_hi);
2199         if (p->irq == PARPORT_IRQ_AUTO) {
2200                 p->irq = PARPORT_IRQ_NONE;
2201                 parport_irq_probe(p);
2202         } else if (p->irq == PARPORT_IRQ_PROBEONLY) {
2203                 p->irq = PARPORT_IRQ_NONE;
2204                 parport_irq_probe(p);
2205                 probedirq = p->irq;
2206                 p->irq = PARPORT_IRQ_NONE;
2207         }
2208         if (p->irq != PARPORT_IRQ_NONE) {
2209                 printk(", irq %d", p->irq);
2210                 priv->ctr_writable |= 0x10;
2211
2212                 if (p->dma == PARPORT_DMA_AUTO) {
2213                         p->dma = PARPORT_DMA_NONE;
2214                         parport_dma_probe(p);
2215                 }
2216         }
2217         if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
2218                                            is mandatory (see above) */
2219                 p->dma = PARPORT_DMA_NONE;
2220
2221 #ifdef CONFIG_PARPORT_PC_FIFO
2222         if (parport_ECP_supported(p) &&
2223             p->dma != PARPORT_DMA_NOFIFO &&
2224             priv->fifo_depth > 0 && p->irq != PARPORT_IRQ_NONE) {
2225                 p->modes |= PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
2226                 p->ops->compat_write_data = parport_pc_compat_write_block_pio;
2227 #ifdef CONFIG_PARPORT_1284
2228                 p->ops->ecp_write_data = parport_pc_ecp_write_block_pio;
2229                 /* currently broken, but working on it.. (FB) */
2230                 /* p->ops->ecp_read_data = parport_pc_ecp_read_block_pio; */
2231 #endif /* IEEE 1284 support */
2232                 if (p->dma != PARPORT_DMA_NONE) {
2233                         printk(", dma %d", p->dma);
2234                         p->modes |= PARPORT_MODE_DMA;
2235                 }
2236                 else printk(", using FIFO");
2237         }
2238         else
2239                 /* We can't use the DMA channel after all. */
2240                 p->dma = PARPORT_DMA_NONE;
2241 #endif /* Allowed to use FIFO/DMA */
2242
2243         printk(" [");
2244 #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
2245         {
2246                 int f = 0;
2247                 printmode(PCSPP);
2248                 printmode(TRISTATE);
2249                 printmode(COMPAT)
2250                 printmode(EPP);
2251                 printmode(ECP);
2252                 printmode(DMA);
2253         }
2254 #undef printmode
2255 #ifndef CONFIG_PARPORT_1284
2256         printk ("(,...)");
2257 #endif /* CONFIG_PARPORT_1284 */
2258         printk("]\n");
2259         if (probedirq != PARPORT_IRQ_NONE) 
2260                 printk(KERN_INFO "%s: irq %d detected\n", p->name, probedirq);
2261
2262         /* If No ECP release the ports grabbed above. */
2263         if (ECR_res && (p->modes & PARPORT_MODE_ECP) == 0) {
2264                 release_region(base_hi, 3);
2265                 ECR_res = NULL;
2266         }
2267         /* Likewise for EEP ports */
2268         if (EPP_res && (p->modes & PARPORT_MODE_EPP) == 0) {
2269                 release_region(base+3, 5);
2270                 EPP_res = NULL;
2271         }
2272         if (p->irq != PARPORT_IRQ_NONE) {
2273                 if (request_irq (p->irq, parport_pc_interrupt,
2274                                  0, p->name, p)) {
2275                         printk (KERN_WARNING "%s: irq %d in use, "
2276                                 "resorting to polled operation\n",
2277                                 p->name, p->irq);
2278                         p->irq = PARPORT_IRQ_NONE;
2279                         p->dma = PARPORT_DMA_NONE;
2280                 }
2281
2282 #ifdef CONFIG_PARPORT_PC_FIFO
2283                 if (p->dma != PARPORT_DMA_NONE) {
2284                         if (request_dma (p->dma, p->name)) {
2285                                 printk (KERN_WARNING "%s: dma %d in use, "
2286                                         "resorting to PIO operation\n",
2287                                         p->name, p->dma);
2288                                 p->dma = PARPORT_DMA_NONE;
2289                         } else {
2290                                 priv->dma_buf =
2291                                   pci_alloc_consistent(priv->dev,
2292                                                        PAGE_SIZE,
2293                                                        &priv->dma_handle);
2294                                 if (! priv->dma_buf) {
2295                                         printk (KERN_WARNING "%s: "
2296                                                 "cannot get buffer for DMA, "
2297                                                 "resorting to PIO operation\n",
2298                                                 p->name);
2299                                         free_dma(p->dma);
2300                                         p->dma = PARPORT_DMA_NONE;
2301                                 }
2302                         }
2303                 }
2304 #endif /* CONFIG_PARPORT_PC_FIFO */
2305         }
2306
2307         /* Done probing.  Now put the port into a sensible start-up state. */
2308         if (priv->ecr)
2309                 /*
2310                  * Put the ECP detected port in PS2 mode.
2311                  * Do this also for ports that have ECR but don't do ECP.
2312                  */
2313                 ECR_WRITE (p, 0x34);
2314
2315         parport_pc_write_data(p, 0);
2316         parport_pc_data_forward (p);
2317
2318         /* Now that we've told the sharing engine about the port, and
2319            found out its characteristics, let the high-level drivers
2320            know about it. */
2321         spin_lock(&ports_lock);
2322         list_add(&priv->list, &ports_list);
2323         spin_unlock(&ports_lock);
2324         parport_announce_port (p);
2325
2326         return p;
2327
2328 out5:
2329         if (ECR_res)
2330                 release_region(base_hi, 3);
2331         if (EPP_res)
2332                 release_region(base+0x3, 5);
2333         release_region(base, 3);
2334 out4:
2335         parport_put_port(p);
2336 out3:
2337         kfree (priv);
2338 out2:
2339         kfree (ops);
2340 out1:
2341         return NULL;
2342 }
2343
2344 EXPORT_SYMBOL (parport_pc_probe_port);
2345
2346 void parport_pc_unregister_port (struct parport *p)
2347 {
2348         struct parport_pc_private *priv = p->private_data;
2349         struct parport_operations *ops = p->ops;
2350
2351         parport_remove_port(p);
2352         spin_lock(&ports_lock);
2353         list_del_init(&priv->list);
2354         spin_unlock(&ports_lock);
2355         if (p->dma != PARPORT_DMA_NONE)
2356                 free_dma(p->dma);
2357         if (p->irq != PARPORT_IRQ_NONE)
2358                 free_irq(p->irq, p);
2359         release_region(p->base, 3);
2360         if (p->size > 3)
2361                 release_region(p->base + 3, p->size - 3);
2362         if (p->modes & PARPORT_MODE_ECP)
2363                 release_region(p->base_hi, 3);
2364 #ifdef CONFIG_PARPORT_PC_FIFO
2365         if (priv->dma_buf)
2366                 pci_free_consistent(priv->dev, PAGE_SIZE,
2367                                     priv->dma_buf,
2368                                     priv->dma_handle);
2369 #endif /* CONFIG_PARPORT_PC_FIFO */
2370         kfree (p->private_data);
2371         parport_put_port(p);
2372         kfree (ops); /* hope no-one cached it */
2373 }
2374
2375 EXPORT_SYMBOL (parport_pc_unregister_port);
2376
2377 #ifdef CONFIG_PCI
2378
2379 /* ITE support maintained by Rich Liu <richliu@poorman.org> */
2380 static int __devinit sio_ite_8872_probe (struct pci_dev *pdev, int autoirq,
2381                                          int autodma)
2382 {
2383         short inta_addr[6] = { 0x2A0, 0x2C0, 0x220, 0x240, 0x1E0 };
2384         struct resource *base_res;
2385         u32 ite8872set;
2386         u32 ite8872_lpt, ite8872_lpthi;
2387         u8 ite8872_irq, type;
2388         char *fake_name = "parport probe";
2389         int irq;
2390         int i;
2391
2392         DPRINTK (KERN_DEBUG "sio_ite_8872_probe()\n");
2393         
2394         // make sure which one chip
2395         for(i = 0; i < 5; i++) {
2396                 base_res = request_region(inta_addr[i], 0x8, fake_name);
2397                 if (base_res) {
2398                         int test;
2399                         pci_write_config_dword (pdev, 0x60,
2400                                                 0xe7000000 | inta_addr[i]);
2401                         pci_write_config_dword (pdev, 0x78,
2402                                                 0x00000000 | inta_addr[i]);
2403                         test = inb (inta_addr[i]);
2404                         if (test != 0xff) break;
2405                         release_region(inta_addr[i], 0x8);
2406                 }
2407         }
2408         if(i >= 5) {
2409                 printk (KERN_INFO "parport_pc: cannot find ITE8872 INTA\n");
2410                 return 0;
2411         }
2412
2413         type = inb (inta_addr[i] + 0x18);
2414         type &= 0x0f;
2415
2416         switch (type) {
2417         case 0x2:
2418                 printk (KERN_INFO "parport_pc: ITE8871 found (1P)\n");
2419                 ite8872set = 0x64200000;
2420                 break;
2421         case 0xa:
2422                 printk (KERN_INFO "parport_pc: ITE8875 found (1P)\n");
2423                 ite8872set = 0x64200000;
2424                 break;
2425         case 0xe:
2426                 printk (KERN_INFO "parport_pc: ITE8872 found (2S1P)\n");
2427                 ite8872set = 0x64e00000;
2428                 break;
2429         case 0x6:
2430                 printk (KERN_INFO "parport_pc: ITE8873 found (1S)\n");
2431                 return 0;
2432         case 0x8:
2433                 DPRINTK (KERN_DEBUG "parport_pc: ITE8874 found (2S)\n");
2434                 return 0;
2435         default:
2436                 printk (KERN_INFO "parport_pc: unknown ITE887x\n");
2437                 printk (KERN_INFO "parport_pc: please mail 'lspci -nvv' "
2438                         "output to Rich.Liu@ite.com.tw\n");
2439                 return 0;
2440         }
2441
2442         pci_read_config_byte (pdev, 0x3c, &ite8872_irq);
2443         pci_read_config_dword (pdev, 0x1c, &ite8872_lpt);
2444         ite8872_lpt &= 0x0000ff00;
2445         pci_read_config_dword (pdev, 0x20, &ite8872_lpthi);
2446         ite8872_lpthi &= 0x0000ff00;
2447         pci_write_config_dword (pdev, 0x6c, 0xe3000000 | ite8872_lpt);
2448         pci_write_config_dword (pdev, 0x70, 0xe3000000 | ite8872_lpthi);
2449         pci_write_config_dword (pdev, 0x80, (ite8872_lpthi<<16) | ite8872_lpt);
2450         // SET SPP&EPP , Parallel Port NO DMA , Enable All Function
2451         // SET Parallel IRQ
2452         pci_write_config_dword (pdev, 0x9c,
2453                                 ite8872set | (ite8872_irq * 0x11111));
2454
2455         DPRINTK (KERN_DEBUG "ITE887x: The IRQ is %d.\n", ite8872_irq);
2456         DPRINTK (KERN_DEBUG "ITE887x: The PARALLEL I/O port is 0x%x.\n",
2457                  ite8872_lpt);
2458         DPRINTK (KERN_DEBUG "ITE887x: The PARALLEL I/O porthi is 0x%x.\n",
2459                  ite8872_lpthi);
2460
2461         /* Let the user (or defaults) steer us away from interrupts */
2462         irq = ite8872_irq;
2463         if (autoirq != PARPORT_IRQ_AUTO)
2464                 irq = PARPORT_IRQ_NONE;
2465
2466         /*
2467          * Release the resource so that parport_pc_probe_port can get it.
2468          */
2469         release_resource(base_res);
2470         if (parport_pc_probe_port (ite8872_lpt, ite8872_lpthi,
2471                                    irq, PARPORT_DMA_NONE, NULL)) {
2472                 printk (KERN_INFO
2473                         "parport_pc: ITE 8872 parallel port: io=0x%X",
2474                         ite8872_lpt);
2475                 if (irq != PARPORT_IRQ_NONE)
2476                         printk (", irq=%d", irq);
2477                 printk ("\n");
2478                 return 1;
2479         }
2480
2481         return 0;
2482 }
2483
2484 /* Via support maintained by Jeff Garzik <jgarzik@pobox.com> */
2485 static int __devinit sio_via_686a_probe (struct pci_dev *pdev, int autoirq,
2486                                          int autodma)
2487 {
2488         u8 tmp;
2489         int dma, irq;
2490         unsigned port1, port2, have_eppecp;
2491
2492         /*
2493          * unlock super i/o configuration, set 0x85_1
2494          */
2495         pci_read_config_byte (pdev, 0x85, &tmp);
2496         tmp |= (1 << 1);
2497         pci_write_config_byte (pdev, 0x85, tmp);
2498         
2499         /* 
2500          * Super I/O configuration, index port == 3f0h, data port == 3f1h
2501          */
2502         
2503         /* 0xE2_1-0: Parallel Port Mode / Enable */
2504         outb (0xE2, 0x3F0);
2505         tmp = inb (0x3F1);
2506         
2507         if ((tmp & 0x03) == 0x03) {
2508                 printk (KERN_INFO "parport_pc: Via 686A parallel port disabled in BIOS\n");
2509                 return 0;
2510         }
2511         
2512         /* 0xE6: Parallel Port I/O Base Address, bits 9-2 */
2513         outb (0xE6, 0x3F0);
2514         port1 = inb (0x3F1) << 2;
2515         
2516         switch (port1) {
2517         case 0x3bc: port2 = 0x7bc; break;
2518         case 0x378: port2 = 0x778; break;
2519         case 0x278: port2 = 0x678; break;
2520         default:
2521                 printk (KERN_INFO "parport_pc: Weird Via 686A parport base 0x%X, ignoring\n",
2522                         port1);
2523                 return 0;
2524         }
2525
2526         /* 0xF0_5: EPP+ECP enable */
2527         outb (0xF0, 0x3F0);
2528         have_eppecp = (inb (0x3F1) & (1 << 5));
2529         
2530         /*
2531          * lock super i/o configuration, clear 0x85_1
2532          */
2533         pci_read_config_byte (pdev, 0x85, &tmp);
2534         tmp &= ~(1 << 1);
2535         pci_write_config_byte (pdev, 0x85, tmp);
2536
2537         /*
2538          * Get DMA and IRQ from PCI->ISA bridge PCI config registers
2539          */
2540
2541         /* 0x50_3-2: PnP Routing for Parallel Port DRQ */
2542         pci_read_config_byte (pdev, 0x50, &tmp);
2543         dma = ((tmp >> 2) & 0x03);
2544         
2545         /* 0x51_7-4: PnP Routing for Parallel Port IRQ */
2546         pci_read_config_byte (pdev, 0x51, &tmp);
2547         irq = ((tmp >> 4) & 0x0F);
2548
2549         /* filter bogus IRQs */
2550         switch (irq) {
2551         case 0:
2552         case 2:
2553         case 8:
2554         case 13:
2555                 irq = PARPORT_IRQ_NONE;
2556                 break;
2557
2558         default: /* do nothing */
2559                 break;
2560         }
2561
2562         /* if ECP not enabled, DMA is not enabled, assumed bogus 'dma' value */
2563         if (!have_eppecp)
2564                 dma = PARPORT_DMA_NONE;
2565
2566         /* Let the user (or defaults) steer us away from interrupts and DMA */
2567         if (autoirq != PARPORT_IRQ_AUTO) {
2568                 irq = PARPORT_IRQ_NONE;
2569                 dma = PARPORT_DMA_NONE;
2570         }
2571         if (autodma != PARPORT_DMA_AUTO)
2572                 dma = PARPORT_DMA_NONE;
2573
2574         /* finally, do the probe with values obtained */
2575         if (parport_pc_probe_port (port1, port2, irq, dma, NULL)) {
2576                 printk (KERN_INFO
2577                         "parport_pc: Via 686A parallel port: io=0x%X", port1);
2578                 if (irq != PARPORT_IRQ_NONE)
2579                         printk (", irq=%d", irq);
2580                 if (dma != PARPORT_DMA_NONE)
2581                         printk (", dma=%d", dma);
2582                 printk ("\n");
2583                 return 1;
2584         }
2585         
2586         printk (KERN_WARNING "parport_pc: Strange, can't probe Via 686A parallel port: io=0x%X, irq=%d, dma=%d\n",
2587                 port1, irq, dma);
2588         return 0;
2589 }
2590
2591
2592 enum parport_pc_sio_types {
2593         sio_via_686a = 0,       /* Via VT82C686A motherboard Super I/O */
2594         sio_ite_8872,
2595         last_sio
2596 };
2597
2598 /* each element directly indexed from enum list, above */
2599 static struct parport_pc_superio {
2600         int (*probe) (struct pci_dev *pdev, int autoirq, int autodma);
2601 } parport_pc_superio_info[] __devinitdata = {
2602         { sio_via_686a_probe, },
2603         { sio_ite_8872_probe, },
2604 };
2605
2606
2607 enum parport_pc_pci_cards {
2608         siig_1p_10x = last_sio,
2609         siig_2p_10x,
2610         siig_1p_20x,
2611         siig_2p_20x,
2612         lava_parallel,
2613         lava_parallel_dual_a,
2614         lava_parallel_dual_b,
2615         boca_ioppar,
2616         plx_9050,
2617         timedia_4078a,
2618         timedia_4079h,
2619         timedia_4085h,
2620         timedia_4088a,
2621         timedia_4089a,
2622         timedia_4095a,
2623         timedia_4096a,
2624         timedia_4078u,
2625         timedia_4079a,
2626         timedia_4085u,
2627         timedia_4079r,
2628         timedia_4079s,
2629         timedia_4079d,
2630         timedia_4079e,
2631         timedia_4079f,
2632         timedia_9079a,
2633         timedia_9079b,
2634         timedia_9079c,
2635         timedia_4006a,
2636         timedia_4014,
2637         timedia_4008a,
2638         timedia_4018,
2639         timedia_9018a,
2640         syba_2p_epp,
2641         syba_1p_ecp,
2642         titan_010l,
2643         titan_1284p2,
2644         avlab_1p,
2645         avlab_2p,
2646         oxsemi_954,
2647         oxsemi_840,
2648         aks_0100,
2649         mobility_pp,
2650         netmos_9705,
2651         netmos_9805,
2652         netmos_9815,
2653         netmos_9855,
2654         netmos_9735,
2655         netmos_9835,
2656         netmos_9755,
2657         netmos_9715
2658 };
2659
2660
2661 /* each element directly indexed from enum list, above 
2662  * (but offset by last_sio) */
2663 static struct parport_pc_pci {
2664         int numports;
2665         struct { /* BAR (base address registers) numbers in the config
2666                     space header */
2667                 int lo;
2668                 int hi; /* -1 if not there, >6 for offset-method (max
2669                            BAR is 6) */
2670         } addr[4];
2671
2672         /* If set, this is called immediately after pci_enable_device.
2673          * If it returns non-zero, no probing will take place and the
2674          * ports will not be used. */
2675         int (*preinit_hook) (struct pci_dev *pdev, int autoirq, int autodma);
2676
2677         /* If set, this is called after probing for ports.  If 'failed'
2678          * is non-zero we couldn't use any of the ports. */
2679         void (*postinit_hook) (struct pci_dev *pdev, int failed);
2680 } cards[] __devinitdata = {
2681         /* siig_1p_10x */               { 1, { { 2, 3 }, } },
2682         /* siig_2p_10x */               { 2, { { 2, 3 }, { 4, 5 }, } },
2683         /* siig_1p_20x */               { 1, { { 0, 1 }, } },
2684         /* siig_2p_20x */               { 2, { { 0, 1 }, { 2, 3 }, } },
2685         /* lava_parallel */             { 1, { { 0, -1 }, } },
2686         /* lava_parallel_dual_a */      { 1, { { 0, -1 }, } },
2687         /* lava_parallel_dual_b */      { 1, { { 0, -1 }, } },
2688         /* boca_ioppar */               { 1, { { 0, -1 }, } },
2689         /* plx_9050 */                  { 2, { { 4, -1 }, { 5, -1 }, } },
2690         /* timedia_4078a */             { 1, { { 2, -1 }, } },
2691         /* timedia_4079h */             { 1, { { 2, 3 }, } },
2692         /* timedia_4085h */             { 2, { { 2, -1 }, { 4, -1 }, } },
2693         /* timedia_4088a */             { 2, { { 2, 3 }, { 4, 5 }, } },
2694         /* timedia_4089a */             { 2, { { 2, 3 }, { 4, 5 }, } },
2695         /* timedia_4095a */             { 2, { { 2, 3 }, { 4, 5 }, } },
2696         /* timedia_4096a */             { 2, { { 2, 3 }, { 4, 5 }, } },
2697         /* timedia_4078u */             { 1, { { 2, -1 }, } },
2698         /* timedia_4079a */             { 1, { { 2, 3 }, } },
2699         /* timedia_4085u */             { 2, { { 2, -1 }, { 4, -1 }, } },
2700         /* timedia_4079r */             { 1, { { 2, 3 }, } },
2701         /* timedia_4079s */             { 1, { { 2, 3 }, } },
2702         /* timedia_4079d */             { 1, { { 2, 3 }, } },
2703         /* timedia_4079e */             { 1, { { 2, 3 }, } },
2704         /* timedia_4079f */             { 1, { { 2, 3 }, } },
2705         /* timedia_9079a */             { 1, { { 2, 3 }, } },
2706         /* timedia_9079b */             { 1, { { 2, 3 }, } },
2707         /* timedia_9079c */             { 1, { { 2, 3 }, } },
2708         /* timedia_4006a */             { 1, { { 0, -1 }, } },
2709         /* timedia_4014  */             { 2, { { 0, -1 }, { 2, -1 }, } },
2710         /* timedia_4008a */             { 1, { { 0, 1 }, } },
2711         /* timedia_4018  */             { 2, { { 0, 1 }, { 2, 3 }, } },
2712         /* timedia_9018a */             { 2, { { 0, 1 }, { 2, 3 }, } },
2713                                         /* SYBA uses fixed offsets in
2714                                            a 1K io window */
2715         /* syba_2p_epp AP138B */        { 2, { { 0, 0x078 }, { 0, 0x178 }, } },
2716         /* syba_1p_ecp W83787 */        { 1, { { 0, 0x078 }, } },
2717         /* titan_010l */                { 1, { { 3, -1 }, } },
2718         /* titan_1284p2 */              { 2, { { 0, 1 }, { 2, 3 }, } },
2719         /* avlab_1p             */      { 1, { { 0, 1}, } },
2720         /* avlab_2p             */      { 2, { { 0, 1}, { 2, 3 },} },
2721         /* The Oxford Semi cards are unusual: 954 doesn't support ECP,
2722          * and 840 locks up if you write 1 to bit 2! */
2723         /* oxsemi_954 */                { 1, { { 0, -1 }, } },
2724         /* oxsemi_840 */                { 1, { { 0, -1 }, } },
2725         /* aks_0100 */                  { 1, { { 0, -1 }, } },
2726         /* mobility_pp */               { 1, { { 0, 1 }, } },
2727         /* netmos_9705 */               { 1, { { 0, -1 }, } }, /* untested */
2728         /* netmos_9805 */               { 1, { { 0, -1 }, } }, /* untested */
2729         /* netmos_9815 */               { 2, { { 0, -1 }, { 2, -1 }, } }, /* untested */
2730         /* netmos_9855 */               { 2, { { 0, -1 }, { 2, -1 }, } }, /* untested */
2731         /* netmos_9735 */               { 1, { { 2, 3 }, } },  /* untested */
2732         /* netmos_9835 */               { 1, { { 2, 3 }, } },  /* untested */
2733         /* netmos_9755 */               { 2, { { 0, 1 }, { 2, 3 },} }, /* untested */
2734         /* netmos_9715 */               { 2, { { 0, 1 }, { 2, 3 },} }, /* untested */
2735 };
2736
2737 static struct pci_device_id parport_pc_pci_tbl[] = {
2738         /* Super-IO onboard chips */
2739         { 0x1106, 0x0686, PCI_ANY_ID, PCI_ANY_ID, 0, 0, sio_via_686a },
2740         { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_8872,
2741           PCI_ANY_ID, PCI_ANY_ID, 0, 0, sio_ite_8872 },
2742
2743         /* PCI cards */
2744         { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1P_10x,
2745           PCI_ANY_ID, PCI_ANY_ID, 0, 0, siig_1p_10x },
2746         { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P_10x,
2747           PCI_ANY_ID, PCI_ANY_ID, 0, 0, siig_2p_10x },
2748         { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1P_20x,
2749           PCI_ANY_ID, PCI_ANY_ID, 0, 0, siig_1p_20x },
2750         { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P_20x,
2751           PCI_ANY_ID, PCI_ANY_ID, 0, 0, siig_2p_20x },
2752         { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PARALLEL,
2753           PCI_ANY_ID, PCI_ANY_ID, 0, 0, lava_parallel },
2754         { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DUAL_PAR_A,
2755           PCI_ANY_ID, PCI_ANY_ID, 0, 0, lava_parallel_dual_a },
2756         { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DUAL_PAR_B,
2757           PCI_ANY_ID, PCI_ANY_ID, 0, 0, lava_parallel_dual_b },
2758         { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_BOCA_IOPPAR,
2759           PCI_ANY_ID, PCI_ANY_ID, 0, 0, boca_ioppar },
2760         { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2761           PCI_SUBVENDOR_ID_EXSYS, PCI_SUBDEVICE_ID_EXSYS_4014, 0,0, plx_9050 },
2762         /* PCI_VENDOR_ID_TIMEDIA/SUNIX has many differing cards ...*/
2763         { 0x1409, 0x7168, 0x1409, 0x4078, 0, 0, timedia_4078a },
2764         { 0x1409, 0x7168, 0x1409, 0x4079, 0, 0, timedia_4079h },
2765         { 0x1409, 0x7168, 0x1409, 0x4085, 0, 0, timedia_4085h },
2766         { 0x1409, 0x7168, 0x1409, 0x4088, 0, 0, timedia_4088a },
2767         { 0x1409, 0x7168, 0x1409, 0x4089, 0, 0, timedia_4089a },
2768         { 0x1409, 0x7168, 0x1409, 0x4095, 0, 0, timedia_4095a },
2769         { 0x1409, 0x7168, 0x1409, 0x4096, 0, 0, timedia_4096a },
2770         { 0x1409, 0x7168, 0x1409, 0x5078, 0, 0, timedia_4078u },
2771         { 0x1409, 0x7168, 0x1409, 0x5079, 0, 0, timedia_4079a },
2772         { 0x1409, 0x7168, 0x1409, 0x5085, 0, 0, timedia_4085u },
2773         { 0x1409, 0x7168, 0x1409, 0x6079, 0, 0, timedia_4079r },
2774         { 0x1409, 0x7168, 0x1409, 0x7079, 0, 0, timedia_4079s },
2775         { 0x1409, 0x7168, 0x1409, 0x8079, 0, 0, timedia_4079d },
2776         { 0x1409, 0x7168, 0x1409, 0x9079, 0, 0, timedia_4079e },
2777         { 0x1409, 0x7168, 0x1409, 0xa079, 0, 0, timedia_4079f },
2778         { 0x1409, 0x7168, 0x1409, 0xb079, 0, 0, timedia_9079a },
2779         { 0x1409, 0x7168, 0x1409, 0xc079, 0, 0, timedia_9079b },
2780         { 0x1409, 0x7168, 0x1409, 0xd079, 0, 0, timedia_9079c },
2781         { 0x1409, 0x7268, 0x1409, 0x0101, 0, 0, timedia_4006a },
2782         { 0x1409, 0x7268, 0x1409, 0x0102, 0, 0, timedia_4014 },
2783         { 0x1409, 0x7268, 0x1409, 0x0103, 0, 0, timedia_4008a },
2784         { 0x1409, 0x7268, 0x1409, 0x0104, 0, 0, timedia_4018 },
2785         { 0x1409, 0x7268, 0x1409, 0x9018, 0, 0, timedia_9018a },
2786         { 0x14f2, 0x0121, PCI_ANY_ID, PCI_ANY_ID, 0, 0, mobility_pp },
2787         { PCI_VENDOR_ID_SYBA, PCI_DEVICE_ID_SYBA_2P_EPP,
2788           PCI_ANY_ID, PCI_ANY_ID, 0, 0, syba_2p_epp },
2789         { PCI_VENDOR_ID_SYBA, PCI_DEVICE_ID_SYBA_1P_ECP,
2790           PCI_ANY_ID, PCI_ANY_ID, 0, 0, syba_1p_ecp },
2791         { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_010L,
2792           PCI_ANY_ID, PCI_ANY_ID, 0, 0, titan_010l },
2793         { 0x9710, 0x9815, 0x1000, 0x0020, 0, 0, titan_1284p2 },
2794         /* PCI_VENDOR_ID_AVLAB/Intek21 has another bunch of cards ...*/
2795         { 0x14db, 0x2120, PCI_ANY_ID, PCI_ANY_ID, 0, 0, avlab_1p}, /* AFAVLAB_TK9902 */
2796         { 0x14db, 0x2121, PCI_ANY_ID, PCI_ANY_ID, 0, 0, avlab_2p},
2797         { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954PP,
2798           PCI_ANY_ID, PCI_ANY_ID, 0, 0, oxsemi_954 },
2799         { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_12PCI840,
2800           PCI_ANY_ID, PCI_ANY_ID, 0, 0, oxsemi_840 },
2801         { PCI_VENDOR_ID_AKS, PCI_DEVICE_ID_AKS_ALADDINCARD,
2802           PCI_ANY_ID, PCI_ANY_ID, 0, 0, aks_0100 },
2803         /* NetMos communication controllers */
2804         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9705,
2805           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9705 },
2806         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9805,
2807           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9805 },
2808         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9815,
2809           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9815 },
2810         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9855,
2811           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9855 },
2812         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9735,
2813           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9735 },
2814         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9835,
2815           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9835 },
2816         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9755,
2817           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9755 },
2818         { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9715,
2819           PCI_ANY_ID, PCI_ANY_ID, 0, 0, netmos_9715 },
2820         { 0, } /* terminate list */
2821 };
2822 MODULE_DEVICE_TABLE(pci,parport_pc_pci_tbl);
2823
2824 static int parport_pc_pci_probe (struct pci_dev *dev,
2825                                            const struct pci_device_id *id)
2826 {
2827         int err, count, n, i = id->driver_data;
2828
2829         if (i < last_sio)
2830                 /* This is an onboard Super-IO and has already been probed */
2831                 return 0;
2832
2833         /* This is a PCI card */
2834         i -= last_sio;
2835         count = 0;
2836         if ((err = pci_enable_device (dev)) != 0)
2837                 return err;
2838
2839         if (cards[i].preinit_hook &&
2840             cards[i].preinit_hook (dev, PARPORT_IRQ_NONE, PARPORT_DMA_NONE))
2841                 return -ENODEV;
2842
2843         for (n = 0; n < cards[i].numports; n++) {
2844                 int lo = cards[i].addr[n].lo;
2845                 int hi = cards[i].addr[n].hi;
2846                 unsigned long io_lo, io_hi;
2847                 io_lo = pci_resource_start (dev, lo);
2848                 io_hi = 0;
2849                 if ((hi >= 0) && (hi <= 6))
2850                         io_hi = pci_resource_start (dev, hi);
2851                 else if (hi > 6)
2852                         io_lo += hi; /* Reinterpret the meaning of
2853                                         "hi" as an offset (see SYBA
2854                                         def.) */
2855                 /* TODO: test if sharing interrupts works */
2856                 printk (KERN_DEBUG "PCI parallel port detected: %04x:%04x, "
2857                         "I/O at %#lx(%#lx)\n",
2858                         parport_pc_pci_tbl[i + last_sio].vendor,
2859                         parport_pc_pci_tbl[i + last_sio].device, io_lo, io_hi);
2860                 if (parport_pc_probe_port (io_lo, io_hi, PARPORT_IRQ_NONE,
2861                                            PARPORT_DMA_NONE, dev))
2862                         count++;
2863         }
2864
2865         if (cards[i].postinit_hook)
2866                 cards[i].postinit_hook (dev, count == 0);
2867
2868         return count == 0 ? -ENODEV : 0;
2869 }
2870
2871 static struct pci_driver parport_pc_pci_driver = {
2872         .name           = "parport_pc",
2873         .id_table       = parport_pc_pci_tbl,
2874         .probe          = parport_pc_pci_probe,
2875 };
2876
2877 static int __init parport_pc_init_superio (int autoirq, int autodma)
2878 {
2879         const struct pci_device_id *id;
2880         struct pci_dev *pdev = NULL;
2881         int ret = 0;
2882
2883         while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
2884                 id = pci_match_device (parport_pc_pci_tbl, pdev);
2885                 if (id == NULL || id->driver_data >= last_sio)
2886                         continue;
2887
2888                 if (parport_pc_superio_info[id->driver_data].probe
2889                         (pdev, autoirq, autodma)) {
2890                         ret++;
2891                 }
2892         }
2893
2894         return ret; /* number of devices found */
2895 }
2896 #else
2897 static struct pci_driver parport_pc_pci_driver;
2898 static int __init parport_pc_init_superio(int autoirq, int autodma) {return 0;}
2899 #endif /* CONFIG_PCI */
2900
2901
2902 static const struct pnp_device_id parport_pc_pnp_tbl[] = {
2903         /* Standard LPT Printer Port */
2904         {.id = "PNP0400", .driver_data = 0},
2905         /* ECP Printer Port */
2906         {.id = "PNP0401", .driver_data = 0},
2907         { }
2908 };
2909
2910 MODULE_DEVICE_TABLE(pnp,parport_pc_pnp_tbl);
2911
2912 static int parport_pc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id)
2913 {
2914         struct parport *pdata;
2915         unsigned long io_lo, io_hi;
2916         int dma, irq;
2917
2918         if (pnp_port_valid(dev,0) &&
2919                 !(pnp_port_flags(dev,0) & IORESOURCE_DISABLED)) {
2920                 io_lo = pnp_port_start(dev,0);
2921         } else
2922                 return -EINVAL;
2923
2924         if (pnp_port_valid(dev,1) &&
2925                 !(pnp_port_flags(dev,1) & IORESOURCE_DISABLED)) {
2926                 io_hi = pnp_port_start(dev,1);
2927         } else
2928                 io_hi = 0;
2929
2930         if (pnp_irq_valid(dev,0) &&
2931                 !(pnp_irq_flags(dev,0) & IORESOURCE_DISABLED)) {
2932                 irq = pnp_irq(dev,0);
2933         } else
2934                 irq = PARPORT_IRQ_NONE;
2935
2936         if (pnp_dma_valid(dev,0) &&
2937                 !(pnp_dma_flags(dev,0) & IORESOURCE_DISABLED)) {
2938                 dma = pnp_dma(dev,0);
2939         } else
2940                 dma = PARPORT_DMA_NONE;
2941
2942         printk(KERN_INFO "parport: PnPBIOS parport detected.\n");
2943         if (!(pdata = parport_pc_probe_port (io_lo, io_hi, irq, dma, NULL)))
2944                 return -ENODEV;
2945
2946         pnp_set_drvdata(dev,pdata);
2947         return 0;
2948 }
2949
2950 static void parport_pc_pnp_remove(struct pnp_dev *dev)
2951 {
2952         struct parport *pdata = (struct parport *)pnp_get_drvdata(dev);
2953         if (!pdata)
2954                 return;
2955
2956         parport_pc_unregister_port(pdata);
2957 }
2958
2959 /* we only need the pnp layer to activate the device, at least for now */
2960 static struct pnp_driver parport_pc_pnp_driver = {
2961         .name           = "parport_pc",
2962         .id_table       = parport_pc_pnp_tbl,
2963         .probe          = parport_pc_pnp_probe,
2964         .remove         = parport_pc_pnp_remove,
2965 };
2966
2967
2968 /* This is called by parport_pc_find_nonpci_ports (in asm/parport.h) */
2969 static int __init __attribute__((unused))
2970 parport_pc_find_isa_ports (int autoirq, int autodma)
2971 {
2972         int count = 0;
2973
2974         if (parport_pc_probe_port(0x3bc, 0x7bc, autoirq, autodma, NULL))
2975                 count++;
2976         if (parport_pc_probe_port(0x378, 0x778, autoirq, autodma, NULL))
2977                 count++;
2978         if (parport_pc_probe_port(0x278, 0x678, autoirq, autodma, NULL))
2979                 count++;
2980
2981         return count;
2982 }
2983
2984 /* This function is called by parport_pc_init if the user didn't
2985  * specify any ports to probe.  Its job is to find some ports.  Order
2986  * is important here -- we want ISA ports to be registered first,
2987  * followed by PCI cards (for least surprise), but before that we want
2988  * to do chipset-specific tests for some onboard ports that we know
2989  * about.
2990  *
2991  * autoirq is PARPORT_IRQ_NONE, PARPORT_IRQ_AUTO, or PARPORT_IRQ_PROBEONLY
2992  * autodma is PARPORT_DMA_NONE or PARPORT_DMA_AUTO
2993  */
2994 static int __init parport_pc_find_ports (int autoirq, int autodma)
2995 {
2996         int count = 0, r;
2997
2998 #ifdef CONFIG_PARPORT_PC_SUPERIO
2999         detect_and_report_winbond ();
3000         detect_and_report_smsc ();
3001 #endif
3002
3003         /* Onboard SuperIO chipsets that show themselves on the PCI bus. */
3004         count += parport_pc_init_superio (autoirq, autodma);
3005
3006         /* PnP ports, skip detection if SuperIO already found them */
3007         if (!count) {
3008                 r = pnp_register_driver (&parport_pc_pnp_driver);
3009                 if (r >= 0) {
3010                         pnp_registered_parport = 1;
3011                         count += r;
3012                 }
3013         }
3014
3015         /* ISA ports and whatever (see asm/parport.h). */
3016         count += parport_pc_find_nonpci_ports (autoirq, autodma);
3017
3018         r = pci_register_driver (&parport_pc_pci_driver);
3019         if (r >= 0) {
3020                 pci_registered_parport = 1;
3021                 count += r;
3022         }
3023
3024         return count;
3025 }
3026
3027 /*
3028  *      Piles of crap below pretend to be a parser for module and kernel
3029  *      parameters.  Say "thank you" to whoever had come up with that
3030  *      syntax and keep in mind that code below is a cleaned up version.
3031  */
3032
3033 static int __initdata io[PARPORT_PC_MAX_PORTS+1] = { [0 ... PARPORT_PC_MAX_PORTS] = 0 };
3034 static int __initdata io_hi[PARPORT_PC_MAX_PORTS+1] =
3035         { [0 ... PARPORT_PC_MAX_PORTS] = PARPORT_IOHI_AUTO };
3036 static int __initdata dmaval[PARPORT_PC_MAX_PORTS] = { [0 ... PARPORT_PC_MAX_PORTS-1] = PARPORT_DMA_NONE };
3037 static int __initdata irqval[PARPORT_PC_MAX_PORTS] = { [0 ... PARPORT_PC_MAX_PORTS-1] = PARPORT_IRQ_PROBEONLY };
3038
3039 static int __init parport_parse_param(const char *s, int *val,
3040                                 int automatic, int none, int nofifo)
3041 {
3042         if (!s)
3043                 return 0;
3044         if (!strncmp(s, "auto", 4))
3045                 *val = automatic;
3046         else if (!strncmp(s, "none", 4))
3047                 *val = none;
3048         else if (nofifo && !strncmp(s, "nofifo", 4))
3049                 *val = nofifo;
3050         else {
3051                 char *ep;
3052                 unsigned long r = simple_strtoul(s, &ep, 0);
3053                 if (ep != s)
3054                         *val = r;
3055                 else {
3056                         printk(KERN_ERR "parport: bad specifier `%s'\n", s);
3057                         return -1;
3058                 }
3059         }
3060         return 0;
3061 }
3062
3063 static int __init parport_parse_irq(const char *irqstr, int *val)
3064 {
3065         return parport_parse_param(irqstr, val, PARPORT_IRQ_AUTO,
3066                                      PARPORT_IRQ_NONE, 0);
3067 }
3068
3069 static int __init parport_parse_dma(const char *dmastr, int *val)
3070 {
3071         return parport_parse_param(dmastr, val, PARPORT_DMA_AUTO,
3072                                      PARPORT_DMA_NONE, PARPORT_DMA_NOFIFO);
3073 }
3074
3075 #ifdef MODULE
3076 static const char *irq[PARPORT_PC_MAX_PORTS];
3077 static const char *dma[PARPORT_PC_MAX_PORTS];
3078
3079 MODULE_PARM_DESC(io, "Base I/O address (SPP regs)");
3080 MODULE_PARM(io, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "i");
3081 MODULE_PARM_DESC(io_hi, "Base I/O address (ECR)");
3082 MODULE_PARM(io_hi, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "i");
3083 MODULE_PARM_DESC(irq, "IRQ line");
3084 MODULE_PARM(irq, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "s");
3085 MODULE_PARM_DESC(dma, "DMA channel");
3086 MODULE_PARM(dma, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "s");
3087 #if defined(CONFIG_PARPORT_PC_SUPERIO) || \
3088        (defined(CONFIG_PARPORT_1284) && defined(CONFIG_PARPORT_PC_FIFO))
3089 MODULE_PARM_DESC(verbose_probing, "Log chit-chat during initialisation");
3090 MODULE_PARM(verbose_probing, "i");
3091 #endif
3092
3093 static int __init parse_parport_params(void)
3094 {
3095         unsigned int i;
3096         int val;
3097
3098         for (i = 0; i < PARPORT_PC_MAX_PORTS && io[i]; i++) {
3099                 if (parport_parse_irq(irq[i], &val))
3100                         return 1;
3101                 irqval[i] = val;
3102                 if (parport_parse_dma(dma[i], &val))
3103                         return 1;
3104                 dmaval[i] = val;
3105         }
3106         if (!io[0]) {
3107                 /* The user can make us use any IRQs or DMAs we find. */
3108                 if (irq[0] && !parport_parse_irq(irq[0], &val))
3109                         switch (val) {
3110                         case PARPORT_IRQ_NONE:
3111                         case PARPORT_IRQ_AUTO:
3112                                 irqval[0] = val;
3113                                 break;
3114                         default:
3115                                 printk (KERN_WARNING
3116                                         "parport_pc: irq specified "
3117                                         "without base address.  Use 'io=' "
3118                                         "to specify one\n");
3119                         }
3120
3121                 if (dma[0] && !parport_parse_dma(dma[0], &val))
3122                         switch (val) {
3123                         case PARPORT_DMA_NONE:
3124                         case PARPORT_DMA_AUTO:
3125                                 dmaval[0] = val;
3126                                 break;
3127                         default:
3128                                 printk (KERN_WARNING
3129                                         "parport_pc: dma specified "
3130                                         "without base address.  Use 'io=' "
3131                                         "to specify one\n");
3132                         }
3133         }
3134         return 0;
3135 }
3136
3137 #else
3138
3139 static int parport_setup_ptr __initdata = 0;
3140
3141 /*
3142  * Acceptable parameters:
3143  *
3144  * parport=0
3145  * parport=auto
3146  * parport=0xBASE[,IRQ[,DMA]]
3147  *
3148  * IRQ/DMA may be numeric or 'auto' or 'none'
3149  */
3150 static int __init parport_setup (char *str)
3151 {
3152         char *endptr;
3153         char *sep;
3154         int val;
3155
3156         if (!str || !*str || (*str == '0' && !*(str+1))) {
3157                 /* Disable parport if "parport=0" in cmdline */
3158                 io[0] = PARPORT_DISABLE;
3159                 return 1;
3160         }
3161
3162         if (!strncmp (str, "auto", 4)) {
3163                 irqval[0] = PARPORT_IRQ_AUTO;
3164                 dmaval[0] = PARPORT_DMA_AUTO;
3165                 return 1;
3166         }
3167
3168         val = simple_strtoul (str, &endptr, 0);
3169         if (endptr == str) {
3170                 printk (KERN_WARNING "parport=%s not understood\n", str);
3171                 return 1;
3172         }
3173
3174         if (parport_setup_ptr == PARPORT_PC_MAX_PORTS) {
3175                 printk(KERN_ERR "parport=%s ignored, too many ports\n", str);
3176                 return 1;
3177         }
3178
3179         io[parport_setup_ptr] = val;
3180         irqval[parport_setup_ptr] = PARPORT_IRQ_NONE;
3181         dmaval[parport_setup_ptr] = PARPORT_DMA_NONE;
3182
3183         sep = strchr(str, ',');
3184         if (sep++) {
3185                 if (parport_parse_irq(sep, &val))
3186                         return 1;
3187                 irqval[parport_setup_ptr] = val;
3188                 sep = strchr(sep, ',');
3189                 if (sep++) {
3190                         if (parport_parse_dma(sep, &val))
3191                                 return 1;
3192                         dmaval[parport_setup_ptr] = val;
3193                 }
3194         }
3195         parport_setup_ptr++;
3196         return 1;
3197 }
3198
3199 static int __init parse_parport_params(void)
3200 {
3201         return io[0] == PARPORT_DISABLE;
3202 }
3203
3204 __setup ("parport=", parport_setup);
3205 #endif
3206
3207 /* "Parser" ends here */
3208
3209 static int __init parport_pc_init(void)
3210 {
3211         int count = 0;
3212
3213         if (parse_parport_params())
3214                 return -EINVAL;
3215
3216         if (io[0]) {
3217                 int i;
3218                 /* Only probe the ports we were given. */
3219                 user_specified = 1;
3220                 for (i = 0; i < PARPORT_PC_MAX_PORTS; i++) {
3221                         if (!io[i])
3222                                 break;
3223                         if ((io_hi[i]) == PARPORT_IOHI_AUTO)
3224                                io_hi[i] = 0x400 + io[i];
3225                         if (parport_pc_probe_port(io[i], io_hi[i],
3226                                                   irqval[i], dmaval[i], NULL))
3227                                 count++;
3228                 }
3229         } else
3230                 count += parport_pc_find_ports (irqval[0], dmaval[0]);
3231
3232         return 0;
3233 }
3234
3235 static void __exit parport_pc_exit(void)
3236 {
3237         if (pci_registered_parport)
3238                 pci_unregister_driver (&parport_pc_pci_driver);
3239         if (pnp_registered_parport)
3240                 pnp_unregister_driver (&parport_pc_pnp_driver);
3241
3242         spin_lock(&ports_lock);
3243         while (!list_empty(&ports_list)) {
3244                 struct parport_pc_private *priv;
3245                 struct parport *port;
3246                 priv = list_entry(ports_list.next,
3247                                   struct parport_pc_private, list);
3248                 port = priv->port;
3249                 spin_unlock(&ports_lock);
3250                 parport_pc_unregister_port(port);
3251                 spin_lock(&ports_lock);
3252         }
3253         spin_unlock(&ports_lock);
3254 }
3255
3256 MODULE_AUTHOR("Phil Blundell, Tim Waugh, others");
3257 MODULE_DESCRIPTION("PC-style parallel port driver");
3258 MODULE_LICENSE("GPL");
3259 module_init(parport_pc_init)
3260 module_exit(parport_pc_exit)