hcd.c 83 KB

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