1/* $FreeBSD: stable/11/lib/libusb/libusb10.h 349668 2019-07-03 18:25:05Z hselasky $ */
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#ifndef LIBUSB_GLOBAL_INCLUDE_FILE
31#include <sys/queue.h>
32#endif
33
34#define	GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
35#define	UNEXPORTED __attribute__((__visibility__("hidden")))
36#define	CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock)
37#define	CTX_TRYLOCK(ctx) pthread_mutex_trylock(&(ctx)->ctx_lock)
38#define	CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock)
39#define	HOTPLUG_LOCK(ctx) pthread_mutex_lock(&(ctx)->hotplug_lock)
40#define	HOTPLUG_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->hotplug_lock)
41
42#define	DPRINTF(ctx, dbg, format, ...) do {			\
43	switch (dbg) {						\
44	case LIBUSB_DEBUG_FUNCTION:				\
45		if ((ctx)->debug & LIBUSB_DEBUG_FUNCTION) {	\
46			printf("LIBUSB_FUNCTION: "		\
47			       format "\n", ## __VA_ARGS__);	\
48		}						\
49		break;						\
50	case LIBUSB_DEBUG_TRANSFER:				\
51		if ((ctx)->debug & LIBUSB_DEBUG_TRANSFER) { 	\
52			printf("LIBUSB_TRANSFER: "		\
53			       format "\n", ## __VA_ARGS__);	\
54		}						\
55		break;						\
56	default:						\
57		break;						\
58	}							\
59} while (0)
60
61/* internal structures */
62
63struct libusb_super_pollfd {
64	TAILQ_ENTRY(libusb_super_pollfd) entry;
65	struct libusb20_device *pdev;
66	struct libusb_pollfd pollfd;
67};
68
69struct libusb_super_transfer {
70	TAILQ_ENTRY(libusb_super_transfer) entry;
71	uint8_t *curr_data;
72	uint32_t rem_len;
73	uint32_t last_len;
74	uint32_t stream_id;
75	uint8_t	state;
76#define	LIBUSB_SUPER_XFER_ST_NONE 0
77#define	LIBUSB_SUPER_XFER_ST_PEND 1
78};
79
80struct libusb_hotplug_callback_handle_struct {
81	TAILQ_ENTRY(libusb_hotplug_callback_handle_struct) entry;
82	int events;
83	int vendor;
84	int product;
85	int devclass;
86	libusb_hotplug_callback_fn fn;
87	void *user_data;
88};
89
90TAILQ_HEAD(libusb_device_head, libusb_device);
91
92struct libusb_context {
93	int	debug;
94	int	debug_fixed;
95	int	ctrl_pipe[2];
96	int	tr_done_ref;
97	int	tr_done_gen;
98
99	pthread_mutex_t ctx_lock;
100  	pthread_mutex_t hotplug_lock;
101	pthread_cond_t ctx_cond;
102	pthread_t hotplug_handler;
103	pthread_t ctx_handler;
104#define	NO_THREAD ((pthread_t)-1)
105
106	TAILQ_HEAD(, libusb_super_pollfd) pollfds;
107	TAILQ_HEAD(, libusb_super_transfer) tr_done;
108	TAILQ_HEAD(, libusb_hotplug_callback_handle_struct) hotplug_cbh;
109	struct libusb_device_head hotplug_devs;
110
111	struct libusb_super_pollfd ctx_poll;
112
113	libusb_pollfd_added_cb fd_added_cb;
114	libusb_pollfd_removed_cb fd_removed_cb;
115	void   *fd_cb_user_data;
116};
117
118struct libusb_device {
119	int	refcnt;
120
121	int	device_is_gone;
122
123	uint32_t claimed_interfaces;
124
125	struct libusb_super_pollfd dev_poll;
126
127	struct libusb_context *ctx;
128
129	TAILQ_ENTRY(libusb_device) hotplug_entry;
130
131	TAILQ_HEAD(, libusb_super_transfer) tr_head;
132
133	struct libusb20_device *os_priv;
134};
135
136extern struct libusb_context *usbi_default_context;
137
138void	libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
139void	libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
140void	libusb10_cancel_all_transfer(libusb_device *dev);
141void	libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev);
142
143#endif					/* __LIBUSB10_H__ */
144