1 /*
2  * Copyright (c) 2015 Brian Swetland
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 
25 #include <app.h>
26 #include <debug.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <printf.h>
30 #include <dev/udc.h>
31 
32 udc_request_t *txreq;
33 udc_request_t *rxreq;
34 udc_endpoint_t *txept;
35 udc_endpoint_t *rxept;
36 
37 static char rxbuf[4096];
38 
rx_complete(udc_request_t * req,unsigned actual,int status)39 static void rx_complete(udc_request_t *req, unsigned actual, int status)
40 {
41     //printf("rx done %d %d\n", actual, status);
42     if (status == 0) {
43         udc_request_queue(rxept, rxreq);
44     }
45 }
46 
tx_complete(udc_request_t * req,unsigned actual,int status)47 static void tx_complete(udc_request_t *req, unsigned actual, int status)
48 {
49     //printf("tx done %d %d\n", actual, status);
50     if (status == 0) {
51         udc_request_queue(txept, txreq);
52     }
53 }
54 
udctest_notify(udc_gadget_t * gadget,unsigned event)55 static void udctest_notify(udc_gadget_t *gadget, unsigned event)
56 {
57     printf("event %d\n", event);
58     if (event == UDC_EVENT_ONLINE) {
59         udc_request_queue(rxept, rxreq);
60         udc_request_queue(txept, txreq);
61     }
62 }
63 
64 static udc_device_t udctest_device = {
65     .vendor_id = 0x18d1,
66     .product_id = 0xdb01,
67     .version_id = 0x0100,
68     .manufacturer = "Frobozz Magic USB Device Company",
69     .product = "Frobozzco USB Device",
70     .serialno = "00000005",
71 };
72 
73 static udc_endpoint_t *udctest_endpoints[2];
74 
75 static udc_gadget_t udctest_gadget = {
76     .notify = udctest_notify,
77     .ifc_class = 0xFF,
78     .ifc_subclass = 0x42,
79     .ifc_protocol = 0x01,
80     .ifc_endpoints = 2,
81     .ifc_string = "string",
82     .ept = udctest_endpoints,
83 };
84 
udctest_init(const struct app_descriptor * app)85 static void udctest_init(const struct app_descriptor *app)
86 {
87     printf("usbtest_init()\n");
88     udc_init(&udctest_device);
89     udctest_endpoints[0] = txept = udc_endpoint_alloc(UDC_BULK_IN, 512);
90     udctest_endpoints[1] = rxept = udc_endpoint_alloc(UDC_BULK_OUT, 512);
91     txreq = udc_request_alloc();
92     rxreq = udc_request_alloc();
93     rxreq->buffer = rxbuf;
94     rxreq->length = sizeof(rxbuf);
95     rxreq->complete = rx_complete;
96     txreq->buffer = rxbuf;
97     txreq->length = sizeof(rxbuf);
98     txreq->complete = tx_complete;
99     udc_register_gadget(&udctest_gadget);
100 }
101 
udctest_entry(const struct app_descriptor * app,void * args)102 static void udctest_entry(const struct app_descriptor *app, void *args)
103 {
104     printf("udctest_entry()\n");
105     udc_start();
106 }
107 
108 APP_START(usbtest)
109 .init = udctest_init,
110  .entry = udctest_entry,
111   APP_END
112 
113 
114