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

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

Use hexadecimals for vendor ids

  • Property mode set to 100644
File size: 11.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 * @brief Functions for recognising kind of attached devices.
34 */
35#include <sys/types.h>
36#include <usb_iface.h>
37#include <usb/usbdrv.h>
38#include <usb/pipes.h>
39#include <usb/recognise.h>
40#include <usb/request.h>
41#include <usb/classes/classes.h>
42#include <stdio.h>
43#include <errno.h>
44
45static size_t device_name_index = 0;
46static FIBRIL_MUTEX_INITIALIZE(device_name_index_mutex);
47
48/** Callback for getting host controller handle.
49 *
50 * @param dev Device in question.
51 * @param[out] handle Devman handle of the host controller.
52 * @return Error code.
53 */
54static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
55{
56 assert(dev);
57 assert(dev->parent != NULL);
58
59 device_t *parent = dev->parent;
60
61 if (parent->ops && parent->ops->interfaces[USB_DEV_IFACE]) {
62 usb_iface_t *usb_iface
63 = (usb_iface_t *) parent->ops->interfaces[USB_DEV_IFACE];
64 assert(usb_iface != NULL);
65 if (usb_iface->get_hc_handle) {
66 int rc = usb_iface->get_hc_handle(parent, handle);
67 return rc;
68 }
69 }
70
71 return ENOTSUP;
72}
73
74static usb_iface_t usb_iface = {
75 .get_hc_handle = usb_iface_get_hc_handle
76};
77
78device_ops_t child_ops = {
79 .interfaces[USB_DEV_IFACE] = &usb_iface
80};
81
82#define BCD_INT(a) (((unsigned int)(a)) / 256)
83#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
84
85#define BCD_FMT "%x.%x"
86#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
87
88/* FIXME: make this dynamic */
89#define MATCH_STRING_MAX 256
90
91/** Add formatted match id.
92 *
93 * @param matches List of match ids where to add to.
94 * @param score Score of the match.
95 * @param format Printf-like format
96 * @return Error code.
97 */
98static int usb_add_match_id(match_id_list_t *matches, int score,
99 const char *format, ...)
100{
101 char *match_str = NULL;
102 match_id_t *match_id = NULL;
103 int rc;
104
105 match_str = malloc(MATCH_STRING_MAX + 1);
106 if (match_str == NULL) {
107 rc = ENOMEM;
108 goto failure;
109 }
110
111 /*
112 * FIXME: replace with dynamic allocation of exact size
113 */
114 va_list args;
115 va_start(args, format );
116 vsnprintf(match_str, MATCH_STRING_MAX, format, args);
117 match_str[MATCH_STRING_MAX] = 0;
118 va_end(args);
119
120 match_id = create_match_id();
121 if (match_id == NULL) {
122 rc = ENOMEM;
123 goto failure;
124 }
125
126 match_id->id = match_str;
127 match_id->score = score;
128 add_match_id(matches, match_id);
129
130 return EOK;
131
132failure:
133 if (match_str != NULL) {
134 free(match_str);
135 }
136 if (match_id != NULL) {
137 match_id->id = NULL;
138 delete_match_id(match_id);
139 }
140
141 return rc;
142}
143
144/** Create DDF match ids from USB device descriptor.
145 *
146 * @param matches List of match ids to extend.
147 * @param device_descriptor Device descriptor returned by given device.
148 * @return Error code.
149 */
150int usb_drv_create_match_ids_from_device_descriptor(
151 match_id_list_t *matches,
152 const usb_standard_device_descriptor_t *device_descriptor)
153{
154 int rc;
155
156 /*
157 * Unless the vendor id is 0, the pair idVendor-idProduct
158 * quite uniquely describes the device.
159 */
160 if (device_descriptor->vendor_id != 0) {
161 /* First, with release number. */
162 rc = usb_add_match_id(matches, 100,
163 "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
164 (int) device_descriptor->vendor_id,
165 (int) device_descriptor->product_id,
166 BCD_ARGS(device_descriptor->device_version));
167 if (rc != EOK) {
168 return rc;
169 }
170
171 /* Next, without release number. */
172 rc = usb_add_match_id(matches, 90,
173 "usb&vendor=0x%04x&product=0x%04x",
174 (int) device_descriptor->vendor_id,
175 (int) device_descriptor->product_id);
176 if (rc != EOK) {
177 return rc;
178 }
179 }
180
181 /*
182 * If the device class points to interface we skip adding
183 * class directly.
184 */
185 if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
186 rc = usb_add_match_id(matches, 50, "usb&class=%s",
187 usb_str_class(device_descriptor->device_class));
188 if (rc != EOK) {
189 return rc;
190 }
191 }
192
193 return EOK;
194}
195
196/** Create DDF match ids from USB configuration descriptor.
197 * The configuration descriptor is expected to be in the complete form,
198 * i.e. including interface, endpoint etc. descriptors.
199 *
200 * @param matches List of match ids to extend.
201 * @param config_descriptor Configuration descriptor returned by given device.
202 * @param total_size Size of the @p config_descriptor.
203 * @return Error code.
204 */
205int usb_drv_create_match_ids_from_configuration_descriptor(
206 match_id_list_t *matches,
207 const void *config_descriptor, size_t total_size)
208{
209 /*
210 * Iterate through config descriptor to find the interface
211 * descriptors.
212 */
213 size_t position = sizeof(usb_standard_configuration_descriptor_t);
214 while (position + 1 < total_size) {
215 uint8_t *current_descriptor
216 = ((uint8_t *) config_descriptor) + position;
217 uint8_t cur_descr_len = current_descriptor[0];
218 uint8_t cur_descr_type = current_descriptor[1];
219
220 if (cur_descr_len == 0) {
221 return ENOENT;
222 }
223
224 position += cur_descr_len;
225
226 if (cur_descr_type != USB_DESCTYPE_INTERFACE) {
227 continue;
228 }
229
230 /*
231 * Finally, we found an interface descriptor.
232 */
233 usb_standard_interface_descriptor_t *interface
234 = (usb_standard_interface_descriptor_t *)
235 current_descriptor;
236
237 int rc = usb_add_match_id(matches, 50,
238 "usb&interface&class=%s",
239 usb_str_class(interface->interface_class));
240 if (rc != EOK) {
241 return rc;
242 }
243 }
244
245 return EOK;
246}
247
248/** Add match ids based on configuration descriptor.
249 *
250 * @param pipe Control pipe to the device.
251 * @param matches Match ids list to add matches to.
252 * @param config_count Number of configurations the device has.
253 * @return Error code.
254 */
255static int usb_add_config_descriptor_match_ids(usb_endpoint_pipe_t *pipe,
256 match_id_list_t *matches, int config_count)
257{
258 int final_rc = EOK;
259
260 int config_index;
261 for (config_index = 0; config_index < config_count; config_index++) {
262 int rc;
263 usb_standard_configuration_descriptor_t config_descriptor;
264 rc = usb_request_get_bare_configuration_descriptor(pipe,
265 config_index, &config_descriptor);
266 if (rc != EOK) {
267 final_rc = rc;
268 continue;
269 }
270
271 size_t full_config_descriptor_size;
272 void *full_config_descriptor
273 = malloc(config_descriptor.total_length);
274 rc = usb_request_get_full_configuration_descriptor(pipe,
275 config_index,
276 full_config_descriptor, config_descriptor.total_length,
277 &full_config_descriptor_size);
278 if (rc != EOK) {
279 final_rc = rc;
280 continue;
281 }
282 if (full_config_descriptor_size
283 != config_descriptor.total_length) {
284 final_rc = ERANGE;
285 continue;
286 }
287
288 rc = usb_drv_create_match_ids_from_configuration_descriptor(
289 matches,
290 full_config_descriptor, full_config_descriptor_size);
291 if (rc != EOK) {
292 final_rc = rc;
293 continue;
294 }
295
296 }
297
298 return final_rc;
299}
300
301/** Create match ids describing attached device.
302 *
303 * @warning The list of match ids @p matches may change even when
304 * function exits with error.
305 *
306 * @param ctrl_pipe Control pipe to given device (session must be already
307 * started).
308 * @param matches Initialized list of match ids.
309 * @return Error code.
310 */
311int usb_device_create_match_ids(usb_endpoint_pipe_t *ctrl_pipe,
312 match_id_list_t *matches)
313{
314 int rc;
315 /*
316 * Retrieve device descriptor and add matches from it.
317 */
318 usb_standard_device_descriptor_t device_descriptor;
319
320 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
321 if (rc != EOK) {
322 return rc;
323 }
324
325 rc = usb_drv_create_match_ids_from_device_descriptor(matches,
326 &device_descriptor);
327 if (rc != EOK) {
328 return rc;
329 }
330
331 /*
332 * Go through all configurations and add matches
333 * based on interface class.
334 */
335 rc = usb_add_config_descriptor_match_ids(ctrl_pipe, matches,
336 device_descriptor.configuration_count);
337 if (rc != EOK) {
338 return rc;
339 }
340
341 /*
342 * As a fallback, provide the simplest match id possible.
343 */
344 rc = usb_add_match_id(matches, 1, "usb&fallback");
345 if (rc != EOK) {
346 return rc;
347 }
348
349 return EOK;
350}
351
352/** Probe for device kind and register it in devman.
353 *
354 * @param[in] address Address of the (unknown) attached device.
355 * @param[in] hc_handle Handle of the host controller.
356 * @param[in] parent Parent device.
357 * @param[out] child_handle Handle of the child device.
358 * @return Error code.
359 */
360int usb_device_register_child_in_devman(usb_address_t address,
361 devman_handle_t hc_handle,
362 device_t *parent, devman_handle_t *child_handle)
363{
364 size_t this_device_name_index;
365
366 fibril_mutex_lock(&device_name_index_mutex);
367 this_device_name_index = device_name_index;
368 device_name_index++;
369 fibril_mutex_unlock(&device_name_index_mutex);
370
371 device_t *child = NULL;
372 char *child_name = NULL;
373 int rc;
374 usb_device_connection_t dev_connection;
375 usb_endpoint_pipe_t ctrl_pipe;
376
377 rc = usb_device_connection_initialize(&dev_connection, hc_handle, address);
378 if (rc != EOK) {
379 goto failure;
380 }
381
382 rc = usb_endpoint_pipe_initialize_default_control(&ctrl_pipe,
383 &dev_connection);
384 if (rc != EOK) {
385 goto failure;
386 }
387
388 child = create_device();
389 if (child == NULL) {
390 rc = ENOMEM;
391 goto failure;
392 }
393
394 /*
395 * TODO: Once the device driver framework support persistent
396 * naming etc., something more descriptive could be created.
397 */
398 rc = asprintf(&child_name, "usbdev%02zu", this_device_name_index);
399 if (rc < 0) {
400 goto failure;
401 }
402 child->parent = parent;
403 child->name = child_name;
404 child->ops = &child_ops;
405
406 rc = usb_endpoint_pipe_start_session(&ctrl_pipe);
407 if (rc != EOK) {
408 goto failure;
409 }
410
411 rc = usb_device_create_match_ids(&ctrl_pipe, &child->match_ids);
412 if (rc != EOK) {
413 goto failure;
414 }
415
416 rc = usb_endpoint_pipe_end_session(&ctrl_pipe);
417 if (rc != EOK) {
418 goto failure;
419 }
420
421 rc = child_device_register(child, parent);
422 if (rc != EOK) {
423 goto failure;
424 }
425
426 if (child_handle != NULL) {
427 *child_handle = child->handle;
428 }
429
430 return EOK;
431
432failure:
433 if (child != NULL) {
434 child->name = NULL;
435 /* This takes care of match_id deallocation as well. */
436 delete_device(child);
437 }
438 if (child_name != NULL) {
439 free(child_name);
440 }
441
442 return rc;
443}
444
445
446/**
447 * @}
448 */
Note: See TracBrowser for help on using the repository browser.