hcd.c 76 KB

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