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