From ed8f1abaabc33501424e7df05cd8fd59073b64cd Mon Sep 17 00:00:00 2001 From: hselasky Date: Fri, 4 May 2012 16:25:35 +0000 Subject: [PATCH] MFC r228235, r228236, r233667, r234687, r234491, r234193, and r233424: Minor code simplification. No functional change. Fix some compile warnings. Fix some mdoc issues. Add missing LibUSB 1.0 API function. git-svn-id: svn://svn.freebsd.org/base/stable/8@235017 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libusb/Makefile | 3 ++- lib/libusb/libusb.3 | 22 +++++++---------- lib/libusb/libusb.h | 1 + lib/libusb/libusb10.c | 46 +++++++++++++++++++++++++++--------- lib/libusb/libusb10_desc.c | 8 +++---- lib/libusb/libusb10_io.c | 20 ++++++++-------- lib/libusb/libusb20.3 | 3 --- lib/libusb/libusb20.c | 5 ++-- lib/libusb/libusb20_desc.c | 14 +++++------ lib/libusb/libusb20_ugen20.c | 2 +- 10 files changed, 71 insertions(+), 53 deletions(-) diff --git a/lib/libusb/Makefile b/lib/libusb/Makefile index ac5f679d5..ae190c38a 100644 --- a/lib/libusb/Makefile +++ b/lib/libusb/Makefile @@ -46,6 +46,7 @@ MLINKS += libusb.3 libusb_get_bus_number.3 MLINKS += libusb.3 libusb_get_device_address.3 MLINKS += libusb.3 libusb_get_device_speed.3 MLINKS += libusb.3 libusb_get_max_packet_size.3 +MLINKS += libusb.3 libusb_get_max_iso_packet_size.3 MLINKS += libusb.3 libusb_ref_device.3 MLINKS += libusb.3 libusb_unref_device.3 MLINKS += libusb.3 libusb_open.3 @@ -67,7 +68,7 @@ MLINKS += libusb.3 libusb_detach_kernel_driver.3 MLINKS += libusb.3 libusb_detach_kernel_driver_np.3 MLINKS += libusb.3 libusb_attach_kernel_driver.3 MLINKS += libusb.3 libusb_get_device_descriptor.3 -MLINKS += libusb.3 libsub_get_active_config_descriptor.3 +MLINKS += libusb.3 libusb_get_active_config_descriptor.3 MLINKS += libusb.3 libusb_get_config_descriptor.3 MLINKS += libusb.3 libusb_get_config_descriptor_by_value.3 MLINKS += libusb.3 libusb_free_config_descriptor.3 diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index c3f2a4487..6b1defdcc 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" -.Dd February 25, 2012 +.Dd April 12, 2012 .Dt LIBUSB 3 .Os .Sh NAME @@ -43,7 +43,6 @@ The library contains interfaces for directly managing a usb device. The current implementation supports v1.0 of the libusb API. .Sh LIBRARY INITIALISATION / DEINITIALISATION -.Pp .Ft int .Fn libusb_init libusb_context **ctx This function initialises libusb. @@ -119,6 +118,12 @@ LIBUSB_SPEED_UNKNOWN is returned in case of unknown wire speed. Returns the wMaxPacketSize value on success, LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist and LIBUSB_ERROR_OTHERS on other failure. .Pp +.Ft int +.Fn libusb_get_max_iso_packet_size "libusb_device *dev" "unsigned char endpoint" +Returns the packet size multiplied by the packet multiplier on success, +LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist and +LIBUSB_ERROR_OTHERS on other failure. +.Pp .Ft libusb_device * .Fn libusb_ref_device "libusb_device *dev" Increment the reference counter of the device @@ -270,9 +275,7 @@ LIBUSB_ERROR_NO_DEVICE if the device has been disconnected, LIBUSB_ERROR_BUSY if the driver cannot be attached because the interface is claimed by a program or driver and a LIBUSB_ERROR code on failure. -.Pp .Sh USB DESCRIPTORS -.Pp .Ft int .Fn libusb_get_device_descriptor "libusb_device *dev" "libusb_device_descriptor *desc" Get the USB device descriptor for the device @@ -282,7 +285,7 @@ Returns 0 on success and a LIBUSB_ERROR code on failure. .Pp .Ft int -.Fn libsub_get_active_config_descriptor "libusb_device *dev" "struct libusb_config_descriptor **config" +.Fn libusb_get_active_config_descriptor "libusb_device *dev" "struct libusb_config_descriptor **config" Get the USB configuration descriptor for the active configuration. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the device is in @@ -349,9 +352,7 @@ libusb_free_bos_descriptor function. .Ft void .Fn libusb_free_bos_descriptor "libusb_bos_descriptor *bos" This function is NULL safe and frees a parsed BOS descriptor. -.Pp .Sh USB ASYNCHRONOUS I/O -.Pp .Ft struct libusb_transfer * .Fn libusb_alloc_transfer "int iso_packets" Allocate a transfer with the number of isochronous packet descriptors @@ -374,9 +375,7 @@ LIBUSB_ERROR code on other failure. .Fn libusb_cancel_transfer "struct libusb_transfer *tr" This function asynchronously cancels a transfer. Returns 0 on success and a LIBUSB_ERROR code on failure. -.Pp .Sh USB SYNCHRONOUS I/O -.Pp .Ft int .Fn libusb_control_transfer "libusb_device_handle *devh" "uint8_t bmRequestType" "uint8_t bRequest" "uint16_t wValue" "uint16_t wIndex" "unsigned char *data" "uint16_t wLength" "unsigned int timeout" Perform a USB control transfer. @@ -411,9 +410,7 @@ if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not supported, LIBUSB_ERROR_OVERFLOW if the device offered more data, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on other failure. -.Pp .Sh USB EVENTS -.Pp .Ft int .Fn libusb_try_lock_events "libusb_context *ctx" Try to acquire the event handling lock. @@ -429,7 +426,7 @@ This function is blocking. Release the event handling lock. This will wake up any thread blocked on -.B libusb_wait_for_event() . +.Fn libusb_wait_for_event . .Pp .Ft int .Fn libusb_event_handling_ok "libusb_context *ctx" @@ -506,7 +503,6 @@ Retrive a list of file descriptors that should be polled by your main loop as libusb event sources. Returns a NULL-terminated list on success or NULL on failure. .Sh LIBUSB VERSION 0.1 COMPATIBILITY -.Pp The library is also compliant with LibUSB version 0.1.12. .Pp .Fn usb_open diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index 5a4d2df98..5f821b613 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -371,6 +371,7 @@ uint8_t libusb_get_device_address(libusb_device * dev); enum libusb_speed libusb_get_device_speed(libusb_device * dev); int libusb_clear_halt(libusb_device_handle *devh, uint8_t endpoint); int libusb_get_max_packet_size(libusb_device * dev, uint8_t endpoint); +int libusb_get_max_iso_packet_size(libusb_device * dev, uint8_t endpoint); libusb_device *libusb_ref_device(libusb_device * dev); void libusb_unref_device(libusb_device * dev); int libusb_open(libusb_device * dev, libusb_device_handle ** devh); diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index 0a7c1e6b1..893160ee7 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -331,6 +331,30 @@ out: return (ret); } +int +libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint) +{ + int multiplier; + int ret; + + ret = libusb_get_max_packet_size(dev, endpoint); + + switch (libusb20_dev_get_speed(dev->os_priv)) { + case LIBUSB20_SPEED_LOW: + case LIBUSB20_SPEED_FULL: + break; + default: + if (ret > -1) { + multiplier = (1 + ((ret >> 11) & 3)); + if (multiplier > 3) + multiplier = 3; + ret = (ret & 0x7FF) * multiplier; + } + break; + } + return (ret); +} + libusb_device * libusb_ref_device(libusb_device *dev) { @@ -417,9 +441,12 @@ libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, if ((i = libusb_get_device_list(ctx, &devs)) < 0) return (NULL); + pdev = NULL; for (j = 0; j < i; j++) { - pdev = devs[j]->os_priv; - pdesc = libusb20_dev_get_device_desc(pdev); + struct libusb20_device *tdev; + + tdev = devs[j]->os_priv; + pdesc = libusb20_dev_get_device_desc(tdev); /* * NOTE: The USB library will automatically swap the * fields in the device descriptor to be of host @@ -427,13 +454,10 @@ libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, */ if (pdesc->idVendor == vendor_id && pdesc->idProduct == product_id) { - if (libusb_open(devs[j], &pdev) < 0) - pdev = NULL; + libusb_open(devs[j], &pdev); break; } } - if (j == i) - pdev = NULL; libusb_free_device_list(devs, 1); DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave"); @@ -627,17 +651,17 @@ libusb_set_interface_alt_setting(struct libusb20_device *pdev, static struct libusb20_transfer * libusb10_get_transfer(struct libusb20_device *pdev, - uint8_t endpoint, uint8_t index) + uint8_t endpoint, uint8_t xfer_index) { - index &= 1; /* double buffering */ + xfer_index &= 1; /* double buffering */ - index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4; + xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4; if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) { /* this is an IN endpoint */ - index |= 2; + xfer_index |= 2; } - return (libusb20_tr_get_pointer(pdev, index)); + return (libusb20_tr_get_pointer(pdev, xfer_index)); } int diff --git a/lib/libusb/libusb10_desc.c b/lib/libusb/libusb10_desc.c index 6d5822e8c..8e39bab15 100644 --- a/lib/libusb/libusb10_desc.c +++ b/lib/libusb/libusb10_desc.c @@ -390,8 +390,8 @@ libusb_parse_bos_descriptor(const void *buf, int len, struct libusb_bos_descriptor **bos) { struct libusb_bos_descriptor *ptr; - struct libusb_usb_2_0_device_capability_descriptor *dcap_20; - struct libusb_ss_usb_device_capability_descriptor *ss_cap; + struct libusb_usb_2_0_device_capability_descriptor *dcap_20 = NULL; + struct libusb_ss_usb_device_capability_descriptor *ss_cap = NULL; if (buf == NULL || bos == NULL || len < 1) return (LIBUSB_ERROR_INVALID_PARAM); @@ -438,7 +438,7 @@ libusb_parse_bos_descriptor(const void *buf, int len, dtype == LIBUSB_DT_DEVICE_CAPABILITY) { switch (((const uint8_t *)buf)[2]) { case LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY: - if (ptr->usb_2_0_ext_cap != NULL) + if (ptr->usb_2_0_ext_cap != NULL || dcap_20 == NULL) break; if (dlen < LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE) break; @@ -455,7 +455,7 @@ libusb_parse_bos_descriptor(const void *buf, int len, break; case LIBUSB_SS_USB_DEVICE_CAPABILITY: - if (ptr->ss_usb_cap != NULL) + if (ptr->ss_usb_cap != NULL || ss_cap == NULL) break; if (dlen < LIBUSB_SS_USB_DEVICE_CAPABILITY_SIZE) break; diff --git a/lib/libusb/libusb10_io.c b/lib/libusb/libusb10_io.c index 380e312bf..63f833af8 100644 --- a/lib/libusb/libusb10_io.c +++ b/lib/libusb/libusb10_io.c @@ -481,7 +481,7 @@ libusb10_do_transfer(libusb_device_handle *devh, { libusb_context *ctx; struct libusb_transfer *xfer; - volatile int complet; + int done; int ret; if (devh == NULL) @@ -502,15 +502,15 @@ libusb10_do_transfer(libusb_device_handle *devh, xfer->timeout = timeout; xfer->buffer = data; xfer->length = length; - xfer->user_data = (void *)&complet; + xfer->user_data = (void *)&done; xfer->callback = libusb10_do_transfer_cb; - complet = 0; + done = 0; if ((ret = libusb_submit_transfer(xfer)) < 0) { libusb_free_transfer(xfer); return (ret); } - while (complet == 0) { + while (done == 0) { if ((ret = libusb_handle_events(ctx)) < 0) { libusb_cancel_transfer(xfer); usleep(1000); /* nice it */ @@ -581,7 +581,7 @@ libusb_interrupt_transfer(libusb_device_handle *devh, } uint8_t * -libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index) +libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off) { uint8_t *ptr; uint32_t n; @@ -589,35 +589,35 @@ libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index) if (transfer->num_iso_packets < 0) return (NULL); - if (index >= (uint32_t)transfer->num_iso_packets) + if (off >= (uint32_t)transfer->num_iso_packets) return (NULL); ptr = transfer->buffer; if (ptr == NULL) return (NULL); - for (n = 0; n != index; n++) { + for (n = 0; n != off; n++) { ptr += transfer->iso_packet_desc[n].length; } return (ptr); } uint8_t * -libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index) +libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off) { uint8_t *ptr; if (transfer->num_iso_packets < 0) return (NULL); - if (index >= (uint32_t)transfer->num_iso_packets) + if (off >= (uint32_t)transfer->num_iso_packets) return (NULL); ptr = transfer->buffer; if (ptr == NULL) return (NULL); - ptr += transfer->iso_packet_desc[0].length * index; + ptr += transfer->iso_packet_desc[0].length * off; return (ptr); } diff --git a/lib/libusb/libusb20.3 b/lib/libusb/libusb20.3 index bd167b239..f5c9a5ea9 100644 --- a/lib/libusb/libusb20.3 +++ b/lib/libusb/libusb20.3 @@ -235,7 +235,6 @@ applications should consider using . .Sh USB TRANSFER OPERATIONS . -.Pp . .Fn libusb20_tr_close will release all kernel resources associated with an USB @@ -534,7 +533,6 @@ with an USB transfer. . .Sh USB DEVICE OPERATIONS . -.Pp . .Fn libusb20_dev_get_backend_name returns a zero terminated string describing the backend used. @@ -1000,7 +998,6 @@ The buffer pointer cannot be NULL. . . .Sh USB DEBUGGING -.Pp .Ft const char * .Fn libusb20_strerror "int code" Get the ASCII representation of the error given by the diff --git a/lib/libusb/libusb20.c b/lib/libusb/libusb20.c index 747c16003..75af7a186 100644 --- a/lib/libusb/libusb20.c +++ b/lib/libusb/libusb20.c @@ -948,9 +948,8 @@ libusb20_dev_get_config_index(struct libusb20_device *pdev) } error = pdev->methods->get_config_index(pdev, &cfg_index); - if (error) { - cfg_index = 0 - 1; /* current config index */ - } + if (error) + cfg_index = 0xFF; /* current config index */ if (do_close) { if (libusb20_dev_close(pdev)) { /* ignore */ diff --git a/lib/libusb/libusb20_desc.c b/lib/libusb/libusb20_desc.c index 0781067be..e2fa5b55c 100644 --- a/lib/libusb/libusb20_desc.c +++ b/lib/libusb/libusb20_desc.c @@ -69,7 +69,7 @@ libusb20_parse_config_desc(const void *config_desc) uint16_t niface_no_alt; uint16_t niface; uint16_t nendpoint; - uint8_t iface_no; + uint16_t iface_no; ptr = config_desc; if (ptr[1] != LIBUSB20_DT_CONFIG) { @@ -82,7 +82,7 @@ libusb20_parse_config_desc(const void *config_desc) niface_no_alt = 0; nendpoint = 0; niface = 0; - iface_no = 0 - 1; + iface_no = 0xFFFF; ptr = NULL; /* get "wTotalLength" and setup "pcdesc" */ @@ -155,7 +155,7 @@ libusb20_parse_config_desc(const void *config_desc) /* reset states */ niface = 0; - iface_no = 0 - 1; + iface_no = 0xFFFF; ptr = NULL; lub_interface--; lub_endpoint--; @@ -450,7 +450,7 @@ libusb20_me_encode(void *ptr, uint16_t len, const void *pd) * and should be * correct: */ - ps->len = 0 - 1; + ps->len = 0xFFFF; } src_len = libusb20_me_get_1(pd, 0); src_ptr = LIBUSB20_ADD_BYTES(ps->ptr, 1); @@ -465,7 +465,7 @@ libusb20_me_encode(void *ptr, uint16_t len, const void *pd) case LIBUSB20_ME_IS_DECODED: /* reserve 3 length bytes */ src_len = libusb20_me_encode(NULL, - 0 - 1 - 3, ps->ptr); + 0xFFFF - 3, ps->ptr); src_ptr = NULL; break; @@ -476,7 +476,7 @@ libusb20_me_encode(void *ptr, uint16_t len, const void *pd) } if (src_len > 0xFE) { - if (src_len > (uint16_t)(0 - 1 - 3)) + if (src_len > (0xFFFF - 3)) /* overflow */ goto done; @@ -516,7 +516,7 @@ libusb20_me_encode(void *ptr, uint16_t len, const void *pd) uint16_t dummy; dummy = libusb20_me_encode(buf, - 0 - 1 - 3, ps->ptr); + 0xFFFF - 3, ps->ptr); } else { bcopy(src_ptr, buf, src_len); } diff --git a/lib/libusb/libusb20_ugen20.c b/lib/libusb/libusb20_ugen20.c index 307ed96d2..17c948b4a 100644 --- a/lib/libusb/libusb20_ugen20.c +++ b/lib/libusb/libusb20_ugen20.c @@ -104,7 +104,7 @@ ugen20_path_convert_one(const char **pp) temp += (*ptr - '0'); if (temp >= 1000000) { /* catch overflow early */ - return (0 - 1); + return (0xFFFFFFFF); } ptr++; } -- 2.45.0