summaryrefslogtreecommitdiff
path: root/src/usbh_driver_gp_xbox.c
blob: d8bb1b2315a186c87ba797bf108887f3e99df26c (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * 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 "usart_helpers.h"
#include "usbh_driver_gp_xbox.h"
#include "driver/usbh_device_driver.h"

#include <stdint.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
};

#define GP_XBOX_CORRECT_TRANSFERRED_LENGTH 20

struct _gp_xbox_device {
	usbh_device_t *usbh_device;
	uint8_t buffer[USBH_GP_XBOX_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 _gp_xbox_device gp_xbox_device_t;

static gp_xbox_device_t gp_xbox_device[USBH_GP_XBOX_MAX_DEVICES];
static const gp_xbox_config_t *gp_xbox_config;

static bool initialized = false;
static void read_gp_xbox_in(gp_xbox_device_t *gp_xbox);

void gp_xbox_driver_init(const gp_xbox_config_t *config)
{
	if (!config) {
		return;
	}
	initialized = true;
	uint32_t i;
	gp_xbox_config = config;
	for (i = 0; i < USBH_GP_XBOX_MAX_DEVICES; i++) {
		gp_xbox_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;
	gp_xbox_device_t *drvdata = 0;

	// find free data space for gp_xbox device
	for (i = 0; i < USBH_GP_XBOX_MAX_DEVICES; i++) {
		if (gp_xbox_device[i].state_next == STATE_INACTIVE) {
			drvdata = &gp_xbox_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 analyze_descriptor(void *drvdata, void *descriptor)
{
	gp_xbox_device_t *gp_xbox = (gp_xbox_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;
			gp_xbox->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)) {
					gp_xbox->endpoint_in_address = epaddr&0x7f;
					if (ep->wMaxPacketSize < USBH_GP_XBOX_BUFFER) {
						gp_xbox->endpoint_in_maxpacketsize = ep->wMaxPacketSize;
					} else {
						gp_xbox->endpoint_in_maxpacketsize = USBH_GP_XBOX_BUFFER;
					}
				}

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

static void parse_data(usbh_device_t *dev)
{
	gp_xbox_device_t *gp_xbox = (gp_xbox_device_t *)dev->drvdata;

	uint8_t *packet = gp_xbox->buffer;

	gp_xbox_packet_t gp_xbox_packet;
	gp_xbox_packet.buttons = 0;

	// DPAD
	const uint8_t data1 = packet[2];
	const uint8_t data2 = packet[3];
	if (data1 & (1 << 0)) {
		gp_xbox_packet.buttons |= GP_XBOX_DPAD_TOP;
	}

	if (data1 & (1 << 1)) {
		gp_xbox_packet.buttons |= GP_XBOX_DPAD_BOTTOM;
	}

	if (data1 & (1 << 2)) {
		gp_xbox_packet.buttons |= GP_XBOX_DPAD_LEFT;
	}

	if (data1 & (1 << 3)) {
		gp_xbox_packet.buttons |= GP_XBOX_DPAD_RIGHT;
	}

	// Start + select

	if (data1 & (1 << 4)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_START;
	}

	if (data1 & (1 << 5)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_SELECT;
	}

	// axis buttons

	if (data1 & (1 << 6)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_AXIS_LEFT;
	}

	if (data1 & (1 << 7)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_AXIS_RIGHT;
	}

	// buttons ABXY

	if (data2 & (1 << 4)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_A;
	}

	if (data2 & (1 << 5)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_B;
	}

	if (data2 & (1 << 6)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_X;
	}

	if (data2 & (1 << 7)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_Y;
	}

	// buttons rear

	if (data2 & (1 << 0)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_LT;
	}

	if (data2 & (1 << 1)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_RT;
	}

	if (data2 & (1 << 2)) {
		gp_xbox_packet.buttons |= GP_XBOX_BUTTON_XBOX;
	}

	// rear levers

	gp_xbox_packet.axis_rear_left = packet[4];
	gp_xbox_packet.axis_rear_right = packet[5];
	gp_xbox_packet.axis_left_x = packet[7]*256 + packet[6];
	gp_xbox_packet.axis_left_y = packet[9]*256 + packet[8];
	gp_xbox_packet.axis_right_x = packet[11]*256 + packet[10];
	gp_xbox_packet.axis_right_y = packet[13]*256 + packet[12];

	// call update callback
	if (gp_xbox_config->update) {
		gp_xbox_config->update(gp_xbox->device_id, gp_xbox_packet);
	}
}

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

			case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
				if (cb_data.transferred_length == GP_XBOX_CORRECT_TRANSFERRED_LENGTH) {
					parse_data(dev);
				}
				gp_xbox->state_next = STATE_READING_REQUEST;
				break;

			case USBH_PACKET_CALLBACK_STATUS_EFATAL:
			case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
				ERROR(cb_data.status);
				gp_xbox->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:
				gp_xbox->state_next = STATE_SET_CONFIGURATION_COMPLETE;
				device_xfer_control_read(0, 0, event, dev);
				break;
			case USBH_PACKET_CALLBACK_STATUS_EFATAL:
			case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
			case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
				ERROR(cb_data.status);
				gp_xbox->state_next = STATE_INACTIVE;
				break;
			}
		}
		break;
	case STATE_SET_CONFIGURATION_COMPLETE: // Configured
		{
			switch (cb_data.status) {
			case USBH_PACKET_CALLBACK_STATUS_OK:
				gp_xbox->state_next = STATE_READING_REQUEST;
				gp_xbox->endpoint_in_toggle = 0;
				LOG_PRINTF("\ngp_xbox CONFIGURED\n");
				if (gp_xbox_config->notify_connected) {
					gp_xbox_config->notify_connected(gp_xbox->device_id);
				}
				break;

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

	case STATE_INACTIVE:
		{
			LOG_PRINTF("XBOX inactive");
		}
		break;
	default:
		{
			LOG_PRINTF("Unknown state\n");
		}
		break;
	}
}


static void read_gp_xbox_in(gp_xbox_device_t *gp_xbox)
{
	usbh_packet_t packet;

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

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

	// LOG_PRINTF("@gp_xbox EP1 |  \n");
}

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

	gp_xbox_device_t *gp_xbox = (gp_xbox_device_t *)drvdata;
	usbh_device_t *dev = gp_xbox->usbh_device;

	switch (gp_xbox->state_next) {
	case STATE_READING_REQUEST:
		{
			read_gp_xbox_in(gp_xbox);
		}
		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 = gp_xbox->configuration_value;
			setup_data.wIndex = 0;
			setup_data.wLength = 0;

			gp_xbox->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)
{
	LOG_PRINTF("Removing xbox\n");

	gp_xbox_device_t *gp_xbox = (gp_xbox_device_t *)drvdata;
	if (gp_xbox_config->notify_disconnected) {
		gp_xbox_config->notify_disconnected(gp_xbox->device_id);
	}
	gp_xbox->state_next = STATE_INACTIVE;
	gp_xbox->endpoint_in_address = 0;
}

static const usbh_dev_driver_info_t driver_info = {
	.deviceClass = 0xff,
	.deviceSubClass = 0xff,
	.deviceProtocol = 0xff,
	.idVendor = 0x045e,
	.idProduct = 0x028e,
	.ifaceClass = 0xff,
	.ifaceSubClass = 93,
	.ifaceProtocol = 0x01
};

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