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