Endianness of control setup packets
[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 <pthread.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 MIN(a, b)       ((a) < (b) ? (a) : (b))
104 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
105
106 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
107
108 enum usbi_log_level {
109         LOG_LEVEL_DEBUG,
110         LOG_LEVEL_INFO,
111         LOG_LEVEL_WARNING,
112         LOG_LEVEL_ERROR,
113 };
114
115 void usbi_log(enum usbi_log_level, const char *function, const char *format, ...);
116
117 #ifdef ENABLE_LOGGING
118 #define _usbi_log(level, fmt...) usbi_log(level, __FUNCTION__, fmt)
119 #else
120 #define _usbi_log(level, fmt...)
121 #endif
122
123 #ifdef ENABLE_DEBUG_LOGGING
124 #define usbi_dbg(fmt...) _usbi_log(LOG_LEVEL_DEBUG, fmt)
125 #else
126 #define usbi_dbg(fmt...)
127 #endif
128
129 #define usbi_info(fmt...) _usbi_log(LOG_LEVEL_INFO, fmt)
130 #define usbi_warn(fmt...) _usbi_log(LOG_LEVEL_WARNING, fmt)
131 #define usbi_err(fmt...) _usbi_log(LOG_LEVEL_ERROR, fmt)
132
133 struct libusb_device {
134         /* lock protects refcnt, everything else is finalized at initialization
135          * time */
136         pthread_mutex_t lock;
137         int refcnt;
138
139         uint8_t bus_number;
140         uint8_t device_address;
141         uint8_t num_configurations;
142
143         struct list_head list;
144         unsigned long session_data;
145         unsigned char os_priv[0];
146 };
147
148 struct libusb_device_handle {
149         /* lock protects claimed_interfaces */
150         pthread_mutex_t lock;
151         unsigned long claimed_interfaces;
152
153         struct list_head list;
154         struct libusb_device *dev;
155         unsigned char os_priv[0];
156 };
157
158 #define USBI_TRANSFER_TIMED_OUT                         (1<<0)
159
160 /* in-memory transfer layout:
161  *
162  * 1. struct usbi_transfer
163  * 2. struct libusb_transfer (which includes iso packets) [variable size]
164  * 3. os private data [variable size]
165  *
166  * from a libusb_transfer, you can get the usbi_transfer by rewinding the
167  * appropriate number of bytes.
168  * the usbi_transfer includes the number of allocated packets, so you can
169  * determine the size of the transfer and hence the start and length of the
170  * OS-private data.
171  */
172
173 struct usbi_transfer {
174         int num_iso_packets;
175         struct list_head list;
176         struct timeval timeout;
177         int transferred;
178         uint8_t flags;
179 };
180
181 #define __USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
182         ((struct libusb_transfer *)(((void *)(transfer)) \
183                 + sizeof(struct usbi_transfer)))
184 #define __LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
185         ((struct usbi_transfer *)(((void *)(transfer)) \
186                 - sizeof(struct usbi_transfer)))
187
188 static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
189 {
190         return ((void *)transfer) + sizeof(struct usbi_transfer)
191                 + sizeof(struct libusb_transfer)
192                 + (transfer->num_iso_packets
193                         * sizeof(struct libusb_iso_packet_descriptor));
194 }
195
196 /* bus structures */
197
198 /* All standard descriptors have these 2 fields in common */
199 struct usb_descriptor_header {
200         uint8_t  bLength;
201         uint8_t  bDescriptorType;
202 };
203
204 /* shared data and functions */
205
206 extern struct list_head usbi_open_devs;
207 extern pthread_mutex_t usbi_open_devs_lock;
208
209 void usbi_io_init(void);
210
211 struct libusb_device *usbi_alloc_device(unsigned long session_id);
212 struct libusb_device *usbi_get_device_by_session_id(unsigned long session_id);
213 int usbi_sanitize_device(struct libusb_device *dev);
214
215 void usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
216         enum libusb_transfer_status status);
217 void usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
218
219 int usbi_parse_descriptor(unsigned char *source, char *descriptor, void *dest);
220 int usbi_get_config_index_by_value(struct libusb_device *dev,
221         uint8_t bConfigurationValue, int *idx);
222
223 /* polling */
224
225 struct usbi_pollfd {
226         /* must come first */
227         struct libusb_pollfd pollfd;
228
229         struct list_head list;
230 };
231
232 int usbi_add_pollfd(int fd, short events);
233 void usbi_remove_pollfd(int fd);
234
235 /* device discovery */
236
237 /* we traverse usbfs without knowing how many devices we are going to find.
238  * so we create this discovered_devs model which is similar to a linked-list
239  * which grows when required. it can be freed once discovery has completed,
240  * eliminating the need for a list node in the libusb_device structure
241  * itself. */
242 struct discovered_devs {
243         size_t len;
244         size_t capacity;
245         struct libusb_device *devices[0];
246 };
247
248 struct discovered_devs *discovered_devs_append(
249         struct discovered_devs *discdevs, struct libusb_device *dev);
250
251 /* OS abstraction */
252
253 struct usbi_os_backend {
254         const char *name;
255         int (*init)(void);
256         void (*exit)(void);
257
258         int (*get_device_list)(struct discovered_devs **discdevs);
259
260         int (*open)(struct libusb_device_handle *handle);
261         void (*close)(struct libusb_device_handle *handle);
262
263         int (*get_device_descriptor)(struct libusb_device *device,
264                 unsigned char *buffer);
265         int (*get_active_config_descriptor)(struct libusb_device *device,
266                 unsigned char *buffer, size_t len);
267         int (*get_config_descriptor)(struct libusb_device *device,
268                 uint8_t config_index, unsigned char *buffer, size_t len);
269
270         int (*set_configuration)(struct libusb_device_handle *handle, int config);
271
272         int (*claim_interface)(struct libusb_device_handle *handle, int iface);
273         int (*release_interface)(struct libusb_device_handle *handle, int iface);
274
275         int (*set_interface_altsetting)(struct libusb_device_handle *handle,
276                 int iface, int altsetting);
277         int (*clear_halt)(struct libusb_device_handle *handle,
278                 unsigned char endpoint);
279         int (*reset_device)(struct libusb_device_handle *handle);
280
281         /* optional */
282         int (*kernel_driver_active)(struct libusb_device_handle *handle,
283                 int interface);
284         int (*detach_kernel_driver)(struct libusb_device_handle *handle,
285                 int interface);
286
287         void (*destroy_device)(struct libusb_device *dev);
288
289         int (*submit_transfer)(struct usbi_transfer *itransfer);
290         int (*cancel_transfer)(struct usbi_transfer *itransfer);
291
292         int (*handle_events)(fd_set *readfds, fd_set *writefds);
293
294         /* number of bytes to reserve for libusb_device.os_priv */
295         size_t device_priv_size;
296
297         /* number of bytes to reserve for libusb_device_handle.os_priv */
298         size_t device_handle_priv_size;
299
300         /* number of bytes to reserve for usbi_transfer.os_priv */
301         size_t transfer_priv_size;
302
303         /* number of additional bytes for os_priv for each iso packet */
304         /* FIXME: linux can't use this any more. if other OS's cannot either,
305          * then remove this */
306         size_t add_iso_packet_size;
307 };
308
309 extern const struct usbi_os_backend * const usbi_backend;
310
311 extern const struct usbi_os_backend linux_usbfs_backend;
312
313 #endif
314