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