source: mainline/uspace/lib/usbvirt/src/virthub_base.c@ 44d518b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 44d518b was f3185a5, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

libusbvirt: Fix sending of hub descriptor.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 * Copyright (c) 2013 Jan Vesely
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 * Virtual USB device main routines.
34 */
35
36#include <assert.h>
37#include <macros.h>
38#include <str.h>
39
40#include "virthub_base.h"
41
42extern const usb_standard_device_descriptor_t virthub_device_descriptor;
43extern const usb_standard_configuration_descriptor_t virthub_configuration_descriptor_without_hub_size;
44extern const usbvirt_device_configuration_extras_t virthub_interface_descriptor_ex;
45extern const usbvirt_device_configuration_extras_t virthub_endpoint_descriptor_ex;
46
47void *virthub_get_data(usbvirt_device_t *dev)
48{
49 assert(dev);
50 virthub_base_t *base = dev->device_data;
51 assert(base);
52 return base->data;
53}
54
55int virthub_base_init(virthub_base_t *instance,
56 const char *name, usbvirt_device_ops_t *ops, void *data,
57 const usb_standard_device_descriptor_t *device_desc,
58 const usb_hub_descriptor_header_t *hub_desc)
59{
60 assert(instance);
61 assert(hub_desc);
62 assert(name);
63
64 instance->config_descriptor =
65 virthub_configuration_descriptor_without_hub_size;
66 instance->config_descriptor.total_length += hub_desc->length;
67
68 instance->descriptors.device =
69 device_desc ? device_desc : &virthub_device_descriptor;
70 instance->descriptors.configuration = &instance->configuration;
71 instance->descriptors.configuration_count = 1;
72
73 instance->configuration.descriptor = &instance->config_descriptor;
74 instance->configuration.extra = instance->extra;
75 instance->configuration.extra_count = ARRAY_SIZE(instance->extra);
76
77 instance->extra[0] = virthub_interface_descriptor_ex;
78 instance->extra[1].data = (void *)hub_desc;
79 instance->extra[1].length = hub_desc->length;
80 instance->extra[2] = virthub_endpoint_descriptor_ex;
81
82 instance->device.ops = ops;
83 instance->device.descriptors = &instance->descriptors;
84 instance->device.name = str_dup(name);
85 instance->device.device_data = instance;
86 instance->device.address = 0;
87 instance->data = data;
88
89 return EOK;
90}
91
92usb_address_t virthub_base_get_address(virthub_base_t *instance)
93{
94 assert(instance);
95 return instance->device.address;
96}
97
98int virthub_base_request(virthub_base_t *instance, usb_target_t target,
99 usb_direction_t dir, const usb_device_request_setup_packet_t *setup,
100 void *buffer, size_t buffer_size, size_t *real_size)
101{
102 assert(instance);
103 assert(real_size);
104 assert(setup);
105
106 if (target.address != virthub_base_get_address(instance))
107 return ENOENT;
108
109 switch (dir) {
110 case USB_DIRECTION_IN:
111 if (target.endpoint == 0) {
112 return usbvirt_control_read(&instance->device,
113 setup, sizeof(*setup), buffer, buffer_size,
114 real_size);
115 } else {
116 return usbvirt_data_in(&instance->device,
117 USB_TRANSFER_INTERRUPT, target.endpoint,
118 buffer, buffer_size, real_size);
119 }
120 case USB_DIRECTION_OUT:
121 if (target.endpoint == 0) {
122 return usbvirt_control_write(&instance->device,
123 setup, sizeof(*setup), buffer, buffer_size);
124 }
125 /* fall through */
126 default:
127 return ENOTSUP;
128
129 }
130}
131
132int virthub_base_get_hub_descriptor(usbvirt_device_t *dev,
133 const usb_device_request_setup_packet_t *request, uint8_t *data,
134 size_t *act_size)
135{
136 assert(dev);
137 virthub_base_t *instance = dev->device_data;
138 assert(instance);
139 if (request->value_high == USB_DESCTYPE_HUB) {
140 usbvirt_control_reply_helper(request, data, act_size,
141 instance->extra[1].data, instance->extra[1].length);
142 return EOK;
143 }
144 /* Let the framework handle all the rest. */
145 return EFORWARD;
146}
147
148int virthub_base_get_null_status(usbvirt_device_t *dev,
149 const usb_device_request_setup_packet_t *request, uint8_t *data,
150 size_t *act_size)
151{
152 uint32_t zero = 0;
153 if (request->length != sizeof(zero))
154 return ESTALL;
155 usbvirt_control_reply_helper(request, data, act_size,
156 &zero, sizeof(zero));
157 return EOK;
158}
159
160/**
161 * @}
162 */
Note: See TracBrowser for help on using the repository browser.