Linux 2.2.0pre4
[davej-history.git] / drivers / scsi / ppa.c
blobe6ffc6189b62cbdafafef349122dcf41dd87d17a
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: {0, 0, ppa_interrupt, NULL}, \
45 jstart: 0, \
46 failed: 0, \
47 p_busy: 0 \
50 #include "ppa.h"
51 #include <linux/parport.h>
53 #define NO_HOSTS 4
54 static ppa_struct ppa_hosts[NO_HOSTS] =
55 {PPA_EMPTY, PPA_EMPTY, PPA_EMPTY, PPA_EMPTY};
57 #define PPA_BASE(x) ppa_hosts[(x)].base
59 void ppa_wakeup(void *ref)
61 ppa_struct *ppa_dev = (ppa_struct *) ref;
63 if (!ppa_dev->p_busy)
64 return;
66 if (parport_claim(ppa_dev->dev)) {
67 printk("ppa: bug in ppa_wakeup\n");
68 return;
70 ppa_dev->p_busy = 0;
71 ppa_dev->base = ppa_dev->dev->port->base;
72 if (ppa_dev->cur_cmd)
73 ppa_dev->cur_cmd->SCp.phase++;
74 return;
77 int ppa_release(struct Scsi_Host *host)
79 int host_no = host->unique_id;
81 printk("Releasing ppa%i\n", host_no);
82 parport_unregister_device(ppa_hosts[host_no].dev);
83 return 0;
86 static int ppa_pb_claim(int host_no)
88 if (parport_claim(ppa_hosts[host_no].dev)) {
89 ppa_hosts[host_no].p_busy = 1;
90 return 1;
92 if (ppa_hosts[host_no].cur_cmd)
93 ppa_hosts[host_no].cur_cmd->SCp.phase++;
94 return 0;
97 #define ppa_pb_release(x) parport_release(ppa_hosts[(x)].dev)
99 /***************************************************************************
100 * Parallel port probing routines *
101 ***************************************************************************/
103 #ifdef MODULE
104 Scsi_Host_Template driver_template = PPA;
105 #include "scsi_module.c"
106 #endif
109 * Start of Chipset kludges
112 int ppa_detect(Scsi_Host_Template * host)
114 struct Scsi_Host *hreg;
115 int ports;
116 int i, nhosts, try_again;
117 struct parport *pb = parport_enumerate();
119 printk("ppa: Version %s\n", PPA_VERSION);
120 nhosts = 0;
121 try_again = 0;
123 if (!pb) {
124 printk("ppa: parport reports no devices.\n");
125 return 0;
127 retry_entry:
128 for (i = 0; pb; i++, pb = pb->next) {
129 int modes, ppb;
131 ppa_hosts[i].dev =
132 parport_register_device(pb, "ppa", NULL, ppa_wakeup,
133 NULL, 0, (void *) &ppa_hosts[i]);
135 if (!ppa_hosts[i].dev)
136 continue;
138 /* Claim the bus so it remembers what we do to the control
139 * registers. [ CTR and ECP ]
141 if (ppa_pb_claim(i)) {
142 unsigned long now = jiffies;
143 while (ppa_hosts[i].p_busy) {
144 schedule(); /* We are safe to schedule here */
145 if (time_after(jiffies, now + 3 * HZ)) {
146 printk(KERN_ERR "ppa%d: failed to claim parport because a "
147 "pardevice is owning the port for too longtime!\n",
149 return 0;
153 ppb = PPA_BASE(i) = ppa_hosts[i].dev->port->base;
154 w_ctr(ppb, 0x0c);
155 modes = ppa_hosts[i].dev->port->modes;
157 /* Mode detection works up the chain of speed
158 * This avoids a nasty if-then-else-if-... tree
160 ppa_hosts[i].mode = PPA_NIBBLE;
162 if (modes & PARPORT_MODE_PCPS2)
163 ppa_hosts[i].mode = PPA_PS2;
165 if (modes & PARPORT_MODE_PCECPPS2) {
166 w_ecr(ppb, 0x20);
167 ppa_hosts[i].mode = PPA_PS2;
169 if (modes & PARPORT_MODE_PCECPEPP)
170 w_ecr(ppb, 0x80);
172 /* Done configuration */
173 ppa_pb_release(i);
175 if (ppa_init(i)) {
176 parport_unregister_device(ppa_hosts[i].dev);
177 continue;
179 /* now the glue ... */
180 switch (ppa_hosts[i].mode) {
181 case PPA_NIBBLE:
182 ports = 3;
183 break;
184 case PPA_PS2:
185 ports = 3;
186 break;
187 case PPA_EPP_8:
188 case PPA_EPP_16:
189 case PPA_EPP_32:
190 ports = 8;
191 break;
192 default: /* Never gets here */
193 continue;
196 host->can_queue = PPA_CAN_QUEUE;
197 host->sg_tablesize = ppa_sg;
198 hreg = scsi_register(host, 0);
199 hreg->io_port = pb->base;
200 hreg->n_io_port = ports;
201 hreg->dma_channel = -1;
202 hreg->unique_id = i;
203 ppa_hosts[i].host = hreg->host_no;
204 nhosts++;
206 if (nhosts == 0) {
207 if (try_again == 1) {
208 printk("WARNING - no ppa compatible devices found.\n");
209 printk(" As of 31/Aug/1998 Iomega started shipping parallel\n");
210 printk(" port ZIP drives with a different interface which is\n");
211 printk(" supported by the imm (ZIP Plus) driver. If the\n");
212 printk(" cable is marked with \"AutoDetect\", this is what has\n");
213 printk(" happened.\n");
214 return 0;
216 try_again = 1;
217 goto retry_entry;
218 } else
219 return 1; /* return number of hosts detected */
222 /* This is to give the ppa driver a way to modify the timings (and other
223 * parameters) by writing to the /proc/scsi/ppa/0 file.
224 * Very simple method really... (To simple, no error checking :( )
225 * Reason: Kernel hackers HATE having to unload and reload modules for
226 * testing...
227 * Also gives a method to use a script to obtain optimum timings (TODO)
230 static inline int ppa_proc_write(int hostno, char *buffer, int length)
232 unsigned long x;
234 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
235 x = simple_strtoul(buffer + 5, NULL, 0);
236 ppa_hosts[hostno].mode = x;
237 return length;
239 printk("ppa /proc: invalid variable\n");
240 return (-EINVAL);
243 int ppa_proc_info(char *buffer, char **start, off_t offset,
244 int length, int hostno, int inout)
246 int i;
247 int len = 0;
249 for (i = 0; i < 4; i++)
250 if (ppa_hosts[i].host == hostno)
251 break;
253 if (inout)
254 return ppa_proc_write(i, buffer, length);
256 len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
257 len += sprintf(buffer + len, "Parport : %s\n", ppa_hosts[i].dev->port->name);
258 len += sprintf(buffer + len, "Mode : %s\n", PPA_MODE_STRING[ppa_hosts[i].mode]);
260 /* Request for beyond end of buffer */
261 if (offset > length)
262 return 0;
264 *start = buffer + offset;
265 len -= offset;
266 if (len > length)
267 len = length;
268 return len;
271 static int device_check(int host_no);
273 #if PPA_DEBUG > 0
274 #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
275 y, __FUNCTION__, __LINE__); ppa_fail_func(x,y);
276 static inline void ppa_fail_func(int host_no, int error_code)
277 #else
278 static inline void ppa_fail(int host_no, int error_code)
279 #endif
281 /* If we fail a device then we trash status / message bytes */
282 if (ppa_hosts[host_no].cur_cmd) {
283 ppa_hosts[host_no].cur_cmd->result = error_code << 16;
284 ppa_hosts[host_no].failed = 1;
289 * Wait for the high bit to be set.
291 * In principle, this could be tied to an interrupt, but the adapter
292 * doesn't appear to be designed to support interrupts. We spin on
293 * the 0x80 ready bit.
295 static unsigned char ppa_wait(int host_no)
297 int k;
298 unsigned short ppb = PPA_BASE(host_no);
299 unsigned char r;
301 k = PPA_SPIN_TMO;
302 do {
303 r = r_str(ppb);
304 k--;
305 udelay(1);
307 while (!(r & 0x80) && (k));
310 * return some status information.
311 * Semantics: 0xc0 = ZIP wants more data
312 * 0xd0 = ZIP wants to send more data
313 * 0xe0 = ZIP is expecting SCSI command data
314 * 0xf0 = end of transfer, ZIP is sending status
316 if (k)
317 return (r & 0xf0);
319 /* Counter expired - Time out occured */
320 ppa_fail(host_no, DID_TIME_OUT);
321 printk("ppa timeout in ppa_wait\n");
322 return 0; /* command timed out */
326 * output a string, in whatever mode is available, according to the
327 * PPA protocol.
329 static inline void epp_reset(unsigned short ppb)
331 int i;
333 i = r_str(ppb);
334 w_str(ppb, i);
335 w_str(ppb, i & 0xfe);
338 static inline void ecp_sync(unsigned short ppb)
340 int i;
342 if ((r_ecr(ppb) & 0xe0) != 0x80)
343 return;
345 for (i = 0; i < 100; i++) {
346 if (r_ecr(ppb) & 0x01)
347 return;
348 udelay(5);
350 printk("ppa: ECP sync failed as data still present in FIFO.\n");
353 static int ppa_byte_out(unsigned short base, const char *buffer, int len)
355 int i;
357 for (i = len; i; i--) {
358 w_dtr(base, *buffer++);
359 w_ctr(base, 0xe);
360 w_ctr(base, 0xc);
362 return 1; /* All went well - we hope! */
365 static int ppa_byte_in(unsigned short base, char *buffer, int len)
367 int i;
369 for (i = len; i; i--) {
370 *buffer++ = r_dtr(base);
371 w_ctr(base, 0x27);
372 w_ctr(base, 0x25);
374 return 1; /* All went well - we hope! */
377 static int ppa_nibble_in(unsigned short base, char *buffer, int len)
379 for (; len; len--) {
380 unsigned char h;
382 w_ctr(base, 0x4);
383 h = r_str(base) & 0xf0;
384 w_ctr(base, 0x6);
385 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
387 return 1; /* All went well - we hope! */
390 static int ppa_out(int host_no, char *buffer, int len)
392 int r;
393 unsigned short ppb = PPA_BASE(host_no);
395 r = ppa_wait(host_no);
397 if ((r & 0x50) != 0x40) {
398 ppa_fail(host_no, DID_ERROR);
399 return 0;
401 switch (ppa_hosts[host_no].mode) {
402 case PPA_NIBBLE:
403 case PPA_PS2:
404 /* 8 bit output, with a loop */
405 r = ppa_byte_out(ppb, buffer, len);
406 break;
408 case PPA_EPP_32:
409 case PPA_EPP_16:
410 case PPA_EPP_8:
411 epp_reset(ppb);
412 w_ctr(ppb, 0x4);
413 #ifdef CONFIG_SCSI_IZIP_EPP16
414 if (!(((long) buffer | len) & 0x01))
415 outsw(ppb + 4, buffer, len >> 1);
416 #else
417 if (!(((long) buffer | len) & 0x03))
418 outsl(ppb + 4, buffer, len >> 2);
419 #endif
420 else
421 outsb(ppb + 4, buffer, len);
422 w_ctr(ppb, 0xc);
423 r = !(r_str(ppb) & 0x01);
424 w_ctr(ppb, 0xc);
425 ecp_sync(ppb);
426 break;
428 default:
429 printk("PPA: bug in ppa_out()\n");
430 r = 0;
432 return r;
435 static int ppa_in(int host_no, char *buffer, int len)
437 int r;
438 unsigned short ppb = PPA_BASE(host_no);
440 r = ppa_wait(host_no);
442 if ((r & 0x50) != 0x50) {
443 ppa_fail(host_no, DID_ERROR);
444 return 0;
446 switch (ppa_hosts[host_no].mode) {
447 case PPA_NIBBLE:
448 /* 4 bit input, with a loop */
449 r = ppa_nibble_in(ppb, buffer, len);
450 w_ctr(ppb, 0xc);
451 break;
453 case PPA_PS2:
454 /* 8 bit input, with a loop */
455 w_ctr(ppb, 0x25);
456 r = ppa_byte_in(ppb, buffer, len);
457 w_ctr(ppb, 0x4);
458 w_ctr(ppb, 0xc);
459 break;
461 case PPA_EPP_32:
462 case PPA_EPP_16:
463 case PPA_EPP_8:
464 epp_reset(ppb);
465 w_ctr(ppb, 0x24);
466 #ifdef CONFIG_SCSI_IZIP_EPP16
467 if (!(((long) buffer | len) & 0x01))
468 insw(ppb + 4, buffer, len >> 1);
469 #else
470 if (!(((long) buffer | len) & 0x03))
471 insl(ppb + 4, buffer, len >> 2);
472 #endif
473 else
474 insb(ppb + 4, buffer, len);
475 w_ctr(ppb, 0x2c);
476 r = !(r_str(ppb) & 0x01);
477 w_ctr(ppb, 0x2c);
478 ecp_sync(ppb);
479 break;
481 default:
482 printk("PPA: bug in ppa_ins()\n");
483 r = 0;
484 break;
486 return r;
489 /* end of ppa_io.h */
490 static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
492 w_dtr(ppb, b);
493 w_ctr(ppb, 0xc);
494 w_ctr(ppb, 0xe);
495 w_ctr(ppb, 0xc);
496 w_ctr(ppb, 0x4);
497 w_ctr(ppb, 0xc);
500 static void ppa_disconnect(int host_no)
502 unsigned short ppb = PPA_BASE(host_no);
504 ppa_d_pulse(ppb, 0);
505 ppa_d_pulse(ppb, 0x3c);
506 ppa_d_pulse(ppb, 0x20);
507 ppa_d_pulse(ppb, 0xf);
510 static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
512 w_dtr(ppb, b);
513 w_ctr(ppb, 0x4);
514 w_ctr(ppb, 0x6);
515 w_ctr(ppb, 0x4);
516 w_ctr(ppb, 0xc);
519 static inline void ppa_connect(int host_no, int flag)
521 unsigned short ppb = PPA_BASE(host_no);
523 ppa_c_pulse(ppb, 0);
524 ppa_c_pulse(ppb, 0x3c);
525 ppa_c_pulse(ppb, 0x20);
526 if ((flag == CONNECT_EPP_MAYBE) &&
527 IN_EPP_MODE(ppa_hosts[host_no].mode))
528 ppa_c_pulse(ppb, 0xcf);
529 else
530 ppa_c_pulse(ppb, 0x8f);
533 static int ppa_select(int host_no, int target)
535 int k;
536 unsigned short ppb = PPA_BASE(host_no);
539 * Bit 6 (0x40) is the device selected bit.
540 * First we must wait till the current device goes off line...
542 k = PPA_SELECT_TMO;
543 do {
544 k--;
545 } while ((r_str(ppb) & 0x40) && (k));
546 if (!k)
547 return 0;
549 w_dtr(ppb, (1 << target));
550 w_ctr(ppb, 0xe);
551 w_ctr(ppb, 0xc);
552 w_dtr(ppb, 0x80); /* This is NOT the initator */
553 w_ctr(ppb, 0x8);
555 k = PPA_SELECT_TMO;
556 do {
557 k--;
559 while (!(r_str(ppb) & 0x40) && (k));
560 if (!k)
561 return 0;
563 return 1;
567 * This is based on a trace of what the Iomega DOS 'guest' driver does.
568 * I've tried several different kinds of parallel ports with guest and
569 * coded this to react in the same ways that it does.
571 * The return value from this function is just a hint about where the
572 * handshaking failed.
575 static int ppa_init(int host_no)
577 int retv;
578 unsigned short ppb = PPA_BASE(host_no);
580 #if defined(CONFIG_PARPORT) || defined(CONFIG_PARPORT_MODULE)
581 if (ppa_pb_claim(host_no))
582 while (ppa_hosts[host_no].p_busy)
583 schedule(); /* We can safe schedule here */
584 #endif
586 ppa_disconnect(host_no);
587 ppa_connect(host_no, CONNECT_NORMAL);
589 retv = 2; /* Failed */
591 w_ctr(ppb, 0xe);
592 if ((r_str(ppb) & 0x08) == 0x08)
593 retv--;
595 w_ctr(ppb, 0xc);
596 if ((r_str(ppb) & 0x08) == 0x00)
597 retv--;
599 if (!retv)
600 ppa_reset_pulse(ppb);
601 udelay(1000); /* Allow devices to settle down */
602 ppa_disconnect(host_no);
603 udelay(1000); /* Another delay to allow devices to settle */
605 if (!retv)
606 retv = device_check(host_no);
608 ppa_pb_release(host_no);
609 return retv;
612 static inline int ppa_send_command(Scsi_Cmnd * cmd)
614 int host_no = cmd->host->unique_id;
615 int k;
617 w_ctr(PPA_BASE(host_no), 0x0c);
619 for (k = 0; k < cmd->cmd_len; k++)
620 if (!ppa_out(host_no, &cmd->cmnd[k], 1))
621 return 0;
622 return 1;
626 * The bulk flag enables some optimisations in the data transfer loops,
627 * it should be true for any command that transfers data in integral
628 * numbers of sectors.
630 * The driver appears to remain stable if we speed up the parallel port
631 * i/o in this function, but not elsewhere.
633 static int ppa_completion(Scsi_Cmnd * cmd)
635 /* Return codes:
636 * -1 Error
637 * 0 Told to schedule
638 * 1 Finished data transfer
640 int host_no = cmd->host->unique_id;
641 unsigned short ppb = PPA_BASE(host_no);
642 unsigned long start_jiffies = jiffies;
644 unsigned char r, v;
645 int fast, bulk, status;
647 v = cmd->cmnd[0];
648 bulk = ((v == READ_6) ||
649 (v == READ_10) ||
650 (v == WRITE_6) ||
651 (v == WRITE_10));
654 * We only get here if the drive is ready to comunicate,
655 * hence no need for a full ppa_wait.
657 r = (r_str(ppb) & 0xf0);
659 while (r != (unsigned char) 0xf0) {
661 * If we have been running for more than a full timer tick
662 * then take a rest.
664 if (time_after(jiffies, start_jiffies + 1))
665 return 0;
667 if (((r & 0xc0) != 0xc0) || (cmd->SCp.this_residual <= 0)) {
668 ppa_fail(host_no, DID_ERROR);
669 return -1; /* ERROR_RETURN */
671 /* determine if we should use burst I/O */ fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
672 ? PPA_BURST_SIZE : 1;
674 if (r == (unsigned char) 0xc0)
675 status = ppa_out(host_no, cmd->SCp.ptr, fast);
676 else
677 status = ppa_in(host_no, cmd->SCp.ptr, fast);
679 cmd->SCp.ptr += fast;
680 cmd->SCp.this_residual -= fast;
682 if (!status) {
683 ppa_fail(host_no, DID_BUS_BUSY);
684 return -1; /* ERROR_RETURN */
686 if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
687 /* if scatter/gather, advance to the next segment */
688 if (cmd->SCp.buffers_residual--) {
689 cmd->SCp.buffer++;
690 cmd->SCp.this_residual = cmd->SCp.buffer->length;
691 cmd->SCp.ptr = cmd->SCp.buffer->address;
694 /* Now check to see if the drive is ready to comunicate */
695 r = (r_str(ppb) & 0xf0);
696 /* If not, drop back down to the scheduler and wait a timer tick */
697 if (!(r & 0x80))
698 return 0;
700 return 1; /* FINISH_RETURN */
703 /* deprecated synchronous interface */
704 int ppa_command(Scsi_Cmnd * cmd)
706 static int first_pass = 1;
707 int host_no = cmd->host->unique_id;
709 if (first_pass) {
710 printk("ppa: using non-queuing interface\n");
711 first_pass = 0;
713 if (ppa_hosts[host_no].cur_cmd) {
714 printk("PPA: bug in ppa_command\n");
715 return 0;
717 ppa_hosts[host_no].failed = 0;
718 ppa_hosts[host_no].jstart = jiffies;
719 ppa_hosts[host_no].cur_cmd = cmd;
720 cmd->result = DID_ERROR << 16; /* default return code */
721 cmd->SCp.phase = 0;
723 ppa_pb_claim(host_no);
725 while (ppa_engine(&ppa_hosts[host_no], cmd))
726 schedule();
728 if (cmd->SCp.phase) /* Only disconnect if we have connected */
729 ppa_disconnect(cmd->host->unique_id);
731 ppa_pb_release(host_no);
732 ppa_hosts[host_no].cur_cmd = 0;
733 return cmd->result;
737 * Since the PPA itself doesn't generate interrupts, we use
738 * the scheduler's task queue to generate a stream of call-backs and
739 * complete the request when the drive is ready.
741 static void ppa_interrupt(void *data)
743 ppa_struct *tmp = (ppa_struct *) data;
744 Scsi_Cmnd *cmd = tmp->cur_cmd;
746 if (!cmd) {
747 printk("PPA: bug in ppa_interrupt\n");
748 return;
750 if (ppa_engine(tmp, cmd)) {
751 tmp->ppa_tq.data = (void *) tmp;
752 tmp->ppa_tq.sync = 0;
753 queue_task(&tmp->ppa_tq, &tq_timer);
754 return;
756 /* Command must of completed hence it is safe to let go... */
757 #if PPA_DEBUG > 0
758 switch ((cmd->result >> 16) & 0xff) {
759 case DID_OK:
760 break;
761 case DID_NO_CONNECT:
762 printk("ppa: no device at SCSI ID %i\n", cmd->target);
763 break;
764 case DID_BUS_BUSY:
765 printk("ppa: BUS BUSY - EPP timeout detected\n");
766 break;
767 case DID_TIME_OUT:
768 printk("ppa: unknown timeout\n");
769 break;
770 case DID_ABORT:
771 printk("ppa: told to abort\n");
772 break;
773 case DID_PARITY:
774 printk("ppa: parity error (???)\n");
775 break;
776 case DID_ERROR:
777 printk("ppa: internal driver error\n");
778 break;
779 case DID_RESET:
780 printk("ppa: told to reset device\n");
781 break;
782 case DID_BAD_INTR:
783 printk("ppa: bad interrupt (???)\n");
784 break;
785 default:
786 printk("ppa: bad return code (%02x)\n", (cmd->result >> 16) & 0xff);
788 #endif
790 if (cmd->SCp.phase > 1)
791 ppa_disconnect(cmd->host->unique_id);
792 if (cmd->SCp.phase > 0)
793 ppa_pb_release(cmd->host->unique_id);
795 tmp->cur_cmd = 0;
796 cmd->scsi_done(cmd);
797 return;
800 static int ppa_engine(ppa_struct * tmp, Scsi_Cmnd * cmd)
802 int host_no = cmd->host->unique_id;
803 unsigned short ppb = PPA_BASE(host_no);
804 unsigned char l = 0, h = 0;
805 int retv;
807 /* First check for any errors that may of occured
808 * Here we check for internal errors
810 if (tmp->failed)
811 return 0;
813 switch (cmd->SCp.phase) {
814 case 0: /* Phase 0 - Waiting for parport */
815 if ((jiffies - tmp->jstart) > HZ) {
817 * We waited more than a second
818 * for parport to call us
820 ppa_fail(host_no, DID_BUS_BUSY);
821 return 0;
823 return 1; /* wait until ppa_wakeup claims parport */
824 case 1: /* Phase 1 - Connected */
825 { /* Perform a sanity check for cable unplugged */
826 int retv = 2; /* Failed */
828 ppa_connect(host_no, CONNECT_EPP_MAYBE);
830 w_ctr(ppb, 0xe);
831 if ((r_str(ppb) & 0x08) == 0x08)
832 retv--;
834 w_ctr(ppb, 0xc);
835 if ((r_str(ppb) & 0x08) == 0x00)
836 retv--;
838 if (retv) {
839 if ((jiffies - tmp->jstart) > (1 * HZ)) {
840 printk("ppa: Parallel port cable is unplugged!!\n");
841 ppa_fail(host_no, DID_BUS_BUSY);
842 return 0;
843 } else {
844 ppa_disconnect(host_no);
845 return 1; /* Try again in a jiffy */
848 cmd->SCp.phase++;
851 case 2: /* Phase 2 - We are now talking to the scsi bus */
852 if (!ppa_select(host_no, cmd->target)) {
853 ppa_fail(host_no, DID_NO_CONNECT);
854 return 0;
856 cmd->SCp.phase++;
858 case 3: /* Phase 3 - Ready to accept a command */
859 w_ctr(ppb, 0x0c);
860 if (!(r_str(ppb) & 0x80))
861 return 1;
863 if (!ppa_send_command(cmd))
864 return 0;
865 cmd->SCp.phase++;
867 case 4: /* Phase 4 - Setup scatter/gather buffers */
868 if (cmd->use_sg) {
869 /* if many buffers are available, start filling the first */
870 cmd->SCp.buffer = (struct scatterlist *) cmd->request_buffer;
871 cmd->SCp.this_residual = cmd->SCp.buffer->length;
872 cmd->SCp.ptr = cmd->SCp.buffer->address;
873 } else {
874 /* else fill the only available buffer */
875 cmd->SCp.buffer = NULL;
876 cmd->SCp.this_residual = cmd->request_bufflen;
877 cmd->SCp.ptr = cmd->request_buffer;
879 cmd->SCp.buffers_residual = cmd->use_sg;
880 cmd->SCp.phase++;
882 case 5: /* Phase 5 - Data transfer stage */
883 w_ctr(ppb, 0x0c);
884 if (!(r_str(ppb) & 0x80))
885 return 1;
887 retv = ppa_completion(cmd);
888 if (retv == -1)
889 return 0;
890 if (retv == 0)
891 return 1;
892 cmd->SCp.phase++;
894 case 6: /* Phase 6 - Read status/message */
895 cmd->result = DID_OK << 16;
896 /* Check for data overrun */
897 if (ppa_wait(host_no) != (unsigned char) 0xf0) {
898 ppa_fail(host_no, DID_ERROR);
899 return 0;
901 if (ppa_in(host_no, &l, 1)) { /* read status byte */
902 /* Check for optional message byte */
903 if (ppa_wait(host_no) == (unsigned char) 0xf0)
904 ppa_in(host_no, &h, 1);
905 cmd->result = (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
907 return 0; /* Finished */
908 break;
910 default:
911 printk("ppa: Invalid scsi phase\n");
913 return 0;
916 int ppa_queuecommand(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
918 int host_no = cmd->host->unique_id;
920 if (ppa_hosts[host_no].cur_cmd) {
921 printk("PPA: bug in ppa_queuecommand\n");
922 return 0;
924 ppa_hosts[host_no].failed = 0;
925 ppa_hosts[host_no].jstart = jiffies;
926 ppa_hosts[host_no].cur_cmd = cmd;
927 cmd->scsi_done = done;
928 cmd->result = DID_ERROR << 16; /* default return code */
929 cmd->SCp.phase = 0; /* bus free */
931 ppa_pb_claim(host_no);
933 ppa_hosts[host_no].ppa_tq.data = ppa_hosts + host_no;
934 ppa_hosts[host_no].ppa_tq.sync = 0;
935 queue_task(&ppa_hosts[host_no].ppa_tq, &tq_immediate);
936 mark_bh(IMMEDIATE_BH);
938 return 0;
942 * Apparently the the disk->capacity attribute is off by 1 sector
943 * for all disk drives. We add the one here, but it should really
944 * be done in sd.c. Even if it gets fixed there, this will still
945 * work.
947 int ppa_biosparam(Disk * disk, kdev_t dev, int ip[])
949 ip[0] = 0x40;
950 ip[1] = 0x20;
951 ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
952 if (ip[2] > 1024) {
953 ip[0] = 0xff;
954 ip[1] = 0x3f;
955 ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
956 if (ip[2] > 1023)
957 ip[2] = 1023;
959 return 0;
962 int ppa_abort(Scsi_Cmnd * cmd)
964 int host_no = cmd->host->unique_id;
966 * There is no method for aborting commands since Iomega
967 * have tied the SCSI_MESSAGE line high in the interface
970 switch (cmd->SCp.phase) {
971 case 0: /* Do not have access to parport */
972 case 1: /* Have not connected to interface */
973 ppa_hosts[host_no].cur_cmd = NULL; /* Forget the problem */
974 return SUCCESS;
975 break;
976 default: /* SCSI command sent, can not abort */
977 return FAILED;
978 break;
982 static void ppa_reset_pulse(unsigned int base)
984 w_dtr(base, 0x40);
985 w_ctr(base, 0x8);
986 udelay(30);
987 w_ctr(base, 0xc);
990 int ppa_reset(Scsi_Cmnd * cmd)
992 int host_no = cmd->host->unique_id;
994 if (cmd->SCp.phase)
995 ppa_disconnect(host_no);
996 ppa_hosts[host_no].cur_cmd = NULL; /* Forget the problem */
998 ppa_connect(host_no, CONNECT_NORMAL);
999 ppa_reset_pulse(PPA_BASE(host_no));
1000 udelay(1000); /* device settle delay */
1001 ppa_disconnect(host_no);
1002 udelay(1000); /* device settle delay */
1003 return SUCCESS;
1006 static int device_check(int host_no)
1008 /* This routine looks for a device and then attempts to use EPP
1009 to send a command. If all goes as planned then EPP is available. */
1011 static char cmd[6] =
1012 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1013 int loop, old_mode, status, k, ppb = PPA_BASE(host_no);
1014 unsigned char l;
1016 old_mode = ppa_hosts[host_no].mode;
1017 for (loop = 0; loop < 8; loop++) {
1018 /* Attempt to use EPP for Test Unit Ready */
1019 if ((ppb & 0x0007) == 0x0000)
1020 ppa_hosts[host_no].mode = PPA_EPP_32;
1022 second_pass:
1023 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1024 /* Select SCSI device */
1025 if (!ppa_select(host_no, loop)) {
1026 ppa_disconnect(host_no);
1027 continue;
1029 printk("ppa: Found device at ID %i, Attempting to use %s\n", loop,
1030 PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1032 /* Send SCSI command */
1033 status = 1;
1034 w_ctr(ppb, 0x0c);
1035 for (l = 0; (l < 6) && (status); l++)
1036 status = ppa_out(host_no, cmd, 1);
1038 if (!status) {
1039 ppa_disconnect(host_no);
1040 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1041 w_dtr(ppb, 0x40);
1042 w_ctr(ppb, 0x08);
1043 udelay(30);
1044 w_ctr(ppb, 0x0c);
1045 udelay(1000);
1046 ppa_disconnect(host_no);
1047 udelay(1000);
1048 if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1049 ppa_hosts[host_no].mode = old_mode;
1050 goto second_pass;
1052 printk("ppa: Unable to establish communication, aborting driver load.\n");
1053 return 1;
1055 w_ctr(ppb, 0x0c);
1056 k = 1000000; /* 1 Second */
1057 do {
1058 l = r_str(ppb);
1059 k--;
1060 udelay(1);
1061 } while (!(l & 0x80) && (k));
1063 l &= 0xf0;
1065 if (l != 0xf0) {
1066 ppa_disconnect(host_no);
1067 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1068 ppa_reset_pulse(ppb);
1069 udelay(1000);
1070 ppa_disconnect(host_no);
1071 udelay(1000);
1072 if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1073 ppa_hosts[host_no].mode = old_mode;
1074 goto second_pass;
1076 printk("ppa: Unable to establish communication, aborting driver load.\n");
1077 return 1;
1079 ppa_disconnect(host_no);
1080 printk("ppa: Communication established with ID %i using %s\n", loop,
1081 PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1082 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1083 ppa_reset_pulse(ppb);
1084 udelay(1000);
1085 ppa_disconnect(host_no);
1086 udelay(1000);
1087 return 0;
1089 printk("ppa: No devices found, aborting driver load.\n");
1090 return 1;