[SCSI] use sfoo_printk() in drivers
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / ppa.c
CommitLineData
1da177e4
LT
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
11 */
12
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>
68b3aa7c 20#include <linux/delay.h>
1da177e4
LT
21#include <asm/io.h>
22
23#include <scsi/scsi.h>
24#include <scsi/scsi_cmnd.h>
25#include <scsi/scsi_device.h>
26#include <scsi/scsi_host.h>
27
28
29static void ppa_reset_pulse(unsigned int base);
30
31typedef 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;
45
46#include "ppa.h"
47
48static inline ppa_struct *ppa_dev(struct Scsi_Host *host)
49{
50 return *(ppa_struct **)&host->hostdata;
51}
52
53static DEFINE_SPINLOCK(arbitration_lock);
54
55static void got_it(ppa_struct *dev)
56{
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);
62}
63
64static void ppa_wakeup(void *ref)
65{
66 ppa_struct *dev = (ppa_struct *) ref;
67 unsigned long flags;
68
69 spin_lock_irqsave(&arbitration_lock, flags);
70 if (dev->wanted) {
71 parport_claim(dev->dev);
72 got_it(dev);
73 dev->wanted = 0;
74 }
75 spin_unlock_irqrestore(&arbitration_lock, flags);
76 return;
77}
78
79static int ppa_pb_claim(ppa_struct *dev)
80{
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;
87 }
88 dev->wanted = res;
89 spin_unlock_irqrestore(&arbitration_lock, flags);
90 return res;
91}
92
93static void ppa_pb_dismiss(ppa_struct *dev)
94{
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);
103}
104
105static inline void ppa_pb_release(ppa_struct *dev)
106{
107 parport_release(dev->dev);
108}
109
110/*
111 * Start of Chipset kludges
112 */
113
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)
120 */
121
122static inline int ppa_proc_write(ppa_struct *dev, char *buffer, int length)
123{
124 unsigned long x;
125
126 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
127 x = simple_strtoul(buffer + 5, NULL, 0);
128 dev->mode = x;
129 return length;
130 }
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;
136 }
137 printk("ppa /proc: invalid variable\n");
138 return (-EINVAL);
139}
140
141static int ppa_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, int inout)
142{
143 int len = 0;
144 ppa_struct *dev = ppa_dev(host);
145
146 if (inout)
147 return ppa_proc_write(dev, buffer, length);
148
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
160
161 /* Request for beyond end of buffer */
162 if (offset > length)
163 return 0;
164
165 *start = buffer + offset;
166 len -= offset;
167 if (len > length)
168 len = length;
169 return len;
170}
171
172static int device_check(ppa_struct *dev);
173
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);
177static inline void ppa_fail_func(ppa_struct *dev, int error_code)
178#else
179static inline void ppa_fail(ppa_struct *dev, int error_code)
180#endif
181{
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;
186 }
187}
188
189/*
190 * Wait for the high bit to be set.
191 *
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.
195 */
196static unsigned char ppa_wait(ppa_struct *dev)
197{
198 int k;
199 unsigned short ppb = dev->base;
200 unsigned char r;
201
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);
207 }
208
209 /*
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
215 */
216 if (k)
217 return (r & 0xf0);
218
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 */
223}
224
225/*
226 * Clear EPP Timeout Bit
227 */
228static inline void epp_reset(unsigned short ppb)
229{
230 int i;
231
232 i = r_str(ppb);
233 w_str(ppb, i);
234 w_str(ppb, i & 0xfe);
235}
236
237/*
238 * Wait for empty ECP fifo (if we are in ECP fifo mode only)
239 */
240static inline void ecp_sync(ppa_struct *dev)
241{
242 int i, ppb_hi = dev->dev->port->base_hi;
243
244 if (ppb_hi == 0)
245 return;
246
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);
252 }
253 printk("ppa: ECP sync failed as data still present in FIFO.\n");
254 }
255}
256
257static int ppa_byte_out(unsigned short base, const char *buffer, int len)
258{
259 int i;
260
261 for (i = len; i; i--) {
262 w_dtr(base, *buffer++);
263 w_ctr(base, 0xe);
264 w_ctr(base, 0xc);
265 }
266 return 1; /* All went well - we hope! */
267}
268
269static int ppa_byte_in(unsigned short base, char *buffer, int len)
270{
271 int i;
272
273 for (i = len; i; i--) {
274 *buffer++ = r_dtr(base);
275 w_ctr(base, 0x27);
276 w_ctr(base, 0x25);
277 }
278 return 1; /* All went well - we hope! */
279}
280
281static int ppa_nibble_in(unsigned short base, char *buffer, int len)
282{
283 for (; len; len--) {
284 unsigned char h;
285
286 w_ctr(base, 0x4);
287 h = r_str(base) & 0xf0;
288 w_ctr(base, 0x6);
289 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
290 }
291 return 1; /* All went well - we hope! */
292}
293
294static int ppa_out(ppa_struct *dev, char *buffer, int len)
295{
296 int r;
297 unsigned short ppb = dev->base;
298
299 r = ppa_wait(dev);
300
301 if ((r & 0x50) != 0x40) {
302 ppa_fail(dev, DID_ERROR);
303 return 0;
304 }
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;
311
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;
331
332 default:
333 printk("PPA: bug in ppa_out()\n");
334 r = 0;
335 }
336 return r;
337}
338
339static int ppa_in(ppa_struct *dev, char *buffer, int len)
340{
341 int r;
342 unsigned short ppb = dev->base;
343
344 r = ppa_wait(dev);
345
346 if ((r & 0x50) != 0x50) {
347 ppa_fail(dev, DID_ERROR);
348 return 0;
349 }
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;
356
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;
364
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;
384
385 default:
386 printk("PPA: bug in ppa_ins()\n");
387 r = 0;
388 break;
389 }
390 return r;
391}
392
393/* end of ppa_io.h */
394static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
395{
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);
402}
403
404static void ppa_disconnect(ppa_struct *dev)
405{
406 unsigned short ppb = dev->base;
407
408 ppa_d_pulse(ppb, 0);
409 ppa_d_pulse(ppb, 0x3c);
410 ppa_d_pulse(ppb, 0x20);
411 ppa_d_pulse(ppb, 0xf);
412}
413
414static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
415{
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);
421}
422
423static inline void ppa_connect(ppa_struct *dev, int flag)
424{
425 unsigned short ppb = dev->base;
426
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);
434}
435
436static int ppa_select(ppa_struct *dev, int target)
437{
438 int k;
439 unsigned short ppb = dev->base;
440
441 /*
442 * Bit 6 (0x40) is the device selected bit.
443 * First we must wait till the current device goes off line...
444 */
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;
452
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);
458
459 k = PPA_SELECT_TMO;
460 do {
461 k--;
462 udelay(1);
463 }
464 while (!(r_str(ppb) & 0x40) && (k));
465 if (!k)
466 return 0;
467
468 return 1;
469}
470
471/*
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.
475 *
476 * The return value from this function is just a hint about where the
477 * handshaking failed.
478 *
479 */
480static int ppa_init(ppa_struct *dev)
481{
482 int retv;
483 unsigned short ppb = dev->base;
484
485 ppa_disconnect(dev);
486 ppa_connect(dev, CONNECT_NORMAL);
487
488 retv = 2; /* Failed */
489
490 w_ctr(ppb, 0xe);
491 if ((r_str(ppb) & 0x08) == 0x08)
492 retv--;
493
494 w_ctr(ppb, 0xc);
495 if ((r_str(ppb) & 0x08) == 0x00)
496 retv--;
497
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 */
503
504 if (retv)
505 return -EIO;
506
507 return device_check(dev);
508}
509
510static inline int ppa_send_command(struct scsi_cmnd *cmd)
511{
512 ppa_struct *dev = ppa_dev(cmd->device->host);
513 int k;
514
515 w_ctr(dev->base, 0x0c);
516
517 for (k = 0; k < cmd->cmd_len; k++)
518 if (!ppa_out(dev, &cmd->cmnd[k], 1))
519 return 0;
520 return 1;
521}
522
523/*
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.
527 *
528 * The driver appears to remain stable if we speed up the parallel port
529 * i/o in this function, but not elsewhere.
530 */
531static int ppa_completion(struct scsi_cmnd *cmd)
532{
533 /* Return codes:
534 * -1 Error
535 * 0 Told to schedule
536 * 1 Finished data transfer
537 */
538 ppa_struct *dev = ppa_dev(cmd->device->host);
539 unsigned short ppb = dev->base;
540 unsigned long start_jiffies = jiffies;
541
542 unsigned char r, v;
543 int fast, bulk, status;
544
545 v = cmd->cmnd[0];
546 bulk = ((v == READ_6) ||
547 (v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
548
549 /*
550 * We only get here if the drive is ready to comunicate,
551 * hence no need for a full ppa_wait.
552 */
553 r = (r_str(ppb) & 0xf0);
554
555 while (r != (unsigned char) 0xf0) {
556 /*
557 * If we have been running for more than a full timer tick
558 * then take a rest.
559 */
560 if (time_after(jiffies, start_jiffies + 1))
561 return 0;
562
563 if ((cmd->SCp.this_residual <= 0)) {
564 ppa_fail(dev, DID_ERROR);
565 return -1; /* ERROR_RETURN */
566 }
567
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.
577 */
578 if ((r & 0xc0) != 0xc0) {
579 /* Wait for reconnection should be no more than
580 * jiffy/2 = 5ms = 5000 loops
581 */
582 unsigned long k = dev->recon_tmo;
583 for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
584 k--)
585 udelay(1);
586
587 if (!k)
588 return 0;
589 }
590
591 /* determine if we should use burst I/O */
592 fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
593 ? PPA_BURST_SIZE : 1;
594
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);
599
600 cmd->SCp.ptr += fast;
601 cmd->SCp.this_residual -= fast;
602
603 if (!status) {
604 ppa_fail(dev, DID_BUS_BUSY);
605 return -1; /* ERROR_RETURN */
606 }
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;
616 }
617 }
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;
623 }
624 return 1; /* FINISH_RETURN */
625}
626
627/*
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.
631 */
632static void ppa_interrupt(void *data)
633{
634 ppa_struct *dev = (ppa_struct *) data;
635 struct scsi_cmnd *cmd = dev->cur_cmd;
636
637 if (!cmd) {
638 printk("PPA: bug in ppa_interrupt\n");
639 return;
640 }
641 if (ppa_engine(dev, cmd)) {
642 dev->ppa_tq.data = (void *) dev;
643 schedule_delayed_work(&dev->ppa_tq, 1);
644 return;
645 }
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);
678 }
679#endif
680
681 if (cmd->SCp.phase > 1)
682 ppa_disconnect(dev);
683
684 ppa_pb_dismiss(dev);
685
686 dev->cur_cmd = NULL;
687
688 cmd->scsi_done(cmd);
689}
690
691static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
692{
693 unsigned short ppb = dev->base;
694 unsigned char l = 0, h = 0;
695 int retv;
696
697 /* First check for any errors that may of occurred
698 * Here we check for internal errors
699 */
700 if (dev->failed)
701 return 0;
702
703 switch (cmd->SCp.phase) {
704 case 0: /* Phase 0 - Waiting for parport */
705 if (time_after(jiffies, dev->jstart + HZ)) {
706 /*
707 * We waited more than a second
708 * for parport to call us
709 */
710 ppa_fail(dev, DID_BUS_BUSY);
711 return 0;
712 }
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 */
717
718 ppa_connect(dev, CONNECT_EPP_MAYBE);
719
720 w_ctr(ppb, 0xe);
721 if ((r_str(ppb) & 0x08) == 0x08)
722 retv--;
723
724 w_ctr(ppb, 0xc);
725 if ((r_str(ppb) & 0x08) == 0x00)
726 retv--;
727
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 */
737 }
738 }
739 cmd->SCp.phase++;
740 }
741
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;
746 }
747 cmd->SCp.phase++;
748
749 case 3: /* Phase 3 - Ready to accept a command */
750 w_ctr(ppb, 0x0c);
751 if (!(r_str(ppb) & 0x80))
752 return 1;
753
754 if (!ppa_send_command(cmd))
755 return 0;
756 cmd->SCp.phase++;
757
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;
772 }
773 cmd->SCp.buffers_residual = cmd->use_sg - 1;
774 cmd->SCp.phase++;
775
776 case 5: /* Phase 5 - Data transfer stage */
777 w_ctr(ppb, 0x0c);
778 if (!(r_str(ppb) & 0x80))
779 return 1;
780
781 retv = ppa_completion(cmd);
782 if (retv == -1)
783 return 0;
784 if (retv == 0)
785 return 1;
786 cmd->SCp.phase++;
787
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;
794 }
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);
801 }
802 return 0; /* Finished */
803 break;
804
805 default:
806 printk("ppa: Invalid scsi phase\n");
807 }
808 return 0;
809}
810
811static int ppa_queuecommand(struct scsi_cmnd *cmd,
812 void (*done) (struct scsi_cmnd *))
813{
814 ppa_struct *dev = ppa_dev(cmd->device->host);
815
816 if (dev->cur_cmd) {
817 printk("PPA: bug in ppa_queuecommand\n");
818 return 0;
819 }
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 */
826
827 dev->ppa_tq.data = dev;
828 schedule_work(&dev->ppa_tq);
829
830 ppa_pb_claim(dev);
831
832 return 0;
833}
834
835/*
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.
840 */
841static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
842 sector_t capacity, int ip[])
843{
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;
853 }
854 return 0;
855}
856
857static int ppa_abort(struct scsi_cmnd *cmd)
858{
859 ppa_struct *dev = ppa_dev(cmd->device->host);
860 /*
861 * There is no method for aborting commands since Iomega
862 * have tied the SCSI_MESSAGE line high in the interface
863 */
864
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;
874 }
875}
876
877static void ppa_reset_pulse(unsigned int base)
878{
879 w_dtr(base, 0x40);
880 w_ctr(base, 0x8);
881 udelay(30);
882 w_ctr(base, 0xc);
883}
884
885static int ppa_reset(struct scsi_cmnd *cmd)
886{
887 ppa_struct *dev = ppa_dev(cmd->device->host);
888
889 if (cmd->SCp.phase)
890 ppa_disconnect(dev);
891 dev->cur_cmd = NULL; /* Forget the problem */
892
893 ppa_connect(dev, CONNECT_NORMAL);
894 ppa_reset_pulse(dev->base);
68b3aa7c 895 mdelay(1); /* device settle delay */
1da177e4 896 ppa_disconnect(dev);
68b3aa7c 897 mdelay(1); /* device settle delay */
1da177e4
LT
898 return SUCCESS;
899}
900
901static int device_check(ppa_struct *dev)
902{
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. */
905
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;
909
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;
915
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;
922 }
923 printk("ppa: Found device at ID %i, Attempting to use %s\n",
924 loop, PPA_MODE_STRING[dev->mode]);
925
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);
931
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;
945 }
946 return -EIO;
947 }
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));
955
956 l &= 0xf0;
957
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;
968 }
969 return -EIO;
970 }
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;
980 }
981 return -ENODEV;
982}
983
984static 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,
999};
1000
1001/***************************************************************************
1002 * Parallel port probing routines *
1003 ***************************************************************************/
1004
1005static LIST_HEAD(ppa_hosts);
1006
1007static int __ppa_attach(struct parport *pb)
1008{
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;
1016
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);
1027
1028 if (!dev->dev)
1029 goto out;
1030
1031 /* Claim the bus so it remembers what we do to the control
1032 * registers. [ CTR and ECP ]
1033 */
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;
1047 }
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;
1054
1055 /* Mode detection works up the chain of speed
1056 * This avoids a nasty if-then-else-if-... tree
1057 */
1058 dev->mode = PPA_NIBBLE;
1059
1060 if (modes & PARPORT_MODE_TRISTATE)
1061 dev->mode = PPA_PS2;
1062
1063 if (modes & PARPORT_MODE_ECP) {
1064 w_ecr(ppb_hi, 0x20);
1065 dev->mode = PPA_PS2;
1066 }
1067 if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
1068 w_ecr(ppb_hi, 0x80);
1069
1070 /* Done configuration */
1071
1072 err = ppa_init(dev);
1073 ppa_pb_release(dev);
1074
1075 if (err)
1076 goto out1;
1077
1078 /* now the glue ... */
1079 if (dev->mode == PPA_NIBBLE || dev->mode == PPA_PS2)
1080 ports = 3;
1081 else
1082 ports = 8;
1083
1084 INIT_WORK(&dev->ppa_tq, ppa_interrupt, dev);
1085
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;
1102out2:
1103 list_del_init(&dev->list);
1104 scsi_host_put(host);
1105out1:
1106 parport_unregister_device(dev->dev);
1107out:
1108 kfree(dev);
1109 return err;
1110}
1111
1112static void ppa_attach(struct parport *pb)
1113{
1114 __ppa_attach(pb);
1115}
1116
1117static void ppa_detach(struct parport *pb)
1118{
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;
1128 }
1129 }
1130}
1131
1132static struct parport_driver ppa_driver = {
1133 .name = "ppa",
1134 .attach = ppa_attach,
1135 .detach = ppa_detach,
1136};
1137
1138static int __init ppa_driver_init(void)
1139{
1140 printk("ppa: Version %s\n", PPA_VERSION);
1141 return parport_register_driver(&ppa_driver);
1142}
1143
1144static void __exit ppa_driver_exit(void)
1145{
1146 parport_unregister_driver(&ppa_driver);
1147}
1148
1149module_init(ppa_driver_init);
1150module_exit(ppa_driver_exit);
1151MODULE_LICENSE("GPL");