Blame libusb/sync.c

Packit Service b0a153
/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
Packit Service b0a153
/*
Packit Service b0a153
 * Synchronous I/O functions for libusb
Packit Service b0a153
 * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
Packit Service b0a153
 * Copyright © 2019 Nathan Hjelm <hjelmn@cs.unm.edu>
Packit Service b0a153
 * Copyright © 2019 Google LLC. All rights reserved.
Packit Service b0a153
 *
Packit Service b0a153
 * This library is free software; you can redistribute it and/or
Packit Service b0a153
 * modify it under the terms of the GNU Lesser General Public
Packit Service b0a153
 * License as published by the Free Software Foundation; either
Packit Service b0a153
 * version 2.1 of the License, or (at your option) any later version.
Packit Service b0a153
 *
Packit Service b0a153
 * This library is distributed in the hope that it will be useful,
Packit Service b0a153
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b0a153
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service b0a153
 * Lesser General Public License for more details.
Packit Service b0a153
 *
Packit Service b0a153
 * You should have received a copy of the GNU Lesser General Public
Packit Service b0a153
 * License along with this library; if not, write to the Free Software
Packit Service b0a153
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit Service b0a153
 */
Packit Service b0a153
Packit Service b0a153
#include <config.h>
Packit Service b0a153
Packit Service b0a153
#include <errno.h>
Packit Service b0a153
#include <stdint.h>
Packit Service b0a153
#include <stdlib.h>
Packit Service b0a153
#include <string.h>
Packit Service b0a153
Packit Service b0a153
#include "libusbi.h"
Packit Service b0a153
Packit Service b0a153
/**
Packit Service b0a153
 * @defgroup libusb_syncio Synchronous device I/O
Packit Service b0a153
 *
Packit Service b0a153
 * This page documents libusb's synchronous (blocking) API for USB device I/O.
Packit Service b0a153
 * This interface is easy to use but has some limitations. More advanced users
Packit Service b0a153
 * may wish to consider using the \ref libusb_asyncio "asynchronous I/O API" instead.
Packit Service b0a153
 */
