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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e5dabf was 6e3c005, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbdev: Doxygen and minor cleanup/renaming.

  • Property mode set to 100644
File size: 11.9 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
[160b75e]29/** @addtogroup libusbdev
[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>
[59c163c]37#include <usb/debug.h>
38#include <usb/dev/hub.h>
[7d521e24]39#include <usb/dev/pipes.h>
40#include <usb/dev/recognise.h>
[357a302]41#include <usb/ddfiface.h>
[7d521e24]42#include <usb/dev/request.h>
[02ccfcd]43#include <usb/classes/classes.h>
44#include <stdio.h>
45#include <errno.h>
[eb1a2f4]46#include <assert.h>
[02ccfcd]47
[a6add7a]48/** DDF operations of child devices. */
[eea3e39]49static ddf_dev_ops_t child_ops = {
[357a302]50 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_child_impl
[71ed4849]51};
[02ccfcd]52
[a6add7a]53/** Get integer part from BCD coded number. */
[02ccfcd]54#define BCD_INT(a) (((unsigned int)(a)) / 256)
[a6add7a]55/** Get fraction part from BCD coded number (as an integer, no less). */
[02ccfcd]56#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
57
[a6add7a]58/** Format for BCD coded number to be used in printf. */
[02ccfcd]59#define BCD_FMT "%x.%x"
[a6add7a]60/** Arguments to printf for BCD coded number. */
[02ccfcd]61#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
62
63/** Add formatted match id.
64 *
65 * @param matches List of match ids where to add to.
66 * @param score Score of the match.
67 * @param format Printf-like format
68 * @return Error code.
69 */
70static int usb_add_match_id(match_id_list_t *matches, int score,
[37e4025]71 const char *match_str)
[02ccfcd]72{
[37e4025]73 assert(matches);
[02ccfcd]74
[37e4025]75 match_id_t *match_id = create_match_id();
[02ccfcd]76 if (match_id == NULL) {
[37e4025]77 return ENOMEM;
[02ccfcd]78 }
79
80 match_id->id = match_str;
81 match_id->score = score;
82 add_match_id(matches, match_id);
83
84 return EOK;
85}
86
[a6add7a]87/** Add match id to list or return with error code.
88 *
89 * @param match_ids List of match ids.
90 * @param score Match id score.
91 * @param format Format of the matching string
92 * @param ... Arguments for the format.
93 */
[6e6dc7d]94#define ADD_MATCHID_OR_RETURN(match_ids, score, format, ...) \
95 do { \
[37e4025]96 char *str = NULL; \
97 int __rc = asprintf(&str, format, ##__VA_ARGS__); \
98 if (__rc > 0) { \
99 __rc = usb_add_match_id((match_ids), (score), str); \
100 } \
[6e6dc7d]101 if (__rc != EOK) { \
[37e4025]102 free(str); \
[6e6dc7d]103 return __rc; \
104 } \
105 } while (0)
106
[540e2d2]107/** Create device match ids based on its interface.
108 *
[bc1c6fb]109 * @param[in] desc_device Device descriptor.
110 * @param[in] desc_interface Interface descriptor.
[540e2d2]111 * @param[out] matches Initialized list of match ids.
112 * @return Error code (the two mentioned are not the only ones).
113 * @retval EINVAL Invalid input parameters (expects non NULL pointers).
[bc1c6fb]114 * @retval ENOENT Device class is not "use interface".
[540e2d2]115 */
116int usb_device_create_match_ids_from_interface(
[51f0e410]117 const usb_standard_device_descriptor_t *desc_device,
118 const usb_standard_interface_descriptor_t *desc_interface,
[540e2d2]119 match_id_list_t *matches)
120{
[37e4025]121 if (desc_interface == NULL || matches == NULL) {
[540e2d2]122 return EINVAL;
123 }
124
[51f0e410]125 if (desc_interface->interface_class == USB_CLASS_USE_INTERFACE) {
[540e2d2]126 return ENOENT;
127 }
128
[51f0e410]129 const char *classname = usb_str_class(desc_interface->interface_class);
[540e2d2]130 assert(classname != NULL);
131
[51f0e410]132#define IFACE_PROTOCOL_FMT "interface&class=%s&subclass=0x%02x&protocol=0x%02x"
133#define IFACE_PROTOCOL_ARGS classname, desc_interface->interface_subclass, \
134 desc_interface->interface_protocol
135
136#define IFACE_SUBCLASS_FMT "interface&class=%s&subclass=0x%02x"
137#define IFACE_SUBCLASS_ARGS classname, desc_interface->interface_subclass
138
139#define IFACE_CLASS_FMT "interface&class=%s"
140#define IFACE_CLASS_ARGS classname
141
142#define VENDOR_RELEASE_FMT "vendor=0x%04x&product=0x%04x&release=" BCD_FMT
143#define VENDOR_RELEASE_ARGS desc_device->vendor_id, desc_device->product_id, \
144 BCD_ARGS(desc_device->device_version)
145
146#define VENDOR_PRODUCT_FMT "vendor=0x%04x&product=0x%04x"
147#define VENDOR_PRODUCT_ARGS desc_device->vendor_id, desc_device->product_id
148
149#define VENDOR_ONLY_FMT "vendor=0x%04x"
150#define VENDOR_ONLY_ARGS desc_device->vendor_id
151
152 /*
153 * If the vendor is specified, create match ids with vendor with
154 * higher score.
155 * Then the same ones without the vendor part.
156 */
157 if ((desc_device != NULL) && (desc_device->vendor_id != 0)) {
158 /* First, interface matches with device release number. */
159 ADD_MATCHID_OR_RETURN(matches, 250,
160 "usb&" VENDOR_RELEASE_FMT "&" IFACE_PROTOCOL_FMT,
161 VENDOR_RELEASE_ARGS, IFACE_PROTOCOL_ARGS);
162 ADD_MATCHID_OR_RETURN(matches, 240,
163 "usb&" VENDOR_RELEASE_FMT "&" IFACE_SUBCLASS_FMT,
164 VENDOR_RELEASE_ARGS, IFACE_SUBCLASS_ARGS);
165 ADD_MATCHID_OR_RETURN(matches, 230,
166 "usb&" VENDOR_RELEASE_FMT "&" IFACE_CLASS_FMT,
167 VENDOR_RELEASE_ARGS, IFACE_CLASS_ARGS);
168
169 /* Next, interface matches without release number. */
170 ADD_MATCHID_OR_RETURN(matches, 220,
171 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_PROTOCOL_FMT,
172 VENDOR_PRODUCT_ARGS, IFACE_PROTOCOL_ARGS);
173 ADD_MATCHID_OR_RETURN(matches, 210,
174 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_SUBCLASS_FMT,
175 VENDOR_PRODUCT_ARGS, IFACE_SUBCLASS_ARGS);
176 ADD_MATCHID_OR_RETURN(matches, 200,
177 "usb&" VENDOR_PRODUCT_FMT "&" IFACE_CLASS_FMT,
178 VENDOR_PRODUCT_ARGS, IFACE_CLASS_ARGS);
179
180 /* Finally, interface matches with only vendor. */
181 ADD_MATCHID_OR_RETURN(matches, 190,
182 "usb&" VENDOR_ONLY_FMT "&" IFACE_PROTOCOL_FMT,
183 VENDOR_ONLY_ARGS, IFACE_PROTOCOL_ARGS);
184 ADD_MATCHID_OR_RETURN(matches, 180,
185 "usb&" VENDOR_ONLY_FMT "&" IFACE_SUBCLASS_FMT,
186 VENDOR_ONLY_ARGS, IFACE_SUBCLASS_ARGS);
187 ADD_MATCHID_OR_RETURN(matches, 170,
188 "usb&" VENDOR_ONLY_FMT "&" IFACE_CLASS_FMT,
189 VENDOR_ONLY_ARGS, IFACE_CLASS_ARGS);
190 }
191
192 /* Now, the same but without any vendor specification. */
193 ADD_MATCHID_OR_RETURN(matches, 160,
194 "usb&" IFACE_PROTOCOL_FMT,
195 IFACE_PROTOCOL_ARGS);
196 ADD_MATCHID_OR_RETURN(matches, 150,
197 "usb&" IFACE_SUBCLASS_FMT,
198 IFACE_SUBCLASS_ARGS);
199 ADD_MATCHID_OR_RETURN(matches, 140,
200 "usb&" IFACE_CLASS_FMT,
201 IFACE_CLASS_ARGS);
202
203#undef IFACE_PROTOCOL_FMT
204#undef IFACE_PROTOCOL_ARGS
205#undef IFACE_SUBCLASS_FMT
206#undef IFACE_SUBCLASS_ARGS
207#undef IFACE_CLASS_FMT
208#undef IFACE_CLASS_ARGS
209#undef VENDOR_RELEASE_FMT
210#undef VENDOR_RELEASE_ARGS
211#undef VENDOR_PRODUCT_FMT
212#undef VENDOR_PRODUCT_ARGS
213#undef VENDOR_ONLY_FMT
214#undef VENDOR_ONLY_ARGS
[540e2d2]215
[2211125e]216 /* As a last resort, try fallback driver. */
217 ADD_MATCHID_OR_RETURN(matches, 10, "usb&interface&fallback");
218
[6e6dc7d]219 return EOK;
[540e2d2]220}
221
[692c0d3e]222/** Create DDF match ids from USB device descriptor.
223 *
224 * @param matches List of match ids to extend.
225 * @param device_descriptor Device descriptor returned by given device.
226 * @return Error code.
227 */
[0f21c0c]228int usb_device_create_match_ids_from_device_descriptor(
229 const usb_standard_device_descriptor_t *device_descriptor,
230 match_id_list_t *matches)
[692c0d3e]231{
232 /*
233 * Unless the vendor id is 0, the pair idVendor-idProduct
234 * quite uniquely describes the device.
235 */
236 if (device_descriptor->vendor_id != 0) {
237 /* First, with release number. */
[6e6dc7d]238 ADD_MATCHID_OR_RETURN(matches, 100,
[345ea18]239 "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
[692c0d3e]240 (int) device_descriptor->vendor_id,
241 (int) device_descriptor->product_id,
242 BCD_ARGS(device_descriptor->device_version));
243
244 /* Next, without release number. */
[6e6dc7d]245 ADD_MATCHID_OR_RETURN(matches, 90,
[345ea18]246 "usb&vendor=0x%04x&product=0x%04x",
[692c0d3e]247 (int) device_descriptor->vendor_id,
248 (int) device_descriptor->product_id);
249 }
250
251 /*
252 * If the device class points to interface we skip adding
[3ae93a8]253 * class directly but we add a multi interface device.
[692c0d3e]254 */
255 if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
[6e6dc7d]256 ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
[692c0d3e]257 usb_str_class(device_descriptor->device_class));
[3ae93a8]258 } else {
[6e6dc7d]259 ADD_MATCHID_OR_RETURN(matches, 50, "usb&mid");
[692c0d3e]260 }
261
[93855d4]262 /* As a last resort, try fallback driver. */
263 ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
264
[692c0d3e]265 return EOK;
266}
267
[a66225f3]268
[02ccfcd]269/** Create match ids describing attached device.
270 *
271 * @warning The list of match ids @p matches may change even when
272 * function exits with error.
273 *
[4e8e1f5]274 * @param ctrl_pipe Control pipe to given device (session must be already
275 * started).
[02ccfcd]276 * @param matches Initialized list of match ids.
277 * @return Error code.
278 */
[a372663]279int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
[4e8e1f5]280 match_id_list_t *matches)
[02ccfcd]281{
[2179cf95]282 assert(ctrl_pipe);
[02ccfcd]283 int rc;
[692c0d3e]284 /*
285 * Retrieve device descriptor and add matches from it.
286 */
[02ccfcd]287 usb_standard_device_descriptor_t device_descriptor;
288
[4e8e1f5]289 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
[02ccfcd]290 if (rc != EOK) {
291 return rc;
292 }
[4e8e1f5]293
[0f21c0c]294 rc = usb_device_create_match_ids_from_device_descriptor(
295 &device_descriptor, matches);
[692c0d3e]296 if (rc != EOK) {
297 return rc;
[a66225f3]298 }
[4e8e1f5]299
[02ccfcd]300 return EOK;
301}
302
303/** Probe for device kind and register it in devman.
304 *
[2179cf95]305 * @param[in] ctrl_pipe Control pipe to the device.
[4e8e1f5]306 * @param[in] parent Parent device.
[eea3e39]307 * @param[in] dev_ops Child device ops. Default child_ops will be used if NULL.
[bc1c6fb]308 * @param[in] dev_data Arbitrary pointer to be stored in the child
309 * as @c driver_data.
310 * @param[out] child_fun Storage where pointer to allocated child function
311 * will be written.
[02ccfcd]312 * @return Error code.
313 */
[2179cf95]314int usb_device_register_child_in_devman(usb_pipe_t *ctrl_pipe,
315 ddf_dev_t *parent, ddf_dev_ops_t *dev_ops, void *dev_data,
316 ddf_fun_t **child_fun)
[02ccfcd]317{
[2179cf95]318 if (child_fun == NULL || ctrl_pipe == NULL)
[ae754e5f]319 return EINVAL;
320
[59c163c]321 if (!dev_ops && dev_data) {
322 usb_log_warning("Using standard fun ops with arbitrary "
323 "driver data. This does not have to work.\n");
324 }
325
[c046942]326 /** Index to append after device name for uniqueness. */
327 static atomic_t device_name_index = {0};
328 const size_t this_device_name_index =
329 (size_t) atomic_preinc(&device_name_index);
[38648f0]330
[eb1a2f4]331 ddf_fun_t *child = NULL;
[7ed5b576]332 int rc;
333
334 /*
[b207803]335 * TODO: Once the device driver framework support persistent
336 * naming etc., something more descriptive could be created.
[7ed5b576]337 */
[2179cf95]338 char child_name[12]; /* The format is: "usbAB_aXYZ", length 11 */
339 rc = snprintf(child_name, sizeof(child_name),
340 "usb%02zu_a%d", this_device_name_index, ctrl_pipe->wire->address);
[7ed5b576]341 if (rc < 0) {
342 goto failure;
343 }
[eb1a2f4]344
345 child = ddf_fun_create(parent, fun_inner, child_name);
346 if (child == NULL) {
347 rc = ENOMEM;
348 goto failure;
349 }
350
351 if (dev_ops != NULL) {
352 child->ops = dev_ops;
353 } else {
354 child->ops = &child_ops;
355 }
356
357 child->driver_data = dev_data;
[59c163c]358 /* Store the attached device in fun driver data if there is no
359 * other data */
360 if (!dev_data) {
361 usb_hub_attached_device_t *new_device = ddf_fun_data_alloc(
362 child, sizeof(usb_hub_attached_device_t));
363 if (!new_device) {
364 rc = ENOMEM;
365 goto failure;
366 }
[2179cf95]367 new_device->address = ctrl_pipe->wire->address;
[59c163c]368 new_device->fun = child;
369 }
370
[4e8e1f5]371
[2179cf95]372 rc = usb_device_create_match_ids(ctrl_pipe, &child->match_ids);
[4e8e1f5]373 if (rc != EOK) {
374 goto failure;
375 }
376
[eb1a2f4]377 rc = ddf_fun_bind(child);
[7ed5b576]378 if (rc != EOK) {
379 goto failure;
380 }
381
[ae754e5f]382 *child_fun = child;
[7ed5b576]383 return EOK;
384
385failure:
386 if (child != NULL) {
[59c163c]387 /* We know nothing about the data if it came from outside. */
388 if (dev_data) {
389 child->driver_data = NULL;
390 }
[7ed5b576]391 /* This takes care of match_id deallocation as well. */
[eb1a2f4]392 ddf_fun_destroy(child);
[7ed5b576]393 }
394
395 return rc;
[02ccfcd]396}
[7ed5b576]397
[02ccfcd]398
399/**
400 * @}
401 */
Note: See TracBrowser for help on using the repository browser.