Welcome to mirror list, hosted at ThFree Co, Russian Federation.

usbh_driver_hid_kbd.c « libusbhost « lib « pastilda « emb - github.com/thirdpin/pastilda.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29d500837316eea83a9d5077c40cf6058afd3b1a (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "usbh_hubbed.h"
#include "driver/usbh_device_driver.h"
#include "usbh_driver_hid_kbd.h"
#include "usart_helpers.h"

#include <libopencm3/usb/usbstd.h>

enum STATES {
	STATE_INACTIVE,
	STATE_READING_COMPLETE,
	STATE_READING_REQUEST,
	STATE_SET_CONFIGURATION_REQUEST,
	STATE_SET_CONFIGURATION_EMPTY_READ,
	STATE_SET_CONFIGURATION_COMPLETE
};

struct _hid_kbd_device {
	usbh_device_t *usbh_device;
	uint8_t buffer[USBH_HID_MOUSE_BUFFER];
	uint16_t endpoint_in_maxpacketsize;
	uint8_t endpoint_in_address;
	enum STATES state_next;
	uint8_t endpoint_in_toggle;
	uint8_t device_id;
	uint8_t configuration_value;
};
typedef struct _hid_kbd_device hid_kbd_device_t;

static hid_kbd_device_t kbd_device[USBH_HID_MOUSE_MAX_DEVICES];
static const hid_kbd_config_t *kbd_config;

#include <stdint.h>

static bool initialized = false;

void hid_kbd_driver_init(const hid_kbd_config_t *config)
{
	uint32_t i;
	initialized = true;

	kbd_config = config;
	for (i = 0; i < USBH_HID_MOUSE_MAX_DEVICES; i++) {
		kbd_device[i].state_next = STATE_INACTIVE;
	}
}

static void *init(void *usbh_dev)
{
	if (!initialized) {
		LOG_PRINTF("\n%s/%d : driver not initialized\r\n", __FILE__, __LINE__);
		return (0);
	}

	uint32_t i;
	hid_kbd_device_t *drvdata = 0;

	// find free data space for mouse device
	for (i = 0; i < USBH_HID_MOUSE_MAX_DEVICES; i++) {
		if (kbd_device[i].state_next == STATE_INACTIVE) {
			drvdata = &kbd_device[i];
			drvdata->device_id = i;
			drvdata->endpoint_in_address = 0;
			drvdata->endpoint_in_toggle = 0;
			drvdata->usbh_device = (usbh_device_t *)usbh_dev;
			break;
		}
	}

	return (drvdata);
}

static bool analyze_descriptor(void *drvdata, void *descriptor)
{
	hid_kbd_device_t *kbd = (hid_kbd_device_t *)drvdata;
	uint8_t desc_type = ((uint8_t *)descriptor)[1];
	switch (desc_type) {
	case USB_DT_CONFIGURATION:
		{
			struct usb_config_descriptor *cfg = (struct usb_config_descriptor*)descriptor;
			kbd->configuration_value = cfg->bConfigurationValue;
		}
		break;
	case USB_DT_DEVICE:
		break;
	case USB_DT_INTERFACE:
		break;
	case USB_DT_ENDPOINT:
		{
			struct usb_endpoint_descriptor *ep = (struct usb_endpoint_descriptor*)descriptor;
			if ((ep->bmAttributes&0x03) == USB_ENDPOINT_ATTR_INTERRUPT) {
				uint8_t epaddr = ep->bEndpointAddress;
				if (epaddr & (1<<7)) {
					kbd->endpoint_in_address = epaddr&0x7f;
					if (ep->wMaxPacketSize < USBH_HID_MOUSE_BUFFER) {
						kbd->endpoint_in_maxpacketsize = ep->wMaxPacketSize;
					} else {
						kbd->endpoint_in_maxpacketsize = USBH_HID_MOUSE_BUFFER;
					}
				}

				if (kbd->endpoint_in_address) {
					kbd->state_next = STATE_SET_CONFIGURATION_REQUEST;
					return (true);
				}
			}
		}
		break;
	// TODO Class Specific descriptors
	default:
		break;
	}
	return (false);
}

static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data)
{
	hid_kbd_device_t *kbd = (hid_kbd_device_t *)dev->drvdata;
	switch (kbd->state_next) {
	case STATE_READING_COMPLETE:
		{
			switch (cb_data.status) {
			case USBH_PACKET_CALLBACK_STATUS_OK:
			case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
				kbd->state_next = STATE_READING_REQUEST;
				break;

			case USBH_PACKET_CALLBACK_STATUS_EFATAL:
			case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
				ERROR(cb_data.status);
				kbd->state_next = STATE_INACTIVE;
				break;
			}
		}
		break;

	case STATE_SET_CONFIGURATION_EMPTY_READ:
		{
			LOG_PRINTF("|empty packet read|");
			switch (cb_data.status) {
			case USBH_PACKET_CALLBACK_STATUS_OK:
				kbd->state_next = STATE_SET_CONFIGURATION_COMPLETE;
				device_xfer_control_read(0, 0, event, dev);
				break;

			case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
			case USBH_PACKET_CALLBACK_STATUS_EFATAL:
			case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
				ERROR(cb_data.status);
				kbd->state_next = STATE_INACTIVE;
				break;
			}
		}
		break;
	case STATE_SET_CONFIGURATION_COMPLETE: // Configured
		{
			switch (cb_data.status) {
			case USBH_PACKET_CALLBACK_STATUS_OK:
				kbd->state_next = STATE_READING_REQUEST;
				kbd->endpoint_in_toggle = 0;
				LOG_PRINTF("\nKEYBOARD CONFIGURED\n");
				break;

			case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
			case USBH_PACKET_CALLBACK_STATUS_EFATAL:
			case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
				ERROR(cb_data.status);
				kbd->state_next = STATE_INACTIVE;
				break;
			}
		}
		break;
	default:
		break;
	}
}

