hcd.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright Linus Torvalds 1999
  4. * (C) Copyright Johannes Erdfelt 1999-2001
  5. * (C) Copyright Andreas Gal 1999
  6. * (C) Copyright Gregory P. Smith 1999
  7. * (C) Copyright Deti Fliegl 1999
  8. * (C) Copyright Randy Dunlap 2000
  9. * (C) Copyright David Brownell 2000-2002
  10. */
  11. #include <linux/bcd.h>
  12. #include <linux/module.h>
  13. #include <linux/version.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/slab.h>
  17. #include <linux/completion.h>
  18. #include <linux/utsname.h>
  19. #include <linux/mm.h>
  20. #include <asm/io.h>
  21. #include <linux/device.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/mutex.h>
  24. #include <asm/irq.h>
  25. #include <asm/byteorder.h>
  26. #include <linux/unaligned.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/types.h>
  31. #include <linux/genalloc.h>
  32. #include <linux/io.h>
  33. #include <linux/kcov.h>
  34. #include <linux/phy/phy.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/hcd.h>
  37. #include <linux/usb/otg.h>
  38. #include "usb.h"
  39. #include "phy.h"
  40. /*-------------------------------------------------------------------------*/
  41. /*
  42. * USB Host Controller Driver framework
  43. *
  44. * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
  45. * HCD-specific behaviors/bugs.
  46. *
  47. * This does error checks, tracks devices and urbs, and delegates to a
  48. * "hc_driver" only for code (and data) that really needs to know about
  49. * hardware differences. That includes root hub registers, i/o queues,
  50. * and so on ... but as little else as possible.
  51. *
  52. * Shared code includes most of the "root hub" code (these are emulated,
  53. * though each HC's hardware works differently) and PCI glue, plus request
  54. * tracking overhead. The HCD code should only block on spinlocks or on
  55. * hardware handshaking; blocking on software events (such as other kernel
  56. * threads releasing resources, or completing actions) is all generic.
  57. *
  58. * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
  59. * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
  60. * only by the hub driver ... and that neither should be seen or used by
  61. * usb client device drivers.
  62. *
  63. * Contributors of ideas or unattributed patches include: David Brownell,
  64. * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
  65. *
  66. * HISTORY:
  67. * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
  68. * associated cleanup. "usb_hcd" still != "usb_bus".
  69. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  70. */
  71. /*-------------------------------------------------------------------------*/
  72. /* Keep track of which host controller drivers are loaded */
  73. unsigned long usb_hcds_loaded;
  74. EXPORT_SYMBOL_GPL(usb_hcds_loaded);
  75. /* host controllers we manage */
  76. DEFINE_IDR (usb_bus_idr);
  77. EXPORT_SYMBOL_GPL (usb_bus_idr);
  78. /* used when allocating bus numbers */
  79. #define USB_MAXBUS 64
  80. /* used when updating list of hcds */
  81. DEFINE_MUTEX(usb_bus_idr_lock); /* exported only for usbfs */
  82. EXPORT_SYMBOL_GPL (usb_bus_idr_lock);
  83. /* used for controlling access to virtual root hubs */
  84. static DEFINE_SPINLOCK(hcd_root_hub_lock);
  85. /* used when updating an endpoint's URB list */
  86. static DEFINE_SPINLOCK(hcd_urb_list_lock);
  87. /* used to protect against unlinking URBs after the device is gone */
  88. static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
  89. /* wait queue for synchronous unlinks */
  90. DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
  91. /*-------------------------------------------------------------------------*/
  92. /*
  93. * Sharable chunks of root hub code.
  94. */
  95. /*-------------------------------------------------------------------------*/
  96. #define KERNEL_REL bin2bcd(LINUX_VERSION_MAJOR)
  97. #define KERNEL_VER bin2bcd(LINUX_VERSION_PATCHLEVEL)
  98. /* usb 3.1 root hub device descriptor */
  99. static const u8 usb31_rh_dev_descriptor[18] = {
  100. 0x12, /* __u8 bLength; */
  101. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  102. 0x10, 0x03, /* __le16 bcdUSB; v3.1 */
  103. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  104. 0x00, /* __u8 bDeviceSubClass; */
  105. 0x03, /* __u8 bDeviceProtocol; USB 3 hub */
  106. 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
  107. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  108. 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
  109. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  110. 0x03, /* __u8 iManufacturer; */
  111. 0x02, /* __u8 iProduct; */
  112. 0x01, /* __u8 iSerialNumber; */
  113. 0x01 /* __u8 bNumConfigurations; */
  114. };
  115. /* usb 3.0 root hub device descriptor */
  116. static const u8 usb3_rh_dev_descriptor[18] = {
  117. 0x12, /* __u8 bLength; */
  118. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  119. 0x00, 0x03, /* __le16 bcdUSB; v3.0 */
  120. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  121. 0x00, /* __u8 bDeviceSubClass; */
  122. 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */
  123. 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
  124. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  125. 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
  126. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  127. 0x03, /* __u8 iManufacturer; */
  128. 0x02, /* __u8 iProduct; */
  129. 0x01, /* __u8 iSerialNumber; */
  130. 0x01 /* __u8 bNumConfigurations; */
  131. };
  132. /* usb 2.0 root hub device descriptor */
  133. static const u8 usb2_rh_dev_descriptor[18] = {
  134. 0x12, /* __u8 bLength; */
  135. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  136. 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
  137. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  138. 0x00, /* __u8 bDeviceSubClass; */
  139. 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
  140. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  141. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  142. 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
  143. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  144. 0x03, /* __u8 iManufacturer; */
  145. 0x02, /* __u8 iProduct; */
  146. 0x01, /* __u8 iSerialNumber; */
  147. 0x01 /* __u8 bNumConfigurations; */
  148. };
  149. /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
  150. /* usb 1.1 root hub device descriptor */
  151. static const u8 usb11_rh_dev_descriptor[18] = {
  152. 0x12, /* __u8 bLength; */
  153. USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
  154. 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
  155. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  156. 0x00, /* __u8 bDeviceSubClass; */
  157. 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
  158. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  159. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  160. 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
  161. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  162. 0x03, /* __u8 iManufacturer; */
  163. 0x02, /* __u8 iProduct; */
  164. 0x01, /* __u8 iSerialNumber; */
  165. 0x01 /* __u8 bNumConfigurations; */
  166. };
  167. /*-------------------------------------------------------------------------*/
  168. /* Configuration descriptors for our root hubs */
  169. static const u8 fs_rh_config_descriptor[] = {
  170. /* one configuration */
  171. 0x09, /* __u8 bLength; */
  172. USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
  173. 0x19, 0x00, /* __le16 wTotalLength; */
  174. 0x01, /* __u8 bNumInterfaces; (1) */
  175. 0x01, /* __u8 bConfigurationValue; */
  176. 0x00, /* __u8 iConfiguration; */
  177. 0xc0, /* __u8 bmAttributes;
  178. Bit 7: must be set,
  179. 6: Self-powered,
  180. 5: Remote wakeup,
  181. 4..0: resvd */
  182. 0x00, /* __u8 MaxPower; */
  183. /* USB 1.1:
  184. * USB 2.0, single TT organization (mandatory):
  185. * one interface, protocol 0
  186. *
  187. * USB 2.0, multiple TT organization (optional):
  188. * two interfaces, protocols 1 (like single TT)
  189. * and 2 (multiple TT mode) ... config is
  190. * sometimes settable
  191. * NOT IMPLEMENTED
  192. */
  193. /* one interface */
  194. 0x09, /* __u8 if_bLength; */
  195. USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
  196. 0x00, /* __u8 if_bInterfaceNumber; */
  197. 0x00, /* __u8 if_bAlternateSetting; */
  198. 0x01, /* __u8 if_bNumEndpoints; */
  199. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  200. 0x00, /* __u8 if_bInterfaceSubClass; */
  201. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  202. 0x00, /* __u8 if_iInterface; */
  203. /* one endpoint (status change endpoint) */
  204. 0x07, /* __u8 ep_bLength; */
  205. USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
  206. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  207. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  208. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  209. 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  210. };
  211. static const u8 hs_rh_config_descriptor[] = {
  212. /* one configuration */
  213. 0x09, /* __u8 bLength; */
  214. USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
  215. 0x19, 0x00, /* __le16 wTotalLength; */
  216. 0x01, /* __u8 bNumInterfaces; (1) */
  217. 0x01, /* __u8 bConfigurationValue; */
  218. 0x00, /* __u8 iConfiguration; */
  219. 0xc0, /* __u8 bmAttributes;
  220. Bit 7: must be set,
  221. 6: Self-powered,
  222. 5: Remote wakeup,
  223. 4..0: resvd */
  224. 0x00, /* __u8 MaxPower; */
  225. /* USB 1.1:
  226. * USB 2.0, single TT organization (mandatory):
  227. * one interface, protocol 0
  228. *
  229. * USB 2.0, multiple TT organization (optional):
  230. * two interfaces, protocols 1 (like single TT)
  231. * and 2 (multiple TT mode) ... config is
  232. * sometimes settable
  233. * NOT IMPLEMENTED
  234. */
  235. /* one interface */
  236. 0x09, /* __u8 if_bLength; */
  237. USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
  238. 0x00, /* __u8 if_bInterfaceNumber; */
  239. 0x00, /* __u8 if_bAlternateSetting; */
  240. 0x01, /* __u8 if_bNumEndpoints; */
  241. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  242. 0x00, /* __u8 if_bInterfaceSubClass; */
  243. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  244. 0x00, /* __u8 if_iInterface; */
  245. /* one endpoint (status change endpoint) */
  246. 0x07, /* __u8 ep_bLength; */
  247. USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
  248. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  249. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  250. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  251. * see hub.c:hub_configure() for details. */
  252. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  253. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  254. };
  255. static const u8 ss_rh_config_descriptor[] = {
  256. /* one configuration */
  257. 0x09, /* __u8 bLength; */
  258. USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
  259. 0x1f, 0x00, /* __le16 wTotalLength; */
  260. 0x01, /* __u8 bNumInterfaces; (1) */
  261. 0x01, /* __u8 bConfigurationValue; */
  262. 0x00, /* __u8 iConfiguration; */
  263. 0xc0, /* __u8 bmAttributes;
  264. Bit 7: must be set,
  265. 6: Self-powered,
  266. 5: Remote wakeup,
  267. 4..0: resvd */
  268. 0x00, /* __u8 MaxPower; */
  269. /* one interface */
  270. 0x09, /* __u8 if_bLength; */
  271. USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
  272. 0x00, /* __u8 if_bInterfaceNumber; */
  273. 0x00, /* __u8 if_bAlternateSetting; */
  274. 0x01, /* __u8 if_bNumEndpoints; */
  275. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  276. 0x00, /* __u8 if_bInterfaceSubClass; */
  277. 0x00, /* __u8 if_bInterfaceProtocol; */
  278. 0x00, /* __u8 if_iInterface; */
  279. /* one endpoint (status change endpoint) */
  280. 0x07, /* __u8 ep_bLength; */
  281. USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
  282. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  283. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  284. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  285. * see hub.c:hub_configure() for details. */
  286. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  287. 0x0c, /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  288. /* one SuperSpeed endpoint companion descriptor */
  289. 0x06, /* __u8 ss_bLength */
  290. USB_DT_SS_ENDPOINT_COMP, /* __u8 ss_bDescriptorType; SuperSpeed EP */
  291. /* Companion */
  292. 0x00, /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */
  293. 0x00, /* __u8 ss_bmAttributes; 1 packet per service interval */
  294. 0x02, 0x00 /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */
  295. };
  296. /* authorized_default behaviour:
  297. * -1 is authorized for all devices (leftover from wireless USB)
  298. * 0 is unauthorized for all devices
  299. * 1 is authorized for all devices
  300. * 2 is authorized for internal devices
  301. */
  302. #define USB_AUTHORIZE_WIRED -1
  303. #define USB_AUTHORIZE_NONE 0
  304. #define USB_AUTHORIZE_ALL 1
  305. #define USB_AUTHORIZE_INTERNAL 2
  306. static int authorized_default = CONFIG_USB_DEFAULT_AUTHORIZATION_MODE;
  307. module_param(authorized_default, int, S_IRUGO|S_IWUSR);
  308. MODULE_PARM_DESC(authorized_default,
  309. "Default USB device authorization: 0 is not authorized, 1 is authorized (default), 2 is authorized for internal devices, -1 is authorized (same as 1)");
  310. /*-------------------------------------------------------------------------*/
  311. /**
  312. * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
  313. * @s: Null-terminated ASCII (actually ISO-8859-1) string
  314. * @buf: Buffer for USB string descriptor (header + UTF-16LE)
  315. * @len: Length (in bytes; may be odd) of descriptor buffer.
  316. *
  317. * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len,
  318. * whichever is less.
  319. *
  320. * Note:
  321. * USB String descriptors can contain at most 126 characters; input
  322. * strings longer than that are truncated.
  323. */
  324. static unsigned
  325. ascii2desc(char const *s, u8 *buf, unsigned len)
  326. {
  327. unsigned n, t = 2 + 2*strlen(s);
  328. if (t > 254)
  329. t = 254; /* Longest possible UTF string descriptor */
  330. if (len > t)
  331. len = t;
  332. t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */
  333. n = len;
  334. while (n--) {
  335. *buf++ = t;
  336. if (!n--)
  337. break;
  338. *buf++ = t >> 8;
  339. t = (unsigned char)*s++;
  340. }
  341. return len;
  342. }
  343. /**
  344. * rh_string() - provides string descriptors for root hub
  345. * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
  346. * @hcd: the host controller for this root hub
  347. * @data: buffer for output packet
  348. * @len: length of the provided buffer
  349. *
  350. * Produces either a manufacturer, product or serial number string for the
  351. * virtual root hub device.
  352. *
  353. * Return: The number of bytes filled in: the length of the descriptor or
  354. * of the provided buffer, whichever is less.
  355. */
  356. static unsigned
  357. rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
  358. {
  359. char buf[100];
  360. char const *s;
  361. static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
  362. /* language ids */
  363. switch (id) {
  364. case 0:
  365. /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
  366. /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
  367. if (len > 4)
  368. len = 4;
  369. memcpy(data, langids, len);
  370. return len;
  371. case 1:
  372. /* Serial number */
  373. s = hcd->self.bus_name;
  374. break;
  375. case 2:
  376. /* Product name */
  377. s = hcd->product_desc;
  378. break;
  379. case 3:
  380. /* Manufacturer */
  381. snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
  382. init_utsname()->release, hcd->driver->description);
  383. s = buf;
  384. break;
  385. default:
  386. /* Can't happen; caller guarantees it */
  387. return 0;
  388. }
  389. return ascii2desc(s, data, len);
  390. }
  391. /* Root hub control transfers execute synchronously */
  392. static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
  393. {
  394. struct usb_ctrlrequest *cmd;
  395. u16 typeReq, wValue, wIndex, wLength;
  396. u8 *ubuf = urb->transfer_buffer;
  397. unsigned len = 0;
  398. int status;
  399. u8 patch_wakeup = 0;
  400. u8 patch_protocol = 0;
  401. u16 tbuf_size;
  402. u8 *tbuf = NULL;
  403. const u8 *bufp;
  404. might_sleep();
  405. spin_lock_irq(&hcd_root_hub_lock);
  406. status = usb_hcd_link_urb_to_ep(hcd, urb);
  407. spin_unlock_irq(&hcd_root_hub_lock);
  408. if (status)
  409. return status;
  410. urb->hcpriv = hcd; /* Indicate it's queued */
  411. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  412. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  413. wValue = le16_to_cpu (cmd->wValue);
  414. wIndex = le16_to_cpu (cmd->wIndex);
  415. wLength = le16_to_cpu (cmd->wLength);
  416. if (wLength > urb->transfer_buffer_length)
  417. goto error;
  418. /*
  419. * tbuf should be at least as big as the
  420. * USB hub descriptor.
  421. */
  422. tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength);
  423. tbuf = kzalloc(tbuf_size, GFP_KERNEL);
  424. if (!tbuf) {
  425. status = -ENOMEM;
  426. goto err_alloc;
  427. }
  428. bufp = tbuf;
  429. urb->actual_length = 0;
  430. switch (typeReq) {
  431. /* DEVICE REQUESTS */
  432. /* The root hub's remote wakeup enable bit is implemented using
  433. * driver model wakeup flags. If this system supports wakeup
  434. * through USB, userspace may change the default "allow wakeup"
  435. * policy through sysfs or these calls.
  436. *
  437. * Most root hubs support wakeup from downstream devices, for
  438. * runtime power management (disabling USB clocks and reducing
  439. * VBUS power usage). However, not all of them do so; silicon,
  440. * board, and BIOS bugs here are not uncommon, so these can't
  441. * be treated quite like external hubs.
  442. *
  443. * Likewise, not all root hubs will pass wakeup events upstream,
  444. * to wake up the whole system. So don't assume root hub and
  445. * controller capabilities are identical.
  446. */
  447. case DeviceRequest | USB_REQ_GET_STATUS:
  448. tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev)
  449. << USB_DEVICE_REMOTE_WAKEUP)
  450. | (1 << USB_DEVICE_SELF_POWERED);
  451. tbuf[1] = 0;
  452. len = 2;
  453. break;
  454. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  455. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  456. device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
  457. else
  458. goto error;
  459. break;
  460. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  461. if (device_can_wakeup(&hcd->self.root_hub->dev)
  462. && wValue == USB_DEVICE_REMOTE_WAKEUP)
  463. device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
  464. else
  465. goto error;
  466. break;
  467. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  468. tbuf[0] = 1;
  469. len = 1;
  470. fallthrough;
  471. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  472. break;
  473. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  474. switch (wValue & 0xff00) {
  475. case USB_DT_DEVICE << 8:
  476. switch (hcd->speed) {
  477. case HCD_USB32:
  478. case HCD_USB31:
  479. bufp = usb31_rh_dev_descriptor;
  480. break;
  481. case HCD_USB3:
  482. bufp = usb3_rh_dev_descriptor;
  483. break;
  484. case HCD_USB2:
  485. bufp = usb2_rh_dev_descriptor;
  486. break;
  487. case HCD_USB11:
  488. bufp = usb11_rh_dev_descriptor;
  489. break;
  490. default:
  491. goto error;
  492. }
  493. len = 18;
  494. if (hcd->has_tt)
  495. patch_protocol = 1;
  496. break;
  497. case USB_DT_CONFIG << 8:
  498. switch (hcd->speed) {
  499. case HCD_USB32:
  500. case HCD_USB31:
  501. case HCD_USB3:
  502. bufp = ss_rh_config_descriptor;
  503. len = sizeof ss_rh_config_descriptor;
  504. break;
  505. case HCD_USB2:
  506. bufp = hs_rh_config_descriptor;
  507. len = sizeof hs_rh_config_descriptor;
  508. break;
  509. case HCD_USB11:
  510. bufp = fs_rh_config_descriptor;
  511. len = sizeof fs_rh_config_descriptor;
  512. break;
  513. default:
  514. goto error;
  515. }
  516. if (device_can_wakeup(&hcd->self.root_hub->dev))
  517. patch_wakeup = 1;
  518. break;
  519. case USB_DT_STRING << 8:
  520. if ((wValue & 0xff) < 4)
  521. urb->actual_length = rh_string(wValue & 0xff,
  522. hcd, ubuf, wLength);
  523. else /* unsupported IDs --> "protocol stall" */
  524. goto error;
  525. break;
  526. case USB_DT_BOS << 8:
  527. goto nongeneric;
  528. default:
  529. goto error;
  530. }
  531. break;
  532. case DeviceRequest | USB_REQ_GET_INTERFACE:
  533. tbuf[0] = 0;
  534. len = 1;
  535. fallthrough;
  536. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  537. break;
  538. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  539. /* wValue == urb->dev->devaddr */
  540. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  541. wValue);
  542. break;
  543. /* INTERFACE REQUESTS (no defined feature/status flags) */
  544. /* ENDPOINT REQUESTS */
  545. case EndpointRequest | USB_REQ_GET_STATUS:
  546. /* ENDPOINT_HALT flag */
  547. tbuf[0] = 0;
  548. tbuf[1] = 0;
  549. len = 2;
  550. fallthrough;
  551. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  552. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  553. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  554. break;
  555. /* CLASS REQUESTS (and errors) */
  556. default:
  557. nongeneric:
  558. /* non-generic request */
  559. switch (typeReq) {
  560. case GetHubStatus:
  561. len = 4;
  562. break;
  563. case GetPortStatus:
  564. if (wValue == HUB_PORT_STATUS)
  565. len = 4;
  566. else
  567. /* other port status types return 8 bytes */
  568. len = 8;
  569. break;
  570. case GetHubDescriptor:
  571. len = sizeof (struct usb_hub_descriptor);
  572. break;
  573. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  574. /* len is returned by hub_control */
  575. break;
  576. }
  577. status = hcd->driver->hub_control (hcd,
  578. typeReq, wValue, wIndex,
  579. tbuf, wLength);
  580. if (typeReq == GetHubDescriptor)
  581. usb_hub_adjust_deviceremovable(hcd->self.root_hub,
  582. (struct usb_hub_descriptor *)tbuf);
  583. break;
  584. error:
  585. /* "protocol stall" on error */
  586. status = -EPIPE;
  587. }
  588. if (status < 0) {
  589. len = 0;
  590. if (status != -EPIPE) {
  591. dev_dbg (hcd->self.controller,
  592. "CTRL: TypeReq=0x%x val=0x%x "
  593. "idx=0x%x len=%d ==> %d\n",
  594. typeReq, wValue, wIndex,
  595. wLength, status);
  596. }
  597. } else if (status > 0) {
  598. /* hub_control may return the length of data copied. */
  599. len = status;
  600. status = 0;
  601. }
  602. if (len) {
  603. if (urb->transfer_buffer_length < len)
  604. len = urb->transfer_buffer_length;
  605. urb->actual_length = len;
  606. /* always USB_DIR_IN, toward host */
  607. memcpy (ubuf, bufp, len);
  608. /* report whether RH hardware supports remote wakeup */
  609. if (patch_wakeup &&
  610. len > offsetof (struct usb_config_descriptor,
  611. bmAttributes))
  612. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  613. |= USB_CONFIG_ATT_WAKEUP;
  614. /* report whether RH hardware has an integrated TT */
  615. if (patch_protocol &&
  616. len > offsetof(struct usb_device_descriptor,
  617. bDeviceProtocol))
  618. ((struct usb_device_descriptor *) ubuf)->
  619. bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT;
  620. }
  621. kfree(tbuf);
  622. err_alloc:
  623. /* any errors get returned through the urb completion */
  624. spin_lock_irq(&hcd_root_hub_lock);
  625. usb_hcd_unlink_urb_from_ep(hcd, urb);
  626. usb_hcd_giveback_urb(hcd, urb, status);
  627. spin_unlock_irq(&hcd_root_hub_lock);
  628. return 0;
  629. }
  630. /*-------------------------------------------------------------------------*/
  631. /*
  632. * Root Hub interrupt transfers are polled using a timer if the
  633. * driver requests it; otherwise the driver is responsible for
  634. * calling usb_hcd_poll_rh_status() when an event occurs.
  635. *
  636. * Completion handler may not sleep. See usb_hcd_giveback_urb() for details.
  637. */
  638. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  639. {
  640. struct urb *urb;
  641. int length;
  642. int status;
  643. unsigned long flags;
  644. char buffer[6]; /* Any root hubs with > 31 ports? */
  645. if (unlikely(!hcd->rh_pollable))
  646. return;
  647. if (!hcd->uses_new_polling && !hcd->status_urb)
  648. return;
  649. length = hcd->driver->hub_status_data(hcd, buffer);
  650. if (length > 0) {
  651. /* try to complete the status urb */
  652. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  653. urb = hcd->status_urb;
  654. if (urb) {
  655. clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  656. hcd->status_urb = NULL;
  657. if (urb->transfer_buffer_length >= length) {
  658. status = 0;
  659. } else {
  660. status = -EOVERFLOW;
  661. length = urb->transfer_buffer_length;
  662. }
  663. urb->actual_length = length;
  664. memcpy(urb->transfer_buffer, buffer, length);
  665. usb_hcd_unlink_urb_from_ep(hcd, urb);
  666. usb_hcd_giveback_urb(hcd, urb, status);
  667. } else {
  668. length = 0;
  669. set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  670. }
  671. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  672. }
  673. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  674. * exceed that limit if HZ is 100. The math is more clunky than
  675. * maybe expected, this is to make sure that all timers for USB devices
  676. * fire at the same time to give the CPU a break in between */
  677. if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
  678. (length == 0 && hcd->status_urb != NULL))
  679. mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  680. }
  681. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  682. /* timer callback */
  683. static void rh_timer_func (struct timer_list *t)
  684. {
  685. struct usb_hcd *_hcd = from_timer(_hcd, t, rh_timer);
  686. usb_hcd_poll_rh_status(_hcd);
  687. }
  688. /*-------------------------------------------------------------------------*/
  689. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  690. {
  691. int retval;
  692. unsigned long flags;
  693. unsigned len = 1 + (urb->dev->maxchild / 8);
  694. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  695. if (hcd->status_urb || urb->transfer_buffer_length < len) {
  696. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  697. retval = -EINVAL;
  698. goto done;
  699. }
  700. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  701. if (retval)
  702. goto done;
  703. hcd->status_urb = urb;
  704. urb->hcpriv = hcd; /* indicate it's queued */
  705. if (!hcd->uses_new_polling)
  706. mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  707. /* If a status change has already occurred, report it ASAP */
  708. else if (HCD_POLL_PENDING(hcd))
  709. mod_timer(&hcd->rh_timer, jiffies);
  710. retval = 0;
  711. done:
  712. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  713. return retval;
  714. }
  715. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  716. {
  717. if (usb_endpoint_xfer_int(&urb->ep->desc))
  718. return rh_queue_status (hcd, urb);
  719. if (usb_endpoint_xfer_control(&urb->ep->desc))
  720. return rh_call_control (hcd, urb);
  721. return -EINVAL;
  722. }
  723. /*-------------------------------------------------------------------------*/
  724. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  725. * since these URBs always execute synchronously.
  726. */
  727. static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  728. {
  729. unsigned long flags;
  730. int rc;
  731. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  732. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  733. if (rc)
  734. goto done;
  735. if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
  736. ; /* Do nothing */
  737. } else { /* Status URB */
  738. if (!hcd->uses_new_polling)
  739. del_timer (&hcd->rh_timer);
  740. if (urb == hcd->status_urb) {
  741. hcd->status_urb = NULL;
  742. usb_hcd_unlink_urb_from_ep(hcd, urb);
  743. usb_hcd_giveback_urb(hcd, urb, status);
  744. }
  745. }
  746. done:
  747. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  748. return rc;
  749. }
  750. /*-------------------------------------------------------------------------*/
  751. /**
  752. * usb_bus_init - shared initialization code
  753. * @bus: the bus structure being initialized
  754. *
  755. * This code is used to initialize a usb_bus structure, memory for which is
  756. * separately managed.
  757. */
  758. static void usb_bus_init (struct usb_bus *bus)
  759. {
  760. memset(&bus->devmap, 0, sizeof(bus->devmap));
  761. bus->devnum_next = 1;
  762. bus->root_hub = NULL;
  763. bus->busnum = -1;
  764. bus->bandwidth_allocated = 0;
  765. bus->bandwidth_int_reqs = 0;
  766. bus->bandwidth_isoc_reqs = 0;
  767. mutex_init(&bus->devnum_next_mutex);
  768. }
  769. /*-------------------------------------------------------------------------*/
  770. /**
  771. * usb_register_bus - registers the USB host controller with the usb core
  772. * @bus: pointer to the bus to register
  773. *
  774. * Context: task context, might sleep.
  775. *
  776. * Assigns a bus number, and links the controller into usbcore data
  777. * structures so that it can be seen by scanning the bus list.
  778. *
  779. * Return: 0 if successful. A negative error code otherwise.
  780. */
  781. static int usb_register_bus(struct usb_bus *bus)
  782. {
  783. int result = -E2BIG;
  784. int busnum;
  785. mutex_lock(&usb_bus_idr_lock);
  786. busnum = idr_alloc(&usb_bus_idr, bus, 1, USB_MAXBUS, GFP_KERNEL);
  787. if (busnum < 0) {
  788. pr_err("%s: failed to get bus number\n", usbcore_name);
  789. goto error_find_busnum;
  790. }
  791. bus->busnum = busnum;
  792. mutex_unlock(&usb_bus_idr_lock);
  793. usb_notify_add_bus(bus);
  794. dev_info (bus->controller, "new USB bus registered, assigned bus "
  795. "number %d\n", bus->busnum);
  796. return 0;
  797. error_find_busnum:
  798. mutex_unlock(&usb_bus_idr_lock);
  799. return result;
  800. }
  801. /**
  802. * usb_deregister_bus - deregisters the USB host controller
  803. * @bus: pointer to the bus to deregister
  804. *
  805. * Context: task context, might sleep.
  806. *
  807. * Recycles the bus number, and unlinks the controller from usbcore data
  808. * structures so that it won't be seen by scanning the bus list.
  809. */
  810. static void usb_deregister_bus (struct usb_bus *bus)
  811. {
  812. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  813. /*
  814. * NOTE: make sure that all the devices are removed by the
  815. * controller code, as well as having it call this when cleaning
  816. * itself up
  817. */
  818. mutex_lock(&usb_bus_idr_lock);
  819. idr_remove(&usb_bus_idr, bus->busnum);
  820. mutex_unlock(&usb_bus_idr_lock);
  821. usb_notify_remove_bus(bus);
  822. }
  823. /**
  824. * register_root_hub - called by usb_add_hcd() to register a root hub
  825. * @hcd: host controller for this root hub
  826. *
  827. * This function registers the root hub with the USB subsystem. It sets up
  828. * the device properly in the device tree and then calls usb_new_device()
  829. * to register the usb device. It also assigns the root hub's USB address
  830. * (always 1).
  831. *
  832. * Return: 0 if successful. A negative error code otherwise.
  833. */
  834. static int register_root_hub(struct usb_hcd *hcd)
  835. {
  836. struct device *parent_dev = hcd->self.controller;
  837. struct usb_device *usb_dev = hcd->self.root_hub;
  838. struct usb_device_descriptor *descr;
  839. const int devnum = 1;
  840. int retval;
  841. usb_dev->devnum = devnum;
  842. usb_dev->bus->devnum_next = devnum + 1;
  843. set_bit(devnum, usb_dev->bus->devmap);
  844. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  845. mutex_lock(&usb_bus_idr_lock);
  846. usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  847. descr = usb_get_device_descriptor(usb_dev);
  848. if (IS_ERR(descr)) {
  849. retval = PTR_ERR(descr);
  850. mutex_unlock(&usb_bus_idr_lock);
  851. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  852. dev_name(&usb_dev->dev), retval);
  853. return retval;
  854. }
  855. usb_dev->descriptor = *descr;
  856. kfree(descr);
  857. if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) {
  858. retval = usb_get_bos_descriptor(usb_dev);
  859. if (!retval) {
  860. usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev);
  861. } else if (usb_dev->speed >= USB_SPEED_SUPER) {
  862. mutex_unlock(&usb_bus_idr_lock);
  863. dev_dbg(parent_dev, "can't read %s bos descriptor %d\n",
  864. dev_name(&usb_dev->dev), retval);
  865. return retval;
  866. }
  867. }
  868. retval = usb_new_device (usb_dev);
  869. if (retval) {
  870. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  871. dev_name(&usb_dev->dev), retval);
  872. } else {
  873. spin_lock_irq (&hcd_root_hub_lock);
  874. hcd->rh_registered = 1;
  875. spin_unlock_irq (&hcd_root_hub_lock);
  876. /* Did the HC die before the root hub was registered? */
  877. if (HCD_DEAD(hcd))
  878. usb_hc_died (hcd); /* This time clean up */
  879. }
  880. mutex_unlock(&usb_bus_idr_lock);
  881. return retval;
  882. }
  883. /*
  884. * usb_hcd_start_port_resume - a root-hub port is sending a resume signal
  885. * @bus: the bus which the root hub belongs to
  886. * @portnum: the port which is being resumed
  887. *
  888. * HCDs should call this function when they know that a resume signal is
  889. * being sent to a root-hub port. The root hub will be prevented from
  890. * going into autosuspend until usb_hcd_end_port_resume() is called.
  891. *
  892. * The bus's private lock must be held by the caller.
  893. */
  894. void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
  895. {
  896. unsigned bit = 1 << portnum;
  897. if (!(bus->resuming_ports & bit)) {
  898. bus->resuming_ports |= bit;
  899. pm_runtime_get_noresume(&bus->root_hub->dev);
  900. }
  901. }
  902. EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
  903. /*
  904. * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal
  905. * @bus: the bus which the root hub belongs to
  906. * @portnum: the port which is being resumed
  907. *
  908. * HCDs should call this function when they know that a resume signal has
  909. * stopped being sent to a root-hub port. The root hub will be allowed to
  910. * autosuspend again.
  911. *
  912. * The bus's private lock must be held by the caller.
  913. */
  914. void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
  915. {
  916. unsigned bit = 1 << portnum;
  917. if (bus->resuming_ports & bit) {
  918. bus->resuming_ports &= ~bit;
  919. pm_runtime_put_noidle(&bus->root_hub->dev);
  920. }
  921. }
  922. EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
  923. /*-------------------------------------------------------------------------*/
  924. /**
  925. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  926. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  927. * @is_input: true iff the transaction sends data to the host
  928. * @isoc: true for isochronous transactions, false for interrupt ones
  929. * @bytecount: how many bytes in the transaction.
  930. *
  931. * Return: Approximate bus time in nanoseconds for a periodic transaction.
  932. *
  933. * Note:
  934. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  935. * scheduled in software, this function is only used for such scheduling.
  936. */
  937. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  938. {
  939. unsigned long tmp;
  940. switch (speed) {
  941. case USB_SPEED_LOW: /* INTR only */
  942. if (is_input) {
  943. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  944. return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  945. } else {
  946. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  947. return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  948. }
  949. case USB_SPEED_FULL: /* ISOC or INTR */
  950. if (isoc) {
  951. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  952. return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
  953. } else {
  954. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  955. return 9107L + BW_HOST_DELAY + tmp;
  956. }
  957. case USB_SPEED_HIGH: /* ISOC or INTR */
  958. /* FIXME adjust for input vs output */
  959. if (isoc)
  960. tmp = HS_NSECS_ISO (bytecount);
  961. else
  962. tmp = HS_NSECS (bytecount);
  963. return tmp;
  964. default:
  965. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  966. return -1;
  967. }
  968. }
  969. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  970. /*-------------------------------------------------------------------------*/
  971. /*
  972. * Generic HC operations.
  973. */
  974. /*-------------------------------------------------------------------------*/
  975. /**
  976. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  977. * @hcd: host controller to which @urb was submitted
  978. * @urb: URB being submitted
  979. *
  980. * Host controller drivers should call this routine in their enqueue()
  981. * method. The HCD's private spinlock must be held and interrupts must
  982. * be disabled. The actions carried out here are required for URB
  983. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  984. *
  985. * Return: 0 for no error, otherwise a negative error code (in which case
  986. * the enqueue() method must fail). If no error occurs but enqueue() fails
  987. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  988. * the private spinlock and returning.
  989. */
  990. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  991. {
  992. int rc = 0;
  993. spin_lock(&hcd_urb_list_lock);
  994. /* Check that the URB isn't being killed */
  995. if (unlikely(atomic_read(&urb->reject))) {
  996. rc = -EPERM;
  997. goto done;
  998. }
  999. if (unlikely(!urb->ep->enabled)) {
  1000. rc = -ENOENT;
  1001. goto done;
  1002. }
  1003. if (unlikely(!urb->dev->can_submit)) {
  1004. rc = -EHOSTUNREACH;
  1005. goto done;
  1006. }
  1007. /*
  1008. * Check the host controller's state and add the URB to the
  1009. * endpoint's queue.
  1010. */
  1011. if (HCD_RH_RUNNING(hcd)) {
  1012. urb->unlinked = 0;
  1013. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  1014. } else {
  1015. rc = -ESHUTDOWN;
  1016. goto done;
  1017. }
  1018. done:
  1019. spin_unlock(&hcd_urb_list_lock);
  1020. return rc;
  1021. }
  1022. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  1023. /**
  1024. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  1025. * @hcd: host controller to which @urb was submitted
  1026. * @urb: URB being checked for unlinkability
  1027. * @status: error code to store in @urb if the unlink succeeds
  1028. *
  1029. * Host controller drivers should call this routine in their dequeue()
  1030. * method. The HCD's private spinlock must be held and interrupts must
  1031. * be disabled. The actions carried out here are required for making
  1032. * sure than an unlink is valid.
  1033. *
  1034. * Return: 0 for no error, otherwise a negative error code (in which case
  1035. * the dequeue() method must fail). The possible error codes are:
  1036. *
  1037. * -EIDRM: @urb was not submitted or has already completed.
  1038. * The completion function may not have been called yet.
  1039. *
  1040. * -EBUSY: @urb has already been unlinked.
  1041. */
  1042. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  1043. int status)
  1044. {
  1045. struct list_head *tmp;
  1046. /* insist the urb is still queued */
  1047. list_for_each(tmp, &urb->ep->urb_list) {
  1048. if (tmp == &urb->urb_list)
  1049. break;
  1050. }
  1051. if (tmp != &urb->urb_list)
  1052. return -EIDRM;
  1053. /* Any status except -EINPROGRESS means something already started to
  1054. * unlink this URB from the hardware. So there's no more work to do.
  1055. */
  1056. if (urb->unlinked)
  1057. return -EBUSY;
  1058. urb->unlinked = status;
  1059. return 0;
  1060. }
  1061. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  1062. /**
  1063. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  1064. * @hcd: host controller to which @urb was submitted
  1065. * @urb: URB being unlinked
  1066. *
  1067. * Host controller drivers should call this routine before calling
  1068. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  1069. * interrupts must be disabled. The actions carried out here are required
  1070. * for URB completion.
  1071. */
  1072. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  1073. {
  1074. /* clear all state linking urb to this dev (and hcd) */
  1075. spin_lock(&hcd_urb_list_lock);
  1076. list_del_init(&urb->urb_list);
  1077. spin_unlock(&hcd_urb_list_lock);
  1078. }
  1079. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  1080. /*
  1081. * Some usb host controllers can only perform dma using a small SRAM area,
  1082. * or have restrictions on addressable DRAM.
  1083. * The usb core itself is however optimized for host controllers that can dma
  1084. * using regular system memory - like pci devices doing bus mastering.
  1085. *
  1086. * To support host controllers with limited dma capabilities we provide dma
  1087. * bounce buffers. This feature can be enabled by initializing
  1088. * hcd->localmem_pool using usb_hcd_setup_local_mem().
  1089. *
  1090. * The initialized hcd->localmem_pool then tells the usb code to allocate all
  1091. * data for dma using the genalloc API.
  1092. *
  1093. * So, to summarize...
  1094. *
  1095. * - We need "local" memory, canonical example being
  1096. * a small SRAM on a discrete controller being the
  1097. * only memory that the controller can read ...
  1098. * (a) "normal" kernel memory is no good, and
  1099. * (b) there's not enough to share
  1100. *
  1101. * - So we use that, even though the primary requirement
  1102. * is that the memory be "local" (hence addressable
  1103. * by that device), not "coherent".
  1104. *
  1105. */
  1106. static int hcd_alloc_coherent(struct usb_bus *bus,
  1107. gfp_t mem_flags, dma_addr_t *dma_handle,
  1108. void **vaddr_handle, size_t size,
  1109. enum dma_data_direction dir)
  1110. {
  1111. unsigned char *vaddr;
  1112. if (*vaddr_handle == NULL) {
  1113. WARN_ON_ONCE(1);
  1114. return -EFAULT;
  1115. }
  1116. vaddr = hcd_buffer_alloc(bus, size + sizeof(unsigned long),
  1117. mem_flags, dma_handle);
  1118. if (!vaddr)
  1119. return -ENOMEM;
  1120. /*
  1121. * Store the virtual address of the buffer at the end
  1122. * of the allocated dma buffer. The size of the buffer
  1123. * may be uneven so use unaligned functions instead
  1124. * of just rounding up. It makes sense to optimize for
  1125. * memory footprint over access speed since the amount
  1126. * of memory available for dma may be limited.
  1127. */
  1128. put_unaligned((unsigned long)*vaddr_handle,
  1129. (unsigned long *)(vaddr + size));
  1130. if (dir == DMA_TO_DEVICE)
  1131. memcpy(vaddr, *vaddr_handle, size);
  1132. *vaddr_handle = vaddr;
  1133. return 0;
  1134. }
  1135. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1136. void **vaddr_handle, size_t size,
  1137. enum dma_data_direction dir)
  1138. {
  1139. unsigned char *vaddr = *vaddr_handle;
  1140. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1141. if (dir == DMA_FROM_DEVICE)
  1142. memcpy(vaddr, *vaddr_handle, size);
  1143. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1144. *vaddr_handle = vaddr;
  1145. *dma_handle = 0;
  1146. }
  1147. void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1148. {
  1149. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1150. (urb->transfer_flags & URB_SETUP_MAP_SINGLE))
  1151. dma_unmap_single(hcd->self.sysdev,
  1152. urb->setup_dma,
  1153. sizeof(struct usb_ctrlrequest),
  1154. DMA_TO_DEVICE);
  1155. else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
  1156. hcd_free_coherent(urb->dev->bus,
  1157. &urb->setup_dma,
  1158. (void **) &urb->setup_packet,
  1159. sizeof(struct usb_ctrlrequest),
  1160. DMA_TO_DEVICE);
  1161. /* Make it safe to call this routine more than once */
  1162. urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
  1163. }
  1164. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
  1165. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1166. {
  1167. if (hcd->driver->unmap_urb_for_dma)
  1168. hcd->driver->unmap_urb_for_dma(hcd, urb);
  1169. else
  1170. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1171. }
  1172. void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1173. {
  1174. enum dma_data_direction dir;
  1175. usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
  1176. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1177. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1178. (urb->transfer_flags & URB_DMA_MAP_SG))
  1179. dma_unmap_sg(hcd->self.sysdev,
  1180. urb->sg,
  1181. urb->num_sgs,
  1182. dir);
  1183. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1184. (urb->transfer_flags & URB_DMA_MAP_PAGE))
  1185. dma_unmap_page(hcd->self.sysdev,
  1186. urb->transfer_dma,
  1187. urb->transfer_buffer_length,
  1188. dir);
  1189. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1190. (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  1191. dma_unmap_single(hcd->self.sysdev,
  1192. urb->transfer_dma,
  1193. urb->transfer_buffer_length,
  1194. dir);
  1195. else if (urb->transfer_flags & URB_MAP_LOCAL)
  1196. hcd_free_coherent(urb->dev->bus,
  1197. &urb->transfer_dma,
  1198. &urb->transfer_buffer,
  1199. urb->transfer_buffer_length,
  1200. dir);
  1201. /* Make it safe to call this routine more than once */
  1202. urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
  1203. URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
  1204. }
  1205. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
  1206. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1207. gfp_t mem_flags)
  1208. {
  1209. if (hcd->driver->map_urb_for_dma)
  1210. return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
  1211. else
  1212. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  1213. }
  1214. int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1215. gfp_t mem_flags)
  1216. {
  1217. enum dma_data_direction dir;
  1218. int ret = 0;
  1219. /* Map the URB's buffers for DMA access.
  1220. * Lower level HCD code should use *_dma exclusively,
  1221. * unless it uses pio or talks to another transport,
  1222. * or uses the provided scatter gather list for bulk.
  1223. */
  1224. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  1225. if (hcd->self.uses_pio_for_control)
  1226. return ret;
  1227. if (hcd->localmem_pool) {
  1228. ret = hcd_alloc_coherent(
  1229. urb->dev->bus, mem_flags,
  1230. &urb->setup_dma,
  1231. (void **)&urb->setup_packet,
  1232. sizeof(struct usb_ctrlrequest),
  1233. DMA_TO_DEVICE);
  1234. if (ret)
  1235. return ret;
  1236. urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
  1237. } else if (hcd_uses_dma(hcd)) {
  1238. if (object_is_on_stack(urb->setup_packet)) {
  1239. WARN_ONCE(1, "setup packet is on stack\n");
  1240. return -EAGAIN;
  1241. }
  1242. urb->setup_dma = dma_map_single(
  1243. hcd->self.sysdev,
  1244. urb->setup_packet,
  1245. sizeof(struct usb_ctrlrequest),
  1246. DMA_TO_DEVICE);
  1247. if (dma_mapping_error(hcd->self.sysdev,
  1248. urb->setup_dma))
  1249. return -EAGAIN;
  1250. urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
  1251. }
  1252. }
  1253. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1254. if (urb->transfer_buffer_length != 0
  1255. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1256. if (hcd->localmem_pool) {
  1257. ret = hcd_alloc_coherent(
  1258. urb->dev->bus, mem_flags,
  1259. &urb->transfer_dma,
  1260. &urb->transfer_buffer,
  1261. urb->transfer_buffer_length,
  1262. dir);
  1263. if (ret == 0)
  1264. urb->transfer_flags |= URB_MAP_LOCAL;
  1265. } else if (hcd_uses_dma(hcd)) {
  1266. if (urb->num_sgs) {
  1267. int n;
  1268. /* We don't support sg for isoc transfers ! */
  1269. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1270. WARN_ON(1);
  1271. return -EINVAL;
  1272. }
  1273. n = dma_map_sg(
  1274. hcd->self.sysdev,
  1275. urb->sg,
  1276. urb->num_sgs,
  1277. dir);
  1278. if (!n)
  1279. ret = -EAGAIN;
  1280. else
  1281. urb->transfer_flags |= URB_DMA_MAP_SG;
  1282. urb->num_mapped_sgs = n;
  1283. if (n != urb->num_sgs)
  1284. urb->transfer_flags |=
  1285. URB_DMA_SG_COMBINED;
  1286. } else if (urb->sg) {
  1287. struct scatterlist *sg = urb->sg;
  1288. urb->transfer_dma = dma_map_page(
  1289. hcd->self.sysdev,
  1290. sg_page(sg),
  1291. sg->offset,
  1292. urb->transfer_buffer_length,
  1293. dir);
  1294. if (dma_mapping_error(hcd->self.sysdev,
  1295. urb->transfer_dma))
  1296. ret = -EAGAIN;
  1297. else
  1298. urb->transfer_flags |= URB_DMA_MAP_PAGE;
  1299. } else if (object_is_on_stack(urb->transfer_buffer)) {
  1300. WARN_ONCE(1, "transfer buffer is on stack\n");
  1301. ret = -EAGAIN;
  1302. } else {
  1303. urb->transfer_dma = dma_map_single(
  1304. hcd->self.sysdev,
  1305. urb->transfer_buffer,
  1306. urb->transfer_buffer_length,
  1307. dir);
  1308. if (dma_mapping_error(hcd->self.sysdev,
  1309. urb->transfer_dma))
  1310. ret = -EAGAIN;
  1311. else
  1312. urb->transfer_flags |= URB_DMA_MAP_SINGLE;
  1313. }
  1314. }
  1315. if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
  1316. URB_SETUP_MAP_LOCAL)))
  1317. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1318. }
  1319. return ret;
  1320. }
  1321. EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
  1322. /*-------------------------------------------------------------------------*/
  1323. /* may be called in any context with a valid urb->dev usecount
  1324. * caller surrenders "ownership" of urb
  1325. * expects usb_submit_urb() to have sanity checked and conditioned all
  1326. * inputs in the urb
  1327. */
  1328. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1329. {
  1330. int status;
  1331. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1332. /* increment urb's reference count as part of giving it to the HCD
  1333. * (which will control it). HCD guarantees that it either returns
  1334. * an error or calls giveback(), but not both.
  1335. */
  1336. usb_get_urb(urb);
  1337. atomic_inc(&urb->use_count);
  1338. atomic_inc(&urb->dev->urbnum);
  1339. usbmon_urb_submit(&hcd->self, urb);
  1340. /* NOTE requirements on root-hub callers (usbfs and the hub
  1341. * driver, for now): URBs' urb->transfer_buffer must be
  1342. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1343. * they could clobber root hub response data. Also, control
  1344. * URBs must be submitted in process context with interrupts
  1345. * enabled.
  1346. */
  1347. if (is_root_hub(urb->dev)) {
  1348. status = rh_urb_enqueue(hcd, urb);
  1349. } else {
  1350. status = map_urb_for_dma(hcd, urb, mem_flags);
  1351. if (likely(status == 0)) {
  1352. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1353. if (unlikely(status))
  1354. unmap_urb_for_dma(hcd, urb);
  1355. }
  1356. }
  1357. if (unlikely(status)) {
  1358. usbmon_urb_submit_error(&hcd->self, urb, status);
  1359. urb->hcpriv = NULL;
  1360. INIT_LIST_HEAD(&urb->urb_list);
  1361. atomic_dec(&urb->use_count);
  1362. /*
  1363. * Order the write of urb->use_count above before the read
  1364. * of urb->reject below. Pairs with the memory barriers in
  1365. * usb_kill_urb() and usb_poison_urb().
  1366. */
  1367. smp_mb__after_atomic();
  1368. atomic_dec(&urb->dev->urbnum);
  1369. if (atomic_read(&urb->reject))
  1370. wake_up(&usb_kill_urb_queue);
  1371. usb_put_urb(urb);
  1372. }
  1373. return status;
  1374. }
  1375. /*-------------------------------------------------------------------------*/
  1376. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1377. * off hardware queues (which may take a while) and returning it as
  1378. * soon as practical. we've already set up the urb's return status,
  1379. * but we can't know if the callback completed already.
  1380. */
  1381. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1382. {
  1383. int value;
  1384. if (is_root_hub(urb->dev))
  1385. value = usb_rh_urb_dequeue(hcd, urb, status);
  1386. else {
  1387. /* The only reason an HCD might fail this call is if
  1388. * it has not yet fully queued the urb to begin with.
  1389. * Such failures should be harmless. */
  1390. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1391. }
  1392. return value;
  1393. }
  1394. /*
  1395. * called in any context
  1396. *
  1397. * caller guarantees urb won't be recycled till both unlink()
  1398. * and the urb's completion function return
  1399. */
  1400. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1401. {
  1402. struct usb_hcd *hcd;
  1403. struct usb_device *udev = urb->dev;
  1404. int retval = -EIDRM;
  1405. unsigned long flags;
  1406. /* Prevent the device and bus from going away while
  1407. * the unlink is carried out. If they are already gone
  1408. * then urb->use_count must be 0, since disconnected
  1409. * devices can't have any active URBs.
  1410. */
  1411. spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
  1412. if (atomic_read(&urb->use_count) > 0) {
  1413. retval = 0;
  1414. usb_get_dev(udev);
  1415. }
  1416. spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
  1417. if (retval == 0) {
  1418. hcd = bus_to_hcd(urb->dev->bus);
  1419. retval = unlink1(hcd, urb, status);
  1420. if (retval == 0)
  1421. retval = -EINPROGRESS;
  1422. else if (retval != -EIDRM && retval != -EBUSY)
  1423. dev_dbg(&udev->dev, "hcd_unlink_urb %pK fail %d\n",
  1424. urb, retval);
  1425. usb_put_dev(udev);
  1426. }
  1427. return retval;
  1428. }
  1429. /*-------------------------------------------------------------------------*/
  1430. static void __usb_hcd_giveback_urb(struct urb *urb)
  1431. {
  1432. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1433. struct usb_anchor *anchor = urb->anchor;
  1434. int status = urb->unlinked;
  1435. urb->hcpriv = NULL;
  1436. if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1437. urb->actual_length < urb->transfer_buffer_length &&
  1438. !status))
  1439. status = -EREMOTEIO;
  1440. unmap_urb_for_dma(hcd, urb);
  1441. usbmon_urb_complete(&hcd->self, urb, status);
  1442. usb_anchor_suspend_wakeups(anchor);
  1443. usb_unanchor_urb(urb);
  1444. if (likely(status == 0))
  1445. usb_led_activity(USB_LED_EVENT_HOST);
  1446. /* pass ownership to the completion handler */
  1447. urb->status = status;
  1448. /*
  1449. * This function can be called in task context inside another remote
  1450. * coverage collection section, but kcov doesn't support that kind of
  1451. * recursion yet. Only collect coverage in softirq context for now.
  1452. */
  1453. kcov_remote_start_usb_softirq((u64)urb->dev->bus->busnum);
  1454. urb->complete(urb);
  1455. kcov_remote_stop_softirq();
  1456. usb_anchor_resume_wakeups(anchor);
  1457. atomic_dec(&urb->use_count);
  1458. /*
  1459. * Order the write of urb->use_count above before the read
  1460. * of urb->reject below. Pairs with the memory barriers in
  1461. * usb_kill_urb() and usb_poison_urb().
  1462. */
  1463. smp_mb__after_atomic();
  1464. if (unlikely(atomic_read(&urb->reject)))
  1465. wake_up(&usb_kill_urb_queue);
  1466. usb_put_urb(urb);
  1467. }
  1468. static void usb_giveback_urb_bh(struct work_struct *work)
  1469. {
  1470. struct giveback_urb_bh *bh =
  1471. container_of(work, struct giveback_urb_bh, bh);
  1472. struct list_head local_list;
  1473. spin_lock_irq(&bh->lock);
  1474. bh->running = true;
  1475. list_replace_init(&bh->head, &local_list);
  1476. spin_unlock_irq(&bh->lock);
  1477. while (!list_empty(&local_list)) {
  1478. struct urb *urb;
  1479. urb = list_entry(local_list.next, struct urb, urb_list);
  1480. list_del_init(&urb->urb_list);
  1481. bh->completing_ep = urb->ep;
  1482. __usb_hcd_giveback_urb(urb);
  1483. bh->completing_ep = NULL;
  1484. }
  1485. /*
  1486. * giveback new URBs next time to prevent this function
  1487. * from not exiting for a long time.
  1488. */
  1489. spin_lock_irq(&bh->lock);
  1490. if (!list_empty(&bh->head)) {
  1491. if (bh->high_prio)
  1492. queue_work(system_bh_highpri_wq, &bh->bh);
  1493. else
  1494. queue_work(system_bh_wq, &bh->bh);
  1495. }
  1496. bh->running = false;
  1497. spin_unlock_irq(&bh->lock);
  1498. }
  1499. /**
  1500. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1501. * @hcd: host controller returning the URB
  1502. * @urb: urb being returned to the USB device driver.
  1503. * @status: completion status code for the URB.
  1504. *
  1505. * Context: atomic. The completion callback is invoked in caller's context.
  1506. * For HCDs with HCD_BH flag set, the completion callback is invoked in BH
  1507. * context (except for URBs submitted to the root hub which always complete in
  1508. * caller's context).
  1509. *
  1510. * This hands the URB from HCD to its USB device driver, using its
  1511. * completion function. The HCD has freed all per-urb resources
  1512. * (and is done using urb->hcpriv). It also released all HCD locks;
  1513. * the device driver won't cause problems if it frees, modifies,
  1514. * or resubmits this URB.
  1515. *
  1516. * If @urb was unlinked, the value of @status will be overridden by
  1517. * @urb->unlinked. Erroneous short transfers are detected in case
  1518. * the HCD hasn't checked for them.
  1519. */
  1520. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1521. {
  1522. struct giveback_urb_bh *bh;
  1523. bool running;
  1524. /* pass status to BH via unlinked */
  1525. if (likely(!urb->unlinked))
  1526. urb->unlinked = status;
  1527. if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
  1528. __usb_hcd_giveback_urb(urb);
  1529. return;
  1530. }
  1531. if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe))
  1532. bh = &hcd->high_prio_bh;
  1533. else
  1534. bh = &hcd->low_prio_bh;
  1535. spin_lock(&bh->lock);
  1536. list_add_tail(&urb->urb_list, &bh->head);
  1537. running = bh->running;
  1538. spin_unlock(&bh->lock);
  1539. if (running)
  1540. ;
  1541. else if (bh->high_prio)
  1542. queue_work(system_bh_highpri_wq, &bh->bh);
  1543. else
  1544. queue_work(system_bh_wq, &bh->bh);
  1545. }
  1546. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1547. /*-------------------------------------------------------------------------*/
  1548. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1549. * queue to drain completely. The caller must first insure that no more
  1550. * URBs can be submitted for this endpoint.
  1551. */
  1552. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1553. struct usb_host_endpoint *ep)
  1554. {
  1555. struct usb_hcd *hcd;
  1556. struct urb *urb;
  1557. if (!ep)
  1558. return;
  1559. might_sleep();
  1560. hcd = bus_to_hcd(udev->bus);
  1561. /* No more submits can occur */
  1562. spin_lock_irq(&hcd_urb_list_lock);
  1563. rescan:
  1564. list_for_each_entry_reverse(urb, &ep->urb_list, urb_list) {
  1565. int is_in;
  1566. if (urb->unlinked)
  1567. continue;
  1568. usb_get_urb (urb);
  1569. is_in = usb_urb_dir_in(urb);
  1570. spin_unlock(&hcd_urb_list_lock);
  1571. /* kick hcd */
  1572. unlink1(hcd, urb, -ESHUTDOWN);
  1573. dev_dbg (hcd->self.controller,
  1574. "shutdown urb %pK ep%d%s-%s\n",
  1575. urb, usb_endpoint_num(&ep->desc),
  1576. is_in ? "in" : "out",
  1577. usb_ep_type_string(usb_endpoint_type(&ep->desc)));
  1578. usb_put_urb (urb);
  1579. /* list contents may have changed */
  1580. spin_lock(&hcd_urb_list_lock);
  1581. goto rescan;
  1582. }
  1583. spin_unlock_irq(&hcd_urb_list_lock);
  1584. /* Wait until the endpoint queue is completely empty */
  1585. while (!list_empty (&ep->urb_list)) {
  1586. spin_lock_irq(&hcd_urb_list_lock);
  1587. /* The list may have changed while we acquired the spinlock */
  1588. urb = NULL;
  1589. if (!list_empty (&ep->urb_list)) {
  1590. urb = list_entry (ep->urb_list.prev, struct urb,
  1591. urb_list);
  1592. usb_get_urb (urb);
  1593. }
  1594. spin_unlock_irq(&hcd_urb_list_lock);
  1595. if (urb) {
  1596. usb_kill_urb (urb);
  1597. usb_put_urb (urb);
  1598. }
  1599. }
  1600. }
  1601. /**
  1602. * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
  1603. * the bus bandwidth
  1604. * @udev: target &usb_device
  1605. * @new_config: new configuration to install
  1606. * @cur_alt: the current alternate interface setting
  1607. * @new_alt: alternate interface setting that is being installed
  1608. *
  1609. * To change configurations, pass in the new configuration in new_config,
  1610. * and pass NULL for cur_alt and new_alt.
  1611. *
  1612. * To reset a device's configuration (put the device in the ADDRESSED state),
  1613. * pass in NULL for new_config, cur_alt, and new_alt.
  1614. *
  1615. * To change alternate interface settings, pass in NULL for new_config,
  1616. * pass in the current alternate interface setting in cur_alt,
  1617. * and pass in the new alternate interface setting in new_alt.
  1618. *
  1619. * Return: An error if the requested bandwidth change exceeds the
  1620. * bus bandwidth or host controller internal resources.
  1621. */
  1622. int usb_hcd_alloc_bandwidth(struct usb_device *udev,
  1623. struct usb_host_config *new_config,
  1624. struct usb_host_interface *cur_alt,
  1625. struct usb_host_interface *new_alt)
  1626. {
  1627. int num_intfs, i, j;
  1628. struct usb_host_interface *alt = NULL;
  1629. int ret = 0;
  1630. struct usb_hcd *hcd;
  1631. struct usb_host_endpoint *ep;
  1632. hcd = bus_to_hcd(udev->bus);
  1633. if (!hcd->driver->check_bandwidth)
  1634. return 0;
  1635. /* Configuration is being removed - set configuration 0 */
  1636. if (!new_config && !cur_alt) {
  1637. for (i = 1; i < 16; ++i) {
  1638. ep = udev->ep_out[i];
  1639. if (ep)
  1640. hcd->driver->drop_endpoint(hcd, udev, ep);
  1641. ep = udev->ep_in[i];
  1642. if (ep)
  1643. hcd->driver->drop_endpoint(hcd, udev, ep);
  1644. }
  1645. hcd->driver->check_bandwidth(hcd, udev);
  1646. return 0;
  1647. }
  1648. /* Check if the HCD says there's enough bandwidth. Enable all endpoints
  1649. * each interface's alt setting 0 and ask the HCD to check the bandwidth
  1650. * of the bus. There will always be bandwidth for endpoint 0, so it's
  1651. * ok to exclude it.
  1652. */
  1653. if (new_config) {
  1654. num_intfs = new_config->desc.bNumInterfaces;
  1655. /* Remove endpoints (except endpoint 0, which is always on the
  1656. * schedule) from the old config from the schedule
  1657. */
  1658. for (i = 1; i < 16; ++i) {
  1659. ep = udev->ep_out[i];
  1660. if (ep) {
  1661. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1662. if (ret < 0)
  1663. goto reset;
  1664. }
  1665. ep = udev->ep_in[i];
  1666. if (ep) {
  1667. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1668. if (ret < 0)
  1669. goto reset;
  1670. }
  1671. }
  1672. for (i = 0; i < num_intfs; ++i) {
  1673. struct usb_host_interface *first_alt;
  1674. int iface_num;
  1675. first_alt = &new_config->intf_cache[i]->altsetting[0];
  1676. iface_num = first_alt->desc.bInterfaceNumber;
  1677. /* Set up endpoints for alternate interface setting 0 */
  1678. alt = usb_find_alt_setting(new_config, iface_num, 0);
  1679. if (!alt)
  1680. /* No alt setting 0? Pick the first setting. */
  1681. alt = first_alt;
  1682. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  1683. ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
  1684. if (ret < 0)
  1685. goto reset;
  1686. }
  1687. }
  1688. }
  1689. if (cur_alt && new_alt) {
  1690. struct usb_interface *iface = usb_ifnum_to_if(udev,
  1691. cur_alt->desc.bInterfaceNumber);
  1692. if (!iface)
  1693. return -EINVAL;
  1694. if (iface->resetting_device) {
  1695. /*
  1696. * The USB core just reset the device, so the xHCI host
  1697. * and the device will think alt setting 0 is installed.
  1698. * However, the USB core will pass in the alternate
  1699. * setting installed before the reset as cur_alt. Dig
  1700. * out the alternate setting 0 structure, or the first
  1701. * alternate setting if a broken device doesn't have alt
  1702. * setting 0.
  1703. */
  1704. cur_alt = usb_altnum_to_altsetting(iface, 0);
  1705. if (!cur_alt)
  1706. cur_alt = &iface->altsetting[0];
  1707. }
  1708. /* Drop all the endpoints in the current alt setting */
  1709. for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
  1710. ret = hcd->driver->drop_endpoint(hcd, udev,
  1711. &cur_alt->endpoint[i]);
  1712. if (ret < 0)
  1713. goto reset;
  1714. }
  1715. /* Add all the endpoints in the new alt setting */
  1716. for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
  1717. ret = hcd->driver->add_endpoint(hcd, udev,
  1718. &new_alt->endpoint[i]);
  1719. if (ret < 0)
  1720. goto reset;
  1721. }
  1722. }
  1723. ret = hcd->driver->check_bandwidth(hcd, udev);
  1724. reset:
  1725. if (ret < 0)
  1726. hcd->driver->reset_bandwidth(hcd, udev);
  1727. return ret;
  1728. }
  1729. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1730. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1731. * have been called previously. Use for set_configuration, set_interface,
  1732. * driver removal, physical disconnect.
  1733. *
  1734. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1735. * type, maxpacket size, toggle, halt status, and scheduling.
  1736. */
  1737. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1738. struct usb_host_endpoint *ep)
  1739. {
  1740. struct usb_hcd *hcd;
  1741. might_sleep();
  1742. hcd = bus_to_hcd(udev->bus);
  1743. if (hcd->driver->endpoint_disable)
  1744. hcd->driver->endpoint_disable(hcd, ep);
  1745. }
  1746. /**
  1747. * usb_hcd_reset_endpoint - reset host endpoint state
  1748. * @udev: USB device.
  1749. * @ep: the endpoint to reset.
  1750. *
  1751. * Resets any host endpoint state such as the toggle bit, sequence
  1752. * number and current window.
  1753. */
  1754. void usb_hcd_reset_endpoint(struct usb_device *udev,
  1755. struct usb_host_endpoint *ep)
  1756. {
  1757. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1758. if (hcd->driver->endpoint_reset)
  1759. hcd->driver->endpoint_reset(hcd, ep);
  1760. else {
  1761. int epnum = usb_endpoint_num(&ep->desc);
  1762. int is_out = usb_endpoint_dir_out(&ep->desc);
  1763. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1764. usb_settoggle(udev, epnum, is_out, 0);
  1765. if (is_control)
  1766. usb_settoggle(udev, epnum, !is_out, 0);
  1767. }
  1768. }
  1769. /**
  1770. * usb_alloc_streams - allocate bulk endpoint stream IDs.
  1771. * @interface: alternate setting that includes all endpoints.
  1772. * @eps: array of endpoints that need streams.
  1773. * @num_eps: number of endpoints in the array.
  1774. * @num_streams: number of streams to allocate.
  1775. * @mem_flags: flags hcd should use to allocate memory.
  1776. *
  1777. * Sets up a group of bulk endpoints to have @num_streams stream IDs available.
  1778. * Drivers may queue multiple transfers to different stream IDs, which may
  1779. * complete in a different order than they were queued.
  1780. *
  1781. * Return: On success, the number of allocated streams. On failure, a negative
  1782. * error code.
  1783. */
  1784. int usb_alloc_streams(struct usb_interface *interface,
  1785. struct usb_host_endpoint **eps, unsigned int num_eps,
  1786. unsigned int num_streams, gfp_t mem_flags)
  1787. {
  1788. struct usb_hcd *hcd;
  1789. struct usb_device *dev;
  1790. int i, ret;
  1791. dev = interface_to_usbdev(interface);
  1792. hcd = bus_to_hcd(dev->bus);
  1793. if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
  1794. return -EINVAL;
  1795. if (dev->speed < USB_SPEED_SUPER)
  1796. return -EINVAL;
  1797. if (dev->state < USB_STATE_CONFIGURED)
  1798. return -ENODEV;
  1799. for (i = 0; i < num_eps; i++) {
  1800. /* Streams only apply to bulk endpoints. */
  1801. if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
  1802. return -EINVAL;
  1803. /* Re-alloc is not allowed */
  1804. if (eps[i]->streams)
  1805. return -EINVAL;
  1806. }
  1807. ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
  1808. num_streams, mem_flags);
  1809. if (ret < 0)
  1810. return ret;
  1811. for (i = 0; i < num_eps; i++)
  1812. eps[i]->streams = ret;
  1813. return ret;
  1814. }
  1815. EXPORT_SYMBOL_GPL(usb_alloc_streams);
  1816. /**
  1817. * usb_free_streams - free bulk endpoint stream IDs.
  1818. * @interface: alternate setting that includes all endpoints.
  1819. * @eps: array of endpoints to remove streams from.
  1820. * @num_eps: number of endpoints in the array.
  1821. * @mem_flags: flags hcd should use to allocate memory.
  1822. *
  1823. * Reverts a group of bulk endpoints back to not using stream IDs.
  1824. * Can fail if we are given bad arguments, or HCD is broken.
  1825. *
  1826. * Return: 0 on success. On failure, a negative error code.
  1827. */
  1828. int usb_free_streams(struct usb_interface *interface,
  1829. struct usb_host_endpoint **eps, unsigned int num_eps,
  1830. gfp_t mem_flags)
  1831. {
  1832. struct usb_hcd *hcd;
  1833. struct usb_device *dev;
  1834. int i, ret;
  1835. dev = interface_to_usbdev(interface);
  1836. hcd = bus_to_hcd(dev->bus);
  1837. if (dev->speed < USB_SPEED_SUPER)
  1838. return -EINVAL;
  1839. /* Double-free is not allowed */
  1840. for (i = 0; i < num_eps; i++)
  1841. if (!eps[i] || !eps[i]->streams)
  1842. return -EINVAL;
  1843. ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
  1844. if (ret < 0)
  1845. return ret;
  1846. for (i = 0; i < num_eps; i++)
  1847. eps[i]->streams = 0;
  1848. return ret;
  1849. }
  1850. EXPORT_SYMBOL_GPL(usb_free_streams);
  1851. /* Protect against drivers that try to unlink URBs after the device
  1852. * is gone, by waiting until all unlinks for @udev are finished.
  1853. * Since we don't currently track URBs by device, simply wait until
  1854. * nothing is running in the locked region of usb_hcd_unlink_urb().
  1855. */
  1856. void usb_hcd_synchronize_unlinks(struct usb_device *udev)
  1857. {
  1858. spin_lock_irq(&hcd_urb_unlink_lock);
  1859. spin_unlock_irq(&hcd_urb_unlink_lock);
  1860. }
  1861. /*-------------------------------------------------------------------------*/
  1862. /* called in any context */
  1863. int usb_hcd_get_frame_number (struct usb_device *udev)
  1864. {
  1865. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1866. if (!HCD_RH_RUNNING(hcd))
  1867. return -ESHUTDOWN;
  1868. return hcd->driver->get_frame_number (hcd);
  1869. }
  1870. /*-------------------------------------------------------------------------*/
  1871. #ifdef CONFIG_USB_HCD_TEST_MODE
  1872. static void usb_ehset_completion(struct urb *urb)
  1873. {
  1874. struct completion *done = urb->context;
  1875. complete(done);
  1876. }
  1877. /*
  1878. * Allocate and initialize a control URB. This request will be used by the
  1879. * EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages
  1880. * of the GetDescriptor request are sent 15 seconds after the SETUP stage.
  1881. * Return NULL if failed.
  1882. */
  1883. static struct urb *request_single_step_set_feature_urb(
  1884. struct usb_device *udev,
  1885. void *dr,
  1886. void *buf,
  1887. struct completion *done)
  1888. {
  1889. struct urb *urb;
  1890. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1891. urb = usb_alloc_urb(0, GFP_KERNEL);
  1892. if (!urb)
  1893. return NULL;
  1894. urb->pipe = usb_rcvctrlpipe(udev, 0);
  1895. urb->ep = &udev->ep0;
  1896. urb->dev = udev;
  1897. urb->setup_packet = (void *)dr;
  1898. urb->transfer_buffer = buf;
  1899. urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
  1900. urb->complete = usb_ehset_completion;
  1901. urb->status = -EINPROGRESS;
  1902. urb->actual_length = 0;
  1903. urb->transfer_flags = URB_DIR_IN | URB_NO_TRANSFER_DMA_MAP;
  1904. usb_get_urb(urb);
  1905. atomic_inc(&urb->use_count);
  1906. atomic_inc(&urb->dev->urbnum);
  1907. if (map_urb_for_dma(hcd, urb, GFP_KERNEL)) {
  1908. usb_put_urb(urb);
  1909. usb_free_urb(urb);
  1910. return NULL;
  1911. }
  1912. urb->context = done;
  1913. return urb;
  1914. }
  1915. int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
  1916. {
  1917. int retval = -ENOMEM;
  1918. struct usb_ctrlrequest *dr;
  1919. struct urb *urb;
  1920. struct usb_device *udev;
  1921. struct usb_device_descriptor *buf;
  1922. DECLARE_COMPLETION_ONSTACK(done);
  1923. /* Obtain udev of the rhub's child port */
  1924. udev = usb_hub_find_child(hcd->self.root_hub, port);
  1925. if (!udev) {
  1926. dev_err(hcd->self.controller, "No device attached to the RootHub\n");
  1927. return -ENODEV;
  1928. }
  1929. buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
  1930. if (!buf)
  1931. return -ENOMEM;
  1932. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  1933. if (!dr) {
  1934. kfree(buf);
  1935. return -ENOMEM;
  1936. }
  1937. /* Fill Setup packet for GetDescriptor */
  1938. dr->bRequestType = USB_DIR_IN;
  1939. dr->bRequest = USB_REQ_GET_DESCRIPTOR;
  1940. dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8);
  1941. dr->wIndex = 0;
  1942. dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
  1943. urb = request_single_step_set_feature_urb(udev, dr, buf, &done);
  1944. if (!urb)
  1945. goto cleanup;
  1946. /* Submit just the SETUP stage */
  1947. retval = hcd->driver->submit_single_step_set_feature(hcd, urb, 1);
  1948. if (retval)
  1949. goto out1;
  1950. if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
  1951. usb_kill_urb(urb);
  1952. retval = -ETIMEDOUT;
  1953. dev_err(hcd->self.controller,
  1954. "%s SETUP stage timed out on ep0\n", __func__);
  1955. goto out1;
  1956. }
  1957. msleep(15 * 1000);
  1958. /* Complete remaining DATA and STATUS stages using the same URB */
  1959. urb->status = -EINPROGRESS;
  1960. urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
  1961. usb_get_urb(urb);
  1962. atomic_inc(&urb->use_count);
  1963. atomic_inc(&urb->dev->urbnum);
  1964. if (map_urb_for_dma(hcd, urb, GFP_KERNEL)) {
  1965. usb_put_urb(urb);
  1966. goto out1;
  1967. }
  1968. retval = hcd->driver->submit_single_step_set_feature(hcd, urb, 0);
  1969. if (!retval && !wait_for_completion_timeout(&done,
  1970. msecs_to_jiffies(2000))) {
  1971. usb_kill_urb(urb);
  1972. retval = -ETIMEDOUT;
  1973. dev_err(hcd->self.controller,
  1974. "%s IN stage timed out on ep0\n", __func__);
  1975. }
  1976. out1:
  1977. usb_free_urb(urb);
  1978. cleanup:
  1979. kfree(dr);
  1980. kfree(buf);
  1981. return retval;
  1982. }
  1983. EXPORT_SYMBOL_GPL(ehset_single_step_set_feature);
  1984. #endif /* CONFIG_USB_HCD_TEST_MODE */
  1985. /*-------------------------------------------------------------------------*/
  1986. #ifdef CONFIG_PM
  1987. int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
  1988. {
  1989. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  1990. int status;
  1991. int old_state = hcd->state;
  1992. dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
  1993. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  1994. rhdev->do_remote_wakeup);
  1995. if (HCD_DEAD(hcd)) {
  1996. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
  1997. return 0;
  1998. }
  1999. if (!hcd->driver->bus_suspend) {
  2000. status = -ENOENT;
  2001. } else {
  2002. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2003. hcd->state = HC_STATE_QUIESCING;
  2004. status = hcd->driver->bus_suspend(hcd);
  2005. }
  2006. if (status == 0) {
  2007. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  2008. hcd->state = HC_STATE_SUSPENDED;
  2009. if (!PMSG_IS_AUTO(msg))
  2010. usb_phy_roothub_suspend(hcd->self.sysdev,
  2011. hcd->phy_roothub);
  2012. /* Did we race with a root-hub wakeup event? */
  2013. if (rhdev->do_remote_wakeup) {
  2014. char buffer[6];
  2015. status = hcd->driver->hub_status_data(hcd, buffer);
  2016. if (status != 0) {
  2017. dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
  2018. hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
  2019. status = -EBUSY;
  2020. }
  2021. }
  2022. } else {
  2023. spin_lock_irq(&hcd_root_hub_lock);
  2024. if (!HCD_DEAD(hcd)) {
  2025. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2026. hcd->state = old_state;
  2027. }
  2028. spin_unlock_irq(&hcd_root_hub_lock);
  2029. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  2030. "suspend", status);
  2031. }
  2032. return status;
  2033. }
  2034. int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
  2035. {
  2036. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  2037. int status;
  2038. int old_state = hcd->state;
  2039. dev_dbg(&rhdev->dev, "usb %sresume\n",
  2040. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  2041. if (HCD_DEAD(hcd)) {
  2042. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
  2043. return 0;
  2044. }
  2045. if (!PMSG_IS_AUTO(msg)) {
  2046. status = usb_phy_roothub_resume(hcd->self.sysdev,
  2047. hcd->phy_roothub);
  2048. if (status)
  2049. return status;
  2050. }
  2051. if (!hcd->driver->bus_resume)
  2052. return -ENOENT;
  2053. if (HCD_RH_RUNNING(hcd))
  2054. return 0;
  2055. hcd->state = HC_STATE_RESUMING;
  2056. status = hcd->driver->bus_resume(hcd);
  2057. clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  2058. if (status == 0)
  2059. status = usb_phy_roothub_calibrate(hcd->phy_roothub);
  2060. if (status == 0) {
  2061. struct usb_device *udev;
  2062. int port1;
  2063. spin_lock_irq(&hcd_root_hub_lock);
  2064. if (!HCD_DEAD(hcd)) {
  2065. usb_set_device_state(rhdev, rhdev->actconfig
  2066. ? USB_STATE_CONFIGURED
  2067. : USB_STATE_ADDRESS);
  2068. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2069. hcd->state = HC_STATE_RUNNING;
  2070. }
  2071. spin_unlock_irq(&hcd_root_hub_lock);
  2072. /*
  2073. * Check whether any of the enabled ports on the root hub are
  2074. * unsuspended. If they are then a TRSMRCY delay is needed
  2075. * (this is what the USB-2 spec calls a "global resume").
  2076. * Otherwise we can skip the delay.
  2077. */
  2078. usb_hub_for_each_child(rhdev, port1, udev) {
  2079. if (udev->state != USB_STATE_NOTATTACHED &&
  2080. !udev->port_is_suspended) {
  2081. usleep_range(10000, 11000); /* TRSMRCY */
  2082. break;
  2083. }
  2084. }
  2085. } else {
  2086. hcd->state = old_state;
  2087. usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
  2088. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  2089. "resume", status);
  2090. if (status != -ESHUTDOWN)
  2091. usb_hc_died(hcd);
  2092. }
  2093. return status;
  2094. }
  2095. /* Workqueue routine for root-hub remote wakeup */
  2096. static void hcd_resume_work(struct work_struct *work)
  2097. {
  2098. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  2099. struct usb_device *udev = hcd->self.root_hub;
  2100. usb_remote_wakeup(udev);
  2101. }
  2102. /**
  2103. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  2104. * @hcd: host controller for this root hub
  2105. *
  2106. * The USB host controller calls this function when its root hub is
  2107. * suspended (with the remote wakeup feature enabled) and a remote
  2108. * wakeup request is received. The routine submits a workqueue request
  2109. * to resume the root hub (that is, manage its downstream ports again).
  2110. */
  2111. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  2112. {
  2113. unsigned long flags;
  2114. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2115. if (hcd->rh_registered) {
  2116. pm_wakeup_event(&hcd->self.root_hub->dev, 0);
  2117. set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  2118. queue_work(pm_wq, &hcd->wakeup_work);
  2119. }
  2120. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2121. }
  2122. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  2123. #endif /* CONFIG_PM */
  2124. /*-------------------------------------------------------------------------*/
  2125. #ifdef CONFIG_USB_OTG
  2126. /**
  2127. * usb_bus_start_enum - start immediate enumeration (for OTG)
  2128. * @bus: the bus (must use hcd framework)
  2129. * @port_num: 1-based number of port; usually bus->otg_port
  2130. * Context: atomic
  2131. *
  2132. * Starts enumeration, with an immediate reset followed later by
  2133. * hub_wq identifying and possibly configuring the device.
  2134. * This is needed by OTG controller drivers, where it helps meet
  2135. * HNP protocol timing requirements for starting a port reset.
  2136. *
  2137. * Return: 0 if successful.
  2138. */
  2139. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  2140. {
  2141. struct usb_hcd *hcd;
  2142. int status = -EOPNOTSUPP;
  2143. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  2144. * boards with root hubs hooked up to internal devices (instead of
  2145. * just the OTG port) may need more attention to resetting...
  2146. */
  2147. hcd = bus_to_hcd(bus);
  2148. if (port_num && hcd->driver->start_port_reset)
  2149. status = hcd->driver->start_port_reset(hcd, port_num);
  2150. /* allocate hub_wq shortly after (first) root port reset finishes;
  2151. * it may issue others, until at least 50 msecs have passed.
  2152. */
  2153. if (status == 0)
  2154. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  2155. return status;
  2156. }
  2157. EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  2158. #endif
  2159. /*-------------------------------------------------------------------------*/
  2160. /**
  2161. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  2162. * @irq: the IRQ being raised
  2163. * @__hcd: pointer to the HCD whose IRQ is being signaled
  2164. *
  2165. * If the controller isn't HALTed, calls the driver's irq handler.
  2166. * Checks whether the controller is now dead.
  2167. *
  2168. * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise.
  2169. */
  2170. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  2171. {
  2172. struct usb_hcd *hcd = __hcd;
  2173. irqreturn_t rc;
  2174. if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd)))
  2175. rc = IRQ_NONE;
  2176. else if (hcd->driver->irq(hcd) == IRQ_NONE)
  2177. rc = IRQ_NONE;
  2178. else
  2179. rc = IRQ_HANDLED;
  2180. return rc;
  2181. }
  2182. EXPORT_SYMBOL_GPL(usb_hcd_irq);
  2183. /*-------------------------------------------------------------------------*/
  2184. /* Workqueue routine for when the root-hub has died. */
  2185. static void hcd_died_work(struct work_struct *work)
  2186. {
  2187. struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work);
  2188. static char *env[] = {
  2189. "ERROR=DEAD",
  2190. NULL
  2191. };
  2192. /* Notify user space that the host controller has died */
  2193. kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_OFFLINE, env);
  2194. }
  2195. /**
  2196. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  2197. * @hcd: pointer to the HCD representing the controller
  2198. *
  2199. * This is called by bus glue to report a USB host controller that died
  2200. * while operations may still have been pending. It's called automatically
  2201. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  2202. *
  2203. * Only call this function with the primary HCD.
  2204. */
  2205. void usb_hc_died (struct usb_hcd *hcd)
  2206. {
  2207. unsigned long flags;
  2208. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  2209. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2210. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2211. set_bit(HCD_FLAG_DEAD, &hcd->flags);
  2212. if (hcd->rh_registered) {
  2213. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2214. /* make hub_wq clean up old urbs and devices */
  2215. usb_set_device_state (hcd->self.root_hub,
  2216. USB_STATE_NOTATTACHED);
  2217. usb_kick_hub_wq(hcd->self.root_hub);
  2218. }
  2219. if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
  2220. hcd = hcd->shared_hcd;
  2221. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2222. set_bit(HCD_FLAG_DEAD, &hcd->flags);
  2223. if (hcd->rh_registered) {
  2224. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2225. /* make hub_wq clean up old urbs and devices */
  2226. usb_set_device_state(hcd->self.root_hub,
  2227. USB_STATE_NOTATTACHED);
  2228. usb_kick_hub_wq(hcd->self.root_hub);
  2229. }
  2230. }
  2231. /* Handle the case where this function gets called with a shared HCD */
  2232. if (usb_hcd_is_primary_hcd(hcd))
  2233. schedule_work(&hcd->died_work);
  2234. else
  2235. schedule_work(&hcd->primary_hcd->died_work);
  2236. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2237. /* Make sure that the other roothub is also deallocated. */
  2238. }
  2239. EXPORT_SYMBOL_GPL (usb_hc_died);
  2240. /*-------------------------------------------------------------------------*/
  2241. static void init_giveback_urb_bh(struct giveback_urb_bh *bh)
  2242. {
  2243. spin_lock_init(&bh->lock);
  2244. INIT_LIST_HEAD(&bh->head);
  2245. INIT_WORK(&bh->bh, usb_giveback_urb_bh);
  2246. }
  2247. struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver,
  2248. struct device *sysdev, struct device *dev, const char *bus_name,
  2249. struct usb_hcd *primary_hcd)
  2250. {
  2251. struct usb_hcd *hcd;
  2252. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  2253. if (!hcd)
  2254. return NULL;
  2255. if (primary_hcd == NULL) {
  2256. hcd->address0_mutex = kmalloc(sizeof(*hcd->address0_mutex),
  2257. GFP_KERNEL);
  2258. if (!hcd->address0_mutex) {
  2259. kfree(hcd);
  2260. dev_dbg(dev, "hcd address0 mutex alloc failed\n");
  2261. return NULL;
  2262. }
  2263. mutex_init(hcd->address0_mutex);
  2264. hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
  2265. GFP_KERNEL);
  2266. if (!hcd->bandwidth_mutex) {
  2267. kfree(hcd->address0_mutex);
  2268. kfree(hcd);
  2269. dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
  2270. return NULL;
  2271. }
  2272. mutex_init(hcd->bandwidth_mutex);
  2273. dev_set_drvdata(dev, hcd);
  2274. } else {
  2275. mutex_lock(&usb_port_peer_mutex);
  2276. hcd->address0_mutex = primary_hcd->address0_mutex;
  2277. hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
  2278. hcd->primary_hcd = primary_hcd;
  2279. primary_hcd->primary_hcd = primary_hcd;
  2280. hcd->shared_hcd = primary_hcd;
  2281. primary_hcd->shared_hcd = hcd;
  2282. mutex_unlock(&usb_port_peer_mutex);
  2283. }
  2284. kref_init(&hcd->kref);
  2285. usb_bus_init(&hcd->self);
  2286. hcd->self.controller = dev;
  2287. hcd->self.sysdev = sysdev;
  2288. hcd->self.bus_name = bus_name;
  2289. timer_setup(&hcd->rh_timer, rh_timer_func, 0);
  2290. #ifdef CONFIG_PM
  2291. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  2292. #endif
  2293. INIT_WORK(&hcd->died_work, hcd_died_work);
  2294. hcd->driver = driver;
  2295. hcd->speed = driver->flags & HCD_MASK;
  2296. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  2297. "USB Host Controller";
  2298. return hcd;
  2299. }
  2300. EXPORT_SYMBOL_GPL(__usb_create_hcd);
  2301. /**
  2302. * usb_create_shared_hcd - create and initialize an HCD structure
  2303. * @driver: HC driver that will use this hcd
  2304. * @dev: device for this HC, stored in hcd->self.controller
  2305. * @bus_name: value to store in hcd->self.bus_name
  2306. * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
  2307. * PCI device. Only allocate certain resources for the primary HCD
  2308. *
  2309. * Context: task context, might sleep.
  2310. *
  2311. * Allocate a struct usb_hcd, with extra space at the end for the
  2312. * HC driver's private data. Initialize the generic members of the
  2313. * hcd structure.
  2314. *
  2315. * Return: On success, a pointer to the created and initialized HCD structure.
  2316. * On failure (e.g. if memory is unavailable), %NULL.
  2317. */
  2318. struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
  2319. struct device *dev, const char *bus_name,
  2320. struct usb_hcd *primary_hcd)
  2321. {
  2322. return __usb_create_hcd(driver, dev, dev, bus_name, primary_hcd);
  2323. }
  2324. EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
  2325. /**
  2326. * usb_create_hcd - create and initialize an HCD structure
  2327. * @driver: HC driver that will use this hcd
  2328. * @dev: device for this HC, stored in hcd->self.controller
  2329. * @bus_name: value to store in hcd->self.bus_name
  2330. *
  2331. * Context: task context, might sleep.
  2332. *
  2333. * Allocate a struct usb_hcd, with extra space at the end for the
  2334. * HC driver's private data. Initialize the generic members of the
  2335. * hcd structure.
  2336. *
  2337. * Return: On success, a pointer to the created and initialized HCD
  2338. * structure. On failure (e.g. if memory is unavailable), %NULL.
  2339. */
  2340. struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
  2341. struct device *dev, const char *bus_name)
  2342. {
  2343. return __usb_create_hcd(driver, dev, dev, bus_name, NULL);
  2344. }
  2345. EXPORT_SYMBOL_GPL(usb_create_hcd);
  2346. /*
  2347. * Roothubs that share one PCI device must also share the bandwidth mutex.
  2348. * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is
  2349. * deallocated.
  2350. *
  2351. * Make sure to deallocate the bandwidth_mutex only when the last HCD is
  2352. * freed. When hcd_release() is called for either hcd in a peer set,
  2353. * invalidate the peer's ->shared_hcd and ->primary_hcd pointers.
  2354. */
  2355. static void hcd_release(struct kref *kref)
  2356. {
  2357. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  2358. mutex_lock(&usb_port_peer_mutex);
  2359. if (hcd->shared_hcd) {
  2360. struct usb_hcd *peer = hcd->shared_hcd;
  2361. peer->shared_hcd = NULL;
  2362. peer->primary_hcd = NULL;
  2363. } else {
  2364. kfree(hcd->address0_mutex);
  2365. kfree(hcd->bandwidth_mutex);
  2366. }
  2367. mutex_unlock(&usb_port_peer_mutex);
  2368. kfree(hcd);
  2369. }
  2370. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  2371. {
  2372. if (hcd)
  2373. kref_get (&hcd->kref);
  2374. return hcd;
  2375. }
  2376. EXPORT_SYMBOL_GPL(usb_get_hcd);
  2377. void usb_put_hcd (struct usb_hcd *hcd)
  2378. {
  2379. if (hcd)
  2380. kref_put (&hcd->kref, hcd_release);
  2381. }
  2382. EXPORT_SYMBOL_GPL(usb_put_hcd);
  2383. int usb_hcd_is_primary_hcd(struct usb_hcd *hcd)
  2384. {
  2385. if (!hcd->primary_hcd)
  2386. return 1;
  2387. return hcd == hcd->primary_hcd;
  2388. }
  2389. EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd);
  2390. int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1)
  2391. {
  2392. if (!hcd->driver->find_raw_port_number)
  2393. return port1;
  2394. return hcd->driver->find_raw_port_number(hcd, port1);
  2395. }
  2396. static int usb_hcd_request_irqs(struct usb_hcd *hcd,
  2397. unsigned int irqnum, unsigned long irqflags)
  2398. {
  2399. int retval;
  2400. if (hcd->driver->irq) {
  2401. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  2402. hcd->driver->description, hcd->self.busnum);
  2403. retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  2404. hcd->irq_descr, hcd);
  2405. if (retval != 0) {
  2406. dev_err(hcd->self.controller,
  2407. "request interrupt %d failed\n",
  2408. irqnum);
  2409. return retval;
  2410. }
  2411. hcd->irq = irqnum;
  2412. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  2413. (hcd->driver->flags & HCD_MEMORY) ?
  2414. "io mem" : "io port",
  2415. (unsigned long long)hcd->rsrc_start);
  2416. } else {
  2417. hcd->irq = 0;
  2418. if (hcd->rsrc_start)
  2419. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  2420. (hcd->driver->flags & HCD_MEMORY) ?
  2421. "io mem" : "io port",
  2422. (unsigned long long)hcd->rsrc_start);
  2423. }
  2424. return 0;
  2425. }
  2426. /*
  2427. * Before we free this root hub, flush in-flight peering attempts
  2428. * and disable peer lookups
  2429. */
  2430. static void usb_put_invalidate_rhdev(struct usb_hcd *hcd)
  2431. {
  2432. struct usb_device *rhdev;
  2433. mutex_lock(&usb_port_peer_mutex);
  2434. rhdev = hcd->self.root_hub;
  2435. hcd->self.root_hub = NULL;
  2436. mutex_unlock(&usb_port_peer_mutex);
  2437. usb_put_dev(rhdev);
  2438. }
  2439. /**
  2440. * usb_stop_hcd - Halt the HCD
  2441. * @hcd: the usb_hcd that has to be halted
  2442. *
  2443. * Stop the root-hub polling timer and invoke the HCD's ->stop callback.
  2444. */
  2445. static void usb_stop_hcd(struct usb_hcd *hcd)
  2446. {
  2447. hcd->rh_pollable = 0;
  2448. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2449. del_timer_sync(&hcd->rh_timer);
  2450. hcd->driver->stop(hcd);
  2451. hcd->state = HC_STATE_HALT;
  2452. /* In case the HCD restarted the timer, stop it again. */
  2453. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2454. del_timer_sync(&hcd->rh_timer);
  2455. }
  2456. /**
  2457. * usb_add_hcd - finish generic HCD structure initialization and register
  2458. * @hcd: the usb_hcd structure to initialize
  2459. * @irqnum: Interrupt line to allocate
  2460. * @irqflags: Interrupt type flags
  2461. *
  2462. * Finish the remaining parts of generic HCD initialization: allocate the
  2463. * buffers of consistent memory, register the bus, request the IRQ line,
  2464. * and call the driver's reset() and start() routines.
  2465. */
  2466. int usb_add_hcd(struct usb_hcd *hcd,
  2467. unsigned int irqnum, unsigned long irqflags)
  2468. {
  2469. int retval;
  2470. struct usb_device *rhdev;
  2471. struct usb_hcd *shared_hcd;
  2472. int skip_phy_initialization;
  2473. if (usb_hcd_is_primary_hcd(hcd))
  2474. skip_phy_initialization = hcd->skip_phy_initialization;
  2475. else
  2476. skip_phy_initialization = hcd->primary_hcd->skip_phy_initialization;
  2477. if (!skip_phy_initialization) {
  2478. if (usb_hcd_is_primary_hcd(hcd)) {
  2479. hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
  2480. if (IS_ERR(hcd->phy_roothub))
  2481. return PTR_ERR(hcd->phy_roothub);
  2482. } else {
  2483. hcd->phy_roothub = usb_phy_roothub_alloc_usb3_phy(hcd->self.sysdev);
  2484. if (IS_ERR(hcd->phy_roothub))
  2485. return PTR_ERR(hcd->phy_roothub);
  2486. }
  2487. retval = usb_phy_roothub_init(hcd->phy_roothub);
  2488. if (retval)
  2489. return retval;
  2490. retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
  2491. PHY_MODE_USB_HOST_SS);
  2492. if (retval)
  2493. retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
  2494. PHY_MODE_USB_HOST);
  2495. if (retval)
  2496. goto err_usb_phy_roothub_power_on;
  2497. retval = usb_phy_roothub_power_on(hcd->phy_roothub);
  2498. if (retval)
  2499. goto err_usb_phy_roothub_power_on;
  2500. }
  2501. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  2502. switch (authorized_default) {
  2503. case USB_AUTHORIZE_NONE:
  2504. hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE;
  2505. break;
  2506. case USB_AUTHORIZE_INTERNAL:
  2507. hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL;
  2508. break;
  2509. case USB_AUTHORIZE_ALL:
  2510. case USB_AUTHORIZE_WIRED:
  2511. default:
  2512. hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL;
  2513. break;
  2514. }
  2515. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2516. /* per default all interfaces are authorized */
  2517. set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  2518. /* HC is in reset state, but accessible. Now do the one-time init,
  2519. * bottom up so that hcds can customize the root hubs before hub_wq
  2520. * starts talking to them. (Note, bus id is assigned early too.)
  2521. */
  2522. retval = hcd_buffer_create(hcd);
  2523. if (retval != 0) {
  2524. dev_dbg(hcd->self.sysdev, "pool alloc failed\n");
  2525. goto err_create_buf;
  2526. }
  2527. retval = usb_register_bus(&hcd->self);
  2528. if (retval < 0)
  2529. goto err_register_bus;
  2530. rhdev = usb_alloc_dev(NULL, &hcd->self, 0);
  2531. if (rhdev == NULL) {
  2532. dev_err(hcd->self.sysdev, "unable to allocate root hub\n");
  2533. retval = -ENOMEM;
  2534. goto err_allocate_root_hub;
  2535. }
  2536. mutex_lock(&usb_port_peer_mutex);
  2537. hcd->self.root_hub = rhdev;
  2538. mutex_unlock(&usb_port_peer_mutex);
  2539. rhdev->rx_lanes = 1;
  2540. rhdev->tx_lanes = 1;
  2541. rhdev->ssp_rate = USB_SSP_GEN_UNKNOWN;
  2542. switch (hcd->speed) {
  2543. case HCD_USB11:
  2544. rhdev->speed = USB_SPEED_FULL;
  2545. break;
  2546. case HCD_USB2:
  2547. rhdev->speed = USB_SPEED_HIGH;
  2548. break;
  2549. case HCD_USB3:
  2550. rhdev->speed = USB_SPEED_SUPER;
  2551. break;
  2552. case HCD_USB32:
  2553. rhdev->rx_lanes = 2;
  2554. rhdev->tx_lanes = 2;
  2555. rhdev->ssp_rate = USB_SSP_GEN_2x2;
  2556. rhdev->speed = USB_SPEED_SUPER_PLUS;
  2557. break;
  2558. case HCD_USB31:
  2559. rhdev->ssp_rate = USB_SSP_GEN_2x1;
  2560. rhdev->speed = USB_SPEED_SUPER_PLUS;
  2561. break;
  2562. default:
  2563. retval = -EINVAL;
  2564. goto err_set_rh_speed;
  2565. }
  2566. /* wakeup flag init defaults to "everything works" for root hubs,
  2567. * but drivers can override it in reset() if needed, along with
  2568. * recording the overall controller's system wakeup capability.
  2569. */
  2570. device_set_wakeup_capable(&rhdev->dev, 1);
  2571. /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
  2572. * registered. But since the controller can die at any time,
  2573. * let's initialize the flag before touching the hardware.
  2574. */
  2575. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2576. /* "reset" is misnamed; its role is now one-time init. the controller
  2577. * should already have been reset (and boot firmware kicked off etc).
  2578. */
  2579. if (hcd->driver->reset) {
  2580. retval = hcd->driver->reset(hcd);
  2581. if (retval < 0) {
  2582. dev_err(hcd->self.controller, "can't setup: %d\n",
  2583. retval);
  2584. goto err_hcd_driver_setup;
  2585. }
  2586. }
  2587. hcd->rh_pollable = 1;
  2588. retval = usb_phy_roothub_calibrate(hcd->phy_roothub);
  2589. if (retval)
  2590. goto err_hcd_driver_setup;
  2591. /* NOTE: root hub and controller capabilities may not be the same */
  2592. if (device_can_wakeup(hcd->self.controller)
  2593. && device_can_wakeup(&hcd->self.root_hub->dev))
  2594. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  2595. /* initialize BHs */
  2596. init_giveback_urb_bh(&hcd->high_prio_bh);
  2597. hcd->high_prio_bh.high_prio = true;
  2598. init_giveback_urb_bh(&hcd->low_prio_bh);
  2599. /* enable irqs just before we start the controller,
  2600. * if the BIOS provides legacy PCI irqs.
  2601. */
  2602. if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
  2603. retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
  2604. if (retval)
  2605. goto err_request_irq;
  2606. }
  2607. hcd->state = HC_STATE_RUNNING;
  2608. retval = hcd->driver->start(hcd);
  2609. if (retval < 0) {
  2610. dev_err(hcd->self.controller, "startup error %d\n", retval);
  2611. goto err_hcd_driver_start;
  2612. }
  2613. /* starting here, usbcore will pay attention to the shared HCD roothub */
  2614. shared_hcd = hcd->shared_hcd;
  2615. if (!usb_hcd_is_primary_hcd(hcd) && shared_hcd && HCD_DEFER_RH_REGISTER(shared_hcd)) {
  2616. retval = register_root_hub(shared_hcd);
  2617. if (retval != 0)
  2618. goto err_register_root_hub;
  2619. if (shared_hcd->uses_new_polling && HCD_POLL_RH(shared_hcd))
  2620. usb_hcd_poll_rh_status(shared_hcd);
  2621. }
  2622. /* starting here, usbcore will pay attention to this root hub */
  2623. if (!HCD_DEFER_RH_REGISTER(hcd)) {
  2624. retval = register_root_hub(hcd);
  2625. if (retval != 0)
  2626. goto err_register_root_hub;
  2627. if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
  2628. usb_hcd_poll_rh_status(hcd);
  2629. }
  2630. return retval;
  2631. err_register_root_hub:
  2632. usb_stop_hcd(hcd);
  2633. err_hcd_driver_start:
  2634. if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
  2635. free_irq(irqnum, hcd);
  2636. err_request_irq:
  2637. err_hcd_driver_setup:
  2638. err_set_rh_speed:
  2639. usb_put_invalidate_rhdev(hcd);
  2640. err_allocate_root_hub:
  2641. usb_deregister_bus(&hcd->self);
  2642. err_register_bus:
  2643. hcd_buffer_destroy(hcd);
  2644. err_create_buf:
  2645. usb_phy_roothub_power_off(hcd->phy_roothub);
  2646. err_usb_phy_roothub_power_on:
  2647. usb_phy_roothub_exit(hcd->phy_roothub);
  2648. return retval;
  2649. }
  2650. EXPORT_SYMBOL_GPL(usb_add_hcd);
  2651. /**
  2652. * usb_remove_hcd - shutdown processing for generic HCDs
  2653. * @hcd: the usb_hcd structure to remove
  2654. *
  2655. * Context: task context, might sleep.
  2656. *
  2657. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  2658. * invoking the HCD's stop() method.
  2659. */
  2660. void usb_remove_hcd(struct usb_hcd *hcd)
  2661. {
  2662. struct usb_device *rhdev;
  2663. bool rh_registered;
  2664. if (!hcd) {
  2665. pr_debug("%s: hcd is NULL\n", __func__);
  2666. return;
  2667. }
  2668. rhdev = hcd->self.root_hub;
  2669. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  2670. usb_get_dev(rhdev);
  2671. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2672. if (HC_IS_RUNNING (hcd->state))
  2673. hcd->state = HC_STATE_QUIESCING;
  2674. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  2675. spin_lock_irq (&hcd_root_hub_lock);
  2676. rh_registered = hcd->rh_registered;
  2677. hcd->rh_registered = 0;
  2678. spin_unlock_irq (&hcd_root_hub_lock);
  2679. #ifdef CONFIG_PM
  2680. cancel_work_sync(&hcd->wakeup_work);
  2681. #endif
  2682. cancel_work_sync(&hcd->died_work);
  2683. mutex_lock(&usb_bus_idr_lock);
  2684. if (rh_registered)
  2685. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2686. mutex_unlock(&usb_bus_idr_lock);
  2687. /*
  2688. * flush_work() isn't needed here because:
  2689. * - driver's disconnect() called from usb_disconnect() should
  2690. * make sure its URBs are completed during the disconnect()
  2691. * callback
  2692. *
  2693. * - it is too late to run complete() here since driver may have
  2694. * been removed already now
  2695. */
  2696. /* Prevent any more root-hub status calls from the timer.
  2697. * The HCD might still restart the timer (if a port status change
  2698. * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
  2699. * the hub_status_data() callback.
  2700. */
  2701. usb_stop_hcd(hcd);
  2702. if (usb_hcd_is_primary_hcd(hcd)) {
  2703. if (hcd->irq > 0)
  2704. free_irq(hcd->irq, hcd);
  2705. }
  2706. usb_deregister_bus(&hcd->self);
  2707. hcd_buffer_destroy(hcd);
  2708. usb_phy_roothub_power_off(hcd->phy_roothub);
  2709. usb_phy_roothub_exit(hcd->phy_roothub);
  2710. usb_put_invalidate_rhdev(hcd);
  2711. hcd->flags = 0;
  2712. }
  2713. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  2714. void
  2715. usb_hcd_platform_shutdown(struct platform_device *dev)
  2716. {
  2717. struct usb_hcd *hcd = platform_get_drvdata(dev);
  2718. /* No need for pm_runtime_put(), we're shutting down */
  2719. pm_runtime_get_sync(&dev->dev);
  2720. if (hcd->driver->shutdown)
  2721. hcd->driver->shutdown(hcd);
  2722. }
  2723. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  2724. int usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr,
  2725. dma_addr_t dma, size_t size)
  2726. {
  2727. int err;
  2728. void *local_mem;
  2729. hcd->localmem_pool = devm_gen_pool_create(hcd->self.sysdev, 4,
  2730. dev_to_node(hcd->self.sysdev),
  2731. dev_name(hcd->self.sysdev));
  2732. if (IS_ERR(hcd->localmem_pool))
  2733. return PTR_ERR(hcd->localmem_pool);
  2734. /*
  2735. * if a physical SRAM address was passed, map it, otherwise
  2736. * allocate system memory as a buffer.
  2737. */
  2738. if (phys_addr)
  2739. local_mem = devm_memremap(hcd->self.sysdev, phys_addr,
  2740. size, MEMREMAP_WC);
  2741. else
  2742. local_mem = dmam_alloc_attrs(hcd->self.sysdev, size, &dma,
  2743. GFP_KERNEL,
  2744. DMA_ATTR_WRITE_COMBINE);
  2745. if (IS_ERR_OR_NULL(local_mem)) {
  2746. if (!local_mem)
  2747. return -ENOMEM;
  2748. return PTR_ERR(local_mem);
  2749. }
  2750. /*
  2751. * Here we pass a dma_addr_t but the arg type is a phys_addr_t.
  2752. * It's not backed by system memory and thus there's no kernel mapping
  2753. * for it.
  2754. */
  2755. err = gen_pool_add_virt(hcd->localmem_pool, (unsigned long)local_mem,
  2756. dma, size, dev_to_node(hcd->self.sysdev));
  2757. if (err < 0) {
  2758. dev_err(hcd->self.sysdev, "gen_pool_add_virt failed with %d\n",
  2759. err);
  2760. return err;
  2761. }
  2762. return 0;
  2763. }
  2764. EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem);
  2765. /*-------------------------------------------------------------------------*/
  2766. #if IS_ENABLED(CONFIG_USB_MON)
  2767. const struct usb_mon_operations *mon_ops;
  2768. /*
  2769. * The registration is unlocked.
  2770. * We do it this way because we do not want to lock in hot paths.
  2771. *
  2772. * Notice that the code is minimally error-proof. Because usbmon needs
  2773. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  2774. */
  2775. int usb_mon_register(const struct usb_mon_operations *ops)
  2776. {
  2777. if (mon_ops)
  2778. return -EBUSY;
  2779. mon_ops = ops;
  2780. mb();
  2781. return 0;
  2782. }
  2783. EXPORT_SYMBOL_GPL (usb_mon_register);
  2784. void usb_mon_deregister (void)
  2785. {
  2786. if (mon_ops == NULL) {
  2787. printk(KERN_ERR "USB: monitor was not registered\n");
  2788. return;
  2789. }
  2790. mon_ops = NULL;
  2791. mb();
  2792. }
  2793. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  2794. #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */