- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / sr_ioctl.c
blobcee7d79ba46f6704e5fe3b9003a493248046c2ec
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 /* 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;
41 u_char sr_cmd[10];
42 int ntracks, ret;
44 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCHDR, &tochdr)))
45 return ret;
47 ntracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
49 if (ti->cdti_trk1 == ntracks)
50 ti->cdti_trk1 = CDROM_LEADOUT;
51 else
52 ti->cdti_trk1 ++;
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)))
60 return ret;
61 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &trk1_te)))
62 return ret;
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)
80 Scsi_Request *SRpnt;
81 Scsi_Device *SDev;
82 struct request *req;
83 int result, err = 0, retries = 0;
84 char *bounce_buffer;
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.");
97 return -ENOMEM;
99 memcpy(bounce_buffer, (char *) buffer, buflength);
100 buffer = bounce_buffer;
102 retry:
103 if (!scsi_block_when_processing_errors(SDev))
104 return -ENODEV;
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) {
122 case UNIT_ATTENTION:
123 scsi_CDs[target].device->changed = 1;
124 if (!quiet)
125 printk(KERN_INFO "sr%d: disc change detected.\n", target);
126 if (retries++ < 10)
127 goto retry;
128 err = -ENOMEDIUM;
129 break;
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 */
134 if (!quiet)
135 printk(KERN_INFO "sr%d: CDROM not ready yet.\n", target);
136 if (retries++ < 10) {
137 /* sleep 2 sec and try again */
138 scsi_sleep(2 * HZ);
139 goto retry;
140 } else {
141 /* 20 secs are enough? */
142 err = -ENOMEDIUM;
143 break;
146 if (!quiet)
147 printk(KERN_INFO "sr%d: CDROM not ready. Make sure there is a disc in the drive.\n", target);
148 #ifdef DEBUG
149 print_req_sense("sr", SRpnt);
150 #endif
151 err = -ENOMEDIUM;
152 break;
153 case ILLEGAL_REQUEST:
154 if (!quiet)
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;
161 } else {
162 err = -EINVAL;
164 #ifdef DEBUG
165 print_command(sr_cmd);
166 print_req_sense("sr", SRpnt);
167 #endif
168 break;
169 default:
170 printk(KERN_ERR "sr%d: CDROM (ioctl) error, command: ", target);
171 print_command(sr_cmd);
172 print_req_sense("sr", SRpnt);
173 err = -EIO;
177 if (sense)
178 memcpy(sense, SRpnt->sr_sense_buffer, sizeof(*sense));
180 /* Wake up a process waiting for device */
181 scsi_release_request(SRpnt);
182 SRpnt = NULL;
183 return err;
186 /* ---------------------------------------------------------------------- */
187 /* interface to cdrom.c */
189 static int test_unit_ready(int minor)
191 u_char sr_cmd[10];
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)
201 u_char sr_cmd[10];
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 */
222 return -EINVAL;
224 if (0 == test_unit_ready(MINOR(cdi->dev)))
225 return CDS_DISC_OK;
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))
244 return CDS_NO_INFO;
245 if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
246 have_datatracks = 1;
247 break;
250 if (!have_datatracks)
251 return CDS_AUDIO;
253 if (scsi_CDs[MINOR(cdi->dev)].xa_flag)
254 return CDS_XA_2_1;
255 else
256 return CDS_DATA_1;
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);
266 return 0;
269 int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
271 u_char sr_cmd[10];
272 char buffer[32];
273 int result;
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;
280 sr_cmd[6] = 0;
281 sr_cmd[7] = 0;
282 sr_cmd[8] = 24;
283 sr_cmd[9] = 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;
290 return result;
293 int sr_reset(struct cdrom_device_info *cdi)
295 invalidate_buffers(cdi->dev);
296 return 0;
299 int sr_select_speed(struct cdrom_device_info *cdi, int speed)
301 u_char sr_cmd[MAX_COMMAND_SIZE];
303 if (speed == 0)
304 speed = 0xffff; /* set to max */
305 else
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))
315 return -EIO;
316 return 0;
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)
327 u_char sr_cmd[10];
328 int result, target = MINOR(cdi->dev);
329 unsigned char buffer[32];
331 memset(sr_cmd, 0, sizeof(sr_cmd));
333 switch (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];
348 break;
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];
371 } else
372 tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
373 + buffer[10]) << 8) + buffer[11];
375 break;
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);
392 break;
395 default:
396 return -EINVAL;
399 #if 0
400 if (result)
401 printk("DEBUG: sr_audio: result for ioctl %x: %x\n", cmd, result);
402 #endif
404 return result;
407 /* -----------------------------------------------------------------------
408 * a function to read all sorts of funny cdrom sectors using the READ_CD
409 * scsi-3 mmc command
411 * lba: linear block address
412 * format: 0 = data (anything)
413 * 1 = audio
414 * 2 = data (mode 1)
415 * 3 = data (mode 2)
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];
425 #ifdef DEBUG
426 printk("sr%d: sr_read_cd lba=%d format=%d blksize=%d\n",
427 minor, lba, format, blksize);
428 #endif
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;
437 cmd[8] = 1;
438 switch (blksize) {
439 case 2336:
440 cmd[9] = 0x58;
441 break;
442 case 2340:
443 cmd[9] = 0x78;
444 break;
445 case 2352:
446 cmd[9] = 0xf8;
447 break;
448 default:
449 cmd[9] = 0x10;
450 break;
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 */
462 int rc;
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)
468 return 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)))
476 return rc;
478 #ifdef DEBUG
479 printk("sr%d: sr_read_sector lba=%d blksize=%d\n", minor, lba, blksize);
480 #endif
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;
489 cmd[8] = 1;
490 rc = sr_do_ioctl(minor, cmd, dest, blksize, 0, SCSI_DATA_READ, NULL);
492 return rc;
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;
503 int is_xa;
505 if (!xa_test)
506 return 0;
508 raw_sector = (unsigned char *) scsi_malloc(2048 + 512);
509 if (!raw_sector)
510 return -ENOMEM;
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;
514 } else {
515 /* read a raw sector failed for some reason. */
516 is_xa = -1;
518 scsi_free(raw_sector, 2048 + 512);
519 #ifdef DEBUG
520 printk("sr%d: sr_is_xa: %d\n", minor, is_xa);
521 #endif
522 return is_xa;
525 int sr_dev_ioctl(struct cdrom_device_info *cdi,
526 unsigned int cmd, unsigned long arg)
528 int target;
530 target = MINOR(cdi->dev);
532 switch (cmd) {
533 case BLKROSET:
534 case BLKROGET:
535 case BLKRASET:
536 case BLKRAGET:
537 case BLKFLSBUF:
538 case BLKSSZGET:
539 return blk_ioctl(cdi->dev, cmd, arg);
541 default:
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
550 * of the file.
551 * ---------------------------------------------------------------------------
552 * Local variables:
553 * c-indent-level: 4
554 * c-brace-imaginary-offset: 0
555 * c-brace-offset: -4
556 * c-argdecl-indent: 4
557 * c-label-offset: -4
558 * c-continued-statement-offset: 4
559 * c-continued-brace-offset: 0
560 * indent-tabs-mode: nil
561 * tab-width: 8
562 * End: