1 #include <linux/kernel.h>
2 #include <linux/sched.h>
5 #include <asm/uaccess.h>
6 #include <linux/errno.h>
7 #include <linux/string.h>
10 #include <linux/blkpg.h>
13 #include <scsi/scsi_ioctl.h>
15 #include <linux/cdrom.h>
22 /* The sr_is_xa() seems to trigger firmware bugs with some drives :-(
23 * It is off by default and can be turned on with this module parameter */
24 static int xa_test
= 0;
26 extern void get_sectorsize(int);
28 #define IOCTL_RETRIES 3
29 /* The CDROM is fairly slow, so we need a little extra time */
30 /* In fact, it is very slow if it has to spin up first */
31 #define IOCTL_TIMEOUT 30*HZ
33 /* We do our own retries because we want to know what the specific
34 error code is. Normally the UNIT_ATTENTION code will automatically
35 clear after one error */
37 int sr_do_ioctl(int target
, unsigned char *sr_cmd
, void *buffer
, unsigned buflength
, int quiet
, int readwrite
)
42 int result
, err
= 0, retries
= 0;
45 SDev
= scsi_CDs
[target
].device
;
46 SRpnt
= scsi_allocate_request(scsi_CDs
[target
].device
);
47 SRpnt
->sr_data_direction
= readwrite
;
49 /* use ISA DMA buffer if necessary */
50 SRpnt
->sr_request
.buffer
= buffer
;
51 if (buffer
&& SRpnt
->sr_host
->unchecked_isa_dma
&&
52 (virt_to_phys(buffer
) + buflength
- 1 > ISA_DMA_THRESHOLD
)) {
53 bounce_buffer
= (char *) scsi_malloc((buflength
+ 511) & ~511);
54 if (bounce_buffer
== NULL
) {
55 printk("SCSI DMA pool exhausted.");
58 memcpy(bounce_buffer
, (char *) buffer
, buflength
);
59 buffer
= bounce_buffer
;
62 if (!scsi_block_when_processing_errors(SDev
))
66 scsi_wait_req(SRpnt
, (void *) sr_cmd
, (void *) buffer
, buflength
,
67 IOCTL_TIMEOUT
, IOCTL_RETRIES
);
69 req
= &SRpnt
->sr_request
;
70 if (SRpnt
->sr_buffer
&& req
->buffer
&& SRpnt
->sr_buffer
!= req
->buffer
) {
71 memcpy(req
->buffer
, SRpnt
->sr_buffer
, SRpnt
->sr_bufflen
);
72 scsi_free(SRpnt
->sr_buffer
, (SRpnt
->sr_bufflen
+ 511) & ~511);
73 SRpnt
->sr_buffer
= req
->buffer
;
76 result
= SRpnt
->sr_result
;
78 /* Minimal error checking. Ignore cases we know about, and report the rest. */
79 if (driver_byte(result
) != 0) {
80 switch (SRpnt
->sr_sense_buffer
[2] & 0xf) {
82 scsi_CDs
[target
].device
->changed
= 1;
84 printk(KERN_INFO
"sr%d: disc change detected.\n", target
);
89 case NOT_READY
: /* This happens if there is no disc in drive */
90 if (SRpnt
->sr_sense_buffer
[12] == 0x04 &&
91 SRpnt
->sr_sense_buffer
[13] == 0x01) {
92 /* sense: Logical unit is in process of becoming ready */
94 printk(KERN_INFO
"sr%d: CDROM not ready yet.\n", target
);
96 /* sleep 2 sec and try again */
100 /* 20 secs are enough? */
106 printk(KERN_INFO
"sr%d: CDROM not ready. Make sure there is a disc in the drive.\n", target
);
108 print_req_sense("sr", SRpnt
);
112 case ILLEGAL_REQUEST
:
114 printk(KERN_ERR
"sr%d: CDROM (ioctl) reports ILLEGAL "
115 "REQUEST.\n", target
);
116 if (SRpnt
->sr_sense_buffer
[12] == 0x20 &&
117 SRpnt
->sr_sense_buffer
[13] == 0x00) {
118 /* sense: Invalid command operation code */
119 err
= -EDRIVE_CANT_DO_THIS
;
124 print_command(sr_cmd
);
125 print_req_sense("sr", SRpnt
);
129 printk(KERN_ERR
"sr%d: CDROM (ioctl) error, command: ", target
);
130 print_command(sr_cmd
);
131 print_req_sense("sr", SRpnt
);
135 result
= SRpnt
->sr_result
;
136 /* Wake up a process waiting for device */
137 scsi_release_request(SRpnt
);
142 /* ---------------------------------------------------------------------- */
143 /* interface to cdrom.c */
145 static int test_unit_ready(int minor
)
149 sr_cmd
[0] = GPCMD_TEST_UNIT_READY
;
150 sr_cmd
[1] = ((scsi_CDs
[minor
].device
->lun
) << 5);
151 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[4] = sr_cmd
[5] = 0;
152 return sr_do_ioctl(minor
, sr_cmd
, NULL
, 0, 1, SCSI_DATA_NONE
);
155 int sr_tray_move(struct cdrom_device_info
*cdi
, int pos
)
159 sr_cmd
[0] = GPCMD_START_STOP_UNIT
;
160 sr_cmd
[1] = ((scsi_CDs
[MINOR(cdi
->dev
)].device
->lun
) << 5);
161 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[5] = 0;
162 sr_cmd
[4] = (pos
== 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
164 return sr_do_ioctl(MINOR(cdi
->dev
), sr_cmd
, NULL
, 0, 0, SCSI_DATA_NONE
);
167 int sr_lock_door(struct cdrom_device_info
*cdi
, int lock
)
169 return scsi_ioctl(scsi_CDs
[MINOR(cdi
->dev
)].device
,
170 lock
? SCSI_IOCTL_DOORLOCK
: SCSI_IOCTL_DOORUNLOCK
,
174 int sr_drive_status(struct cdrom_device_info
*cdi
, int slot
)
176 if (CDSL_CURRENT
!= slot
) {
177 /* we have no changer support */
180 if (0 == test_unit_ready(MINOR(cdi
->dev
)))
183 return CDS_TRAY_OPEN
;
186 int sr_disk_status(struct cdrom_device_info
*cdi
)
188 struct cdrom_tochdr toc_h
;
189 struct cdrom_tocentry toc_e
;
190 int i
, rc
, have_datatracks
= 0;
192 /* look for data tracks */
193 if (0 != (rc
= sr_audio_ioctl(cdi
, CDROMREADTOCHDR
, &toc_h
)))
194 return (rc
== -ENOMEDIUM
) ? CDS_NO_DISC
: CDS_NO_INFO
;
196 for (i
= toc_h
.cdth_trk0
; i
<= toc_h
.cdth_trk1
; i
++) {
197 toc_e
.cdte_track
= i
;
198 toc_e
.cdte_format
= CDROM_LBA
;
199 if (sr_audio_ioctl(cdi
, CDROMREADTOCENTRY
, &toc_e
))
201 if (toc_e
.cdte_ctrl
& CDROM_DATA_TRACK
) {
206 if (!have_datatracks
)
209 if (scsi_CDs
[MINOR(cdi
->dev
)].xa_flag
)
215 int sr_get_last_session(struct cdrom_device_info
*cdi
,
216 struct cdrom_multisession
*ms_info
)
218 ms_info
->addr
.lba
= scsi_CDs
[MINOR(cdi
->dev
)].ms_offset
;
219 ms_info
->xa_flag
= scsi_CDs
[MINOR(cdi
->dev
)].xa_flag
||
220 (scsi_CDs
[MINOR(cdi
->dev
)].ms_offset
> 0);
225 int sr_get_mcn(struct cdrom_device_info
*cdi
, struct cdrom_mcn
*mcn
)
231 sr_cmd
[0] = GPCMD_READ_SUBCHANNEL
;
232 sr_cmd
[1] = ((scsi_CDs
[MINOR(cdi
->dev
)].device
->lun
) << 5);
233 sr_cmd
[2] = 0x40; /* I do want the subchannel info */
234 sr_cmd
[3] = 0x02; /* Give me medium catalog number info */
235 sr_cmd
[4] = sr_cmd
[5] = 0;
241 result
= sr_do_ioctl(MINOR(cdi
->dev
), sr_cmd
, buffer
, 24, 0, SCSI_DATA_READ
);
243 memcpy(mcn
->medium_catalog_number
, buffer
+ 9, 13);
244 mcn
->medium_catalog_number
[13] = 0;
249 int sr_reset(struct cdrom_device_info
*cdi
)
251 invalidate_buffers(cdi
->dev
);
255 int sr_select_speed(struct cdrom_device_info
*cdi
, int speed
)
257 u_char sr_cmd
[MAX_COMMAND_SIZE
];
260 speed
= 0xffff; /* set to max */
262 speed
*= 177; /* Nx to kbyte/s */
264 memset(sr_cmd
, 0, MAX_COMMAND_SIZE
);
265 sr_cmd
[0] = GPCMD_SET_SPEED
; /* SET CD SPEED */
266 sr_cmd
[1] = (scsi_CDs
[MINOR(cdi
->dev
)].device
->lun
) << 5;
267 sr_cmd
[2] = (speed
>> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
268 sr_cmd
[3] = speed
& 0xff; /* LSB */
270 if (sr_do_ioctl(MINOR(cdi
->dev
), sr_cmd
, NULL
, 0, 0, SCSI_DATA_NONE
))
275 /* ----------------------------------------------------------------------- */
276 /* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
277 /* becauce the generic cdrom driver does the user access stuff for us. */
278 /* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
279 /* sr_disk_status interface for the generic cdrom driver. */
281 int sr_audio_ioctl(struct cdrom_device_info
*cdi
, unsigned int cmd
, void *arg
)
284 int result
, target
= MINOR(cdi
->dev
);
285 unsigned char buffer
[32];
287 memset(sr_cmd
, 0, sizeof(sr_cmd
));
290 case CDROMREADTOCHDR
:
292 struct cdrom_tochdr
*tochdr
= (struct cdrom_tochdr
*) arg
;
294 sr_cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
295 sr_cmd
[1] = ((scsi_CDs
[target
].device
->lun
) << 5);
296 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[4] = sr_cmd
[5] = 0;
297 sr_cmd
[8] = 12; /* LSB of length */
299 result
= sr_do_ioctl(target
, sr_cmd
, buffer
, 12, 1, SCSI_DATA_READ
);
301 tochdr
->cdth_trk0
= buffer
[2];
302 tochdr
->cdth_trk1
= buffer
[3];
307 case CDROMREADTOCENTRY
:
309 struct cdrom_tocentry
*tocentry
= (struct cdrom_tocentry
*) arg
;
311 sr_cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
312 sr_cmd
[1] = ((scsi_CDs
[target
].device
->lun
) << 5) |
313 (tocentry
->cdte_format
== CDROM_MSF
? 0x02 : 0);
314 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[4] = sr_cmd
[5] = 0;
315 sr_cmd
[6] = tocentry
->cdte_track
;
316 sr_cmd
[8] = 12; /* LSB of length */
318 result
= sr_do_ioctl(target
, sr_cmd
, buffer
, 12, 0, SCSI_DATA_READ
);
320 tocentry
->cdte_ctrl
= buffer
[5] & 0xf;
321 tocentry
->cdte_adr
= buffer
[5] >> 4;
322 tocentry
->cdte_datamode
= (tocentry
->cdte_ctrl
& 0x04) ? 1 : 0;
323 if (tocentry
->cdte_format
== CDROM_MSF
) {
324 tocentry
->cdte_addr
.msf
.minute
= buffer
[9];
325 tocentry
->cdte_addr
.msf
.second
= buffer
[10];
326 tocentry
->cdte_addr
.msf
.frame
= buffer
[11];
328 tocentry
->cdte_addr
.lba
= (((((buffer
[8] << 8) + buffer
[9]) << 8)
329 + buffer
[10]) << 8) + buffer
[11];
334 case CDROMPLAYTRKIND
: {
335 struct cdrom_ti
* ti
= (struct cdrom_ti
*)arg
;
337 sr_cmd
[0] = GPCMD_PLAYAUDIO_TI
;
338 sr_cmd
[1] = scsi_CDs
[target
].device
->lun
<< 5;
339 sr_cmd
[4] = ti
->cdti_trk0
;
340 sr_cmd
[5] = ti
->cdti_ind0
;
341 sr_cmd
[7] = ti
->cdti_trk1
;
342 sr_cmd
[8] = ti
->cdti_ind1
;
344 result
= sr_do_ioctl(target
, sr_cmd
, NULL
, 255, 0, SCSI_DATA_NONE
);
354 printk("DEBUG: sr_audio: result for ioctl %x: %x\n", cmd
, result
);
360 /* -----------------------------------------------------------------------
361 * a function to read all sorts of funny cdrom sectors using the READ_CD
364 * lba: linear block address
365 * format: 0 = data (anything)
369 * 4 = data (mode 2 form1)
370 * 5 = data (mode 2 form2)
371 * blksize: 2048 | 2336 | 2340 | 2352
374 int sr_read_cd(int minor
, unsigned char *dest
, int lba
, int format
, int blksize
)
376 unsigned char cmd
[MAX_COMMAND_SIZE
];
379 printk("sr%d: sr_read_cd lba=%d format=%d blksize=%d\n",
380 minor
, lba
, format
, blksize
);
383 memset(cmd
, 0, MAX_COMMAND_SIZE
);
384 cmd
[0] = GPCMD_READ_CD
; /* READ_CD */
385 cmd
[1] = (scsi_CDs
[minor
].device
->lun
<< 5) | ((format
& 7) << 2);
386 cmd
[2] = (unsigned char) (lba
>> 24) & 0xff;
387 cmd
[3] = (unsigned char) (lba
>> 16) & 0xff;
388 cmd
[4] = (unsigned char) (lba
>> 8) & 0xff;
389 cmd
[5] = (unsigned char) lba
& 0xff;
405 return sr_do_ioctl(minor
, cmd
, dest
, blksize
, 0, SCSI_DATA_READ
);
409 * read sectors with blocksizes other than 2048
412 int sr_read_sector(int minor
, int lba
, int blksize
, unsigned char *dest
)
414 unsigned char cmd
[MAX_COMMAND_SIZE
]; /* the scsi-command */
417 /* we try the READ CD command first... */
418 if (scsi_CDs
[minor
].readcd_known
) {
419 rc
= sr_read_cd(minor
, dest
, lba
, 0, blksize
);
420 if (-EDRIVE_CANT_DO_THIS
!= rc
)
422 scsi_CDs
[minor
].readcd_known
= 0;
423 printk("CDROM does'nt support READ CD (0xbe) command\n");
424 /* fall & retry the other way */
426 /* ... if this fails, we switch the blocksize using MODE SELECT */
427 if (blksize
!= scsi_CDs
[minor
].device
->sector_size
) {
428 if (0 != (rc
= sr_set_blocklength(minor
, blksize
)))
432 printk("sr%d: sr_read_sector lba=%d blksize=%d\n", minor
, lba
, blksize
);
435 memset(cmd
, 0, MAX_COMMAND_SIZE
);
436 cmd
[0] = GPCMD_READ_10
;
437 cmd
[1] = (scsi_CDs
[minor
].device
->lun
<< 5);
438 cmd
[2] = (unsigned char) (lba
>> 24) & 0xff;
439 cmd
[3] = (unsigned char) (lba
>> 16) & 0xff;
440 cmd
[4] = (unsigned char) (lba
>> 8) & 0xff;
441 cmd
[5] = (unsigned char) lba
& 0xff;
443 rc
= sr_do_ioctl(minor
, cmd
, dest
, blksize
, 0, SCSI_DATA_READ
);
449 * read a sector in raw mode to check the sector format
450 * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
453 int sr_is_xa(int minor
)
455 unsigned char *raw_sector
;
461 raw_sector
= (unsigned char *) scsi_malloc(2048 + 512);
464 if (0 == sr_read_sector(minor
, scsi_CDs
[minor
].ms_offset
+ 16,
465 CD_FRAMESIZE_RAW1
, raw_sector
)) {
466 is_xa
= (raw_sector
[3] == 0x02) ? 1 : 0;
468 /* read a raw sector failed for some reason. */
471 scsi_free(raw_sector
, 2048 + 512);
473 printk("sr%d: sr_is_xa: %d\n", minor
, is_xa
);
478 int sr_dev_ioctl(struct cdrom_device_info
*cdi
,
479 unsigned int cmd
, unsigned long arg
)
483 target
= MINOR(cdi
->dev
);
492 return blk_ioctl(cdi
->dev
, cmd
, arg
);
495 return scsi_ioctl(scsi_CDs
[target
].device
, cmd
, (void *) arg
);
500 * Overrides for Emacs so that we follow Linus's tabbing style.
501 * Emacs will notice this stuff at the end of the file and automatically
502 * adjust the settings for this buffer only. This must remain at the end
504 * ---------------------------------------------------------------------------
507 * c-brace-imaginary-offset: 0
509 * c-argdecl-indent: 4
511 * c-continued-statement-offset: 4
512 * c-continued-brace-offset: 0
513 * indent-tabs-mode: nil