Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 2 | /* |
| 3 | * cdrom.c IOCTLs handling for ide-cd driver. |
| 4 | * |
| 5 | * Copyright (C) 1994-1996 Scott Snyder <snyder@fnald0.fnal.gov> |
| 6 | * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org> |
| 7 | * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/cdrom.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 13 | #include <linux/ide.h> |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 14 | #include <scsi/scsi.h> |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 15 | |
| 16 | #include "ide-cd.h" |
| 17 | |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 18 | /**************************************************************************** |
| 19 | * Other driver requests (open, close, check media change). |
| 20 | */ |
| 21 | int ide_cdrom_open_real(struct cdrom_device_info *cdi, int purpose) |
| 22 | { |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | /* |
| 27 | * Close down the device. Invalidate all cached blocks. |
| 28 | */ |
| 29 | void ide_cdrom_release_real(struct cdrom_device_info *cdi) |
| 30 | { |
| 31 | ide_drive_t *drive = cdi->handle; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 32 | |
| 33 | if (!cdi->use_count) |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 34 | drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* |
| 38 | * add logic to try GET_EVENT command first to check for media and tray |
| 39 | * status. this should be supported by newer cd-r/w and all DVD etc |
| 40 | * drives |
| 41 | */ |
| 42 | int ide_cdrom_drive_status(struct cdrom_device_info *cdi, int slot_nr) |
| 43 | { |
| 44 | ide_drive_t *drive = cdi->handle; |
| 45 | struct media_event_desc med; |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 46 | struct scsi_sense_hdr sshdr; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 47 | int stat; |
| 48 | |
| 49 | if (slot_nr != CDSL_CURRENT) |
| 50 | return -EINVAL; |
| 51 | |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 52 | stat = cdrom_check_status(drive, &sshdr); |
| 53 | if (!stat || sshdr.sense_key == UNIT_ATTENTION) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 54 | return CDS_DISC_OK; |
| 55 | |
| 56 | if (!cdrom_get_media_event(cdi, &med)) { |
| 57 | if (med.media_present) |
| 58 | return CDS_DISC_OK; |
| 59 | else if (med.door_open) |
| 60 | return CDS_TRAY_OPEN; |
| 61 | else |
| 62 | return CDS_NO_DISC; |
| 63 | } |
| 64 | |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 65 | if (sshdr.sense_key == NOT_READY && sshdr.asc == 0x04 |
| 66 | && sshdr.ascq == 0x04) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 67 | return CDS_DISC_OK; |
| 68 | |
| 69 | /* |
| 70 | * If not using Mt Fuji extended media tray reports, |
| 71 | * just return TRAY_OPEN since ATAPI doesn't provide |
| 72 | * any other way to detect this... |
| 73 | */ |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 74 | if (sshdr.sense_key == NOT_READY) { |
| 75 | if (sshdr.asc == 0x3a && sshdr.ascq == 1) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 76 | return CDS_NO_DISC; |
| 77 | else |
| 78 | return CDS_TRAY_OPEN; |
| 79 | } |
| 80 | return CDS_DRIVE_NOT_READY; |
| 81 | } |
| 82 | |
Tejun Heo | 7eec77a | 2011-04-21 19:43:59 +0200 | [diff] [blame] | 83 | /* |
| 84 | * ide-cd always generates media changed event if media is missing, which |
Martin Wilck | 3c12c8e | 2019-03-27 14:51:03 +0100 | [diff] [blame] | 85 | * makes it impossible to use for proper event reporting, so |
| 86 | * DISK_EVENT_FLAG_UEVENT is cleared in disk->event_flags |
| 87 | * and the following function is used only to trigger |
Tejun Heo | 7eec77a | 2011-04-21 19:43:59 +0200 | [diff] [blame] | 88 | * revalidation and never propagated to userland. |
| 89 | */ |
Tejun Heo | 5b03a1b | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 90 | unsigned int ide_cdrom_check_events_real(struct cdrom_device_info *cdi, |
| 91 | unsigned int clearing, int slot_nr) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 92 | { |
| 93 | ide_drive_t *drive = cdi->handle; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 94 | int retval; |
| 95 | |
| 96 | if (slot_nr == CDSL_CURRENT) { |
| 97 | (void) cdrom_check_status(drive, NULL); |
Bartlomiej Zolnierkiewicz | fe11edf | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 98 | retval = (drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED) ? 1 : 0; |
| 99 | drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED; |
Tejun Heo | 5b03a1b | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 100 | return retval ? DISK_EVENT_MEDIA_CHANGE : 0; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 101 | } else { |
Tejun Heo | 5b03a1b | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 102 | return 0; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | /* Eject the disk if EJECTFLAG is 0. |
| 107 | If EJECTFLAG is 1, try to reload the disk. */ |
| 108 | static |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 109 | int cdrom_eject(ide_drive_t *drive, int ejectflag) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 110 | { |
| 111 | struct cdrom_info *cd = drive->driver_data; |
| 112 | struct cdrom_device_info *cdi = &cd->devinfo; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 113 | char loej = 0x02; |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 114 | unsigned char cmd[BLK_MAX_CDB]; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 115 | |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 116 | if ((drive->atapi_flags & IDE_AFLAG_NO_EJECT) && !ejectflag) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 117 | return -EDRIVE_CANT_DO_THIS; |
| 118 | |
| 119 | /* reload fails on some drives, if the tray is locked */ |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 120 | if ((drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED) && ejectflag) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 121 | return 0; |
| 122 | |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 123 | /* only tell drive to close tray if open, if it can do that */ |
| 124 | if (ejectflag && (cdi->mask & CDC_CLOSE_TRAY)) |
| 125 | loej = 0; |
| 126 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 127 | memset(cmd, 0, BLK_MAX_CDB); |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 128 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 129 | cmd[0] = GPCMD_START_STOP_UNIT; |
| 130 | cmd[4] = loej | (ejectflag != 0); |
| 131 | |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 132 | return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, NULL, 0, 0); |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */ |
| 136 | static |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 137 | int ide_cd_lockdoor(ide_drive_t *drive, int lockflag) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 138 | { |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 139 | struct scsi_sense_hdr sshdr; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 140 | int stat; |
| 141 | |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 142 | /* If the drive cannot lock the door, just pretend. */ |
Bartlomiej Zolnierkiewicz | 42619d3 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 143 | if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0) { |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 144 | stat = 0; |
| 145 | } else { |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 146 | unsigned char cmd[BLK_MAX_CDB]; |
| 147 | |
| 148 | memset(cmd, 0, BLK_MAX_CDB); |
| 149 | |
| 150 | cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL; |
| 151 | cmd[4] = lockflag ? 1 : 0; |
| 152 | |
Harvey Harrison | 1a677ab | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 153 | stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 154 | &sshdr, 0, 0); |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* If we got an illegal field error, the drive |
| 158 | probably cannot lock the door. */ |
| 159 | if (stat != 0 && |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 160 | sshdr.sense_key == ILLEGAL_REQUEST && |
| 161 | (sshdr.asc == 0x24 || sshdr.asc == 0x20)) { |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 162 | printk(KERN_ERR "%s: door locking not supported\n", |
| 163 | drive->name); |
Bartlomiej Zolnierkiewicz | 42619d3 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 164 | drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 165 | stat = 0; |
| 166 | } |
| 167 | |
| 168 | /* no medium, that's alright. */ |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 169 | if (stat != 0 && sshdr.sense_key == NOT_READY && sshdr.asc == 0x3a) |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 170 | stat = 0; |
| 171 | |
| 172 | if (stat == 0) { |
| 173 | if (lockflag) |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 174 | drive->atapi_flags |= IDE_AFLAG_DOOR_LOCKED; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 175 | else |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 176 | drive->atapi_flags &= ~IDE_AFLAG_DOOR_LOCKED; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return stat; |
| 180 | } |
| 181 | |
| 182 | int ide_cdrom_tray_move(struct cdrom_device_info *cdi, int position) |
| 183 | { |
| 184 | ide_drive_t *drive = cdi->handle; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 185 | |
| 186 | if (position) { |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 187 | int stat = ide_cd_lockdoor(drive, 0); |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 188 | |
| 189 | if (stat) |
| 190 | return stat; |
| 191 | } |
| 192 | |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 193 | return cdrom_eject(drive, !position); |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 196 | int ide_cdrom_lock_door(struct cdrom_device_info *cdi, int lock) |
| 197 | { |
| 198 | ide_drive_t *drive = cdi->handle; |
| 199 | |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 200 | return ide_cd_lockdoor(drive, lock); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * ATAPI devices are free to select the speed you request or any slower |
| 205 | * rate. :-( Requesting too fast a speed will _not_ produce an error. |
| 206 | */ |
| 207 | int ide_cdrom_select_speed(struct cdrom_device_info *cdi, int speed) |
| 208 | { |
| 209 | ide_drive_t *drive = cdi->handle; |
| 210 | struct cdrom_info *cd = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 211 | u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE]; |
| 212 | int stat; |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 213 | unsigned char cmd[BLK_MAX_CDB]; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 214 | |
| 215 | if (speed == 0) |
| 216 | speed = 0xffff; /* set to max */ |
| 217 | else |
| 218 | speed *= 177; /* Nx to kbytes/s */ |
| 219 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 220 | memset(cmd, 0, BLK_MAX_CDB); |
| 221 | |
| 222 | cmd[0] = GPCMD_SET_SPEED; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 223 | /* Read Drive speed in kbytes/second MSB/LSB */ |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 224 | cmd[2] = (speed >> 8) & 0xff; |
| 225 | cmd[3] = speed & 0xff; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 226 | if ((cdi->mask & (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) != |
| 227 | (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) { |
| 228 | /* Write Drive speed in kbytes/second MSB/LSB */ |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 229 | cmd[4] = (speed >> 8) & 0xff; |
| 230 | cmd[5] = speed & 0xff; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 233 | stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, NULL, 0, 0); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 234 | |
| 235 | if (!ide_cdrom_get_capabilities(drive, buf)) { |
| 236 | ide_cdrom_update_speed(drive, buf); |
| 237 | cdi->speed = cd->current_speed; |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int ide_cdrom_get_last_session(struct cdrom_device_info *cdi, |
| 244 | struct cdrom_multisession *ms_info) |
| 245 | { |
| 246 | struct atapi_toc *toc; |
| 247 | ide_drive_t *drive = cdi->handle; |
| 248 | struct cdrom_info *info = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 249 | int ret; |
| 250 | |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 251 | if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0 || !info->toc) { |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 252 | ret = ide_cd_read_toc(drive); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 253 | if (ret) |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | toc = info->toc; |
| 258 | ms_info->addr.lba = toc->last_session_lba; |
| 259 | ms_info->xa_flag = toc->xa_flag; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | int ide_cdrom_get_mcn(struct cdrom_device_info *cdi, |
| 265 | struct cdrom_mcn *mcn_info) |
| 266 | { |
| 267 | ide_drive_t *drive = cdi->handle; |
| 268 | int stat, mcnlen; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 269 | char buf[24]; |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 270 | unsigned char cmd[BLK_MAX_CDB]; |
| 271 | unsigned len = sizeof(buf); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 272 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 273 | memset(cmd, 0, BLK_MAX_CDB); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 274 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 275 | cmd[0] = GPCMD_READ_SUBCHANNEL; |
| 276 | cmd[1] = 2; /* MSF addressing */ |
| 277 | cmd[2] = 0x40; /* request subQ data */ |
| 278 | cmd[3] = 2; /* format */ |
| 279 | cmd[8] = len; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 280 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 281 | stat = ide_cd_queue_pc(drive, cmd, 0, buf, &len, NULL, 0, 0); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 282 | if (stat) |
| 283 | return stat; |
| 284 | |
| 285 | mcnlen = sizeof(mcn_info->medium_catalog_number) - 1; |
| 286 | memcpy(mcn_info->medium_catalog_number, buf + 9, mcnlen); |
| 287 | mcn_info->medium_catalog_number[mcnlen] = '\0'; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int ide_cdrom_reset(struct cdrom_device_info *cdi) |
| 293 | { |
| 294 | ide_drive_t *drive = cdi->handle; |
| 295 | struct cdrom_info *cd = drive->driver_data; |
FUJITA Tomonori | 0ef4c4d | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 296 | struct request *rq; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 297 | int ret; |
| 298 | |
Christoph Hellwig | ff005a0 | 2018-05-09 09:54:05 +0200 | [diff] [blame] | 299 | rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0); |
Christoph Hellwig | 2f5a8e80 | 2017-01-31 16:57:30 +0100 | [diff] [blame] | 300 | ide_req(rq)->type = ATA_PRIV_MISC; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 301 | rq->rq_flags = RQF_QUIET; |
Christoph Hellwig | b7819b9 | 2017-04-20 16:02:55 +0200 | [diff] [blame] | 302 | blk_execute_rq(drive->queue, cd->disk, rq, 0); |
Christoph Hellwig | 17d5363 | 2017-04-20 16:03:01 +0200 | [diff] [blame] | 303 | ret = scsi_req(rq)->result ? -EIO : 0; |
FUJITA Tomonori | 0ef4c4d | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 304 | blk_put_request(rq); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 305 | /* |
| 306 | * A reset will unlock the door. If it was previously locked, |
| 307 | * lock it again. |
| 308 | */ |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 309 | if (drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED) |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 310 | (void)ide_cd_lockdoor(drive, 1); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 311 | |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static int ide_cd_get_toc_entry(ide_drive_t *drive, int track, |
| 316 | struct atapi_toc_entry **ent) |
| 317 | { |
| 318 | struct cdrom_info *info = drive->driver_data; |
| 319 | struct atapi_toc *toc = info->toc; |
| 320 | int ntracks; |
| 321 | |
| 322 | /* |
| 323 | * don't serve cached data, if the toc isn't valid |
| 324 | */ |
Borislav Petkov | 570f89e | 2008-07-23 19:56:02 +0200 | [diff] [blame] | 325 | if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0) |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | |
| 328 | /* Check validity of requested track number. */ |
| 329 | ntracks = toc->hdr.last_track - toc->hdr.first_track + 1; |
| 330 | |
| 331 | if (toc->hdr.first_track == CDROM_LEADOUT) |
| 332 | ntracks = 0; |
| 333 | |
| 334 | if (track == CDROM_LEADOUT) |
| 335 | *ent = &toc->ent[ntracks]; |
| 336 | else if (track < toc->hdr.first_track || track > toc->hdr.last_track) |
| 337 | return -EINVAL; |
| 338 | else |
| 339 | *ent = &toc->ent[track - toc->hdr.first_track]; |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int ide_cd_fake_play_trkind(ide_drive_t *drive, void *arg) |
| 345 | { |
| 346 | struct cdrom_ti *ti = arg; |
| 347 | struct atapi_toc_entry *first_toc, *last_toc; |
| 348 | unsigned long lba_start, lba_end; |
| 349 | int stat; |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 350 | unsigned char cmd[BLK_MAX_CDB]; |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 351 | |
| 352 | stat = ide_cd_get_toc_entry(drive, ti->cdti_trk0, &first_toc); |
| 353 | if (stat) |
| 354 | return stat; |
| 355 | |
| 356 | stat = ide_cd_get_toc_entry(drive, ti->cdti_trk1, &last_toc); |
| 357 | if (stat) |
| 358 | return stat; |
| 359 | |
| 360 | if (ti->cdti_trk1 != CDROM_LEADOUT) |
| 361 | ++last_toc; |
| 362 | lba_start = first_toc->addr.lba; |
| 363 | lba_end = last_toc->addr.lba; |
| 364 | |
| 365 | if (lba_end <= lba_start) |
| 366 | return -EINVAL; |
| 367 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 368 | memset(cmd, 0, BLK_MAX_CDB); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 369 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 370 | cmd[0] = GPCMD_PLAY_AUDIO_MSF; |
| 371 | lba_to_msf(lba_start, &cmd[3], &cmd[4], &cmd[5]); |
| 372 | lba_to_msf(lba_end - 1, &cmd[6], &cmd[7], &cmd[8]); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 373 | |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 374 | return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, NULL, 0, 0); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int ide_cd_read_tochdr(ide_drive_t *drive, void *arg) |
| 378 | { |
| 379 | struct cdrom_info *cd = drive->driver_data; |
| 380 | struct cdrom_tochdr *tochdr = arg; |
| 381 | struct atapi_toc *toc; |
| 382 | int stat; |
| 383 | |
| 384 | /* Make sure our saved TOC is valid. */ |
Kees Cook | 54f8a7a | 2018-07-31 12:51:46 -0700 | [diff] [blame] | 385 | stat = ide_cd_read_toc(drive); |
Bartlomiej Zolnierkiewicz | 1780299 | 2008-02-01 23:09:25 +0100 | [diff] [blame] | 386 | if (stat) |
| 387 | return stat; |
| 388 | |
| 389 | toc = cd->toc; |
| 390 | tochdr->cdth_trk0 = toc->hdr.first_track; |
| 391 | tochdr->cdth_trk1 = toc->hdr.last_track; |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int ide_cd_read_tocentry(ide_drive_t *drive, void *arg) |
| 397 | { |
| 398 | struct cdrom_tocentry *tocentry = arg; |
| 399 | struct atapi_toc_entry *toce; |
| 400 | int stat; |
| 401 | |
| 402 | stat = ide_cd_get_toc_entry(drive, tocentry->cdte_track, &toce); |
| 403 | if (stat) |
| 404 | return stat; |
| 405 | |
| 406 | tocentry->cdte_ctrl = toce->control; |
| 407 | tocentry->cdte_adr = toce->adr; |
| 408 | if (tocentry->cdte_format == CDROM_MSF) { |
| 409 | lba_to_msf(toce->addr.lba, |
| 410 | &tocentry->cdte_addr.msf.minute, |
| 411 | &tocentry->cdte_addr.msf.second, |
| 412 | &tocentry->cdte_addr.msf.frame); |
| 413 | } else |
| 414 | tocentry->cdte_addr.lba = toce->addr.lba; |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | int ide_cdrom_audio_ioctl(struct cdrom_device_info *cdi, |
| 420 | unsigned int cmd, void *arg) |
| 421 | { |
| 422 | ide_drive_t *drive = cdi->handle; |
| 423 | |
| 424 | switch (cmd) { |
| 425 | /* |
| 426 | * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since |
| 427 | * atapi doesn't support it |
| 428 | */ |
| 429 | case CDROMPLAYTRKIND: |
| 430 | return ide_cd_fake_play_trkind(drive, arg); |
| 431 | case CDROMREADTOCHDR: |
| 432 | return ide_cd_read_tochdr(drive, arg); |
| 433 | case CDROMREADTOCENTRY: |
| 434 | return ide_cd_read_tocentry(drive, arg); |
| 435 | default: |
| 436 | return -EINVAL; |
| 437 | } |
| 438 | } |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 439 | |
| 440 | /* the generic packet interface to cdrom.c */ |
| 441 | int ide_cdrom_packet(struct cdrom_device_info *cdi, |
| 442 | struct packet_command *cgc) |
| 443 | { |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 444 | ide_drive_t *drive = cdi->handle; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 445 | req_flags_t flags = 0; |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 446 | unsigned len = cgc->buflen; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 447 | |
| 448 | if (cgc->timeout <= 0) |
| 449 | cgc->timeout = ATAPI_WAIT_PC; |
| 450 | |
| 451 | /* here we queue the commands from the uniform CD-ROM |
| 452 | layer. the packet must be complete, as we do not |
| 453 | touch it at all. */ |
Bartlomiej Zolnierkiewicz | b348487 | 2008-03-07 21:53:49 +0100 | [diff] [blame] | 454 | |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 455 | if (cgc->sshdr) |
| 456 | memset(cgc->sshdr, 0, sizeof(*cgc->sshdr)); |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 457 | |
| 458 | if (cgc->quiet) |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 459 | flags |= RQF_QUIET; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 460 | |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 461 | cgc->stat = ide_cd_queue_pc(drive, cgc->cmd, |
| 462 | cgc->data_direction == CGC_DATA_WRITE, |
| 463 | cgc->buffer, &len, |
Kees Cook | e7d0748 | 2018-08-02 15:22:13 -0600 | [diff] [blame] | 464 | cgc->sshdr, cgc->timeout, flags); |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 465 | if (!cgc->stat) |
FUJITA Tomonori | 5f82854 | 2008-07-15 21:21:42 +0200 | [diff] [blame] | 466 | cgc->buflen -= len; |
Borislav Petkov | f9afd18 | 2008-02-01 23:09:29 +0100 | [diff] [blame] | 467 | return cgc->stat; |
| 468 | } |