hcd.c 56 KB

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