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

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

libusbdev: Use asprintf instead of 256 byte buffer for match strings.

  • Property mode set to 100644
File size: 12.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#include <sys/types.h>
36#include <fibril_synch.h>
37#include <usb/debug.h>
38#include <usb/dev/hub.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/** Index to append after device name for uniqueness. */
49static size_t device_name_index = 0;
50/** Mutex guard for device_name_index. */
51static FIBRIL_MUTEX_INITIALIZE(device_name_index_mutex);
52
53/** DDF operations of child devices. */
54static ddf_dev_ops_t child_ops = {
55 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_child_impl
56};
57
58/** Get integer part from BCD coded number. */
59#define BCD_INT(a) (((unsigned int)(a)) / 256)
60/** Get fraction part from BCD coded number (as an integer, no less). */
61#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
62
63/** Format for BCD coded number to be used in printf. */
64#define BCD_FMT "%x.%x"
65/** Arguments to printf for BCD coded number. */
66#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
67
68/** Add formatted match id.
69 *
70 * @param matches List of match ids where to add to.
71 * @param score Score of the match.
72 * @param format Printf-like format
73 * @return Error code.
74 */
75static int usb_add_match_id(match_id_list_t *matches, int score,
76 const char *match_str)
77{
78 assert(matches);
79
80 match_id_t *match_id = create_match_id();
81 if (match_id == NULL) {
82 return ENOMEM;
83 }
84
85 match_id->id = match_str;
86 match_id->score = score;
87 add_match_id(matches, match_id);
88
89 return EOK;
90}
91
92/** Add match id to list or return with error code.
93 *
94 * @param match_ids List of match ids.
95 * @param score Match id score.
96 * @param format Format of the matching string
97 * @param ... Arguments for the format.
98 */
99#define ADD_MATCHID_OR_RETURN(match_ids, score, format, ...) \
100 do { \
101 char *str = NULL; \
102 int __rc = asprintf(&str, format, ##__VA_ARGS__); \
103 if (__rc > 0) { \
104 __rc = usb_add_match_id((match_ids), (score), str); \
105 } \
106 if (__rc != EOK) { \
107 free(str); \
108 return __rc; \
109 } \
110 } while (0)
111
112/** Create device match ids based on its interface.
113 *
114 * @param[in] desc_device Device descriptor.
115 * @param[in] desc_interface Interface descriptor.
116 * @param[out] matches Initialized list of match ids.
117 * @return Error code (the two mentioned are not the only ones).
118 * @retval EINVAL Invalid input parameters (expects non NULL pointers).
119 * @retval ENOENT Device class is not "use interface".
120 */
121int usb_device_create_match_ids_from_interface(
122 const usb_standard_device_descriptor_t *desc_device,
123 const usb_standard_interface_descriptor_t *desc_interface,
124 match_id_list_t *matches)
125{
126 if (desc_interface == NULL || matches == NULL) {
127 return EINVAL;
128 }
129
130 if (desc_interface->interface_class == USB_CLASS_USE_INTERFACE) {
131 return ENOENT;
132 }
133
134 const char *classname = usb_str_class(desc_interface->interface_class);
135 assert(classname != NULL);
136
137#define IFACE_PROTOCOL_FMT "interface&class=%s&subclass=0x%02x&protocol=0x%02x"
138#define IFACE_PROTOCOL_ARGS classname, desc_interface->interface_subclass, \
139 desc_interface->interface_protocol
140
141#define IFACE_SUBCLASS_FMT "interface&class=%s&subclass=0x%02x"
142#define IFACE_SUBCLASS_ARGS classname, desc_interface->interface_subclass
143
144#define IFACE_CLASS_FMT "interface&class=%s"
145#define IFACE_CLASS_ARGS classname
146
147#define VENDOR_RELEASE_FMT "vendor=0x%04x&product=0x%04x&release=" BCD_FMT
148#define VENDOR_RELEASE_ARGS desc_device->vendor_id, desc_device->product_id, \
149 BCD_ARGS(desc_device->device_version)
150
151#define VENDOR_PRODUCT_FMT "vendor=0x%04x&product=0x%04x"
152#define VENDOR_PRODUCT_ARGS desc_device->vendor_id, desc_device->product_id
153
154#define VENDOR_ONLY_FMT "vendor=0x%04x"
155#define VENDOR_ONLY_ARGS desc_device->vendor_id
156
157 /*
158 * If the vendor is specified, create match ids with vendor with
159 * higher score.
160 * Then the same ones without the vendor part.
161 */
162 if ((desc_device != NULL) && (desc_device->vendor_id != 0)) {
163 /* First, interface matches with device release number. */
164 ADD_MATCHID_OR_RETURN(matches, 250,
165 "usb&" VENDOR_RELEASE_FMT "&" IFACE_PROTOCOL_FMT,
166 VENDOR_RELEASE_ARGS, IFACE_PROTOCOL_ARGS);
167 ADD_MATCHID_OR_RETURN(matches, 240,
168 "usb&" VENDOR_RELEASE_FMT "&" IFACE_SUBCLASS_FMT,
169 VENDOR_RELEASE_ARGS, IFACE_SUBCLASS_ARGS);
170 ADD_MATCHID_OR_RETURN(matches, 230,
171 "usb&" VENDOR_RELEASE_FMT "&" IFACE_CLASS_FMT,
172 VENDOR_RELEASE_ARGS, IFACE_CLASS_ARGS);
173
174 /* Next, interface matches without release number. */
175 ADD_MATCHID_OR_RETURN(matches, 220,
176 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_PROTOCOL_FMT,
177 VENDOR_PRODUCT_ARGS, IFACE_PROTOCOL_ARGS);
178 ADD_MATCHID_OR_RETURN(matches, 210,
179 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_SUBCLASS_FMT,
180 VENDOR_PRODUCT_ARGS, IFACE_SUBCLASS_ARGS);
181 ADD_MATCHID_OR_RETURN(matches, 200,
182 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_CLASS_FMT,
183 VENDOR_PRODUCT_ARGS, IFACE_CLASS_ARGS);
184
185 /* Finally, interface matches with only vendor. */
186 ADD_MATCHID_OR_RETURN(matches, 190,
187 "usb&" VENDOR_ONLY_FMT "&" IFACE_PROTOCOL_FMT,
188 VENDOR_ONLY_ARGS, IFACE_PROTOCOL_ARGS);
189 ADD_MATCHID_OR_RETURN(matches, 180,
190 "usb&" VENDOR_ONLY_FMT "&" IFACE_SUBCLASS_FMT,
191 VENDOR_ONLY_ARGS, IFACE_SUBCLASS_ARGS);
192 ADD_MATCHID_OR_RETURN(matches, 170,
193 "usb&" VENDOR_ONLY_FMT "&" IFACE_CLASS_FMT,
194 VENDOR_ONLY_ARGS, IFACE_CLASS_ARGS);
195 }
196
197 /* Now, the same but without any vendor specification. */
198 ADD_MATCHID_OR_RETURN(matches, 160,
199 "usb&" IFACE_PROTOCOL_FMT,
200 IFACE_PROTOCOL_ARGS);
201 ADD_MATCHID_OR_RETURN(matches, 150,
202 "usb&" IFACE_SUBCLASS_FMT,
203 IFACE_SUBCLASS_ARGS);
204 ADD_MATCHID_OR_RETURN(matches, 140,
205 "usb&" IFACE_CLASS_FMT,
206 IFACE_CLASS_ARGS);
207
208#undef IFACE_PROTOCOL_FMT
209#undef IFACE_PROTOCOL_ARGS
210#undef IFACE_SUBCLASS_FMT
211#undef IFACE_SUBCLASS_ARGS
212#undef IFACE_CLASS_FMT
213#undef IFACE_CLASS_ARGS
214#undef VENDOR_RELEASE_FMT
215#undef VENDOR_RELEASE_ARGS
216#undef VENDOR_PRODUCT_FMT
217#undef VENDOR_PRODUCT_ARGS
218#undef VENDOR_ONLY_FMT
219#undef VENDOR_ONLY_ARGS
220
221 /* As a last resort, try fallback driver. */
222 ADD_MATCHID_OR_RETURN(matches, 10, "usb&interface&fallback");
223
224 return EOK;
225}
226
227/** Create DDF match ids from USB device descriptor.
228 *
229 * @param matches List of match ids to extend.
230 * @param device_descriptor Device descriptor returned by given device.
231 * @return Error code.
232 */
233int usb_device_create_match_ids_from_device_descriptor(
234 const usb_standard_device_descriptor_t *device_descriptor,
235 match_id_list_t *matches)
236{
237 /*
238 * Unless the vendor id is 0, the pair idVendor-idProduct
239 * quite uniquely describes the device.
240 */
241 if (device_descriptor->vendor_id != 0) {
242 /* First, with release number. */
243 ADD_MATCHID_OR_RETURN(matches, 100,
244 "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
245 (int) device_descriptor->vendor_id,
246 (int) device_descriptor->product_id,
247 BCD_ARGS(device_descriptor->device_version));
248
249 /* Next, without release number. */
250 ADD_MATCHID_OR_RETURN(matches, 90,
251 "usb&vendor=0x%04x&product=0x%04x",
252 (int) device_descriptor->vendor_id,
253 (int) device_descriptor->product_id);
254 }
255
256 /*
257 * If the device class points to interface we skip adding
258 * class directly but we add a multi interface device.
259 */
260 if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
261 ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
262 usb_str_class(device_descriptor->device_class));
263 } else {
264 ADD_MATCHID_OR_RETURN(matches, 50, "usb&mid");
265 }
266
267 /* As a last resort, try fallback driver. */
268 ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
269
270 return EOK;
271}
272
273
274/** Create match ids describing attached device.
275 *
276 * @warning The list of match ids @p matches may change even when
277 * function exits with error.
278 *
279 * @param ctrl_pipe Control pipe to given device (session must be already
280 * started).
281 * @param matches Initialized list of match ids.
282 * @return Error code.
283 */
284int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
285 match_id_list_t *matches)
286{
287 assert(ctrl_pipe);
288 int rc;
289 /*
290 * Retrieve device descriptor and add matches from it.
291 */
292 usb_standard_device_descriptor_t device_descriptor;
293
294 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
295 if (rc != EOK) {
296 return rc;
297 }
298
299 rc = usb_device_create_match_ids_from_device_descriptor(
300 &device_descriptor, matches);
301 if (rc != EOK) {
302 return rc;
303 }
304
305 return EOK;
306}
307
308/** Probe for device kind and register it in devman.
309 *
310 * @param[in] ctrl_pipe Control pipe to the device.
311 * @param[in] parent Parent device.
312 * @param[in] dev_ops Child device ops. Default child_ops will be used if NULL.
313 * @param[in] dev_data Arbitrary pointer to be stored in the child
314 * as @c driver_data.
315 * @param[out] child_fun Storage where pointer to allocated child function
316 * will be written.
317 * @return Error code.
318 */
319int usb_device_register_child_in_devman(usb_pipe_t *ctrl_pipe,
320 ddf_dev_t *parent, ddf_dev_ops_t *dev_ops, void *dev_data,
321 ddf_fun_t **child_fun)
322{
323 if (child_fun == NULL || ctrl_pipe == NULL)
324 return EINVAL;
325
326 if (!dev_ops && dev_data) {
327 usb_log_warning("Using standard fun ops with arbitrary "
328 "driver data. This does not have to work.\n");
329 }
330
331 fibril_mutex_lock(&device_name_index_mutex);
332 const size_t this_device_name_index = device_name_index++;
333 fibril_mutex_unlock(&device_name_index_mutex);
334
335 ddf_fun_t *child = NULL;
336 int rc;
337
338 /*
339 * TODO: Once the device driver framework support persistent
340 * naming etc., something more descriptive could be created.
341 */
342 char child_name[12]; /* The format is: "usbAB_aXYZ", length 11 */
343 rc = snprintf(child_name, sizeof(child_name),
344 "usb%02zu_a%d", this_device_name_index, ctrl_pipe->wire->address);
345 if (rc < 0) {
346 goto failure;
347 }
348
349 child = ddf_fun_create(parent, fun_inner, child_name);
350 if (child == NULL) {
351 rc = ENOMEM;
352 goto failure;
353 }
354
355 if (dev_ops != NULL) {
356 child->ops = dev_ops;
357 } else {
358 child->ops = &child_ops;
359 }
360
361 child->driver_data = dev_data;
362 /* Store the attached device in fun driver data if there is no
363 * other data */
364 if (!dev_data) {
365 usb_hub_attached_device_t *new_device = ddf_fun_data_alloc(
366 child, sizeof(usb_hub_attached_device_t));
367 if (!new_device) {
368 rc = ENOMEM;
369 goto failure;
370 }
371 new_device->address = ctrl_pipe->wire->address;
372 new_device->fun = child;
373 }
374
375
376 rc = usb_device_create_match_ids(ctrl_pipe, &child->match_ids);
377 if (rc != EOK) {
378 goto failure;
379 }
380
381 rc = ddf_fun_bind(child);
382 if (rc != EOK) {
383 goto failure;
384 }
385
386 *child_fun = child;
387 return EOK;
388
389failure:
390 if (child != NULL) {
391 /* We know nothing about the data if it came from outside. */
392 if (dev_data) {
393 child->driver_data = NULL;
394 }
395 /* This takes care of match_id deallocation as well. */
396 ddf_fun_destroy(child);
397 }
398
399 return rc;
400}
401
402
403/**
404 * @}
405 */
Note: See TracBrowser for help on using the repository browser.