source: mainline/uspace/drv/usbmid/usbmid.c@ a6add7a

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

Comment and documentation fixes

In the process also removed several functions that are no longer
needed.

This commit shall not affect functionality in any way.

  • Property mode set to 100644
File size: 5.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 drvusbmid
30 * @{
31 */
32/**
33 * @file
34 * Helper functions.
35 */
36#include <errno.h>
37#include <str_error.h>
38#include <stdlib.h>
39#include <usb_iface.h>
40#include <usb/ddfiface.h>
41#include <usb/pipes.h>
42#include <usb/classes/classes.h>
43#include <usb/recognise.h>
44#include "usbmid.h"
45
46/** Callback for DDF USB interface. */
47static int usb_iface_get_address_impl(ddf_fun_t *fun, devman_handle_t handle,
48 usb_address_t *address)
49{
50 return usb_iface_get_address_hub_impl(fun, handle, address);
51}
52
53/** Callback for DDF USB interface. */
54static int usb_iface_get_interface_impl(ddf_fun_t *fun, devman_handle_t handle,
55 int *iface_no)
56{
57 assert(fun);
58
59 usbmid_interface_t *iface = fun->driver_data;
60 assert(iface);
61
62 if (iface_no != NULL) {
63 *iface_no = iface->interface_no;
64 }
65
66 return EOK;
67}
68
69/** DDF interface of the child - interface function. */
70static usb_iface_t child_usb_iface = {
71 .get_hc_handle = usb_iface_get_hc_handle_hub_child_impl,
72 .get_address = usb_iface_get_address_impl,
73 .get_interface = usb_iface_get_interface_impl
74};
75
76/** Operations for children - interface functions. */
77static ddf_dev_ops_t child_device_ops = {
78 .interfaces[USB_DEV_IFACE] = &child_usb_iface
79};
80
81/** Operations of the device itself. */
82static ddf_dev_ops_t mid_device_ops = {
83 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
84};
85
86/** Create new USB multi interface device.
87 *
88 * @param dev Backing generic DDF device.
89 * @return New USB MID device.
90 * @retval NULL Error occured.
91 */
92usbmid_device_t *usbmid_device_create(ddf_dev_t *dev)
93{
94 usbmid_device_t *mid = malloc(sizeof(usbmid_device_t));
95 if (mid == NULL) {
96 usb_log_error("Out of memory (wanted %zu bytes).\n",
97 sizeof(usbmid_device_t));
98 return NULL;
99 }
100
101 int rc;
102 rc = usb_device_connection_initialize_from_device(&mid->wire, dev);
103 if (rc != EOK) {
104 usb_log_error("Failed to initialize `USB wire': %s.\n",
105 str_error(rc));
106 free(mid);
107 return NULL;
108 }
109
110 rc = usb_endpoint_pipe_initialize_default_control(&mid->ctrl_pipe,
111 &mid->wire);
112 if (rc != EOK) {
113 usb_log_error("Failed to initialize control pipe: %s.\n",
114 str_error(rc));
115 free(mid);
116 return NULL;
117 }
118
119 mid->dev = dev;
120 (void) &mid_device_ops;
121
122 return mid;
123}
124
125/** Create new interface for USB MID device.
126 *
127 * @param dev Backing generic DDF child device (representing interface).
128 * @param iface_no Interface number.
129 * @return New interface.
130 * @retval NULL Error occured.
131 */
132usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)
133{
134 usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));
135 if (iface == NULL) {
136 usb_log_error("Out of memory (wanted %zuB).\n",
137 sizeof(usbmid_interface_t));
138 return NULL;
139 }
140
141 iface->fun = fun;
142 iface->interface_no = iface_no;
143
144 return iface;
145}
146
147
148/** Spawn new child device from one interface.
149 *
150 * @param parent Parent MID device.
151 * @param device_descriptor Device descriptor.
152 * @param interface_descriptor Interface descriptor.
153 * @return Error code.
154 */
155int usbmid_spawn_interface_child(usbmid_device_t *parent,
156 const usb_standard_device_descriptor_t *device_descriptor,
157 const usb_standard_interface_descriptor_t *interface_descriptor)
158{
159 ddf_fun_t *child = NULL;
160 char *child_name = NULL;
161 usbmid_interface_t *child_as_interface = NULL;
162 int rc;
163
164 /*
165 * Name is class name followed by interface number.
166 * The interface number shall provide uniqueness while the
167 * class name something humanly understandable.
168 */
169 rc = asprintf(&child_name, "%s%d",
170 usb_str_class(interface_descriptor->interface_class),
171 (int) interface_descriptor->interface_number);
172 if (rc < 0) {
173 goto error_leave;
174 }
175
176 /* Create the device. */
177 child = ddf_fun_create(parent->dev, fun_inner, child_name);
178 if (child == NULL) {
179 rc = ENOMEM;
180 goto error_leave;
181 }
182
183
184
185 child_as_interface = usbmid_interface_create(child,
186 (int) interface_descriptor->interface_number);
187 if (child_as_interface == NULL) {
188 rc = ENOMEM;
189 goto error_leave;
190 }
191
192 child->driver_data = child_as_interface;
193 child->ops = &child_device_ops;
194
195 rc = usb_device_create_match_ids_from_interface(device_descriptor,
196 interface_descriptor,
197 &child->match_ids);
198 if (rc != EOK) {
199 goto error_leave;
200 }
201
202 rc = ddf_fun_bind(child);
203 if (rc != EOK) {
204 goto error_leave;
205 }
206
207 return EOK;
208
209error_leave:
210 if (child != NULL) {
211 child->name = NULL;
212 /* This takes care of match_id deallocation as well. */
213 ddf_fun_destroy(child);
214 }
215 if (child_name != NULL) {
216 free(child_name);
217 }
218 if (child_as_interface != NULL) {
219 free(child_as_interface);
220 }
221
222 return rc;
223}
224
225/**
226 * @}
227 */
Note: See TracBrowser for help on using the repository browser.