Re: MAX3421E Linux driver?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Here is an updated patch (v0.2).  The main change is to convert the
driver so all SPI accesses are done from a separate thread and without
holding a spin-lock.  This is necessary because SPI-transfers may
sleep.

The basics seem to work pretty well.  Something is clearly still wrong
when shutting down an endpoint and/or the driver as it hangs when
there is pending URBs in those cases.

As usual, all feedback welcome.

  --david
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index f5ed3d7..2e270cc 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_USB_DWC3)		+= dwc3/
 obj-$(CONFIG_USB_MON)		+= mon/
 
 obj-$(CONFIG_PCI)		+= host/
+obj-$(CONFIG_USB_MAX3421_HCD)	+= host/
 obj-$(CONFIG_USB_EHCI_HCD)	+= host/
 obj-$(CONFIG_USB_ISP116X_HCD)	+= host/
 obj-$(CONFIG_USB_OHCI_HCD)	+= host/
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index d6bb128..563a362 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -4,6 +4,16 @@
 comment "USB Host Controller Drivers"
 	depends on USB
 
+config USB_MAX3421_HCD
+       tristate "MAX3421 HCD (USB-over-SPI) support"
+       depends on USB
+       help
+         The Maxim MAX3421E Host Controller Interface supports
+	 standard USB 2.0-compliant full-speed devices.
+
+         To compile this driver as a module, choose M here: the module will
+	 be called max3421.
+
 config USB_C67X00_HCD
 	tristate "Cypress C67x00 HCD support"
 	depends on USB
diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
index 1eb4c30..1dfb836 100644
--- a/drivers/usb/host/Makefile
+++ b/drivers/usb/host/Makefile
@@ -23,6 +23,8 @@ obj-$(CONFIG_USB_WHCI_HCD)	+= whci/
 
 obj-$(CONFIG_PCI)		+= pci-quirks.o
 
+obj-$(CONFIG_USB_MAX3421_HCD)	+= max3421.o
+
 obj-$(CONFIG_USB_EHCI_HCD)	+= ehci-hcd.o
 obj-$(CONFIG_USB_EHCI_PCI)	+= ehci-pci.o
 obj-$(CONFIG_USB_EHCI_HCD_PLATFORM)	+= ehci-platform.o
diff --git a/drivers/usb/host/max3421.c b/drivers/usb/host/max3421.c
index 61df0e6..b890ec3 100644
--- a/drivers/usb/host/max3421.c
+++ b/drivers/usb/host/max3421.c
@@ -16,86 +16,146 @@
  *	o gadget/dummy_hcd.c
  *		For USB HCD implementation.
  *	o Arduino MAX3421 driver
- *		https://github.com/felis/USB_Host_Shield_2.0/blob/master/Usb.cpp
+ *	     https://github.com/felis/USB_Host_Shield_2.0/blob/master/Usb.cpp
  *
  * This file is licenced under the GPL.
+ *
+ * Important note on worst-case (full-speed) packet size constraints
+ * (See USB 2.0 Section 5.6.3 and following):
+ *
+ *	- control:	  64 bytes
+ *	- isochronous:	1023 bytes
+ *	- interrupt:	  64 bytes
+ *	- bulk:		  64 bytes
+ *
+ * Since the MAX3421 FIFO size is 64 bytes, we do not have to work about
+ * multi-FIFO writes/reads for a single USB packet *except* for isochronous
+ * transfers.  We don't support isochronous transfers at this time, so we
+ * just assume that a USB packet always fits into a single FIFO buffer.
+ *
+ * NOTE: The June 2006 version of "MAX3421E Programming Guide"
+ * (AN3785) has conflicting info for the RCVDAVIRQ bit:
+ *
+ *	The description of RCVDAVIRQ says "The CPU *must* clear
+ *	this IRQ bit (by writing a 1 to it) before reading the
+ *	RCVFIFO data.
+ *
+ * However, the earlier section on "Programming BULK-IN
+ * Transfers" says * that:
+ *
+ *	After the CPU retrieves the data, it clears the
+ *	RCVDAVIRQ bit.
+ *
+ * The December 2006 version has been corrected and it consistently
+ * states the second behavior is the correct one.
+ *
+ * Synchronous SPI transactions sleep so we can't perform any such
+ * transactions while holding a spin-lock (and/or while interrupts are
+ * masked).  To achieve this, all SPI transactions are issued from a
+ * single thread (max3421_spi_thread).
  */
 
+#define TRACE 1
+
 #include <linux/module.h>
-#include <linux/scatterlist.h>
 #include <linux/spi/spi.h>
 #include <linux/usb.h>
 #include <linux/usb/hcd.h>
 
 #define DRIVER_DESC	"MAX3421 USB Host-Controller Driver"
-#define DRIVER_VERSION	"0.0"
+#define DRIVER_VERSION	"0.2"
+
+/* 11-bit counter that wraps around (USB 2.0 Section 8.3.3): */
+#define USB_MAX_FRAME_NUMBER	0x7ff
+#define USB_MAX_RETRIES		3 /* # of retries before error is reported */
 
-#define POWER_BUDGET	500	// in mA; use 8 for low-power port testing
+#define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */
 
-// Port-change mask:
+/* Port-change mask: */
 #define PORT_C_MASK	((USB_PORT_STAT_C_CONNECTION |	\
 			  USB_PORT_STAT_C_ENABLE |	\
 			  USB_PORT_STAT_C_SUSPEND |	\
 			  USB_PORT_STAT_C_OVERCURRENT | \
 			  USB_PORT_STAT_C_RESET) << 16)
 
+enum trace_type {
+	TRACE_CMD = 1,
+	TRACE_RESULT,
+	TRACE_RX_LEN,
+	TRACE_RX_SIZE,
+	TRACE_TX_LEN,
+	TRACE_ADDR,
+	TRACE_TOGGLE
+};
+
 enum max3421_rh_state {
 	MAX3421_RH_RESET,
 	MAX3421_RH_SUSPENDED,
 	MAX3421_RH_RUNNING
 };
 
