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