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