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