wa-rpipe.c 15 KB

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