hcd.c 55 KB

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