blob: c330cabbfd623a0ac122a516c0f3250846e7ddb0 [file] [log] [blame]
Mike Lockwood30ff2c72010-05-09 16:23:47 -04001/*
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
Erik Gilling3af05b02010-12-09 15:28:06 -080026#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 Lockwood7a96ba42010-07-01 11:33:41 -040033struct usb_host_context;
Mike Lockwood30ff2c72010-05-09 16:23:47 -040034struct usb_endpoint_descriptor;
35
36struct usb_descriptor_iter {
37 unsigned char* config;
38 unsigned char* config_end;
39 unsigned char* curr_desc;
40};
41
Mike Lockwoode533c5f2011-01-04 20:04:36 -050042struct 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
Mike Lockwood7a96ba42010-07-01 11:33:41 -040053/* 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);
Mike Lockwood30ff2c72010-05-09 16:23:47 -040057
Mike Lockwood7a96ba42010-07-01 11:33:41 -040058/* 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);
Mike Lockwood30ff2c72010-05-09 16:23:47 -040062
Mike Lockwooda8055192010-07-19 20:15:15 -040063/* 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
Mike Lockwood7a96ba42010-07-01 11:33:41 -040068/* 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.
Mike Lockwood30ff2c72010-05-09 16:23:47 -040077 * 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.
Mike Lockwooda8055192010-07-19 20:15:15 -040080 * discovery_done_cb is called after the initial discovery of already
81 * connected devices is complete.
Mike Lockwood30ff2c72010-05-09 16:23:47 -040082 */
Mike Lockwood7a96ba42010-07-01 11:33:41 -040083void usb_host_run(struct usb_host_context *context,
84 usb_device_added_cb added_cb,
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040085 usb_device_removed_cb removed_cb,
Mike Lockwooda8055192010-07-19 20:15:15 -040086 usb_discovery_done_cb discovery_done_cb,
Mike Lockwood6ac3aa12010-05-25 08:10:02 -040087 void *client_data);
Mike Lockwood30ff2c72010-05-09 16:23:47 -040088
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
Mike Lockwoodbe1def82011-01-07 11:31:58 -050095/* Creates a usb_device object for already open USB device */
Mike Lockwoodcd185f22010-12-12 14:17:02 -080096struct usb_device *usb_device_new(const char *dev_name, int fd);
97
Mike Lockwoodbe1def82011-01-07 11:31:58 -050098/* Returns the file descriptor for the usb_device */
Mike Lockwoodcd185f22010-12-12 14:17:02 -080099int usb_device_get_fd(struct usb_device *device);
100
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400101/* 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
Mike Lockwood7d700f82010-12-29 08:47:29 -0500106/* Returns a unique ID for the device.
107 *Currently this is generated from the dev_name path.
Mike Lockwood203f1022010-05-27 10:12:03 -0400108 */
109int usb_device_get_unique_id(struct usb_device *device);
110
Mike Lockwood7d700f82010-12-29 08:47:29 -0500111/* Returns a unique ID for the device name.
112 * Currently this is generated from the device path.
113 */
Mike Lockwood07eb4af2010-07-27 19:05:33 -0400114int usb_device_get_unique_id_from_name(const char* name);
115
Mike Lockwood7d700f82010-12-29 08:47:29 -0500116/* 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
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400120/* 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
Mike Lockwood50372072010-12-13 10:15:25 -0800126const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device);
127
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400128/* Returns a USB descriptor string for the given string ID.
129 * Used to implement usb_device_get_manufacturer_name,
130 * usb_device_get_product_name and usb_device_get_serial.
131 * Call free() to free the result when you are done with it.
132 */
133char* usb_device_get_string(struct usb_device *device, int id);
134
135/* Returns the manufacturer name for the USB device.
136 * Call free() to free the result when you are done with it.
137 */
138char* usb_device_get_manufacturer_name(struct usb_device *device);
139
140/* Returns the product name for the USB device.
141 * Call free() to free the result when you are done with it.
142 */
143char* usb_device_get_product_name(struct usb_device *device);
144
145/* Returns the USB serial number for the USB device.
146 * Call free() to free the result when you are done with it.
147 */
148char* usb_device_get_serial(struct usb_device *device);
149
150/* Returns true if we have write access to the USB device,
151 * and false if we only have access to the USB device configuration.
152 */
153int usb_device_is_writeable(struct usb_device *device);
154
155/* Initializes a usb_descriptor_iter, which can be used to iterate through all
156 * the USB descriptors for a USB device.
157 */
158void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter);
159
160/* Returns the next USB descriptor for a device, or NULL if we have reached the
161 * end of the list.
162 */
163struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter);
164
165/* Claims the specified interface of a USB device */
166int usb_device_claim_interface(struct usb_device *device, unsigned int interface);
167
168/* Releases the specified interface of a USB device */
169int usb_device_release_interface(struct usb_device *device, unsigned int interface);
170
Mike Lockwoodec9e7b12011-01-22 09:17:07 -0800171/* Requests the kernel to connect or disconnect its driver for the specified interface.
172 * This can be used to ask the kernel to disconnect its driver for a device
173 * so usb_device_claim_interface can claim it instead.
174 */
175int usb_device_connect_kernel_driver(struct usb_device *device,
176 unsigned int interface, int connect);
177
Mike Lockwood120b57a2011-01-27 10:46:19 -0800178/* Sends a control message to the specified device on endpoint zero */
179int usb_device_control_transfer(struct usb_device *device,
180 int requestType,
181 int request,
182 int value,
183 int index,
184 void* buffer,
185 int length,
186 unsigned int timeout);
187
188/* Reads or writes on a bulk endpoint */
189int usb_device_bulk_transfer(struct usb_device *device,
190 int endpoint,
191 void* buffer,
192 int length,
193 unsigned int timeout);
194
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500195/* Creates a new usb_request. */
196struct usb_request *usb_request_new(struct usb_device *dev,
197 const struct usb_endpoint_descriptor *ep_desc);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400198
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500199/* Releases all resources associated with the request */
200void usb_request_free(struct usb_request *req);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400201
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500202/* Submits a read or write request on the specified device */
203int usb_request_queue(struct usb_request *req);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400204
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500205 /* Waits for the results of a previous usb_request_queue operation.
206 * Returns a usb_request, or NULL for error.
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400207 */
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500208struct usb_request *usb_request_wait(struct usb_device *dev);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400209
Mike Lockwoode533c5f2011-01-04 20:04:36 -0500210/* Cancels a pending usb_request_queue() operation. */
211int usb_request_cancel(struct usb_request *req);
Mike Lockwood30ff2c72010-05-09 16:23:47 -0400212
213#ifdef __cplusplus
214}
215#endif
216#endif /* __USB_HOST_H */