117 #define PD_VERSION "1.05"
127 #include <linux/types.h>
136 static int drive0[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
137 static int drive1[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
138 static int drive2[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
139 static int drive3[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
141 static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
148 #include <linux/module.h>
150 #include <linux/fs.h>
153 #include <linux/cdrom.h>
156 #include <linux/kernel.h>
158 #include <asm/uaccess.h>
180 #define PD_LOG_HEADS 64
181 #define PD_LOG_SECTS 32
186 #define PD_MAX_RETRIES 5
188 #define PD_SPIN_DEL 50
190 #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
192 #define STAT_ERR 0x00001
193 #define STAT_INDEX 0x00002
194 #define STAT_ECC 0x00004
195 #define STAT_DRQ 0x00008
196 #define STAT_SEEK 0x00010
197 #define STAT_WRERR 0x00020
198 #define STAT_READY 0x00040
199 #define STAT_BUSY 0x00080
201 #define ERR_AMNF 0x00100
202 #define ERR_TK0NF 0x00200
203 #define ERR_ABRT 0x00400
204 #define ERR_MCR 0x00800
205 #define ERR_IDNF 0x01000
206 #define ERR_MC 0x02000
207 #define ERR_UNC 0x04000
208 #define ERR_TMO 0x10000
210 #define IDE_READ 0x20
211 #define IDE_WRITE 0x30
212 #define IDE_READ_VRFY 0x40
213 #define IDE_INIT_DEV_PARMS 0x91
214 #define IDE_STANDBY 0x96
215 #define IDE_ACKCHANGE 0xdb
216 #define IDE_DOORLOCK 0xde
217 #define IDE_DOORUNLOCK 0xdf
218 #define IDE_IDENTIFY 0xec
219 #define IDE_EJECT 0xed
243 static char pd_scratch[512];
245 static char *pd_errs[17] = {
"ERR",
"INDEX",
"ECC",
"DRQ",
"SEEK",
"WRERR",
246 "READY",
"BUSY",
"AMNF",
"TK0NF",
"ABRT",
"MCR",
247 "IDNF",
"MC",
"UNC",
"???",
"TMO"
250 static inline int status_reg(
struct pd_unit *disk)
260 static inline void write_status(
struct pd_unit *disk,
int val)
270 static inline u8 DRIVE(
struct pd_unit *disk)
272 return 0xa0+0x10*disk->
drive;
281 printk(
"%s: %s: status = 0x%x =", disk->
name, msg, status);
283 if (status & (1 << i))
284 printk(
" %s", pd_errs[i]);
288 static void pd_reset(
struct pd_unit *disk)
290 write_status(disk, 4);
292 write_status(disk, 0);
296 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
298 static int pd_wait_for(
struct pd_unit *disk,
int w,
char *msg)
304 r = status_reg(disk);
314 pd_print_error(disk, msg, e);
318 static void pd_send_command(
struct pd_unit *disk,
int n,
int s,
int h,
int c0,
int c1,
int func)
337 c0 = (block >>= 8) & 255;
338 c1 = (block >>= 8) & 255;
339 h = ((block >>= 8) & 15) + 0x40;
341 s = (block % disk->
sectors) + 1;
343 c0 = (block /= disk->
heads) % 256;
346 pd_send_command(disk, count, s, h, c0, c1, func);
356 static void run_fsm(
void);
362 static void schedule_fsm(
void)
375 static enum action do_pd_io_start(
void);
376 static enum action pd_special(
void);
377 static enum action do_pd_read_start(
void);
378 static enum action do_pd_write_start(
void);
379 static enum action do_pd_read_drq(
void);
380 static enum action do_pd_write_done(
void);
383 static int pd_claimed;
385 static struct pd_unit *pd_current;
386 static PIA *pi_current;
388 static void run_fsm(
void)
392 unsigned long saved_flags;
396 pd_current = pd_req->rq_disk->private_data;
397 pi_current = pd_current->
pi;
398 phase = do_pd_io_start;
401 switch (pd_claimed) {
408 pi_current->
proto->connect(pi_current);
411 switch(res =
phase()) {
418 res ==
Ok ? 0 : -
EIO)) {
423 spin_unlock_irqrestore(&pd_lock, saved_flags);
436 static int pd_retries = 0;
443 static enum action do_pd_io_start(
void)
445 if (pd_req->cmd_type == REQ_TYPE_SPECIAL) {
450 pd_cmd = rq_data_dir(pd_req);
452 pd_block = blk_rq_pos(pd_req);
453 pd_count = blk_rq_cur_sectors(pd_req);
454 if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
456 pd_run = blk_rq_sectors(pd_req);
457 pd_buf = pd_req->buffer;
460 return do_pd_read_start();
462 return do_pd_write_start();
467 static enum action pd_special(
void)
470 return func(pd_current);
473 static int pd_next_buf(
void)
475 unsigned long saved_flags;
487 pd_count = blk_rq_cur_sectors(pd_req);
488 pd_buf = pd_req->buffer;
489 spin_unlock_irqrestore(&pd_lock, saved_flags);
493 static unsigned long pd_timeout;
495 static enum action do_pd_read_start(
void)
504 pd_ide_command(pd_current,
IDE_READ, pd_block, pd_run);
505 phase = do_pd_read_drq;
510 static enum action do_pd_write_start(
void)
519 pd_ide_command(pd_current,
IDE_WRITE, pd_block, pd_run);
532 phase = do_pd_write_done;
537 static inline int pd_ready(
void)
539 return !(status_reg(pd_current) &
STAT_BUSY);
542 static enum action do_pd_read_drq(
void)
551 phase = do_pd_read_start;
563 static enum action do_pd_write_done(
void)
571 phase = do_pd_write_start;
587 static void pd_init_dev_parms(
struct pd_unit *disk)
589 pd_wait_for(disk, 0,
DBMSG(
"before init_dev_parms"));
590 pd_send_command(disk, disk->
sectors, 0, disk->
heads - 1, 0, 0,
593 pd_wait_for(disk, 0,
"Initialise device parameters");
616 pd_wait_for(disk, 0,
DBMSG(
"before unlock on eject"));
618 pd_wait_for(disk, 0,
DBMSG(
"after unlock on eject"));
619 pd_wait_for(disk, 0,
DBMSG(
"before eject"));
620 pd_send_command(disk, 0, 0, 0, 0, 0,
IDE_EJECT);
621 pd_wait_for(disk, 0,
DBMSG(
"after eject"));
643 static void pd_standby_off(
struct pd_unit *disk)
645 pd_wait_for(disk, 0,
DBMSG(
"before STANDBY"));
647 pd_wait_for(disk, 0,
DBMSG(
"after STANDBY"));
661 if (disk->
drive == 0)
665 pd_wait_for(disk, 0,
DBMSG(
"before IDENT"));
668 if (pd_wait_for(disk,
STAT_DRQ,
DBMSG(
"IDENT DRQ")) & STAT_ERR)
671 disk->
can_lba = pd_scratch[99] & 2;
683 while ((j >= 0) && (
id[j] <= 0x20))
690 printk(
"%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
692 disk->
drive ?
"slave" :
"master",
695 disk->
removable ?
"removable" :
"fixed");
698 pd_init_dev_parms(disk);
700 pd_standby_off(disk);
718 static int pd_special_command(
struct pd_unit *disk,
726 rq->cmd_type = REQ_TYPE_SPECIAL;
745 pd_special_command(disk, pd_media_check);
746 pd_special_command(disk, pd_door_lock);
770 unsigned int cmd,
unsigned long arg)
778 pd_special_command(disk, pd_eject);
786 static int pd_release(
struct gendisk *
p,
fmode_t mode)
788 struct pd_unit *disk = p->private_data;
792 pd_special_command(disk, pd_door_unlock);
798 static unsigned int pd_check_events(
struct gendisk *p,
unsigned int clearing)
800 struct pd_unit *disk = p->private_data;
804 pd_special_command(disk, pd_media_check);
807 return r ? DISK_EVENT_MEDIA_CHANGE : 0;
810 static int pd_revalidate(
struct gendisk *p)
812 struct pd_unit *disk = p->private_data;
813 if (pd_special_command(disk, pd_identify) == 0)
820 static const struct block_device_operations pd_fops = {
823 .release = pd_release,
826 .check_events = pd_check_events,
827 .revalidate_disk= pd_revalidate
832 static void pd_probe_drive(
struct pd_unit *disk)
840 p->first_minor = (disk - pd) <<
PD_BITS;
842 p->private_data = disk;
845 if (disk->
drive == -1) {
847 if (pd_special_command(disk, pd_identify) == 0)
849 }
else if (pd_special_command(disk, pd_identify) == 0)
855 static int pd_detect(
void)
857 int found = 0,
unit, pd_drive_count = 0;
863 disk->
pi = &disk->
pia;
875 if (pd_drive_count == 0) {
877 if (
pi_init(disk->
pi, 1, -1, -1, -1, -1, -1, pd_scratch,
879 pd_probe_drive(disk);
885 for (unit = 0, disk = pd; unit <
PD_UNITS; unit++, disk++) {
886 int *parm = *drives[
unit];
892 pd_probe_drive(disk);
898 for (unit = 0, disk = pd; unit <
PD_UNITS; unit++, disk++) {
910 static int __init pd_init(
void)
924 printk(
"%s: %s version %s, major %d, cluster %d, nice %d\n",
939 static void __exit pd_exit(
void)
944 for (unit = 0, disk = pd; unit <
PD_UNITS; unit++, disk++) {
945 struct gendisk *p = disk->
gd;