hcd.c 79 KB

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