hcd.c 64 KB

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