usbhost.h revision b5d68a3c28d0b7a947373344cbda2a2369d0b67d
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 17#ifndef __USB_HOST_H 18#define __USB_HOST_H 19 20#ifdef __cplusplus 21extern "C" { 22#endif 23 24#include <stdint.h> 25 26#include <linux/version.h> 27#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) 28#include <linux/usb/ch9.h> 29#else 30#include <linux/usb_ch9.h> 31#endif 32 33struct usb_host_context; 34struct usb_endpoint_descriptor; 35 36struct usb_descriptor_iter { 37 unsigned char* config; 38 unsigned char* config_end; 39 unsigned char* curr_desc; 40}; 41 42struct usb_request 43{ 44 struct usb_device *dev; 45 void* buffer; 46 int buffer_length; 47 int actual_length; 48 int max_packet_size; 49 void *private_data; /* struct usbdevfs_urb* */ 50 int endpoint; 51 void *client_data; /* free for use by client */ 52}; 53 54/* Callback for notification when new USB devices are attached. 55 * Return true to exit from usb_host_run. 56 */ 57typedef int (* usb_device_added_cb)(const char *dev_name, void *client_data); 58 59/* Callback for notification when USB devices are removed. 60 * Return true to exit from usb_host_run. 61 */ 62typedef int (* usb_device_removed_cb)(const char *dev_name, void *client_data); 63 64/* Callback indicating that initial device discovery is done. 65 * Return true to exit from usb_host_run. 66 */ 67typedef int (* usb_discovery_done_cb)(void *client_data); 68 69/* Call this to initialize the USB host library. */ 70struct usb_host_context *usb_host_init(void); 71 72/* Call this to cleanup the USB host library. */ 73void usb_host_cleanup(struct usb_host_context *context); 74 75/* Call this to monitor the USB bus for new and removed devices. 76 * This is intended to be called from a dedicated thread, 77 * as it will not return until one of the callbacks returns true. 78 * added_cb will be called immediately for each existing USB device, 79 * and subsequently each time a new device is added. 80 * removed_cb is called when USB devices are removed from the bus. 81 * discovery_done_cb is called after the initial discovery of already 82 * connected devices is complete. 83 */ 84void usb_host_run(struct usb_host_context *context, 85 usb_device_added_cb added_cb, 86 usb_device_removed_cb removed_cb, 87 usb_discovery_done_cb discovery_done_cb, 88 void *client_data); 89 90/* Creates a usb_device object for a USB device */ 91struct usb_device *usb_device_open(const char *dev_name); 92 93/* Releases all resources associated with the USB device */ 94void usb_device_close(struct usb_device *device); 95 96/* Creates a usb_device object for already open USB device */ 97struct usb_device *usb_device_new(const char *dev_name, int fd); 98 99/* Returns the file descriptor for the usb_device */ 100int usb_device_get_fd(struct usb_device *device); 101 102/* Returns the name for the USB device, which is the same as 103 * the dev_name passed to usb_device_open() 104 */ 105const char* usb_device_get_name(struct usb_device *device); 106 107/* Returns a unique ID for the device. 108 *Currently this is generated from the dev_name path. 109 */ 110int usb_device_get_unique_id(struct usb_device *device); 111 112/* Returns a unique ID for the device name. 113 * Currently this is generated from the device path. 114 */ 115int usb_device_get_unique_id_from_name(const char* name); 116 117/* Returns the device name for the unique ID. 118 * Call free() to deallocate the returned string */ 119char* usb_device_get_name_from_unique_id(int id); 120 121/* Returns the USB vendor ID from the device descriptor for the USB device */ 122uint16_t usb_device_get_vendor_id(struct usb_device *device); 123 124/* Returns the USB product ID from the device descriptor for the USB device */ 125uint16_t usb_device_get_product_id(struct usb_device *device); 126 127const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device); 128 129/* Returns a USB descriptor string for the given string ID. 130 * Used to implement usb_device_get_manufacturer_name, 131 * usb_device_get_product_name and usb_device_get_serial. 132 * Call free() to free the result when you are done with it. 133 */ 134char* usb_device_get_string(struct usb_device *device, int id); 135 136/* Returns the manufacturer name for the USB device. 137 * Call free() to free the result when you are done with it. 138 */ 139char* usb_device_get_manufacturer_name(struct usb_device *device); 140 141/* Returns the product name for the USB device. 142 * Call free() to free the result when you are done with it. 143 */ 144char* usb_device_get_product_name(struct usb_device *device); 145 146/* Returns the USB serial number for the USB device. 147 * Call free() to free the result when you are done with it. 148 */ 149char* usb_device_get_serial(struct usb_device *device); 150 151/* Returns true if we have write access to the USB device, 152 * and false if we only have access to the USB device configuration. 153 */ 154int usb_device_is_writeable(struct usb_device *device); 155 156/* Initializes a usb_descriptor_iter, which can be used to iterate through all 157 * the USB descriptors for a USB device. 158 */ 159void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter); 160 161/* Returns the next USB descriptor for a device, or NULL if we have reached the 162 * end of the list. 163 */ 164struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter); 165 166/* Claims the specified interface of a USB device */ 167int usb_device_claim_interface(struct usb_device *device, unsigned int interface); 168 169/* Releases the specified interface of a USB device */ 170int usb_device_release_interface(struct usb_device *device, unsigned int interface); 171 172/* Requests the kernel to connect or disconnect its driver for the specified interface. 173 * This can be used to ask the kernel to disconnect its driver for a device 174 * so usb_device_claim_interface can claim it instead. 175 */ 176int usb_device_connect_kernel_driver(struct usb_device *device, 177 unsigned int interface, int connect); 178 179/* Sends a control message to the specified device on endpoint zero */ 180int usb_device_control_transfer(struct usb_device *device, 181 int requestType, 182 int request, 183 int value, 184 int index, 185 void* buffer, 186 int length, 187 unsigned int timeout); 188 189/* Reads or writes on a bulk endpoint. 190 * Returns number of bytes transferred, or negative value for error. 191 */ 192int usb_device_bulk_transfer(struct usb_device *device, 193 int endpoint, 194 void* buffer, 195 int length, 196 unsigned int timeout); 197 198/* Creates a new usb_request. */ 199struct usb_request *usb_request_new(struct usb_device *dev, 200 const struct usb_endpoint_descriptor *ep_desc); 201 202/* Releases all resources associated with the request */ 203void usb_request_free(struct usb_request *req); 204 205/* Submits a read or write request on the specified device */ 206int usb_request_queue(struct usb_request *req); 207 208 /* Waits for the results of a previous usb_request_queue operation. 209 * Returns a usb_request, or NULL for error. 210 */ 211struct usb_request *usb_request_wait(struct usb_device *dev); 212 213/* Cancels a pending usb_request_queue() operation. */ 214int usb_request_cancel(struct usb_request *req); 215 216#ifdef __cplusplus 217} 218#endif 219#endif /* __USB_HOST_H */ 220