usb.h (4153B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2020 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifndef DC_USB_H 23 #define DC_USB_H 24 25 #include "common.h" 26 #include "context.h" 27 #include "iostream.h" 28 #include "iterator.h" 29 #include "descriptor.h" 30 #include "ioctl.h" 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif /* __cplusplus */ 35 36 /** 37 * Perform a USB control transfer. 38 * 39 * The parameters for the control transfer are specified in the 40 * #dc_usb_control_t data structure. If the control transfer requires 41 * additional data as in- or output, the buffer must be located 42 * immediately after the #dc_usb_control_t data structure, and the 43 * length of the buffer must be indicated in the #wLength field. The 44 * size of the ioctl request is the total size, including the size of 45 * the #dc_usb_control_t structure. 46 */ 47 #define DC_IOCTL_USB_CONTROL_READ DC_IOCTL_IOR('u', 0, DC_IOCTL_SIZE_VARIABLE) 48 #define DC_IOCTL_USB_CONTROL_WRITE DC_IOCTL_IOW('u', 0, DC_IOCTL_SIZE_VARIABLE) 49 50 /** 51 * USB control transfer. 52 */ 53 typedef struct dc_usb_control_t { 54 unsigned char bmRequestType; 55 unsigned char bRequest; 56 unsigned short wValue; 57 unsigned short wIndex; 58 unsigned short wLength; 59 } dc_usb_control_t; 60 61 /** 62 * Endpoint direction bits of the USB control transfer. 63 */ 64 typedef enum dc_usb_endpoint_t { 65 DC_USB_ENDPOINT_OUT = 0x00, 66 DC_USB_ENDPOINT_IN = 0x80 67 } dc_usb_endpoint_t; 68 69 /** 70 * Request type bits of the USB control transfer. 71 */ 72 typedef enum dc_usb_request_t { 73 DC_USB_REQUEST_STANDARD = 0x00, 74 DC_USB_REQUEST_CLASS = 0x20, 75 DC_USB_REQUEST_VENDOR = 0x40, 76 DC_USB_REQUEST_RESERVED = 0x60 77 } dc_usb_request_t; 78 79 /** 80 * Recipient bits of the USB control transfer. 81 */ 82 typedef enum dc_usb_recipient_t { 83 DC_USB_RECIPIENT_DEVICE = 0x00, 84 DC_USB_RECIPIENT_INTERFACE = 0x01, 85 DC_USB_RECIPIENT_ENDPOINT = 0x02, 86 DC_USB_RECIPIENT_OTHER = 0x03, 87 } dc_usb_recipient_t; 88 89 /** 90 * USB device descriptor. 91 */ 92 typedef struct dc_usb_desc_t { 93 unsigned short vid; 94 unsigned short pid; 95 } dc_usb_desc_t; 96 97 /** 98 * Opaque object representing a USB device. 99 */ 100 typedef struct dc_usb_device_t dc_usb_device_t; 101 102 /** 103 * Get the vendor id (VID) of the USB device. 104 * 105 * @param[in] device A valid USB device. 106 */ 107 unsigned int 108 dc_usb_device_get_vid (dc_usb_device_t *device); 109 110 /** 111 * Get the product id (PID) of the USB device. 112 * 113 * @param[in] device A valid USB device. 114 */ 115 unsigned int 116 dc_usb_device_get_pid (dc_usb_device_t *device); 117 118 /** 119 * Destroy the USB device and free all resources. 120 * 121 * @param[in] device A valid USB device. 122 */ 123 void 124 dc_usb_device_free(dc_usb_device_t *device); 125 126 /** 127 * Create an iterator to enumerate the USB devices. 128 * 129 * @param[out] iterator A location to store the iterator. 130 * @param[in] context A valid context object. 131 * @param[in] descriptor A valid device descriptor or NULL. 132 * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code 133 * on failure. 134 */ 135 dc_status_t 136 dc_usb_iterator_new (dc_iterator_t **iterator, dc_context_t *context, dc_descriptor_t *descriptor); 137 138 /** 139 * Open a USB connection. 140 * 141 * @param[out] iostream A location to store the USB connection. 142 * @param[in] context A valid context object. 143 * @param[in] device A valid USB device. 144 * @returns #DC_STATUS_SUCCESS on success, or another #dc_status_t code 145 * on failure. 146 */ 147 dc_status_t 148 dc_usb_open (dc_iostream_t **iostream, dc_context_t *context, dc_usb_device_t *device); 149 150 #ifdef __cplusplus 151 } 152 #endif /* __cplusplus */ 153 #endif /* DC_USB_H */