1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * USBA: Solaris USB Architecture support 30 * 31 * This module builds a tree of parsed USB standard descriptors and unparsed 32 * Class/Vendor specific (C/V) descriptors. Routines are grouped into three 33 * groups: those which build the tree, those which take it down, and those which 34 * dump it. 35 * 36 * The tree built hangs off of the dev_cfg field of the usb_client_dev_data_t 37 * structure returned by usb_get_dev_data(). The tree consists of different 38 * kinds of tree nodes (usb_xxx_data_t) each containing a standard USB 39 * descriptor (usb_xxx_descr_t) and pointers to arrays of other nodes. 40 * 41 * Arrays are dynamically sized, as the descriptors coming from the device may 42 * lie, but the number of descriptors from the device is a more reliable 43 * indicator of configuration. This makes the code more robust. After the raw 44 * descriptor data has been parsed into a non-sparse tree, the tree is ordered 45 * and made sparse with a bin-sort style algorithm. 46 * 47 * dev_cfg is an array of configuration tree nodes. Each contains space for one 48 * parsed standard USB configuration descriptor, a pointer to an array of c/v 49 * tree nodes and a pointer to an array of interface tree nodes. 50 * 51 * Each interface tree node represents a group of interface descriptors, called 52 * alternates, with the same interface number. Thus, each interface tree node 53 * has a pointer to an array of alternate-interface tree nodes each containing a 54 * standard USB interface descriptor. Alternate-interface tree nodes also 55 * contain a pointer to an array of c/v tree nodes and a pointer to an array of 56 * endpoint tree nodes. 57 * 58 * Endpoint tree nodes contain a standard endpoint descriptor, plus a pointer to 59 * an array of c/v tree nodes. 60 * 61 * Each array in the tree contains elements ranging from 0 to the largest key 62 * value of it's elements. Endpoints are a special case. The direction bit is 63 * right shifted over three unused bits before the index is determined, leaving 64 * a range of 0..31 instead of a sparsely-populated range of 0..255. 65 * 66 * The indices of tree elements coincide with their USB key values. For 67 * example, standard USB devices have no configuration 0; if they have one 68 * configuration it is #1. dev_cfg[0] is zeroed out; dev_cfg[1] is the root 69 * of configuration #1. 70 * 71 * The idea here is for a driver to be able to parse the tree to easily find a 72 * desired descriptor. For example, the interval of endpoint 2, alternate 3, 73 * interface 1, configuration 1 would be: 74 * dv->dev_cfg[1].cfg_if[1].if_alt[3].altif_ep[2].ep_descr.bInterval 75 * 76 * How the tree is built: 77 * 78 * usb_build_descr_tree() is responsible for the whole process. 79 * 80 * Next, usba_build_descr_tree() coordinates parsing this byte stream, 81 * descriptor by descriptor. usba_build_descr_tree() calls the appropriate 82 * usba_process_xx_descr() function to interpret and install each descriptor in 83 * the tree, based on the descriptor's type. When done with this phase, a 84 * non-sparse tree exists containing tree nodes with descriptors in the order 85 * they were found in the raw data. 86 * 87 * All levels of the tree, except alternates, remain non-sparse. Alternates are 88 * moved, possibly, within their array, so that descriptors are indexed by their 89 * alternate ID. 90 * 91 * The usba_reg_state_t structure maintains state of the tree-building process, 92 * helping coordinate all routines involved. 93 */ 94 #define USBA_FRAMEWORK 95 #include <sys/usb/usba.h> 96 #include <sys/usb/usba/usba_impl.h> 97 #include <sys/usb/usba/usba_private.h> 98 #include <sys/usb/usba/hcdi_impl.h> 99 #include <sys/usb/hubd/hub.h> 100 101 #include <sys/usb/usba/usbai_register_impl.h> 102 103 /* 104 * Header needed for use by this module only. 105 * However, function may be used in V0.8 drivers so needs to be global. 106 */ 107 int usb_log_descr_tree(usb_client_dev_data_t *, usb_log_handle_t, 108 uint_t, uint_t); 109 110 /* Debug stuff */ 111 usb_log_handle_t usbai_reg_log_handle; 112 uint_t usbai_register_errlevel = USB_LOG_L2; 113 uint_t usbai_register_dump_errlevel = USB_LOG_L2; 114 uint_t usbai_register_errmask = (uint_t)-1; 115 116 /* Function prototypes */ 117 static int usba_build_descr_tree(dev_info_t *, usba_device_t *, 118 usb_client_dev_data_t *); 119 static void usba_process_cfg_descr(usba_reg_state_t *); 120 static int usba_process_if_descr(usba_reg_state_t *, boolean_t *); 121 static int usba_process_ep_descr(usba_reg_state_t *); 122 static int usba_process_cv_descr(usba_reg_state_t *); 123 static int usba_set_parse_values(dev_info_t *dip, usba_device_t *usba_device, 124 usba_reg_state_t *state); 125 static void* usba_kmem_realloc(void *, int, int); 126 static void usba_augment_array(void **, uint_t, uint_t); 127 static void usba_make_alts_sparse(usb_alt_if_data_t **, uint_t *); 128 129 static void usba_order_tree(usba_reg_state_t *); 130 131 static void usba_free_if_array(usb_if_data_t *, uint_t); 132 static void usba_free_ep_array(usb_ep_data_t *, uint_t); 133 static void usba_free_cv_array(usb_cvs_data_t *, uint_t); 134 135 static int usba_dump_descr_tree(dev_info_t *, usb_client_dev_data_t *, 136 usb_log_handle_t, uint_t, uint_t); 137 static void usba_dump_if(usb_if_data_t *, usb_log_handle_t, 138 uint_t, uint_t, char *); 139 static void usba_dump_ep(uint_t, usb_ep_data_t *, usb_log_handle_t, uint_t, 140 uint_t, char *); 141 static void usba_dump_cv(usb_cvs_data_t *, usb_log_handle_t, uint_t, uint_t, 142 char *, int); 143 static void usba_dump_bin(uint8_t *, int, int, usb_log_handle_t, 144 uint_t, uint_t, char *, int); 145 146 /* Framework initialization. */ 147 void 148 usba_usbai_register_initialization() 149 { 150 usbai_reg_log_handle = usb_alloc_log_hdl(NULL, "usbreg", 151 &usbai_register_errlevel, 152 &usbai_register_errmask, NULL, 153 0); 154 155 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 156 "usba_usbai_register_initialization"); 157 } 158 159 160 /* Framework destruction. */ 161 void 162 usba_usbai_register_destroy() 163 { 164 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 165 "usba_usbai_register destroy"); 166 167 usb_free_log_hdl(usbai_reg_log_handle); 168 } 169 170 171 /* 172 * usb_client_attach: 173 * 174 * Arguments: 175 * dip - pointer to devinfo node of the client 176 * version - USBA registration version number 177 * flags - None used 178 * 179 * Return Values: 180 * USB_SUCCESS - attach succeeded 181 * USB_INVALID_ARGS - received null dip 182 * USB_INVALID_VERSION - version argument is incorrect. 183 * USB_FAILURE - other internal failure 184 */ 185 /*ARGSUSED*/ 186 int 187 usb_client_attach(dev_info_t *dip, uint_t version, usb_flags_t flags) 188 { 189 int rval; 190 usba_device_t *usba_device; 191 192 if (dip == NULL) { 193 194 return (USB_INVALID_ARGS); 195 } 196 197 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 198 "usb_client attach:"); 199 200 usba_device = usba_get_usba_device(dip); 201 202 /* 203 * Allow exact match for legacy (DDK 0.8/9) drivers, or same major 204 * VERSion and smaller or same minor version for non-legacy drivers. 205 */ 206 if ((version != 207 USBA_MAKE_VER(USBA_LEG_MAJOR_VER, USBA_LEG_MINOR_VER)) && 208 ((USBA_GET_MAJOR(version) != USBA_MAJOR_VER) || 209 (USBA_GET_MINOR(version) > USBA_MINOR_VER))) { 210 USB_DPRINTF_L1(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 211 "Incorrect USB driver version for %s%d: found: %d.%d, " 212 "expecting %d.%d", 213 ddi_driver_name(dip), ddi_get_instance(dip), 214 USBA_GET_MAJOR(version), USBA_GET_MINOR(version), 215 USBA_MAJOR_VER, USBA_MINOR_VER); 216 217 return (USB_INVALID_VERSION); 218 } 219 220 if (version == USBA_MAKE_VER(USBA_LEG_MAJOR_VER, USBA_LEG_MINOR_VER)) { 221 USB_DPRINTF_L2(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 222 "Accepting legacy USB driver version %d.%d for %s%d", 223 USBA_LEG_MAJOR_VER, USBA_LEG_MINOR_VER, 224 ddi_driver_name(dip), ddi_get_instance(dip)); 225 } 226 227 rval = ndi_prop_update_int(DDI_DEV_T_NONE, dip, "driver-major", 228 USBA_GET_MAJOR(version)); 229 if (rval != DDI_PROP_SUCCESS) { 230 231 return (USB_FAILURE); 232 } 233 rval = ndi_prop_update_int(DDI_DEV_T_NONE, dip, "driver-minor", 234 USBA_GET_MINOR(version)); 235 if (rval != DDI_PROP_SUCCESS) { 236 237 return (USB_FAILURE); 238 } 239 240 mutex_enter(&usba_device->usb_mutex); 241 if (strcmp(ddi_driver_name(dip), "usb_mid") != 0) { 242 usba_device->usb_client_flags[usba_get_ifno(dip)] |= 243 USBA_CLIENT_FLAG_ATTACH; 244 usba_device->usb_client_attach_list->dip = dip; 245 } 246 mutex_exit(&usba_device->usb_mutex); 247 248 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 249 "usb_client attach: done"); 250 251 return (USB_SUCCESS); 252 } 253 254 255 /* 256 * usb_client_detach: 257 * free dev_data is reg != NULL, not much else to do 258 * 259 * Arguments: 260 * dip - pointer to devinfo node of the client 261 * reg - return registration data at this address 262 */ 263 void 264 usb_client_detach(dev_info_t *dip, usb_client_dev_data_t *reg) 265 { 266 usba_device_t *usba_device = usba_get_usba_device(dip); 267 268 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 269 "usb_client_detach:"); 270 271 if (dip) { 272 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 273 "Unregistering usb client %s%d: reg=0x%p", 274 ddi_driver_name(dip), ddi_get_instance(dip), reg); 275 276 usb_free_dev_data(dip, reg); 277 278 mutex_enter(&usba_device->usb_mutex); 279 if (strcmp(ddi_driver_name(dip), "usb_mid") != 0) { 280 usba_device->usb_client_flags[usba_get_ifno(dip)] &= 281 ~USBA_CLIENT_FLAG_ATTACH; 282 } 283 mutex_exit(&usba_device->usb_mutex); 284 } 285 286 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 287 "usb_client_detach done"); 288 } 289 290 291 /* 292 * usb_register_client (deprecated): 293 * The client registers with USBA during attach. 294 */ 295 /*ARGSUSED*/ 296 int 297 usb_register_client(dev_info_t *dip, uint_t version, 298 usb_client_dev_data_t **reg, usb_reg_parse_lvl_t parse_level, 299 usb_flags_t flags) 300 { 301 int rval = usb_client_attach(dip, version, flags); 302 303 if (rval == USB_SUCCESS) { 304 rval = usb_get_dev_data(dip, reg, parse_level, flags); 305 306 if (rval != USB_SUCCESS) { 307 usb_client_detach(dip, NULL); 308 } 309 } 310 311 return (rval); 312 } 313 314 315 /* 316 * usb_unregister_client (deprecated): 317 * Undo the makings of usb_get_dev_data(). Free memory if allocated. 318 * 319 * Arguments: 320 * dip - pointer to devinfo node of the client 321 * reg - pointer to registration data to be freed 322 */ 323 void 324 usb_unregister_client(dev_info_t *dip, usb_client_dev_data_t *reg) 325 { 326 usb_client_detach(dip, reg); 327 } 328 329 330 /* 331 * usb_get_dev_data: 332 * On completion, the registration data has been initialized. 333 * Most data items are straightforward. 334 * Among the items returned in the data is the tree of 335 * parsed descriptors, in dev_cfg; the number of configurations parsed, 336 * in dev_n_cfg; a pointer to the current configuration in the tree, 337 * in dev_curr_cfg; the index of the first valid interface in the 338 * tree, in dev_curr_if, and a parse level that accurately reflects what 339 * is in the tree, in dev_parse_level. 340 * 341 * This routine sets up directly-initialized fields, and calls 342 * usb_build_descr_tree() to parse the raw descriptors and initialize the 343 * tree. 344 * 345 * Parse_level determines the extent to which the tree is built. It has 346 * the following values: 347 * 348 * USB_PARSE_LVL_NONE - Build no tree. dev_n_cfg will return 0, dev_cfg 349 * and dev_curr_cfg will return NULL. 350 * USB_PARSE_LVL_IF - Parse configured interface only, if configuration# 351 * and interface properties are set (as when different 352 * interfaces are viewed by the OS as different device 353 * instances). If an OS device instance is set up to 354 * represent an entire physical device, this works 355 * like USB_PARSE_LVL_ALL. 356 * USB_PARSE_LVL_CFG - Parse entire configuration of configured interface 357 * only. This is like USB_PARSE_LVL_IF except entire 358 * configuration is returned. 359 * USB_PARSE_LVL_ALL - Parse entire device (all configurations), even 360 * when driver is bound to a single interface of a 361 * single configuration. 362 * 363 * No tree is built for root hubs, regardless of parse_level. 364 * 365 * Arguments: 366 * dip - pointer to devinfo node of the client 367 * version - USBA registration version number 368 * reg - return registration data at this address 369 * parse_level - See above 370 * flags - None used 371 * 372 * Return Values: 373 * USB_SUCCESS - usb_get_dev_data succeeded 374 * USB_INVALID_ARGS - received null dip or reg argument 375 * USB_INVALID_CONTEXT - called from callback context 376 * USB_FAILURE - bad descriptor info or other internal failure 377 * 378 * Note: The non-standard USB descriptors are returned in RAW format. 379 * returns initialized registration data. Most data items are clear. 380 * Among the items returned is the tree of parsed descriptors in dev_cfg; 381 * and the number of configurations parsed in dev_n_cfg. 382 * 383 * The registration data is not shared. each client receives its own 384 * copy. 385 */ 386 /*ARGSUSED*/ 387 int 388 usb_get_dev_data(dev_info_t *dip, 389 usb_client_dev_data_t **reg, usb_reg_parse_lvl_t parse_level, 390 usb_flags_t flags) 391 { 392 usb_client_dev_data_t *usb_reg = NULL; 393 char *tmpbuf = NULL; 394 usba_device_t *usba_device; 395 int rval = USB_SUCCESS; 396 397 if ((dip == NULL) || (reg == NULL)) { 398 399 return (USB_INVALID_ARGS); 400 } 401 402 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 403 "usb_get_dev_data: %s%d", 404 ddi_driver_name(dip), ddi_get_instance(dip)); 405 406 *reg = NULL; 407 408 /* did the client attach first? */ 409 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 410 "driver-major", -1) == -1) { 411 412 return (USB_INVALID_VERSION); 413 } 414 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 415 "driver-minor", -1) == -1) { 416 417 return (USB_INVALID_VERSION); 418 } 419 420 usb_reg = kmem_zalloc(sizeof (usb_client_dev_data_t), KM_SLEEP); 421 usba_device = usba_get_usba_device(dip); 422 usb_reg->dev_descr = usba_device->usb_dev_descr; 423 usb_reg->dev_default_ph = usba_get_dflt_pipe_handle(dip); 424 if (usb_reg->dev_default_ph == NULL) { 425 kmem_free(usb_reg, sizeof (usb_client_dev_data_t)); 426 427 return (USB_FAILURE); 428 } 429 430 usb_reg->dev_iblock_cookie = usba_hcdi_get_hcdi( 431 usba_device->usb_root_hub_dip)->hcdi_soft_iblock_cookie; 432 433 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 434 "cookie = 0x%p", usb_reg->dev_iblock_cookie); 435 436 tmpbuf = (char *)kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP); 437 438 if (usba_device->usb_mfg_str != NULL) { 439 usb_reg->dev_mfg = kmem_zalloc( 440 strlen(usba_device->usb_mfg_str) + 1, KM_SLEEP); 441 (void) strcpy(usb_reg->dev_mfg, usba_device->usb_mfg_str); 442 } 443 444 if (usba_device->usb_product_str != NULL) { 445 usb_reg->dev_product = kmem_zalloc( 446 strlen(usba_device->usb_product_str) + 1, 447 KM_SLEEP); 448 (void) strcpy(usb_reg->dev_product, 449 usba_device->usb_product_str); 450 } 451 452 if (usba_device->usb_serialno_str != NULL) { 453 usb_reg->dev_serial = kmem_zalloc( 454 strlen(usba_device->usb_serialno_str) + 1, 455 KM_SLEEP); 456 (void) strcpy(usb_reg->dev_serial, 457 usba_device->usb_serialno_str); 458 } 459 460 if ((usb_reg->dev_parse_level = parse_level) == USB_PARSE_LVL_NONE) { 461 rval = USB_SUCCESS; 462 463 } else if ((rval = usba_build_descr_tree(dip, usba_device, usb_reg)) != 464 USB_SUCCESS) { 465 usb_unregister_client(dip, usb_reg); 466 usb_reg = NULL; 467 } else { 468 469 /* Current tree cfg is always zero if only one cfg in tree. */ 470 if (usb_reg->dev_n_cfg == 1) { 471 usb_reg->dev_curr_cfg = &usb_reg->dev_cfg[0]; 472 } else { 473 mutex_enter(&usba_device->usb_mutex); 474 usb_reg->dev_curr_cfg = 475 &usb_reg->dev_cfg[usba_device->usb_active_cfg_ndx]; 476 mutex_exit(&usba_device->usb_mutex); 477 ASSERT(usb_reg->dev_curr_cfg != NULL); 478 ASSERT(usb_reg->dev_curr_cfg->cfg_descr.bLength == 479 USB_CFG_DESCR_SIZE); 480 } 481 482 /* 483 * Keep dev_curr_if at device's single interface only if that 484 * particular interface has been explicitly defined by the 485 * device. 486 */ 487 usb_reg->dev_curr_if = usba_get_ifno(dip); 488 #ifdef DEBUG 489 (void) usb_log_descr_tree(usb_reg, usbai_reg_log_handle, 490 usbai_register_dump_errlevel, (uint_t)-1); 491 #endif 492 /* 493 * Fail if interface and configuration of dev_curr_if and 494 * dev_curr_cfg don't exist or are invalid. (Shouldn't happen.) 495 * These indices must be reliable for tree traversal. 496 */ 497 if ((usb_reg->dev_curr_cfg->cfg_n_if <= usb_reg->dev_curr_if) || 498 (usb_reg->dev_curr_cfg->cfg_descr.bLength == 0) || 499 (usb_reg->dev_curr_cfg->cfg_if[usb_reg->dev_curr_if]. 500 if_n_alt == 0)) { 501 USB_DPRINTF_L2(DPRINT_MASK_ALL, usbai_reg_log_handle, 502 "usb_get_dev_data: dev_curr_cfg or " 503 "dev_curr_if have no descriptors"); 504 usb_unregister_client(dip, usb_reg); 505 usb_reg = NULL; 506 rval = USB_FAILURE; 507 } 508 } 509 510 *reg = usb_reg; 511 kmem_free(tmpbuf, USB_MAXSTRINGLEN); 512 513 if (rval == USB_SUCCESS) { 514 usb_client_dev_data_list_t *entry = kmem_zalloc( 515 sizeof (*entry), KM_SLEEP); 516 mutex_enter(&usba_device->usb_mutex); 517 518 usba_device->usb_client_flags[usba_get_ifno(dip)] |= 519 USBA_CLIENT_FLAG_DEV_DATA; 520 521 entry->cddl_dip = dip; 522 entry->cddl_dev_data = usb_reg; 523 entry->cddl_ifno = usba_get_ifno(dip); 524 525 entry->cddl_next = 526 usba_device->usb_client_dev_data_list.cddl_next; 527 if (entry->cddl_next) { 528 entry->cddl_next->cddl_prev = entry; 529 } 530 entry->cddl_prev = &usba_device->usb_client_dev_data_list; 531 usba_device->usb_client_dev_data_list.cddl_next = entry; 532 533 mutex_exit(&usba_device->usb_mutex); 534 } 535 536 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 537 "usb_get_dev_data rval=%d", rval); 538 539 return (rval); 540 } 541 542 543 /* 544 * usb_free_dev_data 545 * undoes what usb_get_dev_data does 546 * 547 * Arguments: 548 * dip - pointer to devinfo node of the client 549 * reg - return registration data at this address 550 */ 551 void 552 usb_free_dev_data(dev_info_t *dip, usb_client_dev_data_t *reg) 553 { 554 if (dip == NULL) { 555 556 return; 557 } 558 559 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 560 "usb_free_dev_data %s%d: reg=0x%p", 561 ddi_driver_name(dip), ddi_get_instance(dip), reg); 562 563 if (reg != NULL) { 564 usba_device_t *usba_device = usba_get_usba_device(dip); 565 usb_client_dev_data_list_t *next, *prev, *entry; 566 int matches = 0; 567 568 if (reg->dev_serial != NULL) { 569 kmem_free((char *)reg->dev_serial, 570 strlen((char *)reg->dev_serial) + 1); 571 } 572 573 if (reg->dev_product != NULL) { 574 kmem_free((char *)reg->dev_product, 575 strlen((char *)reg->dev_product) + 1); 576 } 577 578 if (reg->dev_mfg != NULL) { 579 kmem_free((char *)reg->dev_mfg, 580 strlen((char *)reg->dev_mfg) + 1); 581 } 582 583 /* Free config tree under reg->dev_cfg. */ 584 if (reg->dev_cfg != NULL) { 585 usb_free_descr_tree(dip, reg); 586 } 587 588 mutex_enter(&usba_device->usb_mutex); 589 prev = &usba_device->usb_client_dev_data_list; 590 entry = usba_device->usb_client_dev_data_list.cddl_next; 591 592 /* free the entries in usb_client_data_list */ 593 while (entry) { 594 next = entry->cddl_next; 595 if ((dip == entry->cddl_dip) && 596 (reg == entry->cddl_dev_data)) { 597 prev->cddl_next = entry->cddl_next; 598 if (entry->cddl_next) { 599 entry->cddl_next->cddl_prev = prev; 600 } 601 kmem_free(entry, sizeof (*entry)); 602 } else { 603 /* 604 * any other entries for this interface? 605 */ 606 if (usba_get_ifno(dip) == entry->cddl_ifno) { 607 matches++; 608 } 609 prev = entry; 610 } 611 entry = next; 612 } 613 614 USB_DPRINTF_L3(DPRINT_MASK_REGISTER, 615 usbai_reg_log_handle, 616 "usb_free_dev_data: next=0x%p flags[%d]=0x%x", 617 usba_device->usb_client_dev_data_list.cddl_next, 618 usba_get_ifno(dip), 619 usba_device->usb_client_flags[usba_get_ifno(dip)]); 620 621 if (matches == 0) { 622 usba_device-> 623 usb_client_flags[usba_get_ifno(dip)] &= 624 ~USBA_CLIENT_FLAG_DEV_DATA; 625 } 626 mutex_exit(&usba_device->usb_mutex); 627 628 kmem_free(reg, sizeof (usb_client_dev_data_t)); 629 } 630 631 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 632 "usb_free_dev_data done"); 633 } 634 635 636 /* 637 * usba_build_descr_tree: 638 * This builds the descriptor tree. See module header comment for tree 639 * description. 640 * 641 * Arguments: 642 * dip - devinfo pointer - cannot be NULL. 643 * usba_device - pointer to usba_device structure. 644 * usb_reg - pointer to area returned to client describing device. 645 * number of configuration (dev_n_cfg) and array of 646 * configurations (dev_cfg) are initialized here - 647 * dev_parse_level used and may be modified to fit 648 * current configuration. 649 * Return values: 650 * USB_SUCCESS - Tree build succeeded 651 * USB_INVALID_ARGS - dev_parse_level in usb_reg is invalid. 652 * USB_FAILURE - Bad descriptor info or other internal failure 653 */ 654 static int 655 usba_build_descr_tree(dev_info_t *dip, usba_device_t *usba_device, 656 usb_client_dev_data_t *usb_reg) 657 { 658 usba_reg_state_t state; /* State of tree construction */ 659 int cfg_len_so_far = 0; /* Bytes found, this config. */ 660 uint8_t *last_byte; /* Ptr to the end of the cfg cloud. */ 661 uint_t this_cfg_ndx; /* Configuration counter. */ 662 uint_t high_cfg_bound; /* High config index + 1. */ 663 uint_t low_cfg_bound; /* Low config index. */ 664 boolean_t process_this_if_tree = B_FALSE; /* Save alts, eps, */ 665 /* of this interface. */ 666 667 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 668 "usba_build_descr_tree starting"); 669 670 bzero(&state, sizeof (usba_reg_state_t)); 671 state.dip = dip; 672 673 /* 674 * Set config(s) and interface(s) to parse based on parse level. 675 * Adjust parse_level according to which configs and interfaces are 676 * made available by the device. 677 */ 678 state.st_dev_parse_level = usb_reg->dev_parse_level; 679 if (usba_set_parse_values(dip, usba_device, &state) != USB_SUCCESS) { 680 681 return (USB_INVALID_ARGS); 682 } 683 usb_reg->dev_parse_level = state.st_dev_parse_level; 684 685 /* Preallocate configurations based on parse level. */ 686 if (usb_reg->dev_parse_level == USB_PARSE_LVL_ALL) { 687 usb_reg->dev_n_cfg = usba_device->usb_n_cfgs; 688 low_cfg_bound = 0; 689 high_cfg_bound = usba_device->usb_n_cfgs; 690 } else { 691 usb_reg->dev_n_cfg = 1; 692 mutex_enter(&usba_device->usb_mutex); 693 low_cfg_bound = usba_device->usb_active_cfg_ndx; 694 high_cfg_bound = usba_device->usb_active_cfg_ndx + 1; 695 mutex_exit(&usba_device->usb_mutex); 696 } 697 usb_reg->dev_cfg = state.st_dev_cfg = kmem_zalloc( 698 (usb_reg->dev_n_cfg * sizeof (usb_cfg_data_t)), 699 KM_SLEEP); 700 /* 701 * this_cfg_ndx loops through all configurations presented; 702 * state.st_dev_n_cfg limits the cfgs checked to the number desired. 703 */ 704 state.st_dev_n_cfg = 0; 705 for (this_cfg_ndx = low_cfg_bound; this_cfg_ndx < high_cfg_bound; 706 this_cfg_ndx++) { 707 708 state.st_curr_raw_descr = 709 usba_device->usb_cfg_array[this_cfg_ndx]; 710 ASSERT(state.st_curr_raw_descr != NULL); 711 712 /* Clear the following for config cloud sanity checking. */ 713 last_byte = NULL; 714 state.st_curr_cfg = NULL; 715 state.st_curr_if = NULL; 716 state.st_curr_alt = NULL; 717 state.st_curr_ep = NULL; 718 719 do { 720 /* All descr have length and type at offset 0 and 1 */ 721 state.st_curr_raw_descr_len = 722 state.st_curr_raw_descr[0]; 723 state.st_curr_raw_descr_type = 724 state.st_curr_raw_descr[1]; 725 726 /* First descr in cloud must be a config descr. */ 727 if ((last_byte == NULL) && 728 (state.st_curr_raw_descr_type != 729 USB_DESCR_TYPE_CFG)) { 730 731 return (USB_FAILURE); 732 } 733 734 /* 735 * Bomb if we don't find a new cfg descr when expected. 736 * cfg_len_so_far = total_cfg_length = 0 1st time thru. 737 */ 738 if (cfg_len_so_far > state.st_total_cfg_length) { 739 USB_DPRINTF_L2(DPRINT_MASK_ALL, 740 usbai_reg_log_handle, 741 "usba_build_descr_tree: Configuration (%d) " 742 "larger than wTotalLength (%d).", 743 cfg_len_so_far, state.st_total_cfg_length); 744 745 return (USB_FAILURE); 746 } 747 748 USB_DPRINTF_L3(DPRINT_MASK_REGISTER, 749 usbai_reg_log_handle, 750 "usba_build_descr_tree: Process type %d descr " 751 "(addr=0x%p)", state.st_curr_raw_descr_type, 752 state.st_curr_raw_descr); 753 754 switch (state.st_curr_raw_descr_type) { 755 case USB_DESCR_TYPE_CFG: 756 cfg_len_so_far = 0; 757 process_this_if_tree = B_FALSE; 758 759 state.st_curr_cfg_str = usba_device-> 760 usb_cfg_str_descr[this_cfg_ndx]; 761 usba_process_cfg_descr(&state); 762 state.st_last_processed_descr_type = 763 USB_DESCR_TYPE_CFG; 764 last_byte = state.st_curr_raw_descr + 765 (state.st_total_cfg_length * 766 sizeof (uchar_t)); 767 768 break; 769 770 case USB_DESCR_TYPE_IF: 771 /* 772 * process_this_if_tree == TRUE means this 773 * interface, plus all eps and c/vs in it are 774 * to be processed. 775 */ 776 if (usba_process_if_descr(&state, 777 &process_this_if_tree) != USB_SUCCESS) { 778 779 return (USB_FAILURE); 780 } 781 state.st_last_processed_descr_type = 782 USB_DESCR_TYPE_IF; 783 784 break; 785 786 case USB_DESCR_TYPE_EP: 787 /* 788 * Skip if endpoints of a specific interface are 789 * desired and this endpoint is associated with 790 * a different interface. 791 */ 792 if (process_this_if_tree) { 793 if (usba_process_ep_descr(&state) != 794 USB_SUCCESS) { 795 796 return (USB_FAILURE); 797 } 798 state.st_last_processed_descr_type = 799 USB_DESCR_TYPE_EP; 800 } 801 802 break; 803 804 case USB_DESCR_TYPE_STRING: 805 USB_DPRINTF_L2(DPRINT_MASK_ALL, 806 usbai_reg_log_handle, 807 "usb_get_dev_data: " 808 "Found unexpected str descr at addr 0x%p", 809 state.st_curr_raw_descr); 810 811 break; /* Shouldn't be any here. Skip. */ 812 813 default: 814 /* 815 * Treat all other descr as class/vendor 816 * specific. Skip if c/vs of a specific 817 * interface are desired and this c/v is 818 * associated with a different one. 819 */ 820 if (process_this_if_tree == B_TRUE) { 821 if (usba_process_cv_descr(&state) != 822 USB_SUCCESS) { 823 824 return (USB_FAILURE); 825 } 826 } 827 } 828 829 state.st_curr_raw_descr += state.st_curr_raw_descr_len; 830 cfg_len_so_far += state.st_curr_raw_descr_len; 831 832 } while (state.st_curr_raw_descr < last_byte); 833 } 834 835 /* Make tree sparse, and put elements in order. */ 836 usba_order_tree(&state); 837 838 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 839 "usba_build_descr_tree done"); 840 841 return (USB_SUCCESS); 842 } 843 844 845 /* 846 * usba_process_cfg_descr: 847 * Set up a configuration tree node based on a raw config descriptor. 848 * 849 * Arguments: 850 * state - Pointer to this module's state structure. 851 * 852 * Returns: 853 * B_TRUE: the descr processed corresponds to a requested configuration. 854 * B_FALSE: the descr processed does not correspond to a requested config. 855 */ 856 static void 857 usba_process_cfg_descr(usba_reg_state_t *state) 858 { 859 usb_cfg_data_t *curr_cfg; 860 861 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 862 "usba_process_cfg_descr starting"); 863 864 curr_cfg = state->st_curr_cfg = 865 &state->st_dev_cfg[state->st_dev_n_cfg++]; 866 867 /* Parse and store config descriptor proper in the tree. */ 868 (void) usb_parse_data("2cs5c", 869 state->st_curr_raw_descr, state->st_curr_raw_descr_len, 870 &curr_cfg->cfg_descr, 871 sizeof (usb_cfg_descr_t)); 872 873 state->st_total_cfg_length = curr_cfg->cfg_descr.wTotalLength; 874 875 if (state->st_curr_cfg_str != NULL) { 876 curr_cfg->cfg_strsize = strlen(state->st_curr_cfg_str) + 1; 877 curr_cfg->cfg_str = kmem_zalloc(curr_cfg->cfg_strsize, 878 KM_SLEEP); 879 (void) strcpy(curr_cfg->cfg_str, state->st_curr_cfg_str); 880 } 881 882 curr_cfg->cfg_n_if = curr_cfg->cfg_descr.bNumInterfaces; 883 curr_cfg->cfg_if = kmem_zalloc((curr_cfg->cfg_n_if * 884 sizeof (usb_if_data_t)), KM_SLEEP); 885 886 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 887 "usba_process_cfg_descr done"); 888 } 889 890 891 /* 892 * usba_process_if_descr: 893 * This processes a raw interface descriptor, and sets up an analogous 894 * interface node and child "alternate" nodes (each containing an 895 * interface descriptor) in the descriptor tree. 896 * 897 * It groups all descriptors with the same bInterfaceNumber (alternates) 898 * into an array. It makes entries in an interface array, each of which 899 * points to an array of alternates. 900 * 901 * Arguments: 902 * state - Pointer to this module's state structure. 903 * requested_if - Address into which the following is returned: 904 * B_TRUE - the processed descr is of a requested interface. 905 * B_FALSE - the processed descr if of a non-requested interface. 906 * 907 * Returns: 908 * USB_SUCCESS: Descriptor is successfully parsed. 909 * USB_FAILURE: Descriptor is inappropriately placed in config cloud. 910 */ 911 static int 912 usba_process_if_descr(usba_reg_state_t *state, boolean_t *requested_if) 913 { 914 char *string; 915 usb_if_descr_t *new_if_descr; 916 usba_device_t *usba_device = usba_get_usba_device(state->dip); 917 int is_root_hub = (usba_device->usb_addr == ROOT_HUB_ADDR); 918 919 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 920 "usba_process_if_descr starting"); 921 922 /* No config preceeds this interface. */ 923 if (state->st_curr_cfg == NULL) { 924 USB_DPRINTF_L2(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 925 "usba_process_if_descr found interface after no config."); 926 927 return (USB_FAILURE); 928 } 929 930 new_if_descr = kmem_zalloc(sizeof (usb_if_descr_t), KM_SLEEP); 931 932 /* Strictly speaking, unpacking is not necessary. Could use bcopy. */ 933 (void) usb_parse_data("9c", state->st_curr_raw_descr, 934 state->st_curr_raw_descr_len, 935 new_if_descr, sizeof (usb_if_descr_t)); 936 937 /* Check the interface number in case of a malfunction device */ 938 if (new_if_descr->bInterfaceNumber >= state->st_curr_cfg->cfg_n_if) { 939 USB_DPRINTF_L2(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 940 "usba_process_if_descr: bInterfaceNumber=%d is not " 941 "a valid one", new_if_descr->bInterfaceNumber); 942 kmem_free(new_if_descr, sizeof (usb_if_descr_t)); 943 944 return (USB_FAILURE); 945 } 946 *requested_if = B_TRUE; 947 948 /* Not a requested interface. */ 949 if ((state->st_if_to_build != new_if_descr->bInterfaceNumber) && 950 (state->st_if_to_build != USBA_ALL)) { 951 *requested_if = B_FALSE; 952 953 } else { 954 usb_alt_if_data_t *alt_array; 955 uint_t alt_index; 956 957 /* Point to proper interface node, based on num in descr. */ 958 state->st_curr_if = 959 &state->st_curr_cfg->cfg_if[new_if_descr->bInterfaceNumber]; 960 961 /* Make room for new alternate. */ 962 alt_index = state->st_curr_if->if_n_alt; 963 alt_array = state->st_curr_if->if_alt; 964 usba_augment_array((void **)(&alt_array), alt_index, 965 sizeof (usb_alt_if_data_t)); 966 967 /* Ptr to the current alt, may be used to attach a c/v to it. */ 968 state->st_curr_alt = &alt_array[alt_index]; 969 970 bcopy(new_if_descr, &(alt_array[alt_index++].altif_descr), 971 sizeof (usb_if_descr_t)); 972 state->st_curr_if->if_alt = alt_array; 973 state->st_curr_if->if_n_alt = alt_index; 974 975 string = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP); 976 if (!is_root_hub) { 977 (void) usb_get_string_descr(state->dip, USB_LANG_ID, 978 state->st_curr_alt->altif_descr.iInterface, 979 string, USB_MAXSTRINGLEN); 980 } 981 if (string[0] == '\0') { 982 (void) strcpy(string, "<none>"); 983 } 984 state->st_curr_alt->altif_strsize = strlen(string) + 1; 985 state->st_curr_alt->altif_str = kmem_zalloc( 986 state->st_curr_alt->altif_strsize, KM_SLEEP); 987 (void) strcpy(state->st_curr_alt->altif_str, string); 988 kmem_free(string, USB_MAXSTRINGLEN); 989 } 990 991 kmem_free(new_if_descr, sizeof (usb_if_descr_t)); 992 993 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 994 "usba_process_if_descr done"); 995 996 return (USB_SUCCESS); 997 } 998 999 1000 /* 1001 * usba_process_ep_descr: 1002 * This processes a raw endpoint descriptor, and sets up an analogous 1003 * endpoint descriptor node in the descriptor tree. 1004 * 1005 * Arguments: 1006 * state - Pointer to this module's state structure. 1007 * 1008 * Returns: 1009 * USB_SUCCESS: Descriptor is successfully parsed. 1010 * USB_FAILURE: Descriptor is inappropriately placed in config cloud. 1011 */ 1012 static int 1013 usba_process_ep_descr(usba_reg_state_t *state) 1014 { 1015 usb_alt_if_data_t *curr_alt = state->st_curr_alt; 1016 1017 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1018 "usba_process_ep_descr starting"); 1019 1020 /* No interface preceeds this endpoint. */ 1021 if (state->st_curr_alt == NULL) { 1022 USB_DPRINTF_L2(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1023 "usba_process_ep_descr: no requested alt before endpt."); 1024 1025 return (USB_FAILURE); 1026 } 1027 1028 usba_augment_array((void **)(&curr_alt->altif_ep), 1029 curr_alt->altif_n_ep, sizeof (usb_ep_data_t)); 1030 1031 /* Ptr to the current endpt, may be used to attach a c/v to it. */ 1032 state->st_curr_ep = &curr_alt->altif_ep[curr_alt->altif_n_ep++]; 1033 1034 (void) usb_parse_data("4csc", state->st_curr_raw_descr, 1035 state->st_curr_raw_descr_len, 1036 &state->st_curr_ep->ep_descr, sizeof (usb_ep_descr_t)); 1037 1038 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1039 "usba_process_ep_descr done"); 1040 1041 return (USB_SUCCESS); 1042 } 1043 1044 1045 /* 1046 * usba_process_cv_descr: 1047 * This processes a raw endpoint descriptor, and sets up an analogous 1048 * endpoint descriptor in the descriptor tree. C/Vs are associated with 1049 * other descriptors they follow in the raw data. 1050 * last_processed_descr_type indicates the type of descr this c/v follows. 1051 * 1052 * Arguments: 1053 * state - Pointer to this module's state structure. 1054 * 1055 * Returns: 1056 * USB_SUCCESS: Descriptor is successfully parsed. 1057 * USB_FAILURE: Descriptor is inappropriately placed in config cloud. 1058 */ 1059 static int 1060 usba_process_cv_descr(usba_reg_state_t *state) 1061 { 1062 usb_cvs_data_t *curr_cv_descr; 1063 usb_cvs_data_t **cvs_ptr = NULL; 1064 uint_t *n_cvs_ptr; 1065 1066 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1067 "usba_process_cv_descr starting. Processing c/v for descr type %d", 1068 state->st_last_processed_descr_type); 1069 1070 /* 1071 * Attach the c/v to a node based on the last descr type processed. 1072 * Save handles to appropriate c/v node array and count to update. 1073 */ 1074 switch (state->st_last_processed_descr_type) { 1075 case USB_DESCR_TYPE_CFG: 1076 n_cvs_ptr = &state->st_curr_cfg->cfg_n_cvs; 1077 cvs_ptr = &state->st_curr_cfg->cfg_cvs; 1078 break; 1079 1080 case USB_DESCR_TYPE_IF: 1081 n_cvs_ptr = &state->st_curr_alt->altif_n_cvs; 1082 cvs_ptr = &state->st_curr_alt->altif_cvs; 1083 break; 1084 1085 case USB_DESCR_TYPE_EP: 1086 n_cvs_ptr = &state->st_curr_ep->ep_n_cvs; 1087 cvs_ptr = &state->st_curr_ep->ep_cvs; 1088 break; 1089 1090 default: 1091 USB_DPRINTF_L2(DPRINT_MASK_ALL, usbai_reg_log_handle, 1092 "usba_process_cv_descr: Type of last descriptor unknown. "); 1093 1094 return (USB_FAILURE); 1095 } 1096 1097 usba_augment_array((void **)cvs_ptr, *n_cvs_ptr, 1098 sizeof (usb_cvs_data_t)); 1099 curr_cv_descr = &(*cvs_ptr)[(*n_cvs_ptr)++]; 1100 1101 curr_cv_descr->cvs_buf = 1102 kmem_zalloc(state->st_curr_raw_descr_len, KM_SLEEP); 1103 curr_cv_descr->cvs_buf_len = state->st_curr_raw_descr_len; 1104 bcopy(state->st_curr_raw_descr, curr_cv_descr->cvs_buf, 1105 state->st_curr_raw_descr_len); 1106 1107 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1108 "usba_process_cv_descr done"); 1109 1110 return (USB_SUCCESS); 1111 } 1112 1113 1114 /* 1115 * usba_set_parse_values: 1116 * Based on parse level, set the configuration(s) and interface(s) to build 1117 * 1118 * Returned configuration value can be USBA_ALL indicating to build all 1119 * configurations. Likewise for the returned interface value. 1120 * 1121 * Arguments: 1122 * dip - pointer to devinfo of the device 1123 * usba_device - pointer to usba_device structure of the device 1124 * state - Pointer to this module's state structure. 1125 * if no specific config specified, default to all config 1126 * if no specific interface specified, default to all. 1127 * if_to_build and config_to_build are modified. 1128 * dev_parse_level may be modified. 1129 * 1130 * Returns: 1131 * USB_SUCCESS - success 1132 * USB_INVALID_ARGS - state->st_dev_parse_level is invalid. 1133 */ 1134 static int 1135 usba_set_parse_values(dev_info_t *dip, usba_device_t *usba_device, 1136 usba_reg_state_t *state) 1137 { 1138 /* Default to *all* in case configuration# prop not set. */ 1139 mutex_enter(&usba_device->usb_mutex); 1140 state->st_cfg_to_build = usba_device->usb_active_cfg_ndx; 1141 mutex_exit(&usba_device->usb_mutex); 1142 if (state->st_cfg_to_build == USBA_DEV_CONFIG_INDEX_UNDEFINED) { 1143 state->st_cfg_to_build = USBA_ALL; 1144 } 1145 state->st_if_to_build = usb_get_if_number(dip); 1146 1147 switch (state->st_dev_parse_level) { 1148 case USB_PARSE_LVL_ALL: /* Parse all configurations */ 1149 state->st_cfg_to_build = USBA_ALL; 1150 state->st_if_to_build = USBA_ALL; 1151 break; 1152 1153 case USB_PARSE_LVL_CFG: /* Parse all interfaces of a */ 1154 /* specific configuration. */ 1155 state->st_if_to_build = USBA_ALL; 1156 break; 1157 1158 case USB_PARSE_LVL_IF: /* Parse configured interface only */ 1159 if (state->st_if_to_build < 0) { 1160 state->st_if_to_build = USBA_ALL; 1161 } 1162 break; 1163 1164 default: 1165 1166 return (USB_INVALID_ARGS); 1167 } 1168 1169 /* 1170 * Set parse level to identify this tree properly, regardless of what 1171 * the caller thought the tree would have. 1172 */ 1173 if ((state->st_if_to_build == USBA_ALL) && 1174 (state->st_dev_parse_level == USB_PARSE_LVL_IF)) { 1175 state->st_dev_parse_level = USB_PARSE_LVL_CFG; 1176 } 1177 if ((state->st_cfg_to_build == USBA_ALL) && 1178 (state->st_dev_parse_level == USB_PARSE_LVL_CFG)) { 1179 state->st_dev_parse_level = USB_PARSE_LVL_ALL; 1180 } 1181 1182 return (USB_SUCCESS); 1183 } 1184 1185 1186 /* 1187 * usba_kmem_realloc: 1188 * Resize dynamic memory. Copy contents of old area to 1189 * beginning of new area. 1190 * 1191 * Arguments: 1192 * old_mem - pointer to old memory area. 1193 * old_size - size of old memory area. 0 is OK. 1194 * new_size - size desired. 1195 * 1196 * Returns: 1197 * pointer to new memory area. 1198 */ 1199 static void* 1200 usba_kmem_realloc(void* old_mem, int old_size, int new_size) 1201 { 1202 void *new_mem = NULL; 1203 1204 if (new_size > 0) { 1205 new_mem = kmem_zalloc(new_size, KM_SLEEP); 1206 if (old_size > 0) { 1207 bcopy(old_mem, new_mem, 1208 min(old_size, new_size)); 1209 } 1210 } 1211 1212 if (old_size > 0) { 1213 kmem_free(old_mem, old_size); 1214 } 1215 1216 return (new_mem); 1217 } 1218 1219 1220 /* 1221 * usba_augment_array: 1222 * Add a new element on the end of an array. 1223 * 1224 * Arguments: 1225 * addr - ptr to the array address. Array addr will change. 1226 * n_elements - array element count. 1227 * element_size - size of an array element 1228 */ 1229 static void 1230 usba_augment_array(void **addr, uint_t n_elements, uint_t element_size) 1231 { 1232 *addr = usba_kmem_realloc(*addr, (n_elements * element_size), 1233 ((n_elements + 1) * element_size)); 1234 } 1235 1236 1237 /* 1238 * usba_make_alts_sparse: 1239 * Disburse alternate array elements such that they are at the proper array 1240 * indices for which alt they represent. It is assumed that all key values 1241 * used for ordering the elements are positive. Original array space may 1242 * be freed and new space allocated. 1243 * 1244 * Arguments: 1245 * array - pointer to alternates array; may be modified 1246 * n_elements - number of elements in the array; may be modified 1247 */ 1248 static void 1249 usba_make_alts_sparse(usb_alt_if_data_t **array, uint_t *n_elements) 1250 { 1251 uint_t n_orig_elements = *n_elements; 1252 uint8_t smallest_value; 1253 uint8_t largest_value; 1254 uint8_t curr_value; 1255 uint_t in_order = 0; 1256 usb_alt_if_data_t *orig_addr = *array; /* Non-sparse array base ptr */ 1257 usb_alt_if_data_t *repl_array; /* Base ptr to sparse array */ 1258 uint_t n_repl_elements; /* Number elements in the new array */ 1259 uint_t i; 1260 1261 /* Check for a null array. */ 1262 if ((array == NULL) || (n_orig_elements == 0)) { 1263 1264 return; 1265 } 1266 1267 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1268 "make_sparse: array=0x%p, n_orig_elements=%d", 1269 array, n_orig_elements); 1270 1271 curr_value = orig_addr[0].altif_descr.bAlternateSetting; 1272 smallest_value = largest_value = curr_value; 1273 1274 /* Figure the low-high range of the array. */ 1275 for (i = 1; i < n_orig_elements; i++) { 1276 curr_value = orig_addr[i].altif_descr.bAlternateSetting; 1277 if (curr_value < smallest_value) { 1278 smallest_value = curr_value; 1279 } else if (curr_value > largest_value) { 1280 in_order++; 1281 largest_value = curr_value; 1282 } 1283 } 1284 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1285 "make_sparse: largest=%d, smallest=%d, " 1286 "order=%d", 1287 largest_value, smallest_value, in_order); 1288 1289 n_repl_elements = largest_value + 1; 1290 1291 /* 1292 * No holes to leave, array starts at zero, and everything is already 1293 * in order. Just return original array. 1294 */ 1295 if ((n_repl_elements == n_orig_elements) && 1296 ((in_order + 1) == n_orig_elements)) { 1297 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1298 "No holes"); 1299 1300 return; 1301 } 1302 1303 /* Allocate zeroed space for the array. */ 1304 repl_array = kmem_zalloc( 1305 (n_repl_elements * sizeof (usb_alt_if_data_t)), KM_SLEEP); 1306 1307 /* Now fill in the array. */ 1308 for (i = 0; i < n_orig_elements; i++) { 1309 curr_value = orig_addr[i].altif_descr.bAlternateSetting; 1310 1311 /* Place in sparse array based on key. */ 1312 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1313 "move %d bytes (key %d) from 0x%p to 0x%p", 1314 sizeof (usb_alt_if_data_t), curr_value, &orig_addr[i], 1315 &repl_array[curr_value]); 1316 1317 bcopy((char *)&orig_addr[i], (char *)&repl_array[curr_value], 1318 sizeof (usb_alt_if_data_t)); 1319 } 1320 1321 kmem_free(*array, sizeof (usb_alt_if_data_t) * n_orig_elements); 1322 *array = repl_array; 1323 *n_elements = n_repl_elements; 1324 } 1325 1326 1327 /* 1328 * usba_order_tree: 1329 * Take a tree as built by usba_build_descr_tree and make sure the key 1330 * values of all elements match their indeces. Proper order is implied. 1331 * 1332 * Arguments: 1333 * state - Pointer to this module's state structure. 1334 */ 1335 static void 1336 usba_order_tree(usba_reg_state_t *state) 1337 { 1338 usb_cfg_data_t *this_cfg; 1339 usb_if_data_t *this_if; 1340 uint_t n_cfgs = state->st_dev_n_cfg; 1341 uint_t cfg; 1342 uint_t which_if; 1343 1344 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1345 "usba_order_tree:"); 1346 1347 for (cfg = 0; cfg < n_cfgs; cfg++) { 1348 this_cfg = &state->st_dev_cfg[cfg]; 1349 1350 for (which_if = 0; which_if < this_cfg->cfg_n_if; which_if++) { 1351 this_if = this_cfg->cfg_if; 1352 usba_make_alts_sparse(&this_if->if_alt, 1353 &this_if->if_n_alt); 1354 } 1355 } 1356 } 1357 1358 1359 /* 1360 * usb_free_descr_tree: 1361 * Take down the configuration tree. Called internally and can be called 1362 * from a driver standalone to take the tree down while leaving the rest 1363 * of the registration intact. 1364 * 1365 * Arguments: 1366 * dip - pointer to devinfo of the device 1367 * dev_data - pointer to registration data containing the tree. 1368 */ 1369 void 1370 usb_free_descr_tree(dev_info_t *dip, usb_client_dev_data_t *dev_data) 1371 { 1372 usb_cfg_data_t *cfg_array; 1373 int n_cfgs; 1374 int cfg; 1375 1376 if ((dip == NULL) || (dev_data == NULL)) { 1377 1378 return; 1379 } 1380 cfg_array = dev_data->dev_cfg; 1381 n_cfgs = dev_data->dev_n_cfg; 1382 1383 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1384 "usb_free_descr_tree starting for %s%d", 1385 ddi_driver_name(dip), ddi_get_instance(dip)); 1386 1387 for (cfg = 0; cfg < n_cfgs; cfg++) { 1388 if (cfg_array[cfg].cfg_if) { 1389 usba_free_if_array(cfg_array[cfg].cfg_if, 1390 cfg_array[cfg].cfg_n_if); 1391 } 1392 if (cfg_array[cfg].cfg_cvs) { 1393 usba_free_cv_array(cfg_array[cfg].cfg_cvs, 1394 cfg_array[cfg].cfg_n_cvs); 1395 } 1396 if (cfg_array[cfg].cfg_str) { 1397 kmem_free(cfg_array[cfg].cfg_str, 1398 cfg_array[cfg].cfg_strsize); 1399 } 1400 } 1401 1402 if (cfg_array) { 1403 kmem_free(cfg_array, (sizeof (usb_cfg_data_t) * n_cfgs)); 1404 } 1405 1406 dev_data->dev_parse_level = USB_PARSE_LVL_NONE; 1407 dev_data->dev_n_cfg = 0; 1408 dev_data->dev_cfg = NULL; 1409 dev_data->dev_curr_cfg = NULL; 1410 1411 USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle, 1412 "usb_free_descr_tree done"); 1413 } 1414 1415 1416 /* 1417 * usba_free_if_array: 1418 * Free a configuration's array of interface nodes and their subtrees of 1419 * interface alternate, endpoint and c/v descriptors. 1420 * 1421 * Arguments: 1422 * if_array - pointer to array of interfaces to remove. 1423 * n_ifs - number of elements in the array to remove. 1424 */ 1425 static void 1426 usba_free_if_array(usb_if_data_t *if_array, uint_t n_ifs) 1427 { 1428 uint_t which_if; 1429 uint_t which_alt; 1430 uint_t n_alts; 1431 usb_alt_if_data_t *altif; 1432 1433 for (which_if = 0; which_if < n_ifs; which_if++) { 1434 n_alts = if_array[which_if].if_n_alt; 1435 1436 /* Every interface has at least one alternate. */ 1437 for (which_alt = 0; which_alt < n_alts; which_alt++) { 1438 altif = &if_array[which_if].if_alt[which_alt]; 1439 usba_free_ep_array(altif->altif_ep, altif->altif_n_ep); 1440 usba_free_cv_array(altif->altif_cvs, 1441 altif->altif_n_cvs); 1442 kmem_free(altif->altif_str, altif->altif_strsize); 1443 } 1444 1445 kmem_free(if_array[which_if].if_alt, 1446 (sizeof (usb_alt_if_data_t) * n_alts)); 1447 } 1448 1449 /* Free the interface array itself. */ 1450 kmem_free(if_array, (sizeof (usb_if_data_t) * n_ifs)); 1451 } 1452 1453 1454 /* 1455 * usba_free_ep_array: 1456 * Free an array of endpoint nodes and their subtrees of c/v descriptors. 1457 * 1458 * Arguments: 1459 * ep_array - pointer to array of endpoints to remove. 1460 * n_eps - number of elements in the array to remove. 1461 */ 1462 static void 1463 usba_free_ep_array(usb_ep_data_t *ep_array, uint_t n_eps) 1464 { 1465 uint_t ep; 1466 1467 for (ep = 0; ep < n_eps; ep++) { 1468 usba_free_cv_array(ep_array[ep].ep_cvs, ep_array[ep].ep_n_cvs); 1469 } 1470 1471 kmem_free(ep_array, (sizeof (usb_ep_data_t) * n_eps)); 1472 } 1473 1474 1475 /* 1476 * usba_free_cv_array: 1477 * Free an array of class/vendor (c/v) descriptor nodes. 1478 * 1479 * Arguments: 1480 * cv_array - pointer to array of c/v nodes to remove. 1481 * n_cvs - number of elements in the array to remove. 1482 */ 1483 static void 1484 usba_free_cv_array(usb_cvs_data_t *cv_array, uint_t n_cvs) 1485 { 1486 uint_t cv_node; 1487 1488 /* Free data areas hanging off of each c/v descriptor. */ 1489 for (cv_node = 0; cv_node < n_cvs; cv_node++) { 1490 kmem_free(cv_array[cv_node].cvs_buf, 1491 cv_array[cv_node].cvs_buf_len); 1492 } 1493 1494 /* Free the array of cv descriptors. */ 1495 kmem_free(cv_array, (sizeof (usb_cvs_data_t) * n_cvs)); 1496 } 1497 1498 1499 /* 1500 * usb_log_descr_tree: 1501 * Log to the usba_debug_buf a descriptor tree as returned by 1502 * usbai_register_client. 1503 * 1504 * Arguments: 1505 * dev_data - pointer to registration area containing the tree 1506 * log_handle - pointer to log handle to use for dumping. 1507 * level - print level, one of USB_LOG_L0 ... USB_LOG_L4 1508 * Please see usb_log(9F) for details. 1509 * mask - print mask. Please see usb_log(9F) for details. 1510 * 1511 * Returns: 1512 * USB_SUCCESS - tree successfully dumped 1513 * USB_INVALID_CONTEXT - called from callback context 1514 * USB_INVALID_ARGS - bad arguments given 1515 */ 1516 int 1517 usb_log_descr_tree(usb_client_dev_data_t *dev_data, 1518 usb_log_handle_t log_handle, uint_t level, uint_t mask) 1519 { 1520 return (usba_dump_descr_tree(NULL, dev_data, log_handle, level, mask)); 1521 } 1522 1523 1524 /* 1525 * usb_print_descr_tree: 1526 * Print to the screen a descriptor tree as returned by 1527 * usbai_register_client. 1528 * 1529 * Arguments: 1530 * dip - pointer to devinfo of the client 1531 * dev_data - pointer to registration area containing the tree 1532 * 1533 * Returns: 1534 * USB_SUCCESS - tree successfully dumped 1535 * USB_INVALID_CONTEXT - called from callback context 1536 * USB_INVALID_ARGS - bad arguments given 1537 */ 1538 int 1539 usb_print_descr_tree(dev_info_t *dip, usb_client_dev_data_t *dev_data) 1540 { 1541 return (usba_dump_descr_tree(dip, dev_data, NULL, 0, 0)); 1542 } 1543 1544 1545 /* 1546 * usba_dump_descr_tree: 1547 * Dump a descriptor tree. 1548 * 1549 * Arguments: 1550 * dip - pointer to devinfo of the client. Used when no 1551 * log_handle argument given. 1552 * usb_reg - pointer to registration area containing the tree 1553 * log_handle - pointer to log handle to use for dumping. If NULL, 1554 * use internal log handle, which dumps to screen. 1555 * level - print level, one of USB_LOG_L0 ... USB_LOG_L4 1556 * Used only when log_handle provided. 1557 * mask - print mask, used when log_handle argument provided. 1558 * 1559 * Returns: 1560 * USB_SUCCESS - tree successfully dumped 1561 * USB_INVALID_CONTEXT - called from callback context 1562 * USB_INVALID_ARGS - bad arguments given 1563 */ 1564 static int 1565 usba_dump_descr_tree(dev_info_t *dip, usb_client_dev_data_t *usb_reg, 1566 usb_log_handle_t log_handle, uint_t level, uint_t mask) 1567 { 1568 usb_log_handle_t dump_handle; 1569 uint_t dump_level; 1570 uint_t dump_mask; 1571 int which_config; /* Counters. */ 1572 int which_if; 1573 int which_cv; 1574 usb_cfg_data_t *config; /* ptr to current configuration tree node */ 1575 usb_cfg_descr_t *config_descr; /* and its USB descriptor. */ 1576 char *string; 1577 char *name_string = NULL; 1578 int name_string_size; 1579 1580 if ((usb_reg == NULL) || ((log_handle == NULL) && (dip == NULL))) { 1581 1582 return (USB_INVALID_ARGS); 1583 } 1584 1585 /* 1586 * To keep calling this simple, kmem_zalloc with the sleep flag always. 1587 * This means no interrupt context is allowed. 1588 */ 1589 if (servicing_interrupt()) { 1590 1591 return (USB_INVALID_CONTEXT); 1592 } 1593 1594 string = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP); 1595 1596 if (log_handle != NULL) { 1597 dump_level = level; 1598 dump_mask = mask; 1599 dump_handle = log_handle; 1600 } else { 1601 dump_level = USB_LOG_L1; 1602 dump_mask = DPRINT_MASK_ALL; 1603 1604 /* Build device name string. */ 1605 (void) snprintf(string, USB_MAXSTRINGLEN, 1606 "Port%d", usb_get_addr(dip)); 1607 name_string_size = strlen(string) + 1; 1608 name_string = kmem_zalloc(name_string_size, KM_SLEEP); 1609 (void) strcpy(name_string, string); 1610 1611 /* Allocate a log handle specifying the name string. */ 1612 dump_handle = usb_alloc_log_hdl(NULL, name_string, 1613 &dump_level, &dump_mask, NULL, 1614 USB_FLAGS_SLEEP); 1615 } 1616 1617 (void) usb_log(dump_handle, dump_level, dump_mask, 1618 "USB descriptor tree for %s %s", 1619 (usb_reg->dev_mfg != NULL ? usb_reg->dev_mfg : ""), 1620 (usb_reg->dev_product != NULL ? usb_reg->dev_product : "")); 1621 if (usb_reg->dev_n_cfg == 0) { 1622 (void) usb_log(dump_handle, dump_level, dump_mask, 1623 "No descriptor tree present"); 1624 } else { 1625 (void) usb_log(dump_handle, dump_level, dump_mask, 1626 "highest configuration found=%d", usb_reg->dev_n_cfg - 1); 1627 } 1628 1629 for (which_config = 0; which_config < usb_reg->dev_n_cfg; 1630 which_config++) { 1631 config = &usb_reg->dev_cfg[which_config]; 1632 config_descr = &config->cfg_descr; 1633 if (config_descr->bLength == 0) { 1634 1635 continue; 1636 } 1637 if (dump_level == USB_LOG_L0) { 1638 (void) usb_log(dump_handle, dump_level, dump_mask, " "); 1639 } 1640 (void) usb_log(dump_handle, dump_level, dump_mask, 1641 "Configuration #%d (Addr= 0x%p)", which_config, config); 1642 (void) usb_log(dump_handle, dump_level, dump_mask, 1643 "String descr=%s", config->cfg_str); 1644 (void) usb_log(dump_handle, dump_level, dump_mask, 1645 "config descr: len=%d tp=%d totLen=%d numIf=%d " 1646 "cfgVal=%d att=0x%x pwr=%d", 1647 config_descr->bLength, config_descr->bDescriptorType, 1648 config_descr->wTotalLength, config_descr->bNumInterfaces, 1649 config_descr->bConfigurationValue, 1650 config_descr->bmAttributes, config_descr->bMaxPower); 1651 if ((config->cfg_n_if > 0) || (config->cfg_n_cvs > 0)) { 1652 (void) usb_log(dump_handle, dump_level, dump_mask, 1653 "usb_cfg_data_t shows max if=%d " 1654 "and %d cv descr(s).", 1655 config->cfg_n_if - 1, config->cfg_n_cvs); 1656 } 1657 1658 for (which_if = 0; which_if < config->cfg_n_if; 1659 which_if++) { 1660 1661 if (dump_level == USB_LOG_L0) { 1662 (void) usb_log(dump_handle, dump_level, 1663 dump_mask, " "); 1664 } 1665 (void) usb_log(dump_handle, dump_level, dump_mask, 1666 " interface #%d (0x%p)", 1667 which_if, &config->cfg_if[which_if]); 1668 usba_dump_if(&config->cfg_if[which_if], 1669 dump_handle, dump_level, dump_mask, string); 1670 } 1671 1672 for (which_cv = 0; which_cv < config->cfg_n_cvs; which_cv++) { 1673 (void) usb_log(dump_handle, dump_level, dump_mask, 1674 " config cv descriptor %d (Address=0x%p)", 1675 which_cv, &config->cfg_cvs[which_cv]); 1676 usba_dump_cv(&config->cfg_cvs[which_cv], 1677 dump_handle, dump_level, dump_mask, string, 4); 1678 } 1679 } 1680 1681 (void) usb_log(dump_handle, dump_level, dump_mask, 1682 "Returning dev_curr_cfg:0x%p, dev_curr_if:%d", 1683 usb_reg->dev_curr_cfg, usb_reg->dev_curr_if); 1684 1685 if (log_handle == NULL) { 1686 usb_free_log_hdl(dump_handle); 1687 } 1688 if (name_string != NULL) { 1689 kmem_free(name_string, name_string_size); 1690 } 1691 kmem_free(string, USB_MAXSTRINGLEN); 1692 1693 return (USB_SUCCESS); 1694 } 1695 1696 1697 /* 1698 * usba_dump_if: 1699 * Dump an interface node and its branches. 1700 * 1701 * Arguments: 1702 * which_if - interface node to dump 1703 * dump_handle - write data through this log handle 1704 * dump_level - level passed to usb_log 1705 * dump_mask - mask passed to usb_log 1706 * string - temporary area used for processing 1707 * 1708 */ 1709 static void 1710 usba_dump_if(usb_if_data_t *which_if, usb_log_handle_t dump_handle, 1711 uint_t dump_level, uint_t dump_mask, char *string) 1712 { 1713 int which_alt; /* Number of alt being dumped */ 1714 usb_alt_if_data_t *alt; /* Pointer to it. */ 1715 usb_if_descr_t *if_descr; /* Pointer to its USB descr. */ 1716 int which_ep; /* Endpoint counter. */ 1717 int which_cv; /* C/V descr counter. */ 1718 1719 for (which_alt = 0; which_alt < which_if->if_n_alt; which_alt++) { 1720 alt = &which_if->if_alt[which_alt]; 1721 if_descr = &alt->altif_descr; 1722 1723 if (if_descr->bLength == 0) { 1724 1725 continue; 1726 } 1727 if (dump_level == USB_LOG_L0) { 1728 (void) usb_log(dump_handle, dump_level, dump_mask, " "); 1729 } 1730 (void) usb_log(dump_handle, dump_level, dump_mask, 1731 "\tAlt #%d (0x%p)", which_alt, alt); 1732 (void) usb_log(dump_handle, dump_level, dump_mask, 1733 "\tString descr=%s", alt->altif_str); 1734 (void) usb_log(dump_handle, dump_level, dump_mask, 1735 "\tif descr: len=%d type=%d if=%d alt=%d n_ept=%d " 1736 "cls=%d sub=%d proto=%d", 1737 if_descr->bLength, 1738 if_descr->bDescriptorType, if_descr->bInterfaceNumber, 1739 if_descr->bAlternateSetting, if_descr->bNumEndpoints, 1740 if_descr->bInterfaceClass, if_descr->bInterfaceSubClass, 1741 if_descr->bInterfaceProtocol); 1742 1743 if ((alt->altif_n_ep > 0) || (alt->altif_n_cvs > 0)) { 1744 (void) usb_log(dump_handle, dump_level, dump_mask, 1745 "\tusb_alt_if_data_t shows max ep=%d " 1746 "and %d cv descr(s).", 1747 alt->altif_n_ep - 1, alt->altif_n_cvs); 1748 } 1749 1750 for (which_ep = 0; which_ep < alt->altif_n_ep; 1751 which_ep++) { 1752 if (alt->altif_ep[which_ep].ep_descr.bLength == 0) { 1753 1754 continue; 1755 } 1756 if (dump_level == USB_LOG_L0) { 1757 (void) usb_log(dump_handle, dump_level, 1758 dump_mask, " "); 1759 } 1760 usba_dump_ep(which_ep, &alt->altif_ep[which_ep], 1761 dump_handle, dump_level, dump_mask, string); 1762 } 1763 1764 for (which_cv = 0; which_cv < alt->altif_n_cvs; which_cv++) { 1765 if (dump_level == USB_LOG_L0) { 1766 (void) usb_log(dump_handle, dump_level, 1767 dump_mask, " "); 1768 } 1769 (void) usb_log(dump_handle, dump_level, dump_mask, 1770 "\talt cv descriptor #%d (0x%p), size=%d", 1771 which_cv, &alt->altif_cvs[which_cv], 1772 alt->altif_cvs[which_cv].cvs_buf_len); 1773 usba_dump_cv(&alt->altif_cvs[which_cv], 1774 dump_handle, dump_level, dump_mask, string, 2); 1775 } 1776 } 1777 } 1778 1779 1780 /* 1781 * usba_dump_ep: 1782 * Dump an endpoint node and its branches. 1783 * 1784 * Arguments: 1785 * which_ep - index to display 1786 * ep - endpoint node to dump 1787 * dump_handle - write data through this log handle 1788 * dump_level - level passed to usb_log 1789 * dump_mask - mask passed to usb_log 1790 * string - temporary area used for processing 1791 * 1792 */ 1793 static void 1794 usba_dump_ep(uint_t which_ep, usb_ep_data_t *ep, usb_log_handle_t dump_handle, 1795 uint_t dump_level, uint_t dump_mask, char *string) 1796 { 1797 int which_cv; 1798 usb_ep_descr_t *ep_descr = &ep->ep_descr; 1799 1800 (void) usb_log(dump_handle, dump_level, dump_mask, 1801 "\t endpoint[%d], epaddr=0x%x (0x%p)", which_ep, 1802 ep_descr->bEndpointAddress, ep); 1803 (void) usb_log(dump_handle, dump_level, dump_mask, 1804 "\t len=%d type=%d attr=0x%x pktsize=%d interval=%d", 1805 ep_descr->bLength, ep_descr->bDescriptorType, 1806 ep_descr->bmAttributes, ep_descr->wMaxPacketSize, 1807 ep_descr->bInterval); 1808 if (ep->ep_n_cvs > 0) { 1809 (void) usb_log(dump_handle, dump_level, dump_mask, 1810 "\t usb_ep_data_t shows %d cv descr(s)", ep->ep_n_cvs); 1811 } 1812 1813 for (which_cv = 0; which_cv < ep->ep_n_cvs; which_cv++) { 1814 if (dump_level == USB_LOG_L0) { 1815 (void) usb_log(dump_handle, dump_level, 1816 dump_mask, " "); 1817 } 1818 (void) usb_log(dump_handle, dump_level, dump_mask, 1819 "\t endpoint cv descriptor %d (0x%p), size=%d", 1820 which_cv, &ep->ep_cvs[which_cv], 1821 ep->ep_cvs[which_cv].cvs_buf_len); 1822 usba_dump_cv(&ep->ep_cvs[which_cv], 1823 dump_handle, dump_level, dump_mask, string, 3); 1824 } 1825 } 1826 1827 1828 /* 1829 * usba_dump_cv: 1830 * Dump a raw class or vendor specific descriptor. 1831 * 1832 * Arguments: 1833 * cv_node - pointer to the descriptor to dump 1834 * dump_handle - write data through this log handle 1835 * dump_level - level passed to usb_log 1836 * dump_mask - mask passed to usb_log 1837 * string - temporary area used for processing 1838 * indent - number of tabs to indent output 1839 * 1840 */ 1841 static void 1842 usba_dump_cv(usb_cvs_data_t *cv_node, usb_log_handle_t dump_handle, 1843 uint_t dump_level, uint_t dump_mask, char *string, int indent) 1844 { 1845 if (cv_node) { 1846 usba_dump_bin(cv_node->cvs_buf, cv_node->cvs_buf_len, indent, 1847 dump_handle, dump_level, dump_mask, string, 1848 USB_MAXSTRINGLEN); 1849 } 1850 } 1851 1852 1853 /* 1854 * usba_dump_bin: 1855 * Generic byte dump function. 1856 * 1857 * Arguments: 1858 * data - pointer to the data to dump 1859 * max_bytes - amount of data to dump 1860 * indent - number of indentation levels 1861 * dump_handle - write data through this log handle 1862 * dump_level - level passed to usb_log 1863 * dump_mask - mask passed to usb_log 1864 * buffer - temporary area used for processing 1865 * bufferlen - size of the temporary string area 1866 * 1867 */ 1868 static void 1869 usba_dump_bin(uint8_t *data, int max_bytes, int indent, 1870 usb_log_handle_t dump_handle, uint_t dump_level, uint_t dump_mask, 1871 char *buffer, int bufferlen) 1872 { 1873 int i; 1874 int bufoffset = 0; 1875 int nexthere; 1876 1877 if ((indent * SPACES_PER_INDENT) > 1878 (bufferlen - (BINDUMP_BYTES_PER_LINE * 3))) { 1879 (void) usb_log(dump_handle, dump_level, dump_mask, 1880 "Offset to usb_dump_bin must be %d or less. " 1881 "Setting to 0.\n", 1882 (bufferlen - (BINDUMP_BYTES_PER_LINE * 3))); 1883 indent = 0; 1884 } 1885 1886 /* Assume a tab is 2 four-space units. */ 1887 for (i = 0; i < indent/2; i++) { 1888 buffer[bufoffset] = '\t'; 1889 bufoffset++; 1890 } 1891 1892 if (indent % 2) { 1893 (void) strcpy(&buffer[bufoffset], INDENT_SPACE_STR); 1894 bufoffset += SPACES_PER_INDENT; 1895 } 1896 1897 i = 0; /* Num dumped bytes put on this line. */ 1898 nexthere = bufoffset; 1899 while (i < max_bytes) { 1900 (void) sprintf(&buffer[nexthere], "%2x ", *data++); 1901 nexthere += 3; 1902 i++; 1903 if (!(i % BINDUMP_BYTES_PER_LINE)) { 1904 buffer[nexthere] = '\0'; 1905 (void) usb_log(dump_handle, dump_level, dump_mask, 1906 buffer); 1907 nexthere = bufoffset; 1908 } 1909 } 1910 1911 if (nexthere > bufoffset) { 1912 buffer[nexthere] = '\0'; 1913 (void) usb_log(dump_handle, dump_level, dump_mask, buffer); 1914 } 1915 } 1916