Packit Service b0a153
Packit Service b0a153
static void LIBUSB_CALL sync_transfer_cb(struct libusb_transfer *transfer)
Packit Service b0a153
{
Packit Service b0a153
	int *completed = transfer->user_data;
Packit Service b0a153
	*completed = 1;
Packit Service b0a153
	usbi_dbg("actual_length=%d", transfer->actual_length);
Packit Service b0a153
	/* caller interprets result and frees transfer */
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static void sync_transfer_wait_for_completion(struct libusb_transfer *transfer)
Packit Service b0a153
{
Packit Service b0a153
	int r, *completed = transfer->user_data;
Packit Service b0a153
	struct libusb_context *ctx = HANDLE_CTX(transfer->dev_handle);
Packit Service b0a153
Packit Service b0a153
	while (!*completed) {
Packit Service b0a153
		r = libusb_handle_events_completed(ctx, completed);
Packit Service b0a153
		if (r < 0) {
Packit Service b0a153
			if (r == LIBUSB_ERROR_INTERRUPTED)
Packit Service b0a153
				continue;
Packit Service b0a153
			usbi_err(ctx, "libusb_handle_events failed: %s, cancelling transfer and retrying",
Packit Service b0a153
				 libusb_error_name(r));
Packit Service b0a153
			libusb_cancel_transfer(transfer);
Packit Service b0a153
			continue;
Packit Service b0a153
		}
Packit Service b0a153
		if (NULL == transfer->dev_handle) {
Packit Service b0a153
			/* transfer completion after libusb_close() */
mrstock 2cce27
			transfer->status = LIBUSB_TRANSFER_NO_DEVICE;
Packit Service b0a153
			*completed = 1;
Packit Service b0a153
		}
Packit Service b0a153
	}
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_syncio
Packit Service b0a153
 * Perform a USB control transfer.
Packit Service b0a153
 *
Packit Service b0a153
 * The direction of the transfer is inferred from the bmRequestType field of
Packit Service b0a153
 * the setup packet.
Packit Service b0a153
 *
Packit Service b0a153
 * The wValue, wIndex and wLength fields values should be given in host-endian
Packit Service b0a153
 * byte order.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev_handle a handle for the device to communicate with
Packit Service b0a153
 * \param bmRequestType the request type field for the setup packet
Packit Service b0a153
 * \param bRequest the request field for the setup packet
Packit Service b0a153
 * \param wValue the value field for the setup packet
Packit Service b0a153
 * \param wIndex the index field for the setup packet
Packit Service b0a153
 * \param data a suitably-sized data buffer for either input or output
Packit Service b0a153
 * (depending on direction bits within bmRequestType)
Packit Service b0a153
 * \param wLength the length field for the setup packet. The data buffer should
Packit Service b0a153
 * be at least this size.
Packit Service b0a153
 * \param timeout timeout (in millseconds) that this function should wait
Packit Service b0a153
 * before giving up due to no response being received. For an unlimited
Packit Service b0a153
 * timeout, use value 0.
Packit Service b0a153
 * \returns on success, the number of bytes actually transferred
Packit Service b0a153
 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
Packit Service b0a153
 * \returns LIBUSB_ERROR_PIPE if the control request was not supported by the
Packit Service b0a153
 * device
Packit Service b0a153
 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
Packit Service b0a153
 * \returns LIBUSB_ERROR_BUSY if called from event handling context
Packit Service b0a153
 * \returns LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than
Packit Service b0a153
 * the operating system and/or hardware can support
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on other failures
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_control_transfer(libusb_device_handle *dev_handle,
Packit Service b0a153
	uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
Packit Service b0a153
	unsigned char *data, uint16_t wLength, unsigned int timeout)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_transfer *transfer;
Packit Service b0a153
	unsigned char *buffer;
Packit Service b0a153
	int completed = 0;
Packit Service b0a153
	int r;
Packit Service b0a153
Packit Service b0a153
	if (usbi_handling_events(HANDLE_CTX(dev_handle)))
Packit Service b0a153
		return LIBUSB_ERROR_BUSY;
Packit Service b0a153
Packit Service b0a153
	transfer = libusb_alloc_transfer(0);
Packit Service b0a153
	if (!transfer)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	buffer = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
Packit Service b0a153
	if (!buffer) {
Packit Service b0a153
		libusb_free_transfer(transfer);
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
Packit Service b0a153
		wLength);
Packit Service b0a153
	if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
Packit Service b0a153
		memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
Packit Service b0a153
Packit Service b0a153
	libusb_fill_control_transfer(transfer, dev_handle, buffer,
Packit Service b0a153
		sync_transfer_cb, &completed, timeout);
Packit Service b0a153
	transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER;
Packit Service b0a153
	r = libusb_submit_transfer(transfer);
Packit Service b0a153
	if (r < 0) {
Packit Service b0a153
		libusb_free_transfer(transfer);
Packit Service b0a153
		return r;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	sync_transfer_wait_for_completion(transfer);
Packit Service b0a153
Packit Service b0a153
	if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
Packit Service b0a153
		memcpy(data, libusb_control_transfer_get_data(transfer),
Packit Service b0a153
			transfer->actual_length);
Packit Service b0a153
Packit Service b0a153
	switch (transfer->status) {
Packit Service b0a153
	case LIBUSB_TRANSFER_COMPLETED:
Packit Service b0a153
		r = transfer->actual_length;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_TIMED_OUT:
Packit Service b0a153
		r = LIBUSB_ERROR_TIMEOUT;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_STALL:
Packit Service b0a153
		r = LIBUSB_ERROR_PIPE;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_NO_DEVICE:
Packit Service b0a153
		r = LIBUSB_ERROR_NO_DEVICE;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_OVERFLOW:
Packit Service b0a153
		r = LIBUSB_ERROR_OVERFLOW;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_ERROR:
Packit Service b0a153
	case LIBUSB_TRANSFER_CANCELLED:
Packit Service b0a153
		r = LIBUSB_ERROR_IO;
Packit Service b0a153
		break;
Packit Service b0a153
	default:
Packit Service b0a153
		usbi_warn(HANDLE_CTX(dev_handle),
Packit Service b0a153
			"unrecognised status code %d", transfer->status);
Packit Service b0a153
		r = LIBUSB_ERROR_OTHER;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	libusb_free_transfer(transfer);
Packit Service b0a153
	return r;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
Packit Service b0a153
	unsigned char endpoint, unsigned char *buffer, int length,
Packit Service b0a153
	int *transferred, unsigned int timeout, unsigned char type)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_transfer *transfer;
Packit Service b0a153
	int completed = 0;
Packit Service b0a153
	int r;
Packit Service b0a153
Packit Service b0a153
	if (usbi_handling_events(HANDLE_CTX(dev_handle)))
Packit Service b0a153
		return LIBUSB_ERROR_BUSY;
Packit Service b0a153
Packit Service b0a153
	transfer = libusb_alloc_transfer(0);
Packit Service b0a153
	if (!transfer)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
Packit Service b0a153
		sync_transfer_cb, &completed, timeout);
Packit Service b0a153
	transfer->type = type;
Packit Service b0a153
Packit Service b0a153
	r = libusb_submit_transfer(transfer);
Packit Service b0a153
	if (r < 0) {
Packit Service b0a153
		libusb_free_transfer(transfer);
Packit Service b0a153
		return r;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	sync_transfer_wait_for_completion(transfer);
Packit Service b0a153
Packit Service b0a153
	if (transferred)
Packit Service b0a153
		*transferred = transfer->actual_length;
Packit Service b0a153
Packit Service b0a153
	switch (transfer->status) {
Packit Service b0a153
	case LIBUSB_TRANSFER_COMPLETED:
Packit Service b0a153
		r = 0;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_TIMED_OUT:
Packit Service b0a153
		r = LIBUSB_ERROR_TIMEOUT;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_STALL:
Packit Service b0a153
		r = LIBUSB_ERROR_PIPE;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_OVERFLOW:
Packit Service b0a153
		r = LIBUSB_ERROR_OVERFLOW;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_NO_DEVICE:
Packit Service b0a153
		r = LIBUSB_ERROR_NO_DEVICE;
Packit Service b0a153
		break;
Packit Service b0a153
	case LIBUSB_TRANSFER_ERROR:
Packit Service b0a153
	case LIBUSB_TRANSFER_CANCELLED:
Packit Service b0a153
		r = LIBUSB_ERROR_IO;
Packit Service b0a153
		break;
Packit Service b0a153
	default:
Packit Service b0a153
		usbi_warn(HANDLE_CTX(dev_handle),
Packit Service b0a153
			"unrecognised status code %d", transfer->status);
Packit Service b0a153
		r = LIBUSB_ERROR_OTHER;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	libusb_free_transfer(transfer);
Packit Service b0a153
	return r;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_syncio
Packit Service b0a153
 * Perform a USB bulk transfer. The direction of the transfer is inferred from
Packit Service b0a153
 * the direction bits of the endpoint address.
Packit Service b0a153
 *
Packit Service b0a153
 * For bulk reads, the <tt>length</tt> field indicates the maximum length of
Packit Service b0a153
 * data you are expecting to receive. If less data arrives than expected,
Packit Service b0a153
 * this function will return that data, so be sure to check the
Packit Service b0a153
 * <tt>transferred</tt> output parameter.
Packit Service b0a153
 *
Packit Service b0a153
 * You should also check the <tt>transferred</tt> parameter for bulk writes.
Packit Service b0a153
 * Not all of the data may have been written.
Packit Service b0a153
 *
Packit Service b0a153
 * Also check <tt>transferred</tt> when dealing with a timeout error code.
Packit Service b0a153
 * libusb may have to split your transfer into a number of chunks to satisfy
Packit Service b0a153
 * underlying O/S requirements, meaning that the timeout may expire after
Packit Service b0a153
 * the first few chunks have completed. libusb is careful not to lose any data
Packit Service b0a153
 * that may have been transferred; do not assume that timeout conditions
Packit Service b0a153
 * indicate a complete lack of I/O.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev_handle a handle for the device to communicate with
Packit Service b0a153
 * \param endpoint the address of a valid endpoint to communicate with
Packit Service b0a153
 * \param data a suitably-sized data buffer for either input or output
Packit Service b0a153
 * (depending on endpoint)
Packit Service b0a153
 * \param length for bulk writes, the number of bytes from data to be sent. for
Packit Service b0a153
 * bulk reads, the maximum number of bytes to receive into the data buffer.
Packit Service b0a153
 * \param transferred output location for the number of bytes actually
Packit Service b0a153
 * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
Packit Service b0a153
 * it is legal to pass a NULL pointer if you do not wish to receive this
Packit Service b0a153
 * information.
Packit Service b0a153
 * \param timeout timeout (in millseconds) that this function should wait
Packit Service b0a153
 * before giving up due to no response being received. For an unlimited
Packit Service b0a153
 * timeout, use value 0.
Packit Service b0a153
 *
Packit Service b0a153
 * \returns 0 on success (and populates <tt>transferred</tt>)
Packit Service b0a153
 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
Packit Service b0a153
 * <tt>transferred</tt>)
Packit Service b0a153
 * \returns LIBUSB_ERROR_PIPE if the endpoint halted
Packit Service b0a153
 * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
Packit Service b0a153
 * \ref libusb_packetoverflow
Packit Service b0a153
 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
Packit Service b0a153
 * \returns LIBUSB_ERROR_BUSY if called from event handling context
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on other failures
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
Packit Service b0a153
	unsigned char endpoint, unsigned char *data, int length, int *transferred,
Packit Service b0a153
	unsigned int timeout)
Packit Service b0a153
{
Packit Service b0a153
	return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
Packit Service b0a153
		transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_syncio
Packit Service b0a153
 * Perform a USB interrupt transfer. The direction of the transfer is inferred
Packit Service b0a153
 * from the direction bits of the endpoint address.
Packit Service b0a153
 *
Packit Service b0a153
 * For interrupt reads, the <tt>length</tt> field indicates the maximum length
Packit Service b0a153
 * of data you are expecting to receive. If less data arrives than expected,
Packit Service b0a153
 * this function will return that data, so be sure to check the
Packit Service b0a153
 * <tt>transferred</tt> output parameter.
Packit Service b0a153
 *
Packit Service b0a153
 * You should also check the <tt>transferred</tt> parameter for interrupt
Packit Service b0a153
 * writes. Not all of the data may have been written.
Packit Service b0a153
 *
Packit Service b0a153
 * Also check <tt>transferred</tt> when dealing with a timeout error code.
Packit Service b0a153
 * libusb may have to split your transfer into a number of chunks to satisfy
Packit Service b0a153
 * underlying O/S requirements, meaning that the timeout may expire after
Packit Service b0a153
 * the first few chunks have completed. libusb is careful not to lose any data
Packit Service b0a153
 * that may have been transferred; do not assume that timeout conditions
Packit Service b0a153
 * indicate a complete lack of I/O.
Packit Service b0a153
 *
Packit Service b0a153
 * The default endpoint bInterval value is used as the polling interval.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev_handle a handle for the device to communicate with
Packit Service b0a153
 * \param endpoint the address of a valid endpoint to communicate with
Packit Service b0a153
 * \param data a suitably-sized data buffer for either input or output
Packit Service b0a153
 * (depending on endpoint)
Packit Service b0a153
 * \param length for bulk writes, the number of bytes from data to be sent. for
Packit Service b0a153
 * bulk reads, the maximum number of bytes to receive into the data buffer.
Packit Service b0a153
 * \param transferred output location for the number of bytes actually
Packit Service b0a153
 * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
Packit Service b0a153
 * it is legal to pass a NULL pointer if you do not wish to receive this
Packit Service b0a153
 * information.
Packit Service b0a153
 * \param timeout timeout (in millseconds) that this function should wait
Packit Service b0a153
 * before giving up due to no response being received. For an unlimited
Packit Service b0a153
 * timeout, use value 0.
Packit Service b0a153
 *
Packit Service b0a153
 * \returns 0 on success (and populates <tt>transferred</tt>)
Packit Service b0a153
 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
Packit Service b0a153
 * \returns LIBUSB_ERROR_PIPE if the endpoint halted
Packit Service b0a153
 * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
Packit Service b0a153
 * \ref libusb_packetoverflow
Packit Service b0a153
 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
Packit Service b0a153
 * \returns LIBUSB_ERROR_BUSY if called from event handling context
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on other error
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_interrupt_transfer(
Packit Service b0a153
	struct libusb_device_handle *dev_handle, unsigned char endpoint,
Packit Service b0a153
	unsigned char *data, int length, int *transferred, unsigned int timeout)
Packit Service b0a153
{
Packit Service b0a153
	return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
Packit Service b0a153
		transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
Packit Service b0a153
}