]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/usb-internal.h
6d9210c212921438a20bf6a0a3ecda1f734b515c
[kiibohd-controller.git] / Bootloader / usb-internal.h
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014 <haata@kiibohd.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifndef __USB_INTERNAL_H
19 #define __USB_INTERNAL_H
20
21 /**
22  * Internal driver structures
23  */
24
25 /**
26  * USB state machine
27  * =================
28  *
29  * Device configuration states:
30  *
31  * Attached <-> Powered
32  * Powered -(reset)-> Default
33  * Default -(SET_ADDRESS)-> Address
34  * Address -(SET_CONFIGURATION)-> Configured
35  * Configured -(SET_CONFIGURATION 0)-> Address
36  * Address -(SET_ADDRESS 0)-> Default
37  * [Default, Configured, Address] -(reset)-> Default
38  */
39
40 // ----- Defines -----
41
42 #ifndef USB_MAX_EP
43 #define USB_MAX_EP 16
44 #endif
45
46
47
48 // ----- Structs -----
49
50 struct usbd_ep_pipe_state_t {
51         enum usb_ep_pingpong pingpong; /* next desc to use */
52         enum usb_data01 data01;
53         size_t transfer_size;
54         size_t pos;
55         uint8_t *data_buf;
56         const uint8_t *copy_source;
57         int short_transfer;
58         ep_callback_t callback;
59         void *callback_data;
60         size_t ep_maxsize;
61         /* constant */
62         int ep_num;
63         enum usb_ep_dir ep_dir;
64 };
65
66 struct usbd_ep_state_t {
67         union {
68                 struct usbd_ep_pipe_state_t pipe[2];
69                 struct {
70                         struct usbd_ep_pipe_state_t rx;
71                         struct usbd_ep_pipe_state_t tx;
72                 };
73         };
74 };
75
76 struct usbd_t {
77         struct usbd_function_ctx_header functions;
78         struct usbd_function control_function;
79         const struct usbd_device *identity;
80         int address;
81         int config;
82         enum usbd_dev_state {
83                 USBD_STATE_DISABLED = 0,
84                 USBD_STATE_DEFAULT,
85                 USBD_STATE_SETTING_ADDRESS,
86                 USBD_STATE_ADDRESS,
87                 USBD_STATE_CONFIGURED
88         } state;
89         enum usb_ctrl_req_dir ctrl_dir;
90         struct usbd_ep_state_t ep_state[USB_MAX_EP];
91 };
92
93 extern struct usbd_t usb;
94
95
96
97 // ----- Functions -----
98
99 void usb_restart(void);
100 void usb_enable(void);
101 const struct usbd_config *usb_get_config_data(int config);
102
103 #endif
104