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
|
/*****************************************************************************
* Copyright (c) 2013 IBM Corporation
* All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the BSD License
* which accompanies this distribution, and is available at
* http://www.opensource.org/licenses/bsd-license.php
*
* Contributors:
* IBM Corporation - initial implementation
*****************************************************************************/
#include <string.h>
#include "usb-core.h"
#undef DEBUG
//#define DEBUG
#ifdef DEBUG
#define dprintf(_x ...) do { printf(_x); } while(0)
#else
#define dprintf(_x ...)
#endif
#define __unused __attribute__((unused))
struct usb_hcd_ops *head;
struct usb_dev *devpool;
#define USB_DEVPOOL_SIZE 4096
static struct usb_dev *usb_alloc_devpool(void)
{
struct usb_dev *head, *curr, *prev;
unsigned int dev_count = 0, i;
head = SLOF_alloc_mem(USB_DEVPOOL_SIZE);
if (!head)
return NULL;
dev_count = USB_DEVPOOL_SIZE/sizeof(struct usb_dev);
dprintf("%s: %d number of devices\n", __func__, dev_count);
/* Although an array, link them*/
for (i = 0, curr = head, prev = NULL; i < dev_count; i++, curr++) {
if (prev)
prev->next = curr;
curr->next = NULL;
prev = curr;
}
#ifdef DEBUG
for (i = 0, curr = head; curr; curr = curr->next)
printf("%s: %d dev %p\n", __func__, i++, curr);
#endif
return head;
}
struct usb_dev *usb_devpool_get(void)
{
struct usb_dev *new;
if (!devpool) {
devpool = usb_alloc_devpool();
if (!devpool)
return NULL;
}
new = devpool;
devpool = devpool->next;
memset(new, 0, sizeof(*new));
new->next = NULL;
return new;
}
void usb_devpool_put(struct usb_dev *dev)
{
struct usb_dev *curr;
if (!dev && !devpool)
return;
curr = devpool;
while (curr->next)
curr = curr->next;
curr->next = dev;
dev->next = NULL;
}
#ifndef DEBUG
#define validate_hcd_ops(dev) (dev && dev->hcidev && dev->hcidev->ops)
#else
int validate_hcd_ops(struct usb_dev *dev)
{
int ret = true;
if (!dev) {
printf("dev is NULL\n");
ret = false;
} else if (!dev->hcidev) {
printf("hcidev is NULL\n");
ret = false;
} else if (!dev->hcidev->ops) {
printf("ops is NULL\n");
ret = false;
}
return ret;
}
#endif
struct usb_pipe *usb_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
char *buf, size_t len)
{
if (validate_hcd_ops(dev) && dev->hcidev->ops->get_pipe)
return dev->hcidev->ops->get_pipe(dev, ep, buf, len);
else {
printf("%s: Failed\n", __func__);
return NULL;
}
}
void usb_put_pipe(struct usb_pipe *pipe)
{
struct usb_dev *dev = NULL;
if (pipe && pipe->dev) {
dev = pipe->dev;
if (validate_hcd_ops(dev) && dev->hcidev->ops->put_pipe)
dev->hcidev->ops->put_pipe(pipe);
}
}
int usb_poll_intr(struct usb_pipe *pipe, uint8_t *buf)
{
struct usb_dev *dev = NULL;
if (pipe && pipe->dev) {
dev = pipe->dev;
if (validate_hcd_ops(dev) && dev->hcidev->ops->poll_intr)
return dev->hcidev->ops->poll_intr(pipe, buf);
}
return 0;
}
void usb_hcd_register(struct usb_hcd_ops *ops)
{
struct usb_hcd_ops *list;
if (!ops)
printf("Error");
dprintf("Registering %s %d\n", ops->name, ops->usb_type);
if (head) {
list = head;
while (list->next)
list = list->next;
list->next = ops;
} else
head = ops;
}
void usb_hcd_init(void *hcidev)
{
struct usb_hcd_dev *dev = hcidev;
struct usb_hcd_ops *list = head;
if (!dev) {
printf("Device Error");
return;
}
while (list) {
if (list->usb_type == dev->type) {
dprintf("usb_ops(%p) for the controller found\n", list);
dev->ops = list;
dev->ops->init(dev);
return;
}
list = list->next;
}
dprintf("usb_ops for the controller not found\n");
}
void usb_hcd_exit(void *_hcidev)
{
struct usb_hcd_dev *hcidev = _hcidev;
dprintf("%s: enter \n", __func__);
if (!hcidev) {
printf("Device Error");
return;
}
if (hcidev->ops->exit)
hcidev->ops->exit(hcidev);
}
int usb_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
{
struct usb_dev *dev = NULL;
if (!pipe)
return false;
dev = pipe->dev;
if (validate_hcd_ops(dev) && dev->hcidev->ops->send_ctrl)
return dev->hcidev->ops->send_ctrl(pipe, req, data);
else {
printf("%s: Failed\n", __func__);
return false;
}
}
int usb_transfer_ctrl(void *dev, void *req, void *data)
{
struct usb_pipe *pipe = NULL;
struct usb_dev *usbdev;
if (!dev)
return false;
usbdev = (struct usb_dev *)dev;
pipe = usbdev->control;
return usb_send_ctrl(pipe, req, data);
}
int usb_transfer_bulk(void *dev, int dir, void *td, void *td_phys, void *data, int size)
{
struct usb_pipe *pipe = NULL;
struct usb_dev *usbdev;
if (!dev)
return false;
usbdev = (struct usb_dev *)dev;
pipe = (dir == USB_PIPE_OUT) ? usbdev->bulk_out : usbdev->bulk_in;
if (!pipe)
return false;
if (validate_hcd_ops(usbdev) && usbdev->hcidev->ops->transfer_bulk)
return usbdev->hcidev->ops->transfer_bulk(pipe, td, td_phys, data, size);
else {
printf("%s: Failed\n", __func__);
return false;
}
}
/*
* USB Specification 1.1
* 9.3 USB Device Requests
* 9.4 Standard Device Requests
*/
static int usb_set_address(struct usb_dev *dev, uint32_t port)
{
struct usb_dev_req req;
struct usb_hcd_dev *hcidev;
if (!dev)
return false;
hcidev = dev->hcidev;
req.bmRequestType = 0;
req.bRequest = REQ_SET_ADDRESS;
req.wIndex = 0;
req.wLength = 0;
req.wValue = cpu_to_le16((uint16_t)(hcidev->nextaddr));
if (usb_send_ctrl(dev->control, &req, NULL)) {
dev->addr = hcidev->nextaddr++;
return true;
} else
return false;
}
static int usb_get_device_descr(struct usb_dev *dev, void *data, size_t size)
{
struct usb_dev_req req;
if (!dev)
return false;
req.bmRequestType = 0x80;
req.bRequest = REQ_GET_DESCRIPTOR;
req.wIndex = 0;
req.wLength = cpu_to_le16((uint16_t) size);
req.wValue = cpu_to_le16(DESCR_TYPE_DEVICE << 8);
return usb_send_ctrl(dev->control, &req, data);
}
static int usb_get_config_descr(struct usb_dev *dev, void *data, size_t size)
{
struct usb_dev_req req;
if (!dev)
return false;
req.bmRequestType = 0x80;
req.bRequest = REQ_GET_DESCRIPTOR;
req.wIndex = 0;
req.wLength = cpu_to_le16((uint16_t) size);
req.wValue = cpu_to_le16(DESCR_TYPE_CONFIGURATION << 8);
return usb_send_ctrl(dev->control, &req, data);
}
static int usb_set_config(struct usb_dev *dev, uint8_t cfg_value)
{
struct usb_dev_req req;
if (!dev)
return false;
req.bmRequestType = 0x00;
req.bRequest = REQ_SET_CONFIGURATION;
req.wIndex = 0;
req.wLength = 0;
req.wValue = cpu_to_le16(0x00FF & cfg_value);
return usb_send_ctrl(dev->control, &req, NULL);
}
static int usb_clear_halt(struct usb_pipe *pipe)
{
struct usb_dev_req req;
struct usb_dev *dev;
if (pipe && pipe->dev) {
dev = pipe->dev;
dprintf("Clearing port %d dir %d type %d\n",
pipe->epno, pipe->dir, pipe->type);
req.bmRequestType = REQT_DIR_OUT | REQT_REC_EP;
req.bRequest = REQ_CLEAR_FEATURE;
req.wValue = FEATURE_ENDPOINT_HALT;
req.wIndex = cpu_to_le16(pipe->epno | pipe->dir);
req.wLength = 0;
return usb_send_ctrl(dev->control, &req, NULL);
}
return false;
}
int usb_dev_populate_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
void *buf, size_t len)
{
uint8_t dir, type;
dir = (ep->bEndpointAddress & 0x80) >> 7;
type = ep->bmAttributes & USB_EP_TYPE_MASK;
dprintf("EP: %s: %d size %d type %d\n", dir ? "IN " : "OUT",
ep->bEndpointAddress & 0xF, le16_to_cpu(ep->wMaxPacketSize),
type);
if (type == USB_EP_TYPE_BULK) {
if (dir)
dev->bulk_in = usb_get_pipe(dev, ep, buf, len);
else
dev->bulk_out = usb_get_pipe(dev, ep, buf, len);
} else if (type == USB_EP_TYPE_INTR)
dev->intr = usb_get_pipe(dev, ep, buf, len);
return true;
}
static void usb_dev_copy_epdesc(struct usb_dev *dev, struct usb_ep_descr *ep)
{
uint32_t ep_cnt;
ep_cnt = dev->ep_cnt;
if (ep_cnt < USB_DEV_EP_MAX)
memcpy((void *)&dev->ep[ep_cnt], ep, sizeof(*ep));
else
dprintf("usb-core: only %d EPs supported\n", USB_DEV_EP_MAX);
dev->ep_cnt++;
}
int usb_hid_init(void *vdev)
{
struct usb_dev *dev;
dev = (struct usb_dev *) vdev;
if (!dev)
return false;
if (dev->class == DEV_HID_KEYB)
usb_hid_kbd_init(dev);
return true;
}
int usb_hid_exit(void *vdev)
{
struct usb_dev *dev;
dev = (struct usb_dev *) vdev;
if (!dev)
return false;
if (dev->class == DEV_HID_KEYB)
usb_hid_kbd_exit(dev);
return true;
}
#define usb_get_intf_class(x) ((x & 0x00FF0000) >> 16)
int usb_msc_init(void *vdev)
{
struct usb_dev *dev;
int i;
dev = (struct usb_dev *) vdev;
dprintf("%s: enter %x\n", __func__, dev->class);
if (!dev)
return false;
if (usb_get_intf_class(dev->class) == 8) {
for (i = 0; i < dev->ep_cnt; i++) {
if ((dev->ep[i].bmAttributes & USB_EP_TYPE_MASK)
== USB_EP_TYPE_BULK)
usb_dev_populate_pipe(dev, &dev->ep[i], NULL, 0);
}
}
return true;
}
int usb_msc_exit(void *vdev)
{
struct usb_dev *dev;
dev = (struct usb_dev *) vdev;
dprintf("%s: enter %x\n", __func__, dev->class);
if (!dev)
return false;
if (usb_get_intf_class(dev->class) == 8) {
if (dev->bulk_in)
usb_put_pipe(dev->bulk_in);
if (dev->bulk_out)
usb_put_pipe(dev->bulk_out);
}
return true;
}
static int usb_msc_reset(struct usb_dev *dev)
{
struct usb_dev_req req;
if (!dev)
return false;
req.bmRequestType = REQT_TYPE_CLASS | REQT_REC_INTERFACE | REQT_DIR_OUT;
req.bRequest = 0xFF;
req.wLength = 0;
req.wValue = 0;
req.wIndex = cpu_to_le16(dev->intf_num);
return usb_send_ctrl(dev->control, &req, NULL);
}
void usb_msc_resetrecovery(struct usb_dev *dev)
{
// usb_msc_reset(dev);
usb_clear_halt(dev->bulk_in);
usb_clear_halt(dev->bulk_out);
SLOF_msleep(2);
}
static int usb_handle_device(struct usb_dev *dev, struct usb_dev_config_descr *cfg,
uint8_t *ptr, uint16_t len)
{
struct usb_dev_intf_descr *intf = NULL;
struct usb_ep_descr *ep = NULL;
struct usb_dev_hid_descr *hid __unused = NULL;
uint8_t desc_len, desc_type;
len -= sizeof(struct usb_dev_config_descr);
ptr = (uint8_t *)(ptr + sizeof(struct usb_dev_config_descr));
while (len > 0) {
desc_len = *ptr;
desc_type = *(ptr + 1);
switch (desc_type) {
case DESCR_TYPE_INTERFACE:
intf = (struct usb_dev_intf_descr *)ptr;
dev->class = intf->bInterfaceClass << 16 |
intf->bInterfaceSubClass << 8 |
intf->bInterfaceProtocol;
break;
case DESCR_TYPE_ENDPOINT:
ep = (struct usb_ep_descr *)ptr;
dev->intf_num = intf->bInterfaceNumber;
usb_dev_copy_epdesc(dev, ep);
break;
case DESCR_TYPE_HID:
hid = (struct usb_dev_hid_descr *)ptr;
dprintf("hid-report %d size %d\n",
hid->bReportType, le16_to_cpu(hid->wReportLength));
break;
case DESCR_TYPE_HUB:
break;
default:
printf("ptr %p desc_type %d\n", ptr, desc_type);
}
ptr += desc_len;
len -= desc_len;
}
return true;
}
int setup_new_device(struct usb_dev *dev, unsigned int port)
{
struct usb_dev_descr descr;
struct usb_dev_config_descr cfg;
struct usb_ep_descr ep;
uint16_t len;
void *data = NULL;
dprintf("usb: %s - port %d\n", __func__, port);
dev->addr = 0;
dev->port = port;
ep.bEndpointAddress = 0;
ep.bmAttributes = USB_EP_TYPE_CONTROL;
ep.wMaxPacketSize = cpu_to_le16(8);
dev->control = usb_get_pipe(dev, &ep, NULL, 0);
if (!usb_get_device_descr(dev, &descr, 8))
goto fail;
dev->control->mps = descr.bMaxPacketSize0;
if (dev->speed != USB_SUPER_SPEED) {
if(!usb_set_address(dev, port))
goto fail;
}
mb();
SLOF_msleep(100);
if (!usb_get_device_descr(dev, &descr, sizeof(struct usb_dev_descr)))
goto fail;
if (!usb_get_config_descr(dev, &cfg, sizeof(struct usb_dev_config_descr)))
goto fail;
len = le16_to_cpu(cfg.wTotalLength);
/* No device config descriptor present */
if (len == sizeof(struct usb_dev_config_descr))
goto fail;
data = SLOF_dma_alloc(len);
if (!data) {
printf("%s: alloc failed %d\n", __func__, port);
goto fail;
}
if (!usb_get_config_descr(dev, data, len))
goto fail_mem_free;
if (!usb_set_config(dev, cfg.bConfigurationValue))
goto fail_mem_free;
mb();
SLOF_msleep(100);
if (!usb_handle_device(dev, &cfg, data, len))
goto fail_mem_free;
switch (usb_get_intf_class(dev->class)) {
case 3:
dprintf("HID found %06X\n", dev->class);
slof_usb_handle(dev);
break;
case 8:
dprintf("MASS STORAGE found %d %06X\n", dev->intf_num,
dev->class);
if ((dev->class & 0x50) != 0x50) { /* Bulk-only supported */
printf("Device not supported %06X\n", dev->class);
goto fail_mem_free;
}
if (!usb_msc_reset(dev)) {
printf("%s: bulk reset failed\n", __func__);
goto fail_mem_free;
}
SLOF_msleep(100);
slof_usb_handle(dev);
break;
case 9:
dprintf("HUB found\n");
slof_usb_handle(dev);
break;
default:
printf("USB Interface class -%x- Not supported\n", dev->class);
break;
}
SLOF_dma_free(data, len);
return true;
fail_mem_free:
SLOF_dma_free(data, len);
fail:
return false;
}
|