source: mainline/uspace/drv/bus/usb/usbmid/usbmid.c@ 33b8d024

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 33b8d024 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • 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/**
34 * @file
35 * Helper functions.
36 */
37
38#include <errno.h>
39#include <str_error.h>
40#include <stdlib.h>
41#include <usb_iface.h>
42#include <usb/dev/pipes.h>
43#include <usb/classes/classes.h>
44#include <usb/dev/recognise.h>
45#include "usbmid.h"
46
47/** Get USB device handle by calling the parent usb_device_t.
48 *
49 * @param[in] fun Device function the operation is running on.
50 * @param[out] handle Device handle.
51 * @return Error code.
52 */
53static errno_t usb_iface_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
54{
55 assert(fun);
56 assert(handle);
57 usb_device_t *usb_dev = usb_device_get(ddf_fun_get_dev(fun));
58 *handle = usb_device_get_devman_handle(usb_dev);
59 return EOK;
60}
61
62/** Callback for DDF USB get interface. */
63static errno_t usb_iface_iface_no(ddf_fun_t *fun, int *iface_no)
64{
65 usbmid_interface_t *iface = ddf_fun_data_get(fun);
66 assert(iface);
67
68 if (iface_no)
69 *iface_no = iface->interface_no;
70
71 return EOK;
72}
73
74/** DDF interface of the child - USB functions. */
75static usb_iface_t child_usb_iface = {
76 .get_my_device_handle = usb_iface_device_handle,
77 .get_my_interface = usb_iface_iface_no,
78};
79
80/** Operations for children - interface functions. */
81static ddf_dev_ops_t child_device_ops = {
82 .interfaces[USB_DEV_IFACE] = &child_usb_iface
83};
84
85errno_t usbmid_interface_destroy(usbmid_interface_t *mid_iface)
86{
87 assert(mid_iface);
88 assert_link_not_used(&mid_iface->link);
89 const errno_t ret = ddf_fun_unbind(mid_iface->fun);
90 if (ret != EOK) {
91 return ret;
92 }
93 ddf_fun_destroy(mid_iface->fun);
94 return EOK;
95}
96
97/** Spawn new child device from one interface.
98 *
99 * @param parent Parent MID device.
100 * @param iface Interface information.
101 * @param device_descriptor Device descriptor.
102 * @param interface_descriptor Interface descriptor.
103 * @return Error code.
104 */
105errno_t usbmid_spawn_interface_child(usb_device_t *parent,
106 usbmid_interface_t **iface_ret,
107 const usb_standard_device_descriptor_t *device_descriptor,
108 const usb_standard_interface_descriptor_t *interface_descriptor)
109{
110 ddf_fun_t *child = NULL;
111 char *child_name = NULL;
112 errno_t rc;
113
114 /*
115 * Name is class name followed by interface number.
116 * The interface number shall provide uniqueness while the
117 * class name something humanly understandable.
118 */
119 int ret = asprintf(&child_name, "%s%hhu",
120 usb_str_class(interface_descriptor->interface_class),
121 interface_descriptor->interface_number);
122 if (ret < 0) {
123 return ENOMEM;
124 }
125
126 /* Create the device. */
127 child = usb_device_ddf_fun_create(parent, fun_inner, child_name);
128 free(child_name);
129 if (child == NULL) {
130 return ENOMEM;
131 }
132
133 match_id_list_t match_ids;
134 init_match_ids(&match_ids);
135
136 rc = usb_device_create_match_ids_from_interface(device_descriptor,
137 interface_descriptor, &match_ids);
138 if (rc != EOK) {
139 ddf_fun_destroy(child);
140 return rc;
141 }
142
143 list_foreach(match_ids.ids, link, match_id_t, match_id) {
144 rc = ddf_fun_add_match_id(child, match_id->id, match_id->score);
145 if (rc != EOK) {
146 clean_match_ids(&match_ids);
147 ddf_fun_destroy(child);
148 return rc;
149 }
150 }
151 clean_match_ids(&match_ids);
152 ddf_fun_set_ops(child, &child_device_ops);
153
154 usbmid_interface_t *iface = ddf_fun_data_alloc(child, sizeof(*iface));
155
156 iface->fun = child;
157 iface->interface_no = interface_descriptor->interface_number;
158 link_initialize(&iface->link);
159
160 rc = ddf_fun_bind(child);
161 if (rc != EOK) {
162 /* This takes care of match_id deallocation as well. */
163 ddf_fun_destroy(child);
164 return rc;
165 }
166 *iface_ret = iface;
167
168 return EOK;
169}
170
171/**
172 * @}
173 */
Note: See TracBrowser for help on using the repository browser.