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

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

libusbvirt: Add malloc check.

  • 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.device_data = instance;
85 instance->device.address = 0;
86 instance->data = data;
87 instance->device.name = str_dup(name);
88
89 if (!instance->device.name)
90 return ENOMEM;
91
92 return EOK;
93}
94
95usb_address_t virthub_base_get_address(virthub_base_t *instance)
96{
97 assert(instance);
98 return instance->device.address;
99}
100
101int virthub_base_request(virthub_base_t *instance, usb_target_t target,
102 usb_direction_t dir, const usb_device_request_setup_packet_t *setup,
103 void *buffer, size_t buffer_size, size_t *real_size)
104{
105 assert(instance);
106 assert(real_size);
107 assert(setup);
108
109 if (target.address != virthub_base_get_address(instance))
110 return ENOENT;
111
112 switch (dir) {
113 case USB_DIRECTION_IN:
114 if (target.endpoint == 0) {
115 return usbvirt_control_read(&instance->device,
116 setup, sizeof(*setup), buffer, buffer_size,
117 real_size);
118 } else {
119 return usbvirt_data_in(&instance->device,
120 USB_TRANSFER_INTERRUPT, target.endpoint,
121 buffer, buffer_size, real_size);
122 }
123 case USB_DIRECTION_OUT:
124 if (target.endpoint == 0) {
125 return usbvirt_control_write(&instance->device,
126 setup, sizeof(*setup), buffer, buffer_size);
127 }
128 /* fall through */
129 default:
130 return ENOTSUP;
131
132 }
133}
134
135int virthub_base_get_hub_descriptor(usbvirt_device_t *dev,
136 const usb_device_request_setup_packet_t *request, uint8_t *data,
137 size_t *act_size)
138{
139 assert(dev);
140 virthub_base_t *instance = dev->device_data;
141 assert(instance);
142 if (request->value_high == USB_DESCTYPE_HUB) {
143 usbvirt_control_reply_helper(request, data, act_size,
144 instance->extra[1].data, instance->extra[1].length);
145 return EOK;
146 }
147 /* Let the framework handle all the rest. */
148 return EFORWARD;
149}
150
151int virthub_base_get_null_status(usbvirt_device_t *dev,
152 const usb_device_request_setup_packet_t *request, uint8_t *data,
153 size_t *act_size)
154{
155 uint32_t zero = 0;
156 if (request->length != sizeof(zero))
157 return ESTALL;
158 usbvirt_control_reply_helper(request, data, act_size,
159 &zero, sizeof(zero));
160 return EOK;
161}
162
163/**
164 * @}
165 */
Note: See TracBrowser for help on using the repository browser.