hcd.c 49 KB

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