usb.c 43.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4
/*
 * drivers/usb/usb.c
 *
 * (C) Copyright Linus Torvalds 1999
Linus Torvalds's avatar
Linus Torvalds committed
5
 * (C) Copyright Johannes Erdfelt 1999-2001
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9
 * (C) Copyright Andreas Gal 1999
 * (C) Copyright Gregory P. Smith 1999
 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
 * (C) Copyright Randy Dunlap 2000
Linus Torvalds's avatar
Linus Torvalds committed
10 11
 * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
 	more docs, etc)
Linus Torvalds's avatar
Linus Torvalds committed
12 13
 * (C) Copyright Yggdrasil Computing, Inc. 2000
 *     (usb_device_id matching changes by Adam J. Richter)
14
 * (C) Copyright Greg Kroah-Hartman 2002-2003
Linus Torvalds's avatar
Linus Torvalds committed
15 16 17 18 19 20 21 22 23 24 25
 *
 * NOTE! This is not actually a driver at all, rather this is
 * just a collection of helper routines that implement the
 * generic USB things that the real drivers can use..
 *
 * Think of this as a "USB library" rather than anything else.
 * It should be considered a slave, with no callbacks. Callbacks
 * are evil.
 */

#include <linux/config.h>
26 27 28 29 30 31 32

#ifdef CONFIG_USB_DEBUG
	#define DEBUG
#else
	#undef DEBUG
#endif

Linus Torvalds's avatar
Linus Torvalds committed
33 34 35
#include <linux/module.h>
#include <linux/string.h>
#include <linux/bitops.h>
Linus Torvalds's avatar
Linus Torvalds committed
36
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39
#include <linux/interrupt.h>  /* for in_interrupt() */
#include <linux/kmod.h>
#include <linux/init.h>
Linus Torvalds's avatar
Linus Torvalds committed
40
#include <linux/spinlock.h>
41
#include <linux/errno.h>
42
#include <linux/smp_lock.h>
Linus Torvalds's avatar
Linus Torvalds committed
43 44
#include <linux/usb.h>

45 46 47 48 49
#include <asm/io.h>
#include <asm/scatterlist.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>

50
#include "hcd.h"
51
#include "usb.h"
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54

extern int  usb_hub_init(void);
extern void usb_hub_cleanup(void);
55 56
extern int usb_major_init(void);
extern void usb_major_cleanup(void);
57 58
extern int usb_host_init(void);
extern void usb_host_cleanup(void);
Linus Torvalds's avatar
Linus Torvalds committed
59 60


61 62 63
int nousb;		/* Disable USB when built into kernel image */
			/* Not honored on modular build */

Linus Torvalds's avatar
Linus Torvalds committed
64

