Blame usbredirhost/usbredirhost.h

Packit 9795e1
/* usbredirhost.c usb network redirection usb host code header
Packit 9795e1
Packit 9795e1
   Copyright 2010-2012 Red Hat, Inc.
Packit 9795e1
Packit 9795e1
   Red Hat Authors:
Packit 9795e1
   Hans de Goede <hdegoede@redhat.com>
Packit 9795e1
Packit 9795e1
   This library is free software; you can redistribute it and/or
Packit 9795e1
   modify it under the terms of the GNU Lesser General Public
Packit 9795e1
   License as published by the Free Software Foundation; either
Packit 9795e1
   version 2.1 of the License, or (at your option) any later version.
Packit 9795e1
Packit 9795e1
   This library is distributed in the hope that it will be useful,
Packit 9795e1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9795e1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 9795e1
   Lesser General Public License for more details.
Packit 9795e1
Packit 9795e1
   You should have received a copy of the GNU Lesser General Public
Packit 9795e1
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit 9795e1
*/
Packit 9795e1
#ifndef __USBREDIRHOST_H
Packit 9795e1
#define __USBREDIRHOST_H
Packit 9795e1
Packit 9795e1
#include <libusb.h>
Packit 9795e1
#include "usbredirparser.h"
Packit 9795e1
#include "usbredirfilter.h"
Packit 9795e1
Packit 9795e1
#ifdef __cplusplus
Packit 9795e1
extern "C" {
Packit 9795e1
#endif
Packit 9795e1
Packit 9795e1
struct usbredirhost;
Packit 9795e1
Packit 9795e1
typedef void (*usbredirhost_flush_writes)(void *priv);
Packit 9795e1
Packit 9795e1
typedef uint64_t (*usbredirhost_buffered_output_size)(void *priv);
Packit 9795e1
Packit 9795e1
/* This function creates an usbredirhost instance, including its embedded
Packit 9795e1
   libusbredirparser instance and sends the initial usb_redir_hello packet to
Packit 9795e1
   the usb-guest.
Packit 9795e1
Packit 9795e1
   If usb_dev_handle is not NULL, usbredirhost_set_device will be called
Packit 9795e1
   with the passed in usb_dev_handle after creating the instance. If
Packit 9795e1
   usbredirhost_set_device fails, the instance will be destroyed and NULL
Packit 9795e1
   will be returned.
Packit 9795e1
Packit 9795e1
   log_func is called by the usbredirhost to log various messages
Packit 9795e1
Packit 9795e1
   read_guest_data_func / write_guest_data_func are called by the
Packit 9795e1
   usbredirhost to read/write data from/to the usb-guest.
Packit 9795e1
Packit 9795e1
   This function returns a pointer to the created usbredirhost object on
Packit 9795e1
   success, or NULL on failure.
Packit 9795e1
   Note:
Packit 9795e1
   1) Both the usbredirtransport_log and the usbredirtransport_write
Packit 9795e1
      callbacks may get called before this function completes.
Packit 9795e1
   2) It is the responsibility of the code instantiating the usbredirhost
Packit 9795e1
      to make sure that libusb_handle_events gets called (using the
Packit 9795e1
      libusb_context from the passed in libusb_device_handle) when there are
Packit 9795e1
      events waiting on the filedescriptors libusb_get_pollfds returns
Packit 9795e1
   3) usbredirhost is partially multi-thread safe, see README.multi-thread
Packit 9795e1
*/
Packit 9795e1
Packit 9795e1
enum {
Packit 9795e1
    usbredirhost_fl_write_cb_owns_buffer = 0x01, /* See usbredirparser.h */
Packit 9795e1
};
Packit 9795e1
Packit 9795e1
struct usbredirhost *usbredirhost_open(
Packit 9795e1
    libusb_context *usb_ctx,
Packit 9795e1
    libusb_device_handle *usb_dev_handle,
Packit 9795e1
    usbredirparser_log log_func,
Packit 9795e1
    usbredirparser_read  read_guest_data_func,
Packit 9795e1
    usbredirparser_write write_guest_data_func,
Packit 9795e1
    void *func_priv, const char *version, int verbose, int flags);
Packit 9795e1
Packit 9795e1
/* See README.multi-thread */
Packit 9795e1
struct usbredirhost *usbredirhost_open_full(
Packit 9795e1
    libusb_context *usb_ctx,
Packit 9795e1
    libusb_device_handle *usb_dev_handle,
Packit 9795e1
    usbredirparser_log log_func,
Packit 9795e1
    usbredirparser_read  read_guest_data_func,
Packit 9795e1
    usbredirparser_write write_guest_data_func,
Packit 9795e1
    usbredirhost_flush_writes flush_writes_func,
Packit 9795e1
    usbredirparser_alloc_lock alloc_lock_func,
Packit 9795e1
    usbredirparser_lock lock_func,
Packit 9795e1
    usbredirparser_unlock unlock_func,
Packit 9795e1
    usbredirparser_free_lock free_lock_func,
Packit 9795e1
    void *func_priv, const char *version, int verbose, int flags);
Packit 9795e1
Packit 9795e1
/* Closes (destroys) the usbredirhost, if the usbredirhost currently
Packit 9795e1
   is redirecting a device this function will first call
Packit 9795e1
   usbredirhost_set_device(host, NULL); See the notes for that function!
Packit 9795e1
*/
Packit 9795e1
void usbredirhost_close(struct usbredirhost *host);
Packit 9795e1
Packit 9795e1
/* Call this function with a valid libusb_device_handle to send the initial
Packit 9795e1
   device info (interface_info, ep_info and device_connect packets) and make
Packit 9795e1
   the device available to the usbredir-guest connected to the usbredir-host.
Packit 9795e1
Packit 9795e1
   Note:
Packit 9795e1
   1) This function *takes ownership of* the passed in libusb_device_handle.
Packit 9795e1
   2) The passed in libusb_device_handle is closed on failure.
Packit 9795e1
   3) If the host already has a device that will get disconnected and closed.
Packit 9795e1
Packit 9795e1
   Call this function with NULL as usb_dev_handle to disconnect a redirected
Packit 9795e1
   device from the usbredir-guest and make the device available to the OS
Packit 9795e1
   on which the usbredir-host is running again.
Packit 9795e1
Packit 9795e1
   Note when disconnecting a redirected device, this function calls
Packit 9795e1
   libusb_handle_events to "reap" cancelled urbs before closing the libusb
Packit 9795e1
   device handle. This means that if you are using the same libusb context
Packit 9795e1
   for other purposes your transfer complete callbacks may get called!
Packit 9795e1
Packit 9795e1
   This function returns a usbredirproto.h status code (ie usb_redir_success)
Packit 9795e1
*/
Packit 9795e1
int usbredirhost_set_device(struct usbredirhost *host,
Packit 9795e1
                            libusb_device_handle *usb_dev_handle);
Packit 9795e1
Packit 9795e1
/* Call this function to set a callback in usbredirhost.
Packit 9795e1
   The usbredirhost_buffered_output_size callback should return the
Packit 9795e1
   application's pending writes buffer size (in bytes).
Packit 9795e1
Packit 9795e1
   usbredirhost will set two levels of threshold based in the information
Packit 9795e1
   provided by the usb device. In case the application's buffer is increasing
Packit 9795e1
   too much then usbredirhost uses the threshold limits to drop isochronous
Packit 9795e1
   packages but still send full frames whenever is possible.
Packit 9795e1
*/
Packit 9795e1
void usbredirhost_set_buffered_output_size_cb(struct usbredirhost *host,
Packit 9795e1
    usbredirhost_buffered_output_size buffered_output_size_func);
Packit 9795e1
Packit 9795e1
/* Call this whenever there is data ready for the usbredirhost to read from
Packit 9795e1
   the usb-guest
Packit 9795e1
   returns 0 on success, or an error code from the below enum on error.
Packit 9795e1
   On an usbredirhost_read_io_error this function will continue where it
Packit 9795e1
   left of the last time on the next call. On an usbredirhost_read_parse_error
Packit 9795e1
   it will skip to the next packet (*). On an usbredirhost_read_device_rejected
Packit 9795e1
   error, you are expected to call usbredirhost_set_device(host, NULL).
Packit 9795e1
   An usbredirhost_read_device_lost error means that the host has done the
Packit 9795e1
   equivalent of usbredirhost_set_device(host, NULL) itself because the
Packit 9795e1
   connection to the device was lost.
Packit 9795e1
   *) As determined by the faulty's package headers length field */
Packit 9795e1
enum {
Packit 9795e1
    usbredirhost_read_io_error        = -1,
Packit 9795e1
    usbredirhost_read_parse_error     = -2,
Packit 9795e1
    usbredirhost_read_device_rejected = -3,
Packit 9795e1
    usbredirhost_read_device_lost     = -4,
Packit 9795e1
};
Packit 9795e1
int usbredirhost_read_guest_data(struct usbredirhost *host);
Packit 9795e1
Packit 9795e1
/* This returns the number of usbredir packets queued up for writing */
Packit 9795e1
int usbredirhost_has_data_to_write(struct usbredirhost *host);
Packit 9795e1
Packit 9795e1
/* Call this when usbredirhost_has_data_to_write returns > 0
Packit 9795e1
   returns 0 on success, -1 if a write error happened.
Packit 9795e1
   If a write error happened, this function will retry writing any queued data
Packit 9795e1
   on the next call, and will continue doing so until it has succeeded! */
Packit 9795e1
enum {
Packit 9795e1
    usbredirhost_write_io_error       = -1,
Packit 9795e1
};
Packit 9795e1
int usbredirhost_write_guest_data(struct usbredirhost *host);
Packit 9795e1
Packit 9795e1
/* When passing the usbredirhost_fl_write_cb_owns_buffer flag to
Packit 9795e1
   usbredirhost_open, this function must be called to free the data buffer
Packit 9795e1
   passed to write_guest_data_func when done with this buffer. */
Packit 9795e1
void usbredirhost_free_write_buffer(struct usbredirhost *host, uint8_t *data);
Packit 9795e1
Packit 9795e1
/* Get the *usbredir-guest's* filter, if any. If there is no filter,
Packit 9795e1
   rules is set to NULL and rules_count to 0. */
Packit 9795e1
void usbredirhost_get_guest_filter(struct usbredirhost *host,
Packit 9795e1
    const struct usbredirfilter_rule **rules_ret, int *rules_count_ret);
Packit 9795e1
Packit 9795e1
/* Get device and config descriptors from the USB device dev, and call
Packit 9795e1
   usbredirfilter_check with the passed in filter rules and the needed info
Packit 9795e1
   from the descriptors, flags gets passed to usbredirfilter_check unmodified.
Packit 9795e1
   See the documentation of usbredirfilter_check for more details.
Packit 9795e1
Packit 9795e1
   Return value: -EIO or -ENOMEM when getting the descriptors fails, otherwise
Packit 9795e1
       it returns the return value of the usbredirfilter_check call. */
Packit 9795e1
int usbredirhost_check_device_filter(const struct usbredirfilter_rule *rules,
Packit 9795e1
    int rules_count, libusb_device *dev, int flags);
Packit 9795e1
Packit 9795e1
#ifdef __cplusplus
Packit 9795e1
}
Packit 9795e1
#endif
Packit 9795e1
Packit 9795e1
#endif