hcd.c 86 KB

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