]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/nusb/lib/dump.c
usb: fix wrong pollival calculation in setmaxpkt()
[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 char *
52 seprintiface(char *s, char *e, Iface *i)
53 {
54         int     j;
55         Altc    *a;
56         Ep      *ep;
57         char    *eds, *ets;
58
59         s = seprint(s, e, "\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                 s = seprint(s, e, "\t\t  alt %d attr %d ival %d",
66                         j, a->attrib, a->interval);
67                 if(a->aux != nil)
68                         s = seprint(s, e, " devspec %p\n", a->aux);
69                 else
70                         s = seprint(s, e, "\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                 s = seprint(s, e, "\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         return s;
87 }
88
89 static char*
90 seprintconf(char *s, char *e, Usbdev *d, int ci)
91 {
92         int i;
93         Conf *c;
94         char *hd;
95
96         c = d->conf[ci];
97         s = seprint(s, e, "\tconf: cval %d attrib %x %d mA\n",
98                 c->cval, c->attrib, c->milliamps);
99         for(i = 0; i < Niface; i++)
100                 if(c->iface[i] == nil)
101                         break;
102                 else
103                         s = seprintiface(s, e, c->iface[i]);
104         for(i = 0; i < Nddesc; i++)
105                 if(d->ddesc[i] == nil)
106                         break;
107                 else if(d->ddesc[i]->conf == c){
108                         hd = hexstr((uchar*)&d->ddesc[i]->data,
109                                 d->ddesc[i]->data.bLength);
110                         s = seprint(s, e, "\t\tdev desc %x[%d]: %s\n",
111                                 d->ddesc[i]->data.bDescriptorType,
112                                 d->ddesc[i]->data.bLength, hd);
113                         free(hd);
114                 }
115         return s;
116 }
117
118 int
119 Ufmt(Fmt *f)
120 {
121         int i;
122         Dev *d;
123         Usbdev *ud;
124         char buf[1024];
125         char *s, *e;
126
127         s = buf;
128         e = buf+sizeof(buf);
129         d = va_arg(f->args, Dev*);
130         if(d == nil)
131                 return fmtprint(f, "<nildev>\n");
132         s = seprint(s, e, "%s", d->dir);
133         ud = d->usb;
134         if(ud == nil)
135                 return fmtprint(f, "%s %ld refs\n", buf, d->ref);
136         s = seprint(s, e, " csp %s.%uld.%uld",
137                 classname(Class(ud->csp)), Subclass(ud->csp), Proto(ud->csp));
138         s = seprint(s, e, " vid %#ux did %#ux", ud->vid, ud->did);
139         s = seprint(s, e, " refs %ld\n", d->ref);
140         s = seprint(s, e, "\t%s %s %s\n", ud->vendor, ud->product, ud->serial);
141         for(i = 0; i < Nconf; i++){
142                 if(ud->conf[i] == nil)
143                         break;
144                 else
145                         s = seprintconf(s, e, ud, i);
146         }
147         return fmtprint(f, "%s", buf);
148 }
149
150 char*
151 estrdup(char *s)
152 {
153         char *d;
154
155         d = strdup(s);
156         if(d == nil)
157                 sysfatal("strdup: %r");
158         setmalloctag(d, getcallerpc(&s));
159         return d;
160 }
161
162 void*
163 emallocz(ulong size, int zero)
164 {
165         void *x;
166
167         x = malloc(size);
168         if(x == nil)
169                 sysfatal("malloc: %r");
170         if(zero)
171                 memset(x, 0, size);
172         setmalloctag(x, getcallerpc(&size));
173         return x;
174 }
175