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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8c95dff 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
RevLine 
[02ccfcd]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
[160b75e]29/** @addtogroup libusbdev
[02ccfcd]30 * @{
31 */
32/** @file
[a6add7a]33 * Functions for recognition of attached devices.
[02ccfcd]34 */
[77ad86c]35
[56fd7cf]36/** XXX Fix this */
37#define _DDF_DATA_IMPLANT
38
[17aca1c]39#include <sys/types.h>
[eb1a2f4]40#include <fibril_synch.h>
[59c163c]41#include <usb/debug.h>
42#include <usb/dev/hub.h>
[7d521e24]43#include <usb/dev/pipes.h>
44#include <usb/dev/recognise.h>
[357a302]45#include <usb/ddfiface.h>
[7d521e24]46#include <usb/dev/request.h>
[02ccfcd]47#include <usb/classes/classes.h>
48#include <stdio.h>
49#include <errno.h>
[eb1a2f4]50#include <assert.h>
[02ccfcd]51
[a6add7a]52/** DDF operations of child devices. */
[eea3e39]53static ddf_dev_ops_t child_ops = {
[357a302]54 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_child_impl
[71ed4849]55};
[02ccfcd]56
[a6add7a]57/** Get integer part from BCD coded number. */
[02ccfcd]58#define BCD_INT(a) (((unsigned int)(a)) / 256)
[a6add7a]59/** Get fraction part from BCD coded number (as an integer, no less). */
[02ccfcd]60#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
61
[a6add7a]62/** Format for BCD coded number to be used in printf. */
[02ccfcd]63#define BCD_FMT "%x.%x"
[a6add7a]64/** Arguments to printf for BCD coded number. */
[02ccfcd]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,
[37e4025]75 const char *match_str)
[02ccfcd]76{
[37e4025]77 assert(matches);
[02ccfcd]78
[37e4025]79 match_id_t *match_id = create_match_id();
[02ccfcd]80 if (match_id == NULL) {
[37e4025]81 return ENOMEM;
[02ccfcd]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
[a6add7a]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 */
[6e6dc7d]98#define ADD_MATCHID_OR_RETURN(match_ids, score, format, ...) \
99 do { \
[37e4025]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 } \
[6e6dc7d]105 if (__rc != EOK) { \
[37e4025]106 free(str); \
[6e6dc7d]107 return __rc; \
108 } \
109 } while (0)
110
[540e2d2]111/** Create device match ids based on its interface.
112 *
[bc1c6fb]113 * @param[in] desc_device Device descriptor.
114 * @param[in] desc_interface Interface descriptor.
[540e2d2]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).
[bc1c6fb]118 * @retval ENOENT Device class is not "use interface".
[540e2d2]119 */
120int usb_device_create_match_ids_from_interface(
[51f0e410]121 const usb_standard_device_descriptor_t *desc_device,
122 const usb_standard_interface_descriptor_t *desc_interface,
[540e2d2]123 match_id_list_t *matches)
124{
[37e4025]125 if (desc_interface == NULL || matches == NULL) {
[540e2d2]126 return EINVAL;
127 }
128
[51f0e410]129 if (desc_interface->interface_class == USB_CLASS_USE_INTERFACE) {
[540e2d2]130 return ENOENT;
131 }
132
[51f0e410]133 const char *classname = usb_str_class(desc_interface->interface_class);
[540e2d2]134 assert(classname != NULL);
135
[51f0e410]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
[540e2d2]219
[2211125e]220 /* As a last resort, try fallback driver. */
221 ADD_MATCHID_OR_RETURN(matches, 10, "usb&interface&fallback");
222
[6e6dc7d]223 return EOK;
[540e2d2]224}
225
[692c0d3e]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 */
[0f21c0c]232int usb_device_create_match_ids_from_device_descriptor(
233 const usb_standard_device_descriptor_t *device_descriptor,
234 match_id_list_t *matches)
[692c0d3e]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. */
[6e6dc7d]242 ADD_MATCHID_OR_RETURN(matches, 100,
[345ea18]243 "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
[692c0d3e]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. */
[6e6dc7d]249 ADD_MATCHID_OR_RETURN(matches, 90,
[345ea18]250 "usb&vendor=0x%04x&product=0x%04x",
[692c0d3e]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
[3ae93a8]257 * class directly but we add a multi interface device.
[692c0d3e]258 */
259 if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
[6e6dc7d]260 ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
[692c0d3e]261 usb_str_class(device_descriptor->device_class));
[3ae93a8]262 } else {
[6e6dc7d]263 ADD_MATCHID_OR_RETURN(matches, 50, "usb&mid");
[692c0d3e]264 }
265
[93855d4]266 /* As a last resort, try fallback driver. */
267 ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
268
[692c0d3e]269 return EOK;
270}
271
[a66225f3]272
[02ccfcd]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 *
[4e8e1f5]278 * @param ctrl_pipe Control pipe to given device (session must be already
279 * started).
[02ccfcd]280 * @param matches Initialized list of match ids.
281 * @return Error code.
282 */
[a372663]283int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
[4e8e1f5]284 match_id_list_t *matches)
[02ccfcd]285{
[2179cf95]286 assert(ctrl_pipe);
[02ccfcd]287 int rc;
[692c0d3e]288 /*
289 * Retrieve device descriptor and add matches from it.
290 */
[02ccfcd]291 usb_standard_device_descriptor_t device_descriptor;
292
[4e8e1f5]293 rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
[02ccfcd]294 if (rc != EOK) {
295 return rc;
296 }
[4e8e1f5]297
[0f21c0c]298 rc = usb_device_create_match_ids_from_device_descriptor(
299 &device_descriptor, matches);
[692c0d3e]300 if (rc != EOK) {
301 return rc;
[a66225f3]302 }
[4e8e1f5]303
[02ccfcd]304 return EOK;
305}
306
307/** Probe for device kind and register it in devman.
308 *
[2179cf95]309 * @param[in] ctrl_pipe Control pipe to the device.
[4e8e1f5]310 * @param[in] parent Parent device.
[eea3e39]311 * @param[in] dev_ops Child device ops. Default child_ops will be used if NULL.
[bc1c6fb]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.
[02ccfcd]316 * @return Error code.
[77ad86c]317 *
[02ccfcd]318 */
[2179cf95]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)
[02ccfcd]322{
[2179cf95]323 if (child_fun == NULL || ctrl_pipe == NULL)
[ae754e5f]324 return EINVAL;
[77ad86c]325
[59c163c]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 }
[77ad86c]330
[c046942]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);
[77ad86c]335
[eb1a2f4]336 ddf_fun_t *child = NULL;
[7ed5b576]337 int rc;
[77ad86c]338
[7ed5b576]339 /*
[b207803]340 * TODO: Once the device driver framework support persistent
341 * naming etc., something more descriptive could be created.
[7ed5b576]342 */
[77ad86c]343 char child_name[12]; /* The format is: "usbAB_aXYZ", length 11 */
[2179cf95]344 rc = snprintf(child_name, sizeof(child_name),
345 "usb%02zu_a%d", this_device_name_index, ctrl_pipe->wire->address);
[7ed5b576]346 if (rc < 0) {
347 goto failure;
348 }
[77ad86c]349
[eb1a2f4]350 child = ddf_fun_create(parent, fun_inner, child_name);
351 if (child == NULL) {
352 rc = ENOMEM;
353 goto failure;
354 }
[77ad86c]355
356 if (dev_ops != NULL)
[56fd7cf]357 ddf_fun_set_ops(child, dev_ops);
[77ad86c]358 else
[56fd7cf]359 ddf_fun_set_ops(child, &child_ops);
360
361 ddf_fun_data_implant(child, dev_data);
[77ad86c]362
363 /*
364 * Store the attached device in fun
365 * driver data if there is no other data
366 */
[59c163c]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 }
[77ad86c]374
[2179cf95]375 new_device->address = ctrl_pipe->wire->address;
[59c163c]376 new_device->fun = child;
377 }
[77ad86c]378
[56fd7cf]379 match_id_list_t match_ids;
380 init_match_ids(&match_ids);
381 rc = usb_device_create_match_ids(ctrl_pipe, &match_ids);
[77ad86c]382 if (rc != EOK)
[4e8e1f5]383 goto failure;
[77ad86c]384
[56fd7cf]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
[eb1a2f4]396 rc = ddf_fun_bind(child);
[77ad86c]397 if (rc != EOK)
[7ed5b576]398 goto failure;
[77ad86c]399
[ae754e5f]400 *child_fun = child;
[7ed5b576]401 return EOK;
[77ad86c]402
[7ed5b576]403failure:
404 if (child != NULL) {
405 /* This takes care of match_id deallocation as well. */
[eb1a2f4]406 ddf_fun_destroy(child);
[7ed5b576]407 }
[77ad86c]408
[7ed5b576]409 return rc;
[02ccfcd]410}
[7ed5b576]411
[02ccfcd]412/**
413 * @}
414 */
Note: See TracBrowser for help on using the repository browser.