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 *); |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 110 | static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Life cycle governed by 'struct urb' (the refcount of the struct is |
| 114 | * that of the 'struct urb' and usb_free_urb() would free the whole |
| 115 | * struct). |
| 116 | */ |
| 117 | struct wa_seg { |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 118 | struct urb tr_urb; /* transfer request urb. */ |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 119 | struct urb *isoc_pack_desc_urb; /* for isoc packet descriptor. */ |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 120 | struct urb *dto_urb; /* for data output. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 121 | struct list_head list_node; /* for rpipe->req_list */ |
| 122 | struct wa_xfer *xfer; /* out xfer */ |
| 123 | u8 index; /* which segment we are */ |
| 124 | enum wa_seg_status status; |
| 125 | ssize_t result; /* bytes xfered or error */ |
| 126 | struct wa_xfer_hdr xfer_hdr; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 127 | }; |
| 128 | |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 129 | static inline void wa_seg_init(struct wa_seg *seg) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 130 | { |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 131 | usb_init_urb(&seg->tr_urb); |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 132 | |
| 133 | /* set the remaining memory to 0. */ |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 134 | memset(((void *)seg) + sizeof(seg->tr_urb), 0, |
| 135 | sizeof(*seg) - sizeof(seg->tr_urb)); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Protected by xfer->lock |
| 140 | * |
| 141 | */ |
| 142 | struct wa_xfer { |
| 143 | struct kref refcnt; |
| 144 | struct list_head list_node; |
| 145 | spinlock_t lock; |
| 146 | u32 id; |
| 147 | |
| 148 | struct wahc *wa; /* Wire adapter we are plugged to */ |
| 149 | struct usb_host_endpoint *ep; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 150 | struct urb *urb; /* URB we are transferring for */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 151 | struct wa_seg **seg; /* transfer segments */ |
| 152 | u8 segs, segs_submitted, segs_done; |
| 153 | unsigned is_inbound:1; |
| 154 | unsigned is_dma:1; |
| 155 | size_t seg_size; |
| 156 | int result; |
| 157 | |
| 158 | gfp_t gfp; /* allocation mask */ |
| 159 | |
| 160 | struct wusb_dev *wusb_dev; /* for activity timestamps */ |
| 161 | }; |
| 162 | |
| 163 | static inline void wa_xfer_init(struct wa_xfer *xfer) |
| 164 | { |
| 165 | kref_init(&xfer->refcnt); |
| 166 | INIT_LIST_HEAD(&xfer->list_node); |
| 167 | spin_lock_init(&xfer->lock); |
| 168 | } |
| 169 | |
| 170 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 171 | * Destroy a transfer structure |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 172 | * |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 173 | * Note that freeing xfer->seg[cnt]->tr_urb will free the containing |
Thomas Pugliese | 79731cb | 2013-08-15 14:37:42 -0500 | [diff] [blame] | 174 | * 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] | 175 | */ |
| 176 | static void wa_xfer_destroy(struct kref *_xfer) |
| 177 | { |
| 178 | struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt); |
| 179 | if (xfer->seg) { |
| 180 | unsigned cnt; |
| 181 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 182 | struct wa_seg *seg = xfer->seg[cnt]; |
| 183 | if (seg) { |
| 184 | usb_free_urb(seg->isoc_pack_desc_urb); |
| 185 | if (seg->dto_urb) { |
| 186 | kfree(seg->dto_urb->sg); |
| 187 | usb_free_urb(seg->dto_urb); |
Thomas Pugliese | d993670 | 2013-09-26 14:08:13 -0500 | [diff] [blame] | 188 | } |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 189 | usb_free_urb(&seg->tr_urb); |
Thomas Pugliese | d993670 | 2013-09-26 14:08:13 -0500 | [diff] [blame] | 190 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 191 | } |
Thomas Pugliese | d993670 | 2013-09-26 14:08:13 -0500 | [diff] [blame] | 192 | kfree(xfer->seg); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 193 | } |
| 194 | kfree(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void wa_xfer_get(struct wa_xfer *xfer) |
| 198 | { |
| 199 | kref_get(&xfer->refcnt); |
| 200 | } |
| 201 | |
| 202 | static void wa_xfer_put(struct wa_xfer *xfer) |
| 203 | { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 204 | kref_put(&xfer->refcnt, wa_xfer_destroy); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 208 | * Try to get exclusive access to the DTO endpoint resource. Return true |
| 209 | * if successful. |
| 210 | */ |
| 211 | static inline int __wa_dto_try_get(struct wahc *wa) |
| 212 | { |
| 213 | return (test_and_set_bit(0, &wa->dto_in_use) == 0); |
| 214 | } |
| 215 | |
| 216 | /* Release the DTO endpoint resource. */ |
| 217 | static inline void __wa_dto_put(struct wahc *wa) |
| 218 | { |
| 219 | clear_bit_unlock(0, &wa->dto_in_use); |
| 220 | } |
| 221 | |
| 222 | /* Service RPIPEs that are waiting on the DTO resource. */ |
| 223 | static void wa_check_for_delayed_rpipes(struct wahc *wa) |
| 224 | { |
| 225 | unsigned long flags; |
| 226 | int dto_waiting = 0; |
| 227 | struct wa_rpipe *rpipe; |
| 228 | |
| 229 | spin_lock_irqsave(&wa->rpipe_lock, flags); |
| 230 | while (!list_empty(&wa->rpipe_delayed_list) && !dto_waiting) { |
| 231 | rpipe = list_first_entry(&wa->rpipe_delayed_list, |
| 232 | struct wa_rpipe, list_node); |
| 233 | __wa_xfer_delayed_run(rpipe, &dto_waiting); |
| 234 | /* remove this RPIPE from the list if it is not waiting. */ |
| 235 | if (!dto_waiting) { |
| 236 | pr_debug("%s: RPIPE %d serviced and removed from delayed list.\n", |
| 237 | __func__, |
| 238 | le16_to_cpu(rpipe->descr.wRPipeIndex)); |
| 239 | list_del_init(&rpipe->list_node); |
| 240 | } |
| 241 | } |
| 242 | spin_unlock_irqrestore(&wa->rpipe_lock, flags); |
| 243 | } |
| 244 | |
| 245 | /* add this RPIPE to the end of the delayed RPIPE list. */ |
| 246 | static void wa_add_delayed_rpipe(struct wahc *wa, struct wa_rpipe *rpipe) |
| 247 | { |
| 248 | unsigned long flags; |
| 249 | |
| 250 | spin_lock_irqsave(&wa->rpipe_lock, flags); |
| 251 | /* add rpipe to the list if it is not already on it. */ |
| 252 | if (list_empty(&rpipe->list_node)) { |
| 253 | pr_debug("%s: adding RPIPE %d to the delayed list.\n", |
| 254 | __func__, le16_to_cpu(rpipe->descr.wRPipeIndex)); |
| 255 | list_add_tail(&rpipe->list_node, &wa->rpipe_delayed_list); |
| 256 | } |
| 257 | spin_unlock_irqrestore(&wa->rpipe_lock, flags); |
| 258 | } |
| 259 | |
| 260 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 261 | * xfer is referenced |
| 262 | * |
| 263 | * xfer->lock has to be unlocked |
| 264 | * |
| 265 | * We take xfer->lock for setting the result; this is a barrier |
| 266 | * against drivers/usb/core/hcd.c:unlink1() being called after we call |
| 267 | * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a |
| 268 | * reference to the transfer. |
| 269 | */ |
| 270 | static void wa_xfer_giveback(struct wa_xfer *xfer) |
| 271 | { |
| 272 | unsigned long flags; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 273 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 274 | spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags); |
| 275 | list_del_init(&xfer->list_node); |
| 276 | spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags); |
| 277 | /* FIXME: segmentation broken -- kills DWA */ |
| 278 | wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result); |
| 279 | wa_put(xfer->wa); |
| 280 | wa_xfer_put(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* |
| 284 | * xfer is referenced |
| 285 | * |
| 286 | * xfer->lock has to be unlocked |
| 287 | */ |
| 288 | static void wa_xfer_completion(struct wa_xfer *xfer) |
| 289 | { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 290 | if (xfer->wusb_dev) |
| 291 | wusb_dev_put(xfer->wusb_dev); |
| 292 | rpipe_put(xfer->ep->hcpriv); |
| 293 | wa_xfer_giveback(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 297 | * Initialize a transfer's ID |
| 298 | * |
| 299 | * We need to use a sequential number; if we use the pointer or the |
| 300 | * hash of the pointer, it can repeat over sequential transfers and |
| 301 | * then it will confuse the HWA....wonder why in hell they put a 32 |
| 302 | * bit handle in there then. |
| 303 | */ |
| 304 | static void wa_xfer_id_init(struct wa_xfer *xfer) |
| 305 | { |
| 306 | xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count); |
| 307 | } |
| 308 | |
Thomas Pugliese | fdd160c | 2013-09-27 15:33:35 -0500 | [diff] [blame] | 309 | /* Return the xfer's ID. */ |
| 310 | static inline u32 wa_xfer_id(struct wa_xfer *xfer) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 311 | { |
| 312 | return xfer->id; |
| 313 | } |
| 314 | |
Thomas Pugliese | fdd160c | 2013-09-27 15:33:35 -0500 | [diff] [blame] | 315 | /* Return the xfer's ID in transport format (little endian). */ |
| 316 | static inline __le32 wa_xfer_id_le32(struct wa_xfer *xfer) |
| 317 | { |
| 318 | return cpu_to_le32(xfer->id); |
| 319 | } |
| 320 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 321 | /* |
Thomas Pugliese | b9c84be | 2013-09-27 15:33:36 -0500 | [diff] [blame] | 322 | * If transfer is done, wrap it up and return true |
| 323 | * |
| 324 | * xfer->lock has to be locked |
| 325 | */ |
| 326 | static unsigned __wa_xfer_is_done(struct wa_xfer *xfer) |
| 327 | { |
| 328 | struct device *dev = &xfer->wa->usb_iface->dev; |
| 329 | unsigned result, cnt; |
| 330 | struct wa_seg *seg; |
| 331 | struct urb *urb = xfer->urb; |
| 332 | unsigned found_short = 0; |
| 333 | |
| 334 | result = xfer->segs_done == xfer->segs_submitted; |
| 335 | if (result == 0) |
| 336 | goto out; |
| 337 | urb->actual_length = 0; |
| 338 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
| 339 | seg = xfer->seg[cnt]; |
| 340 | switch (seg->status) { |
| 341 | case WA_SEG_DONE: |
| 342 | if (found_short && seg->result > 0) { |
| 343 | dev_dbg(dev, "xfer %p ID %08X#%u: bad short segments (%zu)\n", |
| 344 | xfer, wa_xfer_id(xfer), cnt, |
| 345 | seg->result); |
| 346 | urb->status = -EINVAL; |
| 347 | goto out; |
| 348 | } |
| 349 | urb->actual_length += seg->result; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 350 | if (!(usb_pipeisoc(xfer->urb->pipe)) |
| 351 | && seg->result < xfer->seg_size |
Thomas Pugliese | b9c84be | 2013-09-27 15:33:36 -0500 | [diff] [blame] | 352 | && cnt != xfer->segs-1) |
| 353 | found_short = 1; |
| 354 | dev_dbg(dev, "xfer %p ID %08X#%u: DONE short %d " |
| 355 | "result %zu urb->actual_length %d\n", |
| 356 | xfer, wa_xfer_id(xfer), seg->index, found_short, |
| 357 | seg->result, urb->actual_length); |
| 358 | break; |
| 359 | case WA_SEG_ERROR: |
| 360 | xfer->result = seg->result; |
Thomas Pugliese | cccd3a25 | 2013-09-30 22:48:46 -0500 | [diff] [blame] | 361 | dev_dbg(dev, "xfer %p ID %08X#%u: ERROR result %zu(0x%08zX)\n", |
Thomas Pugliese | b9c84be | 2013-09-27 15:33:36 -0500 | [diff] [blame] | 362 | xfer, wa_xfer_id(xfer), seg->index, seg->result, |
| 363 | seg->result); |
| 364 | goto out; |
| 365 | case WA_SEG_ABORTED: |
| 366 | dev_dbg(dev, "xfer %p ID %08X#%u ABORTED: result %d\n", |
| 367 | xfer, wa_xfer_id(xfer), seg->index, |
| 368 | urb->status); |
| 369 | xfer->result = urb->status; |
| 370 | goto out; |
| 371 | default: |
| 372 | dev_warn(dev, "xfer %p ID %08X#%u: is_done bad state %d\n", |
| 373 | xfer, wa_xfer_id(xfer), cnt, seg->status); |
| 374 | xfer->result = -EINVAL; |
| 375 | goto out; |
| 376 | } |
| 377 | } |
| 378 | xfer->result = 0; |
| 379 | out: |
| 380 | return result; |
| 381 | } |
| 382 | |
| 383 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 384 | * Search for a transfer list ID on the HCD's URB list |
| 385 | * |
| 386 | * For 32 bit architectures, we use the pointer itself; for 64 bits, a |
| 387 | * 32-bit hash of the pointer. |
| 388 | * |
| 389 | * @returns NULL if not found. |
| 390 | */ |
| 391 | static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id) |
| 392 | { |
| 393 | unsigned long flags; |
| 394 | struct wa_xfer *xfer_itr; |
| 395 | spin_lock_irqsave(&wa->xfer_list_lock, flags); |
| 396 | list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) { |
| 397 | if (id == xfer_itr->id) { |
| 398 | wa_xfer_get(xfer_itr); |
| 399 | goto out; |
| 400 | } |
| 401 | } |
| 402 | xfer_itr = NULL; |
| 403 | out: |
| 404 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags); |
| 405 | return xfer_itr; |
| 406 | } |
| 407 | |
| 408 | struct wa_xfer_abort_buffer { |
| 409 | struct urb urb; |
| 410 | struct wa_xfer_abort cmd; |
| 411 | }; |
| 412 | |
| 413 | static void __wa_xfer_abort_cb(struct urb *urb) |
| 414 | { |
| 415 | struct wa_xfer_abort_buffer *b = urb->context; |
| 416 | usb_put_urb(&b->urb); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Aborts an ongoing transaction |
| 421 | * |
| 422 | * Assumes the transfer is referenced and locked and in a submitted |
| 423 | * state (mainly that there is an endpoint/rpipe assigned). |
| 424 | * |
| 425 | * The callback (see above) does nothing but freeing up the data by |
| 426 | * putting the URB. Because the URB is allocated at the head of the |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 427 | * struct, the whole space we allocated is kfreed. * |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 428 | */ |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 429 | static int __wa_xfer_abort(struct wa_xfer *xfer) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 430 | { |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 431 | int result = -ENOMEM; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 432 | struct device *dev = &xfer->wa->usb_iface->dev; |
| 433 | struct wa_xfer_abort_buffer *b; |
| 434 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 435 | |
| 436 | b = kmalloc(sizeof(*b), GFP_ATOMIC); |
| 437 | if (b == NULL) |
| 438 | goto error_kmalloc; |
| 439 | b->cmd.bLength = sizeof(b->cmd); |
| 440 | b->cmd.bRequestType = WA_XFER_ABORT; |
| 441 | b->cmd.wRPipe = rpipe->descr.wRPipeIndex; |
Thomas Pugliese | fdd160c | 2013-09-27 15:33:35 -0500 | [diff] [blame] | 442 | b->cmd.dwTransferID = wa_xfer_id_le32(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 443 | |
| 444 | usb_init_urb(&b->urb); |
| 445 | usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev, |
| 446 | usb_sndbulkpipe(xfer->wa->usb_dev, |
| 447 | xfer->wa->dto_epd->bEndpointAddress), |
| 448 | &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b); |
| 449 | result = usb_submit_urb(&b->urb, GFP_ATOMIC); |
| 450 | if (result < 0) |
| 451 | goto error_submit; |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 452 | return result; /* callback frees! */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 453 | |
| 454 | |
| 455 | error_submit: |
| 456 | if (printk_ratelimit()) |
| 457 | dev_err(dev, "xfer %p: Can't submit abort request: %d\n", |
| 458 | xfer, result); |
| 459 | kfree(b); |
| 460 | error_kmalloc: |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 461 | return result; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 462 | |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * |
| 467 | * @returns < 0 on error, transfer segment request size if ok |
| 468 | */ |
| 469 | static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer, |
| 470 | enum wa_xfer_type *pxfer_type) |
| 471 | { |
| 472 | ssize_t result; |
| 473 | struct device *dev = &xfer->wa->usb_iface->dev; |
| 474 | size_t maxpktsize; |
| 475 | struct urb *urb = xfer->urb; |
| 476 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 477 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 478 | switch (rpipe->descr.bmAttribute & 0x3) { |
| 479 | case USB_ENDPOINT_XFER_CONTROL: |
| 480 | *pxfer_type = WA_XFER_TYPE_CTL; |
| 481 | result = sizeof(struct wa_xfer_ctl); |
| 482 | break; |
| 483 | case USB_ENDPOINT_XFER_INT: |
| 484 | case USB_ENDPOINT_XFER_BULK: |
| 485 | *pxfer_type = WA_XFER_TYPE_BI; |
| 486 | result = sizeof(struct wa_xfer_bi); |
| 487 | break; |
| 488 | case USB_ENDPOINT_XFER_ISOC: |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 489 | if (usb_pipeout(urb->pipe)) { |
| 490 | *pxfer_type = WA_XFER_TYPE_ISO; |
| 491 | result = sizeof(struct wa_xfer_hwaiso); |
| 492 | } else { |
| 493 | dev_err(dev, "FIXME: ISOC IN not implemented\n"); |
| 494 | result = -ENOSYS; |
| 495 | goto error; |
| 496 | } |
| 497 | break; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 498 | default: |
| 499 | /* never happens */ |
| 500 | BUG(); |
| 501 | result = -EINVAL; /* shut gcc up */ |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 502 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 503 | xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0; |
| 504 | xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 505 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 506 | maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize); |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 507 | if ((rpipe->descr.bmAttribute & 0x3) == USB_ENDPOINT_XFER_ISOC) { |
| 508 | xfer->seg_size = maxpktsize; |
| 509 | xfer->segs = urb->number_of_packets; |
| 510 | } else { |
| 511 | xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks) |
| 512 | * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1); |
| 513 | /* Compute the segment size and make sure it is a multiple of |
| 514 | * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of |
| 515 | * a check (FIXME) */ |
| 516 | if (xfer->seg_size < maxpktsize) { |
| 517 | dev_err(dev, |
| 518 | "HW BUG? seg_size %zu smaller than maxpktsize %zu\n", |
| 519 | xfer->seg_size, maxpktsize); |
| 520 | result = -EINVAL; |
| 521 | goto error; |
| 522 | } |
| 523 | xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize; |
| 524 | xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length, |
| 525 | xfer->seg_size); |
| 526 | if (xfer->segs >= WA_SEGS_MAX) { |
Thomas Pugliese | afc3cba | 2013-10-06 13:48:47 -0500 | [diff] [blame] | 527 | dev_err(dev, "BUG? oops, number of segments %zu bigger than %d\n", |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 528 | (urb->transfer_buffer_length/xfer->seg_size), |
| 529 | WA_SEGS_MAX); |
| 530 | result = -EINVAL; |
| 531 | goto error; |
| 532 | } |
| 533 | if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL) |
| 534 | xfer->segs = 1; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 535 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 536 | error: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 537 | return result; |
| 538 | } |
| 539 | |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 540 | /* Fill in the common request header and xfer-type specific data. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 541 | static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer, |
| 542 | struct wa_xfer_hdr *xfer_hdr0, |
| 543 | enum wa_xfer_type xfer_type, |
| 544 | size_t xfer_hdr_size) |
| 545 | { |
| 546 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 547 | |
| 548 | xfer_hdr0 = &xfer->seg[0]->xfer_hdr; |
| 549 | xfer_hdr0->bLength = xfer_hdr_size; |
| 550 | xfer_hdr0->bRequestType = xfer_type; |
| 551 | xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex; |
Thomas Pugliese | fdd160c | 2013-09-27 15:33:35 -0500 | [diff] [blame] | 552 | xfer_hdr0->dwTransferID = wa_xfer_id_le32(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 553 | xfer_hdr0->bTransferSegment = 0; |
| 554 | switch (xfer_type) { |
| 555 | case WA_XFER_TYPE_CTL: { |
| 556 | struct wa_xfer_ctl *xfer_ctl = |
| 557 | container_of(xfer_hdr0, struct wa_xfer_ctl, hdr); |
| 558 | xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 559 | memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet, |
| 560 | sizeof(xfer_ctl->baSetupData)); |
| 561 | break; |
| 562 | } |
| 563 | case WA_XFER_TYPE_BI: |
| 564 | break; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 565 | case WA_XFER_TYPE_ISO: { |
| 566 | struct wa_xfer_hwaiso *xfer_iso = |
| 567 | container_of(xfer_hdr0, struct wa_xfer_hwaiso, hdr); |
| 568 | struct wa_xfer_packet_info_hwaiso *packet_desc = |
| 569 | ((void *)xfer_iso) + xfer_hdr_size; |
| 570 | struct usb_iso_packet_descriptor *iso_frame_desc = |
| 571 | &(xfer->urb->iso_frame_desc[0]); |
| 572 | /* populate the isoc section of the transfer request. */ |
| 573 | xfer_iso->dwNumOfPackets = cpu_to_le32(1); |
| 574 | /* |
| 575 | * populate isoc packet descriptor. This assumes 1 |
| 576 | * packet per segment. |
| 577 | */ |
| 578 | packet_desc->wLength = cpu_to_le16(sizeof(*packet_desc) + |
| 579 | sizeof(packet_desc->PacketLength[0])); |
| 580 | packet_desc->bPacketType = WA_XFER_ISO_PACKET_INFO; |
| 581 | packet_desc->PacketLength[0] = |
| 582 | cpu_to_le16(iso_frame_desc->length); |
| 583 | break; |
| 584 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 585 | default: |
| 586 | BUG(); |
| 587 | }; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Callback for the OUT data phase of the segment request |
| 592 | * |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 593 | * 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] | 594 | * function does almost the same thing and they work closely |
| 595 | * together. |
| 596 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 597 | * If the seg request has failed but this DTO phase has succeeded, |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 598 | * 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] | 599 | * status to WA_SEG_ERROR, so this will go through 'case 0' and |
| 600 | * effectively do nothing. |
| 601 | */ |
| 602 | static void wa_seg_dto_cb(struct urb *urb) |
| 603 | { |
| 604 | struct wa_seg *seg = urb->context; |
| 605 | struct wa_xfer *xfer = seg->xfer; |
| 606 | struct wahc *wa; |
| 607 | struct device *dev; |
| 608 | struct wa_rpipe *rpipe; |
| 609 | unsigned long flags; |
| 610 | unsigned rpipe_ready = 0; |
| 611 | u8 done = 0; |
| 612 | |
Thomas Pugliese | d5b5c9f | 2013-09-26 14:08:15 -0500 | [diff] [blame] | 613 | /* free the sg if it was used. */ |
| 614 | kfree(urb->sg); |
| 615 | urb->sg = NULL; |
| 616 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 617 | switch (urb->status) { |
| 618 | case 0: |
| 619 | spin_lock_irqsave(&xfer->lock, flags); |
| 620 | wa = xfer->wa; |
| 621 | dev = &wa->usb_iface->dev; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 622 | dev_dbg(dev, "xfer %p#%u: data out done (%d bytes)\n", |
| 623 | xfer, seg->index, urb->actual_length); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 624 | if (seg->status < WA_SEG_PENDING) |
| 625 | seg->status = WA_SEG_PENDING; |
| 626 | seg->result = urb->actual_length; |
| 627 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 628 | break; |
| 629 | case -ECONNRESET: /* URB unlinked; no need to do anything */ |
| 630 | case -ENOENT: /* as it was done by the who unlinked us */ |
| 631 | break; |
| 632 | default: /* Other errors ... */ |
| 633 | spin_lock_irqsave(&xfer->lock, flags); |
| 634 | wa = xfer->wa; |
| 635 | dev = &wa->usb_iface->dev; |
| 636 | rpipe = xfer->ep->hcpriv; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 637 | dev_dbg(dev, "xfer %p#%u: data out error %d\n", |
| 638 | xfer, seg->index, urb->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 639 | if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS, |
| 640 | EDC_ERROR_TIMEFRAME)){ |
| 641 | dev_err(dev, "DTO: URB max acceptable errors " |
| 642 | "exceeded, resetting device\n"); |
| 643 | wa_reset_all(wa); |
| 644 | } |
| 645 | if (seg->status != WA_SEG_ERROR) { |
| 646 | seg->status = WA_SEG_ERROR; |
| 647 | seg->result = urb->status; |
| 648 | xfer->segs_done++; |
| 649 | __wa_xfer_abort(xfer); |
| 650 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 651 | done = __wa_xfer_is_done(xfer); |
| 652 | } |
| 653 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 654 | if (done) |
| 655 | wa_xfer_completion(xfer); |
| 656 | if (rpipe_ready) |
| 657 | wa_xfer_delayed_run(rpipe); |
| 658 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 662 | * Callback for the isoc packet descriptor phase of the segment request |
| 663 | * |
| 664 | * Check wa_seg_tr_cb(); most comments also apply here because this |
| 665 | * function does almost the same thing and they work closely |
| 666 | * together. |
| 667 | * |
| 668 | * If the seg request has failed but this phase has succeeded, |
| 669 | * wa_seg_tr_cb() has already failed the segment and moved the |
| 670 | * status to WA_SEG_ERROR, so this will go through 'case 0' and |
| 671 | * effectively do nothing. |
| 672 | */ |
| 673 | static void wa_seg_iso_pack_desc_cb(struct urb *urb) |
| 674 | { |
| 675 | struct wa_seg *seg = urb->context; |
| 676 | struct wa_xfer *xfer = seg->xfer; |
| 677 | struct wahc *wa; |
| 678 | struct device *dev; |
| 679 | struct wa_rpipe *rpipe; |
| 680 | unsigned long flags; |
| 681 | unsigned rpipe_ready = 0; |
| 682 | u8 done = 0; |
| 683 | |
| 684 | switch (urb->status) { |
| 685 | case 0: |
| 686 | spin_lock_irqsave(&xfer->lock, flags); |
| 687 | wa = xfer->wa; |
| 688 | dev = &wa->usb_iface->dev; |
| 689 | dev_dbg(dev, "iso xfer %p#%u: packet descriptor done\n", |
| 690 | xfer, seg->index); |
| 691 | if (xfer->is_inbound && seg->status < WA_SEG_PENDING) |
| 692 | seg->status = WA_SEG_PENDING; |
| 693 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 694 | break; |
| 695 | case -ECONNRESET: /* URB unlinked; no need to do anything */ |
| 696 | case -ENOENT: /* as it was done by the who unlinked us */ |
| 697 | break; |
| 698 | default: /* Other errors ... */ |
| 699 | spin_lock_irqsave(&xfer->lock, flags); |
| 700 | wa = xfer->wa; |
| 701 | dev = &wa->usb_iface->dev; |
| 702 | rpipe = xfer->ep->hcpriv; |
| 703 | pr_err_ratelimited("iso xfer %p#%u: packet descriptor error %d\n", |
| 704 | xfer, seg->index, urb->status); |
| 705 | if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS, |
| 706 | EDC_ERROR_TIMEFRAME)){ |
| 707 | dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n"); |
| 708 | wa_reset_all(wa); |
| 709 | } |
| 710 | if (seg->status != WA_SEG_ERROR) { |
| 711 | usb_unlink_urb(seg->dto_urb); |
| 712 | seg->status = WA_SEG_ERROR; |
| 713 | seg->result = urb->status; |
| 714 | xfer->segs_done++; |
| 715 | __wa_xfer_abort(xfer); |
| 716 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 717 | done = __wa_xfer_is_done(xfer); |
| 718 | } |
| 719 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 720 | if (done) |
| 721 | wa_xfer_completion(xfer); |
| 722 | if (rpipe_ready) |
| 723 | wa_xfer_delayed_run(rpipe); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 728 | * Callback for the segment request |
| 729 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 730 | * If successful transition state (unless already transitioned or |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 731 | * outbound transfer); otherwise, take a note of the error, mark this |
| 732 | * segment done and try completion. |
| 733 | * |
| 734 | * Note we don't access until we are sure that the transfer hasn't |
| 735 | * been cancelled (ECONNRESET, ENOENT), which could mean that |
| 736 | * seg->xfer could be already gone. |
| 737 | * |
| 738 | * We have to check before setting the status to WA_SEG_PENDING |
| 739 | * because sometimes the xfer result callback arrives before this |
| 740 | * callback (geeeeeeze), so it might happen that we are already in |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 741 | * another state. As well, we don't set it if the transfer is not inbound, |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 742 | * as in that case, wa_seg_dto_cb will do it when the OUT data phase |
| 743 | * finishes. |
| 744 | */ |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 745 | static void wa_seg_tr_cb(struct urb *urb) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 746 | { |
| 747 | struct wa_seg *seg = urb->context; |
| 748 | struct wa_xfer *xfer = seg->xfer; |
| 749 | struct wahc *wa; |
| 750 | struct device *dev; |
| 751 | struct wa_rpipe *rpipe; |
| 752 | unsigned long flags; |
| 753 | unsigned rpipe_ready; |
| 754 | u8 done = 0; |
| 755 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 756 | switch (urb->status) { |
| 757 | case 0: |
| 758 | spin_lock_irqsave(&xfer->lock, flags); |
| 759 | wa = xfer->wa; |
| 760 | dev = &wa->usb_iface->dev; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 761 | dev_dbg(dev, "xfer %p ID 0x%08X#%u: request done\n", |
| 762 | xfer, wa_xfer_id(xfer), seg->index); |
| 763 | if (xfer->is_inbound && |
| 764 | seg->status < WA_SEG_PENDING && |
| 765 | !(usb_pipeisoc(xfer->urb->pipe))) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 766 | seg->status = WA_SEG_PENDING; |
| 767 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 768 | break; |
| 769 | case -ECONNRESET: /* URB unlinked; no need to do anything */ |
| 770 | case -ENOENT: /* as it was done by the who unlinked us */ |
| 771 | break; |
| 772 | default: /* Other errors ... */ |
| 773 | spin_lock_irqsave(&xfer->lock, flags); |
| 774 | wa = xfer->wa; |
| 775 | dev = &wa->usb_iface->dev; |
| 776 | rpipe = xfer->ep->hcpriv; |
| 777 | if (printk_ratelimit()) |
Thomas Pugliese | b9c84be | 2013-09-27 15:33:36 -0500 | [diff] [blame] | 778 | dev_err(dev, "xfer %p ID 0x%08X#%u: request error %d\n", |
| 779 | xfer, wa_xfer_id(xfer), seg->index, |
| 780 | urb->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 781 | if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS, |
| 782 | EDC_ERROR_TIMEFRAME)){ |
| 783 | dev_err(dev, "DTO: URB max acceptable errors " |
| 784 | "exceeded, resetting device\n"); |
| 785 | wa_reset_all(wa); |
| 786 | } |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 787 | usb_unlink_urb(seg->isoc_pack_desc_urb); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 788 | usb_unlink_urb(seg->dto_urb); |
| 789 | seg->status = WA_SEG_ERROR; |
| 790 | seg->result = urb->status; |
| 791 | xfer->segs_done++; |
| 792 | __wa_xfer_abort(xfer); |
| 793 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 794 | done = __wa_xfer_is_done(xfer); |
| 795 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 796 | if (done) |
| 797 | wa_xfer_completion(xfer); |
| 798 | if (rpipe_ready) |
| 799 | wa_xfer_delayed_run(rpipe); |
| 800 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Thomas Pugliese | ffd6d17 | 2013-09-26 14:08:14 -0500 | [diff] [blame] | 803 | /* |
| 804 | * Allocate an SG list to store bytes_to_transfer bytes and copy the |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 805 | * subset of the in_sg that matches the buffer subset |
Thomas Pugliese | ffd6d17 | 2013-09-26 14:08:14 -0500 | [diff] [blame] | 806 | * we are about to transfer. |
| 807 | */ |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 808 | static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg, |
| 809 | const unsigned int bytes_transferred, |
| 810 | const unsigned int bytes_to_transfer, unsigned int *out_num_sgs) |
| 811 | { |
| 812 | struct scatterlist *out_sg; |
| 813 | unsigned int bytes_processed = 0, offset_into_current_page_data = 0, |
| 814 | nents; |
| 815 | struct scatterlist *current_xfer_sg = in_sg; |
| 816 | struct scatterlist *current_seg_sg, *last_seg_sg; |
| 817 | |
| 818 | /* skip previously transferred pages. */ |
| 819 | while ((current_xfer_sg) && |
| 820 | (bytes_processed < bytes_transferred)) { |
| 821 | bytes_processed += current_xfer_sg->length; |
| 822 | |
| 823 | /* advance the sg if current segment starts on or past the |
| 824 | next page. */ |
| 825 | if (bytes_processed <= bytes_transferred) |
| 826 | current_xfer_sg = sg_next(current_xfer_sg); |
| 827 | } |
| 828 | |
| 829 | /* the data for the current segment starts in current_xfer_sg. |
| 830 | calculate the offset. */ |
| 831 | if (bytes_processed > bytes_transferred) { |
| 832 | offset_into_current_page_data = current_xfer_sg->length - |
| 833 | (bytes_processed - bytes_transferred); |
| 834 | } |
| 835 | |
| 836 | /* calculate the number of pages needed by this segment. */ |
| 837 | nents = DIV_ROUND_UP((bytes_to_transfer + |
| 838 | offset_into_current_page_data + |
| 839 | current_xfer_sg->offset), |
| 840 | PAGE_SIZE); |
| 841 | |
| 842 | out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC); |
| 843 | if (out_sg) { |
| 844 | sg_init_table(out_sg, nents); |
| 845 | |
| 846 | /* copy the portion of the incoming SG that correlates to the |
| 847 | * data to be transferred by this segment to the segment SG. */ |
| 848 | last_seg_sg = current_seg_sg = out_sg; |
| 849 | bytes_processed = 0; |
| 850 | |
| 851 | /* reset nents and calculate the actual number of sg entries |
| 852 | needed. */ |
| 853 | nents = 0; |
| 854 | while ((bytes_processed < bytes_to_transfer) && |
| 855 | current_seg_sg && current_xfer_sg) { |
| 856 | unsigned int page_len = min((current_xfer_sg->length - |
| 857 | offset_into_current_page_data), |
| 858 | (bytes_to_transfer - bytes_processed)); |
| 859 | |
| 860 | sg_set_page(current_seg_sg, sg_page(current_xfer_sg), |
| 861 | page_len, |
| 862 | current_xfer_sg->offset + |
| 863 | offset_into_current_page_data); |
| 864 | |
| 865 | bytes_processed += page_len; |
| 866 | |
| 867 | last_seg_sg = current_seg_sg; |
| 868 | current_seg_sg = sg_next(current_seg_sg); |
| 869 | current_xfer_sg = sg_next(current_xfer_sg); |
| 870 | |
| 871 | /* only the first page may require additional offset. */ |
| 872 | offset_into_current_page_data = 0; |
| 873 | nents++; |
| 874 | } |
| 875 | |
| 876 | /* update num_sgs and terminate the list since we may have |
| 877 | * concatenated pages. */ |
| 878 | sg_mark_end(last_seg_sg); |
| 879 | *out_num_sgs = nents; |
| 880 | } |
| 881 | |
| 882 | return out_sg; |
| 883 | } |
| 884 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 885 | /* |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 886 | * Populate DMA buffer info for the isoc dto urb. |
| 887 | */ |
| 888 | static void __wa_populate_dto_urb_iso(struct wa_xfer *xfer, |
| 889 | struct wa_seg *seg, int curr_iso_frame) |
| 890 | { |
| 891 | /* |
| 892 | * dto urb buffer address and size pulled from |
| 893 | * iso_frame_desc. |
| 894 | */ |
| 895 | seg->dto_urb->transfer_dma = xfer->urb->transfer_dma + |
| 896 | xfer->urb->iso_frame_desc[curr_iso_frame].offset; |
| 897 | seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 898 | seg->dto_urb->sg = NULL; |
| 899 | seg->dto_urb->num_sgs = 0; |
| 900 | seg->dto_urb->transfer_buffer_length = |
| 901 | xfer->urb->iso_frame_desc[curr_iso_frame].length; |
| 902 | } |
| 903 | |
| 904 | /* |
Thomas Pugliese | ffd6d17 | 2013-09-26 14:08:14 -0500 | [diff] [blame] | 905 | * Populate buffer ptr and size, DMA buffer or SG list for the dto urb. |
| 906 | */ |
| 907 | static int __wa_populate_dto_urb(struct wa_xfer *xfer, |
| 908 | struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size) |
| 909 | { |
| 910 | int result = 0; |
| 911 | |
| 912 | if (xfer->is_dma) { |
| 913 | seg->dto_urb->transfer_dma = |
| 914 | xfer->urb->transfer_dma + buf_itr_offset; |
| 915 | seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 916 | seg->dto_urb->sg = NULL; |
| 917 | seg->dto_urb->num_sgs = 0; |
| 918 | } else { |
| 919 | /* do buffer or SG processing. */ |
| 920 | seg->dto_urb->transfer_flags &= |
| 921 | ~URB_NO_TRANSFER_DMA_MAP; |
| 922 | /* this should always be 0 before a resubmit. */ |
| 923 | seg->dto_urb->num_mapped_sgs = 0; |
| 924 | |
| 925 | if (xfer->urb->transfer_buffer) { |
| 926 | seg->dto_urb->transfer_buffer = |
| 927 | xfer->urb->transfer_buffer + |
| 928 | buf_itr_offset; |
| 929 | seg->dto_urb->sg = NULL; |
| 930 | seg->dto_urb->num_sgs = 0; |
| 931 | } else { |
| 932 | seg->dto_urb->transfer_buffer = NULL; |
| 933 | |
| 934 | /* |
| 935 | * allocate an SG list to store seg_size bytes |
| 936 | * and copy the subset of the xfer->urb->sg that |
| 937 | * matches the buffer subset we are about to |
| 938 | * read. |
| 939 | */ |
| 940 | seg->dto_urb->sg = wa_xfer_create_subset_sg( |
| 941 | xfer->urb->sg, |
| 942 | buf_itr_offset, buf_itr_size, |
| 943 | &(seg->dto_urb->num_sgs)); |
| 944 | if (!(seg->dto_urb->sg)) |
| 945 | result = -ENOMEM; |
| 946 | } |
| 947 | } |
| 948 | seg->dto_urb->transfer_buffer_length = buf_itr_size; |
| 949 | |
| 950 | return result; |
| 951 | } |
| 952 | |
| 953 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 954 | * Allocate the segs array and initialize each of them |
| 955 | * |
| 956 | * The segments are freed by wa_xfer_destroy() when the xfer use count |
| 957 | * drops to zero; however, because each segment is given the same life |
| 958 | * cycle as the USB URB it contains, it is actually freed by |
| 959 | * usb_put_urb() on the contained USB URB (twisted, eh?). |
| 960 | */ |
| 961 | static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size) |
| 962 | { |
| 963 | int result, cnt; |
| 964 | size_t alloc_size = sizeof(*xfer->seg[0]) |
| 965 | - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size; |
| 966 | struct usb_device *usb_dev = xfer->wa->usb_dev; |
| 967 | const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd; |
| 968 | struct wa_seg *seg; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 969 | size_t buf_itr, buf_size, buf_itr_size, iso_pkt_descr_size = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 970 | |
| 971 | result = -ENOMEM; |
David Vrabel | 92c4d9b | 2008-10-15 14:50:10 +0100 | [diff] [blame] | 972 | xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 973 | if (xfer->seg == NULL) |
| 974 | goto error_segs_kzalloc; |
| 975 | buf_itr = 0; |
| 976 | buf_size = xfer->urb->transfer_buffer_length; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 977 | |
| 978 | if (usb_pipeisoc(xfer->urb->pipe)) { |
| 979 | /* |
| 980 | * This calculation assumes one isoc packet per xfer segment. |
| 981 | * It will need to be updated if this changes. |
| 982 | */ |
| 983 | iso_pkt_descr_size = sizeof(struct wa_xfer_packet_info_hwaiso) + |
| 984 | sizeof(__le16); |
| 985 | alloc_size += iso_pkt_descr_size; |
| 986 | } |
| 987 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 988 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 989 | seg = xfer->seg[cnt] = kmalloc(alloc_size, GFP_ATOMIC); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 990 | if (seg == NULL) |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 991 | goto error_seg_kmalloc; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 992 | wa_seg_init(seg); |
| 993 | seg->xfer = xfer; |
| 994 | seg->index = cnt; |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 995 | usb_fill_bulk_urb(&seg->tr_urb, usb_dev, |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 996 | usb_sndbulkpipe(usb_dev, |
| 997 | dto_epd->bEndpointAddress), |
| 998 | &seg->xfer_hdr, xfer_hdr_size, |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 999 | wa_seg_tr_cb, seg); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1000 | buf_itr_size = min(buf_size, xfer->seg_size); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1001 | if (xfer->is_inbound == 0 && buf_size > 0) { |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1002 | /* outbound data. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1003 | seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 1004 | if (seg->dto_urb == NULL) |
| 1005 | goto error_dto_alloc; |
| 1006 | usb_fill_bulk_urb( |
| 1007 | seg->dto_urb, usb_dev, |
| 1008 | usb_sndbulkpipe(usb_dev, |
| 1009 | dto_epd->bEndpointAddress), |
| 1010 | NULL, 0, wa_seg_dto_cb, seg); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1011 | |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1012 | if (usb_pipeisoc(xfer->urb->pipe)) { |
| 1013 | /* iso packet descriptor. */ |
| 1014 | seg->isoc_pack_desc_urb = |
| 1015 | usb_alloc_urb(0, GFP_ATOMIC); |
| 1016 | if (seg->isoc_pack_desc_urb == NULL) |
| 1017 | goto error_iso_pack_desc_alloc; |
| 1018 | /* |
| 1019 | * The buffer for the isoc packet descriptor |
| 1020 | * after the transfer request header in the |
| 1021 | * segment object memory buffer. |
| 1022 | */ |
| 1023 | usb_fill_bulk_urb( |
| 1024 | seg->isoc_pack_desc_urb, usb_dev, |
| 1025 | usb_sndbulkpipe(usb_dev, |
| 1026 | dto_epd->bEndpointAddress), |
| 1027 | (void *)(&seg->xfer_hdr) + |
| 1028 | xfer_hdr_size, |
| 1029 | iso_pkt_descr_size, |
| 1030 | wa_seg_iso_pack_desc_cb, seg); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1031 | |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1032 | /* fill in the xfer buffer information. */ |
| 1033 | __wa_populate_dto_urb_iso(xfer, seg, cnt); |
| 1034 | } else { |
| 1035 | /* fill in the xfer buffer information. */ |
| 1036 | result = __wa_populate_dto_urb(xfer, seg, |
| 1037 | buf_itr, buf_itr_size); |
| 1038 | if (result < 0) |
| 1039 | goto error_seg_outbound_populate; |
| 1040 | |
| 1041 | buf_itr += buf_itr_size; |
| 1042 | buf_size -= buf_itr_size; |
| 1043 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1044 | } |
| 1045 | seg->status = WA_SEG_READY; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1046 | } |
| 1047 | return 0; |
| 1048 | |
Thomas Pugliese | ffd6d17 | 2013-09-26 14:08:14 -0500 | [diff] [blame] | 1049 | /* |
| 1050 | * Free the memory for the current segment which failed to init. |
| 1051 | * Use the fact that cnt is left at were it failed. The remaining |
| 1052 | * segments will be cleaned up by wa_xfer_destroy. |
| 1053 | */ |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1054 | error_iso_pack_desc_alloc: |
Thomas Pugliese | ffd6d17 | 2013-09-26 14:08:14 -0500 | [diff] [blame] | 1055 | error_seg_outbound_populate: |
Thomas Pugliese | 11b1bf8 | 2013-08-15 14:37:41 -0500 | [diff] [blame] | 1056 | usb_free_urb(xfer->seg[cnt]->dto_urb); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1057 | error_dto_alloc: |
| 1058 | kfree(xfer->seg[cnt]); |
Thomas Pugliese | ffd6d17 | 2013-09-26 14:08:14 -0500 | [diff] [blame] | 1059 | xfer->seg[cnt] = NULL; |
Thomas Pugliese | 66591015d | 2013-08-15 14:37:43 -0500 | [diff] [blame] | 1060 | error_seg_kmalloc: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1061 | error_segs_kzalloc: |
| 1062 | return result; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Allocates all the stuff needed to submit a transfer |
| 1067 | * |
| 1068 | * Breaks the whole data buffer in a list of segments, each one has a |
| 1069 | * structure allocated to it and linked in xfer->seg[index] |
| 1070 | * |
| 1071 | * FIXME: merge setup_segs() and the last part of this function, no |
| 1072 | * need to do two for loops when we could run everything in a |
| 1073 | * single one |
| 1074 | */ |
| 1075 | static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb) |
| 1076 | { |
| 1077 | int result; |
| 1078 | struct device *dev = &xfer->wa->usb_iface->dev; |
| 1079 | enum wa_xfer_type xfer_type = 0; /* shut up GCC */ |
| 1080 | size_t xfer_hdr_size, cnt, transfer_size; |
| 1081 | struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr; |
| 1082 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1083 | result = __wa_xfer_setup_sizes(xfer, &xfer_type); |
| 1084 | if (result < 0) |
| 1085 | goto error_setup_sizes; |
| 1086 | xfer_hdr_size = result; |
| 1087 | result = __wa_xfer_setup_segs(xfer, xfer_hdr_size); |
| 1088 | if (result < 0) { |
| 1089 | dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n", |
| 1090 | xfer, xfer->segs, result); |
| 1091 | goto error_setup_segs; |
| 1092 | } |
| 1093 | /* Fill the first header */ |
| 1094 | xfer_hdr0 = &xfer->seg[0]->xfer_hdr; |
| 1095 | wa_xfer_id_init(xfer); |
| 1096 | __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size); |
| 1097 | |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1098 | /* Fill remaining headers */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1099 | xfer_hdr = xfer_hdr0; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1100 | if (xfer_type == WA_XFER_TYPE_ISO) { |
| 1101 | xfer_hdr0->dwTransferLength = |
| 1102 | cpu_to_le32(xfer->urb->iso_frame_desc[0].length); |
| 1103 | for (cnt = 1; cnt < xfer->segs; cnt++) { |
| 1104 | struct usb_iso_packet_descriptor *iso_frame_desc = |
| 1105 | &(xfer->urb->iso_frame_desc[cnt]); |
| 1106 | struct wa_xfer_packet_info_hwaiso *packet_desc; |
| 1107 | |
| 1108 | xfer_hdr = &xfer->seg[cnt]->xfer_hdr; |
| 1109 | packet_desc = ((void *)xfer_hdr) + xfer_hdr_size; |
| 1110 | /* |
| 1111 | * Copy values from the 0th header and isoc packet |
| 1112 | * descriptor. Segment specific values are set below. |
| 1113 | */ |
| 1114 | memcpy(xfer_hdr, xfer_hdr0, |
| 1115 | xfer_hdr_size + sizeof(*packet_desc)); |
| 1116 | xfer_hdr->bTransferSegment = cnt; |
| 1117 | xfer_hdr->dwTransferLength = |
| 1118 | cpu_to_le32(iso_frame_desc->length); |
| 1119 | /* populate isoc packet descriptor length. */ |
| 1120 | packet_desc->PacketLength[0] = |
| 1121 | cpu_to_le16(iso_frame_desc->length); |
| 1122 | |
| 1123 | xfer->seg[cnt]->status = WA_SEG_READY; |
| 1124 | } |
| 1125 | } else { |
| 1126 | transfer_size = urb->transfer_buffer_length; |
| 1127 | xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ? |
| 1128 | cpu_to_le32(xfer->seg_size) : |
| 1129 | cpu_to_le32(transfer_size); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1130 | transfer_size -= xfer->seg_size; |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1131 | for (cnt = 1; cnt < xfer->segs; cnt++) { |
| 1132 | xfer_hdr = &xfer->seg[cnt]->xfer_hdr; |
| 1133 | memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size); |
| 1134 | xfer_hdr->bTransferSegment = cnt; |
| 1135 | xfer_hdr->dwTransferLength = |
| 1136 | transfer_size > xfer->seg_size ? |
| 1137 | cpu_to_le32(xfer->seg_size) |
| 1138 | : cpu_to_le32(transfer_size); |
| 1139 | xfer->seg[cnt]->status = WA_SEG_READY; |
| 1140 | transfer_size -= xfer->seg_size; |
| 1141 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1142 | } |
| 1143 | xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */ |
| 1144 | result = 0; |
| 1145 | error_setup_segs: |
| 1146 | error_setup_sizes: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1147 | return result; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * |
| 1152 | * |
| 1153 | * rpipe->seg_lock is held! |
| 1154 | */ |
| 1155 | static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer, |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1156 | struct wa_seg *seg, int *dto_done) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1157 | { |
| 1158 | int result; |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1159 | |
| 1160 | /* default to done unless we encounter a multi-frame isoc segment. */ |
| 1161 | *dto_done = 1; |
| 1162 | |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 1163 | /* submit the transfer request. */ |
| 1164 | result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1165 | if (result < 0) { |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1166 | pr_err("%s: xfer %p#%u: REQ submit failed: %d\n", |
| 1167 | __func__, xfer, seg->index, result); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1168 | goto error_seg_submit; |
| 1169 | } |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1170 | /* submit the isoc packet descriptor if present. */ |
| 1171 | if (seg->isoc_pack_desc_urb) { |
| 1172 | result = usb_submit_urb(seg->isoc_pack_desc_urb, GFP_ATOMIC); |
| 1173 | if (result < 0) { |
| 1174 | pr_err("%s: xfer %p#%u: ISO packet descriptor submit failed: %d\n", |
| 1175 | __func__, xfer, seg->index, result); |
| 1176 | goto error_iso_pack_desc_submit; |
| 1177 | } |
| 1178 | } |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 1179 | /* submit the out data if this is an out request. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1180 | if (seg->dto_urb) { |
| 1181 | result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC); |
| 1182 | if (result < 0) { |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1183 | pr_err("%s: xfer %p#%u: DTO submit failed: %d\n", |
| 1184 | __func__, xfer, seg->index, result); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1185 | goto error_dto_submit; |
| 1186 | } |
| 1187 | } |
| 1188 | seg->status = WA_SEG_SUBMITTED; |
| 1189 | rpipe_avail_dec(rpipe); |
| 1190 | return 0; |
| 1191 | |
| 1192 | error_dto_submit: |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1193 | usb_unlink_urb(seg->isoc_pack_desc_urb); |
| 1194 | error_iso_pack_desc_submit: |
Thomas Pugliese | 09d94cb | 2013-09-26 10:49:40 -0500 | [diff] [blame] | 1195 | usb_unlink_urb(&seg->tr_urb); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1196 | error_seg_submit: |
| 1197 | seg->status = WA_SEG_ERROR; |
| 1198 | seg->result = result; |
| 1199 | return result; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1203 | * Execute more queued request segments until the maximum concurrent allowed. |
| 1204 | * Return true if the DTO resource was acquired and released. |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1205 | * |
| 1206 | * The ugly unlock/lock sequence on the error path is needed as the |
| 1207 | * xfer->lock normally nests the seg_lock and not viceversa. |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1208 | */ |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1209 | static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1210 | { |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1211 | int result, dto_acquired = 0, dto_done = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1212 | struct device *dev = &rpipe->wa->usb_iface->dev; |
| 1213 | struct wa_seg *seg; |
| 1214 | struct wa_xfer *xfer; |
| 1215 | unsigned long flags; |
| 1216 | |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1217 | *dto_waiting = 0; |
| 1218 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1219 | spin_lock_irqsave(&rpipe->seg_lock, flags); |
| 1220 | while (atomic_read(&rpipe->segs_available) > 0 |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1221 | && !list_empty(&rpipe->seg_list) |
| 1222 | && (dto_acquired = __wa_dto_try_get(rpipe->wa))) { |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1223 | seg = list_first_entry(&(rpipe->seg_list), struct wa_seg, |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1224 | list_node); |
| 1225 | list_del(&seg->list_node); |
| 1226 | xfer = seg->xfer; |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1227 | result = __wa_seg_submit(rpipe, xfer, seg, &dto_done); |
| 1228 | /* release the dto resource if this RPIPE is done with it. */ |
| 1229 | if (dto_done) |
| 1230 | __wa_dto_put(rpipe->wa); |
Thomas Pugliese | b9c84be | 2013-09-27 15:33:36 -0500 | [diff] [blame] | 1231 | dev_dbg(dev, "xfer %p ID %08X#%u submitted from delayed [%d segments available] %d\n", |
| 1232 | xfer, wa_xfer_id(xfer), seg->index, |
| 1233 | atomic_read(&rpipe->segs_available), result); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1234 | if (unlikely(result < 0)) { |
| 1235 | spin_unlock_irqrestore(&rpipe->seg_lock, flags); |
| 1236 | spin_lock_irqsave(&xfer->lock, flags); |
| 1237 | __wa_xfer_abort(xfer); |
| 1238 | xfer->segs_done++; |
| 1239 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1240 | spin_lock_irqsave(&rpipe->seg_lock, flags); |
| 1241 | } |
| 1242 | } |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1243 | /* |
| 1244 | * Mark this RPIPE as waiting if dto was not acquired, there are |
| 1245 | * delayed segs and no active transfers to wake us up later. |
| 1246 | */ |
| 1247 | if (!dto_acquired && !list_empty(&rpipe->seg_list) |
| 1248 | && (atomic_read(&rpipe->segs_available) == |
| 1249 | le16_to_cpu(rpipe->descr.wRequests))) |
| 1250 | *dto_waiting = 1; |
| 1251 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1252 | spin_unlock_irqrestore(&rpipe->seg_lock, flags); |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1253 | |
| 1254 | return dto_done; |
| 1255 | } |
| 1256 | |
| 1257 | static void wa_xfer_delayed_run(struct wa_rpipe *rpipe) |
| 1258 | { |
| 1259 | int dto_waiting; |
| 1260 | int dto_done = __wa_xfer_delayed_run(rpipe, &dto_waiting); |
| 1261 | |
| 1262 | /* |
| 1263 | * If this RPIPE is waiting on the DTO resource, add it to the tail of |
| 1264 | * the waiting list. |
| 1265 | * Otherwise, if the WA DTO resource was acquired and released by |
| 1266 | * __wa_xfer_delayed_run, another RPIPE may have attempted to acquire |
| 1267 | * DTO and failed during that time. Check the delayed list and process |
| 1268 | * any waiters. Start searching from the next RPIPE index. |
| 1269 | */ |
| 1270 | if (dto_waiting) |
| 1271 | wa_add_delayed_rpipe(rpipe->wa, rpipe); |
| 1272 | else if (dto_done) |
| 1273 | wa_check_for_delayed_rpipes(rpipe->wa); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * |
| 1278 | * xfer->lock is taken |
| 1279 | * |
| 1280 | * On failure submitting we just stop submitting and return error; |
| 1281 | * wa_urb_enqueue_b() will execute the completion path |
| 1282 | */ |
| 1283 | static int __wa_xfer_submit(struct wa_xfer *xfer) |
| 1284 | { |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1285 | int result, dto_acquired = 0, dto_done = 0, dto_waiting = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1286 | struct wahc *wa = xfer->wa; |
| 1287 | struct device *dev = &wa->usb_iface->dev; |
| 1288 | unsigned cnt; |
| 1289 | struct wa_seg *seg; |
| 1290 | unsigned long flags; |
| 1291 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 1292 | size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests); |
| 1293 | u8 available; |
| 1294 | u8 empty; |
| 1295 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1296 | spin_lock_irqsave(&wa->xfer_list_lock, flags); |
| 1297 | list_add_tail(&xfer->list_node, &wa->xfer_list); |
| 1298 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags); |
| 1299 | |
| 1300 | BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests); |
| 1301 | result = 0; |
| 1302 | spin_lock_irqsave(&rpipe->seg_lock, flags); |
| 1303 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1304 | int delay_seg = 1; |
| 1305 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1306 | available = atomic_read(&rpipe->segs_available); |
| 1307 | empty = list_empty(&rpipe->seg_list); |
| 1308 | seg = xfer->seg[cnt]; |
Thomas Pugliese | b9c84be | 2013-09-27 15:33:36 -0500 | [diff] [blame] | 1309 | dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u (%s)\n", |
| 1310 | xfer, wa_xfer_id(xfer), cnt, available, empty, |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 1311 | available == 0 || !empty ? "delayed" : "submitted"); |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1312 | if (available && empty) { |
| 1313 | /* |
| 1314 | * Only attempt to acquire DTO if we have a segment |
| 1315 | * to send. |
| 1316 | */ |
| 1317 | dto_acquired = __wa_dto_try_get(rpipe->wa); |
| 1318 | if (dto_acquired) { |
| 1319 | delay_seg = 0; |
| 1320 | result = __wa_seg_submit(rpipe, xfer, seg, |
| 1321 | &dto_done); |
| 1322 | if (dto_done) |
| 1323 | __wa_dto_put(rpipe->wa); |
| 1324 | |
| 1325 | if (result < 0) { |
| 1326 | __wa_xfer_abort(xfer); |
| 1327 | goto error_seg_submit; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (delay_seg) { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1333 | seg->status = WA_SEG_DELAYED; |
| 1334 | list_add_tail(&seg->list_node, &rpipe->seg_list); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1335 | } |
| 1336 | xfer->segs_submitted++; |
| 1337 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1338 | error_seg_submit: |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1339 | /* |
| 1340 | * Mark this RPIPE as waiting if dto was not acquired, there are |
| 1341 | * delayed segs and no active transfers to wake us up later. |
| 1342 | */ |
| 1343 | if (!dto_acquired && !list_empty(&rpipe->seg_list) |
| 1344 | && (atomic_read(&rpipe->segs_available) == |
| 1345 | le16_to_cpu(rpipe->descr.wRequests))) |
| 1346 | dto_waiting = 1; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1347 | spin_unlock_irqrestore(&rpipe->seg_lock, flags); |
Thomas Pugliese | 679ee47 | 2013-10-07 10:53:57 -0500 | [diff] [blame] | 1348 | |
| 1349 | if (dto_waiting) |
| 1350 | wa_add_delayed_rpipe(rpipe->wa, rpipe); |
| 1351 | else if (dto_done) |
| 1352 | wa_check_for_delayed_rpipes(rpipe->wa); |
| 1353 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1354 | return result; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * Second part of a URB/transfer enqueuement |
| 1359 | * |
| 1360 | * Assumes this comes from wa_urb_enqueue() [maybe through |
| 1361 | * wa_urb_enqueue_run()]. At this point: |
| 1362 | * |
| 1363 | * xfer->wa filled and refcounted |
| 1364 | * xfer->ep filled with rpipe refcounted if |
| 1365 | * delayed == 0 |
| 1366 | * xfer->urb filled and refcounted (this is the case when called |
| 1367 | * from wa_urb_enqueue() as we come from usb_submit_urb() |
| 1368 | * and when called by wa_urb_enqueue_run(), as we took an |
| 1369 | * extra ref dropped by _run() after we return). |
| 1370 | * xfer->gfp filled |
| 1371 | * |
| 1372 | * If we fail at __wa_xfer_submit(), then we just check if we are done |
| 1373 | * and if so, we run the completion procedure. However, if we are not |
| 1374 | * yet done, we do nothing and wait for the completion handlers from |
| 1375 | * the submitted URBs or from the xfer-result path to kick in. If xfer |
| 1376 | * result never kicks in, the xfer will timeout from the USB code and |
| 1377 | * dequeue() will be called. |
| 1378 | */ |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1379 | static int wa_urb_enqueue_b(struct wa_xfer *xfer) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1380 | { |
| 1381 | int result; |
| 1382 | unsigned long flags; |
| 1383 | struct urb *urb = xfer->urb; |
| 1384 | struct wahc *wa = xfer->wa; |
| 1385 | struct wusbhc *wusbhc = wa->wusb; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1386 | struct wusb_dev *wusb_dev; |
| 1387 | unsigned done; |
| 1388 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1389 | result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp); |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1390 | if (result < 0) { |
| 1391 | pr_err("%s: error_rpipe_get\n", __func__); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1392 | goto error_rpipe_get; |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1393 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1394 | result = -ENODEV; |
| 1395 | /* FIXME: segmentation broken -- kills DWA */ |
| 1396 | mutex_lock(&wusbhc->mutex); /* get a WUSB dev */ |
Jiri Slaby | 49fa092 | 2009-03-11 21:47:40 +0100 | [diff] [blame] | 1397 | if (urb->dev == NULL) { |
| 1398 | mutex_unlock(&wusbhc->mutex); |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1399 | pr_err("%s: error usb dev gone\n", __func__); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1400 | goto error_dev_gone; |
Jiri Slaby | 49fa092 | 2009-03-11 21:47:40 +0100 | [diff] [blame] | 1401 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1402 | wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev); |
| 1403 | if (wusb_dev == NULL) { |
| 1404 | mutex_unlock(&wusbhc->mutex); |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1405 | pr_err("%s: error wusb dev gone\n", __func__); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1406 | goto error_dev_gone; |
| 1407 | } |
| 1408 | mutex_unlock(&wusbhc->mutex); |
| 1409 | |
| 1410 | spin_lock_irqsave(&xfer->lock, flags); |
| 1411 | xfer->wusb_dev = wusb_dev; |
| 1412 | result = urb->status; |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1413 | if (urb->status != -EINPROGRESS) { |
| 1414 | pr_err("%s: error_dequeued\n", __func__); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1415 | goto error_dequeued; |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1416 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1417 | |
| 1418 | result = __wa_xfer_setup(xfer, urb); |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1419 | if (result < 0) { |
| 1420 | pr_err("%s: error_xfer_setup\n", __func__); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1421 | goto error_xfer_setup; |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1422 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1423 | result = __wa_xfer_submit(xfer); |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1424 | if (result < 0) { |
| 1425 | pr_err("%s: error_xfer_submit\n", __func__); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1426 | goto error_xfer_submit; |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1427 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1428 | spin_unlock_irqrestore(&xfer->lock, flags); |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1429 | return 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1430 | |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1431 | /* |
| 1432 | * this is basically wa_xfer_completion() broken up wa_xfer_giveback() |
| 1433 | * does a wa_xfer_put() that will call wa_xfer_destroy() and undo |
| 1434 | * setup(). |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1435 | */ |
| 1436 | error_xfer_setup: |
| 1437 | error_dequeued: |
| 1438 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1439 | /* FIXME: segmentation broken, kills DWA */ |
| 1440 | if (wusb_dev) |
| 1441 | wusb_dev_put(wusb_dev); |
| 1442 | error_dev_gone: |
| 1443 | rpipe_put(xfer->ep->hcpriv); |
| 1444 | error_rpipe_get: |
| 1445 | xfer->result = result; |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1446 | return result; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1447 | |
| 1448 | error_xfer_submit: |
| 1449 | done = __wa_xfer_is_done(xfer); |
| 1450 | xfer->result = result; |
| 1451 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1452 | if (done) |
| 1453 | wa_xfer_completion(xfer); |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1454 | /* return success since the completion routine will run. */ |
| 1455 | return 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Execute the delayed transfers in the Wire Adapter @wa |
| 1460 | * |
| 1461 | * We need to be careful here, as dequeue() could be called in the |
| 1462 | * middle. That's why we do the whole thing under the |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1463 | * 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] | 1464 | * 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] | 1465 | * order, we move the delayed list to a separate list while locked and then |
| 1466 | * submit them without the list lock held. |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1467 | */ |
| 1468 | void wa_urb_enqueue_run(struct work_struct *ws) |
| 1469 | { |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1470 | struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1471 | struct wa_xfer *xfer, *next; |
| 1472 | struct urb *urb; |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1473 | LIST_HEAD(tmp_list); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1474 | |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1475 | /* 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] | 1476 | spin_lock_irq(&wa->xfer_list_lock); |
Thomas Pugliese | e9a088f | 2013-08-12 10:10:53 -0500 | [diff] [blame] | 1477 | list_cut_position(&tmp_list, &wa->xfer_delayed_list, |
| 1478 | wa->xfer_delayed_list.prev); |
| 1479 | spin_unlock_irq(&wa->xfer_list_lock); |
| 1480 | |
| 1481 | /* |
| 1482 | * enqueue from temp list without list lock held since wa_urb_enqueue_b |
| 1483 | * can take xfer->lock as well as lock mutexes. |
| 1484 | */ |
| 1485 | list_for_each_entry_safe(xfer, next, &tmp_list, list_node) { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1486 | list_del_init(&xfer->list_node); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1487 | |
| 1488 | urb = xfer->urb; |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1489 | if (wa_urb_enqueue_b(xfer) < 0) |
| 1490 | wa_xfer_giveback(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1491 | usb_put_urb(urb); /* taken when queuing */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1492 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1493 | } |
| 1494 | EXPORT_SYMBOL_GPL(wa_urb_enqueue_run); |
| 1495 | |
| 1496 | /* |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1497 | * Process the errored transfers on the Wire Adapter outside of interrupt. |
| 1498 | */ |
| 1499 | void wa_process_errored_transfers_run(struct work_struct *ws) |
| 1500 | { |
| 1501 | struct wahc *wa = container_of(ws, struct wahc, xfer_error_work); |
| 1502 | struct wa_xfer *xfer, *next; |
| 1503 | LIST_HEAD(tmp_list); |
| 1504 | |
| 1505 | pr_info("%s: Run delayed STALL processing.\n", __func__); |
| 1506 | |
| 1507 | /* Create a copy of the wa->xfer_errored_list while holding the lock */ |
| 1508 | spin_lock_irq(&wa->xfer_list_lock); |
| 1509 | list_cut_position(&tmp_list, &wa->xfer_errored_list, |
| 1510 | wa->xfer_errored_list.prev); |
| 1511 | spin_unlock_irq(&wa->xfer_list_lock); |
| 1512 | |
| 1513 | /* |
| 1514 | * run rpipe_clear_feature_stalled from temp list without list lock |
| 1515 | * held. |
| 1516 | */ |
| 1517 | list_for_each_entry_safe(xfer, next, &tmp_list, list_node) { |
| 1518 | struct usb_host_endpoint *ep; |
| 1519 | unsigned long flags; |
| 1520 | struct wa_rpipe *rpipe; |
| 1521 | |
| 1522 | spin_lock_irqsave(&xfer->lock, flags); |
| 1523 | ep = xfer->ep; |
| 1524 | rpipe = ep->hcpriv; |
| 1525 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1526 | |
| 1527 | /* clear RPIPE feature stalled without holding a lock. */ |
| 1528 | rpipe_clear_feature_stalled(wa, ep); |
| 1529 | |
| 1530 | /* complete the xfer. This removes it from the tmp list. */ |
| 1531 | wa_xfer_completion(xfer); |
| 1532 | |
| 1533 | /* check for work. */ |
| 1534 | wa_xfer_delayed_run(rpipe); |
| 1535 | } |
| 1536 | } |
| 1537 | EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run); |
| 1538 | |
| 1539 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1540 | * Submit a transfer to the Wire Adapter in a delayed way |
| 1541 | * |
| 1542 | * The process of enqueuing involves possible sleeps() [see |
| 1543 | * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are |
| 1544 | * in an atomic section, we defer the enqueue_b() call--else we call direct. |
| 1545 | * |
| 1546 | * @urb: We own a reference to it done by the HCI Linux USB stack that |
| 1547 | * will be given up by calling usb_hcd_giveback_urb() or by |
| 1548 | * returning error from this function -> ergo we don't have to |
| 1549 | * refcount it. |
| 1550 | */ |
| 1551 | int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep, |
| 1552 | struct urb *urb, gfp_t gfp) |
| 1553 | { |
| 1554 | int result; |
| 1555 | struct device *dev = &wa->usb_iface->dev; |
| 1556 | struct wa_xfer *xfer; |
| 1557 | unsigned long my_flags; |
| 1558 | unsigned cant_sleep = irqs_disabled() | in_atomic(); |
| 1559 | |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1560 | if ((urb->transfer_buffer == NULL) |
| 1561 | && (urb->sg == NULL) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1562 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) |
| 1563 | && urb->transfer_buffer_length != 0) { |
| 1564 | dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb); |
| 1565 | dump_stack(); |
| 1566 | } |
| 1567 | |
| 1568 | result = -ENOMEM; |
| 1569 | xfer = kzalloc(sizeof(*xfer), gfp); |
| 1570 | if (xfer == NULL) |
| 1571 | goto error_kmalloc; |
| 1572 | |
| 1573 | result = -ENOENT; |
| 1574 | if (urb->status != -EINPROGRESS) /* cancelled */ |
| 1575 | goto error_dequeued; /* before starting? */ |
| 1576 | wa_xfer_init(xfer); |
| 1577 | xfer->wa = wa_get(wa); |
| 1578 | xfer->urb = urb; |
| 1579 | xfer->gfp = gfp; |
| 1580 | xfer->ep = ep; |
| 1581 | urb->hcpriv = xfer; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 1582 | |
| 1583 | dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n", |
| 1584 | xfer, urb, urb->pipe, urb->transfer_buffer_length, |
| 1585 | urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma", |
| 1586 | urb->pipe & USB_DIR_IN ? "inbound" : "outbound", |
| 1587 | cant_sleep ? "deferred" : "inline"); |
| 1588 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1589 | if (cant_sleep) { |
| 1590 | usb_get_urb(urb); |
| 1591 | spin_lock_irqsave(&wa->xfer_list_lock, my_flags); |
| 1592 | list_add_tail(&xfer->list_node, &wa->xfer_delayed_list); |
| 1593 | spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1594 | queue_work(wusbd, &wa->xfer_enqueue_work); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1595 | } else { |
Thomas Pugliese | 33186c4 | 2013-10-01 10:14:56 -0500 | [diff] [blame] | 1596 | result = wa_urb_enqueue_b(xfer); |
| 1597 | if (result < 0) { |
| 1598 | /* |
| 1599 | * URB submit/enqueue failed. Clean up, return an |
| 1600 | * error and do not run the callback. This avoids |
| 1601 | * an infinite submit/complete loop. |
| 1602 | */ |
| 1603 | dev_err(dev, "%s: URB enqueue failed: %d\n", |
| 1604 | __func__, result); |
| 1605 | wa_put(xfer->wa); |
| 1606 | wa_xfer_put(xfer); |
| 1607 | return result; |
| 1608 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1609 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1610 | return 0; |
| 1611 | |
| 1612 | error_dequeued: |
| 1613 | kfree(xfer); |
| 1614 | error_kmalloc: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1615 | return result; |
| 1616 | } |
| 1617 | EXPORT_SYMBOL_GPL(wa_urb_enqueue); |
| 1618 | |
| 1619 | /* |
| 1620 | * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion |
| 1621 | * handler] is called. |
| 1622 | * |
| 1623 | * Until a transfer goes successfully through wa_urb_enqueue() it |
| 1624 | * needs to be dequeued with completion calling; when stuck in delayed |
| 1625 | * or before wa_xfer_setup() is called, we need to do completion. |
| 1626 | * |
| 1627 | * not setup If there is no hcpriv yet, that means that that enqueue |
| 1628 | * still had no time to set the xfer up. Because |
| 1629 | * urb->status should be other than -EINPROGRESS, |
| 1630 | * enqueue() will catch that and bail out. |
| 1631 | * |
| 1632 | * If the transfer has gone through setup, we just need to clean it |
| 1633 | * up. If it has gone through submit(), we have to abort it [with an |
| 1634 | * asynch request] and then make sure we cancel each segment. |
| 1635 | * |
| 1636 | */ |
| 1637 | int wa_urb_dequeue(struct wahc *wa, struct urb *urb) |
| 1638 | { |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1639 | unsigned long flags, flags2; |
| 1640 | struct wa_xfer *xfer; |
| 1641 | struct wa_seg *seg; |
| 1642 | struct wa_rpipe *rpipe; |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1643 | unsigned cnt, done = 0, xfer_abort_pending; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1644 | unsigned rpipe_ready = 0; |
| 1645 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1646 | xfer = urb->hcpriv; |
| 1647 | if (xfer == NULL) { |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1648 | /* |
| 1649 | * Nothing setup yet enqueue will see urb->status != |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1650 | * -EINPROGRESS (by hcd layer) and bail out with |
| 1651 | * error, no need to do completion |
| 1652 | */ |
| 1653 | BUG_ON(urb->status == -EINPROGRESS); |
| 1654 | goto out; |
| 1655 | } |
| 1656 | spin_lock_irqsave(&xfer->lock, flags); |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1657 | pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer)); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1658 | rpipe = xfer->ep->hcpriv; |
Thomas Pugliese | ec58fad | 2013-08-09 09:52:13 -0500 | [diff] [blame] | 1659 | if (rpipe == NULL) { |
| 1660 | pr_debug("%s: xfer id 0x%08X has no RPIPE. %s", |
| 1661 | __func__, wa_xfer_id(xfer), |
| 1662 | "Probably already aborted.\n" ); |
| 1663 | goto out_unlock; |
| 1664 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1665 | /* Check the delayed list -> if there, release and complete */ |
| 1666 | spin_lock_irqsave(&wa->xfer_list_lock, flags2); |
| 1667 | if (!list_empty(&xfer->list_node) && xfer->seg == NULL) |
| 1668 | goto dequeue_delayed; |
| 1669 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags2); |
| 1670 | if (xfer->seg == NULL) /* still hasn't reached */ |
| 1671 | goto out_unlock; /* setup(), enqueue_b() completes */ |
| 1672 | /* Ok, the xfer is in flight already, it's been setup and submitted.*/ |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1673 | xfer_abort_pending = __wa_xfer_abort(xfer) >= 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1674 | for (cnt = 0; cnt < xfer->segs; cnt++) { |
| 1675 | seg = xfer->seg[cnt]; |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1676 | pr_debug("%s: xfer id 0x%08X#%d status = %d\n", |
| 1677 | __func__, wa_xfer_id(xfer), cnt, seg->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1678 | switch (seg->status) { |
| 1679 | case WA_SEG_NOTREADY: |
| 1680 | case WA_SEG_READY: |
| 1681 | printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n", |
| 1682 | xfer, cnt, seg->status); |
| 1683 | WARN_ON(1); |
| 1684 | break; |
| 1685 | case WA_SEG_DELAYED: |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1686 | /* |
| 1687 | * delete from rpipe delayed list. If no segments on |
| 1688 | * this xfer have been submitted, __wa_xfer_is_done will |
| 1689 | * trigger a giveback below. Otherwise, the submitted |
| 1690 | * segments will be completed in the DTI interrupt. |
| 1691 | */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1692 | seg->status = WA_SEG_ABORTED; |
| 1693 | spin_lock_irqsave(&rpipe->seg_lock, flags2); |
| 1694 | list_del(&seg->list_node); |
| 1695 | xfer->segs_done++; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1696 | spin_unlock_irqrestore(&rpipe->seg_lock, flags2); |
| 1697 | break; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1698 | case WA_SEG_DONE: |
| 1699 | case WA_SEG_ERROR: |
| 1700 | case WA_SEG_ABORTED: |
| 1701 | break; |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1702 | /* |
| 1703 | * In the states below, the HWA device already knows |
| 1704 | * about the transfer. If an abort request was sent, |
| 1705 | * allow the HWA to process it and wait for the |
| 1706 | * results. Otherwise, the DTI state and seg completed |
| 1707 | * counts can get out of sync. |
| 1708 | */ |
| 1709 | case WA_SEG_SUBMITTED: |
| 1710 | case WA_SEG_PENDING: |
| 1711 | case WA_SEG_DTI_PENDING: |
| 1712 | /* |
| 1713 | * Check if the abort was successfully sent. This could |
| 1714 | * be false if the HWA has been removed but we haven't |
| 1715 | * gotten the disconnect notification yet. |
| 1716 | */ |
| 1717 | if (!xfer_abort_pending) { |
| 1718 | seg->status = WA_SEG_ABORTED; |
| 1719 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1720 | xfer->segs_done++; |
| 1721 | } |
| 1722 | break; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | xfer->result = urb->status; /* -ENOENT or -ECONNRESET */ |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1726 | done = __wa_xfer_is_done(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1727 | spin_unlock_irqrestore(&xfer->lock, flags); |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1728 | if (done) |
| 1729 | wa_xfer_completion(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1730 | if (rpipe_ready) |
| 1731 | wa_xfer_delayed_run(rpipe); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1732 | return 0; |
| 1733 | |
| 1734 | out_unlock: |
| 1735 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1736 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1737 | return 0; |
| 1738 | |
| 1739 | dequeue_delayed: |
| 1740 | list_del_init(&xfer->list_node); |
| 1741 | spin_unlock_irqrestore(&wa->xfer_list_lock, flags2); |
| 1742 | xfer->result = urb->status; |
| 1743 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1744 | wa_xfer_giveback(xfer); |
| 1745 | usb_put_urb(urb); /* we got a ref in enqueue() */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1746 | return 0; |
| 1747 | } |
| 1748 | EXPORT_SYMBOL_GPL(wa_urb_dequeue); |
| 1749 | |
| 1750 | /* |
| 1751 | * Translation from WA status codes (WUSB1.0 Table 8.15) to errno |
| 1752 | * codes |
| 1753 | * |
| 1754 | * Positive errno values are internal inconsistencies and should be |
| 1755 | * flagged louder. Negative are to be passed up to the user in the |
| 1756 | * normal way. |
| 1757 | * |
| 1758 | * @status: USB WA status code -- high two bits are stripped. |
| 1759 | */ |
| 1760 | static int wa_xfer_status_to_errno(u8 status) |
| 1761 | { |
| 1762 | int errno; |
| 1763 | u8 real_status = status; |
| 1764 | static int xlat[] = { |
| 1765 | [WA_XFER_STATUS_SUCCESS] = 0, |
| 1766 | [WA_XFER_STATUS_HALTED] = -EPIPE, |
| 1767 | [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS, |
| 1768 | [WA_XFER_STATUS_BABBLE] = -EOVERFLOW, |
| 1769 | [WA_XFER_RESERVED] = EINVAL, |
| 1770 | [WA_XFER_STATUS_NOT_FOUND] = 0, |
| 1771 | [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM, |
| 1772 | [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ, |
| 1773 | [WA_XFER_STATUS_ABORTED] = -EINTR, |
| 1774 | [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL, |
| 1775 | [WA_XFER_INVALID_FORMAT] = EINVAL, |
| 1776 | [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL, |
| 1777 | [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL, |
| 1778 | }; |
| 1779 | status &= 0x3f; |
| 1780 | |
| 1781 | if (status == 0) |
| 1782 | return 0; |
| 1783 | if (status >= ARRAY_SIZE(xlat)) { |
Manuel Zerpies | 9708cd2 | 2011-06-16 14:15:16 +0200 | [diff] [blame] | 1784 | printk_ratelimited(KERN_ERR "%s(): BUG? " |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1785 | "Unknown WA transfer status 0x%02x\n", |
| 1786 | __func__, real_status); |
| 1787 | return -EINVAL; |
| 1788 | } |
| 1789 | errno = xlat[status]; |
| 1790 | if (unlikely(errno > 0)) { |
Manuel Zerpies | 9708cd2 | 2011-06-16 14:15:16 +0200 | [diff] [blame] | 1791 | printk_ratelimited(KERN_ERR "%s(): BUG? " |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1792 | "Inconsistent WA status: 0x%02x\n", |
| 1793 | __func__, real_status); |
| 1794 | errno = -errno; |
| 1795 | } |
| 1796 | return errno; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1800 | * If a last segment flag and/or a transfer result error is encountered, |
| 1801 | * no other segment transfer results will be returned from the device. |
| 1802 | * Mark the remaining submitted or pending xfers as completed so that |
| 1803 | * the xfer will complete cleanly. |
| 1804 | */ |
| 1805 | static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer, |
| 1806 | struct wa_seg *incoming_seg) |
| 1807 | { |
| 1808 | int index; |
| 1809 | struct wa_rpipe *rpipe = xfer->ep->hcpriv; |
| 1810 | |
| 1811 | for (index = incoming_seg->index + 1; index < xfer->segs_submitted; |
| 1812 | index++) { |
| 1813 | struct wa_seg *current_seg = xfer->seg[index]; |
| 1814 | |
| 1815 | BUG_ON(current_seg == NULL); |
| 1816 | |
| 1817 | switch (current_seg->status) { |
| 1818 | case WA_SEG_SUBMITTED: |
| 1819 | case WA_SEG_PENDING: |
| 1820 | case WA_SEG_DTI_PENDING: |
| 1821 | rpipe_avail_inc(rpipe); |
| 1822 | /* |
| 1823 | * do not increment RPIPE avail for the WA_SEG_DELAYED case |
| 1824 | * since it has not been submitted to the RPIPE. |
| 1825 | */ |
| 1826 | case WA_SEG_DELAYED: |
| 1827 | xfer->segs_done++; |
| 1828 | current_seg->status = incoming_seg->status; |
| 1829 | break; |
| 1830 | case WA_SEG_ABORTED: |
| 1831 | break; |
| 1832 | default: |
| 1833 | WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n", |
| 1834 | __func__, wa_xfer_id(xfer), index, |
| 1835 | current_seg->status); |
| 1836 | break; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1842 | * Process a xfer result completion message |
| 1843 | * |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1844 | * inbound transfers: need to schedule a buf_in_urb read |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1845 | * |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1846 | * FIXME: this function needs to be broken up in parts |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1847 | */ |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame] | 1848 | static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer, |
| 1849 | struct wa_xfer_result *xfer_result) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1850 | { |
| 1851 | int result; |
| 1852 | struct device *dev = &wa->usb_iface->dev; |
| 1853 | unsigned long flags; |
| 1854 | u8 seg_idx; |
| 1855 | struct wa_seg *seg; |
| 1856 | struct wa_rpipe *rpipe; |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame] | 1857 | unsigned done = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1858 | u8 usb_status; |
| 1859 | unsigned rpipe_ready = 0; |
| 1860 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1861 | spin_lock_irqsave(&xfer->lock, flags); |
| 1862 | seg_idx = xfer_result->bTransferSegment & 0x7f; |
| 1863 | if (unlikely(seg_idx >= xfer->segs)) |
| 1864 | goto error_bad_seg; |
| 1865 | seg = xfer->seg[seg_idx]; |
| 1866 | rpipe = xfer->ep->hcpriv; |
| 1867 | usb_status = xfer_result->bTransferStatus; |
Thomas Pugliese | b9c84be | 2013-09-27 15:33:36 -0500 | [diff] [blame] | 1868 | dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n", |
| 1869 | xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1870 | if (seg->status == WA_SEG_ABORTED |
| 1871 | || seg->status == WA_SEG_ERROR) /* already handled */ |
| 1872 | goto segment_aborted; |
| 1873 | if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */ |
| 1874 | seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */ |
| 1875 | if (seg->status != WA_SEG_PENDING) { |
| 1876 | if (printk_ratelimit()) |
| 1877 | dev_err(dev, "xfer %p#%u: Bad segment state %u\n", |
| 1878 | xfer, seg_idx, seg->status); |
| 1879 | seg->status = WA_SEG_PENDING; /* workaround/"fix" it */ |
| 1880 | } |
| 1881 | if (usb_status & 0x80) { |
| 1882 | seg->result = wa_xfer_status_to_errno(usb_status); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1883 | dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n", |
| 1884 | xfer, xfer->id, seg->index, usb_status); |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1885 | seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ? |
| 1886 | WA_SEG_ABORTED : WA_SEG_ERROR; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1887 | goto error_complete; |
| 1888 | } |
| 1889 | /* FIXME: we ignore warnings, tally them for stats */ |
| 1890 | if (usb_status & 0x40) /* Warning?... */ |
| 1891 | usb_status = 0; /* ... pass */ |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 1892 | if (usb_pipeisoc(xfer->urb->pipe)) { |
| 1893 | /* set up WA state to read the isoc packet status next. */ |
| 1894 | wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer); |
| 1895 | wa->dti_isoc_xfer_seg = seg_idx; |
| 1896 | wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING; |
| 1897 | } else if (xfer->is_inbound) { /* IN data phase: read to buffer */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1898 | seg->status = WA_SEG_DTI_PENDING; |
| 1899 | BUG_ON(wa->buf_in_urb->status == -EINPROGRESS); |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1900 | /* this should always be 0 before a resubmit. */ |
| 1901 | wa->buf_in_urb->num_mapped_sgs = 0; |
| 1902 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1903 | if (xfer->is_dma) { |
| 1904 | wa->buf_in_urb->transfer_dma = |
| 1905 | xfer->urb->transfer_dma |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1906 | + (seg_idx * xfer->seg_size); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1907 | wa->buf_in_urb->transfer_flags |
| 1908 | |= URB_NO_TRANSFER_DMA_MAP; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1909 | wa->buf_in_urb->transfer_buffer = NULL; |
| 1910 | wa->buf_in_urb->sg = NULL; |
| 1911 | wa->buf_in_urb->num_sgs = 0; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1912 | } else { |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1913 | /* do buffer or SG processing. */ |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1914 | wa->buf_in_urb->transfer_flags |
| 1915 | &= ~URB_NO_TRANSFER_DMA_MAP; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1916 | |
| 1917 | if (xfer->urb->transfer_buffer) { |
| 1918 | wa->buf_in_urb->transfer_buffer = |
| 1919 | xfer->urb->transfer_buffer |
| 1920 | + (seg_idx * xfer->seg_size); |
| 1921 | wa->buf_in_urb->sg = NULL; |
| 1922 | wa->buf_in_urb->num_sgs = 0; |
| 1923 | } else { |
| 1924 | /* allocate an SG list to store seg_size bytes |
| 1925 | and copy the subset of the xfer->urb->sg |
| 1926 | that matches the buffer subset we are |
| 1927 | about to read. */ |
| 1928 | wa->buf_in_urb->sg = wa_xfer_create_subset_sg( |
| 1929 | xfer->urb->sg, |
| 1930 | seg_idx * xfer->seg_size, |
| 1931 | le32_to_cpu( |
| 1932 | xfer_result->dwTransferLength), |
| 1933 | &(wa->buf_in_urb->num_sgs)); |
| 1934 | |
| 1935 | if (!(wa->buf_in_urb->sg)) { |
| 1936 | wa->buf_in_urb->num_sgs = 0; |
| 1937 | goto error_sg_alloc; |
| 1938 | } |
| 1939 | wa->buf_in_urb->transfer_buffer = NULL; |
| 1940 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1941 | } |
| 1942 | wa->buf_in_urb->transfer_buffer_length = |
| 1943 | le32_to_cpu(xfer_result->dwTransferLength); |
| 1944 | wa->buf_in_urb->context = seg; |
| 1945 | result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC); |
| 1946 | if (result < 0) |
| 1947 | goto error_submit_buf_in; |
| 1948 | } else { |
| 1949 | /* OUT data phase, complete it -- */ |
| 1950 | seg->status = WA_SEG_DONE; |
| 1951 | seg->result = le32_to_cpu(xfer_result->dwTransferLength); |
| 1952 | xfer->segs_done++; |
| 1953 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 1954 | done = __wa_xfer_is_done(xfer); |
| 1955 | } |
| 1956 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1957 | if (done) |
| 1958 | wa_xfer_completion(xfer); |
| 1959 | if (rpipe_ready) |
| 1960 | wa_xfer_delayed_run(rpipe); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1961 | return; |
| 1962 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1963 | error_submit_buf_in: |
| 1964 | if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) { |
| 1965 | dev_err(dev, "DTI: URB max acceptable errors " |
| 1966 | "exceeded, resetting device\n"); |
| 1967 | wa_reset_all(wa); |
| 1968 | } |
| 1969 | if (printk_ratelimit()) |
| 1970 | dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n", |
| 1971 | xfer, seg_idx, result); |
| 1972 | seg->result = result; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1973 | kfree(wa->buf_in_urb->sg); |
Thomas Pugliese | 6741448 | 2013-09-26 14:08:16 -0500 | [diff] [blame] | 1974 | wa->buf_in_urb->sg = NULL; |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 1975 | error_sg_alloc: |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1976 | __wa_xfer_abort(xfer); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1977 | seg->status = WA_SEG_ERROR; |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1978 | error_complete: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1979 | xfer->segs_done++; |
| 1980 | rpipe_ready = rpipe_avail_inc(rpipe); |
Thomas Pugliese | 14e1d2d | 2013-09-30 15:58:24 -0500 | [diff] [blame] | 1981 | wa_complete_remaining_xfer_segs(xfer, seg); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 1982 | done = __wa_xfer_is_done(xfer); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1983 | /* |
| 1984 | * queue work item to clear STALL for control endpoints. |
| 1985 | * Otherwise, let endpoint_reset take care of it. |
| 1986 | */ |
| 1987 | if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) && |
| 1988 | usb_endpoint_xfer_control(&xfer->ep->desc) && |
| 1989 | done) { |
| 1990 | |
| 1991 | dev_info(dev, "Control EP stall. Queue delayed work.\n"); |
| 1992 | spin_lock_irq(&wa->xfer_list_lock); |
Wei Yongjun | 8eb4129 | 2013-09-23 14:16:22 +0800 | [diff] [blame] | 1993 | /* move xfer from xfer_list to xfer_errored_list. */ |
| 1994 | list_move_tail(&xfer->list_node, &wa->xfer_errored_list); |
Thomas Pugliese | 6d33f7b | 2013-08-15 12:21:30 -0500 | [diff] [blame] | 1995 | spin_unlock_irq(&wa->xfer_list_lock); |
| 1996 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 1997 | queue_work(wusbd, &wa->xfer_error_work); |
| 1998 | } else { |
| 1999 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 2000 | if (done) |
| 2001 | wa_xfer_completion(xfer); |
| 2002 | if (rpipe_ready) |
| 2003 | wa_xfer_delayed_run(rpipe); |
| 2004 | } |
| 2005 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2006 | return; |
| 2007 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2008 | error_bad_seg: |
| 2009 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 2010 | wa_urb_dequeue(wa, xfer->urb); |
| 2011 | if (printk_ratelimit()) |
| 2012 | dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx); |
| 2013 | if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) { |
| 2014 | dev_err(dev, "DTI: URB max acceptable errors " |
| 2015 | "exceeded, resetting device\n"); |
| 2016 | wa_reset_all(wa); |
| 2017 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2018 | return; |
| 2019 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2020 | segment_aborted: |
| 2021 | /* nothing to do, as the aborter did the completion */ |
| 2022 | spin_unlock_irqrestore(&xfer->lock, flags); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 2026 | * Process a isochronous packet status message |
| 2027 | * |
| 2028 | * inbound transfers: need to schedule a buf_in_urb read |
| 2029 | */ |
| 2030 | static void wa_process_iso_packet_status(struct wahc *wa, struct urb *urb) |
| 2031 | { |
| 2032 | struct device *dev = &wa->usb_iface->dev; |
| 2033 | struct wa_xfer_packet_status_hwaiso *packet_status; |
| 2034 | struct wa_xfer *xfer; |
| 2035 | unsigned long flags; |
| 2036 | struct wa_seg *seg; |
| 2037 | struct wa_rpipe *rpipe; |
| 2038 | unsigned done = 0; |
| 2039 | unsigned rpipe_ready = 0; |
| 2040 | const int expected_size = sizeof(*packet_status) + |
| 2041 | sizeof(packet_status->PacketStatus[0]); |
| 2042 | |
| 2043 | /* We have a xfer result buffer; check it */ |
| 2044 | dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n", |
| 2045 | urb->actual_length, urb->transfer_buffer); |
| 2046 | if (urb->actual_length != expected_size) { |
Thomas Pugliese | afc3cba | 2013-10-06 13:48:47 -0500 | [diff] [blame] | 2047 | dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n", |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 2048 | urb->actual_length, expected_size); |
| 2049 | goto error_parse_buffer; |
| 2050 | } |
| 2051 | packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf); |
| 2052 | if (le16_to_cpu(packet_status->wLength) != expected_size) { |
| 2053 | dev_err(dev, "DTI Error: isoc packet status--bad length %u\n", |
| 2054 | le16_to_cpu(packet_status->wLength)); |
| 2055 | goto error_parse_buffer; |
| 2056 | } |
| 2057 | if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) { |
| 2058 | dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n", |
| 2059 | packet_status->bPacketType); |
| 2060 | goto error_parse_buffer; |
| 2061 | } |
| 2062 | xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress); |
| 2063 | if (xfer == NULL) { |
| 2064 | dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n", |
| 2065 | wa->dti_isoc_xfer_in_progress); |
| 2066 | goto error_parse_buffer; |
| 2067 | } |
| 2068 | spin_lock_irqsave(&xfer->lock, flags); |
| 2069 | if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs)) |
| 2070 | goto error_bad_seg; |
| 2071 | seg = xfer->seg[wa->dti_isoc_xfer_seg]; |
| 2072 | rpipe = xfer->ep->hcpriv; |
| 2073 | |
| 2074 | /* set urb isoc packet status and length. */ |
| 2075 | xfer->urb->iso_frame_desc[seg->index].status = |
| 2076 | wa_xfer_status_to_errno( |
| 2077 | le16_to_cpu(packet_status->PacketStatus[0].PacketStatus)); |
| 2078 | xfer->urb->iso_frame_desc[seg->index].actual_length = |
| 2079 | le16_to_cpu(packet_status->PacketStatus[0].PacketLength); |
| 2080 | |
| 2081 | if (!xfer->is_inbound) { |
| 2082 | /* OUT transfer, complete it -- */ |
| 2083 | seg->status = WA_SEG_DONE; |
| 2084 | xfer->segs_done++; |
| 2085 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 2086 | done = __wa_xfer_is_done(xfer); |
| 2087 | } |
| 2088 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 2089 | wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING; |
| 2090 | if (done) |
| 2091 | wa_xfer_completion(xfer); |
| 2092 | if (rpipe_ready) |
| 2093 | wa_xfer_delayed_run(rpipe); |
| 2094 | wa_xfer_put(xfer); |
| 2095 | return; |
| 2096 | |
| 2097 | error_bad_seg: |
| 2098 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 2099 | wa_xfer_put(xfer); |
| 2100 | error_parse_buffer: |
| 2101 | return; |
| 2102 | } |
| 2103 | |
| 2104 | /* |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2105 | * Callback for the IN data phase |
| 2106 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2107 | * If successful transition state; otherwise, take a note of the |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2108 | * error, mark this segment done and try completion. |
| 2109 | * |
| 2110 | * Note we don't access until we are sure that the transfer hasn't |
| 2111 | * been cancelled (ECONNRESET, ENOENT), which could mean that |
| 2112 | * seg->xfer could be already gone. |
| 2113 | */ |
| 2114 | static void wa_buf_in_cb(struct urb *urb) |
| 2115 | { |
| 2116 | struct wa_seg *seg = urb->context; |
| 2117 | struct wa_xfer *xfer = seg->xfer; |
| 2118 | struct wahc *wa; |
| 2119 | struct device *dev; |
| 2120 | struct wa_rpipe *rpipe; |
| 2121 | unsigned rpipe_ready; |
| 2122 | unsigned long flags; |
| 2123 | u8 done = 0; |
| 2124 | |
Thomas Pugliese | 2b81c08 | 2013-06-11 10:39:31 -0500 | [diff] [blame] | 2125 | /* free the sg if it was used. */ |
| 2126 | kfree(urb->sg); |
| 2127 | urb->sg = NULL; |
| 2128 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2129 | switch (urb->status) { |
| 2130 | case 0: |
| 2131 | spin_lock_irqsave(&xfer->lock, flags); |
| 2132 | wa = xfer->wa; |
| 2133 | dev = &wa->usb_iface->dev; |
| 2134 | rpipe = xfer->ep->hcpriv; |
David Vrabel | bce8369 | 2008-12-22 18:22:50 +0000 | [diff] [blame] | 2135 | dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n", |
| 2136 | xfer, seg->index, (size_t)urb->actual_length); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2137 | seg->status = WA_SEG_DONE; |
| 2138 | seg->result = urb->actual_length; |
| 2139 | xfer->segs_done++; |
| 2140 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 2141 | done = __wa_xfer_is_done(xfer); |
| 2142 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 2143 | if (done) |
| 2144 | wa_xfer_completion(xfer); |
| 2145 | if (rpipe_ready) |
| 2146 | wa_xfer_delayed_run(rpipe); |
| 2147 | break; |
| 2148 | case -ECONNRESET: /* URB unlinked; no need to do anything */ |
| 2149 | case -ENOENT: /* as it was done by the who unlinked us */ |
| 2150 | break; |
| 2151 | default: /* Other errors ... */ |
| 2152 | spin_lock_irqsave(&xfer->lock, flags); |
| 2153 | wa = xfer->wa; |
| 2154 | dev = &wa->usb_iface->dev; |
| 2155 | rpipe = xfer->ep->hcpriv; |
| 2156 | if (printk_ratelimit()) |
| 2157 | dev_err(dev, "xfer %p#%u: data in error %d\n", |
| 2158 | xfer, seg->index, urb->status); |
| 2159 | if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS, |
| 2160 | EDC_ERROR_TIMEFRAME)){ |
| 2161 | dev_err(dev, "DTO: URB max acceptable errors " |
| 2162 | "exceeded, resetting device\n"); |
| 2163 | wa_reset_all(wa); |
| 2164 | } |
| 2165 | seg->status = WA_SEG_ERROR; |
| 2166 | seg->result = urb->status; |
| 2167 | xfer->segs_done++; |
| 2168 | rpipe_ready = rpipe_avail_inc(rpipe); |
| 2169 | __wa_xfer_abort(xfer); |
| 2170 | done = __wa_xfer_is_done(xfer); |
| 2171 | spin_unlock_irqrestore(&xfer->lock, flags); |
| 2172 | if (done) |
| 2173 | wa_xfer_completion(xfer); |
| 2174 | if (rpipe_ready) |
| 2175 | wa_xfer_delayed_run(rpipe); |
| 2176 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * Handle an incoming transfer result buffer |
| 2181 | * |
| 2182 | * Given a transfer result buffer, it completes the transfer (possibly |
| 2183 | * scheduling and buffer in read) and then resubmits the DTI URB for a |
| 2184 | * new transfer result read. |
| 2185 | * |
| 2186 | * |
| 2187 | * The xfer_result DTI URB state machine |
| 2188 | * |
| 2189 | * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In) |
| 2190 | * |
| 2191 | * We start in OFF mode, the first xfer_result notification [through |
| 2192 | * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to |
| 2193 | * read. |
| 2194 | * |
| 2195 | * We receive a buffer -- if it is not a xfer_result, we complain and |
| 2196 | * repost the DTI-URB. If it is a xfer_result then do the xfer seg |
| 2197 | * request accounting. If it is an IN segment, we move to RBI and post |
| 2198 | * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will |
| 2199 | * repost the DTI-URB and move to RXR state. if there was no IN |
| 2200 | * segment, it will repost the DTI-URB. |
| 2201 | * |
| 2202 | * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many |
| 2203 | * errors) in the URBs. |
| 2204 | */ |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame] | 2205 | static void wa_dti_cb(struct urb *urb) |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2206 | { |
| 2207 | int result; |
| 2208 | struct wahc *wa = urb->context; |
| 2209 | struct device *dev = &wa->usb_iface->dev; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2210 | u32 xfer_id; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2211 | u8 usb_status; |
| 2212 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2213 | BUG_ON(wa->dti_urb != urb); |
| 2214 | switch (wa->dti_urb->status) { |
| 2215 | case 0: |
Thomas Pugliese | 7a32d9b | 2013-10-04 10:40:45 -0500 | [diff] [blame] | 2216 | if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) { |
| 2217 | struct wa_xfer_result *xfer_result; |
| 2218 | struct wa_xfer *xfer; |
| 2219 | |
| 2220 | /* We have a xfer result buffer; check it */ |
| 2221 | dev_dbg(dev, "DTI: xfer result %d bytes at %p\n", |
| 2222 | urb->actual_length, urb->transfer_buffer); |
| 2223 | if (urb->actual_length != sizeof(*xfer_result)) { |
| 2224 | dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n", |
| 2225 | urb->actual_length, |
| 2226 | sizeof(*xfer_result)); |
| 2227 | break; |
| 2228 | } |
| 2229 | xfer_result = (struct wa_xfer_result *)(wa->dti_buf); |
| 2230 | if (xfer_result->hdr.bLength != sizeof(*xfer_result)) { |
| 2231 | dev_err(dev, "DTI Error: xfer result--bad header length %u\n", |
| 2232 | xfer_result->hdr.bLength); |
| 2233 | break; |
| 2234 | } |
| 2235 | if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) { |
| 2236 | dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n", |
| 2237 | xfer_result->hdr.bNotifyType); |
| 2238 | break; |
| 2239 | } |
| 2240 | usb_status = xfer_result->bTransferStatus & 0x3f; |
| 2241 | if (usb_status == WA_XFER_STATUS_NOT_FOUND) |
| 2242 | /* taken care of already */ |
| 2243 | break; |
| 2244 | xfer_id = le32_to_cpu(xfer_result->dwTransferID); |
| 2245 | xfer = wa_xfer_get_by_id(wa, xfer_id); |
| 2246 | if (xfer == NULL) { |
| 2247 | /* FIXME: transaction not found. */ |
| 2248 | dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n", |
| 2249 | xfer_id, usb_status); |
| 2250 | break; |
| 2251 | } |
| 2252 | wa_xfer_result_chew(wa, xfer, xfer_result); |
| 2253 | wa_xfer_put(xfer); |
| 2254 | } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) { |
| 2255 | wa_process_iso_packet_status(wa, urb); |
| 2256 | } else { |
| 2257 | dev_err(dev, "DTI Error: unexpected EP state = %d\n", |
| 2258 | wa->dti_state); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2259 | } |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2260 | break; |
| 2261 | case -ENOENT: /* (we killed the URB)...so, no broadcast */ |
| 2262 | case -ESHUTDOWN: /* going away! */ |
| 2263 | dev_dbg(dev, "DTI: going down! %d\n", urb->status); |
| 2264 | goto out; |
| 2265 | default: |
| 2266 | /* Unknown error */ |
| 2267 | if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, |
| 2268 | EDC_ERROR_TIMEFRAME)) { |
| 2269 | dev_err(dev, "DTI: URB max acceptable errors " |
| 2270 | "exceeded, resetting device\n"); |
| 2271 | wa_reset_all(wa); |
| 2272 | goto out; |
| 2273 | } |
| 2274 | if (printk_ratelimit()) |
| 2275 | dev_err(dev, "DTI: URB error %d\n", urb->status); |
| 2276 | break; |
| 2277 | } |
| 2278 | /* Resubmit the DTI URB */ |
| 2279 | result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC); |
| 2280 | if (result < 0) { |
| 2281 | dev_err(dev, "DTI Error: Could not submit DTI URB (%d), " |
| 2282 | "resetting\n", result); |
| 2283 | wa_reset_all(wa); |
| 2284 | } |
| 2285 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2286 | return; |
| 2287 | } |
| 2288 | |
| 2289 | /* |
| 2290 | * Transfer complete notification |
| 2291 | * |
| 2292 | * Called from the notif.c code. We get a notification on EP2 saying |
| 2293 | * that some endpoint has some transfer result data available. We are |
| 2294 | * about to read it. |
| 2295 | * |
| 2296 | * To speed up things, we always have a URB reading the DTI URB; we |
| 2297 | * don't really set it up and start it until the first xfer complete |
| 2298 | * notification arrives, which is what we do here. |
| 2299 | * |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame] | 2300 | * 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] | 2301 | * machine starts. |
| 2302 | * |
| 2303 | * So here we just initialize the DTI URB for reading transfer result |
| 2304 | * notifications and also the buffer-in URB, for reading buffers. Then |
| 2305 | * we just submit the DTI URB. |
| 2306 | * |
| 2307 | * @wa shall be referenced |
| 2308 | */ |
| 2309 | void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr) |
| 2310 | { |
| 2311 | int result; |
| 2312 | struct device *dev = &wa->usb_iface->dev; |
| 2313 | struct wa_notif_xfer *notif_xfer; |
| 2314 | const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd; |
| 2315 | |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2316 | notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr); |
| 2317 | BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER); |
| 2318 | |
| 2319 | if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) { |
| 2320 | /* FIXME: hardcoded limitation, adapt */ |
| 2321 | dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n", |
| 2322 | notif_xfer->bEndpoint, dti_epd->bEndpointAddress); |
| 2323 | goto error; |
| 2324 | } |
| 2325 | if (wa->dti_urb != NULL) /* DTI URB already started */ |
| 2326 | goto out; |
| 2327 | |
| 2328 | wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 2329 | if (wa->dti_urb == NULL) { |
| 2330 | dev_err(dev, "Can't allocate DTI URB\n"); |
| 2331 | goto error_dti_urb_alloc; |
| 2332 | } |
| 2333 | usb_fill_bulk_urb( |
| 2334 | wa->dti_urb, wa->usb_dev, |
| 2335 | usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint), |
Thomas Pugliese | 0367eef | 2013-09-26 10:49:41 -0500 | [diff] [blame] | 2336 | wa->dti_buf, wa->dti_buf_size, |
| 2337 | wa_dti_cb, wa); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2338 | |
| 2339 | wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 2340 | if (wa->buf_in_urb == NULL) { |
| 2341 | dev_err(dev, "Can't allocate BUF-IN URB\n"); |
| 2342 | goto error_buf_in_urb_alloc; |
| 2343 | } |
| 2344 | usb_fill_bulk_urb( |
| 2345 | wa->buf_in_urb, wa->usb_dev, |
| 2346 | usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint), |
| 2347 | NULL, 0, wa_buf_in_cb, wa); |
| 2348 | result = usb_submit_urb(wa->dti_urb, GFP_KERNEL); |
| 2349 | if (result < 0) { |
| 2350 | dev_err(dev, "DTI Error: Could not submit DTI URB (%d), " |
| 2351 | "resetting\n", result); |
| 2352 | goto error_dti_urb_submit; |
| 2353 | } |
| 2354 | out: |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2355 | return; |
| 2356 | |
| 2357 | error_dti_urb_submit: |
| 2358 | usb_put_urb(wa->buf_in_urb); |
Thomas Pugliese | 6741448 | 2013-09-26 14:08:16 -0500 | [diff] [blame] | 2359 | wa->buf_in_urb = NULL; |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2360 | error_buf_in_urb_alloc: |
| 2361 | usb_put_urb(wa->dti_urb); |
| 2362 | wa->dti_urb = NULL; |
| 2363 | error_dti_urb_alloc: |
| 2364 | error: |
| 2365 | wa_reset_all(wa); |
Inaky Perez-Gonzalez | df36542 | 2008-09-17 16:34:29 +0100 | [diff] [blame] | 2366 | } |