2 * Internal header for libusb
3 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
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.
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.
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
28 #include <sys/ioctl.h>
34 #define DEVICE_DESC_LENGTH 18
36 #define USB_MAXENDPOINTS 32
37 #define USB_MAXINTERFACES 32
38 #define USB_MAXCONFIG 8
41 struct list_head *prev, *next;
44 /* Get an entry from the list
45 * ptr - the address of this list_head element in "type"
46 * type - the data type that contains "member"
47 * member - the list_head element in "type"
49 #define list_entry(ptr, type, member) \
50 ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member)))
52 /* Get each entry from a list
53 * pos - A structure pointer has a "member" element
55 * member - the list_head element in "pos"
57 #define list_for_each_entry(pos, head, member) \
58 for (pos = list_entry((head)->next, typeof(*pos), member); \
59 &pos->member != (head); \
60 pos = list_entry(pos->member.next, typeof(*pos), member))
62 #define list_for_each_entry_safe(pos, n, head, member) \
63 for (pos = list_entry((head)->next, typeof(*pos), member), \
64 n = list_entry(pos->member.next, typeof(*pos), member); \
65 &pos->member != (head); \
66 pos = n, n = list_entry(n->member.next, typeof(*n), member))
68 #define list_empty(entry) ((entry)->next == (entry))
70 static inline void list_init(struct list_head *entry)
72 entry->prev = entry->next = entry;
75 static inline void list_add(struct list_head *entry, struct list_head *head)
77 entry->next = head->next;
80 head->next->prev = entry;
84 static inline void list_add_tail(struct list_head *entry,
85 struct list_head *head)
88 entry->prev = head->prev;
90 head->prev->next = entry;
94 static inline void list_del(struct list_head *entry)
96 entry->next->prev = entry->prev;
97 entry->prev->next = entry->next;
100 #define container_of(ptr, type, member) ({ \
101 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
102 (type *)( (char *)__mptr - offsetof(type,member) );})
104 #define bswap16(x) (((x & 0xff) << 8) | (x >> 8))
105 #if __BYTE_ORDER == __LITTLE_ENDIAN
106 #define cpu_to_le16(x) (x)
107 #define le16_to_cpu(x) (x)
108 #elif __BYTE_ORDER == __BIG_ENDIAN
109 #define le16_to_cpu(x) bswap16(x)
110 #define cpu_to_le16(x) bswap16(x)
112 #error "Unrecognized endianness"
115 #define MIN(a, b) ((a) < (b) ? (a) : (b))
116 #define MAX(a, b) ((a) > (b) ? (a) : (b))
118 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
120 enum usbi_log_level {
127 void usbi_log(enum usbi_log_level, const char *function, const char *format, ...);
129 #ifdef ENABLE_LOGGING
130 #define _usbi_log(level, fmt...) usbi_log(level, __FUNCTION__, fmt)
132 #define _usbi_log(level, fmt...)
135 #ifdef ENABLE_DEBUG_LOGGING
136 #define usbi_dbg(fmt...) _usbi_log(LOG_LEVEL_DEBUG, fmt)
138 #define usbi_dbg(fmt...)
141 #define usbi_info(fmt...) _usbi_log(LOG_LEVEL_INFO, fmt)
142 #define usbi_warn(fmt...) _usbi_log(LOG_LEVEL_WARNING, fmt)
143 #define usbi_err(fmt...) _usbi_log(LOG_LEVEL_ERROR, fmt)
145 struct libusb_device {
146 struct list_head list;
148 unsigned long session_data;
150 struct libusb_device_descriptor desc;
151 struct libusb_config_descriptor *config;
154 struct libusb_device_handle {
155 struct list_head list;
156 struct libusb_device *dev;
160 #define USBI_TRANSFER_SYNC_CANCELLED (1<<0)
161 #define USBI_TRANSFER_TIMED_OUT (1<<1)
163 struct usbi_transfer {
164 /* must come first */
165 struct libusb_transfer pub;
168 struct list_head list;
169 struct timeval timeout;
176 /* All standard descriptors have these 2 fields in common */
177 struct usb_descriptor_header {
179 uint8_t bDescriptorType;
182 /* shared data and functions */
184 extern struct list_head open_devs;
186 void usbi_io_init(void);
187 void usbi_add_pollfd(int fd, short events);
188 void usbi_remove_pollfd(int fd);
190 int usbi_parse_descriptor(unsigned char *source, char *descriptor, void *dest);
191 int usbi_parse_configuration(struct libusb_config_descriptor *config,
192 unsigned char *buffer);