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