[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 |
|
---|
[17aca1c] | 36 | #include <sys/types.h>
|
---|
[eb1a2f4] | 37 | #include <fibril_synch.h>
|
---|
[59c163c] | 38 | #include <usb/debug.h>
|
---|
[7d521e24] | 39 | #include <usb/dev/pipes.h>
|
---|
| 40 | #include <usb/dev/recognise.h>
|
---|
| 41 | #include <usb/dev/request.h>
|
---|
[02ccfcd] | 42 | #include <usb/classes/classes.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <errno.h>
|
---|
[eb1a2f4] | 45 | #include <assert.h>
|
---|
[02ccfcd] | 46 |
|
---|
[a6add7a] | 47 | /** Get integer part from BCD coded number. */
|
---|
[02ccfcd] | 48 | #define BCD_INT(a) (((unsigned int)(a)) / 256)
|
---|
[a6add7a] | 49 | /** Get fraction part from BCD coded number (as an integer, no less). */
|
---|
[02ccfcd] | 50 | #define BCD_FRAC(a) (((unsigned int)(a)) % 256)
|
---|
| 51 |
|
---|
[a6add7a] | 52 | /** Format for BCD coded number to be used in printf. */
|
---|
[02ccfcd] | 53 | #define BCD_FMT "%x.%x"
|
---|
[a6add7a] | 54 | /** Arguments to printf for BCD coded number. */
|
---|
[02ccfcd] | 55 | #define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
|
---|
| 56 |
|
---|
| 57 | /** Add formatted match id.
|
---|
| 58 | *
|
---|
| 59 | * @param matches List of match ids where to add to.
|
---|
| 60 | * @param score Score of the match.
|
---|
| 61 | * @param format Printf-like format
|
---|
| 62 | * @return Error code.
|
---|
| 63 | */
|
---|
| 64 | static int usb_add_match_id(match_id_list_t *matches, int score,
|
---|
[37e4025] | 65 | const char *match_str)
|
---|
[02ccfcd] | 66 | {
|
---|
[37e4025] | 67 | assert(matches);
|
---|
[02ccfcd] | 68 |
|
---|
[37e4025] | 69 | match_id_t *match_id = create_match_id();
|
---|
[02ccfcd] | 70 | if (match_id == NULL) {
|
---|
[37e4025] | 71 | return ENOMEM;
|
---|
[02ccfcd] | 72 | }
|
---|
| 73 |
|
---|
| 74 | match_id->id = match_str;
|
---|
| 75 | match_id->score = score;
|
---|
| 76 | add_match_id(matches, match_id);
|
---|
| 77 |
|
---|
| 78 | return EOK;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[a6add7a] | 81 | /** Add match id to list or return with error code.
|
---|
| 82 | *
|
---|
| 83 | * @param match_ids List of match ids.
|
---|
| 84 | * @param score Match id score.
|
---|
| 85 | * @param format Format of the matching string
|
---|
| 86 | * @param ... Arguments for the format.
|
---|
| 87 | */
|
---|
[6e6dc7d] | 88 | #define ADD_MATCHID_OR_RETURN(match_ids, score, format, ...) \
|
---|
| 89 | do { \
|
---|
[37e4025] | 90 | char *str = NULL; \
|
---|
| 91 | int __rc = asprintf(&str, format, ##__VA_ARGS__); \
|
---|
| 92 | if (__rc > 0) { \
|
---|
| 93 | __rc = usb_add_match_id((match_ids), (score), str); \
|
---|
| 94 | } \
|
---|
[6e6dc7d] | 95 | if (__rc != EOK) { \
|
---|
[37e4025] | 96 | free(str); \
|
---|
[6e6dc7d] | 97 | return __rc; \
|
---|
| 98 | } \
|
---|
| 99 | } while (0)
|
---|
| 100 |
|
---|
[540e2d2] | 101 | /** Create device match ids based on its interface.
|
---|
| 102 | *
|
---|
[bc1c6fb] | 103 | * @param[in] desc_device Device descriptor.
|
---|
| 104 | * @param[in] desc_interface Interface descriptor.
|
---|
[540e2d2] | 105 | * @param[out] matches Initialized list of match ids.
|
---|
| 106 | * @return Error code (the two mentioned are not the only ones).
|
---|
| 107 | * @retval EINVAL Invalid input parameters (expects non NULL pointers).
|
---|
[bc1c6fb] | 108 | * @retval ENOENT Device class is not "use interface".
|
---|
[540e2d2] | 109 | */
|
---|
| 110 | int usb_device_create_match_ids_from_interface(
|
---|
[51f0e410] | 111 | const usb_standard_device_descriptor_t *desc_device,
|
---|
| 112 | const usb_standard_interface_descriptor_t *desc_interface,
|
---|
[540e2d2] | 113 | match_id_list_t *matches)
|
---|
| 114 | {
|
---|
[37e4025] | 115 | if (desc_interface == NULL || matches == NULL) {
|
---|
[540e2d2] | 116 | return EINVAL;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[51f0e410] | 119 | if (desc_interface->interface_class == USB_CLASS_USE_INTERFACE) {
|
---|
[540e2d2] | 120 | return ENOENT;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[51f0e410] | 123 | const char *classname = usb_str_class(desc_interface->interface_class);
|
---|
[540e2d2] | 124 | assert(classname != NULL);
|
---|
| 125 |
|
---|
[51f0e410] | 126 | #define IFACE_PROTOCOL_FMT "interface&class=%s&subclass=0x%02x&protocol=0x%02x"
|
---|
| 127 | #define IFACE_PROTOCOL_ARGS classname, desc_interface->interface_subclass, \
|
---|
| 128 | desc_interface->interface_protocol
|
---|
| 129 |
|
---|
| 130 | #define IFACE_SUBCLASS_FMT "interface&class=%s&subclass=0x%02x"
|
---|
| 131 | #define IFACE_SUBCLASS_ARGS classname, desc_interface->interface_subclass
|
---|
| 132 |
|
---|
| 133 | #define IFACE_CLASS_FMT "interface&class=%s"
|
---|
| 134 | #define IFACE_CLASS_ARGS classname
|
---|
| 135 |
|
---|
| 136 | #define VENDOR_RELEASE_FMT "vendor=0x%04x&product=0x%04x&release=" BCD_FMT
|
---|
| 137 | #define VENDOR_RELEASE_ARGS desc_device->vendor_id, desc_device->product_id, \
|
---|
| 138 | BCD_ARGS(desc_device->device_version)
|
---|
| 139 |
|
---|
| 140 | #define VENDOR_PRODUCT_FMT "vendor=0x%04x&product=0x%04x"
|
---|
| 141 | #define VENDOR_PRODUCT_ARGS desc_device->vendor_id, desc_device->product_id
|
---|
| 142 |
|
---|
| 143 | #define VENDOR_ONLY_FMT "vendor=0x%04x"
|
---|
| 144 | #define VENDOR_ONLY_ARGS desc_device->vendor_id
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | * If the vendor is specified, create match ids with vendor with
|
---|
| 148 | * higher score.
|
---|
| 149 | * Then the same ones without the vendor part.
|
---|
| 150 | */
|
---|
| 151 | if ((desc_device != NULL) && (desc_device->vendor_id != 0)) {
|
---|
| 152 | /* First, interface matches with device release number. */
|
---|
| 153 | ADD_MATCHID_OR_RETURN(matches, 250,
|
---|
| 154 | "usb&" VENDOR_RELEASE_FMT "&" IFACE_PROTOCOL_FMT,
|
---|
| 155 | VENDOR_RELEASE_ARGS, IFACE_PROTOCOL_ARGS);
|
---|
| 156 | ADD_MATCHID_OR_RETURN(matches, 240,
|
---|
| 157 | "usb&" VENDOR_RELEASE_FMT "&" IFACE_SUBCLASS_FMT,
|
---|
| 158 | VENDOR_RELEASE_ARGS, IFACE_SUBCLASS_ARGS);
|
---|
| 159 | ADD_MATCHID_OR_RETURN(matches, 230,
|
---|
| 160 | "usb&" VENDOR_RELEASE_FMT "&" IFACE_CLASS_FMT,
|
---|
| 161 | VENDOR_RELEASE_ARGS, IFACE_CLASS_ARGS);
|
---|
| 162 |
|
---|
| 163 | /* Next, interface matches without release number. */
|
---|
| 164 | ADD_MATCHID_OR_RETURN(matches, 220,
|
---|
| 165 | "usb&" VENDOR_PRODUCT_FMT "&" IFACE_PROTOCOL_FMT,
|
---|
| 166 | VENDOR_PRODUCT_ARGS, IFACE_PROTOCOL_ARGS);
|
---|
| 167 | ADD_MATCHID_OR_RETURN(matches, 210,
|
---|
| 168 | "usb&" VENDOR_PRODUCT_FMT "&" IFACE_SUBCLASS_FMT,
|
---|
| 169 | VENDOR_PRODUCT_ARGS, IFACE_SUBCLASS_ARGS);
|
---|
| 170 | ADD_MATCHID_OR_RETURN(matches, 200,
|
---|
| 171 | "usb&" VENDOR_PRODUCT_FMT "&" IFACE_CLASS_FMT,
|
---|
| 172 | VENDOR_PRODUCT_ARGS, IFACE_CLASS_ARGS);
|
---|
| 173 |
|
---|
| 174 | /* Finally, interface matches with only vendor. */
|
---|
| 175 | ADD_MATCHID_OR_RETURN(matches, 190,
|
---|
| 176 | "usb&" VENDOR_ONLY_FMT "&" IFACE_PROTOCOL_FMT,
|
---|
| 177 | VENDOR_ONLY_ARGS, IFACE_PROTOCOL_ARGS);
|
---|
| 178 | ADD_MATCHID_OR_RETURN(matches, 180,
|
---|
| 179 | "usb&" VENDOR_ONLY_FMT "&" IFACE_SUBCLASS_FMT,
|
---|
| 180 | VENDOR_ONLY_ARGS, IFACE_SUBCLASS_ARGS);
|
---|
| 181 | ADD_MATCHID_OR_RETURN(matches, 170,
|
---|
| 182 | "usb&" VENDOR_ONLY_FMT "&" IFACE_CLASS_FMT,
|
---|
| 183 | VENDOR_ONLY_ARGS, IFACE_CLASS_ARGS);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /* Now, the same but without any vendor specification. */
|
---|
| 187 | ADD_MATCHID_OR_RETURN(matches, 160,
|
---|
| 188 | "usb&" IFACE_PROTOCOL_FMT,
|
---|
| 189 | IFACE_PROTOCOL_ARGS);
|
---|
| 190 | ADD_MATCHID_OR_RETURN(matches, 150,
|
---|
| 191 | "usb&" IFACE_SUBCLASS_FMT,
|
---|
| 192 | IFACE_SUBCLASS_ARGS);
|
---|
| 193 | ADD_MATCHID_OR_RETURN(matches, 140,
|
---|
| 194 | "usb&" IFACE_CLASS_FMT,
|
---|
| 195 | IFACE_CLASS_ARGS);
|
---|
| 196 |
|
---|
| 197 | #undef IFACE_PROTOCOL_FMT
|
---|
| 198 | #undef IFACE_PROTOCOL_ARGS
|
---|
| 199 | #undef IFACE_SUBCLASS_FMT
|
---|
| 200 | #undef IFACE_SUBCLASS_ARGS
|
---|
| 201 | #undef IFACE_CLASS_FMT
|
---|
| 202 | #undef IFACE_CLASS_ARGS
|
---|
| 203 | #undef VENDOR_RELEASE_FMT
|
---|
| 204 | #undef VENDOR_RELEASE_ARGS
|
---|
| 205 | #undef VENDOR_PRODUCT_FMT
|
---|
| 206 | #undef VENDOR_PRODUCT_ARGS
|
---|
| 207 | #undef VENDOR_ONLY_FMT
|
---|
| 208 | #undef VENDOR_ONLY_ARGS
|
---|
[540e2d2] | 209 |
|
---|
[2211125e] | 210 | /* As a last resort, try fallback driver. */
|
---|
| 211 | ADD_MATCHID_OR_RETURN(matches, 10, "usb&interface&fallback");
|
---|
| 212 |
|
---|
[6e6dc7d] | 213 | return EOK;
|
---|
[540e2d2] | 214 | }
|
---|
| 215 |
|
---|
[692c0d3e] | 216 | /** Create DDF match ids from USB device descriptor.
|
---|
| 217 | *
|
---|
| 218 | * @param matches List of match ids to extend.
|
---|
| 219 | * @param device_descriptor Device descriptor returned by given device.
|
---|
| 220 | * @return Error code.
|
---|
| 221 | */
|
---|
[0f21c0c] | 222 | int usb_device_create_match_ids_from_device_descriptor(
|
---|
| 223 | const usb_standard_device_descriptor_t *device_descriptor,
|
---|
| 224 | match_id_list_t *matches)
|
---|
[692c0d3e] | 225 | {
|
---|
| 226 | /*
|
---|
| 227 | * Unless the vendor id is 0, the pair idVendor-idProduct
|
---|
| 228 | * quite uniquely describes the device.
|
---|
| 229 | */
|
---|
| 230 | if (device_descriptor->vendor_id != 0) {
|
---|
| 231 | /* First, with release number. */
|
---|
[6e6dc7d] | 232 | ADD_MATCHID_OR_RETURN(matches, 100,
|
---|
[345ea18] | 233 | "usb&vendor=0x%04x&product=0x%04x&release=" BCD_FMT,
|
---|
[692c0d3e] | 234 | (int) device_descriptor->vendor_id,
|
---|
| 235 | (int) device_descriptor->product_id,
|
---|
| 236 | BCD_ARGS(device_descriptor->device_version));
|
---|
| 237 |
|
---|
| 238 | /* Next, without release number. */
|
---|
[6e6dc7d] | 239 | ADD_MATCHID_OR_RETURN(matches, 90,
|
---|
[345ea18] | 240 | "usb&vendor=0x%04x&product=0x%04x",
|
---|
[692c0d3e] | 241 | (int) device_descriptor->vendor_id,
|
---|
| 242 | (int) device_descriptor->product_id);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[daf199f] | 245 | /* Class match id */
|
---|
| 246 | ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
|
---|
| 247 | usb_str_class(device_descriptor->device_class));
|
---|
[692c0d3e] | 248 |
|
---|
[93855d4] | 249 | /* As a last resort, try fallback driver. */
|
---|
| 250 | ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
|
---|
| 251 |
|
---|
[692c0d3e] | 252 | return EOK;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[a66225f3] | 255 |
|
---|
[02ccfcd] | 256 | /** Create match ids describing attached device.
|
---|
| 257 | *
|
---|
| 258 | * @warning The list of match ids @p matches may change even when
|
---|
| 259 | * function exits with error.
|
---|
| 260 | *
|
---|
[4e8e1f5] | 261 | * @param ctrl_pipe Control pipe to given device (session must be already
|
---|
| 262 | * started).
|
---|
[02ccfcd] | 263 | * @param matches Initialized list of match ids.
|
---|
| 264 | * @return Error code.
|
---|
| 265 | */
|
---|
[a372663] | 266 | int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe,
|
---|
[4e8e1f5] | 267 | match_id_list_t *matches)
|
---|
[02ccfcd] | 268 | {
|
---|
[2179cf95] | 269 | assert(ctrl_pipe);
|
---|
[02ccfcd] | 270 | int rc;
|
---|
[692c0d3e] | 271 | /*
|
---|
| 272 | * Retrieve device descriptor and add matches from it.
|
---|
| 273 | */
|
---|
[02ccfcd] | 274 | usb_standard_device_descriptor_t device_descriptor;
|
---|
| 275 |
|
---|
[4e8e1f5] | 276 | rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor);
|
---|
[02ccfcd] | 277 | if (rc != EOK) {
|
---|
| 278 | return rc;
|
---|
| 279 | }
|
---|
[4e8e1f5] | 280 |
|
---|
[0f21c0c] | 281 | rc = usb_device_create_match_ids_from_device_descriptor(
|
---|
| 282 | &device_descriptor, matches);
|
---|
[692c0d3e] | 283 | if (rc != EOK) {
|
---|
| 284 | return rc;
|
---|
[a66225f3] | 285 | }
|
---|
[4e8e1f5] | 286 |
|
---|
[02ccfcd] | 287 | return EOK;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /**
|
---|
| 291 | * @}
|
---|
| 292 | */
|
---|