hcd.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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. WARN_ON (!HC_IS_RUNNING (hcd->state) && hcd->state != HC_STATE_HALT &&
  1019. udev->state != USB_STATE_NOTATTACHED);
  1020. local_irq_disable ();
  1021. /* ep is already gone from udev->ep_{in,out}[]; no more submits */
  1022. rescan:
  1023. spin_lock (&hcd_data_lock);
  1024. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1025. int tmp;
  1026. /* the urb may already have been unlinked */
  1027. if (urb->status != -EINPROGRESS)
  1028. continue;
  1029. usb_get_urb (urb);
  1030. spin_unlock (&hcd_data_lock);
  1031. spin_lock (&urb->lock);
  1032. tmp = urb->status;
  1033. if (tmp == -EINPROGRESS)
  1034. urb->status = -ESHUTDOWN;
  1035. spin_unlock (&urb->lock);
  1036. /* kick hcd unless it's already returning this */
  1037. if (tmp == -EINPROGRESS) {
  1038. tmp = urb->pipe;
  1039. unlink1 (hcd, urb);
  1040. dev_dbg (hcd->self.controller,
  1041. "shutdown urb %p pipe %08x ep%d%s%s\n",
  1042. urb, tmp, usb_pipeendpoint (tmp),
  1043. (tmp & USB_DIR_IN) ? "in" : "out",
  1044. ({ char *s; \
  1045. switch (usb_pipetype (tmp)) { \
  1046. case PIPE_CONTROL: s = ""; break; \
  1047. case PIPE_BULK: s = "-bulk"; break; \
  1048. case PIPE_INTERRUPT: s = "-intr"; break; \
  1049. default: s = "-iso"; break; \
  1050. }; s;}));
  1051. }
  1052. usb_put_urb (urb);
  1053. /* list contents may have changed */
  1054. goto rescan;
  1055. }
  1056. spin_unlock (&hcd_data_lock);
  1057. local_irq_enable ();
  1058. /* synchronize with the hardware, so old configuration state
  1059. * clears out immediately (and will be freed).
  1060. */
  1061. might_sleep ();
  1062. if (hcd->driver->endpoint_disable)
  1063. hcd->driver->endpoint_disable (hcd, ep);
  1064. /* Wait until the endpoint queue is completely empty. Most HCDs
  1065. * will have done this already in their endpoint_disable method,
  1066. * but some might not. And there could be root-hub control URBs
  1067. * still pending since they aren't affected by the HCDs'
  1068. * endpoint_disable methods.
  1069. */
  1070. while (!list_empty (&ep->urb_list)) {
  1071. spin_lock_irq (&hcd_data_lock);
  1072. /* The list may have changed while we acquired the spinlock */
  1073. urb = NULL;
  1074. if (!list_empty (&ep->urb_list)) {
  1075. urb = list_entry (ep->urb_list.prev, struct urb,
  1076. urb_list);
  1077. usb_get_urb (urb);
  1078. }
  1079. spin_unlock_irq (&hcd_data_lock);
  1080. if (urb) {
  1081. usb_kill_urb (urb);
  1082. usb_put_urb (urb);
  1083. }
  1084. }
  1085. }
  1086. /*-------------------------------------------------------------------------*/
  1087. #ifdef CONFIG_PM
  1088. int hcd_bus_suspend (struct usb_bus *bus)
  1089. {
  1090. struct usb_hcd *hcd;
  1091. int status;
  1092. hcd = container_of (bus, struct usb_hcd, self);
  1093. if (!hcd->driver->bus_suspend)
  1094. return -ENOENT;
  1095. hcd->state = HC_STATE_QUIESCING;
  1096. status = hcd->driver->bus_suspend (hcd);
  1097. if (status == 0)
  1098. hcd->state = HC_STATE_SUSPENDED;
  1099. else
  1100. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1101. "suspend", status);
  1102. return status;
  1103. }
  1104. int hcd_bus_resume (struct usb_bus *bus)
  1105. {
  1106. struct usb_hcd *hcd;
  1107. int status;
  1108. hcd = container_of (bus, struct usb_hcd, self);
  1109. if (!hcd->driver->bus_resume)
  1110. return -ENOENT;
  1111. if (hcd->state == HC_STATE_RUNNING)
  1112. return 0;
  1113. hcd->state = HC_STATE_RESUMING;
  1114. status = hcd->driver->bus_resume (hcd);
  1115. if (status == 0)
  1116. hcd->state = HC_STATE_RUNNING;
  1117. else {
  1118. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1119. "resume", status);
  1120. usb_hc_died(hcd);
  1121. }
  1122. return status;
  1123. }
  1124. /* Workqueue routine for root-hub remote wakeup */
  1125. static void hcd_resume_work(struct work_struct *work)
  1126. {
  1127. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  1128. struct usb_device *udev = hcd->self.root_hub;
  1129. usb_lock_device(udev);
  1130. usb_mark_last_busy(udev);
  1131. usb_external_resume_device(udev);
  1132. usb_unlock_device(udev);
  1133. }
  1134. /**
  1135. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1136. * @hcd: host controller for this root hub
  1137. *
  1138. * The USB host controller calls this function when its root hub is
  1139. * suspended (with the remote wakeup feature enabled) and a remote
  1140. * wakeup request is received. The routine submits a workqueue request
  1141. * to resume the root hub (that is, manage its downstream ports again).
  1142. */
  1143. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1144. {
  1145. unsigned long flags;
  1146. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1147. if (hcd->rh_registered)
  1148. queue_work(ksuspend_usb_wq, &hcd->wakeup_work);
  1149. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1150. }
  1151. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1152. #endif
  1153. /*-------------------------------------------------------------------------*/
  1154. #ifdef CONFIG_USB_OTG
  1155. /**
  1156. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1157. * @bus: the bus (must use hcd framework)
  1158. * @port_num: 1-based number of port; usually bus->otg_port
  1159. * Context: in_interrupt()
  1160. *
  1161. * Starts enumeration, with an immediate reset followed later by
  1162. * khubd identifying and possibly configuring the device.
  1163. * This is needed by OTG controller drivers, where it helps meet
  1164. * HNP protocol timing requirements for starting a port reset.
  1165. */
  1166. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1167. {
  1168. struct usb_hcd *hcd;
  1169. int status = -EOPNOTSUPP;
  1170. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1171. * boards with root hubs hooked up to internal devices (instead of
  1172. * just the OTG port) may need more attention to resetting...
  1173. */
  1174. hcd = container_of (bus, struct usb_hcd, self);
  1175. if (port_num && hcd->driver->start_port_reset)
  1176. status = hcd->driver->start_port_reset(hcd, port_num);
  1177. /* run khubd shortly after (first) root port reset finishes;
  1178. * it may issue others, until at least 50 msecs have passed.
  1179. */
  1180. if (status == 0)
  1181. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1182. return status;
  1183. }
  1184. EXPORT_SYMBOL (usb_bus_start_enum);
  1185. #endif
  1186. /*-------------------------------------------------------------------------*/
  1187. /**
  1188. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1189. * @hcd: host controller returning the URB
  1190. * @urb: urb being returned to the USB device driver.
  1191. * Context: in_interrupt()
  1192. *
  1193. * This hands the URB from HCD to its USB device driver, using its
  1194. * completion function. The HCD has freed all per-urb resources
  1195. * (and is done using urb->hcpriv). It also released all HCD locks;
  1196. * the device driver won't cause problems if it frees, modifies,
  1197. * or resubmits this URB.
  1198. */
  1199. void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb)
  1200. {
  1201. int at_root_hub;
  1202. at_root_hub = (urb->dev == hcd->self.root_hub);
  1203. urb_unlink (urb);
  1204. /* lower level hcd code should use *_dma exclusively if the
  1205. * host controller does DMA */
  1206. if (hcd->self.uses_dma && !at_root_hub) {
  1207. if (usb_pipecontrol (urb->pipe)
  1208. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1209. dma_unmap_single (hcd->self.controller, urb->setup_dma,
  1210. sizeof (struct usb_ctrlrequest),
  1211. DMA_TO_DEVICE);
  1212. if (urb->transfer_buffer_length != 0
  1213. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1214. dma_unmap_single (hcd->self.controller,
  1215. urb->transfer_dma,
  1216. urb->transfer_buffer_length,
  1217. usb_pipein (urb->pipe)
  1218. ? DMA_FROM_DEVICE
  1219. : DMA_TO_DEVICE);
  1220. }
  1221. usbmon_urb_complete (&hcd->self, urb);
  1222. /* pass ownership to the completion handler */
  1223. urb->complete (urb);
  1224. atomic_dec (&urb->use_count);
  1225. if (unlikely (urb->reject))
  1226. wake_up (&usb_kill_urb_queue);
  1227. usb_put_urb (urb);
  1228. }
  1229. EXPORT_SYMBOL (usb_hcd_giveback_urb);
  1230. /*-------------------------------------------------------------------------*/
  1231. /**
  1232. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1233. * @irq: the IRQ being raised
  1234. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1235. * @r: saved hardware registers
  1236. *
  1237. * If the controller isn't HALTed, calls the driver's irq handler.
  1238. * Checks whether the controller is now dead.
  1239. */
  1240. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  1241. {
  1242. struct usb_hcd *hcd = __hcd;
  1243. int start = hcd->state;
  1244. if (unlikely(start == HC_STATE_HALT ||
  1245. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
  1246. return IRQ_NONE;
  1247. if (hcd->driver->irq (hcd) == IRQ_NONE)
  1248. return IRQ_NONE;
  1249. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1250. if (unlikely(hcd->state == HC_STATE_HALT))
  1251. usb_hc_died (hcd);
  1252. return IRQ_HANDLED;
  1253. }
  1254. /*-------------------------------------------------------------------------*/
  1255. /**
  1256. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1257. * @hcd: pointer to the HCD representing the controller
  1258. *
  1259. * This is called by bus glue to report a USB host controller that died
  1260. * while operations may still have been pending. It's called automatically
  1261. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1262. */
  1263. void usb_hc_died (struct usb_hcd *hcd)
  1264. {
  1265. unsigned long flags;
  1266. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1267. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1268. if (hcd->rh_registered) {
  1269. hcd->poll_rh = 0;
  1270. /* make khubd clean up old urbs and devices */
  1271. usb_set_device_state (hcd->self.root_hub,
  1272. USB_STATE_NOTATTACHED);
  1273. usb_kick_khubd (hcd->self.root_hub);
  1274. }
  1275. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1276. }
  1277. EXPORT_SYMBOL_GPL (usb_hc_died);
  1278. /*-------------------------------------------------------------------------*/
  1279. /**
  1280. * usb_create_hcd - create and initialize an HCD structure
  1281. * @driver: HC driver that will use this hcd
  1282. * @dev: device for this HC, stored in hcd->self.controller
  1283. * @bus_name: value to store in hcd->self.bus_name
  1284. * Context: !in_interrupt()
  1285. *
  1286. * Allocate a struct usb_hcd, with extra space at the end for the
  1287. * HC driver's private data. Initialize the generic members of the
  1288. * hcd structure.
  1289. *
  1290. * If memory is unavailable, returns NULL.
  1291. */
  1292. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1293. struct device *dev, char *bus_name)
  1294. {
  1295. struct usb_hcd *hcd;
  1296. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1297. if (!hcd) {
  1298. dev_dbg (dev, "hcd alloc failed\n");
  1299. return NULL;
  1300. }
  1301. dev_set_drvdata(dev, hcd);
  1302. kref_init(&hcd->kref);
  1303. usb_bus_init(&hcd->self);
  1304. hcd->self.controller = dev;
  1305. hcd->self.bus_name = bus_name;
  1306. hcd->self.uses_dma = (dev->dma_mask != NULL);
  1307. init_timer(&hcd->rh_timer);
  1308. hcd->rh_timer.function = rh_timer_func;
  1309. hcd->rh_timer.data = (unsigned long) hcd;
  1310. #ifdef CONFIG_PM
  1311. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  1312. #endif
  1313. hcd->driver = driver;
  1314. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1315. "USB Host Controller";
  1316. return hcd;
  1317. }
  1318. EXPORT_SYMBOL (usb_create_hcd);
  1319. static void hcd_release (struct kref *kref)
  1320. {
  1321. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  1322. kfree(hcd);
  1323. }
  1324. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  1325. {
  1326. if (hcd)
  1327. kref_get (&hcd->kref);
  1328. return hcd;
  1329. }
  1330. EXPORT_SYMBOL (usb_get_hcd);
  1331. void usb_put_hcd (struct usb_hcd *hcd)
  1332. {
  1333. if (hcd)
  1334. kref_put (&hcd->kref, hcd_release);
  1335. }
  1336. EXPORT_SYMBOL (usb_put_hcd);
  1337. /**
  1338. * usb_add_hcd - finish generic HCD structure initialization and register
  1339. * @hcd: the usb_hcd structure to initialize
  1340. * @irqnum: Interrupt line to allocate
  1341. * @irqflags: Interrupt type flags
  1342. *
  1343. * Finish the remaining parts of generic HCD initialization: allocate the
  1344. * buffers of consistent memory, register the bus, request the IRQ line,
  1345. * and call the driver's reset() and start() routines.
  1346. */
  1347. int usb_add_hcd(struct usb_hcd *hcd,
  1348. unsigned int irqnum, unsigned long irqflags)
  1349. {
  1350. int retval;
  1351. struct usb_device *rhdev;
  1352. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1353. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1354. /* HC is in reset state, but accessible. Now do the one-time init,
  1355. * bottom up so that hcds can customize the root hubs before khubd
  1356. * starts talking to them. (Note, bus id is assigned early too.)
  1357. */
  1358. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1359. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1360. return retval;
  1361. }
  1362. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1363. goto err_register_bus;
  1364. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1365. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1366. retval = -ENOMEM;
  1367. goto err_allocate_root_hub;
  1368. }
  1369. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1370. USB_SPEED_FULL;
  1371. hcd->self.root_hub = rhdev;
  1372. /* wakeup flag init defaults to "everything works" for root hubs,
  1373. * but drivers can override it in reset() if needed, along with
  1374. * recording the overall controller's system wakeup capability.
  1375. */
  1376. device_init_wakeup(&rhdev->dev, 1);
  1377. /* "reset" is misnamed; its role is now one-time init. the controller
  1378. * should already have been reset (and boot firmware kicked off etc).
  1379. */
  1380. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1381. dev_err(hcd->self.controller, "can't setup\n");
  1382. goto err_hcd_driver_setup;
  1383. }
  1384. /* NOTE: root hub and controller capabilities may not be the same */
  1385. if (device_can_wakeup(hcd->self.controller)
  1386. && device_can_wakeup(&hcd->self.root_hub->dev))
  1387. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1388. /* enable irqs just before we start the controller */
  1389. if (hcd->driver->irq) {
  1390. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1391. hcd->driver->description, hcd->self.busnum);
  1392. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1393. hcd->irq_descr, hcd)) != 0) {
  1394. dev_err(hcd->self.controller,
  1395. "request interrupt %d failed\n", irqnum);
  1396. goto err_request_irq;
  1397. }
  1398. hcd->irq = irqnum;
  1399. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  1400. (hcd->driver->flags & HCD_MEMORY) ?
  1401. "io mem" : "io base",
  1402. (unsigned long long)hcd->rsrc_start);
  1403. } else {
  1404. hcd->irq = -1;
  1405. if (hcd->rsrc_start)
  1406. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1407. (hcd->driver->flags & HCD_MEMORY) ?
  1408. "io mem" : "io base",
  1409. (unsigned long long)hcd->rsrc_start);
  1410. }
  1411. if ((retval = hcd->driver->start(hcd)) < 0) {
  1412. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1413. goto err_hcd_driver_start;
  1414. }
  1415. /* starting here, usbcore will pay attention to this root hub */
  1416. rhdev->bus_mA = min(500u, hcd->power_budget);
  1417. if ((retval = register_root_hub(hcd)) != 0)
  1418. goto err_register_root_hub;
  1419. if (hcd->uses_new_polling && hcd->poll_rh)
  1420. usb_hcd_poll_rh_status(hcd);
  1421. return retval;
  1422. err_register_root_hub:
  1423. hcd->driver->stop(hcd);
  1424. err_hcd_driver_start:
  1425. if (hcd->irq >= 0)
  1426. free_irq(irqnum, hcd);
  1427. err_request_irq:
  1428. err_hcd_driver_setup:
  1429. hcd->self.root_hub = NULL;
  1430. usb_put_dev(rhdev);
  1431. err_allocate_root_hub:
  1432. usb_deregister_bus(&hcd->self);
  1433. err_register_bus:
  1434. hcd_buffer_destroy(hcd);
  1435. return retval;
  1436. }
  1437. EXPORT_SYMBOL (usb_add_hcd);
  1438. /**
  1439. * usb_remove_hcd - shutdown processing for generic HCDs
  1440. * @hcd: the usb_hcd structure to remove
  1441. * Context: !in_interrupt()
  1442. *
  1443. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1444. * invoking the HCD's stop() method.
  1445. */
  1446. void usb_remove_hcd(struct usb_hcd *hcd)
  1447. {
  1448. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1449. if (HC_IS_RUNNING (hcd->state))
  1450. hcd->state = HC_STATE_QUIESCING;
  1451. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1452. spin_lock_irq (&hcd_root_hub_lock);
  1453. hcd->rh_registered = 0;
  1454. spin_unlock_irq (&hcd_root_hub_lock);
  1455. #ifdef CONFIG_PM
  1456. flush_workqueue(ksuspend_usb_wq);
  1457. #endif
  1458. mutex_lock(&usb_bus_list_lock);
  1459. usb_disconnect(&hcd->self.root_hub);
  1460. mutex_unlock(&usb_bus_list_lock);
  1461. hcd->driver->stop(hcd);
  1462. hcd->state = HC_STATE_HALT;
  1463. hcd->poll_rh = 0;
  1464. del_timer_sync(&hcd->rh_timer);
  1465. if (hcd->irq >= 0)
  1466. free_irq(hcd->irq, hcd);
  1467. usb_deregister_bus(&hcd->self);
  1468. hcd_buffer_destroy(hcd);
  1469. }
  1470. EXPORT_SYMBOL (usb_remove_hcd);
  1471. void
  1472. usb_hcd_platform_shutdown(struct platform_device* dev)
  1473. {
  1474. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1475. if (hcd->driver->shutdown)
  1476. hcd->driver->shutdown(hcd);
  1477. }
  1478. EXPORT_SYMBOL (usb_hcd_platform_shutdown);
  1479. /*-------------------------------------------------------------------------*/
  1480. #if defined(CONFIG_USB_MON)
  1481. struct usb_mon_operations *mon_ops;
  1482. /*
  1483. * The registration is unlocked.
  1484. * We do it this way because we do not want to lock in hot paths.
  1485. *
  1486. * Notice that the code is minimally error-proof. Because usbmon needs
  1487. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1488. */
  1489. int usb_mon_register (struct usb_mon_operations *ops)
  1490. {
  1491. if (mon_ops)
  1492. return -EBUSY;
  1493. mon_ops = ops;
  1494. mb();
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL_GPL (usb_mon_register);
  1498. void usb_mon_deregister (void)
  1499. {
  1500. if (mon_ops == NULL) {
  1501. printk(KERN_ERR "USB: monitor was not registered\n");
  1502. return;
  1503. }
  1504. mon_ops = NULL;
  1505. mb();
  1506. }
  1507. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1508. #endif /* CONFIG_USB_MON */