- 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
Approved by:    thompsa (mentor)
This commit is contained in:
Hans Petter Selasky 2010-10-14 20:50:33 +00:00
parent 51fd3d75fe
commit 698e791af5
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=213853
4 changed files with 120 additions and 22 deletions

View File

@ -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
@ -247,12 +256,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
.
@ -279,7 +313,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.
@ -345,7 +379,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

View File

@ -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);
@ -318,6 +319,9 @@ int libusb_release_interface(libusb_device_handle * devh, int interface_number);
int libusb_reset_device(libusb_device_handle * devh);
int libusb_check_connected(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);

View File

@ -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);
@ -687,6 +702,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)
{
@ -698,7 +752,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
@ -1377,3 +1431,9 @@ libusb_le16_to_cpu(uint16_t x)
return (le16toh(x));
}
const char *
libusb_strerror(int code)
{
/* TODO */
return ("Unknown error");
}

View File

@ -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