summaryrefslogtreecommitdiff
path: root/src/usbh_driver_hid_mouse.c
blob: 782cac5c1380596ed7c9ae5256d35f533395ecdd (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * This file is part of the libusbhost library
 * hosted at http://github.com/libusbhost/libusbhost
 *
 * Copyright (C) 2015 Amir Hammad <amir.hammad@hotmail.com>
 *
 *
 * libusbhost is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "usbh_hubbed.h"
#include "driver/usbh_device_driver.h"
#include "usbh_driver_hid_mouse.h"
#include "usart_helpers.h"

#include <libopencm3/usb/usbstd.h>

static void *mouse_init(void *usbh_dev);
static bool mouse_analyze_descriptor(void *drvdata, void *descriptor);
static void mouse_poll(void *drvdata, uint32_t time_curr_us);
static void mouse_remove(void *drvdata);

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

const usbh_dev_driver_t usbh_hid_mouse_driver = {
	.init = mouse_init,
	.analyze_descriptor = mouse_analyze_descriptor,
	.poll = mouse_poll,
	.remove = mouse_remove,
	.info = &usbh_hid_mouse_driver_info
};

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_mouse_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_mouse_device hid_mouse_device_t;

static hid_mouse_device_t mouse_device[USBH_HID_MOUSE_MAX_DEVICES];
static const hid_mouse_config_t *mouse_config;




#include <stdint.h>



void hid_mouse_driver_init(const hid_mouse_config_t *config)
{
	uint32_t i;
	mouse_config = config;
	for (i = 0; i < USBH_HID_MOUSE_MAX_DEVICES; i++) {
		mouse_device[i].state_next = STATE_INACTIVE;
	}
}

/**
 *
 *
 */
static void *mouse_init(void *usbh_dev)
{
	uint32_t i;
	hid_mouse_device_t *drvdata = 0;

	// find free data space for mouse device
	for (i = 0; i < USBH_HID_MOUSE_MAX_DEVICES; i++) {
		if (mouse_device[i].state_next == STATE_INACTIVE) {
			drvdata = &mouse_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;
}

/**
 * Returns true if all needed data are parsed
 */
static bool mouse_analyze_descriptor(void *drvdata, void *descriptor)
{
	hid_mouse_device_t *mouse = (hid_mouse_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;
			mouse->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)) {
					mouse->endpoint_in_address = epaddr&0x7f;
					if (ep->wMaxPacketSize < USBH_HID_MOUSE_BUFFER) {
						mouse->endpoint_in_maxpacketsize = ep->wMaxPacketSize;
					} else {
						mouse->endpoint_in_maxpacketsize = USBH_HID_MOUSE_BUFFER;
					}
				}

				if (mouse->endpoint_in_address) {
					mouse->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_mouse_device_t *mouse = (hid_mouse_device_t *)dev->drvdata;
	switch (mouse->state_next) {
	case STATE_READING_COMPLETE:
		{
			switch (cb_data.status) {
			case USBH_PACKET_CALLBACK_STATUS_OK:
			case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
				mouse->state_next = STATE_READING_REQUEST;
				break;

			case USBH_PACKET_CALLBACK_STATUS_EFATAL:
			case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
				ERROR(cb_data.status);
				mouse->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:
				mouse->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);
				mouse->state_next = STATE_INACTIVE;
				break;
			}
		}
		break;
	case STATE_SET_CONFIGURATION_COMPLETE: // Configured
		{
			switch (cb_data.status) {
			case USBH_PACKET_CALLBACK_STATUS_OK:
				mouse->state_next = STATE_READING_REQUEST;
				mouse->endpoint_in_toggle = 0;
				LOG_PRINTF("\r\nMOUSE CONFIGURED\r\n");
				break;

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


static void read_mouse_in(void *drvdata)
{
	hid_mouse_device_t *mouse = (hid_mouse_device_t *)drvdata;
	usbh_packet_t packet;

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

	mouse->state_next = STATE_READING_COMPLETE;
	usbh_read(mouse->usbh_device, &packet);

	// LOG_PRINTF("@MOUSE EP1 |  \r\n");

}

/**
 * \param time_curr_us - monotically rising time (see usbh_hubbed.h)
 *		unit is microseconds
 */
static void mouse_poll(void *drvdata, uint32_t time_curr_us)
{
	(void)time_curr_us;

	hid_mouse_device_t *mouse = (hid_mouse_device_t *)drvdata;
	usbh_device_t *dev = mouse->usbh_device;
	switch (mouse->state_next) {
	case STATE_READING_REQUEST:
		{
			read_mouse_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 = mouse->configuration_value;
			setup_data.wIndex = 0;
			setup_data.wLength = 0;

			mouse->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 mouse_remove(void *drvdata)
{
	hid_mouse_device_t *mouse = (hid_mouse_device_t *)drvdata;
	mouse->state_next = STATE_INACTIVE;
	mouse->endpoint_in_address = 0;
}