hcd.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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", system_utsname.sysname,
  274. system_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. const u8 *bufp = tbuf;
  298. int len = 0;
  299. int patch_wakeup = 0;
  300. unsigned long flags;
  301. int status = 0;
  302. int n;
  303. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  304. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  305. wValue = le16_to_cpu (cmd->wValue);
  306. wIndex = le16_to_cpu (cmd->wIndex);
  307. wLength = le16_to_cpu (cmd->wLength);
  308. if (wLength > urb->transfer_buffer_length)
  309. goto error;
  310. urb->actual_length = 0;
  311. switch (typeReq) {
  312. /* DEVICE REQUESTS */
  313. /* The root hub's remote wakeup enable bit is implemented using
  314. * driver model wakeup flags. If this system supports wakeup
  315. * through USB, userspace may change the default "allow wakeup"
  316. * policy through sysfs or these calls.
  317. *
  318. * Most root hubs support wakeup from downstream devices, for
  319. * runtime power management (disabling USB clocks and reducing
  320. * VBUS power usage). However, not all of them do so; silicon,
  321. * board, and BIOS bugs here are not uncommon, so these can't
  322. * be treated quite like external hubs.
  323. *
  324. * Likewise, not all root hubs will pass wakeup events upstream,
  325. * to wake up the whole system. So don't assume root hub and
  326. * controller capabilities are identical.
  327. */
  328. case DeviceRequest | USB_REQ_GET_STATUS:
  329. tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
  330. << USB_DEVICE_REMOTE_WAKEUP)
  331. | (1 << USB_DEVICE_SELF_POWERED);
  332. tbuf [1] = 0;
  333. len = 2;
  334. break;
  335. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  336. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  337. device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
  338. else
  339. goto error;
  340. break;
  341. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  342. if (device_can_wakeup(&hcd->self.root_hub->dev)
  343. && wValue == USB_DEVICE_REMOTE_WAKEUP)
  344. device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
  345. else
  346. goto error;
  347. break;
  348. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  349. tbuf [0] = 1;
  350. len = 1;
  351. /* FALLTHROUGH */
  352. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  353. break;
  354. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  355. switch (wValue & 0xff00) {
  356. case USB_DT_DEVICE << 8:
  357. if (hcd->driver->flags & HCD_USB2)
  358. bufp = usb2_rh_dev_descriptor;
  359. else if (hcd->driver->flags & HCD_USB11)
  360. bufp = usb11_rh_dev_descriptor;
  361. else
  362. goto error;
  363. len = 18;
  364. break;
  365. case USB_DT_CONFIG << 8:
  366. if (hcd->driver->flags & HCD_USB2) {
  367. bufp = hs_rh_config_descriptor;
  368. len = sizeof hs_rh_config_descriptor;
  369. } else {
  370. bufp = fs_rh_config_descriptor;
  371. len = sizeof fs_rh_config_descriptor;
  372. }
  373. if (device_can_wakeup(&hcd->self.root_hub->dev))
  374. patch_wakeup = 1;
  375. break;
  376. case USB_DT_STRING << 8:
  377. n = rh_string (wValue & 0xff, hcd, ubuf, wLength);
  378. if (n < 0)
  379. goto error;
  380. urb->actual_length = n;
  381. break;
  382. default:
  383. goto error;
  384. }
  385. break;
  386. case DeviceRequest | USB_REQ_GET_INTERFACE:
  387. tbuf [0] = 0;
  388. len = 1;
  389. /* FALLTHROUGH */
  390. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  391. break;
  392. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  393. // wValue == urb->dev->devaddr
  394. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  395. wValue);
  396. break;
  397. /* INTERFACE REQUESTS (no defined feature/status flags) */
  398. /* ENDPOINT REQUESTS */
  399. case EndpointRequest | USB_REQ_GET_STATUS:
  400. // ENDPOINT_HALT flag
  401. tbuf [0] = 0;
  402. tbuf [1] = 0;
  403. len = 2;
  404. /* FALLTHROUGH */
  405. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  406. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  407. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  408. break;
  409. /* CLASS REQUESTS (and errors) */
  410. default:
  411. /* non-generic request */
  412. switch (typeReq) {
  413. case GetHubStatus:
  414. case GetPortStatus:
  415. len = 4;
  416. break;
  417. case GetHubDescriptor:
  418. len = sizeof (struct usb_hub_descriptor);
  419. break;
  420. }
  421. status = hcd->driver->hub_control (hcd,
  422. typeReq, wValue, wIndex,
  423. tbuf, wLength);
  424. break;
  425. error:
  426. /* "protocol stall" on error */
  427. status = -EPIPE;
  428. }
  429. if (status) {
  430. len = 0;
  431. if (status != -EPIPE) {
  432. dev_dbg (hcd->self.controller,
  433. "CTRL: TypeReq=0x%x val=0x%x "
  434. "idx=0x%x len=%d ==> %d\n",
  435. typeReq, wValue, wIndex,
  436. wLength, status);
  437. }
  438. }
  439. if (len) {
  440. if (urb->transfer_buffer_length < len)
  441. len = urb->transfer_buffer_length;
  442. urb->actual_length = len;
  443. // always USB_DIR_IN, toward host
  444. memcpy (ubuf, bufp, len);
  445. /* report whether RH hardware supports remote wakeup */
  446. if (patch_wakeup &&
  447. len > offsetof (struct usb_config_descriptor,
  448. bmAttributes))
  449. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  450. |= USB_CONFIG_ATT_WAKEUP;
  451. }
  452. /* any errors get returned through the urb completion */
  453. local_irq_save (flags);
  454. spin_lock (&urb->lock);
  455. if (urb->status == -EINPROGRESS)
  456. urb->status = status;
  457. spin_unlock (&urb->lock);
  458. usb_hcd_giveback_urb (hcd, urb, NULL);
  459. local_irq_restore (flags);
  460. return 0;
  461. }
  462. /*-------------------------------------------------------------------------*/
  463. /*
  464. * Root Hub interrupt transfers are polled using a timer if the
  465. * driver requests it; otherwise the driver is responsible for
  466. * calling usb_hcd_poll_rh_status() when an event occurs.
  467. *
  468. * Completions are called in_interrupt(), but they may or may not
  469. * be in_irq().
  470. */
  471. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  472. {
  473. struct urb *urb;
  474. int length;
  475. unsigned long flags;
  476. char buffer[4]; /* Any root hubs with > 31 ports? */
  477. if (!hcd->uses_new_polling && !hcd->status_urb)
  478. return;
  479. length = hcd->driver->hub_status_data(hcd, buffer);
  480. if (length > 0) {
  481. /* try to complete the status urb */
  482. local_irq_save (flags);
  483. spin_lock(&hcd_root_hub_lock);
  484. urb = hcd->status_urb;
  485. if (urb) {
  486. spin_lock(&urb->lock);
  487. if (urb->status == -EINPROGRESS) {
  488. hcd->poll_pending = 0;
  489. hcd->status_urb = NULL;
  490. urb->status = 0;
  491. urb->hcpriv = NULL;
  492. urb->actual_length = length;
  493. memcpy(urb->transfer_buffer, buffer, length);
  494. } else /* urb has been unlinked */
  495. length = 0;
  496. spin_unlock(&urb->lock);
  497. } else
  498. length = 0;
  499. spin_unlock(&hcd_root_hub_lock);
  500. /* local irqs are always blocked in completions */
  501. if (length > 0)
  502. usb_hcd_giveback_urb (hcd, urb, NULL);
  503. else
  504. hcd->poll_pending = 1;
  505. local_irq_restore (flags);
  506. }
  507. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  508. * exceed that limit if HZ is 100. */
  509. if (hcd->uses_new_polling ? hcd->poll_rh :
  510. (length == 0 && hcd->status_urb != NULL))
  511. mod_timer (&hcd->rh_timer, jiffies + msecs_to_jiffies(250));
  512. }
  513. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  514. /* timer callback */
  515. static void rh_timer_func (unsigned long _hcd)
  516. {
  517. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  518. }
  519. /*-------------------------------------------------------------------------*/
  520. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  521. {
  522. int retval;
  523. unsigned long flags;
  524. int len = 1 + (urb->dev->maxchild / 8);
  525. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  526. if (urb->status != -EINPROGRESS) /* already unlinked */
  527. retval = urb->status;
  528. else if (hcd->status_urb || urb->transfer_buffer_length < len) {
  529. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  530. retval = -EINVAL;
  531. } else {
  532. hcd->status_urb = urb;
  533. urb->hcpriv = hcd; /* indicate it's queued */
  534. if (!hcd->uses_new_polling)
  535. mod_timer (&hcd->rh_timer, jiffies +
  536. msecs_to_jiffies(250));
  537. /* If a status change has already occurred, report it ASAP */
  538. else if (hcd->poll_pending)
  539. mod_timer (&hcd->rh_timer, jiffies);
  540. retval = 0;
  541. }
  542. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  543. return retval;
  544. }
  545. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  546. {
  547. if (usb_pipeint (urb->pipe))
  548. return rh_queue_status (hcd, urb);
  549. if (usb_pipecontrol (urb->pipe))
  550. return rh_call_control (hcd, urb);
  551. return -EINVAL;
  552. }
  553. /*-------------------------------------------------------------------------*/
  554. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  555. * since these URBs always execute synchronously.
  556. */
  557. static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  558. {
  559. unsigned long flags;
  560. if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */
  561. ; /* Do nothing */
  562. } else { /* Status URB */
  563. if (!hcd->uses_new_polling)
  564. del_timer (&hcd->rh_timer);
  565. local_irq_save (flags);
  566. spin_lock (&hcd_root_hub_lock);
  567. if (urb == hcd->status_urb) {
  568. hcd->status_urb = NULL;
  569. urb->hcpriv = NULL;
  570. } else
  571. urb = NULL; /* wasn't fully queued */
  572. spin_unlock (&hcd_root_hub_lock);
  573. if (urb)
  574. usb_hcd_giveback_urb (hcd, urb, NULL);
  575. local_irq_restore (flags);
  576. }
  577. return 0;
  578. }
  579. /*-------------------------------------------------------------------------*/
  580. static struct class *usb_host_class;
  581. int usb_host_init(void)
  582. {
  583. int retval = 0;
  584. usb_host_class = class_create(THIS_MODULE, "usb_host");
  585. if (IS_ERR(usb_host_class))
  586. retval = PTR_ERR(usb_host_class);
  587. return retval;
  588. }
  589. void usb_host_cleanup(void)
  590. {
  591. class_destroy(usb_host_class);
  592. }
  593. /**
  594. * usb_bus_init - shared initialization code
  595. * @bus: the bus structure being initialized
  596. *
  597. * This code is used to initialize a usb_bus structure, memory for which is
  598. * separately managed.
  599. */
  600. static void usb_bus_init (struct usb_bus *bus)
  601. {
  602. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  603. bus->devnum_next = 1;
  604. bus->root_hub = NULL;
  605. bus->busnum = -1;
  606. bus->bandwidth_allocated = 0;
  607. bus->bandwidth_int_reqs = 0;
  608. bus->bandwidth_isoc_reqs = 0;
  609. INIT_LIST_HEAD (&bus->bus_list);
  610. }
  611. /*-------------------------------------------------------------------------*/
  612. /**
  613. * usb_register_bus - registers the USB host controller with the usb core
  614. * @bus: pointer to the bus to register
  615. * Context: !in_interrupt()
  616. *
  617. * Assigns a bus number, and links the controller into usbcore data
  618. * structures so that it can be seen by scanning the bus list.
  619. */
  620. static int usb_register_bus(struct usb_bus *bus)
  621. {
  622. int busnum;
  623. mutex_lock(&usb_bus_list_lock);
  624. busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  625. if (busnum < USB_MAXBUS) {
  626. set_bit (busnum, busmap.busmap);
  627. bus->busnum = busnum;
  628. } else {
  629. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  630. mutex_unlock(&usb_bus_list_lock);
  631. return -E2BIG;
  632. }
  633. bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0),
  634. bus->controller, "usb_host%d", busnum);
  635. if (IS_ERR(bus->class_dev)) {
  636. clear_bit(busnum, busmap.busmap);
  637. mutex_unlock(&usb_bus_list_lock);
  638. return PTR_ERR(bus->class_dev);
  639. }
  640. class_set_devdata(bus->class_dev, bus);
  641. /* Add it to the local list of buses */
  642. list_add (&bus->bus_list, &usb_bus_list);
  643. mutex_unlock(&usb_bus_list_lock);
  644. usb_notify_add_bus(bus);
  645. dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
  646. return 0;
  647. }
  648. /**
  649. * usb_deregister_bus - deregisters the USB host controller
  650. * @bus: pointer to the bus to deregister
  651. * Context: !in_interrupt()
  652. *
  653. * Recycles the bus number, and unlinks the controller from usbcore data
  654. * structures so that it won't be seen by scanning the bus list.
  655. */
  656. static void usb_deregister_bus (struct usb_bus *bus)
  657. {
  658. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  659. /*
  660. * NOTE: make sure that all the devices are removed by the
  661. * controller code, as well as having it call this when cleaning
  662. * itself up
  663. */
  664. mutex_lock(&usb_bus_list_lock);
  665. list_del (&bus->bus_list);
  666. mutex_unlock(&usb_bus_list_lock);
  667. usb_notify_remove_bus(bus);
  668. clear_bit (bus->busnum, busmap.busmap);
  669. class_device_unregister(bus->class_dev);
  670. }
  671. /**
  672. * register_root_hub - called by usb_add_hcd() to register a root hub
  673. * @hcd: host controller for this root hub
  674. *
  675. * This function registers the root hub with the USB subsystem. It sets up
  676. * the device properly in the device tree and then calls usb_new_device()
  677. * to register the usb device. It also assigns the root hub's USB address
  678. * (always 1).
  679. */
  680. static int register_root_hub(struct usb_hcd *hcd)
  681. {
  682. struct device *parent_dev = hcd->self.controller;
  683. struct usb_device *usb_dev = hcd->self.root_hub;
  684. const int devnum = 1;
  685. int retval;
  686. usb_dev->devnum = devnum;
  687. usb_dev->bus->devnum_next = devnum + 1;
  688. memset (&usb_dev->bus->devmap.devicemap, 0,
  689. sizeof usb_dev->bus->devmap.devicemap);
  690. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  691. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  692. mutex_lock(&usb_bus_list_lock);
  693. usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  694. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  695. if (retval != sizeof usb_dev->descriptor) {
  696. mutex_unlock(&usb_bus_list_lock);
  697. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  698. usb_dev->dev.bus_id, retval);
  699. return (retval < 0) ? retval : -EMSGSIZE;
  700. }
  701. retval = usb_new_device (usb_dev);
  702. if (retval) {
  703. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  704. usb_dev->dev.bus_id, retval);
  705. }
  706. mutex_unlock(&usb_bus_list_lock);
  707. if (retval == 0) {
  708. spin_lock_irq (&hcd_root_hub_lock);
  709. hcd->rh_registered = 1;
  710. spin_unlock_irq (&hcd_root_hub_lock);
  711. /* Did the HC die before the root hub was registered? */
  712. if (hcd->state == HC_STATE_HALT)
  713. usb_hc_died (hcd); /* This time clean up */
  714. }
  715. return retval;
  716. }
  717. void usb_enable_root_hub_irq (struct usb_bus *bus)
  718. {
  719. struct usb_hcd *hcd;
  720. hcd = container_of (bus, struct usb_hcd, self);
  721. if (hcd->driver->hub_irq_enable && !hcd->poll_rh &&
  722. 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_suspend_root_hub - HCD autosuspends downstream ports
  1245. * @hcd: host controller for this root hub
  1246. *
  1247. * This call arranges that usb_hcd_resume_root_hub() is safe to call later;
  1248. * that the HCD's root hub polling is deactivated; and that the root's hub
  1249. * driver is suspended. HCDs may call this to autosuspend when their root
  1250. * hub's downstream ports are all inactive: unpowered, disconnected,
  1251. * disabled, or suspended.
  1252. *
  1253. * The HCD will autoresume on device connect change detection (using SRP
  1254. * or a D+/D- pullup). The HCD also autoresumes on remote wakeup signaling
  1255. * from any ports that are suspended (if that is enabled). In most cases,
  1256. * overcurrent signaling (on powered ports) will also start autoresume.
  1257. *
  1258. * Always called with IRQs blocked.
  1259. */
  1260. void usb_hcd_suspend_root_hub (struct usb_hcd *hcd)
  1261. {
  1262. struct urb *urb;
  1263. spin_lock (&hcd_root_hub_lock);
  1264. usb_suspend_root_hub (hcd->self.root_hub);
  1265. /* force status urb to complete/unlink while suspended */
  1266. if (hcd->status_urb) {
  1267. urb = hcd->status_urb;
  1268. urb->status = -ECONNRESET;
  1269. urb->hcpriv = NULL;
  1270. urb->actual_length = 0;
  1271. del_timer (&hcd->rh_timer);
  1272. hcd->poll_pending = 0;
  1273. hcd->status_urb = NULL;
  1274. } else
  1275. urb = NULL;
  1276. spin_unlock (&hcd_root_hub_lock);
  1277. hcd->state = HC_STATE_SUSPENDED;
  1278. if (urb)
  1279. usb_hcd_giveback_urb (hcd, urb, NULL);
  1280. }
  1281. EXPORT_SYMBOL_GPL(usb_hcd_suspend_root_hub);
  1282. /**
  1283. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1284. * @hcd: host controller for this root hub
  1285. *
  1286. * The USB host controller calls this function when its root hub is
  1287. * suspended (with the remote wakeup feature enabled) and a remote
  1288. * wakeup request is received. It queues a request for khubd to
  1289. * resume the root hub (that is, manage its downstream ports again).
  1290. */
  1291. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1292. {
  1293. unsigned long flags;
  1294. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1295. if (hcd->rh_registered)
  1296. usb_resume_root_hub (hcd->self.root_hub);
  1297. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1298. }
  1299. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1300. #endif
  1301. /*-------------------------------------------------------------------------*/
  1302. #ifdef CONFIG_USB_OTG
  1303. /**
  1304. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1305. * @bus: the bus (must use hcd framework)
  1306. * @port_num: 1-based number of port; usually bus->otg_port
  1307. * Context: in_interrupt()
  1308. *
  1309. * Starts enumeration, with an immediate reset followed later by
  1310. * khubd identifying and possibly configuring the device.
  1311. * This is needed by OTG controller drivers, where it helps meet
  1312. * HNP protocol timing requirements for starting a port reset.
  1313. */
  1314. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1315. {
  1316. struct usb_hcd *hcd;
  1317. int status = -EOPNOTSUPP;
  1318. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1319. * boards with root hubs hooked up to internal devices (instead of
  1320. * just the OTG port) may need more attention to resetting...
  1321. */
  1322. hcd = container_of (bus, struct usb_hcd, self);
  1323. if (port_num && hcd->driver->start_port_reset)
  1324. status = hcd->driver->start_port_reset(hcd, port_num);
  1325. /* run khubd shortly after (first) root port reset finishes;
  1326. * it may issue others, until at least 50 msecs have passed.
  1327. */
  1328. if (status == 0)
  1329. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1330. return status;
  1331. }
  1332. EXPORT_SYMBOL (usb_bus_start_enum);
  1333. #endif
  1334. /*-------------------------------------------------------------------------*/
  1335. /**
  1336. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1337. * @hcd: host controller returning the URB
  1338. * @urb: urb being returned to the USB device driver.
  1339. * @regs: pt_regs, passed down to the URB completion handler
  1340. * Context: in_interrupt()
  1341. *
  1342. * This hands the URB from HCD to its USB device driver, using its
  1343. * completion function. The HCD has freed all per-urb resources
  1344. * (and is done using urb->hcpriv). It also released all HCD locks;
  1345. * the device driver won't cause problems if it frees, modifies,
  1346. * or resubmits this URB.
  1347. */
  1348. void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
  1349. {
  1350. int at_root_hub;
  1351. at_root_hub = (urb->dev == hcd->self.root_hub);
  1352. urb_unlink (urb);
  1353. /* lower level hcd code should use *_dma exclusively if the
  1354. * host controller does DMA */
  1355. if (hcd->self.uses_dma && !at_root_hub) {
  1356. if (usb_pipecontrol (urb->pipe)
  1357. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1358. dma_unmap_single (hcd->self.controller, urb->setup_dma,
  1359. sizeof (struct usb_ctrlrequest),
  1360. DMA_TO_DEVICE);
  1361. if (urb->transfer_buffer_length != 0
  1362. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1363. dma_unmap_single (hcd->self.controller,
  1364. urb->transfer_dma,
  1365. urb->transfer_buffer_length,
  1366. usb_pipein (urb->pipe)
  1367. ? DMA_FROM_DEVICE
  1368. : DMA_TO_DEVICE);
  1369. }
  1370. usbmon_urb_complete (&hcd->self, urb);
  1371. /* pass ownership to the completion handler */
  1372. urb->complete (urb, regs);
  1373. atomic_dec (&urb->use_count);
  1374. if (unlikely (urb->reject))
  1375. wake_up (&usb_kill_urb_queue);
  1376. usb_put_urb (urb);
  1377. }
  1378. EXPORT_SYMBOL (usb_hcd_giveback_urb);
  1379. /*-------------------------------------------------------------------------*/
  1380. /**
  1381. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1382. * @irq: the IRQ being raised
  1383. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1384. * @r: saved hardware registers
  1385. *
  1386. * If the controller isn't HALTed, calls the driver's irq handler.
  1387. * Checks whether the controller is now dead.
  1388. */
  1389. irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
  1390. {
  1391. struct usb_hcd *hcd = __hcd;
  1392. int start = hcd->state;
  1393. if (unlikely(start == HC_STATE_HALT ||
  1394. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
  1395. return IRQ_NONE;
  1396. if (hcd->driver->irq (hcd, r) == IRQ_NONE)
  1397. return IRQ_NONE;
  1398. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1399. if (unlikely(hcd->state == HC_STATE_HALT))
  1400. usb_hc_died (hcd);
  1401. return IRQ_HANDLED;
  1402. }
  1403. /*-------------------------------------------------------------------------*/
  1404. /**
  1405. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1406. * @hcd: pointer to the HCD representing the controller
  1407. *
  1408. * This is called by bus glue to report a USB host controller that died
  1409. * while operations may still have been pending. It's called automatically
  1410. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1411. */
  1412. void usb_hc_died (struct usb_hcd *hcd)
  1413. {
  1414. unsigned long flags;
  1415. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1416. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1417. if (hcd->rh_registered) {
  1418. hcd->poll_rh = 0;
  1419. /* make khubd clean up old urbs and devices */
  1420. usb_set_device_state (hcd->self.root_hub,
  1421. USB_STATE_NOTATTACHED);
  1422. usb_kick_khubd (hcd->self.root_hub);
  1423. }
  1424. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1425. }
  1426. EXPORT_SYMBOL_GPL (usb_hc_died);
  1427. /*-------------------------------------------------------------------------*/
  1428. /**
  1429. * usb_create_hcd - create and initialize an HCD structure
  1430. * @driver: HC driver that will use this hcd
  1431. * @dev: device for this HC, stored in hcd->self.controller
  1432. * @bus_name: value to store in hcd->self.bus_name
  1433. * Context: !in_interrupt()
  1434. *
  1435. * Allocate a struct usb_hcd, with extra space at the end for the
  1436. * HC driver's private data. Initialize the generic members of the
  1437. * hcd structure.
  1438. *
  1439. * If memory is unavailable, returns NULL.
  1440. */
  1441. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1442. struct device *dev, char *bus_name)
  1443. {
  1444. struct usb_hcd *hcd;
  1445. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1446. if (!hcd) {
  1447. dev_dbg (dev, "hcd alloc failed\n");
  1448. return NULL;
  1449. }
  1450. dev_set_drvdata(dev, hcd);
  1451. kref_init(&hcd->kref);
  1452. usb_bus_init(&hcd->self);
  1453. hcd->self.controller = dev;
  1454. hcd->self.bus_name = bus_name;
  1455. hcd->self.uses_dma = (dev->dma_mask != NULL);
  1456. init_timer(&hcd->rh_timer);
  1457. hcd->rh_timer.function = rh_timer_func;
  1458. hcd->rh_timer.data = (unsigned long) hcd;
  1459. hcd->driver = driver;
  1460. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1461. "USB Host Controller";
  1462. return hcd;
  1463. }
  1464. EXPORT_SYMBOL (usb_create_hcd);
  1465. static void hcd_release (struct kref *kref)
  1466. {
  1467. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  1468. kfree(hcd);
  1469. }
  1470. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  1471. {
  1472. if (hcd)
  1473. kref_get (&hcd->kref);
  1474. return hcd;
  1475. }
  1476. EXPORT_SYMBOL (usb_get_hcd);
  1477. void usb_put_hcd (struct usb_hcd *hcd)
  1478. {
  1479. if (hcd)
  1480. kref_put (&hcd->kref, hcd_release);
  1481. }
  1482. EXPORT_SYMBOL (usb_put_hcd);
  1483. /**
  1484. * usb_add_hcd - finish generic HCD structure initialization and register
  1485. * @hcd: the usb_hcd structure to initialize
  1486. * @irqnum: Interrupt line to allocate
  1487. * @irqflags: Interrupt type flags
  1488. *
  1489. * Finish the remaining parts of generic HCD initialization: allocate the
  1490. * buffers of consistent memory, register the bus, request the IRQ line,
  1491. * and call the driver's reset() and start() routines.
  1492. */
  1493. int usb_add_hcd(struct usb_hcd *hcd,
  1494. unsigned int irqnum, unsigned long irqflags)
  1495. {
  1496. int retval;
  1497. struct usb_device *rhdev;
  1498. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1499. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1500. /* HC is in reset state, but accessible. Now do the one-time init,
  1501. * bottom up so that hcds can customize the root hubs before khubd
  1502. * starts talking to them. (Note, bus id is assigned early too.)
  1503. */
  1504. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1505. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1506. return retval;
  1507. }
  1508. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1509. goto err_register_bus;
  1510. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1511. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1512. retval = -ENOMEM;
  1513. goto err_allocate_root_hub;
  1514. }
  1515. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1516. USB_SPEED_FULL;
  1517. hcd->self.root_hub = rhdev;
  1518. /* wakeup flag init defaults to "everything works" for root hubs,
  1519. * but drivers can override it in reset() if needed, along with
  1520. * recording the overall controller's system wakeup capability.
  1521. */
  1522. device_init_wakeup(&rhdev->dev, 1);
  1523. /* "reset" is misnamed; its role is now one-time init. the controller
  1524. * should already have been reset (and boot firmware kicked off etc).
  1525. */
  1526. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1527. dev_err(hcd->self.controller, "can't setup\n");
  1528. goto err_hcd_driver_setup;
  1529. }
  1530. /* NOTE: root hub and controller capabilities may not be the same */
  1531. if (device_can_wakeup(hcd->self.controller)
  1532. && device_can_wakeup(&hcd->self.root_hub->dev))
  1533. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1534. /* enable irqs just before we start the controller */
  1535. if (hcd->driver->irq) {
  1536. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1537. hcd->driver->description, hcd->self.busnum);
  1538. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1539. hcd->irq_descr, hcd)) != 0) {
  1540. dev_err(hcd->self.controller,
  1541. "request interrupt %d failed\n", irqnum);
  1542. goto err_request_irq;
  1543. }
  1544. hcd->irq = irqnum;
  1545. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  1546. (hcd->driver->flags & HCD_MEMORY) ?
  1547. "io mem" : "io base",
  1548. (unsigned long long)hcd->rsrc_start);
  1549. } else {
  1550. hcd->irq = -1;
  1551. if (hcd->rsrc_start)
  1552. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1553. (hcd->driver->flags & HCD_MEMORY) ?
  1554. "io mem" : "io base",
  1555. (unsigned long long)hcd->rsrc_start);
  1556. }
  1557. if ((retval = hcd->driver->start(hcd)) < 0) {
  1558. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1559. goto err_hcd_driver_start;
  1560. }
  1561. /* starting here, usbcore will pay attention to this root hub */
  1562. rhdev->bus_mA = min(500u, hcd->power_budget);
  1563. if ((retval = register_root_hub(hcd)) != 0)
  1564. goto err_register_root_hub;
  1565. if (hcd->uses_new_polling && hcd->poll_rh)
  1566. usb_hcd_poll_rh_status(hcd);
  1567. return retval;
  1568. err_register_root_hub:
  1569. hcd->driver->stop(hcd);
  1570. err_hcd_driver_start:
  1571. if (hcd->irq >= 0)
  1572. free_irq(irqnum, hcd);
  1573. err_request_irq:
  1574. err_hcd_driver_setup:
  1575. hcd->self.root_hub = NULL;
  1576. usb_put_dev(rhdev);
  1577. err_allocate_root_hub:
  1578. usb_deregister_bus(&hcd->self);
  1579. err_register_bus:
  1580. hcd_buffer_destroy(hcd);
  1581. return retval;
  1582. }
  1583. EXPORT_SYMBOL (usb_add_hcd);
  1584. /**
  1585. * usb_remove_hcd - shutdown processing for generic HCDs
  1586. * @hcd: the usb_hcd structure to remove
  1587. * Context: !in_interrupt()
  1588. *
  1589. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1590. * invoking the HCD's stop() method.
  1591. */
  1592. void usb_remove_hcd(struct usb_hcd *hcd)
  1593. {
  1594. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1595. if (HC_IS_RUNNING (hcd->state))
  1596. hcd->state = HC_STATE_QUIESCING;
  1597. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1598. spin_lock_irq (&hcd_root_hub_lock);
  1599. hcd->rh_registered = 0;
  1600. spin_unlock_irq (&hcd_root_hub_lock);
  1601. mutex_lock(&usb_bus_list_lock);
  1602. usb_disconnect(&hcd->self.root_hub);
  1603. mutex_unlock(&usb_bus_list_lock);
  1604. hcd->poll_rh = 0;
  1605. del_timer_sync(&hcd->rh_timer);
  1606. hcd->driver->stop(hcd);
  1607. hcd->state = HC_STATE_HALT;
  1608. if (hcd->irq >= 0)
  1609. free_irq(hcd->irq, hcd);
  1610. usb_deregister_bus(&hcd->self);
  1611. hcd_buffer_destroy(hcd);
  1612. }
  1613. EXPORT_SYMBOL (usb_remove_hcd);
  1614. void
  1615. usb_hcd_platform_shutdown(struct platform_device* dev)
  1616. {
  1617. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1618. if (hcd->driver->shutdown)
  1619. hcd->driver->shutdown(hcd);
  1620. }
  1621. EXPORT_SYMBOL (usb_hcd_platform_shutdown);
  1622. /*-------------------------------------------------------------------------*/
  1623. #if defined(CONFIG_USB_MON)
  1624. struct usb_mon_operations *mon_ops;
  1625. /*
  1626. * The registration is unlocked.
  1627. * We do it this way because we do not want to lock in hot paths.
  1628. *
  1629. * Notice that the code is minimally error-proof. Because usbmon needs
  1630. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1631. */
  1632. int usb_mon_register (struct usb_mon_operations *ops)
  1633. {
  1634. if (mon_ops)
  1635. return -EBUSY;
  1636. mon_ops = ops;
  1637. mb();
  1638. return 0;
  1639. }
  1640. EXPORT_SYMBOL_GPL (usb_mon_register);
  1641. void usb_mon_deregister (void)
  1642. {
  1643. if (mon_ops == NULL) {
  1644. printk(KERN_ERR "USB: monitor was not registered\n");
  1645. return;
  1646. }
  1647. mon_ops = NULL;
  1648. mb();
  1649. }
  1650. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1651. #endif /* CONFIG_USB_MON */