1/* $FreeBSD$ */
2/*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * This file describes internal structures.
31 */
32
33#ifndef _LIBUSB20_INT_H_
34#define	_LIBUSB20_INT_H_
35
36#ifdef COMPAT_32BIT
37#define	libusb20_pass_ptr(ptr)	((uint64_t)(uintptr_t)(ptr))
38#else
39#define	libusb20_pass_ptr(ptr)	(ptr)
40#endif
41
42struct libusb20_device;
43struct libusb20_backend;
44struct libusb20_transfer;
45struct libusb20_quirk;
46
47union libusb20_session_data {
48	unsigned long session_data;
49	struct timespec tv;
50	uint32_t plugtime;
51};
52
53/* USB backend specific */
54typedef const char *(libusb20_get_backend_name_t)(void);
55typedef int (libusb20_root_get_dev_quirk_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
56typedef int (libusb20_root_get_quirk_name_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
57typedef int (libusb20_root_add_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
58typedef int (libusb20_root_remove_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
59typedef int (libusb20_close_device_t)(struct libusb20_device *pdev);
60typedef int (libusb20_dev_get_info_t)(struct libusb20_device *pdev, struct usb_device_info *pinfo);
61typedef int (libusb20_dev_get_iface_desc_t)(struct libusb20_device *pdev, uint8_t iface_index, char *buf, uint8_t len);
62typedef int (libusb20_init_backend_t)(struct libusb20_backend *pbe);
63typedef int (libusb20_open_device_t)(struct libusb20_device *pdev, uint16_t transfer_count_max);
64typedef void (libusb20_exit_backend_t)(struct libusb20_backend *pbe);
65typedef int (libusb20_root_set_template_t)(struct libusb20_backend *pbe, int temp);
66typedef int (libusb20_root_get_template_t)(struct libusb20_backend *pbe, int *ptemp);
67
68#define	LIBUSB20_DEFINE(n,field) \
69  libusb20_##field##_t *field;
70
71#define	LIBUSB20_DECLARE(n,field) \
72  /* .field = */ n##_##field,
73
74#define	LIBUSB20_BACKEND(m,n) \
75  /* description of this backend */ \
76  m(n, get_backend_name) \
77  /* optional backend methods */ \
78  m(n, init_backend) \
79  m(n, exit_backend) \
80  m(n, dev_get_info) \
81  m(n, dev_get_iface_desc) \
82  m(n, root_get_dev_quirk) \
83  m(n, root_get_quirk_name) \
84  m(n, root_add_dev_quirk) \
85  m(n, root_remove_dev_quirk) \
86  m(n, root_set_template) \
87  m(n, root_get_template) \
88  /* mandatory device methods */ \
89  m(n, open_device) \
90  m(n, close_device) \
91
92struct libusb20_backend_methods {
93	LIBUSB20_BACKEND(LIBUSB20_DEFINE,)
94};
95
96/* USB dummy methods */
97typedef int (libusb20_dummy_int_t)(void);
98typedef void (libusb20_dummy_void_t)(void);
99
100/* USB device specific */
101typedef int (libusb20_detach_kernel_driver_t)(struct libusb20_device *pdev, uint8_t iface_index);
102typedef int (libusb20_do_request_sync_t)(struct libusb20_device *pdev, struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data, uint16_t *pactlen, uint32_t timeout, uint8_t flags);
103typedef int (libusb20_get_config_desc_full_t)(struct libusb20_device *pdev, uint8_t **ppbuf, uint16_t *plen, uint8_t index);
104typedef int (libusb20_get_config_index_t)(struct libusb20_device *pdev, uint8_t *pindex);
105typedef int (libusb20_kernel_driver_active_t)(struct libusb20_device *pdev, uint8_t iface_index);
106typedef int (libusb20_process_t)(struct libusb20_device *pdev);
107typedef int (libusb20_reset_device_t)(struct libusb20_device *pdev);
108typedef int (libusb20_set_power_mode_t)(struct libusb20_device *pdev, uint8_t power_mode);
109typedef int (libusb20_get_power_mode_t)(struct libusb20_device *pdev, uint8_t *power_mode);
110typedef int (libusb20_get_power_usage_t)(struct libusb20_device *pdev, uint16_t *power_usage);
111typedef int (libusb20_get_stats_t)(struct libusb20_device *pdev, struct libusb20_device_stats *pstats);
112typedef int (libusb20_set_alt_index_t)(struct libusb20_device *pdev, uint8_t iface_index, uint8_t alt_index);
113typedef int (libusb20_set_config_index_t)(struct libusb20_device *pdev, uint8_t index);
114typedef int (libusb20_check_connected_t)(struct libusb20_device *pdev);
115
116
117/* USB transfer specific */
118typedef int (libusb20_tr_open_t)(struct libusb20_transfer *xfer, uint32_t MaxBufSize, uint32_t MaxFrameCount, uint8_t ep_no, uint16_t stream_id, uint8_t pre_scale);
119typedef int (libusb20_tr_close_t)(struct libusb20_transfer *xfer);
120typedef int (libusb20_tr_clear_stall_sync_t)(struct libusb20_transfer *xfer);
121typedef void (libusb20_tr_submit_t)(struct libusb20_transfer *xfer);
122typedef void (libusb20_tr_cancel_async_t)(struct libusb20_transfer *xfer);
123
124#define	LIBUSB20_DEVICE(m,n) \
125  m(n, detach_kernel_driver) \
126  m(n, do_request_sync) \
127  m(n, get_config_desc_full) \
128  m(n, get_config_index) \
129  m(n, kernel_driver_active) \
130  m(n, process) \
131  m(n, reset_device) \
132  m(n, check_connected) \
133  m(n, set_power_mode) \
134  m(n, get_power_mode) \
135  m(n, get_power_usage) \
136  m(n, get_stats) \
137  m(n, set_alt_index) \
138  m(n, set_config_index) \
139  m(n, tr_cancel_async) \
140  m(n, tr_clear_stall_sync) \
141  m(n, tr_close) \
142  m(n, tr_open) \
143  m(n, tr_submit) \
144
145struct libusb20_device_methods {
146	LIBUSB20_DEVICE(LIBUSB20_DEFINE,)
147};
148
149struct libusb20_backend {
150	TAILQ_HEAD(, libusb20_device) usb_devs;
151	const struct libusb20_backend_methods *methods;
152};
153
154struct libusb20_transfer {
155	struct libusb20_device *pdev;	/* the USB device we belong to */
156	libusb20_tr_callback_t *callback;
157	void   *priv_sc0;		/* private client data */
158	void   *priv_sc1;		/* private client data */
159	/*
160	 * Pointer to a list of buffer pointers:
161	 */
162#ifdef COMPAT_32BIT
163	uint64_t *ppBuffer;
164#else
165	void  **ppBuffer;
166#endif
167	/*
168	 * Pointer to frame lengths, which are updated to actual length
169	 * after the USB transfer completes:
170	 */
171	uint32_t *pLength;
172	uint32_t maxTotalLength;
173	uint32_t maxFrames;		/* total number of frames */
174	uint32_t nFrames;		/* total number of frames */
175	uint32_t aFrames;		/* actual number of frames */
176	uint32_t timeout;
177	/* isochronous completion time in milliseconds */
178	uint16_t timeComplete;
179	uint16_t trIndex;
180	uint16_t maxPacketLen;
181	uint8_t	flags;			/* see LIBUSB20_TRANSFER_XXX */
182	uint8_t	status;			/* see LIBUSB20_TRANSFER_XXX */
183	uint8_t	is_opened;
184	uint8_t	is_pending;
185	uint8_t	is_cancel;
186	uint8_t	is_draining;
187	uint8_t	is_restart;
188};
189
190struct libusb20_device {
191
192	/* device descriptor */
193	struct LIBUSB20_DEVICE_DESC_DECODED ddesc;
194
195	/* device timestamp */
196	union libusb20_session_data session_data;
197
198	/* our device entry */
199	TAILQ_ENTRY(libusb20_device) dev_entry;
200
201	/* device methods */
202	const struct libusb20_device_methods *methods;
203
204	/* backend methods */
205	const struct libusb20_backend_methods *beMethods;
206
207	/* list of USB transfers */
208	struct libusb20_transfer *pTransfer;
209
210	/* private backend data */
211	void   *privBeData;
212
213	/* libUSB v0.1 and v1.0 compat data */
214	void   *privLuData;
215
216	/* claimed interface */
217	uint8_t claimed_interface;
218
219	/* auto detach kernel driver */
220	uint8_t auto_detach;
221
222	/* device file handle */
223	int	file;
224
225	/* device file handle (control transfers only) */
226	int	file_ctrl;
227
228	/* debugging level */
229	int	debug;
230
231	/* number of USB transfers */
232	uint16_t nTransfer;
233
234	uint8_t	bus_number;
235	uint8_t	device_address;
236	uint8_t	usb_mode;
237	uint8_t	usb_speed;
238	uint8_t	is_opened;
239	uint8_t parent_address;
240	uint8_t parent_port;
241	uint8_t port_level;
242
243	char	usb_desc[96];
244#define	LIBUSB20_DEVICE_PORT_PATH_MAX	32
245	uint8_t	port_path[LIBUSB20_DEVICE_PORT_PATH_MAX];
246};
247
248extern const struct libusb20_backend_methods libusb20_ugen20_backend;
249extern const struct libusb20_backend_methods libusb20_linux_backend;
250
251#endif					/* _LIBUSB20_INT_H_ */
252