hcd.c 87 KB

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