hcd.c 53 KB

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