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