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

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

Shorter device names

Automatic names of USB devices in devman uses counter instead of
pointer (see USB Trac, ticket #37).

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