- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / ppa.c
blobe85391bd1ed4aa521413d03d82bbad66b7b17680
1 /* ppa.c -- low level driver for the IOMEGA PPA3
2 * parallel port SCSI host adapter.
3 *
4 * (The PPA3 is the embedded controller in the ZIP drive.)
5 *
6 * (c) 1995,1996 Grant R. Guenther, grant@torque.net,
7 * under the terms of the GNU Public License.
8 *
9 * Current Maintainer: David Campbell (Perth, Western Australia, GMT+0800)
10 * campbell@torque.net
13 #include <linux/config.h>
15 /* The following #define is to avoid a clash with hosts.c */
16 #define PPA_CODE 1
18 #include <linux/blk.h>
19 #include <asm/io.h>
20 #include <linux/parport.h>
21 #include "sd.h"
22 #include "hosts.h"
23 int ppa_release(struct Scsi_Host *);
24 static void ppa_reset_pulse(unsigned int base);
26 typedef struct {
27 struct pardevice *dev; /* Parport device entry */
28 int base; /* Actual port address */
29 int mode; /* Transfer mode */
30 int host; /* Host number (for proc) */
31 Scsi_Cmnd *cur_cmd; /* Current queued command */
32 struct tq_struct ppa_tq; /* Polling interupt stuff */
33 unsigned long jstart; /* Jiffies at start */
34 unsigned int failed:1; /* Failure flag */
35 unsigned int p_busy:1; /* Parport sharing busy flag */
36 } ppa_struct;
38 #define PPA_EMPTY \
39 { dev: NULL, \
40 base: -1, \
41 mode: PPA_AUTODETECT, \
42 host: -1, \
43 cur_cmd: NULL, \
44 ppa_tq: { routine: ppa_interrupt }, \
45 jstart: 0, \
46 failed: 0, \
47 p_busy: 0 \
50 #include "ppa.h"
52 #define NO_HOSTS 4
53 static ppa_struct ppa_hosts[NO_HOSTS] =
54 {PPA_EMPTY, PPA_EMPTY, PPA_EMPTY, PPA_EMPTY};
56 #define PPA_BASE(x) ppa_hosts[(x)].base
58 void ppa_wakeup(void *ref)
60 ppa_struct *ppa_dev = (ppa_struct *) ref;
62 if (!ppa_dev->p_busy)
63 return;
65 if (parport_claim(ppa_dev->dev)) {
66 printk("ppa: bug in ppa_wakeup\n");
67 return;
69 ppa_dev->p_busy = 0;
70 ppa_dev->base = ppa_dev->dev->port->base;
71 if (ppa_dev->cur_cmd)
72 ppa_dev->cur_cmd->SCp.phase++;
73 return;
76 int ppa_release(struct Scsi_Host *host)
78 int host_no = host->unique_id;
80 printk("Releasing ppa%i\n", host_no);
81 parport_unregister_device(ppa_hosts[host_no].dev);
82 return 0;
85 static int ppa_pb_claim(int host_no)
87 if (parport_claim(ppa_hosts[host_no].dev)) {
88 ppa_hosts[host_no].p_busy = 1;
89 return 1;
91 if (ppa_hosts[host_no].cur_cmd)
92 ppa_hosts[host_no].cur_cmd->SCp.phase++;
93 return 0;
96 #define ppa_pb_release(x) parport_release(ppa_hosts[(x)].dev)
98 /***************************************************************************
99 * Parallel port probing routines *
100 ***************************************************************************/
102 static Scsi_Host_Template driver_template = PPA;
103 #include "scsi_module.c"
106 * Start of Chipset kludges
109 int ppa_detect(Scsi_Host_Template * host)
111 struct Scsi_Host *hreg;
112 int ports;
113 int i, nhosts, try_again;
114 struct parport *pb;
117 * unlock to allow the lowlevel parport driver to probe
118 * the irqs
120 spin_unlock_irq(&io_request_lock);
121 pb = parport_enumerate();
123 printk("ppa: Version %s\n", PPA_VERSION);
124 nhosts = 0;
125 try_again = 0;
127 if (!pb) {
128 printk("ppa: parport reports no devices.\n");
129 spin_lock_irq(&io_request_lock);
130 return 0;
132 retry_entry:
133 for (i = 0; pb; i++, pb = pb->next) {
134 int modes, ppb, ppb_hi;
136 ppa_hosts[i].dev =
137 parport_register_device(pb, "ppa", NULL, ppa_wakeup,
138 NULL, 0, (void *) &ppa_hosts[i]);
140 if (!ppa_hosts[i].dev)
141 continue;
143 /* Claim the bus so it remembers what we do to the control
144 * registers. [ CTR and ECP ]
146 if (ppa_pb_claim(i)) {
147 unsigned long now = jiffies;
148 while (ppa_hosts[i].p_busy) {
149 schedule(); /* We are safe to schedule here */
150 if (time_after(jiffies, now + 3 * HZ)) {
151 printk(KERN_ERR "ppa%d: failed to claim parport because a "
152 "pardevice is owning the port for too longtime!\n",
154 spin_lock_irq(&io_request_lock);
155 return 0;
159 ppb = PPA_BASE(i) = ppa_hosts[i].dev->port->base;
160 ppb_hi = ppa_hosts[i].dev->port->base_hi;
161 w_ctr(ppb, 0x0c);
162 modes = ppa_hosts[i].dev->port->modes;
164 /* Mode detection works up the chain of speed
165 * This avoids a nasty if-then-else-if-... tree
167 ppa_hosts[i].mode = PPA_NIBBLE;
169 if (modes & PARPORT_MODE_TRISTATE)
170 ppa_hosts[i].mode = PPA_PS2;
172 if (modes & PARPORT_MODE_ECP) {
173 w_ecr(ppb_hi, 0x20);
174 ppa_hosts[i].mode = PPA_PS2;
176 if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
177 w_ecr(ppb_hi, 0x80);
179 /* Done configuration */
180 ppa_pb_release(i);
182 if (ppa_init(i)) {
183 parport_unregister_device(ppa_hosts[i].dev);
184 continue;
186 /* now the glue ... */
187 switch (ppa_hosts[i].mode) {
188 case PPA_NIBBLE:
189 ports = 3;
190 break;
191 case PPA_PS2:
192 ports = 3;
193 break;
194 case PPA_EPP_8:
195 case PPA_EPP_16:
196 case PPA_EPP_32:
197 ports = 8;
198 break;
199 default: /* Never gets here */
200 continue;
203 host->can_queue = PPA_CAN_QUEUE;
204 host->sg_tablesize = ppa_sg;
205 hreg = scsi_register(host, 0);
206 if(hreg == NULL)
207 continue;
208 hreg->io_port = pb->base;
209 hreg->n_io_port = ports;
210 hreg->dma_channel = -1;
211 hreg->unique_id = i;
212 ppa_hosts[i].host = hreg->host_no;
213 nhosts++;
215 if (nhosts == 0) {
216 if (try_again == 1) {
217 printk("WARNING - no ppa compatible devices found.\n");
218 printk(" As of 31/Aug/1998 Iomega started shipping parallel\n");
219 printk(" port ZIP drives with a different interface which is\n");
220 printk(" supported by the imm (ZIP Plus) driver. If the\n");
221 printk(" cable is marked with \"AutoDetect\", this is what has\n");
222 printk(" happened.\n");
223 return 0;
224 spin_lock_irq(&io_request_lock);
226 try_again = 1;
227 goto retry_entry;
228 } else {
229 spin_lock_irq(&io_request_lock);
230 return 1; /* return number of hosts detected */
234 /* This is to give the ppa driver a way to modify the timings (and other
235 * parameters) by writing to the /proc/scsi/ppa/0 file.
236 * Very simple method really... (To simple, no error checking :( )
237 * Reason: Kernel hackers HATE having to unload and reload modules for
238 * testing...
239 * Also gives a method to use a script to obtain optimum timings (TODO)
242 static inline int ppa_proc_write(int hostno, char *buffer, int length)
244 unsigned long x;
246 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
247 x = simple_strtoul(buffer + 5, NULL, 0);
248 ppa_hosts[hostno].mode = x;
249 return length;
251 printk("ppa /proc: invalid variable\n");
252 return (-EINVAL);
255 int ppa_proc_info(char *buffer, char **start, off_t offset,
256 int length, int hostno, int inout)
258 int i;
259 int len = 0;
261 for (i = 0; i < 4; i++)
262 if (ppa_hosts[i].host == hostno)
263 break;
265 if (inout)
266 return ppa_proc_write(i, buffer, length);
268 len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
269 len += sprintf(buffer + len, "Parport : %s\n", ppa_hosts[i].dev->port->name);
270 len += sprintf(buffer + len, "Mode : %s\n", PPA_MODE_STRING[ppa_hosts[i].mode]);
272 /* Request for beyond end of buffer */
273 if (offset > length)
274 return 0;
276 *start = buffer + offset;
277 len -= offset;
278 if (len > length)
279 len = length;
280 return len;
283 static int device_check(int host_no);
285 #if PPA_DEBUG > 0
286 #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
287 y, __FUNCTION__, __LINE__); ppa_fail_func(x,y);
288 static inline void ppa_fail_func(int host_no, int error_code)
289 #else
290 static inline void ppa_fail(int host_no, int error_code)
291 #endif
293 /* If we fail a device then we trash status / message bytes */
294 if (ppa_hosts[host_no].cur_cmd) {
295 ppa_hosts[host_no].cur_cmd->result = error_code << 16;
296 ppa_hosts[host_no].failed = 1;
301 * Wait for the high bit to be set.
303 * In principle, this could be tied to an interrupt, but the adapter
304 * doesn't appear to be designed to support interrupts. We spin on
305 * the 0x80 ready bit.
307 static unsigned char ppa_wait(int host_no)
309 int k;
310 unsigned short ppb = PPA_BASE(host_no);
311 unsigned char r;
313 k = PPA_SPIN_TMO;
314 /* Wait for bit 6 and 7 - PJC */
315 for (r = r_str (ppb); ((r & 0xc0)!=0xc0) && (k); k--) {
316 udelay (1);
317 r = r_str (ppb);
321 * return some status information.
322 * Semantics: 0xc0 = ZIP wants more data
323 * 0xd0 = ZIP wants to send more data
324 * 0xe0 = ZIP is expecting SCSI command data
325 * 0xf0 = end of transfer, ZIP is sending status
327 if (k)
328 return (r & 0xf0);
330 /* Counter expired - Time out occurred */
331 ppa_fail(host_no, DID_TIME_OUT);
332 printk("ppa timeout in ppa_wait\n");
333 return 0; /* command timed out */
337 * Clear EPP Timeout Bit
339 static inline void epp_reset(unsigned short ppb)
341 int i;
343 i = r_str(ppb);
344 w_str(ppb, i);
345 w_str(ppb, i & 0xfe);
349 * Wait for empty ECP fifo (if we are in ECP fifo mode only)
351 static inline void ecp_sync(unsigned short hostno)
353 int i, ppb_hi=ppa_hosts[hostno].dev->port->base_hi;
355 if (ppb_hi == 0) return;
357 if ((r_ecr(ppb_hi) & 0xe0) == 0x60) { /* mode 011 == ECP fifo mode */
358 for (i = 0; i < 100; i++) {
359 if (r_ecr(ppb_hi) & 0x01)
360 return;
361 udelay(5);
363 printk("ppa: ECP sync failed as data still present in FIFO.\n");
367 static int ppa_byte_out(unsigned short base, const char *buffer, int len)
369 int i;
371 for (i = len; i; i--) {
372 w_dtr(base, *buffer++);
373 w_ctr(base, 0xe);
374 w_ctr(base, 0xc);
376 return 1; /* All went well - we hope! */
379 static int ppa_byte_in(unsigned short base, char *buffer, int len)
381 int i;
383 for (i = len; i; i--) {
384 *buffer++ = r_dtr(base);
385 w_ctr(base, 0x27);
386 w_ctr(base, 0x25);
388 return 1; /* All went well - we hope! */
391 static int ppa_nibble_in(unsigned short base, char *buffer, int len)
393 for (; len; len--) {
394 unsigned char h;
396 w_ctr(base, 0x4);
397 h = r_str(base) & 0xf0;
398 w_ctr(base, 0x6);
399 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
401 return 1; /* All went well - we hope! */
404 static int ppa_out(int host_no, char *buffer, int len)
406 int r;
407 unsigned short ppb = PPA_BASE(host_no);
409 r = ppa_wait(host_no);
411 if ((r & 0x50) != 0x40) {
412 ppa_fail(host_no, DID_ERROR);
413 return 0;
415 switch (ppa_hosts[host_no].mode) {
416 case PPA_NIBBLE:
417 case PPA_PS2:
418 /* 8 bit output, with a loop */
419 r = ppa_byte_out(ppb, buffer, len);
420 break;
422 case PPA_EPP_32:
423 case PPA_EPP_16:
424 case PPA_EPP_8:
425 epp_reset(ppb);
426 w_ctr(ppb, 0x4);
427 #ifdef CONFIG_SCSI_IZIP_EPP16
428 if (!(((long) buffer | len) & 0x01))
429 outsw(ppb + 4, buffer, len >> 1);
430 #else
431 if (!(((long) buffer | len) & 0x03))
432 outsl(ppb + 4, buffer, len >> 2);
433 #endif
434 else
435 outsb(ppb + 4, buffer, len);
436 w_ctr(ppb, 0xc);
437 r = !(r_str(ppb) & 0x01);
438 w_ctr(ppb, 0xc);
439 ecp_sync(host_no);
440 break;
442 default:
443 printk("PPA: bug in ppa_out()\n");
444 r = 0;
446 return r;
449 static int ppa_in(int host_no, char *buffer, int len)
451 int r;
452 unsigned short ppb = PPA_BASE(host_no);
454 r = ppa_wait(host_no);
456 if ((r & 0x50) != 0x50) {
457 ppa_fail(host_no, DID_ERROR);
458 return 0;
460 switch (ppa_hosts[host_no].mode) {
461 case PPA_NIBBLE:
462 /* 4 bit input, with a loop */
463 r = ppa_nibble_in(ppb, buffer, len);
464 w_ctr(ppb, 0xc);
465 break;
467 case PPA_PS2:
468 /* 8 bit input, with a loop */
469 w_ctr(ppb, 0x25);
470 r = ppa_byte_in(ppb, buffer, len);
471 w_ctr(ppb, 0x4);
472 w_ctr(ppb, 0xc);
473 break;
475 case PPA_EPP_32:
476 case PPA_EPP_16:
477 case PPA_EPP_8:
478 epp_reset(ppb);
479 w_ctr(ppb, 0x24);
480 #ifdef CONFIG_SCSI_IZIP_EPP16
481 if (!(((long) buffer | len) & 0x01))
482 insw(ppb + 4, buffer, len >> 1);
483 #else
484 if (!(((long) buffer | len) & 0x03))
485 insl(ppb + 4, buffer, len >> 2);
486 #endif
487 else
488 insb(ppb + 4, buffer, len);
489 w_ctr(ppb, 0x2c);
490 r = !(r_str(ppb) & 0x01);
491 w_ctr(ppb, 0x2c);
492 ecp_sync(host_no);
493 break;
495 default:
496 printk("PPA: bug in ppa_ins()\n");
497 r = 0;
498 break;
500 return r;
503 /* end of ppa_io.h */
504 static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
506 w_dtr(ppb, b);
507 w_ctr(ppb, 0xc);
508 w_ctr(ppb, 0xe);
509 w_ctr(ppb, 0xc);
510 w_ctr(ppb, 0x4);
511 w_ctr(ppb, 0xc);
514 static void ppa_disconnect(int host_no)
516 unsigned short ppb = PPA_BASE(host_no);
518 ppa_d_pulse(ppb, 0);
519 ppa_d_pulse(ppb, 0x3c);
520 ppa_d_pulse(ppb, 0x20);
521 ppa_d_pulse(ppb, 0xf);
524 static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
526 w_dtr(ppb, b);
527 w_ctr(ppb, 0x4);
528 w_ctr(ppb, 0x6);
529 w_ctr(ppb, 0x4);
530 w_ctr(ppb, 0xc);
533 static inline void ppa_connect(int host_no, int flag)
535 unsigned short ppb = PPA_BASE(host_no);
537 ppa_c_pulse(ppb, 0);
538 ppa_c_pulse(ppb, 0x3c);
539 ppa_c_pulse(ppb, 0x20);
540 if ((flag == CONNECT_EPP_MAYBE) &&
541 IN_EPP_MODE(ppa_hosts[host_no].mode))
542 ppa_c_pulse(ppb, 0xcf);
543 else
544 ppa_c_pulse(ppb, 0x8f);
547 static int ppa_select(int host_no, int target)
549 int k;
550 unsigned short ppb = PPA_BASE(host_no);
553 * Bit 6 (0x40) is the device selected bit.
554 * First we must wait till the current device goes off line...
556 k = PPA_SELECT_TMO;
557 do {
558 k--;
559 } while ((r_str(ppb) & 0x40) && (k));
560 if (!k)
561 return 0;
563 w_dtr(ppb, (1 << target));
564 w_ctr(ppb, 0xe);
565 w_ctr(ppb, 0xc);
566 w_dtr(ppb, 0x80); /* This is NOT the initator */
567 w_ctr(ppb, 0x8);
569 k = PPA_SELECT_TMO;
570 do {
571 k--;
573 while (!(r_str(ppb) & 0x40) && (k));
574 if (!k)
575 return 0;
577 return 1;
581 * This is based on a trace of what the Iomega DOS 'guest' driver does.
582 * I've tried several different kinds of parallel ports with guest and
583 * coded this to react in the same ways that it does.
585 * The return value from this function is just a hint about where the
586 * handshaking failed.
589 static int ppa_init(int host_no)
591 int retv;
592 unsigned short ppb = PPA_BASE(host_no);
594 #if defined(CONFIG_PARPORT) || defined(CONFIG_PARPORT_MODULE)
595 if (ppa_pb_claim(host_no))
596 while (ppa_hosts[host_no].p_busy)
597 schedule(); /* We can safe schedule here */
598 #endif
600 ppa_disconnect(host_no);
601 ppa_connect(host_no, CONNECT_NORMAL);
603 retv = 2; /* Failed */
605 w_ctr(ppb, 0xe);
606 if ((r_str(ppb) & 0x08) == 0x08)
607 retv--;
609 w_ctr(ppb, 0xc);
610 if ((r_str(ppb) & 0x08) == 0x00)
611 retv--;
613 if (!retv)
614 ppa_reset_pulse(ppb);
615 udelay(1000); /* Allow devices to settle down */
616 ppa_disconnect(host_no);
617 udelay(1000); /* Another delay to allow devices to settle */
619 if (!retv)
620 retv = device_check(host_no);
622 ppa_pb_release(host_no);
623 return retv;
626 static inline int ppa_send_command(Scsi_Cmnd * cmd)
628 int host_no = cmd->host->unique_id;
629 int k;
631 w_ctr(PPA_BASE(host_no), 0x0c);
633 for (k = 0; k < cmd->cmd_len; k++)
634 if (!ppa_out(host_no, &cmd->cmnd[k], 1))
635 return 0;
636 return 1;
640 * The bulk flag enables some optimisations in the data transfer loops,
641 * it should be true for any command that transfers data in integral
642 * numbers of sectors.
644 * The driver appears to remain stable if we speed up the parallel port
645 * i/o in this function, but not elsewhere.
647 static int ppa_completion(Scsi_Cmnd * cmd)
649 /* Return codes:
650 * -1 Error
651 * 0 Told to schedule
652 * 1 Finished data transfer
654 int host_no = cmd->host->unique_id;
655 unsigned long start_jiffies = jiffies;
657 unsigned char r, v;
658 int fast, bulk, status;
660 v = cmd->cmnd[0];
661 bulk = ((v == READ_6) ||
662 (v == READ_10) ||
663 (v == WRITE_6) ||
664 (v == WRITE_10));
666 r = ppa_wait(host_no); /* Need a ppa_wait() - PJC */
668 while (r != (unsigned char) 0xf0) {
670 * If we have been running for more than a full timer tick
671 * then take a rest.
673 if (time_after(jiffies, start_jiffies + 1))
674 return 0;
676 if (((r & 0xc0) != 0xc0) || (cmd->SCp.this_residual <= 0)) {
677 ppa_fail(host_no, DID_ERROR);
678 return -1; /* ERROR_RETURN */
680 /* determine if we should use burst I/O */ fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
681 ? PPA_BURST_SIZE : 1;
683 if (r == (unsigned char) 0xc0)
684 status = ppa_out(host_no, cmd->SCp.ptr, fast);
685 else
686 status = ppa_in(host_no, cmd->SCp.ptr, fast);
688 cmd->SCp.ptr += fast;
689 cmd->SCp.this_residual -= fast;
691 if (!status) {
692 ppa_fail(host_no, DID_BUS_BUSY);
693 return -1; /* ERROR_RETURN */
695 if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
696 /* if scatter/gather, advance to the next segment */
697 if (cmd->SCp.buffers_residual--) {
698 cmd->SCp.buffer++;
699 cmd->SCp.this_residual = cmd->SCp.buffer->length;
700 cmd->SCp.ptr = cmd->SCp.buffer->address;
703 /* Now check to see if the drive is ready to comunicate */
704 r = ppa_wait(host_no); /* need ppa_wait() - PJC */
705 /* If not, drop back down to the scheduler and wait a timer tick */
706 if (!(r & 0x80))
707 return 0;
709 return 1; /* FINISH_RETURN */
712 /* deprecated synchronous interface */
713 int ppa_command(Scsi_Cmnd * cmd)
715 static int first_pass = 1;
716 int host_no = cmd->host->unique_id;
718 if (first_pass) {
719 printk("ppa: using non-queuing interface\n");
720 first_pass = 0;
722 if (ppa_hosts[host_no].cur_cmd) {
723 printk("PPA: bug in ppa_command\n");
724 return 0;
726 ppa_hosts[host_no].failed = 0;
727 ppa_hosts[host_no].jstart = jiffies;
728 ppa_hosts[host_no].cur_cmd = cmd;
729 cmd->result = DID_ERROR << 16; /* default return code */
730 cmd->SCp.phase = 0;
732 ppa_pb_claim(host_no);
734 while (ppa_engine(&ppa_hosts[host_no], cmd))
735 schedule();
737 if (cmd->SCp.phase) /* Only disconnect if we have connected */
738 ppa_disconnect(cmd->host->unique_id);
740 ppa_pb_release(host_no);
741 ppa_hosts[host_no].cur_cmd = 0;
742 return cmd->result;
746 * Since the PPA itself doesn't generate interrupts, we use
747 * the scheduler's task queue to generate a stream of call-backs and
748 * complete the request when the drive is ready.
750 static void ppa_interrupt(void *data)
752 ppa_struct *tmp = (ppa_struct *) data;
753 Scsi_Cmnd *cmd = tmp->cur_cmd;
754 unsigned long flags;
756 if (!cmd) {
757 printk("PPA: bug in ppa_interrupt\n");
758 return;
760 if (ppa_engine(tmp, cmd)) {
761 tmp->ppa_tq.data = (void *) tmp;
762 tmp->ppa_tq.sync = 0;
763 queue_task(&tmp->ppa_tq, &tq_timer);
764 return;
766 /* Command must of completed hence it is safe to let go... */
767 #if PPA_DEBUG > 0
768 switch ((cmd->result >> 16) & 0xff) {
769 case DID_OK:
770 break;
771 case DID_NO_CONNECT:
772 printk("ppa: no device at SCSI ID %i\n", cmd->target);
773 break;
774 case DID_BUS_BUSY:
775 printk("ppa: BUS BUSY - EPP timeout detected\n");
776 break;
777 case DID_TIME_OUT:
778 printk("ppa: unknown timeout\n");
779 break;
780 case DID_ABORT:
781 printk("ppa: told to abort\n");
782 break;
783 case DID_PARITY:
784 printk("ppa: parity error (???)\n");
785 break;
786 case DID_ERROR:
787 printk("ppa: internal driver error\n");
788 break;
789 case DID_RESET:
790 printk("ppa: told to reset device\n");
791 break;
792 case DID_BAD_INTR:
793 printk("ppa: bad interrupt (???)\n");
794 break;
795 default:
796 printk("ppa: bad return code (%02x)\n", (cmd->result >> 16) & 0xff);
798 #endif
800 if (cmd->SCp.phase > 1)
801 ppa_disconnect(cmd->host->unique_id);
802 if (cmd->SCp.phase > 0)
803 ppa_pb_release(cmd->host->unique_id);
805 tmp->cur_cmd = 0;
807 spin_lock_irqsave(&io_request_lock, flags);
808 cmd->scsi_done(cmd);
809 spin_unlock_irqrestore(&io_request_lock, flags);
810 return;
813 static int ppa_engine(ppa_struct * tmp, Scsi_Cmnd * cmd)
815 int host_no = cmd->host->unique_id;
816 unsigned short ppb = PPA_BASE(host_no);
817 unsigned char l = 0, h = 0;
818 int retv;
820 /* First check for any errors that may of occurred
821 * Here we check for internal errors
823 if (tmp->failed)
824 return 0;
826 switch (cmd->SCp.phase) {
827 case 0: /* Phase 0 - Waiting for parport */
828 if ((jiffies - tmp->jstart) > HZ) {
830 * We waited more than a second
831 * for parport to call us
833 ppa_fail(host_no, DID_BUS_BUSY);
834 return 0;
836 return 1; /* wait until ppa_wakeup claims parport */
837 case 1: /* Phase 1 - Connected */
838 { /* Perform a sanity check for cable unplugged */
839 int retv = 2; /* Failed */
841 ppa_connect(host_no, CONNECT_EPP_MAYBE);
843 w_ctr(ppb, 0xe);
844 if ((r_str(ppb) & 0x08) == 0x08)
845 retv--;
847 w_ctr(ppb, 0xc);
848 if ((r_str(ppb) & 0x08) == 0x00)
849 retv--;
851 if (retv) {
852 if ((jiffies - tmp->jstart) > (1 * HZ)) {
853 printk("ppa: Parallel port cable is unplugged!!\n");
854 ppa_fail(host_no, DID_BUS_BUSY);
855 return 0;
856 } else {
857 ppa_disconnect(host_no);
858 return 1; /* Try again in a jiffy */
861 cmd->SCp.phase++;
864 case 2: /* Phase 2 - We are now talking to the scsi bus */
865 if (!ppa_select(host_no, cmd->target)) {
866 ppa_fail(host_no, DID_NO_CONNECT);
867 return 0;
869 cmd->SCp.phase++;
871 case 3: /* Phase 3 - Ready to accept a command */
872 w_ctr(ppb, 0x0c);
873 if (!(r_str(ppb) & 0x80))
874 return 1;
876 if (!ppa_send_command(cmd))
877 return 0;
878 cmd->SCp.phase++;
880 case 4: /* Phase 4 - Setup scatter/gather buffers */
881 if (cmd->use_sg) {
882 /* if many buffers are available, start filling the first */
883 cmd->SCp.buffer = (struct scatterlist *) cmd->request_buffer;
884 cmd->SCp.this_residual = cmd->SCp.buffer->length;
885 cmd->SCp.ptr = cmd->SCp.buffer->address;
886 } else {
887 /* else fill the only available buffer */
888 cmd->SCp.buffer = NULL;
889 cmd->SCp.this_residual = cmd->request_bufflen;
890 cmd->SCp.ptr = cmd->request_buffer;
892 cmd->SCp.buffers_residual = cmd->use_sg;
893 cmd->SCp.phase++;
895 case 5: /* Phase 5 - Data transfer stage */
896 w_ctr(ppb, 0x0c);
897 if (!(r_str(ppb) & 0x80))
898 return 1;
900 retv = ppa_completion(cmd);
901 if (retv == -1)
902 return 0;
903 if (retv == 0)
904 return 1;
905 cmd->SCp.phase++;
907 case 6: /* Phase 6 - Read status/message */
908 cmd->result = DID_OK << 16;
909 /* Check for data overrun */
910 if (ppa_wait(host_no) != (unsigned char) 0xf0) {
911 ppa_fail(host_no, DID_ERROR);
912 return 0;
914 if (ppa_in(host_no, &l, 1)) { /* read status byte */
915 /* Check for optional message byte */
916 if (ppa_wait(host_no) == (unsigned char) 0xf0)
917 ppa_in(host_no, &h, 1);
918 cmd->result = (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
920 return 0; /* Finished */
921 break;
923 default:
924 printk("ppa: Invalid scsi phase\n");
926 return 0;
929 int ppa_queuecommand(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
931 int host_no = cmd->host->unique_id;
933 if (ppa_hosts[host_no].cur_cmd) {
934 printk("PPA: bug in ppa_queuecommand\n");
935 return 0;
937 ppa_hosts[host_no].failed = 0;
938 ppa_hosts[host_no].jstart = jiffies;
939 ppa_hosts[host_no].cur_cmd = cmd;
940 cmd->scsi_done = done;
941 cmd->result = DID_ERROR << 16; /* default return code */
942 cmd->SCp.phase = 0; /* bus free */
944 ppa_pb_claim(host_no);
946 ppa_hosts[host_no].ppa_tq.data = ppa_hosts + host_no;
947 ppa_hosts[host_no].ppa_tq.sync = 0;
948 queue_task(&ppa_hosts[host_no].ppa_tq, &tq_immediate);
949 mark_bh(IMMEDIATE_BH);
951 return 0;
955 * Apparently the the disk->capacity attribute is off by 1 sector
956 * for all disk drives. We add the one here, but it should really
957 * be done in sd.c. Even if it gets fixed there, this will still
958 * work.
960 int ppa_biosparam(Disk * disk, kdev_t dev, int ip[])
962 ip[0] = 0x40;
963 ip[1] = 0x20;
964 ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
965 if (ip[2] > 1024) {
966 ip[0] = 0xff;
967 ip[1] = 0x3f;
968 ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
969 if (ip[2] > 1023)
970 ip[2] = 1023;
972 return 0;
975 int ppa_abort(Scsi_Cmnd * cmd)
977 int host_no = cmd->host->unique_id;
979 * There is no method for aborting commands since Iomega
980 * have tied the SCSI_MESSAGE line high in the interface
983 switch (cmd->SCp.phase) {
984 case 0: /* Do not have access to parport */
985 case 1: /* Have not connected to interface */
986 ppa_hosts[host_no].cur_cmd = NULL; /* Forget the problem */
987 return SUCCESS;
988 break;
989 default: /* SCSI command sent, can not abort */
990 return FAILED;
991 break;
995 static void ppa_reset_pulse(unsigned int base)
997 w_dtr(base, 0x40);
998 w_ctr(base, 0x8);
999 udelay(30);
1000 w_ctr(base, 0xc);
1003 int ppa_reset(Scsi_Cmnd * cmd)
1005 int host_no = cmd->host->unique_id;
1007 if (cmd->SCp.phase)
1008 ppa_disconnect(host_no);
1009 ppa_hosts[host_no].cur_cmd = NULL; /* Forget the problem */
1011 ppa_connect(host_no, CONNECT_NORMAL);
1012 ppa_reset_pulse(PPA_BASE(host_no));
1013 udelay(1000); /* device settle delay */
1014 ppa_disconnect(host_no);
1015 udelay(1000); /* device settle delay */
1016 return SUCCESS;
1019 static int device_check(int host_no)
1021 /* This routine looks for a device and then attempts to use EPP
1022 to send a command. If all goes as planned then EPP is available. */
1024 static char cmd[6] =
1025 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1026 int loop, old_mode, status, k, ppb = PPA_BASE(host_no);
1027 unsigned char l;
1029 old_mode = ppa_hosts[host_no].mode;
1030 for (loop = 0; loop < 8; loop++) {
1031 /* Attempt to use EPP for Test Unit Ready */
1032 if ((ppb & 0x0007) == 0x0000)
1033 ppa_hosts[host_no].mode = PPA_EPP_32;
1035 second_pass:
1036 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1037 /* Select SCSI device */
1038 if (!ppa_select(host_no, loop)) {
1039 ppa_disconnect(host_no);
1040 continue;
1042 printk("ppa: Found device at ID %i, Attempting to use %s\n", loop,
1043 PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1045 /* Send SCSI command */
1046 status = 1;
1047 w_ctr(ppb, 0x0c);
1048 for (l = 0; (l < 6) && (status); l++)
1049 status = ppa_out(host_no, cmd, 1);
1051 if (!status) {
1052 ppa_disconnect(host_no);
1053 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1054 w_dtr(ppb, 0x40);
1055 w_ctr(ppb, 0x08);
1056 udelay(30);
1057 w_ctr(ppb, 0x0c);
1058 udelay(1000);
1059 ppa_disconnect(host_no);
1060 udelay(1000);
1061 if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1062 ppa_hosts[host_no].mode = old_mode;
1063 goto second_pass;
1065 printk("ppa: Unable to establish communication, aborting driver load.\n");
1066 return 1;
1068 w_ctr(ppb, 0x0c);
1069 k = 1000000; /* 1 Second */
1070 do {
1071 l = r_str(ppb);
1072 k--;
1073 udelay(1);
1074 } while (!(l & 0x80) && (k));
1076 l &= 0xf0;
1078 if (l != 0xf0) {
1079 ppa_disconnect(host_no);
1080 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1081 ppa_reset_pulse(ppb);
1082 udelay(1000);
1083 ppa_disconnect(host_no);
1084 udelay(1000);
1085 if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1086 ppa_hosts[host_no].mode = old_mode;
1087 goto second_pass;
1089 printk("ppa: Unable to establish communication, aborting driver load.\n");
1090 return 1;
1092 ppa_disconnect(host_no);
1093 printk("ppa: Communication established with ID %i using %s\n", loop,
1094 PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1095 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1096 ppa_reset_pulse(ppb);
1097 udelay(1000);
1098 ppa_disconnect(host_no);
1099 udelay(1000);
1100 return 0;
1102 printk("ppa: No devices found, aborting driver load.\n");
1103 return 1;