usb.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * drivers/usb/usb.c
  3. *
  4. * (C) Copyright Linus Torvalds 1999
  5. * (C) Copyright Johannes Erdfelt 1999-2001
  6. * (C) Copyright Andreas Gal 1999
  7. * (C) Copyright Gregory P. Smith 1999
  8. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  9. * (C) Copyright Randy Dunlap 2000
  10. * (C) Copyright David Brownell 2000-2004
  11. * (C) Copyright Yggdrasil Computing, Inc. 2000
  12. * (usb_device_id matching changes by Adam J. Richter)
  13. * (C) Copyright Greg Kroah-Hartman 2002-2003
  14. *
  15. * NOTE! This is not actually a driver at all, rather this is
  16. * just a collection of helper routines that implement the
  17. * generic USB things that the real drivers can use..
  18. *
  19. * Think of this as a "USB library" rather than anything else.
  20. * It should be considered a slave, with no callbacks. Callbacks
  21. * are evil.
  22. */
  23. #include <linux/config.h>
  24. #ifdef CONFIG_USB_DEBUG
  25. #define DEBUG
  26. #else
  27. #undef DEBUG
  28. #endif
  29. #include <linux/module.h>
  30. #include <linux/string.h>
  31. #include <linux/bitops.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h> /* for in_interrupt() */
  34. #include <linux/kmod.h>
  35. #include <linux/init.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/errno.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/rwsem.h>
  40. #include <linux/usb.h>
  41. #include <asm/io.h>
  42. #include <asm/scatterlist.h>
  43. #include <linux/mm.h>
  44. #include <linux/dma-mapping.h>
  45. #include "hcd.h"
  46. #include "usb.h"
  47. const char *usbcore_name = "usbcore";
  48. static int nousb; /* Disable USB when built into kernel image */
  49. /* Not honored on modular build */
  50. static DECLARE_RWSEM(usb_all_devices_rwsem);
  51. static int generic_probe (struct device *dev)
  52. {
  53. return 0;
  54. }
  55. static int generic_remove (struct device *dev)
  56. {
  57. struct usb_device *udev = to_usb_device(dev);
  58. /* if this is only an unbind, not a physical disconnect, then
  59. * unconfigure the device */
  60. if (udev->state == USB_STATE_CONFIGURED)
  61. usb_set_configuration(udev, 0);
  62. /* in case the call failed or the device was suspended */
  63. if (udev->state >= USB_STATE_CONFIGURED)
  64. usb_disable_device(udev, 0);
  65. return 0;
  66. }
  67. static struct device_driver usb_generic_driver = {
  68. .owner = THIS_MODULE,
  69. .name = "usb",
  70. .bus = &usb_bus_type,
  71. .probe = generic_probe,
  72. .remove = generic_remove,
  73. };
  74. static int usb_generic_driver_data;
  75. /* called from driver core with usb_bus_type.subsys writelock */
  76. static int usb_probe_interface(struct device *dev)
  77. {
  78. struct usb_interface * intf = to_usb_interface(dev);
  79. struct usb_driver * driver = to_usb_driver(dev->driver);
  80. const struct usb_device_id *id;
  81. int error = -ENODEV;
  82. dev_dbg(dev, "%s\n", __FUNCTION__);
  83. if (!driver->probe)
  84. return error;
  85. /* FIXME we'd much prefer to just resume it ... */
  86. if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
  87. return -EHOSTUNREACH;
  88. id = usb_match_id (intf, driver->id_table);
  89. if (id) {
  90. dev_dbg (dev, "%s - got id\n", __FUNCTION__);
  91. intf->condition = USB_INTERFACE_BINDING;
  92. error = driver->probe (intf, id);
  93. intf->condition = error ? USB_INTERFACE_UNBOUND :
  94. USB_INTERFACE_BOUND;
  95. }
  96. return error;
  97. }
  98. /* called from driver core with usb_bus_type.subsys writelock */
  99. static int usb_unbind_interface(struct device *dev)
  100. {
  101. struct usb_interface *intf = to_usb_interface(dev);
  102. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  103. intf->condition = USB_INTERFACE_UNBINDING;
  104. /* release all urbs for this interface */
  105. usb_disable_interface(interface_to_usbdev(intf), intf);
  106. if (driver && driver->disconnect)
  107. driver->disconnect(intf);
  108. /* reset other interface state */
  109. usb_set_interface(interface_to_usbdev(intf),
  110. intf->altsetting[0].desc.bInterfaceNumber,
  111. 0);
  112. usb_set_intfdata(intf, NULL);
  113. intf->condition = USB_INTERFACE_UNBOUND;
  114. return 0;
  115. }
  116. /**
  117. * usb_register - register a USB driver
  118. * @new_driver: USB operations for the driver
  119. *
  120. * Registers a USB driver with the USB core. The list of unattached
  121. * interfaces will be rescanned whenever a new driver is added, allowing
  122. * the new driver to attach to any recognized devices.
  123. * Returns a negative error code on failure and 0 on success.
  124. *
  125. * NOTE: if you want your driver to use the USB major number, you must call
  126. * usb_register_dev() to enable that functionality. This function no longer
  127. * takes care of that.
  128. */
  129. int usb_register(struct usb_driver *new_driver)
  130. {
  131. int retval = 0;
  132. if (nousb)
  133. return -ENODEV;
  134. new_driver->driver.name = (char *)new_driver->name;
  135. new_driver->driver.bus = &usb_bus_type;
  136. new_driver->driver.probe = usb_probe_interface;
  137. new_driver->driver.remove = usb_unbind_interface;
  138. new_driver->driver.owner = new_driver->owner;
  139. usb_lock_all_devices();
  140. retval = driver_register(&new_driver->driver);
  141. usb_unlock_all_devices();
  142. if (!retval) {
  143. pr_info("%s: registered new driver %s\n",
  144. usbcore_name, new_driver->name);
  145. usbfs_update_special();
  146. } else {
  147. printk(KERN_ERR "%s: error %d registering driver %s\n",
  148. usbcore_name, retval, new_driver->name);
  149. }
  150. return retval;
  151. }
  152. /**
  153. * usb_deregister - unregister a USB driver
  154. * @driver: USB operations of the driver to unregister
  155. * Context: must be able to sleep
  156. *
  157. * Unlinks the specified driver from the internal USB driver list.
  158. *
  159. * NOTE: If you called usb_register_dev(), you still need to call
  160. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  161. * this * call will no longer do it for you.
  162. */
  163. void usb_deregister(struct usb_driver *driver)
  164. {
  165. pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
  166. usb_lock_all_devices();
  167. driver_unregister (&driver->driver);
  168. usb_unlock_all_devices();
  169. usbfs_update_special();
  170. }
  171. /**
  172. * usb_ifnum_to_if - get the interface object with a given interface number
  173. * @dev: the device whose current configuration is considered
  174. * @ifnum: the desired interface
  175. *
  176. * This walks the device descriptor for the currently active configuration
  177. * and returns a pointer to the interface with that particular interface
  178. * number, or null.
  179. *
  180. * Note that configuration descriptors are not required to assign interface
  181. * numbers sequentially, so that it would be incorrect to assume that
  182. * the first interface in that descriptor corresponds to interface zero.
  183. * This routine helps device drivers avoid such mistakes.
  184. * However, you should make sure that you do the right thing with any
  185. * alternate settings available for this interfaces.
  186. *
  187. * Don't call this function unless you are bound to one of the interfaces
  188. * on this device or you have locked the device!
  189. */
  190. struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
  191. {
  192. struct usb_host_config *config = dev->actconfig;
  193. int i;
  194. if (!config)
  195. return NULL;
  196. for (i = 0; i < config->desc.bNumInterfaces; i++)
  197. if (config->interface[i]->altsetting[0]
  198. .desc.bInterfaceNumber == ifnum)
  199. return config->interface[i];
  200. return NULL;
  201. }
  202. /**
  203. * usb_altnum_to_altsetting - get the altsetting structure with a given
  204. * alternate setting number.
  205. * @intf: the interface containing the altsetting in question
  206. * @altnum: the desired alternate setting number
  207. *
  208. * This searches the altsetting array of the specified interface for
  209. * an entry with the correct bAlternateSetting value and returns a pointer
  210. * to that entry, or null.
  211. *
  212. * Note that altsettings need not be stored sequentially by number, so
  213. * it would be incorrect to assume that the first altsetting entry in
  214. * the array corresponds to altsetting zero. This routine helps device
  215. * drivers avoid such mistakes.
  216. *
  217. * Don't call this function unless you are bound to the intf interface
  218. * or you have locked the device!
  219. */
  220. struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
  221. unsigned int altnum)
  222. {
  223. int i;
  224. for (i = 0; i < intf->num_altsetting; i++) {
  225. if (intf->altsetting[i].desc.bAlternateSetting == altnum)
  226. return &intf->altsetting[i];
  227. }
  228. return NULL;
  229. }
  230. /**
  231. * usb_driver_claim_interface - bind a driver to an interface
  232. * @driver: the driver to be bound
  233. * @iface: the interface to which it will be bound; must be in the
  234. * usb device's active configuration
  235. * @priv: driver data associated with that interface
  236. *
  237. * This is used by usb device drivers that need to claim more than one
  238. * interface on a device when probing (audio and acm are current examples).
  239. * No device driver should directly modify internal usb_interface or
  240. * usb_device structure members.
  241. *
  242. * Few drivers should need to use this routine, since the most natural
  243. * way to bind to an interface is to return the private data from
  244. * the driver's probe() method.
  245. *
  246. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  247. * writelock. So driver probe() entries don't need extra locking,
  248. * but other call contexts may need to explicitly claim those locks.
  249. */
  250. int usb_driver_claim_interface(struct usb_driver *driver,
  251. struct usb_interface *iface, void* priv)
  252. {
  253. struct device *dev = &iface->dev;
  254. if (dev->driver)
  255. return -EBUSY;
  256. dev->driver = &driver->driver;
  257. usb_set_intfdata(iface, priv);
  258. iface->condition = USB_INTERFACE_BOUND;
  259. /* if interface was already added, bind now; else let
  260. * the future device_add() bind it, bypassing probe()
  261. */
  262. if (klist_node_attached(&dev->knode_bus))
  263. device_bind_driver(dev);
  264. return 0;
  265. }
  266. /**
  267. * usb_driver_release_interface - unbind a driver from an interface
  268. * @driver: the driver to be unbound
  269. * @iface: the interface from which it will be unbound
  270. *
  271. * This can be used by drivers to release an interface without waiting
  272. * for their disconnect() methods to be called. In typical cases this
  273. * also causes the driver disconnect() method to be called.
  274. *
  275. * This call is synchronous, and may not be used in an interrupt context.
  276. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  277. * writelock. So driver disconnect() entries don't need extra locking,
  278. * but other call contexts may need to explicitly claim those locks.
  279. */
  280. void usb_driver_release_interface(struct usb_driver *driver,
  281. struct usb_interface *iface)
  282. {
  283. struct device *dev = &iface->dev;
  284. /* this should never happen, don't release something that's not ours */
  285. if (!dev->driver || dev->driver != &driver->driver)
  286. return;
  287. /* don't release from within disconnect() */
  288. if (iface->condition != USB_INTERFACE_BOUND)
  289. return;
  290. /* release only after device_add() */
  291. if (klist_node_attached(&dev->knode_bus)) {
  292. iface->condition = USB_INTERFACE_UNBINDING;
  293. device_release_driver(dev);
  294. }
  295. dev->driver = NULL;
  296. usb_set_intfdata(iface, NULL);
  297. iface->condition = USB_INTERFACE_UNBOUND;
  298. }
  299. /**
  300. * usb_match_id - find first usb_device_id matching device or interface
  301. * @interface: the interface of interest
  302. * @id: array of usb_device_id structures, terminated by zero entry
  303. *
  304. * usb_match_id searches an array of usb_device_id's and returns
  305. * the first one matching the device or interface, or null.
  306. * This is used when binding (or rebinding) a driver to an interface.
  307. * Most USB device drivers will use this indirectly, through the usb core,
  308. * but some layered driver frameworks use it directly.
  309. * These device tables are exported with MODULE_DEVICE_TABLE, through
  310. * modutils and "modules.usbmap", to support the driver loading
  311. * functionality of USB hotplugging.
  312. *
  313. * What Matches:
  314. *
  315. * The "match_flags" element in a usb_device_id controls which
  316. * members are used. If the corresponding bit is set, the
  317. * value in the device_id must match its corresponding member
  318. * in the device or interface descriptor, or else the device_id
  319. * does not match.
  320. *
  321. * "driver_info" is normally used only by device drivers,
  322. * but you can create a wildcard "matches anything" usb_device_id
  323. * as a driver's "modules.usbmap" entry if you provide an id with
  324. * only a nonzero "driver_info" field. If you do this, the USB device
  325. * driver's probe() routine should use additional intelligence to
  326. * decide whether to bind to the specified interface.
  327. *
  328. * What Makes Good usb_device_id Tables:
  329. *
  330. * The match algorithm is very simple, so that intelligence in
  331. * driver selection must come from smart driver id records.
  332. * Unless you have good reasons to use another selection policy,
  333. * provide match elements only in related groups, and order match
  334. * specifiers from specific to general. Use the macros provided
  335. * for that purpose if you can.
  336. *
  337. * The most specific match specifiers use device descriptor
  338. * data. These are commonly used with product-specific matches;
  339. * the USB_DEVICE macro lets you provide vendor and product IDs,
  340. * and you can also match against ranges of product revisions.
  341. * These are widely used for devices with application or vendor
  342. * specific bDeviceClass values.
  343. *
  344. * Matches based on device class/subclass/protocol specifications
  345. * are slightly more general; use the USB_DEVICE_INFO macro, or
  346. * its siblings. These are used with single-function devices
  347. * where bDeviceClass doesn't specify that each interface has
  348. * its own class.
  349. *
  350. * Matches based on interface class/subclass/protocol are the
  351. * most general; they let drivers bind to any interface on a
  352. * multiple-function device. Use the USB_INTERFACE_INFO
  353. * macro, or its siblings, to match class-per-interface style
  354. * devices (as recorded in bDeviceClass).
  355. *
  356. * Within those groups, remember that not all combinations are
  357. * meaningful. For example, don't give a product version range
  358. * without vendor and product IDs; or specify a protocol without
  359. * its associated class and subclass.
  360. */
  361. const struct usb_device_id *
  362. usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
  363. {
  364. struct usb_host_interface *intf;
  365. struct usb_device *dev;
  366. /* proc_connectinfo in devio.c may call us with id == NULL. */
  367. if (id == NULL)
  368. return NULL;
  369. intf = interface->cur_altsetting;
  370. dev = interface_to_usbdev(interface);
  371. /* It is important to check that id->driver_info is nonzero,
  372. since an entry that is all zeroes except for a nonzero
  373. id->driver_info is the way to create an entry that
  374. indicates that the driver want to examine every
  375. device and interface. */
  376. for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
  377. id->driver_info; id++) {
  378. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  379. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  380. continue;
  381. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  382. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  383. continue;
  384. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  385. greater than any unsigned number. */
  386. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  387. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  388. continue;
  389. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  390. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  391. continue;
  392. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  393. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  394. continue;
  395. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  396. (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
  397. continue;
  398. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  399. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  400. continue;
  401. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  402. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  403. continue;
  404. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  405. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  406. continue;
  407. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  408. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  409. continue;
  410. return id;
  411. }
  412. return NULL;
  413. }
  414. static int __find_interface(struct device * dev, void * data)
  415. {
  416. struct usb_interface ** ret = (struct usb_interface **)data;
  417. struct usb_interface * intf = *ret;
  418. int *minor = (int *)data;
  419. /* can't look at usb devices, only interfaces */
  420. if (dev->driver == &usb_generic_driver)
  421. return 0;
  422. intf = to_usb_interface(dev);
  423. if (intf->minor != -1 && intf->minor == *minor) {
  424. *ret = intf;
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. /**
  430. * usb_find_interface - find usb_interface pointer for driver and device
  431. * @drv: the driver whose current configuration is considered
  432. * @minor: the minor number of the desired device
  433. *
  434. * This walks the driver device list and returns a pointer to the interface
  435. * with the matching minor. Note, this only works for devices that share the
  436. * USB major number.
  437. */
  438. struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
  439. {
  440. struct usb_interface *intf = (struct usb_interface *)(long)minor;
  441. int ret;
  442. ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface);
  443. return ret ? intf : NULL;
  444. }
  445. static int usb_device_match (struct device *dev, struct device_driver *drv)
  446. {
  447. struct usb_interface *intf;
  448. struct usb_driver *usb_drv;
  449. const struct usb_device_id *id;
  450. /* check for generic driver, which we don't match any device with */
  451. if (drv == &usb_generic_driver)
  452. return 0;
  453. intf = to_usb_interface(dev);
  454. usb_drv = to_usb_driver(drv);
  455. id = usb_match_id (intf, usb_drv->id_table);
  456. if (id)
  457. return 1;
  458. return 0;
  459. }
  460. #ifdef CONFIG_HOTPLUG
  461. /*
  462. * USB hotplugging invokes what /proc/sys/kernel/hotplug says
  463. * (normally /sbin/hotplug) when USB devices get added or removed.
  464. *
  465. * This invokes a user mode policy agent, typically helping to load driver
  466. * or other modules, configure the device, and more. Drivers can provide
  467. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  468. *
  469. * We're called either from khubd (the typical case) or from root hub
  470. * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
  471. * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
  472. * device (and this configuration!) are still present.
  473. */
  474. static int usb_hotplug (struct device *dev, char **envp, int num_envp,
  475. char *buffer, int buffer_size)
  476. {
  477. struct usb_interface *intf;
  478. struct usb_device *usb_dev;
  479. int i = 0;
  480. int length = 0;
  481. if (!dev)
  482. return -ENODEV;
  483. /* driver is often null here; dev_dbg() would oops */
  484. pr_debug ("usb %s: hotplug\n", dev->bus_id);
  485. /* Must check driver_data here, as on remove driver is always NULL */
  486. if ((dev->driver == &usb_generic_driver) ||
  487. (dev->driver_data == &usb_generic_driver_data))
  488. return 0;
  489. intf = to_usb_interface(dev);
  490. usb_dev = interface_to_usbdev (intf);
  491. if (usb_dev->devnum < 0) {
  492. pr_debug ("usb %s: already deleted?\n", dev->bus_id);
  493. return -ENODEV;
  494. }
  495. if (!usb_dev->bus) {
  496. pr_debug ("usb %s: bus removed?\n", dev->bus_id);
  497. return -ENODEV;
  498. }
  499. #ifdef CONFIG_USB_DEVICEFS
  500. /* If this is available, userspace programs can directly read
  501. * all the device descriptors we don't tell them about. Or
  502. * even act as usermode drivers.
  503. *
  504. * FIXME reduce hardwired intelligence here
  505. */
  506. if (add_hotplug_env_var(envp, num_envp, &i,
  507. buffer, buffer_size, &length,
  508. "DEVICE=/proc/bus/usb/%03d/%03d",
  509. usb_dev->bus->busnum, usb_dev->devnum))
  510. return -ENOMEM;
  511. #endif
  512. /* per-device configurations are common */
  513. if (add_hotplug_env_var(envp, num_envp, &i,
  514. buffer, buffer_size, &length,
  515. "PRODUCT=%x/%x/%x",
  516. le16_to_cpu(usb_dev->descriptor.idVendor),
  517. le16_to_cpu(usb_dev->descriptor.idProduct),
  518. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  519. return -ENOMEM;
  520. /* class-based driver binding models */
  521. if (add_hotplug_env_var(envp, num_envp, &i,
  522. buffer, buffer_size, &length,
  523. "TYPE=%d/%d/%d",
  524. usb_dev->descriptor.bDeviceClass,
  525. usb_dev->descriptor.bDeviceSubClass,
  526. usb_dev->descriptor.bDeviceProtocol))
  527. return -ENOMEM;
  528. if (usb_dev->descriptor.bDeviceClass == 0) {
  529. struct usb_host_interface *alt = intf->cur_altsetting;
  530. /* 2.4 only exposed interface zero. in 2.5, hotplug
  531. * agents are called for all interfaces, and can use
  532. * $DEVPATH/bInterfaceNumber if necessary.
  533. */
  534. if (add_hotplug_env_var(envp, num_envp, &i,
  535. buffer, buffer_size, &length,
  536. "INTERFACE=%d/%d/%d",
  537. alt->desc.bInterfaceClass,
  538. alt->desc.bInterfaceSubClass,
  539. alt->desc.bInterfaceProtocol))
  540. return -ENOMEM;
  541. if (add_hotplug_env_var(envp, num_envp, &i,
  542. buffer, buffer_size, &length,
  543. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  544. le16_to_cpu(usb_dev->descriptor.idVendor),
  545. le16_to_cpu(usb_dev->descriptor.idProduct),
  546. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  547. usb_dev->descriptor.bDeviceClass,
  548. usb_dev->descriptor.bDeviceSubClass,
  549. usb_dev->descriptor.bDeviceProtocol,
  550. alt->desc.bInterfaceClass,
  551. alt->desc.bInterfaceSubClass,
  552. alt->desc.bInterfaceProtocol))
  553. return -ENOMEM;
  554. } else {
  555. if (add_hotplug_env_var(envp, num_envp, &i,
  556. buffer, buffer_size, &length,
  557. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic*isc*ip*",
  558. le16_to_cpu(usb_dev->descriptor.idVendor),
  559. le16_to_cpu(usb_dev->descriptor.idProduct),
  560. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  561. usb_dev->descriptor.bDeviceClass,
  562. usb_dev->descriptor.bDeviceSubClass,
  563. usb_dev->descriptor.bDeviceProtocol))
  564. return -ENOMEM;
  565. }
  566. envp[i] = NULL;
  567. return 0;
  568. }
  569. #else
  570. static int usb_hotplug (struct device *dev, char **envp,
  571. int num_envp, char *buffer, int buffer_size)
  572. {
  573. return -ENODEV;
  574. }
  575. #endif /* CONFIG_HOTPLUG */
  576. /**
  577. * usb_release_dev - free a usb device structure when all users of it are finished.
  578. * @dev: device that's been disconnected
  579. *
  580. * Will be called only by the device core when all users of this usb device are
  581. * done.
  582. */
  583. static void usb_release_dev(struct device *dev)
  584. {
  585. struct usb_device *udev;
  586. udev = to_usb_device(dev);
  587. usb_destroy_configuration(udev);
  588. usb_bus_put(udev->bus);
  589. kfree(udev->product);
  590. kfree(udev->manufacturer);
  591. kfree(udev->serial);
  592. kfree(udev);
  593. }
  594. /**
  595. * usb_alloc_dev - usb device constructor (usbcore-internal)
  596. * @parent: hub to which device is connected; null to allocate a root hub
  597. * @bus: bus used to access the device
  598. * @port1: one-based index of port; ignored for root hubs
  599. * Context: !in_interrupt ()
  600. *
  601. * Only hub drivers (including virtual root hub drivers for host
  602. * controllers) should ever call this.
  603. *
  604. * This call may not be used in a non-sleeping context.
  605. */
  606. struct usb_device *
  607. usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
  608. {
  609. struct usb_device *dev;
  610. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  611. if (!dev)
  612. return NULL;
  613. memset(dev, 0, sizeof(*dev));
  614. bus = usb_bus_get(bus);
  615. if (!bus) {
  616. kfree(dev);
  617. return NULL;
  618. }
  619. device_initialize(&dev->dev);
  620. dev->dev.bus = &usb_bus_type;
  621. dev->dev.dma_mask = bus->controller->dma_mask;
  622. dev->dev.driver_data = &usb_generic_driver_data;
  623. dev->dev.driver = &usb_generic_driver;
  624. dev->dev.release = usb_release_dev;
  625. dev->state = USB_STATE_ATTACHED;
  626. INIT_LIST_HEAD(&dev->ep0.urb_list);
  627. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  628. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  629. /* ep0 maxpacket comes later, from device descriptor */
  630. dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
  631. /* Save readable and stable topology id, distinguishing devices
  632. * by location for diagnostics, tools, driver model, etc. The
  633. * string is a path along hub ports, from the root. Each device's
  634. * dev->devpath will be stable until USB is re-cabled, and hubs
  635. * are often labeled with these port numbers. The bus_id isn't
  636. * as stable: bus->busnum changes easily from modprobe order,
  637. * cardbus or pci hotplugging, and so on.
  638. */
  639. if (unlikely (!parent)) {
  640. dev->devpath [0] = '0';
  641. dev->dev.parent = bus->controller;
  642. sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
  643. } else {
  644. /* match any labeling on the hubs; it's one-based */
  645. if (parent->devpath [0] == '0')
  646. snprintf (dev->devpath, sizeof dev->devpath,
  647. "%d", port1);
  648. else
  649. snprintf (dev->devpath, sizeof dev->devpath,
  650. "%s.%d", parent->devpath, port1);
  651. dev->dev.parent = &parent->dev;
  652. sprintf (&dev->dev.bus_id[0], "%d-%s",
  653. bus->busnum, dev->devpath);
  654. /* hub driver sets up TT records */
  655. }
  656. dev->bus = bus;
  657. dev->parent = parent;
  658. INIT_LIST_HEAD(&dev->filelist);
  659. init_MUTEX(&dev->serialize);
  660. return dev;
  661. }
  662. /**
  663. * usb_get_dev - increments the reference count of the usb device structure
  664. * @dev: the device being referenced
  665. *
  666. * Each live reference to a device should be refcounted.
  667. *
  668. * Drivers for USB interfaces should normally record such references in
  669. * their probe() methods, when they bind to an interface, and release
  670. * them by calling usb_put_dev(), in their disconnect() methods.
  671. *
  672. * A pointer to the device with the incremented reference counter is returned.
  673. */
  674. struct usb_device *usb_get_dev(struct usb_device *dev)
  675. {
  676. if (dev)
  677. get_device(&dev->dev);
  678. return dev;
  679. }
  680. /**
  681. * usb_put_dev - release a use of the usb device structure
  682. * @dev: device that's been disconnected
  683. *
  684. * Must be called when a user of a device is finished with it. When the last
  685. * user of the device calls this function, the memory of the device is freed.
  686. */
  687. void usb_put_dev(struct usb_device *dev)
  688. {
  689. if (dev)
  690. put_device(&dev->dev);
  691. }
  692. /**
  693. * usb_get_intf - increments the reference count of the usb interface structure
  694. * @intf: the interface being referenced
  695. *
  696. * Each live reference to a interface must be refcounted.
  697. *
  698. * Drivers for USB interfaces should normally record such references in
  699. * their probe() methods, when they bind to an interface, and release
  700. * them by calling usb_put_intf(), in their disconnect() methods.
  701. *
  702. * A pointer to the interface with the incremented reference counter is
  703. * returned.
  704. */
  705. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  706. {
  707. if (intf)
  708. get_device(&intf->dev);
  709. return intf;
  710. }
  711. /**
  712. * usb_put_intf - release a use of the usb interface structure
  713. * @intf: interface that's been decremented
  714. *
  715. * Must be called when a user of an interface is finished with it. When the
  716. * last user of the interface calls this function, the memory of the interface
  717. * is freed.
  718. */
  719. void usb_put_intf(struct usb_interface *intf)
  720. {
  721. if (intf)
  722. put_device(&intf->dev);
  723. }
  724. /* USB device locking
  725. *
  726. * Although locking USB devices should be straightforward, it is
  727. * complicated by the way the driver-model core works. When a new USB
  728. * driver is registered or unregistered, the core will automatically
  729. * probe or disconnect all matching interfaces on all USB devices while
  730. * holding the USB subsystem writelock. There's no good way for us to
  731. * tell which devices will be used or to lock them beforehand; our only
  732. * option is to effectively lock all the USB devices.
  733. *
  734. * We do that by using a private rw-semaphore, usb_all_devices_rwsem.
  735. * When locking an individual device you must first acquire the rwsem's
  736. * readlock. When a driver is registered or unregistered the writelock
  737. * must be held. These actions are encapsulated in the subroutines
  738. * below, so all a driver needs to do is call usb_lock_device() and
  739. * usb_unlock_device().
  740. *
  741. * Complications arise when several devices are to be locked at the same
  742. * time. Only hub-aware drivers that are part of usbcore ever have to
  743. * do this; nobody else needs to worry about it. The problem is that
  744. * usb_lock_device() must not be called to lock a second device since it
  745. * would acquire the rwsem's readlock reentrantly, leading to deadlock if
  746. * another thread was waiting for the writelock. The solution is simple:
  747. *
  748. * When locking more than one device, call usb_lock_device()
  749. * to lock the first one. Lock the others by calling
  750. * down(&udev->serialize) directly.
  751. *
  752. * When unlocking multiple devices, use up(&udev->serialize)
  753. * to unlock all but the last one. Unlock the last one by
  754. * calling usb_unlock_device().
  755. *
  756. * When locking both a device and its parent, always lock the
  757. * the parent first.
  758. */
  759. /**
  760. * usb_lock_device - acquire the lock for a usb device structure
  761. * @udev: device that's being locked
  762. *
  763. * Use this routine when you don't hold any other device locks;
  764. * to acquire nested inner locks call down(&udev->serialize) directly.
  765. * This is necessary for proper interaction with usb_lock_all_devices().
  766. */
  767. void usb_lock_device(struct usb_device *udev)
  768. {
  769. down_read(&usb_all_devices_rwsem);
  770. down(&udev->serialize);
  771. }
  772. /**
  773. * usb_trylock_device - attempt to acquire the lock for a usb device structure
  774. * @udev: device that's being locked
  775. *
  776. * Don't use this routine if you already hold a device lock;
  777. * use down_trylock(&udev->serialize) instead.
  778. * This is necessary for proper interaction with usb_lock_all_devices().
  779. *
  780. * Returns 1 if successful, 0 if contention.
  781. */
  782. int usb_trylock_device(struct usb_device *udev)
  783. {
  784. if (!down_read_trylock(&usb_all_devices_rwsem))
  785. return 0;
  786. if (down_trylock(&udev->serialize)) {
  787. up_read(&usb_all_devices_rwsem);
  788. return 0;
  789. }
  790. return 1;
  791. }
  792. /**
  793. * usb_lock_device_for_reset - cautiously acquire the lock for a
  794. * usb device structure
  795. * @udev: device that's being locked
  796. * @iface: interface bound to the driver making the request (optional)
  797. *
  798. * Attempts to acquire the device lock, but fails if the device is
  799. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  800. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  801. * lock, the routine polls repeatedly. This is to prevent deadlock with
  802. * disconnect; in some drivers (such as usb-storage) the disconnect()
  803. * or suspend() method will block waiting for a device reset to complete.
  804. *
  805. * Returns a negative error code for failure, otherwise 1 or 0 to indicate
  806. * that the device will or will not have to be unlocked. (0 can be
  807. * returned when an interface is given and is BINDING, because in that
  808. * case the driver already owns the device lock.)
  809. */
  810. int usb_lock_device_for_reset(struct usb_device *udev,
  811. struct usb_interface *iface)
  812. {
  813. unsigned long jiffies_expire = jiffies + HZ;
  814. if (udev->state == USB_STATE_NOTATTACHED)
  815. return -ENODEV;
  816. if (udev->state == USB_STATE_SUSPENDED)
  817. return -EHOSTUNREACH;
  818. if (iface) {
  819. switch (iface->condition) {
  820. case USB_INTERFACE_BINDING:
  821. return 0;
  822. case USB_INTERFACE_BOUND:
  823. break;
  824. default:
  825. return -EINTR;
  826. }
  827. }
  828. while (!usb_trylock_device(udev)) {
  829. /* If we can't acquire the lock after waiting one second,
  830. * we're probably deadlocked */
  831. if (time_after(jiffies, jiffies_expire))
  832. return -EBUSY;
  833. msleep(15);
  834. if (udev->state == USB_STATE_NOTATTACHED)
  835. return -ENODEV;
  836. if (udev->state == USB_STATE_SUSPENDED)
  837. return -EHOSTUNREACH;
  838. if (iface && iface->condition != USB_INTERFACE_BOUND)
  839. return -EINTR;
  840. }
  841. return 1;
  842. }
  843. /**
  844. * usb_unlock_device - release the lock for a usb device structure
  845. * @udev: device that's being unlocked
  846. *
  847. * Use this routine when releasing the only device lock you hold;
  848. * to release inner nested locks call up(&udev->serialize) directly.
  849. * This is necessary for proper interaction with usb_lock_all_devices().
  850. */
  851. void usb_unlock_device(struct usb_device *udev)
  852. {
  853. up(&udev->serialize);
  854. up_read(&usb_all_devices_rwsem);
  855. }
  856. /**
  857. * usb_lock_all_devices - acquire the lock for all usb device structures
  858. *
  859. * This is necessary when registering a new driver or probing a bus,
  860. * since the driver-model core may try to use any usb_device.
  861. */
  862. void usb_lock_all_devices(void)
  863. {
  864. down_write(&usb_all_devices_rwsem);
  865. }
  866. /**
  867. * usb_unlock_all_devices - release the lock for all usb device structures
  868. */
  869. void usb_unlock_all_devices(void)
  870. {
  871. up_write(&usb_all_devices_rwsem);
  872. }
  873. static struct usb_device *match_device(struct usb_device *dev,
  874. u16 vendor_id, u16 product_id)
  875. {
  876. struct usb_device *ret_dev = NULL;
  877. int child;
  878. dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
  879. le16_to_cpu(dev->descriptor.idVendor),
  880. le16_to_cpu(dev->descriptor.idProduct));
  881. /* see if this device matches */
  882. if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
  883. (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
  884. dev_dbg (&dev->dev, "matched this device!\n");
  885. ret_dev = usb_get_dev(dev);
  886. goto exit;
  887. }
  888. /* look through all of the children of this device */
  889. for (child = 0; child < dev->maxchild; ++child) {
  890. if (dev->children[child]) {
  891. down(&dev->children[child]->serialize);
  892. ret_dev = match_device(dev->children[child],
  893. vendor_id, product_id);
  894. up(&dev->children[child]->serialize);
  895. if (ret_dev)
  896. goto exit;
  897. }
  898. }
  899. exit:
  900. return ret_dev;
  901. }
  902. /**
  903. * usb_find_device - find a specific usb device in the system
  904. * @vendor_id: the vendor id of the device to find
  905. * @product_id: the product id of the device to find
  906. *
  907. * Returns a pointer to a struct usb_device if such a specified usb
  908. * device is present in the system currently. The usage count of the
  909. * device will be incremented if a device is found. Make sure to call
  910. * usb_put_dev() when the caller is finished with the device.
  911. *
  912. * If a device with the specified vendor and product id is not found,
  913. * NULL is returned.
  914. */
  915. struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
  916. {
  917. struct list_head *buslist;
  918. struct usb_bus *bus;
  919. struct usb_device *dev = NULL;
  920. down(&usb_bus_list_lock);
  921. for (buslist = usb_bus_list.next;
  922. buslist != &usb_bus_list;
  923. buslist = buslist->next) {
  924. bus = container_of(buslist, struct usb_bus, bus_list);
  925. if (!bus->root_hub)
  926. continue;
  927. usb_lock_device(bus->root_hub);
  928. dev = match_device(bus->root_hub, vendor_id, product_id);
  929. usb_unlock_device(bus->root_hub);
  930. if (dev)
  931. goto exit;
  932. }
  933. exit:
  934. up(&usb_bus_list_lock);
  935. return dev;
  936. }
  937. /**
  938. * usb_get_current_frame_number - return current bus frame number
  939. * @dev: the device whose bus is being queried
  940. *
  941. * Returns the current frame number for the USB host controller
  942. * used with the given USB device. This can be used when scheduling
  943. * isochronous requests.
  944. *
  945. * Note that different kinds of host controller have different
  946. * "scheduling horizons". While one type might support scheduling only
  947. * 32 frames into the future, others could support scheduling up to
  948. * 1024 frames into the future.
  949. */
  950. int usb_get_current_frame_number(struct usb_device *dev)
  951. {
  952. return dev->bus->op->get_frame_number (dev);
  953. }
  954. /*-------------------------------------------------------------------*/
  955. /*
  956. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  957. * extra field of the interface and endpoint descriptor structs.
  958. */
  959. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  960. unsigned char type, void **ptr)
  961. {
  962. struct usb_descriptor_header *header;
  963. while (size >= sizeof(struct usb_descriptor_header)) {
  964. header = (struct usb_descriptor_header *)buffer;
  965. if (header->bLength < 2) {
  966. printk(KERN_ERR
  967. "%s: bogus descriptor, type %d length %d\n",
  968. usbcore_name,
  969. header->bDescriptorType,
  970. header->bLength);
  971. return -1;
  972. }
  973. if (header->bDescriptorType == type) {
  974. *ptr = header;
  975. return 0;
  976. }
  977. buffer += header->bLength;
  978. size -= header->bLength;
  979. }
  980. return -1;
  981. }
  982. /**
  983. * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  984. * @dev: device the buffer will be used with
  985. * @size: requested buffer size
  986. * @mem_flags: affect whether allocation may block
  987. * @dma: used to return DMA address of buffer
  988. *
  989. * Return value is either null (indicating no buffer could be allocated), or
  990. * the cpu-space pointer to a buffer that may be used to perform DMA to the
  991. * specified device. Such cpu-space buffers are returned along with the DMA
  992. * address (through the pointer provided).
  993. *
  994. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  995. * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
  996. * mapping hardware for long idle periods. The implementation varies between
  997. * platforms, depending on details of how DMA will work to this device.
  998. * Using these buffers also helps prevent cacheline sharing problems on
  999. * architectures where CPU caches are not DMA-coherent.
  1000. *
  1001. * When the buffer is no longer used, free it with usb_buffer_free().
  1002. */
  1003. void *usb_buffer_alloc (
  1004. struct usb_device *dev,
  1005. size_t size,
  1006. unsigned mem_flags,
  1007. dma_addr_t *dma
  1008. )
  1009. {
  1010. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
  1011. return NULL;
  1012. return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
  1013. }
  1014. /**
  1015. * usb_buffer_free - free memory allocated with usb_buffer_alloc()
  1016. * @dev: device the buffer was used with
  1017. * @size: requested buffer size
  1018. * @addr: CPU address of buffer
  1019. * @dma: DMA address of buffer
  1020. *
  1021. * This reclaims an I/O buffer, letting it be reused. The memory must have
  1022. * been allocated using usb_buffer_alloc(), and the parameters must match
  1023. * those provided in that allocation request.
  1024. */
  1025. void usb_buffer_free (
  1026. struct usb_device *dev,
  1027. size_t size,
  1028. void *addr,
  1029. dma_addr_t dma
  1030. )
  1031. {
  1032. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
  1033. return;
  1034. dev->bus->op->buffer_free (dev->bus, size, addr, dma);
  1035. }
  1036. /**
  1037. * usb_buffer_map - create DMA mapping(s) for an urb
  1038. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  1039. *
  1040. * Return value is either null (indicating no buffer could be mapped), or
  1041. * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
  1042. * added to urb->transfer_flags if the operation succeeds. If the device
  1043. * is connected to this system through a non-DMA controller, this operation
  1044. * always succeeds.
  1045. *
  1046. * This call would normally be used for an urb which is reused, perhaps
  1047. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  1048. * calls to synchronize memory and dma state.
  1049. *
  1050. * Reverse the effect of this call with usb_buffer_unmap().
  1051. */
  1052. #if 0
  1053. struct urb *usb_buffer_map (struct urb *urb)
  1054. {
  1055. struct usb_bus *bus;
  1056. struct device *controller;
  1057. if (!urb
  1058. || !urb->dev
  1059. || !(bus = urb->dev->bus)
  1060. || !(controller = bus->controller))
  1061. return NULL;
  1062. if (controller->dma_mask) {
  1063. urb->transfer_dma = dma_map_single (controller,
  1064. urb->transfer_buffer, urb->transfer_buffer_length,
  1065. usb_pipein (urb->pipe)
  1066. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1067. if (usb_pipecontrol (urb->pipe))
  1068. urb->setup_dma = dma_map_single (controller,
  1069. urb->setup_packet,
  1070. sizeof (struct usb_ctrlrequest),
  1071. DMA_TO_DEVICE);
  1072. // FIXME generic api broken like pci, can't report errors
  1073. // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
  1074. } else
  1075. urb->transfer_dma = ~0;
  1076. urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
  1077. | URB_NO_SETUP_DMA_MAP);
  1078. return urb;
  1079. }
  1080. #endif /* 0 */
  1081. /* XXX DISABLED, no users currently. If you wish to re-enable this
  1082. * XXX please determine whether the sync is to transfer ownership of
  1083. * XXX the buffer from device to cpu or vice verse, and thusly use the
  1084. * XXX appropriate _for_{cpu,device}() method. -DaveM
  1085. */
  1086. #if 0
  1087. /**
  1088. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  1089. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  1090. */
  1091. void usb_buffer_dmasync (struct urb *urb)
  1092. {
  1093. struct usb_bus *bus;
  1094. struct device *controller;
  1095. if (!urb
  1096. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  1097. || !urb->dev
  1098. || !(bus = urb->dev->bus)
  1099. || !(controller = bus->controller))
  1100. return;
  1101. if (controller->dma_mask) {
  1102. dma_sync_single (controller,
  1103. urb->transfer_dma, urb->transfer_buffer_length,
  1104. usb_pipein (urb->pipe)
  1105. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1106. if (usb_pipecontrol (urb->pipe))
  1107. dma_sync_single (controller,
  1108. urb->setup_dma,
  1109. sizeof (struct usb_ctrlrequest),
  1110. DMA_TO_DEVICE);
  1111. }
  1112. }
  1113. #endif
  1114. /**
  1115. * usb_buffer_unmap - free DMA mapping(s) for an urb
  1116. * @urb: urb whose transfer_buffer will be unmapped
  1117. *
  1118. * Reverses the effect of usb_buffer_map().
  1119. */
  1120. #if 0
  1121. void usb_buffer_unmap (struct urb *urb)
  1122. {
  1123. struct usb_bus *bus;
  1124. struct device *controller;
  1125. if (!urb
  1126. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  1127. || !urb->dev
  1128. || !(bus = urb->dev->bus)
  1129. || !(controller = bus->controller))
  1130. return;
  1131. if (controller->dma_mask) {
  1132. dma_unmap_single (controller,
  1133. urb->transfer_dma, urb->transfer_buffer_length,
  1134. usb_pipein (urb->pipe)
  1135. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1136. if (usb_pipecontrol (urb->pipe))
  1137. dma_unmap_single (controller,
  1138. urb->setup_dma,
  1139. sizeof (struct usb_ctrlrequest),
  1140. DMA_TO_DEVICE);
  1141. }
  1142. urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
  1143. | URB_NO_SETUP_DMA_MAP);
  1144. }
  1145. #endif /* 0 */
  1146. /**
  1147. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  1148. * @dev: device to which the scatterlist will be mapped
  1149. * @pipe: endpoint defining the mapping direction
  1150. * @sg: the scatterlist to map
  1151. * @nents: the number of entries in the scatterlist
  1152. *
  1153. * Return value is either < 0 (indicating no buffers could be mapped), or
  1154. * the number of DMA mapping array entries in the scatterlist.
  1155. *
  1156. * The caller is responsible for placing the resulting DMA addresses from
  1157. * the scatterlist into URB transfer buffer pointers, and for setting the
  1158. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  1159. *
  1160. * Top I/O rates come from queuing URBs, instead of waiting for each one
  1161. * to complete before starting the next I/O. This is particularly easy
  1162. * to do with scatterlists. Just allocate and submit one URB for each DMA
  1163. * mapping entry returned, stopping on the first error or when all succeed.
  1164. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  1165. *
  1166. * This call would normally be used when translating scatterlist requests,
  1167. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  1168. * may be able to coalesce mappings for improved I/O efficiency.
  1169. *
  1170. * Reverse the effect of this call with usb_buffer_unmap_sg().
  1171. */
  1172. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  1173. struct scatterlist *sg, int nents)
  1174. {
  1175. struct usb_bus *bus;
  1176. struct device *controller;
  1177. if (!dev
  1178. || usb_pipecontrol (pipe)
  1179. || !(bus = dev->bus)
  1180. || !(controller = bus->controller)
  1181. || !controller->dma_mask)
  1182. return -1;
  1183. // FIXME generic api broken like pci, can't report errors
  1184. return dma_map_sg (controller, sg, nents,
  1185. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1186. }
  1187. /* XXX DISABLED, no users currently. If you wish to re-enable this
  1188. * XXX please determine whether the sync is to transfer ownership of
  1189. * XXX the buffer from device to cpu or vice verse, and thusly use the
  1190. * XXX appropriate _for_{cpu,device}() method. -DaveM
  1191. */
  1192. #if 0
  1193. /**
  1194. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  1195. * @dev: device to which the scatterlist will be mapped
  1196. * @pipe: endpoint defining the mapping direction
  1197. * @sg: the scatterlist to synchronize
  1198. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  1199. *
  1200. * Use this when you are re-using a scatterlist's data buffers for
  1201. * another USB request.
  1202. */
  1203. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  1204. struct scatterlist *sg, int n_hw_ents)
  1205. {
  1206. struct usb_bus *bus;
  1207. struct device *controller;
  1208. if (!dev
  1209. || !(bus = dev->bus)
  1210. || !(controller = bus->controller)
  1211. || !controller->dma_mask)
  1212. return;
  1213. dma_sync_sg (controller, sg, n_hw_ents,
  1214. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1215. }
  1216. #endif
  1217. /**
  1218. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  1219. * @dev: device to which the scatterlist will be mapped
  1220. * @pipe: endpoint defining the mapping direction
  1221. * @sg: the scatterlist to unmap
  1222. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  1223. *
  1224. * Reverses the effect of usb_buffer_map_sg().
  1225. */
  1226. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  1227. struct scatterlist *sg, int n_hw_ents)
  1228. {
  1229. struct usb_bus *bus;
  1230. struct device *controller;
  1231. if (!dev
  1232. || !(bus = dev->bus)
  1233. || !(controller = bus->controller)
  1234. || !controller->dma_mask)
  1235. return;
  1236. dma_unmap_sg (controller, sg, n_hw_ents,
  1237. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1238. }
  1239. static int usb_generic_suspend(struct device *dev, pm_message_t message)
  1240. {
  1241. struct usb_interface *intf;
  1242. struct usb_driver *driver;
  1243. if (dev->driver == &usb_generic_driver)
  1244. return usb_suspend_device (to_usb_device(dev), message);
  1245. if ((dev->driver == NULL) ||
  1246. (dev->driver_data == &usb_generic_driver_data))
  1247. return 0;
  1248. intf = to_usb_interface(dev);
  1249. driver = to_usb_driver(dev->driver);
  1250. /* there's only one USB suspend state */
  1251. if (intf->dev.power.power_state.event)
  1252. return 0;
  1253. if (driver->suspend)
  1254. return driver->suspend(intf, message);
  1255. return 0;
  1256. }
  1257. static int usb_generic_resume(struct device *dev)
  1258. {
  1259. struct usb_interface *intf;
  1260. struct usb_driver *driver;
  1261. /* devices resume through their hub */
  1262. if (dev->driver == &usb_generic_driver)
  1263. return usb_resume_device (to_usb_device(dev));
  1264. if ((dev->driver == NULL) ||
  1265. (dev->driver_data == &usb_generic_driver_data))
  1266. return 0;
  1267. intf = to_usb_interface(dev);
  1268. driver = to_usb_driver(dev->driver);
  1269. if (driver->resume)
  1270. return driver->resume(intf);
  1271. return 0;
  1272. }
  1273. struct bus_type usb_bus_type = {
  1274. .name = "usb",
  1275. .match = usb_device_match,
  1276. .hotplug = usb_hotplug,
  1277. .suspend = usb_generic_suspend,
  1278. .resume = usb_generic_resume,
  1279. };
  1280. #ifndef MODULE
  1281. static int __init usb_setup_disable(char *str)
  1282. {
  1283. nousb = 1;
  1284. return 1;
  1285. }
  1286. /* format to disable USB on kernel command line is: nousb */
  1287. __setup("nousb", usb_setup_disable);
  1288. #endif
  1289. /*
  1290. * for external read access to <nousb>
  1291. */
  1292. int usb_disabled(void)
  1293. {
  1294. return nousb;
  1295. }
  1296. /*
  1297. * Init
  1298. */
  1299. static int __init usb_init(void)
  1300. {
  1301. int retval;
  1302. if (nousb) {
  1303. pr_info ("%s: USB support disabled\n", usbcore_name);
  1304. return 0;
  1305. }
  1306. retval = bus_register(&usb_bus_type);
  1307. if (retval)
  1308. goto out;
  1309. retval = usb_host_init();
  1310. if (retval)
  1311. goto host_init_failed;
  1312. retval = usb_major_init();
  1313. if (retval)
  1314. goto major_init_failed;
  1315. retval = usb_register(&usbfs_driver);
  1316. if (retval)
  1317. goto driver_register_failed;
  1318. retval = usbdev_init();
  1319. if (retval)
  1320. goto usbdevice_init_failed;
  1321. retval = usbfs_init();
  1322. if (retval)
  1323. goto fs_init_failed;
  1324. retval = usb_hub_init();
  1325. if (retval)
  1326. goto hub_init_failed;
  1327. retval = driver_register(&usb_generic_driver);
  1328. if (!retval)
  1329. goto out;
  1330. usb_hub_cleanup();
  1331. hub_init_failed:
  1332. usbfs_cleanup();
  1333. fs_init_failed:
  1334. usbdev_cleanup();
  1335. usbdevice_init_failed:
  1336. usb_deregister(&usbfs_driver);
  1337. driver_register_failed:
  1338. usb_major_cleanup();
  1339. major_init_failed:
  1340. usb_host_cleanup();
  1341. host_init_failed:
  1342. bus_unregister(&usb_bus_type);
  1343. out:
  1344. return retval;
  1345. }
  1346. /*
  1347. * Cleanup
  1348. */
  1349. static void __exit usb_exit(void)
  1350. {
  1351. /* This will matter if shutdown/reboot does exitcalls. */
  1352. if (nousb)
  1353. return;
  1354. driver_unregister(&usb_generic_driver);
  1355. usb_major_cleanup();
  1356. usbfs_cleanup();
  1357. usb_deregister(&usbfs_driver);
  1358. usbdev_cleanup();
  1359. usb_hub_cleanup();
  1360. usb_host_cleanup();
  1361. bus_unregister(&usb_bus_type);
  1362. }
  1363. subsys_initcall(usb_init);
  1364. module_exit(usb_exit);
  1365. /*
  1366. * USB may be built into the kernel or be built as modules.
  1367. * These symbols are exported for device (or host controller)
  1368. * driver modules to use.
  1369. */
  1370. EXPORT_SYMBOL(usb_register);
  1371. EXPORT_SYMBOL(usb_deregister);
  1372. EXPORT_SYMBOL(usb_disabled);
  1373. EXPORT_SYMBOL_GPL(usb_get_intf);
  1374. EXPORT_SYMBOL_GPL(usb_put_intf);
  1375. EXPORT_SYMBOL(usb_alloc_dev);
  1376. EXPORT_SYMBOL(usb_put_dev);
  1377. EXPORT_SYMBOL(usb_get_dev);
  1378. EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
  1379. EXPORT_SYMBOL(usb_lock_device);
  1380. EXPORT_SYMBOL(usb_trylock_device);
  1381. EXPORT_SYMBOL(usb_lock_device_for_reset);
  1382. EXPORT_SYMBOL(usb_unlock_device);
  1383. EXPORT_SYMBOL(usb_driver_claim_interface);
  1384. EXPORT_SYMBOL(usb_driver_release_interface);
  1385. EXPORT_SYMBOL(usb_match_id);
  1386. EXPORT_SYMBOL(usb_find_interface);
  1387. EXPORT_SYMBOL(usb_ifnum_to_if);
  1388. EXPORT_SYMBOL(usb_altnum_to_altsetting);
  1389. EXPORT_SYMBOL(usb_reset_device);
  1390. EXPORT_SYMBOL(usb_disconnect);
  1391. EXPORT_SYMBOL(__usb_get_extra_descriptor);
  1392. EXPORT_SYMBOL(usb_find_device);
  1393. EXPORT_SYMBOL(usb_get_current_frame_number);
  1394. EXPORT_SYMBOL (usb_buffer_alloc);
  1395. EXPORT_SYMBOL (usb_buffer_free);
  1396. #if 0
  1397. EXPORT_SYMBOL (usb_buffer_map);
  1398. EXPORT_SYMBOL (usb_buffer_dmasync);
  1399. EXPORT_SYMBOL (usb_buffer_unmap);
  1400. #endif
  1401. EXPORT_SYMBOL (usb_buffer_map_sg);
  1402. #if 0
  1403. EXPORT_SYMBOL (usb_buffer_dmasync_sg);
  1404. #endif
  1405. EXPORT_SYMBOL (usb_buffer_unmap_sg);
  1406. MODULE_LICENSE("GPL");