hcd.c 65 KB

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