hcd.c 87 KB

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