| 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 | *
|
|---|
| 34 | */
|
|---|
| 35 | #include <usbvirt/device.h>
|
|---|
| 36 | #include <usb/debug.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 | #include "private.h"
|
|---|
| 40 |
|
|---|
| 41 | static int usbvirt_control_transfer(usbvirt_device_t *dev,
|
|---|
| 42 | void *setup, size_t setup_size,
|
|---|
| 43 | void *data, size_t data_size, size_t *data_size_sent)
|
|---|
| 44 | {
|
|---|
| 45 | assert(dev);
|
|---|
| 46 | assert(dev->ops);
|
|---|
| 47 |
|
|---|
| 48 | if (setup_size != sizeof(usb_device_request_setup_packet_t)) {
|
|---|
| 49 | return ESTALL;
|
|---|
| 50 | }
|
|---|
| 51 | usb_device_request_setup_packet_t *setup_packet = setup;
|
|---|
| 52 | if (data_size != setup_packet->length) {
|
|---|
| 53 | return ESTALL;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | int rc;
|
|---|
| 57 |
|
|---|
| 58 | /* Run user handler first. */
|
|---|
| 59 | rc = process_control_transfer(dev, dev->ops->control,
|
|---|
| 60 | setup_packet, data, data_size_sent);
|
|---|
| 61 | if (rc != EFORWARD) {
|
|---|
| 62 | return rc;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /* Run the library handlers afterwards. */
|
|---|
| 66 | rc = process_control_transfer(dev, library_handlers,
|
|---|
| 67 | setup_packet, data, data_size_sent);
|
|---|
| 68 |
|
|---|
| 69 | if (rc == EFORWARD) {
|
|---|
| 70 | usb_log_warning("Control transfer {%s (%s)} not handled.\n",
|
|---|
| 71 | usb_debug_str_buffer(setup, setup_size, 10),
|
|---|
| 72 | setup_packet->request_type & 0x80
|
|---|
| 73 | ? "IN" : usb_debug_str_buffer(data, data_size, 10));
|
|---|
| 74 | rc = EBADCHECKSUM;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | return rc;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | int usbvirt_control_write(usbvirt_device_t *dev, void *setup, size_t setup_size,
|
|---|
| 81 | void *data, size_t data_size)
|
|---|
| 82 | {
|
|---|
| 83 | return usbvirt_control_transfer(dev, setup, setup_size,
|
|---|
| 84 | data, data_size, NULL);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | int usbvirt_control_read(usbvirt_device_t *dev, void *setup, size_t setup_size,
|
|---|
| 88 | void *data, size_t data_size, size_t *data_size_sent)
|
|---|
| 89 | {
|
|---|
| 90 | return usbvirt_control_transfer(dev, setup, setup_size,
|
|---|
| 91 | data, data_size, data_size_sent);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | int usbvirt_data_out(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
|
|---|
| 95 | usb_endpoint_t endpoint, void *data, size_t data_size)
|
|---|
| 96 | {
|
|---|
| 97 | if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
|
|---|
| 98 | return ERANGE;
|
|---|
| 99 | }
|
|---|
| 100 | if ((dev->ops == NULL) || (dev->ops->data_out[endpoint] == NULL)) {
|
|---|
| 101 | return ENOTSUP;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | int rc = dev->ops->data_out[endpoint](dev, endpoint, transf_type,
|
|---|
| 105 | data, data_size);
|
|---|
| 106 |
|
|---|
| 107 | return rc;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | int usbvirt_data_in(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
|
|---|
| 111 | usb_endpoint_t endpoint, void *data, size_t data_size, size_t *data_size_sent)
|
|---|
| 112 | {
|
|---|
| 113 | if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
|
|---|
| 114 | return ERANGE;
|
|---|
| 115 | }
|
|---|
| 116 | if ((dev->ops == NULL) || (dev->ops->data_in[endpoint] == NULL)) {
|
|---|
| 117 | return ENOTSUP;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | size_t data_size_sent_tmp;
|
|---|
| 121 | int rc = dev->ops->data_in[endpoint](dev, endpoint, transf_type,
|
|---|
| 122 | data, data_size, &data_size_sent_tmp);
|
|---|
| 123 |
|
|---|
| 124 | if (rc != EOK) {
|
|---|
| 125 | return rc;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | if (data_size_sent != NULL) {
|
|---|
| 129 | *data_size_sent = data_size_sent_tmp;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return EOK;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * @}
|
|---|
| 137 | */
|
|---|