source: mainline/uspace/lib/usbdev/src/recognise.c@ faa44e58

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

libusbdev uses include usb/dev

  • Property mode set to 100644
File size: 12.5 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
[a6add7a]33 * Functions for recognition of attached devices.
[02ccfcd]34 */
[17aca1c]35#include <sys/types.h>
[eb1a2f4]36#include <fibril_synch.h>
[7d521e24]37#include <usb/dev/pipes.h>
38#include <usb/dev/recognise.h>
[357a302]39#include <usb/ddfiface.h>
[7d521e24]40#include <usb/dev/request.h>
[02ccfcd]41#include <usb/classes/classes.h>
42#include <stdio.h>
43#include <errno.h>
[eb1a2f4]44#include <assert.h>
[02ccfcd]45
[a6add7a]46/** Index to append after device name for uniqueness. */
[4e8e1f5]47static size_t device_name_index = 0;
[a6add7a]48/** Mutex guard for device_name_index. */
[4e8e1f5]49static FIBRIL_MUTEX_INITIALIZE(device_name_index_mutex);
50
[a6add7a]51/** DDF operations of child devices. */
[eb1a2f4]52ddf_dev_ops_t child_ops = {
[357a302]53 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_child_impl
[71ed4849]54};
[02ccfcd]55
[a6add7a]56/** Get integer part from BCD coded number. */
[02ccfcd]57#define BCD_INT(a) (((unsigned int)(a)) / 256)
[a6add7a]58/** Get fraction part from BCD coded number (as an integer, no less). */
[02ccfcd]59#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
60
[a6add7a]61/** Format for BCD coded number to be used in printf. */
[02ccfcd]62#define BCD_FMT "%x.%x"
[a6add7a]63/** Arguments to printf for BCD coded number. */
[02ccfcd]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
[a6add7a]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 */
[6e6dc7d]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
[540e2d2]138/** Create device match ids based on its interface.
139 *
[bc1c6fb]140 * @param[in] desc_device Device descriptor.
141 * @param[in] desc_interface Interface descriptor.
[540e2d2]142 * @param[out] matches Initialized list of match ids.
143 * @return Error code (the two mentioned are not the only ones).
144 * @retval EINVAL Invalid input parameters (expects non NULL pointers).
[bc1c6fb]145 * @retval ENOENT Device class is not "use interface".
[540e2d2]146 */
147int usb_device_create_match_ids_from_interface(
[51f0e410]148 const usb_standard_device_descriptor_t *desc_device,
149 const usb_standard_interface_descriptor_t *desc_interface,
[540e2d2]150 match_id_list_t *matches)
151{
[51f0e410]152 if (desc_interface == NULL) {
[540e2d2]153 return EINVAL;
154 }
155 if (matches == NULL) {
156 return EINVAL;
157 }
158
[51f0e410]159 if (desc_interface->interface_class == USB_CLASS_USE_INTERFACE) {
[540e2d2]160 return ENOENT;
161 }
162
[51f0e410]163 const char *classname = usb_str_class(desc_interface->interface_class);
[540e2d2]164 assert(classname != NULL);
165
[51f0e410]166#define IFACE_PROTOCOL_FMT "interface&class=%s&subclass=0x%02x&protocol=0x%02x"
167#define IFACE_PROTOCOL_ARGS classname, desc_interface->interface_subclass, \
168 desc_interface->interface_protocol
169
170#define IFACE_SUBCLASS_FMT "interface&class=%s&subclass=0x%02x"
171#define IFACE_SUBCLASS_ARGS classname, desc_interface->interface_subclass
172
173#define IFACE_CLASS_FMT "interface&class=%s"
174#define IFACE_CLASS_ARGS classname
175
176#define VENDOR_RELEASE_FMT "vendor=0x%04x&product=0x%04x&release=" BCD_FMT
177#define VENDOR_RELEASE_ARGS desc_device->vendor_id, desc_device->product_id, \
178 BCD_ARGS(desc_device->device_version)
179
180#define VENDOR_PRODUCT_FMT "vendor=0x%04x&product=0x%04x"
181#define VENDOR_PRODUCT_ARGS desc_device->vendor_id, desc_device->product_id
182
183#define VENDOR_ONLY_FMT "vendor=0x%04x"
184#define VENDOR_ONLY_ARGS desc_device->vendor_id
185
186 /*
187 * If the vendor is specified, create match ids with vendor with
188 * higher score.
189 * Then the same ones without the vendor part.
190 */
191 if ((desc_device != NULL) && (desc_device->vendor_id != 0)) {
192 /* First, interface matches with device release number. */
193 ADD_MATCHID_OR_RETURN(matches, 250,
194 "usb&" VENDOR_RELEASE_FMT "&" IFACE_PROTOCOL_FMT,
195 VENDOR_RELEASE_ARGS, IFACE_PROTOCOL_ARGS);
196 ADD_MATCHID_OR_RETURN(matches, 240,
197 "usb&" VENDOR_RELEASE_FMT "&" IFACE_SUBCLASS_FMT,
198 VENDOR_RELEASE_ARGS, IFACE_SUBCLASS_ARGS);
199 ADD_MATCHID_OR_RETURN(matches, 230,
200 "usb&" VENDOR_RELEASE_FMT "&" IFACE_CLASS_FMT,
201 VENDOR_RELEASE_ARGS, IFACE_CLASS_ARGS);
202
203 /* Next, interface matches without release number. */
204 ADD_MATCHID_OR_RETURN(matches, 220,
205 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_PROTOCOL_FMT,
206 VENDOR_PRODUCT_ARGS, IFACE_PROTOCOL_ARGS);
207 ADD_MATCHID_OR_RETURN(matches, 210,
208 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_SUBCLASS_FMT,
209 VENDOR_PRODUCT_ARGS, IFACE_SUBCLASS_ARGS);
210 ADD_MATCHID_OR_RETURN(matches, 200,
211 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_CLASS_FMT,
212 VENDOR_PRODUCT_ARGS, IFACE_CLASS_ARGS);
213
214 /* Finally, interface matches with only vendor. */
215 ADD_MATCHID_OR_RETURN(matches, 190,
216 "usb&" VENDOR_ONLY_FMT "&" IFACE_PROTOCOL_FMT,
217 VENDOR_ONLY_ARGS, IFACE_PROTOCOL_ARGS);
218 ADD_MATCHID_OR_RETURN(matches, 180,
219 "usb&" VENDOR_ONLY_FMT "&" IFACE_SUBCLASS_FMT,
220 VENDOR_ONLY_ARGS, IFACE_SUBCLASS_ARGS);
221 ADD_MATCHID_OR_RETURN(matches, 170,
222 "usb&" VENDOR_ONLY_FMT "&" IFACE_CLASS_FMT,
223 VENDOR_ONLY_ARGS, IFACE_CLASS_ARGS);
224 }
225
226 /* Now, the same but without any vendor specification. */
227 ADD_MATCHID_OR_RETURN(matches, 160,
228 "usb&" IFACE_PROTOCOL_FMT,
229 IFACE_PROTOCOL_ARGS);
230 ADD_MATCHID_OR_RETURN(matches, 150,
231 "usb&" IFACE_SUBCLASS_FMT,
232 IFACE_SUBCLASS_ARGS);
233 ADD_MATCHID_OR_RETURN(matches, 140,
234 "usb&" IFACE_CLASS_FMT,
235 IFACE_CLASS_ARGS);
236
237#undef IFACE_PROTOCOL_FMT
238#undef IFACE_PROTOCOL_ARGS
239#undef IFACE_SUBCLASS_FMT
240#undef IFACE_SUBCLASS_ARGS
241#undef IFACE_CLASS_FMT
242#undef IFACE_CLASS_ARGS
243#undef VENDOR_RELEASE_FMT
244#undef VENDOR_RELEASE_ARGS
245#undef VENDOR_PRODUCT_FMT
246#undef VENDOR_PRODUCT_ARGS
247#undef VENDOR_ONLY_FMT
248#undef VENDOR_ONLY_ARGS
[540e2d2]249
[2211125e]250 /* As a last resort, try fallback driver. */
251 ADD_MATCHID_OR_RETURN(matches, 10, "usb&interface&fallback");
252
[6e6dc7d]253 return EOK;
[540e2d2]254}
255
[692c0d3e]256/** Create DDF match ids from USB device descriptor.
257 *
258 * @param matches List of match ids to extend.
259 * @param device_descriptor Device descriptor returned by given device.
260 * @return Error code.
261 */
[0f21c0c]262int usb_device_create_match_ids_from_device_descriptor(
263 const usb_standard_device_descriptor_t *device_descriptor,
264 match_id_list_t *matches)
[692c0d3e]265{
266 /*
267 * Unless the vendor id is 0, the pair idVendor-idProduct
268 * quite uniquely describes the device.
269 */
270 if (device_descriptor->vendor_id != 0) {
271 /* First, with release number. */
[6e6dc7d]272 ADD_MATCHID_OR_RETURN(matches, 100,
[345ea18]273 "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
[692c0d3e]274 (int) device_descriptor->vendor_id,
275 (int) device_descriptor->product_id,
276 BCD_ARGS(device_descriptor->device_version));
277
278 /* Next, without release number. */
[6e6dc7d]279 ADD_MATCHID_OR_RETURN(matches, 90,
[345ea18]280 "usb&vendor=0x%04x&product=0x%04x",
[692c0d3e]281 (int) device_descriptor->vendor_id,
282 (int) device_descriptor->product_id);
283 }
284
285 /*
286 * If the device class points to interface we skip adding
[3ae93a8]287 * class directly but we add a multi interface device.
[692c0d3e]288 */
289 if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
[6e6dc7d]290 ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
[692c0d3e]291 usb_str_class(device_descriptor->device_class));
[3ae93a8]292 } else {
[6e6dc7d]293 ADD_MATCHID_OR_RETURN(matches, 50, "usb&mid");
[692c0d3e]294 }
295
[93855d4]296 /* As a last resort, try fallback driver. */
297 ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
298
[692c0d3e]299 return EOK;
300}
301
[a66225f3]302
[02ccfcd]303/** Create match ids describing attached device.
304 *
305 * @warning The list of match ids @p matches may change even when
306 * function exits with error.
307 *
[4e8e1f5]308 * @param ctrl_pipe Control pipe to given device (session must be already
309 * started).
[02ccfcd]310 * @param matches Initialized list of match ids.
311 * @return Error code.
312 */
[a372663]313int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
[4e8e1f5]314 match_id_list_t *matches)
[02ccfcd]315{
316 int rc;
[692c0d3e]317 /*
318 * Retrieve device descriptor and add matches from it.
319 */
[02ccfcd]320 usb_standard_device_descriptor_t device_descriptor;
321
[4e8e1f5]322 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
[02ccfcd]323 if (rc != EOK) {
324 return rc;
325 }
[4e8e1f5]326
[0f21c0c]327 rc = usb_device_create_match_ids_from_device_descriptor(
328 &device_descriptor, matches);
[692c0d3e]329 if (rc != EOK) {
330 return rc;
[a66225f3]331 }
[4e8e1f5]332
[02ccfcd]333 return EOK;
334}
335
336/** Probe for device kind and register it in devman.
337 *
[3b77628]338 * @param[in] address Address of the (unknown) attached device.
[4e8e1f5]339 * @param[in] hc_handle Handle of the host controller.
340 * @param[in] parent Parent device.
[3b77628]341 * @param[out] child_handle Handle of the child device.
[bc1c6fb]342 * @param[in] dev_ops Child device ops.
343 * @param[in] dev_data Arbitrary pointer to be stored in the child
344 * as @c driver_data.
345 * @param[out] child_fun Storage where pointer to allocated child function
346 * will be written.
[02ccfcd]347 * @return Error code.
348 */
[4e8e1f5]349int usb_device_register_child_in_devman(usb_address_t address,
350 devman_handle_t hc_handle,
[eb1a2f4]351 ddf_dev_t *parent, devman_handle_t *child_handle,
352 ddf_dev_ops_t *dev_ops, void *dev_data, ddf_fun_t **child_fun)
[02ccfcd]353{
[38648f0]354 size_t this_device_name_index;
355
356 fibril_mutex_lock(&device_name_index_mutex);
357 this_device_name_index = device_name_index;
358 device_name_index++;
359 fibril_mutex_unlock(&device_name_index_mutex);
360
[eb1a2f4]361 ddf_fun_t *child = NULL;
[7ed5b576]362 char *child_name = NULL;
363 int rc;
[4e8e1f5]364 usb_device_connection_t dev_connection;
[a372663]365 usb_pipe_t ctrl_pipe;
[4e8e1f5]366
367 rc = usb_device_connection_initialize(&dev_connection, hc_handle, address);
368 if (rc != EOK) {
369 goto failure;
370 }
371
[3954a63b]372 rc = usb_pipe_initialize_default_control(&ctrl_pipe,
[4e8e1f5]373 &dev_connection);
374 if (rc != EOK) {
375 goto failure;
376 }
[3954a63b]377 rc = usb_pipe_probe_default_control(&ctrl_pipe);
[206f71a]378 if (rc != EOK) {
379 goto failure;
380 }
[7ed5b576]381
382 /*
[b207803]383 * TODO: Once the device driver framework support persistent
384 * naming etc., something more descriptive could be created.
[7ed5b576]385 */
[189cd4e]386 rc = asprintf(&child_name, "usb%02zu_a%d",
387 this_device_name_index, address);
[7ed5b576]388 if (rc < 0) {
389 goto failure;
390 }
[eb1a2f4]391
392 child = ddf_fun_create(parent, fun_inner, child_name);
393 if (child == NULL) {
394 rc = ENOMEM;
395 goto failure;
396 }
397
398 if (dev_ops != NULL) {
399 child->ops = dev_ops;
400 } else {
401 child->ops = &child_ops;
402 }
403
404 child->driver_data = dev_data;
[4e8e1f5]405
406 rc = usb_device_create_match_ids(&ctrl_pipe, &child->match_ids);
407 if (rc != EOK) {
408 goto failure;
409 }
410
[eb1a2f4]411 rc = ddf_fun_bind(child);
[7ed5b576]412 if (rc != EOK) {
413 goto failure;
414 }
415
416 if (child_handle != NULL) {
417 *child_handle = child->handle;
418 }
[4e8e1f5]419
[eb1a2f4]420 if (child_fun != NULL) {
421 *child_fun = child;
422 }
423
[7ed5b576]424 return EOK;
425
426failure:
427 if (child != NULL) {
428 child->name = NULL;
429 /* This takes care of match_id deallocation as well. */
[eb1a2f4]430 ddf_fun_destroy(child);
[7ed5b576]431 }
432 if (child_name != NULL) {
433 free(child_name);
434 }
435
436 return rc;
[02ccfcd]437}
[7ed5b576]438
[02ccfcd]439
440/**
441 * @}
442 */
Note: See TracBrowser for help on using the repository browser.