libusb20_int.h revision 223495
1/* $FreeBSD: head/lib/libusb/libusb20_int.h 223495 2011-06-24 11:14:09Z hselasky $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. 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/*
28 * This file describes internal structures.
29 */
30
31#ifndef _LIBUSB20_INT_H_
32#define	_LIBUSB20_INT_H_
33
34#ifdef COMPAT_32BIT
35#define	libusb20_pass_ptr(ptr)	((uint64_t)(uintptr_t)(ptr))
36#else
37#define	libusb20_pass_ptr(ptr)	(ptr)
38#endif
39
40struct libusb20_device;
41struct libusb20_backend;
42struct libusb20_transfer;
43struct libusb20_quirk;
44
45union libusb20_session_data {
46	unsigned long session_data;
47	struct timespec tv;
48	uint32_t plugtime;
49};
50
51/* USB backend specific */
52typedef const char *(libusb20_get_backend_name_t)(void);
53typedef int (libusb20_root_get_dev_quirk_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
54typedef int (libusb20_root_get_quirk_name_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
55typedef int (libusb20_root_add_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
56typedef int (libusb20_root_remove_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
57typedef int (libusb20_close_device_t)(struct libusb20_device *pdev);
58typedef int (libusb20_dev_get_info_t)(struct libusb20_device *pdev, struct usb_device_info *pinfo);
59typedef int (libusb20_dev_get_iface_desc_t)(struct libusb20_device *pdev, uint8_t iface_index, char *buf, uint8_t len);
60typedef int (libusb20_init_backend_t)(struct libusb20_backend *pbe);
61typedef int (libusb20_open_device_t)(struct libusb20_device *pdev, uint16_t transfer_count_max);
62typedef void (libusb20_exit_backend_t)(struct libusb20_backend *pbe);
63typedef int (libusb20_root_set_template_t)(struct libusb20_backend *pbe, int temp);
64typedef int (libusb20_root_get_template_t)(struct libusb20_backend *pbe, int *ptemp);
65
66#define	LIBUSB20_DEFINE(n,field) \
67  libusb20_##field##_t *field;
68
69#define	LIBUSB20_DECLARE(n,field) \
70  /* .field = */ n##_##field,
71
72#define	LIBUSB20_BACKEND(m,n) \
73  /* description of this backend */ \
74  m(n, get_backend_name) \
75  /* optional backend methods */ \
76  m(n, init_backend) \
77  m(n, exit_backend) \
78  m(n, dev_get_info) \
79  m(n, dev_get_iface_desc) \
80  m(n, root_get_dev_quirk) \
81  m(n, root_get_quirk_name) \
82  m(n, root_add_dev_quirk) \
83  m(n, root_remove_dev_quirk) \
84  m(n, root_set_template) \
85  m(n, root_get_template) \
86  /* mandatory device methods */ \
87  m(n, open_device) \
88  m(n, close_device) \
89
90struct libusb20_backend_methods {
91	LIBUSB20_BACKEND(LIBUSB20_DEFINE,)
92};
93
94/* USB dummy methods */
95typedef int (libusb20_dummy_int_t)(void);
96typedef void (libusb20_dummy_void_t)(void);
97
98/* USB device specific */
99typedef int (libusb20_detach_kernel_driver_t)(struct libusb20_device *pdev, uint8_t iface_index);
100typedef 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);
101typedef int (libusb20_get_config_desc_full_t)(struct libusb20_device *pdev, uint8_t **ppbuf, uint16_t *plen, uint8_t index);
102typedef int (libusb20_get_config_index_t)(struct libusb20_device *pdev, uint8_t *pindex);
103typedef int (libusb20_kernel_driver_active_t)(struct libusb20_device *pdev, uint8_t iface_index);
104typedef int (libusb20_process_t)(struct libusb20_device *pdev);
105typedef int (libusb20_reset_device_t)(struct libusb20_device *pdev);
106typedef int (libusb20_set_power_mode_t)(struct libusb20_device *pdev, uint8_t power_mode);
107typedef int (libusb20_get_power_mode_t)(struct libusb20_device *pdev, uint8_t *power_mode);
108typedef int (libusb20_set_alt_index_t)(struct libusb20_device *pdev, uint8_t iface_index, uint8_t alt_index);
109typedef int (libusb20_set_config_index_t)(struct libusb20_device *pdev, uint8_t index);
110typedef int (libusb20_check_connected_t)(struct libusb20_device *pdev);
111
112/* USB transfer specific */
113typedef int (libusb20_tr_open_t)(struct libusb20_transfer *xfer, uint32_t MaxBufSize, uint32_t MaxFrameCount, uint8_t ep_no, uint8_t pre_scale);
114typedef int (libusb20_tr_close_t)(struct libusb20_transfer *xfer);
115typedef int (libusb20_tr_clear_stall_sync_t)(struct libusb20_transfer *xfer);
116typedef void (libusb20_tr_submit_t)(struct libusb20_transfer *xfer);
117typedef void (libusb20_tr_cancel_async_t)(struct libusb20_transfer *xfer);
118
119#define	LIBUSB20_DEVICE(m,n) \
120  m(n, detach_kernel_driver) \
121  m(n, do_request_sync) \
122  m(n, get_config_desc_full) \
123  m(n, get_config_index) \
124  m(n, kernel_driver_active) \
125  m(n, process) \
126  m(n, reset_device) \
127  m(n, check_connected) \
128  m(n, set_power_mode) \
129  m(n, get_power_mode) \
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#ifdef COMPAT_32BIT
156	uint64_t *ppBuffer;
157#else
158	void  **ppBuffer;
159#endif
160	/*
161	 * Pointer to frame lengths, which are updated to actual length
162	 * after the USB transfer completes:
163	 */
164	uint32_t *pLength;
165	uint32_t maxTotalLength;
166	uint32_t maxFrames;		/* total number of frames */
167	uint32_t nFrames;		/* total number of frames */
168	uint32_t aFrames;		/* actual number of frames */
169	uint32_t timeout;
170	/* isochronous completion time in milliseconds */
171	uint16_t timeComplete;
172	uint16_t trIndex;
173	uint16_t maxPacketLen;
174	uint8_t	flags;			/* see LIBUSB20_TRANSFER_XXX */
175	uint8_t	status;			/* see LIBUSB20_TRANSFER_XXX */
176	uint8_t	is_opened;
177	uint8_t	is_pending;
178	uint8_t	is_cancel;
179	uint8_t	is_draining;
180	uint8_t	is_restart;
181};
182
183struct libusb20_device {
184
185	/* device descriptor */
186	struct LIBUSB20_DEVICE_DESC_DECODED ddesc;
187
188	/* device timestamp */
189	union libusb20_session_data session_data;
190
191	/* our device entry */
192	TAILQ_ENTRY(libusb20_device) dev_entry;
193
194	/* device methods */
195	const struct libusb20_device_methods *methods;
196
197	/* backend methods */
198	const struct libusb20_backend_methods *beMethods;
199
200	/* list of USB transfers */
201	struct libusb20_transfer *pTransfer;
202
203	/* private backend data */
204	void   *privBeData;
205
206	/* libUSB v0.1 and v1.0 compat data */
207	void   *privLuData;
208
209	/* claimed interface */
210	uint8_t claimed_interface;
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
232	char	usb_desc[96];
233};
234
235extern const struct libusb20_backend_methods libusb20_ugen20_backend;
236extern const struct libusb20_backend_methods libusb20_linux_backend;
237
238#endif					/* _LIBUSB20_INT_H_ */
239