hcd.c 53 KB

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