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_request.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_request *SRpnt;
88 struct scsi_device *SDev;
90 int result, err = 0, retries = 0;
93 SRpnt = scsi_allocate_request(SDev, GFP_KERNEL);
95 printk(KERN_ERR "Unable to allocate SCSI request in sr_do_ioctl");
99 SRpnt->sr_data_direction = cgc->data_direction;
102 if (!scsi_block_when_processing_errors(SDev)) {
107 scsi_wait_req(SRpnt, cgc->cmd, cgc->buffer, cgc->buflen,
108 cgc->timeout, IOCTL_RETRIES);
110 req = SRpnt->sr_request;
111 if (SRpnt->sr_buffer && req->buffer && SRpnt->sr_buffer != req->buffer) {
112 memcpy(req->buffer, SRpnt->sr_buffer, SRpnt->sr_bufflen);
113 kfree(SRpnt->sr_buffer);
114 SRpnt->sr_buffer = req->buffer;
117 result = SRpnt->sr_result;
119 /* Minimal error checking. Ignore cases we know about, and report the rest. */
120 if (driver_byte(result) != 0) {
121 switch (SRpnt->sr_sense_buffer[2] & 0xf) {
125 printk(KERN_INFO "%s: disc change detected.\n", cd->cdi.name);
130 case NOT_READY: /* This happens if there is no disc in drive */
131 if (SRpnt->sr_sense_buffer[12] == 0x04 &&
132 SRpnt->sr_sense_buffer[13] == 0x01) {
133 /* sense: Logical unit is in process of becoming ready */
135 printk(KERN_INFO "%s: CDROM not ready yet.\n", cd->cdi.name);
136 if (retries++ < 10) {
137 /* sleep 2 sec and try again */
141 /* 20 secs are enough? */
147 printk(KERN_INFO "%s: CDROM not ready. Make sure there is a disc in the drive.\n", cd->cdi.name);
149 scsi_print_req_sense("sr", SRpnt);
153 case ILLEGAL_REQUEST:
155 if (SRpnt->sr_sense_buffer[12] == 0x20 &&
156 SRpnt->sr_sense_buffer[13] == 0x00)
157 /* sense: Invalid command operation code */
158 err = -EDRIVE_CANT_DO_THIS;
160 __scsi_print_command(cgc->cmd);
161 scsi_print_req_sense("sr", SRpnt);
165 printk(KERN_ERR "%s: CDROM (ioctl) error, command: ", cd->cdi.name);
166 __scsi_print_command(cgc->cmd);
167 scsi_print_req_sense("sr", SRpnt);
173 memcpy(cgc->sense, SRpnt->sr_sense_buffer, sizeof(*cgc->sense));
175 /* Wake up a process waiting for device */
177 scsi_release_request(SRpnt);
184 /* ---------------------------------------------------------------------- */
185 /* interface to cdrom.c */
187 static int test_unit_ready(Scsi_CD *cd)
189 struct packet_command cgc;
191 memset(&cgc, 0, sizeof(struct packet_command));
192 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
194 cgc.data_direction = DMA_NONE;
195 cgc.timeout = IOCTL_TIMEOUT;
196 return sr_do_ioctl(cd, &cgc);
199 int sr_tray_move(struct cdrom_device_info *cdi, int pos)
201 Scsi_CD *cd = cdi->handle;
202 struct packet_command cgc;
204 memset(&cgc, 0, sizeof(struct packet_command));
205 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
206 cgc.cmd[4] = (pos == 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
207 cgc.data_direction = DMA_NONE;
208 cgc.timeout = IOCTL_TIMEOUT;
209 return sr_do_ioctl(cd, &cgc);
212 int sr_lock_door(struct cdrom_device_info *cdi, int lock)
214 Scsi_CD *cd = cdi->handle;
216 return scsi_set_medium_removal(cd->device, lock ?
217 SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
220 int sr_drive_status(struct cdrom_device_info *cdi, int slot)
222 if (CDSL_CURRENT != slot) {
223 /* we have no changer support */
226 if (0 == test_unit_ready(cdi->handle))
229 return CDS_TRAY_OPEN;
232 int sr_disk_status(struct cdrom_device_info *cdi)
234 Scsi_CD *cd = cdi->handle;
235 struct cdrom_tochdr toc_h;
236 struct cdrom_tocentry toc_e;
237 int i, rc, have_datatracks = 0;
239 /* look for data tracks */
240 if (0 != (rc = sr_audio_ioctl(cdi, CDROMREADTOCHDR, &toc_h)))
241 return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
243 for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
244 toc_e.cdte_track = i;
245 toc_e.cdte_format = CDROM_LBA;
246 if (sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &toc_e))
248 if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
253 if (!have_datatracks)
262 int sr_get_last_session(struct cdrom_device_info *cdi,
263 struct cdrom_multisession *ms_info)
265 Scsi_CD *cd = cdi->handle;
267 ms_info->addr.lba = cd->ms_offset;
268 ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
273 /* primitive to determine whether we need to have GFP_DMA set based on
274 * the status of the unchecked_isa_dma flag in the host structure */
275 #define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
277 int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
279 Scsi_CD *cd = cdi->handle;
280 struct packet_command cgc;
281 char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
287 memset(&cgc, 0, sizeof(struct packet_command));
288 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
289 cgc.cmd[2] = 0x40; /* I do want the subchannel info */
290 cgc.cmd[3] = 0x02; /* Give me medium catalog number info */
294 cgc.data_direction = DMA_FROM_DEVICE;
295 cgc.timeout = IOCTL_TIMEOUT;
296 result = sr_do_ioctl(cd, &cgc);
298 memcpy(mcn->medium_catalog_number, buffer + 9, 13);
299 mcn->medium_catalog_number[13] = 0;
305 int sr_reset(struct cdrom_device_info *cdi)
310 int sr_select_speed(struct cdrom_device_info *cdi, int speed)
312 Scsi_CD *cd = cdi->handle;
313 struct packet_command cgc;
316 speed = 0xffff; /* set to max */
318 speed *= 177; /* Nx to kbyte/s */
320 memset(&cgc, 0, sizeof(struct packet_command));
321 cgc.cmd[0] = GPCMD_SET_SPEED; /* SET CD SPEED */
322 cgc.cmd[2] = (speed >> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
323 cgc.cmd[3] = speed & 0xff; /* LSB */
324 cgc.data_direction = DMA_NONE;
325 cgc.timeout = IOCTL_TIMEOUT;
327 if (sr_do_ioctl(cd, &cgc))
332 /* ----------------------------------------------------------------------- */
333 /* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
334 /* because the generic cdrom driver does the user access stuff for us. */
335 /* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
336 /* sr_disk_status interface for the generic cdrom driver. */
338 int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
340 Scsi_CD *cd = cdi->handle;
341 struct packet_command cgc;
343 unsigned char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
348 memset(&cgc, 0, sizeof(struct packet_command));
349 cgc.timeout = IOCTL_TIMEOUT;
352 case CDROMREADTOCHDR:
354 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
356 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
357 cgc.cmd[8] = 12; /* LSB of length */
361 cgc.data_direction = DMA_FROM_DEVICE;
363 result = sr_do_ioctl(cd, &cgc);
365 tochdr->cdth_trk0 = buffer[2];
366 tochdr->cdth_trk1 = buffer[3];
371 case CDROMREADTOCENTRY:
373 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry *) arg;
375 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
376 cgc.cmd[1] |= (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0;
377 cgc.cmd[6] = tocentry->cdte_track;
378 cgc.cmd[8] = 12; /* LSB of length */
381 cgc.data_direction = DMA_FROM_DEVICE;
383 result = sr_do_ioctl(cd, &cgc);
385 tocentry->cdte_ctrl = buffer[5] & 0xf;
386 tocentry->cdte_adr = buffer[5] >> 4;
387 tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
388 if (tocentry->cdte_format == CDROM_MSF) {
389 tocentry->cdte_addr.msf.minute = buffer[9];
390 tocentry->cdte_addr.msf.second = buffer[10];
391 tocentry->cdte_addr.msf.frame = buffer[11];
393 tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
394 + buffer[10]) << 8) + buffer[11];
399 case CDROMPLAYTRKIND: {
400 struct cdrom_ti* ti = (struct cdrom_ti*)arg;
402 cgc.cmd[0] = GPCMD_PLAYAUDIO_TI;
403 cgc.cmd[4] = ti->cdti_trk0;
404 cgc.cmd[5] = ti->cdti_ind0;
405 cgc.cmd[7] = ti->cdti_trk1;
406 cgc.cmd[8] = ti->cdti_ind1;
407 cgc.data_direction = DMA_NONE;
409 result = sr_do_ioctl(cd, &cgc);
410 if (result == -EDRIVE_CANT_DO_THIS)
411 result = sr_fake_playtrkind(cdi, ti);
422 printk("DEBUG: sr_audio: result for ioctl %x: %x\n", cmd, result);
429 /* -----------------------------------------------------------------------
430 * a function to read all sorts of funny cdrom sectors using the READ_CD
433 * lba: linear block address
434 * format: 0 = data (anything)
438 * 4 = data (mode 2 form1)
439 * 5 = data (mode 2 form2)
440 * blksize: 2048 | 2336 | 2340 | 2352
443 static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
445 struct packet_command cgc;
448 printk("%s: sr_read_cd lba=%d format=%d blksize=%d\n",
449 cd->cdi.name, lba, format, blksize);
452 memset(&cgc, 0, sizeof(struct packet_command));
453 cgc.cmd[0] = GPCMD_READ_CD; /* READ_CD */
454 cgc.cmd[1] = ((format & 7) << 2);
455 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
456 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
457 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
458 cgc.cmd[5] = (unsigned char) lba & 0xff;
475 cgc.buflen = blksize;
476 cgc.data_direction = DMA_FROM_DEVICE;
477 cgc.timeout = IOCTL_TIMEOUT;
478 return sr_do_ioctl(cd, &cgc);
482 * read sectors with blocksizes other than 2048
485 static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
487 struct packet_command cgc;
490 /* we try the READ CD command first... */
491 if (cd->readcd_known) {
492 rc = sr_read_cd(cd, dest, lba, 0, blksize);
493 if (-EDRIVE_CANT_DO_THIS != rc)
495 cd->readcd_known = 0;
496 printk("CDROM does'nt support READ CD (0xbe) command\n");
497 /* fall & retry the other way */
499 /* ... if this fails, we switch the blocksize using MODE SELECT */
500 if (blksize != cd->device->sector_size) {
501 if (0 != (rc = sr_set_blocklength(cd, blksize)))
505 printk("%s: sr_read_sector lba=%d blksize=%d\n", cd->cdi.name, lba, blksize);
508 memset(&cgc, 0, sizeof(struct packet_command));
509 cgc.cmd[0] = GPCMD_READ_10;
510 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
511 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
512 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
513 cgc.cmd[5] = (unsigned char) lba & 0xff;
516 cgc.buflen = blksize;
517 cgc.data_direction = DMA_FROM_DEVICE;
518 cgc.timeout = IOCTL_TIMEOUT;
519 rc = sr_do_ioctl(cd, &cgc);
525 * read a sector in raw mode to check the sector format
526 * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
529 int sr_is_xa(Scsi_CD *cd)
531 unsigned char *raw_sector;
537 raw_sector = (unsigned char *) kmalloc(2048, GFP_KERNEL | SR_GFP_DMA(cd));
540 if (0 == sr_read_sector(cd, cd->ms_offset + 16,
541 CD_FRAMESIZE_RAW1, raw_sector)) {
542 is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
544 /* read a raw sector failed for some reason. */
549 printk("%s: sr_is_xa: %d\n", cd->cdi.name, is_xa);
554 int sr_dev_ioctl(struct cdrom_device_info *cdi,
555 unsigned int cmd, unsigned long arg)
557 Scsi_CD *cd = cdi->handle;
560 ret = scsi_nonblockable_ioctl(cd->device, cmd,
561 (void __user *)arg, NULL);
563 * ENODEV means that we didn't recognise the ioctl, or that we
564 * cannot execute it in the current device state. In either
565 * case fall through to scsi_ioctl, which will return ENDOEV again
566 * if it doesn't recognise the ioctl
570 return scsi_ioctl(cd->device, cmd, (void __user *)arg);