wa-rpipe.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * WUSB Wire Adapter
  4. * rpipe management
  5. *
  6. * Copyright (C) 2005-2006 Intel Corporation
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * FIXME: docs
  10. *
  11. * RPIPE
  12. *
  13. * Targeted at different downstream endpoints
  14. *
  15. * Descriptor: use to config the remote pipe.
  16. *
  17. * The number of blocks could be dynamic (wBlocks in descriptor is
  18. * 0)--need to schedule them then.
  19. *
  20. * Each bit in wa->rpipe_bm represents if an rpipe is being used or
  21. * not. Rpipes are represented with a 'struct wa_rpipe' that is
  22. * attached to the hcpriv member of a 'struct usb_host_endpoint'.
  23. *
  24. * When you need to xfer data to an endpoint, you get an rpipe for it
  25. * with wa_ep_rpipe_get(), which gives you a reference to the rpipe
  26. * and keeps a single one (the first one) with the endpoint. When you
  27. * are done transferring, you drop that reference. At the end the
  28. * rpipe is always allocated and bound to the endpoint. There it might
  29. * be recycled when not used.
  30. *
  31. * Addresses:
  32. *
  33. * We use a 1:1 mapping mechanism between port address (0 based
  34. * index, actually) and the address. The USB stack knows about this.
  35. *
  36. * USB Stack port number 4 (1 based)
  37. * WUSB code port index 3 (0 based)
  38. * USB Address 5 (2 based -- 0 is for default, 1 for root hub)
  39. *
  40. * Now, because we don't use the concept as default address exactly
  41. * like the (wired) USB code does, we need to kind of skip it. So we
  42. * never take addresses from the urb->pipe, but from the
  43. * urb->dev->devnum, to make sure that we always have the right
  44. * destination address.
  45. */
  46. #include <linux/atomic.h>
  47. #include <linux/bitmap.h>
  48. #include <linux/slab.h>
  49. #include <linux/export.h>
  50. #include "wusbhc.h"
  51. #include "wa-hc.h"
  52. static int __rpipe_get_descr(struct wahc *wa,
  53. struct usb_rpipe_descriptor *descr, u16 index)
  54. {
  55. ssize_t result;
  56. struct device *dev = &wa->usb_iface->dev;
  57. /* Get the RPIPE descriptor -- we cannot use the usb_get_descriptor()
  58. * function because the arguments are different.
  59. */
  60. result = usb_control_msg(
  61. wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
  62. USB_REQ_GET_DESCRIPTOR,
  63. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_RPIPE,
  64. USB_DT_RPIPE<<8, index, descr, sizeof(*descr),
  65. USB_CTRL_GET_TIMEOUT);
  66. if (result < 0) {
  67. dev_err(dev, "rpipe %u: get descriptor failed: %d\n",
  68. index, (int)result);
  69. goto error;
  70. }
  71. if (result < sizeof(*descr)) {
  72. dev_err(dev, "rpipe %u: got short descriptor "
  73. "(%zd vs %zd bytes needed)\n",
  74. index, result, sizeof(*descr));
  75. result = -EINVAL;
  76. goto error;
  77. }
  78. result = 0;
  79. error:
  80. return result;
  81. }
  82. /*
  83. *
  84. * The descriptor is assumed to be properly initialized (ie: you got
  85. * it through __rpipe_get_descr()).
  86. */
  87. static int __rpipe_set_descr(struct wahc *wa,
  88. struct usb_rpipe_descriptor *descr, u16 index)
  89. {
  90. ssize_t result;
  91. struct device *dev = &wa->usb_iface->dev;
  92. /* we cannot use the usb_get_descriptor() function because the
  93. * arguments are different.
  94. */
  95. result = usb_control_msg(
  96. wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  97. USB_REQ_SET_DESCRIPTOR,
  98. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
  99. USB_DT_RPIPE<<8, index, descr, sizeof(*descr),
  100. USB_CTRL_SET_TIMEOUT);
  101. if (result < 0) {
  102. dev_err(dev, "rpipe %u: set descriptor failed: %d\n",
  103. index, (int)result);
  104. goto error;
  105. }
  106. if (result < sizeof(*descr)) {
  107. dev_err(dev, "rpipe %u: sent short descriptor "
  108. "(%zd vs %zd bytes required)\n",
  109. index, result, sizeof(*descr));
  110. result = -EINVAL;
  111. goto error;
  112. }
  113. result = 0;
  114. error:
  115. return result;
  116. }
  117. static void rpipe_init(struct wa_rpipe *rpipe)
  118. {
  119. kref_init(&rpipe->refcnt);
  120. spin_lock_init(&rpipe->seg_lock);
  121. INIT_LIST_HEAD(&rpipe->seg_list);
  122. INIT_LIST_HEAD(&rpipe->list_node);
  123. }
  124. static unsigned rpipe_get_idx(struct wahc *wa, unsigned rpipe_idx)
  125. {
  126. unsigned long flags;
  127. spin_lock_irqsave(&wa->rpipe_lock, flags);
  128. rpipe_idx = find_next_zero_bit(wa->rpipe_bm, wa->rpipes, rpipe_idx);
  129. if (rpipe_idx < wa->rpipes)
  130. set_bit(rpipe_idx, wa->rpipe_bm);
  131. spin_unlock_irqrestore(&wa->rpipe_lock, flags);
  132. return rpipe_idx;
  133. }
  134. static void rpipe_put_idx(struct wahc *wa, unsigned rpipe_idx)
  135. {
  136. unsigned long flags;
  137. spin_lock_irqsave(&wa->rpipe_lock, flags);
  138. clear_bit(rpipe_idx, wa->rpipe_bm);
  139. spin_unlock_irqrestore(&wa->rpipe_lock, flags);
  140. }
  141. void rpipe_destroy(struct kref *_rpipe)
  142. {
  143. struct wa_rpipe *rpipe = container_of(_rpipe, struct wa_rpipe, refcnt);
  144. u8 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
  145. if (rpipe->ep)
  146. rpipe->ep->hcpriv = NULL;
  147. rpipe_put_idx(rpipe->wa, index);
  148. wa_put(rpipe->wa);
  149. kfree(rpipe);
  150. }
  151. EXPORT_SYMBOL_GPL(rpipe_destroy);
  152. /*
  153. * Locate an idle rpipe, create an structure for it and return it
  154. *
  155. * @wa is referenced and unlocked
  156. * @crs enum rpipe_attr, required endpoint characteristics
  157. *
  158. * The rpipe can be used only sequentially (not in parallel).
  159. *
  160. * The rpipe is moved into the "ready" state.
  161. */
  162. static int rpipe_get_idle(struct wa_rpipe **prpipe, struct wahc *wa, u8 crs,
  163. gfp_t gfp)
  164. {
  165. int result;
  166. unsigned rpipe_idx;
  167. struct wa_rpipe *rpipe;
  168. struct device *dev = &wa->usb_iface->dev;
  169. rpipe = kzalloc(sizeof(*rpipe), gfp);
  170. if (rpipe == NULL)
  171. return -ENOMEM;
  172. rpipe_init(rpipe);
  173. /* Look for an idle pipe */
  174. for (rpipe_idx = 0; rpipe_idx < wa->rpipes; rpipe_idx++) {
  175. rpipe_idx = rpipe_get_idx(wa, rpipe_idx);
  176. if (rpipe_idx >= wa->rpipes) /* no more pipes :( */
  177. break;
  178. result = __rpipe_get_descr(wa, &rpipe->descr, rpipe_idx);
  179. if (result < 0)
  180. dev_err(dev, "Can't get descriptor for rpipe %u: %d\n",
  181. rpipe_idx, result);
  182. else if ((rpipe->descr.bmCharacteristics & crs) != 0)
  183. goto found;
  184. rpipe_put_idx(wa, rpipe_idx);
  185. }
  186. *prpipe = NULL;
  187. kfree(rpipe);
  188. return -ENXIO;
  189. found:
  190. set_bit(rpipe_idx, wa->rpipe_bm);
  191. rpipe->wa = wa_get(wa);
  192. *prpipe = rpipe;
  193. return 0;
  194. }
  195. static int __rpipe_reset(struct wahc *wa, unsigned index)
  196. {
  197. int result;
  198. struct device *dev = &wa->usb_iface->dev;
  199. result = usb_control_msg(
  200. wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  201. USB_REQ_RPIPE_RESET,
  202. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
  203. 0, index, NULL, 0, USB_CTRL_SET_TIMEOUT);
  204. if (result < 0)
  205. dev_err(dev, "rpipe %u: reset failed: %d\n",
  206. index, result);
  207. return result;
  208. }
  209. /*
  210. * Fake companion descriptor for ep0
  211. *
  212. * See WUSB1.0[7.4.4], most of this is zero for bulk/int/ctl
  213. */
  214. static struct usb_wireless_ep_comp_descriptor epc0 = {
  215. .bLength = sizeof(epc0),
  216. .bDescriptorType = USB_DT_WIRELESS_ENDPOINT_COMP,
  217. .bMaxBurst = 1,
  218. .bMaxSequence = 2,
  219. };
  220. /*
  221. * Look for EP companion descriptor
  222. *
  223. * Get there, look for Inara in the endpoint's extra descriptors
  224. */
  225. static struct usb_wireless_ep_comp_descriptor *rpipe_epc_find(
  226. struct device *dev, struct usb_host_endpoint *ep)
  227. {
  228. void *itr;
  229. size_t itr_size;
  230. struct usb_descriptor_header *hdr;
  231. struct usb_wireless_ep_comp_descriptor *epcd;
  232. if (ep->desc.bEndpointAddress == 0) {
  233. epcd = &epc0;
  234. goto out;
  235. }
  236. itr = ep->extra;
  237. itr_size = ep->extralen;
  238. epcd = NULL;
  239. while (itr_size > 0) {
  240. if (itr_size < sizeof(*hdr)) {
  241. dev_err(dev, "HW Bug? ep 0x%02x: extra descriptors "
  242. "at offset %zu: only %zu bytes left\n",
  243. ep->desc.bEndpointAddress,
  244. itr - (void *) ep->extra, itr_size);
  245. break;
  246. }
  247. hdr = itr;
  248. if (hdr->bDescriptorType == USB_DT_WIRELESS_ENDPOINT_COMP) {
  249. epcd = itr;
  250. break;
  251. }
  252. if (hdr->bLength > itr_size) {
  253. dev_err(dev, "HW Bug? ep 0x%02x: extra descriptor "
  254. "at offset %zu (type 0x%02x) "
  255. "length %d but only %zu bytes left\n",
  256. ep->desc.bEndpointAddress,
  257. itr - (void *) ep->extra, hdr->bDescriptorType,
  258. hdr->bLength, itr_size);
  259. break;
  260. }
  261. itr += hdr->bLength;
  262. itr_size -= hdr->bLength;
  263. }
  264. out:
  265. return epcd;
  266. }
  267. /*
  268. * Aim an rpipe to its device & endpoint destination
  269. *
  270. * Make sure we change the address to unauthenticated if the device
  271. * is WUSB and it is not authenticated.
  272. */
  273. static int rpipe_aim(struct wa_rpipe *rpipe, struct wahc *wa,
  274. struct usb_host_endpoint *ep, struct urb *urb, gfp_t gfp)
  275. {
  276. int result = -ENOMSG; /* better code for lack of companion? */
  277. struct device *dev = &wa->usb_iface->dev;
  278. struct usb_device *usb_dev = urb->dev;
  279. struct usb_wireless_ep_comp_descriptor *epcd;
  280. u32 ack_window, epcd_max_sequence;
  281. u8 unauth;
  282. epcd = rpipe_epc_find(dev, ep);
  283. if (epcd == NULL) {
  284. dev_err(dev, "ep 0x%02x: can't find companion descriptor\n",
  285. ep->desc.bEndpointAddress);
  286. goto error;
  287. }
  288. unauth = usb_dev->wusb && !usb_dev->authenticated ? 0x80 : 0;
  289. __rpipe_reset(wa, le16_to_cpu(rpipe->descr.wRPipeIndex));
  290. atomic_set(&rpipe->segs_available,
  291. le16_to_cpu(rpipe->descr.wRequests));
  292. /* FIXME: block allocation system; request with queuing and timeout */
  293. /* FIXME: compute so seg_size > ep->maxpktsize */
  294. rpipe->descr.wBlocks = cpu_to_le16(16); /* given */
  295. /* ep0 maxpktsize is 0x200 (WUSB1.0[4.8.1]) */
  296. if (usb_endpoint_xfer_isoc(&ep->desc))
  297. rpipe->descr.wMaxPacketSize = epcd->wOverTheAirPacketSize;
  298. else
  299. rpipe->descr.wMaxPacketSize = ep->desc.wMaxPacketSize;
  300. rpipe->descr.hwa_bMaxBurst = max(min_t(unsigned int,
  301. epcd->bMaxBurst, 16U), 1U);
  302. rpipe->descr.hwa_bDeviceInfoIndex =
  303. wusb_port_no_to_idx(urb->dev->portnum);
  304. /* FIXME: use maximum speed as supported or recommended by device */
  305. rpipe->descr.bSpeed = usb_pipeendpoint(urb->pipe) == 0 ?
  306. UWB_PHY_RATE_53 : UWB_PHY_RATE_200;
  307. dev_dbg(dev, "addr %u (0x%02x) rpipe #%u ep# %u speed %d\n",
  308. urb->dev->devnum, urb->dev->devnum | unauth,
  309. le16_to_cpu(rpipe->descr.wRPipeIndex),
  310. usb_pipeendpoint(urb->pipe), rpipe->descr.bSpeed);
  311. rpipe->descr.hwa_reserved = 0;
  312. rpipe->descr.bEndpointAddress = ep->desc.bEndpointAddress;
  313. /* FIXME: bDataSequence */
  314. rpipe->descr.bDataSequence = 0;
  315. /* start with base window of hwa_bMaxBurst bits starting at 0. */
  316. ack_window = 0xFFFFFFFF >> (32 - rpipe->descr.hwa_bMaxBurst);
  317. rpipe->descr.dwCurrentWindow = cpu_to_le32(ack_window);
  318. epcd_max_sequence = max(min_t(unsigned int,
  319. epcd->bMaxSequence, 32U), 2U);
  320. rpipe->descr.bMaxDataSequence = epcd_max_sequence - 1;
  321. rpipe->descr.bInterval = ep->desc.bInterval;
  322. if (usb_endpoint_xfer_isoc(&ep->desc))
  323. rpipe->descr.bOverTheAirInterval = epcd->bOverTheAirInterval;
  324. else
  325. rpipe->descr.bOverTheAirInterval = 0; /* 0 if not isoc */
  326. /* FIXME: xmit power & preamble blah blah */
  327. rpipe->descr.bmAttribute = (ep->desc.bmAttributes &
  328. USB_ENDPOINT_XFERTYPE_MASK);
  329. /* rpipe->descr.bmCharacteristics RO */
  330. rpipe->descr.bmRetryOptions = (wa->wusb->retry_count & 0xF);
  331. /* FIXME: use for assessing link quality? */
  332. rpipe->descr.wNumTransactionErrors = 0;
  333. result = __rpipe_set_descr(wa, &rpipe->descr,
  334. le16_to_cpu(rpipe->descr.wRPipeIndex));
  335. if (result < 0) {
  336. dev_err(dev, "Cannot aim rpipe: %d\n", result);
  337. goto error;
  338. }
  339. result = 0;
  340. error:
  341. return result;
  342. }
  343. /*
  344. * Check an aimed rpipe to make sure it points to where we want
  345. *
  346. * We use bit 19 of the Linux USB pipe bitmap for unauth vs auth
  347. * space; when it is like that, we or 0x80 to make an unauth address.
  348. */
  349. static int rpipe_check_aim(const struct wa_rpipe *rpipe, const struct wahc *wa,
  350. const struct usb_host_endpoint *ep,
  351. const struct urb *urb, gfp_t gfp)
  352. {
  353. int result = 0;
  354. struct device *dev = &wa->usb_iface->dev;
  355. u8 portnum = wusb_port_no_to_idx(urb->dev->portnum);
  356. #define AIM_CHECK(rdf, val, text) \
  357. do { \
  358. if (rpipe->descr.rdf != (val)) { \
  359. dev_err(dev, \
  360. "rpipe aim discrepancy: " #rdf " " text "\n", \
  361. rpipe->descr.rdf, (val)); \
  362. result = -EINVAL; \
  363. WARN_ON(1); \
  364. } \
  365. } while (0)
  366. AIM_CHECK(hwa_bDeviceInfoIndex, portnum, "(%u vs %u)");
  367. AIM_CHECK(bSpeed, usb_pipeendpoint(urb->pipe) == 0 ?
  368. UWB_PHY_RATE_53 : UWB_PHY_RATE_200,
  369. "(%u vs %u)");
  370. AIM_CHECK(bEndpointAddress, ep->desc.bEndpointAddress, "(%u vs %u)");
  371. AIM_CHECK(bInterval, ep->desc.bInterval, "(%u vs %u)");
  372. AIM_CHECK(bmAttribute, ep->desc.bmAttributes & 0x03, "(%u vs %u)");
  373. #undef AIM_CHECK
  374. return result;
  375. }
  376. #ifndef CONFIG_BUG
  377. #define CONFIG_BUG 0
  378. #endif
  379. /*
  380. * Make sure there is an rpipe allocated for an endpoint
  381. *
  382. * If already allocated, we just refcount it; if not, we get an
  383. * idle one, aim it to the right location and take it.
  384. *
  385. * Attaches to ep->hcpriv and rpipe->ep to ep.
  386. */
  387. int rpipe_get_by_ep(struct wahc *wa, struct usb_host_endpoint *ep,
  388. struct urb *urb, gfp_t gfp)
  389. {
  390. int result = 0;
  391. struct device *dev = &wa->usb_iface->dev;
  392. struct wa_rpipe *rpipe;
  393. u8 eptype;
  394. mutex_lock(&wa->rpipe_mutex);
  395. rpipe = ep->hcpriv;
  396. if (rpipe != NULL) {
  397. if (CONFIG_BUG == 1) {
  398. result = rpipe_check_aim(rpipe, wa, ep, urb, gfp);
  399. if (result < 0)
  400. goto error;
  401. }
  402. __rpipe_get(rpipe);
  403. dev_dbg(dev, "ep 0x%02x: reusing rpipe %u\n",
  404. ep->desc.bEndpointAddress,
  405. le16_to_cpu(rpipe->descr.wRPipeIndex));
  406. } else {
  407. /* hmm, assign idle rpipe, aim it */
  408. result = -ENOBUFS;
  409. eptype = ep->desc.bmAttributes & 0x03;
  410. result = rpipe_get_idle(&rpipe, wa, 1 << eptype, gfp);
  411. if (result < 0)
  412. goto error;
  413. result = rpipe_aim(rpipe, wa, ep, urb, gfp);
  414. if (result < 0) {
  415. rpipe_put(rpipe);
  416. goto error;
  417. }
  418. ep->hcpriv = rpipe;
  419. rpipe->ep = ep;
  420. __rpipe_get(rpipe); /* for caching into ep->hcpriv */
  421. dev_dbg(dev, "ep 0x%02x: using rpipe %u\n",
  422. ep->desc.bEndpointAddress,
  423. le16_to_cpu(rpipe->descr.wRPipeIndex));
  424. }
  425. error:
  426. mutex_unlock(&wa->rpipe_mutex);
  427. return result;
  428. }
  429. /*
  430. * Allocate the bitmap for each rpipe.
  431. */
  432. int wa_rpipes_create(struct wahc *wa)
  433. {
  434. wa->rpipes = le16_to_cpu(wa->wa_descr->wNumRPipes);
  435. wa->rpipe_bm = kcalloc(BITS_TO_LONGS(wa->rpipes),
  436. sizeof(unsigned long),
  437. GFP_KERNEL);
  438. if (wa->rpipe_bm == NULL)
  439. return -ENOMEM;
  440. return 0;
  441. }
  442. void wa_rpipes_destroy(struct wahc *wa)
  443. {
  444. struct device *dev = &wa->usb_iface->dev;
  445. if (!bitmap_empty(wa->rpipe_bm, wa->rpipes)) {
  446. WARN_ON(1);
  447. dev_err(dev, "BUG: pipes not released on exit: %*pb\n",
  448. wa->rpipes, wa->rpipe_bm);
  449. }
  450. kfree(wa->rpipe_bm);
  451. }
  452. /*
  453. * Release resources allocated for an endpoint
  454. *
  455. * If there is an associated rpipe to this endpoint, Abort any pending
  456. * transfers and put it. If the rpipe ends up being destroyed,
  457. * __rpipe_destroy() will cleanup ep->hcpriv.
  458. *
  459. * This is called before calling hcd->stop(), so you don't need to do
  460. * anything else in there.
  461. */
  462. void rpipe_ep_disable(struct wahc *wa, struct usb_host_endpoint *ep)
  463. {
  464. struct wa_rpipe *rpipe;
  465. mutex_lock(&wa->rpipe_mutex);
  466. rpipe = ep->hcpriv;
  467. if (rpipe != NULL) {
  468. u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
  469. usb_control_msg(
  470. wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  471. USB_REQ_RPIPE_ABORT,
  472. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
  473. 0, index, NULL, 0, USB_CTRL_SET_TIMEOUT);
  474. rpipe_put(rpipe);
  475. }
  476. mutex_unlock(&wa->rpipe_mutex);
  477. }
  478. EXPORT_SYMBOL_GPL(rpipe_ep_disable);
  479. /* Clear the stalled status of an RPIPE. */
  480. void rpipe_clear_feature_stalled(struct wahc *wa, struct usb_host_endpoint *ep)
  481. {
  482. struct wa_rpipe *rpipe;
  483. mutex_lock(&wa->rpipe_mutex);
  484. rpipe = ep->hcpriv;
  485. if (rpipe != NULL) {
  486. u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
  487. usb_control_msg(
  488. wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  489. USB_REQ_CLEAR_FEATURE,
  490. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
  491. RPIPE_STALL, index, NULL, 0, USB_CTRL_SET_TIMEOUT);
  492. }
  493. mutex_unlock(&wa->rpipe_mutex);
  494. }
  495. EXPORT_SYMBOL_GPL(rpipe_clear_feature_stalled);