1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  USB debugging code			File: usbdebug.c
5    *
6    *  This module contains debug code for USB.
7    *
8    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
9    *
10    *********************************************************************
11    *
12    *  Copyright 2000,2001,2002,2003
13    *  Broadcom Corporation. All rights reserved.
14    *
15    *  This software is furnished under license and may be used and
16    *  copied only in accordance with the following terms and
17    *  conditions.  Subject to these conditions, you may download,
18    *  copy, install, use, modify and distribute modified or unmodified
19    *  copies of this software in source and/or binary form.  No title
20    *  or ownership is transferred hereby.
21    *
22    *  1) Any source code used, modified or distributed must reproduce
23    *     and retain this copyright notice and list of conditions
24    *     as they appear in the source file.
25    *
26    *  2) No right is granted to use any trade name, trademark, or
27    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
28    *     name may not be used to endorse or promote products derived
29    *     from this software without the prior written permission of
30    *     Broadcom Corporation.
31    *
32    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44    *     THE POSSIBILITY OF SUCH DAMAGE.
45    ********************************************************************* */
46
47
48#ifndef _CFE_
49#include <stdio.h>
50#include <time.h>
51#include <memory.h>
52#include <stdint.h>
53#include "usbhack.h"
54#else
55#include "lib_types.h"
56#include "lib_string.h"
57#include "lib_printf.h"
58#endif
59
60#include "lib_malloc.h"
61#include "lib_queue.h"
62#include "usbchap9.h"
63#include "usbd.h"
64
65
66void usb_dbg_dumpportstatus(int port,usb_port_status_t *portstatus,int level)
67{
68    int idx;
69    uint16_t x;
70
71    for (idx = 0; idx < level; idx++) printf("  ");
72    printf("PORT %d STATUS\n",port);
73
74    for (idx = 0; idx < level; idx++) printf("  ");
75    x = GETUSBFIELD((portstatus),wPortStatus);
76    printf("wPortStatus     = %04X  ",x);
77    if (x & 1) printf("DevicePresent ");
78    if (x & 2) printf("Enabled ");
79    if (x & 4) printf("Suspend ");
80    if (x & 8) printf("OverCurrent ");
81    if (x & 16) printf("InReset ");
82    if (x & 256) printf("Powered ");
83    if (x & 512) printf("LowSpeed ");
84    printf("\n");
85    for (idx = 0; idx < level; idx++) printf("  ");
86    x = GETUSBFIELD((portstatus),wPortChange);
87    printf("wPortChange     = %04X  ",x);
88    if (x & 1) printf("ConnectChange ");
89    if (x & 2) printf("EnableChange ");
90    if (x & 4) printf("SuspendChange ");
91    if (x & 8) printf("OverCurrentChange ");
92    if (x & 16) printf("ResetChange ");
93    printf("\n");
94}
95
96void usb_dbg_dumpeptdescr(usb_endpoint_descr_t * epdscr)
97{
98    printf("---------------------------------------------------\n");
99    printf("ENDPOINT DESCRIPTOR\n");
100
101    printf("bLength         = %d\n",epdscr->bLength);
102    printf("bDescriptorType = %d\n",epdscr->bDescriptorType);
103    printf("bEndpointAddr   = %02X\n",epdscr->bEndpointAddress);
104    printf("bmAttrbutes     = %02X\n",epdscr->bmAttributes);
105    printf("wMaxPacketSize  = %d\n",GETUSBFIELD(epdscr,wMaxPacketSize));
106    printf("bInterval       = %d\n",epdscr->bInterval);
107}
108
109static char *getstringmaybe(usbdev_t *dev,int string)
110{
111    static char buf[256];
112
113    return "";
114
115    if (string == 0) {
116	strcpy(buf,"none");
117	return buf;
118	}
119
120    memset(buf,0,sizeof(buf));
121
122    usb_get_string(dev,string,buf,sizeof(buf));
123
124    return buf;
125}
126
127void usb_dbg_dumpdescriptors(usbdev_t *dev,uint8_t *ptr,int len)
128{
129    uint8_t *endptr;
130    usb_config_descr_t  *cfgdscr;
131    usb_interface_descr_t *ifdscr;
132    usb_device_descr_t *devdscr;
133    usb_endpoint_descr_t *epdscr;
134    usb_hid_descr_t *hiddscr;
135    usb_hub_descr_t *hubdscr;
136    static char *eptattribs[4] = {"Control","Isoc","Bulk","Interrupt"};
137    int idx;
138
139    endptr = ptr + len;
140
141    while (ptr < endptr) {
142
143	cfgdscr = (usb_config_descr_t *) ptr;
144
145	switch (cfgdscr->bDescriptorType) {
146	    case USB_DEVICE_DESCRIPTOR_TYPE:
147		devdscr = (usb_device_descr_t *) ptr;
148		printf("---------------------------------------------------\n");
149		printf("DEVICE DESCRIPTOR\n");
150		printf("bLength         = %d\n",devdscr->bLength);
151		printf("bDescriptorType = %d\n",devdscr->bDescriptorType);
152		printf("bcdUSB          = %04X\n",GETUSBFIELD(devdscr,bcdUSB));
153		printf("bDeviceClass    = %d\n",devdscr->bDeviceClass);
154		printf("bDeviceSubClass = %d\n",devdscr->bDeviceSubClass);
155		printf("bDeviceProtocol = %d\n",devdscr->bDeviceProtocol);
156		printf("bMaxPktSize0    = %d\n",devdscr->bMaxPacketSize0);
157		if (endptr-ptr <= 8) break;
158		printf("idVendor        = %04X (%d)\n",
159		       GETUSBFIELD(devdscr,idVendor),
160		       GETUSBFIELD(devdscr,idVendor));
161		printf("idProduct       = %04X (%d)\n",
162		       GETUSBFIELD(devdscr,idProduct),
163		       GETUSBFIELD(devdscr,idProduct));
164		printf("bcdDevice       = %04X\n",GETUSBFIELD(devdscr,bcdDevice));
165		printf("iManufacturer   = %d (%s)\n",
166		       devdscr->iManufacturer,
167		       getstringmaybe(dev,devdscr->iManufacturer));
168		printf("iProduct        = %d (%s)\n",
169		       devdscr->iProduct,
170		       getstringmaybe(dev,devdscr->iProduct));
171		printf("iSerialNumber   = %d (%s)\n",
172		       devdscr->iSerialNumber,
173		       getstringmaybe(dev,devdscr->iSerialNumber));
174		printf("bNumConfigs     = %d\n",devdscr->bNumConfigurations);
175		break;
176	    case USB_CONFIGURATION_DESCRIPTOR_TYPE:
177
178		cfgdscr = (usb_config_descr_t *) ptr;
179		printf("---------------------------------------------------\n");
180		printf("CONFIG DESCRIPTOR\n");
181
182		printf("bLength         = %d\n",cfgdscr->bLength);
183		printf("bDescriptorType = %d\n",cfgdscr->bDescriptorType);
184		printf("wTotalLength    = %d\n",GETUSBFIELD(cfgdscr,wTotalLength));
185		printf("bNumInterfaces  = %d\n",cfgdscr->bNumInterfaces);
186		printf("bConfigValue    = %d\n",cfgdscr->bConfigurationValue);
187		printf("iConfiguration  = %d (%s)\n",
188		       cfgdscr->iConfiguration,
189		       getstringmaybe(dev,cfgdscr->iConfiguration));
190		printf("bmAttributes    = %02X\n",cfgdscr->bmAttributes);
191		printf("MaxPower        = %d (%dma)\n",cfgdscr->MaxPower,cfgdscr->MaxPower*2);
192		break;
193
194	    case USB_INTERFACE_DESCRIPTOR_TYPE:
195		printf("---------------------------------------------------\n");
196		printf("INTERFACE DESCRIPTOR\n");
197
198		ifdscr = (usb_interface_descr_t *) ptr;
199
200		printf("bLength         = %d\n",ifdscr->bLength);
201		printf("bDescriptorType = %d\n",ifdscr->bDescriptorType);
202		printf("bInterfaceNum   = %d\n",ifdscr->bInterfaceNumber);
203		printf("bAlternateSet   = %d\n",ifdscr->bAlternateSetting);
204		printf("bNumEndpoints   = %d\n",ifdscr->bNumEndpoints);
205		printf("bInterfaceClass = %d\n",ifdscr->bInterfaceClass);
206		printf("bInterSubClass  = %d\n",ifdscr->bInterfaceSubClass);
207		printf("bInterfaceProto = %d\n",ifdscr->bInterfaceProtocol);
208		printf("iInterface      = %d (%s)\n",
209		       ifdscr->iInterface,
210		       getstringmaybe(dev,ifdscr->iInterface));
211		break;
212
213	    case USB_ENDPOINT_DESCRIPTOR_TYPE:
214		printf("---------------------------------------------------\n");
215		printf("ENDPOINT DESCRIPTOR\n");
216
217		epdscr = (usb_endpoint_descr_t *) ptr;
218
219		printf("bLength         = %d\n",epdscr->bLength);
220		printf("bDescriptorType = %d\n",epdscr->bDescriptorType);
221		printf("bEndpointAddr   = %02X (%d,%s)\n",
222		       epdscr->bEndpointAddress,
223		       epdscr->bEndpointAddress & 0x0F,
224		       (epdscr->bEndpointAddress & USB_ENDPOINT_DIRECTION_IN) ? "IN" : "OUT"
225		       );
226		printf("bmAttrbutes     = %02X (%s)\n",
227		       epdscr->bmAttributes,
228		       eptattribs[epdscr->bmAttributes&3]);
229		printf("wMaxPacketSize  = %d\n",GETUSBFIELD(epdscr,wMaxPacketSize));
230		printf("bInterval       = %d\n",epdscr->bInterval);
231		break;
232
233	    case USB_HID_DESCRIPTOR_TYPE:
234		printf("---------------------------------------------------\n");
235		printf("HID DESCRIPTOR\n");
236
237		hiddscr = (usb_hid_descr_t *) ptr;
238
239		printf("bLength         = %d\n",hiddscr->bLength);
240		printf("bDescriptorType = %d\n",hiddscr->bDescriptorType);
241		printf("bcdHID          = %04X\n",GETUSBFIELD(hiddscr,bcdHID));
242		printf("bCountryCode    = %d\n",hiddscr->bCountryCode);
243		printf("bNumDescriptors = %d\n",hiddscr->bNumDescriptors);
244		printf("bClassDescrType = %d\n",hiddscr->bClassDescrType);
245		printf("wClassDescrLen  = %d\n",GETUSBFIELD(hiddscr,wClassDescrLength));
246		break;
247
248	    case USB_HUB_DESCRIPTOR_TYPE:
249		printf("---------------------------------------------------\n");
250		printf("HUB DESCRIPTOR\n");
251
252		hubdscr = (usb_hub_descr_t *) ptr;
253
254		printf("bLength         = %d\n",hubdscr->bDescriptorLength);
255		printf("bDescriptorType = %d\n",hubdscr->bDescriptorType);
256		printf("bNumberOfPorts  = %d\n",hubdscr->bNumberOfPorts);
257		printf("wHubCharacters  = %04X\n",GETUSBFIELD(hubdscr,wHubCharacteristics));
258		printf("bPowerOnToPwrGd = %d\n",hubdscr->bPowerOnToPowerGood);
259		printf("bHubControlCurr = %d (ma)\n",hubdscr->bHubControlCurrent);
260		printf("bRemPwerMask[0] = %02X\n",hubdscr->bRemoveAndPowerMask[0]);
261
262		break;
263
264	    default:
265		printf("---------------------------------------------------\n");
266		printf("UNKNOWN DESCRIPTOR\n");
267		printf("bLength         = %d\n",cfgdscr->bLength);
268		printf("bDescriptorType = %d\n",cfgdscr->bDescriptorType);
269		printf("Data Bytes      = ");
270		for (idx = 0; idx < cfgdscr->bLength; idx++) {
271		    printf("%02X ",ptr[idx]);
272		    }
273		printf("\n");
274
275	    }
276
277	ptr += cfgdscr->bLength;
278
279	}
280}
281
282
283void usb_dbg_dumpcfgdescr(usbdev_t *dev)
284{
285    uint8_t buffer[512];
286    int res;
287    int len;
288    usb_config_descr_t  *cfgdscr;
289
290    memset(buffer,0,sizeof(buffer));
291
292    cfgdscr = (usb_config_descr_t *) &buffer[0];
293
294    res = usb_get_config_descriptor(dev,cfgdscr,0,sizeof(usb_config_descr_t));
295    if (res != sizeof(usb_config_descr_t)) {
296	printf("[a]usb_get_config_descriptor returns %d\n",res);
297	}
298
299    len = GETUSBFIELD(cfgdscr,wTotalLength);
300
301    res = usb_get_config_descriptor(dev,cfgdscr,0,len);
302    if (res != len) {
303	printf("[b]usb_get_config_descriptor returns %d\n",res);
304	}
305
306    usb_dbg_dumpdescriptors(dev,&buffer[0],res);
307}
308