-
Notifications
You must be signed in to change notification settings - Fork 13
/
rawhid.c
94 lines (89 loc) · 2.49 KB
/
rawhid.c
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
/* 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
*/
#include "rawhid.h"
#include <avr/interrupt.h>
/* [Callbacks section] ----------------------------------------------------- */
bool RAWHID_handle_control_request(struct setup_packet *s)
{
uint8_t i, len, n;
if (s->bmRequestType == 0xA1 && s->bRequest == HID_GET_REPORT) {
len = RAWHID_SIZE;
do {
// wait for host ready for IN packet
do {
i = UEINTX;
} while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
if (i & (1<<RXOUTI)) return false; // abort
// send IN packet
n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
for (i = n; i; i--) {
// just send zeros
UEDATX = 0;
}
len -= n;
USB_flush_IN();
} while (len || n == ENDPOINT0_SIZE);
}
if (s->bmRequestType == 0x21 && s->bRequest == HID_SET_REPORT) {
len = RAWHID_SIZE;
do {
n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
USB_wait_OUT();
// ignore incoming bytes
USB_flush_OUT();
len -= n;
} while (len);
USB_wait_IN();
USB_flush_IN();
}
return true;
}
/* [/Callbacks section] ---------------------------------------------------- */
/* [API section] ----------------------------------------------------------- */
bool RAWHID_send(const void *buffer)
{
if (!USB_get_configuration())
return false;
uint8_t sreg = SREG;
cli();
USB_set_endpoint(RAWHID_TX_ENDPOINT);
if (!USB_IN_ready()) {
SREG = sreg;
return false;
}
USB_IN_write_buffer(buffer, RAWHID_SIZE);
USB_flush_IN();
SREG = sreg;
return true;
}
bool RAWHID_recv(void *buffer)
{
if (!USB_get_configuration())
return false;
uint8_t sreg = SREG;
cli();
USB_set_endpoint(RAWHID_RX_ENDPOINT);
if (!USB_OUT_ready()) {
SREG = sreg;
return false;
}
USB_OUT_read_buffer(buffer, RAWHID_SIZE);
USB_flush_OUT();
SREG = sreg;
return true;
}
/* [/API section] ---------------------------------------------------------- */