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 <sys/types.h>
11 #include <lk/compiler.h>
12 
13 __BEGIN_CDECLS
14 
15 /* top level initialization for usb client, abstracts away the interfaces */
16 typedef struct {
17     void *desc;
18     size_t len;
19     uint flags;
20 } usb_descriptor __ALIGNED(2);
21 
22 #define USB_DESC_FLAG_STATIC (0x1)
23 
24 #define USB_DESC_STATIC(x) { .desc = (void *)(x), .len = sizeof(x), .flags = USB_DESC_FLAG_STATIC }
25 
26 /* callbacks from usbc and usb layers */
27 typedef enum {
28     USB_CB_RESET,
29     USB_CB_SUSPEND,
30     USB_CB_RESUME,
31     USB_CB_DISCONNECT,
32     USB_CB_ONLINE,
33     USB_CB_OFFLINE,
34     USB_CB_SETUP_MSG,
35 } usb_callback_op_t;
36 
37 /* setup arg is valid during CB_SETUP_MSG */
38 union usb_callback_args {
39     const struct usb_setup *setup;
40 };
41 
42 typedef status_t (*usb_callback_t)(void *cookie, usb_callback_op_t op, const union usb_callback_args *args);
43 
44 typedef struct {
45     usb_descriptor string;
46     uint8_t id;
47 } usb_string;
48 
49 /* complete usb config struct, passed in to usb_setup() */
50 typedef struct {
51     struct usb_descriptor_speed {
52         usb_descriptor device;
53         usb_descriptor device_qual;
54         usb_descriptor config;
55     } lowspeed, highspeed;
56     usb_descriptor langid;
57 } usb_config;
58 
59 /* external code needs to set up the usb stack via the following calls */
60 status_t usb_setup(usb_config *config);
61 
62 /* Returns the Interface Number that will be assigned to the next interface that
63    is registered using usb_append_interface_(.*) */
64 uint8_t usb_get_current_iface_num_highspeed(void);
65 uint8_t usb_get_current_iface_num_lowspeed(void);
66 
67 /* Append new interface descriptors to the existing config.*/
68 /* Returns interface number selected or error. */
69 int usb_append_interface_highspeed(const uint8_t *int_descr, size_t len);
70 int usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len);
71 
72 /* add a new string to the existing config */
73 status_t usb_add_string(const char *string, uint8_t id);
74 
75 status_t usb_start(void);
76 status_t usb_stop(void);
77 
78 /* callback api that anyone can register for */
79 status_t usb_register_callback(usb_callback_t, void *cookie);
80 
81 __END_CDECLS
82 
83