[usb] first stab at a usb device stack
diff --git a/include/dev/usb.h b/include/dev/usb.h
index 556a4ed..a35a819 100644
--- a/include/dev/usb.h
+++ b/include/dev/usb.h
@@ -23,9 +23,39 @@
#ifndef __DEV_USB_H
#define __DEV_USB_H
-/* device side usb stack api */
+#include <sys/types.h>
+#include <compiler.h>
+
+/* top level initialization for usb client, abstracts away the interfaces */
+typedef struct {
+ void *desc;
+ size_t len;
+} usb_descriptor __ALIGNED(2);
+
+/* complete usb config struct, passed in to usb_setup() */
+typedef struct {
+ struct usb_descriptor_speed {
+ usb_descriptor device;
+ usb_descriptor device_qual;
+ usb_descriptor config;
+ } lowspeed, highspeed;
+ usb_descriptor device_string;
+ usb_descriptor mfg_string;
+ usb_descriptor serial_string;
+ usb_descriptor langid;
+} usb_config;
void usb_init(void);
+/* external code needs to set up the usb stack via the following calls */
+void usb_setup(usb_config *config);
+
+/* apped new interface descriptors to the existing config if desired */
+int usb_append_interface_highspeed(const uint8_t *int_descr, size_t len);
+int usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len);
+
+void usb_start(void);
+void usb_stop(void);
+
#endif
diff --git a/include/dev/usbc.h b/include/dev/usbc.h
index 26c1945..f566190 100644
--- a/include/dev/usbc.h
+++ b/include/dev/usbc.h
@@ -23,8 +23,76 @@
#ifndef __DEV_USBC_H
#define __DEV_USBC_H
-/* device side usb controller api (used by the usb stack) */
+#include <sys/types.h>
+#include <debug.h>
+#include <hw/usb.h>
+
void usbc_init(void);
+typedef uint ep_t;
+
+typedef enum {
+ IN = 0,
+ OUT
+} ep_dir_t;
+
+typedef enum {
+ CB_RESET,
+ CB_SUSPEND,
+ CB_RESUME,
+ CB_DISCONNECT,
+ CB_ONLINE,
+ CB_OFFLINE,
+ CB_SETUP_MSG,
+
+ /* endpoint transfer stuff */
+ CB_EP_RXCOMPLETE,
+ CB_EP_TXCOMPLETE,
+ CB_EP_TRANSFER_CANCELLED,
+} usbc_callback_op_t;
+
+typedef struct {
+ void *buf;
+ size_t buflen;
+ uint bufpos;
+ int result;
+ void *extra; // extra pointer to store whatever you want
+} usbc_transfer;
+
+enum {
+ USB_TRANSFER_RESULT_OK = 0,
+ USB_TRANSFER_RESULT_ERR = -1,
+ USB_TRANSFER_RESULT_CANCELLED = -2,
+};
+
+typedef int (*ep_callback)(ep_t endpoint, usbc_callback_op_t op, usbc_transfer *transfer);
+
+void usbc_setup_endpoint(ep_t ep, ep_dir_t dir, bool active, ep_callback callback, uint width, uint blocksize);
+int usbc_queue_rx(ep_t ep, usbc_transfer *transfer);
+int usbc_queue_tx(ep_t ep, usbc_transfer *transfer);
+
+/* setup arg is valid during CB_SETUP_MSG */
+union usb_callback_args {
+ const struct usb_setup *setup;
+};
+
+typedef int (*usb_callback)(usbc_callback_op_t op, const union usb_callback_args *args);
+
+int usbc_set_callback(usb_callback);
+int usbc_set_active(bool active);
+
+/* called back from within a callback to handle setup responses */
+void usbc_ep0_ack(void);
+void usbc_ep0_stall(void);
+void usbc_ep0_send(const void *buf, size_t len, size_t maxlen);
+void usbc_ep0_recv(void *buf, size_t len, ep_callback);
+
+bool usbc_is_highspeed(void);
+
+static inline void usbc_dump_transfer(const usbc_transfer *t)
+{
+ printf("usb transfer %p: buf %p, buflen %zd, bufpos %u, result %d\n", t, t->buf, t->buflen, t->bufpos, t->result);
+}
+
#endif