Import 2.1.118
[davej-history.git] / drivers / block / paride / pg.c
blobfd001008dad750281eb38e60ceb31dc9c61014fd
1 /*
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
15 in progress.
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
24 12 bytes in length.
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:
37 /dev/pg0 b 97 0
38 /dev/pg1 b 97 1
39 /dev/pg2 b 97 2
40 /dev/pg3 b 97 3
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.)
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:
52 drive2
53 drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
55 Where,
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.
69 (0 if not given)
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)
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.
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
90 name as well.
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").
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:
107 pg.drive0
108 pg.drive1
109 pg.drive2
110 pg.drive3
112 In addition, you can use the parameter pg.disable to disable
113 the driver entirely.
117 /* Changes:
119 1.01 GRG 1998.06.16 Bug fixes
122 #define PG_VERSION "1.01"
123 #define PG_MAJOR 97
124 #define PG_NAME "pg"
125 #define PG_UNITS 4
127 #ifndef PI_PG
128 #define PI_PG 4
129 #endif
131 /* Here are things one can override from the insmod command.
132 Most are autoprobed by paride unless set here. Verbose is 0
133 by default.
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;
150 #define D_PRT 0
151 #define D_PRO 1
152 #define D_UNI 2
153 #define D_MOD 3
154 #define D_SLV 4
155 #define D_DLY 5
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>
173 #ifndef MODULE
175 #include "setup.h"
177 static STT pg_stt[5] = {{"drive0",6,drive0},
178 {"drive1",6,drive1},
179 {"drive2",6,drive2},
180 {"drive3",6,drive3},
181 {"disable",1,&disable}};
183 void pg_setup( char *str, int *ints)
185 { generic_setup(pg_stt,5,str);
188 #endif
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");
198 #include "paride.h"
200 #define PG_SPIN_DEL 50 /* spin delay in micro-seconds */
201 #define PG_SPIN 200
202 #define PG_TMO HZ
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
216 int pg_init(void);
217 #ifdef MODULE
218 void cleanup_module( void );
219 #endif
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);
231 #define PG_NAMELEN 8
233 struct pg_unit {
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 ? */
244 char *bufptr;
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 */
252 #define PG pg[unit]
253 #define PI PG.pi
255 static char pg_scratch[512]; /* scratch block buffer */
257 /* kernel glue structures */
259 static struct file_operations pg_fops = {
260 NULL, /* lseek - default */
261 pg_read, /* read */
262 pg_write, /* write */
263 NULL, /* readdir - bad */
264 NULL, /* select */
265 NULL, /* ioctl */
266 NULL, /* mmap */
267 pg_open, /* open */
268 NULL, /* flush */
269 pg_release, /* release */
270 NULL, /* fsync */
271 NULL, /* fasync */
272 NULL, /* media change ? */
273 NULL /* revalidate new media */
276 void pg_init_units( void )
278 { int unit, j;
280 pg_drive_count = 0;
281 for (unit=0;unit<PG_UNITS;unit++) {
282 PG.pi = & PG.pia;
283 PG.access = 0;
284 PG.busy = 0;
285 PG.present = 0;
286 PG.bufptr = NULL;
287 PG.drive = DU[D_SLV];
288 j = 0;
289 while ((j < PG_NAMELEN-2) && (PG.name[j]=name[j])) j++;
290 PG.name[j++] = '0' + unit;
291 PG.name[j] = 0;
292 if (DU[D_PRT]) pg_drive_count++;
296 int pg_init (void) /* preliminary initialisation */
298 { int unit;
300 if (disable) return -1;
302 pg_init_units();
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",
308 major);
309 for (unit=0;unit<PG_UNITS;unit++)
310 if (PG.present) pi_release(PI);
311 return -1;
314 return 0;
317 #ifdef MODULE
319 /* Glue for modules ... */
321 void cleanup_module(void);
323 int init_module(void)
325 { int err;
327 err = pg_init();
329 return err;
332 void cleanup_module(void)
334 { int unit;
336 unregister_chrdev(major,name);
338 for (unit=0;unit<PG_UNITS;unit++)
339 if (PG.present) pi_release(PI);
342 #endif
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;
353 schedule();
356 static int pg_wait( int unit, int go, int stop, int tmo, char * msg )
358 { int j, r, e, s, p;
360 PG.status = 0;
362 j = 0;
363 while ((((r=RR(1,6))&go)||(stop&&(!(r&stop))))&&(jiffies<tmo)) {
364 if (j++ < PG_SPIN) udelay(PG_SPIN_DEL);
365 else pg_sleep(1);
368 if ((r&(STAT_ERR&stop))||(jiffies>=tmo)) {
369 s = RR(0,7);
370 e = RR(0,1);
371 p = RR(0,2);
372 if (verbose > 1)
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;
379 return -1;
381 return 0;
384 static int pg_command( int unit, char * cmd, int dlen, int tmo )
386 { int k;
388 pi_connect(PI);
390 WR(0,6,DRIVE);
392 if (pg_wait(unit,STAT_BUSY|STAT_DRQ,0,tmo,"before command")) {
393 pi_disconnect(PI);
394 return -1;
397 WR(0,4,dlen % 256);
398 WR(0,5,dlen / 256);
399 WR(0,7,0xa0); /* ATAPI packet command */
401 if (pg_wait(unit,STAT_BUSY,STAT_DRQ,tmo,"command DRQ")) {
402 pi_disconnect(PI);
403 return -1;
406 if (RR(0,2) != 1) {
407 printk("%s: command phase error\n",PG.name);
408 pi_disconnect(PI);
409 return -1;
412 pi_write_block(PI,cmd,12);
414 if (verbose > 1) {
415 printk("%s: Command sent, dlen=%d packet= ", PG.name,dlen);
416 for (k=0;k<12;k++) printk("%02x ",cmd[k]&0xff);
417 printk("\n");
419 return 0;
422 static int pg_completion( int unit, char * buf, int tmo)
424 { int r, d, n, p;
426 r = pg_wait(unit,STAT_BUSY,STAT_DRQ|STAT_READY|STAT_ERR,
427 tmo,"completion");
429 PG.dlen = 0;
431 while (RR(0,7)&STAT_DRQ) {
432 d = (RR(0,4)+256*RR(0,5));
433 n = ((d+3)&0xfffc);
434 p = RR(0,2)&3;
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,
438 p?"Read":"Write",n);
439 PG.dlen += (1-p)*d;
440 buf += d;
441 r = pg_wait(unit,STAT_BUSY,STAT_DRQ|STAT_READY|STAT_ERR,
442 tmo,"completion");
445 pi_disconnect(PI);
447 return r;
450 static int pg_reset( int unit )
452 { int i, k, flg;
453 int expect[5] = {1,1,1,0x14,0xeb};
455 pi_connect(PI);
456 WR(0,6,DRIVE);
457 WR(0,7,8);
459 pg_sleep(2);
461 k = 0;
462 while ((k++ < PG_RESET_TMO) && (RR(1,6)&STAT_BUSY))
463 pg_sleep(1);
465 flg = 1;
466 for(i=0;i<5;i++) flg &= (RR(0,i+1) == expect[i]);
468 if (verbose) {
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)");
472 printk("\n");
475 pi_disconnect(PI);
476 return flg-1;
479 static void xs( char *buf, char *targ, int offs, int len )
481 { int j,k,l;
483 j=0; l=0;
484 for (k=0;k<len;k++)
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 )
492 { int s;
493 char *ms[2] = {"master","slave"};
494 char mf[10], id[18];
495 char id_cmd[12] = { ATAPI_IDENTIFY,0,0,0,36,0,0,0,0,0,0,0};
496 char buf[36];
498 s = pg_command(unit,id_cmd,36,jiffies+PG_TMO);
499 if (s) return -1;
500 s = pg_completion(unit,buf,jiffies+PG_TMO);
501 if (s) return -1;
503 if (log) {
504 xs(buf,mf,8,8);
505 xs(buf,id,16,16);
506 printk("%s: %s %s, %s\n",PG.name,mf,id,ms[PG.drive]);
509 return 0;
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);
521 } else {
522 if (!pg_reset(unit)) return pg_identify(unit,1);
524 return -1;
527 static int pg_detect( void )
529 { int k, unit;
531 printk("%s: %s version %s, major %d\n",
532 name,name,PG_VERSION,major);
534 k = 0;
535 if (pg_drive_count == 0) {
536 unit = 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)) {
540 PG.present = 1;
541 k++;
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,
548 PG.name)) {
549 if (!pg_probe(unit)) {
550 PG.present = 1;
551 k++;
552 } else pi_release(PI);
555 if (k) return 0;
557 printk("%s: No ATAPI device detected\n",name);
558 return -1;
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;
569 PG.access++;
571 if (PG.access > 1) {
572 PG.access--;
573 return -EBUSY;
576 MOD_INC_USE_COUNT;
578 if (PG.busy) {
579 pg_reset(unit);
580 PG.busy = 0;
583 pg_identify(unit,(verbose>1));
586 PG.bufptr = kmalloc(PG_MAX_DATA,GFP_KERNEL);
587 if (PG.bufptr == NULL) {
588 PG.access--;
589 MOD_DEC_USE_COUNT;
590 printk("%s: buffer allocation failed\n",PG.name);
591 return -ENOMEM;
594 return 0;
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))
602 return -EINVAL;
604 PG.access--;
606 kfree(PG.bufptr);
607 PG.bufptr = NULL;
609 MOD_DEC_USE_COUNT;
611 return 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;
635 return count;
638 if (hdr.func != PG_COMMAND) return -EINVAL;
640 PG.start = jiffies;
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;
645 return -EIO;
648 PG.busy = 1;
650 copy_from_user(PG.bufptr,buf+hs,count-hs);
652 return count;
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);
662 int copy;
664 if (!PG.busy) return -EINVAL;
665 if (count < hs) return -EINVAL;
667 PG.busy = 0;
669 if (pg_completion(unit,PG.bufptr,PG.timeout))
670 if (PG.status & 0x10) return -ETIME;
672 hdr.magic = PG_MAGIC;
673 hdr.dlen = PG.dlen;
674 copy = 0;
676 if (hdr.dlen < 0) {
677 hdr.dlen = -1 * hdr.dlen;
678 copy = 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);
688 return copy+hs;
691 /* end of pg.c */