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

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

Merge development/ changes

  • 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=%d&product=%d&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, "usb&vendor=%d&product=%d",
173 (int) device_descriptor->vendor_id,
174 (int) device_descriptor->product_id);
175 if (rc != EOK) {
176 return rc;
177 }
178 }
179
180 /*
181 * If the device class points to interface we skip adding
182 * class directly.
183 */
184 if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
185 rc = usb_add_match_id(matches, 50, "usb&class=%s",
186 usb_str_class(device_descriptor->device_class));
187 if (rc != EOK) {
188 return rc;
189 }
190 }
191
192 return EOK;
193}
194
195/** Create DDF match ids from USB configuration descriptor.
196 * The configuration descriptor is expected to be in the complete form,
197 * i.e. including interface, endpoint etc. descriptors.
198 *
199 * @param matches List of match ids to extend.
200 * @param config_descriptor Configuration descriptor returned by given device.
201 * @param total_size Size of the @p config_descriptor.
202 * @return Error code.
203 */
204int usb_drv_create_match_ids_from_configuration_descriptor(
205 match_id_list_t *matches,
206 const void *config_descriptor, size_t total_size)
207{
208 /*
209 * Iterate through config descriptor to find the interface
210 * descriptors.
211 */
212 size_t position = sizeof(usb_standard_configuration_descriptor_t);
213 while (position + 1 < total_size) {
214 uint8_t *current_descriptor
215 = ((uint8_t *) config_descriptor) + position;
216 uint8_t cur_descr_len = current_descriptor[0];
217 uint8_t cur_descr_type = current_descriptor[1];
218
219 if (cur_descr_len == 0) {
220 return ENOENT;
221 }
222
223 position += cur_descr_len;
224
225 if (cur_descr_type != USB_DESCTYPE_INTERFACE) {
226 continue;
227 }
228
229 /*
230 * Finally, we found an interface descriptor.
231 */
232 usb_standard_interface_descriptor_t *interface
233 = (usb_standard_interface_descriptor_t *)
234 current_descriptor;
235
236 int rc = usb_add_match_id(matches, 50,
237 "usb&interface&class=%s",
238 usb_str_class(interface->interface_class));
239 if (rc != EOK) {
240 return rc;
241 }
242 }
243
244 return EOK;
245}
246
247/** Add match ids based on configuration descriptor.
248 *
249 * @param pipe Control pipe to the device.
250 * @param matches Match ids list to add matches to.
251 * @param config_count Number of configurations the device has.
252 * @return Error code.
253 */
254static int usb_add_config_descriptor_match_ids(usb_endpoint_pipe_t *pipe,
255 match_id_list_t *matches, int config_count)
256{
257 int final_rc = EOK;
258
259 int config_index;
260 for (config_index = 0; config_index < config_count; config_index++) {
261 int rc;
262 usb_standard_configuration_descriptor_t config_descriptor;
263 rc = usb_request_get_bare_configuration_descriptor(pipe,
264 config_index, &config_descriptor);
265 if (rc != EOK) {
266 final_rc = rc;
267 continue;
268 }
269
270 size_t full_config_descriptor_size;
271 void *full_config_descriptor
272 = malloc(config_descriptor.total_length);
273 rc = usb_request_get_full_configuration_descriptor(pipe,
274 config_index,
275 full_config_descriptor, config_descriptor.total_length,
276 &full_config_descriptor_size);
277 if (rc != EOK) {
278 final_rc = rc;
279 continue;
280 }
281 if (full_config_descriptor_size
282 != config_descriptor.total_length) {
283 final_rc = ERANGE;
284 continue;
285 }
286
287 rc = usb_drv_create_match_ids_from_configuration_descriptor(
288 matches,
289 full_config_descriptor, full_config_descriptor_size);
290 if (rc != EOK) {
291 final_rc = rc;
292 continue;
293 }
294
295 }
296
297 return final_rc;
298}
299
300/** Create match ids describing attached device.
301 *
302 * @warning The list of match ids @p matches may change even when
303 * function exits with error.
304 *
305 * @param ctrl_pipe Control pipe to given device (session must be already
306 * started).
307 * @param matches Initialized list of match ids.
308 * @return Error code.
309 */
310int usb_device_create_match_ids(usb_endpoint_pipe_t *ctrl_pipe,
311 match_id_list_t *matches)
312{
313 int rc;
314 /*
315 * Retrieve device descriptor and add matches from it.
316 */
317 usb_standard_device_descriptor_t device_descriptor;
318
319 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
320 if (rc != EOK) {
321 return rc;
322 }
323
324 rc = usb_drv_create_match_ids_from_device_descriptor(matches,
325 &device_descriptor);
326 if (rc != EOK) {
327 return rc;
328 }
329
330 /*
331 * Go through all configurations and add matches
332 * based on interface class.
333 */
334 rc = usb_add_config_descriptor_match_ids(ctrl_pipe, matches,
335 device_descriptor.configuration_count);
336 if (rc != EOK) {
337 return rc;
338 }
339
340 /*
341 * As a fallback, provide the simplest match id possible.
342 */
343 rc = usb_add_match_id(matches, 1, "usb&fallback");
344 if (rc != EOK) {
345 return rc;
346 }
347
348 return EOK;
349}
350
351/** Probe for device kind and register it in devman.
352 *
353 * @param[in] address Address of the (unknown) attached device.
354 * @param[in] hc_handle Handle of the host controller.
355 * @param[in] parent Parent device.
356 * @param[out] child_handle Handle of the child device.
357 * @return Error code.
358 */
359int usb_device_register_child_in_devman(usb_address_t address,
360 devman_handle_t hc_handle,
361 device_t *parent, devman_handle_t *child_handle)
362{
363 size_t this_device_name_index;
364
365 fibril_mutex_lock(&device_name_index_mutex);
366 this_device_name_index = device_name_index;
367 device_name_index++;
368 fibril_mutex_unlock(&device_name_index_mutex);
369
370 device_t *child = NULL;
371 char *child_name = NULL;
372 int rc;
373 usb_device_connection_t dev_connection;
374 usb_endpoint_pipe_t ctrl_pipe;
375
376 rc = usb_device_connection_initialize(&dev_connection, hc_handle, address);
377 if (rc != EOK) {
378 goto failure;
379 }
380
381 rc = usb_endpoint_pipe_initialize_default_control(&ctrl_pipe,
382 &dev_connection);
383 if (rc != EOK) {
384 goto failure;
385 }
386
387 child = create_device();
388 if (child == NULL) {
389 rc = ENOMEM;
390 goto failure;
391 }
392
393 /*
394 * TODO: Once the device driver framework support persistent
395 * naming etc., something more descriptive could be created.
396 */
397 rc = asprintf(&child_name, "usbdev%02zu", this_device_name_index);
398 if (rc < 0) {
399 goto failure;
400 }
401 child->parent = parent;
402 child->name = child_name;
403 child->ops = &child_ops;
404
405 rc = usb_endpoint_pipe_start_session(&ctrl_pipe);
406 if (rc != EOK) {
407 goto failure;
408 }
409
410 rc = usb_device_create_match_ids(&ctrl_pipe, &child->match_ids);
411 if (rc != EOK) {
412 goto failure;
413 }
414
415 rc = usb_endpoint_pipe_end_session(&ctrl_pipe);
416 if (rc != EOK) {
417 goto failure;
418 }
419
420 rc = child_device_register(child, parent);
421 if (rc != EOK) {
422 goto failure;
423 }
424
425 if (child_handle != NULL) {
426 *child_handle = child->handle;
427 }
428
429 return EOK;
430
431failure:
432 if (child != NULL) {
433 child->name = NULL;
434 /* This takes care of match_id deallocation as well. */
435 delete_device(child);
436 }
437 if (child_name != NULL) {
438 free(child_name);
439 }
440
441 return rc;
442}
443
444
445/**
446 * @}
447 */
Note: See TracBrowser for help on using the repository browser.