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