source: mainline/uspace/drv/usbmid/usbmid.c@ 3954a63b

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

Rename usb_endpoint_pipe_*() ⇒ usb_pipe_*()

No change in functionality.

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