1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef __LIBUSB10_H__ 30 #define __LIBUSB10_H__ 31 32 #ifndef LIBUSB_GLOBAL_INCLUDE_FILE 33 #include <sys/queue.h> 34 #endif 35 36 #define GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx)) 37 #define UNEXPORTED __attribute__((__visibility__("hidden"))) 38 #define CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock) 39 #define CTX_TRYLOCK(ctx) pthread_mutex_trylock(&(ctx)->ctx_lock) 40 #define CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock) 41 #define HOTPLUG_LOCK(ctx) pthread_mutex_lock(&(ctx)->hotplug_lock) 42 #define HOTPLUG_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->hotplug_lock) 43 44 #define DPRINTF(ctx, dbg, format, args...) do { \ 45 if ((ctx)->debug == dbg) { \ 46 switch (dbg) { \ 47 case LIBUSB_DEBUG_FUNCTION: \ 48 printf("LIBUSB_FUNCTION: " \ 49 format "\n", ## args); \ 50 break; \ 51 case LIBUSB_DEBUG_TRANSFER: \ 52 printf("LIBUSB_TRANSFER: " \ 53 format "\n", ## args); \ 54 break; \ 55 default: \ 56 break; \ 57 } \ 58 } \ 59 } while(0) 60 61 /* internal structures */ 62 63 struct libusb_super_pollfd { 64 TAILQ_ENTRY(libusb_super_pollfd) entry; 65 struct libusb20_device *pdev; 66 struct libusb_pollfd pollfd; 67 }; 68 69 struct libusb_super_transfer { 70 TAILQ_ENTRY(libusb_super_transfer) entry; 71 uint8_t *curr_data; 72 uint32_t rem_len; 73 uint32_t last_len; 74 uint32_t stream_id; 75 uint8_t state; 76 #define LIBUSB_SUPER_XFER_ST_NONE 0 77 #define LIBUSB_SUPER_XFER_ST_PEND 1 78 }; 79 80 struct libusb_hotplug_callback_handle_struct { 81 TAILQ_ENTRY(libusb_hotplug_callback_handle_struct) entry; 82 int events; 83 int vendor; 84 int product; 85 int devclass; 86 libusb_hotplug_callback_fn fn; 87 void *user_data; 88 }; 89 90 struct libusb_context { 91 int debug; 92 int debug_fixed; 93 int ctrl_pipe[2]; 94 int tr_done_ref; 95 int tr_done_gen; 96 97 pthread_mutex_t ctx_lock; 98 pthread_mutex_t hotplug_lock; 99 pthread_cond_t ctx_cond; 100 pthread_t hotplug_handler; 101 pthread_t ctx_handler; 102 #define NO_THREAD ((pthread_t)-1) 103 104 TAILQ_HEAD(, libusb_super_pollfd) pollfds; 105 TAILQ_HEAD(, libusb_super_transfer) tr_done; 106 TAILQ_HEAD(, libusb_hotplug_callback_handle_struct) hotplug_cbh; 107 TAILQ_HEAD(, libusb_device) hotplug_devs; 108 109 struct libusb_super_pollfd ctx_poll; 110 111 libusb_pollfd_added_cb fd_added_cb; 112 libusb_pollfd_removed_cb fd_removed_cb; 113 void *fd_cb_user_data; 114 }; 115 116 struct libusb_device { 117 int refcnt; 118 119 uint32_t claimed_interfaces; 120 121 struct libusb_super_pollfd dev_poll; 122 123 struct libusb_context *ctx; 124 125 TAILQ_ENTRY(libusb_device) hotplug_entry; 126 127 TAILQ_HEAD(, libusb_super_transfer) tr_head; 128 129 struct libusb20_device *os_priv; 130 }; 131 132 extern struct libusb_context *usbi_default_context; 133 134 void libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events); 135 void libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd); 136 void libusb10_cancel_all_transfer(libusb_device *dev); 137 138 #endif /* __LIBUSB10_H__ */ 139