--- linux/drivers/usb/usb-ohci.micro	Mon Jun 26 14:51:37 2000
+++ linux/drivers/usb/usb-ohci.c	Mon Jun 26 17:58:18 2000
@@ -11,6 +11,9 @@
  * 
  * History:
  * 
+ *	2000/06/28 use PCI hotplug framework, for better power management
+ *	and for Cardbus support (David Brownell)
+ *
  * v5.2 1999/12/07 URB 3rd preview, 
  * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi)
  * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume 
@@ -24,8 +27,6 @@
  * v2.1 1999/05/09  code clean up
  * v2.0 1999/05/04 
  * v1.0 1999/04/27 initial release
- * 
- 
  */
  
 #include <linux/config.h>
@@ -38,6 +39,7 @@
 #include <linux/malloc.h>
 #include <linux/smp_lock.h>
 #include <linux/errno.h>
+#include <linux/init.h>
 #include <linux/timer.h>
 #include <linux/list.h>
 #include <linux/interrupt.h>  /* for in_interrupt() */
@@ -50,11 +52,10 @@
 #include <asm/unaligned.h>
 
 #define OHCI_USE_NPS		// force NoPowerSwitching mode
+// #define OHCI_VERBOSE_DEBUG	/* not always helpful */
 
 #include "usb-ohci.h"
 
-#include <linux/pm.h>
-static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data);
 
 #ifdef CONFIG_PMAC_PBOOK
 #include <linux/adb.h>
@@ -105,7 +106,6 @@
 static void urb_print (urb_t * urb, char * str, int small)
 {
 	unsigned int pipe= urb->pipe;
-	int i, len;
 	
 	if (!urb->dev || !urb->dev->bus) {
 		dbg("%s URB: no dev", str);
@@ -124,7 +124,10 @@
 		 	urb->actual_length, 
 		 	urb->transfer_buffer_length,
 		 	urb->status, urb->status);
+#ifdef	OHCI_VERBOSE_DEBUG
 	if (!small) {
+		int i, len;
+
 		if (usb_pipecontrol (pipe)) {
 			printk (KERN_DEBUG __FILE__ ": cmd(8):");
 			for (i = 0; i < 8 ; i++) 
@@ -142,6 +145,7 @@
 			printk ("%s stat:%d\n", i < len? "...": "", urb->status);
 		}
 	} 
+#endif
 }
 
 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
@@ -593,6 +597,7 @@
 
 /*-------------------------------------------------------------------------*/
 
+/* may be called from interrupt context */
 /* free private data space of usb device */
   
 static int sohci_free_dev (struct usb_device * usb_dev)
@@ -604,7 +609,8 @@
 	struct ohci_device * dev = usb_to_ohci (usb_dev);
 	ohci_t * ohci = usb_dev->bus->hcpriv;
 	
