]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - lib/libusb/libusb10.h
MFC r338679:
[FreeBSD/stable/9.git] / lib / libusb / libusb10.h
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #ifndef __LIBUSB10_H__
28 #define __LIBUSB10_H__
29
30 #include <sys/queue.h>
31
32 #define GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
33 #define UNEXPORTED __attribute__((__visibility__("hidden")))
34 #define CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock)
35 #define CTX_TRYLOCK(ctx) pthread_mutex_trylock(&(ctx)->ctx_lock)
36 #define CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock)
37 #define HOTPLUG_LOCK(ctx) pthread_mutex_lock(&(ctx)->hotplug_lock)
38 #define HOTPLUG_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->hotplug_lock)
39
40 #define DPRINTF(ctx, dbg, format, ...) do {                     \
41         switch (dbg) {                                          \
42         case LIBUSB_DEBUG_FUNCTION:                             \
43                 if ((ctx)->debug & LIBUSB_DEBUG_FUNCTION) {     \
44                         printf("LIBUSB_FUNCTION: "              \
45                                format "\n", ## __VA_ARGS__);    \
46                 }                                               \
47                 break;                                          \
48         case LIBUSB_DEBUG_TRANSFER:                             \
49                 if ((ctx)->debug & LIBUSB_DEBUG_TRANSFER) {     \
50                         printf("LIBUSB_TRANSFER: "              \
51                                format "\n", ## __VA_ARGS__);    \
52                 }                                               \
53                 break;                                          \
54         default:                                                \
55                 break;                                          \
56         }                                                       \
57 } while (0)
58
59 /* internal structures */
60
61 struct libusb_super_pollfd {
62         TAILQ_ENTRY(libusb_super_pollfd) entry;
63         struct libusb20_device *pdev;
64         struct libusb_pollfd pollfd;
65 };
66
67 struct libusb_super_transfer {
68         TAILQ_ENTRY(libusb_super_transfer) entry;
69         uint8_t *curr_data;
70         uint32_t rem_len;
71         uint32_t last_len;
72         uint32_t stream_id;
73         uint8_t state;
74 #define LIBUSB_SUPER_XFER_ST_NONE 0
75 #define LIBUSB_SUPER_XFER_ST_PEND 1
76 };
77
78 struct libusb_hotplug_callback_handle_struct {
79         TAILQ_ENTRY(libusb_hotplug_callback_handle_struct) entry;
80         int events;
81         int vendor;
82         int product;
83         int devclass;
84         libusb_hotplug_callback_fn fn;
85         void *user_data;
86 };
87
88 struct libusb_context {
89         int     debug;
90         int     debug_fixed;
91         int     ctrl_pipe[2];
92         int     tr_done_ref;
93         int     tr_done_gen;
94
95         pthread_mutex_t ctx_lock;
96         pthread_mutex_t hotplug_lock;
97         pthread_cond_t ctx_cond;
98         pthread_t hotplug_handler;
99         pthread_t ctx_handler;
100 #define NO_THREAD ((pthread_t)-1)
101
102         TAILQ_HEAD(, libusb_super_pollfd) pollfds;
103         TAILQ_HEAD(, libusb_super_transfer) tr_done;
104         TAILQ_HEAD(, libusb_hotplug_callback_handle_struct) hotplug_cbh;
105         TAILQ_HEAD(, libusb_device) hotplug_devs;
106
107         struct libusb_super_pollfd ctx_poll;
108
109         libusb_pollfd_added_cb fd_added_cb;
110         libusb_pollfd_removed_cb fd_removed_cb;
111         void   *fd_cb_user_data;
112 };
113
114 struct libusb_device {
115         int     refcnt;
116
117         int     device_is_gone;
118
119         uint32_t claimed_interfaces;
120
121         struct libusb_super_pollfd dev_poll;
122
123         struct libusb_context *ctx;
124
125         TAILQ_ENTRY(libusb_device) hotplug_entry;
126
127         TAILQ_HEAD(, libusb_super_transfer) tr_head;
128
129         struct libusb20_device *os_priv;
130 };
131
132 extern struct libusb_context *usbi_default_context;
133
134 void    libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
135 void    libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
136 void    libusb10_cancel_all_transfer(libusb_device *dev);
137 void    libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev);
138
139 #endif                                  /* __LIBUSB10_H__ */