at v2.6.14-rc2 722 lines 17 kB view raw
1/* 2 pg.c (c) 1998 Grant R. Guenther <grant@torque.net> 3 Under the terms of the GNU General Public License. 4 5 The pg driver provides a simple character device interface for 6 sending ATAPI commands to a device. With the exception of the 7 ATAPI reset operation, all operations are performed by a pair 8 of read and write operations to the appropriate /dev/pgN device. 9 A write operation delivers a command and any outbound data in 10 a single buffer. Normally, the write will succeed unless the 11 device is offline or malfunctioning, or there is already another 12 command pending. If the write succeeds, it should be followed 13 immediately by a read operation, to obtain any returned data and 14 status information. A read will fail if there is no operation 15 in progress. 16 17 As a special case, the device can be reset with a write operation, 18 and in this case, no following read is expected, or permitted. 19 20 There are no ioctl() operations. Any single operation 21 may transfer at most PG_MAX_DATA bytes. Note that the driver must 22 copy the data through an internal buffer. In keeping with all 23 current ATAPI devices, command packets are assumed to be exactly 24 12 bytes in length. 25 26 To permit future changes to this interface, the headers in the 27 read and write buffers contain a single character "magic" flag. 28 Currently this flag must be the character "P". 29 30 By default, the driver will autoprobe for a single parallel 31 port ATAPI device, but if their individual parameters are 32 specified, the driver can handle up to 4 devices. 33 34 To use this device, you must have the following device 35 special files defined: 36 37 /dev/pg0 c 97 0 38 /dev/pg1 c 97 1 39 /dev/pg2 c 97 2 40 /dev/pg3 c 97 3 41 42 (You'll need to change the 97 to something else if you use 43 the 'major' parameter to install the driver on a different 44 major number.) 45 46 The behaviour of the pg driver can be altered by setting 47 some parameters from the insmod command line. The following 48 parameters are adjustable: 49 50 drive0 These four arguments can be arrays of 51 drive1 1-6 integers as follows: 52 drive2 53 drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<dly> 54 55 Where, 56 57 <prt> is the base of the parallel port address for 58 the corresponding drive. (required) 59 60 <pro> is the protocol number for the adapter that 61 supports this drive. These numbers are 62 logged by 'paride' when the protocol modules 63 are initialised. (0 if not given) 64 65 <uni> for those adapters that support chained 66 devices, this is the unit selector for the 67 chain of devices on the given port. It should 68 be zero for devices that don't support chaining. 69 (0 if not given) 70 71 <mod> this can be -1 to choose the best mode, or one 72 of the mode numbers supported by the adapter. 73 (-1 if not given) 74 75 <slv> ATAPI devices can be jumpered to master or slave. 76 Set this to 0 to choose the master drive, 1 to 77 choose the slave, -1 (the default) to choose the 78 first drive found. 79 80 <dly> some parallel ports require the driver to 81 go more slowly. -1 sets a default value that 82 should work with the chosen protocol. Otherwise, 83 set this to a small integer, the larger it is 84 the slower the port i/o. In some cases, setting 85 this to zero will speed up the device. (default -1) 86 87 major You may use this parameter to overide the 88 default major number (97) that this driver 89 will use. Be sure to change the device 90 name as well. 91 92 name This parameter is a character string that 93 contains the name the kernel will use for this 94 device (in /proc output, for instance). 95 (default "pg"). 96 97 verbose This parameter controls the amount of logging 98 that is done by the driver. Set it to 0 for 99 quiet operation, to 1 to enable progress 100 messages while the driver probes for devices, 101 or to 2 for full debug logging. (default 0) 102 103 If this driver is built into the kernel, you can use 104 the following command line parameters, with the same values 105 as the corresponding module parameters listed above: 106 107 pg.drive0 108 pg.drive1 109 pg.drive2 110 pg.drive3 111 112 In addition, you can use the parameter pg.disable to disable 113 the driver entirely. 114 115*/ 116 117/* Changes: 118 119 1.01 GRG 1998.06.16 Bug fixes 120 1.02 GRG 1998.09.24 Added jumbo support 121 122*/ 123 124#define PG_VERSION "1.02" 125#define PG_MAJOR 97 126#define PG_NAME "pg" 127#define PG_UNITS 4 128 129#ifndef PI_PG 130#define PI_PG 4 131#endif 132 133/* Here are things one can override from the insmod command. 134 Most are autoprobed by paride unless set here. Verbose is 0 135 by default. 136 137*/ 138 139static int verbose = 0; 140static int major = PG_MAJOR; 141static char *name = PG_NAME; 142static int disable = 0; 143 144static int drive0[6] = { 0, 0, 0, -1, -1, -1 }; 145static int drive1[6] = { 0, 0, 0, -1, -1, -1 }; 146static int drive2[6] = { 0, 0, 0, -1, -1, -1 }; 147static int drive3[6] = { 0, 0, 0, -1, -1, -1 }; 148 149static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3}; 150static int pg_drive_count; 151 152enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY}; 153 154/* end of parameters */ 155 156#include <linux/module.h> 157#include <linux/init.h> 158#include <linux/fs.h> 159#include <linux/devfs_fs_kernel.h> 160#include <linux/delay.h> 161#include <linux/slab.h> 162#include <linux/mtio.h> 163#include <linux/pg.h> 164#include <linux/device.h> 165 166#include <asm/uaccess.h> 167 168module_param(verbose, bool, 0644); 169module_param(major, int, 0); 170module_param(name, charp, 0); 171module_param_array(drive0, int, NULL, 0); 172module_param_array(drive1, int, NULL, 0); 173module_param_array(drive2, int, NULL, 0); 174module_param_array(drive3, int, NULL, 0); 175 176#include "paride.h" 177 178#define PG_SPIN_DEL 50 /* spin delay in micro-seconds */ 179#define PG_SPIN 200 180#define PG_TMO HZ 181#define PG_RESET_TMO 10*HZ 182 183#define STAT_ERR 0x01 184#define STAT_INDEX 0x02 185#define STAT_ECC 0x04 186#define STAT_DRQ 0x08 187#define STAT_SEEK 0x10 188#define STAT_WRERR 0x20 189#define STAT_READY 0x40 190#define STAT_BUSY 0x80 191 192#define ATAPI_IDENTIFY 0x12 193 194static int pg_open(struct inode *inode, struct file *file); 195static int pg_release(struct inode *inode, struct file *file); 196static ssize_t pg_read(struct file *filp, char __user *buf, 197 size_t count, loff_t * ppos); 198static ssize_t pg_write(struct file *filp, const char __user *buf, 199 size_t count, loff_t * ppos); 200static int pg_detect(void); 201 202#define PG_NAMELEN 8 203 204struct pg { 205 struct pi_adapter pia; /* interface to paride layer */ 206 struct pi_adapter *pi; 207 int busy; /* write done, read expected */ 208 int start; /* jiffies at command start */ 209 int dlen; /* transfer size requested */ 210 unsigned long timeout; /* timeout requested */ 211 int status; /* last sense key */ 212 int drive; /* drive */ 213 unsigned long access; /* count of active opens ... */ 214 int present; /* device present ? */ 215 char *bufptr; 216 char name[PG_NAMELEN]; /* pg0, pg1, ... */ 217}; 218 219static struct pg devices[PG_UNITS]; 220 221static int pg_identify(struct pg *dev, int log); 222 223static char pg_scratch[512]; /* scratch block buffer */ 224 225static struct class *pg_class; 226 227/* kernel glue structures */ 228 229static struct file_operations pg_fops = { 230 .owner = THIS_MODULE, 231 .read = pg_read, 232 .write = pg_write, 233 .open = pg_open, 234 .release = pg_release, 235}; 236 237static void pg_init_units(void) 238{ 239 int unit; 240 241 pg_drive_count = 0; 242 for (unit = 0; unit < PG_UNITS; unit++) { 243 int *parm = *drives[unit]; 244 struct pg *dev = &devices[unit]; 245 dev->pi = &dev->pia; 246 clear_bit(0, &dev->access); 247 dev->busy = 0; 248 dev->present = 0; 249 dev->bufptr = NULL; 250 dev->drive = parm[D_SLV]; 251 snprintf(dev->name, PG_NAMELEN, "%s%c", name, 'a'+unit); 252 if (parm[D_PRT]) 253 pg_drive_count++; 254 } 255} 256 257static inline int status_reg(struct pg *dev) 258{ 259 return pi_read_regr(dev->pi, 1, 6); 260} 261 262static inline int read_reg(struct pg *dev, int reg) 263{ 264 return pi_read_regr(dev->pi, 0, reg); 265} 266 267static inline void write_reg(struct pg *dev, int reg, int val) 268{ 269 pi_write_regr(dev->pi, 0, reg, val); 270} 271 272static inline u8 DRIVE(struct pg *dev) 273{ 274 return 0xa0+0x10*dev->drive; 275} 276 277static void pg_sleep(int cs) 278{ 279 schedule_timeout_interruptible(cs); 280} 281 282static int pg_wait(struct pg *dev, int go, int stop, unsigned long tmo, char *msg) 283{ 284 int j, r, e, s, p, to; 285 286 dev->status = 0; 287 288 j = 0; 289 while ((((r = status_reg(dev)) & go) || (stop && (!(r & stop)))) 290 && time_before(jiffies, tmo)) { 291 if (j++ < PG_SPIN) 292 udelay(PG_SPIN_DEL); 293 else 294 pg_sleep(1); 295 } 296 297 to = time_after_eq(jiffies, tmo); 298 299 if ((r & (STAT_ERR & stop)) || to) { 300 s = read_reg(dev, 7); 301 e = read_reg(dev, 1); 302 p = read_reg(dev, 2); 303 if (verbose > 1) 304 printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n", 305 dev->name, msg, s, e, p, to ? " timeout" : ""); 306 if (to) 307 e |= 0x100; 308 dev->status = (e >> 4) & 0xff; 309 return -1; 310 } 311 return 0; 312} 313 314static int pg_command(struct pg *dev, char *cmd, int dlen, unsigned long tmo) 315{ 316 int k; 317 318 pi_connect(dev->pi); 319 320 write_reg(dev, 6, DRIVE(dev)); 321 322 if (pg_wait(dev, STAT_BUSY | STAT_DRQ, 0, tmo, "before command")) 323 goto fail; 324 325 write_reg(dev, 4, dlen % 256); 326 write_reg(dev, 5, dlen / 256); 327 write_reg(dev, 7, 0xa0); /* ATAPI packet command */ 328 329 if (pg_wait(dev, STAT_BUSY, STAT_DRQ, tmo, "command DRQ")) 330 goto fail; 331 332 if (read_reg(dev, 2) != 1) { 333 printk("%s: command phase error\n", dev->name); 334 goto fail; 335 } 336 337 pi_write_block(dev->pi, cmd, 12); 338 339 if (verbose > 1) { 340 printk("%s: Command sent, dlen=%d packet= ", dev->name, dlen); 341 for (k = 0; k < 12; k++) 342 printk("%02x ", cmd[k] & 0xff); 343 printk("\n"); 344 } 345 return 0; 346fail: 347 pi_disconnect(dev->pi); 348 return -1; 349} 350 351static int pg_completion(struct pg *dev, char *buf, unsigned long tmo) 352{ 353 int r, d, n, p; 354 355 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR, 356 tmo, "completion"); 357 358 dev->dlen = 0; 359 360 while (read_reg(dev, 7) & STAT_DRQ) { 361 d = (read_reg(dev, 4) + 256 * read_reg(dev, 5)); 362 n = ((d + 3) & 0xfffc); 363 p = read_reg(dev, 2) & 3; 364 if (p == 0) 365 pi_write_block(dev->pi, buf, n); 366 if (p == 2) 367 pi_read_block(dev->pi, buf, n); 368 if (verbose > 1) 369 printk("%s: %s %d bytes\n", dev->name, 370 p ? "Read" : "Write", n); 371 dev->dlen += (1 - p) * d; 372 buf += d; 373 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR, 374 tmo, "completion"); 375 } 376 377 pi_disconnect(dev->pi); 378 379 return r; 380} 381 382static int pg_reset(struct pg *dev) 383{ 384 int i, k, err; 385 int expect[5] = { 1, 1, 1, 0x14, 0xeb }; 386 int got[5]; 387 388 pi_connect(dev->pi); 389 write_reg(dev, 6, DRIVE(dev)); 390 write_reg(dev, 7, 8); 391 392 pg_sleep(20 * HZ / 1000); 393 394 k = 0; 395 while ((k++ < PG_RESET_TMO) && (status_reg(dev) & STAT_BUSY)) 396 pg_sleep(1); 397 398 for (i = 0; i < 5; i++) 399 got[i] = read_reg(dev, i + 1); 400 401 err = memcmp(expect, got, sizeof(got)) ? -1 : 0; 402 403 if (verbose) { 404 printk("%s: Reset (%d) signature = ", dev->name, k); 405 for (i = 0; i < 5; i++) 406 printk("%3x", got[i]); 407 if (err) 408 printk(" (incorrect)"); 409 printk("\n"); 410 } 411 412 pi_disconnect(dev->pi); 413 return err; 414} 415 416static void xs(char *buf, char *targ, int len) 417{ 418 char l = '\0'; 419 int k; 420 421 for (k = 0; k < len; k++) { 422 char c = *buf++; 423 if (c != ' ' || c != l) 424 l = *targ++ = c; 425 } 426 if (l == ' ') 427 targ--; 428 *targ = '\0'; 429} 430 431static int pg_identify(struct pg *dev, int log) 432{ 433 int s; 434 char *ms[2] = { "master", "slave" }; 435 char mf[10], id[18]; 436 char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 }; 437 char buf[36]; 438 439 s = pg_command(dev, id_cmd, 36, jiffies + PG_TMO); 440 if (s) 441 return -1; 442 s = pg_completion(dev, buf, jiffies + PG_TMO); 443 if (s) 444 return -1; 445 446 if (log) { 447 xs(buf + 8, mf, 8); 448 xs(buf + 16, id, 16); 449 printk("%s: %s %s, %s\n", dev->name, mf, id, ms[dev->drive]); 450 } 451 452 return 0; 453} 454 455/* 456 * returns 0, with id set if drive is detected 457 * -1, if drive detection failed 458 */ 459static int pg_probe(struct pg *dev) 460{ 461 if (dev->drive == -1) { 462 for (dev->drive = 0; dev->drive <= 1; dev->drive++) 463 if (!pg_reset(dev)) 464 return pg_identify(dev, 1); 465 } else { 466 if (!pg_reset(dev)) 467 return pg_identify(dev, 1); 468 } 469 return -1; 470} 471 472static int pg_detect(void) 473{ 474 struct pg *dev = &devices[0]; 475 int k, unit; 476 477 printk("%s: %s version %s, major %d\n", name, name, PG_VERSION, major); 478 479 k = 0; 480 if (pg_drive_count == 0) { 481 if (pi_init(dev->pi, 1, -1, -1, -1, -1, -1, pg_scratch, 482 PI_PG, verbose, dev->name)) { 483 if (!pg_probe(dev)) { 484 dev->present = 1; 485 k++; 486 } else 487 pi_release(dev->pi); 488 } 489 490 } else 491 for (unit = 0; unit < PG_UNITS; unit++, dev++) { 492 int *parm = *drives[unit]; 493 if (!parm[D_PRT]) 494 continue; 495 if (pi_init(dev->pi, 0, parm[D_PRT], parm[D_MOD], 496 parm[D_UNI], parm[D_PRO], parm[D_DLY], 497 pg_scratch, PI_PG, verbose, dev->name)) { 498 if (!pg_probe(dev)) { 499 dev->present = 1; 500 k++; 501 } else 502 pi_release(dev->pi); 503 } 504 } 505 506 if (k) 507 return 0; 508 509 printk("%s: No ATAPI device detected\n", name); 510 return -1; 511} 512 513static int pg_open(struct inode *inode, struct file *file) 514{ 515 int unit = iminor(inode) & 0x7f; 516 struct pg *dev = &devices[unit]; 517 518 if ((unit >= PG_UNITS) || (!dev->present)) 519 return -ENODEV; 520 521 if (test_and_set_bit(0, &dev->access)) 522 return -EBUSY; 523 524 if (dev->busy) { 525 pg_reset(dev); 526 dev->busy = 0; 527 } 528 529 pg_identify(dev, (verbose > 1)); 530 531 dev->bufptr = kmalloc(PG_MAX_DATA, GFP_KERNEL); 532 if (dev->bufptr == NULL) { 533 clear_bit(0, &dev->access); 534 printk("%s: buffer allocation failed\n", dev->name); 535 return -ENOMEM; 536 } 537 538 file->private_data = dev; 539 540 return 0; 541} 542 543static int pg_release(struct inode *inode, struct file *file) 544{ 545 struct pg *dev = file->private_data; 546 547 kfree(dev->bufptr); 548 dev->bufptr = NULL; 549 clear_bit(0, &dev->access); 550 551 return 0; 552} 553 554static ssize_t pg_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos) 555{ 556 struct pg *dev = filp->private_data; 557 struct pg_write_hdr hdr; 558 int hs = sizeof (hdr); 559 560 if (dev->busy) 561 return -EBUSY; 562 if (count < hs) 563 return -EINVAL; 564 565 if (copy_from_user(&hdr, buf, hs)) 566 return -EFAULT; 567 568 if (hdr.magic != PG_MAGIC) 569 return -EINVAL; 570 if (hdr.dlen > PG_MAX_DATA) 571 return -EINVAL; 572 if ((count - hs) > PG_MAX_DATA) 573 return -EINVAL; 574 575 if (hdr.func == PG_RESET) { 576 if (count != hs) 577 return -EINVAL; 578 if (pg_reset(dev)) 579 return -EIO; 580 return count; 581 } 582 583 if (hdr.func != PG_COMMAND) 584 return -EINVAL; 585 586 dev->start = jiffies; 587 dev->timeout = hdr.timeout * HZ + HZ / 2 + jiffies; 588 589 if (pg_command(dev, hdr.packet, hdr.dlen, jiffies + PG_TMO)) { 590 if (dev->status & 0x10) 591 return -ETIME; 592 return -EIO; 593 } 594 595 dev->busy = 1; 596 597 if (copy_from_user(dev->bufptr, buf + hs, count - hs)) 598 return -EFAULT; 599 return count; 600} 601 602static ssize_t pg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) 603{ 604 struct pg *dev = filp->private_data; 605 struct pg_read_hdr hdr; 606 int hs = sizeof (hdr); 607 int copy; 608 609 if (!dev->busy) 610 return -EINVAL; 611 if (count < hs) 612 return -EINVAL; 613 614 dev->busy = 0; 615 616 if (pg_completion(dev, dev->bufptr, dev->timeout)) 617 if (dev->status & 0x10) 618 return -ETIME; 619 620 hdr.magic = PG_MAGIC; 621 hdr.dlen = dev->dlen; 622 copy = 0; 623 624 if (hdr.dlen < 0) { 625 hdr.dlen = -1 * hdr.dlen; 626 copy = hdr.dlen; 627 if (copy > (count - hs)) 628 copy = count - hs; 629 } 630 631 hdr.duration = (jiffies - dev->start + HZ / 2) / HZ; 632 hdr.scsi = dev->status & 0x0f; 633 634 if (copy_to_user(buf, &hdr, hs)) 635 return -EFAULT; 636 if (copy > 0) 637 if (copy_to_user(buf + hs, dev->bufptr, copy)) 638 return -EFAULT; 639 return copy + hs; 640} 641 642static int __init pg_init(void) 643{ 644 int unit, err = 0; 645 646 if (disable){ 647 err = -1; 648 goto out; 649 } 650 651 pg_init_units(); 652 653 if (pg_detect()) { 654 err = -1; 655 goto out; 656 } 657 658 if (register_chrdev(major, name, &pg_fops)) { 659 printk("pg_init: unable to get major number %d\n", major); 660 for (unit = 0; unit < PG_UNITS; unit++) { 661 struct pg *dev = &devices[unit]; 662 if (dev->present) 663 pi_release(dev->pi); 664 } 665 err = -1; 666 goto out; 667 } 668 pg_class = class_create(THIS_MODULE, "pg"); 669 if (IS_ERR(pg_class)) { 670 err = PTR_ERR(pg_class); 671 goto out_chrdev; 672 } 673 devfs_mk_dir("pg"); 674 for (unit = 0; unit < PG_UNITS; unit++) { 675 struct pg *dev = &devices[unit]; 676 if (dev->present) { 677 class_device_create(pg_class, MKDEV(major, unit), 678 NULL, "pg%u", unit); 679 err = devfs_mk_cdev(MKDEV(major, unit), 680 S_IFCHR | S_IRUSR | S_IWUSR, "pg/%u", 681 unit); 682 if (err) 683 goto out_class; 684 } 685 } 686 err = 0; 687 goto out; 688 689out_class: 690 class_device_destroy(pg_class, MKDEV(major, unit)); 691 class_destroy(pg_class); 692out_chrdev: 693 unregister_chrdev(major, "pg"); 694out: 695 return err; 696} 697 698static void __exit pg_exit(void) 699{ 700 int unit; 701 702 for (unit = 0; unit < PG_UNITS; unit++) { 703 struct pg *dev = &devices[unit]; 704 if (dev->present) { 705 class_device_destroy(pg_class, MKDEV(major, unit)); 706 devfs_remove("pg/%u", unit); 707 } 708 } 709 class_destroy(pg_class); 710 devfs_remove("pg"); 711 unregister_chrdev(major, name); 712 713 for (unit = 0; unit < PG_UNITS; unit++) { 714 struct pg *dev = &devices[unit]; 715 if (dev->present) 716 pi_release(dev->pi); 717 } 718} 719 720MODULE_LICENSE("GPL"); 721module_init(pg_init) 722module_exit(pg_exit)