Import 2.3.10pre1
[davej-history.git] / drivers / misc / parport_pc.c
blobbfba506ea3c3b03ac64b2c2ab8045c0401c91a2b
1 /* Low-level parallel-port routines for 8255-based PC-style hardware.
2 *
3 * Authors: Phil Blundell <Philip.Blundell@pobox.com>
4 * Tim Waugh <tim@cyberelk.demon.co.uk>
5 * Jose Renau <renau@acm.org>
6 * David Campbell <campbell@torque.net>
7 * Andrea Arcangeli
9 * based on work by Grant Guenther <grant@torque.net> and Phil Blundell.
11 * Cleaned up include files - Russell King <linux@arm.uk.linux.org>
12 * DMA support - Bert De Jonghe <bert@sophis.be>
13 * Better EPP probing - Carlos Henrique Bauer <chbauer@acm.org>
16 /* This driver should work with any hardware that is broadly compatible
17 * with that in the IBM PC. This applies to the majority of integrated
18 * I/O chipsets that are commonly available. The expected register
19 * layout is:
21 * base+0 data
22 * base+1 status
23 * base+2 control
25 * In addition, there are some optional registers:
27 * base+3 EPP address
28 * base+4 EPP data
29 * base+0x400 ECP config A
30 * base+0x401 ECP config B
31 * base+0x402 ECP control
33 * All registers are 8 bits wide and read/write. If your hardware differs
34 * only in register addresses (eg because your registers are on 32-bit
35 * word boundaries) then you can alter the constants in parport_pc.h to
36 * accomodate this.
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/sched.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/ioport.h>
47 #include <linux/kernel.h>
48 #include <linux/malloc.h>
49 #include <linux/pci.h>
50 #include <linux/sysctl.h>
52 #include <asm/io.h>
53 #include <asm/dma.h>
54 #include <asm/uaccess.h>
56 #include <linux/parport.h>
57 #include <linux/parport_pc.h>
59 /* Maximum number of ports to support. It is useless to set this greater
60 than PARPORT_MAX (in <linux/parport.h>). */
61 #define PARPORT_PC_MAX_PORTS 8
63 /* ECR modes */
64 #define ECR_SPP 00
65 #define ECR_PS2 01
66 #define ECR_PPF 02
67 #define ECR_ECP 03
68 #define ECR_EPP 04
69 #define ECR_VND 05
70 #define ECR_TST 06
71 #define ECR_CNF 07
73 static int user_specified __initdata = 0;
75 /* frob_control, but for ECR */
76 static void frob_econtrol (struct parport *pb, unsigned char m,
77 unsigned char v)
79 outb ((inb (ECONTROL (pb)) & ~m) ^ v, ECONTROL (pb));
82 #ifdef CONFIG_PARPORT_1284
83 /* Safely change the mode bits in the ECR */
84 static int change_mode(struct parport *p, int m)
86 const struct parport_pc_private *priv = p->physport->private_data;
87 int ecr = ECONTROL(p);
88 unsigned char oecr;
89 int mode;
91 if (!priv->ecr) {
92 printk (KERN_DEBUG "change_mode: but there's no ECR!\n");
93 return 0;
96 /* Bits <7:5> contain the mode. */
97 oecr = inb (ecr);
98 mode = (oecr >> 5) & 0x7;
99 if (mode == m) return 0;
100 if (mode && m)
101 /* We have to go through mode 000 */
102 change_mode (p, ECR_SPP);
104 if (m < 2 && !(parport_read_control (p) & 0x20)) {
105 /* This mode resets the FIFO, so we may
106 * have to wait for it to drain first. */
107 long expire = jiffies + p->physport->cad->timeout;
108 int counter;
109 switch (mode) {
110 case ECR_PPF: /* Parallel Port FIFO mode */
111 case ECR_ECP: /* ECP Parallel Port mode */
112 /* Busy wait for 200us */
113 for (counter = 0; counter < 40; counter++) {
114 if (inb (ECONTROL (p)) & 0x01)
115 break;
116 if (signal_pending (current)) break;
117 udelay (5);
120 /* Poll slowly. */
121 while (!(inb (ECONTROL (p)) & 0x01)) {
122 if (time_after_eq (jiffies, expire))
123 /* The FIFO is stuck. */
124 return -EBUSY;
125 current->state = TASK_INTERRUPTIBLE;
126 schedule_timeout ((HZ + 99) / 100);
127 if (signal_pending (current))
128 break;
133 /* Set the mode. */
134 oecr &= ~(7 << 5);
135 oecr |= m << 5;
136 outb (oecr, ecr);
137 return 0;
140 /* Find FIFO lossage; FIFO is reset */
141 static int get_fifo_residue (struct parport *p)
143 int residue;
144 int cnfga;
145 const struct parport_pc_private *priv = p->physport->private_data;
147 /* Prevent further data transfer. */
148 parport_frob_control (p,
149 PARPORT_CONTROL_STROBE,
150 PARPORT_CONTROL_STROBE);
152 /* Adjust for the contents of the FIFO. */
153 for (residue = priv->fifo_depth; ; residue--) {
154 if (inb (ECONTROL (p)) & 0x2)
155 /* Full up. */
156 break;
158 outb (0, FIFO (p));
161 printk (KERN_DEBUG "%s: %d PWords were left in FIFO\n", p->name,
162 residue);
164 /* Reset the FIFO. */
165 frob_econtrol (p, 0xe0, 0x20);
166 parport_frob_control (p, PARPORT_CONTROL_STROBE, 0);
168 /* Now change to config mode and clean up. FIXME */
169 frob_econtrol (p, 0xe0, 0xe0);
170 cnfga = inb (CONFIGA (p));
171 printk (KERN_DEBUG "%s: cnfgA contains 0x%02x\n", p->name, cnfga);
173 if (!(cnfga & (1<<2))) {
174 printk (KERN_DEBUG "%s: Accounting for extra byte\n", p->name);
175 residue++;
178 /* Don't care about partial PWords until support is added for
179 * PWord != 1 byte. */
181 /* Back to PS2 mode. */
182 frob_econtrol (p, 0xe0, 0x20);
184 return residue;
187 #endif /* IEEE 1284 support */
190 * Clear TIMEOUT BIT in EPP MODE
192 * This is also used in SPP detection.
194 static int clear_epp_timeout(struct parport *pb)
196 unsigned char r;
198 if (!(parport_pc_read_status(pb) & 0x01))
199 return 1;
201 /* To clear timeout some chips require double read */
202 parport_pc_read_status(pb);
203 r = parport_pc_read_status(pb);
204 outb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
205 outb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
206 r = parport_pc_read_status(pb);
208 return !(r & 0x01);
212 * Access functions.
214 * These aren't static because they may be used by the parport_xxx_yyy
215 * macros. extern __inline__ versions of several of these are in
216 * parport_pc.h.
219 static void parport_pc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
221 parport_generic_irq(irq, (struct parport *) dev_id, regs);
224 void parport_pc_write_data(struct parport *p, unsigned char d)
226 outb (d, DATA (p));
229 unsigned char parport_pc_read_data(struct parport *p)
231 return inb (DATA (p));
234 unsigned char __frob_control (struct parport *p, unsigned char mask,
235 unsigned char val)
237 struct parport_pc_private *priv = p->physport->private_data;
238 unsigned char ctr = priv->ctr;
239 ctr = (ctr & ~mask) ^ val;
240 ctr &= priv->ctr_writable; /* only write writable bits. */
241 outb (ctr, CONTROL (p));
242 return priv->ctr = ctr; /* update soft copy */
245 void parport_pc_write_control(struct parport *p, unsigned char d)
247 const unsigned char wm = (PARPORT_CONTROL_STROBE |
248 PARPORT_CONTROL_AUTOFD |
249 PARPORT_CONTROL_INIT |
250 PARPORT_CONTROL_SELECT);
252 /* Take this out when drivers have adapted to the newer interface. */
253 if (d & 0x20) {
254 printk (KERN_DEBUG "%s (%s): use data_reverse for this!\n",
255 p->name, p->cad->name);
256 parport_pc_data_reverse (p);
259 __frob_control (p, wm, d & wm);
262 unsigned char parport_pc_read_control(struct parport *p)
264 const struct parport_pc_private *priv = p->physport->private_data;
265 return priv->ctr; /* Use soft copy */
268 unsigned char parport_pc_frob_control (struct parport *p, unsigned char mask,
269 unsigned char val)
271 const unsigned char wm = (PARPORT_CONTROL_STROBE |
272 PARPORT_CONTROL_AUTOFD |
273 PARPORT_CONTROL_INIT |
274 PARPORT_CONTROL_SELECT);
276 /* Take this out when drivers have adapted to the newer interface. */
277 if (mask & 0x20) {
278 printk (KERN_DEBUG "%s (%s): use data_reverse for this!\n",
279 p->name, p->cad->name);
280 parport_pc_data_reverse (p);
283 /* Restrict mask and val to control lines. */
284 mask &= wm;
285 val &= wm;
287 return __frob_control (p, mask, val);
290 unsigned char parport_pc_read_status(struct parport *p)
292 return inb (STATUS (p));
295 void parport_pc_disable_irq(struct parport *p)
297 __frob_control (p, 0x10, 0);
300 void parport_pc_enable_irq(struct parport *p)
302 __frob_control (p, 0x10, 0x10);
305 void parport_pc_data_forward (struct parport *p)
307 __frob_control (p, 0x20, 0);
310 void parport_pc_data_reverse (struct parport *p)
312 __frob_control (p, 0x20, 0x20);
315 void parport_pc_init_state(struct pardevice *dev, struct parport_state *s)
317 struct parport_pc_private *priv = dev->port->physport->private_data;
318 priv->ctr = s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
319 s->u.pc.ecr = 0x24;
322 void parport_pc_save_state(struct parport *p, struct parport_state *s)
324 const struct parport_pc_private *priv = p->physport->private_data;
325 s->u.pc.ctr = inb (CONTROL (p));
326 if (priv->ecr)
327 s->u.pc.ecr = inb (ECONTROL (p));
330 void parport_pc_restore_state(struct parport *p, struct parport_state *s)
332 const struct parport_pc_private *priv = p->physport->private_data;
333 outb (s->u.pc.ctr, CONTROL (p));
334 if (priv->ecr)
335 outb (s->u.pc.ecr, ECONTROL (p));
338 #ifdef CONFIG_PARPORT_1284
339 static size_t parport_pc_epp_read_data (struct parport *port, void *buf,
340 size_t length, int flags)
342 size_t got = 0;
343 for (; got < length; got++) {
344 *((char*)buf)++ = inb (EPPDATA(port));
345 if (inb (STATUS(port)) & 0x01) {
346 clear_epp_timeout (port);
347 break;
351 return got;
354 static size_t parport_pc_epp_write_data (struct parport *port, const void *buf,
355 size_t length, int flags)
357 size_t written = 0;
358 for (; written < length; written++) {
359 outb (*((char*)buf)++, EPPDATA(port));
360 if (inb (STATUS(port)) & 0x01) {
361 clear_epp_timeout (port);
362 break;
366 return written;
369 static size_t parport_pc_epp_read_addr (struct parport *port, void *buf,
370 size_t length, int flags)
372 size_t got = 0;
373 for (; got < length; got++) {
374 *((char*)buf)++ = inb (EPPADDR (port));
375 if (inb (STATUS (port)) & 0x01) {
376 clear_epp_timeout (port);
377 break;
381 return got;
384 static size_t parport_pc_epp_write_addr (struct parport *port,
385 const void *buf, size_t length,
386 int flags)
388 size_t written = 0;
389 for (; written < length; written++) {
390 outb (*((char*)buf)++, EPPADDR (port));
391 if (inb (STATUS (port)) & 0x01) {
392 clear_epp_timeout (port);
393 break;
397 return written;
400 static size_t parport_pc_ecpepp_read_data (struct parport *port, void *buf,
401 size_t length, int flags)
403 size_t got;
405 frob_econtrol (port, 0xe0, ECR_EPP << 5);
406 got = parport_pc_epp_read_data (port, buf, length, flags);
407 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
409 return got;
412 static size_t parport_pc_ecpepp_write_data (struct parport *port,
413 const void *buf, size_t length,
414 int flags)
416 size_t written;
418 frob_econtrol (port, 0xe0, ECR_EPP << 5);
419 written = parport_pc_epp_write_data (port, buf, length, flags);
420 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
422 return written;
425 static size_t parport_pc_ecpepp_read_addr (struct parport *port, void *buf,
426 size_t length, int flags)
428 size_t got;
430 frob_econtrol (port, 0xe0, ECR_EPP << 5);
431 got = parport_pc_epp_read_addr (port, buf, length, flags);
432 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
434 return got;
437 static size_t parport_pc_ecpepp_write_addr (struct parport *port,
438 const void *buf, size_t length,
439 int flags)
441 size_t written;
443 frob_econtrol (port, 0xe0, ECR_EPP << 5);
444 written = parport_pc_epp_write_addr (port, buf, length, flags);
445 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
447 return written;
449 #endif /* IEEE 1284 support */
451 #ifdef CONFIG_PARPORT_PC_FIFO
452 static size_t parport_pc_fifo_write_block_pio (struct parport *port,
453 const void *buf, size_t length)
455 int ret = 0;
456 const unsigned char *bufp = buf;
457 size_t left = length;
458 long expire = jiffies + port->physport->cad->timeout;
459 const int fifo = FIFO (port);
460 int poll_for = 8; /* 80 usecs */
461 const struct parport_pc_private *priv = port->physport->private_data;
462 const int fifo_depth = priv->fifo_depth;
464 port = port->physport;
466 /* We don't want to be interrupted every character. */
467 parport_pc_disable_irq (port);
468 frob_econtrol (port, (1<<4), (1<<4)); /* nErrIntrEn */
470 /* Forward mode. */
471 parport_pc_data_forward (port);
473 while (left) {
474 unsigned char byte;
475 unsigned char ecrval = inb (ECONTROL (port));
476 int i = 0;
478 if (current->need_resched && time_before (jiffies, expire))
479 /* Can't yield the port. */
480 schedule ();
482 /* Anyone else waiting for the port? */
483 if (port->waithead) {
484 printk (KERN_DEBUG "Somebody wants the port\n");
485 break;
488 if (ecrval & 0x02) {
489 /* FIFO is full. Wait for interrupt. */
491 /* Clear serviceIntr */
492 outb (ecrval & ~(1<<2), ECONTROL (port));
493 false_alarm:
494 ret = parport_wait_event (port, HZ);
495 if (ret < 0) break;
496 ret = 0;
497 if (!time_before (jiffies, expire)) {
498 /* Timed out. */
499 printk (KERN_DEBUG "Timed out\n");
500 break;
502 ecrval = inb (ECONTROL (port));
503 if (!(ecrval & (1<<2))) {
504 if (current->need_resched &&
505 time_before (jiffies, expire))
506 schedule ();
508 goto false_alarm;
511 continue;
514 /* Can't fail now. */
515 expire = jiffies + port->cad->timeout;
517 poll:
518 if (signal_pending (current))
519 break;
521 if (ecrval & 0x01) {
522 /* FIFO is empty. Blast it full. */
523 const int n = left < fifo_depth ? left : fifo_depth;
524 outsb (fifo, bufp, n);
525 bufp += n;
526 left -= n;
528 /* Adjust the poll time. */
529 if (i < (poll_for - 2)) poll_for--;
530 continue;
531 } else if (i++ < poll_for) {
532 udelay (10);
533 ecrval = inb (ECONTROL (port));
534 goto poll;
537 /* Half-full (call me an optimist) */
538 byte = *bufp++;
539 outb (byte, fifo);
540 left--;
543 return length - left;
546 static size_t parport_pc_fifo_write_block_dma (struct parport *port,
547 const void *buf, size_t length)
549 int ret = 0;
550 unsigned long dmaflag;
551 size_t left = length;
552 const struct parport_pc_private *priv = port->physport->private_data;
554 port = port->physport;
556 /* We don't want to be interrupted every character. */
557 parport_pc_disable_irq (port);
558 frob_econtrol (port, (1<<4), (1<<4)); /* nErrIntrEn */
560 /* Forward mode. */
561 parport_pc_data_forward (port);
563 while (left) {
564 long expire = jiffies + port->physport->cad->timeout;
566 size_t count = left;
568 if (count > PAGE_SIZE)
569 count = PAGE_SIZE;
571 memcpy(priv->dma_buf, buf, count);
573 dmaflag = claim_dma_lock();
574 disable_dma(port->dma);
575 clear_dma_ff(port->dma);
576 set_dma_mode(port->dma, DMA_MODE_WRITE);
577 set_dma_addr(port->dma, virt_to_bus((volatile char *) priv->dma_buf));
578 set_dma_count(port->dma, count);
580 /* Set DMA mode */
581 frob_econtrol (port, 1<<3, 1<<3);
583 /* Clear serviceIntr */
584 frob_econtrol (port, 1<<2, 0);
586 enable_dma(port->dma);
587 release_dma_lock(dmaflag);
589 /* assume DMA will be successful */
590 left -= count;
591 buf += count;
593 /* Wait for interrupt. */
594 false_alarm:
595 ret = parport_wait_event (port, HZ);
596 if (ret < 0) break;
597 ret = 0;
598 if (!time_before (jiffies, expire)) {
599 /* Timed out. */
600 printk (KERN_DEBUG "Timed out\n");
601 break;
603 /* Is serviceIntr set? */
604 if (!(inb (ECONTROL (port)) & (1<<2))) {
605 if (current->need_resched)
606 schedule ();
608 goto false_alarm;
611 dmaflag = claim_dma_lock();
612 disable_dma(port->dma);
613 clear_dma_ff(port->dma);
614 count = get_dma_residue(port->dma);
615 release_dma_lock(dmaflag);
617 if (current->need_resched)
618 /* Can't yield the port. */
619 schedule ();
621 /* Anyone else waiting for the port? */
622 if (port->waithead) {
623 printk (KERN_DEBUG "Somebody wants the port\n");
624 break;
627 /* update for possible DMA residue ! */
628 buf -= count;
629 left += count;
632 /* Maybe got here through break, so adjust for DMA residue! */
633 dmaflag = claim_dma_lock();
634 disable_dma(port->dma);
635 clear_dma_ff(port->dma);
636 left += get_dma_residue(port->dma);
637 release_dma_lock(dmaflag);
639 /* Turn off DMA mode */
640 frob_econtrol (port, 1<<3, 0);
642 return length - left;
645 /* Parallel Port FIFO mode (ECP chipsets) */
646 size_t parport_pc_compat_write_block_pio (struct parport *port,
647 const void *buf, size_t length,
648 int flags)
650 size_t written;
652 /* Special case: a timeout of zero means we cannot call schedule(). */
653 if (!port->physport->cad->timeout)
654 return parport_ieee1284_write_compat (port, buf,
655 length, flags);
657 /* Set up parallel port FIFO mode.*/
658 change_mode (port, ECR_PPF); /* Parallel port FIFO */
659 parport_pc_data_forward (port);
660 port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
662 /* Write the data to the FIFO. */
663 if (port->dma != PARPORT_DMA_NONE)
664 written = parport_pc_fifo_write_block_dma (port, buf, length);
665 else
666 written = parport_pc_fifo_write_block_pio (port, buf, length);
668 /* Finish up. */
669 if (change_mode (port, ECR_PS2) == -EBUSY) {
670 const struct parport_pc_private *priv =
671 port->physport->private_data;
673 printk (KERN_DEBUG "%s: FIFO is stuck\n", port->name);
675 /* Prevent further data transfer. */
676 parport_frob_control (port,
677 PARPORT_CONTROL_STROBE,
678 PARPORT_CONTROL_STROBE);
680 /* Adjust for the contents of the FIFO. */
681 for (written -= priv->fifo_depth; ; written++) {
682 if (inb (ECONTROL (port)) & 0x2)
683 /* Full up. */
684 break;
686 outb (0, FIFO (port));
689 /* Reset the FIFO. */
690 frob_econtrol (port, 0xe0, 0);
692 /* De-assert strobe. */
693 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
696 parport_wait_peripheral (port,
697 PARPORT_STATUS_BUSY,
698 PARPORT_STATUS_BUSY);
699 port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
701 return written;
704 /* ECP */
705 #ifdef CONFIG_PARPORT_1284
706 size_t parport_pc_ecp_write_block_pio (struct parport *port,
707 const void *buf, size_t length,
708 int flags)
710 size_t written;
712 /* Special case: a timeout of zero means we cannot call schedule(). */
713 if (!port->physport->cad->timeout)
714 return parport_ieee1284_ecp_write_data (port, buf,
715 length, flags);
717 /* Switch to forward mode if necessary. */
718 if (port->physport->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
719 /* Event 47: Set nInit high. */
720 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
722 /* Event 40: PError goes high. */
723 parport_wait_peripheral (port,
724 PARPORT_STATUS_PAPEROUT,
725 PARPORT_STATUS_PAPEROUT);
728 /* Set up ECP parallel port mode.*/
729 change_mode (port, ECR_ECP); /* ECP FIFO */
730 parport_pc_data_forward (port);
731 port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
733 /* Write the data to the FIFO. */
734 if (port->dma != PARPORT_DMA_NONE)
735 written = parport_pc_fifo_write_block_dma (port, buf, length);
736 else
737 written = parport_pc_fifo_write_block_pio (port, buf, length);
739 /* Finish up. */
740 if (change_mode (port, ECR_PS2) == -EBUSY) {
741 const struct parport_pc_private *priv =
742 port->physport->private_data;
744 printk (KERN_DEBUG "%s: FIFO is stuck\n", port->name);
746 /* Prevent further data transfer. */
747 parport_frob_control (port,
748 PARPORT_CONTROL_STROBE,
749 PARPORT_CONTROL_STROBE);
751 /* Adjust for the contents of the FIFO. */
752 for (written -= priv->fifo_depth; ; written++) {
753 if (inb (ECONTROL (port)) & 0x2)
754 /* Full up. */
755 break;
757 outb (0, FIFO (port));
760 /* Reset the FIFO. */
761 frob_econtrol (port, 0xe0, 0);
762 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
764 /* Host transfer recovery. */
765 parport_frob_control (port,
766 PARPORT_CONTROL_INIT,
767 PARPORT_CONTROL_INIT);
768 parport_pc_data_reverse (port);
769 parport_wait_peripheral (port, PARPORT_STATUS_PAPEROUT, 0);
770 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
771 parport_wait_peripheral (port,
772 PARPORT_STATUS_PAPEROUT,
773 PARPORT_STATUS_PAPEROUT);
776 parport_wait_peripheral (port,
777 PARPORT_STATUS_BUSY,
778 PARPORT_STATUS_BUSY);
779 port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
781 return written;
784 size_t parport_pc_ecp_read_block_pio (struct parport *port,
785 void *buf, size_t length, int flags)
787 size_t left = length;
788 size_t fifofull;
789 const int fifo = FIFO(port);
790 const struct parport_pc_private *priv = port->physport->private_data;
791 const int fifo_depth = priv->fifo_depth;
792 char *bufp = buf;
794 port = port->physport;
796 /* Special case: a timeout of zero means we cannot call schedule(). */
797 if (!port->cad->timeout)
798 return parport_ieee1284_ecp_read_data (port, buf,
799 length, flags);
801 fifofull = fifo_depth;
802 if (port->ieee1284.mode == IEEE1284_MODE_ECPRLE)
803 /* If the peripheral is allowed to send RLE compressed
804 * data, it is possible for a byte to expand to 128
805 * bytes in the FIFO. */
806 fifofull = 128;
808 /* If the caller wants less than a full FIFO's worth of data,
809 * go through software emulation. Otherwise we may have to through
810 * away data. */
811 if (length < fifofull)
812 return parport_ieee1284_ecp_read_data (port, buf,
813 length, flags);
815 /* Switch to reverse mode if necessary. */
816 if (port->ieee1284.phase != IEEE1284_PH_REV_IDLE) {
817 /* Event 38: Set nAutoFd low */
818 parport_frob_control (port,
819 PARPORT_CONTROL_AUTOFD,
820 PARPORT_CONTROL_AUTOFD);
821 parport_pc_data_reverse (port);
822 udelay (5);
824 /* Event 39: Set nInit low to initiate bus reversal */
825 parport_frob_control (port,
826 PARPORT_CONTROL_INIT,
827 PARPORT_CONTROL_INIT);
829 /* Event 40: PError goes low */
830 parport_wait_peripheral (port, PARPORT_STATUS_PAPEROUT, 0);
833 /* Set up ECP parallel port mode.*/
834 change_mode (port, ECR_ECP); /* ECP FIFO */
835 parport_pc_data_reverse (port);
836 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
838 /* Do the transfer. */
839 while (left > fifofull) {
840 int ret;
841 long int expire = jiffies + port->cad->timeout;
842 unsigned char ecrval = inb (ECONTROL (port));
844 if (current->need_resched && time_before (jiffies, expire))
845 /* Can't yield the port. */
846 schedule ();
848 /* At this point, the FIFO may already be full.
849 * Ideally, we'd be able to tell the port to hold on
850 * for a second while we empty the FIFO, and we'd be
851 * able to ensure that no data is lost. I'm not sure
852 * that's the case. :-( It might be that you can play
853 * games with STB, as in the forward case; someone should
854 * look at a datasheet. */
856 if (ecrval & 0x01) {
857 /* FIFO is empty. Wait for interrupt. */
859 /* Anyone else waiting for the port? */
860 if (port->waithead) {
861 printk (KERN_DEBUG
862 "Somebody wants the port\n");
863 break;
866 /* Clear serviceIntr */
867 outb (ecrval & ~(1<<2), ECONTROL (port));
868 false_alarm:
869 ret = parport_wait_event (port, HZ);
870 if (ret < 0) break;
871 ret = 0;
872 if (!time_before (jiffies, expire)) {
873 /* Timed out. */
874 printk (KERN_DEBUG "Timed out\n");
875 break;
877 ecrval = inb (ECONTROL (port));
878 if (!(ecrval & (1<<2))) {
879 if (current->need_resched &&
880 time_before (jiffies, expire))
881 schedule ();
883 goto false_alarm;
886 continue;
889 if (ecrval & 0x02) {
890 /* FIFO is full. */
891 insb (fifo, bufp, fifo_depth);
892 bufp += fifo_depth;
893 left -= fifo_depth;
894 continue;
897 *bufp++ = inb (fifo);
898 left--;
901 /* Finish up. */
902 if (change_mode (port, ECR_PS2) == -EBUSY) {
903 int lost = get_fifo_residue (port);
904 printk (KERN_DEBUG "%s: DATA LOSS (%d bytes)!\n", port->name,
905 lost);
908 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
910 return length - left;
913 #endif /* IEEE 1284 support */
915 #endif /* Allowed to use FIFO/DMA */
917 void parport_pc_inc_use_count(void)
919 #ifdef MODULE
920 MOD_INC_USE_COUNT;
921 #endif
924 void parport_pc_dec_use_count(void)
926 #ifdef MODULE
927 MOD_DEC_USE_COUNT;
928 #endif
931 static void parport_pc_fill_inode(struct inode *inode, int fill)
933 /* Is this still needed? -tim */
934 #ifdef MODULE
935 if (fill)
936 MOD_INC_USE_COUNT;
937 else
938 MOD_DEC_USE_COUNT;
939 #endif
942 struct parport_operations parport_pc_ops =
944 parport_pc_write_data,
945 parport_pc_read_data,
947 parport_pc_write_control,
948 parport_pc_read_control,
949 parport_pc_frob_control,
951 parport_pc_read_status,
953 parport_pc_enable_irq,
954 parport_pc_disable_irq,
956 parport_pc_data_forward,
957 parport_pc_data_reverse,
959 parport_pc_interrupt,
960 parport_pc_init_state,
961 parport_pc_save_state,
962 parport_pc_restore_state,
964 parport_pc_inc_use_count,
965 parport_pc_dec_use_count,
966 parport_pc_fill_inode,
968 parport_ieee1284_epp_write_data,
969 parport_ieee1284_epp_read_data,
970 parport_ieee1284_epp_write_addr,
971 parport_ieee1284_epp_read_addr,
973 parport_ieee1284_ecp_write_data,
974 parport_ieee1284_ecp_read_data,
975 parport_ieee1284_ecp_write_addr,
977 parport_ieee1284_write_compat,
978 parport_ieee1284_read_nibble,
979 parport_ieee1284_read_byte,
982 /* --- Mode detection ------------------------------------- */
985 * Checks for port existence, all ports support SPP MODE
987 static int __init parport_SPP_supported(struct parport *pb)
989 unsigned char r, w;
992 * first clear an eventually pending EPP timeout
993 * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
994 * that does not even respond to SPP cycles if an EPP
995 * timeout is pending
997 clear_epp_timeout(pb);
999 /* Do a simple read-write test to make sure the port exists. */
1000 w = 0xc;
1001 outb (w, CONTROL (pb));
1003 /* Is there a control register that we can read from? Some
1004 * ports don't allow reads, so read_control just returns a
1005 * software copy. Some ports _do_ allow reads, so bypass the
1006 * software copy here. In addition, some bits aren't
1007 * writable. */
1008 r = inb (CONTROL (pb));
1009 if ((r & 0xf) == w) {
1010 w = 0xe;
1011 outb (w, CONTROL (pb));
1012 r = inb (CONTROL (pb));
1013 outb (0xc, CONTROL (pb));
1014 if ((r & 0xf) == w)
1015 return PARPORT_MODE_PCSPP;
1018 if (user_specified)
1019 /* That didn't work, but the user thinks there's a
1020 * port here. */
1021 printk (KERN_DEBUG "0x%lx: CTR: wrote 0x%02x, read 0x%02x\n",
1022 pb->base, w, r);
1024 /* Try the data register. The data lines aren't tri-stated at
1025 * this stage, so we expect back what we wrote. */
1026 w = 0xaa;
1027 parport_pc_write_data (pb, w);
1028 r = parport_pc_read_data (pb);
1029 if (r == w) {
1030 w = 0x55;
1031 parport_pc_write_data (pb, w);
1032 r = parport_pc_read_data (pb);
1033 if (r == w)
1034 return PARPORT_MODE_PCSPP;
1037 if (user_specified)
1038 /* Didn't work, but the user is convinced this is the
1039 * place. */
1040 printk (KERN_DEBUG "0x%lx: DATA: wrote 0x%02x, read 0x%02x\n",
1041 pb->base, w, r);
1043 /* It's possible that we can't read the control register or
1044 * the data register. In that case just believe the user. */
1045 if (user_specified)
1046 return PARPORT_MODE_PCSPP;
1048 return 0;
1051 /* Check for ECR
1053 * Old style XT ports alias io ports every 0x400, hence accessing ECR
1054 * on these cards actually accesses the CTR.
1056 * Modern cards don't do this but reading from ECR will return 0xff
1057 * regardless of what is written here if the card does NOT support
1058 * ECP.
1060 * We first check to see if ECR is the same as CTR. If not, the low
1061 * two bits of ECR aren't writable, so we check by writing ECR and
1062 * reading it back to see if it's what we expect.
1064 static int __init parport_ECR_present(struct parport *pb)
1066 struct parport_pc_private *priv = pb->private_data;
1067 unsigned char r = 0xc;
1069 priv->ecr = 0;
1070 outb (r, CONTROL (pb));
1071 if ((inb (ECONTROL (pb)) & 0x3) == (r & 0x3)) {
1072 outb (r ^ 0x2, CONTROL (pb)); /* Toggle bit 1 */
1074 r = inb (CONTROL (pb));
1075 if ((inb (ECONTROL (pb)) & 0x2) == (r & 0x2))
1076 goto no_reg; /* Sure that no ECR register exists */
1079 if ((inb (ECONTROL (pb)) & 0x3 ) != 0x1)
1080 goto no_reg;
1082 outb (0x34, ECONTROL (pb));
1083 if (inb (ECONTROL (pb)) != 0x35)
1084 goto no_reg;
1086 priv->ecr = 1;
1087 outb (0xc, CONTROL (pb));
1089 /* Go to mode 000 */
1090 frob_econtrol (pb, 0xe0, ECR_SPP << 5);
1092 return 1;
1094 no_reg:
1095 outb (0xc, CONTROL (pb));
1096 return 0;
1099 #ifdef CONFIG_PARPORT_1284
1100 /* Detect PS/2 support.
1102 * Bit 5 (0x20) sets the PS/2 data direction; setting this high
1103 * allows us to read data from the data lines. In theory we would get back
1104 * 0xff but any peripheral attached to the port may drag some or all of the
1105 * lines down to zero. So if we get back anything that isn't the contents
1106 * of the data register we deem PS/2 support to be present.
1108 * Some SPP ports have "half PS/2" ability - you can't turn off the line
1109 * drivers, but an external peripheral with sufficiently beefy drivers of
1110 * its own can overpower them and assert its own levels onto the bus, from
1111 * where they can then be read back as normal. Ports with this property
1112 * and the right type of device attached are likely to fail the SPP test,
1113 * (as they will appear to have stuck bits) and so the fact that they might
1114 * be misdetected here is rather academic.
1117 static int __init parport_PS2_supported(struct parport *pb)
1119 int ok = 0;
1121 clear_epp_timeout(pb);
1123 /* try to tri-state the buffer */
1124 parport_pc_data_reverse (pb);
1126 parport_pc_write_data(pb, 0x55);
1127 if (parport_pc_read_data(pb) != 0x55) ok++;
1129 parport_pc_write_data(pb, 0xaa);
1130 if (parport_pc_read_data(pb) != 0xaa) ok++;
1132 /* cancel input mode */
1133 parport_pc_data_forward (pb);
1135 if (ok)
1136 pb->modes |= PARPORT_MODE_TRISTATE;
1137 else {
1138 struct parport_pc_private *priv = pb->private_data;
1139 priv->ctr_writable &= ~0x20;
1142 return ok;
1145 static int __init parport_ECP_supported(struct parport *pb)
1147 int i;
1148 int config;
1149 int pword;
1150 struct parport_pc_private *priv = pb->private_data;
1152 /* If there is no ECR, we have no hope of supporting ECP. */
1153 if (!priv->ecr)
1154 return 0;
1156 /* Find out FIFO depth */
1157 outb (ECR_SPP << 5, ECONTROL (pb)); /* Reset FIFO */
1158 outb (ECR_TST << 5, ECONTROL (pb)); /* TEST FIFO */
1159 for (i=0; i < 1024 && !(inb (ECONTROL (pb)) & 0x02); i++)
1160 outb (0xaa, FIFO (pb));
1163 * Using LGS chipset it uses ECR register, but
1164 * it doesn't support ECP or FIFO MODE
1166 if (i == 1024) {
1167 outb (ECR_SPP << 5, ECONTROL (pb));
1168 return 0;
1171 priv->fifo_depth = i;
1172 printk (KERN_INFO "0x%lx: FIFO is %d bytes\n", pb->base, i);
1174 /* Find out writeIntrThreshold */
1175 frob_econtrol (pb, 1<<2, 1<<2);
1176 frob_econtrol (pb, 1<<2, 0);
1177 for (i = 1; i <= priv->fifo_depth; i++) {
1178 inb (FIFO (pb));
1179 udelay (50);
1180 if (inb (ECONTROL (pb)) & (1<<2))
1181 break;
1184 if (i <= priv->fifo_depth)
1185 printk (KERN_INFO "0x%lx: writeIntrThreshold is %d\n",
1186 pb->base, i);
1187 else
1188 /* Number of bytes we know we can write if we get an
1189 interrupt. */
1190 i = 0;
1192 priv->writeIntrThreshold = i;
1194 /* Find out readIntrThreshold */
1195 frob_econtrol (pb, 0xe0, ECR_PS2 << 5); /* Reset FIFO */
1196 parport_pc_data_reverse (pb);
1197 frob_econtrol (pb, 0xe0, ECR_TST << 5); /* Test FIFO */
1198 frob_econtrol (pb, 1<<2, 1<<2);
1199 frob_econtrol (pb, 1<<2, 0);
1200 for (i = 1; i <= priv->fifo_depth; i++) {
1201 outb (0xaa, FIFO (pb));
1202 if (inb (ECONTROL (pb)) & (1<<2))
1203 break;
1206 if (i <= priv->fifo_depth)
1207 printk (KERN_INFO "0x%lx: readIntrThreshold is %d\n",
1208 pb->base, i);
1209 else
1210 /* Number of bytes we can read if we get an interrupt. */
1211 i = 0;
1213 priv->readIntrThreshold = i;
1215 outb (ECR_SPP << 5, ECONTROL (pb)); /* Reset FIFO */
1216 outb (0xf4, ECONTROL (pb)); /* Configuration mode */
1217 config = inb (FIFO (pb));
1218 pword = (config >> 4) & 0x7;
1219 switch (pword) {
1220 case 0:
1221 pword = 2;
1222 printk (KERN_WARNING "0x%lx: Unsupported pword size!\n",
1223 pb->base);
1224 break;
1225 case 2:
1226 pword = 4;
1227 printk (KERN_WARNING "0x%lx: Unsupported pword size!\n",
1228 pb->base);
1229 break;
1230 default:
1231 printk (KERN_WARNING "0x%lx: Unknown implementation ID\n",
1232 pb->base);
1233 /* Assume 1 */
1234 case 1:
1235 pword = 1;
1237 priv->pword = pword;
1238 printk (KERN_DEBUG "0x%lx: PWord is %d bits\n", pb->base, 8 * pword);
1240 config = inb (CONFIGB (pb));
1241 printk (KERN_DEBUG "0x%lx: Interrupts are ISA-%s\n", pb->base,
1242 config & 0x80 ? "Level" : "Pulses");
1244 if (!(config & 0x40)) {
1245 printk (KERN_WARNING "0x%lx: IRQ conflict!\n", pb->base);
1246 pb->irq = PARPORT_IRQ_NONE;
1249 /* Go back to mode 000 */
1250 frob_econtrol (pb, 0xe0, ECR_SPP << 5);
1251 pb->modes |= PARPORT_MODE_ECP;
1253 return 1;
1256 static int __init parport_ECPPS2_supported(struct parport *pb)
1258 const struct parport_pc_private *priv = pb->private_data;
1259 int result;
1260 unsigned char oecr;
1262 if (!priv->ecr)
1263 return 0;
1265 oecr = inb (ECONTROL (pb));
1266 outb (ECR_PS2 << 5, ECONTROL (pb));
1268 result = parport_PS2_supported(pb);
1270 outb (oecr, ECONTROL (pb));
1271 return result;
1274 /* EPP mode detection */
1276 static int __init parport_EPP_supported(struct parport *pb)
1278 /* If EPP timeout bit clear then EPP available */
1279 if (!clear_epp_timeout(pb))
1280 return 0; /* No way to clear timeout */
1283 * Theory:
1284 * Bit 0 of STR is the EPP timeout bit, this bit is 0
1285 * when EPP is possible and is set high when an EPP timeout
1286 * occurs (EPP uses the HALT line to stop the CPU while it does
1287 * the byte transfer, an EPP timeout occurs if the attached
1288 * device fails to respond after 10 micro seconds).
1290 * This bit is cleared by either reading it (National Semi)
1291 * or writing a 1 to the bit (SMC, UMC, WinBond), others ???
1292 * This bit is always high in non EPP modes.
1295 parport_pc_data_reverse (pb);
1296 parport_pc_enable_irq (pb);
1297 clear_epp_timeout(pb);
1299 inb (EPPDATA (pb));
1300 udelay(30); /* Wait for possible EPP timeout */
1302 if (parport_pc_read_status(pb) & 0x01)
1303 goto supported;
1306 * Theory:
1307 * Write two values to the EPP address register and
1308 * read them back. When the transfer times out, the state of
1309 * the EPP register is undefined in some cases (EPP 1.9?) but
1310 * in others (EPP 1.7, ECPEPP?) it is possible to read back
1311 * its value.
1313 clear_epp_timeout(pb);
1314 udelay(30); /* Wait for possible EPP timeout */
1316 /* Enable outputs. */
1317 parport_pc_data_forward (pb);
1318 outb (0x55, EPPADDR (pb));
1320 clear_epp_timeout(pb);
1321 udelay(30); /* Wait for possible EPP timeout */
1323 /* We must enable the outputs to be able to read the address
1324 register. */
1325 parport_pc_data_forward (pb);
1327 if (inb (EPPADDR (pb)) == 0x55) {
1328 clear_epp_timeout(pb);
1329 udelay(30); /* Wait for possible EPP timeout */
1330 outb (0xaa, EPPADDR (pb));
1332 if (inb (EPPADDR (pb)) == 0xaa)
1333 goto supported;
1336 return 0;
1338 supported:
1339 clear_epp_timeout(pb);
1340 pb->modes |= PARPORT_MODE_EPP;
1342 /* Set up access functions to use EPP hardware. */
1343 parport_pc_ops.epp_read_data = parport_pc_epp_read_data;
1344 parport_pc_ops.epp_write_data = parport_pc_epp_write_data;
1345 parport_pc_ops.epp_read_addr = parport_pc_epp_read_addr;
1346 parport_pc_ops.epp_write_addr = parport_pc_epp_write_addr;
1348 return 1;
1351 static int __init parport_ECPEPP_supported(struct parport *pb)
1353 struct parport_pc_private *priv = pb->private_data;
1354 int result;
1355 unsigned char oecr;
1357 if (!priv->ecr)
1358 return 0;
1360 oecr = inb (ECONTROL (pb));
1361 /* Search for SMC style EPP+ECP mode */
1362 outb (0x80, ECONTROL (pb));
1364 result = parport_EPP_supported(pb);
1366 outb (oecr, ECONTROL (pb));
1368 if (result) {
1369 /* Set up access functions to use ECP+EPP hardware. */
1370 parport_pc_ops.epp_read_data = parport_pc_ecpepp_read_data;
1371 parport_pc_ops.epp_write_data = parport_pc_ecpepp_write_data;
1372 parport_pc_ops.epp_read_addr = parport_pc_ecpepp_read_addr;
1373 parport_pc_ops.epp_write_addr = parport_pc_ecpepp_write_addr;
1376 return result;
1379 #else /* No IEEE 1284 support */
1381 /* Don't bother probing for modes we know we won't use. */
1382 static int __init parport_PS2_supported(struct parport *pb) { return 0; }
1383 static int __init parport_ECP_supported(struct parport *pb) { return 0; }
1384 static int __init parport_EPP_supported(struct parport *pb) { return 0; }
1385 static int __init parport_ECPEPP_supported(struct parport *pb) { return 0; }
1386 static int __init parport_ECPPS2_supported(struct parport *pb) { return 0; }
1388 #endif /* No IEEE 1284 support */
1390 /* --- IRQ detection -------------------------------------- */
1392 /* Only if supports ECP mode */
1393 static int __init programmable_irq_support(struct parport *pb)
1395 int irq, intrLine;
1396 unsigned char oecr = inb (ECONTROL (pb));
1397 static const int lookup[8] = {
1398 PARPORT_IRQ_NONE, 7, 9, 10, 11, 14, 15, 5
1401 outb (ECR_CNF << 5, ECONTROL (pb)); /* Configuration MODE */
1403 intrLine = (inb (CONFIGB (pb)) >> 3) & 0x07;
1404 irq = lookup[intrLine];
1406 outb (oecr, ECONTROL (pb));
1407 return irq;
1410 static int __init irq_probe_ECP(struct parport *pb)
1412 int irqs, i;
1414 sti();
1415 irqs = probe_irq_on();
1417 outb (ECR_SPP << 5, ECONTROL (pb)); /* Reset FIFO */
1418 outb ((ECR_TST << 5) | 0x04, ECONTROL (pb));
1419 outb (ECR_TST << 5, ECONTROL (pb));
1421 /* If Full FIFO sure that writeIntrThreshold is generated */
1422 for (i=0; i < 1024 && !(inb (ECONTROL (pb)) & 0x02) ; i++)
1423 outb (0xaa, FIFO (pb));
1425 pb->irq = probe_irq_off(irqs);
1426 outb (ECR_SPP << 5, ECONTROL (pb));
1428 if (pb->irq <= 0)
1429 pb->irq = PARPORT_IRQ_NONE;
1431 return pb->irq;
1435 * This detection seems that only works in National Semiconductors
1436 * This doesn't work in SMC, LGS, and Winbond
1438 static int __init irq_probe_EPP(struct parport *pb)
1440 #ifndef ADVANCED_DETECT
1441 return PARPORT_IRQ_NONE;
1442 #else
1443 int irqs;
1444 unsigned char oecr;
1446 if (pb->modes & PARPORT_MODE_PCECR)
1447 oecr = inb (ECONTROL (pb));
1449 sti();
1450 irqs = probe_irq_on();
1452 if (pb->modes & PARPORT_MODE_PCECR)
1453 frob_econtrol (pb, 0x10, 0x10);
1455 clear_epp_timeout(pb);
1456 parport_pc_frob_control (pb, 0x20, 0x20);
1457 parport_pc_frob_control (pb, 0x10, 0x10);
1458 clear_epp_timeout(pb);
1460 /* Device isn't expecting an EPP read
1461 * and generates an IRQ.
1463 parport_pc_read_epp(pb);
1464 udelay(20);
1466 pb->irq = probe_irq_off (irqs);
1467 if (pb->modes & PARPORT_MODE_PCECR)
1468 outb (oecr, ECONTROL (pb));
1469 parport_pc_write_control(pb, 0xc);
1471 if (pb->irq <= 0)
1472 pb->irq = PARPORT_IRQ_NONE;
1474 return pb->irq;
1475 #endif /* Advanced detection */
1478 static int __init irq_probe_SPP(struct parport *pb)
1480 /* Don't even try to do this. */
1481 return PARPORT_IRQ_NONE;
1484 /* We will attempt to share interrupt requests since other devices
1485 * such as sound cards and network cards seem to like using the
1486 * printer IRQs.
1488 * When ECP is available we can autoprobe for IRQs.
1489 * NOTE: If we can autoprobe it, we can register the IRQ.
1491 static int __init parport_irq_probe(struct parport *pb)
1493 const struct parport_pc_private *priv = pb->private_data;
1495 if (priv->ecr) {
1496 pb->irq = programmable_irq_support(pb);
1497 if (pb->irq != PARPORT_IRQ_NONE)
1498 goto out;
1501 if (pb->modes & PARPORT_MODE_ECP)
1502 pb->irq = irq_probe_ECP(pb);
1504 if (pb->irq == PARPORT_IRQ_NONE && priv->ecr &&
1505 (pb->modes & PARPORT_MODE_EPP))
1506 pb->irq = irq_probe_EPP(pb);
1508 clear_epp_timeout(pb);
1510 if (pb->irq == PARPORT_IRQ_NONE && (pb->modes & PARPORT_MODE_EPP))
1511 pb->irq = irq_probe_EPP(pb);
1513 clear_epp_timeout(pb);
1515 if (pb->irq == PARPORT_IRQ_NONE)
1516 pb->irq = irq_probe_SPP(pb);
1518 out:
1519 return pb->irq;
1522 /* --- DMA detection -------------------------------------- */
1524 /* Only if supports ECP mode */
1525 static int __init programmable_dma_support (struct parport *p)
1527 unsigned char oecr = inb (ECONTROL (p));
1528 int dma;
1530 frob_econtrol (p, 0xe0, ECR_CNF << 5);
1532 dma = inb (CONFIGB(p)) & 0x03;
1533 if (!dma)
1534 dma = PARPORT_DMA_NONE;
1536 outb (oecr, ECONTROL (p));
1537 return dma;
1540 static int __init parport_dma_probe (struct parport *p)
1542 const struct parport_pc_private *priv = p->private_data;
1543 if (priv->ecr)
1544 p->dma = programmable_dma_support(p);
1546 return p->dma;
1549 /* --- Initialisation code -------------------------------- */
1551 static int __init probe_one_port(unsigned long int base,
1552 unsigned long int base_hi,
1553 int irq, int dma)
1555 struct parport_pc_private *priv;
1556 struct parport tmp;
1557 struct parport *p = &tmp;
1558 int probedirq = PARPORT_IRQ_NONE;
1559 if (check_region(base, 3)) return 0;
1560 priv = kmalloc (sizeof (struct parport_pc_private), GFP_KERNEL);
1561 if (!priv) {
1562 printk (KERN_DEBUG "parport (0x%lx): no memory!\n", base);
1563 return 0;
1565 priv->ctr = 0xc;
1566 priv->ctr_writable = 0xff;
1567 priv->ecr = 0;
1568 priv->fifo_depth = 0;
1569 priv->dma_buf = 0;
1570 p->base = base;
1571 p->base_hi = base_hi;
1572 p->irq = irq;
1573 p->dma = dma;
1574 p->modes = PARPORT_MODE_PCSPP;
1575 p->ops = &parport_pc_ops;
1576 p->private_data = priv;
1577 p->physport = p;
1578 if (base_hi && !check_region(base_hi,3)) {
1579 parport_ECR_present(p);
1580 parport_ECP_supported(p);
1581 parport_ECPPS2_supported(p);
1583 if (base != 0x3bc) {
1584 if (!check_region(base+0x3, 5)) {
1585 parport_ECPEPP_supported(p);
1586 parport_EPP_supported(p);
1589 if (!parport_SPP_supported (p)) {
1590 /* No port. */
1591 kfree (priv);
1592 return 0;
1595 parport_PS2_supported (p);
1597 if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
1598 PARPORT_DMA_NONE, &parport_pc_ops))) {
1599 kfree (priv);
1600 return 0;
1603 p->base_hi = base_hi;
1604 p->modes = tmp.modes;
1605 p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
1606 p->private_data = priv;
1608 printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
1609 if (p->base_hi && (p->modes & PARPORT_MODE_ECP))
1610 printk(" (0x%lx)", p->base_hi);
1611 p->irq = irq;
1612 p->dma = dma;
1613 if (p->irq == PARPORT_IRQ_AUTO) {
1614 p->irq = PARPORT_IRQ_NONE;
1615 parport_irq_probe(p);
1616 } else if (p->irq == PARPORT_IRQ_PROBEONLY) {
1617 p->irq = PARPORT_IRQ_NONE;
1618 parport_irq_probe(p);
1619 probedirq = p->irq;
1620 p->irq = PARPORT_IRQ_NONE;
1622 if (p->irq != PARPORT_IRQ_NONE) {
1623 printk(", irq %d", p->irq);
1625 if (p->dma == PARPORT_DMA_AUTO) {
1626 p->dma = PARPORT_DMA_NONE;
1627 parport_dma_probe(p);
1630 if (p->dma == PARPORT_DMA_AUTO)
1631 p->dma = PARPORT_DMA_NONE;
1632 if (p->dma != PARPORT_DMA_NONE)
1633 printk(", dma %d", p->dma);
1635 #ifdef CONFIG_PARPORT_PC_FIFO
1636 if (priv->fifo_depth > 0 && p->irq != PARPORT_IRQ_NONE) {
1637 parport_pc_ops.compat_write_data =
1638 parport_pc_compat_write_block_pio;
1639 #ifdef CONFIG_PARPORT_1284
1640 parport_pc_ops.ecp_write_data =
1641 parport_pc_ecp_write_block_pio;
1642 #endif /* IEEE 1284 support */
1643 if (p->dma != PARPORT_DMA_NONE)
1644 p->modes |= PARPORT_MODE_DMA;
1645 printk(", using FIFO");
1647 #endif /* Allowed to use FIFO/DMA */
1649 printk(" [");
1650 #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
1652 int f = 0;
1653 printmode(PCSPP);
1654 printmode(TRISTATE);
1655 printmode(COMPAT)
1656 printmode(EPP);
1657 printmode(ECP);
1658 printmode(DMA);
1660 #undef printmode
1661 printk("]\n");
1662 if (probedirq != PARPORT_IRQ_NONE)
1663 printk("%s: irq %d detected\n", p->name, probedirq);
1664 parport_proc_register(p);
1666 request_region (p->base, p->size, p->name);
1667 if (p->modes & PARPORT_MODE_ECP)
1668 request_region (p->base_hi, 3, p->name);
1670 if (p->irq != PARPORT_IRQ_NONE) {
1671 if (request_irq (p->irq, parport_pc_interrupt,
1672 0, p->name, p)) {
1673 printk (KERN_WARNING "%s: irq %d in use, "
1674 "resorting to polled operation\n",
1675 p->name, p->irq);
1676 p->irq = PARPORT_IRQ_NONE;
1677 p->dma = PARPORT_DMA_NONE;
1680 if (p->dma != PARPORT_DMA_NONE) {
1681 if (request_dma (p->dma, p->name)) {
1682 printk (KERN_WARNING "%s: dma %d in use, "
1683 "resorting to PIO operation\n",
1684 p->name, p->dma);
1685 p->dma = PARPORT_DMA_NONE;
1686 } else {
1687 priv->dma_buf = (char *) __get_dma_pages(GFP_KERNEL, 1);
1688 if (! priv->dma_buf) {
1689 printk (KERN_WARNING "%s: "
1690 "cannot get buffer for DMA, "
1691 "resorting to PIO operation\n",
1692 p->name);
1693 free_dma(p->dma);
1694 p->dma = PARPORT_DMA_NONE;
1700 /* Done probing. Now put the port into a sensible start-up state.
1701 * SELECT | INIT also puts IEEE1284-compliant devices into
1702 * compatibility mode. */
1703 if (p->modes & PARPORT_MODE_ECP)
1705 * Put the ECP detected port in PS2 mode.
1707 outb (0x24, ECONTROL (p));
1709 parport_pc_write_data(p, 0);
1710 parport_pc_write_control(p, PARPORT_CONTROL_SELECT);
1711 udelay (50);
1712 parport_pc_write_control(p,
1713 PARPORT_CONTROL_SELECT
1714 | PARPORT_CONTROL_INIT);
1715 udelay (50);
1717 /* Now that we've told the sharing engine about the port, and
1718 found out its characteristics, let the high-level drivers
1719 know about it. */
1720 parport_announce_port (p);
1722 return 1;
1725 /* Look for PCI parallel port cards. */
1726 static int __init parport_pc_init_pci (int irq, int dma)
1728 /* These need to go in pci.h: */
1729 #ifndef PCI_VENDOR_ID_SIIG
1730 #define PCI_VENDOR_ID_SIIG 0x131f
1731 #define PCI_DEVICE_ID_SIIG_1S1P_10x_550 0x1010
1732 #define PCI_DEVICE_ID_SIIG_1S1P_10x_650 0x1011
1733 #define PCI_DEVICE_ID_SIIG_1S1P_10x_850 0x1012
1734 #define PCI_DEVICE_ID_SIIG_1P_10x 0x1020
1735 #define PCI_DEVICE_ID_SIIG_2P_10x 0x1021
1736 #define PCI_DEVICE_ID_SIIG_2S1P_10x_550 0x1034
1737 #define PCI_DEVICE_ID_SIIG_2S1P_10x_650 0x1035
1738 #define PCI_DEVICE_ID_SIIG_2S1P_10x_850 0x1036
1739 #define PCI_DEVICE_ID_SIIG_1P_20x 0x2020
1740 #define PCI_DEVICE_ID_SIIG_2P_20x 0x2021
1741 #define PCI_DEVICE_ID_SIIG_2P1S_20x_550 0x2040
1742 #define PCI_DEVICE_ID_SIIG_2P1S_20x_650 0x2041
1743 #define PCI_DEVICE_ID_SIIG_2P1S_20x_850 0x2042
1744 #define PCI_DEVICE_ID_SIIG_1S1P_20x_550 0x2010
1745 #define PCI_DEVICE_ID_SIIG_1S1P_20x_650 0x2011
1746 #define PCI_DEVICE_ID_SIIG_1S1P_20x_850 0x2012
1747 #define PCI_DEVICE_ID_SIIG_2S1P_20x_550 0x2060
1748 #define PCI_DEVICE_ID_SIIG_2S1P_20x_650 0x2061
1749 #define PCI_DEVICE_ID_SIIG_2S1P_20x_850 0x2062
1750 #define PCI_VENDOR_ID_LAVA 0x1407
1751 #define PCI_DEVICE_ID_LAVA_PARALLEL 0x8000
1752 #define PCI_DEVICE_ID_LAVA_DUAL_PAR_A 0x8001 /* The Lava Dual Parallel is */
1753 #define PCI_DEVICE_ID_LAVA_DUAL_PAR_B 0x8002 /* two PCI devices on a card */
1754 #endif
1756 struct {
1757 unsigned int vendor;
1758 unsigned int device;
1759 unsigned int numports;
1760 struct {
1761 unsigned int lo;
1762 unsigned int hi; /* -ve if not there */
1763 } addr[4];
1764 } cards[] = {
1765 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_10x_550, 1,
1766 { { 3, 4 }, } },
1767 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_10x_650, 1,
1768 { { 3, 4 }, } },
1769 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_10x_850, 1,
1770 { { 3, 4 }, } },
1771 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1P_10x, 1,
1772 { { 2, 3 }, } },
1773 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P_10x, 2,
1774 { { 2, 3 }, { 4, 5 }, } },
1775 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_10x_550, 1,
1776 { { 4, 5 }, } },
1777 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_10x_650, 1,
1778 { { 4, 5 }, } },
1779 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_10x_850, 1,
1780 { { 4, 5 }, } },
1781 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1P_20x, 1,
1782 { { 0, 1 }, } },
1783 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P_20x, 2,
1784 { { 0, 1 }, { 2, 3 }, } },
1785 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P1S_20x_550, 2,
1786 { { 1, 2 }, { 3, 4 }, } },
1787 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P1S_20x_650, 2,
1788 { { 1, 2 }, { 3, 4 }, } },
1789 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P1S_20x_850, 2,
1790 { { 1, 2 }, { 3, 4 }, } },
1791 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_20x_550, 1,
1792 { { 1, 2 }, } },
1793 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_20x_650, 1,
1794 { { 1, 2 }, } },
1795 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_20x_850, 1,
1796 { { 1, 2 }, } },
1797 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_20x_550, 1,
1798 { { 2, 3 }, } },
1799 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_20x_650, 1,
1800 { { 2, 3 }, } },
1801 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_20x_850, 1,
1802 { { 2, 3 }, } },
1803 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PARALLEL, 1,
1804 { { 0, -1 }, } },
1805 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DUAL_PAR_A, 1,
1806 { { 0, -1 }, } },
1807 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DUAL_PAR_B, 1,
1808 { { 0, -1 }, } },
1809 { 0, }
1812 int count = 0;
1813 int i;
1815 if (!pci_present ())
1816 return 0;
1818 for (i = 0; cards[i].vendor; i++) {
1819 struct pci_dev *pcidev = NULL;
1820 while ((pcidev = pci_find_device (cards[i].vendor,
1821 cards[i].device,
1822 pcidev)) != NULL) {
1823 int n;
1824 for (n = 0; n < cards[i].numports; n++) {
1825 int lo = cards[i].addr[n].lo;
1826 int hi = cards[i].addr[n].hi;
1827 int io_lo = pcidev->base_address[lo];
1828 int io_hi = ((hi < 0) ? 0 :
1829 pcidev->base_address[hi]);
1830 io_lo &= PCI_BASE_ADDRESS_IO_MASK;
1831 io_hi &= PCI_BASE_ADDRESS_IO_MASK;
1832 count += probe_one_port (io_lo, io_hi,
1833 irq, dma);
1838 return count;
1841 int __init parport_pc_init(int *io, int *io_hi, int *irq, int *dma)
1843 int count = 0, i = 0;
1844 if (io && *io) {
1845 /* Only probe the ports we were given. */
1846 user_specified = 1;
1847 do {
1848 if (!*io_hi) *io_hi = 0x400 + *io;
1849 count += probe_one_port(*(io++), *(io_hi++),
1850 *(irq++), *(dma++));
1851 } while (*io && (++i < PARPORT_PC_MAX_PORTS));
1852 } else {
1853 /* Probe all the likely ports. */
1854 count += probe_one_port(0x3bc, 0x7bc, irq[0], dma[0]);
1855 count += probe_one_port(0x378, 0x778, irq[0], dma[0]);
1856 count += probe_one_port(0x278, 0x678, irq[0], dma[0]);
1857 count += parport_pc_init_pci (irq[0], dma[0]);
1860 return count;
1863 #ifdef MODULE
1864 static int io[PARPORT_PC_MAX_PORTS+1] = { [0 ... PARPORT_PC_MAX_PORTS] = 0 };
1865 static int io_hi[PARPORT_PC_MAX_PORTS+1] = { [0 ... PARPORT_PC_MAX_PORTS] = 0 };
1866 static int dmaval[PARPORT_PC_MAX_PORTS] = { [0 ... PARPORT_PC_MAX_PORTS-1] = PARPORT_DMA_AUTO };
1867 static int irqval[PARPORT_PC_MAX_PORTS] = { [0 ... PARPORT_PC_MAX_PORTS-1] = PARPORT_IRQ_PROBEONLY };
1868 static const char *irq[PARPORT_PC_MAX_PORTS] = { NULL, };
1869 static const char *dma[PARPORT_PC_MAX_PORTS] = { NULL, };
1870 MODULE_PARM(io, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "i");
1871 MODULE_PARM(io_hi, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "i");
1872 MODULE_PARM(irq, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "s");
1873 MODULE_PARM(dma, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "s");
1875 int init_module(void)
1877 /* Work out how many ports we have, then get parport_share to parse
1878 the irq values. */
1879 unsigned int i;
1880 for (i = 0; i < PARPORT_PC_MAX_PORTS && io[i]; i++);
1881 if (i) {
1882 if (parport_parse_irqs(i, irq, irqval)) return 1;
1883 if (parport_parse_dmas(i, dma, dmaval)) return 1;
1885 else {
1886 /* The user can make us use any IRQs or DMAs we find. */
1887 int val;
1889 if (irq[0] && !parport_parse_irqs (1, irq, &val))
1890 switch (val) {
1891 case PARPORT_IRQ_NONE:
1892 case PARPORT_IRQ_AUTO:
1893 irqval[0] = val;
1896 if (dma[0] && !parport_parse_dmas (1, dma, &val))
1897 switch (val) {
1898 case PARPORT_DMA_NONE:
1899 case PARPORT_DMA_AUTO:
1900 dmaval[0] = val;
1904 return (parport_pc_init(io, io_hi, irqval, dmaval)?0:1);
1907 void cleanup_module(void)
1909 struct parport *p = parport_enumerate(), *tmp;
1910 while (p) {
1911 tmp = p->next;
1912 if (p->modes & PARPORT_MODE_PCSPP) {
1913 struct parport_pc_private *priv = p->private_data;
1914 if (p->dma != PARPORT_DMA_NONE)
1915 free_dma(p->dma);
1916 if (p->irq != PARPORT_IRQ_NONE)
1917 free_irq(p->irq, p);
1918 release_region(p->base, p->size);
1919 if (p->modes & PARPORT_MODE_ECP)
1920 release_region(p->base_hi, 3);
1921 parport_proc_unregister(p);
1922 if (priv->dma_buf)
1923 free_page((unsigned long) priv->dma_buf);
1924 kfree (p->private_data);
1925 parport_unregister_port(p);
1927 p = tmp;
1930 #endif