libusb10.c (f1b5fa6e496ae0eb2a3a60ecd613ff92d432e5b9) libusb10.c (698e791af5804cd81e429c07e5901d5c52864068)
1/* $FreeBSD$ */
2/*-
3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 56 unchanged lines hidden (view full) ---

65void
66libusb_set_debug(libusb_context *ctx, int level)
67{
68 ctx = GET_CONTEXT(ctx);
69 if (ctx)
70 ctx->debug = level;
71}
72
1/* $FreeBSD$ */
2/*-
3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 56 unchanged lines hidden (view full) ---

65void
66libusb_set_debug(libusb_context *ctx, int level)
67{
68 ctx = GET_CONTEXT(ctx);
69 if (ctx)
70 ctx->debug = level;
71}
72
73static void
74libusb_set_nonblocking(int f)
75{
76 int flags;
77
78 /*
79 * We ignore any failures in this function, hence the
80 * non-blocking flag is not critical to the operation of
81 * libUSB. We use F_GETFL and F_SETFL to be compatible with
82 * Linux.
83 */
84
85 flags = fcntl(f, F_GETFL, NULL);
86 if (flags == -1)
87 return;
88 flags |= O_NONBLOCK;
89 fcntl(f, F_SETFL, flags);
90}
91
73int
74libusb_init(libusb_context **context)
75{
76 struct libusb_context *ctx;
77 char *debug;
92int
93libusb_init(libusb_context **context)
94{
95 struct libusb_context *ctx;
96 char *debug;
78 int flag;
79 int ret;
80
81 ctx = malloc(sizeof(*ctx));
82 if (!ctx)
83 return (LIBUSB_ERROR_INVALID_PARAM);
84
85 memset(ctx, 0, sizeof(*ctx));
86

--- 14 unchanged lines hidden (view full) ---

101 ret = pipe(ctx->ctrl_pipe);
102 if (ret < 0) {
103 pthread_mutex_destroy(&ctx->ctx_lock);
104 pthread_cond_destroy(&ctx->ctx_cond);
105 free(ctx);
106 return (LIBUSB_ERROR_OTHER);
107 }
108 /* set non-blocking mode on the control pipe to avoid deadlock */
97 int ret;
98
99 ctx = malloc(sizeof(*ctx));
100 if (!ctx)
101 return (LIBUSB_ERROR_INVALID_PARAM);
102
103 memset(ctx, 0, sizeof(*ctx));
104

--- 14 unchanged lines hidden (view full) ---

119 ret = pipe(ctx->ctrl_pipe);
120 if (ret < 0) {
121 pthread_mutex_destroy(&ctx->ctx_lock);
122 pthread_cond_destroy(&ctx->ctx_cond);
123 free(ctx);
124 return (LIBUSB_ERROR_OTHER);
125 }
126 /* set non-blocking mode on the control pipe to avoid deadlock */
109 flag = 1;
110 ret = fcntl(ctx->ctrl_pipe[0], O_NONBLOCK, &flag);
111 assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[0]");
112 flag = 1;
113 ret = fcntl(ctx->ctrl_pipe[1], O_NONBLOCK, &flag);
114 assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[1]");
127 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
128 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
115
116 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
117
118 pthread_mutex_lock(&default_context_lock);
119 if (usbi_default_context == NULL) {
120 usbi_default_context = ctx;
121 }
122 pthread_mutex_unlock(&default_context_lock);

--- 228 unchanged lines hidden (view full) ---

351 return (LIBUSB_ERROR_NO_MEM);
352 }
353 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
354 POLLOUT | POLLRDNORM | POLLWRNORM);
355
356 /* make sure our event loop detects the new device */
357 dummy = 0;
358 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
129
130 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
131
132 pthread_mutex_lock(&default_context_lock);
133 if (usbi_default_context == NULL) {
134 usbi_default_context = ctx;
135 }
136 pthread_mutex_unlock(&default_context_lock);

--- 228 unchanged lines hidden (view full) ---

