patch-2.3.13 linux/drivers/usb/hub.c

Next file: linux/drivers/usb/ohci-hcd.c
Previous file: linux/drivers/usb/cpia.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.12/linux/drivers/usb/hub.c linux/drivers/usb/hub.c
@@ -14,23 +14,91 @@
 #include <linux/module.h>
 
 #include <asm/spinlock.h>
+#include <asm/uaccess.h>
 
 #include "usb.h"
 #include "hub.h"
 
 /* Wakes up khubd */
-static DECLARE_WAIT_QUEUE_HEAD(usb_hub_wait);
 static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
 static spinlock_t hub_list_lock = SPIN_LOCK_UNLOCKED;
 
-/* List of hubs needing servicing */
-static struct list_head hub_event_list;
+static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
+static LIST_HEAD(hub_list);		/* List containing all of the hubs (for cleanup) */
 
-/* List containing all of the hubs (for cleanup) */
-static struct list_head all_hubs_list;
+static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
+static int khubd_pid = 0;			/* PID of khubd */
+static int khubd_running = 0;
 
-/* PID of khubd */
-static int khubd_pid = 0;
+static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
+{
+	devrequest dr;
+
+	dr.requesttype = USB_DIR_IN | USB_RT_HUB;
+	dr.request = USB_REQ_GET_DESCRIPTOR;
+	dr.value = (USB_DT_HUB << 8);
+	dr.index = 0;
+	dr.length = size;
+
+	return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr,
+		data, size);
+}
+
+static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
+{
+	devrequest dr;
+
+	dr.requesttype = USB_RT_PORT;
+	dr.request = USB_REQ_CLEAR_FEATURE;
+	dr.value = feature;
+	dr.index = port;
+	dr.length = 0;
+
+	return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr,
+		NULL, 0);
+}
+
+static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
+{
+	devrequest dr;
+
+	dr.requesttype = USB_RT_PORT;
+	dr.request = USB_REQ_SET_FEATURE;
+	dr.value = feature;
+	dr.index = port;
+	dr.length = 0;
+
+	return dev->bus->op->control_msg(dev, usb_sndctrlpipe(dev,0), &dr,
+		NULL, 0);
+}
+
+static int usb_get_hub_status(struct usb_device *dev, void *data)
+{
+	devrequest dr;
+
+	dr.requesttype = USB_DIR_IN | USB_RT_HUB;
+	dr.request = USB_REQ_GET_STATUS;
+	dr.value = 0;
+	dr.index = 0;
+	dr.length = 4;
+
+	return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr,
+		data, 4);
+}
+
+static int usb_get_port_status(struct usb_device *dev, int port, void *data)
+{
+	devrequest dr;
+
+	dr.requesttype = USB_DIR_IN | USB_RT_PORT;
+	dr.request = USB_REQ_GET_STATUS;
+	dr.value = 0;
+	dr.index = port;
+	dr.length = 4;
+
+	return dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr,
+		data, 4);
+}
 
 /*
  * A irq handler returns non-zero to indicate to
@@ -42,13 +110,13 @@
 	struct usb_hub *hub = dev_id;
 	unsigned long flags;
 
-	if (waitqueue_active(&usb_hub_wait)) {
+	if (waitqueue_active(&khubd_wait)) {
 		/* Add the hub to the event queue */
 		spin_lock_irqsave(&hub_event_lock, flags);
 		if (hub->event_list.next == &hub->event_list) {
 			list_add(&hub->event_list, &hub_event_list);
 			/* Wake up khubd */
-			wake_up(&usb_hub_wait);
+			wake_up(&khubd_wait);
 		}
 		spin_unlock_irqrestore(&hub_event_lock, flags);
 	}
@@ -68,83 +136,63 @@
 		return;
 
 	hub->nports = dev->maxchild = hubdescriptor[2];
