blob: b0bf7a826e724827f476df1b884182a37b5f4a1a [file] [log] [blame]
Brian Swetland3e7e21a2009-01-19 19:41:24 -08001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <string.h>
30#include <stdlib.h>
31#include <debug.h>
32#include <platform/iomap.h>
33#include <platform/irqs.h>
34#include <platform/interrupts.h>
35#include <kernel/thread.h>
36#include <reg.h>
37
38#include <dev/udc.h>
39
40#include "hsusb.h"
41
42/* common code - factor out into a shared file */
43
44struct udc_descriptor {
45 struct udc_descriptor *next;
46 unsigned short tag; /* ((TYPE << 8) | NUM) */
47 unsigned short len; /* total length */
48 unsigned char data[0];
49};
50
51struct udc_descriptor *udc_descriptor_alloc(unsigned type, unsigned num, unsigned len)
52{
53 struct udc_descriptor *desc;
54 if ((len > 255) || (len < 2) || (num > 255) || (type > 255))
55 return 0;
56
57 if(!(desc = malloc(sizeof(struct udc_descriptor) + len)))
58 return 0;
59
60 desc->next = 0;
61 desc->tag = (type << 8) | num;
62 desc->len = len;
63 desc->data[0] = len;
64 desc->data[1] = type;
65
66 return desc;
67}
68
69static struct udc_descriptor *desc_list = 0;
70static unsigned next_string_id = 1;
71
72void udc_descriptor_register(struct udc_descriptor *desc)
73{
74 desc->next = desc_list;
75 desc_list = desc;
76}
77
78unsigned udc_string_desc_alloc(const char *str)
79{
80 unsigned len;
81 struct udc_descriptor *desc;
82 unsigned char *data;
83
84 if (next_string_id > 255)
85 return 0;
86
87 if (!str)
88 return 0;
89
90 len = strlen(str);
91 desc = udc_descriptor_alloc(TYPE_STRING, next_string_id, len * 2 + 2);
92 if (!desc)
93 return 0;
94 next_string_id++;
95
96 /* expand ascii string to utf16 */
97 data = desc->data + 2;
98 while (len-- > 0) {
99 *data++ = *str++;
100 *data++ = 0;
101 }
102
103 udc_descriptor_register(desc);
104 return desc->tag & 0xff;
105}
106
107/* end of common code */
108
109void hsusb_clock_init(void);
110
111#if 1
112#define DBG(x...) do {} while(0)
113#else
114#define DBG(x...) dprintf(INFO, x)
115#endif
116
117#define DBG1(x...) dprintf(INFO, x)
118
119#define usb_status(a,b)
120
121struct usb_request {
122 struct udc_request req;
123 struct ept_queue_item *item;
124};
125
126struct udc_endpoint
127{
128 struct udc_endpoint *next;
129 unsigned bit;
130 struct ept_queue_head *head;
131 struct usb_request *req;
132 unsigned char num;
133 unsigned char in;
134 unsigned short maxpkt;
135};
136
137struct udc_endpoint *ept_list = 0;
138struct ept_queue_head *epts = 0;
139
140static int usb_online = 0;
141static int usb_highspeed = 0;
142
143static struct udc_device *the_device;
144static struct udc_gadget *the_gadget;
145
146struct udc_endpoint *_udc_endpoint_alloc(unsigned num, unsigned in, unsigned max_pkt)
147{
148 struct udc_endpoint *ept;
149 unsigned cfg;
150
151 ept = malloc(sizeof(*ept));
152
153 ept->maxpkt = max_pkt;
154 ept->num = num;
155 ept->in = !!in;
156 ept->req = 0;
157
158 cfg = CONFIG_MAX_PKT(max_pkt) | CONFIG_ZLT;
159
160 if(ept->in) {
161 ept->bit = EPT_TX(ept->num);
162 } else {
163 ept->bit = EPT_RX(ept->num);
164 if(num == 0)
165 cfg |= CONFIG_IOS;
166 }
167
168 ept->head = epts + (num * 2) + (ept->in);
169 ept->head->config = cfg;
170
171 ept->next = ept_list;
172 ept_list = ept;
173
174// arch_clean_invalidate_cache_range(ept->head, 64);
175 DBG("ept%d %s @%p/%p max=%d bit=%x\n",
176 num, in ? "in":"out", ept, ept->head, max_pkt, ept->bit);
177
178 return ept;
179}
180
181static unsigned ept_alloc_table = EPT_TX(0) | EPT_RX(0);
182
183struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt)
184{
185 struct udc_endpoint *ept;
186 unsigned n;
187 unsigned in;
188
189 if (type == UDC_TYPE_BULK_IN) {
190 in = 1;
191 } else if (type == UDC_TYPE_BULK_OUT) {
192 in = 0;
193 } else {
194 return 0;
195 }
196
197 for (n = 1; n < 16; n++) {
198 unsigned bit = in ? EPT_TX(n) : EPT_RX(n);
199 if (ept_alloc_table & bit)
200 continue;
201 ept = _udc_endpoint_alloc(n, in, maxpkt);
202 if (ept)
203 ept_alloc_table |= bit;
204 return ept;
205 }
206 return 0;
207}
208
209void udc_endpoint_free(struct udc_endpoint *ept)
210{
211 /* todo */
212}
213
214static void endpoint_enable(struct udc_endpoint *ept, unsigned yes)
215{
216 unsigned n = readl(USB_ENDPTCTRL(ept->num));
217
218 if(yes) {
219 if(ept->in) {
220 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
221 } else {
222 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
223 }
224
225 if(ept->num != 0) {
226 /* XXX should be more dynamic... */
227 if(usb_highspeed) {
228 ept->head->config = CONFIG_MAX_PKT(512) | CONFIG_ZLT;
229 } else {
230 ept->head->config = CONFIG_MAX_PKT(64) | CONFIG_ZLT;
231 }
232 }
233 }
234 writel(n, USB_ENDPTCTRL(ept->num));
235}
236
237struct udc_request *udc_request_alloc(void)
238{
239 struct usb_request *req;
240 req = malloc(sizeof(*req));
241 req->req.buf = 0;
242 req->req.length = 0;
243 req->item = memalign(32, 32);
244 return &req->req;
245}
246
247void udc_request_free(struct udc_request *req)
248{
249 free(req);
250}
251
252int udc_request_queue(struct udc_endpoint *ept, struct udc_request *_req)
253{
254 struct usb_request *req = (struct usb_request *) _req;
255 struct ept_queue_item *item = req->item;
256 unsigned phys = (unsigned) req->req.buf;
257
258 item->next = TERMINATE;
259 item->info = INFO_BYTES(req->req.length) | INFO_IOC | INFO_ACTIVE;
260 item->page0 = phys;
261 item->page1 = (phys & 0xfffff000) + 0x1000;
262
263 enter_critical_section();
264 ept->head->next = (unsigned) item;
265 ept->head->info = 0;
266 ept->req = req;
267
268// arch_clean_invalidate_cache_range(item, 32);
269// arch_clean_invalidate_cache_range(ept->head, 64);
270// arch_clean_invalidate_cache_range(req->req.buf, req->req.length);
271 DBG("ept%d %s queue req=%p\n",
272 ept->num, ept->in ? "in" : "out", req);
273
274 writel(ept->bit, USB_ENDPTPRIME);
275 exit_critical_section();
276 return 0;
277}
278
279static void handle_ept_complete(struct udc_endpoint *ept)
280{
281 struct ept_queue_item *item;
282 unsigned actual;
283 int status;
284 struct usb_request *req;
285
286 DBG("ept%d %s complete req=%p\n",
287 ept->num, ept->in ? "in" : "out", ept->req);
288
289 req = ept->req;
290 if(req) {
291 ept->req = 0;
292
293 item = req->item;
294
295 /* For some reason we are getting the notification for
296 * transfer completion before the active bit has cleared.
297 * HACK: wait for the ACTIVE bit to clear:
298 */
299 while (readl(&(item->info)) & INFO_ACTIVE) ;
300
301// arch_clean_invalidate_cache_range(item, 32);
302// arch_clean_invalidate_cache_range(req->req.buf, req->req.length);
303
304 if(item->info & 0xff) {
305 actual = 0;
306 status = -1;
307 dprintf(INFO, "EP%d/%s FAIL nfo=%x pg0=%x\n",
308 ept->num, ept->in ? "in" : "out", item->info, item->page0);
309 } else {
310 actual = req->req.length - ((item->info >> 16) & 0x7fff);
311 status = 0;
312 }
313 if(req->req.complete)
314 req->req.complete(&req->req, actual, status);
315 }
316}
317
318static const char *reqname(unsigned r)
319{
320 switch(r) {
321 case GET_STATUS: return "GET_STATUS";
322 case CLEAR_FEATURE: return "CLEAR_FEATURE";
323 case SET_FEATURE: return "SET_FEATURE";
324 case SET_ADDRESS: return "SET_ADDRESS";
325 case GET_DESCRIPTOR: return "GET_DESCRIPTOR";
326 case SET_DESCRIPTOR: return "SET_DESCRIPTOR";
327 case GET_CONFIGURATION: return "GET_CONFIGURATION";
328 case SET_CONFIGURATION: return "SET_CONFIGURATION";
329 case GET_INTERFACE: return "GET_INTERFACE";
330 case SET_INTERFACE: return "SET_INTERFACE";
331 default: return "*UNKNOWN*";
332 }
333}
334
335static struct udc_endpoint *ep0in, *ep0out;
336static struct udc_request *ep0req;
337
338static void setup_ack(void)
339{
340 ep0req->complete = 0;
341 ep0req->length = 0;
342 udc_request_queue(ep0in, ep0req);
343}
344
345static void ep0in_complete(struct udc_request *req, unsigned actual, int status)
346{
347 DBG("ep0in_complete %p %d %d\n", req, actual, status);
348 if(status == 0) {
349 req->length = 0;
350 req->complete = 0;
351 udc_request_queue(ep0out, req);
352 }
353}
354
355static void setup_tx(void *buf, unsigned len)
356{
357 DBG("setup_tx %p %d\n", buf, len);
358 memcpy(ep0req->buf, buf, len);
359 ep0req->complete = ep0in_complete;
360 ep0req->length = len;
361 udc_request_queue(ep0in, ep0req);
362}
363
364static unsigned char usb_config_value = 0;
365
366#define SETUP(type,request) (((type) << 8) | (request))
367
368static void handle_setup(struct udc_endpoint *ept)
369{
370 struct setup_packet s;
371
372 memcpy(&s, ept->head->setup_data, sizeof(s));
373 writel(ept->bit, USB_ENDPTSETUPSTAT);
374
375#if 0
376 DBG("handle_setup type=0x%02x req=0x%02x val=%d idx=%d len=%d (%s)\n",
377 s.type, s.request, s.value, s.index, s.length,
378 reqname(s.request));
379#endif
380 switch (SETUP(s.type,s.request)) {
381 case SETUP(DEVICE_READ, GET_STATUS): {
382 unsigned zero = 0;
383 if (s.length == 2) {
384 setup_tx(&zero, 2);
385 return;
386 }
387 break;
388 }
389 case SETUP(DEVICE_READ, GET_DESCRIPTOR): {
390 struct udc_descriptor *desc;
391 /* usb_highspeed? */
392 for (desc = desc_list; desc; desc = desc->next) {
393 if (desc->tag == s.value) {
394 unsigned len = desc->len;
395 if (len > s.length) len = s.length;
396 setup_tx(desc->data, len);
397 return;
398 }
399 }
400 break;
401 }
402 case SETUP(DEVICE_READ, GET_CONFIGURATION):
403 /* disabling this causes data transaction failures on OSX. Why? */
404 if ((s.value == 0) && (s.index == 0) && (s.length == 1)) {
405 setup_tx(&usb_config_value, 1);
406 return;
407 }
408 break;
409 case SETUP(DEVICE_WRITE, SET_CONFIGURATION):
410 if (s.value == 1) {
411 struct udc_endpoint *ept;
412 /* enable endpoints */
413 for (ept = ept_list; ept; ept = ept->next){
414 if (ept->num == 0)
415 continue;
416 endpoint_enable(ept, s.value);
417 }
418 usb_config_value = 1;
419 the_gadget->notify(the_gadget, UDC_EVENT_ONLINE);
420 } else {
421 writel(0, USB_ENDPTCTRL(1));
422 usb_config_value = 0;
423 the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
424 }
425 setup_ack();
426 usb_online = s.value ? 1 : 0;
427 usb_status(s.value ? 1 : 0, usb_highspeed);
428 return;
429 case SETUP(DEVICE_WRITE, SET_ADDRESS):
430 /* write address delayed (will take effect
431 ** after the next IN txn)
432 */
433 writel((s.value << 25) | (1 << 24), USB_DEVICEADDR);
434 setup_ack();
435 return;
436 case SETUP(INTERFACE_WRITE, SET_INTERFACE):
437 /* if we ack this everything hangs */
438 /* per spec, STALL is valid if there is not alt func */
439 goto stall;
440 case SETUP(ENDPOINT_WRITE, CLEAR_FEATURE): {
441 struct udc_endpoint *ept;
442 unsigned num = s.index & 15;
443 unsigned in = !!(s.index & 0x80);
444
445 if ((s.value == 0) && (s.length == 0)) {
446 DBG("clr feat %d %d\n", num, in);
447 for (ept = ept_list; ept; ept = ept->next) {
448 if ((ept->num == num) && (ept->in == in)) {
449 endpoint_enable(ept, 1);
450 setup_ack();
451 return;
452 }
453 }
454 }
455 break;
456 }
457 }
458
459 dprintf(INFO, "STALL %s %d %d %d %d %d\n",
460 reqname(s.request),
461 s.type, s.request, s.value, s.index, s.length);
462
463stall:
464 writel((1<<16) | (1 << 0), USB_ENDPTCTRL(ept->num));
465}
466
467unsigned ulpi_read(unsigned reg)
468{
469 /* initiate read operation */
470 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
471 USB_ULPI_VIEWPORT);
472
473 /* wait for completion */
474 while(readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ;
475
476 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
477}
478
479void ulpi_write(unsigned val, unsigned reg)
480{
481 /* initiate write operation */
482 writel(ULPI_RUN | ULPI_WRITE |
483 ULPI_ADDR(reg) | ULPI_DATA(val),
484 USB_ULPI_VIEWPORT);
485
486 /* wait for completion */
487 while(readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ;
488}
Ajay Dudani5a1e3302009-12-05 13:19:17 -0800489
490
491void hsusb_clock_init(void)
Ajay Dudani232ce812009-12-02 00:14:11 -0800492{
Ajay Dudani5a1e3302009-12-05 13:19:17 -0800493 // Enable usb clocks from apps processor for 7x30.
494 // USB clocks already initialized for other targets
495 // so skipping proc comm call to enable usb clocks.
496#ifdef PLATFORM_MSM7X30
Ajay Dudani232ce812009-12-02 00:14:11 -0800497 writel(0x00000100, USBH_NS_REG);
498 writel(0x00000900, USBH_NS_REG);
499 writel(0x00000A00, USBH_NS_REG);
500 writel(0x00002A00, USBH_NS_REG);
Ajay Dudani232ce812009-12-02 00:14:11 -0800501#endif
Ajay Dudani5a1e3302009-12-05 13:19:17 -0800502}
Brian Swetland3e7e21a2009-01-19 19:41:24 -0800503
504void board_usb_init(void);
505void board_ulpi_init(void);
506
507int udc_init(struct udc_device *dev)
508{
509 hsusb_clock_init();
510
511 epts = memalign(4096, 4096);
512
513 dprintf(INFO, "USB init ept @ %p\n", epts);
514 memset(epts, 0, 32 * sizeof(struct ept_queue_head));
515
Ajay Dudani232ce812009-12-02 00:14:11 -0800516 //dprintf(INFO, "USB ID %08x\n", readl(USB_ID));
Brian Swetland3e7e21a2009-01-19 19:41:24 -0800517// board_usb_init();
Brian Swetland3e7e21a2009-01-19 19:41:24 -0800518
519 /* select ULPI phy */
520 writel(0x81000000, USB_PORTSC);
521
522 /* RESET */
523 writel(0x00080002, USB_USBCMD);
524
525 thread_sleep(20);
526
527// board_ulpi_init();
528
529// arch_clean_invalidate_cache_range(epts, 32 * sizeof(struct ept_queue_head));
530 writel((unsigned) epts, USB_ENDPOINTLISTADDR);
531
532 /* select DEVICE mode */
533 writel(0x02, USB_USBMODE);
534
535 writel(0xffffffff, USB_ENDPTFLUSH);
536 thread_sleep(20);
537
538 ep0out = _udc_endpoint_alloc(0, 0, 64);
539 ep0in = _udc_endpoint_alloc(0, 1, 64);
540 ep0req = udc_request_alloc();
541 ep0req->buf = malloc(4096);
542
543 {
544 /* create and register a language table descriptor */
545 /* language 0x0409 is US English */
546 struct udc_descriptor *desc = udc_descriptor_alloc(TYPE_STRING, 0, 4);
547 desc->data[2] = 0x09;
548 desc->data[3] = 0x04;
549 udc_descriptor_register(desc);
550 }
551
552 the_device = dev;
553 return 0;
554}
555
556enum handler_return udc_interrupt(void *arg)
557{
558 struct udc_endpoint *ept;
559 unsigned ret = INT_NO_RESCHEDULE;
560 unsigned n = readl(USB_USBSTS);
561 writel(n, USB_USBSTS);
562
563 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
564
565 if (n == 0)
566 return ret;
567
568 if (n & STS_URI) {
569 writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
570 writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
571 writel(0xffffffff, USB_ENDPTFLUSH);
572 writel(0, USB_ENDPTCTRL(1));
573 DBG1("-- reset --\n");
574 usb_online = 0;
575 usb_config_value = 0;
576 the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE);
577
578 /* error out any pending reqs */
579 for (ept = ept_list; ept; ept = ept->next) {
580 /* ensure that ept_complete considers
581 * this to be an error state
582 */
583 if (ept->req) {
584 ept->req->item->info = INFO_HALTED;
585 handle_ept_complete(ept);
586 }
587 }
588 usb_status(0, usb_highspeed);
589 }
590 if (n & STS_SLI) {
591 DBG1("-- suspend --\n");
592 }
593 if (n & STS_PCI) {
594 DBG1("-- portchange --\n");
595 unsigned spd = (readl(USB_PORTSC) >> 26) & 3;
596 if(spd == 2) {
597 usb_highspeed = 1;
598 } else {
599 usb_highspeed = 0;
600 }
601 }
602 if (n & STS_UEI) {
603 dprintf(INFO, "<UEI %x>\n", readl(USB_ENDPTCOMPLETE));
604 }
605#if 0
606 DBG("STS: ");
607 if (n & STS_UEI) DBG("ERROR ");
608 if (n & STS_SLI) DBG("SUSPEND ");
609 if (n & STS_URI) DBG("RESET ");
610 if (n & STS_PCI) DBG("PORTCHANGE ");
611 if (n & STS_UI) DBG("USB ");
612 DBG("\n");
613#endif
614 if ((n & STS_UI) || (n & STS_UEI)) {
615 n = readl(USB_ENDPTSETUPSTAT);
616 if (n & EPT_RX(0)) {
617 handle_setup(ep0out);
618 ret = INT_RESCHEDULE;
619 }
620
621 n = readl(USB_ENDPTCOMPLETE);
622 if (n != 0) {
623 writel(n, USB_ENDPTCOMPLETE);
624 }
625
626 for (ept = ept_list; ept; ept = ept->next){
627 if (n & ept->bit) {
628 handle_ept_complete(ept);
629 ret = INT_RESCHEDULE;
630 }
631 }
632 }
633 return ret;
634}
635
636int udc_register_gadget(struct udc_gadget *gadget)
637{
638 if (the_gadget) {
639 dprintf(CRITICAL, "only one gadget supported\n");
640 return -1;
641 }
642 the_gadget = gadget;
643 return 0;
644}
645
646static void udc_ept_desc_fill(struct udc_endpoint *ept, unsigned char *data)
647{
648 data[0] = 7;
649 data[1] = TYPE_ENDPOINT;
650 data[2] = ept->num | (ept->in ? 0x80 : 0x00);
651 data[3] = 0x02; /* bulk -- the only kind we support */
652 data[4] = ept->maxpkt;
653 data[5] = ept->maxpkt >> 8;
654 data[6] = ept->in ? 0x00 : 0x01;
655}
656
657static unsigned udc_ifc_desc_size(struct udc_gadget *g)
658{
659 return 9 + g->ifc_endpoints * 7;
660}
661
662static void udc_ifc_desc_fill(struct udc_gadget *g, unsigned char *data)
663{
664 unsigned n;
665
666 data[0] = 0x09;
667 data[1] = TYPE_INTERFACE;
668 data[2] = 0x00; /* ifc number */
669 data[3] = 0x00; /* alt number */
670 data[4] = g->ifc_endpoints;
671 data[5] = g->ifc_class;
672 data[6] = g->ifc_subclass;
673 data[7] = g->ifc_protocol;
674 data[8] = udc_string_desc_alloc(g->ifc_string);
675
676 data += 9;
677 for (n = 0; n < g->ifc_endpoints; n++) {
678 udc_ept_desc_fill(g->ept[n], data);
679 data += 7;
680 }
681}
682
683int udc_start(void)
684{
685 struct udc_descriptor *desc;
686 unsigned char *data;
687 unsigned size;
688
689 dprintf(INFO, "udc_start()\n");
690
691 if (!the_device) {
692 dprintf(CRITICAL, "udc cannot start before init\n");
693 return -1;
694 }
695 if (!the_gadget) {
696 dprintf(CRITICAL, "udc has no gadget registered\n");
697 return -1;
698 }
699
700 /* create our device descriptor */
701 desc = udc_descriptor_alloc(TYPE_DEVICE, 0, 18);
702 data = desc->data;
703 data[2] = 0x10; /* usb spec rev 2.10 */
704 data[3] = 0x02;
705 data[4] = 0x00; /* class */
706 data[5] = 0x00; /* subclass */
707 data[6] = 0x00; /* protocol */
708 data[7] = 0x40; /* max packet size on ept 0 */
709 memcpy(data + 8, &the_device->vendor_id, sizeof(short));
710 memcpy(data + 10, &the_device->product_id, sizeof(short));
711 memcpy(data + 12, &the_device->version_id, sizeof(short));
712 data[14] = udc_string_desc_alloc(the_device->manufacturer);
713 data[15] = udc_string_desc_alloc(the_device->product);
714 data[16] = udc_string_desc_alloc(the_device->serialno);
715 data[17] = 1; /* number of configurations */
716 udc_descriptor_register(desc);
717
718 /* create our configuration descriptor */
719 size = 9 + udc_ifc_desc_size(the_gadget);
720 desc = udc_descriptor_alloc(TYPE_CONFIGURATION, 0, size);
721 data = desc->data;
722 data[0] = 0x09;
723 data[2] = size;
724 data[3] = size >> 8;
725 data[4] = 0x01; /* number of interfaces */
726 data[5] = 0x01; /* configuration value */
727 data[6] = 0x00; /* configuration string */
728 data[7] = 0x80; /* attributes */
729 data[8] = 0x80; /* max power (250ma) -- todo fix this */
730 udc_ifc_desc_fill(the_gadget, data + 9);
731 udc_descriptor_register(desc);
732
733 /* go to RUN mode (D+ pullup enable) */
734 writel(0x00080001, USB_USBCMD);
735 register_int_handler(INT_USB_HS, udc_interrupt, (void*) 0);
736 unmask_interrupt(INT_USB_HS);
737 writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR);
738 return 0;
739}
740
741int udc_stop(void)
742{
743 writel(0, USB_USBINTR);
744 mask_interrupt(INT_USB_HS);
745
746 /* disable pullup */
747 writel(0x0008000, USB_USBCMD);
748 thread_sleep(10);
749
750 return 0;
751}
752
753
754