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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ef4e8eb was ef4e8eb, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

Remove unused usb hub code.

All the functionality is now handled by hc driver and libusbhost.

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