* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / parport / parport_pc.c
bloba451d85b08d145ca3d8660fd9582f96fc19f9184
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 * Many ECP bugs fixed. Fred Barnes & Jamie Lokier, 1999
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.
38 * Note that the ECP registers may not start at offset 0x400 for PCI cards,
39 * but rather will start at port->base_hi.
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/sched.h>
46 #include <linux/delay.h>
47 #include <linux/errno.h>
48 #include <linux/interrupt.h>
49 #include <linux/ioport.h>
50 #include <linux/kernel.h>
51 #include <linux/malloc.h>
52 #include <linux/pci.h>
53 #include <linux/sysctl.h>
55 #include <asm/io.h>
56 #include <asm/dma.h>
57 #include <asm/uaccess.h>
59 #include <linux/parport.h>
60 #include <linux/parport_pc.h>
61 #include <asm/parport.h>
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 /* frob_control, but for ECR */
74 static void frob_econtrol (struct parport *pb, unsigned char m,
75 unsigned char v)
77 unsigned char ectr = inb (ECONTROL (pb));
78 #ifdef DEBUG_PARPORT
79 printk (KERN_DEBUG "frob_econtrol(%02x,%02x): %02x -> %02x\n",
80 m, v, ectr, (ectr & ~m) ^ v);
81 #endif
82 outb ((ectr & ~m) ^ v, ECONTROL (pb));
85 #ifdef CONFIG_PARPORT_PC_FIFO
86 /* Safely change the mode bits in the ECR */
87 static int change_mode(struct parport *p, int m)
89 const struct parport_pc_private *priv = p->physport->private_data;
90 int ecr = ECONTROL(p);
91 unsigned char oecr;
92 int mode;
94 if (!priv->ecr) {
95 printk (KERN_DEBUG "change_mode: but there's no ECR!\n");
96 return 0;
99 /* Bits <7:5> contain the mode. */
100 oecr = inb (ecr);
101 mode = (oecr >> 5) & 0x7;
102 if (mode == m) return 0;
104 if (mode >= 2 && !(priv->ctr & 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 if (mode >= 2 && m >= 2) {
134 /* We have to go through mode 001 */
135 oecr &= ~(7 << 5);
136 oecr |= ECR_PS2 << 5;
137 outb (oecr, ecr);
140 /* Set the mode. */
141 oecr &= ~(7 << 5);
142 oecr |= m << 5;
143 outb (oecr, ecr);
144 return 0;
147 #ifdef CONFIG_PARPORT_1284
148 /* Find FIFO lossage; FIFO is reset */
149 static int get_fifo_residue (struct parport *p)
151 int residue;
152 int cnfga;
153 const struct parport_pc_private *priv = p->physport->private_data;
155 /* Prevent further data transfer. */
156 parport_frob_control (p,
157 PARPORT_CONTROL_STROBE,
158 PARPORT_CONTROL_STROBE);
160 /* Adjust for the contents of the FIFO. */
161 for (residue = priv->fifo_depth; ; residue--) {
162 if (inb (ECONTROL (p)) & 0x2)
163 /* Full up. */
164 break;
166 outb (0, FIFO (p));
169 printk (KERN_DEBUG "%s: %d PWords were left in FIFO\n", p->name,
170 residue);
172 /* Reset the FIFO. */
173 frob_econtrol (p, 0xe0, ECR_PS2 << 5);
174 parport_frob_control (p, PARPORT_CONTROL_STROBE, 0);
176 /* Now change to config mode and clean up. FIXME */
177 frob_econtrol (p, 0xe0, ECR_CNF << 5);
178 cnfga = inb (CONFIGA (p));
179 printk (KERN_DEBUG "%s: cnfgA contains 0x%02x\n", p->name, cnfga);
181 if (!(cnfga & (1<<2))) {
182 printk (KERN_DEBUG "%s: Accounting for extra byte\n", p->name);
183 residue++;
186 /* Don't care about partial PWords until support is added for
187 * PWord != 1 byte. */
189 /* Back to PS2 mode. */
190 frob_econtrol (p, 0xe0, ECR_PS2 << 5);
192 return residue;
194 #endif /* IEEE 1284 support */
195 #endif /* FIFO support */
198 * Clear TIMEOUT BIT in EPP MODE
200 * This is also used in SPP detection.
202 static int clear_epp_timeout(struct parport *pb)
204 unsigned char r;
206 if (!(parport_pc_read_status(pb) & 0x01))
207 return 1;
209 /* To clear timeout some chips require double read */
210 parport_pc_read_status(pb);
211 r = parport_pc_read_status(pb);
212 outb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
213 outb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
214 r = parport_pc_read_status(pb);
216 return !(r & 0x01);
220 * Access functions.
222 * Most of these aren't static because they may be used by the
223 * parport_xxx_yyy macros. extern __inline__ versions of several
224 * of these are in parport_pc.h.
227 static void parport_pc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
229 parport_generic_irq(irq, (struct parport *) dev_id, regs);
232 void parport_pc_write_data(struct parport *p, unsigned char d)
234 outb (d, DATA (p));
237 unsigned char parport_pc_read_data(struct parport *p)
239 return inb (DATA (p));
242 void parport_pc_write_control(struct parport *p, unsigned char d)
244 const unsigned char wm = (PARPORT_CONTROL_STROBE |
245 PARPORT_CONTROL_AUTOFD |
246 PARPORT_CONTROL_INIT |
247 PARPORT_CONTROL_SELECT);
249 /* Take this out when drivers have adapted to the newer interface. */
250 if (d & 0x20) {
251 printk (KERN_DEBUG "%s (%s): use data_reverse for this!\n",
252 p->name, p->cad->name);
253 parport_pc_data_reverse (p);
256 __parport_pc_frob_control (p, wm, d & wm);
259 unsigned char parport_pc_read_control(struct parport *p)
261 const unsigned char wm = (PARPORT_CONTROL_STROBE |
262 PARPORT_CONTROL_AUTOFD |
263 PARPORT_CONTROL_INIT |
264 PARPORT_CONTROL_SELECT);
265 const struct parport_pc_private *priv = p->physport->private_data;
266 return priv->ctr & wm; /* Use soft copy */
269 unsigned char parport_pc_frob_control (struct parport *p, unsigned char mask,
270 unsigned char val)
272 const unsigned char wm = (PARPORT_CONTROL_STROBE |
273 PARPORT_CONTROL_AUTOFD |
274 PARPORT_CONTROL_INIT |
275 PARPORT_CONTROL_SELECT);
277 /* Take this out when drivers have adapted to the newer interface. */
278 if (mask & 0x20) {
279 printk (KERN_DEBUG "%s (%s): use data_%s for this!\n",
280 p->name, p->cad->name,
281 (val & 0x20) ? "reverse" : "forward");
282 if (val & 0x20)
283 parport_pc_data_reverse (p);
284 else
285 parport_pc_data_forward (p);
288 /* Restrict mask and val to control lines. */
289 mask &= wm;
290 val &= wm;
292 return __parport_pc_frob_control (p, mask, val);
295 unsigned char parport_pc_read_status(struct parport *p)
297 return inb (STATUS (p));
300 void parport_pc_disable_irq(struct parport *p)
302 __parport_pc_frob_control (p, 0x10, 0);
305 void parport_pc_enable_irq(struct parport *p)
307 __parport_pc_frob_control (p, 0x10, 0x10);
310 void parport_pc_data_forward (struct parport *p)
312 __parport_pc_frob_control (p, 0x20, 0);
315 void parport_pc_data_reverse (struct parport *p)
317 __parport_pc_frob_control (p, 0x20, 0x20);
320 void parport_pc_init_state(struct pardevice *dev, struct parport_state *s)
322 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
323 s->u.pc.ecr = 0x24;
326 void parport_pc_save_state(struct parport *p, struct parport_state *s)
328 const struct parport_pc_private *priv = p->physport->private_data;
329 s->u.pc.ctr = inb (CONTROL (p));
330 if (priv->ecr)
331 s->u.pc.ecr = inb (ECONTROL (p));
334 void parport_pc_restore_state(struct parport *p, struct parport_state *s)
336 const struct parport_pc_private *priv = p->physport->private_data;
337 outb (s->u.pc.ctr, CONTROL (p));
338 if (priv->ecr)
339 outb (s->u.pc.ecr, ECONTROL (p));
342 #ifdef CONFIG_PARPORT_1284
343 static size_t parport_pc_epp_read_data (struct parport *port, void *buf,
344 size_t length, int flags)
346 size_t got = 0;
347 for (; got < length; got++) {
348 *((char*)buf)++ = inb (EPPDATA(port));
349 if (inb (STATUS(port)) & 0x01) {
350 clear_epp_timeout (port);
351 break;
355 return got;
358 static size_t parport_pc_epp_write_data (struct parport *port, const void *buf,
359 size_t length, int flags)
361 size_t written = 0;
362 for (; written < length; written++) {
363 outb (*((char*)buf)++, EPPDATA(port));
364 if (inb (STATUS(port)) & 0x01) {
365 clear_epp_timeout (port);
366 break;
370 return written;
373 static size_t parport_pc_epp_read_addr (struct parport *port, void *buf,
374 size_t length, int flags)
376 size_t got = 0;
377 for (; got < length; got++) {
378 *((char*)buf)++ = inb (EPPADDR (port));
379 if (inb (STATUS (port)) & 0x01) {
380 clear_epp_timeout (port);
381 break;
385 return got;
388 static size_t parport_pc_epp_write_addr (struct parport *port,
389 const void *buf, size_t length,
390 int flags)
392 size_t written = 0;
393 for (; written < length; written++) {
394 outb (*((char*)buf)++, EPPADDR (port));
395 if (inb (STATUS (port)) & 0x01) {
396 clear_epp_timeout (port);
397 break;
401 return written;
404 static size_t parport_pc_ecpepp_read_data (struct parport *port, void *buf,
405 size_t length, int flags)
407 size_t got;
409 frob_econtrol (port, 0xe0, ECR_EPP << 5);
410 got = parport_pc_epp_read_data (port, buf, length, flags);
411 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
413 return got;
416 static size_t parport_pc_ecpepp_write_data (struct parport *port,
417 const void *buf, size_t length,
418 int flags)
420 size_t written;
422 frob_econtrol (port, 0xe0, ECR_EPP << 5);
423 written = parport_pc_epp_write_data (port, buf, length, flags);
424 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
426 return written;
429 static size_t parport_pc_ecpepp_read_addr (struct parport *port, void *buf,
430 size_t length, int flags)
432 size_t got;
434 frob_econtrol (port, 0xe0, ECR_EPP << 5);
435 got = parport_pc_epp_read_addr (port, buf, length, flags);
436 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
438 return got;
441 static size_t parport_pc_ecpepp_write_addr (struct parport *port,
442 const void *buf, size_t length,
443 int flags)
445 size_t written;
447 frob_econtrol (port, 0xe0, ECR_EPP << 5);
448 written = parport_pc_epp_write_addr (port, buf, length, flags);
449 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
451 return written;
453 #endif /* IEEE 1284 support */
455 #ifdef CONFIG_PARPORT_PC_FIFO
456 static size_t parport_pc_fifo_write_block_pio (struct parport *port,
457 const void *buf, size_t length)
459 int ret = 0;
460 const unsigned char *bufp = buf;
461 size_t left = length;
462 long expire = jiffies + port->physport->cad->timeout;
463 const int fifo = FIFO (port);
464 int poll_for = 8; /* 80 usecs */
465 const struct parport_pc_private *priv = port->physport->private_data;
466 const int fifo_depth = priv->fifo_depth;
468 port = port->physport;
470 /* We don't want to be interrupted every character. */
471 parport_pc_disable_irq (port);
472 frob_econtrol (port, (1<<4), (1<<4)); /* nErrIntrEn */
474 /* Forward mode. */
475 parport_pc_data_forward (port); /* Must be in PS2 mode */
477 while (left) {
478 unsigned char byte;
479 unsigned char ecrval = inb (ECONTROL (port));
480 int i = 0;
482 if (current->need_resched && time_before (jiffies, expire))
483 /* Can't yield the port. */
484 schedule ();
486 /* Anyone else waiting for the port? */
487 if (port->waithead) {
488 printk (KERN_DEBUG "Somebody wants the port\n");
489 break;
492 if (ecrval & 0x02) {
493 /* FIFO is full. Wait for interrupt. */
495 /* Clear serviceIntr */
496 outb (ecrval & ~(1<<2), ECONTROL (port));
497 false_alarm:
498 ret = parport_wait_event (port, HZ);
499 if (ret < 0) break;
500 ret = 0;
501 if (!time_before (jiffies, expire)) {
502 /* Timed out. */
503 printk (KERN_DEBUG "Timed out\n");
504 break;
506 ecrval = inb (ECONTROL (port));
507 if (!(ecrval & (1<<2))) {
508 if (current->need_resched &&
509 time_before (jiffies, expire))
510 schedule ();
512 goto false_alarm;
515 continue;
518 /* Can't fail now. */
519 expire = jiffies + port->cad->timeout;
521 poll:
522 if (signal_pending (current))
523 break;
525 if (ecrval & 0x01) {
526 /* FIFO is empty. Blast it full. */
527 const int n = left < fifo_depth ? left : fifo_depth;
528 outsb (fifo, bufp, n);
529 bufp += n;
530 left -= n;
532 /* Adjust the poll time. */
533 if (i < (poll_for - 2)) poll_for--;
534 continue;
535 } else if (i++ < poll_for) {
536 udelay (10);
537 ecrval = inb (ECONTROL (port));
538 goto poll;
541 /* Half-full (call me an optimist) */
542 byte = *bufp++;
543 outb (byte, fifo);
544 left--;
547 return length - left;
550 static size_t parport_pc_fifo_write_block_dma (struct parport *port,
551 const void *buf, size_t length)
553 int ret = 0;
554 unsigned long dmaflag;
555 size_t left = length;
556 const struct parport_pc_private *priv = port->physport->private_data;
558 port = port->physport;
560 /* We don't want to be interrupted every character. */
561 parport_pc_disable_irq (port);
562 frob_econtrol (port, (1<<4), (1<<4)); /* nErrIntrEn */
564 /* Forward mode. */
565 parport_pc_data_forward (port); /* Must be in PS2 mode */
567 while (left) {
568 long expire = jiffies + port->physport->cad->timeout;
570 size_t count = left;
572 if (count > PAGE_SIZE)
573 count = PAGE_SIZE;
575 memcpy(priv->dma_buf, buf, count);
577 dmaflag = claim_dma_lock();
578 disable_dma(port->dma);
579 clear_dma_ff(port->dma);
580 set_dma_mode(port->dma, DMA_MODE_WRITE);
581 set_dma_addr(port->dma, virt_to_bus((volatile char *) priv->dma_buf));
582 set_dma_count(port->dma, count);
584 /* Set DMA mode */
585 frob_econtrol (port, 1<<3, 1<<3);
587 /* Clear serviceIntr */
588 frob_econtrol (port, 1<<2, 0);
590 enable_dma(port->dma);
591 release_dma_lock(dmaflag);
593 /* assume DMA will be successful */
594 left -= count;
595 buf += count;
597 /* Wait for interrupt. */
598 false_alarm:
599 ret = parport_wait_event (port, HZ);
600 if (ret < 0) break;
601 ret = 0;
602 if (!time_before (jiffies, expire)) {
603 /* Timed out. */
604 printk (KERN_DEBUG "Timed out\n");
605 break;
607 /* Is serviceIntr set? */
608 if (!(inb (ECONTROL (port)) & (1<<2))) {
609 if (current->need_resched)
610 schedule ();
612 goto false_alarm;
615 dmaflag = claim_dma_lock();
616 disable_dma(port->dma);
617 clear_dma_ff(port->dma);
618 count = get_dma_residue(port->dma);
619 release_dma_lock(dmaflag);
621 if (current->need_resched)
622 /* Can't yield the port. */
623 schedule ();
625 /* Anyone else waiting for the port? */
626 if (port->waithead) {
627 printk (KERN_DEBUG "Somebody wants the port\n");
628 break;
631 /* update for possible DMA residue ! */
632 buf -= count;
633 left += count;
636 /* Maybe got here through break, so adjust for DMA residue! */
637 dmaflag = claim_dma_lock();
638 disable_dma(port->dma);
639 clear_dma_ff(port->dma);
640 left += get_dma_residue(port->dma);
641 release_dma_lock(dmaflag);
643 /* Turn off DMA mode */
644 frob_econtrol (port, 1<<3, 0);
646 return length - left;
649 /* Parallel Port FIFO mode (ECP chipsets) */
650 size_t parport_pc_compat_write_block_pio (struct parport *port,
651 const void *buf, size_t length,
652 int flags)
654 size_t written;
656 /* Special case: a timeout of zero means we cannot call schedule(). */
657 if (!port->physport->cad->timeout)
658 return parport_ieee1284_write_compat (port, buf,
659 length, flags);
661 /* Set up parallel port FIFO mode.*/
662 parport_pc_data_forward (port); /* Must be in PS2 mode */
663 change_mode (port, ECR_PPF); /* Parallel port FIFO */
664 port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
666 /* Write the data to the FIFO. */
667 if (port->dma != PARPORT_DMA_NONE)
668 written = parport_pc_fifo_write_block_dma (port, buf, length);
669 else
670 written = parport_pc_fifo_write_block_pio (port, buf, length);
672 /* Finish up. */
673 if (change_mode (port, ECR_PS2) == -EBUSY) {
674 const struct parport_pc_private *priv =
675 port->physport->private_data;
677 printk (KERN_DEBUG "%s: FIFO is stuck\n", port->name);
679 /* Prevent further data transfer. */
680 parport_frob_control (port,
681 PARPORT_CONTROL_STROBE,
682 PARPORT_CONTROL_STROBE);
684 /* Adjust for the contents of the FIFO. */
685 for (written -= priv->fifo_depth; ; written++) {
686 if (inb (ECONTROL (port)) & 0x2)
687 /* Full up. */
688 break;
690 outb (0, FIFO (port));
693 /* Reset the FIFO and return to PS2 mode. */
694 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
696 /* De-assert strobe. */
697 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
700 parport_wait_peripheral (port,
701 PARPORT_STATUS_BUSY,
702 PARPORT_STATUS_BUSY);
703 port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
705 return written;
708 /* ECP */
709 #ifdef CONFIG_PARPORT_1284
710 size_t parport_pc_ecp_write_block_pio (struct parport *port,
711 const void *buf, size_t length,
712 int flags)
714 size_t written;
716 /* Special case: a timeout of zero means we cannot call schedule(). */
717 if (!port->physport->cad->timeout)
718 return parport_ieee1284_ecp_write_data (port, buf,
719 length, flags);
721 /* Switch to forward mode if necessary. */
722 if (port->physport->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
723 /* Event 47: Set nInit high. */
724 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
726 /* Event 40: PError goes high. */
727 parport_wait_peripheral (port,
728 PARPORT_STATUS_PAPEROUT,
729 PARPORT_STATUS_PAPEROUT);
732 /* Set up ECP parallel port mode.*/
733 parport_pc_data_forward (port); /* Must be in PS2 mode */
734 change_mode (port, ECR_ECP); /* ECP FIFO */
735 port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
737 /* Write the data to the FIFO. */
738 if (port->dma != PARPORT_DMA_NONE)
739 written = parport_pc_fifo_write_block_dma (port, buf, length);
740 else
741 written = parport_pc_fifo_write_block_pio (port, buf, length);
743 /* Finish up. */
744 if (change_mode (port, ECR_PS2) == -EBUSY) {
745 const struct parport_pc_private *priv =
746 port->physport->private_data;
748 printk (KERN_DEBUG "%s: FIFO is stuck\n", port->name);
750 /* Prevent further data transfer. */
751 parport_frob_control (port,
752 PARPORT_CONTROL_STROBE,
753 PARPORT_CONTROL_STROBE);
755 /* Adjust for the contents of the FIFO. */
756 for (written -= priv->fifo_depth; ; written++) {
757 if (inb (ECONTROL (port)) & 0x2)
758 /* Full up. */
759 break;
761 outb (0, FIFO (port));
764 /* Reset the FIFO and return to PS2 mode. */
765 frob_econtrol (port, 0xe0, ECR_PS2 << 5);
767 /* De-assert strobe. */
768 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
770 /* Host transfer recovery. */
771 parport_pc_data_reverse (port); /* Must be in PS2 mode */
772 udelay (5);
773 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
774 parport_wait_peripheral (port, PARPORT_STATUS_PAPEROUT, 0);
775 parport_frob_control (port,
776 PARPORT_CONTROL_INIT,
777 PARPORT_CONTROL_INIT);
778 parport_wait_peripheral (port,
779 PARPORT_STATUS_PAPEROUT,
780 PARPORT_STATUS_PAPEROUT);
783 parport_wait_peripheral (port,
784 PARPORT_STATUS_BUSY,
785 PARPORT_STATUS_BUSY);
786 port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
788 return written;
791 size_t parport_pc_ecp_read_block_pio (struct parport *port,
792 void *buf, size_t length, int flags)
794 size_t left = length;
795 size_t fifofull;
796 const int fifo = FIFO(port);
797 const struct parport_pc_private *priv = port->physport->private_data;
798 const int fifo_depth = priv->fifo_depth;
799 char *bufp = buf;
801 port = port->physport;
803 /* Special case: a timeout of zero means we cannot call schedule(). */
804 if (!port->cad->timeout)
805 return parport_ieee1284_ecp_read_data (port, buf,
806 length, flags);
808 fifofull = fifo_depth;
809 if (port->ieee1284.mode == IEEE1284_MODE_ECPRLE)
810 /* If the peripheral is allowed to send RLE compressed
811 * data, it is possible for a byte to expand to 128
812 * bytes in the FIFO. */
813 fifofull = 128;
815 /* If the caller wants less than a full FIFO's worth of data,
816 * go through software emulation. Otherwise we may have to through
817 * away data. */
818 if (length < fifofull)
819 return parport_ieee1284_ecp_read_data (port, buf,
820 length, flags);
822 /* Switch to reverse mode if necessary. */
823 if (port->ieee1284.phase != IEEE1284_PH_REV_IDLE) {
824 /* Event 38: Set nAutoFd low */
825 parport_frob_control (port,
826 PARPORT_CONTROL_AUTOFD,
827 PARPORT_CONTROL_AUTOFD);
828 parport_pc_data_reverse (port); /* Must be in PS2 mode */
829 udelay (5);
831 /* Event 39: Set nInit low to initiate bus reversal */
832 parport_frob_control (port,
833 PARPORT_CONTROL_INIT,
836 /* Event 40: PError goes low */
837 parport_wait_peripheral (port, PARPORT_STATUS_PAPEROUT, 0);
840 /* Set up ECP parallel port mode.*/
841 parport_pc_data_reverse (port); /* Must be in PS2 mode */
842 change_mode (port, ECR_ECP); /* ECP FIFO */
843 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
845 /* Do the transfer. */
846 while (left > fifofull) {
847 int ret;
848 long int expire = jiffies + port->cad->timeout;
849 unsigned char ecrval = inb (ECONTROL (port));
851 if (current->need_resched && time_before (jiffies, expire))
852 /* Can't yield the port. */
853 schedule ();
855 /* At this point, the FIFO may already be full.
856 * Ideally, we'd be able to tell the port to hold on
857 * for a second while we empty the FIFO, and we'd be
858 * able to ensure that no data is lost. I'm not sure
859 * that's the case. :-( It might be that you can play
860 * games with STB, as in the forward case; someone should
861 * look at a datasheet. */
863 if (ecrval & 0x01) {
864 /* FIFO is empty. Wait for interrupt. */
866 /* Anyone else waiting for the port? */
867 if (port->waithead) {
868 printk (KERN_DEBUG
869 "Somebody wants the port\n");
870 break;
873 /* Clear serviceIntr */
874 outb (ecrval & ~(1<<2), ECONTROL (port));
875 false_alarm:
876 ret = parport_wait_event (port, HZ);
877 if (ret < 0) break;
878 ret = 0;
879 if (!time_before (jiffies, expire)) {
880 /* Timed out. */
881 printk (KERN_DEBUG "Timed out\n");
882 break;
884 ecrval = inb (ECONTROL (port));
885 if (!(ecrval & (1<<2))) {
886 if (current->need_resched &&
887 time_before (jiffies, expire))
888 schedule ();
890 goto false_alarm;
893 continue;
896 if (ecrval & 0x02) {
897 /* FIFO is full. */
898 insb (fifo, bufp, fifo_depth);
899 bufp += fifo_depth;
900 left -= fifo_depth;
901 continue;
904 *bufp++ = inb (fifo);
905 left--;
908 /* Finish up. */
909 if (change_mode (port, ECR_PS2) == -EBUSY) {
910 int lost = get_fifo_residue (port);
911 printk (KERN_DEBUG "%s: DATA LOSS (%d bytes)!\n", port->name,
912 lost);
915 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
917 return length - left;
920 #endif /* IEEE 1284 support */
922 #endif /* Allowed to use FIFO/DMA */
924 void parport_pc_inc_use_count(void)
926 #ifdef MODULE
927 MOD_INC_USE_COUNT;
928 #endif
931 void parport_pc_dec_use_count(void)
933 #ifdef MODULE
934 MOD_DEC_USE_COUNT;
935 #endif
938 struct parport_operations parport_pc_ops =
940 parport_pc_write_data,
941 parport_pc_read_data,
943 parport_pc_write_control,
944 parport_pc_read_control,
945 parport_pc_frob_control,
947 parport_pc_read_status,
949 parport_pc_enable_irq,
950 parport_pc_disable_irq,
952 parport_pc_data_forward,
953 parport_pc_data_reverse,
955 parport_pc_init_state,
956 parport_pc_save_state,
957 parport_pc_restore_state,
959 parport_pc_inc_use_count,
960 parport_pc_dec_use_count,
962 parport_ieee1284_epp_write_data,
963 parport_ieee1284_epp_read_data,
964 parport_ieee1284_epp_write_addr,
965 parport_ieee1284_epp_read_addr,
967 parport_ieee1284_ecp_write_data,
968 parport_ieee1284_ecp_read_data,
969 parport_ieee1284_ecp_write_addr,
971 parport_ieee1284_write_compat,
972 parport_ieee1284_read_nibble,
973 parport_ieee1284_read_byte,
976 /* --- Mode detection ------------------------------------- */
979 * Checks for port existence, all ports support SPP MODE
981 static int __maybe_init parport_SPP_supported(struct parport *pb)
983 unsigned char r, w;
986 * first clear an eventually pending EPP timeout
987 * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
988 * that does not even respond to SPP cycles if an EPP
989 * timeout is pending
991 clear_epp_timeout(pb);
993 /* Do a simple read-write test to make sure the port exists. */
994 w = 0xc;
995 outb (w, CONTROL (pb));
997 /* Is there a control register that we can read from? Some
998 * ports don't allow reads, so read_control just returns a
999 * software copy. Some ports _do_ allow reads, so bypass the
1000 * software copy here. In addition, some bits aren't
1001 * writable. */
1002 r = inb (CONTROL (pb));
1003 if ((r & 0xf) == w) {
1004 w = 0xe;
1005 outb (w, CONTROL (pb));
1006 r = inb (CONTROL (pb));
1007 outb (0xc, CONTROL (pb));
1008 if ((r & 0xf) == w)
1009 return PARPORT_MODE_PCSPP;
1012 if (user_specified)
1013 /* That didn't work, but the user thinks there's a
1014 * port here. */
1015 printk (KERN_DEBUG "0x%lx: CTR: wrote 0x%02x, read 0x%02x\n",
1016 pb->base, w, r);
1018 /* Try the data register. The data lines aren't tri-stated at
1019 * this stage, so we expect back what we wrote. */
1020 w = 0xaa;
1021 parport_pc_write_data (pb, w);
1022 r = parport_pc_read_data (pb);
1023 if (r == w) {
1024 w = 0x55;
1025 parport_pc_write_data (pb, w);
1026 r = parport_pc_read_data (pb);
1027 if (r == w)
1028 return PARPORT_MODE_PCSPP;
1031 if (user_specified)
1032 /* Didn't work, but the user is convinced this is the
1033 * place. */
1034 printk (KERN_DEBUG "0x%lx: DATA: wrote 0x%02x, read 0x%02x\n",
1035 pb->base, w, r);
1037 /* It's possible that we can't read the control register or
1038 * the data register. In that case just believe the user. */
1039 if (user_specified)
1040 return PARPORT_MODE_PCSPP;
1042 return 0;
1045 /* Check for ECR
1047 * Old style XT ports alias io ports every 0x400, hence accessing ECR
1048 * on these cards actually accesses the CTR.
1050 * Modern cards don't do this but reading from ECR will return 0xff
1051 * regardless of what is written here if the card does NOT support
1052 * ECP.
1054 * We first check to see if ECR is the same as CTR. If not, the low
1055 * two bits of ECR aren't writable, so we check by writing ECR and
1056 * reading it back to see if it's what we expect.
1058 static int __maybe_init parport_ECR_present(struct parport *pb)
1060 struct parport_pc_private *priv = pb->private_data;
1061 unsigned char r = 0xc;
1063 outb (r, CONTROL (pb));
1064 if ((inb (ECONTROL (pb)) & 0x3) == (r & 0x3)) {
1065 outb (r ^ 0x2, CONTROL (pb)); /* Toggle bit 1 */
1067 r = inb (CONTROL (pb));
1068 if ((inb (ECONTROL (pb)) & 0x2) == (r & 0x2))
1069 goto no_reg; /* Sure that no ECR register exists */
1072 if ((inb (ECONTROL (pb)) & 0x3 ) != 0x1)
1073 goto no_reg;
1075 outb (0x34, ECONTROL (pb));
1076 if (inb (ECONTROL (pb)) != 0x35)
1077 goto no_reg;
1079 priv->ecr = 1;
1080 outb (0xc, CONTROL (pb));
1082 /* Go to mode 000 */
1083 frob_econtrol (pb, 0xe0, ECR_SPP << 5);
1085 return 1;
1087 no_reg:
1088 outb (0xc, CONTROL (pb));
1089 return 0;
1092 #ifdef CONFIG_PARPORT_1284
1093 /* Detect PS/2 support.
1095 * Bit 5 (0x20) sets the PS/2 data direction; setting this high
1096 * allows us to read data from the data lines. In theory we would get back
1097 * 0xff but any peripheral attached to the port may drag some or all of the
1098 * lines down to zero. So if we get back anything that isn't the contents
1099 * of the data register we deem PS/2 support to be present.
1101 * Some SPP ports have "half PS/2" ability - you can't turn off the line
1102 * drivers, but an external peripheral with sufficiently beefy drivers of
1103 * its own can overpower them and assert its own levels onto the bus, from
1104 * where they can then be read back as normal. Ports with this property
1105 * and the right type of device attached are likely to fail the SPP test,
1106 * (as they will appear to have stuck bits) and so the fact that they might
1107 * be misdetected here is rather academic.
1110 static int __maybe_init parport_PS2_supported(struct parport *pb)
1112 int ok = 0;
1114 clear_epp_timeout(pb);
1116 /* try to tri-state the buffer */
1117 parport_pc_data_reverse (pb);
1119 parport_pc_write_data(pb, 0x55);
1120 if (parport_pc_read_data(pb) != 0x55) ok++;
1122 parport_pc_write_data(pb, 0xaa);
1123 if (parport_pc_read_data(pb) != 0xaa) ok++;
1125 /* cancel input mode */
1126 parport_pc_data_forward (pb);
1128 if (ok) {
1129 pb->modes |= PARPORT_MODE_TRISTATE;
1130 } else {
1131 struct parport_pc_private *priv = pb->private_data;
1132 priv->ctr_writable &= ~0x20;
1135 return ok;
1138 static int __maybe_init parport_ECP_supported(struct parport *pb)
1140 int i;
1141 int config;
1142 int pword;
1143 struct parport_pc_private *priv = pb->private_data;
1145 /* If there is no ECR, we have no hope of supporting ECP. */
1146 if (!priv->ecr)
1147 return 0;
1149 /* Find out FIFO depth */
1150 outb (ECR_SPP << 5, ECONTROL (pb)); /* Reset FIFO */
1151 outb (ECR_TST << 5, ECONTROL (pb)); /* TEST FIFO */
1152 for (i=0; i < 1024 && !(inb (ECONTROL (pb)) & 0x02); i++)
1153 outb (0xaa, FIFO (pb));
1156 * Using LGS chipset it uses ECR register, but
1157 * it doesn't support ECP or FIFO MODE
1159 if (i == 1024) {
1160 outb (ECR_SPP << 5, ECONTROL (pb));
1161 return 0;
1164 priv->fifo_depth = i;
1165 printk (KERN_INFO "0x%lx: FIFO is %d bytes\n", pb->base, i);
1167 /* Find out writeIntrThreshold */
1168 frob_econtrol (pb, 1<<2, 1<<2);
1169 frob_econtrol (pb, 1<<2, 0);
1170 for (i = 1; i <= priv->fifo_depth; i++) {
1171 inb (FIFO (pb));
1172 udelay (50);
1173 if (inb (ECONTROL (pb)) & (1<<2))
1174 break;
1177 if (i <= priv->fifo_depth)
1178 printk (KERN_INFO "0x%lx: writeIntrThreshold is %d\n",
1179 pb->base, i);
1180 else
1181 /* Number of bytes we know we can write if we get an
1182 interrupt. */
1183 i = 0;
1185 priv->writeIntrThreshold = i;
1187 /* Find out readIntrThreshold */
1188 frob_econtrol (pb, 0xe0, ECR_PS2 << 5); /* Reset FIFO and enable PS2 */
1189 parport_pc_data_reverse (pb); /* Must be in PS2 mode */
1190 frob_econtrol (pb, 0xe0, ECR_TST << 5); /* Test FIFO */
1191 frob_econtrol (pb, 1<<2, 1<<2);
1192 frob_econtrol (pb, 1<<2, 0);
1193 for (i = 1; i <= priv->fifo_depth; i++) {
1194 outb (0xaa, FIFO (pb));
1195 if (inb (ECONTROL (pb)) & (1<<2))
1196 break;
1199 if (i <= priv->fifo_depth)
1200 printk (KERN_INFO "0x%lx: readIntrThreshold is %d\n",
1201 pb->base, i);
1202 else
1203 /* Number of bytes we can read if we get an interrupt. */
1204 i = 0;
1206 priv->readIntrThreshold = i;
1208 outb (ECR_SPP << 5, ECONTROL (pb)); /* Reset FIFO */
1209 outb (0xf4, ECONTROL (pb)); /* Configuration mode */
1210 config = inb (FIFO (pb));
1211 pword = (config >> 4) & 0x7;
1212 switch (pword) {
1213 case 0:
1214 pword = 2;
1215 printk (KERN_WARNING "0x%lx: Unsupported pword size!\n",
1216 pb->base);
1217 break;
1218 case 2:
1219 pword = 4;
1220 printk (KERN_WARNING "0x%lx: Unsupported pword size!\n",
1221 pb->base);
1222 break;
1223 default:
1224 printk (KERN_WARNING "0x%lx: Unknown implementation ID\n",
1225 pb->base);
1226 /* Assume 1 */
1227 case 1:
1228 pword = 1;
1230 priv->pword = pword;
1231 printk (KERN_DEBUG "0x%lx: PWord is %d bits\n", pb->base, 8 * pword);
1233 config = inb (CONFIGB (pb));
1234 printk (KERN_DEBUG "0x%lx: Interrupts are ISA-%s\n", pb->base,
1235 config & 0x80 ? "Level" : "Pulses");
1237 if (!(config & 0x40)) {
1238 printk (KERN_WARNING "0x%lx: IRQ conflict!\n", pb->base);
1239 pb->irq = PARPORT_IRQ_NONE;
1242 /* Go back to mode 000 */
1243 frob_econtrol (pb, 0xe0, ECR_SPP << 5);
1244 pb->modes |= PARPORT_MODE_ECP;
1246 return 1;
1249 static int __maybe_init parport_ECPPS2_supported(struct parport *pb)
1251 const struct parport_pc_private *priv = pb->private_data;
1252 int result;
1253 unsigned char oecr;
1255 if (!priv->ecr)
1256 return 0;
1258 oecr = inb (ECONTROL (pb));
1259 outb (ECR_PS2 << 5, ECONTROL (pb));
1261 result = parport_PS2_supported(pb);
1263 outb (oecr, ECONTROL (pb));
1264 return result;
1267 /* EPP mode detection */
1269 static int __maybe_init parport_EPP_supported(struct parport *pb)
1271 const struct parport_pc_private *priv = pb->private_data;
1274 * Theory:
1275 * Bit 0 of STR is the EPP timeout bit, this bit is 0
1276 * when EPP is possible and is set high when an EPP timeout
1277 * occurs (EPP uses the HALT line to stop the CPU while it does
1278 * the byte transfer, an EPP timeout occurs if the attached
1279 * device fails to respond after 10 micro seconds).
1281 * This bit is cleared by either reading it (National Semi)
1282 * or writing a 1 to the bit (SMC, UMC, WinBond), others ???
1283 * This bit is always high in non EPP modes.
1286 /* If EPP timeout bit clear then EPP available */
1287 if (!clear_epp_timeout(pb))
1288 return 0; /* No way to clear timeout */
1290 /* Check for Intel bug. */
1291 if (priv->ecr) {
1292 unsigned char i;
1293 for (i = 0x00; i < 0x80; i += 0x20) {
1294 outb (i, ECONTROL (pb));
1295 if (clear_epp_timeout (pb))
1296 /* Phony EPP in ECP. */
1297 return 0;
1301 pb->modes |= PARPORT_MODE_EPP;
1303 /* Set up access functions to use EPP hardware. */
1304 pb->ops->epp_read_data = parport_pc_epp_read_data;
1305 pb->ops->epp_write_data = parport_pc_epp_write_data;
1306 pb->ops->epp_read_addr = parport_pc_epp_read_addr;
1307 pb->ops->epp_write_addr = parport_pc_epp_write_addr;
1309 return 1;
1312 static int __maybe_init parport_ECPEPP_supported(struct parport *pb)
1314 struct parport_pc_private *priv = pb->private_data;
1315 int result;
1316 unsigned char oecr;
1318 if (!priv->ecr)
1319 return 0;
1321 oecr = inb (ECONTROL (pb));
1322 /* Search for SMC style EPP+ECP mode */
1323 outb (0x80, ECONTROL (pb));
1325 result = parport_EPP_supported(pb);
1327 outb (oecr, ECONTROL (pb));
1329 if (result) {
1330 /* Set up access functions to use ECP+EPP hardware. */
1331 pb->ops->epp_read_data = parport_pc_ecpepp_read_data;
1332 pb->ops->epp_write_data = parport_pc_ecpepp_write_data;
1333 pb->ops->epp_read_addr = parport_pc_ecpepp_read_addr;
1334 pb->ops->epp_write_addr = parport_pc_ecpepp_write_addr;
1337 return result;
1340 #else /* No IEEE 1284 support */
1342 /* Don't bother probing for modes we know we won't use. */
1343 static int __maybe_init parport_PS2_supported(struct parport *pb) { return 0; }
1344 static int __maybe_init parport_ECP_supported(struct parport *pb) { return 0; }
1345 static int __maybe_init parport_EPP_supported(struct parport *pb) { return 0; }
1346 static int __maybe_init parport_ECPEPP_supported(struct parport *pb){return 0;}
1347 static int __maybe_init parport_ECPPS2_supported(struct parport *pb){return 0;}
1349 #endif /* No IEEE 1284 support */
1351 /* --- IRQ detection -------------------------------------- */
1353 /* Only if supports ECP mode */
1354 static int __maybe_init programmable_irq_support(struct parport *pb)
1356 int irq, intrLine;
1357 unsigned char oecr = inb (ECONTROL (pb));
1358 static const int lookup[8] = {
1359 PARPORT_IRQ_NONE, 7, 9, 10, 11, 14, 15, 5
1362 outb (ECR_CNF << 5, ECONTROL (pb)); /* Configuration MODE */
1364 intrLine = (inb (CONFIGB (pb)) >> 3) & 0x07;
1365 irq = lookup[intrLine];
1367 outb (oecr, ECONTROL (pb));
1368 return irq;
1371 static int __maybe_init irq_probe_ECP(struct parport *pb)
1373 int i;
1374 unsigned long irqs;
1376 sti();
1377 irqs = probe_irq_on();
1379 outb (ECR_SPP << 5, ECONTROL (pb)); /* Reset FIFO */
1380 outb ((ECR_TST << 5) | 0x04, ECONTROL (pb));
1381 outb (ECR_TST << 5, ECONTROL (pb));
1383 /* If Full FIFO sure that writeIntrThreshold is generated */
1384 for (i=0; i < 1024 && !(inb (ECONTROL (pb)) & 0x02) ; i++)
1385 outb (0xaa, FIFO (pb));
1387 pb->irq = probe_irq_off(irqs);
1388 outb (ECR_SPP << 5, ECONTROL (pb));
1390 if (pb->irq <= 0)
1391 pb->irq = PARPORT_IRQ_NONE;
1393 return pb->irq;
1397 * This detection seems that only works in National Semiconductors
1398 * This doesn't work in SMC, LGS, and Winbond
1400 static int __maybe_init irq_probe_EPP(struct parport *pb)
1402 #ifndef ADVANCED_DETECT
1403 return PARPORT_IRQ_NONE;
1404 #else
1405 int irqs;
1406 unsigned char oecr;
1408 if (pb->modes & PARPORT_MODE_PCECR)
1409 oecr = inb (ECONTROL (pb));
1411 sti();
1412 irqs = probe_irq_on();
1414 if (pb->modes & PARPORT_MODE_PCECR)
1415 frob_econtrol (pb, 0x10, 0x10);
1417 clear_epp_timeout(pb);
1418 parport_pc_frob_control (pb, 0x20, 0x20);
1419 parport_pc_frob_control (pb, 0x10, 0x10);
1420 clear_epp_timeout(pb);
1422 /* Device isn't expecting an EPP read
1423 * and generates an IRQ.
1425 parport_pc_read_epp(pb);
1426 udelay(20);
1428 pb->irq = probe_irq_off (irqs);
1429 if (pb->modes & PARPORT_MODE_PCECR)
1430 outb (oecr, ECONTROL (pb));
1431 parport_pc_write_control(pb, 0xc);
1433 if (pb->irq <= 0)
1434 pb->irq = PARPORT_IRQ_NONE;
1436 return pb->irq;
1437 #endif /* Advanced detection */
1440 static int __maybe_init irq_probe_SPP(struct parport *pb)
1442 /* Don't even try to do this. */
1443 return PARPORT_IRQ_NONE;
1446 /* We will attempt to share interrupt requests since other devices
1447 * such as sound cards and network cards seem to like using the
1448 * printer IRQs.
1450 * When ECP is available we can autoprobe for IRQs.
1451 * NOTE: If we can autoprobe it, we can register the IRQ.
1453 static int __maybe_init parport_irq_probe(struct parport *pb)
1455 const struct parport_pc_private *priv = pb->private_data;
1457 if (priv->ecr) {
1458 pb->irq = programmable_irq_support(pb);
1459 if (pb->irq != PARPORT_IRQ_NONE)
1460 goto out;
1463 if (pb->modes & PARPORT_MODE_ECP)
1464 pb->irq = irq_probe_ECP(pb);
1466 if (pb->irq == PARPORT_IRQ_NONE && priv->ecr &&
1467 (pb->modes & PARPORT_MODE_EPP))
1468 pb->irq = irq_probe_EPP(pb);
1470 clear_epp_timeout(pb);
1472 if (pb->irq == PARPORT_IRQ_NONE && (pb->modes & PARPORT_MODE_EPP))
1473 pb->irq = irq_probe_EPP(pb);
1475 clear_epp_timeout(pb);
1477 if (pb->irq == PARPORT_IRQ_NONE)
1478 pb->irq = irq_probe_SPP(pb);
1480 out:
1481 return pb->irq;
1484 /* --- DMA detection -------------------------------------- */
1486 /* Only if supports ECP mode */
1487 static int __maybe_init programmable_dma_support (struct parport *p)
1489 unsigned char oecr = inb (ECONTROL (p));
1490 int dma;
1492 frob_econtrol (p, 0xe0, ECR_CNF << 5);
1494 dma = inb (CONFIGB(p)) & 0x03;
1495 if (!dma)
1496 dma = PARPORT_DMA_NONE;
1498 outb (oecr, ECONTROL (p));
1499 return dma;
1502 static int __maybe_init parport_dma_probe (struct parport *p)
1504 const struct parport_pc_private *priv = p->private_data;
1505 if (priv->ecr)
1506 p->dma = programmable_dma_support(p);
1508 return p->dma;
1511 /* --- Initialisation code -------------------------------- */
1513 struct parport *__maybe_init parport_pc_probe_port (unsigned long int base,
1514 unsigned long int base_hi,
1515 int irq, int dma)
1517 struct parport_pc_private *priv;
1518 struct parport_operations *ops;
1519 struct parport tmp;
1520 struct parport *p = &tmp;
1521 int probedirq = PARPORT_IRQ_NONE;
1522 if (check_region(base, 3)) return NULL;
1523 priv = kmalloc (sizeof (struct parport_pc_private), GFP_KERNEL);
1524 if (!priv) {
1525 printk (KERN_DEBUG "parport (0x%lx): no memory!\n", base);
1526 return NULL;
1528 ops = kmalloc (sizeof (struct parport_operations), GFP_KERNEL);
1529 if (!ops) {
1530 printk (KERN_DEBUG "parport (0x%lx): no memory for ops!\n",
1531 base);
1532 kfree (priv);
1533 return NULL;
1535 memcpy (ops, &parport_pc_ops, sizeof (struct parport_operations));
1536 priv->ctr = 0xc;
1537 priv->ctr_writable = 0xff;
1538 priv->ecr = 0;
1539 priv->fifo_depth = 0;
1540 priv->dma_buf = 0;
1541 p->base = base;
1542 p->base_hi = base_hi;
1543 p->irq = irq;
1544 p->dma = dma;
1545 p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
1546 p->ops = ops;
1547 p->private_data = priv;
1548 p->physport = p;
1549 if (base_hi && !check_region(base_hi,3)) {
1550 parport_ECR_present(p);
1551 parport_ECP_supported(p);
1553 if (base != 0x3bc) {
1554 if (!check_region(base+0x3, 5)) {
1555 if (!parport_EPP_supported(p))
1556 parport_ECPEPP_supported(p);
1559 if (!parport_SPP_supported (p)) {
1560 /* No port. */
1561 kfree (priv);
1562 return NULL;
1564 if (priv->ecr)
1565 parport_ECPPS2_supported(p);
1566 else
1567 parport_PS2_supported (p);
1569 if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
1570 PARPORT_DMA_NONE, ops))) {
1571 kfree (priv);
1572 kfree (ops);
1573 return NULL;
1576 p->base_hi = base_hi;
1577 p->modes = tmp.modes;
1578 p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
1579 p->private_data = priv;
1581 printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
1582 if (p->base_hi && (p->modes & PARPORT_MODE_ECP))
1583 printk(" (0x%lx)", p->base_hi);
1584 p->irq = irq;
1585 p->dma = dma;
1586 if (p->irq == PARPORT_IRQ_AUTO) {
1587 p->irq = PARPORT_IRQ_NONE;
1588 parport_irq_probe(p);
1589 } else if (p->irq == PARPORT_IRQ_PROBEONLY) {
1590 p->irq = PARPORT_IRQ_NONE;
1591 parport_irq_probe(p);
1592 probedirq = p->irq;
1593 p->irq = PARPORT_IRQ_NONE;
1595 if (p->irq != PARPORT_IRQ_NONE) {
1596 printk(", irq %d", p->irq);
1598 if (p->dma == PARPORT_DMA_AUTO) {
1599 p->dma = PARPORT_DMA_NONE;
1600 parport_dma_probe(p);
1603 if (p->dma == PARPORT_DMA_AUTO)
1604 p->dma = PARPORT_DMA_NONE;
1606 #ifdef CONFIG_PARPORT_PC_FIFO
1607 if (p->dma != PARPORT_DMA_NOFIFO &&
1608 priv->fifo_depth > 0 && p->irq != PARPORT_IRQ_NONE) {
1609 p->ops->compat_write_data = parport_pc_compat_write_block_pio;
1610 #ifdef CONFIG_PARPORT_1284
1611 p->ops->ecp_write_data = parport_pc_ecp_write_block_pio;
1612 #endif /* IEEE 1284 support */
1613 if (p->dma != PARPORT_DMA_NONE) {
1614 printk(", dma %d", p->dma);
1615 p->modes |= PARPORT_MODE_DMA;
1617 else printk(", using FIFO");
1619 else
1620 /* We can't use the DMA channel after all. */
1621 p->dma = PARPORT_DMA_NONE;
1622 #endif /* Allowed to use FIFO/DMA */
1624 printk(" [");
1625 #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
1627 int f = 0;
1628 printmode(PCSPP);
1629 printmode(TRISTATE);
1630 printmode(COMPAT)
1631 printmode(EPP);
1632 printmode(ECP);
1633 printmode(DMA);
1635 #undef printmode
1636 printk("]\n");
1637 if (probedirq != PARPORT_IRQ_NONE)
1638 printk("%s: irq %d detected\n", p->name, probedirq);
1639 parport_proc_register(p);
1641 request_region (p->base, 3, p->name);
1642 if (p->size > 3)
1643 request_region (p->base + 3, p->size - 3, p->name);
1644 if (p->modes & PARPORT_MODE_ECP)
1645 request_region (p->base_hi, 3, p->name);
1647 if (p->irq != PARPORT_IRQ_NONE) {
1648 if (request_irq (p->irq, parport_pc_interrupt,
1649 0, p->name, p)) {
1650 printk (KERN_WARNING "%s: irq %d in use, "
1651 "resorting to polled operation\n",
1652 p->name, p->irq);
1653 p->irq = PARPORT_IRQ_NONE;
1654 p->dma = PARPORT_DMA_NONE;
1657 if (p->dma != PARPORT_DMA_NONE) {
1658 if (request_dma (p->dma, p->name)) {
1659 printk (KERN_WARNING "%s: dma %d in use, "
1660 "resorting to PIO operation\n",
1661 p->name, p->dma);
1662 p->dma = PARPORT_DMA_NONE;
1663 } else {
1664 priv->dma_buf =
1665 (char *)__get_dma_pages(GFP_KERNEL, 0);
1666 if (! priv->dma_buf) {
1667 printk (KERN_WARNING "%s: "
1668 "cannot get buffer for DMA, "
1669 "resorting to PIO operation\n",
1670 p->name);
1671 free_dma(p->dma);
1672 p->dma = PARPORT_DMA_NONE;
1678 /* Done probing. Now put the port into a sensible start-up state.
1679 * SELECT | INIT also puts IEEE1284-compliant devices into
1680 * compatibility mode. */
1681 if (priv->ecr)
1683 * Put the ECP detected port in PS2 mode.
1684 * Do this also for ports that have ECR but don't do ECP.
1686 outb (0x34, ECONTROL (p));
1688 parport_pc_write_data(p, 0);
1689 parport_pc_data_forward (p);
1690 parport_pc_write_control(p, PARPORT_CONTROL_SELECT);
1691 udelay (50);
1692 parport_pc_write_control(p,
1693 PARPORT_CONTROL_SELECT
1694 | PARPORT_CONTROL_INIT);
1695 udelay (50);
1697 /* Now that we've told the sharing engine about the port, and
1698 found out its characteristics, let the high-level drivers
1699 know about it. */
1700 parport_announce_port (p);
1702 return p;
1705 /* Look for PCI parallel port cards. */
1706 static int __init parport_pc_init_pci (int irq, int dma)
1708 struct {
1709 unsigned int vendor;
1710 unsigned int device;
1711 unsigned int numports;
1712 struct {
1713 unsigned long lo;
1714 unsigned long hi; /* -ve if not there */
1715 } addr[4];
1716 } cards[] = {
1717 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_10x_550, 1,
1718 { { 3, 4 }, } },
1719 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_10x_650, 1,
1720 { { 3, 4 }, } },
1721 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_10x_850, 1,
1722 { { 3, 4 }, } },
1723 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1P_10x, 1,
1724 { { 2, 3 }, } },
1725 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P_10x, 2,
1726 { { 2, 3 }, { 4, 5 }, } },
1727 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_10x_550, 1,
1728 { { 4, 5 }, } },
1729 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_10x_650, 1,
1730 { { 4, 5 }, } },
1731 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_10x_850, 1,
1732 { { 4, 5 }, } },
1733 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1P_20x, 1,
1734 { { 0, 1 }, } },
1735 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P_20x, 2,
1736 { { 0, 1 }, { 2, 3 }, } },
1737 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P1S_20x_550, 2,
1738 { { 1, 2 }, { 3, 4 }, } },
1739 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P1S_20x_650, 2,
1740 { { 1, 2 }, { 3, 4 }, } },
1741 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2P1S_20x_850, 2,
1742 { { 1, 2 }, { 3, 4 }, } },
1743 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_20x_550, 1,
1744 { { 1, 2 }, } },
1745 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_20x_650, 1,
1746 { { 1, 2 }, } },
1747 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S1P_20x_850, 1,
1748 { { 1, 2 }, } },
1749 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_20x_550, 1,
1750 { { 2, 3 }, } },
1751 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_20x_650, 1,
1752 { { 2, 3 }, } },
1753 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S1P_20x_850, 1,
1754 { { 2, 3 }, } },
1755 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PARALLEL, 1,
1756 { { 0, -1 }, } },
1757 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DUAL_PAR_A, 1,
1758 { { 0, -1 }, } },
1759 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DUAL_PAR_B, 1,
1760 { { 0, -1 }, } },
1761 { PCI_VENDOR_ID_EXSYS, PCI_DEVICE_ID_EXSYS_4014, 2,
1762 { { 2, -1 }, { 3, -1 }, } },
1763 { 0, }
1766 struct pci_dev *pcidev;
1767 int count = 0;
1768 int i;
1770 if (!pci_present ())
1771 return 0;
1773 for (i = 0; cards[i].vendor; i++) {
1774 pcidev = NULL;
1775 while ((pcidev = pci_find_device (cards[i].vendor,
1776 cards[i].device,
1777 pcidev)) != NULL) {
1778 int n;
1779 for (n = 0; n < cards[i].numports; n++) {
1780 unsigned long lo = cards[i].addr[n].lo;
1781 unsigned long hi = cards[i].addr[n].hi;
1782 unsigned long io_lo, io_hi;
1783 io_lo = pcidev->resource[lo].start;
1784 io_hi = ((hi < 0) ? 0 :
1785 pcidev->resource[hi].start);
1786 if (irq == PARPORT_IRQ_AUTO) {
1787 if (parport_pc_probe_port (io_lo,
1788 io_hi,
1789 pcidev->irq,
1790 dma))
1791 count++;
1792 } else if (parport_pc_probe_port (io_lo, io_hi,
1793 irq, dma))
1794 count++;
1799 /* Look for parallel controllers that we don't know about. */
1800 for (pcidev = pci_devices; pcidev; pcidev = pcidev->next) {
1801 const int class_noprogif = pcidev->class & ~0xff;
1802 if (class_noprogif != (PCI_CLASS_COMMUNICATION_PARALLEL << 8))
1803 continue;
1805 for (i = 0; cards[i].vendor; i++)
1806 if ((cards[i].vendor == pcidev->vendor) &&
1807 (cards[i].device == pcidev->device))
1808 break;
1809 if (cards[i].vendor)
1810 /* We know about this one. */
1811 continue;
1813 printk (KERN_INFO
1814 "Unknown PCI parallel I/O card (%04x/%04x)\n"
1815 "Please send 'lspci' output to "
1816 "tim@cyberelk.demon.co.uk\n",
1817 pcidev->vendor, pcidev->device);
1820 return count;
1823 /* Exported symbols. */
1824 #ifdef CONFIG_PARPORT_PC_PCMCIA
1826 /* parport_cs needs this in order to dyncamically get us to find ports. */
1827 EXPORT_SYMBOL (parport_pc_probe_port);
1829 #else
1831 EXPORT_NO_SYMBOLS;
1833 #endif
1835 #ifdef MODULE
1836 static int io[PARPORT_PC_MAX_PORTS+1] = { [0 ... PARPORT_PC_MAX_PORTS] = 0 };
1837 static int io_hi[PARPORT_PC_MAX_PORTS+1] = { [0 ... PARPORT_PC_MAX_PORTS] = 0 };
1838 static int dmaval[PARPORT_PC_MAX_PORTS] = { [0 ... PARPORT_PC_MAX_PORTS-1] = PARPORT_DMA_AUTO };
1839 static int irqval[PARPORT_PC_MAX_PORTS] = { [0 ... PARPORT_PC_MAX_PORTS-1] = PARPORT_IRQ_PROBEONLY };
1840 static const char *irq[PARPORT_PC_MAX_PORTS] = { NULL, };
1841 static const char *dma[PARPORT_PC_MAX_PORTS] = { NULL, };
1842 MODULE_PARM(io, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "i");
1843 MODULE_PARM(io_hi, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "i");
1844 MODULE_PARM(irq, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "s");
1845 MODULE_PARM(dma, "1-" __MODULE_STRING(PARPORT_PC_MAX_PORTS) "s");
1847 int init_module(void)
1849 /* Work out how many ports we have, then get parport_share to parse
1850 the irq values. */
1851 unsigned int i;
1852 for (i = 0; i < PARPORT_PC_MAX_PORTS && io[i]; i++);
1853 if (i) {
1854 if (parport_parse_irqs(i, irq, irqval)) return 1;
1855 if (parport_parse_dmas(i, dma, dmaval)) return 1;
1857 else {
1858 /* The user can make us use any IRQs or DMAs we find. */
1859 int val;
1861 if (irq[0] && !parport_parse_irqs (1, irq, &val))
1862 switch (val) {
1863 case PARPORT_IRQ_NONE:
1864 case PARPORT_IRQ_AUTO:
1865 irqval[0] = val;
1868 if (dma[0] && !parport_parse_dmas (1, dma, &val))
1869 switch (val) {
1870 case PARPORT_DMA_NONE:
1871 case PARPORT_DMA_AUTO:
1872 dmaval[0] = val;
1876 return (parport_pc_init(io, io_hi, irqval, dmaval)?0:1);
1879 void cleanup_module(void)
1881 struct parport *p = parport_enumerate(), *tmp;
1882 while (p) {
1883 tmp = p->next;
1884 if (p->modes & PARPORT_MODE_PCSPP) {
1885 struct parport_pc_private *priv = p->private_data;
1886 struct parport_operations *ops = p->ops;
1887 if (p->dma != PARPORT_DMA_NONE)
1888 free_dma(p->dma);
1889 if (p->irq != PARPORT_IRQ_NONE)
1890 free_irq(p->irq, p);
1891 release_region(p->base, 3);
1892 if (p->size > 3)
1893 release_region(p->base + 3, p->size - 3);
1894 if (p->modes & PARPORT_MODE_ECP)
1895 release_region(p->base_hi, 3);
1896 parport_proc_unregister(p);
1897 if (priv->dma_buf)
1898 free_page((unsigned long) priv->dma_buf);
1899 kfree (p->private_data);
1900 parport_unregister_port(p);
1901 kfree (ops); /* hope no-one cached it */
1903 p = tmp;
1906 #endif