hcd.c 71 KB

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