-
Notifications
You must be signed in to change notification settings - Fork 13
/
usb.h
111 lines (98 loc) · 2.76 KB
/
usb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* This file is part of ukbdc.
*
* ukbdc is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ukbdc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ukbdc; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "usb_hardware.h"
/* [FLAGS FOR bmRequestType] */
/* Request direction */
#define DIRECTION 0x80
#define HOST_TO_DEVICE 0x00
#define DEVICE_TO_HOST 0x80
/* Request type */
#define TYPE 0x60
#define STANDARD 0x00
#define CLASS 0x20
#define VENDOR 0x40
/* Request recipient */
#define RECIPIENT 0x1F
#define DEVICE 0x00
#define INTERFACE 0x01
#define ENDPOINT 0x02
#define OTHER 0x03
#define MASK_ALL (DIRECTION | TYPE | RECIPIENT)
/* [/FLAGS FOR bmRequestType] */
/* standard control endpoint request types */
#define GET_STATUS 0
#define CLEAR_FEATURE 1
#define SET_FEATURE 3
#define SET_ADDRESS 5
#define GET_DESCRIPTOR 6
#define GET_CONFIGURATION 8
#define SET_CONFIGURATION 9
#define GET_INTERFACE 10
#define SET_INTERFACE 11
// HID (human interface device)
#define HID_GET_REPORT 1
#define HID_GET_IDLE 2
#define HID_GET_PROTOCOL 3
#define HID_SET_REPORT 9
#define HID_SET_IDLE 10
#define HID_SET_PROTOCOL 11
/* The only available in USB endpoint feature selector */
#define ENDPOINT_HALT 0x00
#define DEVICE_REMOTE_WAKEUP 0x01
struct setup_packet {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
};
/* A structure which contains endpoint's whole configuration */
struct endpoint_config {
uint8_t num;
uint8_t type;
uint8_t config;
uint8_t int_flags;
};
/* 2 helper functions used to write request conditions */
static inline bool request_type(struct setup_packet *s,
uint8_t mask, uint8_t flags)
{
return (s->bmRequestType & mask) == flags;
}
static inline bool request(struct setup_packet *s, uint8_t req)
{
return s->bRequest == req;
}
static inline void USB_control_write_complete_status_stage()
{
USB_wait_IN();
/* send Zero Length Packet */
USB_flush_IN();
}
static inline void USB_control_read_complete_status_stage()
{
/* wait for Zero Length Packet from host */
USB_wait_OUT();
/* acknowledge it */
USB_flush_OUT();
}
void USB_init();
void USB_close();
bool USB_is_sleeping();
void USB_wakeup();
uint8_t USB_get_configuration();