diff options
Diffstat (limited to 'arch/mips/au1000/common/usbdev.c')
-rw-r--r-- | arch/mips/au1000/common/usbdev.c | 1552 |
1 files changed, 0 insertions, 1552 deletions
diff --git a/arch/mips/au1000/common/usbdev.c b/arch/mips/au1000/common/usbdev.c deleted file mode 100644 index 70dc82e536cd..000000000000 --- a/arch/mips/au1000/common/usbdev.c +++ /dev/null | |||
@@ -1,1552 +0,0 @@ | |||
1 | /* | ||
2 | * BRIEF MODULE DESCRIPTION | ||
3 | * Au1000 USB Device-Side (device layer) | ||
4 | * | ||
5 | * Copyright 2001-2002 MontaVista Software Inc. | ||
6 | * Author: MontaVista Software, Inc. | ||
7 | * stevel@mvista.com or source@mvista.com | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify it | ||
10 | * under the terms of the GNU General Public License as published by the | ||
11 | * Free Software Foundation; either version 2 of the License, or (at your | ||
12 | * option) any later version. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | ||
15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
16 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN | ||
17 | * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
20 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
21 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
24 | * | ||
25 | * You should have received a copy of the GNU General Public License along | ||
26 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
27 | * 675 Mass Ave, Cambridge, MA 02139, USA. | ||
28 | */ | ||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/ioport.h> | ||
31 | #include <linux/sched.h> | ||
32 | #include <linux/signal.h> | ||
33 | #include <linux/errno.h> | ||
34 | #include <linux/poll.h> | ||
35 | #include <linux/init.h> | ||
36 | #include <linux/slab.h> | ||
37 | #include <linux/fcntl.h> | ||
38 | #include <linux/module.h> | ||
39 | #include <linux/spinlock.h> | ||
40 | #include <linux/list.h> | ||
41 | #include <linux/smp_lock.h> | ||
42 | #define DEBUG | ||
43 | #include <linux/usb.h> | ||
44 | |||
45 | #include <asm/io.h> | ||
46 | #include <asm/uaccess.h> | ||
47 | #include <asm/irq.h> | ||
48 | #include <asm/mipsregs.h> | ||
49 | #include <asm/au1000.h> | ||
50 | #include <asm/au1000_dma.h> | ||
51 | #include <asm/au1000_usbdev.h> | ||
52 | |||
53 | #ifdef DEBUG | ||
54 | #undef VDEBUG | ||
55 | #ifdef VDEBUG | ||
56 | #define vdbg(fmt, arg...) printk(KERN_DEBUG __FILE__ ": " fmt "\n" , ## arg) | ||
57 | #else | ||
58 | #define vdbg(fmt, arg...) do {} while (0) | ||
59 | #endif | ||
60 | #else | ||
61 | #define vdbg(fmt, arg...) do {} while (0) | ||
62 | #endif | ||
63 | |||
64 | #define ALLOC_FLAGS (in_interrupt () ? GFP_ATOMIC : GFP_KERNEL) | ||
65 | |||
66 | #define EP_FIFO_DEPTH 8 | ||
67 | |||
68 | typedef enum { | ||
69 | SETUP_STAGE = 0, | ||
70 | DATA_STAGE, | ||
71 | STATUS_STAGE | ||
72 | } ep0_stage_t; | ||
73 | |||
74 | typedef struct { | ||
75 | int read_fifo; | ||
76 | int write_fifo; | ||
77 | int ctrl_stat; | ||
78 | int read_fifo_status; | ||
79 | int write_fifo_status; | ||
80 | } endpoint_reg_t; | ||
81 | |||
82 | typedef struct { | ||
83 | usbdev_pkt_t *head; | ||
84 | usbdev_pkt_t *tail; | ||
85 | int count; | ||
86 | } pkt_list_t; | ||
87 | |||
88 | typedef struct { | ||
89 | int active; | ||
90 | struct usb_endpoint_descriptor *desc; | ||
91 | endpoint_reg_t *reg; | ||
92 | /* Only one of these are used, unless this is the control ep */ | ||
93 | pkt_list_t inlist; | ||
94 | pkt_list_t outlist; | ||
95 | unsigned int indma, outdma; /* DMA channel numbers for IN, OUT */ | ||
96 | /* following are extracted from endpoint descriptor for easy access */ | ||
97 | int max_pkt_size; | ||
98 | int type; | ||
99 | int direction; | ||
100 | /* WE assign endpoint addresses! */ | ||
101 | int address; | ||
102 | spinlock_t lock; | ||
103 | } endpoint_t; | ||
104 | |||
105 | |||
106 | static struct usb_dev { | ||
107 | endpoint_t ep[6]; | ||
108 | ep0_stage_t ep0_stage; | ||
109 | |||
110 | struct usb_device_descriptor * dev_desc; | ||
111 | struct usb_interface_descriptor* if_desc; | ||
112 | struct usb_config_descriptor * conf_desc; | ||
113 | u8 * full_conf_desc; | ||
114 | struct usb_string_descriptor * str_desc[6]; | ||
115 | |||
116 | /* callback to function layer */ | ||
117 | void (*func_cb)(usbdev_cb_type_t type, unsigned long arg, | ||
118 | void *cb_data); | ||
119 | void* cb_data; | ||
120 | |||
121 | usbdev_state_t state; // device state | ||
122 | int suspended; // suspended flag | ||
123 | int address; // device address | ||
124 | int interface; | ||
125 | int num_ep; | ||
126 | u8 alternate_setting; | ||
127 | u8 configuration; // configuration value | ||
128 | int remote_wakeup_en; | ||
129 | } usbdev; | ||
130 | |||
131 | |||
132 | static endpoint_reg_t ep_reg[] = { | ||
133 | // FIFO's 0 and 1 are EP0 default control | ||
134 | {USBD_EP0RD, USBD_EP0WR, USBD_EP0CS, USBD_EP0RDSTAT, USBD_EP0WRSTAT }, | ||
135 | {0}, | ||
136 | // FIFO 2 is EP2, IN | ||
137 | { -1, USBD_EP2WR, USBD_EP2CS, -1, USBD_EP2WRSTAT }, | ||
138 | // FIFO 3 is EP3, IN | ||
139 | { -1, USBD_EP3WR, USBD_EP3CS, -1, USBD_EP3WRSTAT }, | ||
140 | // FIFO 4 is EP4, OUT | ||
141 | {USBD_EP4RD, -1, USBD_EP4CS, USBD_EP4RDSTAT, -1 }, | ||
142 | // FIFO 5 is EP5, OUT | ||
143 | {USBD_EP5RD, -1, USBD_EP5CS, USBD_EP5RDSTAT, -1 } | ||
144 | }; | ||
145 | |||
146 | static struct { | ||
147 | unsigned int id; | ||
148 | const char *str; | ||
149 | } ep_dma_id[] = { | ||
150 | { DMA_ID_USBDEV_EP0_TX, "USBDev EP0 IN" }, | ||
151 | { DMA_ID_USBDEV_EP0_RX, "USBDev EP0 OUT" }, | ||
152 | { DMA_ID_USBDEV_EP2_TX, "USBDev EP2 IN" }, | ||
153 | { DMA_ID_USBDEV_EP3_TX, "USBDev EP3 IN" }, | ||
154 | { DMA_ID_USBDEV_EP4_RX, "USBDev EP4 OUT" }, | ||
155 | { DMA_ID_USBDEV_EP5_RX, "USBDev EP5 OUT" } | ||
156 | }; | ||
157 | |||
158 | #define DIR_OUT 0 | ||
159 | #define DIR_IN (1<<3) | ||
160 | |||
161 | #define CONTROL_EP USB_ENDPOINT_XFER_CONTROL | ||
162 | #define BULK_EP USB_ENDPOINT_XFER_BULK | ||
163 | |||
164 | static inline endpoint_t * | ||
165 | epaddr_to_ep(struct usb_dev* dev, int ep_addr) | ||
166 | { | ||
167 | if (ep_addr >= 0 && ep_addr < 2) | ||
168 | return &dev->ep[0]; | ||
169 | if (ep_addr < 6) | ||
170 | return &dev->ep[ep_addr]; | ||
171 | return NULL; | ||
172 | } | ||
173 | |||
174 | static const char* std_req_name[] = { | ||
175 | "GET_STATUS", | ||
176 | "CLEAR_FEATURE", | ||
177 | "RESERVED", | ||
178 | "SET_FEATURE", | ||
179 | "RESERVED", | ||
180 | "SET_ADDRESS", | ||
181 | "GET_DESCRIPTOR", | ||
182 | "SET_DESCRIPTOR", | ||
183 | "GET_CONFIGURATION", | ||
184 | "SET_CONFIGURATION", | ||
185 | "GET_INTERFACE", | ||
186 | "SET_INTERFACE", | ||
187 | "SYNCH_FRAME" | ||
188 | }; | ||
189 | |||
190 | static inline const char* | ||
191 | get_std_req_name(int req) | ||
192 | { | ||
193 | return (req >= 0 && req <= 12) ? std_req_name[req] : "UNKNOWN"; | ||
194 | } | ||
195 | |||
196 | #if 0 | ||
197 | static void | ||
198 | dump_setup(struct usb_ctrlrequest* s) | ||
199 | { | ||
200 | dbg("%s: requesttype=%d", __FUNCTION__, s->requesttype); | ||
201 | dbg("%s: request=%d %s", __FUNCTION__, s->request, | ||
202 | get_std_req_name(s->request)); | ||
203 | dbg("%s: value=0x%04x", __FUNCTION__, s->wValue); | ||
204 | dbg("%s: index=%d", __FUNCTION__, s->index); | ||
205 | dbg("%s: length=%d", __FUNCTION__, s->length); | ||
206 | } | ||
207 | #endif | ||
208 | |||
209 | static inline usbdev_pkt_t * | ||
210 | alloc_packet(endpoint_t * ep, int data_size, void* data) | ||
211 | { | ||
212 | usbdev_pkt_t* pkt = kmalloc(sizeof(usbdev_pkt_t) + data_size, | ||
213 | ALLOC_FLAGS); | ||
214 | if (!pkt) | ||
215 | return NULL; | ||
216 | pkt->ep_addr = ep->address; | ||
217 | pkt->size = data_size; | ||
218 | pkt->status = 0; | ||
219 | pkt->next = NULL; | ||
220 | if (data) | ||
221 | memcpy(pkt->payload, data, data_size); | ||
222 | |||
223 | return pkt; | ||
224 | } | ||
225 | |||
226 | |||
227 | /* | ||
228 | * Link a packet to the tail of the enpoint's packet list. | ||
229 | * EP spinlock must be held when calling. | ||
230 | */ | ||
231 | static void | ||
232 | link_tail(endpoint_t * ep, pkt_list_t * list, usbdev_pkt_t * pkt) | ||
233 | { | ||
234 | if (!list->tail) { | ||
235 | list->head = list->tail = pkt; | ||
236 | list->count = 1; | ||
237 | } else { | ||
238 | list->tail->next = pkt; | ||
239 | list->tail = pkt; | ||
240 | list->count++; | ||
241 | } | ||
242 | } | ||
243 | |||
244 | /* | ||
245 | * Unlink and return a packet from the head of the given packet | ||
246 | * list. It is the responsibility of the caller to free the packet. | ||
247 | * EP spinlock must be held when calling. | ||
248 | */ | ||
249 | static usbdev_pkt_t * | ||
250 | unlink_head(pkt_list_t * list) | ||
251 | { | ||
252 | usbdev_pkt_t *pkt; | ||
253 | |||
254 | pkt = list->head; | ||
255 | if (!pkt || !list->count) { | ||
256 | return NULL; | ||
257 | } | ||
258 | |||
259 | list->head = pkt->next; | ||
260 | if (!list->head) { | ||
261 | list->head = list->tail = NULL; | ||
262 | list->count = 0; | ||
263 | } else | ||
264 | list->count--; | ||
265 | |||
266 | return pkt; | ||
267 | } | ||
268 | |||
269 | /* | ||
270 | * Create and attach a new packet to the tail of the enpoint's | ||
271 | * packet list. EP spinlock must be held when calling. | ||
272 | */ | ||
273 | static usbdev_pkt_t * | ||
274 | add_packet(endpoint_t * ep, pkt_list_t * list, int size) | ||
275 | { | ||
276 | usbdev_pkt_t *pkt = alloc_packet(ep, size, NULL); | ||
277 | if (!pkt) | ||
278 | return NULL; | ||
279 | |||
280 | link_tail(ep, list, pkt); | ||
281 | return pkt; | ||
282 | } | ||
283 | |||
284 | |||
285 | /* | ||
286 | * Unlink and free a packet from the head of the enpoint's | ||
287 | * packet list. EP spinlock must be held when calling. | ||
288 | */ | ||
289 | static inline void | ||
290 | free_packet(pkt_list_t * list) | ||
291 | { | ||
292 | kfree(unlink_head(list)); | ||
293 | } | ||
294 | |||
295 | /* EP spinlock must be held when calling. */ | ||
296 | static inline void | ||
297 | flush_pkt_list(pkt_list_t * list) | ||
298 | { | ||
299 | while (list->count) | ||
300 | free_packet(list); | ||
301 | } | ||
302 | |||
303 | /* EP spinlock must be held when calling */ | ||
304 | static inline void | ||
305 | flush_write_fifo(endpoint_t * ep) | ||
306 | { | ||
307 | if (ep->reg->write_fifo_status >= 0) { | ||
308 | au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | | ||
309 | USBDEV_FSTAT_OF, | ||
310 | ep->reg->write_fifo_status); | ||
311 | //udelay(100); | ||
312 | //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, | ||
313 | // ep->reg->write_fifo_status); | ||
314 | } | ||
315 | } | ||
316 | |||
317 | /* EP spinlock must be held when calling */ | ||
318 | static inline void | ||
319 | flush_read_fifo(endpoint_t * ep) | ||
320 | { | ||
321 | if (ep->reg->read_fifo_status >= 0) { | ||
322 | au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | | ||
323 | USBDEV_FSTAT_OF, | ||
324 | ep->reg->read_fifo_status); | ||
325 | //udelay(100); | ||
326 | //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, | ||
327 | // ep->reg->read_fifo_status); | ||
328 | } | ||
329 | } | ||
330 | |||
331 | |||
332 | /* EP spinlock must be held when calling. */ | ||
333 | static void | ||
334 | endpoint_flush(endpoint_t * ep) | ||
335 | { | ||
336 | // First, flush all packets | ||
337 | flush_pkt_list(&ep->inlist); | ||
338 | flush_pkt_list(&ep->outlist); | ||
339 | |||
340 | // Now flush the endpoint's h/w FIFO(s) | ||
341 | flush_write_fifo(ep); | ||
342 | flush_read_fifo(ep); | ||
343 | } | ||
344 | |||
345 | /* EP spinlock must be held when calling. */ | ||
346 | static void | ||
347 | endpoint_stall(endpoint_t * ep) | ||
348 | { | ||
349 | u32 cs; | ||
350 | |||
351 | warn("%s", __FUNCTION__); | ||
352 | |||
353 | cs = au_readl(ep->reg->ctrl_stat) | USBDEV_CS_STALL; | ||
354 | au_writel(cs, ep->reg->ctrl_stat); | ||
355 | } | ||
356 | |||
357 | /* EP spinlock must be held when calling. */ | ||
358 | static void | ||
359 | endpoint_unstall(endpoint_t * ep) | ||
360 | { | ||
361 | u32 cs; | ||
362 | |||
363 | warn("%s", __FUNCTION__); | ||
364 | |||
365 | cs = au_readl(ep->reg->ctrl_stat) & ~USBDEV_CS_STALL; | ||
366 | au_writel(cs, ep->reg->ctrl_stat); | ||
367 | } | ||
368 | |||
369 | static void | ||
370 | endpoint_reset_datatoggle(endpoint_t * ep) | ||
371 | { | ||
372 | // FIXME: is this possible? | ||
373 | } | ||
374 | |||
375 | |||
376 | /* EP spinlock must be held when calling. */ | ||
377 | static int | ||
378 | endpoint_fifo_read(endpoint_t * ep) | ||
379 | { | ||
380 | int read_count = 0; | ||
381 | u8 *bufptr; | ||
382 | usbdev_pkt_t *pkt = ep->outlist.tail; | ||
383 | |||
384 | if (!pkt) | ||
385 | return -EINVAL; | ||
386 | |||
387 | bufptr = &pkt->payload[pkt->size]; | ||
388 | while (au_readl(ep->reg->read_fifo_status) & USBDEV_FSTAT_FCNT_MASK) { | ||
389 | *bufptr++ = au_readl(ep->reg->read_fifo) & 0xff; | ||
390 | read_count++; | ||
391 | pkt->size++; | ||
392 | } | ||
393 | |||
394 | return read_count; | ||
395 | } | ||
396 | |||
397 | #if 0 | ||
398 | /* EP spinlock must be held when calling. */ | ||
399 | static int | ||
400 | endpoint_fifo_write(endpoint_t * ep, int index) | ||
401 | { | ||
402 | int write_count = 0; | ||
403 | u8 *bufptr; | ||
404 | usbdev_pkt_t *pkt = ep->inlist.head; | ||
405 | |||
406 | if (!pkt) | ||
407 | return -EINVAL; | ||
408 | |||
409 | bufptr = &pkt->payload[index]; | ||
410 | while ((au_readl(ep->reg->write_fifo_status) & | ||
411 | USBDEV_FSTAT_FCNT_MASK) < EP_FIFO_DEPTH) { | ||
412 | if (bufptr < pkt->payload + pkt->size) { | ||
413 | au_writel(*bufptr++, ep->reg->write_fifo); | ||
414 | write_count++; | ||
415 | } else { | ||
416 | break; | ||
417 | } | ||
418 | } | ||
419 | |||
420 | return write_count; | ||
421 | } | ||
422 | #endif | ||
423 | |||
424 | /* | ||
425 | * This routine is called to restart transmission of a packet. | ||
426 | * The endpoint's TSIZE must be set to the new packet's size, | ||
427 | * and DMA to the write FIFO needs to be restarted. | ||
428 | * EP spinlock must be held when calling. | ||
429 | */ | ||
430 | static void | ||
431 | kickstart_send_packet(endpoint_t * ep) | ||
432 | { | ||
433 | u32 cs; | ||
434 | usbdev_pkt_t *pkt = ep->inlist.head; | ||
435 | |||
436 | vdbg("%s: ep%d, pkt=%p", __FUNCTION__, ep->address, pkt); | ||
437 | |||
438 | if (!pkt) { | ||
439 | err("%s: head=NULL! list->count=%d", __FUNCTION__, | ||
440 | ep->inlist.count); | ||
441 | return; | ||
442 | } | ||
443 | |||
444 | dma_cache_wback_inv((unsigned long)pkt->payload, pkt->size); | ||
445 | |||
446 | /* | ||
447 | * make sure FIFO is empty | ||
448 | */ | ||
449 | flush_write_fifo(ep); | ||
450 | |||
451 | cs = au_readl(ep->reg->ctrl_stat) & USBDEV_CS_STALL; | ||
452 | cs |= (pkt->size << USBDEV_CS_TSIZE_BIT); | ||
453 | au_writel(cs, ep->reg->ctrl_stat); | ||
454 | |||
455 | if (get_dma_active_buffer(ep->indma) == 1) { | ||
456 | set_dma_count1(ep->indma, pkt->size); | ||
457 | set_dma_addr1(ep->indma, virt_to_phys(pkt->payload)); | ||
458 | enable_dma_buffer1(ep->indma); // reenable | ||
459 | } else { | ||
460 | set_dma_count0(ep->indma, pkt->size); | ||
461 | set_dma_addr0(ep->indma, virt_to_phys(pkt->payload)); | ||
462 | enable_dma_buffer0(ep->indma); // reenable | ||
463 | } | ||
464 | if (dma_halted(ep->indma)) | ||
465 | start_dma(ep->indma); | ||
466 | } | ||
467 | |||
468 | |||
469 | /* | ||
470 | * This routine is called when a packet in the inlist has been | ||
471 | * completed. Frees the completed packet and starts sending the | ||
472 | * next. EP spinlock must be held when calling. | ||
473 | */ | ||
474 | static usbdev_pkt_t * | ||
475 | send_packet_complete(endpoint_t * ep) | ||
476 | { | ||
477 | usbdev_pkt_t *pkt = unlink_head(&ep->inlist); | ||
478 | |||
479 | if (pkt) { | ||
480 | pkt->status = | ||
481 | (au_readl(ep->reg->ctrl_stat) & USBDEV_CS_NAK) ? | ||
482 | PKT_STATUS_NAK : PKT_STATUS_ACK; | ||
483 | |||
484 | vdbg("%s: ep%d, %s pkt=%p, list count=%d", __FUNCTION__, | ||
485 | ep->address, (pkt->status & PKT_STATUS_NAK) ? | ||
486 | "NAK" : "ACK", pkt, ep->inlist.count); | ||
487 | } | ||
488 | |||
489 | /* | ||
490 | * The write fifo should already be drained if things are | ||
491 | * working right, but flush it anyway just in case. | ||
492 | */ | ||
493 | flush_write_fifo(ep); | ||
494 | |||
495 | // begin transmitting next packet in the inlist | ||
496 | if (ep->inlist.count) { | ||
497 | kickstart_send_packet(ep); | ||
498 | } | ||
499 | |||
500 | return pkt; | ||
501 | } | ||
502 | |||
503 | /* | ||
504 | * Add a new packet to the tail of the given ep's packet | ||
505 | * inlist. The transmit complete interrupt frees packets from | ||
506 | * the head of this list. EP spinlock must be held when calling. | ||
507 | */ | ||
508 | static int | ||
509 | send_packet(struct usb_dev* dev, usbdev_pkt_t *pkt, int async) | ||
510 | { | ||
511 | pkt_list_t *list; | ||
512 | endpoint_t* ep; | ||
513 | |||
514 | if (!pkt || !(ep = epaddr_to_ep(dev, pkt->ep_addr))) | ||
515 | return -EINVAL; | ||
516 | |||
517 | if (!pkt->size) | ||
518 | return 0; | ||
519 | |||
520 | list = &ep->inlist; | ||
521 | |||
522 | if (!async && list->count) { | ||
523 | halt_dma(ep->indma); | ||
524 | flush_pkt_list(list); | ||
525 | } | ||
526 | |||
527 | link_tail(ep, list, pkt); | ||
528 | |||
529 | vdbg("%s: ep%d, pkt=%p, size=%d, list count=%d", __FUNCTION__, | ||
530 | ep->address, pkt, pkt->size, list->count); | ||
531 | |||
532 | if (list->count == 1) { | ||
533 | /* | ||
534 | * if the packet count is one, it means the list was empty, | ||
535 | * and no more data will go out this ep until we kick-start | ||
536 | * it again. | ||
537 | */ | ||
538 | kickstart_send_packet(ep); | ||
539 | } | ||
540 | |||
541 | return pkt->size; | ||
542 | } | ||
543 | |||
544 | /* | ||
545 | * This routine is called to restart reception of a packet. | ||
546 | * EP spinlock must be held when calling. | ||
547 | */ | ||
548 | static void | ||
549 | kickstart_receive_packet(endpoint_t * ep) | ||
550 | { | ||
551 | usbdev_pkt_t *pkt; | ||
552 | |||
553 | // get and link a new packet for next reception | ||
554 | if (!(pkt = add_packet(ep, &ep->outlist, ep->max_pkt_size))) { | ||
555 | err("%s: could not alloc new packet", __FUNCTION__); | ||
556 | return; | ||
557 | } | ||
558 | |||
559 | if (get_dma_active_buffer(ep->outdma) == 1) { | ||
560 | clear_dma_done1(ep->outdma); | ||
561 | set_dma_count1(ep->outdma, ep->max_pkt_size); | ||
562 | set_dma_count0(ep->outdma, 0); | ||
563 | set_dma_addr1(ep->outdma, virt_to_phys(pkt->payload)); | ||
564 | enable_dma_buffer1(ep->outdma); // reenable | ||
565 | } else { | ||
566 | clear_dma_done0(ep->outdma); | ||
567 | set_dma_count0(ep->outdma, ep->max_pkt_size); | ||
568 | set_dma_count1(ep->outdma, 0); | ||
569 | set_dma_addr0(ep->outdma, virt_to_phys(pkt->payload)); | ||
570 | enable_dma_buffer0(ep->outdma); // reenable | ||
571 | } | ||
572 | if (dma_halted(ep->outdma)) | ||
573 | start_dma(ep->outdma); | ||
574 | } | ||
575 | |||
576 | |||
577 | /* | ||
578 | * This routine is called when a packet in the outlist has been | ||
579 | * completed (received) and we need to prepare for a new packet | ||
580 | * to be received. Halts DMA and computes the packet size from the | ||
581 | * remaining DMA counter. Then prepares a new packet for reception | ||
582 | * and restarts DMA. FIXME: what if another packet comes in | ||
583 | * on top of the completed packet? Counter would be wrong. | ||
584 | * EP spinlock must be held when calling. | ||
585 | */ | ||
586 | static usbdev_pkt_t * | ||
587 | receive_packet_complete(endpoint_t * ep) | ||
588 | { | ||
589 | usbdev_pkt_t *pkt = ep->outlist.tail; | ||
590 | u32 cs; | ||
591 | |||
592 | halt_dma(ep->outdma); | ||
593 | |||
594 | cs = au_readl(ep->reg->ctrl_stat); | ||
595 | |||
596 | if (!pkt) | ||
597 | return NULL; | ||
598 | |||
599 | pkt->size = ep->max_pkt_size - get_dma_residue(ep->outdma); | ||
600 | if (pkt->size) | ||
601 | dma_cache_inv((unsigned long)pkt->payload, pkt->size); | ||
602 | /* | ||
603 | * need to pull out any remaining bytes in the FIFO. | ||
604 | */ | ||
605 | endpoint_fifo_read(ep); | ||
606 | /* | ||
607 | * should be drained now, but flush anyway just in case. | ||
608 | */ | ||
609 | flush_read_fifo(ep); | ||
610 | |||
611 | pkt->status = (cs & USBDEV_CS_NAK) ? PKT_STATUS_NAK : PKT_STATUS_ACK; | ||
612 | if (ep->address == 0 && (cs & USBDEV_CS_SU)) | ||
613 | pkt->status |= PKT_STATUS_SU; | ||
614 | |||
615 | vdbg("%s: ep%d, %s pkt=%p, size=%d", __FUNCTION__, | ||
616 | ep->address, (pkt->status & PKT_STATUS_NAK) ? | ||
617 | "NAK" : "ACK", pkt, pkt->size); | ||
618 | |||
619 | kickstart_receive_packet(ep); | ||
620 | |||
621 | return pkt; | ||
622 | } | ||
623 | |||
624 | |||
625 | /* | ||
626 | **************************************************************************** | ||
627 | * Here starts the standard device request handlers. They are | ||
628 | * all called by do_setup() via a table of function pointers. | ||
629 | **************************************************************************** | ||
630 | */ | ||
631 | |||
632 | static ep0_stage_t | ||
633 | do_get_status(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
634 | { | ||
635 | switch (setup->bRequestType) { | ||
636 | case 0x80: // Device | ||
637 | // FIXME: send device status | ||
638 | break; | ||
639 | case 0x81: // Interface | ||
640 | // FIXME: send interface status | ||
641 | break; | ||
642 | case 0x82: // End Point | ||
643 | // FIXME: send endpoint status | ||
644 | break; | ||
645 | default: | ||
646 | // Invalid Command | ||
647 | endpoint_stall(&dev->ep[0]); // Stall End Point 0 | ||
648 | break; | ||
649 | } | ||
650 | |||
651 | return STATUS_STAGE; | ||
652 | } | ||
653 | |||
654 | static ep0_stage_t | ||
655 | do_clear_feature(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
656 | { | ||
657 | switch (setup->bRequestType) { | ||
658 | case 0x00: // Device | ||
659 | if ((le16_to_cpu(setup->wValue) & 0xff) == 1) | ||
660 | dev->remote_wakeup_en = 0; | ||
661 | else | ||
662 | endpoint_stall(&dev->ep[0]); | ||
663 | break; | ||
664 | case 0x02: // End Point | ||
665 | if ((le16_to_cpu(setup->wValue) & 0xff) == 0) { | ||
666 | endpoint_t *ep = | ||
667 | epaddr_to_ep(dev, | ||
668 | le16_to_cpu(setup->wIndex) & 0xff); | ||
669 | |||
670 | endpoint_unstall(ep); | ||
671 | endpoint_reset_datatoggle(ep); | ||
672 | } else | ||
673 | endpoint_stall(&dev->ep[0]); | ||
674 | break; | ||
675 | } | ||
676 | |||
677 | return SETUP_STAGE; | ||
678 | } | ||
679 | |||
680 | static ep0_stage_t | ||
681 | do_reserved(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
682 | { | ||
683 | // Invalid request, stall End Point 0 | ||
684 | endpoint_stall(&dev->ep[0]); | ||
685 | return SETUP_STAGE; | ||
686 | } | ||
687 | |||
688 | static ep0_stage_t | ||
689 | do_set_feature(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
690 | { | ||
691 | switch (setup->bRequestType) { | ||
692 | case 0x00: // Device | ||
693 | if ((le16_to_cpu(setup->wValue) & 0xff) == 1) | ||
694 | dev->remote_wakeup_en = 1; | ||
695 | else | ||
696 | endpoint_stall(&dev->ep[0]); | ||
697 | break; | ||
698 | case 0x02: // End Point | ||
699 | if ((le16_to_cpu(setup->wValue) & 0xff) == 0) { | ||
700 | endpoint_t *ep = | ||
701 | epaddr_to_ep(dev, | ||
702 | le16_to_cpu(setup->wIndex) & 0xff); | ||
703 | |||
704 | endpoint_stall(ep); | ||
705 | } else | ||
706 | endpoint_stall(&dev->ep[0]); | ||
707 | break; | ||
708 | } | ||
709 | |||
710 | return SETUP_STAGE; | ||
711 | } | ||
712 | |||
713 | static ep0_stage_t | ||
714 | do_set_address(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
715 | { | ||
716 | int new_state = dev->state; | ||
717 | int new_addr = le16_to_cpu(setup->wValue); | ||
718 | |||
719 | dbg("%s: our address=%d", __FUNCTION__, new_addr); | ||
720 | |||
721 | if (new_addr > 127) { | ||
722 | // usb spec doesn't tell us what to do, so just go to | ||
723 | // default state | ||
724 | new_state = DEFAULT; | ||
725 | dev->address = 0; | ||
726 | } else if (dev->address != new_addr) { | ||
727 | dev->address = new_addr; | ||
728 | new_state = ADDRESS; | ||
729 | } | ||
730 | |||
731 | if (dev->state != new_state) { | ||
732 | dev->state = new_state; | ||
733 | /* inform function layer of usbdev state change */ | ||
734 | dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data); | ||
735 | } | ||
736 | |||
737 | return SETUP_STAGE; | ||
738 | } | ||
739 | |||
740 | static ep0_stage_t | ||
741 | do_get_descriptor(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
742 | { | ||
743 | int strnum, desc_len = le16_to_cpu(setup->wLength); | ||
744 | |||
745 | switch (le16_to_cpu(setup->wValue) >> 8) { | ||
746 | case USB_DT_DEVICE: | ||
747 | // send device descriptor! | ||
748 | desc_len = desc_len > dev->dev_desc->bLength ? | ||
749 | dev->dev_desc->bLength : desc_len; | ||
750 | dbg("sending device desc, size=%d", desc_len); | ||
751 | send_packet(dev, alloc_packet(&dev->ep[0], desc_len, | ||
752 | dev->dev_desc), 0); | ||
753 | break; | ||
754 | case USB_DT_CONFIG: | ||
755 | // If the config descr index in low-byte of | ||
756 | // setup->wValue is valid, send config descr, | ||
757 | // otherwise stall ep0. | ||
758 | if ((le16_to_cpu(setup->wValue) & 0xff) == 0) { | ||
759 | // send config descriptor! | ||
760 | if (desc_len <= USB_DT_CONFIG_SIZE) { | ||
761 | dbg("sending partial config desc, size=%d", | ||
762 | desc_len); | ||
763 | send_packet(dev, | ||
764 | alloc_packet(&dev->ep[0], | ||
765 | desc_len, | ||
766 | dev->conf_desc), | ||
767 | 0); | ||
768 | } else { | ||
769 | int len = le16_to_cpu(dev->conf_desc->wTotalLength); | ||
770 | dbg("sending whole config desc," | ||
771 | " size=%d, our size=%d", desc_len, len); | ||
772 | desc_len = desc_len > len ? len : desc_len; | ||
773 | send_packet(dev, | ||
774 | alloc_packet(&dev->ep[0], | ||
775 | desc_len, | ||
776 | dev->full_conf_desc), | ||
777 | 0); | ||
778 | } | ||
779 | } else | ||
780 | endpoint_stall(&dev->ep[0]); | ||
781 | break; | ||
782 | case USB_DT_STRING: | ||
783 | // If the string descr index in low-byte of setup->wValue | ||
784 | // is valid, send string descr, otherwise stall ep0. | ||
785 | strnum = le16_to_cpu(setup->wValue) & 0xff; | ||
786 | if (strnum >= 0 && strnum < 6) { | ||
787 | struct usb_string_descriptor *desc = | ||
788 | dev->str_desc[strnum]; | ||
789 | desc_len = desc_len > desc->bLength ? | ||
790 | desc->bLength : desc_len; | ||
791 | dbg("sending string desc %d", strnum); | ||
792 | send_packet(dev, | ||
793 | alloc_packet(&dev->ep[0], desc_len, | ||
794 | desc), 0); | ||
795 | } else | ||
796 | endpoint_stall(&dev->ep[0]); | ||
797 | break; | ||
798 | default: | ||
799 | // Invalid request | ||
800 | err("invalid get desc=%d, stalled", | ||
801 | le16_to_cpu(setup->wValue) >> 8); | ||
802 | endpoint_stall(&dev->ep[0]); // Stall endpoint 0 | ||
803 | break; | ||
804 | } | ||
805 | |||
806 | return STATUS_STAGE; | ||
807 | } | ||
808 | |||
809 | static ep0_stage_t | ||
810 | do_set_descriptor(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
811 | { | ||
812 | // TODO: implement | ||
813 | // there will be an OUT data stage (the descriptor to set) | ||
814 | return DATA_STAGE; | ||
815 | } | ||
816 | |||
817 | static ep0_stage_t | ||
818 | do_get_configuration(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
819 | { | ||
820 | // send dev->configuration | ||
821 | dbg("sending config"); | ||
822 | send_packet(dev, alloc_packet(&dev->ep[0], 1, &dev->configuration), | ||
823 | 0); | ||
824 | return STATUS_STAGE; | ||
825 | } | ||
826 | |||
827 | static ep0_stage_t | ||
828 | do_set_configuration(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
829 | { | ||
830 | // set active config to low-byte of setup->wValue | ||
831 | dev->configuration = le16_to_cpu(setup->wValue) & 0xff; | ||
832 | dbg("set config, config=%d", dev->configuration); | ||
833 | if (!dev->configuration && dev->state > DEFAULT) { | ||
834 | dev->state = ADDRESS; | ||
835 | /* inform function layer of usbdev state change */ | ||
836 | dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data); | ||
837 | } else if (dev->configuration == 1) { | ||
838 | dev->state = CONFIGURED; | ||
839 | /* inform function layer of usbdev state change */ | ||
840 | dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data); | ||
841 | } else { | ||
842 | // FIXME: "respond with request error" - how? | ||
843 | } | ||
844 | |||
845 | return SETUP_STAGE; | ||
846 | } | ||
847 | |||
848 | static ep0_stage_t | ||
849 | do_get_interface(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
850 | { | ||
851 | // interface must be zero. | ||
852 | if ((le16_to_cpu(setup->wIndex) & 0xff) || dev->state == ADDRESS) { | ||
853 | // FIXME: respond with "request error". how? | ||
854 | } else if (dev->state == CONFIGURED) { | ||
855 | // send dev->alternate_setting | ||
856 | dbg("sending alt setting"); | ||
857 | send_packet(dev, alloc_packet(&dev->ep[0], 1, | ||
858 | &dev->alternate_setting), 0); | ||
859 | } | ||
860 | |||
861 | return STATUS_STAGE; | ||
862 | |||
863 | } | ||
864 | |||
865 | static ep0_stage_t | ||
866 | do_set_interface(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
867 | { | ||
868 | if (dev->state == ADDRESS) { | ||
869 | // FIXME: respond with "request error". how? | ||
870 | } else if (dev->state == CONFIGURED) { | ||
871 | dev->interface = le16_to_cpu(setup->wIndex) & 0xff; | ||
872 | dev->alternate_setting = | ||
873 | le16_to_cpu(setup->wValue) & 0xff; | ||
874 | // interface and alternate_setting must be zero | ||
875 | if (dev->interface || dev->alternate_setting) { | ||
876 | // FIXME: respond with "request error". how? | ||
877 | } | ||
878 | } | ||
879 | |||
880 | return SETUP_STAGE; | ||
881 | } | ||
882 | |||
883 | static ep0_stage_t | ||
884 | do_synch_frame(struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
885 | { | ||
886 | // TODO | ||
887 | return SETUP_STAGE; | ||
888 | } | ||
889 | |||
890 | typedef ep0_stage_t (*req_method_t)(struct usb_dev* dev, | ||
891 | struct usb_ctrlrequest* setup); | ||
892 | |||
893 | |||
894 | /* Table of the standard device request handlers */ | ||
895 | static const req_method_t req_method[] = { | ||
896 | do_get_status, | ||
897 | do_clear_feature, | ||
898 | do_reserved, | ||
899 | do_set_feature, | ||
900 | do_reserved, | ||
901 | do_set_address, | ||
902 | do_get_descriptor, | ||
903 | do_set_descriptor, | ||
904 | do_get_configuration, | ||
905 | do_set_configuration, | ||
906 | do_get_interface, | ||
907 | do_set_interface, | ||
908 | do_synch_frame | ||
909 | }; | ||
910 | |||
911 | |||
912 | // SETUP packet request dispatcher | ||
913 | static void | ||
914 | do_setup (struct usb_dev* dev, struct usb_ctrlrequest* setup) | ||
915 | { | ||
916 | req_method_t m; | ||
917 | |||
918 | dbg("%s: req %d %s", __FUNCTION__, setup->bRequestType, | ||
919 | get_std_req_name(setup->bRequestType)); | ||
920 | |||
921 | if ((setup->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD || | ||
922 | (setup->bRequestType & USB_RECIP_MASK) != USB_RECIP_DEVICE) { | ||
923 | err("%s: invalid requesttype 0x%02x", __FUNCTION__, | ||
924 | setup->bRequestType); | ||
925 | return; | ||
926 | } | ||
927 | |||
928 | if ((setup->bRequestType & 0x80) == USB_DIR_OUT && setup->wLength) | ||
929 | dbg("%s: OUT phase! length=%d", __FUNCTION__, setup->wLength); | ||
930 | |||
931 | if (setup->bRequestType < sizeof(req_method)/sizeof(req_method_t)) | ||
932 | m = req_method[setup->bRequestType]; | ||
933 | else | ||
934 | m = do_reserved; | ||
935 | |||
936 | dev->ep0_stage = (*m)(dev, setup); | ||
937 | } | ||
938 | |||
939 | /* | ||
940 | * A SETUP, DATA0, or DATA1 packet has been received | ||
941 | * on the default control endpoint's fifo. | ||
942 | */ | ||
943 | static void | ||
944 | process_ep0_receive (struct usb_dev* dev) | ||
945 | { | ||
946 | endpoint_t *ep0 = &dev->ep[0]; | ||
947 | usbdev_pkt_t *pkt; | ||
948 | |||
949 | spin_lock(&ep0->lock); | ||
950 | |||
951 | // complete packet and prepare a new packet | ||
952 | pkt = receive_packet_complete(ep0); | ||
953 | if (!pkt) { | ||
954 | // FIXME: should put a warn/err here. | ||
955 | spin_unlock(&ep0->lock); | ||
956 | return; | ||
957 | } | ||
958 | |||
959 | // unlink immediately from endpoint. | ||
960 | unlink_head(&ep0->outlist); | ||
961 | |||
962 | // override current stage if h/w says it's a setup packet | ||
963 | if (pkt->status & PKT_STATUS_SU) | ||
964 | dev->ep0_stage = SETUP_STAGE; | ||
965 | |||
966 | switch (dev->ep0_stage) { | ||
967 | case SETUP_STAGE: | ||
968 | vdbg("SU bit is %s in setup stage", | ||
969 | (pkt->status & PKT_STATUS_SU) ? "set" : "not set"); | ||
970 | |||
971 | if (pkt->size == sizeof(struct usb_ctrlrequest)) { | ||
972 | #ifdef VDEBUG | ||
973 | if (pkt->status & PKT_STATUS_ACK) | ||
974 | vdbg("received SETUP"); | ||
975 | else | ||
976 | vdbg("received NAK SETUP"); | ||
977 | #endif | ||
978 | do_setup(dev, (struct usb_ctrlrequest*)pkt->payload); | ||
979 | } else | ||
980 | err("%s: wrong size SETUP received", __FUNCTION__); | ||
981 | break; | ||
982 | case DATA_STAGE: | ||
983 | /* | ||
984 | * this setup has an OUT data stage. Of the standard | ||
985 | * device requests, only set_descriptor has this stage, | ||
986 | * so this packet is that descriptor. TODO: drop it for | ||
987 | * now, set_descriptor not implemented. | ||
988 | * | ||
989 | * Need to place a byte in the write FIFO here, to prepare | ||
990 | * to send a zero-length DATA ack packet to the host in the | ||
991 | * STATUS stage. | ||
992 | */ | ||
993 | au_writel(0, ep0->reg->write_fifo); | ||
994 | dbg("received OUT stage DATAx on EP0, size=%d", pkt->size); | ||
995 | dev->ep0_stage = SETUP_STAGE; | ||
996 | break; | ||
997 | case STATUS_STAGE: | ||
998 | // this setup had an IN data stage, and host is ACK'ing | ||
999 | // the packet we sent during that stage. | ||
1000 | if (pkt->size != 0) | ||
1001 | warn("received non-zero ACK on EP0??"); | ||
1002 | #ifdef VDEBUG | ||
1003 | else | ||
1004 | vdbg("received ACK on EP0"); | ||
1005 | #endif | ||
1006 | dev->ep0_stage = SETUP_STAGE; | ||
1007 | break; | ||
1008 | } | ||
1009 | |||
1010 | spin_unlock(&ep0->lock); | ||
1011 | // we're done processing the packet, free it | ||
1012 | kfree(pkt); | ||
1013 | } | ||
1014 | |||
1015 | |||
1016 | /* | ||
1017 | * A DATA0/1 packet has been received on one of the OUT endpoints (4 or 5) | ||
1018 | */ | ||
1019 | static void | ||
1020 | process_ep_receive (struct usb_dev* dev, endpoint_t *ep) | ||
1021 | { | ||
1022 | usbdev_pkt_t *pkt; | ||
1023 | |||
1024 | spin_lock(&ep->lock); | ||
1025 | pkt = receive_packet_complete(ep); | ||
1026 | spin_unlock(&ep->lock); | ||
1027 | |||
1028 | dev->func_cb(CB_PKT_COMPLETE, (unsigned long)pkt, dev->cb_data); | ||
1029 | } | ||
1030 | |||
1031 | |||
1032 | |||
1033 | /* This ISR handles the receive complete and suspend events */ | ||
1034 | static void req_sus_intr (int irq, void *dev_id) | ||
1035 | { | ||
1036 | struct usb_dev *dev = (struct usb_dev *) dev_id; | ||
1037 | u32 status; | ||
1038 | |||
1039 | status = au_readl(USBD_INTSTAT); | ||
1040 | au_writel(status, USBD_INTSTAT); // ack'em | ||
1041 | |||
1042 | if (status & (1<<0)) | ||
1043 | process_ep0_receive(dev); | ||
1044 | if (status & (1<<4)) | ||
1045 | process_ep_receive(dev, &dev->ep[4]); | ||
1046 | if (status & (1<<5)) | ||
1047 | process_ep_receive(dev, &dev->ep[5]); | ||
1048 | } | ||
1049 | |||
1050 | |||
1051 | /* This ISR handles the DMA done events on EP0 */ | ||
1052 | static void dma_done_ep0_intr(int irq, void *dev_id) | ||
1053 | { | ||
1054 | struct usb_dev *dev = (struct usb_dev *) dev_id; | ||
1055 | usbdev_pkt_t* pkt; | ||
1056 | endpoint_t *ep0 = &dev->ep[0]; | ||
1057 | u32 cs0, buff_done; | ||
1058 | |||
1059 | spin_lock(&ep0->lock); | ||
1060 | cs0 = au_readl(ep0->reg->ctrl_stat); | ||
1061 | |||
1062 | // first check packet transmit done | ||
1063 | if ((buff_done = get_dma_buffer_done(ep0->indma)) != 0) { | ||
1064 | // transmitted a DATAx packet during DATA stage | ||
1065 | // on control endpoint 0 | ||
1066 | // clear DMA done bit | ||
1067 | if (buff_done & DMA_D0) | ||
1068 | clear_dma_done0(ep0->indma); | ||
1069 | if (buff_done & DMA_D1) | ||
1070 | clear_dma_done1(ep0->indma); | ||
1071 | |||
1072 | pkt = send_packet_complete(ep0); | ||
1073 | kfree(pkt); | ||
1074 | } | ||
1075 | |||
1076 | /* | ||
1077 | * Now check packet receive done. Shouldn't get these, | ||
1078 | * the receive packet complete intr should happen | ||
1079 | * before the DMA done intr occurs. | ||
1080 | */ | ||
1081 | if ((buff_done = get_dma_buffer_done(ep0->outdma)) != 0) { | ||
1082 | // clear DMA done bit | ||
1083 | if (buff_done & DMA_D0) | ||
1084 | clear_dma_done0(ep0->outdma); | ||
1085 | if (buff_done & DMA_D1) | ||
1086 | clear_dma_done1(ep0->outdma); | ||
1087 | |||
1088 | //process_ep0_receive(dev); | ||
1089 | } | ||
1090 | |||
1091 | spin_unlock(&ep0->lock); | ||
1092 | } | ||
1093 | |||
1094 | /* This ISR handles the DMA done events on endpoints 2,3,4,5 */ | ||
1095 | static void dma_done_ep_intr(int irq, void *dev_id) | ||
1096 | { | ||
1097 | struct usb_dev *dev = (struct usb_dev *) dev_id; | ||
1098 | int i; | ||
1099 | |||
1100 | for (i = 2; i < 6; i++) { | ||
1101 | u32 buff_done; | ||
1102 | usbdev_pkt_t* pkt; | ||
1103 | endpoint_t *ep = &dev->ep[i]; | ||
1104 | |||
1105 | if (!ep->active) continue; | ||
1106 | |||
1107 | spin_lock(&ep->lock); | ||
1108 | |||
1109 | if (ep->direction == USB_DIR_IN) { | ||
1110 | buff_done = get_dma_buffer_done(ep->indma); | ||
1111 | if (buff_done != 0) { | ||
1112 | // transmitted a DATAx pkt on the IN ep | ||
1113 | // clear DMA done bit | ||
1114 | if (buff_done & DMA_D0) | ||
1115 | clear_dma_done0(ep->indma); | ||
1116 | if (buff_done & DMA_D1) | ||
1117 | clear_dma_done1(ep->indma); | ||
1118 | |||
1119 | pkt = send_packet_complete(ep); | ||
1120 | |||
1121 | spin_unlock(&ep->lock); | ||
1122 | dev->func_cb(CB_PKT_COMPLETE, | ||
1123 | (unsigned long)pkt, | ||
1124 | dev->cb_data); | ||
1125 | spin_lock(&ep->lock); | ||
1126 | } | ||
1127 | } else { | ||
1128 | /* | ||
1129 | * Check packet receive done (OUT ep). Shouldn't get | ||
1130 | * these, the rx packet complete intr should happen | ||
1131 | * before the DMA done intr occurs. | ||
1132 | */ | ||
1133 | buff_done = get_dma_buffer_done(ep->outdma); | ||
1134 | if (buff_done != 0) { | ||
1135 | // received a DATAx pkt on the OUT ep | ||
1136 | // clear DMA done bit | ||
1137 | if (buff_done & DMA_D0) | ||
1138 | clear_dma_done0(ep->outdma); | ||
1139 | if (buff_done & DMA_D1) | ||
1140 | clear_dma_done1(ep->outdma); | ||
1141 | |||
1142 | //process_ep_receive(dev, ep); | ||
1143 | } | ||
1144 | } | ||
1145 | |||
1146 | spin_unlock(&ep->lock); | ||
1147 | } | ||
1148 | } | ||
1149 | |||
1150 | |||
1151 | /*************************************************************************** | ||
1152 | * Here begins the external interface functions | ||
1153 | *************************************************************************** | ||
1154 | */ | ||
1155 | |||
1156 | /* | ||
1157 | * allocate a new packet | ||
1158 | */ | ||
1159 | int | ||
1160 | usbdev_alloc_packet(int ep_addr, int data_size, usbdev_pkt_t** pkt) | ||
1161 | { | ||
1162 | endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr); | ||
1163 | usbdev_pkt_t* lpkt = NULL; | ||
1164 | |||
1165 | if (!ep || !ep->active || ep->address < 2) | ||
1166 | return -ENODEV; | ||
1167 | if (data_size > ep->max_pkt_size) | ||
1168 | return -EINVAL; | ||
1169 | |||
1170 | lpkt = *pkt = alloc_packet(ep, data_size, NULL); | ||
1171 | if (!lpkt) | ||
1172 | return -ENOMEM; | ||
1173 | return 0; | ||
1174 | } | ||
1175 | |||
1176 | |||
1177 | /* | ||
1178 | * packet send | ||
1179 | */ | ||
1180 | int | ||
1181 | usbdev_send_packet(int ep_addr, usbdev_pkt_t * pkt) | ||
1182 | { | ||
1183 | unsigned long flags; | ||
1184 | int count; | ||
1185 | endpoint_t * ep; | ||
1186 | |||
1187 | if (!pkt || !(ep = epaddr_to_ep(&usbdev, pkt->ep_addr)) || | ||
1188 | !ep->active || ep->address < 2) | ||
1189 | return -ENODEV; | ||
1190 | if (ep->direction != USB_DIR_IN) | ||
1191 | return -EINVAL; | ||
1192 | |||
1193 | spin_lock_irqsave(&ep->lock, flags); | ||
1194 | count = send_packet(&usbdev, pkt, 1); | ||
1195 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1196 | |||
1197 | return count; | ||
1198 | } | ||
1199 | |||
1200 | /* | ||
1201 | * packet receive | ||
1202 | */ | ||
1203 | int | ||
1204 | usbdev_receive_packet(int ep_addr, usbdev_pkt_t** pkt) | ||
1205 | { | ||
1206 | unsigned long flags; | ||
1207 | usbdev_pkt_t* lpkt = NULL; | ||
1208 | endpoint_t *ep = epaddr_to_ep(&usbdev, ep_addr); | ||
1209 | |||
1210 | if (!ep || !ep->active || ep->address < 2) | ||
1211 | return -ENODEV; | ||
1212 | if (ep->direction != USB_DIR_OUT) | ||
1213 | return -EINVAL; | ||
1214 | |||
1215 | spin_lock_irqsave(&ep->lock, flags); | ||
1216 | if (ep->outlist.count > 1) | ||
1217 | lpkt = unlink_head(&ep->outlist); | ||
1218 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1219 | |||
1220 | if (!lpkt) { | ||
1221 | /* no packet available */ | ||
1222 | *pkt = NULL; | ||
1223 | return -ENODATA; | ||
1224 | } | ||
1225 | |||
1226 | *pkt = lpkt; | ||
1227 | |||
1228 | return lpkt->size; | ||
1229 | } | ||
1230 | |||
1231 | |||
1232 | /* | ||
1233 | * return total queued byte count on the endpoint. | ||
1234 | */ | ||
1235 | int | ||
1236 | usbdev_get_byte_count(int ep_addr) | ||
1237 | { | ||
1238 | unsigned long flags; | ||
1239 | pkt_list_t *list; | ||
1240 | usbdev_pkt_t *scan; | ||
1241 | int count = 0; | ||
1242 | endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr); | ||
1243 | |||
1244 | if (!ep || !ep->active || ep->address < 2) | ||
1245 | return -ENODEV; | ||
1246 | |||
1247 | if (ep->direction == USB_DIR_IN) { | ||
1248 | list = &ep->inlist; | ||
1249 | |||
1250 | spin_lock_irqsave(&ep->lock, flags); | ||
1251 | for (scan = list->head; scan; scan = scan->next) | ||
1252 | count += scan->size; | ||
1253 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1254 | } else { | ||
1255 | list = &ep->outlist; | ||
1256 | |||
1257 | spin_lock_irqsave(&ep->lock, flags); | ||
1258 | if (list->count > 1) { | ||
1259 | for (scan = list->head; scan != list->tail; | ||
1260 | scan = scan->next) | ||
1261 | count += scan->size; | ||
1262 | } | ||
1263 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1264 | } | ||
1265 | |||
1266 | return count; | ||
1267 | } | ||
1268 | |||
1269 | |||
1270 | void | ||
1271 | usbdev_exit(void) | ||
1272 | { | ||
1273 | endpoint_t *ep; | ||
1274 | int i; | ||
1275 | |||
1276 | au_writel(0, USBD_INTEN); // disable usb dev ints | ||
1277 | au_writel(0, USBD_ENABLE); // disable usb dev | ||
1278 | |||
1279 | free_irq(AU1000_USB_DEV_REQ_INT, &usbdev); | ||
1280 | free_irq(AU1000_USB_DEV_SUS_INT, &usbdev); | ||
1281 | |||
1282 | // free all control endpoint resources | ||
1283 | ep = &usbdev.ep[0]; | ||
1284 | free_au1000_dma(ep->indma); | ||
1285 | free_au1000_dma(ep->outdma); | ||
1286 | endpoint_flush(ep); | ||
1287 | |||
1288 | // free ep resources | ||
1289 | for (i = 2; i < 6; i++) { | ||
1290 | ep = &usbdev.ep[i]; | ||
1291 | if (!ep->active) continue; | ||
1292 | |||
1293 | if (ep->direction == USB_DIR_IN) { | ||
1294 | free_au1000_dma(ep->indma); | ||
1295 | } else { | ||
1296 | free_au1000_dma(ep->outdma); | ||
1297 | } | ||
1298 | endpoint_flush(ep); | ||
1299 | } | ||
1300 | |||
1301 | kfree(usbdev.full_conf_desc); | ||
1302 | } | ||
1303 | |||
1304 | int | ||
1305 | usbdev_init(struct usb_device_descriptor* dev_desc, | ||
1306 | struct usb_config_descriptor* config_desc, | ||
1307 | struct usb_interface_descriptor* if_desc, | ||
1308 | struct usb_endpoint_descriptor* ep_desc, | ||
1309 | struct usb_string_descriptor* str_desc[], | ||
1310 | void (*cb)(usbdev_cb_type_t, unsigned long, void *), | ||
1311 | void* cb_data) | ||
1312 | { | ||
1313 | endpoint_t *ep0; | ||
1314 | int i, ret=0; | ||
1315 | u8* fcd; | ||
1316 | |||
1317 | if (dev_desc->bNumConfigurations > 1 || | ||
1318 | config_desc->bNumInterfaces > 1 || | ||
1319 | if_desc->bNumEndpoints > 4) { | ||
1320 | err("Only one config, one i/f, and no more " | ||
1321 | "than 4 ep's allowed"); | ||
1322 | ret = -EINVAL; | ||
1323 | goto out; | ||
1324 | } | ||
1325 | |||
1326 | if (!cb) { | ||
1327 | err("Function-layer callback required"); | ||
1328 | ret = -EINVAL; | ||
1329 | goto out; | ||
1330 | } | ||
1331 | |||
1332 | if (dev_desc->bMaxPacketSize0 != USBDEV_EP0_MAX_PACKET_SIZE) { | ||
1333 | warn("EP0 Max Packet size must be %d", | ||
1334 | USBDEV_EP0_MAX_PACKET_SIZE); | ||
1335 | dev_desc->bMaxPacketSize0 = USBDEV_EP0_MAX_PACKET_SIZE; | ||
1336 | } | ||
1337 | |||
1338 | memset(&usbdev, 0, sizeof(struct usb_dev)); | ||
1339 | |||
1340 | usbdev.state = DEFAULT; | ||
1341 | usbdev.dev_desc = dev_desc; | ||
1342 | usbdev.if_desc = if_desc; | ||
1343 | usbdev.conf_desc = config_desc; | ||
1344 | for (i=0; i<6; i++) | ||
1345 | usbdev.str_desc[i] = str_desc[i]; | ||
1346 | usbdev.func_cb = cb; | ||
1347 | usbdev.cb_data = cb_data; | ||
1348 | |||
1349 | /* Initialize default control endpoint */ | ||
1350 | ep0 = &usbdev.ep[0]; | ||
1351 | ep0->active = 1; | ||
1352 | ep0->type = CONTROL_EP; | ||
1353 | ep0->max_pkt_size = USBDEV_EP0_MAX_PACKET_SIZE; | ||
1354 | spin_lock_init(&ep0->lock); | ||
1355 | ep0->desc = NULL; // ep0 has no descriptor | ||
1356 | ep0->address = 0; | ||
1357 | ep0->direction = 0; | ||
1358 | ep0->reg = &ep_reg[0]; | ||
1359 | |||
1360 | /* Initialize the other requested endpoints */ | ||
1361 | for (i = 0; i < if_desc->bNumEndpoints; i++) { | ||
1362 | struct usb_endpoint_descriptor* epd = &ep_desc[i]; | ||
1363 | endpoint_t *ep; | ||
1364 | |||
1365 | if ((epd->bEndpointAddress & 0x80) == USB_DIR_IN) { | ||
1366 | ep = &usbdev.ep[2]; | ||
1367 | ep->address = 2; | ||
1368 | if (ep->active) { | ||
1369 | ep = &usbdev.ep[3]; | ||
1370 | ep->address = 3; | ||
1371 | if (ep->active) { | ||
1372 | err("too many IN ep's requested"); | ||
1373 | ret = -ENODEV; | ||
1374 | goto out; | ||
1375 | } | ||
1376 | } | ||
1377 | } else { | ||
1378 | ep = &usbdev.ep[4]; | ||
1379 | ep->address = 4; | ||
1380 | if (ep->active) { | ||
1381 | ep = &usbdev.ep[5]; | ||
1382 | ep->address = 5; | ||
1383 | if (ep->active) { | ||
1384 | err("too many OUT ep's requested"); | ||
1385 | ret = -ENODEV; | ||
1386 | goto out; | ||
1387 | } | ||
1388 | } | ||
1389 | } | ||
1390 | |||
1391 | ep->active = 1; | ||
1392 | epd->bEndpointAddress &= ~0x0f; | ||
1393 | epd->bEndpointAddress |= (u8)ep->address; | ||
1394 | ep->direction = epd->bEndpointAddress & 0x80; | ||
1395 | ep->type = epd->bmAttributes & 0x03; | ||
1396 | ep->max_pkt_size = le16_to_cpu(epd->wMaxPacketSize); | ||
1397 | spin_lock_init(&ep->lock); | ||
1398 | ep->desc = epd; | ||
1399 | ep->reg = &ep_reg[ep->address]; | ||
1400 | } | ||
1401 | |||
1402 | /* | ||
1403 | * initialize the full config descriptor | ||
1404 | */ | ||
1405 | usbdev.full_conf_desc = fcd = kmalloc(le16_to_cpu(config_desc->wTotalLength), | ||
1406 | ALLOC_FLAGS); | ||
1407 | if (!fcd) { | ||
1408 | err("failed to alloc full config descriptor"); | ||
1409 | ret = -ENOMEM; | ||
1410 | goto out; | ||
1411 | } | ||
1412 | |||
1413 | memcpy(fcd, config_desc, USB_DT_CONFIG_SIZE); | ||
1414 | fcd += USB_DT_CONFIG_SIZE; | ||
1415 | memcpy(fcd, if_desc, USB_DT_INTERFACE_SIZE); | ||
1416 | fcd += USB_DT_INTERFACE_SIZE; | ||
1417 | for (i = 0; i < if_desc->bNumEndpoints; i++) { | ||
1418 | memcpy(fcd, &ep_desc[i], USB_DT_ENDPOINT_SIZE); | ||
1419 | fcd += USB_DT_ENDPOINT_SIZE; | ||
1420 | } | ||
1421 | |||
1422 | /* Now we're ready to enable the controller */ | ||
1423 | au_writel(0x0002, USBD_ENABLE); | ||
1424 | udelay(100); | ||
1425 | au_writel(0x0003, USBD_ENABLE); | ||
1426 | udelay(100); | ||
1427 | |||
1428 | /* build and send config table based on ep descriptors */ | ||
1429 | for (i = 0; i < 6; i++) { | ||
1430 | endpoint_t *ep; | ||
1431 | if (i == 1) | ||
1432 | continue; // skip dummy ep | ||
1433 | ep = &usbdev.ep[i]; | ||
1434 | if (ep->active) { | ||
1435 | au_writel((ep->address << 4) | 0x04, USBD_CONFIG); | ||
1436 | au_writel(((ep->max_pkt_size & 0x380) >> 7) | | ||
1437 | (ep->direction >> 4) | (ep->type << 4), | ||
1438 | USBD_CONFIG); | ||
1439 | au_writel((ep->max_pkt_size & 0x7f) << 1, USBD_CONFIG); | ||
1440 | au_writel(0x00, USBD_CONFIG); | ||
1441 | au_writel(ep->address, USBD_CONFIG); | ||
1442 | } else { | ||
1443 | u8 dir = (i==2 || i==3) ? DIR_IN : DIR_OUT; | ||
1444 | au_writel((i << 4) | 0x04, USBD_CONFIG); | ||
1445 | au_writel(((16 & 0x380) >> 7) | dir | | ||
1446 | (BULK_EP << 4), USBD_CONFIG); | ||
1447 | au_writel((16 & 0x7f) << 1, USBD_CONFIG); | ||
1448 | au_writel(0x00, USBD_CONFIG); | ||
1449 | au_writel(i, USBD_CONFIG); | ||
1450 | } | ||
1451 | } | ||
1452 | |||
1453 | /* | ||
1454 | * Enable Receive FIFO Complete interrupts only. Transmit | ||
1455 | * complete is being handled by the DMA done interrupts. | ||
1456 | */ | ||
1457 | au_writel(0x31, USBD_INTEN); | ||
1458 | |||
1459 | /* | ||
1460 | * Controller is now enabled, request DMA and IRQ | ||
1461 | * resources. | ||
1462 | */ | ||
1463 | |||
1464 | /* request the USB device transfer complete interrupt */ | ||
1465 | if (request_irq(AU1000_USB_DEV_REQ_INT, req_sus_intr, IRQF_DISABLED, | ||
1466 | "USBdev req", &usbdev)) { | ||
1467 | err("Can't get device request intr"); | ||
1468 | ret = -ENXIO; | ||
1469 | goto out; | ||
1470 | } | ||
1471 | /* request the USB device suspend interrupt */ | ||
1472 | if (request_irq(AU1000_USB_DEV_SUS_INT, req_sus_intr, IRQF_DISABLED, | ||
1473 | "USBdev sus", &usbdev)) { | ||
1474 | err("Can't get device suspend intr"); | ||
1475 | ret = -ENXIO; | ||
1476 | goto out; | ||
1477 | } | ||
1478 | |||
1479 | /* Request EP0 DMA and IRQ */ | ||
1480 | if ((ep0->indma = request_au1000_dma(ep_dma_id[0].id, | ||
1481 | ep_dma_id[0].str, | ||
1482 | dma_done_ep0_intr, | ||
1483 | IRQF_DISABLED, | ||
1484 | &usbdev)) < 0) { | ||
1485 | err("Can't get %s DMA", ep_dma_id[0].str); | ||
1486 | ret = -ENXIO; | ||
1487 | goto out; | ||
1488 | } | ||
1489 | if ((ep0->outdma = request_au1000_dma(ep_dma_id[1].id, | ||
1490 | ep_dma_id[1].str, | ||
1491 | NULL, 0, NULL)) < 0) { | ||
1492 | err("Can't get %s DMA", ep_dma_id[1].str); | ||
1493 | ret = -ENXIO; | ||
1494 | goto out; | ||
1495 | } | ||
1496 | |||
1497 | // Flush the ep0 buffers and FIFOs | ||
1498 | endpoint_flush(ep0); | ||
1499 | // start packet reception on ep0 | ||
1500 | kickstart_receive_packet(ep0); | ||
1501 | |||
1502 | /* Request DMA and IRQ for the other endpoints */ | ||
1503 | for (i = 2; i < 6; i++) { | ||
1504 | endpoint_t *ep = &usbdev.ep[i]; | ||
1505 | if (!ep->active) | ||
1506 | continue; | ||
1507 | |||
1508 | // Flush the endpoint buffers and FIFOs | ||
1509 | endpoint_flush(ep); | ||
1510 | |||
1511 | if (ep->direction == USB_DIR_IN) { | ||
1512 | ep->indma = | ||
1513 | request_au1000_dma(ep_dma_id[ep->address].id, | ||
1514 | ep_dma_id[ep->address].str, | ||
1515 | dma_done_ep_intr, | ||
1516 | IRQF_DISABLED, | ||
1517 | &usbdev); | ||
1518 | if (ep->indma < 0) { | ||
1519 | err("Can't get %s DMA", | ||
1520 | ep_dma_id[ep->address].str); | ||
1521 | ret = -ENXIO; | ||
1522 | goto out; | ||
1523 | } | ||
1524 | } else { | ||
1525 | ep->outdma = | ||
1526 | request_au1000_dma(ep_dma_id[ep->address].id, | ||
1527 | ep_dma_id[ep->address].str, | ||
1528 | NULL, 0, NULL); | ||
1529 | if (ep->outdma < 0) { | ||
1530 | err("Can't get %s DMA", | ||
1531 | ep_dma_id[ep->address].str); | ||
1532 | ret = -ENXIO; | ||
1533 | goto out; | ||
1534 | } | ||
1535 | |||
1536 | // start packet reception on OUT endpoint | ||
1537 | kickstart_receive_packet(ep); | ||
1538 | } | ||
1539 | } | ||
1540 | |||
1541 | out: | ||
1542 | if (ret) | ||
1543 | usbdev_exit(); | ||
1544 | return ret; | ||
1545 | } | ||
1546 | |||
1547 | EXPORT_SYMBOL(usbdev_init); | ||
1548 | EXPORT_SYMBOL(usbdev_exit); | ||
1549 | EXPORT_SYMBOL(usbdev_alloc_packet); | ||
1550 | EXPORT_SYMBOL(usbdev_receive_packet); | ||
1551 | EXPORT_SYMBOL(usbdev_send_packet); | ||
1552 | EXPORT_SYMBOL(usbdev_get_byte_count); | ||