-struct max3421_hcd {
-	spinlock_t			lock;
+enum pkt_state {
+	PKT_STATE_SETUP,	/* waiting to send setup packet to ctrl pipe */
+	PKT_STATE_TRANSFER,	/* waiting to xfer transfer_buffer */
+	PKT_STATE_TERMINATE	/* waiting to terminate control transfer */
+};
 
-	struct max3421_hcd		*next;
+enum scheduling_pass {
+	SCHED_PASS_PERIODIC,
+	SCHED_PASS_NON_PERIODIC,
+	SCHED_PASS_DONE
+};
 
-	enum max3421_rh_state		rh_state;
-	// lower 16 bits contain port status, upper 16 bits the change mask:
-	u32				port_status;
+struct max3421_hcd {
+	spinlock_t lock;
 
-	struct list_head		urbp_list;
+	struct task_struct *spi_thread;
 
-	struct max3421_ep		*last_ep;
+	struct max3421_hcd *next;
 
-	u8				chip_rev;	// contents of REVISION register
-	u8				hien;
+	enum max3421_rh_state rh_state;
+	/* lower 16 bits contain port status, upper 16 bits the change mask: */
+	u32 port_status;
 
-	unsigned			active : 1;
-	unsigned			resuming : 1;
-};
+	unsigned active:1;
+	unsigned resuming:1;
 
-struct max3421_ep {
-	unsigned recv_toggle : 1;	// saved receive toggle
-	unsigned send_toggle : 1;	// saved send toggle
-};
+	struct list_head ep_list;	/* list of EP's with work */
 
-/* XXX Fix me: we need this because the URB's list_head is already
-  being used to link the URB to the end-point.  However, we shouldn't
-  need this list in the first place: it'd be better for the
-  max3421_irq_thread to iterate over active endpoints and then use the
-  URB-lists associated with the endpoints.  */
-struct max3421_urbp {
-	struct urb		*urb;
-	struct list_head	urbp_list;
+	/*
+	 * The following are owned by spi_thread (may be accessed by
+	 * SPI-thread without acquiring the HCD lock:
+	 */
+	u16 frame_number;
+	struct urb *curr_urb;		/* URB we're currently processing */
+	enum scheduling_pass sched_pass;
+	struct usb_device *loaded_dev;	/* dev that's loaded into the chip */
+	int loaded_epnum;		/* epnum whose toggles are loaded */
+	int urb_done;			/* > 0 -> no errors, < 0: errno */
+	size_t curr_len;
+	u8 hien;
+	u8 do_enable_irq;
+	u8 do_reset_hcd;
+	u8 do_reset_port;
+	unsigned long type_stat[4];
+	unsigned long err_stat[16];
 };
 
-struct max3421_errata_workaround {
-	size_t len;
-	u8 *data;
+struct max3421_ep {
+	struct usb_host_endpoint *ep;
+	struct list_head ep_list;
+	u16 last_active;		/* frame # this ep was last active */
+	enum pkt_state pkt_state;
+	u8 retries;
+	u8 retransmit;			/* packet needs retransmission */
 };
 
-static unsigned long qlen;
-static unsigned long qint;
-static unsigned long nakcnt;
-static unsigned long dsptchcnt;
-
 static struct max3421_hcd *max3421_hcd_list;
 
 #define MAX3421_FIFO_SIZE	64
 
-#define MAX3421_SPI_DIR_RD	0	// read register from MAX3421
-#define MAX3421_SPI_DIR_WR	1	// write register to MAX3421
+#define MAX3421_SPI_DIR_RD	0	/* read register from MAX3421 */
+#define MAX3421_SPI_DIR_WR	1	/* write register to MAX3421 */
 
 /* SPI commands: */
 #define MAX3421_SPI_DIR_SHIFT	1
@@ -154,14 +214,14 @@ enum {
 };
 
 enum {
-	MAX3421_HI_BUSEVENT_BIT = 0,	// bus-reset/-resume
-	MAX3421_HI_RWU_BIT,		// remote wakeup
-	MAX3421_HI_RCVDAV_BIT,		// receive FIFO data available
-	MAX3421_HI_SNDBAV_BIT,		// send buffer available
-	MAX3421_HI_SUSDN_BIT,		// suspend operation done
-	MAX3421_HI_CONDET_BIT,		// peripheral connect/disconnect
-	MAX3421_HI_FRAME_BIT,		// frame generator
-	MAX3421_HI_HXFRDN_BIT,		// host transfer done
+	MAX3421_HI_BUSEVENT_BIT = 0,	/* bus-reset/-resume */
+	MAX3421_HI_RWU_BIT,		/* remote wakeup */
+	MAX3421_HI_RCVDAV_BIT,		/* receive FIFO data available */
+	MAX3421_HI_SNDBAV_BIT,		/* send buffer available */
+	MAX3421_HI_SUSDN_BIT,		/* suspend operation done */
+	MAX3421_HI_CONDET_BIT,		/* peripheral connect/disconnect */
+	MAX3421_HI_FRAME_BIT,		/* frame generator */
+	MAX3421_HI_HXFRDN_BIT,		/* host transfer done */
 };
 
 enum {
@@ -203,13 +263,34 @@ enum {
 	MAX3421_HRSL_JERR,
 	MAX3421_HRSL_TIMEOUT,
 	MAX3421_HRSL_BABBLE,
-	MAX3421_HRSL_MASK = 0xf,
+	MAX3421_HRSL_RESULT_MASK = 0xf,
 	MAX3421_HRSL_RCVTOGRD_BIT = 4,
 	MAX3421_HRSL_SNDTOGRD_BIT,
 	MAX3421_HRSL_KSTATUS_BIT,
 	MAX3421_HRSL_JSTATUS_BIT
 };
 
+/* Return same error-codes as ohci.h:cc_to_error: */
+static const int hrsl_to_error[] = {
+	[MAX3421_HRSL_OK] =		0,
+	[MAX3421_HRSL_BUSY] =		-EINVAL,
+	[MAX3421_HRSL_BADREQ] =		-EINVAL,
+	[MAX3421_HRSL_UNDEF] =		-EINVAL,
+	[MAX3421_HRSL_NAK] =		-EAGAIN,
+	[MAX3421_HRSL_STALL] =		-EPIPE,
+	[MAX3421_HRSL_TOGERR] =		-EILSEQ,
+	[MAX3421_HRSL_WRONGPID] =	-EPROTO,
+	[MAX3421_HRSL_BADBC] =		-EREMOTEIO,
+	[MAX3421_HRSL_PIDERR] =		-EPROTO,
+	[MAX3421_HRSL_PKTERR] =		-EPROTO,
+	[MAX3421_HRSL_CRCERR] =		-EILSEQ,
+	[MAX3421_HRSL_KERR] =		-EIO,
+	[MAX3421_HRSL_JERR] =		-EIO,
+	[MAX3421_HRSL_TIMEOUT] =	-ETIME,
+	[MAX3421_HRSL_BABBLE] =		-EOVERFLOW
+};
+
+#if 1
 static const char *hrsl_string[] = {
 	[MAX3421_HRSL_OK] =		"Successful Transfer",
 	[MAX3421_HRSL_BUSY] =		"SIE is busy, transfer pending",
@@ -228,42 +309,43 @@ static const char *hrsl_string[] = {
 	[MAX3421_HRSL_TIMEOUT] =	"Device did not respond in time",
 	[MAX3421_HRSL_BABBLE] =		"Device talked too long"
 };
+#endif
 
 /*
- * See http://www.beyondlogic.org/usbnutshell/usb4.shtml#Control for a reasonable overview
- * of how control transfers use the the IN/OUT tokens.
+ * See http://www.beyondlogic.org/usbnutshell/usb4.shtml#Control for a
+ * reasonable overview of how control transfers use the the IN/OUT
+ * tokens.
  */
-#define MAX3421_HXFR_SETUP		0x10
-#define MAX3421_HXFR_HS_IN		0x80		// handshake in
-#define MAX3421_HXFR_HS_OUT		0xa0		// handshake out
-#define MAX3421_HXFR_BULK_IN(ep)	(0x00 | (ep))	// bulk or interrupt
-#define MAX3421_HXFR_BULK_OUT(ep)	(0x20 | (ep))	// bulk or interrupt
+#define MAX3421_HXFR_BULK_IN(ep)	(0x00 | (ep))	/* bulk or interrupt */
+#define MAX3421_HXFR_SETUP		 0x10
+#define MAX3421_HXFR_BULK_OUT(ep)	(0x20 | (ep))	/* bulk or interrupt */
 #define MAX3421_HXFR_ISO_IN(ep)		(0x40 | (ep))
 #define MAX3421_HXFR_ISO_OUT(ep)	(0x60 | (ep))
+#define MAX3421_HXFR_HS_IN		 0x80		/* handshake in */
+#define MAX3421_HXFR_HS_OUT		 0xa0		/* handshake out */
 
-#define mask(bit)	(1u << (bit))
 #define field(val, bit)	((val) << (bit))
 
 static u8
-spi_rd8 (struct spi_device *spi, unsigned int reg, u8 *status)
+spi_rd8(struct spi_device *spi, unsigned int reg, u8 *status)
 {
 	struct spi_transfer transfer;
 	u8 tx_data[2], rx_data[2];
 	struct spi_message msg;
 
-	memset (&transfer, 0, sizeof (transfer));
+	memset(&transfer, 0, sizeof(transfer));
 
-	spi_message_init (&msg);
+	spi_message_init(&msg);
 
-	tx_data[0] = (field (reg, MAX3421_SPI_REG_SHIFT) |
-		      field (MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
+	tx_data[0] = (field(reg, MAX3421_SPI_REG_SHIFT) |
+		      field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
 
 	transfer.tx_buf = tx_data;
 	transfer.rx_buf = rx_data;
 	transfer.len = 2;
 
-	spi_message_add_tail (&transfer, &msg);
-	spi_sync (spi, &msg);
+	spi_message_add_tail(&transfer, &msg);
+	spi_sync(spi, &msg);
 
 	if (status)
 		*status = rx_data[0];
@@ -272,18 +354,18 @@ spi_rd8 (struct spi_device *spi, unsigned int reg, u8 *status)
 }
 
 static void
-spi_wr8 (struct spi_device *spi, unsigned int reg, u8 val, u8 *status)
+spi_wr8(struct spi_device *spi, unsigned int reg, u8 val, u8 *status)
 {
 	struct spi_transfer transfer;
 	u8 tx_data[2], rx_data[2];
 	struct spi_message msg;
 
-	memset (&transfer, 0, sizeof (transfer));
+	memset(&transfer, 0, sizeof(transfer));
 
-	spi_message_init (&msg);
+	spi_message_init(&msg);
 
-	tx_data[0] = (field (reg, MAX3421_SPI_REG_SHIFT) |
-		      field (MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
+	tx_data[0] = (field(reg, MAX3421_SPI_REG_SHIFT) |
+		      field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
 	tx_data[1] = val;
 
 	transfer.tx_buf = tx_data;
@@ -291,27 +373,27 @@ spi_wr8 (struct spi_device *spi, unsigned int reg, u8 val, u8 *status)
 		transfer.rx_buf = rx_data;
 	transfer.len = 2;
 
-	spi_message_add_tail (&transfer, &msg);
-	spi_sync (spi, &msg);
+	spi_message_add_tail(&transfer, &msg);
+	spi_sync(spi, &msg);
 
 	if (status)
 		*status = rx_data[0];
-//	printk ("wr8: tx[%02x]=%02x\n", tx_data[0], tx_data[1]);
 }
 
 static void
-spi_rd_fifo (struct spi_device *spi, unsigned int reg, void *buf, size_t len, u8 *status)
+spi_rd_fifo(struct spi_device *spi, unsigned int reg, void *buf, size_t len,
+	    u8 *status)
 {
 	struct spi_transfer transfer[2];
 	struct spi_message msg;
 	u8 cmd;
 
-	memset (transfer, 0, sizeof (transfer));
+	memset(transfer, 0, sizeof(transfer));
 
-	spi_message_init (&msg);
+	spi_message_init(&msg);
 
-	cmd = (field (reg, MAX3421_SPI_REG_SHIFT) |
-	       field (MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
+	cmd = (field(reg, MAX3421_SPI_REG_SHIFT) |
+	       field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
 
 	transfer[0].tx_buf = &cmd;
 	transfer[0].rx_buf = status;
@@ -320,24 +402,25 @@ spi_rd_fifo (struct spi_device *spi, unsigned int reg, void *buf, size_t len, u8
 	transfer[1].rx_buf = buf;
 	transfer[1].len = len;
 
-	spi_message_add_tail (&transfer[0], &msg);
-	spi_message_add_tail (&transfer[1], &msg);
-	spi_sync (spi, &msg);
+	spi_message_add_tail(&transfer[0], &msg);
+	spi_message_add_tail(&transfer[1], &msg);
+	spi_sync(spi, &msg);
 }
 
 static void
-spi_wr_fifo (struct spi_device *spi, unsigned int reg, void *buf, size_t len, u8 *status)
+spi_wr_fifo(struct spi_device *spi, unsigned int reg, void *buf, size_t len,
+	    u8 *status)
 {
 	struct spi_transfer transfer[2];
 	struct spi_message msg;
 	u8 cmd;
 
-	memset (transfer, 0, sizeof (transfer));
+	memset(transfer, 0, sizeof(transfer));
 
-	spi_message_init (&msg);
+	spi_message_init(&msg);
 
-	cmd = (field (reg, MAX3421_SPI_REG_SHIFT) |
-	       field (MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
+	cmd = (field(reg, MAX3421_SPI_REG_SHIFT) |
+	       field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
 
 	transfer[0].tx_buf = &cmd;
 	transfer[0].rx_buf = status;
@@ -346,665 +429,1004 @@ spi_wr_fifo (struct spi_device *spi, unsigned int reg, void *buf, size_t len, u8
 	transfer[1].tx_buf = buf;
 	transfer[1].len = len;
 
-	spi_message_add_tail (&transfer[0], &msg);
-	spi_message_add_tail (&transfer[1], &msg);
-	spi_sync (spi, &msg);
+	spi_message_add_tail(&transfer[0], &msg);
+	spi_message_add_tail(&transfer[1], &msg);
+	spi_sync(spi, &msg);
+}
+
+static inline s16
+frame_diff(u16 left, u16 right)
+{
+	return ((unsigned) (left - right)) % (USB_MAX_FRAME_NUMBER + 1);
 }
 
 static inline struct max3421_hcd *
-hcd_to_max3421 (struct usb_hcd *hcd)
+hcd_to_max3421(struct usb_hcd *hcd)
 {
 	return (struct max3421_hcd *) hcd->hcd_priv;
 }
 
 static inline struct usb_hcd *
-max3421_to_hcd (struct max3421_hcd *max3421_hcd)
+max3421_to_hcd(struct max3421_hcd *max3421_hcd)
 {
-	return container_of ((void *) max3421_hcd, struct usb_hcd, hcd_priv);
+	return container_of((void *) max3421_hcd, struct usb_hcd, hcd_priv);
 }
 
-static inline void
-hub_descriptor (struct usb_hub_descriptor *desc)
-{
-	memset (desc, 0, sizeof (*desc));
-	/*
-	 * See Table 11-13: Hub Descriptor in USB 2.0 spec.
-	 */
-	desc->bDescriptorType = 0x29;	// hub descriptor
-	desc->bDescLength = 9;
-	desc->wHubCharacteristics = cpu_to_le16 (0x0001);
-	desc->bNbrPorts = 1;
-}
+#if TRACE
 
-static int
-max3421_set_address (struct usb_hcd *hcd, struct urb *urb)
+struct trace_entry {
+	u16 timestamp;
+	u8 type;
+	u8 data;
+};
+
+static struct trace_buffer {
+	unsigned int stop_counter;
+	unsigned int stopped;
+	int index;
+	struct trace_entry e[256];
+} trace_buffer;
+
+static void
+trace_stop(unsigned int delay_count)
 {
-	struct spi_device *spi = to_spi_device (hcd->self.controller);
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
-	struct max3421_ep *max3421_ep = urb->ep->hcpriv;
-	u8 addr;
-
-	// set address:
-	addr = usb_pipedevice (urb->pipe);
-	spi_wr8 (spi, MAX3421_REG_PERADDR, addr, NULL);
-
-	if (max3421_ep != max3421_hcd->last_ep) {
-		// end-point changed
-		u8 hctl;
-		if (max3421_hcd->last_ep) {
-			// save the old end-points toggles:
-			u8 hrsl = spi_rd8 (spi, MAX3421_REG_HRSL, NULL);
-			max3421_hcd->last_ep->recv_toggle = hrsl >> MAX3421_HRSL_RCVTOGRD_BIT;
-			max3421_hcd->last_ep->send_toggle = hrsl >> MAX3421_HRSL_SNDTOGRD_BIT;
-//			printk ("%s: switching from ep %p (R%dS%d) to ep %p (R%dS%d)\n", __FUNCTION__, max3421_hcd->last_ep, max3421_hcd->last_ep->recv_toggle, max3421_hcd->last_ep->send_toggle, max3421_ep, max3421_ep->recv_toggle, max3421_ep->send_toggle);
-		}
-		// restore new endpoint's toggle bits:
-		hctl = (mask (max3421_ep->recv_toggle + MAX3421_HCTL_RCVTOG0_BIT) |
-			mask (max3421_ep->send_toggle + MAX3421_HCTL_SNDTOG0_BIT));
-		spi_wr8 (spi, MAX3421_REG_HCTL, hctl, NULL);
-		max3421_hcd->last_ep = max3421_ep;
-	}
-	return 0;
+	if (delay_count > 0)
+		trace_buffer.stop_counter = delay_count;
+	else
+		trace_buffer.stopped = 1;
 }
 
-static int
-max3421_dispatch_packet (struct usb_hcd *hcd, struct urb *urb, u8 cmd,
-			 struct max3421_errata_workaround *tx)
+static void
+trace_record(struct usb_hcd *hcd, u8 type, u8 data)
 {
-	struct spi_device *spi = to_spi_device (hcd->self.controller);
-	int retval = -ETIMEDOUT, timeout, retries;
-	u8 hrsl, hirq, result_code, m;
-
-	++dsptchcnt;
-
-	for (retries = 0; retries < 8; ++retries) {
-		// start the transfer:
-//printk ("%s: hxfr cmd 0x%02x\n", __FUNCTION__, cmd);
-		spi_wr8 (spi, MAX3421_REG_HXFR, cmd, NULL);
-
-		// wait for HXFRDNIRQ    XXX Fix me: this is a random timeout...:
-		m = mask (MAX3421_HI_HXFRDN_BIT);
-		for (timeout = 1000; timeout >= 0; --timeout) {
-			hrsl = spi_rd8 (spi, MAX3421_REG_HRSL, &hirq);
-			if (hirq & m) {
-				// ack host-transfer-done irq:
-				spi_wr8 (spi, MAX3421_REG_HIRQ, m, NULL);
-				break;
-			}
-		}
-//		printk("D %d\n", timeout);
-		if (timeout < 0) {
-			result_code = MAX3421_HRSL_TIMEOUT;
-			dev_err (&spi->dev, "%s: timeout waiting for host-transfer-done\n",
-				 __FUNCTION__);
-		} else
-			result_code = hrsl & 0xf;
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	struct trace_entry *e = &trace_buffer.e[trace_buffer.index
+						% ARRAY_SIZE(trace_buffer.e)];
 
-#if 0
-		if (result_code != MAX3421_HRSL_OK && result_code != MAX3421_HRSL_NAK)
-			dev_err (&spi->dev, "%s: HRSL=0x%02x (%s)\n",
-				 __FUNCTION__, hrsl, hrsl_string[result_code]);
-#endif
+	if (trace_buffer.stopped)
+		return;
 
-		switch (result_code) {
-		case MAX3421_HRSL_OK:		return 0;
-		case MAX3421_HRSL_BUSY:		return -EINVAL;	// should never happen
-		case MAX3421_HRSL_BADREQ:	return -EINVAL; // bad val in HXFR
-		case MAX3421_HRSL_UNDEF:	return -EINVAL; // reserved
-		case MAX3421_HRSL_STALL:	return -EPIPE; // peripheral returned stall
-		case MAX3421_HRSL_WRONGPID:	return -EBADR;	// received wrong PID
-		case MAX3421_HRSL_BADBC:	return -EINVAL;	// bad byte count
-		case MAX3421_HRSL_PIDERR:	return -EBADR;	// received PID is corrupted
-		case MAX3421_HRSL_PKTERR:	return -EINVAL;	// packet error (stuff, EOP)
-		case MAX3421_HRSL_CRCERR:	return -EINVAL;	// CRC error
-		case MAX3421_HRSL_KERR:		return -EINVAL;	// K-state instead of response
-		case MAX3421_HRSL_JERR:		return -EINVAL;	// J-state instead of response
-		case MAX3421_HRSL_BABBLE:	return -EINVAL;	// device talked too long
-
-		case MAX3421_HRSL_TOGERR:
-#if 0
-			dev_err (&spi->dev, "%s: TOGGLE error on %s\n", __FUNCTION__,
-				 usb_pipein (urb->pipe) ? "input" : "output");
-#endif
-			if (usb_pipein (urb->pipe))
-				; // don't do anything (peripheral will switch its toggle)
-			else
-				// flip the send toggle bit:
-				spi_wr8 (spi, MAX3421_REG_HCTL,
-					 mask ((((hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1) ^ 1)
-					       + MAX3421_HCTL_SNDTOG0_BIT), NULL);
-			break;
+	e->timestamp = max3421_hcd->frame_number;
+	e->type = type;
+	e->data = data;
 
-		case MAX3421_HRSL_NAK:
-			if (usb_pipeint (urb->pipe))
-				break;
-			if (tx) {
-				/*
-				 * Before revision 0x13, the first byte of the SNDFIFO gets
-				 * corrupted when peripheral NAKs a send.  Follow the procedure
-				 * given in:
-				 *
-				 * http://datasheets.maximintegrated.com/en/errata/MAX3421E_REV1.pdf
-				 *
-				 * to work around this bug.
-				 */
-				spi_wr8 (spi, MAX3421_REG_SNDBC, 0, NULL);
-				spi_wr8 (spi, MAX3421_REG_SNDFIFO, tx->data[0], NULL);
-				spi_wr8 (spi, MAX3421_REG_SNDBC, tx->len, NULL);
-			}
-			++nakcnt;
-			if (retries > 4) {
-				retries = 0;
-				msleep (1);
-			}
-			break;
+	++trace_buffer.index;
 
-		case MAX3421_HRSL_TIMEOUT:
-			// retry request
-			break;
-		}
+	if (trace_buffer.stop_counter > 0) {
+		if (--trace_buffer.stop_counter == 0)
+			trace_buffer.stopped = 1;
 	}
-	return retval;
 }
 
-static int
-max3421_transfer_in (struct usb_hcd *hcd, struct urb *urb, u8 ep_addr)
+static void
+trace_dump(void)
 {
-	struct spi_device *spi = to_spi_device (hcd->self.controller);
-	u8 hirq, rcvbc, m_rcvdav = mask (MAX3421_HI_RCVDAV_BIT);
-	size_t remaining;
-	u16 max_packet;
-	int retval;
-	void *dst;
-
-	max_packet = usb_maxpacket (urb->dev, urb->pipe, 0);
-	if (max_packet > MAX3421_FIFO_SIZE)
-		max_packet = MAX3421_FIFO_SIZE;
-
-	remaining = urb->transfer_buffer_length;
-	dst = urb->transfer_buffer;
-	while (remaining > 0) {
-		// tell the device to send the data and wait for it to arrive:
-		if ((retval = max3421_dispatch_packet (hcd, urb, MAX3421_HXFR_BULK_IN (ep_addr),
-						       NULL)) < 0)
-			return retval;
-
-		hirq = spi_rd8 (spi, MAX3421_REG_HIRQ, NULL);
-		if (!(hirq & m_rcvdav)) {
-			// transfer completed but without receiving data...
-			dev_err (&spi->dev, "%s: transfer completed without data", __FUNCTION__);
-			return -ENODATA;
-		}
-
-		/*
-		 * The MAX3421E programming guide has conflicting info here:
-		 *
-		 *	The description of RCVDAVIRQ says "The CPU *must* clear this IRQ bit
-		 *	(by writing a 1 to it) before reading the RCVFIFO data.
-		 *
-		 * However, the earlier section on "Programming BULK-IN Transfers" says
-		 * that:
-		 *
-		 *	After the CPU retrieves the data, it cleares the RCVDAVIRQ bit.
-		 *
-		 * It appears the latter description is the correct one.  Of course, it's easy
-		 * to be off by one because if yoy get out of sync, there is no way to tell.
-		 */
-		do {
-			rcvbc = spi_rd8 (spi, MAX3421_REG_RCVBC, NULL);
-			if (rcvbc > MAX3421_FIFO_SIZE)
-				rcvbc = MAX3421_FIFO_SIZE;	// shouldn't happen
-			if (rcvbc > remaining)
-				rcvbc = remaining;		// probably should never happen...
-			spi_rd_fifo (spi, MAX3421_REG_RCVFIFO, dst, rcvbc, NULL);
-
-			dst += rcvbc;
-			remaining -= rcvbc;
-
-			// clear receive-data-available irq:
-			spi_wr8 (spi, MAX3421_REG_HIRQ, m_rcvdav, NULL);
-			hirq = spi_rd8 (spi, MAX3421_REG_HIRQ, NULL);
-		} while (hirq & m_rcvdav);
-
-		if (rcvbc < max_packet/* && !(urb->transfer_flags & URB_SHORT_NOT_OK)*/)
-			// short reads are usually OK on USB...
-			break;
+	int i, rd, num_entries = trace_buffer.index;
+	struct trace_entry *e;
+	const char *type_str;
+	static const char *const trace_cmd_name[] = {
+	  .TRACE_CMD =		"CMD",
+	  .TRACE_RESULT =	"RES",
+	  .TRACE_RX_LEN =	"#RL",
+	  .TRACE_RX_SIZE =	"#RS",
+	  .TRACE_TX_LEN =	"#TL",
+	  .TRACE_ADDR =		"ADR",
+	  .TRACE_TOGGLE =	"TOG"
+	};
+
+	if (!trace_buffer.stopped)
+		return;
+
+	if (num_entries > ARRAY_SIZE(trace_buffer.e))
+		num_entries = ARRAY_SIZE(trace_buffer.e);
+	rd = (trace_buffer.index - num_entries) % ARRAY_SIZE(trace_buffer.e);
+	for (i = 0; i < num_entries; ++i) {
+		e = &trace_buffer.e[rd];
+		if (e->type < ARRAY_SIZE(trace_cmd_name))
+			type_str = trace_cmd_name[e->type];
+		else
+			type_str = "UNK";
+		printk(KERN_DEBUG "%6u %05u %s 0x%02x\n",
+		       rd, e->timestamp, type_str, e->data);
+		rd = (rd + 1) % ARRAY_SIZE(trace_buffer.e);
 	}
+	trace_buffer.index = 0;
+	trace_buffer.stop_counter = trace_buffer.stopped = 0;
+}
 
-	urb->actual_length = urb->transfer_buffer_length - remaining;
+#else  /* !TRACE */
 
-	if (0) {
-		int i, m;
-
-		printk ("%s: received %u bytes\n", __FUNCTION__, urb->actual_length);
-		printk ("data:");
-		m = urb->actual_length;
-		if (m > 64)
-			m = 64;
-		for (i = 0; i < m; ++i)
-			printk (" %02x", ((u8*)urb->transfer_buffer)[i]);
-		printk ("\n");
-	}
+static inline void trace_stop(unsigned int delay_count) {}
+static inline void trace_record(struct usb_hcd *hcd, u8 type, u8 data) {}
+static inline void trace_dump(void) {}
 
-#if 0
-	if (remaining > 0 && urb->transfer_flags & URB_SHORT_NOT_OK) {
-		// This makes USB mass-storage fail; handled at higher level?
-		dev_err (&spi->dev,
-			 "%s: %zu bytes remaining but short xfers not OK; returning EREMOTEIO\n",
-			 __FUNCTION__, remaining);
-		return -EREMOTEIO;
-	}
-#endif
-
-	return 0;
-}
+#endif /* !TRACE */
 
-static int
-max3421_transfer_out (struct usb_hcd *hcd, struct urb *urb, u8 ep_addr)
+/*
+ * Caller must NOT hold HCD spinlock.
+ */
+static void
+max3421_set_address(struct usb_hcd *hcd, struct usb_device *dev, int epnum,
+		    int force_toggles)
 {
-	struct spi_device *spi = to_spi_device (hcd->self.controller);
-	struct max3421_errata_workaround w, *errata_workaround = NULL;
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
-	size_t remaining, len;
-	int retval, timeout;
-	u16 max_packet;
-	u8 hirq, m;
-	void *src;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	int old_epnum, same_ep, rcvtog, sndtog;
+	struct usb_device *old_dev;
+	u8 hctl;
 
-	max_packet = usb_maxpacket (urb->dev, urb->pipe, 1);
-	if (max_packet > MAX3421_FIFO_SIZE)
-		max_packet = MAX3421_FIFO_SIZE;
-
-	remaining = urb->transfer_buffer_length;
-	src = urb->transfer_buffer;
-	while (remaining > 0) {
-		len = remaining;
-		if (len > max_packet)
-			len = max_packet;
-
-		// wait for SNDBAV irq to be set
-		m = mask (MAX3421_HI_SNDBAV_BIT);
-		for (timeout = 1000; timeout >= 0; --timeout) {
-			hirq = spi_rd8 (spi, MAX3421_REG_HIRQ, NULL);
-			if (hirq & m)
-				break;
-			cond_resched ();
-		}
-//		printk("O %d\n", timeout);
-		if (timeout < 0) {
-			dev_err (&spi->dev, "%s: timed out waiting for send-buffer-available\n",
-				 __FUNCTION__);
-			return -ETIMEDOUT;
-		}
+	old_dev = max3421_hcd->loaded_dev;
+	old_epnum = max3421_hcd->loaded_epnum;
 
-		spi_wr_fifo (spi, MAX3421_REG_SNDFIFO, src, len, NULL);
-		spi_wr8 (spi, MAX3421_REG_SNDBC, len, NULL);
+	same_ep = (dev == old_dev && epnum == old_epnum);
+	if (same_ep && !force_toggles)
+		return;
 
-		if (max3421_hcd->chip_rev < 0x13) {
-			errata_workaround = &w;
-			w.len = len;
-			w.data = src;
-		}
+	if (old_dev && !same_ep) {
+		/* save the old end-points toggles: */
+		u8 hrsl = spi_rd8(spi, MAX3421_REG_HRSL, NULL);
 
-		// send the data to the device and wait for it to arrive:
-		if ((retval = max3421_dispatch_packet (hcd, urb, MAX3421_HXFR_BULK_OUT (ep_addr),
-						       errata_workaround)) < 0)
-			return retval;
+		rcvtog = (hrsl >> MAX3421_HRSL_RCVTOGRD_BIT) & 1;
+		sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
 
-		src += len;
-		remaining -= len;
+		/* no locking: HCD (i.e., we) own toggles, don't we? */
+		usb_settoggle(old_dev, old_epnum, 0, rcvtog);
+		usb_settoggle(old_dev, old_epnum, 1, sndtog);
 	}
-	urb->actual_length = urb->transfer_buffer_length;
-	return 0;
+	/* setup new endpoint's toggle bits: */
+	rcvtog = usb_gettoggle(dev, epnum, 0);
+	sndtog = usb_gettoggle(dev, epnum, 1);
+	hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) |
+		BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
+
+	max3421_hcd->loaded_epnum = epnum;
+	spi_wr8(spi, MAX3421_REG_HCTL, hctl, NULL);
+	trace_record(hcd, TRACE_TOGGLE,
+		     (hctl >> MAX3421_HCTL_RCVTOG0_BIT) & 0xf);
+
+	/*
+	 * Note: devnum for one and the same device can change during
+	 * address-assignment so it's best to just always load the
+	 * address whenever the end-point changed/was forced.
+	 */
+	max3421_hcd->loaded_dev = dev;
+	spi_wr8(spi, MAX3421_REG_PERADDR, dev->devnum, NULL);
+	trace_record(hcd, TRACE_ADDR, dev->devnum);
 }
 
 static int
-max3421_transfer (struct usb_hcd *hcd, struct urb *urb)
+max3421_ctrl_setup(struct usb_hcd *hcd, struct urb *urb)
 {
-	u8 ep_addr = usb_pipeendpoint (urb->pipe);
-	int dir_in;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
 
-	dir_in = usb_pipein (urb->pipe);
+	if (0) {
+		int i;
+		printk("%s: setup_packet =", __func__);
+		for (i = 0; i < 8; ++i)
+			printk(" %02x", urb->setup_packet[i]);
+		printk("\n");
+	}
+	spi_wr_fifo(spi, MAX3421_REG_SUDFIFO, urb->setup_packet, 8, NULL);
+	return MAX3421_HXFR_SETUP;
+}
 
-//	printk ("%s: transfer %d bytes %s EP %d\n", __FUNCTION__, urb->transfer_buffer_length, dir_in ? "from" : "to", ep_addr);
+static int
+max3421_transfer_in(struct usb_hcd *hcd, struct urb *urb)
+{
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	int epnum = usb_pipeendpoint(urb->pipe);
 
-	if (dir_in)
-		return max3421_transfer_in (hcd, urb, ep_addr);
-	else
-		return max3421_transfer_out (hcd, urb, ep_addr);
+	max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT);
+	return MAX3421_HXFR_BULK_IN(epnum);
 }
 
 static int
-max3421_control_req (struct usb_hcd *hcd, struct urb *urb)
+max3421_transfer_out(struct usb_hcd *hcd, struct urb *urb)
 {
-	struct spi_device *spi = to_spi_device (hcd->self.controller);
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
-	struct max3421_ep *max3421_ep = urb->ep->hcpriv;
-	int retval;
-	u8 cmd;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	int epnum = usb_pipeendpoint(urb->pipe);
+	size_t max_packet;
+	void *src;
 
-	// See USB 2.0 spec section 8.6.1 Initialization via SETUP Token
-	max3421_ep->recv_toggle = 1;
-	max3421_ep->send_toggle = 1;
-	max3421_hcd->last_ep = NULL;	// force reload
+	max_packet = usb_maxpacket(urb->dev, urb->pipe, 1);
+	src = urb->transfer_buffer + urb->actual_length;
 
-	if ((retval = max3421_set_address (hcd, urb)) < 0)
-		return retval;
+	if (max_packet > MAX3421_FIFO_SIZE) {
+		/*
+		 * We do not support isochronous transfers at this
+		 * time.
+		 */
+		dev_err(&spi->dev,
+			"%s: packet-size of %zu too big (limit is %zu bytes)",
+			__func__, max_packet, MAX3421_FIFO_SIZE);
+		max3421_hcd->urb_done = -EMSGSIZE;
+		return -EMSGSIZE;
+	}
+	max3421_hcd->curr_len = min((urb->transfer_buffer_length -
+				     urb->actual_length), max_packet);
+	trace_record(hcd, TRACE_TX_LEN, max3421_hcd->curr_len);
 
 	if (0) {
 		int i;
-		printk ("%s: setup_packet =", __FUNCTION__);
-		for (i = 0; i < 8; ++i)
-			printk (" %02x", urb->setup_packet[i]);
-		printk ("\n");
+		printk("%s: tx", __func__);
+		for (i = 0; i < max3421_hcd->curr_len; ++i)
+			printk(" %02x", ((u8 *) src)[i]);
+		printk("\n");
 	}
 
-	// write setup packet to setup-FIFO:
-	spi_wr_fifo (spi, MAX3421_REG_SUDFIFO, urb->setup_packet, 8, NULL);
+	spi_wr_fifo(spi, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len,
+		    NULL);
+	/*
+	 * SNDBAV may not be set if an OUT transfer was NAK'd, but the
+	 * only recourse then is to rewrite the FIFO anyhow (possibly
+	 * with the same data, if there hasn't been another transfer
+	 * in the meantime).
+	 */
+	spi_wr8(spi, MAX3421_REG_SNDBC, max3421_hcd->curr_len, NULL);
+	return MAX3421_HXFR_BULK_OUT(epnum);
+}
 
-	// transfer the setup-packet:
-	if ((retval = max3421_dispatch_packet (hcd, urb, MAX3421_HXFR_SETUP, NULL)) < 0)
-		return retval;
+/*
+ * Issue the next host-transfer command.
+ * Caller must NOT hold HCD spinlock.
+ */
+static void
+max3421_next_transfer(struct usb_hcd *hcd)
+{
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	struct urb *urb = max3421_hcd->curr_urb;
+	struct max3421_ep *max3421_ep = urb->ep->hcpriv;
+	int cmd = -EINVAL;
 
-	if (urb->transfer_buffer_length > 0)
-		if ((retval = max3421_transfer (hcd, urb)) < 0)
-			return retval;
+	switch (max3421_ep->pkt_state) {
+	case PKT_STATE_SETUP:
+		cmd = max3421_ctrl_setup(hcd, urb);
+		break;
 
-	// terminate the control transfer with a handshake IN or OUT command:
-	if (usb_pipein (urb->pipe))
-		cmd = MAX3421_HXFR_HS_OUT;	// IN transfers use OUT token
-	else
-		cmd = MAX3421_HXFR_HS_IN;	// and vice versa
-	return max3421_dispatch_packet (hcd, urb, cmd, NULL);
-}
+	case PKT_STATE_TRANSFER:
+		if (usb_pipein(urb->pipe))
+			cmd = max3421_transfer_in(hcd, urb);
+		else
+			cmd = max3421_transfer_out(hcd, urb);
+		break;
 
-static int
-max3421_bulk_req (struct usb_hcd *hcd, struct urb *urb)
-{
-	int retval;
+	case PKT_STATE_TERMINATE:
+		/*
+		 * IN transfers are terminated with HS_OUT token,
+		 * OUT transfers with HS_IN:
+		 */
+		if (usb_pipein(urb->pipe))
+			cmd = MAX3421_HXFR_HS_OUT;
+		else
+			cmd = MAX3421_HXFR_HS_IN;
+		break;
+	}
 
-	if (urb->transfer_buffer_length == 0)
-		return 0;
+	if (cmd < 0)
+		return;
 
-	if ((retval = max3421_set_address (hcd, urb)) < 0)
-		return retval;
+	spi_wr8(spi, MAX3421_REG_HXFR, cmd, NULL);
+	trace_record(hcd, TRACE_CMD, cmd);
 
-	return max3421_transfer (hcd, urb);
+	/* issue the command and wait for host-xfer-done interrupt: */
+	max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT);
+	spi_wr8(spi, MAX3421_REG_HIEN, max3421_hcd->hien, NULL);
 }
 
-static int
-max3421_interrupt_req (struct usb_hcd *hcd, struct urb *urb)
+/*
+ * Find the next end-point to process.  Since we do not anticipate
+ * ever connecting a USB hub to the MAX3421 chip, at most USB device
+ * can be connected and we can use a simplistic scheduler: at the
+ * start of a frame, schedule all periodic transfers.  Once that is
+ * done, use the remainder of the frame to process non-periodic (bulk
+ * & control) transfers.
+ *
+ * Caller must NOT hold HCD spinlock.
+ */
+static void
+max3421_next_ep(struct usb_hcd *hcd)
 {
-	int retval;
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	struct urb *urb, *curr_urb = NULL;
+	struct max3421_ep *max3421_ep;
+	int epnum, force_toggles = 0;
+	struct usb_host_endpoint *ep;
+	struct list_head *pos;
+	unsigned long flags;
+
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
+
+	for (;
+	     max3421_hcd->sched_pass < SCHED_PASS_DONE;
+	     ++max3421_hcd->sched_pass)
+		list_for_each(pos, &max3421_hcd->ep_list) {
+			urb = NULL;
+			max3421_ep = container_of(pos, struct max3421_ep,
+						  ep_list);
+			ep = max3421_ep->ep;
+
+			switch (usb_endpoint_type(&ep->desc)) {
+			case USB_ENDPOINT_XFER_ISOC:
+			case USB_ENDPOINT_XFER_INT:
+				if (max3421_hcd->sched_pass !=
+				    SCHED_PASS_PERIODIC)
+					continue;
+				break;
 
-	if (urb->transfer_buffer_length == 0)
-		return 0;
+			case USB_ENDPOINT_XFER_CONTROL:
+			case USB_ENDPOINT_XFER_BULK:
+				if (max3421_hcd->sched_pass !=
+				    SCHED_PASS_NON_PERIODIC)
+					continue;
+				break;
+			}
 
-	if ((retval = max3421_set_address (hcd, urb)) < 0)
-		return retval;
+			if (list_empty(&ep->urb_list))
+				continue;	/* nothing to do */
+			urb = list_first_entry(&ep->urb_list, struct urb,
+					       urb_list);
+
+			switch (usb_endpoint_type(&ep->desc)) {
+			case USB_ENDPOINT_XFER_CONTROL:
+				/*
+				 * Allow one control transaction per
+				 * frame per endpoint:
+				 */
+				if (frame_diff(max3421_ep->last_active,
+					       max3421_hcd->frame_number) == 0)
+					continue;
+				break;
+
+			case USB_ENDPOINT_XFER_BULK:
+				if (max3421_ep->retransmit
+				    && (frame_diff(max3421_ep->last_active,
+						   max3421_hcd->frame_number)
+					== 0))
+					/*
+					 * We already tried this EP
+					 * during this frame and got a
+					 * NAK; wait for next frame
+					 */
+					continue;
+				break;
+
+			case USB_ENDPOINT_XFER_ISOC:
+			case USB_ENDPOINT_XFER_INT:
+				if (frame_diff(max3421_hcd->frame_number,
+					       max3421_ep->last_active)
+				    < urb->interval)
+					/*
+					 * We already processed this
+					 * end-point in the current
+					 * frame
+					 */
+					continue;
+				break;
+			}
 
-	return max3421_transfer (hcd, urb);
+			/* move current ep to tail: */
+			list_move_tail(pos, &max3421_hcd->ep_list);
+			curr_urb = urb;
+			goto done;
+		}
+done:
+	urb = max3421_hcd->curr_urb = curr_urb;
+	if (!urb) {
+		spin_unlock_irqrestore(&max3421_hcd->lock, flags);
+		return;
+	}
+
+	epnum = usb_endpoint_num(&urb->ep->desc);
+	if (max3421_ep->retransmit)
+		/* restart (part of) a USB transaction: */
+		max3421_ep->retransmit = 0;
+	else {
+		/* start USB transaction: */
+		if (usb_endpoint_xfer_control(&ep->desc)) {
+			/*
+			 * See USB 2.0 spec section 8.6.1
+			 * Initialization via SETUP Token:
+			 */
+			usb_settoggle(urb->dev, epnum, 0, 1);
+			usb_settoggle(urb->dev, epnum, 1, 1);
+			max3421_ep->pkt_state = PKT_STATE_SETUP;
+			force_toggles = 1;
+		} else
+			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
+		max3421_ep->retries = 0;
+	}
+
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
+
+	if (urb->unlinked) {
+		max3421_hcd->urb_done = 1;
+		return;
+	}
+
+	max3421_ep->last_active = max3421_hcd->frame_number;
+	max3421_set_address(hcd, urb->dev, epnum, force_toggles);
+	max3421_next_transfer(hcd);
 }
 
-static int
-max3421_isochronous_req (struct usb_hcd *hcd, struct urb *urb)
+/*
+ * Caller must NOT hold HCD spinlock.
+ */
+static void
+max3421_retransmit(struct usb_hcd *hcd)
 {
-	printk("%s: implement me\n", __FUNCTION__);
-	return 0;
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	struct urb *urb = max3421_hcd->curr_urb;
+	struct max3421_ep *max3421_ep;
+
+	max3421_ep = urb->ep->hcpriv;
+	max3421_ep->retransmit = 1;
+	max3421_next_ep(hcd);
 }
 
+/*
+ * Caller must NOT hold HCD spinlock.
+ */
 static void
-max3421_process_queue (struct usb_hcd *hcd)
+max3421_recv_data_available(struct usb_hcd *hcd)
 {
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
-	struct max3421_urbp *urbp, *tmp_urbp;
-	struct urb *urb;
-	int total = 64 /* bytes */ * 19 /* packets */;	// USB_SPEED_FULL
-	int type, status;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	size_t max_packet, remaining, transfer_size;
+	struct urb *urb = max3421_hcd->curr_urb;
+	u8 rcvbc;
 
-	++qint;
+	rcvbc = spi_rd8(spi, MAX3421_REG_RCVBC, NULL);
+	trace_record(hcd, TRACE_RX_LEN, rcvbc);
 
-	// XXX should iterate over end-points and their URB-lists
-	list_for_each_entry_safe (urbp, tmp_urbp, &max3421_hcd->urbp_list, urbp_list) {
-		status = -EINPROGRESS;
+if (!urb) {printk("%s: no URB!", __func__); trace_stop(0); trace_dump(); return;}
 
-		++qlen;
+	if (rcvbc > MAX3421_FIFO_SIZE)
+		rcvbc = MAX3421_FIFO_SIZE;
+	if (urb->actual_length >= urb->transfer_buffer_length)
+		remaining = 0;
+	else
+		remaining = urb->transfer_buffer_length - urb->actual_length;
+	transfer_size = rcvbc;
+	if (transfer_size > remaining)
+		transfer_size = remaining;
+	if (transfer_size > 0) {
+		void *dst = urb->transfer_buffer + urb->actual_length;
+		spi_rd_fifo(spi, MAX3421_REG_RCVFIFO, dst, transfer_size,
+			    NULL);
+		urb->actual_length += transfer_size;
+		remaining -= transfer_size;
+		if (0) {
+			int i;
+			printk("%s: rx", __func__);
+			for (i = 0; i < min(urb->actual_length, 32u); ++i)
+				printk(" %02x",
+				       ((u8 *)urb->transfer_buffer)[i]);
+			printk("\n");
+		}
+	}
 
-		if (max3421_hcd->rh_state != MAX3421_RH_RUNNING)
-			return;
+	/* ack the RCVDAV irq now that the FIFO has been read: */
+	spi_wr8(spi, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT), NULL);
 
-		urb = urbp->urb;
-		type = usb_pipetype (urb->pipe);
-		if (total <= 0 && type == PIPE_BULK)
-			// used up this frame's non-periodic bandwidth
-			continue;
+	/*
+	 * USB 2.0 Section 5.3.2 Pipes: packets must be full size
+	 * except for last one.
+	 */
+	max_packet = usb_maxpacket(urb->dev, urb->pipe, 0);
+	if (max_packet > MAX3421_FIFO_SIZE) {
+		/* we do not support isochronous transfers at this time */
+		max3421_hcd->urb_done = -EINVAL;
+		dev_err(&spi->dev,
+			"%s: packet-size of %zu too big (limit is %zu bytes)",
+			__func__, max_packet, MAX3421_FIFO_SIZE);
+		return;
+	}
 
-		// We are commited to this URB now.  Unlink it so we can release the spinlock.
-		list_del (&urbp->urbp_list);
-		kfree (urbp);
-		usb_hcd_unlink_urb_from_ep (hcd, urb);
-		spin_unlock (&max3421_hcd->lock);
+	if (remaining > 0) {
+		if (rcvbc < max_packet) {
+			if (urb->transfer_flags & URB_SHORT_NOT_OK) {
+				/*
+				 * remaining > 0 and received an
+				 * unexpected partial packet ->
+				 * error
+				 */
+				trace_record(hcd, TRACE_RX_SIZE,
+					     urb->transfer_buffer_length);
+				max3421_hcd->urb_done = -EREMOTEIO;
+			} else
+				/* short read, but it's OK */
+				max3421_hcd->urb_done = 1;
+		}
+	} else
+		max3421_hcd->urb_done = 1;
+}
 
-		if (!urb->unlinked) {
-			switch (type) {
+/*
+ * Caller must NOT hold HCD spinlock.
+ */
+static void
+max3421_host_transfer_done(struct usb_hcd *hcd)
+{
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	struct urb *urb = max3421_hcd->curr_urb;
+	struct max3421_ep *max3421_ep;
+	u8 result_code, hrsl;
+	int urb_done = 0;
+
+	max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) |
+			       BIT(MAX3421_HI_RCVDAV_BIT));
+	spi_wr8(spi, MAX3421_REG_HIEN, max3421_hcd->hien, NULL);
+
+if (!urb) {printk("%s: no URB!", __func__); trace_stop(0); trace_dump(); return;}
+
+	if (urb->unlinked) {
+		max3421_hcd->urb_done = 1;
+		return;
+	}
 
-			case PIPE_CONTROL:
-if (urb->number_of_packets > 1) printk ("number_of_packets %d interval %d\n", urb->number_of_packets, urb->interval);
-				status = max3421_control_req (hcd, urb);
-				break;
+	max3421_ep = urb->ep->hcpriv;
 
-			case PIPE_BULK:
-				status = max3421_bulk_req (hcd, urb);
-				total -= urb->actual_length;
-				break;
+	hrsl = spi_rd8(spi, MAX3421_REG_HRSL, NULL);
+	result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
+	trace_record(hcd, TRACE_RESULT, result_code);
 
-			case PIPE_INTERRUPT:
-				status = max3421_interrupt_req (hcd, urb);
-				break;
+	++max3421_hcd->err_stat[result_code];
 
-			case PIPE_ISOCHRONOUS:
-				status = max3421_isochronous_req (hcd, urb);
-				break;
-			}
+	switch (result_code) {
+	case MAX3421_HRSL_OK:
+		break;
+
+	case MAX3421_HRSL_WRONGPID:	/* received wrong PID */
+	case MAX3421_HRSL_BUSY:		/* SIE busy */
+	case MAX3421_HRSL_BADREQ:	/* bad val in HXFR */
+	case MAX3421_HRSL_UNDEF:	/* reserved */
+	case MAX3421_HRSL_KERR:		/* K-state instead of response */
+	case MAX3421_HRSL_JERR:		/* J-state instead of response */
+		/*
+		 * packet experienced an error that we cannot recover
+		 * from; report error
+		 */
+		max3421_hcd->urb_done = hrsl_to_error[result_code];
+		return;
+
+	case MAX3421_HRSL_TOGERR:
+		if (usb_pipein(urb->pipe))
+			; /* don't do anything (device will switch toggle) */
+		else {
+			/* flip the send toggle bit: */
+			int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
+			sndtog ^= 1;
+			spi_wr8(spi, MAX3421_REG_HCTL,
+				BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT), NULL);
+		}
+		/* FALL THROUGH */
+	case MAX3421_HRSL_BADBC:	/* bad byte count */
+	case MAX3421_HRSL_PIDERR:	/* received PID is corrupted */
+	case MAX3421_HRSL_PKTERR:	/* packet error (stuff, EOP) */
+	case MAX3421_HRSL_CRCERR:	/* CRC error */
+	case MAX3421_HRSL_BABBLE:	/* device talked too long */
+	case MAX3421_HRSL_TIMEOUT:
+		if (max3421_ep->retries++ < USB_MAX_RETRIES)
+			/* retry the packet again in the next frame */
+			max3421_retransmit(hcd);
+		else
+			/* Based on ohci.h cc_to_err[]: */
+			max3421_hcd->urb_done = hrsl_to_error[result_code];
+		return;
+
+	case MAX3421_HRSL_STALL:
+#if 1
+		dev_err(&spi->dev, "%s: unexpected error HRSL=0x%02x (%s)",
+			__func__, hrsl, hrsl_string[result_code]);
+		trace_stop(0);
+#endif
+		max3421_hcd->urb_done = hrsl_to_error[result_code];
+		return;
+
+	case MAX3421_HRSL_NAK:
+		/*
+		 * Device wasn't ready for data or has no data available:
+		 * retry the packet again in the next frame
+		 */
+		max3421_retransmit(hcd);
+		return;
+	}
+
+	switch (max3421_ep->pkt_state) {
+
+	case PKT_STATE_SETUP:
+		if (urb->transfer_buffer_length > 0)
+			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
+		else
+			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
+		break;
+
+	case PKT_STATE_TRANSFER:
+		if (usb_pipein(urb->pipe))
+			/* pick up result from max3421_recv_data_available(): */
+			urb_done = max3421_hcd->urb_done;
+		else {
+			urb->actual_length += max3421_hcd->curr_len;
+			if (urb->actual_length >= urb->transfer_buffer_length)
+				urb_done = 1;
 		}
-//printk ("type %d %s len %u buffer=%p EP=%u -> %d (actlen %d)\n", type, usb_pipein (urb->pipe) ? "IN " : "OUT", urb->transfer_buffer_length, urb->transfer_buffer, usb_pipeendpoint (urb->pipe), status, urb->actual_length);
-		usb_hcd_giveback_urb (hcd, urb, status);
-		spin_lock (&max3421_hcd->lock);
+
+		if (urb_done && usb_pipetype(urb->pipe) == PIPE_CONTROL) {
+			/*
+			 * we aren't really done - we still need to
+			 * terminate the control transfer:
+			 */
+			max3421_hcd->urb_done = urb_done = 0;
+			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
+		}
+		break;
+
+	case PKT_STATE_TERMINATE:
+		urb_done = 1;
+		break;
+	}
+
+	if (urb_done) {
+		max3421_hcd->urb_done = 1;
+		return;
 	}
+	max3421_next_transfer(hcd);
 }
 
+/*
+ * Caller must NOT hold HCD spinlock.
+ */
 static void
-max3421_detect_conn (struct usb_hcd *hcd)
+max3421_detect_conn(struct usb_hcd *hcd)
 {
-	struct spi_device *spi = to_spi_device (hcd->self.controller);
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	unsigned long flags;
+	unsigned int jk;
 	u8 hrsl, mode;
-	unsigned jk;
 
-	hrsl = spi_rd8 (spi, MAX3421_REG_HRSL, NULL);
+	hrsl = spi_rd8(spi, MAX3421_REG_HRSL, NULL);
 
 	jk = ((((hrsl >> MAX3421_HRSL_JSTATUS_BIT) & 1) << 0) |
 	      (((hrsl >> MAX3421_HRSL_KSTATUS_BIT) & 1) << 1));
 
-	printk ("%s: JK=%x\n", __FUNCTION__, jk);
-
-	mode = spi_rd8 (spi, MAX3421_REG_MODE, NULL);
+	mode = spi_rd8(spi, MAX3421_REG_MODE, NULL);
 
 	switch (jk) {
-	case 0x0: // SE0: disconnect
-		// turn off SOFKAENAB bit to avoid getting interrupt every milli-second:
-		mode &= ~mask (MAX3421_MODE_SOFKAENAB_BIT);
-		spi_wr8 (spi, MAX3421_REG_MODE, mode, NULL);
-		max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION;
+	case 0x0: /* SE0: disconnect */
+		/*
+		 * Turn off SOFKAENAB bit to avoid getting interrupt
+		 * every milli-second:
+		 */
+		mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT);
+		spi_wr8(spi, MAX3421_REG_MODE, mode, NULL);
 		break;
 
-	case 0x1: // J=0, K=1 => low-speed (in full-speed mode or vice versa)
-	case 0x2: // J=1, K=0 => full-speed (in full-speed mode or vice versa)
+	case 0x1: /* J=0,K=1: low-speed (in full-speed mode or vice versa) */
+	case 0x2: /* J=1,K=0: full-speed (in full-speed mode or vice versa) */
 		if (jk == 0x2)
-			// need to switch to the other speed:
-			mode ^= mask (MAX3421_MODE_LOWSPEED_BIT);
+			/* need to switch to the other speed: */
+			mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT);
 #if 0
-		// XXX This only needs to be turned on when talking to a low-speed device through
-		// a USB hub.  But how to tell?
-		if (mode & mask (MAX3421_MODE_LOWSPEED_BIT))
-			// preceide every low-speed packet with a full-speed PRE PID
-			mode |= mask (MAX3421_MODE_HUBPRE_BIT);
+		/*
+		 * XXX This only needs to be turned on when talking to
+		 * a low-speed device through a USB hub.  But how to
+		 * tell?
+		 */
+		if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT))
+			/*
+			 * precede every low-speed packet with a
+			 * full-speed PRE PID:
+			 */
+			mode |= BIT(MAX3421_MODE_HUBPRE_BIT);
 #endif
-		// turn on SOFKAENAB bit:
-		mode |= mask (MAX3421_MODE_SOFKAENAB_BIT);
-		spi_wr8 (spi, MAX3421_REG_MODE, mode, NULL);
-		if ((mode >> MAX3421_MODE_LOWSPEED_BIT) & 1)
-			max3421_hcd->port_status |=  USB_PORT_STAT_LOW_SPEED;
-		else
-			max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED;
-		max3421_hcd->port_status |= USB_PORT_STAT_CONNECTION;
+		/* turn on SOFKAENAB bit: */
+		mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT);
+		spi_wr8(spi, MAX3421_REG_MODE, mode, NULL);
 		break;
 
-	case 0x3: // illegal
+	case 0x3: /* illegal */
 		break;
 	}
+
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
+	if (mode & BIT(MAX3421_MODE_SOFKAENAB_BIT))
+		max3421_hcd->port_status |=  USB_PORT_STAT_CONNECTION;
+	else
+		max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION;
+	if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT))
+		max3421_hcd->port_status |=  USB_PORT_STAT_LOW_SPEED;
+	else
+		max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED;
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 }
 
-/*
- * This executes as a separate thread with MAX3421 interrupt masked.
- * We have to run in a separate thread because it's impossible to
- * communicate with a SPI device in a non-blocking fashion.
- */
 static irqreturn_t
-max3421_irq_thread (int irq, void *dev_id)
+max3421_irq_handler(int irq, void *dev_id)
 {
-	struct max3421_hcd *max3421_hcd;
 	struct usb_hcd *hcd = dev_id;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+
+	if (max3421_hcd->spi_thread &&
+	    max3421_hcd->spi_thread->state != TASK_RUNNING)
+		wake_up_process(max3421_hcd->spi_thread);
+	max3421_hcd->do_enable_irq = 1;
+	disable_irq_nosync(spi->irq);
+	return IRQ_HANDLED;
+}
+
+/* Return zero if no work was performed, 1 otherwise.  */
+static int
+max3421_handle_irqs(struct usb_hcd *hcd)
+{
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	struct urb *urb_to_give_back = NULL;
 	u32 chg, old_port_status;
-	struct spi_device *spi;
-	u8 hirq, ack;
+	unsigned long flags;
+	int status = 0;
+	u8 hirq;
 
-	max3421_hcd = hcd_to_max3421 (hcd);
+	/*
+	 * Read and ack pending interrupts (CPU must never
+	 * clear SNDBAV directly and RCVDAV must be cleared by
+	 * max3421_recv_data_available()!):
+	 */
+	hirq = spi_rd8(spi, MAX3421_REG_HIRQ, NULL);
+	hirq &= max3421_hcd->hien;
+	if (!hirq)
+		return 0;
+	spi_wr8(spi, MAX3421_REG_HIRQ,
+		hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) |
+			 BIT(MAX3421_HI_RCVDAV_BIT)), NULL);
+
+	if (hirq & BIT(MAX3421_HI_FRAME_BIT)) {
+		max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1)
+					     & USB_MAX_FRAME_NUMBER);
+		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
+		if (!max3421_hcd->curr_urb)
+			max3421_next_ep(hcd);
+	}
 
-	spin_lock (&max3421_hcd->lock);
+	if (hirq & BIT(MAX3421_HI_RCVDAV_BIT))
+		max3421_recv_data_available(hcd);
 
-	spi = to_spi_device (hcd->self.controller);
+	if (hirq & BIT(MAX3421_HI_HXFRDN_BIT))
+		max3421_host_transfer_done(hcd);
+
+	if (hirq & BIT(MAX3421_HI_CONDET_BIT))
+		max3421_detect_conn(hcd);
+
+	if (max3421_hcd->urb_done) {
+		status = max3421_hcd->urb_done;
+if (status < 0) printk("%s: status=%d\n", __func__, status);
+		if (status > 0)
+			status = 0;
+		max3421_hcd->urb_done = 0;
+		urb_to_give_back = max3421_hcd->curr_urb;
+
+		if (urb_to_give_back) {
+			max3421_hcd->curr_urb = NULL;
+			usb_hcd_unlink_urb_from_ep(hcd, urb_to_give_back);
+
+/*if (urb_to_give_back) printk("urb %p done! (actual %d, status %d)\n", urb_to_give_back, urb_to_give_back->actual_length, status);*/
+			/* must be called without holding the HCD spinlock: */
+			usb_hcd_giveback_urb(hcd, urb_to_give_back, status);
+		}
+		max3421_next_ep(hcd);
+	}
 
+	/*
+	 * Now process interrupts that may affect HCD state
+	 * other than the end-points:
+	 */
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
 	old_port_status = max3421_hcd->port_status;
 
-	// read pending interrupts:
-	hirq = spi_rd8 (spi, MAX3421_REG_HIRQ, NULL);
-	// ack pending interrupts (but CPU must never clear SNDBAV directly):
-	ack = hirq & ~mask (MAX3421_HI_SNDBAV_BIT);
-	spi_wr8 (spi, MAX3421_REG_HIRQ, ack, NULL);
-
-	if (hirq & mask (MAX3421_HI_BUSEVENT_BIT))
-		printk ("%s: BUSEVENT\n", __FUNCTION__);
-	if (hirq & mask (MAX3421_HI_RWU_BIT))
-		printk ("%s: RWU\n", __FUNCTION__);
-	if (hirq & mask (MAX3421_HI_RCVDAV_BIT))
-		printk ("%s: RCVDAV\n", __FUNCTION__);
-	if (hirq & mask (MAX3421_HI_SUSDN_BIT))
-		printk ("%s: SUSDN\n", __FUNCTION__);
-	if (hirq & mask (MAX3421_HI_CONDET_BIT))
-		max3421_detect_conn (hcd);
-	if (hirq & mask (MAX3421_HI_FRAME_BIT)) {
-		// handle frame interrupt
+	if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) {
 		if (max3421_hcd->port_status & USB_PORT_STAT_RESET) {
+			/* BUSEVENT due to completion of Bus Reset */
+			printk("%s: BUSEVENT Bus Reset Done\n", __func__);
 			max3421_hcd->port_status &= ~USB_PORT_STAT_RESET;
 			max3421_hcd->port_status |=  USB_PORT_STAT_ENABLE;
-		} else if (hirq & mask (MAX3421_HI_SNDBAV_BIT))
-			max3421_process_queue (hcd);	// this releases/reacquires our lock!
+		} else {
+			/* BUSEVENT due to completion of Bus Resume */
+			printk("%s: BUSEVENT Bus Resume Done\n", __func__);
+		}
 	}
-	if (hirq & mask (MAX3421_HI_HXFRDN_BIT))
-		// handle frame interrupt
-		printk ("%s: HXFRDN\n", __FUNCTION__);
-
-	spin_unlock (&max3421_hcd->lock);
+	if (hirq & BIT(MAX3421_HI_RWU_BIT))
+		printk("%s: RWU\n", __func__);
+	if (hirq & BIT(MAX3421_HI_SUSDN_BIT))
+		printk("%s: SUSDN\n", __func__);
 
 	chg = (old_port_status ^ max3421_hcd->port_status);
-	if (chg) {
-		max3421_hcd->port_status |= chg << 16;
-		usb_hcd_poll_rh_status (hcd);
-	}
+	max3421_hcd->port_status |= chg << 16;
 
-	return IRQ_HANDLED;
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
+
+	if (chg)
+		usb_hcd_poll_rh_status(hcd);
+
+	if (1) {
+		static unsigned long last_time;
+		if (jiffies - last_time > 5*HZ) {
+			int i;
+			printk("%s: fnum %5u urb types",
+			       __func__, max3421_hcd->frame_number);
+			for (i = 0; i < 4; ++i) {
+				printk(" %3lu", max3421_hcd->type_stat[i]);
+				max3421_hcd->type_stat[i] = 0;
+			}
+			printk(" errcnt");
+			for (i = 0; i < 16; ++i) {
+				printk(" %lu", max3421_hcd->err_stat[i]);
+				max3421_hcd->err_stat[i] = 0;
+			}
+			printk("\n");
+			last_time = jiffies;
+
+			trace_dump();
+		}
+	}
+	return 1;
 }
 
 static int
-max3421_reset_port (struct usb_hcd *hcd)
+max3421_reset_hcd(struct usb_hcd *hcd)
 {
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
-	struct spi_device *spi;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	int timeout;
 
-	max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
-				      USB_PORT_STAT_LOW_SPEED |
-				      USB_PORT_STAT_HIGH_SPEED);
+printk("%s()\n", __func__);
+	/* perform a chip reset and wait for OSCIRQ signal to appear: */
+	spi_wr8(spi, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT),
+		NULL);
+	/* clear reset: */
+	spi_wr8(spi, MAX3421_REG_USBCTL, 0, NULL);
+	timeout = 1000;
+	while (1) {
+		if (spi_rd8(spi, MAX3421_REG_USBIRQ, NULL)
+		    & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT))
+			break;
+		if (--timeout < 0) {
+			dev_err(&spi->dev,
+				"timed out waiting for oscillator OK signal");
+			return 1;
+		}
+		cond_resched();
+	}
 
-	spi = to_spi_device (hcd->self.controller);
+	/*
+	 * Turn on host mode, automatic generation of SOF packets, and
+	 * enable pull-down registers on DM/DP:
+	 */
+	spi_wr8(spi, MAX3421_REG_MODE,
+		(BIT(MAX3421_MODE_HOST_BIT) |
+		 BIT(MAX3421_MODE_SOFKAENAB_BIT) |
+		 BIT(MAX3421_MODE_DMPULLDN_BIT) |
+		 BIT(MAX3421_MODE_DPPULLDN_BIT)), NULL);
 
-	// perform a USB bus reset:
-	spi_wr8 (spi, MAX3421_REG_HCTL, mask (MAX3421_HCTL_BUSRST_BIT), NULL);
-	return 0;
+#if 0
+	/* reset frame-number: */
+	max3421_hcd->frame_number = 0;
+	spi_wr8(spi, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT), NULL);
+#endif
+
+	/* sample the state of the D+ and D- lines */
+	spi_wr8(spi, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT), NULL);
+	max3421_detect_conn(hcd);
+
+	/* enable frame, connection-detected, and bus-event interrupts: */
+	max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) |
+			     BIT(MAX3421_HI_CONDET_BIT) |
+			     BIT(MAX3421_HI_BUSEVENT_BIT));
+	spi_wr8(spi, MAX3421_REG_HIEN, max3421_hcd->hien, NULL);
+
+	/* enable interrupts: */
+	spi_wr8(spi, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT), NULL);
+	return 1;
 }
 
 static int
