hcd.c 83 KB

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