1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds #include <linux/kernel.h>
31da177e4SLinus Torvalds #include <linux/mm.h>
41da177e4SLinus Torvalds #include <linux/fs.h>
51da177e4SLinus Torvalds #include <linux/errno.h>
61da177e4SLinus Torvalds #include <linux/string.h>
71da177e4SLinus Torvalds #include <linux/blkdev.h>
8acf3368fSPaul Gortmaker #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/blkpg.h>
101da177e4SLinus Torvalds #include <linux/cdrom.h>
111da177e4SLinus Torvalds #include <linux/delay.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
131da177e4SLinus Torvalds #include <asm/io.h>
147c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
151da177e4SLinus Torvalds
161da177e4SLinus Torvalds #include <scsi/scsi.h>
171da177e4SLinus Torvalds #include <scsi/scsi_dbg.h>
181da177e4SLinus Torvalds #include <scsi/scsi_device.h>
191da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
201da177e4SLinus Torvalds #include <scsi/scsi_host.h>
211da177e4SLinus Torvalds #include <scsi/scsi_ioctl.h>
22820732b5SJames Bottomley #include <scsi/scsi_cmnd.h>
231da177e4SLinus Torvalds
241da177e4SLinus Torvalds #include "sr.h"
251da177e4SLinus Torvalds
261da177e4SLinus Torvalds #if 0
271da177e4SLinus Torvalds #define DEBUG
281da177e4SLinus Torvalds #endif
291da177e4SLinus Torvalds
301da177e4SLinus Torvalds /* The sr_is_xa() seems to trigger firmware bugs with some drives :-(
311da177e4SLinus Torvalds * It is off by default and can be turned on with this module parameter */
321da177e4SLinus Torvalds static int xa_test = 0;
331da177e4SLinus Torvalds
341da177e4SLinus Torvalds module_param(xa_test, int, S_IRUGO | S_IWUSR);
351da177e4SLinus Torvalds
sr_read_tochdr(struct cdrom_device_info * cdi,struct cdrom_tochdr * tochdr)36a09c6311SChristoph Hellwig static int sr_read_tochdr(struct cdrom_device_info *cdi,
37a09c6311SChristoph Hellwig struct cdrom_tochdr *tochdr)
38a09c6311SChristoph Hellwig {
39a09c6311SChristoph Hellwig struct scsi_cd *cd = cdi->handle;
40a09c6311SChristoph Hellwig struct packet_command cgc;
41a09c6311SChristoph Hellwig int result;
42a09c6311SChristoph Hellwig unsigned char *buffer;
43a09c6311SChristoph Hellwig
44faad6cebSTom Rix buffer = kzalloc(32, GFP_KERNEL);
45a09c6311SChristoph Hellwig if (!buffer)
46a09c6311SChristoph Hellwig return -ENOMEM;
47a09c6311SChristoph Hellwig
48a09c6311SChristoph Hellwig memset(&cgc, 0, sizeof(struct packet_command));
49a09c6311SChristoph Hellwig cgc.timeout = IOCTL_TIMEOUT;
50a09c6311SChristoph Hellwig cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
51a09c6311SChristoph Hellwig cgc.cmd[8] = 12; /* LSB of length */
52a09c6311SChristoph Hellwig cgc.buffer = buffer;
53a09c6311SChristoph Hellwig cgc.buflen = 12;
54a09c6311SChristoph Hellwig cgc.quiet = 1;
55a09c6311SChristoph Hellwig cgc.data_direction = DMA_FROM_DEVICE;
56a09c6311SChristoph Hellwig
57a09c6311SChristoph Hellwig result = sr_do_ioctl(cd, &cgc);
58faad6cebSTom Rix if (result)
59faad6cebSTom Rix goto err;
60a09c6311SChristoph Hellwig
61a09c6311SChristoph Hellwig tochdr->cdth_trk0 = buffer[2];
62a09c6311SChristoph Hellwig tochdr->cdth_trk1 = buffer[3];
63a09c6311SChristoph Hellwig
64faad6cebSTom Rix err:
65a09c6311SChristoph Hellwig kfree(buffer);
66a09c6311SChristoph Hellwig return result;
67a09c6311SChristoph Hellwig }
68a09c6311SChristoph Hellwig
sr_read_tocentry(struct cdrom_device_info * cdi,struct cdrom_tocentry * tocentry)69a09c6311SChristoph Hellwig static int sr_read_tocentry(struct cdrom_device_info *cdi,
70a09c6311SChristoph Hellwig struct cdrom_tocentry *tocentry)
71a09c6311SChristoph Hellwig {
72a09c6311SChristoph Hellwig struct scsi_cd *cd = cdi->handle;
73a09c6311SChristoph Hellwig struct packet_command cgc;
74a09c6311SChristoph Hellwig int result;
75a09c6311SChristoph Hellwig unsigned char *buffer;
76a09c6311SChristoph Hellwig
77faad6cebSTom Rix buffer = kzalloc(32, GFP_KERNEL);
78a09c6311SChristoph Hellwig if (!buffer)
79a09c6311SChristoph Hellwig return -ENOMEM;
80a09c6311SChristoph Hellwig
81a09c6311SChristoph Hellwig memset(&cgc, 0, sizeof(struct packet_command));
82a09c6311SChristoph Hellwig cgc.timeout = IOCTL_TIMEOUT;
83a09c6311SChristoph Hellwig cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
84a09c6311SChristoph Hellwig cgc.cmd[1] |= (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0;
85a09c6311SChristoph Hellwig cgc.cmd[6] = tocentry->cdte_track;
86a09c6311SChristoph Hellwig cgc.cmd[8] = 12; /* LSB of length */
87a09c6311SChristoph Hellwig cgc.buffer = buffer;
88a09c6311SChristoph Hellwig cgc.buflen = 12;
89a09c6311SChristoph Hellwig cgc.data_direction = DMA_FROM_DEVICE;
90a09c6311SChristoph Hellwig
91a09c6311SChristoph Hellwig result = sr_do_ioctl(cd, &cgc);
92faad6cebSTom Rix if (result)
93faad6cebSTom Rix goto err;
94a09c6311SChristoph Hellwig
95a09c6311SChristoph Hellwig tocentry->cdte_ctrl = buffer[5] & 0xf;
96a09c6311SChristoph Hellwig tocentry->cdte_adr = buffer[5] >> 4;
97a09c6311SChristoph Hellwig tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
98a09c6311SChristoph Hellwig if (tocentry->cdte_format == CDROM_MSF) {
99a09c6311SChristoph Hellwig tocentry->cdte_addr.msf.minute = buffer[9];
100a09c6311SChristoph Hellwig tocentry->cdte_addr.msf.second = buffer[10];
101a09c6311SChristoph Hellwig tocentry->cdte_addr.msf.frame = buffer[11];
102a09c6311SChristoph Hellwig } else
103a09c6311SChristoph Hellwig tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
104a09c6311SChristoph Hellwig + buffer[10]) << 8) + buffer[11];
105a09c6311SChristoph Hellwig
106faad6cebSTom Rix err:
107a09c6311SChristoph Hellwig kfree(buffer);
108a09c6311SChristoph Hellwig return result;
109a09c6311SChristoph Hellwig }
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds #define IOCTL_RETRIES 3
1121da177e4SLinus Torvalds
1131da177e4SLinus Torvalds /* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command. When these drives
1141da177e4SLinus Torvalds are emulating a SCSI device via the idescsi module, they need to have
1151da177e4SLinus Torvalds CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
1161da177e4SLinus Torvalds
sr_fake_playtrkind(struct cdrom_device_info * cdi,struct cdrom_ti * ti)1171da177e4SLinus Torvalds static int sr_fake_playtrkind(struct cdrom_device_info *cdi, struct cdrom_ti *ti)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds struct cdrom_tocentry trk0_te, trk1_te;
1201da177e4SLinus Torvalds struct cdrom_tochdr tochdr;
1211da177e4SLinus Torvalds struct packet_command cgc;
1221da177e4SLinus Torvalds int ntracks, ret;
1231da177e4SLinus Torvalds
124a09c6311SChristoph Hellwig ret = sr_read_tochdr(cdi, &tochdr);
125a09c6311SChristoph Hellwig if (ret)
1261da177e4SLinus Torvalds return ret;
1271da177e4SLinus Torvalds
1281da177e4SLinus Torvalds ntracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
1291da177e4SLinus Torvalds
1301da177e4SLinus Torvalds if (ti->cdti_trk1 == ntracks)
1311da177e4SLinus Torvalds ti->cdti_trk1 = CDROM_LEADOUT;
1321da177e4SLinus Torvalds else if (ti->cdti_trk1 != CDROM_LEADOUT)
1331da177e4SLinus Torvalds ti->cdti_trk1 ++;
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds trk0_te.cdte_track = ti->cdti_trk0;
1361da177e4SLinus Torvalds trk0_te.cdte_format = CDROM_MSF;
1371da177e4SLinus Torvalds trk1_te.cdte_track = ti->cdti_trk1;
1381da177e4SLinus Torvalds trk1_te.cdte_format = CDROM_MSF;
1391da177e4SLinus Torvalds
140a09c6311SChristoph Hellwig ret = sr_read_tocentry(cdi, &trk0_te);
141a09c6311SChristoph Hellwig if (ret)
1421da177e4SLinus Torvalds return ret;
143a09c6311SChristoph Hellwig ret = sr_read_tocentry(cdi, &trk1_te);
144a09c6311SChristoph Hellwig if (ret)
1451da177e4SLinus Torvalds return ret;
1461da177e4SLinus Torvalds
1471da177e4SLinus Torvalds memset(&cgc, 0, sizeof(struct packet_command));
1481da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
1491da177e4SLinus Torvalds cgc.cmd[3] = trk0_te.cdte_addr.msf.minute;
1501da177e4SLinus Torvalds cgc.cmd[4] = trk0_te.cdte_addr.msf.second;
1511da177e4SLinus Torvalds cgc.cmd[5] = trk0_te.cdte_addr.msf.frame;
1521da177e4SLinus Torvalds cgc.cmd[6] = trk1_te.cdte_addr.msf.minute;
1531da177e4SLinus Torvalds cgc.cmd[7] = trk1_te.cdte_addr.msf.second;
1541da177e4SLinus Torvalds cgc.cmd[8] = trk1_te.cdte_addr.msf.frame;
1551da177e4SLinus Torvalds cgc.data_direction = DMA_NONE;
1561da177e4SLinus Torvalds cgc.timeout = IOCTL_TIMEOUT;
1571da177e4SLinus Torvalds return sr_do_ioctl(cdi->handle, &cgc);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds
sr_play_trkind(struct cdrom_device_info * cdi,struct cdrom_ti * ti)160a09c6311SChristoph Hellwig static int sr_play_trkind(struct cdrom_device_info *cdi,
161a09c6311SChristoph Hellwig struct cdrom_ti *ti)
162a09c6311SChristoph Hellwig
163a09c6311SChristoph Hellwig {
164a09c6311SChristoph Hellwig struct scsi_cd *cd = cdi->handle;
165a09c6311SChristoph Hellwig struct packet_command cgc;
166a09c6311SChristoph Hellwig int result;
167a09c6311SChristoph Hellwig
168a09c6311SChristoph Hellwig memset(&cgc, 0, sizeof(struct packet_command));
169a09c6311SChristoph Hellwig cgc.timeout = IOCTL_TIMEOUT;
170a09c6311SChristoph Hellwig cgc.cmd[0] = GPCMD_PLAYAUDIO_TI;
171a09c6311SChristoph Hellwig cgc.cmd[4] = ti->cdti_trk0;
172a09c6311SChristoph Hellwig cgc.cmd[5] = ti->cdti_ind0;
173a09c6311SChristoph Hellwig cgc.cmd[7] = ti->cdti_trk1;
174a09c6311SChristoph Hellwig cgc.cmd[8] = ti->cdti_ind1;
175a09c6311SChristoph Hellwig cgc.data_direction = DMA_NONE;
176a09c6311SChristoph Hellwig
177a09c6311SChristoph Hellwig result = sr_do_ioctl(cd, &cgc);
178a09c6311SChristoph Hellwig if (result == -EDRIVE_CANT_DO_THIS)
179a09c6311SChristoph Hellwig result = sr_fake_playtrkind(cdi, ti);
180a09c6311SChristoph Hellwig
181a09c6311SChristoph Hellwig return result;
182a09c6311SChristoph Hellwig }
183a09c6311SChristoph Hellwig
1841da177e4SLinus Torvalds /* We do our own retries because we want to know what the specific
1851da177e4SLinus Torvalds error code is. Normally the UNIT_ATTENTION code will automatically
1861da177e4SLinus Torvalds clear after one error */
1871da177e4SLinus Torvalds
sr_do_ioctl(Scsi_CD * cd,struct packet_command * cgc)1881da177e4SLinus Torvalds int sr_do_ioctl(Scsi_CD *cd, struct packet_command *cgc)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds struct scsi_device *SDev;
19149d33b62SMike Christie struct scsi_sense_hdr local_sshdr, *sshdr;
1921da177e4SLinus Torvalds int result, err = 0, retries = 0;
19349d33b62SMike Christie const struct scsi_exec_args exec_args = {
19449d33b62SMike Christie .sshdr = cgc->sshdr ? : &local_sshdr,
19549d33b62SMike Christie };
1961da177e4SLinus Torvalds
1971da177e4SLinus Torvalds SDev = cd->device;
198820732b5SJames Bottomley
19949d33b62SMike Christie sshdr = exec_args.sshdr;
200f7068114SJens Axboe
2011da177e4SLinus Torvalds retry:
2021da177e4SLinus Torvalds if (!scsi_block_when_processing_errors(SDev)) {
2031da177e4SLinus Torvalds err = -ENODEV;
204820732b5SJames Bottomley goto out;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds
20749d33b62SMike Christie result = scsi_execute_cmd(SDev, cgc->cmd,
20849d33b62SMike Christie cgc->data_direction == DMA_TO_DEVICE ?
20949d33b62SMike Christie REQ_OP_DRV_OUT : REQ_OP_DRV_IN, cgc->buffer,
21049d33b62SMike Christie cgc->buflen, cgc->timeout, IOCTL_RETRIES,
21149d33b62SMike Christie &exec_args);
2121da177e4SLinus Torvalds /* Minimal error checking. Ignore cases we know about, and report the rest. */
213ced202f7SHannes Reinecke if (result < 0) {
214ced202f7SHannes Reinecke err = result;
215ced202f7SHannes Reinecke goto out;
216ced202f7SHannes Reinecke }
2173d45cefcSHannes Reinecke if (scsi_status_is_check_condition(result)) {
218e7d0748dSKees Cook switch (sshdr->sense_key) {
2191da177e4SLinus Torvalds case UNIT_ATTENTION:
2201da177e4SLinus Torvalds SDev->changed = 1;
2211da177e4SLinus Torvalds if (!cgc->quiet)
22296eefad2SHannes Reinecke sr_printk(KERN_INFO, cd,
22396eefad2SHannes Reinecke "disc change detected.\n");
2241da177e4SLinus Torvalds if (retries++ < 10)
2251da177e4SLinus Torvalds goto retry;
2261da177e4SLinus Torvalds err = -ENOMEDIUM;
2271da177e4SLinus Torvalds break;
2281da177e4SLinus Torvalds case NOT_READY: /* This happens if there is no disc in drive */
229e7d0748dSKees Cook if (sshdr->asc == 0x04 &&
230e7d0748dSKees Cook sshdr->ascq == 0x01) {
2311da177e4SLinus Torvalds /* sense: Logical unit is in process of becoming ready */
2321da177e4SLinus Torvalds if (!cgc->quiet)
23396eefad2SHannes Reinecke sr_printk(KERN_INFO, cd,
23496eefad2SHannes Reinecke "CDROM not ready yet.\n");
2351da177e4SLinus Torvalds if (retries++ < 10) {
2361da177e4SLinus Torvalds /* sleep 2 sec and try again */
2371da177e4SLinus Torvalds ssleep(2);
2381da177e4SLinus Torvalds goto retry;
2391da177e4SLinus Torvalds } else {
2401da177e4SLinus Torvalds /* 20 secs are enough? */
2411da177e4SLinus Torvalds err = -ENOMEDIUM;
2421da177e4SLinus Torvalds break;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds if (!cgc->quiet)
24696eefad2SHannes Reinecke sr_printk(KERN_INFO, cd,
24796eefad2SHannes Reinecke "CDROM not ready. Make sure there "
24896eefad2SHannes Reinecke "is a disc in the drive.\n");
2491da177e4SLinus Torvalds err = -ENOMEDIUM;
2501da177e4SLinus Torvalds break;
2511da177e4SLinus Torvalds case ILLEGAL_REQUEST:
2521da177e4SLinus Torvalds err = -EIO;
253e7d0748dSKees Cook if (sshdr->asc == 0x20 &&
254e7d0748dSKees Cook sshdr->ascq == 0x00)
2551da177e4SLinus Torvalds /* sense: Invalid command operation code */
2561da177e4SLinus Torvalds err = -EDRIVE_CANT_DO_THIS;
2571da177e4SLinus Torvalds break;
2581da177e4SLinus Torvalds default:
2591da177e4SLinus Torvalds err = -EIO;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds
2631da177e4SLinus Torvalds /* Wake up a process waiting for device */
2641da177e4SLinus Torvalds out:
2651da177e4SLinus Torvalds cgc->stat = err;
2661da177e4SLinus Torvalds return err;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds /* ---------------------------------------------------------------------- */
2701da177e4SLinus Torvalds /* interface to cdrom.c */
2711da177e4SLinus Torvalds
sr_tray_move(struct cdrom_device_info * cdi,int pos)2721da177e4SLinus Torvalds int sr_tray_move(struct cdrom_device_info *cdi, int pos)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds Scsi_CD *cd = cdi->handle;
2751da177e4SLinus Torvalds struct packet_command cgc;
2761da177e4SLinus Torvalds
2771da177e4SLinus Torvalds memset(&cgc, 0, sizeof(struct packet_command));
2781da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_START_STOP_UNIT;
2791da177e4SLinus Torvalds cgc.cmd[4] = (pos == 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
2801da177e4SLinus Torvalds cgc.data_direction = DMA_NONE;
2811da177e4SLinus Torvalds cgc.timeout = IOCTL_TIMEOUT;
2821da177e4SLinus Torvalds return sr_do_ioctl(cd, &cgc);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds
sr_lock_door(struct cdrom_device_info * cdi,int lock)2851da177e4SLinus Torvalds int sr_lock_door(struct cdrom_device_info *cdi, int lock)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds Scsi_CD *cd = cdi->handle;
2881da177e4SLinus Torvalds
2891da177e4SLinus Torvalds return scsi_set_medium_removal(cd->device, lock ?
2901da177e4SLinus Torvalds SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds
sr_drive_status(struct cdrom_device_info * cdi,int slot)2931da177e4SLinus Torvalds int sr_drive_status(struct cdrom_device_info *cdi, int slot)
2941da177e4SLinus Torvalds {
295210ba1d1SJames Bottomley struct scsi_cd *cd = cdi->handle;
296210ba1d1SJames Bottomley struct scsi_sense_hdr sshdr;
297210ba1d1SJames Bottomley struct media_event_desc med;
298210ba1d1SJames Bottomley
2991da177e4SLinus Torvalds if (CDSL_CURRENT != slot) {
3001da177e4SLinus Torvalds /* we have no changer support */
3011da177e4SLinus Torvalds return -EINVAL;
3021da177e4SLinus Torvalds }
3039f8a2c23STejun Heo if (!scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
3041da177e4SLinus Torvalds return CDS_DISC_OK;
3051da177e4SLinus Torvalds
30696bcc722SPeter Jones /* SK/ASC/ASCQ of 2/4/1 means "unit is becoming ready" */
30796bcc722SPeter Jones if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
30896bcc722SPeter Jones && sshdr.asc == 0x04 && sshdr.ascq == 0x01)
30996bcc722SPeter Jones return CDS_DRIVE_NOT_READY;
31096bcc722SPeter Jones
311210ba1d1SJames Bottomley if (!cdrom_get_media_event(cdi, &med)) {
312210ba1d1SJames Bottomley if (med.media_present)
313210ba1d1SJames Bottomley return CDS_DISC_OK;
314210ba1d1SJames Bottomley else if (med.door_open)
3151da177e4SLinus Torvalds return CDS_TRAY_OPEN;
316210ba1d1SJames Bottomley else
317210ba1d1SJames Bottomley return CDS_NO_DISC;
318210ba1d1SJames Bottomley }
319210ba1d1SJames Bottomley
320210ba1d1SJames Bottomley /*
321b8479178SRobert Jennings * SK/ASC/ASCQ of 2/4/2 means "initialization required"
322b8479178SRobert Jennings * Using CD_TRAY_OPEN results in an START_STOP_UNIT to close
323b8479178SRobert Jennings * the tray, which resolves the initialization requirement.
324b8479178SRobert Jennings */
325b8479178SRobert Jennings if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
326b8479178SRobert Jennings && sshdr.asc == 0x04 && sshdr.ascq == 0x02)
327b8479178SRobert Jennings return CDS_TRAY_OPEN;
328b8479178SRobert Jennings
329b8479178SRobert Jennings /*
330210ba1d1SJames Bottomley * 0x04 is format in progress .. but there must be a disc present!
331210ba1d1SJames Bottomley */
332210ba1d1SJames Bottomley if (sshdr.sense_key == NOT_READY && sshdr.asc == 0x04)
333210ba1d1SJames Bottomley return CDS_DISC_OK;
334210ba1d1SJames Bottomley
335210ba1d1SJames Bottomley /*
336210ba1d1SJames Bottomley * If not using Mt Fuji extended media tray reports,
337210ba1d1SJames Bottomley * just return TRAY_OPEN since ATAPI doesn't provide
338210ba1d1SJames Bottomley * any other way to detect this...
339210ba1d1SJames Bottomley */
340210ba1d1SJames Bottomley if (scsi_sense_valid(&sshdr) &&
341210ba1d1SJames Bottomley /* 0x3a is medium not present */
342210ba1d1SJames Bottomley sshdr.asc == 0x3a)
343210ba1d1SJames Bottomley return CDS_NO_DISC;
344210ba1d1SJames Bottomley else
345210ba1d1SJames Bottomley return CDS_TRAY_OPEN;
346210ba1d1SJames Bottomley
347210ba1d1SJames Bottomley return CDS_DRIVE_NOT_READY;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds
sr_disk_status(struct cdrom_device_info * cdi)3501da177e4SLinus Torvalds int sr_disk_status(struct cdrom_device_info *cdi)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds Scsi_CD *cd = cdi->handle;
3531da177e4SLinus Torvalds struct cdrom_tochdr toc_h;
3541da177e4SLinus Torvalds struct cdrom_tocentry toc_e;
3551da177e4SLinus Torvalds int i, rc, have_datatracks = 0;
3561da177e4SLinus Torvalds
3571da177e4SLinus Torvalds /* look for data tracks */
358a09c6311SChristoph Hellwig rc = sr_read_tochdr(cdi, &toc_h);
359a09c6311SChristoph Hellwig if (rc)
3601da177e4SLinus Torvalds return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
3611da177e4SLinus Torvalds
3621da177e4SLinus Torvalds for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
3631da177e4SLinus Torvalds toc_e.cdte_track = i;
3641da177e4SLinus Torvalds toc_e.cdte_format = CDROM_LBA;
365a09c6311SChristoph Hellwig if (sr_read_tocentry(cdi, &toc_e))
3661da177e4SLinus Torvalds return CDS_NO_INFO;
3671da177e4SLinus Torvalds if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
3681da177e4SLinus Torvalds have_datatracks = 1;
3691da177e4SLinus Torvalds break;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds if (!have_datatracks)
3731da177e4SLinus Torvalds return CDS_AUDIO;
3741da177e4SLinus Torvalds
3751da177e4SLinus Torvalds if (cd->xa_flag)
3761da177e4SLinus Torvalds return CDS_XA_2_1;
3771da177e4SLinus Torvalds else
3781da177e4SLinus Torvalds return CDS_DATA_1;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds
sr_get_last_session(struct cdrom_device_info * cdi,struct cdrom_multisession * ms_info)3811da177e4SLinus Torvalds int sr_get_last_session(struct cdrom_device_info *cdi,
3821da177e4SLinus Torvalds struct cdrom_multisession *ms_info)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds Scsi_CD *cd = cdi->handle;
3851da177e4SLinus Torvalds
3861da177e4SLinus Torvalds ms_info->addr.lba = cd->ms_offset;
3871da177e4SLinus Torvalds ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
3881da177e4SLinus Torvalds
3891da177e4SLinus Torvalds return 0;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds
sr_get_mcn(struct cdrom_device_info * cdi,struct cdrom_mcn * mcn)3921da177e4SLinus Torvalds int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds Scsi_CD *cd = cdi->handle;
3951da177e4SLinus Torvalds struct packet_command cgc;
396faad6cebSTom Rix char *buffer = kzalloc(32, GFP_KERNEL);
3971da177e4SLinus Torvalds int result;
3981da177e4SLinus Torvalds
3993a73e8c7SNate Dailey if (!buffer)
4003a73e8c7SNate Dailey return -ENOMEM;
4013a73e8c7SNate Dailey
4021da177e4SLinus Torvalds memset(&cgc, 0, sizeof(struct packet_command));
4031da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
4041da177e4SLinus Torvalds cgc.cmd[2] = 0x40; /* I do want the subchannel info */
4051da177e4SLinus Torvalds cgc.cmd[3] = 0x02; /* Give me medium catalog number info */
4061da177e4SLinus Torvalds cgc.cmd[8] = 24;
4071da177e4SLinus Torvalds cgc.buffer = buffer;
4081da177e4SLinus Torvalds cgc.buflen = 24;
4091da177e4SLinus Torvalds cgc.data_direction = DMA_FROM_DEVICE;
4101da177e4SLinus Torvalds cgc.timeout = IOCTL_TIMEOUT;
4111da177e4SLinus Torvalds result = sr_do_ioctl(cd, &cgc);
412faad6cebSTom Rix if (result)
413faad6cebSTom Rix goto err;
4141da177e4SLinus Torvalds
4151da177e4SLinus Torvalds memcpy(mcn->medium_catalog_number, buffer + 9, 13);
4161da177e4SLinus Torvalds mcn->medium_catalog_number[13] = 0;
4171da177e4SLinus Torvalds
418faad6cebSTom Rix err:
4191da177e4SLinus Torvalds kfree(buffer);
4201da177e4SLinus Torvalds return result;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds
sr_reset(struct cdrom_device_info * cdi)4231da177e4SLinus Torvalds int sr_reset(struct cdrom_device_info *cdi)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds return 0;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds
sr_select_speed(struct cdrom_device_info * cdi,unsigned long speed)42874fac04eSJustin Stitt int sr_select_speed(struct cdrom_device_info *cdi, unsigned long speed)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds Scsi_CD *cd = cdi->handle;
4311da177e4SLinus Torvalds struct packet_command cgc;
4321da177e4SLinus Torvalds
43374fac04eSJustin Stitt /* avoid exceeding the max speed or overflowing integer bounds */
434*6c0cf602SLinus Torvalds speed = clamp(speed, 0, 0xffff / 177);
43574fac04eSJustin Stitt
4361da177e4SLinus Torvalds if (speed == 0)
4371da177e4SLinus Torvalds speed = 0xffff; /* set to max */
4381da177e4SLinus Torvalds else
4391da177e4SLinus Torvalds speed *= 177; /* Nx to kbyte/s */
4401da177e4SLinus Torvalds
4411da177e4SLinus Torvalds memset(&cgc, 0, sizeof(struct packet_command));
4421da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_SET_SPEED; /* SET CD SPEED */
4431da177e4SLinus Torvalds cgc.cmd[2] = (speed >> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
4441da177e4SLinus Torvalds cgc.cmd[3] = speed & 0xff; /* LSB */
4451da177e4SLinus Torvalds cgc.data_direction = DMA_NONE;
4461da177e4SLinus Torvalds cgc.timeout = IOCTL_TIMEOUT;
4471da177e4SLinus Torvalds
4481da177e4SLinus Torvalds if (sr_do_ioctl(cd, &cgc))
4491da177e4SLinus Torvalds return -EIO;
4501da177e4SLinus Torvalds return 0;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds
4531da177e4SLinus Torvalds /* ----------------------------------------------------------------------- */
4541da177e4SLinus Torvalds /* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
4551da177e4SLinus Torvalds /* because the generic cdrom driver does the user access stuff for us. */
4561da177e4SLinus Torvalds /* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
4571da177e4SLinus Torvalds /* sr_disk_status interface for the generic cdrom driver. */
4581da177e4SLinus Torvalds
sr_audio_ioctl(struct cdrom_device_info * cdi,unsigned int cmd,void * arg)4591da177e4SLinus Torvalds int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds switch (cmd) {
4621da177e4SLinus Torvalds case CDROMREADTOCHDR:
463a09c6311SChristoph Hellwig return sr_read_tochdr(cdi, arg);
4641da177e4SLinus Torvalds case CDROMREADTOCENTRY:
465a09c6311SChristoph Hellwig return sr_read_tocentry(cdi, arg);
466a09c6311SChristoph Hellwig case CDROMPLAYTRKIND:
467a09c6311SChristoph Hellwig return sr_play_trkind(cdi, arg);
4681da177e4SLinus Torvalds default:
469a09c6311SChristoph Hellwig return -EINVAL;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds
4731da177e4SLinus Torvalds /* -----------------------------------------------------------------------
4741da177e4SLinus Torvalds * a function to read all sorts of funny cdrom sectors using the READ_CD
4751da177e4SLinus Torvalds * scsi-3 mmc command
4761da177e4SLinus Torvalds *
4771da177e4SLinus Torvalds * lba: linear block address
4781da177e4SLinus Torvalds * format: 0 = data (anything)
4791da177e4SLinus Torvalds * 1 = audio
4801da177e4SLinus Torvalds * 2 = data (mode 1)
4811da177e4SLinus Torvalds * 3 = data (mode 2)
4821da177e4SLinus Torvalds * 4 = data (mode 2 form1)
4831da177e4SLinus Torvalds * 5 = data (mode 2 form2)
4841da177e4SLinus Torvalds * blksize: 2048 | 2336 | 2340 | 2352
4851da177e4SLinus Torvalds */
4861da177e4SLinus Torvalds
sr_read_cd(Scsi_CD * cd,unsigned char * dest,int lba,int format,int blksize)4871da177e4SLinus Torvalds static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds struct packet_command cgc;
4901da177e4SLinus Torvalds
4911da177e4SLinus Torvalds #ifdef DEBUG
49296eefad2SHannes Reinecke sr_printk(KERN_INFO, cd, "sr_read_cd lba=%d format=%d blksize=%d\n",
49396eefad2SHannes Reinecke lba, format, blksize);
4941da177e4SLinus Torvalds #endif
4951da177e4SLinus Torvalds
4961da177e4SLinus Torvalds memset(&cgc, 0, sizeof(struct packet_command));
4971da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_READ_CD; /* READ_CD */
4981da177e4SLinus Torvalds cgc.cmd[1] = ((format & 7) << 2);
4991da177e4SLinus Torvalds cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
5001da177e4SLinus Torvalds cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
5011da177e4SLinus Torvalds cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
5021da177e4SLinus Torvalds cgc.cmd[5] = (unsigned char) lba & 0xff;
5031da177e4SLinus Torvalds cgc.cmd[8] = 1;
5041da177e4SLinus Torvalds switch (blksize) {
5051da177e4SLinus Torvalds case 2336:
5061da177e4SLinus Torvalds cgc.cmd[9] = 0x58;
5071da177e4SLinus Torvalds break;
5081da177e4SLinus Torvalds case 2340:
5091da177e4SLinus Torvalds cgc.cmd[9] = 0x78;
5101da177e4SLinus Torvalds break;
5111da177e4SLinus Torvalds case 2352:
5121da177e4SLinus Torvalds cgc.cmd[9] = 0xf8;
5131da177e4SLinus Torvalds break;
5141da177e4SLinus Torvalds default:
5151da177e4SLinus Torvalds cgc.cmd[9] = 0x10;
5161da177e4SLinus Torvalds break;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds cgc.buffer = dest;
5191da177e4SLinus Torvalds cgc.buflen = blksize;
5201da177e4SLinus Torvalds cgc.data_direction = DMA_FROM_DEVICE;
5211da177e4SLinus Torvalds cgc.timeout = IOCTL_TIMEOUT;
5221da177e4SLinus Torvalds return sr_do_ioctl(cd, &cgc);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds
5251da177e4SLinus Torvalds /*
5261da177e4SLinus Torvalds * read sectors with blocksizes other than 2048
5271da177e4SLinus Torvalds */
5281da177e4SLinus Torvalds
sr_read_sector(Scsi_CD * cd,int lba,int blksize,unsigned char * dest)5291da177e4SLinus Torvalds static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds struct packet_command cgc;
5321da177e4SLinus Torvalds int rc;
5331da177e4SLinus Torvalds
5341da177e4SLinus Torvalds /* we try the READ CD command first... */
5351da177e4SLinus Torvalds if (cd->readcd_known) {
5361da177e4SLinus Torvalds rc = sr_read_cd(cd, dest, lba, 0, blksize);
5371da177e4SLinus Torvalds if (-EDRIVE_CANT_DO_THIS != rc)
5381da177e4SLinus Torvalds return rc;
5391da177e4SLinus Torvalds cd->readcd_known = 0;
54096eefad2SHannes Reinecke sr_printk(KERN_INFO, cd,
541e699a4e1SColin Ian King "CDROM doesn't support READ CD (0xbe) command\n");
5421da177e4SLinus Torvalds /* fall & retry the other way */
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds /* ... if this fails, we switch the blocksize using MODE SELECT */
5451da177e4SLinus Torvalds if (blksize != cd->device->sector_size) {
5461da177e4SLinus Torvalds if (0 != (rc = sr_set_blocklength(cd, blksize)))
5471da177e4SLinus Torvalds return rc;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds #ifdef DEBUG
55096eefad2SHannes Reinecke sr_printk(KERN_INFO, cd, "sr_read_sector lba=%d blksize=%d\n",
55196eefad2SHannes Reinecke lba, blksize);
5521da177e4SLinus Torvalds #endif
5531da177e4SLinus Torvalds
5541da177e4SLinus Torvalds memset(&cgc, 0, sizeof(struct packet_command));
5551da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_READ_10;
5561da177e4SLinus Torvalds cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
5571da177e4SLinus Torvalds cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
5581da177e4SLinus Torvalds cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
5591da177e4SLinus Torvalds cgc.cmd[5] = (unsigned char) lba & 0xff;
5601da177e4SLinus Torvalds cgc.cmd[8] = 1;
5611da177e4SLinus Torvalds cgc.buffer = dest;
5621da177e4SLinus Torvalds cgc.buflen = blksize;
5631da177e4SLinus Torvalds cgc.data_direction = DMA_FROM_DEVICE;
5641da177e4SLinus Torvalds cgc.timeout = IOCTL_TIMEOUT;
5651da177e4SLinus Torvalds rc = sr_do_ioctl(cd, &cgc);
5661da177e4SLinus Torvalds
5678d2ac857SSebastian Andrzej Siewior if (blksize != CD_FRAMESIZE)
5688d2ac857SSebastian Andrzej Siewior rc |= sr_set_blocklength(cd, CD_FRAMESIZE);
5691da177e4SLinus Torvalds return rc;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds
5721da177e4SLinus Torvalds /*
5731da177e4SLinus Torvalds * read a sector in raw mode to check the sector format
5741da177e4SLinus Torvalds * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
5751da177e4SLinus Torvalds */
5761da177e4SLinus Torvalds
sr_is_xa(Scsi_CD * cd)5771da177e4SLinus Torvalds int sr_is_xa(Scsi_CD *cd)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds unsigned char *raw_sector;
5801da177e4SLinus Torvalds int is_xa;
5811da177e4SLinus Torvalds
5821da177e4SLinus Torvalds if (!xa_test)
5831da177e4SLinus Torvalds return 0;
5841da177e4SLinus Torvalds
585aaff5ebaSChristoph Hellwig raw_sector = kmalloc(2048, GFP_KERNEL);
5861da177e4SLinus Torvalds if (!raw_sector)
5871da177e4SLinus Torvalds return -ENOMEM;
5881da177e4SLinus Torvalds if (0 == sr_read_sector(cd, cd->ms_offset + 16,
5891da177e4SLinus Torvalds CD_FRAMESIZE_RAW1, raw_sector)) {
5901da177e4SLinus Torvalds is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
5911da177e4SLinus Torvalds } else {
5921da177e4SLinus Torvalds /* read a raw sector failed for some reason. */
5931da177e4SLinus Torvalds is_xa = -1;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds kfree(raw_sector);
5961da177e4SLinus Torvalds #ifdef DEBUG
59796eefad2SHannes Reinecke sr_printk(KERN_INFO, cd, "sr_is_xa: %d\n", is_xa);
5981da177e4SLinus Torvalds #endif
5991da177e4SLinus Torvalds return is_xa;
6001da177e4SLinus Torvalds }
601