hcd.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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/config.h>
  25. #include <linux/module.h>
  26. #include <linux/version.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/completion.h>
  30. #include <linux/utsname.h>
  31. #include <linux/mm.h>
  32. #include <asm/io.h>
  33. #include <asm/scatterlist.h>
  34. #include <linux/device.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/mutex.h>
  37. #include <asm/irq.h>
  38. #include <asm/byteorder.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. case DeviceRequest | USB_REQ_GET_STATUS:
  314. tbuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP)
  315. | (1 << USB_DEVICE_SELF_POWERED);
  316. tbuf [1] = 0;
  317. len = 2;
  318. break;
  319. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  320. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  321. hcd->remote_wakeup = 0;
  322. else
  323. goto error;
  324. break;
  325. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  326. if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP)
  327. hcd->remote_wakeup = 1;
  328. else
  329. goto error;
  330. break;
  331. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  332. tbuf [0] = 1;
  333. len = 1;
  334. /* FALLTHROUGH */
  335. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  336. break;
  337. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  338. switch (wValue & 0xff00) {
  339. case USB_DT_DEVICE << 8:
  340. if (hcd->driver->flags & HCD_USB2)
  341. bufp = usb2_rh_dev_descriptor;
  342. else if (hcd->driver->flags & HCD_USB11)
  343. bufp = usb11_rh_dev_descriptor;
  344. else
  345. goto error;
  346. len = 18;
  347. break;
  348. case USB_DT_CONFIG << 8:
  349. if (hcd->driver->flags & HCD_USB2) {
  350. bufp = hs_rh_config_descriptor;
  351. len = sizeof hs_rh_config_descriptor;
  352. } else {
  353. bufp = fs_rh_config_descriptor;
  354. len = sizeof fs_rh_config_descriptor;
  355. }
  356. if (hcd->can_wakeup)
  357. patch_wakeup = 1;
  358. break;
  359. case USB_DT_STRING << 8:
  360. n = rh_string (wValue & 0xff, hcd, ubuf, wLength);
  361. if (n < 0)
  362. goto error;
  363. urb->actual_length = n;
  364. break;
  365. default:
  366. goto error;
  367. }
  368. break;
  369. case DeviceRequest | USB_REQ_GET_INTERFACE:
  370. tbuf [0] = 0;
  371. len = 1;
  372. /* FALLTHROUGH */
  373. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  374. break;
  375. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  376. // wValue == urb->dev->devaddr
  377. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  378. wValue);
  379. break;
  380. /* INTERFACE REQUESTS (no defined feature/status flags) */
  381. /* ENDPOINT REQUESTS */
  382. case EndpointRequest | USB_REQ_GET_STATUS:
  383. // ENDPOINT_HALT flag
  384. tbuf [0] = 0;
  385. tbuf [1] = 0;
  386. len = 2;
  387. /* FALLTHROUGH */
  388. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  389. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  390. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  391. break;
  392. /* CLASS REQUESTS (and errors) */
  393. default:
  394. /* non-generic request */
  395. switch (typeReq) {
  396. case GetHubStatus:
  397. case GetPortStatus:
  398. len = 4;
  399. break;
  400. case GetHubDescriptor:
  401. len = sizeof (struct usb_hub_descriptor);
  402. break;
  403. }
  404. status = hcd->driver->hub_control (hcd,
  405. typeReq, wValue, wIndex,
  406. tbuf, wLength);
  407. break;
  408. error:
  409. /* "protocol stall" on error */
  410. status = -EPIPE;
  411. }
  412. if (status) {
  413. len = 0;
  414. if (status != -EPIPE) {
  415. dev_dbg (hcd->self.controller,
  416. "CTRL: TypeReq=0x%x val=0x%x "
  417. "idx=0x%x len=%d ==> %d\n",
  418. typeReq, wValue, wIndex,
  419. wLength, status);
  420. }
  421. }
  422. if (len) {
  423. if (urb->transfer_buffer_length < len)
  424. len = urb->transfer_buffer_length;
  425. urb->actual_length = len;
  426. // always USB_DIR_IN, toward host
  427. memcpy (ubuf, bufp, len);
  428. /* report whether RH hardware supports remote wakeup */
  429. if (patch_wakeup &&
  430. len > offsetof (struct usb_config_descriptor,
  431. bmAttributes))
  432. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  433. |= USB_CONFIG_ATT_WAKEUP;
  434. }
  435. /* any errors get returned through the urb completion */
  436. local_irq_save (flags);
  437. spin_lock (&urb->lock);
  438. if (urb->status == -EINPROGRESS)
  439. urb->status = status;
  440. spin_unlock (&urb->lock);
  441. usb_hcd_giveback_urb (hcd, urb, NULL);
  442. local_irq_restore (flags);
  443. return 0;
  444. }
  445. /*-------------------------------------------------------------------------*/
  446. /*
  447. * Root Hub interrupt transfers are polled using a timer if the
  448. * driver requests it; otherwise the driver is responsible for
  449. * calling usb_hcd_poll_rh_status() when an event occurs.
  450. *
  451. * Completions are called in_interrupt(), but they may or may not
  452. * be in_irq().
  453. */
  454. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  455. {
  456. struct urb *urb;
  457. int length;
  458. unsigned long flags;
  459. char buffer[4]; /* Any root hubs with > 31 ports? */
  460. if (!hcd->uses_new_polling && !hcd->status_urb)
  461. return;
  462. length = hcd->driver->hub_status_data(hcd, buffer);
  463. if (length > 0) {
  464. /* try to complete the status urb */
  465. local_irq_save (flags);
  466. spin_lock(&hcd_root_hub_lock);
  467. urb = hcd->status_urb;
  468. if (urb) {
  469. spin_lock(&urb->lock);
  470. if (urb->status == -EINPROGRESS) {
  471. hcd->poll_pending = 0;
  472. hcd->status_urb = NULL;
  473. urb->status = 0;
  474. urb->hcpriv = NULL;
  475. urb->actual_length = length;
  476. memcpy(urb->transfer_buffer, buffer, length);
  477. } else /* urb has been unlinked */
  478. length = 0;
  479. spin_unlock(&urb->lock);
  480. } else
  481. length = 0;
  482. spin_unlock(&hcd_root_hub_lock);
  483. /* local irqs are always blocked in completions */
  484. if (length > 0)
  485. usb_hcd_giveback_urb (hcd, urb, NULL);
  486. else
  487. hcd->poll_pending = 1;
  488. local_irq_restore (flags);
  489. }
  490. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  491. * exceed that limit if HZ is 100. */
  492. if (hcd->uses_new_polling ? hcd->poll_rh :
  493. (length == 0 && hcd->status_urb != NULL))
  494. mod_timer (&hcd->rh_timer, jiffies + msecs_to_jiffies(250));
  495. }
  496. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  497. /* timer callback */
  498. static void rh_timer_func (unsigned long _hcd)
  499. {
  500. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  501. }
  502. /*-------------------------------------------------------------------------*/
  503. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  504. {
  505. int retval;
  506. unsigned long flags;
  507. int len = 1 + (urb->dev->maxchild / 8);
  508. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  509. if (urb->status != -EINPROGRESS) /* already unlinked */
  510. retval = urb->status;
  511. else if (hcd->status_urb || urb->transfer_buffer_length < len) {
  512. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  513. retval = -EINVAL;
  514. } else {
  515. hcd->status_urb = urb;
  516. urb->hcpriv = hcd; /* indicate it's queued */
  517. if (!hcd->uses_new_polling)
  518. mod_timer (&hcd->rh_timer, jiffies +
  519. msecs_to_jiffies(250));
  520. /* If a status change has already occurred, report it ASAP */
  521. else if (hcd->poll_pending)
  522. mod_timer (&hcd->rh_timer, jiffies);
  523. retval = 0;
  524. }
  525. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  526. return retval;
  527. }
  528. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  529. {
  530. if (usb_pipeint (urb->pipe))
  531. return rh_queue_status (hcd, urb);
  532. if (usb_pipecontrol (urb->pipe))
  533. return rh_call_control (hcd, urb);
  534. return -EINVAL;
  535. }
  536. /*-------------------------------------------------------------------------*/
  537. /* Asynchronous unlinks of root-hub control URBs are legal, but they
  538. * don't do anything. Status URB unlinks must be made in process context
  539. * with interrupts enabled.
  540. */
  541. static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  542. {
  543. if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */
  544. if (in_interrupt())
  545. return 0; /* nothing to do */
  546. spin_lock_irq(&urb->lock); /* from usb_kill_urb */
  547. ++urb->reject;
  548. spin_unlock_irq(&urb->lock);
  549. wait_event(usb_kill_urb_queue,
  550. atomic_read(&urb->use_count) == 0);
  551. spin_lock_irq(&urb->lock);
  552. --urb->reject;
  553. spin_unlock_irq(&urb->lock);
  554. } else { /* Status URB */
  555. if (!hcd->uses_new_polling)
  556. del_timer_sync (&hcd->rh_timer);
  557. local_irq_disable ();
  558. spin_lock (&hcd_root_hub_lock);
  559. if (urb == hcd->status_urb) {
  560. hcd->status_urb = NULL;
  561. urb->hcpriv = NULL;
  562. } else
  563. urb = NULL; /* wasn't fully queued */
  564. spin_unlock (&hcd_root_hub_lock);
  565. if (urb)
  566. usb_hcd_giveback_urb (hcd, urb, NULL);
  567. local_irq_enable ();
  568. }
  569. return 0;
  570. }
  571. /*-------------------------------------------------------------------------*/
  572. /* exported only within usbcore */
  573. struct usb_bus *usb_bus_get(struct usb_bus *bus)
  574. {
  575. if (bus)
  576. kref_get(&bus->kref);
  577. return bus;
  578. }
  579. static void usb_host_release(struct kref *kref)
  580. {
  581. struct usb_bus *bus = container_of(kref, struct usb_bus, kref);
  582. if (bus->release)
  583. bus->release(bus);
  584. }
  585. /* exported only within usbcore */
  586. void usb_bus_put(struct usb_bus *bus)
  587. {
  588. if (bus)
  589. kref_put(&bus->kref, usb_host_release);
  590. }
  591. /*-------------------------------------------------------------------------*/
  592. static struct class *usb_host_class;
  593. int usb_host_init(void)
  594. {
  595. int retval = 0;
  596. usb_host_class = class_create(THIS_MODULE, "usb_host");
  597. if (IS_ERR(usb_host_class))
  598. retval = PTR_ERR(usb_host_class);
  599. return retval;
  600. }
  601. void usb_host_cleanup(void)
  602. {
  603. class_destroy(usb_host_class);
  604. }
  605. /**
  606. * usb_bus_init - shared initialization code
  607. * @bus: the bus structure being initialized
  608. *
  609. * This code is used to initialize a usb_bus structure, memory for which is
  610. * separately managed.
  611. */
  612. static void usb_bus_init (struct usb_bus *bus)
  613. {
  614. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  615. bus->devnum_next = 1;
  616. bus->root_hub = NULL;
  617. bus->hcpriv = NULL;
  618. bus->busnum = -1;
  619. bus->bandwidth_allocated = 0;
  620. bus->bandwidth_int_reqs = 0;
  621. bus->bandwidth_isoc_reqs = 0;
  622. INIT_LIST_HEAD (&bus->bus_list);
  623. kref_init(&bus->kref);
  624. }
  625. /**
  626. * usb_alloc_bus - creates a new USB host controller structure
  627. * @op: pointer to a struct usb_operations that this bus structure should use
  628. * Context: !in_interrupt()
  629. *
  630. * Creates a USB host controller bus structure with the specified
  631. * usb_operations and initializes all the necessary internal objects.
  632. *
  633. * If no memory is available, NULL is returned.
  634. *
  635. * The caller should call usb_put_bus() when it is finished with the structure.
  636. */
  637. struct usb_bus *usb_alloc_bus (struct usb_operations *op)
  638. {
  639. struct usb_bus *bus;
  640. bus = kzalloc (sizeof *bus, GFP_KERNEL);
  641. if (!bus)
  642. return NULL;
  643. usb_bus_init (bus);
  644. bus->op = op;
  645. return bus;
  646. }
  647. /*-------------------------------------------------------------------------*/
  648. /**
  649. * usb_register_bus - registers the USB host controller with the usb core
  650. * @bus: pointer to the bus to register
  651. * Context: !in_interrupt()
  652. *
  653. * Assigns a bus number, and links the controller into usbcore data
  654. * structures so that it can be seen by scanning the bus list.
  655. */
  656. static int usb_register_bus(struct usb_bus *bus)
  657. {
  658. int busnum;
  659. mutex_lock(&usb_bus_list_lock);
  660. busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  661. if (busnum < USB_MAXBUS) {
  662. set_bit (busnum, busmap.busmap);
  663. bus->busnum = busnum;
  664. } else {
  665. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  666. mutex_unlock(&usb_bus_list_lock);
  667. return -E2BIG;
  668. }
  669. bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0),
  670. bus->controller, "usb_host%d", busnum);
  671. if (IS_ERR(bus->class_dev)) {
  672. clear_bit(busnum, busmap.busmap);
  673. mutex_unlock(&usb_bus_list_lock);
  674. return PTR_ERR(bus->class_dev);
  675. }
  676. class_set_devdata(bus->class_dev, bus);
  677. /* Add it to the local list of buses */
  678. list_add (&bus->bus_list, &usb_bus_list);
  679. mutex_unlock(&usb_bus_list_lock);
  680. usb_notify_add_bus(bus);
  681. dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
  682. return 0;
  683. }
  684. /**
  685. * usb_deregister_bus - deregisters the USB host controller
  686. * @bus: pointer to the bus to deregister
  687. * Context: !in_interrupt()
  688. *
  689. * Recycles the bus number, and unlinks the controller from usbcore data
  690. * structures so that it won't be seen by scanning the bus list.
  691. */
  692. static void usb_deregister_bus (struct usb_bus *bus)
  693. {
  694. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  695. /*
  696. * NOTE: make sure that all the devices are removed by the
  697. * controller code, as well as having it call this when cleaning
  698. * itself up
  699. */
  700. mutex_lock(&usb_bus_list_lock);
  701. list_del (&bus->bus_list);
  702. mutex_unlock(&usb_bus_list_lock);
  703. usb_notify_remove_bus(bus);
  704. clear_bit (bus->busnum, busmap.busmap);
  705. class_device_unregister(bus->class_dev);
  706. }
  707. /**
  708. * register_root_hub - called by usb_add_hcd() to register a root hub
  709. * @usb_dev: the usb root hub device to be registered.
  710. * @hcd: host controller for this root hub
  711. *
  712. * This function registers the root hub with the USB subsystem. It sets up
  713. * the device properly in the device tree and stores the root_hub pointer
  714. * in the bus structure, then calls usb_new_device() to register the usb
  715. * device. It also assigns the root hub's USB address (always 1).
  716. */
  717. static int register_root_hub (struct usb_device *usb_dev,
  718. struct usb_hcd *hcd)
  719. {
  720. struct device *parent_dev = hcd->self.controller;
  721. const int devnum = 1;
  722. int retval;
  723. usb_dev->devnum = devnum;
  724. usb_dev->bus->devnum_next = devnum + 1;
  725. memset (&usb_dev->bus->devmap.devicemap, 0,
  726. sizeof usb_dev->bus->devmap.devicemap);
  727. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  728. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  729. mutex_lock(&usb_bus_list_lock);
  730. usb_dev->bus->root_hub = usb_dev;
  731. usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  732. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  733. if (retval != sizeof usb_dev->descriptor) {
  734. usb_dev->bus->root_hub = NULL;
  735. mutex_unlock(&usb_bus_list_lock);
  736. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  737. usb_dev->dev.bus_id, retval);
  738. return (retval < 0) ? retval : -EMSGSIZE;
  739. }
  740. retval = usb_new_device (usb_dev);
  741. if (retval) {
  742. usb_dev->bus->root_hub = NULL;
  743. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  744. usb_dev->dev.bus_id, retval);
  745. }
  746. mutex_unlock(&usb_bus_list_lock);
  747. if (retval == 0) {
  748. spin_lock_irq (&hcd_root_hub_lock);
  749. hcd->rh_registered = 1;
  750. spin_unlock_irq (&hcd_root_hub_lock);
  751. /* Did the HC die before the root hub was registered? */
  752. if (hcd->state == HC_STATE_HALT)
  753. usb_hc_died (hcd); /* This time clean up */
  754. }
  755. return retval;
  756. }
  757. void usb_enable_root_hub_irq (struct usb_bus *bus)
  758. {
  759. struct usb_hcd *hcd;
  760. hcd = container_of (bus, struct usb_hcd, self);
  761. if (hcd->driver->hub_irq_enable && !hcd->poll_rh &&
  762. hcd->state != HC_STATE_HALT)
  763. hcd->driver->hub_irq_enable (hcd);
  764. }
  765. /*-------------------------------------------------------------------------*/
  766. /**
  767. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  768. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  769. * @is_input: true iff the transaction sends data to the host
  770. * @isoc: true for isochronous transactions, false for interrupt ones
  771. * @bytecount: how many bytes in the transaction.
  772. *
  773. * Returns approximate bus time in nanoseconds for a periodic transaction.
  774. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  775. * scheduled in software, this function is only used for such scheduling.
  776. */
  777. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  778. {
  779. unsigned long tmp;
  780. switch (speed) {
  781. case USB_SPEED_LOW: /* INTR only */
  782. if (is_input) {
  783. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  784. return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  785. } else {
  786. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  787. return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  788. }
  789. case USB_SPEED_FULL: /* ISOC or INTR */
  790. if (isoc) {
  791. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  792. return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
  793. } else {
  794. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  795. return (9107L + BW_HOST_DELAY + tmp);
  796. }
  797. case USB_SPEED_HIGH: /* ISOC or INTR */
  798. // FIXME adjust for input vs output
  799. if (isoc)
  800. tmp = HS_NSECS_ISO (bytecount);
  801. else
  802. tmp = HS_NSECS (bytecount);
  803. return tmp;
  804. default:
  805. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  806. return -1;
  807. }
  808. }
  809. EXPORT_SYMBOL (usb_calc_bus_time);
  810. /*
  811. * usb_check_bandwidth():
  812. *
  813. * old_alloc is from host_controller->bandwidth_allocated in microseconds;
  814. * bustime is from calc_bus_time(), but converted to microseconds.
  815. *
  816. * returns <bustime in us> if successful,
  817. * or -ENOSPC if bandwidth request fails.
  818. *
  819. * FIXME:
  820. * This initial implementation does not use Endpoint.bInterval
  821. * in managing bandwidth allocation.
  822. * It probably needs to be expanded to use Endpoint.bInterval.
  823. * This can be done as a later enhancement (correction).
  824. *
  825. * This will also probably require some kind of
  826. * frame allocation tracking...meaning, for example,
  827. * that if multiple drivers request interrupts every 10 USB frames,
  828. * they don't all have to be allocated at
  829. * frame numbers N, N+10, N+20, etc. Some of them could be at
  830. * N+11, N+21, N+31, etc., and others at
  831. * N+12, N+22, N+32, etc.
  832. *
  833. * Similarly for isochronous transfers...
  834. *
  835. * Individual HCDs can schedule more directly ... this logic
  836. * is not correct for high speed transfers.
  837. */
  838. int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
  839. {
  840. unsigned int pipe = urb->pipe;
  841. long bustime;
  842. int is_in = usb_pipein (pipe);
  843. int is_iso = usb_pipeisoc (pipe);
  844. int old_alloc = dev->bus->bandwidth_allocated;
  845. int new_alloc;
  846. bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
  847. usb_maxpacket (dev, pipe, !is_in)));
  848. if (is_iso)
  849. bustime /= urb->number_of_packets;
  850. new_alloc = old_alloc + (int) bustime;
  851. if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
  852. #ifdef DEBUG
  853. char *mode =
  854. #ifdef CONFIG_USB_BANDWIDTH
  855. "";
  856. #else
  857. "would have ";
  858. #endif
  859. dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
  860. mode, old_alloc, bustime, new_alloc);
  861. #endif
  862. #ifdef CONFIG_USB_BANDWIDTH
  863. bustime = -ENOSPC; /* report error */
  864. #endif
  865. }
  866. return bustime;
  867. }
  868. EXPORT_SYMBOL (usb_check_bandwidth);
  869. /**
  870. * usb_claim_bandwidth - records bandwidth for a periodic transfer
  871. * @dev: source/target of request
  872. * @urb: request (urb->dev == dev)
  873. * @bustime: bandwidth consumed, in (average) microseconds per frame
  874. * @isoc: true iff the request is isochronous
  875. *
  876. * Bus bandwidth reservations are recorded purely for diagnostic purposes.
  877. * HCDs are expected not to overcommit periodic bandwidth, and to record such
  878. * reservations whenever endpoints are added to the periodic schedule.
  879. *
  880. * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
  881. * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
  882. * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
  883. * large its periodic schedule is.
  884. */
  885. void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
  886. {
  887. dev->bus->bandwidth_allocated += bustime;
  888. if (isoc)
  889. dev->bus->bandwidth_isoc_reqs++;
  890. else
  891. dev->bus->bandwidth_int_reqs++;
  892. urb->bandwidth = bustime;
  893. #ifdef USB_BANDWIDTH_MESSAGES
  894. dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
  895. bustime,
  896. isoc ? "ISOC" : "INTR",
  897. dev->bus->bandwidth_allocated,
  898. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  899. #endif
  900. }
  901. EXPORT_SYMBOL (usb_claim_bandwidth);
  902. /**
  903. * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
  904. * @dev: source/target of request
  905. * @urb: request (urb->dev == dev)
  906. * @isoc: true iff the request is isochronous
  907. *
  908. * This records that previously allocated bandwidth has been released.
  909. * Bandwidth is released when endpoints are removed from the host controller's
  910. * periodic schedule.
  911. */
  912. void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
  913. {
  914. dev->bus->bandwidth_allocated -= urb->bandwidth;
  915. if (isoc)
  916. dev->bus->bandwidth_isoc_reqs--;
  917. else
  918. dev->bus->bandwidth_int_reqs--;
  919. #ifdef USB_BANDWIDTH_MESSAGES
  920. dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
  921. urb->bandwidth,
  922. isoc ? "ISOC" : "INTR",
  923. dev->bus->bandwidth_allocated,
  924. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  925. #endif
  926. urb->bandwidth = 0;
  927. }
  928. EXPORT_SYMBOL (usb_release_bandwidth);
  929. /*-------------------------------------------------------------------------*/
  930. /*
  931. * Generic HC operations.
  932. */
  933. /*-------------------------------------------------------------------------*/
  934. static void urb_unlink (struct urb *urb)
  935. {
  936. unsigned long flags;
  937. /* Release any periodic transfer bandwidth */
  938. if (urb->bandwidth)
  939. usb_release_bandwidth (urb->dev, urb,
  940. usb_pipeisoc (urb->pipe));
  941. /* clear all state linking urb to this dev (and hcd) */
  942. spin_lock_irqsave (&hcd_data_lock, flags);
  943. list_del_init (&urb->urb_list);
  944. spin_unlock_irqrestore (&hcd_data_lock, flags);
  945. usb_put_dev (urb->dev);
  946. }
  947. /* may be called in any context with a valid urb->dev usecount
  948. * caller surrenders "ownership" of urb
  949. * expects usb_submit_urb() to have sanity checked and conditioned all
  950. * inputs in the urb
  951. */
  952. static int hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  953. {
  954. int status;
  955. struct usb_hcd *hcd = urb->dev->bus->hcpriv;
  956. struct usb_host_endpoint *ep;
  957. unsigned long flags;
  958. if (!hcd)
  959. return -ENODEV;
  960. usbmon_urb_submit(&hcd->self, urb);
  961. /*
  962. * Atomically queue the urb, first to our records, then to the HCD.
  963. * Access to urb->status is controlled by urb->lock ... changes on
  964. * i/o completion (normal or fault) or unlinking.
  965. */
  966. // FIXME: verify that quiescing hc works right (RH cleans up)
  967. spin_lock_irqsave (&hcd_data_lock, flags);
  968. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  969. [usb_pipeendpoint(urb->pipe)];
  970. if (unlikely (!ep))
  971. status = -ENOENT;
  972. else if (unlikely (urb->reject))
  973. status = -EPERM;
  974. else switch (hcd->state) {
  975. case HC_STATE_RUNNING:
  976. case HC_STATE_RESUMING:
  977. doit:
  978. usb_get_dev (urb->dev);
  979. list_add_tail (&urb->urb_list, &ep->urb_list);
  980. status = 0;
  981. break;
  982. case HC_STATE_SUSPENDED:
  983. /* HC upstream links (register access, wakeup signaling) can work
  984. * even when the downstream links (and DMA etc) are quiesced; let
  985. * usbcore talk to the root hub.
  986. */
  987. if (hcd->self.controller->power.power_state.event == PM_EVENT_ON
  988. && urb->dev->parent == NULL)
  989. goto doit;
  990. /* FALL THROUGH */
  991. default:
  992. status = -ESHUTDOWN;
  993. break;
  994. }
  995. spin_unlock_irqrestore (&hcd_data_lock, flags);
  996. if (status) {
  997. INIT_LIST_HEAD (&urb->urb_list);
  998. usbmon_urb_submit_error(&hcd->self, urb, status);
  999. return status;
  1000. }
  1001. /* increment urb's reference count as part of giving it to the HCD
  1002. * (which now controls it). HCD guarantees that it either returns
  1003. * an error or calls giveback(), but not both.
  1004. */
  1005. urb = usb_get_urb (urb);
  1006. atomic_inc (&urb->use_count);
  1007. if (urb->dev == hcd->self.root_hub) {
  1008. /* NOTE: requirement on hub callers (usbfs and the hub
  1009. * driver, for now) that URBs' urb->transfer_buffer be
  1010. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1011. * they could clobber root hub response data.
  1012. */
  1013. status = rh_urb_enqueue (hcd, urb);
  1014. goto done;
  1015. }
  1016. /* lower level hcd code should use *_dma exclusively,
  1017. * unless it uses pio or talks to another transport.
  1018. */
  1019. if (hcd->self.controller->dma_mask) {
  1020. if (usb_pipecontrol (urb->pipe)
  1021. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1022. urb->setup_dma = dma_map_single (
  1023. hcd->self.controller,
  1024. urb->setup_packet,
  1025. sizeof (struct usb_ctrlrequest),
  1026. DMA_TO_DEVICE);
  1027. if (urb->transfer_buffer_length != 0
  1028. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1029. urb->transfer_dma = dma_map_single (
  1030. hcd->self.controller,
  1031. urb->transfer_buffer,
  1032. urb->transfer_buffer_length,
  1033. usb_pipein (urb->pipe)
  1034. ? DMA_FROM_DEVICE
  1035. : DMA_TO_DEVICE);
  1036. }
  1037. status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags);
  1038. done:
  1039. if (unlikely (status)) {
  1040. urb_unlink (urb);
  1041. atomic_dec (&urb->use_count);
  1042. if (urb->reject)
  1043. wake_up (&usb_kill_urb_queue);
  1044. usb_put_urb (urb);
  1045. usbmon_urb_submit_error(&hcd->self, urb, status);
  1046. }
  1047. return status;
  1048. }
  1049. /*-------------------------------------------------------------------------*/
  1050. /* called in any context */
  1051. static int hcd_get_frame_number (struct usb_device *udev)
  1052. {
  1053. struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
  1054. if (!HC_IS_RUNNING (hcd->state))
  1055. return -ESHUTDOWN;
  1056. return hcd->driver->get_frame_number (hcd);
  1057. }
  1058. /*-------------------------------------------------------------------------*/
  1059. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1060. * off hardware queues (which may take a while) and returning it as
  1061. * soon as practical. we've already set up the urb's return status,
  1062. * but we can't know if the callback completed already.
  1063. */
  1064. static int
  1065. unlink1 (struct usb_hcd *hcd, struct urb *urb)
  1066. {
  1067. int value;
  1068. if (urb->dev == hcd->self.root_hub)
  1069. value = usb_rh_urb_dequeue (hcd, urb);
  1070. else {
  1071. /* The only reason an HCD might fail this call is if
  1072. * it has not yet fully queued the urb to begin with.
  1073. * Such failures should be harmless. */
  1074. value = hcd->driver->urb_dequeue (hcd, urb);
  1075. }
  1076. if (value != 0)
  1077. dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
  1078. urb, value);
  1079. return value;
  1080. }
  1081. /*
  1082. * called in any context
  1083. *
  1084. * caller guarantees urb won't be recycled till both unlink()
  1085. * and the urb's completion function return
  1086. */
  1087. static int hcd_unlink_urb (struct urb *urb, int status)
  1088. {
  1089. struct usb_host_endpoint *ep;
  1090. struct usb_hcd *hcd = NULL;
  1091. struct device *sys = NULL;
  1092. unsigned long flags;
  1093. struct list_head *tmp;
  1094. int retval;
  1095. if (!urb)
  1096. return -EINVAL;
  1097. if (!urb->dev || !urb->dev->bus)
  1098. return -ENODEV;
  1099. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  1100. [usb_pipeendpoint(urb->pipe)];
  1101. if (!ep)
  1102. return -ENODEV;
  1103. /*
  1104. * we contend for urb->status with the hcd core,
  1105. * which changes it while returning the urb.
  1106. *
  1107. * Caller guaranteed that the urb pointer hasn't been freed, and
  1108. * that it was submitted. But as a rule it can't know whether or
  1109. * not it's already been unlinked ... so we respect the reversed
  1110. * lock sequence needed for the usb_hcd_giveback_urb() code paths
  1111. * (urb lock, then hcd_data_lock) in case some other CPU is now
  1112. * unlinking it.
  1113. */
  1114. spin_lock_irqsave (&urb->lock, flags);
  1115. spin_lock (&hcd_data_lock);
  1116. sys = &urb->dev->dev;
  1117. hcd = urb->dev->bus->hcpriv;
  1118. if (hcd == NULL) {
  1119. retval = -ENODEV;
  1120. goto done;
  1121. }
  1122. /* insist the urb is still queued */
  1123. list_for_each(tmp, &ep->urb_list) {
  1124. if (tmp == &urb->urb_list)
  1125. break;
  1126. }
  1127. if (tmp != &urb->urb_list) {
  1128. retval = -EIDRM;
  1129. goto done;
  1130. }
  1131. /* Any status except -EINPROGRESS means something already started to
  1132. * unlink this URB from the hardware. So there's no more work to do.
  1133. */
  1134. if (urb->status != -EINPROGRESS) {
  1135. retval = -EBUSY;
  1136. goto done;
  1137. }
  1138. /* IRQ setup can easily be broken so that USB controllers
  1139. * never get completion IRQs ... maybe even the ones we need to
  1140. * finish unlinking the initial failed usb_set_address()
  1141. * or device descriptor fetch.
  1142. */
  1143. if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags)
  1144. && hcd->self.root_hub != urb->dev) {
  1145. dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
  1146. "Controller is probably using the wrong IRQ."
  1147. "\n");
  1148. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1149. }
  1150. urb->status = status;
  1151. spin_unlock (&hcd_data_lock);
  1152. spin_unlock_irqrestore (&urb->lock, flags);
  1153. retval = unlink1 (hcd, urb);
  1154. if (retval == 0)
  1155. retval = -EINPROGRESS;
  1156. return retval;
  1157. done:
  1158. spin_unlock (&hcd_data_lock);
  1159. spin_unlock_irqrestore (&urb->lock, flags);
  1160. if (retval != -EIDRM && sys && sys->driver)
  1161. dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
  1162. return retval;
  1163. }
  1164. /*-------------------------------------------------------------------------*/
  1165. /* disables the endpoint: cancels any pending urbs, then synchronizes with
  1166. * the hcd to make sure all endpoint state is gone from hardware. use for
  1167. * set_configuration, set_interface, driver removal, physical disconnect.
  1168. *
  1169. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1170. * type, maxpacket size, toggle, halt status, and scheduling.
  1171. */
  1172. static void
  1173. hcd_endpoint_disable (struct usb_device *udev, struct usb_host_endpoint *ep)
  1174. {
  1175. struct usb_hcd *hcd;
  1176. struct urb *urb;
  1177. hcd = udev->bus->hcpriv;
  1178. WARN_ON (!HC_IS_RUNNING (hcd->state) && hcd->state != HC_STATE_HALT &&
  1179. udev->state != USB_STATE_NOTATTACHED);
  1180. local_irq_disable ();
  1181. /* FIXME move most of this into message.c as part of its
  1182. * endpoint disable logic
  1183. */
  1184. /* ep is already gone from udev->ep_{in,out}[]; no more submits */
  1185. rescan:
  1186. spin_lock (&hcd_data_lock);
  1187. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1188. int tmp;
  1189. /* another cpu may be in hcd, spinning on hcd_data_lock
  1190. * to giveback() this urb. the races here should be
  1191. * small, but a full fix needs a new "can't submit"
  1192. * urb state.
  1193. * FIXME urb->reject should allow that...
  1194. */
  1195. if (urb->status != -EINPROGRESS)
  1196. continue;
  1197. usb_get_urb (urb);
  1198. spin_unlock (&hcd_data_lock);
  1199. spin_lock (&urb->lock);
  1200. tmp = urb->status;
  1201. if (tmp == -EINPROGRESS)
  1202. urb->status = -ESHUTDOWN;
  1203. spin_unlock (&urb->lock);
  1204. /* kick hcd unless it's already returning this */
  1205. if (tmp == -EINPROGRESS) {
  1206. tmp = urb->pipe;
  1207. unlink1 (hcd, urb);
  1208. dev_dbg (hcd->self.controller,
  1209. "shutdown urb %p pipe %08x ep%d%s%s\n",
  1210. urb, tmp, usb_pipeendpoint (tmp),
  1211. (tmp & USB_DIR_IN) ? "in" : "out",
  1212. ({ char *s; \
  1213. switch (usb_pipetype (tmp)) { \
  1214. case PIPE_CONTROL: s = ""; break; \
  1215. case PIPE_BULK: s = "-bulk"; break; \
  1216. case PIPE_INTERRUPT: s = "-intr"; break; \
  1217. default: s = "-iso"; break; \
  1218. }; s;}));
  1219. }
  1220. usb_put_urb (urb);
  1221. /* list contents may have changed */
  1222. goto rescan;
  1223. }
  1224. spin_unlock (&hcd_data_lock);
  1225. local_irq_enable ();
  1226. /* synchronize with the hardware, so old configuration state
  1227. * clears out immediately (and will be freed).
  1228. */
  1229. might_sleep ();
  1230. if (hcd->driver->endpoint_disable)
  1231. hcd->driver->endpoint_disable (hcd, ep);
  1232. }
  1233. /*-------------------------------------------------------------------------*/
  1234. #ifdef CONFIG_PM
  1235. int hcd_bus_suspend (struct usb_bus *bus)
  1236. {
  1237. struct usb_hcd *hcd;
  1238. int status;
  1239. hcd = container_of (bus, struct usb_hcd, self);
  1240. if (!hcd->driver->bus_suspend)
  1241. return -ENOENT;
  1242. hcd->state = HC_STATE_QUIESCING;
  1243. status = hcd->driver->bus_suspend (hcd);
  1244. if (status == 0)
  1245. hcd->state = HC_STATE_SUSPENDED;
  1246. else
  1247. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1248. "suspend", status);
  1249. return status;
  1250. }
  1251. int hcd_bus_resume (struct usb_bus *bus)
  1252. {
  1253. struct usb_hcd *hcd;
  1254. int status;
  1255. hcd = container_of (bus, struct usb_hcd, self);
  1256. if (!hcd->driver->bus_resume)
  1257. return -ENOENT;
  1258. if (hcd->state == HC_STATE_RUNNING)
  1259. return 0;
  1260. hcd->state = HC_STATE_RESUMING;
  1261. status = hcd->driver->bus_resume (hcd);
  1262. if (status == 0)
  1263. hcd->state = HC_STATE_RUNNING;
  1264. else {
  1265. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1266. "resume", status);
  1267. usb_hc_died(hcd);
  1268. }
  1269. return status;
  1270. }
  1271. /*
  1272. * usb_hcd_suspend_root_hub - HCD autosuspends downstream ports
  1273. * @hcd: host controller for this root hub
  1274. *
  1275. * This call arranges that usb_hcd_resume_root_hub() is safe to call later;
  1276. * that the HCD's root hub polling is deactivated; and that the root's hub
  1277. * driver is suspended. HCDs may call this to autosuspend when their root
  1278. * hub's downstream ports are all inactive: unpowered, disconnected,
  1279. * disabled, or suspended.
  1280. *
  1281. * The HCD will autoresume on device connect change detection (using SRP
  1282. * or a D+/D- pullup). The HCD also autoresumes on remote wakeup signaling
  1283. * from any ports that are suspended (if that is enabled). In most cases,
  1284. * overcurrent signaling (on powered ports) will also start autoresume.
  1285. *
  1286. * Always called with IRQs blocked.
  1287. */
  1288. void usb_hcd_suspend_root_hub (struct usb_hcd *hcd)
  1289. {
  1290. struct urb *urb;
  1291. spin_lock (&hcd_root_hub_lock);
  1292. usb_suspend_root_hub (hcd->self.root_hub);
  1293. /* force status urb to complete/unlink while suspended */
  1294. if (hcd->status_urb) {
  1295. urb = hcd->status_urb;
  1296. urb->status = -ECONNRESET;
  1297. urb->hcpriv = NULL;
  1298. urb->actual_length = 0;
  1299. del_timer (&hcd->rh_timer);
  1300. hcd->poll_pending = 0;
  1301. hcd->status_urb = NULL;
  1302. } else
  1303. urb = NULL;
  1304. spin_unlock (&hcd_root_hub_lock);
  1305. hcd->state = HC_STATE_SUSPENDED;
  1306. if (urb)
  1307. usb_hcd_giveback_urb (hcd, urb, NULL);
  1308. }
  1309. EXPORT_SYMBOL_GPL(usb_hcd_suspend_root_hub);
  1310. /**
  1311. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1312. * @hcd: host controller for this root hub
  1313. *
  1314. * The USB host controller calls this function when its root hub is
  1315. * suspended (with the remote wakeup feature enabled) and a remote
  1316. * wakeup request is received. It queues a request for khubd to
  1317. * resume the root hub (that is, manage its downstream ports again).
  1318. */
  1319. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1320. {
  1321. unsigned long flags;
  1322. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1323. if (hcd->rh_registered)
  1324. usb_resume_root_hub (hcd->self.root_hub);
  1325. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1326. }
  1327. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1328. #endif
  1329. /*-------------------------------------------------------------------------*/
  1330. #ifdef CONFIG_USB_OTG
  1331. /**
  1332. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1333. * @bus: the bus (must use hcd framework)
  1334. * @port_num: 1-based number of port; usually bus->otg_port
  1335. * Context: in_interrupt()
  1336. *
  1337. * Starts enumeration, with an immediate reset followed later by
  1338. * khubd identifying and possibly configuring the device.
  1339. * This is needed by OTG controller drivers, where it helps meet
  1340. * HNP protocol timing requirements for starting a port reset.
  1341. */
  1342. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1343. {
  1344. struct usb_hcd *hcd;
  1345. int status = -EOPNOTSUPP;
  1346. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1347. * boards with root hubs hooked up to internal devices (instead of
  1348. * just the OTG port) may need more attention to resetting...
  1349. */
  1350. hcd = container_of (bus, struct usb_hcd, self);
  1351. if (port_num && hcd->driver->start_port_reset)
  1352. status = hcd->driver->start_port_reset(hcd, port_num);
  1353. /* run khubd shortly after (first) root port reset finishes;
  1354. * it may issue others, until at least 50 msecs have passed.
  1355. */
  1356. if (status == 0)
  1357. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1358. return status;
  1359. }
  1360. EXPORT_SYMBOL (usb_bus_start_enum);
  1361. #endif
  1362. /*-------------------------------------------------------------------------*/
  1363. /*
  1364. * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue)
  1365. */
  1366. static struct usb_operations usb_hcd_operations = {
  1367. .get_frame_number = hcd_get_frame_number,
  1368. .submit_urb = hcd_submit_urb,
  1369. .unlink_urb = hcd_unlink_urb,
  1370. .buffer_alloc = hcd_buffer_alloc,
  1371. .buffer_free = hcd_buffer_free,
  1372. .disable = hcd_endpoint_disable,
  1373. };
  1374. /*-------------------------------------------------------------------------*/
  1375. /**
  1376. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1377. * @hcd: host controller returning the URB
  1378. * @urb: urb being returned to the USB device driver.
  1379. * @regs: pt_regs, passed down to the URB completion handler
  1380. * Context: in_interrupt()
  1381. *
  1382. * This hands the URB from HCD to its USB device driver, using its
  1383. * completion function. The HCD has freed all per-urb resources
  1384. * (and is done using urb->hcpriv). It also released all HCD locks;
  1385. * the device driver won't cause problems if it frees, modifies,
  1386. * or resubmits this URB.
  1387. */
  1388. void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
  1389. {
  1390. int at_root_hub;
  1391. at_root_hub = (urb->dev == hcd->self.root_hub);
  1392. urb_unlink (urb);
  1393. /* lower level hcd code should use *_dma exclusively */
  1394. if (hcd->self.controller->dma_mask && !at_root_hub) {
  1395. if (usb_pipecontrol (urb->pipe)
  1396. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1397. dma_unmap_single (hcd->self.controller, urb->setup_dma,
  1398. sizeof (struct usb_ctrlrequest),
  1399. DMA_TO_DEVICE);
  1400. if (urb->transfer_buffer_length != 0
  1401. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1402. dma_unmap_single (hcd->self.controller,
  1403. urb->transfer_dma,
  1404. urb->transfer_buffer_length,
  1405. usb_pipein (urb->pipe)
  1406. ? DMA_FROM_DEVICE
  1407. : DMA_TO_DEVICE);
  1408. }
  1409. usbmon_urb_complete (&hcd->self, urb);
  1410. /* pass ownership to the completion handler */
  1411. urb->complete (urb, regs);
  1412. atomic_dec (&urb->use_count);
  1413. if (unlikely (urb->reject))
  1414. wake_up (&usb_kill_urb_queue);
  1415. usb_put_urb (urb);
  1416. }
  1417. EXPORT_SYMBOL (usb_hcd_giveback_urb);
  1418. /*-------------------------------------------------------------------------*/
  1419. /**
  1420. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1421. * @irq: the IRQ being raised
  1422. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1423. * @r: saved hardware registers
  1424. *
  1425. * If the controller isn't HALTed, calls the driver's irq handler.
  1426. * Checks whether the controller is now dead.
  1427. */
  1428. irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
  1429. {
  1430. struct usb_hcd *hcd = __hcd;
  1431. int start = hcd->state;
  1432. if (unlikely(start == HC_STATE_HALT ||
  1433. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
  1434. return IRQ_NONE;
  1435. if (hcd->driver->irq (hcd, r) == IRQ_NONE)
  1436. return IRQ_NONE;
  1437. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1438. if (unlikely(hcd->state == HC_STATE_HALT))
  1439. usb_hc_died (hcd);
  1440. return IRQ_HANDLED;
  1441. }
  1442. /*-------------------------------------------------------------------------*/
  1443. /**
  1444. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1445. * @hcd: pointer to the HCD representing the controller
  1446. *
  1447. * This is called by bus glue to report a USB host controller that died
  1448. * while operations may still have been pending. It's called automatically
  1449. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1450. */
  1451. void usb_hc_died (struct usb_hcd *hcd)
  1452. {
  1453. unsigned long flags;
  1454. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1455. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1456. if (hcd->rh_registered) {
  1457. hcd->poll_rh = 0;
  1458. /* make khubd clean up old urbs and devices */
  1459. usb_set_device_state (hcd->self.root_hub,
  1460. USB_STATE_NOTATTACHED);
  1461. usb_kick_khubd (hcd->self.root_hub);
  1462. }
  1463. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1464. }
  1465. EXPORT_SYMBOL_GPL (usb_hc_died);
  1466. /*-------------------------------------------------------------------------*/
  1467. static void hcd_release (struct usb_bus *bus)
  1468. {
  1469. struct usb_hcd *hcd;
  1470. hcd = container_of(bus, struct usb_hcd, self);
  1471. kfree(hcd);
  1472. }
  1473. /**
  1474. * usb_create_hcd - create and initialize an HCD structure
  1475. * @driver: HC driver that will use this hcd
  1476. * @dev: device for this HC, stored in hcd->self.controller
  1477. * @bus_name: value to store in hcd->self.bus_name
  1478. * Context: !in_interrupt()
  1479. *
  1480. * Allocate a struct usb_hcd, with extra space at the end for the
  1481. * HC driver's private data. Initialize the generic members of the
  1482. * hcd structure.
  1483. *
  1484. * If memory is unavailable, returns NULL.
  1485. */
  1486. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1487. struct device *dev, char *bus_name)
  1488. {
  1489. struct usb_hcd *hcd;
  1490. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1491. if (!hcd) {
  1492. dev_dbg (dev, "hcd alloc failed\n");
  1493. return NULL;
  1494. }
  1495. dev_set_drvdata(dev, hcd);
  1496. usb_bus_init(&hcd->self);
  1497. hcd->self.op = &usb_hcd_operations;
  1498. hcd->self.hcpriv = hcd;
  1499. hcd->self.release = &hcd_release;
  1500. hcd->self.controller = dev;
  1501. hcd->self.bus_name = bus_name;
  1502. init_timer(&hcd->rh_timer);
  1503. hcd->rh_timer.function = rh_timer_func;
  1504. hcd->rh_timer.data = (unsigned long) hcd;
  1505. hcd->driver = driver;
  1506. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1507. "USB Host Controller";
  1508. return hcd;
  1509. }
  1510. EXPORT_SYMBOL (usb_create_hcd);
  1511. void usb_put_hcd (struct usb_hcd *hcd)
  1512. {
  1513. dev_set_drvdata(hcd->self.controller, NULL);
  1514. usb_bus_put(&hcd->self);
  1515. }
  1516. EXPORT_SYMBOL (usb_put_hcd);
  1517. /**
  1518. * usb_add_hcd - finish generic HCD structure initialization and register
  1519. * @hcd: the usb_hcd structure to initialize
  1520. * @irqnum: Interrupt line to allocate
  1521. * @irqflags: Interrupt type flags
  1522. *
  1523. * Finish the remaining parts of generic HCD initialization: allocate the
  1524. * buffers of consistent memory, register the bus, request the IRQ line,
  1525. * and call the driver's reset() and start() routines.
  1526. */
  1527. int usb_add_hcd(struct usb_hcd *hcd,
  1528. unsigned int irqnum, unsigned long irqflags)
  1529. {
  1530. int retval;
  1531. struct usb_device *rhdev;
  1532. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1533. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1534. /* till now HC has been in an indeterminate state ... */
  1535. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1536. dev_err(hcd->self.controller, "can't reset\n");
  1537. return retval;
  1538. }
  1539. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1540. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1541. return retval;
  1542. }
  1543. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1544. goto err_register_bus;
  1545. if (hcd->driver->irq) {
  1546. char buf[8], *bufp = buf;
  1547. #ifdef __sparc__
  1548. bufp = __irq_itoa(irqnum);
  1549. #else
  1550. sprintf(buf, "%d", irqnum);
  1551. #endif
  1552. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1553. hcd->driver->description, hcd->self.busnum);
  1554. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1555. hcd->irq_descr, hcd)) != 0) {
  1556. dev_err(hcd->self.controller,
  1557. "request interrupt %s failed\n", bufp);
  1558. goto err_request_irq;
  1559. }
  1560. hcd->irq = irqnum;
  1561. dev_info(hcd->self.controller, "irq %s, %s 0x%08llx\n", bufp,
  1562. (hcd->driver->flags & HCD_MEMORY) ?
  1563. "io mem" : "io base",
  1564. (unsigned long long)hcd->rsrc_start);
  1565. } else {
  1566. hcd->irq = -1;
  1567. if (hcd->rsrc_start)
  1568. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1569. (hcd->driver->flags & HCD_MEMORY) ?
  1570. "io mem" : "io base",
  1571. (unsigned long long)hcd->rsrc_start);
  1572. }
  1573. /* Allocate the root hub before calling hcd->driver->start(),
  1574. * but don't register it until afterward so that the hardware
  1575. * is running.
  1576. */
  1577. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1578. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1579. retval = -ENOMEM;
  1580. goto err_allocate_root_hub;
  1581. }
  1582. /* Although in principle hcd->driver->start() might need to use rhdev,
  1583. * none of the current drivers do.
  1584. */
  1585. if ((retval = hcd->driver->start(hcd)) < 0) {
  1586. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1587. goto err_hcd_driver_start;
  1588. }
  1589. /* hcd->driver->start() reported can_wakeup, probably with
  1590. * assistance from board's boot firmware.
  1591. * NOTE: normal devices won't enable wakeup by default.
  1592. */
  1593. if (hcd->can_wakeup)
  1594. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1595. hcd->remote_wakeup = hcd->can_wakeup;
  1596. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1597. USB_SPEED_FULL;
  1598. rhdev->bus_mA = min(500u, hcd->power_budget);
  1599. if ((retval = register_root_hub(rhdev, hcd)) != 0)
  1600. goto err_register_root_hub;
  1601. if (hcd->uses_new_polling && hcd->poll_rh)
  1602. usb_hcd_poll_rh_status(hcd);
  1603. return retval;
  1604. err_register_root_hub:
  1605. hcd->driver->stop(hcd);
  1606. err_hcd_driver_start:
  1607. usb_put_dev(rhdev);
  1608. err_allocate_root_hub:
  1609. if (hcd->irq >= 0)
  1610. free_irq(irqnum, hcd);
  1611. err_request_irq:
  1612. usb_deregister_bus(&hcd->self);
  1613. err_register_bus:
  1614. hcd_buffer_destroy(hcd);
  1615. return retval;
  1616. }
  1617. EXPORT_SYMBOL (usb_add_hcd);
  1618. /**
  1619. * usb_remove_hcd - shutdown processing for generic HCDs
  1620. * @hcd: the usb_hcd structure to remove
  1621. * Context: !in_interrupt()
  1622. *
  1623. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1624. * invoking the HCD's stop() method.
  1625. */
  1626. void usb_remove_hcd(struct usb_hcd *hcd)
  1627. {
  1628. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1629. if (HC_IS_RUNNING (hcd->state))
  1630. hcd->state = HC_STATE_QUIESCING;
  1631. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1632. spin_lock_irq (&hcd_root_hub_lock);
  1633. hcd->rh_registered = 0;
  1634. spin_unlock_irq (&hcd_root_hub_lock);
  1635. mutex_lock(&usb_bus_list_lock);
  1636. usb_disconnect(&hcd->self.root_hub);
  1637. mutex_unlock(&usb_bus_list_lock);
  1638. hcd->poll_rh = 0;
  1639. del_timer_sync(&hcd->rh_timer);
  1640. hcd->driver->stop(hcd);
  1641. hcd->state = HC_STATE_HALT;
  1642. if (hcd->irq >= 0)
  1643. free_irq(hcd->irq, hcd);
  1644. usb_deregister_bus(&hcd->self);
  1645. hcd_buffer_destroy(hcd);
  1646. }
  1647. EXPORT_SYMBOL (usb_remove_hcd);
  1648. /*-------------------------------------------------------------------------*/
  1649. #if defined(CONFIG_USB_MON)
  1650. struct usb_mon_operations *mon_ops;
  1651. /*
  1652. * The registration is unlocked.
  1653. * We do it this way because we do not want to lock in hot paths.
  1654. *
  1655. * Notice that the code is minimally error-proof. Because usbmon needs
  1656. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1657. */
  1658. int usb_mon_register (struct usb_mon_operations *ops)
  1659. {
  1660. if (mon_ops)
  1661. return -EBUSY;
  1662. mon_ops = ops;
  1663. mb();
  1664. return 0;
  1665. }
  1666. EXPORT_SYMBOL_GPL (usb_mon_register);
  1667. void usb_mon_deregister (void)
  1668. {
  1669. if (mon_ops == NULL) {
  1670. printk(KERN_ERR "USB: monitor was not registered\n");
  1671. return;
  1672. }
  1673. mon_ops = NULL;
  1674. mb();
  1675. }
  1676. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1677. #endif /* CONFIG_USB_MON */