source: mainline/uspace/lib/usbvirt/src/transfer.c@ aa9a53d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aa9a53d was 6cb58e6, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Virtual USB layer rewritten

Major changes include

  • IPC sends whole transfers (not transactions)
  • separate transfer queues for each device in host controller
  • possibility to return NAK from virtual device (handled by HC)
  • better implementation of callbacks for non-zero endpoints

Still missing

  • communication for some transfer types (bulk)
  • face-lift ;-)
  • documentation
  • Property mode set to 100644
File size: 3.9 KB
Line 
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
41static 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} not handled.\n",
71 usb_debug_str_buffer(setup, setup_size, 10));
72 rc = EBADCHECKSUM;
73 }
74
75 return rc;
76}
77
78int usbvirt_control_write(usbvirt_device_t *dev, void *setup, size_t setup_size,
79 void *data, size_t data_size)
80{
81 return usbvirt_control_transfer(dev, setup, setup_size,
82 data, data_size, NULL);
83}
84
85int usbvirt_control_read(usbvirt_device_t *dev, void *setup, size_t setup_size,
86 void *data, size_t data_size, size_t *data_size_sent)
87{
88 return usbvirt_control_transfer(dev, setup, setup_size,
89 data, data_size, data_size_sent);
90}
91
92int usbvirt_data_out(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
93 usb_endpoint_t endpoint, void *data, size_t data_size)
94{
95 if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
96 return ERANGE;
97 }
98 if ((dev->ops == NULL) || (dev->ops->data_out[endpoint] == NULL)) {
99 return ENOTSUP;
100 }
101
102 int rc = dev->ops->data_out[endpoint](dev, endpoint, transf_type,
103 data, data_size);
104
105 return rc;
106}
107
108int usbvirt_data_in(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
109 usb_endpoint_t endpoint, void *data, size_t data_size, size_t *data_size_sent)
110{
111 if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
112 return ERANGE;
113 }
114 if ((dev->ops == NULL) || (dev->ops->data_in[endpoint] == NULL)) {
115 return ENOTSUP;
116 }
117
118 size_t data_size_sent_tmp;
119 int rc = dev->ops->data_in[endpoint](dev, endpoint, transf_type,
120 data, data_size, &data_size_sent_tmp);
121
122 if (rc != EOK) {
123 return rc;
124 }
125
126 if (data_size_sent != NULL) {
127 *data_size_sent = data_size_sent_tmp;
128 }
129
130 return EOK;
131}
132
133/**
134 * @}
135 */
Note: See TracBrowser for help on using the repository browser.