wa-rpipe.c 15 KB

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