2 pg.c (c) 1998 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU 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
122 #define PG_VERSION "1.01"
131 /* Here are things one can override from the insmod command.
132 Most are autoprobed by paride unless set here. Verbose is 0
137 static int verbose
= 0;
138 static int major
= PG_MAJOR
;
139 static char *name
= PG_NAME
;
140 static int disable
= 0;
142 static int drive0
[6] = {0,0,0,-1,-1,-1};
143 static int drive1
[6] = {0,0,0,-1,-1,-1};
144 static int drive2
[6] = {0,0,0,-1,-1,-1};
145 static int drive3
[6] = {0,0,0,-1,-1,-1};
147 static int (*drives
[4])[6] = {&drive0
,&drive1
,&drive2
,&drive3
};
148 static int pg_drive_count
;
157 #define DU (*drives[unit])
159 /* end of parameters */
162 #include <linux/module.h>
163 #include <linux/errno.h>
164 #include <linux/fs.h>
165 #include <linux/kernel.h>
166 #include <linux/delay.h>
167 #include <linux/malloc.h>
168 #include <linux/mtio.h>
169 #include <linux/pg.h>
171 #include <asm/uaccess.h>
177 static STT pg_stt
[5] = {{"drive0",6,drive0
},
181 {"disable",1,&disable
}};
183 void pg_setup( char *str
, int *ints
)
185 { generic_setup(pg_stt
,5,str
);
190 MODULE_PARM(verbose
,"i");
191 MODULE_PARM(major
,"i");
192 MODULE_PARM(name
,"s");
193 MODULE_PARM(drive0
,"1-6i");
194 MODULE_PARM(drive1
,"1-6i");
195 MODULE_PARM(drive2
,"1-6i");
196 MODULE_PARM(drive3
,"1-6i");
200 #define PG_SPIN_DEL 50 /* spin delay in micro-seconds */
203 #define PG_RESET_TMO 10*HZ
205 #define STAT_ERR 0x01
206 #define STAT_INDEX 0x02
207 #define STAT_ECC 0x04
208 #define STAT_DRQ 0x08
209 #define STAT_SEEK 0x10
210 #define STAT_WRERR 0x20
211 #define STAT_READY 0x40
212 #define STAT_BUSY 0x80
214 #define ATAPI_IDENTIFY 0x12
218 void cleanup_module( void );
221 static int pg_open(struct inode
*inode
, struct file
*file
);
222 static int pg_release (struct inode
*inode
, struct file
*file
);
223 static ssize_t
pg_read(struct file
* filp
, char * buf
,
224 size_t count
, loff_t
*ppos
);
225 static ssize_t
pg_write(struct file
* filp
, const char * buf
,
226 size_t count
, loff_t
*ppos
);
227 static int pg_detect(void);
229 static int pg_identify (int unit
, int log
);
234 struct pi_adapter pia
; /* interface to paride layer */
235 struct pi_adapter
*pi
;
236 int busy
; /* write done, read expected */
237 int start
; /* jiffies at command start */
238 int dlen
; /* transfer size requested */
239 int timeout
; /* timeout requested */
240 int status
; /* last sense key */
241 int drive
; /* drive */
242 int access
; /* count of active opens ... */
243 int present
; /* device present ? */
245 char name
[PG_NAMELEN
]; /* pg0, pg1, ... */
248 struct pg_unit pg
[PG_UNITS
];
250 /* 'unit' must be defined in all functions - either as a local or a param */
255 static char pg_scratch
[512]; /* scratch block buffer */
257 /* kernel glue structures */
259 static struct file_operations pg_fops
= {
260 NULL
, /* lseek - default */
262 pg_write
, /* write */
263 NULL
, /* readdir - bad */
269 pg_release
, /* release */
272 NULL
, /* media change ? */
273 NULL
/* revalidate new media */
276 void pg_init_units( void )
281 for (unit
=0;unit
<PG_UNITS
;unit
++) {
287 PG
.drive
= DU
[D_SLV
];
289 while ((j
< PG_NAMELEN
-2) && (PG
.name
[j
]=name
[j
])) j
++;
290 PG
.name
[j
++] = '0' + unit
;
292 if (DU
[D_PRT
]) pg_drive_count
++;
296 int pg_init (void) /* preliminary initialisation */
300 if (disable
) return -1;
304 if (pg_detect()) return -1;
306 if (register_chrdev(major
,name
,&pg_fops
)) {
307 printk("pg_init: unable to get major number %d\n",
309 for (unit
=0;unit
<PG_UNITS
;unit
++)
310 if (PG
.present
) pi_release(PI
);
319 /* Glue for modules ... */
321 void cleanup_module(void);
323 int init_module(void)
332 void cleanup_module(void)
336 unregister_chrdev(major
,name
);
338 for (unit
=0;unit
<PG_UNITS
;unit
++)
339 if (PG
.present
) pi_release(PI
);
344 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
345 #define RR(c,r) (pi_read_regr(PI,c,r))
347 #define DRIVE (0xa0+0x10*PG.drive)
349 static void pg_sleep( int cs
)
351 { current
->state
= TASK_INTERRUPTIBLE
;
352 current
->timeout
= jiffies
+ cs
;
356 static int pg_wait( int unit
, int go
, int stop
, int tmo
, char * msg
)
363 while ((((r
=RR(1,6))&go
)||(stop
&&(!(r
&stop
))))&&(jiffies
<tmo
)) {
364 if (j
++ < PG_SPIN
) udelay(PG_SPIN_DEL
);
368 if ((r
&(STAT_ERR
&stop
))||(jiffies
>=tmo
)) {
373 printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
374 PG
.name
,msg
,s
,e
,p
,(jiffies
>=tmo
)?" timeout":"");
377 if (jiffies
>=tmo
) e
|= 0x100;
378 PG
.status
= (e
>> 4) & 0xff;
384 static int pg_command( int unit
, char * cmd
, int dlen
, int tmo
)
392 if (pg_wait(unit
,STAT_BUSY
|STAT_DRQ
,0,tmo
,"before command")) {
399 WR(0,7,0xa0); /* ATAPI packet command */
401 if (pg_wait(unit
,STAT_BUSY
,STAT_DRQ
,tmo
,"command DRQ")) {
407 printk("%s: command phase error\n",PG
.name
);
412 pi_write_block(PI
,cmd
,12);
415 printk("%s: Command sent, dlen=%d packet= ", PG
.name
,dlen
);
416 for (k
=0;k
<12;k
++) printk("%02x ",cmd
[k
]&0xff);
422 static int pg_completion( int unit
, char * buf
, int tmo
)
426 r
= pg_wait(unit
,STAT_BUSY
,STAT_DRQ
|STAT_READY
|STAT_ERR
,
431 while (RR(0,7)&STAT_DRQ
) {
432 d
= (RR(0,4)+256*RR(0,5));
435 if (p
== 0) pi_write_block(PI
,buf
,n
);
436 if (p
== 2) pi_read_block(PI
,buf
,n
);
437 if (verbose
> 1) printk("%s: %s %d bytes\n",PG
.name
,
441 r
= pg_wait(unit
,STAT_BUSY
,STAT_DRQ
|STAT_READY
|STAT_ERR
,
450 static int pg_reset( int unit
)
453 int expect
[5] = {1,1,1,0x14,0xeb};
462 while ((k
++ < PG_RESET_TMO
) && (RR(1,6)&STAT_BUSY
))
466 for(i
=0;i
<5;i
++) flg
&= (RR(0,i
+1) == expect
[i
]);
469 printk("%s: Reset (%d) signature = ",PG
.name
,k
);
470 for (i
=0;i
<5;i
++) printk("%3x",RR(0,i
+1));
471 if (!flg
) printk(" (incorrect)");
479 static void xs( char *buf
, char *targ
, int offs
, int len
)
485 if((buf
[k
+offs
]!=0x20)||(buf
[k
+offs
]!=l
))
486 l
=targ
[j
++]=buf
[k
+offs
];
487 if (l
==0x20) j
--; targ
[j
]=0;
490 static int pg_identify( int unit
, int log
)
493 char *ms
[2] = {"master","slave"};
495 char id_cmd
[12] = { ATAPI_IDENTIFY
,0,0,0,36,0,0,0,0,0,0,0};
498 s
= pg_command(unit
,id_cmd
,36,jiffies
+PG_TMO
);
500 s
= pg_completion(unit
,buf
,jiffies
+PG_TMO
);
506 printk("%s: %s %s, %s\n",PG
.name
,mf
,id
,ms
[PG
.drive
]);
512 static int pg_probe( int unit
)
514 /* returns 0, with id set if drive is detected
515 -1, if drive detection failed
518 { if (PG
.drive
== -1) {
519 for (PG
.drive
=0;PG
.drive
<=1;PG
.drive
++)
520 if (!pg_reset(unit
)) return pg_identify(unit
,1);
522 if (!pg_reset(unit
)) return pg_identify(unit
,1);
527 static int pg_detect( void )
531 printk("%s: %s version %s, major %d\n",
532 name
,name
,PG_VERSION
,major
);
535 if (pg_drive_count
== 0) {
537 if (pi_init(PI
,1,-1,-1,-1,-1,-1,pg_scratch
,
538 PI_PG
,verbose
,PG
.name
)) {
539 if (!pg_probe(unit
)) {
542 } else pi_release(PI
);
545 } else for (unit
=0;unit
<PG_UNITS
;unit
++) if (DU
[D_PRT
])
546 if (pi_init(PI
,0,DU
[D_PRT
],DU
[D_MOD
],DU
[D_UNI
],
547 DU
[D_PRO
],DU
[D_DLY
],pg_scratch
,PI_PG
,verbose
,
549 if (!pg_probe(unit
)) {
552 } else pi_release(PI
);
557 printk("%s: No ATAPI device detected\n",name
);
561 #define DEVICE_NR(dev) (MINOR(dev) % 128)
563 static int pg_open (struct inode
*inode
, struct file
*file
)
565 { int unit
= DEVICE_NR(inode
->i_rdev
);
567 if ((unit
>= PG_UNITS
) || (!PG
.present
)) return -ENODEV
;
583 pg_identify(unit
,(verbose
>1));
586 PG
.bufptr
= kmalloc(PG_MAX_DATA
,GFP_KERNEL
);
587 if (PG
.bufptr
== NULL
) {
590 printk("%s: buffer allocation failed\n",PG
.name
);
597 static int pg_release (struct inode
*inode
, struct file
*file
)
599 int unit
= DEVICE_NR(inode
->i_rdev
);
601 if ((unit
>= PG_UNITS
) || (PG
.access
<= 0))
615 static ssize_t
pg_write(struct file
* filp
, const char * buf
,
616 size_t count
, loff_t
*ppos
)
618 { struct inode
*ino
= filp
->f_dentry
->d_inode
;
619 int unit
= DEVICE_NR(ino
->i_rdev
);
620 struct pg_write_hdr hdr
;
621 int hs
= sizeof(hdr
);
623 if (PG
.busy
) return -EBUSY
;
624 if (count
< hs
) return -EINVAL
;
626 copy_from_user((char *)&hdr
,buf
,hs
);
628 if (hdr
.magic
!= PG_MAGIC
) return -EINVAL
;
629 if (hdr
.dlen
> PG_MAX_DATA
) return -EINVAL
;
630 if ((count
- hs
) > PG_MAX_DATA
) return -EINVAL
;
632 if (hdr
.func
== PG_RESET
) {
633 if (count
!= hs
) return -EINVAL
;
634 if (pg_reset(unit
)) return -EIO
;
638 if (hdr
.func
!= PG_COMMAND
) return -EINVAL
;
641 PG
.timeout
= hdr
.timeout
*HZ
+ HZ
/2 + jiffies
;
643 if (pg_command(unit
,hdr
.packet
,hdr
.dlen
,jiffies
+PG_TMO
)) {
644 if (PG
.status
& 0x10) return -ETIME
;
650 copy_from_user(PG
.bufptr
,buf
+hs
,count
-hs
);
655 static ssize_t
pg_read(struct file
* filp
, char * buf
,
656 size_t count
, loff_t
*ppos
)
658 { struct inode
*ino
= filp
->f_dentry
->d_inode
;
659 int unit
= DEVICE_NR(ino
->i_rdev
);
660 struct pg_read_hdr hdr
;
661 int hs
= sizeof(hdr
);
664 if (!PG
.busy
) return -EINVAL
;
665 if (count
< hs
) return -EINVAL
;
669 if (pg_completion(unit
,PG
.bufptr
,PG
.timeout
))
670 if (PG
.status
& 0x10) return -ETIME
;
672 hdr
.magic
= PG_MAGIC
;
677 hdr
.dlen
= -1 * hdr
.dlen
;
679 if (copy
> (count
- hs
)) copy
= count
- hs
;
682 hdr
.duration
= (jiffies
- PG
.start
+ HZ
/2) / HZ
;
683 hdr
.scsi
= PG
.status
& 0x0f;
685 copy_to_user(buf
,(char *)&hdr
,hs
);
686 if (copy
> 0) copy_to_user(buf
+hs
,PG
.bufptr
,copy
);