Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 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 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 27 | * Targeted at different downstream endpoints |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 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) |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 52 | * USB Address 5 (2 based -- 0 is for default, 1 for root hub) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 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 | */ |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 60 | #include <linux/atomic.h> |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 61 | #include <linux/bitmap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 62 | #include <linux/slab.h> |
Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 63 | #include <linux/export.h> |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 64 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 65 | #include "wusbhc.h" |
| 66 | #include "wa-hc.h" |
| 67 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 68 | static int __rpipe_get_descr(struct wahc *wa, |
| 69 | struct usb_rpipe_descriptor *descr, u16 index) |
| 70 | { |
| 71 | ssize_t result; |
| 72 | struct device *dev = &wa->usb_iface->dev; |
| 73 | |
| 74 | /* Get the RPIPE descriptor -- we cannot use the usb_get_descriptor() |
| 75 | * function because the arguments are different. |
| 76 | */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 77 | result = usb_control_msg( |
| 78 | wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0), |
| 79 | USB_REQ_GET_DESCRIPTOR, |
| 80 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_RPIPE, |
| 81 | USB_DT_RPIPE<<8, index, descr, sizeof(*descr), |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 82 | USB_CTRL_GET_TIMEOUT); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 83 | if (result < 0) { |
| 84 | dev_err(dev, "rpipe %u: get descriptor failed: %d\n", |
| 85 | index, (int)result); |
| 86 | goto error; |
| 87 | } |
| 88 | if (result < sizeof(*descr)) { |
| 89 | dev_err(dev, "rpipe %u: got short descriptor " |
| 90 | "(%zd vs %zd bytes needed)\n", |
| 91 | index, result, sizeof(*descr)); |
| 92 | result = -EINVAL; |
| 93 | goto error; |
| 94 | } |
| 95 | result = 0; |
| 96 | |
| 97 | error: |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * |
| 103 | * The descriptor is assumed to be properly initialized (ie: you got |
| 104 | * it through __rpipe_get_descr()). |
| 105 | */ |
| 106 | static int __rpipe_set_descr(struct wahc *wa, |
| 107 | struct usb_rpipe_descriptor *descr, u16 index) |
| 108 | { |
| 109 | ssize_t result; |
| 110 | struct device *dev = &wa->usb_iface->dev; |
| 111 | |
| 112 | /* we cannot use the usb_get_descriptor() function because the |
| 113 | * arguments are different. |
| 114 | */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 115 | result = usb_control_msg( |
| 116 | wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0), |
| 117 | USB_REQ_SET_DESCRIPTOR, |
| 118 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE, |
| 119 | USB_DT_RPIPE<<8, index, descr, sizeof(*descr), |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 120 | USB_CTRL_SET_TIMEOUT); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 121 | if (result < 0) { |
| 122 | dev_err(dev, "rpipe %u: set descriptor failed: %d\n", |
| 123 | index, (int)result); |
| 124 | goto error; |
| 125 | } |
| 126 | if (result < sizeof(*descr)) { |
| 127 | dev_err(dev, "rpipe %u: sent short descriptor " |
| 128 | "(%zd vs %zd bytes required)\n", |
| 129 | index, result, sizeof(*descr)); |
| 130 | result = -EINVAL; |
| 131 | goto error; |
| 132 | } |
| 133 | result = 0; |
| 134 | |
| 135 | error: |
| 136 | return result; |
| 137 | |
| 138 | } |
| 139 | |
| 140 | static void rpipe_init(struct wa_rpipe *rpipe) |
| 141 | { |
| 142 | kref_init(&rpipe->refcnt); |
| 143 | spin_lock_init(&rpipe->seg_lock); |
| 144 | INIT_LIST_HEAD(&rpipe->seg_list); |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 145 | INIT_LIST_HEAD(&rpipe->list_node); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static unsigned rpipe_get_idx(struct wahc *wa, unsigned rpipe_idx) |
| 149 | { |
| 150 | unsigned long flags; |
| 151 | |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 152 | spin_lock_irqsave(&wa->rpipe_lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 153 | rpipe_idx = find_next_zero_bit(wa->rpipe_bm, wa->rpipes, rpipe_idx); |
| 154 | if (rpipe_idx < wa->rpipes) |
| 155 | set_bit(rpipe_idx, wa->rpipe_bm); |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 156 | spin_unlock_irqrestore(&wa->rpipe_lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 157 | |
| 158 | return rpipe_idx; |
| 159 | } |
| 160 | |
| 161 | static void rpipe_put_idx(struct wahc *wa, unsigned rpipe_idx) |
| 162 | { |
| 163 | unsigned long flags; |
| 164 | |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 165 | spin_lock_irqsave(&wa->rpipe_lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 166 | clear_bit(rpipe_idx, wa->rpipe_bm); |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 167 | spin_unlock_irqrestore(&wa->rpipe_lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void rpipe_destroy(struct kref *_rpipe) |
| 171 | { |
| 172 | struct wa_rpipe *rpipe = container_of(_rpipe, struct wa_rpipe, refcnt); |
| 173 | u8 index = le16_to_cpu(rpipe->descr.wRPipeIndex); |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 174 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 175 | if (rpipe->ep) |
| 176 | rpipe->ep->hcpriv = NULL; |
| 177 | rpipe_put_idx(rpipe->wa, index); |
| 178 | wa_put(rpipe->wa); |
| 179 | kfree(rpipe); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 180 | } |
| 181 | EXPORT_SYMBOL_GPL(rpipe_destroy); |
| 182 | |
| 183 | /* |
| 184 | * Locate an idle rpipe, create an structure for it and return it |
| 185 | * |
Rahul Bedarkar | 0c106d0 | 2014-01-04 14:12:24 +0530 | [diff] [blame] | 186 | * @wa is referenced and unlocked |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 187 | * @crs enum rpipe_attr, required endpoint characteristics |
| 188 | * |
| 189 | * The rpipe can be used only sequentially (not in parallel). |
| 190 | * |
| 191 | * The rpipe is moved into the "ready" state. |
| 192 | */ |
| 193 | static int rpipe_get_idle(struct wa_rpipe **prpipe, struct wahc *wa, u8 crs, |
| 194 | gfp_t gfp) |
| 195 | { |
| 196 | int result; |
| 197 | unsigned rpipe_idx; |
| 198 | struct wa_rpipe *rpipe; |
| 199 | struct device *dev = &wa->usb_iface->dev; |
| 200 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 201 | rpipe = kzalloc(sizeof(*rpipe), gfp); |
| 202 | if (rpipe == NULL) |
| 203 | return -ENOMEM; |
| 204 | rpipe_init(rpipe); |
| 205 | |
| 206 | /* Look for an idle pipe */ |
| 207 | for (rpipe_idx = 0; rpipe_idx < wa->rpipes; rpipe_idx++) { |
| 208 | rpipe_idx = rpipe_get_idx(wa, rpipe_idx); |
| 209 | if (rpipe_idx >= wa->rpipes) /* no more pipes :( */ |
| 210 | break; |
| 211 | result = __rpipe_get_descr(wa, &rpipe->descr, rpipe_idx); |
| 212 | if (result < 0) |
| 213 | dev_err(dev, "Can't get descriptor for rpipe %u: %d\n", |
| 214 | rpipe_idx, result); |
| 215 | else if ((rpipe->descr.bmCharacteristics & crs) != 0) |
| 216 | goto found; |
| 217 | rpipe_put_idx(wa, rpipe_idx); |
| 218 | } |
| 219 | *prpipe = NULL; |
| 220 | kfree(rpipe); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 221 | return -ENXIO; |
| 222 | |
| 223 | found: |
| 224 | set_bit(rpipe_idx, wa->rpipe_bm); |
| 225 | rpipe->wa = wa_get(wa); |
| 226 | *prpipe = rpipe; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int __rpipe_reset(struct wahc *wa, unsigned index) |
| 231 | { |
| 232 | int result; |
| 233 | struct device *dev = &wa->usb_iface->dev; |
| 234 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 235 | result = usb_control_msg( |
| 236 | wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0), |
| 237 | USB_REQ_RPIPE_RESET, |
| 238 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE, |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 239 | 0, index, NULL, 0, USB_CTRL_SET_TIMEOUT); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 240 | if (result < 0) |
| 241 | dev_err(dev, "rpipe %u: reset failed: %d\n", |
| 242 | index, result); |
| 243 | return result; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Fake companion descriptor for ep0 |
| 248 | * |
| 249 | * See WUSB1.0[7.4.4], most of this is zero for bulk/int/ctl |
| 250 | */ |
| 251 | static struct usb_wireless_ep_comp_descriptor epc0 = { |
| 252 | .bLength = sizeof(epc0), |
| 253 | .bDescriptorType = USB_DT_WIRELESS_ENDPOINT_COMP, |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 254 | .bMaxBurst = 1, |
| 255 | .bMaxSequence = 2, |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | /* |
| 259 | * Look for EP companion descriptor |
| 260 | * |
| 261 | * Get there, look for Inara in the endpoint's extra descriptors |
| 262 | */ |
| 263 | static struct usb_wireless_ep_comp_descriptor *rpipe_epc_find( |
| 264 | struct device *dev, struct usb_host_endpoint *ep) |
| 265 | { |
| 266 | void *itr; |
| 267 | size_t itr_size; |
| 268 | struct usb_descriptor_header *hdr; |
| 269 | struct usb_wireless_ep_comp_descriptor *epcd; |
| 270 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 271 | if (ep->desc.bEndpointAddress == 0) { |
| 272 | epcd = &epc0; |
| 273 | goto out; |
| 274 | } |
| 275 | itr = ep->extra; |
| 276 | itr_size = ep->extralen; |
| 277 | epcd = NULL; |
| 278 | while (itr_size > 0) { |
| 279 | if (itr_size < sizeof(*hdr)) { |
| 280 | dev_err(dev, "HW Bug? ep 0x%02x: extra descriptors " |
| 281 | "at offset %zu: only %zu bytes left\n", |
| 282 | ep->desc.bEndpointAddress, |
| 283 | itr - (void *) ep->extra, itr_size); |
| 284 | break; |
| 285 | } |
| 286 | hdr = itr; |
| 287 | if (hdr->bDescriptorType == USB_DT_WIRELESS_ENDPOINT_COMP) { |
| 288 | epcd = itr; |
| 289 | break; |
| 290 | } |
| 291 | if (hdr->bLength > itr_size) { |
| 292 | dev_err(dev, "HW Bug? ep 0x%02x: extra descriptor " |
| 293 | "at offset %zu (type 0x%02x) " |
| 294 | "length %d but only %zu bytes left\n", |
| 295 | ep->desc.bEndpointAddress, |
| 296 | itr - (void *) ep->extra, hdr->bDescriptorType, |
| 297 | hdr->bLength, itr_size); |
| 298 | break; |
| 299 | } |
| 300 | itr += hdr->bLength; |
Thomas Pugliese | 4659a24 | 2014-02-28 15:15:19 -0600 | [diff] [blame] | 301 | itr_size -= hdr->bLength; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 302 | } |
| 303 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 304 | return epcd; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Aim an rpipe to its device & endpoint destination |
| 309 | * |
Rahul Bedarkar | 1076e7a | 2014-01-04 12:37:52 +0530 | [diff] [blame] | 310 | * Make sure we change the address to unauthenticated if the device |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 311 | * is WUSB and it is not authenticated. |
| 312 | */ |
| 313 | static int rpipe_aim(struct wa_rpipe *rpipe, struct wahc *wa, |
| 314 | struct usb_host_endpoint *ep, struct urb *urb, gfp_t gfp) |
| 315 | { |
| 316 | int result = -ENOMSG; /* better code for lack of companion? */ |
| 317 | struct device *dev = &wa->usb_iface->dev; |
| 318 | struct usb_device *usb_dev = urb->dev; |
| 319 | struct usb_wireless_ep_comp_descriptor *epcd; |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 320 | u32 ack_window, epcd_max_sequence; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 321 | u8 unauth; |
| 322 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 323 | epcd = rpipe_epc_find(dev, ep); |
| 324 | if (epcd == NULL) { |
| 325 | dev_err(dev, "ep 0x%02x: can't find companion descriptor\n", |
| 326 | ep->desc.bEndpointAddress); |
| 327 | goto error; |
| 328 | } |
| 329 | unauth = usb_dev->wusb && !usb_dev->authenticated ? 0x80 : 0; |
| 330 | __rpipe_reset(wa, le16_to_cpu(rpipe->descr.wRPipeIndex)); |
Rahul Bedarkar | 0c106d0 | 2014-01-04 14:12:24 +0530 | [diff] [blame] | 331 | atomic_set(&rpipe->segs_available, |
| 332 | le16_to_cpu(rpipe->descr.wRequests)); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 333 | /* FIXME: block allocation system; request with queuing and timeout */ |
| 334 | /* FIXME: compute so seg_size > ep->maxpktsize */ |
| 335 | rpipe->descr.wBlocks = cpu_to_le16(16); /* given */ |
| 336 | /* ep0 maxpktsize is 0x200 (WUSB1.0[4.8.1]) */ |
Thomas Pugliese | 7b6bc07 | 2013-10-23 14:44:26 -0500 | [diff] [blame] | 337 | if (usb_endpoint_xfer_isoc(&ep->desc)) |
| 338 | rpipe->descr.wMaxPacketSize = epcd->wOverTheAirPacketSize; |
| 339 | else |
| 340 | rpipe->descr.wMaxPacketSize = ep->desc.wMaxPacketSize; |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 341 | |
| 342 | rpipe->descr.hwa_bMaxBurst = max(min_t(unsigned int, |
| 343 | epcd->bMaxBurst, 16U), 1U); |
| 344 | rpipe->descr.hwa_bDeviceInfoIndex = |
| 345 | wusb_port_no_to_idx(urb->dev->portnum); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 346 | /* FIXME: use maximum speed as supported or recommended by device */ |
| 347 | rpipe->descr.bSpeed = usb_pipeendpoint(urb->pipe) == 0 ? |
| 348 | UWB_PHY_RATE_53 : UWB_PHY_RATE_200; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 349 | |
| 350 | dev_dbg(dev, "addr %u (0x%02x) rpipe #%u ep# %u speed %d\n", |
| 351 | urb->dev->devnum, urb->dev->devnum | unauth, |
| 352 | le16_to_cpu(rpipe->descr.wRPipeIndex), |
| 353 | usb_pipeendpoint(urb->pipe), rpipe->descr.bSpeed); |
| 354 | |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 355 | rpipe->descr.hwa_reserved = 0; |
| 356 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 357 | rpipe->descr.bEndpointAddress = ep->desc.bEndpointAddress; |
| 358 | /* FIXME: bDataSequence */ |
| 359 | rpipe->descr.bDataSequence = 0; |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 360 | |
| 361 | /* start with base window of hwa_bMaxBurst bits starting at 0. */ |
| 362 | ack_window = 0xFFFFFFFF >> (32 - rpipe->descr.hwa_bMaxBurst); |
| 363 | rpipe->descr.dwCurrentWindow = cpu_to_le32(ack_window); |
| 364 | epcd_max_sequence = max(min_t(unsigned int, |
| 365 | epcd->bMaxSequence, 32U), 2U); |
| 366 | rpipe->descr.bMaxDataSequence = epcd_max_sequence - 1; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 367 | rpipe->descr.bInterval = ep->desc.bInterval; |
Thomas Pugliese | 1a7ff0e | 2013-10-01 14:04:34 -0500 | [diff] [blame] | 368 | if (usb_endpoint_xfer_isoc(&ep->desc)) |
| 369 | rpipe->descr.bOverTheAirInterval = epcd->bOverTheAirInterval; |
| 370 | else |
| 371 | rpipe->descr.bOverTheAirInterval = 0; /* 0 if not isoc */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 372 | /* FIXME: xmit power & preamble blah blah */ |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 373 | rpipe->descr.bmAttribute = (ep->desc.bmAttributes & |
| 374 | USB_ENDPOINT_XFERTYPE_MASK); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 375 | /* rpipe->descr.bmCharacteristics RO */ |
Thomas Pugliese | f265d4d | 2013-06-18 13:31:26 -0500 | [diff] [blame] | 376 | rpipe->descr.bmRetryOptions = (wa->wusb->retry_count & 0xF); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 377 | /* FIXME: use for assessing link quality? */ |
| 378 | rpipe->descr.wNumTransactionErrors = 0; |
| 379 | result = __rpipe_set_descr(wa, &rpipe->descr, |
| 380 | le16_to_cpu(rpipe->descr.wRPipeIndex)); |
| 381 | if (result < 0) { |
| 382 | dev_err(dev, "Cannot aim rpipe: %d\n", result); |
| 383 | goto error; |
| 384 | } |
| 385 | result = 0; |
| 386 | error: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 387 | return result; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Check an aimed rpipe to make sure it points to where we want |
| 392 | * |
| 393 | * We use bit 19 of the Linux USB pipe bitmap for unauth vs auth |
| 394 | * space; when it is like that, we or 0x80 to make an unauth address. |
| 395 | */ |
| 396 | static int rpipe_check_aim(const struct wa_rpipe *rpipe, const struct wahc *wa, |
| 397 | const struct usb_host_endpoint *ep, |
| 398 | const struct urb *urb, gfp_t gfp) |
| 399 | { |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 400 | int result = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 401 | struct device *dev = &wa->usb_iface->dev; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 402 | u8 portnum = wusb_port_no_to_idx(urb->dev->portnum); |
| 403 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 404 | #define AIM_CHECK(rdf, val, text) \ |
| 405 | do { \ |
| 406 | if (rpipe->descr.rdf != (val)) { \ |
| 407 | dev_err(dev, \ |
| 408 | "rpipe aim discrepancy: " #rdf " " text "\n", \ |
| 409 | rpipe->descr.rdf, (val)); \ |
| 410 | result = -EINVAL; \ |
| 411 | WARN_ON(1); \ |
| 412 | } \ |
| 413 | } while (0) |
Thomas Pugliese | ee0218f | 2013-06-06 14:06:01 -0500 | [diff] [blame] | 414 | AIM_CHECK(hwa_bDeviceInfoIndex, portnum, "(%u vs %u)"); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 415 | AIM_CHECK(bSpeed, usb_pipeendpoint(urb->pipe) == 0 ? |
| 416 | UWB_PHY_RATE_53 : UWB_PHY_RATE_200, |
| 417 | "(%u vs %u)"); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 418 | AIM_CHECK(bEndpointAddress, ep->desc.bEndpointAddress, "(%u vs %u)"); |
| 419 | AIM_CHECK(bInterval, ep->desc.bInterval, "(%u vs %u)"); |
| 420 | AIM_CHECK(bmAttribute, ep->desc.bmAttributes & 0x03, "(%u vs %u)"); |
| 421 | #undef AIM_CHECK |
| 422 | return result; |
| 423 | } |
| 424 | |
| 425 | #ifndef CONFIG_BUG |
| 426 | #define CONFIG_BUG 0 |
| 427 | #endif |
| 428 | |
| 429 | /* |
| 430 | * Make sure there is an rpipe allocated for an endpoint |
| 431 | * |
| 432 | * If already allocated, we just refcount it; if not, we get an |
| 433 | * idle one, aim it to the right location and take it. |
| 434 | * |
| 435 | * Attaches to ep->hcpriv and rpipe->ep to ep. |
| 436 | */ |
| 437 | int rpipe_get_by_ep(struct wahc *wa, struct usb_host_endpoint *ep, |
| 438 | struct urb *urb, gfp_t gfp) |
| 439 | { |
| 440 | int result = 0; |
| 441 | struct device *dev = &wa->usb_iface->dev; |
| 442 | struct wa_rpipe *rpipe; |
| 443 | u8 eptype; |
| 444 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 445 | mutex_lock(&wa->rpipe_mutex); |
| 446 | rpipe = ep->hcpriv; |
| 447 | if (rpipe != NULL) { |
| 448 | if (CONFIG_BUG == 1) { |
| 449 | result = rpipe_check_aim(rpipe, wa, ep, urb, gfp); |
| 450 | if (result < 0) |
| 451 | goto error; |
| 452 | } |
| 453 | __rpipe_get(rpipe); |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 454 | dev_dbg(dev, "ep 0x%02x: reusing rpipe %u\n", |
| 455 | ep->desc.bEndpointAddress, |
| 456 | le16_to_cpu(rpipe->descr.wRPipeIndex)); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 457 | } else { |
| 458 | /* hmm, assign idle rpipe, aim it */ |
| 459 | result = -ENOBUFS; |
| 460 | eptype = ep->desc.bmAttributes & 0x03; |
| 461 | result = rpipe_get_idle(&rpipe, wa, 1 << eptype, gfp); |
| 462 | if (result < 0) |
| 463 | goto error; |
| 464 | result = rpipe_aim(rpipe, wa, ep, urb, gfp); |
| 465 | if (result < 0) { |
| 466 | rpipe_put(rpipe); |
| 467 | goto error; |
| 468 | } |
| 469 | ep->hcpriv = rpipe; |
| 470 | rpipe->ep = ep; |
| 471 | __rpipe_get(rpipe); /* for caching into ep->hcpriv */ |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 472 | dev_dbg(dev, "ep 0x%02x: using rpipe %u\n", |
| 473 | ep->desc.bEndpointAddress, |
| 474 | le16_to_cpu(rpipe->descr.wRPipeIndex)); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 475 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 476 | error: |
| 477 | mutex_unlock(&wa->rpipe_mutex); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 478 | return result; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Allocate the bitmap for each rpipe. |
| 483 | */ |
| 484 | int wa_rpipes_create(struct wahc *wa) |
| 485 | { |
Thomas Pugliese | 1653d2f | 2013-10-07 10:07:51 -0500 | [diff] [blame] | 486 | wa->rpipes = le16_to_cpu(wa->wa_descr->wNumRPipes); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 487 | wa->rpipe_bm = kzalloc(BITS_TO_LONGS(wa->rpipes)*sizeof(unsigned long), |
| 488 | GFP_KERNEL); |
| 489 | if (wa->rpipe_bm == NULL) |
| 490 | return -ENOMEM; |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | void wa_rpipes_destroy(struct wahc *wa) |
| 495 | { |
| 496 | struct device *dev = &wa->usb_iface->dev; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 497 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 498 | if (!bitmap_empty(wa->rpipe_bm, wa->rpipes)) { |
| 499 | char buf[256]; |
| 500 | WARN_ON(1); |
| 501 | bitmap_scnprintf(buf, sizeof(buf), wa->rpipe_bm, wa->rpipes); |
| 502 | dev_err(dev, "BUG: pipes not released on exit: %s\n", buf); |
| 503 | } |
| 504 | kfree(wa->rpipe_bm); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Release resources allocated for an endpoint |
| 509 | * |
| 510 | * If there is an associated rpipe to this endpoint, Abort any pending |
| 511 | * transfers and put it. If the rpipe ends up being destroyed, |
| 512 | * __rpipe_destroy() will cleanup ep->hcpriv. |
| 513 | * |
| 514 | * This is called before calling hcd->stop(), so you don't need to do |
| 515 | * anything else in there. |
| 516 | */ |
| 517 | void rpipe_ep_disable(struct wahc *wa, struct usb_host_endpoint *ep) |
| 518 | { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 519 | struct wa_rpipe *rpipe; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 520 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 521 | mutex_lock(&wa->rpipe_mutex); |
| 522 | rpipe = ep->hcpriv; |
| 523 | if (rpipe != NULL) { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 524 | u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex); |
| 525 | |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 526 | usb_control_msg( |
Johan Hovold | df3ce23 | 2014-05-26 19:23:52 +0200 | [diff] [blame] | 527 | wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0), |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 528 | USB_REQ_RPIPE_ABORT, |
Anderson Lizardo | b0a8132 | 2008-09-17 16:34:32 +0100 | [diff] [blame] | 529 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE, |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 530 | 0, index, NULL, 0, USB_CTRL_SET_TIMEOUT); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 531 | rpipe_put(rpipe); |
| 532 | } |
| 533 | mutex_unlock(&wa->rpipe_mutex); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 534 | } |
| 535 | EXPORT_SYMBOL_GPL(rpipe_ep_disable); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 536 | |
| 537 | /* Clear the stalled status of an RPIPE. */ |
| 538 | void rpipe_clear_feature_stalled(struct wahc *wa, struct usb_host_endpoint *ep) |
| 539 | { |
| 540 | struct wa_rpipe *rpipe; |
| 541 | |
| 542 | mutex_lock(&wa->rpipe_mutex); |
| 543 | rpipe = ep->hcpriv; |
| 544 | if (rpipe != NULL) { |
| 545 | u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex); |
| 546 | |
| 547 | usb_control_msg( |
Johan Hovold | df3ce23 | 2014-05-26 19:23:52 +0200 | [diff] [blame] | 548 | wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0), |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 549 | USB_REQ_CLEAR_FEATURE, |
| 550 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE, |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 551 | RPIPE_STALL, index, NULL, 0, USB_CTRL_SET_TIMEOUT); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 552 | } |
| 553 | mutex_unlock(&wa->rpipe_mutex); |
| 554 | } |
| 555 | EXPORT_SYMBOL_GPL(rpipe_clear_feature_stalled); |