hcd.c 77 KB

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