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 /* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command. When these drives
34 are emulating a SCSI device via the idescsi module, they need to have
35 CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
37 static int sr_fake_playtrkind(struct cdrom_device_info
*cdi
, struct cdrom_ti
*ti
)
39 struct cdrom_tocentry trk0_te
, trk1_te
;
40 struct cdrom_tochdr tochdr
;
44 if ((ret
= sr_audio_ioctl(cdi
, CDROMREADTOCHDR
, &tochdr
)))
47 ntracks
= tochdr
.cdth_trk1
- tochdr
.cdth_trk0
+ 1;
49 if (ti
->cdti_trk1
== ntracks
)
50 ti
->cdti_trk1
= CDROM_LEADOUT
;
54 trk0_te
.cdte_track
= ti
->cdti_trk0
;
55 trk0_te
.cdte_format
= CDROM_MSF
;
56 trk1_te
.cdte_track
= ti
->cdti_trk1
;
57 trk1_te
.cdte_format
= CDROM_MSF
;
59 if ((ret
= sr_audio_ioctl(cdi
, CDROMREADTOCENTRY
, &trk0_te
)))
61 if ((ret
= sr_audio_ioctl(cdi
, CDROMREADTOCENTRY
, &trk1_te
)))
64 sr_cmd
[0] = GPCMD_PLAY_AUDIO_MSF
;
65 sr_cmd
[3] = trk0_te
.cdte_addr
.msf
.minute
;
66 sr_cmd
[4] = trk0_te
.cdte_addr
.msf
.second
;
67 sr_cmd
[5] = trk0_te
.cdte_addr
.msf
.frame
;
68 sr_cmd
[6] = trk1_te
.cdte_addr
.msf
.minute
;
69 sr_cmd
[7] = trk1_te
.cdte_addr
.msf
.second
;
70 sr_cmd
[8] = trk1_te
.cdte_addr
.msf
.frame
;
71 return sr_do_ioctl(MINOR(cdi
->dev
), sr_cmd
, NULL
, 0, 0, SCSI_DATA_NONE
, NULL
);
74 /* We do our own retries because we want to know what the specific
75 error code is. Normally the UNIT_ATTENTION code will automatically
76 clear after one error */
78 int sr_do_ioctl(int target
, unsigned char *sr_cmd
, void *buffer
, unsigned buflength
, int quiet
, int readwrite
, struct request_sense
*sense
)
83 int result
, err
= 0, retries
= 0;
86 SDev
= scsi_CDs
[target
].device
;
87 SRpnt
= scsi_allocate_request(scsi_CDs
[target
].device
);
88 SRpnt
->sr_data_direction
= readwrite
;
90 /* use ISA DMA buffer if necessary */
91 SRpnt
->sr_request
.buffer
= buffer
;
92 if (buffer
&& SRpnt
->sr_host
->unchecked_isa_dma
&&
93 (virt_to_phys(buffer
) + buflength
- 1 > ISA_DMA_THRESHOLD
)) {
94 bounce_buffer
= (char *) scsi_malloc((buflength
+ 511) & ~511);
95 if (bounce_buffer
== NULL
) {
96 printk("SCSI DMA pool exhausted.");
99 memcpy(bounce_buffer
, (char *) buffer
, buflength
);
100 buffer
= bounce_buffer
;
103 if (!scsi_block_when_processing_errors(SDev
))
107 scsi_wait_req(SRpnt
, (void *) sr_cmd
, (void *) buffer
, buflength
,
108 IOCTL_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 scsi_free(SRpnt
->sr_buffer
, (SRpnt
->sr_bufflen
+ 511) & ~511);
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) {
123 scsi_CDs
[target
].device
->changed
= 1;
125 printk(KERN_INFO
"sr%d: disc change detected.\n", target
);
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
"sr%d: CDROM not ready yet.\n", target
);
136 if (retries
++ < 10) {
137 /* sleep 2 sec and try again */
141 /* 20 secs are enough? */
147 printk(KERN_INFO
"sr%d: CDROM not ready. Make sure there is a disc in the drive.\n", target
);
149 print_req_sense("sr", SRpnt
);
153 case ILLEGAL_REQUEST
:
155 printk(KERN_ERR
"sr%d: CDROM (ioctl) reports ILLEGAL "
156 "REQUEST.\n", target
);
157 if (SRpnt
->sr_sense_buffer
[12] == 0x20 &&
158 SRpnt
->sr_sense_buffer
[13] == 0x00) {
159 /* sense: Invalid command operation code */
160 err
= -EDRIVE_CANT_DO_THIS
;
165 print_command(sr_cmd
);
166 print_req_sense("sr", SRpnt
);
170 printk(KERN_ERR
"sr%d: CDROM (ioctl) error, command: ", target
);
171 print_command(sr_cmd
);
172 print_req_sense("sr", SRpnt
);
178 memcpy(sense
, SRpnt
->sr_sense_buffer
, sizeof(*sense
));
180 /* Wake up a process waiting for device */
181 scsi_release_request(SRpnt
);
186 /* ---------------------------------------------------------------------- */
187 /* interface to cdrom.c */
189 static int test_unit_ready(int minor
)
193 sr_cmd
[0] = GPCMD_TEST_UNIT_READY
;
194 sr_cmd
[1] = ((scsi_CDs
[minor
].device
->lun
) << 5);
195 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[4] = sr_cmd
[5] = 0;
196 return sr_do_ioctl(minor
, sr_cmd
, NULL
, 0, 1, SCSI_DATA_NONE
, NULL
);
199 int sr_tray_move(struct cdrom_device_info
*cdi
, int pos
)
203 sr_cmd
[0] = GPCMD_START_STOP_UNIT
;
204 sr_cmd
[1] = ((scsi_CDs
[MINOR(cdi
->dev
)].device
->lun
) << 5);
205 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[5] = 0;
206 sr_cmd
[4] = (pos
== 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
208 return sr_do_ioctl(MINOR(cdi
->dev
), sr_cmd
, NULL
, 0, 0, SCSI_DATA_NONE
, NULL
);
211 int sr_lock_door(struct cdrom_device_info
*cdi
, int lock
)
213 return scsi_ioctl(scsi_CDs
[MINOR(cdi
->dev
)].device
,
214 lock
? SCSI_IOCTL_DOORLOCK
: SCSI_IOCTL_DOORUNLOCK
,
218 int sr_drive_status(struct cdrom_device_info
*cdi
, int slot
)
220 if (CDSL_CURRENT
!= slot
) {
221 /* we have no changer support */
224 if (0 == test_unit_ready(MINOR(cdi
->dev
)))
227 return CDS_TRAY_OPEN
;
230 int sr_disk_status(struct cdrom_device_info
*cdi
)
232 struct cdrom_tochdr toc_h
;
233 struct cdrom_tocentry toc_e
;
234 int i
, rc
, have_datatracks
= 0;
236 /* look for data tracks */
237 if (0 != (rc
= sr_audio_ioctl(cdi
, CDROMREADTOCHDR
, &toc_h
)))
238 return (rc
== -ENOMEDIUM
) ? CDS_NO_DISC
: CDS_NO_INFO
;
240 for (i
= toc_h
.cdth_trk0
; i
<= toc_h
.cdth_trk1
; i
++) {
241 toc_e
.cdte_track
= i
;
242 toc_e
.cdte_format
= CDROM_LBA
;
243 if (sr_audio_ioctl(cdi
, CDROMREADTOCENTRY
, &toc_e
))
245 if (toc_e
.cdte_ctrl
& CDROM_DATA_TRACK
) {
250 if (!have_datatracks
)
253 if (scsi_CDs
[MINOR(cdi
->dev
)].xa_flag
)
259 int sr_get_last_session(struct cdrom_device_info
*cdi
,
260 struct cdrom_multisession
*ms_info
)
262 ms_info
->addr
.lba
= scsi_CDs
[MINOR(cdi
->dev
)].ms_offset
;
263 ms_info
->xa_flag
= scsi_CDs
[MINOR(cdi
->dev
)].xa_flag
||
264 (scsi_CDs
[MINOR(cdi
->dev
)].ms_offset
> 0);
269 int sr_get_mcn(struct cdrom_device_info
*cdi
, struct cdrom_mcn
*mcn
)
275 sr_cmd
[0] = GPCMD_READ_SUBCHANNEL
;
276 sr_cmd
[1] = ((scsi_CDs
[MINOR(cdi
->dev
)].device
->lun
) << 5);
277 sr_cmd
[2] = 0x40; /* I do want the subchannel info */
278 sr_cmd
[3] = 0x02; /* Give me medium catalog number info */
279 sr_cmd
[4] = sr_cmd
[5] = 0;
285 result
= sr_do_ioctl(MINOR(cdi
->dev
), sr_cmd
, buffer
, 24, 0, SCSI_DATA_READ
, NULL
);
287 memcpy(mcn
->medium_catalog_number
, buffer
+ 9, 13);
288 mcn
->medium_catalog_number
[13] = 0;
293 int sr_reset(struct cdrom_device_info
*cdi
)
295 invalidate_buffers(cdi
->dev
);
299 int sr_select_speed(struct cdrom_device_info
*cdi
, int speed
)
301 u_char sr_cmd
[MAX_COMMAND_SIZE
];
304 speed
= 0xffff; /* set to max */
306 speed
*= 177; /* Nx to kbyte/s */
308 memset(sr_cmd
, 0, MAX_COMMAND_SIZE
);
309 sr_cmd
[0] = GPCMD_SET_SPEED
; /* SET CD SPEED */
310 sr_cmd
[1] = (scsi_CDs
[MINOR(cdi
->dev
)].device
->lun
) << 5;
311 sr_cmd
[2] = (speed
>> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
312 sr_cmd
[3] = speed
& 0xff; /* LSB */
314 if (sr_do_ioctl(MINOR(cdi
->dev
), sr_cmd
, NULL
, 0, 0, SCSI_DATA_NONE
, NULL
))
319 /* ----------------------------------------------------------------------- */
320 /* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
321 /* because the generic cdrom driver does the user access stuff for us. */
322 /* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
323 /* sr_disk_status interface for the generic cdrom driver. */
325 int sr_audio_ioctl(struct cdrom_device_info
*cdi
, unsigned int cmd
, void *arg
)
328 int result
, target
= MINOR(cdi
->dev
);
329 unsigned char buffer
[32];
331 memset(sr_cmd
, 0, sizeof(sr_cmd
));
334 case CDROMREADTOCHDR
:
336 struct cdrom_tochdr
*tochdr
= (struct cdrom_tochdr
*) arg
;
338 sr_cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
339 sr_cmd
[1] = ((scsi_CDs
[target
].device
->lun
) << 5);
340 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[4] = sr_cmd
[5] = 0;
341 sr_cmd
[8] = 12; /* LSB of length */
343 result
= sr_do_ioctl(target
, sr_cmd
, buffer
, 12, 1, SCSI_DATA_READ
, NULL
);
345 tochdr
->cdth_trk0
= buffer
[2];
346 tochdr
->cdth_trk1
= buffer
[3];
351 case CDROMREADTOCENTRY
:
353 struct cdrom_tocentry
*tocentry
= (struct cdrom_tocentry
*) arg
;
355 sr_cmd
[0] = GPCMD_READ_TOC_PMA_ATIP
;
356 sr_cmd
[1] = ((scsi_CDs
[target
].device
->lun
) << 5) |
357 (tocentry
->cdte_format
== CDROM_MSF
? 0x02 : 0);
358 sr_cmd
[2] = sr_cmd
[3] = sr_cmd
[4] = sr_cmd
[5] = 0;
359 sr_cmd
[6] = tocentry
->cdte_track
;
360 sr_cmd
[8] = 12; /* LSB of length */
362 result
= sr_do_ioctl(target
, sr_cmd
, buffer
, 12, 0, SCSI_DATA_READ
, NULL
);
364 tocentry
->cdte_ctrl
= buffer
[5] & 0xf;
365 tocentry
->cdte_adr
= buffer
[5] >> 4;
366 tocentry
->cdte_datamode
= (tocentry
->cdte_ctrl
& 0x04) ? 1 : 0;
367 if (tocentry
->cdte_format
== CDROM_MSF
) {
368 tocentry
->cdte_addr
.msf
.minute
= buffer
[9];
369 tocentry
->cdte_addr
.msf
.second
= buffer
[10];
370 tocentry
->cdte_addr
.msf
.frame
= buffer
[11];
372 tocentry
->cdte_addr
.lba
= (((((buffer
[8] << 8) + buffer
[9]) << 8)
373 + buffer
[10]) << 8) + buffer
[11];
378 case CDROMPLAYTRKIND
: {
379 struct cdrom_ti
* ti
= (struct cdrom_ti
*)arg
;
381 sr_cmd
[0] = GPCMD_PLAYAUDIO_TI
;
382 sr_cmd
[1] = scsi_CDs
[target
].device
->lun
<< 5;
383 sr_cmd
[4] = ti
->cdti_trk0
;
384 sr_cmd
[5] = ti
->cdti_ind0
;
385 sr_cmd
[7] = ti
->cdti_trk1
;
386 sr_cmd
[8] = ti
->cdti_ind1
;
388 result
= sr_do_ioctl(target
, sr_cmd
, NULL
, 0, 0, SCSI_DATA_NONE
, NULL
);
389 if (result
== -EDRIVE_CANT_DO_THIS
)
390 result
= sr_fake_playtrkind(cdi
, ti
);
401 printk("DEBUG: sr_audio: result for ioctl %x: %x\n", cmd
, result
);
407 /* -----------------------------------------------------------------------
408 * a function to read all sorts of funny cdrom sectors using the READ_CD
411 * lba: linear block address
412 * format: 0 = data (anything)
416 * 4 = data (mode 2 form1)
417 * 5 = data (mode 2 form2)
418 * blksize: 2048 | 2336 | 2340 | 2352
421 int sr_read_cd(int minor
, unsigned char *dest
, int lba
, int format
, int blksize
)
423 unsigned char cmd
[MAX_COMMAND_SIZE
];
426 printk("sr%d: sr_read_cd lba=%d format=%d blksize=%d\n",
427 minor
, lba
, format
, blksize
);
430 memset(cmd
, 0, MAX_COMMAND_SIZE
);
431 cmd
[0] = GPCMD_READ_CD
; /* READ_CD */
432 cmd
[1] = (scsi_CDs
[minor
].device
->lun
<< 5) | ((format
& 7) << 2);
433 cmd
[2] = (unsigned char) (lba
>> 24) & 0xff;
434 cmd
[3] = (unsigned char) (lba
>> 16) & 0xff;
435 cmd
[4] = (unsigned char) (lba
>> 8) & 0xff;
436 cmd
[5] = (unsigned char) lba
& 0xff;
452 return sr_do_ioctl(minor
, cmd
, dest
, blksize
, 0, SCSI_DATA_READ
, NULL
);
456 * read sectors with blocksizes other than 2048
459 int sr_read_sector(int minor
, int lba
, int blksize
, unsigned char *dest
)
461 unsigned char cmd
[MAX_COMMAND_SIZE
]; /* the scsi-command */
464 /* we try the READ CD command first... */
465 if (scsi_CDs
[minor
].readcd_known
) {
466 rc
= sr_read_cd(minor
, dest
, lba
, 0, blksize
);
467 if (-EDRIVE_CANT_DO_THIS
!= rc
)
469 scsi_CDs
[minor
].readcd_known
= 0;
470 printk("CDROM does'nt support READ CD (0xbe) command\n");
471 /* fall & retry the other way */
473 /* ... if this fails, we switch the blocksize using MODE SELECT */
474 if (blksize
!= scsi_CDs
[minor
].device
->sector_size
) {
475 if (0 != (rc
= sr_set_blocklength(minor
, blksize
)))
479 printk("sr%d: sr_read_sector lba=%d blksize=%d\n", minor
, lba
, blksize
);
482 memset(cmd
, 0, MAX_COMMAND_SIZE
);
483 cmd
[0] = GPCMD_READ_10
;
484 cmd
[1] = (scsi_CDs
[minor
].device
->lun
<< 5);
485 cmd
[2] = (unsigned char) (lba
>> 24) & 0xff;
486 cmd
[3] = (unsigned char) (lba
>> 16) & 0xff;
487 cmd
[4] = (unsigned char) (lba
>> 8) & 0xff;
488 cmd
[5] = (unsigned char) lba
& 0xff;
490 rc
= sr_do_ioctl(minor
, cmd
, dest
, blksize
, 0, SCSI_DATA_READ
, NULL
);
496 * read a sector in raw mode to check the sector format
497 * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
500 int sr_is_xa(int minor
)
502 unsigned char *raw_sector
;
508 raw_sector
= (unsigned char *) scsi_malloc(2048 + 512);
511 if (0 == sr_read_sector(minor
, scsi_CDs
[minor
].ms_offset
+ 16,
512 CD_FRAMESIZE_RAW1
, raw_sector
)) {
513 is_xa
= (raw_sector
[3] == 0x02) ? 1 : 0;
515 /* read a raw sector failed for some reason. */
518 scsi_free(raw_sector
, 2048 + 512);
520 printk("sr%d: sr_is_xa: %d\n", minor
, is_xa
);
525 int sr_dev_ioctl(struct cdrom_device_info
*cdi
,
526 unsigned int cmd
, unsigned long arg
)
530 target
= MINOR(cdi
->dev
);
539 return blk_ioctl(cdi
->dev
, cmd
, arg
);
542 return scsi_ioctl(scsi_CDs
[target
].device
, cmd
, (void *) arg
);
547 * Overrides for Emacs so that we follow Linus's tabbing style.
548 * Emacs will notice this stuff at the end of the file and automatically
549 * adjust the settings for this buffer only. This must remain at the end
551 * ---------------------------------------------------------------------------
554 * c-brace-imaginary-offset: 0
556 * c-argdecl-indent: 4
558 * c-continued-statement-offset: 4
559 * c-continued-brace-offset: 0
560 * indent-tabs-mode: nil