]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/usb-internal.h
Code cleanup
[kiibohd-controller.git] / Bootloader / usb-internal.h
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014-2015 <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 #pragma once
19
20 /**
21  * Internal driver structures
22  */
23
24 /**
25  * USB state machine
26  * =================
27  *
28  * Device configuration states:
29  *
30  * Attached <-> Powered
31  * Powered -(reset)-> Default
32  * Default -(SET_ADDRESS)-> Address
33  * Address -(SET_CONFIGURATION)-> Configured
34  * Configured -(SET_CONFIGURATION 0)-> Address
35  * Address -(SET_ADDRESS 0)-> Default
36  * [Default, Configured, Address] -(reset)-> Default
37  */
38
39 // ----- Defines -----
40
41 #ifndef USB_MAX_EP
42 #define USB_MAX_EP 16
43 #endif
44
45
46
47 // ----- Structs -----
48
49 struct usbd_ep_pipe_state_t {
50         enum usb_ep_pingpong pingpong; /* next desc to use */
51         enum usb_data01 data01;
52         size_t transfer_size;
53         size_t pos;
54         uint8_t *data_buf;
55         const uint8_t *copy_source;
56         int short_transfer;
57         ep_callback_t callback;
58         void *callback_data;
59         size_t ep_maxsize;
60         /* constant */
61         int ep_num;
62         enum usb_ep_dir ep_dir;
63 };
64
65 struct usbd_ep_state_t {
66         union {
67                 struct usbd_ep_pipe_state_t pipe[2];
68                 struct {
69                         struct usbd_ep_pipe_state_t rx;
70                         struct usbd_ep_pipe_state_t tx;
71                 };
72         };
73 };
74
75 struct usbd_t {
76         struct usbd_function_ctx_header functions;
77         struct usbd_function control_function;
78         const struct usbd_device *identity;
79         int address;
80         int config;
81         enum usbd_dev_state {
82                 USBD_STATE_DISABLED = 0,
83                 USBD_STATE_DEFAULT,
84                 USBD_STATE_SETTING_ADDRESS,
85                 USBD_STATE_ADDRESS,
86                 USBD_STATE_CONFIGURED
87         } state;
88         enum usb_ctrl_req_dir ctrl_dir;
89         struct usbd_ep_state_t ep_state[USB_MAX_EP];
90 };
91
92 extern struct usbd_t usb;
93
94
95
96 // ----- Functions -----
97
98 void usb_restart(void);
99 void usb_enable(void);
100 const struct usbd_config *usb_get_config_data(int config);
101