hcd.c 59 KB

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