1df4b8c2aSAndrew Thompson /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
44594d907SAndrew Thompson * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
5df4b8c2aSAndrew Thompson *
6df4b8c2aSAndrew Thompson * Redistribution and use in source and binary forms, with or without
7df4b8c2aSAndrew Thompson * modification, are permitted provided that the following conditions
8df4b8c2aSAndrew Thompson * are met:
9df4b8c2aSAndrew Thompson * 1. Redistributions of source code must retain the above copyright
10df4b8c2aSAndrew Thompson * notice, this list of conditions and the following disclaimer.
11df4b8c2aSAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright
12df4b8c2aSAndrew Thompson * notice, this list of conditions and the following disclaimer in the
13df4b8c2aSAndrew Thompson * documentation and/or other materials provided with the distribution.
14df4b8c2aSAndrew Thompson *
15df4b8c2aSAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16df4b8c2aSAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17df4b8c2aSAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18df4b8c2aSAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19df4b8c2aSAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20df4b8c2aSAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21df4b8c2aSAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22df4b8c2aSAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23df4b8c2aSAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24df4b8c2aSAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25df4b8c2aSAndrew Thompson * SUCH DAMAGE.
26df4b8c2aSAndrew Thompson */
27df4b8c2aSAndrew Thompson
2866194130SHans Petter Selasky #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
2966194130SHans Petter Selasky #include LIBUSB_GLOBAL_INCLUDE_FILE
3066194130SHans Petter Selasky #else
31f3cba95cSWojciech A. Koszek #include <ctype.h>
32f3cba95cSWojciech A. Koszek #include <poll.h>
33df4b8c2aSAndrew Thompson #include <stdio.h>
34df4b8c2aSAndrew Thompson #include <stdlib.h>
35df4b8c2aSAndrew Thompson #include <string.h>
3666194130SHans Petter Selasky #include <time.h>
3766194130SHans Petter Selasky #include <sys/queue.h>
3866194130SHans Petter Selasky #endif
39df4b8c2aSAndrew Thompson
40df4b8c2aSAndrew Thompson #include "libusb20.h"
41df4b8c2aSAndrew Thompson #include "libusb20_desc.h"
42df4b8c2aSAndrew Thompson #include "libusb20_int.h"
43df4b8c2aSAndrew Thompson
44df4b8c2aSAndrew Thompson static int
dummy_int(void)45df4b8c2aSAndrew Thompson dummy_int(void)
46df4b8c2aSAndrew Thompson {
47df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NOT_SUPPORTED);
48df4b8c2aSAndrew Thompson }
49df4b8c2aSAndrew Thompson
50df4b8c2aSAndrew Thompson static void
dummy_void(void)51df4b8c2aSAndrew Thompson dummy_void(void)
52df4b8c2aSAndrew Thompson {
53df4b8c2aSAndrew Thompson return;
54df4b8c2aSAndrew Thompson }
55df4b8c2aSAndrew Thompson
56df4b8c2aSAndrew Thompson static void
dummy_callback(struct libusb20_transfer * xfer)57df4b8c2aSAndrew Thompson dummy_callback(struct libusb20_transfer *xfer)
58df4b8c2aSAndrew Thompson {
59df4b8c2aSAndrew Thompson ; /* style fix */
60df4b8c2aSAndrew Thompson switch (libusb20_tr_get_status(xfer)) {
61df4b8c2aSAndrew Thompson case LIBUSB20_TRANSFER_START:
62df4b8c2aSAndrew Thompson libusb20_tr_submit(xfer);
63df4b8c2aSAndrew Thompson break;
64df4b8c2aSAndrew Thompson default:
65df4b8c2aSAndrew Thompson /* complete or error */
66df4b8c2aSAndrew Thompson break;
67df4b8c2aSAndrew Thompson }
68df4b8c2aSAndrew Thompson return;
69df4b8c2aSAndrew Thompson }
70df4b8c2aSAndrew Thompson
71df4b8c2aSAndrew Thompson #define dummy_get_config_desc_full (void *)dummy_int
72df4b8c2aSAndrew Thompson #define dummy_get_config_index (void *)dummy_int
73df4b8c2aSAndrew Thompson #define dummy_set_config_index (void *)dummy_int
74df4b8c2aSAndrew Thompson #define dummy_set_alt_index (void *)dummy_int
75df4b8c2aSAndrew Thompson #define dummy_reset_device (void *)dummy_int
76e50ac68bSAndrew Thompson #define dummy_check_connected (void *)dummy_int
77df4b8c2aSAndrew Thompson #define dummy_set_power_mode (void *)dummy_int
78df4b8c2aSAndrew Thompson #define dummy_get_power_mode (void *)dummy_int
79aafcb732SHans Petter Selasky #define dummy_get_power_usage (void *)dummy_int
8034b0ca24SHans Petter Selasky #define dummy_get_stats (void *)dummy_int
81df4b8c2aSAndrew Thompson #define dummy_kernel_driver_active (void *)dummy_int
82df4b8c2aSAndrew Thompson #define dummy_detach_kernel_driver (void *)dummy_int
83df4b8c2aSAndrew Thompson #define dummy_do_request_sync (void *)dummy_int
84df4b8c2aSAndrew Thompson #define dummy_tr_open (void *)dummy_int
85df4b8c2aSAndrew Thompson #define dummy_tr_close (void *)dummy_int
86df4b8c2aSAndrew Thompson #define dummy_tr_clear_stall_sync (void *)dummy_int
87df4b8c2aSAndrew Thompson #define dummy_process (void *)dummy_int
88df4b8c2aSAndrew Thompson #define dummy_dev_info (void *)dummy_int
89df4b8c2aSAndrew Thompson #define dummy_dev_get_iface_driver (void *)dummy_int
90df4b8c2aSAndrew Thompson
91df4b8c2aSAndrew Thompson #define dummy_tr_submit (void *)dummy_void
92df4b8c2aSAndrew Thompson #define dummy_tr_cancel_async (void *)dummy_void
93df4b8c2aSAndrew Thompson
94df4b8c2aSAndrew Thompson static const struct libusb20_device_methods libusb20_dummy_methods = {
95df4b8c2aSAndrew Thompson LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
96df4b8c2aSAndrew Thompson };
97df4b8c2aSAndrew Thompson
98df4b8c2aSAndrew Thompson void
libusb20_tr_callback_wrapper(struct libusb20_transfer * xfer)99df4b8c2aSAndrew Thompson libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
100df4b8c2aSAndrew Thompson {
101df4b8c2aSAndrew Thompson ; /* style fix */
102df4b8c2aSAndrew Thompson
103df4b8c2aSAndrew Thompson repeat:
104df4b8c2aSAndrew Thompson
105df4b8c2aSAndrew Thompson if (!xfer->is_pending) {
106df4b8c2aSAndrew Thompson xfer->status = LIBUSB20_TRANSFER_START;
107df4b8c2aSAndrew Thompson } else {
108df4b8c2aSAndrew Thompson xfer->is_pending = 0;
109df4b8c2aSAndrew Thompson }
110df4b8c2aSAndrew Thompson
111df4b8c2aSAndrew Thompson xfer->callback(xfer);
112df4b8c2aSAndrew Thompson
113df4b8c2aSAndrew Thompson if (xfer->is_restart) {
114df4b8c2aSAndrew Thompson xfer->is_restart = 0;
115df4b8c2aSAndrew Thompson goto repeat;
116df4b8c2aSAndrew Thompson }
117df4b8c2aSAndrew Thompson if (xfer->is_draining &&
118df4b8c2aSAndrew Thompson (!xfer->is_pending)) {
119df4b8c2aSAndrew Thompson xfer->is_draining = 0;
120df4b8c2aSAndrew Thompson xfer->status = LIBUSB20_TRANSFER_DRAINED;
121df4b8c2aSAndrew Thompson xfer->callback(xfer);
122df4b8c2aSAndrew Thompson }
123df4b8c2aSAndrew Thompson return;
124df4b8c2aSAndrew Thompson }
125df4b8c2aSAndrew Thompson
126df4b8c2aSAndrew Thompson int
libusb20_tr_close(struct libusb20_transfer * xfer)127df4b8c2aSAndrew Thompson libusb20_tr_close(struct libusb20_transfer *xfer)
128df4b8c2aSAndrew Thompson {
129df4b8c2aSAndrew Thompson int error;
130df4b8c2aSAndrew Thompson
131df4b8c2aSAndrew Thompson if (!xfer->is_opened) {
132df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER);
133df4b8c2aSAndrew Thompson }
134df4b8c2aSAndrew Thompson error = xfer->pdev->methods->tr_close(xfer);
135df4b8c2aSAndrew Thompson
136df4b8c2aSAndrew Thompson if (xfer->pLength) {
137df4b8c2aSAndrew Thompson free(xfer->pLength);
138df4b8c2aSAndrew Thompson }
139df4b8c2aSAndrew Thompson if (xfer->ppBuffer) {
140df4b8c2aSAndrew Thompson free(xfer->ppBuffer);
141df4b8c2aSAndrew Thompson }
142c740e8e4SAndrew Thompson /* reset variable fields in case the transfer is opened again */
1432bf49386SPedro F. Giffuni xfer->priv_sc0 = NULL;
1442bf49386SPedro F. Giffuni xfer->priv_sc1 = NULL;
145df4b8c2aSAndrew Thompson xfer->is_opened = 0;
146c740e8e4SAndrew Thompson xfer->is_pending = 0;
147c740e8e4SAndrew Thompson xfer->is_cancel = 0;
148c740e8e4SAndrew Thompson xfer->is_draining = 0;
149c740e8e4SAndrew Thompson xfer->is_restart = 0;
150c740e8e4SAndrew Thompson xfer->status = 0;
151c740e8e4SAndrew Thompson xfer->flags = 0;
152c740e8e4SAndrew Thompson xfer->nFrames = 0;
153c740e8e4SAndrew Thompson xfer->aFrames = 0;
154c740e8e4SAndrew Thompson xfer->timeout = 0;
155df4b8c2aSAndrew Thompson xfer->maxFrames = 0;
156df4b8c2aSAndrew Thompson xfer->maxTotalLength = 0;
157df4b8c2aSAndrew Thompson xfer->maxPacketLen = 0;
158df4b8c2aSAndrew Thompson return (error);
159df4b8c2aSAndrew Thompson }
160df4b8c2aSAndrew Thompson
161df4b8c2aSAndrew Thompson int
libusb20_tr_open(struct libusb20_transfer * xfer,uint32_t MaxBufSize,uint32_t MaxFrameCount,uint8_t ep_no)162df4b8c2aSAndrew Thompson libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
163df4b8c2aSAndrew Thompson uint32_t MaxFrameCount, uint8_t ep_no)
164df4b8c2aSAndrew Thompson {
16507b6ce3bSHans Petter Selasky return (libusb20_tr_open_stream(xfer, MaxBufSize, MaxFrameCount, ep_no, 0));
16607b6ce3bSHans Petter Selasky }
16707b6ce3bSHans Petter Selasky
16807b6ce3bSHans Petter Selasky int
libusb20_tr_open_stream(struct libusb20_transfer * xfer,uint32_t MaxBufSize,uint32_t MaxFrameCount,uint8_t ep_no,uint16_t stream_id)16907b6ce3bSHans Petter Selasky libusb20_tr_open_stream(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
17007b6ce3bSHans Petter Selasky uint32_t MaxFrameCount, uint8_t ep_no, uint16_t stream_id)
17107b6ce3bSHans Petter Selasky {
172df4b8c2aSAndrew Thompson uint32_t size;
1731c497368SHans Petter Selasky uint8_t pre_scale;
174df4b8c2aSAndrew Thompson int error;
175df4b8c2aSAndrew Thompson
1761c497368SHans Petter Selasky if (xfer->is_opened)
177df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_BUSY);
1781c497368SHans Petter Selasky if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
1791c497368SHans Petter Selasky MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
1805f518148SHans Petter Selasky /*
1815f518148SHans Petter Selasky * The kernel can setup 8 times more frames when
1825f518148SHans Petter Selasky * pre-scaling ISOCHRONOUS transfers. Make sure the
1835f518148SHans Petter Selasky * length and pointer buffers are big enough:
1845f518148SHans Petter Selasky */
1855f518148SHans Petter Selasky MaxFrameCount *= 8;
1861c497368SHans Petter Selasky pre_scale = 1;
1871c497368SHans Petter Selasky } else {
1881c497368SHans Petter Selasky pre_scale = 0;
189df4b8c2aSAndrew Thompson }
1901c497368SHans Petter Selasky if (MaxFrameCount == 0)
191df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM);
1921c497368SHans Petter Selasky
193df4b8c2aSAndrew Thompson xfer->maxFrames = MaxFrameCount;
194df4b8c2aSAndrew Thompson
195df4b8c2aSAndrew Thompson size = MaxFrameCount * sizeof(xfer->pLength[0]);
196df4b8c2aSAndrew Thompson xfer->pLength = malloc(size);
197df4b8c2aSAndrew Thompson if (xfer->pLength == NULL) {
198df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NO_MEM);
199df4b8c2aSAndrew Thompson }
200df4b8c2aSAndrew Thompson memset(xfer->pLength, 0, size);
201df4b8c2aSAndrew Thompson
202df4b8c2aSAndrew Thompson size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
203df4b8c2aSAndrew Thompson xfer->ppBuffer = malloc(size);
204df4b8c2aSAndrew Thompson if (xfer->ppBuffer == NULL) {
205df4b8c2aSAndrew Thompson free(xfer->pLength);
206df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NO_MEM);
207df4b8c2aSAndrew Thompson }
208df4b8c2aSAndrew Thompson memset(xfer->ppBuffer, 0, size);
209df4b8c2aSAndrew Thompson
2105f518148SHans Petter Selasky if (pre_scale) {
211df4b8c2aSAndrew Thompson error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
2125f518148SHans Petter Selasky MaxFrameCount / 8, ep_no, stream_id, 1);
2135f518148SHans Petter Selasky } else {
2145f518148SHans Petter Selasky error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
2155f518148SHans Petter Selasky MaxFrameCount, ep_no, stream_id, 0);
2165f518148SHans Petter Selasky }
217df4b8c2aSAndrew Thompson
218df4b8c2aSAndrew Thompson if (error) {
219df4b8c2aSAndrew Thompson free(xfer->ppBuffer);
220df4b8c2aSAndrew Thompson free(xfer->pLength);
221df4b8c2aSAndrew Thompson } else {
222df4b8c2aSAndrew Thompson xfer->is_opened = 1;
223df4b8c2aSAndrew Thompson }
224df4b8c2aSAndrew Thompson return (error);
225df4b8c2aSAndrew Thompson }
226df4b8c2aSAndrew Thompson
227df4b8c2aSAndrew Thompson struct libusb20_transfer *
libusb20_tr_get_pointer(struct libusb20_device * pdev,uint16_t trIndex)228df4b8c2aSAndrew Thompson libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
229df4b8c2aSAndrew Thompson {
230df4b8c2aSAndrew Thompson if (trIndex >= pdev->nTransfer) {
231df4b8c2aSAndrew Thompson return (NULL);
232df4b8c2aSAndrew Thompson }
233df4b8c2aSAndrew Thompson return (pdev->pTransfer + trIndex);
234df4b8c2aSAndrew Thompson }
235df4b8c2aSAndrew Thompson
236df4b8c2aSAndrew Thompson uint32_t
libusb20_tr_get_actual_frames(struct libusb20_transfer * xfer)237df4b8c2aSAndrew Thompson libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
238df4b8c2aSAndrew Thompson {
239df4b8c2aSAndrew Thompson return (xfer->aFrames);
240df4b8c2aSAndrew Thompson }
241df4b8c2aSAndrew Thompson
242df4b8c2aSAndrew Thompson uint16_t
libusb20_tr_get_time_complete(struct libusb20_transfer * xfer)243df4b8c2aSAndrew Thompson libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
244df4b8c2aSAndrew Thompson {
245df4b8c2aSAndrew Thompson return (xfer->timeComplete);
246df4b8c2aSAndrew Thompson }
247df4b8c2aSAndrew Thompson
248df4b8c2aSAndrew Thompson uint32_t
libusb20_tr_get_actual_length(struct libusb20_transfer * xfer)249df4b8c2aSAndrew Thompson libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
250df4b8c2aSAndrew Thompson {
251df4b8c2aSAndrew Thompson uint32_t x;
252df4b8c2aSAndrew Thompson uint32_t actlen = 0;
253df4b8c2aSAndrew Thompson
254df4b8c2aSAndrew Thompson for (x = 0; x != xfer->aFrames; x++) {
255df4b8c2aSAndrew Thompson actlen += xfer->pLength[x];
256df4b8c2aSAndrew Thompson }
257df4b8c2aSAndrew Thompson return (actlen);
258df4b8c2aSAndrew Thompson }
259df4b8c2aSAndrew Thompson
260df4b8c2aSAndrew Thompson uint32_t
libusb20_tr_get_max_frames(struct libusb20_transfer * xfer)261df4b8c2aSAndrew Thompson libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
262df4b8c2aSAndrew Thompson {
263df4b8c2aSAndrew Thompson return (xfer->maxFrames);
264df4b8c2aSAndrew Thompson }
265df4b8c2aSAndrew Thompson
266df4b8c2aSAndrew Thompson uint32_t
libusb20_tr_get_max_packet_length(struct libusb20_transfer * xfer)267df4b8c2aSAndrew Thompson libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
268df4b8c2aSAndrew Thompson {
269df4b8c2aSAndrew Thompson /*
270df4b8c2aSAndrew Thompson * Special Case NOTE: If the packet multiplier is non-zero for
271df4b8c2aSAndrew Thompson * High Speed USB, the value returned is equal to
272df4b8c2aSAndrew Thompson * "wMaxPacketSize * multiplier" !
273df4b8c2aSAndrew Thompson */
274df4b8c2aSAndrew Thompson return (xfer->maxPacketLen);
275df4b8c2aSAndrew Thompson }
276df4b8c2aSAndrew Thompson
277df4b8c2aSAndrew Thompson uint32_t
libusb20_tr_get_max_total_length(struct libusb20_transfer * xfer)278df4b8c2aSAndrew Thompson libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
279df4b8c2aSAndrew Thompson {
280df4b8c2aSAndrew Thompson return (xfer->maxTotalLength);
281df4b8c2aSAndrew Thompson }
282df4b8c2aSAndrew Thompson
283df4b8c2aSAndrew Thompson uint8_t
libusb20_tr_get_status(struct libusb20_transfer * xfer)284df4b8c2aSAndrew Thompson libusb20_tr_get_status(struct libusb20_transfer *xfer)
285df4b8c2aSAndrew Thompson {
286df4b8c2aSAndrew Thompson return (xfer->status);
287df4b8c2aSAndrew Thompson }
288df4b8c2aSAndrew Thompson
289df4b8c2aSAndrew Thompson uint8_t
libusb20_tr_pending(struct libusb20_transfer * xfer)290df4b8c2aSAndrew Thompson libusb20_tr_pending(struct libusb20_transfer *xfer)
291df4b8c2aSAndrew Thompson {
292df4b8c2aSAndrew Thompson return (xfer->is_pending);
293df4b8c2aSAndrew Thompson }
294df4b8c2aSAndrew Thompson
295df4b8c2aSAndrew Thompson void *
libusb20_tr_get_priv_sc0(struct libusb20_transfer * xfer)296df4b8c2aSAndrew Thompson libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
297df4b8c2aSAndrew Thompson {
298df4b8c2aSAndrew Thompson return (xfer->priv_sc0);
299df4b8c2aSAndrew Thompson }
300df4b8c2aSAndrew Thompson
301df4b8c2aSAndrew Thompson void *
libusb20_tr_get_priv_sc1(struct libusb20_transfer * xfer)302df4b8c2aSAndrew Thompson libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
303df4b8c2aSAndrew Thompson {
304df4b8c2aSAndrew Thompson return (xfer->priv_sc1);
305df4b8c2aSAndrew Thompson }
306df4b8c2aSAndrew Thompson
307df4b8c2aSAndrew Thompson void
libusb20_tr_stop(struct libusb20_transfer * xfer)308df4b8c2aSAndrew Thompson libusb20_tr_stop(struct libusb20_transfer *xfer)
309df4b8c2aSAndrew Thompson {
3104594d907SAndrew Thompson if (!xfer->is_opened) {
3114594d907SAndrew Thompson /* transfer is not opened */
3124594d907SAndrew Thompson return;
3134594d907SAndrew Thompson }
314df4b8c2aSAndrew Thompson if (!xfer->is_pending) {
315df4b8c2aSAndrew Thompson /* transfer not pending */
316df4b8c2aSAndrew Thompson return;
317df4b8c2aSAndrew Thompson }
318df4b8c2aSAndrew Thompson if (xfer->is_cancel) {
319df4b8c2aSAndrew Thompson /* already cancelling */
320df4b8c2aSAndrew Thompson return;
321df4b8c2aSAndrew Thompson }
322df4b8c2aSAndrew Thompson xfer->is_cancel = 1; /* we are cancelling */
323df4b8c2aSAndrew Thompson
324df4b8c2aSAndrew Thompson xfer->pdev->methods->tr_cancel_async(xfer);
325df4b8c2aSAndrew Thompson return;
326df4b8c2aSAndrew Thompson }
327df4b8c2aSAndrew Thompson
328df4b8c2aSAndrew Thompson void
libusb20_tr_drain(struct libusb20_transfer * xfer)329df4b8c2aSAndrew Thompson libusb20_tr_drain(struct libusb20_transfer *xfer)
330df4b8c2aSAndrew Thompson {
3314594d907SAndrew Thompson if (!xfer->is_opened) {
3324594d907SAndrew Thompson /* transfer is not opened */
3334594d907SAndrew Thompson return;
3344594d907SAndrew Thompson }
335df4b8c2aSAndrew Thompson /* make sure that we are cancelling */
336df4b8c2aSAndrew Thompson libusb20_tr_stop(xfer);
337df4b8c2aSAndrew Thompson
338df4b8c2aSAndrew Thompson if (xfer->is_pending) {
339df4b8c2aSAndrew Thompson xfer->is_draining = 1;
340df4b8c2aSAndrew Thompson }
341df4b8c2aSAndrew Thompson return;
342df4b8c2aSAndrew Thompson }
343df4b8c2aSAndrew Thompson
344df4b8c2aSAndrew Thompson void
libusb20_tr_clear_stall_sync(struct libusb20_transfer * xfer)345df4b8c2aSAndrew Thompson libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
346df4b8c2aSAndrew Thompson {
347df4b8c2aSAndrew Thompson xfer->pdev->methods->tr_clear_stall_sync(xfer);
348df4b8c2aSAndrew Thompson return;
349df4b8c2aSAndrew Thompson }
350df4b8c2aSAndrew Thompson
351df4b8c2aSAndrew Thompson void
libusb20_tr_set_buffer(struct libusb20_transfer * xfer,void * buffer,uint16_t frIndex)352df4b8c2aSAndrew Thompson libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
353df4b8c2aSAndrew Thompson {
3548010f4adSBrooks Davis xfer->ppBuffer[frIndex] = buffer;
355df4b8c2aSAndrew Thompson return;
356df4b8c2aSAndrew Thompson }
357df4b8c2aSAndrew Thompson
358df4b8c2aSAndrew Thompson void
libusb20_tr_set_callback(struct libusb20_transfer * xfer,libusb20_tr_callback_t * cb)359df4b8c2aSAndrew Thompson libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
360df4b8c2aSAndrew Thompson {
361df4b8c2aSAndrew Thompson xfer->callback = cb;
362df4b8c2aSAndrew Thompson return;
363df4b8c2aSAndrew Thompson }
364df4b8c2aSAndrew Thompson
365df4b8c2aSAndrew Thompson void
libusb20_tr_set_flags(struct libusb20_transfer * xfer,uint8_t flags)366df4b8c2aSAndrew Thompson libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
367df4b8c2aSAndrew Thompson {
368df4b8c2aSAndrew Thompson xfer->flags = flags;
369df4b8c2aSAndrew Thompson return;
370df4b8c2aSAndrew Thompson }
371df4b8c2aSAndrew Thompson
372545b01adSAndrew Thompson uint32_t
libusb20_tr_get_length(struct libusb20_transfer * xfer,uint16_t frIndex)373545b01adSAndrew Thompson libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
374545b01adSAndrew Thompson {
375545b01adSAndrew Thompson return (xfer->pLength[frIndex]);
376545b01adSAndrew Thompson }
377545b01adSAndrew Thompson
378df4b8c2aSAndrew Thompson void
libusb20_tr_set_length(struct libusb20_transfer * xfer,uint32_t length,uint16_t frIndex)379df4b8c2aSAndrew Thompson libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
380df4b8c2aSAndrew Thompson {
381df4b8c2aSAndrew Thompson xfer->pLength[frIndex] = length;
382df4b8c2aSAndrew Thompson return;
383df4b8c2aSAndrew Thompson }
384df4b8c2aSAndrew Thompson
385df4b8c2aSAndrew Thompson void
libusb20_tr_set_priv_sc0(struct libusb20_transfer * xfer,void * sc0)386df4b8c2aSAndrew Thompson libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
387df4b8c2aSAndrew Thompson {
388df4b8c2aSAndrew Thompson xfer->priv_sc0 = sc0;
389df4b8c2aSAndrew Thompson return;
390df4b8c2aSAndrew Thompson }
391df4b8c2aSAndrew Thompson
392df4b8c2aSAndrew Thompson void
libusb20_tr_set_priv_sc1(struct libusb20_transfer * xfer,void * sc1)393df4b8c2aSAndrew Thompson libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
394df4b8c2aSAndrew Thompson {
395df4b8c2aSAndrew Thompson xfer->priv_sc1 = sc1;
396df4b8c2aSAndrew Thompson return;
397df4b8c2aSAndrew Thompson }
398df4b8c2aSAndrew Thompson
399df4b8c2aSAndrew Thompson void
libusb20_tr_set_timeout(struct libusb20_transfer * xfer,uint32_t timeout)400df4b8c2aSAndrew Thompson libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
401df4b8c2aSAndrew Thompson {
402df4b8c2aSAndrew Thompson xfer->timeout = timeout;
403df4b8c2aSAndrew Thompson return;
404df4b8c2aSAndrew Thompson }
405df4b8c2aSAndrew Thompson
406df4b8c2aSAndrew Thompson void
libusb20_tr_set_total_frames(struct libusb20_transfer * xfer,uint32_t nFrames)407df4b8c2aSAndrew Thompson libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
408df4b8c2aSAndrew Thompson {
409df4b8c2aSAndrew Thompson if (nFrames > xfer->maxFrames) {
410df4b8c2aSAndrew Thompson /* should not happen */
411df4b8c2aSAndrew Thompson nFrames = xfer->maxFrames;
412df4b8c2aSAndrew Thompson }
413df4b8c2aSAndrew Thompson xfer->nFrames = nFrames;
414df4b8c2aSAndrew Thompson return;
415df4b8c2aSAndrew Thompson }
416df4b8c2aSAndrew Thompson
417df4b8c2aSAndrew Thompson void
libusb20_tr_setup_bulk(struct libusb20_transfer * xfer,void * pBuf,uint32_t length,uint32_t timeout)418df4b8c2aSAndrew Thompson libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
419df4b8c2aSAndrew Thompson {
4208010f4adSBrooks Davis xfer->ppBuffer[0] = pBuf;
421df4b8c2aSAndrew Thompson xfer->pLength[0] = length;
422df4b8c2aSAndrew Thompson xfer->timeout = timeout;
423df4b8c2aSAndrew Thompson xfer->nFrames = 1;
424df4b8c2aSAndrew Thompson return;
425df4b8c2aSAndrew Thompson }
426df4b8c2aSAndrew Thompson
427df4b8c2aSAndrew Thompson void
libusb20_tr_setup_control(struct libusb20_transfer * xfer,void * psetup,void * pBuf,uint32_t timeout)428df4b8c2aSAndrew Thompson libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
429df4b8c2aSAndrew Thompson {
430df4b8c2aSAndrew Thompson uint16_t len;
431df4b8c2aSAndrew Thompson
4328010f4adSBrooks Davis xfer->ppBuffer[0] = psetup;
433df4b8c2aSAndrew Thompson xfer->pLength[0] = 8; /* fixed */
434df4b8c2aSAndrew Thompson xfer->timeout = timeout;
435df4b8c2aSAndrew Thompson
436df4b8c2aSAndrew Thompson len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
437df4b8c2aSAndrew Thompson
438df4b8c2aSAndrew Thompson if (len != 0) {
439df4b8c2aSAndrew Thompson xfer->nFrames = 2;
4408010f4adSBrooks Davis xfer->ppBuffer[1] = pBuf;
441df4b8c2aSAndrew Thompson xfer->pLength[1] = len;
442df4b8c2aSAndrew Thompson } else {
443df4b8c2aSAndrew Thompson xfer->nFrames = 1;
444df4b8c2aSAndrew Thompson }
445df4b8c2aSAndrew Thompson return;
446df4b8c2aSAndrew Thompson }
447df4b8c2aSAndrew Thompson
448df4b8c2aSAndrew Thompson void
libusb20_tr_setup_intr(struct libusb20_transfer * xfer,void * pBuf,uint32_t length,uint32_t timeout)449df4b8c2aSAndrew Thompson libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
450df4b8c2aSAndrew Thompson {
4518010f4adSBrooks Davis xfer->ppBuffer[0] = pBuf;
452df4b8c2aSAndrew Thompson xfer->pLength[0] = length;
453df4b8c2aSAndrew Thompson xfer->timeout = timeout;
454df4b8c2aSAndrew Thompson xfer->nFrames = 1;
455df4b8c2aSAndrew Thompson return;
456df4b8c2aSAndrew Thompson }
457df4b8c2aSAndrew Thompson
458df4b8c2aSAndrew Thompson void
libusb20_tr_setup_isoc(struct libusb20_transfer * xfer,void * pBuf,uint32_t length,uint16_t frIndex)459df4b8c2aSAndrew Thompson libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
460df4b8c2aSAndrew Thompson {
461df4b8c2aSAndrew Thompson if (frIndex >= xfer->maxFrames) {
462df4b8c2aSAndrew Thompson /* should not happen */
463df4b8c2aSAndrew Thompson return;
464df4b8c2aSAndrew Thompson }
4658010f4adSBrooks Davis xfer->ppBuffer[frIndex] = pBuf;
466df4b8c2aSAndrew Thompson xfer->pLength[frIndex] = length;
467df4b8c2aSAndrew Thompson return;
468df4b8c2aSAndrew Thompson }
469df4b8c2aSAndrew Thompson
4704594d907SAndrew Thompson uint8_t
libusb20_tr_bulk_intr_sync(struct libusb20_transfer * xfer,void * pbuf,uint32_t length,uint32_t * pactlen,uint32_t timeout)4714594d907SAndrew Thompson libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
4724594d907SAndrew Thompson void *pbuf, uint32_t length, uint32_t *pactlen,
4734594d907SAndrew Thompson uint32_t timeout)
4744594d907SAndrew Thompson {
4754594d907SAndrew Thompson struct libusb20_device *pdev = xfer->pdev;
4764594d907SAndrew Thompson uint32_t transfer_max;
4774594d907SAndrew Thompson uint32_t transfer_act;
4784594d907SAndrew Thompson uint8_t retval;
4794594d907SAndrew Thompson
4804594d907SAndrew Thompson /* set some sensible default value */
4814594d907SAndrew Thompson if (pactlen != NULL)
4824594d907SAndrew Thompson *pactlen = 0;
4834594d907SAndrew Thompson
4844594d907SAndrew Thompson /* check for error condition */
4854594d907SAndrew Thompson if (libusb20_tr_pending(xfer))
4864594d907SAndrew Thompson return (LIBUSB20_ERROR_OTHER);
4874594d907SAndrew Thompson
4884594d907SAndrew Thompson do {
4894594d907SAndrew Thompson /* compute maximum transfer length */
4904594d907SAndrew Thompson transfer_max =
4914594d907SAndrew Thompson libusb20_tr_get_max_total_length(xfer);
4924594d907SAndrew Thompson
4934594d907SAndrew Thompson if (transfer_max > length)
4944594d907SAndrew Thompson transfer_max = length;
4954594d907SAndrew Thompson
4964594d907SAndrew Thompson /* setup bulk or interrupt transfer */
4974594d907SAndrew Thompson libusb20_tr_setup_bulk(xfer, pbuf,
4984594d907SAndrew Thompson transfer_max, timeout);
4994594d907SAndrew Thompson
5004594d907SAndrew Thompson /* start the transfer */
5014594d907SAndrew Thompson libusb20_tr_start(xfer);
5024594d907SAndrew Thompson
5034594d907SAndrew Thompson /* wait for transfer completion */
5044594d907SAndrew Thompson while (libusb20_dev_process(pdev) == 0) {
5054594d907SAndrew Thompson
5064594d907SAndrew Thompson if (libusb20_tr_pending(xfer) == 0)
5074594d907SAndrew Thompson break;
5084594d907SAndrew Thompson
5094594d907SAndrew Thompson libusb20_dev_wait_process(pdev, -1);
5104594d907SAndrew Thompson }
5114594d907SAndrew Thompson
5124594d907SAndrew Thompson transfer_act = libusb20_tr_get_actual_length(xfer);
5134594d907SAndrew Thompson
5144594d907SAndrew Thompson /* update actual length, if any */
5154594d907SAndrew Thompson if (pactlen != NULL)
5164594d907SAndrew Thompson pactlen[0] += transfer_act;
5174594d907SAndrew Thompson
5184594d907SAndrew Thompson /* check transfer status */
5194594d907SAndrew Thompson retval = libusb20_tr_get_status(xfer);
5204594d907SAndrew Thompson if (retval)
5214594d907SAndrew Thompson break;
5224594d907SAndrew Thompson
5234594d907SAndrew Thompson /* check for short transfer */
5244594d907SAndrew Thompson if (transfer_act != transfer_max)
5254594d907SAndrew Thompson break;
5264594d907SAndrew Thompson
5274594d907SAndrew Thompson /* update buffer pointer and length */
5284594d907SAndrew Thompson pbuf = ((uint8_t *)pbuf) + transfer_max;
5294594d907SAndrew Thompson length = length - transfer_max;
5304594d907SAndrew Thompson
5314594d907SAndrew Thompson } while (length != 0);
5324594d907SAndrew Thompson
5334594d907SAndrew Thompson return (retval);
5344594d907SAndrew Thompson }
5354594d907SAndrew Thompson
536df4b8c2aSAndrew Thompson void
libusb20_tr_submit(struct libusb20_transfer * xfer)537df4b8c2aSAndrew Thompson libusb20_tr_submit(struct libusb20_transfer *xfer)
538df4b8c2aSAndrew Thompson {
5394594d907SAndrew Thompson if (!xfer->is_opened) {
5404594d907SAndrew Thompson /* transfer is not opened */
5414594d907SAndrew Thompson return;
5424594d907SAndrew Thompson }
543df4b8c2aSAndrew Thompson if (xfer->is_pending) {
544df4b8c2aSAndrew Thompson /* should not happen */
545df4b8c2aSAndrew Thompson return;
546df4b8c2aSAndrew Thompson }
547df4b8c2aSAndrew Thompson xfer->is_pending = 1; /* we are pending */
548df4b8c2aSAndrew Thompson xfer->is_cancel = 0; /* not cancelling */
549df4b8c2aSAndrew Thompson xfer->is_restart = 0; /* not restarting */
550df4b8c2aSAndrew Thompson
551df4b8c2aSAndrew Thompson xfer->pdev->methods->tr_submit(xfer);
552df4b8c2aSAndrew Thompson return;
553df4b8c2aSAndrew Thompson }
554df4b8c2aSAndrew Thompson
555df4b8c2aSAndrew Thompson void
libusb20_tr_start(struct libusb20_transfer * xfer)556df4b8c2aSAndrew Thompson libusb20_tr_start(struct libusb20_transfer *xfer)
557df4b8c2aSAndrew Thompson {
5584594d907SAndrew Thompson if (!xfer->is_opened) {
5594594d907SAndrew Thompson /* transfer is not opened */
5604594d907SAndrew Thompson return;
5614594d907SAndrew Thompson }
562df4b8c2aSAndrew Thompson if (xfer->is_pending) {
563df4b8c2aSAndrew Thompson if (xfer->is_cancel) {
564df4b8c2aSAndrew Thompson /* cancelling - restart */
565df4b8c2aSAndrew Thompson xfer->is_restart = 1;
566df4b8c2aSAndrew Thompson }
567df4b8c2aSAndrew Thompson /* transfer not pending */
568df4b8c2aSAndrew Thompson return;
569df4b8c2aSAndrew Thompson }
570df4b8c2aSAndrew Thompson /* get into the callback */
571df4b8c2aSAndrew Thompson libusb20_tr_callback_wrapper(xfer);
572df4b8c2aSAndrew Thompson return;
573df4b8c2aSAndrew Thompson }
574df4b8c2aSAndrew Thompson
575df4b8c2aSAndrew Thompson /* USB device operations */
576df4b8c2aSAndrew Thompson
577df4b8c2aSAndrew Thompson int
libusb20_dev_close(struct libusb20_device * pdev)578df4b8c2aSAndrew Thompson libusb20_dev_close(struct libusb20_device *pdev)
579df4b8c2aSAndrew Thompson {
580df4b8c2aSAndrew Thompson struct libusb20_transfer *xfer;
581df4b8c2aSAndrew Thompson uint16_t x;
582df4b8c2aSAndrew Thompson int error = 0;
583df4b8c2aSAndrew Thompson
584df4b8c2aSAndrew Thompson if (!pdev->is_opened) {
585df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER);
586df4b8c2aSAndrew Thompson }
587df4b8c2aSAndrew Thompson for (x = 0; x != pdev->nTransfer; x++) {
588df4b8c2aSAndrew Thompson xfer = pdev->pTransfer + x;
589df4b8c2aSAndrew Thompson
5904594d907SAndrew Thompson if (!xfer->is_opened) {
5914594d907SAndrew Thompson /* transfer is not opened */
5924594d907SAndrew Thompson continue;
5934594d907SAndrew Thompson }
5944594d907SAndrew Thompson
595df4b8c2aSAndrew Thompson libusb20_tr_drain(xfer);
5964594d907SAndrew Thompson
5974594d907SAndrew Thompson libusb20_tr_close(xfer);
598df4b8c2aSAndrew Thompson }
599df4b8c2aSAndrew Thompson
600df4b8c2aSAndrew Thompson if (pdev->pTransfer != NULL) {
601df4b8c2aSAndrew Thompson free(pdev->pTransfer);
602df4b8c2aSAndrew Thompson pdev->pTransfer = NULL;
603df4b8c2aSAndrew Thompson }
604df4b8c2aSAndrew Thompson error = pdev->beMethods->close_device(pdev);
605df4b8c2aSAndrew Thompson
606df4b8c2aSAndrew Thompson pdev->methods = &libusb20_dummy_methods;
607df4b8c2aSAndrew Thompson
608df4b8c2aSAndrew Thompson pdev->is_opened = 0;
609df4b8c2aSAndrew Thompson
6104315b3c9SAndrew Thompson /*
6116847ea50SHans Petter Selasky * Make sure libusb20_tr_get_pointer() fails:
6126847ea50SHans Petter Selasky */
6136847ea50SHans Petter Selasky pdev->nTransfer = 0;
6146847ea50SHans Petter Selasky
6156847ea50SHans Petter Selasky /*
6164315b3c9SAndrew Thompson * The following variable is only used by the libusb v0.1
6174315b3c9SAndrew Thompson * compat layer:
6184315b3c9SAndrew Thompson */
6194315b3c9SAndrew Thompson pdev->claimed_interface = 0;
620df4b8c2aSAndrew Thompson
6215b40d960SHans Petter Selasky /*
6225b40d960SHans Petter Selasky * The following variable is only used by the libusb v1.0
6235b40d960SHans Petter Selasky * compat layer:
6245b40d960SHans Petter Selasky */
6255b40d960SHans Petter Selasky pdev->auto_detach = 0;
6265b40d960SHans Petter Selasky
627df4b8c2aSAndrew Thompson return (error);
628df4b8c2aSAndrew Thompson }
629df4b8c2aSAndrew Thompson
630df4b8c2aSAndrew Thompson int
libusb20_dev_detach_kernel_driver(struct libusb20_device * pdev,uint8_t ifaceIndex)631df4b8c2aSAndrew Thompson libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
632df4b8c2aSAndrew Thompson {
633df4b8c2aSAndrew Thompson int error;
634df4b8c2aSAndrew Thompson
635df4b8c2aSAndrew Thompson error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
636df4b8c2aSAndrew Thompson return (error);
637df4b8c2aSAndrew Thompson }
638df4b8c2aSAndrew Thompson
639df4b8c2aSAndrew Thompson struct LIBUSB20_DEVICE_DESC_DECODED *
libusb20_dev_get_device_desc(struct libusb20_device * pdev)640df4b8c2aSAndrew Thompson libusb20_dev_get_device_desc(struct libusb20_device *pdev)
641df4b8c2aSAndrew Thompson {
642df4b8c2aSAndrew Thompson return (&(pdev->ddesc));
643df4b8c2aSAndrew Thompson }
644df4b8c2aSAndrew Thompson
645df4b8c2aSAndrew Thompson int
libusb20_dev_get_fd(struct libusb20_device * pdev)646df4b8c2aSAndrew Thompson libusb20_dev_get_fd(struct libusb20_device *pdev)
647df4b8c2aSAndrew Thompson {
648df4b8c2aSAndrew Thompson return (pdev->file);
649df4b8c2aSAndrew Thompson }
650df4b8c2aSAndrew Thompson
651df4b8c2aSAndrew Thompson int
libusb20_dev_kernel_driver_active(struct libusb20_device * pdev,uint8_t ifaceIndex)652df4b8c2aSAndrew Thompson libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
653df4b8c2aSAndrew Thompson {
654df4b8c2aSAndrew Thompson int error;
655df4b8c2aSAndrew Thompson
656df4b8c2aSAndrew Thompson error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
657df4b8c2aSAndrew Thompson return (error);
658df4b8c2aSAndrew Thompson }
659df4b8c2aSAndrew Thompson
660df4b8c2aSAndrew Thompson int
libusb20_dev_open(struct libusb20_device * pdev,uint16_t nTransferMax)661df4b8c2aSAndrew Thompson libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
662df4b8c2aSAndrew Thompson {
663df4b8c2aSAndrew Thompson struct libusb20_transfer *xfer;
664df4b8c2aSAndrew Thompson uint32_t size;
665df4b8c2aSAndrew Thompson uint16_t x;
666df4b8c2aSAndrew Thompson int error;
667df4b8c2aSAndrew Thompson
668df4b8c2aSAndrew Thompson if (pdev->is_opened) {
669df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_BUSY);
670df4b8c2aSAndrew Thompson }
671df4b8c2aSAndrew Thompson if (nTransferMax >= 256) {
672df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM);
673df4b8c2aSAndrew Thompson } else if (nTransferMax != 0) {
674df4b8c2aSAndrew Thompson size = sizeof(pdev->pTransfer[0]) * nTransferMax;
675df4b8c2aSAndrew Thompson pdev->pTransfer = malloc(size);
676df4b8c2aSAndrew Thompson if (pdev->pTransfer == NULL) {
677df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NO_MEM);
678df4b8c2aSAndrew Thompson }
679df4b8c2aSAndrew Thompson memset(pdev->pTransfer, 0, size);
680df4b8c2aSAndrew Thompson }
681df4b8c2aSAndrew Thompson /* initialise all transfers */
682df4b8c2aSAndrew Thompson for (x = 0; x != nTransferMax; x++) {
683df4b8c2aSAndrew Thompson
684df4b8c2aSAndrew Thompson xfer = pdev->pTransfer + x;
685df4b8c2aSAndrew Thompson
686df4b8c2aSAndrew Thompson xfer->pdev = pdev;
687df4b8c2aSAndrew Thompson xfer->trIndex = x;
688df4b8c2aSAndrew Thompson xfer->callback = &dummy_callback;
689df4b8c2aSAndrew Thompson }
690df4b8c2aSAndrew Thompson
691df4b8c2aSAndrew Thompson /* set "nTransfer" early */
692df4b8c2aSAndrew Thompson pdev->nTransfer = nTransferMax;
693df4b8c2aSAndrew Thompson
694df4b8c2aSAndrew Thompson error = pdev->beMethods->open_device(pdev, nTransferMax);
695df4b8c2aSAndrew Thompson
696df4b8c2aSAndrew Thompson if (error) {
697df4b8c2aSAndrew Thompson if (pdev->pTransfer != NULL) {
698df4b8c2aSAndrew Thompson free(pdev->pTransfer);
699df4b8c2aSAndrew Thompson pdev->pTransfer = NULL;
700df4b8c2aSAndrew Thompson }
701df4b8c2aSAndrew Thompson pdev->file = -1;
702df4b8c2aSAndrew Thompson pdev->file_ctrl = -1;
703df4b8c2aSAndrew Thompson pdev->nTransfer = 0;
704df4b8c2aSAndrew Thompson } else {
705df4b8c2aSAndrew Thompson pdev->is_opened = 1;
706df4b8c2aSAndrew Thompson }
707df4b8c2aSAndrew Thompson return (error);
708df4b8c2aSAndrew Thompson }
709df4b8c2aSAndrew Thompson
710df4b8c2aSAndrew Thompson int
libusb20_dev_reset(struct libusb20_device * pdev)711df4b8c2aSAndrew Thompson libusb20_dev_reset(struct libusb20_device *pdev)
712df4b8c2aSAndrew Thompson {
713df4b8c2aSAndrew Thompson int error;
714df4b8c2aSAndrew Thompson
715df4b8c2aSAndrew Thompson error = pdev->methods->reset_device(pdev);
716df4b8c2aSAndrew Thompson return (error);
717df4b8c2aSAndrew Thompson }
718df4b8c2aSAndrew Thompson
719df4b8c2aSAndrew Thompson int
libusb20_dev_check_connected(struct libusb20_device * pdev)720e50ac68bSAndrew Thompson libusb20_dev_check_connected(struct libusb20_device *pdev)
721e50ac68bSAndrew Thompson {
722e50ac68bSAndrew Thompson int error;
723e50ac68bSAndrew Thompson
724e50ac68bSAndrew Thompson error = pdev->methods->check_connected(pdev);
725e50ac68bSAndrew Thompson return (error);
726e50ac68bSAndrew Thompson }
727e50ac68bSAndrew Thompson
728e50ac68bSAndrew Thompson int
libusb20_dev_set_power_mode(struct libusb20_device * pdev,uint8_t power_mode)729df4b8c2aSAndrew Thompson libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
730df4b8c2aSAndrew Thompson {
731df4b8c2aSAndrew Thompson int error;
732df4b8c2aSAndrew Thompson
733df4b8c2aSAndrew Thompson error = pdev->methods->set_power_mode(pdev, power_mode);
734df4b8c2aSAndrew Thompson return (error);
735df4b8c2aSAndrew Thompson }
736df4b8c2aSAndrew Thompson
737df4b8c2aSAndrew Thompson uint8_t
libusb20_dev_get_power_mode(struct libusb20_device * pdev)738df4b8c2aSAndrew Thompson libusb20_dev_get_power_mode(struct libusb20_device *pdev)
739df4b8c2aSAndrew Thompson {
740df4b8c2aSAndrew Thompson int error;
741df4b8c2aSAndrew Thompson uint8_t power_mode;
742df4b8c2aSAndrew Thompson
743df4b8c2aSAndrew Thompson error = pdev->methods->get_power_mode(pdev, &power_mode);
744df4b8c2aSAndrew Thompson if (error)
745df4b8c2aSAndrew Thompson power_mode = LIBUSB20_POWER_ON; /* fake power mode */
746df4b8c2aSAndrew Thompson return (power_mode);
747df4b8c2aSAndrew Thompson }
748df4b8c2aSAndrew Thompson
749c77a24c2SHans Petter Selasky int
libusb20_dev_get_port_path(struct libusb20_device * pdev,uint8_t * buf,uint8_t bufsize)750c77a24c2SHans Petter Selasky libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
751c77a24c2SHans Petter Selasky {
752fd2ef04fSHans Petter Selasky
753fd2ef04fSHans Petter Selasky if (pdev->port_level == 0) {
754fd2ef04fSHans Petter Selasky /*
755fd2ef04fSHans Petter Selasky * Fallback for backends without port path:
756fd2ef04fSHans Petter Selasky */
757fd2ef04fSHans Petter Selasky if (bufsize < 2)
758fd2ef04fSHans Petter Selasky return (LIBUSB20_ERROR_OVERFLOW);
759fd2ef04fSHans Petter Selasky buf[0] = pdev->parent_address;
760fd2ef04fSHans Petter Selasky buf[1] = pdev->parent_port;
761fd2ef04fSHans Petter Selasky return (2);
762fd2ef04fSHans Petter Selasky }
763fd2ef04fSHans Petter Selasky
764fd2ef04fSHans Petter Selasky /* check if client buffer is too small */
765fd2ef04fSHans Petter Selasky if (pdev->port_level > bufsize)
766fd2ef04fSHans Petter Selasky return (LIBUSB20_ERROR_OVERFLOW);
767fd2ef04fSHans Petter Selasky
768fd2ef04fSHans Petter Selasky /* copy port number information */
769fd2ef04fSHans Petter Selasky memcpy(buf, pdev->port_path, pdev->port_level);
770fd2ef04fSHans Petter Selasky
771fd2ef04fSHans Petter Selasky return (pdev->port_level); /* success */
772c77a24c2SHans Petter Selasky }
773c77a24c2SHans Petter Selasky
774aafcb732SHans Petter Selasky uint16_t
libusb20_dev_get_power_usage(struct libusb20_device * pdev)775aafcb732SHans Petter Selasky libusb20_dev_get_power_usage(struct libusb20_device *pdev)
776aafcb732SHans Petter Selasky {
777aafcb732SHans Petter Selasky int error;
778aafcb732SHans Petter Selasky uint16_t power_usage;
779aafcb732SHans Petter Selasky
780aafcb732SHans Petter Selasky error = pdev->methods->get_power_usage(pdev, &power_usage);
781aafcb732SHans Petter Selasky if (error)
782aafcb732SHans Petter Selasky power_usage = 0;
783aafcb732SHans Petter Selasky return (power_usage);
784aafcb732SHans Petter Selasky }
785aafcb732SHans Petter Selasky
786df4b8c2aSAndrew Thompson int
libusb20_dev_set_alt_index(struct libusb20_device * pdev,uint8_t ifaceIndex,uint8_t altIndex)787df4b8c2aSAndrew Thompson libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
788df4b8c2aSAndrew Thompson {
789df4b8c2aSAndrew Thompson int error;
790df4b8c2aSAndrew Thompson
791df4b8c2aSAndrew Thompson error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
792df4b8c2aSAndrew Thompson return (error);
793df4b8c2aSAndrew Thompson }
794df4b8c2aSAndrew Thompson
795df4b8c2aSAndrew Thompson int
libusb20_dev_set_config_index(struct libusb20_device * pdev,uint8_t configIndex)796df4b8c2aSAndrew Thompson libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
797df4b8c2aSAndrew Thompson {
798df4b8c2aSAndrew Thompson int error;
799df4b8c2aSAndrew Thompson
800df4b8c2aSAndrew Thompson error = pdev->methods->set_config_index(pdev, configIndex);
801df4b8c2aSAndrew Thompson return (error);
802df4b8c2aSAndrew Thompson }
803df4b8c2aSAndrew Thompson
804df4b8c2aSAndrew Thompson int
libusb20_dev_request_sync(struct libusb20_device * pdev,struct LIBUSB20_CONTROL_SETUP_DECODED * setup,void * data,uint16_t * pactlen,uint32_t timeout,uint8_t flags)805df4b8c2aSAndrew Thompson libusb20_dev_request_sync(struct libusb20_device *pdev,
806df4b8c2aSAndrew Thompson struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
807df4b8c2aSAndrew Thompson uint16_t *pactlen, uint32_t timeout, uint8_t flags)
808df4b8c2aSAndrew Thompson {
809df4b8c2aSAndrew Thompson int error;
810df4b8c2aSAndrew Thompson
811df4b8c2aSAndrew Thompson error = pdev->methods->do_request_sync(pdev,
812df4b8c2aSAndrew Thompson setup, data, pactlen, timeout, flags);
813df4b8c2aSAndrew Thompson return (error);
814df4b8c2aSAndrew Thompson }
815df4b8c2aSAndrew Thompson
816df4b8c2aSAndrew Thompson int
libusb20_dev_req_string_sync(struct libusb20_device * pdev,uint8_t str_index,uint16_t langid,void * ptr,uint16_t len)817df4b8c2aSAndrew Thompson libusb20_dev_req_string_sync(struct libusb20_device *pdev,
818df4b8c2aSAndrew Thompson uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
819df4b8c2aSAndrew Thompson {
820df4b8c2aSAndrew Thompson struct LIBUSB20_CONTROL_SETUP_DECODED req;
821df4b8c2aSAndrew Thompson int error;
822dfd30b26SHans Petter Selasky int flags;
823df4b8c2aSAndrew Thompson
824ccef4ddfSAndrew Thompson /* make sure memory is initialised */
825ccef4ddfSAndrew Thompson memset(ptr, 0, len);
826ccef4ddfSAndrew Thompson
827df4b8c2aSAndrew Thompson if (len < 4) {
828df4b8c2aSAndrew Thompson /* invalid length */
829df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM);
830df4b8c2aSAndrew Thompson }
831df4b8c2aSAndrew Thompson LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
832df4b8c2aSAndrew Thompson
833df4b8c2aSAndrew Thompson /*
834df4b8c2aSAndrew Thompson * We need to read the USB string in two steps else some USB
835df4b8c2aSAndrew Thompson * devices will complain.
836df4b8c2aSAndrew Thompson */
837df4b8c2aSAndrew Thompson req.bmRequestType =
838df4b8c2aSAndrew Thompson LIBUSB20_REQUEST_TYPE_STANDARD |
839df4b8c2aSAndrew Thompson LIBUSB20_RECIPIENT_DEVICE |
840df4b8c2aSAndrew Thompson LIBUSB20_ENDPOINT_IN;
841df4b8c2aSAndrew Thompson req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
842df4b8c2aSAndrew Thompson req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
843df4b8c2aSAndrew Thompson req.wIndex = langid;
844df4b8c2aSAndrew Thompson req.wLength = 4; /* bytes */
845df4b8c2aSAndrew Thompson
846df4b8c2aSAndrew Thompson error = libusb20_dev_request_sync(pdev, &req,
847df4b8c2aSAndrew Thompson ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
848df4b8c2aSAndrew Thompson if (error) {
849dfd30b26SHans Petter Selasky /* try to request full string */
850dfd30b26SHans Petter Selasky req.wLength = 255;
851dfd30b26SHans Petter Selasky flags = 0;
852dfd30b26SHans Petter Selasky } else {
853dfd30b26SHans Petter Selasky /* extract length and request full string */
854dfd30b26SHans Petter Selasky req.wLength = *(uint8_t *)ptr;
855dfd30b26SHans Petter Selasky flags = LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK;
856df4b8c2aSAndrew Thompson }
857df4b8c2aSAndrew Thompson if (req.wLength > len) {
858df4b8c2aSAndrew Thompson /* partial string read */
859df4b8c2aSAndrew Thompson req.wLength = len;
860df4b8c2aSAndrew Thompson }
861dfd30b26SHans Petter Selasky error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, flags);
862dfd30b26SHans Petter Selasky if (error)
863df4b8c2aSAndrew Thompson return (error);
864dfd30b26SHans Petter Selasky
865dfd30b26SHans Petter Selasky if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING)
866df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER);
867df4b8c2aSAndrew Thompson return (0); /* success */
868df4b8c2aSAndrew Thompson }
869df4b8c2aSAndrew Thompson
870df4b8c2aSAndrew Thompson int
libusb20_dev_req_string_simple_sync(struct libusb20_device * pdev,uint8_t str_index,void * ptr,uint16_t len)871df4b8c2aSAndrew Thompson libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
872df4b8c2aSAndrew Thompson uint8_t str_index, void *ptr, uint16_t len)
873df4b8c2aSAndrew Thompson {
874df4b8c2aSAndrew Thompson char *buf;
875df4b8c2aSAndrew Thompson int error;
876df4b8c2aSAndrew Thompson uint16_t langid;
877df4b8c2aSAndrew Thompson uint16_t n;
878df4b8c2aSAndrew Thompson uint16_t i;
879df4b8c2aSAndrew Thompson uint16_t c;
880df4b8c2aSAndrew Thompson uint8_t temp[255];
881df4b8c2aSAndrew Thompson uint8_t swap;
882df4b8c2aSAndrew Thompson
883df4b8c2aSAndrew Thompson /* the following code derives from the FreeBSD USB kernel */
884df4b8c2aSAndrew Thompson
885df4b8c2aSAndrew Thompson if ((len < 1) || (ptr == NULL)) {
886df4b8c2aSAndrew Thompson /* too short buffer */
887df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM);
888df4b8c2aSAndrew Thompson }
889df4b8c2aSAndrew Thompson error = libusb20_dev_req_string_sync(pdev,
890df4b8c2aSAndrew Thompson 0, 0, temp, sizeof(temp));
891df4b8c2aSAndrew Thompson if (error < 0) {
892df4b8c2aSAndrew Thompson *(uint8_t *)ptr = 0; /* zero terminate */
893df4b8c2aSAndrew Thompson return (error);
894df4b8c2aSAndrew Thompson }
895df4b8c2aSAndrew Thompson langid = temp[2] | (temp[3] << 8);
896df4b8c2aSAndrew Thompson
897df4b8c2aSAndrew Thompson error = libusb20_dev_req_string_sync(pdev, str_index,
898df4b8c2aSAndrew Thompson langid, temp, sizeof(temp));
899df4b8c2aSAndrew Thompson if (error < 0) {
900df4b8c2aSAndrew Thompson *(uint8_t *)ptr = 0; /* zero terminate */
901df4b8c2aSAndrew Thompson return (error);
902df4b8c2aSAndrew Thompson }
903df4b8c2aSAndrew Thompson if (temp[0] < 2) {
904df4b8c2aSAndrew Thompson /* string length is too short */
905df4b8c2aSAndrew Thompson *(uint8_t *)ptr = 0; /* zero terminate */
906df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER);
907df4b8c2aSAndrew Thompson }
908df4b8c2aSAndrew Thompson /* reserve one byte for terminating zero */
909df4b8c2aSAndrew Thompson len--;
910df4b8c2aSAndrew Thompson
911df4b8c2aSAndrew Thompson /* find maximum length */
912df4b8c2aSAndrew Thompson n = (temp[0] / 2) - 1;
913df4b8c2aSAndrew Thompson if (n > len) {
914df4b8c2aSAndrew Thompson n = len;
915df4b8c2aSAndrew Thompson }
916df4b8c2aSAndrew Thompson /* reset swap state */
917df4b8c2aSAndrew Thompson swap = 3;
918df4b8c2aSAndrew Thompson
919df4b8c2aSAndrew Thompson /* setup output buffer pointer */
920df4b8c2aSAndrew Thompson buf = ptr;
921df4b8c2aSAndrew Thompson
922df4b8c2aSAndrew Thompson /* convert and filter */
923df4b8c2aSAndrew Thompson for (i = 0; (i != n); i++) {
924df4b8c2aSAndrew Thompson c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
925df4b8c2aSAndrew Thompson
926df4b8c2aSAndrew Thompson /* convert from Unicode, handle buggy strings */
927df4b8c2aSAndrew Thompson if (((c & 0xff00) == 0) && (swap & 1)) {
928df4b8c2aSAndrew Thompson /* Little Endian, default */
929df4b8c2aSAndrew Thompson *buf = c;
930df4b8c2aSAndrew Thompson swap = 1;
931df4b8c2aSAndrew Thompson } else if (((c & 0x00ff) == 0) && (swap & 2)) {
932df4b8c2aSAndrew Thompson /* Big Endian */
933df4b8c2aSAndrew Thompson *buf = c >> 8;
934df4b8c2aSAndrew Thompson swap = 2;
935df4b8c2aSAndrew Thompson } else {
936df4b8c2aSAndrew Thompson /* skip invalid character */
937df4b8c2aSAndrew Thompson continue;
938df4b8c2aSAndrew Thompson }
939df4b8c2aSAndrew Thompson /*
940df4b8c2aSAndrew Thompson * Filter by default - we don't allow greater and less than
941df4b8c2aSAndrew Thompson * signs because they might confuse the dmesg printouts!
942df4b8c2aSAndrew Thompson */
943df4b8c2aSAndrew Thompson if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
944df4b8c2aSAndrew Thompson /* skip invalid character */
945df4b8c2aSAndrew Thompson continue;
946df4b8c2aSAndrew Thompson }
947df4b8c2aSAndrew Thompson buf++;
948df4b8c2aSAndrew Thompson }
949df4b8c2aSAndrew Thompson *buf = 0; /* zero terminate string */
950df4b8c2aSAndrew Thompson
951df4b8c2aSAndrew Thompson return (0);
952df4b8c2aSAndrew Thompson }
953df4b8c2aSAndrew Thompson
954df4b8c2aSAndrew Thompson struct libusb20_config *
libusb20_dev_alloc_config(struct libusb20_device * pdev,uint8_t configIndex)955df4b8c2aSAndrew Thompson libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
956df4b8c2aSAndrew Thompson {
957df4b8c2aSAndrew Thompson struct libusb20_config *retval = NULL;
958df4b8c2aSAndrew Thompson uint8_t *ptr;
959df4b8c2aSAndrew Thompson uint16_t len;
960df4b8c2aSAndrew Thompson uint8_t do_close;
961df4b8c2aSAndrew Thompson int error;
962df4b8c2aSAndrew Thompson
96308216d18SHans Petter Selasky /*
96408216d18SHans Petter Selasky * Catch invalid configuration descriptor reads early on to
96508216d18SHans Petter Selasky * avoid issues with devices that don't check for a valid USB
96608216d18SHans Petter Selasky * configuration read request.
96708216d18SHans Petter Selasky */
96808216d18SHans Petter Selasky if (configIndex >= pdev->ddesc.bNumConfigurations)
96908216d18SHans Petter Selasky return (NULL);
97008216d18SHans Petter Selasky
971df4b8c2aSAndrew Thompson if (!pdev->is_opened) {
972df4b8c2aSAndrew Thompson error = libusb20_dev_open(pdev, 0);
973df4b8c2aSAndrew Thompson if (error) {
974df4b8c2aSAndrew Thompson return (NULL);
975df4b8c2aSAndrew Thompson }
976df4b8c2aSAndrew Thompson do_close = 1;
977df4b8c2aSAndrew Thompson } else {
978df4b8c2aSAndrew Thompson do_close = 0;
979df4b8c2aSAndrew Thompson }
980df4b8c2aSAndrew Thompson error = pdev->methods->get_config_desc_full(pdev,
981df4b8c2aSAndrew Thompson &ptr, &len, configIndex);
982df4b8c2aSAndrew Thompson
983df4b8c2aSAndrew Thompson if (error) {
984df4b8c2aSAndrew Thompson goto done;
985df4b8c2aSAndrew Thompson }
986df4b8c2aSAndrew Thompson /* parse new config descriptor */
987df4b8c2aSAndrew Thompson retval = libusb20_parse_config_desc(ptr);
988df4b8c2aSAndrew Thompson
989df4b8c2aSAndrew Thompson /* free config descriptor */
990df4b8c2aSAndrew Thompson free(ptr);
991df4b8c2aSAndrew Thompson
992df4b8c2aSAndrew Thompson done:
993df4b8c2aSAndrew Thompson if (do_close) {
994df4b8c2aSAndrew Thompson error = libusb20_dev_close(pdev);
995df4b8c2aSAndrew Thompson }
996df4b8c2aSAndrew Thompson return (retval);
997df4b8c2aSAndrew Thompson }
998df4b8c2aSAndrew Thompson
999df4b8c2aSAndrew Thompson struct libusb20_device *
libusb20_dev_alloc(void)1000df4b8c2aSAndrew Thompson libusb20_dev_alloc(void)
1001df4b8c2aSAndrew Thompson {
1002df4b8c2aSAndrew Thompson struct libusb20_device *pdev;
1003df4b8c2aSAndrew Thompson
1004df4b8c2aSAndrew Thompson pdev = malloc(sizeof(*pdev));
1005df4b8c2aSAndrew Thompson if (pdev == NULL) {
1006df4b8c2aSAndrew Thompson return (NULL);
1007df4b8c2aSAndrew Thompson }
1008df4b8c2aSAndrew Thompson memset(pdev, 0, sizeof(*pdev));
1009df4b8c2aSAndrew Thompson
1010df4b8c2aSAndrew Thompson pdev->file = -1;
1011df4b8c2aSAndrew Thompson pdev->file_ctrl = -1;
1012df4b8c2aSAndrew Thompson pdev->methods = &libusb20_dummy_methods;
1013df4b8c2aSAndrew Thompson return (pdev);
1014df4b8c2aSAndrew Thompson }
1015df4b8c2aSAndrew Thompson
1016df4b8c2aSAndrew Thompson uint8_t
libusb20_dev_get_config_index(struct libusb20_device * pdev)1017df4b8c2aSAndrew Thompson libusb20_dev_get_config_index(struct libusb20_device *pdev)
1018df4b8c2aSAndrew Thompson {
1019df4b8c2aSAndrew Thompson int error;
1020df4b8c2aSAndrew Thompson uint8_t cfg_index;
1021df4b8c2aSAndrew Thompson uint8_t do_close;
1022df4b8c2aSAndrew Thompson
1023df4b8c2aSAndrew Thompson if (!pdev->is_opened) {
1024df4b8c2aSAndrew Thompson error = libusb20_dev_open(pdev, 0);
1025df4b8c2aSAndrew Thompson if (error == 0) {
1026df4b8c2aSAndrew Thompson do_close = 1;
1027df4b8c2aSAndrew Thompson } else {
1028df4b8c2aSAndrew Thompson do_close = 0;
1029df4b8c2aSAndrew Thompson }
1030df4b8c2aSAndrew Thompson } else {
1031df4b8c2aSAndrew Thompson do_close = 0;
1032df4b8c2aSAndrew Thompson }
1033df4b8c2aSAndrew Thompson
1034df4b8c2aSAndrew Thompson error = pdev->methods->get_config_index(pdev, &cfg_index);
1035d81535d1SHans Petter Selasky if (error)
1036d81535d1SHans Petter Selasky cfg_index = 0xFF; /* current config index */
1037df4b8c2aSAndrew Thompson if (do_close) {
1038df4b8c2aSAndrew Thompson if (libusb20_dev_close(pdev)) {
1039df4b8c2aSAndrew Thompson /* ignore */
1040df4b8c2aSAndrew Thompson }
1041df4b8c2aSAndrew Thompson }
1042df4b8c2aSAndrew Thompson return (cfg_index);
1043df4b8c2aSAndrew Thompson }
1044df4b8c2aSAndrew Thompson
1045df4b8c2aSAndrew Thompson uint8_t
libusb20_dev_get_mode(struct libusb20_device * pdev)1046df4b8c2aSAndrew Thompson libusb20_dev_get_mode(struct libusb20_device *pdev)
1047df4b8c2aSAndrew Thompson {
1048df4b8c2aSAndrew Thompson return (pdev->usb_mode);
1049df4b8c2aSAndrew Thompson }
1050df4b8c2aSAndrew Thompson
1051df4b8c2aSAndrew Thompson uint8_t
libusb20_dev_get_speed(struct libusb20_device * pdev)1052df4b8c2aSAndrew Thompson libusb20_dev_get_speed(struct libusb20_device *pdev)
1053df4b8c2aSAndrew Thompson {
1054df4b8c2aSAndrew Thompson return (pdev->usb_speed);
1055df4b8c2aSAndrew Thompson }
1056df4b8c2aSAndrew Thompson
105734b0ca24SHans Petter Selasky int
libusb20_dev_get_stats(struct libusb20_device * pdev,struct libusb20_device_stats * pstats)105834b0ca24SHans Petter Selasky libusb20_dev_get_stats(struct libusb20_device *pdev, struct libusb20_device_stats *pstats)
105934b0ca24SHans Petter Selasky {
106034b0ca24SHans Petter Selasky uint8_t do_close;
106134b0ca24SHans Petter Selasky int error;
106234b0ca24SHans Petter Selasky
106334b0ca24SHans Petter Selasky if (!pdev->is_opened) {
106434b0ca24SHans Petter Selasky error = libusb20_dev_open(pdev, 0);
106534b0ca24SHans Petter Selasky if (error == 0) {
106634b0ca24SHans Petter Selasky do_close = 1;
106734b0ca24SHans Petter Selasky } else {
106834b0ca24SHans Petter Selasky do_close = 0;
106934b0ca24SHans Petter Selasky }
107034b0ca24SHans Petter Selasky } else {
107134b0ca24SHans Petter Selasky do_close = 0;
107234b0ca24SHans Petter Selasky }
107334b0ca24SHans Petter Selasky
107434b0ca24SHans Petter Selasky error = pdev->methods->get_stats(pdev, pstats);
107534b0ca24SHans Petter Selasky
107634b0ca24SHans Petter Selasky if (do_close)
107734b0ca24SHans Petter Selasky (void) libusb20_dev_close(pdev);
107834b0ca24SHans Petter Selasky
107934b0ca24SHans Petter Selasky return (error);
108034b0ca24SHans Petter Selasky }
108134b0ca24SHans Petter Selasky
1082df4b8c2aSAndrew Thompson /* if this function returns an error, the device is gone */
1083df4b8c2aSAndrew Thompson int
libusb20_dev_process(struct libusb20_device * pdev)1084df4b8c2aSAndrew Thompson libusb20_dev_process(struct libusb20_device *pdev)
1085df4b8c2aSAndrew Thompson {
1086df4b8c2aSAndrew Thompson int error;
1087df4b8c2aSAndrew Thompson
1088df4b8c2aSAndrew Thompson error = pdev->methods->process(pdev);
1089df4b8c2aSAndrew Thompson return (error);
1090df4b8c2aSAndrew Thompson }
1091df4b8c2aSAndrew Thompson
1092df4b8c2aSAndrew Thompson void
libusb20_dev_wait_process(struct libusb20_device * pdev,int timeout)1093df4b8c2aSAndrew Thompson libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1094df4b8c2aSAndrew Thompson {
1095df4b8c2aSAndrew Thompson struct pollfd pfd[1];
1096df4b8c2aSAndrew Thompson
1097df4b8c2aSAndrew Thompson if (!pdev->is_opened) {
1098df4b8c2aSAndrew Thompson return;
1099df4b8c2aSAndrew Thompson }
1100df4b8c2aSAndrew Thompson pfd[0].fd = pdev->file;
1101df4b8c2aSAndrew Thompson pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1102df4b8c2aSAndrew Thompson pfd[0].revents = 0;
1103df4b8c2aSAndrew Thompson
1104df4b8c2aSAndrew Thompson if (poll(pfd, 1, timeout)) {
1105df4b8c2aSAndrew Thompson /* ignore any error */
1106df4b8c2aSAndrew Thompson }
1107df4b8c2aSAndrew Thompson return;
1108df4b8c2aSAndrew Thompson }
1109df4b8c2aSAndrew Thompson
1110df4b8c2aSAndrew Thompson void
libusb20_dev_free(struct libusb20_device * pdev)1111df4b8c2aSAndrew Thompson libusb20_dev_free(struct libusb20_device *pdev)
1112df4b8c2aSAndrew Thompson {
1113df4b8c2aSAndrew Thompson if (pdev == NULL) {
1114df4b8c2aSAndrew Thompson /* be NULL safe */
1115df4b8c2aSAndrew Thompson return;
1116df4b8c2aSAndrew Thompson }
1117df4b8c2aSAndrew Thompson if (pdev->is_opened) {
1118df4b8c2aSAndrew Thompson if (libusb20_dev_close(pdev)) {
1119df4b8c2aSAndrew Thompson /* ignore any errors */
1120df4b8c2aSAndrew Thompson }
1121df4b8c2aSAndrew Thompson }
1122df4b8c2aSAndrew Thompson free(pdev);
1123df4b8c2aSAndrew Thompson return;
1124df4b8c2aSAndrew Thompson }
1125df4b8c2aSAndrew Thompson
1126df4b8c2aSAndrew Thompson int
libusb20_dev_get_info(struct libusb20_device * pdev,struct usb_device_info * pinfo)1127df4b8c2aSAndrew Thompson libusb20_dev_get_info(struct libusb20_device *pdev,
1128760bc48eSAndrew Thompson struct usb_device_info *pinfo)
1129df4b8c2aSAndrew Thompson {
1130df4b8c2aSAndrew Thompson if (pinfo == NULL)
1131df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM);
1132df4b8c2aSAndrew Thompson
1133df4b8c2aSAndrew Thompson return (pdev->beMethods->dev_get_info(pdev, pinfo));
1134df4b8c2aSAndrew Thompson }
1135df4b8c2aSAndrew Thompson
1136df4b8c2aSAndrew Thompson const char *
libusb20_dev_get_backend_name(struct libusb20_device * pdev)1137df4b8c2aSAndrew Thompson libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1138df4b8c2aSAndrew Thompson {
1139df4b8c2aSAndrew Thompson return (pdev->beMethods->get_backend_name());
1140df4b8c2aSAndrew Thompson }
1141df4b8c2aSAndrew Thompson
1142df4b8c2aSAndrew Thompson const char *
libusb20_dev_get_desc(struct libusb20_device * pdev)1143df4b8c2aSAndrew Thompson libusb20_dev_get_desc(struct libusb20_device *pdev)
1144df4b8c2aSAndrew Thompson {
1145df4b8c2aSAndrew Thompson return (pdev->usb_desc);
1146df4b8c2aSAndrew Thompson }
1147df4b8c2aSAndrew Thompson
1148df4b8c2aSAndrew Thompson void
libusb20_dev_set_debug(struct libusb20_device * pdev,int debug)1149df4b8c2aSAndrew Thompson libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1150df4b8c2aSAndrew Thompson {
1151df4b8c2aSAndrew Thompson pdev->debug = debug;
1152df4b8c2aSAndrew Thompson return;
1153df4b8c2aSAndrew Thompson }
1154df4b8c2aSAndrew Thompson
1155df4b8c2aSAndrew Thompson int
libusb20_dev_get_debug(struct libusb20_device * pdev)1156df4b8c2aSAndrew Thompson libusb20_dev_get_debug(struct libusb20_device *pdev)
1157df4b8c2aSAndrew Thompson {
1158df4b8c2aSAndrew Thompson return (pdev->debug);
1159df4b8c2aSAndrew Thompson }
1160df4b8c2aSAndrew Thompson
1161df4b8c2aSAndrew Thompson uint8_t
libusb20_dev_get_address(struct libusb20_device * pdev)1162df4b8c2aSAndrew Thompson libusb20_dev_get_address(struct libusb20_device *pdev)
1163df4b8c2aSAndrew Thompson {
1164df4b8c2aSAndrew Thompson return (pdev->device_address);
1165df4b8c2aSAndrew Thompson }
1166df4b8c2aSAndrew Thompson
1167df4b8c2aSAndrew Thompson uint8_t
libusb20_dev_get_parent_address(struct libusb20_device * pdev)11682485d8a7SHans Petter Selasky libusb20_dev_get_parent_address(struct libusb20_device *pdev)
11692485d8a7SHans Petter Selasky {
11702485d8a7SHans Petter Selasky return (pdev->parent_address);
11712485d8a7SHans Petter Selasky }
11722485d8a7SHans Petter Selasky
11732485d8a7SHans Petter Selasky uint8_t
libusb20_dev_get_parent_port(struct libusb20_device * pdev)11742485d8a7SHans Petter Selasky libusb20_dev_get_parent_port(struct libusb20_device *pdev)
11752485d8a7SHans Petter Selasky {
11762485d8a7SHans Petter Selasky return (pdev->parent_port);
11772485d8a7SHans Petter Selasky }
11782485d8a7SHans Petter Selasky
11792485d8a7SHans Petter Selasky uint8_t
libusb20_dev_get_bus_number(struct libusb20_device * pdev)1180df4b8c2aSAndrew Thompson libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1181df4b8c2aSAndrew Thompson {
1182df4b8c2aSAndrew Thompson return (pdev->bus_number);
1183df4b8c2aSAndrew Thompson }
1184df4b8c2aSAndrew Thompson
1185df4b8c2aSAndrew Thompson int
libusb20_dev_get_iface_desc(struct libusb20_device * pdev,uint8_t iface_index,char * buf,uint8_t len)1186df4b8c2aSAndrew Thompson libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1187df4b8c2aSAndrew Thompson uint8_t iface_index, char *buf, uint8_t len)
1188df4b8c2aSAndrew Thompson {
1189df4b8c2aSAndrew Thompson if ((buf == NULL) || (len == 0))
1190df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM);
1191df4b8c2aSAndrew Thompson
11924eb5923dSHans Petter Selasky buf[0] = 0; /* set default string value */
11934eb5923dSHans Petter Selasky
1194df4b8c2aSAndrew Thompson return (pdev->beMethods->dev_get_iface_desc(
1195df4b8c2aSAndrew Thompson pdev, iface_index, buf, len));
1196df4b8c2aSAndrew Thompson }
1197df4b8c2aSAndrew Thompson
1198df4b8c2aSAndrew Thompson /* USB backend operations */
1199df4b8c2aSAndrew Thompson
1200df4b8c2aSAndrew Thompson int
libusb20_be_get_dev_quirk(struct libusb20_backend * pbe,uint16_t quirk_index,struct libusb20_quirk * pq)1201df4b8c2aSAndrew Thompson libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1202df4b8c2aSAndrew Thompson uint16_t quirk_index, struct libusb20_quirk *pq)
1203df4b8c2aSAndrew Thompson {
1204df4b8c2aSAndrew Thompson return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1205df4b8c2aSAndrew Thompson }
1206df4b8c2aSAndrew Thompson
1207df4b8c2aSAndrew Thompson int
libusb20_be_get_quirk_name(struct libusb20_backend * pbe,uint16_t quirk_index,struct libusb20_quirk * pq)1208df4b8c2aSAndrew Thompson libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1209df4b8c2aSAndrew Thompson uint16_t quirk_index, struct libusb20_quirk *pq)
1210df4b8c2aSAndrew Thompson {
1211df4b8c2aSAndrew Thompson return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1212df4b8c2aSAndrew Thompson }
1213df4b8c2aSAndrew Thompson
1214df4b8c2aSAndrew Thompson int
libusb20_be_add_dev_quirk(struct libusb20_backend * pbe,struct libusb20_quirk * pq)1215df4b8c2aSAndrew Thompson libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1216df4b8c2aSAndrew Thompson struct libusb20_quirk *pq)
1217df4b8c2aSAndrew Thompson {
1218df4b8c2aSAndrew Thompson return (pbe->methods->root_add_dev_quirk(pbe, pq));
1219df4b8c2aSAndrew Thompson }
1220df4b8c2aSAndrew Thompson
1221df4b8c2aSAndrew Thompson int
libusb20_be_remove_dev_quirk(struct libusb20_backend * pbe,struct libusb20_quirk * pq)1222df4b8c2aSAndrew Thompson libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1223df4b8c2aSAndrew Thompson struct libusb20_quirk *pq)
1224df4b8c2aSAndrew Thompson {
1225df4b8c2aSAndrew Thompson return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1226df4b8c2aSAndrew Thompson }
1227df4b8c2aSAndrew Thompson
1228df4b8c2aSAndrew Thompson int
libusb20_be_set_template(struct libusb20_backend * pbe,int temp)1229df4b8c2aSAndrew Thompson libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1230df4b8c2aSAndrew Thompson {
1231df4b8c2aSAndrew Thompson return (pbe->methods->root_set_template(pbe, temp));
1232df4b8c2aSAndrew Thompson }
1233df4b8c2aSAndrew Thompson
1234df4b8c2aSAndrew Thompson int
libusb20_be_get_template(struct libusb20_backend * pbe,int * ptemp)1235df4b8c2aSAndrew Thompson libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1236df4b8c2aSAndrew Thompson {
1237df4b8c2aSAndrew Thompson int temp;
1238df4b8c2aSAndrew Thompson
1239df4b8c2aSAndrew Thompson if (ptemp == NULL)
1240df4b8c2aSAndrew Thompson ptemp = &temp;
1241df4b8c2aSAndrew Thompson
1242df4b8c2aSAndrew Thompson return (pbe->methods->root_get_template(pbe, ptemp));
1243df4b8c2aSAndrew Thompson }
1244df4b8c2aSAndrew Thompson
1245df4b8c2aSAndrew Thompson struct libusb20_device *
libusb20_be_device_foreach(struct libusb20_backend * pbe,struct libusb20_device * pdev)1246df4b8c2aSAndrew Thompson libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1247df4b8c2aSAndrew Thompson {
1248df4b8c2aSAndrew Thompson if (pbe == NULL) {
1249df4b8c2aSAndrew Thompson pdev = NULL;
1250df4b8c2aSAndrew Thompson } else if (pdev == NULL) {
1251df4b8c2aSAndrew Thompson pdev = TAILQ_FIRST(&(pbe->usb_devs));
1252df4b8c2aSAndrew Thompson } else {
1253df4b8c2aSAndrew Thompson pdev = TAILQ_NEXT(pdev, dev_entry);
1254df4b8c2aSAndrew Thompson }
1255df4b8c2aSAndrew Thompson return (pdev);
1256df4b8c2aSAndrew Thompson }
1257df4b8c2aSAndrew Thompson
1258df4b8c2aSAndrew Thompson struct libusb20_backend *
libusb20_be_alloc(const struct libusb20_backend_methods * methods)1259df4b8c2aSAndrew Thompson libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1260df4b8c2aSAndrew Thompson {
1261df4b8c2aSAndrew Thompson struct libusb20_backend *pbe;
1262df4b8c2aSAndrew Thompson
1263df4b8c2aSAndrew Thompson pbe = malloc(sizeof(*pbe));
1264df4b8c2aSAndrew Thompson if (pbe == NULL) {
1265df4b8c2aSAndrew Thompson return (NULL);
1266df4b8c2aSAndrew Thompson }
1267df4b8c2aSAndrew Thompson memset(pbe, 0, sizeof(*pbe));
1268df4b8c2aSAndrew Thompson
1269df4b8c2aSAndrew Thompson TAILQ_INIT(&(pbe->usb_devs));
1270df4b8c2aSAndrew Thompson
1271df4b8c2aSAndrew Thompson pbe->methods = methods; /* set backend methods */
1272df4b8c2aSAndrew Thompson
1273df4b8c2aSAndrew Thompson /* do the initial device scan */
1274df4b8c2aSAndrew Thompson if (pbe->methods->init_backend) {
1275df4b8c2aSAndrew Thompson pbe->methods->init_backend(pbe);
1276df4b8c2aSAndrew Thompson }
1277df4b8c2aSAndrew Thompson return (pbe);
1278df4b8c2aSAndrew Thompson }
1279df4b8c2aSAndrew Thompson
1280df4b8c2aSAndrew Thompson struct libusb20_backend *
libusb20_be_alloc_linux(void)1281df4b8c2aSAndrew Thompson libusb20_be_alloc_linux(void)
1282df4b8c2aSAndrew Thompson {
128399cd1f32SHans Petter Selasky return (NULL);
1284df4b8c2aSAndrew Thompson }
1285df4b8c2aSAndrew Thompson
1286df4b8c2aSAndrew Thompson struct libusb20_backend *
libusb20_be_alloc_ugen20(void)1287df4b8c2aSAndrew Thompson libusb20_be_alloc_ugen20(void)
1288df4b8c2aSAndrew Thompson {
128999cd1f32SHans Petter Selasky return (libusb20_be_alloc(&libusb20_ugen20_backend));
1290df4b8c2aSAndrew Thompson }
1291df4b8c2aSAndrew Thompson
1292df4b8c2aSAndrew Thompson struct libusb20_backend *
libusb20_be_alloc_default(void)1293df4b8c2aSAndrew Thompson libusb20_be_alloc_default(void)
1294df4b8c2aSAndrew Thompson {
1295df4b8c2aSAndrew Thompson struct libusb20_backend *pbe;
1296df4b8c2aSAndrew Thompson
129799cd1f32SHans Petter Selasky #ifdef __linux__
1298df4b8c2aSAndrew Thompson pbe = libusb20_be_alloc_linux();
1299df4b8c2aSAndrew Thompson if (pbe) {
1300df4b8c2aSAndrew Thompson return (pbe);
1301df4b8c2aSAndrew Thompson }
130299cd1f32SHans Petter Selasky #endif
1303df4b8c2aSAndrew Thompson pbe = libusb20_be_alloc_ugen20();
1304df4b8c2aSAndrew Thompson if (pbe) {
1305df4b8c2aSAndrew Thompson return (pbe);
1306df4b8c2aSAndrew Thompson }
1307df4b8c2aSAndrew Thompson return (NULL); /* no backend found */
1308df4b8c2aSAndrew Thompson }
1309df4b8c2aSAndrew Thompson
1310df4b8c2aSAndrew Thompson void
libusb20_be_free(struct libusb20_backend * pbe)1311df4b8c2aSAndrew Thompson libusb20_be_free(struct libusb20_backend *pbe)
1312df4b8c2aSAndrew Thompson {
1313df4b8c2aSAndrew Thompson struct libusb20_device *pdev;
1314df4b8c2aSAndrew Thompson
1315df4b8c2aSAndrew Thompson if (pbe == NULL) {
1316df4b8c2aSAndrew Thompson /* be NULL safe */
1317df4b8c2aSAndrew Thompson return;
1318df4b8c2aSAndrew Thompson }
1319df4b8c2aSAndrew Thompson while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1320df4b8c2aSAndrew Thompson libusb20_be_dequeue_device(pbe, pdev);
1321df4b8c2aSAndrew Thompson libusb20_dev_free(pdev);
1322df4b8c2aSAndrew Thompson }
1323df4b8c2aSAndrew Thompson if (pbe->methods->exit_backend) {
1324df4b8c2aSAndrew Thompson pbe->methods->exit_backend(pbe);
1325df4b8c2aSAndrew Thompson }
1326ccef4ddfSAndrew Thompson /* free backend */
1327ccef4ddfSAndrew Thompson free(pbe);
1328df4b8c2aSAndrew Thompson }
1329df4b8c2aSAndrew Thompson
1330df4b8c2aSAndrew Thompson void
libusb20_be_enqueue_device(struct libusb20_backend * pbe,struct libusb20_device * pdev)1331df4b8c2aSAndrew Thompson libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1332df4b8c2aSAndrew Thompson {
1333df4b8c2aSAndrew Thompson pdev->beMethods = pbe->methods; /* copy backend methods */
1334df4b8c2aSAndrew Thompson TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1335df4b8c2aSAndrew Thompson }
1336df4b8c2aSAndrew Thompson
1337df4b8c2aSAndrew Thompson void
libusb20_be_dequeue_device(struct libusb20_backend * pbe,struct libusb20_device * pdev)1338df4b8c2aSAndrew Thompson libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1339df4b8c2aSAndrew Thompson struct libusb20_device *pdev)
1340df4b8c2aSAndrew Thompson {
1341df4b8c2aSAndrew Thompson TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1342df4b8c2aSAndrew Thompson }
1343c61f2561SHans Petter Selasky
1344c61f2561SHans Petter Selasky const char *
libusb20_strerror(int code)1345c61f2561SHans Petter Selasky libusb20_strerror(int code)
1346c61f2561SHans Petter Selasky {
1347c61f2561SHans Petter Selasky switch (code) {
1348c61f2561SHans Petter Selasky case LIBUSB20_SUCCESS:
1349c61f2561SHans Petter Selasky return ("Success");
1350c61f2561SHans Petter Selasky case LIBUSB20_ERROR_IO:
1351c61f2561SHans Petter Selasky return ("I/O error");
1352c61f2561SHans Petter Selasky case LIBUSB20_ERROR_INVALID_PARAM:
1353c61f2561SHans Petter Selasky return ("Invalid parameter");
1354c61f2561SHans Petter Selasky case LIBUSB20_ERROR_ACCESS:
1355c61f2561SHans Petter Selasky return ("Permissions error");
1356c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NO_DEVICE:
1357c61f2561SHans Petter Selasky return ("No device");
1358c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NOT_FOUND:
1359c61f2561SHans Petter Selasky return ("Not found");
1360c61f2561SHans Petter Selasky case LIBUSB20_ERROR_BUSY:
1361c61f2561SHans Petter Selasky return ("Device busy");
1362c61f2561SHans Petter Selasky case LIBUSB20_ERROR_TIMEOUT:
1363c61f2561SHans Petter Selasky return ("Timeout");
1364c61f2561SHans Petter Selasky case LIBUSB20_ERROR_OVERFLOW:
1365c61f2561SHans Petter Selasky return ("Overflow");
1366c61f2561SHans Petter Selasky case LIBUSB20_ERROR_PIPE:
1367c61f2561SHans Petter Selasky return ("Pipe error");
1368c61f2561SHans Petter Selasky case LIBUSB20_ERROR_INTERRUPTED:
1369c61f2561SHans Petter Selasky return ("Interrupted");
1370c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NO_MEM:
1371c61f2561SHans Petter Selasky return ("Out of memory");
1372c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NOT_SUPPORTED:
1373c61f2561SHans Petter Selasky return ("Not supported");
1374c61f2561SHans Petter Selasky case LIBUSB20_ERROR_OTHER:
1375c61f2561SHans Petter Selasky return ("Other error");
1376c61f2561SHans Petter Selasky default:
1377c61f2561SHans Petter Selasky return ("Unknown error");
1378c61f2561SHans Petter Selasky }
1379c61f2561SHans Petter Selasky }
1380c61f2561SHans Petter Selasky
1381c61f2561SHans Petter Selasky const char *
libusb20_error_name(int code)1382c61f2561SHans Petter Selasky libusb20_error_name(int code)
1383c61f2561SHans Petter Selasky {
1384c61f2561SHans Petter Selasky switch (code) {
1385c61f2561SHans Petter Selasky case LIBUSB20_SUCCESS:
1386c61f2561SHans Petter Selasky return ("LIBUSB20_SUCCESS");
1387c61f2561SHans Petter Selasky case LIBUSB20_ERROR_IO:
1388c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_IO");
1389c61f2561SHans Petter Selasky case LIBUSB20_ERROR_INVALID_PARAM:
1390c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_INVALID_PARAM");
1391c61f2561SHans Petter Selasky case LIBUSB20_ERROR_ACCESS:
1392c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_ACCESS");
1393c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NO_DEVICE:
1394c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_NO_DEVICE");
1395c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NOT_FOUND:
1396c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_NOT_FOUND");
1397c61f2561SHans Petter Selasky case LIBUSB20_ERROR_BUSY:
1398c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_BUSY");
1399c61f2561SHans Petter Selasky case LIBUSB20_ERROR_TIMEOUT:
1400c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_TIMEOUT");
1401c61f2561SHans Petter Selasky case LIBUSB20_ERROR_OVERFLOW:
1402c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_OVERFLOW");
1403c61f2561SHans Petter Selasky case LIBUSB20_ERROR_PIPE:
1404c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_PIPE");
1405c61f2561SHans Petter Selasky case LIBUSB20_ERROR_INTERRUPTED:
1406c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_INTERRUPTED");
1407c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NO_MEM:
1408c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_NO_MEM");
1409c61f2561SHans Petter Selasky case LIBUSB20_ERROR_NOT_SUPPORTED:
1410c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1411c61f2561SHans Petter Selasky case LIBUSB20_ERROR_OTHER:
1412c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_OTHER");
1413c61f2561SHans Petter Selasky default:
1414c61f2561SHans Petter Selasky return ("LIBUSB20_ERROR_UNKNOWN");
1415c61f2561SHans Petter Selasky }
1416c61f2561SHans Petter Selasky }
1417