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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ec700c7 was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

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