116 #define PF_VERSION "1.04"
121 #include <linux/types.h>
136 static int drive0[7] = { 0, 0, 0, -1, -1, -1, -1 };
137 static int drive1[7] = { 0, 0, 0, -1, -1, -1, -1 };
138 static int drive2[7] = { 0, 0, 0, -1, -1, -1, -1 };
139 static int drive3[7] = { 0, 0, 0, -1, -1, -1, -1 };
141 static int (*drives[4])[7] = {&drive0, &drive1, &drive2, &drive3};
142 static int pf_drive_count;
148 #include <linux/module.h>
150 #include <linux/fs.h>
153 #include <linux/cdrom.h>
158 #include <asm/uaccess.h>
178 #define PF_FD_MAX 8192
184 #define PF_MAX_RETRIES 5
186 #define PF_SPIN_DEL 50
188 #define PF_SPIN (1000000*PF_TMO)/(HZ*PF_SPIN_DEL)
190 #define STAT_ERR 0x00001
191 #define STAT_INDEX 0x00002
192 #define STAT_ECC 0x00004
193 #define STAT_DRQ 0x00008
194 #define STAT_SEEK 0x00010
195 #define STAT_WRERR 0x00020
196 #define STAT_READY 0x00040
197 #define STAT_BUSY 0x00080
199 #define ATAPI_REQ_SENSE 0x03
200 #define ATAPI_LOCK 0x1e
201 #define ATAPI_DOOR 0x1b
202 #define ATAPI_MODE_SENSE 0x5a
203 #define ATAPI_CAPACITY 0x25
204 #define ATAPI_IDENTIFY 0x12
205 #define ATAPI_READ_10 0x28
206 #define ATAPI_WRITE_10 0x2a
211 unsigned int cmd,
unsigned long arg);
214 static int pf_release(
struct gendisk *disk,
fmode_t mode);
216 static int pf_detect(
void);
217 static void do_pf_read(
void);
218 static void do_pf_read_start(
void);
219 static void do_pf_write(
void);
220 static void do_pf_write_start(
void);
221 static void do_pf_read_drq(
void);
222 static void do_pf_write_done(
void);
245 static int pf_identify(
struct pf_unit *
pf);
247 static void pf_eject(
struct pf_unit *
pf);
248 static unsigned int pf_check_events(
struct gendisk *
disk,
249 unsigned int clearing);
251 static char pf_scratch[512];
257 static int pf_retries = 0;
258 static int pf_busy = 0;
264 static struct pf_unit *pf_current;
270 static const struct block_device_operations pf_fops = {
273 .release = pf_release,
276 .check_events = pf_check_events,
279 static void __init pf_init_units(
void)
285 for (unit = 0, pf = units; unit <
PF_UNITS; unit++, pf++) {
296 disk->first_minor =
unit;
298 disk->fops = &pf_fops;
299 if (!(*drives[unit])[
D_PRT])
363 static int pf_release(
struct gendisk *disk,
fmode_t mode)
365 struct pf_unit *pf = disk->private_data;
383 static unsigned int pf_check_events(
struct gendisk *disk,
unsigned int clearing)
385 return DISK_EVENT_MEDIA_CHANGE;
388 static inline int status_reg(
struct pf_unit *pf)
408 while ((((r = status_reg(pf)) & go) || (stop && (!(r & stop))))
419 printk(
"%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
420 " loop=%d phase=%d\n",
421 pf->
name, fun, msg, r, s, e, j, p);
427 static int pf_command(
struct pf_unit *pf,
char *cmd,
int dlen,
char *fun)
448 printk(
"%s: %s: command phase error\n", pf->
name, fun);
458 static int pf_completion(
struct pf_unit *pf,
char *
buf,
char *fun)
481 {
ATAPI_REQ_SENSE, pf->
lun << 5, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
485 r = pf_command(pf, rs_cmd, 16,
"Request sense");
488 pf_completion(pf, buf,
"Request sense");
490 if ((!r) && (!quiet))
491 printk(
"%s: Sense key: %x, ASC: %x, ASQ: %x\n",
492 pf->
name, buf[2] & 0xf, buf[12], buf[13]);
495 static int pf_atapi(
struct pf_unit *pf,
char *cmd,
int dlen,
char *buf,
char *fun)
499 r = pf_command(pf, cmd, dlen, fun);
502 r = pf_completion(pf, buf, fun);
504 pf_req_sense(pf, !fun);
511 char lo_cmd[12] = {
ATAPI_LOCK, pf->
lun << 5, 0, 0,
func, 0, 0, 0, 0, 0, 0, 0 };
513 pf_atapi(pf, lo_cmd, 0, pf_scratch, func ?
"lock" :
"unlock");
516 static void pf_eject(
struct pf_unit *pf)
518 char ej_cmd[12] = {
ATAPI_DOOR, pf->
lun << 5, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
521 pf_atapi(pf, ej_cmd, 0, pf_scratch,
"eject");
524 #define PF_RESET_TMO 30
526 static void pf_sleep(
int cs)
536 static int pf_reset(
struct pf_unit *pf)
539 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
545 pf_sleep(20 *
HZ / 1000);
552 for (i = 0; i < 5; i++)
553 flg &= (
read_reg(pf, i + 1) == expect[i]);
556 printk(
"%s: Reset (%d) signature = ", pf->
name, k);
557 for (i = 0; i < 5; i++)
568 static void pf_mode_sense(
struct pf_unit *pf)
571 {
ATAPI_MODE_SENSE, pf->
lun << 5, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0 };
574 pf_atapi(pf, ms_cmd, 8, buf,
"mode sense");
580 static void xs(
char *buf,
char *targ,
int offs,
int len)
586 for (k = 0; k < len; k++)
587 if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
588 l = targ[j++] = buf[k +
offs];
594 static int xl(
char *buf,
int offs)
599 for (k = 0; k < 4; k++)
600 v = v * 256 + (buf[k + offs] & 0xff);
604 static void pf_get_capacity(
struct pf_unit *pf)
606 char rc_cmd[12] = {
ATAPI_CAPACITY, pf->
lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
610 if (pf_atapi(pf, rc_cmd, 8, buf,
"get capacity")) {
614 set_capacity(pf->
disk, xl(buf, 0) + 1);
617 set_capacity(pf->
disk, 0);
619 printk(
"%s: Drive %d, LUN %d,"
620 " unsupported block size %d\n",
625 static int pf_identify(
struct pf_unit *pf)
628 char *
ms[2] = {
"master",
"slave" };
631 {
ATAPI_IDENTIFY, pf->
lun << 5, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
634 s = pf_atapi(pf, id_cmd, 36, buf,
"identify");
639 if ((dt != 0) && (dt != 7)) {
641 printk(
"%s: Drive %d, LUN %d, unsupported type %d\n",
657 printk(
"%s: %s %s, %s LUN %d, type %d",
667 (
unsigned long long)get_capacity(pf->
disk));
675 static int pf_probe(
struct pf_unit *pf)
677 if (pf->
drive == -1) {
681 return pf_identify(pf);
683 for (pf->
lun = 0; pf->
lun < 8; pf->
lun++)
684 if (!pf_identify(pf))
691 return pf_identify(pf);
692 for (pf->
lun = 0; pf->
lun < 8; pf->
lun++)
693 if (!pf_identify(pf))
699 static int pf_detect(
void)
704 printk(
"%s: %s version %s, major %d, cluster %d, nice %d\n",
708 if (pf_drive_count == 0) {
711 if (!pf_probe(pf) && pf->
disk) {
719 for (unit = 0; unit <
PF_UNITS; unit++, pf++) {
720 int *conf = *drives[
unit];
726 if (pf->
disk && !pf_probe(pf)) {
737 for (pf = units, unit = 0; unit <
PF_UNITS; pf++, unit++)
744 static int pf_start(
struct pf_unit *pf,
int cmd,
int b,
int c)
747 char io_cmd[12] = {
cmd, pf->
lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
749 for (i = 0; i < 4; i++) {
750 io_cmd[5 -
i] = b & 0xff;
754 io_cmd[8] = c & 0xff;
755 io_cmd[7] = (c >> 8) & 0xff;
757 i = pf_command(pf, io_cmd, c * 512,
"start i/o");
764 static int pf_ready(
void)
766 return (((status_reg(pf_current) & (STAT_BUSY | pf_mask)) == pf_mask));
771 static void pf_end_request(
int err)
788 pf_current = pf_req->rq_disk->private_data;
789 pf_block = blk_rq_pos(pf_req);
790 pf_run = blk_rq_sectors(pf_req);
791 pf_count = blk_rq_cur_sectors(pf_req);
793 if (pf_block + pf_count > get_capacity(pf_req->rq_disk)) {
794 pf_end_request(-
EIO);
798 pf_cmd = rq_data_dir(pf_req);
799 pf_buf = pf_req->buffer;
805 else if (pf_cmd ==
WRITE)
809 pf_end_request(-
EIO);
814 static int pf_next_buf(
void)
816 unsigned long saved_flags;
827 spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
830 pf_count = blk_rq_cur_sectors(pf_req);
831 pf_buf = pf_req->buffer;
836 static inline void next_request(
int err)
838 unsigned long saved_flags;
843 do_pf_request(pf_queue);
844 spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
848 static void do_pf_read(
void)
850 ps_set_intr(do_pf_read_start,
NULL, 0, nice);
853 static void do_pf_read_start(
void)
868 ps_set_intr(do_pf_read_drq, pf_ready,
PF_TMO, nice);
871 static void do_pf_read_drq(
void)
875 "read block",
"completion") &
STAT_ERR) {
878 pf_req_sense(pf_current, 0);
894 static void do_pf_write(
void)
896 ps_set_intr(do_pf_write_start,
NULL, 0, nice);
899 static void do_pf_write_start(
void)
916 "write block",
"data wait") &
STAT_ERR) {
931 ps_set_intr(do_pf_write_done, pf_ready,
PF_TMO, nice);
934 static void do_pf_write_done(
void)
936 if (pf_wait(pf_current, STAT_BUSY, 0,
"write block",
"done") &
STAT_ERR) {
950 static int __init pf_init(
void)
965 for (pf = units, unit = 0; unit <
PF_UNITS; pf++, unit++)
972 for (pf = units, unit = 0; unit <
PF_UNITS; pf++, unit++)
979 for (pf = units, unit = 0; unit <
PF_UNITS; pf++, unit++) {
980 struct gendisk *disk = pf->
disk;
984 disk->private_data =
pf;
985 disk->queue = pf_queue;
991 static void __exit pf_exit(
void)
996 for (pf = units, unit = 0; unit <
PF_UNITS; pf++, unit++) {