From 17d258a6e9b4823f3ceb69ebc87b47bf1ba0df39 Mon Sep 17 00:00:00 2001 From: thompsa Date: Fri, 19 Nov 2010 21:04:18 +0000 Subject: [PATCH] MFC r213853 - Add missing LibUSB API functions: * libusb_strerror() * libusb_get_driver[_np]() * libusb_detach_kernel_driver[_np]() - Factor out setting of non-blocking flag inside libusb. - Add missing NULL check after libusb_get_device() call. - Correct some wrong error codes due to copy and paste error. PR: usb/150546 Submitted by: Robert Jenssen, Alexander Leidinger git-svn-id: svn://svn.freebsd.org/base/stable/8@215546 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libusb/libusb.3 | 50 +++++++++++++++++++++----- lib/libusb/libusb.h | 4 +++ lib/libusb/libusb10.c | 84 ++++++++++++++++++++++++++++++++++++------- lib/libusb/libusb20.3 | 4 +-- 4 files changed, 120 insertions(+), 22 deletions(-) diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index 406685252..8fccea344 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 22, 2009 +.Dd October 14, 2010 .Dt LIBUSB 3 .Os .Sh NAME @@ -72,6 +72,15 @@ Deinitialise libusb. Must be called at the end of the application. . .Pp . +.Ft const char * +.Fn libusb_strerror "int code" +Get ASCII representation of the error given by the +.Fa code +argument. +. +. +.Pp +. .Ft void .Fn libusb_set_debug "libusb_context *ctx" "int level" Set debug to the @@ -239,12 +248,37 @@ if the device has been disconnected and return a LIBUSB_ERROR code on failure. .Pp . .Ft int +.Fn libusb_get_driver "libusb_device_handle *devh" "int interface" "char *name" "int namelen" +or +.Ft int +.Fn libusb_get_driver_np "libusb_device_handle *devh" "int interface" "char *name" "int namelen" +Gets the name of the driver attached to the given +.Fa device +and +.Fa interface +into the buffer given by +.Fa name +and +.Fa namelen . +Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver is attached +to the given interface and LIBUSB_ERROR_INVALID_PARAM if the interface does +not exist. +This function is non-portable. +The buffer pointed to by +.Fa name +is only zero terminated on success. +. +.Pp +. +.Ft int .Fn libusb_detach_kernel_driver "libusb_device_handle *devh" "int interface" -Detach a kernel driver from an interface. This is needed to claim an interface -required by a kernel driver. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if -no kernel driver was active, LIBUSB_ERROR_INVALID_PARAM if the interface does not -exist, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a -LIBUSB_ERROR code on failure. +or +.Ft int +.Fn libusb_detach_kernel_driver_np "libusb_device_handle *devh" "int interface" +Detach a kernel driver from an interface. +This is needed to claim an interface required by a kernel driver. +Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver was active, +LIBUSB_ERROR_INVALID_PARAM if the interface does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on failure. This function is non-portable. . .Pp . @@ -271,7 +305,7 @@ failure. . .Pp .Ft int -.Fn libsub_get_active_config_descriptor "libusb_device *dev" "libusb_device_descriptor **config" +.Fn libsub_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, returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state and return another LIBUSB_ERROR code on error. @@ -337,7 +371,7 @@ LIBUSB_ERROR code on failure. . .Pp .Ft int -.Fn libusb_control_transfer "libusb_device_handle *devh" "uint8_t bmRequestType" "uint16_t wIndex" "unsigned char *data" "uint16_t wLength" "unsigned int timeout" +.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. Returns 0 on success, LIBUSB_ERROR_TIMEOUT if the transfer timeout, LIBUSB_ERROR_PIPE if the control request was not supported, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index b8e2322b7..e25bddceb 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -294,6 +294,7 @@ typedef struct libusb_transfer { /* Library initialisation */ void libusb_set_debug(libusb_context * ctx, int level); +const char *libusb_strerror(int code); int libusb_init(libusb_context ** context); void libusb_exit(struct libusb_context *ctx); @@ -317,6 +318,9 @@ int libusb_claim_interface(libusb_device_handle * devh, int interface_number); int libusb_release_interface(libusb_device_handle * devh, int interface_number); int libusb_reset_device(libusb_device_handle * devh); int libusb_kernel_driver_active(libusb_device_handle * devh, int interface); +int libusb_get_driver_np(libusb_device_handle * devh, int interface, char *name, int namelen); +int libusb_get_driver(libusb_device_handle * devh, int interface, char *name, int namelen); +int libusb_detach_kernel_driver_np(libusb_device_handle * devh, int interface); int libusb_detach_kernel_driver(libusb_device_handle * devh, int interface); int libusb_attach_kernel_driver(libusb_device_handle * devh, int interface); int libusb_set_interface_alt_setting(libusb_device_handle * devh, int interface_number, int alternate_setting); diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index a3f0087b8..0d5e2b4cd 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -70,12 +70,30 @@ libusb_set_debug(libusb_context *ctx, int level) ctx->debug = level; } +static void +libusb_set_nonblocking(int f) +{ + int flags; + + /* + * We ignore any failures in this function, hence the + * non-blocking flag is not critical to the operation of + * libUSB. We use F_GETFL and F_SETFL to be compatible with + * Linux. + */ + + flags = fcntl(f, F_GETFL, NULL); + if (flags == -1) + return; + flags |= O_NONBLOCK; + fcntl(f, F_SETFL, flags); +} + int libusb_init(libusb_context **context) { struct libusb_context *ctx; char *debug; - int flag; int ret; ctx = malloc(sizeof(*ctx)); @@ -106,12 +124,8 @@ libusb_init(libusb_context **context) return (LIBUSB_ERROR_OTHER); } /* set non-blocking mode on the control pipe to avoid deadlock */ - flag = 1; - ret = fcntl(ctx->ctrl_pipe[0], O_NONBLOCK, &flag); - assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[0]"); - flag = 1; - ret = fcntl(ctx->ctrl_pipe[1], O_NONBLOCK, &flag); - assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[1]"); + libusb_set_nonblocking(ctx->ctrl_pipe[0]); + libusb_set_nonblocking(ctx->ctrl_pipe[1]); libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN); @@ -356,7 +370,7 @@ libusb_open(libusb_device *dev, libusb_device_handle **devh) /* make sure our event loop detects the new device */ dummy = 0; err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); - if (err < sizeof(dummy)) { + if (err < (int)sizeof(dummy)) { /* ignore error, if any */ DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!"); } @@ -431,7 +445,7 @@ libusb_close(struct libusb20_device *pdev) /* make sure our event loop detects the closed device */ dummy = 0; err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); - if (err < sizeof(dummy)) { + if (err < (int)sizeof(dummy)) { /* ignore error, if any */ DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!"); } @@ -473,7 +487,6 @@ libusb_set_configuration(struct libusb20_device *pdev, int configuration) uint8_t i; dev = libusb_get_device(pdev); - if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); @@ -620,6 +633,8 @@ libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint) return (LIBUSB_ERROR_INVALID_PARAM); dev = libusb_get_device(pdev); + if (dev == NULL) + return (LIBUSB_ERROR_INVALID_PARAM); CTX_LOCK(dev->ctx); err = libusb20_tr_open(xfer, 0, 0, endpoint); @@ -647,7 +662,7 @@ libusb_reset_device(struct libusb20_device *pdev) dev = libusb_get_device(pdev); if (dev == NULL) - return (LIBUSB20_ERROR_INVALID_PARAM); + return (LIBUSB_ERROR_INVALID_PARAM); libusb10_cancel_all_transfer(dev); @@ -672,6 +687,45 @@ libusb_kernel_driver_active(struct libusb20_device *pdev, int interface) pdev, interface)); } +int +libusb_get_driver_np(struct libusb20_device *pdev, int interface, + char *name, int namelen) +{ + return (libusb_get_driver(pdev, interface, name, namelen)); +} + +int +libusb_get_driver(struct libusb20_device *pdev, int interface, + char *name, int namelen) +{ + char *ptr; + int err; + + if (pdev == NULL) + return (LIBUSB_ERROR_INVALID_PARAM); + if (namelen < 1) + return (LIBUSB_ERROR_INVALID_PARAM); + + err = libusb20_dev_get_iface_desc( + pdev, interface, name, namelen); + + if (err != 0) + return (LIBUSB_ERROR_OTHER); + + /* we only want the driver name */ + ptr = strstr(name, ":"); + if (ptr != NULL) + *ptr = 0; + + return (0); +} + +int +libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface) +{ + return (libusb_detach_kernel_driver(pdev, interface)); +} + int libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface) { @@ -683,7 +737,7 @@ libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface) err = libusb20_dev_detach_kernel_driver( pdev, interface); - return (err ? LIBUSB20_ERROR_OTHER : 0); + return (err ? LIBUSB_ERROR_OTHER : 0); } int @@ -1362,3 +1416,9 @@ libusb_le16_to_cpu(uint16_t x) return (le16toh(x)); } +const char * +libusb_strerror(int code) +{ + /* TODO */ + return ("Unknown error"); +} diff --git a/lib/libusb/libusb20.3 b/lib/libusb/libusb20.3 index 7896e8f01..a9e77b8c6 100644 --- a/lib/libusb/libusb20.3 +++ b/lib/libusb/libusb20.3 @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" -.Dd November 18, 2009 +.Dd October 14, 2010 .Dt LIBUSB20 3 .Os .Sh NAME @@ -177,7 +177,7 @@ USB access library (libusb -lusb) .Ft int .Fn libusb20_be_set_template "struct libusb20_backend *pbe" "int temp" .Ft int -.Fn libusb20_be_get_dev_quirk "struct libusb20_backend *pber", "uint16_t index" "struct libusb20_quirk *pq" +.Fn libusb20_be_get_dev_quirk "struct libusb20_backend *pber" "uint16_t index" "struct libusb20_quirk *pq" .Ft int .Fn libusb20_be_get_quirk_name "struct libusb20_backend *pbe" "uint16_t index" "struct libusb20_quirk *pq" .Ft int -- 2.45.0