xref: /freebsd/lib/libusb/libusb20_ugen20.c (revision e50ac68b3dc725aa4b428d2446bb58fe4f5c288c)
1df4b8c2aSAndrew Thompson /* $FreeBSD$ */
2df4b8c2aSAndrew Thompson /*-
3df4b8c2aSAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4df4b8c2aSAndrew Thompson  *
5df4b8c2aSAndrew Thompson  * Redistribution and use in source and binary forms, with or without
6df4b8c2aSAndrew Thompson  * modification, are permitted provided that the following conditions
7df4b8c2aSAndrew Thompson  * are met:
8df4b8c2aSAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
9df4b8c2aSAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
10df4b8c2aSAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
11df4b8c2aSAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
12df4b8c2aSAndrew Thompson  *    documentation and/or other materials provided with the distribution.
13df4b8c2aSAndrew Thompson  *
14df4b8c2aSAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15df4b8c2aSAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16df4b8c2aSAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17df4b8c2aSAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18df4b8c2aSAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19df4b8c2aSAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20df4b8c2aSAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21df4b8c2aSAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22df4b8c2aSAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23df4b8c2aSAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24df4b8c2aSAndrew Thompson  * SUCH DAMAGE.
25df4b8c2aSAndrew Thompson  */
26df4b8c2aSAndrew Thompson 
27df4b8c2aSAndrew Thompson #include <sys/queue.h>
28df4b8c2aSAndrew Thompson #include <sys/types.h>
29df4b8c2aSAndrew Thompson 
30df4b8c2aSAndrew Thompson #include <stdio.h>
31df4b8c2aSAndrew Thompson #include <stdlib.h>
32df4b8c2aSAndrew Thompson #include <unistd.h>
33df4b8c2aSAndrew Thompson #include <string.h>
34df4b8c2aSAndrew Thompson #include <poll.h>
35df4b8c2aSAndrew Thompson #include <fcntl.h>
36df4b8c2aSAndrew Thompson #include <errno.h>
37df4b8c2aSAndrew Thompson 
38df4b8c2aSAndrew Thompson #include "libusb20.h"
39df4b8c2aSAndrew Thompson #include "libusb20_desc.h"
40df4b8c2aSAndrew Thompson #include "libusb20_int.h"
41df4b8c2aSAndrew Thompson 
42df4b8c2aSAndrew Thompson #include <dev/usb/usb.h>
43ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
44df4b8c2aSAndrew Thompson #include <dev/usb/usb_ioctl.h>
45df4b8c2aSAndrew Thompson 
46df4b8c2aSAndrew Thompson static libusb20_init_backend_t ugen20_init_backend;
47df4b8c2aSAndrew Thompson static libusb20_open_device_t ugen20_open_device;
48df4b8c2aSAndrew Thompson static libusb20_close_device_t ugen20_close_device;
49df4b8c2aSAndrew Thompson static libusb20_get_backend_name_t ugen20_get_backend_name;
50df4b8c2aSAndrew Thompson static libusb20_exit_backend_t ugen20_exit_backend;
51df4b8c2aSAndrew Thompson static libusb20_dev_get_iface_desc_t ugen20_dev_get_iface_desc;
52df4b8c2aSAndrew Thompson static libusb20_dev_get_info_t ugen20_dev_get_info;
53df4b8c2aSAndrew Thompson static libusb20_root_get_dev_quirk_t ugen20_root_get_dev_quirk;
54df4b8c2aSAndrew Thompson static libusb20_root_get_quirk_name_t ugen20_root_get_quirk_name;
55df4b8c2aSAndrew Thompson static libusb20_root_add_dev_quirk_t ugen20_root_add_dev_quirk;
56df4b8c2aSAndrew Thompson static libusb20_root_remove_dev_quirk_t ugen20_root_remove_dev_quirk;
57df4b8c2aSAndrew Thompson static libusb20_root_set_template_t ugen20_root_set_template;
58df4b8c2aSAndrew Thompson static libusb20_root_get_template_t ugen20_root_get_template;
59df4b8c2aSAndrew Thompson 
60df4b8c2aSAndrew Thompson const struct libusb20_backend_methods libusb20_ugen20_backend = {
61df4b8c2aSAndrew Thompson 	LIBUSB20_BACKEND(LIBUSB20_DECLARE, ugen20)
62df4b8c2aSAndrew Thompson };
63df4b8c2aSAndrew Thompson 
64df4b8c2aSAndrew Thompson /* USB device specific */
65df4b8c2aSAndrew Thompson static libusb20_get_config_desc_full_t ugen20_get_config_desc_full;
66df4b8c2aSAndrew Thompson static libusb20_get_config_index_t ugen20_get_config_index;
67df4b8c2aSAndrew Thompson static libusb20_set_config_index_t ugen20_set_config_index;
68df4b8c2aSAndrew Thompson static libusb20_set_alt_index_t ugen20_set_alt_index;
69df4b8c2aSAndrew Thompson static libusb20_reset_device_t ugen20_reset_device;
70e50ac68bSAndrew Thompson static libusb20_check_connected_t ugen20_check_connected;
71df4b8c2aSAndrew Thompson static libusb20_set_power_mode_t ugen20_set_power_mode;
72df4b8c2aSAndrew Thompson static libusb20_get_power_mode_t ugen20_get_power_mode;
73df4b8c2aSAndrew Thompson static libusb20_kernel_driver_active_t ugen20_kernel_driver_active;
74df4b8c2aSAndrew Thompson static libusb20_detach_kernel_driver_t ugen20_detach_kernel_driver;
75df4b8c2aSAndrew Thompson static libusb20_do_request_sync_t ugen20_do_request_sync;
76df4b8c2aSAndrew Thompson static libusb20_process_t ugen20_process;
77df4b8c2aSAndrew Thompson 
78df4b8c2aSAndrew Thompson /* USB transfer specific */
79df4b8c2aSAndrew Thompson static libusb20_tr_open_t ugen20_tr_open;
80df4b8c2aSAndrew Thompson static libusb20_tr_close_t ugen20_tr_close;
81df4b8c2aSAndrew Thompson static libusb20_tr_clear_stall_sync_t ugen20_tr_clear_stall_sync;
82df4b8c2aSAndrew Thompson static libusb20_tr_submit_t ugen20_tr_submit;
83df4b8c2aSAndrew Thompson static libusb20_tr_cancel_async_t ugen20_tr_cancel_async;
84df4b8c2aSAndrew Thompson 
85df4b8c2aSAndrew Thompson static const struct libusb20_device_methods libusb20_ugen20_device_methods = {
86df4b8c2aSAndrew Thompson 	LIBUSB20_DEVICE(LIBUSB20_DECLARE, ugen20)
87df4b8c2aSAndrew Thompson };
88df4b8c2aSAndrew Thompson 
89df4b8c2aSAndrew Thompson static const char *
90df4b8c2aSAndrew Thompson ugen20_get_backend_name(void)
91df4b8c2aSAndrew Thompson {
92df4b8c2aSAndrew Thompson 	return ("FreeBSD UGEN 2.0");
93df4b8c2aSAndrew Thompson }
94df4b8c2aSAndrew Thompson 
95df4b8c2aSAndrew Thompson static uint32_t
96df4b8c2aSAndrew Thompson ugen20_path_convert_one(const char **pp)
97df4b8c2aSAndrew Thompson {
98df4b8c2aSAndrew Thompson 	const char *ptr;
99df4b8c2aSAndrew Thompson 	uint32_t temp = 0;
100df4b8c2aSAndrew Thompson 
101df4b8c2aSAndrew Thompson 	ptr = *pp;
102df4b8c2aSAndrew Thompson 
103df4b8c2aSAndrew Thompson 	while ((*ptr >= '0') && (*ptr <= '9')) {
104df4b8c2aSAndrew Thompson 		temp *= 10;
105df4b8c2aSAndrew Thompson 		temp += (*ptr - '0');
106df4b8c2aSAndrew Thompson 		if (temp >= 1000000) {
107df4b8c2aSAndrew Thompson 			/* catch overflow early */
108df4b8c2aSAndrew Thompson 			return (0 - 1);
109df4b8c2aSAndrew Thompson 		}
110df4b8c2aSAndrew Thompson 		ptr++;
111df4b8c2aSAndrew Thompson 	}
112df4b8c2aSAndrew Thompson 
113df4b8c2aSAndrew Thompson 	if (*ptr == '.') {
114df4b8c2aSAndrew Thompson 		/* skip dot */
115df4b8c2aSAndrew Thompson 		ptr++;
116df4b8c2aSAndrew Thompson 	}
117df4b8c2aSAndrew Thompson 	*pp = ptr;
118df4b8c2aSAndrew Thompson 
119df4b8c2aSAndrew Thompson 	return (temp);
120df4b8c2aSAndrew Thompson }
121df4b8c2aSAndrew Thompson 
122df4b8c2aSAndrew Thompson static int
123df4b8c2aSAndrew Thompson ugen20_enumerate(struct libusb20_device *pdev, const char *id)
124df4b8c2aSAndrew Thompson {
125df4b8c2aSAndrew Thompson 	const char *tmp = id;
126760bc48eSAndrew Thompson 	struct usb_device_descriptor ddesc;
127760bc48eSAndrew Thompson 	struct usb_device_info devinfo;
128df4b8c2aSAndrew Thompson 	uint32_t plugtime;
129df4b8c2aSAndrew Thompson 	char buf[64];
130df4b8c2aSAndrew Thompson 	int f;
131df4b8c2aSAndrew Thompson 	int error;
132df4b8c2aSAndrew Thompson 
133df4b8c2aSAndrew Thompson 	pdev->bus_number = ugen20_path_convert_one(&tmp);
134df4b8c2aSAndrew Thompson 	pdev->device_address = ugen20_path_convert_one(&tmp);
135df4b8c2aSAndrew Thompson 
136df4b8c2aSAndrew Thompson 	snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u",
137df4b8c2aSAndrew Thompson 	    pdev->bus_number, pdev->device_address);
138df4b8c2aSAndrew Thompson 
139df4b8c2aSAndrew Thompson 	f = open(buf, O_RDWR);
140df4b8c2aSAndrew Thompson 	if (f < 0) {
141df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
142df4b8c2aSAndrew Thompson 	}
143df4b8c2aSAndrew Thompson 	if (ioctl(f, USB_GET_PLUGTIME, &plugtime)) {
144df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
145df4b8c2aSAndrew Thompson 		goto done;
146df4b8c2aSAndrew Thompson 	}
147df4b8c2aSAndrew Thompson 	/* store when the device was plugged */
148df4b8c2aSAndrew Thompson 	pdev->session_data.plugtime = plugtime;
149df4b8c2aSAndrew Thompson 
150df4b8c2aSAndrew Thompson 	if (ioctl(f, USB_GET_DEVICE_DESC, &ddesc)) {
151df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
152df4b8c2aSAndrew Thompson 		goto done;
153df4b8c2aSAndrew Thompson 	}
154df4b8c2aSAndrew Thompson 	LIBUSB20_INIT(LIBUSB20_DEVICE_DESC, &(pdev->ddesc));
155df4b8c2aSAndrew Thompson 
156df4b8c2aSAndrew Thompson 	libusb20_me_decode(&ddesc, sizeof(ddesc), &(pdev->ddesc));
157df4b8c2aSAndrew Thompson 
158df4b8c2aSAndrew Thompson 	if (pdev->ddesc.bNumConfigurations == 0) {
159df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
160df4b8c2aSAndrew Thompson 		goto done;
161df4b8c2aSAndrew Thompson 	} else if (pdev->ddesc.bNumConfigurations >= 8) {
162df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
163df4b8c2aSAndrew Thompson 		goto done;
164df4b8c2aSAndrew Thompson 	}
165df4b8c2aSAndrew Thompson 	if (ioctl(f, USB_GET_DEVICEINFO, &devinfo)) {
166df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
167df4b8c2aSAndrew Thompson 		goto done;
168df4b8c2aSAndrew Thompson 	}
169df4b8c2aSAndrew Thompson 	switch (devinfo.udi_mode) {
170df4b8c2aSAndrew Thompson 	case USB_MODE_DEVICE:
171df4b8c2aSAndrew Thompson 		pdev->usb_mode = LIBUSB20_MODE_DEVICE;
172df4b8c2aSAndrew Thompson 		break;
173df4b8c2aSAndrew Thompson 	default:
174df4b8c2aSAndrew Thompson 		pdev->usb_mode = LIBUSB20_MODE_HOST;
175df4b8c2aSAndrew Thompson 		break;
176df4b8c2aSAndrew Thompson 	}
177df4b8c2aSAndrew Thompson 
178df4b8c2aSAndrew Thompson 	switch (devinfo.udi_speed) {
179df4b8c2aSAndrew Thompson 	case USB_SPEED_LOW:
180df4b8c2aSAndrew Thompson 		pdev->usb_speed = LIBUSB20_SPEED_LOW;
181df4b8c2aSAndrew Thompson 		break;
182df4b8c2aSAndrew Thompson 	case USB_SPEED_FULL:
183df4b8c2aSAndrew Thompson 		pdev->usb_speed = LIBUSB20_SPEED_FULL;
184df4b8c2aSAndrew Thompson 		break;
185df4b8c2aSAndrew Thompson 	case USB_SPEED_HIGH:
186df4b8c2aSAndrew Thompson 		pdev->usb_speed = LIBUSB20_SPEED_HIGH;
187df4b8c2aSAndrew Thompson 		break;
188df4b8c2aSAndrew Thompson 	case USB_SPEED_VARIABLE:
189df4b8c2aSAndrew Thompson 		pdev->usb_speed = LIBUSB20_SPEED_VARIABLE;
190df4b8c2aSAndrew Thompson 		break;
191df4b8c2aSAndrew Thompson 	case USB_SPEED_SUPER:
192df4b8c2aSAndrew Thompson 		pdev->usb_speed = LIBUSB20_SPEED_SUPER;
193df4b8c2aSAndrew Thompson 		break;
194df4b8c2aSAndrew Thompson 	default:
195df4b8c2aSAndrew Thompson 		pdev->usb_speed = LIBUSB20_SPEED_UNKNOWN;
196df4b8c2aSAndrew Thompson 		break;
197df4b8c2aSAndrew Thompson 	}
198df4b8c2aSAndrew Thompson 
199df4b8c2aSAndrew Thompson 	/* generate a nice description for printout */
200df4b8c2aSAndrew Thompson 
201df4b8c2aSAndrew Thompson 	snprintf(pdev->usb_desc, sizeof(pdev->usb_desc),
202df4b8c2aSAndrew Thompson 	    USB_GENERIC_NAME "%u.%u: <%s %s> at usbus%u", pdev->bus_number,
203df4b8c2aSAndrew Thompson 	    pdev->device_address, devinfo.udi_product,
204df4b8c2aSAndrew Thompson 	    devinfo.udi_vendor, pdev->bus_number);
205df4b8c2aSAndrew Thompson 
206df4b8c2aSAndrew Thompson 	error = 0;
207df4b8c2aSAndrew Thompson done:
208df4b8c2aSAndrew Thompson 	close(f);
209df4b8c2aSAndrew Thompson 	return (error);
210df4b8c2aSAndrew Thompson }
211df4b8c2aSAndrew Thompson 
212df4b8c2aSAndrew Thompson struct ugen20_urd_state {
213760bc48eSAndrew Thompson 	struct usb_read_dir urd;
214df4b8c2aSAndrew Thompson 	uint32_t nparsed;
215df4b8c2aSAndrew Thompson 	int	f;
216df4b8c2aSAndrew Thompson 	uint8_t *ptr;
217df4b8c2aSAndrew Thompson 	const char *src;
218df4b8c2aSAndrew Thompson 	const char *dst;
219df4b8c2aSAndrew Thompson 	uint8_t	buf[256];
220df4b8c2aSAndrew Thompson 	uint8_t	dummy_zero[1];
221df4b8c2aSAndrew Thompson };
222df4b8c2aSAndrew Thompson 
223df4b8c2aSAndrew Thompson static int
224df4b8c2aSAndrew Thompson ugen20_readdir(struct ugen20_urd_state *st)
225df4b8c2aSAndrew Thompson {
226df4b8c2aSAndrew Thompson 	;				/* style fix */
227df4b8c2aSAndrew Thompson repeat:
228df4b8c2aSAndrew Thompson 	if (st->ptr == NULL) {
229df4b8c2aSAndrew Thompson 		st->urd.urd_startentry += st->nparsed;
230df4b8c2aSAndrew Thompson 		st->urd.urd_data = st->buf;
231df4b8c2aSAndrew Thompson 		st->urd.urd_maxlen = sizeof(st->buf);
232df4b8c2aSAndrew Thompson 		st->nparsed = 0;
233df4b8c2aSAndrew Thompson 
234df4b8c2aSAndrew Thompson 		if (ioctl(st->f, USB_READ_DIR, &st->urd)) {
235df4b8c2aSAndrew Thompson 			return (EINVAL);
236df4b8c2aSAndrew Thompson 		}
237df4b8c2aSAndrew Thompson 		st->ptr = st->buf;
238df4b8c2aSAndrew Thompson 	}
239df4b8c2aSAndrew Thompson 	if (st->ptr[0] == 0) {
240df4b8c2aSAndrew Thompson 		if (st->nparsed) {
241df4b8c2aSAndrew Thompson 			st->ptr = NULL;
242df4b8c2aSAndrew Thompson 			goto repeat;
243df4b8c2aSAndrew Thompson 		} else {
244df4b8c2aSAndrew Thompson 			return (ENXIO);
245df4b8c2aSAndrew Thompson 		}
246df4b8c2aSAndrew Thompson 	}
247df4b8c2aSAndrew Thompson 	st->src = (void *)(st->ptr + 1);
248df4b8c2aSAndrew Thompson 	st->dst = st->src + strlen(st->src) + 1;
249df4b8c2aSAndrew Thompson 	st->ptr = st->ptr + st->ptr[0];
250df4b8c2aSAndrew Thompson 	st->nparsed++;
251df4b8c2aSAndrew Thompson 
252df4b8c2aSAndrew Thompson 	if ((st->ptr < st->buf) ||
253df4b8c2aSAndrew Thompson 	    (st->ptr > st->dummy_zero)) {
254df4b8c2aSAndrew Thompson 		/* invalid entry */
255df4b8c2aSAndrew Thompson 		return (EINVAL);
256df4b8c2aSAndrew Thompson 	}
257df4b8c2aSAndrew Thompson 	return (0);
258df4b8c2aSAndrew Thompson }
259df4b8c2aSAndrew Thompson 
260df4b8c2aSAndrew Thompson static int
261df4b8c2aSAndrew Thompson ugen20_init_backend(struct libusb20_backend *pbe)
262df4b8c2aSAndrew Thompson {
263df4b8c2aSAndrew Thompson 	struct ugen20_urd_state state;
264df4b8c2aSAndrew Thompson 	struct libusb20_device *pdev;
265df4b8c2aSAndrew Thompson 
266df4b8c2aSAndrew Thompson 	memset(&state, 0, sizeof(state));
267df4b8c2aSAndrew Thompson 
268df4b8c2aSAndrew Thompson 	state.f = open("/dev/" USB_DEVICE_NAME, O_RDONLY);
269df4b8c2aSAndrew Thompson 	if (state.f < 0)
270df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
271df4b8c2aSAndrew Thompson 
272df4b8c2aSAndrew Thompson 	while (ugen20_readdir(&state) == 0) {
273df4b8c2aSAndrew Thompson 
274df4b8c2aSAndrew Thompson 		if ((state.src[0] != 'u') ||
275df4b8c2aSAndrew Thompson 		    (state.src[1] != 'g') ||
276df4b8c2aSAndrew Thompson 		    (state.src[2] != 'e') ||
277df4b8c2aSAndrew Thompson 		    (state.src[3] != 'n')) {
278df4b8c2aSAndrew Thompson 			continue;
279df4b8c2aSAndrew Thompson 		}
280df4b8c2aSAndrew Thompson 		pdev = libusb20_dev_alloc();
281df4b8c2aSAndrew Thompson 		if (pdev == NULL) {
282df4b8c2aSAndrew Thompson 			continue;
283df4b8c2aSAndrew Thompson 		}
284df4b8c2aSAndrew Thompson 		if (ugen20_enumerate(pdev, state.src + 4)) {
285df4b8c2aSAndrew Thompson 			libusb20_dev_free(pdev);
286df4b8c2aSAndrew Thompson 			continue;
287df4b8c2aSAndrew Thompson 		}
288df4b8c2aSAndrew Thompson 		/* put the device on the backend list */
289df4b8c2aSAndrew Thompson 		libusb20_be_enqueue_device(pbe, pdev);
290df4b8c2aSAndrew Thompson 	}
291df4b8c2aSAndrew Thompson 	close(state.f);
292df4b8c2aSAndrew Thompson 	return (0);			/* success */
293df4b8c2aSAndrew Thompson }
294df4b8c2aSAndrew Thompson 
295df4b8c2aSAndrew Thompson static void
296df4b8c2aSAndrew Thompson ugen20_tr_release(struct libusb20_device *pdev)
297df4b8c2aSAndrew Thompson {
298760bc48eSAndrew Thompson 	struct usb_fs_uninit fs_uninit;
299df4b8c2aSAndrew Thompson 
300df4b8c2aSAndrew Thompson 	if (pdev->nTransfer == 0) {
301df4b8c2aSAndrew Thompson 		return;
302df4b8c2aSAndrew Thompson 	}
303df4b8c2aSAndrew Thompson 	/* release all pending USB transfers */
304df4b8c2aSAndrew Thompson 	if (pdev->privBeData != NULL) {
305df4b8c2aSAndrew Thompson 		memset(&fs_uninit, 0, sizeof(fs_uninit));
306df4b8c2aSAndrew Thompson 		if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) {
307df4b8c2aSAndrew Thompson 			/* ignore any errors of this kind */
308df4b8c2aSAndrew Thompson 		}
309df4b8c2aSAndrew Thompson 	}
310df4b8c2aSAndrew Thompson 	return;
311df4b8c2aSAndrew Thompson }
312df4b8c2aSAndrew Thompson 
313df4b8c2aSAndrew Thompson static int
314df4b8c2aSAndrew Thompson ugen20_tr_renew(struct libusb20_device *pdev)
315df4b8c2aSAndrew Thompson {
316760bc48eSAndrew Thompson 	struct usb_fs_init fs_init;
317760bc48eSAndrew Thompson 	struct usb_fs_endpoint *pfse;
318df4b8c2aSAndrew Thompson 	int error;
319df4b8c2aSAndrew Thompson 	uint32_t size;
320df4b8c2aSAndrew Thompson 	uint16_t nMaxTransfer;
321df4b8c2aSAndrew Thompson 
322df4b8c2aSAndrew Thompson 	nMaxTransfer = pdev->nTransfer;
323df4b8c2aSAndrew Thompson 	error = 0;
324df4b8c2aSAndrew Thompson 
325df4b8c2aSAndrew Thompson 	if (nMaxTransfer == 0) {
326df4b8c2aSAndrew Thompson 		goto done;
327df4b8c2aSAndrew Thompson 	}
328df4b8c2aSAndrew Thompson 	size = nMaxTransfer * sizeof(*pfse);
329df4b8c2aSAndrew Thompson 
330df4b8c2aSAndrew Thompson 	if (pdev->privBeData == NULL) {
331df4b8c2aSAndrew Thompson 		pfse = malloc(size);
332df4b8c2aSAndrew Thompson 		if (pfse == NULL) {
333df4b8c2aSAndrew Thompson 			error = LIBUSB20_ERROR_NO_MEM;
334df4b8c2aSAndrew Thompson 			goto done;
335df4b8c2aSAndrew Thompson 		}
336df4b8c2aSAndrew Thompson 		pdev->privBeData = pfse;
337df4b8c2aSAndrew Thompson 	}
338df4b8c2aSAndrew Thompson 	/* reset endpoint data */
339df4b8c2aSAndrew Thompson 	memset(pdev->privBeData, 0, size);
340df4b8c2aSAndrew Thompson 
341df4b8c2aSAndrew Thompson 	memset(&fs_init, 0, sizeof(fs_init));
342df4b8c2aSAndrew Thompson 
343df4b8c2aSAndrew Thompson 	fs_init.pEndpoints = pdev->privBeData;
344df4b8c2aSAndrew Thompson 	fs_init.ep_index_max = nMaxTransfer;
345df4b8c2aSAndrew Thompson 
346df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file, USB_FS_INIT, &fs_init)) {
347df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
348df4b8c2aSAndrew Thompson 		goto done;
349df4b8c2aSAndrew Thompson 	}
350df4b8c2aSAndrew Thompson done:
351df4b8c2aSAndrew Thompson 	return (error);
352df4b8c2aSAndrew Thompson }
353df4b8c2aSAndrew Thompson 
354df4b8c2aSAndrew Thompson static int
355df4b8c2aSAndrew Thompson ugen20_open_device(struct libusb20_device *pdev, uint16_t nMaxTransfer)
356df4b8c2aSAndrew Thompson {
357df4b8c2aSAndrew Thompson 	uint32_t plugtime;
358df4b8c2aSAndrew Thompson 	char buf[64];
359df4b8c2aSAndrew Thompson 	int f;
360df4b8c2aSAndrew Thompson 	int g;
361df4b8c2aSAndrew Thompson 	int error;
362df4b8c2aSAndrew Thompson 
363df4b8c2aSAndrew Thompson 	snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u",
364df4b8c2aSAndrew Thompson 	    pdev->bus_number, pdev->device_address);
365df4b8c2aSAndrew Thompson 
366df4b8c2aSAndrew Thompson 	/*
367df4b8c2aSAndrew Thompson 	 * We need two file handles, one for the control endpoint and one
368df4b8c2aSAndrew Thompson 	 * for BULK, INTERRUPT and ISOCHRONOUS transactions due to optimised
369df4b8c2aSAndrew Thompson 	 * kernel locking.
370df4b8c2aSAndrew Thompson 	 */
371df4b8c2aSAndrew Thompson 	g = open(buf, O_RDWR);
372df4b8c2aSAndrew Thompson 	if (g < 0) {
373df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_NO_DEVICE);
374df4b8c2aSAndrew Thompson 	}
375df4b8c2aSAndrew Thompson 	f = open(buf, O_RDWR);
376df4b8c2aSAndrew Thompson 	if (f < 0) {
377df4b8c2aSAndrew Thompson 		close(g);
378df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_NO_DEVICE);
379df4b8c2aSAndrew Thompson 	}
380df4b8c2aSAndrew Thompson 	if (ioctl(f, USB_GET_PLUGTIME, &plugtime)) {
381df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
382df4b8c2aSAndrew Thompson 		goto done;
383df4b8c2aSAndrew Thompson 	}
384df4b8c2aSAndrew Thompson 	/* check that the correct device is still plugged */
385df4b8c2aSAndrew Thompson 	if (pdev->session_data.plugtime != plugtime) {
386df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_NO_DEVICE;
387df4b8c2aSAndrew Thompson 		goto done;
388df4b8c2aSAndrew Thompson 	}
389df4b8c2aSAndrew Thompson 	/* need to set this before "tr_renew()" */
390df4b8c2aSAndrew Thompson 	pdev->file = f;
391df4b8c2aSAndrew Thompson 	pdev->file_ctrl = g;
392df4b8c2aSAndrew Thompson 
393df4b8c2aSAndrew Thompson 	/* renew all USB transfers */
394df4b8c2aSAndrew Thompson 	error = ugen20_tr_renew(pdev);
395df4b8c2aSAndrew Thompson 	if (error) {
396df4b8c2aSAndrew Thompson 		goto done;
397df4b8c2aSAndrew Thompson 	}
398df4b8c2aSAndrew Thompson 	/* set methods */
399df4b8c2aSAndrew Thompson 	pdev->methods = &libusb20_ugen20_device_methods;
400df4b8c2aSAndrew Thompson 
401df4b8c2aSAndrew Thompson done:
402df4b8c2aSAndrew Thompson 	if (error) {
403df4b8c2aSAndrew Thompson 		if (pdev->privBeData) {
404df4b8c2aSAndrew Thompson 			/* cleanup after "tr_renew()" */
405df4b8c2aSAndrew Thompson 			free(pdev->privBeData);
406df4b8c2aSAndrew Thompson 			pdev->privBeData = NULL;
407df4b8c2aSAndrew Thompson 		}
408df4b8c2aSAndrew Thompson 		pdev->file = -1;
409df4b8c2aSAndrew Thompson 		pdev->file_ctrl = -1;
410df4b8c2aSAndrew Thompson 		close(f);
411df4b8c2aSAndrew Thompson 		close(g);
412df4b8c2aSAndrew Thompson 	}
413df4b8c2aSAndrew Thompson 	return (error);
414df4b8c2aSAndrew Thompson }
415df4b8c2aSAndrew Thompson 
416df4b8c2aSAndrew Thompson static int
417df4b8c2aSAndrew Thompson ugen20_close_device(struct libusb20_device *pdev)
418df4b8c2aSAndrew Thompson {
419760bc48eSAndrew Thompson 	struct usb_fs_uninit fs_uninit;
420df4b8c2aSAndrew Thompson 
421df4b8c2aSAndrew Thompson 	if (pdev->privBeData) {
422df4b8c2aSAndrew Thompson 		memset(&fs_uninit, 0, sizeof(fs_uninit));
423df4b8c2aSAndrew Thompson 		if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) {
424df4b8c2aSAndrew Thompson 			/* ignore this error */
425df4b8c2aSAndrew Thompson 		}
426df4b8c2aSAndrew Thompson 		free(pdev->privBeData);
427df4b8c2aSAndrew Thompson 	}
428df4b8c2aSAndrew Thompson 	pdev->nTransfer = 0;
429df4b8c2aSAndrew Thompson 	pdev->privBeData = NULL;
430df4b8c2aSAndrew Thompson 	close(pdev->file);
431df4b8c2aSAndrew Thompson 	close(pdev->file_ctrl);
432df4b8c2aSAndrew Thompson 	pdev->file = -1;
433df4b8c2aSAndrew Thompson 	pdev->file_ctrl = -1;
434df4b8c2aSAndrew Thompson 	return (0);			/* success */
435df4b8c2aSAndrew Thompson }
436df4b8c2aSAndrew Thompson 
437df4b8c2aSAndrew Thompson static void
438df4b8c2aSAndrew Thompson ugen20_exit_backend(struct libusb20_backend *pbe)
439df4b8c2aSAndrew Thompson {
440df4b8c2aSAndrew Thompson 	return;				/* nothing to do */
441df4b8c2aSAndrew Thompson }
442df4b8c2aSAndrew Thompson 
443df4b8c2aSAndrew Thompson static int
444df4b8c2aSAndrew Thompson ugen20_get_config_desc_full(struct libusb20_device *pdev,
445df4b8c2aSAndrew Thompson     uint8_t **ppbuf, uint16_t *plen, uint8_t cfg_index)
446df4b8c2aSAndrew Thompson {
447760bc48eSAndrew Thompson 	struct usb_gen_descriptor gen_desc;
448760bc48eSAndrew Thompson 	struct usb_config_descriptor cdesc;
449df4b8c2aSAndrew Thompson 	uint8_t *ptr;
450df4b8c2aSAndrew Thompson 	uint16_t len;
451df4b8c2aSAndrew Thompson 	int error;
452df4b8c2aSAndrew Thompson 
453ccef4ddfSAndrew Thompson 	/* make sure memory is initialised */
454ccef4ddfSAndrew Thompson 	memset(&cdesc, 0, sizeof(cdesc));
455df4b8c2aSAndrew Thompson 	memset(&gen_desc, 0, sizeof(gen_desc));
456df4b8c2aSAndrew Thompson 
457df4b8c2aSAndrew Thompson 	gen_desc.ugd_data = &cdesc;
458df4b8c2aSAndrew Thompson 	gen_desc.ugd_maxlen = sizeof(cdesc);
459df4b8c2aSAndrew Thompson 	gen_desc.ugd_config_index = cfg_index;
460df4b8c2aSAndrew Thompson 
461df4b8c2aSAndrew Thompson 	error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc);
462df4b8c2aSAndrew Thompson 	if (error) {
463df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
464df4b8c2aSAndrew Thompson 	}
465df4b8c2aSAndrew Thompson 	len = UGETW(cdesc.wTotalLength);
466df4b8c2aSAndrew Thompson 	if (len < sizeof(cdesc)) {
467df4b8c2aSAndrew Thompson 		/* corrupt descriptor */
468df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
469df4b8c2aSAndrew Thompson 	}
470df4b8c2aSAndrew Thompson 	ptr = malloc(len);
471df4b8c2aSAndrew Thompson 	if (!ptr) {
472df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_NO_MEM);
473df4b8c2aSAndrew Thompson 	}
474ccef4ddfSAndrew Thompson 
475ccef4ddfSAndrew Thompson 	/* make sure memory is initialised */
476ccef4ddfSAndrew Thompson 	memset(ptr, 0, len);
477ccef4ddfSAndrew Thompson 
478df4b8c2aSAndrew Thompson 	gen_desc.ugd_data = ptr;
479df4b8c2aSAndrew Thompson 	gen_desc.ugd_maxlen = len;
480df4b8c2aSAndrew Thompson 
481df4b8c2aSAndrew Thompson 	error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc);
482df4b8c2aSAndrew Thompson 	if (error) {
483df4b8c2aSAndrew Thompson 		free(ptr);
484df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
485df4b8c2aSAndrew Thompson 	}
486df4b8c2aSAndrew Thompson 	/* make sure that the device doesn't fool us */
487df4b8c2aSAndrew Thompson 	memcpy(ptr, &cdesc, sizeof(cdesc));
488df4b8c2aSAndrew Thompson 
489df4b8c2aSAndrew Thompson 	*ppbuf = ptr;
490df4b8c2aSAndrew Thompson 	*plen = len;
491df4b8c2aSAndrew Thompson 
492df4b8c2aSAndrew Thompson 	return (0);			/* success */
493df4b8c2aSAndrew Thompson }
494df4b8c2aSAndrew Thompson 
495df4b8c2aSAndrew Thompson static int
496df4b8c2aSAndrew Thompson ugen20_get_config_index(struct libusb20_device *pdev, uint8_t *pindex)
497df4b8c2aSAndrew Thompson {
498df4b8c2aSAndrew Thompson 	int temp;
499df4b8c2aSAndrew Thompson 
500df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_GET_CONFIG, &temp)) {
501df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
502df4b8c2aSAndrew Thompson 	}
503df4b8c2aSAndrew Thompson 	*pindex = temp;
504df4b8c2aSAndrew Thompson 
505df4b8c2aSAndrew Thompson 	return (0);
506df4b8c2aSAndrew Thompson }
507df4b8c2aSAndrew Thompson 
508df4b8c2aSAndrew Thompson static int
509df4b8c2aSAndrew Thompson ugen20_set_config_index(struct libusb20_device *pdev, uint8_t cfg_index)
510df4b8c2aSAndrew Thompson {
511df4b8c2aSAndrew Thompson 	int temp = cfg_index;
512df4b8c2aSAndrew Thompson 
513df4b8c2aSAndrew Thompson 	/* release all active USB transfers */
514df4b8c2aSAndrew Thompson 	ugen20_tr_release(pdev);
515df4b8c2aSAndrew Thompson 
516df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_SET_CONFIG, &temp)) {
517df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
518df4b8c2aSAndrew Thompson 	}
519df4b8c2aSAndrew Thompson 	return (ugen20_tr_renew(pdev));
520df4b8c2aSAndrew Thompson }
521df4b8c2aSAndrew Thompson 
522df4b8c2aSAndrew Thompson static int
523df4b8c2aSAndrew Thompson ugen20_set_alt_index(struct libusb20_device *pdev,
524df4b8c2aSAndrew Thompson     uint8_t iface_index, uint8_t alt_index)
525df4b8c2aSAndrew Thompson {
526760bc48eSAndrew Thompson 	struct usb_alt_interface alt_iface;
527df4b8c2aSAndrew Thompson 
528df4b8c2aSAndrew Thompson 	memset(&alt_iface, 0, sizeof(alt_iface));
529df4b8c2aSAndrew Thompson 
530df4b8c2aSAndrew Thompson 	alt_iface.uai_interface_index = iface_index;
531df4b8c2aSAndrew Thompson 	alt_iface.uai_alt_index = alt_index;
532df4b8c2aSAndrew Thompson 
533df4b8c2aSAndrew Thompson 	/* release all active USB transfers */
534df4b8c2aSAndrew Thompson 	ugen20_tr_release(pdev);
535df4b8c2aSAndrew Thompson 
536df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_SET_ALTINTERFACE, &alt_iface)) {
537df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
538df4b8c2aSAndrew Thompson 	}
539df4b8c2aSAndrew Thompson 	return (ugen20_tr_renew(pdev));
540df4b8c2aSAndrew Thompson }
541df4b8c2aSAndrew Thompson 
542df4b8c2aSAndrew Thompson static int
543df4b8c2aSAndrew Thompson ugen20_reset_device(struct libusb20_device *pdev)
544df4b8c2aSAndrew Thompson {
545df4b8c2aSAndrew Thompson 	int temp = 0;
546df4b8c2aSAndrew Thompson 
547df4b8c2aSAndrew Thompson 	/* release all active USB transfers */
548df4b8c2aSAndrew Thompson 	ugen20_tr_release(pdev);
549df4b8c2aSAndrew Thompson 
550df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_DEVICEENUMERATE, &temp)) {
551df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
552df4b8c2aSAndrew Thompson 	}
553df4b8c2aSAndrew Thompson 	return (ugen20_tr_renew(pdev));
554df4b8c2aSAndrew Thompson }
555df4b8c2aSAndrew Thompson 
556df4b8c2aSAndrew Thompson static int
557e50ac68bSAndrew Thompson ugen20_check_connected(struct libusb20_device *pdev)
558e50ac68bSAndrew Thompson {
559e50ac68bSAndrew Thompson 	uint32_t plugtime;
560e50ac68bSAndrew Thompson 	int error = 0;
561e50ac68bSAndrew Thompson 
562e50ac68bSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_GET_PLUGTIME, &plugtime)) {
563e50ac68bSAndrew Thompson 		error = LIBUSB20_ERROR_NO_DEVICE;
564e50ac68bSAndrew Thompson 		goto done;
565e50ac68bSAndrew Thompson 	}
566e50ac68bSAndrew Thompson 
567e50ac68bSAndrew Thompson 	if (pdev->session_data.plugtime != plugtime) {
568e50ac68bSAndrew Thompson 		error = LIBUSB20_ERROR_NO_DEVICE;
569e50ac68bSAndrew Thompson 		goto done;
570e50ac68bSAndrew Thompson 	}
571e50ac68bSAndrew Thompson done:
572e50ac68bSAndrew Thompson 	return (error);
573e50ac68bSAndrew Thompson }
574e50ac68bSAndrew Thompson 
575e50ac68bSAndrew Thompson static int
576df4b8c2aSAndrew Thompson ugen20_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
577df4b8c2aSAndrew Thompson {
578df4b8c2aSAndrew Thompson 	int temp;
579df4b8c2aSAndrew Thompson 
580df4b8c2aSAndrew Thompson 	switch (power_mode) {
581df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_OFF:
582df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_OFF;
583df4b8c2aSAndrew Thompson 		break;
584df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_ON:
585df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_ON;
586df4b8c2aSAndrew Thompson 		break;
587df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_SAVE:
588df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_SAVE;
589df4b8c2aSAndrew Thompson 		break;
590df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_SUSPEND:
591df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_SUSPEND;
592df4b8c2aSAndrew Thompson 		break;
593df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_RESUME:
594df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_RESUME;
595df4b8c2aSAndrew Thompson 		break;
596df4b8c2aSAndrew Thompson 	default:
597df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
598df4b8c2aSAndrew Thompson 	}
599df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_SET_POWER_MODE, &temp)) {
600df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
601df4b8c2aSAndrew Thompson 	}
602df4b8c2aSAndrew Thompson 	return (0);
603df4b8c2aSAndrew Thompson }
604df4b8c2aSAndrew Thompson 
605df4b8c2aSAndrew Thompson static int
606df4b8c2aSAndrew Thompson ugen20_get_power_mode(struct libusb20_device *pdev, uint8_t *power_mode)
607df4b8c2aSAndrew Thompson {
608df4b8c2aSAndrew Thompson 	int temp;
609df4b8c2aSAndrew Thompson 
610df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_GET_POWER_MODE, &temp)) {
611df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
612df4b8c2aSAndrew Thompson 	}
613df4b8c2aSAndrew Thompson 	switch (temp) {
614df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_OFF:
615df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_OFF;
616df4b8c2aSAndrew Thompson 		break;
617df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_ON:
618df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_ON;
619df4b8c2aSAndrew Thompson 		break;
620df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_SAVE:
621df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_SAVE;
622df4b8c2aSAndrew Thompson 		break;
623df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_SUSPEND:
624df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_SUSPEND;
625df4b8c2aSAndrew Thompson 		break;
626df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_RESUME:
627df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_RESUME;
628df4b8c2aSAndrew Thompson 		break;
629df4b8c2aSAndrew Thompson 	default:
630df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_ON;
631df4b8c2aSAndrew Thompson 		break;
632df4b8c2aSAndrew Thompson 	}
633df4b8c2aSAndrew Thompson 	*power_mode = temp;
634df4b8c2aSAndrew Thompson 	return (0);			/* success */
635df4b8c2aSAndrew Thompson }
636df4b8c2aSAndrew Thompson 
637df4b8c2aSAndrew Thompson static int
638df4b8c2aSAndrew Thompson ugen20_kernel_driver_active(struct libusb20_device *pdev,
639df4b8c2aSAndrew Thompson     uint8_t iface_index)
640df4b8c2aSAndrew Thompson {
641df4b8c2aSAndrew Thompson 	int temp = iface_index;
642df4b8c2aSAndrew Thompson 
643df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_ACTIVE, &temp)) {
644df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
645df4b8c2aSAndrew Thompson 	}
646df4b8c2aSAndrew Thompson 	return (0);			/* kernel driver is active */
647df4b8c2aSAndrew Thompson }
648df4b8c2aSAndrew Thompson 
649df4b8c2aSAndrew Thompson static int
650df4b8c2aSAndrew Thompson ugen20_detach_kernel_driver(struct libusb20_device *pdev,
651df4b8c2aSAndrew Thompson     uint8_t iface_index)
652df4b8c2aSAndrew Thompson {
653df4b8c2aSAndrew Thompson 	int temp = iface_index;
654df4b8c2aSAndrew Thompson 
655df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_DETACH, &temp)) {
656df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
657df4b8c2aSAndrew Thompson 	}
658df4b8c2aSAndrew Thompson 	return (0);			/* kernel driver is active */
659df4b8c2aSAndrew Thompson }
660df4b8c2aSAndrew Thompson 
661df4b8c2aSAndrew Thompson static int
662df4b8c2aSAndrew Thompson ugen20_do_request_sync(struct libusb20_device *pdev,
663df4b8c2aSAndrew Thompson     struct LIBUSB20_CONTROL_SETUP_DECODED *setup,
664df4b8c2aSAndrew Thompson     void *data, uint16_t *pactlen, uint32_t timeout, uint8_t flags)
665df4b8c2aSAndrew Thompson {
666760bc48eSAndrew Thompson 	struct usb_ctl_request req;
667df4b8c2aSAndrew Thompson 
668df4b8c2aSAndrew Thompson 	memset(&req, 0, sizeof(req));
669df4b8c2aSAndrew Thompson 
670df4b8c2aSAndrew Thompson 	req.ucr_data = data;
671df4b8c2aSAndrew Thompson 	if (!(flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) {
672df4b8c2aSAndrew Thompson 		req.ucr_flags |= USB_SHORT_XFER_OK;
673df4b8c2aSAndrew Thompson 	}
674df4b8c2aSAndrew Thompson 	if (libusb20_me_encode(&req.ucr_request,
675df4b8c2aSAndrew Thompson 	    sizeof(req.ucr_request), setup)) {
676df4b8c2aSAndrew Thompson 		/* ignore */
677df4b8c2aSAndrew Thompson 	}
678df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_DO_REQUEST, &req)) {
679df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
680df4b8c2aSAndrew Thompson 	}
681df4b8c2aSAndrew Thompson 	if (pactlen) {
682df4b8c2aSAndrew Thompson 		/* get actual length */
683df4b8c2aSAndrew Thompson 		*pactlen = req.ucr_actlen;
684df4b8c2aSAndrew Thompson 	}
685df4b8c2aSAndrew Thompson 	return (0);			/* kernel driver is active */
686df4b8c2aSAndrew Thompson }
687df4b8c2aSAndrew Thompson 
688df4b8c2aSAndrew Thompson static int
689df4b8c2aSAndrew Thompson ugen20_process(struct libusb20_device *pdev)
690df4b8c2aSAndrew Thompson {
691760bc48eSAndrew Thompson 	struct usb_fs_complete temp;
692760bc48eSAndrew Thompson 	struct usb_fs_endpoint *fsep;
693df4b8c2aSAndrew Thompson 	struct libusb20_transfer *xfer;
694df4b8c2aSAndrew Thompson 
695df4b8c2aSAndrew Thompson 	while (1) {
696df4b8c2aSAndrew Thompson 
697df4b8c2aSAndrew Thompson 		if (ioctl(pdev->file, USB_FS_COMPLETE, &temp)) {
698df4b8c2aSAndrew Thompson 			if (errno == EBUSY) {
699df4b8c2aSAndrew Thompson 				break;
700df4b8c2aSAndrew Thompson 			} else {
701df4b8c2aSAndrew Thompson 				/* device detached */
702df4b8c2aSAndrew Thompson 				return (LIBUSB20_ERROR_OTHER);
703df4b8c2aSAndrew Thompson 			}
704df4b8c2aSAndrew Thompson 		}
705df4b8c2aSAndrew Thompson 		fsep = pdev->privBeData;
706df4b8c2aSAndrew Thompson 		xfer = pdev->pTransfer;
707df4b8c2aSAndrew Thompson 		fsep += temp.ep_index;
708df4b8c2aSAndrew Thompson 		xfer += temp.ep_index;
709df4b8c2aSAndrew Thompson 
710df4b8c2aSAndrew Thompson 		/* update transfer status */
711df4b8c2aSAndrew Thompson 
712df4b8c2aSAndrew Thompson 		if (fsep->status == 0) {
713df4b8c2aSAndrew Thompson 			xfer->aFrames = fsep->aFrames;
714df4b8c2aSAndrew Thompson 			xfer->timeComplete = fsep->isoc_time_complete;
715df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_COMPLETED;
716df4b8c2aSAndrew Thompson 		} else if (fsep->status == USB_ERR_CANCELLED) {
717df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
718df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
719df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_CANCELLED;
720df4b8c2aSAndrew Thompson 		} else if (fsep->status == USB_ERR_STALLED) {
721df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
722df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
723df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_STALL;
724df4b8c2aSAndrew Thompson 		} else if (fsep->status == USB_ERR_TIMEOUT) {
725df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
726df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
727df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_TIMED_OUT;
728df4b8c2aSAndrew Thompson 		} else {
729df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
730df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
731df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_ERROR;
732df4b8c2aSAndrew Thompson 		}
733df4b8c2aSAndrew Thompson 		libusb20_tr_callback_wrapper(xfer);
734df4b8c2aSAndrew Thompson 	}
735df4b8c2aSAndrew Thompson 	return (0);			/* done */
736df4b8c2aSAndrew Thompson }
737df4b8c2aSAndrew Thompson 
738df4b8c2aSAndrew Thompson static int
739df4b8c2aSAndrew Thompson ugen20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
740df4b8c2aSAndrew Thompson     uint32_t MaxFrameCount, uint8_t ep_no)
741df4b8c2aSAndrew Thompson {
742760bc48eSAndrew Thompson 	struct usb_fs_open temp;
743760bc48eSAndrew Thompson 	struct usb_fs_endpoint *fsep;
744df4b8c2aSAndrew Thompson 
745df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
746df4b8c2aSAndrew Thompson 
747df4b8c2aSAndrew Thompson 	fsep = xfer->pdev->privBeData;
748df4b8c2aSAndrew Thompson 	fsep += xfer->trIndex;
749df4b8c2aSAndrew Thompson 
750df4b8c2aSAndrew Thompson 	temp.max_bufsize = MaxBufSize;
751df4b8c2aSAndrew Thompson 	temp.max_frames = MaxFrameCount;
752df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
753df4b8c2aSAndrew Thompson 	temp.ep_no = ep_no;
754df4b8c2aSAndrew Thompson 
755df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_OPEN, &temp)) {
756df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
757df4b8c2aSAndrew Thompson 	}
758df4b8c2aSAndrew Thompson 	/* maximums might have changed - update */
759df4b8c2aSAndrew Thompson 	xfer->maxFrames = temp.max_frames;
760df4b8c2aSAndrew Thompson 
761df4b8c2aSAndrew Thompson 	/* "max_bufsize" should be multiple of "max_packet_length" */
762df4b8c2aSAndrew Thompson 	xfer->maxTotalLength = temp.max_bufsize;
763df4b8c2aSAndrew Thompson 	xfer->maxPacketLen = temp.max_packet_length;
764df4b8c2aSAndrew Thompson 
765df4b8c2aSAndrew Thompson 	/* setup buffer and length lists */
766df4b8c2aSAndrew Thompson 	fsep->ppBuffer = xfer->ppBuffer;/* zero copy */
767df4b8c2aSAndrew Thompson 	fsep->pLength = xfer->pLength;	/* zero copy */
768df4b8c2aSAndrew Thompson 
769df4b8c2aSAndrew Thompson 	return (0);			/* success */
770df4b8c2aSAndrew Thompson }
771df4b8c2aSAndrew Thompson 
772df4b8c2aSAndrew Thompson static int
773df4b8c2aSAndrew Thompson ugen20_tr_close(struct libusb20_transfer *xfer)
774df4b8c2aSAndrew Thompson {
775760bc48eSAndrew Thompson 	struct usb_fs_close temp;
776df4b8c2aSAndrew Thompson 
777df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
778df4b8c2aSAndrew Thompson 
779df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
780df4b8c2aSAndrew Thompson 
781df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_CLOSE, &temp)) {
782df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
783df4b8c2aSAndrew Thompson 	}
784df4b8c2aSAndrew Thompson 	return (0);			/* success */
785df4b8c2aSAndrew Thompson }
786df4b8c2aSAndrew Thompson 
787df4b8c2aSAndrew Thompson static int
788df4b8c2aSAndrew Thompson ugen20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
789df4b8c2aSAndrew Thompson {
790760bc48eSAndrew Thompson 	struct usb_fs_clear_stall_sync temp;
791df4b8c2aSAndrew Thompson 
792df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
793df4b8c2aSAndrew Thompson 
794df4b8c2aSAndrew Thompson 	/* if the transfer is active, an error will be returned */
795df4b8c2aSAndrew Thompson 
796df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
797df4b8c2aSAndrew Thompson 
798df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_CLEAR_STALL_SYNC, &temp)) {
799df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
800df4b8c2aSAndrew Thompson 	}
801df4b8c2aSAndrew Thompson 	return (0);			/* success */
802df4b8c2aSAndrew Thompson }
803df4b8c2aSAndrew Thompson 
804df4b8c2aSAndrew Thompson static void
805df4b8c2aSAndrew Thompson ugen20_tr_submit(struct libusb20_transfer *xfer)
806df4b8c2aSAndrew Thompson {
807760bc48eSAndrew Thompson 	struct usb_fs_start temp;
808760bc48eSAndrew Thompson 	struct usb_fs_endpoint *fsep;
809df4b8c2aSAndrew Thompson 
810df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
811df4b8c2aSAndrew Thompson 
812df4b8c2aSAndrew Thompson 	fsep = xfer->pdev->privBeData;
813df4b8c2aSAndrew Thompson 	fsep += xfer->trIndex;
814df4b8c2aSAndrew Thompson 
815df4b8c2aSAndrew Thompson 	fsep->nFrames = xfer->nFrames;
816df4b8c2aSAndrew Thompson 	fsep->flags = 0;
817df4b8c2aSAndrew Thompson 	if (!(xfer->flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) {
818df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_SINGLE_SHORT_OK;
819df4b8c2aSAndrew Thompson 	}
820df4b8c2aSAndrew Thompson 	if (!(xfer->flags & LIBUSB20_TRANSFER_MULTI_SHORT_NOT_OK)) {
821df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_MULTI_SHORT_OK;
822df4b8c2aSAndrew Thompson 	}
823df4b8c2aSAndrew Thompson 	if (xfer->flags & LIBUSB20_TRANSFER_FORCE_SHORT) {
824df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_FORCE_SHORT;
825df4b8c2aSAndrew Thompson 	}
826df4b8c2aSAndrew Thompson 	if (xfer->flags & LIBUSB20_TRANSFER_DO_CLEAR_STALL) {
827df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_CLEAR_STALL;
828df4b8c2aSAndrew Thompson 	}
8290c35eaadSAndrew Thompson 	/* NOTE: The "fsep->timeout" variable is 16-bit. */
8300c35eaadSAndrew Thompson 	if (xfer->timeout > 65535)
8310c35eaadSAndrew Thompson 		fsep->timeout = 65535;
8320c35eaadSAndrew Thompson 	else
833df4b8c2aSAndrew Thompson 		fsep->timeout = xfer->timeout;
834df4b8c2aSAndrew Thompson 
835df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
836df4b8c2aSAndrew Thompson 
837df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_START, &temp)) {
838df4b8c2aSAndrew Thompson 		/* ignore any errors - should never happen */
839df4b8c2aSAndrew Thompson 	}
840df4b8c2aSAndrew Thompson 	return;				/* success */
841df4b8c2aSAndrew Thompson }
842df4b8c2aSAndrew Thompson 
843df4b8c2aSAndrew Thompson static void
844df4b8c2aSAndrew Thompson ugen20_tr_cancel_async(struct libusb20_transfer *xfer)
845df4b8c2aSAndrew Thompson {
846760bc48eSAndrew Thompson 	struct usb_fs_stop temp;
847df4b8c2aSAndrew Thompson 
848df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
849df4b8c2aSAndrew Thompson 
850df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
851df4b8c2aSAndrew Thompson 
852df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_STOP, &temp)) {
853df4b8c2aSAndrew Thompson 		/* ignore any errors - should never happen */
854df4b8c2aSAndrew Thompson 	}
855df4b8c2aSAndrew Thompson 	return;
856df4b8c2aSAndrew Thompson }
857df4b8c2aSAndrew Thompson 
858df4b8c2aSAndrew Thompson static int
859df4b8c2aSAndrew Thompson ugen20_be_ioctl(uint32_t cmd, void *data)
860df4b8c2aSAndrew Thompson {
861df4b8c2aSAndrew Thompson 	int f;
862df4b8c2aSAndrew Thompson 	int error;
863df4b8c2aSAndrew Thompson 
864df4b8c2aSAndrew Thompson 	f = open("/dev/" USB_DEVICE_NAME, O_RDONLY);
865df4b8c2aSAndrew Thompson 	if (f < 0)
866df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
867df4b8c2aSAndrew Thompson 	error = ioctl(f, cmd, data);
868df4b8c2aSAndrew Thompson 	if (error == -1) {
869df4b8c2aSAndrew Thompson 		if (errno == EPERM) {
870df4b8c2aSAndrew Thompson 			error = LIBUSB20_ERROR_ACCESS;
871df4b8c2aSAndrew Thompson 		} else {
872df4b8c2aSAndrew Thompson 			error = LIBUSB20_ERROR_OTHER;
873df4b8c2aSAndrew Thompson 		}
874df4b8c2aSAndrew Thompson 	}
875df4b8c2aSAndrew Thompson 	close(f);
876df4b8c2aSAndrew Thompson 	return (error);
877df4b8c2aSAndrew Thompson }
878df4b8c2aSAndrew Thompson 
879df4b8c2aSAndrew Thompson static int
880df4b8c2aSAndrew Thompson ugen20_dev_get_iface_desc(struct libusb20_device *pdev,
881df4b8c2aSAndrew Thompson     uint8_t iface_index, char *buf, uint8_t len)
882df4b8c2aSAndrew Thompson {
883760bc48eSAndrew Thompson 	struct usb_gen_descriptor ugd;
884df4b8c2aSAndrew Thompson 
885df4b8c2aSAndrew Thompson 	memset(&ugd, 0, sizeof(ugd));
886df4b8c2aSAndrew Thompson 
887df4b8c2aSAndrew Thompson 	ugd.ugd_data = buf;
888df4b8c2aSAndrew Thompson 	ugd.ugd_maxlen = len;
889df4b8c2aSAndrew Thompson 	ugd.ugd_iface_index = iface_index;
890df4b8c2aSAndrew Thompson 
891df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file, USB_GET_IFACE_DRIVER, &ugd)) {
892df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
893df4b8c2aSAndrew Thompson 	}
894df4b8c2aSAndrew Thompson 	return (0);
895df4b8c2aSAndrew Thompson }
896df4b8c2aSAndrew Thompson 
897df4b8c2aSAndrew Thompson static int
898df4b8c2aSAndrew Thompson ugen20_dev_get_info(struct libusb20_device *pdev,
899760bc48eSAndrew Thompson     struct usb_device_info *pinfo)
900df4b8c2aSAndrew Thompson {
901df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file, USB_GET_DEVICEINFO, pinfo)) {
902df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
903df4b8c2aSAndrew Thompson 	}
904df4b8c2aSAndrew Thompson 	return (0);
905df4b8c2aSAndrew Thompson }
906df4b8c2aSAndrew Thompson 
907df4b8c2aSAndrew Thompson static int
908df4b8c2aSAndrew Thompson ugen20_root_get_dev_quirk(struct libusb20_backend *pbe,
909df4b8c2aSAndrew Thompson     uint16_t quirk_index, struct libusb20_quirk *pq)
910df4b8c2aSAndrew Thompson {
911760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
912df4b8c2aSAndrew Thompson 	int error;
913df4b8c2aSAndrew Thompson 
914df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
915df4b8c2aSAndrew Thompson 
916df4b8c2aSAndrew Thompson 	q.index = quirk_index;
917df4b8c2aSAndrew Thompson 
918df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_DEV_QUIRK_GET, &q);
919df4b8c2aSAndrew Thompson 
920df4b8c2aSAndrew Thompson 	if (error) {
921df4b8c2aSAndrew Thompson 		if (errno == EINVAL) {
922df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NOT_FOUND);
923df4b8c2aSAndrew Thompson 		}
924df4b8c2aSAndrew Thompson 	} else {
925df4b8c2aSAndrew Thompson 		pq->vid = q.vid;
926df4b8c2aSAndrew Thompson 		pq->pid = q.pid;
927df4b8c2aSAndrew Thompson 		pq->bcdDeviceLow = q.bcdDeviceLow;
928df4b8c2aSAndrew Thompson 		pq->bcdDeviceHigh = q.bcdDeviceHigh;
929df4b8c2aSAndrew Thompson 		strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname));
930df4b8c2aSAndrew Thompson 	}
931df4b8c2aSAndrew Thompson 	return (error);
932df4b8c2aSAndrew Thompson }
933df4b8c2aSAndrew Thompson 
934df4b8c2aSAndrew Thompson static int
935df4b8c2aSAndrew Thompson ugen20_root_get_quirk_name(struct libusb20_backend *pbe, uint16_t quirk_index,
936df4b8c2aSAndrew Thompson     struct libusb20_quirk *pq)
937df4b8c2aSAndrew Thompson {
938760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
939df4b8c2aSAndrew Thompson 	int error;
940df4b8c2aSAndrew Thompson 
941df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
942df4b8c2aSAndrew Thompson 
943df4b8c2aSAndrew Thompson 	q.index = quirk_index;
944df4b8c2aSAndrew Thompson 
945df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_QUIRK_NAME_GET, &q);
946df4b8c2aSAndrew Thompson 
947df4b8c2aSAndrew Thompson 	if (error) {
948df4b8c2aSAndrew Thompson 		if (errno == EINVAL) {
949df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NOT_FOUND);
950df4b8c2aSAndrew Thompson 		}
951df4b8c2aSAndrew Thompson 	} else {
952df4b8c2aSAndrew Thompson 		strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname));
953df4b8c2aSAndrew Thompson 	}
954df4b8c2aSAndrew Thompson 	return (error);
955df4b8c2aSAndrew Thompson }
956df4b8c2aSAndrew Thompson 
957df4b8c2aSAndrew Thompson static int
958df4b8c2aSAndrew Thompson ugen20_root_add_dev_quirk(struct libusb20_backend *pbe,
959df4b8c2aSAndrew Thompson     struct libusb20_quirk *pq)
960df4b8c2aSAndrew Thompson {
961760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
962df4b8c2aSAndrew Thompson 	int error;
963df4b8c2aSAndrew Thompson 
964df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
965df4b8c2aSAndrew Thompson 
966df4b8c2aSAndrew Thompson 	q.vid = pq->vid;
967df4b8c2aSAndrew Thompson 	q.pid = pq->pid;
968df4b8c2aSAndrew Thompson 	q.bcdDeviceLow = pq->bcdDeviceLow;
969df4b8c2aSAndrew Thompson 	q.bcdDeviceHigh = pq->bcdDeviceHigh;
970df4b8c2aSAndrew Thompson 	strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname));
971df4b8c2aSAndrew Thompson 
972df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_DEV_QUIRK_ADD, &q);
973df4b8c2aSAndrew Thompson 	if (error) {
974df4b8c2aSAndrew Thompson 		if (errno == ENOMEM) {
975df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NO_MEM);
976df4b8c2aSAndrew Thompson 		}
977df4b8c2aSAndrew Thompson 	}
978df4b8c2aSAndrew Thompson 	return (error);
979df4b8c2aSAndrew Thompson }
980df4b8c2aSAndrew Thompson 
981df4b8c2aSAndrew Thompson static int
982df4b8c2aSAndrew Thompson ugen20_root_remove_dev_quirk(struct libusb20_backend *pbe,
983df4b8c2aSAndrew Thompson     struct libusb20_quirk *pq)
984df4b8c2aSAndrew Thompson {
985760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
986df4b8c2aSAndrew Thompson 	int error;
987df4b8c2aSAndrew Thompson 
988df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
989df4b8c2aSAndrew Thompson 
990df4b8c2aSAndrew Thompson 	q.vid = pq->vid;
991df4b8c2aSAndrew Thompson 	q.pid = pq->pid;
992df4b8c2aSAndrew Thompson 	q.bcdDeviceLow = pq->bcdDeviceLow;
993df4b8c2aSAndrew Thompson 	q.bcdDeviceHigh = pq->bcdDeviceHigh;
994df4b8c2aSAndrew Thompson 	strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname));
995df4b8c2aSAndrew Thompson 
996df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_DEV_QUIRK_REMOVE, &q);
997df4b8c2aSAndrew Thompson 	if (error) {
998df4b8c2aSAndrew Thompson 		if (errno == EINVAL) {
999df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NOT_FOUND);
1000df4b8c2aSAndrew Thompson 		}
1001df4b8c2aSAndrew Thompson 	}
1002df4b8c2aSAndrew Thompson 	return (error);
1003df4b8c2aSAndrew Thompson }
1004df4b8c2aSAndrew Thompson 
1005df4b8c2aSAndrew Thompson static int
1006df4b8c2aSAndrew Thompson ugen20_root_set_template(struct libusb20_backend *pbe, int temp)
1007df4b8c2aSAndrew Thompson {
1008df4b8c2aSAndrew Thompson 	return (ugen20_be_ioctl(USB_SET_TEMPLATE, &temp));
1009df4b8c2aSAndrew Thompson }
1010df4b8c2aSAndrew Thompson 
1011df4b8c2aSAndrew Thompson static int
1012df4b8c2aSAndrew Thompson ugen20_root_get_template(struct libusb20_backend *pbe, int *ptemp)
1013df4b8c2aSAndrew Thompson {
1014df4b8c2aSAndrew Thompson 	return (ugen20_be_ioctl(USB_GET_TEMPLATE, ptemp));
1015df4b8c2aSAndrew Thompson }
1016