hcd.c 55 KB

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