0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 #define PG_VERSION "1.02"
0125 #define PG_MAJOR 97
0126 #define PG_NAME "pg"
0127 #define PG_UNITS 4
0128
0129 #ifndef PI_PG
0130 #define PI_PG 4
0131 #endif
0132
0133 #include <linux/types.h>
0134
0135
0136
0137
0138
0139
0140 static int verbose;
0141 static int major = PG_MAJOR;
0142 static char *name = PG_NAME;
0143 static int disable = 0;
0144
0145 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
0146 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
0147 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
0148 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
0149
0150 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
0151 static int pg_drive_count;
0152
0153 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
0154
0155
0156
0157 #include <linux/module.h>
0158 #include <linux/init.h>
0159 #include <linux/fs.h>
0160 #include <linux/delay.h>
0161 #include <linux/slab.h>
0162 #include <linux/mtio.h>
0163 #include <linux/pg.h>
0164 #include <linux/device.h>
0165 #include <linux/sched.h> /* current, TASK_* */
0166 #include <linux/mutex.h>
0167 #include <linux/jiffies.h>
0168
0169 #include <linux/uaccess.h>
0170
0171 module_param(verbose, int, 0644);
0172 module_param(major, int, 0);
0173 module_param(name, charp, 0);
0174 module_param_array(drive0, int, NULL, 0);
0175 module_param_array(drive1, int, NULL, 0);
0176 module_param_array(drive2, int, NULL, 0);
0177 module_param_array(drive3, int, NULL, 0);
0178
0179 #include "paride.h"
0180
0181 #define PG_SPIN_DEL 50
0182 #define PG_SPIN 200
0183 #define PG_TMO HZ
0184 #define PG_RESET_TMO 10*HZ
0185
0186 #define STAT_ERR 0x01
0187 #define STAT_INDEX 0x02
0188 #define STAT_ECC 0x04
0189 #define STAT_DRQ 0x08
0190 #define STAT_SEEK 0x10
0191 #define STAT_WRERR 0x20
0192 #define STAT_READY 0x40
0193 #define STAT_BUSY 0x80
0194
0195 #define ATAPI_IDENTIFY 0x12
0196
0197 static DEFINE_MUTEX(pg_mutex);
0198 static int pg_open(struct inode *inode, struct file *file);
0199 static int pg_release(struct inode *inode, struct file *file);
0200 static ssize_t pg_read(struct file *filp, char __user *buf,
0201 size_t count, loff_t * ppos);
0202 static ssize_t pg_write(struct file *filp, const char __user *buf,
0203 size_t count, loff_t * ppos);
0204 static int pg_detect(void);
0205
0206 #define PG_NAMELEN 8
0207
0208 struct pg {
0209 struct pi_adapter pia;
0210 struct pi_adapter *pi;
0211 int busy;
0212 int start;
0213 int dlen;
0214 unsigned long timeout;
0215 int status;
0216 int drive;
0217 unsigned long access;
0218 int present;
0219 char *bufptr;
0220 char name[PG_NAMELEN];
0221 };
0222
0223 static struct pg devices[PG_UNITS];
0224
0225 static int pg_identify(struct pg *dev, int log);
0226
0227 static char pg_scratch[512];
0228
0229 static struct class *pg_class;
0230 static void *par_drv;
0231
0232
0233
0234 static const struct file_operations pg_fops = {
0235 .owner = THIS_MODULE,
0236 .read = pg_read,
0237 .write = pg_write,
0238 .open = pg_open,
0239 .release = pg_release,
0240 .llseek = noop_llseek,
0241 };
0242
0243 static void pg_init_units(void)
0244 {
0245 int unit;
0246
0247 pg_drive_count = 0;
0248 for (unit = 0; unit < PG_UNITS; unit++) {
0249 int *parm = *drives[unit];
0250 struct pg *dev = &devices[unit];
0251 dev->pi = &dev->pia;
0252 clear_bit(0, &dev->access);
0253 dev->busy = 0;
0254 dev->present = 0;
0255 dev->bufptr = NULL;
0256 dev->drive = parm[D_SLV];
0257 snprintf(dev->name, PG_NAMELEN, "%s%c", name, 'a'+unit);
0258 if (parm[D_PRT])
0259 pg_drive_count++;
0260 }
0261 }
0262
0263 static inline int status_reg(struct pg *dev)
0264 {
0265 return pi_read_regr(dev->pi, 1, 6);
0266 }
0267
0268 static inline int read_reg(struct pg *dev, int reg)
0269 {
0270 return pi_read_regr(dev->pi, 0, reg);
0271 }
0272
0273 static inline void write_reg(struct pg *dev, int reg, int val)
0274 {
0275 pi_write_regr(dev->pi, 0, reg, val);
0276 }
0277
0278 static inline u8 DRIVE(struct pg *dev)
0279 {
0280 return 0xa0+0x10*dev->drive;
0281 }
0282
0283 static void pg_sleep(int cs)
0284 {
0285 schedule_timeout_interruptible(cs);
0286 }
0287
0288 static int pg_wait(struct pg *dev, int go, int stop, unsigned long tmo, char *msg)
0289 {
0290 int j, r, e, s, p, to;
0291
0292 dev->status = 0;
0293
0294 j = 0;
0295 while ((((r = status_reg(dev)) & go) || (stop && (!(r & stop))))
0296 && time_before(jiffies, tmo)) {
0297 if (j++ < PG_SPIN)
0298 udelay(PG_SPIN_DEL);
0299 else
0300 pg_sleep(1);
0301 }
0302
0303 to = time_after_eq(jiffies, tmo);
0304
0305 if ((r & (STAT_ERR & stop)) || to) {
0306 s = read_reg(dev, 7);
0307 e = read_reg(dev, 1);
0308 p = read_reg(dev, 2);
0309 if (verbose > 1)
0310 printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
0311 dev->name, msg, s, e, p, to ? " timeout" : "");
0312 if (to)
0313 e |= 0x100;
0314 dev->status = (e >> 4) & 0xff;
0315 return -1;
0316 }
0317 return 0;
0318 }
0319
0320 static int pg_command(struct pg *dev, char *cmd, int dlen, unsigned long tmo)
0321 {
0322 int k;
0323
0324 pi_connect(dev->pi);
0325
0326 write_reg(dev, 6, DRIVE(dev));
0327
0328 if (pg_wait(dev, STAT_BUSY | STAT_DRQ, 0, tmo, "before command"))
0329 goto fail;
0330
0331 write_reg(dev, 4, dlen % 256);
0332 write_reg(dev, 5, dlen / 256);
0333 write_reg(dev, 7, 0xa0);
0334
0335 if (pg_wait(dev, STAT_BUSY, STAT_DRQ, tmo, "command DRQ"))
0336 goto fail;
0337
0338 if (read_reg(dev, 2) != 1) {
0339 printk("%s: command phase error\n", dev->name);
0340 goto fail;
0341 }
0342
0343 pi_write_block(dev->pi, cmd, 12);
0344
0345 if (verbose > 1) {
0346 printk("%s: Command sent, dlen=%d packet= ", dev->name, dlen);
0347 for (k = 0; k < 12; k++)
0348 printk("%02x ", cmd[k] & 0xff);
0349 printk("\n");
0350 }
0351 return 0;
0352 fail:
0353 pi_disconnect(dev->pi);
0354 return -1;
0355 }
0356
0357 static int pg_completion(struct pg *dev, char *buf, unsigned long tmo)
0358 {
0359 int r, d, n, p;
0360
0361 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
0362 tmo, "completion");
0363
0364 dev->dlen = 0;
0365
0366 while (read_reg(dev, 7) & STAT_DRQ) {
0367 d = (read_reg(dev, 4) + 256 * read_reg(dev, 5));
0368 n = ((d + 3) & 0xfffc);
0369 p = read_reg(dev, 2) & 3;
0370 if (p == 0)
0371 pi_write_block(dev->pi, buf, n);
0372 if (p == 2)
0373 pi_read_block(dev->pi, buf, n);
0374 if (verbose > 1)
0375 printk("%s: %s %d bytes\n", dev->name,
0376 p ? "Read" : "Write", n);
0377 dev->dlen += (1 - p) * d;
0378 buf += d;
0379 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
0380 tmo, "completion");
0381 }
0382
0383 pi_disconnect(dev->pi);
0384
0385 return r;
0386 }
0387
0388 static int pg_reset(struct pg *dev)
0389 {
0390 int i, k, err;
0391 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
0392 int got[5];
0393
0394 pi_connect(dev->pi);
0395 write_reg(dev, 6, DRIVE(dev));
0396 write_reg(dev, 7, 8);
0397
0398 pg_sleep(20 * HZ / 1000);
0399
0400 k = 0;
0401 while ((k++ < PG_RESET_TMO) && (status_reg(dev) & STAT_BUSY))
0402 pg_sleep(1);
0403
0404 for (i = 0; i < 5; i++)
0405 got[i] = read_reg(dev, i + 1);
0406
0407 err = memcmp(expect, got, sizeof(got)) ? -1 : 0;
0408
0409 if (verbose) {
0410 printk("%s: Reset (%d) signature = ", dev->name, k);
0411 for (i = 0; i < 5; i++)
0412 printk("%3x", got[i]);
0413 if (err)
0414 printk(" (incorrect)");
0415 printk("\n");
0416 }
0417
0418 pi_disconnect(dev->pi);
0419 return err;
0420 }
0421
0422 static void xs(char *buf, char *targ, int len)
0423 {
0424 char l = '\0';
0425 int k;
0426
0427 for (k = 0; k < len; k++) {
0428 char c = *buf++;
0429 if (c != ' ' && c != l)
0430 l = *targ++ = c;
0431 }
0432 if (l == ' ')
0433 targ--;
0434 *targ = '\0';
0435 }
0436
0437 static int pg_identify(struct pg *dev, int log)
0438 {
0439 int s;
0440 char *ms[2] = { "master", "slave" };
0441 char mf[10], id[18];
0442 char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
0443 char buf[36];
0444
0445 s = pg_command(dev, id_cmd, 36, jiffies + PG_TMO);
0446 if (s)
0447 return -1;
0448 s = pg_completion(dev, buf, jiffies + PG_TMO);
0449 if (s)
0450 return -1;
0451
0452 if (log) {
0453 xs(buf + 8, mf, 8);
0454 xs(buf + 16, id, 16);
0455 printk("%s: %s %s, %s\n", dev->name, mf, id, ms[dev->drive]);
0456 }
0457
0458 return 0;
0459 }
0460
0461
0462
0463
0464
0465 static int pg_probe(struct pg *dev)
0466 {
0467 if (dev->drive == -1) {
0468 for (dev->drive = 0; dev->drive <= 1; dev->drive++)
0469 if (!pg_reset(dev))
0470 return pg_identify(dev, 1);
0471 } else {
0472 if (!pg_reset(dev))
0473 return pg_identify(dev, 1);
0474 }
0475 return -1;
0476 }
0477
0478 static int pg_detect(void)
0479 {
0480 struct pg *dev = &devices[0];
0481 int k, unit;
0482
0483 printk("%s: %s version %s, major %d\n", name, name, PG_VERSION, major);
0484
0485 par_drv = pi_register_driver(name);
0486 if (!par_drv) {
0487 pr_err("failed to register %s driver\n", name);
0488 return -1;
0489 }
0490
0491 k = 0;
0492 if (pg_drive_count == 0) {
0493 if (pi_init(dev->pi, 1, -1, -1, -1, -1, -1, pg_scratch,
0494 PI_PG, verbose, dev->name)) {
0495 if (!pg_probe(dev)) {
0496 dev->present = 1;
0497 k++;
0498 } else
0499 pi_release(dev->pi);
0500 }
0501
0502 } else
0503 for (unit = 0; unit < PG_UNITS; unit++, dev++) {
0504 int *parm = *drives[unit];
0505 if (!parm[D_PRT])
0506 continue;
0507 if (pi_init(dev->pi, 0, parm[D_PRT], parm[D_MOD],
0508 parm[D_UNI], parm[D_PRO], parm[D_DLY],
0509 pg_scratch, PI_PG, verbose, dev->name)) {
0510 if (!pg_probe(dev)) {
0511 dev->present = 1;
0512 k++;
0513 } else
0514 pi_release(dev->pi);
0515 }
0516 }
0517
0518 if (k)
0519 return 0;
0520
0521 pi_unregister_driver(par_drv);
0522 printk("%s: No ATAPI device detected\n", name);
0523 return -1;
0524 }
0525
0526 static int pg_open(struct inode *inode, struct file *file)
0527 {
0528 int unit = iminor(inode) & 0x7f;
0529 struct pg *dev = &devices[unit];
0530 int ret = 0;
0531
0532 mutex_lock(&pg_mutex);
0533 if ((unit >= PG_UNITS) || (!dev->present)) {
0534 ret = -ENODEV;
0535 goto out;
0536 }
0537
0538 if (test_and_set_bit(0, &dev->access)) {
0539 ret = -EBUSY;
0540 goto out;
0541 }
0542
0543 if (dev->busy) {
0544 pg_reset(dev);
0545 dev->busy = 0;
0546 }
0547
0548 pg_identify(dev, (verbose > 1));
0549
0550 dev->bufptr = kmalloc(PG_MAX_DATA, GFP_KERNEL);
0551 if (dev->bufptr == NULL) {
0552 clear_bit(0, &dev->access);
0553 printk("%s: buffer allocation failed\n", dev->name);
0554 ret = -ENOMEM;
0555 goto out;
0556 }
0557
0558 file->private_data = dev;
0559
0560 out:
0561 mutex_unlock(&pg_mutex);
0562 return ret;
0563 }
0564
0565 static int pg_release(struct inode *inode, struct file *file)
0566 {
0567 struct pg *dev = file->private_data;
0568
0569 kfree(dev->bufptr);
0570 dev->bufptr = NULL;
0571 clear_bit(0, &dev->access);
0572
0573 return 0;
0574 }
0575
0576 static ssize_t pg_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
0577 {
0578 struct pg *dev = filp->private_data;
0579 struct pg_write_hdr hdr;
0580 int hs = sizeof (hdr);
0581
0582 if (dev->busy)
0583 return -EBUSY;
0584 if (count < hs)
0585 return -EINVAL;
0586
0587 if (copy_from_user(&hdr, buf, hs))
0588 return -EFAULT;
0589
0590 if (hdr.magic != PG_MAGIC)
0591 return -EINVAL;
0592 if (hdr.dlen < 0 || hdr.dlen > PG_MAX_DATA)
0593 return -EINVAL;
0594 if ((count - hs) > PG_MAX_DATA)
0595 return -EINVAL;
0596
0597 if (hdr.func == PG_RESET) {
0598 if (count != hs)
0599 return -EINVAL;
0600 if (pg_reset(dev))
0601 return -EIO;
0602 return count;
0603 }
0604
0605 if (hdr.func != PG_COMMAND)
0606 return -EINVAL;
0607
0608 dev->start = jiffies;
0609 dev->timeout = hdr.timeout * HZ + HZ / 2 + jiffies;
0610
0611 if (pg_command(dev, hdr.packet, hdr.dlen, jiffies + PG_TMO)) {
0612 if (dev->status & 0x10)
0613 return -ETIME;
0614 return -EIO;
0615 }
0616
0617 dev->busy = 1;
0618
0619 if (copy_from_user(dev->bufptr, buf + hs, count - hs))
0620 return -EFAULT;
0621 return count;
0622 }
0623
0624 static ssize_t pg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
0625 {
0626 struct pg *dev = filp->private_data;
0627 struct pg_read_hdr hdr;
0628 int hs = sizeof (hdr);
0629 int copy;
0630
0631 if (!dev->busy)
0632 return -EINVAL;
0633 if (count < hs)
0634 return -EINVAL;
0635
0636 dev->busy = 0;
0637
0638 if (pg_completion(dev, dev->bufptr, dev->timeout))
0639 if (dev->status & 0x10)
0640 return -ETIME;
0641
0642 memset(&hdr, 0, sizeof(hdr));
0643 hdr.magic = PG_MAGIC;
0644 hdr.dlen = dev->dlen;
0645 copy = 0;
0646
0647 if (hdr.dlen < 0) {
0648 hdr.dlen = -1 * hdr.dlen;
0649 copy = hdr.dlen;
0650 if (copy > (count - hs))
0651 copy = count - hs;
0652 }
0653
0654 hdr.duration = (jiffies - dev->start + HZ / 2) / HZ;
0655 hdr.scsi = dev->status & 0x0f;
0656
0657 if (copy_to_user(buf, &hdr, hs))
0658 return -EFAULT;
0659 if (copy > 0)
0660 if (copy_to_user(buf + hs, dev->bufptr, copy))
0661 return -EFAULT;
0662 return copy + hs;
0663 }
0664
0665 static int __init pg_init(void)
0666 {
0667 int unit;
0668 int err;
0669
0670 if (disable){
0671 err = -EINVAL;
0672 goto out;
0673 }
0674
0675 pg_init_units();
0676
0677 if (pg_detect()) {
0678 err = -ENODEV;
0679 goto out;
0680 }
0681
0682 err = register_chrdev(major, name, &pg_fops);
0683 if (err < 0) {
0684 printk("pg_init: unable to get major number %d\n", major);
0685 for (unit = 0; unit < PG_UNITS; unit++) {
0686 struct pg *dev = &devices[unit];
0687 if (dev->present)
0688 pi_release(dev->pi);
0689 }
0690 goto out;
0691 }
0692 major = err;
0693 pg_class = class_create(THIS_MODULE, "pg");
0694 if (IS_ERR(pg_class)) {
0695 err = PTR_ERR(pg_class);
0696 goto out_chrdev;
0697 }
0698 for (unit = 0; unit < PG_UNITS; unit++) {
0699 struct pg *dev = &devices[unit];
0700 if (dev->present)
0701 device_create(pg_class, NULL, MKDEV(major, unit), NULL,
0702 "pg%u", unit);
0703 }
0704 err = 0;
0705 goto out;
0706
0707 out_chrdev:
0708 unregister_chrdev(major, "pg");
0709 out:
0710 return err;
0711 }
0712
0713 static void __exit pg_exit(void)
0714 {
0715 int unit;
0716
0717 for (unit = 0; unit < PG_UNITS; unit++) {
0718 struct pg *dev = &devices[unit];
0719 if (dev->present)
0720 device_destroy(pg_class, MKDEV(major, unit));
0721 }
0722 class_destroy(pg_class);
0723 unregister_chrdev(major, name);
0724
0725 for (unit = 0; unit < PG_UNITS; unit++) {
0726 struct pg *dev = &devices[unit];
0727 if (dev->present)
0728 pi_release(dev->pi);
0729 }
0730 }
0731
0732 MODULE_LICENSE("GPL");
0733 module_init(pg_init)
0734 module_exit(pg_exit)