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