hcd.c 83 KB

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