365 return (LIBUSB_ERROR_NO_MEM);
366 }
367 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
368 POLLOUT | POLLRDNORM | POLLWRNORM);
369
370 /* make sure our event loop detects the new device */
371 dummy = 0;
372 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
359 if (err < sizeof(dummy)) {
373 if (err < (int)sizeof(dummy)) {
360 /* ignore error, if any */
361 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
362 }
363 *devh = pdev;
364
365 return (0);
366}
367

--- 58 unchanged lines hidden (view full) ---

426 libusb20_dev_close(pdev);
427
428 /* unref will free the "pdev" when the refcount reaches zero */
429 libusb_unref_device(dev);
430
431 /* make sure our event loop detects the closed device */
432 dummy = 0;
433 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
374 /* ignore error, if any */
375 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
376 }
377 *devh = pdev;
378
379 return (0);
380}
381

--- 58 unchanged lines hidden (view full) ---

440 libusb20_dev_close(pdev);
441
442 /* unref will free the "pdev" when the refcount reaches zero */
443 libusb_unref_device(dev);
444
445 /* make sure our event loop detects the closed device */
446 dummy = 0;
447 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
434 if (err < sizeof(dummy)) {
448 if (err < (int)sizeof(dummy)) {
435 /* ignore error, if any */
436 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
437 }
438}
439
440libusb_device *
441libusb_get_device(struct libusb20_device *pdev)
442{

--- 25 unchanged lines hidden (view full) ---

468libusb_set_configuration(struct libusb20_device *pdev, int configuration)
469{
470 struct libusb20_config *pconf;
471 struct libusb_device *dev;
472 int err;
473 uint8_t i;
474
475 dev = libusb_get_device(pdev);
449 /* ignore error, if any */
450 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
451 }
452}
453
454libusb_device *
455libusb_get_device(struct libusb20_device *pdev)
456{

--- 25 unchanged lines hidden (view full) ---

482libusb_set_configuration(struct libusb20_device *pdev, int configuration)
483{
484 struct libusb20_config *pconf;
485 struct libusb_device *dev;
486 int err;
487 uint8_t i;
488
489 dev = libusb_get_device(pdev);
476
477 if (dev == NULL)
478 return (LIBUSB_ERROR_INVALID_PARAM);
479
480 if (configuration < 1) {
481 /* unconfigure */
482 i = 255;
483 } else {
484 for (i = 0; i != 255; i++) {

--- 130 unchanged lines hidden (view full) ---

615 struct libusb_device *dev;
616 int err;
617
618 xfer = libusb10_get_transfer(pdev, endpoint, 0);
619 if (xfer == NULL)
620 return (LIBUSB_ERROR_INVALID_PARAM);
621
622 dev = libusb_get_device(pdev);
490 if (dev == NULL)
491 return (LIBUSB_ERROR_INVALID_PARAM);
492
493 if (configuration < 1) {
494 /* unconfigure */
495 i = 255;
496 } else {
497 for (i = 0; i != 255; i++) {

--- 130 unchanged lines hidden (view full) ---

628 struct libusb_device *dev;
629 int err;
630
631 xfer = libusb10_get_transfer(pdev, endpoint, 0);
632 if (xfer == NULL)
633 return (LIBUSB_ERROR_INVALID_PARAM);
634
635 dev = libusb_get_device(pdev);
636 if (dev == NULL)
637 return (LIBUSB_ERROR_INVALID_PARAM);
623
624 CTX_LOCK(dev->ctx);
625 err = libusb20_tr_open(xfer, 0, 0, endpoint);
626 CTX_UNLOCK(dev->ctx);
627
628 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
629 return (LIBUSB_ERROR_OTHER);
630

--- 11 unchanged lines hidden (view full) ---

642int
643libusb_reset_device(struct libusb20_device *pdev)
644{
645 libusb_device *dev;
646 int err;
647
648 dev = libusb_get_device(pdev);
649 if (dev == NULL)
638
639 CTX_LOCK(dev->ctx);
640 err = libusb20_tr_open(xfer, 0, 0, endpoint);
641 CTX_UNLOCK(dev->ctx);
642
643 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
644 return (LIBUSB_ERROR_OTHER);
645

--- 11 unchanged lines hidden (view full) ---

657int
658libusb_reset_device(struct libusb20_device *pdev)
659{
660 libusb_device *dev;
661 int err;
662
663 dev = libusb_get_device(pdev);
664 if (dev == NULL)
650 return (LIBUSB20_ERROR_INVALID_PARAM);
665 return (LIBUSB_ERROR_INVALID_PARAM);
651
652 libusb10_cancel_all_transfer(dev);
653
654 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
655
656 err = libusb20_dev_reset(pdev);
657
658 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,

--- 24 unchanged lines hidden (view full) ---

683 if (pdev == NULL)
684 return (LIBUSB_ERROR_INVALID_PARAM);
685
686 return (libusb20_dev_kernel_driver_active(
687 pdev, interface));
688}
689
690int
666
667 libusb10_cancel_all_transfer(dev);
668
669 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
670
671 err = libusb20_dev_reset(pdev);
672
673 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,

--- 24 unchanged lines hidden (view full) ---

698 if (pdev == NULL)
699 return (LIBUSB_ERROR_INVALID_PARAM);
700
701 return (libusb20_dev_kernel_driver_active(
702 pdev, interface));
703}
704
705int
706libusb_get_driver_np(struct libusb20_device *pdev, int interface,
707 char *name, int namelen)
708{
709 return (libusb_get_driver(pdev, interface, name, namelen));
710}
711
712int
713libusb_get_driver(struct libusb20_device *pdev, int interface,
714 char *name, int namelen)
715{
716 char *ptr;
717 int err;
718
719 if (pdev == NULL)
720 return (LIBUSB_ERROR_INVALID_PARAM);
721 if (namelen < 1)
722 return (LIBUSB_ERROR_INVALID_PARAM);
723
724 err = libusb20_dev_get_iface_desc(
725 pdev, interface, name, namelen);
726
727 if (err != 0)
728 return (LIBUSB_ERROR_OTHER);
729
730 /* we only want the driver name */
731 ptr = strstr(name, ":");
732 if (ptr != NULL)
733 *ptr = 0;
734
735 return (0);
736}
737
738int
739libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
740{
741 return (libusb_detach_kernel_driver(pdev, interface));
742}
743
744int
691libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
692{
693 int err;
694
695 if (pdev == NULL)
696 return (LIBUSB_ERROR_INVALID_PARAM);
697
698 err = libusb20_dev_detach_kernel_driver(
699 pdev, interface);
700
745libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
746{
747 int err;
748
749 if (pdev == NULL)
750 return (LIBUSB_ERROR_INVALID_PARAM);
751
752 err = libusb20_dev_detach_kernel_driver(
753 pdev, interface);
754
701 return (err ? LIBUSB20_ERROR_OTHER : 0);
755 return (err ? LIBUSB_ERROR_OTHER : 0);
702}
703
704int
705libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
706{
707 if (pdev == NULL)
708 return (LIBUSB_ERROR_INVALID_PARAM);
709 /* stub - currently not supported by libusb20 */

--- 662 unchanged lines hidden (view full) ---

1372}
1373
1374uint16_t
1375libusb_le16_to_cpu(uint16_t x)
1376{
1377 return (le16toh(x));
1378}
1379
756}
757
758int
759libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
760{
761 if (pdev == NULL)
762 return (LIBUSB_ERROR_INVALID_PARAM);
763 /* stub - currently not supported by libusb20 */

--- 662 unchanged lines hidden (view full) ---

1426}
1427
1428uint16_t
1429libusb_le16_to_cpu(uint16_t x)
1430{
1431 return (le16toh(x));
1432}
1433
1434const char *
1435libusb_strerror(int code)
1436{
1437 /* TODO */
1438 return ("Unknown error");
1439}