xref: /freebsd/lib/libusb/libusb20_int.h (revision 9768746b)
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 struct libusb20_device;
37 struct libusb20_backend;
38 struct libusb20_transfer;
39 struct libusb20_quirk;
40 
41 union libusb20_session_data {
42 	unsigned long session_data;
43 	struct timespec tv;
44 	uint32_t plugtime;
45 };
46 
47 /* USB backend specific */
48 typedef const char *(libusb20_get_backend_name_t)(void);
49 typedef int (libusb20_root_get_dev_quirk_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
50 typedef int (libusb20_root_get_quirk_name_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
51 typedef int (libusb20_root_add_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
52 typedef int (libusb20_root_remove_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
53 typedef int (libusb20_close_device_t)(struct libusb20_device *pdev);
54 typedef int (libusb20_dev_get_info_t)(struct libusb20_device *pdev, struct usb_device_info *pinfo);
55 typedef int (libusb20_dev_get_iface_desc_t)(struct libusb20_device *pdev, uint8_t iface_index, char *buf, uint8_t len);
56 typedef int (libusb20_init_backend_t)(struct libusb20_backend *pbe);
57 typedef int (libusb20_open_device_t)(struct libusb20_device *pdev, uint16_t transfer_count_max);
58 typedef void (libusb20_exit_backend_t)(struct libusb20_backend *pbe);
59 typedef int (libusb20_root_set_template_t)(struct libusb20_backend *pbe, int temp);
60 typedef int (libusb20_root_get_template_t)(struct libusb20_backend *pbe, int *ptemp);
61 
62 #define	LIBUSB20_DEFINE(n,field) \
63   libusb20_##field##_t *field;
64 
65 #define	LIBUSB20_DECLARE(n,field) \
66   /* .field = */ n##_##field,
67 
68 #define	LIBUSB20_BACKEND(m,n) \
69   /* description of this backend */ \
70   m(n, get_backend_name) \
71   /* optional backend methods */ \
72   m(n, init_backend) \
73   m(n, exit_backend) \
74   m(n, dev_get_info) \
75   m(n, dev_get_iface_desc) \
76   m(n, root_get_dev_quirk) \
77   m(n, root_get_quirk_name) \
78   m(n, root_add_dev_quirk) \
79   m(n, root_remove_dev_quirk) \
80   m(n, root_set_template) \
81   m(n, root_get_template) \
82   /* mandatory device methods */ \
83   m(n, open_device) \
84   m(n, close_device) \
85 
86 struct libusb20_backend_methods {
87 	LIBUSB20_BACKEND(LIBUSB20_DEFINE,)
88 };
89 
90 /* USB dummy methods */
91 typedef int (libusb20_dummy_int_t)(void);
92 typedef void (libusb20_dummy_void_t)(void);
93 
94 /* USB device specific */
95 typedef int (libusb20_detach_kernel_driver_t)(struct libusb20_device *pdev, uint8_t iface_index);
96 typedef 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);
97 typedef int (libusb20_get_config_desc_full_t)(struct libusb20_device *pdev, uint8_t **ppbuf, uint16_t *plen, uint8_t index);
98 typedef int (libusb20_get_config_index_t)(struct libusb20_device *pdev, uint8_t *pindex);
99 typedef int (libusb20_kernel_driver_active_t)(struct libusb20_device *pdev, uint8_t iface_index);
100 typedef int (libusb20_process_t)(struct libusb20_device *pdev);
101 typedef int (libusb20_reset_device_t)(struct libusb20_device *pdev);
102 typedef int (libusb20_set_power_mode_t)(struct libusb20_device *pdev, uint8_t power_mode);
103 typedef int (libusb20_get_power_mode_t)(struct libusb20_device *pdev, uint8_t *power_mode);
104 typedef int (libusb20_get_power_usage_t)(struct libusb20_device *pdev, uint16_t *power_usage);
105 typedef int (libusb20_get_stats_t)(struct libusb20_device *pdev, struct libusb20_device_stats *pstats);
106 typedef int (libusb20_set_alt_index_t)(struct libusb20_device *pdev, uint8_t iface_index, uint8_t alt_index);
107 typedef int (libusb20_set_config_index_t)(struct libusb20_device *pdev, uint8_t index);
108 typedef int (libusb20_check_connected_t)(struct libusb20_device *pdev);
109 
110 
111 /* USB transfer specific */
112 typedef 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);
113 typedef int (libusb20_tr_close_t)(struct libusb20_transfer *xfer);
114 typedef int (libusb20_tr_clear_stall_sync_t)(struct libusb20_transfer *xfer);
115 typedef void (libusb20_tr_submit_t)(struct libusb20_transfer *xfer);
116 typedef void (libusb20_tr_cancel_async_t)(struct libusb20_transfer *xfer);
117 
118 #define	LIBUSB20_DEVICE(m,n) \
119   m(n, detach_kernel_driver) \
120   m(n, do_request_sync) \
121   m(n, get_config_desc_full) \
122   m(n, get_config_index) \
123   m(n, kernel_driver_active) \
124   m(n, process) \
125   m(n, reset_device) \
126   m(n, check_connected) \
127   m(n, set_power_mode) \
128   m(n, get_power_mode) \
129   m(n, get_power_usage) \
130   m(n, get_stats) \
131   m(n, set_alt_index) \
132   m(n, set_config_index) \
133   m(n, tr_cancel_async) \
134   m(n, tr_clear_stall_sync) \
135   m(n, tr_close) \
136   m(n, tr_open) \
137   m(n, tr_submit) \
138 
139 struct libusb20_device_methods {
140 	LIBUSB20_DEVICE(LIBUSB20_DEFINE,)
141 };
142 
143 struct libusb20_backend {
144 	TAILQ_HEAD(, libusb20_device) usb_devs;
145 	const struct libusb20_backend_methods *methods;
146 };
147 
148 struct libusb20_transfer {
149 	struct libusb20_device *pdev;	/* the USB device we belong to */
150 	libusb20_tr_callback_t *callback;
151 	void   *priv_sc0;		/* private client data */
152 	void   *priv_sc1;		/* private client data */
153 	/*
154 	 * Pointer to a list of buffer pointers:
155 	 */
156 	void  **ppBuffer;
157 	/*
158 	 * Pointer to frame lengths, which are updated to actual length
159 	 * after the USB transfer completes:
160 	 */
161 	uint32_t *pLength;
162 	uint32_t maxTotalLength;
163 	uint32_t maxFrames;		/* total number of frames */
164 	uint32_t nFrames;		/* total number of frames */
165 	uint32_t aFrames;		/* actual number of frames */
166 	uint32_t timeout;
167 	/* isochronous completion time in milliseconds */
168 	uint16_t timeComplete;
169 	uint16_t trIndex;
170 	uint16_t maxPacketLen;
171 	uint8_t	flags;			/* see LIBUSB20_TRANSFER_XXX */
172 	uint8_t	status;			/* see LIBUSB20_TRANSFER_XXX */
173 	uint8_t	is_opened;
174 	uint8_t	is_pending;
175 	uint8_t	is_cancel;
176 	uint8_t	is_draining;
177 	uint8_t	is_restart;
178 };
179 
180 struct libusb20_device {
181 
182 	/* device descriptor */
183 	struct LIBUSB20_DEVICE_DESC_DECODED ddesc;
184 
185 	/* device timestamp */
186 	union libusb20_session_data session_data;
187 
188 	/* our device entry */
189 	TAILQ_ENTRY(libusb20_device) dev_entry;
190 
191 	/* device methods */
192 	const struct libusb20_device_methods *methods;
193 
194 	/* backend methods */
195 	const struct libusb20_backend_methods *beMethods;
196 
197 	/* list of USB transfers */
198 	struct libusb20_transfer *pTransfer;
199 
200 	/* private backend data */
201 	void   *privBeData;
202 
203 	/* libUSB v0.1 and v1.0 compat data */
204 	void   *privLuData;
205 
206 	/* claimed interface */
207 	uint8_t claimed_interface;
208 
209 	/* auto detach kernel driver */
210 	uint8_t auto_detach;
211 
212 	/* device file handle */
213 	int	file;
214 
215 	/* device file handle (control transfers only) */
216 	int	file_ctrl;
217 
218 	/* debugging level */
219 	int	debug;
220 
221 	/* number of USB transfers */
222 	uint16_t nTransfer;
223 
224 	uint8_t	bus_number;
225 	uint8_t	device_address;
226 	uint8_t	usb_mode;
227 	uint8_t	usb_speed;
228 	uint8_t	is_opened;
229 	uint8_t parent_address;
230 	uint8_t parent_port;
231 	uint8_t port_level;
232 
233 	char	usb_desc[96];
234 #define	LIBUSB20_DEVICE_PORT_PATH_MAX	32
235 	uint8_t	port_path[LIBUSB20_DEVICE_PORT_PATH_MAX];
236 };
237 
238 extern const struct libusb20_backend_methods libusb20_ugen20_backend;
239 extern const struct libusb20_backend_methods libusb20_linux_backend;
240 
241 #endif					/* _LIBUSB20_INT_H_ */
242