Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Nov 2010 21:04:18 +0000 (UTC)
From:      Andrew Thompson <thompsa@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r215546 - stable/8/lib/libusb
Message-ID:  <201011192104.oAJL4ILw089443@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: thompsa
Date: Fri Nov 19 21:04:18 2010
New Revision: 215546
URL: http://svn.freebsd.org/changeset/base/215546

Log:
  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

Modified:
  stable/8/lib/libusb/libusb.3
  stable/8/lib/libusb/libusb.h
  stable/8/lib/libusb/libusb10.c
  stable/8/lib/libusb/libusb20.3
Directory Properties:
  stable/8/lib/libusb/   (props changed)
  stable/8/lib/libusb/usb.h   (props changed)

Modified: stable/8/lib/libusb/libusb.3
==============================================================================
--- stable/8/lib/libusb/libusb.3	Fri Nov 19 20:23:01 2010	(r215545)
+++ stable/8/lib/libusb/libusb.3	Fri Nov 19 21:04:18 2010	(r215546)
@@ -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 t
 .
 .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 
 .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 

Modified: stable/8/lib/libusb/libusb.h
==============================================================================
--- stable/8/lib/libusb/libusb.h	Fri Nov 19 20:23:01 2010	(r215545)
+++ stable/8/lib/libusb/libusb.h	Fri Nov 19 21:04:18 2010	(r215546)
@@ -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
 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);

Modified: stable/8/lib/libusb/libusb10.c
==============================================================================
--- stable/8/lib/libusb/libusb10.c	Fri Nov 19 20:23:01 2010	(r215545)
+++ stable/8/lib/libusb/libusb10.c	Fri Nov 19 21:04:18 2010	(r215546)
@@ -70,12 +70,30 @@ libusb_set_debug(libusb_context *ctx, in
 		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_d
 	/* 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 *pde
 	/* 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
 	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
 		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_devi
 
 	dev = libusb_get_device(pdev);
 	if (dev == NULL)
-		return (LIBUSB20_ERROR_INVALID_PARAM);
+		return (LIBUSB_ERROR_INVALID_PARAM);
 
 	libusb10_cancel_all_transfer(dev);
 
@@ -673,6 +688,45 @@ libusb_kernel_driver_active(struct libus
 }
 
 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)
 {
 	int err;
@@ -683,7 +737,7 @@ libusb_detach_kernel_driver(struct libus
 	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");
+}

Modified: stable/8/lib/libusb/libusb20.3
==============================================================================
--- stable/8/lib/libusb/libusb20.3	Fri Nov 19 20:23:01 2010	(r215545)
+++ stable/8/lib/libusb/libusb20.3	Fri Nov 19 21:04:18 2010	(r215546)
@@ -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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201011192104.oAJL4ILw089443>