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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e6a98c5 was 56fd7cf, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

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