usbhost.h revision 7d700f8bdce747a26e3ee4737683194d77286ba3
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 42/* Callback for notification when new USB devices are attached. 43 * Return true to exit from usb_host_run. 44 */ 45typedef int (* usb_device_added_cb)(const char *dev_name, void *client_data); 46 47/* Callback for notification when USB devices are removed. 48 * Return true to exit from usb_host_run. 49 */ 50typedef int (* usb_device_removed_cb)(const char *dev_name, void *client_data); 51 52/* Callback indicating that initial device discovery is done. 53 * Return true to exit from usb_host_run. 54 */ 55typedef int (* usb_discovery_done_cb)(void *client_data); 56 57/* Call this to initialize the USB host library. */ 58struct usb_host_context *usb_host_init(void); 59 60/* Call this to cleanup the USB host library. */ 61void usb_host_cleanup(struct usb_host_context *context); 62 63/* Call this to monitor the USB bus for new and removed devices. 64 * This is intended to be called from a dedicated thread, 65 * as it will not return until one of the callbacks returns true. 66 * added_cb will be called immediately for each existing USB device, 67 * and subsequently each time a new device is added. 68 * removed_cb is called when USB devices are removed from the bus. 69 * discovery_done_cb is called after the initial discovery of already 70 * connected devices is complete. 71 */ 72void usb_host_run(struct usb_host_context *context, 73 usb_device_added_cb added_cb, 74 usb_device_removed_cb removed_cb, 75 usb_discovery_done_cb discovery_done_cb, 76 void *client_data); 77 78/* Creates a usb_device object for a USB device */ 79struct usb_device *usb_device_open(const char *dev_name); 80 81/* Releases all resources associated with the USB device */ 82void usb_device_close(struct usb_device *device); 83 84/* Creates a usb_device object for already open USB device. 85 * This is intended to facilitate sharing USB devices across address spaces. 86 */ 87struct usb_device *usb_device_new(const char *dev_name, int fd); 88 89/* Returns the file descriptor for the usb_device. Used in conjunction with 90 * usb_device_new() for sharing USB devices across address spaces. 91 */ 92int usb_device_get_fd(struct usb_device *device); 93 94/* Returns the name for the USB device, which is the same as 95 * the dev_name passed to usb_device_open() 96 */ 97const char* usb_device_get_name(struct usb_device *device); 98 99/* Returns a unique ID for the device. 100 *Currently this is generated from the dev_name path. 101 */ 102int usb_device_get_unique_id(struct usb_device *device); 103 104/* Returns a unique ID for the device name. 105 * Currently this is generated from the device path. 106 */ 107int usb_device_get_unique_id_from_name(const char* name); 108 109/* Returns the device name for the unique ID. 110 * Call free() to deallocate the returned string */ 111char* usb_device_get_name_from_unique_id(int id); 112 113/* Returns the USB vendor ID from the device descriptor for the USB device */ 114uint16_t usb_device_get_vendor_id(struct usb_device *device); 115 116/* Returns the USB product ID from the device descriptor for the USB device */ 117uint16_t usb_device_get_product_id(struct usb_device *device); 118 119const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device); 120 121/* Sends a control message to the specified device on endpoint zero */ 122int usb_device_send_control(struct usb_device *device, 123 int requestType, 124 int request, 125 int value, 126 int index, 127 int length, 128 void* buffer); 129 130/* Returns a USB descriptor string for the given string ID. 131 * Used to implement usb_device_get_manufacturer_name, 132 * usb_device_get_product_name and usb_device_get_serial. 133 * Call free() to free the result when you are done with it. 134 */ 135char* usb_device_get_string(struct usb_device *device, int id); 136 137/* Returns the manufacturer name for the USB device. 138 * Call free() to free the result when you are done with it. 139 */ 140char* usb_device_get_manufacturer_name(struct usb_device *device); 141 142/* Returns the product name for the USB device. 143 * Call free() to free the result when you are done with it. 144 */ 145char* usb_device_get_product_name(struct usb_device *device); 146 147/* Returns the USB serial number for the USB device. 148 * Call free() to free the result when you are done with it. 149 */ 150char* usb_device_get_serial(struct usb_device *device); 151 152/* Returns true if we have write access to the USB device, 153 * and false if we only have access to the USB device configuration. 154 */ 155int usb_device_is_writeable(struct usb_device *device); 156 157/* Initializes a usb_descriptor_iter, which can be used to iterate through all 158 * the USB descriptors for a USB device. 159 */ 160void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter); 161 162/* Returns the next USB descriptor for a device, or NULL if we have reached the 163 * end of the list. 164 */ 165struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter); 166 167/* Claims the specified interface of a USB device */ 168int usb_device_claim_interface(struct usb_device *device, unsigned int interface); 169 170/* Releases the specified interface of a USB device */ 171int usb_device_release_interface(struct usb_device *device, unsigned int interface); 172 173 174/* Creates a new usb_endpoint for the specified endpoint of a USB device. 175 * This can be used to read or write data across the endpoint. 176 */ 177struct usb_endpoint *usb_endpoint_open(struct usb_device *dev, 178 const struct usb_endpoint_descriptor *desc); 179 180/* Releases all resources associated with the endpoint */ 181void usb_endpoint_close(struct usb_endpoint *ep); 182 183/* Begins a read or write operation on the specified endpoint */ 184int usb_endpoint_queue(struct usb_endpoint *ep, void *data, int len); 185 186 /* Waits for the results of a previous usb_endpoint_queue operation on the 187 * specified endpoint. Returns number of bytes transferred, or a negative 188 * value for error. 189 */ 190int usb_endpoint_wait(struct usb_device *device, int *out_ep_num); 191 192/* Cancels a pending usb_endpoint_queue() operation on an endpoint. */ 193int usb_endpoint_cancel(struct usb_endpoint *ep); 194 195/* Returns the usb_device for the given endpoint */ 196struct usb_device *usb_endpoint_get_device(struct usb_endpoint *ep); 197 198/* Returns the endpoint address for the given endpoint */ 199int usb_endpoint_number(struct usb_endpoint *ep); 200 201/* Returns the maximum packet size for the given endpoint. 202 * For bulk endpoints this should be 512 for highspeed or 64 for fullspeed. 203 */ 204int usb_endpoint_max_packet(struct usb_endpoint *ep); 205 206#ifdef __cplusplus 207} 208#endif 209#endif /* __USB_HOST_H */ 210