# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher.
# This patch includes the following deltas:
#	           ChangeSet	1.517   -> 1.518  
#	drivers/usb/storage/usb.c	1.24    -> 1.25   
#	drivers/usb/storage/scsiglue.c	1.21    -> 1.22   
#	drivers/usb/storage/usb.h	1.10    -> 1.11   
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 02/06/18	mdharm-usb@one-eyed-alien.net	1.518
# [PATCH] USB storage: change atomic_t to bitfield, consolidate #defines
# 
# This patch changes from using an atomic_t with two states to using a
# bitfield to determine if a device is attached.  It also moves some common
# #defines into a common header file.
# 
# courtsey of Alan Stern <stern@rowland.org>
# --------------------------------------------
#
diff -Nru a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
--- a/drivers/usb/storage/scsiglue.c	Tue Jun 18 17:02:00 2002
+++ b/drivers/usb/storage/scsiglue.c	Tue Jun 18 17:02:00 2002
@@ -51,12 +51,6 @@
 
 #include <linux/slab.h>
 
-/*
- * kernel thread actions
- */
-
-#define US_ACT_COMMAND		1
-#define US_ACT_EXIT		5
 
 /***********************************************************************
  * Host functions 
@@ -204,7 +198,7 @@
 	US_DEBUGP("device_reset() called\n" );
 
 	/* if the device was removed, then we're already reset */
-	if (atomic_read(&us->sm_state) == US_STATE_DETACHED)
+	if (!test_bit(DEV_ATTACHED, &us->bitflags))
 		return SUCCESS;
 
 	scsi_unlock(srb->host);
@@ -235,7 +229,7 @@
 	US_DEBUGP("bus_reset() called\n");
 
 	/* if the device has been removed, this worked */
