hcd.c 87 KB

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