hcd.c 56 KB

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