hcd.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049
  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/otg.h>
  35. #include "usb.h"
  36. #include "phy.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_USB32:
  493. case HCD_USB31:
  494. bufp = usb31_rh_dev_descriptor;
  495. break;
  496. case HCD_USB3:
  497. bufp = usb3_rh_dev_descriptor;
  498. break;
  499. case HCD_USB25:
  500. bufp = usb25_rh_dev_descriptor;
  501. break;
  502. case HCD_USB2:
  503. bufp = usb2_rh_dev_descriptor;
  504. break;
  505. case HCD_USB11:
  506. bufp = usb11_rh_dev_descriptor;
  507. break;
  508. default:
  509. goto error;
  510. }
  511. len = 18;
  512. if (hcd->has_tt)
  513. patch_protocol = 1;
  514. break;
  515. case USB_DT_CONFIG << 8:
  516. switch (hcd->speed) {
  517. case HCD_USB32:
  518. case HCD_USB31:
  519. case HCD_USB3:
  520. bufp = ss_rh_config_descriptor;
  521. len = sizeof ss_rh_config_descriptor;
  522. break;
  523. case HCD_USB25:
  524. case HCD_USB2:
  525. bufp = hs_rh_config_descriptor;
  526. len = sizeof hs_rh_config_descriptor;
  527. break;
  528. case HCD_USB11:
  529. bufp = fs_rh_config_descriptor;
  530. len = sizeof fs_rh_config_descriptor;
  531. break;
  532. default:
  533. goto error;
  534. }
  535. if (device_can_wakeup(&hcd->self.root_hub->dev))
  536. patch_wakeup = 1;
  537. break;
  538. case USB_DT_STRING << 8:
  539. if ((wValue & 0xff) < 4)
  540. urb->actual_length = rh_string(wValue & 0xff,
  541. hcd, ubuf, wLength);
  542. else /* unsupported IDs --> "protocol stall" */
  543. goto error;
  544. break;
  545. case USB_DT_BOS << 8:
  546. goto nongeneric;
  547. default:
  548. goto error;
  549. }
  550. break;
  551. case DeviceRequest | USB_REQ_GET_INTERFACE:
  552. tbuf[0] = 0;
  553. len = 1;
  554. /* FALLTHROUGH */
  555. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  556. break;
  557. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  558. /* wValue == urb->dev->devaddr */
  559. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  560. wValue);
  561. break;
  562. /* INTERFACE REQUESTS (no defined feature/status flags) */
  563. /* ENDPOINT REQUESTS */
  564. case EndpointRequest | USB_REQ_GET_STATUS:
  565. /* ENDPOINT_HALT flag */
  566. tbuf[0] = 0;
  567. tbuf[1] = 0;
  568. len = 2;
  569. /* FALLTHROUGH */
  570. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  571. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  572. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  573. break;
  574. /* CLASS REQUESTS (and errors) */
  575. default:
  576. nongeneric:
  577. /* non-generic request */
  578. switch (typeReq) {
  579. case GetHubStatus:
  580. len = 4;
  581. break;
  582. case GetPortStatus:
  583. if (wValue == HUB_PORT_STATUS)
  584. len = 4;
  585. else
  586. /* other port status types return 8 bytes */
  587. len = 8;
  588. break;
  589. case GetHubDescriptor:
  590. len = sizeof (struct usb_hub_descriptor);
  591. break;
  592. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  593. /* len is returned by hub_control */
  594. break;
  595. }
  596. status = hcd->driver->hub_control (hcd,
  597. typeReq, wValue, wIndex,
  598. tbuf, wLength);
  599. if (typeReq == GetHubDescriptor)
  600. usb_hub_adjust_deviceremovable(hcd->self.root_hub,
  601. (struct usb_hub_descriptor *)tbuf);
  602. break;
  603. error:
  604. /* "protocol stall" on error */
  605. status = -EPIPE;
  606. }
  607. if (status < 0) {
  608. len = 0;
  609. if (status != -EPIPE) {
  610. dev_dbg (hcd->self.controller,
  611. "CTRL: TypeReq=0x%x val=0x%x "
  612. "idx=0x%x len=%d ==> %d\n",
  613. typeReq, wValue, wIndex,
  614. wLength, status);
  615. }
  616. } else if (status > 0) {
  617. /* hub_control may return the length of data copied. */
  618. len = status;
  619. status = 0;
  620. }
  621. if (len) {
  622. if (urb->transfer_buffer_length < len)
  623. len = urb->transfer_buffer_length;
  624. urb->actual_length = len;
  625. /* always USB_DIR_IN, toward host */
  626. memcpy (ubuf, bufp, len);
  627. /* report whether RH hardware supports remote wakeup */
  628. if (patch_wakeup &&
  629. len > offsetof (struct usb_config_descriptor,
  630. bmAttributes))
  631. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  632. |= USB_CONFIG_ATT_WAKEUP;
  633. /* report whether RH hardware has an integrated TT */
  634. if (patch_protocol &&
  635. len > offsetof(struct usb_device_descriptor,
  636. bDeviceProtocol))
  637. ((struct usb_device_descriptor *) ubuf)->
  638. bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT;
  639. }
  640. kfree(tbuf);
  641. err_alloc:
  642. /* any errors get returned through the urb completion */
  643. spin_lock_irq(&hcd_root_hub_lock);
  644. usb_hcd_unlink_urb_from_ep(hcd, urb);
  645. usb_hcd_giveback_urb(hcd, urb, status);
  646. spin_unlock_irq(&hcd_root_hub_lock);
  647. return 0;
  648. }
  649. /*-------------------------------------------------------------------------*/
  650. /*
  651. * Root Hub interrupt transfers are polled using a timer if the
  652. * driver requests it; otherwise the driver is responsible for
  653. * calling usb_hcd_poll_rh_status() when an event occurs.
  654. *
  655. * Completions are called in_interrupt(), but they may or may not
  656. * be in_irq().
  657. */
  658. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  659. {
  660. struct urb *urb;
  661. int length;
  662. unsigned long flags;
  663. char buffer[6]; /* Any root hubs with > 31 ports? */
  664. if (unlikely(!hcd->rh_pollable))
  665. return;
  666. if (!hcd->uses_new_polling && !hcd->status_urb)
  667. return;
  668. length = hcd->driver->hub_status_data(hcd, buffer);
  669. if (length > 0) {
  670. /* try to complete the status urb */
  671. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  672. urb = hcd->status_urb;
  673. if (urb) {
  674. clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  675. hcd->status_urb = NULL;
  676. urb->actual_length = length;
  677. memcpy(urb->transfer_buffer, buffer, length);
  678. usb_hcd_unlink_urb_from_ep(hcd, urb);
  679. usb_hcd_giveback_urb(hcd, urb, 0);
  680. } else {
  681. length = 0;
  682. set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  683. }
  684. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  685. }
  686. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  687. * exceed that limit if HZ is 100. The math is more clunky than
  688. * maybe expected, this is to make sure that all timers for USB devices
  689. * fire at the same time to give the CPU a break in between */
  690. if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
  691. (length == 0 && hcd->status_urb != NULL))
  692. mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  693. }
  694. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  695. /* timer callback */
  696. static void rh_timer_func (struct timer_list *t)
  697. {
  698. struct usb_hcd *_hcd = from_timer(_hcd, t, rh_timer);
  699. usb_hcd_poll_rh_status(_hcd);
  700. }
  701. /*-------------------------------------------------------------------------*/
  702. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  703. {
  704. int retval;
  705. unsigned long flags;
  706. unsigned len = 1 + (urb->dev->maxchild / 8);
  707. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  708. if (hcd->status_urb || urb->transfer_buffer_length < len) {
  709. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  710. retval = -EINVAL;
  711. goto done;
  712. }
  713. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  714. if (retval)
  715. goto done;
  716. hcd->status_urb = urb;
  717. urb->hcpriv = hcd; /* indicate it's queued */
  718. if (!hcd->uses_new_polling)
  719. mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  720. /* If a status change has already occurred, report it ASAP */
  721. else if (HCD_POLL_PENDING(hcd))
  722. mod_timer(&hcd->rh_timer, jiffies);
  723. retval = 0;
  724. done:
  725. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  726. return retval;
  727. }
  728. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  729. {
  730. if (usb_endpoint_xfer_int(&urb->ep->desc))
  731. return rh_queue_status (hcd, urb);
  732. if (usb_endpoint_xfer_control(&urb->ep->desc))
  733. return rh_call_control (hcd, urb);
  734. return -EINVAL;
  735. }
  736. /*-------------------------------------------------------------------------*/
  737. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  738. * since these URBs always execute synchronously.
  739. */
  740. static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  741. {
  742. unsigned long flags;
  743. int rc;
  744. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  745. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  746. if (rc)
  747. goto done;
  748. if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
  749. ; /* Do nothing */
  750. } else { /* Status URB */
  751. if (!hcd->uses_new_polling)
  752. del_timer (&hcd->rh_timer);
  753. if (urb == hcd->status_urb) {
  754. hcd->status_urb = NULL;
  755. usb_hcd_unlink_urb_from_ep(hcd, urb);
  756. usb_hcd_giveback_urb(hcd, urb, status);
  757. }
  758. }
  759. done:
  760. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  761. return rc;
  762. }
  763. /*
  764. * Show & store the current value of authorized_default
  765. */
  766. static ssize_t authorized_default_show(struct device *dev,
  767. struct device_attribute *attr, char *buf)
  768. {
  769. struct usb_device *rh_usb_dev = to_usb_device(dev);
  770. struct usb_bus *usb_bus = rh_usb_dev->bus;
  771. struct usb_hcd *hcd;
  772. hcd = bus_to_hcd(usb_bus);
  773. return snprintf(buf, PAGE_SIZE, "%u\n", !!HCD_DEV_AUTHORIZED(hcd));
  774. }
  775. static ssize_t authorized_default_store(struct device *dev,
  776. struct device_attribute *attr,
  777. const char *buf, size_t size)
  778. {
  779. ssize_t result;
  780. unsigned val;
  781. struct usb_device *rh_usb_dev = to_usb_device(dev);
  782. struct usb_bus *usb_bus = rh_usb_dev->bus;
  783. struct usb_hcd *hcd;
  784. hcd = bus_to_hcd(usb_bus);
  785. result = sscanf(buf, "%u\n", &val);
  786. if (result == 1) {
  787. if (val)
  788. set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  789. else
  790. clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  791. result = size;
  792. } else {
  793. result = -EINVAL;
  794. }
  795. return result;
  796. }
  797. static DEVICE_ATTR_RW(authorized_default);
  798. /*
  799. * interface_authorized_default_show - show default authorization status
  800. * for USB interfaces
  801. *
  802. * note: interface_authorized_default is the default value
  803. * for initializing the authorized attribute of interfaces
  804. */
  805. static ssize_t interface_authorized_default_show(struct device *dev,
  806. struct device_attribute *attr, char *buf)
  807. {
  808. struct usb_device *usb_dev = to_usb_device(dev);
  809. struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
  810. return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd));
  811. }
  812. /*
  813. * interface_authorized_default_store - store default authorization status
  814. * for USB interfaces
  815. *
  816. * note: interface_authorized_default is the default value
  817. * for initializing the authorized attribute of interfaces
  818. */
  819. static ssize_t interface_authorized_default_store(struct device *dev,
  820. struct device_attribute *attr, const char *buf, size_t count)
  821. {
  822. struct usb_device *usb_dev = to_usb_device(dev);
  823. struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
  824. int rc = count;
  825. bool val;
  826. if (strtobool(buf, &val) != 0)
  827. return -EINVAL;
  828. if (val)
  829. set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  830. else
  831. clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  832. return rc;
  833. }
  834. static DEVICE_ATTR_RW(interface_authorized_default);
  835. /* Group all the USB bus attributes */
  836. static struct attribute *usb_bus_attrs[] = {
  837. &dev_attr_authorized_default.attr,
  838. &dev_attr_interface_authorized_default.attr,
  839. NULL,
  840. };
  841. static const struct attribute_group usb_bus_attr_group = {
  842. .name = NULL, /* we want them in the same directory */
  843. .attrs = usb_bus_attrs,
  844. };
  845. /*-------------------------------------------------------------------------*/
  846. /**
  847. * usb_bus_init - shared initialization code
  848. * @bus: the bus structure being initialized
  849. *
  850. * This code is used to initialize a usb_bus structure, memory for which is
  851. * separately managed.
  852. */
  853. static void usb_bus_init (struct usb_bus *bus)
  854. {
  855. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  856. bus->devnum_next = 1;
  857. bus->root_hub = NULL;
  858. bus->busnum = -1;
  859. bus->bandwidth_allocated = 0;
  860. bus->bandwidth_int_reqs = 0;
  861. bus->bandwidth_isoc_reqs = 0;
  862. mutex_init(&bus->devnum_next_mutex);
  863. }
  864. /*-------------------------------------------------------------------------*/
  865. /**
  866. * usb_register_bus - registers the USB host controller with the usb core
  867. * @bus: pointer to the bus to register
  868. * Context: !in_interrupt()
  869. *
  870. * Assigns a bus number, and links the controller into usbcore data
  871. * structures so that it can be seen by scanning the bus list.
  872. *
  873. * Return: 0 if successful. A negative error code otherwise.
  874. */
  875. static int usb_register_bus(struct usb_bus *bus)
  876. {
  877. int result = -E2BIG;
  878. int busnum;
  879. mutex_lock(&usb_bus_idr_lock);
  880. busnum = idr_alloc(&usb_bus_idr, bus, 1, USB_MAXBUS, GFP_KERNEL);
  881. if (busnum < 0) {
  882. pr_err("%s: failed to get bus number\n", usbcore_name);
  883. goto error_find_busnum;
  884. }
  885. bus->busnum = busnum;
  886. mutex_unlock(&usb_bus_idr_lock);
  887. usb_notify_add_bus(bus);
  888. dev_info (bus->controller, "new USB bus registered, assigned bus "
  889. "number %d\n", bus->busnum);
  890. return 0;
  891. error_find_busnum:
  892. mutex_unlock(&usb_bus_idr_lock);
  893. return result;
  894. }
  895. /**
  896. * usb_deregister_bus - deregisters the USB host controller
  897. * @bus: pointer to the bus to deregister
  898. * Context: !in_interrupt()
  899. *
  900. * Recycles the bus number, and unlinks the controller from usbcore data
  901. * structures so that it won't be seen by scanning the bus list.
  902. */
  903. static void usb_deregister_bus (struct usb_bus *bus)
  904. {
  905. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  906. /*
  907. * NOTE: make sure that all the devices are removed by the
  908. * controller code, as well as having it call this when cleaning
  909. * itself up
  910. */
  911. mutex_lock(&usb_bus_idr_lock);
  912. idr_remove(&usb_bus_idr, bus->busnum);
  913. mutex_unlock(&usb_bus_idr_lock);
  914. usb_notify_remove_bus(bus);
  915. }
  916. /**
  917. * register_root_hub - called by usb_add_hcd() to register a root hub
  918. * @hcd: host controller for this root hub
  919. *
  920. * This function registers the root hub with the USB subsystem. It sets up
  921. * the device properly in the device tree and then calls usb_new_device()
  922. * to register the usb device. It also assigns the root hub's USB address
  923. * (always 1).
  924. *
  925. * Return: 0 if successful. A negative error code otherwise.
  926. */
  927. static int register_root_hub(struct usb_hcd *hcd)
  928. {
  929. struct device *parent_dev = hcd->self.controller;
  930. struct usb_device *usb_dev = hcd->self.root_hub;
  931. const int devnum = 1;
  932. int retval;
  933. usb_dev->devnum = devnum;
  934. usb_dev->bus->devnum_next = devnum + 1;
  935. memset (&usb_dev->bus->devmap.devicemap, 0,
  936. sizeof usb_dev->bus->devmap.devicemap);
  937. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  938. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  939. mutex_lock(&usb_bus_idr_lock);
  940. usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  941. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  942. if (retval != sizeof usb_dev->descriptor) {
  943. mutex_unlock(&usb_bus_idr_lock);
  944. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  945. dev_name(&usb_dev->dev), retval);
  946. return (retval < 0) ? retval : -EMSGSIZE;
  947. }
  948. if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) {
  949. retval = usb_get_bos_descriptor(usb_dev);
  950. if (!retval) {
  951. usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev);
  952. } else if (usb_dev->speed >= USB_SPEED_SUPER) {
  953. mutex_unlock(&usb_bus_idr_lock);
  954. dev_dbg(parent_dev, "can't read %s bos descriptor %d\n",
  955. dev_name(&usb_dev->dev), retval);
  956. return retval;
  957. }
  958. }
  959. retval = usb_new_device (usb_dev);
  960. if (retval) {
  961. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  962. dev_name(&usb_dev->dev), retval);
  963. } else {
  964. spin_lock_irq (&hcd_root_hub_lock);
  965. hcd->rh_registered = 1;
  966. spin_unlock_irq (&hcd_root_hub_lock);
  967. /* Did the HC die before the root hub was registered? */
  968. if (HCD_DEAD(hcd))
  969. usb_hc_died (hcd); /* This time clean up */
  970. }
  971. mutex_unlock(&usb_bus_idr_lock);
  972. return retval;
  973. }
  974. /*
  975. * usb_hcd_start_port_resume - a root-hub port is sending a resume signal
  976. * @bus: the bus which the root hub belongs to
  977. * @portnum: the port which is being resumed
  978. *
  979. * HCDs should call this function when they know that a resume signal is
  980. * being sent to a root-hub port. The root hub will be prevented from
  981. * going into autosuspend until usb_hcd_end_port_resume() is called.
  982. *
  983. * The bus's private lock must be held by the caller.
  984. */
  985. void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
  986. {
  987. unsigned bit = 1 << portnum;
  988. if (!(bus->resuming_ports & bit)) {
  989. bus->resuming_ports |= bit;
  990. pm_runtime_get_noresume(&bus->root_hub->dev);
  991. }
  992. }
  993. EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
  994. /*
  995. * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal
  996. * @bus: the bus which the root hub belongs to
  997. * @portnum: the port which is being resumed
  998. *
  999. * HCDs should call this function when they know that a resume signal has
  1000. * stopped being sent to a root-hub port. The root hub will be allowed to
  1001. * autosuspend again.
  1002. *
  1003. * The bus's private lock must be held by the caller.
  1004. */
  1005. void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
  1006. {
  1007. unsigned bit = 1 << portnum;
  1008. if (bus->resuming_ports & bit) {
  1009. bus->resuming_ports &= ~bit;
  1010. pm_runtime_put_noidle(&bus->root_hub->dev);
  1011. }
  1012. }
  1013. EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
  1014. /*-------------------------------------------------------------------------*/
  1015. /**
  1016. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  1017. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  1018. * @is_input: true iff the transaction sends data to the host
  1019. * @isoc: true for isochronous transactions, false for interrupt ones
  1020. * @bytecount: how many bytes in the transaction.
  1021. *
  1022. * Return: Approximate bus time in nanoseconds for a periodic transaction.
  1023. *
  1024. * Note:
  1025. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  1026. * scheduled in software, this function is only used for such scheduling.
  1027. */
  1028. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  1029. {
  1030. unsigned long tmp;
  1031. switch (speed) {
  1032. case USB_SPEED_LOW: /* INTR only */
  1033. if (is_input) {
  1034. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1035. return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  1036. } else {
  1037. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1038. return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  1039. }
  1040. case USB_SPEED_FULL: /* ISOC or INTR */
  1041. if (isoc) {
  1042. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1043. return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
  1044. } else {
  1045. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  1046. return 9107L + BW_HOST_DELAY + tmp;
  1047. }
  1048. case USB_SPEED_HIGH: /* ISOC or INTR */
  1049. /* FIXME adjust for input vs output */
  1050. if (isoc)
  1051. tmp = HS_NSECS_ISO (bytecount);
  1052. else
  1053. tmp = HS_NSECS (bytecount);
  1054. return tmp;
  1055. default:
  1056. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  1057. return -1;
  1058. }
  1059. }
  1060. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  1061. /*-------------------------------------------------------------------------*/
  1062. /*
  1063. * Generic HC operations.
  1064. */
  1065. /*-------------------------------------------------------------------------*/
  1066. /**
  1067. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  1068. * @hcd: host controller to which @urb was submitted
  1069. * @urb: URB being submitted
  1070. *
  1071. * Host controller drivers should call this routine in their enqueue()
  1072. * method. The HCD's private spinlock must be held and interrupts must
  1073. * be disabled. The actions carried out here are required for URB
  1074. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  1075. *
  1076. * Return: 0 for no error, otherwise a negative error code (in which case
  1077. * the enqueue() method must fail). If no error occurs but enqueue() fails
  1078. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  1079. * the private spinlock and returning.
  1080. */
  1081. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  1082. {
  1083. int rc = 0;
  1084. spin_lock(&hcd_urb_list_lock);
  1085. /* Check that the URB isn't being killed */
  1086. if (unlikely(atomic_read(&urb->reject))) {
  1087. rc = -EPERM;
  1088. goto done;
  1089. }
  1090. if (unlikely(!urb->ep->enabled)) {
  1091. rc = -ENOENT;
  1092. goto done;
  1093. }
  1094. if (unlikely(!urb->dev->can_submit)) {
  1095. rc = -EHOSTUNREACH;
  1096. goto done;
  1097. }
  1098. /*
  1099. * Check the host controller's state and add the URB to the
  1100. * endpoint's queue.
  1101. */
  1102. if (HCD_RH_RUNNING(hcd)) {
  1103. urb->unlinked = 0;
  1104. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  1105. } else {
  1106. rc = -ESHUTDOWN;
  1107. goto done;
  1108. }
  1109. done:
  1110. spin_unlock(&hcd_urb_list_lock);
  1111. return rc;
  1112. }
  1113. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  1114. /**
  1115. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  1116. * @hcd: host controller to which @urb was submitted
  1117. * @urb: URB being checked for unlinkability
  1118. * @status: error code to store in @urb if the unlink succeeds
  1119. *
  1120. * Host controller drivers should call this routine in their dequeue()
  1121. * method. The HCD's private spinlock must be held and interrupts must
  1122. * be disabled. The actions carried out here are required for making
  1123. * sure than an unlink is valid.
  1124. *
  1125. * Return: 0 for no error, otherwise a negative error code (in which case
  1126. * the dequeue() method must fail). The possible error codes are:
  1127. *
  1128. * -EIDRM: @urb was not submitted or has already completed.
  1129. * The completion function may not have been called yet.
  1130. *
  1131. * -EBUSY: @urb has already been unlinked.
  1132. */
  1133. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  1134. int status)
  1135. {
  1136. struct list_head *tmp;
  1137. /* insist the urb is still queued */
  1138. list_for_each(tmp, &urb->ep->urb_list) {
  1139. if (tmp == &urb->urb_list)
  1140. break;
  1141. }
  1142. if (tmp != &urb->urb_list)
  1143. return -EIDRM;
  1144. /* Any status except -EINPROGRESS means something already started to
  1145. * unlink this URB from the hardware. So there's no more work to do.
  1146. */
  1147. if (urb->unlinked)
  1148. return -EBUSY;
  1149. urb->unlinked = status;
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  1153. /**
  1154. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  1155. * @hcd: host controller to which @urb was submitted
  1156. * @urb: URB being unlinked
  1157. *
  1158. * Host controller drivers should call this routine before calling
  1159. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  1160. * interrupts must be disabled. The actions carried out here are required
  1161. * for URB completion.
  1162. */
  1163. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  1164. {
  1165. /* clear all state linking urb to this dev (and hcd) */
  1166. spin_lock(&hcd_urb_list_lock);
  1167. list_del_init(&urb->urb_list);
  1168. spin_unlock(&hcd_urb_list_lock);
  1169. }
  1170. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  1171. /*
  1172. * Some usb host controllers can only perform dma using a small SRAM area.
  1173. * The usb core itself is however optimized for host controllers that can dma
  1174. * using regular system memory - like pci devices doing bus mastering.
  1175. *
  1176. * To support host controllers with limited dma capabilities we provide dma
  1177. * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
  1178. * For this to work properly the host controller code must first use the
  1179. * function dma_declare_coherent_memory() to point out which memory area
  1180. * that should be used for dma allocations.
  1181. *
  1182. * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
  1183. * dma using dma_alloc_coherent() which in turn allocates from the memory
  1184. * area pointed out with dma_declare_coherent_memory().
  1185. *
  1186. * So, to summarize...
  1187. *
  1188. * - We need "local" memory, canonical example being
  1189. * a small SRAM on a discrete controller being the
  1190. * only memory that the controller can read ...
  1191. * (a) "normal" kernel memory is no good, and
  1192. * (b) there's not enough to share
  1193. *
  1194. * - The only *portable* hook for such stuff in the
  1195. * DMA framework is dma_declare_coherent_memory()
  1196. *
  1197. * - So we use that, even though the primary requirement
  1198. * is that the memory be "local" (hence addressable
  1199. * by that device), not "coherent".
  1200. *
  1201. */
  1202. static int hcd_alloc_coherent(struct usb_bus *bus,
  1203. gfp_t mem_flags, dma_addr_t *dma_handle,
  1204. void **vaddr_handle, size_t size,
  1205. enum dma_data_direction dir)
  1206. {
  1207. unsigned char *vaddr;
  1208. if (*vaddr_handle == NULL) {
  1209. WARN_ON_ONCE(1);
  1210. return -EFAULT;
  1211. }
  1212. vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
  1213. mem_flags, dma_handle);
  1214. if (!vaddr)
  1215. return -ENOMEM;
  1216. /*
  1217. * Store the virtual address of the buffer at the end
  1218. * of the allocated dma buffer. The size of the buffer
  1219. * may be uneven so use unaligned functions instead
  1220. * of just rounding up. It makes sense to optimize for
  1221. * memory footprint over access speed since the amount
  1222. * of memory available for dma may be limited.
  1223. */
  1224. put_unaligned((unsigned long)*vaddr_handle,
  1225. (unsigned long *)(vaddr + size));
  1226. if (dir == DMA_TO_DEVICE)
  1227. memcpy(vaddr, *vaddr_handle, size);
  1228. *vaddr_handle = vaddr;
  1229. return 0;
  1230. }
  1231. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1232. void **vaddr_handle, size_t size,
  1233. enum dma_data_direction dir)
  1234. {
  1235. unsigned char *vaddr = *vaddr_handle;
  1236. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1237. if (dir == DMA_FROM_DEVICE)
  1238. memcpy(vaddr, *vaddr_handle, size);
  1239. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1240. *vaddr_handle = vaddr;
  1241. *dma_handle = 0;
  1242. }
  1243. void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1244. {
  1245. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1246. (urb->transfer_flags & URB_SETUP_MAP_SINGLE))
  1247. dma_unmap_single(hcd->self.sysdev,
  1248. urb->setup_dma,
  1249. sizeof(struct usb_ctrlrequest),
  1250. DMA_TO_DEVICE);
  1251. else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
  1252. hcd_free_coherent(urb->dev->bus,
  1253. &urb->setup_dma,
  1254. (void **) &urb->setup_packet,
  1255. sizeof(struct usb_ctrlrequest),
  1256. DMA_TO_DEVICE);
  1257. /* Make it safe to call this routine more than once */
  1258. urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
  1259. }
  1260. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
  1261. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1262. {
  1263. if (hcd->driver->unmap_urb_for_dma)
  1264. hcd->driver->unmap_urb_for_dma(hcd, urb);
  1265. else
  1266. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1267. }
  1268. void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1269. {
  1270. enum dma_data_direction dir;
  1271. usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
  1272. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1273. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1274. (urb->transfer_flags & URB_DMA_MAP_SG))
  1275. dma_unmap_sg(hcd->self.sysdev,
  1276. urb->sg,
  1277. urb->num_sgs,
  1278. dir);
  1279. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1280. (urb->transfer_flags & URB_DMA_MAP_PAGE))
  1281. dma_unmap_page(hcd->self.sysdev,
  1282. urb->transfer_dma,
  1283. urb->transfer_buffer_length,
  1284. dir);
  1285. else if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1286. (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  1287. dma_unmap_single(hcd->self.sysdev,
  1288. urb->transfer_dma,
  1289. urb->transfer_buffer_length,
  1290. dir);
  1291. else if (urb->transfer_flags & URB_MAP_LOCAL)
  1292. hcd_free_coherent(urb->dev->bus,
  1293. &urb->transfer_dma,
  1294. &urb->transfer_buffer,
  1295. urb->transfer_buffer_length,
  1296. dir);
  1297. /* Make it safe to call this routine more than once */
  1298. urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
  1299. URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
  1300. }
  1301. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
  1302. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1303. gfp_t mem_flags)
  1304. {
  1305. if (hcd->driver->map_urb_for_dma)
  1306. return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
  1307. else
  1308. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  1309. }
  1310. int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1311. gfp_t mem_flags)
  1312. {
  1313. enum dma_data_direction dir;
  1314. int ret = 0;
  1315. /* Map the URB's buffers for DMA access.
  1316. * Lower level HCD code should use *_dma exclusively,
  1317. * unless it uses pio or talks to another transport,
  1318. * or uses the provided scatter gather list for bulk.
  1319. */
  1320. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  1321. if (hcd->self.uses_pio_for_control)
  1322. return ret;
  1323. if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {
  1324. if (is_vmalloc_addr(urb->setup_packet)) {
  1325. WARN_ONCE(1, "setup packet is not dma capable\n");
  1326. return -EAGAIN;
  1327. } else if (object_is_on_stack(urb->setup_packet)) {
  1328. WARN_ONCE(1, "setup packet is on stack\n");
  1329. return -EAGAIN;
  1330. }
  1331. urb->setup_dma = dma_map_single(
  1332. hcd->self.sysdev,
  1333. urb->setup_packet,
  1334. sizeof(struct usb_ctrlrequest),
  1335. DMA_TO_DEVICE);
  1336. if (dma_mapping_error(hcd->self.sysdev,
  1337. urb->setup_dma))
  1338. return -EAGAIN;
  1339. urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
  1340. } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1341. ret = hcd_alloc_coherent(
  1342. urb->dev->bus, mem_flags,
  1343. &urb->setup_dma,
  1344. (void **)&urb->setup_packet,
  1345. sizeof(struct usb_ctrlrequest),
  1346. DMA_TO_DEVICE);
  1347. if (ret)
  1348. return ret;
  1349. urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
  1350. }
  1351. }
  1352. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1353. if (urb->transfer_buffer_length != 0
  1354. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1355. if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {
  1356. if (urb->num_sgs) {
  1357. int n;
  1358. /* We don't support sg for isoc transfers ! */
  1359. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1360. WARN_ON(1);
  1361. return -EINVAL;
  1362. }
  1363. n = dma_map_sg(
  1364. hcd->self.sysdev,
  1365. urb->sg,
  1366. urb->num_sgs,
  1367. dir);
  1368. if (n <= 0)
  1369. ret = -EAGAIN;
  1370. else
  1371. urb->transfer_flags |= URB_DMA_MAP_SG;
  1372. urb->num_mapped_sgs = n;
  1373. if (n != urb->num_sgs)
  1374. urb->transfer_flags |=
  1375. URB_DMA_SG_COMBINED;
  1376. } else if (urb->sg) {
  1377. struct scatterlist *sg = urb->sg;
  1378. urb->transfer_dma = dma_map_page(
  1379. hcd->self.sysdev,
  1380. sg_page(sg),
  1381. sg->offset,
  1382. urb->transfer_buffer_length,
  1383. dir);
  1384. if (dma_mapping_error(hcd->self.sysdev,
  1385. urb->transfer_dma))
  1386. ret = -EAGAIN;
  1387. else
  1388. urb->transfer_flags |= URB_DMA_MAP_PAGE;
  1389. } else if (is_vmalloc_addr(urb->transfer_buffer)) {
  1390. WARN_ONCE(1, "transfer buffer not dma capable\n");
  1391. ret = -EAGAIN;
  1392. } else if (object_is_on_stack(urb->transfer_buffer)) {
  1393. WARN_ONCE(1, "transfer buffer is on stack\n");
  1394. ret = -EAGAIN;
  1395. } else {
  1396. urb->transfer_dma = dma_map_single(
  1397. hcd->self.sysdev,
  1398. urb->transfer_buffer,
  1399. urb->transfer_buffer_length,
  1400. dir);
  1401. if (dma_mapping_error(hcd->self.sysdev,
  1402. urb->transfer_dma))
  1403. ret = -EAGAIN;
  1404. else
  1405. urb->transfer_flags |= URB_DMA_MAP_SINGLE;
  1406. }
  1407. } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1408. ret = hcd_alloc_coherent(
  1409. urb->dev->bus, mem_flags,
  1410. &urb->transfer_dma,
  1411. &urb->transfer_buffer,
  1412. urb->transfer_buffer_length,
  1413. dir);
  1414. if (ret == 0)
  1415. urb->transfer_flags |= URB_MAP_LOCAL;
  1416. }
  1417. if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
  1418. URB_SETUP_MAP_LOCAL)))
  1419. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1420. }
  1421. return ret;
  1422. }
  1423. EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
  1424. /*-------------------------------------------------------------------------*/
  1425. /* may be called in any context with a valid urb->dev usecount
  1426. * caller surrenders "ownership" of urb
  1427. * expects usb_submit_urb() to have sanity checked and conditioned all
  1428. * inputs in the urb
  1429. */
  1430. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1431. {
  1432. int status;
  1433. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1434. /* increment urb's reference count as part of giving it to the HCD
  1435. * (which will control it). HCD guarantees that it either returns
  1436. * an error or calls giveback(), but not both.
  1437. */
  1438. usb_get_urb(urb);
  1439. atomic_inc(&urb->use_count);
  1440. atomic_inc(&urb->dev->urbnum);
  1441. usbmon_urb_submit(&hcd->self, urb);
  1442. /* NOTE requirements on root-hub callers (usbfs and the hub
  1443. * driver, for now): URBs' urb->transfer_buffer must be
  1444. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1445. * they could clobber root hub response data. Also, control
  1446. * URBs must be submitted in process context with interrupts
  1447. * enabled.
  1448. */
  1449. if (is_root_hub(urb->dev)) {
  1450. status = rh_urb_enqueue(hcd, urb);
  1451. } else {
  1452. status = map_urb_for_dma(hcd, urb, mem_flags);
  1453. if (likely(status == 0)) {
  1454. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1455. if (unlikely(status))
  1456. unmap_urb_for_dma(hcd, urb);
  1457. }
  1458. }
  1459. if (unlikely(status)) {
  1460. usbmon_urb_submit_error(&hcd->self, urb, status);
  1461. urb->hcpriv = NULL;
  1462. INIT_LIST_HEAD(&urb->urb_list);
  1463. atomic_dec(&urb->use_count);
  1464. atomic_dec(&urb->dev->urbnum);
  1465. if (atomic_read(&urb->reject))
  1466. wake_up(&usb_kill_urb_queue);
  1467. usb_put_urb(urb);
  1468. }
  1469. return status;
  1470. }
  1471. /*-------------------------------------------------------------------------*/
  1472. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1473. * off hardware queues (which may take a while) and returning it as
  1474. * soon as practical. we've already set up the urb's return status,
  1475. * but we can't know if the callback completed already.
  1476. */
  1477. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1478. {
  1479. int value;
  1480. if (is_root_hub(urb->dev))
  1481. value = usb_rh_urb_dequeue(hcd, urb, status);
  1482. else {
  1483. /* The only reason an HCD might fail this call is if
  1484. * it has not yet fully queued the urb to begin with.
  1485. * Such failures should be harmless. */
  1486. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1487. }
  1488. return value;
  1489. }
  1490. /*
  1491. * called in any context
  1492. *
  1493. * caller guarantees urb won't be recycled till both unlink()
  1494. * and the urb's completion function return
  1495. */
  1496. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1497. {
  1498. struct usb_hcd *hcd;
  1499. struct usb_device *udev = urb->dev;
  1500. int retval = -EIDRM;
  1501. unsigned long flags;
  1502. /* Prevent the device and bus from going away while
  1503. * the unlink is carried out. If they are already gone
  1504. * then urb->use_count must be 0, since disconnected
  1505. * devices can't have any active URBs.
  1506. */
  1507. spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
  1508. if (atomic_read(&urb->use_count) > 0) {
  1509. retval = 0;
  1510. usb_get_dev(udev);
  1511. }
  1512. spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
  1513. if (retval == 0) {
  1514. hcd = bus_to_hcd(urb->dev->bus);
  1515. retval = unlink1(hcd, urb, status);
  1516. if (retval == 0)
  1517. retval = -EINPROGRESS;
  1518. else if (retval != -EIDRM && retval != -EBUSY)
  1519. dev_dbg(&udev->dev, "hcd_unlink_urb %pK fail %d\n",
  1520. urb, retval);
  1521. usb_put_dev(udev);
  1522. }
  1523. return retval;
  1524. }
  1525. /*-------------------------------------------------------------------------*/
  1526. static void __usb_hcd_giveback_urb(struct urb *urb)
  1527. {
  1528. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1529. struct usb_anchor *anchor = urb->anchor;
  1530. int status = urb->unlinked;
  1531. urb->hcpriv = NULL;
  1532. if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1533. urb->actual_length < urb->transfer_buffer_length &&
  1534. !status))
  1535. status = -EREMOTEIO;
  1536. unmap_urb_for_dma(hcd, urb);
  1537. usbmon_urb_complete(&hcd->self, urb, status);
  1538. usb_anchor_suspend_wakeups(anchor);
  1539. usb_unanchor_urb(urb);
  1540. if (likely(status == 0))
  1541. usb_led_activity(USB_LED_EVENT_HOST);
  1542. /* pass ownership to the completion handler */
  1543. urb->status = status;
  1544. urb->complete(urb);
  1545. usb_anchor_resume_wakeups(anchor);
  1546. atomic_dec(&urb->use_count);
  1547. if (unlikely(atomic_read(&urb->reject)))
  1548. wake_up(&usb_kill_urb_queue);
  1549. usb_put_urb(urb);
  1550. }
  1551. static void usb_giveback_urb_bh(unsigned long param)
  1552. {
  1553. struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param;
  1554. struct list_head local_list;
  1555. spin_lock_irq(&bh->lock);
  1556. bh->running = true;
  1557. restart:
  1558. list_replace_init(&bh->head, &local_list);
  1559. spin_unlock_irq(&bh->lock);
  1560. while (!list_empty(&local_list)) {
  1561. struct urb *urb;
  1562. urb = list_entry(local_list.next, struct urb, urb_list);
  1563. list_del_init(&urb->urb_list);
  1564. bh->completing_ep = urb->ep;
  1565. __usb_hcd_giveback_urb(urb);
  1566. bh->completing_ep = NULL;
  1567. }
  1568. /* check if there are new URBs to giveback */
  1569. spin_lock_irq(&bh->lock);
  1570. if (!list_empty(&bh->head))
  1571. goto restart;
  1572. bh->running = false;
  1573. spin_unlock_irq(&bh->lock);
  1574. }
  1575. /**
  1576. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1577. * @hcd: host controller returning the URB
  1578. * @urb: urb being returned to the USB device driver.
  1579. * @status: completion status code for the URB.
  1580. * Context: in_interrupt()
  1581. *
  1582. * This hands the URB from HCD to its USB device driver, using its
  1583. * completion function. The HCD has freed all per-urb resources
  1584. * (and is done using urb->hcpriv). It also released all HCD locks;
  1585. * the device driver won't cause problems if it frees, modifies,
  1586. * or resubmits this URB.
  1587. *
  1588. * If @urb was unlinked, the value of @status will be overridden by
  1589. * @urb->unlinked. Erroneous short transfers are detected in case
  1590. * the HCD hasn't checked for them.
  1591. */
  1592. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1593. {
  1594. struct giveback_urb_bh *bh;
  1595. bool running, high_prio_bh;
  1596. /* pass status to tasklet via unlinked */
  1597. if (likely(!urb->unlinked))
  1598. urb->unlinked = status;
  1599. if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
  1600. __usb_hcd_giveback_urb(urb);
  1601. return;
  1602. }
  1603. if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) {
  1604. bh = &hcd->high_prio_bh;
  1605. high_prio_bh = true;
  1606. } else {
  1607. bh = &hcd->low_prio_bh;
  1608. high_prio_bh = false;
  1609. }
  1610. spin_lock(&bh->lock);
  1611. list_add_tail(&urb->urb_list, &bh->head);
  1612. running = bh->running;
  1613. spin_unlock(&bh->lock);
  1614. if (running)
  1615. ;
  1616. else if (high_prio_bh)
  1617. tasklet_hi_schedule(&bh->bh);
  1618. else
  1619. tasklet_schedule(&bh->bh);
  1620. }
  1621. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1622. /*-------------------------------------------------------------------------*/
  1623. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1624. * queue to drain completely. The caller must first insure that no more
  1625. * URBs can be submitted for this endpoint.
  1626. */
  1627. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1628. struct usb_host_endpoint *ep)
  1629. {
  1630. struct usb_hcd *hcd;
  1631. struct urb *urb;
  1632. if (!ep)
  1633. return;
  1634. might_sleep();
  1635. hcd = bus_to_hcd(udev->bus);
  1636. /* No more submits can occur */
  1637. spin_lock_irq(&hcd_urb_list_lock);
  1638. rescan:
  1639. list_for_each_entry_reverse(urb, &ep->urb_list, urb_list) {
  1640. int is_in;
  1641. if (urb->unlinked)
  1642. continue;
  1643. usb_get_urb (urb);
  1644. is_in = usb_urb_dir_in(urb);
  1645. spin_unlock(&hcd_urb_list_lock);
  1646. /* kick hcd */
  1647. unlink1(hcd, urb, -ESHUTDOWN);
  1648. dev_dbg (hcd->self.controller,
  1649. "shutdown urb %pK ep%d%s%s\n",
  1650. urb, usb_endpoint_num(&ep->desc),
  1651. is_in ? "in" : "out",
  1652. ({ char *s;
  1653. switch (usb_endpoint_type(&ep->desc)) {
  1654. case USB_ENDPOINT_XFER_CONTROL:
  1655. s = ""; break;
  1656. case USB_ENDPOINT_XFER_BULK:
  1657. s = "-bulk"; break;
  1658. case USB_ENDPOINT_XFER_INT:
  1659. s = "-intr"; break;
  1660. default:
  1661. s = "-iso"; break;
  1662. };
  1663. s;
  1664. }));
  1665. usb_put_urb (urb);
  1666. /* list contents may have changed */
  1667. spin_lock(&hcd_urb_list_lock);
  1668. goto rescan;
  1669. }
  1670. spin_unlock_irq(&hcd_urb_list_lock);
  1671. /* Wait until the endpoint queue is completely empty */
  1672. while (!list_empty (&ep->urb_list)) {
  1673. spin_lock_irq(&hcd_urb_list_lock);
  1674. /* The list may have changed while we acquired the spinlock */
  1675. urb = NULL;
  1676. if (!list_empty (&ep->urb_list)) {
  1677. urb = list_entry (ep->urb_list.prev, struct urb,
  1678. urb_list);
  1679. usb_get_urb (urb);
  1680. }
  1681. spin_unlock_irq(&hcd_urb_list_lock);
  1682. if (urb) {
  1683. usb_kill_urb (urb);
  1684. usb_put_urb (urb);
  1685. }
  1686. }
  1687. }
  1688. /**
  1689. * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
  1690. * the bus bandwidth
  1691. * @udev: target &usb_device
  1692. * @new_config: new configuration to install
  1693. * @cur_alt: the current alternate interface setting
  1694. * @new_alt: alternate interface setting that is being installed
  1695. *
  1696. * To change configurations, pass in the new configuration in new_config,
  1697. * and pass NULL for cur_alt and new_alt.
  1698. *
  1699. * To reset a device's configuration (put the device in the ADDRESSED state),
  1700. * pass in NULL for new_config, cur_alt, and new_alt.
  1701. *
  1702. * To change alternate interface settings, pass in NULL for new_config,
  1703. * pass in the current alternate interface setting in cur_alt,
  1704. * and pass in the new alternate interface setting in new_alt.
  1705. *
  1706. * Return: An error if the requested bandwidth change exceeds the
  1707. * bus bandwidth or host controller internal resources.
  1708. */
  1709. int usb_hcd_alloc_bandwidth(struct usb_device *udev,
  1710. struct usb_host_config *new_config,
  1711. struct usb_host_interface *cur_alt,
  1712. struct usb_host_interface *new_alt)
  1713. {
  1714. int num_intfs, i, j;
  1715. struct usb_host_interface *alt = NULL;
  1716. int ret = 0;
  1717. struct usb_hcd *hcd;
  1718. struct usb_host_endpoint *ep;
  1719. hcd = bus_to_hcd(udev->bus);
  1720. if (!hcd->driver->check_bandwidth)
  1721. return 0;
  1722. /* Configuration is being removed - set configuration 0 */
  1723. if (!new_config && !cur_alt) {
  1724. for (i = 1; i < 16; ++i) {
  1725. ep = udev->ep_out[i];
  1726. if (ep)
  1727. hcd->driver->drop_endpoint(hcd, udev, ep);
  1728. ep = udev->ep_in[i];
  1729. if (ep)
  1730. hcd->driver->drop_endpoint(hcd, udev, ep);
  1731. }
  1732. hcd->driver->check_bandwidth(hcd, udev);
  1733. return 0;
  1734. }
  1735. /* Check if the HCD says there's enough bandwidth. Enable all endpoints
  1736. * each interface's alt setting 0 and ask the HCD to check the bandwidth
  1737. * of the bus. There will always be bandwidth for endpoint 0, so it's
  1738. * ok to exclude it.
  1739. */
  1740. if (new_config) {
  1741. num_intfs = new_config->desc.bNumInterfaces;
  1742. /* Remove endpoints (except endpoint 0, which is always on the
  1743. * schedule) from the old config from the schedule
  1744. */
  1745. for (i = 1; i < 16; ++i) {
  1746. ep = udev->ep_out[i];
  1747. if (ep) {
  1748. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1749. if (ret < 0)
  1750. goto reset;
  1751. }
  1752. ep = udev->ep_in[i];
  1753. if (ep) {
  1754. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1755. if (ret < 0)
  1756. goto reset;
  1757. }
  1758. }
  1759. for (i = 0; i < num_intfs; ++i) {
  1760. struct usb_host_interface *first_alt;
  1761. int iface_num;
  1762. first_alt = &new_config->intf_cache[i]->altsetting[0];
  1763. iface_num = first_alt->desc.bInterfaceNumber;
  1764. /* Set up endpoints for alternate interface setting 0 */
  1765. alt = usb_find_alt_setting(new_config, iface_num, 0);
  1766. if (!alt)
  1767. /* No alt setting 0? Pick the first setting. */
  1768. alt = first_alt;
  1769. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  1770. ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
  1771. if (ret < 0)
  1772. goto reset;
  1773. }
  1774. }
  1775. }
  1776. if (cur_alt && new_alt) {
  1777. struct usb_interface *iface = usb_ifnum_to_if(udev,
  1778. cur_alt->desc.bInterfaceNumber);
  1779. if (!iface)
  1780. return -EINVAL;
  1781. if (iface->resetting_device) {
  1782. /*
  1783. * The USB core just reset the device, so the xHCI host
  1784. * and the device will think alt setting 0 is installed.
  1785. * However, the USB core will pass in the alternate
  1786. * setting installed before the reset as cur_alt. Dig
  1787. * out the alternate setting 0 structure, or the first
  1788. * alternate setting if a broken device doesn't have alt
  1789. * setting 0.
  1790. */
  1791. cur_alt = usb_altnum_to_altsetting(iface, 0);
  1792. if (!cur_alt)
  1793. cur_alt = &iface->altsetting[0];
  1794. }
  1795. /* Drop all the endpoints in the current alt setting */
  1796. for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
  1797. ret = hcd->driver->drop_endpoint(hcd, udev,
  1798. &cur_alt->endpoint[i]);
  1799. if (ret < 0)
  1800. goto reset;
  1801. }
  1802. /* Add all the endpoints in the new alt setting */
  1803. for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
  1804. ret = hcd->driver->add_endpoint(hcd, udev,
  1805. &new_alt->endpoint[i]);
  1806. if (ret < 0)
  1807. goto reset;
  1808. }
  1809. }
  1810. ret = hcd->driver->check_bandwidth(hcd, udev);
  1811. reset:
  1812. if (ret < 0)
  1813. hcd->driver->reset_bandwidth(hcd, udev);
  1814. return ret;
  1815. }
  1816. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1817. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1818. * have been called previously. Use for set_configuration, set_interface,
  1819. * driver removal, physical disconnect.
  1820. *
  1821. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1822. * type, maxpacket size, toggle, halt status, and scheduling.
  1823. */
  1824. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1825. struct usb_host_endpoint *ep)
  1826. {
  1827. struct usb_hcd *hcd;
  1828. might_sleep();
  1829. hcd = bus_to_hcd(udev->bus);
  1830. if (hcd->driver->endpoint_disable)
  1831. hcd->driver->endpoint_disable(hcd, ep);
  1832. }
  1833. /**
  1834. * usb_hcd_reset_endpoint - reset host endpoint state
  1835. * @udev: USB device.
  1836. * @ep: the endpoint to reset.
  1837. *
  1838. * Resets any host endpoint state such as the toggle bit, sequence
  1839. * number and current window.
  1840. */
  1841. void usb_hcd_reset_endpoint(struct usb_device *udev,
  1842. struct usb_host_endpoint *ep)
  1843. {
  1844. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1845. if (hcd->driver->endpoint_reset)
  1846. hcd->driver->endpoint_reset(hcd, ep);
  1847. else {
  1848. int epnum = usb_endpoint_num(&ep->desc);
  1849. int is_out = usb_endpoint_dir_out(&ep->desc);
  1850. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1851. usb_settoggle(udev, epnum, is_out, 0);
  1852. if (is_control)
  1853. usb_settoggle(udev, epnum, !is_out, 0);
  1854. }
  1855. }
  1856. /**
  1857. * usb_alloc_streams - allocate bulk endpoint stream IDs.
  1858. * @interface: alternate setting that includes all endpoints.
  1859. * @eps: array of endpoints that need streams.
  1860. * @num_eps: number of endpoints in the array.
  1861. * @num_streams: number of streams to allocate.
  1862. * @mem_flags: flags hcd should use to allocate memory.
  1863. *
  1864. * Sets up a group of bulk endpoints to have @num_streams stream IDs available.
  1865. * Drivers may queue multiple transfers to different stream IDs, which may
  1866. * complete in a different order than they were queued.
  1867. *
  1868. * Return: On success, the number of allocated streams. On failure, a negative
  1869. * error code.
  1870. */
  1871. int usb_alloc_streams(struct usb_interface *interface,
  1872. struct usb_host_endpoint **eps, unsigned int num_eps,
  1873. unsigned int num_streams, gfp_t mem_flags)
  1874. {
  1875. struct usb_hcd *hcd;
  1876. struct usb_device *dev;
  1877. int i, ret;
  1878. dev = interface_to_usbdev(interface);
  1879. hcd = bus_to_hcd(dev->bus);
  1880. if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
  1881. return -EINVAL;
  1882. if (dev->speed < USB_SPEED_SUPER)
  1883. return -EINVAL;
  1884. if (dev->state < USB_STATE_CONFIGURED)
  1885. return -ENODEV;
  1886. for (i = 0; i < num_eps; i++) {
  1887. /* Streams only apply to bulk endpoints. */
  1888. if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
  1889. return -EINVAL;
  1890. /* Re-alloc is not allowed */
  1891. if (eps[i]->streams)
  1892. return -EINVAL;
  1893. }
  1894. ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
  1895. num_streams, mem_flags);
  1896. if (ret < 0)
  1897. return ret;
  1898. for (i = 0; i < num_eps; i++)
  1899. eps[i]->streams = ret;
  1900. return ret;
  1901. }
  1902. EXPORT_SYMBOL_GPL(usb_alloc_streams);
  1903. /**
  1904. * usb_free_streams - free bulk endpoint stream IDs.
  1905. * @interface: alternate setting that includes all endpoints.
  1906. * @eps: array of endpoints to remove streams from.
  1907. * @num_eps: number of endpoints in the array.
  1908. * @mem_flags: flags hcd should use to allocate memory.
  1909. *
  1910. * Reverts a group of bulk endpoints back to not using stream IDs.
  1911. * Can fail if we are given bad arguments, or HCD is broken.
  1912. *
  1913. * Return: 0 on success. On failure, a negative error code.
  1914. */
  1915. int usb_free_streams(struct usb_interface *interface,
  1916. struct usb_host_endpoint **eps, unsigned int num_eps,
  1917. gfp_t mem_flags)
  1918. {
  1919. struct usb_hcd *hcd;
  1920. struct usb_device *dev;
  1921. int i, ret;
  1922. dev = interface_to_usbdev(interface);
  1923. hcd = bus_to_hcd(dev->bus);
  1924. if (dev->speed < USB_SPEED_SUPER)
  1925. return -EINVAL;
  1926. /* Double-free is not allowed */
  1927. for (i = 0; i < num_eps; i++)
  1928. if (!eps[i] || !eps[i]->streams)
  1929. return -EINVAL;
  1930. ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
  1931. if (ret < 0)
  1932. return ret;
  1933. for (i = 0; i < num_eps; i++)
  1934. eps[i]->streams = 0;
  1935. return ret;
  1936. }
  1937. EXPORT_SYMBOL_GPL(usb_free_streams);
  1938. /* Protect against drivers that try to unlink URBs after the device
  1939. * is gone, by waiting until all unlinks for @udev are finished.
  1940. * Since we don't currently track URBs by device, simply wait until
  1941. * nothing is running in the locked region of usb_hcd_unlink_urb().
  1942. */
  1943. void usb_hcd_synchronize_unlinks(struct usb_device *udev)
  1944. {
  1945. spin_lock_irq(&hcd_urb_unlink_lock);
  1946. spin_unlock_irq(&hcd_urb_unlink_lock);
  1947. }
  1948. /*-------------------------------------------------------------------------*/
  1949. /* called in any context */
  1950. int usb_hcd_get_frame_number (struct usb_device *udev)
  1951. {
  1952. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1953. if (!HCD_RH_RUNNING(hcd))
  1954. return -ESHUTDOWN;
  1955. return hcd->driver->get_frame_number (hcd);
  1956. }
  1957. /*-------------------------------------------------------------------------*/
  1958. #ifdef CONFIG_PM
  1959. int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
  1960. {
  1961. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  1962. int status;
  1963. int old_state = hcd->state;
  1964. dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
  1965. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  1966. rhdev->do_remote_wakeup);
  1967. if (HCD_DEAD(hcd)) {
  1968. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
  1969. return 0;
  1970. }
  1971. if (!hcd->driver->bus_suspend) {
  1972. status = -ENOENT;
  1973. } else {
  1974. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1975. hcd->state = HC_STATE_QUIESCING;
  1976. status = hcd->driver->bus_suspend(hcd);
  1977. }
  1978. if (status == 0) {
  1979. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  1980. hcd->state = HC_STATE_SUSPENDED;
  1981. if (!PMSG_IS_AUTO(msg))
  1982. usb_phy_roothub_suspend(hcd->self.sysdev,
  1983. hcd->phy_roothub);
  1984. /* Did we race with a root-hub wakeup event? */
  1985. if (rhdev->do_remote_wakeup) {
  1986. char buffer[6];
  1987. status = hcd->driver->hub_status_data(hcd, buffer);
  1988. if (status != 0) {
  1989. dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
  1990. hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
  1991. status = -EBUSY;
  1992. }
  1993. }
  1994. } else {
  1995. spin_lock_irq(&hcd_root_hub_lock);
  1996. if (!HCD_DEAD(hcd)) {
  1997. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1998. hcd->state = old_state;
  1999. }
  2000. spin_unlock_irq(&hcd_root_hub_lock);
  2001. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  2002. "suspend", status);
  2003. }
  2004. return status;
  2005. }
  2006. int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
  2007. {
  2008. struct usb_hcd *hcd = bus_to_hcd(rhdev->bus);
  2009. int status;
  2010. int old_state = hcd->state;
  2011. dev_dbg(&rhdev->dev, "usb %sresume\n",
  2012. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  2013. if (HCD_DEAD(hcd)) {
  2014. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
  2015. return 0;
  2016. }
  2017. if (!PMSG_IS_AUTO(msg)) {
  2018. status = usb_phy_roothub_resume(hcd->self.sysdev,
  2019. hcd->phy_roothub);
  2020. if (status)
  2021. return status;
  2022. }
  2023. if (!hcd->driver->bus_resume)
  2024. return -ENOENT;
  2025. if (HCD_RH_RUNNING(hcd))
  2026. return 0;
  2027. hcd->state = HC_STATE_RESUMING;
  2028. status = hcd->driver->bus_resume(hcd);
  2029. clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  2030. if (status == 0) {
  2031. struct usb_device *udev;
  2032. int port1;
  2033. spin_lock_irq(&hcd_root_hub_lock);
  2034. if (!HCD_DEAD(hcd)) {
  2035. usb_set_device_state(rhdev, rhdev->actconfig
  2036. ? USB_STATE_CONFIGURED
  2037. : USB_STATE_ADDRESS);
  2038. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2039. hcd->state = HC_STATE_RUNNING;
  2040. }
  2041. spin_unlock_irq(&hcd_root_hub_lock);
  2042. /*
  2043. * Check whether any of the enabled ports on the root hub are
  2044. * unsuspended. If they are then a TRSMRCY delay is needed
  2045. * (this is what the USB-2 spec calls a "global resume").
  2046. * Otherwise we can skip the delay.
  2047. */
  2048. usb_hub_for_each_child(rhdev, port1, udev) {
  2049. if (udev->state != USB_STATE_NOTATTACHED &&
  2050. !udev->port_is_suspended) {
  2051. usleep_range(10000, 11000); /* TRSMRCY */
  2052. break;
  2053. }
  2054. }
  2055. } else {
  2056. hcd->state = old_state;
  2057. usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
  2058. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  2059. "resume", status);
  2060. if (status != -ESHUTDOWN)
  2061. usb_hc_died(hcd);
  2062. }
  2063. return status;
  2064. }
  2065. /* Workqueue routine for root-hub remote wakeup */
  2066. static void hcd_resume_work(struct work_struct *work)
  2067. {
  2068. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  2069. struct usb_device *udev = hcd->self.root_hub;
  2070. usb_remote_wakeup(udev);
  2071. }
  2072. /**
  2073. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  2074. * @hcd: host controller for this root hub
  2075. *
  2076. * The USB host controller calls this function when its root hub is
  2077. * suspended (with the remote wakeup feature enabled) and a remote
  2078. * wakeup request is received. The routine submits a workqueue request
  2079. * to resume the root hub (that is, manage its downstream ports again).
  2080. */
  2081. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  2082. {
  2083. unsigned long flags;
  2084. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2085. if (hcd->rh_registered) {
  2086. pm_wakeup_event(&hcd->self.root_hub->dev, 0);
  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 (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) {
  2407. hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
  2408. if (IS_ERR(hcd->phy_roothub))
  2409. return PTR_ERR(hcd->phy_roothub);
  2410. retval = usb_phy_roothub_init(hcd->phy_roothub);
  2411. if (retval)
  2412. return retval;
  2413. retval = usb_phy_roothub_power_on(hcd->phy_roothub);
  2414. if (retval)
  2415. goto err_usb_phy_roothub_power_on;
  2416. }
  2417. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  2418. /* Keep old behaviour if authorized_default is not in [0, 1]. */
  2419. if (authorized_default < 0 || authorized_default > 1) {
  2420. if (hcd->wireless)
  2421. clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2422. else
  2423. set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2424. } else {
  2425. if (authorized_default)
  2426. set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2427. else
  2428. clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
  2429. }
  2430. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2431. /* per default all interfaces are authorized */
  2432. set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
  2433. /* HC is in reset state, but accessible. Now do the one-time init,
  2434. * bottom up so that hcds can customize the root hubs before hub_wq
  2435. * starts talking to them. (Note, bus id is assigned early too.)
  2436. */
  2437. retval = hcd_buffer_create(hcd);
  2438. if (retval != 0) {
  2439. dev_dbg(hcd->self.sysdev, "pool alloc failed\n");
  2440. goto err_create_buf;
  2441. }
  2442. retval = usb_register_bus(&hcd->self);
  2443. if (retval < 0)
  2444. goto err_register_bus;
  2445. rhdev = usb_alloc_dev(NULL, &hcd->self, 0);
  2446. if (rhdev == NULL) {
  2447. dev_err(hcd->self.sysdev, "unable to allocate root hub\n");
  2448. retval = -ENOMEM;
  2449. goto err_allocate_root_hub;
  2450. }
  2451. mutex_lock(&usb_port_peer_mutex);
  2452. hcd->self.root_hub = rhdev;
  2453. mutex_unlock(&usb_port_peer_mutex);
  2454. rhdev->rx_lanes = 1;
  2455. rhdev->tx_lanes = 1;
  2456. switch (hcd->speed) {
  2457. case HCD_USB11:
  2458. rhdev->speed = USB_SPEED_FULL;
  2459. break;
  2460. case HCD_USB2:
  2461. rhdev->speed = USB_SPEED_HIGH;
  2462. break;
  2463. case HCD_USB25:
  2464. rhdev->speed = USB_SPEED_WIRELESS;
  2465. break;
  2466. case HCD_USB3:
  2467. rhdev->speed = USB_SPEED_SUPER;
  2468. break;
  2469. case HCD_USB32:
  2470. rhdev->rx_lanes = 2;
  2471. rhdev->tx_lanes = 2;
  2472. /* fall through */
  2473. case HCD_USB31:
  2474. rhdev->speed = USB_SPEED_SUPER_PLUS;
  2475. break;
  2476. default:
  2477. retval = -EINVAL;
  2478. goto err_set_rh_speed;
  2479. }
  2480. /* wakeup flag init defaults to "everything works" for root hubs,
  2481. * but drivers can override it in reset() if needed, along with
  2482. * recording the overall controller's system wakeup capability.
  2483. */
  2484. device_set_wakeup_capable(&rhdev->dev, 1);
  2485. /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
  2486. * registered. But since the controller can die at any time,
  2487. * let's initialize the flag before touching the hardware.
  2488. */
  2489. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2490. /* "reset" is misnamed; its role is now one-time init. the controller
  2491. * should already have been reset (and boot firmware kicked off etc).
  2492. */
  2493. if (hcd->driver->reset) {
  2494. retval = hcd->driver->reset(hcd);
  2495. if (retval < 0) {
  2496. dev_err(hcd->self.controller, "can't setup: %d\n",
  2497. retval);
  2498. goto err_hcd_driver_setup;
  2499. }
  2500. }
  2501. hcd->rh_pollable = 1;
  2502. /* NOTE: root hub and controller capabilities may not be the same */
  2503. if (device_can_wakeup(hcd->self.controller)
  2504. && device_can_wakeup(&hcd->self.root_hub->dev))
  2505. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  2506. /* initialize tasklets */
  2507. init_giveback_urb_bh(&hcd->high_prio_bh);
  2508. init_giveback_urb_bh(&hcd->low_prio_bh);
  2509. /* enable irqs just before we start the controller,
  2510. * if the BIOS provides legacy PCI irqs.
  2511. */
  2512. if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
  2513. retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
  2514. if (retval)
  2515. goto err_request_irq;
  2516. }
  2517. hcd->state = HC_STATE_RUNNING;
  2518. retval = hcd->driver->start(hcd);
  2519. if (retval < 0) {
  2520. dev_err(hcd->self.controller, "startup error %d\n", retval);
  2521. goto err_hcd_driver_start;
  2522. }
  2523. /* starting here, usbcore will pay attention to this root hub */
  2524. retval = register_root_hub(hcd);
  2525. if (retval != 0)
  2526. goto err_register_root_hub;
  2527. retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  2528. if (retval < 0) {
  2529. printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
  2530. retval);
  2531. goto error_create_attr_group;
  2532. }
  2533. if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
  2534. usb_hcd_poll_rh_status(hcd);
  2535. return retval;
  2536. error_create_attr_group:
  2537. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2538. if (HC_IS_RUNNING(hcd->state))
  2539. hcd->state = HC_STATE_QUIESCING;
  2540. spin_lock_irq(&hcd_root_hub_lock);
  2541. hcd->rh_registered = 0;
  2542. spin_unlock_irq(&hcd_root_hub_lock);
  2543. #ifdef CONFIG_PM
  2544. cancel_work_sync(&hcd->wakeup_work);
  2545. #endif
  2546. mutex_lock(&usb_bus_idr_lock);
  2547. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2548. mutex_unlock(&usb_bus_idr_lock);
  2549. err_register_root_hub:
  2550. hcd->rh_pollable = 0;
  2551. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2552. del_timer_sync(&hcd->rh_timer);
  2553. hcd->driver->stop(hcd);
  2554. hcd->state = HC_STATE_HALT;
  2555. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2556. del_timer_sync(&hcd->rh_timer);
  2557. err_hcd_driver_start:
  2558. if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
  2559. free_irq(irqnum, hcd);
  2560. err_request_irq:
  2561. err_hcd_driver_setup:
  2562. err_set_rh_speed:
  2563. usb_put_invalidate_rhdev(hcd);
  2564. err_allocate_root_hub:
  2565. usb_deregister_bus(&hcd->self);
  2566. err_register_bus:
  2567. hcd_buffer_destroy(hcd);
  2568. err_create_buf:
  2569. usb_phy_roothub_power_off(hcd->phy_roothub);
  2570. err_usb_phy_roothub_power_on:
  2571. usb_phy_roothub_exit(hcd->phy_roothub);
  2572. return retval;
  2573. }
  2574. EXPORT_SYMBOL_GPL(usb_add_hcd);
  2575. /**
  2576. * usb_remove_hcd - shutdown processing for generic HCDs
  2577. * @hcd: the usb_hcd structure to remove
  2578. * Context: !in_interrupt()
  2579. *
  2580. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  2581. * invoking the HCD's stop() method.
  2582. */
  2583. void usb_remove_hcd(struct usb_hcd *hcd)
  2584. {
  2585. struct usb_device *rhdev = hcd->self.root_hub;
  2586. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  2587. usb_get_dev(rhdev);
  2588. sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  2589. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2590. if (HC_IS_RUNNING (hcd->state))
  2591. hcd->state = HC_STATE_QUIESCING;
  2592. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  2593. spin_lock_irq (&hcd_root_hub_lock);
  2594. hcd->rh_registered = 0;
  2595. spin_unlock_irq (&hcd_root_hub_lock);
  2596. #ifdef CONFIG_PM
  2597. cancel_work_sync(&hcd->wakeup_work);
  2598. #endif
  2599. mutex_lock(&usb_bus_idr_lock);
  2600. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2601. mutex_unlock(&usb_bus_idr_lock);
  2602. /*
  2603. * tasklet_kill() isn't needed here because:
  2604. * - driver's disconnect() called from usb_disconnect() should
  2605. * make sure its URBs are completed during the disconnect()
  2606. * callback
  2607. *
  2608. * - it is too late to run complete() here since driver may have
  2609. * been removed already now
  2610. */
  2611. /* Prevent any more root-hub status calls from the timer.
  2612. * The HCD might still restart the timer (if a port status change
  2613. * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
  2614. * the hub_status_data() callback.
  2615. */
  2616. hcd->rh_pollable = 0;
  2617. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2618. del_timer_sync(&hcd->rh_timer);
  2619. hcd->driver->stop(hcd);
  2620. hcd->state = HC_STATE_HALT;
  2621. /* In case the HCD restarted the timer, stop it again. */
  2622. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2623. del_timer_sync(&hcd->rh_timer);
  2624. if (usb_hcd_is_primary_hcd(hcd)) {
  2625. if (hcd->irq > 0)
  2626. free_irq(hcd->irq, hcd);
  2627. }
  2628. usb_deregister_bus(&hcd->self);
  2629. hcd_buffer_destroy(hcd);
  2630. usb_phy_roothub_power_off(hcd->phy_roothub);
  2631. usb_phy_roothub_exit(hcd->phy_roothub);
  2632. usb_put_invalidate_rhdev(hcd);
  2633. hcd->flags = 0;
  2634. }
  2635. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  2636. void
  2637. usb_hcd_platform_shutdown(struct platform_device *dev)
  2638. {
  2639. struct usb_hcd *hcd = platform_get_drvdata(dev);
  2640. if (hcd->driver->shutdown)
  2641. hcd->driver->shutdown(hcd);
  2642. }
  2643. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  2644. /*-------------------------------------------------------------------------*/
  2645. #if IS_ENABLED(CONFIG_USB_MON)
  2646. const struct usb_mon_operations *mon_ops;
  2647. /*
  2648. * The registration is unlocked.
  2649. * We do it this way because we do not want to lock in hot paths.
  2650. *
  2651. * Notice that the code is minimally error-proof. Because usbmon needs
  2652. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  2653. */
  2654. int usb_mon_register(const struct usb_mon_operations *ops)
  2655. {
  2656. if (mon_ops)
  2657. return -EBUSY;
  2658. mon_ops = ops;
  2659. mb();
  2660. return 0;
  2661. }
  2662. EXPORT_SYMBOL_GPL (usb_mon_register);
  2663. void usb_mon_deregister (void)
  2664. {
  2665. if (mon_ops == NULL) {
  2666. printk(KERN_ERR "USB: monitor was not registered\n");
  2667. return;
  2668. }
  2669. mon_ops = NULL;
  2670. mb();
  2671. }
  2672. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  2673. #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */