Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 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 |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | |
Erik Gilling | 3af05b0 | 2010-12-09 15:28:06 -0800 | [diff] [blame^] | 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 | |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 33 | struct usb_host_context; |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 34 | struct usb_endpoint_descriptor; |
| 35 | |
| 36 | struct usb_descriptor_iter { |
| 37 | unsigned char* config; |
| 38 | unsigned char* config_end; |
| 39 | unsigned char* curr_desc; |
| 40 | }; |
| 41 | |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 42 | /* Callback for notification when new USB devices are attached. |
| 43 | * Return true to exit from usb_host_run. |
| 44 | */ |
| 45 | typedef int (* usb_device_added_cb)(const char *dev_name, void *client_data); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 46 | |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 47 | /* Callback for notification when USB devices are removed. |
| 48 | * Return true to exit from usb_host_run. |
| 49 | */ |
| 50 | typedef int (* usb_device_removed_cb)(const char *dev_name, void *client_data); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 51 | |
Mike Lockwood | a805519 | 2010-07-19 20:15:15 -0400 | [diff] [blame] | 52 | /* Callback indicating that initial device discovery is done. |
| 53 | * Return true to exit from usb_host_run. |
| 54 | */ |
| 55 | typedef int (* usb_discovery_done_cb)(void *client_data); |
| 56 | |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 57 | /* Call this to initialize the USB host library. */ |
| 58 | struct usb_host_context *usb_host_init(void); |
| 59 | |
| 60 | /* Call this to cleanup the USB host library. */ |
| 61 | void 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. |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 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. |
Mike Lockwood | a805519 | 2010-07-19 20:15:15 -0400 | [diff] [blame] | 69 | * discovery_done_cb is called after the initial discovery of already |
| 70 | * connected devices is complete. |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 71 | */ |
Mike Lockwood | 7a96ba4 | 2010-07-01 11:33:41 -0400 | [diff] [blame] | 72 | void usb_host_run(struct usb_host_context *context, |
| 73 | usb_device_added_cb added_cb, |
Mike Lockwood | 6ac3aa1 | 2010-05-25 08:10:02 -0400 | [diff] [blame] | 74 | usb_device_removed_cb removed_cb, |
Mike Lockwood | a805519 | 2010-07-19 20:15:15 -0400 | [diff] [blame] | 75 | usb_discovery_done_cb discovery_done_cb, |
Mike Lockwood | 6ac3aa1 | 2010-05-25 08:10:02 -0400 | [diff] [blame] | 76 | void *client_data); |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 77 | |
| 78 | /* Creates a usb_device object for a USB device */ |
| 79 | struct usb_device *usb_device_open(const char *dev_name); |
| 80 | |
| 81 | /* Releases all resources associated with the USB device */ |
| 82 | void usb_device_close(struct usb_device *device); |
| 83 | |
| 84 | /* Returns the name for the USB device, which is the same as |
| 85 | * the dev_name passed to usb_device_open() |
| 86 | */ |
| 87 | const char* usb_device_get_name(struct usb_device *device); |
| 88 | |
Mike Lockwood | 203f102 | 2010-05-27 10:12:03 -0400 | [diff] [blame] | 89 | /* Returns a unique ID for the device. Currently this is generated from the |
| 90 | * dev_name path. |
| 91 | */ |
| 92 | int usb_device_get_unique_id(struct usb_device *device); |
| 93 | |
Mike Lockwood | 07eb4af | 2010-07-27 19:05:33 -0400 | [diff] [blame] | 94 | int usb_device_get_unique_id_from_name(const char* name); |
| 95 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 96 | /* Returns the USB vendor ID from the device descriptor for the USB device */ |
| 97 | uint16_t usb_device_get_vendor_id(struct usb_device *device); |
| 98 | |
| 99 | /* Returns the USB product ID from the device descriptor for the USB device */ |
| 100 | uint16_t usb_device_get_product_id(struct usb_device *device); |
| 101 | |
Mike Lockwood | 1b7d991 | 2010-07-24 13:57:21 -0400 | [diff] [blame] | 102 | /* Sends a control message to the specified device on endpoint zero */ |
| 103 | int usb_device_send_control(struct usb_device *device, |
| 104 | int requestType, |
| 105 | int request, |
| 106 | int value, |
| 107 | int index, |
| 108 | int length, |
| 109 | void* buffer); |
| 110 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 111 | /* Returns a USB descriptor string for the given string ID. |
| 112 | * Used to implement usb_device_get_manufacturer_name, |
| 113 | * usb_device_get_product_name and usb_device_get_serial. |
| 114 | * Call free() to free the result when you are done with it. |
| 115 | */ |
| 116 | char* usb_device_get_string(struct usb_device *device, int id); |
| 117 | |
| 118 | /* Returns the manufacturer name for the USB device. |
| 119 | * Call free() to free the result when you are done with it. |
| 120 | */ |
| 121 | char* usb_device_get_manufacturer_name(struct usb_device *device); |
| 122 | |
| 123 | /* Returns the product name for the USB device. |
| 124 | * Call free() to free the result when you are done with it. |
| 125 | */ |
| 126 | char* usb_device_get_product_name(struct usb_device *device); |
| 127 | |
| 128 | /* Returns the USB serial number for the USB device. |
| 129 | * Call free() to free the result when you are done with it. |
| 130 | */ |
| 131 | char* usb_device_get_serial(struct usb_device *device); |
| 132 | |
| 133 | /* Returns true if we have write access to the USB device, |
| 134 | * and false if we only have access to the USB device configuration. |
| 135 | */ |
| 136 | int usb_device_is_writeable(struct usb_device *device); |
| 137 | |
| 138 | /* Initializes a usb_descriptor_iter, which can be used to iterate through all |
| 139 | * the USB descriptors for a USB device. |
| 140 | */ |
| 141 | void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter); |
| 142 | |
| 143 | /* Returns the next USB descriptor for a device, or NULL if we have reached the |
| 144 | * end of the list. |
| 145 | */ |
| 146 | struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter); |
| 147 | |
| 148 | /* Claims the specified interface of a USB device */ |
| 149 | int usb_device_claim_interface(struct usb_device *device, unsigned int interface); |
| 150 | |
| 151 | /* Releases the specified interface of a USB device */ |
| 152 | int usb_device_release_interface(struct usb_device *device, unsigned int interface); |
| 153 | |
| 154 | |
| 155 | /* Creates a new usb_endpoint for the specified endpoint of a USB device. |
| 156 | * This can be used to read or write data across the endpoint. |
| 157 | */ |
| 158 | struct usb_endpoint *usb_endpoint_open(struct usb_device *dev, |
| 159 | const struct usb_endpoint_descriptor *desc); |
| 160 | |
| 161 | /* Releases all resources associated with the endpoint */ |
| 162 | void usb_endpoint_close(struct usb_endpoint *ep); |
| 163 | |
| 164 | /* Begins a read or write operation on the specified endpoint */ |
| 165 | int usb_endpoint_queue(struct usb_endpoint *ep, void *data, int len); |
| 166 | |
| 167 | /* Waits for the results of a previous usb_endpoint_queue operation on the |
| 168 | * specified endpoint. Returns number of bytes transferred, or a negative |
| 169 | * value for error. |
| 170 | */ |
| 171 | int usb_endpoint_wait(struct usb_device *device, int *out_ep_num); |
| 172 | |
| 173 | /* Cancels a pending usb_endpoint_queue() operation on an endpoint. */ |
| 174 | int usb_endpoint_cancel(struct usb_endpoint *ep); |
| 175 | |
Mike Lockwood | 5e567cb | 2010-05-12 08:53:49 -0400 | [diff] [blame] | 176 | /* Returns the usb_device for the given endpoint */ |
| 177 | struct usb_device *usb_endpoint_get_device(struct usb_endpoint *ep); |
| 178 | |
Mike Lockwood | 30ff2c7 | 2010-05-09 16:23:47 -0400 | [diff] [blame] | 179 | /* Returns the endpoint address for the given endpoint */ |
| 180 | int usb_endpoint_number(struct usb_endpoint *ep); |
| 181 | |
| 182 | /* Returns the maximum packet size for the given endpoint. |
| 183 | * For bulk endpoints this should be 512 for highspeed or 64 for fullspeed. |
| 184 | */ |
| 185 | int usb_endpoint_max_packet(struct usb_endpoint *ep); |
| 186 | |
| 187 | #ifdef __cplusplus |
| 188 | } |
| 189 | #endif |
| 190 | #endif /* __USB_HOST_H */ |