-max3421_reset (struct usb_hcd *hcd)
+max3421_spi_thread(void *dev_id)
 {
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
-	struct spi_device *spi;
-	int timeout;
+	struct usb_hcd *hcd = dev_id;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	int i_worked = 1;
+	u8 hirq, rev;
 
-	hcd->self.sg_tablesize = 0;
-	hcd->speed = HCD_USB2;
-	hcd->self.root_hub->speed = USB_SPEED_FULL;
+printk("%s()\n", __func__);
 
-	spi = to_spi_device (hcd->self.controller);
+	/* set full-duplex SPI mode, low-active interrupt pin: */
+	spi_wr8(spi, MAX3421_REG_PINCTL,
+		(BIT(MAX3421_PINCTL_FDUPSPI_BIT) |	/* full-duplex */
+		 BIT(MAX3421_PINCTL_INTLEVEL_BIT)),	/* low-active irq */
+		NULL);
 
-	// perform a chip reset and wait for OSCIRQ signal to appear:
-	spi_wr8 (spi, MAX3421_REG_USBCTL, mask (MAX3421_USBCTL_CHIPRES_BIT), NULL);
-	// clear reset:
-	spi_wr8 (spi, MAX3421_REG_USBCTL, 0, NULL);
-	timeout = 1000;
-	while (!(spi_rd8 (spi, MAX3421_REG_USBIRQ, NULL) & mask (MAX3421_USBIRQ_OSCOKIRQ_BIT))) {
-		if (--timeout < 0) {
-			dev_err (&spi->dev, "timed out waiting for oscillator OK signal");
-			return -ENODEV;
+	rev = spi_rd8(spi, MAX3421_REG_REVISION, &hirq);
+	if (rev != 0x12 && rev != 0x13) {
+		dev_err(&spi->dev, "bad rev 0x%x (hirq 0x%x)", rev, hirq);
+		return -ENODEV;
+	}
+
+	dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n",
+		 rev, spi->max_speed_hz, spi->bits_per_word, spi->irq);
+
+	while (!kthread_should_stop()) {
+		if (!i_worked) {
+			set_current_state(TASK_INTERRUPTIBLE);
+			if (max3421_hcd->do_enable_irq) {
+				max3421_hcd->do_enable_irq = 0;
+				enable_irq(spi->irq);
+			}
+			schedule();
+			__set_current_state(TASK_RUNNING);
 		}
-		cond_resched ();
+		i_worked = 0;
+
+		if (max3421_hcd->do_reset_hcd) {
+printk("%s: reset HCD\n", __func__);
+			/* reset the HCD: */
+			max3421_hcd->do_reset_hcd = 0;
+			i_worked |= max3421_reset_hcd(hcd);
+		}
+
+		if (max3421_hcd->do_reset_port) {
+printk("%s: reset port\n", __func__);
+			/* perform a USB bus reset: */
+			max3421_hcd->do_reset_port = 0;
+			spi_wr8(spi, MAX3421_REG_HCTL,
+				BIT(MAX3421_HCTL_BUSRST_BIT), NULL);
+			i_worked |= 1;
+		}
+		i_worked |= max3421_handle_irqs(hcd);
 	}
+	set_current_state(TASK_RUNNING);
+	dev_info(&spi->dev, "SPI thread exiting");
+	return 0;
+}
 
-	// turn on host mode, automatic generation of SOF packets, and
-	// enable pull-down registers on DM/DP:
-	spi_wr8 (spi, MAX3421_REG_MODE,
-		 (mask (MAX3421_MODE_HOST_BIT) |
-		  mask (MAX3421_MODE_SOFKAENAB_BIT) |
-		  mask (MAX3421_MODE_DMPULLDN_BIT) |
-		  mask (MAX3421_MODE_DPPULLDN_BIT)), NULL);
-
-	// enable frame, connection-detected, and bus-event interrupts:
-	max3421_hcd->hien = (mask (MAX3421_HI_FRAME_BIT) |
-			     mask (MAX3421_HI_CONDET_BIT) |
-			     mask (MAX3421_HI_BUSEVENT_BIT));
-	spi_wr8 (spi, MAX3421_REG_HIEN, max3421_hcd->hien, NULL);
-
-	// clear pending host interrupts:
-	spi_wr8 (spi, MAX3421_REG_HIRQ, 0xff, NULL);
-
-	// enable interrupts:
-	spi_wr8 (spi, MAX3421_REG_CPUCTL, mask (MAX3421_CPUCTL_IE_BIT), NULL);
-
-	// sample the state of the D+ and D- lines
-	spi_wr8 (spi, MAX3421_REG_HCTL, mask (MAX3421_HCTL_SAMPLEBUS_BIT), NULL);
-	max3421_detect_conn (hcd);
+static int
+max3421_reset_port(struct usb_hcd *hcd)
+{
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+
+	max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
+				      USB_PORT_STAT_LOW_SPEED);
+	max3421_hcd->do_reset_port = 1;
+	wake_up_process(max3421_hcd->spi_thread);
 	return 0;
 }
 
