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