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