• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stddef.h>
25 #include <stdint.h>
26 
27 #include <linux/version.h>
28 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
29 #include <linux/usb/ch9.h>
30 #else
31 #include <linux/usb_ch9.h>
32 #endif
33 
34 struct usb_host_context;
35 struct usb_endpoint_descriptor;
36 
37 struct usb_descriptor_iter {
38     unsigned char*  config;
39     unsigned char*  config_end;
40     unsigned char*  curr_desc;
41 };
42 
43 struct usb_request
44 {
45     struct usb_device *dev;
46     void* buffer;
47     int buffer_length;
48     int actual_length;
49     int max_packet_size;
50     void *private_data; /* struct usbdevfs_urb* */
51     int endpoint;
52     void *client_data;  /* free for use by client */
53 };
54 
55 /* Callback for notification when new USB devices are attached.
56  * Return true to exit from usb_host_run.
57  */
58 typedef int (* usb_device_added_cb)(const char *dev_name, void *client_data);
59 
60 /* Callback for notification when USB devices are removed.
61  * Return true to exit from usb_host_run.
62  */
63 typedef int (* usb_device_removed_cb)(const char *dev_name, void *client_data);
64 
65 /* Callback indicating that initial device discovery is done.
66  * Return true to exit from usb_host_run.
67  */
68 typedef int (* usb_discovery_done_cb)(void *client_data);
69 
70 /* Call this to initialize the USB host library. */
71 struct usb_host_context *usb_host_init(void);
72 
73 /* Call this to cleanup the USB host library. */
74 void usb_host_cleanup(struct usb_host_context *context);
75 
76 /* Call this to get the inotify file descriptor. */
77 int usb_host_get_fd(struct usb_host_context *context);
78 
79 /* Call this to initialize the usb host context. */
80 int usb_host_load(struct usb_host_context *context,
81                   usb_device_added_cb added_cb,
82                   usb_device_removed_cb removed_cb,
83                   usb_discovery_done_cb discovery_done_cb,
84                   void *client_data);
85 
86 /* Call this to read and handle occuring usb event. */
87 int usb_host_read_event(struct usb_host_context *context);
88 
89 /* Call this to monitor the USB bus for new and removed devices.
90  * This is intended to be called from a dedicated thread,
91  * as it will not return until one of the callbacks returns true.
92  * added_cb will be called immediately for each existing USB device,
93  * and subsequently each time a new device is added.
94  * removed_cb is called when USB devices are removed from the bus.
95  * discovery_done_cb is called after the initial discovery of already
96  * connected devices is complete.
97  */
98 void usb_host_run(struct usb_host_context *context,
99                   usb_device_added_cb added_cb,
100                   usb_device_removed_cb removed_cb,
101                   usb_discovery_done_cb discovery_done_cb,
102                   void *client_data);
103 
104 /* Creates a usb_device object for a USB device */
105 struct usb_device *usb_device_open(const char *dev_name);
106 
107 /* Releases all resources associated with the USB device */
108 void usb_device_close(struct usb_device *device);
109 
110 /* Creates a usb_device object for already open USB device */
111 struct usb_device *usb_device_new(const char *dev_name, int fd);
112 
113 /* Returns the file descriptor for the usb_device */
114 int usb_device_get_fd(struct usb_device *device);
115 
116 /* Returns the name for the USB device, which is the same as
117  * the dev_name passed to usb_device_open()
118  */
119 const char* usb_device_get_name(struct usb_device *device);
120 
121 /* Returns a unique ID for the device.
122  *Currently this is generated from the dev_name path.
123  */
124 int usb_device_get_unique_id(struct usb_device *device);
125 
126 /* Returns a unique ID for the device name.
127  * Currently this is generated from the device path.
128  */
129 int usb_device_get_unique_id_from_name(const char* name);
130 
131 /* Returns the device name for the unique ID.
132  * Call free() to deallocate the returned string */
133 char* usb_device_get_name_from_unique_id(int id);
134 
135 /* Returns the USB vendor ID from the device descriptor for the USB device */
136 uint16_t usb_device_get_vendor_id(struct usb_device *device);
137 
138 /* Returns the USB product ID from the device descriptor for the USB device */
139 uint16_t usb_device_get_product_id(struct usb_device *device);
140 
141 /* Returns a pointer to device descriptor */
142 const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device);
143 
144 /* Returns a USB descriptor string for the given string ID.
145  * Return value: < 0 on error.  0 on success.
146  * The string is returned in ucs2_out in USB-native UCS-2 encoding.
147  *
148  * parameters:
149  *  id - the string descriptor index.
150  *  timeout - in milliseconds (see Documentation/driver-api/usb/usb.rst)
151  *  ucs2_out - Must point to null on call.
152  *             Will be filled in with a buffer on success.
153  *             If this is non-null on return, it must be free()d.
154  *  response_size - size, in bytes, of ucs-2 string in ucs2_out.
155  *                  The size isn't guaranteed to include null termination.
156  * Call free() to free the result when you are done with it.
157  */
158 int usb_device_get_string_ucs2(struct usb_device* device, int id, int timeout, void** ucs2_out,
159                                size_t* response_size);
160 
161 /* Returns the length in bytes read into the raw descriptors array */
162 size_t usb_device_get_descriptors_length(const struct usb_device* device);
163 
164 /* Returns a pointer to the raw descriptors array */
165 const unsigned char* usb_device_get_raw_descriptors(const struct usb_device* device);
166 
167 /* Returns a USB descriptor string for the given string ID.
168  * Used to implement usb_device_get_manufacturer_name,
169  * usb_device_get_product_name and usb_device_get_serial.
170  * Returns ascii - non ascii characters will be replaced with '?'.
171  * Call free() to free the result when you are done with it.
172  */
173 char* usb_device_get_string(struct usb_device *device, int id, int timeout);
174 
175 /* Returns the manufacturer name for the USB device.
176  * Call free() to free the result when you are done with it.
177  */
178 char* usb_device_get_manufacturer_name(struct usb_device *device, int timeout);
179 
180 /* Returns the product name for the USB device.
181  * Call free() to free the result when you are done with it.
182  */
183 char* usb_device_get_product_name(struct usb_device *device, int timeout);
184 
185 /* Returns the version number for the USB device.
186  */
187 int usb_device_get_version(struct usb_device *device);
188 
189 /* Returns the USB serial number for the USB device.
190  * Call free() to free the result when you are done with it.
191  */
192 char* usb_device_get_serial(struct usb_device *device, int timeout);
193 
194 /* Returns true if we have write access to the USB device,
195  * and false if we only have access to the USB device configuration.
196  */
197 int usb_device_is_writeable(struct usb_device *device);
198 
199 /* Initializes a usb_descriptor_iter, which can be used to iterate through all
200  * the USB descriptors for a USB device.
201  */
202 void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter);
203 
204 /* Returns the next USB descriptor for a device, or NULL if we have reached the
205  * end of the list.
206  */
207 struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter);
208 
209 /* Claims the specified interface of a USB device */
210 int usb_device_claim_interface(struct usb_device *device, unsigned int interface);
211 
212 /* Releases the specified interface of a USB device */
213 int usb_device_release_interface(struct usb_device *device, unsigned int interface);
214 
215 /* Requests the kernel to connect or disconnect its driver for the specified interface.
216  * This can be used to ask the kernel to disconnect its driver for a device
217  * so usb_device_claim_interface can claim it instead.
218  */
219 int usb_device_connect_kernel_driver(struct usb_device *device,
220         unsigned int interface, int connect);
221 
222 /* Sets the current configuration for the device to the specified configuration */
223 int usb_device_set_configuration(struct usb_device *device, int configuration);
224 
225 /* Sets the specified interface of a USB device */
226 int usb_device_set_interface(struct usb_device *device, unsigned int interface,
227                             unsigned int alt_setting);
228 
229 /* Sends a control message to the specified device on endpoint zero */
230 int usb_device_control_transfer(struct usb_device *device,
231                             int requestType,
232                             int request,
233                             int value,
234                             int index,
235                             void* buffer,
236                             int length,
237                             unsigned int timeout);
238 
239 /* Reads or writes on a bulk endpoint.
240  * Returns number of bytes transferred, or negative value for error.
241  */
242 int usb_device_bulk_transfer(struct usb_device *device,
243                             int endpoint,
244                             void* buffer,
245                             unsigned int length,
246                             unsigned int timeout);
247 
248 /** Reset USB bus for the device */
249 int usb_device_reset(struct usb_device *device);
250 
251 /* Creates a new usb_request. */
252 struct usb_request *usb_request_new(struct usb_device *dev,
253         const struct usb_endpoint_descriptor *ep_desc);
254 
255 /* Releases all resources associated with the request */
256 void usb_request_free(struct usb_request *req);
257 
258 /* Submits a read or write request on the specified device */
259 int usb_request_queue(struct usb_request *req);
260 
261  /* Waits for the results of a previous usb_request_queue operation. timeoutMillis == -1 requests
262   * to wait forever.
263   * Returns a usb_request, or NULL for error.
264   */
265 struct usb_request *usb_request_wait(struct usb_device *dev, int timeoutMillis);
266 
267 /* Cancels a pending usb_request_queue() operation. */
268 int usb_request_cancel(struct usb_request *req);
269 
270 #ifdef __cplusplus
271 }
272 #endif
273 #endif /* __USB_HOST_H */
274