hcd.c 87 KB

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