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

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

Merge mainline changes (DDF refactoring)

This merge includes DDF refactoring that brought multifunctional devices
(i.e. ddf_dev_t and ddf_fun_t). Please, see ticket #295 at HelenOS
upstream Trac.

The conflicts themselves were easy to solve (merely several renamings).

Changes to USB subsystem:

  • drivers uses ddf_dev_t and ddf_fun_t
  • different signatures of many library functions
  • several hacks around communication with parent device (now the communication is clearer and somehow what we have now is hack about other hacks)
    • will repair and clean later
  • maybe added some extra debugging messages (the diff has about 240K, and I admit I have no energy to double check that)

WARNING:

  • the diff is VERY long, recommended is viewing partial diffs of the merge (i.e. merges in mainline branch that lead to the parent one)
  • merging with your branches might involve huge renamings, sorry, no other way is possible

BUGS:

  • hub driver will not work (no function created)

GOOD NEWS:

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