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