driver.c 49 KB

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