usbhost.h revision 6ac3aa157493ef24bc837b679dd8292fad8961e0
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
26struct usb_endpoint_descriptor;
27
28struct usb_descriptor_iter {
29    unsigned char*  config;
30    unsigned char*  config_end;
31    unsigned char*  curr_desc;
32};
33
34/* callback for notification when new USB devices are attached */
35typedef void (* usb_device_added_cb)(const char *dev_name, void *client_data);
36
37/* callback for notification when USB devices are removed */
38typedef void (* usb_device_removed_cb)(const char *dev_name, void *client_data);
39
40/* Call this to start monitoring the USB bus.
41 * added_cb will be called immediately for each existing USB device,
42 * and subsequently each time a new device is added.
43 * removed_cb is called when USB devices are removed from the bus.
44 */
45int usb_host_init(usb_device_added_cb added_cb,
46                  usb_device_removed_cb removed_cb,
47                  void *client_data);
48
49/* Creates a usb_device object for a USB device */
50struct usb_device *usb_device_open(const char *dev_name);
51
52/* Releases all resources associated with the USB device */
53void usb_device_close(struct usb_device *device);
54
55/* Returns the name for the USB device, which is the same as
56 * the dev_name passed to usb_device_open()
57 */
58const char* usb_device_get_name(struct usb_device *device);
59
60/* Returns the USB vendor ID from the device descriptor for the USB device */
61uint16_t usb_device_get_vendor_id(struct usb_device *device);
62
63/* Returns the USB product ID from the device descriptor for the USB device */
64uint16_t usb_device_get_product_id(struct usb_device *device);
65
66/* Returns a USB descriptor string for the given string ID.
67 * Used to implement usb_device_get_manufacturer_name,
68 * usb_device_get_product_name and usb_device_get_serial.
69 * Call free() to free the result when you are done with it.
70 */
71char* usb_device_get_string(struct usb_device *device, int id);
72
73/* Returns the manufacturer name for the USB device.
74 * Call free() to free the result when you are done with it.
75 */
76char* usb_device_get_manufacturer_name(struct usb_device *device);
77
78/* Returns the product name for the USB device.
79 * Call free() to free the result when you are done with it.
80 */
81char* usb_device_get_product_name(struct usb_device *device);
82
83/* Returns the USB serial number for the USB device.
84 * Call free() to free the result when you are done with it.
85 */
86char* usb_device_get_serial(struct usb_device *device);
87
88/* Returns true if we have write access to the USB device,
89 * and false if we only have access to the USB device configuration.
90 */
91int usb_device_is_writeable(struct usb_device *device);
92
93/* Initializes a usb_descriptor_iter, which can be used to iterate through all
94 * the USB descriptors for a USB device.
95 */
96void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter);
97
98/* Returns the next USB descriptor for a device, or NULL if we have reached the
99 * end of the list.
100 */
101struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter);
102
103/* Claims the specified interface of a USB device */
104int usb_device_claim_interface(struct usb_device *device, unsigned int interface);
105
106/* Releases the specified interface of a USB device */
107int usb_device_release_interface(struct usb_device *device, unsigned int interface);
108
109
110/* Creates a new usb_endpoint for the specified endpoint of a USB device.
111 * This can be used to read or write data across the endpoint.
112 */
113struct usb_endpoint *usb_endpoint_open(struct usb_device *dev,
114                const struct usb_endpoint_descriptor *desc);
115
116/* Releases all resources associated with the endpoint */
117void usb_endpoint_close(struct usb_endpoint *ep);
118
119/* Begins a read or write operation on the specified endpoint */
120int usb_endpoint_queue(struct usb_endpoint *ep, void *data, int len);
121
122 /* Waits for the results of a previous usb_endpoint_queue operation on the
123  * specified endpoint.  Returns number of bytes transferred, or a negative
124  * value for error.
125  */
126int usb_endpoint_wait(struct usb_device *device, int *out_ep_num);
127
128/* Cancels a pending usb_endpoint_queue() operation on an endpoint. */
129int usb_endpoint_cancel(struct usb_endpoint *ep);
130
131/* Returns the usb_device for the given endpoint */
132struct usb_device *usb_endpoint_get_device(struct usb_endpoint *ep);
133
134/* Returns the endpoint address for the given endpoint */
135int usb_endpoint_number(struct usb_endpoint *ep);
136
137/* Returns the maximum packet size for the given endpoint.
138 * For bulk endpoints this should be 512 for highspeed or 64 for fullspeed.
139 */
140int usb_endpoint_max_packet(struct usb_endpoint *ep);
141
142#ifdef __cplusplus
143}
144#endif
145#endif /* __USB_HOST_H */
146