124 #define PG_VERSION "1.02"
133 #include <linux/types.h>
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;
157 #include <linux/module.h>
159 #include <linux/fs.h>
161 #include <linux/slab.h>
164 #include <linux/device.h>
165 #include <linux/sched.h>
169 #include <asm/uaccess.h>
181 #define PG_SPIN_DEL 50
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
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);
225 static int pg_identify(
struct pg *
dev,
int log);
227 static char pg_scratch[512];
229 static struct class *pg_class;
238 .release = pg_release,
242 static void pg_init_units(
void)
247 for (unit = 0; unit <
PG_UNITS; unit++) {
262 static inline int status_reg(
struct pg *
dev)
277 static inline u8 DRIVE(
struct pg *
dev)
279 return 0xa0+0x10*dev->
drive;
282 static void pg_sleep(
int cs)
287 static int pg_wait(
struct pg *
dev,
int go,
int stop,
unsigned long tmo,
char *
msg)
289 int j,
r,
e,
s,
p, to;
294 while ((((r = status_reg(dev)) & go) || (stop && (!(r & stop))))
304 if ((r & (
STAT_ERR & stop)) || to) {
309 printk(
"%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
310 dev->
name, msg, s, e, p, to ?
" timeout" :
"");
313 dev->
status = (e >> 4) & 0xff;
319 static int pg_command(
struct pg *dev,
char *
cmd,
int dlen,
unsigned long tmo)
338 printk(
"%s: command phase error\n", dev->
name);
345 printk(
"%s: Command sent, dlen=%d packet= ", dev->
name, dlen);
346 for (k = 0; k < 12; k++)
347 printk(
"%02x ", cmd[k] & 0xff);
356 static int pg_completion(
struct pg *dev,
char *
buf,
unsigned long tmo)
367 n = ((d + 3) & 0xfffc);
375 p ?
"Read" :
"Write", n);
376 dev->
dlen += (1 -
p) * d;
387 static int pg_reset(
struct pg *dev)
390 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
397 pg_sleep(20 *
HZ / 1000);
403 for (i = 0; i < 5; i++)
406 err =
memcmp(expect, got,
sizeof(got)) ? -1 : 0;
409 printk(
"%s: Reset (%d) signature = ", dev->
name, k);
410 for (i = 0; i < 5; i++)
421 static void xs(
char *buf,
char *targ,
int len)
426 for (k = 0; k < len; k++) {
428 if (c !=
' ' && c != l)
436 static int pg_identify(
struct pg *dev,
int log)
439 char *
ms[2] = {
"master",
"slave" };
441 char id_cmd[12] = {
ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
444 s = pg_command(dev, id_cmd, 36, jiffies +
PG_TMO);
447 s = pg_completion(dev, buf, jiffies +
PG_TMO);
453 xs(buf + 16,
id, 16);
464 static int pg_probe(
struct pg *dev)
466 if (dev->
drive == -1) {
469 return pg_identify(dev, 1);
472 return pg_identify(dev, 1);
477 static int pg_detect(
void)
479 struct pg *dev = &devices[0];
485 if (pg_drive_count == 0) {
486 if (
pi_init(dev->
pi, 1, -1, -1, -1, -1, -1, pg_scratch,
488 if (!pg_probe(dev)) {
496 for (unit = 0; unit <
PG_UNITS; unit++, dev++) {
503 if (!pg_probe(dev)) {
514 printk(
"%s: No ATAPI device detected\n",
name);
520 int unit = iminor(inode) & 0x7f;
521 struct pg *dev = &devices[
unit];
525 if ((unit >= PG_UNITS) || (!dev->
present)) {
540 pg_identify(dev, (
verbose > 1));
545 printk(
"%s: buffer allocation failed\n", dev->
name);
557 static int pg_release(
struct inode *inode,
struct file *file)
568 static ssize_t pg_write(
struct file *filp,
const char __user *buf,
size_t count, loff_t *ppos)
572 int hs =
sizeof (
hdr);
603 if (pg_command(dev,
hdr.packet,
hdr.dlen, jiffies +
PG_TMO)) {
616 static ssize_t pg_read(
struct file *filp,
char __user *buf,
size_t count, loff_t *ppos)
620 int hs =
sizeof (
hdr);
642 if (copy > (count - hs))
657 static int __init pg_init(
void)
674 err = register_chrdev(major,
name, &pg_fops);
676 printk(
"pg_init: unable to get major number %d\n", major);
677 for (unit = 0; unit <
PG_UNITS; unit++) {
678 struct pg *dev = &devices[
unit];
686 if (IS_ERR(pg_class)) {
687 err = PTR_ERR(pg_class);
690 for (unit = 0; unit <
PG_UNITS; unit++) {
691 struct pg *dev = &devices[
unit];
700 unregister_chrdev(major,
"pg");
705 static void __exit pg_exit(
void)
709 for (unit = 0; unit <
PG_UNITS; unit++) {
710 struct pg *dev = &devices[
unit];
715 unregister_chrdev(major,
name);
717 for (unit = 0; unit <
PG_UNITS; unit++) {
718 struct pg *dev = &devices[
unit];