1194676Sthompsa/* $FreeBSD$ */
2194676Sthompsa/*-
3194676Sthompsa * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4194676Sthompsa *
5194676Sthompsa * Redistribution and use in source and binary forms, with or without
6194676Sthompsa * modification, are permitted provided that the following conditions
7194676Sthompsa * are met:
8194676Sthompsa * 1. Redistributions of source code must retain the above copyright
9194676Sthompsa *    notice, this list of conditions and the following disclaimer.
10194676Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
11194676Sthompsa *    notice, this list of conditions and the following disclaimer in the
12194676Sthompsa *    documentation and/or other materials provided with the distribution.
13194676Sthompsa *
14194676Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15194676Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16194676Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17194676Sthompsa * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18194676Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19194676Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20194676Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21194676Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22194676Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23194676Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24194676Sthompsa * SUCH DAMAGE.
25194676Sthompsa */
26194676Sthompsa
27194676Sthompsa#ifndef __LIBUSB10_H__
28195957Salfred#define	__LIBUSB10_H__
29194676Sthompsa
30248236Shselasky#ifndef LIBUSB_GLOBAL_INCLUDE_FILE
31195957Salfred#include <sys/queue.h>
32248236Shselasky#endif
33194676Sthompsa
34195957Salfred#define	GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
35195957Salfred#define	UNEXPORTED __attribute__((__visibility__("hidden")))
36195957Salfred#define	CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock)
37195957Salfred#define	CTX_TRYLOCK(ctx) pthread_mutex_trylock(&(ctx)->ctx_lock)
38195957Salfred#define	CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock)
39194676Sthompsa
40195957Salfred#define	DPRINTF(ctx, dbg, format, args...) do {	\
41195957Salfred    if ((ctx)->debug == dbg) {			\
42195957Salfred	switch (dbg) {				\
43195957Salfred	case LIBUSB_DEBUG_FUNCTION:		\
44195957Salfred		printf("LIBUSB_FUNCTION: "	\
45195957Salfred		    format "\n", ## args);	\
46195957Salfred		break;				\
47195957Salfred	case LIBUSB_DEBUG_TRANSFER:		\
48195957Salfred		printf("LIBUSB_TRANSFER: "	\
49195957Salfred		    format "\n", ## args);	\
50195957Salfred		break;				\
51195957Salfred	default:				\
52195957Salfred		break;				\
53195957Salfred	}					\
54195957Salfred    }						\
55195957Salfred} while(0)
56194676Sthompsa
57195957Salfred/* internal structures */
58194676Sthompsa
59195957Salfredstruct libusb_super_pollfd {
60195957Salfred	TAILQ_ENTRY(libusb_super_pollfd) entry;
61195957Salfred	struct libusb20_device *pdev;
62195957Salfred	struct libusb_pollfd pollfd;
63195957Salfred};
64194676Sthompsa
65195957Salfredstruct libusb_super_transfer {
66195957Salfred	TAILQ_ENTRY(libusb_super_transfer) entry;
67195957Salfred	uint8_t *curr_data;
68195957Salfred	uint32_t rem_len;
69195957Salfred	uint32_t last_len;
70199575Sthompsa	uint8_t	state;
71199575Sthompsa#define	LIBUSB_SUPER_XFER_ST_NONE 0
72199575Sthompsa#define	LIBUSB_SUPER_XFER_ST_PEND 1
73195957Salfred};
74194676Sthompsa
75195957Salfredstruct libusb_context {
76195957Salfred	int	debug;
77195957Salfred	int	debug_fixed;
78195957Salfred	int	ctrl_pipe[2];
79195957Salfred	int	tr_done_ref;
80195957Salfred	int	tr_done_gen;
81194676Sthompsa
82195957Salfred	pthread_mutex_t ctx_lock;
83195957Salfred	pthread_cond_t ctx_cond;
84195957Salfred	pthread_t ctx_handler;
85195957Salfred#define	NO_THREAD ((pthread_t)-1)
86195957Salfred
87195957Salfred	TAILQ_HEAD(, libusb_super_pollfd) pollfds;
88195957Salfred	TAILQ_HEAD(, libusb_super_transfer) tr_done;
89195957Salfred
90195957Salfred	struct libusb_super_pollfd ctx_poll;
91195957Salfred
92195957Salfred	libusb_pollfd_added_cb fd_added_cb;
93195957Salfred	libusb_pollfd_removed_cb fd_removed_cb;
94195957Salfred	void   *fd_cb_user_data;
95195957Salfred};
96195957Salfred
97195957Salfredstruct libusb_device {
98195957Salfred	int	refcnt;
99195957Salfred
100195957Salfred	uint32_t claimed_interfaces;
101195957Salfred
102195957Salfred	struct libusb_super_pollfd dev_poll;
103195957Salfred
104195957Salfred	struct libusb_context *ctx;
105195957Salfred
106195957Salfred	TAILQ_HEAD(, libusb_super_transfer) tr_head;
107195957Salfred
108195957Salfred	struct libusb20_device *os_priv;
109195957Salfred};
110195957Salfred
111195957Salfredextern struct libusb_context *usbi_default_context;
112195957Salfred
113195957Salfredvoid	libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
114195957Salfredvoid	libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
115195957Salfredvoid	libusb10_cancel_all_transfer(libusb_device *dev);
116195957Salfred
117195957Salfred#endif					/* __LIBUSB10_H__ */
118