-
Notifications
You must be signed in to change notification settings - Fork 2
/
hid_host.c
1455 lines (1198 loc) · 47.9 KB
/
hid_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
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <Arduino.h>
#include "esp_check.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "usb/usb_host.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <sys/queue.h>
#include "hid_host.h"
// HID spinlock
static portMUX_TYPE hid_lock = portMUX_INITIALIZER_UNLOCKED;
#define HID_ENTER_CRITICAL() portENTER_CRITICAL(&hid_lock)
#define HID_EXIT_CRITICAL() portEXIT_CRITICAL(&hid_lock)
// HID verification macroses
#define HID_GOTO_ON_FALSE_CRITICAL(exp, err) \
do { \
if (!(exp)) { \
HID_EXIT_CRITICAL(); \
ret = err; \
goto fail; \
} \
} while (0)
#define HID_RETURN_ON_FALSE_CRITICAL(exp, err) \
do { \
if (!(exp)) { \
HID_EXIT_CRITICAL(); \
return err; \
} \
} while (0)
#define HID_GOTO_ON_ERROR(exp, msg) ESP_GOTO_ON_ERROR(exp, fail, TAG, msg)
#define HID_GOTO_ON_FALSE(exp, err, msg) \
ESP_GOTO_ON_FALSE((exp), err, fail, TAG, msg)
#define HID_RETURN_ON_ERROR(exp, msg) ESP_RETURN_ON_ERROR((exp), TAG, msg)
#define HID_RETURN_ON_FALSE(exp, err, msg) \
ESP_RETURN_ON_FALSE((exp), (err), TAG, msg)
#define HID_RETURN_ON_INVALID_ARG(exp) \
ESP_RETURN_ON_FALSE((exp) != NULL, ESP_ERR_INVALID_ARG, TAG, "Argument error")
static const char *TAG = "hid-host";
#define DEFAULT_TIMEOUT_MS (5000)
/**
* @brief HID Device structure.
*
*/
typedef struct hid_host_device {
STAILQ_ENTRY(hid_host_device) tailq_entry; /**< HID device queue */
SemaphoreHandle_t device_busy; /**< HID device main mutex */
SemaphoreHandle_t ctrl_xfer_done; /**< Control transfer semaphore */
usb_transfer_t *ctrl_xfer; /**< Pointer to control transfer buffer */
usb_device_handle_t dev_hdl; /**< USB device handle */
uint8_t dev_addr; /**< USB devce address */
} hid_device_t;
/**
* @brief HID Interface state
*/
typedef enum {
HID_INTERFACE_STATE_NOT_INITIALIZED =
0x00, /**< HID Interface not initialized */
HID_INTERFACE_STATE_IDLE, /**< HID Interface has been found in connected USB
device */
HID_INTERFACE_STATE_READY, /**< HID Interface opened and ready to start
transfer */
HID_INTERFACE_STATE_ACTIVE, /**< HID Interface is in use */
HID_INTERFACE_STATE_WAIT_USER_DELETION, /**< HID Interface wait user to be
removed */
HID_INTERFACE_STATE_MAX
} hid_iface_state_t;
/**
* @brief HID Interface structure in device to interact with. After HID device
* opening keeps the interface configuration
*
*/
typedef struct hid_interface {
STAILQ_ENTRY(hid_interface) tailq_entry;
hid_device_t *parent; /**< Parent USB HID device */
hid_host_dev_params_t dev_params; /**< USB device parameters */
uint8_t ep_in; /**< Interrupt IN EP number */
uint16_t ep_in_mps; /**< Interrupt IN max size */
uint8_t country_code; /**< Country code */
uint16_t report_desc_size; /**< Size of Report */
uint8_t *report_desc; /**< Pointer to HID Report */
usb_transfer_t *in_xfer; /**< Pointer to IN transfer buffer */
hid_host_interface_event_cb_t user_cb; /**< Interface application callback */
void *user_cb_arg; /**< Interface application callback arg */
hid_iface_state_t state; /**< Interface state */
} hid_iface_t;
/**
* @brief HID driver default context
*
* This context is created during HID Host install.
*/
typedef struct {
STAILQ_HEAD(devices, hid_host_device)
hid_devices_tailq; /**< STAILQ of HID interfaces */
STAILQ_HEAD(interfaces, hid_interface)
hid_ifaces_tailq; /**< STAILQ of HID interfaces */
usb_host_client_handle_t client_handle; /**< Client task handle */
hid_host_driver_event_cb_t user_cb; /**< User application callback */
void *user_arg; /**< User application callback args */
SemaphoreHandle_t all_events_handled; /**< Events handler semaphore */
volatile bool end_client_event_handling; /**< Client event handling flag */
} hid_driver_t;
static hid_driver_t *s_hid_driver; /**< Internal pointer to HID driver */
// ----------------------- Private Prototypes ----------------------------------
static esp_err_t hid_host_install_device(uint8_t dev_addr,
usb_device_handle_t dev_hdl,
hid_device_t **hid_device);
static esp_err_t hid_host_uninstall_device(hid_device_t *hid_device);
// --------------------------- Internal Logic ----------------------------------
/**
* @brief HID class specific request
*/
typedef struct hid_class_request {
uint8_t bRequest; /**< bRequest */
uint16_t wValue; /**< wValue: Report Type and Report ID */
uint16_t wIndex; /**< wIndex: Interface */
uint16_t wLength; /**< wLength: Report Length */
uint8_t *data; /**< Pointer to data */
} hid_class_request_t;
/**
* @brief USB Event handler
*
* Handle all USB related events such as USB host (usbh) events or hub events
* from USB hardware
*
* @param[in] arg Argument, does not used
*/
static void event_handler_task(void *arg) {
while (1) {
/* Here wee need a timeout 50 ms to handle end_client_event_handling flag
* during situation when all devices were removed and it is time to remove
* and destroy everything.
*/
usb_host_client_handle_events(s_hid_driver->client_handle, portMAX_DELAY);
if (s_hid_driver->end_client_event_handling) {
break;
}
}
ESP_ERROR_CHECK(usb_host_client_deregister(s_hid_driver->client_handle));
xSemaphoreGive(s_hid_driver->all_events_handled);
vTaskDelete(NULL);
}
/**
* @brief Return HID device in devices list by USB device handle
*
* @param[in] usb_device_handle_t USB device handle
* @return hid_device_t Pointer to device, NULL if device not present
*/
static hid_device_t *get_hid_device_by_handle(usb_device_handle_t usb_handle) {
hid_device_t *device = NULL;
HID_ENTER_CRITICAL();
STAILQ_FOREACH(device, &s_hid_driver->hid_devices_tailq, tailq_entry) {
if (usb_handle == device->dev_hdl) {
HID_EXIT_CRITICAL();
return device;
}
}
HID_EXIT_CRITICAL();
return NULL;
}
/**
* @brief Return HID Device fron the transfer context
*
* @param[in] xfer USB transfer struct
* @return hid_device_t Pointer to HID Device
*/
static inline hid_device_t *get_hid_device_from_context(usb_transfer_t *xfer) {
return (hid_device_t *)xfer->context;
}
/**
* @brief Get HID Interface pointer by Endpoint address
*
* @param[in] ep_addr Endpoint address
* @return hid_iface_t Pointer to HID Interface configuration structure
*/
static hid_iface_t *get_interface_by_ep(uint8_t ep_addr) {
hid_iface_t *interface = NULL;
HID_ENTER_CRITICAL();
STAILQ_FOREACH(interface, &s_hid_driver->hid_ifaces_tailq, tailq_entry) {
if (ep_addr == interface->ep_in) {
HID_EXIT_CRITICAL();
return interface;
}
}
HID_EXIT_CRITICAL();
return NULL;
}
/**
* @brief Verify presence of Interface in the RAM list
*
* @param[in] iface Pointer to an Interface structure
* @return true Interface is in the list
* @return false Interface is not in the list
*/
static inline bool is_interface_in_list(hid_iface_t *iface) {
hid_iface_t *interface = NULL;
HID_ENTER_CRITICAL();
STAILQ_FOREACH(interface, &s_hid_driver->hid_ifaces_tailq, tailq_entry) {
if (iface == interface) {
HID_EXIT_CRITICAL();
return true;
}
}
HID_EXIT_CRITICAL();
return false;
}
/**
* @brief Get HID Interface pointer by external HID Device handle with
* verification in RAM list
*
* @param[in] hid_dev_handle HID Device handle
* @return hid_iface_t Pointer to an Interface structure
*/
static hid_iface_t *
get_iface_by_handle(hid_host_device_handle_t hid_dev_handle) {
hid_iface_t *hid_iface = (hid_iface_t *)hid_dev_handle;
if (!is_interface_in_list(hid_iface)) {
ESP_LOGE(TAG, "HID device handle not found");
return NULL;
}
return hid_iface;
}
/**
* @brief Check HID interface descriptor present
*
* @param[in] config_desc Pointer to Configuration Descriptor
* @return esp_err_t
*/
static bool hid_interface_present(const usb_config_desc_t *config_desc) {
const usb_intf_desc_t *iface_desc = NULL;
int offset = 0;
for (int num = 0; num < config_desc->bNumInterfaces; num++) {
iface_desc = usb_parse_interface_descriptor(config_desc, num, 0, &offset);
if (USB_CLASS_HID == iface_desc->bInterfaceClass) {
return true;
}
}
return false;
}
/**
* @brief HID Interface user callback function.
*
* @param[in] hid_iface Pointer to an Interface structure
* @param[in] event_id HID Interface event
*/
static inline void
hid_host_user_interface_callback(hid_iface_t *hid_iface,
const hid_host_interface_event_t event) {
assert(hid_iface);
hid_host_dev_params_t *dev_params = &hid_iface->dev_params;
assert(dev_params);
if (hid_iface->user_cb) {
hid_iface->user_cb(hid_iface, event, hid_iface->user_cb_arg);
}
}
/**
* @brief HID Device user callback function.
*
* @param[in] event_id HID Device event
* @param[in] dev_params HID Device parameters
*/
static inline void
hid_host_user_device_callback(hid_iface_t *hid_iface,
const hid_host_driver_event_t event) {
assert(hid_iface);
hid_host_dev_params_t *dev_params = &hid_iface->dev_params;
assert(dev_params);
if (s_hid_driver && s_hid_driver->user_cb) {
s_hid_driver->user_cb(hid_iface, event, s_hid_driver->user_arg);
}
}
/**
* @brief Add interface in a list
*
* @param[in] hid_device HID device handle
* @param[in] iface_desc Pointer to an Interface descriptor
* @param[in] hid_desc Pointer to an HID device descriptor
* @param[in] ep_desc Pointer to an EP descriptor
* @return esp_err_t
*/
static esp_err_t hid_host_add_interface(hid_device_t *hid_device,
const usb_intf_desc_t *iface_desc,
const hid_descriptor_t *hid_desc,
const usb_ep_desc_t *ep_in_desc) {
hid_iface_t *hid_iface = calloc(1, sizeof(hid_iface_t));
HID_RETURN_ON_FALSE(hid_iface, ESP_ERR_NO_MEM, "Unable to allocate memory");
HID_ENTER_CRITICAL();
hid_iface->parent = hid_device;
hid_iface->state = HID_INTERFACE_STATE_NOT_INITIALIZED;
hid_iface->dev_params.addr = hid_device->dev_addr;
if (iface_desc) {
hid_iface->dev_params.iface_num = iface_desc->bInterfaceNumber;
hid_iface->dev_params.sub_class = iface_desc->bInterfaceSubClass;
hid_iface->dev_params.proto = iface_desc->bInterfaceProtocol;
}
if (hid_desc) {
hid_iface->country_code = hid_desc->bCountryCode;
hid_iface->report_desc_size = hid_desc->wReportDescriptorLength;
}
// EP IN && INT Type
if (ep_in_desc) {
if ((ep_in_desc->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK) &&
(ep_in_desc->bmAttributes & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK)) {
hid_iface->ep_in = ep_in_desc->bEndpointAddress;
hid_iface->ep_in_mps = USB_EP_DESC_GET_MPS(ep_in_desc);
} else {
ESP_EARLY_LOGE(TAG, "HID device EP IN %#X configuration error",
ep_in_desc->bEndpointAddress);
}
}
if (iface_desc && hid_desc && ep_in_desc) {
hid_iface->state = HID_INTERFACE_STATE_IDLE;
}
STAILQ_INSERT_TAIL(&s_hid_driver->hid_ifaces_tailq, hid_iface, tailq_entry);
HID_EXIT_CRITICAL();
return ESP_OK;
}
/**
* @brief Notify user about the connected Interfaces
*
* @param[in] hid_device Pointer to HID device structure
*/
static void hid_host_notify_interface_connected(hid_device_t *hid_device) {
HID_ENTER_CRITICAL();
hid_iface_t *iface = STAILQ_FIRST(&s_hid_driver->hid_ifaces_tailq);
hid_iface_t *tmp = NULL;
while (iface != NULL) {
tmp = STAILQ_NEXT(iface, tailq_entry);
HID_EXIT_CRITICAL();
if (iface->parent && (iface->parent->dev_addr == hid_device->dev_addr)) {
hid_host_user_device_callback(iface, HID_HOST_DRIVER_EVENT_CONNECTED);
}
iface = tmp;
HID_ENTER_CRITICAL();
}
HID_EXIT_CRITICAL();
}
/**
* @brief Create a list of available interfaces in RAM
*
* @param[in] hid_device Pointer to HID device structure
* @param[in] dev_addr USB device physical address
* @param[in] sub_class USB HID SubClass value
* @return esp_err_t
*/
static esp_err_t
hid_host_interface_list_create(hid_device_t *hid_device,
const usb_config_desc_t *config_desc) {
size_t total_length = config_desc->wTotalLength;
const usb_intf_desc_t *iface_desc = NULL;
const hid_descriptor_t *hid_desc = NULL;
const usb_ep_desc_t *ep_desc = NULL;
const usb_ep_desc_t *ep_in_desc = NULL;
int offset = 0;
// For every Interface
for (int i = 0; i < config_desc->bNumInterfaces; i++) {
iface_desc = usb_parse_interface_descriptor(config_desc, i, 0, &offset);
hid_desc = NULL;
ep_in_desc = NULL;
if (USB_CLASS_HID == iface_desc->bInterfaceClass) {
// HID descriptor
hid_desc = (const hid_descriptor_t *)usb_parse_next_descriptor_of_type(
(const usb_standard_desc_t *)iface_desc, total_length,
HID_CLASS_DESCRIPTOR_TYPE_HID, &offset);
if (!hid_desc) {
return ESP_ERR_NOT_FOUND;
}
// EP descriptors for Interface
for (int i = 0; i < iface_desc->bNumEndpoints; i++) {
int ep_offset = 0;
ep_desc = usb_parse_endpoint_descriptor_by_index(
iface_desc, i, total_length, &ep_offset);
if (ep_desc) {
if (USB_EP_DESC_GET_EP_DIR(ep_desc)) {
ep_in_desc = ep_desc;
}
} else {
return ESP_ERR_NOT_FOUND;
}
} // for every EP within Interface
// Add Interface to the list
HID_RETURN_ON_ERROR(
hid_host_add_interface(hid_device, iface_desc, hid_desc, ep_in_desc),
"Unable to add HID Interface to the RAM list");
} // USB_CLASS_HID
}
hid_host_notify_interface_connected(hid_device);
return ESP_OK;
}
/**
* @brief HID Host initialize device attempt
*
* @param[in] dev_addr USB device physical address
* @return true USB device contain HID Interface and device was initialized
* @return false USB does not contain HID Interface
*/
static bool hid_host_device_init_attempt(uint8_t dev_addr) {
bool is_hid_device = false;
usb_device_handle_t dev_hdl;
const usb_config_desc_t *config_desc = NULL;
hid_device_t *hid_device = NULL;
if (usb_host_device_open(s_hid_driver->client_handle, dev_addr, &dev_hdl) ==
ESP_OK) {
if (usb_host_get_active_config_descriptor(dev_hdl, &config_desc) ==
ESP_OK) {
is_hid_device = hid_interface_present(config_desc);
}
}
// Create HID interfaces list in RAM, connected to the particular USB dev
if (is_hid_device) {
// Proceed, add HID device to the list, get handle if necessary
ESP_ERROR_CHECK(hid_host_install_device(dev_addr, dev_hdl, &hid_device));
// Create Interfaces list for a possibility to claim Interface
ESP_ERROR_CHECK(hid_host_interface_list_create(hid_device, config_desc));
} else {
usb_host_device_close(s_hid_driver->client_handle, dev_hdl);
ESP_LOGW(TAG, "No HID device at USB port %d", dev_addr);
}
return is_hid_device;
}
/**
* @brief USB device was removed we need to shutdown HID Interface
*
* @param[in] hid_dev_handle Handle of the HID devive to close
* @param[in] force To shutdown HID interface immediately
* @return esp_err_t
*/
static esp_err_t
hid_host_interface_shutdown(hid_host_device_handle_t hid_dev_handle,
bool force) {
hid_iface_t *iface = get_iface_by_handle(hid_dev_handle);
HID_RETURN_ON_INVALID_ARG(iface);
iface->state = HID_INTERFACE_STATE_WAIT_USER_DELETION;
// Not force shutdown and Interface was started by user
if (!force && iface->user_cb) {
hid_host_user_interface_callback(iface,
HID_HOST_INTERFACE_EVENT_DISCONNECTED);
return ESP_OK;
}
// Interface has not been opened, delete it right away
return hid_host_device_close(iface);
}
/**
* @brief Destroy a list of available interfaces in RAM
*
* @return esp_err_t
*/
static esp_err_t hid_host_interface_list_destroy(void) {
bool active_shutdown = false;
hid_iface_t *iface = STAILQ_FIRST(&s_hid_driver->hid_ifaces_tailq);
hid_iface_t *tmp = NULL;
while (iface != NULL) {
tmp = STAILQ_NEXT(iface, tailq_entry);
// free interface
if (HID_INTERFACE_STATE_NOT_INITIALIZED != iface->state) {
active_shutdown = true;
ESP_ERROR_CHECK(hid_host_device_close(iface));
ESP_ERROR_CHECK(hid_host_interface_shutdown(iface, true));
}
HID_ENTER_CRITICAL();
iface->state = HID_INTERFACE_STATE_NOT_INITIALIZED;
STAILQ_REMOVE(&s_hid_driver->hid_ifaces_tailq, iface, hid_interface,
tailq_entry);
free(iface);
HID_EXIT_CRITICAL();
iface = tmp;
}
if (active_shutdown) {
ESP_LOGE(TAG, "Shutdown active HID device.");
}
return ESP_OK;
}
/**
* @brief Deinit USB device by handle
*
* @param[in] dev_hdl USB device handle
* @return esp_err_t
*/
static esp_err_t hid_host_device_disconnected(usb_device_handle_t dev_hdl) {
hid_device_t *hid_device = get_hid_device_by_handle(dev_hdl);
// Device should be in the list
assert(hid_device);
HID_ENTER_CRITICAL();
hid_iface_t *iface = STAILQ_FIRST(&s_hid_driver->hid_ifaces_tailq);
hid_iface_t *tmp = NULL;
while (iface != NULL) {
tmp = STAILQ_NEXT(iface, tailq_entry);
HID_EXIT_CRITICAL();
if (iface->parent && (iface->parent->dev_addr == hid_device->dev_addr)) {
HID_RETURN_ON_ERROR(hid_host_device_close(iface),
"Unable to close device");
HID_RETURN_ON_ERROR(hid_host_interface_shutdown(iface, false),
"Unable to shutdown interface");
}
iface = tmp;
HID_ENTER_CRITICAL();
}
HID_EXIT_CRITICAL();
// Delete HID compliant device
HID_RETURN_ON_ERROR(hid_host_uninstall_device(hid_device),
"Unable to uninstall device");
return ESP_OK;
}
/**
* @brief USB Host Client's event callback
*
* @param[in] event Client event message
* @param[in] arg Argument, does not used
*/
static void client_event_cb(const usb_host_client_event_msg_t *event,
void *arg) {
if (event->event == USB_HOST_CLIENT_EVENT_NEW_DEV) {
hid_host_device_init_attempt(event->new_dev.address);
} else if (event->event == USB_HOST_CLIENT_EVENT_DEV_GONE) {
hid_host_device_disconnected(event->dev_gone.dev_hdl);
}
}
/**
* @brief HID Host claim Interface and prepare transfer, change state to READY
*
* @param[in] iface Pointer to Interface structure,
* @return esp_err_t
*/
static esp_err_t
hid_host_interface_claim_and_prepare_transfer(hid_iface_t *iface) {
HID_RETURN_ON_ERROR(usb_host_interface_claim(s_hid_driver->client_handle,
iface->parent->dev_hdl,
iface->dev_params.iface_num, 0),
"Unable to claim Interface");
HID_RETURN_ON_ERROR(
usb_host_transfer_alloc(iface->ep_in_mps, 0, &iface->in_xfer),
"Unable to allocate transfer buffer for EP IN");
// Change state
iface->state = HID_INTERFACE_STATE_READY;
return ESP_OK;
}
/**
* @brief HID Host release Interface and free transfer, change state to IDLE
*
* @param[in] iface Pointer to Interface structure,
* @return esp_err_t
*/
static esp_err_t
hid_host_interface_release_and_free_transfer(hid_iface_t *iface) {
HID_RETURN_ON_INVALID_ARG(iface);
HID_RETURN_ON_INVALID_ARG(iface->parent);
HID_RETURN_ON_FALSE(is_interface_in_list(iface), ESP_ERR_NOT_FOUND,
"Interface handle not found");
HID_RETURN_ON_ERROR(usb_host_interface_release(s_hid_driver->client_handle,
iface->parent->dev_hdl,
iface->dev_params.iface_num),
"Unable to release HID Interface");
ESP_ERROR_CHECK(usb_host_transfer_free(iface->in_xfer));
// Change state
iface->state = HID_INTERFACE_STATE_IDLE;
return ESP_OK;
}
/**
* @brief Disable active interface
*
* @param[in] iface Pointer to Interface structure
* @return esp_err_t
*/
static esp_err_t hid_host_disable_interface(hid_iface_t *iface) {
HID_RETURN_ON_INVALID_ARG(iface);
HID_RETURN_ON_INVALID_ARG(iface->parent);
HID_RETURN_ON_FALSE(is_interface_in_list(iface), ESP_ERR_NOT_FOUND,
"Interface handle not found");
HID_RETURN_ON_FALSE((HID_INTERFACE_STATE_ACTIVE == iface->state),
ESP_ERR_INVALID_STATE, "Interface wrong state");
HID_RETURN_ON_ERROR(
usb_host_endpoint_halt(iface->parent->dev_hdl, iface->ep_in),
"Unable to HALT EP");
HID_RETURN_ON_ERROR(
usb_host_endpoint_flush(iface->parent->dev_hdl, iface->ep_in),
"Unable to FLUSH EP");
usb_host_endpoint_clear(iface->parent->dev_hdl, iface->ep_in);
iface->state = HID_INTERFACE_STATE_READY;
return ESP_OK;
}
/**
* @brief HID IN Transfer complete callback
*
* @param[in] transfer Pointer to transfer data structure
*/
static void in_xfer_done(usb_transfer_t *in_xfer) {
assert(in_xfer);
hid_iface_t *iface = get_interface_by_ep(in_xfer->bEndpointAddress);
assert(iface);
// Interfaces' parent device should be the same as the hid_device in context
assert(get_hid_device_from_context(in_xfer) == iface->parent);
switch (in_xfer->status) {
case USB_TRANSFER_STATUS_COMPLETED:
// Notify user
hid_host_user_interface_callback(iface,
HID_HOST_INTERFACE_EVENT_INPUT_REPORT);
// Relaunch transfer
usb_host_transfer_submit(in_xfer);
return;
case USB_TRANSFER_STATUS_NO_DEVICE:
case USB_TRANSFER_STATUS_CANCELED:
// User is notified about device disconnection from usb_event_cb
// No need to do anything
return;
default:
// Any other error
break;
}
ESP_LOGE(TAG, "Transfer failed, status %d", in_xfer->status);
// Notify user about transfer or any other error
hid_host_user_interface_callback(iface,
HID_HOST_INTERFACE_EVENT_TRANSFER_ERROR);
}
/** Lock HID device from other task
*
* @param[in] hid_device Pointer to HID device structure
* @param[in] timeout_ms Timeout of trying to take the mutex
* @return esp_err_t
*/
static inline esp_err_t hid_device_try_lock(hid_device_t *hid_device,
uint32_t timeout_ms) {
return (xSemaphoreTake(hid_device->device_busy, pdMS_TO_TICKS(timeout_ms))
? ESP_OK
: ESP_ERR_TIMEOUT);
}
/** Unlock HID device from other task
*
* @param[in] hid_device Pointer to HID device structure
* @param[in] timeout_ms Timeout of trying to take the mutex
* @return esp_err_t
*/
static inline void hid_device_unlock(hid_device_t *hid_device) {
xSemaphoreGive(hid_device->device_busy);
}
/**
* @brief HID Control transfer complete callback
*
* @param[in] ctrl_xfer Pointer to transfer data structure
*/
static void ctrl_xfer_done(usb_transfer_t *ctrl_xfer) {
assert(ctrl_xfer);
hid_device_t *hid_device = (hid_device_t *)ctrl_xfer->context;
xSemaphoreGive(hid_device->ctrl_xfer_done);
}
/**
* @brief HID control transfer synchronous.
*
* @note Passes interface and endpoint descriptors to obtain:
* - interface number, IN endpoint, OUT endpoint, max. packet size
*
* @param[in] hid_device Pointer to HID device structure
* @param[in] ctrl_xfer Pointer to the Transfer structure
* @param[in] len Number of bytes to transfer
* @param[in] timeout_ms Timeout in ms
* @return esp_err_t
*/
static esp_err_t hid_control_transfer(hid_device_t *hid_device, size_t len,
uint32_t timeout_ms) {
usb_transfer_t *ctrl_xfer = hid_device->ctrl_xfer;
ctrl_xfer->device_handle = hid_device->dev_hdl;
ctrl_xfer->callback = ctrl_xfer_done;
ctrl_xfer->context = hid_device;
ctrl_xfer->bEndpointAddress = 0;
ctrl_xfer->timeout_ms = timeout_ms;
ctrl_xfer->num_bytes = len;
HID_RETURN_ON_ERROR(
usb_host_transfer_submit_control(s_hid_driver->client_handle, ctrl_xfer),
"Unable to submit control transfer");
BaseType_t received = xSemaphoreTake(hid_device->ctrl_xfer_done,
pdMS_TO_TICKS(ctrl_xfer->timeout_ms));
if (received != pdTRUE) {
// Transfer was not finished, error in USB LIB. Reset the endpoint
ESP_LOGE(TAG, "Control Transfer Timeout");
HID_RETURN_ON_ERROR(usb_host_endpoint_halt(hid_device->dev_hdl,
ctrl_xfer->bEndpointAddress),
"Unable to HALT EP");
HID_RETURN_ON_ERROR(usb_host_endpoint_flush(hid_device->dev_hdl,
ctrl_xfer->bEndpointAddress),
"Unable to FLUSH EP");
usb_host_endpoint_clear(hid_device->dev_hdl, ctrl_xfer->bEndpointAddress);
return ESP_ERR_TIMEOUT;
}
ESP_LOG_BUFFER_HEXDUMP(TAG, ctrl_xfer->data_buffer,
ctrl_xfer->actual_num_bytes, ESP_LOG_DEBUG);
return ESP_OK;
}
/**
* @brief USB class standard request get descriptor
*
* @param[in] hidh_device Pointer to HID device structure
* @param[in] req Pointer to a class specific request structure
* @return esp_err_t
*/
static esp_err_t
usb_class_request_get_descriptor(hid_device_t *hid_device,
const hid_class_request_t *req) {
esp_err_t ret;
usb_transfer_t *ctrl_xfer = hid_device->ctrl_xfer;
const size_t ctrl_size = hid_device->ctrl_xfer->data_buffer_size;
HID_RETURN_ON_INVALID_ARG(hid_device);
HID_RETURN_ON_INVALID_ARG(hid_device->ctrl_xfer);
HID_RETURN_ON_INVALID_ARG(req);
HID_RETURN_ON_INVALID_ARG(req->data);
HID_RETURN_ON_ERROR(hid_device_try_lock(hid_device, DEFAULT_TIMEOUT_MS),
"HID Device is busy by other task");
if (ctrl_size < (USB_SETUP_PACKET_SIZE + req->wLength)) {
usb_device_info_t dev_info;
ESP_ERROR_CHECK(usb_host_device_info(hid_device->dev_hdl, &dev_info));
// reallocate the ctrl xfer buffer for new length
ESP_LOGI(TAG, "Change HID ctrl xfer size from %d to %d", ctrl_size,
(int)(USB_SETUP_PACKET_SIZE + req->wLength));
usb_host_transfer_free(hid_device->ctrl_xfer);
HID_RETURN_ON_ERROR(
usb_host_transfer_alloc(USB_SETUP_PACKET_SIZE + req->wLength, 0,
&hid_device->ctrl_xfer),
"Unable to allocate transfer buffer for EP0");
}
usb_setup_packet_t *setup = (usb_setup_packet_t *)ctrl_xfer->data_buffer;
setup->bmRequestType = USB_BM_REQUEST_TYPE_DIR_IN |
USB_BM_REQUEST_TYPE_TYPE_STANDARD |
USB_BM_REQUEST_TYPE_RECIP_INTERFACE;
setup->bRequest = req->bRequest;
setup->wValue = req->wValue;
setup->wIndex = req->wIndex;
setup->wLength = req->wLength;
ret = hid_control_transfer(hid_device, USB_SETUP_PACKET_SIZE + req->wLength,
DEFAULT_TIMEOUT_MS);
if (ESP_OK == ret) {
ctrl_xfer->actual_num_bytes -= USB_SETUP_PACKET_SIZE;
if (ctrl_xfer->actual_num_bytes <= req->wLength) {
memcpy(req->data, ctrl_xfer->data_buffer + USB_SETUP_PACKET_SIZE,
ctrl_xfer->actual_num_bytes);
} else {
ret = ESP_ERR_INVALID_SIZE;
}
}
hid_device_unlock(hid_device);
return ret;
}
/**
* @brief HID Host Request Report Descriptor
*
* @param[in] hidh_iface Pointer to HID Interface configuration structure
* @return esp_err_t
*/
static esp_err_t hid_class_request_report_descriptor(hid_iface_t *iface) {
HID_RETURN_ON_INVALID_ARG(iface);
// Get Report Descritpor is possible only in Ready or Active state
HID_RETURN_ON_FALSE(
(HID_INTERFACE_STATE_READY == iface->state) ||
(HID_INTERFACE_STATE_ACTIVE == iface->state),
ESP_ERR_INVALID_STATE,
"Unable to request report decriptor. Interface is not ready");
iface->report_desc = malloc(iface->report_desc_size);
HID_RETURN_ON_FALSE(iface->report_desc, ESP_ERR_NO_MEM,
"Unable to allocate memory");
const hid_class_request_t get_desc = {
.bRequest = USB_B_REQUEST_GET_DESCRIPTOR,
.wValue = (HID_CLASS_DESCRIPTOR_TYPE_REPORT << 8),
.wIndex = iface->dev_params.iface_num,
.wLength = iface->report_desc_size,
.data = iface->report_desc};
return usb_class_request_get_descriptor(iface->parent, &get_desc);
}
/**
* @brief HID class specific request Set
*
* @param[in] hid_device Pointer to HID device structure
* @param[in] req Pointer to a class specific request structure
* @return esp_err_t
*/
static esp_err_t hid_class_request_set(hid_device_t *hid_device,
const hid_class_request_t *req) {
esp_err_t ret;
usb_transfer_t *ctrl_xfer = hid_device->ctrl_xfer;
HID_RETURN_ON_INVALID_ARG(hid_device);
HID_RETURN_ON_INVALID_ARG(hid_device->ctrl_xfer);
HID_RETURN_ON_ERROR(hid_device_try_lock(hid_device, DEFAULT_TIMEOUT_MS),
"HID Device is busy by other task");
usb_setup_packet_t *setup = (usb_setup_packet_t *)ctrl_xfer->data_buffer;
setup->bmRequestType = USB_BM_REQUEST_TYPE_DIR_OUT |
USB_BM_REQUEST_TYPE_TYPE_CLASS |
USB_BM_REQUEST_TYPE_RECIP_INTERFACE;
setup->bRequest = req->bRequest;
setup->wValue = req->wValue;
setup->wIndex = req->wIndex;
setup->wLength = req->wLength;
if (req->wLength && req->data) {
memcpy(ctrl_xfer->data_buffer + USB_SETUP_PACKET_SIZE, req->data,
req->wLength);
}
ret = hid_control_transfer(hid_device, USB_SETUP_PACKET_SIZE + setup->wLength,
DEFAULT_TIMEOUT_MS);
hid_device_unlock(hid_device);
return ret;
}
/**
* @brief HID class specific request Get
*
* @param[in] hid_device Pointer to HID device structure
* @param[in] req Pointer to a class specific request structure
* @param[out] out_length Length of the response in data buffer of req struct
* @return esp_err_t
*/
static esp_err_t hid_class_request_get(hid_device_t *hid_device,
const hid_class_request_t *req,
size_t *out_length) {
esp_err_t ret;
HID_RETURN_ON_INVALID_ARG(hid_device);
HID_RETURN_ON_INVALID_ARG(hid_device->ctrl_xfer);
usb_transfer_t *ctrl_xfer = hid_device->ctrl_xfer;
HID_RETURN_ON_ERROR(hid_device_try_lock(hid_device, DEFAULT_TIMEOUT_MS),
"HID Device is busy by other task");
usb_setup_packet_t *setup = (usb_setup_packet_t *)ctrl_xfer->data_buffer;
setup->bmRequestType = USB_BM_REQUEST_TYPE_DIR_IN |
USB_BM_REQUEST_TYPE_TYPE_CLASS |
USB_BM_REQUEST_TYPE_RECIP_INTERFACE;
setup->bRequest = req->bRequest;
setup->wValue = req->wValue;
setup->wIndex = req->wIndex;
setup->wLength = req->wLength;
ret = hid_control_transfer(hid_device, USB_SETUP_PACKET_SIZE + setup->wLength,
DEFAULT_TIMEOUT_MS);
if (ESP_OK == ret) {
// We do not need the setup data, which is still in the transfer data buffer
ctrl_xfer->actual_num_bytes -= USB_SETUP_PACKET_SIZE;
// Copy data if the size is ok
if (ctrl_xfer->actual_num_bytes <= req->wLength) {
memcpy(req->data, ctrl_xfer->data_buffer + USB_SETUP_PACKET_SIZE,
ctrl_xfer->actual_num_bytes);
// return actual num bytes of response
if (out_length) {
*out_length = ctrl_xfer->actual_num_bytes;
}
} else {
ret = ESP_ERR_INVALID_SIZE;
}
}