source: mainline/uspace/lib/usbdev/src/recognise.c@ 58563585

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 58563585 was 58563585, checked in by Martin Decky <martin@…>, 9 years ago

code review and cstyle cleanup (no change in functionality)

  • Property mode set to 100644
File size: 9.1 KB
Line 
1/*
2 * Copyright (c) 2010 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 libusbdev
30 * @{
31 */
32/** @file
33 * Functions for recognition of attached devices.
34 */
35
36#include <usb/dev/pipes.h>
37#include <usb/dev/recognise.h>
38#include <usb/dev/request.h>
39#include <usb/classes/classes.h>
40
41#include <assert.h>
42#include <errno.h>
43#include <stdio.h>
44#include <sys/types.h>
45
46/** Get integer part from BCD coded number. */
47#define BCD_INT(a) (((unsigned int)(a)) / 256)
48/** Get fraction part from BCD coded number (as an integer, no less). */
49#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
50
51/** Format for BCD coded number to be used in printf. */
52#define BCD_FMT "%x.%x"
53/** Arguments to printf for BCD coded number. */
54#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
55
56/** Add formatted match id.
57 *
58 * @param matches List of match ids where to add to.
59 * @param score Score of the match.
60 * @param format Printf-like format
61 * @return Error code.
62 */
63static int usb_add_match_id(match_id_list_t *matches, int score,
64 const char *match_str)
65{
66 assert(matches);
67
68 match_id_t *match_id = create_match_id();
69 if (match_id == NULL) {
70 return ENOMEM;
71 }
72
73 match_id->id = match_str;
74 match_id->score = score;
75 add_match_id(matches, match_id);
76
77 return EOK;
78}
79
80/** Add match id to list or return with error code.
81 *
82 * @param match_ids List of match ids.
83 * @param score Match id score.
84 * @param format Format of the matching string
85 * @param ... Arguments for the format.
86 */
87#define ADD_MATCHID_OR_RETURN(match_ids, score, format, ...) \
88 do { \
89 char *str = NULL; \
90 int __rc = asprintf(&str, format, ##__VA_ARGS__); \
91 if (__rc > 0) { \
92 __rc = usb_add_match_id((match_ids), (score), str); \
93 } \
94 if (__rc != EOK) { \
95 free(str); \
96 return __rc; \
97 } \
98 } while (0)
99
100/** Create device match ids based on its interface.
101 *
102 * @param[in] desc_device Device descriptor.
103 * @param[in] desc_interface Interface descriptor.
104 * @param[out] matches Initialized list of match ids.
105 * @return Error code (the two mentioned are not the only ones).
106 * @retval EINVAL Invalid input parameters (expects non NULL pointers).
107 * @retval ENOENT Device class is not "use interface".
108 */
109int usb_device_create_match_ids_from_interface(
110 const usb_standard_device_descriptor_t *desc_device,
111 const usb_standard_interface_descriptor_t *desc_interface,
112 match_id_list_t *matches)
113{
114 if (desc_interface == NULL || matches == NULL) {
115 return EINVAL;
116 }
117
118 if (desc_interface->interface_class == USB_CLASS_USE_INTERFACE) {
119 return ENOENT;
120 }
121
122 const char *classname = usb_str_class(desc_interface->interface_class);
123 assert(classname != NULL);
124
125#define IFACE_PROTOCOL_FMT "interface&class=%s&subclass=0x%02x&protocol=0x%02x"
126#define IFACE_PROTOCOL_ARGS classname, desc_interface->interface_subclass, \
127 desc_interface->interface_protocol
128
129#define IFACE_SUBCLASS_FMT "interface&class=%s&subclass=0x%02x"
130#define IFACE_SUBCLASS_ARGS classname, desc_interface->interface_subclass
131
132#define IFACE_CLASS_FMT "interface&class=%s"
133#define IFACE_CLASS_ARGS classname
134
135#define VENDOR_RELEASE_FMT "vendor=0x%04x&product=0x%04x&release=" BCD_FMT
136#define VENDOR_RELEASE_ARGS desc_device->vendor_id, desc_device->product_id, \
137 BCD_ARGS(desc_device->device_version)
138
139#define VENDOR_PRODUCT_FMT "vendor=0x%04x&product=0x%04x"
140#define VENDOR_PRODUCT_ARGS desc_device->vendor_id, desc_device->product_id
141
142#define VENDOR_ONLY_FMT "vendor=0x%04x"
143#define VENDOR_ONLY_ARGS desc_device->vendor_id
144
145 /*
146 * If the vendor is specified, create match ids with vendor with
147 * higher score.
148 * Then the same ones without the vendor part.
149 */
150 if ((desc_device != NULL) && (desc_device->vendor_id != 0)) {
151 /* First, interface matches with device release number. */
152 ADD_MATCHID_OR_RETURN(matches, 250,
153 "usb&" VENDOR_RELEASE_FMT "&" IFACE_PROTOCOL_FMT,
154 VENDOR_RELEASE_ARGS, IFACE_PROTOCOL_ARGS);
155 ADD_MATCHID_OR_RETURN(matches, 240,
156 "usb&" VENDOR_RELEASE_FMT "&" IFACE_SUBCLASS_FMT,
157 VENDOR_RELEASE_ARGS, IFACE_SUBCLASS_ARGS);
158 ADD_MATCHID_OR_RETURN(matches, 230,
159 "usb&" VENDOR_RELEASE_FMT "&" IFACE_CLASS_FMT,
160 VENDOR_RELEASE_ARGS, IFACE_CLASS_ARGS);
161
162 /* Next, interface matches without release number. */
163 ADD_MATCHID_OR_RETURN(matches, 220,
164 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_PROTOCOL_FMT,
165 VENDOR_PRODUCT_ARGS, IFACE_PROTOCOL_ARGS);
166 ADD_MATCHID_OR_RETURN(matches, 210,
167 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_SUBCLASS_FMT,
168 VENDOR_PRODUCT_ARGS, IFACE_SUBCLASS_ARGS);
169 ADD_MATCHID_OR_RETURN(matches, 200,
170 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_CLASS_FMT,
171 VENDOR_PRODUCT_ARGS, IFACE_CLASS_ARGS);
172
173 /* Finally, interface matches with only vendor. */
174 ADD_MATCHID_OR_RETURN(matches, 190,
175 "usb&" VENDOR_ONLY_FMT "&" IFACE_PROTOCOL_FMT,
176 VENDOR_ONLY_ARGS, IFACE_PROTOCOL_ARGS);
177 ADD_MATCHID_OR_RETURN(matches, 180,
178 "usb&" VENDOR_ONLY_FMT "&" IFACE_SUBCLASS_FMT,
179 VENDOR_ONLY_ARGS, IFACE_SUBCLASS_ARGS);
180 ADD_MATCHID_OR_RETURN(matches, 170,
181 "usb&" VENDOR_ONLY_FMT "&" IFACE_CLASS_FMT,
182 VENDOR_ONLY_ARGS, IFACE_CLASS_ARGS);
183 }
184
185 /* Now, the same but without any vendor specification. */
186 ADD_MATCHID_OR_RETURN(matches, 160,
187 "usb&" IFACE_PROTOCOL_FMT,
188 IFACE_PROTOCOL_ARGS);
189 ADD_MATCHID_OR_RETURN(matches, 150,
190 "usb&" IFACE_SUBCLASS_FMT,
191 IFACE_SUBCLASS_ARGS);
192 ADD_MATCHID_OR_RETURN(matches, 140,
193 "usb&" IFACE_CLASS_FMT,
194 IFACE_CLASS_ARGS);
195
196#undef IFACE_PROTOCOL_FMT
197#undef IFACE_PROTOCOL_ARGS
198#undef IFACE_SUBCLASS_FMT
199#undef IFACE_SUBCLASS_ARGS
200#undef IFACE_CLASS_FMT
201#undef IFACE_CLASS_ARGS
202#undef VENDOR_RELEASE_FMT
203#undef VENDOR_RELEASE_ARGS
204#undef VENDOR_PRODUCT_FMT
205#undef VENDOR_PRODUCT_ARGS
206#undef VENDOR_ONLY_FMT
207#undef VENDOR_ONLY_ARGS
208
209 /* As a last resort, try fallback driver. */
210 ADD_MATCHID_OR_RETURN(matches, 10, "usb&interface&fallback");
211
212 return EOK;
213}
214
215/** Create DDF match ids from USB device descriptor.
216 *
217 * @param matches List of match ids to extend.
218 * @param device_descriptor Device descriptor returned by given device.
219 * @return Error code.
220 */
221int usb_device_create_match_ids_from_device_descriptor(
222 const usb_standard_device_descriptor_t *device_descriptor,
223 match_id_list_t *matches)
224{
225 /*
226 * Unless the vendor id is 0, the pair idVendor-idProduct
227 * quite uniquely describes the device.
228 */
229 if (device_descriptor->vendor_id != 0) {
230 /* First, with release number. */
231 ADD_MATCHID_OR_RETURN(matches, 100,
232 "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
233 (int) device_descriptor->vendor_id,
234 (int) device_descriptor->product_id,
235 BCD_ARGS(device_descriptor->device_version));
236
237 /* Next, without release number. */
238 ADD_MATCHID_OR_RETURN(matches, 90,
239 "usb&vendor=0x%04x&product=0x%04x",
240 (int) device_descriptor->vendor_id,
241 (int) device_descriptor->product_id);
242 }
243
244 /* Class match id */
245 ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
246 usb_str_class(device_descriptor->device_class));
247
248 /* As a last resort, try fallback driver. */
249 ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
250
251 return EOK;
252}
253
254
255/** Create match ids describing attached device.
256 *
257 * @warning The list of match ids @p matches may change even when
258 * function exits with error.
259 *
260 * @param ctrl_pipe Control pipe to given device (session must be already
261 * started).
262 * @param matches Initialized list of match ids.
263 * @return Error code.
264 */
265int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
266 match_id_list_t *matches)
267{
268 assert(ctrl_pipe);
269 int rc;
270 /*
271 * Retrieve device descriptor and add matches from it.
272 */
273 usb_standard_device_descriptor_t device_descriptor;
274
275 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
276 if (rc != EOK) {
277 return rc;
278 }
279
280 rc = usb_device_create_match_ids_from_device_descriptor(
281 &device_descriptor, matches);
282 if (rc != EOK) {
283 return rc;
284 }
285
286 return EOK;
287}
288
289/**
290 * @}
291 */
Note: See TracBrowser for help on using the repository browser.