Import 2.3.10pre1
[davej-history.git] / drivers / scsi / ppa.c
blob715a15acf5d4c920aa3a37205f3313ec416a922c
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"
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 #ifdef MODULE
103 Scsi_Host_Template driver_template = PPA;
104 #include "scsi_module.c"
105 #endif
108 * Start of Chipset kludges
111 int ppa_detect(Scsi_Host_Template * host)
113 struct Scsi_Host *hreg;
114 int ports;
115 int i, nhosts, try_again;
116 struct parport *pb = parport_enumerate();
118 printk("ppa: Version %s\n", PPA_VERSION);
119 nhosts = 0;
120 try_again = 0;
122 if (!pb) {
123 printk("ppa: parport reports no devices.\n");
124 return 0;
126 retry_entry:
127 for (i = 0; pb; i++, pb = pb->next) {
128 int modes, ppb;
130 ppa_hosts[i].dev =
131 parport_register_device(pb, "ppa", NULL, ppa_wakeup,
132 NULL, 0, (void *) &ppa_hosts[i]);
134 if (!ppa_hosts[i].dev)
135 continue;
137 /* Claim the bus so it remembers what we do to the control
138 * registers. [ CTR and ECP ]
140 if (ppa_pb_claim(i)) {
141 unsigned long now = jiffies;
142 while (ppa_hosts[i].p_busy) {
143 schedule(); /* We are safe to schedule here */
144 if (time_after(jiffies, now + 3 * HZ)) {
145 printk(KERN_ERR "ppa%d: failed to claim parport because a "
146 "pardevice is owning the port for too longtime!\n",
148 return 0;
152 ppb = PPA_BASE(i) = ppa_hosts[i].dev->port->base;
153 w_ctr(ppb, 0x0c);
154 modes = ppa_hosts[i].dev->port->modes;
156 /* Mode detection works up the chain of speed
157 * This avoids a nasty if-then-else-if-... tree
159 ppa_hosts[i].mode = PPA_NIBBLE;
161 if (modes & PARPORT_MODE_TRISTATE)
162 ppa_hosts[i].mode = PPA_PS2;
164 if (modes & PARPORT_MODE_ECP) {
165 w_ecr(ppb, 0x20);
166 ppa_hosts[i].mode = PPA_PS2;
168 if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
169 w_ecr(ppb, 0x80);
171 /* Done configuration */
172 ppa_pb_release(i);
174 if (ppa_init(i)) {
175 parport_unregister_device(ppa_hosts[i].dev);
176 continue;
178 /* now the glue ... */
179 switch (ppa_hosts[i].mode) {
180 case PPA_NIBBLE:
181 ports = 3;
182 break;
183 case PPA_PS2:
184 ports = 3;
185 break;
186 case PPA_EPP_8:
187 case PPA_EPP_16:
188 case PPA_EPP_32:
189 ports = 8;
190 break;
191 default: /* Never gets here */
192 continue;
195 host->can_queue = PPA_CAN_QUEUE;
196 host->sg_tablesize = ppa_sg;
197 hreg = scsi_register(host, 0);
198 hreg->io_port = pb->base;
199 hreg->n_io_port = ports;
200 hreg->dma_channel = -1;
201 hreg->unique_id = i;
202 ppa_hosts[i].host = hreg->host_no;
203 nhosts++;
205 if (nhosts == 0) {
206 if (try_again == 1) {
207 printk("WARNING - no ppa compatible devices found.\n");
208 printk(" As of 31/Aug/1998 Iomega started shipping parallel\n");
209 printk(" port ZIP drives with a different interface which is\n");
210 printk(" supported by the imm (ZIP Plus) driver. If the\n");
211 printk(" cable is marked with \"AutoDetect\", this is what has\n");
212 printk(" happened.\n");
213 return 0;
215 try_again = 1;
216 goto retry_entry;
217 } else
218 return 1; /* return number of hosts detected */
221 /* This is to give the ppa driver a way to modify the timings (and other
222 * parameters) by writing to the /proc/scsi/ppa/0 file.
223 * Very simple method really... (To simple, no error checking :( )
224 * Reason: Kernel hackers HATE having to unload and reload modules for
225 * testing...
226 * Also gives a method to use a script to obtain optimum timings (TODO)
229 static inline int ppa_proc_write(int hostno, char *buffer, int length)
231 unsigned long x;
233 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
234 x = simple_strtoul(buffer + 5, NULL, 0);
235 ppa_hosts[hostno].mode = x;
236 return length;
238 printk("ppa /proc: invalid variable\n");
239 return (-EINVAL);
242 int ppa_proc_info(char *buffer, char **start, off_t offset,
243 int length, int hostno, int inout)
245 int i;
246 int len = 0;
248 for (i = 0; i < 4; i++)
249 if (ppa_hosts[i].host == hostno)
250 break;
252 if (inout)
253 return ppa_proc_write(i, buffer, length);
255 len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
256 len += sprintf(buffer + len, "Parport : %s\n", ppa_hosts[i].dev->port->name);
257 len += sprintf(buffer + len, "Mode : %s\n", PPA_MODE_STRING[ppa_hosts[i].mode]);
259 /* Request for beyond end of buffer */
260 if (offset > length)
261 return 0;
263 *start = buffer + offset;
264 len -= offset;
265 if (len > length)
266 len = length;
267 return len;
270 static int device_check(int host_no);
272 #if PPA_DEBUG > 0
273 #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
274 y, __FUNCTION__, __LINE__); ppa_fail_func(x,y);
275 static inline void ppa_fail_func(int host_no, int error_code)
276 #else
277 static inline void ppa_fail(int host_no, int error_code)
278 #endif
280 /* If we fail a device then we trash status / message bytes */
281 if (ppa_hosts[host_no].cur_cmd) {
282 ppa_hosts[host_no].cur_cmd->result = error_code << 16;
283 ppa_hosts[host_no].failed = 1;
288 * Wait for the high bit to be set.
290 * In principle, this could be tied to an interrupt, but the adapter
291 * doesn't appear to be designed to support interrupts. We spin on
292 * the 0x80 ready bit.
294 static unsigned char ppa_wait(int host_no)
296 int k;
297 unsigned short ppb = PPA_BASE(host_no);
298 unsigned char r;
300 k = PPA_SPIN_TMO;
301 do {
302 r = r_str(ppb);
303 k--;
304 udelay(1);
306 while (!(r & 0x80) && (k));
309 * return some status information.
310 * Semantics: 0xc0 = ZIP wants more data
311 * 0xd0 = ZIP wants to send more data
312 * 0xe0 = ZIP is expecting SCSI command data
313 * 0xf0 = end of transfer, ZIP is sending status
315 if (k)
316 return (r & 0xf0);
318 /* Counter expired - Time out occurred */
319 ppa_fail(host_no, DID_TIME_OUT);
320 printk("ppa timeout in ppa_wait\n");
321 return 0; /* command timed out */
325 * output a string, in whatever mode is available, according to the
326 * PPA protocol.
328 static inline void epp_reset(unsigned short ppb)
330 int i;
332 i = r_str(ppb);
333 w_str(ppb, i);
334 w_str(ppb, i & 0xfe);
337 static inline void ecp_sync(unsigned short ppb)
339 int i;
341 if ((r_ecr(ppb) & 0xe0) != 0x80)
342 return;
344 for (i = 0; i < 100; i++) {
345 if (r_ecr(ppb) & 0x01)
346 return;
347 udelay(5);
349 printk("ppa: ECP sync failed as data still present in FIFO.\n");
352 static int ppa_byte_out(unsigned short base, const char *buffer, int len)
354 int i;
356 for (i = len; i; i--) {
357 w_dtr(base, *buffer++);
358 w_ctr(base, 0xe);
359 w_ctr(base, 0xc);
361 return 1; /* All went well - we hope! */
364 static int ppa_byte_in(unsigned short base, char *buffer, int len)
366 int i;
368 for (i = len; i; i--) {
369 *buffer++ = r_dtr(base);
370 w_ctr(base, 0x27);
371 w_ctr(base, 0x25);
373 return 1; /* All went well - we hope! */
376 static int ppa_nibble_in(unsigned short base, char *buffer, int len)
378 for (; len; len--) {
379 unsigned char h;
381 w_ctr(base, 0x4);
382 h = r_str(base) & 0xf0;
383 w_ctr(base, 0x6);
384 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
386 return 1; /* All went well - we hope! */
389 static int ppa_out(int host_no, char *buffer, int len)
391 int r;
392 unsigned short ppb = PPA_BASE(host_no);
394 r = ppa_wait(host_no);
396 if ((r & 0x50) != 0x40) {
397 ppa_fail(host_no, DID_ERROR);
398 return 0;
400 switch (ppa_hosts[host_no].mode) {
401 case PPA_NIBBLE:
402 case PPA_PS2:
403 /* 8 bit output, with a loop */
404 r = ppa_byte_out(ppb, buffer, len);
405 break;
407 case PPA_EPP_32:
408 case PPA_EPP_16:
409 case PPA_EPP_8:
410 epp_reset(ppb);
411 w_ctr(ppb, 0x4);
412 #ifdef CONFIG_SCSI_IZIP_EPP16
413 if (!(((long) buffer | len) & 0x01))
414 outsw(ppb + 4, buffer, len >> 1);
415 #else
416 if (!(((long) buffer | len) & 0x03))
417 outsl(ppb + 4, buffer, len >> 2);
418 #endif
419 else
420 outsb(ppb + 4, buffer, len);
421 w_ctr(ppb, 0xc);
422 r = !(r_str(ppb) & 0x01);
423 w_ctr(ppb, 0xc);
424 ecp_sync(ppb);
425 break;
427 default:
428 printk("PPA: bug in ppa_out()\n");
429 r = 0;
431 return r;
434 static int ppa_in(int host_no, char *buffer, int len)
436 int r;
437 unsigned short ppb = PPA_BASE(host_no);
439 r = ppa_wait(host_no);
441 if ((r & 0x50) != 0x50) {
442 ppa_fail(host_no, DID_ERROR);
443 return 0;
445 switch (ppa_hosts[host_no].mode) {
446 case PPA_NIBBLE:
447 /* 4 bit input, with a loop */
448 r = ppa_nibble_in(ppb, buffer, len);
449 w_ctr(ppb, 0xc);
450 break;
452 case PPA_PS2:
453 /* 8 bit input, with a loop */
454 w_ctr(ppb, 0x25);
455 r = ppa_byte_in(ppb, buffer, len);
456 w_ctr(ppb, 0x4);
457 w_ctr(ppb, 0xc);
458 break;
460 case PPA_EPP_32:
461 case PPA_EPP_16:
462 case PPA_EPP_8:
463 epp_reset(ppb);
464 w_ctr(ppb, 0x24);
465 #ifdef CONFIG_SCSI_IZIP_EPP16
466 if (!(((long) buffer | len) & 0x01))
467 insw(ppb + 4, buffer, len >> 1);
468 #else
469 if (!(((long) buffer | len) & 0x03))
470 insl(ppb + 4, buffer, len >> 2);
471 #endif
472 else
473 insb(ppb + 4, buffer, len);
474 w_ctr(ppb, 0x2c);
475 r = !(r_str(ppb) & 0x01);
476 w_ctr(ppb, 0x2c);
477 ecp_sync(ppb);
478 break;
480 default:
481 printk("PPA: bug in ppa_ins()\n");
482 r = 0;
483 break;
485 return r;
488 /* end of ppa_io.h */
489 static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
491 w_dtr(ppb, b);
492 w_ctr(ppb, 0xc);
493 w_ctr(ppb, 0xe);
494 w_ctr(ppb, 0xc);
495 w_ctr(ppb, 0x4);
496 w_ctr(ppb, 0xc);
499 static void ppa_disconnect(int host_no)
501 unsigned short ppb = PPA_BASE(host_no);
503 ppa_d_pulse(ppb, 0);
504 ppa_d_pulse(ppb, 0x3c);
505 ppa_d_pulse(ppb, 0x20);
506 ppa_d_pulse(ppb, 0xf);
509 static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
511 w_dtr(ppb, b);
512 w_ctr(ppb, 0x4);
513 w_ctr(ppb, 0x6);
514 w_ctr(ppb, 0x4);
515 w_ctr(ppb, 0xc);
518 static inline void ppa_connect(int host_no, int flag)
520 unsigned short ppb = PPA_BASE(host_no);
522 ppa_c_pulse(ppb, 0);
523 ppa_c_pulse(ppb, 0x3c);
524 ppa_c_pulse(ppb, 0x20);
525 if ((flag == CONNECT_EPP_MAYBE) &&
526 IN_EPP_MODE(ppa_hosts[host_no].mode))
527 ppa_c_pulse(ppb, 0xcf);
528 else
529 ppa_c_pulse(ppb, 0x8f);
532 static int ppa_select(int host_no, int target)
534 int k;
535 unsigned short ppb = PPA_BASE(host_no);
538 * Bit 6 (0x40) is the device selected bit.
539 * First we must wait till the current device goes off line...
541 k = PPA_SELECT_TMO;
542 do {
543 k--;
544 } while ((r_str(ppb) & 0x40) && (k));
545 if (!k)
546 return 0;
548 w_dtr(ppb, (1 << target));
549 w_ctr(ppb, 0xe);
550 w_ctr(ppb, 0xc);
551 w_dtr(ppb, 0x80); /* This is NOT the initator */
552 w_ctr(ppb, 0x8);
554 k = PPA_SELECT_TMO;
555 do {
556 k--;
558 while (!(r_str(ppb) & 0x40) && (k));
559 if (!k)
560 return 0;
562 return 1;
566 * This is based on a trace of what the Iomega DOS 'guest' driver does.
567 * I've tried several different kinds of parallel ports with guest and
568 * coded this to react in the same ways that it does.
570 * The return value from this function is just a hint about where the
571 * handshaking failed.
574 static int ppa_init(int host_no)
576 int retv;
577 unsigned short ppb = PPA_BASE(host_no);
579 #if defined(CONFIG_PARPORT) || defined(CONFIG_PARPORT_MODULE)
580 if (ppa_pb_claim(host_no))
581 while (ppa_hosts[host_no].p_busy)
582 schedule(); /* We can safe schedule here */
583 #endif
585 ppa_disconnect(host_no);
586 ppa_connect(host_no, CONNECT_NORMAL);
588 retv = 2; /* Failed */
590 w_ctr(ppb, 0xe);
591 if ((r_str(ppb) & 0x08) == 0x08)
592 retv--;
594 w_ctr(ppb, 0xc);
595 if ((r_str(ppb) & 0x08) == 0x00)
596 retv--;
598 if (!retv)
599 ppa_reset_pulse(ppb);
600 udelay(1000); /* Allow devices to settle down */
601 ppa_disconnect(host_no);
602 udelay(1000); /* Another delay to allow devices to settle */
604 if (!retv)
605 retv = device_check(host_no);
607 ppa_pb_release(host_no);
608 return retv;
611 static inline int ppa_send_command(Scsi_Cmnd * cmd)
613 int host_no = cmd->host->unique_id;
614 int k;
616 w_ctr(PPA_BASE(host_no), 0x0c);
618 for (k = 0; k < cmd->cmd_len; k++)
619 if (!ppa_out(host_no, &cmd->cmnd[k], 1))
620 return 0;
621 return 1;
625 * The bulk flag enables some optimisations in the data transfer loops,
626 * it should be true for any command that transfers data in integral
627 * numbers of sectors.
629 * The driver appears to remain stable if we speed up the parallel port
630 * i/o in this function, but not elsewhere.
632 static int ppa_completion(Scsi_Cmnd * cmd)
634 /* Return codes:
635 * -1 Error
636 * 0 Told to schedule
637 * 1 Finished data transfer
639 int host_no = cmd->host->unique_id;
640 unsigned short ppb = PPA_BASE(host_no);
641 unsigned long start_jiffies = jiffies;
643 unsigned char r, v;
644 int fast, bulk, status;
646 v = cmd->cmnd[0];
647 bulk = ((v == READ_6) ||
648 (v == READ_10) ||
649 (v == WRITE_6) ||
650 (v == WRITE_10));
653 * We only get here if the drive is ready to comunicate,
654 * hence no need for a full ppa_wait.
656 r = (r_str(ppb) & 0xf0);
658 while (r != (unsigned char) 0xf0) {
660 * If we have been running for more than a full timer tick
661 * then take a rest.
663 if (time_after(jiffies, start_jiffies + 1))
664 return 0;
666 if (((r & 0xc0) != 0xc0) || (cmd->SCp.this_residual <= 0)) {
667 ppa_fail(host_no, DID_ERROR);
668 return -1; /* ERROR_RETURN */
670 /* determine if we should use burst I/O */ fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
671 ? PPA_BURST_SIZE : 1;
673 if (r == (unsigned char) 0xc0)
674 status = ppa_out(host_no, cmd->SCp.ptr, fast);
675 else
676 status = ppa_in(host_no, cmd->SCp.ptr, fast);
678 cmd->SCp.ptr += fast;
679 cmd->SCp.this_residual -= fast;
681 if (!status) {
682 ppa_fail(host_no, DID_BUS_BUSY);
683 return -1; /* ERROR_RETURN */
685 if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
686 /* if scatter/gather, advance to the next segment */
687 if (cmd->SCp.buffers_residual--) {
688 cmd->SCp.buffer++;
689 cmd->SCp.this_residual = cmd->SCp.buffer->length;
690 cmd->SCp.ptr = cmd->SCp.buffer->address;
693 /* Now check to see if the drive is ready to comunicate */
694 r = (r_str(ppb) & 0xf0);
695 /* If not, drop back down to the scheduler and wait a timer tick */
696 if (!(r & 0x80))
697 return 0;
699 return 1; /* FINISH_RETURN */
702 /* deprecated synchronous interface */
703 int ppa_command(Scsi_Cmnd * cmd)
705 static int first_pass = 1;
706 int host_no = cmd->host->unique_id;
708 if (first_pass) {
709 printk("ppa: using non-queuing interface\n");
710 first_pass = 0;
712 if (ppa_hosts[host_no].cur_cmd) {
713 printk("PPA: bug in ppa_command\n");
714 return 0;
716 ppa_hosts[host_no].failed = 0;
717 ppa_hosts[host_no].jstart = jiffies;
718 ppa_hosts[host_no].cur_cmd = cmd;
719 cmd->result = DID_ERROR << 16; /* default return code */
720 cmd->SCp.phase = 0;
722 ppa_pb_claim(host_no);
724 while (ppa_engine(&ppa_hosts[host_no], cmd))
725 schedule();
727 if (cmd->SCp.phase) /* Only disconnect if we have connected */
728 ppa_disconnect(cmd->host->unique_id);
730 ppa_pb_release(host_no);
731 ppa_hosts[host_no].cur_cmd = 0;
732 return cmd->result;
736 * Since the PPA itself doesn't generate interrupts, we use
737 * the scheduler's task queue to generate a stream of call-backs and
738 * complete the request when the drive is ready.
740 static void ppa_interrupt(void *data)
742 ppa_struct *tmp = (ppa_struct *) data;
743 Scsi_Cmnd *cmd = tmp->cur_cmd;
744 unsigned long flags;
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;
797 spin_lock_irqsave(&io_request_lock, flags);
798 cmd->scsi_done(cmd);
799 spin_unlock_irqrestore(&io_request_lock, flags);
800 return;
803 static int ppa_engine(ppa_struct * tmp, Scsi_Cmnd * cmd)
805 int host_no = cmd->host->unique_id;
806 unsigned short ppb = PPA_BASE(host_no);
807 unsigned char l = 0, h = 0;
808 int retv;
810 /* First check for any errors that may of occurred
811 * Here we check for internal errors
813 if (tmp->failed)
814 return 0;
816 switch (cmd->SCp.phase) {
817 case 0: /* Phase 0 - Waiting for parport */
818 if ((jiffies - tmp->jstart) > HZ) {
820 * We waited more than a second
821 * for parport to call us
823 ppa_fail(host_no, DID_BUS_BUSY);
824 return 0;
826 return 1; /* wait until ppa_wakeup claims parport */
827 case 1: /* Phase 1 - Connected */
828 { /* Perform a sanity check for cable unplugged */
829 int retv = 2; /* Failed */
831 ppa_connect(host_no, CONNECT_EPP_MAYBE);
833 w_ctr(ppb, 0xe);
834 if ((r_str(ppb) & 0x08) == 0x08)
835 retv--;
837 w_ctr(ppb, 0xc);
838 if ((r_str(ppb) & 0x08) == 0x00)
839 retv--;
841 if (retv) {
842 if ((jiffies - tmp->jstart) > (1 * HZ)) {
843 printk("ppa: Parallel port cable is unplugged!!\n");
844 ppa_fail(host_no, DID_BUS_BUSY);
845 return 0;
846 } else {
847 ppa_disconnect(host_no);
848 return 1; /* Try again in a jiffy */
851 cmd->SCp.phase++;
854 case 2: /* Phase 2 - We are now talking to the scsi bus */
855 if (!ppa_select(host_no, cmd->target)) {
856 ppa_fail(host_no, DID_NO_CONNECT);
857 return 0;
859 cmd->SCp.phase++;
861 case 3: /* Phase 3 - Ready to accept a command */
862 w_ctr(ppb, 0x0c);
863 if (!(r_str(ppb) & 0x80))
864 return 1;
866 if (!ppa_send_command(cmd))
867 return 0;
868 cmd->SCp.phase++;
870 case 4: /* Phase 4 - Setup scatter/gather buffers */
871 if (cmd->use_sg) {
872 /* if many buffers are available, start filling the first */
873 cmd->SCp.buffer = (struct scatterlist *) cmd->request_buffer;
874 cmd->SCp.this_residual = cmd->SCp.buffer->length;
875 cmd->SCp.ptr = cmd->SCp.buffer->address;
876 } else {
877 /* else fill the only available buffer */
878 cmd->SCp.buffer = NULL;
879 cmd->SCp.this_residual = cmd->request_bufflen;
880 cmd->SCp.ptr = cmd->request_buffer;
882 cmd->SCp.buffers_residual = cmd->use_sg;
883 cmd->SCp.phase++;
885 case 5: /* Phase 5 - Data transfer stage */
886 w_ctr(ppb, 0x0c);
887 if (!(r_str(ppb) & 0x80))
888 return 1;
890 retv = ppa_completion(cmd);
891 if (retv == -1)
892 return 0;
893 if (retv == 0)
894 return 1;
895 cmd->SCp.phase++;
897 case 6: /* Phase 6 - Read status/message */
898 cmd->result = DID_OK << 16;
899 /* Check for data overrun */
900 if (ppa_wait(host_no) != (unsigned char) 0xf0) {
901 ppa_fail(host_no, DID_ERROR);
902 return 0;
904 if (ppa_in(host_no, &l, 1)) { /* read status byte */
905 /* Check for optional message byte */
906 if (ppa_wait(host_no) == (unsigned char) 0xf0)
907 ppa_in(host_no, &h, 1);
908 cmd->result = (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
910 return 0; /* Finished */
911 break;
913 default:
914 printk("ppa: Invalid scsi phase\n");
916 return 0;
919 int ppa_queuecommand(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
921 int host_no = cmd->host->unique_id;
923 if (ppa_hosts[host_no].cur_cmd) {
924 printk("PPA: bug in ppa_queuecommand\n");
925 return 0;
927 ppa_hosts[host_no].failed = 0;
928 ppa_hosts[host_no].jstart = jiffies;
929 ppa_hosts[host_no].cur_cmd = cmd;
930 cmd->scsi_done = done;
931 cmd->result = DID_ERROR << 16; /* default return code */
932 cmd->SCp.phase = 0; /* bus free */
934 ppa_pb_claim(host_no);
936 ppa_hosts[host_no].ppa_tq.data = ppa_hosts + host_no;
937 ppa_hosts[host_no].ppa_tq.sync = 0;
938 queue_task(&ppa_hosts[host_no].ppa_tq, &tq_immediate);
939 mark_bh(IMMEDIATE_BH);
941 return 0;
945 * Apparently the the disk->capacity attribute is off by 1 sector
946 * for all disk drives. We add the one here, but it should really
947 * be done in sd.c. Even if it gets fixed there, this will still
948 * work.
950 int ppa_biosparam(Disk * disk, kdev_t dev, int ip[])
952 ip[0] = 0x40;
953 ip[1] = 0x20;
954 ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
955 if (ip[2] > 1024) {
956 ip[0] = 0xff;
957 ip[1] = 0x3f;
958 ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
959 if (ip[2] > 1023)
960 ip[2] = 1023;
962 return 0;
965 int ppa_abort(Scsi_Cmnd * cmd)
967 int host_no = cmd->host->unique_id;
969 * There is no method for aborting commands since Iomega
970 * have tied the SCSI_MESSAGE line high in the interface
973 switch (cmd->SCp.phase) {
974 case 0: /* Do not have access to parport */
975 case 1: /* Have not connected to interface */
976 ppa_hosts[host_no].cur_cmd = NULL; /* Forget the problem */
977 return SUCCESS;
978 break;
979 default: /* SCSI command sent, can not abort */
980 return FAILED;
981 break;
985 static void ppa_reset_pulse(unsigned int base)
987 w_dtr(base, 0x40);
988 w_ctr(base, 0x8);
989 udelay(30);
990 w_ctr(base, 0xc);
993 int ppa_reset(Scsi_Cmnd * cmd)
995 int host_no = cmd->host->unique_id;
997 if (cmd->SCp.phase)
998 ppa_disconnect(host_no);
999 ppa_hosts[host_no].cur_cmd = NULL; /* Forget the problem */
1001 ppa_connect(host_no, CONNECT_NORMAL);
1002 ppa_reset_pulse(PPA_BASE(host_no));
1003 udelay(1000); /* device settle delay */
1004 ppa_disconnect(host_no);
1005 udelay(1000); /* device settle delay */
1006 return SUCCESS;
1009 static int device_check(int host_no)
1011 /* This routine looks for a device and then attempts to use EPP
1012 to send a command. If all goes as planned then EPP is available. */
1014 static char cmd[6] =
1015 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1016 int loop, old_mode, status, k, ppb = PPA_BASE(host_no);
1017 unsigned char l;
1019 old_mode = ppa_hosts[host_no].mode;
1020 for (loop = 0; loop < 8; loop++) {
1021 /* Attempt to use EPP for Test Unit Ready */
1022 if ((ppb & 0x0007) == 0x0000)
1023 ppa_hosts[host_no].mode = PPA_EPP_32;
1025 second_pass:
1026 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1027 /* Select SCSI device */
1028 if (!ppa_select(host_no, loop)) {
1029 ppa_disconnect(host_no);
1030 continue;
1032 printk("ppa: Found device at ID %i, Attempting to use %s\n", loop,
1033 PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1035 /* Send SCSI command */
1036 status = 1;
1037 w_ctr(ppb, 0x0c);
1038 for (l = 0; (l < 6) && (status); l++)
1039 status = ppa_out(host_no, cmd, 1);
1041 if (!status) {
1042 ppa_disconnect(host_no);
1043 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1044 w_dtr(ppb, 0x40);
1045 w_ctr(ppb, 0x08);
1046 udelay(30);
1047 w_ctr(ppb, 0x0c);
1048 udelay(1000);
1049 ppa_disconnect(host_no);
1050 udelay(1000);
1051 if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1052 ppa_hosts[host_no].mode = old_mode;
1053 goto second_pass;
1055 printk("ppa: Unable to establish communication, aborting driver load.\n");
1056 return 1;
1058 w_ctr(ppb, 0x0c);
1059 k = 1000000; /* 1 Second */
1060 do {
1061 l = r_str(ppb);
1062 k--;
1063 udelay(1);
1064 } while (!(l & 0x80) && (k));
1066 l &= 0xf0;
1068 if (l != 0xf0) {
1069 ppa_disconnect(host_no);
1070 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1071 ppa_reset_pulse(ppb);
1072 udelay(1000);
1073 ppa_disconnect(host_no);
1074 udelay(1000);
1075 if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1076 ppa_hosts[host_no].mode = old_mode;
1077 goto second_pass;
1079 printk("ppa: Unable to establish communication, aborting driver load.\n");
1080 return 1;
1082 ppa_disconnect(host_no);
1083 printk("ppa: Communication established with ID %i using %s\n", loop,
1084 PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1085 ppa_connect(host_no, CONNECT_EPP_MAYBE);
1086 ppa_reset_pulse(ppb);
1087 udelay(1000);
1088 ppa_disconnect(host_no);
1089 udelay(1000);
1090 return 0;
1092 printk("ppa: No devices found, aborting driver load.\n");
1093 return 1;