1 /*
2  * Copyright (c) 2009, 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 #pragma once
30 
31 #include <stdint.h>
32 
33 typedef struct udc_request udc_request_t;
34 typedef struct udc_gadget udc_gadget_t;
35 typedef struct udc_device udc_device_t;
36 
37 /* endpoints are opaque handles specific to the particular device controller */
38 typedef struct udc_endpoint udc_endpoint_t;
39 
40 /* USB Device Controller Transfer Request */
41 struct udc_request {
42     void *buffer;
43     unsigned length;
44     void (*complete)(udc_request_t *req, unsigned actual, int status);
45     void *context;
46 };
47 
48 udc_request_t *udc_request_alloc(void);
49 void udc_request_free(udc_request_t *req);
50 int udc_request_queue(udc_endpoint_t *ept, udc_request_t *req);
51 int udc_request_cancel(udc_endpoint_t *ept, udc_request_t *req);
52 
53 #define UDC_BULK_IN    0x82
54 #define UDC_BULK_OUT   0x02
55 
56 udc_endpoint_t *udc_endpoint_alloc(unsigned type, unsigned maxpkt);
57 void udc_endpoint_free(udc_endpoint_t *ept);
58 
59 #define UDC_EVENT_ONLINE    1
60 #define UDC_EVENT_OFFLINE   2
61 
62 struct udc_gadget {
63     void (*notify)(udc_gadget_t *gadget, unsigned event);
64     void *context;
65 
66     struct udc_gadget *next; // do not modify
67 
68     uint8_t ifc_class;
69     uint8_t ifc_subclass;
70     uint8_t ifc_protocol;
71     uint8_t ifc_endpoints;
72     const char *ifc_string;
73     unsigned flags;
74 
75     udc_endpoint_t **ept;
76 };
77 
78 struct udc_device {
79     uint16_t vendor_id;
80     uint16_t product_id;
81     uint16_t version_id;
82 
83     const char *manufacturer;
84     const char *product;
85     const char *serialno;
86 };
87 
88 int udc_init(udc_device_t *devinfo);
89 int udc_register_gadget(udc_gadget_t *gadget);
90 int udc_start(void);
91 int udc_stop(void);
92 
93