hcd.c 53 KB

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