-	printk("hub: %d-port%s detected\n", hub->nports,
+	printk(KERN_INFO "hub: %d-port%s detected\n", hub->nports,
 		(hub->nports == 1) ? "" : "s");
 
 	charac = (hubdescriptor[4] << 8) + hubdescriptor[3];
 	switch (charac & HUB_CHAR_LPSM) {
 		case 0x00:
-			printk("hub: ganged power switching\n");
+			printk(KERN_INFO "hub: ganged power switching\n");
 			break;
 		case 0x01:
-			printk("hub: individual port power switching\n");
+			printk(KERN_INFO "hub: individual port power switching\n");
 			break;
 		case 0x02:
 		case 0x03:
-			printk("hub: unknown reserved power switching mode\n");
+			printk(KERN_INFO "hub: unknown reserved power switching mode\n");
 			break;
 	}
 
 	if (charac & HUB_CHAR_COMPOUND)
-		printk("hub: part of a compound device\n");
+		printk(KERN_INFO "hub: part of a compound device\n");
 	else
-		printk("hub: standalone hub\n");
+		printk(KERN_INFO "hub: standalone hub\n");
 
 	switch (charac & HUB_CHAR_OCPM) {
 		case 0x00:
-			printk("hub: global over current protection\n");
+			printk(KERN_INFO "hub: global over current protection\n");
 			break;
 		case 0x08:
-			printk("hub: individual port over current protection\n");
+			printk(KERN_INFO "hub: individual port over current protection\n");
 			break;
 		case 0x10:
 		case 0x18:
-			printk("hub: no over current protection\n");
+			printk(KERN_INFO "hub: no over current protection\n");
                         break;
 	}
 
-	printk("hub: power on to power good time: %dms\n",
+	printk(KERN_INFO "hub: power on to power good time: %dms\n",
 		hubdescriptor[5] * 2);
 
-	printk("hub: hub controller current requirement: %dmA\n",
+	printk(KERN_INFO "hub: hub controller current requirement: %dmA\n",
 		hubdescriptor[6]);
 
 	for (i = 0; i < dev->maxchild; i++)
-		printk("hub:  port %d is%s removable\n", i + 1,
+		printk(KERN_INFO "hub:  port %d is%s removable\n", i + 1,
 			hubdescriptor[7 + ((i + 1)/8)] & (1 << ((i + 1) % 8))
 			? " not" : "");
 
 	if (usb_get_hub_status(dev, buf))
 		return;
 
-	printk("hub: local power source is %s\n",
+	printk(KERN_INFO "hub: local power source is %s\n",
 		(buf[0] & 1) ? "lost (inactive)" : "good");
 
-	printk("hub: %sover current condition exists\n",
+	printk(KERN_INFO "hub: %sover current condition exists\n",
 		(buf[0] & 2) ? "" : "no ");
 
-#if 0
-	for (i = 0; i < hub->nports; i++) {
-		int portstat, portchange;
-		unsigned char portstatus[4];
-
-		if (usb_get_port_status(dev, i + 1, portstatus))
-			return;
-		portstat = (portstatus[1] << 8) + portstatus[0];
-		portchange = (portstatus[3] << 8) + portstatus[2];
-
-		printk("hub: port %d status\n", i + 1);
-		printk("hub:  %sdevice present\n", (portstat & 1) ? "" : "no ");
-		printk("hub:  %s\n", (portstat & 2) ? "enabled" : "disabled");
-		printk("hub:  %ssuspended\n", (portstat & 4) ? "" : "not ");
-		printk("hub:  %sover current\n", (portstat & 8) ? "" : "not ");
-		printk("hub:  has %spower\n", (portstat & 0x100) ? "" : "no ");
-		printk("hub:  %s speed\n", (portstat & 0x200) ? "low" : "full");
-	}
-#endif
-
 	/* Enable power to the ports */
-	printk("enabling power on all ports\n");
+	printk(KERN_INFO "hub: enabling power on all ports\n");
 	for (i = 0; i < hub->nports; i++)
 		usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
 }
@@ -180,7 +228,7 @@
 	endpoint = &interface->endpoint[0];
 
 	/* Output endpoint? Curiousier and curiousier.. */
-	if (!(endpoint->bEndpointAddress & 0x80))
+	if (!(endpoint->bEndpointAddress & USB_DIR_IN))
 		return -1;
 
 	/* If it's not an interrupt endpoint, we'd better punt! */
@@ -188,10 +236,10 @@
 		return -1;
 
 	/* We found a hub */
-	printk("USB hub found\n");
+	printk(KERN_INFO "USB hub found\n");
 
 	if ((hub = kmalloc(sizeof(*hub), GFP_KERNEL)) == NULL) {
-		printk("couldn't kmalloc hub struct\n");
+		printk(KERN_ERR "couldn't kmalloc hub struct\n");
 		return -1;
 	}
 
@@ -205,15 +253,16 @@
 	/* Record the new hub's existence */
 	spin_lock_irqsave(&hub_list_lock, flags);
 	INIT_LIST_HEAD(&hub->hub_list);
-	list_add(&hub->hub_list, &all_hubs_list);
+	list_add(&hub->hub_list, &hub_list);
 	spin_unlock_irqrestore(&hub_list_lock, flags);
 
 	usb_hub_configure(hub);
 
-	hub->irq_handle = usb_request_irq(dev, usb_rcvctrlpipe(dev, endpoint->bEndpointAddress), hub_irq, endpoint->bInterval, hub);
+	hub->irq_handle = usb_request_irq(dev, usb_rcvctrlpipe(dev,
+		endpoint->bEndpointAddress), hub_irq, endpoint->bInterval, hub);
 
 	/* Wake up khubd */
-	wake_up(&usb_hub_wait);
+	wake_up(&khubd_wait);
 
 	return 0;
 }
@@ -246,40 +295,51 @@
 	unsigned char buf[4];
 	unsigned short portstatus, portchange;
 
+	/* Disconnect anything that may have been there */
 	usb_disconnect(&hub->children[port]);
 
+	/* Reset the port */
 	usb_set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
 
 	wait_ms(50);	/* FIXME: This is from the *BSD stack, thanks! :) */
 
+	/* Check status */
 	if (usb_get_port_status(hub, port + 1, buf)) {
-		printk("get_port_status failed\n");
+		printk(KERN_ERR "get_port_status failed\n");
 		return;
 	}
 
 	portstatus = le16_to_cpup((unsigned short *)buf + 0);
 	portchange = le16_to_cpup((unsigned short *)buf + 1);
 
+	/* If it's not in CONNECT and ENABLE state, we're done */
 	if ((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
-		(!(portstatus & USB_PORT_STAT_ENABLE))) {
+	    (!(portstatus & USB_PORT_STAT_ENABLE)))
 		/* We're done now, we already disconnected the device */
-		/* printk("not connected/enabled\n"); */
 		return;
-	}
 
+	/* Allocate a new device struct for it */
 	usb = hub->bus->op->allocate(hub);
 	if (!usb) {
-		printk("couldn't allocate usb_device\n");
+		printk(KERN_ERR "couldn't allocate usb_device\n");
 		return;
 	}
 
-	usb_connect(usb);
-
 	usb->slow = (portstatus & USB_PORT_STAT_LOW_SPEED) ? 1 : 0;
 
 	hub->children[port] = usb;
 
-	usb_new_device(usb);
+	/* Find a new device ID for it */
+	usb_connect(usb);
+
+	/* Run it through the hoops (find a driver, etc) */
+	if (usb_new_device(usb)) {
+		/* Woops, disable the port */
+		printk(KERN_DEBUG "hub: disabling malfunctioning port %d\n",
+			port + 1);
+		usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
+		usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_POWER);
+	}
 }
 
 static void usb_hub_events(void)
@@ -306,7 +366,7 @@
 
 		for (i = 0; i < hub->nports; i++) {
 			if (usb_get_port_status(dev, i + 1, buf)) {
-				printk("get_port_status failed\n");
+				printk(KERN_ERR "get_port_status failed\n");
 				continue;
 			}
 
@@ -314,7 +374,8 @@
 			portchange = le16_to_cpup((unsigned short *)buf + 1);
 
 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
-				printk("hub: port %d connection change\n", i + 1);
+				printk(KERN_INFO "hub: port %d connection change\n",
+					i + 1);
 
 				usb_clear_port_feature(dev, i + 1,
 					USB_PORT_FEAT_C_CONNECTION);
@@ -323,46 +384,29 @@
 			}
 
 			if (portchange & USB_PORT_STAT_C_ENABLE) {
-				printk("hub: port %d enable change\n", i + 1);
+				printk(KERN_INFO "hub: port %d enable change\n",
+					i + 1);
 				usb_clear_port_feature(dev, i + 1,
 					USB_PORT_FEAT_C_ENABLE);
 			}
 
 			if (portchange & USB_PORT_STAT_C_SUSPEND)
-				printk("hub: port %d suspend change\n", i + 1);
+				printk(KERN_INFO "hub: port %d suspend change\n",
+					i + 1);
 
 			if (portchange & USB_PORT_STAT_C_OVERCURRENT)
-				printk("hub: port %d over-current change\n", i + 1);
+				printk(KERN_INFO "hub: port %d over-current change\n",
+					i + 1);
 
 			if (portchange & USB_PORT_STAT_C_RESET) {
-				printk("hub: port %d reset change\n", i + 1);
+				printk(KERN_INFO "hub: port %d reset change\n",
+					i + 1);
 				usb_clear_port_feature(dev, i + 1,
 					USB_PORT_FEAT_C_RESET);
 			}
 
-#if 0
-			if (!portchange)
-				continue;
-
-			if (usb_get_port_status(dev, i + 1, buf))
-				return;
-
-			portstatus = (buf[1] << 8) + buf[0];
-			portchange = (buf[3] << 8) + buf[2];
-
-			printk("hub: port %d status\n", i + 1);
-			printk("hub:  %sdevice present\n", (portstatus & 1) ? "" : "no ");
-			printk("hub:  %s\n", (portstatus & 2) ? "enabled" : "disabled");
-			printk("hub:  %ssuspended\n", (portstatus & 4) ? "" : "not ");
-			printk("hub:  %sover current\n", (portstatus & 8) ? "" : "not ");
-			printk("hub:  has %spower\n", (portstatus & 0x100) ? "" : "no ");
-			printk("hub:  %s speed\n", (portstatus & 0x200) ? "low" : "full");
-#endif
 		}
 		tmp = next;
-#if 0
-		wait_ms(1000);
-#endif
         }
 
 	spin_unlock_irqrestore(&hub_event_lock, flags);
