Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1 | /* |
| 2 | * MUSB OTG driver peripheral support |
| 3 | * |
| 4 | * Copyright 2005 Mentor Graphics Corporation |
| 5 | * Copyright (C) 2005-2006 by Texas Instruments |
| 6 | * Copyright (C) 2006-2007 Nokia Corporation |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 7 | * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com> |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| 26 | * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 29 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 30 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/list.h> |
| 38 | #include <linux/timer.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/smp.h> |
| 41 | #include <linux/spinlock.h> |
| 42 | #include <linux/delay.h> |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 43 | #include <linux/dma-mapping.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 44 | #include <linux/slab.h> |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 45 | |
| 46 | #include "musb_core.h" |
| 47 | |
| 48 | |
| 49 | /* MUSB PERIPHERAL status 3-mar-2006: |
| 50 | * |
| 51 | * - EP0 seems solid. It passes both USBCV and usbtest control cases. |
| 52 | * Minor glitches: |
| 53 | * |
| 54 | * + remote wakeup to Linux hosts work, but saw USBCV failures; |
| 55 | * in one test run (operator error?) |
| 56 | * + endpoint halt tests -- in both usbtest and usbcv -- seem |
| 57 | * to break when dma is enabled ... is something wrongly |
| 58 | * clearing SENDSTALL? |
| 59 | * |
| 60 | * - Mass storage behaved ok when last tested. Network traffic patterns |
| 61 | * (with lots of short transfers etc) need retesting; they turn up the |
| 62 | * worst cases of the DMA, since short packets are typical but are not |
| 63 | * required. |
| 64 | * |
| 65 | * - TX/IN |
| 66 | * + both pio and dma behave in with network and g_zero tests |
| 67 | * + no cppi throughput issues other than no-hw-queueing |
| 68 | * + failed with FLAT_REG (DaVinci) |
| 69 | * + seems to behave with double buffering, PIO -and- CPPI |
| 70 | * + with gadgetfs + AIO, requests got lost? |
| 71 | * |
| 72 | * - RX/OUT |
| 73 | * + both pio and dma behave in with network and g_zero tests |
| 74 | * + dma is slow in typical case (short_not_ok is clear) |
| 75 | * + double buffering ok with PIO |
| 76 | * + double buffering *FAILS* with CPPI, wrong data bytes sometimes |
| 77 | * + request lossage observed with gadgetfs |
| 78 | * |
| 79 | * - ISO not tested ... might work, but only weakly isochronous |
| 80 | * |
| 81 | * - Gadget driver disabling of softconnect during bind() is ignored; so |
| 82 | * drivers can't hold off host requests until userspace is ready. |
| 83 | * (Workaround: they can turn it off later.) |
| 84 | * |
| 85 | * - PORTABILITY (assumes PIO works): |
| 86 | * + DaVinci, basically works with cppi dma |
| 87 | * + OMAP 2430, ditto with mentor dma |
| 88 | * + TUSB 6010, platform-specific dma in the works |
| 89 | */ |
| 90 | |
| 91 | /* ----------------------------------------------------------------------- */ |
| 92 | |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 93 | #define is_buffer_mapped(req) (is_dma_capable() && \ |
| 94 | (req->map_state != UN_MAPPED)) |
| 95 | |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 96 | /* Maps the buffer to dma */ |
| 97 | |
| 98 | static inline void map_dma_buffer(struct musb_request *request, |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 99 | struct musb *musb, struct musb_ep *musb_ep) |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 100 | { |
Mian Yousaf Kaukab | 5f5761c | 2011-01-04 12:47:03 +0100 | [diff] [blame] | 101 | int compatible = true; |
| 102 | struct dma_controller *dma = musb->dma_controller; |
| 103 | |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 104 | request->map_state = UN_MAPPED; |
| 105 | |
| 106 | if (!is_dma_capable() || !musb_ep->dma) |
| 107 | return; |
| 108 | |
Mian Yousaf Kaukab | 5f5761c | 2011-01-04 12:47:03 +0100 | [diff] [blame] | 109 | /* Check if DMA engine can handle this request. |
| 110 | * DMA code must reject the USB request explicitly. |
| 111 | * Default behaviour is to map the request. |
| 112 | */ |
| 113 | if (dma->is_compatible) |
| 114 | compatible = dma->is_compatible(musb_ep->dma, |
| 115 | musb_ep->packet_sz, request->request.buf, |
| 116 | request->request.length); |
| 117 | if (!compatible) |
| 118 | return; |
| 119 | |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 120 | if (request->request.dma == DMA_ADDR_INVALID) { |
| 121 | request->request.dma = dma_map_single( |
| 122 | musb->controller, |
| 123 | request->request.buf, |
| 124 | request->request.length, |
| 125 | request->tx |
| 126 | ? DMA_TO_DEVICE |
| 127 | : DMA_FROM_DEVICE); |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 128 | request->map_state = MUSB_MAPPED; |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 129 | } else { |
| 130 | dma_sync_single_for_device(musb->controller, |
| 131 | request->request.dma, |
| 132 | request->request.length, |
| 133 | request->tx |
| 134 | ? DMA_TO_DEVICE |
| 135 | : DMA_FROM_DEVICE); |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 136 | request->map_state = PRE_MAPPED; |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | /* Unmap the buffer from dma and maps it back to cpu */ |
| 141 | static inline void unmap_dma_buffer(struct musb_request *request, |
| 142 | struct musb *musb) |
| 143 | { |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 144 | if (!is_buffer_mapped(request)) |
| 145 | return; |
| 146 | |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 147 | if (request->request.dma == DMA_ADDR_INVALID) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 148 | dev_vdbg(musb->controller, |
| 149 | "not unmapping a never mapped buffer\n"); |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 150 | return; |
| 151 | } |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 152 | if (request->map_state == MUSB_MAPPED) { |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 153 | dma_unmap_single(musb->controller, |
| 154 | request->request.dma, |
| 155 | request->request.length, |
| 156 | request->tx |
| 157 | ? DMA_TO_DEVICE |
| 158 | : DMA_FROM_DEVICE); |
| 159 | request->request.dma = DMA_ADDR_INVALID; |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 160 | } else { /* PRE_MAPPED */ |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 161 | dma_sync_single_for_cpu(musb->controller, |
| 162 | request->request.dma, |
| 163 | request->request.length, |
| 164 | request->tx |
| 165 | ? DMA_TO_DEVICE |
| 166 | : DMA_FROM_DEVICE); |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 167 | } |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 168 | request->map_state = UN_MAPPED; |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 169 | } |
| 170 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 171 | /* |
| 172 | * Immediately complete a request. |
| 173 | * |
| 174 | * @param request the request to complete |
| 175 | * @param status the status to complete the request with |
| 176 | * Context: controller locked, IRQs blocked. |
| 177 | */ |
| 178 | void musb_g_giveback( |
| 179 | struct musb_ep *ep, |
| 180 | struct usb_request *request, |
| 181 | int status) |
| 182 | __releases(ep->musb->lock) |
| 183 | __acquires(ep->musb->lock) |
| 184 | { |
| 185 | struct musb_request *req; |
| 186 | struct musb *musb; |
| 187 | int busy = ep->busy; |
| 188 | |
| 189 | req = to_musb_request(request); |
| 190 | |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 191 | list_del(&req->list); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 192 | if (req->request.status == -EINPROGRESS) |
| 193 | req->request.status = status; |
| 194 | musb = req->musb; |
| 195 | |
| 196 | ep->busy = 1; |
| 197 | spin_unlock(&musb->lock); |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 198 | unmap_dma_buffer(req, musb); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 199 | if (request->status == 0) |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 200 | dev_dbg(musb->controller, "%s done request %p, %d/%d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 201 | ep->end_point.name, request, |
| 202 | req->request.actual, req->request.length); |
| 203 | else |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 204 | dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 205 | ep->end_point.name, request, |
| 206 | req->request.actual, req->request.length, |
| 207 | request->status); |
| 208 | req->request.complete(&req->ep->end_point, &req->request); |
| 209 | spin_lock(&musb->lock); |
| 210 | ep->busy = busy; |
| 211 | } |
| 212 | |
| 213 | /* ----------------------------------------------------------------------- */ |
| 214 | |
| 215 | /* |
| 216 | * Abort requests queued to an endpoint using the status. Synchronous. |
| 217 | * caller locked controller and blocked irqs, and selected this ep. |
| 218 | */ |
| 219 | static void nuke(struct musb_ep *ep, const int status) |
| 220 | { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 221 | struct musb *musb = ep->musb; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 222 | struct musb_request *req = NULL; |
| 223 | void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs; |
| 224 | |
| 225 | ep->busy = 1; |
| 226 | |
| 227 | if (is_dma_capable() && ep->dma) { |
| 228 | struct dma_controller *c = ep->musb->dma_controller; |
| 229 | int value; |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 230 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 231 | if (ep->is_in) { |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 232 | /* |
| 233 | * The programming guide says that we must not clear |
| 234 | * the DMAMODE bit before DMAENAB, so we only |
| 235 | * clear it in the second write... |
| 236 | */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 237 | musb_writew(epio, MUSB_TXCSR, |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 238 | MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 239 | musb_writew(epio, MUSB_TXCSR, |
| 240 | 0 | MUSB_TXCSR_FLUSHFIFO); |
| 241 | } else { |
| 242 | musb_writew(epio, MUSB_RXCSR, |
| 243 | 0 | MUSB_RXCSR_FLUSHFIFO); |
| 244 | musb_writew(epio, MUSB_RXCSR, |
| 245 | 0 | MUSB_RXCSR_FLUSHFIFO); |
| 246 | } |
| 247 | |
| 248 | value = c->channel_abort(ep->dma); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 249 | dev_dbg(musb->controller, "%s: abort DMA --> %d\n", |
| 250 | ep->name, value); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 251 | c->channel_release(ep->dma); |
| 252 | ep->dma = NULL; |
| 253 | } |
| 254 | |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 255 | while (!list_empty(&ep->req_list)) { |
| 256 | req = list_first_entry(&ep->req_list, struct musb_request, list); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 257 | musb_g_giveback(ep, &req->request, status); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* ----------------------------------------------------------------------- */ |
| 262 | |
| 263 | /* Data transfers - pure PIO, pure DMA, or mixed mode */ |
| 264 | |
| 265 | /* |
| 266 | * This assumes the separate CPPI engine is responding to DMA requests |
| 267 | * from the usb core ... sequenced a bit differently from mentor dma. |
| 268 | */ |
| 269 | |
| 270 | static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep) |
| 271 | { |
| 272 | if (can_bulk_split(musb, ep->type)) |
| 273 | return ep->hw_ep->max_packet_sz_tx; |
| 274 | else |
| 275 | return ep->packet_sz; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 280 | |
| 281 | /* Peripheral tx (IN) using Mentor DMA works as follows: |
| 282 | Only mode 0 is used for transfers <= wPktSize, |
| 283 | mode 1 is used for larger transfers, |
| 284 | |
| 285 | One of the following happens: |
| 286 | - Host sends IN token which causes an endpoint interrupt |
| 287 | -> TxAvail |
| 288 | -> if DMA is currently busy, exit. |
| 289 | -> if queue is non-empty, txstate(). |
| 290 | |
| 291 | - Request is queued by the gadget driver. |
| 292 | -> if queue was previously empty, txstate() |
| 293 | |
| 294 | txstate() |
| 295 | -> start |
| 296 | /\ -> setup DMA |
| 297 | | (data is transferred to the FIFO, then sent out when |
| 298 | | IN token(s) are recd from Host. |
| 299 | | -> DMA interrupt on completion |
| 300 | | calls TxAvail. |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 301 | | -> stop DMA, ~DMAENAB, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 302 | | -> set TxPktRdy for last short pkt or zlp |
| 303 | | -> Complete Request |
| 304 | | -> Continue next request (call txstate) |
| 305 | |___________________________________| |
| 306 | |
| 307 | * Non-Mentor DMA engines can of course work differently, such as by |
| 308 | * upleveling from irq-per-packet to irq-per-buffer. |
| 309 | */ |
| 310 | |
| 311 | #endif |
| 312 | |
| 313 | /* |
| 314 | * An endpoint is transmitting data. This can be called either from |
| 315 | * the IRQ routine or from ep.queue() to kickstart a request on an |
| 316 | * endpoint. |
| 317 | * |
| 318 | * Context: controller locked, IRQs blocked, endpoint selected |
| 319 | */ |
| 320 | static void txstate(struct musb *musb, struct musb_request *req) |
| 321 | { |
| 322 | u8 epnum = req->epnum; |
| 323 | struct musb_ep *musb_ep; |
| 324 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 325 | struct usb_request *request; |
| 326 | u16 fifo_count = 0, csr; |
| 327 | int use_dma = 0; |
| 328 | |
| 329 | musb_ep = req->ep; |
| 330 | |
Vikram Pandita | abf710e | 2012-05-18 13:48:04 -0700 | [diff] [blame^] | 331 | /* Check if EP is disabled */ |
| 332 | if (!musb_ep->desc) { |
| 333 | dev_dbg(musb->controller, "ep:%s disabled - ignore request\n", |
| 334 | musb_ep->end_point.name); |
| 335 | return; |
| 336 | } |
| 337 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 338 | /* we shouldn't get here while DMA is active ... but we do ... */ |
| 339 | if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 340 | dev_dbg(musb->controller, "dma pending...\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 341 | return; |
| 342 | } |
| 343 | |
| 344 | /* read TXCSR before */ |
| 345 | csr = musb_readw(epio, MUSB_TXCSR); |
| 346 | |
| 347 | request = &req->request; |
| 348 | fifo_count = min(max_ep_writesize(musb, musb_ep), |
| 349 | (int)(request->length - request->actual)); |
| 350 | |
| 351 | if (csr & MUSB_TXCSR_TXPKTRDY) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 352 | dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 353 | musb_ep->end_point.name, csr); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | if (csr & MUSB_TXCSR_P_SENDSTALL) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 358 | dev_dbg(musb->controller, "%s stalling, txcsr %03x\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 359 | musb_ep->end_point.name, csr); |
| 360 | return; |
| 361 | } |
| 362 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 363 | dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 364 | epnum, musb_ep->packet_sz, fifo_count, |
| 365 | csr); |
| 366 | |
| 367 | #ifndef CONFIG_MUSB_PIO_ONLY |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 368 | if (is_buffer_mapped(req)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 369 | struct dma_controller *c = musb->dma_controller; |
Ming Lei | 66af83d | 2010-09-20 10:32:06 +0300 | [diff] [blame] | 370 | size_t request_size; |
| 371 | |
| 372 | /* setup DMA, then program endpoint CSR */ |
| 373 | request_size = min_t(size_t, request->length - request->actual, |
| 374 | musb_ep->dma->max_len); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 375 | |
| 376 | use_dma = (request->dma != DMA_ADDR_INVALID); |
| 377 | |
| 378 | /* MUSB_TXCSR_P_ISO is still set correctly */ |
| 379 | |
Mian Yousaf Kaukab | a48ff90 | 2011-03-22 15:55:56 +0100 | [diff] [blame] | 380 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 381 | { |
Anand Gadiyar | d1043a2 | 2009-04-02 12:07:08 -0700 | [diff] [blame] | 382 | if (request_size < musb_ep->packet_sz) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 383 | musb_ep->dma->desired_mode = 0; |
| 384 | else |
| 385 | musb_ep->dma->desired_mode = 1; |
| 386 | |
| 387 | use_dma = use_dma && c->channel_program( |
| 388 | musb_ep->dma, musb_ep->packet_sz, |
| 389 | musb_ep->dma->desired_mode, |
Cliff Cai | 796a83f | 2009-12-21 21:18:02 -0500 | [diff] [blame] | 390 | request->dma + request->actual, request_size); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 391 | if (use_dma) { |
| 392 | if (musb_ep->dma->desired_mode == 0) { |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 393 | /* |
| 394 | * We must not clear the DMAMODE bit |
| 395 | * before the DMAENAB bit -- and the |
| 396 | * latter doesn't always get cleared |
| 397 | * before we get here... |
| 398 | */ |
| 399 | csr &= ~(MUSB_TXCSR_AUTOSET |
| 400 | | MUSB_TXCSR_DMAENAB); |
| 401 | musb_writew(epio, MUSB_TXCSR, csr |
| 402 | | MUSB_TXCSR_P_WZC_BITS); |
| 403 | csr &= ~MUSB_TXCSR_DMAMODE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 404 | csr |= (MUSB_TXCSR_DMAENAB | |
| 405 | MUSB_TXCSR_MODE); |
| 406 | /* against programming guide */ |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 407 | } else { |
| 408 | csr |= (MUSB_TXCSR_DMAENAB |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 409 | | MUSB_TXCSR_DMAMODE |
| 410 | | MUSB_TXCSR_MODE); |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 411 | if (!musb_ep->hb_mult) |
| 412 | csr |= MUSB_TXCSR_AUTOSET; |
| 413 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 414 | csr &= ~MUSB_TXCSR_P_UNDERRUN; |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 415 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 416 | musb_writew(epio, MUSB_TXCSR, csr); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | #elif defined(CONFIG_USB_TI_CPPI_DMA) |
| 421 | /* program endpoint CSR first, then setup DMA */ |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 422 | csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); |
Sergei Shtylyov | 37e3ee9 | 2009-03-27 12:53:32 -0700 | [diff] [blame] | 423 | csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE | |
| 424 | MUSB_TXCSR_MODE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 425 | musb_writew(epio, MUSB_TXCSR, |
| 426 | (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN) |
| 427 | | csr); |
| 428 | |
| 429 | /* ensure writebuffer is empty */ |
| 430 | csr = musb_readw(epio, MUSB_TXCSR); |
| 431 | |
| 432 | /* NOTE host side sets DMAENAB later than this; both are |
| 433 | * OK since the transfer dma glue (between CPPI and Mentor |
| 434 | * fifos) just tells CPPI it could start. Data only moves |
| 435 | * to the USB TX fifo when both fifos are ready. |
| 436 | */ |
| 437 | |
| 438 | /* "mode" is irrelevant here; handle terminating ZLPs like |
| 439 | * PIO does, since the hardware RNDIS mode seems unreliable |
| 440 | * except for the last-packet-is-already-short case. |
| 441 | */ |
| 442 | use_dma = use_dma && c->channel_program( |
| 443 | musb_ep->dma, musb_ep->packet_sz, |
| 444 | 0, |
Ming Lei | 66af83d | 2010-09-20 10:32:06 +0300 | [diff] [blame] | 445 | request->dma + request->actual, |
| 446 | request_size); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 447 | if (!use_dma) { |
| 448 | c->channel_release(musb_ep->dma); |
| 449 | musb_ep->dma = NULL; |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 450 | csr &= ~MUSB_TXCSR_DMAENAB; |
| 451 | musb_writew(epio, MUSB_TXCSR, csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 452 | /* invariant: prequest->buf is non-null */ |
| 453 | } |
| 454 | #elif defined(CONFIG_USB_TUSB_OMAP_DMA) |
| 455 | use_dma = use_dma && c->channel_program( |
| 456 | musb_ep->dma, musb_ep->packet_sz, |
| 457 | request->zero, |
Ming Lei | 66af83d | 2010-09-20 10:32:06 +0300 | [diff] [blame] | 458 | request->dma + request->actual, |
| 459 | request_size); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 460 | #endif |
| 461 | } |
| 462 | #endif |
| 463 | |
| 464 | if (!use_dma) { |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 465 | /* |
| 466 | * Unmap the dma buffer back to cpu if dma channel |
| 467 | * programming fails |
| 468 | */ |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 469 | unmap_dma_buffer(req, musb); |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 470 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 471 | musb_write_fifo(musb_ep->hw_ep, fifo_count, |
| 472 | (u8 *) (request->buf + request->actual)); |
| 473 | request->actual += fifo_count; |
| 474 | csr |= MUSB_TXCSR_TXPKTRDY; |
| 475 | csr &= ~MUSB_TXCSR_P_UNDERRUN; |
| 476 | musb_writew(epio, MUSB_TXCSR, csr); |
| 477 | } |
| 478 | |
| 479 | /* host may already have the data when this message shows... */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 480 | dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 481 | musb_ep->end_point.name, use_dma ? "dma" : "pio", |
| 482 | request->actual, request->length, |
| 483 | musb_readw(epio, MUSB_TXCSR), |
| 484 | fifo_count, |
| 485 | musb_readw(epio, MUSB_TXMAXP)); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * FIFO state update (e.g. data ready). |
| 490 | * Called from IRQ, with controller locked. |
| 491 | */ |
| 492 | void musb_g_tx(struct musb *musb, u8 epnum) |
| 493 | { |
| 494 | u16 csr; |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 495 | struct musb_request *req; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 496 | struct usb_request *request; |
| 497 | u8 __iomem *mbase = musb->mregs; |
| 498 | struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in; |
| 499 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 500 | struct dma_channel *dma; |
| 501 | |
| 502 | musb_ep_select(mbase, epnum); |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 503 | req = next_request(musb_ep); |
| 504 | request = &req->request; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 505 | |
| 506 | csr = musb_readw(epio, MUSB_TXCSR); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 507 | dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 508 | |
| 509 | dma = is_dma_capable() ? musb_ep->dma : NULL; |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX |
| 513 | * probably rates reporting as a host error. |
| 514 | */ |
| 515 | if (csr & MUSB_TXCSR_P_SENTSTALL) { |
| 516 | csr |= MUSB_TXCSR_P_WZC_BITS; |
| 517 | csr &= ~MUSB_TXCSR_P_SENTSTALL; |
| 518 | musb_writew(epio, MUSB_TXCSR, csr); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (csr & MUSB_TXCSR_P_UNDERRUN) { |
| 523 | /* We NAKed, no big deal... little reason to care. */ |
| 524 | csr |= MUSB_TXCSR_P_WZC_BITS; |
| 525 | csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); |
| 526 | musb_writew(epio, MUSB_TXCSR, csr); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 527 | dev_vdbg(musb->controller, "underrun on ep%d, req %p\n", |
| 528 | epnum, request); |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 532 | /* |
| 533 | * SHOULD NOT HAPPEN... has with CPPI though, after |
| 534 | * changing SENDSTALL (and other cases); harmless? |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 535 | */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 536 | dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name); |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
| 540 | if (request) { |
| 541 | u8 is_dma = 0; |
| 542 | |
| 543 | if (dma && (csr & MUSB_TXCSR_DMAENAB)) { |
| 544 | is_dma = 1; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 545 | csr |= MUSB_TXCSR_P_WZC_BITS; |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 546 | csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN | |
Mian Yousaf Kaukab | 100d4a9 | 2011-03-15 16:24:24 +0100 | [diff] [blame] | 547 | MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 548 | musb_writew(epio, MUSB_TXCSR, csr); |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 549 | /* Ensure writebuffer is empty. */ |
| 550 | csr = musb_readw(epio, MUSB_TXCSR); |
| 551 | request->actual += musb_ep->dma->actual_len; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 552 | dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n", |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 553 | epnum, csr, musb_ep->dma->actual_len, request); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 554 | } |
| 555 | |
Ming Lei | e7379aa | 2010-09-24 13:44:14 +0300 | [diff] [blame] | 556 | /* |
| 557 | * First, maybe a terminating short packet. Some DMA |
| 558 | * engines might handle this by themselves. |
| 559 | */ |
| 560 | if ((request->zero && request->length |
| 561 | && (request->length % musb_ep->packet_sz == 0) |
| 562 | && (request->actual == request->length)) |
Mian Yousaf Kaukab | a48ff90 | 2011-03-22 15:55:56 +0100 | [diff] [blame] | 563 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) |
Ming Lei | e7379aa | 2010-09-24 13:44:14 +0300 | [diff] [blame] | 564 | || (is_dma && (!dma->desired_mode || |
| 565 | (request->actual & |
| 566 | (musb_ep->packet_sz - 1)))) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 567 | #endif |
Ming Lei | e7379aa | 2010-09-24 13:44:14 +0300 | [diff] [blame] | 568 | ) { |
| 569 | /* |
| 570 | * On DMA completion, FIFO may not be |
| 571 | * available yet... |
| 572 | */ |
| 573 | if (csr & MUSB_TXCSR_TXPKTRDY) |
| 574 | return; |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 575 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 576 | dev_dbg(musb->controller, "sending zero pkt\n"); |
Ming Lei | e7379aa | 2010-09-24 13:44:14 +0300 | [diff] [blame] | 577 | musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE |
| 578 | | MUSB_TXCSR_TXPKTRDY); |
| 579 | request->zero = 0; |
| 580 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 581 | |
Ming Lei | e7379aa | 2010-09-24 13:44:14 +0300 | [diff] [blame] | 582 | if (request->actual == request->length) { |
| 583 | musb_g_giveback(musb_ep, request, 0); |
Supriya Karanth | 3928707 | 2012-02-17 14:54:52 +0530 | [diff] [blame] | 584 | /* |
| 585 | * In the giveback function the MUSB lock is |
| 586 | * released and acquired after sometime. During |
| 587 | * this time period the INDEX register could get |
| 588 | * changed by the gadget_queue function especially |
| 589 | * on SMP systems. Reselect the INDEX to be sure |
| 590 | * we are reading/modifying the right registers |
| 591 | */ |
| 592 | musb_ep_select(mbase, epnum); |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 593 | req = musb_ep->desc ? next_request(musb_ep) : NULL; |
| 594 | if (!req) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 595 | dev_dbg(musb->controller, "%s idle now\n", |
Ming Lei | e7379aa | 2010-09-24 13:44:14 +0300 | [diff] [blame] | 596 | musb_ep->end_point.name); |
| 597 | return; |
Sergei Shtylyov | 95962a7 | 2009-12-16 20:38:31 +0300 | [diff] [blame] | 598 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 599 | } |
| 600 | |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 601 | txstate(musb, req); |
Sergei Shtylyov | 7723de7 | 2009-11-18 22:55:28 +0300 | [diff] [blame] | 602 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* ------------------------------------------------------------ */ |
| 606 | |
| 607 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 608 | |
| 609 | /* Peripheral rx (OUT) using Mentor DMA works as follows: |
| 610 | - Only mode 0 is used. |
| 611 | |
| 612 | - Request is queued by the gadget class driver. |
| 613 | -> if queue was previously empty, rxstate() |
| 614 | |
| 615 | - Host sends OUT token which causes an endpoint interrupt |
| 616 | /\ -> RxReady |
| 617 | | -> if request queued, call rxstate |
| 618 | | /\ -> setup DMA |
| 619 | | | -> DMA interrupt on completion |
| 620 | | | -> RxReady |
| 621 | | | -> stop DMA |
| 622 | | | -> ack the read |
| 623 | | | -> if data recd = max expected |
| 624 | | | by the request, or host |
| 625 | | | sent a short packet, |
| 626 | | | complete the request, |
| 627 | | | and start the next one. |
| 628 | | |_____________________________________| |
| 629 | | else just wait for the host |
| 630 | | to send the next OUT token. |
| 631 | |__________________________________________________| |
| 632 | |
| 633 | * Non-Mentor DMA engines can of course work differently. |
| 634 | */ |
| 635 | |
| 636 | #endif |
| 637 | |
| 638 | /* |
| 639 | * Context: controller locked, IRQs blocked, endpoint selected |
| 640 | */ |
| 641 | static void rxstate(struct musb *musb, struct musb_request *req) |
| 642 | { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 643 | const u8 epnum = req->epnum; |
| 644 | struct usb_request *request = &req->request; |
Ming Lei | bd2e74d | 2010-09-20 10:32:01 +0300 | [diff] [blame] | 645 | struct musb_ep *musb_ep; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 646 | void __iomem *epio = musb->endpoints[epnum].regs; |
Felipe Balbi | c2c9632 | 2009-02-21 15:29:42 -0800 | [diff] [blame] | 647 | unsigned fifo_count = 0; |
Ming Lei | bd2e74d | 2010-09-20 10:32:01 +0300 | [diff] [blame] | 648 | u16 len; |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 649 | u16 csr = musb_readw(epio, MUSB_RXCSR); |
Ming Lei | bd2e74d | 2010-09-20 10:32:01 +0300 | [diff] [blame] | 650 | struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; |
Anand Gadiyar | 0ae52d5 | 2011-07-19 22:11:58 -0700 | [diff] [blame] | 651 | u8 use_mode_1; |
Ming Lei | bd2e74d | 2010-09-20 10:32:01 +0300 | [diff] [blame] | 652 | |
| 653 | if (hw_ep->is_shared_fifo) |
| 654 | musb_ep = &hw_ep->ep_in; |
| 655 | else |
| 656 | musb_ep = &hw_ep->ep_out; |
| 657 | |
| 658 | len = musb_ep->packet_sz; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 659 | |
Vikram Pandita | abf710e | 2012-05-18 13:48:04 -0700 | [diff] [blame^] | 660 | /* Check if EP is disabled */ |
| 661 | if (!musb_ep->desc) { |
| 662 | dev_dbg(musb->controller, "ep:%s disabled - ignore request\n", |
| 663 | musb_ep->end_point.name); |
| 664 | return; |
| 665 | } |
| 666 | |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 667 | /* We shouldn't get here while DMA is active, but we do... */ |
| 668 | if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 669 | dev_dbg(musb->controller, "DMA pending...\n"); |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 670 | return; |
| 671 | } |
| 672 | |
| 673 | if (csr & MUSB_RXCSR_P_SENDSTALL) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 674 | dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n", |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 675 | musb_ep->end_point.name, csr); |
| 676 | return; |
| 677 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 678 | |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 679 | if (is_cppi_enabled() && is_buffer_mapped(req)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 680 | struct dma_controller *c = musb->dma_controller; |
| 681 | struct dma_channel *channel = musb_ep->dma; |
| 682 | |
| 683 | /* NOTE: CPPI won't actually stop advancing the DMA |
| 684 | * queue after short packet transfers, so this is almost |
| 685 | * always going to run as IRQ-per-packet DMA so that |
| 686 | * faults will be handled correctly. |
| 687 | */ |
| 688 | if (c->channel_program(channel, |
| 689 | musb_ep->packet_sz, |
| 690 | !request->short_not_ok, |
| 691 | request->dma + request->actual, |
| 692 | request->length - request->actual)) { |
| 693 | |
| 694 | /* make sure that if an rxpkt arrived after the irq, |
| 695 | * the cppi engine will be ready to take it as soon |
| 696 | * as DMA is enabled |
| 697 | */ |
| 698 | csr &= ~(MUSB_RXCSR_AUTOCLEAR |
| 699 | | MUSB_RXCSR_DMAMODE); |
| 700 | csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS; |
| 701 | musb_writew(epio, MUSB_RXCSR, csr); |
| 702 | return; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (csr & MUSB_RXCSR_RXPKTRDY) { |
| 707 | len = musb_readw(epio, MUSB_RXCOUNT); |
Anand Gadiyar | 0ae52d5 | 2011-07-19 22:11:58 -0700 | [diff] [blame] | 708 | |
| 709 | /* |
| 710 | * Enable Mode 1 on RX transfers only when short_not_ok flag |
| 711 | * is set. Currently short_not_ok flag is set only from |
| 712 | * file_storage and f_mass_storage drivers |
| 713 | */ |
| 714 | |
| 715 | if (request->short_not_ok && len == musb_ep->packet_sz) |
| 716 | use_mode_1 = 1; |
| 717 | else |
| 718 | use_mode_1 = 0; |
| 719 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 720 | if (request->actual < request->length) { |
| 721 | #ifdef CONFIG_USB_INVENTRA_DMA |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 722 | if (is_buffer_mapped(req)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 723 | struct dma_controller *c; |
| 724 | struct dma_channel *channel; |
| 725 | int use_dma = 0; |
| 726 | |
| 727 | c = musb->dma_controller; |
| 728 | channel = musb_ep->dma; |
| 729 | |
| 730 | /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in |
| 731 | * mode 0 only. So we do not get endpoint interrupts due to DMA |
| 732 | * completion. We only get interrupts from DMA controller. |
| 733 | * |
| 734 | * We could operate in DMA mode 1 if we knew the size of the tranfer |
| 735 | * in advance. For mass storage class, request->length = what the host |
| 736 | * sends, so that'd work. But for pretty much everything else, |
| 737 | * request->length is routinely more than what the host sends. For |
| 738 | * most these gadgets, end of is signified either by a short packet, |
| 739 | * or filling the last byte of the buffer. (Sending extra data in |
| 740 | * that last pckate should trigger an overflow fault.) But in mode 1, |
Vitaliy Ivanov | fb914eb | 2011-06-23 20:01:55 +0300 | [diff] [blame] | 741 | * we don't get DMA completion interrupt for short packets. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 742 | * |
| 743 | * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1), |
| 744 | * to get endpoint interrupt on every DMA req, but that didn't seem |
| 745 | * to work reliably. |
| 746 | * |
| 747 | * REVISIT an updated g_file_storage can set req->short_not_ok, which |
| 748 | * then becomes usable as a runtime "use mode 1" hint... |
| 749 | */ |
| 750 | |
Anand Gadiyar | 0ae52d5 | 2011-07-19 22:11:58 -0700 | [diff] [blame] | 751 | /* Experimental: Mode1 works with mass storage use cases */ |
| 752 | if (use_mode_1) { |
Ming Lei | 9001d80 | 2010-09-25 05:50:43 -0500 | [diff] [blame] | 753 | csr |= MUSB_RXCSR_AUTOCLEAR; |
Anand Gadiyar | 0ae52d5 | 2011-07-19 22:11:58 -0700 | [diff] [blame] | 754 | musb_writew(epio, MUSB_RXCSR, csr); |
| 755 | csr |= MUSB_RXCSR_DMAENAB; |
| 756 | musb_writew(epio, MUSB_RXCSR, csr); |
| 757 | |
| 758 | /* |
| 759 | * this special sequence (enabling and then |
| 760 | * disabling MUSB_RXCSR_DMAMODE) is required |
| 761 | * to get DMAReq to activate |
| 762 | */ |
| 763 | musb_writew(epio, MUSB_RXCSR, |
| 764 | csr | MUSB_RXCSR_DMAMODE); |
| 765 | musb_writew(epio, MUSB_RXCSR, csr); |
| 766 | |
| 767 | } else { |
| 768 | if (!musb_ep->hb_mult && |
| 769 | musb_ep->hw_ep->rx_double_buffered) |
| 770 | csr |= MUSB_RXCSR_AUTOCLEAR; |
| 771 | csr |= MUSB_RXCSR_DMAENAB; |
| 772 | musb_writew(epio, MUSB_RXCSR, csr); |
| 773 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 774 | |
| 775 | if (request->actual < request->length) { |
| 776 | int transfer_size = 0; |
Anand Gadiyar | 0ae52d5 | 2011-07-19 22:11:58 -0700 | [diff] [blame] | 777 | if (use_mode_1) { |
| 778 | transfer_size = min(request->length - request->actual, |
| 779 | channel->max_len); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 780 | musb_ep->dma->desired_mode = 1; |
Anand Gadiyar | 0ae52d5 | 2011-07-19 22:11:58 -0700 | [diff] [blame] | 781 | } else { |
| 782 | transfer_size = min(request->length - request->actual, |
| 783 | (unsigned)len); |
| 784 | musb_ep->dma->desired_mode = 0; |
| 785 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 786 | |
| 787 | use_dma = c->channel_program( |
| 788 | channel, |
| 789 | musb_ep->packet_sz, |
| 790 | channel->desired_mode, |
| 791 | request->dma |
| 792 | + request->actual, |
| 793 | transfer_size); |
| 794 | } |
| 795 | |
| 796 | if (use_dma) |
| 797 | return; |
| 798 | } |
Mian Yousaf Kaukab | a48ff90 | 2011-03-22 15:55:56 +0100 | [diff] [blame] | 799 | #elif defined(CONFIG_USB_UX500_DMA) |
| 800 | if ((is_buffer_mapped(req)) && |
| 801 | (request->actual < request->length)) { |
| 802 | |
| 803 | struct dma_controller *c; |
| 804 | struct dma_channel *channel; |
| 805 | int transfer_size = 0; |
| 806 | |
| 807 | c = musb->dma_controller; |
| 808 | channel = musb_ep->dma; |
| 809 | |
| 810 | /* In case first packet is short */ |
| 811 | if (len < musb_ep->packet_sz) |
| 812 | transfer_size = len; |
| 813 | else if (request->short_not_ok) |
| 814 | transfer_size = min(request->length - |
| 815 | request->actual, |
| 816 | channel->max_len); |
| 817 | else |
| 818 | transfer_size = min(request->length - |
| 819 | request->actual, |
| 820 | (unsigned)len); |
| 821 | |
| 822 | csr &= ~MUSB_RXCSR_DMAMODE; |
| 823 | csr |= (MUSB_RXCSR_DMAENAB | |
| 824 | MUSB_RXCSR_AUTOCLEAR); |
| 825 | |
| 826 | musb_writew(epio, MUSB_RXCSR, csr); |
| 827 | |
| 828 | if (transfer_size <= musb_ep->packet_sz) { |
| 829 | musb_ep->dma->desired_mode = 0; |
| 830 | } else { |
| 831 | musb_ep->dma->desired_mode = 1; |
| 832 | /* Mode must be set after DMAENAB */ |
| 833 | csr |= MUSB_RXCSR_DMAMODE; |
| 834 | musb_writew(epio, MUSB_RXCSR, csr); |
| 835 | } |
| 836 | |
| 837 | if (c->channel_program(channel, |
| 838 | musb_ep->packet_sz, |
| 839 | channel->desired_mode, |
| 840 | request->dma |
| 841 | + request->actual, |
| 842 | transfer_size)) |
| 843 | |
| 844 | return; |
| 845 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 846 | #endif /* Mentor's DMA */ |
| 847 | |
| 848 | fifo_count = request->length - request->actual; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 849 | dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 850 | musb_ep->end_point.name, |
| 851 | len, fifo_count, |
| 852 | musb_ep->packet_sz); |
| 853 | |
Felipe Balbi | c2c9632 | 2009-02-21 15:29:42 -0800 | [diff] [blame] | 854 | fifo_count = min_t(unsigned, len, fifo_count); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 855 | |
| 856 | #ifdef CONFIG_USB_TUSB_OMAP_DMA |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 857 | if (tusb_dma_omap() && is_buffer_mapped(req)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 858 | struct dma_controller *c = musb->dma_controller; |
| 859 | struct dma_channel *channel = musb_ep->dma; |
| 860 | u32 dma_addr = request->dma + request->actual; |
| 861 | int ret; |
| 862 | |
| 863 | ret = c->channel_program(channel, |
| 864 | musb_ep->packet_sz, |
| 865 | channel->desired_mode, |
| 866 | dma_addr, |
| 867 | fifo_count); |
| 868 | if (ret) |
| 869 | return; |
| 870 | } |
| 871 | #endif |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 872 | /* |
| 873 | * Unmap the dma buffer back to cpu if dma channel |
| 874 | * programming fails. This buffer is mapped if the |
| 875 | * channel allocation is successful |
| 876 | */ |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 877 | if (is_buffer_mapped(req)) { |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 878 | unmap_dma_buffer(req, musb); |
| 879 | |
Ming Lei | e75df37 | 2010-11-16 23:37:37 +0800 | [diff] [blame] | 880 | /* |
| 881 | * Clear DMAENAB and AUTOCLEAR for the |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 882 | * PIO mode transfer |
| 883 | */ |
Ming Lei | e75df37 | 2010-11-16 23:37:37 +0800 | [diff] [blame] | 884 | csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR); |
Hema Kalliguddi | 92d2711 | 2010-11-15 04:24:01 -0600 | [diff] [blame] | 885 | musb_writew(epio, MUSB_RXCSR, csr); |
| 886 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 887 | |
| 888 | musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *) |
| 889 | (request->buf + request->actual)); |
| 890 | request->actual += fifo_count; |
| 891 | |
| 892 | /* REVISIT if we left anything in the fifo, flush |
| 893 | * it and report -EOVERFLOW |
| 894 | */ |
| 895 | |
| 896 | /* ack the read! */ |
| 897 | csr |= MUSB_RXCSR_P_WZC_BITS; |
| 898 | csr &= ~MUSB_RXCSR_RXPKTRDY; |
| 899 | musb_writew(epio, MUSB_RXCSR, csr); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /* reach the end or short packet detected */ |
| 904 | if (request->actual == request->length || len < musb_ep->packet_sz) |
| 905 | musb_g_giveback(musb_ep, request, 0); |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Data ready for a request; called from IRQ |
| 910 | */ |
| 911 | void musb_g_rx(struct musb *musb, u8 epnum) |
| 912 | { |
| 913 | u16 csr; |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 914 | struct musb_request *req; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 915 | struct usb_request *request; |
| 916 | void __iomem *mbase = musb->mregs; |
Ming Lei | bd2e74d | 2010-09-20 10:32:01 +0300 | [diff] [blame] | 917 | struct musb_ep *musb_ep; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 918 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 919 | struct dma_channel *dma; |
Ming Lei | bd2e74d | 2010-09-20 10:32:01 +0300 | [diff] [blame] | 920 | struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; |
| 921 | |
| 922 | if (hw_ep->is_shared_fifo) |
| 923 | musb_ep = &hw_ep->ep_in; |
| 924 | else |
| 925 | musb_ep = &hw_ep->ep_out; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 926 | |
| 927 | musb_ep_select(mbase, epnum); |
| 928 | |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 929 | req = next_request(musb_ep); |
| 930 | if (!req) |
Maulik Mankad | 0abdc36 | 2009-12-22 16:18:19 +0530 | [diff] [blame] | 931 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 932 | |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 933 | request = &req->request; |
| 934 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 935 | csr = musb_readw(epio, MUSB_RXCSR); |
| 936 | dma = is_dma_capable() ? musb_ep->dma : NULL; |
| 937 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 938 | dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 939 | csr, dma ? " (dma)" : "", request); |
| 940 | |
| 941 | if (csr & MUSB_RXCSR_P_SENTSTALL) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 942 | csr |= MUSB_RXCSR_P_WZC_BITS; |
| 943 | csr &= ~MUSB_RXCSR_P_SENTSTALL; |
| 944 | musb_writew(epio, MUSB_RXCSR, csr); |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 945 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | if (csr & MUSB_RXCSR_P_OVERRUN) { |
| 949 | /* csr |= MUSB_RXCSR_P_WZC_BITS; */ |
| 950 | csr &= ~MUSB_RXCSR_P_OVERRUN; |
| 951 | musb_writew(epio, MUSB_RXCSR, csr); |
| 952 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 953 | dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request); |
Sergei Shtylyov | 4346786 | 2010-09-24 13:44:12 +0300 | [diff] [blame] | 954 | if (request->status == -EINPROGRESS) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 955 | request->status = -EOVERFLOW; |
| 956 | } |
| 957 | if (csr & MUSB_RXCSR_INCOMPRX) { |
| 958 | /* REVISIT not necessarily an error */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 959 | dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 963 | /* "should not happen"; likely RXPKTRDY pending for DMA */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 964 | dev_dbg(musb->controller, "%s busy, csr %04x\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 965 | musb_ep->end_point.name, csr); |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 966 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | if (dma && (csr & MUSB_RXCSR_DMAENAB)) { |
| 970 | csr &= ~(MUSB_RXCSR_AUTOCLEAR |
| 971 | | MUSB_RXCSR_DMAENAB |
| 972 | | MUSB_RXCSR_DMAMODE); |
| 973 | musb_writew(epio, MUSB_RXCSR, |
| 974 | MUSB_RXCSR_P_WZC_BITS | csr); |
| 975 | |
| 976 | request->actual += musb_ep->dma->actual_len; |
| 977 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 978 | dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 979 | epnum, csr, |
| 980 | musb_readw(epio, MUSB_RXCSR), |
| 981 | musb_ep->dma->actual_len, request); |
| 982 | |
Mian Yousaf Kaukab | a48ff90 | 2011-03-22 15:55:56 +0100 | [diff] [blame] | 983 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ |
| 984 | defined(CONFIG_USB_UX500_DMA) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 985 | /* Autoclear doesn't clear RxPktRdy for short packets */ |
Ming Lei | 9001d80 | 2010-09-25 05:50:43 -0500 | [diff] [blame] | 986 | if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 987 | || (dma->actual_len |
| 988 | & (musb_ep->packet_sz - 1))) { |
| 989 | /* ack the read! */ |
| 990 | csr &= ~MUSB_RXCSR_RXPKTRDY; |
| 991 | musb_writew(epio, MUSB_RXCSR, csr); |
| 992 | } |
| 993 | |
| 994 | /* incomplete, and not short? wait for next IN packet */ |
| 995 | if ((request->actual < request->length) |
| 996 | && (musb_ep->dma->actual_len |
Ming Lei | 9001d80 | 2010-09-25 05:50:43 -0500 | [diff] [blame] | 997 | == musb_ep->packet_sz)) { |
| 998 | /* In double buffer case, continue to unload fifo if |
| 999 | * there is Rx packet in FIFO. |
| 1000 | **/ |
| 1001 | csr = musb_readw(epio, MUSB_RXCSR); |
| 1002 | if ((csr & MUSB_RXCSR_RXPKTRDY) && |
| 1003 | hw_ep->rx_double_buffered) |
| 1004 | goto exit; |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1005 | return; |
Ming Lei | 9001d80 | 2010-09-25 05:50:43 -0500 | [diff] [blame] | 1006 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1007 | #endif |
| 1008 | musb_g_giveback(musb_ep, request, 0); |
Supriya Karanth | 3928707 | 2012-02-17 14:54:52 +0530 | [diff] [blame] | 1009 | /* |
| 1010 | * In the giveback function the MUSB lock is |
| 1011 | * released and acquired after sometime. During |
| 1012 | * this time period the INDEX register could get |
| 1013 | * changed by the gadget_queue function especially |
| 1014 | * on SMP systems. Reselect the INDEX to be sure |
| 1015 | * we are reading/modifying the right registers |
| 1016 | */ |
| 1017 | musb_ep_select(mbase, epnum); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1018 | |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 1019 | req = next_request(musb_ep); |
| 1020 | if (!req) |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1021 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1022 | } |
Mian Yousaf Kaukab | a48ff90 | 2011-03-22 15:55:56 +0100 | [diff] [blame] | 1023 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ |
| 1024 | defined(CONFIG_USB_UX500_DMA) |
Ming Lei | 9001d80 | 2010-09-25 05:50:43 -0500 | [diff] [blame] | 1025 | exit: |
Ajay Kumar Gupta | bb324b0 | 2010-11-22 14:22:41 +0530 | [diff] [blame] | 1026 | #endif |
Sergei Shtylyov | 4346786 | 2010-09-24 13:44:12 +0300 | [diff] [blame] | 1027 | /* Analyze request */ |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 1028 | rxstate(musb, req); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* ------------------------------------------------------------ */ |
| 1032 | |
| 1033 | static int musb_gadget_enable(struct usb_ep *ep, |
| 1034 | const struct usb_endpoint_descriptor *desc) |
| 1035 | { |
| 1036 | unsigned long flags; |
| 1037 | struct musb_ep *musb_ep; |
| 1038 | struct musb_hw_ep *hw_ep; |
| 1039 | void __iomem *regs; |
| 1040 | struct musb *musb; |
| 1041 | void __iomem *mbase; |
| 1042 | u8 epnum; |
| 1043 | u16 csr; |
| 1044 | unsigned tmp; |
| 1045 | int status = -EINVAL; |
| 1046 | |
| 1047 | if (!ep || !desc) |
| 1048 | return -EINVAL; |
| 1049 | |
| 1050 | musb_ep = to_musb_ep(ep); |
| 1051 | hw_ep = musb_ep->hw_ep; |
| 1052 | regs = hw_ep->regs; |
| 1053 | musb = musb_ep->musb; |
| 1054 | mbase = musb->mregs; |
| 1055 | epnum = musb_ep->current_epnum; |
| 1056 | |
| 1057 | spin_lock_irqsave(&musb->lock, flags); |
| 1058 | |
| 1059 | if (musb_ep->desc) { |
| 1060 | status = -EBUSY; |
| 1061 | goto fail; |
| 1062 | } |
Julia Lawall | 96bcd09 | 2009-01-24 17:57:24 -0800 | [diff] [blame] | 1063 | musb_ep->type = usb_endpoint_type(desc); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1064 | |
| 1065 | /* check direction and (later) maxpacket size against endpoint */ |
Julia Lawall | 96bcd09 | 2009-01-24 17:57:24 -0800 | [diff] [blame] | 1066 | if (usb_endpoint_num(desc) != epnum) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1067 | goto fail; |
| 1068 | |
| 1069 | /* REVISIT this rules out high bandwidth periodic transfers */ |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 1070 | tmp = usb_endpoint_maxp(desc); |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 1071 | if (tmp & ~0x07ff) { |
| 1072 | int ok; |
| 1073 | |
| 1074 | if (usb_endpoint_dir_in(desc)) |
| 1075 | ok = musb->hb_iso_tx; |
| 1076 | else |
| 1077 | ok = musb->hb_iso_rx; |
| 1078 | |
| 1079 | if (!ok) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1080 | dev_dbg(musb->controller, "no support for high bandwidth ISO\n"); |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 1081 | goto fail; |
| 1082 | } |
| 1083 | musb_ep->hb_mult = (tmp >> 11) & 3; |
| 1084 | } else { |
| 1085 | musb_ep->hb_mult = 0; |
| 1086 | } |
| 1087 | |
| 1088 | musb_ep->packet_sz = tmp & 0x7ff; |
| 1089 | tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1090 | |
| 1091 | /* enable the interrupts for the endpoint, set the endpoint |
| 1092 | * packet size (or fail), set the mode, clear the fifo |
| 1093 | */ |
| 1094 | musb_ep_select(mbase, epnum); |
Julia Lawall | 96bcd09 | 2009-01-24 17:57:24 -0800 | [diff] [blame] | 1095 | if (usb_endpoint_dir_in(desc)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1096 | u16 int_txe = musb_readw(mbase, MUSB_INTRTXE); |
| 1097 | |
| 1098 | if (hw_ep->is_shared_fifo) |
| 1099 | musb_ep->is_in = 1; |
| 1100 | if (!musb_ep->is_in) |
| 1101 | goto fail; |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 1102 | |
| 1103 | if (tmp > hw_ep->max_packet_sz_tx) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1104 | dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1105 | goto fail; |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 1106 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1107 | |
| 1108 | int_txe |= (1 << epnum); |
| 1109 | musb_writew(mbase, MUSB_INTRTXE, int_txe); |
| 1110 | |
| 1111 | /* REVISIT if can_bulk_split(), use by updating "tmp"; |
| 1112 | * likewise high bandwidth periodic tx |
| 1113 | */ |
Cliff Cai | 9f445cb | 2010-01-28 20:44:18 -0500 | [diff] [blame] | 1114 | /* Set TXMAXP with the FIFO size of the endpoint |
Ming Lei | 31c9909 | 2010-10-19 19:08:25 -0500 | [diff] [blame] | 1115 | * to disable double buffering mode. |
Cliff Cai | 9f445cb | 2010-01-28 20:44:18 -0500 | [diff] [blame] | 1116 | */ |
Felipe Balbi | 0662481 | 2011-01-21 13:39:20 +0800 | [diff] [blame] | 1117 | if (musb->double_buffer_not_ok) |
| 1118 | musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx); |
| 1119 | else |
| 1120 | musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz |
| 1121 | | (musb_ep->hb_mult << 11)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1122 | |
| 1123 | csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG; |
| 1124 | if (musb_readw(regs, MUSB_TXCSR) |
| 1125 | & MUSB_TXCSR_FIFONOTEMPTY) |
| 1126 | csr |= MUSB_TXCSR_FLUSHFIFO; |
| 1127 | if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) |
| 1128 | csr |= MUSB_TXCSR_P_ISO; |
| 1129 | |
| 1130 | /* set twice in case of double buffering */ |
| 1131 | musb_writew(regs, MUSB_TXCSR, csr); |
| 1132 | /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ |
| 1133 | musb_writew(regs, MUSB_TXCSR, csr); |
| 1134 | |
| 1135 | } else { |
| 1136 | u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE); |
| 1137 | |
| 1138 | if (hw_ep->is_shared_fifo) |
| 1139 | musb_ep->is_in = 0; |
| 1140 | if (musb_ep->is_in) |
| 1141 | goto fail; |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 1142 | |
| 1143 | if (tmp > hw_ep->max_packet_sz_rx) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1144 | dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1145 | goto fail; |
Ming Lei | f11d893 | 2010-09-24 13:44:04 +0300 | [diff] [blame] | 1146 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1147 | |
| 1148 | int_rxe |= (1 << epnum); |
| 1149 | musb_writew(mbase, MUSB_INTRRXE, int_rxe); |
| 1150 | |
| 1151 | /* REVISIT if can_bulk_combine() use by updating "tmp" |
| 1152 | * likewise high bandwidth periodic rx |
| 1153 | */ |
Cliff Cai | 9f445cb | 2010-01-28 20:44:18 -0500 | [diff] [blame] | 1154 | /* Set RXMAXP with the FIFO size of the endpoint |
| 1155 | * to disable double buffering mode. |
| 1156 | */ |
Felipe Balbi | 0662481 | 2011-01-21 13:39:20 +0800 | [diff] [blame] | 1157 | if (musb->double_buffer_not_ok) |
| 1158 | musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx); |
| 1159 | else |
| 1160 | musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz |
| 1161 | | (musb_ep->hb_mult << 11)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1162 | |
| 1163 | /* force shared fifo to OUT-only mode */ |
| 1164 | if (hw_ep->is_shared_fifo) { |
| 1165 | csr = musb_readw(regs, MUSB_TXCSR); |
| 1166 | csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY); |
| 1167 | musb_writew(regs, MUSB_TXCSR, csr); |
| 1168 | } |
| 1169 | |
| 1170 | csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG; |
| 1171 | if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) |
| 1172 | csr |= MUSB_RXCSR_P_ISO; |
| 1173 | else if (musb_ep->type == USB_ENDPOINT_XFER_INT) |
| 1174 | csr |= MUSB_RXCSR_DISNYET; |
| 1175 | |
| 1176 | /* set twice in case of double buffering */ |
| 1177 | musb_writew(regs, MUSB_RXCSR, csr); |
| 1178 | musb_writew(regs, MUSB_RXCSR, csr); |
| 1179 | } |
| 1180 | |
| 1181 | /* NOTE: all the I/O code _should_ work fine without DMA, in case |
| 1182 | * for some reason you run out of channels here. |
| 1183 | */ |
| 1184 | if (is_dma_capable() && musb->dma_controller) { |
| 1185 | struct dma_controller *c = musb->dma_controller; |
| 1186 | |
| 1187 | musb_ep->dma = c->channel_alloc(c, hw_ep, |
| 1188 | (desc->bEndpointAddress & USB_DIR_IN)); |
| 1189 | } else |
| 1190 | musb_ep->dma = NULL; |
| 1191 | |
| 1192 | musb_ep->desc = desc; |
| 1193 | musb_ep->busy = 0; |
Sergei Shtylyov | 47e9760 | 2009-11-18 22:51:51 +0300 | [diff] [blame] | 1194 | musb_ep->wedged = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1195 | status = 0; |
| 1196 | |
| 1197 | pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n", |
| 1198 | musb_driver_name, musb_ep->end_point.name, |
| 1199 | ({ char *s; switch (musb_ep->type) { |
| 1200 | case USB_ENDPOINT_XFER_BULK: s = "bulk"; break; |
| 1201 | case USB_ENDPOINT_XFER_INT: s = "int"; break; |
| 1202 | default: s = "iso"; break; |
| 1203 | }; s; }), |
| 1204 | musb_ep->is_in ? "IN" : "OUT", |
| 1205 | musb_ep->dma ? "dma, " : "", |
| 1206 | musb_ep->packet_sz); |
| 1207 | |
| 1208 | schedule_work(&musb->irq_work); |
| 1209 | |
| 1210 | fail: |
| 1211 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1212 | return status; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Disable an endpoint flushing all requests queued. |
| 1217 | */ |
| 1218 | static int musb_gadget_disable(struct usb_ep *ep) |
| 1219 | { |
| 1220 | unsigned long flags; |
| 1221 | struct musb *musb; |
| 1222 | u8 epnum; |
| 1223 | struct musb_ep *musb_ep; |
| 1224 | void __iomem *epio; |
| 1225 | int status = 0; |
| 1226 | |
| 1227 | musb_ep = to_musb_ep(ep); |
| 1228 | musb = musb_ep->musb; |
| 1229 | epnum = musb_ep->current_epnum; |
| 1230 | epio = musb->endpoints[epnum].regs; |
| 1231 | |
| 1232 | spin_lock_irqsave(&musb->lock, flags); |
| 1233 | musb_ep_select(musb->mregs, epnum); |
| 1234 | |
| 1235 | /* zero the endpoint sizes */ |
| 1236 | if (musb_ep->is_in) { |
| 1237 | u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE); |
| 1238 | int_txe &= ~(1 << epnum); |
| 1239 | musb_writew(musb->mregs, MUSB_INTRTXE, int_txe); |
| 1240 | musb_writew(epio, MUSB_TXMAXP, 0); |
| 1241 | } else { |
| 1242 | u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE); |
| 1243 | int_rxe &= ~(1 << epnum); |
| 1244 | musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe); |
| 1245 | musb_writew(epio, MUSB_RXMAXP, 0); |
| 1246 | } |
| 1247 | |
| 1248 | musb_ep->desc = NULL; |
| 1249 | |
| 1250 | /* abort all pending DMA and requests */ |
| 1251 | nuke(musb_ep, -ESHUTDOWN); |
| 1252 | |
| 1253 | schedule_work(&musb->irq_work); |
| 1254 | |
| 1255 | spin_unlock_irqrestore(&(musb->lock), flags); |
| 1256 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1257 | dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1258 | |
| 1259 | return status; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * Allocate a request for an endpoint. |
| 1264 | * Reused by ep0 code. |
| 1265 | */ |
| 1266 | struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) |
| 1267 | { |
| 1268 | struct musb_ep *musb_ep = to_musb_ep(ep); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1269 | struct musb *musb = musb_ep->musb; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1270 | struct musb_request *request = NULL; |
| 1271 | |
| 1272 | request = kzalloc(sizeof *request, gfp_flags); |
Felipe Balbi | 0607f86 | 2010-12-01 11:03:54 +0200 | [diff] [blame] | 1273 | if (!request) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1274 | dev_dbg(musb->controller, "not enough memory\n"); |
Felipe Balbi | 0607f86 | 2010-12-01 11:03:54 +0200 | [diff] [blame] | 1275 | return NULL; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1276 | } |
| 1277 | |
Felipe Balbi | 0607f86 | 2010-12-01 11:03:54 +0200 | [diff] [blame] | 1278 | request->request.dma = DMA_ADDR_INVALID; |
| 1279 | request->epnum = musb_ep->current_epnum; |
| 1280 | request->ep = musb_ep; |
| 1281 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1282 | return &request->request; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Free a request |
| 1287 | * Reused by ep0 code. |
| 1288 | */ |
| 1289 | void musb_free_request(struct usb_ep *ep, struct usb_request *req) |
| 1290 | { |
| 1291 | kfree(to_musb_request(req)); |
| 1292 | } |
| 1293 | |
| 1294 | static LIST_HEAD(buffers); |
| 1295 | |
| 1296 | struct free_record { |
| 1297 | struct list_head list; |
| 1298 | struct device *dev; |
| 1299 | unsigned bytes; |
| 1300 | dma_addr_t dma; |
| 1301 | }; |
| 1302 | |
| 1303 | /* |
| 1304 | * Context: controller locked, IRQs blocked. |
| 1305 | */ |
Sergei Shtylyov | a666e3e | 2010-09-11 13:23:12 -0500 | [diff] [blame] | 1306 | void musb_ep_restart(struct musb *musb, struct musb_request *req) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1307 | { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1308 | dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1309 | req->tx ? "TX/IN" : "RX/OUT", |
| 1310 | &req->request, req->request.length, req->epnum); |
| 1311 | |
| 1312 | musb_ep_select(musb->mregs, req->epnum); |
| 1313 | if (req->tx) |
| 1314 | txstate(musb, req); |
| 1315 | else |
| 1316 | rxstate(musb, req); |
| 1317 | } |
| 1318 | |
| 1319 | static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, |
| 1320 | gfp_t gfp_flags) |
| 1321 | { |
| 1322 | struct musb_ep *musb_ep; |
| 1323 | struct musb_request *request; |
| 1324 | struct musb *musb; |
| 1325 | int status = 0; |
| 1326 | unsigned long lockflags; |
| 1327 | |
| 1328 | if (!ep || !req) |
| 1329 | return -EINVAL; |
| 1330 | if (!req->buf) |
| 1331 | return -ENODATA; |
| 1332 | |
| 1333 | musb_ep = to_musb_ep(ep); |
| 1334 | musb = musb_ep->musb; |
| 1335 | |
| 1336 | request = to_musb_request(req); |
| 1337 | request->musb = musb; |
| 1338 | |
| 1339 | if (request->ep != musb_ep) |
| 1340 | return -EINVAL; |
| 1341 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1342 | dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1343 | |
| 1344 | /* request is mine now... */ |
| 1345 | request->request.actual = 0; |
| 1346 | request->request.status = -EINPROGRESS; |
| 1347 | request->epnum = musb_ep->current_epnum; |
| 1348 | request->tx = musb_ep->is_in; |
| 1349 | |
Mian Yousaf Kaukab | c65bfa6 | 2011-01-04 12:47:02 +0100 | [diff] [blame] | 1350 | map_dma_buffer(request, musb, musb_ep); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1351 | |
| 1352 | spin_lock_irqsave(&musb->lock, lockflags); |
| 1353 | |
| 1354 | /* don't queue if the ep is down */ |
| 1355 | if (!musb_ep->desc) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1356 | dev_dbg(musb->controller, "req %p queued to %s while ep %s\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1357 | req, ep->name, "disabled"); |
| 1358 | status = -ESHUTDOWN; |
| 1359 | goto cleanup; |
| 1360 | } |
| 1361 | |
| 1362 | /* add request to the list */ |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 1363 | list_add_tail(&request->list, &musb_ep->req_list); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1364 | |
| 1365 | /* it this is the head of the queue, start i/o ... */ |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 1366 | if (!musb_ep->busy && &request->list == musb_ep->req_list.next) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1367 | musb_ep_restart(musb, request); |
| 1368 | |
| 1369 | cleanup: |
| 1370 | spin_unlock_irqrestore(&musb->lock, lockflags); |
| 1371 | return status; |
| 1372 | } |
| 1373 | |
| 1374 | static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request) |
| 1375 | { |
| 1376 | struct musb_ep *musb_ep = to_musb_ep(ep); |
Felipe Balbi | 4cbbf08 | 2011-02-28 10:44:50 +0200 | [diff] [blame] | 1377 | struct musb_request *req = to_musb_request(request); |
| 1378 | struct musb_request *r; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1379 | unsigned long flags; |
| 1380 | int status = 0; |
| 1381 | struct musb *musb = musb_ep->musb; |
| 1382 | |
| 1383 | if (!ep || !request || to_musb_request(request)->ep != musb_ep) |
| 1384 | return -EINVAL; |
| 1385 | |
| 1386 | spin_lock_irqsave(&musb->lock, flags); |
| 1387 | |
| 1388 | list_for_each_entry(r, &musb_ep->req_list, list) { |
Felipe Balbi | 4cbbf08 | 2011-02-28 10:44:50 +0200 | [diff] [blame] | 1389 | if (r == req) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1390 | break; |
| 1391 | } |
Felipe Balbi | 4cbbf08 | 2011-02-28 10:44:50 +0200 | [diff] [blame] | 1392 | if (r != req) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1393 | dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1394 | status = -EINVAL; |
| 1395 | goto done; |
| 1396 | } |
| 1397 | |
| 1398 | /* if the hardware doesn't have the request, easy ... */ |
Felipe Balbi | 3d5ad13 | 2011-03-22 11:38:49 +0200 | [diff] [blame] | 1399 | if (musb_ep->req_list.next != &req->list || musb_ep->busy) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1400 | musb_g_giveback(musb_ep, request, -ECONNRESET); |
| 1401 | |
| 1402 | /* ... else abort the dma transfer ... */ |
| 1403 | else if (is_dma_capable() && musb_ep->dma) { |
| 1404 | struct dma_controller *c = musb->dma_controller; |
| 1405 | |
| 1406 | musb_ep_select(musb->mregs, musb_ep->current_epnum); |
| 1407 | if (c->channel_abort) |
| 1408 | status = c->channel_abort(musb_ep->dma); |
| 1409 | else |
| 1410 | status = -EBUSY; |
| 1411 | if (status == 0) |
| 1412 | musb_g_giveback(musb_ep, request, -ECONNRESET); |
| 1413 | } else { |
| 1414 | /* NOTE: by sticking to easily tested hardware/driver states, |
| 1415 | * we leave counting of in-flight packets imprecise. |
| 1416 | */ |
| 1417 | musb_g_giveback(musb_ep, request, -ECONNRESET); |
| 1418 | } |
| 1419 | |
| 1420 | done: |
| 1421 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1422 | return status; |
| 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any |
| 1427 | * data but will queue requests. |
| 1428 | * |
| 1429 | * exported to ep0 code |
| 1430 | */ |
Felipe Balbi | 1b6c3b0 | 2009-12-04 15:47:46 +0200 | [diff] [blame] | 1431 | static int musb_gadget_set_halt(struct usb_ep *ep, int value) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1432 | { |
| 1433 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1434 | u8 epnum = musb_ep->current_epnum; |
| 1435 | struct musb *musb = musb_ep->musb; |
| 1436 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 1437 | void __iomem *mbase; |
| 1438 | unsigned long flags; |
| 1439 | u16 csr; |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1440 | struct musb_request *request; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1441 | int status = 0; |
| 1442 | |
| 1443 | if (!ep) |
| 1444 | return -EINVAL; |
| 1445 | mbase = musb->mregs; |
| 1446 | |
| 1447 | spin_lock_irqsave(&musb->lock, flags); |
| 1448 | |
| 1449 | if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) { |
| 1450 | status = -EINVAL; |
| 1451 | goto done; |
| 1452 | } |
| 1453 | |
| 1454 | musb_ep_select(mbase, epnum); |
| 1455 | |
Felipe Balbi | ad1adb8 | 2011-02-16 12:40:05 +0200 | [diff] [blame] | 1456 | request = next_request(musb_ep); |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1457 | if (value) { |
| 1458 | if (request) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1459 | dev_dbg(musb->controller, "request in progress, cannot halt %s\n", |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1460 | ep->name); |
| 1461 | status = -EAGAIN; |
| 1462 | goto done; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1463 | } |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1464 | /* Cannot portably stall with non-empty FIFO */ |
| 1465 | if (musb_ep->is_in) { |
| 1466 | csr = musb_readw(epio, MUSB_TXCSR); |
| 1467 | if (csr & MUSB_TXCSR_FIFONOTEMPTY) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1468 | dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name); |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1469 | status = -EAGAIN; |
| 1470 | goto done; |
| 1471 | } |
| 1472 | } |
Sergei Shtylyov | 47e9760 | 2009-11-18 22:51:51 +0300 | [diff] [blame] | 1473 | } else |
| 1474 | musb_ep->wedged = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1475 | |
| 1476 | /* set/clear the stall and toggle bits */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1477 | dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1478 | if (musb_ep->is_in) { |
| 1479 | csr = musb_readw(epio, MUSB_TXCSR); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1480 | csr |= MUSB_TXCSR_P_WZC_BITS |
| 1481 | | MUSB_TXCSR_CLRDATATOG; |
| 1482 | if (value) |
| 1483 | csr |= MUSB_TXCSR_P_SENDSTALL; |
| 1484 | else |
| 1485 | csr &= ~(MUSB_TXCSR_P_SENDSTALL |
| 1486 | | MUSB_TXCSR_P_SENTSTALL); |
| 1487 | csr &= ~MUSB_TXCSR_TXPKTRDY; |
| 1488 | musb_writew(epio, MUSB_TXCSR, csr); |
| 1489 | } else { |
| 1490 | csr = musb_readw(epio, MUSB_RXCSR); |
| 1491 | csr |= MUSB_RXCSR_P_WZC_BITS |
| 1492 | | MUSB_RXCSR_FLUSHFIFO |
| 1493 | | MUSB_RXCSR_CLRDATATOG; |
| 1494 | if (value) |
| 1495 | csr |= MUSB_RXCSR_P_SENDSTALL; |
| 1496 | else |
| 1497 | csr &= ~(MUSB_RXCSR_P_SENDSTALL |
| 1498 | | MUSB_RXCSR_P_SENTSTALL); |
| 1499 | musb_writew(epio, MUSB_RXCSR, csr); |
| 1500 | } |
| 1501 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1502 | /* maybe start the first request in the queue */ |
| 1503 | if (!musb_ep->busy && !value && request) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1504 | dev_dbg(musb->controller, "restarting the request\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1505 | musb_ep_restart(musb, request); |
| 1506 | } |
| 1507 | |
Sergei Shtylyov | cea8324 | 2009-11-18 22:51:18 +0300 | [diff] [blame] | 1508 | done: |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1509 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1510 | return status; |
| 1511 | } |
| 1512 | |
Sergei Shtylyov | 47e9760 | 2009-11-18 22:51:51 +0300 | [diff] [blame] | 1513 | /* |
| 1514 | * Sets the halt feature with the clear requests ignored |
| 1515 | */ |
Felipe Balbi | 1b6c3b0 | 2009-12-04 15:47:46 +0200 | [diff] [blame] | 1516 | static int musb_gadget_set_wedge(struct usb_ep *ep) |
Sergei Shtylyov | 47e9760 | 2009-11-18 22:51:51 +0300 | [diff] [blame] | 1517 | { |
| 1518 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1519 | |
| 1520 | if (!ep) |
| 1521 | return -EINVAL; |
| 1522 | |
| 1523 | musb_ep->wedged = 1; |
| 1524 | |
| 1525 | return usb_ep_set_halt(ep); |
| 1526 | } |
| 1527 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1528 | static int musb_gadget_fifo_status(struct usb_ep *ep) |
| 1529 | { |
| 1530 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1531 | void __iomem *epio = musb_ep->hw_ep->regs; |
| 1532 | int retval = -EINVAL; |
| 1533 | |
| 1534 | if (musb_ep->desc && !musb_ep->is_in) { |
| 1535 | struct musb *musb = musb_ep->musb; |
| 1536 | int epnum = musb_ep->current_epnum; |
| 1537 | void __iomem *mbase = musb->mregs; |
| 1538 | unsigned long flags; |
| 1539 | |
| 1540 | spin_lock_irqsave(&musb->lock, flags); |
| 1541 | |
| 1542 | musb_ep_select(mbase, epnum); |
| 1543 | /* FIXME return zero unless RXPKTRDY is set */ |
| 1544 | retval = musb_readw(epio, MUSB_RXCOUNT); |
| 1545 | |
| 1546 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1547 | } |
| 1548 | return retval; |
| 1549 | } |
| 1550 | |
| 1551 | static void musb_gadget_fifo_flush(struct usb_ep *ep) |
| 1552 | { |
| 1553 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1554 | struct musb *musb = musb_ep->musb; |
| 1555 | u8 epnum = musb_ep->current_epnum; |
| 1556 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 1557 | void __iomem *mbase; |
| 1558 | unsigned long flags; |
| 1559 | u16 csr, int_txe; |
| 1560 | |
| 1561 | mbase = musb->mregs; |
| 1562 | |
| 1563 | spin_lock_irqsave(&musb->lock, flags); |
| 1564 | musb_ep_select(mbase, (u8) epnum); |
| 1565 | |
| 1566 | /* disable interrupts */ |
| 1567 | int_txe = musb_readw(mbase, MUSB_INTRTXE); |
| 1568 | musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum)); |
| 1569 | |
| 1570 | if (musb_ep->is_in) { |
| 1571 | csr = musb_readw(epio, MUSB_TXCSR); |
| 1572 | if (csr & MUSB_TXCSR_FIFONOTEMPTY) { |
| 1573 | csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS; |
Yauheni Kaliuta | 4858f06 | 2011-06-08 17:12:02 +0300 | [diff] [blame] | 1574 | /* |
| 1575 | * Setting both TXPKTRDY and FLUSHFIFO makes controller |
| 1576 | * to interrupt current FIFO loading, but not flushing |
| 1577 | * the already loaded ones. |
| 1578 | */ |
| 1579 | csr &= ~MUSB_TXCSR_TXPKTRDY; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1580 | musb_writew(epio, MUSB_TXCSR, csr); |
| 1581 | /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ |
| 1582 | musb_writew(epio, MUSB_TXCSR, csr); |
| 1583 | } |
| 1584 | } else { |
| 1585 | csr = musb_readw(epio, MUSB_RXCSR); |
| 1586 | csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS; |
| 1587 | musb_writew(epio, MUSB_RXCSR, csr); |
| 1588 | musb_writew(epio, MUSB_RXCSR, csr); |
| 1589 | } |
| 1590 | |
| 1591 | /* re-enable interrupt */ |
| 1592 | musb_writew(mbase, MUSB_INTRTXE, int_txe); |
| 1593 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1594 | } |
| 1595 | |
| 1596 | static const struct usb_ep_ops musb_ep_ops = { |
| 1597 | .enable = musb_gadget_enable, |
| 1598 | .disable = musb_gadget_disable, |
| 1599 | .alloc_request = musb_alloc_request, |
| 1600 | .free_request = musb_free_request, |
| 1601 | .queue = musb_gadget_queue, |
| 1602 | .dequeue = musb_gadget_dequeue, |
| 1603 | .set_halt = musb_gadget_set_halt, |
Sergei Shtylyov | 47e9760 | 2009-11-18 22:51:51 +0300 | [diff] [blame] | 1604 | .set_wedge = musb_gadget_set_wedge, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1605 | .fifo_status = musb_gadget_fifo_status, |
| 1606 | .fifo_flush = musb_gadget_fifo_flush |
| 1607 | }; |
| 1608 | |
| 1609 | /* ----------------------------------------------------------------------- */ |
| 1610 | |
| 1611 | static int musb_gadget_get_frame(struct usb_gadget *gadget) |
| 1612 | { |
| 1613 | struct musb *musb = gadget_to_musb(gadget); |
| 1614 | |
| 1615 | return (int)musb_readw(musb->mregs, MUSB_FRAME); |
| 1616 | } |
| 1617 | |
| 1618 | static int musb_gadget_wakeup(struct usb_gadget *gadget) |
| 1619 | { |
| 1620 | struct musb *musb = gadget_to_musb(gadget); |
| 1621 | void __iomem *mregs = musb->mregs; |
| 1622 | unsigned long flags; |
| 1623 | int status = -EINVAL; |
| 1624 | u8 power, devctl; |
| 1625 | int retries; |
| 1626 | |
| 1627 | spin_lock_irqsave(&musb->lock, flags); |
| 1628 | |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 1629 | switch (musb->xceiv->state) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1630 | case OTG_STATE_B_PERIPHERAL: |
| 1631 | /* NOTE: OTG state machine doesn't include B_SUSPENDED; |
| 1632 | * that's part of the standard usb 1.1 state machine, and |
| 1633 | * doesn't affect OTG transitions. |
| 1634 | */ |
| 1635 | if (musb->may_wakeup && musb->is_suspended) |
| 1636 | break; |
| 1637 | goto done; |
| 1638 | case OTG_STATE_B_IDLE: |
| 1639 | /* Start SRP ... OTG not required. */ |
| 1640 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1641 | dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1642 | devctl |= MUSB_DEVCTL_SESSION; |
| 1643 | musb_writeb(mregs, MUSB_DEVCTL, devctl); |
| 1644 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 1645 | retries = 100; |
| 1646 | while (!(devctl & MUSB_DEVCTL_SESSION)) { |
| 1647 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 1648 | if (retries-- < 1) |
| 1649 | break; |
| 1650 | } |
| 1651 | retries = 10000; |
| 1652 | while (devctl & MUSB_DEVCTL_SESSION) { |
| 1653 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 1654 | if (retries-- < 1) |
| 1655 | break; |
| 1656 | } |
| 1657 | |
Hema HK | 8620543 | 2011-03-22 16:54:22 +0530 | [diff] [blame] | 1658 | spin_unlock_irqrestore(&musb->lock, flags); |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 1659 | otg_start_srp(musb->xceiv->otg); |
Hema HK | 8620543 | 2011-03-22 16:54:22 +0530 | [diff] [blame] | 1660 | spin_lock_irqsave(&musb->lock, flags); |
| 1661 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1662 | /* Block idling for at least 1s */ |
| 1663 | musb_platform_try_idle(musb, |
| 1664 | jiffies + msecs_to_jiffies(1 * HZ)); |
| 1665 | |
| 1666 | status = 0; |
| 1667 | goto done; |
| 1668 | default: |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1669 | dev_dbg(musb->controller, "Unhandled wake: %s\n", |
Anatolij Gustschin | 3df0045 | 2011-05-05 12:11:21 +0200 | [diff] [blame] | 1670 | otg_state_string(musb->xceiv->state)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1671 | goto done; |
| 1672 | } |
| 1673 | |
| 1674 | status = 0; |
| 1675 | |
| 1676 | power = musb_readb(mregs, MUSB_POWER); |
| 1677 | power |= MUSB_POWER_RESUME; |
| 1678 | musb_writeb(mregs, MUSB_POWER, power); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1679 | dev_dbg(musb->controller, "issue wakeup\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1680 | |
| 1681 | /* FIXME do this next chunk in a timer callback, no udelay */ |
| 1682 | mdelay(2); |
| 1683 | |
| 1684 | power = musb_readb(mregs, MUSB_POWER); |
| 1685 | power &= ~MUSB_POWER_RESUME; |
| 1686 | musb_writeb(mregs, MUSB_POWER, power); |
| 1687 | done: |
| 1688 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1689 | return status; |
| 1690 | } |
| 1691 | |
| 1692 | static int |
| 1693 | musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered) |
| 1694 | { |
| 1695 | struct musb *musb = gadget_to_musb(gadget); |
| 1696 | |
| 1697 | musb->is_self_powered = !!is_selfpowered; |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | static void musb_pullup(struct musb *musb, int is_on) |
| 1702 | { |
| 1703 | u8 power; |
| 1704 | |
| 1705 | power = musb_readb(musb->mregs, MUSB_POWER); |
| 1706 | if (is_on) |
| 1707 | power |= MUSB_POWER_SOFTCONN; |
| 1708 | else |
| 1709 | power &= ~MUSB_POWER_SOFTCONN; |
| 1710 | |
| 1711 | /* FIXME if on, HdrcStart; if off, HdrcStop */ |
| 1712 | |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 1713 | dev_dbg(musb->controller, "gadget D+ pullup %s\n", |
| 1714 | is_on ? "on" : "off"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1715 | musb_writeb(musb->mregs, MUSB_POWER, power); |
| 1716 | } |
| 1717 | |
| 1718 | #if 0 |
| 1719 | static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active) |
| 1720 | { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1721 | dev_dbg(musb->controller, "<= %s =>\n", __func__); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1722 | |
| 1723 | /* |
| 1724 | * FIXME iff driver's softconnect flag is set (as it is during probe, |
| 1725 | * though that can clear it), just musb_pullup(). |
| 1726 | */ |
| 1727 | |
| 1728 | return -EINVAL; |
| 1729 | } |
| 1730 | #endif |
| 1731 | |
| 1732 | static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) |
| 1733 | { |
| 1734 | struct musb *musb = gadget_to_musb(gadget); |
| 1735 | |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 1736 | if (!musb->xceiv->set_power) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1737 | return -EOPNOTSUPP; |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 1738 | return usb_phy_set_power(musb->xceiv, mA); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on) |
| 1742 | { |
| 1743 | struct musb *musb = gadget_to_musb(gadget); |
| 1744 | unsigned long flags; |
| 1745 | |
| 1746 | is_on = !!is_on; |
| 1747 | |
John Stultz | 93e098a | 2011-07-20 17:09:34 -0700 | [diff] [blame] | 1748 | pm_runtime_get_sync(musb->controller); |
| 1749 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1750 | /* NOTE: this assumes we are sensing vbus; we'd rather |
| 1751 | * not pullup unless the B-session is active. |
| 1752 | */ |
| 1753 | spin_lock_irqsave(&musb->lock, flags); |
| 1754 | if (is_on != musb->softconnect) { |
| 1755 | musb->softconnect = is_on; |
| 1756 | musb_pullup(musb, is_on); |
| 1757 | } |
| 1758 | spin_unlock_irqrestore(&musb->lock, flags); |
John Stultz | 93e098a | 2011-07-20 17:09:34 -0700 | [diff] [blame] | 1759 | |
| 1760 | pm_runtime_put(musb->controller); |
| 1761 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1762 | return 0; |
| 1763 | } |
| 1764 | |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 1765 | static int musb_gadget_start(struct usb_gadget *g, |
| 1766 | struct usb_gadget_driver *driver); |
| 1767 | static int musb_gadget_stop(struct usb_gadget *g, |
| 1768 | struct usb_gadget_driver *driver); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1769 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1770 | static const struct usb_gadget_ops musb_gadget_operations = { |
| 1771 | .get_frame = musb_gadget_get_frame, |
| 1772 | .wakeup = musb_gadget_wakeup, |
| 1773 | .set_selfpowered = musb_gadget_set_self_powered, |
| 1774 | /* .vbus_session = musb_gadget_vbus_session, */ |
| 1775 | .vbus_draw = musb_gadget_vbus_draw, |
| 1776 | .pullup = musb_gadget_pullup, |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 1777 | .udc_start = musb_gadget_start, |
| 1778 | .udc_stop = musb_gadget_stop, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1779 | }; |
| 1780 | |
| 1781 | /* ----------------------------------------------------------------------- */ |
| 1782 | |
| 1783 | /* Registration */ |
| 1784 | |
| 1785 | /* Only this registration code "knows" the rule (from USB standards) |
| 1786 | * about there being only one external upstream port. It assumes |
| 1787 | * all peripheral ports are external... |
| 1788 | */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1789 | |
| 1790 | static void musb_gadget_release(struct device *dev) |
| 1791 | { |
| 1792 | /* kref_put(WHAT) */ |
| 1793 | dev_dbg(dev, "%s\n", __func__); |
| 1794 | } |
| 1795 | |
| 1796 | |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 1797 | static void __devinit |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1798 | init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in) |
| 1799 | { |
| 1800 | struct musb_hw_ep *hw_ep = musb->endpoints + epnum; |
| 1801 | |
| 1802 | memset(ep, 0, sizeof *ep); |
| 1803 | |
| 1804 | ep->current_epnum = epnum; |
| 1805 | ep->musb = musb; |
| 1806 | ep->hw_ep = hw_ep; |
| 1807 | ep->is_in = is_in; |
| 1808 | |
| 1809 | INIT_LIST_HEAD(&ep->req_list); |
| 1810 | |
| 1811 | sprintf(ep->name, "ep%d%s", epnum, |
| 1812 | (!epnum || hw_ep->is_shared_fifo) ? "" : ( |
| 1813 | is_in ? "in" : "out")); |
| 1814 | ep->end_point.name = ep->name; |
| 1815 | INIT_LIST_HEAD(&ep->end_point.ep_list); |
| 1816 | if (!epnum) { |
| 1817 | ep->end_point.maxpacket = 64; |
| 1818 | ep->end_point.ops = &musb_g_ep0_ops; |
| 1819 | musb->g.ep0 = &ep->end_point; |
| 1820 | } else { |
| 1821 | if (is_in) |
| 1822 | ep->end_point.maxpacket = hw_ep->max_packet_sz_tx; |
| 1823 | else |
| 1824 | ep->end_point.maxpacket = hw_ep->max_packet_sz_rx; |
| 1825 | ep->end_point.ops = &musb_ep_ops; |
| 1826 | list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | * Initialize the endpoints exposed to peripheral drivers, with backlinks |
| 1832 | * to the rest of the driver state. |
| 1833 | */ |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 1834 | static inline void __devinit musb_g_init_endpoints(struct musb *musb) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1835 | { |
| 1836 | u8 epnum; |
| 1837 | struct musb_hw_ep *hw_ep; |
| 1838 | unsigned count = 0; |
| 1839 | |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1840 | /* initialize endpoint list just once */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1841 | INIT_LIST_HEAD(&(musb->g.ep_list)); |
| 1842 | |
| 1843 | for (epnum = 0, hw_ep = musb->endpoints; |
| 1844 | epnum < musb->nr_endpoints; |
| 1845 | epnum++, hw_ep++) { |
| 1846 | if (hw_ep->is_shared_fifo /* || !epnum */) { |
| 1847 | init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0); |
| 1848 | count++; |
| 1849 | } else { |
| 1850 | if (hw_ep->max_packet_sz_tx) { |
| 1851 | init_peripheral_ep(musb, &hw_ep->ep_in, |
| 1852 | epnum, 1); |
| 1853 | count++; |
| 1854 | } |
| 1855 | if (hw_ep->max_packet_sz_rx) { |
| 1856 | init_peripheral_ep(musb, &hw_ep->ep_out, |
| 1857 | epnum, 0); |
| 1858 | count++; |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* called once during driver setup to initialize and link into |
| 1865 | * the driver model; memory is zeroed. |
| 1866 | */ |
Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 1867 | int __devinit musb_gadget_setup(struct musb *musb) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1868 | { |
| 1869 | int status; |
| 1870 | |
| 1871 | /* REVISIT minor race: if (erroneously) setting up two |
| 1872 | * musb peripherals at the same time, only the bus lock |
| 1873 | * is probably held. |
| 1874 | */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1875 | |
| 1876 | musb->g.ops = &musb_gadget_operations; |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 1877 | musb->g.max_speed = USB_SPEED_HIGH; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1878 | musb->g.speed = USB_SPEED_UNKNOWN; |
| 1879 | |
| 1880 | /* this "gadget" abstracts/virtualizes the controller */ |
Kay Sievers | 427c4f3 | 2008-11-07 01:52:53 +0100 | [diff] [blame] | 1881 | dev_set_name(&musb->g.dev, "gadget"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1882 | musb->g.dev.parent = musb->controller; |
| 1883 | musb->g.dev.dma_mask = musb->controller->dma_mask; |
| 1884 | musb->g.dev.release = musb_gadget_release; |
| 1885 | musb->g.name = musb_driver_name; |
| 1886 | |
| 1887 | if (is_otg_enabled(musb)) |
| 1888 | musb->g.is_otg = 1; |
| 1889 | |
| 1890 | musb_g_init_endpoints(musb); |
| 1891 | |
| 1892 | musb->is_active = 0; |
| 1893 | musb_platform_try_idle(musb, 0); |
| 1894 | |
| 1895 | status = device_register(&musb->g.dev); |
Rahul Ruikar | e2c3404 | 2010-10-02 01:35:48 -0500 | [diff] [blame] | 1896 | if (status != 0) { |
| 1897 | put_device(&musb->g.dev); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1898 | return status; |
Rahul Ruikar | e2c3404 | 2010-10-02 01:35:48 -0500 | [diff] [blame] | 1899 | } |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1900 | status = usb_add_gadget_udc(musb->controller, &musb->g); |
| 1901 | if (status) |
| 1902 | goto err; |
| 1903 | |
| 1904 | return 0; |
| 1905 | err: |
Sebastian Andrzej Siewior | 6193d69 | 2011-08-10 11:01:57 +0200 | [diff] [blame] | 1906 | musb->g.dev.parent = NULL; |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1907 | device_unregister(&musb->g.dev); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1908 | return status; |
| 1909 | } |
| 1910 | |
| 1911 | void musb_gadget_cleanup(struct musb *musb) |
| 1912 | { |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1913 | usb_del_gadget_udc(&musb->g); |
Sebastian Andrzej Siewior | 6193d69 | 2011-08-10 11:01:57 +0200 | [diff] [blame] | 1914 | if (musb->g.dev.parent) |
| 1915 | device_unregister(&musb->g.dev); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | /* |
| 1919 | * Register the gadget driver. Used by gadget drivers when |
| 1920 | * registering themselves with the controller. |
| 1921 | * |
| 1922 | * -EINVAL something went wrong (not driver) |
| 1923 | * -EBUSY another gadget is already using the controller |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1924 | * -ENOMEM no memory to perform the operation |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1925 | * |
| 1926 | * @param driver the gadget driver |
| 1927 | * @return <0 if error, 0 if everything is fine |
| 1928 | */ |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 1929 | static int musb_gadget_start(struct usb_gadget *g, |
| 1930 | struct usb_gadget_driver *driver) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1931 | { |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 1932 | struct musb *musb = gadget_to_musb(g); |
Heikki Krogerus | d445b6d | 2012-02-13 13:24:15 +0200 | [diff] [blame] | 1933 | struct usb_otg *otg = musb->xceiv->otg; |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1934 | unsigned long flags; |
| 1935 | int retval = -EINVAL; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1936 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 1937 | if (driver->max_speed < USB_SPEED_HIGH) |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1938 | goto err0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1939 | |
Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 1940 | pm_runtime_get_sync(musb->controller); |
| 1941 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1942 | dev_dbg(musb->controller, "registering driver %s\n", driver->function); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1943 | |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 1944 | musb->softconnect = 0; |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1945 | musb->gadget_driver = driver; |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1946 | |
| 1947 | spin_lock_irqsave(&musb->lock, flags); |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 1948 | musb->is_active = 1; |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1949 | |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 1950 | otg_set_peripheral(otg, &musb->g); |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1951 | musb->xceiv->state = OTG_STATE_B_IDLE; |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1952 | |
| 1953 | /* |
| 1954 | * FIXME this ignores the softconnect flag. Drivers are |
| 1955 | * allowed hold the peripheral inactive until for example |
| 1956 | * userspace hooks up printer hardware or DSP codecs, so |
| 1957 | * hosts only see fully functional devices. |
| 1958 | */ |
| 1959 | |
| 1960 | if (!is_otg_enabled(musb)) |
| 1961 | musb_start(musb); |
| 1962 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1963 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1964 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1965 | if (is_otg_enabled(musb)) { |
| 1966 | struct usb_hcd *hcd = musb_to_hcd(musb); |
Felipe Balbi | f362a47 | 2008-08-04 13:53:52 +0300 | [diff] [blame] | 1967 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1968 | dev_dbg(musb->controller, "OTG startup...\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1969 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1970 | /* REVISIT: funcall to other code, which also |
| 1971 | * handles power budgeting ... this way also |
| 1972 | * ensures HdrcStart is indirectly called. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1973 | */ |
Felipe Balbi | cd70469 | 2012-02-29 16:46:23 +0200 | [diff] [blame] | 1974 | retval = usb_add_hcd(musb_to_hcd(musb), 0, 0); |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1975 | if (retval < 0) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1976 | dev_dbg(musb->controller, "add_hcd failed, %d\n", retval); |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1977 | goto err2; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1978 | } |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1979 | |
Hema HK | 5f1e8ce | 2011-03-17 16:11:58 +0530 | [diff] [blame] | 1980 | if ((musb->xceiv->last_event == USB_EVENT_ID) |
Heikki Krogerus | d445b6d | 2012-02-13 13:24:15 +0200 | [diff] [blame] | 1981 | && otg->set_vbus) |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 1982 | otg_set_vbus(otg, 1); |
Hema HK | 5f1e8ce | 2011-03-17 16:11:58 +0530 | [diff] [blame] | 1983 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1984 | hcd->self.uses_pio_for_control = 1; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1985 | } |
Jarkko Nikula | cdefce1 | 2011-04-29 16:17:35 +0300 | [diff] [blame] | 1986 | if (musb->xceiv->last_event == USB_EVENT_NONE) |
| 1987 | pm_runtime_put(musb->controller); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1988 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1989 | return 0; |
| 1990 | |
| 1991 | err2: |
| 1992 | if (!is_otg_enabled(musb)) |
| 1993 | musb_stop(musb); |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 1994 | err0: |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1995 | return retval; |
| 1996 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1997 | |
| 1998 | static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver) |
| 1999 | { |
| 2000 | int i; |
| 2001 | struct musb_hw_ep *hw_ep; |
| 2002 | |
| 2003 | /* don't disconnect if it's not connected */ |
| 2004 | if (musb->g.speed == USB_SPEED_UNKNOWN) |
| 2005 | driver = NULL; |
| 2006 | else |
| 2007 | musb->g.speed = USB_SPEED_UNKNOWN; |
| 2008 | |
| 2009 | /* deactivate the hardware */ |
| 2010 | if (musb->softconnect) { |
| 2011 | musb->softconnect = 0; |
| 2012 | musb_pullup(musb, 0); |
| 2013 | } |
| 2014 | musb_stop(musb); |
| 2015 | |
| 2016 | /* killing any outstanding requests will quiesce the driver; |
| 2017 | * then report disconnect |
| 2018 | */ |
| 2019 | if (driver) { |
| 2020 | for (i = 0, hw_ep = musb->endpoints; |
| 2021 | i < musb->nr_endpoints; |
| 2022 | i++, hw_ep++) { |
| 2023 | musb_ep_select(musb->mregs, i); |
| 2024 | if (hw_ep->is_shared_fifo /* || !epnum */) { |
| 2025 | nuke(&hw_ep->ep_in, -ESHUTDOWN); |
| 2026 | } else { |
| 2027 | if (hw_ep->max_packet_sz_tx) |
| 2028 | nuke(&hw_ep->ep_in, -ESHUTDOWN); |
| 2029 | if (hw_ep->max_packet_sz_rx) |
| 2030 | nuke(&hw_ep->ep_out, -ESHUTDOWN); |
| 2031 | } |
| 2032 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | /* |
| 2037 | * Unregister the gadget driver. Used by gadget drivers when |
| 2038 | * unregistering themselves from the controller. |
| 2039 | * |
| 2040 | * @param driver the gadget driver to unregister |
| 2041 | */ |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 2042 | static int musb_gadget_stop(struct usb_gadget *g, |
| 2043 | struct usb_gadget_driver *driver) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2044 | { |
Sebastian Andrzej Siewior | e71eb39 | 2011-06-23 14:26:16 +0200 | [diff] [blame] | 2045 | struct musb *musb = gadget_to_musb(g); |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2046 | unsigned long flags; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2047 | |
Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 2048 | if (musb->xceiv->last_event == USB_EVENT_NONE) |
| 2049 | pm_runtime_get_sync(musb->controller); |
| 2050 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2051 | /* |
| 2052 | * REVISIT always use otg_set_peripheral() here too; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2053 | * this needs to shut down the OTG engine. |
| 2054 | */ |
| 2055 | |
| 2056 | spin_lock_irqsave(&musb->lock, flags); |
| 2057 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2058 | musb_hnp_stop(musb); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2059 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2060 | (void) musb_gadget_vbus_draw(&musb->g, 0); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2061 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2062 | musb->xceiv->state = OTG_STATE_UNDEFINED; |
| 2063 | stop_activity(musb, driver); |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 2064 | otg_set_peripheral(musb->xceiv->otg, NULL); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2065 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2066 | dev_dbg(musb->controller, "unregistering driver %s\n", driver->function); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2067 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2068 | musb->is_active = 0; |
| 2069 | musb_platform_try_idle(musb, 0); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2070 | spin_unlock_irqrestore(&musb->lock, flags); |
| 2071 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2072 | if (is_otg_enabled(musb)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2073 | usb_remove_hcd(musb_to_hcd(musb)); |
| 2074 | /* FIXME we need to be able to register another |
| 2075 | * gadget driver here and have everything work; |
| 2076 | * that currently misbehaves. |
| 2077 | */ |
| 2078 | } |
| 2079 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2080 | if (!is_otg_enabled(musb)) |
| 2081 | musb_stop(musb); |
| 2082 | |
Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 2083 | pm_runtime_put(musb->controller); |
| 2084 | |
Felipe Balbi | 63eed2b | 2011-01-17 10:34:38 +0200 | [diff] [blame] | 2085 | return 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2086 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2087 | |
| 2088 | /* ----------------------------------------------------------------------- */ |
| 2089 | |
| 2090 | /* lifecycle operations called through plat_uds.c */ |
| 2091 | |
| 2092 | void musb_g_resume(struct musb *musb) |
| 2093 | { |
| 2094 | musb->is_suspended = 0; |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2095 | switch (musb->xceiv->state) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2096 | case OTG_STATE_B_IDLE: |
| 2097 | break; |
| 2098 | case OTG_STATE_B_WAIT_ACON: |
| 2099 | case OTG_STATE_B_PERIPHERAL: |
| 2100 | musb->is_active = 1; |
| 2101 | if (musb->gadget_driver && musb->gadget_driver->resume) { |
| 2102 | spin_unlock(&musb->lock); |
| 2103 | musb->gadget_driver->resume(&musb->g); |
| 2104 | spin_lock(&musb->lock); |
| 2105 | } |
| 2106 | break; |
| 2107 | default: |
| 2108 | WARNING("unhandled RESUME transition (%s)\n", |
Anatolij Gustschin | 3df0045 | 2011-05-05 12:11:21 +0200 | [diff] [blame] | 2109 | otg_state_string(musb->xceiv->state)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | /* called when SOF packets stop for 3+ msec */ |
| 2114 | void musb_g_suspend(struct musb *musb) |
| 2115 | { |
| 2116 | u8 devctl; |
| 2117 | |
| 2118 | devctl = musb_readb(musb->mregs, MUSB_DEVCTL); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2119 | dev_dbg(musb->controller, "devctl %02x\n", devctl); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2120 | |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2121 | switch (musb->xceiv->state) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2122 | case OTG_STATE_B_IDLE: |
| 2123 | if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2124 | musb->xceiv->state = OTG_STATE_B_PERIPHERAL; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2125 | break; |
| 2126 | case OTG_STATE_B_PERIPHERAL: |
| 2127 | musb->is_suspended = 1; |
| 2128 | if (musb->gadget_driver && musb->gadget_driver->suspend) { |
| 2129 | spin_unlock(&musb->lock); |
| 2130 | musb->gadget_driver->suspend(&musb->g); |
| 2131 | spin_lock(&musb->lock); |
| 2132 | } |
| 2133 | break; |
| 2134 | default: |
| 2135 | /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ; |
| 2136 | * A_PERIPHERAL may need care too |
| 2137 | */ |
| 2138 | WARNING("unhandled SUSPEND transition (%s)\n", |
Anatolij Gustschin | 3df0045 | 2011-05-05 12:11:21 +0200 | [diff] [blame] | 2139 | otg_state_string(musb->xceiv->state)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | /* Called during SRP */ |
| 2144 | void musb_g_wakeup(struct musb *musb) |
| 2145 | { |
| 2146 | musb_gadget_wakeup(&musb->g); |
| 2147 | } |
| 2148 | |
| 2149 | /* called when VBUS drops below session threshold, and in other cases */ |
| 2150 | void musb_g_disconnect(struct musb *musb) |
| 2151 | { |
| 2152 | void __iomem *mregs = musb->mregs; |
| 2153 | u8 devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 2154 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2155 | dev_dbg(musb->controller, "devctl %02x\n", devctl); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2156 | |
| 2157 | /* clear HR */ |
| 2158 | musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION); |
| 2159 | |
| 2160 | /* don't draw vbus until new b-default session */ |
| 2161 | (void) musb_gadget_vbus_draw(&musb->g, 0); |
| 2162 | |
| 2163 | musb->g.speed = USB_SPEED_UNKNOWN; |
| 2164 | if (musb->gadget_driver && musb->gadget_driver->disconnect) { |
| 2165 | spin_unlock(&musb->lock); |
| 2166 | musb->gadget_driver->disconnect(&musb->g); |
| 2167 | spin_lock(&musb->lock); |
| 2168 | } |
| 2169 | |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2170 | switch (musb->xceiv->state) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2171 | default: |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2172 | dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n", |
Anatolij Gustschin | 3df0045 | 2011-05-05 12:11:21 +0200 | [diff] [blame] | 2173 | otg_state_string(musb->xceiv->state)); |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2174 | musb->xceiv->state = OTG_STATE_A_IDLE; |
David Brownell | ab983f2a | 2009-03-31 12:35:09 -0700 | [diff] [blame] | 2175 | MUSB_HST_MODE(musb); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2176 | break; |
| 2177 | case OTG_STATE_A_PERIPHERAL: |
David Brownell | 1de00da | 2009-04-02 10:16:11 -0700 | [diff] [blame] | 2178 | musb->xceiv->state = OTG_STATE_A_WAIT_BCON; |
David Brownell | ab983f2a | 2009-03-31 12:35:09 -0700 | [diff] [blame] | 2179 | MUSB_HST_MODE(musb); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2180 | break; |
| 2181 | case OTG_STATE_B_WAIT_ACON: |
| 2182 | case OTG_STATE_B_HOST: |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2183 | case OTG_STATE_B_PERIPHERAL: |
| 2184 | case OTG_STATE_B_IDLE: |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2185 | musb->xceiv->state = OTG_STATE_B_IDLE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2186 | break; |
| 2187 | case OTG_STATE_B_SRP_INIT: |
| 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | musb->is_active = 0; |
| 2192 | } |
| 2193 | |
| 2194 | void musb_g_reset(struct musb *musb) |
| 2195 | __releases(musb->lock) |
| 2196 | __acquires(musb->lock) |
| 2197 | { |
| 2198 | void __iomem *mbase = musb->mregs; |
| 2199 | u8 devctl = musb_readb(mbase, MUSB_DEVCTL); |
| 2200 | u8 power; |
| 2201 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2202 | dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2203 | (devctl & MUSB_DEVCTL_BDEVICE) |
| 2204 | ? "B-Device" : "A-Device", |
| 2205 | musb_readb(mbase, MUSB_FADDR), |
| 2206 | musb->gadget_driver |
| 2207 | ? musb->gadget_driver->driver.name |
| 2208 | : NULL |
| 2209 | ); |
| 2210 | |
| 2211 | /* report disconnect, if we didn't already (flushing EP state) */ |
| 2212 | if (musb->g.speed != USB_SPEED_UNKNOWN) |
| 2213 | musb_g_disconnect(musb); |
| 2214 | |
| 2215 | /* clear HR */ |
| 2216 | else if (devctl & MUSB_DEVCTL_HR) |
| 2217 | musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); |
| 2218 | |
| 2219 | |
| 2220 | /* what speed did we negotiate? */ |
| 2221 | power = musb_readb(mbase, MUSB_POWER); |
| 2222 | musb->g.speed = (power & MUSB_POWER_HSMODE) |
| 2223 | ? USB_SPEED_HIGH : USB_SPEED_FULL; |
| 2224 | |
| 2225 | /* start in USB_STATE_DEFAULT */ |
| 2226 | musb->is_active = 1; |
| 2227 | musb->is_suspended = 0; |
| 2228 | MUSB_DEV_MODE(musb); |
| 2229 | musb->address = 0; |
| 2230 | musb->ep0_state = MUSB_EP0_STAGE_SETUP; |
| 2231 | |
| 2232 | musb->may_wakeup = 0; |
| 2233 | musb->g.b_hnp_enable = 0; |
| 2234 | musb->g.a_alt_hnp_support = 0; |
| 2235 | musb->g.a_hnp_support = 0; |
| 2236 | |
| 2237 | /* Normal reset, as B-Device; |
| 2238 | * or else after HNP, as A-Device |
| 2239 | */ |
| 2240 | if (devctl & MUSB_DEVCTL_BDEVICE) { |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2241 | musb->xceiv->state = OTG_STATE_B_PERIPHERAL; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2242 | musb->g.is_a_peripheral = 0; |
| 2243 | } else if (is_otg_enabled(musb)) { |
David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 2244 | musb->xceiv->state = OTG_STATE_A_PERIPHERAL; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2245 | musb->g.is_a_peripheral = 1; |
| 2246 | } else |
| 2247 | WARN_ON(1); |
| 2248 | |
| 2249 | /* start with default limits on VBUS power draw */ |
| 2250 | (void) musb_gadget_vbus_draw(&musb->g, |
| 2251 | is_otg_enabled(musb) ? 8 : 100); |
| 2252 | } |