driver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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
  20. * matching, probing, releasing, suspending and resuming for
  21. * real drivers.
  22. *
  23. */
  24. #include <linux/device.h>
  25. #include <linux/usb.h>
  26. #include "hcd.h"
  27. #include "usb.h"
  28. static int usb_match_one_id(struct usb_interface *interface,
  29. const struct usb_device_id *id);
  30. struct usb_dynid {
  31. struct list_head node;
  32. struct usb_device_id id;
  33. };
  34. #ifdef CONFIG_HOTPLUG
  35. /*
  36. * Adds a new dynamic USBdevice ID to this driver,
  37. * and cause the driver to probe for all devices again.
  38. */
  39. static ssize_t store_new_id(struct device_driver *driver,
  40. const char *buf, size_t count)
  41. {
  42. struct usb_driver *usb_drv = to_usb_driver(driver);
  43. struct usb_dynid *dynid;
  44. u32 idVendor = 0;
  45. u32 idProduct = 0;
  46. int fields = 0;
  47. fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
  48. if (fields < 2)
  49. return -EINVAL;
  50. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  51. if (!dynid)
  52. return -ENOMEM;
  53. INIT_LIST_HEAD(&dynid->node);
  54. dynid->id.idVendor = idVendor;
  55. dynid->id.idProduct = idProduct;
  56. dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  57. spin_lock(&usb_drv->dynids.lock);
  58. list_add_tail(&usb_drv->dynids.list, &dynid->node);
  59. spin_unlock(&usb_drv->dynids.lock);
  60. if (get_driver(driver)) {
  61. driver_attach(driver);
  62. put_driver(driver);
  63. }
  64. return count;
  65. }
  66. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  67. static int usb_create_newid_file(struct usb_driver *usb_drv)
  68. {
  69. int error = 0;
  70. if (usb_drv->no_dynamic_id)
  71. goto exit;
  72. if (usb_drv->probe != NULL)
  73. error = sysfs_create_file(&usb_drv->driver.kobj,
  74. &driver_attr_new_id.attr);
  75. exit:
  76. return error;
  77. }
  78. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  79. {
  80. if (usb_drv->no_dynamic_id)
  81. return;
  82. if (usb_drv->probe != NULL)
  83. sysfs_remove_file(&usb_drv->driver.kobj,
  84. &driver_attr_new_id.attr);
  85. }
  86. static void usb_free_dynids(struct usb_driver *usb_drv)
  87. {
  88. struct usb_dynid *dynid, *n;
  89. spin_lock(&usb_drv->dynids.lock);
  90. list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
  91. list_del(&dynid->node);
  92. kfree(dynid);
  93. }
  94. spin_unlock(&usb_drv->dynids.lock);
  95. }
  96. #else
  97. static inline int usb_create_newid_file(struct usb_driver *usb_drv)
  98. {
  99. return 0;
  100. }
  101. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  102. {
  103. }
  104. static inline void usb_free_dynids(struct usb_driver *usb_drv)
  105. {
  106. }
  107. #endif
  108. static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
  109. struct usb_driver *drv)
  110. {
  111. struct usb_dynid *dynid;
  112. spin_lock(&drv->dynids.lock);
  113. list_for_each_entry(dynid, &drv->dynids.list, node) {
  114. if (usb_match_one_id(intf, &dynid->id)) {
  115. spin_unlock(&drv->dynids.lock);
  116. return &dynid->id;
  117. }
  118. }
  119. spin_unlock(&drv->dynids.lock);
  120. return NULL;
  121. }
  122. /* called from driver core with usb_bus_type.subsys writelock */
  123. static int usb_probe_interface(struct device *dev)
  124. {
  125. struct usb_interface * intf = to_usb_interface(dev);
  126. struct usb_driver * driver = to_usb_driver(dev->driver);
  127. const struct usb_device_id *id;
  128. int error = -ENODEV;
  129. dev_dbg(dev, "%s\n", __FUNCTION__);
  130. if (!driver->probe)
  131. return error;
  132. /* FIXME we'd much prefer to just resume it ... */
  133. if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
  134. return -EHOSTUNREACH;
  135. id = usb_match_id(intf, driver->id_table);
  136. if (!id)
  137. id = usb_match_dynamic_id(intf, driver);
  138. if (id) {
  139. dev_dbg(dev, "%s - got id\n", __FUNCTION__);
  140. /* Interface "power state" doesn't correspond to any hardware
  141. * state whatsoever. We use it to record when it's bound to
  142. * a driver that may start I/0: it's not frozen/quiesced.
  143. */
  144. mark_active(intf);
  145. intf->condition = USB_INTERFACE_BINDING;
  146. error = driver->probe(intf, id);
  147. if (error) {
  148. mark_quiesced(intf);
  149. intf->condition = USB_INTERFACE_UNBOUND;
  150. } else
  151. intf->condition = USB_INTERFACE_BOUND;
  152. }
  153. return error;
  154. }
  155. /* called from driver core with usb_bus_type.subsys writelock */
  156. static int usb_unbind_interface(struct device *dev)
  157. {
  158. struct usb_interface *intf = to_usb_interface(dev);
  159. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  160. intf->condition = USB_INTERFACE_UNBINDING;
  161. /* release all urbs for this interface */
  162. usb_disable_interface(interface_to_usbdev(intf), intf);
  163. if (driver && driver->disconnect)
  164. driver->disconnect(intf);
  165. /* reset other interface state */
  166. usb_set_interface(interface_to_usbdev(intf),
  167. intf->altsetting[0].desc.bInterfaceNumber,
  168. 0);
  169. usb_set_intfdata(intf, NULL);
  170. intf->condition = USB_INTERFACE_UNBOUND;
  171. mark_quiesced(intf);
  172. return 0;
  173. }
  174. /**
  175. * usb_driver_claim_interface - bind a driver to an interface
  176. * @driver: the driver to be bound
  177. * @iface: the interface to which it will be bound; must be in the
  178. * usb device's active configuration
  179. * @priv: driver data associated with that interface
  180. *
  181. * This is used by usb device drivers that need to claim more than one
  182. * interface on a device when probing (audio and acm are current examples).
  183. * No device driver should directly modify internal usb_interface or
  184. * usb_device structure members.
  185. *
  186. * Few drivers should need to use this routine, since the most natural
  187. * way to bind to an interface is to return the private data from
  188. * the driver's probe() method.
  189. *
  190. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  191. * writelock. So driver probe() entries don't need extra locking,
  192. * but other call contexts may need to explicitly claim those locks.
  193. */
  194. int usb_driver_claim_interface(struct usb_driver *driver,
  195. struct usb_interface *iface, void* priv)
  196. {
  197. struct device *dev = &iface->dev;
  198. if (dev->driver)
  199. return -EBUSY;
  200. dev->driver = &driver->driver;
  201. usb_set_intfdata(iface, priv);
  202. iface->condition = USB_INTERFACE_BOUND;
  203. mark_active(iface);
  204. /* if interface was already added, bind now; else let
  205. * the future device_add() bind it, bypassing probe()
  206. */
  207. if (device_is_registered(dev))
  208. device_bind_driver(dev);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(usb_driver_claim_interface);
  212. /**
  213. * usb_driver_release_interface - unbind a driver from an interface
  214. * @driver: the driver to be unbound
  215. * @iface: the interface from which it will be unbound
  216. *
  217. * This can be used by drivers to release an interface without waiting
  218. * for their disconnect() methods to be called. In typical cases this
  219. * also causes the driver disconnect() method to be called.
  220. *
  221. * This call is synchronous, and may not be used in an interrupt context.
  222. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  223. * writelock. So driver disconnect() entries don't need extra locking,
  224. * but other call contexts may need to explicitly claim those locks.
  225. */
  226. void usb_driver_release_interface(struct usb_driver *driver,
  227. struct usb_interface *iface)
  228. {
  229. struct device *dev = &iface->dev;
  230. /* this should never happen, don't release something that's not ours */
  231. if (!dev->driver || dev->driver != &driver->driver)
  232. return;
  233. /* don't release from within disconnect() */
  234. if (iface->condition != USB_INTERFACE_BOUND)
  235. return;
  236. /* don't release if the interface hasn't been added yet */
  237. if (device_is_registered(dev)) {
  238. iface->condition = USB_INTERFACE_UNBINDING;
  239. device_release_driver(dev);
  240. }
  241. dev->driver = NULL;
  242. usb_set_intfdata(iface, NULL);
  243. iface->condition = USB_INTERFACE_UNBOUND;
  244. mark_quiesced(iface);
  245. }
  246. EXPORT_SYMBOL(usb_driver_release_interface);
  247. /* returns 0 if no match, 1 if match */
  248. static int usb_match_one_id(struct usb_interface *interface,
  249. const struct usb_device_id *id)
  250. {
  251. struct usb_host_interface *intf;
  252. struct usb_device *dev;
  253. /* proc_connectinfo in devio.c may call us with id == NULL. */
  254. if (id == NULL)
  255. return 0;
  256. intf = interface->cur_altsetting;
  257. dev = interface_to_usbdev(interface);
  258. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  259. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  260. return 0;
  261. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  262. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  263. return 0;
  264. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  265. greater than any unsigned number. */
  266. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  267. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  268. return 0;
  269. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  270. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  271. return 0;
  272. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  273. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  274. return 0;
  275. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  276. (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
  277. return 0;
  278. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  279. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  280. return 0;
  281. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  282. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  283. return 0;
  284. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  285. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  286. return 0;
  287. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  288. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  289. return 0;
  290. return 1;
  291. }
  292. /**
  293. * usb_match_id - find first usb_device_id matching device or interface
  294. * @interface: the interface of interest
  295. * @id: array of usb_device_id structures, terminated by zero entry
  296. *
  297. * usb_match_id searches an array of usb_device_id's and returns
  298. * the first one matching the device or interface, or null.
  299. * This is used when binding (or rebinding) a driver to an interface.
  300. * Most USB device drivers will use this indirectly, through the usb core,
  301. * but some layered driver frameworks use it directly.
  302. * These device tables are exported with MODULE_DEVICE_TABLE, through
  303. * modutils, to support the driver loading functionality of USB hotplugging.
  304. *
  305. * What Matches:
  306. *
  307. * The "match_flags" element in a usb_device_id controls which
  308. * members are used. If the corresponding bit is set, the
  309. * value in the device_id must match its corresponding member
  310. * in the device or interface descriptor, or else the device_id
  311. * does not match.
  312. *
  313. * "driver_info" is normally used only by device drivers,
  314. * but you can create a wildcard "matches anything" usb_device_id
  315. * as a driver's "modules.usbmap" entry if you provide an id with
  316. * only a nonzero "driver_info" field. If you do this, the USB device
  317. * driver's probe() routine should use additional intelligence to
  318. * decide whether to bind to the specified interface.
  319. *
  320. * What Makes Good usb_device_id Tables:
  321. *
  322. * The match algorithm is very simple, so that intelligence in
  323. * driver selection must come from smart driver id records.
  324. * Unless you have good reasons to use another selection policy,
  325. * provide match elements only in related groups, and order match
  326. * specifiers from specific to general. Use the macros provided
  327. * for that purpose if you can.
  328. *
  329. * The most specific match specifiers use device descriptor
  330. * data. These are commonly used with product-specific matches;
  331. * the USB_DEVICE macro lets you provide vendor and product IDs,
  332. * and you can also match against ranges of product revisions.
  333. * These are widely used for devices with application or vendor
  334. * specific bDeviceClass values.
  335. *
  336. * Matches based on device class/subclass/protocol specifications
  337. * are slightly more general; use the USB_DEVICE_INFO macro, or
  338. * its siblings. These are used with single-function devices
  339. * where bDeviceClass doesn't specify that each interface has
  340. * its own class.
  341. *
  342. * Matches based on interface class/subclass/protocol are the
  343. * most general; they let drivers bind to any interface on a
  344. * multiple-function device. Use the USB_INTERFACE_INFO
  345. * macro, or its siblings, to match class-per-interface style
  346. * devices (as recorded in bDeviceClass).
  347. *
  348. * Within those groups, remember that not all combinations are
  349. * meaningful. For example, don't give a product version range
  350. * without vendor and product IDs; or specify a protocol without
  351. * its associated class and subclass.
  352. */
  353. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  354. const struct usb_device_id *id)
  355. {
  356. /* proc_connectinfo in devio.c may call us with id == NULL. */
  357. if (id == NULL)
  358. return NULL;
  359. /* It is important to check that id->driver_info is nonzero,
  360. since an entry that is all zeroes except for a nonzero
  361. id->driver_info is the way to create an entry that
  362. indicates that the driver want to examine every
  363. device and interface. */
  364. for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
  365. id->driver_info; id++) {
  366. if (usb_match_one_id(interface, id))
  367. return id;
  368. }
  369. return NULL;
  370. }
  371. EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
  372. int usb_device_match(struct device *dev, struct device_driver *drv)
  373. {
  374. struct usb_interface *intf;
  375. struct usb_driver *usb_drv;
  376. const struct usb_device_id *id;
  377. /* check for generic driver, which we don't match any device with */
  378. if (drv == &usb_generic_driver)
  379. return 0;
  380. intf = to_usb_interface(dev);
  381. usb_drv = to_usb_driver(drv);
  382. id = usb_match_id(intf, usb_drv->id_table);
  383. if (id)
  384. return 1;
  385. id = usb_match_dynamic_id(intf, usb_drv);
  386. if (id)
  387. return 1;
  388. return 0;
  389. }
  390. #ifdef CONFIG_HOTPLUG
  391. /*
  392. * This sends an uevent to userspace, typically helping to load driver
  393. * or other modules, configure the device, and more. Drivers can provide
  394. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  395. *
  396. * We're called either from khubd (the typical case) or from root hub
  397. * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
  398. * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
  399. * device (and this configuration!) are still present.
  400. */
  401. static int usb_uevent(struct device *dev, char **envp, int num_envp,
  402. char *buffer, int buffer_size)
  403. {
  404. struct usb_interface *intf;
  405. struct usb_device *usb_dev;
  406. struct usb_host_interface *alt;
  407. int i = 0;
  408. int length = 0;
  409. if (!dev)
  410. return -ENODEV;
  411. /* driver is often null here; dev_dbg() would oops */
  412. pr_debug ("usb %s: uevent\n", dev->bus_id);
  413. /* Must check driver_data here, as on remove driver is always NULL */
  414. if ((dev->driver == &usb_generic_driver) ||
  415. (dev->driver_data == &usb_generic_driver_data))
  416. return 0;
  417. intf = to_usb_interface(dev);
  418. usb_dev = interface_to_usbdev (intf);
  419. alt = intf->cur_altsetting;
  420. if (usb_dev->devnum < 0) {
  421. pr_debug ("usb %s: already deleted?\n", dev->bus_id);
  422. return -ENODEV;
  423. }
  424. if (!usb_dev->bus) {
  425. pr_debug ("usb %s: bus removed?\n", dev->bus_id);
  426. return -ENODEV;
  427. }
  428. #ifdef CONFIG_USB_DEVICEFS
  429. /* If this is available, userspace programs can directly read
  430. * all the device descriptors we don't tell them about. Or
  431. * even act as usermode drivers.
  432. *
  433. * FIXME reduce hardwired intelligence here
  434. */
  435. if (add_uevent_var(envp, num_envp, &i,
  436. buffer, buffer_size, &length,
  437. "DEVICE=/proc/bus/usb/%03d/%03d",
  438. usb_dev->bus->busnum, usb_dev->devnum))
  439. return -ENOMEM;
  440. #endif
  441. /* per-device configurations are common */
  442. if (add_uevent_var(envp, num_envp, &i,
  443. buffer, buffer_size, &length,
  444. "PRODUCT=%x/%x/%x",
  445. le16_to_cpu(usb_dev->descriptor.idVendor),
  446. le16_to_cpu(usb_dev->descriptor.idProduct),
  447. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  448. return -ENOMEM;
  449. /* class-based driver binding models */
  450. if (add_uevent_var(envp, num_envp, &i,
  451. buffer, buffer_size, &length,
  452. "TYPE=%d/%d/%d",
  453. usb_dev->descriptor.bDeviceClass,
  454. usb_dev->descriptor.bDeviceSubClass,
  455. usb_dev->descriptor.bDeviceProtocol))
  456. return -ENOMEM;
  457. if (add_uevent_var(envp, num_envp, &i,
  458. buffer, buffer_size, &length,
  459. "INTERFACE=%d/%d/%d",
  460. alt->desc.bInterfaceClass,
  461. alt->desc.bInterfaceSubClass,
  462. alt->desc.bInterfaceProtocol))
  463. return -ENOMEM;
  464. if (add_uevent_var(envp, num_envp, &i,
  465. buffer, buffer_size, &length,
  466. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  467. le16_to_cpu(usb_dev->descriptor.idVendor),
  468. le16_to_cpu(usb_dev->descriptor.idProduct),
  469. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  470. usb_dev->descriptor.bDeviceClass,
  471. usb_dev->descriptor.bDeviceSubClass,
  472. usb_dev->descriptor.bDeviceProtocol,
  473. alt->desc.bInterfaceClass,
  474. alt->desc.bInterfaceSubClass,
  475. alt->desc.bInterfaceProtocol))
  476. return -ENOMEM;
  477. envp[i] = NULL;
  478. return 0;
  479. }
  480. #else
  481. static int usb_uevent(struct device *dev, char **envp,
  482. int num_envp, char *buffer, int buffer_size)
  483. {
  484. return -ENODEV;
  485. }
  486. #endif /* CONFIG_HOTPLUG */
  487. /**
  488. * usb_register_driver - register a USB driver
  489. * @new_driver: USB operations for the driver
  490. * @owner: module owner of this driver.
  491. *
  492. * Registers a USB driver with the USB core. The list of unattached
  493. * interfaces will be rescanned whenever a new driver is added, allowing
  494. * the new driver to attach to any recognized devices.
  495. * Returns a negative error code on failure and 0 on success.
  496. *
  497. * NOTE: if you want your driver to use the USB major number, you must call
  498. * usb_register_dev() to enable that functionality. This function no longer
  499. * takes care of that.
  500. */
  501. int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
  502. {
  503. int retval = 0;
  504. if (usb_disabled())
  505. return -ENODEV;
  506. new_driver->driver.name = (char *)new_driver->name;
  507. new_driver->driver.bus = &usb_bus_type;
  508. new_driver->driver.probe = usb_probe_interface;
  509. new_driver->driver.remove = usb_unbind_interface;
  510. new_driver->driver.owner = owner;
  511. spin_lock_init(&new_driver->dynids.lock);
  512. INIT_LIST_HEAD(&new_driver->dynids.list);
  513. retval = driver_register(&new_driver->driver);
  514. if (!retval) {
  515. pr_info("%s: registered new driver %s\n",
  516. usbcore_name, new_driver->name);
  517. usbfs_update_special();
  518. usb_create_newid_file(new_driver);
  519. } else {
  520. printk(KERN_ERR "%s: error %d registering driver %s\n",
  521. usbcore_name, retval, new_driver->name);
  522. }
  523. return retval;
  524. }
  525. EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
  526. /**
  527. * usb_deregister - unregister a USB driver
  528. * @driver: USB operations of the driver to unregister
  529. * Context: must be able to sleep
  530. *
  531. * Unlinks the specified driver from the internal USB driver list.
  532. *
  533. * NOTE: If you called usb_register_dev(), you still need to call
  534. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  535. * this * call will no longer do it for you.
  536. */
  537. void usb_deregister(struct usb_driver *driver)
  538. {
  539. pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
  540. usb_remove_newid_file(driver);
  541. usb_free_dynids(driver);
  542. driver_unregister(&driver->driver);
  543. usbfs_update_special();
  544. }
  545. EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
  546. #ifdef CONFIG_PM
  547. static int verify_suspended(struct device *dev, void *unused)
  548. {
  549. if (dev->driver == NULL)
  550. return 0;
  551. return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
  552. }
  553. static int usb_generic_suspend(struct device *dev, pm_message_t message)
  554. {
  555. struct usb_interface *intf;
  556. struct usb_driver *driver;
  557. int status;
  558. /* USB devices enter SUSPEND state through their hubs, but can be
  559. * marked for FREEZE as soon as their children are already idled.
  560. * But those semantics are useless, so we equate the two (sigh).
  561. */
  562. if (dev->driver == &usb_generic_driver) {
  563. if (dev->power.power_state.event == message.event)
  564. return 0;
  565. /* we need to rule out bogus requests through sysfs */
  566. status = device_for_each_child(dev, NULL, verify_suspended);
  567. if (status)
  568. return status;
  569. return usb_port_suspend(to_usb_device(dev));
  570. }
  571. if ((dev->driver == NULL) ||
  572. (dev->driver_data == &usb_generic_driver_data))
  573. return 0;
  574. intf = to_usb_interface(dev);
  575. driver = to_usb_driver(dev->driver);
  576. /* with no hardware, USB interfaces only use FREEZE and ON states */
  577. if (!is_active(intf))
  578. return 0;
  579. if (driver->suspend && driver->resume) {
  580. status = driver->suspend(intf, message);
  581. if (status)
  582. dev_err(dev, "%s error %d\n", "suspend", status);
  583. else
  584. mark_quiesced(intf);
  585. } else {
  586. // FIXME else if there's no suspend method, disconnect...
  587. dev_warn(dev, "no suspend for driver %s?\n", driver->name);
  588. mark_quiesced(intf);
  589. status = 0;
  590. }
  591. return status;
  592. }
  593. static int usb_generic_resume(struct device *dev)
  594. {
  595. struct usb_interface *intf;
  596. struct usb_driver *driver;
  597. struct usb_device *udev;
  598. int status;
  599. if (dev->power.power_state.event == PM_EVENT_ON)
  600. return 0;
  601. /* mark things as "on" immediately, no matter what errors crop up */
  602. dev->power.power_state.event = PM_EVENT_ON;
  603. /* devices resume through their hubs */
  604. if (dev->driver == &usb_generic_driver) {
  605. udev = to_usb_device(dev);
  606. if (udev->state == USB_STATE_NOTATTACHED)
  607. return 0;
  608. return usb_port_resume(udev);
  609. }
  610. if ((dev->driver == NULL) ||
  611. (dev->driver_data == &usb_generic_driver_data)) {
  612. dev->power.power_state.event = PM_EVENT_FREEZE;
  613. return 0;
  614. }
  615. intf = to_usb_interface(dev);
  616. driver = to_usb_driver(dev->driver);
  617. udev = interface_to_usbdev(intf);
  618. if (udev->state == USB_STATE_NOTATTACHED)
  619. return 0;
  620. /* if driver was suspended, it has a resume method;
  621. * however, sysfs can wrongly mark things as suspended
  622. * (on the "no suspend method" FIXME path above)
  623. */
  624. if (driver->resume) {
  625. status = driver->resume(intf);
  626. if (status) {
  627. dev_err(dev, "%s error %d\n", "resume", status);
  628. mark_quiesced(intf);
  629. }
  630. } else
  631. dev_warn(dev, "no resume for driver %s?\n", driver->name);
  632. return 0;
  633. }
  634. #endif /* CONFIG_PM */
  635. struct bus_type usb_bus_type = {
  636. .name = "usb",
  637. .match = usb_device_match,
  638. .uevent = usb_uevent,
  639. #ifdef CONFIG_PM
  640. .suspend = usb_generic_suspend,
  641. .resume = usb_generic_resume,
  642. #endif
  643. };