1 /*
2  * Copyright (c) 2008 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 #include <lk/compiler.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <hw/usb.h>
15 #include <dev/usb.h>
16 
17 __BEGIN_CDECLS
18 
19 void usbc_init(void);
20 
21 typedef uint ep_t;
22 
23 typedef enum {
24     USB_IN = 0,
25     USB_OUT
26 } ep_dir_t;
27 
28 typedef enum {
29     USB_CTRL = 0x00,
30     USB_ISOC = 0x01,
31     USB_BULK = 0x02,
32     USB_INTR = 0x03,
33 } ep_type_t;
34 
35 
36 struct usbc_transfer;
37 typedef status_t (*ep_callback)(ep_t endpoint, struct usbc_transfer *transfer);
38 
39 typedef struct usbc_transfer {
40     ep_callback callback;
41     status_t result;
42     void *buf;
43     size_t buflen;
44     uint bufpos;
45     void *extra; // extra pointer to store whatever you want
46 } usbc_transfer_t;
47 
48 enum {
49     USB_TRANSFER_RESULT_OK = 0,
50     USB_TRANSFER_RESULT_ERR = -1,
51     USB_TRANSFER_RESULT_CANCELLED = -2,
52 };
53 
54 status_t usbc_setup_endpoint(ep_t ep, ep_dir_t dir, uint width, ep_type_t type);
55 status_t usbc_queue_rx(ep_t ep, usbc_transfer_t *transfer);
56 status_t usbc_queue_tx(ep_t ep, usbc_transfer_t *transfer);
57 status_t usbc_flush_ep(ep_t ep);
58 
59 status_t usbc_set_active(bool active);
60 void usbc_set_address(uint8_t address);
61 
62 /* callback api the usbc driver uses */
63 status_t usbc_callback(usb_callback_op_t op, const union usb_callback_args *args);
64 
65 /* called back from within a callback to handle setup responses */
66 void usbc_ep0_ack(void);
67 void usbc_ep0_stall(void);
68 void usbc_ep0_send(const void *buf, size_t len, size_t maxlen);
69 void usbc_ep0_recv(void *buf, size_t len, ep_callback);
70 
71 bool usbc_is_highspeed(void);
72 
usbc_dump_transfer(const usbc_transfer_t * t)73 static inline void usbc_dump_transfer(const usbc_transfer_t *t) {
74     printf("usb transfer %p: cb %p buf %p, buflen %zd, bufpos %u, result %d\n", t, t->callback, t->buf, t->buflen, t->bufpos, t->result);
75 }
76 
77 __END_CDECLS
78 
79