hcd.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072
  1. /*
  2. * (C) Copyright Linus Torvalds 1999
  3. * (C) Copyright Johannes Erdfelt 1999-2001
  4. * (C) Copyright Andreas Gal 1999
  5. * (C) Copyright Gregory P. Smith 1999
  6. * (C) Copyright Deti Fliegl 1999
  7. * (C) Copyright Randy Dunlap 2000
  8. * (C) Copyright David Brownell 2000-2002
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. * for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/bcd.h>
  25. #include <linux/module.h>
  26. #include <linux/version.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/completion.h>
  30. #include <linux/utsname.h>
  31. #include <linux/mm.h>
  32. #include <asm/io.h>
  33. #include <linux/device.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/mutex.h>
  36. #include <asm/irq.h>
  37. #include <asm/byteorder.h>
  38. #include <asm/unaligned.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/pm_runtime.h>
  42. #include <linux/types.h>
  43. #include <linux/phy/phy.h>
  44. #include <linux/usb.h>
  45. #include <linux/usb/hcd.h>
  46. #include <linux/usb/phy.h>
  47. #include "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->devnum_next_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. usb_dev->dev.of_node = parent_dev->of_node;
  976. }
  977. mutex_unlock(&usb_bus_idr_lock);
  978. return retval;
  979. }
  980. /*
  981. * usb_hcd_start_port_resume - a root-hub port is sending a resume signal
  982. * @bus: the bus which the root hub belongs to
  983. * @portnum: the port which is being resumed
  984. *
  985. * HCDs should call this function when they know that a resume signal is
  986. * being sent to a root-hub port. The root hub will be prevented from
  987. * going into autosuspend until usb_hcd_end_port_resume() is called.
  988. *
  989. * The bus's private lock must be held by the caller.
  990. */
  991. void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
  992. {
  993. unsigned bit = 1 << portnum;
  994. if (!(bus->resuming_ports & bit)) {
  995. bus->resuming_ports |= bit;
  996. pm_runtime_get_noresume(&bus->root_hub->dev);
  997. }
  998. }
  999. EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
  1000. /*
  1001. * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal
  1002. * @bus: the bus which the root hub belongs to
  1003. * @portnum: the port which is being resumed
  1004. *
  1005. * HCDs should call this function when they know that a resume signal has
  1006. * stopped being sent to a root-hub port. The root hub will be allowed to
  1007. * autosuspend again.
  1008. *
  1009. * The bus's private lock must be held by the caller.
  1010. */
  1011. void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
  1012. {
  1013. unsigned bit = 1 << portnum;
  1014. if (bus->resuming_ports & bit) {
  1015. bus->resuming_ports &= ~bit;
  1016. pm_runtime_put_noidle(&bus->root_hub->dev);
  1017. }
  1018. }
  1019. EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
  1020. /*-------------------------------------------------------------------------*/
  1021. /**
  1022. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  1023. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  1024. * @is_input: true iff the transaction sends data to the host
  1025. * @isoc: true for isochronous transactions, false for interrupt ones
  1026. * @bytecount: how many bytes in the transaction.
  1027. *
  1028. * Return: Approximate bus time in nanoseconds for a periodic transaction.
  1029. *
  1030. * Note:
  1031. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  1032. * scheduled in software, this function is only used for such scheduling.
  1033. */
  1034. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  1035. {
  1036. unsigned long tmp;
  1037. switch (speed) {
  1038. case USB_SPEED_LOW: /* INTR only */
  1039. if (is_input) {
  1040. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1041. return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  1042. } else {
  1043. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1044. return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  1045. }
  1046. case USB_SPEED_FULL: /* ISOC or INTR */
  1047. if (isoc) {
  1048. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1049. return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
  1050. } else {
  1051. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1052. return 9107L + BW_HOST_DELAY + tmp;
  1053. }
  1054. case USB_SPEED_HIGH: /* ISOC or INTR */
  1055. /* FIXME adjust for input vs output */
  1056. if (isoc)
  1057. tmp = HS_NSECS_ISO (bytecount);
  1058. else
  1059. tmp = HS_NSECS (bytecount);
  1060. return tmp;
  1061. default:
  1062. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  1063. return -1;
  1064. }
  1065. }
  1066. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  1067. /*-------------------------------------------------------------------------*/
  1068. /*
  1069. * Generic HC operations.
  1070. */
  1071. /*-------------------------------------------------------------------------*/
  1072. /**
  1073. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  1074. * @hcd: host controller to which @urb was submitted
  1075. * @urb: URB being submitted
  1076. *
  1077. * Host controller drivers should call this routine in their enqueue()
  1078. * method. The HCD's private spinlock must be held and interrupts must
  1079. * be disabled. The actions carried out here are required for URB
  1080. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  1081. *
  1082. * Return: 0 for no error, otherwise a negative error code (in which case
  1083. * the enqueue() method must fail). If no error occurs but enqueue() fails
  1084. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  1085. * the private spinlock and returning.
  1086. */
  1087. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  1088. {
  1089. int rc = 0;
  1090. spin_lock(&hcd_urb_list_lock);
  1091. /* Check that the URB isn't being killed */
  1092. if (unlikely(atomic_read(&urb->reject))) {
  1093. rc = -EPERM;
  1094. goto done;
  1095. }
  1096. if (unlikely(!urb->ep->enabled)) {
  1097. rc = -ENOENT;
  1098. goto done;
  1099. }
  1100. if (unlikely(!urb->dev->can_submit)) {
  1101. rc = -EHOSTUNREACH;
  1102. goto done;
  1103. }
  1104. /*
  1105. * Check the host controller's state and add the URB to the
  1106. * endpoint's queue.
  1107. */
  1108. if (HCD_RH_RUNNING(hcd)) {
  1109. urb->unlinked = 0;
  1110. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  1111. } else {
  1112. rc = -ESHUTDOWN;
  1113. goto done;
  1114. }
  1115. done:
  1116. spin_unlock(&hcd_urb_list_lock);
  1117. return rc;
  1118. }
  1119. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  1120. /**
  1121. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  1122. * @hcd: host controller to which @urb was submitted
  1123. * @urb: URB being checked for unlinkability
  1124. * @status: error code to store in @urb if the unlink succeeds
  1125. *
  1126. * Host controller drivers should call this routine in their dequeue()
  1127. * method. The HCD's private spinlock must be held and interrupts must
  1128. * be disabled. The actions carried out here are required for making
  1129. * sure than an unlink is valid.
  1130. *
  1131. * Return: 0 for no error, otherwise a negative error code (in which case
  1132. * the dequeue() method must fail). The possible error codes are:
  1133. *
  1134. * -EIDRM: @urb was not submitted or has already completed.
  1135. * The completion function may not have been called yet.
  1136. *
  1137. * -EBUSY: @urb has already been unlinked.
  1138. */
  1139. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  1140. int status)
  1141. {
  1142. struct list_head *tmp;
  1143. /* insist the urb is still queued */
  1144. list_for_each(tmp, &urb->ep->urb_list) {
  1145. if (tmp == &urb->urb_list)
  1146. break;
  1147. }
  1148. if (tmp != &urb->urb_list)
  1149. return -EIDRM;
  1150. /* Any status except -EINPROGRESS means something already started to
  1151. * unlink this URB from the hardware. So there's no more work to do.
  1152. */
  1153. if (urb->unlinked)
  1154. return -EBUSY;
  1155. urb->unlinked = status;
  1156. return 0;
  1157. }
  1158. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  1159. /**
  1160. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  1161. * @hcd: host controller to which @urb was submitted
  1162. * @urb: URB being unlinked
  1163. *
  1164. * Host controller drivers should call this routine before calling
  1165. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  1166. * interrupts must be disabled. The actions carried out here are required
  1167. * for URB completion.
  1168. */
  1169. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  1170. {
  1171. /* clear all state linking urb to this dev (and hcd) */
  1172. spin_lock(&hcd_urb_list_lock);
  1173. list_del_init(&urb->urb_list);
  1174. spin_unlock(&hcd_urb_list_lock);
  1175. }
  1176. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  1177. /*
  1178. * Some usb host controllers can only perform dma using a small SRAM area.
  1179. * The usb core itself is however optimized for host controllers that can dma
  1180. * using regular system memory - like pci devices doing bus mastering.
  1181. *
  1182. * To support host controllers with limited dma capabilities we provide dma
  1183. * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
  1184. * For this to work properly the host controller code must first use the
  1185. * function dma_declare_coherent_memory() to point out which memory area
  1186. * that should be used for dma allocations.
  1187. *
  1188. * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
  1189. * dma using dma_alloc_coherent() which in turn allocates from the memory
  1190. * area pointed out with dma_declare_coherent_memory().
  1191. *
  1192. * So, to summarize...
  1193. *
  1194. * - We need "local" memory, canonical example being
  1195. * a small SRAM on a discrete controller being the
  1196. * only memory that the controller can read ...
  1197. * (a) "normal" kernel memory is no good, and
  1198. * (b) there's not enough to share
  1199. *
  1200. * - The only *portable* hook for such stuff in the
  1201. * DMA framework is dma_declare_coherent_memory()
  1202. *
  1203. * - So we use that, even though the primary requirement
  1204. * is that the memory be "local" (hence addressable
  1205. * by that device), not "coherent".
  1206. *
  1207. */
  1208. static int hcd_alloc_coherent(struct usb_bus *bus,
  1209. gfp_t mem_flags, dma_addr_t *dma_handle,
  1210. void **vaddr_handle, size_t size,
  1211. enum dma_data_direction dir)
  1212. {
  1213. unsigned char *vaddr;
  1214. if (*vaddr_handle == NULL) {
  1215. WARN_ON_ONCE(1);
  1216. return -EFAULT;
  1217. }
  1218. vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
  1219. mem_flags, dma_handle);
  1220. if (!vaddr)
  1221. return -ENOMEM;
  1222. /*
  1223. * Store the virtual address of the buffer at the end
  1224. * of the allocated dma buffer. The size of the buffer
  1225. * may be uneven so use unaligned functions instead
  1226. * of just rounding up. It makes sense to optimize for
  1227. * memory footprint over access speed since the amount
  1228. * of memory available for dma may be limited.
  1229. */
  1230. put_unaligned((unsigned long)*vaddr_handle,
  1231. (unsigned long *)(vaddr + size));
  1232. if (dir == DMA_TO_DEVICE)
  1233. memcpy(vaddr, *vaddr_handle, size);
  1234. *vaddr_handle = vaddr;
  1235. return 0;
  1236. }
  1237. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1238. void **vaddr_handle, size_t size,
  1239. enum dma_data_direction dir)
  1240. {
  1241. unsigned char *vaddr = *vaddr_handle;
  1242. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1243. if (dir == DMA_FROM_DEVICE)
  1244. memcpy(vaddr, *vaddr_handle, size);
  1245. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1246. *vaddr_handle = vaddr;
  1247. *dma_handle = 0;
  1248. }
  1249. void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1250. {
  1251. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1252. (urb->transfer_flags & URB_SETUP_MAP_SINGLE))
  1253. dma_unmap_single(hcd->self.controller,
  1254. urb->setup_dma,
  1255. sizeof(struct usb_ctrlrequest),
  1256. DMA_TO_DEVICE);
  1257. else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
  1258. hcd_free_coherent(urb->dev->bus,
  1259. &urb->setup_dma,
  1260. (void **) &urb->setup_packet,
  1261. sizeof(struct usb_ctrlrequest),
  1262. DMA_TO_DEVICE);
  1263. /* Make it safe to call this routine more than once */
  1264. urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
  1265. }
  1266. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
  1267. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1268. {
  1269. if (hcd->driver->unmap_urb_for_dma)
  1270. hcd->driver->unmap_urb_for_dma(hcd, urb);
  1271. else
  1272. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1273. }
  1274. void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1275. {
  1276. enum dma_data_direction dir;
  1277. usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
  1278. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1279. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1280. (urb->transfer_flags & URB_DMA_MAP_SG))
  1281. dma_unmap_sg(hcd->self.controller,
  1282. urb->sg,
  1283. urb->num_sgs,
  1284. dir);
  1285. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1286. (urb->transfer_flags & URB_DMA_MAP_PAGE))
  1287. dma_unmap_page(hcd->self.controller,
  1288. urb->transfer_dma,
  1289. urb->transfer_buffer_length,
  1290. dir);
  1291. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1292. (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  1293. dma_unmap_single(hcd->self.controller,
  1294. urb->transfer_dma,
  1295. urb->transfer_buffer_length,
  1296. dir);
  1297. else if (urb->transfer_flags & URB_MAP_LOCAL)
  1298. hcd_free_coherent(urb->dev->bus,
  1299. &urb->transfer_dma,
  1300. &urb->transfer_buffer,
  1301. urb->transfer_buffer_length,
  1302. dir);
  1303. /* Make it safe to call this routine more than once */
  1304. urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
  1305. URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
  1306. }
  1307. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
  1308. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1309. gfp_t mem_flags)
  1310. {
  1311. if (hcd->driver->map_urb_for_dma)
  1312. return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
  1313. else
  1314. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  1315. }
  1316. int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1317. gfp_t mem_flags)
  1318. {
  1319. enum dma_data_direction dir;
  1320. int ret = 0;
  1321. /* Map the URB's buffers for DMA access.
  1322. * Lower level HCD code should use *_dma exclusively,
  1323. * unless it uses pio or talks to another transport,
  1324. * or uses the provided scatter gather list for bulk.
  1325. */
  1326. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  1327. if (hcd->self.uses_pio_for_control)
  1328. return ret;
  1329. if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {
  1330. urb->setup_dma = dma_map_single(
  1331. hcd->self.controller,
  1332. urb->setup_packet,
  1333. sizeof(struct usb_ctrlrequest),
  1334. DMA_TO_DEVICE);
  1335. if (dma_mapping_error(hcd->self.controller,
  1336. urb->setup_dma))
  1337. return -EAGAIN;
  1338. urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
  1339. } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1340. ret = hcd_alloc_coherent(
  1341. urb->dev->bus, mem_flags,
  1342. &urb->setup_dma,
  1343. (void **)&urb->setup_packet,
  1344. sizeof(struct usb_ctrlrequest),
  1345. DMA_TO_DEVICE);
  1346. if (ret)
  1347. return ret;
  1348. urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
  1349. }
  1350. }
  1351. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1352. if (urb->transfer_buffer_length != 0
  1353. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1354. if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {
  1355. if (urb->num_sgs) {
  1356. int n;
  1357. /* We don't support sg for isoc transfers ! */
  1358. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1359. WARN_ON(1);
  1360. return -EINVAL;
  1361. }
  1362. n = dma_map_sg(
  1363. hcd->self.controller,
  1364. urb->sg,
  1365. urb->num_sgs,
  1366. dir);
  1367. if (n <= 0)
  1368. ret = -EAGAIN;
  1369. else
  1370. urb->transfer_flags |= URB_DMA_MAP_SG;
  1371. urb->num_mapped_sgs = n;
  1372. if (n != urb->num_sgs)
  1373. urb->transfer_flags |=
  1374. URB_DMA_SG_COMBINED;
  1375. } else if (urb->sg) {
  1376. struct scatterlist *sg = urb->sg;
  1377. urb->transfer_dma = dma_map_page(
  1378. hcd->self.controller,
  1379. sg_page(sg),
  1380. sg->offset,
  1381. urb->transfer_buffer_length,
  1382. dir);
  1383. if (dma_mapping_error(hcd->self.controller,
  1384. urb->transfer_dma))
  1385. ret = -EAGAIN;
  1386. else
  1387. urb->transfer_flags |= URB_DMA_MAP_PAGE;
  1388. } else if (is_vmalloc_addr(urb->transfer_buffer)) {
  1389. WARN_ONCE(1, "transfer buffer not dma capable\n");
  1390. ret = -EAGAIN;
  1391. } else {
  1392. urb->transfer_dma = dma_map_single(
  1393. hcd->self.controller,
  1394. urb->transfer_buffer,
  1395. urb->transfer_buffer_length,
  1396. dir);
  1397. if (dma_mapping_error(hcd->self.controller,
  1398. urb->transfer_dma))
  1399. ret = -EAGAIN;
  1400. else
  1401. urb->transfer_flags |= URB_DMA_MAP_SINGLE;
  1402. }
  1403. } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1404. ret = hcd_alloc_coherent(
  1405. urb->dev->bus, mem_flags,
  1406. &urb->transfer_dma,
  1407. &urb->transfer_buffer,
  1408. urb->transfer_buffer_length,
  1409. dir);
  1410. if (ret == 0)
  1411. urb->transfer_flags |= URB_MAP_LOCAL;
  1412. }
  1413. if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
  1414. URB_SETUP_MAP_LOCAL)))
  1415. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1416. }
  1417. return ret;
  1418. }
  1419. EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
  1420. /*-------------------------------------------------------------------------*/
  1421. /* may be called in any context with a valid urb->dev usecount
  1422. * caller surrenders "ownership" of urb
  1423. * expects usb_submit_urb() to have sanity checked and conditioned all
  1424. * inputs in the urb
  1425. */
  1426. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1427. {
  1428. int status;
  1429. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1430. /* increment urb's reference count as part of giving it to the HCD
  1431. * (which will control it). HCD guarantees that it either returns
  1432. * an error or calls giveback(), but not both.
  1433. */
  1434. usb_get_urb(urb);
  1435. atomic_inc(&urb->use_count);
  1436. atomic_inc(&urb->dev->urbnum);
  1437. usbmon_urb_submit(&hcd->self, urb);
  1438. /* NOTE requirements on root-hub callers (usbfs and the hub
  1439. * driver, for now): URBs' urb->transfer_buffer must be
  1440. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1441. * they could clobber root hub response data. Also, control
  1442. * URBs must be submitted in process context with interrupts
  1443. * enabled.
  1444. */
  1445. if (is_root_hub(urb->dev)) {
  1446. status = rh_urb_enqueue(hcd, urb);
  1447. } else {
  1448. status = map_urb_for_dma(hcd, urb, mem_flags);
  1449. if (likely(status == 0)) {
  1450. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1451. if (unlikely(status))
  1452. unmap_urb_for_dma(hcd, urb);
  1453. }
  1454. }
  1455. if (unlikely(status)) {
  1456. usbmon_urb_submit_error(&hcd->self, urb, status);
  1457. urb->hcpriv = NULL;
  1458. INIT_LIST_HEAD(&urb->urb_list);
  1459. atomic_dec(&urb->use_count);
  1460. atomic_dec(&urb->dev->urbnum);
  1461. if (atomic_read(&urb->reject))
  1462. wake_up(&usb_kill_urb_queue);
  1463. usb_put_urb(urb);
  1464. }
  1465. return status;
  1466. }
  1467. /*-------------------------------------------------------------------------*/
  1468. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1469. * off hardware queues (which may take a while) and returning it as
  1470. * soon as practical. we've already set up the urb's return status,
  1471. * but we can't know if the callback completed already.
  1472. */
  1473. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1474. {
  1475. int value;
  1476. if (is_root_hub(urb->dev))
  1477. value = usb_rh_urb_dequeue(hcd, urb, status);
  1478. else {
  1479. /* The only reason an HCD might fail this call is if
  1480. * it has not yet fully queued the urb to begin with.
  1481. * Such failures should be harmless. */
  1482. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1483. }
  1484. return value;
  1485. }
  1486. /*
  1487. * called in any context
  1488. *
  1489. * caller guarantees urb won't be recycled till both unlink()
  1490. * and the urb's completion function return
  1491. */
  1492. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1493. {
  1494. struct usb_hcd *hcd;
  1495. struct usb_device *udev = urb->dev;
  1496. int retval = -EIDRM;
  1497. unsigned long flags;
  1498. /* Prevent the device and bus from going away while
  1499. * the unlink is carried out. If they are already gone
  1500. * then urb->use_count must be 0, since disconnected
  1501. * devices can't have any active URBs.
  1502. */
  1503. spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
  1504. if (atomic_read(&urb->use_count) > 0) {
  1505. retval = 0;
  1506. usb_get_dev(udev);
  1507. }
  1508. spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
  1509. if (retval == 0) {
  1510. hcd = bus_to_hcd(urb->dev->bus);
  1511. retval = unlink1(hcd, urb, status);
  1512. if (retval == 0)
  1513. retval = -EINPROGRESS;
  1514. else if (retval != -EIDRM && retval != -EBUSY)
  1515. dev_dbg(&udev->dev, "hcd_unlink_urb %p fail %d\n",
  1516. urb, retval);
  1517. usb_put_dev(udev);
  1518. }
  1519. return retval;
  1520. }
  1521. /*-------------------------------------------------------------------------*/
  1522. static void __usb_hcd_giveback_urb(struct urb *urb)
  1523. {
  1524. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1525. struct usb_anchor *anchor = urb->anchor;
  1526. int status = urb->unlinked;
  1527. unsigned long flags;
  1528. urb->hcpriv = NULL;
  1529. if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1530. urb->actual_length < urb->transfer_buffer_length &&
  1531. !status))
  1532. status = -EREMOTEIO;
  1533. unmap_urb_for_dma(hcd, urb);
  1534. usbmon_urb_complete(&hcd->self, urb, status);
  1535. usb_anchor_suspend_wakeups(anchor);
  1536. usb_unanchor_urb(urb);
  1537. if (likely(status == 0))
  1538. usb_led_activity(USB_LED_EVENT_HOST);
  1539. /* pass ownership to the completion handler */
  1540. urb->status = status;
  1541. /*
  1542. * We disable local IRQs here avoid possible deadlock because
  1543. * drivers may call spin_lock() to hold lock which might be
  1544. * acquired in one hard interrupt handler.
  1545. *
  1546. * The local_irq_save()/local_irq_restore() around complete()
  1547. * will be removed if current USB drivers have been cleaned up
  1548. * and no one may trigger the above deadlock situation when
  1549. * running complete() in tasklet.
  1550. */
  1551. local_irq_save(flags);
  1552. urb->complete(urb);
  1553. local_irq_restore(flags);
  1554. usb_anchor_resume_wakeups(anchor);
  1555. atomic_dec(&urb->use_count);
  1556. if (unlikely(atomic_read(&urb->reject)))
  1557. wake_up(&usb_kill_urb_queue);
  1558. usb_put_urb(urb);
  1559. }
  1560. static void usb_giveback_urb_bh(unsigned long param)
  1561. {
  1562. struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param;
  1563. struct list_head local_list;
  1564. spin_lock_irq(&bh->lock);
  1565. bh->running = true;
  1566. restart:
  1567. list_replace_init(&bh->head, &local_list);
  1568. spin_unlock_irq(&bh->lock);
  1569. while (!list_empty(&local_list)) {
  1570. struct urb *urb;
  1571. urb = list_entry(local_list.next, struct urb, urb_list);
  1572. list_del_init(&urb->urb_list);
  1573. bh->completing_ep = urb->ep;
  1574. __usb_hcd_giveback_urb(urb);
  1575. bh->completing_ep = NULL;
  1576. }
  1577. /* check if there are new URBs to giveback */
  1578. spin_lock_irq(&bh->lock);
  1579. if (!list_empty(&bh->head))
  1580. goto restart;
  1581. bh->running = false;
  1582. spin_unlock_irq(&bh->lock);
  1583. }
  1584. /**
  1585. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1586. * @hcd: host controller returning the URB
  1587. * @urb: urb being returned to the USB device driver.
  1588. * @status: completion status code for the URB.
  1589. * Context: in_interrupt()
  1590. *
  1591. * This hands the URB from HCD to its USB device driver, using its
  1592. * completion function. The HCD has freed all per-urb resources
  1593. * (and is done using urb->hcpriv). It also released all HCD locks;
  1594. * the device driver won't cause problems if it frees, modifies,
  1595. * or resubmits this URB.
  1596. *
  1597. * If @urb was unlinked, the value of @status will be overridden by
  1598. * @urb->unlinked. Erroneous short transfers are detected in case
  1599. * the HCD hasn't checked for them.
  1600. */
  1601. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1602. {
  1603. struct giveback_urb_bh *bh;
  1604. bool running, high_prio_bh;
  1605. /* pass status to tasklet via unlinked */
  1606. if (likely(!urb->unlinked))
  1607. urb->unlinked = status;
  1608. if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
  1609. __usb_hcd_giveback_urb(urb);
  1610. return;
  1611. }
  1612. if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) {
  1613. bh = &hcd->high_prio_bh;
  1614. high_prio_bh = true;
  1615. } else {
  1616. bh = &hcd->low_prio_bh;
  1617. high_prio_bh = false;
  1618. }
  1619. spin_lock(&bh->lock);
  1620. list_add_tail(&urb->urb_list, &bh->head);
  1621. running = bh->running;
  1622. spin_unlock(&bh->lock);
  1623. if (running)
  1624. ;
  1625. else if (high_prio_bh)
  1626. tasklet_hi_schedule(&bh->bh);
  1627. else
  1628. tasklet_schedule(&bh->bh);
  1629. }
  1630. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1631. /*-------------------------------------------------------------------------*/
  1632. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1633. * queue to drain completely. The caller must first insure that no more
  1634. * URBs can be submitted for this endpoint.
  1635. */
  1636. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1637. struct usb_host_endpoint *ep)
  1638. {
  1639. struct usb_hcd *hcd;
  1640. struct urb *urb;
  1641. if (!ep)
  1642. return;
  1643. might_sleep();
  1644. hcd = bus_to_hcd(udev->bus);
  1645. /* No more submits can occur */
  1646. spin_lock_irq(&hcd_urb_list_lock);
  1647. rescan:
  1648. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1649. int is_in;
  1650. if (urb->unlinked)
  1651. continue;
  1652. usb_get_urb (urb);
  1653. is_in = usb_urb_dir_in(urb);
  1654. spin_unlock(&hcd_urb_list_lock);
  1655. /* kick hcd */
  1656. unlink1(hcd, urb, -ESHUTDOWN);
  1657. dev_dbg (hcd->self.controller,
  1658. "shutdown urb %p ep%d%s%s\n",
  1659. urb, usb_endpoint_num(&ep->desc),
  1660. is_in ? "in" : "out",
  1661. ({ char *s;
  1662. switch (usb_endpoint_type(&ep->desc)) {
  1663. case USB_ENDPOINT_XFER_CONTROL:
  1664. s = ""; break;
  1665. case USB_ENDPOINT_XFER_BULK:
  1666. s = "-bulk"; break;
  1667. case USB_ENDPOINT_XFER_INT:
  1668. s = "-intr"; break;
  1669. default:
  1670. s = "-iso"; break;
  1671. };
  1672. s;
  1673. }));
  1674. usb_put_urb (urb);
  1675. /* list contents may have changed */
  1676. spin_lock(&hcd_urb_list_lock);
  1677. goto rescan;
  1678. }
  1679. spin_unlock_irq(&hcd_urb_list_lock);
  1680. /* Wait until the endpoint queue is completely empty */
  1681. while (!list_empty (&ep->urb_list)) {
  1682. spin_lock_irq(&hcd_urb_list_lock);
  1683. /* The list may have changed while we acquired the spinlock */
  1684. urb = NULL;
  1685. if (!list_empty (&ep->urb_list)) {
  1686. urb = list_entry (ep->urb_list.prev, struct urb,
  1687. urb_list);
  1688. usb_get_urb (urb);
  1689. }
  1690. spin_unlock_irq(&hcd_urb_list_lock);
  1691. if (urb) {
  1692. usb_kill_urb (urb);
  1693. usb_put_urb (urb);
  1694. }
  1695. }
  1696. }
  1697. /**
  1698. * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
  1699. * the bus bandwidth
  1700. * @udev: target &usb_device
  1701. * @new_config: new configuration to install
  1702. * @cur_alt: the current alternate interface setting
  1703. * @new_alt: alternate interface setting that is being installed
  1704. *
  1705. * To change configurations, pass in the new configuration in new_config,
  1706. * and pass NULL for cur_alt and new_alt.
  1707. *
  1708. * To reset a device's configuration (put the device in the ADDRESSED state),
  1709. * pass in NULL for new_config, cur_alt, and new_alt.
  1710. *
  1711. * To change alternate interface settings, pass in NULL for new_config,
  1712. * pass in the current alternate interface setting in cur_alt,
  1713. * and pass in the new alternate interface setting in new_alt.
  1714. *
  1715. * Return: An error if the requested bandwidth change exceeds the
  1716. * bus bandwidth or host controller internal resources.
  1717. */
  1718. int usb_hcd_alloc_bandwidth(struct usb_device *udev,
  1719. struct usb_host_config *new_config,
  1720. struct usb_host_interface *cur_alt,
  1721. struct usb_host_interface *new_alt)
  1722. {
  1723. int num_intfs, i, j;
  1724. struct usb_host_interface *alt = NULL;
  1725. int ret = 0;
  1726. struct usb_hcd *hcd;
  1727. struct usb_host_endpoint *ep;
  1728. hcd = bus_to_hcd(udev->bus);
  1729. if (!hcd->driver->check_bandwidth)
  1730. return 0;
  1731. /* Configuration is being removed - set configuration 0 */
  1732. if (!new_config && !cur_alt) {
  1733. for (i = 1; i < 16; ++i) {
  1734. ep = udev->ep_out[i];
  1735. if (ep)
  1736. hcd->driver->drop_endpoint(hcd, udev, ep);
  1737. ep = udev->ep_in[i];
  1738. if (ep)
  1739. hcd->driver->drop_endpoint(hcd, udev, ep);
  1740. }
  1741. hcd->driver->check_bandwidth(hcd, udev);
  1742. return 0;
  1743. }
  1744. /* Check if the HCD says there's enough bandwidth. Enable all endpoints
  1745. * each interface's alt setting 0 and ask the HCD to check the bandwidth
  1746. * of the bus. There will always be bandwidth for endpoint 0, so it's
  1747. * ok to exclude it.
  1748. */
  1749. if (new_config) {
  1750. num_intfs = new_config->desc.bNumInterfaces;
  1751. /* Remove endpoints (except endpoint 0, which is always on the
  1752. * schedule) from the old config from the schedule
  1753. */
  1754. for (i = 1; i < 16; ++i) {
  1755. ep = udev->ep_out[i];
  1756. if (ep) {
  1757. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1758. if (ret < 0)
  1759. goto reset;
  1760. }
  1761. ep = udev->ep_in[i];
  1762. if (ep) {
  1763. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1764. if (ret < 0)
  1765. goto reset;
  1766. }
  1767. }
  1768. for (i = 0; i < num_intfs; ++i) {
  1769. struct usb_host_interface *first_alt;
  1770. int iface_num;
  1771. first_alt = &new_config->intf_cache[i]->altsetting[0];
  1772. iface_num = first_alt->desc.bInterfaceNumber;
  1773. /* Set up endpoints for alternate interface setting 0 */
  1774. alt = usb_find_alt_setting(new_config, iface_num, 0);
  1775. if (!alt)
  1776. /* No alt setting 0? Pick the first setting. */
  1777. alt = first_alt;
  1778. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  1779. ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
  1780. if (ret < 0)
  1781. goto reset;
  1782. }
  1783. }
  1784. }
  1785. if (cur_alt && new_alt) {
  1786. struct usb_interface *iface = usb_ifnum_to_if(udev,
  1787. cur_alt->desc.bInterfaceNumber);
  1788. if (!iface)
  1789. return -EINVAL;
  1790. if (iface->resetting_device) {
  1791. /*
  1792. * The USB core just reset the device, so the xHCI host
  1793. * and the device will think alt setting 0 is installed.
  1794. * However, the USB core will pass in the alternate
  1795. * setting installed before the reset as cur_alt. Dig
  1796. * out the alternate setting 0 structure, or the first
  1797. * alternate setting if a broken device doesn't have alt
  1798. * setting 0.
  1799. */
  1800. cur_alt = usb_altnum_to_altsetting(iface, 0);
  1801. if (!cur_alt)
  1802. cur_alt = &iface->altsetting[0];
  1803. }
  1804. /* Drop all the endpoints in the current alt setting */
  1805. for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
  1806. ret = hcd->driver->drop_endpoint(hcd, udev,
  1807. &cur_alt->endpoint[i]);
  1808. if (ret < 0)
  1809. goto reset;
  1810. }
  1811. /* Add all the endpoints in the new alt setting */
  1812. for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
  1813. ret = hcd->driver->add_endpoint(hcd, udev,
  1814. &new_alt->endpoint[i]);
  1815. if (ret < 0)
  1816. goto reset;
  1817. }
  1818. }
  1819. ret = hcd->driver->check_bandwidth(hcd, udev);
  1820. reset:
  1821. if (ret < 0)
  1822. hcd->driver->reset_bandwidth(hcd, udev);
  1823. return ret;
  1824. }
  1825. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1826. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1827. * have been called previously. Use for set_configuration, set_interface,
  1828. * driver removal, physical disconnect.
  1829. *
  1830. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1831. * type, maxpacket size, toggle, halt status, and scheduling.
  1832. */
  1833. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1834. struct usb_host_endpoint *ep)
  1835. {
  1836. struct usb_hcd *hcd;
  1837. might_sleep();
  1838. hcd = bus_to_hcd(udev->bus);
  1839. if (hcd->driver->endpoint_disable)
  1840. hcd->driver->endpoint_disable(hcd, ep);
  1841. }
  1842. /**
  1843. * usb_hcd_reset_endpoint - reset host endpoint state
  1844. * @udev: USB device.
  1845. * @ep: the endpoint to reset.
  1846. *
  1847. * Resets any host endpoint state such as the toggle bit, sequence
  1848. * number and current window.
  1849. */
  1850. void usb_hcd_reset_endpoint(struct usb_device *udev,
  1851. struct usb_host_endpoint *ep)
  1852. {
  1853. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1854. if (hcd->driver->endpoint_reset)
  1855. hcd->driver->endpoint_reset(hcd, ep);
  1856. else {
  1857. int epnum = usb_endpoint_num(&ep->desc);
  1858. int is_out = usb_endpoint_dir_out(&ep->desc);
  1859. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1860. usb_settoggle(udev, epnum, is_out, 0);
  1861. if (is_control)
  1862. usb_settoggle(udev, epnum, !is_out, 0);
  1863. }
  1864. }
  1865. /**
  1866. * usb_alloc_streams - allocate bulk endpoint stream IDs.
  1867. * @interface: alternate setting that includes all endpoints.
  1868. * @eps: array of endpoints that need streams.
  1869. * @num_eps: number of endpoints in the array.
  1870. * @num_streams: number of streams to allocate.
  1871. * @mem_flags: flags hcd should use to allocate memory.
  1872. *
  1873. * Sets up a group of bulk endpoints to have @num_streams stream IDs available.
  1874. * Drivers may queue multiple transfers to different stream IDs, which may
  1875. * complete in a different order than they were queued.
  1876. *
  1877. * Return: On success, the number of allocated streams. On failure, a negative
  1878. * error code.
  1879. */
  1880. int usb_alloc_streams(struct usb_interface *interface,
  1881. struct usb_host_endpoint **eps, unsigned int num_eps,
  1882. unsigned int num_streams, gfp_t mem_flags)
  1883. {
  1884. struct usb_hcd *hcd;
  1885. struct usb_device *dev;
  1886. int i, ret;
  1887. dev = interface_to_usbdev(interface);
  1888. hcd = bus_to_hcd(dev->bus);
  1889. if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
  1890. return -EINVAL;
  1891. if (dev->speed < USB_SPEED_SUPER)
  1892. return -EINVAL;
  1893. if (dev->state < USB_STATE_CONFIGURED)
  1894. return -ENODEV;
  1895. for (i = 0; i < num_eps; i++) {
  1896. /* Streams only apply to bulk endpoints. */
  1897. if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
  1898. return -EINVAL;
  1899. /* Re-alloc is not allowed */
  1900. if (eps[i]->streams)
  1901. return -EINVAL;
  1902. }
  1903. ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
  1904. num_streams, mem_flags);
  1905. if (ret < 0)
  1906. return ret;
  1907. for (i = 0; i < num_eps; i++)
  1908. eps[i]->streams = ret;
  1909. return ret;
  1910. }
  1911. EXPORT_SYMBOL_GPL(usb_alloc_streams);
  1912. /**
  1913. * usb_free_streams - free bulk endpoint stream IDs.
  1914. * @interface: alternate setting that includes all endpoints.
  1915. * @eps: array of endpoints to remove streams from.
  1916. * @num_eps: number of endpoints in the array.
  1917. * @mem_flags: flags hcd should use to allocate memory.
  1918. *
  1919. * Reverts a group of bulk endpoints back to not using stream IDs.
  1920. * Can fail if we are given bad arguments, or HCD is broken.
  1921. *
  1922. * Return: 0 on success. On failure, a negative error code.
  1923. */
  1924. int usb_free_streams(struct usb_interface *interface,
  1925. struct usb_host_endpoint **eps, unsigned int num_eps,
  1926. gfp_t mem_flags)
  1927. {
  1928. struct usb_hcd *hcd;
  1929. struct usb_device *dev;
  1930. int i, ret;
  1931. dev = interface_to_usbdev(interface);
  1932. hcd = bus_to_hcd(dev->bus);
  1933. if (dev->speed < USB_SPEED_SUPER)
  1934. return -EINVAL;
  1935. /* Double-free is not allowed */
  1936. for (i = 0; i < num_eps; i++)
  1937. if (!eps[i] || !eps[i]->streams)
  1938. return -EINVAL;
  1939. ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
  1940. if (ret < 0)
  1941. return ret;
  1942. for (i = 0; i < num_eps; i++)
  1943. eps[i]->streams = 0;
  1944. return ret;
  1945. }
  1946. EXPORT_SYMBOL_GPL(usb_free_streams);
  1947. /* Protect against drivers that try to unlink URBs after the device
  1948. * is gone, by waiting until all unlinks for @udev are finished.
  1949. * Since we don't currently track URBs by device, simply wait until
  1950. * nothing is running in the locked region of usb_hcd_unlink_urb().
  1951. */
  1952. void usb_hcd_synchronize_unlinks(struct usb_device *udev)
  1953. {
  1954. spin_lock_irq(&hcd_urb_unlink_lock);
  1955. spin_unlock_irq(&hcd_urb_unlink_lock);
  1956. }
  1957. /*-------------------------------------------------------------------------*/
  1958. /* called in any context */
  1959. int usb_hcd_get_frame_number (struct usb_device *udev)
  1960. {
  1961. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1962. if (!HCD_RH_RUNNING(hcd))
  1963. return -ESHUTDOWN;
  1964. return hcd->driver->get_frame_number (hcd);
  1965. }
  1966. /*-------------------------------------------------------------------------*/
  1967. #ifdef CONFIG_PM
  1968. int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
  1969. {
  1970. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  1971. int status;
  1972. int old_state = hcd->state;
  1973. dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
  1974. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  1975. rhdev->do_remote_wakeup);
  1976. if (HCD_DEAD(hcd)) {
  1977. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
  1978. return 0;
  1979. }
  1980. if (!hcd->driver->bus_suspend) {
  1981. status = -ENOENT;
  1982. } else {
  1983. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1984. hcd->state = HC_STATE_QUIESCING;
  1985. status = hcd->driver->bus_suspend(hcd);
  1986. }
  1987. if (status == 0) {
  1988. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  1989. hcd->state = HC_STATE_SUSPENDED;
  1990. /* Did we race with a root-hub wakeup event? */
  1991. if (rhdev->do_remote_wakeup) {
  1992. char buffer[6];
  1993. status = hcd->driver->hub_status_data(hcd, buffer);
  1994. if (status != 0) {
  1995. dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
  1996. hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
  1997. status = -EBUSY;
  1998. }
  1999. }
  2000. } else {
  2001. spin_lock_irq(&hcd_root_hub_lock);
  2002. if (!HCD_DEAD(hcd)) {
  2003. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2004. hcd->state = old_state;
  2005. }
  2006. spin_unlock_irq(&hcd_root_hub_lock);
  2007. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  2008. "suspend", status);
  2009. }
  2010. return status;
  2011. }
  2012. int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
  2013. {
  2014. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  2015. int status;
  2016. int old_state = hcd->state;
  2017. dev_dbg(&rhdev->dev, "usb %sresume\n",
  2018. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  2019. if (HCD_DEAD(hcd)) {
  2020. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
  2021. return 0;
  2022. }
  2023. if (!hcd->driver->bus_resume)
  2024. return -ENOENT;
  2025. if (HCD_RH_RUNNING(hcd))
  2026. return 0;
  2027. hcd->state = HC_STATE_RESUMING;
  2028. status = hcd->driver->bus_resume(hcd);
  2029. clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  2030. if (status == 0) {
  2031. struct usb_device *udev;
  2032. int port1;
  2033. spin_lock_irq(&hcd_root_hub_lock);
  2034. if (!HCD_DEAD(hcd)) {
  2035. usb_set_device_state(rhdev, rhdev->actconfig
  2036. ? USB_STATE_CONFIGURED
  2037. : USB_STATE_ADDRESS);
  2038. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2039. hcd->state = HC_STATE_RUNNING;
  2040. }
  2041. spin_unlock_irq(&hcd_root_hub_lock);
  2042. /*
  2043. * Check whether any of the enabled ports on the root hub are
  2044. * unsuspended. If they are then a TRSMRCY delay is needed
  2045. * (this is what the USB-2 spec calls a "global resume").
  2046. * Otherwise we can skip the delay.
  2047. */
  2048. usb_hub_for_each_child(rhdev, port1, udev) {
  2049. if (udev->state != USB_STATE_NOTATTACHED &&
  2050. !udev->port_is_suspended) {
  2051. usleep_range(10000, 11000); /* TRSMRCY */
  2052. break;
  2053. }
  2054. }
  2055. } else {
  2056. hcd->state = old_state;
  2057. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  2058. "resume", status);
  2059. if (status != -ESHUTDOWN)
  2060. usb_hc_died(hcd);
  2061. }
  2062. return status;
  2063. }
  2064. /* Workqueue routine for root-hub remote wakeup */
  2065. static void hcd_resume_work(struct work_struct *work)
  2066. {
  2067. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  2068. struct usb_device *udev = hcd->self.root_hub;
  2069. usb_remote_wakeup(udev);
  2070. }
  2071. /**
  2072. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  2073. * @hcd: host controller for this root hub
  2074. *
  2075. * The USB host controller calls this function when its root hub is
  2076. * suspended (with the remote wakeup feature enabled) and a remote
  2077. * wakeup request is received. The routine submits a workqueue request
  2078. * to resume the root hub (that is, manage its downstream ports again).
  2079. */
  2080. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  2081. {
  2082. unsigned long flags;
  2083. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2084. if (hcd->rh_registered) {
  2085. set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  2086. queue_work(pm_wq, &hcd->wakeup_work);
  2087. }
  2088. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2089. }
  2090. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  2091. #endif /* CONFIG_PM */
  2092. /*-------------------------------------------------------------------------*/
  2093. #ifdef CONFIG_USB_OTG
  2094. /**
  2095. * usb_bus_start_enum - start immediate enumeration (for OTG)
  2096. * @bus: the bus (must use hcd framework)
  2097. * @port_num: 1-based number of port; usually bus->otg_port
  2098. * Context: in_interrupt()
  2099. *
  2100. * Starts enumeration, with an immediate reset followed later by
  2101. * hub_wq identifying and possibly configuring the device.
  2102. * This is needed by OTG controller drivers, where it helps meet
  2103. * HNP protocol timing requirements for starting a port reset.
  2104. *
  2105. * Return: 0 if successful.
  2106. */
  2107. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  2108. {
  2109. struct usb_hcd *hcd;
  2110. int status = -EOPNOTSUPP;
  2111. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  2112. * boards with root hubs hooked up to internal devices (instead of
  2113. * just the OTG port) may need more attention to resetting...
  2114. */
  2115. hcd = bus_to_hcd(bus);
  2116. if (port_num && hcd->driver->start_port_reset)
  2117. status = hcd->driver->start_port_reset(hcd, port_num);
  2118. /* allocate hub_wq shortly after (first) root port reset finishes;
  2119. * it may issue others, until at least 50 msecs have passed.
  2120. */
  2121. if (status == 0)
  2122. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  2123. return status;
  2124. }
  2125. EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  2126. #endif
  2127. /*-------------------------------------------------------------------------*/
  2128. /**
  2129. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  2130. * @irq: the IRQ being raised
  2131. * @__hcd: pointer to the HCD whose IRQ is being signaled
  2132. *
  2133. * If the controller isn't HALTed, calls the driver's irq handler.
  2134. * Checks whether the controller is now dead.
  2135. *
  2136. * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise.
  2137. */
  2138. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  2139. {
  2140. struct usb_hcd *hcd = __hcd;
  2141. irqreturn_t rc;
  2142. if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd)))
  2143. rc = IRQ_NONE;
  2144. else if (hcd->driver->irq(hcd) == IRQ_NONE)
  2145. rc = IRQ_NONE;
  2146. else
  2147. rc = IRQ_HANDLED;
  2148. return rc;
  2149. }
  2150. EXPORT_SYMBOL_GPL(usb_hcd_irq);
  2151. /*-------------------------------------------------------------------------*/
  2152. /**
  2153. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  2154. * @hcd: pointer to the HCD representing the controller
  2155. *
  2156. * This is called by bus glue to report a USB host controller that died
  2157. * while operations may still have been pending. It's called automatically
  2158. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  2159. *
  2160. * Only call this function with the primary HCD.
  2161. */
  2162. void usb_hc_died (struct usb_hcd *hcd)
  2163. {
  2164. unsigned long flags;
  2165. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  2166. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2167. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2168. set_bit(HCD_FLAG_DEAD, &hcd->flags);
  2169. if (hcd->rh_registered) {
  2170. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2171. /* make hub_wq clean up old urbs and devices */
  2172. usb_set_device_state (hcd->self.root_hub,
  2173. USB_STATE_NOTATTACHED);
  2174. usb_kick_hub_wq(hcd->self.root_hub);
  2175. }
  2176. if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
  2177. hcd = hcd->shared_hcd;
  2178. if (hcd->rh_registered) {
  2179. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2180. /* make hub_wq clean up old urbs and devices */
  2181. usb_set_device_state(hcd->self.root_hub,
  2182. USB_STATE_NOTATTACHED);
  2183. usb_kick_hub_wq(hcd->self.root_hub);
  2184. }
  2185. }
  2186. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2187. /* Make sure that the other roothub is also deallocated. */
  2188. }
  2189. EXPORT_SYMBOL_GPL (usb_hc_died);
  2190. /*-------------------------------------------------------------------------*/
  2191. static void init_giveback_urb_bh(struct giveback_urb_bh *bh)
  2192. {
  2193. spin_lock_init(&bh->lock);
  2194. INIT_LIST_HEAD(&bh->head);
  2195. tasklet_init(&bh->bh, usb_giveback_urb_bh, (unsigned long)bh);
  2196. }
  2197. /**
  2198. * usb_create_shared_hcd - create and initialize an HCD structure
  2199. * @driver: HC driver that will use this hcd
  2200. * @dev: device for this HC, stored in hcd->self.controller
  2201. * @bus_name: value to store in hcd->self.bus_name
  2202. * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
  2203. * PCI device. Only allocate certain resources for the primary HCD
  2204. * Context: !in_interrupt()
  2205. *
  2206. * Allocate a struct usb_hcd, with extra space at the end for the
  2207. * HC driver's private data. Initialize the generic members of the
  2208. * hcd structure.
  2209. *
  2210. * Return: On success, a pointer to the created and initialized HCD structure.
  2211. * On failure (e.g. if memory is unavailable), %NULL.
  2212. */
  2213. struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
  2214. struct device *dev, const char *bus_name,
  2215. struct usb_hcd *primary_hcd)
  2216. {
  2217. struct usb_hcd *hcd;
  2218. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  2219. if (!hcd) {
  2220. dev_dbg (dev, "hcd alloc failed\n");
  2221. return NULL;
  2222. }
  2223. if (primary_hcd == NULL) {
  2224. hcd->address0_mutex = kmalloc(sizeof(*hcd->address0_mutex),
  2225. GFP_KERNEL);
  2226. if (!hcd->address0_mutex) {
  2227. kfree(hcd);
  2228. dev_dbg(dev, "hcd address0 mutex alloc failed\n");
  2229. return NULL;
  2230. }
  2231. mutex_init(hcd->address0_mutex);
  2232. hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
  2233. GFP_KERNEL);
  2234. if (!hcd->bandwidth_mutex) {
  2235. kfree(hcd);
  2236. dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
  2237. return NULL;
  2238. }
  2239. mutex_init(hcd->bandwidth_mutex);
  2240. dev_set_drvdata(dev, hcd);
  2241. } else {
  2242. mutex_lock(&usb_port_peer_mutex);
  2243. hcd->address0_mutex = primary_hcd->address0_mutex;
  2244. hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
  2245. hcd->primary_hcd = primary_hcd;
  2246. primary_hcd->primary_hcd = primary_hcd;
  2247. hcd->shared_hcd = primary_hcd;
  2248. primary_hcd->shared_hcd = hcd;
  2249. mutex_unlock(&usb_port_peer_mutex);
  2250. }
  2251. kref_init(&hcd->kref);
  2252. usb_bus_init(&hcd->self);
  2253. hcd->self.controller = dev;
  2254. hcd->self.bus_name = bus_name;
  2255. hcd->self.uses_dma = (dev->dma_mask != NULL);
  2256. init_timer(&hcd->rh_timer);
  2257. hcd->rh_timer.function = rh_timer_func;
  2258. hcd->rh_timer.data = (unsigned long) hcd;
  2259. #ifdef CONFIG_PM
  2260. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  2261. #endif
  2262. hcd->driver = driver;
  2263. hcd->speed = driver->flags & HCD_MASK;
  2264. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  2265. "USB Host Controller";
  2266. return hcd;
  2267. }
  2268. EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
  2269. /**
  2270. * usb_create_hcd - create and initialize an HCD structure
  2271. * @driver: HC driver that will use this hcd
  2272. * @dev: device for this HC, stored in hcd->self.controller
  2273. * @bus_name: value to store in hcd->self.bus_name
  2274. * Context: !in_interrupt()
  2275. *
  2276. * Allocate a struct usb_hcd, with extra space at the end for the
  2277. * HC driver's private data. Initialize the generic members of the
  2278. * hcd structure.
  2279. *
  2280. * Return: On success, a pointer to the created and initialized HCD
  2281. * structure. On failure (e.g. if memory is unavailable), %NULL.
  2282. */
  2283. struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
  2284. struct device *dev, const char *bus_name)
  2285. {
  2286. return usb_create_shared_hcd(driver, dev, bus_name, NULL);
  2287. }
  2288. EXPORT_SYMBOL_GPL(usb_create_hcd);
  2289. /*
  2290. * Roothubs that share one PCI device must also share the bandwidth mutex.
  2291. * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is
  2292. * deallocated.
  2293. *
  2294. * Make sure to deallocate the bandwidth_mutex only when the last HCD is
  2295. * freed. When hcd_release() is called for either hcd in a peer set,
  2296. * invalidate the peer's ->shared_hcd and ->primary_hcd pointers.
  2297. */
  2298. static void hcd_release(struct kref *kref)
  2299. {
  2300. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  2301. mutex_lock(&usb_port_peer_mutex);
  2302. if (hcd->shared_hcd) {
  2303. struct usb_hcd *peer = hcd->shared_hcd;
  2304. peer->shared_hcd = NULL;
  2305. peer->primary_hcd = NULL;
  2306. } else {
  2307. kfree(hcd->address0_mutex);
  2308. kfree(hcd->bandwidth_mutex);
  2309. }
  2310. mutex_unlock(&usb_port_peer_mutex);
  2311. kfree(hcd);
  2312. }
  2313. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  2314. {
  2315. if (hcd)
  2316. kref_get (&hcd->kref);
  2317. return hcd;
  2318. }
  2319. EXPORT_SYMBOL_GPL(usb_get_hcd);
  2320. void usb_put_hcd (struct usb_hcd *hcd)
  2321. {
  2322. if (hcd)
  2323. kref_put (&hcd->kref, hcd_release);
  2324. }
  2325. EXPORT_SYMBOL_GPL(usb_put_hcd);
  2326. int usb_hcd_is_primary_hcd(struct usb_hcd *hcd)
  2327. {
  2328. if (!hcd->primary_hcd)
  2329. return 1;
  2330. return hcd == hcd->primary_hcd;
  2331. }
  2332. EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd);
  2333. int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1)
  2334. {
  2335. if (!hcd->driver->find_raw_port_number)
  2336. return port1;
  2337. return hcd->driver->find_raw_port_number(hcd, port1);
  2338. }
  2339. static int usb_hcd_request_irqs(struct usb_hcd *hcd,
  2340. unsigned int irqnum, unsigned long irqflags)
  2341. {
  2342. int retval;
  2343. if (hcd->driver->irq) {
  2344. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  2345. hcd->driver->description, hcd->self.busnum);
  2346. retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  2347. hcd->irq_descr, hcd);
  2348. if (retval != 0) {
  2349. dev_err(hcd->self.controller,
  2350. "request interrupt %d failed\n",
  2351. irqnum);
  2352. return retval;
  2353. }
  2354. hcd->irq = irqnum;
  2355. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  2356. (hcd->driver->flags & HCD_MEMORY) ?
  2357. "io mem" : "io base",
  2358. (unsigned long long)hcd->rsrc_start);
  2359. } else {
  2360. hcd->irq = 0;
  2361. if (hcd->rsrc_start)
  2362. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  2363. (hcd->driver->flags & HCD_MEMORY) ?
  2364. "io mem" : "io base",
  2365. (unsigned long long)hcd->rsrc_start);
  2366. }
  2367. return 0;
  2368. }
  2369. /*
  2370. * Before we free this root hub, flush in-flight peering attempts
  2371. * and disable peer lookups
  2372. */
  2373. static void usb_put_invalidate_rhdev(struct usb_hcd *hcd)
  2374. {
  2375. struct usb_device *rhdev;
  2376. mutex_lock(&usb_port_peer_mutex);
  2377. rhdev = hcd->self.root_hub;
  2378. hcd->self.root_hub = NULL;
  2379. mutex_unlock(&usb_port_peer_mutex);
  2380. usb_put_dev(rhdev);
  2381. }
  2382. /**
  2383. * usb_add_hcd - finish generic HCD structure initialization and register
  2384. * @hcd: the usb_hcd structure to initialize
  2385. * @irqnum: Interrupt line to allocate
  2386. * @irqflags: Interrupt type flags
  2387. *
  2388. * Finish the remaining parts of generic HCD initialization: allocate the
  2389. * buffers of consistent memory, register the bus, request the IRQ line,
  2390. * and call the driver's reset() and start() routines.
  2391. */
  2392. int usb_add_hcd(struct usb_hcd *hcd,
  2393. unsigned int irqnum, unsigned long irqflags)
  2394. {
  2395. int retval;
  2396. struct usb_device *rhdev;
  2397. if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
  2398. struct usb_phy *phy = usb_get_phy_dev(hcd->self.controller, 0);
  2399. if (IS_ERR(phy)) {
  2400. retval = PTR_ERR(phy);
  2401. if (retval == -EPROBE_DEFER)
  2402. return retval;
  2403. } else {
  2404. retval = usb_phy_init(phy);
  2405. if (retval) {
  2406. usb_put_phy(phy);
  2407. return retval;
  2408. }
  2409. hcd->usb_phy = phy;
  2410. hcd->remove_phy = 1;
  2411. }
  2412. }
  2413. if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
  2414. struct phy *phy = phy_get(hcd->self.controller, "usb");
  2415. if (IS_ERR(phy)) {
  2416. retval = PTR_ERR(phy);
  2417. if (retval == -EPROBE_DEFER)
  2418. goto err_phy;
  2419. } else {
  2420. retval = phy_init(phy);
  2421. if (retval) {
  2422. phy_put(phy);
  2423. goto err_phy;
  2424. }
  2425. retval = phy_power_on(phy);
  2426. if (retval) {
  2427. phy_exit(phy);
  2428. phy_put(phy);
  2429. goto err_phy;
  2430. }
  2431. hcd->phy = phy;
  2432. hcd->remove_phy = 1;
  2433. }
  2434. }
  2435. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  2436. /* Keep old behaviour if authorized_default is not in [0, 1]. */
  2437. if (authorized_default < 0 || authorized_default > 1) {
  2438. if (hcd->wireless)
  2439. clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2440. else
  2441. set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2442. } else {
  2443. if (authorized_default)
  2444. set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2445. else
  2446. clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2447. }
  2448. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2449. /* per default all interfaces are authorized */
  2450. set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  2451. /* HC is in reset state, but accessible. Now do the one-time init,
  2452. * bottom up so that hcds can customize the root hubs before hub_wq
  2453. * starts talking to them. (Note, bus id is assigned early too.)
  2454. */
  2455. retval = hcd_buffer_create(hcd);
  2456. if (retval != 0) {
  2457. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  2458. goto err_create_buf;
  2459. }
  2460. retval = usb_register_bus(&hcd->self);
  2461. if (retval < 0)
  2462. goto err_register_bus;
  2463. rhdev = usb_alloc_dev(NULL, &hcd->self, 0);
  2464. if (rhdev == NULL) {
  2465. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  2466. retval = -ENOMEM;
  2467. goto err_allocate_root_hub;
  2468. }
  2469. mutex_lock(&usb_port_peer_mutex);
  2470. hcd->self.root_hub = rhdev;
  2471. mutex_unlock(&usb_port_peer_mutex);
  2472. switch (hcd->speed) {
  2473. case HCD_USB11:
  2474. rhdev->speed = USB_SPEED_FULL;
  2475. break;
  2476. case HCD_USB2:
  2477. rhdev->speed = USB_SPEED_HIGH;
  2478. break;
  2479. case HCD_USB25:
  2480. rhdev->speed = USB_SPEED_WIRELESS;
  2481. break;
  2482. case HCD_USB3:
  2483. rhdev->speed = USB_SPEED_SUPER;
  2484. break;
  2485. case HCD_USB31:
  2486. rhdev->speed = USB_SPEED_SUPER_PLUS;
  2487. break;
  2488. default:
  2489. retval = -EINVAL;
  2490. goto err_set_rh_speed;
  2491. }
  2492. /* wakeup flag init defaults to "everything works" for root hubs,
  2493. * but drivers can override it in reset() if needed, along with
  2494. * recording the overall controller's system wakeup capability.
  2495. */
  2496. device_set_wakeup_capable(&rhdev->dev, 1);
  2497. /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
  2498. * registered. But since the controller can die at any time,
  2499. * let's initialize the flag before touching the hardware.
  2500. */
  2501. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2502. /* "reset" is misnamed; its role is now one-time init. the controller
  2503. * should already have been reset (and boot firmware kicked off etc).
  2504. */
  2505. if (hcd->driver->reset) {
  2506. retval = hcd->driver->reset(hcd);
  2507. if (retval < 0) {
  2508. dev_err(hcd->self.controller, "can't setup: %d\n",
  2509. retval);
  2510. goto err_hcd_driver_setup;
  2511. }
  2512. }
  2513. hcd->rh_pollable = 1;
  2514. /* NOTE: root hub and controller capabilities may not be the same */
  2515. if (device_can_wakeup(hcd->self.controller)
  2516. && device_can_wakeup(&hcd->self.root_hub->dev))
  2517. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  2518. /* initialize tasklets */
  2519. init_giveback_urb_bh(&hcd->high_prio_bh);
  2520. init_giveback_urb_bh(&hcd->low_prio_bh);
  2521. /* enable irqs just before we start the controller,
  2522. * if the BIOS provides legacy PCI irqs.
  2523. */
  2524. if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
  2525. retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
  2526. if (retval)
  2527. goto err_request_irq;
  2528. }
  2529. hcd->state = HC_STATE_RUNNING;
  2530. retval = hcd->driver->start(hcd);
  2531. if (retval < 0) {
  2532. dev_err(hcd->self.controller, "startup error %d\n", retval);
  2533. goto err_hcd_driver_start;
  2534. }
  2535. /* starting here, usbcore will pay attention to this root hub */
  2536. retval = register_root_hub(hcd);
  2537. if (retval != 0)
  2538. goto err_register_root_hub;
  2539. retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  2540. if (retval < 0) {
  2541. printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
  2542. retval);
  2543. goto error_create_attr_group;
  2544. }
  2545. if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
  2546. usb_hcd_poll_rh_status(hcd);
  2547. return retval;
  2548. error_create_attr_group:
  2549. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2550. if (HC_IS_RUNNING(hcd->state))
  2551. hcd->state = HC_STATE_QUIESCING;
  2552. spin_lock_irq(&hcd_root_hub_lock);
  2553. hcd->rh_registered = 0;
  2554. spin_unlock_irq(&hcd_root_hub_lock);
  2555. #ifdef CONFIG_PM
  2556. cancel_work_sync(&hcd->wakeup_work);
  2557. #endif
  2558. mutex_lock(&usb_bus_idr_lock);
  2559. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2560. mutex_unlock(&usb_bus_idr_lock);
  2561. err_register_root_hub:
  2562. hcd->rh_pollable = 0;
  2563. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2564. del_timer_sync(&hcd->rh_timer);
  2565. hcd->driver->stop(hcd);
  2566. hcd->state = HC_STATE_HALT;
  2567. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2568. del_timer_sync(&hcd->rh_timer);
  2569. err_hcd_driver_start:
  2570. if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
  2571. free_irq(irqnum, hcd);
  2572. err_request_irq:
  2573. err_hcd_driver_setup:
  2574. err_set_rh_speed:
  2575. usb_put_invalidate_rhdev(hcd);
  2576. err_allocate_root_hub:
  2577. usb_deregister_bus(&hcd->self);
  2578. err_register_bus:
  2579. hcd_buffer_destroy(hcd);
  2580. err_create_buf:
  2581. if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
  2582. phy_power_off(hcd->phy);
  2583. phy_exit(hcd->phy);
  2584. phy_put(hcd->phy);
  2585. hcd->phy = NULL;
  2586. }
  2587. err_phy:
  2588. if (hcd->remove_phy && hcd->usb_phy) {
  2589. usb_phy_shutdown(hcd->usb_phy);
  2590. usb_put_phy(hcd->usb_phy);
  2591. hcd->usb_phy = NULL;
  2592. }
  2593. return retval;
  2594. }
  2595. EXPORT_SYMBOL_GPL(usb_add_hcd);
  2596. /**
  2597. * usb_remove_hcd - shutdown processing for generic HCDs
  2598. * @hcd: the usb_hcd structure to remove
  2599. * Context: !in_interrupt()
  2600. *
  2601. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  2602. * invoking the HCD's stop() method.
  2603. */
  2604. void usb_remove_hcd(struct usb_hcd *hcd)
  2605. {
  2606. struct usb_device *rhdev = hcd->self.root_hub;
  2607. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  2608. usb_get_dev(rhdev);
  2609. sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  2610. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2611. if (HC_IS_RUNNING (hcd->state))
  2612. hcd->state = HC_STATE_QUIESCING;
  2613. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  2614. spin_lock_irq (&hcd_root_hub_lock);
  2615. hcd->rh_registered = 0;
  2616. spin_unlock_irq (&hcd_root_hub_lock);
  2617. #ifdef CONFIG_PM
  2618. cancel_work_sync(&hcd->wakeup_work);
  2619. #endif
  2620. mutex_lock(&usb_bus_idr_lock);
  2621. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2622. mutex_unlock(&usb_bus_idr_lock);
  2623. /*
  2624. * tasklet_kill() isn't needed here because:
  2625. * - driver's disconnect() called from usb_disconnect() should
  2626. * make sure its URBs are completed during the disconnect()
  2627. * callback
  2628. *
  2629. * - it is too late to run complete() here since driver may have
  2630. * been removed already now
  2631. */
  2632. /* Prevent any more root-hub status calls from the timer.
  2633. * The HCD might still restart the timer (if a port status change
  2634. * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
  2635. * the hub_status_data() callback.
  2636. */
  2637. hcd->rh_pollable = 0;
  2638. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2639. del_timer_sync(&hcd->rh_timer);
  2640. hcd->driver->stop(hcd);
  2641. hcd->state = HC_STATE_HALT;
  2642. /* In case the HCD restarted the timer, stop it again. */
  2643. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2644. del_timer_sync(&hcd->rh_timer);
  2645. if (usb_hcd_is_primary_hcd(hcd)) {
  2646. if (hcd->irq > 0)
  2647. free_irq(hcd->irq, hcd);
  2648. }
  2649. usb_deregister_bus(&hcd->self);
  2650. hcd_buffer_destroy(hcd);
  2651. if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
  2652. phy_power_off(hcd->phy);
  2653. phy_exit(hcd->phy);
  2654. phy_put(hcd->phy);
  2655. hcd->phy = NULL;
  2656. }
  2657. if (hcd->remove_phy && hcd->usb_phy) {
  2658. usb_phy_shutdown(hcd->usb_phy);
  2659. usb_put_phy(hcd->usb_phy);
  2660. hcd->usb_phy = NULL;
  2661. }
  2662. usb_put_invalidate_rhdev(hcd);
  2663. }
  2664. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  2665. void
  2666. usb_hcd_platform_shutdown(struct platform_device *dev)
  2667. {
  2668. struct usb_hcd *hcd = platform_get_drvdata(dev);
  2669. if (hcd->driver->shutdown)
  2670. hcd->driver->shutdown(hcd);
  2671. }
  2672. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  2673. /*-------------------------------------------------------------------------*/
  2674. #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
  2675. const struct usb_mon_operations *mon_ops;
  2676. /*
  2677. * The registration is unlocked.
  2678. * We do it this way because we do not want to lock in hot paths.
  2679. *
  2680. * Notice that the code is minimally error-proof. Because usbmon needs
  2681. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  2682. */
  2683. int usb_mon_register(const struct usb_mon_operations *ops)
  2684. {
  2685. if (mon_ops)
  2686. return -EBUSY;
  2687. mon_ops = ops;
  2688. mb();
  2689. return 0;
  2690. }
  2691. EXPORT_SYMBOL_GPL (usb_mon_register);
  2692. void usb_mon_deregister (void)
  2693. {
  2694. if (mon_ops == NULL) {
  2695. printk(KERN_ERR "USB: monitor was not registered\n");
  2696. return;
  2697. }
  2698. mon_ops = NULL;
  2699. mb();
  2700. }
  2701. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  2702. #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */