source: mainline/uspace/lib/usb/src/recognise.c@ e936e8e

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

Removal of API that use phones directly

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