blob: d89e7d32a3b604716a63a03edf7ad0fd3492f6a5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#include <linux/delay.h>
160#include <linux/slab.h>
161#include <linux/mtio.h>
162#include <linux/pg.h>
163#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -0800164#include <linux/sched.h> /* current, TASK_* */
165#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167#include <asm/uaccess.h>
168
169module_param(verbose, bool, 0644);
170module_param(major, int, 0);
171module_param(name, charp, 0);
172module_param_array(drive0, int, NULL, 0);
173module_param_array(drive1, int, NULL, 0);
174module_param_array(drive2, int, NULL, 0);
175module_param_array(drive3, int, NULL, 0);
176
177#include "paride.h"
178
179#define PG_SPIN_DEL 50 /* spin delay in micro-seconds */
180#define PG_SPIN 200
181#define PG_TMO HZ
182#define PG_RESET_TMO 10*HZ
183
184#define STAT_ERR 0x01
185#define STAT_INDEX 0x02
186#define STAT_ECC 0x04
187#define STAT_DRQ 0x08
188#define STAT_SEEK 0x10
189#define STAT_WRERR 0x20
190#define STAT_READY 0x40
191#define STAT_BUSY 0x80
192
193#define ATAPI_IDENTIFY 0x12
194
195static int pg_open(struct inode *inode, struct file *file);
196static int pg_release(struct inode *inode, struct file *file);
197static ssize_t pg_read(struct file *filp, char __user *buf,
198 size_t count, loff_t * ppos);
199static ssize_t pg_write(struct file *filp, const char __user *buf,
200 size_t count, loff_t * ppos);
201static int pg_detect(void);
202
203#define PG_NAMELEN 8
204
205struct pg {
206 struct pi_adapter pia; /* interface to paride layer */
207 struct pi_adapter *pi;
208 int busy; /* write done, read expected */
209 int start; /* jiffies at command start */
210 int dlen; /* transfer size requested */
211 unsigned long timeout; /* timeout requested */
212 int status; /* last sense key */
213 int drive; /* drive */
214 unsigned long access; /* count of active opens ... */
215 int present; /* device present ? */
216 char *bufptr;
217 char name[PG_NAMELEN]; /* pg0, pg1, ... */
218};
219
220static struct pg devices[PG_UNITS];
221
222static int pg_identify(struct pg *dev, int log);
223
224static char pg_scratch[512]; /* scratch block buffer */
225
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800226static struct class *pg_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228/* kernel glue structures */
229
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800230static const struct file_operations pg_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .owner = THIS_MODULE,
232 .read = pg_read,
233 .write = pg_write,
234 .open = pg_open,
235 .release = pg_release,
236};
237
238static void pg_init_units(void)
239{
240 int unit;
241
242 pg_drive_count = 0;
243 for (unit = 0; unit < PG_UNITS; unit++) {
244 int *parm = *drives[unit];
245 struct pg *dev = &devices[unit];
246 dev->pi = &dev->pia;
247 clear_bit(0, &dev->access);
248 dev->busy = 0;
249 dev->present = 0;
250 dev->bufptr = NULL;
251 dev->drive = parm[D_SLV];
252 snprintf(dev->name, PG_NAMELEN, "%s%c", name, 'a'+unit);
253 if (parm[D_PRT])
254 pg_drive_count++;
255 }
256}
257
258static inline int status_reg(struct pg *dev)
259{
260 return pi_read_regr(dev->pi, 1, 6);
261}
262
263static inline int read_reg(struct pg *dev, int reg)
264{
265 return pi_read_regr(dev->pi, 0, reg);
266}
267
268static inline void write_reg(struct pg *dev, int reg, int val)
269{
270 pi_write_regr(dev->pi, 0, reg, val);
271}
272
273static inline u8 DRIVE(struct pg *dev)
274{
275 return 0xa0+0x10*dev->drive;
276}
277
278static void pg_sleep(int cs)
279{
Nishanth Aravamudan86e84862005-09-10 00:27:28 -0700280 schedule_timeout_interruptible(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283static int pg_wait(struct pg *dev, int go, int stop, unsigned long tmo, char *msg)
284{
285 int j, r, e, s, p, to;
286
287 dev->status = 0;
288
289 j = 0;
290 while ((((r = status_reg(dev)) & go) || (stop && (!(r & stop))))
291 && time_before(jiffies, tmo)) {
292 if (j++ < PG_SPIN)
293 udelay(PG_SPIN_DEL);
294 else
295 pg_sleep(1);
296 }
297
298 to = time_after_eq(jiffies, tmo);
299
300 if ((r & (STAT_ERR & stop)) || to) {
301 s = read_reg(dev, 7);
302 e = read_reg(dev, 1);
303 p = read_reg(dev, 2);
304 if (verbose > 1)
305 printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
306 dev->name, msg, s, e, p, to ? " timeout" : "");
307 if (to)
308 e |= 0x100;
309 dev->status = (e >> 4) & 0xff;
310 return -1;
311 }
312 return 0;
313}
314
315static int pg_command(struct pg *dev, char *cmd, int dlen, unsigned long tmo)
316{
317 int k;
318
319 pi_connect(dev->pi);
320
321 write_reg(dev, 6, DRIVE(dev));
322
323 if (pg_wait(dev, STAT_BUSY | STAT_DRQ, 0, tmo, "before command"))
324 goto fail;
325
326 write_reg(dev, 4, dlen % 256);
327 write_reg(dev, 5, dlen / 256);
328 write_reg(dev, 7, 0xa0); /* ATAPI packet command */
329
330 if (pg_wait(dev, STAT_BUSY, STAT_DRQ, tmo, "command DRQ"))
331 goto fail;
332
333 if (read_reg(dev, 2) != 1) {
334 printk("%s: command phase error\n", dev->name);
335 goto fail;
336 }
337
338 pi_write_block(dev->pi, cmd, 12);
339
340 if (verbose > 1) {
341 printk("%s: Command sent, dlen=%d packet= ", dev->name, dlen);
342 for (k = 0; k < 12; k++)
343 printk("%02x ", cmd[k] & 0xff);
344 printk("\n");
345 }
346 return 0;
347fail:
348 pi_disconnect(dev->pi);
349 return -1;
350}
351
352static int pg_completion(struct pg *dev, char *buf, unsigned long tmo)
353{
354 int r, d, n, p;
355
356 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
357 tmo, "completion");
358
359 dev->dlen = 0;
360
361 while (read_reg(dev, 7) & STAT_DRQ) {
362 d = (read_reg(dev, 4) + 256 * read_reg(dev, 5));
363 n = ((d + 3) & 0xfffc);
364 p = read_reg(dev, 2) & 3;
365 if (p == 0)
366 pi_write_block(dev->pi, buf, n);
367 if (p == 2)
368 pi_read_block(dev->pi, buf, n);
369 if (verbose > 1)
370 printk("%s: %s %d bytes\n", dev->name,
371 p ? "Read" : "Write", n);
372 dev->dlen += (1 - p) * d;
373 buf += d;
374 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
375 tmo, "completion");
376 }
377
378 pi_disconnect(dev->pi);
379
380 return r;
381}
382
383static int pg_reset(struct pg *dev)
384{
385 int i, k, err;
386 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
387 int got[5];
388
389 pi_connect(dev->pi);
390 write_reg(dev, 6, DRIVE(dev));
391 write_reg(dev, 7, 8);
392
393 pg_sleep(20 * HZ / 1000);
394
395 k = 0;
396 while ((k++ < PG_RESET_TMO) && (status_reg(dev) & STAT_BUSY))
397 pg_sleep(1);
398
399 for (i = 0; i < 5; i++)
400 got[i] = read_reg(dev, i + 1);
401
402 err = memcmp(expect, got, sizeof(got)) ? -1 : 0;
403
404 if (verbose) {
405 printk("%s: Reset (%d) signature = ", dev->name, k);
406 for (i = 0; i < 5; i++)
407 printk("%3x", got[i]);
408 if (err)
409 printk(" (incorrect)");
410 printk("\n");
411 }
412
413 pi_disconnect(dev->pi);
414 return err;
415}
416
417static void xs(char *buf, char *targ, int len)
418{
419 char l = '\0';
420 int k;
421
422 for (k = 0; k < len; k++) {
423 char c = *buf++;
424 if (c != ' ' || c != l)
425 l = *targ++ = c;
426 }
427 if (l == ' ')
428 targ--;
429 *targ = '\0';
430}
431
432static int pg_identify(struct pg *dev, int log)
433{
434 int s;
435 char *ms[2] = { "master", "slave" };
436 char mf[10], id[18];
437 char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
438 char buf[36];
439
440 s = pg_command(dev, id_cmd, 36, jiffies + PG_TMO);
441 if (s)
442 return -1;
443 s = pg_completion(dev, buf, jiffies + PG_TMO);
444 if (s)
445 return -1;
446
447 if (log) {
448 xs(buf + 8, mf, 8);
449 xs(buf + 16, id, 16);
450 printk("%s: %s %s, %s\n", dev->name, mf, id, ms[dev->drive]);
451 }
452
453 return 0;
454}
455
456/*
457 * returns 0, with id set if drive is detected
458 * -1, if drive detection failed
459 */
460static int pg_probe(struct pg *dev)
461{
462 if (dev->drive == -1) {
463 for (dev->drive = 0; dev->drive <= 1; dev->drive++)
464 if (!pg_reset(dev))
465 return pg_identify(dev, 1);
466 } else {
467 if (!pg_reset(dev))
468 return pg_identify(dev, 1);
469 }
470 return -1;
471}
472
473static int pg_detect(void)
474{
475 struct pg *dev = &devices[0];
476 int k, unit;
477
478 printk("%s: %s version %s, major %d\n", name, name, PG_VERSION, major);
479
480 k = 0;
481 if (pg_drive_count == 0) {
482 if (pi_init(dev->pi, 1, -1, -1, -1, -1, -1, pg_scratch,
483 PI_PG, verbose, dev->name)) {
484 if (!pg_probe(dev)) {
485 dev->present = 1;
486 k++;
487 } else
488 pi_release(dev->pi);
489 }
490
491 } else
492 for (unit = 0; unit < PG_UNITS; unit++, dev++) {
493 int *parm = *drives[unit];
494 if (!parm[D_PRT])
495 continue;
496 if (pi_init(dev->pi, 0, parm[D_PRT], parm[D_MOD],
497 parm[D_UNI], parm[D_PRO], parm[D_DLY],
498 pg_scratch, PI_PG, verbose, dev->name)) {
499 if (!pg_probe(dev)) {
500 dev->present = 1;
501 k++;
502 } else
503 pi_release(dev->pi);
504 }
505 }
506
507 if (k)
508 return 0;
509
510 printk("%s: No ATAPI device detected\n", name);
511 return -1;
512}
513
514static int pg_open(struct inode *inode, struct file *file)
515{
516 int unit = iminor(inode) & 0x7f;
517 struct pg *dev = &devices[unit];
518
519 if ((unit >= PG_UNITS) || (!dev->present))
520 return -ENODEV;
521
522 if (test_and_set_bit(0, &dev->access))
523 return -EBUSY;
524
525 if (dev->busy) {
526 pg_reset(dev);
527 dev->busy = 0;
528 }
529
530 pg_identify(dev, (verbose > 1));
531
532 dev->bufptr = kmalloc(PG_MAX_DATA, GFP_KERNEL);
533 if (dev->bufptr == NULL) {
534 clear_bit(0, &dev->access);
535 printk("%s: buffer allocation failed\n", dev->name);
536 return -ENOMEM;
537 }
538
539 file->private_data = dev;
540
541 return 0;
542}
543
544static int pg_release(struct inode *inode, struct file *file)
545{
546 struct pg *dev = file->private_data;
547
548 kfree(dev->bufptr);
549 dev->bufptr = NULL;
550 clear_bit(0, &dev->access);
551
552 return 0;
553}
554
555static ssize_t pg_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
556{
557 struct pg *dev = filp->private_data;
558 struct pg_write_hdr hdr;
559 int hs = sizeof (hdr);
560
561 if (dev->busy)
562 return -EBUSY;
563 if (count < hs)
564 return -EINVAL;
565
566 if (copy_from_user(&hdr, buf, hs))
567 return -EFAULT;
568
569 if (hdr.magic != PG_MAGIC)
570 return -EINVAL;
571 if (hdr.dlen > PG_MAX_DATA)
572 return -EINVAL;
573 if ((count - hs) > PG_MAX_DATA)
574 return -EINVAL;
575
576 if (hdr.func == PG_RESET) {
577 if (count != hs)
578 return -EINVAL;
579 if (pg_reset(dev))
580 return -EIO;
581 return count;
582 }
583
584 if (hdr.func != PG_COMMAND)
585 return -EINVAL;
586
587 dev->start = jiffies;
588 dev->timeout = hdr.timeout * HZ + HZ / 2 + jiffies;
589
590 if (pg_command(dev, hdr.packet, hdr.dlen, jiffies + PG_TMO)) {
591 if (dev->status & 0x10)
592 return -ETIME;
593 return -EIO;
594 }
595
596 dev->busy = 1;
597
598 if (copy_from_user(dev->bufptr, buf + hs, count - hs))
599 return -EFAULT;
600 return count;
601}
602
603static ssize_t pg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
604{
605 struct pg *dev = filp->private_data;
606 struct pg_read_hdr hdr;
607 int hs = sizeof (hdr);
608 int copy;
609
610 if (!dev->busy)
611 return -EINVAL;
612 if (count < hs)
613 return -EINVAL;
614
615 dev->busy = 0;
616
617 if (pg_completion(dev, dev->bufptr, dev->timeout))
618 if (dev->status & 0x10)
619 return -ETIME;
620
621 hdr.magic = PG_MAGIC;
622 hdr.dlen = dev->dlen;
623 copy = 0;
624
625 if (hdr.dlen < 0) {
626 hdr.dlen = -1 * hdr.dlen;
627 copy = hdr.dlen;
628 if (copy > (count - hs))
629 copy = count - hs;
630 }
631
632 hdr.duration = (jiffies - dev->start + HZ / 2) / HZ;
633 hdr.scsi = dev->status & 0x0f;
634
635 if (copy_to_user(buf, &hdr, hs))
636 return -EFAULT;
637 if (copy > 0)
638 if (copy_to_user(buf + hs, dev->bufptr, copy))
639 return -EFAULT;
640 return copy + hs;
641}
642
643static int __init pg_init(void)
644{
Andrew Morton86379802006-03-28 01:56:18 -0800645 int unit;
646 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (disable){
Akinobu Mita8bca98c2006-12-06 20:36:43 -0800649 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto out;
651 }
652
653 pg_init_units();
654
655 if (pg_detect()) {
Akinobu Mita8bca98c2006-12-06 20:36:43 -0800656 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto out;
658 }
659
Andrew Morton86379802006-03-28 01:56:18 -0800660 err = register_chrdev(major, name, &pg_fops);
661 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 printk("pg_init: unable to get major number %d\n", major);
663 for (unit = 0; unit < PG_UNITS; unit++) {
664 struct pg *dev = &devices[unit];
665 if (dev->present)
666 pi_release(dev->pi);
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto out;
669 }
Andrew Morton86379802006-03-28 01:56:18 -0800670 major = err; /* In case the user specified `major=0' (dynamic) */
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800671 pg_class = class_create(THIS_MODULE, "pg");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (IS_ERR(pg_class)) {
673 err = PTR_ERR(pg_class);
674 goto out_chrdev;
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 for (unit = 0; unit < PG_UNITS; unit++) {
677 struct pg *dev = &devices[unit];
Greg Kroah-Hartman7c69ef72005-06-20 21:15:16 -0700678 if (dev->present)
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700679 class_device_create(pg_class, NULL, MKDEV(major, unit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 NULL, "pg%u", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 err = 0;
683 goto out;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685out_chrdev:
686 unregister_chrdev(major, "pg");
687out:
688 return err;
689}
690
691static void __exit pg_exit(void)
692{
693 int unit;
694
695 for (unit = 0; unit < PG_UNITS; unit++) {
696 struct pg *dev = &devices[unit];
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -0700697 if (dev->present)
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800698 class_device_destroy(pg_class, MKDEV(major, unit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
gregkh@suse.dedeb36972005-03-23 09:52:10 -0800700 class_destroy(pg_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unregister_chrdev(major, name);
702
703 for (unit = 0; unit < PG_UNITS; unit++) {
704 struct pg *dev = &devices[unit];
705 if (dev->present)
706 pi_release(dev->pi);
707 }
708}
709
710MODULE_LICENSE("GPL");
711module_init(pg_init)
712module_exit(pg_exit)