source: mainline/uspace/drv/bus/usb/usbmid/usbmid.c

Last change on this file was e0a5d4c, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usb: update copyrights

The data was generated by a script, guided manually. If you feel your
name is missing somewhere, please add it!

The semi-automated process was roughly:

1) Changes per file and author (limited to our team) were counted
2) Trivial numbers were thrown away
3) Authors were sorted by lines added to file
4) All previous copyrights were replaced by the newly generated one
5) Hunks changing only year were discarded

It seems that a lot of my copyrights were added. It is due to me being
both sticking my nose everywhere and lazy to update the copyright right
away :)

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2018 Ondrej Hlavaty
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup drvusbmid
31 * @{
32 */
33
34/**
35 * @file
36 * Helper functions.
37 */
38
39#include <errno.h>
40#include <str_error.h>
41#include <stdlib.h>
42#include <usb_iface.h>
43#include <usb/dev/pipes.h>
44#include <usb/classes/classes.h>
45#include <usb/dev/recognise.h>
46#include "usbmid.h"
47
48/**
49 * Get USB device description by calling HC and altering the interface field.
50 *
51 * @param[in] fun Device function the operation is running on.
52 * @param[out] desc Device descriptor.
53 * @return Error code.
54 */
55static errno_t usb_iface_description(ddf_fun_t *fun, usb_device_desc_t *desc)
56{
57 usbmid_interface_t *iface = ddf_fun_data_get(fun);
58 assert(iface);
59 usb_device_t *usb_dev = ddf_dev_data_get(ddf_fun_get_dev(fun));
60 assert(usb_dev);
61
62 async_exch_t *exch = usb_device_bus_exchange_begin(usb_dev);
63 if (!exch)
64 return EPARTY;
65
66 usb_device_desc_t tmp_desc;
67 const errno_t ret = usb_get_my_description(exch, &tmp_desc);
68
69 if (ret == EOK && desc) {
70 *desc = tmp_desc;
71 desc->iface = iface->interface_no;
72 }
73
74 usb_device_bus_exchange_end(exch);
75
76 return EOK;
77}
78
79/** DDF interface of the child - USB functions. */
80static usb_iface_t child_usb_iface = {
81 .get_my_description = usb_iface_description,
82};
83
84/** Operations for children - interface functions. */
85static ddf_dev_ops_t child_device_ops = {
86 .interfaces[USB_DEV_IFACE] = &child_usb_iface
87};
88
89errno_t usbmid_interface_destroy(usbmid_interface_t *mid_iface)
90{
91 assert(mid_iface);
92 assert_link_not_used(&mid_iface->link);
93 const errno_t ret = ddf_fun_unbind(mid_iface->fun);
94 if (ret != EOK) {
95 return ret;
96 }
97 ddf_fun_destroy(mid_iface->fun);
98 return EOK;
99}
100
101/** Spawn new child device from one interface.
102 *
103 * @param parent Parent MID device.
104 * @param iface Interface information.
105 * @param device_descriptor Device descriptor.
106 * @param interface_descriptor Interface descriptor.
107 * @return Error code.
108 */
109errno_t usbmid_spawn_interface_child(usb_device_t *parent,
110 usbmid_interface_t **iface_ret,
111 const usb_standard_device_descriptor_t *device_descriptor,
112 const usb_standard_interface_descriptor_t *interface_descriptor)
113{
114 ddf_fun_t *child = NULL;
115 char *child_name = NULL;
116 errno_t rc;
117
118 /*
119 * Name is class name followed by interface number.
120 * The interface number shall provide uniqueness while the
121 * class name something humanly understandable.
122 */
123 errno_t ret = asprintf(&child_name, "%s%hhu",
124 usb_str_class(interface_descriptor->interface_class),
125 interface_descriptor->interface_number);
126 if (ret < 0) {
127 return ENOMEM;
128 }
129
130 /* Create the device. */
131 child = usb_device_ddf_fun_create(parent, fun_inner, child_name);
132 free(child_name);
133 if (child == NULL) {
134 return ENOMEM;
135 }
136
137 match_id_list_t match_ids;
138 init_match_ids(&match_ids);
139
140 rc = usb_device_create_match_ids_from_interface(device_descriptor,
141 interface_descriptor, &match_ids);
142 if (rc != EOK) {
143 ddf_fun_destroy(child);
144 return rc;
145 }
146
147 list_foreach(match_ids.ids, link, match_id_t, match_id) {
148 rc = ddf_fun_add_match_id(child, match_id->id, match_id->score);
149 if (rc != EOK) {
150 clean_match_ids(&match_ids);
151 ddf_fun_destroy(child);
152 return rc;
153 }
154 }
155 clean_match_ids(&match_ids);
156 ddf_fun_set_ops(child, &child_device_ops);
157
158 usbmid_interface_t *iface = ddf_fun_data_alloc(child, sizeof(*iface));
159
160 iface->fun = child;
161 iface->interface_no = interface_descriptor->interface_number;
162 link_initialize(&iface->link);
163
164 rc = ddf_fun_bind(child);
165 if (rc != EOK) {
166 /* This takes care of match_id deallocation as well. */
167 ddf_fun_destroy(child);
168 return rc;
169 }
170 *iface_ret = iface;
171
172 return EOK;
173}
174
175/**
176 * @}
177 */
Note: See TracBrowser for help on using the repository browser.