static void read_kbd_in(void *drvdata)
{
	hid_kbd_device_t *kbd = (hid_kbd_device_t *)drvdata;
	usbh_packet_t packet;

	packet.address = kbd->usbh_device->address;
	packet.data = &kbd->buffer[0];
	packet.datalen = kbd->endpoint_in_maxpacketsize;
	packet.endpoint_address = kbd->endpoint_in_address;
	packet.endpoint_size_max = kbd->endpoint_in_maxpacketsize;
	packet.endpoint_type = USBH_EPTYP_INTERRUPT;
	packet.speed = kbd->usbh_device->speed;
	packet.callback = event;
	packet.callback_arg = kbd->usbh_device;
	packet.toggle = &kbd->endpoint_in_toggle;

	kbd->state_next = STATE_READING_COMPLETE;
	usbh_read(kbd->usbh_device, &packet);
	kbd_config->kbd_in_message_handler(packet.datalen, packet.data);
}

static void poll(void *drvdata, uint32_t time_curr_us)
{
	(void)time_curr_us;

	hid_kbd_device_t *kbd = (hid_kbd_device_t *)drvdata;
	usbh_device_t *dev = kbd->usbh_device;
	switch (kbd->state_next) {
	case STATE_READING_REQUEST:
		{
			read_kbd_in(drvdata);
		}
		break;

	case STATE_SET_CONFIGURATION_REQUEST:
		{
			struct usb_setup_data setup_data;

			setup_data.bmRequestType = 0b00000000;
			setup_data.bRequest = USB_REQ_SET_CONFIGURATION;
			setup_data.wValue = kbd->configuration_value;
			setup_data.wIndex = 0;
			setup_data.wLength = 0;

			kbd->state_next = STATE_SET_CONFIGURATION_EMPTY_READ;

			device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
		}
		break;

	default:
		// do nothing - probably transfer is in progress
		break;
	}
}

static void remove(void *drvdata)
{
	hid_kbd_device_t *kbd = (hid_kbd_device_t *)drvdata;
	kbd->state_next = STATE_INACTIVE;
	kbd->endpoint_in_address = 0;
}

static const usbh_dev_driver_info_t driver_info = {
	.deviceClass = -1,
	.deviceSubClass = -1,
	.deviceProtocol = -1,
	.idVendor = -1,
	.idProduct = -1,
	.ifaceClass = 0x03,
	.ifaceSubClass = 1, //-1
	.ifaceProtocol = 0x01 //0x02
};

const usbh_dev_driver_t usbh_hid_kbd_driver = {
	.init = init,
	.analyze_descriptor = analyze_descriptor,
	.poll = poll,
	.remove = remove,
	.info = &driver_info
};