Re: [PATCH RESEND] SCSI not showing tray status correctly

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <James.Bottomley@...>
Cc: <linux-kernel@...>, <linux-scsi@...>, <axboe@...>, <tasio@...>, <dsd@...>
Date: Saturday, October 27, 2007 - 6:58 pm

> This patch is too simplistic.  ide-cd.c:ide_cdrom_drive_status() looks
This patch hasn't been tested at all, I'm sorry if I gave you that
impression, it isn't in Gentoo's patches, it's just been brought to our
attention in the bug I linked too.

I created another patch, based on your recommendations, and with a lot of
help from Daniel Drake (dsd@gentoo.org), that's included below. Some
changes are made to test_unit_ready() to be able to pass the sense data
to sr_drive_status(). Currently, sr_do_ioctl() swallows this data and
makes its own interpretations of sense codes, which isn't what we want
here.

Is this what you had in mind? Do you think the possible problems with
USB drives that you mentioned will prevent this patch from going in? 
The patch has only been compile tested right now, we can do some real
testing later, for now I'd just like to get your feedback on it.

Maarten

---

--- a/drivers/scsi/sr_ioctl.c	2007-10-26 22:40:41.000000000 +0200
+++ b/drivers/scsi/sr_ioctl.c	2007-10-27 23:56:16.000000000 +0200
@@ -275,16 +275,21 @@ int sr_do_ioctl(Scsi_CD *cd, struct pack
 /* ---------------------------------------------------------------------- */
 /* interface to cdrom.c                                                   */
 
-static int test_unit_ready(Scsi_CD *cd)
+static int test_unit_ready(Scsi_CD *cd, struct request_sense *sense)
 {
-	struct packet_command cgc;
+	struct scsi_device *SDev = cd->device;
+	unsigned char cmd[CDROM_PACKET_SIZE] = { GPCMD_TEST_UNIT_READY };
+	int result;
 
-	memset(&cgc, 0, sizeof(struct packet_command));
-	cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
-	cgc.quiet = 1;
-	cgc.data_direction = DMA_NONE;
-	cgc.timeout = IOCTL_TIMEOUT;
-	return sr_do_ioctl(cd, &cgc);
+	if (!scsi_block_when_processing_errors(SDev))
+		return -ENODEV;
+
+	memset(sense, 0, sizeof(*sense));
+	result = scsi_execute(SDev, cmd, DMA_NONE,
+			      NULL, 0, (char *)sense,
+			      IOCTL_TIMEOUT, IOCTL_RETRIES, 0);
+
+	return driver_byte(result);
 }
 
 int sr_tray_move(struct cdrom_device_info *cdi, int pos)
@@ -310,14 +315,41 @@ int sr_lock_door(struct cdrom_device_inf
 
 int sr_drive_status(struct cdrom_device_info *cdi, int slot)
 {
+	struct request_sense sense;
+	struct media_event_desc med;
+
 	if (CDSL_CURRENT != slot) {
 		/* we have no changer support */
 		return -EINVAL;
 	}
-	if (0 == test_unit_ready(cdi->handle))
+	if ((0 == test_unit_ready(cdi->handle, &sense)) || 
+	    sense.sense_key == UNIT_ATTENTION)
 		return CDS_DISC_OK;
 
-	return CDS_TRAY_OPEN;
+	if (!cdrom_get_media_event(cdi, &med)) {
+		if (med.media_present)
+			return CDS_DISC_OK;
+		else if (med.door_open)
+			return CDS_TRAY_OPEN;
+		else
+			return CDS_NO_DISC;
+	}
+
+	if (sense.sense_key == NOT_READY && sense.asc == 0x04 && sense.ascq == 0x04)
+		return CDS_DISC_OK;
+
+	/*
+	 * If not using Mt Fuji extended media tray reports,
+	 * just return TRAY_OPEN since ATAPI doesn't provide
+	 * any other way to detect this...
+	 */
+	if (sense.sense_key == NOT_READY) {
+		if (sense.asc == 0x3a && sense.ascq == 1)
+			return CDS_NO_DISC;
+		else
+			return CDS_TRAY_OPEN;
+	}
+	return CDS_DRIVE_NOT_READY;
 }
 
 int sr_disk_status(struct cdrom_device_info *cdi)
-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [PATCH RESEND] SCSI not showing tray status correctly, Maarten Bressers, (Sat Oct 27, 6:58 pm)
Re: [PATCH RESEND] SCSI not showing tray status correctly, James Bottomley, (Tue Oct 30, 11:32 pm)
[PATCH] sr: update to follow tray status correctly, James Bottomley, (Sat Jan 5, 12:39 pm)
Re: [PATCH] sr: update to follow tray status correctly, Daniel Drake, (Wed Feb 6, 1:09 pm)
Re: [PATCH] sr: update to follow tray status correctly, James Bottomley, (Wed Feb 6, 3:01 pm)
Re: [PATCH] sr: update to follow tray status correctly, Daniel Drake, (Wed Feb 6, 4:54 pm)