1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stddef.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/mdb_modapi.h>
31*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_ks.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
40*7c478bd9Sstevel@tonic-gate #include <unistd.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * Prototypes
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate /* usba.c */
47*7c478bd9Sstevel@tonic-gate extern uintptr_t mdb_usba_get_usba_device(uintptr_t);
48*7c478bd9Sstevel@tonic-gate extern uintptr_t mdb_usba_hcdi_get_hcdi(struct dev_info *);
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * Defines
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate /* dcmd options */
54*7c478bd9Sstevel@tonic-gate #define	USB_DUMP_VERBOSE	0x01
55*7c478bd9Sstevel@tonic-gate #define	USB_DUMP_ACTIVE_PIPES	0x02
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /* Hardcoded slop factor designed into debug buf logic */
58*7c478bd9Sstevel@tonic-gate #define	USB_DEBUG_SIZE_EXTRA_ALLOC 8
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * Callback arg struct for find_dip (callback func used in usba_device2devinfo).
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate typedef struct usba_device2devinfo_data {
65*7c478bd9Sstevel@tonic-gate 	uintptr_t	u2d_target_usb_dev_p;	/* one we're looking for */
66*7c478bd9Sstevel@tonic-gate 	uintptr_t	*u2d_dip_addr;		/* Where to store result */
67*7c478bd9Sstevel@tonic-gate 	boolean_t	u2d_found;		/* Match found */
68*7c478bd9Sstevel@tonic-gate } usba_device2devinfo_cbdata_t;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate /*
72*7c478bd9Sstevel@tonic-gate  * Callback for usba_device2dip.
73*7c478bd9Sstevel@tonic-gate  * Callback called from the devinfo_children walk invoked in usba_device2dip.
74*7c478bd9Sstevel@tonic-gate  *
75*7c478bd9Sstevel@tonic-gate  * For the current dip, get the (potential) pointer to its usba_device_t
76*7c478bd9Sstevel@tonic-gate  * struct.
77*7c478bd9Sstevel@tonic-gate  * See if this pointer matches the address of the usba_device_t we're looking
78*7c478bd9Sstevel@tonic-gate  * for (passed in as usb_dev_p).  If so, stuff its value in u2d_dip_addr,
79*7c478bd9Sstevel@tonic-gate  * and terminate the walk.
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  * - dip_addr is the address in core of the dip currently being processed by the
82*7c478bd9Sstevel@tonic-gate  * walk
83*7c478bd9Sstevel@tonic-gate  * - local_dip is a pointer to a copy of the struct dev_info in local memory
84*7c478bd9Sstevel@tonic-gate  * - cb_data is the addr of the callback arg the walker was invoked with
85*7c478bd9Sstevel@tonic-gate  * (passed through transparently from walk invoker).
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  * Returns:
88*7c478bd9Sstevel@tonic-gate  * - WALK_NEXT on success (match not found yet)
89*7c478bd9Sstevel@tonic-gate  * - WALK_ERR on errors.
90*7c478bd9Sstevel@tonic-gate  * - WALK_DONE is returned, cb_data.found is set to TRUE, and
91*7c478bd9Sstevel@tonic-gate  * *cb_data.u2d_dip_addr is set to the matched dip addr if a dip corresponding
92*7c478bd9Sstevel@tonic-gate  * to the desired usba_device_t* is found.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
95*7c478bd9Sstevel@tonic-gate static int
96*7c478bd9Sstevel@tonic-gate find_dip(uintptr_t dip_addr, const void *local_dip, void *cb_arg)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	uintptr_t			cur_usb_dev;
99*7c478bd9Sstevel@tonic-gate 	usba_device2devinfo_cbdata_t	*cb_data =
100*7c478bd9Sstevel@tonic-gate 			    (usba_device2devinfo_cbdata_t *)cb_arg;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if ((cur_usb_dev = mdb_usba_get_usba_device(dip_addr)) == NULL) {
103*7c478bd9Sstevel@tonic-gate 		/*
104*7c478bd9Sstevel@tonic-gate 		 * If there's no corresponding usba_device_t, this dip isn't
105*7c478bd9Sstevel@tonic-gate 		 * a usb node.  Might be an sd node.  Ignore it.
106*7c478bd9Sstevel@tonic-gate 		 */
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if (cur_usb_dev == cb_data->u2d_target_usb_dev_p) {
112*7c478bd9Sstevel@tonic-gate 		*cb_data->u2d_dip_addr = dip_addr;
113*7c478bd9Sstevel@tonic-gate 		cb_data->u2d_found = TRUE;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /*
123*7c478bd9Sstevel@tonic-gate  * Given a usba_device pointer, figure out which dip is associated with it.
124*7c478bd9Sstevel@tonic-gate  * Relies on usba_device.usb_root_hub_dip being accurate.
125*7c478bd9Sstevel@tonic-gate  *
126*7c478bd9Sstevel@tonic-gate  * - usb_dev_addr is a pointer to a usba_device_t in core.
127*7c478bd9Sstevel@tonic-gate  * - dip_addr is the address of a uintptr_t to receive the address in core
128*7c478bd9Sstevel@tonic-gate  * of the found dip (if any).
129*7c478bd9Sstevel@tonic-gate  *
130*7c478bd9Sstevel@tonic-gate  * Returns:
131*7c478bd9Sstevel@tonic-gate  *  0 on success (no match found)
132*7c478bd9Sstevel@tonic-gate  *  1 on success (match found)
133*7c478bd9Sstevel@tonic-gate  * -1 on errors.
134*7c478bd9Sstevel@tonic-gate  */
135*7c478bd9Sstevel@tonic-gate static int
136*7c478bd9Sstevel@tonic-gate usba_device2dip(uintptr_t usb_dev_addr, uintptr_t *dip_addr)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	usba_device_t			usb_dev;
139*7c478bd9Sstevel@tonic-gate 	usba_device2devinfo_cbdata_t	cb_data;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	/*
142*7c478bd9Sstevel@tonic-gate 	 * Walk all USB children of the root hub devinfo.
143*7c478bd9Sstevel@tonic-gate 	 * The callback func looks for a match on the usba_device address.
144*7c478bd9Sstevel@tonic-gate 	 */
145*7c478bd9Sstevel@tonic-gate 	cb_data.u2d_target_usb_dev_p = usb_dev_addr;
146*7c478bd9Sstevel@tonic-gate 	cb_data.u2d_dip_addr = dip_addr;
147*7c478bd9Sstevel@tonic-gate 	cb_data.u2d_found = FALSE;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (mdb_vread((void *)&usb_dev, sizeof (usba_device_t),
150*7c478bd9Sstevel@tonic-gate 	    usb_dev_addr) == -1) {
151*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usba_device struct");
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 		return (-1);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/*
157*7c478bd9Sstevel@tonic-gate 	 * Walk devinfo children starting with the root hub node,
158*7c478bd9Sstevel@tonic-gate 	 * looking for a match on the usba_device pointer (which is what
159*7c478bd9Sstevel@tonic-gate 	 * find_dip does).
160*7c478bd9Sstevel@tonic-gate 	 * Result is placed in cb_data.dip_addr.
161*7c478bd9Sstevel@tonic-gate 	 */
162*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk("devinfo_children", find_dip, (void *)&cb_data,
163*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)usb_dev.usb_root_hub_dip) != 0) {
164*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to walk devinfo_children");
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 		return (-1);
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if (cb_data.u2d_found == TRUE) {
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		return (1);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	return (0);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate /*
179*7c478bd9Sstevel@tonic-gate  * Generic walker usba_list_entry_t walker.
180*7c478bd9Sstevel@tonic-gate  * Works for any usba_list_entry_t list.
181*7c478bd9Sstevel@tonic-gate  */
182*7c478bd9Sstevel@tonic-gate int
183*7c478bd9Sstevel@tonic-gate usba_list_walk_init(mdb_walk_state_t *wsp)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	/* Must have a start addr.  */
186*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
187*7c478bd9Sstevel@tonic-gate 		mdb_warn("not a global walk.  Starting address required\n");
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate /*
197*7c478bd9Sstevel@tonic-gate  * Generic list walker step routine.
198*7c478bd9Sstevel@tonic-gate  * NOTE: multiple walkers share this routine.
199*7c478bd9Sstevel@tonic-gate  */
200*7c478bd9Sstevel@tonic-gate int
201*7c478bd9Sstevel@tonic-gate usba_list_walk_step(mdb_walk_state_t *wsp)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	int			status;
204*7c478bd9Sstevel@tonic-gate 	usba_list_entry_t	list_entry;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&list_entry, sizeof (usba_list_entry_t),
207*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)wsp->walk_addr) == -1) {
208*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usba_list_entry_t at %p",
209*7c478bd9Sstevel@tonic-gate 		    wsp->walk_addr);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &list_entry,
215*7c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata);
216*7c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)list_entry.next;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	/* Check if we're at the last element */
219*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	return (status);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate /*
229*7c478bd9Sstevel@tonic-gate  * usb_pipe_handle walker
230*7c478bd9Sstevel@tonic-gate  * Given a pointer to a usba_device_t, walk the array of endpoint
231*7c478bd9Sstevel@tonic-gate  * pipe_handle lists.
232*7c478bd9Sstevel@tonic-gate  * For each list, traverse the list, invoking the callback on each element.
233*7c478bd9Sstevel@tonic-gate  *
234*7c478bd9Sstevel@tonic-gate  * Note this function takes the address of a usba_device struct (which is
235*7c478bd9Sstevel@tonic-gate  * easily obtainable), but actually traverses a sub-portion of the struct
236*7c478bd9Sstevel@tonic-gate  * (which address is not so easily obtainable).
237*7c478bd9Sstevel@tonic-gate  */
238*7c478bd9Sstevel@tonic-gate int
239*7c478bd9Sstevel@tonic-gate usb_pipe_handle_walk_init(mdb_walk_state_t *wsp)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
242*7c478bd9Sstevel@tonic-gate 		mdb_warn("not a global walk; usba_device_t required\n");
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc((sizeof (usba_ph_impl_t)) * USBA_N_ENDPOINTS,
248*7c478bd9Sstevel@tonic-gate 					UM_SLEEP | UM_GC);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	/*
251*7c478bd9Sstevel@tonic-gate 	 * Read the usb_ph_list array into local memory.
252*7c478bd9Sstevel@tonic-gate 	 * Set start address to first element/endpoint in usb_pipehandle_list
253*7c478bd9Sstevel@tonic-gate 	 */
254*7c478bd9Sstevel@tonic-gate 	if (mdb_vread((void *)wsp->walk_data,
255*7c478bd9Sstevel@tonic-gate 	    (sizeof (usba_ph_impl_t)) * USBA_N_ENDPOINTS,
256*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)((size_t)(wsp->walk_addr) +
257*7c478bd9Sstevel@tonic-gate 	    offsetof(usba_device_t, usb_ph_list))) == -1) {
258*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usb_pipehandle_list at %p",
259*7c478bd9Sstevel@tonic-gate 		    wsp->walk_addr);
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	wsp->walk_arg = 0;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate int
271*7c478bd9Sstevel@tonic-gate usb_pipe_handle_walk_step(mdb_walk_state_t *wsp)
272*7c478bd9Sstevel@tonic-gate {
273*7c478bd9Sstevel@tonic-gate 	int status;
274*7c478bd9Sstevel@tonic-gate 	usba_ph_impl_t *impl_list = (usba_ph_impl_t *)(wsp->walk_data);
275*7c478bd9Sstevel@tonic-gate 	intptr_t index = (intptr_t)wsp->walk_arg;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	/* Find the first valid endpoint, starting from where we left off. */
278*7c478bd9Sstevel@tonic-gate 	while ((index < USBA_N_ENDPOINTS) &&
279*7c478bd9Sstevel@tonic-gate 	    (impl_list[index].usba_ph_data == NULL)) {
280*7c478bd9Sstevel@tonic-gate 		index++;
281*7c478bd9Sstevel@tonic-gate 	}
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	/* No more valid endpoints. */
284*7c478bd9Sstevel@tonic-gate 	if (index >= USBA_N_ENDPOINTS) {
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
287*7c478bd9Sstevel@tonic-gate 	}
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback((uintptr_t)impl_list[index].usba_ph_data,
290*7c478bd9Sstevel@tonic-gate 					wsp->walk_data, wsp->walk_cbdata);
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	/* Set up to start at next pipe handle next time. */
293*7c478bd9Sstevel@tonic-gate 	wsp->walk_arg = (void *)(index + 1);
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	return (status);
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate /*
300*7c478bd9Sstevel@tonic-gate  * Given the address of a usba_pipe_handle_data_t, dump summary info.
301*7c478bd9Sstevel@tonic-gate  */
302*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
303*7c478bd9Sstevel@tonic-gate int
304*7c478bd9Sstevel@tonic-gate usb_pipe_handle(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
305*7c478bd9Sstevel@tonic-gate {
306*7c478bd9Sstevel@tonic-gate 	char			*dir, *type, *state;
307*7c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		ept_descr;
308*7c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	pipe_handle;
309*7c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		ph_impl;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&pipe_handle,
317*7c478bd9Sstevel@tonic-gate 	    sizeof (usba_pipe_handle_data_t), addr) == -1) {
318*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read pipe handle at %p", addr);
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&ph_impl, sizeof (usba_ph_impl_t),
324*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)pipe_handle.p_ph_impl) == -1) {
325*7c478bd9Sstevel@tonic-gate 		state = "*******";
326*7c478bd9Sstevel@tonic-gate 	} else {
327*7c478bd9Sstevel@tonic-gate 		switch (ph_impl.usba_ph_state) {
328*7c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_CLOSED:
329*7c478bd9Sstevel@tonic-gate 			state = "CLOSED ";
330*7c478bd9Sstevel@tonic-gate 			break;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_IDLE:
333*7c478bd9Sstevel@tonic-gate 			state = "IDLE   ";
334*7c478bd9Sstevel@tonic-gate 			break;
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_ACTIVE:
337*7c478bd9Sstevel@tonic-gate 			state = "ACTIVE ";
338*7c478bd9Sstevel@tonic-gate 			break;
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_ERROR:
341*7c478bd9Sstevel@tonic-gate 			state = "ERROR  ";
342*7c478bd9Sstevel@tonic-gate 			break;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_CLOSING:
345*7c478bd9Sstevel@tonic-gate 			state = "CLOSING";
346*7c478bd9Sstevel@tonic-gate 			break;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 		default:
349*7c478bd9Sstevel@tonic-gate 			state = "ILLEGAL";
350*7c478bd9Sstevel@tonic-gate 			break;
351*7c478bd9Sstevel@tonic-gate 		}
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	bcopy(&pipe_handle.p_ep, &ept_descr, sizeof (usb_ep_descr_t));
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
357*7c478bd9Sstevel@tonic-gate 		mdb_printf("\n    %<u>%-3s %5s %3s %7s %-?s %-?s %-?s%</u>\n",
358*7c478bd9Sstevel@tonic-gate 		    "EP", "TYPE ", "DIR", "STATE  ", "P_HANDLE", "P_POLICY",
359*7c478bd9Sstevel@tonic-gate 		    "EP DESCR");
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	dir = ((ept_descr.bEndpointAddress & USB_EP_DIR_MASK) &
363*7c478bd9Sstevel@tonic-gate 	    USB_EP_DIR_IN) ? "In " : "Out";
364*7c478bd9Sstevel@tonic-gate 	switch (ept_descr.bmAttributes & USB_EP_ATTR_MASK) {
365*7c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
366*7c478bd9Sstevel@tonic-gate 		type = "Cntrl";
367*7c478bd9Sstevel@tonic-gate 		break;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
370*7c478bd9Sstevel@tonic-gate 		type = "Isoch";
371*7c478bd9Sstevel@tonic-gate 		break;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
374*7c478bd9Sstevel@tonic-gate 		type = "Bulk ";
375*7c478bd9Sstevel@tonic-gate 		break;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
378*7c478bd9Sstevel@tonic-gate 		type = "Intr ";
379*7c478bd9Sstevel@tonic-gate 		break;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	default:
382*7c478bd9Sstevel@tonic-gate 		type = "*****";
383*7c478bd9Sstevel@tonic-gate 		break;
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	mdb_printf("    %3d %5s %3s %7s %-?p %-?p %-?p\n",
387*7c478bd9Sstevel@tonic-gate 	    ept_descr.bEndpointAddress & USB_EP_NUM_MASK, type, dir, state,
388*7c478bd9Sstevel@tonic-gate 	    addr, addr + offsetof(usba_pipe_handle_data_t, p_policy),
389*7c478bd9Sstevel@tonic-gate 	    addr + offsetof(usba_pipe_handle_data_t, p_ep));
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate /*
396*7c478bd9Sstevel@tonic-gate  * usba_device walker:
397*7c478bd9Sstevel@tonic-gate  *
398*7c478bd9Sstevel@tonic-gate  * walks the chain of usba_device structs headed by usba_device_list in usba.c
399*7c478bd9Sstevel@tonic-gate  * NOTE: It uses the generic list walk step routine usba_list_walk_step.
400*7c478bd9Sstevel@tonic-gate  * No walk_fini routine is needed.
401*7c478bd9Sstevel@tonic-gate  */
402*7c478bd9Sstevel@tonic-gate int
403*7c478bd9Sstevel@tonic-gate usba_device_walk_init(mdb_walk_state_t *wsp)
404*7c478bd9Sstevel@tonic-gate {
405*7c478bd9Sstevel@tonic-gate 	usba_list_entry_t	list_entry;
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
408*7c478bd9Sstevel@tonic-gate 		mdb_warn(
409*7c478bd9Sstevel@tonic-gate 		    "global walk only.  Must be invoked without an address\n");
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
412*7c478bd9Sstevel@tonic-gate 	}
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	if (mdb_readvar((void*)&list_entry, "usba_device_list") == -1) {
415*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usba_device_list");
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
418*7c478bd9Sstevel@tonic-gate 	}
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	/* List head is not part of usba_device_t, get first usba_device_t */
421*7c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)list_entry.next;
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
424*7c478bd9Sstevel@tonic-gate }
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate /*
428*7c478bd9Sstevel@tonic-gate  * usba_device dcmd
429*7c478bd9Sstevel@tonic-gate  *	Given the address of a usba_device struct, dump summary info
430*7c478bd9Sstevel@tonic-gate  *	-v:	Print more (verbose) info
431*7c478bd9Sstevel@tonic-gate  *	-p:	Walk/dump all open pipes for this usba_device
432*7c478bd9Sstevel@tonic-gate  */
433*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
434*7c478bd9Sstevel@tonic-gate int
435*7c478bd9Sstevel@tonic-gate usba_device(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	int		status;
438*7c478bd9Sstevel@tonic-gate 	char		pathname[MAXNAMELEN];
439*7c478bd9Sstevel@tonic-gate 	char		dname[MODMAXNAMELEN + 1] = "<unatt>"; /* Driver name */
440*7c478bd9Sstevel@tonic-gate 	char		drv_statep[MODMAXNAMELEN+ 10];
441*7c478bd9Sstevel@tonic-gate 	uint_t		usb_flag  = NULL;
442*7c478bd9Sstevel@tonic-gate 	boolean_t	no_driver_attached = FALSE;
443*7c478bd9Sstevel@tonic-gate 	uintptr_t	dip_addr;
444*7c478bd9Sstevel@tonic-gate 	struct dev_info	devinfo;
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
447*7c478bd9Sstevel@tonic-gate 		/* Global walk */
448*7c478bd9Sstevel@tonic-gate 		if (mdb_walk_dcmd("usba_device", "usba_device", argc,
449*7c478bd9Sstevel@tonic-gate 		    argv) == -1) {
450*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to walk usba_device");
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
453*7c478bd9Sstevel@tonic-gate 		}
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
456*7c478bd9Sstevel@tonic-gate 	}
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
459*7c478bd9Sstevel@tonic-gate 	    'p', MDB_OPT_SETBITS, USB_DUMP_ACTIVE_PIPES, &usb_flag,
460*7c478bd9Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, USB_DUMP_VERBOSE, &usb_flag, NULL) != argc) {
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
463*7c478bd9Sstevel@tonic-gate 	}
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if (usb_flag && !(DCMD_HDRSPEC(flags))) {
466*7c478bd9Sstevel@tonic-gate 		mdb_printf("\n");
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
470*7c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%-15s %4s %-?s %-42s%</u>\n",
471*7c478bd9Sstevel@tonic-gate 		    "NAME", "INST", "DIP", "PATH                             ");
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	status = usba_device2dip(addr, &dip_addr);
475*7c478bd9Sstevel@tonic-gate 	/*
476*7c478bd9Sstevel@tonic-gate 	 * -1 = error
477*7c478bd9Sstevel@tonic-gate 	 * 0 = no error, no match
478*7c478bd9Sstevel@tonic-gate 	 * 1 = no error, match
479*7c478bd9Sstevel@tonic-gate 	 */
480*7c478bd9Sstevel@tonic-gate 	if (status != 1) {
481*7c478bd9Sstevel@tonic-gate 		if (status == -1) {
482*7c478bd9Sstevel@tonic-gate 			mdb_warn("error looking for dip for usba_device %p",
483*7c478bd9Sstevel@tonic-gate 			    addr);
484*7c478bd9Sstevel@tonic-gate 		} else {
485*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to find dip for usba_device %p\n",
486*7c478bd9Sstevel@tonic-gate 			    addr);
487*7c478bd9Sstevel@tonic-gate 		}
488*7c478bd9Sstevel@tonic-gate 		mdb_warn("dip and statep unobtainable\n");
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
491*7c478bd9Sstevel@tonic-gate 	}
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	/* Figure out what driver (name) is attached to this node. */
494*7c478bd9Sstevel@tonic-gate 	(void) mdb_devinfo2driver(dip_addr, (char *)dname, sizeof (dname));
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	if (mdb_vread((void *)&devinfo, sizeof (struct dev_info),
497*7c478bd9Sstevel@tonic-gate 	    dip_addr) == -1) {
498*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read devinfo");
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
501*7c478bd9Sstevel@tonic-gate 	}
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	if (!(DDI_CF2(&devinfo))) {
504*7c478bd9Sstevel@tonic-gate 		no_driver_attached = TRUE;
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	(void) mdb_ddi_pathname(dip_addr, pathname, sizeof (pathname));
508*7c478bd9Sstevel@tonic-gate 	mdb_printf("%-15s %2d   %-?p %s\n", dname, devinfo.devi_instance,
509*7c478bd9Sstevel@tonic-gate 	    dip_addr, pathname);
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	if (usb_flag & USB_DUMP_VERBOSE) {
512*7c478bd9Sstevel@tonic-gate 		int		i;
513*7c478bd9Sstevel@tonic-gate 		uintptr_t	statep = NULL;
514*7c478bd9Sstevel@tonic-gate 		char		*string_descr;
515*7c478bd9Sstevel@tonic-gate 		char		**config_cloud, **conf_str_descr;
516*7c478bd9Sstevel@tonic-gate 		usb_dev_descr_t	usb_dev_descr;
517*7c478bd9Sstevel@tonic-gate 		usba_device_t	usba_device_struct;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 		if (mdb_vread((void *)&usba_device_struct,
520*7c478bd9Sstevel@tonic-gate 		    sizeof (usba_device_t), addr) == -1) {
521*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read usba_device struct");
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
524*7c478bd9Sstevel@tonic-gate 		}
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 		mdb_printf("    usba_device: %-16p\n\n", (usba_device_t *)addr);
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&usb_dev_descr, sizeof (usb_dev_descr),
529*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)usba_device_struct.usb_dev_descr) == -1) {
530*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read usb_dev_descr_t struct");
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
533*7c478bd9Sstevel@tonic-gate 		}
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 		mdb_printf("\n    idVendor: 0x%04x idProduct: 0x%04x "
536*7c478bd9Sstevel@tonic-gate 		    "usb_addr: 0x%02x\n", usb_dev_descr.idVendor,
537*7c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idProduct, usba_device_struct.usb_addr);
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 		/* Get the string descriptor string into local space. */
540*7c478bd9Sstevel@tonic-gate 		string_descr = (char *)mdb_alloc(USB_MAXSTRINGLEN, UM_GC);
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 		if (usba_device_struct.usb_mfg_str == NULL) {
543*7c478bd9Sstevel@tonic-gate 			(void) strcpy(string_descr, "<No Manufacturer String>");
544*7c478bd9Sstevel@tonic-gate 		} else {
545*7c478bd9Sstevel@tonic-gate 			if (mdb_readstr(string_descr, USB_MAXSTRINGLEN,
546*7c478bd9Sstevel@tonic-gate 			    (uintptr_t)usba_device_struct.usb_mfg_str) == -1) {
547*7c478bd9Sstevel@tonic-gate 				mdb_warn("failed to read manufacturer "
548*7c478bd9Sstevel@tonic-gate 				    "string descriptor");
549*7c478bd9Sstevel@tonic-gate 				(void) strcpy(string_descr, "???");
550*7c478bd9Sstevel@tonic-gate 			}
551*7c478bd9Sstevel@tonic-gate 		}
552*7c478bd9Sstevel@tonic-gate 		mdb_printf("\n    Manufacturer String:\t%s\n", string_descr);
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 		if (usba_device_struct.usb_product_str == NULL) {
555*7c478bd9Sstevel@tonic-gate 			(void) strcpy(string_descr, "<No Product String>");
556*7c478bd9Sstevel@tonic-gate 		} else {
557*7c478bd9Sstevel@tonic-gate 			if (mdb_readstr(string_descr, USB_MAXSTRINGLEN,
558*7c478bd9Sstevel@tonic-gate 			    (uintptr_t)usba_device_struct.usb_product_str) ==
559*7c478bd9Sstevel@tonic-gate 			    -1) {
560*7c478bd9Sstevel@tonic-gate 				mdb_warn("failed to read product string "
561*7c478bd9Sstevel@tonic-gate 				    "descriptor");
562*7c478bd9Sstevel@tonic-gate 				(void) strcpy(string_descr, "???");
563*7c478bd9Sstevel@tonic-gate 			}
564*7c478bd9Sstevel@tonic-gate 		}
565*7c478bd9Sstevel@tonic-gate 		mdb_printf("    Product String:\t\t%s\n", string_descr);
566*7c478bd9Sstevel@tonic-gate 
567*7c478bd9Sstevel@tonic-gate 		if (usba_device_struct.usb_serialno_str == NULL) {
568*7c478bd9Sstevel@tonic-gate 			(void) strcpy(string_descr, "<No SerialNumber String>");
569*7c478bd9Sstevel@tonic-gate 		} else {
570*7c478bd9Sstevel@tonic-gate 			if (mdb_readstr(string_descr, USB_MAXSTRINGLEN,
571*7c478bd9Sstevel@tonic-gate 			    (uintptr_t)usba_device_struct.usb_serialno_str) ==
572*7c478bd9Sstevel@tonic-gate 			    -1) {
573*7c478bd9Sstevel@tonic-gate 				mdb_warn("failed to read serial number string "
574*7c478bd9Sstevel@tonic-gate 				    "descriptor");
575*7c478bd9Sstevel@tonic-gate 				(void) strcpy(string_descr, "???");
576*7c478bd9Sstevel@tonic-gate 			}
577*7c478bd9Sstevel@tonic-gate 		}
578*7c478bd9Sstevel@tonic-gate 		mdb_printf("    SerialNumber String:\t%s\n", string_descr);
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 		if (no_driver_attached) {
581*7c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
582*7c478bd9Sstevel@tonic-gate 		} else {
583*7c478bd9Sstevel@tonic-gate 			mdb_printf("      state_p: ");
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 			/*
586*7c478bd9Sstevel@tonic-gate 			 * Given the dip, find the associated statep. The
587*7c478bd9Sstevel@tonic-gate 			 * convention to generate this soft state anchor is:
588*7c478bd9Sstevel@tonic-gate 			 *	<driver_name>_statep
589*7c478bd9Sstevel@tonic-gate 			 */
590*7c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(drv_statep, sizeof (drv_statep),
591*7c478bd9Sstevel@tonic-gate 			    "%s_statep", dname);
592*7c478bd9Sstevel@tonic-gate 			if (mdb_devinfo2statep(dip_addr, drv_statep,
593*7c478bd9Sstevel@tonic-gate 			    &statep) == -1) {
594*7c478bd9Sstevel@tonic-gate 				mdb_warn("failed to find %s state struct for "
595*7c478bd9Sstevel@tonic-gate 				    "dip %p", drv_statep, dip_addr);
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 				return (DCMD_ERR);
598*7c478bd9Sstevel@tonic-gate 			}
599*7c478bd9Sstevel@tonic-gate 			mdb_printf("%-?p\n", statep);
600*7c478bd9Sstevel@tonic-gate 		}
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 		config_cloud = (char **)mdb_alloc(sizeof (void *) *
603*7c478bd9Sstevel@tonic-gate 		    usba_device_struct.usb_n_cfgs, UM_GC);
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 		conf_str_descr = (char **)mdb_alloc(sizeof (void *) *
606*7c478bd9Sstevel@tonic-gate 		    usba_device_struct.usb_n_cfgs, UM_GC);
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 		if ((usba_device_struct.usb_cfg_array) &&
609*7c478bd9Sstevel@tonic-gate 		    (usba_device_struct.usb_cfg_str_descr)) {
610*7c478bd9Sstevel@tonic-gate 			if ((mdb_vread(config_cloud,  sizeof (void *) *
611*7c478bd9Sstevel@tonic-gate 			    usba_device_struct.usb_n_cfgs,
612*7c478bd9Sstevel@tonic-gate 			    (uintptr_t)usba_device_struct.usb_cfg_array) ==
613*7c478bd9Sstevel@tonic-gate 			    -1) || (mdb_vread(conf_str_descr, sizeof (void *)
614*7c478bd9Sstevel@tonic-gate 			    * usba_device_struct.usb_n_cfgs, (uintptr_t)
615*7c478bd9Sstevel@tonic-gate 			    usba_device_struct.usb_cfg_str_descr)) == -1) {
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate 			    mdb_warn("failed to read config cloud pointers");
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 			} else {
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 				mdb_printf("\n    Device Config Clouds:\n"
622*7c478bd9Sstevel@tonic-gate 				    "    Index\tConfig\t\tConfiguration "
623*7c478bd9Sstevel@tonic-gate 				    "String\n"
624*7c478bd9Sstevel@tonic-gate 				    "    -----\t------\t\t"
625*7c478bd9Sstevel@tonic-gate 				    "--------------------\n");
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < usba_device_struct.usb_n_cfgs;
628*7c478bd9Sstevel@tonic-gate 				    i++) {
629*7c478bd9Sstevel@tonic-gate 					if (mdb_readstr(string_descr,
630*7c478bd9Sstevel@tonic-gate 					    USB_MAXSTRINGLEN,
631*7c478bd9Sstevel@tonic-gate 					    (uintptr_t)conf_str_descr[i]) ==
632*7c478bd9Sstevel@tonic-gate 					    -1) {
633*7c478bd9Sstevel@tonic-gate 						(void) strcpy(string_descr,
634*7c478bd9Sstevel@tonic-gate 						    "<No Configuration "
635*7c478bd9Sstevel@tonic-gate 						    "String>");
636*7c478bd9Sstevel@tonic-gate 					}
637*7c478bd9Sstevel@tonic-gate 					mdb_printf("    %4d\t0x%p\t%s\n", i,
638*7c478bd9Sstevel@tonic-gate 					    config_cloud[i], string_descr);
639*7c478bd9Sstevel@tonic-gate 				}
640*7c478bd9Sstevel@tonic-gate 			}
641*7c478bd9Sstevel@tonic-gate 		}
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate 		mdb_printf("\n    Active configuration index: %d\n",
644*7c478bd9Sstevel@tonic-gate 		    usba_device_struct.usb_active_cfg_ndx);
645*7c478bd9Sstevel@tonic-gate 	}
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 	if (usb_flag & USB_DUMP_ACTIVE_PIPES) {
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 		if (mdb_pwalk_dcmd("usb_pipe_handle", "usb_pipe_handle",
650*7c478bd9Sstevel@tonic-gate 		    0, NULL, addr) == -1) {
651*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to walk usb_pipe_handle");
652*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
653*7c478bd9Sstevel@tonic-gate 		}
654*7c478bd9Sstevel@tonic-gate 	}
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
657*7c478bd9Sstevel@tonic-gate }
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate /*
661*7c478bd9Sstevel@tonic-gate  * Dump the contents of the usba_debug_buf, from the oldest to newest,
662*7c478bd9Sstevel@tonic-gate  * wrapping around if necessary.
663*7c478bd9Sstevel@tonic-gate  */
664*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
665*7c478bd9Sstevel@tonic-gate int
666*7c478bd9Sstevel@tonic-gate usba_debug_buf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
667*7c478bd9Sstevel@tonic-gate {
668*7c478bd9Sstevel@tonic-gate 	char	*debug_buf_addr;	/* addr in core */
669*7c478bd9Sstevel@tonic-gate 	char	*local_debug_buf;	/* local copy of buf */
670*7c478bd9Sstevel@tonic-gate 	int	debug_buf_size;
671*7c478bd9Sstevel@tonic-gate 	char	*term_p;
672*7c478bd9Sstevel@tonic-gate 	int	being_cleared;
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
677*7c478bd9Sstevel@tonic-gate 	}
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate 	if (mdb_readvar((void*)&being_cleared, "usba_clear_debug_buf_flag") ==
680*7c478bd9Sstevel@tonic-gate 	    -1) {
681*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usba_clear_debug_buf_flag");
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
684*7c478bd9Sstevel@tonic-gate 	}
685*7c478bd9Sstevel@tonic-gate 	if (being_cleared) {
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
688*7c478bd9Sstevel@tonic-gate 	}
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate 	if (mdb_readvar((void*)&debug_buf_addr, "usba_debug_buf") == -1) {
691*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usba_debug_buf");
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
694*7c478bd9Sstevel@tonic-gate 	}
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 	if (debug_buf_addr == NULL) {
697*7c478bd9Sstevel@tonic-gate 		mdb_warn("usba_debug_buf not allocated\n");
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
700*7c478bd9Sstevel@tonic-gate 	}
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 	if (mdb_readvar((void*)&debug_buf_size, "usba_debug_buf_size") == -1) {
704*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usba_debug_buf_size");
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
707*7c478bd9Sstevel@tonic-gate 	}
708*7c478bd9Sstevel@tonic-gate 
709*7c478bd9Sstevel@tonic-gate 	debug_buf_size += USB_DEBUG_SIZE_EXTRA_ALLOC;
710*7c478bd9Sstevel@tonic-gate 	local_debug_buf = (char *)mdb_alloc(debug_buf_size, UM_SLEEP | UM_GC);
711*7c478bd9Sstevel@tonic-gate 
712*7c478bd9Sstevel@tonic-gate 	if ((int)(mdb_vread(local_debug_buf, debug_buf_size,
713*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)debug_buf_addr)) == -1) {
714*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read usba_debug_buf at %p",
715*7c478bd9Sstevel@tonic-gate 		    local_debug_buf);
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
718*7c478bd9Sstevel@tonic-gate 	}
719*7c478bd9Sstevel@tonic-gate 	local_debug_buf[debug_buf_size - 1] = '\0';
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate 	if (strlen(local_debug_buf) == NULL) {
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
724*7c478bd9Sstevel@tonic-gate 	}
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate 	if ((term_p = strstr(local_debug_buf, ">>>>")) == NULL) {
727*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to find terminator \">>>>\"\n");
728*7c478bd9Sstevel@tonic-gate 
729*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
730*7c478bd9Sstevel@tonic-gate 	}
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate 	/*
733*7c478bd9Sstevel@tonic-gate 	 * Print the chunk of buffer from the terminator to the end.
734*7c478bd9Sstevel@tonic-gate 	 * This will print a null string if no wrap has occurred yet.
735*7c478bd9Sstevel@tonic-gate 	 */
736*7c478bd9Sstevel@tonic-gate 	mdb_printf("%s", term_p+5);	/* after >>>>\0 to end of buf */
737*7c478bd9Sstevel@tonic-gate 	mdb_printf("%s\n", local_debug_buf);	/* beg of buf to >>>>\0 */
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
740*7c478bd9Sstevel@tonic-gate }
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
743*7c478bd9Sstevel@tonic-gate int
744*7c478bd9Sstevel@tonic-gate usba_clear_debug_buf(
745*7c478bd9Sstevel@tonic-gate 	uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
746*7c478bd9Sstevel@tonic-gate {
747*7c478bd9Sstevel@tonic-gate 	int clear = 1;
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate 	/* stop the tracing */
750*7c478bd9Sstevel@tonic-gate 	if (mdb_writevar((void*)&clear, "usba_clear_debug_buf_flag") == -1) {
751*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to set usba_clear_debug_buf_flag");
752*7c478bd9Sstevel@tonic-gate 
753*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
754*7c478bd9Sstevel@tonic-gate 	}
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
757*7c478bd9Sstevel@tonic-gate }
758*7c478bd9Sstevel@tonic-gate 
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate /*
761*7c478bd9Sstevel@tonic-gate  * MDB module linkage information:
762*7c478bd9Sstevel@tonic-gate  *
763*7c478bd9Sstevel@tonic-gate  * We declare a list of structures describing our dcmds, and a function
764*7c478bd9Sstevel@tonic-gate  * named _mdb_init to return a pointer to our module information.
765*7c478bd9Sstevel@tonic-gate  */
766*7c478bd9Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
767*7c478bd9Sstevel@tonic-gate 	{ "usb_pipe_handle", ":",
768*7c478bd9Sstevel@tonic-gate 	    "print a usb_pipe_handle struct", usb_pipe_handle, NULL},
769*7c478bd9Sstevel@tonic-gate 	{ "usba_device", ": [-pv]",
770*7c478bd9Sstevel@tonic-gate 	    "print summary info for a usba_device_t struct", usba_device, NULL},
771*7c478bd9Sstevel@tonic-gate 	{ "usba_debug_buf", NULL,
772*7c478bd9Sstevel@tonic-gate 	    "print usba_debug_buf", usba_debug_buf, NULL},
773*7c478bd9Sstevel@tonic-gate 	{ "usba_clear_debug_buf", NULL,
774*7c478bd9Sstevel@tonic-gate 	    "clear usba_debug_buf", usba_clear_debug_buf, NULL},
775*7c478bd9Sstevel@tonic-gate 	{ NULL }
776*7c478bd9Sstevel@tonic-gate };
777*7c478bd9Sstevel@tonic-gate 
778*7c478bd9Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
779*7c478bd9Sstevel@tonic-gate 	/* Generic list walker. */
780*7c478bd9Sstevel@tonic-gate 	{ "usba_list_entry", "walk list of usba_list_entry_t structures",
781*7c478bd9Sstevel@tonic-gate 	    usba_list_walk_init, usba_list_walk_step, NULL, NULL },
782*7c478bd9Sstevel@tonic-gate 	{ "usb_pipe_handle", "walk USB pipe handles, given a usba_device_t ptr",
783*7c478bd9Sstevel@tonic-gate 	    usb_pipe_handle_walk_init, usb_pipe_handle_walk_step, NULL, NULL },
784*7c478bd9Sstevel@tonic-gate 	{ "usba_device", "walk global list of usba_device_t structures",
785*7c478bd9Sstevel@tonic-gate 	    usba_device_walk_init, usba_list_walk_step, NULL, NULL },
786*7c478bd9Sstevel@tonic-gate 	{ NULL }
787*7c478bd9Sstevel@tonic-gate };
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
790*7c478bd9Sstevel@tonic-gate 	MDB_API_VERSION, dcmds, walkers
791*7c478bd9Sstevel@tonic-gate };
792*7c478bd9Sstevel@tonic-gate 
793*7c478bd9Sstevel@tonic-gate const mdb_modinfo_t *
794*7c478bd9Sstevel@tonic-gate _mdb_init(void)
795*7c478bd9Sstevel@tonic-gate {
796*7c478bd9Sstevel@tonic-gate 	return (&modinfo);
797*7c478bd9Sstevel@tonic-gate }
798