1 #include <linux/kernel.h>
2 #include <linux/sched.h>
5 #include <linux/errno.h>
6 #include <linux/string.h>
7 #include <linux/blkdev.h>
8 #include <linux/blkpg.h>
9 #include <linux/cdrom.h>
10 #include <linux/delay.h>
12 #include <asm/uaccess.h>
14 #include <scsi/scsi.h>
15 #include <scsi/scsi_dbg.h>
16 #include <scsi/scsi_device.h>
17 #include <scsi/scsi_eh.h>
18 #include <scsi/scsi_host.h>
19 #include <scsi/scsi_ioctl.h>
20 #include <scsi/scsi_cmnd.h>
28 /* The sr_is_xa() seems to trigger firmware bugs with some drives :-(
29 * It is off by default and can be turned on with this module parameter */
30 static int xa_test = 0;
32 module_param(xa_test, int, S_IRUGO | S_IWUSR);
35 #define IOCTL_RETRIES 3
37 /* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command. When these drives
38 are emulating a SCSI device via the idescsi module, they need to have
39 CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
41 static int sr_fake_playtrkind(struct cdrom_device_info *cdi, struct cdrom_ti *ti)
43 struct cdrom_tocentry trk0_te, trk1_te;
44 struct cdrom_tochdr tochdr;
45 struct packet_command cgc;
48 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCHDR, &tochdr)))
51 ntracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
53 if (ti->cdti_trk1 == ntracks)
54 ti->cdti_trk1 = CDROM_LEADOUT;
55 else if (ti->cdti_trk1 != CDROM_LEADOUT)
58 trk0_te.cdte_track = ti->cdti_trk0;
59 trk0_te.cdte_format = CDROM_MSF;
60 trk1_te.cdte_track = ti->cdti_trk1;
61 trk1_te.cdte_format = CDROM_MSF;
63 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &trk0_te)))
65 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &trk1_te)))
68 memset(&cgc, 0, sizeof(struct packet_command));
69 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
70 cgc.cmd[3] = trk0_te.cdte_addr.msf.minute;
71 cgc.cmd[4] = trk0_te.cdte_addr.msf.second;
72 cgc.cmd[5] = trk0_te.cdte_addr.msf.frame;
73 cgc.cmd[6] = trk1_te.cdte_addr.msf.minute;
74 cgc.cmd[7] = trk1_te.cdte_addr.msf.second;
75 cgc.cmd[8] = trk1_te.cdte_addr.msf.frame;
76 cgc.data_direction = DMA_NONE;
77 cgc.timeout = IOCTL_TIMEOUT;
78 return sr_do_ioctl(cdi->handle, &cgc);
81 /* We do our own retries because we want to know what the specific
82 error code is. Normally the UNIT_ATTENTION code will automatically
83 clear after one error */
85 int sr_do_ioctl(Scsi_CD *cd, struct packet_command *cgc)
87 struct scsi_device *SDev;
88 struct scsi_sense_hdr sshdr;
89 int result, err = 0, retries = 0;
90 struct request_sense *sense = cgc->sense;
95 sense = kmalloc(sizeof(*sense), GFP_KERNEL);
103 if (!scsi_block_when_processing_errors(SDev)) {
108 memset(sense, 0, sizeof(*sense));
109 result = scsi_execute(SDev, cgc->cmd, cgc->data_direction,
110 cgc->buffer, cgc->buflen, (char *)sense,
111 cgc->timeout, IOCTL_RETRIES, 0);
113 scsi_normalize_sense((char *)sense, sizeof(*sense), &sshdr);
115 /* Minimal error checking. Ignore cases we know about, and report the rest. */
116 if (driver_byte(result) != 0) {
117 switch (sshdr.sense_key) {
121 printk(KERN_INFO "%s: disc change detected.\n", cd->cdi.name);
126 case NOT_READY: /* This happens if there is no disc in drive */
127 if (sshdr.asc == 0x04 &&
128 sshdr.ascq == 0x01) {
129 /* sense: Logical unit is in process of becoming ready */
131 printk(KERN_INFO "%s: CDROM not ready yet.\n", cd->cdi.name);
132 if (retries++ < 10) {
133 /* sleep 2 sec and try again */
137 /* 20 secs are enough? */
143 printk(KERN_INFO "%s: CDROM not ready. Make sure there is a disc in the drive.\n", cd->cdi.name);
145 scsi_print_sense_hdr("sr", &sshdr);
149 case ILLEGAL_REQUEST:
151 if (sshdr.asc == 0x20 &&
153 /* sense: Invalid command operation code */
154 err = -EDRIVE_CANT_DO_THIS;
156 __scsi_print_command(cgc->cmd);
157 scsi_print_sense_hdr("sr", &sshdr);
161 printk(KERN_ERR "%s: CDROM (ioctl) error, command: ", cd->cdi.name);
162 __scsi_print_command(cgc->cmd);
163 scsi_print_sense_hdr("sr", &sshdr);
168 /* Wake up a process waiting for device */
176 /* ---------------------------------------------------------------------- */
177 /* interface to cdrom.c */
179 static int test_unit_ready(Scsi_CD *cd)
181 struct packet_command cgc;
183 memset(&cgc, 0, sizeof(struct packet_command));
184 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
186 cgc.data_direction = DMA_NONE;
187 cgc.timeout = IOCTL_TIMEOUT;
188 return sr_do_ioctl(cd, &cgc);
191 int sr_tray_move(struct cdrom_device_info *cdi, int pos)
193 Scsi_CD *cd = cdi->handle;
194 struct packet_command cgc;
196 memset(&cgc, 0, sizeof(struct packet_command));
197 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
198 cgc.cmd[4] = (pos == 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
199 cgc.data_direction = DMA_NONE;
200 cgc.timeout = IOCTL_TIMEOUT;
201 return sr_do_ioctl(cd, &cgc);
204 int sr_lock_door(struct cdrom_device_info *cdi, int lock)
206 Scsi_CD *cd = cdi->handle;
208 return scsi_set_medium_removal(cd->device, lock ?
209 SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
212 int sr_drive_status(struct cdrom_device_info *cdi, int slot)
214 if (CDSL_CURRENT != slot) {
215 /* we have no changer support */
218 if (0 == test_unit_ready(cdi->handle))
221 return CDS_TRAY_OPEN;
224 int sr_disk_status(struct cdrom_device_info *cdi)
226 Scsi_CD *cd = cdi->handle;
227 struct cdrom_tochdr toc_h;
228 struct cdrom_tocentry toc_e;
229 int i, rc, have_datatracks = 0;
231 /* look for data tracks */
232 if (0 != (rc = sr_audio_ioctl(cdi, CDROMREADTOCHDR, &toc_h)))
233 return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
235 for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
236 toc_e.cdte_track = i;
237 toc_e.cdte_format = CDROM_LBA;
238 if (sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &toc_e))
240 if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
245 if (!have_datatracks)
254 int sr_get_last_session(struct cdrom_device_info *cdi,
255 struct cdrom_multisession *ms_info)
257 Scsi_CD *cd = cdi->handle;
259 ms_info->addr.lba = cd->ms_offset;
260 ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
265 /* primitive to determine whether we need to have GFP_DMA set based on
266 * the status of the unchecked_isa_dma flag in the host structure */
267 #define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
269 int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
271 Scsi_CD *cd = cdi->handle;
272 struct packet_command cgc;
273 char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
279 memset(&cgc, 0, sizeof(struct packet_command));
280 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
281 cgc.cmd[2] = 0x40; /* I do want the subchannel info */
282 cgc.cmd[3] = 0x02; /* Give me medium catalog number info */
286 cgc.data_direction = DMA_FROM_DEVICE;
287 cgc.timeout = IOCTL_TIMEOUT;
288 result = sr_do_ioctl(cd, &cgc);
290 memcpy(mcn->medium_catalog_number, buffer + 9, 13);
291 mcn->medium_catalog_number[13] = 0;
297 int sr_reset(struct cdrom_device_info *cdi)
302 int sr_select_speed(struct cdrom_device_info *cdi, int speed)
304 Scsi_CD *cd = cdi->handle;
305 struct packet_command cgc;
308 speed = 0xffff; /* set to max */
310 speed *= 177; /* Nx to kbyte/s */
312 memset(&cgc, 0, sizeof(struct packet_command));
313 cgc.cmd[0] = GPCMD_SET_SPEED; /* SET CD SPEED */
314 cgc.cmd[2] = (speed >> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
315 cgc.cmd[3] = speed & 0xff; /* LSB */
316 cgc.data_direction = DMA_NONE;
317 cgc.timeout = IOCTL_TIMEOUT;
319 if (sr_do_ioctl(cd, &cgc))
324 /* ----------------------------------------------------------------------- */
325 /* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
326 /* because the generic cdrom driver does the user access stuff for us. */
327 /* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
328 /* sr_disk_status interface for the generic cdrom driver. */
330 int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
332 Scsi_CD *cd = cdi->handle;
333 struct packet_command cgc;
335 unsigned char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
340 memset(&cgc, 0, sizeof(struct packet_command));
341 cgc.timeout = IOCTL_TIMEOUT;
344 case CDROMREADTOCHDR:
346 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
348 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
349 cgc.cmd[8] = 12; /* LSB of length */
353 cgc.data_direction = DMA_FROM_DEVICE;
355 result = sr_do_ioctl(cd, &cgc);
357 tochdr->cdth_trk0 = buffer[2];
358 tochdr->cdth_trk1 = buffer[3];
363 case CDROMREADTOCENTRY:
365 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry *) arg;
367 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
368 cgc.cmd[1] |= (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0;
369 cgc.cmd[6] = tocentry->cdte_track;
370 cgc.cmd[8] = 12; /* LSB of length */
373 cgc.data_direction = DMA_FROM_DEVICE;
375 result = sr_do_ioctl(cd, &cgc);
377 tocentry->cdte_ctrl = buffer[5] & 0xf;
378 tocentry->cdte_adr = buffer[5] >> 4;
379 tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
380 if (tocentry->cdte_format == CDROM_MSF) {
381 tocentry->cdte_addr.msf.minute = buffer[9];
382 tocentry->cdte_addr.msf.second = buffer[10];
383 tocentry->cdte_addr.msf.frame = buffer[11];
385 tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
386 + buffer[10]) << 8) + buffer[11];
391 case CDROMPLAYTRKIND: {
392 struct cdrom_ti* ti = (struct cdrom_ti*)arg;
394 cgc.cmd[0] = GPCMD_PLAYAUDIO_TI;
395 cgc.cmd[4] = ti->cdti_trk0;
396 cgc.cmd[5] = ti->cdti_ind0;
397 cgc.cmd[7] = ti->cdti_trk1;
398 cgc.cmd[8] = ti->cdti_ind1;
399 cgc.data_direction = DMA_NONE;
401 result = sr_do_ioctl(cd, &cgc);
402 if (result == -EDRIVE_CANT_DO_THIS)
403 result = sr_fake_playtrkind(cdi, ti);
414 printk("DEBUG: sr_audio: result for ioctl %x: %x\n", cmd, result);
421 /* -----------------------------------------------------------------------
422 * a function to read all sorts of funny cdrom sectors using the READ_CD
425 * lba: linear block address
426 * format: 0 = data (anything)
430 * 4 = data (mode 2 form1)
431 * 5 = data (mode 2 form2)
432 * blksize: 2048 | 2336 | 2340 | 2352
435 static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
437 struct packet_command cgc;
440 printk("%s: sr_read_cd lba=%d format=%d blksize=%d\n",
441 cd->cdi.name, lba, format, blksize);
444 memset(&cgc, 0, sizeof(struct packet_command));
445 cgc.cmd[0] = GPCMD_READ_CD; /* READ_CD */
446 cgc.cmd[1] = ((format & 7) << 2);
447 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
448 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
449 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
450 cgc.cmd[5] = (unsigned char) lba & 0xff;
467 cgc.buflen = blksize;
468 cgc.data_direction = DMA_FROM_DEVICE;
469 cgc.timeout = IOCTL_TIMEOUT;
470 return sr_do_ioctl(cd, &cgc);
474 * read sectors with blocksizes other than 2048
477 static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
479 struct packet_command cgc;
482 /* we try the READ CD command first... */
483 if (cd->readcd_known) {
484 rc = sr_read_cd(cd, dest, lba, 0, blksize);
485 if (-EDRIVE_CANT_DO_THIS != rc)
487 cd->readcd_known = 0;
488 printk("CDROM does'nt support READ CD (0xbe) command\n");
489 /* fall & retry the other way */
491 /* ... if this fails, we switch the blocksize using MODE SELECT */
492 if (blksize != cd->device->sector_size) {
493 if (0 != (rc = sr_set_blocklength(cd, blksize)))
497 printk("%s: sr_read_sector lba=%d blksize=%d\n", cd->cdi.name, lba, blksize);
500 memset(&cgc, 0, sizeof(struct packet_command));
501 cgc.cmd[0] = GPCMD_READ_10;
502 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
503 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
504 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
505 cgc.cmd[5] = (unsigned char) lba & 0xff;
508 cgc.buflen = blksize;
509 cgc.data_direction = DMA_FROM_DEVICE;
510 cgc.timeout = IOCTL_TIMEOUT;
511 rc = sr_do_ioctl(cd, &cgc);
517 * read a sector in raw mode to check the sector format
518 * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
521 int sr_is_xa(Scsi_CD *cd)
523 unsigned char *raw_sector;
529 raw_sector = (unsigned char *) kmalloc(2048, GFP_KERNEL | SR_GFP_DMA(cd));
532 if (0 == sr_read_sector(cd, cd->ms_offset + 16,
533 CD_FRAMESIZE_RAW1, raw_sector)) {
534 is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
536 /* read a raw sector failed for some reason. */
541 printk("%s: sr_is_xa: %d\n", cd->cdi.name, is_xa);
546 int sr_dev_ioctl(struct cdrom_device_info *cdi,
547 unsigned int cmd, unsigned long arg)
549 Scsi_CD *cd = cdi->handle;
552 ret = scsi_nonblockable_ioctl(cd->device, cmd,
553 (void __user *)arg, NULL);
555 * ENODEV means that we didn't recognise the ioctl, or that we
556 * cannot execute it in the current device state. In either
557 * case fall through to scsi_ioctl, which will return ENDOEV again
558 * if it doesn't recognise the ioctl
562 return scsi_ioctl(cd->device, cmd, (void __user *)arg);