23 #define DRV_NAME "ide-cd"
24 #define PFX DRV_NAME ": "
26 #define IDECD_VERSION "5.00"
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
34 #include <linux/slab.h>
36 #include <linux/errno.h>
37 #include <linux/cdrom.h>
44 #include <scsi/scsi.h>
47 #include <asm/byteorder.h>
49 #include <asm/unaligned.h>
56 static void ide_cd_release(
struct device *);
101 if (!sense || !rq || (rq->cmd_flags &
REQ_QUIET))
106 switch (sense->sense_key) {
115 if (sense->
asc == 0x3a || sense->
asc == 0x04)
134 cdrom_saw_media_change(drive);
143 static void cdrom_analyze_sense_data(
ide_drive_t *drive,
144 struct request *failed_command)
149 unsigned long bio_sectors;
152 sense->error_code, sense->sense_key);
156 failed_command->cmd[0]);
158 if (!cdrom_log_sense(drive, failed_command))
167 if (sense->sense_key == 0x05 && sense->
asc == 0x24)
171 if (sense->error_code == 0x70) {
172 switch (sense->sense_key) {
178 if (failed_command ==
NULL ||
179 failed_command->cmd_type != REQ_TYPE_FS)
186 if (queue_logical_block_size(drive->
queue) == 2048)
190 bio_sectors =
max(bio_sectors(failed_command->bio), 4
U);
191 sector &= ~(bio_sectors - 1);
201 if (sector < get_capacity(info->
disk) &&
203 set_capacity(info->
disk, sector);
219 void *sense = bio_data(rq->bio);
228 memcpy(failed->sense, sense, 18);
229 failed->sense_len = rq->sense_len;
231 cdrom_analyze_sense_data(drive, failed);
236 cdrom_analyze_sense_data(drive,
NULL);
282 sense_key = err >> 4;
286 rq->cmd[0], rq->cmd_type, err, stat);
288 if (rq->cmd_type == REQ_TYPE_SENSE) {
299 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !rq->errors)
302 if (blk_noretry_request(rq))
307 if (rq->cmd_type == REQ_TYPE_FS && rq_data_dir(rq) ==
WRITE) {
308 if (ide_cd_breathe(drive, rq))
311 cdrom_saw_media_change(drive);
313 if (rq->cmd_type == REQ_TYPE_FS &&
321 cdrom_saw_media_change(drive);
323 if (rq->cmd_type != REQ_TYPE_FS)
360 "(bad sector)", stat);
371 if (rq->cmd_type != REQ_TYPE_FS)
375 ide_error(drive,
"cdrom_decode_status", stat);
382 if (rq->cmd_type != REQ_TYPE_FS) {
400 if (stat & ATA_ERR) {
425 unsigned int cmd_flags)
430 unsigned int flags = 0;
433 sense = &local_sense;
437 cmd[0], write, timeout, cmd_flags);
446 memcpy(rq->cmd, cmd, BLK_MAX_CDB);
447 rq->cmd_type = REQ_TYPE_ATA_PC;
449 rq->cmd_flags |= cmd_flags;
450 rq->timeout = timeout;
463 *bufflen = rq->resid_len;
465 flags = rq->cmd_flags;
480 cdrom_saw_media_change(drive);
481 else if (reqbuf->sense_key ==
NOT_READY &&
482 reqbuf->
asc == 4 && reqbuf->
ascq != 4) {
497 }
while ((flags &
REQ_FAILED) && retries >= 0);
500 return (flags & REQ_FAILED) ? -
EIO : 0;
527 int dma_error = 0,
dma, thislen, uptodate = 0;
529 int sense = (rq->cmd_type == REQ_TYPE_SENSE);
541 dma_error = hwif->
dma_ops->dma_end(drive);
545 write ?
"write" :
"read");
551 stat = hwif->
tp_ops->read_status(hwif);
554 rc = cdrom_decode_status(drive, stat);
565 return ide_error(drive,
"dma error", stat);
572 thislen = (rq->cmd_type == REQ_TYPE_FS) ? len : cmd->
nleft;
581 if (rq->cmd_type == REQ_TYPE_FS) {
587 if (cmd->
nleft > 0) {
589 "(%u bytes)\n", drive->
name, __func__,
595 }
else if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
596 ide_cd_request_sense_fixup(drive, cmd);
598 uptodate = cmd->
nleft ? 0 : 1;
604 if (!(stat & ATA_ERR) && !uptodate && thislen) {
606 uptodate = cmd->
nleft ? 0 : 1;
623 rq->cmd_type, ireason);
626 while (thislen > 0) {
638 if (sense && write == 0)
639 rq->sense_len += blen;
644 if (rq->cmd_type != REQ_TYPE_FS || write == 0)
653 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
654 timeout = rq->timeout;
657 if (rq->cmd_type != REQ_TYPE_FS)
666 if (rq->cmd_type == REQ_TYPE_BLOCK_PC &&
rc == 0) {
671 if (sense && uptodate)
672 ide_cd_complete_failed_rq(drive, rq);
674 if (rq->cmd_type == REQ_TYPE_FS) {
678 if (uptodate <= 0 && rq->
errors == 0)
682 if (uptodate == 0 && rq->bio)
683 if (ide_cd_error_cmd(drive, cmd))
687 if (rq->cmd_type != REQ_TYPE_FS) {
695 if (sense &&
rc == 2)
696 ide_error(drive,
"request sense failure", stat);
705 int write = rq_data_dir(rq) ==
WRITE;
706 unsigned short sectors_per_frame =
710 "secs_per_frame: %u",
711 rq->cmd[0], rq->cmd_flags, sectors_per_frame);
715 if (get_disk_ro(cd->
disk))
722 q->prep_rq_fn(q, rq);
726 if ((blk_rq_sectors(rq) & (sectors_per_frame - 1)) ||
727 (blk_rq_pos(rq) & (sectors_per_frame - 1)))
745 rq->cmd[0], rq->cmd_type);
747 if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
757 char *
buf = bio_data(rq->bio);
768 alignment = queue_dma_alignment(q) | q->dma_pad_mask;
769 if ((
unsigned long)buf & alignment
770 || blk_rq_bytes(rq) & q->dma_pad_mask
771 || object_is_on_stack(buf))
781 unsigned int nsectors;
784 rq->cmd[0], (
unsigned long long)block);
789 switch (rq->cmd_type) {
795 case REQ_TYPE_BLOCK_PC:
796 case REQ_TYPE_ATA_PC:
800 cdrom_do_block_pc(drive, rq);
802 case REQ_TYPE_SPECIAL:
813 memset(&cmd, 0,
sizeof(cmd));
820 if (rq->cmd_type == REQ_TYPE_FS || blk_rq_bytes(rq)) {
827 nsectors = blk_rq_sectors(rq);
846 static void msf_from_bcd(
struct atapi_msf *msf)
857 unsigned char cmd[BLK_MAX_CDB];
861 memset(cmd, 0, BLK_MAX_CDB);
874 unsigned long *sectors_per_frame,
883 unsigned char cmd[BLK_MAX_CDB];
884 unsigned len =
sizeof(capbuf);
889 memset(cmd, 0, BLK_MAX_CDB);
912 "setting default block size to 2048\n",
913 drive->
name, blocklen);
922 *capacity, *sectors_per_frame);
927 static int cdrom_read_tocentry(
ide_drive_t *drive,
int trackno,
int msf_flag,
931 unsigned char cmd[BLK_MAX_CDB];
935 memset(cmd, 0, BLK_MAX_CDB);
939 cmd[7] = (buflen >> 8);
940 cmd[8] = (buflen & 0xff);
941 cmd[9] = (format << 6);
952 int stat, ntracks,
i;
986 stat = cdrom_read_capacity(drive, &toc->
capacity, §ors_per_frame,
991 set_capacity(info->
disk, toc->
capacity * sectors_per_frame);
999 stat = cdrom_read_tocentry(drive, 0, 1, 0, (
char *) &toc->
hdr,
1009 ntracks = toc->
hdr.last_track - toc->
hdr.first_track + 1;
1016 stat = cdrom_read_tocentry(drive, toc->
hdr.first_track, 1, 0,
1022 if (stat && toc->
hdr.first_track > 1) {
1064 for (i = 0; i <= ntracks; i++) {
1068 msf_from_bcd(&toc->
ent[i].addr.msf);
1070 toc->
ent[
i].addr.lba = msf_to_lba(toc->
ent[i].addr.msf.minute,
1071 toc->
ent[i].addr.msf.second,
1072 toc->
ent[i].addr.msf.frame);
1077 stat = cdrom_read_tocentry(drive, 0, 0, 1, (
char *)&ms_tmp,
1078 sizeof(ms_tmp), sense);
1085 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
1091 stat = cdrom_read_tocentry(drive, 0, 1, 1, (
char *)&ms_tmp,
1092 sizeof(ms_tmp), sense);
1096 msf_from_bcd(&ms_tmp.ent.addr.msf);
1098 ms_tmp.ent.addr.msf.second,
1099 ms_tmp.ent.addr.msf.frame);
1102 toc->
xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1106 if (!stat && (last_written > toc->
capacity)) {
1108 set_capacity(info->
disk, toc->
capacity * sectors_per_frame);
1136 }
while (--attempts);
1143 u16 curspeed, maxspeed;
1156 curspeed, maxspeed);
1162 #define IDE_CD_CAPABILITIES \
1163 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1164 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1165 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1166 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1167 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
1185 static int ide_cdrom_register(
ide_drive_t *drive,
int nslots)
1192 devinfo->
ops = &ide_cdrom_dops;
1205 static int ide_cdrom_probe_capabilities(
ide_drive_t *drive)
1240 cdi->
ops = &ide_cdrom_dops;
1245 if ((buf[8 + 6] & 0x01) == 0)
1247 if (buf[8 + 6] & 0x08)
1249 if (buf[8 + 3] & 0x01)
1251 if (buf[8 + 3] & 0x02)
1253 if (buf[8 + 2] & 0x38)
1255 if (buf[8 + 3] & 0x20)
1257 if (buf[8 + 3] & 0x10)
1262 mechtype = buf[8 + 6] >> 5;
1312 int hard_sect = queue_logical_block_size(q);
1313 long block = (
long)blk_rq_pos(rq) / (hard_sect >> 9);
1314 unsigned long blocks = blk_rq_sectors(rq) / (hard_sect >> 9);
1316 memset(rq->cmd, 0, BLK_MAX_CDB);
1318 if (rq_data_dir(rq) ==
READ)
1326 rq->cmd[2] = (block >> 24) & 0xff;
1327 rq->cmd[3] = (block >> 16) & 0xff;
1328 rq->cmd[4] = (block >> 8) & 0xff;
1329 rq->cmd[5] = block & 0xff;
1334 rq->cmd[7] = (blocks >> 8) & 0xff;
1335 rq->cmd[8] = blocks & 0xff;
1344 static int ide_cdrom_prep_pc(
struct request *rq)
1368 return BLKPREP_KILL;
1376 if (rq->cmd_type == REQ_TYPE_FS)
1377 return ide_cdrom_prep_fs(q, rq);
1378 else if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
1379 return ide_cdrom_prep_pc(rq);
1390 #ifdef CONFIG_IDE_PROC_FS
1393 unsigned long capacity, sectors_per_frame;
1395 if (cdrom_read_capacity(drive, &capacity, §ors_per_frame,
NULL))
1398 return capacity * sectors_per_frame;
1401 static int idecd_capacity_proc_show(
struct seq_file *
m,
void *
v)
1405 seq_printf(m,
"%llu\n", (
long long)ide_cdrom_capacity(drive));
1416 .open = idecd_capacity_proc_open,
1422 static ide_proc_entry_t idecd_proc[] = {
1427 static ide_proc_entry_t *ide_cd_proc_entries(
ide_drive_t *drive)
1432 static const struct ide_proc_devset *ide_cd_proc_devsets(
ide_drive_t *drive)
1480 static unsigned int ide_cd_flags(
u16 *
id)
1500 u16 *
id = drive->
id;
1514 fw_rev[4] ==
'1' && fw_rev[6] <=
'2')
1518 fw_rev[4] ==
'1' && fw_rev[6] <=
'2')
1524 nslots = ide_cdrom_probe_capabilities(drive);
1528 if (ide_cdrom_register(drive, nslots)) {
1530 " cdrom driver.\n", drive->
name, __func__);
1554 static void ide_cd_release(
struct device *
dev)
1559 struct gendisk *
g = info->
disk;
1564 if (devinfo->
handle == drive)
1568 g->private_data =
NULL;
1575 static struct ide_driver ide_cdrom_driver = {
1578 .name =
"ide-cdrom",
1581 .probe = ide_cd_probe,
1582 .remove = ide_cd_remove,
1584 .do_request = ide_cd_do_request,
1585 #ifdef CONFIG_IDE_PROC_FS
1586 .proc_entries = ide_cd_proc_entries,
1587 .proc_devsets = ide_cd_proc_devsets,
1597 info = ide_cd_get(bdev->
bd_disk);
1609 static int idecd_release(
struct gendisk *disk,
fmode_t mode)
1638 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1655 spindown = buffer[11] & 0x0f;
1656 if (
copy_to_user((
void __user *)arg, &spindown,
sizeof(
char)))
1662 unsigned int cmd,
unsigned long arg)
1669 return idecd_set_spindown(&info->
devinfo, arg);
1671 return idecd_get_spindown(&info->
devinfo, arg);
1684 unsigned int cmd,
unsigned long arg)
1689 ret = idecd_locked_ioctl(bdev, mode, cmd, arg);
1696 static unsigned int idecd_check_events(
struct gendisk *disk,
1697 unsigned int clearing)
1703 static int idecd_revalidate_disk(
struct gendisk *disk)
1713 static const struct block_device_operations idecd_ops = {
1716 .release = idecd_release,
1717 .ioctl = idecd_ioctl,
1718 .check_events = idecd_check_events,
1719 .revalidate_disk = idecd_revalidate_disk
1723 static unsigned long debug_mask;
1760 info->
dev.release = ide_cd_release;
1766 info->
drive = drive;
1767 info->
driver = &ide_cdrom_driver;
1770 g->private_data = &info->
driver;
1775 g->driverfs_dev = &drive->
gendev;
1776 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
1777 if (ide_cdrom_setup(drive)) {
1783 g->fops = &idecd_ops;
1784 g->flags |= GENHD_FL_REMOVABLE | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
1796 static void __exit ide_cdrom_exit(
void)
1801 static int __init ide_cdrom_init(
void)