-
Notifications
You must be signed in to change notification settings - Fork 17
/
libhoth_usb_device.h
105 lines (90 loc) · 3.5 KB
/
libhoth_usb_device.h
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
// 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.
#ifndef _LIBHOTH_LIBHOTH_USB_DEVICE_H_
#define _LIBHOTH_LIBHOTH_USB_DEVICE_H_
#include <libusb.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LIBHOTH_USB_VENDOR_ID 0x18d1
#define LIBHOTH_USB_INTERFACE_CLASS LIBUSB_CLASS_VENDOR_SPEC
#define LIBHOTH_USB_MAILBOX_INTERFACE_SUBCLASS 0x71
#define LIBHOTH_USB_MAILBOX_INTERFACE_PROTOCOL 0x01
#define LIBHOTH_USB_FIFO_INTERFACE_SUBCLASS 0x58
#define LIBHOTH_USB_FIFO_INTERFACE_PROTOCOL 0x01
// Note on multi-threading: This library is not generally thread-safe. Some
// drivers may use the asynchronous libusb transfer functions and call
// libusb_handle_events_completed().
enum libhoth_usb_interface_type {
LIBHOTH_USB_INTERFACE_TYPE_UNKNOWN = 0,
LIBHOTH_USB_INTERFACE_TYPE_MAILBOX,
LIBHOTH_USB_INTERFACE_TYPE_FIFO,
};
struct libhoth_usb_mailbox {
uint16_t max_packet_size_in;
uint16_t max_packet_size_out;
uint8_t ep_in;
uint8_t ep_out;
};
struct libhoth_usb_fifo {
struct libusb_transfer *in_transfer;
struct libusb_transfer *out_transfer;
uint8_t *in_buffer;
uint8_t *out_buffer;
uint16_t max_packet_size_in;
uint16_t max_packet_size_out;
uint8_t ep_in;
uint8_t ep_out;
int all_transfers_completed;
bool in_transfer_completed;
bool out_transfer_completed;
};
struct libhoth_usb_interface_info {
enum libhoth_usb_interface_type type;
uint8_t interface_id;
uint8_t interface_altsetting;
};
struct libhoth_usb_device {
libusb_context *ctx;
libusb_device_handle *handle;
struct libhoth_usb_interface_info info;
union driver_data {
struct libhoth_usb_mailbox mailbox;
struct libhoth_usb_fifo fifo;
} driver_data;
};
int libhoth_usb_fifo_open(struct libhoth_usb_device *dev,
const struct libusb_config_descriptor *descriptor);
int libhoth_usb_fifo_send_request(struct libhoth_usb_device *dev,
const void *request, size_t request_size);
int libhoth_usb_fifo_receive_response(struct libhoth_usb_device *dev,
void *response, size_t response_size,
size_t *actual_size, int timeout_ms);
int libhoth_usb_fifo_close(struct libhoth_usb_device *dev);
int libhoth_usb_mailbox_open(struct libhoth_usb_device *dev,
const struct libusb_config_descriptor *descriptor);
int libhoth_usb_mailbox_send_request(struct libhoth_usb_device *dev,
const void *request, size_t request_size);
int libhoth_usb_mailbox_receive_response(struct libhoth_usb_device *dev,
void *response, size_t response_size,
size_t *actual_size, int timeout_ms);
int libhoth_usb_mailbox_close(struct libhoth_usb_device *dev);
enum libusb_error transfer_status_to_error(
enum libusb_transfer_status transfer_status);
#ifdef __cplusplus
}
#endif
#endif // _LIBHOTH_USB_LIBHOTH_USB_DEVICE_H_