-	if (!dev) return 0;
+	if (!dev)
+		return 0;
 	
 	if (usb_dev->devnum >= 0) {
 	
@@ -613,7 +619,8 @@
 		for(i = 0; i < NUM_EDS; i++) {
   			ed = &(dev->ed[i]);
   			if (ed->state != ED_NEW) {
-  				if (ed->state == ED_OPER) ep_unlink (ohci, ed);
+  				if (ed->state == ED_OPER)
+					ep_unlink (ohci, ed);
   				ep_rm_ed (usb_dev, ed);
   				ed->state = ED_DEL;
   				cnt++;
@@ -622,10 +629,29 @@
   		spin_unlock_irqrestore (&usb_ed_lock, flags);
   		
 		if (cnt > 0) {
-			add_wait_queue (&op_wakeup, &wait);
-			current->state = TASK_UNINTERRUPTIBLE;
-			schedule_timeout (HZ / 10);
-			remove_wait_queue (&op_wakeup, &wait);
+
+			if (ohci->disabled) {
+				/* FIXME: Something like this can kick in,
+				 * but I'm not sure exactly what yet ...
+				 * the controller sure won't be touching
+				 * these lists again!!
+				 *
+				dl_del_list (ohci,
+					le16_to_cpu (ohci->hcca.frame_no) & 1);
+				 */
+
+
+			} else if (!in_interrupt ()) {
+				/* SF interrupt handler calls dl_del_list */
+				add_wait_queue (&op_wakeup, &wait);
+				current->state = TASK_UNINTERRUPTIBLE;
+				schedule_timeout (HZ / 10);
+				remove_wait_queue (&op_wakeup, &wait);
+
+			} else {
+				/* can we fix this? */
+				err ("can't free tds, interrupt context");
+			}
 		}
 	}
 	kfree (dev);
@@ -942,24 +970,31 @@
  
 /* request the removal of an endpoint
  * put the ep on the rm_list and request a stop of the bulk or ctrl list 
- * real removal is done at the next start of frame (SOF) hardware interrupt */
+ * real removal is done at the next start frame (SF) hardware interrupt */
  
 static void ep_rm_ed (struct usb_device * usb_dev, ed_t * ed)
 {    
 	unsigned int frame;
 	ohci_t * ohci = usb_dev->bus->hcpriv;
 
-	if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) return;
+	if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL))
+		return;
 	
 	ed->hwINFO  |=  cpu_to_le32 (OHCI_ED_SKIP);
-	
-	writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
-	writel (OHCI_INTR_SF, &ohci->regs->intrenable); /* enable sof interrupt */
+
+	if (!ohci->disabled) {
+		/* enable SOF interrupt */
+		writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
+		writel (OHCI_INTR_SF, &ohci->regs->intrenable);
+	}
 
 	frame = le16_to_cpu (ohci->hcca.frame_no) & 0x1;
 	ed->ed_rm_list = ohci->ed_rm_list[frame];
 	ohci->ed_rm_list[frame] = ed;
 
