1 /*
2 * Copyright (c) 2008 Travis Geiselbrecht
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 #ifndef __DEV_USBC_H
24 #define __DEV_USBC_H
25
26 #include <compiler.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <hw/usb.h>
31
32 __BEGIN_CDECLS
33
34 void usbc_init(void);
35
36 typedef uint ep_t;
37
38 typedef enum {
39 USB_IN = 0,
40 USB_OUT
41 } ep_dir_t;
42
43 typedef enum {
44 USB_CTRL = 0x00,
45 USB_ISOC = 0x01,
46 USB_BULK = 0x02,
47 USB_INTR = 0x03,
48 } ep_type_t;
49
50
51 struct usbc_transfer;
52 typedef status_t (*ep_callback)(ep_t endpoint, struct usbc_transfer *transfer);
53
54 typedef struct usbc_transfer {
55 ep_callback callback;
56 status_t result;
57 void *buf;
58 size_t buflen;
59 uint bufpos;
60 void *extra; // extra pointer to store whatever you want
61 } usbc_transfer_t;
62
63 enum {
64 USB_TRANSFER_RESULT_OK = 0,
65 USB_TRANSFER_RESULT_ERR = -1,
66 USB_TRANSFER_RESULT_CANCELLED = -2,
67 };
68
69 status_t usbc_setup_endpoint(ep_t ep, ep_dir_t dir, uint width, ep_type_t type);
70 status_t usbc_queue_rx(ep_t ep, usbc_transfer_t *transfer);
71 status_t usbc_queue_tx(ep_t ep, usbc_transfer_t *transfer);
72 status_t usbc_flush_ep(ep_t ep);
73
74 status_t usbc_set_active(bool active);
75 void usbc_set_address(uint8_t address);
76
77 /* called back from within a callback to handle setup responses */
78 void usbc_ep0_ack(void);
79 void usbc_ep0_stall(void);
80 void usbc_ep0_send(const void *buf, size_t len, size_t maxlen);
81 void usbc_ep0_recv(void *buf, size_t len, ep_callback);
82
83 bool usbc_is_highspeed(void);
84
usbc_dump_transfer(const usbc_transfer_t * t)85 static inline void usbc_dump_transfer(const usbc_transfer_t *t)
86 {
87 printf("usb transfer %p: cb %p buf %p, buflen %zd, bufpos %u, result %d\n", (void*)t, (void*)t->callback, (void*)t->buf, t->buflen, t->bufpos, t->result);
88 }
89
90 __END_CDECLS
91
92 #endif
93
94