source: mainline/uspace/lib/usb/src/recognise.c@ a6add7a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a6add7a was a6add7a, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Comment and documentation fixes

In the process also removed several functions that are no longer
needed.

This commit shall not affect functionality in any way.

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