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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * Copyright 2005 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_L1(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 	*requested_if = B_TRUE;
931 	new_if_descr = kmem_zalloc(sizeof (usb_if_descr_t), KM_SLEEP);
932 
933 	/* Strictly speaking, unpacking is not necessary.  Could use bcopy. */
934 	(void) usb_parse_data("9c", state->st_curr_raw_descr,
935 			state->st_curr_raw_descr_len,
936 			new_if_descr, sizeof (usb_if_descr_t));
937 
938 	/* Not a requested interface. */
939 	if ((state->st_if_to_build != new_if_descr->bInterfaceNumber) &&
940 	    (state->st_if_to_build != USBA_ALL)) {
941 		*requested_if = B_FALSE;
942 
943 	} else {
944 		usb_alt_if_data_t *alt_array;
945 		uint_t		alt_index;
946 
947 		/* Point to proper interface node, based on num in descr. */
948 		state->st_curr_if =
949 		    &state->st_curr_cfg->cfg_if[new_if_descr->bInterfaceNumber];
950 
951 		/* Make room for new alternate. */
952 		alt_index = state->st_curr_if->if_n_alt;
953 		alt_array = state->st_curr_if->if_alt;
954 		usba_augment_array((void **)(&alt_array), alt_index,
955 				sizeof (usb_alt_if_data_t));
956 
957 		/* Ptr to the current alt, may be used to attach a c/v to it. */
958 		state->st_curr_alt = &alt_array[alt_index];
959 
960 		bcopy(new_if_descr, &(alt_array[alt_index++].altif_descr),
961 		    sizeof (usb_if_descr_t));
962 		state->st_curr_if->if_alt = alt_array;
963 		state->st_curr_if->if_n_alt = alt_index;
964 
965 		string = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
966 		if (!is_root_hub) {
967 			(void) usb_get_string_descr(state->dip, USB_LANG_ID,
968 			    state->st_curr_alt->altif_descr.iInterface,
969 			    string, USB_MAXSTRINGLEN);
970 		}
971 		if (string[0] == '\0') {
972 			(void) strcpy(string, "<none>");
973 		}
974 		state->st_curr_alt->altif_strsize = strlen(string) + 1;
975 		state->st_curr_alt->altif_str = kmem_zalloc(
976 		    state->st_curr_alt->altif_strsize, KM_SLEEP);
977 		(void) strcpy(state->st_curr_alt->altif_str, string);
978 		kmem_free(string, USB_MAXSTRINGLEN);
979 	}
980 
981 	kmem_free(new_if_descr, sizeof (usb_if_descr_t));
982 
983 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
984 	    "usba_process_if_descr done");
985 
986 	return (USB_SUCCESS);
987 }
988 
989 
990 /*
991  * usba_process_ep_descr:
992  *	This processes a raw endpoint descriptor, and sets up an analogous
993  *	endpoint descriptor node in the descriptor tree.
994  *
995  * Arguments:
996  *	state		- Pointer to this module's state structure.
997  *
998  * Returns:
999  *	USB_SUCCESS:	Descriptor is successfully parsed.
1000  *	USB_FAILURE:	Descriptor is inappropriately placed in config cloud.
1001  */
1002 static int
1003 usba_process_ep_descr(usba_reg_state_t *state)
1004 {
1005 	usb_alt_if_data_t *curr_alt = state->st_curr_alt;
1006 
1007 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1008 	    "usba_process_ep_descr starting");
1009 
1010 	/* No interface preceeds this endpoint. */
1011 	if (state->st_curr_alt == NULL) {
1012 		USB_DPRINTF_L2(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1013 		    "usba_process_ep_descr: no requested alt before endpt.");
1014 
1015 		return (USB_FAILURE);
1016 	}
1017 
1018 	usba_augment_array((void **)(&curr_alt->altif_ep),
1019 			curr_alt->altif_n_ep, sizeof (usb_ep_data_t));
1020 
1021 	/* Ptr to the current endpt, may be used to attach a c/v to it. */
1022 	state->st_curr_ep = &curr_alt->altif_ep[curr_alt->altif_n_ep++];
1023 
1024 	(void) usb_parse_data("4csc", state->st_curr_raw_descr,
1025 			state->st_curr_raw_descr_len,
1026 			&state->st_curr_ep->ep_descr, sizeof (usb_ep_descr_t));
1027 
1028 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1029 	    "usba_process_ep_descr done");
1030 
1031 	return (USB_SUCCESS);
1032 }
1033 
1034 
1035 /*
1036  * usba_process_cv_descr:
1037  *	This processes a raw endpoint descriptor, and sets up an analogous
1038  *	endpoint descriptor in the descriptor tree.  C/Vs are associated with
1039  *	other descriptors they follow in the raw data.
1040  *	last_processed_descr_type indicates the type of descr this c/v follows.
1041  *
1042  * Arguments:
1043  *	state		- Pointer to this module's state structure.
1044  *
1045  * Returns:
1046  *	USB_SUCCESS:	Descriptor is successfully parsed.
1047  *	USB_FAILURE:	Descriptor is inappropriately placed in config cloud.
1048  */
1049 static int
1050 usba_process_cv_descr(usba_reg_state_t *state)
1051 {
1052 	usb_cvs_data_t	*curr_cv_descr;
1053 	usb_cvs_data_t	**cvs_ptr = NULL;
1054 	uint_t		*n_cvs_ptr;
1055 
1056 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1057 	    "usba_process_cv_descr starting.  Processing c/v for descr type %d",
1058 	    state->st_last_processed_descr_type);
1059 
1060 	/*
1061 	 * Attach the c/v to a node based on the last descr type processed.
1062 	 * Save handles to appropriate c/v node array and count to update.
1063 	 */
1064 	switch (state->st_last_processed_descr_type) {
1065 	case USB_DESCR_TYPE_CFG:
1066 		n_cvs_ptr = &state->st_curr_cfg->cfg_n_cvs;
1067 		cvs_ptr = &state->st_curr_cfg->cfg_cvs;
1068 		break;
1069 
1070 	case USB_DESCR_TYPE_IF:
1071 		n_cvs_ptr = &state->st_curr_alt->altif_n_cvs;
1072 		cvs_ptr = &state->st_curr_alt->altif_cvs;
1073 		break;
1074 
1075 	case USB_DESCR_TYPE_EP:
1076 		n_cvs_ptr = &state->st_curr_ep->ep_n_cvs;
1077 		cvs_ptr = &state->st_curr_ep->ep_cvs;
1078 		break;
1079 
1080 	default:
1081 		USB_DPRINTF_L2(DPRINT_MASK_ALL, usbai_reg_log_handle,
1082 		    "usba_process_cv_descr: Type of last descriptor unknown. ");
1083 
1084 		return (USB_FAILURE);
1085 	}
1086 
1087 	usba_augment_array((void **)cvs_ptr, *n_cvs_ptr,
1088 			sizeof (usb_cvs_data_t));
1089 	curr_cv_descr = &(*cvs_ptr)[(*n_cvs_ptr)++];
1090 
1091 	curr_cv_descr->cvs_buf =
1092 			kmem_zalloc(state->st_curr_raw_descr_len, KM_SLEEP);
1093 	curr_cv_descr->cvs_buf_len = state->st_curr_raw_descr_len;
1094 	bcopy(state->st_curr_raw_descr, curr_cv_descr->cvs_buf,
1095 	    state->st_curr_raw_descr_len);
1096 
1097 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1098 	    "usba_process_cv_descr done");
1099 
1100 	return (USB_SUCCESS);
1101 }
1102 
1103 
1104 /*
1105  * usba_set_parse_values:
1106  *	Based on parse level, set the configuration(s) and interface(s) to build
1107  *
1108  *	Returned configuration value can be USBA_ALL indicating to build all
1109  *	configurations.  Likewise for the returned interface value.
1110  *
1111  * Arguments:
1112  *	dip		- pointer to devinfo of the device
1113  *	usba_device	- pointer to usba_device structure of the device
1114  *	state		- Pointer to this module's state structure.
1115  *			  if no specific config specified, default to all config
1116  *			  if no specific interface specified, default to all.
1117  *			  if_to_build and config_to_build are modified.
1118  *			  dev_parse_level may be modified.
1119  *
1120  * Returns:
1121  *	USB_SUCCESS	- success
1122  *	USB_INVALID_ARGS - state->st_dev_parse_level is invalid.
1123  */
1124 static int
1125 usba_set_parse_values(dev_info_t *dip, usba_device_t *usba_device,
1126     usba_reg_state_t *state)
1127 {
1128 	/* Default to *all* in case configuration# prop not set. */
1129 	mutex_enter(&usba_device->usb_mutex);
1130 	state->st_cfg_to_build = usba_device->usb_active_cfg_ndx;
1131 	mutex_exit(&usba_device->usb_mutex);
1132 	if (state->st_cfg_to_build == USBA_DEV_CONFIG_INDEX_UNDEFINED) {
1133 		state->st_cfg_to_build = USBA_ALL;
1134 	}
1135 	state->st_if_to_build = usb_get_if_number(dip);
1136 
1137 	switch (state->st_dev_parse_level) {
1138 	case USB_PARSE_LVL_ALL:		/* Parse all configurations */
1139 		state->st_cfg_to_build = USBA_ALL;
1140 		state->st_if_to_build = USBA_ALL;
1141 		break;
1142 
1143 	case USB_PARSE_LVL_CFG:		/* Parse all interfaces of a */
1144 					/* specific configuration. */
1145 		state->st_if_to_build = USBA_ALL;
1146 		break;
1147 
1148 	case USB_PARSE_LVL_IF:		/* Parse configured interface only */
1149 		if (state->st_if_to_build < 0) {
1150 			state->st_if_to_build = USBA_ALL;
1151 		}
1152 		break;
1153 
1154 	default:
1155 
1156 		return (USB_INVALID_ARGS);
1157 	}
1158 
1159 	/*
1160 	 * Set parse level to identify this tree properly, regardless of what
1161 	 * the caller thought the tree would have.
1162 	 */
1163 	if ((state->st_if_to_build == USBA_ALL) &&
1164 	    (state->st_dev_parse_level == USB_PARSE_LVL_IF)) {
1165 		state->st_dev_parse_level = USB_PARSE_LVL_CFG;
1166 	}
1167 	if ((state->st_cfg_to_build == USBA_ALL) &&
1168 	    (state->st_dev_parse_level == USB_PARSE_LVL_CFG)) {
1169 		state->st_dev_parse_level = USB_PARSE_LVL_ALL;
1170 	}
1171 
1172 	return (USB_SUCCESS);
1173 }
1174 
1175 
1176 /*
1177  * usba_kmem_realloc:
1178  *	Resize dynamic memory.	Copy contents of old area to
1179  *	beginning of new area.
1180  *
1181  * Arguments:
1182  *	old_mem		- pointer to old memory area.
1183  *	old_size	- size of old memory area.  0 is OK.
1184  *	new_size	- size desired.
1185  *
1186  * Returns:
1187  *	pointer to new memory area.
1188  */
1189 static void*
1190 usba_kmem_realloc(void* old_mem, int old_size, int new_size)
1191 {
1192 	void *new_mem = NULL;
1193 
1194 	if (new_size > 0) {
1195 		new_mem = kmem_zalloc(new_size, KM_SLEEP);
1196 		if (old_size > 0) {
1197 			bcopy(old_mem, new_mem,
1198 			    min(old_size, new_size));
1199 		}
1200 	}
1201 
1202 	if (old_size > 0) {
1203 		kmem_free(old_mem, old_size);
1204 	}
1205 
1206 	return (new_mem);
1207 }
1208 
1209 
1210 /*
1211  * usba_augment_array:
1212  *	Add a new element on the end of an array.
1213  *
1214  * Arguments:
1215  *	addr		- ptr to the array address.  Array addr will change.
1216  *	n_elements	- array element count.
1217  *	element_size	- size of an array element
1218  */
1219 static void
1220 usba_augment_array(void **addr, uint_t n_elements, uint_t element_size)
1221 {
1222 	*addr = usba_kmem_realloc(*addr, (n_elements * element_size),
1223 					((n_elements + 1) * element_size));
1224 }
1225 
1226 
1227 /*
1228  * usba_make_alts_sparse:
1229  *	Disburse alternate array elements such that they are at the proper array
1230  *	indices for which alt they represent.  It is assumed that all key values
1231  *	used for ordering the elements are positive.  Original array space may
1232  *	be freed and new space allocated.
1233  *
1234  * Arguments:
1235  *	array		- pointer to alternates array; may be modified
1236  *	n_elements	- number of elements in the array; may be modified
1237  */
1238 static void
1239 usba_make_alts_sparse(usb_alt_if_data_t **array, uint_t *n_elements)
1240 {
1241 	uint_t	n_orig_elements = *n_elements;
1242 	uint8_t smallest_value;
1243 	uint8_t largest_value;
1244 	uint8_t curr_value;
1245 	uint_t	in_order = 0;
1246 	usb_alt_if_data_t *orig_addr = *array; /* Non-sparse array base ptr */
1247 	usb_alt_if_data_t *repl_array;	/* Base ptr to sparse array */
1248 	uint_t	n_repl_elements;	/* Number elements in the new array */
1249 	uint_t	i;
1250 
1251 	/* Check for a null array. */
1252 	if ((array == NULL) || (n_orig_elements == 0)) {
1253 
1254 		return;
1255 	}
1256 
1257 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1258 	    "make_sparse: array=0x%p, n_orig_elements=%d",
1259 	    array, n_orig_elements);
1260 
1261 	curr_value = orig_addr[0].altif_descr.bAlternateSetting;
1262 	smallest_value = largest_value = curr_value;
1263 
1264 	/* Figure the low-high range of the array. */
1265 	for (i = 1; i < n_orig_elements; i++) {
1266 		curr_value = orig_addr[i].altif_descr.bAlternateSetting;
1267 		if (curr_value < smallest_value) {
1268 			smallest_value = curr_value;
1269 		} else if (curr_value > largest_value) {
1270 			in_order++;
1271 			largest_value = curr_value;
1272 		}
1273 	}
1274 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1275 		"make_sparse: largest=%d, smallest=%d, "
1276 		"order=%d",
1277 		largest_value, smallest_value, in_order);
1278 
1279 	n_repl_elements = largest_value + 1;
1280 
1281 	/*
1282 	 * No holes to leave, array starts at zero, and everything is already
1283 	 * in order.  Just return original array.
1284 	 */
1285 	if ((n_repl_elements == n_orig_elements) &&
1286 	    ((in_order + 1) == n_orig_elements)) {
1287 		USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1288 				"No holes");
1289 
1290 		return;
1291 	}
1292 
1293 	/* Allocate zeroed space for the array. */
1294 	repl_array = kmem_zalloc(
1295 		(n_repl_elements * sizeof (usb_alt_if_data_t)), KM_SLEEP);
1296 
1297 	/* Now fill in the array. */
1298 	for (i = 0; i < n_orig_elements; i++) {
1299 		curr_value = orig_addr[i].altif_descr.bAlternateSetting;
1300 
1301 		/* Place in sparse array based on key. */
1302 		USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1303 		    "move %d bytes (key %d) from 0x%p to 0x%p",
1304 		    sizeof (usb_alt_if_data_t), curr_value, &orig_addr[i],
1305 		    &repl_array[curr_value]);
1306 
1307 		bcopy((char *)&orig_addr[i], (char *)&repl_array[curr_value],
1308 		    sizeof (usb_alt_if_data_t));
1309 	}
1310 
1311 	kmem_free(*array, sizeof (usb_alt_if_data_t) * n_orig_elements);
1312 	*array = repl_array;
1313 	*n_elements = n_repl_elements;
1314 }
1315 
1316 
1317 /*
1318  * usba_order_tree:
1319  *	Take a tree as built by usba_build_descr_tree and make sure the key
1320  *	values of all elements match their indeces.  Proper order is implied.
1321  *
1322  * Arguments:
1323  *	state		- Pointer to this module's state structure.
1324  */
1325 static void
1326 usba_order_tree(usba_reg_state_t *state)
1327 {
1328 	usb_cfg_data_t	*this_cfg;
1329 	usb_if_data_t	*this_if;
1330 	uint_t		n_cfgs = state->st_dev_n_cfg;
1331 	uint_t		cfg;
1332 	uint_t		which_if;
1333 
1334 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1335 	    "usba_order_tree:");
1336 
1337 	for (cfg = 0; cfg < n_cfgs; cfg++) {
1338 		this_cfg = &state->st_dev_cfg[cfg];
1339 
1340 		for (which_if = 0; which_if < this_cfg->cfg_n_if; which_if++) {
1341 			this_if = this_cfg->cfg_if;
1342 			usba_make_alts_sparse(&this_if->if_alt,
1343 						&this_if->if_n_alt);
1344 		}
1345 	}
1346 }
1347 
1348 
1349 /*
1350  * usb_free_descr_tree:
1351  *	Take down the configuration tree.  Called internally and can be called
1352  *	from a driver standalone to take the tree down while leaving the rest
1353  *	of the registration intact.
1354  *
1355  * Arguments:
1356  *	dip		- pointer to devinfo of the device
1357  *	dev_data	- pointer to registration data containing the tree.
1358  */
1359 void
1360 usb_free_descr_tree(dev_info_t *dip, usb_client_dev_data_t *dev_data)
1361 {
1362 	usb_cfg_data_t *cfg_array;
1363 	int n_cfgs;
1364 	int cfg;
1365 
1366 	if ((dip == NULL) || (dev_data == NULL)) {
1367 
1368 		return;
1369 	}
1370 	cfg_array = dev_data->dev_cfg;
1371 	n_cfgs = dev_data->dev_n_cfg;
1372 
1373 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1374 	    "usb_free_descr_tree starting for %s%d",
1375 	    ddi_driver_name(dip), ddi_get_instance(dip));
1376 
1377 	for (cfg = 0; cfg < n_cfgs; cfg++) {
1378 		if (cfg_array[cfg].cfg_if) {
1379 			usba_free_if_array(cfg_array[cfg].cfg_if,
1380 					cfg_array[cfg].cfg_n_if);
1381 		}
1382 		if (cfg_array[cfg].cfg_cvs) {
1383 			usba_free_cv_array(cfg_array[cfg].cfg_cvs,
1384 					cfg_array[cfg].cfg_n_cvs);
1385 		}
1386 		if (cfg_array[cfg].cfg_str) {
1387 			kmem_free(cfg_array[cfg].cfg_str,
1388 					cfg_array[cfg].cfg_strsize);
1389 		}
1390 	}
1391 
1392 	if (cfg_array) {
1393 		kmem_free(cfg_array, (sizeof (usb_cfg_data_t) * n_cfgs));
1394 	}
1395 
1396 	dev_data->dev_parse_level = USB_PARSE_LVL_NONE;
1397 	dev_data->dev_n_cfg = 0;
1398 	dev_data->dev_cfg = NULL;
1399 	dev_data->dev_curr_cfg = NULL;
1400 
1401 	USB_DPRINTF_L4(DPRINT_MASK_REGISTER, usbai_reg_log_handle,
1402 	    "usb_free_descr_tree done");
1403 }
1404 
1405 
1406 /*
1407  * usba_free_if_array:
1408  *	Free a configuration's array of interface nodes and their subtrees of
1409  *	interface alternate, endpoint and c/v descriptors.
1410  *
1411  * Arguments:
1412  *	if_array	- pointer to array of interfaces to remove.
1413  *	n_ifs		- number of elements in the array to remove.
1414  */
1415 static void
1416 usba_free_if_array(usb_if_data_t *if_array, uint_t n_ifs)
1417 {
1418 	uint_t which_if;
1419 	uint_t which_alt;
1420 	uint_t n_alts;
1421 	usb_alt_if_data_t *altif;
1422 
1423 	for (which_if = 0; which_if < n_ifs; which_if++) {
1424 		n_alts = if_array[which_if].if_n_alt;
1425 
1426 		/* Every interface has at least one alternate. */
1427 		for (which_alt = 0; which_alt < n_alts; which_alt++) {
1428 			altif = &if_array[which_if].if_alt[which_alt];
1429 			usba_free_ep_array(altif->altif_ep, altif->altif_n_ep);
1430 			usba_free_cv_array(altif->altif_cvs,
1431 					altif->altif_n_cvs);
1432 			kmem_free(altif->altif_str, altif->altif_strsize);
1433 		}
1434 
1435 		kmem_free(if_array[which_if].if_alt,
1436 		    (sizeof (usb_alt_if_data_t) * n_alts));
1437 	}
1438 
1439 	/* Free the interface array itself. */
1440 	kmem_free(if_array, (sizeof (usb_if_data_t) * n_ifs));
1441 }
1442 
1443 
1444 /*
1445  * usba_free_ep_array:
1446  *	Free an array of endpoint nodes and their subtrees of c/v descriptors.
1447  *
1448  * Arguments:
1449  *	ep_array	- pointer to array of endpoints to remove.
1450  *	n_eps		- number of elements in the array to remove.
1451  */
1452 static void
1453 usba_free_ep_array(usb_ep_data_t *ep_array, uint_t n_eps)
1454 {
1455 	uint_t ep;
1456 
1457 	for (ep = 0; ep < n_eps; ep++) {
1458 		usba_free_cv_array(ep_array[ep].ep_cvs, ep_array[ep].ep_n_cvs);
1459 	}
1460 
1461 	kmem_free(ep_array, (sizeof (usb_ep_data_t) * n_eps));
1462 }
1463 
1464 
1465 /*
1466  * usba_free_cv_array:
1467  *	Free an array of class/vendor (c/v) descriptor nodes.
1468  *
1469  * Arguments:
1470  *	cv_array	- pointer to array of c/v nodes to remove.
1471  *	n_cvs		- number of elements in the array to remove.
1472  */
1473 static void
1474 usba_free_cv_array(usb_cvs_data_t *cv_array, uint_t n_cvs)
1475 {
1476 	uint_t cv_node;
1477 
1478 	/* Free data areas hanging off of each c/v descriptor. */
1479 	for (cv_node = 0; cv_node < n_cvs; cv_node++) {
1480 		kmem_free(cv_array[cv_node].cvs_buf,
1481 				cv_array[cv_node].cvs_buf_len);
1482 	}
1483 
1484 	/* Free the array of cv descriptors. */
1485 	kmem_free(cv_array, (sizeof (usb_cvs_data_t) * n_cvs));
1486 }
1487 
1488 
1489 /*
1490  * usb_log_descr_tree:
1491  *	Log to the usba_debug_buf a descriptor tree as returned by
1492  *	usbai_register_client.
1493  *
1494  * Arguments:
1495  *	dev_data	- pointer to registration area containing the tree
1496  *	log_handle	- pointer to log handle to use for dumping.
1497  *	level		- print level, one of USB_LOG_L0 ... USB_LOG_L4
1498  *			  Please see usb_log(9F) for details.
1499  *	mask		- print mask.  Please see usb_log(9F) for details.
1500  *
1501  * Returns:
1502  *	USB_SUCCESS		- tree successfully dumped
1503  *	USB_INVALID_CONTEXT	- called from callback context
1504  *	USB_INVALID_ARGS	- bad arguments given
1505  */
1506 int
1507 usb_log_descr_tree(usb_client_dev_data_t *dev_data,
1508     usb_log_handle_t log_handle, uint_t level, uint_t mask)
1509 {
1510 	return (usba_dump_descr_tree(NULL, dev_data, log_handle, level, mask));
1511 }
1512 
1513 
1514 /*
1515  * usb_print_descr_tree:
1516  *	Print to the screen a descriptor tree as returned by
1517  *	usbai_register_client.
1518  *
1519  * Arguments:
1520  *	dip		- pointer to devinfo of the client
1521  *	dev_data	- pointer to registration area containing the tree
1522  *
1523  * Returns:
1524  *	USB_SUCCESS		- tree successfully dumped
1525  *	USB_INVALID_CONTEXT	- called from callback context
1526  *	USB_INVALID_ARGS	- bad arguments given
1527  */
1528 int
1529 usb_print_descr_tree(dev_info_t *dip, usb_client_dev_data_t *dev_data)
1530 {
1531 	return (usba_dump_descr_tree(dip, dev_data, NULL, 0, 0));
1532 }
1533 
1534 
1535 /*
1536  * usba_dump_descr_tree:
1537  *	Dump a descriptor tree.
1538  *
1539  * Arguments:
1540  *	dip		- pointer to devinfo of the client.  Used when no
1541  *			  log_handle argument given.
1542  *	usb_reg		- pointer to registration area containing the tree
1543  *	log_handle	- pointer to log handle to use for dumping.  If NULL,
1544  *			  use internal log handle, which dumps to screen.
1545  *	level		- print level, one of USB_LOG_L0 ... USB_LOG_L4
1546  *			  Used only when log_handle provided.
1547  *	mask		- print mask, used when log_handle argument provided.
1548  *
1549  * Returns:
1550  *	USB_SUCCESS		- tree successfully dumped
1551  *	USB_INVALID_CONTEXT	- called from callback context
1552  *	USB_INVALID_ARGS	- bad arguments given
1553  */
1554 static int
1555 usba_dump_descr_tree(dev_info_t *dip, usb_client_dev_data_t *usb_reg,
1556     usb_log_handle_t log_handle, uint_t level, uint_t mask)
1557 {
1558 	usb_log_handle_t dump_handle;
1559 	uint_t		dump_level;
1560 	uint_t		dump_mask;
1561 	int		which_config; /* Counters. */
1562 	int		which_if;
1563 	int		which_cv;
1564 	usb_cfg_data_t	*config; /* ptr to current configuration tree node */
1565 	usb_cfg_descr_t *config_descr; /* and its USB descriptor. */
1566 	char		*string;
1567 	char		*name_string = NULL;
1568 	int		name_string_size;
1569 
1570 	if ((usb_reg == NULL) || ((log_handle == NULL) && (dip == NULL))) {
1571 
1572 		return (USB_INVALID_ARGS);
1573 	}
1574 
1575 	/*
1576 	 * To keep calling this simple, kmem_zalloc with the sleep flag always.
1577 	 * This means no interrupt context is allowed.
1578 	 */
1579 	if (servicing_interrupt()) {
1580 
1581 		return (USB_INVALID_CONTEXT);
1582 	}
1583 
1584 	string = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
1585 
1586 	if (log_handle != NULL) {
1587 		dump_level = level;
1588 		dump_mask = mask;
1589 		dump_handle = log_handle;
1590 	} else {
1591 		dump_level = USB_LOG_L1;
1592 		dump_mask = DPRINT_MASK_ALL;
1593 
1594 		/* Build device name string. */
1595 		(void) snprintf(string, USB_MAXSTRINGLEN,
1596 				"Port%d", usb_get_addr(dip));
1597 		name_string_size = strlen(string) + 1;
1598 		name_string = kmem_zalloc(name_string_size, KM_SLEEP);
1599 		(void) strcpy(name_string, string);
1600 
1601 		/* Allocate a log handle specifying the name string. */
1602 		dump_handle = usb_alloc_log_hdl(NULL, name_string,
1603 					&dump_level, &dump_mask, NULL,
1604 					USB_FLAGS_SLEEP);
1605 	}
1606 
1607 	(void) usb_log(dump_handle, dump_level, dump_mask,
1608 	    "USB descriptor tree for %s %s",
1609 	    (usb_reg->dev_mfg != NULL ? usb_reg->dev_mfg : ""),
1610 	    (usb_reg->dev_product != NULL ? usb_reg->dev_product : ""));
1611 	if (usb_reg->dev_n_cfg == 0) {
1612 		(void) usb_log(dump_handle, dump_level, dump_mask,
1613 		    "No descriptor tree present");
1614 	} else {
1615 		(void) usb_log(dump_handle, dump_level, dump_mask,
1616 		    "highest configuration found=%d", usb_reg->dev_n_cfg - 1);
1617 	}
1618 
1619 	for (which_config = 0; which_config < usb_reg->dev_n_cfg;
1620 	    which_config++) {
1621 		config = &usb_reg->dev_cfg[which_config];
1622 		config_descr = &config->cfg_descr;
1623 		if (config_descr->bLength == 0) {
1624 
1625 			continue;
1626 		}
1627 		if (dump_level == USB_LOG_L0) {
1628 			(void) usb_log(dump_handle, dump_level, dump_mask, " ");
1629 		}
1630 		(void) usb_log(dump_handle, dump_level, dump_mask,
1631 		    "Configuration #%d (Addr= 0x%p)", which_config, config);
1632 		(void) usb_log(dump_handle, dump_level, dump_mask,
1633 		    "String descr=%s", config->cfg_str);
1634 		(void) usb_log(dump_handle, dump_level, dump_mask,
1635 		    "config descr: len=%d tp=%d totLen=%d numIf=%d "
1636 		    "cfgVal=%d att=0x%x pwr=%d",
1637 		    config_descr->bLength, config_descr->bDescriptorType,
1638 		    config_descr->wTotalLength, config_descr->bNumInterfaces,
1639 		    config_descr->bConfigurationValue,
1640 		    config_descr->bmAttributes, config_descr->bMaxPower);
1641 		if ((config->cfg_n_if > 0) || (config->cfg_n_cvs > 0)) {
1642 			(void) usb_log(dump_handle, dump_level, dump_mask,
1643 			    "usb_cfg_data_t shows max if=%d "
1644 			    "and %d cv descr(s).",
1645 			    config->cfg_n_if - 1, config->cfg_n_cvs);
1646 		}
1647 
1648 		for (which_if = 0; which_if < config->cfg_n_if;
1649 		    which_if++) {
1650 
1651 			if (dump_level == USB_LOG_L0) {
1652 				(void) usb_log(dump_handle, dump_level,
1653 					dump_mask, " ");
1654 			}
1655 			(void) usb_log(dump_handle, dump_level, dump_mask,
1656 			    "	 interface #%d (0x%p)",
1657 			    which_if, &config->cfg_if[which_if]);
1658 			usba_dump_if(&config->cfg_if[which_if],
1659 			    dump_handle, dump_level, dump_mask, string);
1660 		}
1661 
1662 		for (which_cv = 0; which_cv < config->cfg_n_cvs; which_cv++) {
1663 			(void) usb_log(dump_handle, dump_level, dump_mask,
1664 			    "  config cv descriptor %d (Address=0x%p)",
1665 			    which_cv, &config->cfg_cvs[which_cv]);
1666 			usba_dump_cv(&config->cfg_cvs[which_cv],
1667 			    dump_handle, dump_level, dump_mask, string, 4);
1668 		}
1669 	}
1670 
1671 	(void) usb_log(dump_handle, dump_level, dump_mask,
1672 	    "Returning dev_curr_cfg:0x%p, dev_curr_if:%d",
1673 	    usb_reg->dev_curr_cfg, usb_reg->dev_curr_if);
1674 
1675 	if (log_handle == NULL) {
1676 		usb_free_log_hdl(dump_handle);
1677 	}
1678 	if (name_string != NULL) {
1679 		kmem_free(name_string, name_string_size);
1680 	}
1681 	kmem_free(string, USB_MAXSTRINGLEN);
1682 
1683 	return (USB_SUCCESS);
1684 }
1685 
1686 
1687 /*
1688  * usba_dump_if:
1689  *	Dump an interface node and its branches.
1690  *
1691  * Arguments:
1692  *	which_if	- interface node to dump
1693  *	dump_handle	- write data through this log handle
1694  *	dump_level	- level passed to usb_log
1695  *	dump_mask	- mask passed to usb_log
1696  *	string		- temporary area used for processing
1697  *
1698  */
1699 static void
1700 usba_dump_if(usb_if_data_t *which_if, usb_log_handle_t dump_handle,
1701     uint_t dump_level, uint_t dump_mask, char *string)
1702 {
1703 	int		which_alt;	/* Number of alt being dumped */
1704 	usb_alt_if_data_t *alt;		/* Pointer to it. */
1705 	usb_if_descr_t *if_descr;	/* Pointer to its USB descr. */
1706 	int		which_ep;	/* Endpoint counter. */
1707 	int		which_cv;	/* C/V descr counter. */
1708 
1709 	for (which_alt = 0; which_alt < which_if->if_n_alt; which_alt++) {
1710 		alt = &which_if->if_alt[which_alt];
1711 		if_descr = &alt->altif_descr;
1712 
1713 		if (if_descr->bLength == 0) {
1714 
1715 			continue;
1716 		}
1717 		if (dump_level == USB_LOG_L0) {
1718 			(void) usb_log(dump_handle, dump_level, dump_mask, " ");
1719 		}
1720 		(void) usb_log(dump_handle, dump_level, dump_mask,
1721 		    "\tAlt #%d (0x%p)", which_alt, alt);
1722 		(void) usb_log(dump_handle, dump_level, dump_mask,
1723 		    "\tString descr=%s", alt->altif_str);
1724 		(void) usb_log(dump_handle, dump_level, dump_mask,
1725 		    "\tif descr: len=%d type=%d if=%d alt=%d n_ept=%d "
1726 		    "cls=%d sub=%d proto=%d",
1727 		    if_descr->bLength,
1728 		    if_descr->bDescriptorType, if_descr->bInterfaceNumber,
1729 		    if_descr->bAlternateSetting, if_descr->bNumEndpoints,
1730 		    if_descr->bInterfaceClass, if_descr->bInterfaceSubClass,
1731 		    if_descr->bInterfaceProtocol);
1732 
1733 		if ((alt->altif_n_ep > 0) || (alt->altif_n_cvs > 0)) {
1734 			(void) usb_log(dump_handle, dump_level, dump_mask,
1735 			    "\tusb_alt_if_data_t shows max ep=%d "
1736 			    "and %d cv descr(s).",
1737 			    alt->altif_n_ep - 1, alt->altif_n_cvs);
1738 		}
1739 
1740 		for (which_ep = 0; which_ep < alt->altif_n_ep;
1741 		    which_ep++) {
1742 			if (alt->altif_ep[which_ep].ep_descr.bLength == 0) {
1743 
1744 				continue;
1745 			}
1746 			if (dump_level == USB_LOG_L0) {
1747 				(void) usb_log(dump_handle, dump_level,
1748 					dump_mask, " ");
1749 			}
1750 			usba_dump_ep(which_ep, &alt->altif_ep[which_ep],
1751 			    dump_handle, dump_level, dump_mask, string);
1752 		}
1753 
1754 		for (which_cv = 0; which_cv < alt->altif_n_cvs; which_cv++) {
1755 			if (dump_level == USB_LOG_L0) {
1756 				(void) usb_log(dump_handle, dump_level,
1757 					dump_mask, " ");
1758 			}
1759 			(void) usb_log(dump_handle, dump_level, dump_mask,
1760 			    "\talt cv descriptor #%d (0x%p), size=%d",
1761 			    which_cv, &alt->altif_cvs[which_cv],
1762 			    alt->altif_cvs[which_cv].cvs_buf_len);
1763 			usba_dump_cv(&alt->altif_cvs[which_cv],
1764 			    dump_handle, dump_level, dump_mask, string, 2);
1765 		}
1766 	}
1767 }
1768 
1769 
1770 /*
1771  * usba_dump_ep:
1772  *	Dump an endpoint node and its branches.
1773  *
1774  * Arguments:
1775  *	which_ep	- index to display
1776  *	ep		- endpoint node to dump
1777  *	dump_handle	- write data through this log handle
1778  *	dump_level	- level passed to usb_log
1779  *	dump_mask	- mask passed to usb_log
1780  *	string		- temporary area used for processing
1781  *
1782  */
1783 static void
1784 usba_dump_ep(uint_t which_ep, usb_ep_data_t *ep, usb_log_handle_t dump_handle,
1785 		uint_t dump_level, uint_t dump_mask, char *string)
1786 {
1787 	int which_cv;
1788 	usb_ep_descr_t *ep_descr = &ep->ep_descr;
1789 
1790 	(void) usb_log(dump_handle, dump_level, dump_mask,
1791 	    "\t    endpoint[%d], epaddr=0x%x (0x%p)", which_ep,
1792 	    ep_descr->bEndpointAddress, ep);
1793 	(void) usb_log(dump_handle, dump_level, dump_mask,
1794 	    "\t    len=%d type=%d attr=0x%x pktsize=%d interval=%d",
1795 	    ep_descr->bLength, ep_descr->bDescriptorType,
1796 	    ep_descr->bmAttributes, ep_descr->wMaxPacketSize,
1797 	    ep_descr->bInterval);
1798 	if (ep->ep_n_cvs > 0) {
1799 		(void) usb_log(dump_handle, dump_level, dump_mask,
1800 		    "\t    usb_ep_data_t shows %d cv descr(s)", ep->ep_n_cvs);
1801 	}
1802 
1803 	for (which_cv = 0; which_cv < ep->ep_n_cvs; which_cv++) {
1804 		if (dump_level == USB_LOG_L0) {
1805 			(void) usb_log(dump_handle, dump_level,
1806 				dump_mask, " ");
1807 		}
1808 		(void) usb_log(dump_handle, dump_level, dump_mask,
1809 		    "\t    endpoint cv descriptor %d (0x%p), size=%d",
1810 		    which_cv, &ep->ep_cvs[which_cv],
1811 		    ep->ep_cvs[which_cv].cvs_buf_len);
1812 		usba_dump_cv(&ep->ep_cvs[which_cv],
1813 		    dump_handle, dump_level, dump_mask, string, 3);
1814 	}
1815 }
1816 
1817 
1818 /*
1819  * usba_dump_cv:
1820  *	Dump a raw class or vendor specific descriptor.
1821  *
1822  * Arguments:
1823  *	cv_node		- pointer to the descriptor to dump
1824  *	dump_handle	- write data through this log handle
1825  *	dump_level	- level passed to usb_log
1826  *	dump_mask	- mask passed to usb_log
1827  *	string		- temporary area used for processing
1828  *	indent		- number of tabs to indent output
1829  *
1830  */
1831 static void
1832 usba_dump_cv(usb_cvs_data_t *cv_node, usb_log_handle_t dump_handle,
1833     uint_t dump_level, uint_t dump_mask, char *string, int indent)
1834 {
1835 	if (cv_node) {
1836 		usba_dump_bin(cv_node->cvs_buf, cv_node->cvs_buf_len, indent,
1837 				dump_handle, dump_level, dump_mask, string,
1838 				USB_MAXSTRINGLEN);
1839 	}
1840 }
1841 
1842 
1843 /*
1844  * usba_dump_bin:
1845  *	Generic byte dump function.
1846  *
1847  * Arguments:
1848  *	data		- pointer to the data to dump
1849  *	max_bytes	- amount of data to dump
1850  *	indent		- number of indentation levels
1851  *	dump_handle	- write data through this log handle
1852  *	dump_level	- level passed to usb_log
1853  *	dump_mask	- mask passed to usb_log
1854  *	buffer		- temporary area used for processing
1855  *	bufferlen	- size of the temporary string area
1856  *
1857  */
1858 static void
1859 usba_dump_bin(uint8_t *data, int max_bytes, int indent,
1860     usb_log_handle_t dump_handle, uint_t dump_level, uint_t dump_mask,
1861     char *buffer, int bufferlen)
1862 {
1863 	int i;
1864 	int bufoffset = 0;
1865 	int nexthere;
1866 
1867 	if ((indent * SPACES_PER_INDENT) >
1868 	    (bufferlen - (BINDUMP_BYTES_PER_LINE * 3))) {
1869 		(void) usb_log(dump_handle, dump_level, dump_mask,
1870 		    "Offset to usb_dump_bin must be %d or less.  "
1871 		    "Setting to 0.\n",
1872 		    (bufferlen - (BINDUMP_BYTES_PER_LINE * 3)));
1873 		indent = 0;
1874 	}
1875 
1876 	/* Assume a tab is 2 four-space units. */
1877 	for (i = 0; i < indent/2; i++) {
1878 	    buffer[bufoffset] = '\t';
1879 	    bufoffset++;
1880 	}
1881 
1882 	if (indent % 2) {
1883 		(void) strcpy(&buffer[bufoffset], INDENT_SPACE_STR);
1884 		bufoffset += SPACES_PER_INDENT;
1885 	}
1886 
1887 	i = 0;			/* Num dumped bytes put on this line. */
1888 	nexthere = bufoffset;
1889 	while (i < max_bytes) {
1890 		(void) sprintf(&buffer[nexthere], "%2x ", *data++);
1891 		nexthere += 3;
1892 		i++;
1893 		if (!(i % BINDUMP_BYTES_PER_LINE)) {
1894 			buffer[nexthere] = '\0';
1895 			(void) usb_log(dump_handle, dump_level, dump_mask,
1896 			    buffer);
1897 			nexthere = bufoffset;
1898 		}
1899 	}
1900 
1901 	if (nexthere > bufoffset) {
1902 		buffer[nexthere] = '\0';
1903 		(void) usb_log(dump_handle, dump_level, dump_mask, buffer);
1904 	}
1905 }
1906