hcd.c 87 KB

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