hcd.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  1. /*
  2. * (C) Copyright Linus Torvalds 1999
  3. * (C) Copyright Johannes Erdfelt 1999-2001
  4. * (C) Copyright Andreas Gal 1999
  5. * (C) Copyright Gregory P. Smith 1999
  6. * (C) Copyright Deti Fliegl 1999
  7. * (C) Copyright Randy Dunlap 2000
  8. * (C) Copyright David Brownell 2000-2002
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. * for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/version.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/completion.h>
  29. #include <linux/utsname.h>
  30. #include <linux/mm.h>
  31. #include <asm/io.h>
  32. #include <linux/device.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/mutex.h>
  35. #include <asm/irq.h>
  36. #include <asm/byteorder.h>
  37. #include <asm/unaligned.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/mutex.h>
  41. #include <linux/pm_runtime.h>
  42. #include <linux/usb.h>
  43. #include "usb.h"
  44. #include "hcd.h"
  45. #include "hub.h"
  46. /*-------------------------------------------------------------------------*/
  47. /*
  48. * USB Host Controller Driver framework
  49. *
  50. * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
  51. * HCD-specific behaviors/bugs.
  52. *
  53. * This does error checks, tracks devices and urbs, and delegates to a
  54. * "hc_driver" only for code (and data) that really needs to know about
  55. * hardware differences. That includes root hub registers, i/o queues,
  56. * and so on ... but as little else as possible.
  57. *
  58. * Shared code includes most of the "root hub" code (these are emulated,
  59. * though each HC's hardware works differently) and PCI glue, plus request
  60. * tracking overhead. The HCD code should only block on spinlocks or on
  61. * hardware handshaking; blocking on software events (such as other kernel
  62. * threads releasing resources, or completing actions) is all generic.
  63. *
  64. * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
  65. * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
  66. * only by the hub driver ... and that neither should be seen or used by
  67. * usb client device drivers.
  68. *
  69. * Contributors of ideas or unattributed patches include: David Brownell,
  70. * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
  71. *
  72. * HISTORY:
  73. * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
  74. * associated cleanup. "usb_hcd" still != "usb_bus".
  75. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  76. */
  77. /*-------------------------------------------------------------------------*/
  78. /* Keep track of which host controller drivers are loaded */
  79. unsigned long usb_hcds_loaded;
  80. EXPORT_SYMBOL_GPL(usb_hcds_loaded);
  81. /* host controllers we manage */
  82. LIST_HEAD (usb_bus_list);
  83. EXPORT_SYMBOL_GPL (usb_bus_list);
  84. /* used when allocating bus numbers */
  85. #define USB_MAXBUS 64
  86. struct usb_busmap {
  87. unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
  88. };
  89. static struct usb_busmap busmap;
  90. /* used when updating list of hcds */
  91. DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
  92. EXPORT_SYMBOL_GPL (usb_bus_list_lock);
  93. /* used for controlling access to virtual root hubs */
  94. static DEFINE_SPINLOCK(hcd_root_hub_lock);
  95. /* used when updating an endpoint's URB list */
  96. static DEFINE_SPINLOCK(hcd_urb_list_lock);
  97. /* used to protect against unlinking URBs after the device is gone */
  98. static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
  99. /* wait queue for synchronous unlinks */
  100. DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
  101. static inline int is_root_hub(struct usb_device *udev)
  102. {
  103. return (udev->parent == NULL);
  104. }
  105. /*-------------------------------------------------------------------------*/
  106. /*
  107. * Sharable chunks of root hub code.
  108. */
  109. /*-------------------------------------------------------------------------*/
  110. #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
  111. #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
  112. /* usb 3.0 root hub device descriptor */
  113. static const u8 usb3_rh_dev_descriptor[18] = {
  114. 0x12, /* __u8 bLength; */
  115. 0x01, /* __u8 bDescriptorType; Device */
  116. 0x00, 0x03, /* __le16 bcdUSB; v3.0 */
  117. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  118. 0x00, /* __u8 bDeviceSubClass; */
  119. 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */
  120. 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
  121. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
  122. 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
  123. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  124. 0x03, /* __u8 iManufacturer; */
  125. 0x02, /* __u8 iProduct; */
  126. 0x01, /* __u8 iSerialNumber; */
  127. 0x01 /* __u8 bNumConfigurations; */
  128. };
  129. /* usb 2.0 root hub device descriptor */
  130. static const u8 usb2_rh_dev_descriptor [18] = {
  131. 0x12, /* __u8 bLength; */
  132. 0x01, /* __u8 bDescriptorType; Device */
  133. 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
  134. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  135. 0x00, /* __u8 bDeviceSubClass; */
  136. 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
  137. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  138. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
  139. 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
  140. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  141. 0x03, /* __u8 iManufacturer; */
  142. 0x02, /* __u8 iProduct; */
  143. 0x01, /* __u8 iSerialNumber; */
  144. 0x01 /* __u8 bNumConfigurations; */
  145. };
  146. /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
  147. /* usb 1.1 root hub device descriptor */
  148. static const u8 usb11_rh_dev_descriptor [18] = {
  149. 0x12, /* __u8 bLength; */
  150. 0x01, /* __u8 bDescriptorType; Device */
  151. 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
  152. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  153. 0x00, /* __u8 bDeviceSubClass; */
  154. 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
  155. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  156. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
  157. 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
  158. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  159. 0x03, /* __u8 iManufacturer; */
  160. 0x02, /* __u8 iProduct; */
  161. 0x01, /* __u8 iSerialNumber; */
  162. 0x01 /* __u8 bNumConfigurations; */
  163. };
  164. /*-------------------------------------------------------------------------*/
  165. /* Configuration descriptors for our root hubs */
  166. static const u8 fs_rh_config_descriptor [] = {
  167. /* one configuration */
  168. 0x09, /* __u8 bLength; */
  169. 0x02, /* __u8 bDescriptorType; Configuration */
  170. 0x19, 0x00, /* __le16 wTotalLength; */
  171. 0x01, /* __u8 bNumInterfaces; (1) */
  172. 0x01, /* __u8 bConfigurationValue; */
  173. 0x00, /* __u8 iConfiguration; */
  174. 0xc0, /* __u8 bmAttributes;
  175. Bit 7: must be set,
  176. 6: Self-powered,
  177. 5: Remote wakeup,
  178. 4..0: resvd */
  179. 0x00, /* __u8 MaxPower; */
  180. /* USB 1.1:
  181. * USB 2.0, single TT organization (mandatory):
  182. * one interface, protocol 0
  183. *
  184. * USB 2.0, multiple TT organization (optional):
  185. * two interfaces, protocols 1 (like single TT)
  186. * and 2 (multiple TT mode) ... config is
  187. * sometimes settable
  188. * NOT IMPLEMENTED
  189. */
  190. /* one interface */
  191. 0x09, /* __u8 if_bLength; */
  192. 0x04, /* __u8 if_bDescriptorType; Interface */
  193. 0x00, /* __u8 if_bInterfaceNumber; */
  194. 0x00, /* __u8 if_bAlternateSetting; */
  195. 0x01, /* __u8 if_bNumEndpoints; */
  196. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  197. 0x00, /* __u8 if_bInterfaceSubClass; */
  198. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  199. 0x00, /* __u8 if_iInterface; */
  200. /* one endpoint (status change endpoint) */
  201. 0x07, /* __u8 ep_bLength; */
  202. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  203. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  204. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  205. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  206. 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  207. };
  208. static const u8 hs_rh_config_descriptor [] = {
  209. /* one configuration */
  210. 0x09, /* __u8 bLength; */
  211. 0x02, /* __u8 bDescriptorType; Configuration */
  212. 0x19, 0x00, /* __le16 wTotalLength; */
  213. 0x01, /* __u8 bNumInterfaces; (1) */
  214. 0x01, /* __u8 bConfigurationValue; */
  215. 0x00, /* __u8 iConfiguration; */
  216. 0xc0, /* __u8 bmAttributes;
  217. Bit 7: must be set,
  218. 6: Self-powered,
  219. 5: Remote wakeup,
  220. 4..0: resvd */
  221. 0x00, /* __u8 MaxPower; */
  222. /* USB 1.1:
  223. * USB 2.0, single TT organization (mandatory):
  224. * one interface, protocol 0
  225. *
  226. * USB 2.0, multiple TT organization (optional):
  227. * two interfaces, protocols 1 (like single TT)
  228. * and 2 (multiple TT mode) ... config is
  229. * sometimes settable
  230. * NOT IMPLEMENTED
  231. */
  232. /* one interface */
  233. 0x09, /* __u8 if_bLength; */
  234. 0x04, /* __u8 if_bDescriptorType; Interface */
  235. 0x00, /* __u8 if_bInterfaceNumber; */
  236. 0x00, /* __u8 if_bAlternateSetting; */
  237. 0x01, /* __u8 if_bNumEndpoints; */
  238. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  239. 0x00, /* __u8 if_bInterfaceSubClass; */
  240. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  241. 0x00, /* __u8 if_iInterface; */
  242. /* one endpoint (status change endpoint) */
  243. 0x07, /* __u8 ep_bLength; */
  244. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  245. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  246. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  247. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  248. * see hub.c:hub_configure() for details. */
  249. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  250. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  251. };
  252. static const u8 ss_rh_config_descriptor[] = {
  253. /* one configuration */
  254. 0x09, /* __u8 bLength; */
  255. 0x02, /* __u8 bDescriptorType; Configuration */
  256. 0x19, 0x00, /* __le16 wTotalLength; FIXME */
  257. 0x01, /* __u8 bNumInterfaces; (1) */
  258. 0x01, /* __u8 bConfigurationValue; */
  259. 0x00, /* __u8 iConfiguration; */
  260. 0xc0, /* __u8 bmAttributes;
  261. Bit 7: must be set,
  262. 6: Self-powered,
  263. 5: Remote wakeup,
  264. 4..0: resvd */
  265. 0x00, /* __u8 MaxPower; */
  266. /* one interface */
  267. 0x09, /* __u8 if_bLength; */
  268. 0x04, /* __u8 if_bDescriptorType; Interface */
  269. 0x00, /* __u8 if_bInterfaceNumber; */
  270. 0x00, /* __u8 if_bAlternateSetting; */
  271. 0x01, /* __u8 if_bNumEndpoints; */
  272. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  273. 0x00, /* __u8 if_bInterfaceSubClass; */
  274. 0x00, /* __u8 if_bInterfaceProtocol; */
  275. 0x00, /* __u8 if_iInterface; */
  276. /* one endpoint (status change endpoint) */
  277. 0x07, /* __u8 ep_bLength; */
  278. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  279. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  280. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  281. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  282. * see hub.c:hub_configure() for details. */
  283. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  284. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  285. /*
  286. * All 3.0 hubs should have an endpoint companion descriptor,
  287. * but we're ignoring that for now. FIXME?
  288. */
  289. };
  290. /*-------------------------------------------------------------------------*/
  291. /**
  292. * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
  293. * @s: Null-terminated ASCII (actually ISO-8859-1) string
  294. * @buf: Buffer for USB string descriptor (header + UTF-16LE)
  295. * @len: Length (in bytes; may be odd) of descriptor buffer.
  296. *
  297. * The return value is the number of bytes filled in: 2 + 2*strlen(s) or
  298. * buflen, whichever is less.
  299. *
  300. * USB String descriptors can contain at most 126 characters; input
  301. * strings longer than that are truncated.
  302. */
  303. static unsigned
  304. ascii2desc(char const *s, u8 *buf, unsigned len)
  305. {
  306. unsigned n, t = 2 + 2*strlen(s);
  307. if (t > 254)
  308. t = 254; /* Longest possible UTF string descriptor */
  309. if (len > t)
  310. len = t;
  311. t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */
  312. n = len;
  313. while (n--) {
  314. *buf++ = t;
  315. if (!n--)
  316. break;
  317. *buf++ = t >> 8;
  318. t = (unsigned char)*s++;
  319. }
  320. return len;
  321. }
  322. /**
  323. * rh_string() - provides string descriptors for root hub
  324. * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
  325. * @hcd: the host controller for this root hub
  326. * @data: buffer for output packet
  327. * @len: length of the provided buffer
  328. *
  329. * Produces either a manufacturer, product or serial number string for the
  330. * virtual root hub device.
  331. * Returns the number of bytes filled in: the length of the descriptor or
  332. * of the provided buffer, whichever is less.
  333. */
  334. static unsigned
  335. rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
  336. {
  337. char buf[100];
  338. char const *s;
  339. static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
  340. // language ids
  341. switch (id) {
  342. case 0:
  343. /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
  344. /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
  345. if (len > 4)
  346. len = 4;
  347. memcpy(data, langids, len);
  348. return len;
  349. case 1:
  350. /* Serial number */
  351. s = hcd->self.bus_name;
  352. break;
  353. case 2:
  354. /* Product name */
  355. s = hcd->product_desc;
  356. break;
  357. case 3:
  358. /* Manufacturer */
  359. snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
  360. init_utsname()->release, hcd->driver->description);
  361. s = buf;
  362. break;
  363. default:
  364. /* Can't happen; caller guarantees it */
  365. return 0;
  366. }
  367. return ascii2desc(s, data, len);
  368. }
  369. /* Root hub control transfers execute synchronously */
  370. static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
  371. {
  372. struct usb_ctrlrequest *cmd;
  373. u16 typeReq, wValue, wIndex, wLength;
  374. u8 *ubuf = urb->transfer_buffer;
  375. u8 tbuf [sizeof (struct usb_hub_descriptor)]
  376. __attribute__((aligned(4)));
  377. const u8 *bufp = tbuf;
  378. unsigned len = 0;
  379. int status;
  380. u8 patch_wakeup = 0;
  381. u8 patch_protocol = 0;
  382. might_sleep();
  383. spin_lock_irq(&hcd_root_hub_lock);
  384. status = usb_hcd_link_urb_to_ep(hcd, urb);
  385. spin_unlock_irq(&hcd_root_hub_lock);
  386. if (status)
  387. return status;
  388. urb->hcpriv = hcd; /* Indicate it's queued */
  389. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  390. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  391. wValue = le16_to_cpu (cmd->wValue);
  392. wIndex = le16_to_cpu (cmd->wIndex);
  393. wLength = le16_to_cpu (cmd->wLength);
  394. if (wLength > urb->transfer_buffer_length)
  395. goto error;
  396. urb->actual_length = 0;
  397. switch (typeReq) {
  398. /* DEVICE REQUESTS */
  399. /* The root hub's remote wakeup enable bit is implemented using
  400. * driver model wakeup flags. If this system supports wakeup
  401. * through USB, userspace may change the default "allow wakeup"
  402. * policy through sysfs or these calls.
  403. *
  404. * Most root hubs support wakeup from downstream devices, for
  405. * runtime power management (disabling USB clocks and reducing
  406. * VBUS power usage). However, not all of them do so; silicon,
  407. * board, and BIOS bugs here are not uncommon, so these can't
  408. * be treated quite like external hubs.
  409. *
  410. * Likewise, not all root hubs will pass wakeup events upstream,
  411. * to wake up the whole system. So don't assume root hub and
  412. * controller capabilities are identical.
  413. */
  414. case DeviceRequest | USB_REQ_GET_STATUS:
  415. tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
  416. << USB_DEVICE_REMOTE_WAKEUP)
  417. | (1 << USB_DEVICE_SELF_POWERED);
  418. tbuf [1] = 0;
  419. len = 2;
  420. break;
  421. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  422. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  423. device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
  424. else
  425. goto error;
  426. break;
  427. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  428. if (device_can_wakeup(&hcd->self.root_hub->dev)
  429. && wValue == USB_DEVICE_REMOTE_WAKEUP)
  430. device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
  431. else
  432. goto error;
  433. break;
  434. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  435. tbuf [0] = 1;
  436. len = 1;
  437. /* FALLTHROUGH */
  438. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  439. break;
  440. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  441. switch (wValue & 0xff00) {
  442. case USB_DT_DEVICE << 8:
  443. switch (hcd->driver->flags & HCD_MASK) {
  444. case HCD_USB3:
  445. bufp = usb3_rh_dev_descriptor;
  446. break;
  447. case HCD_USB2:
  448. bufp = usb2_rh_dev_descriptor;
  449. break;
  450. case HCD_USB11:
  451. bufp = usb11_rh_dev_descriptor;
  452. break;
  453. default:
  454. goto error;
  455. }
  456. len = 18;
  457. if (hcd->has_tt)
  458. patch_protocol = 1;
  459. break;
  460. case USB_DT_CONFIG << 8:
  461. switch (hcd->driver->flags & HCD_MASK) {
  462. case HCD_USB3:
  463. bufp = ss_rh_config_descriptor;
  464. len = sizeof ss_rh_config_descriptor;
  465. break;
  466. case HCD_USB2:
  467. bufp = hs_rh_config_descriptor;
  468. len = sizeof hs_rh_config_descriptor;
  469. break;
  470. case HCD_USB11:
  471. bufp = fs_rh_config_descriptor;
  472. len = sizeof fs_rh_config_descriptor;
  473. break;
  474. default:
  475. goto error;
  476. }
  477. if (device_can_wakeup(&hcd->self.root_hub->dev))
  478. patch_wakeup = 1;
  479. break;
  480. case USB_DT_STRING << 8:
  481. if ((wValue & 0xff) < 4)
  482. urb->actual_length = rh_string(wValue & 0xff,
  483. hcd, ubuf, wLength);
  484. else /* unsupported IDs --> "protocol stall" */
  485. goto error;
  486. break;
  487. default:
  488. goto error;
  489. }
  490. break;
  491. case DeviceRequest | USB_REQ_GET_INTERFACE:
  492. tbuf [0] = 0;
  493. len = 1;
  494. /* FALLTHROUGH */
  495. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  496. break;
  497. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  498. // wValue == urb->dev->devaddr
  499. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  500. wValue);
  501. break;
  502. /* INTERFACE REQUESTS (no defined feature/status flags) */
  503. /* ENDPOINT REQUESTS */
  504. case EndpointRequest | USB_REQ_GET_STATUS:
  505. // ENDPOINT_HALT flag
  506. tbuf [0] = 0;
  507. tbuf [1] = 0;
  508. len = 2;
  509. /* FALLTHROUGH */
  510. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  511. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  512. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  513. break;
  514. /* CLASS REQUESTS (and errors) */
  515. default:
  516. /* non-generic request */
  517. switch (typeReq) {
  518. case GetHubStatus:
  519. case GetPortStatus:
  520. len = 4;
  521. break;
  522. case GetHubDescriptor:
  523. len = sizeof (struct usb_hub_descriptor);
  524. break;
  525. }
  526. status = hcd->driver->hub_control (hcd,
  527. typeReq, wValue, wIndex,
  528. tbuf, wLength);
  529. break;
  530. error:
  531. /* "protocol stall" on error */
  532. status = -EPIPE;
  533. }
  534. if (status) {
  535. len = 0;
  536. if (status != -EPIPE) {
  537. dev_dbg (hcd->self.controller,
  538. "CTRL: TypeReq=0x%x val=0x%x "
  539. "idx=0x%x len=%d ==> %d\n",
  540. typeReq, wValue, wIndex,
  541. wLength, status);
  542. }
  543. }
  544. if (len) {
  545. if (urb->transfer_buffer_length < len)
  546. len = urb->transfer_buffer_length;
  547. urb->actual_length = len;
  548. // always USB_DIR_IN, toward host
  549. memcpy (ubuf, bufp, len);
  550. /* report whether RH hardware supports remote wakeup */
  551. if (patch_wakeup &&
  552. len > offsetof (struct usb_config_descriptor,
  553. bmAttributes))
  554. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  555. |= USB_CONFIG_ATT_WAKEUP;
  556. /* report whether RH hardware has an integrated TT */
  557. if (patch_protocol &&
  558. len > offsetof(struct usb_device_descriptor,
  559. bDeviceProtocol))
  560. ((struct usb_device_descriptor *) ubuf)->
  561. bDeviceProtocol = 1;
  562. }
  563. /* any errors get returned through the urb completion */
  564. spin_lock_irq(&hcd_root_hub_lock);
  565. usb_hcd_unlink_urb_from_ep(hcd, urb);
  566. /* This peculiar use of spinlocks echoes what real HC drivers do.
  567. * Avoiding calls to local_irq_disable/enable makes the code
  568. * RT-friendly.
  569. */
  570. spin_unlock(&hcd_root_hub_lock);
  571. usb_hcd_giveback_urb(hcd, urb, status);
  572. spin_lock(&hcd_root_hub_lock);
  573. spin_unlock_irq(&hcd_root_hub_lock);
  574. return 0;
  575. }
  576. /*-------------------------------------------------------------------------*/
  577. /*
  578. * Root Hub interrupt transfers are polled using a timer if the
  579. * driver requests it; otherwise the driver is responsible for
  580. * calling usb_hcd_poll_rh_status() when an event occurs.
  581. *
  582. * Completions are called in_interrupt(), but they may or may not
  583. * be in_irq().
  584. */
  585. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  586. {
  587. struct urb *urb;
  588. int length;
  589. unsigned long flags;
  590. char buffer[6]; /* Any root hubs with > 31 ports? */
  591. if (unlikely(!hcd->rh_registered))
  592. return;
  593. if (!hcd->uses_new_polling && !hcd->status_urb)
  594. return;
  595. length = hcd->driver->hub_status_data(hcd, buffer);
  596. if (length > 0) {
  597. /* try to complete the status urb */
  598. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  599. urb = hcd->status_urb;
  600. if (urb) {
  601. hcd->poll_pending = 0;
  602. hcd->status_urb = NULL;
  603. urb->actual_length = length;
  604. memcpy(urb->transfer_buffer, buffer, length);
  605. usb_hcd_unlink_urb_from_ep(hcd, urb);
  606. spin_unlock(&hcd_root_hub_lock);
  607. usb_hcd_giveback_urb(hcd, urb, 0);
  608. spin_lock(&hcd_root_hub_lock);
  609. } else {
  610. length = 0;
  611. hcd->poll_pending = 1;
  612. }
  613. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  614. }
  615. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  616. * exceed that limit if HZ is 100. The math is more clunky than
  617. * maybe expected, this is to make sure that all timers for USB devices
  618. * fire at the same time to give the CPU a break inbetween */
  619. if (hcd->uses_new_polling ? hcd->poll_rh :
  620. (length == 0 && hcd->status_urb != NULL))
  621. mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  622. }
  623. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  624. /* timer callback */
  625. static void rh_timer_func (unsigned long _hcd)
  626. {
  627. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  628. }
  629. /*-------------------------------------------------------------------------*/
  630. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  631. {
  632. int retval;
  633. unsigned long flags;
  634. unsigned len = 1 + (urb->dev->maxchild / 8);
  635. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  636. if (hcd->status_urb || urb->transfer_buffer_length < len) {
  637. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  638. retval = -EINVAL;
  639. goto done;
  640. }
  641. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  642. if (retval)
  643. goto done;
  644. hcd->status_urb = urb;
  645. urb->hcpriv = hcd; /* indicate it's queued */
  646. if (!hcd->uses_new_polling)
  647. mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  648. /* If a status change has already occurred, report it ASAP */
  649. else if (hcd->poll_pending)
  650. mod_timer(&hcd->rh_timer, jiffies);
  651. retval = 0;
  652. done:
  653. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  654. return retval;
  655. }
  656. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  657. {
  658. if (usb_endpoint_xfer_int(&urb->ep->desc))
  659. return rh_queue_status (hcd, urb);
  660. if (usb_endpoint_xfer_control(&urb->ep->desc))
  661. return rh_call_control (hcd, urb);
  662. return -EINVAL;
  663. }
  664. /*-------------------------------------------------------------------------*/
  665. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  666. * since these URBs always execute synchronously.
  667. */
  668. static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  669. {
  670. unsigned long flags;
  671. int rc;
  672. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  673. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  674. if (rc)
  675. goto done;
  676. if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
  677. ; /* Do nothing */
  678. } else { /* Status URB */
  679. if (!hcd->uses_new_polling)
  680. del_timer (&hcd->rh_timer);
  681. if (urb == hcd->status_urb) {
  682. hcd->status_urb = NULL;
  683. usb_hcd_unlink_urb_from_ep(hcd, urb);
  684. spin_unlock(&hcd_root_hub_lock);
  685. usb_hcd_giveback_urb(hcd, urb, status);
  686. spin_lock(&hcd_root_hub_lock);
  687. }
  688. }
  689. done:
  690. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  691. return rc;
  692. }
  693. /*
  694. * Show & store the current value of authorized_default
  695. */
  696. static ssize_t usb_host_authorized_default_show(struct device *dev,
  697. struct device_attribute *attr,
  698. char *buf)
  699. {
  700. struct usb_device *rh_usb_dev = to_usb_device(dev);
  701. struct usb_bus *usb_bus = rh_usb_dev->bus;
  702. struct usb_hcd *usb_hcd;
  703. if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
  704. return -ENODEV;
  705. usb_hcd = bus_to_hcd(usb_bus);
  706. return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default);
  707. }
  708. static ssize_t usb_host_authorized_default_store(struct device *dev,
  709. struct device_attribute *attr,
  710. const char *buf, size_t size)
  711. {
  712. ssize_t result;
  713. unsigned val;
  714. struct usb_device *rh_usb_dev = to_usb_device(dev);
  715. struct usb_bus *usb_bus = rh_usb_dev->bus;
  716. struct usb_hcd *usb_hcd;
  717. if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
  718. return -ENODEV;
  719. usb_hcd = bus_to_hcd(usb_bus);
  720. result = sscanf(buf, "%u\n", &val);
  721. if (result == 1) {
  722. usb_hcd->authorized_default = val? 1 : 0;
  723. result = size;
  724. }
  725. else
  726. result = -EINVAL;
  727. return result;
  728. }
  729. static DEVICE_ATTR(authorized_default, 0644,
  730. usb_host_authorized_default_show,
  731. usb_host_authorized_default_store);
  732. /* Group all the USB bus attributes */
  733. static struct attribute *usb_bus_attrs[] = {
  734. &dev_attr_authorized_default.attr,
  735. NULL,
  736. };
  737. static struct attribute_group usb_bus_attr_group = {
  738. .name = NULL, /* we want them in the same directory */
  739. .attrs = usb_bus_attrs,
  740. };
  741. /*-------------------------------------------------------------------------*/
  742. /**
  743. * usb_bus_init - shared initialization code
  744. * @bus: the bus structure being initialized
  745. *
  746. * This code is used to initialize a usb_bus structure, memory for which is
  747. * separately managed.
  748. */
  749. static void usb_bus_init (struct usb_bus *bus)
  750. {
  751. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  752. bus->devnum_next = 1;
  753. bus->root_hub = NULL;
  754. bus->busnum = -1;
  755. bus->bandwidth_allocated = 0;
  756. bus->bandwidth_int_reqs = 0;
  757. bus->bandwidth_isoc_reqs = 0;
  758. INIT_LIST_HEAD (&bus->bus_list);
  759. }
  760. /*-------------------------------------------------------------------------*/
  761. /**
  762. * usb_register_bus - registers the USB host controller with the usb core
  763. * @bus: pointer to the bus to register
  764. * Context: !in_interrupt()
  765. *
  766. * Assigns a bus number, and links the controller into usbcore data
  767. * structures so that it can be seen by scanning the bus list.
  768. */
  769. static int usb_register_bus(struct usb_bus *bus)
  770. {
  771. int result = -E2BIG;
  772. int busnum;
  773. mutex_lock(&usb_bus_list_lock);
  774. busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  775. if (busnum >= USB_MAXBUS) {
  776. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  777. goto error_find_busnum;
  778. }
  779. set_bit (busnum, busmap.busmap);
  780. bus->busnum = busnum;
  781. /* Add it to the local list of buses */
  782. list_add (&bus->bus_list, &usb_bus_list);
  783. mutex_unlock(&usb_bus_list_lock);
  784. usb_notify_add_bus(bus);
  785. dev_info (bus->controller, "new USB bus registered, assigned bus "
  786. "number %d\n", bus->busnum);
  787. return 0;
  788. error_find_busnum:
  789. mutex_unlock(&usb_bus_list_lock);
  790. return result;
  791. }
  792. /**
  793. * usb_deregister_bus - deregisters the USB host controller
  794. * @bus: pointer to the bus to deregister
  795. * Context: !in_interrupt()
  796. *
  797. * Recycles the bus number, and unlinks the controller from usbcore data
  798. * structures so that it won't be seen by scanning the bus list.
  799. */
  800. static void usb_deregister_bus (struct usb_bus *bus)
  801. {
  802. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  803. /*
  804. * NOTE: make sure that all the devices are removed by the
  805. * controller code, as well as having it call this when cleaning
  806. * itself up
  807. */
  808. mutex_lock(&usb_bus_list_lock);
  809. list_del (&bus->bus_list);
  810. mutex_unlock(&usb_bus_list_lock);
  811. usb_notify_remove_bus(bus);
  812. clear_bit (bus->busnum, busmap.busmap);
  813. }
  814. /**
  815. * register_root_hub - called by usb_add_hcd() to register a root hub
  816. * @hcd: host controller for this root hub
  817. *
  818. * This function registers the root hub with the USB subsystem. It sets up
  819. * the device properly in the device tree and then calls usb_new_device()
  820. * to register the usb device. It also assigns the root hub's USB address
  821. * (always 1).
  822. */
  823. static int register_root_hub(struct usb_hcd *hcd)
  824. {
  825. struct device *parent_dev = hcd->self.controller;
  826. struct usb_device *usb_dev = hcd->self.root_hub;
  827. const int devnum = 1;
  828. int retval;
  829. usb_dev->devnum = devnum;
  830. usb_dev->bus->devnum_next = devnum + 1;
  831. memset (&usb_dev->bus->devmap.devicemap, 0,
  832. sizeof usb_dev->bus->devmap.devicemap);
  833. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  834. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  835. mutex_lock(&usb_bus_list_lock);
  836. usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  837. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  838. if (retval != sizeof usb_dev->descriptor) {
  839. mutex_unlock(&usb_bus_list_lock);
  840. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  841. dev_name(&usb_dev->dev), retval);
  842. return (retval < 0) ? retval : -EMSGSIZE;
  843. }
  844. retval = usb_new_device (usb_dev);
  845. if (retval) {
  846. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  847. dev_name(&usb_dev->dev), retval);
  848. }
  849. mutex_unlock(&usb_bus_list_lock);
  850. if (retval == 0) {
  851. spin_lock_irq (&hcd_root_hub_lock);
  852. hcd->rh_registered = 1;
  853. spin_unlock_irq (&hcd_root_hub_lock);
  854. /* Did the HC die before the root hub was registered? */
  855. if (hcd->state == HC_STATE_HALT)
  856. usb_hc_died (hcd); /* This time clean up */
  857. }
  858. return retval;
  859. }
  860. /*-------------------------------------------------------------------------*/
  861. /**
  862. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  863. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  864. * @is_input: true iff the transaction sends data to the host
  865. * @isoc: true for isochronous transactions, false for interrupt ones
  866. * @bytecount: how many bytes in the transaction.
  867. *
  868. * Returns approximate bus time in nanoseconds for a periodic transaction.
  869. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  870. * scheduled in software, this function is only used for such scheduling.
  871. */
  872. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  873. {
  874. unsigned long tmp;
  875. switch (speed) {
  876. case USB_SPEED_LOW: /* INTR only */
  877. if (is_input) {
  878. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  879. return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  880. } else {
  881. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  882. return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  883. }
  884. case USB_SPEED_FULL: /* ISOC or INTR */
  885. if (isoc) {
  886. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  887. return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
  888. } else {
  889. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  890. return (9107L + BW_HOST_DELAY + tmp);
  891. }
  892. case USB_SPEED_HIGH: /* ISOC or INTR */
  893. // FIXME adjust for input vs output
  894. if (isoc)
  895. tmp = HS_NSECS_ISO (bytecount);
  896. else
  897. tmp = HS_NSECS (bytecount);
  898. return tmp;
  899. default:
  900. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  901. return -1;
  902. }
  903. }
  904. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  905. /*-------------------------------------------------------------------------*/
  906. /*
  907. * Generic HC operations.
  908. */
  909. /*-------------------------------------------------------------------------*/
  910. /**
  911. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  912. * @hcd: host controller to which @urb was submitted
  913. * @urb: URB being submitted
  914. *
  915. * Host controller drivers should call this routine in their enqueue()
  916. * method. The HCD's private spinlock must be held and interrupts must
  917. * be disabled. The actions carried out here are required for URB
  918. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  919. *
  920. * Returns 0 for no error, otherwise a negative error code (in which case
  921. * the enqueue() method must fail). If no error occurs but enqueue() fails
  922. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  923. * the private spinlock and returning.
  924. */
  925. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  926. {
  927. int rc = 0;
  928. spin_lock(&hcd_urb_list_lock);
  929. /* Check that the URB isn't being killed */
  930. if (unlikely(atomic_read(&urb->reject))) {
  931. rc = -EPERM;
  932. goto done;
  933. }
  934. if (unlikely(!urb->ep->enabled)) {
  935. rc = -ENOENT;
  936. goto done;
  937. }
  938. if (unlikely(!urb->dev->can_submit)) {
  939. rc = -EHOSTUNREACH;
  940. goto done;
  941. }
  942. /*
  943. * Check the host controller's state and add the URB to the
  944. * endpoint's queue.
  945. */
  946. switch (hcd->state) {
  947. case HC_STATE_RUNNING:
  948. case HC_STATE_RESUMING:
  949. urb->unlinked = 0;
  950. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  951. break;
  952. default:
  953. rc = -ESHUTDOWN;
  954. goto done;
  955. }
  956. done:
  957. spin_unlock(&hcd_urb_list_lock);
  958. return rc;
  959. }
  960. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  961. /**
  962. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  963. * @hcd: host controller to which @urb was submitted
  964. * @urb: URB being checked for unlinkability
  965. * @status: error code to store in @urb if the unlink succeeds
  966. *
  967. * Host controller drivers should call this routine in their dequeue()
  968. * method. The HCD's private spinlock must be held and interrupts must
  969. * be disabled. The actions carried out here are required for making
  970. * sure than an unlink is valid.
  971. *
  972. * Returns 0 for no error, otherwise a negative error code (in which case
  973. * the dequeue() method must fail). The possible error codes are:
  974. *
  975. * -EIDRM: @urb was not submitted or has already completed.
  976. * The completion function may not have been called yet.
  977. *
  978. * -EBUSY: @urb has already been unlinked.
  979. */
  980. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  981. int status)
  982. {
  983. struct list_head *tmp;
  984. /* insist the urb is still queued */
  985. list_for_each(tmp, &urb->ep->urb_list) {
  986. if (tmp == &urb->urb_list)
  987. break;
  988. }
  989. if (tmp != &urb->urb_list)
  990. return -EIDRM;
  991. /* Any status except -EINPROGRESS means something already started to
  992. * unlink this URB from the hardware. So there's no more work to do.
  993. */
  994. if (urb->unlinked)
  995. return -EBUSY;
  996. urb->unlinked = status;
  997. /* IRQ setup can easily be broken so that USB controllers
  998. * never get completion IRQs ... maybe even the ones we need to
  999. * finish unlinking the initial failed usb_set_address()
  1000. * or device descriptor fetch.
  1001. */
  1002. if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) &&
  1003. !is_root_hub(urb->dev)) {
  1004. dev_warn(hcd->self.controller, "Unlink after no-IRQ? "
  1005. "Controller is probably using the wrong IRQ.\n");
  1006. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1007. }
  1008. return 0;
  1009. }
  1010. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  1011. /**
  1012. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  1013. * @hcd: host controller to which @urb was submitted
  1014. * @urb: URB being unlinked
  1015. *
  1016. * Host controller drivers should call this routine before calling
  1017. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  1018. * interrupts must be disabled. The actions carried out here are required
  1019. * for URB completion.
  1020. */
  1021. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  1022. {
  1023. /* clear all state linking urb to this dev (and hcd) */
  1024. spin_lock(&hcd_urb_list_lock);
  1025. list_del_init(&urb->urb_list);
  1026. spin_unlock(&hcd_urb_list_lock);
  1027. }
  1028. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  1029. /*
  1030. * Some usb host controllers can only perform dma using a small SRAM area.
  1031. * The usb core itself is however optimized for host controllers that can dma
  1032. * using regular system memory - like pci devices doing bus mastering.
  1033. *
  1034. * To support host controllers with limited dma capabilites we provide dma
  1035. * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
  1036. * For this to work properly the host controller code must first use the
  1037. * function dma_declare_coherent_memory() to point out which memory area
  1038. * that should be used for dma allocations.
  1039. *
  1040. * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
  1041. * dma using dma_alloc_coherent() which in turn allocates from the memory
  1042. * area pointed out with dma_declare_coherent_memory().
  1043. *
  1044. * So, to summarize...
  1045. *
  1046. * - We need "local" memory, canonical example being
  1047. * a small SRAM on a discrete controller being the
  1048. * only memory that the controller can read ...
  1049. * (a) "normal" kernel memory is no good, and
  1050. * (b) there's not enough to share
  1051. *
  1052. * - The only *portable* hook for such stuff in the
  1053. * DMA framework is dma_declare_coherent_memory()
  1054. *
  1055. * - So we use that, even though the primary requirement
  1056. * is that the memory be "local" (hence addressible
  1057. * by that device), not "coherent".
  1058. *
  1059. */
  1060. static int hcd_alloc_coherent(struct usb_bus *bus,
  1061. gfp_t mem_flags, dma_addr_t *dma_handle,
  1062. void **vaddr_handle, size_t size,
  1063. enum dma_data_direction dir)
  1064. {
  1065. unsigned char *vaddr;
  1066. vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
  1067. mem_flags, dma_handle);
  1068. if (!vaddr)
  1069. return -ENOMEM;
  1070. /*
  1071. * Store the virtual address of the buffer at the end
  1072. * of the allocated dma buffer. The size of the buffer
  1073. * may be uneven so use unaligned functions instead
  1074. * of just rounding up. It makes sense to optimize for
  1075. * memory footprint over access speed since the amount
  1076. * of memory available for dma may be limited.
  1077. */
  1078. put_unaligned((unsigned long)*vaddr_handle,
  1079. (unsigned long *)(vaddr + size));
  1080. if (dir == DMA_TO_DEVICE)
  1081. memcpy(vaddr, *vaddr_handle, size);
  1082. *vaddr_handle = vaddr;
  1083. return 0;
  1084. }
  1085. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1086. void **vaddr_handle, size_t size,
  1087. enum dma_data_direction dir)
  1088. {
  1089. unsigned char *vaddr = *vaddr_handle;
  1090. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1091. if (dir == DMA_FROM_DEVICE)
  1092. memcpy(vaddr, *vaddr_handle, size);
  1093. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1094. *vaddr_handle = vaddr;
  1095. *dma_handle = 0;
  1096. }
  1097. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1098. gfp_t mem_flags)
  1099. {
  1100. enum dma_data_direction dir;
  1101. int ret = 0;
  1102. /* Map the URB's buffers for DMA access.
  1103. * Lower level HCD code should use *_dma exclusively,
  1104. * unless it uses pio or talks to another transport,
  1105. * or uses the provided scatter gather list for bulk.
  1106. */
  1107. if (is_root_hub(urb->dev))
  1108. return 0;
  1109. if (usb_endpoint_xfer_control(&urb->ep->desc)
  1110. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
  1111. if (hcd->self.uses_dma) {
  1112. urb->setup_dma = dma_map_single(
  1113. hcd->self.controller,
  1114. urb->setup_packet,
  1115. sizeof(struct usb_ctrlrequest),
  1116. DMA_TO_DEVICE);
  1117. if (dma_mapping_error(hcd->self.controller,
  1118. urb->setup_dma))
  1119. return -EAGAIN;
  1120. } else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1121. ret = hcd_alloc_coherent(
  1122. urb->dev->bus, mem_flags,
  1123. &urb->setup_dma,
  1124. (void **)&urb->setup_packet,
  1125. sizeof(struct usb_ctrlrequest),
  1126. DMA_TO_DEVICE);
  1127. }
  1128. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1129. if (ret == 0 && urb->transfer_buffer_length != 0
  1130. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1131. if (hcd->self.uses_dma) {
  1132. urb->transfer_dma = dma_map_single (
  1133. hcd->self.controller,
  1134. urb->transfer_buffer,
  1135. urb->transfer_buffer_length,
  1136. dir);
  1137. if (dma_mapping_error(hcd->self.controller,
  1138. urb->transfer_dma))
  1139. return -EAGAIN;
  1140. } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1141. ret = hcd_alloc_coherent(
  1142. urb->dev->bus, mem_flags,
  1143. &urb->transfer_dma,
  1144. &urb->transfer_buffer,
  1145. urb->transfer_buffer_length,
  1146. dir);
  1147. if (ret && usb_endpoint_xfer_control(&urb->ep->desc)
  1148. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1149. hcd_free_coherent(urb->dev->bus,
  1150. &urb->setup_dma,
  1151. (void **)&urb->setup_packet,
  1152. sizeof(struct usb_ctrlrequest),
  1153. DMA_TO_DEVICE);
  1154. }
  1155. }
  1156. return ret;
  1157. }
  1158. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1159. {
  1160. enum dma_data_direction dir;
  1161. if (is_root_hub(urb->dev))
  1162. return;
  1163. if (usb_endpoint_xfer_control(&urb->ep->desc)
  1164. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
  1165. if (hcd->self.uses_dma)
  1166. dma_unmap_single(hcd->self.controller, urb->setup_dma,
  1167. sizeof(struct usb_ctrlrequest),
  1168. DMA_TO_DEVICE);
  1169. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1170. hcd_free_coherent(urb->dev->bus, &urb->setup_dma,
  1171. (void **)&urb->setup_packet,
  1172. sizeof(struct usb_ctrlrequest),
  1173. DMA_TO_DEVICE);
  1174. }
  1175. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1176. if (urb->transfer_buffer_length != 0
  1177. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1178. if (hcd->self.uses_dma)
  1179. dma_unmap_single(hcd->self.controller,
  1180. urb->transfer_dma,
  1181. urb->transfer_buffer_length,
  1182. dir);
  1183. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1184. hcd_free_coherent(urb->dev->bus, &urb->transfer_dma,
  1185. &urb->transfer_buffer,
  1186. urb->transfer_buffer_length,
  1187. dir);
  1188. }
  1189. }
  1190. /*-------------------------------------------------------------------------*/
  1191. /* may be called in any context with a valid urb->dev usecount
  1192. * caller surrenders "ownership" of urb
  1193. * expects usb_submit_urb() to have sanity checked and conditioned all
  1194. * inputs in the urb
  1195. */
  1196. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1197. {
  1198. int status;
  1199. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1200. /* increment urb's reference count as part of giving it to the HCD
  1201. * (which will control it). HCD guarantees that it either returns
  1202. * an error or calls giveback(), but not both.
  1203. */
  1204. usb_get_urb(urb);
  1205. atomic_inc(&urb->use_count);
  1206. atomic_inc(&urb->dev->urbnum);
  1207. usbmon_urb_submit(&hcd->self, urb);
  1208. /* NOTE requirements on root-hub callers (usbfs and the hub
  1209. * driver, for now): URBs' urb->transfer_buffer must be
  1210. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1211. * they could clobber root hub response data. Also, control
  1212. * URBs must be submitted in process context with interrupts
  1213. * enabled.
  1214. */
  1215. status = map_urb_for_dma(hcd, urb, mem_flags);
  1216. if (unlikely(status)) {
  1217. usbmon_urb_submit_error(&hcd->self, urb, status);
  1218. goto error;
  1219. }
  1220. if (is_root_hub(urb->dev))
  1221. status = rh_urb_enqueue(hcd, urb);
  1222. else
  1223. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1224. if (unlikely(status)) {
  1225. usbmon_urb_submit_error(&hcd->self, urb, status);
  1226. unmap_urb_for_dma(hcd, urb);
  1227. error:
  1228. urb->hcpriv = NULL;
  1229. INIT_LIST_HEAD(&urb->urb_list);
  1230. atomic_dec(&urb->use_count);
  1231. atomic_dec(&urb->dev->urbnum);
  1232. if (atomic_read(&urb->reject))
  1233. wake_up(&usb_kill_urb_queue);
  1234. usb_put_urb(urb);
  1235. }
  1236. return status;
  1237. }
  1238. /*-------------------------------------------------------------------------*/
  1239. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1240. * off hardware queues (which may take a while) and returning it as
  1241. * soon as practical. we've already set up the urb's return status,
  1242. * but we can't know if the callback completed already.
  1243. */
  1244. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1245. {
  1246. int value;
  1247. if (is_root_hub(urb->dev))
  1248. value = usb_rh_urb_dequeue(hcd, urb, status);
  1249. else {
  1250. /* The only reason an HCD might fail this call is if
  1251. * it has not yet fully queued the urb to begin with.
  1252. * Such failures should be harmless. */
  1253. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1254. }
  1255. return value;
  1256. }
  1257. /*
  1258. * called in any context
  1259. *
  1260. * caller guarantees urb won't be recycled till both unlink()
  1261. * and the urb's completion function return
  1262. */
  1263. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1264. {
  1265. struct usb_hcd *hcd;
  1266. int retval = -EIDRM;
  1267. unsigned long flags;
  1268. /* Prevent the device and bus from going away while
  1269. * the unlink is carried out. If they are already gone
  1270. * then urb->use_count must be 0, since disconnected
  1271. * devices can't have any active URBs.
  1272. */
  1273. spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
  1274. if (atomic_read(&urb->use_count) > 0) {
  1275. retval = 0;
  1276. usb_get_dev(urb->dev);
  1277. }
  1278. spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
  1279. if (retval == 0) {
  1280. hcd = bus_to_hcd(urb->dev->bus);
  1281. retval = unlink1(hcd, urb, status);
  1282. usb_put_dev(urb->dev);
  1283. }
  1284. if (retval == 0)
  1285. retval = -EINPROGRESS;
  1286. else if (retval != -EIDRM && retval != -EBUSY)
  1287. dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
  1288. urb, retval);
  1289. return retval;
  1290. }
  1291. /*-------------------------------------------------------------------------*/
  1292. /**
  1293. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1294. * @hcd: host controller returning the URB
  1295. * @urb: urb being returned to the USB device driver.
  1296. * @status: completion status code for the URB.
  1297. * Context: in_interrupt()
  1298. *
  1299. * This hands the URB from HCD to its USB device driver, using its
  1300. * completion function. The HCD has freed all per-urb resources
  1301. * (and is done using urb->hcpriv). It also released all HCD locks;
  1302. * the device driver won't cause problems if it frees, modifies,
  1303. * or resubmits this URB.
  1304. *
  1305. * If @urb was unlinked, the value of @status will be overridden by
  1306. * @urb->unlinked. Erroneous short transfers are detected in case
  1307. * the HCD hasn't checked for them.
  1308. */
  1309. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1310. {
  1311. urb->hcpriv = NULL;
  1312. if (unlikely(urb->unlinked))
  1313. status = urb->unlinked;
  1314. else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1315. urb->actual_length < urb->transfer_buffer_length &&
  1316. !status))
  1317. status = -EREMOTEIO;
  1318. unmap_urb_for_dma(hcd, urb);
  1319. usbmon_urb_complete(&hcd->self, urb, status);
  1320. usb_unanchor_urb(urb);
  1321. /* pass ownership to the completion handler */
  1322. urb->status = status;
  1323. urb->complete (urb);
  1324. atomic_dec (&urb->use_count);
  1325. if (unlikely(atomic_read(&urb->reject)))
  1326. wake_up (&usb_kill_urb_queue);
  1327. usb_put_urb (urb);
  1328. }
  1329. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1330. /*-------------------------------------------------------------------------*/
  1331. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1332. * queue to drain completely. The caller must first insure that no more
  1333. * URBs can be submitted for this endpoint.
  1334. */
  1335. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1336. struct usb_host_endpoint *ep)
  1337. {
  1338. struct usb_hcd *hcd;
  1339. struct urb *urb;
  1340. if (!ep)
  1341. return;
  1342. might_sleep();
  1343. hcd = bus_to_hcd(udev->bus);
  1344. /* No more submits can occur */
  1345. spin_lock_irq(&hcd_urb_list_lock);
  1346. rescan:
  1347. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1348. int is_in;
  1349. if (urb->unlinked)
  1350. continue;
  1351. usb_get_urb (urb);
  1352. is_in = usb_urb_dir_in(urb);
  1353. spin_unlock(&hcd_urb_list_lock);
  1354. /* kick hcd */
  1355. unlink1(hcd, urb, -ESHUTDOWN);
  1356. dev_dbg (hcd->self.controller,
  1357. "shutdown urb %p ep%d%s%s\n",
  1358. urb, usb_endpoint_num(&ep->desc),
  1359. is_in ? "in" : "out",
  1360. ({ char *s;
  1361. switch (usb_endpoint_type(&ep->desc)) {
  1362. case USB_ENDPOINT_XFER_CONTROL:
  1363. s = ""; break;
  1364. case USB_ENDPOINT_XFER_BULK:
  1365. s = "-bulk"; break;
  1366. case USB_ENDPOINT_XFER_INT:
  1367. s = "-intr"; break;
  1368. default:
  1369. s = "-iso"; break;
  1370. };
  1371. s;
  1372. }));
  1373. usb_put_urb (urb);
  1374. /* list contents may have changed */
  1375. spin_lock(&hcd_urb_list_lock);
  1376. goto rescan;
  1377. }
  1378. spin_unlock_irq(&hcd_urb_list_lock);
  1379. /* Wait until the endpoint queue is completely empty */
  1380. while (!list_empty (&ep->urb_list)) {
  1381. spin_lock_irq(&hcd_urb_list_lock);
  1382. /* The list may have changed while we acquired the spinlock */
  1383. urb = NULL;
  1384. if (!list_empty (&ep->urb_list)) {
  1385. urb = list_entry (ep->urb_list.prev, struct urb,
  1386. urb_list);
  1387. usb_get_urb (urb);
  1388. }
  1389. spin_unlock_irq(&hcd_urb_list_lock);
  1390. if (urb) {
  1391. usb_kill_urb (urb);
  1392. usb_put_urb (urb);
  1393. }
  1394. }
  1395. }
  1396. /**
  1397. * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
  1398. * the bus bandwidth
  1399. * @udev: target &usb_device
  1400. * @new_config: new configuration to install
  1401. * @cur_alt: the current alternate interface setting
  1402. * @new_alt: alternate interface setting that is being installed
  1403. *
  1404. * To change configurations, pass in the new configuration in new_config,
  1405. * and pass NULL for cur_alt and new_alt.
  1406. *
  1407. * To reset a device's configuration (put the device in the ADDRESSED state),
  1408. * pass in NULL for new_config, cur_alt, and new_alt.
  1409. *
  1410. * To change alternate interface settings, pass in NULL for new_config,
  1411. * pass in the current alternate interface setting in cur_alt,
  1412. * and pass in the new alternate interface setting in new_alt.
  1413. *
  1414. * Returns an error if the requested bandwidth change exceeds the
  1415. * bus bandwidth or host controller internal resources.
  1416. */
  1417. int usb_hcd_alloc_bandwidth(struct usb_device *udev,
  1418. struct usb_host_config *new_config,
  1419. struct usb_host_interface *cur_alt,
  1420. struct usb_host_interface *new_alt)
  1421. {
  1422. int num_intfs, i, j;
  1423. struct usb_host_interface *alt = NULL;
  1424. int ret = 0;
  1425. struct usb_hcd *hcd;
  1426. struct usb_host_endpoint *ep;
  1427. hcd = bus_to_hcd(udev->bus);
  1428. if (!hcd->driver->check_bandwidth)
  1429. return 0;
  1430. /* Configuration is being removed - set configuration 0 */
  1431. if (!new_config && !cur_alt) {
  1432. for (i = 1; i < 16; ++i) {
  1433. ep = udev->ep_out[i];
  1434. if (ep)
  1435. hcd->driver->drop_endpoint(hcd, udev, ep);
  1436. ep = udev->ep_in[i];
  1437. if (ep)
  1438. hcd->driver->drop_endpoint(hcd, udev, ep);
  1439. }
  1440. hcd->driver->check_bandwidth(hcd, udev);
  1441. return 0;
  1442. }
  1443. /* Check if the HCD says there's enough bandwidth. Enable all endpoints
  1444. * each interface's alt setting 0 and ask the HCD to check the bandwidth
  1445. * of the bus. There will always be bandwidth for endpoint 0, so it's
  1446. * ok to exclude it.
  1447. */
  1448. if (new_config) {
  1449. num_intfs = new_config->desc.bNumInterfaces;
  1450. /* Remove endpoints (except endpoint 0, which is always on the
  1451. * schedule) from the old config from the schedule
  1452. */
  1453. for (i = 1; i < 16; ++i) {
  1454. ep = udev->ep_out[i];
  1455. if (ep) {
  1456. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1457. if (ret < 0)
  1458. goto reset;
  1459. }
  1460. ep = udev->ep_in[i];
  1461. if (ep) {
  1462. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1463. if (ret < 0)
  1464. goto reset;
  1465. }
  1466. }
  1467. for (i = 0; i < num_intfs; ++i) {
  1468. struct usb_host_interface *first_alt;
  1469. int iface_num;
  1470. first_alt = &new_config->intf_cache[i]->altsetting[0];
  1471. iface_num = first_alt->desc.bInterfaceNumber;
  1472. /* Set up endpoints for alternate interface setting 0 */
  1473. alt = usb_find_alt_setting(new_config, iface_num, 0);
  1474. if (!alt)
  1475. /* No alt setting 0? Pick the first setting. */
  1476. alt = first_alt;
  1477. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  1478. ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
  1479. if (ret < 0)
  1480. goto reset;
  1481. }
  1482. }
  1483. }
  1484. if (cur_alt && new_alt) {
  1485. struct usb_interface *iface = usb_ifnum_to_if(udev,
  1486. cur_alt->desc.bInterfaceNumber);
  1487. if (iface->resetting_device) {
  1488. /*
  1489. * The USB core just reset the device, so the xHCI host
  1490. * and the device will think alt setting 0 is installed.
  1491. * However, the USB core will pass in the alternate
  1492. * setting installed before the reset as cur_alt. Dig
  1493. * out the alternate setting 0 structure, or the first
  1494. * alternate setting if a broken device doesn't have alt
  1495. * setting 0.
  1496. */
  1497. cur_alt = usb_altnum_to_altsetting(iface, 0);
  1498. if (!cur_alt)
  1499. cur_alt = &iface->altsetting[0];
  1500. }
  1501. /* Drop all the endpoints in the current alt setting */
  1502. for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
  1503. ret = hcd->driver->drop_endpoint(hcd, udev,
  1504. &cur_alt->endpoint[i]);
  1505. if (ret < 0)
  1506. goto reset;
  1507. }
  1508. /* Add all the endpoints in the new alt setting */
  1509. for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
  1510. ret = hcd->driver->add_endpoint(hcd, udev,
  1511. &new_alt->endpoint[i]);
  1512. if (ret < 0)
  1513. goto reset;
  1514. }
  1515. }
  1516. ret = hcd->driver->check_bandwidth(hcd, udev);
  1517. reset:
  1518. if (ret < 0)
  1519. hcd->driver->reset_bandwidth(hcd, udev);
  1520. return ret;
  1521. }
  1522. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1523. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1524. * have been called previously. Use for set_configuration, set_interface,
  1525. * driver removal, physical disconnect.
  1526. *
  1527. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1528. * type, maxpacket size, toggle, halt status, and scheduling.
  1529. */
  1530. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1531. struct usb_host_endpoint *ep)
  1532. {
  1533. struct usb_hcd *hcd;
  1534. might_sleep();
  1535. hcd = bus_to_hcd(udev->bus);
  1536. if (hcd->driver->endpoint_disable)
  1537. hcd->driver->endpoint_disable(hcd, ep);
  1538. }
  1539. /**
  1540. * usb_hcd_reset_endpoint - reset host endpoint state
  1541. * @udev: USB device.
  1542. * @ep: the endpoint to reset.
  1543. *
  1544. * Resets any host endpoint state such as the toggle bit, sequence
  1545. * number and current window.
  1546. */
  1547. void usb_hcd_reset_endpoint(struct usb_device *udev,
  1548. struct usb_host_endpoint *ep)
  1549. {
  1550. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1551. if (hcd->driver->endpoint_reset)
  1552. hcd->driver->endpoint_reset(hcd, ep);
  1553. else {
  1554. int epnum = usb_endpoint_num(&ep->desc);
  1555. int is_out = usb_endpoint_dir_out(&ep->desc);
  1556. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1557. usb_settoggle(udev, epnum, is_out, 0);
  1558. if (is_control)
  1559. usb_settoggle(udev, epnum, !is_out, 0);
  1560. }
  1561. }
  1562. /* Protect against drivers that try to unlink URBs after the device
  1563. * is gone, by waiting until all unlinks for @udev are finished.
  1564. * Since we don't currently track URBs by device, simply wait until
  1565. * nothing is running in the locked region of usb_hcd_unlink_urb().
  1566. */
  1567. void usb_hcd_synchronize_unlinks(struct usb_device *udev)
  1568. {
  1569. spin_lock_irq(&hcd_urb_unlink_lock);
  1570. spin_unlock_irq(&hcd_urb_unlink_lock);
  1571. }
  1572. /*-------------------------------------------------------------------------*/
  1573. /* called in any context */
  1574. int usb_hcd_get_frame_number (struct usb_device *udev)
  1575. {
  1576. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1577. if (!HC_IS_RUNNING (hcd->state))
  1578. return -ESHUTDOWN;
  1579. return hcd->driver->get_frame_number (hcd);
  1580. }
  1581. /*-------------------------------------------------------------------------*/
  1582. #ifdef CONFIG_PM
  1583. int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
  1584. {
  1585. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1586. int status;
  1587. int old_state = hcd->state;
  1588. dev_dbg(&rhdev->dev, "bus %s%s\n",
  1589. (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend");
  1590. if (!hcd->driver->bus_suspend) {
  1591. status = -ENOENT;
  1592. } else {
  1593. hcd->state = HC_STATE_QUIESCING;
  1594. status = hcd->driver->bus_suspend(hcd);
  1595. }
  1596. if (status == 0) {
  1597. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  1598. hcd->state = HC_STATE_SUSPENDED;
  1599. } else {
  1600. hcd->state = old_state;
  1601. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1602. "suspend", status);
  1603. }
  1604. return status;
  1605. }
  1606. int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
  1607. {
  1608. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1609. int status;
  1610. int old_state = hcd->state;
  1611. dev_dbg(&rhdev->dev, "usb %s%s\n",
  1612. (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume");
  1613. if (!hcd->driver->bus_resume)
  1614. return -ENOENT;
  1615. if (hcd->state == HC_STATE_RUNNING)
  1616. return 0;
  1617. hcd->state = HC_STATE_RESUMING;
  1618. status = hcd->driver->bus_resume(hcd);
  1619. if (status == 0) {
  1620. /* TRSMRCY = 10 msec */
  1621. msleep(10);
  1622. usb_set_device_state(rhdev, rhdev->actconfig
  1623. ? USB_STATE_CONFIGURED
  1624. : USB_STATE_ADDRESS);
  1625. hcd->state = HC_STATE_RUNNING;
  1626. } else {
  1627. hcd->state = old_state;
  1628. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1629. "resume", status);
  1630. if (status != -ESHUTDOWN)
  1631. usb_hc_died(hcd);
  1632. }
  1633. return status;
  1634. }
  1635. #endif /* CONFIG_PM */
  1636. #ifdef CONFIG_USB_SUSPEND
  1637. /* Workqueue routine for root-hub remote wakeup */
  1638. static void hcd_resume_work(struct work_struct *work)
  1639. {
  1640. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  1641. struct usb_device *udev = hcd->self.root_hub;
  1642. usb_lock_device(udev);
  1643. usb_remote_wakeup(udev);
  1644. usb_unlock_device(udev);
  1645. }
  1646. /**
  1647. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1648. * @hcd: host controller for this root hub
  1649. *
  1650. * The USB host controller calls this function when its root hub is
  1651. * suspended (with the remote wakeup feature enabled) and a remote
  1652. * wakeup request is received. The routine submits a workqueue request
  1653. * to resume the root hub (that is, manage its downstream ports again).
  1654. */
  1655. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1656. {
  1657. unsigned long flags;
  1658. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1659. if (hcd->rh_registered)
  1660. queue_work(pm_wq, &hcd->wakeup_work);
  1661. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1662. }
  1663. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1664. #endif /* CONFIG_USB_SUSPEND */
  1665. /*-------------------------------------------------------------------------*/
  1666. #ifdef CONFIG_USB_OTG
  1667. /**
  1668. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1669. * @bus: the bus (must use hcd framework)
  1670. * @port_num: 1-based number of port; usually bus->otg_port
  1671. * Context: in_interrupt()
  1672. *
  1673. * Starts enumeration, with an immediate reset followed later by
  1674. * khubd identifying and possibly configuring the device.
  1675. * This is needed by OTG controller drivers, where it helps meet
  1676. * HNP protocol timing requirements for starting a port reset.
  1677. */
  1678. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1679. {
  1680. struct usb_hcd *hcd;
  1681. int status = -EOPNOTSUPP;
  1682. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1683. * boards with root hubs hooked up to internal devices (instead of
  1684. * just the OTG port) may need more attention to resetting...
  1685. */
  1686. hcd = container_of (bus, struct usb_hcd, self);
  1687. if (port_num && hcd->driver->start_port_reset)
  1688. status = hcd->driver->start_port_reset(hcd, port_num);
  1689. /* run khubd shortly after (first) root port reset finishes;
  1690. * it may issue others, until at least 50 msecs have passed.
  1691. */
  1692. if (status == 0)
  1693. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1694. return status;
  1695. }
  1696. EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  1697. #endif
  1698. /*-------------------------------------------------------------------------*/
  1699. /**
  1700. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1701. * @irq: the IRQ being raised
  1702. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1703. *
  1704. * If the controller isn't HALTed, calls the driver's irq handler.
  1705. * Checks whether the controller is now dead.
  1706. */
  1707. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  1708. {
  1709. struct usb_hcd *hcd = __hcd;
  1710. unsigned long flags;
  1711. irqreturn_t rc;
  1712. /* IRQF_DISABLED doesn't work correctly with shared IRQs
  1713. * when the first handler doesn't use it. So let's just
  1714. * assume it's never used.
  1715. */
  1716. local_irq_save(flags);
  1717. if (unlikely(hcd->state == HC_STATE_HALT ||
  1718. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
  1719. rc = IRQ_NONE;
  1720. } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
  1721. rc = IRQ_NONE;
  1722. } else {
  1723. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1724. if (unlikely(hcd->state == HC_STATE_HALT))
  1725. usb_hc_died(hcd);
  1726. rc = IRQ_HANDLED;
  1727. }
  1728. local_irq_restore(flags);
  1729. return rc;
  1730. }
  1731. /*-------------------------------------------------------------------------*/
  1732. /**
  1733. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1734. * @hcd: pointer to the HCD representing the controller
  1735. *
  1736. * This is called by bus glue to report a USB host controller that died
  1737. * while operations may still have been pending. It's called automatically
  1738. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1739. */
  1740. void usb_hc_died (struct usb_hcd *hcd)
  1741. {
  1742. unsigned long flags;
  1743. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1744. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1745. if (hcd->rh_registered) {
  1746. hcd->poll_rh = 0;
  1747. /* make khubd clean up old urbs and devices */
  1748. usb_set_device_state (hcd->self.root_hub,
  1749. USB_STATE_NOTATTACHED);
  1750. usb_kick_khubd (hcd->self.root_hub);
  1751. }
  1752. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1753. }
  1754. EXPORT_SYMBOL_GPL (usb_hc_died);
  1755. /*-------------------------------------------------------------------------*/
  1756. /**
  1757. * usb_create_hcd - create and initialize an HCD structure
  1758. * @driver: HC driver that will use this hcd
  1759. * @dev: device for this HC, stored in hcd->self.controller
  1760. * @bus_name: value to store in hcd->self.bus_name
  1761. * Context: !in_interrupt()
  1762. *
  1763. * Allocate a struct usb_hcd, with extra space at the end for the
  1764. * HC driver's private data. Initialize the generic members of the
  1765. * hcd structure.
  1766. *
  1767. * If memory is unavailable, returns NULL.
  1768. */
  1769. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1770. struct device *dev, const char *bus_name)
  1771. {
  1772. struct usb_hcd *hcd;
  1773. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1774. if (!hcd) {
  1775. dev_dbg (dev, "hcd alloc failed\n");
  1776. return NULL;
  1777. }
  1778. dev_set_drvdata(dev, hcd);
  1779. kref_init(&hcd->kref);
  1780. usb_bus_init(&hcd->self);
  1781. hcd->self.controller = dev;
  1782. hcd->self.bus_name = bus_name;
  1783. hcd->self.uses_dma = (dev->dma_mask != NULL);
  1784. init_timer(&hcd->rh_timer);
  1785. hcd->rh_timer.function = rh_timer_func;
  1786. hcd->rh_timer.data = (unsigned long) hcd;
  1787. #ifdef CONFIG_USB_SUSPEND
  1788. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  1789. #endif
  1790. mutex_init(&hcd->bandwidth_mutex);
  1791. hcd->driver = driver;
  1792. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1793. "USB Host Controller";
  1794. return hcd;
  1795. }
  1796. EXPORT_SYMBOL_GPL(usb_create_hcd);
  1797. static void hcd_release (struct kref *kref)
  1798. {
  1799. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  1800. kfree(hcd);
  1801. }
  1802. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  1803. {
  1804. if (hcd)
  1805. kref_get (&hcd->kref);
  1806. return hcd;
  1807. }
  1808. EXPORT_SYMBOL_GPL(usb_get_hcd);
  1809. void usb_put_hcd (struct usb_hcd *hcd)
  1810. {
  1811. if (hcd)
  1812. kref_put (&hcd->kref, hcd_release);
  1813. }
  1814. EXPORT_SYMBOL_GPL(usb_put_hcd);
  1815. /**
  1816. * usb_add_hcd - finish generic HCD structure initialization and register
  1817. * @hcd: the usb_hcd structure to initialize
  1818. * @irqnum: Interrupt line to allocate
  1819. * @irqflags: Interrupt type flags
  1820. *
  1821. * Finish the remaining parts of generic HCD initialization: allocate the
  1822. * buffers of consistent memory, register the bus, request the IRQ line,
  1823. * and call the driver's reset() and start() routines.
  1824. */
  1825. int usb_add_hcd(struct usb_hcd *hcd,
  1826. unsigned int irqnum, unsigned long irqflags)
  1827. {
  1828. int retval;
  1829. struct usb_device *rhdev;
  1830. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1831. hcd->authorized_default = hcd->wireless? 0 : 1;
  1832. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1833. /* HC is in reset state, but accessible. Now do the one-time init,
  1834. * bottom up so that hcds can customize the root hubs before khubd
  1835. * starts talking to them. (Note, bus id is assigned early too.)
  1836. */
  1837. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1838. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1839. return retval;
  1840. }
  1841. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1842. goto err_register_bus;
  1843. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1844. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1845. retval = -ENOMEM;
  1846. goto err_allocate_root_hub;
  1847. }
  1848. switch (hcd->driver->flags & HCD_MASK) {
  1849. case HCD_USB11:
  1850. rhdev->speed = USB_SPEED_FULL;
  1851. break;
  1852. case HCD_USB2:
  1853. rhdev->speed = USB_SPEED_HIGH;
  1854. break;
  1855. case HCD_USB3:
  1856. rhdev->speed = USB_SPEED_SUPER;
  1857. break;
  1858. default:
  1859. goto err_allocate_root_hub;
  1860. }
  1861. hcd->self.root_hub = rhdev;
  1862. /* wakeup flag init defaults to "everything works" for root hubs,
  1863. * but drivers can override it in reset() if needed, along with
  1864. * recording the overall controller's system wakeup capability.
  1865. */
  1866. device_init_wakeup(&rhdev->dev, 1);
  1867. /* "reset" is misnamed; its role is now one-time init. the controller
  1868. * should already have been reset (and boot firmware kicked off etc).
  1869. */
  1870. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1871. dev_err(hcd->self.controller, "can't setup\n");
  1872. goto err_hcd_driver_setup;
  1873. }
  1874. /* NOTE: root hub and controller capabilities may not be the same */
  1875. if (device_can_wakeup(hcd->self.controller)
  1876. && device_can_wakeup(&hcd->self.root_hub->dev))
  1877. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1878. /* enable irqs just before we start the controller */
  1879. if (hcd->driver->irq) {
  1880. /* IRQF_DISABLED doesn't work as advertised when used together
  1881. * with IRQF_SHARED. As usb_hcd_irq() will always disable
  1882. * interrupts we can remove it here.
  1883. */
  1884. if (irqflags & IRQF_SHARED)
  1885. irqflags &= ~IRQF_DISABLED;
  1886. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1887. hcd->driver->description, hcd->self.busnum);
  1888. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1889. hcd->irq_descr, hcd)) != 0) {
  1890. dev_err(hcd->self.controller,
  1891. "request interrupt %d failed\n", irqnum);
  1892. goto err_request_irq;
  1893. }
  1894. hcd->irq = irqnum;
  1895. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  1896. (hcd->driver->flags & HCD_MEMORY) ?
  1897. "io mem" : "io base",
  1898. (unsigned long long)hcd->rsrc_start);
  1899. } else {
  1900. hcd->irq = -1;
  1901. if (hcd->rsrc_start)
  1902. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1903. (hcd->driver->flags & HCD_MEMORY) ?
  1904. "io mem" : "io base",
  1905. (unsigned long long)hcd->rsrc_start);
  1906. }
  1907. if ((retval = hcd->driver->start(hcd)) < 0) {
  1908. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1909. goto err_hcd_driver_start;
  1910. }
  1911. /* starting here, usbcore will pay attention to this root hub */
  1912. rhdev->bus_mA = min(500u, hcd->power_budget);
  1913. if ((retval = register_root_hub(hcd)) != 0)
  1914. goto err_register_root_hub;
  1915. retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  1916. if (retval < 0) {
  1917. printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
  1918. retval);
  1919. goto error_create_attr_group;
  1920. }
  1921. if (hcd->uses_new_polling && hcd->poll_rh)
  1922. usb_hcd_poll_rh_status(hcd);
  1923. return retval;
  1924. error_create_attr_group:
  1925. mutex_lock(&usb_bus_list_lock);
  1926. usb_disconnect(&hcd->self.root_hub);
  1927. mutex_unlock(&usb_bus_list_lock);
  1928. err_register_root_hub:
  1929. hcd->driver->stop(hcd);
  1930. err_hcd_driver_start:
  1931. if (hcd->irq >= 0)
  1932. free_irq(irqnum, hcd);
  1933. err_request_irq:
  1934. err_hcd_driver_setup:
  1935. hcd->self.root_hub = NULL;
  1936. usb_put_dev(rhdev);
  1937. err_allocate_root_hub:
  1938. usb_deregister_bus(&hcd->self);
  1939. err_register_bus:
  1940. hcd_buffer_destroy(hcd);
  1941. return retval;
  1942. }
  1943. EXPORT_SYMBOL_GPL(usb_add_hcd);
  1944. /**
  1945. * usb_remove_hcd - shutdown processing for generic HCDs
  1946. * @hcd: the usb_hcd structure to remove
  1947. * Context: !in_interrupt()
  1948. *
  1949. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1950. * invoking the HCD's stop() method.
  1951. */
  1952. void usb_remove_hcd(struct usb_hcd *hcd)
  1953. {
  1954. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1955. if (HC_IS_RUNNING (hcd->state))
  1956. hcd->state = HC_STATE_QUIESCING;
  1957. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1958. spin_lock_irq (&hcd_root_hub_lock);
  1959. hcd->rh_registered = 0;
  1960. spin_unlock_irq (&hcd_root_hub_lock);
  1961. #ifdef CONFIG_USB_SUSPEND
  1962. cancel_work_sync(&hcd->wakeup_work);
  1963. #endif
  1964. sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
  1965. mutex_lock(&usb_bus_list_lock);
  1966. usb_disconnect(&hcd->self.root_hub);
  1967. mutex_unlock(&usb_bus_list_lock);
  1968. hcd->driver->stop(hcd);
  1969. hcd->state = HC_STATE_HALT;
  1970. hcd->poll_rh = 0;
  1971. del_timer_sync(&hcd->rh_timer);
  1972. if (hcd->irq >= 0)
  1973. free_irq(hcd->irq, hcd);
  1974. usb_deregister_bus(&hcd->self);
  1975. hcd_buffer_destroy(hcd);
  1976. }
  1977. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  1978. void
  1979. usb_hcd_platform_shutdown(struct platform_device* dev)
  1980. {
  1981. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1982. if (hcd->driver->shutdown)
  1983. hcd->driver->shutdown(hcd);
  1984. }
  1985. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  1986. /*-------------------------------------------------------------------------*/
  1987. #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
  1988. struct usb_mon_operations *mon_ops;
  1989. /*
  1990. * The registration is unlocked.
  1991. * We do it this way because we do not want to lock in hot paths.
  1992. *
  1993. * Notice that the code is minimally error-proof. Because usbmon needs
  1994. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1995. */
  1996. int usb_mon_register (struct usb_mon_operations *ops)
  1997. {
  1998. if (mon_ops)
  1999. return -EBUSY;
  2000. mon_ops = ops;
  2001. mb();
  2002. return 0;
  2003. }
  2004. EXPORT_SYMBOL_GPL (usb_mon_register);
  2005. void usb_mon_deregister (void)
  2006. {
  2007. if (mon_ops == NULL) {
  2008. printk(KERN_ERR "USB: monitor was not registered\n");
  2009. return;
  2010. }
  2011. mon_ops = NULL;
  2012. mb();
  2013. }
  2014. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  2015. #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */