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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 64e3dad 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
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 libusbdev
30 * @{
31 */
32/** @file
33 * Functions for recognition of attached devices.
34 */
35#include <sys/types.h>
36#include <fibril_synch.h>
37#include <usb/debug.h>
38#include <usb/dev/hub.h>
39#include <usb/dev/pipes.h>
40#include <usb/dev/recognise.h>
41#include <usb/ddfiface.h>
42#include <usb/dev/request.h>
43#include <usb/classes/classes.h>
44#include <stdio.h>
45#include <errno.h>
46#include <assert.h>
47
48/** DDF operations of child devices. */
49static ddf_dev_ops_t child_ops = {
50 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_child_impl
51};
52
53/** Get integer part from BCD coded number. */
54#define BCD_INT(a) (((unsigned int)(a)) / 256)
55/** Get fraction part from BCD coded number (as an integer, no less). */
56#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
57
58/** Format for BCD coded number to be used in printf. */
59#define BCD_FMT "%x.%x"
60/** Arguments to printf for BCD coded number. */
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,
71 const char *match_str)
72{
73 assert(matches);
74
75 match_id_t *match_id = create_match_id();
76 if (match_id == NULL) {
77 return ENOMEM;
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
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 */
94#define ADD_MATCHID_OR_RETURN(match_ids, score, format, ...) \
95 do { \
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 } \
101 if (__rc != EOK) { \
102 free(str); \
103 return __rc; \
104 } \
105 } while (0)
106
107/** Create device match ids based on its interface.
108 *
109 * @param[in] desc_device Device descriptor.
110 * @param[in] desc_interface Interface descriptor.
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).
114 * @retval ENOENT Device class is not "use interface".
115 */
116int usb_device_create_match_ids_from_interface(
117 const usb_standard_device_descriptor_t *desc_device,
118 const usb_standard_interface_descriptor_t *desc_interface,
119 match_id_list_t *matches)
120{
121 if (desc_interface == NULL || matches == NULL) {
122 return EINVAL;
123 }
124
125 if (desc_interface->interface_class == USB_CLASS_USE_INTERFACE) {
126 return ENOENT;
127 }
128
129 const char *classname = usb_str_class(desc_interface->interface_class);
130 assert(classname != NULL);
131
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
215
216 /* As a last resort, try fallback driver. */
217 ADD_MATCHID_OR_RETURN(matches, 10, "usb&interface&fallback");
218
219 return EOK;
220}
221
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 */
228int usb_device_create_match_ids_from_device_descriptor(
229 const usb_standard_device_descriptor_t *device_descriptor,
230 match_id_list_t *matches)
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. */
238 ADD_MATCHID_OR_RETURN(matches, 100,
239 "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
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. */
245 ADD_MATCHID_OR_RETURN(matches, 90,
246 "usb&vendor=0x%04x&product=0x%04x",
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
253 * class directly but we add a multi interface device.
254 */
255 if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
256 ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
257 usb_str_class(device_descriptor->device_class));
258 } else {
259 ADD_MATCHID_OR_RETURN(matches, 50, "usb&mid");
260 }
261
262 /* As a last resort, try fallback driver. */
263 ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
264
265 return EOK;
266}
267
268
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 *
274 * @param ctrl_pipe Control pipe to given device (session must be already
275 * started).
276 * @param matches Initialized list of match ids.
277 * @return Error code.
278 */
279int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
280 match_id_list_t *matches)
281{
282 assert(ctrl_pipe);
283 int rc;
284 /*
285 * Retrieve device descriptor and add matches from it.
286 */
287 usb_standard_device_descriptor_t device_descriptor;
288
289 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
290 if (rc != EOK) {
291 return rc;
292 }
293
294 rc = usb_device_create_match_ids_from_device_descriptor(
295 &device_descriptor, matches);
296 if (rc != EOK) {
297 return rc;
298 }
299
300 return EOK;
301}
302
303/** Probe for device kind and register it in devman.
304 *
305 * @param[in] ctrl_pipe Control pipe to the device.
306 * @param[in] parent Parent device.
307 * @param[in] dev_ops Child device ops. Default child_ops will be used if NULL.
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.
312 * @return Error code.
313 */
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)
317{
318 if (child_fun == NULL || ctrl_pipe == NULL)
319 return EINVAL;
320
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
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);
330
331 ddf_fun_t *child = NULL;
332 int rc;
333
334 /*
335 * TODO: Once the device driver framework support persistent
336 * naming etc., something more descriptive could be created.
337 */
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);
341 if (rc < 0) {
342 goto failure;
343 }
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;
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 }
367 new_device->address = ctrl_pipe->wire->address;
368 new_device->fun = child;
369 }
370
371
372 rc = usb_device_create_match_ids(ctrl_pipe, &child->match_ids);
373 if (rc != EOK) {
374 goto failure;
375 }
376
377 rc = ddf_fun_bind(child);
378 if (rc != EOK) {
379 goto failure;
380 }
381
382 *child_fun = child;
383 return EOK;
384
385failure:
386 if (child != NULL) {
387 /* We know nothing about the data if it came from outside. */
388 if (dev_data) {
389 child->driver_data = NULL;
390 }
391 /* This takes care of match_id deallocation as well. */
392 ddf_fun_destroy(child);
393 }
394
395 return rc;
396}
397
398
399/**
400 * @}
401 */
Note: See TracBrowser for help on using the repository browser.