/
core.c
749 lines (635 loc) · 18.5 KB
/
core.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
* Human Interface Device (HID) USB class core
*
* Copyright (c) 2018 Intel Corporation
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(usb_hid, CONFIG_USB_HID_LOG_LEVEL);
#include <zephyr/sys/byteorder.h>
#include <usb_device.h>
#include <usb_descriptor.h>
#include <class/usb_hid.h>
#include <stdlib.h>
#define HID_INT_IN_EP_IDX 0
#define HID_INT_OUT_EP_IDX 1
struct usb_hid_class_subdescriptor {
uint8_t bDescriptorType;
uint16_t wDescriptorLength;
} __packed;
struct usb_hid_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
/*
* Specification says at least one Class Descriptor needs to
* be present (Report Descriptor).
*/
struct usb_hid_class_subdescriptor subdesc[1];
} __packed;
struct usb_hid_config {
struct usb_if_descriptor if0;
struct usb_hid_descriptor if0_hid;
struct usb_ep_descriptor if0_int_in_ep;
#ifdef CONFIG_ENABLE_HID_INT_OUT_EP
struct usb_ep_descriptor if0_int_out_ep;
#endif
} __packed;
#if defined(CONFIG_USB_HID_BOOT_PROTOCOL)
#define INITIALIZER_IF \
{ \
.bLength = sizeof(struct usb_if_descriptor), \
.bDescriptorType = USB_DESC_INTERFACE, \
.bInterfaceNumber = 0, \
.bAlternateSetting = 0, \
.bNumEndpoints = 1, \
.bInterfaceClass = USB_BCC_HID, \
.bInterfaceSubClass = 1, \
.bInterfaceProtocol = 0, \
.iInterface = 0, \
}
#else
#define INITIALIZER_IF \
{ \
.bLength = sizeof(struct usb_if_descriptor), \
.bDescriptorType = USB_DESC_INTERFACE, \
.bInterfaceNumber = 0, \
.bAlternateSetting = 0, \
.bNumEndpoints = 1, \
.bInterfaceClass = USB_BCC_HID, \
.bInterfaceSubClass = 0, \
.bInterfaceProtocol = 0, \
.iInterface = 0, \
}
#endif
/* Descriptor length needs to be set after initialization */
#define INITIALIZER_IF_HID \
{ \
.bLength = sizeof(struct usb_hid_descriptor), \
.bDescriptorType = USB_DESC_HID, \
.bcdHID = sys_cpu_to_le16(USB_HID_VERSION), \
.bCountryCode = 0, \
.bNumDescriptors = 1, \
.subdesc[0] = { \
.bDescriptorType = USB_DESC_HID_REPORT, \
.wDescriptorLength = 0, \
}, \
}
#define INITIALIZER_IF_EP(addr, attr, mps) \
{ \
.bLength = sizeof(struct usb_ep_descriptor), \
.bDescriptorType = USB_DESC_ENDPOINT, \
.bEndpointAddress = addr, \
.bmAttributes = attr, \
.wMaxPacketSize = sys_cpu_to_le16(mps), \
.bInterval = CONFIG_USB_HID_POLL_INTERVAL_MS, \
}
#ifdef CONFIG_ENABLE_HID_INT_OUT_EP
#define DEFINE_HID_DESCR(x, _) \
USBD_CLASS_DESCR_DEFINE(primary, x) \
struct usb_hid_config hid_cfg_##x = { \
/* Interface descriptor */ \
.if0 = INITIALIZER_IF, \
.if0_hid = INITIALIZER_IF_HID, \
.if0_int_in_ep = \
INITIALIZER_IF_EP(AUTO_EP_IN, \
USB_DC_EP_INTERRUPT, \
CONFIG_HID_INTERRUPT_EP_MPS), \
.if0_int_out_ep = \
INITIALIZER_IF_EP(AUTO_EP_OUT, \
USB_DC_EP_INTERRUPT, \
CONFIG_HID_INTERRUPT_EP_MPS), \
}
#else
#define DEFINE_HID_DESCR(x, _) \
USBD_CLASS_DESCR_DEFINE(primary, x) \
struct usb_hid_config hid_cfg_##x = { \
/* Interface descriptor */ \
.if0 = INITIALIZER_IF, \
.if0_hid = INITIALIZER_IF_HID, \
.if0_int_in_ep = \
INITIALIZER_IF_EP(AUTO_EP_IN, \
USB_DC_EP_INTERRUPT, \
CONFIG_HID_INTERRUPT_EP_MPS), \
}
#endif
struct hid_device_info {
const uint8_t *report_desc;
size_t report_size;
const struct hid_ops *ops;
#ifdef CONFIG_USB_DEVICE_SOF
uint32_t sof_cnt[CONFIG_USB_HID_REPORTS];
bool idle_on;
uint8_t idle_rate[CONFIG_USB_HID_REPORTS];
#endif
#ifdef CONFIG_USB_HID_BOOT_PROTOCOL
uint8_t protocol;
#endif
bool configured;
bool suspended;
struct usb_dev_data common;
};
static sys_slist_t usb_hid_devlist;
static int hid_on_get_idle(struct hid_device_info *dev_data,
struct usb_setup_packet *setup, int32_t *len,
uint8_t **data)
{
#ifdef CONFIG_USB_DEVICE_SOF
uint8_t report_id = (uint8_t)setup->wValue;
if (report_id > CONFIG_USB_HID_REPORTS) {
LOG_ERR("Report id out of limit: %d", report_id);
return -ENOTSUP;
}
LOG_DBG("Get Idle callback, report_id: %d", report_id);
if (report_id == 0U) {
/*
* Common value, stored on Set Idle request with Report ID 0,
* can be outdated because the duration can also
* be set individually for each Report ID.
*/
*data = &dev_data->idle_rate[0];
*len = sizeof(dev_data->idle_rate[0]);
} else {
*data = &dev_data->idle_rate[report_id - 1];
*len = sizeof(dev_data->idle_rate[report_id - 1]);
}
return 0;
#else
return -ENOTSUP;
#endif
}
static int hid_on_get_report(struct hid_device_info *dev_data,
struct usb_setup_packet *setup, int32_t *len,
uint8_t **data)
{
LOG_DBG("Get Report callback");
/* TODO: Do something. */
return -ENOTSUP;
}
static int hid_on_get_protocol(struct hid_device_info *dev_data,
struct usb_setup_packet *setup, int32_t *len,
uint8_t **data)
{
#ifdef CONFIG_USB_HID_BOOT_PROTOCOL
uint32_t size = sizeof(dev_data->protocol);
if (setup->wValue) {
LOG_ERR("wValue should be 0");
return -ENOTSUP;
}
LOG_DBG("Get Protocol: %d", dev_data->protocol);
*data = &dev_data->protocol;
*len = size;
return 0;
#else
return -ENOTSUP;
#endif
}
static int hid_on_set_idle(struct hid_device_info *dev_data,
struct usb_setup_packet *setup, int32_t *len,
uint8_t **data)
{
#ifdef CONFIG_USB_DEVICE_SOF
uint8_t rate = (uint8_t)(setup->wValue >> 8);
uint8_t report_id = (uint8_t)setup->wValue;
if (report_id > CONFIG_USB_HID_REPORTS) {
LOG_ERR("Report id out of limit: %d", report_id);
return -ENOTSUP;
}
LOG_DBG("Set Idle callback, rate: %d, report_id: %d", rate, report_id);
if (report_id == 0U) {
for (uint16_t i = 0; i < CONFIG_USB_HID_REPORTS; i++) {
dev_data->idle_rate[i] = rate;
dev_data->sof_cnt[i] = 0U;
}
} else {
dev_data->idle_rate[report_id - 1] = rate;
dev_data->sof_cnt[report_id - 1] = 0U;
}
dev_data->idle_on = (bool)setup->wValue;
return 0;
#else
return -ENOTSUP;
#endif
}
static int hid_on_set_report(struct hid_device_info *dev_data,
struct usb_setup_packet *setup, int32_t *len,
uint8_t **data)
{
LOG_DBG("Set Report callback");
/* TODO: Do something. */
return -ENOTSUP;
}
static int hid_on_set_protocol(const struct device *dev,
struct hid_device_info *dev_data,
struct usb_setup_packet *setup)
{
#ifdef CONFIG_USB_HID_BOOT_PROTOCOL
uint16_t protocol = setup->wValue;
if (protocol > HID_PROTOCOL_REPORT) {
LOG_ERR("Unsupported protocol: %u", protocol);
return -ENOTSUP;
}
LOG_DBG("Set Protocol: %u", protocol);
if (dev_data->protocol != protocol) {
dev_data->protocol = protocol;
if (dev_data->ops && dev_data->ops->protocol_change) {
dev_data->ops->protocol_change(dev, protocol);
}
}
return 0;
#else
return -ENOTSUP;
#endif
}
static void usb_set_hid_report_size(const struct usb_cfg_data *cfg, uint16_t size)
{
struct usb_if_descriptor *if_desc = (void *)cfg->interface_descriptor;
struct usb_hid_config *desc =
CONTAINER_OF(if_desc, struct usb_hid_config, if0);
LOG_DBG("if_desc %p desc %p size %u", if_desc, desc, size);
sys_put_le16(size,
(uint8_t *)&(desc->if0_hid.subdesc[0].wDescriptorLength));
}
#ifdef CONFIG_USB_DEVICE_SOF
void hid_clear_idle_ctx(struct hid_device_info *dev_data)
{
dev_data->idle_on = false;
for (uint16_t i = 0; i < CONFIG_USB_HID_REPORTS; i++) {
dev_data->sof_cnt[i] = 0U;
dev_data->idle_rate[i] = 0U;
}
}
void hid_sof_handler(struct hid_device_info *dev_data)
{
const struct device *dev = dev_data->common.dev;
bool reported = false;
if (dev_data->ops == NULL || dev_data->ops->on_idle == NULL) {
return;
}
for (uint16_t i = 0; i < CONFIG_USB_HID_REPORTS; i++) {
if (dev_data->idle_rate[i]) {
dev_data->sof_cnt[i]++;
} else {
continue;
}
int32_t diff = abs((int32_t) ((uint32_t) dev_data->idle_rate[i] * 4U -
dev_data->sof_cnt[i]));
if (diff < 2 && reported == false) {
dev_data->sof_cnt[i] = 0U;
/*
* We can submit only one report at a time
* because we have only one endpoint and
* there is no queue for the packets/reports.
*/
reported = true;
dev_data->ops->on_idle(dev, i + 1);
} else if (diff == 0 && reported == true) {
/* Delay it once until it has spread. */
dev_data->sof_cnt[i]--;
}
}
}
#endif
static void hid_do_status_cb(struct hid_device_info *dev_data,
enum usb_dc_status_code status,
const uint8_t *param)
{
switch (status) {
case USB_DC_ERROR:
LOG_INF("Device error");
break;
case USB_DC_RESET: {
LOG_INF("Device reset detected");
dev_data->configured = false;
dev_data->suspended = false;
#ifdef CONFIG_USB_HID_BOOT_PROTOCOL
const struct device *dev = dev_data->common.dev;
uint8_t prev = dev_data->protocol;
dev_data->protocol = HID_PROTOCOL_REPORT;
if (prev != HID_PROTOCOL_REPORT) {
if (dev_data->ops && dev_data->ops->protocol_change) {
dev_data->ops->protocol_change(dev, dev_data->protocol);
}
}
#endif
}
#ifdef CONFIG_USB_DEVICE_SOF
hid_clear_idle_ctx(dev_data);
#endif
break;
case USB_DC_CONNECTED:
LOG_INF("Device connected");
break;
case USB_DC_CONFIGURED:
LOG_INF("Device configured");
dev_data->configured = true;
break;
case USB_DC_DISCONNECTED:
LOG_INF("Device disconnected");
dev_data->configured = false;
dev_data->suspended = false;
break;
case USB_DC_SUSPEND:
LOG_INF("Device suspended");
dev_data->suspended = true;
break;
case USB_DC_RESUME:
LOG_INF("Device resumed");
if (dev_data->suspended) {
LOG_INF("from suspend");
dev_data->suspended = false;
} else {
LOG_DBG("Spurious resume event");
}
break;
case USB_DC_SOF:
#ifdef CONFIG_USB_DEVICE_SOF
if (dev_data->idle_on) {
hid_sof_handler(dev_data);
}
#endif
break;
case USB_DC_UNKNOWN:
default:
LOG_INF("Unknown event");
break;
}
}
static void hid_status_cb(struct usb_cfg_data *cfg,
enum usb_dc_status_code status,
const uint8_t *param)
{
struct hid_device_info *dev_data;
struct usb_dev_data *common;
LOG_DBG("cfg %p status %d", cfg, status);
common = usb_get_dev_data_by_cfg(&usb_hid_devlist, cfg);
if (common == NULL) {
LOG_WRN("Device data not found for cfg %p", cfg);
return;
}
dev_data = CONTAINER_OF(common, struct hid_device_info, common);
hid_do_status_cb(dev_data, status, param);
}
static int hid_class_handle_req(struct usb_setup_packet *setup,
int32_t *len, uint8_t **data)
{
struct hid_device_info *dev_data;
struct usb_dev_data *common;
const struct device *dev;
LOG_DBG("Class request:"
"bRequest 0x%02x, bmRequestType 0x%02x len %d",
setup->bRequest, setup->bmRequestType, *len);
common = usb_get_dev_data_by_iface(&usb_hid_devlist,
(uint8_t)setup->wIndex);
if (common == NULL) {
LOG_WRN("Device data not found for interface %u",
setup->wIndex);
return -ENODEV;
}
dev_data = CONTAINER_OF(common, struct hid_device_info, common);
dev = common->dev;
if (usb_reqtype_is_to_host(setup)) {
switch (setup->bRequest) {
case USB_HID_GET_IDLE:
return hid_on_get_idle(dev_data, setup, len, data);
case USB_HID_GET_REPORT:
if (dev_data->ops && dev_data->ops->get_report) {
return dev_data->ops->get_report(dev, setup,
len, data);
} else {
return hid_on_get_report(dev_data, setup, len,
data);
}
break;
case USB_HID_GET_PROTOCOL:
return hid_on_get_protocol(dev_data, setup, len, data);
default:
LOG_ERR("Unhandled request 0x%02x", setup->bRequest);
break;
}
} else {
switch (setup->bRequest) {
case USB_HID_SET_IDLE:
return hid_on_set_idle(dev_data, setup, len, data);
case USB_HID_SET_REPORT:
if (dev_data->ops && dev_data->ops->set_report) {
return dev_data->ops->set_report(dev, setup,
len, data);
} else {
return hid_on_set_report(dev_data, setup, len,
data);
}
break;
case USB_HID_SET_PROTOCOL:
return hid_on_set_protocol(dev, dev_data, setup);
default:
LOG_ERR("Unhandled request 0x%02x", setup->bRequest);
break;
}
}
return -ENOTSUP;
}
static int hid_custom_handle_req(struct usb_setup_packet *setup,
int32_t *len, uint8_t **data)
{
LOG_DBG("Standard request:"
"bRequest 0x%02x, bmRequestType 0x%02x, len %d",
setup->bRequest, setup->bmRequestType, setup->wLength);
if (usb_reqtype_is_to_host(setup) &&
setup->RequestType.recipient == USB_REQTYPE_RECIPIENT_INTERFACE &&
setup->bRequest == USB_SREQ_GET_DESCRIPTOR) {
uint8_t value = (uint8_t)(setup->wValue >> 8);
uint8_t iface_num = (uint8_t)setup->wIndex;
struct hid_device_info *dev_data;
struct usb_dev_data *common;
const struct usb_cfg_data *cfg;
const struct usb_hid_config *hid_desc;
common = usb_get_dev_data_by_iface(&usb_hid_devlist, iface_num);
if (common == NULL) {
LOG_WRN("Device data not found for interface %u",
iface_num);
return -EINVAL;
}
dev_data = CONTAINER_OF(common, struct hid_device_info, common);
switch (value) {
case USB_DESC_HID:
cfg = common->dev->config;
hid_desc = cfg->interface_descriptor;
LOG_DBG("Return HID Descriptor");
*len = MIN(setup->wLength, hid_desc->if0_hid.bLength);
*data = (uint8_t *)&hid_desc->if0_hid;
break;
case USB_DESC_HID_REPORT:
LOG_DBG("Return Report Descriptor");
*len = MIN(setup->wLength, dev_data->report_size);
*data = (uint8_t *)dev_data->report_desc;
break;
default:
return -ENOTSUP;
}
return 0;
}
return -EINVAL;
}
static void hid_int_in(uint8_t ep, enum usb_dc_ep_cb_status_code ep_status)
{
struct hid_device_info *dev_data;
struct usb_dev_data *common;
common = usb_get_dev_data_by_ep(&usb_hid_devlist, ep);
if (common == NULL) {
LOG_WRN("Device data not found for endpoint %u", ep);
return;
}
dev_data = CONTAINER_OF(common, struct hid_device_info, common);
if (ep_status != USB_DC_EP_DATA_IN || dev_data->ops == NULL ||
dev_data->ops->int_in_ready == NULL) {
return;
}
dev_data->ops->int_in_ready(common->dev);
}
#ifdef CONFIG_ENABLE_HID_INT_OUT_EP
static void hid_int_out(uint8_t ep, enum usb_dc_ep_cb_status_code ep_status)
{
struct hid_device_info *dev_data;
struct usb_dev_data *common;
common = usb_get_dev_data_by_ep(&usb_hid_devlist, ep);
if (common == NULL) {
LOG_WRN("Device data not found for endpoint %u", ep);
return;
}
dev_data = CONTAINER_OF(common, struct hid_device_info, common);
if (ep_status != USB_DC_EP_DATA_OUT || dev_data->ops == NULL ||
dev_data->ops->int_out_ready == NULL) {
return;
}
dev_data->ops->int_out_ready(common->dev);
}
#endif
#define INITIALIZER_EP_DATA(cb, addr) \
{ \
.ep_cb = cb, \
.ep_addr = addr, \
}
/* Describe Endpoints configuration */
#ifdef CONFIG_ENABLE_HID_INT_OUT_EP
#define DEFINE_HID_EP(x, _) \
static struct usb_ep_cfg_data hid_ep_data_##x[] = { \
INITIALIZER_EP_DATA(hid_int_in, AUTO_EP_IN), \
INITIALIZER_EP_DATA(hid_int_out, AUTO_EP_OUT), \
}
#else
#define DEFINE_HID_EP(x, _) \
static struct usb_ep_cfg_data hid_ep_data_##x[] = { \
INITIALIZER_EP_DATA(hid_int_in, AUTO_EP_IN), \
}
#endif
static void hid_interface_config(struct usb_desc_header *head,
uint8_t bInterfaceNumber)
{
struct usb_if_descriptor *if_desc = (struct usb_if_descriptor *)head;
struct usb_hid_config *desc =
CONTAINER_OF(if_desc, struct usb_hid_config, if0);
LOG_DBG("");
desc->if0.bInterfaceNumber = bInterfaceNumber;
#ifdef CONFIG_ENABLE_HID_INT_OUT_EP
desc->if0.bNumEndpoints = 2;
#endif
}
#define DEFINE_HID_CFG_DATA(x, _) \
USBD_DEFINE_CFG_DATA(hid_config_##x) = { \
.usb_device_description = NULL, \
.interface_config = hid_interface_config, \
.interface_descriptor = &hid_cfg_##x.if0, \
.cb_usb_status = hid_status_cb, \
.interface = { \
.class_handler = hid_class_handle_req, \
.custom_handler = hid_custom_handle_req, \
}, \
.num_endpoints = ARRAY_SIZE(hid_ep_data_##x), \
.endpoint = hid_ep_data_##x, \
}
int usb_hid_set_proto_code(const struct device *dev, uint8_t proto_code)
{
const struct usb_cfg_data *cfg = dev->config;
struct usb_if_descriptor *if_desc = cfg->interface_descriptor;
if (IS_ENABLED(CONFIG_USB_HID_BOOT_PROTOCOL)) {
if_desc->bInterfaceProtocol = proto_code;
return 0;
}
return -ENOTSUP;
}
int usb_hid_init(const struct device *dev)
{
struct usb_cfg_data *cfg = (void *)dev->config;
struct hid_device_info *dev_data = dev->data;
LOG_DBG("Initializing HID Device: dev %p", dev);
/*
* Modify Report Descriptor Size
*/
usb_set_hid_report_size(cfg, dev_data->report_size);
return 0;
}
void usb_hid_register_device(const struct device *dev, const uint8_t *desc,
size_t size, const struct hid_ops *ops)
{
struct hid_device_info *dev_data = dev->data;
dev_data->report_desc = desc;
dev_data->report_size = size;
dev_data->ops = ops;
dev_data->common.dev = dev;
sys_slist_append(&usb_hid_devlist, &dev_data->common.node);
LOG_DBG("Added dev_data %p dev %p to devlist %p", dev_data, dev,
&usb_hid_devlist);
}
int hid_int_ep_write(const struct device *dev, const uint8_t *data, uint32_t data_len,
uint32_t *bytes_ret)
{
const struct usb_cfg_data *cfg = dev->config;
struct hid_device_info *hid_dev_data = dev->data;
if (hid_dev_data->configured && !hid_dev_data->suspended) {
return usb_write(cfg->endpoint[HID_INT_IN_EP_IDX].ep_addr, data,
data_len, bytes_ret);
} else {
LOG_WRN("Device is not configured");
return -EAGAIN;
}
}
int hid_int_ep_read(const struct device *dev, uint8_t *data, uint32_t max_data_len,
uint32_t *ret_bytes)
{
#ifdef CONFIG_ENABLE_HID_INT_OUT_EP
const struct usb_cfg_data *cfg = dev->config;
return usb_read(cfg->endpoint[HID_INT_OUT_EP_IDX].ep_addr,
data, max_data_len, ret_bytes);
#else
return -ENOTSUP;
#endif
}
static const struct usb_hid_device_api {
void (*init)(void);
} hid_api;
static int usb_hid_device_init(const struct device *dev)
{
LOG_DBG("Init HID Device: dev %p (%s)", dev, dev->name);
return 0;
}
#define DEFINE_HID_DEV_DATA(x, _) \
struct hid_device_info usb_hid_dev_data_##x
#define DEFINE_HID_DEVICE(x, _) \
DEVICE_DEFINE(usb_hid_device_##x, \
CONFIG_USB_HID_DEVICE_NAME "_" #x, \
&usb_hid_device_init, \
NULL, \
&usb_hid_dev_data_##x, \
&hid_config_##x, POST_KERNEL, \
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&hid_api)
LISTIFY(CONFIG_USB_HID_DEVICE_COUNT, DEFINE_HID_DESCR, (;), _);
LISTIFY(CONFIG_USB_HID_DEVICE_COUNT, DEFINE_HID_EP, (;), _);
LISTIFY(CONFIG_USB_HID_DEVICE_COUNT, DEFINE_HID_CFG_DATA, (;), _);
LISTIFY(CONFIG_USB_HID_DEVICE_COUNT, DEFINE_HID_DEV_DATA, (;), _);
LISTIFY(CONFIG_USB_HID_DEVICE_COUNT, DEFINE_HID_DEVICE, (;), _);