hcd.c 87 KB

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