source: mainline/uspace/drv/usbmid/usbmid.c@ 5857be2

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

USB MID uses the new framework

  • Property mode set to 100644
File size: 4.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/** Create new interface for USB MID device.
82 *
83 * @param fun Backing generic DDF device function (representing interface).
84 * @param iface_no Interface number.
85 * @return New interface.
86 * @retval NULL Error occured.
87 */
88usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)
89{
90 usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));
91 if (iface == NULL) {
92 usb_log_error("Out of memory (wanted %zuB).\n",
93 sizeof(usbmid_interface_t));
94 return NULL;
95 }
96
97 iface->fun = fun;
98 iface->interface_no = iface_no;
99
100 return iface;
101}
102
103
104/** Spawn new child device from one interface.
105 *
106 * @param parent Parent MID device.
107 * @param device_descriptor Device descriptor.
108 * @param interface_descriptor Interface descriptor.
109 * @return Error code.
110 */
111int usbmid_spawn_interface_child(usb_device_t *parent,
112 const usb_standard_device_descriptor_t *device_descriptor,
113 const usb_standard_interface_descriptor_t *interface_descriptor)
114{
115 ddf_fun_t *child = NULL;
116 char *child_name = NULL;
117 usbmid_interface_t *child_as_interface = NULL;
118 int rc;
119
120 /*
121 * Name is class name followed by interface number.
122 * The interface number shall provide uniqueness while the
123 * class name something humanly understandable.
124 */
125 rc = asprintf(&child_name, "%s%d",
126 usb_str_class(interface_descriptor->interface_class),
127 (int) interface_descriptor->interface_number);
128 if (rc < 0) {
129 goto error_leave;
130 }
131
132 /* Create the device. */
133 child = ddf_fun_create(parent->ddf_dev, fun_inner, child_name);
134 if (child == NULL) {
135 rc = ENOMEM;
136 goto error_leave;
137 }
138
139
140
141 child_as_interface = usbmid_interface_create(child,
142 (int) interface_descriptor->interface_number);
143 if (child_as_interface == NULL) {
144 rc = ENOMEM;
145 goto error_leave;
146 }
147
148 child->driver_data = child_as_interface;
149 child->ops = &child_device_ops;
150
151 rc = usb_device_create_match_ids_from_interface(device_descriptor,
152 interface_descriptor,
153 &child->match_ids);
154 if (rc != EOK) {
155 goto error_leave;
156 }
157
158 rc = ddf_fun_bind(child);
159 if (rc != EOK) {
160 goto error_leave;
161 }
162
163 return EOK;
164
165error_leave:
166 if (child != NULL) {
167 child->name = NULL;
168 /* This takes care of match_id deallocation as well. */
169 ddf_fun_destroy(child);
170 }
171 if (child_name != NULL) {
172 free(child_name);
173 }
174 if (child_as_interface != NULL) {
175 free(child_as_interface);
176 }
177
178 return rc;
179}
180
181/**
182 * @}
183 */
Note: See TracBrowser for help on using the repository browser.