+static int
+max3421_reset(struct usb_hcd *hcd)
+{
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+
+printk("%s()\n", __func__);
+	hcd->self.sg_tablesize = 0;
+	hcd->speed = HCD_USB2;
+	hcd->self.root_hub->speed = USB_SPEED_FULL;
+	max3421_hcd->do_reset_hcd = 1;
+	wake_up_process(max3421_hcd->spi_thread);
+	return 0;
+}
 
 static int
-max3421_start (struct usb_hcd *hcd)
+max3421_start(struct usb_hcd *hcd)
 {
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 
-	spin_lock_init (&max3421_hcd->lock);
+	spin_lock_init(&max3421_hcd->lock);
 	max3421_hcd->rh_state = MAX3421_RH_RUNNING;
 
-	INIT_LIST_HEAD (&max3421_hcd->urbp_list);
+	INIT_LIST_HEAD(&max3421_hcd->ep_list);
 
 	hcd->power_budget = POWER_BUDGET;
 	hcd->state = HC_STATE_RUNNING;
@@ -1013,99 +1435,119 @@ max3421_start (struct usb_hcd *hcd)
 }
 
 static void
-max3421_stop (struct usb_hcd *hcd)
+max3421_stop(struct usb_hcd *hcd)
 {
-	printk ("%s()\n", __FUNCTION__);
+	printk("%s()\n", __func__);
 }
 
 static int
