wa-rpipe.c 15 KB

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