hcd.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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 <asm/scatterlist.h>
  33. #include <linux/device.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/mutex.h>
  36. #include <asm/irq.h>
  37. #include <asm/byteorder.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/usb.h>
  40. #include "usb.h"
  41. #include "hcd.h"
  42. #include "hub.h"
  43. // #define USB_BANDWIDTH_MESSAGES
  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. /* host controllers we manage */
  77. LIST_HEAD (usb_bus_list);
  78. EXPORT_SYMBOL_GPL (usb_bus_list);
  79. /* used when allocating bus numbers */
  80. #define USB_MAXBUS 64
  81. struct usb_busmap {
  82. unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
  83. };
  84. static struct usb_busmap busmap;
  85. /* used when updating list of hcds */
  86. DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
  87. EXPORT_SYMBOL_GPL (usb_bus_list_lock);
  88. /* used for controlling access to virtual root hubs */
  89. static DEFINE_SPINLOCK(hcd_root_hub_lock);
  90. /* used when updating hcd data */
  91. static DEFINE_SPINLOCK(hcd_data_lock);
  92. /* wait queue for synchronous unlinks */
  93. DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
  94. /*-------------------------------------------------------------------------*/
  95. /*
  96. * Sharable chunks of root hub code.
  97. */
  98. /*-------------------------------------------------------------------------*/
  99. #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
  100. #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
  101. /* usb 2.0 root hub device descriptor */
  102. static const u8 usb2_rh_dev_descriptor [18] = {
  103. 0x12, /* __u8 bLength; */
  104. 0x01, /* __u8 bDescriptorType; Device */
  105. 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
  106. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  107. 0x00, /* __u8 bDeviceSubClass; */
  108. 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/
  109. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  110. 0x00, 0x00, /* __le16 idVendor; */
  111. 0x00, 0x00, /* __le16 idProduct; */
  112. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  113. 0x03, /* __u8 iManufacturer; */
  114. 0x02, /* __u8 iProduct; */
  115. 0x01, /* __u8 iSerialNumber; */
  116. 0x01 /* __u8 bNumConfigurations; */
  117. };
  118. /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
  119. /* usb 1.1 root hub device descriptor */
  120. static const u8 usb11_rh_dev_descriptor [18] = {
  121. 0x12, /* __u8 bLength; */
  122. 0x01, /* __u8 bDescriptorType; Device */
  123. 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
  124. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  125. 0x00, /* __u8 bDeviceSubClass; */
  126. 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
  127. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  128. 0x00, 0x00, /* __le16 idVendor; */
  129. 0x00, 0x00, /* __le16 idProduct; */
  130. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  131. 0x03, /* __u8 iManufacturer; */
  132. 0x02, /* __u8 iProduct; */
  133. 0x01, /* __u8 iSerialNumber; */
  134. 0x01 /* __u8 bNumConfigurations; */
  135. };
  136. /*-------------------------------------------------------------------------*/
  137. /* Configuration descriptors for our root hubs */
  138. static const u8 fs_rh_config_descriptor [] = {
  139. /* one configuration */
  140. 0x09, /* __u8 bLength; */
  141. 0x02, /* __u8 bDescriptorType; Configuration */
  142. 0x19, 0x00, /* __le16 wTotalLength; */
  143. 0x01, /* __u8 bNumInterfaces; (1) */
  144. 0x01, /* __u8 bConfigurationValue; */
  145. 0x00, /* __u8 iConfiguration; */
  146. 0xc0, /* __u8 bmAttributes;
  147. Bit 7: must be set,
  148. 6: Self-powered,
  149. 5: Remote wakeup,
  150. 4..0: resvd */
  151. 0x00, /* __u8 MaxPower; */
  152. /* USB 1.1:
  153. * USB 2.0, single TT organization (mandatory):
  154. * one interface, protocol 0
  155. *
  156. * USB 2.0, multiple TT organization (optional):
  157. * two interfaces, protocols 1 (like single TT)
  158. * and 2 (multiple TT mode) ... config is
  159. * sometimes settable
  160. * NOT IMPLEMENTED
  161. */
  162. /* one interface */
  163. 0x09, /* __u8 if_bLength; */
  164. 0x04, /* __u8 if_bDescriptorType; Interface */
  165. 0x00, /* __u8 if_bInterfaceNumber; */
  166. 0x00, /* __u8 if_bAlternateSetting; */
  167. 0x01, /* __u8 if_bNumEndpoints; */
  168. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  169. 0x00, /* __u8 if_bInterfaceSubClass; */
  170. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  171. 0x00, /* __u8 if_iInterface; */
  172. /* one endpoint (status change endpoint) */
  173. 0x07, /* __u8 ep_bLength; */
  174. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  175. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  176. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  177. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  178. 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  179. };
  180. static const u8 hs_rh_config_descriptor [] = {
  181. /* one configuration */
  182. 0x09, /* __u8 bLength; */
  183. 0x02, /* __u8 bDescriptorType; Configuration */
  184. 0x19, 0x00, /* __le16 wTotalLength; */
  185. 0x01, /* __u8 bNumInterfaces; (1) */
  186. 0x01, /* __u8 bConfigurationValue; */
  187. 0x00, /* __u8 iConfiguration; */
  188. 0xc0, /* __u8 bmAttributes;
  189. Bit 7: must be set,
  190. 6: Self-powered,
  191. 5: Remote wakeup,
  192. 4..0: resvd */
  193. 0x00, /* __u8 MaxPower; */
  194. /* USB 1.1:
  195. * USB 2.0, single TT organization (mandatory):
  196. * one interface, protocol 0
  197. *
  198. * USB 2.0, multiple TT organization (optional):
  199. * two interfaces, protocols 1 (like single TT)
  200. * and 2 (multiple TT mode) ... config is
  201. * sometimes settable
  202. * NOT IMPLEMENTED
  203. */
  204. /* one interface */
  205. 0x09, /* __u8 if_bLength; */
  206. 0x04, /* __u8 if_bDescriptorType; Interface */
  207. 0x00, /* __u8 if_bInterfaceNumber; */
  208. 0x00, /* __u8 if_bAlternateSetting; */
  209. 0x01, /* __u8 if_bNumEndpoints; */
  210. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  211. 0x00, /* __u8 if_bInterfaceSubClass; */
  212. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  213. 0x00, /* __u8 if_iInterface; */
  214. /* one endpoint (status change endpoint) */
  215. 0x07, /* __u8 ep_bLength; */
  216. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  217. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  218. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  219. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  220. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  221. };
  222. /*-------------------------------------------------------------------------*/
  223. /*
  224. * helper routine for returning string descriptors in UTF-16LE
  225. * input can actually be ISO-8859-1; ASCII is its 7-bit subset
  226. */
  227. static int ascii2utf (char *s, u8 *utf, int utfmax)
  228. {
  229. int retval;
  230. for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
  231. *utf++ = *s++;
  232. *utf++ = 0;
  233. }
  234. if (utfmax > 0) {
  235. *utf = *s;
  236. ++retval;
  237. }
  238. return retval;
  239. }
  240. /*
  241. * rh_string - provides manufacturer, product and serial strings for root hub
  242. * @id: the string ID number (1: serial number, 2: product, 3: vendor)
  243. * @hcd: the host controller for this root hub
  244. * @type: string describing our driver
  245. * @data: return packet in UTF-16 LE
  246. * @len: length of the return packet
  247. *
  248. * Produces either a manufacturer, product or serial number string for the
  249. * virtual root hub device.
  250. */
  251. static int rh_string (
  252. int id,
  253. struct usb_hcd *hcd,
  254. u8 *data,
  255. int len
  256. ) {
  257. char buf [100];
  258. // language ids
  259. if (id == 0) {
  260. buf[0] = 4; buf[1] = 3; /* 4 bytes string data */
  261. buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */
  262. len = min (len, 4);
  263. memcpy (data, buf, len);
  264. return len;
  265. // serial number
  266. } else if (id == 1) {
  267. strlcpy (buf, hcd->self.bus_name, sizeof buf);
  268. // product description
  269. } else if (id == 2) {
  270. strlcpy (buf, hcd->product_desc, sizeof buf);
  271. // id 3 == vendor description
  272. } else if (id == 3) {
  273. snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
  274. init_utsname()->release, hcd->driver->description);
  275. // unsupported IDs --> "protocol stall"
  276. } else
  277. return -EPIPE;
  278. switch (len) { /* All cases fall through */
  279. default:
  280. len = 2 + ascii2utf (buf, data + 2, len - 2);
  281. case 2:
  282. data [1] = 3; /* type == string */
  283. case 1:
  284. data [0] = 2 * (strlen (buf) + 1);
  285. case 0:
  286. ; /* Compiler wants a statement here */
  287. }
  288. return len;
  289. }
  290. /* Root hub control transfers execute synchronously */
  291. static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
  292. {
  293. struct usb_ctrlrequest *cmd;
  294. u16 typeReq, wValue, wIndex, wLength;
  295. u8 *ubuf = urb->transfer_buffer;
  296. u8 tbuf [sizeof (struct usb_hub_descriptor)]
  297. __attribute__((aligned(4)));
  298. const u8 *bufp = tbuf;
  299. int len = 0;
  300. int patch_wakeup = 0;
  301. unsigned long flags;
  302. int status = 0;
  303. int n;
  304. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  305. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  306. wValue = le16_to_cpu (cmd->wValue);
  307. wIndex = le16_to_cpu (cmd->wIndex);
  308. wLength = le16_to_cpu (cmd->wLength);
  309. if (wLength > urb->transfer_buffer_length)
  310. goto error;
  311. urb->actual_length = 0;
  312. switch (typeReq) {
  313. /* DEVICE REQUESTS */
  314. /* The root hub's remote wakeup enable bit is implemented using
  315. * driver model wakeup flags. If this system supports wakeup
  316. * through USB, userspace may change the default "allow wakeup"
  317. * policy through sysfs or these calls.
  318. *
  319. * Most root hubs support wakeup from downstream devices, for
  320. * runtime power management (disabling USB clocks and reducing
  321. * VBUS power usage). However, not all of them do so; silicon,
  322. * board, and BIOS bugs here are not uncommon, so these can't
  323. * be treated quite like external hubs.
  324. *
  325. * Likewise, not all root hubs will pass wakeup events upstream,
  326. * to wake up the whole system. So don't assume root hub and
  327. * controller capabilities are identical.
  328. */
  329. case DeviceRequest | USB_REQ_GET_STATUS:
  330. tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
  331. << USB_DEVICE_REMOTE_WAKEUP)
  332. | (1 << USB_DEVICE_SELF_POWERED);
  333. tbuf [1] = 0;
  334. len = 2;
  335. break;
  336. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  337. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  338. device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
  339. else
  340. goto error;
  341. break;
  342. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  343. if (device_can_wakeup(&hcd->self.root_hub->dev)
  344. && wValue == USB_DEVICE_REMOTE_WAKEUP)
  345. device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
  346. else
  347. goto error;
  348. break;
  349. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  350. tbuf [0] = 1;
  351. len = 1;
  352. /* FALLTHROUGH */
  353. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  354. break;
  355. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  356. switch (wValue & 0xff00) {
  357. case USB_DT_DEVICE << 8:
  358. if (hcd->driver->flags & HCD_USB2)
  359. bufp = usb2_rh_dev_descriptor;
  360. else if (hcd->driver->flags & HCD_USB11)
  361. bufp = usb11_rh_dev_descriptor;
  362. else
  363. goto error;
  364. len = 18;
  365. break;
  366. case USB_DT_CONFIG << 8:
  367. if (hcd->driver->flags & HCD_USB2) {
  368. bufp = hs_rh_config_descriptor;
  369. len = sizeof hs_rh_config_descriptor;
  370. } else {
  371. bufp = fs_rh_config_descriptor;
  372. len = sizeof fs_rh_config_descriptor;
  373. }
  374. if (device_can_wakeup(&hcd->self.root_hub->dev))
  375. patch_wakeup = 1;
  376. break;
  377. case USB_DT_STRING << 8:
  378. n = rh_string (wValue & 0xff, hcd, ubuf, wLength);
  379. if (n < 0)
  380. goto error;
  381. urb->actual_length = n;
  382. break;
  383. default:
  384. goto error;
  385. }
  386. break;
  387. case DeviceRequest | USB_REQ_GET_INTERFACE:
  388. tbuf [0] = 0;
  389. len = 1;
  390. /* FALLTHROUGH */
  391. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  392. break;
  393. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  394. // wValue == urb->dev->devaddr
  395. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  396. wValue);
  397. break;
  398. /* INTERFACE REQUESTS (no defined feature/status flags) */
  399. /* ENDPOINT REQUESTS */
  400. case EndpointRequest | USB_REQ_GET_STATUS:
  401. // ENDPOINT_HALT flag
  402. tbuf [0] = 0;
  403. tbuf [1] = 0;
  404. len = 2;
  405. /* FALLTHROUGH */
  406. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  407. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  408. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  409. break;
  410. /* CLASS REQUESTS (and errors) */
  411. default:
  412. /* non-generic request */
  413. switch (typeReq) {
  414. case GetHubStatus:
  415. case GetPortStatus:
  416. len = 4;
  417. break;
  418. case GetHubDescriptor:
  419. len = sizeof (struct usb_hub_descriptor);
  420. break;
  421. }
  422. status = hcd->driver->hub_control (hcd,
  423. typeReq, wValue, wIndex,
  424. tbuf, wLength);
  425. break;
  426. error:
  427. /* "protocol stall" on error */
  428. status = -EPIPE;
  429. }
  430. if (status) {
  431. len = 0;
  432. if (status != -EPIPE) {
  433. dev_dbg (hcd->self.controller,
  434. "CTRL: TypeReq=0x%x val=0x%x "
  435. "idx=0x%x len=%d ==> %d\n",
  436. typeReq, wValue, wIndex,
  437. wLength, status);
  438. }
  439. }
  440. if (len) {
  441. if (urb->transfer_buffer_length < len)
  442. len = urb->transfer_buffer_length;
  443. urb->actual_length = len;
  444. // always USB_DIR_IN, toward host
  445. memcpy (ubuf, bufp, len);
  446. /* report whether RH hardware supports remote wakeup */
  447. if (patch_wakeup &&
  448. len > offsetof (struct usb_config_descriptor,
  449. bmAttributes))
  450. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  451. |= USB_CONFIG_ATT_WAKEUP;
  452. }
  453. /* any errors get returned through the urb completion */
  454. local_irq_save (flags);
  455. spin_lock (&urb->lock);
  456. if (urb->status == -EINPROGRESS)
  457. urb->status = status;
  458. spin_unlock (&urb->lock);
  459. usb_hcd_giveback_urb (hcd, urb);
  460. local_irq_restore (flags);
  461. return 0;
  462. }
  463. /*-------------------------------------------------------------------------*/
  464. /*
  465. * Root Hub interrupt transfers are polled using a timer if the
  466. * driver requests it; otherwise the driver is responsible for
  467. * calling usb_hcd_poll_rh_status() when an event occurs.
  468. *
  469. * Completions are called in_interrupt(), but they may or may not
  470. * be in_irq().
  471. */
  472. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  473. {
  474. struct urb *urb;
  475. int length;
  476. unsigned long flags;
  477. char buffer[4]; /* Any root hubs with > 31 ports? */
  478. if (!hcd->uses_new_polling && !hcd->status_urb)
  479. return;
  480. length = hcd->driver->hub_status_data(hcd, buffer);
  481. if (length > 0) {
  482. /* try to complete the status urb */
  483. local_irq_save (flags);
  484. spin_lock(&hcd_root_hub_lock);
  485. urb = hcd->status_urb;
  486. if (urb) {
  487. spin_lock(&urb->lock);
  488. if (urb->status == -EINPROGRESS) {
  489. hcd->poll_pending = 0;
  490. hcd->status_urb = NULL;
  491. urb->status = 0;
  492. urb->hcpriv = NULL;
  493. urb->actual_length = length;
  494. memcpy(urb->transfer_buffer, buffer, length);
  495. } else /* urb has been unlinked */
  496. length = 0;
  497. spin_unlock(&urb->lock);
  498. } else
  499. length = 0;
  500. spin_unlock(&hcd_root_hub_lock);
  501. /* local irqs are always blocked in completions */
  502. if (length > 0)
  503. usb_hcd_giveback_urb (hcd, urb);
  504. else
  505. hcd->poll_pending = 1;
  506. local_irq_restore (flags);
  507. }
  508. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  509. * exceed that limit if HZ is 100. */
  510. if (hcd->uses_new_polling ? hcd->poll_rh :
  511. (length == 0 && hcd->status_urb != NULL))
  512. mod_timer (&hcd->rh_timer, jiffies + msecs_to_jiffies(250));
  513. }
  514. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  515. /* timer callback */
  516. static void rh_timer_func (unsigned long _hcd)
  517. {
  518. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  519. }
  520. /*-------------------------------------------------------------------------*/
  521. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  522. {
  523. int retval;
  524. unsigned long flags;
  525. int len = 1 + (urb->dev->maxchild / 8);
  526. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  527. if (urb->status != -EINPROGRESS) /* already unlinked */
  528. retval = urb->status;
  529. else if (hcd->status_urb || urb->transfer_buffer_length < len) {
  530. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  531. retval = -EINVAL;
  532. } else {
  533. hcd->status_urb = urb;
  534. urb->hcpriv = hcd; /* indicate it's queued */
  535. if (!hcd->uses_new_polling)
  536. mod_timer (&hcd->rh_timer, jiffies +
  537. msecs_to_jiffies(250));
  538. /* If a status change has already occurred, report it ASAP */
  539. else if (hcd->poll_pending)
  540. mod_timer (&hcd->rh_timer, jiffies);
  541. retval = 0;
  542. }
  543. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  544. return retval;
  545. }
  546. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  547. {
  548. if (usb_pipeint (urb->pipe))
  549. return rh_queue_status (hcd, urb);
  550. if (usb_pipecontrol (urb->pipe))
  551. return rh_call_control (hcd, urb);
  552. return -EINVAL;
  553. }
  554. /*-------------------------------------------------------------------------*/
  555. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  556. * since these URBs always execute synchronously.
  557. */
  558. static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  559. {
  560. unsigned long flags;
  561. if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */
  562. ; /* Do nothing */
  563. } else { /* Status URB */
  564. if (!hcd->uses_new_polling)
  565. del_timer (&hcd->rh_timer);
  566. local_irq_save (flags);
  567. spin_lock (&hcd_root_hub_lock);
  568. if (urb == hcd->status_urb) {
  569. hcd->status_urb = NULL;
  570. urb->hcpriv = NULL;
  571. } else
  572. urb = NULL; /* wasn't fully queued */
  573. spin_unlock (&hcd_root_hub_lock);
  574. if (urb)
  575. usb_hcd_giveback_urb (hcd, urb);
  576. local_irq_restore (flags);
  577. }
  578. return 0;
  579. }
  580. /*-------------------------------------------------------------------------*/
  581. static struct class *usb_host_class;
  582. int usb_host_init(void)
  583. {
  584. int retval = 0;
  585. usb_host_class = class_create(THIS_MODULE, "usb_host");
  586. if (IS_ERR(usb_host_class))
  587. retval = PTR_ERR(usb_host_class);
  588. return retval;
  589. }
  590. void usb_host_cleanup(void)
  591. {
  592. class_destroy(usb_host_class);
  593. }
  594. /**
  595. * usb_bus_init - shared initialization code
  596. * @bus: the bus structure being initialized
  597. *
  598. * This code is used to initialize a usb_bus structure, memory for which is
  599. * separately managed.
  600. */
  601. static void usb_bus_init (struct usb_bus *bus)
  602. {
  603. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  604. bus->devnum_next = 1;
  605. bus->root_hub = NULL;
  606. bus->busnum = -1;
  607. bus->bandwidth_allocated = 0;
  608. bus->bandwidth_int_reqs = 0;
  609. bus->bandwidth_isoc_reqs = 0;
  610. INIT_LIST_HEAD (&bus->bus_list);
  611. }
  612. /*-------------------------------------------------------------------------*/
  613. /**
  614. * usb_register_bus - registers the USB host controller with the usb core
  615. * @bus: pointer to the bus to register
  616. * Context: !in_interrupt()
  617. *
  618. * Assigns a bus number, and links the controller into usbcore data
  619. * structures so that it can be seen by scanning the bus list.
  620. */
  621. static int usb_register_bus(struct usb_bus *bus)
  622. {
  623. int busnum;
  624. mutex_lock(&usb_bus_list_lock);
  625. busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  626. if (busnum < USB_MAXBUS) {
  627. set_bit (busnum, busmap.busmap);
  628. bus->busnum = busnum;
  629. } else {
  630. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  631. mutex_unlock(&usb_bus_list_lock);
  632. return -E2BIG;
  633. }
  634. bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0),
  635. bus->controller, "usb_host%d", busnum);
  636. if (IS_ERR(bus->class_dev)) {
  637. clear_bit(busnum, busmap.busmap);
  638. mutex_unlock(&usb_bus_list_lock);
  639. return PTR_ERR(bus->class_dev);
  640. }
  641. class_set_devdata(bus->class_dev, bus);
  642. /* Add it to the local list of buses */
  643. list_add (&bus->bus_list, &usb_bus_list);
  644. mutex_unlock(&usb_bus_list_lock);
  645. usb_notify_add_bus(bus);
  646. dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
  647. return 0;
  648. }
  649. /**
  650. * usb_deregister_bus - deregisters the USB host controller
  651. * @bus: pointer to the bus to deregister
  652. * Context: !in_interrupt()
  653. *
  654. * Recycles the bus number, and unlinks the controller from usbcore data
  655. * structures so that it won't be seen by scanning the bus list.
  656. */
  657. static void usb_deregister_bus (struct usb_bus *bus)
  658. {
  659. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  660. /*
  661. * NOTE: make sure that all the devices are removed by the
  662. * controller code, as well as having it call this when cleaning
  663. * itself up
  664. */
  665. mutex_lock(&usb_bus_list_lock);
  666. list_del (&bus->bus_list);
  667. mutex_unlock(&usb_bus_list_lock);
  668. usb_notify_remove_bus(bus);
  669. clear_bit (bus->busnum, busmap.busmap);
  670. class_device_unregister(bus->class_dev);
  671. }
  672. /**
  673. * register_root_hub - called by usb_add_hcd() to register a root hub
  674. * @hcd: host controller for this root hub
  675. *
  676. * This function registers the root hub with the USB subsystem. It sets up
  677. * the device properly in the device tree and then calls usb_new_device()
  678. * to register the usb device. It also assigns the root hub's USB address
  679. * (always 1).
  680. */
  681. static int register_root_hub(struct usb_hcd *hcd)
  682. {
  683. struct device *parent_dev = hcd->self.controller;
  684. struct usb_device *usb_dev = hcd->self.root_hub;
  685. const int devnum = 1;
  686. int retval;
  687. usb_dev->devnum = devnum;
  688. usb_dev->bus->devnum_next = devnum + 1;
  689. memset (&usb_dev->bus->devmap.devicemap, 0,
  690. sizeof usb_dev->bus->devmap.devicemap);
  691. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  692. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  693. mutex_lock(&usb_bus_list_lock);
  694. usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  695. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  696. if (retval != sizeof usb_dev->descriptor) {
  697. mutex_unlock(&usb_bus_list_lock);
  698. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  699. usb_dev->dev.bus_id, retval);
  700. return (retval < 0) ? retval : -EMSGSIZE;
  701. }
  702. retval = usb_new_device (usb_dev);
  703. if (retval) {
  704. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  705. usb_dev->dev.bus_id, retval);
  706. }
  707. mutex_unlock(&usb_bus_list_lock);
  708. if (retval == 0) {
  709. spin_lock_irq (&hcd_root_hub_lock);
  710. hcd->rh_registered = 1;
  711. spin_unlock_irq (&hcd_root_hub_lock);
  712. /* Did the HC die before the root hub was registered? */
  713. if (hcd->state == HC_STATE_HALT)
  714. usb_hc_died (hcd); /* This time clean up */
  715. }
  716. return retval;
  717. }
  718. void usb_enable_root_hub_irq (struct usb_bus *bus)
  719. {
  720. struct usb_hcd *hcd;
  721. hcd = container_of (bus, struct usb_hcd, self);
  722. if (hcd->driver->hub_irq_enable && hcd->state != HC_STATE_HALT)
  723. hcd->driver->hub_irq_enable (hcd);
  724. }
  725. /*-------------------------------------------------------------------------*/
  726. /**
  727. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  728. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  729. * @is_input: true iff the transaction sends data to the host
  730. * @isoc: true for isochronous transactions, false for interrupt ones
  731. * @bytecount: how many bytes in the transaction.
  732. *
  733. * Returns approximate bus time in nanoseconds for a periodic transaction.
  734. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  735. * scheduled in software, this function is only used for such scheduling.
  736. */
  737. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  738. {
  739. unsigned long tmp;
  740. switch (speed) {
  741. case USB_SPEED_LOW: /* INTR only */
  742. if (is_input) {
  743. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  744. return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  745. } else {
  746. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  747. return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  748. }
  749. case USB_SPEED_FULL: /* ISOC or INTR */
  750. if (isoc) {
  751. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  752. return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
  753. } else {
  754. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  755. return (9107L + BW_HOST_DELAY + tmp);
  756. }
  757. case USB_SPEED_HIGH: /* ISOC or INTR */
  758. // FIXME adjust for input vs output
  759. if (isoc)
  760. tmp = HS_NSECS_ISO (bytecount);
  761. else
  762. tmp = HS_NSECS (bytecount);
  763. return tmp;
  764. default:
  765. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  766. return -1;
  767. }
  768. }
  769. EXPORT_SYMBOL (usb_calc_bus_time);
  770. /*
  771. * usb_check_bandwidth():
  772. *
  773. * old_alloc is from host_controller->bandwidth_allocated in microseconds;
  774. * bustime is from calc_bus_time(), but converted to microseconds.
  775. *
  776. * returns <bustime in us> if successful,
  777. * or -ENOSPC if bandwidth request fails.
  778. *
  779. * FIXME:
  780. * This initial implementation does not use Endpoint.bInterval
  781. * in managing bandwidth allocation.
  782. * It probably needs to be expanded to use Endpoint.bInterval.
  783. * This can be done as a later enhancement (correction).
  784. *
  785. * This will also probably require some kind of
  786. * frame allocation tracking...meaning, for example,
  787. * that if multiple drivers request interrupts every 10 USB frames,
  788. * they don't all have to be allocated at
  789. * frame numbers N, N+10, N+20, etc. Some of them could be at
  790. * N+11, N+21, N+31, etc., and others at
  791. * N+12, N+22, N+32, etc.
  792. *
  793. * Similarly for isochronous transfers...
  794. *
  795. * Individual HCDs can schedule more directly ... this logic
  796. * is not correct for high speed transfers.
  797. */
  798. int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
  799. {
  800. unsigned int pipe = urb->pipe;
  801. long bustime;
  802. int is_in = usb_pipein (pipe);
  803. int is_iso = usb_pipeisoc (pipe);
  804. int old_alloc = dev->bus->bandwidth_allocated;
  805. int new_alloc;
  806. bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
  807. usb_maxpacket (dev, pipe, !is_in)));
  808. if (is_iso)
  809. bustime /= urb->number_of_packets;
  810. new_alloc = old_alloc + (int) bustime;
  811. if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
  812. #ifdef DEBUG
  813. char *mode =
  814. #ifdef CONFIG_USB_BANDWIDTH
  815. "";
  816. #else
  817. "would have ";
  818. #endif
  819. dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
  820. mode, old_alloc, bustime, new_alloc);
  821. #endif
  822. #ifdef CONFIG_USB_BANDWIDTH
  823. bustime = -ENOSPC; /* report error */
  824. #endif
  825. }
  826. return bustime;
  827. }
  828. EXPORT_SYMBOL (usb_check_bandwidth);
  829. /**
  830. * usb_claim_bandwidth - records bandwidth for a periodic transfer
  831. * @dev: source/target of request
  832. * @urb: request (urb->dev == dev)
  833. * @bustime: bandwidth consumed, in (average) microseconds per frame
  834. * @isoc: true iff the request is isochronous
  835. *
  836. * Bus bandwidth reservations are recorded purely for diagnostic purposes.
  837. * HCDs are expected not to overcommit periodic bandwidth, and to record such
  838. * reservations whenever endpoints are added to the periodic schedule.
  839. *
  840. * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
  841. * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
  842. * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
  843. * large its periodic schedule is.
  844. */
  845. void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
  846. {
  847. dev->bus->bandwidth_allocated += bustime;
  848. if (isoc)
  849. dev->bus->bandwidth_isoc_reqs++;
  850. else
  851. dev->bus->bandwidth_int_reqs++;
  852. urb->bandwidth = bustime;
  853. #ifdef USB_BANDWIDTH_MESSAGES
  854. dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
  855. bustime,
  856. isoc ? "ISOC" : "INTR",
  857. dev->bus->bandwidth_allocated,
  858. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  859. #endif
  860. }
  861. EXPORT_SYMBOL (usb_claim_bandwidth);
  862. /**
  863. * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
  864. * @dev: source/target of request
  865. * @urb: request (urb->dev == dev)
  866. * @isoc: true iff the request is isochronous
  867. *
  868. * This records that previously allocated bandwidth has been released.
  869. * Bandwidth is released when endpoints are removed from the host controller's
  870. * periodic schedule.
  871. */
  872. void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
  873. {
  874. dev->bus->bandwidth_allocated -= urb->bandwidth;
  875. if (isoc)
  876. dev->bus->bandwidth_isoc_reqs--;
  877. else
  878. dev->bus->bandwidth_int_reqs--;
  879. #ifdef USB_BANDWIDTH_MESSAGES
  880. dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
  881. urb->bandwidth,
  882. isoc ? "ISOC" : "INTR",
  883. dev->bus->bandwidth_allocated,
  884. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  885. #endif
  886. urb->bandwidth = 0;
  887. }
  888. EXPORT_SYMBOL (usb_release_bandwidth);
  889. /*-------------------------------------------------------------------------*/
  890. /*
  891. * Generic HC operations.
  892. */
  893. /*-------------------------------------------------------------------------*/
  894. static void urb_unlink (struct urb *urb)
  895. {
  896. unsigned long flags;
  897. /* Release any periodic transfer bandwidth */
  898. if (urb->bandwidth)
  899. usb_release_bandwidth (urb->dev, urb,
  900. usb_pipeisoc (urb->pipe));
  901. /* clear all state linking urb to this dev (and hcd) */
  902. spin_lock_irqsave (&hcd_data_lock, flags);
  903. list_del_init (&urb->urb_list);
  904. spin_unlock_irqrestore (&hcd_data_lock, flags);
  905. }
  906. /* may be called in any context with a valid urb->dev usecount
  907. * caller surrenders "ownership" of urb
  908. * expects usb_submit_urb() to have sanity checked and conditioned all
  909. * inputs in the urb
  910. */
  911. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  912. {
  913. int status;
  914. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  915. struct usb_host_endpoint *ep;
  916. unsigned long flags;
  917. if (!hcd)
  918. return -ENODEV;
  919. usbmon_urb_submit(&hcd->self, urb);
  920. /*
  921. * Atomically queue the urb, first to our records, then to the HCD.
  922. * Access to urb->status is controlled by urb->lock ... changes on
  923. * i/o completion (normal or fault) or unlinking.
  924. */
  925. // FIXME: verify that quiescing hc works right (RH cleans up)
  926. spin_lock_irqsave (&hcd_data_lock, flags);
  927. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  928. [usb_pipeendpoint(urb->pipe)];
  929. if (unlikely (!ep))
  930. status = -ENOENT;
  931. else if (unlikely (urb->reject))
  932. status = -EPERM;
  933. else switch (hcd->state) {
  934. case HC_STATE_RUNNING:
  935. case HC_STATE_RESUMING:
  936. doit:
  937. list_add_tail (&urb->urb_list, &ep->urb_list);
  938. status = 0;
  939. break;
  940. case HC_STATE_SUSPENDED:
  941. /* HC upstream links (register access, wakeup signaling) can work
  942. * even when the downstream links (and DMA etc) are quiesced; let
  943. * usbcore talk to the root hub.
  944. */
  945. if (hcd->self.controller->power.power_state.event == PM_EVENT_ON
  946. && urb->dev->parent == NULL)
  947. goto doit;
  948. /* FALL THROUGH */
  949. default:
  950. status = -ESHUTDOWN;
  951. break;
  952. }
  953. spin_unlock_irqrestore (&hcd_data_lock, flags);
  954. if (status) {
  955. INIT_LIST_HEAD (&urb->urb_list);
  956. usbmon_urb_submit_error(&hcd->self, urb, status);
  957. return status;
  958. }
  959. /* increment urb's reference count as part of giving it to the HCD
  960. * (which now controls it). HCD guarantees that it either returns
  961. * an error or calls giveback(), but not both.
  962. */
  963. urb = usb_get_urb (urb);
  964. atomic_inc (&urb->use_count);
  965. if (urb->dev == hcd->self.root_hub) {
  966. /* NOTE: requirement on hub callers (usbfs and the hub
  967. * driver, for now) that URBs' urb->transfer_buffer be
  968. * valid and usb_buffer_{sync,unmap}() not be needed, since
  969. * they could clobber root hub response data.
  970. */
  971. status = rh_urb_enqueue (hcd, urb);
  972. goto done;
  973. }
  974. /* lower level hcd code should use *_dma exclusively,
  975. * unless it uses pio or talks to another transport.
  976. */
  977. if (hcd->self.uses_dma) {
  978. if (usb_pipecontrol (urb->pipe)
  979. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  980. urb->setup_dma = dma_map_single (
  981. hcd->self.controller,
  982. urb->setup_packet,
  983. sizeof (struct usb_ctrlrequest),
  984. DMA_TO_DEVICE);
  985. if (urb->transfer_buffer_length != 0
  986. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  987. urb->transfer_dma = dma_map_single (
  988. hcd->self.controller,
  989. urb->transfer_buffer,
  990. urb->transfer_buffer_length,
  991. usb_pipein (urb->pipe)
  992. ? DMA_FROM_DEVICE
  993. : DMA_TO_DEVICE);
  994. }
  995. status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags);
  996. done:
  997. if (unlikely (status)) {
  998. urb_unlink (urb);
  999. atomic_dec (&urb->use_count);
  1000. if (urb->reject)
  1001. wake_up (&usb_kill_urb_queue);
  1002. usb_put_urb (urb);
  1003. usbmon_urb_submit_error(&hcd->self, urb, status);
  1004. }
  1005. return status;
  1006. }
  1007. /*-------------------------------------------------------------------------*/
  1008. /* called in any context */
  1009. int usb_hcd_get_frame_number (struct usb_device *udev)
  1010. {
  1011. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1012. if (!HC_IS_RUNNING (hcd->state))
  1013. return -ESHUTDOWN;
  1014. return hcd->driver->get_frame_number (hcd);
  1015. }
  1016. /*-------------------------------------------------------------------------*/
  1017. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1018. * off hardware queues (which may take a while) and returning it as
  1019. * soon as practical. we've already set up the urb's return status,
  1020. * but we can't know if the callback completed already.
  1021. */
  1022. static int
  1023. unlink1 (struct usb_hcd *hcd, struct urb *urb)
  1024. {
  1025. int value;
  1026. if (urb->dev == hcd->self.root_hub)
  1027. value = usb_rh_urb_dequeue (hcd, urb);
  1028. else {
  1029. /* The only reason an HCD might fail this call is if
  1030. * it has not yet fully queued the urb to begin with.
  1031. * Such failures should be harmless. */
  1032. value = hcd->driver->urb_dequeue (hcd, urb);
  1033. }
  1034. if (value != 0)
  1035. dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
  1036. urb, value);
  1037. return value;
  1038. }
  1039. /*
  1040. * called in any context
  1041. *
  1042. * caller guarantees urb won't be recycled till both unlink()
  1043. * and the urb's completion function return
  1044. */
  1045. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1046. {
  1047. struct usb_host_endpoint *ep;
  1048. struct usb_hcd *hcd = NULL;
  1049. struct device *sys = NULL;
  1050. unsigned long flags;
  1051. struct list_head *tmp;
  1052. int retval;
  1053. if (!urb)
  1054. return -EINVAL;
  1055. if (!urb->dev || !urb->dev->bus)
  1056. return -ENODEV;
  1057. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  1058. [usb_pipeendpoint(urb->pipe)];
  1059. if (!ep)
  1060. return -ENODEV;
  1061. /*
  1062. * we contend for urb->status with the hcd core,
  1063. * which changes it while returning the urb.
  1064. *
  1065. * Caller guaranteed that the urb pointer hasn't been freed, and
  1066. * that it was submitted. But as a rule it can't know whether or
  1067. * not it's already been unlinked ... so we respect the reversed
  1068. * lock sequence needed for the usb_hcd_giveback_urb() code paths
  1069. * (urb lock, then hcd_data_lock) in case some other CPU is now
  1070. * unlinking it.
  1071. */
  1072. spin_lock_irqsave (&urb->lock, flags);
  1073. spin_lock (&hcd_data_lock);
  1074. sys = &urb->dev->dev;
  1075. hcd = bus_to_hcd(urb->dev->bus);
  1076. if (hcd == NULL) {
  1077. retval = -ENODEV;
  1078. goto done;
  1079. }
  1080. /* insist the urb is still queued */
  1081. list_for_each(tmp, &ep->urb_list) {
  1082. if (tmp == &urb->urb_list)
  1083. break;
  1084. }
  1085. if (tmp != &urb->urb_list) {
  1086. retval = -EIDRM;
  1087. goto done;
  1088. }
  1089. /* Any status except -EINPROGRESS means something already started to
  1090. * unlink this URB from the hardware. So there's no more work to do.
  1091. */
  1092. if (urb->status != -EINPROGRESS) {
  1093. retval = -EBUSY;
  1094. goto done;
  1095. }
  1096. /* IRQ setup can easily be broken so that USB controllers
  1097. * never get completion IRQs ... maybe even the ones we need to
  1098. * finish unlinking the initial failed usb_set_address()
  1099. * or device descriptor fetch.
  1100. */
  1101. if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags)
  1102. && hcd->self.root_hub != urb->dev) {
  1103. dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
  1104. "Controller is probably using the wrong IRQ."
  1105. "\n");
  1106. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1107. }
  1108. urb->status = status;
  1109. spin_unlock (&hcd_data_lock);
  1110. spin_unlock_irqrestore (&urb->lock, flags);
  1111. retval = unlink1 (hcd, urb);
  1112. if (retval == 0)
  1113. retval = -EINPROGRESS;
  1114. return retval;
  1115. done:
  1116. spin_unlock (&hcd_data_lock);
  1117. spin_unlock_irqrestore (&urb->lock, flags);
  1118. if (retval != -EIDRM && sys && sys->driver)
  1119. dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
  1120. return retval;
  1121. }
  1122. /*-------------------------------------------------------------------------*/
  1123. /* disables the endpoint: cancels any pending urbs, then synchronizes with
  1124. * the hcd to make sure all endpoint state is gone from hardware, and then
  1125. * waits until the endpoint's queue is completely drained. use for
  1126. * set_configuration, set_interface, driver removal, physical disconnect.
  1127. *
  1128. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1129. * type, maxpacket size, toggle, halt status, and scheduling.
  1130. */
  1131. void usb_hcd_endpoint_disable (struct usb_device *udev,
  1132. struct usb_host_endpoint *ep)
  1133. {
  1134. struct usb_hcd *hcd;
  1135. struct urb *urb;
  1136. hcd = bus_to_hcd(udev->bus);
  1137. WARN_ON (!HC_IS_RUNNING (hcd->state) && hcd->state != HC_STATE_HALT &&
  1138. udev->state != USB_STATE_NOTATTACHED);
  1139. local_irq_disable ();
  1140. /* ep is already gone from udev->ep_{in,out}[]; no more submits */
  1141. rescan:
  1142. spin_lock (&hcd_data_lock);
  1143. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1144. int tmp;
  1145. /* the urb may already have been unlinked */
  1146. if (urb->status != -EINPROGRESS)
  1147. continue;
  1148. usb_get_urb (urb);
  1149. spin_unlock (&hcd_data_lock);
  1150. spin_lock (&urb->lock);
  1151. tmp = urb->status;
  1152. if (tmp == -EINPROGRESS)
  1153. urb->status = -ESHUTDOWN;
  1154. spin_unlock (&urb->lock);
  1155. /* kick hcd unless it's already returning this */
  1156. if (tmp == -EINPROGRESS) {
  1157. tmp = urb->pipe;
  1158. unlink1 (hcd, urb);
  1159. dev_dbg (hcd->self.controller,
  1160. "shutdown urb %p pipe %08x ep%d%s%s\n",
  1161. urb, tmp, usb_pipeendpoint (tmp),
  1162. (tmp & USB_DIR_IN) ? "in" : "out",
  1163. ({ char *s; \
  1164. switch (usb_pipetype (tmp)) { \
  1165. case PIPE_CONTROL: s = ""; break; \
  1166. case PIPE_BULK: s = "-bulk"; break; \
  1167. case PIPE_INTERRUPT: s = "-intr"; break; \
  1168. default: s = "-iso"; break; \
  1169. }; s;}));
  1170. }
  1171. usb_put_urb (urb);
  1172. /* list contents may have changed */
  1173. goto rescan;
  1174. }
  1175. spin_unlock (&hcd_data_lock);
  1176. local_irq_enable ();
  1177. /* synchronize with the hardware, so old configuration state
  1178. * clears out immediately (and will be freed).
  1179. */
  1180. might_sleep ();
  1181. if (hcd->driver->endpoint_disable)
  1182. hcd->driver->endpoint_disable (hcd, ep);
  1183. /* Wait until the endpoint queue is completely empty. Most HCDs
  1184. * will have done this already in their endpoint_disable method,
  1185. * but some might not. And there could be root-hub control URBs
  1186. * still pending since they aren't affected by the HCDs'
  1187. * endpoint_disable methods.
  1188. */
  1189. while (!list_empty (&ep->urb_list)) {
  1190. spin_lock_irq (&hcd_data_lock);
  1191. /* The list may have changed while we acquired the spinlock */
  1192. urb = NULL;
  1193. if (!list_empty (&ep->urb_list)) {
  1194. urb = list_entry (ep->urb_list.prev, struct urb,
  1195. urb_list);
  1196. usb_get_urb (urb);
  1197. }
  1198. spin_unlock_irq (&hcd_data_lock);
  1199. if (urb) {
  1200. usb_kill_urb (urb);
  1201. usb_put_urb (urb);
  1202. }
  1203. }
  1204. }
  1205. /*-------------------------------------------------------------------------*/
  1206. #ifdef CONFIG_PM
  1207. int hcd_bus_suspend (struct usb_bus *bus)
  1208. {
  1209. struct usb_hcd *hcd;
  1210. int status;
  1211. hcd = container_of (bus, struct usb_hcd, self);
  1212. if (!hcd->driver->bus_suspend)
  1213. return -ENOENT;
  1214. hcd->state = HC_STATE_QUIESCING;
  1215. status = hcd->driver->bus_suspend (hcd);
  1216. if (status == 0)
  1217. hcd->state = HC_STATE_SUSPENDED;
  1218. else
  1219. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1220. "suspend", status);
  1221. return status;
  1222. }
  1223. int hcd_bus_resume (struct usb_bus *bus)
  1224. {
  1225. struct usb_hcd *hcd;
  1226. int status;
  1227. hcd = container_of (bus, struct usb_hcd, self);
  1228. if (!hcd->driver->bus_resume)
  1229. return -ENOENT;
  1230. if (hcd->state == HC_STATE_RUNNING)
  1231. return 0;
  1232. hcd->state = HC_STATE_RESUMING;
  1233. status = hcd->driver->bus_resume (hcd);
  1234. if (status == 0)
  1235. hcd->state = HC_STATE_RUNNING;
  1236. else {
  1237. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1238. "resume", status);
  1239. usb_hc_died(hcd);
  1240. }
  1241. return status;
  1242. }
  1243. /**
  1244. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1245. * @hcd: host controller for this root hub
  1246. *
  1247. * The USB host controller calls this function when its root hub is
  1248. * suspended (with the remote wakeup feature enabled) and a remote
  1249. * wakeup request is received. It queues a request for khubd to
  1250. * resume the root hub (that is, manage its downstream ports again).
  1251. */
  1252. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1253. {
  1254. unsigned long flags;
  1255. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1256. if (hcd->rh_registered)
  1257. usb_resume_root_hub (hcd->self.root_hub);
  1258. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1259. }
  1260. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1261. #endif
  1262. /*-------------------------------------------------------------------------*/
  1263. #ifdef CONFIG_USB_OTG
  1264. /**
  1265. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1266. * @bus: the bus (must use hcd framework)
  1267. * @port_num: 1-based number of port; usually bus->otg_port
  1268. * Context: in_interrupt()
  1269. *
  1270. * Starts enumeration, with an immediate reset followed later by
  1271. * khubd identifying and possibly configuring the device.
  1272. * This is needed by OTG controller drivers, where it helps meet
  1273. * HNP protocol timing requirements for starting a port reset.
  1274. */
  1275. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1276. {
  1277. struct usb_hcd *hcd;
  1278. int status = -EOPNOTSUPP;
  1279. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1280. * boards with root hubs hooked up to internal devices (instead of
  1281. * just the OTG port) may need more attention to resetting...
  1282. */
  1283. hcd = container_of (bus, struct usb_hcd, self);
  1284. if (port_num && hcd->driver->start_port_reset)
  1285. status = hcd->driver->start_port_reset(hcd, port_num);
  1286. /* run khubd shortly after (first) root port reset finishes;
  1287. * it may issue others, until at least 50 msecs have passed.
  1288. */
  1289. if (status == 0)
  1290. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1291. return status;
  1292. }
  1293. EXPORT_SYMBOL (usb_bus_start_enum);
  1294. #endif
  1295. /*-------------------------------------------------------------------------*/
  1296. /**
  1297. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1298. * @hcd: host controller returning the URB
  1299. * @urb: urb being returned to the USB device driver.
  1300. * Context: in_interrupt()
  1301. *
  1302. * This hands the URB from HCD to its USB device driver, using its
  1303. * completion function. The HCD has freed all per-urb resources
  1304. * (and is done using urb->hcpriv). It also released all HCD locks;
  1305. * the device driver won't cause problems if it frees, modifies,
  1306. * or resubmits this URB.
  1307. */
  1308. void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb)
  1309. {
  1310. int at_root_hub;
  1311. at_root_hub = (urb->dev == hcd->self.root_hub);
  1312. urb_unlink (urb);
  1313. /* lower level hcd code should use *_dma exclusively if the
  1314. * host controller does DMA */
  1315. if (hcd->self.uses_dma && !at_root_hub) {
  1316. if (usb_pipecontrol (urb->pipe)
  1317. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1318. dma_unmap_single (hcd->self.controller, urb->setup_dma,
  1319. sizeof (struct usb_ctrlrequest),
  1320. DMA_TO_DEVICE);
  1321. if (urb->transfer_buffer_length != 0
  1322. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1323. dma_unmap_single (hcd->self.controller,
  1324. urb->transfer_dma,
  1325. urb->transfer_buffer_length,
  1326. usb_pipein (urb->pipe)
  1327. ? DMA_FROM_DEVICE
  1328. : DMA_TO_DEVICE);
  1329. }
  1330. usbmon_urb_complete (&hcd->self, urb);
  1331. /* pass ownership to the completion handler */
  1332. urb->complete (urb);
  1333. atomic_dec (&urb->use_count);
  1334. if (unlikely (urb->reject))
  1335. wake_up (&usb_kill_urb_queue);
  1336. usb_put_urb (urb);
  1337. }
  1338. EXPORT_SYMBOL (usb_hcd_giveback_urb);
  1339. /*-------------------------------------------------------------------------*/
  1340. /**
  1341. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1342. * @irq: the IRQ being raised
  1343. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1344. * @r: saved hardware registers
  1345. *
  1346. * If the controller isn't HALTed, calls the driver's irq handler.
  1347. * Checks whether the controller is now dead.
  1348. */
  1349. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  1350. {
  1351. struct usb_hcd *hcd = __hcd;
  1352. int start = hcd->state;
  1353. if (unlikely(start == HC_STATE_HALT ||
  1354. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
  1355. return IRQ_NONE;
  1356. if (hcd->driver->irq (hcd) == IRQ_NONE)
  1357. return IRQ_NONE;
  1358. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1359. if (unlikely(hcd->state == HC_STATE_HALT))
  1360. usb_hc_died (hcd);
  1361. return IRQ_HANDLED;
  1362. }
  1363. /*-------------------------------------------------------------------------*/
  1364. /**
  1365. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1366. * @hcd: pointer to the HCD representing the controller
  1367. *
  1368. * This is called by bus glue to report a USB host controller that died
  1369. * while operations may still have been pending. It's called automatically
  1370. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1371. */
  1372. void usb_hc_died (struct usb_hcd *hcd)
  1373. {
  1374. unsigned long flags;
  1375. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1376. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1377. if (hcd->rh_registered) {
  1378. hcd->poll_rh = 0;
  1379. /* make khubd clean up old urbs and devices */
  1380. usb_set_device_state (hcd->self.root_hub,
  1381. USB_STATE_NOTATTACHED);
  1382. usb_kick_khubd (hcd->self.root_hub);
  1383. }
  1384. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1385. }
  1386. EXPORT_SYMBOL_GPL (usb_hc_died);
  1387. /*-------------------------------------------------------------------------*/
  1388. /**
  1389. * usb_create_hcd - create and initialize an HCD structure
  1390. * @driver: HC driver that will use this hcd
  1391. * @dev: device for this HC, stored in hcd->self.controller
  1392. * @bus_name: value to store in hcd->self.bus_name
  1393. * Context: !in_interrupt()
  1394. *
  1395. * Allocate a struct usb_hcd, with extra space at the end for the
  1396. * HC driver's private data. Initialize the generic members of the
  1397. * hcd structure.
  1398. *
  1399. * If memory is unavailable, returns NULL.
  1400. */
  1401. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1402. struct device *dev, char *bus_name)
  1403. {
  1404. struct usb_hcd *hcd;
  1405. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1406. if (!hcd) {
  1407. dev_dbg (dev, "hcd alloc failed\n");
  1408. return NULL;
  1409. }
  1410. dev_set_drvdata(dev, hcd);
  1411. kref_init(&hcd->kref);
  1412. usb_bus_init(&hcd->self);
  1413. hcd->self.controller = dev;
  1414. hcd->self.bus_name = bus_name;
  1415. hcd->self.uses_dma = (dev->dma_mask != NULL);
  1416. init_timer(&hcd->rh_timer);
  1417. hcd->rh_timer.function = rh_timer_func;
  1418. hcd->rh_timer.data = (unsigned long) hcd;
  1419. hcd->driver = driver;
  1420. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1421. "USB Host Controller";
  1422. return hcd;
  1423. }
  1424. EXPORT_SYMBOL (usb_create_hcd);
  1425. static void hcd_release (struct kref *kref)
  1426. {
  1427. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  1428. kfree(hcd);
  1429. }
  1430. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  1431. {
  1432. if (hcd)
  1433. kref_get (&hcd->kref);
  1434. return hcd;
  1435. }
  1436. EXPORT_SYMBOL (usb_get_hcd);
  1437. void usb_put_hcd (struct usb_hcd *hcd)
  1438. {
  1439. if (hcd)
  1440. kref_put (&hcd->kref, hcd_release);
  1441. }
  1442. EXPORT_SYMBOL (usb_put_hcd);
  1443. /**
  1444. * usb_add_hcd - finish generic HCD structure initialization and register
  1445. * @hcd: the usb_hcd structure to initialize
  1446. * @irqnum: Interrupt line to allocate
  1447. * @irqflags: Interrupt type flags
  1448. *
  1449. * Finish the remaining parts of generic HCD initialization: allocate the
  1450. * buffers of consistent memory, register the bus, request the IRQ line,
  1451. * and call the driver's reset() and start() routines.
  1452. */
  1453. int usb_add_hcd(struct usb_hcd *hcd,
  1454. unsigned int irqnum, unsigned long irqflags)
  1455. {
  1456. int retval;
  1457. struct usb_device *rhdev;
  1458. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1459. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1460. /* HC is in reset state, but accessible. Now do the one-time init,
  1461. * bottom up so that hcds can customize the root hubs before khubd
  1462. * starts talking to them. (Note, bus id is assigned early too.)
  1463. */
  1464. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1465. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1466. return retval;
  1467. }
  1468. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1469. goto err_register_bus;
  1470. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1471. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1472. retval = -ENOMEM;
  1473. goto err_allocate_root_hub;
  1474. }
  1475. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1476. USB_SPEED_FULL;
  1477. hcd->self.root_hub = rhdev;
  1478. /* wakeup flag init defaults to "everything works" for root hubs,
  1479. * but drivers can override it in reset() if needed, along with
  1480. * recording the overall controller's system wakeup capability.
  1481. */
  1482. device_init_wakeup(&rhdev->dev, 1);
  1483. /* "reset" is misnamed; its role is now one-time init. the controller
  1484. * should already have been reset (and boot firmware kicked off etc).
  1485. */
  1486. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1487. dev_err(hcd->self.controller, "can't setup\n");
  1488. goto err_hcd_driver_setup;
  1489. }
  1490. /* NOTE: root hub and controller capabilities may not be the same */
  1491. if (device_can_wakeup(hcd->self.controller)
  1492. && device_can_wakeup(&hcd->self.root_hub->dev))
  1493. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1494. /* enable irqs just before we start the controller */
  1495. if (hcd->driver->irq) {
  1496. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1497. hcd->driver->description, hcd->self.busnum);
  1498. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1499. hcd->irq_descr, hcd)) != 0) {
  1500. dev_err(hcd->self.controller,
  1501. "request interrupt %d failed\n", irqnum);
  1502. goto err_request_irq;
  1503. }
  1504. hcd->irq = irqnum;
  1505. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  1506. (hcd->driver->flags & HCD_MEMORY) ?
  1507. "io mem" : "io base",
  1508. (unsigned long long)hcd->rsrc_start);
  1509. } else {
  1510. hcd->irq = -1;
  1511. if (hcd->rsrc_start)
  1512. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1513. (hcd->driver->flags & HCD_MEMORY) ?
  1514. "io mem" : "io base",
  1515. (unsigned long long)hcd->rsrc_start);
  1516. }
  1517. if ((retval = hcd->driver->start(hcd)) < 0) {
  1518. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1519. goto err_hcd_driver_start;
  1520. }
  1521. /* starting here, usbcore will pay attention to this root hub */
  1522. rhdev->bus_mA = min(500u, hcd->power_budget);
  1523. if ((retval = register_root_hub(hcd)) != 0)
  1524. goto err_register_root_hub;
  1525. if (hcd->uses_new_polling && hcd->poll_rh)
  1526. usb_hcd_poll_rh_status(hcd);
  1527. return retval;
  1528. err_register_root_hub:
  1529. hcd->driver->stop(hcd);
  1530. err_hcd_driver_start:
  1531. if (hcd->irq >= 0)
  1532. free_irq(irqnum, hcd);
  1533. err_request_irq:
  1534. err_hcd_driver_setup:
  1535. hcd->self.root_hub = NULL;
  1536. usb_put_dev(rhdev);
  1537. err_allocate_root_hub:
  1538. usb_deregister_bus(&hcd->self);
  1539. err_register_bus:
  1540. hcd_buffer_destroy(hcd);
  1541. return retval;
  1542. }
  1543. EXPORT_SYMBOL (usb_add_hcd);
  1544. /**
  1545. * usb_remove_hcd - shutdown processing for generic HCDs
  1546. * @hcd: the usb_hcd structure to remove
  1547. * Context: !in_interrupt()
  1548. *
  1549. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1550. * invoking the HCD's stop() method.
  1551. */
  1552. void usb_remove_hcd(struct usb_hcd *hcd)
  1553. {
  1554. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1555. if (HC_IS_RUNNING (hcd->state))
  1556. hcd->state = HC_STATE_QUIESCING;
  1557. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1558. spin_lock_irq (&hcd_root_hub_lock);
  1559. hcd->rh_registered = 0;
  1560. spin_unlock_irq (&hcd_root_hub_lock);
  1561. mutex_lock(&usb_bus_list_lock);
  1562. usb_disconnect(&hcd->self.root_hub);
  1563. mutex_unlock(&usb_bus_list_lock);
  1564. hcd->poll_rh = 0;
  1565. del_timer_sync(&hcd->rh_timer);
  1566. hcd->driver->stop(hcd);
  1567. hcd->state = HC_STATE_HALT;
  1568. if (hcd->irq >= 0)
  1569. free_irq(hcd->irq, hcd);
  1570. usb_deregister_bus(&hcd->self);
  1571. hcd_buffer_destroy(hcd);
  1572. }
  1573. EXPORT_SYMBOL (usb_remove_hcd);
  1574. void
  1575. usb_hcd_platform_shutdown(struct platform_device* dev)
  1576. {
  1577. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1578. if (hcd->driver->shutdown)
  1579. hcd->driver->shutdown(hcd);
  1580. }
  1581. EXPORT_SYMBOL (usb_hcd_platform_shutdown);
  1582. /*-------------------------------------------------------------------------*/
  1583. #if defined(CONFIG_USB_MON)
  1584. struct usb_mon_operations *mon_ops;
  1585. /*
  1586. * The registration is unlocked.
  1587. * We do it this way because we do not want to lock in hot paths.
  1588. *
  1589. * Notice that the code is minimally error-proof. Because usbmon needs
  1590. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1591. */
  1592. int usb_mon_register (struct usb_mon_operations *ops)
  1593. {
  1594. if (mon_ops)
  1595. return -EBUSY;
  1596. mon_ops = ops;
  1597. mb();
  1598. return 0;
  1599. }
  1600. EXPORT_SYMBOL_GPL (usb_mon_register);
  1601. void usb_mon_deregister (void)
  1602. {
  1603. if (mon_ops == NULL) {
  1604. printk(KERN_ERR "USB: monitor was not registered\n");
  1605. return;
  1606. }
  1607. mon_ops = NULL;
  1608. mb();
  1609. }
  1610. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1611. #endif /* CONFIG_USB_MON */