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 /* Here are things one can override from the insmod command.
134 Most are autoprobed by paride unless set here. Verbose is 0
139 static int verbose = 0;
140 static int major = PG_MAJOR;
141 static char *name = PG_NAME;
142 static int disable = 0;
144 static int drive0[6] = {0,0,0,-1,-1,-1};
145 static int drive1[6] = {0,0,0,-1,-1,-1};
146 static int drive2[6] = {0,0,0,-1,-1,-1};
147 static int drive3[6] = {0,0,0,-1,-1,-1};
149 static int (*drives[4])[6] = {&drive0,&drive1,&drive2,&drive3};
150 static int pg_drive_count;
159 #define DU (*drives[unit])
161 /* end of parameters */
164 #include <linux/module.h>
165 #include <linux/errno.h>
166 #include <linux/fs.h>
167 #include <linux/devfs_fs_kernel.h>
168 #include <linux/kernel.h>
169 #include <linux/delay.h>
170 #include <linux/slab.h>
171 #include <linux/mtio.h>
172 #include <linux/pg.h>
173 #include <linux/wait.h>
174 #include <linux/smp_lock.h>
176 #include <asm/uaccess.h>
182 static STT pg_stt[5] = {{"drive0",6,drive0},
186 {"disable",1,&disable}};
188 void pg_setup( char *str, int *ints)
190 { generic_setup(pg_stt,5,str);
195 MODULE_PARM(verbose,"i");
196 MODULE_PARM(major,"i");
197 MODULE_PARM(name,"s");
198 MODULE_PARM(drive0,"1-6i");
199 MODULE_PARM(drive1,"1-6i");
200 MODULE_PARM(drive2,"1-6i");
201 MODULE_PARM(drive3,"1-6i");
205 #define PG_SPIN_DEL 50 /* spin delay in micro-seconds */
208 #define PG_RESET_TMO 10*HZ
210 #define STAT_ERR 0x01
211 #define STAT_INDEX 0x02
212 #define STAT_ECC 0x04
213 #define STAT_DRQ 0x08
214 #define STAT_SEEK 0x10
215 #define STAT_WRERR 0x20
216 #define STAT_READY 0x40
217 #define STAT_BUSY 0x80
219 #define ATAPI_IDENTIFY 0x12
223 void cleanup_module( void );
226 static int pg_open(struct inode *inode, struct file *file);
227 static int pg_release (struct inode *inode, struct file *file);
228 static ssize_t pg_read(struct file * filp, char * buf,
229 size_t count, loff_t *ppos);
230 static ssize_t pg_write(struct file * filp, const char * buf,
231 size_t count, loff_t *ppos);
232 static int pg_detect(void);
234 static int pg_identify (int unit, int log);
239 struct pi_adapter pia; /* interface to paride layer */
240 struct pi_adapter *pi;
241 int busy; /* write done, read expected */
242 int start; /* jiffies at command start */
243 int dlen; /* transfer size requested */
244 int timeout; /* timeout requested */
245 int status; /* last sense key */
246 int drive; /* drive */
247 int access; /* count of active opens ... */
248 int present; /* device present ? */
250 char name[PG_NAMELEN]; /* pg0, pg1, ... */
253 struct pg_unit pg[PG_UNITS];
255 /* 'unit' must be defined in all functions - either as a local or a param */
260 static char pg_scratch[512]; /* scratch block buffer */
262 /* kernel glue structures */
264 static struct file_operations pg_fops = {
272 void pg_init_units( void )
277 for (unit=0;unit<PG_UNITS;unit++) {
283 PG.drive = DU[D_SLV];
285 while ((j < PG_NAMELEN-2) && (PG.name[j]=name[j])) j++;
286 PG.name[j++] = '0' + unit;
288 if (DU[D_PRT]) pg_drive_count++;
292 static devfs_handle_t devfs_handle;
294 int pg_init (void) /* preliminary initialisation */
298 if (disable) return -1;
302 if (pg_detect()) return -1;
304 if (devfs_register_chrdev(major,name,&pg_fops)) {
305 printk("pg_init: unable to get major number %d\n",
307 for (unit=0;unit<PG_UNITS;unit++)
308 if (PG.present) pi_release(PI);
311 devfs_handle = devfs_mk_dir (NULL, "pg", NULL);
312 devfs_register_series (devfs_handle, "%u", 4, DEVFS_FL_DEFAULT,
313 major, 0, S_IFCHR | S_IRUSR | S_IWUSR,
320 /* Glue for modules ... */
322 void cleanup_module(void);
324 int init_module(void)
329 { extern paride_init();
339 void cleanup_module(void)
343 devfs_unregister (devfs_handle);
344 devfs_unregister_chrdev(major,name);
346 for (unit=0;unit<PG_UNITS;unit++)
347 if (PG.present) pi_release(PI);
352 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
353 #define RR(c,r) (pi_read_regr(PI,c,r))
355 #define DRIVE (0xa0+0x10*PG.drive)
357 static void pg_sleep( int cs )
359 { current->state = TASK_INTERRUPTIBLE;
360 schedule_timeout(cs);
363 static int pg_wait( int unit, int go, int stop, int tmo, char * msg )
370 while ((((r=RR(1,6))&go)||(stop&&(!(r&stop))))&&(time_before(jiffies,tmo))) {
371 if (j++ < PG_SPIN) udelay(PG_SPIN_DEL);
375 if ((r&(STAT_ERR&stop))||time_after_eq(jiffies, tmo)) {
380 printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
381 PG.name,msg,s,e,p,time_after_eq(jiffies, tmo)?" timeout":"");
384 if (time_after_eq(jiffies, tmo)) e |= 0x100;
385 PG.status = (e >> 4) & 0xff;
391 static int pg_command( int unit, char * cmd, int dlen, int tmo )
399 if (pg_wait(unit,STAT_BUSY|STAT_DRQ,0,tmo,"before command")) {
406 WR(0,7,0xa0); /* ATAPI packet command */
408 if (pg_wait(unit,STAT_BUSY,STAT_DRQ,tmo,"command DRQ")) {
414 printk("%s: command phase error\n",PG.name);
419 pi_write_block(PI,cmd,12);
422 printk("%s: Command sent, dlen=%d packet= ", PG.name,dlen);
423 for (k=0;k<12;k++) printk("%02x ",cmd[k]&0xff);
429 static int pg_completion( int unit, char * buf, int tmo)
433 r = pg_wait(unit,STAT_BUSY,STAT_DRQ|STAT_READY|STAT_ERR,
438 while (RR(0,7)&STAT_DRQ) {
439 d = (RR(0,4)+256*RR(0,5));
442 if (p == 0) pi_write_block(PI,buf,n);
443 if (p == 2) pi_read_block(PI,buf,n);
444 if (verbose > 1) printk("%s: %s %d bytes\n",PG.name,
448 r = pg_wait(unit,STAT_BUSY,STAT_DRQ|STAT_READY|STAT_ERR,
457 static int pg_reset( int unit )
460 int expect[5] = {1,1,1,0x14,0xeb};
466 pg_sleep(20*HZ/1000);
469 while ((k++ < PG_RESET_TMO) && (RR(1,6)&STAT_BUSY))
473 for(i=0;i<5;i++) flg &= (RR(0,i+1) == expect[i]);
476 printk("%s: Reset (%d) signature = ",PG.name,k);
477 for (i=0;i<5;i++) printk("%3x",RR(0,i+1));
478 if (!flg) printk(" (incorrect)");
486 static void xs( char *buf, char *targ, int offs, int len )
492 if((buf[k+offs]!=0x20)||(buf[k+offs]!=l))
493 l=targ[j++]=buf[k+offs];
498 static int pg_identify( int unit, int log )
501 char *ms[2] = {"master","slave"};
503 char id_cmd[12] = { ATAPI_IDENTIFY,0,0,0,36,0,0,0,0,0,0,0};
506 s = pg_command(unit,id_cmd,36,jiffies+PG_TMO);
508 s = pg_completion(unit,buf,jiffies+PG_TMO);
514 printk("%s: %s %s, %s\n",PG.name,mf,id,ms[PG.drive]);
520 static int pg_probe( int unit )
522 /* returns 0, with id set if drive is detected
523 -1, if drive detection failed
526 { if (PG.drive == -1) {
527 for (PG.drive=0;PG.drive<=1;PG.drive++)
528 if (!pg_reset(unit)) return pg_identify(unit,1);
530 if (!pg_reset(unit)) return pg_identify(unit,1);
535 static int pg_detect( void )
539 printk("%s: %s version %s, major %d\n",
540 name,name,PG_VERSION,major);
543 if (pg_drive_count == 0) {
545 if (pi_init(PI,1,-1,-1,-1,-1,-1,pg_scratch,
546 PI_PG,verbose,PG.name)) {
547 if (!pg_probe(unit)) {
550 } else pi_release(PI);
553 } else for (unit=0;unit<PG_UNITS;unit++) if (DU[D_PRT])
554 if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
555 DU[D_PRO],DU[D_DLY],pg_scratch,PI_PG,verbose,
557 if (!pg_probe(unit)) {
560 } else pi_release(PI);
565 printk("%s: No ATAPI device detected\n",name);
569 #define DEVICE_NR(dev) (MINOR(dev) % 128)
571 static int pg_open (struct inode *inode, struct file *file)
573 { int unit = DEVICE_NR(inode->i_rdev);
575 if ((unit >= PG_UNITS) || (!PG.present)) return -ENODEV;
589 pg_identify(unit,(verbose>1));
592 PG.bufptr = kmalloc(PG_MAX_DATA,GFP_KERNEL);
593 if (PG.bufptr == NULL) {
595 printk("%s: buffer allocation failed\n",PG.name);
602 static int pg_release (struct inode *inode, struct file *file)
604 int unit = DEVICE_NR(inode->i_rdev);
606 if ((unit >= PG_UNITS) || (PG.access <= 0))
620 static ssize_t pg_write(struct file * filp, const char * buf,
621 size_t count, loff_t *ppos)
623 { struct inode *ino = filp->f_dentry->d_inode;
624 int unit = DEVICE_NR(ino->i_rdev);
625 struct pg_write_hdr hdr;
626 int hs = sizeof(hdr);
628 if (PG.busy) return -EBUSY;
629 if (count < hs) return -EINVAL;
631 if (copy_from_user((char *)&hdr, buf, hs))
634 if (hdr.magic != PG_MAGIC) return -EINVAL;
635 if (hdr.dlen > PG_MAX_DATA) return -EINVAL;
636 if ((count - hs) > PG_MAX_DATA) return -EINVAL;
638 if (hdr.func == PG_RESET) {
639 if (count != hs) return -EINVAL;
640 if (pg_reset(unit)) return -EIO;
644 if (hdr.func != PG_COMMAND) return -EINVAL;
647 PG.timeout = hdr.timeout*HZ + HZ/2 + jiffies;
649 if (pg_command(unit,hdr.packet,hdr.dlen,jiffies+PG_TMO)) {
650 if (PG.status & 0x10) return -ETIME;
656 if (copy_from_user(PG.bufptr, buf + hs, count - hs))
661 static ssize_t pg_read(struct file * filp, char * buf,
662 size_t count, loff_t *ppos)
664 { struct inode *ino = filp->f_dentry->d_inode;
665 int unit = DEVICE_NR(ino->i_rdev);
666 struct pg_read_hdr hdr;
667 int hs = sizeof(hdr);
670 if (!PG.busy) return -EINVAL;
671 if (count < hs) return -EINVAL;
675 if (pg_completion(unit,PG.bufptr,PG.timeout))
676 if (PG.status & 0x10) return -ETIME;
678 hdr.magic = PG_MAGIC;
683 hdr.dlen = -1 * hdr.dlen;
685 if (copy > (count - hs)) copy = count - hs;
688 hdr.duration = (jiffies - PG.start + HZ/2) / HZ;
689 hdr.scsi = PG.status & 0x0f;
691 if (copy_to_user(buf, (char *)&hdr, hs))
694 if (copy_to_user(buf+hs,PG.bufptr,copy))
701 MODULE_LICENSE("GPL");