hcd.c 55 KB

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