hcd.c 50 KB

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