| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * drivers/usb/driver.c - most of the driver model stuff for usb | 
 | 3 |  * | 
 | 4 |  * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de> | 
 | 5 |  * | 
 | 6 |  * based on drivers/usb/usb.c which had the following copyrights: | 
 | 7 |  *	(C) Copyright Linus Torvalds 1999 | 
 | 8 |  *	(C) Copyright Johannes Erdfelt 1999-2001 | 
 | 9 |  *	(C) Copyright Andreas Gal 1999 | 
 | 10 |  *	(C) Copyright Gregory P. Smith 1999 | 
 | 11 |  *	(C) Copyright Deti Fliegl 1999 (new USB architecture) | 
 | 12 |  *	(C) Copyright Randy Dunlap 2000 | 
 | 13 |  *	(C) Copyright David Brownell 2000-2004 | 
 | 14 |  *	(C) Copyright Yggdrasil Computing, Inc. 2000 | 
 | 15 |  *		(usb_device_id matching changes by Adam J. Richter) | 
 | 16 |  *	(C) Copyright Greg Kroah-Hartman 2002-2003 | 
 | 17 |  * | 
 | 18 |  * NOTE! This is not actually a driver at all, rather this is | 
 | 19 |  * just a collection of helper routines that implement the | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 20 |  * matching, probing, releasing, suspending and resuming for | 
 | 21 |  * real drivers. | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 22 |  * | 
 | 23 |  */ | 
 | 24 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 25 | #include <linux/device.h> | 
 | 26 | #include <linux/usb.h> | 
| Alan Stern | bd85928 | 2006-09-19 10:14:07 -0400 | [diff] [blame] | 27 | #include <linux/workqueue.h> | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 28 | #include "hcd.h" | 
 | 29 | #include "usb.h" | 
 | 30 |  | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 31 | #ifdef CONFIG_HOTPLUG | 
 | 32 |  | 
 | 33 | /* | 
 | 34 |  * Adds a new dynamic USBdevice ID to this driver, | 
 | 35 |  * and cause the driver to probe for all devices again. | 
 | 36 |  */ | 
| Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame^] | 37 | ssize_t usb_store_new_id(struct usb_dynids *dynids, | 
 | 38 | 			 struct device_driver *driver, | 
 | 39 | 			 const char *buf, size_t count) | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 40 | { | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 41 | 	struct usb_dynid *dynid; | 
 | 42 | 	u32 idVendor = 0; | 
 | 43 | 	u32 idProduct = 0; | 
 | 44 | 	int fields = 0; | 
| Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 45 | 	int retval = 0; | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 46 |  | 
 | 47 | 	fields = sscanf(buf, "%x %x", &idVendor, &idProduct); | 
 | 48 | 	if (fields < 2) | 
 | 49 | 		return -EINVAL; | 
 | 50 |  | 
 | 51 | 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); | 
 | 52 | 	if (!dynid) | 
 | 53 | 		return -ENOMEM; | 
 | 54 |  | 
 | 55 | 	INIT_LIST_HEAD(&dynid->node); | 
 | 56 | 	dynid->id.idVendor = idVendor; | 
 | 57 | 	dynid->id.idProduct = idProduct; | 
 | 58 | 	dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; | 
 | 59 |  | 
| Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame^] | 60 | 	spin_lock(&dynids->lock); | 
 | 61 | 	list_add_tail(&dynids->list, &dynid->node); | 
 | 62 | 	spin_unlock(&dynids->lock); | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 63 |  | 
 | 64 | 	if (get_driver(driver)) { | 
| Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 65 | 		retval = driver_attach(driver); | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 66 | 		put_driver(driver); | 
 | 67 | 	} | 
 | 68 |  | 
| Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 69 | 	if (retval) | 
 | 70 | 		return retval; | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 71 | 	return count; | 
 | 72 | } | 
| Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame^] | 73 | EXPORT_SYMBOL_GPL(usb_store_new_id); | 
 | 74 |  | 
 | 75 | static ssize_t store_new_id(struct device_driver *driver, | 
 | 76 | 			    const char *buf, size_t count) | 
 | 77 | { | 
 | 78 | 	struct usb_driver *usb_drv = to_usb_driver(driver); | 
 | 79 |  | 
 | 80 | 	return usb_store_new_id(&usb_drv->dynids, driver, buf, count); | 
 | 81 | } | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 82 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); | 
 | 83 |  | 
 | 84 | static int usb_create_newid_file(struct usb_driver *usb_drv) | 
 | 85 | { | 
 | 86 | 	int error = 0; | 
 | 87 |  | 
| Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 88 | 	if (usb_drv->no_dynamic_id) | 
 | 89 | 		goto exit; | 
 | 90 |  | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 91 | 	if (usb_drv->probe != NULL) | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 92 | 		error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj, | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 93 | 					  &driver_attr_new_id.attr); | 
| Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 94 | exit: | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 95 | 	return error; | 
 | 96 | } | 
 | 97 |  | 
| Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 98 | static void usb_remove_newid_file(struct usb_driver *usb_drv) | 
 | 99 | { | 
 | 100 | 	if (usb_drv->no_dynamic_id) | 
 | 101 | 		return; | 
 | 102 |  | 
 | 103 | 	if (usb_drv->probe != NULL) | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 104 | 		sysfs_remove_file(&usb_drv->drvwrap.driver.kobj, | 
| Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 105 | 				  &driver_attr_new_id.attr); | 
 | 106 | } | 
 | 107 |  | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 108 | static void usb_free_dynids(struct usb_driver *usb_drv) | 
 | 109 | { | 
 | 110 | 	struct usb_dynid *dynid, *n; | 
 | 111 |  | 
 | 112 | 	spin_lock(&usb_drv->dynids.lock); | 
 | 113 | 	list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { | 
 | 114 | 		list_del(&dynid->node); | 
 | 115 | 		kfree(dynid); | 
 | 116 | 	} | 
 | 117 | 	spin_unlock(&usb_drv->dynids.lock); | 
 | 118 | } | 
 | 119 | #else | 
 | 120 | static inline int usb_create_newid_file(struct usb_driver *usb_drv) | 
 | 121 | { | 
 | 122 | 	return 0; | 
 | 123 | } | 
 | 124 |  | 
| Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 125 | static void usb_remove_newid_file(struct usb_driver *usb_drv) | 
 | 126 | { | 
 | 127 | } | 
 | 128 |  | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 129 | static inline void usb_free_dynids(struct usb_driver *usb_drv) | 
 | 130 | { | 
 | 131 | } | 
 | 132 | #endif | 
 | 133 |  | 
 | 134 | static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf, | 
 | 135 | 							struct usb_driver *drv) | 
 | 136 | { | 
 | 137 | 	struct usb_dynid *dynid; | 
 | 138 |  | 
 | 139 | 	spin_lock(&drv->dynids.lock); | 
 | 140 | 	list_for_each_entry(dynid, &drv->dynids.list, node) { | 
 | 141 | 		if (usb_match_one_id(intf, &dynid->id)) { | 
 | 142 | 			spin_unlock(&drv->dynids.lock); | 
 | 143 | 			return &dynid->id; | 
 | 144 | 		} | 
 | 145 | 	} | 
 | 146 | 	spin_unlock(&drv->dynids.lock); | 
 | 147 | 	return NULL; | 
 | 148 | } | 
 | 149 |  | 
 | 150 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 151 | /* called from driver core with dev locked */ | 
 | 152 | static int usb_probe_device(struct device *dev) | 
 | 153 | { | 
 | 154 | 	struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); | 
 | 155 | 	struct usb_device *udev; | 
 | 156 | 	int error = -ENODEV; | 
 | 157 |  | 
 | 158 | 	dev_dbg(dev, "%s\n", __FUNCTION__); | 
 | 159 |  | 
 | 160 | 	if (!is_usb_device(dev))	/* Sanity check */ | 
 | 161 | 		return error; | 
 | 162 |  | 
 | 163 | 	udev = to_usb_device(dev); | 
 | 164 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 165 | 	/* TODO: Add real matching code */ | 
 | 166 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 167 | 	/* The device should always appear to be in use | 
 | 168 | 	 * unless the driver suports autosuspend. | 
 | 169 | 	 */ | 
 | 170 | 	udev->pm_usage_cnt = !(udriver->supports_autosuspend); | 
 | 171 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 172 | 	error = udriver->probe(udev); | 
 | 173 | 	return error; | 
 | 174 | } | 
 | 175 |  | 
 | 176 | /* called from driver core with dev locked */ | 
 | 177 | static int usb_unbind_device(struct device *dev) | 
 | 178 | { | 
 | 179 | 	struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); | 
 | 180 |  | 
 | 181 | 	udriver->disconnect(to_usb_device(dev)); | 
 | 182 | 	return 0; | 
 | 183 | } | 
 | 184 |  | 
 | 185 |  | 
 | 186 | /* called from driver core with dev locked */ | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 187 | static int usb_probe_interface(struct device *dev) | 
 | 188 | { | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 189 | 	struct usb_driver *driver = to_usb_driver(dev->driver); | 
 | 190 | 	struct usb_interface *intf; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 191 | 	struct usb_device *udev; | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 192 | 	const struct usb_device_id *id; | 
 | 193 | 	int error = -ENODEV; | 
 | 194 |  | 
 | 195 | 	dev_dbg(dev, "%s\n", __FUNCTION__); | 
 | 196 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 197 | 	if (is_usb_device(dev))		/* Sanity check */ | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 198 | 		return error; | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 199 |  | 
 | 200 | 	intf = to_usb_interface(dev); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 201 | 	udev = interface_to_usbdev(intf); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 202 |  | 
 | 203 | 	id = usb_match_id(intf, driver->id_table); | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 204 | 	if (!id) | 
 | 205 | 		id = usb_match_dynamic_id(intf, driver); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 206 | 	if (id) { | 
 | 207 | 		dev_dbg(dev, "%s - got id\n", __FUNCTION__); | 
 | 208 |  | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 209 | 		error = usb_autoresume_device(udev); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 210 | 		if (error) | 
 | 211 | 			return error; | 
 | 212 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 213 | 		/* Interface "power state" doesn't correspond to any hardware | 
 | 214 | 		 * state whatsoever.  We use it to record when it's bound to | 
 | 215 | 		 * a driver that may start I/0:  it's not frozen/quiesced. | 
 | 216 | 		 */ | 
 | 217 | 		mark_active(intf); | 
 | 218 | 		intf->condition = USB_INTERFACE_BINDING; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 219 |  | 
 | 220 | 		/* The interface should always appear to be in use | 
 | 221 | 		 * unless the driver suports autosuspend. | 
 | 222 | 		 */ | 
 | 223 | 		intf->pm_usage_cnt = !(driver->supports_autosuspend); | 
 | 224 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 225 | 		error = driver->probe(intf, id); | 
 | 226 | 		if (error) { | 
 | 227 | 			mark_quiesced(intf); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 228 | 			intf->needs_remote_wakeup = 0; | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 229 | 			intf->condition = USB_INTERFACE_UNBOUND; | 
 | 230 | 		} else | 
 | 231 | 			intf->condition = USB_INTERFACE_BOUND; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 232 |  | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 233 | 		usb_autosuspend_device(udev); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 234 | 	} | 
 | 235 |  | 
 | 236 | 	return error; | 
 | 237 | } | 
 | 238 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 239 | /* called from driver core with dev locked */ | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 240 | static int usb_unbind_interface(struct device *dev) | 
 | 241 | { | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 242 | 	struct usb_driver *driver = to_usb_driver(dev->driver); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 243 | 	struct usb_interface *intf = to_usb_interface(dev); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 244 | 	struct usb_device *udev; | 
 | 245 | 	int error; | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 246 |  | 
 | 247 | 	intf->condition = USB_INTERFACE_UNBINDING; | 
 | 248 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 249 | 	/* Autoresume for set_interface call below */ | 
 | 250 | 	udev = interface_to_usbdev(intf); | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 251 | 	error = usb_autoresume_device(udev); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 252 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 253 | 	/* release all urbs for this interface */ | 
 | 254 | 	usb_disable_interface(interface_to_usbdev(intf), intf); | 
 | 255 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 256 | 	driver->disconnect(intf); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 257 |  | 
 | 258 | 	/* reset other interface state */ | 
 | 259 | 	usb_set_interface(interface_to_usbdev(intf), | 
 | 260 | 			intf->altsetting[0].desc.bInterfaceNumber, | 
 | 261 | 			0); | 
 | 262 | 	usb_set_intfdata(intf, NULL); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 263 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 264 | 	intf->condition = USB_INTERFACE_UNBOUND; | 
 | 265 | 	mark_quiesced(intf); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 266 | 	intf->needs_remote_wakeup = 0; | 
 | 267 |  | 
 | 268 | 	if (!error) | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 269 | 		usb_autosuspend_device(udev); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 270 |  | 
 | 271 | 	return 0; | 
 | 272 | } | 
 | 273 |  | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 274 | /** | 
 | 275 |  * usb_driver_claim_interface - bind a driver to an interface | 
 | 276 |  * @driver: the driver to be bound | 
 | 277 |  * @iface: the interface to which it will be bound; must be in the | 
 | 278 |  *	usb device's active configuration | 
 | 279 |  * @priv: driver data associated with that interface | 
 | 280 |  * | 
 | 281 |  * This is used by usb device drivers that need to claim more than one | 
 | 282 |  * interface on a device when probing (audio and acm are current examples). | 
 | 283 |  * No device driver should directly modify internal usb_interface or | 
 | 284 |  * usb_device structure members. | 
 | 285 |  * | 
 | 286 |  * Few drivers should need to use this routine, since the most natural | 
 | 287 |  * way to bind to an interface is to return the private data from | 
 | 288 |  * the driver's probe() method. | 
 | 289 |  * | 
 | 290 |  * Callers must own the device lock and the driver model's usb_bus_type.subsys | 
 | 291 |  * writelock.  So driver probe() entries don't need extra locking, | 
 | 292 |  * but other call contexts may need to explicitly claim those locks. | 
 | 293 |  */ | 
 | 294 | int usb_driver_claim_interface(struct usb_driver *driver, | 
 | 295 | 				struct usb_interface *iface, void* priv) | 
 | 296 | { | 
 | 297 | 	struct device *dev = &iface->dev; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 298 | 	struct usb_device *udev = interface_to_usbdev(iface); | 
| Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 299 | 	int retval = 0; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 300 |  | 
 | 301 | 	if (dev->driver) | 
 | 302 | 		return -EBUSY; | 
 | 303 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 304 | 	dev->driver = &driver->drvwrap.driver; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 305 | 	usb_set_intfdata(iface, priv); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 306 |  | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 307 | 	usb_pm_lock(udev); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 308 | 	iface->condition = USB_INTERFACE_BOUND; | 
 | 309 | 	mark_active(iface); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 310 | 	iface->pm_usage_cnt = !(driver->supports_autosuspend); | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 311 | 	usb_pm_unlock(udev); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 312 |  | 
 | 313 | 	/* if interface was already added, bind now; else let | 
 | 314 | 	 * the future device_add() bind it, bypassing probe() | 
 | 315 | 	 */ | 
 | 316 | 	if (device_is_registered(dev)) | 
| Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 317 | 		retval = device_bind_driver(dev); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 318 |  | 
| Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 319 | 	return retval; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 320 | } | 
 | 321 | EXPORT_SYMBOL(usb_driver_claim_interface); | 
 | 322 |  | 
 | 323 | /** | 
 | 324 |  * usb_driver_release_interface - unbind a driver from an interface | 
 | 325 |  * @driver: the driver to be unbound | 
 | 326 |  * @iface: the interface from which it will be unbound | 
 | 327 |  * | 
 | 328 |  * This can be used by drivers to release an interface without waiting | 
 | 329 |  * for their disconnect() methods to be called.  In typical cases this | 
 | 330 |  * also causes the driver disconnect() method to be called. | 
 | 331 |  * | 
 | 332 |  * This call is synchronous, and may not be used in an interrupt context. | 
 | 333 |  * Callers must own the device lock and the driver model's usb_bus_type.subsys | 
 | 334 |  * writelock.  So driver disconnect() entries don't need extra locking, | 
 | 335 |  * but other call contexts may need to explicitly claim those locks. | 
 | 336 |  */ | 
 | 337 | void usb_driver_release_interface(struct usb_driver *driver, | 
 | 338 | 					struct usb_interface *iface) | 
 | 339 | { | 
 | 340 | 	struct device *dev = &iface->dev; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 341 | 	struct usb_device *udev = interface_to_usbdev(iface); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 342 |  | 
 | 343 | 	/* this should never happen, don't release something that's not ours */ | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 344 | 	if (!dev->driver || dev->driver != &driver->drvwrap.driver) | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 345 | 		return; | 
 | 346 |  | 
 | 347 | 	/* don't release from within disconnect() */ | 
 | 348 | 	if (iface->condition != USB_INTERFACE_BOUND) | 
 | 349 | 		return; | 
 | 350 |  | 
 | 351 | 	/* don't release if the interface hasn't been added yet */ | 
 | 352 | 	if (device_is_registered(dev)) { | 
 | 353 | 		iface->condition = USB_INTERFACE_UNBINDING; | 
 | 354 | 		device_release_driver(dev); | 
 | 355 | 	} | 
 | 356 |  | 
 | 357 | 	dev->driver = NULL; | 
 | 358 | 	usb_set_intfdata(iface, NULL); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 359 |  | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 360 | 	usb_pm_lock(udev); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 361 | 	iface->condition = USB_INTERFACE_UNBOUND; | 
 | 362 | 	mark_quiesced(iface); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 363 | 	iface->needs_remote_wakeup = 0; | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 364 | 	usb_pm_unlock(udev); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 365 | } | 
 | 366 | EXPORT_SYMBOL(usb_driver_release_interface); | 
 | 367 |  | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 368 | /* returns 0 if no match, 1 if match */ | 
| Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame^] | 369 | int usb_match_one_id(struct usb_interface *interface, | 
 | 370 | 		     const struct usb_device_id *id) | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 371 | { | 
 | 372 | 	struct usb_host_interface *intf; | 
 | 373 | 	struct usb_device *dev; | 
 | 374 |  | 
 | 375 | 	/* proc_connectinfo in devio.c may call us with id == NULL. */ | 
 | 376 | 	if (id == NULL) | 
 | 377 | 		return 0; | 
 | 378 |  | 
 | 379 | 	intf = interface->cur_altsetting; | 
 | 380 | 	dev = interface_to_usbdev(interface); | 
 | 381 |  | 
 | 382 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && | 
 | 383 | 	    id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) | 
 | 384 | 		return 0; | 
 | 385 |  | 
 | 386 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && | 
 | 387 | 	    id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) | 
 | 388 | 		return 0; | 
 | 389 |  | 
 | 390 | 	/* No need to test id->bcdDevice_lo != 0, since 0 is never | 
 | 391 | 	   greater than any unsigned number. */ | 
 | 392 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && | 
 | 393 | 	    (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) | 
 | 394 | 		return 0; | 
 | 395 |  | 
 | 396 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && | 
 | 397 | 	    (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) | 
 | 398 | 		return 0; | 
 | 399 |  | 
 | 400 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && | 
 | 401 | 	    (id->bDeviceClass != dev->descriptor.bDeviceClass)) | 
 | 402 | 		return 0; | 
 | 403 |  | 
 | 404 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && | 
 | 405 | 	    (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) | 
 | 406 | 		return 0; | 
 | 407 |  | 
 | 408 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && | 
 | 409 | 	    (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) | 
 | 410 | 		return 0; | 
 | 411 |  | 
| Alan Stern | 93c8bf4 | 2006-10-18 16:41:51 -0400 | [diff] [blame] | 412 | 	/* The interface class, subclass, and protocol should never be | 
 | 413 | 	 * checked for a match if the device class is Vendor Specific, | 
 | 414 | 	 * unless the match record specifies the Vendor ID. */ | 
 | 415 | 	if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC && | 
 | 416 | 			!(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && | 
 | 417 | 			(id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | | 
 | 418 | 				USB_DEVICE_ID_MATCH_INT_SUBCLASS | | 
 | 419 | 				USB_DEVICE_ID_MATCH_INT_PROTOCOL))) | 
 | 420 | 		return 0; | 
 | 421 |  | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 422 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && | 
 | 423 | 	    (id->bInterfaceClass != intf->desc.bInterfaceClass)) | 
 | 424 | 		return 0; | 
 | 425 |  | 
 | 426 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && | 
 | 427 | 	    (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) | 
 | 428 | 		return 0; | 
 | 429 |  | 
 | 430 | 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && | 
 | 431 | 	    (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) | 
 | 432 | 		return 0; | 
 | 433 |  | 
 | 434 | 	return 1; | 
 | 435 | } | 
| Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame^] | 436 | EXPORT_SYMBOL_GPL(usb_match_one_id); | 
 | 437 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 438 | /** | 
 | 439 |  * usb_match_id - find first usb_device_id matching device or interface | 
 | 440 |  * @interface: the interface of interest | 
 | 441 |  * @id: array of usb_device_id structures, terminated by zero entry | 
 | 442 |  * | 
 | 443 |  * usb_match_id searches an array of usb_device_id's and returns | 
 | 444 |  * the first one matching the device or interface, or null. | 
 | 445 |  * This is used when binding (or rebinding) a driver to an interface. | 
 | 446 |  * Most USB device drivers will use this indirectly, through the usb core, | 
 | 447 |  * but some layered driver frameworks use it directly. | 
 | 448 |  * These device tables are exported with MODULE_DEVICE_TABLE, through | 
 | 449 |  * modutils, to support the driver loading functionality of USB hotplugging. | 
 | 450 |  * | 
 | 451 |  * What Matches: | 
 | 452 |  * | 
 | 453 |  * The "match_flags" element in a usb_device_id controls which | 
 | 454 |  * members are used.  If the corresponding bit is set, the | 
 | 455 |  * value in the device_id must match its corresponding member | 
 | 456 |  * in the device or interface descriptor, or else the device_id | 
 | 457 |  * does not match. | 
 | 458 |  * | 
 | 459 |  * "driver_info" is normally used only by device drivers, | 
 | 460 |  * but you can create a wildcard "matches anything" usb_device_id | 
 | 461 |  * as a driver's "modules.usbmap" entry if you provide an id with | 
 | 462 |  * only a nonzero "driver_info" field.  If you do this, the USB device | 
 | 463 |  * driver's probe() routine should use additional intelligence to | 
 | 464 |  * decide whether to bind to the specified interface. | 
 | 465 |  * | 
 | 466 |  * What Makes Good usb_device_id Tables: | 
 | 467 |  * | 
 | 468 |  * The match algorithm is very simple, so that intelligence in | 
 | 469 |  * driver selection must come from smart driver id records. | 
 | 470 |  * Unless you have good reasons to use another selection policy, | 
 | 471 |  * provide match elements only in related groups, and order match | 
 | 472 |  * specifiers from specific to general.  Use the macros provided | 
 | 473 |  * for that purpose if you can. | 
 | 474 |  * | 
 | 475 |  * The most specific match specifiers use device descriptor | 
 | 476 |  * data.  These are commonly used with product-specific matches; | 
 | 477 |  * the USB_DEVICE macro lets you provide vendor and product IDs, | 
 | 478 |  * and you can also match against ranges of product revisions. | 
 | 479 |  * These are widely used for devices with application or vendor | 
 | 480 |  * specific bDeviceClass values. | 
 | 481 |  * | 
 | 482 |  * Matches based on device class/subclass/protocol specifications | 
 | 483 |  * are slightly more general; use the USB_DEVICE_INFO macro, or | 
 | 484 |  * its siblings.  These are used with single-function devices | 
 | 485 |  * where bDeviceClass doesn't specify that each interface has | 
 | 486 |  * its own class. | 
 | 487 |  * | 
 | 488 |  * Matches based on interface class/subclass/protocol are the | 
 | 489 |  * most general; they let drivers bind to any interface on a | 
 | 490 |  * multiple-function device.  Use the USB_INTERFACE_INFO | 
 | 491 |  * macro, or its siblings, to match class-per-interface style | 
| Alan Stern | 93c8bf4 | 2006-10-18 16:41:51 -0400 | [diff] [blame] | 492 |  * devices (as recorded in bInterfaceClass). | 
 | 493 |  * | 
 | 494 |  * Note that an entry created by USB_INTERFACE_INFO won't match | 
 | 495 |  * any interface if the device class is set to Vendor-Specific. | 
 | 496 |  * This is deliberate; according to the USB spec the meanings of | 
 | 497 |  * the interface class/subclass/protocol for these devices are also | 
 | 498 |  * vendor-specific, and hence matching against a standard product | 
 | 499 |  * class wouldn't work anyway.  If you really want to use an | 
 | 500 |  * interface-based match for such a device, create a match record | 
 | 501 |  * that also specifies the vendor ID.  (Unforunately there isn't a | 
 | 502 |  * standard macro for creating records like this.) | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 503 |  * | 
 | 504 |  * Within those groups, remember that not all combinations are | 
 | 505 |  * meaningful.  For example, don't give a product version range | 
 | 506 |  * without vendor and product IDs; or specify a protocol without | 
 | 507 |  * its associated class and subclass. | 
 | 508 |  */ | 
 | 509 | const struct usb_device_id *usb_match_id(struct usb_interface *interface, | 
 | 510 | 					 const struct usb_device_id *id) | 
 | 511 | { | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 512 | 	/* proc_connectinfo in devio.c may call us with id == NULL. */ | 
 | 513 | 	if (id == NULL) | 
 | 514 | 		return NULL; | 
 | 515 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 516 | 	/* It is important to check that id->driver_info is nonzero, | 
 | 517 | 	   since an entry that is all zeroes except for a nonzero | 
 | 518 | 	   id->driver_info is the way to create an entry that | 
 | 519 | 	   indicates that the driver want to examine every | 
 | 520 | 	   device and interface. */ | 
 | 521 | 	for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass || | 
 | 522 | 	       id->driver_info; id++) { | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 523 | 		if (usb_match_one_id(interface, id)) | 
 | 524 | 			return id; | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 525 | 	} | 
 | 526 |  | 
 | 527 | 	return NULL; | 
 | 528 | } | 
| Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 529 | EXPORT_SYMBOL_GPL_FUTURE(usb_match_id); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 530 |  | 
| Adrian Bunk | 8bb22d2 | 2006-11-21 22:02:54 +0100 | [diff] [blame] | 531 | static int usb_device_match(struct device *dev, struct device_driver *drv) | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 532 | { | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 533 | 	/* devices and interfaces are handled separately */ | 
 | 534 | 	if (is_usb_device(dev)) { | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 535 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 536 | 		/* interface drivers never match devices */ | 
 | 537 | 		if (!is_usb_device_driver(drv)) | 
 | 538 | 			return 0; | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 539 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 540 | 		/* TODO: Add real matching code */ | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 541 | 		return 1; | 
 | 542 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 543 | 	} else { | 
 | 544 | 		struct usb_interface *intf; | 
 | 545 | 		struct usb_driver *usb_drv; | 
 | 546 | 		const struct usb_device_id *id; | 
 | 547 |  | 
 | 548 | 		/* device drivers never match interfaces */ | 
 | 549 | 		if (is_usb_device_driver(drv)) | 
 | 550 | 			return 0; | 
 | 551 |  | 
 | 552 | 		intf = to_usb_interface(dev); | 
 | 553 | 		usb_drv = to_usb_driver(drv); | 
 | 554 |  | 
 | 555 | 		id = usb_match_id(intf, usb_drv->id_table); | 
 | 556 | 		if (id) | 
 | 557 | 			return 1; | 
 | 558 |  | 
 | 559 | 		id = usb_match_dynamic_id(intf, usb_drv); | 
 | 560 | 		if (id) | 
 | 561 | 			return 1; | 
 | 562 | 	} | 
 | 563 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 564 | 	return 0; | 
 | 565 | } | 
 | 566 |  | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 567 | #ifdef	CONFIG_HOTPLUG | 
 | 568 |  | 
 | 569 | /* | 
 | 570 |  * This sends an uevent to userspace, typically helping to load driver | 
 | 571 |  * or other modules, configure the device, and more.  Drivers can provide | 
 | 572 |  * a MODULE_DEVICE_TABLE to help with module loading subtasks. | 
 | 573 |  * | 
 | 574 |  * We're called either from khubd (the typical case) or from root hub | 
 | 575 |  * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle | 
 | 576 |  * delays in event delivery.  Use sysfs (and DEVPATH) to make sure the | 
 | 577 |  * device (and this configuration!) are still present. | 
 | 578 |  */ | 
 | 579 | static int usb_uevent(struct device *dev, char **envp, int num_envp, | 
 | 580 | 		      char *buffer, int buffer_size) | 
 | 581 | { | 
 | 582 | 	struct usb_interface *intf; | 
 | 583 | 	struct usb_device *usb_dev; | 
 | 584 | 	struct usb_host_interface *alt; | 
 | 585 | 	int i = 0; | 
 | 586 | 	int length = 0; | 
 | 587 |  | 
 | 588 | 	if (!dev) | 
 | 589 | 		return -ENODEV; | 
 | 590 |  | 
 | 591 | 	/* driver is often null here; dev_dbg() would oops */ | 
 | 592 | 	pr_debug ("usb %s: uevent\n", dev->bus_id); | 
 | 593 |  | 
| Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 594 | 	if (is_usb_device(dev)) { | 
 | 595 | 		usb_dev = to_usb_device(dev); | 
 | 596 | 		alt = NULL; | 
 | 597 | 	} else { | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 598 | 		intf = to_usb_interface(dev); | 
 | 599 | 		usb_dev = interface_to_usbdev(intf); | 
 | 600 | 		alt = intf->cur_altsetting; | 
 | 601 | 	} | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 602 |  | 
 | 603 | 	if (usb_dev->devnum < 0) { | 
 | 604 | 		pr_debug ("usb %s: already deleted?\n", dev->bus_id); | 
 | 605 | 		return -ENODEV; | 
 | 606 | 	} | 
 | 607 | 	if (!usb_dev->bus) { | 
 | 608 | 		pr_debug ("usb %s: bus removed?\n", dev->bus_id); | 
 | 609 | 		return -ENODEV; | 
 | 610 | 	} | 
 | 611 |  | 
 | 612 | #ifdef	CONFIG_USB_DEVICEFS | 
 | 613 | 	/* If this is available, userspace programs can directly read | 
 | 614 | 	 * all the device descriptors we don't tell them about.  Or | 
 | 615 | 	 * even act as usermode drivers. | 
 | 616 | 	 * | 
 | 617 | 	 * FIXME reduce hardwired intelligence here | 
 | 618 | 	 */ | 
 | 619 | 	if (add_uevent_var(envp, num_envp, &i, | 
 | 620 | 			   buffer, buffer_size, &length, | 
 | 621 | 			   "DEVICE=/proc/bus/usb/%03d/%03d", | 
 | 622 | 			   usb_dev->bus->busnum, usb_dev->devnum)) | 
 | 623 | 		return -ENOMEM; | 
 | 624 | #endif | 
 | 625 |  | 
 | 626 | 	/* per-device configurations are common */ | 
 | 627 | 	if (add_uevent_var(envp, num_envp, &i, | 
 | 628 | 			   buffer, buffer_size, &length, | 
 | 629 | 			   "PRODUCT=%x/%x/%x", | 
 | 630 | 			   le16_to_cpu(usb_dev->descriptor.idVendor), | 
 | 631 | 			   le16_to_cpu(usb_dev->descriptor.idProduct), | 
 | 632 | 			   le16_to_cpu(usb_dev->descriptor.bcdDevice))) | 
 | 633 | 		return -ENOMEM; | 
 | 634 |  | 
 | 635 | 	/* class-based driver binding models */ | 
 | 636 | 	if (add_uevent_var(envp, num_envp, &i, | 
 | 637 | 			   buffer, buffer_size, &length, | 
 | 638 | 			   "TYPE=%d/%d/%d", | 
 | 639 | 			   usb_dev->descriptor.bDeviceClass, | 
 | 640 | 			   usb_dev->descriptor.bDeviceSubClass, | 
 | 641 | 			   usb_dev->descriptor.bDeviceProtocol)) | 
 | 642 | 		return -ENOMEM; | 
 | 643 |  | 
| Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 644 | 	if (!is_usb_device(dev)) { | 
 | 645 |  | 
 | 646 | 		if (add_uevent_var(envp, num_envp, &i, | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 647 | 			   buffer, buffer_size, &length, | 
 | 648 | 			   "INTERFACE=%d/%d/%d", | 
 | 649 | 			   alt->desc.bInterfaceClass, | 
 | 650 | 			   alt->desc.bInterfaceSubClass, | 
 | 651 | 			   alt->desc.bInterfaceProtocol)) | 
| Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 652 | 			return -ENOMEM; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 653 |  | 
| Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 654 | 		if (add_uevent_var(envp, num_envp, &i, | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 655 | 			   buffer, buffer_size, &length, | 
 | 656 | 			   "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", | 
 | 657 | 			   le16_to_cpu(usb_dev->descriptor.idVendor), | 
 | 658 | 			   le16_to_cpu(usb_dev->descriptor.idProduct), | 
 | 659 | 			   le16_to_cpu(usb_dev->descriptor.bcdDevice), | 
 | 660 | 			   usb_dev->descriptor.bDeviceClass, | 
 | 661 | 			   usb_dev->descriptor.bDeviceSubClass, | 
 | 662 | 			   usb_dev->descriptor.bDeviceProtocol, | 
 | 663 | 			   alt->desc.bInterfaceClass, | 
 | 664 | 			   alt->desc.bInterfaceSubClass, | 
 | 665 | 			   alt->desc.bInterfaceProtocol)) | 
| Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 666 | 			return -ENOMEM; | 
 | 667 | 	} | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 668 |  | 
 | 669 | 	envp[i] = NULL; | 
 | 670 |  | 
 | 671 | 	return 0; | 
 | 672 | } | 
 | 673 |  | 
 | 674 | #else | 
 | 675 |  | 
 | 676 | static int usb_uevent(struct device *dev, char **envp, | 
 | 677 | 			int num_envp, char *buffer, int buffer_size) | 
 | 678 | { | 
 | 679 | 	return -ENODEV; | 
 | 680 | } | 
 | 681 |  | 
 | 682 | #endif	/* CONFIG_HOTPLUG */ | 
 | 683 |  | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 684 | /** | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 685 |  * usb_register_device_driver - register a USB device (not interface) driver | 
 | 686 |  * @new_udriver: USB operations for the device driver | 
| Greg Kroah-Hartman | 2143acc | 2005-11-21 14:53:03 -0800 | [diff] [blame] | 687 |  * @owner: module owner of this driver. | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 688 |  * | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 689 |  * Registers a USB device driver with the USB core.  The list of | 
 | 690 |  * unattached devices will be rescanned whenever a new driver is | 
 | 691 |  * added, allowing the new driver to attach to any recognized devices. | 
 | 692 |  * Returns a negative error code on failure and 0 on success. | 
 | 693 |  */ | 
 | 694 | int usb_register_device_driver(struct usb_device_driver *new_udriver, | 
 | 695 | 		struct module *owner) | 
 | 696 | { | 
 | 697 | 	int retval = 0; | 
 | 698 |  | 
 | 699 | 	if (usb_disabled()) | 
 | 700 | 		return -ENODEV; | 
 | 701 |  | 
 | 702 | 	new_udriver->drvwrap.for_devices = 1; | 
 | 703 | 	new_udriver->drvwrap.driver.name = (char *) new_udriver->name; | 
 | 704 | 	new_udriver->drvwrap.driver.bus = &usb_bus_type; | 
 | 705 | 	new_udriver->drvwrap.driver.probe = usb_probe_device; | 
 | 706 | 	new_udriver->drvwrap.driver.remove = usb_unbind_device; | 
 | 707 | 	new_udriver->drvwrap.driver.owner = owner; | 
 | 708 |  | 
 | 709 | 	retval = driver_register(&new_udriver->drvwrap.driver); | 
 | 710 |  | 
 | 711 | 	if (!retval) { | 
 | 712 | 		pr_info("%s: registered new device driver %s\n", | 
 | 713 | 			usbcore_name, new_udriver->name); | 
 | 714 | 		usbfs_update_special(); | 
 | 715 | 	} else { | 
 | 716 | 		printk(KERN_ERR "%s: error %d registering device " | 
 | 717 | 			"	driver %s\n", | 
 | 718 | 			usbcore_name, retval, new_udriver->name); | 
 | 719 | 	} | 
 | 720 |  | 
 | 721 | 	return retval; | 
 | 722 | } | 
 | 723 | EXPORT_SYMBOL_GPL(usb_register_device_driver); | 
 | 724 |  | 
 | 725 | /** | 
 | 726 |  * usb_deregister_device_driver - unregister a USB device (not interface) driver | 
 | 727 |  * @udriver: USB operations of the device driver to unregister | 
 | 728 |  * Context: must be able to sleep | 
 | 729 |  * | 
 | 730 |  * Unlinks the specified driver from the internal USB driver list. | 
 | 731 |  */ | 
 | 732 | void usb_deregister_device_driver(struct usb_device_driver *udriver) | 
 | 733 | { | 
 | 734 | 	pr_info("%s: deregistering device driver %s\n", | 
 | 735 | 			usbcore_name, udriver->name); | 
 | 736 |  | 
 | 737 | 	driver_unregister(&udriver->drvwrap.driver); | 
 | 738 | 	usbfs_update_special(); | 
 | 739 | } | 
 | 740 | EXPORT_SYMBOL_GPL(usb_deregister_device_driver); | 
 | 741 |  | 
 | 742 | /** | 
 | 743 |  * usb_register_driver - register a USB interface driver | 
 | 744 |  * @new_driver: USB operations for the interface driver | 
 | 745 |  * @owner: module owner of this driver. | 
 | 746 |  * | 
 | 747 |  * Registers a USB interface driver with the USB core.  The list of | 
 | 748 |  * unattached interfaces will be rescanned whenever a new driver is | 
 | 749 |  * added, allowing the new driver to attach to any recognized interfaces. | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 750 |  * Returns a negative error code on failure and 0 on success. | 
 | 751 |  * | 
 | 752 |  * NOTE: if you want your driver to use the USB major number, you must call | 
 | 753 |  * usb_register_dev() to enable that functionality.  This function no longer | 
 | 754 |  * takes care of that. | 
 | 755 |  */ | 
