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