Import 2.1.118
[davej-history.git] / drivers / block / paride / pf.c
bloba2f1f1da9e306d151c6245346b39df154c846dff
1 /*
2 pf.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU public license.
5 This is the high-level driver for parallel port ATAPI disk
6 drives based on chips supported by the paride module.
8 By default, the driver will autoprobe for a single parallel
9 port ATAPI disk drive, but if their individual parameters are
10 specified, the driver can handle up to 4 drives.
12 The behaviour of the pf driver can be altered by setting
13 some parameters from the insmod command line. The following
14 parameters are adjustable:
16 drive0 These four arguments can be arrays of
17 drive1 1-7 integers as follows:
18 drive2
19 drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<lun>,<dly>
21 Where,
23 <prt> is the base of the parallel port address for
24 the corresponding drive. (required)
26 <pro> is the protocol number for the adapter that
27 supports this drive. These numbers are
28 logged by 'paride' when the protocol modules
29 are initialised. (0 if not given)
31 <uni> for those adapters that support chained
32 devices, this is the unit selector for the
33 chain of devices on the given port. It should
34 be zero for devices that don't support chaining.
35 (0 if not given)
37 <mod> this can be -1 to choose the best mode, or one
38 of the mode numbers supported by the adapter.
39 (-1 if not given)
41 <slv> ATAPI CDroms can be jumpered to master or slave.
42 Set this to 0 to choose the master drive, 1 to
43 choose the slave, -1 (the default) to choose the
44 first drive found.
46 <lun> Some ATAPI devices support multiple LUNs.
47 One example is the ATAPI PD/CD drive from
48 Matshita/Panasonic. This device has a
49 CD drive on LUN 0 and a PD drive on LUN 1.
50 By default, the driver will search for the
51 first LUN with a supported device. Set
52 this parameter to force it to use a specific
53 LUN. (default -1)
55 <dly> some parallel ports require the driver to
56 go more slowly. -1 sets a default value that
57 should work with the chosen protocol. Otherwise,
58 set this to a small integer, the larger it is
59 the slower the port i/o. In some cases, setting
60 this to zero will speed up the device. (default -1)
62 major You may use this parameter to overide the
63 default major number (47) that this driver
64 will use. Be sure to change the device
65 name as well.
67 name This parameter is a character string that
68 contains the name the kernel will use for this
69 device (in /proc output, for instance).
70 (default "pf").
72 cluster The driver will attempt to aggregate requests
73 for adjacent blocks into larger multi-block
74 clusters. The maximum cluster size (in 512
75 byte sectors) is set with this parameter.
76 (default 64)
78 verbose This parameter controls the amount of logging
79 that the driver will do. Set it to 0 for
80 normal operation, 1 to see autoprobe progress
81 messages, or 2 to see additional debugging
82 output. (default 0)
84 nice This parameter controls the driver's use of
85 idle CPU time, at the expense of some speed.
87 If this driver is built into the kernel, you can use the
88 following command line parameters, with the same values
89 as the corresponding module parameters listed above:
91 pf.drive0
92 pf.drive1
93 pf.drive2
94 pf.drive3
95 pf.cluster
96 pf.nice
98 In addition, you can use the parameter pf.disable to disable
99 the driver entirely.
103 /* Changes:
105 1.01 GRG 1998.05.03 Changes for SMP. Eliminate sti().
106 Fix for drives that don't clear STAT_ERR
107 until after next CDB delivered.
108 Small change in pf_completion to round
109 up transfer size.
110 1.02 GRG 1998.06.16 Eliminated an Ugh
111 1.03 GRG 1998.08.16 Use HZ in loop timings, extra debugging
115 #define PF_VERSION "1.03"
116 #define PF_MAJOR 47
117 #define PF_NAME "pf"
118 #define PF_UNITS 4
120 /* Here are things one can override from the insmod command.
121 Most are autoprobed by paride unless set here. Verbose is off
122 by default.
126 static int verbose = 0;
127 static int major = PF_MAJOR;
128 static char *name = PF_NAME;
129 static int cluster = 64;
130 static int nice = 0;
131 static int disable = 0;
133 static int drive0[7] = {0,0,0,-1,-1,-1,-1};
134 static int drive1[7] = {0,0,0,-1,-1,-1,-1};
135 static int drive2[7] = {0,0,0,-1,-1,-1,-1};
136 static int drive3[7] = {0,0,0,-1,-1,-1,-1};
138 static int (*drives[4])[7] = {&drive0,&drive1,&drive2,&drive3};
139 static int pf_drive_count;
141 #define D_PRT 0
142 #define D_PRO 1
143 #define D_UNI 2
144 #define D_MOD 3
145 #define D_SLV 4
146 #define D_LUN 5
147 #define D_DLY 6
149 #define DU (*drives[unit])
151 /* end of parameters */
154 #include <linux/module.h>
155 #include <linux/errno.h>
156 #include <linux/fs.h>
157 #include <linux/kernel.h>
158 #include <linux/delay.h>
159 #include <linux/genhd.h>
160 #include <linux/hdreg.h>
161 #include <linux/cdrom.h>
162 #include <asm/spinlock.h>
164 #include <asm/uaccess.h>
166 #ifndef MODULE
168 #include "setup.h"
170 static STT pf_stt[7] = {{"drive0",7,drive0},
171 {"drive1",7,drive1},
172 {"drive2",7,drive2},
173 {"drive3",7,drive3},
174 {"disable",1,&disable},
175 {"cluster",1,&cluster},
176 {"nice",1,&nice}};
178 void pf_setup( char *str, int *ints)
180 { generic_setup(pf_stt,7,str);
183 #endif
185 MODULE_PARM(verbose,"i");
186 MODULE_PARM(major,"i");
187 MODULE_PARM(name,"s");
188 MODULE_PARM(cluster,"i");
189 MODULE_PARM(nice,"i");
190 MODULE_PARM(drive0,"1-7i");
191 MODULE_PARM(drive1,"1-7i");
192 MODULE_PARM(drive2,"1-7i");
193 MODULE_PARM(drive3,"1-7i");
195 #include "paride.h"
197 /* set up defines for blk.h, why don't all drivers do it this way ? */
199 #define MAJOR_NR major
200 #define DEVICE_NAME "PF"
201 #define DEVICE_REQUEST do_pf_request
202 #define DEVICE_NR(device) MINOR(device)
203 #define DEVICE_ON(device)
204 #define DEVICE_OFF(device)
206 #include <linux/blk.h>
208 #include "pseudo.h"
210 /* constants for faking geometry numbers */
212 #define PF_FD_MAX 8192 /* use FD geometry under this size */
213 #define PF_FD_HDS 2
214 #define PF_FD_SPT 18
215 #define PF_HD_HDS 64
216 #define PF_HD_SPT 32
218 #define PF_MAX_RETRIES 5
219 #define PF_TMO 800 /* interrupt timeout in jiffies */
220 #define PF_SPIN_DEL 50 /* spin delay in micro-seconds */
222 #define PF_SPIN (1000000*PF_TMO)/(HZ*PF_SPIN_DEL)
224 #define STAT_ERR 0x00001
225 #define STAT_INDEX 0x00002
226 #define STAT_ECC 0x00004
227 #define STAT_DRQ 0x00008
228 #define STAT_SEEK 0x00010
229 #define STAT_WRERR 0x00020
230 #define STAT_READY 0x00040
231 #define STAT_BUSY 0x00080
233 #define ATAPI_REQ_SENSE 0x03
234 #define ATAPI_LOCK 0x1e
235 #define ATAPI_DOOR 0x1b
236 #define ATAPI_MODE_SENSE 0x5a
237 #define ATAPI_CAPACITY 0x25
238 #define ATAPI_IDENTIFY 0x12
239 #define ATAPI_READ_10 0x28
240 #define ATAPI_WRITE_10 0x2a
242 int pf_init(void);
243 #ifdef MODULE
244 void cleanup_module( void );
245 #endif
246 static int pf_open(struct inode *inode, struct file *file);
247 static void do_pf_request(void);
248 static int pf_ioctl(struct inode *inode,struct file *file,
249 unsigned int cmd, unsigned long arg);
251 static int pf_release (struct inode *inode, struct file *file);
253 static int pf_detect(void);
254 static void do_pf_read(void);
255 static void do_pf_read_start(void);
256 static void do_pf_write(void);
257 static void do_pf_write_start(void);
258 static void do_pf_read_drq( void );
259 static void do_pf_write_done( void );
261 static int pf_identify (int unit);
262 static void pf_lock(int unit, int func);
263 static void pf_eject(int unit);
264 static int pf_check_media(kdev_t dev);
266 static int pf_blocksizes[PF_UNITS];
268 #define PF_NM 0
269 #define PF_RO 1
270 #define PF_RW 2
272 #define PF_NAMELEN 8
274 struct pf_unit {
275 struct pi_adapter pia; /* interface to paride layer */
276 struct pi_adapter *pi;
277 int removable; /* removable media device ? */
278 int media_status; /* media present ? WP ? */
279 int drive; /* drive */
280 int lun;
281 int access; /* count of active opens ... */
282 int capacity; /* Size of this volume in sectors */
283 int present; /* device present ? */
284 char name[PF_NAMELEN]; /* pf0, pf1, ... */
287 struct pf_unit pf[PF_UNITS];
289 /* 'unit' must be defined in all functions - either as a local or a param */
291 #define PF pf[unit]
292 #define PI PF.pi
294 static char pf_scratch[512]; /* scratch block buffer */
296 /* the variables below are used mainly in the I/O request engine, which
297 processes only one request at a time.
300 static int pf_retries = 0; /* i/o error retry count */
301 static int pf_busy = 0; /* request being processed ? */
302 static int pf_block; /* address of next requested block */
303 static int pf_count; /* number of blocks still to do */
304 static int pf_run; /* sectors in current cluster */
305 static int pf_cmd; /* current command READ/WRITE */
306 static int pf_unit; /* unit of current request */
307 static int pf_mask; /* stopper for pseudo-int */
308 static char * pf_buf; /* buffer for request in progress */
310 /* kernel glue structures */
312 static struct file_operations pf_fops = {
313 NULL, /* lseek - default */
314 block_read, /* read - general block-dev read */
315 block_write, /* write - general block-dev write */
316 NULL, /* readdir - bad */
317 NULL, /* select */
318 pf_ioctl, /* ioctl */
319 NULL, /* mmap */
320 pf_open, /* open */
321 NULL, /* flush */
322 pf_release, /* release */
323 block_fsync, /* fsync */
324 NULL, /* fasync */
325 pf_check_media, /* media change ? */
326 NULL /* revalidate new media */
329 void pf_init_units( void )
331 { int unit, j;
333 pf_drive_count = 0;
334 for (unit=0;unit<PF_UNITS;unit++) {
335 PF.pi = & PF.pia;
336 PF.access = 0;
337 PF.media_status = PF_NM;
338 PF.capacity = 0;
339 PF.present = 0;
340 PF.drive = DU[D_SLV];
341 PF.lun = DU[D_LUN];
342 j = 0;
343 while ((j < PF_NAMELEN-2) && (PF.name[j]=name[j])) j++;
344 PF.name[j++] = '0' + unit;
345 PF.name[j] = 0;
346 if (DU[D_PRT]) pf_drive_count++;
350 int pf_init (void) /* preliminary initialisation */
352 { int i;
354 if (disable) return -1;
356 pf_init_units();
358 if (pf_detect()) return -1;
359 pf_busy = 0;
361 if (register_blkdev(MAJOR_NR,name,&pf_fops)) {
362 printk("pf_init: unable to get major number %d\n",
363 major);
364 return -1;
366 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
367 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read ahead */
369 for (i=0;i<PF_UNITS;i++) pf_blocksizes[i] = 1024;
370 blksize_size[MAJOR_NR] = pf_blocksizes;
372 return 0;
375 static int pf_open (struct inode *inode, struct file *file)
377 { int unit = DEVICE_NR(inode->i_rdev);
379 if ((unit >= PF_UNITS) || (!PF.present)) return -ENODEV;
381 MOD_INC_USE_COUNT;
383 pf_identify(unit);
385 if (PF.media_status == PF_NM) {
386 MOD_DEC_USE_COUNT;
387 return -ENODEV;
390 if ((PF.media_status == PF_RO) && (file ->f_mode & 2)) {
391 MOD_DEC_USE_COUNT;
392 return -EROFS;
395 PF.access++;
396 if (PF.removable) pf_lock(unit,1);
398 return 0;
401 static int pf_ioctl(struct inode *inode,struct file *file,
402 unsigned int cmd, unsigned long arg)
404 { int err, unit;
405 struct hd_geometry *geo = (struct hd_geometry *) arg;
407 if ((!inode) || (!inode->i_rdev)) return -EINVAL;
408 unit = DEVICE_NR(inode->i_rdev);
409 if (unit >= PF_UNITS) return -EINVAL;
410 if (!PF.present) return -ENODEV;
412 switch (cmd) {
413 case CDROMEJECT:
414 if (PF.access == 1) {
415 pf_eject(unit);
416 return 0;
418 case HDIO_GETGEO:
419 if (!geo) return -EINVAL;
420 err = verify_area(VERIFY_WRITE,geo,sizeof(*geo));
421 if (err) return err;
422 if (PF.capacity < PF_FD_MAX) {
423 put_user(PF.capacity/(PF_FD_HDS*PF_FD_SPT),
424 (short *) &geo->cylinders);
425 put_user(PF_FD_HDS, (char *) &geo->heads);
426 put_user(PF_FD_SPT, (char *) &geo->sectors);
427 } else {
428 put_user(PF.capacity/(PF_HD_HDS*PF_HD_SPT),
429 (short *) &geo->cylinders);
430 put_user(PF_HD_HDS, (char *) &geo->heads);
431 put_user(PF_HD_SPT, (char *) &geo->sectors);
433 put_user(0,(long *)&geo->start);
434 return 0;
435 case BLKRASET:
436 if(!capable(CAP_SYS_ADMIN)) return -EACCES;
437 if(!(inode->i_rdev)) return -EINVAL;
438 if(arg > 0xff) return -EINVAL;
439 read_ahead[MAJOR(inode->i_rdev)] = arg;
440 return 0;
441 case BLKRAGET:
442 if (!arg) return -EINVAL;
443 err = verify_area(VERIFY_WRITE,(long *) arg,sizeof(long));
444 if (err) return (err);
445 put_user(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
446 return (0);
447 case BLKGETSIZE:
448 if (!arg) return -EINVAL;
449 err = verify_area(VERIFY_WRITE,(long *) arg,sizeof(long));
450 if (err) return (err);
451 put_user(PF.capacity,(long *) arg);
452 return (0);
453 case BLKFLSBUF:
454 if(!capable(CAP_SYS_ADMIN)) return -EACCES;
455 if(!(inode->i_rdev)) return -EINVAL;
456 fsync_dev(inode->i_rdev);
457 invalidate_buffers(inode->i_rdev);
458 return 0;
459 RO_IOCTLS(inode->i_rdev,arg);
460 default:
461 return -EINVAL;
466 static int pf_release (struct inode *inode, struct file *file)
468 { kdev_t devp;
469 int unit;
471 struct super_block *sb;
473 devp = inode->i_rdev;
474 unit = DEVICE_NR(devp);
476 if ((unit >= PF_UNITS) || (PF.access <= 0))
477 return -EINVAL;
479 PF.access--;
481 if (!PF.access) {
482 fsync_dev(devp);
484 sb = get_super(devp);
485 if (sb) invalidate_inodes(sb);
487 invalidate_buffers(devp);
488 if (PF.removable) pf_lock(unit,0);
491 MOD_DEC_USE_COUNT;
493 return 0;
497 static int pf_check_media( kdev_t dev)
499 { return 1;
502 #ifdef MODULE
504 /* Glue for modules ... */
506 void cleanup_module(void);
508 int init_module(void)
510 { int err;
512 err = pf_init();
514 return err;
517 void cleanup_module(void)
519 { int unit;
521 unregister_blkdev(MAJOR_NR,name);
523 for (unit=0;unit<PF_UNITS;unit++)
524 if (PF.present) pi_release(PI);
527 #endif
529 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
530 #define RR(c,r) (pi_read_regr(PI,c,r))
532 #define LUN (0x20*PF.lun)
533 #define DRIVE (0xa0+0x10*PF.drive)
535 static int pf_wait( int unit, int go, int stop, char * fun, char * msg )
537 { int j, r, e, s, p;
539 j = 0;
540 while ((((r=RR(1,6))&go)||(stop&&(!(r&stop))))&&(j++<PF_SPIN))
541 udelay(PF_SPIN_DEL);
543 if ((r&(STAT_ERR&stop))||(j>=PF_SPIN)) {
544 s = RR(0,7);
545 e = RR(0,1);
546 p = RR(0,2);
547 if (j >= PF_SPIN) e |= 0x100;
548 if (fun) printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
549 " loop=%d phase=%d\n",
550 PF.name,fun,msg,r,s,e,j,p);
551 return (e<<8)+s;
553 return 0;
556 static int pf_command( int unit, char * cmd, int dlen, char * fun )
558 { pi_connect(PI);
560 WR(0,6,DRIVE);
562 if (pf_wait(unit,STAT_BUSY|STAT_DRQ,0,fun,"before command")) {
563 pi_disconnect(PI);
564 return -1;
567 WR(0,4,dlen % 256);
568 WR(0,5,dlen / 256);
569 WR(0,7,0xa0); /* ATAPI packet command */
571 if (pf_wait(unit,STAT_BUSY,STAT_DRQ,fun,"command DRQ")) {
572 pi_disconnect(PI);
573 return -1;
576 if (RR(0,2) != 1) {
577 printk("%s: %s: command phase error\n",PF.name,fun);
578 pi_disconnect(PI);
579 return -1;
582 pi_write_block(PI,cmd,12);
584 return 0;
587 static int pf_completion( int unit, char * buf, char * fun )
589 { int r, s, n;
591 r = pf_wait(unit,STAT_BUSY,STAT_DRQ|STAT_READY|STAT_ERR,
592 fun,"completion");
594 if ((RR(0,2)&2) && (RR(0,7)&STAT_DRQ)) {
595 n = (((RR(0,4)+256*RR(0,5))+3)&0xfffc);
596 pi_read_block(PI,buf,n);
599 s = pf_wait(unit,STAT_BUSY,STAT_READY|STAT_ERR,fun,"data done");
601 pi_disconnect(PI);
603 return (r?r:s);
606 static void pf_req_sense( int unit, int quiet )
608 { char rs_cmd[12] = { ATAPI_REQ_SENSE,LUN,0,0,16,0,0,0,0,0,0,0 };
609 char buf[16];
610 int r;
612 r = pf_command(unit,rs_cmd,16,"Request sense");
613 mdelay(1);
614 if (!r) pf_completion(unit,buf,"Request sense");
616 if ((!r)&&(!quiet))
617 printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
618 PF.name,buf[2]&0xf,buf[12],buf[13]);
621 static int pf_atapi( int unit, char * cmd, int dlen, char * buf, char * fun )
623 { int r;
625 r = pf_command(unit,cmd,dlen,fun);
626 mdelay(1);
627 if (!r) r = pf_completion(unit,buf,fun);
628 if (r) pf_req_sense(unit,!fun);
630 return r;
633 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
635 static void pf_lock(int unit, int func)
637 { char lo_cmd[12] = { ATAPI_LOCK,LUN,0,0,func,0,0,0,0,0,0,0 };
639 pf_atapi(unit,lo_cmd,0,pf_scratch,func?"unlock":"lock");
643 static void pf_eject( int unit )
645 { char ej_cmd[12] = { ATAPI_DOOR,LUN,0,0,2,0,0,0,0,0,0,0 };
647 pf_lock(unit,0);
648 pf_atapi(unit,ej_cmd,0,pf_scratch,"eject");
651 #define PF_RESET_TMO 30 /* in tenths of a second */
653 static void pf_sleep( int cs )
655 { current->state = TASK_INTERRUPTIBLE;
656 current->timeout = jiffies + cs;
657 schedule();
661 static int pf_reset( int unit )
663 /* the ATAPI standard actually specifies the contents of all 7 registers
664 after a reset, but the specification is ambiguous concerning the last
665 two bytes, and different drives interpret the standard differently.
668 { int i, k, flg;
669 int expect[5] = {1,1,1,0x14,0xeb};
671 pi_connect(PI);
672 WR(0,6,DRIVE);
673 WR(0,7,8);
675 pf_sleep(2);
677 k = 0;
678 while ((k++ < PF_RESET_TMO) && (RR(1,6)&STAT_BUSY))
679 pf_sleep(10);
681 flg = 1;
682 for(i=0;i<5;i++) flg &= (RR(0,i+1) == expect[i]);
684 if (verbose) {
685 printk("%s: Reset (%d) signature = ",PF.name,k);
686 for (i=0;i<5;i++) printk("%3x",RR(0,i+1));
687 if (!flg) printk(" (incorrect)");
688 printk("\n");
691 pi_disconnect(PI);
692 return flg-1;
695 static void pf_mode_sense( int unit )
697 { char ms_cmd[12] = { ATAPI_MODE_SENSE,LUN,0,0,0,0,0,0,8,0,0,0};
698 char buf[8];
700 pf_atapi(unit,ms_cmd,8,buf,DBMSG("mode sense"));
701 PF.media_status = PF_RW;
702 if (buf[3] & 0x80) PF.media_status = PF_RO;
705 static void xs( char *buf, char *targ, int offs, int len )
707 { int j,k,l;
709 j=0; l=0;
710 for (k=0;k<len;k++)
711 if((buf[k+offs]!=0x20)||(buf[k+offs]!=l))
712 l=targ[j++]=buf[k+offs];
713 if (l==0x20) j--; targ[j]=0;
716 static int xl( char *buf, int offs )
718 { int v,k;
720 v=0;
721 for(k=0;k<4;k++) v=v*256+(buf[k+offs]&0xff);
722 return v;
725 static void pf_get_capacity( int unit )
727 { char rc_cmd[12] = { ATAPI_CAPACITY,LUN,0,0,0,0,0,0,0,0,0,0};
728 char buf[8];
729 int bs;
731 if (pf_atapi(unit,rc_cmd,8,buf,DBMSG("get capacity"))) {
732 PF.media_status = PF_NM;
733 return;
735 PF.capacity = xl(buf,0) + 1;
736 bs = xl(buf,4);
737 if (bs != 512) {
738 PF.capacity = 0;
739 if (verbose) printk("%s: Drive %d, LUN %d,"
740 " unsupported block size %d\n",
741 PF.name,PF.drive,PF.lun,bs);
745 static int pf_identify( int unit )
747 { int dt, s;
748 char *ms[2] = {"master","slave"};
749 char mf[10], id[18];
750 char id_cmd[12] = { ATAPI_IDENTIFY,LUN,0,0,36,0,0,0,0,0,0,0};
751 char buf[36];
753 s = pf_atapi(unit,id_cmd,36,buf,"identify");
754 if (s) return -1;
756 dt = buf[0] & 0x1f;
757 if ((dt != 0) && (dt != 7)) {
758 if (verbose)
759 printk("%s: Drive %d, LUN %d, unsupported type %d\n",
760 PF.name,PF.drive,PF.lun,dt);
761 return -1;
764 xs(buf,mf,8,8);
765 xs(buf,id,16,16);
767 PF.removable = (buf[1] & 0x80);
769 pf_mode_sense(unit);
770 pf_mode_sense(unit);
771 pf_mode_sense(unit);
773 pf_get_capacity(unit);
775 printk("%s: %s %s, %s LUN %d, type %d",
776 PF.name,mf,id,ms[PF.drive],PF.lun,dt);
777 if (PF.removable) printk(", removable");
778 if (PF.media_status == PF_NM)
779 printk(", no media\n");
780 else { if (PF.media_status == PF_RO) printk(", RO");
781 printk(", %d blocks\n",PF.capacity);
784 return 0;
787 static int pf_probe( int unit )
789 /* returns 0, with id set if drive is detected
790 -1, if drive detection failed
793 { if (PF.drive == -1) {
794 for (PF.drive=0;PF.drive<=1;PF.drive++)
795 if (!pf_reset(unit)) {
796 if (PF.lun != -1) return pf_identify(unit);
797 else for (PF.lun=0;PF.lun<8;PF.lun++)
798 if (!pf_identify(unit)) return 0;
800 } else {
801 if (pf_reset(unit)) return -1;
802 if (PF.lun != -1) return pf_identify(unit);
803 for (PF.lun=0;PF.lun<8;PF.lun++)
804 if (!pf_identify(unit)) return 0;
806 return -1;
809 static int pf_detect( void )
811 { int k, unit;
813 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
814 name,name,PF_VERSION,major,cluster,nice);
816 k = 0;
817 if (pf_drive_count == 0) {
818 unit = 0;
819 if (pi_init(PI,1,-1,-1,-1,-1,-1,pf_scratch,
820 PI_PF,verbose,PF.name)) {
821 if (!pf_probe(unit)) {
822 PF.present = 1;
823 k++;
824 } else pi_release(PI);
827 } else for (unit=0;unit<PF_UNITS;unit++) if (DU[D_PRT])
828 if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
829 DU[D_PRO],DU[D_DLY],pf_scratch,PI_PF,verbose,
830 PF.name)) {
831 if (!pf_probe(unit)) {
832 PF.present = 1;
833 k++;
834 } else pi_release(PI);
837 if (k) return 0;
839 printk("%s: No ATAPI disk detected\n",name);
840 return -1;
843 /* The i/o request engine */
845 static int pf_start( int unit, int cmd, int b, int c )
847 { int i;
848 char io_cmd[12] = {cmd,LUN,0,0,0,0,0,0,0,0,0,0};
850 for(i=0;i<4;i++) {
851 io_cmd[5-i] = b & 0xff;
852 b = b >> 8;
855 io_cmd[8] = c & 0xff;
856 io_cmd[7] = (c >> 8) & 0xff;
858 i = pf_command(unit,io_cmd,c*512,"start i/o");
860 mdelay(1);
862 return i;
865 static int pf_ready( void )
867 { int unit = pf_unit;
869 return (((RR(1,6)&(STAT_BUSY|pf_mask)) == pf_mask));
872 static void do_pf_request (void)
874 { struct buffer_head * bh;
875 struct request * req;
876 int unit;
878 if (pf_busy) return;
879 repeat:
880 if ((!CURRENT) || (CURRENT->rq_status == RQ_INACTIVE)) return;
881 INIT_REQUEST;
883 pf_unit = unit = DEVICE_NR(CURRENT->rq_dev);
884 pf_block = CURRENT->sector;
885 pf_count = CURRENT->nr_sectors;
887 bh = CURRENT->bh;
888 req = CURRENT;
889 if (bh->b_reqnext)
890 printk("%s: OUCH: b_reqnext != NULL\n",PF.name);
892 if ((pf_unit >= PF_UNITS) || (pf_block+pf_count > PF.capacity)) {
893 end_request(0);
894 goto repeat;
897 pf_cmd = CURRENT->cmd;
898 pf_run = pf_count;
899 while ((pf_run <= cluster) &&
900 (req = req->next) &&
901 (pf_block+pf_run == req->sector) &&
902 (pf_cmd == req->cmd) &&
903 (pf_unit == DEVICE_NR(req->rq_dev)))
904 pf_run += req->nr_sectors;
906 pf_buf = CURRENT->buffer;
907 pf_retries = 0;
909 pf_busy = 1;
910 if (pf_cmd == READ) pi_do_claimed(PI,do_pf_read);
911 else if (pf_cmd == WRITE) pi_do_claimed(PI,do_pf_write);
912 else { pf_busy = 0;
913 end_request(0);
914 goto repeat;
918 static void pf_next_buf( int unit )
920 { long saved_flags;
922 spin_lock_irqsave(&io_request_lock,saved_flags);
923 end_request(1);
924 if (!pf_run) { spin_unlock_irqrestore(&io_request_lock,saved_flags);
925 return;
928 /* paranoia */
930 if ((!CURRENT) ||
931 (CURRENT->cmd != pf_cmd) ||
932 (DEVICE_NR(CURRENT->rq_dev) != pf_unit) ||
933 (CURRENT->rq_status == RQ_INACTIVE) ||
934 (CURRENT->sector != pf_block))
935 printk("%s: OUCH: request list changed unexpectedly\n",
936 PF.name);
938 pf_count = CURRENT->nr_sectors;
939 pf_buf = CURRENT->buffer;
940 spin_unlock_irqrestore(&io_request_lock,saved_flags);
943 static void do_pf_read( void )
945 /* detach from the calling context - in case the spinlock is held */
947 { ps_set_intr(do_pf_read_start,0,0,nice);
950 static void do_pf_read_start( void )
952 { int unit = pf_unit;
953 long saved_flags;
955 pf_busy = 1;
957 if (pf_start(unit,ATAPI_READ_10,pf_block,pf_run)) {
958 pi_disconnect(PI);
959 if (pf_retries < PF_MAX_RETRIES) {
960 pf_retries++;
961 pi_do_claimed(PI,do_pf_read_start);
962 return;
964 spin_lock_irqsave(&io_request_lock,saved_flags);
965 end_request(0);
966 pf_busy = 0;
967 do_pf_request();
968 spin_unlock_irqrestore(&io_request_lock,saved_flags);
969 return;
971 pf_mask = STAT_DRQ;
972 ps_set_intr(do_pf_read_drq,pf_ready,PF_TMO,nice);
975 static void do_pf_read_drq( void )
977 { int unit = pf_unit;
978 long saved_flags;
980 while (1) {
981 if (pf_wait(unit,STAT_BUSY,STAT_DRQ|STAT_ERR,
982 "read block","completion") & STAT_ERR) {
983 pi_disconnect(PI);
984 if (pf_retries < PF_MAX_RETRIES) {
985 pf_req_sense(unit,0);
986 pf_retries++;
987 pi_do_claimed(PI,do_pf_read_start);
988 return;
990 spin_lock_irqsave(&io_request_lock,saved_flags);
991 end_request(0);
992 pf_busy = 0;
993 do_pf_request();
994 spin_unlock_irqrestore(&io_request_lock,saved_flags);
995 return;
997 pi_read_block(PI,pf_buf,512);
998 pf_count--; pf_run--;
999 pf_buf += 512;
1000 pf_block++;
1001 if (!pf_run) break;
1002 if (!pf_count) pf_next_buf(unit);
1004 pi_disconnect(PI);
1005 spin_lock_irqsave(&io_request_lock,saved_flags);
1006 end_request(1);
1007 pf_busy = 0;
1008 do_pf_request();
1009 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1012 static void do_pf_write( void )
1014 { ps_set_intr(do_pf_write_start,0,0,nice);
1017 static void do_pf_write_start( void )
1019 { int unit = pf_unit;
1020 long saved_flags;
1022 pf_busy = 1;
1024 if (pf_start(unit,ATAPI_WRITE_10,pf_block,pf_run)) {
1025 pi_disconnect(PI);
1026 if (pf_retries < PF_MAX_RETRIES) {
1027 pf_retries++;
1028 pi_do_claimed(PI,do_pf_write_start);
1029 return;
1031 spin_lock_irqsave(&io_request_lock,saved_flags);
1032 end_request(0);
1033 pf_busy = 0;
1034 do_pf_request();
1035 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1036 return;
1039 while (1) {
1040 if (pf_wait(unit,STAT_BUSY,STAT_DRQ|STAT_ERR,
1041 "write block","data wait") & STAT_ERR) {
1042 pi_disconnect(PI);
1043 if (pf_retries < PF_MAX_RETRIES) {
1044 pf_retries++;
1045 pi_do_claimed(PI,do_pf_write_start);
1046 return;
1048 spin_lock_irqsave(&io_request_lock,saved_flags);
1049 end_request(0);
1050 pf_busy = 0;
1051 do_pf_request();
1052 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1053 return;
1055 pi_write_block(PI,pf_buf,512);
1056 pf_count--; pf_run--;
1057 pf_buf += 512;
1058 pf_block++;
1059 if (!pf_run) break;
1060 if (!pf_count) pf_next_buf(unit);
1062 pf_mask = 0;
1063 ps_set_intr(do_pf_write_done,pf_ready,PF_TMO,nice);
1066 static void do_pf_write_done( void )
1068 { int unit = pf_unit;
1069 long saved_flags;
1071 if (pf_wait(unit,STAT_BUSY,0,"write block","done") & STAT_ERR) {
1072 pi_disconnect(PI);
1073 if (pf_retries < PF_MAX_RETRIES) {
1074 pf_retries++;
1075 pi_do_claimed(PI,do_pf_write_start);
1076 return;
1078 spin_lock_irqsave(&io_request_lock,saved_flags);
1079 end_request(0);
1080 pf_busy = 0;
1081 do_pf_request();
1082 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1083 return;
1085 pi_disconnect(PI);
1086 spin_lock_irqsave(&io_request_lock,saved_flags);
1087 end_request(1);
1088 pf_busy = 0;
1089 do_pf_request();
1090 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1093 /* end of pf.c */