]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - lib/libusb/libusb20.c
MFC r301956, r301957, r301964, r301966, r301968, r301969, r302080,
[FreeBSD/stable/9.git] / lib / libusb / libusb20.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/queue.h>
28
29 #include <ctype.h>
30 #include <poll.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "libusb20.h"
36 #include "libusb20_desc.h"
37 #include "libusb20_int.h"
38
39 static int
40 dummy_int(void)
41 {
42         return (LIBUSB20_ERROR_NOT_SUPPORTED);
43 }
44
45 static void
46 dummy_void(void)
47 {
48         return;
49 }
50
51 static void
52 dummy_callback(struct libusb20_transfer *xfer)
53 {
54         ;                               /* style fix */
55         switch (libusb20_tr_get_status(xfer)) {
56         case LIBUSB20_TRANSFER_START:
57                 libusb20_tr_submit(xfer);
58                 break;
59         default:
60                 /* complete or error */
61                 break;
62         }
63         return;
64 }
65
66 #define dummy_get_config_desc_full (void *)dummy_int
67 #define dummy_get_config_index (void *)dummy_int
68 #define dummy_set_config_index (void *)dummy_int
69 #define dummy_set_alt_index (void *)dummy_int
70 #define dummy_reset_device (void *)dummy_int
71 #define dummy_check_connected (void *)dummy_int
72 #define dummy_set_power_mode (void *)dummy_int
73 #define dummy_get_power_mode (void *)dummy_int
74 #define dummy_get_port_path (void *)dummy_int
75 #define dummy_get_power_usage (void *)dummy_int
76 #define dummy_kernel_driver_active (void *)dummy_int
77 #define dummy_detach_kernel_driver (void *)dummy_int
78 #define dummy_do_request_sync (void *)dummy_int
79 #define dummy_tr_open (void *)dummy_int
80 #define dummy_tr_close (void *)dummy_int
81 #define dummy_tr_clear_stall_sync (void *)dummy_int
82 #define dummy_process (void *)dummy_int
83 #define dummy_dev_info (void *)dummy_int
84 #define dummy_dev_get_iface_driver (void *)dummy_int
85
86 #define dummy_tr_submit (void *)dummy_void
87 #define dummy_tr_cancel_async (void *)dummy_void
88
89 static const struct libusb20_device_methods libusb20_dummy_methods = {
90         LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
91 };
92
93 void
94 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
95 {
96         ;                               /* style fix */
97
98 repeat:
99
100         if (!xfer->is_pending) {
101                 xfer->status = LIBUSB20_TRANSFER_START;
102         } else {
103                 xfer->is_pending = 0;
104         }
105
106         xfer->callback(xfer);
107
108         if (xfer->is_restart) {
109                 xfer->is_restart = 0;
110                 goto repeat;
111         }
112         if (xfer->is_draining &&
113             (!xfer->is_pending)) {
114                 xfer->is_draining = 0;
115                 xfer->status = LIBUSB20_TRANSFER_DRAINED;
116                 xfer->callback(xfer);
117         }
118         return;
119 }
120
121 int
122 libusb20_tr_close(struct libusb20_transfer *xfer)
123 {
124         int error;
125
126         if (!xfer->is_opened) {
127                 return (LIBUSB20_ERROR_OTHER);
128         }
129         error = xfer->pdev->methods->tr_close(xfer);
130
131         if (xfer->pLength) {
132                 free(xfer->pLength);
133         }
134         if (xfer->ppBuffer) {
135                 free(xfer->ppBuffer);
136         }
137         /* reset variable fields in case the transfer is opened again */
138         xfer->priv_sc0 = 0;
139         xfer->priv_sc1 = 0;
140         xfer->is_opened = 0;
141         xfer->is_pending = 0;
142         xfer->is_cancel = 0;
143         xfer->is_draining = 0;
144         xfer->is_restart = 0;
145         xfer->status = 0;
146         xfer->flags = 0;
147         xfer->nFrames = 0;
148         xfer->aFrames = 0;
149         xfer->timeout = 0;
150         xfer->maxFrames = 0;
151         xfer->maxTotalLength = 0;
152         xfer->maxPacketLen = 0;
153         return (error);
154 }
155
156 int
157 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
158     uint32_t MaxFrameCount, uint8_t ep_no)
159 {
160         uint32_t size;
161         uint8_t pre_scale;
162         int error;
163
164         if (xfer->is_opened)
165                 return (LIBUSB20_ERROR_BUSY);
166         if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
167                 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
168                 pre_scale = 1;
169         } else {
170                 pre_scale = 0;
171         }
172         if (MaxFrameCount == 0)
173                 return (LIBUSB20_ERROR_INVALID_PARAM);
174
175         xfer->maxFrames = MaxFrameCount;
176
177         size = MaxFrameCount * sizeof(xfer->pLength[0]);
178         xfer->pLength = malloc(size);
179         if (xfer->pLength == NULL) {
180                 return (LIBUSB20_ERROR_NO_MEM);
181         }
182         memset(xfer->pLength, 0, size);
183
184         size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
185         xfer->ppBuffer = malloc(size);
186         if (xfer->ppBuffer == NULL) {
187                 free(xfer->pLength);
188                 return (LIBUSB20_ERROR_NO_MEM);
189         }
190         memset(xfer->ppBuffer, 0, size);
191
192         error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
193             MaxFrameCount, ep_no, pre_scale);
194
195         if (error) {
196                 free(xfer->ppBuffer);
197                 free(xfer->pLength);
198         } else {
199                 xfer->is_opened = 1;
200         }
201         return (error);
202 }
203
204 struct libusb20_transfer *
205 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
206 {
207         if (trIndex >= pdev->nTransfer) {
208                 return (NULL);
209         }
210         return (pdev->pTransfer + trIndex);
211 }
212
213 uint32_t
214 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
215 {
216         return (xfer->aFrames);
217 }
218
219 uint16_t
220 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
221 {
222         return (xfer->timeComplete);
223 }
224
225 uint32_t
226 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
227 {
228         uint32_t x;
229         uint32_t actlen = 0;
230
231         for (x = 0; x != xfer->aFrames; x++) {
232                 actlen += xfer->pLength[x];
233         }
234         return (actlen);
235 }
236
237 uint32_t
238 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
239 {
240         return (xfer->maxFrames);
241 }
242
243 uint32_t
244 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
245 {
246         /*
247          * Special Case NOTE: If the packet multiplier is non-zero for
248          * High Speed USB, the value returned is equal to
249          * "wMaxPacketSize * multiplier" !
250          */
251         return (xfer->maxPacketLen);
252 }
253
254 uint32_t
255 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
256 {
257         return (xfer->maxTotalLength);
258 }
259
260 uint8_t
261 libusb20_tr_get_status(struct libusb20_transfer *xfer)
262 {
263         return (xfer->status);
264 }
265
266 uint8_t
267 libusb20_tr_pending(struct libusb20_transfer *xfer)
268 {
269         return (xfer->is_pending);
270 }
271
272 void   *
273 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
274 {
275         return (xfer->priv_sc0);
276 }
277
278 void   *
279 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
280 {
281         return (xfer->priv_sc1);
282 }
283
284 void
285 libusb20_tr_stop(struct libusb20_transfer *xfer)
286 {
287         if (!xfer->is_opened) {
288                 /* transfer is not opened */
289                 return;
290         }
291         if (!xfer->is_pending) {
292                 /* transfer not pending */
293                 return;
294         }
295         if (xfer->is_cancel) {
296                 /* already cancelling */
297                 return;
298         }
299         xfer->is_cancel = 1;            /* we are cancelling */
300
301         xfer->pdev->methods->tr_cancel_async(xfer);
302         return;
303 }
304
305 void
306 libusb20_tr_drain(struct libusb20_transfer *xfer)
307 {
308         if (!xfer->is_opened) {
309                 /* transfer is not opened */
310                 return;
311         }
312         /* make sure that we are cancelling */
313         libusb20_tr_stop(xfer);
314
315         if (xfer->is_pending) {
316                 xfer->is_draining = 1;
317         }
318         return;
319 }
320
321 void
322 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
323 {
324         xfer->pdev->methods->tr_clear_stall_sync(xfer);
325         return;
326 }
327
328 void
329 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
330 {
331         xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
332         return;
333 }
334
335 void
336 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
337 {
338         xfer->callback = cb;
339         return;
340 }
341
342 void
343 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
344 {
345         xfer->flags = flags;
346         return;
347 }
348
349 uint32_t
350 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
351 {
352         return (xfer->pLength[frIndex]);
353 }
354
355 void
356 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
357 {
358         xfer->pLength[frIndex] = length;
359         return;
360 }
361
362 void
363 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
364 {
365         xfer->priv_sc0 = sc0;
366         return;
367 }
368
369 void
370 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
371 {
372         xfer->priv_sc1 = sc1;
373         return;
374 }
375
376 void
377 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
378 {
379         xfer->timeout = timeout;
380         return;
381 }
382
383 void
384 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
385 {
386         if (nFrames > xfer->maxFrames) {
387                 /* should not happen */
388                 nFrames = xfer->maxFrames;
389         }
390         xfer->nFrames = nFrames;
391         return;
392 }
393
394 void
395 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
396 {
397         xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
398         xfer->pLength[0] = length;
399         xfer->timeout = timeout;
400         xfer->nFrames = 1;
401         return;
402 }
403
404 void
405 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
406 {
407         uint16_t len;
408
409         xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
410         xfer->pLength[0] = 8;           /* fixed */
411         xfer->timeout = timeout;
412
413         len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
414
415         if (len != 0) {
416                 xfer->nFrames = 2;
417                 xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
418                 xfer->pLength[1] = len;
419         } else {
420                 xfer->nFrames = 1;
421         }
422         return;
423 }
424
425 void
426 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
427 {
428         xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
429         xfer->pLength[0] = length;
430         xfer->timeout = timeout;
431         xfer->nFrames = 1;
432         return;
433 }
434
435 void
436 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
437 {
438         if (frIndex >= xfer->maxFrames) {
439                 /* should not happen */
440                 return;
441         }
442         xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
443         xfer->pLength[frIndex] = length;
444         return;
445 }
446
447 uint8_t
448 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
449     void *pbuf, uint32_t length, uint32_t *pactlen,
450     uint32_t timeout)
451 {
452         struct libusb20_device *pdev = xfer->pdev;
453         uint32_t transfer_max;
454         uint32_t transfer_act;
455         uint8_t retval;
456
457         /* set some sensible default value */
458         if (pactlen != NULL)
459                 *pactlen = 0;
460
461         /* check for error condition */
462         if (libusb20_tr_pending(xfer))
463                 return (LIBUSB20_ERROR_OTHER);
464
465         do {
466                 /* compute maximum transfer length */
467                 transfer_max = 
468                     libusb20_tr_get_max_total_length(xfer);
469
470                 if (transfer_max > length)
471                         transfer_max = length;
472
473                 /* setup bulk or interrupt transfer */
474                 libusb20_tr_setup_bulk(xfer, pbuf, 
475                     transfer_max, timeout);
476
477                 /* start the transfer */
478                 libusb20_tr_start(xfer);
479
480                 /* wait for transfer completion */
481                 while (libusb20_dev_process(pdev) == 0) {
482
483                         if (libusb20_tr_pending(xfer) == 0)
484                                 break;
485
486                         libusb20_dev_wait_process(pdev, -1);
487                 }
488
489                 transfer_act = libusb20_tr_get_actual_length(xfer);
490
491                 /* update actual length, if any */
492                 if (pactlen != NULL)
493                         pactlen[0] += transfer_act;
494
495                 /* check transfer status */
496                 retval = libusb20_tr_get_status(xfer);
497                 if (retval)
498                         break;
499
500                 /* check for short transfer */
501                 if (transfer_act != transfer_max)
502                         break;
503
504                 /* update buffer pointer and length */
505                 pbuf = ((uint8_t *)pbuf) + transfer_max;
506                 length = length - transfer_max;
507
508         } while (length != 0);
509
510         return (retval);
511 }
512
513 void
514 libusb20_tr_submit(struct libusb20_transfer *xfer)
515 {
516         if (!xfer->is_opened) {
517                 /* transfer is not opened */
518                 return;
519         }
520         if (xfer->is_pending) {
521                 /* should not happen */
522                 return;
523         }
524         xfer->is_pending = 1;           /* we are pending */
525         xfer->is_cancel = 0;            /* not cancelling */
526         xfer->is_restart = 0;           /* not restarting */
527
528         xfer->pdev->methods->tr_submit(xfer);
529         return;
530 }
531
532 void
533 libusb20_tr_start(struct libusb20_transfer *xfer)
534 {
535         if (!xfer->is_opened) {
536                 /* transfer is not opened */
537                 return;
538         }
539         if (xfer->is_pending) {
540                 if (xfer->is_cancel) {
541                         /* cancelling - restart */
542                         xfer->is_restart = 1;
543                 }
544                 /* transfer not pending */
545                 return;
546         }
547         /* get into the callback */
548         libusb20_tr_callback_wrapper(xfer);
549         return;
550 }
551
552 /* USB device operations */
553
554 int
555 libusb20_dev_close(struct libusb20_device *pdev)
556 {
557         struct libusb20_transfer *xfer;
558         uint16_t x;
559         int error = 0;
560
561         if (!pdev->is_opened) {
562                 return (LIBUSB20_ERROR_OTHER);
563         }
564         for (x = 0; x != pdev->nTransfer; x++) {
565                 xfer = pdev->pTransfer + x;
566
567                 if (!xfer->is_opened) {
568                         /* transfer is not opened */
569                         continue;
570                 }
571
572                 libusb20_tr_drain(xfer);
573
574                 libusb20_tr_close(xfer);
575         }
576
577         if (pdev->pTransfer != NULL) {
578                 free(pdev->pTransfer);
579                 pdev->pTransfer = NULL;
580         }
581         error = pdev->beMethods->close_device(pdev);
582
583         pdev->methods = &libusb20_dummy_methods;
584
585         pdev->is_opened = 0;
586
587         /* 
588          * The following variable is only used by the libusb v0.1
589          * compat layer:
590          */
591         pdev->claimed_interface = 0;
592
593         /*
594          * The following variable is only used by the libusb v1.0
595          * compat layer:
596          */
597         pdev->auto_detach = 0;
598
599         return (error);
600 }
601
602 int
603 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
604 {
605         int error;
606
607         error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
608         return (error);
609 }
610
611 struct LIBUSB20_DEVICE_DESC_DECODED *
612 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
613 {
614         return (&(pdev->ddesc));
615 }
616
617 int
618 libusb20_dev_get_fd(struct libusb20_device *pdev)
619 {
620         return (pdev->file);
621 }
622
623 int
624 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
625 {
626         int error;
627
628         error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
629         return (error);
630 }
631
632 int
633 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
634 {
635         struct libusb20_transfer *xfer;
636         uint32_t size;
637         uint16_t x;
638         int error;
639
640         if (pdev->is_opened) {
641                 return (LIBUSB20_ERROR_BUSY);
642         }
643         if (nTransferMax >= 256) {
644                 return (LIBUSB20_ERROR_INVALID_PARAM);
645         } else if (nTransferMax != 0) {
646                 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
647                 pdev->pTransfer = malloc(size);
648                 if (pdev->pTransfer == NULL) {
649                         return (LIBUSB20_ERROR_NO_MEM);
650                 }
651                 memset(pdev->pTransfer, 0, size);
652         }
653         /* initialise all transfers */
654         for (x = 0; x != nTransferMax; x++) {
655
656                 xfer = pdev->pTransfer + x;
657
658                 xfer->pdev = pdev;
659                 xfer->trIndex = x;
660                 xfer->callback = &dummy_callback;
661         }
662
663         /* set "nTransfer" early */
664         pdev->nTransfer = nTransferMax;
665
666         error = pdev->beMethods->open_device(pdev, nTransferMax);
667
668         if (error) {
669                 if (pdev->pTransfer != NULL) {
670                         free(pdev->pTransfer);
671                         pdev->pTransfer = NULL;
672                 }
673                 pdev->file = -1;
674                 pdev->file_ctrl = -1;
675                 pdev->nTransfer = 0;
676         } else {
677                 pdev->is_opened = 1;
678         }
679         return (error);
680 }
681
682 int
683 libusb20_dev_reset(struct libusb20_device *pdev)
684 {
685         int error;
686
687         error = pdev->methods->reset_device(pdev);
688         return (error);
689 }
690
691 int
692 libusb20_dev_check_connected(struct libusb20_device *pdev)
693 {
694         int error;
695
696         error = pdev->methods->check_connected(pdev);
697         return (error);
698 }
699
700 int
701 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
702 {
703         int error;
704
705         error = pdev->methods->set_power_mode(pdev, power_mode);
706         return (error);
707 }
708
709 uint8_t
710 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
711 {
712         int error;
713         uint8_t power_mode;
714
715         error = pdev->methods->get_power_mode(pdev, &power_mode);
716         if (error)
717                 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
718         return (power_mode);
719 }
720
721 int
722 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
723 {
724         return (pdev->methods->get_port_path(pdev, buf, bufsize));
725 }
726
727 uint16_t
728 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
729 {
730         int error;
731         uint16_t power_usage;
732
733         error = pdev->methods->get_power_usage(pdev, &power_usage);
734         if (error)
735                 power_usage = 0;
736         return (power_usage);
737 }
738
739 int
740 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
741 {
742         int error;
743
744         error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
745         return (error);
746 }
747
748 int
749 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
750 {
751         int error;
752
753         error = pdev->methods->set_config_index(pdev, configIndex);
754         return (error);
755 }
756
757 int
758 libusb20_dev_request_sync(struct libusb20_device *pdev,
759     struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
760     uint16_t *pactlen, uint32_t timeout, uint8_t flags)
761 {
762         int error;
763
764         error = pdev->methods->do_request_sync(pdev,
765             setup, data, pactlen, timeout, flags);
766         return (error);
767 }
768
769 int
770 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
771     uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
772 {
773         struct LIBUSB20_CONTROL_SETUP_DECODED req;
774         int error;
775
776         /* make sure memory is initialised */
777         memset(ptr, 0, len);
778
779         if (len < 4) {
780                 /* invalid length */
781                 return (LIBUSB20_ERROR_INVALID_PARAM);
782         }
783         LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
784
785         /*
786          * We need to read the USB string in two steps else some USB
787          * devices will complain.
788          */
789         req.bmRequestType =
790             LIBUSB20_REQUEST_TYPE_STANDARD |
791             LIBUSB20_RECIPIENT_DEVICE |
792             LIBUSB20_ENDPOINT_IN;
793         req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
794         req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
795         req.wIndex = langid;
796         req.wLength = 4;                /* bytes */
797
798         error = libusb20_dev_request_sync(pdev, &req,
799             ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
800         if (error) {
801                 return (error);
802         }
803         req.wLength = *(uint8_t *)ptr;  /* bytes */
804         if (req.wLength > len) {
805                 /* partial string read */
806                 req.wLength = len;
807         }
808         error = libusb20_dev_request_sync(pdev, &req,
809             ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
810
811         if (error) {
812                 return (error);
813         }
814         if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
815                 return (LIBUSB20_ERROR_OTHER);
816         }
817         return (0);                     /* success */
818 }
819
820 int
821 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
822     uint8_t str_index, void *ptr, uint16_t len)
823 {
824         char *buf;
825         int error;
826         uint16_t langid;
827         uint16_t n;
828         uint16_t i;
829         uint16_t c;
830         uint8_t temp[255];
831         uint8_t swap;
832
833         /* the following code derives from the FreeBSD USB kernel */
834
835         if ((len < 1) || (ptr == NULL)) {
836                 /* too short buffer */
837                 return (LIBUSB20_ERROR_INVALID_PARAM);
838         }
839         error = libusb20_dev_req_string_sync(pdev,
840             0, 0, temp, sizeof(temp));
841         if (error < 0) {
842                 *(uint8_t *)ptr = 0;    /* zero terminate */
843                 return (error);
844         }
845         langid = temp[2] | (temp[3] << 8);
846
847         error = libusb20_dev_req_string_sync(pdev, str_index,
848             langid, temp, sizeof(temp));
849         if (error < 0) {
850                 *(uint8_t *)ptr = 0;    /* zero terminate */
851                 return (error);
852         }
853         if (temp[0] < 2) {
854                 /* string length is too short */
855                 *(uint8_t *)ptr = 0;    /* zero terminate */
856                 return (LIBUSB20_ERROR_OTHER);
857         }
858         /* reserve one byte for terminating zero */
859         len--;
860
861         /* find maximum length */
862         n = (temp[0] / 2) - 1;
863         if (n > len) {
864                 n = len;
865         }
866         /* reset swap state */
867         swap = 3;
868
869         /* setup output buffer pointer */
870         buf = ptr;
871
872         /* convert and filter */
873         for (i = 0; (i != n); i++) {
874                 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
875
876                 /* convert from Unicode, handle buggy strings */
877                 if (((c & 0xff00) == 0) && (swap & 1)) {
878                         /* Little Endian, default */
879                         *buf = c;
880                         swap = 1;
881                 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
882                         /* Big Endian */
883                         *buf = c >> 8;
884                         swap = 2;
885                 } else {
886                         /* skip invalid character */
887                         continue;
888                 }
889                 /*
890                  * Filter by default - we don't allow greater and less than
891                  * signs because they might confuse the dmesg printouts!
892                  */
893                 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
894                         /* skip invalid character */
895                         continue;
896                 }
897                 buf++;
898         }
899         *buf = 0;                       /* zero terminate string */
900
901         return (0);
902 }
903
904 struct libusb20_config *
905 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
906 {
907         struct libusb20_config *retval = NULL;
908         uint8_t *ptr;
909         uint16_t len;
910         uint8_t do_close;
911         int error;
912
913         if (!pdev->is_opened) {
914                 error = libusb20_dev_open(pdev, 0);
915                 if (error) {
916                         return (NULL);
917                 }
918                 do_close = 1;
919         } else {
920                 do_close = 0;
921         }
922         error = pdev->methods->get_config_desc_full(pdev,
923             &ptr, &len, configIndex);
924
925         if (error) {
926                 goto done;
927         }
928         /* parse new config descriptor */
929         retval = libusb20_parse_config_desc(ptr);
930
931         /* free config descriptor */
932         free(ptr);
933
934 done:
935         if (do_close) {
936                 error = libusb20_dev_close(pdev);
937         }
938         return (retval);
939 }
940
941 struct libusb20_device *
942 libusb20_dev_alloc(void)
943 {
944         struct libusb20_device *pdev;
945
946         pdev = malloc(sizeof(*pdev));
947         if (pdev == NULL) {
948                 return (NULL);
949         }
950         memset(pdev, 0, sizeof(*pdev));
951
952         pdev->file = -1;
953         pdev->file_ctrl = -1;
954         pdev->methods = &libusb20_dummy_methods;
955         return (pdev);
956 }
957
958 uint8_t
959 libusb20_dev_get_config_index(struct libusb20_device *pdev)
960 {
961         int error;
962         uint8_t cfg_index;
963         uint8_t do_close;
964
965         if (!pdev->is_opened) {
966                 error = libusb20_dev_open(pdev, 0);
967                 if (error == 0) {
968                         do_close = 1;
969                 } else {
970                         do_close = 0;
971                 }
972         } else {
973                 do_close = 0;
974         }
975
976         error = pdev->methods->get_config_index(pdev, &cfg_index);
977         if (error)
978                 cfg_index = 0xFF;       /* current config index */
979         if (do_close) {
980                 if (libusb20_dev_close(pdev)) {
981                         /* ignore */
982                 }
983         }
984         return (cfg_index);
985 }
986
987 uint8_t
988 libusb20_dev_get_mode(struct libusb20_device *pdev)
989 {
990         return (pdev->usb_mode);
991 }
992
993 uint8_t
994 libusb20_dev_get_speed(struct libusb20_device *pdev)
995 {
996         return (pdev->usb_speed);
997 }
998
999 /* if this function returns an error, the device is gone */
1000 int
1001 libusb20_dev_process(struct libusb20_device *pdev)
1002 {
1003         int error;
1004
1005         error = pdev->methods->process(pdev);
1006         return (error);
1007 }
1008
1009 void
1010 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1011 {
1012         struct pollfd pfd[1];
1013
1014         if (!pdev->is_opened) {
1015                 return;
1016         }
1017         pfd[0].fd = pdev->file;
1018         pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1019         pfd[0].revents = 0;
1020
1021         if (poll(pfd, 1, timeout)) {
1022                 /* ignore any error */
1023         }
1024         return;
1025 }
1026
1027 void
1028 libusb20_dev_free(struct libusb20_device *pdev)
1029 {
1030         if (pdev == NULL) {
1031                 /* be NULL safe */
1032                 return;
1033         }
1034         if (pdev->is_opened) {
1035                 if (libusb20_dev_close(pdev)) {
1036                         /* ignore any errors */
1037                 }
1038         }
1039         free(pdev);
1040         return;
1041 }
1042
1043 int
1044 libusb20_dev_get_info(struct libusb20_device *pdev,
1045     struct usb_device_info *pinfo)
1046 {
1047         if (pinfo == NULL)
1048                 return (LIBUSB20_ERROR_INVALID_PARAM);
1049
1050         return (pdev->beMethods->dev_get_info(pdev, pinfo));
1051 }
1052
1053 const char *
1054 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1055 {
1056         return (pdev->beMethods->get_backend_name());
1057 }
1058
1059 const char *
1060 libusb20_dev_get_desc(struct libusb20_device *pdev)
1061 {
1062         return (pdev->usb_desc);
1063 }
1064
1065 void
1066 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1067 {
1068         pdev->debug = debug;
1069         return;
1070 }
1071
1072 int
1073 libusb20_dev_get_debug(struct libusb20_device *pdev)
1074 {
1075         return (pdev->debug);
1076 }
1077
1078 uint8_t
1079 libusb20_dev_get_address(struct libusb20_device *pdev)
1080 {
1081         return (pdev->device_address);
1082 }
1083
1084 uint8_t
1085 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1086 {
1087         return (pdev->parent_address);
1088 }
1089
1090 uint8_t
1091 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1092 {
1093         return (pdev->parent_port);
1094 }
1095
1096 uint8_t
1097 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1098 {
1099         return (pdev->bus_number);
1100 }
1101
1102 int
1103 libusb20_dev_get_iface_desc(struct libusb20_device *pdev, 
1104     uint8_t iface_index, char *buf, uint8_t len)
1105 {
1106         if ((buf == NULL) || (len == 0))
1107                 return (LIBUSB20_ERROR_INVALID_PARAM);
1108
1109         buf[0] = 0;             /* set default string value */
1110
1111         return (pdev->beMethods->dev_get_iface_desc(
1112             pdev, iface_index, buf, len));
1113 }
1114
1115 /* USB backend operations */
1116
1117 int
1118 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1119     uint16_t quirk_index, struct libusb20_quirk *pq)
1120 {
1121         return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1122 }
1123
1124 int
1125 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1126     uint16_t quirk_index, struct libusb20_quirk *pq)
1127 {
1128         return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1129 }
1130
1131 int
1132 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1133     struct libusb20_quirk *pq)
1134 {
1135         return (pbe->methods->root_add_dev_quirk(pbe, pq));
1136 }
1137
1138 int
1139 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1140     struct libusb20_quirk *pq)
1141 {
1142         return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1143 }
1144
1145 int
1146 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1147 {
1148         return (pbe->methods->root_set_template(pbe, temp));
1149 }
1150
1151 int
1152 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1153 {
1154         int temp;
1155
1156         if (ptemp == NULL)
1157                 ptemp = &temp;
1158
1159         return (pbe->methods->root_get_template(pbe, ptemp));
1160 }
1161
1162 struct libusb20_device *
1163 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1164 {
1165         if (pbe == NULL) {
1166                 pdev = NULL;
1167         } else if (pdev == NULL) {
1168                 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1169         } else {
1170                 pdev = TAILQ_NEXT(pdev, dev_entry);
1171         }
1172         return (pdev);
1173 }
1174
1175 struct libusb20_backend *
1176 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1177 {
1178         struct libusb20_backend *pbe;
1179
1180         pbe = malloc(sizeof(*pbe));
1181         if (pbe == NULL) {
1182                 return (NULL);
1183         }
1184         memset(pbe, 0, sizeof(*pbe));
1185
1186         TAILQ_INIT(&(pbe->usb_devs));
1187
1188         pbe->methods = methods;         /* set backend methods */
1189
1190         /* do the initial device scan */
1191         if (pbe->methods->init_backend) {
1192                 pbe->methods->init_backend(pbe);
1193         }
1194         return (pbe);
1195 }
1196
1197 struct libusb20_backend *
1198 libusb20_be_alloc_linux(void)
1199 {
1200         struct libusb20_backend *pbe;
1201
1202 #ifdef __linux__
1203         pbe = libusb20_be_alloc(&libusb20_linux_backend);
1204 #else
1205         pbe = NULL;
1206 #endif
1207         return (pbe);
1208 }
1209
1210 struct libusb20_backend *
1211 libusb20_be_alloc_ugen20(void)
1212 {
1213         struct libusb20_backend *pbe;
1214
1215 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1216         pbe = libusb20_be_alloc(&libusb20_ugen20_backend);
1217 #else
1218         pbe = NULL;
1219 #endif
1220         return (pbe);
1221 }
1222
1223 struct libusb20_backend *
1224 libusb20_be_alloc_default(void)
1225 {
1226         struct libusb20_backend *pbe;
1227
1228         pbe = libusb20_be_alloc_linux();
1229         if (pbe) {
1230                 return (pbe);
1231         }
1232         pbe = libusb20_be_alloc_ugen20();
1233         if (pbe) {
1234                 return (pbe);
1235         }
1236         return (NULL);                  /* no backend found */
1237 }
1238
1239 void
1240 libusb20_be_free(struct libusb20_backend *pbe)
1241 {
1242         struct libusb20_device *pdev;
1243
1244         if (pbe == NULL) {
1245                 /* be NULL safe */
1246                 return;
1247         }
1248         while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1249                 libusb20_be_dequeue_device(pbe, pdev);
1250                 libusb20_dev_free(pdev);
1251         }
1252         if (pbe->methods->exit_backend) {
1253                 pbe->methods->exit_backend(pbe);
1254         }
1255         /* free backend */
1256         free(pbe);
1257 }
1258
1259 void
1260 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1261 {
1262         pdev->beMethods = pbe->methods; /* copy backend methods */
1263         TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1264 }
1265
1266 void
1267 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1268     struct libusb20_device *pdev)
1269 {
1270         TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1271 }
1272
1273 const char *
1274 libusb20_strerror(int code)
1275 {
1276         switch (code) {
1277         case LIBUSB20_SUCCESS:
1278                 return ("Success");
1279         case LIBUSB20_ERROR_IO:
1280                 return ("I/O error");
1281         case LIBUSB20_ERROR_INVALID_PARAM:
1282                 return ("Invalid parameter");
1283         case LIBUSB20_ERROR_ACCESS:
1284                 return ("Permissions error");
1285         case LIBUSB20_ERROR_NO_DEVICE:
1286                 return ("No device");
1287         case LIBUSB20_ERROR_NOT_FOUND:
1288                 return ("Not found");
1289         case LIBUSB20_ERROR_BUSY:
1290                 return ("Device busy");
1291         case LIBUSB20_ERROR_TIMEOUT:
1292                 return ("Timeout");
1293         case LIBUSB20_ERROR_OVERFLOW:
1294                 return ("Overflow");
1295         case LIBUSB20_ERROR_PIPE:
1296                 return ("Pipe error");
1297         case LIBUSB20_ERROR_INTERRUPTED:
1298                 return ("Interrupted");
1299         case LIBUSB20_ERROR_NO_MEM:
1300                 return ("Out of memory");
1301         case LIBUSB20_ERROR_NOT_SUPPORTED:
1302                 return ("Not supported");
1303         case LIBUSB20_ERROR_OTHER:
1304                 return ("Other error");
1305         default:
1306                 return ("Unknown error");
1307         }
1308 }
1309
1310 const char *
1311 libusb20_error_name(int code)
1312 {
1313         switch (code) {
1314         case LIBUSB20_SUCCESS:
1315                 return ("LIBUSB20_SUCCESS");
1316         case LIBUSB20_ERROR_IO:
1317                 return ("LIBUSB20_ERROR_IO");
1318         case LIBUSB20_ERROR_INVALID_PARAM:
1319                 return ("LIBUSB20_ERROR_INVALID_PARAM");
1320         case LIBUSB20_ERROR_ACCESS:
1321                 return ("LIBUSB20_ERROR_ACCESS");
1322         case LIBUSB20_ERROR_NO_DEVICE:
1323                 return ("LIBUSB20_ERROR_NO_DEVICE");
1324         case LIBUSB20_ERROR_NOT_FOUND:
1325                 return ("LIBUSB20_ERROR_NOT_FOUND");
1326         case LIBUSB20_ERROR_BUSY:
1327                 return ("LIBUSB20_ERROR_BUSY");
1328         case LIBUSB20_ERROR_TIMEOUT:
1329                 return ("LIBUSB20_ERROR_TIMEOUT");
1330         case LIBUSB20_ERROR_OVERFLOW:
1331                 return ("LIBUSB20_ERROR_OVERFLOW");
1332         case LIBUSB20_ERROR_PIPE:
1333                 return ("LIBUSB20_ERROR_PIPE");
1334         case LIBUSB20_ERROR_INTERRUPTED:
1335                 return ("LIBUSB20_ERROR_INTERRUPTED");
1336         case LIBUSB20_ERROR_NO_MEM:
1337                 return ("LIBUSB20_ERROR_NO_MEM");
1338         case LIBUSB20_ERROR_NOT_SUPPORTED:
1339                 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1340         case LIBUSB20_ERROR_OTHER:
1341                 return ("LIBUSB20_ERROR_OTHER");
1342         default:
1343                 return ("LIBUSB20_ERROR_UNKNOWN");
1344         }
1345 }