forked from google/libhoth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibhoth_usb.c
275 lines (239 loc) · 8.45 KB
/
libhoth_usb.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
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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "libhoth_usb.h"
#include <libusb.h>
#include <stdlib.h>
#include "libhoth.h"
#include "libhoth_usb_device.h"
int libhoth_usb_send_request(struct libhoth_device* dev, const void* request,
size_t request_size);
int libhoth_usb_receive_response(struct libhoth_device* dev, void* response,
size_t max_response_size, size_t* actual_size,
int timeout_ms);
static struct libhoth_usb_interface_info libhoth_usb_find_interface(
const struct libusb_config_descriptor* configuration) {
struct libhoth_usb_interface_info info = {
.type = LIBHOTH_USB_INTERFACE_TYPE_UNKNOWN,
};
for (int i = 0; i < configuration->bNumInterfaces; i++) {
for (int j = 0; j < configuration->interface[i].num_altsetting; j++) {
const struct libusb_interface_descriptor* interface =
&configuration->interface[i].altsetting[j];
if (interface->bInterfaceClass != LIBHOTH_USB_INTERFACE_CLASS) continue;
if (interface->bInterfaceSubClass ==
LIBHOTH_USB_MAILBOX_INTERFACE_SUBCLASS &&
interface->bInterfaceProtocol ==
LIBHOTH_USB_MAILBOX_INTERFACE_PROTOCOL) {
info.type = LIBHOTH_USB_INTERFACE_TYPE_MAILBOX;
info.interface_id = i;
info.interface_altsetting = j;
return info;
}
if (interface->bInterfaceSubClass ==
LIBHOTH_USB_FIFO_INTERFACE_SUBCLASS &&
interface->bInterfaceProtocol ==
LIBHOTH_USB_FIFO_INTERFACE_PROTOCOL) {
info.type = LIBHOTH_USB_INTERFACE_TYPE_FIFO;
info.interface_id = i;
info.interface_altsetting = j;
return info;
}
}
}
return info;
}
static int libhoth_usb_claim(struct libhoth_device* dev) {
struct libhoth_usb_device* usb_dev = dev->user_ctx;
int status =
libusb_claim_interface(usb_dev->handle, usb_dev->info.interface_id);
if (status == LIBUSB_ERROR_BUSY) {
return LIBHOTH_ERR_INTERFACE_BUSY;
}
return status;
}
static int libhoth_usb_release(struct libhoth_device* dev) {
struct libhoth_usb_device* usb_dev = dev->user_ctx;
return libusb_release_interface(usb_dev->handle, usb_dev->info.interface_id);
}
int libhoth_usb_open(const struct libhoth_usb_device_init_options* options,
struct libhoth_device** out) {
if (out == NULL || options == NULL || options->usb_device == NULL) {
return LIBUSB_ERROR_INVALID_PARAM;
}
struct libhoth_device* dev = NULL;
struct libhoth_usb_device* usb_dev = NULL;
struct libusb_device_descriptor device_descriptor;
int status =
libusb_get_device_descriptor(options->usb_device, &device_descriptor);
if (status != LIBUSB_SUCCESS) {
return status;
}
// Ensure vendor ID matches
if (device_descriptor.idVendor != LIBHOTH_USB_VENDOR_ID) {
return LIBHOTH_ERR_UNKNOWN_VENDOR;
}
// Pick the correct driver based on the interface type
struct libusb_config_descriptor* config_descriptor = NULL;
status = libusb_get_active_config_descriptor(options->usb_device,
&config_descriptor);
if (status != LIBUSB_SUCCESS) {
return status;
}
// Verify that the device has a supported interface
struct libhoth_usb_interface_info info =
libhoth_usb_find_interface(config_descriptor);
if (info.type == LIBHOTH_USB_INTERFACE_TYPE_UNKNOWN) {
status = LIBHOTH_ERR_INTERFACE_NOT_FOUND;
goto err_out;
}
dev = calloc(1, sizeof(struct libhoth_device));
if (dev == NULL) {
status = LIBHOTH_ERR_MALLOC_FAILED;
goto err_out;
}
usb_dev = calloc(1, sizeof(struct libhoth_usb_device));
if (usb_dev == NULL) {
status = LIBHOTH_ERR_MALLOC_FAILED;
goto err_out;
}
usb_dev->info = info;
usb_dev->ctx = options->usb_ctx;
status = libusb_open(options->usb_device, &usb_dev->handle);
if (status != LIBUSB_SUCCESS) {
goto err_out;
}
status = libusb_claim_interface(usb_dev->handle, info.interface_id);
if (status != LIBUSB_SUCCESS) {
goto err_out;
}
// Fill in driver-specific data
switch (info.type) {
case LIBHOTH_USB_INTERFACE_TYPE_MAILBOX:
status = libhoth_usb_mailbox_open(usb_dev, config_descriptor);
break;
case LIBHOTH_USB_INTERFACE_TYPE_FIFO:
status = libhoth_usb_fifo_open(usb_dev, config_descriptor);
break;
default:
status = LIBHOTH_ERR_INTERFACE_NOT_FOUND;
break;
}
if (status != LIBHOTH_OK) goto err_out;
dev->send = libhoth_usb_send_request;
dev->receive = libhoth_usb_receive_response;
dev->close = libhoth_usb_close;
dev->claim = libhoth_usb_claim;
dev->release = libhoth_usb_release;
dev->user_ctx = usb_dev;
*out = dev;
libusb_free_config_descriptor(config_descriptor);
return LIBHOTH_OK;
err_out:
if (dev != NULL) {
if (usb_dev != NULL) {
if (usb_dev->handle != NULL) {
libusb_release_interface(usb_dev->handle, usb_dev->info.interface_id);
libusb_close(usb_dev->handle);
}
free(usb_dev);
}
free(dev);
}
libusb_free_config_descriptor(config_descriptor);
return status;
}
int libhoth_usb_send_request(struct libhoth_device* dev, const void* request,
size_t request_size) {
if (dev->user_ctx == NULL) {
return LIBUSB_ERROR_INVALID_PARAM;
}
struct libhoth_usb_device* usb_dev =
(struct libhoth_usb_device*)dev->user_ctx;
switch (usb_dev->info.type) {
case LIBHOTH_USB_INTERFACE_TYPE_MAILBOX:
return libhoth_usb_mailbox_send_request(usb_dev, request, request_size);
case LIBHOTH_USB_INTERFACE_TYPE_FIFO:
return libhoth_usb_fifo_send_request(usb_dev, request, request_size);
default:
return LIBHOTH_ERR_INTERFACE_NOT_FOUND;
}
return LIBUSB_ERROR_NOT_SUPPORTED;
}
int libhoth_usb_receive_response(struct libhoth_device* dev, void* response,
size_t max_response_size, size_t* actual_size,
int timeout_ms) {
if (dev->user_ctx == NULL) {
return LIBUSB_ERROR_INVALID_PARAM;
}
struct libhoth_usb_device* usb_dev =
(struct libhoth_usb_device*)dev->user_ctx;
switch (usb_dev->info.type) {
case LIBHOTH_USB_INTERFACE_TYPE_MAILBOX:
return libhoth_usb_mailbox_receive_response(
usb_dev, response, max_response_size, actual_size, timeout_ms);
case LIBHOTH_USB_INTERFACE_TYPE_FIFO:
return libhoth_usb_fifo_receive_response(
usb_dev, response, max_response_size, actual_size, timeout_ms);
default:
return LIBHOTH_ERR_INTERFACE_NOT_FOUND;
}
return LIBUSB_ERROR_NOT_SUPPORTED;
}
int libhoth_usb_close(struct libhoth_device* dev) {
int status;
if (dev->user_ctx == NULL) {
return LIBUSB_ERROR_INVALID_PARAM;
}
struct libhoth_usb_device* usb_dev =
(struct libhoth_usb_device*)dev->user_ctx;
switch (usb_dev->info.type) {
case LIBHOTH_USB_INTERFACE_TYPE_MAILBOX:
status = libhoth_usb_mailbox_close(usb_dev);
break;
case LIBHOTH_USB_INTERFACE_TYPE_FIFO:
status = libhoth_usb_fifo_close(usb_dev);
break;
default:
return LIBHOTH_ERR_INTERFACE_NOT_FOUND;
}
if (status != LIBHOTH_OK) {
return status;
}
if (usb_dev->handle != NULL) {
libusb_release_interface(usb_dev->handle, usb_dev->info.interface_id);
libusb_close(usb_dev->handle);
}
free(usb_dev);
return LIBHOTH_OK;
}
enum libusb_error transfer_status_to_error(
enum libusb_transfer_status transfer_status) {
switch (transfer_status) {
case LIBUSB_TRANSFER_COMPLETED:
return LIBUSB_SUCCESS;
case LIBUSB_TRANSFER_ERROR:
case LIBUSB_TRANSFER_CANCELLED:
return LIBUSB_ERROR_IO;
case LIBUSB_TRANSFER_TIMED_OUT:
return LIBUSB_ERROR_TIMEOUT;
case LIBUSB_TRANSFER_STALL:
return LIBUSB_ERROR_PIPE;
case LIBUSB_TRANSFER_NO_DEVICE:
return LIBUSB_ERROR_NO_DEVICE;
case LIBUSB_TRANSFER_OVERFLOW:
return LIBUSB_ERROR_OVERFLOW;
default:
return LIBUSB_ERROR_OTHER;
}
}