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