+	if (ohci->disabled)
+		return;
+
 	switch (ed->type) {
 		case CTRL: /* stop CTRL list */
 			writel (ohci->hc_control &= ~OHCI_CTRL_CLE, &ohci->regs->control); 
@@ -1202,18 +1237,21 @@
 		for (td = tdHeadP; td != tdTailP; td = td_next) { 
 			urb_t * urb = td->urb;
 			urb_priv_t * urb_priv = td->urb->hcpriv;
-			
+
 			td_next = bus_to_virt (le32_to_cpup (&td->hwNextTD) & 0xfffffff0);
 			if ((urb_priv->state == URB_DEL) || (ed->state & ED_DEL)) {
 				tdINFO = le32_to_cpup (&td->hwINFO);
-				if (TD_CC_GET (tdINFO) < 0xE) dl_transfer_length (td);
+				if (TD_CC_GET (tdINFO) < 0xE)
+					dl_transfer_length (td);
 				*td_p = td->hwNextTD | (*td_p & cpu_to_le32 (0x3));
 				if(++ (urb_priv->td_cnt) == urb_priv->length) 
 					urb_rm_priv (urb);
-					if (urb->transfer_flags & USB_ASYNC_UNLINK) {
+					if (urb->transfer_flags & USB_ASYNC_UNLINK
+							|| ohci->disabled) {
 						usb_dec_dev_use (urb->dev);
 						urb->status = -ECONNRESET;
-						urb->complete (urb); 
+						if (urb->complete)
+							urb->complete (urb); 
 					} else {	
 						wake_up (&op_wakeup);
 					}
@@ -1240,11 +1278,16 @@
   		if ((ed->type & 3) == BULK) bulk |= 1;  
    	}
    	
-   	if (ctrl) writel (0, &ohci->regs->ed_controlcurrent); /* reset CTRL list */
-   	if (bulk) writel (0, &ohci->regs->ed_bulkcurrent);    /* reset BULK list */
-	if (!ohci->ed_rm_list[!frame]) { 		/* enable CTRL and BULK lists */ 
-  		ohci->hc_control |= OHCI_CTRL_CLE | OHCI_CTRL_BLE;
-  		writel (ohci->hc_control, &ohci->regs->control);   
+	/* maybe reenable CTRL and BULK lists */ 
+	if (!ohci->disabled) {
+		if (ctrl) 	/* reset CTRL list */
+			writel (0, &ohci->regs->ed_controlcurrent);
+		if (bulk)	/* reset BULK list */
+			writel (0, &ohci->regs->ed_bulkcurrent);
+		if (!ohci->ed_rm_list[!frame]) {
+			ohci->hc_control |= OHCI_CTRL_CLE | OHCI_CTRL_BLE;
+			writel (ohci->hc_control, &ohci->regs->control);   
+		}
 	}
    	ohci->ed_rm_list[frame] = NULL;
 
@@ -1847,28 +1890,9 @@
 
 /*-------------------------------------------------------------------------*/
 
-/* reinitialize after controller reset */
-
-static void hc_reinit_ohci (ohci_t *ohci)
-{
-	int i;
-
-	/* for load balancing of the interrupt branches */
-	for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
-	for (i = 0; i < NUM_INTS; i++) ohci->hcca.int_table[i] = 0;
-	
-	ohci->ed_rm_list [0] = NULL;
-	ohci->ed_rm_list [1] = NULL;
-
-	/* end of control and bulk lists */	 
-	ohci->ed_isotail     = NULL;
-	ohci->ed_controltail = NULL;
-	ohci->ed_bulktail    = NULL;
-} 
-
 /* allocate OHCI */
 
-static ohci_t * hc_alloc_ohci (void * mem_base)
+static ohci_t * __devinit hc_alloc_ohci (void * mem_base)
 {
 	ohci_t * ohci;
 	struct usb_bus * bus;
@@ -1918,6 +1942,9 @@
 
 	usb_deregister_bus (ohci->bus);
 	usb_free_bus (ohci->bus);
+
+	list_del (&ohci->ohci_hcd_list);
+	INIT_LIST_HEAD (&ohci->ohci_hcd_list);
     
 	/* unmap the IO address space */
 	iounmap (ohci->regs);
@@ -1930,7 +1957,8 @@
 /* Increment the module usage count, start the control thread and
  * return success. */
  
-static int hc_found_ohci (struct pci_dev *dev, int irq, void * mem_base)
+static int __devinit
+hc_found_ohci (struct pci_dev *dev, int irq, void * mem_base)
 {
 	ohci_t * ohci;
 	char buf[8], *bufp = buf;
@@ -1959,22 +1987,15 @@
 		return -ENODEV;
 	}
 
+	/* FIXME this is a second reset; why?? */
 	writel (ohci->hc_control = OHCI_USB_RESET, &ohci->regs->control);
 	wait_ms (10);
+
 	usb_register_bus (ohci->bus);
 	
 	if (request_irq (irq, hc_interrupt, SA_SHIRQ, "usb-ohci", ohci) == 0) {
-		struct pm_dev *pmdev;
-
 		ohci->irq = irq;     
 		hc_start (ohci);
-
-		pmdev = pm_register (PM_PCI_DEV,
-				     PM_PCI_ID(dev),
-				     handle_pm_event);
-		if (pmdev)
-			pmdev->data = ohci;
-
 #ifdef	DEBUG
 		ohci_dump (ohci, 1);
 #endif
@@ -1986,31 +2007,221 @@
 }
 
 /*-------------------------------------------------------------------------*/
- 
-static int hc_start_ohci (struct pci_dev * dev)
+
+/* controller died; cleanup debris, then restart */
+/* must not be called from interrupt context */
+
+static void hc_restart (ohci_t *ohci)
+{
+	int temp;
+	int i;
+
+	ohci->disabled = 1;
+	if (ohci->bus->root_hub)
+		usb_disconnect (&ohci->bus->root_hub);
+	
+	/* empty the interrupt branches */
+	for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
+	for (i = 0; i < NUM_INTS; i++) ohci->hcca.int_table[i] = 0;
+	
+	/* no EDs to remove */
+	ohci->ed_rm_list [0] = NULL;
+	ohci->ed_rm_list [1] = NULL;
+
+	/* empty control and bulk lists */	 
+	ohci->ed_isotail     = NULL;
+	ohci->ed_controltail = NULL;
+	ohci->ed_bulktail    = NULL;
+
+	if ((temp = hc_reset (ohci)) < 0 || (temp = hc_start (ohci)) < 0) {
+		ohci->disabled = 1;
+		err ("can't restart %s, %d", ohci->ohci_dev->slot_name, temp);
+	} else
+		dbg ("restart %s completed", ohci->ohci_dev->slot_name);
+}
+
+/*-------------------------------------------------------------------------*/
+
+/* configured so that an OHCI device is always provided */
+/* always called with process context; sleeping is OK */
+
+static int __devinit
+ohci_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
 {
-	unsigned long mem_base;
+	unsigned long mem_resource;
+	void *mem_base;
 
 	if (pci_enable_device(dev) < 0)
 		return -ENODEV;
 	
 	pci_set_master (dev);
-	mem_base = pci_resource_start(dev, 0);
-	mem_base = (unsigned long) ioremap_nocache (mem_base, 4096);
+	mem_resource = pci_resource_start(dev, 0);
+	/* request_mem_region ... */
+	mem_base = ioremap_nocache (mem_resource, 4096);
 
 	if (!mem_base) {
 		err("Error mapping OHCI memory");
 		return -EFAULT;
 	}
-	return hc_found_ohci (dev, dev->irq, (void *) mem_base);
+	return hc_found_ohci (dev, dev->irq, mem_base);
 } 
 
 /*-------------------------------------------------------------------------*/
 
+/* may be called from interrupt context [interface spec] */
+/* may be called without controller present */
+/* may be called with controller, bus, and devices active */
+
+static void __devexit
+ohci_pci_remove (struct pci_dev *dev)
+{
+	ohci_t		*ohci = (ohci_t *) dev->driver_data;
+
+	dbg ("remove%s controller %s%s, state %s",
+		ohci->disabled ? " disabled" : "",
+		dev->slot_name,
+		in_interrupt () ? " in interrupt" : "",
+		hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS)
+		);
+
+	/* don't wake up sleeping controllers, or block in interrupt context */
+	if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER || in_interrupt ()) {
+		dbg ("controller being disabled");
+		ohci->disabled = 1;
+	}
+
+	/* on return, USB will always be reset (if present) */
+	if (ohci->disabled)
+		writel (ohci->hc_control = OHCI_USB_RESET,
+			&ohci->regs->control);
+
+	hc_release_ohci (ohci);
+}
+
+/*-------------------------------------------------------------------------*/
+
+#ifdef	CONFIG_PM
+
+static void
+ohci_pci_suspend (struct pci_dev *dev)
+{
+	ohci_t		*ohci = (ohci_t *) dev->driver_data;
+
+	if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER) {
+		dbg ("can't suspend %s (state is %s)", dev->slot_name,
+			hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS));
+		return;
+	}
+
+	/* act as if usb suspend can always be used */
+	info ("USB suspend: %s", dev->slot_name);
+#ifdef CONFIG_PMAC_PBOOK
+	disable_irq (ohci->irq);
+		/* else, assume shared irqs -- don't disable */
+#endif
+	ohci->hc_control = OHCI_USB_SUSPEND;
+	writel (ohci->hc_control, &ohci->regs->control);
+	wait_ms (10);
+}
+
+/*-------------------------------------------------------------------------*/
+
+static void
+ohci_pci_resume (struct pci_dev *dev)
+{
+	ohci_t		*ohci = (ohci_t *) dev->driver_data;
+	int		temp;
+
+	/* did we suspend, or were we powered off? */
+	ohci->hc_control = readl (&ohci->regs->control);
+	temp = ohci->hc_control & OHCI_CTRL_HCFS;
+
+#ifdef DEBUG
+	/* the registers may look crazy here */
+	ohci_dump_status (ohci);
+#endif
+
+	switch (temp) {
+
+	case OHCI_USB_RESET:	// lost power
+		info ("USB restart: %s", dev->slot_name);
+		hc_restart (ohci);
+		break;
+
+	case OHCI_USB_SUSPEND:	// host wakeup
+	case OHCI_USB_RESUME:	// remote wakeup
+		info ("USB continue: %s from %s wakeup", dev->slot_name,
+			(temp == OHCI_USB_SUSPEND)
+				? "host" : "remote");
+		ohci->hc_control = OHCI_USB_RESUME;
+		writel (ohci->hc_control, &ohci->regs->control);
+		wait_ms (20);
+
+		temp = readl (&ohci->regs->control);
+		temp = ohci->hc_control & OHCI_CTRL_HCFS;
+		if (temp != OHCI_USB_RESUME) {
+			err ("controller %s won't resume", dev->slot_name);
+			ohci->disabled = 1;
+			return;
+		}
+
+		ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
+		writel (ohci->hc_control, &ohci->regs->control);
 #ifdef CONFIG_PMAC_PBOOK
+		enable_irq (ohci->irq);
+#endif
+		break;
+
+	default:
+		warn ("odd PCI resume for %s", dev->slot_name);
+	}
+}
+#endif	/* CONFIG_PM */
+
+
+/*-------------------------------------------------------------------------*/
+
+#define PCI_CLASS_SERIAL_USB_OHCI 0x0C0310
+
+static const struct __devinitdata pci_device_id	ohci_pci_ids [] = { {
+
+	/* handle any USB OHCI controller */
+	class: 		PCI_CLASS_SERIAL_USB_OHCI,
+	class_mask: 	~0,
+
+	/* no matter who makes it */
+	vendor:		PCI_ANY_ID,
+	device:		PCI_ANY_ID,
+	subvendor:	PCI_ANY_ID,
+	subdevice:	PCI_ANY_ID,
+
+	}, { /* end: all zeroes */ }
+};
+
+MODULE_DEVICE_TABLE (pci, ohci_pci_ids);
+
+static struct pci_driver ohci_pci_driver = {
+	name:		"usb-ohci",
+	id_table:	&ohci_pci_ids [0],
 
-/* On Powerbooks, put the controller into suspend mode when going
- * to sleep, and do a resume when waking up. */
+	probe:		ohci_pci_probe,
+	remove:		ohci_pci_remove,
+
+#ifdef	CONFIG_PMAC_PBOOK
+	/* apparently, pbook PCI thinks different ... */
+
+#else
+#ifdef	CONFIG_PM
+	suspend:	ohci_pci_suspend,
+	resume:		ohci_pci_resume,
+#endif	/* PM */
+#endif	/* PBOOK */
+};
+
+ 
+/*-------------------------------------------------------------------------*/
+
+#ifdef CONFIG_PMAC_PBOOK
 
 static int ohci_sleep_notify (struct pmu_sleep_notifier * self, int when)
 {
@@ -2022,16 +2233,10 @@
 
 		switch (when) {
 		case PBOOK_SLEEP_NOW:
-			disable_irq (ohci->irq);
-			writel (ohci->hc_control = OHCI_USB_SUSPEND, &ohci->regs->control);
-			wait_ms (10);
+			ohci_pci_suspend (ohci->ohci_dev);
 			break;
 		case PBOOK_WAKE:
- 			writel (ohci->hc_control = OHCI_USB_RESUME, &ohci->regs->control);
-			wait_ms (20);
-			ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
-			writel (ohci->hc_control, &ohci->regs->control);
-			enable_irq (ohci->irq);
+			ohci_pci_resume (ohci->ohci_dev);
 			break;
 		}
 	}
@@ -2045,108 +2250,32 @@
 
 /*-------------------------------------------------------------------------*/
 
-static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
+int __init ohci_hcd_init (void) 
 {
-	ohci_t * ohci = (ohci_t*) dev->data;
-	int temp = 0;
-
-	if (ohci) {
-		switch (rqst) {
-		case PM_SUSPEND:
-			/* act as if usb suspend can always be used */
-			dbg("USB suspend: %s", ohci->ohci_dev->slot_name);
-			ohci->hc_control = OHCI_USB_SUSPEND;
-			writel (ohci->hc_control, &ohci->regs->control);
-			wait_ms (10);
-			break;
-
-		case PM_RESUME:
-			/* did we suspend, or were we powered off? */
-			ohci->hc_control = readl (&ohci->regs->control);
-			temp = ohci->hc_control & OHCI_CTRL_HCFS;
-			switch (temp) {
-
-			case OHCI_USB_RESET:	// lost power
-				dbg("USB reset: %s", ohci->ohci_dev->slot_name);
-				ohci->disabled = 1;
-				if (ohci->bus->root_hub)
-					usb_disconnect (&ohci->bus->root_hub);
-				hc_reinit_ohci (ohci);
-				if ((temp = hc_reset (ohci)) < 0
-					|| (temp = hc_start (ohci)) < 0) {
-					ohci->disabled = 1;
-					err ("can't restart %s, %d",
-						ohci->ohci_dev->slot_name,
-						temp);
-				}
-				dbg ("reset done");
-				break;
-
-			case OHCI_USB_SUSPEND:	// host wakeup
-			case OHCI_USB_RESUME:	// remote wakeup
-				dbg("USB resume: %s", ohci->ohci_dev->slot_name);
-				ohci->hc_control = OHCI_USB_RESUME;
-				writel (ohci->hc_control, &ohci->regs->control);
-				wait_ms (20);
-
-				ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
-				writel (ohci->hc_control, &ohci->regs->control);
-				break;
-
-			default:
-				warn ("odd PM_RESUME");
-			}
-			break;
-		}
-	}
-	return 0;
-}
-
-/*-------------------------------------------------------------------------*/
+	int ret = pci_module_init (&ohci_pci_driver);
 
-#define PCI_CLASS_SERIAL_USB_OHCI 0x0C0310
- 
-int ohci_hcd_init (void) 
-{
-	int ret = -ENODEV;
-	struct pci_dev * dev = NULL;
- 
-	while ((dev = pci_find_class (PCI_CLASS_SERIAL_USB_OHCI, dev))) { 
-		if (hc_start_ohci(dev) >= 0) ret = 0;
-	}
-    
 #ifdef CONFIG_PMAC_PBOOK
-	pmu_register_sleep_notifier (&ohci_sleep_notifier);
+	if (ret >= 0)
+		pmu_register_sleep_notifier (&ohci_sleep_notifier);
 #endif  
-    return ret;
+	return ret;
 }
 
 /*-------------------------------------------------------------------------*/
 
 #ifdef MODULE
-int init_module (void) 
-{
-	return ohci_hcd_init ();
-}
-
-/*-------------------------------------------------------------------------*/
 
-void cleanup_module (void) 
+void __exit ohci_hcd_cleanup (void) 
 {	
-	ohci_t * ohci;
-	
-	pm_unregister_all (handle_pm_event);
-
 #ifdef CONFIG_PMAC_PBOOK
 	pmu_unregister_sleep_notifier (&ohci_sleep_notifier);
 #endif  
-
-	while (!list_empty (&ohci_hcd_list)) {
-		ohci = list_entry (ohci_hcd_list.next, ohci_t, ohci_hcd_list);
-		list_del (&ohci->ohci_hcd_list);
-		INIT_LIST_HEAD (&ohci->ohci_hcd_list);
-		hc_release_ohci (ohci);
-	}		
+	pci_unregister_driver (&ohci_pci_driver);
 }
-#endif //MODULE
 
+module_init (ohci_hcd_init);
+module_exit (ohci_hcd_cleanup);
+
+#endif /* MODULE */
+
+MODULE_DESCRIPTION ("USB OHCI Controller Driver");

