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