| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libusbvirt
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Virtual USB device.
|
|---|
| 34 | */
|
|---|
| 35 | #ifndef LIBUSBVIRT_DEVICE_H_
|
|---|
| 36 | #define LIBUSBVIRT_DEVICE_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <usb/usb.h>
|
|---|
| 39 | #include <usb/request.h>
|
|---|
| 40 |
|
|---|
| 41 | #define USBVIRT_ENDPOINT_MAX 16
|
|---|
| 42 |
|
|---|
| 43 | typedef struct usbvirt_device usbvirt_device_t;
|
|---|
| 44 |
|
|---|
| 45 | typedef int (*usbvirt_on_data_to_device_t)(usbvirt_device_t *, usb_endpoint_t,
|
|---|
| 46 | usb_transfer_type_t, void *, size_t);
|
|---|
| 47 | typedef int (*usbvirt_on_data_from_device_t)(usbvirt_device_t *, usb_endpoint_t,
|
|---|
| 48 | usb_transfer_type_t, void *, size_t, size_t *);
|
|---|
| 49 | typedef int (*usbvirt_on_control_t)(usbvirt_device_t *,
|
|---|
| 50 | const usb_device_request_setup_packet_t *, uint8_t *, size_t *);
|
|---|
| 51 |
|
|---|
| 52 | typedef struct {
|
|---|
| 53 | usb_direction_t req_direction;
|
|---|
| 54 | usb_request_recipient_t req_recipient;
|
|---|
| 55 | usb_request_type_t req_type;
|
|---|
| 56 | uint8_t request;
|
|---|
| 57 | const char *name;
|
|---|
| 58 | usbvirt_on_control_t callback;
|
|---|
| 59 | } usbvirt_control_request_handler_t;
|
|---|
| 60 |
|
|---|
| 61 | /** Extra configuration data for GET_CONFIGURATION request. */
|
|---|
| 62 | typedef struct {
|
|---|
| 63 | /** Actual data. */
|
|---|
| 64 | uint8_t *data;
|
|---|
| 65 | /** Data length. */
|
|---|
| 66 | size_t length;
|
|---|
| 67 | } usbvirt_device_configuration_extras_t;
|
|---|
| 68 |
|
|---|
| 69 | /** Single device configuration. */
|
|---|
| 70 | typedef struct {
|
|---|
| 71 | /** Standard configuration descriptor. */
|
|---|
| 72 | usb_standard_configuration_descriptor_t *descriptor;
|
|---|
| 73 | /** Array of extra data. */
|
|---|
| 74 | usbvirt_device_configuration_extras_t *extra;
|
|---|
| 75 | /** Length of @c extra array. */
|
|---|
| 76 | size_t extra_count;
|
|---|
| 77 | } usbvirt_device_configuration_t;
|
|---|
| 78 |
|
|---|
| 79 | /** Standard USB descriptors. */
|
|---|
| 80 | typedef struct {
|
|---|
| 81 | /** Standard device descriptor.
|
|---|
| 82 | * There is always only one such descriptor for the device.
|
|---|
| 83 | */
|
|---|
| 84 | usb_standard_device_descriptor_t *device;
|
|---|
| 85 |
|
|---|
| 86 | /** Configurations. */
|
|---|
| 87 | usbvirt_device_configuration_t *configuration;
|
|---|
| 88 | /** Number of configurations. */
|
|---|
| 89 | size_t configuration_count;
|
|---|
| 90 | } usbvirt_descriptors_t;
|
|---|
| 91 |
|
|---|
| 92 | /** Possible states of virtual USB device.
|
|---|
| 93 | * Notice that these are not 1:1 mappings to those in USB specification.
|
|---|
| 94 | */
|
|---|
| 95 | typedef enum {
|
|---|
| 96 | /** Default state, device listens at default address. */
|
|---|
| 97 | USBVIRT_STATE_DEFAULT,
|
|---|
| 98 | /** Device has non-default address assigned. */
|
|---|
| 99 | USBVIRT_STATE_ADDRESS,
|
|---|
| 100 | /** Device is configured. */
|
|---|
| 101 | USBVIRT_STATE_CONFIGURED
|
|---|
| 102 | } usbvirt_device_state_t;
|
|---|
| 103 |
|
|---|
| 104 | typedef struct {
|
|---|
| 105 | usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
|
|---|
| 106 | usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
|
|---|
| 107 | usbvirt_control_request_handler_t *control;
|
|---|
| 108 | void (*state_changed)(usbvirt_device_t *dev,
|
|---|
| 109 | usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
|
|---|
| 110 | } usbvirt_device_ops_t;
|
|---|
| 111 |
|
|---|
| 112 | struct usbvirt_device {
|
|---|
| 113 | const char *name;
|
|---|
| 114 | void *device_data;
|
|---|
| 115 | usbvirt_device_ops_t *ops;
|
|---|
| 116 | usbvirt_descriptors_t *descriptors;
|
|---|
| 117 | usb_address_t address;
|
|---|
| 118 | usbvirt_device_state_t state;
|
|---|
| 119 | };
|
|---|
| 120 |
|
|---|
| 121 | int usbvirt_device_plug(usbvirt_device_t *, const char *);
|
|---|
| 122 |
|
|---|
| 123 | void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *,
|
|---|
| 124 | uint8_t *, size_t *, void *, size_t);
|
|---|
| 125 |
|
|---|
| 126 | int usbvirt_control_write(usbvirt_device_t *, void *, size_t, void *, size_t);
|
|---|
| 127 | int usbvirt_control_read(usbvirt_device_t *, void *, size_t, void *, size_t, size_t *);
|
|---|
| 128 | int usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
|
|---|
| 129 | void *, size_t);
|
|---|
| 130 | int usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
|
|---|
| 131 | void *, size_t, size_t *);
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 | #endif
|
|---|
| 135 | /**
|
|---|
| 136 | * @}
|
|---|
| 137 | */
|
|---|