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