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
38#define	DPRINTF(ctx, dbg, format, args...) do {	\
39    if ((ctx)->debug == dbg) {			\
40	switch (dbg) {				\
41	case LIBUSB_DEBUG_FUNCTION:		\
42		printf("LIBUSB_FUNCTION: "	\
43		    format "\n", ## args);	\
44		break;				\
45	case LIBUSB_DEBUG_TRANSFER:		\
46		printf("LIBUSB_TRANSFER: "	\
47		    format "\n", ## args);	\
48		break;				\
49	default:				\
50		break;				\
51	}					\
52    }						\
53} while(0)
54
55/* internal structures */
56
57struct libusb_super_pollfd {
58	TAILQ_ENTRY(libusb_super_pollfd) entry;
59	struct libusb20_device *pdev;
60	struct libusb_pollfd pollfd;
61};
62
63struct libusb_super_transfer {
64	TAILQ_ENTRY(libusb_super_transfer) entry;
65	uint8_t *curr_data;
66	uint32_t rem_len;
67	uint32_t last_len;
68	uint8_t	state;
69#define	LIBUSB_SUPER_XFER_ST_NONE 0
70#define	LIBUSB_SUPER_XFER_ST_PEND 1
71};
72
73struct libusb_context {
74	int	debug;
75	int	debug_fixed;
76	int	ctrl_pipe[2];
77	int	tr_done_ref;
78	int	tr_done_gen;
79
80	pthread_mutex_t ctx_lock;
81	pthread_cond_t ctx_cond;
82	pthread_t ctx_handler;
83#define	NO_THREAD ((pthread_t)-1)
84
85	TAILQ_HEAD(, libusb_super_pollfd) pollfds;
86	TAILQ_HEAD(, libusb_super_transfer) tr_done;
87
88	struct libusb_super_pollfd ctx_poll;
89
90	libusb_pollfd_added_cb fd_added_cb;
91	libusb_pollfd_removed_cb fd_removed_cb;
92	void   *fd_cb_user_data;
93};
94
95struct libusb_device {
96	int	refcnt;
97
98	uint32_t claimed_interfaces;
99
100	struct libusb_super_pollfd dev_poll;
101
102	struct libusb_context *ctx;
103
104	TAILQ_HEAD(, libusb_super_transfer) tr_head;
105
106	struct libusb20_device *os_priv;
107};
108
109extern struct libusb_context *usbi_default_context;
110
111void	libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
112void	libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
113void	libusb10_cancel_all_transfer(libusb_device *dev);
114
115#endif					/* __LIBUSB10_H__ */
116