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
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
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:
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
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:
53 drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
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.
71 <mod> this can be -1 to choose the best mode, or one
72 of the mode numbers supported by the adapter.
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
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
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).
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:
112 In addition, you can use the parameter pg.disable to disable
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"
133 #include <linux/types.h>
134 /* Here are things one can override from the insmod command.
135 Most are autoprobed by paride unless set here. Verbose is 0
141 static int major
= PG_MAJOR
;
142 static char *name
= PG_NAME
;
143 static int disable
= 0;
145 static int drive0
[6] = { 0, 0, 0, -1, -1, -1 };
146 static int drive1
[6] = { 0, 0, 0, -1, -1, -1 };
147 static int drive2
[6] = { 0, 0, 0, -1, -1, -1 };
148 static int drive3
[6] = { 0, 0, 0, -1, -1, -1 };
150 static int (*drives
[4])[6] = {&drive0
, &drive1
, &drive2
, &drive3
};
151 static int pg_drive_count
;
153 enum {D_PRT
, D_PRO
, D_UNI
, D_MOD
, D_SLV
, D_DLY
};
155 /* end of parameters */
157 #include <linux/module.h>
158 #include <linux/init.h>
159 #include <linux/fs.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 #include <linux/sched.h> /* current, TASK_* */
166 #include <linux/mutex.h>
167 #include <linux/jiffies.h>
169 #include <linux/uaccess.h>
171 module_param(verbose
, int, 0644);
172 module_param(major
, int, 0);
173 module_param(name
, charp
, 0);
174 module_param_array(drive0
, int, NULL
, 0);
175 module_param_array(drive1
, int, NULL
, 0);
176 module_param_array(drive2
, int, NULL
, 0);
177 module_param_array(drive3
, int, NULL
, 0);
181 #define PG_SPIN_DEL 50 /* spin delay in micro-seconds */
184 #define PG_RESET_TMO 10*HZ
186 #define STAT_ERR 0x01
187 #define STAT_INDEX 0x02
188 #define STAT_ECC 0x04
189 #define STAT_DRQ 0x08
190 #define STAT_SEEK 0x10
191 #define STAT_WRERR 0x20
192 #define STAT_READY 0x40
193 #define STAT_BUSY 0x80
195 #define ATAPI_IDENTIFY 0x12
197 static DEFINE_MUTEX(pg_mutex
);
198 static int pg_open(struct inode
*inode
, struct file
*file
);
199 static int pg_release(struct inode
*inode
, struct file
*file
);
200 static ssize_t
pg_read(struct file
*filp
, char __user
*buf
,
201 size_t count
, loff_t
* ppos
);
202 static ssize_t
pg_write(struct file
*filp
, const char __user
*buf
,
203 size_t count
, loff_t
* ppos
);
204 static int pg_detect(void);
209 struct pi_adapter pia
; /* interface to paride layer */
210 struct pi_adapter
*pi
;
211 int busy
; /* write done, read expected */
212 int start
; /* jiffies at command start */
213 int dlen
; /* transfer size requested */
214 unsigned long timeout
; /* timeout requested */
215 int status
; /* last sense key */
216 int drive
; /* drive */
217 unsigned long access
; /* count of active opens ... */
218 int present
; /* device present ? */
220 char name
[PG_NAMELEN
]; /* pg0, pg1, ... */
223 static struct pg devices
[PG_UNITS
];
225 static int pg_identify(struct pg
*dev
, int log
);
227 static char pg_scratch
[512]; /* scratch block buffer */
229 static struct class *pg_class
;
230 static void *par_drv
; /* reference of parport driver */
232 /* kernel glue structures */
234 static const struct file_operations pg_fops
= {
235 .owner
= THIS_MODULE
,
239 .release
= pg_release
,
240 .llseek
= noop_llseek
,
243 static void pg_init_units(void)
248 for (unit
= 0; unit
< PG_UNITS
; unit
++) {
249 int *parm
= *drives
[unit
];
250 struct pg
*dev
= &devices
[unit
];
252 clear_bit(0, &dev
->access
);
256 dev
->drive
= parm
[D_SLV
];
257 snprintf(dev
->name
, PG_NAMELEN
, "%s%c", name
, 'a'+unit
);
263 static inline int status_reg(struct pg
*dev
)
265 return pi_read_regr(dev
->pi
, 1, 6);
268 static inline int read_reg(struct pg
*dev
, int reg
)
270 return pi_read_regr(dev
->pi
, 0, reg
);
273 static inline void write_reg(struct pg
*dev
, int reg
, int val
)
275 pi_write_regr(dev
->pi
, 0, reg
, val
);
278 static inline u8
DRIVE(struct pg
*dev
)
280 return 0xa0+0x10*dev
->drive
;
283 static void pg_sleep(int cs
)
285 schedule_timeout_interruptible(cs
);
288 static int pg_wait(struct pg
*dev
, int go
, int stop
, unsigned long tmo
, char *msg
)
290 int j
, r
, e
, s
, p
, to
;
295 while ((((r
= status_reg(dev
)) & go
) || (stop
&& (!(r
& stop
))))
296 && time_before(jiffies
, tmo
)) {
303 to
= time_after_eq(jiffies
, tmo
);
305 if ((r
& (STAT_ERR
& stop
)) || to
) {
306 s
= read_reg(dev
, 7);
307 e
= read_reg(dev
, 1);
308 p
= read_reg(dev
, 2);
310 printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
311 dev
->name
, msg
, s
, e
, p
, to
? " timeout" : "");
314 dev
->status
= (e
>> 4) & 0xff;
320 static int pg_command(struct pg
*dev
, char *cmd
, int dlen
, unsigned long tmo
)
326 write_reg(dev
, 6, DRIVE(dev
));
328 if (pg_wait(dev
, STAT_BUSY
| STAT_DRQ
, 0, tmo
, "before command"))
331 write_reg(dev
, 4, dlen
% 256);
332 write_reg(dev
, 5, dlen
/ 256);
333 write_reg(dev
, 7, 0xa0); /* ATAPI packet command */
335 if (pg_wait(dev
, STAT_BUSY
, STAT_DRQ
, tmo
, "command DRQ"))
338 if (read_reg(dev
, 2) != 1) {
339 printk("%s: command phase error\n", dev
->name
);
343 pi_write_block(dev
->pi
, cmd
, 12);
346 printk("%s: Command sent, dlen=%d packet= ", dev
->name
, dlen
);
347 for (k
= 0; k
< 12; k
++)
348 printk("%02x ", cmd
[k
] & 0xff);
353 pi_disconnect(dev
->pi
);
357 static int pg_completion(struct pg
*dev
, char *buf
, unsigned long tmo
)
361 r
= pg_wait(dev
, STAT_BUSY
, STAT_DRQ
| STAT_READY
| STAT_ERR
,
366 while (read_reg(dev
, 7) & STAT_DRQ
) {
367 d
= (read_reg(dev
, 4) + 256 * read_reg(dev
, 5));
368 n
= ((d
+ 3) & 0xfffc);
369 p
= read_reg(dev
, 2) & 3;
371 pi_write_block(dev
->pi
, buf
, n
);
373 pi_read_block(dev
->pi
, buf
, n
);
375 printk("%s: %s %d bytes\n", dev
->name
,
376 p
? "Read" : "Write", n
);
377 dev
->dlen
+= (1 - p
) * d
;
379 r
= pg_wait(dev
, STAT_BUSY
, STAT_DRQ
| STAT_READY
| STAT_ERR
,
383 pi_disconnect(dev
->pi
);
388 static int pg_reset(struct pg
*dev
)
391 int expect
[5] = { 1, 1, 1, 0x14, 0xeb };
395 write_reg(dev
, 6, DRIVE(dev
));
396 write_reg(dev
, 7, 8);
398 pg_sleep(20 * HZ
/ 1000);
401 while ((k
++ < PG_RESET_TMO
) && (status_reg(dev
) & STAT_BUSY
))
404 for (i
= 0; i
< 5; i
++)
405 got
[i
] = read_reg(dev
, i
+ 1);
407 err
= memcmp(expect
, got
, sizeof(got
)) ? -1 : 0;
410 printk("%s: Reset (%d) signature = ", dev
->name
, k
);
411 for (i
= 0; i
< 5; i
++)
412 printk("%3x", got
[i
]);
414 printk(" (incorrect)");
418 pi_disconnect(dev
->pi
);
422 static void xs(char *buf
, char *targ
, int len
)
427 for (k
= 0; k
< len
; k
++) {
429 if (c
!= ' ' && c
!= l
)
437 static int pg_identify(struct pg
*dev
, int log
)
440 char *ms
[2] = { "master", "slave" };
442 char id_cmd
[12] = { ATAPI_IDENTIFY
, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
445 s
= pg_command(dev
, id_cmd
, 36, jiffies
+ PG_TMO
);
448 s
= pg_completion(dev
, buf
, jiffies
+ PG_TMO
);
454 xs(buf
+ 16, id
, 16);
455 printk("%s: %s %s, %s\n", dev
->name
, mf
, id
, ms
[dev
->drive
]);
462 * returns 0, with id set if drive is detected
463 * -1, if drive detection failed
465 static int pg_probe(struct pg
*dev
)
467 if (dev
->drive
== -1) {
468 for (dev
->drive
= 0; dev
->drive
<= 1; dev
->drive
++)
470 return pg_identify(dev
, 1);
473 return pg_identify(dev
, 1);
478 static int pg_detect(void)
480 struct pg
*dev
= &devices
[0];
483 printk("%s: %s version %s, major %d\n", name
, name
, PG_VERSION
, major
);
485 par_drv
= pi_register_driver(name
);
487 pr_err("failed to register %s driver\n", name
);
492 if (pg_drive_count
== 0) {
493 if (pi_init(dev
->pi
, 1, -1, -1, -1, -1, -1, pg_scratch
,
494 PI_PG
, verbose
, dev
->name
)) {
495 if (!pg_probe(dev
)) {
503 for (unit
= 0; unit
< PG_UNITS
; unit
++, dev
++) {
504 int *parm
= *drives
[unit
];
507 if (pi_init(dev
->pi
, 0, parm
[D_PRT
], parm
[D_MOD
],
508 parm
[D_UNI
], parm
[D_PRO
], parm
[D_DLY
],
509 pg_scratch
, PI_PG
, verbose
, dev
->name
)) {
510 if (!pg_probe(dev
)) {
521 pi_unregister_driver(par_drv
);
522 printk("%s: No ATAPI device detected\n", name
);
526 static int pg_open(struct inode
*inode
, struct file
*file
)
528 int unit
= iminor(inode
) & 0x7f;
529 struct pg
*dev
= &devices
[unit
];
532 mutex_lock(&pg_mutex
);
533 if ((unit
>= PG_UNITS
) || (!dev
->present
)) {
538 if (test_and_set_bit(0, &dev
->access
)) {
548 pg_identify(dev
, (verbose
> 1));
550 dev
->bufptr
= kmalloc(PG_MAX_DATA
, GFP_KERNEL
);
551 if (dev
->bufptr
== NULL
) {
552 clear_bit(0, &dev
->access
);
553 printk("%s: buffer allocation failed\n", dev
->name
);
558 file
->private_data
= dev
;
561 mutex_unlock(&pg_mutex
);
565 static int pg_release(struct inode
*inode
, struct file
*file
)
567 struct pg
*dev
= file
->private_data
;
571 clear_bit(0, &dev
->access
);
576 static ssize_t
pg_write(struct file
*filp
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
578 struct pg
*dev
= filp
->private_data
;
579 struct pg_write_hdr hdr
;
580 int hs
= sizeof (hdr
);
587 if (copy_from_user(&hdr
, buf
, hs
))
590 if (hdr
.magic
!= PG_MAGIC
)
592 if (hdr
.dlen
< 0 || hdr
.dlen
> PG_MAX_DATA
)
594 if ((count
- hs
) > PG_MAX_DATA
)
597 if (hdr
.func
== PG_RESET
) {
605 if (hdr
.func
!= PG_COMMAND
)
608 dev
->start
= jiffies
;
609 dev
->timeout
= hdr
.timeout
* HZ
+ HZ
/ 2 + jiffies
;
611 if (pg_command(dev
, hdr
.packet
, hdr
.dlen
, jiffies
+ PG_TMO
)) {
612 if (dev
->status
& 0x10)
619 if (copy_from_user(dev
->bufptr
, buf
+ hs
, count
- hs
))
624 static ssize_t
pg_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
626 struct pg
*dev
= filp
->private_data
;
627 struct pg_read_hdr hdr
;
628 int hs
= sizeof (hdr
);
638 if (pg_completion(dev
, dev
->bufptr
, dev
->timeout
))
639 if (dev
->status
& 0x10)
642 memset(&hdr
, 0, sizeof(hdr
));
643 hdr
.magic
= PG_MAGIC
;
644 hdr
.dlen
= dev
->dlen
;
648 hdr
.dlen
= -1 * hdr
.dlen
;
650 if (copy
> (count
- hs
))
654 hdr
.duration
= (jiffies
- dev
->start
+ HZ
/ 2) / HZ
;
655 hdr
.scsi
= dev
->status
& 0x0f;
657 if (copy_to_user(buf
, &hdr
, hs
))
660 if (copy_to_user(buf
+ hs
, dev
->bufptr
, copy
))
665 static int __init
pg_init(void)
682 err
= register_chrdev(major
, name
, &pg_fops
);
684 printk("pg_init: unable to get major number %d\n", major
);
685 for (unit
= 0; unit
< PG_UNITS
; unit
++) {
686 struct pg
*dev
= &devices
[unit
];
692 major
= err
; /* In case the user specified `major=0' (dynamic) */
693 pg_class
= class_create(THIS_MODULE
, "pg");
694 if (IS_ERR(pg_class
)) {
695 err
= PTR_ERR(pg_class
);
698 for (unit
= 0; unit
< PG_UNITS
; unit
++) {
699 struct pg
*dev
= &devices
[unit
];
701 device_create(pg_class
, NULL
, MKDEV(major
, unit
), NULL
,
708 unregister_chrdev(major
, "pg");
713 static void __exit
pg_exit(void)
717 for (unit
= 0; unit
< PG_UNITS
; unit
++) {
718 struct pg
*dev
= &devices
[unit
];
720 device_destroy(pg_class
, MKDEV(major
, unit
));
722 class_destroy(pg_class
);
723 unregister_chrdev(major
, name
);
725 for (unit
= 0; unit
< PG_UNITS
; unit
++) {
726 struct pg
*dev
= &devices
[unit
];
732 MODULE_LICENSE("GPL");