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