blob: 6e45ac3c43c5e193b146ce793feb5ad51bf6e3d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/kernel.h>
2#include <linux/sched.h>
3#include <linux/mm.h>
4#include <linux/fs.h>
5#include <linux/errno.h>
6#include <linux/string.h>
7#include <linux/blkdev.h>
8#include <linux/blkpg.h>
9#include <linux/cdrom.h>
10#include <linux/delay.h>
11#include <asm/io.h>
12#include <asm/uaccess.h>
13
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>
James Bottomley820732b2005-06-12 22:21:29 -050020#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "sr.h"
23
24#if 0
25#define DEBUG
26#endif
27
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 */
30static int xa_test = 0;
31
32module_param(xa_test, int, S_IRUGO | S_IWUSR);
33
34
35#define IOCTL_RETRIES 3
36
37/* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command. When these drives
38 are emulating a SCSI device via the idescsi module, they need to have
39 CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
40
41static int sr_fake_playtrkind(struct cdrom_device_info *cdi, struct cdrom_ti *ti)
42{
43 struct cdrom_tocentry trk0_te, trk1_te;
44 struct cdrom_tochdr tochdr;
45 struct packet_command cgc;
46 int ntracks, ret;
47
48 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCHDR, &tochdr)))
49 return ret;
50
51 ntracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
52
53 if (ti->cdti_trk1 == ntracks)
54 ti->cdti_trk1 = CDROM_LEADOUT;
55 else if (ti->cdti_trk1 != CDROM_LEADOUT)
56 ti->cdti_trk1 ++;
57
58 trk0_te.cdte_track = ti->cdti_trk0;
59 trk0_te.cdte_format = CDROM_MSF;
60 trk1_te.cdte_track = ti->cdti_trk1;
61 trk1_te.cdte_format = CDROM_MSF;
62
63 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &trk0_te)))
64 return ret;
65 if ((ret = sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &trk1_te)))
66 return ret;
67
68 memset(&cgc, 0, sizeof(struct packet_command));
69 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
70 cgc.cmd[3] = trk0_te.cdte_addr.msf.minute;
71 cgc.cmd[4] = trk0_te.cdte_addr.msf.second;
72 cgc.cmd[5] = trk0_te.cdte_addr.msf.frame;
73 cgc.cmd[6] = trk1_te.cdte_addr.msf.minute;
74 cgc.cmd[7] = trk1_te.cdte_addr.msf.second;
75 cgc.cmd[8] = trk1_te.cdte_addr.msf.frame;
76 cgc.data_direction = DMA_NONE;
77 cgc.timeout = IOCTL_TIMEOUT;
78 return sr_do_ioctl(cdi->handle, &cgc);
79}
80
81/* We do our own retries because we want to know what the specific
82 error code is. Normally the UNIT_ATTENTION code will automatically
83 clear after one error */
84
85int sr_do_ioctl(Scsi_CD *cd, struct packet_command *cgc)
86{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct scsi_device *SDev;
James Bottomley820732b2005-06-12 22:21:29 -050088 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int result, err = 0, retries = 0;
James Bottomley820732b2005-06-12 22:21:29 -050090 struct request_sense *sense = cgc->sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 SDev = cd->device;
James Bottomley820732b2005-06-12 22:21:29 -050093
94 if (!sense) {
95 sense = kmalloc(sizeof(*sense), GFP_KERNEL);
96 if (!sense) {
97 err = -ENOMEM;
98 goto out;
99 }
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 retry:
103 if (!scsi_block_when_processing_errors(SDev)) {
104 err = -ENODEV;
James Bottomley820732b2005-06-12 22:21:29 -0500105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
James Bottomley820732b2005-06-12 22:21:29 -0500108 memset(sense, 0, sizeof(*sense));
109 result = scsi_execute(SDev, cgc->cmd, cgc->data_direction,
110 cgc->buffer, cgc->buflen, (char *)sense,
111 cgc->timeout, IOCTL_RETRIES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
James Bottomley820732b2005-06-12 22:21:29 -0500113 scsi_normalize_sense((char *)sense, sizeof(*sense), &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* Minimal error checking. Ignore cases we know about, and report the rest. */
116 if (driver_byte(result) != 0) {
James Bottomley820732b2005-06-12 22:21:29 -0500117 switch (sshdr.sense_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 case UNIT_ATTENTION:
119 SDev->changed = 1;
120 if (!cgc->quiet)
121 printk(KERN_INFO "%s: disc change detected.\n", cd->cdi.name);
122 if (retries++ < 10)
123 goto retry;
124 err = -ENOMEDIUM;
125 break;
126 case NOT_READY: /* This happens if there is no disc in drive */
James Bottomley820732b2005-06-12 22:21:29 -0500127 if (sshdr.asc == 0x04 &&
128 sshdr.ascq == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* sense: Logical unit is in process of becoming ready */
130 if (!cgc->quiet)
131 printk(KERN_INFO "%s: CDROM not ready yet.\n", cd->cdi.name);
132 if (retries++ < 10) {
133 /* sleep 2 sec and try again */
134 ssleep(2);
135 goto retry;
136 } else {
137 /* 20 secs are enough? */
138 err = -ENOMEDIUM;
139 break;
140 }
141 }
142 if (!cgc->quiet)
143 printk(KERN_INFO "%s: CDROM not ready. Make sure there is a disc in the drive.\n", cd->cdi.name);
144#ifdef DEBUG
James Bottomley820732b2005-06-12 22:21:29 -0500145 scsi_print_sense_hdr("sr", &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#endif
147 err = -ENOMEDIUM;
148 break;
149 case ILLEGAL_REQUEST:
150 err = -EIO;
James Bottomley820732b2005-06-12 22:21:29 -0500151 if (sshdr.asc == 0x20 &&
152 sshdr.ascq == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* sense: Invalid command operation code */
154 err = -EDRIVE_CANT_DO_THIS;
155#ifdef DEBUG
156 __scsi_print_command(cgc->cmd);
James Bottomley820732b2005-06-12 22:21:29 -0500157 scsi_print_sense_hdr("sr", &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#endif
159 break;
160 default:
161 printk(KERN_ERR "%s: CDROM (ioctl) error, command: ", cd->cdi.name);
162 __scsi_print_command(cgc->cmd);
James Bottomley820732b2005-06-12 22:21:29 -0500163 scsi_print_sense_hdr("sr", &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 err = -EIO;
165 }
166 }
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* Wake up a process waiting for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 out:
James Bottomley820732b2005-06-12 22:21:29 -0500170 if (!cgc->sense)
171 kfree(sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 cgc->stat = err;
173 return err;
174}
175
176/* ---------------------------------------------------------------------- */
177/* interface to cdrom.c */
178
179static int test_unit_ready(Scsi_CD *cd)
180{
181 struct packet_command cgc;
182
183 memset(&cgc, 0, sizeof(struct packet_command));
184 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
185 cgc.quiet = 1;
186 cgc.data_direction = DMA_NONE;
187 cgc.timeout = IOCTL_TIMEOUT;
188 return sr_do_ioctl(cd, &cgc);
189}
190
191int sr_tray_move(struct cdrom_device_info *cdi, int pos)
192{
193 Scsi_CD *cd = cdi->handle;
194 struct packet_command cgc;
195
196 memset(&cgc, 0, sizeof(struct packet_command));
197 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
198 cgc.cmd[4] = (pos == 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
199 cgc.data_direction = DMA_NONE;
200 cgc.timeout = IOCTL_TIMEOUT;
201 return sr_do_ioctl(cd, &cgc);
202}
203
204int sr_lock_door(struct cdrom_device_info *cdi, int lock)
205{
206 Scsi_CD *cd = cdi->handle;
207
208 return scsi_set_medium_removal(cd->device, lock ?
209 SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
210}
211
212int sr_drive_status(struct cdrom_device_info *cdi, int slot)
213{
214 if (CDSL_CURRENT != slot) {
215 /* we have no changer support */
216 return -EINVAL;
217 }
218 if (0 == test_unit_ready(cdi->handle))
219 return CDS_DISC_OK;
220
221 return CDS_TRAY_OPEN;
222}
223
224int sr_disk_status(struct cdrom_device_info *cdi)
225{
226 Scsi_CD *cd = cdi->handle;
227 struct cdrom_tochdr toc_h;
228 struct cdrom_tocentry toc_e;
229 int i, rc, have_datatracks = 0;
230
231 /* look for data tracks */
232 if (0 != (rc = sr_audio_ioctl(cdi, CDROMREADTOCHDR, &toc_h)))
233 return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
234
235 for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
236 toc_e.cdte_track = i;
237 toc_e.cdte_format = CDROM_LBA;
238 if (sr_audio_ioctl(cdi, CDROMREADTOCENTRY, &toc_e))
239 return CDS_NO_INFO;
240 if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
241 have_datatracks = 1;
242 break;
243 }
244 }
245 if (!have_datatracks)
246 return CDS_AUDIO;
247
248 if (cd->xa_flag)
249 return CDS_XA_2_1;
250 else
251 return CDS_DATA_1;
252}
253
254int sr_get_last_session(struct cdrom_device_info *cdi,
255 struct cdrom_multisession *ms_info)
256{
257 Scsi_CD *cd = cdi->handle;
258
259 ms_info->addr.lba = cd->ms_offset;
260 ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
261
262 return 0;
263}
264
265/* primitive to determine whether we need to have GFP_DMA set based on
266 * the status of the unchecked_isa_dma flag in the host structure */
267#define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
268
269int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
270{
271 Scsi_CD *cd = cdi->handle;
272 struct packet_command cgc;
273 char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
274 int result;
275
Nate Dailey3a73e8c2005-04-21 16:14:05 -0400276 if (!buffer)
277 return -ENOMEM;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 memset(&cgc, 0, sizeof(struct packet_command));
280 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
281 cgc.cmd[2] = 0x40; /* I do want the subchannel info */
282 cgc.cmd[3] = 0x02; /* Give me medium catalog number info */
283 cgc.cmd[8] = 24;
284 cgc.buffer = buffer;
285 cgc.buflen = 24;
286 cgc.data_direction = DMA_FROM_DEVICE;
287 cgc.timeout = IOCTL_TIMEOUT;
288 result = sr_do_ioctl(cd, &cgc);
289
290 memcpy(mcn->medium_catalog_number, buffer + 9, 13);
291 mcn->medium_catalog_number[13] = 0;
292
293 kfree(buffer);
294 return result;
295}
296
297int sr_reset(struct cdrom_device_info *cdi)
298{
299 return 0;
300}
301
302int sr_select_speed(struct cdrom_device_info *cdi, int speed)
303{
304 Scsi_CD *cd = cdi->handle;
305 struct packet_command cgc;
306
307 if (speed == 0)
308 speed = 0xffff; /* set to max */
309 else
310 speed *= 177; /* Nx to kbyte/s */
311
312 memset(&cgc, 0, sizeof(struct packet_command));
313 cgc.cmd[0] = GPCMD_SET_SPEED; /* SET CD SPEED */
314 cgc.cmd[2] = (speed >> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
315 cgc.cmd[3] = speed & 0xff; /* LSB */
316 cgc.data_direction = DMA_NONE;
317 cgc.timeout = IOCTL_TIMEOUT;
318
319 if (sr_do_ioctl(cd, &cgc))
320 return -EIO;
321 return 0;
322}
323
324/* ----------------------------------------------------------------------- */
325/* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
326/* because the generic cdrom driver does the user access stuff for us. */
327/* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
328/* sr_disk_status interface for the generic cdrom driver. */
329
330int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
331{
332 Scsi_CD *cd = cdi->handle;
333 struct packet_command cgc;
334 int result;
335 unsigned char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
336
337 if (!buffer)
338 return -ENOMEM;
339
340 memset(&cgc, 0, sizeof(struct packet_command));
341 cgc.timeout = IOCTL_TIMEOUT;
342
343 switch (cmd) {
344 case CDROMREADTOCHDR:
345 {
346 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
347
348 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
349 cgc.cmd[8] = 12; /* LSB of length */
350 cgc.buffer = buffer;
351 cgc.buflen = 12;
352 cgc.quiet = 1;
353 cgc.data_direction = DMA_FROM_DEVICE;
354
355 result = sr_do_ioctl(cd, &cgc);
356
357 tochdr->cdth_trk0 = buffer[2];
358 tochdr->cdth_trk1 = buffer[3];
359
360 break;
361 }
362
363 case CDROMREADTOCENTRY:
364 {
365 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry *) arg;
366
367 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
368 cgc.cmd[1] |= (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0;
369 cgc.cmd[6] = tocentry->cdte_track;
370 cgc.cmd[8] = 12; /* LSB of length */
371 cgc.buffer = buffer;
372 cgc.buflen = 12;
373 cgc.data_direction = DMA_FROM_DEVICE;
374
375 result = sr_do_ioctl(cd, &cgc);
376
377 tocentry->cdte_ctrl = buffer[5] & 0xf;
378 tocentry->cdte_adr = buffer[5] >> 4;
379 tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
380 if (tocentry->cdte_format == CDROM_MSF) {
381 tocentry->cdte_addr.msf.minute = buffer[9];
382 tocentry->cdte_addr.msf.second = buffer[10];
383 tocentry->cdte_addr.msf.frame = buffer[11];
384 } else
385 tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
386 + buffer[10]) << 8) + buffer[11];
387
388 break;
389 }
390
391 case CDROMPLAYTRKIND: {
392 struct cdrom_ti* ti = (struct cdrom_ti*)arg;
393
394 cgc.cmd[0] = GPCMD_PLAYAUDIO_TI;
395 cgc.cmd[4] = ti->cdti_trk0;
396 cgc.cmd[5] = ti->cdti_ind0;
397 cgc.cmd[7] = ti->cdti_trk1;
398 cgc.cmd[8] = ti->cdti_ind1;
399 cgc.data_direction = DMA_NONE;
400
401 result = sr_do_ioctl(cd, &cgc);
402 if (result == -EDRIVE_CANT_DO_THIS)
403 result = sr_fake_playtrkind(cdi, ti);
404
405 break;
406 }
407
408 default:
409 result = -EINVAL;
410 }
411
412#if 0
413 if (result)
414 printk("DEBUG: sr_audio: result for ioctl %x: %x\n", cmd, result);
415#endif
416
417 kfree(buffer);
418 return result;
419}
420
421/* -----------------------------------------------------------------------
422 * a function to read all sorts of funny cdrom sectors using the READ_CD
423 * scsi-3 mmc command
424 *
425 * lba: linear block address
426 * format: 0 = data (anything)
427 * 1 = audio
428 * 2 = data (mode 1)
429 * 3 = data (mode 2)
430 * 4 = data (mode 2 form1)
431 * 5 = data (mode 2 form2)
432 * blksize: 2048 | 2336 | 2340 | 2352
433 */
434
435static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
436{
437 struct packet_command cgc;
438
439#ifdef DEBUG
440 printk("%s: sr_read_cd lba=%d format=%d blksize=%d\n",
441 cd->cdi.name, lba, format, blksize);
442#endif
443
444 memset(&cgc, 0, sizeof(struct packet_command));
445 cgc.cmd[0] = GPCMD_READ_CD; /* READ_CD */
446 cgc.cmd[1] = ((format & 7) << 2);
447 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
448 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
449 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
450 cgc.cmd[5] = (unsigned char) lba & 0xff;
451 cgc.cmd[8] = 1;
452 switch (blksize) {
453 case 2336:
454 cgc.cmd[9] = 0x58;
455 break;
456 case 2340:
457 cgc.cmd[9] = 0x78;
458 break;
459 case 2352:
460 cgc.cmd[9] = 0xf8;
461 break;
462 default:
463 cgc.cmd[9] = 0x10;
464 break;
465 }
466 cgc.buffer = dest;
467 cgc.buflen = blksize;
468 cgc.data_direction = DMA_FROM_DEVICE;
469 cgc.timeout = IOCTL_TIMEOUT;
470 return sr_do_ioctl(cd, &cgc);
471}
472
473/*
474 * read sectors with blocksizes other than 2048
475 */
476
477static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
478{
479 struct packet_command cgc;
480 int rc;
481
482 /* we try the READ CD command first... */
483 if (cd->readcd_known) {
484 rc = sr_read_cd(cd, dest, lba, 0, blksize);
485 if (-EDRIVE_CANT_DO_THIS != rc)
486 return rc;
487 cd->readcd_known = 0;
488 printk("CDROM does'nt support READ CD (0xbe) command\n");
489 /* fall & retry the other way */
490 }
491 /* ... if this fails, we switch the blocksize using MODE SELECT */
492 if (blksize != cd->device->sector_size) {
493 if (0 != (rc = sr_set_blocklength(cd, blksize)))
494 return rc;
495 }
496#ifdef DEBUG
497 printk("%s: sr_read_sector lba=%d blksize=%d\n", cd->cdi.name, lba, blksize);
498#endif
499
500 memset(&cgc, 0, sizeof(struct packet_command));
501 cgc.cmd[0] = GPCMD_READ_10;
502 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
503 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
504 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
505 cgc.cmd[5] = (unsigned char) lba & 0xff;
506 cgc.cmd[8] = 1;
507 cgc.buffer = dest;
508 cgc.buflen = blksize;
509 cgc.data_direction = DMA_FROM_DEVICE;
510 cgc.timeout = IOCTL_TIMEOUT;
511 rc = sr_do_ioctl(cd, &cgc);
512
513 return rc;
514}
515
516/*
517 * read a sector in raw mode to check the sector format
518 * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
519 */
520
521int sr_is_xa(Scsi_CD *cd)
522{
523 unsigned char *raw_sector;
524 int is_xa;
525
526 if (!xa_test)
527 return 0;
528
529 raw_sector = (unsigned char *) kmalloc(2048, GFP_KERNEL | SR_GFP_DMA(cd));
530 if (!raw_sector)
531 return -ENOMEM;
532 if (0 == sr_read_sector(cd, cd->ms_offset + 16,
533 CD_FRAMESIZE_RAW1, raw_sector)) {
534 is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
535 } else {
536 /* read a raw sector failed for some reason. */
537 is_xa = -1;
538 }
539 kfree(raw_sector);
540#ifdef DEBUG
541 printk("%s: sr_is_xa: %d\n", cd->cdi.name, is_xa);
542#endif
543 return is_xa;
544}
545
546int sr_dev_ioctl(struct cdrom_device_info *cdi,
547 unsigned int cmd, unsigned long arg)
548{
549 Scsi_CD *cd = cdi->handle;
550 int ret;
551
552 ret = scsi_nonblockable_ioctl(cd->device, cmd,
553 (void __user *)arg, NULL);
554 /*
555 * ENODEV means that we didn't recognise the ioctl, or that we
556 * cannot execute it in the current device state. In either
557 * case fall through to scsi_ioctl, which will return ENDOEV again
558 * if it doesn't recognise the ioctl
559 */
560 if (ret != -ENODEV)
561 return ret;
562 return scsi_ioctl(cd->device, cmd, (void __user *)arg);
563}