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