diff options
author | Linus Torvalds <torvalds@g5.osdl.org> | 2006-03-21 12:25:47 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-03-21 12:25:47 -0500 |
commit | 2bf2154c6bb5599e3ec3f73c34861a0b12aa839e (patch) | |
tree | 62691bd915e2e3c2e6648306d3fb893f7a1dc57e /drivers/usb/gadget/at91_udc.c | |
parent | 08a4ecee986dd98e86090ff5faac4782b6765aed (diff) | |
parent | 71a8924bee63d891f6256d560e32416a458440b3 (diff) |
Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6
* master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6: (81 commits)
[PATCH] USB: omninet: fix up debugging comments
[PATCH] USB serial: add navman driver
[PATCH] USB: Fix irda-usb use after use
[PATCH] USB: rtl8150 small fix
[PATCH] USB: ftdi_sio: add Icom ID1 USB product and vendor ids
[PATCH] USB: cp2101: add new device IDs
[PATCH] USB: fix check_ctrlrecip to allow control transfers in state ADDRESS
[PATCH] USB: vicam.c: fix a NULL pointer dereference
[PATCH] USB: ZC0301 driver bugfix
[PATCH] USB: add support for Creativelabs Silvercrest USB keyboard
[PATCH] USB: storage: new unusual_devs.h entry: Mitsumi 7in1 Card Reader
[PATCH] USB: storage: unusual_devs.h entry 0420:0001
[PATCH] USB: storage: another unusual_devs.h entry
[PATCH] USB: storage: sandisk unusual_devices entry
[PATCH] USB: fix initdata issue in isp116x-hcd
[PATCH] USB: usbcore: usb_set_configuration oops (NULL ptr dereference)
[PATCH] USB: usbcore: Don't assume a USB configuration includes any interfaces
[PATCH] USB: ub 03 drop stall clearing
[PATCH] USB: ub 02 remove diag
[PATCH] USB: ub 01 remove first_open
...
Diffstat (limited to 'drivers/usb/gadget/at91_udc.c')
-rw-r--r-- | drivers/usb/gadget/at91_udc.c | 1773 |
1 files changed, 1773 insertions, 0 deletions
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c new file mode 100644 index 000000000000..865858cfd1c2 --- /dev/null +++ b/drivers/usb/gadget/at91_udc.c | |||
@@ -0,0 +1,1773 @@ | |||
1 | /* | ||
2 | * at91_udc -- driver for at91-series USB peripheral controller | ||
3 | * | ||
4 | * Copyright (C) 2004 by Thomas Rathbone | ||
5 | * Copyright (C) 2005 by HP Labs | ||
6 | * Copyright (C) 2005 by David Brownell | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU 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 | ||
20 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
21 | * Boston, MA 02111-1307, USA. | ||
22 | */ | ||
23 | |||
24 | #undef DEBUG | ||
25 | #undef VERBOSE | ||
26 | #undef PACKET_TRACE | ||
27 | |||
28 | #include <linux/config.h> | ||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/module.h> | ||
31 | #include <linux/platform_device.h> | ||
32 | #include <linux/delay.h> | ||
33 | #include <linux/ioport.h> | ||
34 | #include <linux/sched.h> | ||
35 | #include <linux/slab.h> | ||
36 | #include <linux/smp_lock.h> | ||
37 | #include <linux/errno.h> | ||
38 | #include <linux/init.h> | ||
39 | #include <linux/list.h> | ||
40 | #include <linux/interrupt.h> | ||
41 | #include <linux/proc_fs.h> | ||
42 | #include <linux/clk.h> | ||
43 | #include <linux/usb_ch9.h> | ||
44 | #include <linux/usb_gadget.h> | ||
45 | |||
46 | #include <asm/byteorder.h> | ||
47 | #include <asm/io.h> | ||
48 | #include <asm/irq.h> | ||
49 | #include <asm/system.h> | ||
50 | #include <asm/mach-types.h> | ||
51 | |||
52 | #include <asm/arch/hardware.h> | ||
53 | #include <asm/arch/gpio.h> | ||
54 | #include <asm/arch/board.h> | ||
55 | |||
56 | #include "at91_udc.h" | ||
57 | |||
58 | |||
59 | /* | ||
60 | * This controller is simple and PIO-only. It's used in many AT91-series | ||
61 | * ARMv4T controllers, including the at91rm9200 (arm920T, with MMU), | ||
62 | * at91sam9261 (arm926ejs, with MMU), and several no-mmu versions. | ||
63 | * | ||
64 | * This driver expects the board has been wired with two GPIOs suppporting | ||
65 | * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the | ||
66 | * testing hasn't covered such cases.) The pullup is most important; it | ||
67 | * provides software control over whether the host enumerates the device. | ||
68 | * The VBUS sensing helps during enumeration, and allows both USB clocks | ||
69 | * (and the transceiver) to stay gated off until they're necessary, saving | ||
70 | * power. During USB suspend, the 48 MHz clock is gated off. | ||
71 | */ | ||
72 | |||
73 | #define DRIVER_VERSION "8 March 2005" | ||
74 | |||
75 | static const char driver_name [] = "at91_udc"; | ||
76 | static const char ep0name[] = "ep0"; | ||
77 | |||
78 | /*-------------------------------------------------------------------------*/ | ||
79 | |||
80 | /* | ||
81 | * Read from a UDP register. | ||
82 | */ | ||
83 | static inline unsigned long at91_udp_read(unsigned int reg) | ||
84 | { | ||
85 | void __iomem *udp_base = (void __iomem *)AT91_VA_BASE_UDP; | ||
86 | |||
87 | return __raw_readl(udp_base + reg); | ||
88 | } | ||
89 | |||
90 | /* | ||
91 | * Write to a UDP register. | ||
92 | */ | ||
93 | static inline void at91_udp_write(unsigned int reg, unsigned long value) | ||
94 | { | ||
95 | void __iomem *udp_base = (void __iomem *)AT91_VA_BASE_UDP; | ||
96 | |||
97 | __raw_writel(value, udp_base + reg); | ||
98 | } | ||
99 | |||
100 | /*-------------------------------------------------------------------------*/ | ||
101 | |||
102 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES | ||
103 | |||
104 | #include <linux/seq_file.h> | ||
105 | |||
106 | static const char debug_filename[] = "driver/udc"; | ||
107 | |||
108 | #define FOURBITS "%s%s%s%s" | ||
109 | #define EIGHTBITS FOURBITS FOURBITS | ||
110 | |||
111 | static void proc_ep_show(struct seq_file *s, struct at91_ep *ep) | ||
112 | { | ||
113 | static char *types[] = { | ||
114 | "control", "out-iso", "out-bulk", "out-int", | ||
115 | "BOGUS", "in-iso", "in-bulk", "in-int"}; | ||
116 | |||
117 | u32 csr; | ||
118 | struct at91_request *req; | ||
119 | unsigned long flags; | ||
120 | |||
121 | local_irq_save(flags); | ||
122 | |||
123 | csr = __raw_readl(ep->creg); | ||
124 | |||
125 | /* NOTE: not collecting per-endpoint irq statistics... */ | ||
126 | |||
127 | seq_printf(s, "\n"); | ||
128 | seq_printf(s, "%s, maxpacket %d %s%s %s%s\n", | ||
129 | ep->ep.name, ep->ep.maxpacket, | ||
130 | ep->is_in ? "in" : "out", | ||
131 | ep->is_iso ? " iso" : "", | ||
132 | ep->is_pingpong | ||
133 | ? (ep->fifo_bank ? "pong" : "ping") | ||
134 | : "", | ||
135 | ep->stopped ? " stopped" : ""); | ||
136 | seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n", | ||
137 | csr, | ||
138 | (csr & 0x07ff0000) >> 16, | ||
139 | (csr & (1 << 15)) ? "enabled" : "disabled", | ||
140 | (csr & (1 << 11)) ? "DATA1" : "DATA0", | ||
141 | types[(csr & 0x700) >> 8], | ||
142 | |||
143 | /* iff type is control then print current direction */ | ||
144 | (!(csr & 0x700)) | ||
145 | ? ((csr & (1 << 7)) ? " IN" : " OUT") | ||
146 | : "", | ||
147 | (csr & (1 << 6)) ? " rxdatabk1" : "", | ||
148 | (csr & (1 << 5)) ? " forcestall" : "", | ||
149 | (csr & (1 << 4)) ? " txpktrdy" : "", | ||
150 | |||
151 | (csr & (1 << 3)) ? " stallsent" : "", | ||
152 | (csr & (1 << 2)) ? " rxsetup" : "", | ||
153 | (csr & (1 << 1)) ? " rxdatabk0" : "", | ||
154 | (csr & (1 << 0)) ? " txcomp" : ""); | ||
155 | if (list_empty (&ep->queue)) | ||
156 | seq_printf(s, "\t(queue empty)\n"); | ||
157 | |||
158 | else list_for_each_entry (req, &ep->queue, queue) { | ||
159 | unsigned length = req->req.actual; | ||
160 | |||
161 | seq_printf(s, "\treq %p len %d/%d buf %p\n", | ||
162 | &req->req, length, | ||
163 | req->req.length, req->req.buf); | ||
164 | } | ||
165 | local_irq_restore(flags); | ||
166 | } | ||
167 | |||
168 | static void proc_irq_show(struct seq_file *s, const char *label, u32 mask) | ||
169 | { | ||
170 | int i; | ||
171 | |||
172 | seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask, | ||
173 | (mask & (1 << 13)) ? " wakeup" : "", | ||
174 | (mask & (1 << 12)) ? " endbusres" : "", | ||
175 | |||
176 | (mask & (1 << 11)) ? " sofint" : "", | ||
177 | (mask & (1 << 10)) ? " extrsm" : "", | ||
178 | (mask & (1 << 9)) ? " rxrsm" : "", | ||
179 | (mask & (1 << 8)) ? " rxsusp" : ""); | ||
180 | for (i = 0; i < 8; i++) { | ||
181 | if (mask & (1 << i)) | ||
182 | seq_printf(s, " ep%d", i); | ||
183 | } | ||
184 | seq_printf(s, "\n"); | ||
185 | } | ||
186 | |||
187 | static int proc_udc_show(struct seq_file *s, void *unused) | ||
188 | { | ||
189 | struct at91_udc *udc = s->private; | ||
190 | struct at91_ep *ep; | ||
191 | u32 tmp; | ||
192 | |||
193 | seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION); | ||
194 | |||
195 | seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n", | ||
196 | udc->vbus ? "present" : "off", | ||
197 | udc->enabled | ||
198 | ? (udc->vbus ? "active" : "enabled") | ||
199 | : "disabled", | ||
200 | udc->selfpowered ? "self" : "VBUS", | ||
201 | udc->suspended ? ", suspended" : "", | ||
202 | udc->driver ? udc->driver->driver.name : "(none)"); | ||
203 | |||
204 | /* don't access registers when interface isn't clocked */ | ||
205 | if (!udc->clocked) { | ||
206 | seq_printf(s, "(not clocked)\n"); | ||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | tmp = at91_udp_read(AT91_UDP_FRM_NUM); | ||
211 | seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp, | ||
212 | (tmp & AT91_UDP_FRM_OK) ? " ok" : "", | ||
213 | (tmp & AT91_UDP_FRM_ERR) ? " err" : "", | ||
214 | (tmp & AT91_UDP_NUM)); | ||
215 | |||
216 | tmp = at91_udp_read(AT91_UDP_GLB_STAT); | ||
217 | seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp, | ||
218 | (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "", | ||
219 | (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "", | ||
220 | (tmp & AT91_UDP_ESR) ? " esr" : "", | ||
221 | (tmp & AT91_UDP_CONFG) ? " confg" : "", | ||
222 | (tmp & AT91_UDP_FADDEN) ? " fadden" : ""); | ||
223 | |||
224 | tmp = at91_udp_read(AT91_UDP_FADDR); | ||
225 | seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp, | ||
226 | (tmp & AT91_UDP_FEN) ? " fen" : "", | ||
227 | (tmp & AT91_UDP_FADD)); | ||
228 | |||
229 | proc_irq_show(s, "imr ", at91_udp_read(AT91_UDP_IMR)); | ||
230 | proc_irq_show(s, "isr ", at91_udp_read(AT91_UDP_ISR)); | ||
231 | |||
232 | if (udc->enabled && udc->vbus) { | ||
233 | proc_ep_show(s, &udc->ep[0]); | ||
234 | list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) { | ||
235 | if (ep->desc) | ||
236 | proc_ep_show(s, ep); | ||
237 | } | ||
238 | } | ||
239 | return 0; | ||
240 | } | ||
241 | |||
242 | static int proc_udc_open(struct inode *inode, struct file *file) | ||
243 | { | ||
244 | return single_open(file, proc_udc_show, PDE(inode)->data); | ||
245 | } | ||
246 | |||
247 | static struct file_operations proc_ops = { | ||
248 | .open = proc_udc_open, | ||
249 | .read = seq_read, | ||
250 | .llseek = seq_lseek, | ||
251 | .release = single_release, | ||
252 | }; | ||
253 | |||
254 | static void create_debug_file(struct at91_udc *udc) | ||
255 | { | ||
256 | struct proc_dir_entry *pde; | ||
257 | |||
258 | pde = create_proc_entry (debug_filename, 0, NULL); | ||
259 | udc->pde = pde; | ||
260 | if (pde == NULL) | ||
261 | return; | ||
262 | |||
263 | pde->proc_fops = &proc_ops; | ||
264 | pde->data = udc; | ||
265 | } | ||
266 | |||
267 | static void remove_debug_file(struct at91_udc *udc) | ||
268 | { | ||
269 | if (udc->pde) | ||
270 | remove_proc_entry(debug_filename, NULL); | ||
271 | } | ||
272 | |||
273 | #else | ||
274 | |||
275 | static inline void create_debug_file(struct at91_udc *udc) {} | ||
276 | static inline void remove_debug_file(struct at91_udc *udc) {} | ||
277 | |||
278 | #endif | ||
279 | |||
280 | |||
281 | /*-------------------------------------------------------------------------*/ | ||
282 | |||
283 | static void done(struct at91_ep *ep, struct at91_request *req, int status) | ||
284 | { | ||
285 | unsigned stopped = ep->stopped; | ||
286 | |||
287 | list_del_init(&req->queue); | ||
288 | if (req->req.status == -EINPROGRESS) | ||
289 | req->req.status = status; | ||
290 | else | ||
291 | status = req->req.status; | ||
292 | if (status && status != -ESHUTDOWN) | ||
293 | VDBG("%s done %p, status %d\n", ep->ep.name, req, status); | ||
294 | |||
295 | ep->stopped = 1; | ||
296 | req->req.complete(&ep->ep, &req->req); | ||
297 | ep->stopped = stopped; | ||
298 | |||
299 | /* ep0 is always ready; other endpoints need a non-empty queue */ | ||
300 | if (list_empty(&ep->queue) && ep->int_mask != (1 << 0)) | ||
301 | at91_udp_write(AT91_UDP_IDR, ep->int_mask); | ||
302 | } | ||
303 | |||
304 | /*-------------------------------------------------------------------------*/ | ||
305 | |||
306 | /* bits indicating OUT fifo has data ready */ | ||
307 | #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1) | ||
308 | |||
309 | /* | ||
310 | * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write | ||
311 | * back most of the value you just read (because of side effects, including | ||
312 | * bits that may change after reading and before writing). | ||
313 | * | ||
314 | * Except when changing a specific bit, always write values which: | ||
315 | * - clear SET_FX bits (setting them could change something) | ||
316 | * - set CLR_FX bits (clearing them could change something) | ||
317 | * | ||
318 | * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE | ||
319 | * that shouldn't normally be changed. | ||
320 | */ | ||
321 | #define SET_FX (AT91_UDP_TXPKTRDY) | ||
322 | #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP) | ||
323 | |||
324 | /* pull OUT packet data from the endpoint's fifo */ | ||
325 | static int read_fifo (struct at91_ep *ep, struct at91_request *req) | ||
326 | { | ||
327 | u32 __iomem *creg = ep->creg; | ||
328 | u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0)); | ||
329 | u32 csr; | ||
330 | u8 *buf; | ||
331 | unsigned int count, bufferspace, is_done; | ||
332 | |||
333 | buf = req->req.buf + req->req.actual; | ||
334 | bufferspace = req->req.length - req->req.actual; | ||
335 | |||
336 | /* | ||
337 | * there might be nothing to read if ep_queue() calls us, | ||
338 | * or if we already emptied both pingpong buffers | ||
339 | */ | ||
340 | rescan: | ||
341 | csr = __raw_readl(creg); | ||
342 | if ((csr & RX_DATA_READY) == 0) | ||
343 | return 0; | ||
344 | |||
345 | count = (csr & AT91_UDP_RXBYTECNT) >> 16; | ||
346 | if (count > ep->ep.maxpacket) | ||
347 | count = ep->ep.maxpacket; | ||
348 | if (count > bufferspace) { | ||
349 | DBG("%s buffer overflow\n", ep->ep.name); | ||
350 | req->req.status = -EOVERFLOW; | ||
351 | count = bufferspace; | ||
352 | } | ||
353 | __raw_readsb(dreg, buf, count); | ||
354 | |||
355 | /* release and swap pingpong mem bank */ | ||
356 | csr |= CLR_FX; | ||
357 | if (ep->is_pingpong) { | ||
358 | if (ep->fifo_bank == 0) { | ||
359 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0); | ||
360 | ep->fifo_bank = 1; | ||
361 | } else { | ||
362 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1); | ||
363 | ep->fifo_bank = 0; | ||
364 | } | ||
365 | } else | ||
366 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0); | ||
367 | __raw_writel(csr, creg); | ||
368 | |||
369 | req->req.actual += count; | ||
370 | is_done = (count < ep->ep.maxpacket); | ||
371 | if (count == bufferspace) | ||
372 | is_done = 1; | ||
373 | |||
374 | PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count, | ||
375 | is_done ? " (done)" : ""); | ||
376 | |||
377 | /* | ||
378 | * avoid extra trips through IRQ logic for packets already in | ||
379 | * the fifo ... maybe preventing an extra (expensive) OUT-NAK | ||
380 | */ | ||
381 | if (is_done) | ||
382 | done(ep, req, 0); | ||
383 | else if (ep->is_pingpong) { | ||
384 | bufferspace -= count; | ||
385 | buf += count; | ||
386 | goto rescan; | ||
387 | } | ||
388 | |||
389 | return is_done; | ||
390 | } | ||
391 | |||
392 | /* load fifo for an IN packet */ | ||
393 | static int write_fifo(struct at91_ep *ep, struct at91_request *req) | ||
394 | { | ||
395 | u32 __iomem *creg = ep->creg; | ||
396 | u32 csr = __raw_readl(creg); | ||
397 | u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0)); | ||
398 | unsigned total, count, is_last; | ||
399 | |||
400 | /* | ||
401 | * TODO: allow for writing two packets to the fifo ... that'll | ||
402 | * reduce the amount of IN-NAKing, but probably won't affect | ||
403 | * throughput much. (Unlike preventing OUT-NAKing!) | ||
404 | */ | ||
405 | |||
406 | /* | ||
407 | * If ep_queue() calls us, the queue is empty and possibly in | ||
408 | * odd states like TXCOMP not yet cleared (we do it, saving at | ||
409 | * least one IRQ) or the fifo not yet being free. Those aren't | ||
410 | * issues normally (IRQ handler fast path). | ||
411 | */ | ||
412 | if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) { | ||
413 | if (csr & AT91_UDP_TXCOMP) { | ||
414 | csr |= CLR_FX; | ||
415 | csr &= ~(SET_FX | AT91_UDP_TXCOMP); | ||
416 | __raw_writel(csr, creg); | ||
417 | csr = __raw_readl(creg); | ||
418 | } | ||
419 | if (csr & AT91_UDP_TXPKTRDY) | ||
420 | return 0; | ||
421 | } | ||
422 | |||
423 | total = req->req.length - req->req.actual; | ||
424 | if (ep->ep.maxpacket < total) { | ||
425 | count = ep->ep.maxpacket; | ||
426 | is_last = 0; | ||
427 | } else { | ||
428 | count = total; | ||
429 | is_last = (count < ep->ep.maxpacket) || !req->req.zero; | ||
430 | } | ||
431 | |||
432 | /* | ||
433 | * Write the packet, maybe it's a ZLP. | ||
434 | * | ||
435 | * NOTE: incrementing req->actual before we receive the ACK means | ||
436 | * gadget driver IN bytecounts can be wrong in fault cases. That's | ||
437 | * fixable with PIO drivers like this one (save "count" here, and | ||
438 | * do the increment later on TX irq), but not for most DMA hardware. | ||
439 | * | ||
440 | * So all gadget drivers must accept that potential error. Some | ||
441 | * hardware supports precise fifo status reporting, letting them | ||
442 | * recover when the actual bytecount matters (e.g. for USB Test | ||
443 | * and Measurement Class devices). | ||
444 | */ | ||
445 | __raw_writesb(dreg, req->req.buf + req->req.actual, count); | ||
446 | csr &= ~SET_FX; | ||
447 | csr |= CLR_FX | AT91_UDP_TXPKTRDY; | ||
448 | __raw_writel(csr, creg); | ||
449 | req->req.actual += count; | ||
450 | |||
451 | PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count, | ||
452 | is_last ? " (done)" : ""); | ||
453 | if (is_last) | ||
454 | done(ep, req, 0); | ||
455 | return is_last; | ||
456 | } | ||
457 | |||
458 | static void nuke(struct at91_ep *ep, int status) | ||
459 | { | ||
460 | struct at91_request *req; | ||
461 | |||
462 | // terminer chaque requete dans la queue | ||
463 | ep->stopped = 1; | ||
464 | if (list_empty(&ep->queue)) | ||
465 | return; | ||
466 | |||
467 | VDBG("%s %s\n", __FUNCTION__, ep->ep.name); | ||
468 | while (!list_empty(&ep->queue)) { | ||
469 | req = list_entry(ep->queue.next, struct at91_request, queue); | ||
470 | done(ep, req, status); | ||
471 | } | ||
472 | } | ||
473 | |||
474 | /*-------------------------------------------------------------------------*/ | ||
475 | |||
476 | static int at91_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) | ||
477 | { | ||
478 | struct at91_ep *ep = container_of(_ep, struct at91_ep, ep); | ||
479 | struct at91_udc *dev = ep->udc; | ||
480 | u16 maxpacket; | ||
481 | u32 tmp; | ||
482 | unsigned long flags; | ||
483 | |||
484 | if (!_ep || !ep | ||
485 | || !desc || ep->desc | ||
486 | || _ep->name == ep0name | ||
487 | || desc->bDescriptorType != USB_DT_ENDPOINT | ||
488 | || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0 | ||
489 | || maxpacket > ep->maxpacket) { | ||
490 | DBG("bad ep or descriptor\n"); | ||
491 | return -EINVAL; | ||
492 | } | ||
493 | |||
494 | if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) { | ||
495 | DBG("bogus device state\n"); | ||
496 | return -ESHUTDOWN; | ||
497 | } | ||
498 | |||
499 | tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; | ||
500 | switch (tmp) { | ||
501 | case USB_ENDPOINT_XFER_CONTROL: | ||
502 | DBG("only one control endpoint\n"); | ||
503 | return -EINVAL; | ||
504 | case USB_ENDPOINT_XFER_INT: | ||
505 | if (maxpacket > 64) | ||
506 | goto bogus_max; | ||
507 | break; | ||
508 | case USB_ENDPOINT_XFER_BULK: | ||
509 | switch (maxpacket) { | ||
510 | case 8: | ||
511 | case 16: | ||
512 | case 32: | ||
513 | case 64: | ||
514 | goto ok; | ||
515 | } | ||
516 | bogus_max: | ||
517 | DBG("bogus maxpacket %d\n", maxpacket); | ||
518 | return -EINVAL; | ||
519 | case USB_ENDPOINT_XFER_ISOC: | ||
520 | if (!ep->is_pingpong) { | ||
521 | DBG("iso requires double buffering\n"); | ||
522 | return -EINVAL; | ||
523 | } | ||
524 | break; | ||
525 | } | ||
526 | |||
527 | ok: | ||
528 | local_irq_save(flags); | ||
529 | |||
530 | /* initialize endpoint to match this descriptor */ | ||
531 | ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0; | ||
532 | ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC); | ||
533 | ep->stopped = 0; | ||
534 | if (ep->is_in) | ||
535 | tmp |= 0x04; | ||
536 | tmp <<= 8; | ||
537 | tmp |= AT91_UDP_EPEDS; | ||
538 | __raw_writel(tmp, ep->creg); | ||
539 | |||
540 | ep->desc = desc; | ||
541 | ep->ep.maxpacket = maxpacket; | ||
542 | |||
543 | /* | ||
544 | * reset/init endpoint fifo. NOTE: leaves fifo_bank alone, | ||
545 | * since endpoint resets don't reset hw pingpong state. | ||
546 | */ | ||
547 | at91_udp_write(AT91_UDP_RST_EP, ep->int_mask); | ||
548 | at91_udp_write(AT91_UDP_RST_EP, 0); | ||
549 | |||
550 | local_irq_restore(flags); | ||
551 | return 0; | ||
552 | } | ||
553 | |||
554 | static int at91_ep_disable (struct usb_ep * _ep) | ||
555 | { | ||
556 | struct at91_ep *ep = container_of(_ep, struct at91_ep, ep); | ||
557 | unsigned long flags; | ||
558 | |||
559 | if (ep == &ep->udc->ep[0]) | ||
560 | return -EINVAL; | ||
561 | |||
562 | local_irq_save(flags); | ||
563 | |||
564 | nuke(ep, -ESHUTDOWN); | ||
565 | |||
566 | /* restore the endpoint's pristine config */ | ||
567 | ep->desc = NULL; | ||
568 | ep->ep.maxpacket = ep->maxpacket; | ||
569 | |||
570 | /* reset fifos and endpoint */ | ||
571 | if (ep->udc->clocked) { | ||
572 | at91_udp_write(AT91_UDP_RST_EP, ep->int_mask); | ||
573 | at91_udp_write(AT91_UDP_RST_EP, 0); | ||
574 | __raw_writel(0, ep->creg); | ||
575 | } | ||
576 | |||
577 | local_irq_restore(flags); | ||
578 | return 0; | ||
579 | } | ||
580 | |||
581 | /* | ||
582 | * this is a PIO-only driver, so there's nothing | ||
583 | * interesting for request or buffer allocation. | ||
584 | */ | ||
585 | |||
586 | static struct usb_request *at91_ep_alloc_request (struct usb_ep *_ep, unsigned int gfp_flags) | ||
587 | { | ||
588 | struct at91_request *req; | ||
589 | |||
590 | req = kcalloc(1, sizeof (struct at91_request), SLAB_KERNEL); | ||
591 | if (!req) | ||
592 | return NULL; | ||
593 | |||
594 | INIT_LIST_HEAD(&req->queue); | ||
595 | return &req->req; | ||
596 | } | ||
597 | |||
598 | static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req) | ||
599 | { | ||
600 | struct at91_request *req; | ||
601 | |||
602 | req = container_of(_req, struct at91_request, req); | ||
603 | BUG_ON(!list_empty(&req->queue)); | ||
604 | kfree(req); | ||
605 | } | ||
606 | |||
607 | static void *at91_ep_alloc_buffer( | ||
608 | struct usb_ep *_ep, | ||
609 | unsigned bytes, | ||
610 | dma_addr_t *dma, | ||
611 | gfp_t gfp_flags) | ||
612 | { | ||
613 | *dma = ~0; | ||
614 | return kmalloc(bytes, gfp_flags); | ||
615 | } | ||
616 | |||
617 | static void at91_ep_free_buffer( | ||
618 | struct usb_ep *ep, | ||
619 | void *buf, | ||
620 | dma_addr_t dma, | ||
621 | unsigned bytes) | ||
622 | { | ||
623 | kfree(buf); | ||
624 | } | ||
625 | |||
626 | static int at91_ep_queue(struct usb_ep *_ep, | ||
627 | struct usb_request *_req, gfp_t gfp_flags) | ||
628 | { | ||
629 | struct at91_request *req; | ||
630 | struct at91_ep *ep; | ||
631 | struct at91_udc *dev; | ||
632 | int status; | ||
633 | unsigned long flags; | ||
634 | |||
635 | req = container_of(_req, struct at91_request, req); | ||
636 | ep = container_of(_ep, struct at91_ep, ep); | ||
637 | |||
638 | if (!_req || !_req->complete | ||
639 | || !_req->buf || !list_empty(&req->queue)) { | ||
640 | DBG("invalid request\n"); | ||
641 | return -EINVAL; | ||
642 | } | ||
643 | |||
644 | if (!_ep || (!ep->desc && ep->ep.name != ep0name)) { | ||
645 | DBG("invalid ep\n"); | ||
646 | return -EINVAL; | ||
647 | } | ||
648 | |||
649 | dev = ep->udc; | ||
650 | |||
651 | if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) { | ||
652 | DBG("invalid device\n"); | ||
653 | return -EINVAL; | ||
654 | } | ||
655 | |||
656 | _req->status = -EINPROGRESS; | ||
657 | _req->actual = 0; | ||
658 | |||
659 | local_irq_save(flags); | ||
660 | |||
661 | /* try to kickstart any empty and idle queue */ | ||
662 | if (list_empty(&ep->queue) && !ep->stopped) { | ||
663 | int is_ep0; | ||
664 | |||
665 | /* | ||
666 | * If this control request has a non-empty DATA stage, this | ||
667 | * will start that stage. It works just like a non-control | ||
668 | * request (until the status stage starts, maybe early). | ||
669 | * | ||
670 | * If the data stage is empty, then this starts a successful | ||
671 | * IN/STATUS stage. (Unsuccessful ones use set_halt.) | ||
672 | */ | ||
673 | is_ep0 = (ep->ep.name == ep0name); | ||
674 | if (is_ep0) { | ||
675 | u32 tmp; | ||
676 | |||
677 | if (!dev->req_pending) { | ||
678 | status = -EINVAL; | ||
679 | goto done; | ||
680 | } | ||
681 | |||
682 | /* | ||
683 | * defer changing CONFG until after the gadget driver | ||
684 | * reconfigures the endpoints. | ||
685 | */ | ||
686 | if (dev->wait_for_config_ack) { | ||
687 | tmp = at91_udp_read(AT91_UDP_GLB_STAT); | ||
688 | tmp ^= AT91_UDP_CONFG; | ||
689 | VDBG("toggle config\n"); | ||
690 | at91_udp_write(AT91_UDP_GLB_STAT, tmp); | ||
691 | } | ||
692 | if (req->req.length == 0) { | ||
693 | ep0_in_status: | ||
694 | PACKET("ep0 in/status\n"); | ||
695 | status = 0; | ||
696 | tmp = __raw_readl(ep->creg); | ||
697 | tmp &= ~SET_FX; | ||
698 | tmp |= CLR_FX | AT91_UDP_TXPKTRDY; | ||
699 | __raw_writel(tmp, ep->creg); | ||
700 | dev->req_pending = 0; | ||
701 | goto done; | ||
702 | } | ||
703 | } | ||
704 | |||
705 | if (ep->is_in) | ||
706 | status = write_fifo(ep, req); | ||
707 | else { | ||
708 | status = read_fifo(ep, req); | ||
709 | |||
710 | /* IN/STATUS stage is otherwise triggered by irq */ | ||
711 | if (status && is_ep0) | ||
712 | goto ep0_in_status; | ||
713 | } | ||
714 | } else | ||
715 | status = 0; | ||
716 | |||
717 | if (req && !status) { | ||
718 | list_add_tail (&req->queue, &ep->queue); | ||
719 | at91_udp_write(AT91_UDP_IER, ep->int_mask); | ||
720 | } | ||
721 | done: | ||
722 | local_irq_restore(flags); | ||
723 | return (status < 0) ? status : 0; | ||
724 | } | ||
725 | |||
726 | static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) | ||
727 | { | ||
728 | struct at91_ep *ep; | ||
729 | struct at91_request *req; | ||
730 | |||
731 | ep = container_of(_ep, struct at91_ep, ep); | ||
732 | if (!_ep || ep->ep.name == ep0name) | ||
733 | return -EINVAL; | ||
734 | |||
735 | /* make sure it's actually queued on this endpoint */ | ||
736 | list_for_each_entry (req, &ep->queue, queue) { | ||
737 | if (&req->req == _req) | ||
738 | break; | ||
739 | } | ||
740 | if (&req->req != _req) | ||
741 | return -EINVAL; | ||
742 | |||
743 | done(ep, req, -ECONNRESET); | ||
744 | return 0; | ||
745 | } | ||
746 | |||
747 | static int at91_ep_set_halt(struct usb_ep *_ep, int value) | ||
748 | { | ||
749 | struct at91_ep *ep = container_of(_ep, struct at91_ep, ep); | ||
750 | u32 __iomem *creg; | ||
751 | u32 csr; | ||
752 | unsigned long flags; | ||
753 | int status = 0; | ||
754 | |||
755 | if (!_ep || ep->is_iso || !ep->udc->clocked) | ||
756 | return -EINVAL; | ||
757 | |||
758 | creg = ep->creg; | ||
759 | local_irq_save(flags); | ||
760 | |||
761 | csr = __raw_readl(creg); | ||
762 | |||
763 | /* | ||
764 | * fail with still-busy IN endpoints, ensuring correct sequencing | ||
765 | * of data tx then stall. note that the fifo rx bytecount isn't | ||
766 | * completely accurate as a tx bytecount. | ||
767 | */ | ||
768 | if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0)) | ||
769 | status = -EAGAIN; | ||
770 | else { | ||
771 | csr |= CLR_FX; | ||
772 | csr &= ~SET_FX; | ||
773 | if (value) { | ||
774 | csr |= AT91_UDP_FORCESTALL; | ||
775 | VDBG("halt %s\n", ep->ep.name); | ||
776 | } else { | ||
777 | at91_udp_write(AT91_UDP_RST_EP, ep->int_mask); | ||
778 | at91_udp_write(AT91_UDP_RST_EP, 0); | ||
779 | csr &= ~AT91_UDP_FORCESTALL; | ||
780 | } | ||
781 | __raw_writel(csr, creg); | ||
782 | } | ||
783 | |||
784 | local_irq_restore(flags); | ||
785 | return status; | ||
786 | } | ||
787 | |||
788 | static struct usb_ep_ops at91_ep_ops = { | ||
789 | .enable = at91_ep_enable, | ||
790 | .disable = at91_ep_disable, | ||
791 | .alloc_request = at91_ep_alloc_request, | ||
792 | .free_request = at91_ep_free_request, | ||
793 | .alloc_buffer = at91_ep_alloc_buffer, | ||
794 | .free_buffer = at91_ep_free_buffer, | ||
795 | .queue = at91_ep_queue, | ||
796 | .dequeue = at91_ep_dequeue, | ||
797 | .set_halt = at91_ep_set_halt, | ||
798 | // there's only imprecise fifo status reporting | ||
799 | }; | ||
800 | |||
801 | /*-------------------------------------------------------------------------*/ | ||
802 | |||
803 | static int at91_get_frame(struct usb_gadget *gadget) | ||
804 | { | ||
805 | if (!to_udc(gadget)->clocked) | ||
806 | return -EINVAL; | ||
807 | return at91_udp_read(AT91_UDP_FRM_NUM) & AT91_UDP_NUM; | ||
808 | } | ||
809 | |||
810 | static int at91_wakeup(struct usb_gadget *gadget) | ||
811 | { | ||
812 | struct at91_udc *udc = to_udc(gadget); | ||
813 | u32 glbstate; | ||
814 | int status = -EINVAL; | ||
815 | unsigned long flags; | ||
816 | |||
817 | DBG("%s\n", __FUNCTION__ ); | ||
818 | local_irq_save(flags); | ||
819 | |||
820 | if (!udc->clocked || !udc->suspended) | ||
821 | goto done; | ||
822 | |||
823 | /* NOTE: some "early versions" handle ESR differently ... */ | ||
824 | |||
825 | glbstate = at91_udp_read(AT91_UDP_GLB_STAT); | ||
826 | if (!(glbstate & AT91_UDP_ESR)) | ||
827 | goto done; | ||
828 | glbstate |= AT91_UDP_ESR; | ||
829 | at91_udp_write(AT91_UDP_GLB_STAT, glbstate); | ||
830 | |||
831 | done: | ||
832 | local_irq_restore(flags); | ||
833 | return status; | ||
834 | } | ||
835 | |||
836 | /* reinit == restore inital software state */ | ||
837 | static void udc_reinit(struct at91_udc *udc) | ||
838 | { | ||
839 | u32 i; | ||
840 | |||
841 | INIT_LIST_HEAD(&udc->gadget.ep_list); | ||
842 | INIT_LIST_HEAD(&udc->gadget.ep0->ep_list); | ||
843 | |||
844 | for (i = 0; i < NUM_ENDPOINTS; i++) { | ||
845 | struct at91_ep *ep = &udc->ep[i]; | ||
846 | |||
847 | if (i != 0) | ||
848 | list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); | ||
849 | ep->desc = NULL; | ||
850 | ep->stopped = 0; | ||
851 | ep->fifo_bank = 0; | ||
852 | ep->ep.maxpacket = ep->maxpacket; | ||
853 | // initialiser une queue par endpoint | ||
854 | INIT_LIST_HEAD(&ep->queue); | ||
855 | } | ||
856 | } | ||
857 | |||
858 | static void stop_activity(struct at91_udc *udc) | ||
859 | { | ||
860 | struct usb_gadget_driver *driver = udc->driver; | ||
861 | int i; | ||
862 | |||
863 | if (udc->gadget.speed == USB_SPEED_UNKNOWN) | ||
864 | driver = NULL; | ||
865 | udc->gadget.speed = USB_SPEED_UNKNOWN; | ||
866 | |||
867 | for (i = 0; i < NUM_ENDPOINTS; i++) { | ||
868 | struct at91_ep *ep = &udc->ep[i]; | ||
869 | ep->stopped = 1; | ||
870 | nuke(ep, -ESHUTDOWN); | ||
871 | } | ||
872 | if (driver) | ||
873 | driver->disconnect(&udc->gadget); | ||
874 | |||
875 | udc_reinit(udc); | ||
876 | } | ||
877 | |||
878 | static void clk_on(struct at91_udc *udc) | ||
879 | { | ||
880 | if (udc->clocked) | ||
881 | return; | ||
882 | udc->clocked = 1; | ||
883 | clk_enable(udc->iclk); | ||
884 | clk_enable(udc->fclk); | ||
885 | } | ||
886 | |||
887 | static void clk_off(struct at91_udc *udc) | ||
888 | { | ||
889 | if (!udc->clocked) | ||
890 | return; | ||
891 | udc->clocked = 0; | ||
892 | udc->gadget.speed = USB_SPEED_UNKNOWN; | ||
893 | clk_disable(udc->iclk); | ||
894 | clk_disable(udc->fclk); | ||
895 | } | ||
896 | |||
897 | /* | ||
898 | * activate/deactivate link with host; minimize power usage for | ||
899 | * inactive links by cutting clocks and transceiver power. | ||
900 | */ | ||
901 | static void pullup(struct at91_udc *udc, int is_on) | ||
902 | { | ||
903 | if (!udc->enabled || !udc->vbus) | ||
904 | is_on = 0; | ||
905 | DBG("%sactive\n", is_on ? "" : "in"); | ||
906 | if (is_on) { | ||
907 | clk_on(udc); | ||
908 | at91_udp_write(AT91_UDP_TXVC, 0); | ||
909 | at91_set_gpio_value(udc->board.pullup_pin, 1); | ||
910 | } else { | ||
911 | stop_activity(udc); | ||
912 | at91_udp_write(AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS); | ||
913 | at91_set_gpio_value(udc->board.pullup_pin, 0); | ||
914 | clk_off(udc); | ||
915 | |||
916 | // REVISIT: with transceiver disabled, will D- float | ||
917 | // so that a host would falsely detect a device? | ||
918 | } | ||
919 | } | ||
920 | |||
921 | /* vbus is here! turn everything on that's ready */ | ||
922 | static int at91_vbus_session(struct usb_gadget *gadget, int is_active) | ||
923 | { | ||
924 | struct at91_udc *udc = to_udc(gadget); | ||
925 | unsigned long flags; | ||
926 | |||
927 | // VDBG("vbus %s\n", is_active ? "on" : "off"); | ||
928 | local_irq_save(flags); | ||
929 | udc->vbus = (is_active != 0); | ||
930 | pullup(udc, is_active); | ||
931 | local_irq_restore(flags); | ||
932 | return 0; | ||
933 | } | ||
934 | |||
935 | static int at91_pullup(struct usb_gadget *gadget, int is_on) | ||
936 | { | ||
937 | struct at91_udc *udc = to_udc(gadget); | ||
938 | unsigned long flags; | ||
939 | |||
940 | local_irq_save(flags); | ||
941 | udc->enabled = is_on = !!is_on; | ||
942 | pullup(udc, is_on); | ||
943 | local_irq_restore(flags); | ||
944 | return 0; | ||
945 | } | ||
946 | |||
947 | static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on) | ||
948 | { | ||
949 | struct at91_udc *udc = to_udc(gadget); | ||
950 | unsigned long flags; | ||
951 | |||
952 | local_irq_save(flags); | ||
953 | udc->selfpowered = (is_on != 0); | ||
954 | local_irq_restore(flags); | ||
955 | return 0; | ||
956 | } | ||
957 | |||
958 | static const struct usb_gadget_ops at91_udc_ops = { | ||
959 | .get_frame = at91_get_frame, | ||
960 | .wakeup = at91_wakeup, | ||
961 | .set_selfpowered = at91_set_selfpowered, | ||
962 | .vbus_session = at91_vbus_session, | ||
963 | .pullup = at91_pullup, | ||
964 | |||
965 | /* | ||
966 | * VBUS-powered devices may also also want to support bigger | ||
967 | * power budgets after an appropriate SET_CONFIGURATION. | ||
968 | */ | ||
969 | // .vbus_power = at91_vbus_power, | ||
970 | }; | ||
971 | |||
972 | /*-------------------------------------------------------------------------*/ | ||
973 | |||
974 | static int handle_ep(struct at91_ep *ep) | ||
975 | { | ||
976 | struct at91_request *req; | ||
977 | u32 __iomem *creg = ep->creg; | ||
978 | u32 csr = __raw_readl(creg); | ||
979 | |||
980 | if (!list_empty(&ep->queue)) | ||
981 | req = list_entry(ep->queue.next, | ||
982 | struct at91_request, queue); | ||
983 | else | ||
984 | req = NULL; | ||
985 | |||
986 | if (ep->is_in) { | ||
987 | if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) { | ||
988 | csr |= CLR_FX; | ||
989 | csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP); | ||
990 | __raw_writel(csr, creg); | ||
991 | } | ||
992 | if (req) | ||
993 | return write_fifo(ep, req); | ||
994 | |||
995 | } else { | ||
996 | if (csr & AT91_UDP_STALLSENT) { | ||
997 | /* STALLSENT bit == ISOERR */ | ||
998 | if (ep->is_iso && req) | ||
999 | req->req.status = -EILSEQ; | ||
1000 | csr |= CLR_FX; | ||
1001 | csr &= ~(SET_FX | AT91_UDP_STALLSENT); | ||
1002 | __raw_writel(csr, creg); | ||
1003 | csr = __raw_readl(creg); | ||
1004 | } | ||
1005 | if (req && (csr & RX_DATA_READY)) | ||
1006 | return read_fifo(ep, req); | ||
1007 | } | ||
1008 | return 0; | ||
1009 | } | ||
1010 | |||
1011 | union setup { | ||
1012 | u8 raw[8]; | ||
1013 | struct usb_ctrlrequest r; | ||
1014 | }; | ||
1015 | |||
1016 | static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr) | ||
1017 | { | ||
1018 | u32 __iomem *creg = ep->creg; | ||
1019 | u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0)); | ||
1020 | unsigned rxcount, i = 0; | ||
1021 | u32 tmp; | ||
1022 | union setup pkt; | ||
1023 | int status = 0; | ||
1024 | |||
1025 | /* read and ack SETUP; hard-fail for bogus packets */ | ||
1026 | rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16; | ||
1027 | if (likely(rxcount == 8)) { | ||
1028 | while (rxcount--) | ||
1029 | pkt.raw[i++] = __raw_readb(dreg); | ||
1030 | if (pkt.r.bRequestType & USB_DIR_IN) { | ||
1031 | csr |= AT91_UDP_DIR; | ||
1032 | ep->is_in = 1; | ||
1033 | } else { | ||
1034 | csr &= ~AT91_UDP_DIR; | ||
1035 | ep->is_in = 0; | ||
1036 | } | ||
1037 | } else { | ||
1038 | // REVISIT this happens sometimes under load; why?? | ||
1039 | ERR("SETUP len %d, csr %08x\n", rxcount, csr); | ||
1040 | status = -EINVAL; | ||
1041 | } | ||
1042 | csr |= CLR_FX; | ||
1043 | csr &= ~(SET_FX | AT91_UDP_RXSETUP); | ||
1044 | __raw_writel(csr, creg); | ||
1045 | udc->wait_for_addr_ack = 0; | ||
1046 | udc->wait_for_config_ack = 0; | ||
1047 | ep->stopped = 0; | ||
1048 | if (unlikely(status != 0)) | ||
1049 | goto stall; | ||
1050 | |||
1051 | #define w_index le16_to_cpu(pkt.r.wIndex) | ||
1052 | #define w_value le16_to_cpu(pkt.r.wValue) | ||
1053 | #define w_length le16_to_cpu(pkt.r.wLength) | ||
1054 | |||
1055 | VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n", | ||
1056 | pkt.r.bRequestType, pkt.r.bRequest, | ||
1057 | w_value, w_index, w_length); | ||
1058 | |||
1059 | /* | ||
1060 | * A few standard requests get handled here, ones that touch | ||
1061 | * hardware ... notably for device and endpoint features. | ||
1062 | */ | ||
1063 | udc->req_pending = 1; | ||
1064 | csr = __raw_readl(creg); | ||
1065 | csr |= CLR_FX; | ||
1066 | csr &= ~SET_FX; | ||
1067 | switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) { | ||
1068 | |||
1069 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | ||
1070 | | USB_REQ_SET_ADDRESS: | ||
1071 | __raw_writel(csr | AT91_UDP_TXPKTRDY, creg); | ||
1072 | udc->addr = w_value; | ||
1073 | udc->wait_for_addr_ack = 1; | ||
1074 | udc->req_pending = 0; | ||
1075 | /* FADDR is set later, when we ack host STATUS */ | ||
1076 | return; | ||
1077 | |||
1078 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | ||
1079 | | USB_REQ_SET_CONFIGURATION: | ||
1080 | tmp = at91_udp_read(AT91_UDP_GLB_STAT) & AT91_UDP_CONFG; | ||
1081 | if (pkt.r.wValue) | ||
1082 | udc->wait_for_config_ack = (tmp == 0); | ||
1083 | else | ||
1084 | udc->wait_for_config_ack = (tmp != 0); | ||
1085 | if (udc->wait_for_config_ack) | ||
1086 | VDBG("wait for config\n"); | ||
1087 | /* CONFG is toggled later, if gadget driver succeeds */ | ||
1088 | break; | ||
1089 | |||
1090 | /* | ||
1091 | * Hosts may set or clear remote wakeup status, and | ||
1092 | * devices may report they're VBUS powered. | ||
1093 | */ | ||
1094 | case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | ||
1095 | | USB_REQ_GET_STATUS: | ||
1096 | tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED); | ||
1097 | if (at91_udp_read(AT91_UDP_GLB_STAT) & AT91_UDP_ESR) | ||
1098 | tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP); | ||
1099 | PACKET("get device status\n"); | ||
1100 | __raw_writeb(tmp, dreg); | ||
1101 | __raw_writeb(0, dreg); | ||
1102 | goto write_in; | ||
1103 | /* then STATUS starts later, automatically */ | ||
1104 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | ||
1105 | | USB_REQ_SET_FEATURE: | ||
1106 | if (w_value != USB_DEVICE_REMOTE_WAKEUP) | ||
1107 | goto stall; | ||
1108 | tmp = at91_udp_read(AT91_UDP_GLB_STAT); | ||
1109 | tmp |= AT91_UDP_ESR; | ||
1110 | at91_udp_write(AT91_UDP_GLB_STAT, tmp); | ||
1111 | goto succeed; | ||
1112 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | ||
1113 | | USB_REQ_CLEAR_FEATURE: | ||
1114 | if (w_value != USB_DEVICE_REMOTE_WAKEUP) | ||
1115 | goto stall; | ||
1116 | tmp = at91_udp_read(AT91_UDP_GLB_STAT); | ||
1117 | tmp &= ~AT91_UDP_ESR; | ||
1118 | at91_udp_write(AT91_UDP_GLB_STAT, tmp); | ||
1119 | goto succeed; | ||
1120 | |||
1121 | /* | ||
1122 | * Interfaces have no feature settings; this is pretty useless. | ||
1123 | * we won't even insist the interface exists... | ||
1124 | */ | ||
1125 | case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8) | ||
1126 | | USB_REQ_GET_STATUS: | ||
1127 | PACKET("get interface status\n"); | ||
1128 | __raw_writeb(0, dreg); | ||
1129 | __raw_writeb(0, dreg); | ||
1130 | goto write_in; | ||
1131 | /* then STATUS starts later, automatically */ | ||
1132 | case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8) | ||
1133 | | USB_REQ_SET_FEATURE: | ||
1134 | case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8) | ||
1135 | | USB_REQ_CLEAR_FEATURE: | ||
1136 | goto stall; | ||
1137 | |||
1138 | /* | ||
1139 | * Hosts may clear bulk/intr endpoint halt after the gadget | ||
1140 | * driver sets it (not widely used); or set it (for testing) | ||
1141 | */ | ||
1142 | case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8) | ||
1143 | | USB_REQ_GET_STATUS: | ||
1144 | tmp = w_index & USB_ENDPOINT_NUMBER_MASK; | ||
1145 | ep = &udc->ep[tmp]; | ||
1146 | if (tmp > NUM_ENDPOINTS || (tmp && !ep->desc)) | ||
1147 | goto stall; | ||
1148 | |||
1149 | if (tmp) { | ||
1150 | if ((w_index & USB_DIR_IN)) { | ||
1151 | if (!ep->is_in) | ||
1152 | goto stall; | ||
1153 | } else if (ep->is_in) | ||
1154 | goto stall; | ||
1155 | } | ||
1156 | PACKET("get %s status\n", ep->ep.name); | ||
1157 | if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL) | ||
1158 | tmp = (1 << USB_ENDPOINT_HALT); | ||
1159 | else | ||
1160 | tmp = 0; | ||
1161 | __raw_writeb(tmp, dreg); | ||
1162 | __raw_writeb(0, dreg); | ||
1163 | goto write_in; | ||
1164 | /* then STATUS starts later, automatically */ | ||
1165 | case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8) | ||
1166 | | USB_REQ_SET_FEATURE: | ||
1167 | tmp = w_index & USB_ENDPOINT_NUMBER_MASK; | ||
1168 | ep = &udc->ep[tmp]; | ||
1169 | if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS) | ||
1170 | goto stall; | ||
1171 | if (!ep->desc || ep->is_iso) | ||
1172 | goto stall; | ||
1173 | if ((w_index & USB_DIR_IN)) { | ||
1174 | if (!ep->is_in) | ||
1175 | goto stall; | ||
1176 | } else if (ep->is_in) | ||
1177 | goto stall; | ||
1178 | |||
1179 | tmp = __raw_readl(ep->creg); | ||
1180 | tmp &= ~SET_FX; | ||
1181 | tmp |= CLR_FX | AT91_UDP_FORCESTALL; | ||
1182 | __raw_writel(tmp, ep->creg); | ||
1183 | goto succeed; | ||
1184 | case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8) | ||
1185 | | USB_REQ_CLEAR_FEATURE: | ||
1186 | tmp = w_index & USB_ENDPOINT_NUMBER_MASK; | ||
1187 | ep = &udc->ep[tmp]; | ||
1188 | if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS) | ||
1189 | goto stall; | ||
1190 | if (tmp == 0) | ||
1191 | goto succeed; | ||
1192 | if (!ep->desc || ep->is_iso) | ||
1193 | goto stall; | ||
1194 | if ((w_index & USB_DIR_IN)) { | ||
1195 | if (!ep->is_in) | ||
1196 | goto stall; | ||
1197 | } else if (ep->is_in) | ||
1198 | goto stall; | ||
1199 | |||
1200 | at91_udp_write(AT91_UDP_RST_EP, ep->int_mask); | ||
1201 | at91_udp_write(AT91_UDP_RST_EP, 0); | ||
1202 | tmp = __raw_readl(ep->creg); | ||
1203 | tmp |= CLR_FX; | ||
1204 | tmp &= ~(SET_FX | AT91_UDP_FORCESTALL); | ||
1205 | __raw_writel(tmp, ep->creg); | ||
1206 | if (!list_empty(&ep->queue)) | ||
1207 | handle_ep(ep); | ||
1208 | goto succeed; | ||
1209 | } | ||
1210 | |||
1211 | #undef w_value | ||
1212 | #undef w_index | ||
1213 | #undef w_length | ||
1214 | |||
1215 | /* pass request up to the gadget driver */ | ||
1216 | status = udc->driver->setup(&udc->gadget, &pkt.r); | ||
1217 | if (status < 0) { | ||
1218 | stall: | ||
1219 | VDBG("req %02x.%02x protocol STALL; stat %d\n", | ||
1220 | pkt.r.bRequestType, pkt.r.bRequest, status); | ||
1221 | csr |= AT91_UDP_FORCESTALL; | ||
1222 | __raw_writel(csr, creg); | ||
1223 | udc->req_pending = 0; | ||
1224 | } | ||
1225 | return; | ||
1226 | |||
1227 | succeed: | ||
1228 | /* immediate successful (IN) STATUS after zero length DATA */ | ||
1229 | PACKET("ep0 in/status\n"); | ||
1230 | write_in: | ||
1231 | csr |= AT91_UDP_TXPKTRDY; | ||
1232 | __raw_writel(csr, creg); | ||
1233 | udc->req_pending = 0; | ||
1234 | return; | ||
1235 | } | ||
1236 | |||
1237 | static void handle_ep0(struct at91_udc *udc) | ||
1238 | { | ||
1239 | struct at91_ep *ep0 = &udc->ep[0]; | ||
1240 | u32 __iomem *creg = ep0->creg; | ||
1241 | u32 csr = __raw_readl(creg); | ||
1242 | struct at91_request *req; | ||
1243 | |||
1244 | if (unlikely(csr & AT91_UDP_STALLSENT)) { | ||
1245 | nuke(ep0, -EPROTO); | ||
1246 | udc->req_pending = 0; | ||
1247 | csr |= CLR_FX; | ||
1248 | csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL); | ||
1249 | __raw_writel(csr, creg); | ||
1250 | VDBG("ep0 stalled\n"); | ||
1251 | csr = __raw_readl(creg); | ||
1252 | } | ||
1253 | if (csr & AT91_UDP_RXSETUP) { | ||
1254 | nuke(ep0, 0); | ||
1255 | udc->req_pending = 0; | ||
1256 | handle_setup(udc, ep0, csr); | ||
1257 | return; | ||
1258 | } | ||
1259 | |||
1260 | if (list_empty(&ep0->queue)) | ||
1261 | req = NULL; | ||
1262 | else | ||
1263 | req = list_entry(ep0->queue.next, struct at91_request, queue); | ||
1264 | |||
1265 | /* host ACKed an IN packet that we sent */ | ||
1266 | if (csr & AT91_UDP_TXCOMP) { | ||
1267 | csr |= CLR_FX; | ||
1268 | csr &= ~(SET_FX | AT91_UDP_TXCOMP); | ||
1269 | |||
1270 | /* write more IN DATA? */ | ||
1271 | if (req && ep0->is_in) { | ||
1272 | if (handle_ep(ep0)) | ||
1273 | udc->req_pending = 0; | ||
1274 | |||
1275 | /* | ||
1276 | * Ack after: | ||
1277 | * - last IN DATA packet (including GET_STATUS) | ||
1278 | * - IN/STATUS for OUT DATA | ||
1279 | * - IN/STATUS for any zero-length DATA stage | ||
1280 | * except for the IN DATA case, the host should send | ||
1281 | * an OUT status later, which we'll ack. | ||
1282 | */ | ||
1283 | } else { | ||
1284 | udc->req_pending = 0; | ||
1285 | __raw_writel(csr, creg); | ||
1286 | |||
1287 | /* | ||
1288 | * SET_ADDRESS takes effect only after the STATUS | ||
1289 | * (to the original address) gets acked. | ||
1290 | */ | ||
1291 | if (udc->wait_for_addr_ack) { | ||
1292 | u32 tmp; | ||
1293 | |||
1294 | at91_udp_write(AT91_UDP_FADDR, AT91_UDP_FEN | udc->addr); | ||
1295 | tmp = at91_udp_read(AT91_UDP_GLB_STAT); | ||
1296 | tmp &= ~AT91_UDP_FADDEN; | ||
1297 | if (udc->addr) | ||
1298 | tmp |= AT91_UDP_FADDEN; | ||
1299 | at91_udp_write(AT91_UDP_GLB_STAT, tmp); | ||
1300 | |||
1301 | udc->wait_for_addr_ack = 0; | ||
1302 | VDBG("address %d\n", udc->addr); | ||
1303 | } | ||
1304 | } | ||
1305 | } | ||
1306 | |||
1307 | /* OUT packet arrived ... */ | ||
1308 | else if (csr & AT91_UDP_RX_DATA_BK0) { | ||
1309 | csr |= CLR_FX; | ||
1310 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0); | ||
1311 | |||
1312 | /* OUT DATA stage */ | ||
1313 | if (!ep0->is_in) { | ||
1314 | if (req) { | ||
1315 | if (handle_ep(ep0)) { | ||
1316 | /* send IN/STATUS */ | ||
1317 | PACKET("ep0 in/status\n"); | ||
1318 | csr = __raw_readl(creg); | ||
1319 | csr &= ~SET_FX; | ||
1320 | csr |= CLR_FX | AT91_UDP_TXPKTRDY; | ||
1321 | __raw_writel(csr, creg); | ||
1322 | udc->req_pending = 0; | ||
1323 | } | ||
1324 | } else if (udc->req_pending) { | ||
1325 | /* | ||
1326 | * AT91 hardware has a hard time with this | ||
1327 | * "deferred response" mode for control-OUT | ||
1328 | * transfers. (For control-IN it's fine.) | ||
1329 | * | ||
1330 | * The normal solution leaves OUT data in the | ||
1331 | * fifo until the gadget driver is ready. | ||
1332 | * We couldn't do that here without disabling | ||
1333 | * the IRQ that tells about SETUP packets, | ||
1334 | * e.g. when the host gets impatient... | ||
1335 | * | ||
1336 | * Working around it by copying into a buffer | ||
1337 | * would almost be a non-deferred response, | ||
1338 | * except that it wouldn't permit reliable | ||
1339 | * stalling of the request. Instead, demand | ||
1340 | * that gadget drivers not use this mode. | ||
1341 | */ | ||
1342 | DBG("no control-OUT deferred responses!\n"); | ||
1343 | __raw_writel(csr | AT91_UDP_FORCESTALL, creg); | ||
1344 | udc->req_pending = 0; | ||
1345 | } | ||
1346 | |||
1347 | /* STATUS stage for control-IN; ack. */ | ||
1348 | } else { | ||
1349 | PACKET("ep0 out/status ACK\n"); | ||
1350 | __raw_writel(csr, creg); | ||
1351 | |||
1352 | /* "early" status stage */ | ||
1353 | if (req) | ||
1354 | done(ep0, req, 0); | ||
1355 | } | ||
1356 | } | ||
1357 | } | ||
1358 | |||
1359 | static irqreturn_t at91_udc_irq (int irq, void *_udc, struct pt_regs *r) | ||
1360 | { | ||
1361 | struct at91_udc *udc = _udc; | ||
1362 | u32 rescans = 5; | ||
1363 | |||
1364 | while (rescans--) { | ||
1365 | u32 status = at91_udp_read(AT91_UDP_ISR); | ||
1366 | |||
1367 | status &= at91_udp_read(AT91_UDP_IMR); | ||
1368 | if (!status) | ||
1369 | break; | ||
1370 | |||
1371 | /* USB reset irq: not maskable */ | ||
1372 | if (status & AT91_UDP_ENDBUSRES) { | ||
1373 | at91_udp_write(AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS); | ||
1374 | at91_udp_write(AT91_UDP_IER, MINIMUS_INTERRUPTUS); | ||
1375 | /* Atmel code clears this irq twice */ | ||
1376 | at91_udp_write(AT91_UDP_ICR, AT91_UDP_ENDBUSRES); | ||
1377 | at91_udp_write(AT91_UDP_ICR, AT91_UDP_ENDBUSRES); | ||
1378 | VDBG("end bus reset\n"); | ||
1379 | udc->addr = 0; | ||
1380 | stop_activity(udc); | ||
1381 | |||
1382 | /* enable ep0 */ | ||
1383 | at91_udp_write(AT91_UDP_CSR(0), AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL); | ||
1384 | udc->gadget.speed = USB_SPEED_FULL; | ||
1385 | udc->suspended = 0; | ||
1386 | at91_udp_write(AT91_UDP_IER, AT91_UDP_EP(0)); | ||
1387 | |||
1388 | /* | ||
1389 | * NOTE: this driver keeps clocks off unless the | ||
1390 | * USB host is present. That saves power, and also | ||
1391 | * eliminates IRQs (reset, resume, suspend) that can | ||
1392 | * otherwise flood from the controller. If your | ||
1393 | * board doesn't support VBUS detection, suspend and | ||
1394 | * resume irq logic may need more attention... | ||
1395 | */ | ||
1396 | |||
1397 | /* host initiated suspend (3+ms bus idle) */ | ||
1398 | } else if (status & AT91_UDP_RXSUSP) { | ||
1399 | at91_udp_write(AT91_UDP_IDR, AT91_UDP_RXSUSP); | ||
1400 | at91_udp_write(AT91_UDP_IER, AT91_UDP_RXRSM); | ||
1401 | at91_udp_write(AT91_UDP_ICR, AT91_UDP_RXSUSP); | ||
1402 | // VDBG("bus suspend\n"); | ||
1403 | if (udc->suspended) | ||
1404 | continue; | ||
1405 | udc->suspended = 1; | ||
1406 | |||
1407 | /* | ||
1408 | * NOTE: when suspending a VBUS-powered device, the | ||
1409 | * gadget driver should switch into slow clock mode | ||
1410 | * and then into standby to avoid drawing more than | ||
1411 | * 500uA power (2500uA for some high-power configs). | ||
1412 | */ | ||
1413 | if (udc->driver && udc->driver->suspend) | ||
1414 | udc->driver->suspend(&udc->gadget); | ||
1415 | |||
1416 | /* host initiated resume */ | ||
1417 | } else if (status & AT91_UDP_RXRSM) { | ||
1418 | at91_udp_write(AT91_UDP_IDR, AT91_UDP_RXRSM); | ||
1419 | at91_udp_write(AT91_UDP_IER, AT91_UDP_RXSUSP); | ||
1420 | at91_udp_write(AT91_UDP_ICR, AT91_UDP_RXRSM); | ||
1421 | // VDBG("bus resume\n"); | ||
1422 | if (!udc->suspended) | ||
1423 | continue; | ||
1424 | udc->suspended = 0; | ||
1425 | |||
1426 | /* | ||
1427 | * NOTE: for a VBUS-powered device, the gadget driver | ||
1428 | * would normally want to switch out of slow clock | ||
1429 | * mode into normal mode. | ||
1430 | */ | ||
1431 | if (udc->driver && udc->driver->resume) | ||
1432 | udc->driver->resume(&udc->gadget); | ||
1433 | |||
1434 | /* endpoint IRQs are cleared by handling them */ | ||
1435 | } else { | ||
1436 | int i; | ||
1437 | unsigned mask = 1; | ||
1438 | struct at91_ep *ep = &udc->ep[1]; | ||
1439 | |||
1440 | if (status & mask) | ||
1441 | handle_ep0(udc); | ||
1442 | for (i = 1; i < NUM_ENDPOINTS; i++) { | ||
1443 | mask <<= 1; | ||
1444 | if (status & mask) | ||
1445 | handle_ep(ep); | ||
1446 | ep++; | ||
1447 | } | ||
1448 | } | ||
1449 | } | ||
1450 | |||
1451 | return IRQ_HANDLED; | ||
1452 | } | ||
1453 | |||
1454 | /*-------------------------------------------------------------------------*/ | ||
1455 | |||
1456 | static struct at91_udc controller = { | ||
1457 | .gadget = { | ||
1458 | .ops = &at91_udc_ops, | ||
1459 | .ep0 = &controller.ep[0].ep, | ||
1460 | .name = driver_name, | ||
1461 | .dev = { | ||
1462 | .bus_id = "gadget" | ||
1463 | } | ||
1464 | }, | ||
1465 | .ep[0] = { | ||
1466 | .ep = { | ||
1467 | .name = ep0name, | ||
1468 | .ops = &at91_ep_ops, | ||
1469 | }, | ||
1470 | .udc = &controller, | ||
1471 | .maxpacket = 8, | ||
1472 | .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(0)), | ||
1473 | .int_mask = 1 << 0, | ||
1474 | }, | ||
1475 | .ep[1] = { | ||
1476 | .ep = { | ||
1477 | .name = "ep1", | ||
1478 | .ops = &at91_ep_ops, | ||
1479 | }, | ||
1480 | .udc = &controller, | ||
1481 | .is_pingpong = 1, | ||
1482 | .maxpacket = 64, | ||
1483 | .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(1)), | ||
1484 | .int_mask = 1 << 1, | ||
1485 | }, | ||
1486 | .ep[2] = { | ||
1487 | .ep = { | ||
1488 | .name = "ep2", | ||
1489 | .ops = &at91_ep_ops, | ||
1490 | }, | ||
1491 | .udc = &controller, | ||
1492 | .is_pingpong = 1, | ||
1493 | .maxpacket = 64, | ||
1494 | .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(2)), | ||
1495 | .int_mask = 1 << 2, | ||
1496 | }, | ||
1497 | .ep[3] = { | ||
1498 | .ep = { | ||
1499 | /* could actually do bulk too */ | ||
1500 | .name = "ep3-int", | ||
1501 | .ops = &at91_ep_ops, | ||
1502 | }, | ||
1503 | .udc = &controller, | ||
1504 | .maxpacket = 8, | ||
1505 | .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(3)), | ||
1506 | .int_mask = 1 << 3, | ||
1507 | }, | ||
1508 | .ep[4] = { | ||
1509 | .ep = { | ||
1510 | .name = "ep4", | ||
1511 | .ops = &at91_ep_ops, | ||
1512 | }, | ||
1513 | .udc = &controller, | ||
1514 | .is_pingpong = 1, | ||
1515 | .maxpacket = 256, | ||
1516 | .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(4)), | ||
1517 | .int_mask = 1 << 4, | ||
1518 | }, | ||
1519 | .ep[5] = { | ||
1520 | .ep = { | ||
1521 | .name = "ep5", | ||
1522 | .ops = &at91_ep_ops, | ||
1523 | }, | ||
1524 | .udc = &controller, | ||
1525 | .is_pingpong = 1, | ||
1526 | .maxpacket = 256, | ||
1527 | .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(5)), | ||
1528 | .int_mask = 1 << 5, | ||
1529 | }, | ||
1530 | /* ep6 and ep7 are also reserved */ | ||
1531 | }; | ||
1532 | |||
1533 | static irqreturn_t at91_vbus_irq(int irq, void *_udc, struct pt_regs *r) | ||
1534 | { | ||
1535 | struct at91_udc *udc = _udc; | ||
1536 | unsigned value; | ||
1537 | |||
1538 | /* vbus needs at least brief debouncing */ | ||
1539 | udelay(10); | ||
1540 | value = at91_get_gpio_value(udc->board.vbus_pin); | ||
1541 | if (value != udc->vbus) | ||
1542 | at91_vbus_session(&udc->gadget, value); | ||
1543 | |||
1544 | return IRQ_HANDLED; | ||
1545 | } | ||
1546 | |||
1547 | int usb_gadget_register_driver (struct usb_gadget_driver *driver) | ||
1548 | { | ||
1549 | struct at91_udc *udc = &controller; | ||
1550 | int retval; | ||
1551 | |||
1552 | if (!driver | ||
1553 | || driver->speed != USB_SPEED_FULL | ||
1554 | || !driver->bind | ||
1555 | || !driver->unbind | ||
1556 | || !driver->setup) { | ||
1557 | DBG("bad parameter.\n"); | ||
1558 | return -EINVAL; | ||
1559 | } | ||
1560 | |||
1561 | if (udc->driver) { | ||
1562 | DBG("UDC already has a gadget driver\n"); | ||
1563 | return -EBUSY; | ||
1564 | } | ||
1565 | |||
1566 | udc->driver = driver; | ||
1567 | udc->gadget.dev.driver = &driver->driver; | ||
1568 | udc->gadget.dev.driver_data = &driver->driver; | ||
1569 | udc->enabled = 1; | ||
1570 | udc->selfpowered = 1; | ||
1571 | |||
1572 | retval = driver->bind(&udc->gadget); | ||
1573 | if (retval) { | ||
1574 | DBG("driver->bind() returned %d\n", retval); | ||
1575 | udc->driver = NULL; | ||
1576 | return retval; | ||
1577 | } | ||
1578 | |||
1579 | local_irq_disable(); | ||
1580 | pullup(udc, 1); | ||
1581 | local_irq_enable(); | ||
1582 | |||
1583 | DBG("bound to %s\n", driver->driver.name); | ||
1584 | return 0; | ||
1585 | } | ||
1586 | EXPORT_SYMBOL (usb_gadget_register_driver); | ||
1587 | |||
1588 | int usb_gadget_unregister_driver (struct usb_gadget_driver *driver) | ||
1589 | { | ||
1590 | struct at91_udc *udc = &controller; | ||
1591 | |||
1592 | if (!driver || driver != udc->driver) | ||
1593 | return -EINVAL; | ||
1594 | |||
1595 | local_irq_disable(); | ||
1596 | udc->enabled = 0; | ||
1597 | pullup(udc, 0); | ||
1598 | local_irq_enable(); | ||
1599 | |||
1600 | driver->unbind(&udc->gadget); | ||
1601 | udc->driver = NULL; | ||
1602 | |||
1603 | DBG("unbound from %s\n", driver->driver.name); | ||
1604 | return 0; | ||
1605 | } | ||
1606 | EXPORT_SYMBOL (usb_gadget_unregister_driver); | ||
1607 | |||
1608 | /*-------------------------------------------------------------------------*/ | ||
1609 | |||
1610 | static void at91udc_shutdown(struct platform_device *dev) | ||
1611 | { | ||
1612 | /* force disconnect on reboot */ | ||
1613 | pullup(platform_get_drvdata(dev), 0); | ||
1614 | } | ||
1615 | |||
1616 | static int __devinit at91udc_probe(struct platform_device *pdev) | ||
1617 | { | ||
1618 | struct device *dev = &pdev->dev; | ||
1619 | struct at91_udc *udc; | ||
1620 | int retval; | ||
1621 | |||
1622 | if (!dev->platform_data) { | ||
1623 | /* small (so we copy it) but critical! */ | ||
1624 | DBG("missing platform_data\n"); | ||
1625 | return -ENODEV; | ||
1626 | } | ||
1627 | |||
1628 | if (!request_mem_region(AT91_BASE_UDP, SZ_16K, driver_name)) { | ||
1629 | DBG("someone's using UDC memory\n"); | ||
1630 | return -EBUSY; | ||
1631 | } | ||
1632 | |||
1633 | /* init software state */ | ||
1634 | udc = &controller; | ||
1635 | udc->gadget.dev.parent = dev; | ||
1636 | udc->board = *(struct at91_udc_data *) dev->platform_data; | ||
1637 | udc->pdev = pdev; | ||
1638 | udc_reinit(udc); | ||
1639 | udc->enabled = 0; | ||
1640 | |||
1641 | /* get interface and function clocks */ | ||
1642 | udc->iclk = clk_get(dev, "udc_clk"); | ||
1643 | udc->fclk = clk_get(dev, "udpck"); | ||
1644 | if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) { | ||
1645 | DBG("clocks missing\n"); | ||
1646 | return -ENODEV; | ||
1647 | } | ||
1648 | |||
1649 | retval = device_register(&udc->gadget.dev); | ||
1650 | if (retval < 0) | ||
1651 | goto fail0; | ||
1652 | |||
1653 | /* disable everything until there's a gadget driver and vbus */ | ||
1654 | pullup(udc, 0); | ||
1655 | |||
1656 | /* request UDC and maybe VBUS irqs */ | ||
1657 | if (request_irq(AT91_ID_UDP, at91_udc_irq, SA_INTERRUPT, driver_name, udc)) { | ||
1658 | DBG("request irq %d failed\n", AT91_ID_UDP); | ||
1659 | retval = -EBUSY; | ||
1660 | goto fail1; | ||
1661 | } | ||
1662 | if (udc->board.vbus_pin > 0) { | ||
1663 | if (request_irq(udc->board.vbus_pin, at91_vbus_irq, SA_INTERRUPT, driver_name, udc)) { | ||
1664 | DBG("request vbus irq %d failed\n", udc->board.vbus_pin); | ||
1665 | free_irq(AT91_ID_UDP, udc); | ||
1666 | retval = -EBUSY; | ||
1667 | goto fail1; | ||
1668 | } | ||
1669 | } else { | ||
1670 | DBG("no VBUS detection, assuming always-on\n"); | ||
1671 | udc->vbus = 1; | ||
1672 | } | ||
1673 | dev_set_drvdata(dev, udc); | ||
1674 | create_debug_file(udc); | ||
1675 | |||
1676 | INFO("%s version %s\n", driver_name, DRIVER_VERSION); | ||
1677 | return 0; | ||
1678 | |||
1679 | fail1: | ||
1680 | device_unregister(&udc->gadget.dev); | ||
1681 | fail0: | ||
1682 | release_mem_region(AT91_VA_BASE_UDP, SZ_16K); | ||
1683 | DBG("%s probe failed, %d\n", driver_name, retval); | ||
1684 | return retval; | ||
1685 | } | ||
1686 | |||
1687 | static int __devexit at91udc_remove(struct platform_device *dev) | ||
1688 | { | ||
1689 | struct at91_udc *udc = platform_get_drvdata(dev); | ||
1690 | |||
1691 | DBG("remove\n"); | ||
1692 | |||
1693 | pullup(udc, 0); | ||
1694 | |||
1695 | if (udc->driver != 0) | ||
1696 | usb_gadget_unregister_driver(udc->driver); | ||
1697 | |||
1698 | remove_debug_file(udc); | ||
1699 | if (udc->board.vbus_pin > 0) | ||
1700 | free_irq(udc->board.vbus_pin, udc); | ||
1701 | free_irq(AT91_ID_UDP, udc); | ||
1702 | device_unregister(&udc->gadget.dev); | ||
1703 | release_mem_region(AT91_BASE_UDP, SZ_16K); | ||
1704 | |||
1705 | clk_put(udc->iclk); | ||
1706 | clk_put(udc->fclk); | ||
1707 | |||
1708 | return 0; | ||
1709 | } | ||
1710 | |||
1711 | #ifdef CONFIG_PM | ||
1712 | static int at91udc_suspend(struct platform_device *dev, u32 state, u32 level) | ||
1713 | { | ||
1714 | struct at91_udc *udc = platform_get_drvdata(dev); | ||
1715 | |||
1716 | /* | ||
1717 | * The "safe" suspend transitions are opportunistic ... e.g. when | ||
1718 | * the USB link is suspended (48MHz clock autogated off), or when | ||
1719 | * it's disconnected (programmatically gated off, elsewhere). | ||
1720 | * Then we can suspend, and the chip can enter slow clock mode. | ||
1721 | * | ||
1722 | * The problem case is some component (user mode?) suspending this | ||
1723 | * device while it's active, with the 48 MHz clock in use. There | ||
1724 | * are two basic approaches: (a) veto suspend levels involving slow | ||
1725 | * clock mode, (b) disconnect, so 48 MHz will no longer be in use | ||
1726 | * and we can enter slow clock mode. This uses (b) for now, since | ||
1727 | * it's simplest until AT91 PM exists and supports the other option. | ||
1728 | */ | ||
1729 | if (udc->vbus && !udc->suspended) | ||
1730 | pullup(udc, 0); | ||
1731 | return 0; | ||
1732 | } | ||
1733 | |||
1734 | static int at91udc_resume(struct platform_device *dev, u32 level) | ||
1735 | { | ||
1736 | struct at91_udc *udc = platform_get_drvdata(dev); | ||
1737 | |||
1738 | /* maybe reconnect to host; if so, clocks on */ | ||
1739 | pullup(udc, 1); | ||
1740 | return 0; | ||
1741 | } | ||
1742 | #else | ||
1743 | #define at91udc_suspend NULL | ||
1744 | #define at91udc_resume NULL | ||
1745 | #endif | ||
1746 | |||
1747 | static struct platform_driver at91_udc = { | ||
1748 | .probe = at91udc_probe, | ||
1749 | .remove = __devexit_p(at91udc_remove), | ||
1750 | .shutdown = at91udc_shutdown, | ||
1751 | .suspend = at91udc_suspend, | ||
1752 | .resume = at91udc_resume, | ||
1753 | .driver = { | ||
1754 | .name = (char *) driver_name, | ||
1755 | .owner = THIS_MODULE, | ||
1756 | }, | ||
1757 | }; | ||
1758 | |||
1759 | static int __devinit udc_init_module(void) | ||
1760 | { | ||
1761 | return platform_driver_register(&at91_udc); | ||
1762 | } | ||
1763 | module_init(udc_init_module); | ||
1764 | |||
1765 | static void __devexit udc_exit_module(void) | ||
1766 | { | ||
1767 | platform_driver_unregister(&at91_udc); | ||
1768 | } | ||
1769 | module_exit(udc_exit_module); | ||
1770 | |||
1771 | MODULE_DESCRIPTION("AT91RM9200 udc driver"); | ||
1772 | MODULE_AUTHOR("Thomas Rathbone, David Brownell"); | ||
1773 | MODULE_LICENSE("GPL"); | ||