Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * WUSB Wire Adapter |
| 3 | * Data transfer and URB enqueing |
| 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 | * How transfers work: get a buffer, break it up in segments (segment |
| 24 | * size is a multiple of the maxpacket size). For each segment issue a |
| 25 | * segment request (struct wa_xfer_*), then send the data buffer if |
| 26 | * out or nothing if in (all over the DTO endpoint). |
| 27 | * |
| 28 | * For each submitted segment request, a notification will come over |
| 29 | * the NEP endpoint and a transfer result (struct xfer_result) will |
| 30 | * arrive in the DTI URB. Read it, get the xfer ID, see if there is |
| 31 | * data coming (inbound transfer), schedule a read and handle it. |
| 32 | * |
| 33 | * Sounds simple, it is a pain to implement. |
| 34 | * |
| 35 | * |
| 36 | * ENTRY POINTS |
| 37 | * |
| 38 | * FIXME |
| 39 | * |
| 40 | * LIFE CYCLE / STATE DIAGRAM |
| 41 | * |
| 42 | * FIXME |
| 43 | * |
| 44 | * THIS CODE IS DISGUSTING |
| 45 | * |
| 46 | * Warned you are; it's my second try and still not happy with it. |
| 47 | * |
| 48 | * NOTES: |
| 49 | * |
| 50 | * - No iso |
| 51 | * |
| 52 | * - Supports DMA xfers, control, bulk and maybe interrupt |
| 53 | * |
| 54 | * - Does not recycle unused rpipes |
| 55 | * |
| 56 | * An rpipe is assigned to an endpoint the first time it is used, |
| 57 | * and then it's there, assigned, until the endpoint is disabled |
| 58 | * (destroyed [{h,d}wahc_op_ep_disable()]. The assignment of the |
| 59 | * rpipe to the endpoint is done under the wa->rpipe_sem semaphore |
| 60 | * (should be a mutex). |
| 61 | * |
| 62 | * Two methods it could be done: |
| 63 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 64 | * (a) set up a timer every time an rpipe's use count drops to 1 |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 65 | * (which means unused) or when a transfer ends. Reset the |
| 66 | * timer when a xfer is queued. If the timer expires, release |
| 67 | * the rpipe [see rpipe_ep_disable()]. |
| 68 | * |
| 69 | * (b) when looking for free rpipes to attach [rpipe_get_by_ep()], |
| 70 | * when none are found go over the list, check their endpoint |
| 71 | * and their activity record (if no last-xfer-done-ts in the |
| 72 | * last x seconds) take it |
| 73 | * |
| 74 | * However, due to the fact that we have a set of limited |
| 75 | * resources (max-segments-at-the-same-time per xfer, |
| 76 | * xfers-per-ripe, blocks-per-rpipe, rpipes-per-host), at the end |
| 77 | * we are going to have to rebuild all this based on an scheduler, |
| 78 | * to where we have a list of transactions to do and based on the |
Gilles Espinasse | f77f13e | 2010-03-29 15:41:47 +0200 | [diff] [blame] | 79 | * availability of the different required components (blocks, |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 80 | * rpipes, segment slots, etc), we go scheduling them. Painful. |
| 81 | */ |
| 82 | #include <linux/init.h> |
| 83 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 84 | #include <linux/slab.h> |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 85 | #include <linux/hash.h> |
Manuel Zerpies | 9708cd2 | 2011-06-16 14:15:16 +0200 | [diff] [blame] | 86 | #include <linux/ratelimit.h> |
Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 87 | #include <linux/export.h> |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 88 | #include <linux/scatterlist.h> |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 89 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 90 | #include "wa-hc.h" |
| 91 | #include "wusbhc.h" |
| 92 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 93 | enum { |
| 94 | WA_SEGS_MAX = 255, |
| 95 | }; |
| 96 | |
| 97 | enum wa_seg_status { |
| 98 | WA_SEG_NOTREADY, |
| 99 | WA_SEG_READY, |
| 100 | WA_SEG_DELAYED, |
| 101 | WA_SEG_SUBMITTED, |
| 102 | WA_SEG_PENDING, |
| 103 | WA_SEG_DTI_PENDING, |
| 104 | WA_SEG_DONE, |
| 105 | WA_SEG_ERROR, |
| 106 | WA_SEG_ABORTED, |
| 107 | }; |
| 108 | |
| 109 | static void wa_xfer_delayed_run(struct wa_rpipe *); |
| 110 | |
| 111 | /* |
| 112 | * Life cycle governed by 'struct urb' (the refcount of the struct is |
| 113 | * that of the 'struct urb' and usb_free_urb() would free the whole |
| 114 | * struct). |
| 115 | */ |
| 116 | struct wa_seg { |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 117 | struct urb tr_urb; /* transfer request urb. */ |
| 118 | struct urb *dto_urb; /* for data output. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 119 | struct list_head list_node; /* for rpipe->req_list */ |
| 120 | struct wa_xfer *xfer; /* out xfer */ |
| 121 | u8 index; /* which segment we are */ |
| 122 | enum wa_seg_status status; |
| 123 | ssize_t result; /* bytes xfered or error */ |
| 124 | struct wa_xfer_hdr xfer_hdr; |
| 125 | u8 xfer_extra[]; /* xtra space for xfer_hdr_ctl */ |
| 126 | }; |
| 127 | |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 128 | static inline void wa_seg_init(struct wa_seg *seg) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 129 | { |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 130 | usb_init_urb(&seg->tr_urb); |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 131 | |
| 132 | /* set the remaining memory to 0. */ |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 133 | memset(((void *)seg) + sizeof(seg->tr_urb), 0, |
| 134 | sizeof(*seg) - sizeof(seg->tr_urb)); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Protected by xfer->lock |
| 139 | * |
| 140 | */ |
| 141 | struct wa_xfer { |
| 142 | struct kref refcnt; |
| 143 | struct list_head list_node; |
| 144 | spinlock_t lock; |
| 145 | u32 id; |
| 146 | |
| 147 | struct wahc *wa; /* Wire adapter we are plugged to */ |
| 148 | struct usb_host_endpoint *ep; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 149 | struct urb *urb; /* URB we are transferring for */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 150 | struct wa_seg **seg; /* transfer segments */ |
| 151 | u8 segs, segs_submitted, segs_done; |
| 152 | unsigned is_inbound:1; |
| 153 | unsigned is_dma:1; |
| 154 | size_t seg_size; |
| 155 | int result; |
| 156 | |
| 157 | gfp_t gfp; /* allocation mask */ |
| 158 | |
| 159 | struct wusb_dev *wusb_dev; /* for activity timestamps */ |
| 160 | }; |
| 161 | |
| 162 | static inline void wa_xfer_init(struct wa_xfer *xfer) |
| 163 | { |
| 164 | kref_init(&xfer->refcnt); |
| 165 | INIT_LIST_HEAD(&xfer->list_node); |
| 166 | spin_lock_init(&xfer->lock); |
| 167 | } |
| 168 | |
| 169 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 170 | * Destroy a transfer structure |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 171 | * |
Thomas Pugliese | 79731cb | 2013-08-15 14:37:42 -0500 | [diff] [blame] | 172 | * Note that freeing xfer->seg[cnt]->urb will free the containing |
| 173 | * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs. |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 174 | */ |
| 175 | static void wa_xfer_destroy(struct kref *_xfer) |
| 176 | { |
| 177 | struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt); |
| 178 | if (xfer->seg) { |
| 179 | unsigned cnt; |
| 180 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
Thomas Pugliese | 79731cb | 2013-08-15 14:37:42 -0500 | [diff] [blame] | 181 | usb_free_urb(xfer->seg[cnt]->dto_urb); |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 182 | usb_free_urb(&xfer->seg[cnt]->tr_urb); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | kfree(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void wa_xfer_get(struct wa_xfer *xfer) |
| 189 | { |
| 190 | kref_get(&xfer->refcnt); |
| 191 | } |
| 192 | |
| 193 | static void wa_xfer_put(struct wa_xfer *xfer) |
| 194 | { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 195 | kref_put(&xfer->refcnt, wa_xfer_destroy); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * xfer is referenced |
| 200 | * |
| 201 | * xfer->lock has to be unlocked |
| 202 | * |
| 203 | * We take xfer->lock for setting the result; this is a barrier |
| 204 | * against drivers/usb/core/hcd.c:unlink1() being called after we call |
| 205 | * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a |
| 206 | * reference to the transfer. |
| 207 | */ |
| 208 | static void wa_xfer_giveback(struct wa_xfer *xfer) |
| 209 | { |
| 210 | unsigned long flags; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 211 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 212 | spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags); |
| 213 | list_del_init(&xfer->list_node); |
| 214 | spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags); |
| 215 | /* FIXME: segmentation broken -- kills DWA */ |
| 216 | wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result); |
| 217 | wa_put(xfer->wa); |
| 218 | wa_xfer_put(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* |
| 222 | * xfer is referenced |
| 223 | * |
| 224 | * xfer->lock has to be unlocked |
| 225 | */ |
| 226 | static void wa_xfer_completion(struct wa_xfer *xfer) |
| 227 | { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 228 | if (xfer->wusb_dev) |
| 229 | wusb_dev_put(xfer->wusb_dev); |
| 230 | rpipe_put(xfer->ep->hcpriv); |
| 231 | wa_xfer_giveback(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * If transfer is done, wrap it up and return true |
| 236 | * |
| 237 | * xfer->lock has to be locked |
| 238 | */ |
| 239 | static unsigned __wa_xfer_is_done(struct wa_xfer *xfer) |
| 240 | { |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 241 | struct device *dev = &xfer->wa->usb_iface->dev; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 242 | unsigned result, cnt; |
| 243 | struct wa_seg *seg; |
| 244 | struct urb *urb = xfer->urb; |
| 245 | unsigned found_short = 0; |
| 246 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 247 | result = xfer->segs_done == xfer->segs_submitted; |
| 248 | if (result == 0) |
| 249 | goto out; |
| 250 | urb->actual_length = 0; |
| 251 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
| 252 | seg = xfer->seg[cnt]; |
| 253 | switch (seg->status) { |
| 254 | case WA_SEG_DONE: |
| 255 | if (found_short && seg->result > 0) { |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 256 | dev_dbg(dev, "xfer %p#%u: bad short segments (%zu)\n", |
| 257 | xfer, cnt, seg->result); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 258 | urb->status = -EINVAL; |
| 259 | goto out; |
| 260 | } |
| 261 | urb->actual_length += seg->result; |
| 262 | if (seg->result < xfer->seg_size |
| 263 | && cnt != xfer->segs-1) |
| 264 | found_short = 1; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 265 | dev_dbg(dev, "xfer %p#%u: DONE short %d " |
| 266 | "result %zu urb->actual_length %d\n", |
| 267 | xfer, seg->index, found_short, seg->result, |
| 268 | urb->actual_length); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 269 | break; |
| 270 | case WA_SEG_ERROR: |
| 271 | xfer->result = seg->result; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 272 | dev_dbg(dev, "xfer %p#%u: ERROR result %zu\n", |
| 273 | xfer, seg->index, seg->result); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 274 | goto out; |
| 275 | case WA_SEG_ABORTED: |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 276 | dev_dbg(dev, "xfer %p#%u ABORTED: result %d\n", |
| 277 | xfer, seg->index, urb->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 278 | xfer->result = urb->status; |
| 279 | goto out; |
| 280 | default: |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 281 | dev_warn(dev, "xfer %p#%u: is_done bad state %d\n", |
| 282 | xfer, cnt, seg->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 283 | xfer->result = -EINVAL; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 284 | goto out; |
| 285 | } |
| 286 | } |
| 287 | xfer->result = 0; |
| 288 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 289 | return result; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Initialize a transfer's ID |
| 294 | * |
| 295 | * We need to use a sequential number; if we use the pointer or the |
| 296 | * hash of the pointer, it can repeat over sequential transfers and |
| 297 | * then it will confuse the HWA....wonder why in hell they put a 32 |
| 298 | * bit handle in there then. |
| 299 | */ |
| 300 | static void wa_xfer_id_init(struct wa_xfer *xfer) |
| 301 | { |
| 302 | xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Return the xfer's ID associated with xfer |
| 307 | * |
| 308 | * Need to generate a |
| 309 | */ |
| 310 | static u32 wa_xfer_id(struct wa_xfer *xfer) |
| 311 | { |
| 312 | return xfer->id; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Search for a transfer list ID on the HCD's URB list |
| 317 | * |
| 318 | * For 32 bit architectures, we use the pointer itself; for 64 bits, a |
| 319 | * 32-bit hash of the pointer. |
| 320 | * |
| 321 | * @returns NULL if not found. |
| 322 | */ |
| 323 | static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id) |
| 324 | { |
| 325 | unsigned long flags; |
| 326 | struct wa_xfer *xfer_itr; |
| 327 | spin_lock_irqsave(&wa->xfer_list_lock, flags); |
| 328 | list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) { |
| 329 | if (id == xfer_itr->id) { |
| 330 | wa_xfer_get(xfer_itr); |
| 331 | goto out; |
| 332 | } |
| 333 | } |
| 334 | xfer_itr = NULL; |
| 335 | out: |
| 336 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags); |
| 337 | return xfer_itr; |
| 338 | } |
| 339 | |
| 340 | struct wa_xfer_abort_buffer { |
| 341 | struct urb urb; |
| 342 | struct wa_xfer_abort cmd; |
| 343 | }; |
| 344 | |
| 345 | static void __wa_xfer_abort_cb(struct urb *urb) |
| 346 | { |
| 347 | struct wa_xfer_abort_buffer *b = urb->context; |
| 348 | usb_put_urb(&b->urb); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Aborts an ongoing transaction |
| 353 | * |
| 354 | * Assumes the transfer is referenced and locked and in a submitted |
| 355 | * state (mainly that there is an endpoint/rpipe assigned). |
| 356 | * |
| 357 | * The callback (see above) does nothing but freeing up the data by |
| 358 | * putting the URB. Because the URB is allocated at the head of the |
| 359 | * struct, the whole space we allocated is kfreed. |
| 360 | * |
| 361 | * We'll get an 'aborted transaction' xfer result on DTI, that'll |
| 362 | * politely ignore because at this point the transaction has been |
| 363 | * marked as aborted already. |
| 364 | */ |
| 365 | static void __wa_xfer_abort(struct wa_xfer *xfer) |
| 366 | { |
| 367 | int result; |
| 368 | struct device *dev = &xfer->wa->usb_iface->dev; |
| 369 | struct wa_xfer_abort_buffer *b; |
| 370 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 371 | |
| 372 | b = kmalloc(sizeof(*b), GFP_ATOMIC); |
| 373 | if (b == NULL) |
| 374 | goto error_kmalloc; |
| 375 | b->cmd.bLength = sizeof(b->cmd); |
| 376 | b->cmd.bRequestType = WA_XFER_ABORT; |
| 377 | b->cmd.wRPipe = rpipe->descr.wRPipeIndex; |
| 378 | b->cmd.dwTransferID = wa_xfer_id(xfer); |
| 379 | |
| 380 | usb_init_urb(&b->urb); |
| 381 | usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev, |
| 382 | usb_sndbulkpipe(xfer->wa->usb_dev, |
| 383 | xfer->wa->dto_epd->bEndpointAddress), |
| 384 | &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b); |
| 385 | result = usb_submit_urb(&b->urb, GFP_ATOMIC); |
| 386 | if (result < 0) |
| 387 | goto error_submit; |
| 388 | return; /* callback frees! */ |
| 389 | |
| 390 | |
| 391 | error_submit: |
| 392 | if (printk_ratelimit()) |
| 393 | dev_err(dev, "xfer %p: Can't submit abort request: %d\n", |
| 394 | xfer, result); |
| 395 | kfree(b); |
| 396 | error_kmalloc: |
| 397 | return; |
| 398 | |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * |
| 403 | * @returns < 0 on error, transfer segment request size if ok |
| 404 | */ |
| 405 | static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer, |
| 406 | enum wa_xfer_type *pxfer_type) |
| 407 | { |
| 408 | ssize_t result; |
| 409 | struct device *dev = &xfer->wa->usb_iface->dev; |
| 410 | size_t maxpktsize; |
| 411 | struct urb *urb = xfer->urb; |
| 412 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 413 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 414 | switch (rpipe->descr.bmAttribute & 0x3) { |
| 415 | case USB_ENDPOINT_XFER_CONTROL: |
| 416 | *pxfer_type = WA_XFER_TYPE_CTL; |
| 417 | result = sizeof(struct wa_xfer_ctl); |
| 418 | break; |
| 419 | case USB_ENDPOINT_XFER_INT: |
| 420 | case USB_ENDPOINT_XFER_BULK: |
| 421 | *pxfer_type = WA_XFER_TYPE_BI; |
| 422 | result = sizeof(struct wa_xfer_bi); |
| 423 | break; |
| 424 | case USB_ENDPOINT_XFER_ISOC: |
| 425 | dev_err(dev, "FIXME: ISOC not implemented\n"); |
| 426 | result = -ENOSYS; |
| 427 | goto error; |
| 428 | default: |
| 429 | /* never happens */ |
| 430 | BUG(); |
| 431 | result = -EINVAL; /* shut gcc up */ |
| 432 | }; |
| 433 | xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0; |
| 434 | xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0; |
| 435 | xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks) |
| 436 | * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1); |
| 437 | /* Compute the segment size and make sure it is a multiple of |
| 438 | * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of |
| 439 | * a check (FIXME) */ |
| 440 | maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize); |
| 441 | if (xfer->seg_size < maxpktsize) { |
| 442 | dev_err(dev, "HW BUG? seg_size %zu smaller than maxpktsize " |
| 443 | "%zu\n", xfer->seg_size, maxpktsize); |
| 444 | result = -EINVAL; |
| 445 | goto error; |
| 446 | } |
| 447 | xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 448 | xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length, xfer->seg_size); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 449 | if (xfer->segs >= WA_SEGS_MAX) { |
| 450 | dev_err(dev, "BUG? ops, number of segments %d bigger than %d\n", |
| 451 | (int)(urb->transfer_buffer_length / xfer->seg_size), |
| 452 | WA_SEGS_MAX); |
| 453 | result = -EINVAL; |
| 454 | goto error; |
| 455 | } |
| 456 | if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL) |
| 457 | xfer->segs = 1; |
| 458 | error: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 459 | return result; |
| 460 | } |
| 461 | |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 462 | /* Fill in the common request header and xfer-type specific data. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 463 | static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer, |
| 464 | struct wa_xfer_hdr *xfer_hdr0, |
| 465 | enum wa_xfer_type xfer_type, |
| 466 | size_t xfer_hdr_size) |
| 467 | { |
| 468 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 469 | |
| 470 | xfer_hdr0 = &xfer->seg[0]->xfer_hdr; |
| 471 | xfer_hdr0->bLength = xfer_hdr_size; |
| 472 | xfer_hdr0->bRequestType = xfer_type; |
| 473 | xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex; |
| 474 | xfer_hdr0->dwTransferID = wa_xfer_id(xfer); |
| 475 | xfer_hdr0->bTransferSegment = 0; |
| 476 | switch (xfer_type) { |
| 477 | case WA_XFER_TYPE_CTL: { |
| 478 | struct wa_xfer_ctl *xfer_ctl = |
| 479 | container_of(xfer_hdr0, struct wa_xfer_ctl, hdr); |
| 480 | xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 481 | memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet, |
| 482 | sizeof(xfer_ctl->baSetupData)); |
| 483 | break; |
| 484 | } |
| 485 | case WA_XFER_TYPE_BI: |
| 486 | break; |
| 487 | case WA_XFER_TYPE_ISO: |
| 488 | printk(KERN_ERR "FIXME: ISOC not implemented\n"); |
| 489 | default: |
| 490 | BUG(); |
| 491 | }; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Callback for the OUT data phase of the segment request |
| 496 | * |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 497 | * Check wa_seg_tr_cb(); most comments also apply here because this |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 498 | * function does almost the same thing and they work closely |
| 499 | * together. |
| 500 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 501 | * If the seg request has failed but this DTO phase has succeeded, |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 502 | * wa_seg_tr_cb() has already failed the segment and moved the |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 503 | * status to WA_SEG_ERROR, so this will go through 'case 0' and |
| 504 | * effectively do nothing. |
| 505 | */ |
| 506 | static void wa_seg_dto_cb(struct urb *urb) |
| 507 | { |
| 508 | struct wa_seg *seg = urb->context; |
| 509 | struct wa_xfer *xfer = seg->xfer; |
| 510 | struct wahc *wa; |
| 511 | struct device *dev; |
| 512 | struct wa_rpipe *rpipe; |
| 513 | unsigned long flags; |
| 514 | unsigned rpipe_ready = 0; |
| 515 | u8 done = 0; |
| 516 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 517 | switch (urb->status) { |
| 518 | case 0: |
| 519 | spin_lock_irqsave(&xfer->lock, flags); |
| 520 | wa = xfer->wa; |
| 521 | dev = &wa->usb_iface->dev; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 522 | dev_dbg(dev, "xfer %p#%u: data out done (%d bytes)\n", |
| 523 | xfer, seg->index, urb->actual_length); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 524 | if (seg->status < WA_SEG_PENDING) |
| 525 | seg->status = WA_SEG_PENDING; |
| 526 | seg->result = urb->actual_length; |
| 527 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 528 | break; |
| 529 | case -ECONNRESET: /* URB unlinked; no need to do anything */ |
| 530 | case -ENOENT: /* as it was done by the who unlinked us */ |
| 531 | break; |
| 532 | default: /* Other errors ... */ |
| 533 | spin_lock_irqsave(&xfer->lock, flags); |
| 534 | wa = xfer->wa; |
| 535 | dev = &wa->usb_iface->dev; |
| 536 | rpipe = xfer->ep->hcpriv; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 537 | dev_dbg(dev, "xfer %p#%u: data out error %d\n", |
| 538 | xfer, seg->index, urb->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 539 | if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS, |
| 540 | EDC_ERROR_TIMEFRAME)){ |
| 541 | dev_err(dev, "DTO: URB max acceptable errors " |
| 542 | "exceeded, resetting device\n"); |
| 543 | wa_reset_all(wa); |
| 544 | } |
| 545 | if (seg->status != WA_SEG_ERROR) { |
| 546 | seg->status = WA_SEG_ERROR; |
| 547 | seg->result = urb->status; |
| 548 | xfer->segs_done++; |
| 549 | __wa_xfer_abort(xfer); |
| 550 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 551 | done = __wa_xfer_is_done(xfer); |
| 552 | } |
| 553 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 554 | if (done) |
| 555 | wa_xfer_completion(xfer); |
| 556 | if (rpipe_ready) |
| 557 | wa_xfer_delayed_run(rpipe); |
| 558 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Callback for the segment request |
| 563 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 564 | * If successful transition state (unless already transitioned or |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 565 | * outbound transfer); otherwise, take a note of the error, mark this |
| 566 | * segment done and try completion. |
| 567 | * |
| 568 | * Note we don't access until we are sure that the transfer hasn't |
| 569 | * been cancelled (ECONNRESET, ENOENT), which could mean that |
| 570 | * seg->xfer could be already gone. |
| 571 | * |
| 572 | * We have to check before setting the status to WA_SEG_PENDING |
| 573 | * because sometimes the xfer result callback arrives before this |
| 574 | * callback (geeeeeeze), so it might happen that we are already in |
| 575 | * another state. As well, we don't set it if the transfer is inbound, |
| 576 | * as in that case, wa_seg_dto_cb will do it when the OUT data phase |
| 577 | * finishes. |
| 578 | */ |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 579 | static void wa_seg_tr_cb(struct urb *urb) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 580 | { |
| 581 | struct wa_seg *seg = urb->context; |
| 582 | struct wa_xfer *xfer = seg->xfer; |
| 583 | struct wahc *wa; |
| 584 | struct device *dev; |
| 585 | struct wa_rpipe *rpipe; |
| 586 | unsigned long flags; |
| 587 | unsigned rpipe_ready; |
| 588 | u8 done = 0; |
| 589 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 590 | switch (urb->status) { |
| 591 | case 0: |
| 592 | spin_lock_irqsave(&xfer->lock, flags); |
| 593 | wa = xfer->wa; |
| 594 | dev = &wa->usb_iface->dev; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 595 | dev_dbg(dev, "xfer %p#%u: request done\n", xfer, seg->index); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 596 | if (xfer->is_inbound && seg->status < WA_SEG_PENDING) |
| 597 | seg->status = WA_SEG_PENDING; |
| 598 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 599 | break; |
| 600 | case -ECONNRESET: /* URB unlinked; no need to do anything */ |
| 601 | case -ENOENT: /* as it was done by the who unlinked us */ |
| 602 | break; |
| 603 | default: /* Other errors ... */ |
| 604 | spin_lock_irqsave(&xfer->lock, flags); |
| 605 | wa = xfer->wa; |
| 606 | dev = &wa->usb_iface->dev; |
| 607 | rpipe = xfer->ep->hcpriv; |
| 608 | if (printk_ratelimit()) |
| 609 | dev_err(dev, "xfer %p#%u: request error %d\n", |
| 610 | xfer, seg->index, urb->status); |
| 611 | if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS, |
| 612 | EDC_ERROR_TIMEFRAME)){ |
| 613 | dev_err(dev, "DTO: URB max acceptable errors " |
| 614 | "exceeded, resetting device\n"); |
| 615 | wa_reset_all(wa); |
| 616 | } |
| 617 | usb_unlink_urb(seg->dto_urb); |
| 618 | seg->status = WA_SEG_ERROR; |
| 619 | seg->result = urb->status; |
| 620 | xfer->segs_done++; |
| 621 | __wa_xfer_abort(xfer); |
| 622 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 623 | done = __wa_xfer_is_done(xfer); |
| 624 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 625 | if (done) |
| 626 | wa_xfer_completion(xfer); |
| 627 | if (rpipe_ready) |
| 628 | wa_xfer_delayed_run(rpipe); |
| 629 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 632 | /* allocate an SG list to store bytes_to_transfer bytes and copy the |
| 633 | * subset of the in_sg that matches the buffer subset |
| 634 | * we are about to transfer. */ |
| 635 | static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg, |
| 636 | const unsigned int bytes_transferred, |
| 637 | const unsigned int bytes_to_transfer, unsigned int *out_num_sgs) |
| 638 | { |
| 639 | struct scatterlist *out_sg; |
| 640 | unsigned int bytes_processed = 0, offset_into_current_page_data = 0, |
| 641 | nents; |
| 642 | struct scatterlist *current_xfer_sg = in_sg; |
| 643 | struct scatterlist *current_seg_sg, *last_seg_sg; |
| 644 | |
| 645 | /* skip previously transferred pages. */ |
| 646 | while ((current_xfer_sg) && |
| 647 | (bytes_processed < bytes_transferred)) { |
| 648 | bytes_processed += current_xfer_sg->length; |
| 649 | |
| 650 | /* advance the sg if current segment starts on or past the |
| 651 | next page. */ |
| 652 | if (bytes_processed <= bytes_transferred) |
| 653 | current_xfer_sg = sg_next(current_xfer_sg); |
| 654 | } |
| 655 | |
| 656 | /* the data for the current segment starts in current_xfer_sg. |
| 657 | calculate the offset. */ |
| 658 | if (bytes_processed > bytes_transferred) { |
| 659 | offset_into_current_page_data = current_xfer_sg->length - |
| 660 | (bytes_processed - bytes_transferred); |
| 661 | } |
| 662 | |
| 663 | /* calculate the number of pages needed by this segment. */ |
| 664 | nents = DIV_ROUND_UP((bytes_to_transfer + |
| 665 | offset_into_current_page_data + |
| 666 | current_xfer_sg->offset), |
| 667 | PAGE_SIZE); |
| 668 | |
| 669 | out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC); |
| 670 | if (out_sg) { |
| 671 | sg_init_table(out_sg, nents); |
| 672 | |
| 673 | /* copy the portion of the incoming SG that correlates to the |
| 674 | * data to be transferred by this segment to the segment SG. */ |
| 675 | last_seg_sg = current_seg_sg = out_sg; |
| 676 | bytes_processed = 0; |
| 677 | |
| 678 | /* reset nents and calculate the actual number of sg entries |
| 679 | needed. */ |
| 680 | nents = 0; |
| 681 | while ((bytes_processed < bytes_to_transfer) && |
| 682 | current_seg_sg && current_xfer_sg) { |
| 683 | unsigned int page_len = min((current_xfer_sg->length - |
| 684 | offset_into_current_page_data), |
| 685 | (bytes_to_transfer - bytes_processed)); |
| 686 | |
| 687 | sg_set_page(current_seg_sg, sg_page(current_xfer_sg), |
| 688 | page_len, |
| 689 | current_xfer_sg->offset + |
| 690 | offset_into_current_page_data); |
| 691 | |
| 692 | bytes_processed += page_len; |
| 693 | |
| 694 | last_seg_sg = current_seg_sg; |
| 695 | current_seg_sg = sg_next(current_seg_sg); |
| 696 | current_xfer_sg = sg_next(current_xfer_sg); |
| 697 | |
| 698 | /* only the first page may require additional offset. */ |
| 699 | offset_into_current_page_data = 0; |
| 700 | nents++; |
| 701 | } |
| 702 | |
| 703 | /* update num_sgs and terminate the list since we may have |
| 704 | * concatenated pages. */ |
| 705 | sg_mark_end(last_seg_sg); |
| 706 | *out_num_sgs = nents; |
| 707 | } |
| 708 | |
| 709 | return out_sg; |
| 710 | } |
| 711 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 712 | /* |
| 713 | * Allocate the segs array and initialize each of them |
| 714 | * |
| 715 | * The segments are freed by wa_xfer_destroy() when the xfer use count |
| 716 | * drops to zero; however, because each segment is given the same life |
| 717 | * cycle as the USB URB it contains, it is actually freed by |
| 718 | * usb_put_urb() on the contained USB URB (twisted, eh?). |
| 719 | */ |
| 720 | static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size) |
| 721 | { |
| 722 | int result, cnt; |
| 723 | size_t alloc_size = sizeof(*xfer->seg[0]) |
| 724 | - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size; |
| 725 | struct usb_device *usb_dev = xfer->wa->usb_dev; |
| 726 | const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd; |
| 727 | struct wa_seg *seg; |
| 728 | size_t buf_itr, buf_size, buf_itr_size; |
| 729 | |
| 730 | result = -ENOMEM; |
David Vrabel | 92c4d9b | 2008-10-15 14:50:10 +0100 | [diff] [blame] | 731 | xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 732 | if (xfer->seg == NULL) |
| 733 | goto error_segs_kzalloc; |
| 734 | buf_itr = 0; |
| 735 | buf_size = xfer->urb->transfer_buffer_length; |
| 736 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 737 | seg = xfer->seg[cnt] = kmalloc(alloc_size, GFP_ATOMIC); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 738 | if (seg == NULL) |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 739 | goto error_seg_kmalloc; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 740 | wa_seg_init(seg); |
| 741 | seg->xfer = xfer; |
| 742 | seg->index = cnt; |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 743 | usb_fill_bulk_urb(&seg->tr_urb, usb_dev, |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 744 | usb_sndbulkpipe(usb_dev, |
| 745 | dto_epd->bEndpointAddress), |
| 746 | &seg->xfer_hdr, xfer_hdr_size, |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 747 | wa_seg_tr_cb, seg); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 748 | buf_itr_size = min(buf_size, xfer->seg_size); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 749 | if (xfer->is_inbound == 0 && buf_size > 0) { |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 750 | /* outbound data. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 751 | seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 752 | if (seg->dto_urb == NULL) |
| 753 | goto error_dto_alloc; |
| 754 | usb_fill_bulk_urb( |
| 755 | seg->dto_urb, usb_dev, |
| 756 | usb_sndbulkpipe(usb_dev, |
| 757 | dto_epd->bEndpointAddress), |
| 758 | NULL, 0, wa_seg_dto_cb, seg); |
| 759 | if (xfer->is_dma) { |
| 760 | seg->dto_urb->transfer_dma = |
| 761 | xfer->urb->transfer_dma + buf_itr; |
| 762 | seg->dto_urb->transfer_flags |= |
| 763 | URB_NO_TRANSFER_DMA_MAP; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 764 | seg->dto_urb->transfer_buffer = NULL; |
| 765 | seg->dto_urb->sg = NULL; |
| 766 | seg->dto_urb->num_sgs = 0; |
| 767 | } else { |
| 768 | /* do buffer or SG processing. */ |
| 769 | seg->dto_urb->transfer_flags &= |
| 770 | ~URB_NO_TRANSFER_DMA_MAP; |
| 771 | /* this should always be 0 before a resubmit. */ |
| 772 | seg->dto_urb->num_mapped_sgs = 0; |
| 773 | |
| 774 | if (xfer->urb->transfer_buffer) { |
| 775 | seg->dto_urb->transfer_buffer = |
| 776 | xfer->urb->transfer_buffer + |
| 777 | buf_itr; |
| 778 | seg->dto_urb->sg = NULL; |
| 779 | seg->dto_urb->num_sgs = 0; |
| 780 | } else { |
| 781 | /* allocate an SG list to store seg_size |
| 782 | bytes and copy the subset of the |
| 783 | xfer->urb->sg that matches the |
| 784 | buffer subset we are about to read. |
| 785 | */ |
| 786 | seg->dto_urb->sg = |
| 787 | wa_xfer_create_subset_sg( |
| 788 | xfer->urb->sg, |
| 789 | buf_itr, buf_itr_size, |
| 790 | &(seg->dto_urb->num_sgs)); |
| 791 | |
| 792 | if (!(seg->dto_urb->sg)) { |
| 793 | seg->dto_urb->num_sgs = 0; |
| 794 | goto error_sg_alloc; |
| 795 | } |
| 796 | |
| 797 | seg->dto_urb->transfer_buffer = NULL; |
| 798 | } |
| 799 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 800 | seg->dto_urb->transfer_buffer_length = buf_itr_size; |
| 801 | } |
| 802 | seg->status = WA_SEG_READY; |
| 803 | buf_itr += buf_itr_size; |
| 804 | buf_size -= buf_itr_size; |
| 805 | } |
| 806 | return 0; |
| 807 | |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 808 | error_sg_alloc: |
Thomas Pugliese | 11b1bf8 | 2013-08-15 14:37:41 -0500 | [diff] [blame] | 809 | usb_free_urb(xfer->seg[cnt]->dto_urb); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 810 | error_dto_alloc: |
| 811 | kfree(xfer->seg[cnt]); |
| 812 | cnt--; |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 813 | error_seg_kmalloc: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 814 | /* use the fact that cnt is left at were it failed */ |
Dan Carpenter | f07af4b | 2013-02-01 15:53:34 +0300 | [diff] [blame] | 815 | for (; cnt >= 0; cnt--) { |
Thomas Pugliese | 11b1bf8 | 2013-08-15 14:37:41 -0500 | [diff] [blame] | 816 | if (xfer->seg[cnt] && xfer->is_inbound == 0) { |
Dan Carpenter | f07af4b | 2013-02-01 15:53:34 +0300 | [diff] [blame] | 817 | usb_free_urb(xfer->seg[cnt]->dto_urb); |
Thomas Pugliese | 11b1bf8 | 2013-08-15 14:37:41 -0500 | [diff] [blame] | 818 | kfree(xfer->seg[cnt]->dto_urb->sg); |
| 819 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 820 | kfree(xfer->seg[cnt]); |
| 821 | } |
| 822 | error_segs_kzalloc: |
| 823 | return result; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Allocates all the stuff needed to submit a transfer |
| 828 | * |
| 829 | * Breaks the whole data buffer in a list of segments, each one has a |
| 830 | * structure allocated to it and linked in xfer->seg[index] |
| 831 | * |
| 832 | * FIXME: merge setup_segs() and the last part of this function, no |
| 833 | * need to do two for loops when we could run everything in a |
| 834 | * single one |
| 835 | */ |
| 836 | static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb) |
| 837 | { |
| 838 | int result; |
| 839 | struct device *dev = &xfer->wa->usb_iface->dev; |
| 840 | enum wa_xfer_type xfer_type = 0; /* shut up GCC */ |
| 841 | size_t xfer_hdr_size, cnt, transfer_size; |
| 842 | struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr; |
| 843 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 844 | result = __wa_xfer_setup_sizes(xfer, &xfer_type); |
| 845 | if (result < 0) |
| 846 | goto error_setup_sizes; |
| 847 | xfer_hdr_size = result; |
| 848 | result = __wa_xfer_setup_segs(xfer, xfer_hdr_size); |
| 849 | if (result < 0) { |
| 850 | dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n", |
| 851 | xfer, xfer->segs, result); |
| 852 | goto error_setup_segs; |
| 853 | } |
| 854 | /* Fill the first header */ |
| 855 | xfer_hdr0 = &xfer->seg[0]->xfer_hdr; |
| 856 | wa_xfer_id_init(xfer); |
| 857 | __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size); |
| 858 | |
| 859 | /* Fill remainig headers */ |
| 860 | xfer_hdr = xfer_hdr0; |
| 861 | transfer_size = urb->transfer_buffer_length; |
| 862 | xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ? |
| 863 | xfer->seg_size : transfer_size; |
| 864 | transfer_size -= xfer->seg_size; |
| 865 | for (cnt = 1; cnt < xfer->segs; cnt++) { |
| 866 | xfer_hdr = &xfer->seg[cnt]->xfer_hdr; |
| 867 | memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size); |
| 868 | xfer_hdr->bTransferSegment = cnt; |
| 869 | xfer_hdr->dwTransferLength = transfer_size > xfer->seg_size ? |
| 870 | cpu_to_le32(xfer->seg_size) |
| 871 | : cpu_to_le32(transfer_size); |
| 872 | xfer->seg[cnt]->status = WA_SEG_READY; |
| 873 | transfer_size -= xfer->seg_size; |
| 874 | } |
| 875 | xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */ |
| 876 | result = 0; |
| 877 | error_setup_segs: |
| 878 | error_setup_sizes: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 879 | return result; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * |
| 884 | * |
| 885 | * rpipe->seg_lock is held! |
| 886 | */ |
| 887 | static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer, |
| 888 | struct wa_seg *seg) |
| 889 | { |
| 890 | int result; |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 891 | /* submit the transfer request. */ |
| 892 | result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 893 | if (result < 0) { |
| 894 | printk(KERN_ERR "xfer %p#%u: REQ submit failed: %d\n", |
| 895 | xfer, seg->index, result); |
| 896 | goto error_seg_submit; |
| 897 | } |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 898 | /* submit the out data if this is an out request. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 899 | if (seg->dto_urb) { |
| 900 | result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC); |
| 901 | if (result < 0) { |
| 902 | printk(KERN_ERR "xfer %p#%u: DTO submit failed: %d\n", |
| 903 | xfer, seg->index, result); |
| 904 | goto error_dto_submit; |
| 905 | } |
| 906 | } |
| 907 | seg->status = WA_SEG_SUBMITTED; |
| 908 | rpipe_avail_dec(rpipe); |
| 909 | return 0; |
| 910 | |
| 911 | error_dto_submit: |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 912 | usb_unlink_urb(&seg->tr_urb); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 913 | error_seg_submit: |
| 914 | seg->status = WA_SEG_ERROR; |
| 915 | seg->result = result; |
| 916 | return result; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Execute more queued request segments until the maximum concurrent allowed |
| 921 | * |
| 922 | * The ugly unlock/lock sequence on the error path is needed as the |
| 923 | * xfer->lock normally nests the seg_lock and not viceversa. |
| 924 | * |
| 925 | */ |
| 926 | static void wa_xfer_delayed_run(struct wa_rpipe *rpipe) |
| 927 | { |
| 928 | int result; |
| 929 | struct device *dev = &rpipe->wa->usb_iface->dev; |
| 930 | struct wa_seg *seg; |
| 931 | struct wa_xfer *xfer; |
| 932 | unsigned long flags; |
| 933 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 934 | spin_lock_irqsave(&rpipe->seg_lock, flags); |
| 935 | while (atomic_read(&rpipe->segs_available) > 0 |
| 936 | && !list_empty(&rpipe->seg_list)) { |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 937 | seg = list_first_entry(&(rpipe->seg_list), struct wa_seg, |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 938 | list_node); |
| 939 | list_del(&seg->list_node); |
| 940 | xfer = seg->xfer; |
| 941 | result = __wa_seg_submit(rpipe, xfer, seg); |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 942 | dev_dbg(dev, "xfer %p#%u submitted from delayed [%d segments available] %d\n", |
| 943 | xfer, seg->index, atomic_read(&rpipe->segs_available), result); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 944 | if (unlikely(result < 0)) { |
| 945 | spin_unlock_irqrestore(&rpipe->seg_lock, flags); |
| 946 | spin_lock_irqsave(&xfer->lock, flags); |
| 947 | __wa_xfer_abort(xfer); |
| 948 | xfer->segs_done++; |
| 949 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 950 | spin_lock_irqsave(&rpipe->seg_lock, flags); |
| 951 | } |
| 952 | } |
| 953 | spin_unlock_irqrestore(&rpipe->seg_lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* |
| 957 | * |
| 958 | * xfer->lock is taken |
| 959 | * |
| 960 | * On failure submitting we just stop submitting and return error; |
| 961 | * wa_urb_enqueue_b() will execute the completion path |
| 962 | */ |
| 963 | static int __wa_xfer_submit(struct wa_xfer *xfer) |
| 964 | { |
| 965 | int result; |
| 966 | struct wahc *wa = xfer->wa; |
| 967 | struct device *dev = &wa->usb_iface->dev; |
| 968 | unsigned cnt; |
| 969 | struct wa_seg *seg; |
| 970 | unsigned long flags; |
| 971 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 972 | size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests); |
| 973 | u8 available; |
| 974 | u8 empty; |
| 975 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 976 | spin_lock_irqsave(&wa->xfer_list_lock, flags); |
| 977 | list_add_tail(&xfer->list_node, &wa->xfer_list); |
| 978 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags); |
| 979 | |
| 980 | BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests); |
| 981 | result = 0; |
| 982 | spin_lock_irqsave(&rpipe->seg_lock, flags); |
| 983 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
| 984 | available = atomic_read(&rpipe->segs_available); |
| 985 | empty = list_empty(&rpipe->seg_list); |
| 986 | seg = xfer->seg[cnt]; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 987 | dev_dbg(dev, "xfer %p#%u: available %u empty %u (%s)\n", |
| 988 | xfer, cnt, available, empty, |
| 989 | available == 0 || !empty ? "delayed" : "submitted"); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 990 | if (available == 0 || !empty) { |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 991 | dev_dbg(dev, "xfer %p#%u: delayed\n", xfer, cnt); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 992 | seg->status = WA_SEG_DELAYED; |
| 993 | list_add_tail(&seg->list_node, &rpipe->seg_list); |
| 994 | } else { |
| 995 | result = __wa_seg_submit(rpipe, xfer, seg); |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 996 | if (result < 0) { |
| 997 | __wa_xfer_abort(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 998 | goto error_seg_submit; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 999 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1000 | } |
| 1001 | xfer->segs_submitted++; |
| 1002 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1003 | error_seg_submit: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1004 | spin_unlock_irqrestore(&rpipe->seg_lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1005 | return result; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * Second part of a URB/transfer enqueuement |
| 1010 | * |
| 1011 | * Assumes this comes from wa_urb_enqueue() [maybe through |
| 1012 | * wa_urb_enqueue_run()]. At this point: |
| 1013 | * |
| 1014 | * xfer->wa filled and refcounted |
| 1015 | * xfer->ep filled with rpipe refcounted if |
| 1016 | * delayed == 0 |
| 1017 | * xfer->urb filled and refcounted (this is the case when called |
| 1018 | * from wa_urb_enqueue() as we come from usb_submit_urb() |
| 1019 | * and when called by wa_urb_enqueue_run(), as we took an |
| 1020 | * extra ref dropped by _run() after we return). |
| 1021 | * xfer->gfp filled |
| 1022 | * |
| 1023 | * If we fail at __wa_xfer_submit(), then we just check if we are done |
| 1024 | * and if so, we run the completion procedure. However, if we are not |
| 1025 | * yet done, we do nothing and wait for the completion handlers from |
| 1026 | * the submitted URBs or from the xfer-result path to kick in. If xfer |
| 1027 | * result never kicks in, the xfer will timeout from the USB code and |
| 1028 | * dequeue() will be called. |
| 1029 | */ |
| 1030 | static void wa_urb_enqueue_b(struct wa_xfer *xfer) |
| 1031 | { |
| 1032 | int result; |
| 1033 | unsigned long flags; |
| 1034 | struct urb *urb = xfer->urb; |
| 1035 | struct wahc *wa = xfer->wa; |
| 1036 | struct wusbhc *wusbhc = wa->wusb; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1037 | struct wusb_dev *wusb_dev; |
| 1038 | unsigned done; |
| 1039 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1040 | result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp); |
| 1041 | if (result < 0) |
| 1042 | goto error_rpipe_get; |
| 1043 | result = -ENODEV; |
| 1044 | /* FIXME: segmentation broken -- kills DWA */ |
| 1045 | mutex_lock(&wusbhc->mutex); /* get a WUSB dev */ |
Jiri Slaby | 49fa092 | 2009-03-11 21:47:40 +0100 | [diff] [blame] | 1046 | if (urb->dev == NULL) { |
| 1047 | mutex_unlock(&wusbhc->mutex); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1048 | goto error_dev_gone; |
Jiri Slaby | 49fa092 | 2009-03-11 21:47:40 +0100 | [diff] [blame] | 1049 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1050 | wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev); |
| 1051 | if (wusb_dev == NULL) { |
| 1052 | mutex_unlock(&wusbhc->mutex); |
| 1053 | goto error_dev_gone; |
| 1054 | } |
| 1055 | mutex_unlock(&wusbhc->mutex); |
| 1056 | |
| 1057 | spin_lock_irqsave(&xfer->lock, flags); |
| 1058 | xfer->wusb_dev = wusb_dev; |
| 1059 | result = urb->status; |
| 1060 | if (urb->status != -EINPROGRESS) |
| 1061 | goto error_dequeued; |
| 1062 | |
| 1063 | result = __wa_xfer_setup(xfer, urb); |
| 1064 | if (result < 0) |
| 1065 | goto error_xfer_setup; |
| 1066 | result = __wa_xfer_submit(xfer); |
| 1067 | if (result < 0) |
| 1068 | goto error_xfer_submit; |
| 1069 | spin_unlock_irqrestore(&xfer->lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1070 | return; |
| 1071 | |
| 1072 | /* this is basically wa_xfer_completion() broken up wa_xfer_giveback() |
| 1073 | * does a wa_xfer_put() that will call wa_xfer_destroy() and clean |
| 1074 | * upundo setup(). |
| 1075 | */ |
| 1076 | error_xfer_setup: |
| 1077 | error_dequeued: |
| 1078 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1079 | /* FIXME: segmentation broken, kills DWA */ |
| 1080 | if (wusb_dev) |
| 1081 | wusb_dev_put(wusb_dev); |
| 1082 | error_dev_gone: |
| 1083 | rpipe_put(xfer->ep->hcpriv); |
| 1084 | error_rpipe_get: |
| 1085 | xfer->result = result; |
| 1086 | wa_xfer_giveback(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1087 | return; |
| 1088 | |
| 1089 | error_xfer_submit: |
| 1090 | done = __wa_xfer_is_done(xfer); |
| 1091 | xfer->result = result; |
| 1092 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1093 | if (done) |
| 1094 | wa_xfer_completion(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Execute the delayed transfers in the Wire Adapter @wa |
| 1099 | * |
| 1100 | * We need to be careful here, as dequeue() could be called in the |
| 1101 | * middle. That's why we do the whole thing under the |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1102 | * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1103 | * and then checks the list -- so as we would be acquiring in inverse |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1104 | * order, we move the delayed list to a separate list while locked and then |
| 1105 | * submit them without the list lock held. |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1106 | */ |
| 1107 | void wa_urb_enqueue_run(struct work_struct *ws) |
| 1108 | { |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1109 | struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1110 | struct wa_xfer *xfer, *next; |
| 1111 | struct urb *urb; |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1112 | LIST_HEAD(tmp_list); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1113 | |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1114 | /* Create a copy of the wa->xfer_delayed_list while holding the lock */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1115 | spin_lock_irq(&wa->xfer_list_lock); |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1116 | list_cut_position(&tmp_list, &wa->xfer_delayed_list, |
| 1117 | wa->xfer_delayed_list.prev); |
| 1118 | spin_unlock_irq(&wa->xfer_list_lock); |
| 1119 | |
| 1120 | /* |
| 1121 | * enqueue from temp list without list lock held since wa_urb_enqueue_b |
| 1122 | * can take xfer->lock as well as lock mutexes. |
| 1123 | */ |
| 1124 | list_for_each_entry_safe(xfer, next, &tmp_list, list_node) { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1125 | list_del_init(&xfer->list_node); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1126 | |
| 1127 | urb = xfer->urb; |
| 1128 | wa_urb_enqueue_b(xfer); |
| 1129 | usb_put_urb(urb); /* taken when queuing */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1130 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1131 | } |
| 1132 | EXPORT_SYMBOL_GPL(wa_urb_enqueue_run); |
| 1133 | |
| 1134 | /* |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1135 | * Process the errored transfers on the Wire Adapter outside of interrupt. |
| 1136 | */ |
| 1137 | void wa_process_errored_transfers_run(struct work_struct *ws) |
| 1138 | { |
| 1139 | struct wahc *wa = container_of(ws, struct wahc, xfer_error_work); |
| 1140 | struct wa_xfer *xfer, *next; |
| 1141 | LIST_HEAD(tmp_list); |
| 1142 | |
| 1143 | pr_info("%s: Run delayed STALL processing.\n", __func__); |
| 1144 | |
| 1145 | /* Create a copy of the wa->xfer_errored_list while holding the lock */ |
| 1146 | spin_lock_irq(&wa->xfer_list_lock); |
| 1147 | list_cut_position(&tmp_list, &wa->xfer_errored_list, |
| 1148 | wa->xfer_errored_list.prev); |
| 1149 | spin_unlock_irq(&wa->xfer_list_lock); |
| 1150 | |
| 1151 | /* |
| 1152 | * run rpipe_clear_feature_stalled from temp list without list lock |
| 1153 | * held. |
| 1154 | */ |
| 1155 | list_for_each_entry_safe(xfer, next, &tmp_list, list_node) { |
| 1156 | struct usb_host_endpoint *ep; |
| 1157 | unsigned long flags; |
| 1158 | struct wa_rpipe *rpipe; |
| 1159 | |
| 1160 | spin_lock_irqsave(&xfer->lock, flags); |
| 1161 | ep = xfer->ep; |
| 1162 | rpipe = ep->hcpriv; |
| 1163 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1164 | |
| 1165 | /* clear RPIPE feature stalled without holding a lock. */ |
| 1166 | rpipe_clear_feature_stalled(wa, ep); |
| 1167 | |
| 1168 | /* complete the xfer. This removes it from the tmp list. */ |
| 1169 | wa_xfer_completion(xfer); |
| 1170 | |
| 1171 | /* check for work. */ |
| 1172 | wa_xfer_delayed_run(rpipe); |
| 1173 | } |
| 1174 | } |
| 1175 | EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run); |
| 1176 | |
| 1177 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1178 | * Submit a transfer to the Wire Adapter in a delayed way |
| 1179 | * |
| 1180 | * The process of enqueuing involves possible sleeps() [see |
| 1181 | * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are |
| 1182 | * in an atomic section, we defer the enqueue_b() call--else we call direct. |
| 1183 | * |
| 1184 | * @urb: We own a reference to it done by the HCI Linux USB stack that |
| 1185 | * will be given up by calling usb_hcd_giveback_urb() or by |
| 1186 | * returning error from this function -> ergo we don't have to |
| 1187 | * refcount it. |
| 1188 | */ |
| 1189 | int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep, |
| 1190 | struct urb *urb, gfp_t gfp) |
| 1191 | { |
| 1192 | int result; |
| 1193 | struct device *dev = &wa->usb_iface->dev; |
| 1194 | struct wa_xfer *xfer; |
| 1195 | unsigned long my_flags; |
| 1196 | unsigned cant_sleep = irqs_disabled() | in_atomic(); |
| 1197 | |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1198 | if ((urb->transfer_buffer == NULL) |
| 1199 | && (urb->sg == NULL) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1200 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) |
| 1201 | && urb->transfer_buffer_length != 0) { |
| 1202 | dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb); |
| 1203 | dump_stack(); |
| 1204 | } |
| 1205 | |
| 1206 | result = -ENOMEM; |
| 1207 | xfer = kzalloc(sizeof(*xfer), gfp); |
| 1208 | if (xfer == NULL) |
| 1209 | goto error_kmalloc; |
| 1210 | |
| 1211 | result = -ENOENT; |
| 1212 | if (urb->status != -EINPROGRESS) /* cancelled */ |
| 1213 | goto error_dequeued; /* before starting? */ |
| 1214 | wa_xfer_init(xfer); |
| 1215 | xfer->wa = wa_get(wa); |
| 1216 | xfer->urb = urb; |
| 1217 | xfer->gfp = gfp; |
| 1218 | xfer->ep = ep; |
| 1219 | urb->hcpriv = xfer; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 1220 | |
| 1221 | dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n", |
| 1222 | xfer, urb, urb->pipe, urb->transfer_buffer_length, |
| 1223 | urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma", |
| 1224 | urb->pipe & USB_DIR_IN ? "inbound" : "outbound", |
| 1225 | cant_sleep ? "deferred" : "inline"); |
| 1226 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1227 | if (cant_sleep) { |
| 1228 | usb_get_urb(urb); |
| 1229 | spin_lock_irqsave(&wa->xfer_list_lock, my_flags); |
| 1230 | list_add_tail(&xfer->list_node, &wa->xfer_delayed_list); |
| 1231 | spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1232 | queue_work(wusbd, &wa->xfer_enqueue_work); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1233 | } else { |
| 1234 | wa_urb_enqueue_b(xfer); |
| 1235 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1236 | return 0; |
| 1237 | |
| 1238 | error_dequeued: |
| 1239 | kfree(xfer); |
| 1240 | error_kmalloc: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1241 | return result; |
| 1242 | } |
| 1243 | EXPORT_SYMBOL_GPL(wa_urb_enqueue); |
| 1244 | |
| 1245 | /* |
| 1246 | * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion |
| 1247 | * handler] is called. |
| 1248 | * |
| 1249 | * Until a transfer goes successfully through wa_urb_enqueue() it |
| 1250 | * needs to be dequeued with completion calling; when stuck in delayed |
| 1251 | * or before wa_xfer_setup() is called, we need to do completion. |
| 1252 | * |
| 1253 | * not setup If there is no hcpriv yet, that means that that enqueue |
| 1254 | * still had no time to set the xfer up. Because |
| 1255 | * urb->status should be other than -EINPROGRESS, |
| 1256 | * enqueue() will catch that and bail out. |
| 1257 | * |
| 1258 | * If the transfer has gone through setup, we just need to clean it |
| 1259 | * up. If it has gone through submit(), we have to abort it [with an |
| 1260 | * asynch request] and then make sure we cancel each segment. |
| 1261 | * |
| 1262 | */ |
| 1263 | int wa_urb_dequeue(struct wahc *wa, struct urb *urb) |
| 1264 | { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1265 | unsigned long flags, flags2; |
| 1266 | struct wa_xfer *xfer; |
| 1267 | struct wa_seg *seg; |
| 1268 | struct wa_rpipe *rpipe; |
| 1269 | unsigned cnt; |
| 1270 | unsigned rpipe_ready = 0; |
| 1271 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1272 | xfer = urb->hcpriv; |
| 1273 | if (xfer == NULL) { |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1274 | /* |
| 1275 | * Nothing setup yet enqueue will see urb->status != |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1276 | * -EINPROGRESS (by hcd layer) and bail out with |
| 1277 | * error, no need to do completion |
| 1278 | */ |
| 1279 | BUG_ON(urb->status == -EINPROGRESS); |
| 1280 | goto out; |
| 1281 | } |
| 1282 | spin_lock_irqsave(&xfer->lock, flags); |
| 1283 | rpipe = xfer->ep->hcpriv; |
Thomas Pugliese | ec58fad | 2013-08-09 09:52:13 -0500 | [diff] [blame] | 1284 | if (rpipe == NULL) { |
| 1285 | pr_debug("%s: xfer id 0x%08X has no RPIPE. %s", |
| 1286 | __func__, wa_xfer_id(xfer), |
| 1287 | "Probably already aborted.\n" ); |
| 1288 | goto out_unlock; |
| 1289 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1290 | /* Check the delayed list -> if there, release and complete */ |
| 1291 | spin_lock_irqsave(&wa->xfer_list_lock, flags2); |
| 1292 | if (!list_empty(&xfer->list_node) && xfer->seg == NULL) |
| 1293 | goto dequeue_delayed; |
| 1294 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags2); |
| 1295 | if (xfer->seg == NULL) /* still hasn't reached */ |
| 1296 | goto out_unlock; /* setup(), enqueue_b() completes */ |
| 1297 | /* Ok, the xfer is in flight already, it's been setup and submitted.*/ |
| 1298 | __wa_xfer_abort(xfer); |
| 1299 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
| 1300 | seg = xfer->seg[cnt]; |
| 1301 | switch (seg->status) { |
| 1302 | case WA_SEG_NOTREADY: |
| 1303 | case WA_SEG_READY: |
| 1304 | printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n", |
| 1305 | xfer, cnt, seg->status); |
| 1306 | WARN_ON(1); |
| 1307 | break; |
| 1308 | case WA_SEG_DELAYED: |
| 1309 | seg->status = WA_SEG_ABORTED; |
| 1310 | spin_lock_irqsave(&rpipe->seg_lock, flags2); |
| 1311 | list_del(&seg->list_node); |
| 1312 | xfer->segs_done++; |
| 1313 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1314 | spin_unlock_irqrestore(&rpipe->seg_lock, flags2); |
| 1315 | break; |
| 1316 | case WA_SEG_SUBMITTED: |
| 1317 | seg->status = WA_SEG_ABORTED; |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 1318 | usb_unlink_urb(&seg->tr_urb); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1319 | if (xfer->is_inbound == 0) |
| 1320 | usb_unlink_urb(seg->dto_urb); |
| 1321 | xfer->segs_done++; |
| 1322 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1323 | break; |
| 1324 | case WA_SEG_PENDING: |
| 1325 | seg->status = WA_SEG_ABORTED; |
| 1326 | xfer->segs_done++; |
| 1327 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1328 | break; |
| 1329 | case WA_SEG_DTI_PENDING: |
| 1330 | usb_unlink_urb(wa->dti_urb); |
| 1331 | seg->status = WA_SEG_ABORTED; |
| 1332 | xfer->segs_done++; |
| 1333 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1334 | break; |
| 1335 | case WA_SEG_DONE: |
| 1336 | case WA_SEG_ERROR: |
| 1337 | case WA_SEG_ABORTED: |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | xfer->result = urb->status; /* -ENOENT or -ECONNRESET */ |
| 1342 | __wa_xfer_is_done(xfer); |
| 1343 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1344 | wa_xfer_completion(xfer); |
| 1345 | if (rpipe_ready) |
| 1346 | wa_xfer_delayed_run(rpipe); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1347 | return 0; |
| 1348 | |
| 1349 | out_unlock: |
| 1350 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1351 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1352 | return 0; |
| 1353 | |
| 1354 | dequeue_delayed: |
| 1355 | list_del_init(&xfer->list_node); |
| 1356 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags2); |
| 1357 | xfer->result = urb->status; |
| 1358 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1359 | wa_xfer_giveback(xfer); |
| 1360 | usb_put_urb(urb); /* we got a ref in enqueue() */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1361 | return 0; |
| 1362 | } |
| 1363 | EXPORT_SYMBOL_GPL(wa_urb_dequeue); |
| 1364 | |
| 1365 | /* |
| 1366 | * Translation from WA status codes (WUSB1.0 Table 8.15) to errno |
| 1367 | * codes |
| 1368 | * |
| 1369 | * Positive errno values are internal inconsistencies and should be |
| 1370 | * flagged louder. Negative are to be passed up to the user in the |
| 1371 | * normal way. |
| 1372 | * |
| 1373 | * @status: USB WA status code -- high two bits are stripped. |
| 1374 | */ |
| 1375 | static int wa_xfer_status_to_errno(u8 status) |
| 1376 | { |
| 1377 | int errno; |
| 1378 | u8 real_status = status; |
| 1379 | static int xlat[] = { |
| 1380 | [WA_XFER_STATUS_SUCCESS] = 0, |
| 1381 | [WA_XFER_STATUS_HALTED] = -EPIPE, |
| 1382 | [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS, |
| 1383 | [WA_XFER_STATUS_BABBLE] = -EOVERFLOW, |
| 1384 | [WA_XFER_RESERVED] = EINVAL, |
| 1385 | [WA_XFER_STATUS_NOT_FOUND] = 0, |
| 1386 | [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM, |
| 1387 | [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ, |
| 1388 | [WA_XFER_STATUS_ABORTED] = -EINTR, |
| 1389 | [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL, |
| 1390 | [WA_XFER_INVALID_FORMAT] = EINVAL, |
| 1391 | [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL, |
| 1392 | [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL, |
| 1393 | }; |
| 1394 | status &= 0x3f; |
| 1395 | |
| 1396 | if (status == 0) |
| 1397 | return 0; |
| 1398 | if (status >= ARRAY_SIZE(xlat)) { |
Manuel Zerpies | 9708cd2 | 2011-06-16 14:15:16 +0200 | [diff] [blame] | 1399 | printk_ratelimited(KERN_ERR "%s(): BUG? " |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1400 | "Unknown WA transfer status 0x%02x\n", |
| 1401 | __func__, real_status); |
| 1402 | return -EINVAL; |
| 1403 | } |
| 1404 | errno = xlat[status]; |
| 1405 | if (unlikely(errno > 0)) { |
Manuel Zerpies | 9708cd2 | 2011-06-16 14:15:16 +0200 | [diff] [blame] | 1406 | printk_ratelimited(KERN_ERR "%s(): BUG? " |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1407 | "Inconsistent WA status: 0x%02x\n", |
| 1408 | __func__, real_status); |
| 1409 | errno = -errno; |
| 1410 | } |
| 1411 | return errno; |
| 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * Process a xfer result completion message |
| 1416 | * |
| 1417 | * inbound transfers: need to schedule a DTI read |
| 1418 | * |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1419 | * FIXME: this function needs to be broken up in parts |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1420 | */ |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame^] | 1421 | static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer, |
| 1422 | struct wa_xfer_result *xfer_result) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1423 | { |
| 1424 | int result; |
| 1425 | struct device *dev = &wa->usb_iface->dev; |
| 1426 | unsigned long flags; |
| 1427 | u8 seg_idx; |
| 1428 | struct wa_seg *seg; |
| 1429 | struct wa_rpipe *rpipe; |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame^] | 1430 | unsigned done = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1431 | u8 usb_status; |
| 1432 | unsigned rpipe_ready = 0; |
| 1433 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1434 | spin_lock_irqsave(&xfer->lock, flags); |
| 1435 | seg_idx = xfer_result->bTransferSegment & 0x7f; |
| 1436 | if (unlikely(seg_idx >= xfer->segs)) |
| 1437 | goto error_bad_seg; |
| 1438 | seg = xfer->seg[seg_idx]; |
| 1439 | rpipe = xfer->ep->hcpriv; |
| 1440 | usb_status = xfer_result->bTransferStatus; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1441 | dev_dbg(dev, "xfer %p#%u: bTransferStatus 0x%02x (seg status %u)\n", |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 1442 | xfer, seg_idx, usb_status, seg->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1443 | if (seg->status == WA_SEG_ABORTED |
| 1444 | || seg->status == WA_SEG_ERROR) /* already handled */ |
| 1445 | goto segment_aborted; |
| 1446 | if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */ |
| 1447 | seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */ |
| 1448 | if (seg->status != WA_SEG_PENDING) { |
| 1449 | if (printk_ratelimit()) |
| 1450 | dev_err(dev, "xfer %p#%u: Bad segment state %u\n", |
| 1451 | xfer, seg_idx, seg->status); |
| 1452 | seg->status = WA_SEG_PENDING; /* workaround/"fix" it */ |
| 1453 | } |
| 1454 | if (usb_status & 0x80) { |
| 1455 | seg->result = wa_xfer_status_to_errno(usb_status); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1456 | dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n", |
| 1457 | xfer, xfer->id, seg->index, usb_status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1458 | goto error_complete; |
| 1459 | } |
| 1460 | /* FIXME: we ignore warnings, tally them for stats */ |
| 1461 | if (usb_status & 0x40) /* Warning?... */ |
| 1462 | usb_status = 0; /* ... pass */ |
| 1463 | if (xfer->is_inbound) { /* IN data phase: read to buffer */ |
| 1464 | seg->status = WA_SEG_DTI_PENDING; |
| 1465 | BUG_ON(wa->buf_in_urb->status == -EINPROGRESS); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1466 | /* this should always be 0 before a resubmit. */ |
| 1467 | wa->buf_in_urb->num_mapped_sgs = 0; |
| 1468 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1469 | if (xfer->is_dma) { |
| 1470 | wa->buf_in_urb->transfer_dma = |
| 1471 | xfer->urb->transfer_dma |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1472 | + (seg_idx * xfer->seg_size); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1473 | wa->buf_in_urb->transfer_flags |
| 1474 | |= URB_NO_TRANSFER_DMA_MAP; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1475 | wa->buf_in_urb->transfer_buffer = NULL; |
| 1476 | wa->buf_in_urb->sg = NULL; |
| 1477 | wa->buf_in_urb->num_sgs = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1478 | } else { |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1479 | /* do buffer or SG processing. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1480 | wa->buf_in_urb->transfer_flags |
| 1481 | &= ~URB_NO_TRANSFER_DMA_MAP; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1482 | |
| 1483 | if (xfer->urb->transfer_buffer) { |
| 1484 | wa->buf_in_urb->transfer_buffer = |
| 1485 | xfer->urb->transfer_buffer |
| 1486 | + (seg_idx * xfer->seg_size); |
| 1487 | wa->buf_in_urb->sg = NULL; |
| 1488 | wa->buf_in_urb->num_sgs = 0; |
| 1489 | } else { |
| 1490 | /* allocate an SG list to store seg_size bytes |
| 1491 | and copy the subset of the xfer->urb->sg |
| 1492 | that matches the buffer subset we are |
| 1493 | about to read. */ |
| 1494 | wa->buf_in_urb->sg = wa_xfer_create_subset_sg( |
| 1495 | xfer->urb->sg, |
| 1496 | seg_idx * xfer->seg_size, |
| 1497 | le32_to_cpu( |
| 1498 | xfer_result->dwTransferLength), |
| 1499 | &(wa->buf_in_urb->num_sgs)); |
| 1500 | |
| 1501 | if (!(wa->buf_in_urb->sg)) { |
| 1502 | wa->buf_in_urb->num_sgs = 0; |
| 1503 | goto error_sg_alloc; |
| 1504 | } |
| 1505 | wa->buf_in_urb->transfer_buffer = NULL; |
| 1506 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1507 | } |
| 1508 | wa->buf_in_urb->transfer_buffer_length = |
| 1509 | le32_to_cpu(xfer_result->dwTransferLength); |
| 1510 | wa->buf_in_urb->context = seg; |
| 1511 | result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC); |
| 1512 | if (result < 0) |
| 1513 | goto error_submit_buf_in; |
| 1514 | } else { |
| 1515 | /* OUT data phase, complete it -- */ |
| 1516 | seg->status = WA_SEG_DONE; |
| 1517 | seg->result = le32_to_cpu(xfer_result->dwTransferLength); |
| 1518 | xfer->segs_done++; |
| 1519 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1520 | done = __wa_xfer_is_done(xfer); |
| 1521 | } |
| 1522 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1523 | if (done) |
| 1524 | wa_xfer_completion(xfer); |
| 1525 | if (rpipe_ready) |
| 1526 | wa_xfer_delayed_run(rpipe); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1527 | return; |
| 1528 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1529 | error_submit_buf_in: |
| 1530 | if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) { |
| 1531 | dev_err(dev, "DTI: URB max acceptable errors " |
| 1532 | "exceeded, resetting device\n"); |
| 1533 | wa_reset_all(wa); |
| 1534 | } |
| 1535 | if (printk_ratelimit()) |
| 1536 | dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n", |
| 1537 | xfer, seg_idx, result); |
| 1538 | seg->result = result; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1539 | kfree(wa->buf_in_urb->sg); |
| 1540 | error_sg_alloc: |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1541 | __wa_xfer_abort(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1542 | error_complete: |
| 1543 | seg->status = WA_SEG_ERROR; |
| 1544 | xfer->segs_done++; |
| 1545 | rpipe_ready = rpipe_avail_inc(rpipe); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1546 | done = __wa_xfer_is_done(xfer); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1547 | /* |
| 1548 | * queue work item to clear STALL for control endpoints. |
| 1549 | * Otherwise, let endpoint_reset take care of it. |
| 1550 | */ |
| 1551 | if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) && |
| 1552 | usb_endpoint_xfer_control(&xfer->ep->desc) && |
| 1553 | done) { |
| 1554 | |
| 1555 | dev_info(dev, "Control EP stall. Queue delayed work.\n"); |
| 1556 | spin_lock_irq(&wa->xfer_list_lock); |
Wei Yongjun | 8eb4129 | 2013-09-23 14:16:22 +0800 | [diff] [blame] | 1557 | /* move xfer from xfer_list to xfer_errored_list. */ |
| 1558 | list_move_tail(&xfer->list_node, &wa->xfer_errored_list); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1559 | spin_unlock_irq(&wa->xfer_list_lock); |
| 1560 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1561 | queue_work(wusbd, &wa->xfer_error_work); |
| 1562 | } else { |
| 1563 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1564 | if (done) |
| 1565 | wa_xfer_completion(xfer); |
| 1566 | if (rpipe_ready) |
| 1567 | wa_xfer_delayed_run(rpipe); |
| 1568 | } |
| 1569 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1570 | return; |
| 1571 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1572 | error_bad_seg: |
| 1573 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1574 | wa_urb_dequeue(wa, xfer->urb); |
| 1575 | if (printk_ratelimit()) |
| 1576 | dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx); |
| 1577 | if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) { |
| 1578 | dev_err(dev, "DTI: URB max acceptable errors " |
| 1579 | "exceeded, resetting device\n"); |
| 1580 | wa_reset_all(wa); |
| 1581 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1582 | return; |
| 1583 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1584 | segment_aborted: |
| 1585 | /* nothing to do, as the aborter did the completion */ |
| 1586 | spin_unlock_irqrestore(&xfer->lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /* |
| 1590 | * Callback for the IN data phase |
| 1591 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1592 | * If successful transition state; otherwise, take a note of the |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1593 | * error, mark this segment done and try completion. |
| 1594 | * |
| 1595 | * Note we don't access until we are sure that the transfer hasn't |
| 1596 | * been cancelled (ECONNRESET, ENOENT), which could mean that |
| 1597 | * seg->xfer could be already gone. |
| 1598 | */ |
| 1599 | static void wa_buf_in_cb(struct urb *urb) |
| 1600 | { |
| 1601 | struct wa_seg *seg = urb->context; |
| 1602 | struct wa_xfer *xfer = seg->xfer; |
| 1603 | struct wahc *wa; |
| 1604 | struct device *dev; |
| 1605 | struct wa_rpipe *rpipe; |
| 1606 | unsigned rpipe_ready; |
| 1607 | unsigned long flags; |
| 1608 | u8 done = 0; |
| 1609 | |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1610 | /* free the sg if it was used. */ |
| 1611 | kfree(urb->sg); |
| 1612 | urb->sg = NULL; |
| 1613 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1614 | switch (urb->status) { |
| 1615 | case 0: |
| 1616 | spin_lock_irqsave(&xfer->lock, flags); |
| 1617 | wa = xfer->wa; |
| 1618 | dev = &wa->usb_iface->dev; |
| 1619 | rpipe = xfer->ep->hcpriv; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 1620 | dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n", |
| 1621 | xfer, seg->index, (size_t)urb->actual_length); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1622 | seg->status = WA_SEG_DONE; |
| 1623 | seg->result = urb->actual_length; |
| 1624 | xfer->segs_done++; |
| 1625 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1626 | done = __wa_xfer_is_done(xfer); |
| 1627 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1628 | if (done) |
| 1629 | wa_xfer_completion(xfer); |
| 1630 | if (rpipe_ready) |
| 1631 | wa_xfer_delayed_run(rpipe); |
| 1632 | break; |
| 1633 | case -ECONNRESET: /* URB unlinked; no need to do anything */ |
| 1634 | case -ENOENT: /* as it was done by the who unlinked us */ |
| 1635 | break; |
| 1636 | default: /* Other errors ... */ |
| 1637 | spin_lock_irqsave(&xfer->lock, flags); |
| 1638 | wa = xfer->wa; |
| 1639 | dev = &wa->usb_iface->dev; |
| 1640 | rpipe = xfer->ep->hcpriv; |
| 1641 | if (printk_ratelimit()) |
| 1642 | dev_err(dev, "xfer %p#%u: data in error %d\n", |
| 1643 | xfer, seg->index, urb->status); |
| 1644 | if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS, |
| 1645 | EDC_ERROR_TIMEFRAME)){ |
| 1646 | dev_err(dev, "DTO: URB max acceptable errors " |
| 1647 | "exceeded, resetting device\n"); |
| 1648 | wa_reset_all(wa); |
| 1649 | } |
| 1650 | seg->status = WA_SEG_ERROR; |
| 1651 | seg->result = urb->status; |
| 1652 | xfer->segs_done++; |
| 1653 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1654 | __wa_xfer_abort(xfer); |
| 1655 | done = __wa_xfer_is_done(xfer); |
| 1656 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1657 | if (done) |
| 1658 | wa_xfer_completion(xfer); |
| 1659 | if (rpipe_ready) |
| 1660 | wa_xfer_delayed_run(rpipe); |
| 1661 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * Handle an incoming transfer result buffer |
| 1666 | * |
| 1667 | * Given a transfer result buffer, it completes the transfer (possibly |
| 1668 | * scheduling and buffer in read) and then resubmits the DTI URB for a |
| 1669 | * new transfer result read. |
| 1670 | * |
| 1671 | * |
| 1672 | * The xfer_result DTI URB state machine |
| 1673 | * |
| 1674 | * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In) |
| 1675 | * |
| 1676 | * We start in OFF mode, the first xfer_result notification [through |
| 1677 | * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to |
| 1678 | * read. |
| 1679 | * |
| 1680 | * We receive a buffer -- if it is not a xfer_result, we complain and |
| 1681 | * repost the DTI-URB. If it is a xfer_result then do the xfer seg |
| 1682 | * request accounting. If it is an IN segment, we move to RBI and post |
| 1683 | * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will |
| 1684 | * repost the DTI-URB and move to RXR state. if there was no IN |
| 1685 | * segment, it will repost the DTI-URB. |
| 1686 | * |
| 1687 | * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many |
| 1688 | * errors) in the URBs. |
| 1689 | */ |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame^] | 1690 | static void wa_dti_cb(struct urb *urb) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1691 | { |
| 1692 | int result; |
| 1693 | struct wahc *wa = urb->context; |
| 1694 | struct device *dev = &wa->usb_iface->dev; |
| 1695 | struct wa_xfer_result *xfer_result; |
| 1696 | u32 xfer_id; |
| 1697 | struct wa_xfer *xfer; |
| 1698 | u8 usb_status; |
| 1699 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1700 | BUG_ON(wa->dti_urb != urb); |
| 1701 | switch (wa->dti_urb->status) { |
| 1702 | case 0: |
| 1703 | /* We have a xfer result buffer; check it */ |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 1704 | dev_dbg(dev, "DTI: xfer result %d bytes at %p\n", |
| 1705 | urb->actual_length, urb->transfer_buffer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1706 | if (wa->dti_urb->actual_length != sizeof(*xfer_result)) { |
| 1707 | dev_err(dev, "DTI Error: xfer result--bad size " |
| 1708 | "xfer result (%d bytes vs %zu needed)\n", |
| 1709 | urb->actual_length, sizeof(*xfer_result)); |
| 1710 | break; |
| 1711 | } |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame^] | 1712 | xfer_result = (struct wa_xfer_result *)(wa->dti_buf); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1713 | if (xfer_result->hdr.bLength != sizeof(*xfer_result)) { |
| 1714 | dev_err(dev, "DTI Error: xfer result--" |
| 1715 | "bad header length %u\n", |
| 1716 | xfer_result->hdr.bLength); |
| 1717 | break; |
| 1718 | } |
| 1719 | if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) { |
| 1720 | dev_err(dev, "DTI Error: xfer result--" |
| 1721 | "bad header type 0x%02x\n", |
| 1722 | xfer_result->hdr.bNotifyType); |
| 1723 | break; |
| 1724 | } |
| 1725 | usb_status = xfer_result->bTransferStatus & 0x3f; |
Thomas Pugliese | ec58fad | 2013-08-09 09:52:13 -0500 | [diff] [blame] | 1726 | if (usb_status == WA_XFER_STATUS_NOT_FOUND) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1727 | /* taken care of already */ |
| 1728 | break; |
| 1729 | xfer_id = xfer_result->dwTransferID; |
| 1730 | xfer = wa_xfer_get_by_id(wa, xfer_id); |
| 1731 | if (xfer == NULL) { |
| 1732 | /* FIXME: transaction might have been cancelled */ |
| 1733 | dev_err(dev, "DTI Error: xfer result--" |
| 1734 | "unknown xfer 0x%08x (status 0x%02x)\n", |
| 1735 | xfer_id, usb_status); |
| 1736 | break; |
| 1737 | } |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame^] | 1738 | wa_xfer_result_chew(wa, xfer, xfer_result); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1739 | wa_xfer_put(xfer); |
| 1740 | break; |
| 1741 | case -ENOENT: /* (we killed the URB)...so, no broadcast */ |
| 1742 | case -ESHUTDOWN: /* going away! */ |
| 1743 | dev_dbg(dev, "DTI: going down! %d\n", urb->status); |
| 1744 | goto out; |
| 1745 | default: |
| 1746 | /* Unknown error */ |
| 1747 | if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, |
| 1748 | EDC_ERROR_TIMEFRAME)) { |
| 1749 | dev_err(dev, "DTI: URB max acceptable errors " |
| 1750 | "exceeded, resetting device\n"); |
| 1751 | wa_reset_all(wa); |
| 1752 | goto out; |
| 1753 | } |
| 1754 | if (printk_ratelimit()) |
| 1755 | dev_err(dev, "DTI: URB error %d\n", urb->status); |
| 1756 | break; |
| 1757 | } |
| 1758 | /* Resubmit the DTI URB */ |
| 1759 | result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC); |
| 1760 | if (result < 0) { |
| 1761 | dev_err(dev, "DTI Error: Could not submit DTI URB (%d), " |
| 1762 | "resetting\n", result); |
| 1763 | wa_reset_all(wa); |
| 1764 | } |
| 1765 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Transfer complete notification |
| 1771 | * |
| 1772 | * Called from the notif.c code. We get a notification on EP2 saying |
| 1773 | * that some endpoint has some transfer result data available. We are |
| 1774 | * about to read it. |
| 1775 | * |
| 1776 | * To speed up things, we always have a URB reading the DTI URB; we |
| 1777 | * don't really set it up and start it until the first xfer complete |
| 1778 | * notification arrives, which is what we do here. |
| 1779 | * |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame^] | 1780 | * Follow up in wa_dti_cb(), as that's where the whole state |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1781 | * machine starts. |
| 1782 | * |
| 1783 | * So here we just initialize the DTI URB for reading transfer result |
| 1784 | * notifications and also the buffer-in URB, for reading buffers. Then |
| 1785 | * we just submit the DTI URB. |
| 1786 | * |
| 1787 | * @wa shall be referenced |
| 1788 | */ |
| 1789 | void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr) |
| 1790 | { |
| 1791 | int result; |
| 1792 | struct device *dev = &wa->usb_iface->dev; |
| 1793 | struct wa_notif_xfer *notif_xfer; |
| 1794 | const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd; |
| 1795 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1796 | notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr); |
| 1797 | BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER); |
| 1798 | |
| 1799 | if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) { |
| 1800 | /* FIXME: hardcoded limitation, adapt */ |
| 1801 | dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n", |
| 1802 | notif_xfer->bEndpoint, dti_epd->bEndpointAddress); |
| 1803 | goto error; |
| 1804 | } |
| 1805 | if (wa->dti_urb != NULL) /* DTI URB already started */ |
| 1806 | goto out; |
| 1807 | |
| 1808 | wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1809 | if (wa->dti_urb == NULL) { |
| 1810 | dev_err(dev, "Can't allocate DTI URB\n"); |
| 1811 | goto error_dti_urb_alloc; |
| 1812 | } |
| 1813 | usb_fill_bulk_urb( |
| 1814 | wa->dti_urb, wa->usb_dev, |
| 1815 | usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint), |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame^] | 1816 | wa->dti_buf, wa->dti_buf_size, |
| 1817 | wa_dti_cb, wa); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1818 | |
| 1819 | wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1820 | if (wa->buf_in_urb == NULL) { |
| 1821 | dev_err(dev, "Can't allocate BUF-IN URB\n"); |
| 1822 | goto error_buf_in_urb_alloc; |
| 1823 | } |
| 1824 | usb_fill_bulk_urb( |
| 1825 | wa->buf_in_urb, wa->usb_dev, |
| 1826 | usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint), |
| 1827 | NULL, 0, wa_buf_in_cb, wa); |
| 1828 | result = usb_submit_urb(wa->dti_urb, GFP_KERNEL); |
| 1829 | if (result < 0) { |
| 1830 | dev_err(dev, "DTI Error: Could not submit DTI URB (%d), " |
| 1831 | "resetting\n", result); |
| 1832 | goto error_dti_urb_submit; |
| 1833 | } |
| 1834 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1835 | return; |
| 1836 | |
| 1837 | error_dti_urb_submit: |
| 1838 | usb_put_urb(wa->buf_in_urb); |
| 1839 | error_buf_in_urb_alloc: |
| 1840 | usb_put_urb(wa->dti_urb); |
| 1841 | wa->dti_urb = NULL; |
| 1842 | error_dti_urb_alloc: |
| 1843 | error: |
| 1844 | wa_reset_all(wa); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1845 | } |