1 #include <linux/kernel.h>
4 #include <linux/errno.h>
5 #include <linux/string.h>
6 #include <linux/blkdev.h>
7 #include <linux/blkpg.h>
8 #include <linux/cdrom.h>
9 #include <linux/delay.h>
10 #include <linux/slab.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
);
34 /* primitive to determine whether we need to have GFP_DMA set based on
35 * the status of the unchecked_isa_dma flag in the host structure */
36 #define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
39 static int sr_read_tochdr(struct cdrom_device_info
*cdi
,
40 struct cdrom_tochdr
*tochdr
)
42 struct scsi_cd
*cd
= cdi
->handle
;
43 struct packet_command cgc
;
45 unsigned char *buffer
;
47 buffer
= kmalloc(32, GFP_KERNEL
| SR_GFP_DMA(cd
));
51 memset(&cgc
, 0, sizeof(struct packet_command
));
52 cgc
.timeout
= IOCTL_TIMEOUT
;
53 cgc
.cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
54 cgc
.cmd
[8] = 12; /* LSB of length */
58 cgc
.data_direction
= DMA_FROM_DEVICE
;
60 result
= sr_do_ioctl(cd
, &cgc
);
62 tochdr
->cdth_trk0
= buffer
[2];
63 tochdr
->cdth_trk1
= buffer
[3];
69 static int sr_read_tocentry(struct cdrom_device_info
*cdi
,
70 struct cdrom_tocentry
*tocentry
)
72 struct scsi_cd
*cd
= cdi
->handle
;
73 struct packet_command cgc
;
75 unsigned char *buffer
;
77 buffer
= kmalloc(32, GFP_KERNEL
| SR_GFP_DMA(cd
));
81 memset(&cgc
, 0, sizeof(struct packet_command
));
82 cgc
.timeout
= IOCTL_TIMEOUT
;
83 cgc
.cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
84 cgc
.cmd
[1] |= (tocentry
->cdte_format
== CDROM_MSF
) ? 0x02 : 0;
85 cgc
.cmd
[6] = tocentry
->cdte_track
;
86 cgc
.cmd
[8] = 12; /* LSB of length */
89 cgc
.data_direction
= DMA_FROM_DEVICE
;
91 result
= sr_do_ioctl(cd
, &cgc
);
93 tocentry
->cdte_ctrl
= buffer
[5] & 0xf;
94 tocentry
->cdte_adr
= buffer
[5] >> 4;
95 tocentry
->cdte_datamode
= (tocentry
->cdte_ctrl
& 0x04) ? 1 : 0;
96 if (tocentry
->cdte_format
== CDROM_MSF
) {
97 tocentry
->cdte_addr
.msf
.minute
= buffer
[9];
98 tocentry
->cdte_addr
.msf
.second
= buffer
[10];
99 tocentry
->cdte_addr
.msf
.frame
= buffer
[11];
101 tocentry
->cdte_addr
.lba
= (((((buffer
[8] << 8) + buffer
[9]) << 8)
102 + buffer
[10]) << 8) + buffer
[11];
108 #define IOCTL_RETRIES 3
110 /* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command. When these drives
111 are emulating a SCSI device via the idescsi module, they need to have
112 CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
114 static int sr_fake_playtrkind(struct cdrom_device_info
*cdi
, struct cdrom_ti
*ti
)
116 struct cdrom_tocentry trk0_te
, trk1_te
;
117 struct cdrom_tochdr tochdr
;
118 struct packet_command cgc
;
121 ret
= sr_read_tochdr(cdi
, &tochdr
);
125 ntracks
= tochdr
.cdth_trk1
- tochdr
.cdth_trk0
+ 1;
127 if (ti
->cdti_trk1
== ntracks
)
128 ti
->cdti_trk1
= CDROM_LEADOUT
;
129 else if (ti
->cdti_trk1
!= CDROM_LEADOUT
)
132 trk0_te
.cdte_track
= ti
->cdti_trk0
;
133 trk0_te
.cdte_format
= CDROM_MSF
;
134 trk1_te
.cdte_track
= ti
->cdti_trk1
;
135 trk1_te
.cdte_format
= CDROM_MSF
;
137 ret
= sr_read_tocentry(cdi
, &trk0_te
);
140 ret
= sr_read_tocentry(cdi
, &trk1_te
);
144 memset(&cgc
, 0, sizeof(struct packet_command
));
145 cgc
.cmd
[0] = GPCMD_PLAY_AUDIO_MSF
;
146 cgc
.cmd
[3] = trk0_te
.cdte_addr
.msf
.minute
;
147 cgc
.cmd
[4] = trk0_te
.cdte_addr
.msf
.second
;
148 cgc
.cmd
[5] = trk0_te
.cdte_addr
.msf
.frame
;
149 cgc
.cmd
[6] = trk1_te
.cdte_addr
.msf
.minute
;
150 cgc
.cmd
[7] = trk1_te
.cdte_addr
.msf
.second
;
151 cgc
.cmd
[8] = trk1_te
.cdte_addr
.msf
.frame
;
152 cgc
.data_direction
= DMA_NONE
;
153 cgc
.timeout
= IOCTL_TIMEOUT
;
154 return sr_do_ioctl(cdi
->handle
, &cgc
);
157 static int sr_play_trkind(struct cdrom_device_info
*cdi
,
161 struct scsi_cd
*cd
= cdi
->handle
;
162 struct packet_command cgc
;
165 memset(&cgc
, 0, sizeof(struct packet_command
));
166 cgc
.timeout
= IOCTL_TIMEOUT
;
167 cgc
.cmd
[0] = GPCMD_PLAYAUDIO_TI
;
168 cgc
.cmd
[4] = ti
->cdti_trk0
;
169 cgc
.cmd
[5] = ti
->cdti_ind0
;
170 cgc
.cmd
[7] = ti
->cdti_trk1
;
171 cgc
.cmd
[8] = ti
->cdti_ind1
;
172 cgc
.data_direction
= DMA_NONE
;
174 result
= sr_do_ioctl(cd
, &cgc
);
175 if (result
== -EDRIVE_CANT_DO_THIS
)
176 result
= sr_fake_playtrkind(cdi
, ti
);
181 /* We do our own retries because we want to know what the specific
182 error code is. Normally the UNIT_ATTENTION code will automatically
183 clear after one error */
185 int sr_do_ioctl(Scsi_CD
*cd
, struct packet_command
*cgc
)
187 struct scsi_device
*SDev
;
188 struct scsi_sense_hdr sshdr
;
189 int result
, err
= 0, retries
= 0;
190 struct request_sense
*sense
= cgc
->sense
;
195 sense
= kmalloc(SCSI_SENSE_BUFFERSIZE
, GFP_KERNEL
);
203 if (!scsi_block_when_processing_errors(SDev
)) {
208 memset(sense
, 0, sizeof(*sense
));
209 result
= scsi_execute(SDev
, cgc
->cmd
, cgc
->data_direction
,
210 cgc
->buffer
, cgc
->buflen
, (char *)sense
,
211 cgc
->timeout
, IOCTL_RETRIES
, 0, NULL
);
213 scsi_normalize_sense((char *)sense
, sizeof(*sense
), &sshdr
);
215 /* Minimal error checking. Ignore cases we know about, and report the rest. */
216 if (driver_byte(result
) != 0) {
217 switch (sshdr
.sense_key
) {
221 printk(KERN_INFO
"%s: disc change detected.\n", cd
->cdi
.name
);
226 case NOT_READY
: /* This happens if there is no disc in drive */
227 if (sshdr
.asc
== 0x04 &&
228 sshdr
.ascq
== 0x01) {
229 /* sense: Logical unit is in process of becoming ready */
231 printk(KERN_INFO
"%s: CDROM not ready yet.\n", cd
->cdi
.name
);
232 if (retries
++ < 10) {
233 /* sleep 2 sec and try again */
237 /* 20 secs are enough? */
243 printk(KERN_INFO
"%s: CDROM not ready. Make sure there is a disc in the drive.\n", cd
->cdi
.name
);
245 scsi_print_sense_hdr("sr", &sshdr
);
249 case ILLEGAL_REQUEST
:
251 if (sshdr
.asc
== 0x20 &&
253 /* sense: Invalid command operation code */
254 err
= -EDRIVE_CANT_DO_THIS
;
256 __scsi_print_command(cgc
->cmd
);
257 scsi_print_sense_hdr("sr", &sshdr
);
261 printk(KERN_ERR
"%s: CDROM (ioctl) error, command: ", cd
->cdi
.name
);
262 __scsi_print_command(cgc
->cmd
);
263 scsi_print_sense_hdr("sr", &sshdr
);
268 /* Wake up a process waiting for device */
276 /* ---------------------------------------------------------------------- */
277 /* interface to cdrom.c */
279 int sr_tray_move(struct cdrom_device_info
*cdi
, int pos
)
281 Scsi_CD
*cd
= cdi
->handle
;
282 struct packet_command cgc
;
284 memset(&cgc
, 0, sizeof(struct packet_command
));
285 cgc
.cmd
[0] = GPCMD_START_STOP_UNIT
;
286 cgc
.cmd
[4] = (pos
== 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
287 cgc
.data_direction
= DMA_NONE
;
288 cgc
.timeout
= IOCTL_TIMEOUT
;
289 return sr_do_ioctl(cd
, &cgc
);
292 int sr_lock_door(struct cdrom_device_info
*cdi
, int lock
)
294 Scsi_CD
*cd
= cdi
->handle
;
296 return scsi_set_medium_removal(cd
->device
, lock
?
297 SCSI_REMOVAL_PREVENT
: SCSI_REMOVAL_ALLOW
);
300 int sr_drive_status(struct cdrom_device_info
*cdi
, int slot
)
302 struct scsi_cd
*cd
= cdi
->handle
;
303 struct scsi_sense_hdr sshdr
;
304 struct media_event_desc med
;
306 if (CDSL_CURRENT
!= slot
) {
307 /* we have no changer support */
310 if (!scsi_test_unit_ready(cd
->device
, SR_TIMEOUT
, MAX_RETRIES
, &sshdr
))
313 /* SK/ASC/ASCQ of 2/4/1 means "unit is becoming ready" */
314 if (scsi_sense_valid(&sshdr
) && sshdr
.sense_key
== NOT_READY
315 && sshdr
.asc
== 0x04 && sshdr
.ascq
== 0x01)
316 return CDS_DRIVE_NOT_READY
;
318 if (!cdrom_get_media_event(cdi
, &med
)) {
319 if (med
.media_present
)
321 else if (med
.door_open
)
322 return CDS_TRAY_OPEN
;
328 * SK/ASC/ASCQ of 2/4/2 means "initialization required"
329 * Using CD_TRAY_OPEN results in an START_STOP_UNIT to close
330 * the tray, which resolves the initialization requirement.
332 if (scsi_sense_valid(&sshdr
) && sshdr
.sense_key
== NOT_READY
333 && sshdr
.asc
== 0x04 && sshdr
.ascq
== 0x02)
334 return CDS_TRAY_OPEN
;
337 * 0x04 is format in progress .. but there must be a disc present!
339 if (sshdr
.sense_key
== NOT_READY
&& sshdr
.asc
== 0x04)
343 * If not using Mt Fuji extended media tray reports,
344 * just return TRAY_OPEN since ATAPI doesn't provide
345 * any other way to detect this...
347 if (scsi_sense_valid(&sshdr
) &&
348 /* 0x3a is medium not present */
352 return CDS_TRAY_OPEN
;
354 return CDS_DRIVE_NOT_READY
;
357 int sr_disk_status(struct cdrom_device_info
*cdi
)
359 Scsi_CD
*cd
= cdi
->handle
;
360 struct cdrom_tochdr toc_h
;
361 struct cdrom_tocentry toc_e
;
362 int i
, rc
, have_datatracks
= 0;
364 /* look for data tracks */
365 rc
= sr_read_tochdr(cdi
, &toc_h
);
367 return (rc
== -ENOMEDIUM
) ? CDS_NO_DISC
: CDS_NO_INFO
;
369 for (i
= toc_h
.cdth_trk0
; i
<= toc_h
.cdth_trk1
; i
++) {
370 toc_e
.cdte_track
= i
;
371 toc_e
.cdte_format
= CDROM_LBA
;
372 if (sr_read_tocentry(cdi
, &toc_e
))
374 if (toc_e
.cdte_ctrl
& CDROM_DATA_TRACK
) {
379 if (!have_datatracks
)
388 int sr_get_last_session(struct cdrom_device_info
*cdi
,
389 struct cdrom_multisession
*ms_info
)
391 Scsi_CD
*cd
= cdi
->handle
;
393 ms_info
->addr
.lba
= cd
->ms_offset
;
394 ms_info
->xa_flag
= cd
->xa_flag
|| cd
->ms_offset
> 0;
399 int sr_get_mcn(struct cdrom_device_info
*cdi
, struct cdrom_mcn
*mcn
)
401 Scsi_CD
*cd
= cdi
->handle
;
402 struct packet_command cgc
;
403 char *buffer
= kmalloc(32, GFP_KERNEL
| SR_GFP_DMA(cd
));
409 memset(&cgc
, 0, sizeof(struct packet_command
));
410 cgc
.cmd
[0] = GPCMD_READ_SUBCHANNEL
;
411 cgc
.cmd
[2] = 0x40; /* I do want the subchannel info */
412 cgc
.cmd
[3] = 0x02; /* Give me medium catalog number info */
416 cgc
.data_direction
= DMA_FROM_DEVICE
;
417 cgc
.timeout
= IOCTL_TIMEOUT
;
418 result
= sr_do_ioctl(cd
, &cgc
);
420 memcpy(mcn
->medium_catalog_number
, buffer
+ 9, 13);
421 mcn
->medium_catalog_number
[13] = 0;
427 int sr_reset(struct cdrom_device_info
*cdi
)
432 int sr_select_speed(struct cdrom_device_info
*cdi
, int speed
)
434 Scsi_CD
*cd
= cdi
->handle
;
435 struct packet_command cgc
;
438 speed
= 0xffff; /* set to max */
440 speed
*= 177; /* Nx to kbyte/s */
442 memset(&cgc
, 0, sizeof(struct packet_command
));
443 cgc
.cmd
[0] = GPCMD_SET_SPEED
; /* SET CD SPEED */
444 cgc
.cmd
[2] = (speed
>> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
445 cgc
.cmd
[3] = speed
& 0xff; /* LSB */
446 cgc
.data_direction
= DMA_NONE
;
447 cgc
.timeout
= IOCTL_TIMEOUT
;
449 if (sr_do_ioctl(cd
, &cgc
))
454 /* ----------------------------------------------------------------------- */
455 /* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
456 /* because the generic cdrom driver does the user access stuff for us. */
457 /* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
458 /* sr_disk_status interface for the generic cdrom driver. */
460 int sr_audio_ioctl(struct cdrom_device_info
*cdi
, unsigned int cmd
, void *arg
)
463 case CDROMREADTOCHDR
:
464 return sr_read_tochdr(cdi
, arg
);
465 case CDROMREADTOCENTRY
:
466 return sr_read_tocentry(cdi
, arg
);
467 case CDROMPLAYTRKIND
:
468 return sr_play_trkind(cdi
, arg
);
474 /* -----------------------------------------------------------------------
475 * a function to read all sorts of funny cdrom sectors using the READ_CD
478 * lba: linear block address
479 * format: 0 = data (anything)
483 * 4 = data (mode 2 form1)
484 * 5 = data (mode 2 form2)
485 * blksize: 2048 | 2336 | 2340 | 2352
488 static int sr_read_cd(Scsi_CD
*cd
, unsigned char *dest
, int lba
, int format
, int blksize
)
490 struct packet_command cgc
;
493 printk("%s: sr_read_cd lba=%d format=%d blksize=%d\n",
494 cd
->cdi
.name
, lba
, format
, blksize
);
497 memset(&cgc
, 0, sizeof(struct packet_command
));
498 cgc
.cmd
[0] = GPCMD_READ_CD
; /* READ_CD */
499 cgc
.cmd
[1] = ((format
& 7) << 2);
500 cgc
.cmd
[2] = (unsigned char) (lba
>> 24) & 0xff;
501 cgc
.cmd
[3] = (unsigned char) (lba
>> 16) & 0xff;
502 cgc
.cmd
[4] = (unsigned char) (lba
>> 8) & 0xff;
503 cgc
.cmd
[5] = (unsigned char) lba
& 0xff;
520 cgc
.buflen
= blksize
;
521 cgc
.data_direction
= DMA_FROM_DEVICE
;
522 cgc
.timeout
= IOCTL_TIMEOUT
;
523 return sr_do_ioctl(cd
, &cgc
);
527 * read sectors with blocksizes other than 2048
530 static int sr_read_sector(Scsi_CD
*cd
, int lba
, int blksize
, unsigned char *dest
)
532 struct packet_command cgc
;
535 /* we try the READ CD command first... */
536 if (cd
->readcd_known
) {
537 rc
= sr_read_cd(cd
, dest
, lba
, 0, blksize
);
538 if (-EDRIVE_CANT_DO_THIS
!= rc
)
540 cd
->readcd_known
= 0;
541 printk("CDROM does'nt support READ CD (0xbe) command\n");
542 /* fall & retry the other way */
544 /* ... if this fails, we switch the blocksize using MODE SELECT */
545 if (blksize
!= cd
->device
->sector_size
) {
546 if (0 != (rc
= sr_set_blocklength(cd
, blksize
)))
550 printk("%s: sr_read_sector lba=%d blksize=%d\n", cd
->cdi
.name
, lba
, blksize
);
553 memset(&cgc
, 0, sizeof(struct packet_command
));
554 cgc
.cmd
[0] = GPCMD_READ_10
;
555 cgc
.cmd
[2] = (unsigned char) (lba
>> 24) & 0xff;
556 cgc
.cmd
[3] = (unsigned char) (lba
>> 16) & 0xff;
557 cgc
.cmd
[4] = (unsigned char) (lba
>> 8) & 0xff;
558 cgc
.cmd
[5] = (unsigned char) lba
& 0xff;
561 cgc
.buflen
= blksize
;
562 cgc
.data_direction
= DMA_FROM_DEVICE
;
563 cgc
.timeout
= IOCTL_TIMEOUT
;
564 rc
= sr_do_ioctl(cd
, &cgc
);
570 * read a sector in raw mode to check the sector format
571 * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
574 int sr_is_xa(Scsi_CD
*cd
)
576 unsigned char *raw_sector
;
582 raw_sector
= kmalloc(2048, GFP_KERNEL
| SR_GFP_DMA(cd
));
585 if (0 == sr_read_sector(cd
, cd
->ms_offset
+ 16,
586 CD_FRAMESIZE_RAW1
, raw_sector
)) {
587 is_xa
= (raw_sector
[3] == 0x02) ? 1 : 0;
589 /* read a raw sector failed for some reason. */
594 printk("%s: sr_is_xa: %d\n", cd
->cdi
.name
, is_xa
);