hcd.c 83 KB

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