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

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

libusbdev: Drop special handling of multiinterface device match id generation.

Add class matchid even if class is USB_CLASS_USE_INTERFACE.
usbmid match id will handle this.

  • Property mode set to 100644
File size: 11.6 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 /* Class match id */
253 ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
254 usb_str_class(device_descriptor->device_class));
255
256 /* As a last resort, try fallback driver. */
257 ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
258
259 return EOK;
260}
261
262
263/** Create match ids describing attached device.
264 *
265 * @warning The list of match ids @p matches may change even when
266 * function exits with error.
267 *
268 * @param ctrl_pipe Control pipe to given device (session must be already
269 * started).
270 * @param matches Initialized list of match ids.
271 * @return Error code.
272 */
273int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
274 match_id_list_t *matches)
275{
276 assert(ctrl_pipe);
277 int rc;
278 /*
279 * Retrieve device descriptor and add matches from it.
280 */
281 usb_standard_device_descriptor_t device_descriptor;
282
283 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
284 if (rc != EOK) {
285 return rc;
286 }
287
288 rc = usb_device_create_match_ids_from_device_descriptor(
289 &device_descriptor, matches);
290 if (rc != EOK) {
291 return rc;
292 }
293
294 return EOK;
295}
296
297/** Probe for device kind and register it in devman.
298 *
299 * @param[in] ctrl_pipe Control pipe to the device.
300 * @param[in] parent Parent device.
301 * @param[in] dev_ops Child device ops. Default child_ops will be used if NULL.
302 * @param[in] dev_data Arbitrary pointer to be stored in the child
303 * as @c driver_data.
304 * @param[out] child_fun Storage where pointer to allocated child function
305 * will be written.
306 * @return Error code.
307 *
308 */
309int usb_device_register_child_in_devman(usb_pipe_t *ctrl_pipe,
310 ddf_dev_t *parent, ddf_fun_t **child_fun)
311{
312 if (child_fun == NULL || ctrl_pipe == NULL)
313 return EINVAL;
314
315 /** Index to append after device name for uniqueness. */
316 static atomic_t device_name_index = {0};
317 const size_t this_device_name_index =
318 (size_t) atomic_preinc(&device_name_index);
319
320 ddf_fun_t *child = NULL;
321 int rc;
322
323 /*
324 * TODO: Once the device driver framework support persistent
325 * naming etc., something more descriptive could be created.
326 */
327 char child_name[12]; /* The format is: "usbAB_aXYZ", length 11 */
328 rc = snprintf(child_name, sizeof(child_name),
329 "usb%02zu_a%d", this_device_name_index, ctrl_pipe->wire->address);
330 if (rc < 0) {
331 goto failure;
332 }
333
334 child = ddf_fun_create(parent, fun_inner, child_name);
335 if (child == NULL) {
336 rc = ENOMEM;
337 goto failure;
338 }
339
340 ddf_fun_set_ops(child, &child_ops);
341 /*
342 * Store the attached device in fun
343 * driver data if there is no other data
344 */
345 usb_hub_attached_device_t *new_device = ddf_fun_data_alloc(
346 child, sizeof(usb_hub_attached_device_t));
347 if (!new_device) {
348 rc = ENOMEM;
349 goto failure;
350 }
351
352 new_device->address = ctrl_pipe->wire->address;
353 new_device->fun = child;
354
355 match_id_list_t match_ids;
356 init_match_ids(&match_ids);
357 rc = usb_device_create_match_ids(ctrl_pipe, &match_ids);
358 if (rc != EOK)
359 goto failure;
360
361 list_foreach(match_ids.ids, id_link) {
362 match_id_t *match_id = list_get_instance(id_link, match_id_t, link);
363 rc = ddf_fun_add_match_id(child, match_id->id, match_id->score);
364 if (rc != EOK) {
365 clean_match_ids(&match_ids);
366 goto failure;
367 }
368 }
369
370 clean_match_ids(&match_ids);
371
372 rc = ddf_fun_bind(child);
373 if (rc != EOK)
374 goto failure;
375
376 *child_fun = child;
377 return EOK;
378
379failure:
380 if (child != NULL) {
381 /* This takes care of match_id deallocation as well. */
382 ddf_fun_destroy(child);
383 }
384
385 return rc;
386}
387
388/**
389 * @}
390 */
Note: See TracBrowser for help on using the repository browser.