1194676Sthompsa/* $FreeBSD: stable/10/lib/libusb/libusb10.h 349669 2019-07-03 18:26:07Z hselasky $ */
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)
39302275Shselasky#define	HOTPLUG_LOCK(ctx) pthread_mutex_lock(&(ctx)->hotplug_lock)
40302275Shselasky#define	HOTPLUG_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->hotplug_lock)
41194676Sthompsa
42338792Shselasky#define	DPRINTF(ctx, dbg, format, ...) do {			\
43338792Shselasky	switch (dbg) {						\
44338792Shselasky	case LIBUSB_DEBUG_FUNCTION:				\
45338792Shselasky		if ((ctx)->debug & LIBUSB_DEBUG_FUNCTION) {	\
46338792Shselasky			printf("LIBUSB_FUNCTION: "		\
47338792Shselasky			       format "\n", ## __VA_ARGS__);	\
48338792Shselasky		}						\
49338792Shselasky		break;						\
50338792Shselasky	case LIBUSB_DEBUG_TRANSFER:				\
51338792Shselasky		if ((ctx)->debug & LIBUSB_DEBUG_TRANSFER) { 	\
52338792Shselasky			printf("LIBUSB_TRANSFER: "		\
53338792Shselasky			       format "\n", ## __VA_ARGS__);	\
54338792Shselasky		}						\
55338792Shselasky		break;						\
56338792Shselasky	default:						\
57338792Shselasky		break;						\
58338792Shselasky	}							\
59338792Shselasky} while (0)
60194676Sthompsa
61195957Salfred/* internal structures */
62194676Sthompsa
63195957Salfredstruct libusb_super_pollfd {
64195957Salfred	TAILQ_ENTRY(libusb_super_pollfd) entry;
65195957Salfred	struct libusb20_device *pdev;
66195957Salfred	struct libusb_pollfd pollfd;
67195957Salfred};
68194676Sthompsa
69195957Salfredstruct libusb_super_transfer {
70195957Salfred	TAILQ_ENTRY(libusb_super_transfer) entry;
71195957Salfred	uint8_t *curr_data;
72195957Salfred	uint32_t rem_len;
73195957Salfred	uint32_t last_len;
74302275Shselasky	uint32_t stream_id;
75199575Sthompsa	uint8_t	state;
76199575Sthompsa#define	LIBUSB_SUPER_XFER_ST_NONE 0
77199575Sthompsa#define	LIBUSB_SUPER_XFER_ST_PEND 1
78195957Salfred};
79194676Sthompsa
80302275Shselaskystruct libusb_hotplug_callback_handle_struct {
81302275Shselasky	TAILQ_ENTRY(libusb_hotplug_callback_handle_struct) entry;
82302275Shselasky	int events;
83302275Shselasky	int vendor;
84302275Shselasky	int product;
85302275Shselasky	int devclass;
86302275Shselasky	libusb_hotplug_callback_fn fn;
87302275Shselasky	void *user_data;
88302275Shselasky};
89302275Shselasky
90349669ShselaskyTAILQ_HEAD(libusb_device_head, libusb_device);
91349669Shselasky
92195957Salfredstruct libusb_context {
93195957Salfred	int	debug;
94195957Salfred	int	debug_fixed;
95195957Salfred	int	ctrl_pipe[2];
96195957Salfred	int	tr_done_ref;
97195957Salfred	int	tr_done_gen;
98194676Sthompsa
99195957Salfred	pthread_mutex_t ctx_lock;
100302275Shselasky  	pthread_mutex_t hotplug_lock;
101195957Salfred	pthread_cond_t ctx_cond;
102302275Shselasky	pthread_t hotplug_handler;
103195957Salfred	pthread_t ctx_handler;
104195957Salfred#define	NO_THREAD ((pthread_t)-1)
105195957Salfred
106195957Salfred	TAILQ_HEAD(, libusb_super_pollfd) pollfds;
107195957Salfred	TAILQ_HEAD(, libusb_super_transfer) tr_done;
108302275Shselasky	TAILQ_HEAD(, libusb_hotplug_callback_handle_struct) hotplug_cbh;
109349669Shselasky	struct libusb_device_head hotplug_devs;
110195957Salfred
111195957Salfred	struct libusb_super_pollfd ctx_poll;
112195957Salfred
113195957Salfred	libusb_pollfd_added_cb fd_added_cb;
114195957Salfred	libusb_pollfd_removed_cb fd_removed_cb;
115195957Salfred	void   *fd_cb_user_data;
116195957Salfred};
117195957Salfred
118195957Salfredstruct libusb_device {
119195957Salfred	int	refcnt;
120195957Salfred
121338789Shselasky	int	device_is_gone;
122338789Shselasky
123195957Salfred	uint32_t claimed_interfaces;
124195957Salfred
125195957Salfred	struct libusb_super_pollfd dev_poll;
126195957Salfred
127195957Salfred	struct libusb_context *ctx;
128195957Salfred
129302275Shselasky	TAILQ_ENTRY(libusb_device) hotplug_entry;
130302275Shselasky
131195957Salfred	TAILQ_HEAD(, libusb_super_transfer) tr_head;
132195957Salfred
133195957Salfred	struct libusb20_device *os_priv;
134195957Salfred};
135195957Salfred
136195957Salfredextern struct libusb_context *usbi_default_context;
137195957Salfred
138195957Salfredvoid	libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
139195957Salfredvoid	libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
140195957Salfredvoid	libusb10_cancel_all_transfer(libusb_device *dev);
141338789Shselaskyvoid	libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev);
142195957Salfred
143195957Salfred#endif					/* __LIBUSB10_H__ */
144