- pre5:
[davej-history.git] / drivers / scsi / sr_ioctl.c
blob82869fb81cabc64fe6957ff2c3de4ebcdcf7ea7a
1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/mm.h>
4 #include <linux/fs.h>
5 #include <asm/uaccess.h>
6 #include <linux/errno.h>
7 #include <linux/string.h>
9 #include <linux/blk.h>
10 #include <linux/blkpg.h>
11 #include "scsi.h"
12 #include "hosts.h"
13 #include <scsi/scsi_ioctl.h>
15 #include <linux/cdrom.h>
16 #include "sr.h"
18 #if 0
19 #define DEBUG
20 #endif
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)
39 Scsi_Request *SRpnt;
40 Scsi_Device *SDev;
41 struct request *req;
42 int result, err = 0, retries = 0;
43 char *bounce_buffer;
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.");
56 return -ENOMEM;
58 memcpy(bounce_buffer, (char *) buffer, buflength);
59 buffer = bounce_buffer;
61 retry:
62 if (!scsi_block_when_processing_errors(SDev))
63 return -ENODEV;
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) {
81 case UNIT_ATTENTION:
82 scsi_CDs[target].device->changed = 1;
83 if (!quiet)
84 printk(KERN_INFO "sr%d: disc change detected.\n", target);
85 if (retries++ < 10)
86 goto retry;
87 err = -ENOMEDIUM;
88 break;
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 */
93 if (!quiet)
94 printk(KERN_INFO "sr%d: CDROM not ready yet.\n", target);
95 if (retries++ < 10) {
96 /* sleep 2 sec and try again */
97 scsi_sleep(2 * HZ);
98 goto retry;
99 } else {
100 /* 20 secs are enough? */
101 err = -ENOMEDIUM;
102 break;
105 if (!quiet)
106 printk(KERN_INFO "sr%d: CDROM not ready. Make sure there is a disc in the drive.\n", target);
107 #ifdef DEBUG
108 print_req_sense("sr", SRpnt);
109 #endif
110 err = -ENOMEDIUM;
111 break;
112 case ILLEGAL_REQUEST:
113 if (!quiet)
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;
120 } else {
121 err = -EINVAL;
123 #ifdef DEBUG
124 print_command(sr_cmd);
125 print_req_sense("sr", SRpnt);
126 #endif
127 break;
128 default:
129 printk(KERN_ERR "sr%d: CDROM (ioctl) error, command: ", target);
130 print_command(sr_cmd);
131 print_req_sense("sr", SRpnt);
132 err = -EIO;
135 result = SRpnt->sr_result;
136 /* Wake up a process waiting for device */
137 scsi_release_request(SRpnt);
138 SRpnt = NULL;
139 return err;
142 /* ---------------------------------------------------------------------- */
143 /* interface to cdrom.c */
145 static int test_unit_ready(int minor)
147 u_char sr_cmd[10];
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)
157 u_char sr_cmd[10];
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 */
178 return -EINVAL;
180 if (0 == test_unit_ready(MINOR(cdi->dev)))
181 return CDS_DISC_OK;
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))
200 return CDS_NO_INFO;
201 if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
202 have_datatracks = 1;
203 break;
206 if (!have_datatracks)
207 return CDS_AUDIO;
209 if (scsi_CDs[MINOR(cdi->dev)].xa_flag)
210 return CDS_XA_2_1;
211 else
212 return CDS_DATA_1;
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);
222 return 0;
225 int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
227 u_char sr_cmd[10];
228 char buffer[32];
229 int result;
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;
236 sr_cmd[6] = 0;
237 sr_cmd[7] = 0;
238 sr_cmd[8] = 24;
239 sr_cmd[9] = 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;
246 return result;
249 int sr_reset(struct cdrom_device_info *cdi)
251 invalidate_buffers(cdi->dev);
252 return 0;
255 int sr_select_speed(struct cdrom_device_info *cdi, int speed)
257 u_char sr_cmd[MAX_COMMAND_SIZE];
259 if (speed == 0)
260 speed = 0xffff; /* set to max */
261 else
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))
271 return -EIO;
272 return 0;
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)
283 u_char sr_cmd[10];
284 int result, target = MINOR(cdi->dev);
285 unsigned char buffer[32];
287 memset(sr_cmd, 0, sizeof(sr_cmd));
289 switch (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];
304 break;
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];
327 } else
328 tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
329 + buffer[10]) << 8) + buffer[11];
331 break;
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);
345 break;
348 default:
349 return -EINVAL;
352 #if 0
353 if (result)
354 printk("DEBUG: sr_audio: result for ioctl %x: %x\n", cmd, result);
355 #endif
357 return result;
360 /* -----------------------------------------------------------------------
361 * a function to read all sorts of funny cdrom sectors using the READ_CD
362 * scsi-3 mmc command
364 * lba: linear block address
365 * format: 0 = data (anything)
366 * 1 = audio
367 * 2 = data (mode 1)
368 * 3 = data (mode 2)
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];
378 #ifdef DEBUG
379 printk("sr%d: sr_read_cd lba=%d format=%d blksize=%d\n",
380 minor, lba, format, blksize);
381 #endif
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;
390 cmd[8] = 1;
391 switch (blksize) {
392 case 2336:
393 cmd[9] = 0x58;
394 break;
395 case 2340:
396 cmd[9] = 0x78;
397 break;
398 case 2352:
399 cmd[9] = 0xf8;
400 break;
401 default:
402 cmd[9] = 0x10;
403 break;
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 */
415 int rc;
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)
421 return 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)))
429 return rc;
431 #ifdef DEBUG
432 printk("sr%d: sr_read_sector lba=%d blksize=%d\n", minor, lba, blksize);
433 #endif
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;
442 cmd[8] = 1;
443 rc = sr_do_ioctl(minor, cmd, dest, blksize, 0, SCSI_DATA_READ);
445 return rc;
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;
456 int is_xa;
458 if (!xa_test)
459 return 0;
461 raw_sector = (unsigned char *) scsi_malloc(2048 + 512);
462 if (!raw_sector)
463 return -ENOMEM;
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;
467 } else {
468 /* read a raw sector failed for some reason. */
469 is_xa = -1;
471 scsi_free(raw_sector, 2048 + 512);
472 #ifdef DEBUG
473 printk("sr%d: sr_is_xa: %d\n", minor, is_xa);
474 #endif
475 return is_xa;
478 int sr_dev_ioctl(struct cdrom_device_info *cdi,
479 unsigned int cmd, unsigned long arg)
481 int target;
483 target = MINOR(cdi->dev);
485 switch (cmd) {
486 case BLKROSET:
487 case BLKROGET:
488 case BLKRASET:
489 case BLKRAGET:
490 case BLKFLSBUF:
491 case BLKSSZGET:
492 return blk_ioctl(cdi->dev, cmd, arg);
494 default:
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
503 * of the file.
504 * ---------------------------------------------------------------------------
505 * Local variables:
506 * c-indent-level: 4
507 * c-brace-imaginary-offset: 0
508 * c-brace-offset: -4
509 * c-argdecl-indent: 4
510 * c-label-offset: -4
511 * c-continued-statement-offset: 4
512 * c-continued-brace-offset: 0
513 * indent-tabs-mode: nil
514 * tab-width: 8
515 * End: