hcd.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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/config.h>
  25. #include <linux/module.h>
  26. #include <linux/version.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/completion.h>
  30. #include <linux/utsname.h>
  31. #include <linux/mm.h>
  32. #include <asm/io.h>
  33. #include <asm/scatterlist.h>
  34. #include <linux/device.h>
  35. #include <linux/dma-mapping.h>
  36. #include <asm/irq.h>
  37. #include <asm/byteorder.h>
  38. #include <linux/usb.h>
  39. #include "usb.h"
  40. #include "hcd.h"
  41. #include "hub.h"
  42. // #define USB_BANDWIDTH_MESSAGES
  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. /* host controllers we manage */
  76. LIST_HEAD (usb_bus_list);
  77. EXPORT_SYMBOL_GPL (usb_bus_list);
  78. /* used when allocating bus numbers */
  79. #define USB_MAXBUS 64
  80. struct usb_busmap {
  81. unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
  82. };
  83. static struct usb_busmap busmap;
  84. /* used when updating list of hcds */
  85. DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */
  86. EXPORT_SYMBOL_GPL (usb_bus_list_lock);
  87. /* used for controlling access to virtual root hubs */
  88. static DEFINE_SPINLOCK(hcd_root_hub_lock);
  89. /* used when updating hcd data */
  90. static DEFINE_SPINLOCK(hcd_data_lock);
  91. /* wait queue for synchronous unlinks */
  92. DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
  93. /*-------------------------------------------------------------------------*/
  94. /*
  95. * Sharable chunks of root hub code.
  96. */
  97. /*-------------------------------------------------------------------------*/
  98. #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
  99. #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
  100. /* usb 2.0 root hub device descriptor */
  101. static const u8 usb2_rh_dev_descriptor [18] = {
  102. 0x12, /* __u8 bLength; */
  103. 0x01, /* __u8 bDescriptorType; Device */
  104. 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
  105. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  106. 0x00, /* __u8 bDeviceSubClass; */
  107. 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/
  108. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  109. 0x00, 0x00, /* __le16 idVendor; */
  110. 0x00, 0x00, /* __le16 idProduct; */
  111. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  112. 0x03, /* __u8 iManufacturer; */
  113. 0x02, /* __u8 iProduct; */
  114. 0x01, /* __u8 iSerialNumber; */
  115. 0x01 /* __u8 bNumConfigurations; */
  116. };
  117. /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
  118. /* usb 1.1 root hub device descriptor */
  119. static const u8 usb11_rh_dev_descriptor [18] = {
  120. 0x12, /* __u8 bLength; */
  121. 0x01, /* __u8 bDescriptorType; Device */
  122. 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
  123. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  124. 0x00, /* __u8 bDeviceSubClass; */
  125. 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
  126. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  127. 0x00, 0x00, /* __le16 idVendor; */
  128. 0x00, 0x00, /* __le16 idProduct; */
  129. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  130. 0x03, /* __u8 iManufacturer; */
  131. 0x02, /* __u8 iProduct; */
  132. 0x01, /* __u8 iSerialNumber; */
  133. 0x01 /* __u8 bNumConfigurations; */
  134. };
  135. /*-------------------------------------------------------------------------*/
  136. /* Configuration descriptors for our root hubs */
  137. static const u8 fs_rh_config_descriptor [] = {
  138. /* one configuration */
  139. 0x09, /* __u8 bLength; */
  140. 0x02, /* __u8 bDescriptorType; Configuration */
  141. 0x19, 0x00, /* __le16 wTotalLength; */
  142. 0x01, /* __u8 bNumInterfaces; (1) */
  143. 0x01, /* __u8 bConfigurationValue; */
  144. 0x00, /* __u8 iConfiguration; */
  145. 0xc0, /* __u8 bmAttributes;
  146. Bit 7: must be set,
  147. 6: Self-powered,
  148. 5: Remote wakeup,
  149. 4..0: resvd */
  150. 0x00, /* __u8 MaxPower; */
  151. /* USB 1.1:
  152. * USB 2.0, single TT organization (mandatory):
  153. * one interface, protocol 0
  154. *
  155. * USB 2.0, multiple TT organization (optional):
  156. * two interfaces, protocols 1 (like single TT)
  157. * and 2 (multiple TT mode) ... config is
  158. * sometimes settable
  159. * NOT IMPLEMENTED
  160. */
  161. /* one interface */
  162. 0x09, /* __u8 if_bLength; */
  163. 0x04, /* __u8 if_bDescriptorType; Interface */
  164. 0x00, /* __u8 if_bInterfaceNumber; */
  165. 0x00, /* __u8 if_bAlternateSetting; */
  166. 0x01, /* __u8 if_bNumEndpoints; */
  167. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  168. 0x00, /* __u8 if_bInterfaceSubClass; */
  169. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  170. 0x00, /* __u8 if_iInterface; */
  171. /* one endpoint (status change endpoint) */
  172. 0x07, /* __u8 ep_bLength; */
  173. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  174. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  175. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  176. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  177. 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  178. };
  179. static const u8 hs_rh_config_descriptor [] = {
  180. /* one configuration */
  181. 0x09, /* __u8 bLength; */
  182. 0x02, /* __u8 bDescriptorType; Configuration */
  183. 0x19, 0x00, /* __le16 wTotalLength; */
  184. 0x01, /* __u8 bNumInterfaces; (1) */
  185. 0x01, /* __u8 bConfigurationValue; */
  186. 0x00, /* __u8 iConfiguration; */
  187. 0xc0, /* __u8 bmAttributes;
  188. Bit 7: must be set,
  189. 6: Self-powered,
  190. 5: Remote wakeup,
  191. 4..0: resvd */
  192. 0x00, /* __u8 MaxPower; */
  193. /* USB 1.1:
  194. * USB 2.0, single TT organization (mandatory):
  195. * one interface, protocol 0
  196. *
  197. * USB 2.0, multiple TT organization (optional):
  198. * two interfaces, protocols 1 (like single TT)
  199. * and 2 (multiple TT mode) ... config is
  200. * sometimes settable
  201. * NOT IMPLEMENTED
  202. */
  203. /* one interface */
  204. 0x09, /* __u8 if_bLength; */
  205. 0x04, /* __u8 if_bDescriptorType; Interface */
  206. 0x00, /* __u8 if_bInterfaceNumber; */
  207. 0x00, /* __u8 if_bAlternateSetting; */
  208. 0x01, /* __u8 if_bNumEndpoints; */
  209. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  210. 0x00, /* __u8 if_bInterfaceSubClass; */
  211. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  212. 0x00, /* __u8 if_iInterface; */
  213. /* one endpoint (status change endpoint) */
  214. 0x07, /* __u8 ep_bLength; */
  215. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  216. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  217. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  218. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  219. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  220. };
  221. /*-------------------------------------------------------------------------*/
  222. /*
  223. * helper routine for returning string descriptors in UTF-16LE
  224. * input can actually be ISO-8859-1; ASCII is its 7-bit subset
  225. */
  226. static int ascii2utf (char *s, u8 *utf, int utfmax)
  227. {
  228. int retval;
  229. for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
  230. *utf++ = *s++;
  231. *utf++ = 0;
  232. }
  233. if (utfmax > 0) {
  234. *utf = *s;
  235. ++retval;
  236. }
  237. return retval;
  238. }
  239. /*
  240. * rh_string - provides manufacturer, product and serial strings for root hub
  241. * @id: the string ID number (1: serial number, 2: product, 3: vendor)
  242. * @hcd: the host controller for this root hub
  243. * @type: string describing our driver
  244. * @data: return packet in UTF-16 LE
  245. * @len: length of the return packet
  246. *
  247. * Produces either a manufacturer, product or serial number string for the
  248. * virtual root hub device.
  249. */
  250. static int rh_string (
  251. int id,
  252. struct usb_hcd *hcd,
  253. u8 *data,
  254. int len
  255. ) {
  256. char buf [100];
  257. // language ids
  258. if (id == 0) {
  259. buf[0] = 4; buf[1] = 3; /* 4 bytes string data */
  260. buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */
  261. len = min (len, 4);
  262. memcpy (data, buf, len);
  263. return len;
  264. // serial number
  265. } else if (id == 1) {
  266. strlcpy (buf, hcd->self.bus_name, sizeof buf);
  267. // product description
  268. } else if (id == 2) {
  269. strlcpy (buf, hcd->product_desc, sizeof buf);
  270. // id 3 == vendor description
  271. } else if (id == 3) {
  272. snprintf (buf, sizeof buf, "%s %s %s", system_utsname.sysname,
  273. system_utsname.release, hcd->driver->description);
  274. // unsupported IDs --> "protocol stall"
  275. } else
  276. return -EPIPE;
  277. switch (len) { /* All cases fall through */
  278. default:
  279. len = 2 + ascii2utf (buf, data + 2, len - 2);
  280. case 2:
  281. data [1] = 3; /* type == string */
  282. case 1:
  283. data [0] = 2 * (strlen (buf) + 1);
  284. case 0:
  285. ; /* Compiler wants a statement here */
  286. }
  287. return len;
  288. }
  289. /* Root hub control transfers execute synchronously */
  290. static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
  291. {
  292. struct usb_ctrlrequest *cmd;
  293. u16 typeReq, wValue, wIndex, wLength;
  294. u8 *ubuf = urb->transfer_buffer;
  295. u8 tbuf [sizeof (struct usb_hub_descriptor)];
  296. const u8 *bufp = tbuf;
  297. int len = 0;
  298. int patch_wakeup = 0;
  299. unsigned long flags;
  300. int status = 0;
  301. int n;
  302. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  303. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  304. wValue = le16_to_cpu (cmd->wValue);
  305. wIndex = le16_to_cpu (cmd->wIndex);
  306. wLength = le16_to_cpu (cmd->wLength);
  307. if (wLength > urb->transfer_buffer_length)
  308. goto error;
  309. urb->actual_length = 0;
  310. switch (typeReq) {
  311. /* DEVICE REQUESTS */
  312. case DeviceRequest | USB_REQ_GET_STATUS:
  313. tbuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP)
  314. | (1 << USB_DEVICE_SELF_POWERED);
  315. tbuf [1] = 0;
  316. len = 2;
  317. break;
  318. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  319. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  320. hcd->remote_wakeup = 0;
  321. else
  322. goto error;
  323. break;
  324. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  325. if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP)
  326. hcd->remote_wakeup = 1;
  327. else
  328. goto error;
  329. break;
  330. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  331. tbuf [0] = 1;
  332. len = 1;
  333. /* FALLTHROUGH */
  334. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  335. break;
  336. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  337. switch (wValue & 0xff00) {
  338. case USB_DT_DEVICE << 8:
  339. if (hcd->driver->flags & HCD_USB2)
  340. bufp = usb2_rh_dev_descriptor;
  341. else if (hcd->driver->flags & HCD_USB11)
  342. bufp = usb11_rh_dev_descriptor;
  343. else
  344. goto error;
  345. len = 18;
  346. break;
  347. case USB_DT_CONFIG << 8:
  348. if (hcd->driver->flags & HCD_USB2) {
  349. bufp = hs_rh_config_descriptor;
  350. len = sizeof hs_rh_config_descriptor;
  351. } else {
  352. bufp = fs_rh_config_descriptor;
  353. len = sizeof fs_rh_config_descriptor;
  354. }
  355. if (hcd->can_wakeup)
  356. patch_wakeup = 1;
  357. break;
  358. case USB_DT_STRING << 8:
  359. n = rh_string (wValue & 0xff, hcd, ubuf, wLength);
  360. if (n < 0)
  361. goto error;
  362. urb->actual_length = n;
  363. break;
  364. default:
  365. goto error;
  366. }
  367. break;
  368. case DeviceRequest | USB_REQ_GET_INTERFACE:
  369. tbuf [0] = 0;
  370. len = 1;
  371. /* FALLTHROUGH */
  372. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  373. break;
  374. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  375. // wValue == urb->dev->devaddr
  376. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  377. wValue);
  378. break;
  379. /* INTERFACE REQUESTS (no defined feature/status flags) */
  380. /* ENDPOINT REQUESTS */
  381. case EndpointRequest | USB_REQ_GET_STATUS:
  382. // ENDPOINT_HALT flag
  383. tbuf [0] = 0;
  384. tbuf [1] = 0;
  385. len = 2;
  386. /* FALLTHROUGH */
  387. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  388. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  389. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  390. break;
  391. /* CLASS REQUESTS (and errors) */
  392. default:
  393. /* non-generic request */
  394. switch (typeReq) {
  395. case GetHubStatus:
  396. case GetPortStatus:
  397. len = 4;
  398. break;
  399. case GetHubDescriptor:
  400. len = sizeof (struct usb_hub_descriptor);
  401. break;
  402. }
  403. status = hcd->driver->hub_control (hcd,
  404. typeReq, wValue, wIndex,
  405. tbuf, wLength);
  406. break;
  407. error:
  408. /* "protocol stall" on error */
  409. status = -EPIPE;
  410. }
  411. if (status) {
  412. len = 0;
  413. if (status != -EPIPE) {
  414. dev_dbg (hcd->self.controller,
  415. "CTRL: TypeReq=0x%x val=0x%x "
  416. "idx=0x%x len=%d ==> %d\n",
  417. typeReq, wValue, wIndex,
  418. wLength, status);
  419. }
  420. }
  421. if (len) {
  422. if (urb->transfer_buffer_length < len)
  423. len = urb->transfer_buffer_length;
  424. urb->actual_length = len;
  425. // always USB_DIR_IN, toward host
  426. memcpy (ubuf, bufp, len);
  427. /* report whether RH hardware supports remote wakeup */
  428. if (patch_wakeup &&
  429. len > offsetof (struct usb_config_descriptor,
  430. bmAttributes))
  431. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  432. |= USB_CONFIG_ATT_WAKEUP;
  433. }
  434. /* any errors get returned through the urb completion */
  435. local_irq_save (flags);
  436. spin_lock (&urb->lock);
  437. if (urb->status == -EINPROGRESS)
  438. urb->status = status;
  439. spin_unlock (&urb->lock);
  440. usb_hcd_giveback_urb (hcd, urb, NULL);
  441. local_irq_restore (flags);
  442. return 0;
  443. }
  444. /*-------------------------------------------------------------------------*/
  445. /*
  446. * Root Hub interrupt transfers are polled using a timer if the
  447. * driver requests it; otherwise the driver is responsible for
  448. * calling usb_hcd_poll_rh_status() when an event occurs.
  449. *
  450. * Completions are called in_interrupt(), but they may or may not
  451. * be in_irq().
  452. */
  453. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  454. {
  455. struct urb *urb;
  456. int length;
  457. unsigned long flags;
  458. char buffer[4]; /* Any root hubs with > 31 ports? */
  459. if (!hcd->uses_new_polling && !hcd->status_urb)
  460. return;
  461. length = hcd->driver->hub_status_data(hcd, buffer);
  462. if (length > 0) {
  463. /* try to complete the status urb */
  464. local_irq_save (flags);
  465. spin_lock(&hcd_root_hub_lock);
  466. urb = hcd->status_urb;
  467. if (urb) {
  468. spin_lock(&urb->lock);
  469. if (urb->status == -EINPROGRESS) {
  470. hcd->poll_pending = 0;
  471. hcd->status_urb = NULL;
  472. urb->status = 0;
  473. urb->hcpriv = NULL;
  474. urb->actual_length = length;
  475. memcpy(urb->transfer_buffer, buffer, length);
  476. } else /* urb has been unlinked */
  477. length = 0;
  478. spin_unlock(&urb->lock);
  479. } else
  480. length = 0;
  481. spin_unlock(&hcd_root_hub_lock);
  482. /* local irqs are always blocked in completions */
  483. if (length > 0)
  484. usb_hcd_giveback_urb (hcd, urb, NULL);
  485. else
  486. hcd->poll_pending = 1;
  487. local_irq_restore (flags);
  488. }
  489. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  490. * exceed that limit if HZ is 100. */
  491. if (hcd->uses_new_polling ? hcd->poll_rh :
  492. (length == 0 && hcd->status_urb != NULL))
  493. mod_timer (&hcd->rh_timer, jiffies + msecs_to_jiffies(250));
  494. }
  495. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  496. /* timer callback */
  497. static void rh_timer_func (unsigned long _hcd)
  498. {
  499. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  500. }
  501. /*-------------------------------------------------------------------------*/
  502. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  503. {
  504. int retval;
  505. unsigned long flags;
  506. int len = 1 + (urb->dev->maxchild / 8);
  507. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  508. if (urb->status != -EINPROGRESS) /* already unlinked */
  509. retval = urb->status;
  510. else if (hcd->status_urb || urb->transfer_buffer_length < len) {
  511. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  512. retval = -EINVAL;
  513. } else {
  514. hcd->status_urb = urb;
  515. urb->hcpriv = hcd; /* indicate it's queued */
  516. if (!hcd->uses_new_polling)
  517. mod_timer (&hcd->rh_timer, jiffies +
  518. msecs_to_jiffies(250));
  519. /* If a status change has already occurred, report it ASAP */
  520. else if (hcd->poll_pending)
  521. mod_timer (&hcd->rh_timer, jiffies);
  522. retval = 0;
  523. }
  524. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  525. return retval;
  526. }
  527. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  528. {
  529. if (usb_pipeint (urb->pipe))
  530. return rh_queue_status (hcd, urb);
  531. if (usb_pipecontrol (urb->pipe))
  532. return rh_call_control (hcd, urb);
  533. return -EINVAL;
  534. }
  535. /*-------------------------------------------------------------------------*/
  536. /* Asynchronous unlinks of root-hub control URBs are legal, but they
  537. * don't do anything. Status URB unlinks must be made in process context
  538. * with interrupts enabled.
  539. */
  540. static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  541. {
  542. if (usb_pipeendpoint(urb->pipe) == 0) { /* Control URB */
  543. if (in_interrupt())
  544. return 0; /* nothing to do */
  545. spin_lock_irq(&urb->lock); /* from usb_kill_urb */
  546. ++urb->reject;
  547. spin_unlock_irq(&urb->lock);
  548. wait_event(usb_kill_urb_queue,
  549. atomic_read(&urb->use_count) == 0);
  550. spin_lock_irq(&urb->lock);
  551. --urb->reject;
  552. spin_unlock_irq(&urb->lock);
  553. } else { /* Status URB */
  554. if (!hcd->uses_new_polling)
  555. del_timer_sync (&hcd->rh_timer);
  556. local_irq_disable ();
  557. spin_lock (&hcd_root_hub_lock);
  558. if (urb == hcd->status_urb) {
  559. hcd->status_urb = NULL;
  560. urb->hcpriv = NULL;
  561. } else
  562. urb = NULL; /* wasn't fully queued */
  563. spin_unlock (&hcd_root_hub_lock);
  564. if (urb)
  565. usb_hcd_giveback_urb (hcd, urb, NULL);
  566. local_irq_enable ();
  567. }
  568. return 0;
  569. }
  570. /*-------------------------------------------------------------------------*/
  571. /* exported only within usbcore */
  572. struct usb_bus *usb_bus_get(struct usb_bus *bus)
  573. {
  574. if (bus)
  575. kref_get(&bus->kref);
  576. return bus;
  577. }
  578. static void usb_host_release(struct kref *kref)
  579. {
  580. struct usb_bus *bus = container_of(kref, struct usb_bus, kref);
  581. if (bus->release)
  582. bus->release(bus);
  583. }
  584. /* exported only within usbcore */
  585. void usb_bus_put(struct usb_bus *bus)
  586. {
  587. if (bus)
  588. kref_put(&bus->kref, usb_host_release);
  589. }
  590. /*-------------------------------------------------------------------------*/
  591. static struct class *usb_host_class;
  592. int usb_host_init(void)
  593. {
  594. int retval = 0;
  595. usb_host_class = class_create(THIS_MODULE, "usb_host");
  596. if (IS_ERR(usb_host_class))
  597. retval = PTR_ERR(usb_host_class);
  598. return retval;
  599. }
  600. void usb_host_cleanup(void)
  601. {
  602. class_destroy(usb_host_class);
  603. }
  604. /**
  605. * usb_bus_init - shared initialization code
  606. * @bus: the bus structure being initialized
  607. *
  608. * This code is used to initialize a usb_bus structure, memory for which is
  609. * separately managed.
  610. */
  611. static void usb_bus_init (struct usb_bus *bus)
  612. {
  613. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  614. bus->devnum_next = 1;
  615. bus->root_hub = NULL;
  616. bus->hcpriv = NULL;
  617. bus->busnum = -1;
  618. bus->bandwidth_allocated = 0;
  619. bus->bandwidth_int_reqs = 0;
  620. bus->bandwidth_isoc_reqs = 0;
  621. INIT_LIST_HEAD (&bus->bus_list);
  622. kref_init(&bus->kref);
  623. }
  624. /**
  625. * usb_alloc_bus - creates a new USB host controller structure
  626. * @op: pointer to a struct usb_operations that this bus structure should use
  627. * Context: !in_interrupt()
  628. *
  629. * Creates a USB host controller bus structure with the specified
  630. * usb_operations and initializes all the necessary internal objects.
  631. *
  632. * If no memory is available, NULL is returned.
  633. *
  634. * The caller should call usb_put_bus() when it is finished with the structure.
  635. */
  636. struct usb_bus *usb_alloc_bus (struct usb_operations *op)
  637. {
  638. struct usb_bus *bus;
  639. bus = kzalloc (sizeof *bus, GFP_KERNEL);
  640. if (!bus)
  641. return NULL;
  642. usb_bus_init (bus);
  643. bus->op = op;
  644. return bus;
  645. }
  646. /*-------------------------------------------------------------------------*/
  647. /**
  648. * usb_register_bus - registers the USB host controller with the usb core
  649. * @bus: pointer to the bus to register
  650. * Context: !in_interrupt()
  651. *
  652. * Assigns a bus number, and links the controller into usbcore data
  653. * structures so that it can be seen by scanning the bus list.
  654. */
  655. static int usb_register_bus(struct usb_bus *bus)
  656. {
  657. int busnum;
  658. down (&usb_bus_list_lock);
  659. busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  660. if (busnum < USB_MAXBUS) {
  661. set_bit (busnum, busmap.busmap);
  662. bus->busnum = busnum;
  663. } else {
  664. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  665. up(&usb_bus_list_lock);
  666. return -E2BIG;
  667. }
  668. bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0),
  669. bus->controller, "usb_host%d", busnum);
  670. if (IS_ERR(bus->class_dev)) {
  671. clear_bit(busnum, busmap.busmap);
  672. up(&usb_bus_list_lock);
  673. return PTR_ERR(bus->class_dev);
  674. }
  675. class_set_devdata(bus->class_dev, bus);
  676. /* Add it to the local list of buses */
  677. list_add (&bus->bus_list, &usb_bus_list);
  678. up (&usb_bus_list_lock);
  679. usb_notify_add_bus(bus);
  680. dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
  681. return 0;
  682. }
  683. /**
  684. * usb_deregister_bus - deregisters the USB host controller
  685. * @bus: pointer to the bus to deregister
  686. * Context: !in_interrupt()
  687. *
  688. * Recycles the bus number, and unlinks the controller from usbcore data
  689. * structures so that it won't be seen by scanning the bus list.
  690. */
  691. static void usb_deregister_bus (struct usb_bus *bus)
  692. {
  693. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  694. /*
  695. * NOTE: make sure that all the devices are removed by the
  696. * controller code, as well as having it call this when cleaning
  697. * itself up
  698. */
  699. down (&usb_bus_list_lock);
  700. list_del (&bus->bus_list);
  701. up (&usb_bus_list_lock);
  702. usb_notify_remove_bus(bus);
  703. clear_bit (bus->busnum, busmap.busmap);
  704. class_device_unregister(bus->class_dev);
  705. }
  706. /**
  707. * register_root_hub - called by usb_add_hcd() to register a root hub
  708. * @usb_dev: the usb root hub device to be registered.
  709. * @hcd: host controller for this root hub
  710. *
  711. * This function registers the root hub with the USB subsystem. It sets up
  712. * the device properly in the device tree and stores the root_hub pointer
  713. * in the bus structure, then calls usb_new_device() to register the usb
  714. * device. It also assigns the root hub's USB address (always 1).
  715. */
  716. static int register_root_hub (struct usb_device *usb_dev,
  717. struct usb_hcd *hcd)
  718. {
  719. struct device *parent_dev = hcd->self.controller;
  720. const int devnum = 1;
  721. int retval;
  722. usb_dev->devnum = devnum;
  723. usb_dev->bus->devnum_next = devnum + 1;
  724. memset (&usb_dev->bus->devmap.devicemap, 0,
  725. sizeof usb_dev->bus->devmap.devicemap);
  726. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  727. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  728. down (&usb_bus_list_lock);
  729. usb_dev->bus->root_hub = usb_dev;
  730. usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  731. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  732. if (retval != sizeof usb_dev->descriptor) {
  733. usb_dev->bus->root_hub = NULL;
  734. up (&usb_bus_list_lock);
  735. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  736. usb_dev->dev.bus_id, retval);
  737. return (retval < 0) ? retval : -EMSGSIZE;
  738. }
  739. retval = usb_new_device (usb_dev);
  740. if (retval) {
  741. usb_dev->bus->root_hub = NULL;
  742. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  743. usb_dev->dev.bus_id, retval);
  744. }
  745. up (&usb_bus_list_lock);
  746. if (retval == 0) {
  747. spin_lock_irq (&hcd_root_hub_lock);
  748. hcd->rh_registered = 1;
  749. spin_unlock_irq (&hcd_root_hub_lock);
  750. /* Did the HC die before the root hub was registered? */
  751. if (hcd->state == HC_STATE_HALT)
  752. usb_hc_died (hcd); /* This time clean up */
  753. }
  754. return retval;
  755. }
  756. void usb_enable_root_hub_irq (struct usb_bus *bus)
  757. {
  758. struct usb_hcd *hcd;
  759. hcd = container_of (bus, struct usb_hcd, self);
  760. if (hcd->driver->hub_irq_enable && !hcd->poll_rh &&
  761. hcd->state != HC_STATE_HALT)
  762. hcd->driver->hub_irq_enable (hcd);
  763. }
  764. /*-------------------------------------------------------------------------*/
  765. /**
  766. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  767. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  768. * @is_input: true iff the transaction sends data to the host
  769. * @isoc: true for isochronous transactions, false for interrupt ones
  770. * @bytecount: how many bytes in the transaction.
  771. *
  772. * Returns approximate bus time in nanoseconds for a periodic transaction.
  773. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  774. * scheduled in software, this function is only used for such scheduling.
  775. */
  776. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  777. {
  778. unsigned long tmp;
  779. switch (speed) {
  780. case USB_SPEED_LOW: /* INTR only */
  781. if (is_input) {
  782. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  783. return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  784. } else {
  785. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  786. return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  787. }
  788. case USB_SPEED_FULL: /* ISOC or INTR */
  789. if (isoc) {
  790. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  791. return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
  792. } else {
  793. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  794. return (9107L + BW_HOST_DELAY + tmp);
  795. }
  796. case USB_SPEED_HIGH: /* ISOC or INTR */
  797. // FIXME adjust for input vs output
  798. if (isoc)
  799. tmp = HS_NSECS_ISO (bytecount);
  800. else
  801. tmp = HS_NSECS (bytecount);
  802. return tmp;
  803. default:
  804. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  805. return -1;
  806. }
  807. }
  808. EXPORT_SYMBOL (usb_calc_bus_time);
  809. /*
  810. * usb_check_bandwidth():
  811. *
  812. * old_alloc is from host_controller->bandwidth_allocated in microseconds;
  813. * bustime is from calc_bus_time(), but converted to microseconds.
  814. *
  815. * returns <bustime in us> if successful,
  816. * or -ENOSPC if bandwidth request fails.
  817. *
  818. * FIXME:
  819. * This initial implementation does not use Endpoint.bInterval
  820. * in managing bandwidth allocation.
  821. * It probably needs to be expanded to use Endpoint.bInterval.
  822. * This can be done as a later enhancement (correction).
  823. *
  824. * This will also probably require some kind of
  825. * frame allocation tracking...meaning, for example,
  826. * that if multiple drivers request interrupts every 10 USB frames,
  827. * they don't all have to be allocated at
  828. * frame numbers N, N+10, N+20, etc. Some of them could be at
  829. * N+11, N+21, N+31, etc., and others at
  830. * N+12, N+22, N+32, etc.
  831. *
  832. * Similarly for isochronous transfers...
  833. *
  834. * Individual HCDs can schedule more directly ... this logic
  835. * is not correct for high speed transfers.
  836. */
  837. int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
  838. {
  839. unsigned int pipe = urb->pipe;
  840. long bustime;
  841. int is_in = usb_pipein (pipe);
  842. int is_iso = usb_pipeisoc (pipe);
  843. int old_alloc = dev->bus->bandwidth_allocated;
  844. int new_alloc;
  845. bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
  846. usb_maxpacket (dev, pipe, !is_in)));
  847. if (is_iso)
  848. bustime /= urb->number_of_packets;
  849. new_alloc = old_alloc + (int) bustime;
  850. if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
  851. #ifdef DEBUG
  852. char *mode =
  853. #ifdef CONFIG_USB_BANDWIDTH
  854. "";
  855. #else
  856. "would have ";
  857. #endif
  858. dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
  859. mode, old_alloc, bustime, new_alloc);
  860. #endif
  861. #ifdef CONFIG_USB_BANDWIDTH
  862. bustime = -ENOSPC; /* report error */
  863. #endif
  864. }
  865. return bustime;
  866. }
  867. EXPORT_SYMBOL (usb_check_bandwidth);
  868. /**
  869. * usb_claim_bandwidth - records bandwidth for a periodic transfer
  870. * @dev: source/target of request
  871. * @urb: request (urb->dev == dev)
  872. * @bustime: bandwidth consumed, in (average) microseconds per frame
  873. * @isoc: true iff the request is isochronous
  874. *
  875. * Bus bandwidth reservations are recorded purely for diagnostic purposes.
  876. * HCDs are expected not to overcommit periodic bandwidth, and to record such
  877. * reservations whenever endpoints are added to the periodic schedule.
  878. *
  879. * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
  880. * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
  881. * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
  882. * large its periodic schedule is.
  883. */
  884. void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
  885. {
  886. dev->bus->bandwidth_allocated += bustime;
  887. if (isoc)
  888. dev->bus->bandwidth_isoc_reqs++;
  889. else
  890. dev->bus->bandwidth_int_reqs++;
  891. urb->bandwidth = bustime;
  892. #ifdef USB_BANDWIDTH_MESSAGES
  893. dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
  894. bustime,
  895. isoc ? "ISOC" : "INTR",
  896. dev->bus->bandwidth_allocated,
  897. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  898. #endif
  899. }
  900. EXPORT_SYMBOL (usb_claim_bandwidth);
  901. /**
  902. * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
  903. * @dev: source/target of request
  904. * @urb: request (urb->dev == dev)
  905. * @isoc: true iff the request is isochronous
  906. *
  907. * This records that previously allocated bandwidth has been released.
  908. * Bandwidth is released when endpoints are removed from the host controller's
  909. * periodic schedule.
  910. */
  911. void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
  912. {
  913. dev->bus->bandwidth_allocated -= urb->bandwidth;
  914. if (isoc)
  915. dev->bus->bandwidth_isoc_reqs--;
  916. else
  917. dev->bus->bandwidth_int_reqs--;
  918. #ifdef USB_BANDWIDTH_MESSAGES
  919. dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
  920. urb->bandwidth,
  921. isoc ? "ISOC" : "INTR",
  922. dev->bus->bandwidth_allocated,
  923. dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
  924. #endif
  925. urb->bandwidth = 0;
  926. }
  927. EXPORT_SYMBOL (usb_release_bandwidth);
  928. /*-------------------------------------------------------------------------*/
  929. /*
  930. * Generic HC operations.
  931. */
  932. /*-------------------------------------------------------------------------*/
  933. static void urb_unlink (struct urb *urb)
  934. {
  935. unsigned long flags;
  936. /* Release any periodic transfer bandwidth */
  937. if (urb->bandwidth)
  938. usb_release_bandwidth (urb->dev, urb,
  939. usb_pipeisoc (urb->pipe));
  940. /* clear all state linking urb to this dev (and hcd) */
  941. spin_lock_irqsave (&hcd_data_lock, flags);
  942. list_del_init (&urb->urb_list);
  943. spin_unlock_irqrestore (&hcd_data_lock, flags);
  944. usb_put_dev (urb->dev);
  945. }
  946. /* may be called in any context with a valid urb->dev usecount
  947. * caller surrenders "ownership" of urb
  948. * expects usb_submit_urb() to have sanity checked and conditioned all
  949. * inputs in the urb
  950. */
  951. static int hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  952. {
  953. int status;
  954. struct usb_hcd *hcd = urb->dev->bus->hcpriv;
  955. struct usb_host_endpoint *ep;
  956. unsigned long flags;
  957. if (!hcd)
  958. return -ENODEV;
  959. usbmon_urb_submit(&hcd->self, urb);
  960. /*
  961. * Atomically queue the urb, first to our records, then to the HCD.
  962. * Access to urb->status is controlled by urb->lock ... changes on
  963. * i/o completion (normal or fault) or unlinking.
  964. */
  965. // FIXME: verify that quiescing hc works right (RH cleans up)
  966. spin_lock_irqsave (&hcd_data_lock, flags);
  967. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  968. [usb_pipeendpoint(urb->pipe)];
  969. if (unlikely (!ep))
  970. status = -ENOENT;
  971. else if (unlikely (urb->reject))
  972. status = -EPERM;
  973. else switch (hcd->state) {
  974. case HC_STATE_RUNNING:
  975. case HC_STATE_RESUMING:
  976. doit:
  977. usb_get_dev (urb->dev);
  978. list_add_tail (&urb->urb_list, &ep->urb_list);
  979. status = 0;
  980. break;
  981. case HC_STATE_SUSPENDED:
  982. /* HC upstream links (register access, wakeup signaling) can work
  983. * even when the downstream links (and DMA etc) are quiesced; let
  984. * usbcore talk to the root hub.
  985. */
  986. if (hcd->self.controller->power.power_state.event == PM_EVENT_ON
  987. && urb->dev->parent == NULL)
  988. goto doit;
  989. /* FALL THROUGH */
  990. default:
  991. status = -ESHUTDOWN;
  992. break;
  993. }
  994. spin_unlock_irqrestore (&hcd_data_lock, flags);
  995. if (status) {
  996. INIT_LIST_HEAD (&urb->urb_list);
  997. usbmon_urb_submit_error(&hcd->self, urb, status);
  998. return status;
  999. }
  1000. /* increment urb's reference count as part of giving it to the HCD
  1001. * (which now controls it). HCD guarantees that it either returns
  1002. * an error or calls giveback(), but not both.
  1003. */
  1004. urb = usb_get_urb (urb);
  1005. atomic_inc (&urb->use_count);
  1006. if (urb->dev == hcd->self.root_hub) {
  1007. /* NOTE: requirement on hub callers (usbfs and the hub
  1008. * driver, for now) that URBs' urb->transfer_buffer be
  1009. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1010. * they could clobber root hub response data.
  1011. */
  1012. status = rh_urb_enqueue (hcd, urb);
  1013. goto done;
  1014. }
  1015. /* lower level hcd code should use *_dma exclusively,
  1016. * unless it uses pio or talks to another transport.
  1017. */
  1018. if (hcd->self.controller->dma_mask) {
  1019. if (usb_pipecontrol (urb->pipe)
  1020. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1021. urb->setup_dma = dma_map_single (
  1022. hcd->self.controller,
  1023. urb->setup_packet,
  1024. sizeof (struct usb_ctrlrequest),
  1025. DMA_TO_DEVICE);
  1026. if (urb->transfer_buffer_length != 0
  1027. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1028. urb->transfer_dma = dma_map_single (
  1029. hcd->self.controller,
  1030. urb->transfer_buffer,
  1031. urb->transfer_buffer_length,
  1032. usb_pipein (urb->pipe)
  1033. ? DMA_FROM_DEVICE
  1034. : DMA_TO_DEVICE);
  1035. }
  1036. status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags);
  1037. done:
  1038. if (unlikely (status)) {
  1039. urb_unlink (urb);
  1040. atomic_dec (&urb->use_count);
  1041. if (urb->reject)
  1042. wake_up (&usb_kill_urb_queue);
  1043. usb_put_urb (urb);
  1044. usbmon_urb_submit_error(&hcd->self, urb, status);
  1045. }
  1046. return status;
  1047. }
  1048. /*-------------------------------------------------------------------------*/
  1049. /* called in any context */
  1050. static int hcd_get_frame_number (struct usb_device *udev)
  1051. {
  1052. struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
  1053. if (!HC_IS_RUNNING (hcd->state))
  1054. return -ESHUTDOWN;
  1055. return hcd->driver->get_frame_number (hcd);
  1056. }
  1057. /*-------------------------------------------------------------------------*/
  1058. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1059. * off hardware queues (which may take a while) and returning it as
  1060. * soon as practical. we've already set up the urb's return status,
  1061. * but we can't know if the callback completed already.
  1062. */
  1063. static int
  1064. unlink1 (struct usb_hcd *hcd, struct urb *urb)
  1065. {
  1066. int value;
  1067. if (urb->dev == hcd->self.root_hub)
  1068. value = usb_rh_urb_dequeue (hcd, urb);
  1069. else {
  1070. /* The only reason an HCD might fail this call is if
  1071. * it has not yet fully queued the urb to begin with.
  1072. * Such failures should be harmless. */
  1073. value = hcd->driver->urb_dequeue (hcd, urb);
  1074. }
  1075. if (value != 0)
  1076. dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
  1077. urb, value);
  1078. return value;
  1079. }
  1080. /*
  1081. * called in any context
  1082. *
  1083. * caller guarantees urb won't be recycled till both unlink()
  1084. * and the urb's completion function return
  1085. */
  1086. static int hcd_unlink_urb (struct urb *urb, int status)
  1087. {
  1088. struct usb_host_endpoint *ep;
  1089. struct usb_hcd *hcd = NULL;
  1090. struct device *sys = NULL;
  1091. unsigned long flags;
  1092. struct list_head *tmp;
  1093. int retval;
  1094. if (!urb)
  1095. return -EINVAL;
  1096. if (!urb->dev || !urb->dev->bus)
  1097. return -ENODEV;
  1098. ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
  1099. [usb_pipeendpoint(urb->pipe)];
  1100. if (!ep)
  1101. return -ENODEV;
  1102. /*
  1103. * we contend for urb->status with the hcd core,
  1104. * which changes it while returning the urb.
  1105. *
  1106. * Caller guaranteed that the urb pointer hasn't been freed, and
  1107. * that it was submitted. But as a rule it can't know whether or
  1108. * not it's already been unlinked ... so we respect the reversed
  1109. * lock sequence needed for the usb_hcd_giveback_urb() code paths
  1110. * (urb lock, then hcd_data_lock) in case some other CPU is now
  1111. * unlinking it.
  1112. */
  1113. spin_lock_irqsave (&urb->lock, flags);
  1114. spin_lock (&hcd_data_lock);
  1115. sys = &urb->dev->dev;
  1116. hcd = urb->dev->bus->hcpriv;
  1117. if (hcd == NULL) {
  1118. retval = -ENODEV;
  1119. goto done;
  1120. }
  1121. /* insist the urb is still queued */
  1122. list_for_each(tmp, &ep->urb_list) {
  1123. if (tmp == &urb->urb_list)
  1124. break;
  1125. }
  1126. if (tmp != &urb->urb_list) {
  1127. retval = -EIDRM;
  1128. goto done;
  1129. }
  1130. /* Any status except -EINPROGRESS means something already started to
  1131. * unlink this URB from the hardware. So there's no more work to do.
  1132. */
  1133. if (urb->status != -EINPROGRESS) {
  1134. retval = -EBUSY;
  1135. goto done;
  1136. }
  1137. /* IRQ setup can easily be broken so that USB controllers
  1138. * never get completion IRQs ... maybe even the ones we need to
  1139. * finish unlinking the initial failed usb_set_address()
  1140. * or device descriptor fetch.
  1141. */
  1142. if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags)
  1143. && hcd->self.root_hub != urb->dev) {
  1144. dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
  1145. "Controller is probably using the wrong IRQ."
  1146. "\n");
  1147. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1148. }
  1149. urb->status = status;
  1150. spin_unlock (&hcd_data_lock);
  1151. spin_unlock_irqrestore (&urb->lock, flags);
  1152. retval = unlink1 (hcd, urb);
  1153. if (retval == 0)
  1154. retval = -EINPROGRESS;
  1155. return retval;
  1156. done:
  1157. spin_unlock (&hcd_data_lock);
  1158. spin_unlock_irqrestore (&urb->lock, flags);
  1159. if (retval != -EIDRM && sys && sys->driver)
  1160. dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
  1161. return retval;
  1162. }
  1163. /*-------------------------------------------------------------------------*/
  1164. /* disables the endpoint: cancels any pending urbs, then synchronizes with
  1165. * the hcd to make sure all endpoint state is gone from hardware. use for
  1166. * set_configuration, set_interface, driver removal, physical disconnect.
  1167. *
  1168. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1169. * type, maxpacket size, toggle, halt status, and scheduling.
  1170. */
  1171. static void
  1172. hcd_endpoint_disable (struct usb_device *udev, struct usb_host_endpoint *ep)
  1173. {
  1174. struct usb_hcd *hcd;
  1175. struct urb *urb;
  1176. hcd = udev->bus->hcpriv;
  1177. WARN_ON (!HC_IS_RUNNING (hcd->state) && hcd->state != HC_STATE_HALT &&
  1178. udev->state != USB_STATE_NOTATTACHED);
  1179. local_irq_disable ();
  1180. /* FIXME move most of this into message.c as part of its
  1181. * endpoint disable logic
  1182. */
  1183. /* ep is already gone from udev->ep_{in,out}[]; no more submits */
  1184. rescan:
  1185. spin_lock (&hcd_data_lock);
  1186. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1187. int tmp;
  1188. /* another cpu may be in hcd, spinning on hcd_data_lock
  1189. * to giveback() this urb. the races here should be
  1190. * small, but a full fix needs a new "can't submit"
  1191. * urb state.
  1192. * FIXME urb->reject should allow that...
  1193. */
  1194. if (urb->status != -EINPROGRESS)
  1195. continue;
  1196. usb_get_urb (urb);
  1197. spin_unlock (&hcd_data_lock);
  1198. spin_lock (&urb->lock);
  1199. tmp = urb->status;
  1200. if (tmp == -EINPROGRESS)
  1201. urb->status = -ESHUTDOWN;
  1202. spin_unlock (&urb->lock);
  1203. /* kick hcd unless it's already returning this */
  1204. if (tmp == -EINPROGRESS) {
  1205. tmp = urb->pipe;
  1206. unlink1 (hcd, urb);
  1207. dev_dbg (hcd->self.controller,
  1208. "shutdown urb %p pipe %08x ep%d%s%s\n",
  1209. urb, tmp, usb_pipeendpoint (tmp),
  1210. (tmp & USB_DIR_IN) ? "in" : "out",
  1211. ({ char *s; \
  1212. switch (usb_pipetype (tmp)) { \
  1213. case PIPE_CONTROL: s = ""; break; \
  1214. case PIPE_BULK: s = "-bulk"; break; \
  1215. case PIPE_INTERRUPT: s = "-intr"; break; \
  1216. default: s = "-iso"; break; \
  1217. }; s;}));
  1218. }
  1219. usb_put_urb (urb);
  1220. /* list contents may have changed */
  1221. goto rescan;
  1222. }
  1223. spin_unlock (&hcd_data_lock);
  1224. local_irq_enable ();
  1225. /* synchronize with the hardware, so old configuration state
  1226. * clears out immediately (and will be freed).
  1227. */
  1228. might_sleep ();
  1229. if (hcd->driver->endpoint_disable)
  1230. hcd->driver->endpoint_disable (hcd, ep);
  1231. }
  1232. /*-------------------------------------------------------------------------*/
  1233. #ifdef CONFIG_PM
  1234. int hcd_bus_suspend (struct usb_bus *bus)
  1235. {
  1236. struct usb_hcd *hcd;
  1237. int status;
  1238. hcd = container_of (bus, struct usb_hcd, self);
  1239. if (!hcd->driver->bus_suspend)
  1240. return -ENOENT;
  1241. hcd->state = HC_STATE_QUIESCING;
  1242. status = hcd->driver->bus_suspend (hcd);
  1243. if (status == 0)
  1244. hcd->state = HC_STATE_SUSPENDED;
  1245. else
  1246. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1247. "suspend", status);
  1248. return status;
  1249. }
  1250. int hcd_bus_resume (struct usb_bus *bus)
  1251. {
  1252. struct usb_hcd *hcd;
  1253. int status;
  1254. hcd = container_of (bus, struct usb_hcd, self);
  1255. if (!hcd->driver->bus_resume)
  1256. return -ENOENT;
  1257. if (hcd->state == HC_STATE_RUNNING)
  1258. return 0;
  1259. hcd->state = HC_STATE_RESUMING;
  1260. status = hcd->driver->bus_resume (hcd);
  1261. if (status == 0)
  1262. hcd->state = HC_STATE_RUNNING;
  1263. else {
  1264. dev_dbg(&bus->root_hub->dev, "%s fail, err %d\n",
  1265. "resume", status);
  1266. usb_hc_died(hcd);
  1267. }
  1268. return status;
  1269. }
  1270. /*
  1271. * usb_hcd_suspend_root_hub - HCD autosuspends downstream ports
  1272. * @hcd: host controller for this root hub
  1273. *
  1274. * This call arranges that usb_hcd_resume_root_hub() is safe to call later;
  1275. * that the HCD's root hub polling is deactivated; and that the root's hub
  1276. * driver is suspended. HCDs may call this to autosuspend when their root
  1277. * hub's downstream ports are all inactive: unpowered, disconnected,
  1278. * disabled, or suspended.
  1279. *
  1280. * The HCD will autoresume on device connect change detection (using SRP
  1281. * or a D+/D- pullup). The HCD also autoresumes on remote wakeup signaling
  1282. * from any ports that are suspended (if that is enabled). In most cases,
  1283. * overcurrent signaling (on powered ports) will also start autoresume.
  1284. *
  1285. * Always called with IRQs blocked.
  1286. */
  1287. void usb_hcd_suspend_root_hub (struct usb_hcd *hcd)
  1288. {
  1289. struct urb *urb;
  1290. spin_lock (&hcd_root_hub_lock);
  1291. usb_suspend_root_hub (hcd->self.root_hub);
  1292. /* force status urb to complete/unlink while suspended */
  1293. if (hcd->status_urb) {
  1294. urb = hcd->status_urb;
  1295. urb->status = -ECONNRESET;
  1296. urb->hcpriv = NULL;
  1297. urb->actual_length = 0;
  1298. del_timer (&hcd->rh_timer);
  1299. hcd->poll_pending = 0;
  1300. hcd->status_urb = NULL;
  1301. } else
  1302. urb = NULL;
  1303. spin_unlock (&hcd_root_hub_lock);
  1304. hcd->state = HC_STATE_SUSPENDED;
  1305. if (urb)
  1306. usb_hcd_giveback_urb (hcd, urb, NULL);
  1307. }
  1308. EXPORT_SYMBOL_GPL(usb_hcd_suspend_root_hub);
  1309. /**
  1310. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1311. * @hcd: host controller for this root hub
  1312. *
  1313. * The USB host controller calls this function when its root hub is
  1314. * suspended (with the remote wakeup feature enabled) and a remote
  1315. * wakeup request is received. It queues a request for khubd to
  1316. * resume the root hub (that is, manage its downstream ports again).
  1317. */
  1318. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1319. {
  1320. unsigned long flags;
  1321. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1322. if (hcd->rh_registered)
  1323. usb_resume_root_hub (hcd->self.root_hub);
  1324. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1325. }
  1326. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1327. #endif
  1328. /*-------------------------------------------------------------------------*/
  1329. #ifdef CONFIG_USB_OTG
  1330. /**
  1331. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1332. * @bus: the bus (must use hcd framework)
  1333. * @port_num: 1-based number of port; usually bus->otg_port
  1334. * Context: in_interrupt()
  1335. *
  1336. * Starts enumeration, with an immediate reset followed later by
  1337. * khubd identifying and possibly configuring the device.
  1338. * This is needed by OTG controller drivers, where it helps meet
  1339. * HNP protocol timing requirements for starting a port reset.
  1340. */
  1341. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1342. {
  1343. struct usb_hcd *hcd;
  1344. int status = -EOPNOTSUPP;
  1345. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1346. * boards with root hubs hooked up to internal devices (instead of
  1347. * just the OTG port) may need more attention to resetting...
  1348. */
  1349. hcd = container_of (bus, struct usb_hcd, self);
  1350. if (port_num && hcd->driver->start_port_reset)
  1351. status = hcd->driver->start_port_reset(hcd, port_num);
  1352. /* run khubd shortly after (first) root port reset finishes;
  1353. * it may issue others, until at least 50 msecs have passed.
  1354. */
  1355. if (status == 0)
  1356. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1357. return status;
  1358. }
  1359. EXPORT_SYMBOL (usb_bus_start_enum);
  1360. #endif
  1361. /*-------------------------------------------------------------------------*/
  1362. /*
  1363. * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue)
  1364. */
  1365. static struct usb_operations usb_hcd_operations = {
  1366. .get_frame_number = hcd_get_frame_number,
  1367. .submit_urb = hcd_submit_urb,
  1368. .unlink_urb = hcd_unlink_urb,
  1369. .buffer_alloc = hcd_buffer_alloc,
  1370. .buffer_free = hcd_buffer_free,
  1371. .disable = hcd_endpoint_disable,
  1372. };
  1373. /*-------------------------------------------------------------------------*/
  1374. /**
  1375. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1376. * @hcd: host controller returning the URB
  1377. * @urb: urb being returned to the USB device driver.
  1378. * @regs: pt_regs, passed down to the URB completion handler
  1379. * Context: in_interrupt()
  1380. *
  1381. * This hands the URB from HCD to its USB device driver, using its
  1382. * completion function. The HCD has freed all per-urb resources
  1383. * (and is done using urb->hcpriv). It also released all HCD locks;
  1384. * the device driver won't cause problems if it frees, modifies,
  1385. * or resubmits this URB.
  1386. */
  1387. void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
  1388. {
  1389. int at_root_hub;
  1390. at_root_hub = (urb->dev == hcd->self.root_hub);
  1391. urb_unlink (urb);
  1392. /* lower level hcd code should use *_dma exclusively */
  1393. if (hcd->self.controller->dma_mask && !at_root_hub) {
  1394. if (usb_pipecontrol (urb->pipe)
  1395. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1396. dma_unmap_single (hcd->self.controller, urb->setup_dma,
  1397. sizeof (struct usb_ctrlrequest),
  1398. DMA_TO_DEVICE);
  1399. if (urb->transfer_buffer_length != 0
  1400. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  1401. dma_unmap_single (hcd->self.controller,
  1402. urb->transfer_dma,
  1403. urb->transfer_buffer_length,
  1404. usb_pipein (urb->pipe)
  1405. ? DMA_FROM_DEVICE
  1406. : DMA_TO_DEVICE);
  1407. }
  1408. usbmon_urb_complete (&hcd->self, urb);
  1409. /* pass ownership to the completion handler */
  1410. urb->complete (urb, regs);
  1411. atomic_dec (&urb->use_count);
  1412. if (unlikely (urb->reject))
  1413. wake_up (&usb_kill_urb_queue);
  1414. usb_put_urb (urb);
  1415. }
  1416. EXPORT_SYMBOL (usb_hcd_giveback_urb);
  1417. /*-------------------------------------------------------------------------*/
  1418. /**
  1419. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1420. * @irq: the IRQ being raised
  1421. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1422. * @r: saved hardware registers
  1423. *
  1424. * If the controller isn't HALTed, calls the driver's irq handler.
  1425. * Checks whether the controller is now dead.
  1426. */
  1427. irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
  1428. {
  1429. struct usb_hcd *hcd = __hcd;
  1430. int start = hcd->state;
  1431. if (unlikely(start == HC_STATE_HALT ||
  1432. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
  1433. return IRQ_NONE;
  1434. if (hcd->driver->irq (hcd, r) == IRQ_NONE)
  1435. return IRQ_NONE;
  1436. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1437. if (unlikely(hcd->state == HC_STATE_HALT))
  1438. usb_hc_died (hcd);
  1439. return IRQ_HANDLED;
  1440. }
  1441. /*-------------------------------------------------------------------------*/
  1442. /**
  1443. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1444. * @hcd: pointer to the HCD representing the controller
  1445. *
  1446. * This is called by bus glue to report a USB host controller that died
  1447. * while operations may still have been pending. It's called automatically
  1448. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1449. */
  1450. void usb_hc_died (struct usb_hcd *hcd)
  1451. {
  1452. unsigned long flags;
  1453. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1454. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1455. if (hcd->rh_registered) {
  1456. hcd->poll_rh = 0;
  1457. /* make khubd clean up old urbs and devices */
  1458. usb_set_device_state (hcd->self.root_hub,
  1459. USB_STATE_NOTATTACHED);
  1460. usb_kick_khubd (hcd->self.root_hub);
  1461. }
  1462. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1463. }
  1464. EXPORT_SYMBOL_GPL (usb_hc_died);
  1465. /*-------------------------------------------------------------------------*/
  1466. static void hcd_release (struct usb_bus *bus)
  1467. {
  1468. struct usb_hcd *hcd;
  1469. hcd = container_of(bus, struct usb_hcd, self);
  1470. kfree(hcd);
  1471. }
  1472. /**
  1473. * usb_create_hcd - create and initialize an HCD structure
  1474. * @driver: HC driver that will use this hcd
  1475. * @dev: device for this HC, stored in hcd->self.controller
  1476. * @bus_name: value to store in hcd->self.bus_name
  1477. * Context: !in_interrupt()
  1478. *
  1479. * Allocate a struct usb_hcd, with extra space at the end for the
  1480. * HC driver's private data. Initialize the generic members of the
  1481. * hcd structure.
  1482. *
  1483. * If memory is unavailable, returns NULL.
  1484. */
  1485. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1486. struct device *dev, char *bus_name)
  1487. {
  1488. struct usb_hcd *hcd;
  1489. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1490. if (!hcd) {
  1491. dev_dbg (dev, "hcd alloc failed\n");
  1492. return NULL;
  1493. }
  1494. dev_set_drvdata(dev, hcd);
  1495. usb_bus_init(&hcd->self);
  1496. hcd->self.op = &usb_hcd_operations;
  1497. hcd->self.hcpriv = hcd;
  1498. hcd->self.release = &hcd_release;
  1499. hcd->self.controller = dev;
  1500. hcd->self.bus_name = bus_name;
  1501. init_timer(&hcd->rh_timer);
  1502. hcd->rh_timer.function = rh_timer_func;
  1503. hcd->rh_timer.data = (unsigned long) hcd;
  1504. hcd->driver = driver;
  1505. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1506. "USB Host Controller";
  1507. return hcd;
  1508. }
  1509. EXPORT_SYMBOL (usb_create_hcd);
  1510. void usb_put_hcd (struct usb_hcd *hcd)
  1511. {
  1512. dev_set_drvdata(hcd->self.controller, NULL);
  1513. usb_bus_put(&hcd->self);
  1514. }
  1515. EXPORT_SYMBOL (usb_put_hcd);
  1516. /**
  1517. * usb_add_hcd - finish generic HCD structure initialization and register
  1518. * @hcd: the usb_hcd structure to initialize
  1519. * @irqnum: Interrupt line to allocate
  1520. * @irqflags: Interrupt type flags
  1521. *
  1522. * Finish the remaining parts of generic HCD initialization: allocate the
  1523. * buffers of consistent memory, register the bus, request the IRQ line,
  1524. * and call the driver's reset() and start() routines.
  1525. */
  1526. int usb_add_hcd(struct usb_hcd *hcd,
  1527. unsigned int irqnum, unsigned long irqflags)
  1528. {
  1529. int retval;
  1530. struct usb_device *rhdev;
  1531. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1532. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1533. /* till now HC has been in an indeterminate state ... */
  1534. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1535. dev_err(hcd->self.controller, "can't reset\n");
  1536. return retval;
  1537. }
  1538. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1539. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1540. return retval;
  1541. }
  1542. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1543. goto err_register_bus;
  1544. if (hcd->driver->irq) {
  1545. char buf[8], *bufp = buf;
  1546. #ifdef __sparc__
  1547. bufp = __irq_itoa(irqnum);
  1548. #else
  1549. sprintf(buf, "%d", irqnum);
  1550. #endif
  1551. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1552. hcd->driver->description, hcd->self.busnum);
  1553. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1554. hcd->irq_descr, hcd)) != 0) {
  1555. dev_err(hcd->self.controller,
  1556. "request interrupt %s failed\n", bufp);
  1557. goto err_request_irq;
  1558. }
  1559. hcd->irq = irqnum;
  1560. dev_info(hcd->self.controller, "irq %s, %s 0x%08llx\n", bufp,
  1561. (hcd->driver->flags & HCD_MEMORY) ?
  1562. "io mem" : "io base",
  1563. (unsigned long long)hcd->rsrc_start);
  1564. } else {
  1565. hcd->irq = -1;
  1566. if (hcd->rsrc_start)
  1567. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1568. (hcd->driver->flags & HCD_MEMORY) ?
  1569. "io mem" : "io base",
  1570. (unsigned long long)hcd->rsrc_start);
  1571. }
  1572. /* Allocate the root hub before calling hcd->driver->start(),
  1573. * but don't register it until afterward so that the hardware
  1574. * is running.
  1575. */
  1576. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1577. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1578. retval = -ENOMEM;
  1579. goto err_allocate_root_hub;
  1580. }
  1581. /* Although in principle hcd->driver->start() might need to use rhdev,
  1582. * none of the current drivers do.
  1583. */
  1584. if ((retval = hcd->driver->start(hcd)) < 0) {
  1585. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1586. goto err_hcd_driver_start;
  1587. }
  1588. /* hcd->driver->start() reported can_wakeup, probably with
  1589. * assistance from board's boot firmware.
  1590. * NOTE: normal devices won't enable wakeup by default.
  1591. */
  1592. if (hcd->can_wakeup)
  1593. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1594. hcd->remote_wakeup = hcd->can_wakeup;
  1595. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1596. USB_SPEED_FULL;
  1597. rhdev->bus_mA = min(500u, hcd->power_budget);
  1598. if ((retval = register_root_hub(rhdev, hcd)) != 0)
  1599. goto err_register_root_hub;
  1600. if (hcd->uses_new_polling && hcd->poll_rh)
  1601. usb_hcd_poll_rh_status(hcd);
  1602. return retval;
  1603. err_register_root_hub:
  1604. hcd->driver->stop(hcd);
  1605. err_hcd_driver_start:
  1606. usb_put_dev(rhdev);
  1607. err_allocate_root_hub:
  1608. if (hcd->irq >= 0)
  1609. free_irq(irqnum, hcd);
  1610. err_request_irq:
  1611. usb_deregister_bus(&hcd->self);
  1612. err_register_bus:
  1613. hcd_buffer_destroy(hcd);
  1614. return retval;
  1615. }
  1616. EXPORT_SYMBOL (usb_add_hcd);
  1617. /**
  1618. * usb_remove_hcd - shutdown processing for generic HCDs
  1619. * @hcd: the usb_hcd structure to remove
  1620. * Context: !in_interrupt()
  1621. *
  1622. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1623. * invoking the HCD's stop() method.
  1624. */
  1625. void usb_remove_hcd(struct usb_hcd *hcd)
  1626. {
  1627. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1628. if (HC_IS_RUNNING (hcd->state))
  1629. hcd->state = HC_STATE_QUIESCING;
  1630. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1631. spin_lock_irq (&hcd_root_hub_lock);
  1632. hcd->rh_registered = 0;
  1633. spin_unlock_irq (&hcd_root_hub_lock);
  1634. down(&usb_bus_list_lock);
  1635. usb_disconnect(&hcd->self.root_hub);
  1636. up(&usb_bus_list_lock);
  1637. hcd->poll_rh = 0;
  1638. del_timer_sync(&hcd->rh_timer);
  1639. hcd->driver->stop(hcd);
  1640. hcd->state = HC_STATE_HALT;
  1641. if (hcd->irq >= 0)
  1642. free_irq(hcd->irq, hcd);
  1643. usb_deregister_bus(&hcd->self);
  1644. hcd_buffer_destroy(hcd);
  1645. }
  1646. EXPORT_SYMBOL (usb_remove_hcd);
  1647. /*-------------------------------------------------------------------------*/
  1648. #if defined(CONFIG_USB_MON)
  1649. struct usb_mon_operations *mon_ops;
  1650. /*
  1651. * The registration is unlocked.
  1652. * We do it this way because we do not want to lock in hot paths.
  1653. *
  1654. * Notice that the code is minimally error-proof. Because usbmon needs
  1655. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1656. */
  1657. int usb_mon_register (struct usb_mon_operations *ops)
  1658. {
  1659. if (mon_ops)
  1660. return -EBUSY;
  1661. mon_ops = ops;
  1662. mb();
  1663. return 0;
  1664. }
  1665. EXPORT_SYMBOL_GPL (usb_mon_register);
  1666. void usb_mon_deregister (void)
  1667. {
  1668. if (mon_ops == NULL) {
  1669. printk(KERN_ERR "USB: monitor was not registered\n");
  1670. return;
  1671. }
  1672. mon_ops = NULL;
  1673. mb();
  1674. }
  1675. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1676. #endif /* CONFIG_USB_MON */