/
usb_host.c
624 lines (493 loc) · 17.4 KB
/
usb_host.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/**
* @file usb_host.c
* @version 7.0.0
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library 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 2.1 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.
*
* This file is part of the Simba project.
*/
#include "simba.h"
/* Only one device supported for now. */
#define DEVICE_ADDRESS 1
struct module_t {
int initialized;
/* A list of all drivers. */
struct usb_host_driver_t *drivers_p;
struct usb_host_device_driver_t *device_drivers_p;
#if CONFIG_FS_CMD_USB_HOST_LIST == 1
struct fs_command_t cmd_list;
#endif
};
static int device_enumerate(struct usb_host_device_t *device_p);
#include "usb_host_port.i"
static struct module_t module;
#if CONFIG_FS_CMD_USB_HOST_LIST == 1
static int cmd_list_cb(int argc,
const char *argv[],
chan_t *out_p,
chan_t *in_p,
void * arg_p,
void *call_arg_p)
{
int i;
int verbose = 0;
struct usb_host_driver_t *self_p;
struct usb_host_device_t *device_p;
if (argc == 2) {
if (strcmp("-v", argv[1]) == 0) {
verbose = 1;
}
}
std_fprintf(out_p, FSTR("BUS ADDRESS VID PID DESCRIPTION\r\n"));
self_p = module.drivers_p;
while (self_p != NULL) {
device_p = &self_p->devices_p[0];
for (i = 0; i < self_p->length; i++, device_p++) {
if (device_p->state != USB_HOST_DEVICE_STATE_ATTACHED) {
continue;
}
std_fprintf(out_p,
FSTR("%3d %8d %04x %04x %s\r\n"),
device_p->self_p->id,
device_p->address,
device_p->vid,
device_p->pid,
device_p->description_p);
if (verbose == 1) {
usb_format_descriptors(out_p,
device_p->descriptors.buf,
device_p->descriptors.size);
}
}
self_p = self_p->next_p;
}
return (0);
}
#endif
/**
* Set the device address.
*/
static int device_set_address(struct usb_host_device_t *device_p,
uint16_t address)
{
struct usb_setup_t setup;
std_printf(FSTR("Setting device address.\r\n"));
/* Initiate the setup. */
setup.request_type = (REQUEST_TYPE_DATA_DIRECTION_HOST_TO_DEVICE);
setup.request = REQUEST_SET_ADDRESS;
setup.u.set_address.device_address = address;
setup.u.set_address.zero = 0;
setup.length = 0;
/* Write the setup to the hardware and read the response into
* dst_p. */
if (usb_host_device_control_transfer(device_p, &setup, NULL, 0) != 0) {
std_printf(FSTR("Unable to set device address.\r\n"));
return (-1);
}
device_p->address = address;
return (usb_pipe_set_address(device_p->self_p,
device_p->pipes[0],
address));
}
/**
* Set the device configuration.
*/
int usb_host_device_set_configuration(struct usb_host_device_t *device_p,
uint8_t configuration)
{
struct usb_setup_t setup;
struct usb_pipe_t *pipe_p;
int i, j;
uint8_t *buf_p;
size_t size;
struct usb_descriptor_interface_t *int_p = NULL;
struct usb_descriptor_endpoint_t *ep_p = NULL;
int type;
std_printf(FSTR("Setting device configuration %d.\r\n"), configuration);
/* Initiate the setup. */
setup.request_type = REQUEST_TYPE_DATA_DIRECTION_HOST_TO_DEVICE;
setup.request = REQUEST_SET_CONFIGURATION;
setup.u.set_configuration.configuration_value = configuration;
setup.u.set_configuration.zero = 0;
setup.length = 0;
/* Write the setup to the hardware and read the response into
* dst_p. */
if (usb_host_device_control_transfer(device_p, &setup, NULL, 0) != 0) {
std_printf(FSTR("Unable to set configration.\r\n"));
return (-1);
}
device_p->current.configuration = configuration;
device_p->current.descriptor.conf_p = NULL;
/* Iterate over all interfaces in the configuration and allocate a
* pipe for each endpoint. */
buf_p = device_p->descriptors.buf;
size = device_p->descriptors.size;
for (i = 0;
(int_p = usb_desc_get_interface(buf_p, size, 1, i)) != NULL;
i++) {
for (j = 1;
(ep_p = usb_desc_get_endpoint(buf_p, size, 1, i, j)) != NULL;
j++) {
switch (ENDPOINT_ATTRIBUTES_TRANSFER_TYPE(ep_p->attributes)) {
case ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_CONTROL:
type = USB_PIPE_TYPE_CONTROL;
break;
case ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_ISOCHRONOUS:
type = USB_PIPE_TYPE_ISOCHRONOUS;
break;
case ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_BULK:
type = USB_PIPE_TYPE_BULK;
break;
case ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_INTERRUPT:
type = USB_PIPE_TYPE_INTERRUPT;
break;
default:
return (-1);
}
pipe_p = usb_pipe_alloc(device_p->self_p,
type,
(ep_p->endpoint_address & 0x7f),
device_p->address,
ep_p->interval);
if (pipe_p == NULL) {
std_printf(FSTR("Pipe allocation failed.\r\n"));
return (-1);
}
}
}
return (0);
}
/**
* Get the device descriptors from the device.
*/
static ssize_t device_get_descriptor_device(struct usb_host_device_t *device_p,
struct usb_descriptor_device_t *desc_p)
{
struct usb_setup_t setup;
/* Initiate the setup datastructure. */
setup.request_type = REQUEST_TYPE_DATA_DIRECTION_DEVICE_TO_HOST;
setup.request = REQUEST_GET_DESCRIPTOR;
setup.u.get_descriptor.descriptor_index = 0;
setup.u.get_descriptor.descriptor_type = DESCRIPTOR_TYPE_DEVICE;
setup.u.get_descriptor.language_id = 0;
setup.length = 8; /* Only read 8 bytes in the first read. */
if (usb_host_device_control_transfer(device_p, &setup, desc_p, setup.length)
!= setup.length) {
std_printf(FSTR("failed to read first 8 bytes of the device descriptor.\r\n"));
return (-1);
}
/* Set the maximum packet size the device supports. */
device_p->max_packet_size = desc_p->max_packet_size_0;
std_printf(FSTR("max packet size = %d\r\n"), device_p->max_packet_size);
/* Read the whole descriptor. */
setup.length = sizeof(*desc_p);
if (usb_host_device_control_transfer(device_p, &setup, desc_p, setup.length)
!= setup.length) {
std_printf(FSTR("failed to read the device descriptor.\r\n"));
return (-1);
}
device_p->current.descriptor.dev_p = desc_p;
device_p->vid = desc_p->id_vendor;
device_p->pid = desc_p->id_product;
return (0);
}
/**
* Get a configuration descriptors from the device.
*/
static ssize_t device_get_descriptor_configuration(struct usb_host_device_t *device_p,
uint8_t index,
uint8_t *dst_p,
size_t size)
{
struct usb_setup_t setup;
struct usb_descriptor_configuration_t *desc_p;
desc_p = (struct usb_descriptor_configuration_t *)dst_p;
std_printf(FSTR("Getting device configuration.\r\n"));
/* Initiate the setup. */
setup.request_type = REQUEST_TYPE_DATA_DIRECTION_DEVICE_TO_HOST;
setup.request = REQUEST_GET_DESCRIPTOR;
setup.u.get_descriptor.descriptor_index = index;
setup.u.get_descriptor.descriptor_type = DESCRIPTOR_TYPE_CONFIGURATION;
setup.u.get_descriptor.language_id = 0;
setup.length = size;
/* Write the setup to the hardware and read the response into */
/* dst_p. */
if (usb_host_device_control_transfer(device_p,
&setup,
desc_p,
setup.length) != setup.length) {
std_printf(FSTR("Unable to read the configuration descriptor from the device.\r\n"));
return (-1);
}
return (setup.length);
}
/**
* Get all descriptors from the device.
*/
static ssize_t device_get_descriptors(struct usb_host_device_t *device_p)
{
struct usb_descriptor_configuration_t *desc_p;
uint8_t *dst_p;
dst_p = device_p->descriptors.buf;
dst_p += device_p->descriptors.size;
/* Get the configuration descriptor at index 0. */
if (device_get_descriptor_configuration(device_p,
0,
dst_p,
sizeof(struct usb_descriptor_configuration_t))
!= sizeof(struct usb_descriptor_configuration_t)) {
return (-1);
}
desc_p = (struct usb_descriptor_configuration_t *)dst_p;
if (desc_p->total_length > sizeof(struct usb_descriptor_configuration_t)) {
if (device_get_descriptor_configuration(device_p,
0,
dst_p,
desc_p->total_length)
!= desc_p->total_length) {
return (-1);
}
}
device_p->descriptors.size += desc_p->total_length;
return (0);
}
static struct usb_host_device_driver_t *
find_device_driver(struct usb_host_device_t *device_p)
{
struct usb_host_device_driver_t *driver_p;
driver_p = module.device_drivers_p;
while (driver_p != NULL) {
if (driver_p->supports(device_p)) {
return (driver_p);
}
driver_p = driver_p->next_p;
}
return (NULL);
}
/**
* Enumerate a detected device. After the enumeration the device has
* been configured and is ready to use by the application.
*/
static int device_enumerate(struct usb_host_device_t *device_p)
{
struct usb_descriptor_device_t *device_desc_p;
struct usb_host_device_driver_t *driver_p;
struct usb_pipe_t *pipe_p = NULL;
union usb_message_t message;
std_printf(FSTR("Starting the USB enumeration process for a device.\r\n"));
/* Set the initial maximum packet size.*/
device_p->max_packet_size = 8;
/* Reset the device. */
if (device_reset(device_p) != 0) {
return (-1);
}
/* Allocate a control pipe. */
pipe_p = usb_pipe_alloc(device_p->self_p,
USB_PIPE_TYPE_CONTROL,
0,
0,
0);
if (pipe_p == NULL) {
goto err;
}
device_p->pipes[0] = pipe_p;
/* Get the device descriptor. */
device_desc_p = (struct usb_descriptor_device_t *)(device_p->descriptors.buf);
if (device_get_descriptor_device(device_p, device_desc_p) != 0) {
goto err;
}
device_p->descriptors.size = sizeof(struct usb_descriptor_device_t);
/* Assign the device an address. */
if (device_set_address(device_p, DEVICE_ADDRESS) != 0) {
std_printf(FSTR("failed to set device address\r\n"));
goto err;
}
/* Get various descriptors from the device. */
if (device_get_descriptors(device_p) != 0) {
std_printf(FSTR("failed to get device descriptors\r\n"));
goto err;
}
driver_p = find_device_driver(device_p);
if (driver_p != NULL) {
/* Driver specific enumeration. */
if (driver_p->enumerate(device_p) != 0) {
goto err;
}
} else {
std_printf(FSTR("Unsupported USB device.\r\n"));
}
device_p->state = USB_HOST_DEVICE_STATE_ATTACHED;
device_p->description_p = "";
std_printf(FSTR("Enumeration done.\r\n"));
usb_format_descriptors(sys_get_stdout(),
device_p->descriptors.buf,
device_p->descriptors.size);
/* Notify the controller. */
message.add.header.type = USB_MESSAGE_TYPE_ADD;
message.add.device = device_p->id;
queue_write(&device_p->self_p->control,
&message,
sizeof(message));
return (0);
err:
if (pipe_p != NULL) {
usb_pipe_free(device_p->self_p, pipe_p);
}
return (-1);
}
int usb_host_module_init(void)
{
/* Return immediately if the module is already initialized. */
if (module.initialized == 1) {
return (0);
}
module.initialized = 1;
#if CONFIG_FS_CMD_USB_HOST_LIST == 1
fs_command_init(&module.cmd_list,
FSTR("/drivers/usb_host/list"),
cmd_list_cb,
NULL);
fs_command_register(&module.cmd_list);
#endif
return (0);
}
int usb_host_init(struct usb_host_driver_t *self_p,
struct usb_device_t *dev_p,
struct usb_host_device_t *devices_p,
size_t length)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(dev_p != NULL, EINVAL);
ASSERTN(devices_p != NULL, EINVAL);
ASSERTN(length > 0, EINVAL);
int i;
self_p->dev_p = dev_p;
self_p->length = length;
self_p->devices_p = devices_p;
queue_init(&self_p->control, NULL, 0);
for (i = 0; i < length; i++) {
self_p->devices_p[i].id = i;
self_p->devices_p[i].state = USB_HOST_DEVICE_STATE_NONE;
self_p->devices_p[i].self_p = self_p;
}
return (usb_host_port_init(self_p, dev_p));
}
int usb_host_start(struct usb_host_driver_t *self_p)
{
ASSERTN(self_p != NULL, EINVAL);
if (usb_host_port_start(self_p) != 0) {
return (-1);
}
/* Add the driver to the list. */
self_p->next_p = module.drivers_p;
module.drivers_p = self_p;
return (0);
}
int usb_host_stop(struct usb_host_driver_t *self_p)
{
ASSERTN(self_p != NULL, EINVAL);
return (usb_host_port_stop(self_p));
}
int usb_host_driver_add(struct usb_host_driver_t *self_p,
struct usb_host_device_driver_t *driver_p,
void *arg_p)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(driver_p != NULL, EINVAL);
driver_p->next_p = module.device_drivers_p;
module.device_drivers_p = driver_p;
return (0);
}
struct usb_host_device_t *
usb_host_device_open(struct usb_host_driver_t *self_p,
int device)
{
ASSERTN(self_p != NULL, EINVAL);
struct usb_host_device_t *device_p;
if (device > self_p->length - 1) {
return (NULL);
}
device_p = &self_p->devices_p[device];
if (device_p->state != USB_HOST_DEVICE_STATE_ATTACHED) {
return (NULL);
}
return (device_p);
}
int usb_host_device_close(struct usb_host_driver_t *self_p,
int device)
{
ASSERTN(self_p != NULL, EINVAL);
if (device > self_p->length - 1) {
return (-1);
}
return (0);
}
ssize_t usb_host_device_read(struct usb_host_device_t *device_p,
int endpoint,
void *buf_p,
size_t size)
{
ASSERTN(device_p != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
return (usb_host_port_device_read(device_p, endpoint, buf_p, size));
}
ssize_t usb_host_device_write(struct usb_host_device_t *device_p,
int endpoint,
const void *buf_p,
size_t size)
{
ASSERTN(device_p != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
return (usb_host_port_device_write(device_p, endpoint, buf_p, size));
}
ssize_t usb_host_device_control_transfer(struct usb_host_device_t *device_p,
struct usb_setup_t *setup_p,
void *buf_p,
size_t size)
{
ASSERTN(device_p != NULL, EINVAL);
ASSERTN(setup_p != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
std_printf(FSTR("Starting a control transfer.\r\n"));
if (usb_host_port_device_write_setup(device_p, setup_p) != sizeof(*setup_p)) {
std_printf(FSTR("Failed to write SETUP.\r\n"));
return (-1);
}
/* Data and status stages. */
if (setup_p->request_type & REQUEST_TYPE_DATA_DIRECTION_DEVICE_TO_HOST) {
if (size > 0) {
/* Read data from the device. */
if (usb_host_port_device_read(device_p, 0, buf_p, size) != size) {
return (-1);
}
}
if (usb_host_port_device_write(device_p, 0, NULL, 0) != 0) {
return (-1);
}
} else {
if (size > 0) {
/* Write data to the device. */
if (usb_host_port_device_write(device_p, 0, buf_p, size) != size) {
return (-1);
}
}
if (usb_host_port_device_read(device_p, 0, NULL, 0) != 0) {
return (-1);
}
}
return (size);
}