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