From maxlor@maxlor.mine.nu  Mon Jun 30 15:29:22 2003
Return-Path: <maxlor@maxlor.mine.nu>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id A6F3537B401
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 30 Jun 2003 15:29:22 -0700 (PDT)
Received: from maxlor.mine.nu (c-213-160-32-54.customer.ggaweb.ch [213.160.32.54])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 2134743FBD
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 30 Jun 2003 15:29:22 -0700 (PDT)
	(envelope-from maxlor@maxlor.mine.nu)
Received: by maxlor.mine.nu (Postfix, from userid 1000)
	id 187CB12D262; Tue,  1 Jul 2003 00:29:21 +0200 (CEST)
Message-Id: <20030630222921.187CB12D262@maxlor.mine.nu>
Date: Tue,  1 Jul 2003 00:29:21 +0200 (CEST)
From: Benjamin Lutz <benlutz@datacomm.ch>
Reply-To: Benjamin Lutz <benlutz@datacomm.ch>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] ATAPI CD drives don't open the tray if they're empty
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         53941
>Category:       kern
>Synopsis:       [PATCH] ATAPI CD drives don't open the tray if they're empty
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    sos
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jun 30 15:30:14 PDT 2003
>Closed-Date:    Sat Oct 25 12:40:39 PDT 2003
>Last-Modified:  Sat Oct 25 12:40:39 PDT 2003
>Originator:     Benjamin Lutz
>Release:        FreeBSD 5.1-RELEASE i386
>Organization:
>Environment:
System: FreeBSD merlin 5.1-RELEASE FreeBSD 5.1-RELEASE #6: Mon Jun 30 23:29:15 CEST 2003 root@merlin:/usr/src/sys/i386/compile/MERLIN i386

>Description:
cdcontrol -f /dev/acd0 eject does not work if the cd drive is empty. This is not a bug in cdcontrol, but in the kernel's atapi-cd code. If the drive is empty, it seems that acd_start_stop(cdp, 0) always returns EBUSY.

>How-To-Repeat:
Type "cdcontrol -f /dev/acd0 eject". The cd tray opens if a cd is in the drive, but doesn't if there isn't one.

>Fix:
----- DIFF START -----
--- atapi-cd.c.orig     Mon Jun 30 23:42:47 2003
+++ atapi-cd.c  Mon Jun 30 23:28:54 2003
@@ -1860,9 +1860,9 @@
 {
     int error;

-    if ((error = acd_start_stop(cdp, 0)) == EBUSY) {
-       if (!close)
-           return 0;
+    error = acd_start_stop(cdp, 0);
+
+    if (close && (error == EBUSY)) {
        if ((error = acd_start_stop(cdp, 3)))
            return error;
        acd_read_toc(cdp);
@@ -1870,10 +1870,6 @@
        cdp->flags |= F_LOCKED;
        return 0;
     }
-    if (error)
-       return error;
-    if (close)
-       return 0;
     acd_prevent_allow(cdp, 0);
     cdp->flags &= ~F_LOCKED;
     cdp->device->flags |= ATA_D_MEDIA_CHANGED;
---- DIFF END ----

I changed the code to ignore the error returned by acd_start_stop(cdp, 0) if we're trying to open the tray. Shouldn't matter much, since if the drive shouldn't be opened, it'll be locked.

I tested this by using a cd burner and trying to open the tray while burning a CD. As expected it did not interrupt the burning process. Now, there's one thing that still bothers me a tiny bit... Maybe someone with more background knowledge can check this. My code now executes the following if we're trying to open the drive:

acd_start_stop(cdp, 0);
acd_prevent_allow(cdp, 0);
cdp->flags &= ~F_LOCKED;
cdp->device->flags |= ATA_D_MEDIA_CHANGED;
return acd_start_stop(cdp, 2);

In other words, cdp->flags and cdp->device->flags always get changed. As I said, this doesn't seem to matter on my machine, but maybe I'm overlooking something.

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->sos 
Responsible-Changed-By: brooks 
Responsible-Changed-When: Fri Jul 11 16:43:59 PDT 2003 
Responsible-Changed-Why:  
Over to ATA maintainer. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=53941 

From: Brooks Davis <brooks@one-eyed-alien.net>
To: Benjamin Lutz <benlutz@datacomm.ch>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: kern/53941: [PATCH] ATAPI CD drives don't open the tray if they're empty
Date: Fri, 11 Jul 2003 16:46:06 -0700

 On Tue, Jul 01, 2003 at 12:29:21AM +0200, Benjamin Lutz wrote:
 > >How-To-Repeat:
 > Type "cdcontrol -f /dev/acd0 eject". The cd tray opens if a cd is in
 > the drive, but doesn't if there isn't one.
 
 This also fixed the long-standing annoyance that programs like xmcd lock
 the drive until you close the application if you close the drive empty
 when I applied it to my CURRENT laptop.
 
 -- Brooks
State-Changed-From-To: open->closed 
State-Changed-By: sos 
State-Changed-When: Sat Oct 25 12:39:27 PDT 2003 
State-Changed-Why:  
THis is fixed in current (in another way than this patch though). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=53941 
>Unformatted:
