]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/nusb/lib/dump.c
etheriwl: don't break controller on command flush timeout
[plan9front.git] / sys / src / cmd / nusb / lib / dump.c
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "usb.h"
5
6 int usbdebug;
7
8 static char *edir[] = {"in", "out", "inout"};
9 static char *etype[] = {"ctl", "iso", "bulk", "intr"};
10 static char* cnames[] =
11 {
12         "none", "audio", "comms", "hid", "",
13         "", "", "printer", "storage", "hub", "data"
14 };
15 static char* devstates[] =
16 {
17         "detached", "attached", "enabled", "assigned", "configured"
18 };
19
20 char*
21 classname(int c)
22 {
23         static char buf[30];
24
25         if(c >= 0 && c < nelem(cnames))
26                 return cnames[c];
27         else{
28                 seprint(buf, buf+30, "%d", c);
29                 return buf;
30         }
31 }
32
33 char *
34 hexstr(void *a, int n)
35 {
36         int i;
37         char *dbuff, *s, *e;
38         uchar *b;
39
40         b = a;
41         dbuff = s = emallocz(1024, 0);
42         *s = 0;
43         e = s + 1024;
44         for(i = 0; i < n; i++)
45                 s = seprint(s, e, " %.2ux", b[i]);
46         if(s == e)
47                 fprint(2, "%s: usb/lib: hexdump: bug: small buffer\n", argv0);
48         return dbuff;
49 }
50
51 static void
52 fmtprintiface(Fmt *f, Iface *i)
53 {
54         int     j;
55         Altc    *a;
56         Ep      *ep;
57         char    *eds, *ets;
58
59         fmtprint(f, "\t\tiface csp %s.%uld.%uld\n",
60                 classname(Class(i->csp)), Subclass(i->csp), Proto(i->csp));
61         for(j = 0; j < Naltc; j++){
62                 a=i->altc[j];
63                 if(a == nil)
64                         break;
65                 fmtprint(f, "\t\t  alt %d attr %d ival %d",
66                         j, a->attrib, a->interval);
67                 if(a->aux != nil)
68                         fmtprint(f, " devspec %p\n", a->aux);
69                 else
70                         fmtprint(f, "\n");
71         }
72         for(j = 0; j < Nep; j++){
73                 ep = i->ep[j];
74                 if(ep == nil)
75                         break;
76                 eds = ets = "";
77                 if(ep->dir <= nelem(edir))
78                         eds = edir[ep->dir];
79                 if(ep->type <= nelem(etype))
80                         ets = etype[ep->type];
81                 fmtprint(f, "\t\t  ep id %d addr %d dir %s type %s"
82                         " itype %d maxpkt %d ntds %d\n",
83                         ep->id, ep->addr, eds, ets, ep->isotype,
84                         ep->maxpkt, ep->ntds);
85         }
86 }
87
88 static void
89 fmtprintconf(Fmt *f, Usbdev *d, int ci)
90 {
91         int i;
92         Conf *c;
93         char *hd;
94
95         c = d->conf[ci];
96         fmtprint(f, "\tconf: cval %d attrib %x %d mA\n",
97                 c->cval, c->attrib, c->milliamps);
98         for(i = 0; i < Niface; i++)
99                 if(c->iface[i] == nil)
100                         break;
101                 else
102                         fmtprintiface(f, c->iface[i]);
103         for(i = 0; i < Nddesc; i++)
104                 if(d->ddesc[i] == nil)
105                         break;
106                 else if(d->ddesc[i]->conf == c){
107                         hd = hexstr((uchar*)&d->ddesc[i]->data,
108                                 d->ddesc[i]->data.bLength);
109                         fmtprint(f, "\t\tdev desc %x[%d]: %s\n",
110                                 d->ddesc[i]->data.bDescriptorType,
111                                 d->ddesc[i]->data.bLength, hd);
112                         free(hd);
113                 }
114 }
115
116 int
117 Ufmt(Fmt *f)
118 {
119         int i;
120         Dev *d;
121         Usbdev *ud;
122
123         d = va_arg(f->args, Dev*);
124         if(d == nil)
125                 return fmtprint(f, "<nildev>\n");
126         fmtprint(f, "%s", d->dir);
127         ud = d->usb;
128         if(ud == nil)
129                 return fmtprint(f, " %ld refs\n", d->ref);
130         fmtprint(f, " csp %s.%uld.%uld",
131                 classname(Class(ud->csp)), Subclass(ud->csp), Proto(ud->csp));
132         fmtprint(f, " vid %#ux did %#ux", ud->vid, ud->did);
133         fmtprint(f, " refs %ld\n", d->ref);
134         fmtprint(f, "\t%s %s %s\n", ud->vendor, ud->product, ud->serial);
135         for(i = 0; i < Nconf; i++){
136                 if(ud->conf[i] == nil)
137                         break;
138                 else
139                         fmtprintconf(f, ud, i);
140         }
141         return 0;
142 }
143
144 char*
145 estrdup(char *s)
146 {
147         char *d;
148
149         d = strdup(s);
150         if(d == nil)
151                 sysfatal("strdup: %r");
152         setmalloctag(d, getcallerpc(&s));
153         return d;
154 }
155
156 void*
157 emallocz(ulong size, int zero)
158 {
159         void *x;
160
161         x = malloc(size);
162         if(x == nil)
163                 sysfatal("malloc: %r");
164         if(zero)
165                 memset(x, 0, size);
166         setmalloctag(x, getcallerpc(&size));
167         return x;
168 }
169