hcd.c 87 KB

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