libusb20_int.h revision 195957
1827SN/A/* $FreeBSD: head/lib/libusb/libusb20_int.h 195957 2009-07-30 00:11:41Z alfred $ */
29330SN/A/*-
3827SN/A * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4827SN/A *
5827SN/A * Redistribution and use in source and binary forms, with or without
6827SN/A * modification, are permitted provided that the following conditions
72362SN/A * are met:
8827SN/A * 1. Redistributions of source code must retain the above copyright
92362SN/A *    notice, this list of conditions and the following disclaimer.
10827SN/A * 2. Redistributions in binary form must reproduce the above copyright
11827SN/A *    notice, this list of conditions and the following disclaimer in the
12827SN/A *    documentation and/or other materials provided with the distribution.
13827SN/A *
14827SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15827SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16827SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17827SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18827SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19827SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20827SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212362SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222362SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232362SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24827SN/A * SUCH DAMAGE.
25827SN/A */
26827SN/A
27827SN/A/*
28827SN/A * This file describes internal structures.
29827SN/A */
30827SN/A
31827SN/A#ifndef _LIBUSB20_INT_H_
32827SN/A#define	_LIBUSB20_INT_H_
33827SN/A
34827SN/Astruct libusb20_device;
35827SN/Astruct libusb20_backend;
368565SN/Astruct libusb20_transfer;
37827SN/Astruct libusb20_quirk;
38827SN/A
39827SN/Aunion libusb20_session_data {
40827SN/A	unsigned long session_data;
41827SN/A	struct timespec tv;
42827SN/A	uint32_t plugtime;
43827SN/A};
44827SN/A
45827SN/A/* USB backend specific */
46827SN/Atypedef const char *(libusb20_get_backend_name_t)(void);
47827SN/Atypedef int (libusb20_root_get_dev_quirk_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
48827SN/Atypedef int (libusb20_root_get_quirk_name_t)(struct libusb20_backend *pbe, uint16_t index, struct libusb20_quirk *pq);
49827SN/Atypedef int (libusb20_root_add_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
50827SN/Atypedef int (libusb20_root_remove_dev_quirk_t)(struct libusb20_backend *pbe, struct libusb20_quirk *pq);
51827SN/Atypedef int (libusb20_close_device_t)(struct libusb20_device *pdev);
52827SN/Atypedef int (libusb20_dev_get_info_t)(struct libusb20_device *pdev, struct usb_device_info *pinfo);
53827SN/Atypedef int (libusb20_dev_get_iface_desc_t)(struct libusb20_device *pdev, uint8_t iface_index, char *buf, uint8_t len);
54827SN/Atypedef int (libusb20_init_backend_t)(struct libusb20_backend *pbe);
558565SN/Atypedef int (libusb20_open_device_t)(struct libusb20_device *pdev, uint16_t transfer_count_max);
56827SN/Atypedef void (libusb20_exit_backend_t)(struct libusb20_backend *pbe);
57827SN/Atypedef int (libusb20_root_set_template_t)(struct libusb20_backend *pbe, int temp);
58827SN/Atypedef int (libusb20_root_get_template_t)(struct libusb20_backend *pbe, int *ptemp);
59827SN/A
60827SN/A#define	LIBUSB20_DEFINE(n,field) \
61827SN/A  libusb20_##field##_t *field;
62827SN/A
63827SN/A#define	LIBUSB20_DECLARE(n,field) \
64827SN/A  /* .field = */ n##_##field,
65827SN/A
66827SN/A#define	LIBUSB20_BACKEND(m,n) \
67827SN/A  /* description of this backend */ \
68827SN/A  m(n, get_backend_name) \
69827SN/A  /* optional backend methods */ \
70827SN/A  m(n, init_backend) \
71827SN/A  m(n, exit_backend) \
72827SN/A  m(n, dev_get_info) \
73827SN/A  m(n, dev_get_iface_desc) \
74827SN/A  m(n, root_get_dev_quirk) \
75827SN/A  m(n, root_get_quirk_name) \
76827SN/A  m(n, root_add_dev_quirk) \
77827SN/A  m(n, root_remove_dev_quirk) \
78827SN/A  m(n, root_set_template) \
79827SN/A  m(n, root_get_template) \
80827SN/A  /* mandatory device methods */ \
81827SN/A  m(n, open_device) \
82827SN/A  m(n, close_device) \
83827SN/A
84827SN/Astruct libusb20_backend_methods {
85827SN/A	LIBUSB20_BACKEND(LIBUSB20_DEFINE,)
86827SN/A};
87827SN/A
88827SN/A/* USB dummy methods */
89827SN/Atypedef int (libusb20_dummy_int_t)(void);
90827SN/Atypedef void (libusb20_dummy_void_t)(void);
91827SN/A
92827SN/A/* USB device specific */
93typedef int (libusb20_detach_kernel_driver_t)(struct libusb20_device *pdev, uint8_t iface_index);
94typedef 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);
95typedef int (libusb20_get_config_desc_full_t)(struct libusb20_device *pdev, uint8_t **ppbuf, uint16_t *plen, uint8_t index);
96typedef int (libusb20_get_config_index_t)(struct libusb20_device *pdev, uint8_t *pindex);
97typedef int (libusb20_kernel_driver_active_t)(struct libusb20_device *pdev, uint8_t iface_index);
98typedef int (libusb20_process_t)(struct libusb20_device *pdev);
99typedef int (libusb20_reset_device_t)(struct libusb20_device *pdev);
100typedef int (libusb20_set_power_mode_t)(struct libusb20_device *pdev, uint8_t power_mode);
101typedef int (libusb20_get_power_mode_t)(struct libusb20_device *pdev, uint8_t *power_mode);
102typedef int (libusb20_set_alt_index_t)(struct libusb20_device *pdev, uint8_t iface_index, uint8_t alt_index);
103typedef int (libusb20_set_config_index_t)(struct libusb20_device *pdev, uint8_t index);
104
105/* USB transfer specific */
106typedef int (libusb20_tr_open_t)(struct libusb20_transfer *xfer, uint32_t MaxBufSize, uint32_t MaxFrameCount, uint8_t ep_no);
107typedef int (libusb20_tr_close_t)(struct libusb20_transfer *xfer);
108typedef int (libusb20_tr_clear_stall_sync_t)(struct libusb20_transfer *xfer);
109typedef void (libusb20_tr_submit_t)(struct libusb20_transfer *xfer);
110typedef void (libusb20_tr_cancel_async_t)(struct libusb20_transfer *xfer);
111
112#define	LIBUSB20_DEVICE(m,n) \
113  m(n, detach_kernel_driver) \
114  m(n, do_request_sync) \
115  m(n, get_config_desc_full) \
116  m(n, get_config_index) \
117  m(n, kernel_driver_active) \
118  m(n, process) \
119  m(n, reset_device) \
120  m(n, set_power_mode) \
121  m(n, get_power_mode) \
122  m(n, set_alt_index) \
123  m(n, set_config_index) \
124  m(n, tr_cancel_async) \
125  m(n, tr_clear_stall_sync) \
126  m(n, tr_close) \
127  m(n, tr_open) \
128  m(n, tr_submit) \
129
130struct libusb20_device_methods {
131	LIBUSB20_DEVICE(LIBUSB20_DEFINE,)
132};
133
134struct libusb20_backend {
135	TAILQ_HEAD(, libusb20_device) usb_devs;
136	const struct libusb20_backend_methods *methods;
137};
138
139struct libusb20_transfer {
140	struct libusb20_device *pdev;	/* the USB device we belong to */
141	libusb20_tr_callback_t *callback;
142	void   *priv_sc0;		/* private client data */
143	void   *priv_sc1;		/* private client data */
144	/*
145	 * Pointer to a list of buffer pointers:
146	 */
147	void  **ppBuffer;
148	/*
149	 * Pointer to frame lengths, which are updated to actual length
150	 * after the USB transfer completes:
151	 */
152	uint32_t *pLength;
153	uint32_t maxTotalLength;
154	uint32_t maxFrames;		/* total number of frames */
155	uint32_t nFrames;		/* total number of frames */
156	uint32_t aFrames;		/* actual number of frames */
157	uint32_t timeout;
158	/* isochronous completion time in milliseconds */
159	uint16_t timeComplete;
160	uint16_t trIndex;
161	uint16_t maxPacketLen;
162	uint8_t	flags;			/* see LIBUSB20_TRANSFER_XXX */
163	uint8_t	status;			/* see LIBUSB20_TRANSFER_XXX */
164	uint8_t	is_opened;
165	uint8_t	is_pending;
166	uint8_t	is_cancel;
167	uint8_t	is_draining;
168	uint8_t	is_restart;
169};
170
171struct libusb20_device {
172
173	/* device descriptor */
174	struct LIBUSB20_DEVICE_DESC_DECODED ddesc;
175
176	/* device timestamp */
177	union libusb20_session_data session_data;
178
179	/* our device entry */
180	TAILQ_ENTRY(libusb20_device) dev_entry;
181
182	/* device methods */
183	const struct libusb20_device_methods *methods;
184
185	/* backend methods */
186	const struct libusb20_backend_methods *beMethods;
187
188	/* list of USB transfers */
189	struct libusb20_transfer *pTransfer;
190
191	/* private backend data */
192	void   *privBeData;
193
194	/* libUSB v0.1 and v1.0 compat data */
195	void   *privLuData;
196
197	/* claimed interface */
198	uint8_t claimed_interface;
199
200	/* device file handle */
201	int	file;
202
203	/* device file handle (control transfers only) */
204	int	file_ctrl;
205
206	/* debugging level */
207	int	debug;
208
209	/* number of USB transfers */
210	uint16_t nTransfer;
211
212	uint8_t	bus_number;
213	uint8_t	device_address;
214	uint8_t	usb_mode;
215	uint8_t	usb_speed;
216	uint8_t	is_opened;
217
218	char	usb_desc[96];
219};
220
221extern const struct libusb20_backend_methods libusb20_ugen20_backend;
222extern const struct libusb20_backend_methods libusb20_linux_backend;
223
224#endif					/* _LIBUSB20_INT_H_ */
225