hcd.c 57 KB

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