/* pd.c (c) 1997-8 Grant R. Guenther Under the terms of the GNU General Public License. This is the high-level driver for parallel port IDE hard drives based on chips supported by the paride module. By default, the driver will autoprobe for a single parallel port IDE drive, but if their individual parameters are specified, the driver can handle up to 4 drives. The behaviour of the pd driver can be altered by setting some parameters from the insmod command line. The following parameters are adjustable: drive0 These four arguments can be arrays of drive1 1-8 integers as follows: drive2 drive3 ,,,,,,, Where, is the base of the parallel port address for the corresponding drive. (required) is the protocol number for the adapter that supports this drive. These numbers are logged by 'paride' when the protocol modules are initialised. (0 if not given) for those adapters that support chained devices, this is the unit selector for the chain of devices on the given port. It should be zero for devices that don't support chaining. (0 if not given) this can be -1 to choose the best mode, or one of the mode numbers supported by the adapter. (-1 if not given) this defaults to 0 to indicate that the driver should use the CHS geometry provided by the drive itself. If set to 1, the driver will provide a logical geometry with 64 heads and 32 sectors per track, to be consistent with most SCSI drivers. (0 if not given) set this to zero to disable the power saving standby mode, if needed. (1 if not given) some parallel ports require the driver to go more slowly. -1 sets a default value that should work with the chosen protocol. Otherwise, set this to a small integer, the larger it is the slower the port i/o. In some cases, setting this to zero will speed up the device. (default -1) IDE disks can be jumpered to master or slave. Set this to 0 to choose the master drive, 1 to choose the slave, -1 (the default) to choose the first drive found. major You may use this parameter to overide the default major number (45) that this driver will use. Be sure to change the device name as well. name This parameter is a character string that contains the name the kernel will use for this device (in /proc output, for instance). (default "pd") cluster The driver will attempt to aggregate requests for adjacent blocks into larger multi-block clusters. The maximum cluster size (in 512 byte sectors) is set with this parameter. (default 64) verbose This parameter controls the amount of logging that the driver will do. Set it to 0 for normal operation, 1 to see autoprobe progress messages, or 2 to see additional debugging output. (default 0) nice This parameter controls the driver's use of idle CPU time, at the expense of some speed. If this driver is built into the kernel, you can use kernel the following command line parameters, with the same values as the corresponding module parameters listed above: pd.drive0 pd.drive1 pd.drive2 pd.drive3 pd.cluster pd.nice In addition, you can use the parameter pd.disable to disable the driver entirely. */ /* Changes: 1.01 GRG 1997.01.24 Restored pd_reset() Added eject ioctl 1.02 GRG 1998.05.06 SMP spinlock changes, Added slave support 1.03 GRG 1998.06.16 Eliminate an Ugh. 1.04 GRG 1998.08.15 Extra debugging, use HZ in loop timing 1.05 GRG 1998.09.24 Added jumbo support */ #define PD_VERSION "1.05" #define PD_MAJOR 45 #define PD_NAME "pd" #define PD_UNITS 4 /* Here are things one can override from the insmod command. Most are autoprobed by paride unless set here. Verbose is off by default. */ static int verbose = 0; static int major = PD_MAJOR; static char *name = PD_NAME; static int cluster = 64; static int nice = 0; static int disable = 0; static int drive0[8] = {0,0,0,-1,0,1,-1,-1}; static int drive1[8] = {0,0,0,-1,0,1,-1,-1}; static int drive2[8] = {0,0,0,-1,0,1,-1,-1}; static int drive3[8] = {0,0,0,-1,0,1,-1,-1}; static int (*drives[4])[8] = {&drive0,&drive1,&drive2,&drive3}; static int pd_drive_count; #define D_PRT 0 #define D_PRO 1 #define D_UNI 2 #define D_MOD 3 #define D_GEO 4 #define D_SBY 5 #define D_DLY 6 #define D_SLV 7 #define DU (*drives[unit]) /* end of parameters */ #include #include #include #include #include #include #include #include #include /* for the eject ioctl */ #include #include #ifndef MODULE #include "setup.h" static STT pd_stt[7] = {{"drive0",8,drive0}, {"drive1",8,drive1}, {"drive2",8,drive2}, {"drive3",8,drive3}, {"disable",1,&disable}, {"cluster",1,&cluster}, {"nice",1,&nice}}; void pd_setup( char *str, int *ints) { generic_setup(pd_stt,7,str); } #endif MODULE_PARM(verbose,"i"); MODULE_PARM(major,"i"); MODULE_PARM(name,"s"); MODULE_PARM(cluster,"i"); MODULE_PARM(nice,"i"); MODULE_PARM(drive0,"1-8i"); MODULE_PARM(drive1,"1-8i"); MODULE_PARM(drive2,"1-8i"); MODULE_PARM(drive3,"1-8i"); #include "paride.h" #define PD_BITS 4 /* set up defines for blk.h, why don't all drivers do it this way ? */ #define MAJOR_NR major #define DEVICE_NAME "PD" #define DEVICE_REQUEST do_pd_request #define DEVICE_NR(device) (MINOR(device)>>PD_BITS) #define DEVICE_ON(device) #define DEVICE_OFF(device) #include #include #include "pseudo.h" #define PD_PARTNS (1<i_rdev); if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV; wait_event (pd_wait_open, pd_valid); PD.access++; if (PD.removable) { pd_media_check(unit); pd_doorlock(unit,IDE_DOORLOCK); } return 0; } static int pd_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg) { struct hd_geometry *geo = (struct hd_geometry *) arg; int dev, err, unit; if ((!inode) || (!inode->i_rdev)) return -EINVAL; dev = MINOR(inode->i_rdev); unit = DEVICE_NR(inode->i_rdev); if (dev >= PD_DEVS) return -EINVAL; if (!PD.present) return -ENODEV; switch (cmd) { case CDROMEJECT: if (PD.access == 1) pd_eject(unit); return 0; case HDIO_GETGEO: if (!geo) return -EINVAL; err = verify_area(VERIFY_WRITE,geo,sizeof(*geo)); if (err) return err; if (PD.alt_geom) { put_user(PD.capacity/(PD_LOG_HEADS*PD_LOG_SECTS), (short *) &geo->cylinders); put_user(PD_LOG_HEADS, (char *) &geo->heads); put_user(PD_LOG_SECTS, (char *) &geo->sectors); } else { put_user(PD.cylinders, (short *) &geo->cylinders); put_user(PD.heads, (char *) &geo->heads); put_user(PD.sectors, (char *) &geo->sectors); } put_user(pd_hd[dev].start_sect,(long *)&geo->start); return 0; case BLKRRPART: if (!capable(CAP_SYS_ADMIN)) return -EACCES; return pd_revalidate(inode->i_rdev); case BLKGETSIZE: case BLKGETSIZE64: case BLKROSET: case BLKROGET: case BLKRASET: case BLKRAGET: case BLKFLSBUF: case BLKPG: return blk_ioctl(inode->i_rdev, cmd, arg); default: return -EINVAL; } } static int pd_release (struct inode *inode, struct file *file) { kdev_t devp; int unit; devp = inode->i_rdev; unit = DEVICE_NR(devp); if ((unit >= PD_UNITS) || (PD.access <= 0)) return -EINVAL; PD.access--; if (!PD.access && PD.removable) pd_doorlock(unit,IDE_DOORUNLOCK); return 0; } static int pd_check_media( kdev_t dev) { int r, unit; unit = DEVICE_NR(dev); if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV; if (!PD.removable) return 0; pd_media_check(unit); r = PD.changed; PD.changed = 0; return r; } static int pd_revalidate(kdev_t dev) { int p, unit, minor; long flags; unit = DEVICE_NR(dev); if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV; save_flags(flags); cli(); if (PD.access > 1) { restore_flags(flags); return -EBUSY; } pd_valid = 0; restore_flags(flags); for (p=(PD_PARTNS-1);p>=0;p--) { minor = p + unit*PD_PARTNS; invalidate_device(MKDEV(MAJOR_NR, minor), 1); pd_hd[minor].start_sect = 0; pd_hd[minor].nr_sects = 0; } if (pd_identify(unit)) grok_partitions(&pd_gendisk,unit,1<1)?(msg):NULL) static int pd_wait_for( int unit, int w, char * msg ) /* polled wait */ { int k, r, e; k=0; while(k < PD_SPIN) { r = RR(1,6); k++; if (((r & w) == w) && !(r & STAT_BUSY)) break; udelay(PD_SPIN_DEL); } e = (RR(0,1)<<8) + RR(0,7); if (k >= PD_SPIN) e |= ERR_TMO; if ((e & (STAT_ERR|ERR_TMO)) && (msg != NULL)) pd_print_error(unit,msg,e); return e; } static void pd_send_command( int unit, int n, int s, int h, int c0, int c1, int func ) { WR(0,6,DRIVE+h); WR(0,1,0); /* the IDE task file */ WR(0,2,n); WR(0,3,s); WR(0,4,c0); WR(0,5,c1); WR(0,7,func); udelay(1); } static void pd_ide_command( int unit, int func, int block, int count ) /* Don't use this call if the capacity is zero. */ { int c1, c0, h, s; s = ( block % PD.sectors) + 1; h = ( block / PD.sectors) % PD.heads; c0 = ( block / (PD.sectors*PD.heads)) % 256; c1 = ( block / (PD.sectors*PD.heads*256)); pd_send_command(unit,count,s,h,c0,c1,func); } /* According to the ATA standard, the default CHS geometry should be available following a reset. Some Western Digital drives come up in a mode where only LBA addresses are accepted until the device parameters are initialised. */ static void pd_init_dev_parms( int unit ) { pi_connect(PI); pd_wait_for(unit,0,DBMSG("before init_dev_parms")); pd_send_command(unit,PD.sectors,0,PD.heads-1,0,0,IDE_INIT_DEV_PARMS); udelay(300); pd_wait_for(unit,0,"Initialise device parameters"); pi_disconnect(PI); } static void pd_doorlock( int unit, int func ) { pi_connect(PI); if (pd_wait_for(unit,STAT_READY,"Lock") & STAT_ERR) { pi_disconnect(PI); return; } pd_send_command(unit,1,0,0,0,0,func); pd_wait_for(unit,STAT_READY,"Lock done"); pi_disconnect(PI); } static void pd_eject( int unit ) { pi_connect(PI); pd_wait_for(unit,0,DBMSG("before unlock on eject")); pd_send_command(unit,1,0,0,0,0,IDE_DOORUNLOCK); pd_wait_for(unit,0,DBMSG("after unlock on eject")); pd_wait_for(unit,0,DBMSG("before eject")); pd_send_command(unit,0,0,0,0,0,IDE_EJECT); pd_wait_for(unit,0,DBMSG("after eject")); pi_disconnect(PI); } static void pd_media_check( int unit ) { int r; pi_connect(PI); r = pd_wait_for(unit,STAT_READY,DBMSG("before media_check")); if (!(r & STAT_ERR)) { pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY); r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after READ_VRFY")); } else PD.changed = 1; /* say changed if other error */ if (r & ERR_MC) { PD.changed = 1; pd_send_command(unit,1,0,0,0,0,IDE_ACKCHANGE); pd_wait_for(unit,STAT_READY,DBMSG("RDY after ACKCHANGE")); pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY); r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after VRFY")); } pi_disconnect(PI); } static void pd_standby_off( int unit ) { pi_connect(PI); pd_wait_for(unit,0,DBMSG("before STANDBY")); pd_send_command(unit,0,0,0,0,0,IDE_STANDBY); pd_wait_for(unit,0,DBMSG("after STANDBY")); pi_disconnect(PI); } #define word_val(n) ((pd_scratch[2*n]&0xff)+256*(pd_scratch[2*n+1]&0xff)) static int pd_identify( int unit ) { int j; char id[PD_ID_LEN+1]; /* WARNING: here there may be dragons. reset() applies to both drives, but we call it only on probing the MASTER. This should allow most common configurations to work, but be warned that a reset can clear settings on the SLAVE drive. */ if (PD.drive == 0) pd_reset(unit); pi_connect(PI); WR(0,6,DRIVE); pd_wait_for(unit,0,DBMSG("before IDENT")); pd_send_command(unit,1,0,0,0,0,IDE_IDENTIFY); if (pd_wait_for(unit,STAT_DRQ,DBMSG("IDENT DRQ")) & STAT_ERR) { pi_disconnect(PI); return 0; } pi_read_block(PI,pd_scratch,512); pi_disconnect(PI); PD.sectors = word_val(6); PD.heads = word_val(3); PD.cylinders = word_val(1); PD.capacity = PD.sectors*PD.heads*PD.cylinders; for(j=0;j= 0) && (id[j] <= 0x20)) j--; j++; id[j] = 0; PD.removable = (word_val(0) & 0x80); printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n", PD.name,id, PD.drive?"slave":"master", PD.capacity,PD.capacity/2048, PD.cylinders,PD.heads,PD.sectors, PD.removable?"removable":"fixed"); if (PD.capacity) pd_init_dev_parms(unit); if (!PD.standby) pd_standby_off(unit); return 1; } static int pd_probe_drive( int unit ) { if (PD.drive == -1) { for (PD.drive=0;PD.drive<=1;PD.drive++) if (pd_identify(unit)) return 1; return 0; } return pd_identify(unit); } static int pd_detect( void ) { int k, unit; k = 0; if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */ unit = 0; if (pi_init(PI,1,-1,-1,-1,-1,-1,pd_scratch, PI_PD,verbose,PD.name)) { if (pd_probe_drive(unit)) { PD.present = 1; k = 1; } else pi_release(PI); } } else for (unit=0;unitrq_status == RQ_INACTIVE)) return; INIT_REQUEST; pd_dev = MINOR(CURRENT->rq_dev); pd_unit = unit = DEVICE_NR(CURRENT->rq_dev); pd_block = CURRENT->sector; pd_run = CURRENT->nr_sectors; pd_count = CURRENT->current_nr_sectors; bh = CURRENT->bh; if ((pd_dev >= PD_DEVS) || ((pd_block+pd_count) > pd_hd[pd_dev].nr_sects)) { end_request(0); goto repeat; } pd_cmd = CURRENT->cmd; pd_poffs = pd_hd[pd_dev].start_sect; pd_block += pd_poffs; pd_buf = CURRENT->buffer; pd_retries = 0; pd_busy = 1; if (pd_cmd == READ) pi_do_claimed(PI,do_pd_read); else if (pd_cmd == WRITE) pi_do_claimed(PI,do_pd_write); else { pd_busy = 0; end_request(0); goto repeat; } } static void pd_next_buf( int unit ) { long saved_flags; spin_lock_irqsave(&io_request_lock,saved_flags); end_request(1); if (!pd_run) { spin_unlock_irqrestore(&io_request_lock,saved_flags); return; } /* paranoia */ if (QUEUE_EMPTY || (CURRENT->cmd != pd_cmd) || (MINOR(CURRENT->rq_dev) != pd_dev) || (CURRENT->rq_status == RQ_INACTIVE) || (CURRENT->sector+pd_poffs != pd_block)) printk("%s: OUCH: request list changed unexpectedly\n", PD.name); pd_count = CURRENT->current_nr_sectors; pd_buf = CURRENT->buffer; spin_unlock_irqrestore(&io_request_lock,saved_flags); } static void do_pd_read( void ) { ps_set_intr(do_pd_read_start,0,0,nice); } static void do_pd_read_start( void ) { int unit = pd_unit; long saved_flags; pd_busy = 1; pi_connect(PI); if (pd_wait_for(unit,STAT_READY,"do_pd_read") & STAT_ERR) { pi_disconnect(PI); if (pd_retries < PD_MAX_RETRIES) { pd_retries++; pi_do_claimed(PI,do_pd_read_start); return; } spin_lock_irqsave(&io_request_lock,saved_flags); end_request(0); pd_busy = 0; do_pd_request(NULL); spin_unlock_irqrestore(&io_request_lock,saved_flags); return; } pd_ide_command(unit,IDE_READ,pd_block,pd_run); ps_set_intr(do_pd_read_drq,pd_ready,PD_TMO,nice); } static void do_pd_read_drq( void ) { int unit = pd_unit; long saved_flags; while (1) { if (pd_wait_for(unit,STAT_DRQ,"do_pd_read_drq") & STAT_ERR) { pi_disconnect(PI); if (pd_retries < PD_MAX_RETRIES) { pd_retries++; pi_do_claimed(PI,do_pd_read_start); return; } spin_lock_irqsave(&io_request_lock,saved_flags); end_request(0); pd_busy = 0; do_pd_request(NULL); spin_unlock_irqrestore(&io_request_lock,saved_flags); return; } pi_read_block(PI,pd_buf,512); pd_count--; pd_run--; pd_buf += 512; pd_block++; if (!pd_run) break; if (!pd_count) pd_next_buf(unit); } pi_disconnect(PI); spin_lock_irqsave(&io_request_lock,saved_flags); end_request(1); pd_busy = 0; do_pd_request(NULL); spin_unlock_irqrestore(&io_request_lock,saved_flags); } static void do_pd_write( void ) { ps_set_intr(do_pd_write_start,0,0,nice); } static void do_pd_write_start( void ) { int unit = pd_unit; long saved_flags; pd_busy = 1; pi_connect(PI); if (pd_wait_for(unit,STAT_READY,"do_pd_write") & STAT_ERR) { pi_disconnect(PI); if (pd_retries < PD_MAX_RETRIES) { pd_retries++; pi_do_claimed(PI,do_pd_write_start); return; } spin_lock_irqsave(&io_request_lock,saved_flags); end_request(0); pd_busy = 0; do_pd_request(NULL); spin_unlock_irqrestore(&io_request_lock,saved_flags); return; } pd_ide_command(unit,IDE_WRITE,pd_block,pd_run); while (1) { if (pd_wait_for(unit,STAT_DRQ,"do_pd_write_drq") & STAT_ERR) { pi_disconnect(PI); if (pd_retries < PD_MAX_RETRIES) { pd_retries++; pi_do_claimed(PI,do_pd_write_start); return; } spin_lock_irqsave(&io_request_lock,saved_flags); end_request(0); pd_busy = 0; do_pd_request(NULL); spin_unlock_irqrestore(&io_request_lock,saved_flags); return; } pi_write_block(PI,pd_buf,512); pd_count--; pd_run--; pd_buf += 512; pd_block++; if (!pd_run) break; if (!pd_count) pd_next_buf(unit); } ps_set_intr(do_pd_write_done,pd_ready,PD_TMO,nice); } static void do_pd_write_done( void ) { int unit = pd_unit; long saved_flags; if (pd_wait_for(unit,STAT_READY,"do_pd_write_done") & STAT_ERR) { pi_disconnect(PI); if (pd_retries < PD_MAX_RETRIES) { pd_retries++; pi_do_claimed(PI,do_pd_write_start); return; } spin_lock_irqsave(&io_request_lock,saved_flags); end_request(0); pd_busy = 0; do_pd_request(NULL); spin_unlock_irqrestore(&io_request_lock,saved_flags); return; } pi_disconnect(PI); spin_lock_irqsave(&io_request_lock,saved_flags); end_request(1); pd_busy = 0; do_pd_request(NULL); spin_unlock_irqrestore(&io_request_lock,saved_flags); } /* end of pd.c */ MODULE_LICENSE("GPL");