[PATCH] gfp_t: dma-mapping (ppc)
[linux-2.6/mini2440.git] / drivers / scsi / ppa.c
blobfafcf5d185e761d43b0fe949be06e2d4f5a3898f
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 General Public License.
8 *
9 * Current Maintainer: David Campbell (Perth, Western Australia, GMT+0800)
10 * campbell@torque.net
13 #include <linux/config.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/parport.h>
19 #include <linux/workqueue.h>
20 #include <linux/delay.h>
21 #include <asm/io.h>
23 #include <scsi/scsi.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_host.h>
29 static void ppa_reset_pulse(unsigned int base);
31 typedef struct {
32 struct pardevice *dev; /* Parport device entry */
33 int base; /* Actual port address */
34 int mode; /* Transfer mode */
35 struct scsi_cmnd *cur_cmd; /* Current queued command */
36 struct work_struct ppa_tq; /* Polling interrupt stuff */
37 unsigned long jstart; /* Jiffies at start */
38 unsigned long recon_tmo; /* How many usecs to wait for reconnection (6th bit) */
39 unsigned int failed:1; /* Failure flag */
40 unsigned wanted:1; /* Parport sharing busy flag */
41 wait_queue_head_t *waiting;
42 struct Scsi_Host *host;
43 struct list_head list;
44 } ppa_struct;
46 #include "ppa.h"
48 static inline ppa_struct *ppa_dev(struct Scsi_Host *host)
50 return *(ppa_struct **)&host->hostdata;
53 static DEFINE_SPINLOCK(arbitration_lock);
55 static void got_it(ppa_struct *dev)
57 dev->base = dev->dev->port->base;
58 if (dev->cur_cmd)
59 dev->cur_cmd->SCp.phase = 1;
60 else
61 wake_up(dev->waiting);
64 static void ppa_wakeup(void *ref)
66 ppa_struct *dev = (ppa_struct *) ref;
67 unsigned long flags;
69 spin_lock_irqsave(&arbitration_lock, flags);
70 if (dev->wanted) {
71 parport_claim(dev->dev);
72 got_it(dev);
73 dev->wanted = 0;
75 spin_unlock_irqrestore(&arbitration_lock, flags);
76 return;
79 static int ppa_pb_claim(ppa_struct *dev)
81 unsigned long flags;
82 int res = 1;
83 spin_lock_irqsave(&arbitration_lock, flags);
84 if (parport_claim(dev->dev) == 0) {
85 got_it(dev);
86 res = 0;
88 dev->wanted = res;
89 spin_unlock_irqrestore(&arbitration_lock, flags);
90 return res;
93 static void ppa_pb_dismiss(ppa_struct *dev)
95 unsigned long flags;
96 int wanted;
97 spin_lock_irqsave(&arbitration_lock, flags);
98 wanted = dev->wanted;
99 dev->wanted = 0;
100 spin_unlock_irqrestore(&arbitration_lock, flags);
101 if (!wanted)
102 parport_release(dev->dev);
105 static inline void ppa_pb_release(ppa_struct *dev)
107 parport_release(dev->dev);
111 * Start of Chipset kludges
114 /* This is to give the ppa driver a way to modify the timings (and other
115 * parameters) by writing to the /proc/scsi/ppa/0 file.
116 * Very simple method really... (To simple, no error checking :( )
117 * Reason: Kernel hackers HATE having to unload and reload modules for
118 * testing...
119 * Also gives a method to use a script to obtain optimum timings (TODO)
122 static inline int ppa_proc_write(ppa_struct *dev, char *buffer, int length)
124 unsigned long x;
126 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
127 x = simple_strtoul(buffer + 5, NULL, 0);
128 dev->mode = x;
129 return length;
131 if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
132 x = simple_strtoul(buffer + 10, NULL, 0);
133 dev->recon_tmo = x;
134 printk("ppa: recon_tmo set to %ld\n", x);
135 return length;
137 printk("ppa /proc: invalid variable\n");
138 return (-EINVAL);
141 static int ppa_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, int inout)
143 int len = 0;
144 ppa_struct *dev = ppa_dev(host);
146 if (inout)
147 return ppa_proc_write(dev, buffer, length);
149 len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
150 len +=
151 sprintf(buffer + len, "Parport : %s\n",
152 dev->dev->port->name);
153 len +=
154 sprintf(buffer + len, "Mode : %s\n",
155 PPA_MODE_STRING[dev->mode]);
156 #if PPA_DEBUG > 0
157 len +=
158 sprintf(buffer + len, "recon_tmo : %lu\n", dev->recon_tmo);
159 #endif
161 /* Request for beyond end of buffer */
162 if (offset > length)
163 return 0;
165 *start = buffer + offset;
166 len -= offset;
167 if (len > length)
168 len = length;
169 return len;
172 static int device_check(ppa_struct *dev);
174 #if PPA_DEBUG > 0
175 #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
176 y, __FUNCTION__, __LINE__); ppa_fail_func(x,y);
177 static inline void ppa_fail_func(ppa_struct *dev, int error_code)
178 #else
179 static inline void ppa_fail(ppa_struct *dev, int error_code)
180 #endif
182 /* If we fail a device then we trash status / message bytes */
183 if (dev->cur_cmd) {
184 dev->cur_cmd->result = error_code << 16;
185 dev->failed = 1;
190 * Wait for the high bit to be set.
192 * In principle, this could be tied to an interrupt, but the adapter
193 * doesn't appear to be designed to support interrupts. We spin on
194 * the 0x80 ready bit.
196 static unsigned char ppa_wait(ppa_struct *dev)
198 int k;
199 unsigned short ppb = dev->base;
200 unsigned char r;
202 k = PPA_SPIN_TMO;
203 /* Wait for bit 6 and 7 - PJC */
204 for (r = r_str(ppb); ((r & 0xc0) != 0xc0) && (k); k--) {
205 udelay(1);
206 r = r_str(ppb);
210 * return some status information.
211 * Semantics: 0xc0 = ZIP wants more data
212 * 0xd0 = ZIP wants to send more data
213 * 0xe0 = ZIP is expecting SCSI command data
214 * 0xf0 = end of transfer, ZIP is sending status
216 if (k)
217 return (r & 0xf0);
219 /* Counter expired - Time out occurred */
220 ppa_fail(dev, DID_TIME_OUT);
221 printk("ppa timeout in ppa_wait\n");
222 return 0; /* command timed out */
226 * Clear EPP Timeout Bit
228 static inline void epp_reset(unsigned short ppb)
230 int i;
232 i = r_str(ppb);
233 w_str(ppb, i);
234 w_str(ppb, i & 0xfe);
238 * Wait for empty ECP fifo (if we are in ECP fifo mode only)
240 static inline void ecp_sync(ppa_struct *dev)
242 int i, ppb_hi = dev->dev->port->base_hi;
244 if (ppb_hi == 0)
245 return;
247 if ((r_ecr(ppb_hi) & 0xe0) == 0x60) { /* mode 011 == ECP fifo mode */
248 for (i = 0; i < 100; i++) {
249 if (r_ecr(ppb_hi) & 0x01)
250 return;
251 udelay(5);
253 printk("ppa: ECP sync failed as data still present in FIFO.\n");
257 static int ppa_byte_out(unsigned short base, const char *buffer, int len)
259 int i;
261 for (i = len; i; i--) {
262 w_dtr(base, *buffer++);
263 w_ctr(base, 0xe);
264 w_ctr(base, 0xc);
266 return 1; /* All went well - we hope! */
269 static int ppa_byte_in(unsigned short base, char *buffer, int len)
271 int i;
273 for (i = len; i; i--) {
274 *buffer++ = r_dtr(base);
275 w_ctr(base, 0x27);
276 w_ctr(base, 0x25);
278 return 1; /* All went well - we hope! */
281 static int ppa_nibble_in(unsigned short base, char *buffer, int len)
283 for (; len; len--) {
284 unsigned char h;
286 w_ctr(base, 0x4);
287 h = r_str(base) & 0xf0;
288 w_ctr(base, 0x6);
289 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
291 return 1; /* All went well - we hope! */
294 static int ppa_out(ppa_struct *dev, char *buffer, int len)
296 int r;
297 unsigned short ppb = dev->base;
299 r = ppa_wait(dev);
301 if ((r & 0x50) != 0x40) {
302 ppa_fail(dev, DID_ERROR);
303 return 0;
305 switch (dev->mode) {
306 case PPA_NIBBLE:
307 case PPA_PS2:
308 /* 8 bit output, with a loop */
309 r = ppa_byte_out(ppb, buffer, len);
310 break;
312 case PPA_EPP_32:
313 case PPA_EPP_16:
314 case PPA_EPP_8:
315 epp_reset(ppb);
316 w_ctr(ppb, 0x4);
317 #ifdef CONFIG_SCSI_IZIP_EPP16
318 if (!(((long) buffer | len) & 0x01))
319 outsw(ppb + 4, buffer, len >> 1);
320 #else
321 if (!(((long) buffer | len) & 0x03))
322 outsl(ppb + 4, buffer, len >> 2);
323 #endif
324 else
325 outsb(ppb + 4, buffer, len);
326 w_ctr(ppb, 0xc);
327 r = !(r_str(ppb) & 0x01);
328 w_ctr(ppb, 0xc);
329 ecp_sync(dev);
330 break;
332 default:
333 printk("PPA: bug in ppa_out()\n");
334 r = 0;
336 return r;
339 static int ppa_in(ppa_struct *dev, char *buffer, int len)
341 int r;
342 unsigned short ppb = dev->base;
344 r = ppa_wait(dev);
346 if ((r & 0x50) != 0x50) {
347 ppa_fail(dev, DID_ERROR);
348 return 0;
350 switch (dev->mode) {
351 case PPA_NIBBLE:
352 /* 4 bit input, with a loop */
353 r = ppa_nibble_in(ppb, buffer, len);
354 w_ctr(ppb, 0xc);
355 break;
357 case PPA_PS2:
358 /* 8 bit input, with a loop */
359 w_ctr(ppb, 0x25);
360 r = ppa_byte_in(ppb, buffer, len);
361 w_ctr(ppb, 0x4);
362 w_ctr(ppb, 0xc);
363 break;
365 case PPA_EPP_32:
366 case PPA_EPP_16:
367 case PPA_EPP_8:
368 epp_reset(ppb);
369 w_ctr(ppb, 0x24);
370 #ifdef CONFIG_SCSI_IZIP_EPP16
371 if (!(((long) buffer | len) & 0x01))
372 insw(ppb + 4, buffer, len >> 1);
373 #else
374 if (!(((long) buffer | len) & 0x03))
375 insl(ppb + 4, buffer, len >> 2);
376 #endif
377 else
378 insb(ppb + 4, buffer, len);
379 w_ctr(ppb, 0x2c);
380 r = !(r_str(ppb) & 0x01);
381 w_ctr(ppb, 0x2c);
382 ecp_sync(dev);
383 break;
385 default:
386 printk("PPA: bug in ppa_ins()\n");
387 r = 0;
388 break;
390 return r;
393 /* end of ppa_io.h */
394 static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
396 w_dtr(ppb, b);
397 w_ctr(ppb, 0xc);
398 w_ctr(ppb, 0xe);
399 w_ctr(ppb, 0xc);
400 w_ctr(ppb, 0x4);
401 w_ctr(ppb, 0xc);
404 static void ppa_disconnect(ppa_struct *dev)
406 unsigned short ppb = dev->base;
408 ppa_d_pulse(ppb, 0);
409 ppa_d_pulse(ppb, 0x3c);
410 ppa_d_pulse(ppb, 0x20);
411 ppa_d_pulse(ppb, 0xf);
414 static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
416 w_dtr(ppb, b);
417 w_ctr(ppb, 0x4);
418 w_ctr(ppb, 0x6);
419 w_ctr(ppb, 0x4);
420 w_ctr(ppb, 0xc);
423 static inline void ppa_connect(ppa_struct *dev, int flag)
425 unsigned short ppb = dev->base;
427 ppa_c_pulse(ppb, 0);
428 ppa_c_pulse(ppb, 0x3c);
429 ppa_c_pulse(ppb, 0x20);
430 if ((flag == CONNECT_EPP_MAYBE) && IN_EPP_MODE(dev->mode))
431 ppa_c_pulse(ppb, 0xcf);
432 else
433 ppa_c_pulse(ppb, 0x8f);
436 static int ppa_select(ppa_struct *dev, int target)
438 int k;
439 unsigned short ppb = dev->base;
442 * Bit 6 (0x40) is the device selected bit.
443 * First we must wait till the current device goes off line...
445 k = PPA_SELECT_TMO;
446 do {
447 k--;
448 udelay(1);
449 } while ((r_str(ppb) & 0x40) && (k));
450 if (!k)
451 return 0;
453 w_dtr(ppb, (1 << target));
454 w_ctr(ppb, 0xe);
455 w_ctr(ppb, 0xc);
456 w_dtr(ppb, 0x80); /* This is NOT the initator */
457 w_ctr(ppb, 0x8);
459 k = PPA_SELECT_TMO;
460 do {
461 k--;
462 udelay(1);
464 while (!(r_str(ppb) & 0x40) && (k));
465 if (!k)
466 return 0;
468 return 1;
472 * This is based on a trace of what the Iomega DOS 'guest' driver does.
473 * I've tried several different kinds of parallel ports with guest and
474 * coded this to react in the same ways that it does.
476 * The return value from this function is just a hint about where the
477 * handshaking failed.
480 static int ppa_init(ppa_struct *dev)
482 int retv;
483 unsigned short ppb = dev->base;
485 ppa_disconnect(dev);
486 ppa_connect(dev, CONNECT_NORMAL);
488 retv = 2; /* Failed */
490 w_ctr(ppb, 0xe);
491 if ((r_str(ppb) & 0x08) == 0x08)
492 retv--;
494 w_ctr(ppb, 0xc);
495 if ((r_str(ppb) & 0x08) == 0x00)
496 retv--;
498 if (!retv)
499 ppa_reset_pulse(ppb);
500 udelay(1000); /* Allow devices to settle down */
501 ppa_disconnect(dev);
502 udelay(1000); /* Another delay to allow devices to settle */
504 if (retv)
505 return -EIO;
507 return device_check(dev);
510 static inline int ppa_send_command(struct scsi_cmnd *cmd)
512 ppa_struct *dev = ppa_dev(cmd->device->host);
513 int k;
515 w_ctr(dev->base, 0x0c);
517 for (k = 0; k < cmd->cmd_len; k++)
518 if (!ppa_out(dev, &cmd->cmnd[k], 1))
519 return 0;
520 return 1;
524 * The bulk flag enables some optimisations in the data transfer loops,
525 * it should be true for any command that transfers data in integral
526 * numbers of sectors.
528 * The driver appears to remain stable if we speed up the parallel port
529 * i/o in this function, but not elsewhere.
531 static int ppa_completion(struct scsi_cmnd *cmd)
533 /* Return codes:
534 * -1 Error
535 * 0 Told to schedule
536 * 1 Finished data transfer
538 ppa_struct *dev = ppa_dev(cmd->device->host);
539 unsigned short ppb = dev->base;
540 unsigned long start_jiffies = jiffies;
542 unsigned char r, v;
543 int fast, bulk, status;
545 v = cmd->cmnd[0];
546 bulk = ((v == READ_6) ||
547 (v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
550 * We only get here if the drive is ready to comunicate,
551 * hence no need for a full ppa_wait.
553 r = (r_str(ppb) & 0xf0);
555 while (r != (unsigned char) 0xf0) {
557 * If we have been running for more than a full timer tick
558 * then take a rest.
560 if (time_after(jiffies, start_jiffies + 1))
561 return 0;
563 if ((cmd->SCp.this_residual <= 0)) {
564 ppa_fail(dev, DID_ERROR);
565 return -1; /* ERROR_RETURN */
568 /* On some hardware we have SCSI disconnected (6th bit low)
569 * for about 100usecs. It is too expensive to wait a
570 * tick on every loop so we busy wait for no more than
571 * 500usecs to give the drive a chance first. We do not
572 * change things for "normal" hardware since generally
573 * the 6th bit is always high.
574 * This makes the CPU load higher on some hardware
575 * but otherwise we can not get more than 50K/secs
576 * on this problem hardware.
578 if ((r & 0xc0) != 0xc0) {
579 /* Wait for reconnection should be no more than
580 * jiffy/2 = 5ms = 5000 loops
582 unsigned long k = dev->recon_tmo;
583 for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
584 k--)
585 udelay(1);
587 if (!k)
588 return 0;
591 /* determine if we should use burst I/O */
592 fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
593 ? PPA_BURST_SIZE : 1;
595 if (r == (unsigned char) 0xc0)
596 status = ppa_out(dev, cmd->SCp.ptr, fast);
597 else
598 status = ppa_in(dev, cmd->SCp.ptr, fast);
600 cmd->SCp.ptr += fast;
601 cmd->SCp.this_residual -= fast;
603 if (!status) {
604 ppa_fail(dev, DID_BUS_BUSY);
605 return -1; /* ERROR_RETURN */
607 if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
608 /* if scatter/gather, advance to the next segment */
609 if (cmd->SCp.buffers_residual--) {
610 cmd->SCp.buffer++;
611 cmd->SCp.this_residual =
612 cmd->SCp.buffer->length;
613 cmd->SCp.ptr =
614 page_address(cmd->SCp.buffer->page) +
615 cmd->SCp.buffer->offset;
618 /* Now check to see if the drive is ready to comunicate */
619 r = (r_str(ppb) & 0xf0);
620 /* If not, drop back down to the scheduler and wait a timer tick */
621 if (!(r & 0x80))
622 return 0;
624 return 1; /* FINISH_RETURN */
628 * Since the PPA itself doesn't generate interrupts, we use
629 * the scheduler's task queue to generate a stream of call-backs and
630 * complete the request when the drive is ready.
632 static void ppa_interrupt(void *data)
634 ppa_struct *dev = (ppa_struct *) data;
635 struct scsi_cmnd *cmd = dev->cur_cmd;
637 if (!cmd) {
638 printk("PPA: bug in ppa_interrupt\n");
639 return;
641 if (ppa_engine(dev, cmd)) {
642 dev->ppa_tq.data = (void *) dev;
643 schedule_delayed_work(&dev->ppa_tq, 1);
644 return;
646 /* Command must of completed hence it is safe to let go... */
647 #if PPA_DEBUG > 0
648 switch ((cmd->result >> 16) & 0xff) {
649 case DID_OK:
650 break;
651 case DID_NO_CONNECT:
652 printk("ppa: no device at SCSI ID %i\n", cmd->device->target);
653 break;
654 case DID_BUS_BUSY:
655 printk("ppa: BUS BUSY - EPP timeout detected\n");
656 break;
657 case DID_TIME_OUT:
658 printk("ppa: unknown timeout\n");
659 break;
660 case DID_ABORT:
661 printk("ppa: told to abort\n");
662 break;
663 case DID_PARITY:
664 printk("ppa: parity error (???)\n");
665 break;
666 case DID_ERROR:
667 printk("ppa: internal driver error\n");
668 break;
669 case DID_RESET:
670 printk("ppa: told to reset device\n");
671 break;
672 case DID_BAD_INTR:
673 printk("ppa: bad interrupt (???)\n");
674 break;
675 default:
676 printk("ppa: bad return code (%02x)\n",
677 (cmd->result >> 16) & 0xff);
679 #endif
681 if (cmd->SCp.phase > 1)
682 ppa_disconnect(dev);
684 ppa_pb_dismiss(dev);
686 dev->cur_cmd = NULL;
688 cmd->scsi_done(cmd);
691 static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
693 unsigned short ppb = dev->base;
694 unsigned char l = 0, h = 0;
695 int retv;
697 /* First check for any errors that may of occurred
698 * Here we check for internal errors
700 if (dev->failed)
701 return 0;
703 switch (cmd->SCp.phase) {
704 case 0: /* Phase 0 - Waiting for parport */
705 if (time_after(jiffies, dev->jstart + HZ)) {
707 * We waited more than a second
708 * for parport to call us
710 ppa_fail(dev, DID_BUS_BUSY);
711 return 0;
713 return 1; /* wait until ppa_wakeup claims parport */
714 case 1: /* Phase 1 - Connected */
715 { /* Perform a sanity check for cable unplugged */
716 int retv = 2; /* Failed */
718 ppa_connect(dev, CONNECT_EPP_MAYBE);
720 w_ctr(ppb, 0xe);
721 if ((r_str(ppb) & 0x08) == 0x08)
722 retv--;
724 w_ctr(ppb, 0xc);
725 if ((r_str(ppb) & 0x08) == 0x00)
726 retv--;
728 if (retv) {
729 if ((jiffies - dev->jstart) > (1 * HZ)) {
730 printk
731 ("ppa: Parallel port cable is unplugged!!\n");
732 ppa_fail(dev, DID_BUS_BUSY);
733 return 0;
734 } else {
735 ppa_disconnect(dev);
736 return 1; /* Try again in a jiffy */
739 cmd->SCp.phase++;
742 case 2: /* Phase 2 - We are now talking to the scsi bus */
743 if (!ppa_select(dev, cmd->device->id)) {
744 ppa_fail(dev, DID_NO_CONNECT);
745 return 0;
747 cmd->SCp.phase++;
749 case 3: /* Phase 3 - Ready to accept a command */
750 w_ctr(ppb, 0x0c);
751 if (!(r_str(ppb) & 0x80))
752 return 1;
754 if (!ppa_send_command(cmd))
755 return 0;
756 cmd->SCp.phase++;
758 case 4: /* Phase 4 - Setup scatter/gather buffers */
759 if (cmd->use_sg) {
760 /* if many buffers are available, start filling the first */
761 cmd->SCp.buffer =
762 (struct scatterlist *) cmd->request_buffer;
763 cmd->SCp.this_residual = cmd->SCp.buffer->length;
764 cmd->SCp.ptr =
765 page_address(cmd->SCp.buffer->page) +
766 cmd->SCp.buffer->offset;
767 } else {
768 /* else fill the only available buffer */
769 cmd->SCp.buffer = NULL;
770 cmd->SCp.this_residual = cmd->request_bufflen;
771 cmd->SCp.ptr = cmd->request_buffer;
773 cmd->SCp.buffers_residual = cmd->use_sg - 1;
774 cmd->SCp.phase++;
776 case 5: /* Phase 5 - Data transfer stage */
777 w_ctr(ppb, 0x0c);
778 if (!(r_str(ppb) & 0x80))
779 return 1;
781 retv = ppa_completion(cmd);
782 if (retv == -1)
783 return 0;
784 if (retv == 0)
785 return 1;
786 cmd->SCp.phase++;
788 case 6: /* Phase 6 - Read status/message */
789 cmd->result = DID_OK << 16;
790 /* Check for data overrun */
791 if (ppa_wait(dev) != (unsigned char) 0xf0) {
792 ppa_fail(dev, DID_ERROR);
793 return 0;
795 if (ppa_in(dev, &l, 1)) { /* read status byte */
796 /* Check for optional message byte */
797 if (ppa_wait(dev) == (unsigned char) 0xf0)
798 ppa_in(dev, &h, 1);
799 cmd->result =
800 (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
802 return 0; /* Finished */
803 break;
805 default:
806 printk("ppa: Invalid scsi phase\n");
808 return 0;
811 static int ppa_queuecommand(struct scsi_cmnd *cmd,
812 void (*done) (struct scsi_cmnd *))
814 ppa_struct *dev = ppa_dev(cmd->device->host);
816 if (dev->cur_cmd) {
817 printk("PPA: bug in ppa_queuecommand\n");
818 return 0;
820 dev->failed = 0;
821 dev->jstart = jiffies;
822 dev->cur_cmd = cmd;
823 cmd->scsi_done = done;
824 cmd->result = DID_ERROR << 16; /* default return code */
825 cmd->SCp.phase = 0; /* bus free */
827 dev->ppa_tq.data = dev;
828 schedule_work(&dev->ppa_tq);
830 ppa_pb_claim(dev);
832 return 0;
836 * Apparently the disk->capacity attribute is off by 1 sector
837 * for all disk drives. We add the one here, but it should really
838 * be done in sd.c. Even if it gets fixed there, this will still
839 * work.
841 static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
842 sector_t capacity, int ip[])
844 ip[0] = 0x40;
845 ip[1] = 0x20;
846 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
847 if (ip[2] > 1024) {
848 ip[0] = 0xff;
849 ip[1] = 0x3f;
850 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
851 if (ip[2] > 1023)
852 ip[2] = 1023;
854 return 0;
857 static int ppa_abort(struct scsi_cmnd *cmd)
859 ppa_struct *dev = ppa_dev(cmd->device->host);
861 * There is no method for aborting commands since Iomega
862 * have tied the SCSI_MESSAGE line high in the interface
865 switch (cmd->SCp.phase) {
866 case 0: /* Do not have access to parport */
867 case 1: /* Have not connected to interface */
868 dev->cur_cmd = NULL; /* Forget the problem */
869 return SUCCESS;
870 break;
871 default: /* SCSI command sent, can not abort */
872 return FAILED;
873 break;
877 static void ppa_reset_pulse(unsigned int base)
879 w_dtr(base, 0x40);
880 w_ctr(base, 0x8);
881 udelay(30);
882 w_ctr(base, 0xc);
885 static int ppa_reset(struct scsi_cmnd *cmd)
887 ppa_struct *dev = ppa_dev(cmd->device->host);
889 if (cmd->SCp.phase)
890 ppa_disconnect(dev);
891 dev->cur_cmd = NULL; /* Forget the problem */
893 ppa_connect(dev, CONNECT_NORMAL);
894 ppa_reset_pulse(dev->base);
895 mdelay(1); /* device settle delay */
896 ppa_disconnect(dev);
897 mdelay(1); /* device settle delay */
898 return SUCCESS;
901 static int device_check(ppa_struct *dev)
903 /* This routine looks for a device and then attempts to use EPP
904 to send a command. If all goes as planned then EPP is available. */
906 static char cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
907 int loop, old_mode, status, k, ppb = dev->base;
908 unsigned char l;
910 old_mode = dev->mode;
911 for (loop = 0; loop < 8; loop++) {
912 /* Attempt to use EPP for Test Unit Ready */
913 if ((ppb & 0x0007) == 0x0000)
914 dev->mode = PPA_EPP_32;
916 second_pass:
917 ppa_connect(dev, CONNECT_EPP_MAYBE);
918 /* Select SCSI device */
919 if (!ppa_select(dev, loop)) {
920 ppa_disconnect(dev);
921 continue;
923 printk("ppa: Found device at ID %i, Attempting to use %s\n",
924 loop, PPA_MODE_STRING[dev->mode]);
926 /* Send SCSI command */
927 status = 1;
928 w_ctr(ppb, 0x0c);
929 for (l = 0; (l < 6) && (status); l++)
930 status = ppa_out(dev, cmd, 1);
932 if (!status) {
933 ppa_disconnect(dev);
934 ppa_connect(dev, CONNECT_EPP_MAYBE);
935 w_dtr(ppb, 0x40);
936 w_ctr(ppb, 0x08);
937 udelay(30);
938 w_ctr(ppb, 0x0c);
939 udelay(1000);
940 ppa_disconnect(dev);
941 udelay(1000);
942 if (dev->mode == PPA_EPP_32) {
943 dev->mode = old_mode;
944 goto second_pass;
946 return -EIO;
948 w_ctr(ppb, 0x0c);
949 k = 1000000; /* 1 Second */
950 do {
951 l = r_str(ppb);
952 k--;
953 udelay(1);
954 } while (!(l & 0x80) && (k));
956 l &= 0xf0;
958 if (l != 0xf0) {
959 ppa_disconnect(dev);
960 ppa_connect(dev, CONNECT_EPP_MAYBE);
961 ppa_reset_pulse(ppb);
962 udelay(1000);
963 ppa_disconnect(dev);
964 udelay(1000);
965 if (dev->mode == PPA_EPP_32) {
966 dev->mode = old_mode;
967 goto second_pass;
969 return -EIO;
971 ppa_disconnect(dev);
972 printk("ppa: Communication established with ID %i using %s\n",
973 loop, PPA_MODE_STRING[dev->mode]);
974 ppa_connect(dev, CONNECT_EPP_MAYBE);
975 ppa_reset_pulse(ppb);
976 udelay(1000);
977 ppa_disconnect(dev);
978 udelay(1000);
979 return 0;
981 return -ENODEV;
984 static struct scsi_host_template ppa_template = {
985 .module = THIS_MODULE,
986 .proc_name = "ppa",
987 .proc_info = ppa_proc_info,
988 .name = "Iomega VPI0 (ppa) interface",
989 .queuecommand = ppa_queuecommand,
990 .eh_abort_handler = ppa_abort,
991 .eh_bus_reset_handler = ppa_reset,
992 .eh_host_reset_handler = ppa_reset,
993 .bios_param = ppa_biosparam,
994 .this_id = -1,
995 .sg_tablesize = SG_ALL,
996 .cmd_per_lun = 1,
997 .use_clustering = ENABLE_CLUSTERING,
998 .can_queue = 1,
1001 /***************************************************************************
1002 * Parallel port probing routines *
1003 ***************************************************************************/
1005 static LIST_HEAD(ppa_hosts);
1007 static int __ppa_attach(struct parport *pb)
1009 struct Scsi_Host *host;
1010 DECLARE_WAIT_QUEUE_HEAD(waiting);
1011 DEFINE_WAIT(wait);
1012 ppa_struct *dev;
1013 int ports;
1014 int modes, ppb, ppb_hi;
1015 int err = -ENOMEM;
1017 dev = kmalloc(sizeof(ppa_struct), GFP_KERNEL);
1018 if (!dev)
1019 return -ENOMEM;
1020 memset(dev, 0, sizeof(ppa_struct));
1021 dev->base = -1;
1022 dev->mode = PPA_AUTODETECT;
1023 dev->recon_tmo = PPA_RECON_TMO;
1024 init_waitqueue_head(&waiting);
1025 dev->dev = parport_register_device(pb, "ppa", NULL, ppa_wakeup,
1026 NULL, 0, dev);
1028 if (!dev->dev)
1029 goto out;
1031 /* Claim the bus so it remembers what we do to the control
1032 * registers. [ CTR and ECP ]
1034 err = -EBUSY;
1035 dev->waiting = &waiting;
1036 prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
1037 if (ppa_pb_claim(dev))
1038 schedule_timeout(3 * HZ);
1039 if (dev->wanted) {
1040 printk(KERN_ERR "ppa%d: failed to claim parport because "
1041 "a pardevice is owning the port for too long "
1042 "time!\n", pb->number);
1043 ppa_pb_dismiss(dev);
1044 dev->waiting = NULL;
1045 finish_wait(&waiting, &wait);
1046 goto out1;
1048 dev->waiting = NULL;
1049 finish_wait(&waiting, &wait);
1050 ppb = dev->base = dev->dev->port->base;
1051 ppb_hi = dev->dev->port->base_hi;
1052 w_ctr(ppb, 0x0c);
1053 modes = dev->dev->port->modes;
1055 /* Mode detection works up the chain of speed
1056 * This avoids a nasty if-then-else-if-... tree
1058 dev->mode = PPA_NIBBLE;
1060 if (modes & PARPORT_MODE_TRISTATE)
1061 dev->mode = PPA_PS2;
1063 if (modes & PARPORT_MODE_ECP) {
1064 w_ecr(ppb_hi, 0x20);
1065 dev->mode = PPA_PS2;
1067 if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
1068 w_ecr(ppb_hi, 0x80);
1070 /* Done configuration */
1072 err = ppa_init(dev);
1073 ppa_pb_release(dev);
1075 if (err)
1076 goto out1;
1078 /* now the glue ... */
1079 if (dev->mode == PPA_NIBBLE || dev->mode == PPA_PS2)
1080 ports = 3;
1081 else
1082 ports = 8;
1084 INIT_WORK(&dev->ppa_tq, ppa_interrupt, dev);
1086 err = -ENOMEM;
1087 host = scsi_host_alloc(&ppa_template, sizeof(ppa_struct *));
1088 if (!host)
1089 goto out1;
1090 host->io_port = pb->base;
1091 host->n_io_port = ports;
1092 host->dma_channel = -1;
1093 host->unique_id = pb->number;
1094 *(ppa_struct **)&host->hostdata = dev;
1095 dev->host = host;
1096 list_add_tail(&dev->list, &ppa_hosts);
1097 err = scsi_add_host(host, NULL);
1098 if (err)
1099 goto out2;
1100 scsi_scan_host(host);
1101 return 0;
1102 out2:
1103 list_del_init(&dev->list);
1104 scsi_host_put(host);
1105 out1:
1106 parport_unregister_device(dev->dev);
1107 out:
1108 kfree(dev);
1109 return err;
1112 static void ppa_attach(struct parport *pb)
1114 __ppa_attach(pb);
1117 static void ppa_detach(struct parport *pb)
1119 ppa_struct *dev;
1120 list_for_each_entry(dev, &ppa_hosts, list) {
1121 if (dev->dev->port == pb) {
1122 list_del_init(&dev->list);
1123 scsi_remove_host(dev->host);
1124 scsi_host_put(dev->host);
1125 parport_unregister_device(dev->dev);
1126 kfree(dev);
1127 break;
1132 static struct parport_driver ppa_driver = {
1133 .name = "ppa",
1134 .attach = ppa_attach,
1135 .detach = ppa_detach,
1138 static int __init ppa_driver_init(void)
1140 printk("ppa: Version %s\n", PPA_VERSION);
1141 return parport_register_driver(&ppa_driver);
1144 static void __exit ppa_driver_exit(void)
1146 parport_unregister_driver(&ppa_driver);
1149 module_init(ppa_driver_init);
1150 module_exit(ppa_driver_exit);
1151 MODULE_LICENSE("GPL");