Handle hot-unplugging
[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 <poll.h>
27 #include <pthread.h>
28 #include <stddef.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 void usbi_handle_disconnect(struct libusb_device_handle *handle);
215
216 void usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
217         enum libusb_transfer_status status);
218 void usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
219
220 int usbi_parse_descriptor(unsigned char *source, char *descriptor, void *dest);
221 int usbi_get_config_index_by_value(struct libusb_device *dev,
222         uint8_t bConfigurationValue, int *idx);
223
224 /* polling */
225
226 struct usbi_pollfd {
227         /* must come first */
228         struct libusb_pollfd pollfd;
229
230         struct list_head list;
231 };
232
233 int usbi_add_pollfd(int fd, short events);
234 void usbi_remove_pollfd(int fd);
235
236 /* device discovery */
237
238 /* we traverse usbfs without knowing how many devices we are going to find.
239  * so we create this discovered_devs model which is similar to a linked-list
240  * which grows when required. it can be freed once discovery has completed,
241  * eliminating the need for a list node in the libusb_device structure
242  * itself. */
243 struct discovered_devs {
244         size_t len;
245         size_t capacity;
246         struct libusb_device *devices[0];
247 };
248
249 struct discovered_devs *discovered_devs_append(
250         struct discovered_devs *discdevs, struct libusb_device *dev);
251
252 /* OS abstraction */
253
254 struct usbi_os_backend {
255         const char *name;
256         int (*init)(void);
257         void (*exit)(void);
258
259         int (*get_device_list)(struct discovered_devs **discdevs);
260
261         int (*open)(struct libusb_device_handle *handle);
262         void (*close)(struct libusb_device_handle *handle);
263
264         int (*get_device_descriptor)(struct libusb_device *device,
265                 unsigned char *buffer);
266         int (*get_active_config_descriptor)(struct libusb_device *device,
267                 unsigned char *buffer, size_t len);
268         int (*get_config_descriptor)(struct libusb_device *device,
269                 uint8_t config_index, unsigned char *buffer, size_t len);
270
271         int (*set_configuration)(struct libusb_device_handle *handle, int config);
272
273         int (*claim_interface)(struct libusb_device_handle *handle, int iface);
274         int (*release_interface)(struct libusb_device_handle *handle, int iface);
275
276         int (*set_interface_altsetting)(struct libusb_device_handle *handle,
277                 int iface, int altsetting);
278         int (*clear_halt)(struct libusb_device_handle *handle,
279                 unsigned char endpoint);
280         int (*reset_device)(struct libusb_device_handle *handle);
281
282         /* optional */
283         int (*kernel_driver_active)(struct libusb_device_handle *handle,
284                 int interface);
285         int (*detach_kernel_driver)(struct libusb_device_handle *handle,
286                 int interface);
287
288         void (*destroy_device)(struct libusb_device *dev);
289
290         int (*submit_transfer)(struct usbi_transfer *itransfer);
291         int (*cancel_transfer)(struct usbi_transfer *itransfer);
292         void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
293
294         int (*handle_events)(struct pollfd *fds, nfds_t nfds, int num_ready);
295
296         /* number of bytes to reserve for libusb_device.os_priv */
297         size_t device_priv_size;
298
299         /* number of bytes to reserve for libusb_device_handle.os_priv */
300         size_t device_handle_priv_size;
301
302         /* number of bytes to reserve for usbi_transfer.os_priv */
303         size_t transfer_priv_size;
304
305         /* number of additional bytes for os_priv for each iso packet */
306         /* FIXME: linux can't use this any more. if other OS's cannot either,
307          * then remove this */
308         size_t add_iso_packet_size;
309 };
310
311 extern const struct usbi_os_backend * const usbi_backend;
312
313 extern const struct usbi_os_backend linux_usbfs_backend;
314
315 #endif
316