| Greg Kroah-Hartman | 2143acc | 2005-11-21 14:53:03 -0800 | [diff] [blame] | 756 | int usb_register_driver(struct usb_driver *new_driver, struct module *owner) | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 757 | { | 
 | 758 | 	int retval = 0; | 
 | 759 |  | 
 | 760 | 	if (usb_disabled()) | 
 | 761 | 		return -ENODEV; | 
 | 762 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 763 | 	new_driver->drvwrap.for_devices = 0; | 
 | 764 | 	new_driver->drvwrap.driver.name = (char *) new_driver->name; | 
 | 765 | 	new_driver->drvwrap.driver.bus = &usb_bus_type; | 
 | 766 | 	new_driver->drvwrap.driver.probe = usb_probe_interface; | 
 | 767 | 	new_driver->drvwrap.driver.remove = usb_unbind_interface; | 
 | 768 | 	new_driver->drvwrap.driver.owner = owner; | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 769 | 	spin_lock_init(&new_driver->dynids.lock); | 
 | 770 | 	INIT_LIST_HEAD(&new_driver->dynids.list); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 771 |  | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 772 | 	retval = driver_register(&new_driver->drvwrap.driver); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 773 |  | 
 | 774 | 	if (!retval) { | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 775 | 		pr_info("%s: registered new interface driver %s\n", | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 776 | 			usbcore_name, new_driver->name); | 
 | 777 | 		usbfs_update_special(); | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 778 | 		usb_create_newid_file(new_driver); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 779 | 	} else { | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 780 | 		printk(KERN_ERR "%s: error %d registering interface " | 
 | 781 | 			"	driver %s\n", | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 782 | 			usbcore_name, retval, new_driver->name); | 
 | 783 | 	} | 
 | 784 |  | 
 | 785 | 	return retval; | 
 | 786 | } | 
| Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 788 |  | 
 | 789 | /** | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 790 |  * usb_deregister - unregister a USB interface driver | 
 | 791 |  * @driver: USB operations of the interface driver to unregister | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 792 |  * Context: must be able to sleep | 
 | 793 |  * | 
 | 794 |  * Unlinks the specified driver from the internal USB driver list. | 
 | 795 |  * | 
 | 796 |  * NOTE: If you called usb_register_dev(), you still need to call | 
 | 797 |  * usb_deregister_dev() to clean up your driver's allocated minor numbers, | 
 | 798 |  * this * call will no longer do it for you. | 
 | 799 |  */ | 
 | 800 | void usb_deregister(struct usb_driver *driver) | 
 | 801 | { | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 802 | 	pr_info("%s: deregistering interface driver %s\n", | 
 | 803 | 			usbcore_name, driver->name); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 804 |  | 
| Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 805 | 	usb_remove_newid_file(driver); | 
| Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 806 | 	usb_free_dynids(driver); | 
| Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 807 | 	driver_unregister(&driver->drvwrap.driver); | 
| Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 808 |  | 
 | 809 | 	usbfs_update_special(); | 
 | 810 | } | 
| Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 811 | EXPORT_SYMBOL_GPL_FUTURE(usb_deregister); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 812 |  | 
 | 813 | #ifdef CONFIG_PM | 
 | 814 |  | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 815 | /* Caller has locked udev's pm_mutex */ | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 816 | static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 817 | { | 
| Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 818 | 	struct usb_device_driver	*udriver; | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 819 | 	int				status = 0; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 820 |  | 
| Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 821 | 	if (udev->state == USB_STATE_NOTATTACHED || | 
 | 822 | 			udev->state == USB_STATE_SUSPENDED) | 
 | 823 | 		goto done; | 
 | 824 |  | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 825 | 	/* For devices that don't have a driver, we do a standard suspend. */ | 
 | 826 | 	if (udev->dev.driver == NULL) { | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 827 | 		udev->do_remote_wakeup = 0; | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 828 | 		status = usb_port_suspend(udev); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 829 | 		goto done; | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 830 | 	} | 
 | 831 |  | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 832 | 	udriver = to_usb_device_driver(udev->dev.driver); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 833 | 	status = udriver->suspend(udev, msg); | 
 | 834 |  | 
 | 835 | done: | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 836 | 	// dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 837 | 	if (status == 0) | 
 | 838 | 		udev->dev.power.power_state.event = msg.event; | 
 | 839 | 	return status; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 840 | } | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 841 |  | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 842 | /* Caller has locked udev's pm_mutex */ | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 843 | static int usb_resume_device(struct usb_device *udev) | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 844 | { | 
 | 845 | 	struct usb_device_driver	*udriver; | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 846 | 	int				status = 0; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 847 |  | 
| Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 848 | 	if (udev->state == USB_STATE_NOTATTACHED || | 
 | 849 | 			udev->state != USB_STATE_SUSPENDED) | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 850 | 		goto done; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 851 |  | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 852 | 	/* Can't resume it if it doesn't have a driver. */ | 
 | 853 | 	if (udev->dev.driver == NULL) { | 
 | 854 | 		status = -ENOTCONN; | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 855 | 		goto done; | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 856 | 	} | 
 | 857 |  | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 858 | 	udriver = to_usb_device_driver(udev->dev.driver); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 859 | 	status = udriver->resume(udev); | 
 | 860 |  | 
 | 861 | done: | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 862 | 	// dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 863 | 	if (status == 0) | 
 | 864 | 		udev->dev.power.power_state.event = PM_EVENT_ON; | 
 | 865 | 	return status; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 866 | } | 
 | 867 |  | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 868 | /* Caller has locked intf's usb_device's pm mutex */ | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 869 | static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg) | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 870 | { | 
 | 871 | 	struct usb_driver	*driver; | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 872 | 	int			status = 0; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 873 |  | 
| Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 874 | 	/* with no hardware, USB interfaces only use FREEZE and ON states */ | 
 | 875 | 	if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || | 
 | 876 | 			!is_active(intf)) | 
 | 877 | 		goto done; | 
 | 878 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 879 | 	if (intf->condition == USB_INTERFACE_UNBOUND)	/* This can't happen */ | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 880 | 		goto done; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 881 | 	driver = to_usb_driver(intf->dev.driver); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 882 |  | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 883 | 	if (driver->suspend && driver->resume) { | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 884 | 		status = driver->suspend(intf, msg); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 885 | 		if (status == 0) | 
 | 886 | 			mark_quiesced(intf); | 
 | 887 | 		else if (!interface_to_usbdev(intf)->auto_pm) | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 888 | 			dev_err(&intf->dev, "%s error %d\n", | 
 | 889 | 					"suspend", status); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 890 | 	} else { | 
 | 891 | 		// FIXME else if there's no suspend method, disconnect... | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 892 | 		// Not possible if auto_pm is set... | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 893 | 		dev_warn(&intf->dev, "no suspend for driver %s?\n", | 
 | 894 | 				driver->name); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 895 | 		mark_quiesced(intf); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 896 | 	} | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 897 |  | 
 | 898 | done: | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 899 | 	// dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 900 | 	if (status == 0) | 
 | 901 | 		intf->dev.power.power_state.event = msg.event; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 902 | 	return status; | 
 | 903 | } | 
 | 904 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 905 | /* Caller has locked intf's usb_device's pm_mutex */ | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 906 | static int usb_resume_interface(struct usb_interface *intf) | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 907 | { | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 908 | 	struct usb_driver	*driver; | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 909 | 	int			status = 0; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 910 |  | 
| Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 911 | 	if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || | 
 | 912 | 			is_active(intf)) | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 913 | 		goto done; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 914 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 915 | 	/* Don't let autoresume interfere with unbinding */ | 
 | 916 | 	if (intf->condition == USB_INTERFACE_UNBINDING) | 
 | 917 | 		goto done; | 
 | 918 |  | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 919 | 	/* Can't resume it if it doesn't have a driver. */ | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 920 | 	if (intf->condition == USB_INTERFACE_UNBOUND) { | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 921 | 		status = -ENOTCONN; | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 922 | 		goto done; | 
| Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 923 | 	} | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 924 | 	driver = to_usb_driver(intf->dev.driver); | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 925 |  | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 926 | 	if (driver->resume) { | 
 | 927 | 		status = driver->resume(intf); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 928 | 		if (status) | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 929 | 			dev_err(&intf->dev, "%s error %d\n", | 
 | 930 | 					"resume", status); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 931 | 		else | 
 | 932 | 			mark_active(intf); | 
 | 933 | 	} else { | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 934 | 		dev_warn(&intf->dev, "no resume for driver %s?\n", | 
 | 935 | 				driver->name); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 936 | 		mark_active(intf); | 
 | 937 | 	} | 
 | 938 |  | 
 | 939 | done: | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 940 | 	// dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); | 
| Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 941 | 	if (status == 0) | 
 | 942 | 		intf->dev.power.power_state.event = PM_EVENT_ON; | 
 | 943 | 	return status; | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 944 | } | 
 | 945 |  | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 946 | #ifdef	CONFIG_USB_SUSPEND | 
 | 947 |  | 
| Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 948 | /* Internal routine to check whether we may autosuspend a device. */ | 
 | 949 | static int autosuspend_check(struct usb_device *udev) | 
 | 950 | { | 
 | 951 | 	int			i; | 
 | 952 | 	struct usb_interface	*intf; | 
 | 953 |  | 
 | 954 | 	/* For autosuspend, fail fast if anything is in use. | 
 | 955 | 	 * Also fail if any interfaces require remote wakeup but it | 
 | 956 | 	 * isn't available. */ | 
 | 957 | 	udev->do_remote_wakeup = device_may_wakeup(&udev->dev); | 
 | 958 | 	if (udev->pm_usage_cnt > 0) | 
 | 959 | 		return -EBUSY; | 
 | 960 | 	if (udev->actconfig) { | 
 | 961 | 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { | 
 | 962 | 			intf = udev->actconfig->interface[i]; | 
 | 963 | 			if (!is_active(intf)) | 
 | 964 | 				continue; | 
 | 965 | 			if (intf->pm_usage_cnt > 0) | 
 | 966 | 				return -EBUSY; | 
 | 967 | 			if (intf->needs_remote_wakeup && | 
 | 968 | 					!udev->do_remote_wakeup) { | 
 | 969 | 				dev_dbg(&udev->dev, "remote wakeup needed " | 
 | 970 | 						"for autosuspend\n"); | 
 | 971 | 				return -EOPNOTSUPP; | 
 | 972 | 			} | 
 | 973 | 		} | 
 | 974 | 	} | 
 | 975 | 	return 0; | 
 | 976 | } | 
 | 977 |  | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 978 | #else | 
 | 979 |  | 
 | 980 | #define autosuspend_check(udev)		0 | 
 | 981 |  | 
 | 982 | #endif | 
 | 983 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 984 | /** | 
 | 985 |  * usb_suspend_both - suspend a USB device and its interfaces | 
 | 986 |  * @udev: the usb_device to suspend | 
 | 987 |  * @msg: Power Management message describing this state transition | 
 | 988 |  * | 
 | 989 |  * This is the central routine for suspending USB devices.  It calls the | 
 | 990 |  * suspend methods for all the interface drivers in @udev and then calls | 
 | 991 |  * the suspend method for @udev itself.  If an error occurs at any stage, | 
 | 992 |  * all the interfaces which were suspended are resumed so that they remain | 
 | 993 |  * in the same state as the device. | 
 | 994 |  * | 
 | 995 |  * If an autosuspend is in progress (@udev->auto_pm is set), the routine | 
 | 996 |  * checks first to make sure that neither the device itself or any of its | 
 | 997 |  * active interfaces is in use (pm_usage_cnt is greater than 0).  If they | 
 | 998 |  * are, the autosuspend fails. | 
 | 999 |  * | 
 | 1000 |  * If the suspend succeeds, the routine recursively queues an autosuspend | 
 | 1001 |  * request for @udev's parent device, thereby propagating the change up | 
 | 1002 |  * the device tree.  If all of the parent's children are now suspended, | 
 | 1003 |  * the parent will autosuspend in turn. | 
 | 1004 |  * | 
 | 1005 |  * The suspend method calls are subject to mutual exclusion under control | 
 | 1006 |  * of @udev's pm_mutex.  Many of these calls are also under the protection | 
 | 1007 |  * of @udev's device lock (including all requests originating outside the | 
 | 1008 |  * USB subsystem), but autosuspend requests generated by a child device or | 
 | 1009 |  * interface driver may not be.  Usbcore will insure that the method calls | 
 | 1010 |  * do not arrive during bind, unbind, or reset operations.  However, drivers | 
 | 1011 |  * must be prepared to handle suspend calls arriving at unpredictable times. | 
 | 1012 |  * The only way to block such calls is to do an autoresume (preventing | 
 | 1013 |  * autosuspends) while holding @udev's device lock (preventing outside | 
 | 1014 |  * suspends). | 
 | 1015 |  * | 
 | 1016 |  * The caller must hold @udev->pm_mutex. | 
 | 1017 |  * | 
 | 1018 |  * This routine can run only in process context. | 
 | 1019 |  */ | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1020 | int usb_suspend_both(struct usb_device *udev, pm_message_t msg) | 
 | 1021 | { | 
 | 1022 | 	int			status = 0; | 
 | 1023 | 	int			i = 0; | 
 | 1024 | 	struct usb_interface	*intf; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1025 | 	struct usb_device	*parent = udev->parent; | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1026 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1027 | 	cancel_delayed_work(&udev->autosuspend); | 
 | 1028 | 	if (udev->state == USB_STATE_NOTATTACHED) | 
 | 1029 | 		return 0; | 
 | 1030 | 	if (udev->state == USB_STATE_SUSPENDED) | 
 | 1031 | 		return 0; | 
 | 1032 |  | 
 | 1033 | 	udev->do_remote_wakeup = device_may_wakeup(&udev->dev); | 
 | 1034 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1035 | 	if (udev->auto_pm) { | 
| Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1036 | 		status = autosuspend_check(udev); | 
 | 1037 | 		if (status < 0) | 
 | 1038 | 			return status; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1039 | 	} | 
 | 1040 |  | 
 | 1041 | 	/* Suspend all the interfaces and then udev itself */ | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1042 | 	if (udev->actconfig) { | 
 | 1043 | 		for (; i < udev->actconfig->desc.bNumInterfaces; i++) { | 
 | 1044 | 			intf = udev->actconfig->interface[i]; | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1045 | 			status = usb_suspend_interface(intf, msg); | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1046 | 			if (status != 0) | 
 | 1047 | 				break; | 
 | 1048 | 		} | 
 | 1049 | 	} | 
 | 1050 | 	if (status == 0) | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1051 | 		status = usb_suspend_device(udev, msg); | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1052 |  | 
 | 1053 | 	/* If the suspend failed, resume interfaces that did get suspended */ | 
 | 1054 | 	if (status != 0) { | 
 | 1055 | 		while (--i >= 0) { | 
 | 1056 | 			intf = udev->actconfig->interface[i]; | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1057 | 			usb_resume_interface(intf); | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1058 | 		} | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1059 |  | 
 | 1060 | 	/* If the suspend succeeded, propagate it up the tree */ | 
 | 1061 | 	} else if (parent) | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1062 | 		usb_autosuspend_device(parent); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1063 |  | 
 | 1064 | 	// dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1065 | 	return status; | 
 | 1066 | } | 
 | 1067 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1068 | /** | 
 | 1069 |  * usb_resume_both - resume a USB device and its interfaces | 
 | 1070 |  * @udev: the usb_device to resume | 
 | 1071 |  * | 
 | 1072 |  * This is the central routine for resuming USB devices.  It calls the | 
 | 1073 |  * the resume method for @udev and then calls the resume methods for all | 
 | 1074 |  * the interface drivers in @udev. | 
 | 1075 |  * | 
 | 1076 |  * Before starting the resume, the routine calls itself recursively for | 
 | 1077 |  * the parent device of @udev, thereby propagating the change up the device | 
 | 1078 |  * tree and assuring that @udev will be able to resume.  If the parent is | 
 | 1079 |  * unable to resume successfully, the routine fails. | 
 | 1080 |  * | 
 | 1081 |  * The resume method calls are subject to mutual exclusion under control | 
 | 1082 |  * of @udev's pm_mutex.  Many of these calls are also under the protection | 
 | 1083 |  * of @udev's device lock (including all requests originating outside the | 
 | 1084 |  * USB subsystem), but autoresume requests generated by a child device or | 
 | 1085 |  * interface driver may not be.  Usbcore will insure that the method calls | 
 | 1086 |  * do not arrive during bind, unbind, or reset operations.  However, drivers | 
 | 1087 |  * must be prepared to handle resume calls arriving at unpredictable times. | 
 | 1088 |  * The only way to block such calls is to do an autoresume (preventing | 
 | 1089 |  * other autoresumes) while holding @udev's device lock (preventing outside | 
 | 1090 |  * resumes). | 
 | 1091 |  * | 
 | 1092 |  * The caller must hold @udev->pm_mutex. | 
 | 1093 |  * | 
 | 1094 |  * This routine can run only in process context. | 
 | 1095 |  */ | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1096 | int usb_resume_both(struct usb_device *udev) | 
 | 1097 | { | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1098 | 	int			status = 0; | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1099 | 	int			i; | 
 | 1100 | 	struct usb_interface	*intf; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1101 | 	struct usb_device	*parent = udev->parent; | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1102 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1103 | 	cancel_delayed_work(&udev->autosuspend); | 
 | 1104 | 	if (udev->state == USB_STATE_NOTATTACHED) | 
 | 1105 | 		return -ENODEV; | 
 | 1106 |  | 
 | 1107 | 	/* Propagate the resume up the tree, if necessary */ | 
 | 1108 | 	if (udev->state == USB_STATE_SUSPENDED) { | 
 | 1109 | 		if (parent) { | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1110 | 			status = usb_autoresume_device(parent); | 
| Alan Stern | ee49fb5 | 2006-11-22 16:55:54 -0500 | [diff] [blame] | 1111 | 			if (status == 0) { | 
 | 1112 | 				status = usb_resume_device(udev); | 
 | 1113 | 				if (status) { | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1114 | 					usb_autosuspend_device(parent); | 
| Alan Stern | ee49fb5 | 2006-11-22 16:55:54 -0500 | [diff] [blame] | 1115 |  | 
 | 1116 | 					/* It's possible usb_resume_device() | 
 | 1117 | 					 * failed after the port was | 
 | 1118 | 					 * unsuspended, causing udev to be | 
 | 1119 | 					 * logically disconnected.  We don't | 
 | 1120 | 					 * want usb_disconnect() to autosuspend | 
 | 1121 | 					 * the parent again, so tell it that | 
 | 1122 | 					 * udev disconnected while still | 
 | 1123 | 					 * suspended. */ | 
 | 1124 | 					if (udev->state == | 
 | 1125 | 							USB_STATE_NOTATTACHED) | 
 | 1126 | 						udev->discon_suspended = 1; | 
 | 1127 | 				} | 
 | 1128 | 			} | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1129 | 		} else { | 
 | 1130 |  | 
 | 1131 | 			/* We can't progagate beyond the USB subsystem, | 
 | 1132 | 			 * so if a root hub's controller is suspended | 
 | 1133 | 			 * then we're stuck. */ | 
 | 1134 | 			if (udev->dev.parent->power.power_state.event != | 
 | 1135 | 					PM_EVENT_ON) | 
 | 1136 | 				status = -EHOSTUNREACH; | 
| Alan Stern | ee49fb5 | 2006-11-22 16:55:54 -0500 | [diff] [blame] | 1137 | 			else | 
 | 1138 | 				status = usb_resume_device(udev); | 
 | 1139 |  		} | 
| Alan Stern | 592fbbe | 2006-09-19 10:08:43 -0400 | [diff] [blame] | 1140 | 	} else { | 
 | 1141 |  | 
 | 1142 | 		/* Needed only for setting udev->dev.power.power_state.event | 
 | 1143 | 		 * and for possible debugging message. */ | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1144 | 		status = usb_resume_device(udev); | 
| Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 1145 | 	} | 
 | 1146 |  | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1147 | 	if (status == 0 && udev->actconfig) { | 
 | 1148 | 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { | 
 | 1149 | 			intf = udev->actconfig->interface[i]; | 
| Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1150 | 			usb_resume_interface(intf); | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1151 | 		} | 
 | 1152 | 	} | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1153 |  | 
 | 1154 | 	// dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1155 | 	return status; | 
 | 1156 | } | 
 | 1157 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1158 | #ifdef CONFIG_USB_SUSPEND | 
 | 1159 |  | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1160 | /* Internal routine to adjust a device's usage counter and change | 
 | 1161 |  * its autosuspend state. | 
 | 1162 |  */ | 
 | 1163 | static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt) | 
 | 1164 | { | 
 | 1165 | 	int	status = 0; | 
 | 1166 |  | 
 | 1167 | 	usb_pm_lock(udev); | 
 | 1168 | 	udev->pm_usage_cnt += inc_usage_cnt; | 
 | 1169 | 	WARN_ON(udev->pm_usage_cnt < 0); | 
 | 1170 | 	if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) { | 
 | 1171 | 		udev->auto_pm = 1; | 
 | 1172 | 		status = usb_resume_both(udev); | 
 | 1173 | 		if (status != 0) | 
 | 1174 | 			udev->pm_usage_cnt -= inc_usage_cnt; | 
 | 1175 | 	} else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0) | 
 | 1176 | 		queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, | 
 | 1177 | 				USB_AUTOSUSPEND_DELAY); | 
 | 1178 | 	usb_pm_unlock(udev); | 
 | 1179 | 	return status; | 
 | 1180 | } | 
 | 1181 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1182 | /** | 
 | 1183 |  * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces | 
| Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1184 |  * @udev: the usb_device to autosuspend | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1185 |  * | 
 | 1186 |  * This routine should be called when a core subsystem is finished using | 
 | 1187 |  * @udev and wants to allow it to autosuspend.  Examples would be when | 
 | 1188 |  * @udev's device file in usbfs is closed or after a configuration change. | 
 | 1189 |  * | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1190 |  * @udev's usage counter is decremented.  If it or any of the usage counters | 
 | 1191 |  * for an active interface is greater than 0, no autosuspend request will be | 
 | 1192 |  * queued.  (If an interface driver does not support autosuspend then its | 
 | 1193 |  * usage counter is permanently positive.)  Furthermore, if an interface | 
 | 1194 |  * driver requires remote-wakeup capability during autosuspend but remote | 
 | 1195 |  * wakeup is disabled, the autosuspend will fail. | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1196 |  * | 
 | 1197 |  * Often the caller will hold @udev's device lock, but this is not | 
 | 1198 |  * necessary. | 
 | 1199 |  * | 
 | 1200 |  * This routine can run only in process context. | 
 | 1201 |  */ | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1202 | void usb_autosuspend_device(struct usb_device *udev) | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1203 | { | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1204 | 	int	status; | 
 | 1205 |  | 
 | 1206 | 	status = usb_autopm_do_device(udev, -1); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1207 | 	// dev_dbg(&udev->dev, "%s: cnt %d\n", | 
 | 1208 | 	//		__FUNCTION__, udev->pm_usage_cnt); | 
 | 1209 | } | 
 | 1210 |  | 
 | 1211 | /** | 
 | 1212 |  * usb_autoresume_device - immediately autoresume a USB device and its interfaces | 
| Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1213 |  * @udev: the usb_device to autoresume | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1214 |  * | 
 | 1215 |  * This routine should be called when a core subsystem wants to use @udev | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1216 |  * and needs to guarantee that it is not suspended.  No autosuspend will | 
 | 1217 |  * occur until usb_autosuspend_device is called.  (Note that this will not | 
 | 1218 |  * prevent suspend events originating in the PM core.)  Examples would be | 
 | 1219 |  * when @udev's device file in usbfs is opened or when a remote-wakeup | 
 | 1220 |  * request is received. | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1221 |  * | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1222 |  * @udev's usage counter is incremented to prevent subsequent autosuspends. | 
 | 1223 |  * However if the autoresume fails then the usage counter is re-decremented. | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1224 |  * | 
 | 1225 |  * Often the caller will hold @udev's device lock, but this is not | 
 | 1226 |  * necessary (and attempting it might cause deadlock). | 
 | 1227 |  * | 
 | 1228 |  * This routine can run only in process context. | 
 | 1229 |  */ | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1230 | int usb_autoresume_device(struct usb_device *udev) | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1231 | { | 
 | 1232 | 	int	status; | 
 | 1233 |  | 
| Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1234 | 	status = usb_autopm_do_device(udev, 1); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1235 | 	// dev_dbg(&udev->dev, "%s: status %d cnt %d\n", | 
 | 1236 | 	//		__FUNCTION__, status, udev->pm_usage_cnt); | 
 | 1237 | 	return status; | 
 | 1238 | } | 
 | 1239 |  | 
| Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1240 | /* Internal routine to adjust an interface's usage counter and change | 
 | 1241 |  * its device's autosuspend state. | 
 | 1242 |  */ | 
 | 1243 | static int usb_autopm_do_interface(struct usb_interface *intf, | 
 | 1244 | 		int inc_usage_cnt) | 
 | 1245 | { | 
 | 1246 | 	struct usb_device	*udev = interface_to_usbdev(intf); | 
 | 1247 | 	int			status = 0; | 
 | 1248 |  | 
 | 1249 | 	usb_pm_lock(udev); | 
 | 1250 | 	if (intf->condition == USB_INTERFACE_UNBOUND) | 
 | 1251 | 		status = -ENODEV; | 
 | 1252 | 	else { | 
 | 1253 | 		intf->pm_usage_cnt += inc_usage_cnt; | 
 | 1254 | 		if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) { | 
 | 1255 | 			udev->auto_pm = 1; | 
 | 1256 | 			status = usb_resume_both(udev); | 
 | 1257 | 			if (status != 0) | 
 | 1258 | 				intf->pm_usage_cnt -= inc_usage_cnt; | 
 | 1259 | 		} else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0) | 
 | 1260 | 			queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, | 
 | 1261 | 					USB_AUTOSUSPEND_DELAY); | 
 | 1262 | 	} | 
 | 1263 | 	usb_pm_unlock(udev); | 
 | 1264 | 	return status; | 
 | 1265 | } | 
 | 1266 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1267 | /** | 
 | 1268 |  * usb_autopm_put_interface - decrement a USB interface's PM-usage counter | 
| Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1269 |  * @intf: the usb_interface whose counter should be decremented | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1270 |  * | 
 | 1271 |  * This routine should be called by an interface driver when it is | 
 | 1272 |  * finished using @intf and wants to allow it to autosuspend.  A typical | 
 | 1273 |  * example would be a character-device driver when its device file is | 
 | 1274 |  * closed. | 
 | 1275 |  * | 
 | 1276 |  * The routine decrements @intf's usage counter.  When the counter reaches | 
 | 1277 |  * 0, a delayed autosuspend request for @intf's device is queued.  When | 
 | 1278 |  * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all | 
 | 1279 |  * the other usage counters for the sibling interfaces and @intf's | 
 | 1280 |  * usb_device, the device and all its interfaces will be autosuspended. | 
 | 1281 |  * | 
 | 1282 |  * Note that @intf->pm_usage_cnt is owned by the interface driver.  The | 
 | 1283 |  * core will not change its value other than the increment and decrement | 
 | 1284 |  * in usb_autopm_get_interface and usb_autopm_put_interface.  The driver | 
 | 1285 |  * may use this simple counter-oriented discipline or may set the value | 
 | 1286 |  * any way it likes. | 
 | 1287 |  * | 
 | 1288 |  * If the driver has set @intf->needs_remote_wakeup then autosuspend will | 
 | 1289 |  * take place only if the device's remote-wakeup facility is enabled. | 
 | 1290 |  * | 
 | 1291 |  * Suspend method calls queued by this routine can arrive at any time | 
 | 1292 |  * while @intf is resumed and its usage counter is equal to 0.  They are | 
 | 1293 |  * not protected by the usb_device's lock but only by its pm_mutex. | 
 | 1294 |  * Drivers must provide their own synchronization. | 
 | 1295 |  * | 
 | 1296 |  * This routine can run only in process context. | 
 | 1297 |  */ | 
 | 1298 | void usb_autopm_put_interface(struct usb_interface *intf) | 
 | 1299 | { | 
| Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1300 | 	int	status; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1301 |  | 
| Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1302 | 	status = usb_autopm_do_interface(intf, -1); | 
 | 1303 | 	// dev_dbg(&intf->dev, "%s: status %d cnt %d\n", | 
 | 1304 | 	//		__FUNCTION__, status, intf->pm_usage_cnt); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1305 | } | 
 | 1306 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface); | 
 | 1307 |  | 
 | 1308 | /** | 
 | 1309 |  * usb_autopm_get_interface - increment a USB interface's PM-usage counter | 
| Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1310 |  * @intf: the usb_interface whose counter should be incremented | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1311 |  * | 
 | 1312 |  * This routine should be called by an interface driver when it wants to | 
 | 1313 |  * use @intf and needs to guarantee that it is not suspended.  In addition, | 
 | 1314 |  * the routine prevents @intf from being autosuspended subsequently.  (Note | 
 | 1315 |  * that this will not prevent suspend events originating in the PM core.) | 
 | 1316 |  * This prevention will persist until usb_autopm_put_interface() is called | 
 | 1317 |  * or @intf is unbound.  A typical example would be a character-device | 
 | 1318 |  * driver when its device file is opened. | 
 | 1319 |  * | 
 | 1320 |  * The routine increments @intf's usage counter.  So long as the counter | 
 | 1321 |  * is greater than 0, autosuspend will not be allowed for @intf or its | 
 | 1322 |  * usb_device.  When the driver is finished using @intf it should call | 
 | 1323 |  * usb_autopm_put_interface() to decrement the usage counter and queue | 
 | 1324 |  * a delayed autosuspend request (if the counter is <= 0). | 
 | 1325 |  * | 
 | 1326 |  * Note that @intf->pm_usage_cnt is owned by the interface driver.  The | 
 | 1327 |  * core will not change its value other than the increment and decrement | 
 | 1328 |  * in usb_autopm_get_interface and usb_autopm_put_interface.  The driver | 
 | 1329 |  * may use this simple counter-oriented discipline or may set the value | 
 | 1330 |  * any way it likes. | 
 | 1331 |  * | 
 | 1332 |  * Resume method calls generated by this routine can arrive at any time | 
 | 1333 |  * while @intf is suspended.  They are not protected by the usb_device's | 
 | 1334 |  * lock but only by its pm_mutex.  Drivers must provide their own | 
 | 1335 |  * synchronization. | 
 | 1336 |  * | 
 | 1337 |  * This routine can run only in process context. | 
 | 1338 |  */ | 
 | 1339 | int usb_autopm_get_interface(struct usb_interface *intf) | 
 | 1340 | { | 
| Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1341 | 	int	status; | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1342 |  | 
| Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1343 | 	status = usb_autopm_do_interface(intf, 1); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1344 | 	// dev_dbg(&intf->dev, "%s: status %d cnt %d\n", | 
 | 1345 | 	//		__FUNCTION__, status, intf->pm_usage_cnt); | 
 | 1346 | 	return status; | 
 | 1347 | } | 
 | 1348 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface); | 
 | 1349 |  | 
| Alan Stern | 692a186 | 2006-10-30 17:07:51 -0500 | [diff] [blame] | 1350 | /** | 
 | 1351 |  * usb_autopm_set_interface - set a USB interface's autosuspend state | 
 | 1352 |  * @intf: the usb_interface whose state should be set | 
 | 1353 |  * | 
 | 1354 |  * This routine sets the autosuspend state of @intf's device according | 
 | 1355 |  * to @intf's usage counter, which the caller must have set previously. | 
 | 1356 |  * If the counter is <= 0, the device is autosuspended (if it isn't | 
 | 1357 |  * already suspended and if nothing else prevents the autosuspend).  If | 
 | 1358 |  * the counter is > 0, the device is autoresumed (if it isn't already | 
 | 1359 |  * awake). | 
 | 1360 |  */ | 
 | 1361 | int usb_autopm_set_interface(struct usb_interface *intf) | 
 | 1362 | { | 
 | 1363 | 	int	status; | 
 | 1364 |  | 
 | 1365 | 	status = usb_autopm_do_interface(intf, 0); | 
 | 1366 | 	// dev_dbg(&intf->dev, "%s: status %d cnt %d\n", | 
 | 1367 | 	//		__FUNCTION__, status, intf->pm_usage_cnt); | 
 | 1368 | 	return status; | 
 | 1369 | } | 
 | 1370 | EXPORT_SYMBOL_GPL(usb_autopm_set_interface); | 
 | 1371 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1372 | #endif /* CONFIG_USB_SUSPEND */ | 
 | 1373 |  | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1374 | static int usb_suspend(struct device *dev, pm_message_t message) | 
 | 1375 | { | 
 | 1376 | 	int	status; | 
 | 1377 |  | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1378 | 	if (is_usb_device(dev)) { | 
 | 1379 | 		struct usb_device *udev = to_usb_device(dev); | 
 | 1380 |  | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 1381 | 		usb_pm_lock(udev); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1382 | 		udev->auto_pm = 0; | 
 | 1383 | 		status = usb_suspend_both(udev, message); | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 1384 | 		usb_pm_unlock(udev); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1385 | 	} else | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1386 | 		status = 0; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1387 | 	return status; | 
 | 1388 | } | 
 | 1389 |  | 
 | 1390 | static int usb_resume(struct device *dev) | 
 | 1391 | { | 
 | 1392 | 	int	status; | 
 | 1393 |  | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1394 | 	if (is_usb_device(dev)) { | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1395 | 		struct usb_device *udev = to_usb_device(dev); | 
 | 1396 |  | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 1397 | 		usb_pm_lock(udev); | 
| Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1398 | 		udev->auto_pm = 0; | 
 | 1399 | 		status = usb_resume_both(udev); | 
| Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 1400 | 		usb_pm_unlock(udev); | 
| Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1401 |  | 
 | 1402 | 		/* Rebind drivers that had no suspend method? */ | 
 | 1403 | 	} else | 
 | 1404 | 		status = 0; | 
| Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1405 | 	return status; | 
 | 1406 | } | 
 | 1407 |  | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1408 | #endif /* CONFIG_PM */ | 
 | 1409 |  | 
 | 1410 | struct bus_type usb_bus_type = { | 
 | 1411 | 	.name =		"usb", | 
 | 1412 | 	.match =	usb_device_match, | 
 | 1413 | 	.uevent =	usb_uevent, | 
 | 1414 | #ifdef CONFIG_PM | 
| Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1415 | 	.suspend =	usb_suspend, | 
 | 1416 | 	.resume =	usb_resume, | 
| Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1417 | #endif | 
 | 1418 | }; |