driver.c 50 KB

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