Notifications for changes to the fd set
[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 USBFS_PATH "/dev/bus/usb"
35 #define DEVICE_DESC_LENGTH              18
36
37 #define USB_MAXENDPOINTS        32
38 #define USB_MAXINTERFACES       32
39 #define USB_MAXCONFIG           8
40
41 struct list_head {
42         struct list_head *prev, *next;
43 };
44
45 /* Get an entry from the list 
46  *      ptr - the address of this list_head element in "type" 
47  *      type - the data type that contains "member"
48  *      member - the list_head element in "type" 
49  */
50 #define list_entry(ptr, type, member) \
51         ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member)))
52
53 /* Get each entry from a list
54  *      pos - A structure pointer has a "member" element
55  *      head - list head
56  *      member - the list_head element in "pos"
57  */
58 #define list_for_each_entry(pos, head, member)                          \
59         for (pos = list_entry((head)->next, typeof(*pos), member);      \
60              &pos->member != (head);                                    \
61              pos = list_entry(pos->member.next, typeof(*pos), member))
62
63 #define list_for_each_entry_safe(pos, n, head, member)                  \
64         for (pos = list_entry((head)->next, typeof(*pos), member),      \
65                 n = list_entry(pos->member.next, typeof(*pos), member); \
66              &pos->member != (head);                                    \
67              pos = n, n = list_entry(n->member.next, typeof(*n), member))
68
69 #define list_empty(entry) ((entry)->next == (entry))
70
71 static inline void list_init(struct list_head *entry)
72 {
73         entry->prev = entry->next = entry;
74 }
75
76 static inline void list_add(struct list_head *entry, struct list_head *head)
77 {
78         entry->next = head->next;
79         entry->prev = head;
80
81         head->next->prev = entry;
82         head->next = entry;
83 }
84
85 static inline void list_add_tail(struct list_head *entry,
86         struct list_head *head)
87 {
88         entry->next = head;
89         entry->prev = head->prev;
90
91         head->prev->next = entry;
92         head->prev = entry;
93 }
94
95 static inline void list_del(struct list_head *entry)
96 {
97         entry->next->prev = entry->prev;
98         entry->prev->next = entry->next;
99 }
100
101 #define container_of(ptr, type, member) ({                      \
102         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
103         (type *)( (char *)__mptr - offsetof(type,member) );})
104
105 #define bswap16(x) (((x & 0xff) << 8) | (x >> 8))
106 #if __BYTE_ORDER == __LITTLE_ENDIAN
107 #define cpu_to_le16(x) (x)
108 #define le16_to_cpu(x) (x)
109 #elif __BYTE_ORDER == __BIG_ENDIAN
110 #define le16_to_cpu(x) bswap16(x)
111 #define cpu_to_le16(x) bswap16(x)
112 #else
113 #error "Unrecognized endianness"
114 #endif
115
116 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
117 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
118
119 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
120
121 enum usbi_log_level {
122         LOG_LEVEL_DEBUG,
123         LOG_LEVEL_INFO,
124         LOG_LEVEL_WARNING,
125         LOG_LEVEL_ERROR,
126 };
127
128 void usbi_log(enum usbi_log_level, const char *function, const char *format, ...);
129
130 #ifdef ENABLE_LOGGING
131 #define _usbi_log(level, fmt...) usbi_log(level, __FUNCTION__, fmt)
132 #else
133 #define _usbi_log(level, fmt...)
134 #endif
135
136 #ifdef ENABLE_DEBUG_LOGGING
137 #define usbi_dbg(fmt...) _usbi_log(LOG_LEVEL_DEBUG, fmt)
138 #else
139 #define usbi_dbg(fmt...)
140 #endif
141
142 #define usbi_info(fmt...) _usbi_log(LOG_LEVEL_INFO, fmt)
143 #define usbi_warn(fmt...) _usbi_log(LOG_LEVEL_WARNING, fmt)
144 #define usbi_err(fmt...) _usbi_log(LOG_LEVEL_ERROR, fmt)
145
146 struct libusb_dev {
147         struct list_head list;
148         char *nodepath;
149         struct libusb_dev_descriptor desc;
150         struct libusb_config_descriptor *config;
151 };
152
153 struct libusb_dev_handle {
154         struct list_head list;
155         struct libusb_dev *dev;
156         int fd;
157 };
158
159 enum libusb_urb_type {
160         LIBUSB_URB_CONTROL,
161         LIBUSB_URB_BULK,
162 };
163
164 #define LIBUSB_URBH_DATA_BELONGS_TO_USER                (1<<0)
165 #define LIBUSB_URBH_SYNC_CANCELLED                      (1<<1)
166 #define LIBUSB_URBH_TIMED_OUT                           (1<<2)
167
168 struct libusb_urb_handle {
169         struct libusb_dev_handle *devh;
170         struct usb_urb urb;
171         struct list_head list;
172         struct timeval timeout;
173         unsigned char urb_type;
174         unsigned char endpoint;
175         int transfer_len;
176         int transferred;
177         unsigned char *buffer;
178         void *callback;
179         void *user_data;
180         uint8_t flags;
181 };
182
183 /* bus structures */
184
185 /* All standard descriptors have these 2 fields in common */
186 struct usb_descriptor_header {
187         uint8_t  bLength;
188         uint8_t  bDescriptorType;
189 };
190
191 /* shared data and functions */
192
193 extern struct list_head open_devs;
194
195 void usbi_io_init(void);
196 void usbi_add_pollfd(int fd, short events);
197 void usbi_remove_pollfd(int fd);
198
199 int usbi_parse_descriptor(unsigned char *source, char *descriptor, void *dest);
200 int usbi_parse_configuration(struct libusb_config_descriptor *config,
201                 unsigned char *buffer);
202
203 #endif
204