-	if (atomic_read(&us->sm_state) == US_STATE_DETACHED) {
+	if (!test_bit(DEV_ATTACHED, &us->bitflags)) {
 		US_DEBUGP("-- device removed already\n");
 		return SUCCESS;
 	}
@@ -337,8 +331,8 @@
 
 	/* show the GUID of the device */
 	SPRINTF("         GUID: " GUID_FORMAT "\n", GUID_ARGS(us->guid));
-	SPRINTF("     Attached: %s\n", (atomic_read(&us->sm_state) ==
-			US_STATE_DETACHED) ? "Yes" : "No");
+	SPRINTF("     Attached: %s\n", (test_bit(DEV_ATTACHED, &us->bitflags)
+			? "Yes" : "No"));
 
 	/*
 	 * Calculate start of next buffer, and return value.
diff -Nru a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
--- a/drivers/usb/storage/usb.c	Tue Jun 18 17:02:00 2002
+++ b/drivers/usb/storage/usb.c	Tue Jun 18 17:02:00 2002
@@ -99,13 +99,6 @@
 
 static int my_host_number;
 
-/*
- * kernel thread actions
- */
-
-#define US_ACT_COMMAND		1
-#define US_ACT_EXIT		5
-
 /* The list of structures and the protective lock for them */
 struct us_data *us_list;
 struct semaphore us_list_semaphore;
@@ -426,7 +419,7 @@
 		down(&(us->dev_semaphore));
 
 		/* our device has gone - pretend not ready */
-		if (atomic_read(&us->device_state) == US_STATE_DETACHED) {
+		if (!test_bit(DEV_ATTACHED, &us->bitflags)) {
 			US_DEBUGP("Request is for removed device\n");
 			/* For REQUEST_SENSE, it's the data.  But
 			 * for anything else, it should look like
@@ -450,7 +443,7 @@
 				       sizeof(usb_stor_sense_notready));
 				us->srb->result = CHECK_CONDITION << 1;
 			}
-		} else { /* atomic_read(&us->device_state) == STATE_DETACHED */
+		} else { /* test_bit(DEV_ATTACHED, &us->bitflags) */
 
 			/* Handle those devices which need us to fake 
 			 * their inquiry data */
@@ -695,7 +688,7 @@
 	 */
 	ss = us_list;
 	while ((ss != NULL) && 
-	           ((atomic_read(&ss->device_state) == US_STATE_ATTACHED) ||
+	           (test_bit(DEV_ATTACHED, &ss->bitflags) ||
 		    !GUID_EQUAL(guid, ss->guid)))
 		ss = ss->next;
 
@@ -710,7 +703,7 @@
 		/* establish the connection to the new device upon reconnect */
 		ss->ifnum = ifnum;
 		ss->pusb_dev = dev;
-		atomic_set(&ss->device_state, US_STATE_ATTACHED);
+		set_bit(DEV_ATTACHED, &ss->bitflags);
 
 		/* copy over the endpoint data */
 		ss->ep_in = ep_in->bEndpointAddress & 
@@ -979,7 +972,7 @@
 
 		/* start up our control thread */
 		atomic_set(&ss->sm_state, US_STATE_IDLE);
-		atomic_set(&ss->device_state, US_STATE_ATTACHED);
+		set_bit(DEV_ATTACHED, &ss->bitflags);
 		ss->pid = kernel_thread(usb_stor_control_thread, ss,
 					CLONE_VM);
 		if (ss->pid < 0) {
@@ -1040,7 +1033,7 @@
 		ss->current_urb = NULL;
 	}
 
-	atomic_set(&ss->device_state, US_STATE_DETACHED);
+	clear_bit(DEV_ATTACHED, &ss->bitflags);
 	ss->pusb_dev = NULL;
 	if (new_device)
 		kfree(ss);
@@ -1088,7 +1081,7 @@
 	/* mark the device as gone */
 	usb_put_dev(ss->pusb_dev);
 	ss->pusb_dev = NULL;
-	atomic_set(&ss->sm_state, US_STATE_DETACHED);
+	clear_bit(DEV_ATTACHED, &ss->bitflags);
 
 	/* unlock access to the device data structure */
 	up(&(ss->dev_semaphore));
diff -Nru a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
--- a/drivers/usb/storage/usb.h	Tue Jun 18 17:02:00 2002
+++ b/drivers/usb/storage/usb.h	Tue Jun 18 17:02:00 2002
@@ -103,9 +103,10 @@
 #define US_FL_SCM_MULT_TARG   0x00000020 /* supports multiple targets */
 #define US_FL_FIX_INQUIRY     0x00000040 /* INQUIRY response needs fixing */
 
-/* device attached/detached states */
-#define US_STATE_DETACHED	1
-#define US_STATE_ATTACHED	2
+
+/* kernel thread actions */
+#define US_ACT_COMMAND		1
+#define US_ACT_EXIT		5
 
 /* processing state machine states */
 #define US_STATE_IDLE		1
@@ -127,10 +128,9 @@
 	/* The device we're working with
 	 * It's important to note:
 	 *    (o) you must hold dev_semaphore to change pusb_dev
-	 *    (o) device_state should change whenever pusb_dev does
+	 *    (o) DEV_ATTACHED in bitflags should change whenever pusb_dev does
 	 */
 	struct semaphore	dev_semaphore;	 /* protect pusb_dev */
-	atomic_t		device_state;	 /* attached or detached */
 	struct usb_device	*pusb_dev;	 /* this usb_device */
 
 	unsigned int		flags;		 /* from filter initially */
@@ -174,6 +174,7 @@
 	struct semaphore	ip_waitq;	 /* for CBI interrupts	 */
 	unsigned long		bitflags;	 /* single-bit flags:	 */
 #define IP_WANTED	1			 /* is an IRQ expected?	 */
+#define DEV_ATTACHED	2			 /* is the dev. attached?*/
 
 	/* interrupt communications data */
 	struct semaphore	irq_urb_sem;	 /* to protect irq_urb	 */
