Compiler flags update
[platform/upstream/libusb.git] / libfpusb / fpusbi.h
1 /*
2  * Internal header for libfpusb
3  * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
4  *
5  * Portions based on libusb-0.1
6  * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #ifndef __FPUSBI_H__
24 #define __FPUSBI_H__
25
26 #include <config.h>
27
28 #include <endian.h>
29 #include <stddef.h>
30 #include <sys/ioctl.h>
31 #include <time.h>
32
33 #include <fpusb.h>
34 #include <usbfs.h>
35
36 #define USBFS_PATH "/dev/bus/usb"
37 #define DEVICE_DESC_LENGTH              18
38
39 #define USB_MAXENDPOINTS        32
40 #define USB_MAXINTERFACES       32
41 #define USB_MAXCONFIG           8
42
43 struct list_head {
44         struct list_head *prev, *next;
45 };
46
47 /* Get an entry from the list 
48  *      ptr - the address of this list_head element in "type" 
49  *      type - the data type that contains "member"
50  *      member - the list_head element in "type" 
51  */
52 #define list_entry(ptr, type, member) \
53         ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member)))
54
55 /* Get each entry from a list
56  *      pos - A structure pointer has a "member" element
57  *      head - list head
58  *      member - the list_head element in "pos"
59  */
60 #define list_for_each_entry(pos, head, member)                          \
61         for (pos = list_entry((head)->next, typeof(*pos), member);      \
62              &pos->member != (head);                                    \
63              pos = list_entry(pos->member.next, typeof(*pos), member))
64
65 #define list_for_each_entry_safe(pos, n, head, member)                  \
66         for (pos = list_entry((head)->next, typeof(*pos), member),      \
67                 n = list_entry(pos->member.next, typeof(*pos), member); \
68              &pos->member != (head);                                    \
69              pos = n, n = list_entry(n->member.next, typeof(*n), member))
70
71 #define list_empty(entry) ((entry)->next == (entry))
72
73 static inline void list_init(struct list_head *entry)
74 {
75         entry->prev = entry->next = entry;
76 }
77
78 static inline void list_add(struct list_head *entry, struct list_head *head)
79 {
80         entry->next = head->next;
81         entry->prev = head;
82
83         head->next->prev = entry;
84         head->next = entry;
85 }
86
87 static inline void list_add_tail(struct list_head *entry,
88         struct list_head *head)
89 {
90         entry->next = head;
91         entry->prev = head->prev;
92
93         head->prev->next = entry;
94         head->prev = entry;
95 }
96
97 static inline void list_del(struct list_head *entry)
98 {
99         entry->next->prev = entry->prev;
100         entry->prev->next = entry->next;
101 }
102
103 #define container_of(ptr, type, member) ({                      \
104         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
105         (type *)( (char *)__mptr - offsetof(type,member) );})
106
107 #define bswap16(x) (((x & 0xff) << 8) | (x >> 8))
108 #if __BYTE_ORDER == __LITTLE_ENDIAN
109 #define cpu_to_le16(x) (x)
110 #define le16_to_cpu(x) (x)
111 #elif __BYTE_ORDER == __BIG_ENDIAN
112 #define le16_to_cpu(x) bswap16(x)
113 #define cpu_to_le16(x) bswap16(x)
114 #else
115 #error "Unrecognized endianness"
116 #endif
117
118 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
119 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
120
121 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
122
123 enum fpi_log_level {
124         LOG_LEVEL_DEBUG,
125         LOG_LEVEL_INFO,
126         LOG_LEVEL_WARNING,
127         LOG_LEVEL_ERROR,
128 };
129
130 void fpi_log(enum fpi_log_level, const char *function, const char *format, ...);
131
132 #ifdef ENABLE_LOGGING
133 #define _fpi_log(level, fmt...) fpi_log(level, __FUNCTION__, fmt)
134 #else
135 #define _fpi_log(level, fmt...)
136 #endif
137
138 #ifdef ENABLE_DEBUG_LOGGING
139 #define fp_dbg(fmt...) _fpi_log(LOG_LEVEL_DEBUG, fmt)
140 #else
141 #define fp_dbg(fmt...)
142 #endif
143
144 #define fp_info(fmt...) _fpi_log(LOG_LEVEL_INFO, fmt)
145 #define fp_warn(fmt...) _fpi_log(LOG_LEVEL_WARNING, fmt)
146 #define fp_err(fmt...) _fpi_log(LOG_LEVEL_ERROR, fmt)
147
148 struct fpusb_dev {
149         struct list_head list;
150         char *nodepath;
151         struct usb_dev_descriptor desc;
152         struct usb_config_descriptor *config;
153 };
154
155 struct fpusb_dev_handle {
156         struct list_head list;
157         struct fpusb_dev *dev;
158         int fd;
159 };
160
161 enum fpusb_urb_type {
162         FPUSB_URB_CONTROL,
163         FPUSB_URB_BULK,
164 };
165
166 #define FPUSB_URBH_DATA_BELONGS_TO_USER         (1<<0)
167 #define FPUSB_URBH_SYNC_CANCELLED                       (1<<1)
168 #define FPUSB_URBH_TIMED_OUT                            (1<<2)
169
170 struct fpusb_urb_handle {
171         struct fpusb_dev_handle *devh;
172         struct usb_urb urb;
173         struct list_head list;
174         struct timespec timeout;
175         timer_t timer;
176         unsigned char urb_type;
177         unsigned char endpoint;
178         int transfer_len;
179         int transferred;
180         unsigned char *buffer;
181         void *callback;
182         void *user_data;
183         uint8_t flags;
184 };
185
186 /* bus structures */
187
188 /* All standard descriptors have these 2 fields in common */
189 struct usb_descriptor_header {
190         uint8_t  bLength;
191         uint8_t  bDescriptorType;
192 };
193
194 /* shared data and functions */
195
196 extern struct list_head open_devs;
197
198 int fpi_io_init(int _signum);
199 void fpi_io_exit(void);
200
201 int fpi_parse_descriptor(unsigned char *source, char *descriptor, void *dest);
202 int fpi_parse_configuration(struct usb_config_descriptor *config,
203                 unsigned char *buffer);
204
205 #endif
206