Support for changing altsetting
[platform/upstream/libusb.git] / libusb / libusbi.h
1 /*
2  * Internal header for libusb
3  * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
4  * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #ifndef __LIBUSBI_H__
22 #define __LIBUSBI_H__
23
24 #include <config.h>
25
26 #include <endian.h>
27 #include <stddef.h>
28 #include <sys/select.h>
29 #include <time.h>
30
31 #include <libusb.h>
32
33 #define DEVICE_DESC_LENGTH              18
34
35 #define USB_MAXENDPOINTS        32
36 #define USB_MAXINTERFACES       32
37 #define USB_MAXCONFIG           8
38
39 struct list_head {
40         struct list_head *prev, *next;
41 };
42
43 /* Get an entry from the list 
44  *      ptr - the address of this list_head element in "type" 
45  *      type - the data type that contains "member"
46  *      member - the list_head element in "type" 
47  */
48 #define list_entry(ptr, type, member) \
49         ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member)))
50
51 /* Get each entry from a list
52  *      pos - A structure pointer has a "member" element
53  *      head - list head
54  *      member - the list_head element in "pos"
55  */
56 #define list_for_each_entry(pos, head, member)                          \
57         for (pos = list_entry((head)->next, typeof(*pos), member);      \
58              &pos->member != (head);                                    \
59              pos = list_entry(pos->member.next, typeof(*pos), member))
60
61 #define list_for_each_entry_safe(pos, n, head, member)                  \
62         for (pos = list_entry((head)->next, typeof(*pos), member),      \
63                 n = list_entry(pos->member.next, typeof(*pos), member); \
64              &pos->member != (head);                                    \
65              pos = n, n = list_entry(n->member.next, typeof(*n), member))
66
67 #define list_empty(entry) ((entry)->next == (entry))
68
69 static inline void list_init(struct list_head *entry)
70 {
71         entry->prev = entry->next = entry;
72 }
73
74 static inline void list_add(struct list_head *entry, struct list_head *head)
75 {
76         entry->next = head->next;
77         entry->prev = head;
78
79         head->next->prev = entry;
80         head->next = entry;
81 }
82
83 static inline void list_add_tail(struct list_head *entry,
84         struct list_head *head)
85 {
86         entry->next = head;
87         entry->prev = head->prev;
88
89         head->prev->next = entry;
90         head->prev = entry;
91 }
92
93 static inline void list_del(struct list_head *entry)
94 {
95         entry->next->prev = entry->prev;
96         entry->prev->next = entry->next;
97 }
98
99 #define container_of(ptr, type, member) ({                      \
100         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
101         (type *)( (char *)__mptr - offsetof(type,member) );})
102
103 #define bswap16(x) (((x & 0xff) << 8) | (x >> 8))
104 #if __BYTE_ORDER == __LITTLE_ENDIAN
105 #define cpu_to_le16(x) (x)
106 #define le16_to_cpu(x) (x)
107 #elif __BYTE_ORDER == __BIG_ENDIAN
108 #define le16_to_cpu(x) bswap16(x)
109 #define cpu_to_le16(x) bswap16(x)
110 #else
111 #error "Unrecognized endianness"
112 #endif
113
114 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
115 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
116
117 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
118
119 enum usbi_log_level {
120         LOG_LEVEL_DEBUG,
121         LOG_LEVEL_INFO,
122         LOG_LEVEL_WARNING,
123         LOG_LEVEL_ERROR,
124 };
125
126 void usbi_log(enum usbi_log_level, const char *function, const char *format, ...);
127
128 #ifdef ENABLE_LOGGING
129 #define _usbi_log(level, fmt...) usbi_log(level, __FUNCTION__, fmt)
130 #else
131 #define _usbi_log(level, fmt...)
132 #endif
133
134 #ifdef ENABLE_DEBUG_LOGGING
135 #define usbi_dbg(fmt...) _usbi_log(LOG_LEVEL_DEBUG, fmt)
136 #else
137 #define usbi_dbg(fmt...)
138 #endif
139
140 #define usbi_info(fmt...) _usbi_log(LOG_LEVEL_INFO, fmt)
141 #define usbi_warn(fmt...) _usbi_log(LOG_LEVEL_WARNING, fmt)
142 #define usbi_err(fmt...) _usbi_log(LOG_LEVEL_ERROR, fmt)
143
144 struct libusb_device {
145         struct list_head list;
146         int refcnt;
147         unsigned long session_data;
148         struct libusb_device_descriptor desc;
149         struct libusb_config_descriptor *config;
150         unsigned char os_priv[0];
151 };
152
153 struct libusb_device_handle {
154         struct list_head list;
155         struct libusb_device *dev;
156         unsigned char os_priv[0];
157 };
158
159 #define USBI_TRANSFER_SYNC_CANCELLED            (1<<0)
160 #define USBI_TRANSFER_TIMED_OUT                         (1<<1)
161
162 struct usbi_transfer {
163         /* must come first */
164         struct libusb_transfer pub;
165
166         struct list_head list;
167         struct timeval timeout;
168         int transferred;
169         uint8_t flags;
170
171         unsigned char os_priv[0];
172 };
173
174 /* bus structures */
175
176 /* All standard descriptors have these 2 fields in common */
177 struct usb_descriptor_header {
178         uint8_t  bLength;
179         uint8_t  bDescriptorType;
180 };
181
182 /* shared data and functions */
183
184 extern struct list_head usbi_open_devs;
185
186 void usbi_io_init(void);
187
188 struct libusb_device *usbi_alloc_device(unsigned long session_id);
189 struct libusb_device *usbi_get_device_by_session_id(unsigned long session_id);
190
191 void usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
192         enum libusb_transfer_status status);
193 void usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
194
195 int usbi_parse_descriptor(unsigned char *source, char *descriptor, void *dest);
196 int usbi_parse_configuration(struct libusb_config_descriptor *config,
197                 unsigned char *buffer);
198
199 /* polling */
200
201 struct usbi_pollfd {
202         /* must come first */
203         struct libusb_pollfd pollfd;
204
205         struct list_head list;
206 };
207
208 int usbi_add_pollfd(int fd, short events);
209 void usbi_remove_pollfd(int fd);
210
211 /* device discovery */
212
213 /* we traverse usbfs without knowing how many devices we are going to find.
214  * so we create this discovered_devs model which is similar to a linked-list
215  * which grows when required. it can be freed once discovery has completed,
216  * eliminating the need for a list node in the libusb_device structure
217  * itself. */
218 struct discovered_devs {
219         size_t len;
220         size_t capacity;
221         struct libusb_device *devices[0];
222 };
223
224 struct discovered_devs *discovered_devs_append(
225         struct discovered_devs *discdevs, struct libusb_device *dev);
226
227 /* OS abstraction */
228
229 struct usbi_os_backend {
230         const char *name;
231         int (*init)(void);
232         void (*exit)(void);
233
234         int (*get_device_list)(struct discovered_devs **discdevs);
235
236         int (*open)(struct libusb_device_handle *handle);
237         void (*close)(struct libusb_device_handle *handle);
238
239         int (*claim_interface)(struct libusb_device_handle *handle, int iface);
240         int (*release_interface)(struct libusb_device_handle *handle, int iface);
241
242         int (*set_interface_altsetting)(struct libusb_device_handle *handle,
243                 int iface, int altsetting);
244
245         void (*destroy_device)(struct libusb_device *dev);
246
247         int (*submit_transfer)(struct usbi_transfer *itransfer);
248         int (*cancel_transfer)(struct usbi_transfer *itransfer);
249
250         int (*handle_events)(fd_set *readfds, fd_set *writefds);
251
252         /* number of bytes to reserve for libusb_device.os_priv */
253         size_t device_priv_size;
254
255         /* number of bytes to reserve for libusb_device_handle.os_priv */
256         size_t device_handle_priv_size;
257
258         /* number of bytes to reserve for usbi_transfer.os_priv */
259         size_t transfer_priv_size;
260 };
261
262 extern const struct usbi_os_backend * const usbi_backend;
263
264 extern const struct usbi_os_backend linux_usbfs_backend;
265
266 #endif
267