hcd.c 70 KB

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