@@ -370,9 +414,11 @@
 
 static int usb_hub_thread(void *__hub)
 {
+/*
 	MOD_INC_USE_COUNT;
+*/
 	
-	printk(KERN_INFO "USB hub driver registered\n");
+	khubd_running = 1;
 
 	lock_kernel();
 
@@ -389,13 +435,16 @@
 
 	/* Send me a signal to get me die (for debugging) */
 	do {
-		interruptible_sleep_on(&usb_hub_wait);
 		usb_hub_events();
+		interruptible_sleep_on(&khubd_wait);
 	} while (!signal_pending(current));
 
+/*
 	MOD_DEC_USE_COUNT;
+*/
 
 	printk("usb_hub_thread exiting\n");
+	khubd_running = 0;
 
 	return 0;
 }
@@ -414,24 +463,44 @@
 {
 	int pid;
 
-	INIT_LIST_HEAD(&hub_event_list);
-	INIT_LIST_HEAD(&all_hubs_list);
-
 	usb_register(&hub_driver);
-	pid = kernel_thread(usb_hub_thread, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
+	printk(KERN_INFO "USB hub driver registered\n");
+
+	pid = kernel_thread(usb_hub_thread, NULL,
+		CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
 	if (pid >= 0) {
 		khubd_pid = pid;
+
 		return 0;
 	}
 
 	/* Fall through if kernel_thread failed */
 	usb_deregister(&hub_driver);
 
-	return 0;
+	return 1;
 }
 
 void usb_hub_cleanup(void)
 {
+	struct list_head *next, *tmp, *head = &hub_list;
+	struct usb_hub *hub;
+	unsigned long flags, flags2;
+	int ret;
+
+	/* Kill the thread */
+	ret = kill_proc(khubd_pid, SIGTERM, 1);
+	if (!ret) {
+		int count = 10;
+
+		while (khubd_running && --count) {
+			current->state = TASK_INTERRUPTIBLE;
+			schedule_timeout(1);
+		}
+
+		if (!count)
+			printk(KERN_ERR "hub: giving up on killing khubd\n");
+	}
+
 	/*
 	 * Hub resources are freed for us by usb_deregister.  It
 	 * usb_driver_purge on every device which in turn calls that
@@ -443,11 +512,14 @@
 } /* usb_hub_cleanup() */
 
 #ifdef MODULE
-int init_module(void){
+int init_module(void)
+{
 	return usb_hub_init();
 }
 
-void cleanup_module(void){
+void cleanup_module(void)
+{
 	usb_hub_cleanup();
 }
 #endif
+

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)