Loading...
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 */
10
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/blkdev.h>
16#include <linux/parport.h>
17#include <linux/workqueue.h>
18#include <linux/delay.h>
19#include <linux/jiffies.h>
20#include <asm/io.h>
21
22#include <scsi/scsi.h>
23#include <scsi/scsi_cmnd.h>
24#include <scsi/scsi_device.h>
25#include <scsi/scsi_host.h>
26
27
28static void ppa_reset_pulse(unsigned int base);
29
30typedef struct {
31 struct pardevice *dev; /* Parport device entry */
32 int base; /* Actual port address */
33 int mode; /* Transfer mode */
34 struct scsi_cmnd *cur_cmd; /* Current queued command */
35 struct delayed_work ppa_tq; /* Polling interrupt stuff */
36 unsigned long jstart; /* Jiffies at start */
37 unsigned long recon_tmo; /* How many usecs to wait for reconnection (6th bit) */
38 unsigned int failed:1; /* Failure flag */
39 unsigned wanted:1; /* Parport sharing busy flag */
40 unsigned int dev_no; /* Device number */
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_write_info(struct Scsi_Host *host, char *buffer, int length)
123{
124 ppa_struct *dev = ppa_dev(host);
125 unsigned long x;
126
127 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
128 x = simple_strtoul(buffer + 5, NULL, 0);
129 dev->mode = x;
130 return length;
131 }
132 if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
133 x = simple_strtoul(buffer + 10, NULL, 0);
134 dev->recon_tmo = x;
135 printk(KERN_INFO "ppa: recon_tmo set to %ld\n", x);
136 return length;
137 }
138 printk(KERN_WARNING "ppa /proc: invalid variable\n");
139 return -EINVAL;
140}
141
142static int ppa_show_info(struct seq_file *m, struct Scsi_Host *host)
143{
144 ppa_struct *dev = ppa_dev(host);
145
146 seq_printf(m, "Version : %s\n", PPA_VERSION);
147 seq_printf(m, "Parport : %s\n", dev->dev->port->name);
148 seq_printf(m, "Mode : %s\n", PPA_MODE_STRING[dev->mode]);
149#if PPA_DEBUG > 0
150 seq_printf(m, "recon_tmo : %lu\n", dev->recon_tmo);
151#endif
152 return 0;
153}
154
155static int device_check(ppa_struct *dev);
156
157#if PPA_DEBUG > 0
158#define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
159 y, __func__, __LINE__); ppa_fail_func(x,y);
160static inline void ppa_fail_func(ppa_struct *dev, int error_code)
161#else
162static inline void ppa_fail(ppa_struct *dev, int error_code)
163#endif
164{
165 /* If we fail a device then we trash status / message bytes */
166 if (dev->cur_cmd) {
167 dev->cur_cmd->result = error_code << 16;
168 dev->failed = 1;
169 }
170}
171
172/*
173 * Wait for the high bit to be set.
174 *
175 * In principle, this could be tied to an interrupt, but the adapter
176 * doesn't appear to be designed to support interrupts. We spin on
177 * the 0x80 ready bit.
178 */
179static unsigned char ppa_wait(ppa_struct *dev)
180{
181 int k;
182 unsigned short ppb = dev->base;
183 unsigned char r;
184
185 k = PPA_SPIN_TMO;
186 /* Wait for bit 6 and 7 - PJC */
187 for (r = r_str(ppb); ((r & 0xc0) != 0xc0) && (k); k--) {
188 udelay(1);
189 r = r_str(ppb);
190 }
191
192 /*
193 * return some status information.
194 * Semantics: 0xc0 = ZIP wants more data
195 * 0xd0 = ZIP wants to send more data
196 * 0xe0 = ZIP is expecting SCSI command data
197 * 0xf0 = end of transfer, ZIP is sending status
198 */
199 if (k)
200 return (r & 0xf0);
201
202 /* Counter expired - Time out occurred */
203 ppa_fail(dev, DID_TIME_OUT);
204 printk(KERN_WARNING "ppa timeout in ppa_wait\n");
205 return 0; /* command timed out */
206}
207
208/*
209 * Clear EPP Timeout Bit
210 */
211static inline void epp_reset(unsigned short ppb)
212{
213 int i;
214
215 i = r_str(ppb);
216 w_str(ppb, i);
217 w_str(ppb, i & 0xfe);
218}
219
220/*
221 * Wait for empty ECP fifo (if we are in ECP fifo mode only)
222 */
223static inline void ecp_sync(ppa_struct *dev)
224{
225 int i, ppb_hi = dev->dev->port->base_hi;
226
227 if (ppb_hi == 0)
228 return;
229
230 if ((r_ecr(ppb_hi) & 0xe0) == 0x60) { /* mode 011 == ECP fifo mode */
231 for (i = 0; i < 100; i++) {
232 if (r_ecr(ppb_hi) & 0x01)
233 return;
234 udelay(5);
235 }
236 printk(KERN_WARNING "ppa: ECP sync failed as data still present in FIFO.\n");
237 }
238}
239
240static int ppa_byte_out(unsigned short base, const char *buffer, int len)
241{
242 int i;
243
244 for (i = len; i; i--) {
245 w_dtr(base, *buffer++);
246 w_ctr(base, 0xe);
247 w_ctr(base, 0xc);
248 }
249 return 1; /* All went well - we hope! */
250}
251
252static int ppa_byte_in(unsigned short base, char *buffer, int len)
253{
254 int i;
255
256 for (i = len; i; i--) {
257 *buffer++ = r_dtr(base);
258 w_ctr(base, 0x27);
259 w_ctr(base, 0x25);
260 }
261 return 1; /* All went well - we hope! */
262}
263
264static int ppa_nibble_in(unsigned short base, char *buffer, int len)
265{
266 for (; len; len--) {
267 unsigned char h;
268
269 w_ctr(base, 0x4);
270 h = r_str(base) & 0xf0;
271 w_ctr(base, 0x6);
272 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
273 }
274 return 1; /* All went well - we hope! */
275}
276
277static int ppa_out(ppa_struct *dev, char *buffer, int len)
278{
279 int r;
280 unsigned short ppb = dev->base;
281
282 r = ppa_wait(dev);
283
284 if ((r & 0x50) != 0x40) {
285 ppa_fail(dev, DID_ERROR);
286 return 0;
287 }
288 switch (dev->mode) {
289 case PPA_NIBBLE:
290 case PPA_PS2:
291 /* 8 bit output, with a loop */
292 r = ppa_byte_out(ppb, buffer, len);
293 break;
294
295 case PPA_EPP_32:
296 case PPA_EPP_16:
297 case PPA_EPP_8:
298 epp_reset(ppb);
299 w_ctr(ppb, 0x4);
300#ifdef CONFIG_SCSI_IZIP_EPP16
301 if (!(((long) buffer | len) & 0x01))
302 outsw(ppb + 4, buffer, len >> 1);
303#else
304 if (!(((long) buffer | len) & 0x03))
305 outsl(ppb + 4, buffer, len >> 2);
306#endif
307 else
308 outsb(ppb + 4, buffer, len);
309 w_ctr(ppb, 0xc);
310 r = !(r_str(ppb) & 0x01);
311 w_ctr(ppb, 0xc);
312 ecp_sync(dev);
313 break;
314
315 default:
316 printk(KERN_ERR "PPA: bug in ppa_out()\n");
317 r = 0;
318 }
319 return r;
320}
321
322static int ppa_in(ppa_struct *dev, char *buffer, int len)
323{
324 int r;
325 unsigned short ppb = dev->base;
326
327 r = ppa_wait(dev);
328
329 if ((r & 0x50) != 0x50) {
330 ppa_fail(dev, DID_ERROR);
331 return 0;
332 }
333 switch (dev->mode) {
334 case PPA_NIBBLE:
335 /* 4 bit input, with a loop */
336 r = ppa_nibble_in(ppb, buffer, len);
337 w_ctr(ppb, 0xc);
338 break;
339
340 case PPA_PS2:
341 /* 8 bit input, with a loop */
342 w_ctr(ppb, 0x25);
343 r = ppa_byte_in(ppb, buffer, len);
344 w_ctr(ppb, 0x4);
345 w_ctr(ppb, 0xc);
346 break;
347
348 case PPA_EPP_32:
349 case PPA_EPP_16:
350 case PPA_EPP_8:
351 epp_reset(ppb);
352 w_ctr(ppb, 0x24);
353#ifdef CONFIG_SCSI_IZIP_EPP16
354 if (!(((long) buffer | len) & 0x01))
355 insw(ppb + 4, buffer, len >> 1);
356#else
357 if (!(((long) buffer | len) & 0x03))
358 insl(ppb + 4, buffer, len >> 2);
359#endif
360 else
361 insb(ppb + 4, buffer, len);
362 w_ctr(ppb, 0x2c);
363 r = !(r_str(ppb) & 0x01);
364 w_ctr(ppb, 0x2c);
365 ecp_sync(dev);
366 break;
367
368 default:
369 printk(KERN_ERR "PPA: bug in ppa_ins()\n");
370 r = 0;
371 break;
372 }
373 return r;
374}
375
376/* end of ppa_io.h */
377static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
378{
379 w_dtr(ppb, b);
380 w_ctr(ppb, 0xc);
381 w_ctr(ppb, 0xe);
382 w_ctr(ppb, 0xc);
383 w_ctr(ppb, 0x4);
384 w_ctr(ppb, 0xc);
385}
386
387static void ppa_disconnect(ppa_struct *dev)
388{
389 unsigned short ppb = dev->base;
390
391 ppa_d_pulse(ppb, 0);
392 ppa_d_pulse(ppb, 0x3c);
393 ppa_d_pulse(ppb, 0x20);
394 ppa_d_pulse(ppb, 0xf);
395}
396
397static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
398{
399 w_dtr(ppb, b);
400 w_ctr(ppb, 0x4);
401 w_ctr(ppb, 0x6);
402 w_ctr(ppb, 0x4);
403 w_ctr(ppb, 0xc);
404}
405
406static inline void ppa_connect(ppa_struct *dev, int flag)
407{
408 unsigned short ppb = dev->base;
409
410 ppa_c_pulse(ppb, 0);
411 ppa_c_pulse(ppb, 0x3c);
412 ppa_c_pulse(ppb, 0x20);
413 if ((flag == CONNECT_EPP_MAYBE) && IN_EPP_MODE(dev->mode))
414 ppa_c_pulse(ppb, 0xcf);
415 else
416 ppa_c_pulse(ppb, 0x8f);
417}
418
419static int ppa_select(ppa_struct *dev, int target)
420{
421 int k;
422 unsigned short ppb = dev->base;
423
424 /*
425 * Bit 6 (0x40) is the device selected bit.
426 * First we must wait till the current device goes off line...
427 */
428 k = PPA_SELECT_TMO;
429 do {
430 k--;
431 udelay(1);
432 } while ((r_str(ppb) & 0x40) && (k));
433 if (!k)
434 return 0;
435
436 w_dtr(ppb, (1 << target));
437 w_ctr(ppb, 0xe);
438 w_ctr(ppb, 0xc);
439 w_dtr(ppb, 0x80); /* This is NOT the initator */
440 w_ctr(ppb, 0x8);
441
442 k = PPA_SELECT_TMO;
443 do {
444 k--;
445 udelay(1);
446 }
447 while (!(r_str(ppb) & 0x40) && (k));
448 if (!k)
449 return 0;
450
451 return 1;
452}
453
454/*
455 * This is based on a trace of what the Iomega DOS 'guest' driver does.
456 * I've tried several different kinds of parallel ports with guest and
457 * coded this to react in the same ways that it does.
458 *
459 * The return value from this function is just a hint about where the
460 * handshaking failed.
461 *
462 */
463static int ppa_init(ppa_struct *dev)
464{
465 int retv;
466 unsigned short ppb = dev->base;
467
468 ppa_disconnect(dev);
469 ppa_connect(dev, CONNECT_NORMAL);
470
471 retv = 2; /* Failed */
472
473 w_ctr(ppb, 0xe);
474 if ((r_str(ppb) & 0x08) == 0x08)
475 retv--;
476
477 w_ctr(ppb, 0xc);
478 if ((r_str(ppb) & 0x08) == 0x00)
479 retv--;
480
481 if (!retv)
482 ppa_reset_pulse(ppb);
483 udelay(1000); /* Allow devices to settle down */
484 ppa_disconnect(dev);
485 udelay(1000); /* Another delay to allow devices to settle */
486
487 if (retv)
488 return -EIO;
489
490 return device_check(dev);
491}
492
493static inline int ppa_send_command(struct scsi_cmnd *cmd)
494{
495 ppa_struct *dev = ppa_dev(cmd->device->host);
496 int k;
497
498 w_ctr(dev->base, 0x0c);
499
500 for (k = 0; k < cmd->cmd_len; k++)
501 if (!ppa_out(dev, &cmd->cmnd[k], 1))
502 return 0;
503 return 1;
504}
505
506/*
507 * The bulk flag enables some optimisations in the data transfer loops,
508 * it should be true for any command that transfers data in integral
509 * numbers of sectors.
510 *
511 * The driver appears to remain stable if we speed up the parallel port
512 * i/o in this function, but not elsewhere.
513 */
514static int ppa_completion(struct scsi_cmnd *cmd)
515{
516 /* Return codes:
517 * -1 Error
518 * 0 Told to schedule
519 * 1 Finished data transfer
520 */
521 ppa_struct *dev = ppa_dev(cmd->device->host);
522 unsigned short ppb = dev->base;
523 unsigned long start_jiffies = jiffies;
524
525 unsigned char r, v;
526 int fast, bulk, status;
527
528 v = cmd->cmnd[0];
529 bulk = ((v == READ_6) ||
530 (v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
531
532 /*
533 * We only get here if the drive is ready to comunicate,
534 * hence no need for a full ppa_wait.
535 */
536 r = (r_str(ppb) & 0xf0);
537
538 while (r != (unsigned char) 0xf0) {
539 /*
540 * If we have been running for more than a full timer tick
541 * then take a rest.
542 */
543 if (time_after(jiffies, start_jiffies + 1))
544 return 0;
545
546 if ((cmd->SCp.this_residual <= 0)) {
547 ppa_fail(dev, DID_ERROR);
548 return -1; /* ERROR_RETURN */
549 }
550
551 /* On some hardware we have SCSI disconnected (6th bit low)
552 * for about 100usecs. It is too expensive to wait a
553 * tick on every loop so we busy wait for no more than
554 * 500usecs to give the drive a chance first. We do not
555 * change things for "normal" hardware since generally
556 * the 6th bit is always high.
557 * This makes the CPU load higher on some hardware
558 * but otherwise we can not get more than 50K/secs
559 * on this problem hardware.
560 */
561 if ((r & 0xc0) != 0xc0) {
562 /* Wait for reconnection should be no more than
563 * jiffy/2 = 5ms = 5000 loops
564 */
565 unsigned long k = dev->recon_tmo;
566 for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
567 k--)
568 udelay(1);
569
570 if (!k)
571 return 0;
572 }
573
574 /* determine if we should use burst I/O */
575 fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
576 ? PPA_BURST_SIZE : 1;
577
578 if (r == (unsigned char) 0xc0)
579 status = ppa_out(dev, cmd->SCp.ptr, fast);
580 else
581 status = ppa_in(dev, cmd->SCp.ptr, fast);
582
583 cmd->SCp.ptr += fast;
584 cmd->SCp.this_residual -= fast;
585
586 if (!status) {
587 ppa_fail(dev, DID_BUS_BUSY);
588 return -1; /* ERROR_RETURN */
589 }
590 if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
591 /* if scatter/gather, advance to the next segment */
592 if (cmd->SCp.buffers_residual--) {
593 cmd->SCp.buffer = sg_next(cmd->SCp.buffer);
594 cmd->SCp.this_residual =
595 cmd->SCp.buffer->length;
596 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
597 }
598 }
599 /* Now check to see if the drive is ready to comunicate */
600 r = (r_str(ppb) & 0xf0);
601 /* If not, drop back down to the scheduler and wait a timer tick */
602 if (!(r & 0x80))
603 return 0;
604 }
605 return 1; /* FINISH_RETURN */
606}
607
608/*
609 * Since the PPA itself doesn't generate interrupts, we use
610 * the scheduler's task queue to generate a stream of call-backs and
611 * complete the request when the drive is ready.
612 */
613static void ppa_interrupt(struct work_struct *work)
614{
615 ppa_struct *dev = container_of(work, ppa_struct, ppa_tq.work);
616 struct scsi_cmnd *cmd = dev->cur_cmd;
617
618 if (!cmd) {
619 printk(KERN_ERR "PPA: bug in ppa_interrupt\n");
620 return;
621 }
622 if (ppa_engine(dev, cmd)) {
623 schedule_delayed_work(&dev->ppa_tq, 1);
624 return;
625 }
626 /* Command must of completed hence it is safe to let go... */
627#if PPA_DEBUG > 0
628 switch ((cmd->result >> 16) & 0xff) {
629 case DID_OK:
630 break;
631 case DID_NO_CONNECT:
632 printk(KERN_DEBUG "ppa: no device at SCSI ID %i\n", cmd->device->target);
633 break;
634 case DID_BUS_BUSY:
635 printk(KERN_DEBUG "ppa: BUS BUSY - EPP timeout detected\n");
636 break;
637 case DID_TIME_OUT:
638 printk(KERN_DEBUG "ppa: unknown timeout\n");
639 break;
640 case DID_ABORT:
641 printk(KERN_DEBUG "ppa: told to abort\n");
642 break;
643 case DID_PARITY:
644 printk(KERN_DEBUG "ppa: parity error (???)\n");
645 break;
646 case DID_ERROR:
647 printk(KERN_DEBUG "ppa: internal driver error\n");
648 break;
649 case DID_RESET:
650 printk(KERN_DEBUG "ppa: told to reset device\n");
651 break;
652 case DID_BAD_INTR:
653 printk(KERN_WARNING "ppa: bad interrupt (???)\n");
654 break;
655 default:
656 printk(KERN_WARNING "ppa: bad return code (%02x)\n",
657 (cmd->result >> 16) & 0xff);
658 }
659#endif
660
661 if (cmd->SCp.phase > 1)
662 ppa_disconnect(dev);
663
664 ppa_pb_dismiss(dev);
665
666 dev->cur_cmd = NULL;
667
668 cmd->scsi_done(cmd);
669}
670
671static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
672{
673 unsigned short ppb = dev->base;
674 unsigned char l = 0, h = 0;
675 int retv;
676
677 /* First check for any errors that may of occurred
678 * Here we check for internal errors
679 */
680 if (dev->failed)
681 return 0;
682
683 switch (cmd->SCp.phase) {
684 case 0: /* Phase 0 - Waiting for parport */
685 if (time_after(jiffies, dev->jstart + HZ)) {
686 /*
687 * We waited more than a second
688 * for parport to call us
689 */
690 ppa_fail(dev, DID_BUS_BUSY);
691 return 0;
692 }
693 return 1; /* wait until ppa_wakeup claims parport */
694 case 1: /* Phase 1 - Connected */
695 { /* Perform a sanity check for cable unplugged */
696 int retv = 2; /* Failed */
697
698 ppa_connect(dev, CONNECT_EPP_MAYBE);
699
700 w_ctr(ppb, 0xe);
701 if ((r_str(ppb) & 0x08) == 0x08)
702 retv--;
703
704 w_ctr(ppb, 0xc);
705 if ((r_str(ppb) & 0x08) == 0x00)
706 retv--;
707
708 if (retv) {
709 if (time_after(jiffies, dev->jstart + (1 * HZ))) {
710 printk(KERN_ERR "ppa: Parallel port cable is unplugged.\n");
711 ppa_fail(dev, DID_BUS_BUSY);
712 return 0;
713 } else {
714 ppa_disconnect(dev);
715 return 1; /* Try again in a jiffy */
716 }
717 }
718 cmd->SCp.phase++;
719 }
720 fallthrough;
721
722 case 2: /* Phase 2 - We are now talking to the scsi bus */
723 if (!ppa_select(dev, scmd_id(cmd))) {
724 ppa_fail(dev, DID_NO_CONNECT);
725 return 0;
726 }
727 cmd->SCp.phase++;
728 fallthrough;
729
730 case 3: /* Phase 3 - Ready to accept a command */
731 w_ctr(ppb, 0x0c);
732 if (!(r_str(ppb) & 0x80))
733 return 1;
734
735 if (!ppa_send_command(cmd))
736 return 0;
737 cmd->SCp.phase++;
738 fallthrough;
739
740 case 4: /* Phase 4 - Setup scatter/gather buffers */
741 if (scsi_bufflen(cmd)) {
742 cmd->SCp.buffer = scsi_sglist(cmd);
743 cmd->SCp.this_residual = cmd->SCp.buffer->length;
744 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
745 } else {
746 cmd->SCp.buffer = NULL;
747 cmd->SCp.this_residual = 0;
748 cmd->SCp.ptr = NULL;
749 }
750 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
751 cmd->SCp.phase++;
752 fallthrough;
753
754 case 5: /* Phase 5 - Data transfer stage */
755 w_ctr(ppb, 0x0c);
756 if (!(r_str(ppb) & 0x80))
757 return 1;
758
759 retv = ppa_completion(cmd);
760 if (retv == -1)
761 return 0;
762 if (retv == 0)
763 return 1;
764 cmd->SCp.phase++;
765 fallthrough;
766
767 case 6: /* Phase 6 - Read status/message */
768 cmd->result = DID_OK << 16;
769 /* Check for data overrun */
770 if (ppa_wait(dev) != (unsigned char) 0xf0) {
771 ppa_fail(dev, DID_ERROR);
772 return 0;
773 }
774 if (ppa_in(dev, &l, 1)) { /* read status byte */
775 /* Check for optional message byte */
776 if (ppa_wait(dev) == (unsigned char) 0xf0)
777 ppa_in(dev, &h, 1);
778 cmd->result =
779 (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
780 }
781 return 0; /* Finished */
782
783 default:
784 printk(KERN_ERR "ppa: Invalid scsi phase\n");
785 }
786 return 0;
787}
788
789static int ppa_queuecommand_lck(struct scsi_cmnd *cmd,
790 void (*done) (struct scsi_cmnd *))
791{
792 ppa_struct *dev = ppa_dev(cmd->device->host);
793
794 if (dev->cur_cmd) {
795 printk(KERN_ERR "PPA: bug in ppa_queuecommand\n");
796 return 0;
797 }
798 dev->failed = 0;
799 dev->jstart = jiffies;
800 dev->cur_cmd = cmd;
801 cmd->scsi_done = done;
802 cmd->result = DID_ERROR << 16; /* default return code */
803 cmd->SCp.phase = 0; /* bus free */
804
805 schedule_delayed_work(&dev->ppa_tq, 0);
806
807 ppa_pb_claim(dev);
808
809 return 0;
810}
811
812static DEF_SCSI_QCMD(ppa_queuecommand)
813
814/*
815 * Apparently the disk->capacity attribute is off by 1 sector
816 * for all disk drives. We add the one here, but it should really
817 * be done in sd.c. Even if it gets fixed there, this will still
818 * work.
819 */
820static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
821 sector_t capacity, int ip[])
822{
823 ip[0] = 0x40;
824 ip[1] = 0x20;
825 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
826 if (ip[2] > 1024) {
827 ip[0] = 0xff;
828 ip[1] = 0x3f;
829 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
830 if (ip[2] > 1023)
831 ip[2] = 1023;
832 }
833 return 0;
834}
835
836static int ppa_abort(struct scsi_cmnd *cmd)
837{
838 ppa_struct *dev = ppa_dev(cmd->device->host);
839 /*
840 * There is no method for aborting commands since Iomega
841 * have tied the SCSI_MESSAGE line high in the interface
842 */
843
844 switch (cmd->SCp.phase) {
845 case 0: /* Do not have access to parport */
846 case 1: /* Have not connected to interface */
847 dev->cur_cmd = NULL; /* Forget the problem */
848 return SUCCESS;
849 default: /* SCSI command sent, can not abort */
850 return FAILED;
851 }
852}
853
854static void ppa_reset_pulse(unsigned int base)
855{
856 w_dtr(base, 0x40);
857 w_ctr(base, 0x8);
858 udelay(30);
859 w_ctr(base, 0xc);
860}
861
862static int ppa_reset(struct scsi_cmnd *cmd)
863{
864 ppa_struct *dev = ppa_dev(cmd->device->host);
865
866 if (cmd->SCp.phase)
867 ppa_disconnect(dev);
868 dev->cur_cmd = NULL; /* Forget the problem */
869
870 ppa_connect(dev, CONNECT_NORMAL);
871 ppa_reset_pulse(dev->base);
872 mdelay(1); /* device settle delay */
873 ppa_disconnect(dev);
874 mdelay(1); /* device settle delay */
875 return SUCCESS;
876}
877
878static int device_check(ppa_struct *dev)
879{
880 /* This routine looks for a device and then attempts to use EPP
881 to send a command. If all goes as planned then EPP is available. */
882
883 static u8 cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
884 int loop, old_mode, status, k, ppb = dev->base;
885 unsigned char l;
886
887 old_mode = dev->mode;
888 for (loop = 0; loop < 8; loop++) {
889 /* Attempt to use EPP for Test Unit Ready */
890 if ((ppb & 0x0007) == 0x0000)
891 dev->mode = PPA_EPP_32;
892
893second_pass:
894 ppa_connect(dev, CONNECT_EPP_MAYBE);
895 /* Select SCSI device */
896 if (!ppa_select(dev, loop)) {
897 ppa_disconnect(dev);
898 continue;
899 }
900 printk(KERN_INFO "ppa: Found device at ID %i, Attempting to use %s\n",
901 loop, PPA_MODE_STRING[dev->mode]);
902
903 /* Send SCSI command */
904 status = 1;
905 w_ctr(ppb, 0x0c);
906 for (l = 0; (l < 6) && (status); l++)
907 status = ppa_out(dev, cmd, 1);
908
909 if (!status) {
910 ppa_disconnect(dev);
911 ppa_connect(dev, CONNECT_EPP_MAYBE);
912 w_dtr(ppb, 0x40);
913 w_ctr(ppb, 0x08);
914 udelay(30);
915 w_ctr(ppb, 0x0c);
916 udelay(1000);
917 ppa_disconnect(dev);
918 udelay(1000);
919 if (dev->mode == PPA_EPP_32) {
920 dev->mode = old_mode;
921 goto second_pass;
922 }
923 return -EIO;
924 }
925 w_ctr(ppb, 0x0c);
926 k = 1000000; /* 1 Second */
927 do {
928 l = r_str(ppb);
929 k--;
930 udelay(1);
931 } while (!(l & 0x80) && (k));
932
933 l &= 0xf0;
934
935 if (l != 0xf0) {
936 ppa_disconnect(dev);
937 ppa_connect(dev, CONNECT_EPP_MAYBE);
938 ppa_reset_pulse(ppb);
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 ppa_disconnect(dev);
949 printk(KERN_INFO "ppa: Communication established with ID %i using %s\n",
950 loop, PPA_MODE_STRING[dev->mode]);
951 ppa_connect(dev, CONNECT_EPP_MAYBE);
952 ppa_reset_pulse(ppb);
953 udelay(1000);
954 ppa_disconnect(dev);
955 udelay(1000);
956 return 0;
957 }
958 return -ENODEV;
959}
960
961static int ppa_adjust_queue(struct scsi_device *device)
962{
963 blk_queue_bounce_limit(device->request_queue, BLK_BOUNCE_HIGH);
964 return 0;
965}
966
967static struct scsi_host_template ppa_template = {
968 .module = THIS_MODULE,
969 .proc_name = "ppa",
970 .show_info = ppa_show_info,
971 .write_info = ppa_write_info,
972 .name = "Iomega VPI0 (ppa) interface",
973 .queuecommand = ppa_queuecommand,
974 .eh_abort_handler = ppa_abort,
975 .eh_host_reset_handler = ppa_reset,
976 .bios_param = ppa_biosparam,
977 .this_id = -1,
978 .sg_tablesize = SG_ALL,
979 .can_queue = 1,
980 .slave_alloc = ppa_adjust_queue,
981};
982
983/***************************************************************************
984 * Parallel port probing routines *
985 ***************************************************************************/
986
987static LIST_HEAD(ppa_hosts);
988
989/*
990 * Finds the first available device number that can be alloted to the
991 * new ppa device and returns the address of the previous node so that
992 * we can add to the tail and have a list in the ascending order.
993 */
994
995static inline ppa_struct *find_parent(void)
996{
997 ppa_struct *dev, *par = NULL;
998 unsigned int cnt = 0;
999
1000 if (list_empty(&ppa_hosts))
1001 return NULL;
1002
1003 list_for_each_entry(dev, &ppa_hosts, list) {
1004 if (dev->dev_no != cnt)
1005 return par;
1006 cnt++;
1007 par = dev;
1008 }
1009
1010 return par;
1011}
1012
1013static int __ppa_attach(struct parport *pb)
1014{
1015 struct Scsi_Host *host;
1016 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waiting);
1017 DEFINE_WAIT(wait);
1018 ppa_struct *dev, *temp;
1019 int ports;
1020 int modes, ppb, ppb_hi;
1021 int err = -ENOMEM;
1022 struct pardev_cb ppa_cb;
1023
1024 dev = kzalloc(sizeof(ppa_struct), GFP_KERNEL);
1025 if (!dev)
1026 return -ENOMEM;
1027 dev->base = -1;
1028 dev->mode = PPA_AUTODETECT;
1029 dev->recon_tmo = PPA_RECON_TMO;
1030 init_waitqueue_head(&waiting);
1031 temp = find_parent();
1032 if (temp)
1033 dev->dev_no = temp->dev_no + 1;
1034
1035 memset(&ppa_cb, 0, sizeof(ppa_cb));
1036 ppa_cb.private = dev;
1037 ppa_cb.wakeup = ppa_wakeup;
1038
1039 dev->dev = parport_register_dev_model(pb, "ppa", &ppa_cb, dev->dev_no);
1040
1041 if (!dev->dev)
1042 goto out;
1043
1044 /* Claim the bus so it remembers what we do to the control
1045 * registers. [ CTR and ECP ]
1046 */
1047 err = -EBUSY;
1048 dev->waiting = &waiting;
1049 prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
1050 if (ppa_pb_claim(dev))
1051 schedule_timeout(3 * HZ);
1052 if (dev->wanted) {
1053 printk(KERN_ERR "ppa%d: failed to claim parport because "
1054 "a pardevice is owning the port for too long "
1055 "time!\n", pb->number);
1056 ppa_pb_dismiss(dev);
1057 dev->waiting = NULL;
1058 finish_wait(&waiting, &wait);
1059 goto out1;
1060 }
1061 dev->waiting = NULL;
1062 finish_wait(&waiting, &wait);
1063 ppb = dev->base = dev->dev->port->base;
1064 ppb_hi = dev->dev->port->base_hi;
1065 w_ctr(ppb, 0x0c);
1066 modes = dev->dev->port->modes;
1067
1068 /* Mode detection works up the chain of speed
1069 * This avoids a nasty if-then-else-if-... tree
1070 */
1071 dev->mode = PPA_NIBBLE;
1072
1073 if (modes & PARPORT_MODE_TRISTATE)
1074 dev->mode = PPA_PS2;
1075
1076 if (modes & PARPORT_MODE_ECP) {
1077 w_ecr(ppb_hi, 0x20);
1078 dev->mode = PPA_PS2;
1079 }
1080 if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
1081 w_ecr(ppb_hi, 0x80);
1082
1083 /* Done configuration */
1084
1085 err = ppa_init(dev);
1086 ppa_pb_release(dev);
1087
1088 if (err)
1089 goto out1;
1090
1091 /* now the glue ... */
1092 if (dev->mode == PPA_NIBBLE || dev->mode == PPA_PS2)
1093 ports = 3;
1094 else
1095 ports = 8;
1096
1097 INIT_DELAYED_WORK(&dev->ppa_tq, ppa_interrupt);
1098
1099 err = -ENOMEM;
1100 host = scsi_host_alloc(&ppa_template, sizeof(ppa_struct *));
1101 if (!host)
1102 goto out1;
1103 host->io_port = pb->base;
1104 host->n_io_port = ports;
1105 host->dma_channel = -1;
1106 host->unique_id = pb->number;
1107 *(ppa_struct **)&host->hostdata = dev;
1108 dev->host = host;
1109 list_add_tail(&dev->list, &ppa_hosts);
1110 err = scsi_add_host(host, NULL);
1111 if (err)
1112 goto out2;
1113 scsi_scan_host(host);
1114 return 0;
1115out2:
1116 list_del_init(&dev->list);
1117 scsi_host_put(host);
1118out1:
1119 parport_unregister_device(dev->dev);
1120out:
1121 kfree(dev);
1122 return err;
1123}
1124
1125static void ppa_attach(struct parport *pb)
1126{
1127 __ppa_attach(pb);
1128}
1129
1130static void ppa_detach(struct parport *pb)
1131{
1132 ppa_struct *dev;
1133 list_for_each_entry(dev, &ppa_hosts, list) {
1134 if (dev->dev->port == pb) {
1135 list_del_init(&dev->list);
1136 scsi_remove_host(dev->host);
1137 scsi_host_put(dev->host);
1138 parport_unregister_device(dev->dev);
1139 kfree(dev);
1140 break;
1141 }
1142 }
1143}
1144
1145static struct parport_driver ppa_driver = {
1146 .name = "ppa",
1147 .match_port = ppa_attach,
1148 .detach = ppa_detach,
1149 .devmodel = true,
1150};
1151module_parport_driver(ppa_driver);
1152
1153MODULE_LICENSE("GPL");
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 */
10
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/blkdev.h>
16#include <linux/parport.h>
17#include <linux/workqueue.h>
18#include <linux/delay.h>
19#include <linux/jiffies.h>
20#include <asm/io.h>
21
22#include <scsi/scsi.h>
23#include <scsi/scsi_cmnd.h>
24#include <scsi/scsi_device.h>
25#include <scsi/scsi_host.h>
26
27
28static void ppa_reset_pulse(unsigned int base);
29
30typedef struct {
31 struct pardevice *dev; /* Parport device entry */
32 int base; /* Actual port address */
33 int mode; /* Transfer mode */
34 struct scsi_cmnd *cur_cmd; /* Current queued command */
35 struct delayed_work ppa_tq; /* Polling interrupt stuff */
36 unsigned long jstart; /* Jiffies at start */
37 unsigned long recon_tmo; /* How many usecs to wait for reconnection (6th bit) */
38 unsigned int failed:1; /* Failure flag */
39 unsigned wanted:1; /* Parport sharing busy flag */
40 unsigned int dev_no; /* Device number */
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 struct scsi_pointer *ppa_scsi_pointer(struct scsi_cmnd *cmd)
49{
50 return scsi_cmd_priv(cmd);
51}
52
53static inline ppa_struct *ppa_dev(struct Scsi_Host *host)
54{
55 return *(ppa_struct **)&host->hostdata;
56}
57
58static DEFINE_SPINLOCK(arbitration_lock);
59
60static void got_it(ppa_struct *dev)
61{
62 dev->base = dev->dev->port->base;
63 if (dev->cur_cmd)
64 ppa_scsi_pointer(dev->cur_cmd)->phase = 1;
65 else
66 wake_up(dev->waiting);
67}
68
69static void ppa_wakeup(void *ref)
70{
71 ppa_struct *dev = (ppa_struct *) ref;
72 unsigned long flags;
73
74 spin_lock_irqsave(&arbitration_lock, flags);
75 if (dev->wanted) {
76 parport_claim(dev->dev);
77 got_it(dev);
78 dev->wanted = 0;
79 }
80 spin_unlock_irqrestore(&arbitration_lock, flags);
81 return;
82}
83
84static int ppa_pb_claim(ppa_struct *dev)
85{
86 unsigned long flags;
87 int res = 1;
88 spin_lock_irqsave(&arbitration_lock, flags);
89 if (parport_claim(dev->dev) == 0) {
90 got_it(dev);
91 res = 0;
92 }
93 dev->wanted = res;
94 spin_unlock_irqrestore(&arbitration_lock, flags);
95 return res;
96}
97
98static void ppa_pb_dismiss(ppa_struct *dev)
99{
100 unsigned long flags;
101 int wanted;
102 spin_lock_irqsave(&arbitration_lock, flags);
103 wanted = dev->wanted;
104 dev->wanted = 0;
105 spin_unlock_irqrestore(&arbitration_lock, flags);
106 if (!wanted)
107 parport_release(dev->dev);
108}
109
110static inline void ppa_pb_release(ppa_struct *dev)
111{
112 parport_release(dev->dev);
113}
114
115/*
116 * Start of Chipset kludges
117 */
118
119/* This is to give the ppa driver a way to modify the timings (and other
120 * parameters) by writing to the /proc/scsi/ppa/0 file.
121 * Very simple method really... (To simple, no error checking :( )
122 * Reason: Kernel hackers HATE having to unload and reload modules for
123 * testing...
124 * Also gives a method to use a script to obtain optimum timings (TODO)
125 */
126
127static inline int ppa_write_info(struct Scsi_Host *host, char *buffer, int length)
128{
129 ppa_struct *dev = ppa_dev(host);
130 unsigned long x;
131
132 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
133 x = simple_strtoul(buffer + 5, NULL, 0);
134 dev->mode = x;
135 return length;
136 }
137 if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
138 x = simple_strtoul(buffer + 10, NULL, 0);
139 dev->recon_tmo = x;
140 printk(KERN_INFO "ppa: recon_tmo set to %ld\n", x);
141 return length;
142 }
143 printk(KERN_WARNING "ppa /proc: invalid variable\n");
144 return -EINVAL;
145}
146
147static int ppa_show_info(struct seq_file *m, struct Scsi_Host *host)
148{
149 ppa_struct *dev = ppa_dev(host);
150
151 seq_printf(m, "Version : %s\n", PPA_VERSION);
152 seq_printf(m, "Parport : %s\n", dev->dev->port->name);
153 seq_printf(m, "Mode : %s\n", PPA_MODE_STRING[dev->mode]);
154#if PPA_DEBUG > 0
155 seq_printf(m, "recon_tmo : %lu\n", dev->recon_tmo);
156#endif
157 return 0;
158}
159
160static int device_check(ppa_struct *dev);
161
162#if PPA_DEBUG > 0
163#define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
164 y, __func__, __LINE__); ppa_fail_func(x,y);
165static inline void ppa_fail_func(ppa_struct *dev, int error_code)
166#else
167static inline void ppa_fail(ppa_struct *dev, int error_code)
168#endif
169{
170 /* If we fail a device then we trash status / message bytes */
171 if (dev->cur_cmd) {
172 dev->cur_cmd->result = error_code << 16;
173 dev->failed = 1;
174 }
175}
176
177/*
178 * Wait for the high bit to be set.
179 *
180 * In principle, this could be tied to an interrupt, but the adapter
181 * doesn't appear to be designed to support interrupts. We spin on
182 * the 0x80 ready bit.
183 */
184static unsigned char ppa_wait(ppa_struct *dev)
185{
186 int k;
187 unsigned short ppb = dev->base;
188 unsigned char r;
189
190 k = PPA_SPIN_TMO;
191 /* Wait for bit 6 and 7 - PJC */
192 for (r = r_str(ppb); ((r & 0xc0) != 0xc0) && (k); k--) {
193 udelay(1);
194 r = r_str(ppb);
195 }
196
197 /*
198 * return some status information.
199 * Semantics: 0xc0 = ZIP wants more data
200 * 0xd0 = ZIP wants to send more data
201 * 0xe0 = ZIP is expecting SCSI command data
202 * 0xf0 = end of transfer, ZIP is sending status
203 */
204 if (k)
205 return (r & 0xf0);
206
207 /* Counter expired - Time out occurred */
208 ppa_fail(dev, DID_TIME_OUT);
209 printk(KERN_WARNING "ppa timeout in ppa_wait\n");
210 return 0; /* command timed out */
211}
212
213/*
214 * Clear EPP Timeout Bit
215 */
216static inline void epp_reset(unsigned short ppb)
217{
218 int i;
219
220 i = r_str(ppb);
221 w_str(ppb, i);
222 w_str(ppb, i & 0xfe);
223}
224
225/*
226 * Wait for empty ECP fifo (if we are in ECP fifo mode only)
227 */
228static inline void ecp_sync(ppa_struct *dev)
229{
230 int i, ppb_hi = dev->dev->port->base_hi;
231
232 if (ppb_hi == 0)
233 return;
234
235 if ((r_ecr(ppb_hi) & 0xe0) == 0x60) { /* mode 011 == ECP fifo mode */
236 for (i = 0; i < 100; i++) {
237 if (r_ecr(ppb_hi) & 0x01)
238 return;
239 udelay(5);
240 }
241 printk(KERN_WARNING "ppa: ECP sync failed as data still present in FIFO.\n");
242 }
243}
244
245static int ppa_byte_out(unsigned short base, const char *buffer, int len)
246{
247 int i;
248
249 for (i = len; i; i--) {
250 w_dtr(base, *buffer++);
251 w_ctr(base, 0xe);
252 w_ctr(base, 0xc);
253 }
254 return 1; /* All went well - we hope! */
255}
256
257static int ppa_byte_in(unsigned short base, char *buffer, int len)
258{
259 int i;
260
261 for (i = len; i; i--) {
262 *buffer++ = r_dtr(base);
263 w_ctr(base, 0x27);
264 w_ctr(base, 0x25);
265 }
266 return 1; /* All went well - we hope! */
267}
268
269static int ppa_nibble_in(unsigned short base, char *buffer, int len)
270{
271 for (; len; len--) {
272 unsigned char h;
273
274 w_ctr(base, 0x4);
275 h = r_str(base) & 0xf0;
276 w_ctr(base, 0x6);
277 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
278 }
279 return 1; /* All went well - we hope! */
280}
281
282static int ppa_out(ppa_struct *dev, char *buffer, int len)
283{
284 int r;
285 unsigned short ppb = dev->base;
286
287 r = ppa_wait(dev);
288
289 if ((r & 0x50) != 0x40) {
290 ppa_fail(dev, DID_ERROR);
291 return 0;
292 }
293 switch (dev->mode) {
294 case PPA_NIBBLE:
295 case PPA_PS2:
296 /* 8 bit output, with a loop */
297 r = ppa_byte_out(ppb, buffer, len);
298 break;
299
300 case PPA_EPP_32:
301 case PPA_EPP_16:
302 case PPA_EPP_8:
303 epp_reset(ppb);
304 w_ctr(ppb, 0x4);
305#ifdef CONFIG_SCSI_IZIP_EPP16
306 if (!(((long) buffer | len) & 0x01))
307 outsw(ppb + 4, buffer, len >> 1);
308#else
309 if (!(((long) buffer | len) & 0x03))
310 outsl(ppb + 4, buffer, len >> 2);
311#endif
312 else
313 outsb(ppb + 4, buffer, len);
314 w_ctr(ppb, 0xc);
315 r = !(r_str(ppb) & 0x01);
316 w_ctr(ppb, 0xc);
317 ecp_sync(dev);
318 break;
319
320 default:
321 printk(KERN_ERR "PPA: bug in ppa_out()\n");
322 r = 0;
323 }
324 return r;
325}
326
327static int ppa_in(ppa_struct *dev, char *buffer, int len)
328{
329 int r;
330 unsigned short ppb = dev->base;
331
332 r = ppa_wait(dev);
333
334 if ((r & 0x50) != 0x50) {
335 ppa_fail(dev, DID_ERROR);
336 return 0;
337 }
338 switch (dev->mode) {
339 case PPA_NIBBLE:
340 /* 4 bit input, with a loop */
341 r = ppa_nibble_in(ppb, buffer, len);
342 w_ctr(ppb, 0xc);
343 break;
344
345 case PPA_PS2:
346 /* 8 bit input, with a loop */
347 w_ctr(ppb, 0x25);
348 r = ppa_byte_in(ppb, buffer, len);
349 w_ctr(ppb, 0x4);
350 w_ctr(ppb, 0xc);
351 break;
352
353 case PPA_EPP_32:
354 case PPA_EPP_16:
355 case PPA_EPP_8:
356 epp_reset(ppb);
357 w_ctr(ppb, 0x24);
358#ifdef CONFIG_SCSI_IZIP_EPP16
359 if (!(((long) buffer | len) & 0x01))
360 insw(ppb + 4, buffer, len >> 1);
361#else
362 if (!(((long) buffer | len) & 0x03))
363 insl(ppb + 4, buffer, len >> 2);
364#endif
365 else
366 insb(ppb + 4, buffer, len);
367 w_ctr(ppb, 0x2c);
368 r = !(r_str(ppb) & 0x01);
369 w_ctr(ppb, 0x2c);
370 ecp_sync(dev);
371 break;
372
373 default:
374 printk(KERN_ERR "PPA: bug in ppa_ins()\n");
375 r = 0;
376 break;
377 }
378 return r;
379}
380
381/* end of ppa_io.h */
382static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
383{
384 w_dtr(ppb, b);
385 w_ctr(ppb, 0xc);
386 w_ctr(ppb, 0xe);
387 w_ctr(ppb, 0xc);
388 w_ctr(ppb, 0x4);
389 w_ctr(ppb, 0xc);
390}
391
392static void ppa_disconnect(ppa_struct *dev)
393{
394 unsigned short ppb = dev->base;
395
396 ppa_d_pulse(ppb, 0);
397 ppa_d_pulse(ppb, 0x3c);
398 ppa_d_pulse(ppb, 0x20);
399 ppa_d_pulse(ppb, 0xf);
400}
401
402static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
403{
404 w_dtr(ppb, b);
405 w_ctr(ppb, 0x4);
406 w_ctr(ppb, 0x6);
407 w_ctr(ppb, 0x4);
408 w_ctr(ppb, 0xc);
409}
410
411static inline void ppa_connect(ppa_struct *dev, int flag)
412{
413 unsigned short ppb = dev->base;
414
415 ppa_c_pulse(ppb, 0);
416 ppa_c_pulse(ppb, 0x3c);
417 ppa_c_pulse(ppb, 0x20);
418 if ((flag == CONNECT_EPP_MAYBE) && IN_EPP_MODE(dev->mode))
419 ppa_c_pulse(ppb, 0xcf);
420 else
421 ppa_c_pulse(ppb, 0x8f);
422}
423
424static int ppa_select(ppa_struct *dev, int target)
425{
426 int k;
427 unsigned short ppb = dev->base;
428
429 /*
430 * Bit 6 (0x40) is the device selected bit.
431 * First we must wait till the current device goes off line...
432 */
433 k = PPA_SELECT_TMO;
434 do {
435 k--;
436 udelay(1);
437 } while ((r_str(ppb) & 0x40) && (k));
438 if (!k)
439 return 0;
440
441 w_dtr(ppb, (1 << target));
442 w_ctr(ppb, 0xe);
443 w_ctr(ppb, 0xc);
444 w_dtr(ppb, 0x80); /* This is NOT the initator */
445 w_ctr(ppb, 0x8);
446
447 k = PPA_SELECT_TMO;
448 do {
449 k--;
450 udelay(1);
451 }
452 while (!(r_str(ppb) & 0x40) && (k));
453 if (!k)
454 return 0;
455
456 return 1;
457}
458
459/*
460 * This is based on a trace of what the Iomega DOS 'guest' driver does.
461 * I've tried several different kinds of parallel ports with guest and
462 * coded this to react in the same ways that it does.
463 *
464 * The return value from this function is just a hint about where the
465 * handshaking failed.
466 *
467 */
468static int ppa_init(ppa_struct *dev)
469{
470 int retv;
471 unsigned short ppb = dev->base;
472
473 ppa_disconnect(dev);
474 ppa_connect(dev, CONNECT_NORMAL);
475
476 retv = 2; /* Failed */
477
478 w_ctr(ppb, 0xe);
479 if ((r_str(ppb) & 0x08) == 0x08)
480 retv--;
481
482 w_ctr(ppb, 0xc);
483 if ((r_str(ppb) & 0x08) == 0x00)
484 retv--;
485
486 if (!retv)
487 ppa_reset_pulse(ppb);
488 udelay(1000); /* Allow devices to settle down */
489 ppa_disconnect(dev);
490 udelay(1000); /* Another delay to allow devices to settle */
491
492 if (retv)
493 return -EIO;
494
495 return device_check(dev);
496}
497
498static inline int ppa_send_command(struct scsi_cmnd *cmd)
499{
500 ppa_struct *dev = ppa_dev(cmd->device->host);
501 int k;
502
503 w_ctr(dev->base, 0x0c);
504
505 for (k = 0; k < cmd->cmd_len; k++)
506 if (!ppa_out(dev, &cmd->cmnd[k], 1))
507 return 0;
508 return 1;
509}
510
511/*
512 * The bulk flag enables some optimisations in the data transfer loops,
513 * it should be true for any command that transfers data in integral
514 * numbers of sectors.
515 *
516 * The driver appears to remain stable if we speed up the parallel port
517 * i/o in this function, but not elsewhere.
518 */
519static int ppa_completion(struct scsi_cmnd *const cmd)
520{
521 /* Return codes:
522 * -1 Error
523 * 0 Told to schedule
524 * 1 Finished data transfer
525 */
526 struct scsi_pointer *scsi_pointer = ppa_scsi_pointer(cmd);
527 ppa_struct *dev = ppa_dev(cmd->device->host);
528 unsigned short ppb = dev->base;
529 unsigned long start_jiffies = jiffies;
530
531 unsigned char r, v;
532 int fast, bulk, status;
533
534 v = cmd->cmnd[0];
535 bulk = ((v == READ_6) ||
536 (v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
537
538 /*
539 * We only get here if the drive is ready to comunicate,
540 * hence no need for a full ppa_wait.
541 */
542 r = (r_str(ppb) & 0xf0);
543
544 while (r != (unsigned char) 0xf0) {
545 /*
546 * If we have been running for more than a full timer tick
547 * then take a rest.
548 */
549 if (time_after(jiffies, start_jiffies + 1))
550 return 0;
551
552 if (scsi_pointer->this_residual <= 0) {
553 ppa_fail(dev, DID_ERROR);
554 return -1; /* ERROR_RETURN */
555 }
556
557 /* On some hardware we have SCSI disconnected (6th bit low)
558 * for about 100usecs. It is too expensive to wait a
559 * tick on every loop so we busy wait for no more than
560 * 500usecs to give the drive a chance first. We do not
561 * change things for "normal" hardware since generally
562 * the 6th bit is always high.
563 * This makes the CPU load higher on some hardware
564 * but otherwise we can not get more than 50K/secs
565 * on this problem hardware.
566 */
567 if ((r & 0xc0) != 0xc0) {
568 /* Wait for reconnection should be no more than
569 * jiffy/2 = 5ms = 5000 loops
570 */
571 unsigned long k = dev->recon_tmo;
572 for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
573 k--)
574 udelay(1);
575
576 if (!k)
577 return 0;
578 }
579
580 /* determine if we should use burst I/O */
581 fast = bulk && scsi_pointer->this_residual >= PPA_BURST_SIZE ?
582 PPA_BURST_SIZE : 1;
583
584 if (r == (unsigned char) 0xc0)
585 status = ppa_out(dev, scsi_pointer->ptr, fast);
586 else
587 status = ppa_in(dev, scsi_pointer->ptr, fast);
588
589 scsi_pointer->ptr += fast;
590 scsi_pointer->this_residual -= fast;
591
592 if (!status) {
593 ppa_fail(dev, DID_BUS_BUSY);
594 return -1; /* ERROR_RETURN */
595 }
596 if (scsi_pointer->buffer && !scsi_pointer->this_residual) {
597 /* if scatter/gather, advance to the next segment */
598 if (scsi_pointer->buffers_residual--) {
599 scsi_pointer->buffer =
600 sg_next(scsi_pointer->buffer);
601 scsi_pointer->this_residual =
602 scsi_pointer->buffer->length;
603 scsi_pointer->ptr =
604 sg_virt(scsi_pointer->buffer);
605 }
606 }
607 /* Now check to see if the drive is ready to comunicate */
608 r = (r_str(ppb) & 0xf0);
609 /* If not, drop back down to the scheduler and wait a timer tick */
610 if (!(r & 0x80))
611 return 0;
612 }
613 return 1; /* FINISH_RETURN */
614}
615
616/*
617 * Since the PPA itself doesn't generate interrupts, we use
618 * the scheduler's task queue to generate a stream of call-backs and
619 * complete the request when the drive is ready.
620 */
621static void ppa_interrupt(struct work_struct *work)
622{
623 ppa_struct *dev = container_of(work, ppa_struct, ppa_tq.work);
624 struct scsi_cmnd *cmd = dev->cur_cmd;
625
626 if (!cmd) {
627 printk(KERN_ERR "PPA: bug in ppa_interrupt\n");
628 return;
629 }
630 if (ppa_engine(dev, cmd)) {
631 schedule_delayed_work(&dev->ppa_tq, 1);
632 return;
633 }
634 /* Command must of completed hence it is safe to let go... */
635#if PPA_DEBUG > 0
636 switch ((cmd->result >> 16) & 0xff) {
637 case DID_OK:
638 break;
639 case DID_NO_CONNECT:
640 printk(KERN_DEBUG "ppa: no device at SCSI ID %i\n", cmd->device->target);
641 break;
642 case DID_BUS_BUSY:
643 printk(KERN_DEBUG "ppa: BUS BUSY - EPP timeout detected\n");
644 break;
645 case DID_TIME_OUT:
646 printk(KERN_DEBUG "ppa: unknown timeout\n");
647 break;
648 case DID_ABORT:
649 printk(KERN_DEBUG "ppa: told to abort\n");
650 break;
651 case DID_PARITY:
652 printk(KERN_DEBUG "ppa: parity error (???)\n");
653 break;
654 case DID_ERROR:
655 printk(KERN_DEBUG "ppa: internal driver error\n");
656 break;
657 case DID_RESET:
658 printk(KERN_DEBUG "ppa: told to reset device\n");
659 break;
660 case DID_BAD_INTR:
661 printk(KERN_WARNING "ppa: bad interrupt (???)\n");
662 break;
663 default:
664 printk(KERN_WARNING "ppa: bad return code (%02x)\n",
665 (cmd->result >> 16) & 0xff);
666 }
667#endif
668
669 if (ppa_scsi_pointer(cmd)->phase > 1)
670 ppa_disconnect(dev);
671
672 ppa_pb_dismiss(dev);
673
674 dev->cur_cmd = NULL;
675
676 scsi_done(cmd);
677}
678
679static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
680{
681 struct scsi_pointer *scsi_pointer = ppa_scsi_pointer(cmd);
682 unsigned short ppb = dev->base;
683 unsigned char l = 0, h = 0;
684 int retv;
685
686 /* First check for any errors that may of occurred
687 * Here we check for internal errors
688 */
689 if (dev->failed)
690 return 0;
691
692 switch (scsi_pointer->phase) {
693 case 0: /* Phase 0 - Waiting for parport */
694 if (time_after(jiffies, dev->jstart + HZ)) {
695 /*
696 * We waited more than a second
697 * for parport to call us
698 */
699 ppa_fail(dev, DID_BUS_BUSY);
700 return 0;
701 }
702 return 1; /* wait until ppa_wakeup claims parport */
703 case 1: /* Phase 1 - Connected */
704 { /* Perform a sanity check for cable unplugged */
705 int retv = 2; /* Failed */
706
707 ppa_connect(dev, CONNECT_EPP_MAYBE);
708
709 w_ctr(ppb, 0xe);
710 if ((r_str(ppb) & 0x08) == 0x08)
711 retv--;
712
713 w_ctr(ppb, 0xc);
714 if ((r_str(ppb) & 0x08) == 0x00)
715 retv--;
716
717 if (retv) {
718 if (time_after(jiffies, dev->jstart + (1 * HZ))) {
719 printk(KERN_ERR "ppa: Parallel port cable is unplugged.\n");
720 ppa_fail(dev, DID_BUS_BUSY);
721 return 0;
722 } else {
723 ppa_disconnect(dev);
724 return 1; /* Try again in a jiffy */
725 }
726 }
727 scsi_pointer->phase++;
728 }
729 fallthrough;
730
731 case 2: /* Phase 2 - We are now talking to the scsi bus */
732 if (!ppa_select(dev, scmd_id(cmd))) {
733 ppa_fail(dev, DID_NO_CONNECT);
734 return 0;
735 }
736 scsi_pointer->phase++;
737 fallthrough;
738
739 case 3: /* Phase 3 - Ready to accept a command */
740 w_ctr(ppb, 0x0c);
741 if (!(r_str(ppb) & 0x80))
742 return 1;
743
744 if (!ppa_send_command(cmd))
745 return 0;
746 scsi_pointer->phase++;
747 fallthrough;
748
749 case 4: /* Phase 4 - Setup scatter/gather buffers */
750 if (scsi_bufflen(cmd)) {
751 scsi_pointer->buffer = scsi_sglist(cmd);
752 scsi_pointer->this_residual =
753 scsi_pointer->buffer->length;
754 scsi_pointer->ptr = sg_virt(scsi_pointer->buffer);
755 } else {
756 scsi_pointer->buffer = NULL;
757 scsi_pointer->this_residual = 0;
758 scsi_pointer->ptr = NULL;
759 }
760 scsi_pointer->buffers_residual = scsi_sg_count(cmd) - 1;
761 scsi_pointer->phase++;
762 fallthrough;
763
764 case 5: /* Phase 5 - Data transfer stage */
765 w_ctr(ppb, 0x0c);
766 if (!(r_str(ppb) & 0x80))
767 return 1;
768
769 retv = ppa_completion(cmd);
770 if (retv == -1)
771 return 0;
772 if (retv == 0)
773 return 1;
774 scsi_pointer->phase++;
775 fallthrough;
776
777 case 6: /* Phase 6 - Read status/message */
778 cmd->result = DID_OK << 16;
779 /* Check for data overrun */
780 if (ppa_wait(dev) != (unsigned char) 0xf0) {
781 ppa_fail(dev, DID_ERROR);
782 return 0;
783 }
784 if (ppa_in(dev, &l, 1)) { /* read status byte */
785 /* Check for optional message byte */
786 if (ppa_wait(dev) == (unsigned char) 0xf0)
787 ppa_in(dev, &h, 1);
788 cmd->result =
789 (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
790 }
791 return 0; /* Finished */
792
793 default:
794 printk(KERN_ERR "ppa: Invalid scsi phase\n");
795 }
796 return 0;
797}
798
799static int ppa_queuecommand_lck(struct scsi_cmnd *cmd)
800{
801 ppa_struct *dev = ppa_dev(cmd->device->host);
802
803 if (dev->cur_cmd) {
804 printk(KERN_ERR "PPA: bug in ppa_queuecommand\n");
805 return 0;
806 }
807 dev->failed = 0;
808 dev->jstart = jiffies;
809 dev->cur_cmd = cmd;
810 cmd->result = DID_ERROR << 16; /* default return code */
811 ppa_scsi_pointer(cmd)->phase = 0; /* bus free */
812
813 schedule_delayed_work(&dev->ppa_tq, 0);
814
815 ppa_pb_claim(dev);
816
817 return 0;
818}
819
820static DEF_SCSI_QCMD(ppa_queuecommand)
821
822/*
823 * Apparently the disk->capacity attribute is off by 1 sector
824 * for all disk drives. We add the one here, but it should really
825 * be done in sd.c. Even if it gets fixed there, this will still
826 * work.
827 */
828static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
829 sector_t capacity, int ip[])
830{
831 ip[0] = 0x40;
832 ip[1] = 0x20;
833 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
834 if (ip[2] > 1024) {
835 ip[0] = 0xff;
836 ip[1] = 0x3f;
837 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
838 if (ip[2] > 1023)
839 ip[2] = 1023;
840 }
841 return 0;
842}
843
844static int ppa_abort(struct scsi_cmnd *cmd)
845{
846 ppa_struct *dev = ppa_dev(cmd->device->host);
847 /*
848 * There is no method for aborting commands since Iomega
849 * have tied the SCSI_MESSAGE line high in the interface
850 */
851
852 switch (ppa_scsi_pointer(cmd)->phase) {
853 case 0: /* Do not have access to parport */
854 case 1: /* Have not connected to interface */
855 dev->cur_cmd = NULL; /* Forget the problem */
856 return SUCCESS;
857 default: /* SCSI command sent, can not abort */
858 return FAILED;
859 }
860}
861
862static void ppa_reset_pulse(unsigned int base)
863{
864 w_dtr(base, 0x40);
865 w_ctr(base, 0x8);
866 udelay(30);
867 w_ctr(base, 0xc);
868}
869
870static int ppa_reset(struct scsi_cmnd *cmd)
871{
872 ppa_struct *dev = ppa_dev(cmd->device->host);
873
874 if (ppa_scsi_pointer(cmd)->phase)
875 ppa_disconnect(dev);
876 dev->cur_cmd = NULL; /* Forget the problem */
877
878 ppa_connect(dev, CONNECT_NORMAL);
879 ppa_reset_pulse(dev->base);
880 mdelay(1); /* device settle delay */
881 ppa_disconnect(dev);
882 mdelay(1); /* device settle delay */
883 return SUCCESS;
884}
885
886static int device_check(ppa_struct *dev)
887{
888 /* This routine looks for a device and then attempts to use EPP
889 to send a command. If all goes as planned then EPP is available. */
890
891 static u8 cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
892 int loop, old_mode, status, k, ppb = dev->base;
893 unsigned char l;
894
895 old_mode = dev->mode;
896 for (loop = 0; loop < 8; loop++) {
897 /* Attempt to use EPP for Test Unit Ready */
898 if ((ppb & 0x0007) == 0x0000)
899 dev->mode = PPA_EPP_32;
900
901second_pass:
902 ppa_connect(dev, CONNECT_EPP_MAYBE);
903 /* Select SCSI device */
904 if (!ppa_select(dev, loop)) {
905 ppa_disconnect(dev);
906 continue;
907 }
908 printk(KERN_INFO "ppa: Found device at ID %i, Attempting to use %s\n",
909 loop, PPA_MODE_STRING[dev->mode]);
910
911 /* Send SCSI command */
912 status = 1;
913 w_ctr(ppb, 0x0c);
914 for (l = 0; (l < 6) && (status); l++)
915 status = ppa_out(dev, cmd, 1);
916
917 if (!status) {
918 ppa_disconnect(dev);
919 ppa_connect(dev, CONNECT_EPP_MAYBE);
920 w_dtr(ppb, 0x40);
921 w_ctr(ppb, 0x08);
922 udelay(30);
923 w_ctr(ppb, 0x0c);
924 udelay(1000);
925 ppa_disconnect(dev);
926 udelay(1000);
927 if (dev->mode == PPA_EPP_32) {
928 dev->mode = old_mode;
929 goto second_pass;
930 }
931 return -EIO;
932 }
933 w_ctr(ppb, 0x0c);
934 k = 1000000; /* 1 Second */
935 do {
936 l = r_str(ppb);
937 k--;
938 udelay(1);
939 } while (!(l & 0x80) && (k));
940
941 l &= 0xf0;
942
943 if (l != 0xf0) {
944 ppa_disconnect(dev);
945 ppa_connect(dev, CONNECT_EPP_MAYBE);
946 ppa_reset_pulse(ppb);
947 udelay(1000);
948 ppa_disconnect(dev);
949 udelay(1000);
950 if (dev->mode == PPA_EPP_32) {
951 dev->mode = old_mode;
952 goto second_pass;
953 }
954 return -EIO;
955 }
956 ppa_disconnect(dev);
957 printk(KERN_INFO "ppa: Communication established with ID %i using %s\n",
958 loop, PPA_MODE_STRING[dev->mode]);
959 ppa_connect(dev, CONNECT_EPP_MAYBE);
960 ppa_reset_pulse(ppb);
961 udelay(1000);
962 ppa_disconnect(dev);
963 udelay(1000);
964 return 0;
965 }
966 return -ENODEV;
967}
968
969static int ppa_adjust_queue(struct scsi_device *device)
970{
971 blk_queue_bounce_limit(device->request_queue, BLK_BOUNCE_HIGH);
972 return 0;
973}
974
975static struct scsi_host_template ppa_template = {
976 .module = THIS_MODULE,
977 .proc_name = "ppa",
978 .show_info = ppa_show_info,
979 .write_info = ppa_write_info,
980 .name = "Iomega VPI0 (ppa) interface",
981 .queuecommand = ppa_queuecommand,
982 .eh_abort_handler = ppa_abort,
983 .eh_host_reset_handler = ppa_reset,
984 .bios_param = ppa_biosparam,
985 .this_id = -1,
986 .sg_tablesize = SG_ALL,
987 .can_queue = 1,
988 .slave_alloc = ppa_adjust_queue,
989 .cmd_size = sizeof(struct scsi_pointer),
990};
991
992/***************************************************************************
993 * Parallel port probing routines *
994 ***************************************************************************/
995
996static LIST_HEAD(ppa_hosts);
997
998/*
999 * Finds the first available device number that can be alloted to the
1000 * new ppa device and returns the address of the previous node so that
1001 * we can add to the tail and have a list in the ascending order.
1002 */
1003
1004static inline ppa_struct *find_parent(void)
1005{
1006 ppa_struct *dev, *par = NULL;
1007 unsigned int cnt = 0;
1008
1009 if (list_empty(&ppa_hosts))
1010 return NULL;
1011
1012 list_for_each_entry(dev, &ppa_hosts, list) {
1013 if (dev->dev_no != cnt)
1014 return par;
1015 cnt++;
1016 par = dev;
1017 }
1018
1019 return par;
1020}
1021
1022static int __ppa_attach(struct parport *pb)
1023{
1024 struct Scsi_Host *host;
1025 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waiting);
1026 DEFINE_WAIT(wait);
1027 ppa_struct *dev, *temp;
1028 int ports;
1029 int modes, ppb, ppb_hi;
1030 int err = -ENOMEM;
1031 struct pardev_cb ppa_cb;
1032
1033 dev = kzalloc(sizeof(ppa_struct), GFP_KERNEL);
1034 if (!dev)
1035 return -ENOMEM;
1036 dev->base = -1;
1037 dev->mode = PPA_AUTODETECT;
1038 dev->recon_tmo = PPA_RECON_TMO;
1039 init_waitqueue_head(&waiting);
1040 temp = find_parent();
1041 if (temp)
1042 dev->dev_no = temp->dev_no + 1;
1043
1044 memset(&ppa_cb, 0, sizeof(ppa_cb));
1045 ppa_cb.private = dev;
1046 ppa_cb.wakeup = ppa_wakeup;
1047
1048 dev->dev = parport_register_dev_model(pb, "ppa", &ppa_cb, dev->dev_no);
1049
1050 if (!dev->dev)
1051 goto out;
1052
1053 /* Claim the bus so it remembers what we do to the control
1054 * registers. [ CTR and ECP ]
1055 */
1056 err = -EBUSY;
1057 dev->waiting = &waiting;
1058 prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
1059 if (ppa_pb_claim(dev))
1060 schedule_timeout(3 * HZ);
1061 if (dev->wanted) {
1062 printk(KERN_ERR "ppa%d: failed to claim parport because "
1063 "a pardevice is owning the port for too long "
1064 "time!\n", pb->number);
1065 ppa_pb_dismiss(dev);
1066 dev->waiting = NULL;
1067 finish_wait(&waiting, &wait);
1068 goto out1;
1069 }
1070 dev->waiting = NULL;
1071 finish_wait(&waiting, &wait);
1072 ppb = dev->base = dev->dev->port->base;
1073 ppb_hi = dev->dev->port->base_hi;
1074 w_ctr(ppb, 0x0c);
1075 modes = dev->dev->port->modes;
1076
1077 /* Mode detection works up the chain of speed
1078 * This avoids a nasty if-then-else-if-... tree
1079 */
1080 dev->mode = PPA_NIBBLE;
1081
1082 if (modes & PARPORT_MODE_TRISTATE)
1083 dev->mode = PPA_PS2;
1084
1085 if (modes & PARPORT_MODE_ECP) {
1086 w_ecr(ppb_hi, 0x20);
1087 dev->mode = PPA_PS2;
1088 }
1089 if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
1090 w_ecr(ppb_hi, 0x80);
1091
1092 /* Done configuration */
1093
1094 err = ppa_init(dev);
1095 ppa_pb_release(dev);
1096
1097 if (err)
1098 goto out1;
1099
1100 /* now the glue ... */
1101 if (dev->mode == PPA_NIBBLE || dev->mode == PPA_PS2)
1102 ports = 3;
1103 else
1104 ports = 8;
1105
1106 INIT_DELAYED_WORK(&dev->ppa_tq, ppa_interrupt);
1107
1108 err = -ENOMEM;
1109 host = scsi_host_alloc(&ppa_template, sizeof(ppa_struct *));
1110 if (!host)
1111 goto out1;
1112 host->io_port = pb->base;
1113 host->n_io_port = ports;
1114 host->dma_channel = -1;
1115 host->unique_id = pb->number;
1116 *(ppa_struct **)&host->hostdata = dev;
1117 dev->host = host;
1118 list_add_tail(&dev->list, &ppa_hosts);
1119 err = scsi_add_host(host, NULL);
1120 if (err)
1121 goto out2;
1122 scsi_scan_host(host);
1123 return 0;
1124out2:
1125 list_del_init(&dev->list);
1126 scsi_host_put(host);
1127out1:
1128 parport_unregister_device(dev->dev);
1129out:
1130 kfree(dev);
1131 return err;
1132}
1133
1134static void ppa_attach(struct parport *pb)
1135{
1136 __ppa_attach(pb);
1137}
1138
1139static void ppa_detach(struct parport *pb)
1140{
1141 ppa_struct *dev;
1142 list_for_each_entry(dev, &ppa_hosts, list) {
1143 if (dev->dev->port == pb) {
1144 list_del_init(&dev->list);
1145 scsi_remove_host(dev->host);
1146 scsi_host_put(dev->host);
1147 parport_unregister_device(dev->dev);
1148 kfree(dev);
1149 break;
1150 }
1151 }
1152}
1153
1154static struct parport_driver ppa_driver = {
1155 .name = "ppa",
1156 .match_port = ppa_attach,
1157 .detach = ppa_detach,
1158 .devmodel = true,
1159};
1160module_parport_driver(ppa_driver);
1161
1162MODULE_LICENSE("GPL");