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_USB_H
24 #define __DEV_USB_H
25 
26 #include <sys/types.h>
27 #include <compiler.h>
28 
29 __BEGIN_CDECLS
30 
31 /* top level initialization for usb client, abstracts away the interfaces */
32 typedef struct {
33     void *desc;
34     size_t len;
35     uint flags;
36 } usb_descriptor __ALIGNED(2);
37 
38 #define USB_DESC_FLAG_STATIC (0x1)
39 
40 #define USB_DESC_STATIC(x) { .desc = (void *)(x), .len = sizeof(x), .flags = USB_DESC_FLAG_STATIC }
41 
42 /* callbacks from usbc and usb layers */
43 typedef enum {
44     USB_CB_RESET,
45     USB_CB_SUSPEND,
46     USB_CB_RESUME,
47     USB_CB_DISCONNECT,
48     USB_CB_ONLINE,
49     USB_CB_OFFLINE,
50     USB_CB_SETUP_MSG,
51 } usb_callback_op_t;
52 
53 /* setup arg is valid during CB_SETUP_MSG */
54 union usb_callback_args {
55     const struct usb_setup *setup;
56 };
57 
58 typedef status_t (*usb_callback_t)(void *cookie, usb_callback_op_t op, const union usb_callback_args *args);
59 
60 typedef struct {
61     usb_descriptor string;
62     uint8_t id;
63 } usb_string;
64 
65 /* complete usb config struct, passed in to usb_setup() */
66 typedef struct {
67     struct usb_descriptor_speed {
68         usb_descriptor device;
69         usb_descriptor device_qual;
70         usb_descriptor config;
71     } lowspeed, highspeed;
72     usb_descriptor langid;
73 } usb_config;
74 
75 /* external code needs to set up the usb stack via the following calls */
76 status_t usb_setup(usb_config *config);
77 
78 /* Returns the Interface Number that will be assigned to the next interface that
79    is registered using usb_append_interface_(.*) */
80 uint8_t usb_get_current_iface_num_highspeed(void);
81 uint8_t usb_get_current_iface_num_lowspeed(void);
82 
83 /* apped new interface descriptors to the existing config if desired */
84 status_t usb_append_interface_highspeed(const uint8_t *int_descr, size_t len);
85 status_t usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len);
86 
87 status_t usb_add_string(const char *string, uint8_t id);
88 
89 status_t usb_start(void);
90 status_t usb_stop(void);
91 
92 /* callback api the usbc driver uses */
93 status_t usbc_callback(usb_callback_op_t op, const union usb_callback_args *args);
94 
95 /* callback api that anyone can register for */
96 status_t usb_register_callback(usb_callback_t, void *cookie);
97 
98 __END_CDECLS
99 
100 #endif
101 
102