hcd.c 78 KB

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