Rename libusb_dev_handle to libusb_device_handle
[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/ioctl.h>
29 #include <time.h>
30
31 #include <libusb.h>
32 #include <usbfs.h>
33
34 #define DEVICE_DESC_LENGTH              18
35
36 #define USB_MAXENDPOINTS        32
37 #define USB_MAXINTERFACES       32
38 #define USB_MAXCONFIG           8
39
40 struct list_head {
41         struct list_head *prev, *next;
42 };
43
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" 
48  */
49 #define list_entry(ptr, type, member) \
50         ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member)))
51
52 /* Get each entry from a list
53  *      pos - A structure pointer has a "member" element
54  *      head - list head
55  *      member - the list_head element in "pos"
56  */
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))
61
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))
67
68 #define list_empty(entry) ((entry)->next == (entry))
69
70 static inline void list_init(struct list_head *entry)
71 {
72         entry->prev = entry->next = entry;
73 }
74
75 static inline void list_add(struct list_head *entry, struct list_head *head)
76 {
77         entry->next = head->next;
78         entry->prev = head;
79
80         head->next->prev = entry;
81         head->next = entry;
82 }
83
84 static inline void list_add_tail(struct list_head *entry,
85         struct list_head *head)
86 {
87         entry->next = head;
88         entry->prev = head->prev;
89
90         head->prev->next = entry;
91         head->prev = entry;
92 }
93
94 static inline void list_del(struct list_head *entry)
95 {
96         entry->next->prev = entry->prev;
97         entry->prev->next = entry->next;
98 }
99
100 #define container_of(ptr, type, member) ({                      \
101         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
102         (type *)( (char *)__mptr - offsetof(type,member) );})
103
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)
111 #else
112 #error "Unrecognized endianness"
113 #endif
114
115 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
116 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
117
118 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
119
120 enum usbi_log_level {
121         LOG_LEVEL_DEBUG,
122         LOG_LEVEL_INFO,
123         LOG_LEVEL_WARNING,
124         LOG_LEVEL_ERROR,
125 };
126
127 void usbi_log(enum usbi_log_level, const char *function, const char *format, ...);
128
129 #ifdef ENABLE_LOGGING
130 #define _usbi_log(level, fmt...) usbi_log(level, __FUNCTION__, fmt)
131 #else
132 #define _usbi_log(level, fmt...)
133 #endif
134
135 #ifdef ENABLE_DEBUG_LOGGING
136 #define usbi_dbg(fmt...) _usbi_log(LOG_LEVEL_DEBUG, fmt)
137 #else
138 #define usbi_dbg(fmt...)
139 #endif
140
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)
144
145 struct libusb_device {
146         struct list_head list;
147         int refcnt;
148         unsigned long session_data;
149         char *nodepath;
150         struct libusb_dev_descriptor desc;
151         struct libusb_config_descriptor *config;
152 };
153
154 struct libusb_device_handle {
155         struct list_head list;
156         struct libusb_device *dev;
157         int fd;
158 };
159
160 #define USBI_TRANSFER_SYNC_CANCELLED            (1<<0)
161 #define USBI_TRANSFER_TIMED_OUT                         (1<<1)
162
163 struct usbi_transfer {
164         /* must come first */
165         struct libusb_transfer pub;
166
167         struct usb_urb urb;
168         struct list_head list;
169         struct timeval timeout;
170         int transferred;
171         uint8_t flags;
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 open_devs;
185
186 void usbi_io_init(void);
187 void usbi_add_pollfd(int fd, short events);
188 void usbi_remove_pollfd(int fd);
189
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);
193
194 #endif
195