1/* $FreeBSD: head/lib/libusb/libusb10_io.c 194676 2009-06-23 01:04:58Z thompsa $ */
| 1/* $FreeBSD: head/lib/libusb/libusb10_io.c 195560 2009-07-10 14:15:53Z thompsa $ */
|
2/*- 3 * Copyright (c) 2009 Sylvestre Gallon. 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#include <stdlib.h> 29#include <unistd.h> 30#include <stdio.h> 31#include <poll.h> 32#include <pthread.h> 33#include <time.h> 34#include <errno.h> 35 36#include "libusb20.h" 37#include "libusb20_desc.h" 38#include "libusb20_int.h" 39#include "libusb.h" 40#include "libusb10.h" 41
| 2/*- 3 * Copyright (c) 2009 Sylvestre Gallon. 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#include <stdlib.h> 29#include <unistd.h> 30#include <stdio.h> 31#include <poll.h> 32#include <pthread.h> 33#include <time.h> 34#include <errno.h> 35 36#include "libusb20.h" 37#include "libusb20_desc.h" 38#include "libusb20_int.h" 39#include "libusb.h" 40#include "libusb10.h" 41
|
42static int
| 42UNEXPORTED int 43usb_add_pollfd(libusb_context *ctx, int fd, short events) 44{ 45 struct usb_pollfd *pollfd; 46 47 if (ctx == NULL) 48 return (LIBUSB_ERROR_INVALID_PARAM); 49 50 pollfd = malloc(sizeof(*pollfd)); 51 if (pollfd == NULL) 52 return (LIBUSB_ERROR_NO_MEM); 53 54 pollfd->pollfd.fd = fd; 55 pollfd->pollfd.events = events; 56 57 pthread_mutex_lock(&ctx->pollfds_lock); 58 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, list); 59 pthread_mutex_unlock(&ctx->pollfds_lock); 60 61 if (ctx->fd_added_cb) 62 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data); 63 return (0); 64} 65 66UNEXPORTED void 67usb_remove_pollfd(libusb_context *ctx, int fd) 68{ 69 struct usb_pollfd *pollfd; 70 int found; 71 72 found = 0; 73 pthread_mutex_lock(&ctx->pollfds_lock); 74 75 TAILQ_FOREACH(pollfd, &ctx->pollfds, list) { 76 if (pollfd->pollfd.fd == fd) { 77 found = 1; 78 break ; 79 } 80 } 81 82 if (found == 0) { 83 pthread_mutex_unlock(&ctx->pollfds_lock); 84 return ; 85 } 86 87 TAILQ_REMOVE(&ctx->pollfds, pollfd, list); 88 pthread_mutex_unlock(&ctx->pollfds_lock); 89 free(pollfd); 90 91 if (ctx->fd_removed_cb) 92 ctx->fd_removed_cb(fd, ctx->fd_cb_user_data); 93} 94 95UNEXPORTED void 96usb_handle_transfer_completion(struct usb_transfer *uxfer, 97 enum libusb_transfer_status status) 98{ 99 libusb_transfer *xfer; 100 libusb_context *ctx; 101 int len; 102 103 xfer = (struct libusb_transfer *) ((uint8_t *)uxfer + 104 sizeof(struct usb_transfer)); 105 ctx = xfer->dev_handle->dev->ctx; 106 107 pthread_mutex_lock(&ctx->flying_transfers_lock); 108 TAILQ_REMOVE(&ctx->flying_transfers, uxfer, list); 109 pthread_mutex_unlock(&ctx->flying_transfers_lock); 110 111 if (status == LIBUSB_TRANSFER_COMPLETED && xfer->flags & 112 LIBUSB_TRANSFER_SHORT_NOT_OK) { 113 len = xfer->length; 114 if (xfer->type == LIBUSB_TRANSFER_TYPE_CONTROL) 115 len -= sizeof(libusb_control_setup); 116 if (len != uxfer->transferred) { 117 status = LIBUSB_TRANSFER_ERROR; 118 } 119 } 120 121 xfer->status = status; 122 xfer->actual_length = uxfer->transferred; 123 124 if (xfer->callback) 125 xfer->callback(xfer); 126 if (xfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER) 127 libusb_free_transfer(xfer); 128 129 pthread_mutex_lock(&ctx->event_waiters_lock); 130 pthread_cond_broadcast(&ctx->event_waiters_cond); 131 pthread_mutex_unlock(&ctx->event_waiters_lock); 132} 133 134UNEXPORTED void 135usb_handle_disconnect(struct libusb_device_handle *devh) 136{ 137 struct libusb_context *ctx; 138 struct libusb_transfer *xfer; 139 struct usb_transfer *cur; 140 struct usb_transfer *to_cancel; 141 142 ctx = devh->dev->ctx; 143 144 while (1) { 145 pthread_mutex_lock(&ctx->flying_transfers_lock); 146 to_cancel = NULL; 147 TAILQ_FOREACH(cur, &ctx->flying_transfers, list) { 148 xfer = (struct libusb_transfer *) ((uint8_t *)cur + 149 sizeof(struct usb_transfer)); 150 if (xfer->dev_handle == devh) { 151 to_cancel = cur; 152 break ; 153 } 154 } 155 pthread_mutex_unlock(&ctx->flying_transfers_lock); 156 157 if (to_cancel == NULL) 158 break ; 159 160 usb_handle_transfer_completion(to_cancel, LIBUSB_TRANSFER_NO_DEVICE); 161 } 162 return ; 163} 164 165UNEXPORTED int
|
43get_next_timeout(libusb_context *ctx, struct timeval *tv, struct timeval *out) 44{ 45 struct timeval timeout;
| 166get_next_timeout(libusb_context *ctx, struct timeval *tv, struct timeval *out) 167{ 168 struct timeval timeout;
|
46 int ret;
| |
47
| 169
|
48 ret = libusb_get_next_timeout(ctx, &timeout); 49 50 if (ret) {
| 170 if (libusb_get_next_timeout(ctx, &timeout)) {
|
51 if (timerisset(&timeout) == 0) 52 return 1; 53 if (timercmp(&timeout, tv, <) != 0) 54 *out = timeout; 55 else 56 *out = *tv; 57 } else { 58 *out = *tv; 59 } 60 61 return (0); 62} 63
| 171 if (timerisset(&timeout) == 0) 172 return 1; 173 if (timercmp(&timeout, tv, <) != 0) 174 *out = timeout; 175 else 176 *out = *tv; 177 } else { 178 *out = *tv; 179 } 180 181 return (0); 182} 183
|
64static int
| 184UNEXPORTED int
|
65handle_timeouts(struct libusb_context *ctx) 66{ 67 struct timespec sys_ts; 68 struct timeval sys_tv; 69 struct timeval *cur_tv; 70 struct usb_transfer *xfer; 71 struct libusb_transfer *uxfer; 72 int ret; 73 74 GET_CONTEXT(ctx); 75 ret = 0; 76 77 pthread_mutex_lock(&ctx->flying_transfers_lock);
| 185handle_timeouts(struct libusb_context *ctx) 186{ 187 struct timespec sys_ts; 188 struct timeval sys_tv; 189 struct timeval *cur_tv; 190 struct usb_transfer *xfer; 191 struct libusb_transfer *uxfer; 192 int ret; 193 194 GET_CONTEXT(ctx); 195 ret = 0; 196 197 pthread_mutex_lock(&ctx->flying_transfers_lock);
|
78 if (USB_LIST_EMPTY(&ctx->flying_transfers))
| 198 if (TAILQ_EMPTY(&ctx->flying_transfers))
|
79 goto out; 80 81 ret = clock_gettime(CLOCK_MONOTONIC, &sys_ts); 82 TIMESPEC_TO_TIMEVAL(&sys_tv, &sys_ts); 83
| 199 goto out; 200 201 ret = clock_gettime(CLOCK_MONOTONIC, &sys_ts); 202 TIMESPEC_TO_TIMEVAL(&sys_tv, &sys_ts); 203
|
84 LIST_FOREACH_ENTRY(xfer, &ctx->flying_transfers, list) {
| 204 TAILQ_FOREACH(xfer, &ctx->flying_transfers, list) {
|
85 cur_tv = &xfer->timeout; 86 87 if (timerisset(cur_tv) == 0) 88 goto out; 89 90 if (xfer->flags & USB_TIMED_OUT) 91 continue; 92 93 if ((cur_tv->tv_sec > sys_tv.tv_sec) || (cur_tv->tv_sec == sys_tv.tv_sec && 94 cur_tv->tv_usec > sys_tv.tv_usec)) 95 goto out; 96 97 xfer->flags |= USB_TIMED_OUT; 98 uxfer = (libusb_transfer *) ((uint8_t *)xfer + 99 sizeof(struct usb_transfer)); 100 ret = libusb_cancel_transfer(uxfer); 101 } 102out: 103 pthread_mutex_unlock(&ctx->flying_transfers_lock); 104 return (ret); 105} 106
| 205 cur_tv = &xfer->timeout; 206 207 if (timerisset(cur_tv) == 0) 208 goto out; 209 210 if (xfer->flags & USB_TIMED_OUT) 211 continue; 212 213 if ((cur_tv->tv_sec > sys_tv.tv_sec) || (cur_tv->tv_sec == sys_tv.tv_sec && 214 cur_tv->tv_usec > sys_tv.tv_usec)) 215 goto out; 216 217 xfer->flags |= USB_TIMED_OUT; 218 uxfer = (libusb_transfer *) ((uint8_t *)xfer + 219 sizeof(struct usb_transfer)); 220 ret = libusb_cancel_transfer(uxfer); 221 } 222out: 223 pthread_mutex_unlock(&ctx->flying_transfers_lock); 224 return (ret); 225} 226
|
107static int
| 227UNEXPORTED int
|
108handle_events(struct libusb_context *ctx, struct timeval *tv) 109{ 110 struct libusb_pollfd *tmppollfd; 111 struct libusb_device_handle *devh; 112 struct usb_pollfd *ipollfd;
| 228handle_events(struct libusb_context *ctx, struct timeval *tv) 229{ 230 struct libusb_pollfd *tmppollfd; 231 struct libusb_device_handle *devh; 232 struct usb_pollfd *ipollfd;
|
113 struct usb_transfer *cur; 114 struct usb_transfer *cancel; 115 struct libusb_transfer *xfer;
| |
116 struct pollfd *fds; 117 struct pollfd *tfds; 118 nfds_t nfds; 119 int tmpfd;
| 233 struct pollfd *fds; 234 struct pollfd *tfds; 235 nfds_t nfds; 236 int tmpfd;
|
120 int tmp;
| |
121 int ret; 122 int timeout; 123 int i; 124 125 GET_CONTEXT(ctx);
| 237 int ret; 238 int timeout; 239 int i; 240 241 GET_CONTEXT(ctx);
|
126 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "handle_events enter");
| 242 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "handle_events enter");
|
127 128 nfds = 0; 129 i = -1; 130 131 pthread_mutex_lock(&ctx->pollfds_lock);
| 243 244 nfds = 0; 245 i = -1; 246 247 pthread_mutex_lock(&ctx->pollfds_lock);
|
132 LIST_FOREACH_ENTRY(ipollfd, &ctx->pollfds, list)
| 248 TAILQ_FOREACH(ipollfd, &ctx->pollfds, list)
|
133 nfds++; 134
| 249 nfds++; 250
|
135 fds = malloc(sizeof(*fds) * nfds);
| 251 fds = alloca(sizeof(*fds) * nfds);
|
136 if (fds == NULL) 137 return (LIBUSB_ERROR_NO_MEM); 138
| 252 if (fds == NULL) 253 return (LIBUSB_ERROR_NO_MEM); 254
|
139 LIST_FOREACH_ENTRY(ipollfd, &ctx->pollfds, list) {
| 255 TAILQ_FOREACH(ipollfd, &ctx->pollfds, list) {
|
140 tmppollfd = &ipollfd->pollfd; 141 tmpfd = tmppollfd->fd; 142 i++; 143 fds[i].fd = tmpfd; 144 fds[i].events = tmppollfd->events; 145 fds[i].revents = 0; 146 } 147 148 pthread_mutex_unlock(&ctx->pollfds_lock); 149 150 timeout = (tv->tv_sec * 1000) + (tv->tv_usec / 1000); 151 if (tv->tv_usec % 1000) 152 timeout++; 153 154 ret = poll(fds, nfds, timeout);
| 256 tmppollfd = &ipollfd->pollfd; 257 tmpfd = tmppollfd->fd; 258 i++; 259 fds[i].fd = tmpfd; 260 fds[i].events = tmppollfd->events; 261 fds[i].revents = 0; 262 } 263 264 pthread_mutex_unlock(&ctx->pollfds_lock); 265 266 timeout = (tv->tv_sec * 1000) + (tv->tv_usec / 1000); 267 if (tv->tv_usec % 1000) 268 timeout++; 269 270 ret = poll(fds, nfds, timeout);
|
155 if (ret == 0) { 156 free(fds);
| 271 if (ret == 0)
|
157 return (handle_timeouts(ctx));
| 272 return (handle_timeouts(ctx));
|
158 } else if (ret == -1 && errno == EINTR) { 159 free(fds);
| 273 else if (ret == -1 && errno == EINTR)
|
160 return (LIBUSB_ERROR_INTERRUPTED);
| 274 return (LIBUSB_ERROR_INTERRUPTED);
|
161 } else if (ret < 0) { 162 free(fds);
| 275 else if (ret < 0)
|
163 return (LIBUSB_ERROR_IO);
| 276 return (LIBUSB_ERROR_IO);
|
164 }
| |
165 166 if (fds[0].revents) { 167 if (ret == 1){ 168 ret = 0; 169 goto handled; 170 } else { 171 fds[0].revents = 0; 172 ret--; 173 } 174 } 175 176 pthread_mutex_lock(&ctx->open_devs_lock);
| 277 278 if (fds[0].revents) { 279 if (ret == 1){ 280 ret = 0; 281 goto handled; 282 } else { 283 fds[0].revents = 0; 284 ret--; 285 } 286 } 287 288 pthread_mutex_lock(&ctx->open_devs_lock);
|
177 for (i = 0 ; i < nfds && ret > 0 ; i++) {
| 289 for (i = 0, devh = NULL ; i < nfds && ret > 0 ; i++) {
|
178 179 tfds = &fds[i]; 180 if (!tfds->revents) 181 continue; 182 183 ret--;
| 290 291 tfds = &fds[i]; 292 if (!tfds->revents) 293 continue; 294 295 ret--;
|
184 LIST_FOREACH_ENTRY(devh, &ctx->open_devs, list) {
| 296 TAILQ_FOREACH(devh, &ctx->open_devs, list) {
|
185 if (libusb20_dev_get_fd(devh->os_priv) == tfds->fd) 186 break ; 187 } 188 189 if (tfds->revents & POLLERR) { 190 usb_remove_pollfd(ctx, libusb20_dev_get_fd(devh->os_priv));
| 297 if (libusb20_dev_get_fd(devh->os_priv) == tfds->fd) 298 break ; 299 } 300 301 if (tfds->revents & POLLERR) { 302 usb_remove_pollfd(ctx, libusb20_dev_get_fd(devh->os_priv));
|
191 usb_handle_disconnect(devh);
| 303 if (devh != NULL) 304 usb_handle_disconnect(devh);
|
192 continue ; 193 } 194 195 196 pthread_mutex_lock(&libusb20_lock);
| 305 continue ; 306 } 307 308 309 pthread_mutex_lock(&libusb20_lock);
|
197 dprintf(ctx, LIBUSB_DEBUG_TRANSFER, "LIBUSB20_PROCESS"); 198 ret = libusb20_dev_process(devh->os_priv);
| 310 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "LIBUSB20_PROCESS"); 311 if (devh != NULL) 312 ret = libusb20_dev_process(devh->os_priv);
|
199 pthread_mutex_unlock(&libusb20_lock); 200 201 202 if (ret == 0 || ret == LIBUSB20_ERROR_NO_DEVICE) 203 continue; 204 else if (ret < 0) 205 goto out; 206 } 207 208 ret = 0; 209out: 210 pthread_mutex_unlock(&ctx->open_devs_lock); 211 212handled:
| 313 pthread_mutex_unlock(&libusb20_lock); 314 315 316 if (ret == 0 || ret == LIBUSB20_ERROR_NO_DEVICE) 317 continue; 318 else if (ret < 0) 319 goto out; 320 } 321 322 ret = 0; 323out: 324 pthread_mutex_unlock(&ctx->open_devs_lock); 325 326handled:
|
213 free(fds); 214 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "handle_events leave");
| 327 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "handle_events leave");
|
215 return ret; 216} 217 218/* Polling and timing */ 219 220int 221libusb_try_lock_events(libusb_context * ctx) 222{ 223 int ret; 224 225 GET_CONTEXT(ctx);
| 328 return ret; 329} 330 331/* Polling and timing */ 332 333int 334libusb_try_lock_events(libusb_context * ctx) 335{ 336 int ret; 337 338 GET_CONTEXT(ctx);
|
226 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_try_lock_events enter");
| 339 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_try_lock_events enter");
|
227 228 pthread_mutex_lock(&ctx->pollfd_modify_lock); 229 ret = ctx->pollfd_modify; 230 pthread_mutex_unlock(&ctx->pollfd_modify_lock); 231 232 if (ret != 0) 233 return (1); 234 235 ret = pthread_mutex_trylock(&ctx->events_lock); 236 237 if (ret != 0) 238 return (1); 239 240 ctx->event_handler_active = 1; 241
| 340 341 pthread_mutex_lock(&ctx->pollfd_modify_lock); 342 ret = ctx->pollfd_modify; 343 pthread_mutex_unlock(&ctx->pollfd_modify_lock); 344 345 if (ret != 0) 346 return (1); 347 348 ret = pthread_mutex_trylock(&ctx->events_lock); 349 350 if (ret != 0) 351 return (1); 352 353 ctx->event_handler_active = 1; 354
|
242 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_try_lock_events leave");
| 355 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_try_lock_events leave");
|
243 return (0); 244} 245 246void 247libusb_lock_events(libusb_context * ctx) 248{ 249 GET_CONTEXT(ctx);
| 356 return (0); 357} 358 359void 360libusb_lock_events(libusb_context * ctx) 361{ 362 GET_CONTEXT(ctx);
|
250 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_events enter");
| 363 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_events enter");
|
251 252 pthread_mutex_lock(&ctx->events_lock); 253 ctx->event_handler_active = 1; 254
| 364 365 pthread_mutex_lock(&ctx->events_lock); 366 ctx->event_handler_active = 1; 367
|
255 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_events leave");
| 368 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_events leave");
|
256} 257 258void 259libusb_unlock_events(libusb_context * ctx) 260{ 261 GET_CONTEXT(ctx);
| 369} 370 371void 372libusb_unlock_events(libusb_context * ctx) 373{ 374 GET_CONTEXT(ctx);
|
262 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_events enter");
| 375 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_events enter");
|
263 264 ctx->event_handler_active = 0; 265 pthread_mutex_unlock(&ctx->events_lock); 266 267 pthread_mutex_lock(&ctx->event_waiters_lock); 268 pthread_cond_broadcast(&ctx->event_waiters_cond); 269 pthread_mutex_unlock(&ctx->event_waiters_lock); 270
| 376 377 ctx->event_handler_active = 0; 378 pthread_mutex_unlock(&ctx->events_lock); 379 380 pthread_mutex_lock(&ctx->event_waiters_lock); 381 pthread_cond_broadcast(&ctx->event_waiters_cond); 382 pthread_mutex_unlock(&ctx->event_waiters_lock); 383
|
271 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_events leave");
| 384 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_events leave");
|
272} 273 274int 275libusb_event_handling_ok(libusb_context * ctx) 276{ 277 int ret; 278 279 GET_CONTEXT(ctx);
| 385} 386 387int 388libusb_event_handling_ok(libusb_context * ctx) 389{ 390 int ret; 391 392 GET_CONTEXT(ctx);
|
280 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handling_ok enter");
| 393 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handling_ok enter");
|
281 282 pthread_mutex_lock(&ctx->pollfd_modify_lock); 283 ret = ctx->pollfd_modify; 284 pthread_mutex_unlock(&ctx->pollfd_modify_lock); 285 286 if (ret != 0) 287 return (0); 288
| 394 395 pthread_mutex_lock(&ctx->pollfd_modify_lock); 396 ret = ctx->pollfd_modify; 397 pthread_mutex_unlock(&ctx->pollfd_modify_lock); 398 399 if (ret != 0) 400 return (0); 401
|
289 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handling_ok leave");
| 402 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handling_ok leave");
|
290 return (1); 291} 292 293int 294libusb_event_handler_active(libusb_context * ctx) 295{ 296 int ret; 297 298 GET_CONTEXT(ctx);
| 403 return (1); 404} 405 406int 407libusb_event_handler_active(libusb_context * ctx) 408{ 409 int ret; 410 411 GET_CONTEXT(ctx);
|
299 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handler_active enter");
| 412 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handler_active enter");
|
300 301 pthread_mutex_lock(&ctx->pollfd_modify_lock); 302 ret = ctx->pollfd_modify; 303 pthread_mutex_unlock(&ctx->pollfd_modify_lock); 304 305 if (ret != 0) 306 return (1); 307
| 413 414 pthread_mutex_lock(&ctx->pollfd_modify_lock); 415 ret = ctx->pollfd_modify; 416 pthread_mutex_unlock(&ctx->pollfd_modify_lock); 417 418 if (ret != 0) 419 return (1); 420
|
308 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handler_active leave");
| 421 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handler_active leave");
|
309 return (ctx->event_handler_active); 310} 311 312void 313libusb_lock_event_waiters(libusb_context * ctx) 314{ 315 GET_CONTEXT(ctx);
| 422 return (ctx->event_handler_active); 423} 424 425void 426libusb_lock_event_waiters(libusb_context * ctx) 427{ 428 GET_CONTEXT(ctx);
|
316 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_event_waiters enter");
| 429 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_event_waiters enter");
|
317 318 pthread_mutex_lock(&ctx->event_waiters_lock); 319
| 430 431 pthread_mutex_lock(&ctx->event_waiters_lock); 432
|
320 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_event_waiters leave");
| 433 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_event_waiters leave");
|
321} 322 323void 324libusb_unlock_event_waiters(libusb_context * ctx) 325{ 326 GET_CONTEXT(ctx);
| 434} 435 436void 437libusb_unlock_event_waiters(libusb_context * ctx) 438{ 439 GET_CONTEXT(ctx);
|
327 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_event_waiters enter");
| 440 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_event_waiters enter");
|
328 329 pthread_mutex_unlock(&ctx->event_waiters_lock); 330
| 441 442 pthread_mutex_unlock(&ctx->event_waiters_lock); 443
|
331 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_event_waiters leave");
| 444 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_event_waiters leave");
|
332} 333 334int 335libusb_wait_for_event(libusb_context * ctx, struct timeval *tv) 336{ 337 int ret; 338 struct timespec ts; 339 340 GET_CONTEXT(ctx);
| 445} 446 447int 448libusb_wait_for_event(libusb_context * ctx, struct timeval *tv) 449{ 450 int ret; 451 struct timespec ts; 452 453 GET_CONTEXT(ctx);
|
341 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
| 454 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
|
342 343 if (tv == NULL) { 344 pthread_cond_wait(&ctx->event_waiters_cond, 345 &ctx->event_waiters_lock); 346 return (0); 347 } 348 349 ret = clock_gettime(CLOCK_REALTIME, &ts); 350 if (ret < 0) 351 return (LIBUSB_ERROR_OTHER); 352 353 ts.tv_sec = tv->tv_sec; 354 ts.tv_nsec = tv->tv_usec * 1000; 355 if (ts.tv_nsec > 1000000000) { 356 ts.tv_nsec -= 1000000000; 357 ts.tv_sec++; 358 } 359 360 ret = pthread_cond_timedwait(&ctx->event_waiters_cond, 361 &ctx->event_waiters_lock, &ts); 362 363 if (ret == ETIMEDOUT) 364 return (1); 365
| 455 456 if (tv == NULL) { 457 pthread_cond_wait(&ctx->event_waiters_cond, 458 &ctx->event_waiters_lock); 459 return (0); 460 } 461 462 ret = clock_gettime(CLOCK_REALTIME, &ts); 463 if (ret < 0) 464 return (LIBUSB_ERROR_OTHER); 465 466 ts.tv_sec = tv->tv_sec; 467 ts.tv_nsec = tv->tv_usec * 1000; 468 if (ts.tv_nsec > 1000000000) { 469 ts.tv_nsec -= 1000000000; 470 ts.tv_sec++; 471 } 472 473 ret = pthread_cond_timedwait(&ctx->event_waiters_cond, 474 &ctx->event_waiters_lock, &ts); 475 476 if (ret == ETIMEDOUT) 477 return (1); 478
|
366 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event leave");
| 479 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event leave");
|
367 return (0); 368} 369 370int 371libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv) 372{
| 480 return (0); 481} 482 483int 484libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv) 485{
|
373 struct timeval timeout;
| |
374 struct timeval poll_timeout; 375 int ret; 376 377 GET_CONTEXT(ctx);
| 486 struct timeval poll_timeout; 487 int ret; 488 489 GET_CONTEXT(ctx);
|
378 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
| 490 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
|
379 380 ret = get_next_timeout(ctx, tv, &poll_timeout); 381 if (ret != 0) { 382 return handle_timeouts(ctx); 383 } 384retry: 385 if (libusb_try_lock_events(ctx) == 0) { 386 ret = handle_events(ctx, &poll_timeout); 387 libusb_unlock_events(ctx); 388 return ret; 389 } 390 391 libusb_lock_event_waiters(ctx); 392 if (libusb_event_handler_active(ctx) == 0) { 393 libusb_unlock_event_waiters(ctx); 394 goto retry; 395 } 396 397 ret = libusb_wait_for_event(ctx, &poll_timeout); 398 libusb_unlock_event_waiters(ctx); 399 400 if (ret < 0) 401 return ret; 402 else if (ret == 1) 403 return (handle_timeouts(ctx)); 404
| 491 492 ret = get_next_timeout(ctx, tv, &poll_timeout); 493 if (ret != 0) { 494 return handle_timeouts(ctx); 495 } 496retry: 497 if (libusb_try_lock_events(ctx) == 0) { 498 ret = handle_events(ctx, &poll_timeout); 499 libusb_unlock_events(ctx); 500 return ret; 501 } 502 503 libusb_lock_event_waiters(ctx); 504 if (libusb_event_handler_active(ctx) == 0) { 505 libusb_unlock_event_waiters(ctx); 506 goto retry; 507 } 508 509 ret = libusb_wait_for_event(ctx, &poll_timeout); 510 libusb_unlock_event_waiters(ctx); 511 512 if (ret < 0) 513 return ret; 514 else if (ret == 1) 515 return (handle_timeouts(ctx)); 516
|
405 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
| 517 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
|
406 return (0); 407} 408 409int 410libusb_handle_events(libusb_context * ctx) 411{ 412 struct timeval tv; 413 int ret; 414 415 GET_CONTEXT(ctx);
| 518 return (0); 519} 520 521int 522libusb_handle_events(libusb_context * ctx) 523{ 524 struct timeval tv; 525 int ret; 526 527 GET_CONTEXT(ctx);
|
416 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events enter");
| 528 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events enter");
|
417 418 tv.tv_sec = 2; 419 tv.tv_usec = 0; 420 ret = libusb_handle_events_timeout(ctx, &tv); 421
| 529 530 tv.tv_sec = 2; 531 tv.tv_usec = 0; 532 ret = libusb_handle_events_timeout(ctx, &tv); 533
|
422 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events leave");
| 534 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events leave");
|
423 return (ret); 424} 425 426int 427libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv) 428{ 429 int ret; 430 struct timeval poll_tv; 431 432 GET_CONTEXT(ctx);
| 535 return (ret); 536} 537 538int 539libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv) 540{ 541 int ret; 542 struct timeval poll_tv; 543 544 GET_CONTEXT(ctx);
|
433 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_locked enter");
| 545 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_locked enter");
|
434 435 ret = get_next_timeout(ctx, tv, &poll_tv); 436 if (ret != 0) { 437 return handle_timeouts(ctx); 438 } 439 440 ret = handle_events(ctx, &poll_tv); 441
| 546 547 ret = get_next_timeout(ctx, tv, &poll_tv); 548 if (ret != 0) { 549 return handle_timeouts(ctx); 550 } 551 552 ret = handle_events(ctx, &poll_tv); 553
|
442 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_locked leave");
| 554 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_locked leave");
|
443 return (ret); 444} 445 446int 447libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv) 448{ 449 struct usb_transfer *xfer; 450 struct timeval *next_tv; 451 struct timeval cur_tv; 452 struct timespec cur_ts; 453 int found; 454 int ret; 455 456 GET_CONTEXT(ctx);
| 555 return (ret); 556} 557 558int 559libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv) 560{ 561 struct usb_transfer *xfer; 562 struct timeval *next_tv; 563 struct timeval cur_tv; 564 struct timespec cur_ts; 565 int found; 566 int ret; 567 568 GET_CONTEXT(ctx);
|
457 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_next_timeout enter");
| 569 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_next_timeout enter");
|
458 459 found = 0; 460 pthread_mutex_lock(&ctx->flying_transfers_lock);
| 570 571 found = 0; 572 pthread_mutex_lock(&ctx->flying_transfers_lock);
|
461 if (USB_LIST_EMPTY(&ctx->flying_transfers)) {
| 573 if (TAILQ_EMPTY(&ctx->flying_transfers)) {
|
462 pthread_mutex_unlock(&ctx->flying_transfers_lock); 463 return (0); 464 } 465
| 574 pthread_mutex_unlock(&ctx->flying_transfers_lock); 575 return (0); 576 } 577
|
466 LIST_FOREACH_ENTRY(xfer, &ctx->flying_transfers, list) {
| 578 TAILQ_FOREACH(xfer, &ctx->flying_transfers, list) {
|
467 if (!(xfer->flags & USB_TIMED_OUT)) { 468 found = 1; 469 break ; 470 } 471 } 472 pthread_mutex_unlock(&ctx->flying_transfers_lock); 473 474 if (found == 0) { 475 return 0; 476 } 477 478 next_tv = &xfer->timeout; 479 if (timerisset(next_tv) == 0) 480 return (0); 481 482 ret = clock_gettime(CLOCK_MONOTONIC, &cur_ts); 483 if (ret < 0) 484 return (LIBUSB_ERROR_OTHER); 485 TIMESPEC_TO_TIMEVAL(&cur_tv, &cur_ts); 486 487 if (timercmp(&cur_tv, next_tv, >=) != 0) 488 timerclear(tv); 489 else 490 timersub(next_tv, &cur_tv, tv); 491
| 579 if (!(xfer->flags & USB_TIMED_OUT)) { 580 found = 1; 581 break ; 582 } 583 } 584 pthread_mutex_unlock(&ctx->flying_transfers_lock); 585 586 if (found == 0) { 587 return 0; 588 } 589 590 next_tv = &xfer->timeout; 591 if (timerisset(next_tv) == 0) 592 return (0); 593 594 ret = clock_gettime(CLOCK_MONOTONIC, &cur_ts); 595 if (ret < 0) 596 return (LIBUSB_ERROR_OTHER); 597 TIMESPEC_TO_TIMEVAL(&cur_tv, &cur_ts); 598 599 if (timercmp(&cur_tv, next_tv, >=) != 0) 600 timerclear(tv); 601 else 602 timersub(next_tv, &cur_tv, tv); 603
|
492 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_next_timeout leave");
| 604 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_next_timeout leave");
|
493 return (1); 494} 495 496void 497libusb_set_pollfd_notifiers(libusb_context * ctx, 498 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, 499 void *user_data) 500{ 501 GET_CONTEXT(ctx);
| 605 return (1); 606} 607 608void 609libusb_set_pollfd_notifiers(libusb_context * ctx, 610 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, 611 void *user_data) 612{ 613 GET_CONTEXT(ctx);
|
502 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_pollfd_notifiers enter");
| 614 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_pollfd_notifiers enter");
|
503 504 ctx->fd_added_cb = added_cb; 505 ctx->fd_removed_cb = removed_cb; 506 ctx->fd_cb_user_data = user_data; 507
| 615 616 ctx->fd_added_cb = added_cb; 617 ctx->fd_removed_cb = removed_cb; 618 ctx->fd_cb_user_data = user_data; 619
|
508 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_pollfd_notifiers leave");
| 620 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_pollfd_notifiers leave");
|
509} 510 511struct libusb_pollfd ** 512libusb_get_pollfds(libusb_context * ctx) 513{ 514 struct usb_pollfd *pollfd; 515 libusb_pollfd **ret; 516 int i; 517 518 GET_CONTEXT(ctx);
| 621} 622 623struct libusb_pollfd ** 624libusb_get_pollfds(libusb_context * ctx) 625{ 626 struct usb_pollfd *pollfd; 627 libusb_pollfd **ret; 628 int i; 629 630 GET_CONTEXT(ctx);
|
519 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_pollfds enter");
| 631 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_pollfds enter");
|
520 521 i = 0; 522 pthread_mutex_lock(&ctx->pollfds_lock);
| 632 633 i = 0; 634 pthread_mutex_lock(&ctx->pollfds_lock);
|
523 LIST_FOREACH_ENTRY(pollfd, &ctx->pollfds, list)
| 635 TAILQ_FOREACH(pollfd, &ctx->pollfds, list)
|
524 i++; 525 526 ret = calloc(i + 1 , sizeof(struct libusb_pollfd *)); 527 if (ret == NULL) { 528 pthread_mutex_unlock(&ctx->pollfds_lock); 529 return (ret); 530 } 531 532 i = 0;
| 636 i++; 637 638 ret = calloc(i + 1 , sizeof(struct libusb_pollfd *)); 639 if (ret == NULL) { 640 pthread_mutex_unlock(&ctx->pollfds_lock); 641 return (ret); 642 } 643 644 i = 0;
|
533 LIST_FOREACH_ENTRY(pollfd, &ctx->pollfds, list)
| 645 TAILQ_FOREACH(pollfd, &ctx->pollfds, list)
|
534 ret[i++] = (struct libusb_pollfd *) pollfd; 535 ret[i] = NULL; 536
| 646 ret[i++] = (struct libusb_pollfd *) pollfd; 647 ret[i] = NULL; 648
|
537 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_pollfds leave");
| 649 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_pollfds leave");
|
538 return (ret); 539} 540 541 542/* Synchronous device I/O */ 543 544static void ctrl_tr_cb(struct libusb_transfer *transfer) 545{ 546 libusb_context *ctx; 547 int *complet; 548 549 ctx = NULL; 550 GET_CONTEXT(ctx);
| 650 return (ret); 651} 652 653 654/* Synchronous device I/O */ 655 656static void ctrl_tr_cb(struct libusb_transfer *transfer) 657{ 658 libusb_context *ctx; 659 int *complet; 660 661 ctx = NULL; 662 GET_CONTEXT(ctx);
|
551 dprintf(ctx, LIBUSB_DEBUG_TRANSFER, "CALLBACK ENTER");
| 663 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "CALLBACK ENTER");
|
552 553 complet = transfer->user_data; 554 *complet = 1; 555} 556 557int 558libusb_control_transfer(libusb_device_handle * devh, 559 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 560 unsigned char *data, uint16_t wLength, unsigned int timeout) 561{ 562 struct libusb_transfer *xfer; 563 struct libusb_control_setup *ctr; 564 libusb_context *ctx; 565 unsigned char *buff; 566 int complet; 567 int ret; 568 569 ctx = devh->dev->ctx; 570 GET_CONTEXT(ctx);
| 664 665 complet = transfer->user_data; 666 *complet = 1; 667} 668 669int 670libusb_control_transfer(libusb_device_handle * devh, 671 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 672 unsigned char *data, uint16_t wLength, unsigned int timeout) 673{ 674 struct libusb_transfer *xfer; 675 struct libusb_control_setup *ctr; 676 libusb_context *ctx; 677 unsigned char *buff; 678 int complet; 679 int ret; 680 681 ctx = devh->dev->ctx; 682 GET_CONTEXT(ctx);
|
571 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_control_transfer enter");
| 683 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_control_transfer enter");
|
572 573 if (devh == NULL || data == NULL) 574 return (LIBUSB_ERROR_NO_MEM); 575 576 xfer = libusb_alloc_transfer(0); 577 if (xfer == NULL) 578 return (LIBUSB_ERROR_NO_MEM); 579 580 buff = malloc(sizeof(libusb_control_setup) + wLength); 581 if (buff == NULL) { 582 libusb_free_transfer(xfer); 583 return (LIBUSB_ERROR_NO_MEM); 584 } 585 586 ctr = (libusb_control_setup *)buff; 587 ctr->bmRequestType = bmRequestType; 588 ctr->bRequest = bRequest; 589 ctr->wValue = wValue; 590 ctr->wIndex = wIndex; 591 ctr->wLength = wLength; 592 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) 593 memcpy(buff + sizeof(libusb_control_setup), data, wLength); 594 595 xfer->dev_handle = devh; 596 xfer->endpoint = 0; 597 xfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; 598 xfer->timeout = timeout; 599 xfer->buffer = buff; 600 xfer->length = sizeof(libusb_control_setup) + wLength; 601 xfer->user_data = &complet; 602 xfer->callback = ctrl_tr_cb; 603 xfer->flags = LIBUSB_TRANSFER_FREE_TRANSFER; 604 complet = 0; 605 606 if ((ret = libusb_submit_transfer(xfer)) < 0) { 607 libusb_free_transfer(xfer); 608 return (ret); 609 } 610 611 while (complet == 0) 612 if ((ret = libusb_handle_events(ctx)) < 0) { 613 libusb_cancel_transfer(xfer); 614 while (complet == 0) 615 if (libusb_handle_events(ctx) < 0) { 616 break; 617 } 618 libusb_free_transfer(xfer); 619 return (ret); 620 } 621 622 623 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) 624 memcpy(data, buff + sizeof(libusb_control_setup), wLength); 625 626 switch (xfer->status) { 627 case LIBUSB_TRANSFER_COMPLETED: 628 ret = xfer->actual_length; 629 break; 630 case LIBUSB_TRANSFER_TIMED_OUT: 631 case LIBUSB_TRANSFER_STALL: 632 case LIBUSB_TRANSFER_NO_DEVICE: 633 ret = xfer->status; 634 break; 635 default: 636 ret = LIBUSB_ERROR_OTHER; 637 } 638 libusb_free_transfer(xfer); 639
| 684 685 if (devh == NULL || data == NULL) 686 return (LIBUSB_ERROR_NO_MEM); 687 688 xfer = libusb_alloc_transfer(0); 689 if (xfer == NULL) 690 return (LIBUSB_ERROR_NO_MEM); 691 692 buff = malloc(sizeof(libusb_control_setup) + wLength); 693 if (buff == NULL) { 694 libusb_free_transfer(xfer); 695 return (LIBUSB_ERROR_NO_MEM); 696 } 697 698 ctr = (libusb_control_setup *)buff; 699 ctr->bmRequestType = bmRequestType; 700 ctr->bRequest = bRequest; 701 ctr->wValue = wValue; 702 ctr->wIndex = wIndex; 703 ctr->wLength = wLength; 704 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) 705 memcpy(buff + sizeof(libusb_control_setup), data, wLength); 706 707 xfer->dev_handle = devh; 708 xfer->endpoint = 0; 709 xfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; 710 xfer->timeout = timeout; 711 xfer->buffer = buff; 712 xfer->length = sizeof(libusb_control_setup) + wLength; 713 xfer->user_data = &complet; 714 xfer->callback = ctrl_tr_cb; 715 xfer->flags = LIBUSB_TRANSFER_FREE_TRANSFER; 716 complet = 0; 717 718 if ((ret = libusb_submit_transfer(xfer)) < 0) { 719 libusb_free_transfer(xfer); 720 return (ret); 721 } 722 723 while (complet == 0) 724 if ((ret = libusb_handle_events(ctx)) < 0) { 725 libusb_cancel_transfer(xfer); 726 while (complet == 0) 727 if (libusb_handle_events(ctx) < 0) { 728 break; 729 } 730 libusb_free_transfer(xfer); 731 return (ret); 732 } 733 734 735 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) 736 memcpy(data, buff + sizeof(libusb_control_setup), wLength); 737 738 switch (xfer->status) { 739 case LIBUSB_TRANSFER_COMPLETED: 740 ret = xfer->actual_length; 741 break; 742 case LIBUSB_TRANSFER_TIMED_OUT: 743 case LIBUSB_TRANSFER_STALL: 744 case LIBUSB_TRANSFER_NO_DEVICE: 745 ret = xfer->status; 746 break; 747 default: 748 ret = LIBUSB_ERROR_OTHER; 749 } 750 libusb_free_transfer(xfer); 751
|
640 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_control_transfer leave");
| 752 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_control_transfer leave");
|
641 return (ret); 642} 643 644static int 645do_transfer(struct libusb_device_handle *devh, 646 unsigned char endpoint, unsigned char *data, int length, 647 int *transferred, unsigned int timeout, int type) 648{ 649 struct libusb_transfer *xfer; 650 libusb_context *ctx; 651 int complet; 652 int ret; 653 654 if (devh == NULL || data == NULL) 655 return (LIBUSB_ERROR_NO_MEM); 656 657 xfer = libusb_alloc_transfer(0); 658 if (xfer == NULL) 659 return (LIBUSB_ERROR_NO_MEM); 660 661 ctx = devh->dev->ctx;
| 753 return (ret); 754} 755 756static int 757do_transfer(struct libusb_device_handle *devh, 758 unsigned char endpoint, unsigned char *data, int length, 759 int *transferred, unsigned int timeout, int type) 760{ 761 struct libusb_transfer *xfer; 762 libusb_context *ctx; 763 int complet; 764 int ret; 765 766 if (devh == NULL || data == NULL) 767 return (LIBUSB_ERROR_NO_MEM); 768 769 xfer = libusb_alloc_transfer(0); 770 if (xfer == NULL) 771 return (LIBUSB_ERROR_NO_MEM); 772 773 ctx = devh->dev->ctx;
|
| 774 GET_CONTEXT(ctx);
|
662 663 xfer->dev_handle = devh; 664 xfer->endpoint = endpoint; 665 xfer->type = type; 666 xfer->timeout = timeout; 667 xfer->buffer = data; 668 xfer->length = length; 669 xfer->user_data = &complet; 670 xfer->callback = ctrl_tr_cb; 671 complet = 0; 672
| 775 776 xfer->dev_handle = devh; 777 xfer->endpoint = endpoint; 778 xfer->type = type; 779 xfer->timeout = timeout; 780 xfer->buffer = data; 781 xfer->length = length; 782 xfer->user_data = &complet; 783 xfer->callback = ctrl_tr_cb; 784 complet = 0; 785
|
| 786 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "SUBMIT_TRANSFER");
|
673 if ((ret = libusb_submit_transfer(xfer)) < 0) { 674 libusb_free_transfer(xfer);
| 787 if ((ret = libusb_submit_transfer(xfer)) < 0) { 788 libusb_free_transfer(xfer);
|
| 789 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "SUBMIT_TRANSFER FAILED %i", ret);
|
675 return (ret); 676 } 677 678 while (complet == 0) { 679 if ((ret = libusb_handle_events(ctx)) < 0) { 680 libusb_cancel_transfer(xfer); 681 libusb_free_transfer(xfer); 682 while (complet == 0) { 683 if (libusb_handle_events(ctx) < 0) 684 break ; 685 } 686 return (ret); 687 } 688 } 689 690 *transferred = xfer->actual_length;
| 790 return (ret); 791 } 792 793 while (complet == 0) { 794 if ((ret = libusb_handle_events(ctx)) < 0) { 795 libusb_cancel_transfer(xfer); 796 libusb_free_transfer(xfer); 797 while (complet == 0) { 798 if (libusb_handle_events(ctx) < 0) 799 break ; 800 } 801 return (ret); 802 } 803 } 804 805 *transferred = xfer->actual_length;
|
| 806 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "xfer->status %i", xfer->status);
|
691 switch (xfer->status) { 692 case LIBUSB_TRANSFER_COMPLETED: 693 ret = xfer->actual_length; 694 break; 695 case LIBUSB_TRANSFER_TIMED_OUT: 696 case LIBUSB_TRANSFER_OVERFLOW: 697 case LIBUSB_TRANSFER_STALL: 698 case LIBUSB_TRANSFER_NO_DEVICE: 699 ret = xfer->status; 700 break; 701 default: 702 ret = LIBUSB_ERROR_OTHER; 703 } 704 705 libusb_free_transfer(xfer); 706 return (ret); 707} 708 709int 710libusb_bulk_transfer(struct libusb_device_handle *devh, 711 unsigned char endpoint, unsigned char *data, int length, 712 int *transferred, unsigned int timeout) 713{ 714 libusb_context *ctx; 715 int ret; 716 717 ctx = NULL; 718 GET_CONTEXT(ctx);
| 807 switch (xfer->status) { 808 case LIBUSB_TRANSFER_COMPLETED: 809 ret = xfer->actual_length; 810 break; 811 case LIBUSB_TRANSFER_TIMED_OUT: 812 case LIBUSB_TRANSFER_OVERFLOW: 813 case LIBUSB_TRANSFER_STALL: 814 case LIBUSB_TRANSFER_NO_DEVICE: 815 ret = xfer->status; 816 break; 817 default: 818 ret = LIBUSB_ERROR_OTHER; 819 } 820 821 libusb_free_transfer(xfer); 822 return (ret); 823} 824 825int 826libusb_bulk_transfer(struct libusb_device_handle *devh, 827 unsigned char endpoint, unsigned char *data, int length, 828 int *transferred, unsigned int timeout) 829{ 830 libusb_context *ctx; 831 int ret; 832 833 ctx = NULL; 834 GET_CONTEXT(ctx);
|
719 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
| 835 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
|
720 721 ret = do_transfer(devh, endpoint, data, length, transferred, 722 timeout, LIBUSB_TRANSFER_TYPE_BULK); 723
| 836 837 ret = do_transfer(devh, endpoint, data, length, transferred, 838 timeout, LIBUSB_TRANSFER_TYPE_BULK); 839
|
724 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
| 840 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
|
725 return (ret); 726} 727 728/* 729 * Need to fix xfer->type 730 */ 731int 732libusb_interrupt_transfer(struct libusb_device_handle *devh, 733 unsigned char endpoint, unsigned char *data, int length, 734 int *transferred, unsigned int timeout) 735{ 736 libusb_context *ctx; 737 int ret; 738 739 ctx = NULL; 740 GET_CONTEXT(ctx);
| 841 return (ret); 842} 843 844/* 845 * Need to fix xfer->type 846 */ 847int 848libusb_interrupt_transfer(struct libusb_device_handle *devh, 849 unsigned char endpoint, unsigned char *data, int length, 850 int *transferred, unsigned int timeout) 851{ 852 libusb_context *ctx; 853 int ret; 854 855 ctx = NULL; 856 GET_CONTEXT(ctx);
|
741 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
| 857 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
|
742 743 ret = do_transfer(devh, endpoint, data, length, transferred, 744 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT); 745
| 858 859 ret = do_transfer(devh, endpoint, data, length, transferred, 860 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT); 861
|
746 dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
| 862 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
|
747 return (ret); 748}
| 863 return (ret); 864}
|