hcd.c 51 KB

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