hcd.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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. switch (typeReq) {
  398. case GetHubStatus:
  399. case GetPortStatus:
  400. len = 4;
  401. break;
  402. case GetHubDescriptor:
  403. len = sizeof (struct usb_hub_descriptor);
  404. break;
  405. }
  406. status = hcd->driver->hub_control (hcd,
  407. typeReq, wValue, wIndex,
  408. tbuf, wLength);
  409. break;
  410. error:
  411. /* "protocol stall" on error */
  412. status = -EPIPE;
  413. }
  414. if (status) {
  415. len = 0;
  416. if (status != -EPIPE) {
  417. dev_dbg (hcd->self.controller,
  418. "CTRL: TypeReq=0x%x val=0x%x "
  419. "idx=0x%x len=%d ==> %d\n",
  420. typeReq, wValue, wIndex,
  421. wLength, status);
  422. }
  423. }
  424. if (len) {
  425. if (urb->transfer_buffer_length < len)
  426. len = urb->transfer_buffer_length;
  427. urb->actual_length = len;
  428. // always USB_DIR_IN, toward host
  429. memcpy (ubuf, bufp, len);
  430. /* report whether RH hardware supports remote wakeup */
  431. if (patch_wakeup &&
  432. len > offsetof (struct usb_config_descriptor,
  433. bmAttributes))
  434. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  435. |= USB_CONFIG_ATT_WAKEUP;
  436. }
  437. /* any errors get returned through the urb completion */
  438. local_irq_save (flags);
  439. spin_lock (&urb->lock);
  440. if (urb->status == -EINPROGRESS)
  441. urb->status = status;
  442. spin_unlock (&urb->lock);
  443. usb_hcd_giveback_urb (hcd, urb, NULL);
  444. local_irq_restore (flags);
  445. return 0;
  446. }
  447. /*-------------------------------------------------------------------------*/
  448. /*
  449. * Root Hub interrupt transfers are polled using a timer if the
  450. * driver requests it; otherwise the driver is responsible for
  451. * calling usb_hcd_poll_rh_status() when an event occurs.
  452. *
  453. * Completions are called in_interrupt(), but they may or may not
  454. * be in_irq().
  455. */
  456. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  457. {
  458. struct urb *urb;
  459. int length;
  460. unsigned long flags;
  461. char buffer[4]; /* Any root hubs with > 31 ports? */
  462. if (!hcd->uses_new_polling && !hcd->status_urb)
  463. return;
  464. length = hcd->driver->hub_status_data(hcd, buffer);
  465. if (length > 0) {
  466. /* try to complete the status urb */
  467. local_irq_save (flags);
  468. spin_lock(&hcd_root_hub_lock);
  469. urb = hcd->status_urb;
  470. if (urb) {
  471. spin_lock(&urb->lock);
  472. if (urb->status == -EINPROGRESS) {
  473. hcd->poll_pending = 0;
  474. hcd->status_urb = NULL;
  475. urb->status = 0;
  476. urb->hcpriv = NULL;
  477. urb->actual_length = length;
  478. memcpy(urb->transfer_buffer, buffer, length);
  479. } else /* urb has been unlinked */
  480. length = 0;
  481. spin_unlock(&urb->lock);
  482. } else
  483. length = 0;
  484. spin_unlock(&hcd_root_hub_lock);
  485. /* local irqs are always blocked in completions */
  486. if (length > 0)
  487. usb_hcd_giveback_urb (hcd, urb, NULL);
  488. else
  489. hcd->poll_pending = 1;
  490. local_irq_restore (flags);
  491. }
  492. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  493. * exceed that limit if HZ is 100. */
  494. if (hcd->uses_new_polling ? hcd->poll_rh :
  495. (length == 0 && hcd->status_urb != NULL))
  496. mod_timer (&hcd->rh_timer, jiffies + msecs_to_jiffies(250));
  497. }
  498. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  499. /* timer callback */
  500. static void rh_timer_func (unsigned long _hcd)
  501. {
  502. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  503. }
  504. /*-------------------------------------------------------------------------*/
  505. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  506. {
  507. int retval;
  508. unsigned long flags;
  509. int len = 1 + (urb->dev->maxchild / 8);
  510. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  511. if (urb->status != -EINPROGRESS) /* already unlinked */
  512. retval = urb->status;
  513. else if (hcd->status_urb || urb->transfer_buffer_length < len) {
  514. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  515. retval = -EINVAL;
  516. } else {
  517. hcd->status_urb = urb;
  518. urb->hcpriv = hcd; /* indicate it's queued */
  519. if (!hcd->uses_new_polling)
  520. mod_timer (&hcd->rh_timer, jiffies +
  521. msecs_to_jiffies(250));
  522. /* If a status change has already occurred, report it ASAP */
  523. else if (hcd->poll_pending)
  524. mod_timer (&hcd->rh_timer, jiffies);
  525. retval = 0;
  526. }
  527. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  528. return retval;
  529. }
  530. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  531. {
  532. if (usb_pipeint (urb->pipe))
  533. return rh_queue_status (hcd, urb);
  534. if (usb_pipecontrol (urb->pipe))
  535. return rh_call_control (hcd, urb);
  536. return -EINVAL;
  537. }
  538. /*-------------------------------------------------------------------------*/
  539. /* Asynchronous unlinks of root-hub control URBs are legal, but they
  540. * don't do anything. Status URB unlinks must be made in process context
  541. * with interrupts enabled.
  542. */
  543. static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  544. {
  545. if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */
  546. if (in_interrupt())
  547. return 0; /* nothing to do */
  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 { /* Status URB */
  557. if (!hcd->uses_new_polling)
  558. del_timer_sync (&hcd->rh_timer);
  559. local_irq_disable ();
  560. spin_lock (&hcd_root_hub_lock);
  561. if (urb == hcd->status_urb) {
  562. hcd->status_urb = NULL;
  563. urb->hcpriv = NULL;
  564. } else
  565. urb = NULL; /* wasn't fully queued */
  566. spin_unlock (&hcd_root_hub_lock);
  567. if (urb)
  568. usb_hcd_giveback_urb (hcd, urb, NULL);
  569. local_irq_enable ();
  570. }
  571. return 0;
  572. }
  573. /*-------------------------------------------------------------------------*/
  574. /* exported only within usbcore */
  575. struct usb_bus *usb_bus_get(struct usb_bus *bus)
  576. {
  577. if (bus)
  578. kref_get(&bus->kref);
  579. return bus;
  580. }
  581. static void usb_host_release(struct kref *kref)
  582. {
  583. struct usb_bus *bus = container_of(kref, struct usb_bus, kref);
  584. if (bus->release)
  585. bus->release(bus);
  586. }
  587. /* exported only within usbcore */
  588. void usb_bus_put(struct usb_bus *bus)
  589. {
  590. if (bus)
  591. kref_put(&bus->kref, usb_host_release);
  592. }
  593. /*-------------------------------------------------------------------------*/
  594. static struct class *usb_host_class;
  595. int usb_host_init(void)
  596. {
  597. int retval = 0;
  598. usb_host_class = class_create(THIS_MODULE, "usb_host");
  599. if (IS_ERR(usb_host_class))
  600. retval = PTR_ERR(usb_host_class);
  601. return retval;
  602. }
  603. void usb_host_cleanup(void)
  604. {
  605. class_destroy(usb_host_class);
  606. }
  607. /**
  608. * usb_bus_init - shared initialization code
  609. * @bus: the bus structure being initialized
  610. *
  611. * This code is used to initialize a usb_bus structure, memory for which is
  612. * separately managed.
  613. */
  614. static void usb_bus_init (struct usb_bus *bus)
  615. {
  616. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  617. bus->devnum_next = 1;
  618. bus->root_hub = NULL;
  619. bus->hcpriv = NULL;
  620. bus->busnum = -1;
  621. bus->bandwidth_allocated = 0;
  622. bus->bandwidth_int_reqs = 0;
  623. bus->bandwidth_isoc_reqs = 0;
  624. INIT_LIST_HEAD (&bus->bus_list);
  625. kref_init(&bus->kref);
  626. }
  627. /**
  628. * usb_alloc_bus - creates a new USB host controller structure
  629. * @op: pointer to a struct usb_operations that this bus structure should use
  630. * Context: !in_interrupt()
  631. *
  632. * Creates a USB host controller bus structure with the specified
  633. * usb_operations and initializes all the necessary internal objects.
  634. *
  635. * If no memory is available, NULL is returned.
  636. *
  637. * The caller should call usb_put_bus() when it is finished with the structure.
  638. */
  639. struct usb_bus *usb_alloc_bus (struct usb_operations *op)
  640. {
  641. struct usb_bus *bus;
  642. bus = kmalloc (sizeof *bus, GFP_KERNEL);
  643. if (!bus)
  644. return NULL;
  645. memset(bus, 0, sizeof(struct usb_bus));
  646. usb_bus_init (bus);
  647. bus->op = op;
  648. return bus;
  649. }
  650. /*-------------------------------------------------------------------------*/
  651. /**
  652. * usb_register_bus - registers the USB host controller with the usb core
  653. * @bus: pointer to the bus to register
  654. * Context: !in_interrupt()
  655. *
  656. * Assigns a bus number, and links the controller into usbcore data
  657. * structures so that it can be seen by scanning the bus list.
  658. */
  659. static int usb_register_bus(struct usb_bus *bus)
  660. {
  661. int busnum;
  662. down (&usb_bus_list_lock);
  663. busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  664. if (busnum < USB_MAXBUS) {
  665. set_bit (busnum, busmap.busmap);
  666. bus->busnum = busnum;
  667. } else {
  668. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  669. up(&usb_bus_list_lock);
  670. return -E2BIG;
  671. }
  672. bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0),
  673. bus->controller, "usb_host%d", busnum);
  674. if (IS_ERR(bus->class_dev)) {
  675. clear_bit(busnum, busmap.busmap);
  676. up(&usb_bus_list_lock);
  677. return PTR_ERR(bus->class_dev);
  678. }
  679. class_set_devdata(bus->class_dev, bus);
  680. /* Add it to the local list of buses */
  681. list_add (&bus->bus_list, &usb_bus_list);
  682. up (&usb_bus_list_lock);
  683. usb_notify_add_bus(bus);
  684. dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
  685. return 0;
  686. }
  687. /**
  688. * usb_deregister_bus - deregisters the USB host controller
  689. * @bus: pointer to the bus to deregister
  690. * Context: !in_interrupt()
  691. *
  692. * Recycles the bus number, and unlinks the controller from usbcore data
  693. * structures so that it won't be seen by scanning the bus list.
  694. */
  695. static void usb_deregister_bus (struct usb_bus *bus)
  696. {
  697. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  698. /*
  699. * NOTE: make sure that all the devices are removed by the
  700. * controller code, as well as having it call this when cleaning
  701. * itself up
  702. */
  703. down (&usb_bus_list_lock);
  704. list_del (&bus->bus_list);
  705. up (&usb_bus_list_lock);
  706. usb_notify_remove_bus(bus);
  707. clear_bit (bus->busnum, busmap.busmap);
  708. class_device_unregister(bus->class_dev);
  709. }
  710. /**
  711. * register_root_hub - called by usb_add_hcd() to register a root hub
  712. * @usb_dev: the usb root hub device to be registered.
  713. * @hcd: host controller for this root hub
  714. *
  715. * This function registers the root hub with the USB subsystem. It sets up
  716. * the device properly in the device tree and stores the root_hub pointer
  717. * in the bus structure, then calls usb_new_device() to register the usb
  718. * device. It also assigns the root hub's USB address (always 1).
  719. */
  720. static int register_root_hub (struct usb_device *usb_dev,
  721. struct usb_hcd *hcd)
  722. {
  723. struct device *parent_dev = hcd->self.controller;
  724. const int devnum = 1;
  725. int retval;
  726. usb_dev->devnum = devnum;
  727. usb_dev->bus->devnum_next = devnum + 1;
  728. memset (&usb_dev->bus->devmap.devicemap, 0,
  729. sizeof usb_dev->bus->devmap.devicemap);
  730. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  731. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  732. down (&usb_bus_list_lock);
  733. usb_dev->bus->root_hub = usb_dev;
  734. usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  735. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  736. if (retval != sizeof usb_dev->descriptor) {
  737. usb_dev->bus->root_hub = NULL;
  738. up (&usb_bus_list_lock);
  739. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  740. usb_dev->dev.bus_id, retval);
  741. return (retval < 0) ? retval : -EMSGSIZE;
  742. }
  743. usb_lock_device (usb_dev);
  744. retval = usb_new_device (usb_dev);
  745. usb_unlock_device (usb_dev);
  746. if (retval) {
  747. usb_dev->bus->root_hub = NULL;
  748. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  749. usb_dev->dev.bus_id, retval);
  750. }
  751. up (&usb_bus_list_lock);
  752. if (retval == 0) {
  753. spin_lock_irq (&hcd_root_hub_lock);
  754. hcd->rh_registered = 1;
  755. spin_unlock_irq (&hcd_root_hub_lock);
  756. /* Did the HC die before the root hub was registered? */
  757. if (hcd->state == HC_STATE_HALT)
  758. usb_hc_died (hcd); /* This time clean up */
  759. }
  760. return retval;
  761. }
  762. void usb_enable_root_hub_irq (struct usb_bus *bus)
  763. {
  764. struct usb_hcd *hcd;
  765. hcd = container_of (bus, struct usb_hcd, self);
  766. if (hcd->driver->hub_irq_enable && !hcd->poll_rh &&
  767. hcd->state != HC_STATE_HALT)
  768. hcd->driver->hub_irq_enable (hcd);
  769. }
  770. /*-------------------------------------------------------------------------*/
  771. /**
  772. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  773. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  774. * @is_input: true iff the transaction sends data to the host
  775. * @isoc: true for isochronous transactions, false for interrupt ones
  776. * @bytecount: how many bytes in the transaction.
  777. *
  778. * Returns approximate bus time in nanoseconds for a periodic transaction.
  779. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  780. * scheduled in software, this function is only used for such scheduling.
  781. */
  782. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  783. {
  784. unsigned long tmp;
  785. switch (speed) {
  786. case USB_SPEED_LOW: /* INTR only */
  787. if (is_input) {
  788. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  789. return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  790. } else {
  791. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  792. return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  793. }
  794. case USB_SPEED_FULL: /* ISOC or INTR */
  795. if (isoc) {
  796. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  797. return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
  798. } else {
  799. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  800. return (9107L + BW_HOST_DELAY + tmp);
  801. }
  802. case USB_SPEED_HIGH: /* ISOC or INTR */
  803. // FIXME adjust for input vs output
  804. if (isoc)
  805. tmp = HS_NSECS_ISO (bytecount);
  806. else
  807. tmp = HS_NSECS (bytecount);
  808. return tmp;
  809. default:
  810. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  811. return -1;
  812. }
  813. }
  814. EXPORT_SYMBOL (usb_calc_bus_time);
  815. /*
  816. * usb_check_bandwidth():
  817. *
  818. * old_alloc is from host_controller->bandwidth_allocated in microseconds;
  819. * bustime is from calc_bus_time(), but converted to microseconds.
  820. *
  821. * returns <bustime in us> if successful,
  822. * or -ENOSPC if bandwidth request fails.
  823. *
  824. * FIXME:
  825. * This initial implementation does not use Endpoint.bInterval
  826. * in managing bandwidth allocation.
  827. * It probably needs to be expanded to use Endpoint.bInterval.
  828. * This can be done as a later enhancement (correction).
  829. *
  830. * This will also probably require some kind of
  831. * frame allocation tracking...meaning, for example,
  832. * that if multiple drivers request interrupts every 10 USB frames,
  833. * they don't all have to be allocated at
  834. * frame numbers N, N+10, N+20, etc. Some of them could be at
  835. * N+11, N+21, N+31, etc., and others at
  836. * N+12, N+22, N+32, etc.
  837. *
  838. * Similarly for isochronous transfers...
  839. *
  840. * Individual HCDs can schedule more directly ... this logic
  841. * is not correct for high speed transfers.
  842. */
  843. int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
  844. {
  845. unsigned int pipe = urb->pipe;
  846. long bustime;
  847. int is_in = usb_pipein (pipe);
  848. int is_iso = usb_pipeisoc (pipe);
  849. int old_alloc = dev->bus->bandwidth_allocated;
  850. int new_alloc;
  851. bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
  852. usb_maxpacket (dev, pipe, !is_in)));
  853. if (is_iso)
  854. bustime /= urb->number_of_packets;
  855. new_alloc = old_alloc + (int) bustime;
  856. if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
  857. #ifdef DEBUG
  858. char *mode =
  859. #ifdef CONFIG_USB_BANDWIDTH
  860. "";
  861. #else
  862. "would have ";
  863. #endif
  864. dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
  865. mode, old_alloc, bustime, new_alloc);
  866. #endif
  867. #ifdef CONFIG_USB_BANDWIDTH
  868. bustime = -ENOSPC; /* report error */
  869. #endif
  870. }
  871. return bustime;
  872. }
  873. EXPORT_SYMBOL (usb_check_bandwidth);
  874. /**
  875. * usb_claim_bandwidth - records bandwidth for a periodic transfer
  876. * @dev: source/target of request
  877. * @urb: request (urb->dev == dev)
  878. * @bustime: bandwidth consumed, in (average) microseconds per frame
  879. * @isoc: true iff the request is isochronous
  880. *
  881. * Bus bandwidth reservations are recorded purely for diagnostic purposes.
  882. * HCDs are expected not to overcommit periodic bandwidth, and to record such
  883. * reservations whenever endpoints are added to the periodic schedule.
  884. *
  885. * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
  886. * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
  887. * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
  888. * large its periodic schedule is.
  889. */
  890. void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
  891. {
  892. dev->bus->bandwidth_allocated += bustime;
  893. if (isoc)
  894. dev->bus->bandwidth_isoc_reqs++;
  895. else
  896. dev->bus->bandwidth_int_reqs++;
  897. urb->bandwidth = bustime;
  898. #ifdef USB_BANDWIDTH_MESSAGES
  899. dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
  900. bustime,
  901. isoc ? "ISOC" : "INTR",
  902. dev->bus->bandwidth_allocated,
  903. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  904. #endif
  905. }
  906. EXPORT_SYMBOL (usb_claim_bandwidth);
  907. /**
  908. * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
  909. * @dev: source/target of request
  910. * @urb: request (urb->dev == dev)
  911. * @isoc: true iff the request is isochronous
  912. *
  913. * This records that previously allocated bandwidth has been released.
  914. * Bandwidth is released when endpoints are removed from the host controller's
  915. * periodic schedule.
  916. */
  917. void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
  918. {
  919. dev->bus->bandwidth_allocated -= urb->bandwidth;
  920. if (isoc)
  921. dev->bus->bandwidth_isoc_reqs--;
  922. else
  923. dev->bus->bandwidth_int_reqs--;
  924. #ifdef USB_BANDWIDTH_MESSAGES
  925. dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
  926. urb->bandwidth,
  927. isoc ? "ISOC" : "INTR",
  928. dev->bus->bandwidth_allocated,
  929. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  930. #endif
  931. urb->bandwidth = 0;
  932. }
  933. EXPORT_SYMBOL (usb_release_bandwidth);
  934. /*-------------------------------------------------------------------------*/
  935. /*
  936. * Generic HC operations.
  937. */
  938. /*-------------------------------------------------------------------------*/
  939. static void urb_unlink (struct urb *urb)
  940. {
  941. unsigned long flags;
  942. /* Release any periodic transfer bandwidth */
  943. if (urb->bandwidth)
  944. usb_release_bandwidth (urb->dev, urb,
  945. usb_pipeisoc (urb->pipe));
  946. /* clear all state linking urb to this dev (and hcd) */
  947. spin_lock_irqsave (&hcd_data_lock, flags);
  948. list_del_init (&urb->urb_list);
  949. spin_unlock_irqrestore (&hcd_data_lock, flags);
  950. usb_put_dev (urb->dev);
  951. }
  952. /* may be called in any context with a valid urb->dev usecount
  953. * caller surrenders "ownership" of urb
  954. * expects usb_submit_urb() to have sanity checked and conditioned all
  955. * inputs in the urb
  956. */
  957. static int hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  958. {
  959. int status;
  960. struct usb_hcd *hcd = urb->dev->bus->hcpriv;
  961. struct usb_host_endpoint *ep;
  962. unsigned long flags;
  963. if (!hcd)
  964. return -ENODEV;
  965. usbmon_urb_submit(&hcd->self, urb);
  966. /*
  967. * Atomically queue the urb, first to our records, then to the HCD.
  968. * Access to urb->status is controlled by urb->lock ... changes on
  969. * i/o completion (normal or fault) or unlinking.
  970. */
  971. // FIXME: verify that quiescing hc works right (RH cleans up)
  972. spin_lock_irqsave (&hcd_data_lock, flags);
  973. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  974. [usb_pipeendpoint(urb->pipe)];
  975. if (unlikely (!ep))
  976. status = -ENOENT;
  977. else if (unlikely (urb->reject))
  978. status = -EPERM;
  979. else switch (hcd->state) {
  980. case HC_STATE_RUNNING:
  981. case HC_STATE_RESUMING:
  982. doit:
  983. usb_get_dev (urb->dev);
  984. list_add_tail (&urb->urb_list, &ep->urb_list);
  985. status = 0;
  986. break;
  987. case HC_STATE_SUSPENDED:
  988. /* HC upstream links (register access, wakeup signaling) can work
  989. * even when the downstream links (and DMA etc) are quiesced; let
  990. * usbcore talk to the root hub.
  991. */
  992. if (hcd->self.controller->power.power_state.event == PM_EVENT_ON
  993. && urb->dev->parent == NULL)
  994. goto doit;
  995. /* FALL THROUGH */
  996. default:
  997. status = -ESHUTDOWN;
  998. break;
  999. }
  1000. spin_unlock_irqrestore (&hcd_data_lock, flags);
  1001. if (status) {
  1002. INIT_LIST_HEAD (&urb->urb_list);
  1003. usbmon_urb_submit_error(&hcd->self, urb, status);
  1004. return status;
  1005. }
  1006. /* increment urb's reference count as part of giving it to the HCD
  1007. * (which now controls it). HCD guarantees that it either returns
  1008. * an error or calls giveback(), but not both.
  1009. */
  1010. urb = usb_get_urb (urb);
  1011. atomic_inc (&urb->use_count);
  1012. if (urb->dev == hcd->self.root_hub) {
  1013. /* NOTE: requirement on hub callers (usbfs and the hub
  1014. * driver, for now) that URBs' urb->transfer_buffer be
  1015. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1016. * they could clobber root hub response data.
  1017. */
  1018. status = rh_urb_enqueue (hcd, urb);
  1019. goto done;
  1020. }
  1021. /* lower level hcd code should use *_dma exclusively,
  1022. * unless it uses pio or talks to another transport.
  1023. */
  1024. if (hcd->self.controller->dma_mask) {
  1025. if (usb_pipecontrol (urb->pipe)
  1026. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1027. urb->setup_dma = dma_map_single (
  1028. hcd->self.controller,
  1029. urb->setup_packet,
  1030. sizeof (struct usb_ctrlrequest),
  1031. DMA_TO_DEVICE);
  1032. if (urb->transfer_buffer_length != 0
  1033. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1034. urb->transfer_dma = dma_map_single (
  1035. hcd->self.controller,
  1036. urb->transfer_buffer,
  1037. urb->transfer_buffer_length,
  1038. usb_pipein (urb->pipe)
  1039. ? DMA_FROM_DEVICE
  1040. : DMA_TO_DEVICE);
  1041. }
  1042. status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags);
  1043. done:
  1044. if (unlikely (status)) {
  1045. urb_unlink (urb);
  1046. atomic_dec (&urb->use_count);
  1047. if (urb->reject)
  1048. wake_up (&usb_kill_urb_queue);
  1049. usb_put_urb (urb);
  1050. usbmon_urb_submit_error(&hcd->self, urb, status);
  1051. }
  1052. return status;
  1053. }
  1054. /*-------------------------------------------------------------------------*/
  1055. /* called in any context */
  1056. static int hcd_get_frame_number (struct usb_device *udev)
  1057. {
  1058. struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
  1059. if (!HC_IS_RUNNING (hcd->state))
  1060. return -ESHUTDOWN;
  1061. return hcd->driver->get_frame_number (hcd);
  1062. }
  1063. /*-------------------------------------------------------------------------*/
  1064. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1065. * off hardware queues (which may take a while) and returning it as
  1066. * soon as practical. we've already set up the urb's return status,
  1067. * but we can't know if the callback completed already.
  1068. */
  1069. static int
  1070. unlink1 (struct usb_hcd *hcd, struct urb *urb)
  1071. {
  1072. int value;
  1073. if (urb->dev == hcd->self.root_hub)
  1074. value = usb_rh_urb_dequeue (hcd, urb);
  1075. else {
  1076. /* The only reason an HCD might fail this call is if
  1077. * it has not yet fully queued the urb to begin with.
  1078. * Such failures should be harmless. */
  1079. value = hcd->driver->urb_dequeue (hcd, urb);
  1080. }
  1081. if (value != 0)
  1082. dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
  1083. urb, value);
  1084. return value;
  1085. }
  1086. /*
  1087. * called in any context
  1088. *
  1089. * caller guarantees urb won't be recycled till both unlink()
  1090. * and the urb's completion function return
  1091. */
  1092. static int hcd_unlink_urb (struct urb *urb, int status)
  1093. {
  1094. struct usb_host_endpoint *ep;
  1095. struct usb_hcd *hcd = NULL;
  1096. struct device *sys = NULL;
  1097. unsigned long flags;
  1098. struct list_head *tmp;
  1099. int retval;
  1100. if (!urb)
  1101. return -EINVAL;
  1102. if (!urb->dev || !urb->dev->bus)
  1103. return -ENODEV;
  1104. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  1105. [usb_pipeendpoint(urb->pipe)];
  1106. if (!ep)
  1107. return -ENODEV;
  1108. /*
  1109. * we contend for urb->status with the hcd core,
  1110. * which changes it while returning the urb.
  1111. *
  1112. * Caller guaranteed that the urb pointer hasn't been freed, and
  1113. * that it was submitted. But as a rule it can't know whether or
  1114. * not it's already been unlinked ... so we respect the reversed
  1115. * lock sequence needed for the usb_hcd_giveback_urb() code paths
  1116. * (urb lock, then hcd_data_lock) in case some other CPU is now
  1117. * unlinking it.
  1118. */
  1119. spin_lock_irqsave (&urb->lock, flags);
  1120. spin_lock (&hcd_data_lock);
  1121. sys = &urb->dev->dev;
  1122. hcd = urb->dev->bus->hcpriv;
  1123. if (hcd == NULL) {
  1124. retval = -ENODEV;
  1125. goto done;
  1126. }
  1127. /* insist the urb is still queued */
  1128. list_for_each(tmp, &ep->urb_list) {
  1129. if (tmp == &urb->urb_list)
  1130. break;
  1131. }
  1132. if (tmp != &urb->urb_list) {
  1133. retval = -EIDRM;
  1134. goto done;
  1135. }
  1136. /* Any status except -EINPROGRESS means something already started to
  1137. * unlink this URB from the hardware. So there's no more work to do.
  1138. */
  1139. if (urb->status != -EINPROGRESS) {
  1140. retval = -EBUSY;
  1141. goto done;
  1142. }
  1143. /* IRQ setup can easily be broken so that USB controllers
  1144. * never get completion IRQs ... maybe even the ones we need to
  1145. * finish unlinking the initial failed usb_set_address()
  1146. * or device descriptor fetch.
  1147. */
  1148. if (!hcd->saw_irq && hcd->self.root_hub != urb->dev) {
  1149. dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
  1150. "Controller is probably using the wrong IRQ."
  1151. "\n");
  1152. hcd->saw_irq = 1;
  1153. }
  1154. urb->status = status;
  1155. spin_unlock (&hcd_data_lock);
  1156. spin_unlock_irqrestore (&urb->lock, flags);
  1157. retval = unlink1 (hcd, urb);
  1158. if (retval == 0)
  1159. retval = -EINPROGRESS;
  1160. return retval;
  1161. done:
  1162. spin_unlock (&hcd_data_lock);
  1163. spin_unlock_irqrestore (&urb->lock, flags);
  1164. if (retval != -EIDRM && sys && sys->driver)
  1165. dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
  1166. return retval;
  1167. }
  1168. /*-------------------------------------------------------------------------*/
  1169. /* disables the endpoint: cancels any pending urbs, then synchronizes with
  1170. * the hcd to make sure all endpoint state is gone from hardware. use for
  1171. * set_configuration, set_interface, driver removal, physical disconnect.
  1172. *
  1173. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1174. * type, maxpacket size, toggle, halt status, and scheduling.
  1175. */
  1176. static void
  1177. hcd_endpoint_disable (struct usb_device *udev, struct usb_host_endpoint *ep)
  1178. {
  1179. struct usb_hcd *hcd;
  1180. struct urb *urb;
  1181. hcd = udev->bus->hcpriv;
  1182. WARN_ON (!HC_IS_RUNNING (hcd->state) && hcd->state != HC_STATE_HALT &&
  1183. udev->state != USB_STATE_NOTATTACHED);
  1184. local_irq_disable ();
  1185. /* FIXME move most of this into message.c as part of its
  1186. * endpoint disable logic
  1187. */
  1188. /* ep is already gone from udev->ep_{in,out}[]; no more submits */
  1189. rescan:
  1190. spin_lock (&hcd_data_lock);
  1191. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1192. int tmp;
  1193. /* another cpu may be in hcd, spinning on hcd_data_lock
  1194. * to giveback() this urb. the races here should be
  1195. * small, but a full fix needs a new "can't submit"
  1196. * urb state.
  1197. * FIXME urb->reject should allow that...
  1198. */
  1199. if (urb->status != -EINPROGRESS)
  1200. continue;
  1201. usb_get_urb (urb);
  1202. spin_unlock (&hcd_data_lock);
  1203. spin_lock (&urb->lock);
  1204. tmp = urb->status;
  1205. if (tmp == -EINPROGRESS)
  1206. urb->status = -ESHUTDOWN;
  1207. spin_unlock (&urb->lock);
  1208. /* kick hcd unless it's already returning this */
  1209. if (tmp == -EINPROGRESS) {
  1210. tmp = urb->pipe;
  1211. unlink1 (hcd, urb);
  1212. dev_dbg (hcd->self.controller,
  1213. "shutdown urb %p pipe %08x ep%d%s%s\n",
  1214. urb, tmp, usb_pipeendpoint (tmp),
  1215. (tmp & USB_DIR_IN) ? "in" : "out",
  1216. ({ char *s; \
  1217. switch (usb_pipetype (tmp)) { \
  1218. case PIPE_CONTROL: s = ""; break; \
  1219. case PIPE_BULK: s = "-bulk"; break; \
  1220. case PIPE_INTERRUPT: s = "-intr"; break; \
  1221. default: s = "-iso"; break; \
  1222. }; s;}));
  1223. }
  1224. usb_put_urb (urb);
  1225. /* list contents may have changed */
  1226. goto rescan;
  1227. }
  1228. spin_unlock (&hcd_data_lock);
  1229. local_irq_enable ();
  1230. /* synchronize with the hardware, so old configuration state
  1231. * clears out immediately (and will be freed).
  1232. */
  1233. might_sleep ();
  1234. if (hcd->driver->endpoint_disable)
  1235. hcd->driver->endpoint_disable (hcd, ep);
  1236. }
  1237. /*-------------------------------------------------------------------------*/
  1238. #ifdef CONFIG_PM
  1239. int hcd_bus_suspend (struct usb_bus *bus)
  1240. {
  1241. struct usb_hcd *hcd;
  1242. int status;
  1243. hcd = container_of (bus, struct usb_hcd, self);
  1244. if (!hcd->driver->bus_suspend)
  1245. return -ENOENT;
  1246. hcd->state = HC_STATE_QUIESCING;
  1247. status = hcd->driver->bus_suspend (hcd);
  1248. if (status == 0)
  1249. hcd->state = HC_STATE_SUSPENDED;
  1250. else
  1251. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1252. "suspend", status);
  1253. return status;
  1254. }
  1255. int hcd_bus_resume (struct usb_bus *bus)
  1256. {
  1257. struct usb_hcd *hcd;
  1258. int status;
  1259. hcd = container_of (bus, struct usb_hcd, self);
  1260. if (!hcd->driver->bus_resume)
  1261. return -ENOENT;
  1262. if (hcd->state == HC_STATE_RUNNING)
  1263. return 0;
  1264. hcd->state = HC_STATE_RESUMING;
  1265. status = hcd->driver->bus_resume (hcd);
  1266. if (status == 0)
  1267. hcd->state = HC_STATE_RUNNING;
  1268. else {
  1269. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1270. "resume", status);
  1271. usb_hc_died(hcd);
  1272. }
  1273. return status;
  1274. }
  1275. /*
  1276. * usb_hcd_suspend_root_hub - HCD autosuspends downstream ports
  1277. * @hcd: host controller for this root hub
  1278. *
  1279. * This call arranges that usb_hcd_resume_root_hub() is safe to call later;
  1280. * that the HCD's root hub polling is deactivated; and that the root's hub
  1281. * driver is suspended. HCDs may call this to autosuspend when their root
  1282. * hub's downstream ports are all inactive: unpowered, disconnected,
  1283. * disabled, or suspended.
  1284. *
  1285. * The HCD will autoresume on device connect change detection (using SRP
  1286. * or a D+/D- pullup). The HCD also autoresumes on remote wakeup signaling
  1287. * from any ports that are suspended (if that is enabled). In most cases,
  1288. * overcurrent signaling (on powered ports) will also start autoresume.
  1289. *
  1290. * Always called with IRQs blocked.
  1291. */
  1292. void usb_hcd_suspend_root_hub (struct usb_hcd *hcd)
  1293. {
  1294. struct urb *urb;
  1295. spin_lock (&hcd_root_hub_lock);
  1296. usb_suspend_root_hub (hcd->self.root_hub);
  1297. /* force status urb to complete/unlink while suspended */
  1298. if (hcd->status_urb) {
  1299. urb = hcd->status_urb;
  1300. urb->status = -ECONNRESET;
  1301. urb->hcpriv = NULL;
  1302. urb->actual_length = 0;
  1303. del_timer (&hcd->rh_timer);
  1304. hcd->poll_pending = 0;
  1305. hcd->status_urb = NULL;
  1306. } else
  1307. urb = NULL;
  1308. spin_unlock (&hcd_root_hub_lock);
  1309. hcd->state = HC_STATE_SUSPENDED;
  1310. if (urb)
  1311. usb_hcd_giveback_urb (hcd, urb, NULL);
  1312. }
  1313. EXPORT_SYMBOL_GPL(usb_hcd_suspend_root_hub);
  1314. /**
  1315. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1316. * @hcd: host controller for this root hub
  1317. *
  1318. * The USB host controller calls this function when its root hub is
  1319. * suspended (with the remote wakeup feature enabled) and a remote
  1320. * wakeup request is received. It queues a request for khubd to
  1321. * resume the root hub (that is, manage its downstream ports again).
  1322. */
  1323. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1324. {
  1325. unsigned long flags;
  1326. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1327. if (hcd->rh_registered)
  1328. usb_resume_root_hub (hcd->self.root_hub);
  1329. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1330. }
  1331. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1332. #endif
  1333. /*-------------------------------------------------------------------------*/
  1334. #ifdef CONFIG_USB_OTG
  1335. /**
  1336. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1337. * @bus: the bus (must use hcd framework)
  1338. * @port_num: 1-based number of port; usually bus->otg_port
  1339. * Context: in_interrupt()
  1340. *
  1341. * Starts enumeration, with an immediate reset followed later by
  1342. * khubd identifying and possibly configuring the device.
  1343. * This is needed by OTG controller drivers, where it helps meet
  1344. * HNP protocol timing requirements for starting a port reset.
  1345. */
  1346. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1347. {
  1348. struct usb_hcd *hcd;
  1349. int status = -EOPNOTSUPP;
  1350. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1351. * boards with root hubs hooked up to internal devices (instead of
  1352. * just the OTG port) may need more attention to resetting...
  1353. */
  1354. hcd = container_of (bus, struct usb_hcd, self);
  1355. if (port_num && hcd->driver->start_port_reset)
  1356. status = hcd->driver->start_port_reset(hcd, port_num);
  1357. /* run khubd shortly after (first) root port reset finishes;
  1358. * it may issue others, until at least 50 msecs have passed.
  1359. */
  1360. if (status == 0)
  1361. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1362. return status;
  1363. }
  1364. EXPORT_SYMBOL (usb_bus_start_enum);
  1365. #endif
  1366. /*-------------------------------------------------------------------------*/
  1367. /*
  1368. * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue)
  1369. */
  1370. static struct usb_operations usb_hcd_operations = {
  1371. .get_frame_number = hcd_get_frame_number,
  1372. .submit_urb = hcd_submit_urb,
  1373. .unlink_urb = hcd_unlink_urb,
  1374. .buffer_alloc = hcd_buffer_alloc,
  1375. .buffer_free = hcd_buffer_free,
  1376. .disable = hcd_endpoint_disable,
  1377. };
  1378. /*-------------------------------------------------------------------------*/
  1379. /**
  1380. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1381. * @hcd: host controller returning the URB
  1382. * @urb: urb being returned to the USB device driver.
  1383. * @regs: pt_regs, passed down to the URB completion handler
  1384. * Context: in_interrupt()
  1385. *
  1386. * This hands the URB from HCD to its USB device driver, using its
  1387. * completion function. The HCD has freed all per-urb resources
  1388. * (and is done using urb->hcpriv). It also released all HCD locks;
  1389. * the device driver won't cause problems if it frees, modifies,
  1390. * or resubmits this URB.
  1391. */
  1392. void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
  1393. {
  1394. int at_root_hub;
  1395. at_root_hub = (urb->dev == hcd->self.root_hub);
  1396. urb_unlink (urb);
  1397. /* lower level hcd code should use *_dma exclusively */
  1398. if (hcd->self.controller->dma_mask && !at_root_hub) {
  1399. if (usb_pipecontrol (urb->pipe)
  1400. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1401. dma_unmap_single (hcd->self.controller, urb->setup_dma,
  1402. sizeof (struct usb_ctrlrequest),
  1403. DMA_TO_DEVICE);
  1404. if (urb->transfer_buffer_length != 0
  1405. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1406. dma_unmap_single (hcd->self.controller,
  1407. urb->transfer_dma,
  1408. urb->transfer_buffer_length,
  1409. usb_pipein (urb->pipe)
  1410. ? DMA_FROM_DEVICE
  1411. : DMA_TO_DEVICE);
  1412. }
  1413. usbmon_urb_complete (&hcd->self, urb);
  1414. /* pass ownership to the completion handler */
  1415. urb->complete (urb, regs);
  1416. atomic_dec (&urb->use_count);
  1417. if (unlikely (urb->reject))
  1418. wake_up (&usb_kill_urb_queue);
  1419. usb_put_urb (urb);
  1420. }
  1421. EXPORT_SYMBOL (usb_hcd_giveback_urb);
  1422. /*-------------------------------------------------------------------------*/
  1423. /**
  1424. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1425. * @irq: the IRQ being raised
  1426. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1427. * @r: saved hardware registers
  1428. *
  1429. * If the controller isn't HALTed, calls the driver's irq handler.
  1430. * Checks whether the controller is now dead.
  1431. */
  1432. irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
  1433. {
  1434. struct usb_hcd *hcd = __hcd;
  1435. int start = hcd->state;
  1436. if (start == HC_STATE_HALT)
  1437. return IRQ_NONE;
  1438. if (hcd->driver->irq (hcd, r) == IRQ_NONE)
  1439. return IRQ_NONE;
  1440. hcd->saw_irq = 1;
  1441. if (hcd->state == HC_STATE_HALT)
  1442. usb_hc_died (hcd);
  1443. return IRQ_HANDLED;
  1444. }
  1445. /*-------------------------------------------------------------------------*/
  1446. /**
  1447. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1448. * @hcd: pointer to the HCD representing the controller
  1449. *
  1450. * This is called by bus glue to report a USB host controller that died
  1451. * while operations may still have been pending. It's called automatically
  1452. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1453. */
  1454. void usb_hc_died (struct usb_hcd *hcd)
  1455. {
  1456. unsigned long flags;
  1457. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1458. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1459. if (hcd->rh_registered) {
  1460. hcd->poll_rh = 0;
  1461. /* make khubd clean up old urbs and devices */
  1462. usb_set_device_state (hcd->self.root_hub,
  1463. USB_STATE_NOTATTACHED);
  1464. usb_kick_khubd (hcd->self.root_hub);
  1465. }
  1466. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1467. }
  1468. EXPORT_SYMBOL_GPL (usb_hc_died);
  1469. /*-------------------------------------------------------------------------*/
  1470. static void hcd_release (struct usb_bus *bus)
  1471. {
  1472. struct usb_hcd *hcd;
  1473. hcd = container_of(bus, struct usb_hcd, self);
  1474. kfree(hcd);
  1475. }
  1476. /**
  1477. * usb_create_hcd - create and initialize an HCD structure
  1478. * @driver: HC driver that will use this hcd
  1479. * @dev: device for this HC, stored in hcd->self.controller
  1480. * @bus_name: value to store in hcd->self.bus_name
  1481. * Context: !in_interrupt()
  1482. *
  1483. * Allocate a struct usb_hcd, with extra space at the end for the
  1484. * HC driver's private data. Initialize the generic members of the
  1485. * hcd structure.
  1486. *
  1487. * If memory is unavailable, returns NULL.
  1488. */
  1489. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1490. struct device *dev, char *bus_name)
  1491. {
  1492. struct usb_hcd *hcd;
  1493. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1494. if (!hcd) {
  1495. dev_dbg (dev, "hcd alloc failed\n");
  1496. return NULL;
  1497. }
  1498. dev_set_drvdata(dev, hcd);
  1499. usb_bus_init(&hcd->self);
  1500. hcd->self.op = &usb_hcd_operations;
  1501. hcd->self.hcpriv = hcd;
  1502. hcd->self.release = &hcd_release;
  1503. hcd->self.controller = dev;
  1504. hcd->self.bus_name = bus_name;
  1505. init_timer(&hcd->rh_timer);
  1506. hcd->rh_timer.function = rh_timer_func;
  1507. hcd->rh_timer.data = (unsigned long) hcd;
  1508. hcd->driver = driver;
  1509. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1510. "USB Host Controller";
  1511. return hcd;
  1512. }
  1513. EXPORT_SYMBOL (usb_create_hcd);
  1514. void usb_put_hcd (struct usb_hcd *hcd)
  1515. {
  1516. dev_set_drvdata(hcd->self.controller, NULL);
  1517. usb_bus_put(&hcd->self);
  1518. }
  1519. EXPORT_SYMBOL (usb_put_hcd);
  1520. /**
  1521. * usb_add_hcd - finish generic HCD structure initialization and register
  1522. * @hcd: the usb_hcd structure to initialize
  1523. * @irqnum: Interrupt line to allocate
  1524. * @irqflags: Interrupt type flags
  1525. *
  1526. * Finish the remaining parts of generic HCD initialization: allocate the
  1527. * buffers of consistent memory, register the bus, request the IRQ line,
  1528. * and call the driver's reset() and start() routines.
  1529. */
  1530. int usb_add_hcd(struct usb_hcd *hcd,
  1531. unsigned int irqnum, unsigned long irqflags)
  1532. {
  1533. int retval;
  1534. struct usb_device *rhdev;
  1535. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1536. /* till now HC has been in an indeterminate state ... */
  1537. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1538. dev_err(hcd->self.controller, "can't reset\n");
  1539. return retval;
  1540. }
  1541. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1542. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1543. return retval;
  1544. }
  1545. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1546. goto err_register_bus;
  1547. if (hcd->driver->irq) {
  1548. char buf[8], *bufp = buf;
  1549. #ifdef __sparc__
  1550. bufp = __irq_itoa(irqnum);
  1551. #else
  1552. sprintf(buf, "%d", irqnum);
  1553. #endif
  1554. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1555. hcd->driver->description, hcd->self.busnum);
  1556. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1557. hcd->irq_descr, hcd)) != 0) {
  1558. dev_err(hcd->self.controller,
  1559. "request interrupt %s failed\n", bufp);
  1560. goto err_request_irq;
  1561. }
  1562. hcd->irq = irqnum;
  1563. dev_info(hcd->self.controller, "irq %s, %s 0x%08llx\n", bufp,
  1564. (hcd->driver->flags & HCD_MEMORY) ?
  1565. "io mem" : "io base",
  1566. (unsigned long long)hcd->rsrc_start);
  1567. } else {
  1568. hcd->irq = -1;
  1569. if (hcd->rsrc_start)
  1570. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1571. (hcd->driver->flags & HCD_MEMORY) ?
  1572. "io mem" : "io base",
  1573. (unsigned long long)hcd->rsrc_start);
  1574. }
  1575. /* Allocate the root hub before calling hcd->driver->start(),
  1576. * but don't register it until afterward so that the hardware
  1577. * is running.
  1578. */
  1579. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1580. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1581. retval = -ENOMEM;
  1582. goto err_allocate_root_hub;
  1583. }
  1584. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1585. USB_SPEED_FULL;
  1586. /* Although in principle hcd->driver->start() might need to use rhdev,
  1587. * none of the current drivers do.
  1588. */
  1589. if ((retval = hcd->driver->start(hcd)) < 0) {
  1590. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1591. goto err_hcd_driver_start;
  1592. }
  1593. /* hcd->driver->start() reported can_wakeup, probably with
  1594. * assistance from board's boot firmware.
  1595. * NOTE: normal devices won't enable wakeup by default.
  1596. */
  1597. if (hcd->can_wakeup)
  1598. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1599. hcd->remote_wakeup = hcd->can_wakeup;
  1600. if ((retval = register_root_hub(rhdev, hcd)) != 0)
  1601. goto err_register_root_hub;
  1602. if (hcd->uses_new_polling && hcd->poll_rh)
  1603. usb_hcd_poll_rh_status(hcd);
  1604. return retval;
  1605. err_register_root_hub:
  1606. hcd->driver->stop(hcd);
  1607. err_hcd_driver_start:
  1608. usb_put_dev(rhdev);
  1609. err_allocate_root_hub:
  1610. if (hcd->irq >= 0)
  1611. free_irq(irqnum, hcd);
  1612. err_request_irq:
  1613. usb_deregister_bus(&hcd->self);
  1614. err_register_bus:
  1615. hcd_buffer_destroy(hcd);
  1616. return retval;
  1617. }
  1618. EXPORT_SYMBOL (usb_add_hcd);
  1619. /**
  1620. * usb_remove_hcd - shutdown processing for generic HCDs
  1621. * @hcd: the usb_hcd structure to remove
  1622. * Context: !in_interrupt()
  1623. *
  1624. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1625. * invoking the HCD's stop() method.
  1626. */
  1627. void usb_remove_hcd(struct usb_hcd *hcd)
  1628. {
  1629. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1630. if (HC_IS_RUNNING (hcd->state))
  1631. hcd->state = HC_STATE_QUIESCING;
  1632. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1633. spin_lock_irq (&hcd_root_hub_lock);
  1634. hcd->rh_registered = 0;
  1635. spin_unlock_irq (&hcd_root_hub_lock);
  1636. usb_disconnect(&hcd->self.root_hub);
  1637. hcd->poll_rh = 0;
  1638. del_timer_sync(&hcd->rh_timer);
  1639. hcd->driver->stop(hcd);
  1640. hcd->state = HC_STATE_HALT;
  1641. if (hcd->irq >= 0)
  1642. free_irq(hcd->irq, hcd);
  1643. usb_deregister_bus(&hcd->self);
  1644. hcd_buffer_destroy(hcd);
  1645. }
  1646. EXPORT_SYMBOL (usb_remove_hcd);
  1647. /*-------------------------------------------------------------------------*/
  1648. #if defined(CONFIG_USB_MON)
  1649. struct usb_mon_operations *mon_ops;
  1650. /*
  1651. * The registration is unlocked.
  1652. * We do it this way because we do not want to lock in hot paths.
  1653. *
  1654. * Notice that the code is minimally error-proof. Because usbmon needs
  1655. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1656. */
  1657. int usb_mon_register (struct usb_mon_operations *ops)
  1658. {
  1659. if (mon_ops)
  1660. return -EBUSY;
  1661. mon_ops = ops;
  1662. mb();
  1663. return 0;
  1664. }
  1665. EXPORT_SYMBOL_GPL (usb_mon_register);
  1666. void usb_mon_deregister (void)
  1667. {
  1668. if (mon_ops == NULL) {
  1669. printk(KERN_ERR "USB: monitor was not registered\n");
  1670. return;
  1671. }
  1672. mon_ops = NULL;
  1673. mb();
  1674. }
  1675. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1676. #endif /* CONFIG_USB_MON */