1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef __USB_HOST_H 17 #define __USB_HOST_H 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 #include <stdint.h> 22 #include <linux/version.h> 23 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) 24 #include <linux/usb/ch9.h> 25 #else 26 #include <linux/usb_ch9.h> 27 #endif 28 struct usb_host_context; 29 struct usb_endpoint_descriptor; 30 struct usb_descriptor_iter { 31 unsigned char* config; 32 unsigned char* config_end; 33 unsigned char* curr_desc; 34 }; 35 struct usb_request 36 { 37 struct usb_device *dev; 38 void* buffer; 39 int buffer_length; 40 int actual_length; 41 int max_packet_size; 42 void *private_data; /* struct usbdevfs_urb* */ 43 int endpoint; 44 void *client_data; /* free for use by client */ 45 }; 46 /* Callback for notification when new USB devices are attached. 47 * Return true to exit from usb_host_run. 48 */ 49 typedef int (* usb_device_added_cb)(const char *dev_name, void *client_data); 50 /* Callback for notification when USB devices are removed. 51 * Return true to exit from usb_host_run. 52 */ 53 typedef int (* usb_device_removed_cb)(const char *dev_name, void *client_data); 54 /* Callback indicating that initial device discovery is done. 55 * Return true to exit from usb_host_run. 56 */ 57 typedef int (* usb_discovery_done_cb)(void *client_data); 58 /* Call this to initialize the USB host library. */ 59 struct usb_host_context *usb_host_init(void); 60 /* Call this to cleanup the USB host library. */ 61 void usb_host_cleanup(struct usb_host_context *context); 62 /* Call this to get the inotify file descriptor. */ 63 int usb_host_get_fd(struct usb_host_context *context); 64 /* Call this to initialize the usb host context. */ 65 int usb_host_load(struct usb_host_context *context, 66 usb_device_added_cb added_cb, 67 usb_device_removed_cb removed_cb, 68 usb_discovery_done_cb discovery_done_cb, 69 void *client_data); 70 /* Call this to read and handle occuring usb event. */ 71 int usb_host_read_event(struct usb_host_context *context); 72 /* Call this to monitor the USB bus for new and removed devices. 73 * This is intended to be called from a dedicated thread, 74 * as it will not return until one of the callbacks returns true. 75 * added_cb will be called immediately for each existing USB device, 76 * and subsequently each time a new device is added. 77 * removed_cb is called when USB devices are removed from the bus. 78 * discovery_done_cb is called after the initial discovery of already 79 * connected devices is complete. 80 */ 81 void usb_host_run(struct usb_host_context *context, 82 usb_device_added_cb added_cb, 83 usb_device_removed_cb removed_cb, 84 usb_discovery_done_cb discovery_done_cb, 85 void *client_data); 86 /* Creates a usb_device object for a USB device */ 87 struct usb_device *usb_device_open(const char *dev_name); 88 /* Releases all resources associated with the USB device */ 89 void usb_device_close(struct usb_device *device); 90 /* Creates a usb_device object for already open USB device */ 91 struct usb_device *usb_device_new(const char *dev_name, int fd); 92 /* Returns the file descriptor for the usb_device */ 93 int usb_device_get_fd(struct usb_device *device); 94 /* Returns the name for the USB device, which is the same as 95 * the dev_name passed to usb_device_open() 96 */ 97 const char* usb_device_get_name(struct usb_device *device); 98 /* Returns a unique ID for the device. 99 *Currently this is generated from the dev_name path. 100 */ 101 int usb_device_get_unique_id(struct usb_device *device); 102 /* Returns a unique ID for the device name. 103 * Currently this is generated from the device path. 104 */ 105 int usb_device_get_unique_id_from_name(const char* name); 106 /* Returns the device name for the unique ID. 107 * Call free() to deallocate the returned string */ 108 char* usb_device_get_name_from_unique_id(int id); 109 /* Returns the USB vendor ID from the device descriptor for the USB device */ 110 uint16_t usb_device_get_vendor_id(struct usb_device *device); 111 /* Returns the USB product ID from the device descriptor for the USB device */ 112 uint16_t usb_device_get_product_id(struct usb_device *device); 113 /* Returns a pointer to device descriptor */ 114 const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device); 115 /* Returns a USB descriptor string for the given string ID. 116 * Return value: < 0 on error. 0 on success. 117 * The string is returned in ucs2_out in USB-native UCS-2 encoding. 118 * 119 * parameters: 120 * id - the string descriptor index. 121 * timeout - in milliseconds (see Documentation/driver-api/usb/usb.rst) 122 * ucs2_out - Must point to null on call. 123 * Will be filled in with a buffer on success. 124 * If this is non-null on return, it must be free()d. 125 * response_size - size, in bytes, of ucs-2 string in ucs2_out. 126 * The size isn't guaranteed to include null termination. 127 * Call free() to free the result when you are done with it. 128 */ 129 int usb_device_get_string_ucs2(struct usb_device* device, int id, int timeout, void** ucs2_out, 130 size_t* response_size); 131 /* Returns the length in bytes read into the raw descriptors array */ 132 size_t usb_device_get_descriptors_length(const struct usb_device* device); 133 /* Returns a pointer to the raw descriptors array */ 134 const unsigned char* usb_device_get_raw_descriptors(const struct usb_device* device); 135 /* Returns a USB descriptor string for the given string ID. 136 * Used to implement usb_device_get_manufacturer_name, 137 * usb_device_get_product_name and usb_device_get_serial. 138 * Returns ascii - non ascii characters will be replaced with '?'. 139 * Call free() to free the result when you are done with it. 140 */ 141 char* usb_device_get_string(struct usb_device *device, int id, int timeout); 142 /* Returns the manufacturer name for the USB device. 143 * Call free() to free the result when you are done with it. 144 */ 145 char* usb_device_get_manufacturer_name(struct usb_device *device, int timeout); 146 /* Returns the product name for the USB device. 147 * Call free() to free the result when you are done with it. 148 */ 149 char* usb_device_get_product_name(struct usb_device *device, int timeout); 150 /* Returns the version number for the USB device. 151 */ 152 int usb_device_get_version(struct usb_device *device); 153 /* Returns the USB serial number for the USB device. 154 * Call free() to free the result when you are done with it. 155 */ 156 char* usb_device_get_serial(struct usb_device *device, int timeout); 157 /* Returns true if we have write access to the USB device, 158 * and false if we only have access to the USB device configuration. 159 */ 160 int usb_device_is_writeable(struct usb_device *device); 161 /* Initializes a usb_descriptor_iter, which can be used to iterate through all 162 * the USB descriptors for a USB device. 163 */ 164 void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter); 165 /* Returns the next USB descriptor for a device, or NULL if we have reached the 166 * end of the list. 167 */ 168 struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter); 169 /* Claims the specified interface of a USB device */ 170 int usb_device_claim_interface(struct usb_device *device, unsigned int interface); 171 /* Releases the specified interface of a USB device */ 172 int usb_device_release_interface(struct usb_device *device, unsigned int interface); 173 /* Requests the kernel to connect or disconnect its driver for the specified interface. 174 * This can be used to ask the kernel to disconnect its driver for a device 175 * so usb_device_claim_interface can claim it instead. 176 */ 177 int usb_device_connect_kernel_driver(struct usb_device *device, 178 unsigned int interface, int connect); 179 /* Sets the current configuration for the device to the specified configuration */ 180 int usb_device_set_configuration(struct usb_device *device, int configuration); 181 /* Sets the specified interface of a USB device */ 182 int usb_device_set_interface(struct usb_device *device, unsigned int interface, 183 unsigned int alt_setting); 184 /* Sends a control message to the specified device on endpoint zero */ 185 int usb_device_control_transfer(struct usb_device *device, 186 int requestType, 187 int request, 188 int value, 189 int index, 190 void* buffer, 191 int length, 192 unsigned int timeout); 193 /* Reads or writes on a bulk endpoint. 194 * Returns number of bytes transferred, or negative value for error. 195 */ 196 int usb_device_bulk_transfer(struct usb_device *device, 197 int endpoint, 198 void* buffer, 199 unsigned int length, 200 unsigned int timeout); 201 /** Reset USB bus for the device */ 202 int usb_device_reset(struct usb_device *device); 203 /* Creates a new usb_request. */ 204 struct usb_request *usb_request_new(struct usb_device *dev, 205 const struct usb_endpoint_descriptor *ep_desc); 206 /* Releases all resources associated with the request */ 207 void usb_request_free(struct usb_request *req); 208 /* Submits a read or write request on the specified device */ 209 int usb_request_queue(struct usb_request *req); 210 /* Waits for the results of a previous usb_request_queue operation. timeoutMillis == -1 requests 211 * to wait forever. 212 * Returns a usb_request, or NULL for error. 213 */ 214 struct usb_request *usb_request_wait(struct usb_device *dev, int timeoutMillis); 215 /* Cancels a pending usb_request_queue() operation. */ 216 int usb_request_cancel(struct usb_request *req); 217 #ifdef __cplusplus 218 } 219 #endif 220 #endif /* __USB_HOST_H */