hcd.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright Linus Torvalds 1999
  4. * (C) Copyright Johannes Erdfelt 1999-2001
  5. * (C) Copyright Andreas Gal 1999
  6. * (C) Copyright Gregory P. Smith 1999
  7. * (C) Copyright Deti Fliegl 1999
  8. * (C) Copyright Randy Dunlap 2000
  9. * (C) Copyright David Brownell 2000-2002
  10. */
  11. #include <linux/bcd.h>
  12. #include <linux/module.h>
  13. #include <linux/version.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/slab.h>
  17. #include <linux/completion.h>
  18. #include <linux/utsname.h>
  19. #include <linux/mm.h>
  20. #include <asm/io.h>
  21. #include <linux/device.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/mutex.h>
  24. #include <asm/irq.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/unaligned.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/types.h>
  31. #include <linux/genalloc.h>
  32. #include <linux/io.h>
  33. #include <linux/kcov.h>
  34. #include <linux/phy/phy.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/hcd.h>
  37. #include <linux/usb/otg.h>
  38. #include "usb.h"
  39. #include "phy.h"
  40. /*-------------------------------------------------------------------------*/
  41. /*
  42. * USB Host Controller Driver framework
  43. *
  44. * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
  45. * HCD-specific behaviors/bugs.
  46. *
  47. * This does error checks, tracks devices and urbs, and delegates to a
  48. * "hc_driver" only for code (and data) that really needs to know about
  49. * hardware differences. That includes root hub registers, i/o queues,
  50. * and so on ... but as little else as possible.
  51. *
  52. * Shared code includes most of the "root hub" code (these are emulated,
  53. * though each HC's hardware works differently) and PCI glue, plus request
  54. * tracking overhead. The HCD code should only block on spinlocks or on
  55. * hardware handshaking; blocking on software events (such as other kernel
  56. * threads releasing resources, or completing actions) is all generic.
  57. *
  58. * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
  59. * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
  60. * only by the hub driver ... and that neither should be seen or used by
  61. * usb client device drivers.
  62. *
  63. * Contributors of ideas or unattributed patches include: David Brownell,
  64. * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
  65. *
  66. * HISTORY:
  67. * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
  68. * associated cleanup. "usb_hcd" still != "usb_bus".
  69. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  70. */
  71. /*-------------------------------------------------------------------------*/
  72. /* Keep track of which host controller drivers are loaded */
  73. unsigned long usb_hcds_loaded;
  74. EXPORT_SYMBOL_GPL(usb_hcds_loaded);
  75. /* host controllers we manage */
  76. DEFINE_IDR (usb_bus_idr);
  77. EXPORT_SYMBOL_GPL (usb_bus_idr);
  78. /* used when allocating bus numbers */
  79. #define USB_MAXBUS 64
  80. /* used when updating list of hcds */
  81. DEFINE_MUTEX(usb_bus_idr_lock); /* exported only for usbfs */
  82. EXPORT_SYMBOL_GPL (usb_bus_idr_lock);
  83. /* used for controlling access to virtual root hubs */
  84. static DEFINE_SPINLOCK(hcd_root_hub_lock);
  85. /* used when updating an endpoint's URB list */
  86. static DEFINE_SPINLOCK(hcd_urb_list_lock);
  87. /* used to protect against unlinking URBs after the device is gone */
  88. static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
  89. /* wait queue for synchronous unlinks */
  90. DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
  91. /*-------------------------------------------------------------------------*/
  92. /*
  93. * Sharable chunks of root hub code.
  94. */
  95. /*-------------------------------------------------------------------------*/
  96. #define KERNEL_REL bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff))
  97. #define KERNEL_VER bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff))
  98. /* usb 3.1 root hub device descriptor */
  99. static const u8 usb31_rh_dev_descriptor[18] = {
  100. 0x12, /* __u8 bLength; */
  101. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  102. 0x10, 0x03, /* __le16 bcdUSB; v3.1 */
  103. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  104. 0x00, /* __u8 bDeviceSubClass; */
  105. 0x03, /* __u8 bDeviceProtocol; USB 3 hub */
  106. 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
  107. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  108. 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
  109. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  110. 0x03, /* __u8 iManufacturer; */
  111. 0x02, /* __u8 iProduct; */
  112. 0x01, /* __u8 iSerialNumber; */
  113. 0x01 /* __u8 bNumConfigurations; */
  114. };
  115. /* usb 3.0 root hub device descriptor */
  116. static const u8 usb3_rh_dev_descriptor[18] = {
  117. 0x12, /* __u8 bLength; */
  118. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  119. 0x00, 0x03, /* __le16 bcdUSB; v3.0 */
  120. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  121. 0x00, /* __u8 bDeviceSubClass; */
  122. 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */
  123. 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
  124. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  125. 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
  126. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  127. 0x03, /* __u8 iManufacturer; */
  128. 0x02, /* __u8 iProduct; */
  129. 0x01, /* __u8 iSerialNumber; */
  130. 0x01 /* __u8 bNumConfigurations; */
  131. };
  132. /* usb 2.5 (wireless USB 1.0) root hub device descriptor */
  133. static const u8 usb25_rh_dev_descriptor[18] = {
  134. 0x12, /* __u8 bLength; */
  135. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  136. 0x50, 0x02, /* __le16 bcdUSB; v2.5 */
  137. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  138. 0x00, /* __u8 bDeviceSubClass; */
  139. 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
  140. 0xFF, /* __u8 bMaxPacketSize0; always 0xFF (WUSB Spec 7.4.1). */
  141. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  142. 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
  143. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  144. 0x03, /* __u8 iManufacturer; */
  145. 0x02, /* __u8 iProduct; */
  146. 0x01, /* __u8 iSerialNumber; */
  147. 0x01 /* __u8 bNumConfigurations; */
  148. };
  149. /* usb 2.0 root hub device descriptor */
  150. static const u8 usb2_rh_dev_descriptor[18] = {
  151. 0x12, /* __u8 bLength; */
  152. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  153. 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
  154. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  155. 0x00, /* __u8 bDeviceSubClass; */
  156. 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
  157. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  158. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  159. 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
  160. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  161. 0x03, /* __u8 iManufacturer; */
  162. 0x02, /* __u8 iProduct; */
  163. 0x01, /* __u8 iSerialNumber; */
  164. 0x01 /* __u8 bNumConfigurations; */
  165. };
  166. /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
  167. /* usb 1.1 root hub device descriptor */
  168. static const u8 usb11_rh_dev_descriptor[18] = {
  169. 0x12, /* __u8 bLength; */
  170. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  171. 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
  172. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  173. 0x00, /* __u8 bDeviceSubClass; */
  174. 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
  175. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  176. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  177. 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
  178. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  179. 0x03, /* __u8 iManufacturer; */
  180. 0x02, /* __u8 iProduct; */
  181. 0x01, /* __u8 iSerialNumber; */
  182. 0x01 /* __u8 bNumConfigurations; */
  183. };
  184. /*-------------------------------------------------------------------------*/
  185. /* Configuration descriptors for our root hubs */
  186. static const u8 fs_rh_config_descriptor[] = {
  187. /* one configuration */
  188. 0x09, /* __u8 bLength; */
  189. USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
  190. 0x19, 0x00, /* __le16 wTotalLength; */
  191. 0x01, /* __u8 bNumInterfaces; (1) */
  192. 0x01, /* __u8 bConfigurationValue; */
  193. 0x00, /* __u8 iConfiguration; */
  194. 0xc0, /* __u8 bmAttributes;
  195. Bit 7: must be set,
  196. 6: Self-powered,
  197. 5: Remote wakeup,
  198. 4..0: resvd */
  199. 0x00, /* __u8 MaxPower; */
  200. /* USB 1.1:
  201. * USB 2.0, single TT organization (mandatory):
  202. * one interface, protocol 0
  203. *
  204. * USB 2.0, multiple TT organization (optional):
  205. * two interfaces, protocols 1 (like single TT)
  206. * and 2 (multiple TT mode) ... config is
  207. * sometimes settable
  208. * NOT IMPLEMENTED
  209. */
  210. /* one interface */
  211. 0x09, /* __u8 if_bLength; */
  212. USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
  213. 0x00, /* __u8 if_bInterfaceNumber; */
  214. 0x00, /* __u8 if_bAlternateSetting; */
  215. 0x01, /* __u8 if_bNumEndpoints; */
  216. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  217. 0x00, /* __u8 if_bInterfaceSubClass; */
  218. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  219. 0x00, /* __u8 if_iInterface; */
  220. /* one endpoint (status change endpoint) */
  221. 0x07, /* __u8 ep_bLength; */
  222. USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
  223. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  224. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  225. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  226. 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  227. };
  228. static const u8 hs_rh_config_descriptor[] = {
  229. /* one configuration */
  230. 0x09, /* __u8 bLength; */
  231. USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
  232. 0x19, 0x00, /* __le16 wTotalLength; */
  233. 0x01, /* __u8 bNumInterfaces; (1) */
  234. 0x01, /* __u8 bConfigurationValue; */
  235. 0x00, /* __u8 iConfiguration; */
  236. 0xc0, /* __u8 bmAttributes;
  237. Bit 7: must be set,
  238. 6: Self-powered,
  239. 5: Remote wakeup,
  240. 4..0: resvd */
  241. 0x00, /* __u8 MaxPower; */
  242. /* USB 1.1:
  243. * USB 2.0, single TT organization (mandatory):
  244. * one interface, protocol 0
  245. *
  246. * USB 2.0, multiple TT organization (optional):
  247. * two interfaces, protocols 1 (like single TT)
  248. * and 2 (multiple TT mode) ... config is
  249. * sometimes settable
  250. * NOT IMPLEMENTED
  251. */
  252. /* one interface */
  253. 0x09, /* __u8 if_bLength; */
  254. USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
  255. 0x00, /* __u8 if_bInterfaceNumber; */
  256. 0x00, /* __u8 if_bAlternateSetting; */
  257. 0x01, /* __u8 if_bNumEndpoints; */
  258. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  259. 0x00, /* __u8 if_bInterfaceSubClass; */
  260. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  261. 0x00, /* __u8 if_iInterface; */
  262. /* one endpoint (status change endpoint) */
  263. 0x07, /* __u8 ep_bLength; */
  264. USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
  265. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  266. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  267. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  268. * see hub.c:hub_configure() for details. */
  269. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  270. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  271. };
  272. static const u8 ss_rh_config_descriptor[] = {
  273. /* one configuration */
  274. 0x09, /* __u8 bLength; */
  275. USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
  276. 0x1f, 0x00, /* __le16 wTotalLength; */
  277. 0x01, /* __u8 bNumInterfaces; (1) */
  278. 0x01, /* __u8 bConfigurationValue; */
  279. 0x00, /* __u8 iConfiguration; */
  280. 0xc0, /* __u8 bmAttributes;
  281. Bit 7: must be set,
  282. 6: Self-powered,
  283. 5: Remote wakeup,
  284. 4..0: resvd */
  285. 0x00, /* __u8 MaxPower; */
  286. /* one interface */
  287. 0x09, /* __u8 if_bLength; */
  288. USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
  289. 0x00, /* __u8 if_bInterfaceNumber; */
  290. 0x00, /* __u8 if_bAlternateSetting; */
  291. 0x01, /* __u8 if_bNumEndpoints; */
  292. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  293. 0x00, /* __u8 if_bInterfaceSubClass; */
  294. 0x00, /* __u8 if_bInterfaceProtocol; */
  295. 0x00, /* __u8 if_iInterface; */
  296. /* one endpoint (status change endpoint) */
  297. 0x07, /* __u8 ep_bLength; */
  298. USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
  299. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  300. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  301. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  302. * see hub.c:hub_configure() for details. */
  303. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  304. 0x0c, /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  305. /* one SuperSpeed endpoint companion descriptor */
  306. 0x06, /* __u8 ss_bLength */
  307. USB_DT_SS_ENDPOINT_COMP, /* __u8 ss_bDescriptorType; SuperSpeed EP */
  308. /* Companion */
  309. 0x00, /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */
  310. 0x00, /* __u8 ss_bmAttributes; 1 packet per service interval */
  311. 0x02, 0x00 /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */
  312. };
  313. /* authorized_default behaviour:
  314. * -1 is authorized for all devices except wireless (old behaviour)
  315. * 0 is unauthorized for all devices
  316. * 1 is authorized for all devices
  317. * 2 is authorized for internal devices
  318. */
  319. #define USB_AUTHORIZE_WIRED -1
  320. #define USB_AUTHORIZE_NONE 0
  321. #define USB_AUTHORIZE_ALL 1
  322. #define USB_AUTHORIZE_INTERNAL 2
  323. static int authorized_default = USB_AUTHORIZE_WIRED;
  324. module_param(authorized_default, int, S_IRUGO|S_IWUSR);
  325. MODULE_PARM_DESC(authorized_default,
  326. "Default USB device authorization: 0 is not authorized, 1 is "
  327. "authorized, 2 is authorized for internal devices, -1 is "
  328. "authorized except for wireless USB (default, old behaviour)");
  329. /*-------------------------------------------------------------------------*/
  330. /**
  331. * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
  332. * @s: Null-terminated ASCII (actually ISO-8859-1) string
  333. * @buf: Buffer for USB string descriptor (header + UTF-16LE)
  334. * @len: Length (in bytes; may be odd) of descriptor buffer.
  335. *
  336. * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len,
  337. * whichever is less.
  338. *
  339. * Note:
  340. * USB String descriptors can contain at most 126 characters; input
  341. * strings longer than that are truncated.
  342. */
  343. static unsigned
  344. ascii2desc(char const *s, u8 *buf, unsigned len)
  345. {
  346. unsigned n, t = 2 + 2*strlen(s);
  347. if (t > 254)
  348. t = 254; /* Longest possible UTF string descriptor */
  349. if (len > t)
  350. len = t;
  351. t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */
  352. n = len;
  353. while (n--) {
  354. *buf++ = t;
  355. if (!n--)
  356. break;
  357. *buf++ = t >> 8;
  358. t = (unsigned char)*s++;
  359. }
  360. return len;
  361. }
  362. /**
  363. * rh_string() - provides string descriptors for root hub
  364. * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
  365. * @hcd: the host controller for this root hub
  366. * @data: buffer for output packet
  367. * @len: length of the provided buffer
  368. *
  369. * Produces either a manufacturer, product or serial number string for the
  370. * virtual root hub device.
  371. *
  372. * Return: The number of bytes filled in: the length of the descriptor or
  373. * of the provided buffer, whichever is less.
  374. */
  375. static unsigned
  376. rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
  377. {
  378. char buf[100];
  379. char const *s;
  380. static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
  381. /* language ids */
  382. switch (id) {
  383. case 0:
  384. /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
  385. /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
  386. if (len > 4)
  387. len = 4;
  388. memcpy(data, langids, len);
  389. return len;
  390. case 1:
  391. /* Serial number */
  392. s = hcd->self.bus_name;
  393. break;
  394. case 2:
  395. /* Product name */
  396. s = hcd->product_desc;
  397. break;
  398. case 3:
  399. /* Manufacturer */
  400. snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
  401. init_utsname()->release, hcd->driver->description);
  402. s = buf;
  403. break;
  404. default:
  405. /* Can't happen; caller guarantees it */
  406. return 0;
  407. }
  408. return ascii2desc(s, data, len);
  409. }
  410. /* Root hub control transfers execute synchronously */
  411. static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
  412. {
  413. struct usb_ctrlrequest *cmd;
  414. u16 typeReq, wValue, wIndex, wLength;
  415. u8 *ubuf = urb->transfer_buffer;
  416. unsigned len = 0;
  417. int status;
  418. u8 patch_wakeup = 0;
  419. u8 patch_protocol = 0;
  420. u16 tbuf_size;
  421. u8 *tbuf = NULL;
  422. const u8 *bufp;
  423. might_sleep();
  424. spin_lock_irq(&hcd_root_hub_lock);
  425. status = usb_hcd_link_urb_to_ep(hcd, urb);
  426. spin_unlock_irq(&hcd_root_hub_lock);
  427. if (status)
  428. return status;
  429. urb->hcpriv = hcd; /* Indicate it's queued */
  430. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  431. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  432. wValue = le16_to_cpu (cmd->wValue);
  433. wIndex = le16_to_cpu (cmd->wIndex);
  434. wLength = le16_to_cpu (cmd->wLength);
  435. if (wLength > urb->transfer_buffer_length)
  436. goto error;
  437. /*
  438. * tbuf should be at least as big as the
  439. * USB hub descriptor.
  440. */
  441. tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength);
  442. tbuf = kzalloc(tbuf_size, GFP_KERNEL);
  443. if (!tbuf) {
  444. status = -ENOMEM;
  445. goto err_alloc;
  446. }
  447. bufp = tbuf;
  448. urb->actual_length = 0;
  449. switch (typeReq) {
  450. /* DEVICE REQUESTS */
  451. /* The root hub's remote wakeup enable bit is implemented using
  452. * driver model wakeup flags. If this system supports wakeup
  453. * through USB, userspace may change the default "allow wakeup"
  454. * policy through sysfs or these calls.
  455. *
  456. * Most root hubs support wakeup from downstream devices, for
  457. * runtime power management (disabling USB clocks and reducing
  458. * VBUS power usage). However, not all of them do so; silicon,
  459. * board, and BIOS bugs here are not uncommon, so these can't
  460. * be treated quite like external hubs.
  461. *
  462. * Likewise, not all root hubs will pass wakeup events upstream,
  463. * to wake up the whole system. So don't assume root hub and
  464. * controller capabilities are identical.
  465. */
  466. case DeviceRequest | USB_REQ_GET_STATUS:
  467. tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev)
  468. << USB_DEVICE_REMOTE_WAKEUP)
  469. | (1 << USB_DEVICE_SELF_POWERED);
  470. tbuf[1] = 0;
  471. len = 2;
  472. break;
  473. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  474. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  475. device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
  476. else
  477. goto error;
  478. break;
  479. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  480. if (device_can_wakeup(&hcd->self.root_hub->dev)
  481. && wValue == USB_DEVICE_REMOTE_WAKEUP)
  482. device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
  483. else
  484. goto error;
  485. break;
  486. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  487. tbuf[0] = 1;
  488. len = 1;
  489. fallthrough;
  490. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  491. break;
  492. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  493. switch (wValue & 0xff00) {
  494. case USB_DT_DEVICE << 8:
  495. switch (hcd->speed) {
  496. case HCD_USB32:
  497. case HCD_USB31:
  498. bufp = usb31_rh_dev_descriptor;
  499. break;
  500. case HCD_USB3:
  501. bufp = usb3_rh_dev_descriptor;
  502. break;
  503. case HCD_USB25:
  504. bufp = usb25_rh_dev_descriptor;
  505. break;
  506. case HCD_USB2:
  507. bufp = usb2_rh_dev_descriptor;
  508. break;
  509. case HCD_USB11:
  510. bufp = usb11_rh_dev_descriptor;
  511. break;
  512. default:
  513. goto error;
  514. }
  515. len = 18;
  516. if (hcd->has_tt)
  517. patch_protocol = 1;
  518. break;
  519. case USB_DT_CONFIG << 8:
  520. switch (hcd->speed) {
  521. case HCD_USB32:
  522. case HCD_USB31:
  523. case HCD_USB3:
  524. bufp = ss_rh_config_descriptor;
  525. len = sizeof ss_rh_config_descriptor;
  526. break;
  527. case HCD_USB25:
  528. case HCD_USB2:
  529. bufp = hs_rh_config_descriptor;
  530. len = sizeof hs_rh_config_descriptor;
  531. break;
  532. case HCD_USB11:
  533. bufp = fs_rh_config_descriptor;
  534. len = sizeof fs_rh_config_descriptor;
  535. break;
  536. default:
  537. goto error;
  538. }
  539. if (device_can_wakeup(&hcd->self.root_hub->dev))
  540. patch_wakeup = 1;
  541. break;
  542. case USB_DT_STRING << 8:
  543. if ((wValue & 0xff) < 4)
  544. urb->actual_length = rh_string(wValue & 0xff,
  545. hcd, ubuf, wLength);
  546. else /* unsupported IDs --> "protocol stall" */
  547. goto error;
  548. break;
  549. case USB_DT_BOS << 8:
  550. goto nongeneric;
  551. default:
  552. goto error;
  553. }
  554. break;
  555. case DeviceRequest | USB_REQ_GET_INTERFACE:
  556. tbuf[0] = 0;
  557. len = 1;
  558. fallthrough;
  559. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  560. break;
  561. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  562. /* wValue == urb->dev->devaddr */
  563. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  564. wValue);
  565. break;
  566. /* INTERFACE REQUESTS (no defined feature/status flags) */
  567. /* ENDPOINT REQUESTS */
  568. case EndpointRequest | USB_REQ_GET_STATUS:
  569. /* ENDPOINT_HALT flag */
  570. tbuf[0] = 0;
  571. tbuf[1] = 0;
  572. len = 2;
  573. fallthrough;
  574. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  575. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  576. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  577. break;
  578. /* CLASS REQUESTS (and errors) */
  579. default:
  580. nongeneric:
  581. /* non-generic request */
  582. switch (typeReq) {
  583. case GetHubStatus:
  584. len = 4;
  585. break;
  586. case GetPortStatus:
  587. if (wValue == HUB_PORT_STATUS)
  588. len = 4;
  589. else
  590. /* other port status types return 8 bytes */
  591. len = 8;
  592. break;
  593. case GetHubDescriptor:
  594. len = sizeof (struct usb_hub_descriptor);
  595. break;
  596. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  597. /* len is returned by hub_control */
  598. break;
  599. }
  600. status = hcd->driver->hub_control (hcd,
  601. typeReq, wValue, wIndex,
  602. tbuf, wLength);
  603. if (typeReq == GetHubDescriptor)
  604. usb_hub_adjust_deviceremovable(hcd->self.root_hub,
  605. (struct usb_hub_descriptor *)tbuf);
  606. break;
  607. error:
  608. /* "protocol stall" on error */
  609. status = -EPIPE;
  610. }
  611. if (status < 0) {
  612. len = 0;
  613. if (status != -EPIPE) {
  614. dev_dbg (hcd->self.controller,
  615. "CTRL: TypeReq=0x%x val=0x%x "
  616. "idx=0x%x len=%d ==> %d\n",
  617. typeReq, wValue, wIndex,
  618. wLength, status);
  619. }
  620. } else if (status > 0) {
  621. /* hub_control may return the length of data copied. */
  622. len = status;
  623. status = 0;
  624. }
  625. if (len) {
  626. if (urb->transfer_buffer_length < len)
  627. len = urb->transfer_buffer_length;
  628. urb->actual_length = len;
  629. /* always USB_DIR_IN, toward host */
  630. memcpy (ubuf, bufp, len);
  631. /* report whether RH hardware supports remote wakeup */
  632. if (patch_wakeup &&
  633. len > offsetof (struct usb_config_descriptor,
  634. bmAttributes))
  635. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  636. |= USB_CONFIG_ATT_WAKEUP;
  637. /* report whether RH hardware has an integrated TT */
  638. if (patch_protocol &&
  639. len > offsetof(struct usb_device_descriptor,
  640. bDeviceProtocol))
  641. ((struct usb_device_descriptor *) ubuf)->
  642. bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT;
  643. }
  644. kfree(tbuf);
  645. err_alloc:
  646. /* any errors get returned through the urb completion */
  647. spin_lock_irq(&hcd_root_hub_lock);
  648. usb_hcd_unlink_urb_from_ep(hcd, urb);
  649. usb_hcd_giveback_urb(hcd, urb, status);
  650. spin_unlock_irq(&hcd_root_hub_lock);
  651. return 0;
  652. }
  653. /*-------------------------------------------------------------------------*/
  654. /*
  655. * Root Hub interrupt transfers are polled using a timer if the
  656. * driver requests it; otherwise the driver is responsible for
  657. * calling usb_hcd_poll_rh_status() when an event occurs.
  658. *
  659. * Completions are called in_interrupt(), but they may or may not
  660. * be in_irq().
  661. */
  662. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  663. {
  664. struct urb *urb;
  665. int length;
  666. int status;
  667. unsigned long flags;
  668. char buffer[6]; /* Any root hubs with > 31 ports? */
  669. if (unlikely(!hcd->rh_pollable))
  670. return;
  671. if (!hcd->uses_new_polling && !hcd->status_urb)
  672. return;
  673. length = hcd->driver->hub_status_data(hcd, buffer);
  674. if (length > 0) {
  675. /* try to complete the status urb */
  676. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  677. urb = hcd->status_urb;
  678. if (urb) {
  679. clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  680. hcd->status_urb = NULL;
  681. if (urb->transfer_buffer_length >= length) {
  682. status = 0;
  683. } else {
  684. status = -EOVERFLOW;
  685. length = urb->transfer_buffer_length;
  686. }
  687. urb->actual_length = length;
  688. memcpy(urb->transfer_buffer, buffer, length);
  689. usb_hcd_unlink_urb_from_ep(hcd, urb);
  690. usb_hcd_giveback_urb(hcd, urb, status);
  691. } else {
  692. length = 0;
  693. set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  694. }
  695. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  696. }
  697. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  698. * exceed that limit if HZ is 100. The math is more clunky than
  699. * maybe expected, this is to make sure that all timers for USB devices
  700. * fire at the same time to give the CPU a break in between */
  701. if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
  702. (length == 0 && hcd->status_urb != NULL))
  703. mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  704. }
  705. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  706. /* timer callback */
  707. static void rh_timer_func (struct timer_list *t)
  708. {
  709. struct usb_hcd *_hcd = from_timer(_hcd, t, rh_timer);
  710. usb_hcd_poll_rh_status(_hcd);
  711. }
  712. /*-------------------------------------------------------------------------*/
  713. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  714. {
  715. int retval;
  716. unsigned long flags;
  717. unsigned len = 1 + (urb->dev->maxchild / 8);
  718. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  719. if (hcd->status_urb || urb->transfer_buffer_length < len) {
  720. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  721. retval = -EINVAL;
  722. goto done;
  723. }
  724. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  725. if (retval)
  726. goto done;
  727. hcd->status_urb = urb;
  728. urb->hcpriv = hcd; /* indicate it's queued */
  729. if (!hcd->uses_new_polling)
  730. mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  731. /* If a status change has already occurred, report it ASAP */
  732. else if (HCD_POLL_PENDING(hcd))
  733. mod_timer(&hcd->rh_timer, jiffies);
  734. retval = 0;
  735. done:
  736. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  737. return retval;
  738. }
  739. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  740. {
  741. if (usb_endpoint_xfer_int(&urb->ep->desc))
  742. return rh_queue_status (hcd, urb);
  743. if (usb_endpoint_xfer_control(&urb->ep->desc))
  744. return rh_call_control (hcd, urb);
  745. return -EINVAL;
  746. }
  747. /*-------------------------------------------------------------------------*/
  748. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  749. * since these URBs always execute synchronously.
  750. */
  751. static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  752. {
  753. unsigned long flags;
  754. int rc;
  755. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  756. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  757. if (rc)
  758. goto done;
  759. if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
  760. ; /* Do nothing */
  761. } else { /* Status URB */
  762. if (!hcd->uses_new_polling)
  763. del_timer (&hcd->rh_timer);
  764. if (urb == hcd->status_urb) {
  765. hcd->status_urb = NULL;
  766. usb_hcd_unlink_urb_from_ep(hcd, urb);
  767. usb_hcd_giveback_urb(hcd, urb, status);
  768. }
  769. }
  770. done:
  771. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  772. return rc;
  773. }
  774. /*-------------------------------------------------------------------------*/
  775. /**
  776. * usb_bus_init - shared initialization code
  777. * @bus: the bus structure being initialized
  778. *
  779. * This code is used to initialize a usb_bus structure, memory for which is
  780. * separately managed.
  781. */
  782. static void usb_bus_init (struct usb_bus *bus)
  783. {
  784. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  785. bus->devnum_next = 1;
  786. bus->root_hub = NULL;
  787. bus->busnum = -1;
  788. bus->bandwidth_allocated = 0;
  789. bus->bandwidth_int_reqs = 0;
  790. bus->bandwidth_isoc_reqs = 0;
  791. mutex_init(&bus->devnum_next_mutex);
  792. }
  793. /*-------------------------------------------------------------------------*/
  794. /**
  795. * usb_register_bus - registers the USB host controller with the usb core
  796. * @bus: pointer to the bus to register
  797. * Context: !in_interrupt()
  798. *
  799. * Assigns a bus number, and links the controller into usbcore data
  800. * structures so that it can be seen by scanning the bus list.
  801. *
  802. * Return: 0 if successful. A negative error code otherwise.
  803. */
  804. static int usb_register_bus(struct usb_bus *bus)
  805. {
  806. int result = -E2BIG;
  807. int busnum;
  808. mutex_lock(&usb_bus_idr_lock);
  809. busnum = idr_alloc(&usb_bus_idr, bus, 1, USB_MAXBUS, GFP_KERNEL);
  810. if (busnum < 0) {
  811. pr_err("%s: failed to get bus number\n", usbcore_name);
  812. goto error_find_busnum;
  813. }
  814. bus->busnum = busnum;
  815. mutex_unlock(&usb_bus_idr_lock);
  816. usb_notify_add_bus(bus);
  817. dev_info (bus->controller, "new USB bus registered, assigned bus "
  818. "number %d\n", bus->busnum);
  819. return 0;
  820. error_find_busnum:
  821. mutex_unlock(&usb_bus_idr_lock);
  822. return result;
  823. }
  824. /**
  825. * usb_deregister_bus - deregisters the USB host controller
  826. * @bus: pointer to the bus to deregister
  827. * Context: !in_interrupt()
  828. *
  829. * Recycles the bus number, and unlinks the controller from usbcore data
  830. * structures so that it won't be seen by scanning the bus list.
  831. */
  832. static void usb_deregister_bus (struct usb_bus *bus)
  833. {
  834. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  835. /*
  836. * NOTE: make sure that all the devices are removed by the
  837. * controller code, as well as having it call this when cleaning
  838. * itself up
  839. */
  840. mutex_lock(&usb_bus_idr_lock);
  841. idr_remove(&usb_bus_idr, bus->busnum);
  842. mutex_unlock(&usb_bus_idr_lock);
  843. usb_notify_remove_bus(bus);
  844. }
  845. /**
  846. * register_root_hub - called by usb_add_hcd() to register a root hub
  847. * @hcd: host controller for this root hub
  848. *
  849. * This function registers the root hub with the USB subsystem. It sets up
  850. * the device properly in the device tree and then calls usb_new_device()
  851. * to register the usb device. It also assigns the root hub's USB address
  852. * (always 1).
  853. *
  854. * Return: 0 if successful. A negative error code otherwise.
  855. */
  856. static int register_root_hub(struct usb_hcd *hcd)
  857. {
  858. struct device *parent_dev = hcd->self.controller;
  859. struct usb_device *usb_dev = hcd->self.root_hub;
  860. const int devnum = 1;
  861. int retval;
  862. usb_dev->devnum = devnum;
  863. usb_dev->bus->devnum_next = devnum + 1;
  864. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  865. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  866. mutex_lock(&usb_bus_idr_lock);
  867. usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  868. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  869. if (retval != sizeof usb_dev->descriptor) {
  870. mutex_unlock(&usb_bus_idr_lock);
  871. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  872. dev_name(&usb_dev->dev), retval);
  873. return (retval < 0) ? retval : -EMSGSIZE;
  874. }
  875. if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) {
  876. retval = usb_get_bos_descriptor(usb_dev);
  877. if (!retval) {
  878. usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev);
  879. } else if (usb_dev->speed >= USB_SPEED_SUPER) {
  880. mutex_unlock(&usb_bus_idr_lock);
  881. dev_dbg(parent_dev, "can't read %s bos descriptor %d\n",
  882. dev_name(&usb_dev->dev), retval);
  883. return retval;
  884. }
  885. }
  886. retval = usb_new_device (usb_dev);
  887. if (retval) {
  888. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  889. dev_name(&usb_dev->dev), retval);
  890. } else {
  891. spin_lock_irq (&hcd_root_hub_lock);
  892. hcd->rh_registered = 1;
  893. spin_unlock_irq (&hcd_root_hub_lock);
  894. /* Did the HC die before the root hub was registered? */
  895. if (HCD_DEAD(hcd))
  896. usb_hc_died (hcd); /* This time clean up */
  897. }
  898. mutex_unlock(&usb_bus_idr_lock);
  899. return retval;
  900. }
  901. /*
  902. * usb_hcd_start_port_resume - a root-hub port is sending a resume signal
  903. * @bus: the bus which the root hub belongs to
  904. * @portnum: the port which is being resumed
  905. *
  906. * HCDs should call this function when they know that a resume signal is
  907. * being sent to a root-hub port. The root hub will be prevented from
  908. * going into autosuspend until usb_hcd_end_port_resume() is called.
  909. *
  910. * The bus's private lock must be held by the caller.
  911. */
  912. void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
  913. {
  914. unsigned bit = 1 << portnum;
  915. if (!(bus->resuming_ports & bit)) {
  916. bus->resuming_ports |= bit;
  917. pm_runtime_get_noresume(&bus->root_hub->dev);
  918. }
  919. }
  920. EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
  921. /*
  922. * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal
  923. * @bus: the bus which the root hub belongs to
  924. * @portnum: the port which is being resumed
  925. *
  926. * HCDs should call this function when they know that a resume signal has
  927. * stopped being sent to a root-hub port. The root hub will be allowed to
  928. * autosuspend again.
  929. *
  930. * The bus's private lock must be held by the caller.
  931. */
  932. void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
  933. {
  934. unsigned bit = 1 << portnum;
  935. if (bus->resuming_ports & bit) {
  936. bus->resuming_ports &= ~bit;
  937. pm_runtime_put_noidle(&bus->root_hub->dev);
  938. }
  939. }
  940. EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
  941. /*-------------------------------------------------------------------------*/
  942. /**
  943. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  944. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  945. * @is_input: true iff the transaction sends data to the host
  946. * @isoc: true for isochronous transactions, false for interrupt ones
  947. * @bytecount: how many bytes in the transaction.
  948. *
  949. * Return: Approximate bus time in nanoseconds for a periodic transaction.
  950. *
  951. * Note:
  952. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  953. * scheduled in software, this function is only used for such scheduling.
  954. */
  955. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  956. {
  957. unsigned long tmp;
  958. switch (speed) {
  959. case USB_SPEED_LOW: /* INTR only */
  960. if (is_input) {
  961. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  962. return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  963. } else {
  964. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  965. return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  966. }
  967. case USB_SPEED_FULL: /* ISOC or INTR */
  968. if (isoc) {
  969. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  970. return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
  971. } else {
  972. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  973. return 9107L + BW_HOST_DELAY + tmp;
  974. }
  975. case USB_SPEED_HIGH: /* ISOC or INTR */
  976. /* FIXME adjust for input vs output */
  977. if (isoc)
  978. tmp = HS_NSECS_ISO (bytecount);
  979. else
  980. tmp = HS_NSECS (bytecount);
  981. return tmp;
  982. default:
  983. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  984. return -1;
  985. }
  986. }
  987. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  988. /*-------------------------------------------------------------------------*/
  989. /*
  990. * Generic HC operations.
  991. */
  992. /*-------------------------------------------------------------------------*/
  993. /**
  994. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  995. * @hcd: host controller to which @urb was submitted
  996. * @urb: URB being submitted
  997. *
  998. * Host controller drivers should call this routine in their enqueue()
  999. * method. The HCD's private spinlock must be held and interrupts must
  1000. * be disabled. The actions carried out here are required for URB
  1001. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  1002. *
  1003. * Return: 0 for no error, otherwise a negative error code (in which case
  1004. * the enqueue() method must fail). If no error occurs but enqueue() fails
  1005. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  1006. * the private spinlock and returning.
  1007. */
  1008. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  1009. {
  1010. int rc = 0;
  1011. spin_lock(&hcd_urb_list_lock);
  1012. /* Check that the URB isn't being killed */
  1013. if (unlikely(atomic_read(&urb->reject))) {
  1014. rc = -EPERM;
  1015. goto done;
  1016. }
  1017. if (unlikely(!urb->ep->enabled)) {
  1018. rc = -ENOENT;
  1019. goto done;
  1020. }
  1021. if (unlikely(!urb->dev->can_submit)) {
  1022. rc = -EHOSTUNREACH;
  1023. goto done;
  1024. }
  1025. /*
  1026. * Check the host controller's state and add the URB to the
  1027. * endpoint's queue.
  1028. */
  1029. if (HCD_RH_RUNNING(hcd)) {
  1030. urb->unlinked = 0;
  1031. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  1032. } else {
  1033. rc = -ESHUTDOWN;
  1034. goto done;
  1035. }
  1036. done:
  1037. spin_unlock(&hcd_urb_list_lock);
  1038. return rc;
  1039. }
  1040. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  1041. /**
  1042. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  1043. * @hcd: host controller to which @urb was submitted
  1044. * @urb: URB being checked for unlinkability
  1045. * @status: error code to store in @urb if the unlink succeeds
  1046. *
  1047. * Host controller drivers should call this routine in their dequeue()
  1048. * method. The HCD's private spinlock must be held and interrupts must
  1049. * be disabled. The actions carried out here are required for making
  1050. * sure than an unlink is valid.
  1051. *
  1052. * Return: 0 for no error, otherwise a negative error code (in which case
  1053. * the dequeue() method must fail). The possible error codes are:
  1054. *
  1055. * -EIDRM: @urb was not submitted or has already completed.
  1056. * The completion function may not have been called yet.
  1057. *
  1058. * -EBUSY: @urb has already been unlinked.
  1059. */
  1060. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  1061. int status)
  1062. {
  1063. struct list_head *tmp;
  1064. /* insist the urb is still queued */
  1065. list_for_each(tmp, &urb->ep->urb_list) {
  1066. if (tmp == &urb->urb_list)
  1067. break;
  1068. }
  1069. if (tmp != &urb->urb_list)
  1070. return -EIDRM;
  1071. /* Any status except -EINPROGRESS means something already started to
  1072. * unlink this URB from the hardware. So there's no more work to do.
  1073. */
  1074. if (urb->unlinked)
  1075. return -EBUSY;
  1076. urb->unlinked = status;
  1077. return 0;
  1078. }
  1079. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  1080. /**
  1081. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  1082. * @hcd: host controller to which @urb was submitted
  1083. * @urb: URB being unlinked
  1084. *
  1085. * Host controller drivers should call this routine before calling
  1086. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  1087. * interrupts must be disabled. The actions carried out here are required
  1088. * for URB completion.
  1089. */
  1090. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  1091. {
  1092. /* clear all state linking urb to this dev (and hcd) */
  1093. spin_lock(&hcd_urb_list_lock);
  1094. list_del_init(&urb->urb_list);
  1095. spin_unlock(&hcd_urb_list_lock);
  1096. }
  1097. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  1098. /*
  1099. * Some usb host controllers can only perform dma using a small SRAM area.
  1100. * The usb core itself is however optimized for host controllers that can dma
  1101. * using regular system memory - like pci devices doing bus mastering.
  1102. *
  1103. * To support host controllers with limited dma capabilities we provide dma
  1104. * bounce buffers. This feature can be enabled by initializing
  1105. * hcd->localmem_pool using usb_hcd_setup_local_mem().
  1106. *
  1107. * The initialized hcd->localmem_pool then tells the usb code to allocate all
  1108. * data for dma using the genalloc API.
  1109. *
  1110. * So, to summarize...
  1111. *
  1112. * - We need "local" memory, canonical example being
  1113. * a small SRAM on a discrete controller being the
  1114. * only memory that the controller can read ...
  1115. * (a) "normal" kernel memory is no good, and
  1116. * (b) there's not enough to share
  1117. *
  1118. * - So we use that, even though the primary requirement
  1119. * is that the memory be "local" (hence addressable
  1120. * by that device), not "coherent".
  1121. *
  1122. */
  1123. static int hcd_alloc_coherent(struct usb_bus *bus,
  1124. gfp_t mem_flags, dma_addr_t *dma_handle,
  1125. void **vaddr_handle, size_t size,
  1126. enum dma_data_direction dir)
  1127. {
  1128. unsigned char *vaddr;
  1129. if (*vaddr_handle == NULL) {
  1130. WARN_ON_ONCE(1);
  1131. return -EFAULT;
  1132. }
  1133. vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
  1134. mem_flags, dma_handle);
  1135. if (!vaddr)
  1136. return -ENOMEM;
  1137. /*
  1138. * Store the virtual address of the buffer at the end
  1139. * of the allocated dma buffer. The size of the buffer
  1140. * may be uneven so use unaligned functions instead
  1141. * of just rounding up. It makes sense to optimize for
  1142. * memory footprint over access speed since the amount
  1143. * of memory available for dma may be limited.
  1144. */
  1145. put_unaligned((unsigned long)*vaddr_handle,
  1146. (unsigned long *)(vaddr + size));
  1147. if (dir == DMA_TO_DEVICE)
  1148. memcpy(vaddr, *vaddr_handle, size);
  1149. *vaddr_handle = vaddr;
  1150. return 0;
  1151. }
  1152. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1153. void **vaddr_handle, size_t size,
  1154. enum dma_data_direction dir)
  1155. {
  1156. unsigned char *vaddr = *vaddr_handle;
  1157. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1158. if (dir == DMA_FROM_DEVICE)
  1159. memcpy(vaddr, *vaddr_handle, size);
  1160. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1161. *vaddr_handle = vaddr;
  1162. *dma_handle = 0;
  1163. }
  1164. void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1165. {
  1166. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1167. (urb->transfer_flags & URB_SETUP_MAP_SINGLE))
  1168. dma_unmap_single(hcd->self.sysdev,
  1169. urb->setup_dma,
  1170. sizeof(struct usb_ctrlrequest),
  1171. DMA_TO_DEVICE);
  1172. else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
  1173. hcd_free_coherent(urb->dev->bus,
  1174. &urb->setup_dma,
  1175. (void **) &urb->setup_packet,
  1176. sizeof(struct usb_ctrlrequest),
  1177. DMA_TO_DEVICE);
  1178. /* Make it safe to call this routine more than once */
  1179. urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
  1180. }
  1181. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
  1182. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1183. {
  1184. if (hcd->driver->unmap_urb_for_dma)
  1185. hcd->driver->unmap_urb_for_dma(hcd, urb);
  1186. else
  1187. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1188. }
  1189. void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1190. {
  1191. enum dma_data_direction dir;
  1192. usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
  1193. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1194. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1195. (urb->transfer_flags & URB_DMA_MAP_SG))
  1196. dma_unmap_sg(hcd->self.sysdev,
  1197. urb->sg,
  1198. urb->num_sgs,
  1199. dir);
  1200. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1201. (urb->transfer_flags & URB_DMA_MAP_PAGE))
  1202. dma_unmap_page(hcd->self.sysdev,
  1203. urb->transfer_dma,
  1204. urb->transfer_buffer_length,
  1205. dir);
  1206. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1207. (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  1208. dma_unmap_single(hcd->self.sysdev,
  1209. urb->transfer_dma,
  1210. urb->transfer_buffer_length,
  1211. dir);
  1212. else if (urb->transfer_flags & URB_MAP_LOCAL)
  1213. hcd_free_coherent(urb->dev->bus,
  1214. &urb->transfer_dma,
  1215. &urb->transfer_buffer,
  1216. urb->transfer_buffer_length,
  1217. dir);
  1218. /* Make it safe to call this routine more than once */
  1219. urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
  1220. URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
  1221. }
  1222. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
  1223. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1224. gfp_t mem_flags)
  1225. {
  1226. if (hcd->driver->map_urb_for_dma)
  1227. return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
  1228. else
  1229. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  1230. }
  1231. int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1232. gfp_t mem_flags)
  1233. {
  1234. enum dma_data_direction dir;
  1235. int ret = 0;
  1236. /* Map the URB's buffers for DMA access.
  1237. * Lower level HCD code should use *_dma exclusively,
  1238. * unless it uses pio or talks to another transport,
  1239. * or uses the provided scatter gather list for bulk.
  1240. */
  1241. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  1242. if (hcd->self.uses_pio_for_control)
  1243. return ret;
  1244. if (hcd->localmem_pool) {
  1245. ret = hcd_alloc_coherent(
  1246. urb->dev->bus, mem_flags,
  1247. &urb->setup_dma,
  1248. (void **)&urb->setup_packet,
  1249. sizeof(struct usb_ctrlrequest),
  1250. DMA_TO_DEVICE);
  1251. if (ret)
  1252. return ret;
  1253. urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
  1254. } else if (hcd_uses_dma(hcd)) {
  1255. if (object_is_on_stack(urb->setup_packet)) {
  1256. WARN_ONCE(1, "setup packet is on stack\n");
  1257. return -EAGAIN;
  1258. }
  1259. urb->setup_dma = dma_map_single(
  1260. hcd->self.sysdev,
  1261. urb->setup_packet,
  1262. sizeof(struct usb_ctrlrequest),
  1263. DMA_TO_DEVICE);
  1264. if (dma_mapping_error(hcd->self.sysdev,
  1265. urb->setup_dma))
  1266. return -EAGAIN;
  1267. urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
  1268. }
  1269. }
  1270. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1271. if (urb->transfer_buffer_length != 0
  1272. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1273. if (hcd->localmem_pool) {
  1274. ret = hcd_alloc_coherent(
  1275. urb->dev->bus, mem_flags,
  1276. &urb->transfer_dma,
  1277. &urb->transfer_buffer,
  1278. urb->transfer_buffer_length,
  1279. dir);
  1280. if (ret == 0)
  1281. urb->transfer_flags |= URB_MAP_LOCAL;
  1282. } else if (hcd_uses_dma(hcd)) {
  1283. if (urb->num_sgs) {
  1284. int n;
  1285. /* We don't support sg for isoc transfers ! */
  1286. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1287. WARN_ON(1);
  1288. return -EINVAL;
  1289. }
  1290. n = dma_map_sg(
  1291. hcd->self.sysdev,
  1292. urb->sg,
  1293. urb->num_sgs,
  1294. dir);
  1295. if (n <= 0)
  1296. ret = -EAGAIN;
  1297. else
  1298. urb->transfer_flags |= URB_DMA_MAP_SG;
  1299. urb->num_mapped_sgs = n;
  1300. if (n != urb->num_sgs)
  1301. urb->transfer_flags |=
  1302. URB_DMA_SG_COMBINED;
  1303. } else if (urb->sg) {
  1304. struct scatterlist *sg = urb->sg;
  1305. urb->transfer_dma = dma_map_page(
  1306. hcd->self.sysdev,
  1307. sg_page(sg),
  1308. sg->offset,
  1309. urb->transfer_buffer_length,
  1310. dir);
  1311. if (dma_mapping_error(hcd->self.sysdev,
  1312. urb->transfer_dma))
  1313. ret = -EAGAIN;
  1314. else
  1315. urb->transfer_flags |= URB_DMA_MAP_PAGE;
  1316. } else if (object_is_on_stack(urb->transfer_buffer)) {
  1317. WARN_ONCE(1, "transfer buffer is on stack\n");
  1318. ret = -EAGAIN;
  1319. } else {
  1320. urb->transfer_dma = dma_map_single(
  1321. hcd->self.sysdev,
  1322. urb->transfer_buffer,
  1323. urb->transfer_buffer_length,
  1324. dir);
  1325. if (dma_mapping_error(hcd->self.sysdev,
  1326. urb->transfer_dma))
  1327. ret = -EAGAIN;
  1328. else
  1329. urb->transfer_flags |= URB_DMA_MAP_SINGLE;
  1330. }
  1331. }
  1332. if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
  1333. URB_SETUP_MAP_LOCAL)))
  1334. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1335. }
  1336. return ret;
  1337. }
  1338. EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
  1339. /*-------------------------------------------------------------------------*/
  1340. /* may be called in any context with a valid urb->dev usecount
  1341. * caller surrenders "ownership" of urb
  1342. * expects usb_submit_urb() to have sanity checked and conditioned all
  1343. * inputs in the urb
  1344. */
  1345. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1346. {
  1347. int status;
  1348. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1349. /* increment urb's reference count as part of giving it to the HCD
  1350. * (which will control it). HCD guarantees that it either returns
  1351. * an error or calls giveback(), but not both.
  1352. */
  1353. usb_get_urb(urb);
  1354. atomic_inc(&urb->use_count);
  1355. atomic_inc(&urb->dev->urbnum);
  1356. usbmon_urb_submit(&hcd->self, urb);
  1357. /* NOTE requirements on root-hub callers (usbfs and the hub
  1358. * driver, for now): URBs' urb->transfer_buffer must be
  1359. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1360. * they could clobber root hub response data. Also, control
  1361. * URBs must be submitted in process context with interrupts
  1362. * enabled.
  1363. */
  1364. if (is_root_hub(urb->dev)) {
  1365. status = rh_urb_enqueue(hcd, urb);
  1366. } else {
  1367. status = map_urb_for_dma(hcd, urb, mem_flags);
  1368. if (likely(status == 0)) {
  1369. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1370. if (unlikely(status))
  1371. unmap_urb_for_dma(hcd, urb);
  1372. }
  1373. }
  1374. if (unlikely(status)) {
  1375. usbmon_urb_submit_error(&hcd->self, urb, status);
  1376. urb->hcpriv = NULL;
  1377. INIT_LIST_HEAD(&urb->urb_list);
  1378. atomic_dec(&urb->use_count);
  1379. /*
  1380. * Order the write of urb->use_count above before the read
  1381. * of urb->reject below. Pairs with the memory barriers in
  1382. * usb_kill_urb() and usb_poison_urb().
  1383. */
  1384. smp_mb__after_atomic();
  1385. atomic_dec(&urb->dev->urbnum);
  1386. if (atomic_read(&urb->reject))
  1387. wake_up(&usb_kill_urb_queue);
  1388. usb_put_urb(urb);
  1389. }
  1390. return status;
  1391. }
  1392. /*-------------------------------------------------------------------------*/
  1393. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1394. * off hardware queues (which may take a while) and returning it as
  1395. * soon as practical. we've already set up the urb's return status,
  1396. * but we can't know if the callback completed already.
  1397. */
  1398. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1399. {
  1400. int value;
  1401. if (is_root_hub(urb->dev))
  1402. value = usb_rh_urb_dequeue(hcd, urb, status);
  1403. else {
  1404. /* The only reason an HCD might fail this call is if
  1405. * it has not yet fully queued the urb to begin with.
  1406. * Such failures should be harmless. */
  1407. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1408. }
  1409. return value;
  1410. }
  1411. /*
  1412. * called in any context
  1413. *
  1414. * caller guarantees urb won't be recycled till both unlink()
  1415. * and the urb's completion function return
  1416. */
  1417. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1418. {
  1419. struct usb_hcd *hcd;
  1420. struct usb_device *udev = urb->dev;
  1421. int retval = -EIDRM;
  1422. unsigned long flags;
  1423. /* Prevent the device and bus from going away while
  1424. * the unlink is carried out. If they are already gone
  1425. * then urb->use_count must be 0, since disconnected
  1426. * devices can't have any active URBs.
  1427. */
  1428. spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
  1429. if (atomic_read(&urb->use_count) > 0) {
  1430. retval = 0;
  1431. usb_get_dev(udev);
  1432. }
  1433. spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
  1434. if (retval == 0) {
  1435. hcd = bus_to_hcd(urb->dev->bus);
  1436. retval = unlink1(hcd, urb, status);
  1437. if (retval == 0)
  1438. retval = -EINPROGRESS;
  1439. else if (retval != -EIDRM && retval != -EBUSY)
  1440. dev_dbg(&udev->dev, "hcd_unlink_urb %pK fail %d\n",
  1441. urb, retval);
  1442. usb_put_dev(udev);
  1443. }
  1444. return retval;
  1445. }
  1446. /*-------------------------------------------------------------------------*/
  1447. static void __usb_hcd_giveback_urb(struct urb *urb)
  1448. {
  1449. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1450. struct usb_anchor *anchor = urb->anchor;
  1451. int status = urb->unlinked;
  1452. urb->hcpriv = NULL;
  1453. if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1454. urb->actual_length < urb->transfer_buffer_length &&
  1455. !status))
  1456. status = -EREMOTEIO;
  1457. unmap_urb_for_dma(hcd, urb);
  1458. usbmon_urb_complete(&hcd->self, urb, status);
  1459. usb_anchor_suspend_wakeups(anchor);
  1460. usb_unanchor_urb(urb);
  1461. if (likely(status == 0))
  1462. usb_led_activity(USB_LED_EVENT_HOST);
  1463. /* pass ownership to the completion handler */
  1464. urb->status = status;
  1465. /*
  1466. * This function can be called in task context inside another remote
  1467. * coverage collection section, but KCOV doesn't support that kind of
  1468. * recursion yet. Only collect coverage in softirq context for now.
  1469. */
  1470. if (in_serving_softirq())
  1471. kcov_remote_start_usb((u64)urb->dev->bus->busnum);
  1472. urb->complete(urb);
  1473. if (in_serving_softirq())
  1474. kcov_remote_stop();
  1475. usb_anchor_resume_wakeups(anchor);
  1476. atomic_dec(&urb->use_count);
  1477. /*
  1478. * Order the write of urb->use_count above before the read
  1479. * of urb->reject below. Pairs with the memory barriers in
  1480. * usb_kill_urb() and usb_poison_urb().
  1481. */
  1482. smp_mb__after_atomic();
  1483. if (unlikely(atomic_read(&urb->reject)))
  1484. wake_up(&usb_kill_urb_queue);
  1485. usb_put_urb(urb);
  1486. }
  1487. static void usb_giveback_urb_bh(struct tasklet_struct *t)
  1488. {
  1489. struct giveback_urb_bh *bh = from_tasklet(bh, t, bh);
  1490. struct list_head local_list;
  1491. spin_lock_irq(&bh->lock);
  1492. bh->running = true;
  1493. restart:
  1494. list_replace_init(&bh->head, &local_list);
  1495. spin_unlock_irq(&bh->lock);
  1496. while (!list_empty(&local_list)) {
  1497. struct urb *urb;
  1498. urb = list_entry(local_list.next, struct urb, urb_list);
  1499. list_del_init(&urb->urb_list);
  1500. bh->completing_ep = urb->ep;
  1501. __usb_hcd_giveback_urb(urb);
  1502. bh->completing_ep = NULL;
  1503. }
  1504. /* check if there are new URBs to giveback */
  1505. spin_lock_irq(&bh->lock);
  1506. if (!list_empty(&bh->head))
  1507. goto restart;
  1508. bh->running = false;
  1509. spin_unlock_irq(&bh->lock);
  1510. }
  1511. /**
  1512. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1513. * @hcd: host controller returning the URB
  1514. * @urb: urb being returned to the USB device driver.
  1515. * @status: completion status code for the URB.
  1516. * Context: in_interrupt()
  1517. *
  1518. * This hands the URB from HCD to its USB device driver, using its
  1519. * completion function. The HCD has freed all per-urb resources
  1520. * (and is done using urb->hcpriv). It also released all HCD locks;
  1521. * the device driver won't cause problems if it frees, modifies,
  1522. * or resubmits this URB.
  1523. *
  1524. * If @urb was unlinked, the value of @status will be overridden by
  1525. * @urb->unlinked. Erroneous short transfers are detected in case
  1526. * the HCD hasn't checked for them.
  1527. */
  1528. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1529. {
  1530. struct giveback_urb_bh *bh;
  1531. bool running, high_prio_bh;
  1532. /* pass status to tasklet via unlinked */
  1533. if (likely(!urb->unlinked))
  1534. urb->unlinked = status;
  1535. if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
  1536. __usb_hcd_giveback_urb(urb);
  1537. return;
  1538. }
  1539. if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) {
  1540. bh = &hcd->high_prio_bh;
  1541. high_prio_bh = true;
  1542. } else {
  1543. bh = &hcd->low_prio_bh;
  1544. high_prio_bh = false;
  1545. }
  1546. spin_lock(&bh->lock);
  1547. list_add_tail(&urb->urb_list, &bh->head);
  1548. running = bh->running;
  1549. spin_unlock(&bh->lock);
  1550. if (running)
  1551. ;
  1552. else if (high_prio_bh)
  1553. tasklet_hi_schedule(&bh->bh);
  1554. else
  1555. tasklet_schedule(&bh->bh);
  1556. }
  1557. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1558. /*-------------------------------------------------------------------------*/
  1559. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1560. * queue to drain completely. The caller must first insure that no more
  1561. * URBs can be submitted for this endpoint.
  1562. */
  1563. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1564. struct usb_host_endpoint *ep)
  1565. {
  1566. struct usb_hcd *hcd;
  1567. struct urb *urb;
  1568. if (!ep)
  1569. return;
  1570. might_sleep();
  1571. hcd = bus_to_hcd(udev->bus);
  1572. /* No more submits can occur */
  1573. spin_lock_irq(&hcd_urb_list_lock);
  1574. rescan:
  1575. list_for_each_entry_reverse(urb, &ep->urb_list, urb_list) {
  1576. int is_in;
  1577. if (urb->unlinked)
  1578. continue;
  1579. usb_get_urb (urb);
  1580. is_in = usb_urb_dir_in(urb);
  1581. spin_unlock(&hcd_urb_list_lock);
  1582. /* kick hcd */
  1583. unlink1(hcd, urb, -ESHUTDOWN);
  1584. dev_dbg (hcd->self.controller,
  1585. "shutdown urb %pK ep%d%s-%s\n",
  1586. urb, usb_endpoint_num(&ep->desc),
  1587. is_in ? "in" : "out",
  1588. usb_ep_type_string(usb_endpoint_type(&ep->desc)));
  1589. usb_put_urb (urb);
  1590. /* list contents may have changed */
  1591. spin_lock(&hcd_urb_list_lock);
  1592. goto rescan;
  1593. }
  1594. spin_unlock_irq(&hcd_urb_list_lock);
  1595. /* Wait until the endpoint queue is completely empty */
  1596. while (!list_empty (&ep->urb_list)) {
  1597. spin_lock_irq(&hcd_urb_list_lock);
  1598. /* The list may have changed while we acquired the spinlock */
  1599. urb = NULL;
  1600. if (!list_empty (&ep->urb_list)) {
  1601. urb = list_entry (ep->urb_list.prev, struct urb,
  1602. urb_list);
  1603. usb_get_urb (urb);
  1604. }
  1605. spin_unlock_irq(&hcd_urb_list_lock);
  1606. if (urb) {
  1607. usb_kill_urb (urb);
  1608. usb_put_urb (urb);
  1609. }
  1610. }
  1611. }
  1612. /**
  1613. * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
  1614. * the bus bandwidth
  1615. * @udev: target &usb_device
  1616. * @new_config: new configuration to install
  1617. * @cur_alt: the current alternate interface setting
  1618. * @new_alt: alternate interface setting that is being installed
  1619. *
  1620. * To change configurations, pass in the new configuration in new_config,
  1621. * and pass NULL for cur_alt and new_alt.
  1622. *
  1623. * To reset a device's configuration (put the device in the ADDRESSED state),
  1624. * pass in NULL for new_config, cur_alt, and new_alt.
  1625. *
  1626. * To change alternate interface settings, pass in NULL for new_config,
  1627. * pass in the current alternate interface setting in cur_alt,
  1628. * and pass in the new alternate interface setting in new_alt.
  1629. *
  1630. * Return: An error if the requested bandwidth change exceeds the
  1631. * bus bandwidth or host controller internal resources.
  1632. */
  1633. int usb_hcd_alloc_bandwidth(struct usb_device *udev,
  1634. struct usb_host_config *new_config,
  1635. struct usb_host_interface *cur_alt,
  1636. struct usb_host_interface *new_alt)
  1637. {
  1638. int num_intfs, i, j;
  1639. struct usb_host_interface *alt = NULL;
  1640. int ret = 0;
  1641. struct usb_hcd *hcd;
  1642. struct usb_host_endpoint *ep;
  1643. hcd = bus_to_hcd(udev->bus);
  1644. if (!hcd->driver->check_bandwidth)
  1645. return 0;
  1646. /* Configuration is being removed - set configuration 0 */
  1647. if (!new_config && !cur_alt) {
  1648. for (i = 1; i < 16; ++i) {
  1649. ep = udev->ep_out[i];
  1650. if (ep)
  1651. hcd->driver->drop_endpoint(hcd, udev, ep);
  1652. ep = udev->ep_in[i];
  1653. if (ep)
  1654. hcd->driver->drop_endpoint(hcd, udev, ep);
  1655. }
  1656. hcd->driver->check_bandwidth(hcd, udev);
  1657. return 0;
  1658. }
  1659. /* Check if the HCD says there's enough bandwidth. Enable all endpoints
  1660. * each interface's alt setting 0 and ask the HCD to check the bandwidth
  1661. * of the bus. There will always be bandwidth for endpoint 0, so it's
  1662. * ok to exclude it.
  1663. */
  1664. if (new_config) {
  1665. num_intfs = new_config->desc.bNumInterfaces;
  1666. /* Remove endpoints (except endpoint 0, which is always on the
  1667. * schedule) from the old config from the schedule
  1668. */
  1669. for (i = 1; i < 16; ++i) {
  1670. ep = udev->ep_out[i];
  1671. if (ep) {
  1672. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1673. if (ret < 0)
  1674. goto reset;
  1675. }
  1676. ep = udev->ep_in[i];
  1677. if (ep) {
  1678. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1679. if (ret < 0)
  1680. goto reset;
  1681. }
  1682. }
  1683. for (i = 0; i < num_intfs; ++i) {
  1684. struct usb_host_interface *first_alt;
  1685. int iface_num;
  1686. first_alt = &new_config->intf_cache[i]->altsetting[0];
  1687. iface_num = first_alt->desc.bInterfaceNumber;
  1688. /* Set up endpoints for alternate interface setting 0 */
  1689. alt = usb_find_alt_setting(new_config, iface_num, 0);
  1690. if (!alt)
  1691. /* No alt setting 0? Pick the first setting. */
  1692. alt = first_alt;
  1693. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  1694. ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
  1695. if (ret < 0)
  1696. goto reset;
  1697. }
  1698. }
  1699. }
  1700. if (cur_alt && new_alt) {
  1701. struct usb_interface *iface = usb_ifnum_to_if(udev,
  1702. cur_alt->desc.bInterfaceNumber);
  1703. if (!iface)
  1704. return -EINVAL;
  1705. if (iface->resetting_device) {
  1706. /*
  1707. * The USB core just reset the device, so the xHCI host
  1708. * and the device will think alt setting 0 is installed.
  1709. * However, the USB core will pass in the alternate
  1710. * setting installed before the reset as cur_alt. Dig
  1711. * out the alternate setting 0 structure, or the first
  1712. * alternate setting if a broken device doesn't have alt
  1713. * setting 0.
  1714. */
  1715. cur_alt = usb_altnum_to_altsetting(iface, 0);
  1716. if (!cur_alt)
  1717. cur_alt = &iface->altsetting[0];
  1718. }
  1719. /* Drop all the endpoints in the current alt setting */
  1720. for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
  1721. ret = hcd->driver->drop_endpoint(hcd, udev,
  1722. &cur_alt->endpoint[i]);
  1723. if (ret < 0)
  1724. goto reset;
  1725. }
  1726. /* Add all the endpoints in the new alt setting */
  1727. for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
  1728. ret = hcd->driver->add_endpoint(hcd, udev,
  1729. &new_alt->endpoint[i]);
  1730. if (ret < 0)
  1731. goto reset;
  1732. }
  1733. }
  1734. ret = hcd->driver->check_bandwidth(hcd, udev);
  1735. reset:
  1736. if (ret < 0)
  1737. hcd->driver->reset_bandwidth(hcd, udev);
  1738. return ret;
  1739. }
  1740. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1741. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1742. * have been called previously. Use for set_configuration, set_interface,
  1743. * driver removal, physical disconnect.
  1744. *
  1745. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1746. * type, maxpacket size, toggle, halt status, and scheduling.
  1747. */
  1748. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1749. struct usb_host_endpoint *ep)
  1750. {
  1751. struct usb_hcd *hcd;
  1752. might_sleep();
  1753. hcd = bus_to_hcd(udev->bus);
  1754. if (hcd->driver->endpoint_disable)
  1755. hcd->driver->endpoint_disable(hcd, ep);
  1756. }
  1757. /**
  1758. * usb_hcd_reset_endpoint - reset host endpoint state
  1759. * @udev: USB device.
  1760. * @ep: the endpoint to reset.
  1761. *
  1762. * Resets any host endpoint state such as the toggle bit, sequence
  1763. * number and current window.
  1764. */
  1765. void usb_hcd_reset_endpoint(struct usb_device *udev,
  1766. struct usb_host_endpoint *ep)
  1767. {
  1768. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1769. if (hcd->driver->endpoint_reset)
  1770. hcd->driver->endpoint_reset(hcd, ep);
  1771. else {
  1772. int epnum = usb_endpoint_num(&ep->desc);
  1773. int is_out = usb_endpoint_dir_out(&ep->desc);
  1774. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1775. usb_settoggle(udev, epnum, is_out, 0);
  1776. if (is_control)
  1777. usb_settoggle(udev, epnum, !is_out, 0);
  1778. }
  1779. }
  1780. /**
  1781. * usb_alloc_streams - allocate bulk endpoint stream IDs.
  1782. * @interface: alternate setting that includes all endpoints.
  1783. * @eps: array of endpoints that need streams.
  1784. * @num_eps: number of endpoints in the array.
  1785. * @num_streams: number of streams to allocate.
  1786. * @mem_flags: flags hcd should use to allocate memory.
  1787. *
  1788. * Sets up a group of bulk endpoints to have @num_streams stream IDs available.
  1789. * Drivers may queue multiple transfers to different stream IDs, which may
  1790. * complete in a different order than they were queued.
  1791. *
  1792. * Return: On success, the number of allocated streams. On failure, a negative
  1793. * error code.
  1794. */
  1795. int usb_alloc_streams(struct usb_interface *interface,
  1796. struct usb_host_endpoint **eps, unsigned int num_eps,
  1797. unsigned int num_streams, gfp_t mem_flags)
  1798. {
  1799. struct usb_hcd *hcd;
  1800. struct usb_device *dev;
  1801. int i, ret;
  1802. dev = interface_to_usbdev(interface);
  1803. hcd = bus_to_hcd(dev->bus);
  1804. if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
  1805. return -EINVAL;
  1806. if (dev->speed < USB_SPEED_SUPER)
  1807. return -EINVAL;
  1808. if (dev->state < USB_STATE_CONFIGURED)
  1809. return -ENODEV;
  1810. for (i = 0; i < num_eps; i++) {
  1811. /* Streams only apply to bulk endpoints. */
  1812. if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
  1813. return -EINVAL;
  1814. /* Re-alloc is not allowed */
  1815. if (eps[i]->streams)
  1816. return -EINVAL;
  1817. }
  1818. ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
  1819. num_streams, mem_flags);
  1820. if (ret < 0)
  1821. return ret;
  1822. for (i = 0; i < num_eps; i++)
  1823. eps[i]->streams = ret;
  1824. return ret;
  1825. }
  1826. EXPORT_SYMBOL_GPL(usb_alloc_streams);
  1827. /**
  1828. * usb_free_streams - free bulk endpoint stream IDs.
  1829. * @interface: alternate setting that includes all endpoints.
  1830. * @eps: array of endpoints to remove streams from.
  1831. * @num_eps: number of endpoints in the array.
  1832. * @mem_flags: flags hcd should use to allocate memory.
  1833. *
  1834. * Reverts a group of bulk endpoints back to not using stream IDs.
  1835. * Can fail if we are given bad arguments, or HCD is broken.
  1836. *
  1837. * Return: 0 on success. On failure, a negative error code.
  1838. */
  1839. int usb_free_streams(struct usb_interface *interface,
  1840. struct usb_host_endpoint **eps, unsigned int num_eps,
  1841. gfp_t mem_flags)
  1842. {
  1843. struct usb_hcd *hcd;
  1844. struct usb_device *dev;
  1845. int i, ret;
  1846. dev = interface_to_usbdev(interface);
  1847. hcd = bus_to_hcd(dev->bus);
  1848. if (dev->speed < USB_SPEED_SUPER)
  1849. return -EINVAL;
  1850. /* Double-free is not allowed */
  1851. for (i = 0; i < num_eps; i++)
  1852. if (!eps[i] || !eps[i]->streams)
  1853. return -EINVAL;
  1854. ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
  1855. if (ret < 0)
  1856. return ret;
  1857. for (i = 0; i < num_eps; i++)
  1858. eps[i]->streams = 0;
  1859. return ret;
  1860. }
  1861. EXPORT_SYMBOL_GPL(usb_free_streams);
  1862. /* Protect against drivers that try to unlink URBs after the device
  1863. * is gone, by waiting until all unlinks for @udev are finished.
  1864. * Since we don't currently track URBs by device, simply wait until
  1865. * nothing is running in the locked region of usb_hcd_unlink_urb().
  1866. */
  1867. void usb_hcd_synchronize_unlinks(struct usb_device *udev)
  1868. {
  1869. spin_lock_irq(&hcd_urb_unlink_lock);
  1870. spin_unlock_irq(&hcd_urb_unlink_lock);
  1871. }
  1872. /*-------------------------------------------------------------------------*/
  1873. /* called in any context */
  1874. int usb_hcd_get_frame_number (struct usb_device *udev)
  1875. {
  1876. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1877. if (!HCD_RH_RUNNING(hcd))
  1878. return -ESHUTDOWN;
  1879. return hcd->driver->get_frame_number (hcd);
  1880. }
  1881. /*-------------------------------------------------------------------------*/
  1882. #ifdef CONFIG_PM
  1883. int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
  1884. {
  1885. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  1886. int status;
  1887. int old_state = hcd->state;
  1888. dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
  1889. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  1890. rhdev->do_remote_wakeup);
  1891. if (HCD_DEAD(hcd)) {
  1892. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
  1893. return 0;
  1894. }
  1895. if (!hcd->driver->bus_suspend) {
  1896. status = -ENOENT;
  1897. } else {
  1898. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1899. hcd->state = HC_STATE_QUIESCING;
  1900. status = hcd->driver->bus_suspend(hcd);
  1901. }
  1902. if (status == 0) {
  1903. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  1904. hcd->state = HC_STATE_SUSPENDED;
  1905. if (!PMSG_IS_AUTO(msg))
  1906. usb_phy_roothub_suspend(hcd->self.sysdev,
  1907. hcd->phy_roothub);
  1908. /* Did we race with a root-hub wakeup event? */
  1909. if (rhdev->do_remote_wakeup) {
  1910. char buffer[6];
  1911. status = hcd->driver->hub_status_data(hcd, buffer);
  1912. if (status != 0) {
  1913. dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
  1914. hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
  1915. status = -EBUSY;
  1916. }
  1917. }
  1918. } else {
  1919. spin_lock_irq(&hcd_root_hub_lock);
  1920. if (!HCD_DEAD(hcd)) {
  1921. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1922. hcd->state = old_state;
  1923. }
  1924. spin_unlock_irq(&hcd_root_hub_lock);
  1925. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1926. "suspend", status);
  1927. }
  1928. return status;
  1929. }
  1930. int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
  1931. {
  1932. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  1933. int status;
  1934. int old_state = hcd->state;
  1935. dev_dbg(&rhdev->dev, "usb %sresume\n",
  1936. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  1937. if (HCD_DEAD(hcd)) {
  1938. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
  1939. return 0;
  1940. }
  1941. if (!PMSG_IS_AUTO(msg)) {
  1942. status = usb_phy_roothub_resume(hcd->self.sysdev,
  1943. hcd->phy_roothub);
  1944. if (status)
  1945. return status;
  1946. }
  1947. if (!hcd->driver->bus_resume)
  1948. return -ENOENT;
  1949. if (HCD_RH_RUNNING(hcd))
  1950. return 0;
  1951. hcd->state = HC_STATE_RESUMING;
  1952. status = hcd->driver->bus_resume(hcd);
  1953. clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  1954. if (status == 0)
  1955. status = usb_phy_roothub_calibrate(hcd->phy_roothub);
  1956. if (status == 0) {
  1957. struct usb_device *udev;
  1958. int port1;
  1959. spin_lock_irq(&hcd_root_hub_lock);
  1960. if (!HCD_DEAD(hcd)) {
  1961. usb_set_device_state(rhdev, rhdev->actconfig
  1962. ? USB_STATE_CONFIGURED
  1963. : USB_STATE_ADDRESS);
  1964. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1965. hcd->state = HC_STATE_RUNNING;
  1966. }
  1967. spin_unlock_irq(&hcd_root_hub_lock);
  1968. /*
  1969. * Check whether any of the enabled ports on the root hub are
  1970. * unsuspended. If they are then a TRSMRCY delay is needed
  1971. * (this is what the USB-2 spec calls a "global resume").
  1972. * Otherwise we can skip the delay.
  1973. */
  1974. usb_hub_for_each_child(rhdev, port1, udev) {
  1975. if (udev->state != USB_STATE_NOTATTACHED &&
  1976. !udev->port_is_suspended) {
  1977. usleep_range(10000, 11000); /* TRSMRCY */
  1978. break;
  1979. }
  1980. }
  1981. } else {
  1982. hcd->state = old_state;
  1983. usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
  1984. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1985. "resume", status);
  1986. if (status != -ESHUTDOWN)
  1987. usb_hc_died(hcd);
  1988. }
  1989. return status;
  1990. }
  1991. /* Workqueue routine for root-hub remote wakeup */
  1992. static void hcd_resume_work(struct work_struct *work)
  1993. {
  1994. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  1995. struct usb_device *udev = hcd->self.root_hub;
  1996. usb_remote_wakeup(udev);
  1997. }
  1998. /**
  1999. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  2000. * @hcd: host controller for this root hub
  2001. *
  2002. * The USB host controller calls this function when its root hub is
  2003. * suspended (with the remote wakeup feature enabled) and a remote
  2004. * wakeup request is received. The routine submits a workqueue request
  2005. * to resume the root hub (that is, manage its downstream ports again).
  2006. */
  2007. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  2008. {
  2009. unsigned long flags;
  2010. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2011. if (hcd->rh_registered) {
  2012. pm_wakeup_event(&hcd->self.root_hub->dev, 0);
  2013. set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  2014. queue_work(pm_wq, &hcd->wakeup_work);
  2015. }
  2016. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2017. }
  2018. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  2019. #endif /* CONFIG_PM */
  2020. /*-------------------------------------------------------------------------*/
  2021. #ifdef CONFIG_USB_OTG
  2022. /**
  2023. * usb_bus_start_enum - start immediate enumeration (for OTG)
  2024. * @bus: the bus (must use hcd framework)
  2025. * @port_num: 1-based number of port; usually bus->otg_port
  2026. * Context: in_interrupt()
  2027. *
  2028. * Starts enumeration, with an immediate reset followed later by
  2029. * hub_wq identifying and possibly configuring the device.
  2030. * This is needed by OTG controller drivers, where it helps meet
  2031. * HNP protocol timing requirements for starting a port reset.
  2032. *
  2033. * Return: 0 if successful.
  2034. */
  2035. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  2036. {
  2037. struct usb_hcd *hcd;
  2038. int status = -EOPNOTSUPP;
  2039. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  2040. * boards with root hubs hooked up to internal devices (instead of
  2041. * just the OTG port) may need more attention to resetting...
  2042. */
  2043. hcd = bus_to_hcd(bus);
  2044. if (port_num && hcd->driver->start_port_reset)
  2045. status = hcd->driver->start_port_reset(hcd, port_num);
  2046. /* allocate hub_wq shortly after (first) root port reset finishes;
  2047. * it may issue others, until at least 50 msecs have passed.
  2048. */
  2049. if (status == 0)
  2050. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  2051. return status;
  2052. }
  2053. EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  2054. #endif
  2055. /*-------------------------------------------------------------------------*/
  2056. /**
  2057. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  2058. * @irq: the IRQ being raised
  2059. * @__hcd: pointer to the HCD whose IRQ is being signaled
  2060. *
  2061. * If the controller isn't HALTed, calls the driver's irq handler.
  2062. * Checks whether the controller is now dead.
  2063. *
  2064. * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise.
  2065. */
  2066. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  2067. {
  2068. struct usb_hcd *hcd = __hcd;
  2069. irqreturn_t rc;
  2070. if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd)))
  2071. rc = IRQ_NONE;
  2072. else if (hcd->driver->irq(hcd) == IRQ_NONE)
  2073. rc = IRQ_NONE;
  2074. else
  2075. rc = IRQ_HANDLED;
  2076. return rc;
  2077. }
  2078. EXPORT_SYMBOL_GPL(usb_hcd_irq);
  2079. /*-------------------------------------------------------------------------*/
  2080. /* Workqueue routine for when the root-hub has died. */
  2081. static void hcd_died_work(struct work_struct *work)
  2082. {
  2083. struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work);
  2084. static char *env[] = {
  2085. "ERROR=DEAD",
  2086. NULL
  2087. };
  2088. /* Notify user space that the host controller has died */
  2089. kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_OFFLINE, env);
  2090. }
  2091. /**
  2092. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  2093. * @hcd: pointer to the HCD representing the controller
  2094. *
  2095. * This is called by bus glue to report a USB host controller that died
  2096. * while operations may still have been pending. It's called automatically
  2097. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  2098. *
  2099. * Only call this function with the primary HCD.
  2100. */
  2101. void usb_hc_died (struct usb_hcd *hcd)
  2102. {
  2103. unsigned long flags;
  2104. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  2105. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2106. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2107. set_bit(HCD_FLAG_DEAD, &hcd->flags);
  2108. if (hcd->rh_registered) {
  2109. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2110. /* make hub_wq clean up old urbs and devices */
  2111. usb_set_device_state (hcd->self.root_hub,
  2112. USB_STATE_NOTATTACHED);
  2113. usb_kick_hub_wq(hcd->self.root_hub);
  2114. }
  2115. if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
  2116. hcd = hcd->shared_hcd;
  2117. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2118. set_bit(HCD_FLAG_DEAD, &hcd->flags);
  2119. if (hcd->rh_registered) {
  2120. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2121. /* make hub_wq clean up old urbs and devices */
  2122. usb_set_device_state(hcd->self.root_hub,
  2123. USB_STATE_NOTATTACHED);
  2124. usb_kick_hub_wq(hcd->self.root_hub);
  2125. }
  2126. }
  2127. /* Handle the case where this function gets called with a shared HCD */
  2128. if (usb_hcd_is_primary_hcd(hcd))
  2129. schedule_work(&hcd->died_work);
  2130. else
  2131. schedule_work(&hcd->primary_hcd->died_work);
  2132. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2133. /* Make sure that the other roothub is also deallocated. */
  2134. }
  2135. EXPORT_SYMBOL_GPL (usb_hc_died);
  2136. /*-------------------------------------------------------------------------*/
  2137. static void init_giveback_urb_bh(struct giveback_urb_bh *bh)
  2138. {
  2139. spin_lock_init(&bh->lock);
  2140. INIT_LIST_HEAD(&bh->head);
  2141. tasklet_setup(&bh->bh, usb_giveback_urb_bh);
  2142. }
  2143. struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver,
  2144. struct device *sysdev, struct device *dev, const char *bus_name,
  2145. struct usb_hcd *primary_hcd)
  2146. {
  2147. struct usb_hcd *hcd;
  2148. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  2149. if (!hcd)
  2150. return NULL;
  2151. if (primary_hcd == NULL) {
  2152. hcd->address0_mutex = kmalloc(sizeof(*hcd->address0_mutex),
  2153. GFP_KERNEL);
  2154. if (!hcd->address0_mutex) {
  2155. kfree(hcd);
  2156. dev_dbg(dev, "hcd address0 mutex alloc failed\n");
  2157. return NULL;
  2158. }
  2159. mutex_init(hcd->address0_mutex);
  2160. hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
  2161. GFP_KERNEL);
  2162. if (!hcd->bandwidth_mutex) {
  2163. kfree(hcd->address0_mutex);
  2164. kfree(hcd);
  2165. dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
  2166. return NULL;
  2167. }
  2168. mutex_init(hcd->bandwidth_mutex);
  2169. dev_set_drvdata(dev, hcd);
  2170. } else {
  2171. mutex_lock(&usb_port_peer_mutex);
  2172. hcd->address0_mutex = primary_hcd->address0_mutex;
  2173. hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
  2174. hcd->primary_hcd = primary_hcd;
  2175. primary_hcd->primary_hcd = primary_hcd;
  2176. hcd->shared_hcd = primary_hcd;
  2177. primary_hcd->shared_hcd = hcd;
  2178. mutex_unlock(&usb_port_peer_mutex);
  2179. }
  2180. kref_init(&hcd->kref);
  2181. usb_bus_init(&hcd->self);
  2182. hcd->self.controller = dev;
  2183. hcd->self.sysdev = sysdev;
  2184. hcd->self.bus_name = bus_name;
  2185. timer_setup(&hcd->rh_timer, rh_timer_func, 0);
  2186. #ifdef CONFIG_PM
  2187. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  2188. #endif
  2189. INIT_WORK(&hcd->died_work, hcd_died_work);
  2190. hcd->driver = driver;
  2191. hcd->speed = driver->flags & HCD_MASK;
  2192. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  2193. "USB Host Controller";
  2194. return hcd;
  2195. }
  2196. EXPORT_SYMBOL_GPL(__usb_create_hcd);
  2197. /**
  2198. * usb_create_shared_hcd - create and initialize an HCD structure
  2199. * @driver: HC driver that will use this hcd
  2200. * @dev: device for this HC, stored in hcd->self.controller
  2201. * @bus_name: value to store in hcd->self.bus_name
  2202. * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
  2203. * PCI device. Only allocate certain resources for the primary HCD
  2204. * Context: !in_interrupt()
  2205. *
  2206. * Allocate a struct usb_hcd, with extra space at the end for the
  2207. * HC driver's private data. Initialize the generic members of the
  2208. * hcd structure.
  2209. *
  2210. * Return: On success, a pointer to the created and initialized HCD structure.
  2211. * On failure (e.g. if memory is unavailable), %NULL.
  2212. */
  2213. struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
  2214. struct device *dev, const char *bus_name,
  2215. struct usb_hcd *primary_hcd)
  2216. {
  2217. return __usb_create_hcd(driver, dev, dev, bus_name, primary_hcd);
  2218. }
  2219. EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
  2220. /**
  2221. * usb_create_hcd - create and initialize an HCD structure
  2222. * @driver: HC driver that will use this hcd
  2223. * @dev: device for this HC, stored in hcd->self.controller
  2224. * @bus_name: value to store in hcd->self.bus_name
  2225. * Context: !in_interrupt()
  2226. *
  2227. * Allocate a struct usb_hcd, with extra space at the end for the
  2228. * HC driver's private data. Initialize the generic members of the
  2229. * hcd structure.
  2230. *
  2231. * Return: On success, a pointer to the created and initialized HCD
  2232. * structure. On failure (e.g. if memory is unavailable), %NULL.
  2233. */
  2234. struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
  2235. struct device *dev, const char *bus_name)
  2236. {
  2237. return __usb_create_hcd(driver, dev, dev, bus_name, NULL);
  2238. }
  2239. EXPORT_SYMBOL_GPL(usb_create_hcd);
  2240. /*
  2241. * Roothubs that share one PCI device must also share the bandwidth mutex.
  2242. * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is
  2243. * deallocated.
  2244. *
  2245. * Make sure to deallocate the bandwidth_mutex only when the last HCD is
  2246. * freed. When hcd_release() is called for either hcd in a peer set,
  2247. * invalidate the peer's ->shared_hcd and ->primary_hcd pointers.
  2248. */
  2249. static void hcd_release(struct kref *kref)
  2250. {
  2251. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  2252. mutex_lock(&usb_port_peer_mutex);
  2253. if (hcd->shared_hcd) {
  2254. struct usb_hcd *peer = hcd->shared_hcd;
  2255. peer->shared_hcd = NULL;
  2256. peer->primary_hcd = NULL;
  2257. } else {
  2258. kfree(hcd->address0_mutex);
  2259. kfree(hcd->bandwidth_mutex);
  2260. }
  2261. mutex_unlock(&usb_port_peer_mutex);
  2262. kfree(hcd);
  2263. }
  2264. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  2265. {
  2266. if (hcd)
  2267. kref_get (&hcd->kref);
  2268. return hcd;
  2269. }
  2270. EXPORT_SYMBOL_GPL(usb_get_hcd);
  2271. void usb_put_hcd (struct usb_hcd *hcd)
  2272. {
  2273. if (hcd)
  2274. kref_put (&hcd->kref, hcd_release);
  2275. }
  2276. EXPORT_SYMBOL_GPL(usb_put_hcd);
  2277. int usb_hcd_is_primary_hcd(struct usb_hcd *hcd)
  2278. {
  2279. if (!hcd->primary_hcd)
  2280. return 1;
  2281. return hcd == hcd->primary_hcd;
  2282. }
  2283. EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd);
  2284. int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1)
  2285. {
  2286. if (!hcd->driver->find_raw_port_number)
  2287. return port1;
  2288. return hcd->driver->find_raw_port_number(hcd, port1);
  2289. }
  2290. static int usb_hcd_request_irqs(struct usb_hcd *hcd,
  2291. unsigned int irqnum, unsigned long irqflags)
  2292. {
  2293. int retval;
  2294. if (hcd->driver->irq) {
  2295. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  2296. hcd->driver->description, hcd->self.busnum);
  2297. retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  2298. hcd->irq_descr, hcd);
  2299. if (retval != 0) {
  2300. dev_err(hcd->self.controller,
  2301. "request interrupt %d failed\n",
  2302. irqnum);
  2303. return retval;
  2304. }
  2305. hcd->irq = irqnum;
  2306. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  2307. (hcd->driver->flags & HCD_MEMORY) ?
  2308. "io mem" : "io base",
  2309. (unsigned long long)hcd->rsrc_start);
  2310. } else {
  2311. hcd->irq = 0;
  2312. if (hcd->rsrc_start)
  2313. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  2314. (hcd->driver->flags & HCD_MEMORY) ?
  2315. "io mem" : "io base",
  2316. (unsigned long long)hcd->rsrc_start);
  2317. }
  2318. return 0;
  2319. }
  2320. /*
  2321. * Before we free this root hub, flush in-flight peering attempts
  2322. * and disable peer lookups
  2323. */
  2324. static void usb_put_invalidate_rhdev(struct usb_hcd *hcd)
  2325. {
  2326. struct usb_device *rhdev;
  2327. mutex_lock(&usb_port_peer_mutex);
  2328. rhdev = hcd->self.root_hub;
  2329. hcd->self.root_hub = NULL;
  2330. mutex_unlock(&usb_port_peer_mutex);
  2331. usb_put_dev(rhdev);
  2332. }
  2333. /**
  2334. * usb_add_hcd - finish generic HCD structure initialization and register
  2335. * @hcd: the usb_hcd structure to initialize
  2336. * @irqnum: Interrupt line to allocate
  2337. * @irqflags: Interrupt type flags
  2338. *
  2339. * Finish the remaining parts of generic HCD initialization: allocate the
  2340. * buffers of consistent memory, register the bus, request the IRQ line,
  2341. * and call the driver's reset() and start() routines.
  2342. */
  2343. int usb_add_hcd(struct usb_hcd *hcd,
  2344. unsigned int irqnum, unsigned long irqflags)
  2345. {
  2346. int retval;
  2347. struct usb_device *rhdev;
  2348. if (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) {
  2349. hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
  2350. if (IS_ERR(hcd->phy_roothub))
  2351. return PTR_ERR(hcd->phy_roothub);
  2352. retval = usb_phy_roothub_init(hcd->phy_roothub);
  2353. if (retval)
  2354. return retval;
  2355. retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
  2356. PHY_MODE_USB_HOST_SS);
  2357. if (retval)
  2358. retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
  2359. PHY_MODE_USB_HOST);
  2360. if (retval)
  2361. goto err_usb_phy_roothub_power_on;
  2362. retval = usb_phy_roothub_power_on(hcd->phy_roothub);
  2363. if (retval)
  2364. goto err_usb_phy_roothub_power_on;
  2365. }
  2366. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  2367. switch (authorized_default) {
  2368. case USB_AUTHORIZE_NONE:
  2369. hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE;
  2370. break;
  2371. case USB_AUTHORIZE_ALL:
  2372. hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL;
  2373. break;
  2374. case USB_AUTHORIZE_INTERNAL:
  2375. hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL;
  2376. break;
  2377. case USB_AUTHORIZE_WIRED:
  2378. default:
  2379. hcd->dev_policy = hcd->wireless ?
  2380. USB_DEVICE_AUTHORIZE_NONE : USB_DEVICE_AUTHORIZE_ALL;
  2381. break;
  2382. }
  2383. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2384. /* per default all interfaces are authorized */
  2385. set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  2386. /* HC is in reset state, but accessible. Now do the one-time init,
  2387. * bottom up so that hcds can customize the root hubs before hub_wq
  2388. * starts talking to them. (Note, bus id is assigned early too.)
  2389. */
  2390. retval = hcd_buffer_create(hcd);
  2391. if (retval != 0) {
  2392. dev_dbg(hcd->self.sysdev, "pool alloc failed\n");
  2393. goto err_create_buf;
  2394. }
  2395. retval = usb_register_bus(&hcd->self);
  2396. if (retval < 0)
  2397. goto err_register_bus;
  2398. rhdev = usb_alloc_dev(NULL, &hcd->self, 0);
  2399. if (rhdev == NULL) {
  2400. dev_err(hcd->self.sysdev, "unable to allocate root hub\n");
  2401. retval = -ENOMEM;
  2402. goto err_allocate_root_hub;
  2403. }
  2404. mutex_lock(&usb_port_peer_mutex);
  2405. hcd->self.root_hub = rhdev;
  2406. mutex_unlock(&usb_port_peer_mutex);
  2407. rhdev->rx_lanes = 1;
  2408. rhdev->tx_lanes = 1;
  2409. switch (hcd->speed) {
  2410. case HCD_USB11:
  2411. rhdev->speed = USB_SPEED_FULL;
  2412. break;
  2413. case HCD_USB2:
  2414. rhdev->speed = USB_SPEED_HIGH;
  2415. break;
  2416. case HCD_USB25:
  2417. rhdev->speed = USB_SPEED_WIRELESS;
  2418. break;
  2419. case HCD_USB3:
  2420. rhdev->speed = USB_SPEED_SUPER;
  2421. break;
  2422. case HCD_USB32:
  2423. rhdev->rx_lanes = 2;
  2424. rhdev->tx_lanes = 2;
  2425. fallthrough;
  2426. case HCD_USB31:
  2427. rhdev->speed = USB_SPEED_SUPER_PLUS;
  2428. break;
  2429. default:
  2430. retval = -EINVAL;
  2431. goto err_set_rh_speed;
  2432. }
  2433. /* wakeup flag init defaults to "everything works" for root hubs,
  2434. * but drivers can override it in reset() if needed, along with
  2435. * recording the overall controller's system wakeup capability.
  2436. */
  2437. device_set_wakeup_capable(&rhdev->dev, 1);
  2438. /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
  2439. * registered. But since the controller can die at any time,
  2440. * let's initialize the flag before touching the hardware.
  2441. */
  2442. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2443. /* "reset" is misnamed; its role is now one-time init. the controller
  2444. * should already have been reset (and boot firmware kicked off etc).
  2445. */
  2446. if (hcd->driver->reset) {
  2447. retval = hcd->driver->reset(hcd);
  2448. if (retval < 0) {
  2449. dev_err(hcd->self.controller, "can't setup: %d\n",
  2450. retval);
  2451. goto err_hcd_driver_setup;
  2452. }
  2453. }
  2454. hcd->rh_pollable = 1;
  2455. retval = usb_phy_roothub_calibrate(hcd->phy_roothub);
  2456. if (retval)
  2457. goto err_hcd_driver_setup;
  2458. /* NOTE: root hub and controller capabilities may not be the same */
  2459. if (device_can_wakeup(hcd->self.controller)
  2460. && device_can_wakeup(&hcd->self.root_hub->dev))
  2461. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  2462. /* initialize tasklets */
  2463. init_giveback_urb_bh(&hcd->high_prio_bh);
  2464. init_giveback_urb_bh(&hcd->low_prio_bh);
  2465. /* enable irqs just before we start the controller,
  2466. * if the BIOS provides legacy PCI irqs.
  2467. */
  2468. if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
  2469. retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
  2470. if (retval)
  2471. goto err_request_irq;
  2472. }
  2473. hcd->state = HC_STATE_RUNNING;
  2474. retval = hcd->driver->start(hcd);
  2475. if (retval < 0) {
  2476. dev_err(hcd->self.controller, "startup error %d\n", retval);
  2477. goto err_hcd_driver_start;
  2478. }
  2479. /* starting here, usbcore will pay attention to this root hub */
  2480. retval = register_root_hub(hcd);
  2481. if (retval != 0)
  2482. goto err_register_root_hub;
  2483. if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
  2484. usb_hcd_poll_rh_status(hcd);
  2485. return retval;
  2486. err_register_root_hub:
  2487. hcd->rh_pollable = 0;
  2488. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2489. del_timer_sync(&hcd->rh_timer);
  2490. hcd->driver->stop(hcd);
  2491. hcd->state = HC_STATE_HALT;
  2492. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2493. del_timer_sync(&hcd->rh_timer);
  2494. err_hcd_driver_start:
  2495. if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
  2496. free_irq(irqnum, hcd);
  2497. err_request_irq:
  2498. err_hcd_driver_setup:
  2499. err_set_rh_speed:
  2500. usb_put_invalidate_rhdev(hcd);
  2501. err_allocate_root_hub:
  2502. usb_deregister_bus(&hcd->self);
  2503. err_register_bus:
  2504. hcd_buffer_destroy(hcd);
  2505. err_create_buf:
  2506. usb_phy_roothub_power_off(hcd->phy_roothub);
  2507. err_usb_phy_roothub_power_on:
  2508. usb_phy_roothub_exit(hcd->phy_roothub);
  2509. return retval;
  2510. }
  2511. EXPORT_SYMBOL_GPL(usb_add_hcd);
  2512. /**
  2513. * usb_remove_hcd - shutdown processing for generic HCDs
  2514. * @hcd: the usb_hcd structure to remove
  2515. * Context: !in_interrupt()
  2516. *
  2517. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  2518. * invoking the HCD's stop() method.
  2519. */
  2520. void usb_remove_hcd(struct usb_hcd *hcd)
  2521. {
  2522. struct usb_device *rhdev = hcd->self.root_hub;
  2523. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  2524. usb_get_dev(rhdev);
  2525. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2526. if (HC_IS_RUNNING (hcd->state))
  2527. hcd->state = HC_STATE_QUIESCING;
  2528. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  2529. spin_lock_irq (&hcd_root_hub_lock);
  2530. hcd->rh_registered = 0;
  2531. spin_unlock_irq (&hcd_root_hub_lock);
  2532. #ifdef CONFIG_PM
  2533. cancel_work_sync(&hcd->wakeup_work);
  2534. #endif
  2535. cancel_work_sync(&hcd->died_work);
  2536. mutex_lock(&usb_bus_idr_lock);
  2537. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2538. mutex_unlock(&usb_bus_idr_lock);
  2539. /*
  2540. * tasklet_kill() isn't needed here because:
  2541. * - driver's disconnect() called from usb_disconnect() should
  2542. * make sure its URBs are completed during the disconnect()
  2543. * callback
  2544. *
  2545. * - it is too late to run complete() here since driver may have
  2546. * been removed already now
  2547. */
  2548. /* Prevent any more root-hub status calls from the timer.
  2549. * The HCD might still restart the timer (if a port status change
  2550. * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
  2551. * the hub_status_data() callback.
  2552. */
  2553. hcd->rh_pollable = 0;
  2554. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2555. del_timer_sync(&hcd->rh_timer);
  2556. hcd->driver->stop(hcd);
  2557. hcd->state = HC_STATE_HALT;
  2558. /* In case the HCD restarted the timer, stop it again. */
  2559. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2560. del_timer_sync(&hcd->rh_timer);
  2561. if (usb_hcd_is_primary_hcd(hcd)) {
  2562. if (hcd->irq > 0)
  2563. free_irq(hcd->irq, hcd);
  2564. }
  2565. usb_deregister_bus(&hcd->self);
  2566. hcd_buffer_destroy(hcd);
  2567. usb_phy_roothub_power_off(hcd->phy_roothub);
  2568. usb_phy_roothub_exit(hcd->phy_roothub);
  2569. usb_put_invalidate_rhdev(hcd);
  2570. hcd->flags = 0;
  2571. }
  2572. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  2573. void
  2574. usb_hcd_platform_shutdown(struct platform_device *dev)
  2575. {
  2576. struct usb_hcd *hcd = platform_get_drvdata(dev);
  2577. /* No need for pm_runtime_put(), we're shutting down */
  2578. pm_runtime_get_sync(&dev->dev);
  2579. if (hcd->driver->shutdown)
  2580. hcd->driver->shutdown(hcd);
  2581. }
  2582. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  2583. int usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr,
  2584. dma_addr_t dma, size_t size)
  2585. {
  2586. int err;
  2587. void *local_mem;
  2588. hcd->localmem_pool = devm_gen_pool_create(hcd->self.sysdev, 4,
  2589. dev_to_node(hcd->self.sysdev),
  2590. dev_name(hcd->self.sysdev));
  2591. if (IS_ERR(hcd->localmem_pool))
  2592. return PTR_ERR(hcd->localmem_pool);
  2593. local_mem = devm_memremap(hcd->self.sysdev, phys_addr,
  2594. size, MEMREMAP_WC);
  2595. if (IS_ERR(local_mem))
  2596. return PTR_ERR(local_mem);
  2597. /*
  2598. * Here we pass a dma_addr_t but the arg type is a phys_addr_t.
  2599. * It's not backed by system memory and thus there's no kernel mapping
  2600. * for it.
  2601. */
  2602. err = gen_pool_add_virt(hcd->localmem_pool, (unsigned long)local_mem,
  2603. dma, size, dev_to_node(hcd->self.sysdev));
  2604. if (err < 0) {
  2605. dev_err(hcd->self.sysdev, "gen_pool_add_virt failed with %d\n",
  2606. err);
  2607. return err;
  2608. }
  2609. return 0;
  2610. }
  2611. EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem);
  2612. /*-------------------------------------------------------------------------*/
  2613. #if IS_ENABLED(CONFIG_USB_MON)
  2614. const struct usb_mon_operations *mon_ops;
  2615. /*
  2616. * The registration is unlocked.
  2617. * We do it this way because we do not want to lock in hot paths.
  2618. *
  2619. * Notice that the code is minimally error-proof. Because usbmon needs
  2620. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  2621. */
  2622. int usb_mon_register(const struct usb_mon_operations *ops)
  2623. {
  2624. if (mon_ops)
  2625. return -EBUSY;
  2626. mon_ops = ops;
  2627. mb();
  2628. return 0;
  2629. }
  2630. EXPORT_SYMBOL_GPL (usb_mon_register);
  2631. void usb_mon_deregister (void)
  2632. {
  2633. if (mon_ops == NULL) {
  2634. printk(KERN_ERR "USB: monitor was not registered\n");
  2635. return;
  2636. }
  2637. mon_ops = NULL;
  2638. mb();
  2639. }
  2640. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  2641. #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */