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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 77ad86c was 77ad86c, checked in by Martin Decky <martin@…>, 13 years ago

cstyle (no change in functionality)

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