initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / block / paride / pg.c
blobd73e3ec9f860ea3f028139c1ac081f33b33af0f4
1 /*
2 pg.c (c) 1998 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
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.
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.
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.
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".
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.
34 To use this device, you must have the following device
35 special files defined:
37 /dev/pg0 c 97 0
38 /dev/pg1 c 97 1
39 /dev/pg2 c 97 2
40 /dev/pg3 c 97 3
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.)
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:
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>
55 Where,
57 <prt> is the base of the parallel port address for
58 the corresponding drive. (required)
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)
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)
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)
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.
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)
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.
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").
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)
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:
107 pg.drive0
108 pg.drive1
109 pg.drive2
110 pg.drive3
112 In addition, you can use the parameter pg.disable to disable
113 the driver entirely.
117 /* Changes:
119 1.01 GRG 1998.06.16 Bug fixes
120 1.02 GRG 1998.09.24 Added jumbo support
124 #define PG_VERSION "1.02"
125 #define PG_MAJOR 97
126 #define PG_NAME "pg"
127 #define PG_UNITS 4
129 #ifndef PI_PG
130 #define PI_PG 4
131 #endif
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.
139 static int verbose = 0;
140 static int major = PG_MAJOR;
141 static char *name = PG_NAME;
142 static int disable = 0;
144 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
145 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
146 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
147 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
149 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
150 static int pg_drive_count;
152 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
154 /* end of parameters */
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>
166 #include <asm/uaccess.h>
168 #ifndef MODULE
170 #include "setup.h"
172 static STT pg_stt[5] = {
173 {"drive0", 6, drive0},
174 {"drive1", 6, drive1},
175 {"drive2", 6, drive2},
176 {"drive3", 6, drive3},
177 {"disable", 1, &disable}
180 void pg_setup(char *str, int *ints)
182 generic_setup(pg_stt, 5, str);
185 #endif
187 MODULE_PARM(verbose, "i");
188 MODULE_PARM(major, "i");
189 MODULE_PARM(name, "s");
190 MODULE_PARM(drive0, "1-6i");
191 MODULE_PARM(drive1, "1-6i");
192 MODULE_PARM(drive2, "1-6i");
193 MODULE_PARM(drive3, "1-6i");
195 #include "paride.h"
197 #define PG_SPIN_DEL 50 /* spin delay in micro-seconds */
198 #define PG_SPIN 200
199 #define PG_TMO HZ
200 #define PG_RESET_TMO 10*HZ
202 #define STAT_ERR 0x01
203 #define STAT_INDEX 0x02
204 #define STAT_ECC 0x04
205 #define STAT_DRQ 0x08
206 #define STAT_SEEK 0x10
207 #define STAT_WRERR 0x20
208 #define STAT_READY 0x40
209 #define STAT_BUSY 0x80
211 #define ATAPI_IDENTIFY 0x12
213 static int pg_open(struct inode *inode, struct file *file);
214 static int pg_release(struct inode *inode, struct file *file);
215 static ssize_t pg_read(struct file *filp, char __user *buf,
216 size_t count, loff_t * ppos);
217 static ssize_t pg_write(struct file *filp, const char __user *buf,
218 size_t count, loff_t * ppos);
219 static int pg_detect(void);
221 #define PG_NAMELEN 8
223 struct pg {
224 struct pi_adapter pia; /* interface to paride layer */
225 struct pi_adapter *pi;
226 int busy; /* write done, read expected */
227 int start; /* jiffies at command start */
228 int dlen; /* transfer size requested */
229 unsigned long timeout; /* timeout requested */
230 int status; /* last sense key */
231 int drive; /* drive */
232 unsigned long access; /* count of active opens ... */
233 int present; /* device present ? */
234 char *bufptr;
235 char name[PG_NAMELEN]; /* pg0, pg1, ... */
238 struct pg devices[PG_UNITS];
240 static int pg_identify(struct pg *dev, int log);
242 static char pg_scratch[512]; /* scratch block buffer */
244 static struct class_simple *pg_class;
246 /* kernel glue structures */
248 static struct file_operations pg_fops = {
249 .owner = THIS_MODULE,
250 .read = pg_read,
251 .write = pg_write,
252 .open = pg_open,
253 .release = pg_release,
256 static void pg_init_units(void)
258 int unit;
260 pg_drive_count = 0;
261 for (unit = 0; unit < PG_UNITS; unit++) {
262 int *parm = *drives[unit];
263 struct pg *dev = &devices[unit];
264 dev->pi = &dev->pia;
265 set_bit(0, &dev->access);
266 dev->busy = 0;
267 dev->present = 0;
268 dev->bufptr = NULL;
269 dev->drive = parm[D_SLV];
270 snprintf(dev->name, PG_NAMELEN, "%s%c", name, 'a'+unit);
271 if (parm[D_PRT])
272 pg_drive_count++;
276 static inline int status_reg(struct pg *dev)
278 return pi_read_regr(dev->pi, 1, 6);
281 static inline int read_reg(struct pg *dev, int reg)
283 return pi_read_regr(dev->pi, 0, reg);
286 static inline void write_reg(struct pg *dev, int reg, int val)
288 pi_write_regr(dev->pi, 0, reg, val);
291 static inline u8 DRIVE(struct pg *dev)
293 return 0xa0+0x10*dev->drive;
296 static void pg_sleep(int cs)
298 current->state = TASK_INTERRUPTIBLE;
299 schedule_timeout(cs);
302 static int pg_wait(struct pg *dev, int go, int stop, unsigned long tmo, char *msg)
304 int j, r, e, s, p, to;
306 dev->status = 0;
308 j = 0;
309 while ((((r = status_reg(dev)) & go) || (stop && (!(r & stop))))
310 && time_before(jiffies, tmo)) {
311 if (j++ < PG_SPIN)
312 udelay(PG_SPIN_DEL);
313 else
314 pg_sleep(1);
317 to = time_after_eq(jiffies, tmo);
319 if ((r & (STAT_ERR & stop)) || to) {
320 s = read_reg(dev, 7);
321 e = read_reg(dev, 1);
322 p = read_reg(dev, 2);
323 if (verbose > 1)
324 printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
325 dev->name, msg, s, e, p, to ? " timeout" : "");
326 if (to)
327 e |= 0x100;
328 dev->status = (e >> 4) & 0xff;
329 return -1;
331 return 0;
334 static int pg_command(struct pg *dev, char *cmd, int dlen, unsigned long tmo)
336 int k;
338 pi_connect(dev->pi);
340 write_reg(dev, 6, DRIVE(dev));
342 if (pg_wait(dev, STAT_BUSY | STAT_DRQ, 0, tmo, "before command"))
343 goto fail;
345 write_reg(dev, 4, dlen % 256);
346 write_reg(dev, 5, dlen / 256);
347 write_reg(dev, 7, 0xa0); /* ATAPI packet command */
349 if (pg_wait(dev, STAT_BUSY, STAT_DRQ, tmo, "command DRQ"))
350 goto fail;
352 if (read_reg(dev, 2) != 1) {
353 printk("%s: command phase error\n", dev->name);
354 goto fail;
357 pi_write_block(dev->pi, cmd, 12);
359 if (verbose > 1) {
360 printk("%s: Command sent, dlen=%d packet= ", dev->name, dlen);
361 for (k = 0; k < 12; k++)
362 printk("%02x ", cmd[k] & 0xff);
363 printk("\n");
365 return 0;
366 fail:
367 pi_disconnect(dev->pi);
368 return -1;
371 static int pg_completion(struct pg *dev, char *buf, unsigned long tmo)
373 int r, d, n, p;
375 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
376 tmo, "completion");
378 dev->dlen = 0;
380 while (read_reg(dev, 7) & STAT_DRQ) {
381 d = (read_reg(dev, 4) + 256 * read_reg(dev, 5));
382 n = ((d + 3) & 0xfffc);
383 p = read_reg(dev, 2) & 3;
384 if (p == 0)
385 pi_write_block(dev->pi, buf, n);
386 if (p == 2)
387 pi_read_block(dev->pi, buf, n);
388 if (verbose > 1)
389 printk("%s: %s %d bytes\n", dev->name,
390 p ? "Read" : "Write", n);
391 dev->dlen += (1 - p) * d;
392 buf += d;
393 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
394 tmo, "completion");
397 pi_disconnect(dev->pi);
399 return r;
402 static int pg_reset(struct pg *dev)
404 int i, k, err;
405 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
406 int got[5];
408 pi_connect(dev->pi);
409 write_reg(dev, 6, DRIVE(dev));
410 write_reg(dev, 7, 8);
412 pg_sleep(20 * HZ / 1000);
414 k = 0;
415 while ((k++ < PG_RESET_TMO) && (status_reg(dev) & STAT_BUSY))
416 pg_sleep(1);
418 for (i = 0; i < 5; i++)
419 got[i] = read_reg(dev, i + 1);
421 err = memcmp(expect, got, sizeof(got)) ? -1 : 0;
423 if (verbose) {
424 printk("%s: Reset (%d) signature = ", dev->name, k);
425 for (i = 0; i < 5; i++)
426 printk("%3x", got[i]);
427 if (err)
428 printk(" (incorrect)");
429 printk("\n");
432 pi_disconnect(dev->pi);
433 return err;
436 static void xs(char *buf, char *targ, int len)
438 char l = '\0';
439 int k;
441 for (k = 0; k < len; k++) {
442 char c = *buf++;
443 if (c != ' ' || c != l)
444 l = *targ++ = c;
446 if (l == ' ')
447 targ--;
448 *targ = '\0';
451 static int pg_identify(struct pg *dev, int log)
453 int s;
454 char *ms[2] = { "master", "slave" };
455 char mf[10], id[18];
456 char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
457 char buf[36];
459 s = pg_command(dev, id_cmd, 36, jiffies + PG_TMO);
460 if (s)
461 return -1;
462 s = pg_completion(dev, buf, jiffies + PG_TMO);
463 if (s)
464 return -1;
466 if (log) {
467 xs(buf + 8, mf, 8);
468 xs(buf + 16, id, 16);
469 printk("%s: %s %s, %s\n", dev->name, mf, id, ms[dev->drive]);
472 return 0;
476 * returns 0, with id set if drive is detected
477 * -1, if drive detection failed
479 static int pg_probe(struct pg *dev)
481 if (dev->drive == -1) {
482 for (dev->drive = 0; dev->drive <= 1; dev->drive++)
483 if (!pg_reset(dev))
484 return pg_identify(dev, 1);
485 } else {
486 if (!pg_reset(dev))
487 return pg_identify(dev, 1);
489 return -1;
492 static int pg_detect(void)
494 struct pg *dev = &devices[0];
495 int k, unit;
497 printk("%s: %s version %s, major %d\n", name, name, PG_VERSION, major);
499 k = 0;
500 if (pg_drive_count == 0) {
501 if (pi_init(dev->pi, 1, -1, -1, -1, -1, -1, pg_scratch,
502 PI_PG, verbose, dev->name)) {
503 if (!pg_probe(dev)) {
504 dev->present = 1;
505 k++;
506 } else
507 pi_release(dev->pi);
510 } else
511 for (unit = 0; unit < PG_UNITS; unit++, dev++) {
512 int *parm = *drives[unit];
513 if (!parm[D_PRT])
514 continue;
515 if (pi_init(dev->pi, 0, parm[D_PRT], parm[D_MOD],
516 parm[D_UNI], parm[D_PRO], parm[D_DLY],
517 pg_scratch, PI_PG, verbose, dev->name)) {
518 if (!pg_probe(dev)) {
519 dev->present = 1;
520 k++;
521 } else
522 pi_release(dev->pi);
526 if (k)
527 return 0;
529 printk("%s: No ATAPI device detected\n", name);
530 return -1;
533 static int pg_open(struct inode *inode, struct file *file)
535 int unit = iminor(inode) & 0x7f;
536 struct pg *dev = &devices[unit];
538 if ((unit >= PG_UNITS) || (!dev->present))
539 return -ENODEV;
541 if (test_and_set_bit(0, &dev->access))
542 return -EBUSY;
544 if (dev->busy) {
545 pg_reset(dev);
546 dev->busy = 0;
549 pg_identify(dev, (verbose > 1));
551 dev->bufptr = kmalloc(PG_MAX_DATA, GFP_KERNEL);
552 if (dev->bufptr == NULL) {
553 clear_bit(0, &dev->access);
554 printk("%s: buffer allocation failed\n", dev->name);
555 return -ENOMEM;
558 file->private_data = dev;
560 return 0;
563 static int pg_release(struct inode *inode, struct file *file)
565 struct pg *dev = file->private_data;
567 kfree(dev->bufptr);
568 dev->bufptr = NULL;
569 clear_bit(0, &dev->access);
571 return 0;
574 static ssize_t pg_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
576 struct pg *dev = filp->private_data;
577 struct pg_write_hdr hdr;
578 int hs = sizeof (hdr);
580 if (dev->busy)
581 return -EBUSY;
582 if (count < hs)
583 return -EINVAL;
585 if (copy_from_user(&hdr, buf, hs))
586 return -EFAULT;
588 if (hdr.magic != PG_MAGIC)
589 return -EINVAL;
590 if (hdr.dlen > PG_MAX_DATA)
591 return -EINVAL;
592 if ((count - hs) > PG_MAX_DATA)
593 return -EINVAL;
595 if (hdr.func == PG_RESET) {
596 if (count != hs)
597 return -EINVAL;
598 if (pg_reset(dev))
599 return -EIO;
600 return count;
603 if (hdr.func != PG_COMMAND)
604 return -EINVAL;
606 dev->start = jiffies;
607 dev->timeout = hdr.timeout * HZ + HZ / 2 + jiffies;
609 if (pg_command(dev, hdr.packet, hdr.dlen, jiffies + PG_TMO)) {
610 if (dev->status & 0x10)
611 return -ETIME;
612 return -EIO;
615 dev->busy = 1;
617 if (copy_from_user(dev->bufptr, buf + hs, count - hs))
618 return -EFAULT;
619 return count;
622 static ssize_t pg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
624 struct pg *dev = filp->private_data;
625 struct pg_read_hdr hdr;
626 int hs = sizeof (hdr);
627 int copy;
629 if (!dev->busy)
630 return -EINVAL;
631 if (count < hs)
632 return -EINVAL;
634 dev->busy = 0;
636 if (pg_completion(dev, dev->bufptr, dev->timeout))
637 if (dev->status & 0x10)
638 return -ETIME;
640 hdr.magic = PG_MAGIC;
641 hdr.dlen = dev->dlen;
642 copy = 0;
644 if (hdr.dlen < 0) {
645 hdr.dlen = -1 * hdr.dlen;
646 copy = hdr.dlen;
647 if (copy > (count - hs))
648 copy = count - hs;
651 hdr.duration = (jiffies - dev->start + HZ / 2) / HZ;
652 hdr.scsi = dev->status & 0x0f;
654 if (copy_to_user(buf, &hdr, hs))
655 return -EFAULT;
656 if (copy > 0)
657 if (copy_to_user(buf + hs, dev->bufptr, copy))
658 return -EFAULT;
659 return copy + hs;
662 static int __init pg_init(void)
664 int unit, err = 0;
666 if (disable){
667 err = -1;
668 goto out;
671 pg_init_units();
673 if (pg_detect()) {
674 err = -1;
675 goto out;
678 if (register_chrdev(major, name, &pg_fops)) {
679 printk("pg_init: unable to get major number %d\n", major);
680 for (unit = 0; unit < PG_UNITS; unit++) {
681 struct pg *dev = &devices[unit];
682 if (dev->present)
683 pi_release(dev->pi);
685 err = -1;
686 goto out;
688 pg_class = class_simple_create(THIS_MODULE, "pg");
689 if (IS_ERR(pg_class)) {
690 err = PTR_ERR(pg_class);
691 goto out_chrdev;
693 devfs_mk_dir("pg");
694 for (unit = 0; unit < PG_UNITS; unit++) {
695 struct pg *dev = &devices[unit];
696 if (dev->present) {
697 class_simple_device_add(pg_class, MKDEV(major, unit),
698 NULL, "pg%u", unit);
699 err = devfs_mk_cdev(MKDEV(major, unit),
700 S_IFCHR | S_IRUSR | S_IWUSR, "pg/%u",
701 unit);
702 if (err)
703 goto out_class;
706 err = 0;
707 goto out;
709 out_class:
710 class_simple_device_remove(MKDEV(major, unit));
711 class_simple_destroy(pg_class);
712 out_chrdev:
713 unregister_chrdev(major, "pg");
714 out:
715 return err;
718 static void __exit pg_exit(void)
720 int unit;
722 for (unit = 0; unit < PG_UNITS; unit++) {
723 struct pg *dev = &devices[unit];
724 if (dev->present) {
725 class_simple_device_remove(MKDEV(major, unit));
726 devfs_remove("pg/%u", unit);
729 class_simple_destroy(pg_class);
730 devfs_remove("pg");
731 unregister_chrdev(major, name);
733 for (unit = 0; unit < PG_UNITS; unit++) {
734 struct pg *dev = &devices[unit];
735 if (dev->present)
736 pi_release(dev->pi);
740 MODULE_LICENSE("GPL");
741 module_init(pg_init)
742 module_exit(pg_exit)