hcd.c 88 KB

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