65
static int generic_probe (struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
66
{
67 68 69 70 71 72
	return 0;
}
static int generic_remove (struct device *dev)
{
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
73

74
static struct device_driver usb_generic_driver = {
75
	.name =	"usb",
76
	.bus = &usb_bus_type,
77 78 79
	.probe = generic_probe,
	.remove = generic_remove,
};
80

81 82
static int usb_generic_driver_data;

83
/* needs to be called with BKL held */
84
int usb_probe_interface(struct device *dev)
85 86 87 88 89
{
	struct usb_interface * intf = to_usb_interface(dev);
	struct usb_driver * driver = to_usb_driver(dev->driver);
	const struct usb_device_id *id;
	int error = -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
90

91
	dev_dbg(dev, "%s\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
92

93 94
	if (!driver->probe)
		return error;
95

96 97
	id = usb_match_id (intf, driver->id_table);
	if (id) {
98
		dev_dbg (dev, "%s - got id\n", __FUNCTION__);
99 100 101 102 103 104
		down (&driver->serialize);
		error = driver->probe (intf, id);
		up (&driver->serialize);
	}
	if (!error)
		intf->driver = driver;
Linus Torvalds's avatar
Linus Torvalds committed
105

106
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
107 108
}

109
int usb_unbind_interface(struct device *dev)
110
{
111 112
	struct usb_interface *intf = to_usb_interface(dev);
	struct usb_driver *driver = to_usb_driver(dev->driver);
113 114 115

	down(&driver->serialize);

116 117
	/* release all urbs for this interface */
	usb_disable_interface(interface_to_usbdev(intf), intf);
118

119 120 121
	if (intf->driver && intf->driver->disconnect)
		intf->driver->disconnect(intf);

122 123
	/* force a release and re-initialize the interface */
	usb_driver_release_interface(driver, intf);
124 125

	up(&driver->serialize);
126 127

	return 0;
128 129 130
}

/**
131 132
 * usb_register - register a USB driver
 * @new_driver: USB operations for the driver
133
 *
134 135 136 137 138 139 140 141
 * Registers a USB driver with the USB core.  The list of unattached
 * interfaces will be rescanned whenever a new driver is added, allowing
 * the new driver to attach to any recognized devices.
 * Returns a negative error code on failure and 0 on success.
 * 
 * NOTE: if you want your driver to use the USB major number, you must call
 * usb_register_dev() to enable that functionality.  This function no longer
 * takes care of that.
142
 */
143
int usb_register(struct usb_driver *new_driver)
144
{
145
	int retval = 0;
146

147 148 149
	if (nousb)
		return -ENODEV;

150 151
	new_driver->driver.name = (char *)new_driver->name;
	new_driver->driver.bus = &usb_bus_type;
152 153
	new_driver->driver.probe = usb_probe_interface;
	new_driver->driver.remove = usb_unbind_interface;
154

155
	init_MUTEX(&new_driver->serialize);
156

157
	retval = driver_register(&new_driver->driver);
Linus Torvalds's avatar
Linus Torvalds committed
158

159 160 161 162 163 164
	if (!retval) {
		info("registered new driver %s", new_driver->name);
		usbfs_update_special();
	} else {
		err("problem %d when registering driver %s",
			retval, new_driver->name);
Linus Torvalds's avatar
Linus Torvalds committed
165 166
	}

167
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170
}

/**
171 172
 * usb_deregister - unregister a USB driver
 * @driver: USB operations of the driver to unregister
173
 * Context: !in_interrupt (), must be called with BKL held
Linus Torvalds's avatar
Linus Torvalds committed
174
 *
175 176 177 178 179
 * Unlinks the specified driver from the internal USB driver list.
 * 
 * NOTE: If you called usb_register_dev(), you still need to call
 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
 * this * call will no longer do it for you.
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183
 */
void usb_deregister(struct usb_driver *driver)
{
	info("deregistering driver %s", driver->name);
184

185
	driver_unregister (&driver->driver);
Linus Torvalds's avatar
Linus Torvalds committed
186 187

	usbfs_update_special();
Linus Torvalds's avatar
Linus Torvalds committed
188 189
}

Greg Kroah-Hartman's avatar
USB  
Greg Kroah-Hartman committed
190
/**
David Brownell's avatar
David Brownell committed
191
 * usb_ifnum_to_if - get the interface object with a given interface number (usbcore-internal)
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205
 * @dev: the device whose current configuration is considered
 * @ifnum: the desired interface
 *
 * This walks the device descriptor for the currently active configuration
 * and returns a pointer to the interface with that particular interface
 * number, or null.
 *
 * Note that configuration descriptors are not required to assign interface
 * numbers sequentially, so that it would be incorrect to assume that
 * the first interface in that descriptor corresponds to interface zero.
 * This routine helps device drivers avoid such mistakes.
 * However, you should make sure that you do the right thing with any
 * alternate settings available for this interfaces.
 */
Linus Torvalds's avatar
Linus Torvalds committed
206 207 208 209
struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
{
	int i;

210
	for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
211
		if (dev->actconfig->interface[i]->altsetting[0]
212
				.desc.bInterfaceNumber == ifnum)
213
			return dev->actconfig->interface[i];
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217

	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
218 219
/**
 * usb_epnum_to_ep_desc - get the endpoint object with a given endpoint number
220 221
 * @dev: the device whose current configuration+altsettings is considered
 * @epnum: the desired endpoint, masked with USB_DIR_IN as appropriate.
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225 226
 *
 * This walks the device descriptor for the currently active configuration,
 * and returns a pointer to the endpoint with that particular endpoint
 * number, or null.
 *
227 228 229
 * Note that interface descriptors are not required to list endpoint
 * numbers in any standardized order, so that it would be wrong to
 * assume that ep2in precedes either ep5in, ep2out, or even ep1out.
Linus Torvalds's avatar
Linus Torvalds committed
230 231
 * This routine helps device drivers avoid such mistakes.
 */
232 233
struct usb_endpoint_descriptor *
usb_epnum_to_ep_desc(struct usb_device *dev, unsigned epnum)
Linus Torvalds's avatar
Linus Torvalds committed
234
{
235
	int i, k;
Linus Torvalds's avatar
Linus Torvalds committed
236

237 238 239 240 241 242 243 244 245 246 247 248
	for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
		struct usb_interface		*intf;
		struct usb_host_interface	*alt;

		/* only endpoints in current altseting are active */
		intf = dev->actconfig->interface[i];
		alt = intf->altsetting + intf->act_altsetting;

		for (k = 0; k < alt->desc.bNumEndpoints; k++)
			if (epnum == alt->endpoint[k].desc.bEndpointAddress)
				return &alt->endpoint[k].desc;
	}
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251 252

	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
253 254 255 256 257 258 259 260 261 262 263 264 265
/**
 * usb_driver_claim_interface - bind a driver to an interface
 * @driver: the driver to be bound
 * @iface: the interface to which it will be bound
 * @priv: driver data associated with that interface
 *
 * This is used by usb device drivers that need to claim more than one
 * interface on a device when probing (audio and acm are current examples).
 * No device driver should directly modify internal usb_interface or
 * usb_device structure members.
 *
 * Few drivers should need to use this routine, since the most natural
 * way to bind to an interface is to return the private data from
266
 * the driver's probe() method.
Linus Torvalds's avatar
Linus Torvalds committed
267
 */
268
int usb_driver_claim_interface(struct usb_driver *driver, struct usb_interface *iface, void* priv)
Linus Torvalds's avatar
Linus Torvalds committed
269 270
{
	if (!iface || !driver)
271 272
		return -EINVAL;

273
	/* this is mainly to lock against usbfs */
274 275 276 277 278 279 280
	lock_kernel();
	if (iface->driver) {
		unlock_kernel();
		err ("%s driver booted %s off interface %p",
			driver->name, iface->driver->name, iface);
		return -EBUSY;
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
281
	    dbg("%s driver claimed interface %p", driver->name, iface);
282
	}
Linus Torvalds's avatar
Linus Torvalds committed
283 284

	iface->driver = driver;
285
	usb_set_intfdata(iface, priv);
286 287
	unlock_kernel();
	return 0;
288
}
Linus Torvalds's avatar
Linus Torvalds committed
289

Linus Torvalds's avatar
Linus Torvalds committed
290 291 292 293
/**
 * usb_interface_claimed - returns true iff an interface is claimed
 * @iface: the interface being checked
 *
Linus Torvalds's avatar
Linus Torvalds committed
294
 * This should be used by drivers to check other interfaces to see if
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297 298 299
 * they are available or not.  If another driver has claimed the interface,
 * they may not claim it.  Otherwise it's OK to claim it using
 * usb_driver_claim_interface().
 *
 * Returns true (nonzero) iff the interface is claimed, else false (zero).
Linus Torvalds's avatar
Linus Torvalds committed
300 301 302 303 304 305 306 307 308
 */
int usb_interface_claimed(struct usb_interface *iface)
{
	if (!iface)
		return 0;

	return (iface->driver != NULL);
} /* usb_interface_claimed() */

Linus Torvalds's avatar
Linus Torvalds committed
309 310 311 312
/**
 * usb_driver_release_interface - unbind a driver from an interface
 * @driver: the driver to be unbound
 * @iface: the interface from which it will be unbound
313 314 315
 *
 * In addition to unbinding the driver, this re-initializes the interface
 * by selecting altsetting 0, the default alternate setting.
Linus Torvalds's avatar
Linus Torvalds committed
316
 * 
317 318
 * This can be used by drivers to release an interface without waiting
 * for their disconnect() methods to be called.
Linus Torvalds's avatar
Linus Torvalds committed
319 320 321 322 323
 *
 * When the USB subsystem disconnect()s a driver from some interface,
 * it automatically invokes this method for that interface.  That
 * means that even drivers that used usb_driver_claim_interface()
 * usually won't need to call this.
324 325
 *
 * This call is synchronous, and may not be used in an interrupt context.
Linus Torvalds's avatar
Linus Torvalds committed
326 327 328 329
 */
void usb_driver_release_interface(struct usb_driver *driver, struct usb_interface *iface)
{
	/* this should never happen, don't release something that's not ours */
330
	if (iface->driver && iface->driver != driver)
Linus Torvalds's avatar
Linus Torvalds committed
331 332
		return;

333 334 335
	usb_set_interface(interface_to_usbdev(iface),
			iface->altsetting[0].desc.bInterfaceNumber,
			0);
336 337
	usb_set_intfdata(iface, NULL);
	iface->driver = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
338 339
}

Linus Torvalds's avatar
Linus Torvalds committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
/**
 * usb_match_id - find first usb_device_id matching device or interface
 * @interface: the interface of interest
 * @id: array of usb_device_id structures, terminated by zero entry
 *
 * usb_match_id searches an array of usb_device_id's and returns
 * the first one matching the device or interface, or null.
 * This is used when binding (or rebinding) a driver to an interface.
 * Most USB device drivers will use this indirectly, through the usb core,
 * but some layered driver frameworks use it directly.
 * These device tables are exported with MODULE_DEVICE_TABLE, through
 * modutils and "modules.usbmap", to support the driver loading
 * functionality of USB hotplugging.
 *
 * What Matches:
 *
 * The "match_flags" element in a usb_device_id controls which
 * members are used.  If the corresponding bit is set, the
 * value in the device_id must match its corresponding member
 * in the device or interface descriptor, or else the device_id
 * does not match.
 *
 * "driver_info" is normally used only by device drivers,
 * but you can create a wildcard "matches anything" usb_device_id
 * as a driver's "modules.usbmap" entry if you provide an id with
 * only a nonzero "driver_info" field.  If you do this, the USB device
 * driver's probe() routine should use additional intelligence to
 * decide whether to bind to the specified interface.
 * 
 * What Makes Good usb_device_id Tables:
 *
 * The match algorithm is very simple, so that intelligence in
 * driver selection must come from smart driver id records.
 * Unless you have good reasons to use another selection policy,
 * provide match elements only in related groups, and order match
 * specifiers from specific to general.  Use the macros provided
 * for that purpose if you can.
 *
 * The most specific match specifiers use device descriptor
 * data.  These are commonly used with product-specific matches;
 * the USB_DEVICE macro lets you provide vendor and product IDs,
Linus Torvalds's avatar
Linus Torvalds committed
381
 * and you can also match against ranges of product revisions.
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
 * These are widely used for devices with application or vendor
 * specific bDeviceClass values.
 *
 * Matches based on device class/subclass/protocol specifications
 * are slightly more general; use the USB_DEVICE_INFO macro, or
 * its siblings.  These are used with single-function devices
 * where bDeviceClass doesn't specify that each interface has
 * its own class. 
 *
 * Matches based on interface class/subclass/protocol are the
 * most general; they let drivers bind to any interface on a
 * multiple-function device.  Use the USB_INTERFACE_INFO
 * macro, or its siblings, to match class-per-interface style 
 * devices (as recorded in bDeviceClass).
 *  
 * Within those groups, remember that not all combinations are
 * meaningful.  For example, don't give a product version range
 * without vendor and product IDs; or specify a protocol without
 * its associated class and subclass.
 */   
Linus Torvalds's avatar
Linus Torvalds committed
402
const struct usb_device_id *
403
usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
Linus Torvalds's avatar
Linus Torvalds committed
404
{
405
	struct usb_host_interface *intf;
406
	struct usb_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
407 408 409 410 411

	/* proc_connectinfo in devio.c may call us with id == NULL. */
	if (id == NULL)
		return NULL;

412 413 414
	intf = &interface->altsetting [interface->act_altsetting];
	dev = interface_to_usbdev(interface);

Linus Torvalds's avatar
Linus Torvalds committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	/* It is important to check that id->driver_info is nonzero,
	   since an entry that is all zeroes except for a nonzero
	   id->driver_info is the way to create an entry that
	   indicates that the driver want to examine every
	   device and interface. */
	for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
	       id->driver_info; id++) {

		if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
		    id->idVendor != dev->descriptor.idVendor)
			continue;

		if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
		    id->idProduct != dev->descriptor.idProduct)
			continue;

		/* No need to test id->bcdDevice_lo != 0, since 0 is never
		   greater than any unsigned number. */
		if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
		    (id->bcdDevice_lo > dev->descriptor.bcdDevice))
			continue;

		if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
		    (id->bcdDevice_hi < dev->descriptor.bcdDevice))
			continue;

		if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
		    (id->bDeviceClass != dev->descriptor.bDeviceClass))
			continue;

		if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
		    (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
			continue;

		if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
		    (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
			continue;

		if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
454
		    (id->bInterfaceClass != intf->desc.bInterfaceClass))
Linus Torvalds's avatar
Linus Torvalds committed
455 456 457
			continue;

		if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
458
		    (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
459
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
460 461

		if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
462
		    (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
463
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466 467 468 469 470

		return id;
	}

	return NULL;
}

471 472 473
/**
 * usb_find_interface - find usb_interface pointer for driver and device
 * @drv: the driver whose current configuration is considered
474
 * @minor: the minor number of the desired device
475 476
 *
 * This walks the driver device list and returns a pointer to the interface 
477 478
 * with the matching minor.  Note, this only works for devices that share the
 * USB major number.
479
 */
480
struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
481 482 483 484 485 486 487 488 489 490 491 492 493
{
	struct list_head *entry;
	struct device *dev;
	struct usb_interface *intf;

	list_for_each(entry, &drv->driver.devices) {
		dev = container_of(entry, struct device, driver_list);

		/* can't look at usb devices, only interfaces */
		if (dev->driver == &usb_generic_driver)
			continue;

		intf = to_usb_interface(dev);
494 495 496
		if (intf->minor == -1)
			continue;
		if (intf->minor == minor)
497 498 499 500 501 502 503
			return intf;
	}

	/* no device found that matches */
	return NULL;	
}

504
static int usb_device_match (struct device *dev, struct device_driver *drv)
Linus Torvalds's avatar
Linus Torvalds committed
505
{
506 507 508
	struct usb_interface *intf;
	struct usb_driver *usb_drv;
	const struct usb_device_id *id;
Linus Torvalds's avatar
Linus Torvalds committed
509

510 511 512 513
	/* check for generic driver, which we don't match any device with */
	if (drv == &usb_generic_driver)
		return 0;

514
	intf = to_usb_interface(dev);
Linus Torvalds's avatar
Linus Torvalds committed
515

516 517 518 519 520 521
	usb_drv = to_usb_driver(drv);
	id = usb_drv->id_table;
	
	id = usb_match_id (intf, usb_drv->id_table);
	if (id)
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
522

523
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
524 525
}

526

Linus Torvalds's avatar
Linus Torvalds committed
527 528 529 530 531 532 533 534 535 536
#ifdef	CONFIG_HOTPLUG

/*
 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
 * (normally /sbin/hotplug) when USB devices get added or removed.
 *
 * This invokes a user mode policy agent, typically helping to load driver
 * or other modules, configure the device, and more.  Drivers can provide
 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
 *
537 538 539 540
 * We're called either from khubd (the typical case) or from root hub
 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
 * delays in event delivery.  Use sysfs (and DEVPATH) to make sure the
 * device (and this configuration!) are still present.
Linus Torvalds's avatar
Linus Torvalds committed
541
 */
542 543
static int usb_hotplug (struct device *dev, char **envp, int num_envp,
			char *buffer, int buffer_size)
Linus Torvalds's avatar
Linus Torvalds committed
544
{
545 546 547 548 549
	struct usb_interface *intf;
	struct usb_device *usb_dev;
	char *scratch;
	int i = 0;
	int length = 0;
Linus Torvalds's avatar
Linus Torvalds committed
550

551
	dbg ("%s", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
552

553 554
	if (!dev)
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
555

556 557 558
	/* Must check driver_data here, as on remove driver is always NULL */
	if ((dev->driver == &usb_generic_driver) || 
	    (dev->driver_data == &usb_generic_driver_data))
559
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
560

561 562 563 564 565 566 567 568 569 570 571 572 573
	intf = to_usb_interface(dev);
	usb_dev = interface_to_usbdev (intf);
	
	if (usb_dev->devnum < 0) {
		dbg ("device already deleted ??");
		return -ENODEV;
	}
	if (!usb_dev->bus) {
		dbg ("bus already removed?");
		return -ENODEV;
	}

	scratch = buffer;
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576 577 578 579 580 581

#ifdef	CONFIG_USB_DEVICEFS
	/* If this is available, userspace programs can directly read
	 * all the device descriptors we don't tell them about.  Or
	 * even act as usermode drivers.
	 *
	 * FIXME reduce hardwired intelligence here
	 */
582 583 584 585 586 587 588 589
	envp [i++] = scratch;
	length += snprintf (scratch, buffer_size - length,
			    "DEVICE=/proc/bus/usb/%03d/%03d",
			    usb_dev->bus->busnum, usb_dev->devnum);
	if ((buffer_size - length <= 0) || (i >= num_envp))
		return -ENOMEM;
	++length;
	scratch += length;
Linus Torvalds's avatar
Linus Torvalds committed
590 591
#endif

592
	/* per-device configurations are common */
Linus Torvalds's avatar
Linus Torvalds committed
593
	envp [i++] = scratch;
594 595 596 597 598 599 600 601
	length += snprintf (scratch, buffer_size - length, "PRODUCT=%x/%x/%x",
			    usb_dev->descriptor.idVendor,
			    usb_dev->descriptor.idProduct,
			    usb_dev->descriptor.bcdDevice);
	if ((buffer_size - length <= 0) || (i >= num_envp))
		return -ENOMEM;
	++length;
	scratch += length;
Linus Torvalds's avatar
Linus Torvalds committed
602 603 604

	/* class-based driver binding models */
	envp [i++] = scratch;
605 606 607 608 609 610 611 612 613 614 615
	length += snprintf (scratch, buffer_size - length, "TYPE=%d/%d/%d",
			    usb_dev->descriptor.bDeviceClass,
			    usb_dev->descriptor.bDeviceSubClass,
			    usb_dev->descriptor.bDeviceProtocol);
	if ((buffer_size - length <= 0) || (i >= num_envp))
		return -ENOMEM;
	++length;
	scratch += length;

	if (usb_dev->descriptor.bDeviceClass == 0) {
		int alt = intf->act_altsetting;
Linus Torvalds's avatar
Linus Torvalds committed
616

617 618 619
		/* 2.4 only exposed interface zero.  in 2.5, hotplug
		 * agents are called for all interfaces, and can use
		 * $DEVPATH/bInterfaceNumber if necessary.
Linus Torvalds's avatar
Linus Torvalds committed
620 621
		 */
		envp [i++] = scratch;
622
		length += snprintf (scratch, buffer_size - length,
623 624 625 626
			    "INTERFACE=%d/%d/%d",
			    intf->altsetting[alt].desc.bInterfaceClass,
			    intf->altsetting[alt].desc.bInterfaceSubClass,
			    intf->altsetting[alt].desc.bInterfaceProtocol);
627 628 629 630 631
		if ((buffer_size - length <= 0) || (i >= num_envp))
			return -ENOMEM;
		++length;
		scratch += length;

Linus Torvalds's avatar
Linus Torvalds committed
632 633 634
	}
	envp [i++] = 0;

635
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
636 637 638 639
}

#else

640
static int usb_hotplug (struct device *dev, char **envp,
641
			int num_envp, char *buffer, int buffer_size)
642 643 644
{
	return -ENODEV;
}
Linus Torvalds's avatar
Linus Torvalds committed
645

Linus Torvalds's avatar
Linus Torvalds committed
646
#endif	/* CONFIG_HOTPLUG */
Linus Torvalds's avatar
Linus Torvalds committed
647

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
/**
 * usb_release_dev - free a usb device structure when all users of it are finished.
 * @dev: device that's been disconnected
 *
 * Will be called only by the device core when all users of this usb device are
 * done.
 */
static void usb_release_dev(struct device *dev)
{
	struct usb_device *udev;

	udev = to_usb_device(dev);

	if (udev->bus && udev->bus->op && udev->bus->op->deallocate)
		udev->bus->op->deallocate(udev);
	usb_destroy_configuration(udev);
	usb_bus_put(udev->bus);
	kfree (udev);
}

Linus Torvalds's avatar
Linus Torvalds committed
668 669 670 671
/**
 * usb_alloc_dev - allocate a usb device structure (usbcore-internal)
 * @parent: hub to which device is connected
 * @bus: bus used to access the device
672
 * Context: !in_interrupt ()
Linus Torvalds's avatar
Linus Torvalds committed
673 674 675 676 677
 *
 * Only hub drivers (including virtual root hub drivers for host
 * controllers) should ever call this.
 *
 * This call is synchronous, and may not be used in an interrupt context.
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680 681 682 683 684 685 686 687 688
 */
struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus)
{
	struct usb_device *dev;

	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return NULL;

	memset(dev, 0, sizeof(*dev));

689 690 691 692 693 694
	bus = usb_bus_get(bus);
	if (!bus) {
		kfree(dev);
		return NULL;
	}

695
	device_initialize(&dev->dev);
696
	dev->dev.release = usb_release_dev;
697
	dev->state = USB_STATE_ATTACHED;
698

Linus Torvalds's avatar
Linus Torvalds committed
699
	if (!parent)
David Brownell's avatar
David Brownell committed
700
		dev->devpath [0] = '0';
Linus Torvalds's avatar
Linus Torvalds committed
701 702 703 704
	dev->bus = bus;
	dev->parent = parent;
	INIT_LIST_HEAD(&dev->filelist);

Linus Torvalds's avatar
Linus Torvalds committed
705 706
	init_MUTEX(&dev->serialize);

707 708
	if (dev->bus->op->allocate)
		dev->bus->op->allocate(dev);
Linus Torvalds's avatar
Linus Torvalds committed
709 710 711 712

	return dev;
}

713
/**
714
 * usb_get_dev - increments the reference count of the usb device structure
715 716 717 718 719 720 721 722 723 724 725 726
 * @dev: the device being referenced
 *
 * Each live reference to a device should be refcounted.
 *
 * Drivers for USB interfaces should normally record such references in
 * their probe() methods, when they bind to an interface, and release
 * them by calling usb_put_dev(), in their disconnect() methods.
 *
 * A pointer to the device with the incremented reference counter is returned.
 */
struct usb_device *usb_get_dev (struct usb_device *dev)
{
727 728 729 730 731 732 733 734 735 736
	struct device *tmp;

	if (!dev)
		return NULL;

	tmp = get_device(&dev->dev);
	if (tmp)        
		return to_usb_device(tmp);
	else
		return NULL;
737 738 739
}

/**
740
 * usb_put_dev - release a use of the usb device structure
741 742
 * @dev: device that's been disconnected
 *
743 744
 * Must be called when a user of a device is finished with it.  When the last
 * user of the device calls this function, the memory of the device is freed.
745
 */
746
void usb_put_dev(struct usb_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
747
{
748 749 750 751
	if (dev)
		put_device(&dev->dev);
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
static struct usb_device *match_device(struct usb_device *dev,
				       u16 vendor_id, u16 product_id)
{
	struct usb_device *ret_dev = NULL;
	int child;

	dbg("looking at vendor %d, product %d",
	    dev->descriptor.idVendor,
	    dev->descriptor.idProduct);

	/* see if this device matches */
	if ((dev->descriptor.idVendor == vendor_id) &&
	    (dev->descriptor.idProduct == product_id)) {
		dbg ("found the device!");
		ret_dev = usb_get_dev(dev);
		goto exit;
	}

	/* look through all of the children of this device */
	for (child = 0; child < dev->maxchild; ++child) {
		if (dev->children[child]) {
			ret_dev = match_device(dev->children[child],
					       vendor_id, product_id);
			if (ret_dev)
				goto exit;
		}
	}
exit:
	return ret_dev;
}

/**
 * usb_find_device - find a specific usb device in the system
 * @vendor_id: the vendor id of the device to find
 * @product_id: the product id of the device to find
 *
 * Returns a pointer to a struct usb_device if such a specified usb
 * device is present in the system currently.  The usage count of the
 * device will be incremented if a device is found.  Make sure to call
 * usb_put_dev() when the caller is finished with the device.
 *
 * If a device with the specified vendor and product id is not found,
 * NULL is returned.
 */
struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
{
	struct list_head *buslist;
	struct usb_bus *bus;
	struct usb_device *dev = NULL;
	
	down(&usb_bus_list_lock);
	for (buslist = usb_bus_list.next;
	     buslist != &usb_bus_list; 
	     buslist = buslist->next) {
		bus = container_of(buslist, struct usb_bus, bus_list);
		dev = match_device(bus->root_hub, vendor_id, product_id);
		if (dev)
			goto exit;
	}
exit:
	up(&usb_bus_list_lock);
	return dev;
}

Linus Torvalds's avatar
Linus Torvalds committed
816 817 818
/**
 * usb_get_current_frame_number - return current bus frame number
 * @dev: the device whose bus is being queried
Linus Torvalds's avatar
Linus Torvalds committed
819
 *
Linus Torvalds's avatar
Linus Torvalds committed
820 821 822
 * Returns the current frame number for the USB host controller
 * used with the given USB device.  This can be used when scheduling
 * isochronous requests.
823 824 825 826 827
 *
 * Note that different kinds of host controller have different
 * "scheduling horizons".  While one type might support scheduling only
 * 32 frames into the future, others could support scheduling up to
 * 1024 frames into the future.
Linus Torvalds's avatar
Linus Torvalds committed
828
 */
Linus Torvalds's avatar
Linus Torvalds committed
829
int usb_get_current_frame_number(struct usb_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
830
{
Linus Torvalds's avatar
Linus Torvalds committed
831
	return dev->bus->op->get_frame_number (dev);
Linus Torvalds's avatar
Linus Torvalds committed
832
}
Linus Torvalds's avatar
Linus Torvalds committed
833

Linus Torvalds's avatar
Linus Torvalds committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
/*-------------------------------------------------------------------*/
/*
 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
 * extra field of the interface and endpoint descriptor structs.
 */

int __usb_get_extra_descriptor(char *buffer, unsigned size, unsigned char type, void **ptr)
{
	struct usb_descriptor_header *header;

	while (size >= sizeof(struct usb_descriptor_header)) {
		header = (struct usb_descriptor_header *)buffer;

		if (header->bLength < 2) {
			err("invalid descriptor length of %d", header->bLength);
			return -1;
		}

		if (header->bDescriptorType == type) {
			*ptr = header;
			return 0;
		}

		buffer += header->bLength;
		size -= header->bLength;
	}
	return -1;
}

Linus Torvalds's avatar
Linus Torvalds committed
863 864 865
/**
 * usb_disconnect - disconnect a device (usbcore-internal)
 * @pdev: pointer to device being disconnected
866
 * Context: !in_interrupt ()
Linus Torvalds's avatar
Linus Torvalds committed
867
 *
Linus Torvalds's avatar
Linus Torvalds committed
868
 * Something got disconnected. Get rid of it, and all of its children.
Linus Torvalds's avatar
Linus Torvalds committed
869 870 871 872 873
 *
 * Only hub drivers (including virtual root hub drivers for host
 * controllers) should ever call this.
 *
 * This call is synchronous, and may not be used in an interrupt context.
Linus Torvalds's avatar
Linus Torvalds committed
874 875 876
 */
void usb_disconnect(struct usb_device **pdev)
{
877
	struct usb_device	*dev = *pdev;
878 879
	struct usb_bus		*bus;
	struct usb_operations	*ops;
880 881 882
	int			i;

	might_sleep ();
Linus Torvalds's avatar
Linus Torvalds committed
883

884 885 886 887 888 889 890
	if (!dev) {
		pr_debug ("%s nodev\n", __FUNCTION__);
		return;
	}
	bus = dev->bus;
	if (!bus) {
		pr_debug ("%s nobus\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
891
		return;
892 893
	}
	ops = bus->op;
Linus Torvalds's avatar
Linus Torvalds committed
894 895 896

	*pdev = NULL;

897 898 899 900
	/* mark the device as inactive, so any further urb submissions for
	 * this device will fail.
	 */
	dev->state = USB_STATE_NOTATTACHED;
901
	down(&dev->serialize);
902

903
	dev_info (&dev->dev, "USB disconnect, address %d\n", dev->devnum);
Linus Torvalds's avatar
Linus Torvalds committed
904

905
	/* Free up all the children before we remove this device */
Linus Torvalds's avatar
Linus Torvalds committed
906 907 908 909 910 911
	for (i = 0; i < USB_MAXCHILDREN; i++) {
		struct usb_device **child = dev->children + i;
		if (*child)
			usb_disconnect(child);
	}

912 913 914
	/* deallocate hcd/hardware state ... nuking all pending urbs and
	 * cleaning up all state associated with the current configuration
	 */
915
	usb_disable_device(dev, 0);
916

917
	dev_dbg (&dev->dev, "unregistering device\n");
Linus Torvalds's avatar
Linus Torvalds committed
918 919
	/* Free the device number and remove the /proc/bus/usb entry */
	if (dev->devnum > 0) {
920
		clear_bit(dev->devnum, dev->bus->devmap.devicemap);
Linus Torvalds's avatar
Linus Torvalds committed
921
		usbfs_remove_device(dev);
Linus Torvalds's avatar
Linus Torvalds committed
922
	}
923
	up(&dev->serialize);
924
	device_unregister(&dev->dev);
Linus Torvalds's avatar
Linus Torvalds committed
925 926
}

Linus Torvalds's avatar
Linus Torvalds committed
927
/**
928
 * usb_choose_address - pick device address (usbcore-internal)
929
 * @dev: newly detected device (in DEFAULT state)
Linus Torvalds's avatar
Linus Torvalds committed
930
 *
931 932
 * Picks a device address.  It's up to the hub (or root hub) driver
 * to handle and manage enumeration, starting from the DEFAULT state.
933
 * Only hub drivers (but not virtual root hub drivers for host
Linus Torvalds's avatar
Linus Torvalds committed
934
 * controllers) should ever call this.
Linus Torvalds's avatar
Linus Torvalds committed
935
 */
936
void usb_choose_address(struct usb_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
937 938 939 940 941 942
{
	int devnum;
	// FIXME needs locking for SMP!!
	/* why? this is called only from the hub thread, 
	 * which hopefully doesn't run on multiple CPU's simultaneously 8-)
	 */
943

Linus Torvalds's avatar
Linus Torvalds committed
944 945
	/* Try to allocate the next devnum beginning at bus->devnum_next. */
	devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, dev->bus->devnum_next);
Linus Torvalds's avatar
Linus Torvalds committed
946 947 948
	if (devnum >= 128)
		devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, 1);

Linus Torvalds's avatar
Linus Torvalds committed
949
	dev->bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
Linus Torvalds's avatar
Linus Torvalds committed
950 951 952 953 954 955 956 957

	if (devnum < 128) {
		set_bit(devnum, dev->bus->devmap.devicemap);
		dev->devnum = devnum;
	}
}


Linus Torvalds's avatar
Linus Torvalds committed
958 959
// hub-only!! ... and only exported for reset/reinit path.
// otherwise used internally, for usb_new_device()
Linus Torvalds's avatar
Linus Torvalds committed
960 961
int usb_set_address(struct usb_device *dev)
{
962 963 964 965 966 967 968 969 970 971 972
	int retval;

	if (dev->devnum == 0)
		return -EINVAL;
	if (dev->state != USB_STATE_DEFAULT && dev->state != USB_STATE_ADDRESS)
		return -EINVAL;
	retval = usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS,
		0, dev->devnum, 0, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
	if (retval == 0)
		dev->state = USB_STATE_ADDRESS;
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
973 974 975
}

/*
976
 * By the time we get here, we chose a new device address
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979 980
 * and is in the default state. We need to identify the thing and
 * get the ball rolling..
 *
 * Returns 0 for success, != 0 for error.
Linus Torvalds's avatar
Linus Torvalds committed
981 982 983
 *
 * This call is synchronous, and may not be used in an interrupt context.
 *
984 985
 * Only the hub driver should ever call this; root hub registration
 * uses it only indirectly.
Linus Torvalds's avatar
Linus Torvalds committed
986
 */
987 988
#define NEW_DEVICE_RETRYS	2
#define SET_ADDRESS_RETRYS	2
989
int usb_new_device(struct usb_device *dev, struct device *parent)
Linus Torvalds's avatar
Linus Torvalds committed
990
{
991
	int err = -EINVAL;
992 993
	int i;
	int j;
Linus Torvalds's avatar
Linus Torvalds committed
994

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	/*
	 * Set the driver for the usb device to point to the "generic" driver.
	 * This prevents the main usb device from being sent to the usb bus
	 * probe function.  Yes, it's a hack, but a nice one :)
	 *
	 * Do it asap, so more driver model stuff (like the device.h message
	 * utilities) can be used in hcd submit/unlink code paths.
	 */
	usb_generic_driver.bus = &usb_bus_type;
	dev->dev.parent = parent;
	dev->dev.driver = &usb_generic_driver;
	dev->dev.bus = &usb_bus_type;
1007
	dev->dev.driver_data = &usb_generic_driver_data;
1008 1009 1010 1011
	if (dev->dev.bus_id[0] == 0)
		sprintf (&dev->dev.bus_id[0], "%d-%s",
			 dev->bus->busnum, dev->devpath);

1012 1013 1014
	/* dma masks come from the controller; readonly, except to hcd */
	dev->dev.dma_mask = parent->dma_mask;

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
	 * it's fixed size except for full speed devices.
	 */
	switch (dev->speed) {
	case USB_SPEED_HIGH:		/* fixed at 64 */
		i = 64;
		break;
	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
		/* to determine the ep0 maxpacket size, read the first 8
		 * bytes from the device descriptor to get bMaxPacketSize0;
		 * then correct our initial (small) guess.
		 */
		// FALLTHROUGH
	case USB_SPEED_LOW:		/* fixed at 8 */
		i = 8;
		break;
	default:
1032
		goto fail;
1033 1034 1035
	}
	dev->epmaxpacketin [0] = i;
	dev->epmaxpacketout[0] = i;
Linus Torvalds's avatar
Linus Torvalds committed
1036

1037
	for (i = 0; i < NEW_DEVICE_RETRYS; ++i) {
Linus Torvalds's avatar
Linus Torvalds committed
1038

1039 1040 1041 1042 1043 1044 1045
		for (j = 0; j < SET_ADDRESS_RETRYS; ++j) {
			err = usb_set_address(dev);
			if (err >= 0)
				break;
			wait_ms(200);
		}
		if (err < 0) {
1046 1047
			dev_err(&dev->dev,
				"device not accepting address %d, error %d\n",
1048
				dev->devnum, err);
1049
			goto fail;
1050 1051 1052 1053
		}

		wait_ms(10);	/* Let the SET_ADDRESS settle */

1054
		/* high and low speed devices don't need this... */
1055 1056 1057 1058 1059
		err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8);
		if (err >= 8)
			break;
		wait_ms(100);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1060 1061

	if (err < 8) {
1062 1063
		dev_err(&dev->dev, "device descriptor read/8, error %d\n", err);
		goto fail;
Linus Torvalds's avatar
Linus Torvalds committed
1064
	}
1065
	if (dev->speed == USB_SPEED_FULL) {
1066 1067 1068
		usb_disable_endpoint(dev, 0);
		usb_endpoint_running(dev, 0, 1);
		usb_endpoint_running(dev, 0, 0);
1069 1070 1071
		dev->epmaxpacketin [0] = dev->descriptor.bMaxPacketSize0;
		dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1072

1073 1074
	/* USB device state == addressed ... still not usable */

Linus Torvalds's avatar
Linus Torvalds committed
1075
	err = usb_get_device_descriptor(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1076
	if (err < (signed)sizeof(dev->descriptor)) {
1077 1078
		dev_err(&dev->dev, "device descriptor read/all, error %d\n", err);
		goto fail;
Linus Torvalds's avatar
Linus Torvalds committed
1079 1080 1081 1082
	}

	err = usb_get_configuration(dev);
	if (err < 0) {
1083 1084
		dev_err(&dev->dev, "can't read configurations, error %d\n",
			err);
1085
		goto fail;
Linus Torvalds's avatar
Linus Torvalds committed
1086 1087
	}

1088
	/* Tell the world! */
1089
	dev_dbg(&dev->dev, "new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Linus Torvalds's avatar
Linus Torvalds committed
1090
		dev->descriptor.iManufacturer, dev->descriptor.iProduct, dev->descriptor.iSerialNumber);
David Brownell's avatar
David Brownell committed
1091

Linus Torvalds's avatar
Linus Torvalds committed
1092
#ifdef DEBUG
1093 1094 1095 1096
	if (dev->descriptor.iProduct)
		usb_show_string(dev, "Product", dev->descriptor.iProduct);
	if (dev->descriptor.iManufacturer)
		usb_show_string(dev, "Manufacturer", dev->descriptor.iManufacturer);
Linus Torvalds's avatar
Linus Torvalds committed
1097 1098 1099 1100
	if (dev->descriptor.iSerialNumber)
		usb_show_string(dev, "SerialNumber", dev->descriptor.iSerialNumber);
#endif

1101
	/* put device-specific files into sysfs */
1102
	err = device_add (&dev->dev);
1103 1104
	if (err) {
		dev_err(&dev->dev, "can't device_add, error %d\n", err);
1105
		goto fail;
1106
	}
1107
	usb_create_driverfs_dev_files (dev);
1108

1109 1110 1111 1112 1113 1114 1115 1116
	/* choose and set the configuration. that registers the interfaces
	 * with the driver core, and lets usb device drivers bind to them.
	 */
	if (dev->descriptor.bNumConfigurations != 1) {
		dev_info(&dev->dev,
			"configuration #%d chosen from %d choices\n",
			dev->config[0].desc.bConfigurationValue,
			dev->descriptor.bNumConfigurations);
1117
	}
1118 1119 1120 1121 1122
	err = usb_set_configuration(dev,
			dev->config[0].desc.bConfigurationValue);
	if (err) {
		dev_err(&dev->dev, "can't set config #%d, error %d\n",
			dev->config[0].desc.bConfigurationValue, err);
1123
		device_del(&dev->dev);
1124 1125 1126 1127
		goto fail;
	}

	/* USB device state == configured ... usable */
1128 1129 1130

	/* add a /proc/bus/usb entry */
	usbfs_add_device(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1131 1132

	return 0;
1133 1134 1135 1136 1137
fail:
	dev->state = USB_STATE_DEFAULT;
	clear_bit(dev->devnum, dev->bus->devmap.devicemap);
	dev->devnum = -1;
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1138 1139
}

1140
/**
1141
 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
 * @dev: device the buffer will be used with
 * @size: requested buffer size
 * @mem_flags: affect whether allocation may block
 * @dma: used to return DMA address of buffer
 *
 * Return value is either null (indicating no buffer could be allocated), or
 * the cpu-space pointer to a buffer that may be used to perform DMA to the
 * specified device.  Such cpu-space buffers are returned along with the DMA
 * address (through the pointer provided).
 *
1152 1153 1154
 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
 * mapping hardware for long idle periods.  The implementation varies between
1155
 * platforms, depending on details of how DMA will work to this device.
1156 1157
 * Using these buffers also helps prevent cacheline sharing problems on
 * architectures where CPU caches are not DMA-coherent.
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
 *
 * When the buffer is no longer used, free it with usb_buffer_free().
 */
void *usb_buffer_alloc (
	struct usb_device *dev,
	size_t size,
	int mem_flags,
	dma_addr_t *dma
)
{
	if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
		return 0;
	return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
}

/**
 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
 * @dev: device the buffer was used with
 * @size: requested buffer size
 * @addr: CPU address of buffer
 * @dma: DMA address of buffer
 *
 * This reclaims an I/O buffer, letting it be reused.  The memory must have
 * been allocated using usb_buffer_alloc(), and the parameters must match
 * those provided in that allocation request. 
 */
void usb_buffer_free (
	struct usb_device *dev,
	size_t size,
	void *addr,
	dma_addr_t dma
)
{
	if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
	    	return;
	dev->bus->op->buffer_free (dev->bus, size, addr, dma);
}

/**
 * usb_buffer_map - create DMA mapping(s) for an urb
1198
 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1199 1200
 *
 * Return value is either null (indicating no buffer could be mapped), or
1201 1202 1203 1204
 * the parameter.  URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
 * added to urb->transfer_flags if the operation succeeds.  If the device
 * is connected to this system through a non-DMA controller, this operation
 * always succeeds.
1205 1206 1207
 *
 * This call would normally be used for an urb which is reused, perhaps
 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1208
 * calls to synchronize memory and dma state.
1209 1210 1211 1212 1213 1214
 *
 * Reverse the effect of this call with usb_buffer_unmap().
 */
struct urb *usb_buffer_map (struct urb *urb)
{
	struct usb_bus		*bus;
1215
	struct device		*controller;
1216 1217 1218 1219

	if (!urb
			|| !urb->dev
			|| !(bus = urb->dev->bus)
1220
			|| !(controller = bus->controller))
1221 1222
		return 0;

1223 1224
	if (controller->dma_mask) {
		urb->transfer_dma = dma_map_single (controller,
1225
			urb->transfer_buffer, urb->transfer_buffer_length,
1226
			usb_pipein (urb->pipe)
1227
				? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1228 1229 1230 1231 1232
		if (usb_pipecontrol (urb->pipe))
			urb->setup_dma = dma_map_single (controller,
					urb->setup_packet,
					sizeof (struct usb_ctrlrequest),
					DMA_TO_DEVICE);
1233 1234
	// FIXME generic api broken like pci, can't report errors
	// if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1235 1236
	} else
		urb->transfer_dma = ~0;
1237 1238
	urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
				| URB_NO_SETUP_DMA_MAP);
1239 1240 1241 1242 1243
	return urb;
}

/**
 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1244
 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1245 1246 1247 1248
 */
void usb_buffer_dmasync (struct urb *urb)
{
	struct usb_bus		*bus;
1249
	struct device		*controller;
1250 1251

	if (!urb
1252
			|| !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1253 1254
			|| !urb->dev
			|| !(bus = urb->dev->bus)
1255
			|| !(controller = bus->controller))
1256 1257
		return;

1258
	if (controller->dma_mask) {
1259
		dma_sync_single (controller,
1260
			urb->transfer_dma, urb->transfer_buffer_length,
1261
			usb_pipein (urb->pipe)
1262
				? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1263 1264 1265 1266 1267 1268
		if (usb_pipecontrol (urb->pipe))
			dma_sync_single (controller,
					urb->setup_dma,
					sizeof (struct usb_ctrlrequest),
					DMA_TO_DEVICE);
	}
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
}

/**
 * usb_buffer_unmap - free DMA mapping(s) for an urb
 * @urb: urb whose transfer_buffer will be unmapped
 *
 * Reverses the effect of usb_buffer_map().
 */
void usb_buffer_unmap (struct urb *urb)
{
	struct usb_bus		*bus;
1280
	struct device		*controller;
1281 1282

	if (!urb
1283
			|| !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1284 1285
			|| !urb->dev
			|| !(bus = urb->dev->bus)
1286
			|| !(controller = bus->controller))
1287 1288
		return;

1289
	if (controller->dma_mask) {
1290
		dma_unmap_single (controller,
1291
			urb->transfer_dma, urb->transfer_buffer_length,
1292
			usb_pipein (urb->pipe)
1293
				? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1294 1295 1296 1297 1298 1299 1300 1301
		if (usb_pipecontrol (urb->pipe))
			dma_unmap_single (controller,
					urb->setup_dma,
					sizeof (struct usb_ctrlrequest),
					DMA_TO_DEVICE);
	}
	urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
				| URB_NO_SETUP_DMA_MAP);
1302
}
1303

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
/**
 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
 * @dev: device to which the scatterlist will be mapped
 * @pipe: endpoint defining the mapping direction
 * @sg: the scatterlist to map
 * @nents: the number of entries in the scatterlist
 *
 * Return value is either < 0 (indicating no buffers could be mapped), or
 * the number of DMA mapping array entries in the scatterlist.
 *
 * The caller is responsible for placing the resulting DMA addresses from
 * the scatterlist into URB transfer buffer pointers, and for setting the
1316
 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1317 1318 1319 1320 1321
 *
 * Top I/O rates come from queuing URBs, instead of waiting for each one
 * to complete before starting the next I/O.   This is particularly easy
 * to do with scatterlists.  Just allocate and submit one URB for each DMA
 * mapping entry returned, stopping on the first error or when all succeed.
1322
 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
 *
 * This call would normally be used when translating scatterlist requests,
 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
 * may be able to coalesce mappings for improved I/O efficiency.
 *
 * Reverse the effect of this call with usb_buffer_unmap_sg().
 */
int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
		struct scatterlist *sg, int nents)
{
	struct usb_bus		*bus;
1334
	struct device		*controller;
1335 1336 1337 1338

	if (!dev
			|| usb_pipecontrol (pipe)
			|| !(bus = dev->bus)
1339 1340
			|| !(controller = bus->controller)
			|| !controller->dma_mask)
1341 1342
		return -1;

1343 1344 1345
	// FIXME generic api broken like pci, can't report errors
	return dma_map_sg (controller, sg, nents,
			usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
}

/**
 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
 * @dev: device to which the scatterlist will be mapped
 * @pipe: endpoint defining the mapping direction
 * @sg: the scatterlist to synchronize
 * @n_hw_ents: the positive return value from usb_buffer_map_sg
 *
 * Use this when you are re-using a scatterlist's data buffers for
 * another USB request.
 */
void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
		struct scatterlist *sg, int n_hw_ents)
{
	struct usb_bus		*bus;
1362
	struct device		*controller;
1363 1364 1365

	if (!dev
			|| !(bus = dev->bus)
1366 1367
			|| !(controller = bus->controller)
			|| !controller->dma_mask)
1368 1369
		return;

1370 1371
	dma_sync_sg (controller, sg, n_hw_ents,
			usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
}

/**
 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
 * @dev: device to which the scatterlist will be mapped
 * @pipe: endpoint defining the mapping direction
 * @sg: the scatterlist to unmap
 * @n_hw_ents: the positive return value from usb_buffer_map_sg
 *
 * Reverses the effect of usb_buffer_map_sg().
 */
void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
		struct scatterlist *sg, int n_hw_ents)
{
	struct usb_bus		*bus;
1387
	struct device		*controller;
1388 1389 1390

	if (!dev
			|| !(bus = dev->bus)
1391 1392
			|| !(controller = bus->controller)
			|| !controller->dma_mask)
1393 1394
		return;

1395 1396
	dma_unmap_sg (controller, sg, n_hw_ents,
			usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1397 1398
}

1399 1400 1401 1402 1403
static int usb_device_suspend(struct device *dev, u32 state)
{
	struct usb_interface *intf;
	struct usb_driver *driver;

1404 1405
	if ((dev->driver == NULL) ||
	    (dev->driver == &usb_generic_driver) ||
1406 1407 1408 1409 1410 1411
	    (dev->driver_data == &usb_generic_driver_data))
		return 0;

	intf = to_usb_interface(dev);
	driver = to_usb_driver(dev->driver);

1412
	if (driver->suspend)
1413 1414 1415 1416 1417 1418 1419 1420 1421
		return driver->suspend(intf, state);
	return 0;
}

static int usb_device_resume(struct device *dev)
{
	struct usb_interface *intf;
	struct usb_driver *driver;

1422 1423
	if ((dev->driver == NULL) ||
	    (dev->driver == &usb_generic_driver) ||
1424 1425 1426 1427 1428 1429
	    (dev->driver_data == &usb_generic_driver_data))
		return 0;

	intf = to_usb_interface(dev);
	driver = to_usb_driver(dev->driver);

1430
	if (driver->resume)
1431 1432 1433
		return driver->resume(intf);
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1434

1435
struct bus_type usb_bus_type = {
1436 1437
	.name =		"usb",
	.match =	usb_device_match,
1438
	.hotplug =	usb_hotplug,
1439 1440
	.suspend =	usb_device_suspend,
	.resume =	usb_device_resume,
1441
};
Linus Torvalds's avatar
Linus Torvalds committed
1442

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
#ifndef MODULE

static int __init usb_setup_disable(char *str)
{
	nousb = 1;
	return 1;
}

/* format to disable USB on kernel command line is: nousb */
__setup("nousb", usb_setup_disable);

#endif

/*
 * for external read access to <nousb>
 */
int usb_disabled(void)
{
	return nousb;
}

Linus Torvalds's avatar
Linus Torvalds committed
1464 1465 1466 1467 1468
/*
 * Init
 */
static int __init usb_init(void)
{
1469 1470 1471 1472 1473
	if (nousb) {
		info("USB support disabled\n");
		return 0;
	}

1474
	bus_register(&usb_bus_type);
1475
	usb_host_init();
Linus Torvalds's avatar
Linus Torvalds committed
1476
	usb_major_init();
Linus Torvalds's avatar
Linus Torvalds committed
1477
	usbfs_init();
Linus Torvalds's avatar
Linus Torvalds committed
1478 1479
	usb_hub_init();

1480 1481
	driver_register(&usb_generic_driver);

Linus Torvalds's avatar
Linus Torvalds committed
1482 1483 1484 1485 1486 1487 1488 1489
	return 0;
}

/*
 * Cleanup
 */
static void __exit usb_exit(void)
{
1490 1491 1492 1493
	/* This will matter if shutdown/reboot does exitcalls. */
	if (nousb)
		return;

1494
	driver_unregister(&usb_generic_driver);
Linus Torvalds's avatar
Linus Torvalds committed
1495
	usb_major_cleanup();
Linus Torvalds's avatar
Linus Torvalds committed
1496
	usbfs_cleanup();
Linus Torvalds's avatar
Linus Torvalds committed
1497
	usb_hub_cleanup();
1498
	usb_host_cleanup();
1499
	bus_unregister(&usb_bus_type);
Linus Torvalds's avatar
Linus Torvalds committed
1500 1501
}

1502
subsys_initcall(usb_init);
Linus Torvalds's avatar
Linus Torvalds committed
1503 1504 1505 1506
module_exit(usb_exit);

/*
 * USB may be built into the kernel or be built as modules.
1507 1508
 * These symbols are exported for device (or host controller)
 * driver modules to use.
Linus Torvalds's avatar
Linus Torvalds committed
1509 1510 1511 1512 1513
 */
EXPORT_SYMBOL(usb_epnum_to_ep_desc);

EXPORT_SYMBOL(usb_register);
EXPORT_SYMBOL(usb_deregister);
1514
EXPORT_SYMBOL(usb_disabled);
1515

Linus Torvalds's avatar
Linus Torvalds committed
1516
EXPORT_SYMBOL(usb_alloc_dev);
1517
EXPORT_SYMBOL(usb_put_dev);
1518
EXPORT_SYMBOL(usb_get_dev);
1519
EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
Linus Torvalds's avatar
Linus Torvalds committed
1520 1521 1522 1523 1524

EXPORT_SYMBOL(usb_driver_claim_interface);
EXPORT_SYMBOL(usb_interface_claimed);
EXPORT_SYMBOL(usb_driver_release_interface);
EXPORT_SYMBOL(usb_match_id);
1525
EXPORT_SYMBOL(usb_find_interface);
1526
EXPORT_SYMBOL(usb_ifnum_to_if);
Linus Torvalds's avatar
Linus Torvalds committed
1527 1528 1529 1530 1531 1532

EXPORT_SYMBOL(usb_reset_device);
EXPORT_SYMBOL(usb_disconnect);

EXPORT_SYMBOL(__usb_get_extra_descriptor);

1533
EXPORT_SYMBOL(usb_find_device);
Linus Torvalds's avatar
Linus Torvalds committed
1534 1535
EXPORT_SYMBOL(usb_get_current_frame_number);

1536 1537 1538 1539 1540 1541 1542
EXPORT_SYMBOL (usb_buffer_alloc);
EXPORT_SYMBOL (usb_buffer_free);

EXPORT_SYMBOL (usb_buffer_map);
EXPORT_SYMBOL (usb_buffer_dmasync);
EXPORT_SYMBOL (usb_buffer_unmap);

1543 1544 1545 1546
EXPORT_SYMBOL (usb_buffer_map_sg);
EXPORT_SYMBOL (usb_buffer_dmasync_sg);
EXPORT_SYMBOL (usb_buffer_unmap_sg);

Linus Torvalds's avatar
Linus Torvalds committed
1547
MODULE_LICENSE("GPL");