0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/init.h>
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/module.h>
0015 #include <linux/blkdev.h>
0016 #include <linux/parport.h>
0017 #include <linux/workqueue.h>
0018 #include <linux/delay.h>
0019 #include <linux/jiffies.h>
0020 #include <asm/io.h>
0021
0022 #include <scsi/scsi.h>
0023 #include <scsi/scsi_cmnd.h>
0024 #include <scsi/scsi_device.h>
0025 #include <scsi/scsi_host.h>
0026
0027
0028 static void ppa_reset_pulse(unsigned int base);
0029
0030 typedef struct {
0031 struct pardevice *dev;
0032 int base;
0033 int mode;
0034 struct scsi_cmnd *cur_cmd;
0035 struct delayed_work ppa_tq;
0036 unsigned long jstart;
0037 unsigned long recon_tmo;
0038 unsigned int failed:1;
0039 unsigned wanted:1;
0040 unsigned int dev_no;
0041 wait_queue_head_t *waiting;
0042 struct Scsi_Host *host;
0043 struct list_head list;
0044 } ppa_struct;
0045
0046 #include "ppa.h"
0047
0048 static struct scsi_pointer *ppa_scsi_pointer(struct scsi_cmnd *cmd)
0049 {
0050 return scsi_cmd_priv(cmd);
0051 }
0052
0053 static inline ppa_struct *ppa_dev(struct Scsi_Host *host)
0054 {
0055 return *(ppa_struct **)&host->hostdata;
0056 }
0057
0058 static DEFINE_SPINLOCK(arbitration_lock);
0059
0060 static void got_it(ppa_struct *dev)
0061 {
0062 dev->base = dev->dev->port->base;
0063 if (dev->cur_cmd)
0064 ppa_scsi_pointer(dev->cur_cmd)->phase = 1;
0065 else
0066 wake_up(dev->waiting);
0067 }
0068
0069 static void ppa_wakeup(void *ref)
0070 {
0071 ppa_struct *dev = (ppa_struct *) ref;
0072 unsigned long flags;
0073
0074 spin_lock_irqsave(&arbitration_lock, flags);
0075 if (dev->wanted) {
0076 parport_claim(dev->dev);
0077 got_it(dev);
0078 dev->wanted = 0;
0079 }
0080 spin_unlock_irqrestore(&arbitration_lock, flags);
0081 return;
0082 }
0083
0084 static int ppa_pb_claim(ppa_struct *dev)
0085 {
0086 unsigned long flags;
0087 int res = 1;
0088 spin_lock_irqsave(&arbitration_lock, flags);
0089 if (parport_claim(dev->dev) == 0) {
0090 got_it(dev);
0091 res = 0;
0092 }
0093 dev->wanted = res;
0094 spin_unlock_irqrestore(&arbitration_lock, flags);
0095 return res;
0096 }
0097
0098 static void ppa_pb_dismiss(ppa_struct *dev)
0099 {
0100 unsigned long flags;
0101 int wanted;
0102 spin_lock_irqsave(&arbitration_lock, flags);
0103 wanted = dev->wanted;
0104 dev->wanted = 0;
0105 spin_unlock_irqrestore(&arbitration_lock, flags);
0106 if (!wanted)
0107 parport_release(dev->dev);
0108 }
0109
0110 static inline void ppa_pb_release(ppa_struct *dev)
0111 {
0112 parport_release(dev->dev);
0113 }
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127 static inline int ppa_write_info(struct Scsi_Host *host, char *buffer, int length)
0128 {
0129 ppa_struct *dev = ppa_dev(host);
0130 unsigned long x;
0131
0132 if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
0133 x = simple_strtoul(buffer + 5, NULL, 0);
0134 dev->mode = x;
0135 return length;
0136 }
0137 if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
0138 x = simple_strtoul(buffer + 10, NULL, 0);
0139 dev->recon_tmo = x;
0140 printk(KERN_INFO "ppa: recon_tmo set to %ld\n", x);
0141 return length;
0142 }
0143 printk(KERN_WARNING "ppa /proc: invalid variable\n");
0144 return -EINVAL;
0145 }
0146
0147 static int ppa_show_info(struct seq_file *m, struct Scsi_Host *host)
0148 {
0149 ppa_struct *dev = ppa_dev(host);
0150
0151 seq_printf(m, "Version : %s\n", PPA_VERSION);
0152 seq_printf(m, "Parport : %s\n", dev->dev->port->name);
0153 seq_printf(m, "Mode : %s\n", PPA_MODE_STRING[dev->mode]);
0154 #if PPA_DEBUG > 0
0155 seq_printf(m, "recon_tmo : %lu\n", dev->recon_tmo);
0156 #endif
0157 return 0;
0158 }
0159
0160 static int device_check(ppa_struct *dev);
0161
0162 #if PPA_DEBUG > 0
0163 #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
0164 y, __func__, __LINE__); ppa_fail_func(x,y);
0165 static inline void ppa_fail_func(ppa_struct *dev, int error_code)
0166 #else
0167 static inline void ppa_fail(ppa_struct *dev, int error_code)
0168 #endif
0169 {
0170
0171 if (dev->cur_cmd) {
0172 dev->cur_cmd->result = error_code << 16;
0173 dev->failed = 1;
0174 }
0175 }
0176
0177
0178
0179
0180
0181
0182
0183
0184 static unsigned char ppa_wait(ppa_struct *dev)
0185 {
0186 int k;
0187 unsigned short ppb = dev->base;
0188 unsigned char r;
0189
0190 k = PPA_SPIN_TMO;
0191
0192 for (r = r_str(ppb); ((r & 0xc0) != 0xc0) && (k); k--) {
0193 udelay(1);
0194 r = r_str(ppb);
0195 }
0196
0197
0198
0199
0200
0201
0202
0203
0204 if (k)
0205 return (r & 0xf0);
0206
0207
0208 ppa_fail(dev, DID_TIME_OUT);
0209 printk(KERN_WARNING "ppa timeout in ppa_wait\n");
0210 return 0;
0211 }
0212
0213
0214
0215
0216 static inline void epp_reset(unsigned short ppb)
0217 {
0218 int i;
0219
0220 i = r_str(ppb);
0221 w_str(ppb, i);
0222 w_str(ppb, i & 0xfe);
0223 }
0224
0225
0226
0227
0228 static inline void ecp_sync(ppa_struct *dev)
0229 {
0230 int i, ppb_hi = dev->dev->port->base_hi;
0231
0232 if (ppb_hi == 0)
0233 return;
0234
0235 if ((r_ecr(ppb_hi) & 0xe0) == 0x60) {
0236 for (i = 0; i < 100; i++) {
0237 if (r_ecr(ppb_hi) & 0x01)
0238 return;
0239 udelay(5);
0240 }
0241 printk(KERN_WARNING "ppa: ECP sync failed as data still present in FIFO.\n");
0242 }
0243 }
0244
0245 static int ppa_byte_out(unsigned short base, const char *buffer, int len)
0246 {
0247 int i;
0248
0249 for (i = len; i; i--) {
0250 w_dtr(base, *buffer++);
0251 w_ctr(base, 0xe);
0252 w_ctr(base, 0xc);
0253 }
0254 return 1;
0255 }
0256
0257 static int ppa_byte_in(unsigned short base, char *buffer, int len)
0258 {
0259 int i;
0260
0261 for (i = len; i; i--) {
0262 *buffer++ = r_dtr(base);
0263 w_ctr(base, 0x27);
0264 w_ctr(base, 0x25);
0265 }
0266 return 1;
0267 }
0268
0269 static int ppa_nibble_in(unsigned short base, char *buffer, int len)
0270 {
0271 for (; len; len--) {
0272 unsigned char h;
0273
0274 w_ctr(base, 0x4);
0275 h = r_str(base) & 0xf0;
0276 w_ctr(base, 0x6);
0277 *buffer++ = h | ((r_str(base) & 0xf0) >> 4);
0278 }
0279 return 1;
0280 }
0281
0282 static int ppa_out(ppa_struct *dev, char *buffer, int len)
0283 {
0284 int r;
0285 unsigned short ppb = dev->base;
0286
0287 r = ppa_wait(dev);
0288
0289 if ((r & 0x50) != 0x40) {
0290 ppa_fail(dev, DID_ERROR);
0291 return 0;
0292 }
0293 switch (dev->mode) {
0294 case PPA_NIBBLE:
0295 case PPA_PS2:
0296
0297 r = ppa_byte_out(ppb, buffer, len);
0298 break;
0299
0300 case PPA_EPP_32:
0301 case PPA_EPP_16:
0302 case PPA_EPP_8:
0303 epp_reset(ppb);
0304 w_ctr(ppb, 0x4);
0305 #ifdef CONFIG_SCSI_IZIP_EPP16
0306 if (!(((long) buffer | len) & 0x01))
0307 outsw(ppb + 4, buffer, len >> 1);
0308 #else
0309 if (!(((long) buffer | len) & 0x03))
0310 outsl(ppb + 4, buffer, len >> 2);
0311 #endif
0312 else
0313 outsb(ppb + 4, buffer, len);
0314 w_ctr(ppb, 0xc);
0315 r = !(r_str(ppb) & 0x01);
0316 w_ctr(ppb, 0xc);
0317 ecp_sync(dev);
0318 break;
0319
0320 default:
0321 printk(KERN_ERR "PPA: bug in ppa_out()\n");
0322 r = 0;
0323 }
0324 return r;
0325 }
0326
0327 static int ppa_in(ppa_struct *dev, char *buffer, int len)
0328 {
0329 int r;
0330 unsigned short ppb = dev->base;
0331
0332 r = ppa_wait(dev);
0333
0334 if ((r & 0x50) != 0x50) {
0335 ppa_fail(dev, DID_ERROR);
0336 return 0;
0337 }
0338 switch (dev->mode) {
0339 case PPA_NIBBLE:
0340
0341 r = ppa_nibble_in(ppb, buffer, len);
0342 w_ctr(ppb, 0xc);
0343 break;
0344
0345 case PPA_PS2:
0346
0347 w_ctr(ppb, 0x25);
0348 r = ppa_byte_in(ppb, buffer, len);
0349 w_ctr(ppb, 0x4);
0350 w_ctr(ppb, 0xc);
0351 break;
0352
0353 case PPA_EPP_32:
0354 case PPA_EPP_16:
0355 case PPA_EPP_8:
0356 epp_reset(ppb);
0357 w_ctr(ppb, 0x24);
0358 #ifdef CONFIG_SCSI_IZIP_EPP16
0359 if (!(((long) buffer | len) & 0x01))
0360 insw(ppb + 4, buffer, len >> 1);
0361 #else
0362 if (!(((long) buffer | len) & 0x03))
0363 insl(ppb + 4, buffer, len >> 2);
0364 #endif
0365 else
0366 insb(ppb + 4, buffer, len);
0367 w_ctr(ppb, 0x2c);
0368 r = !(r_str(ppb) & 0x01);
0369 w_ctr(ppb, 0x2c);
0370 ecp_sync(dev);
0371 break;
0372
0373 default:
0374 printk(KERN_ERR "PPA: bug in ppa_ins()\n");
0375 r = 0;
0376 break;
0377 }
0378 return r;
0379 }
0380
0381
0382 static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
0383 {
0384 w_dtr(ppb, b);
0385 w_ctr(ppb, 0xc);
0386 w_ctr(ppb, 0xe);
0387 w_ctr(ppb, 0xc);
0388 w_ctr(ppb, 0x4);
0389 w_ctr(ppb, 0xc);
0390 }
0391
0392 static void ppa_disconnect(ppa_struct *dev)
0393 {
0394 unsigned short ppb = dev->base;
0395
0396 ppa_d_pulse(ppb, 0);
0397 ppa_d_pulse(ppb, 0x3c);
0398 ppa_d_pulse(ppb, 0x20);
0399 ppa_d_pulse(ppb, 0xf);
0400 }
0401
0402 static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
0403 {
0404 w_dtr(ppb, b);
0405 w_ctr(ppb, 0x4);
0406 w_ctr(ppb, 0x6);
0407 w_ctr(ppb, 0x4);
0408 w_ctr(ppb, 0xc);
0409 }
0410
0411 static inline void ppa_connect(ppa_struct *dev, int flag)
0412 {
0413 unsigned short ppb = dev->base;
0414
0415 ppa_c_pulse(ppb, 0);
0416 ppa_c_pulse(ppb, 0x3c);
0417 ppa_c_pulse(ppb, 0x20);
0418 if ((flag == CONNECT_EPP_MAYBE) && IN_EPP_MODE(dev->mode))
0419 ppa_c_pulse(ppb, 0xcf);
0420 else
0421 ppa_c_pulse(ppb, 0x8f);
0422 }
0423
0424 static int ppa_select(ppa_struct *dev, int target)
0425 {
0426 int k;
0427 unsigned short ppb = dev->base;
0428
0429
0430
0431
0432
0433 k = PPA_SELECT_TMO;
0434 do {
0435 k--;
0436 udelay(1);
0437 } while ((r_str(ppb) & 0x40) && (k));
0438 if (!k)
0439 return 0;
0440
0441 w_dtr(ppb, (1 << target));
0442 w_ctr(ppb, 0xe);
0443 w_ctr(ppb, 0xc);
0444 w_dtr(ppb, 0x80);
0445 w_ctr(ppb, 0x8);
0446
0447 k = PPA_SELECT_TMO;
0448 do {
0449 k--;
0450 udelay(1);
0451 }
0452 while (!(r_str(ppb) & 0x40) && (k));
0453 if (!k)
0454 return 0;
0455
0456 return 1;
0457 }
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468 static int ppa_init(ppa_struct *dev)
0469 {
0470 int retv;
0471 unsigned short ppb = dev->base;
0472
0473 ppa_disconnect(dev);
0474 ppa_connect(dev, CONNECT_NORMAL);
0475
0476 retv = 2;
0477
0478 w_ctr(ppb, 0xe);
0479 if ((r_str(ppb) & 0x08) == 0x08)
0480 retv--;
0481
0482 w_ctr(ppb, 0xc);
0483 if ((r_str(ppb) & 0x08) == 0x00)
0484 retv--;
0485
0486 if (!retv)
0487 ppa_reset_pulse(ppb);
0488 udelay(1000);
0489 ppa_disconnect(dev);
0490 udelay(1000);
0491
0492 if (retv)
0493 return -EIO;
0494
0495 return device_check(dev);
0496 }
0497
0498 static inline int ppa_send_command(struct scsi_cmnd *cmd)
0499 {
0500 ppa_struct *dev = ppa_dev(cmd->device->host);
0501 int k;
0502
0503 w_ctr(dev->base, 0x0c);
0504
0505 for (k = 0; k < cmd->cmd_len; k++)
0506 if (!ppa_out(dev, &cmd->cmnd[k], 1))
0507 return 0;
0508 return 1;
0509 }
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519 static int ppa_completion(struct scsi_cmnd *const cmd)
0520 {
0521
0522
0523
0524
0525
0526 struct scsi_pointer *scsi_pointer = ppa_scsi_pointer(cmd);
0527 ppa_struct *dev = ppa_dev(cmd->device->host);
0528 unsigned short ppb = dev->base;
0529 unsigned long start_jiffies = jiffies;
0530
0531 unsigned char r, v;
0532 int fast, bulk, status;
0533
0534 v = cmd->cmnd[0];
0535 bulk = ((v == READ_6) ||
0536 (v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
0537
0538
0539
0540
0541
0542 r = (r_str(ppb) & 0xf0);
0543
0544 while (r != (unsigned char) 0xf0) {
0545
0546
0547
0548
0549 if (time_after(jiffies, start_jiffies + 1))
0550 return 0;
0551
0552 if (scsi_pointer->this_residual <= 0) {
0553 ppa_fail(dev, DID_ERROR);
0554 return -1;
0555 }
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567 if ((r & 0xc0) != 0xc0) {
0568
0569
0570
0571 unsigned long k = dev->recon_tmo;
0572 for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0;
0573 k--)
0574 udelay(1);
0575
0576 if (!k)
0577 return 0;
0578 }
0579
0580
0581 fast = bulk && scsi_pointer->this_residual >= PPA_BURST_SIZE ?
0582 PPA_BURST_SIZE : 1;
0583
0584 if (r == (unsigned char) 0xc0)
0585 status = ppa_out(dev, scsi_pointer->ptr, fast);
0586 else
0587 status = ppa_in(dev, scsi_pointer->ptr, fast);
0588
0589 scsi_pointer->ptr += fast;
0590 scsi_pointer->this_residual -= fast;
0591
0592 if (!status) {
0593 ppa_fail(dev, DID_BUS_BUSY);
0594 return -1;
0595 }
0596 if (scsi_pointer->buffer && !scsi_pointer->this_residual) {
0597
0598 if (scsi_pointer->buffers_residual--) {
0599 scsi_pointer->buffer =
0600 sg_next(scsi_pointer->buffer);
0601 scsi_pointer->this_residual =
0602 scsi_pointer->buffer->length;
0603 scsi_pointer->ptr =
0604 sg_virt(scsi_pointer->buffer);
0605 }
0606 }
0607
0608 r = (r_str(ppb) & 0xf0);
0609
0610 if (!(r & 0x80))
0611 return 0;
0612 }
0613 return 1;
0614 }
0615
0616
0617
0618
0619
0620
0621 static void ppa_interrupt(struct work_struct *work)
0622 {
0623 ppa_struct *dev = container_of(work, ppa_struct, ppa_tq.work);
0624 struct scsi_cmnd *cmd = dev->cur_cmd;
0625
0626 if (!cmd) {
0627 printk(KERN_ERR "PPA: bug in ppa_interrupt\n");
0628 return;
0629 }
0630 if (ppa_engine(dev, cmd)) {
0631 schedule_delayed_work(&dev->ppa_tq, 1);
0632 return;
0633 }
0634
0635 #if PPA_DEBUG > 0
0636 switch ((cmd->result >> 16) & 0xff) {
0637 case DID_OK:
0638 break;
0639 case DID_NO_CONNECT:
0640 printk(KERN_DEBUG "ppa: no device at SCSI ID %i\n", cmd->device->target);
0641 break;
0642 case DID_BUS_BUSY:
0643 printk(KERN_DEBUG "ppa: BUS BUSY - EPP timeout detected\n");
0644 break;
0645 case DID_TIME_OUT:
0646 printk(KERN_DEBUG "ppa: unknown timeout\n");
0647 break;
0648 case DID_ABORT:
0649 printk(KERN_DEBUG "ppa: told to abort\n");
0650 break;
0651 case DID_PARITY:
0652 printk(KERN_DEBUG "ppa: parity error (???)\n");
0653 break;
0654 case DID_ERROR:
0655 printk(KERN_DEBUG "ppa: internal driver error\n");
0656 break;
0657 case DID_RESET:
0658 printk(KERN_DEBUG "ppa: told to reset device\n");
0659 break;
0660 case DID_BAD_INTR:
0661 printk(KERN_WARNING "ppa: bad interrupt (???)\n");
0662 break;
0663 default:
0664 printk(KERN_WARNING "ppa: bad return code (%02x)\n",
0665 (cmd->result >> 16) & 0xff);
0666 }
0667 #endif
0668
0669 if (ppa_scsi_pointer(cmd)->phase > 1)
0670 ppa_disconnect(dev);
0671
0672 ppa_pb_dismiss(dev);
0673
0674 dev->cur_cmd = NULL;
0675
0676 scsi_done(cmd);
0677 }
0678
0679 static int ppa_engine(ppa_struct *dev, struct scsi_cmnd *cmd)
0680 {
0681 struct scsi_pointer *scsi_pointer = ppa_scsi_pointer(cmd);
0682 unsigned short ppb = dev->base;
0683 unsigned char l = 0, h = 0;
0684 int retv;
0685
0686
0687
0688
0689 if (dev->failed)
0690 return 0;
0691
0692 switch (scsi_pointer->phase) {
0693 case 0:
0694 if (time_after(jiffies, dev->jstart + HZ)) {
0695
0696
0697
0698
0699 ppa_fail(dev, DID_BUS_BUSY);
0700 return 0;
0701 }
0702 return 1;
0703 case 1:
0704 {
0705 int retv = 2;
0706
0707 ppa_connect(dev, CONNECT_EPP_MAYBE);
0708
0709 w_ctr(ppb, 0xe);
0710 if ((r_str(ppb) & 0x08) == 0x08)
0711 retv--;
0712
0713 w_ctr(ppb, 0xc);
0714 if ((r_str(ppb) & 0x08) == 0x00)
0715 retv--;
0716
0717 if (retv) {
0718 if (time_after(jiffies, dev->jstart + (1 * HZ))) {
0719 printk(KERN_ERR "ppa: Parallel port cable is unplugged.\n");
0720 ppa_fail(dev, DID_BUS_BUSY);
0721 return 0;
0722 } else {
0723 ppa_disconnect(dev);
0724 return 1;
0725 }
0726 }
0727 scsi_pointer->phase++;
0728 }
0729 fallthrough;
0730
0731 case 2:
0732 if (!ppa_select(dev, scmd_id(cmd))) {
0733 ppa_fail(dev, DID_NO_CONNECT);
0734 return 0;
0735 }
0736 scsi_pointer->phase++;
0737 fallthrough;
0738
0739 case 3:
0740 w_ctr(ppb, 0x0c);
0741 if (!(r_str(ppb) & 0x80))
0742 return 1;
0743
0744 if (!ppa_send_command(cmd))
0745 return 0;
0746 scsi_pointer->phase++;
0747 fallthrough;
0748
0749 case 4:
0750 if (scsi_bufflen(cmd)) {
0751 scsi_pointer->buffer = scsi_sglist(cmd);
0752 scsi_pointer->this_residual =
0753 scsi_pointer->buffer->length;
0754 scsi_pointer->ptr = sg_virt(scsi_pointer->buffer);
0755 } else {
0756 scsi_pointer->buffer = NULL;
0757 scsi_pointer->this_residual = 0;
0758 scsi_pointer->ptr = NULL;
0759 }
0760 scsi_pointer->buffers_residual = scsi_sg_count(cmd) - 1;
0761 scsi_pointer->phase++;
0762 fallthrough;
0763
0764 case 5:
0765 w_ctr(ppb, 0x0c);
0766 if (!(r_str(ppb) & 0x80))
0767 return 1;
0768
0769 retv = ppa_completion(cmd);
0770 if (retv == -1)
0771 return 0;
0772 if (retv == 0)
0773 return 1;
0774 scsi_pointer->phase++;
0775 fallthrough;
0776
0777 case 6:
0778 cmd->result = DID_OK << 16;
0779
0780 if (ppa_wait(dev) != (unsigned char) 0xf0) {
0781 ppa_fail(dev, DID_ERROR);
0782 return 0;
0783 }
0784 if (ppa_in(dev, &l, 1)) {
0785
0786 if (ppa_wait(dev) == (unsigned char) 0xf0)
0787 ppa_in(dev, &h, 1);
0788 cmd->result =
0789 (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
0790 }
0791 return 0;
0792
0793 default:
0794 printk(KERN_ERR "ppa: Invalid scsi phase\n");
0795 }
0796 return 0;
0797 }
0798
0799 static int ppa_queuecommand_lck(struct scsi_cmnd *cmd)
0800 {
0801 ppa_struct *dev = ppa_dev(cmd->device->host);
0802
0803 if (dev->cur_cmd) {
0804 printk(KERN_ERR "PPA: bug in ppa_queuecommand\n");
0805 return 0;
0806 }
0807 dev->failed = 0;
0808 dev->jstart = jiffies;
0809 dev->cur_cmd = cmd;
0810 cmd->result = DID_ERROR << 16;
0811 ppa_scsi_pointer(cmd)->phase = 0;
0812
0813 schedule_delayed_work(&dev->ppa_tq, 0);
0814
0815 ppa_pb_claim(dev);
0816
0817 return 0;
0818 }
0819
0820 static DEF_SCSI_QCMD(ppa_queuecommand)
0821
0822
0823
0824
0825
0826
0827
0828 static int ppa_biosparam(struct scsi_device *sdev, struct block_device *dev,
0829 sector_t capacity, int ip[])
0830 {
0831 ip[0] = 0x40;
0832 ip[1] = 0x20;
0833 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
0834 if (ip[2] > 1024) {
0835 ip[0] = 0xff;
0836 ip[1] = 0x3f;
0837 ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
0838 if (ip[2] > 1023)
0839 ip[2] = 1023;
0840 }
0841 return 0;
0842 }
0843
0844 static int ppa_abort(struct scsi_cmnd *cmd)
0845 {
0846 ppa_struct *dev = ppa_dev(cmd->device->host);
0847
0848
0849
0850
0851
0852 switch (ppa_scsi_pointer(cmd)->phase) {
0853 case 0:
0854 case 1:
0855 dev->cur_cmd = NULL;
0856 return SUCCESS;
0857 default:
0858 return FAILED;
0859 }
0860 }
0861
0862 static void ppa_reset_pulse(unsigned int base)
0863 {
0864 w_dtr(base, 0x40);
0865 w_ctr(base, 0x8);
0866 udelay(30);
0867 w_ctr(base, 0xc);
0868 }
0869
0870 static int ppa_reset(struct scsi_cmnd *cmd)
0871 {
0872 ppa_struct *dev = ppa_dev(cmd->device->host);
0873
0874 if (ppa_scsi_pointer(cmd)->phase)
0875 ppa_disconnect(dev);
0876 dev->cur_cmd = NULL;
0877
0878 ppa_connect(dev, CONNECT_NORMAL);
0879 ppa_reset_pulse(dev->base);
0880 mdelay(1);
0881 ppa_disconnect(dev);
0882 mdelay(1);
0883 return SUCCESS;
0884 }
0885
0886 static int device_check(ppa_struct *dev)
0887 {
0888
0889
0890
0891 static u8 cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
0892 int loop, old_mode, status, k, ppb = dev->base;
0893 unsigned char l;
0894
0895 old_mode = dev->mode;
0896 for (loop = 0; loop < 8; loop++) {
0897
0898 if ((ppb & 0x0007) == 0x0000)
0899 dev->mode = PPA_EPP_32;
0900
0901 second_pass:
0902 ppa_connect(dev, CONNECT_EPP_MAYBE);
0903
0904 if (!ppa_select(dev, loop)) {
0905 ppa_disconnect(dev);
0906 continue;
0907 }
0908 printk(KERN_INFO "ppa: Found device at ID %i, Attempting to use %s\n",
0909 loop, PPA_MODE_STRING[dev->mode]);
0910
0911
0912 status = 1;
0913 w_ctr(ppb, 0x0c);
0914 for (l = 0; (l < 6) && (status); l++)
0915 status = ppa_out(dev, cmd, 1);
0916
0917 if (!status) {
0918 ppa_disconnect(dev);
0919 ppa_connect(dev, CONNECT_EPP_MAYBE);
0920 w_dtr(ppb, 0x40);
0921 w_ctr(ppb, 0x08);
0922 udelay(30);
0923 w_ctr(ppb, 0x0c);
0924 udelay(1000);
0925 ppa_disconnect(dev);
0926 udelay(1000);
0927 if (dev->mode == PPA_EPP_32) {
0928 dev->mode = old_mode;
0929 goto second_pass;
0930 }
0931 return -EIO;
0932 }
0933 w_ctr(ppb, 0x0c);
0934 k = 1000000;
0935 do {
0936 l = r_str(ppb);
0937 k--;
0938 udelay(1);
0939 } while (!(l & 0x80) && (k));
0940
0941 l &= 0xf0;
0942
0943 if (l != 0xf0) {
0944 ppa_disconnect(dev);
0945 ppa_connect(dev, CONNECT_EPP_MAYBE);
0946 ppa_reset_pulse(ppb);
0947 udelay(1000);
0948 ppa_disconnect(dev);
0949 udelay(1000);
0950 if (dev->mode == PPA_EPP_32) {
0951 dev->mode = old_mode;
0952 goto second_pass;
0953 }
0954 return -EIO;
0955 }
0956 ppa_disconnect(dev);
0957 printk(KERN_INFO "ppa: Communication established with ID %i using %s\n",
0958 loop, PPA_MODE_STRING[dev->mode]);
0959 ppa_connect(dev, CONNECT_EPP_MAYBE);
0960 ppa_reset_pulse(ppb);
0961 udelay(1000);
0962 ppa_disconnect(dev);
0963 udelay(1000);
0964 return 0;
0965 }
0966 return -ENODEV;
0967 }
0968
0969 static int ppa_adjust_queue(struct scsi_device *device)
0970 {
0971 blk_queue_bounce_limit(device->request_queue, BLK_BOUNCE_HIGH);
0972 return 0;
0973 }
0974
0975 static struct scsi_host_template ppa_template = {
0976 .module = THIS_MODULE,
0977 .proc_name = "ppa",
0978 .show_info = ppa_show_info,
0979 .write_info = ppa_write_info,
0980 .name = "Iomega VPI0 (ppa) interface",
0981 .queuecommand = ppa_queuecommand,
0982 .eh_abort_handler = ppa_abort,
0983 .eh_host_reset_handler = ppa_reset,
0984 .bios_param = ppa_biosparam,
0985 .this_id = -1,
0986 .sg_tablesize = SG_ALL,
0987 .can_queue = 1,
0988 .slave_alloc = ppa_adjust_queue,
0989 .cmd_size = sizeof(struct scsi_pointer),
0990 };
0991
0992
0993
0994
0995
0996 static LIST_HEAD(ppa_hosts);
0997
0998
0999
1000
1001
1002
1003
1004 static 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
1022 static 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
1054
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
1078
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
1093
1094 err = ppa_init(dev);
1095 ppa_pb_release(dev);
1096
1097 if (err)
1098 goto out1;
1099
1100
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;
1124 out2:
1125 list_del_init(&dev->list);
1126 scsi_host_put(host);
1127 out1:
1128 parport_unregister_device(dev->dev);
1129 out:
1130 kfree(dev);
1131 return err;
1132 }
1133
1134 static void ppa_attach(struct parport *pb)
1135 {
1136 __ppa_attach(pb);
1137 }
1138
1139 static 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
1154 static struct parport_driver ppa_driver = {
1155 .name = "ppa",
1156 .match_port = ppa_attach,
1157 .detach = ppa_detach,
1158 .devmodel = true,
1159 };
1160 module_parport_driver(ppa_driver);
1161
1162 MODULE_LICENSE("GPL");