hcd.c 76 KB

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