driver.c 47 KB

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