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