hcd.c 76 KB

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