hcd.c 62 KB

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