-max3421_urb_enqueue (struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
+max3421_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
 {
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
-	struct max3421_urbp *urbp;
+	struct spi_device *spi = to_spi_device(hcd->self.controller);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	struct max3421_ep *max3421_ep;
+	unsigned long flags;
 	int retval;
 
-	if (0) {
-		static unsigned stats[4];
-		static unsigned long last_time;
-		++stats[usb_pipetype (urb->pipe)];
-		if (jiffies - last_time > HZ) {
-			int i;
-			printk ("%s: stats", __FUNCTION__);
-			for (i = 0; i < 4; ++i) {
-				printk (" %u", stats[i]);
-				stats[i] = 0;
-			}
-			printk (" (nak %lu dsptch %lu qlen %lu qint %lu)\n", nakcnt, dsptchcnt, qlen, qint);
-			last_time = jiffies;
+/*printk ("%s(urb=%p,type=%d,epnum=%d,len=%zu,interval=%d)\n", __func__, urb, usb_pipetype (urb->pipe), usb_pipeendpoint (urb->pipe), urb->transfer_buffer_length, urb->interval); */
+
+	switch (usb_pipetype(urb->pipe)) {
+	case PIPE_INTERRUPT:
+	case PIPE_ISOCHRONOUS:
+		if (interval < 0) {
+			dev_err(&spi->dev,
+				"%s: interval for interrupt-/iso-pipes "
+				"expected to be > 0, not %d\n",
+				__func__, urb->interval);
+			return -EINVAL;
 		}
+	default:
+		break;
 	}
 
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
 
-	if (!urb->ep->hcpriv) {
-		// gets freed in max3421_endpoint_disable:
-		struct max3421_ep *max3421_ep = kzalloc (sizeof (struct max3421_ep), mem_flags);
+	max3421_ep = urb->ep->hcpriv;
+	if (!max3421_ep) {
+		/* gets freed in max3421_endpoint_disable: */
+		max3421_ep = kzalloc(sizeof(struct max3421_ep), mem_flags);
 		if (!max3421_ep)
 			return -ENOMEM;
+		max3421_ep->ep = urb->ep;
+		max3421_ep->last_active = max3421_hcd->frame_number;
 		urb->ep->hcpriv = max3421_ep;
-	}
-
-	urbp = kzalloc (sizeof (*urbp), mem_flags);
-	if (!urbp)
-		return -ENOMEM;
-	urbp->urb = urb;
 
-	spin_lock (&max3421_hcd->lock);
+		list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list);
+	}
 
-	urb->hcpriv = urbp;
+	++max3421_hcd->type_stat[usb_pipetype(urb->pipe)];
 
-	if ((retval = usb_hcd_link_urb_to_ep (hcd, urb)) != 0) {
-		kfree (urbp);
-		goto done;
+	retval = usb_hcd_link_urb_to_ep(hcd, urb);
+	if (retval == 0) {
+		/* Since we added to the queue, restart scheduling: */
+		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
+		wake_up_process(max3421_hcd->spi_thread);
 	}
 
-	list_add_tail (&urbp->urbp_list, &max3421_hcd->urbp_list);
-
-done:
-	spin_unlock (&max3421_hcd->lock);
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 	return retval;
 }
 
 static int
-max3421_urb_dequeue (struct usb_hcd *hcd, struct urb *urb, int status)
+max3421_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 {
-	struct max3421_hcd *max3421_hcd;
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	unsigned long flags;
 	int retval;
 
-	printk ("%s(status=%d)\n", __FUNCTION__, status);
-	max3421_hcd = hcd_to_max3421 (hcd);
+	printk("%s(urb=%p, status=%d)\n", __func__, urb, status);
+
+	if (status == -104)
+		dump_stack();
 
-	spin_lock (&max3421_hcd->lock);
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
 
 	/*
-	 * This will set urb->unlinked which in turns causes the entry to be dropped
-	 * in max3421_irq_thread:
+	 * This will set urb->unlinked which in turn causes the entry
+	 * to be dropped at the next opportunity.
 	 */
-	retval = usb_hcd_check_unlink_urb (hcd, urb, status);
+	retval = usb_hcd_check_unlink_urb(hcd, urb, status);
+	wake_up_process(max3421_hcd->spi_thread);
 
-	spin_unlock (&max3421_hcd->lock);
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 	return retval;
 }
 
 static void
-max3421_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
+max3421_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
 {
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	unsigned long flags;
+
+	printk("%s()\n", __func__);
+
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
+
 	if (ep->hcpriv) {
-		kfree (ep->hcpriv);
+		struct max3421_ep *max3421_ep = ep->hcpriv;
+
+		/* remove myself from the ep_list: */
+		if (!list_empty(&max3421_ep->ep_list))
+			list_del(&max3421_ep->ep_list);
+		kfree(max3421_ep);
 		ep->hcpriv = NULL;
 	}
+
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
+
+	printk("%s: done!\n", __func__);
 }
 
 static int
-max3421_get_frame_number (struct usb_hcd *hcd)
+max3421_get_frame_number(struct usb_hcd *hcd)
 {
-	printk ("%s()\n", __FUNCTION__);
-	return -1;
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	return max3421_hcd->frame_number;
 }
 
 /*
@@ -1113,47 +1555,61 @@ max3421_get_frame_number (struct usb_hcd *hcd)
  * root hub is suspended.
  */
 static int
-max3421_hub_status_data (struct usb_hcd *hcd, char *buf)
+max3421_hub_status_data(struct usb_hcd *hcd, char *buf)
 {
-	struct max3421_hcd *max3421_hcd;
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	unsigned long flags;
 	int retval = 0;
 
-	max3421_hcd = hcd_to_max3421 (hcd);
-
-	spin_lock (&max3421_hcd->lock);
-	if (!HCD_HW_ACCESSIBLE (hcd))
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
+	if (!HCD_HW_ACCESSIBLE(hcd))
 		goto done;
 
 	*buf = 0;
 	if ((max3421_hcd->port_status & PORT_C_MASK) != 0) {
-		*buf = (1 << 1);	// a hub over-current condition exists
-		dev_dbg (hcd->self.controller, "port status 0x%08x has changes\n",
-			 max3421_hcd->port_status);
+		*buf = (1 << 1); /* a hub over-current condition exists */
+		dev_dbg(hcd->self.controller,
+			"port status 0x%08x has changes\n",
+			max3421_hcd->port_status);
 		retval = 1;
 		if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED)
-			usb_hcd_resume_root_hub (hcd);
+			usb_hcd_resume_root_hub(hcd);
 	}
 done:
-	printk ("%s(ret=%d,port_status_data=0x%x)\n", __FUNCTION__, retval, ((u8*)buf)[0]);
-	spin_unlock (&max3421_hcd->lock);
+	printk("%s(ret=%d,port_status_data=0x%x)\n", __func__, retval, ((u8 *)buf)[0]);
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 	return retval;
 }
 
+static inline void
+hub_descriptor(struct usb_hub_descriptor *desc)
+{
+	memset(desc, 0, sizeof(*desc));
+	/*
+	 * See Table 11-13: Hub Descriptor in USB 2.0 spec.
+	 */
+	desc->bDescriptorType = 0x29;	/* hub descriptor */
+	desc->bDescLength = 9;
+	desc->wHubCharacteristics = cpu_to_le16(0x0001);
+	desc->bNbrPorts = 1;
+}
+
 static int
-max3421_hub_control (struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
-		     char *buf, u16 length)
+max3421_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
+		    char *buf, u16 length)
 {
-	struct max3421_hcd *max3421_hcd = hcd_to_max3421 (hcd);
+	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
+	unsigned long flags;
 	int retval = 0;
 
-	spin_lock (&max3421_hcd->lock);
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
 
 	switch (type_req) {
 	case ClearHubFeature:
-		printk ("%s: ClearHubFeature\n", __FUNCTION__);
+		printk("%s: ClearHubFeature\n", __func__);
 		break;
 	case ClearPortFeature:
-		printk ("%s: ClearPortFeature(%d)\n", __FUNCTION__, value);
+		printk("%s: ClearPortFeature(%d)\n", __func__, value);
 		switch (value) {
 		case USB_PORT_FEAT_SUSPEND:
 			if (max3421_hcd->port_status & USB_PORT_STAT_SUSPEND) {
@@ -1163,24 +1619,24 @@ max3421_hub_control (struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
 			break;
 		case USB_PORT_FEAT_POWER:
 			if (max3421_hcd->port_status & USB_SS_PORT_STAT_POWER)
-				dev_dbg (hcd->self.controller, "power-off\n");
+				dev_dbg(hcd->self.controller, "power-off\n");
 			/* FALLS THROUGH */
 		default:
 			max3421_hcd->port_status &= ~(1 << value);
 		}
 		break;
 	case GetHubDescriptor:
-		hub_descriptor ((struct usb_hub_descriptor *) buf);
+		hub_descriptor((struct usb_hub_descriptor *) buf);
 		break;
 
 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
 	case GetPortErrorCount:
 	case SetHubDepth:
-		// USB3 only
+		/* USB3 only */
 		goto error;
 
 	case GetHubStatus:
-		*(__le32 *) buf = cpu_to_le32 (0);
+		*(__le32 *) buf = cpu_to_le32(0);
 		break;
 
 	case GetPortStatus:
@@ -1188,9 +1644,10 @@ max3421_hub_control (struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
 			retval = -EPIPE;
 			goto error;
 		}
-		printk ("%s: GetPortStatus(%x) -> 0x%04x (chg 0x%04x)\n", __FUNCTION__, value, max3421_hcd->port_status & 0xffff, max3421_hcd->port_status >> 16);
+		printk("%s: GetPortStatus(%x) -> 0x%04x (chg 0x%04x)\n", __func__, value, max3421_hcd->port_status & 0xffff, max3421_hcd->port_status >> 16);
 		((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status);
-		((__le16 *) buf)[1] = cpu_to_le16(max3421_hcd->port_status >> 16);
+		((__le16 *) buf)[1] =
+			cpu_to_le16(max3421_hcd->port_status >> 16);
 		break;
 
 	case SetHubFeature:
@@ -1198,7 +1655,7 @@ max3421_hub_control (struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
 		break;
 
 	case SetPortFeature:
-		printk ("%s: SetPortFeature(%x)\n", __FUNCTION__, value);
+		printk("%s: SetPortFeature(%x)\n", __func__, value);
 
 		switch (value) {
 		case USB_PORT_FEAT_LINK_STATE:
@@ -1208,17 +1665,19 @@ max3421_hub_control (struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
 			goto error;
 		case USB_PORT_FEAT_SUSPEND:
 			if (max3421_hcd->active)
-				max3421_hcd->port_status |= USB_PORT_STAT_SUSPEND;
+				max3421_hcd->port_status |=
+					USB_PORT_STAT_SUSPEND;
 			break;
 		case USB_PORT_FEAT_POWER:
 			max3421_hcd->port_status |= USB_PORT_STAT_POWER;
 			break;
 		case USB_PORT_FEAT_RESET:
 			/* if it's already enabled, disable */
-			max3421_reset_port (hcd);
+			max3421_reset_port(hcd);
 			/* FALLS THROUGH */
 		default:
-			if ((max3421_hcd->port_status & USB_PORT_STAT_POWER) != 0)
+			if ((max3421_hcd->port_status & USB_PORT_STAT_POWER)
+			    != 0)
 				max3421_hcd->port_status |= (1 << value);
 		}
 		break;
@@ -1232,47 +1691,47 @@ max3421_hub_control (struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
 		retval = -EPIPE;
 	}
 
-	spin_unlock (&max3421_hcd->lock);
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 
 	if ((max3421_hcd->port_status & PORT_C_MASK) != 0)
-		usb_hcd_poll_rh_status (hcd);
+		usb_hcd_poll_rh_status(hcd);
 	return retval;
 }
 
 static int
-max3421_bus_suspend (struct usb_hcd *hcd)
+max3421_bus_suspend(struct usb_hcd *hcd)
 {
-	printk ("%s()\n", __FUNCTION__);
+	printk("%s()\n", __func__);
 	return -1;
 }
 
 static int
-max3421_bus_resume (struct usb_hcd *hcd)
+max3421_bus_resume(struct usb_hcd *hcd)
 {
-	printk ("%s()\n", __FUNCTION__);
+	printk("%s()\n", __func__);
 	return -1;
 }
 
 /*
- * The SPI driver already takes care of DMA-mapping/unmapping, so no reason to do it
- * twice.
+ * The SPI driver already takes care of DMA-mapping/unmapping, so no
+ * reason to do it twice.
  */
 static int
-max3421_map_urb_for_dma (struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
+max3421_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
 {
 	return 0;
 }
 
 static void
-max3421_unmap_urb_for_dma (struct usb_hcd *hcd, struct urb *urb)
+max3421_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
 {
 }
 
 static struct hc_driver max3421_hcd_desc = {
 	.description =		"max3421",
 	.product_desc =		DRIVER_DESC,
-	.hcd_priv_size =	sizeof (struct max3421_hcd),
-	.flags =		HCD_USB2,
+	.hcd_priv_size =	sizeof(struct max3421_hcd),
+	.flags =		HCD_USB11,
 	.reset =		max3421_reset,
 	.start =		max3421_start,
 	.stop =			max3421_stop,
@@ -1289,97 +1748,93 @@ static struct hc_driver max3421_hcd_desc = {
 };
 
 static int __devinit
-max3421_probe (struct spi_device *spi)
+max3421_probe(struct spi_device *spi)
 {
 	struct max3421_hcd *max3421_hcd;
 	struct usb_hcd *hcd;
-	u8 rev, status;
 	int retval;
 
-	hcd = usb_create_hcd (&max3421_hcd_desc, &spi->dev, dev_name (&spi->dev));
-	if (!hcd) {
-		printk (KERN_ERR "%s: failed to create HCD structure\n",
-			__FUNCTION__);
-		return -ENOMEM;
-	}
-
-	if (spi_setup (spi) < 0) {
-		dev_err (&spi->dev, "Unable to setup SPI bus");
+	if (spi_setup(spi) < 0) {
+		dev_err(&spi->dev, "Unable to setup SPI bus");
 		return -EFAULT;
 	}
 
-	// set full-duplex SPI mode, low-active interrupt pin:
-	spi_wr8 (spi, MAX3421_REG_PINCTL,
-		 (mask (MAX3421_PINCTL_FDUPSPI_BIT) |	// full-duplex
-		  mask (MAX3421_PINCTL_INTLEVEL_BIT)),	// low-level active irq
-		 NULL);
-
-	rev = spi_rd8 (spi, MAX3421_REG_REVISION, &status);
-	if (rev != 0x12 && rev != 0x13) {
-		dev_err (&spi->dev, "bad rev 0x%x (status 0x%x)", rev, status);
-		return -ENODEV;
+	hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev,
+			     dev_name(&spi->dev));
+	if (!hcd) {
+		dev_err(&spi->dev, "failed to create HCD structure\n");
+		return -ENOMEM;
 	}
-
-	printk (KERN_INFO "%s: chip rev 0x%x, SPI clock %dHz, bpw %u, irq %d\n",
-		__FUNCTION__, rev, spi->max_speed_hz, spi->bits_per_word, spi->irq);
-
-	max3421_hcd = hcd_to_max3421 (hcd);
+	max3421_hcd = hcd_to_max3421(hcd);
 	max3421_hcd->next = max3421_hcd_list;
 	max3421_hcd_list = max3421_hcd;
 
-	max3421_hcd->chip_rev = rev;
+	max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd,
+					      "max3421_spi_thread");
+	if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) {
+		dev_err(&spi->dev,
+			"failed to create SPI thread (out of memory)\n");
+		return -ENOMEM;
+	}
 
-	retval = usb_add_hcd (hcd, 0, 0);
+	retval = usb_add_hcd(hcd, 0, 0);
 	if (retval) {
-		printk (KERN_ERR "%s: failed to add HCD\n", __FUNCTION__);
-		usb_put_hcd (hcd);
+		dev_err(&spi->dev, "failed to add HCD\n");
+		usb_put_hcd(hcd);
 		return retval;
 	}
 
-	retval = request_threaded_irq (spi->irq, NULL, max3421_irq_thread,
-				       IRQF_TRIGGER_LOW | IRQF_ONESHOT, "max3421", hcd);
+	retval = request_irq(spi->irq, max3421_irq_handler,
+			     IRQF_TRIGGER_LOW, "max3421", hcd);
 	if (retval < 0) {
-		usb_put_hcd (hcd);
-		dev_err (&spi->dev, "failed to request irq %d", spi->irq);
+		usb_put_hcd(hcd);
+		dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
 		return retval;
 	}
 	return 0;
 }
 
 static int __devexit
-max3421_remove (struct spi_device *spi)
+max3421_remove(struct spi_device *spi)
 {
 	struct max3421_hcd *max3421_hcd = NULL, **prev;
 	struct usb_hcd *hcd = NULL;
+	unsigned long flags;
 
-	printk ("%s()\n", __FUNCTION__);
+	printk("%s()\n", __func__);
 
 	for (prev = &max3421_hcd_list; *prev; prev = &(*prev)->next) {
 		max3421_hcd = *prev;
-		hcd = max3421_to_hcd (max3421_hcd);
+		hcd = max3421_to_hcd(max3421_hcd);
 		if (hcd->self.controller == &spi->dev)
 			break;
 	}
 	if (!max3421_hcd) {
-		printk (KERN_ERR "%s: no MAX3421 HCD found for SPI device %p\n",
-			__FUNCTION__, spi);
+		dev_err(&spi->dev, "no MAX3421 HCD found for SPI device %p\n",
+			spi);
 		return -ENODEV;
 	}
 
-	spin_lock (&max3421_hcd->lock);
+	spin_lock_irqsave(&max3421_hcd->lock, flags);
 
+	kthread_stop(max3421_hcd->spi_thread);
 	*prev = max3421_hcd->next;
 
-	spin_unlock (&max3421_hcd->lock);
+	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
+
+	free_irq(spi->irq, hcd);
 
-	usb_remove_hcd (hcd);
-	usb_put_hcd (hcd);
+	printk("calling usb_remove_hcd()\n");
+	usb_remove_hcd(hcd);
+	printk("calling usb_put_hcd()\n");
+	usb_put_hcd(hcd);
+	printk("back from usb_put_hcd()\n");
 	return 0;
 }
 
 static struct spi_driver max3421_driver = {
 	.probe		= max3421_probe,
-	.remove		= __devexit_p (max3421_remove),
+	.remove		= __devexit_p(max3421_remove),
 	.driver		= {
 		.name	= "max3421",
 		.owner	= THIS_MODULE,
@@ -1387,20 +1842,20 @@ static struct spi_driver max3421_driver = {
 };
 
 static int __init
-max3421_mod_init (void)
+max3421_mod_init(void)
 {
-	return spi_register_driver (&max3421_driver);
+	return spi_register_driver(&max3421_driver);
 }
 
 static void __exit
-max3421_mod_exit (void)
+max3421_mod_exit(void)
 {
-	spi_unregister_driver (&max3421_driver);
+	spi_unregister_driver(&max3421_driver);
 }
 
 module_init(max3421_mod_init);
 module_exit(max3421_mod_exit);
 
-MODULE_DESCRIPTION (DRIVER_DESC);
-MODULE_AUTHOR ("eGauge Systems LLC");
-MODULE_LICENSE ("GPL");
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_AUTHOR("eGauge Systems LLC");
+MODULE_LICENSE("GPL");

[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux