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