2 * Internal header for libfpusb
3 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
5 * Portions based on libusb-0.1
6 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
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.
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.
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
30 #include <sys/ioctl.h>
36 #define USBFS_PATH "/dev/bus/usb"
37 #define DEVICE_DESC_LENGTH 18
39 #define USB_MAXENDPOINTS 32
40 #define USB_MAXINTERFACES 32
41 #define USB_MAXCONFIG 8
44 struct list_head *prev, *next;
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"
52 #define list_entry(ptr, type, member) \
53 ((type *)((char *)(ptr) - (unsigned long)(&((type *)0L)->member)))
55 /* Get each entry from a list
56 * pos - A structure pointer has a "member" element
58 * member - the list_head element in "pos"
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))
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))
71 #define list_empty(entry) ((entry)->next == (entry))
73 static inline void list_init(struct list_head *entry)
75 entry->prev = entry->next = entry;
78 static inline void list_add(struct list_head *entry, struct list_head *head)
80 entry->next = head->next;
83 head->next->prev = entry;
87 static inline void list_add_tail(struct list_head *entry,
88 struct list_head *head)
91 entry->prev = head->prev;
93 head->prev->next = entry;
97 static inline void list_del(struct list_head *entry)
99 entry->next->prev = entry->prev;
100 entry->prev->next = entry->next;
103 #define container_of(ptr, type, member) ({ \
104 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
105 (type *)( (char *)__mptr - offsetof(type,member) );})
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)
115 #error "Unrecognized endianness"
118 #define MIN(a, b) ((a) < (b) ? (a) : (b))
119 #define MAX(a, b) ((a) > (b) ? (a) : (b))
121 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
130 void fpi_log(enum fpi_log_level, const char *function, const char *format, ...);
132 #ifdef ENABLE_LOGGING
133 #define _fpi_log(level, fmt...) fpi_log(level, __FUNCTION__, fmt)
135 #define _fpi_log(level, fmt...)
138 #ifdef ENABLE_DEBUG_LOGGING
139 #define fp_dbg(fmt...) _fpi_log(LOG_LEVEL_DEBUG, fmt)
141 #define fp_dbg(fmt...)
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)
149 struct list_head list;
151 struct usb_dev_descriptor desc;
152 struct usb_config_descriptor *config;
155 struct fpusb_dev_handle {
156 struct list_head list;
157 struct fpusb_dev *dev;
161 enum fpusb_urb_type {
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)
170 struct fpusb_urb_handle {
171 struct fpusb_dev_handle *devh;
173 struct list_head list;
174 struct timespec timeout;
176 unsigned char urb_type;
177 unsigned char endpoint;
180 unsigned char *buffer;
188 /* All standard descriptors have these 2 fields in common */
189 struct usb_descriptor_header {
191 uint8_t bDescriptorType;
194 /* shared data and functions */
196 extern struct list_head open_devs;
198 int fpi_io_init(int _signum);
199 void fpi_io_exit(void);
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);