hcd.c 49 KB

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