xref: /freebsd/lib/libusb/libusb20_ugen20.c (revision 2485d8a793b882e77fe7be4dc1a034390c96abbb)
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 
198*2485d8a7SHans Petter Selasky 	/* get parent HUB index and port */
199*2485d8a7SHans Petter Selasky 
200*2485d8a7SHans Petter Selasky 	pdev->parent_address = devinfo.udi_hubindex;
201*2485d8a7SHans Petter Selasky 	pdev->parent_port = devinfo.udi_hubport;
202*2485d8a7SHans Petter Selasky 
203df4b8c2aSAndrew Thompson 	/* generate a nice description for printout */
204df4b8c2aSAndrew Thompson 
205df4b8c2aSAndrew Thompson 	snprintf(pdev->usb_desc, sizeof(pdev->usb_desc),
206df4b8c2aSAndrew Thompson 	    USB_GENERIC_NAME "%u.%u: <%s %s> at usbus%u", pdev->bus_number,
207df4b8c2aSAndrew Thompson 	    pdev->device_address, devinfo.udi_product,
208df4b8c2aSAndrew Thompson 	    devinfo.udi_vendor, pdev->bus_number);
209df4b8c2aSAndrew Thompson 
210df4b8c2aSAndrew Thompson 	error = 0;
211df4b8c2aSAndrew Thompson done:
212df4b8c2aSAndrew Thompson 	close(f);
213df4b8c2aSAndrew Thompson 	return (error);
214df4b8c2aSAndrew Thompson }
215df4b8c2aSAndrew Thompson 
216df4b8c2aSAndrew Thompson struct ugen20_urd_state {
217760bc48eSAndrew Thompson 	struct usb_read_dir urd;
218df4b8c2aSAndrew Thompson 	uint32_t nparsed;
219df4b8c2aSAndrew Thompson 	int	f;
220df4b8c2aSAndrew Thompson 	uint8_t *ptr;
221df4b8c2aSAndrew Thompson 	const char *src;
222df4b8c2aSAndrew Thompson 	const char *dst;
223df4b8c2aSAndrew Thompson 	uint8_t	buf[256];
224df4b8c2aSAndrew Thompson 	uint8_t	dummy_zero[1];
225df4b8c2aSAndrew Thompson };
226df4b8c2aSAndrew Thompson 
227df4b8c2aSAndrew Thompson static int
228df4b8c2aSAndrew Thompson ugen20_readdir(struct ugen20_urd_state *st)
229df4b8c2aSAndrew Thompson {
230df4b8c2aSAndrew Thompson 	;				/* style fix */
231df4b8c2aSAndrew Thompson repeat:
232df4b8c2aSAndrew Thompson 	if (st->ptr == NULL) {
233df4b8c2aSAndrew Thompson 		st->urd.urd_startentry += st->nparsed;
23451fd3d75SHans Petter Selasky 		st->urd.urd_data = libusb20_pass_ptr(st->buf);
235df4b8c2aSAndrew Thompson 		st->urd.urd_maxlen = sizeof(st->buf);
236df4b8c2aSAndrew Thompson 		st->nparsed = 0;
237df4b8c2aSAndrew Thompson 
238df4b8c2aSAndrew Thompson 		if (ioctl(st->f, USB_READ_DIR, &st->urd)) {
239df4b8c2aSAndrew Thompson 			return (EINVAL);
240df4b8c2aSAndrew Thompson 		}
241df4b8c2aSAndrew Thompson 		st->ptr = st->buf;
242df4b8c2aSAndrew Thompson 	}
243df4b8c2aSAndrew Thompson 	if (st->ptr[0] == 0) {
244df4b8c2aSAndrew Thompson 		if (st->nparsed) {
245df4b8c2aSAndrew Thompson 			st->ptr = NULL;
246df4b8c2aSAndrew Thompson 			goto repeat;
247df4b8c2aSAndrew Thompson 		} else {
248df4b8c2aSAndrew Thompson 			return (ENXIO);
249df4b8c2aSAndrew Thompson 		}
250df4b8c2aSAndrew Thompson 	}
251df4b8c2aSAndrew Thompson 	st->src = (void *)(st->ptr + 1);
252df4b8c2aSAndrew Thompson 	st->dst = st->src + strlen(st->src) + 1;
253df4b8c2aSAndrew Thompson 	st->ptr = st->ptr + st->ptr[0];
254df4b8c2aSAndrew Thompson 	st->nparsed++;
255df4b8c2aSAndrew Thompson 
256df4b8c2aSAndrew Thompson 	if ((st->ptr < st->buf) ||
257df4b8c2aSAndrew Thompson 	    (st->ptr > st->dummy_zero)) {
258df4b8c2aSAndrew Thompson 		/* invalid entry */
259df4b8c2aSAndrew Thompson 		return (EINVAL);
260df4b8c2aSAndrew Thompson 	}
261df4b8c2aSAndrew Thompson 	return (0);
262df4b8c2aSAndrew Thompson }
263df4b8c2aSAndrew Thompson 
264df4b8c2aSAndrew Thompson static int
265df4b8c2aSAndrew Thompson ugen20_init_backend(struct libusb20_backend *pbe)
266df4b8c2aSAndrew Thompson {
267df4b8c2aSAndrew Thompson 	struct ugen20_urd_state state;
268df4b8c2aSAndrew Thompson 	struct libusb20_device *pdev;
269df4b8c2aSAndrew Thompson 
270df4b8c2aSAndrew Thompson 	memset(&state, 0, sizeof(state));
271df4b8c2aSAndrew Thompson 
272df4b8c2aSAndrew Thompson 	state.f = open("/dev/" USB_DEVICE_NAME, O_RDONLY);
273df4b8c2aSAndrew Thompson 	if (state.f < 0)
274df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
275df4b8c2aSAndrew Thompson 
276df4b8c2aSAndrew Thompson 	while (ugen20_readdir(&state) == 0) {
277df4b8c2aSAndrew Thompson 
278df4b8c2aSAndrew Thompson 		if ((state.src[0] != 'u') ||
279df4b8c2aSAndrew Thompson 		    (state.src[1] != 'g') ||
280df4b8c2aSAndrew Thompson 		    (state.src[2] != 'e') ||
281df4b8c2aSAndrew Thompson 		    (state.src[3] != 'n')) {
282df4b8c2aSAndrew Thompson 			continue;
283df4b8c2aSAndrew Thompson 		}
284df4b8c2aSAndrew Thompson 		pdev = libusb20_dev_alloc();
285df4b8c2aSAndrew Thompson 		if (pdev == NULL) {
286df4b8c2aSAndrew Thompson 			continue;
287df4b8c2aSAndrew Thompson 		}
288df4b8c2aSAndrew Thompson 		if (ugen20_enumerate(pdev, state.src + 4)) {
289df4b8c2aSAndrew Thompson 			libusb20_dev_free(pdev);
290df4b8c2aSAndrew Thompson 			continue;
291df4b8c2aSAndrew Thompson 		}
292df4b8c2aSAndrew Thompson 		/* put the device on the backend list */
293df4b8c2aSAndrew Thompson 		libusb20_be_enqueue_device(pbe, pdev);
294df4b8c2aSAndrew Thompson 	}
295df4b8c2aSAndrew Thompson 	close(state.f);
296df4b8c2aSAndrew Thompson 	return (0);			/* success */
297df4b8c2aSAndrew Thompson }
298df4b8c2aSAndrew Thompson 
299df4b8c2aSAndrew Thompson static void
300df4b8c2aSAndrew Thompson ugen20_tr_release(struct libusb20_device *pdev)
301df4b8c2aSAndrew Thompson {
302760bc48eSAndrew Thompson 	struct usb_fs_uninit fs_uninit;
303df4b8c2aSAndrew Thompson 
304df4b8c2aSAndrew Thompson 	if (pdev->nTransfer == 0) {
305df4b8c2aSAndrew Thompson 		return;
306df4b8c2aSAndrew Thompson 	}
307df4b8c2aSAndrew Thompson 	/* release all pending USB transfers */
308df4b8c2aSAndrew Thompson 	if (pdev->privBeData != NULL) {
309df4b8c2aSAndrew Thompson 		memset(&fs_uninit, 0, sizeof(fs_uninit));
310df4b8c2aSAndrew Thompson 		if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) {
311df4b8c2aSAndrew Thompson 			/* ignore any errors of this kind */
312df4b8c2aSAndrew Thompson 		}
313df4b8c2aSAndrew Thompson 	}
314df4b8c2aSAndrew Thompson 	return;
315df4b8c2aSAndrew Thompson }
316df4b8c2aSAndrew Thompson 
317df4b8c2aSAndrew Thompson static int
318df4b8c2aSAndrew Thompson ugen20_tr_renew(struct libusb20_device *pdev)
319df4b8c2aSAndrew Thompson {
320760bc48eSAndrew Thompson 	struct usb_fs_init fs_init;
321760bc48eSAndrew Thompson 	struct usb_fs_endpoint *pfse;
322df4b8c2aSAndrew Thompson 	int error;
323df4b8c2aSAndrew Thompson 	uint32_t size;
324df4b8c2aSAndrew Thompson 	uint16_t nMaxTransfer;
325df4b8c2aSAndrew Thompson 
326df4b8c2aSAndrew Thompson 	nMaxTransfer = pdev->nTransfer;
327df4b8c2aSAndrew Thompson 	error = 0;
328df4b8c2aSAndrew Thompson 
329df4b8c2aSAndrew Thompson 	if (nMaxTransfer == 0) {
330df4b8c2aSAndrew Thompson 		goto done;
331df4b8c2aSAndrew Thompson 	}
332df4b8c2aSAndrew Thompson 	size = nMaxTransfer * sizeof(*pfse);
333df4b8c2aSAndrew Thompson 
334df4b8c2aSAndrew Thompson 	if (pdev->privBeData == NULL) {
335df4b8c2aSAndrew Thompson 		pfse = malloc(size);
336df4b8c2aSAndrew Thompson 		if (pfse == NULL) {
337df4b8c2aSAndrew Thompson 			error = LIBUSB20_ERROR_NO_MEM;
338df4b8c2aSAndrew Thompson 			goto done;
339df4b8c2aSAndrew Thompson 		}
340df4b8c2aSAndrew Thompson 		pdev->privBeData = pfse;
341df4b8c2aSAndrew Thompson 	}
342df4b8c2aSAndrew Thompson 	/* reset endpoint data */
343df4b8c2aSAndrew Thompson 	memset(pdev->privBeData, 0, size);
344df4b8c2aSAndrew Thompson 
345df4b8c2aSAndrew Thompson 	memset(&fs_init, 0, sizeof(fs_init));
346df4b8c2aSAndrew Thompson 
34751fd3d75SHans Petter Selasky 	fs_init.pEndpoints = libusb20_pass_ptr(pdev->privBeData);
348df4b8c2aSAndrew Thompson 	fs_init.ep_index_max = nMaxTransfer;
349df4b8c2aSAndrew Thompson 
350df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file, USB_FS_INIT, &fs_init)) {
351df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
352df4b8c2aSAndrew Thompson 		goto done;
353df4b8c2aSAndrew Thompson 	}
354df4b8c2aSAndrew Thompson done:
355df4b8c2aSAndrew Thompson 	return (error);
356df4b8c2aSAndrew Thompson }
357df4b8c2aSAndrew Thompson 
358df4b8c2aSAndrew Thompson static int
359df4b8c2aSAndrew Thompson ugen20_open_device(struct libusb20_device *pdev, uint16_t nMaxTransfer)
360df4b8c2aSAndrew Thompson {
361df4b8c2aSAndrew Thompson 	uint32_t plugtime;
362df4b8c2aSAndrew Thompson 	char buf[64];
363df4b8c2aSAndrew Thompson 	int f;
364df4b8c2aSAndrew Thompson 	int g;
365df4b8c2aSAndrew Thompson 	int error;
366df4b8c2aSAndrew Thompson 
367df4b8c2aSAndrew Thompson 	snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u",
368df4b8c2aSAndrew Thompson 	    pdev->bus_number, pdev->device_address);
369df4b8c2aSAndrew Thompson 
370df4b8c2aSAndrew Thompson 	/*
371df4b8c2aSAndrew Thompson 	 * We need two file handles, one for the control endpoint and one
372df4b8c2aSAndrew Thompson 	 * for BULK, INTERRUPT and ISOCHRONOUS transactions due to optimised
373df4b8c2aSAndrew Thompson 	 * kernel locking.
374df4b8c2aSAndrew Thompson 	 */
375df4b8c2aSAndrew Thompson 	g = open(buf, O_RDWR);
376df4b8c2aSAndrew Thompson 	if (g < 0) {
377df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_NO_DEVICE);
378df4b8c2aSAndrew Thompson 	}
379df4b8c2aSAndrew Thompson 	f = open(buf, O_RDWR);
380df4b8c2aSAndrew Thompson 	if (f < 0) {
381df4b8c2aSAndrew Thompson 		close(g);
382df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_NO_DEVICE);
383df4b8c2aSAndrew Thompson 	}
384df4b8c2aSAndrew Thompson 	if (ioctl(f, USB_GET_PLUGTIME, &plugtime)) {
385df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_OTHER;
386df4b8c2aSAndrew Thompson 		goto done;
387df4b8c2aSAndrew Thompson 	}
388df4b8c2aSAndrew Thompson 	/* check that the correct device is still plugged */
389df4b8c2aSAndrew Thompson 	if (pdev->session_data.plugtime != plugtime) {
390df4b8c2aSAndrew Thompson 		error = LIBUSB20_ERROR_NO_DEVICE;
391df4b8c2aSAndrew Thompson 		goto done;
392df4b8c2aSAndrew Thompson 	}
393df4b8c2aSAndrew Thompson 	/* need to set this before "tr_renew()" */
394df4b8c2aSAndrew Thompson 	pdev->file = f;
395df4b8c2aSAndrew Thompson 	pdev->file_ctrl = g;
396df4b8c2aSAndrew Thompson 
397df4b8c2aSAndrew Thompson 	/* renew all USB transfers */
398df4b8c2aSAndrew Thompson 	error = ugen20_tr_renew(pdev);
399df4b8c2aSAndrew Thompson 	if (error) {
400df4b8c2aSAndrew Thompson 		goto done;
401df4b8c2aSAndrew Thompson 	}
402df4b8c2aSAndrew Thompson 	/* set methods */
403df4b8c2aSAndrew Thompson 	pdev->methods = &libusb20_ugen20_device_methods;
404df4b8c2aSAndrew Thompson 
405df4b8c2aSAndrew Thompson done:
406df4b8c2aSAndrew Thompson 	if (error) {
407df4b8c2aSAndrew Thompson 		if (pdev->privBeData) {
408df4b8c2aSAndrew Thompson 			/* cleanup after "tr_renew()" */
409df4b8c2aSAndrew Thompson 			free(pdev->privBeData);
410df4b8c2aSAndrew Thompson 			pdev->privBeData = NULL;
411df4b8c2aSAndrew Thompson 		}
412df4b8c2aSAndrew Thompson 		pdev->file = -1;
413df4b8c2aSAndrew Thompson 		pdev->file_ctrl = -1;
414df4b8c2aSAndrew Thompson 		close(f);
415df4b8c2aSAndrew Thompson 		close(g);
416df4b8c2aSAndrew Thompson 	}
417df4b8c2aSAndrew Thompson 	return (error);
418df4b8c2aSAndrew Thompson }
419df4b8c2aSAndrew Thompson 
420df4b8c2aSAndrew Thompson static int
421df4b8c2aSAndrew Thompson ugen20_close_device(struct libusb20_device *pdev)
422df4b8c2aSAndrew Thompson {
423760bc48eSAndrew Thompson 	struct usb_fs_uninit fs_uninit;
424df4b8c2aSAndrew Thompson 
425df4b8c2aSAndrew Thompson 	if (pdev->privBeData) {
426df4b8c2aSAndrew Thompson 		memset(&fs_uninit, 0, sizeof(fs_uninit));
427df4b8c2aSAndrew Thompson 		if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) {
428df4b8c2aSAndrew Thompson 			/* ignore this error */
429df4b8c2aSAndrew Thompson 		}
430df4b8c2aSAndrew Thompson 		free(pdev->privBeData);
431df4b8c2aSAndrew Thompson 	}
432df4b8c2aSAndrew Thompson 	pdev->nTransfer = 0;
433df4b8c2aSAndrew Thompson 	pdev->privBeData = NULL;
434df4b8c2aSAndrew Thompson 	close(pdev->file);
435df4b8c2aSAndrew Thompson 	close(pdev->file_ctrl);
436df4b8c2aSAndrew Thompson 	pdev->file = -1;
437df4b8c2aSAndrew Thompson 	pdev->file_ctrl = -1;
438df4b8c2aSAndrew Thompson 	return (0);			/* success */
439df4b8c2aSAndrew Thompson }
440df4b8c2aSAndrew Thompson 
441df4b8c2aSAndrew Thompson static void
442df4b8c2aSAndrew Thompson ugen20_exit_backend(struct libusb20_backend *pbe)
443df4b8c2aSAndrew Thompson {
444df4b8c2aSAndrew Thompson 	return;				/* nothing to do */
445df4b8c2aSAndrew Thompson }
446df4b8c2aSAndrew Thompson 
447df4b8c2aSAndrew Thompson static int
448df4b8c2aSAndrew Thompson ugen20_get_config_desc_full(struct libusb20_device *pdev,
449df4b8c2aSAndrew Thompson     uint8_t **ppbuf, uint16_t *plen, uint8_t cfg_index)
450df4b8c2aSAndrew Thompson {
451760bc48eSAndrew Thompson 	struct usb_gen_descriptor gen_desc;
452760bc48eSAndrew Thompson 	struct usb_config_descriptor cdesc;
453df4b8c2aSAndrew Thompson 	uint8_t *ptr;
454df4b8c2aSAndrew Thompson 	uint16_t len;
455df4b8c2aSAndrew Thompson 	int error;
456df4b8c2aSAndrew Thompson 
457ccef4ddfSAndrew Thompson 	/* make sure memory is initialised */
458ccef4ddfSAndrew Thompson 	memset(&cdesc, 0, sizeof(cdesc));
459df4b8c2aSAndrew Thompson 	memset(&gen_desc, 0, sizeof(gen_desc));
460df4b8c2aSAndrew Thompson 
46151fd3d75SHans Petter Selasky 	gen_desc.ugd_data = libusb20_pass_ptr(&cdesc);
462df4b8c2aSAndrew Thompson 	gen_desc.ugd_maxlen = sizeof(cdesc);
463df4b8c2aSAndrew Thompson 	gen_desc.ugd_config_index = cfg_index;
464df4b8c2aSAndrew Thompson 
465df4b8c2aSAndrew Thompson 	error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc);
466df4b8c2aSAndrew Thompson 	if (error) {
467df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
468df4b8c2aSAndrew Thompson 	}
469df4b8c2aSAndrew Thompson 	len = UGETW(cdesc.wTotalLength);
470df4b8c2aSAndrew Thompson 	if (len < sizeof(cdesc)) {
471df4b8c2aSAndrew Thompson 		/* corrupt descriptor */
472df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
473df4b8c2aSAndrew Thompson 	}
474df4b8c2aSAndrew Thompson 	ptr = malloc(len);
475df4b8c2aSAndrew Thompson 	if (!ptr) {
476df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_NO_MEM);
477df4b8c2aSAndrew Thompson 	}
478ccef4ddfSAndrew Thompson 
479ccef4ddfSAndrew Thompson 	/* make sure memory is initialised */
480ccef4ddfSAndrew Thompson 	memset(ptr, 0, len);
481ccef4ddfSAndrew Thompson 
48251fd3d75SHans Petter Selasky 	gen_desc.ugd_data = libusb20_pass_ptr(ptr);
483df4b8c2aSAndrew Thompson 	gen_desc.ugd_maxlen = len;
484df4b8c2aSAndrew Thompson 
485df4b8c2aSAndrew Thompson 	error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc);
486df4b8c2aSAndrew Thompson 	if (error) {
487df4b8c2aSAndrew Thompson 		free(ptr);
488df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
489df4b8c2aSAndrew Thompson 	}
490df4b8c2aSAndrew Thompson 	/* make sure that the device doesn't fool us */
491df4b8c2aSAndrew Thompson 	memcpy(ptr, &cdesc, sizeof(cdesc));
492df4b8c2aSAndrew Thompson 
493df4b8c2aSAndrew Thompson 	*ppbuf = ptr;
494df4b8c2aSAndrew Thompson 	*plen = len;
495df4b8c2aSAndrew Thompson 
496df4b8c2aSAndrew Thompson 	return (0);			/* success */
497df4b8c2aSAndrew Thompson }
498df4b8c2aSAndrew Thompson 
499df4b8c2aSAndrew Thompson static int
500df4b8c2aSAndrew Thompson ugen20_get_config_index(struct libusb20_device *pdev, uint8_t *pindex)
501df4b8c2aSAndrew Thompson {
502df4b8c2aSAndrew Thompson 	int temp;
503df4b8c2aSAndrew Thompson 
504df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_GET_CONFIG, &temp)) {
505df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
506df4b8c2aSAndrew Thompson 	}
507df4b8c2aSAndrew Thompson 	*pindex = temp;
508df4b8c2aSAndrew Thompson 
509df4b8c2aSAndrew Thompson 	return (0);
510df4b8c2aSAndrew Thompson }
511df4b8c2aSAndrew Thompson 
512df4b8c2aSAndrew Thompson static int
513df4b8c2aSAndrew Thompson ugen20_set_config_index(struct libusb20_device *pdev, uint8_t cfg_index)
514df4b8c2aSAndrew Thompson {
515df4b8c2aSAndrew Thompson 	int temp = cfg_index;
516df4b8c2aSAndrew Thompson 
517df4b8c2aSAndrew Thompson 	/* release all active USB transfers */
518df4b8c2aSAndrew Thompson 	ugen20_tr_release(pdev);
519df4b8c2aSAndrew Thompson 
520df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_SET_CONFIG, &temp)) {
521df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
522df4b8c2aSAndrew Thompson 	}
523df4b8c2aSAndrew Thompson 	return (ugen20_tr_renew(pdev));
524df4b8c2aSAndrew Thompson }
525df4b8c2aSAndrew Thompson 
526df4b8c2aSAndrew Thompson static int
527df4b8c2aSAndrew Thompson ugen20_set_alt_index(struct libusb20_device *pdev,
528df4b8c2aSAndrew Thompson     uint8_t iface_index, uint8_t alt_index)
529df4b8c2aSAndrew Thompson {
530760bc48eSAndrew Thompson 	struct usb_alt_interface alt_iface;
531df4b8c2aSAndrew Thompson 
532df4b8c2aSAndrew Thompson 	memset(&alt_iface, 0, sizeof(alt_iface));
533df4b8c2aSAndrew Thompson 
534df4b8c2aSAndrew Thompson 	alt_iface.uai_interface_index = iface_index;
535df4b8c2aSAndrew Thompson 	alt_iface.uai_alt_index = alt_index;
536df4b8c2aSAndrew Thompson 
537df4b8c2aSAndrew Thompson 	/* release all active USB transfers */
538df4b8c2aSAndrew Thompson 	ugen20_tr_release(pdev);
539df4b8c2aSAndrew Thompson 
540df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_SET_ALTINTERFACE, &alt_iface)) {
541df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
542df4b8c2aSAndrew Thompson 	}
543df4b8c2aSAndrew Thompson 	return (ugen20_tr_renew(pdev));
544df4b8c2aSAndrew Thompson }
545df4b8c2aSAndrew Thompson 
546df4b8c2aSAndrew Thompson static int
547df4b8c2aSAndrew Thompson ugen20_reset_device(struct libusb20_device *pdev)
548df4b8c2aSAndrew Thompson {
549df4b8c2aSAndrew Thompson 	int temp = 0;
550df4b8c2aSAndrew Thompson 
551df4b8c2aSAndrew Thompson 	/* release all active USB transfers */
552df4b8c2aSAndrew Thompson 	ugen20_tr_release(pdev);
553df4b8c2aSAndrew Thompson 
554df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_DEVICEENUMERATE, &temp)) {
555df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
556df4b8c2aSAndrew Thompson 	}
557df4b8c2aSAndrew Thompson 	return (ugen20_tr_renew(pdev));
558df4b8c2aSAndrew Thompson }
559df4b8c2aSAndrew Thompson 
560df4b8c2aSAndrew Thompson static int
561e50ac68bSAndrew Thompson ugen20_check_connected(struct libusb20_device *pdev)
562e50ac68bSAndrew Thompson {
563e50ac68bSAndrew Thompson 	uint32_t plugtime;
564e50ac68bSAndrew Thompson 	int error = 0;
565e50ac68bSAndrew Thompson 
566e50ac68bSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_GET_PLUGTIME, &plugtime)) {
567e50ac68bSAndrew Thompson 		error = LIBUSB20_ERROR_NO_DEVICE;
568e50ac68bSAndrew Thompson 		goto done;
569e50ac68bSAndrew Thompson 	}
570e50ac68bSAndrew Thompson 
571e50ac68bSAndrew Thompson 	if (pdev->session_data.plugtime != plugtime) {
572e50ac68bSAndrew Thompson 		error = LIBUSB20_ERROR_NO_DEVICE;
573e50ac68bSAndrew Thompson 		goto done;
574e50ac68bSAndrew Thompson 	}
575e50ac68bSAndrew Thompson done:
576e50ac68bSAndrew Thompson 	return (error);
577e50ac68bSAndrew Thompson }
578e50ac68bSAndrew Thompson 
579e50ac68bSAndrew Thompson static int
580df4b8c2aSAndrew Thompson ugen20_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
581df4b8c2aSAndrew Thompson {
582df4b8c2aSAndrew Thompson 	int temp;
583df4b8c2aSAndrew Thompson 
584df4b8c2aSAndrew Thompson 	switch (power_mode) {
585df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_OFF:
586df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_OFF;
587df4b8c2aSAndrew Thompson 		break;
588df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_ON:
589df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_ON;
590df4b8c2aSAndrew Thompson 		break;
591df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_SAVE:
592df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_SAVE;
593df4b8c2aSAndrew Thompson 		break;
594df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_SUSPEND:
595df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_SUSPEND;
596df4b8c2aSAndrew Thompson 		break;
597df4b8c2aSAndrew Thompson 	case LIBUSB20_POWER_RESUME:
598df4b8c2aSAndrew Thompson 		temp = USB_POWER_MODE_RESUME;
599df4b8c2aSAndrew Thompson 		break;
600df4b8c2aSAndrew Thompson 	default:
601df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
602df4b8c2aSAndrew Thompson 	}
603df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_SET_POWER_MODE, &temp)) {
604df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
605df4b8c2aSAndrew Thompson 	}
606df4b8c2aSAndrew Thompson 	return (0);
607df4b8c2aSAndrew Thompson }
608df4b8c2aSAndrew Thompson 
609df4b8c2aSAndrew Thompson static int
610df4b8c2aSAndrew Thompson ugen20_get_power_mode(struct libusb20_device *pdev, uint8_t *power_mode)
611df4b8c2aSAndrew Thompson {
612df4b8c2aSAndrew Thompson 	int temp;
613df4b8c2aSAndrew Thompson 
614df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_GET_POWER_MODE, &temp)) {
615df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
616df4b8c2aSAndrew Thompson 	}
617df4b8c2aSAndrew Thompson 	switch (temp) {
618df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_OFF:
619df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_OFF;
620df4b8c2aSAndrew Thompson 		break;
621df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_ON:
622df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_ON;
623df4b8c2aSAndrew Thompson 		break;
624df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_SAVE:
625df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_SAVE;
626df4b8c2aSAndrew Thompson 		break;
627df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_SUSPEND:
628df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_SUSPEND;
629df4b8c2aSAndrew Thompson 		break;
630df4b8c2aSAndrew Thompson 	case USB_POWER_MODE_RESUME:
631df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_RESUME;
632df4b8c2aSAndrew Thompson 		break;
633df4b8c2aSAndrew Thompson 	default:
634df4b8c2aSAndrew Thompson 		temp = LIBUSB20_POWER_ON;
635df4b8c2aSAndrew Thompson 		break;
636df4b8c2aSAndrew Thompson 	}
637df4b8c2aSAndrew Thompson 	*power_mode = temp;
638df4b8c2aSAndrew Thompson 	return (0);			/* success */
639df4b8c2aSAndrew Thompson }
640df4b8c2aSAndrew Thompson 
641df4b8c2aSAndrew Thompson static int
642df4b8c2aSAndrew Thompson ugen20_kernel_driver_active(struct libusb20_device *pdev,
643df4b8c2aSAndrew Thompson     uint8_t iface_index)
644df4b8c2aSAndrew Thompson {
645df4b8c2aSAndrew Thompson 	int temp = iface_index;
646df4b8c2aSAndrew Thompson 
647df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_ACTIVE, &temp)) {
648df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
649df4b8c2aSAndrew Thompson 	}
650df4b8c2aSAndrew Thompson 	return (0);			/* kernel driver is active */
651df4b8c2aSAndrew Thompson }
652df4b8c2aSAndrew Thompson 
653df4b8c2aSAndrew Thompson static int
654df4b8c2aSAndrew Thompson ugen20_detach_kernel_driver(struct libusb20_device *pdev,
655df4b8c2aSAndrew Thompson     uint8_t iface_index)
656df4b8c2aSAndrew Thompson {
657df4b8c2aSAndrew Thompson 	int temp = iface_index;
658df4b8c2aSAndrew Thompson 
659df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_DETACH, &temp)) {
660df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
661df4b8c2aSAndrew Thompson 	}
662df4b8c2aSAndrew Thompson 	return (0);			/* kernel driver is active */
663df4b8c2aSAndrew Thompson }
664df4b8c2aSAndrew Thompson 
665df4b8c2aSAndrew Thompson static int
666df4b8c2aSAndrew Thompson ugen20_do_request_sync(struct libusb20_device *pdev,
667df4b8c2aSAndrew Thompson     struct LIBUSB20_CONTROL_SETUP_DECODED *setup,
668df4b8c2aSAndrew Thompson     void *data, uint16_t *pactlen, uint32_t timeout, uint8_t flags)
669df4b8c2aSAndrew Thompson {
670760bc48eSAndrew Thompson 	struct usb_ctl_request req;
671df4b8c2aSAndrew Thompson 
672df4b8c2aSAndrew Thompson 	memset(&req, 0, sizeof(req));
673df4b8c2aSAndrew Thompson 
67451fd3d75SHans Petter Selasky 	req.ucr_data = libusb20_pass_ptr(data);
675df4b8c2aSAndrew Thompson 	if (!(flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) {
676df4b8c2aSAndrew Thompson 		req.ucr_flags |= USB_SHORT_XFER_OK;
677df4b8c2aSAndrew Thompson 	}
678df4b8c2aSAndrew Thompson 	if (libusb20_me_encode(&req.ucr_request,
679df4b8c2aSAndrew Thompson 	    sizeof(req.ucr_request), setup)) {
680df4b8c2aSAndrew Thompson 		/* ignore */
681df4b8c2aSAndrew Thompson 	}
682df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file_ctrl, USB_DO_REQUEST, &req)) {
683df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
684df4b8c2aSAndrew Thompson 	}
685df4b8c2aSAndrew Thompson 	if (pactlen) {
686df4b8c2aSAndrew Thompson 		/* get actual length */
687df4b8c2aSAndrew Thompson 		*pactlen = req.ucr_actlen;
688df4b8c2aSAndrew Thompson 	}
689df4b8c2aSAndrew Thompson 	return (0);			/* kernel driver is active */
690df4b8c2aSAndrew Thompson }
691df4b8c2aSAndrew Thompson 
692df4b8c2aSAndrew Thompson static int
693df4b8c2aSAndrew Thompson ugen20_process(struct libusb20_device *pdev)
694df4b8c2aSAndrew Thompson {
695760bc48eSAndrew Thompson 	struct usb_fs_complete temp;
696760bc48eSAndrew Thompson 	struct usb_fs_endpoint *fsep;
697df4b8c2aSAndrew Thompson 	struct libusb20_transfer *xfer;
698df4b8c2aSAndrew Thompson 
699df4b8c2aSAndrew Thompson 	while (1) {
700df4b8c2aSAndrew Thompson 
701df4b8c2aSAndrew Thompson 		if (ioctl(pdev->file, USB_FS_COMPLETE, &temp)) {
702df4b8c2aSAndrew Thompson 			if (errno == EBUSY) {
703df4b8c2aSAndrew Thompson 				break;
704df4b8c2aSAndrew Thompson 			} else {
705df4b8c2aSAndrew Thompson 				/* device detached */
706df4b8c2aSAndrew Thompson 				return (LIBUSB20_ERROR_OTHER);
707df4b8c2aSAndrew Thompson 			}
708df4b8c2aSAndrew Thompson 		}
709df4b8c2aSAndrew Thompson 		fsep = pdev->privBeData;
710df4b8c2aSAndrew Thompson 		xfer = pdev->pTransfer;
711df4b8c2aSAndrew Thompson 		fsep += temp.ep_index;
712df4b8c2aSAndrew Thompson 		xfer += temp.ep_index;
713df4b8c2aSAndrew Thompson 
714df4b8c2aSAndrew Thompson 		/* update transfer status */
715df4b8c2aSAndrew Thompson 
716df4b8c2aSAndrew Thompson 		if (fsep->status == 0) {
717df4b8c2aSAndrew Thompson 			xfer->aFrames = fsep->aFrames;
718df4b8c2aSAndrew Thompson 			xfer->timeComplete = fsep->isoc_time_complete;
719df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_COMPLETED;
720df4b8c2aSAndrew Thompson 		} else if (fsep->status == USB_ERR_CANCELLED) {
721df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
722df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
723df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_CANCELLED;
724df4b8c2aSAndrew Thompson 		} else if (fsep->status == USB_ERR_STALLED) {
725df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
726df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
727df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_STALL;
728df4b8c2aSAndrew Thompson 		} else if (fsep->status == USB_ERR_TIMEOUT) {
729df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
730df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
731df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_TIMED_OUT;
732df4b8c2aSAndrew Thompson 		} else {
733df4b8c2aSAndrew Thompson 			xfer->aFrames = 0;
734df4b8c2aSAndrew Thompson 			xfer->timeComplete = 0;
735df4b8c2aSAndrew Thompson 			xfer->status = LIBUSB20_TRANSFER_ERROR;
736df4b8c2aSAndrew Thompson 		}
737df4b8c2aSAndrew Thompson 		libusb20_tr_callback_wrapper(xfer);
738df4b8c2aSAndrew Thompson 	}
739df4b8c2aSAndrew Thompson 	return (0);			/* done */
740df4b8c2aSAndrew Thompson }
741df4b8c2aSAndrew Thompson 
742df4b8c2aSAndrew Thompson static int
743df4b8c2aSAndrew Thompson ugen20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
7441c497368SHans Petter Selasky     uint32_t MaxFrameCount, uint8_t ep_no, uint8_t pre_scale)
745df4b8c2aSAndrew Thompson {
746760bc48eSAndrew Thompson 	struct usb_fs_open temp;
747760bc48eSAndrew Thompson 	struct usb_fs_endpoint *fsep;
748df4b8c2aSAndrew Thompson 
7491c497368SHans Petter Selasky 	if (pre_scale)
7501c497368SHans Petter Selasky 		MaxFrameCount |= USB_FS_MAX_FRAMES_PRE_SCALE;
7511c497368SHans Petter Selasky 
752df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
753df4b8c2aSAndrew Thompson 
754df4b8c2aSAndrew Thompson 	fsep = xfer->pdev->privBeData;
755df4b8c2aSAndrew Thompson 	fsep += xfer->trIndex;
756df4b8c2aSAndrew Thompson 
757df4b8c2aSAndrew Thompson 	temp.max_bufsize = MaxBufSize;
758df4b8c2aSAndrew Thompson 	temp.max_frames = MaxFrameCount;
759df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
760df4b8c2aSAndrew Thompson 	temp.ep_no = ep_no;
761df4b8c2aSAndrew Thompson 
762df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_OPEN, &temp)) {
763df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
764df4b8c2aSAndrew Thompson 	}
765df4b8c2aSAndrew Thompson 	/* maximums might have changed - update */
766df4b8c2aSAndrew Thompson 	xfer->maxFrames = temp.max_frames;
767df4b8c2aSAndrew Thompson 
768df4b8c2aSAndrew Thompson 	/* "max_bufsize" should be multiple of "max_packet_length" */
769df4b8c2aSAndrew Thompson 	xfer->maxTotalLength = temp.max_bufsize;
770df4b8c2aSAndrew Thompson 	xfer->maxPacketLen = temp.max_packet_length;
771df4b8c2aSAndrew Thompson 
77251fd3d75SHans Petter Selasky 	/* setup buffer and length lists using zero copy */
77351fd3d75SHans Petter Selasky 	fsep->ppBuffer = libusb20_pass_ptr(xfer->ppBuffer);
77451fd3d75SHans Petter Selasky 	fsep->pLength = libusb20_pass_ptr(xfer->pLength);
775df4b8c2aSAndrew Thompson 
776df4b8c2aSAndrew Thompson 	return (0);			/* success */
777df4b8c2aSAndrew Thompson }
778df4b8c2aSAndrew Thompson 
779df4b8c2aSAndrew Thompson static int
780df4b8c2aSAndrew Thompson ugen20_tr_close(struct libusb20_transfer *xfer)
781df4b8c2aSAndrew Thompson {
782760bc48eSAndrew Thompson 	struct usb_fs_close temp;
783df4b8c2aSAndrew Thompson 
784df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
785df4b8c2aSAndrew Thompson 
786df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
787df4b8c2aSAndrew Thompson 
788df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_CLOSE, &temp)) {
789df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
790df4b8c2aSAndrew Thompson 	}
791df4b8c2aSAndrew Thompson 	return (0);			/* success */
792df4b8c2aSAndrew Thompson }
793df4b8c2aSAndrew Thompson 
794df4b8c2aSAndrew Thompson static int
795df4b8c2aSAndrew Thompson ugen20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
796df4b8c2aSAndrew Thompson {
797760bc48eSAndrew Thompson 	struct usb_fs_clear_stall_sync temp;
798df4b8c2aSAndrew Thompson 
799df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
800df4b8c2aSAndrew Thompson 
801df4b8c2aSAndrew Thompson 	/* if the transfer is active, an error will be returned */
802df4b8c2aSAndrew Thompson 
803df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
804df4b8c2aSAndrew Thompson 
805df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_CLEAR_STALL_SYNC, &temp)) {
806df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
807df4b8c2aSAndrew Thompson 	}
808df4b8c2aSAndrew Thompson 	return (0);			/* success */
809df4b8c2aSAndrew Thompson }
810df4b8c2aSAndrew Thompson 
811df4b8c2aSAndrew Thompson static void
812df4b8c2aSAndrew Thompson ugen20_tr_submit(struct libusb20_transfer *xfer)
813df4b8c2aSAndrew Thompson {
814760bc48eSAndrew Thompson 	struct usb_fs_start temp;
815760bc48eSAndrew Thompson 	struct usb_fs_endpoint *fsep;
816df4b8c2aSAndrew Thompson 
817df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
818df4b8c2aSAndrew Thompson 
819df4b8c2aSAndrew Thompson 	fsep = xfer->pdev->privBeData;
820df4b8c2aSAndrew Thompson 	fsep += xfer->trIndex;
821df4b8c2aSAndrew Thompson 
822df4b8c2aSAndrew Thompson 	fsep->nFrames = xfer->nFrames;
823df4b8c2aSAndrew Thompson 	fsep->flags = 0;
824df4b8c2aSAndrew Thompson 	if (!(xfer->flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) {
825df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_SINGLE_SHORT_OK;
826df4b8c2aSAndrew Thompson 	}
827df4b8c2aSAndrew Thompson 	if (!(xfer->flags & LIBUSB20_TRANSFER_MULTI_SHORT_NOT_OK)) {
828df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_MULTI_SHORT_OK;
829df4b8c2aSAndrew Thompson 	}
830df4b8c2aSAndrew Thompson 	if (xfer->flags & LIBUSB20_TRANSFER_FORCE_SHORT) {
831df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_FORCE_SHORT;
832df4b8c2aSAndrew Thompson 	}
833df4b8c2aSAndrew Thompson 	if (xfer->flags & LIBUSB20_TRANSFER_DO_CLEAR_STALL) {
834df4b8c2aSAndrew Thompson 		fsep->flags |= USB_FS_FLAG_CLEAR_STALL;
835df4b8c2aSAndrew Thompson 	}
8360c35eaadSAndrew Thompson 	/* NOTE: The "fsep->timeout" variable is 16-bit. */
8370c35eaadSAndrew Thompson 	if (xfer->timeout > 65535)
8380c35eaadSAndrew Thompson 		fsep->timeout = 65535;
8390c35eaadSAndrew Thompson 	else
840df4b8c2aSAndrew Thompson 		fsep->timeout = xfer->timeout;
841df4b8c2aSAndrew Thompson 
842df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
843df4b8c2aSAndrew Thompson 
844df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_START, &temp)) {
845df4b8c2aSAndrew Thompson 		/* ignore any errors - should never happen */
846df4b8c2aSAndrew Thompson 	}
847df4b8c2aSAndrew Thompson 	return;				/* success */
848df4b8c2aSAndrew Thompson }
849df4b8c2aSAndrew Thompson 
850df4b8c2aSAndrew Thompson static void
851df4b8c2aSAndrew Thompson ugen20_tr_cancel_async(struct libusb20_transfer *xfer)
852df4b8c2aSAndrew Thompson {
853760bc48eSAndrew Thompson 	struct usb_fs_stop temp;
854df4b8c2aSAndrew Thompson 
855df4b8c2aSAndrew Thompson 	memset(&temp, 0, sizeof(temp));
856df4b8c2aSAndrew Thompson 
857df4b8c2aSAndrew Thompson 	temp.ep_index = xfer->trIndex;
858df4b8c2aSAndrew Thompson 
859df4b8c2aSAndrew Thompson 	if (ioctl(xfer->pdev->file, USB_FS_STOP, &temp)) {
860df4b8c2aSAndrew Thompson 		/* ignore any errors - should never happen */
861df4b8c2aSAndrew Thompson 	}
862df4b8c2aSAndrew Thompson 	return;
863df4b8c2aSAndrew Thompson }
864df4b8c2aSAndrew Thompson 
865df4b8c2aSAndrew Thompson static int
866df4b8c2aSAndrew Thompson ugen20_be_ioctl(uint32_t cmd, void *data)
867df4b8c2aSAndrew Thompson {
868df4b8c2aSAndrew Thompson 	int f;
869df4b8c2aSAndrew Thompson 	int error;
870df4b8c2aSAndrew Thompson 
871df4b8c2aSAndrew Thompson 	f = open("/dev/" USB_DEVICE_NAME, O_RDONLY);
872df4b8c2aSAndrew Thompson 	if (f < 0)
873df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_OTHER);
874df4b8c2aSAndrew Thompson 	error = ioctl(f, cmd, data);
875df4b8c2aSAndrew Thompson 	if (error == -1) {
876df4b8c2aSAndrew Thompson 		if (errno == EPERM) {
877df4b8c2aSAndrew Thompson 			error = LIBUSB20_ERROR_ACCESS;
878df4b8c2aSAndrew Thompson 		} else {
879df4b8c2aSAndrew Thompson 			error = LIBUSB20_ERROR_OTHER;
880df4b8c2aSAndrew Thompson 		}
881df4b8c2aSAndrew Thompson 	}
882df4b8c2aSAndrew Thompson 	close(f);
883df4b8c2aSAndrew Thompson 	return (error);
884df4b8c2aSAndrew Thompson }
885df4b8c2aSAndrew Thompson 
886df4b8c2aSAndrew Thompson static int
887df4b8c2aSAndrew Thompson ugen20_dev_get_iface_desc(struct libusb20_device *pdev,
888df4b8c2aSAndrew Thompson     uint8_t iface_index, char *buf, uint8_t len)
889df4b8c2aSAndrew Thompson {
890760bc48eSAndrew Thompson 	struct usb_gen_descriptor ugd;
891df4b8c2aSAndrew Thompson 
892df4b8c2aSAndrew Thompson 	memset(&ugd, 0, sizeof(ugd));
893df4b8c2aSAndrew Thompson 
89451fd3d75SHans Petter Selasky 	ugd.ugd_data = libusb20_pass_ptr(buf);
895df4b8c2aSAndrew Thompson 	ugd.ugd_maxlen = len;
896df4b8c2aSAndrew Thompson 	ugd.ugd_iface_index = iface_index;
897df4b8c2aSAndrew Thompson 
898df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file, USB_GET_IFACE_DRIVER, &ugd)) {
899df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
900df4b8c2aSAndrew Thompson 	}
901df4b8c2aSAndrew Thompson 	return (0);
902df4b8c2aSAndrew Thompson }
903df4b8c2aSAndrew Thompson 
904df4b8c2aSAndrew Thompson static int
905df4b8c2aSAndrew Thompson ugen20_dev_get_info(struct libusb20_device *pdev,
906760bc48eSAndrew Thompson     struct usb_device_info *pinfo)
907df4b8c2aSAndrew Thompson {
908df4b8c2aSAndrew Thompson 	if (ioctl(pdev->file, USB_GET_DEVICEINFO, pinfo)) {
909df4b8c2aSAndrew Thompson 		return (LIBUSB20_ERROR_INVALID_PARAM);
910df4b8c2aSAndrew Thompson 	}
911df4b8c2aSAndrew Thompson 	return (0);
912df4b8c2aSAndrew Thompson }
913df4b8c2aSAndrew Thompson 
914df4b8c2aSAndrew Thompson static int
915df4b8c2aSAndrew Thompson ugen20_root_get_dev_quirk(struct libusb20_backend *pbe,
916df4b8c2aSAndrew Thompson     uint16_t quirk_index, struct libusb20_quirk *pq)
917df4b8c2aSAndrew Thompson {
918760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
919df4b8c2aSAndrew Thompson 	int error;
920df4b8c2aSAndrew Thompson 
921df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
922df4b8c2aSAndrew Thompson 
923df4b8c2aSAndrew Thompson 	q.index = quirk_index;
924df4b8c2aSAndrew Thompson 
925df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_DEV_QUIRK_GET, &q);
926df4b8c2aSAndrew Thompson 
927df4b8c2aSAndrew Thompson 	if (error) {
928df4b8c2aSAndrew Thompson 		if (errno == EINVAL) {
929df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NOT_FOUND);
930df4b8c2aSAndrew Thompson 		}
931df4b8c2aSAndrew Thompson 	} else {
932df4b8c2aSAndrew Thompson 		pq->vid = q.vid;
933df4b8c2aSAndrew Thompson 		pq->pid = q.pid;
934df4b8c2aSAndrew Thompson 		pq->bcdDeviceLow = q.bcdDeviceLow;
935df4b8c2aSAndrew Thompson 		pq->bcdDeviceHigh = q.bcdDeviceHigh;
936df4b8c2aSAndrew Thompson 		strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname));
937df4b8c2aSAndrew Thompson 	}
938df4b8c2aSAndrew Thompson 	return (error);
939df4b8c2aSAndrew Thompson }
940df4b8c2aSAndrew Thompson 
941df4b8c2aSAndrew Thompson static int
942df4b8c2aSAndrew Thompson ugen20_root_get_quirk_name(struct libusb20_backend *pbe, uint16_t quirk_index,
943df4b8c2aSAndrew Thompson     struct libusb20_quirk *pq)
944df4b8c2aSAndrew Thompson {
945760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
946df4b8c2aSAndrew Thompson 	int error;
947df4b8c2aSAndrew Thompson 
948df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
949df4b8c2aSAndrew Thompson 
950df4b8c2aSAndrew Thompson 	q.index = quirk_index;
951df4b8c2aSAndrew Thompson 
952df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_QUIRK_NAME_GET, &q);
953df4b8c2aSAndrew Thompson 
954df4b8c2aSAndrew Thompson 	if (error) {
955df4b8c2aSAndrew Thompson 		if (errno == EINVAL) {
956df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NOT_FOUND);
957df4b8c2aSAndrew Thompson 		}
958df4b8c2aSAndrew Thompson 	} else {
959df4b8c2aSAndrew Thompson 		strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname));
960df4b8c2aSAndrew Thompson 	}
961df4b8c2aSAndrew Thompson 	return (error);
962df4b8c2aSAndrew Thompson }
963df4b8c2aSAndrew Thompson 
964df4b8c2aSAndrew Thompson static int
965df4b8c2aSAndrew Thompson ugen20_root_add_dev_quirk(struct libusb20_backend *pbe,
966df4b8c2aSAndrew Thompson     struct libusb20_quirk *pq)
967df4b8c2aSAndrew Thompson {
968760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
969df4b8c2aSAndrew Thompson 	int error;
970df4b8c2aSAndrew Thompson 
971df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
972df4b8c2aSAndrew Thompson 
973df4b8c2aSAndrew Thompson 	q.vid = pq->vid;
974df4b8c2aSAndrew Thompson 	q.pid = pq->pid;
975df4b8c2aSAndrew Thompson 	q.bcdDeviceLow = pq->bcdDeviceLow;
976df4b8c2aSAndrew Thompson 	q.bcdDeviceHigh = pq->bcdDeviceHigh;
977df4b8c2aSAndrew Thompson 	strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname));
978df4b8c2aSAndrew Thompson 
979df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_DEV_QUIRK_ADD, &q);
980df4b8c2aSAndrew Thompson 	if (error) {
981df4b8c2aSAndrew Thompson 		if (errno == ENOMEM) {
982df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NO_MEM);
983df4b8c2aSAndrew Thompson 		}
984df4b8c2aSAndrew Thompson 	}
985df4b8c2aSAndrew Thompson 	return (error);
986df4b8c2aSAndrew Thompson }
987df4b8c2aSAndrew Thompson 
988df4b8c2aSAndrew Thompson static int
989df4b8c2aSAndrew Thompson ugen20_root_remove_dev_quirk(struct libusb20_backend *pbe,
990df4b8c2aSAndrew Thompson     struct libusb20_quirk *pq)
991df4b8c2aSAndrew Thompson {
992760bc48eSAndrew Thompson 	struct usb_gen_quirk q;
993df4b8c2aSAndrew Thompson 	int error;
994df4b8c2aSAndrew Thompson 
995df4b8c2aSAndrew Thompson 	memset(&q, 0, sizeof(q));
996df4b8c2aSAndrew Thompson 
997df4b8c2aSAndrew Thompson 	q.vid = pq->vid;
998df4b8c2aSAndrew Thompson 	q.pid = pq->pid;
999df4b8c2aSAndrew Thompson 	q.bcdDeviceLow = pq->bcdDeviceLow;
1000df4b8c2aSAndrew Thompson 	q.bcdDeviceHigh = pq->bcdDeviceHigh;
1001df4b8c2aSAndrew Thompson 	strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname));
1002df4b8c2aSAndrew Thompson 
1003df4b8c2aSAndrew Thompson 	error = ugen20_be_ioctl(USB_DEV_QUIRK_REMOVE, &q);
1004df4b8c2aSAndrew Thompson 	if (error) {
1005df4b8c2aSAndrew Thompson 		if (errno == EINVAL) {
1006df4b8c2aSAndrew Thompson 			return (LIBUSB20_ERROR_NOT_FOUND);
1007df4b8c2aSAndrew Thompson 		}
1008df4b8c2aSAndrew Thompson 	}
1009df4b8c2aSAndrew Thompson 	return (error);
1010df4b8c2aSAndrew Thompson }
1011df4b8c2aSAndrew Thompson 
1012df4b8c2aSAndrew Thompson static int
1013df4b8c2aSAndrew Thompson ugen20_root_set_template(struct libusb20_backend *pbe, int temp)
1014df4b8c2aSAndrew Thompson {
1015df4b8c2aSAndrew Thompson 	return (ugen20_be_ioctl(USB_SET_TEMPLATE, &temp));
1016df4b8c2aSAndrew Thompson }
1017df4b8c2aSAndrew Thompson 
1018df4b8c2aSAndrew Thompson static int
1019df4b8c2aSAndrew Thompson ugen20_root_get_template(struct libusb20_backend *pbe, int *ptemp)
1020df4b8c2aSAndrew Thompson {
1021df4b8c2aSAndrew Thompson 	return (ugen20_be_ioctl(USB_GET_TEMPLATE, ptemp));
1022df4b8c2aSAndrew Thompson }
1023