ref: f7486cc764978365d3473ab38aec33da00319b64
parent: 490ebc54bb9b46a5719f059da6aef5b8b381eecf
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Feb 9 18:50:11 EST 2025
nusb/lib: get rid of hexstr(), just use %H encodefmt.
--- a/sys/src/cmd/nusb/disk/disk.c
+++ b/sys/src/cmd/nusb/disk/disk.c
@@ -1074,6 +1074,7 @@
}ARGEND
if(argc != 1)
usage();
+ fmtinstall('H', encodefmt); /* for usbdebug */
dev = getdev(*argv);
if(dev == nil)
sysfatal("getdev: %r");
--- a/sys/src/cmd/nusb/lib/dev.c
+++ b/sys/src/cmd/nusb/lib/dev.c
@@ -373,7 +373,6 @@
int ndata, n;
uchar *wp;
uchar buf[8];
- char *hd, *rs;
assert(d != nil);
if(data == nil){
@@ -390,12 +389,10 @@
PUT2(wp+4, index);
PUT2(wp+6, count);
if(usbdebug>2){
- hd = hexstr(wp, ndata+8);
- rs = reqstr(type, req);
- fprint(2, "%s: %s val %d|%d idx %d cnt %d out[%d] %s\n",
- d->dir, rs, value>>8, value&0xFF,
- index, count, ndata+8, hd);
- free(hd);
+ fprint(2, "%s: %s val %d|%d idx %d cnt %d out[%d] %.*H\n",
+ d->dir, reqstr(type, req), value>>8, value&0xFF,
+ index, count, ndata+8,
+ ndata+8, wp);
}
n = write(d->dfd, wp, 8+ndata);
if(wp != buf)
@@ -412,14 +409,9 @@
static int
cmdrep(Dev *d, void *buf, int nb)
{
- char *hd;
-
nb = read(d->dfd, buf, nb);
- if(nb > 0 && usbdebug > 2){
- hd = hexstr(buf, nb);
- fprint(2, "%s: in[%d] %s\n", d->dir, nb, hd);
- free(hd);
- }
+ if(nb > 0 && usbdebug > 2)
+ fprint(2, "%s: in[%d] %.*H\n", d->dir, nb, nb, buf);
return nb;
}
--- a/sys/src/cmd/nusb/lib/dump.c
+++ b/sys/src/cmd/nusb/lib/dump.c
@@ -60,24 +60,6 @@
}
}
-char *
-hexstr(void *a, int n)
-{
- int i;
- char *dbuff, *s, *e;
- uchar *b;
-
- b = a;
- dbuff = s = emallocz(1024, 0);
- *s = 0;
- e = s + 1024;
- for(i = 0; i < n; i++)
- s = seprint(s, e, " %.2ux", b[i]);
- if(s == e)
- fprint(2, "%s: usb/lib: hexdump: bug: small buffer\n", argv0);
- return dbuff;
-}
-
static void
fmtprintiface(Fmt *f, Iface *i)
{
@@ -109,7 +91,6 @@
int i;
Conf *c;
Iface *fc;
- char *hd;
c = d->conf[ci];
fmtprint(f, "\tconf: cval %d attrib %x %d mA\n",
@@ -122,12 +103,10 @@
if(d->ddesc[i] == nil)
break;
else if(d->ddesc[i]->conf == c){
- hd = hexstr((uchar*)&d->ddesc[i]->data,
- d->ddesc[i]->data.bLength);
- fmtprint(f, "\t\tdev desc %x[%d]: %s\n",
+ fmtprint(f, "\t\tdev desc %x[%d]: %.*H\n",
d->ddesc[i]->data.bDescriptorType,
- d->ddesc[i]->data.bLength, hd);
- free(hd);
+ d->ddesc[i]->data.bLength,
+ d->ddesc[i]->data.bLength, &d->ddesc[i]->data);
}
}
--- a/sys/src/cmd/nusb/lib/parse.c
+++ b/sys/src/cmd/nusb/lib/parse.c
@@ -8,15 +8,11 @@
{
Usbdev *d;
DDev *dd;
- char *hd;
d = xd->usb;
dd = (DDev*)b;
- if(usbdebug>1){
- hd = hexstr(b, Ddevlen);
- fprint(2, "%s: parsedev %s: %s\n", argv0, xd->dir, hd);
- free(hd);
- }
+ if(usbdebug>1)
+ fprint(2, "%s: parsedev %s: %.*H\n", argv0, xd->dir, Ddevlen, b);
if(dd->bLength < Ddevlen){
werrstr("short dev descr. (%d < %d)", dd->bLength, Ddevlen);
return -1;
@@ -198,7 +194,6 @@
int len, nd, tot;
Iface *ip;
Ep *ep;
- char *hd;
tot = 0;
ip = nil;
@@ -209,10 +204,8 @@
while(n > 2 && (len = b[0]) != 0 && len <= n){
if(usbdebug>1){
- hd = hexstr(b, len);
- fprint(2, "%s:\t\tparsedesc %s %x[%d] %s\n",
- argv0, dname(b[1]), b[1], b[0], hd);
- free(hd);
+ fprint(2, "%s:\t\tparsedesc %s %x[%d] %.*H\n",
+ argv0, dname(b[1]), b[1], b[0], len, b);
}
switch(b[1]){
case Ddev:
@@ -264,14 +257,10 @@
DConf* dc;
int l;
int nr;
- char *hd;
dc = (DConf*)b;
- if(usbdebug>1){
- hd = hexstr(b, Dconflen);
- fprint(2, "%s:\tparseconf %s\n", argv0, hd);
- free(hd);
- }
+ if(usbdebug>1)
+ fprint(2, "%s:\tparseconf %.*H\n", argv0, Dconflen, b);
if(dc->bLength < Dconflen){
werrstr("short configuration descriptor");
return -1;
--- a/sys/src/cmd/nusb/lib/usb.h
+++ b/sys/src/cmd/nusb/lib/usb.h
@@ -336,7 +336,6 @@
int devctl(Dev *dev, char *fmt, ...);
void* emallocz(ulong size, int zero);
char* estrdup(char *s);
-char* hexstr(void *a, int n);
int loaddevconf(Dev *d, int n);
int loaddevdesc(Dev *d);
char* loaddevstr(Dev *d, int sid);
--- a/sys/src/cmd/nusb/serial/serial.c
+++ b/sys/src/cmd/nusb/serial/serial.c
@@ -728,6 +728,7 @@
}ARGEND
if(argc != 1)
usage();
+ fmtinstall('H', encodefmt); /* for usbdebug */
dev = getdev(*argv);
if(dev == nil)
sysfatal("getdev: %r");
--- a/sys/src/cmd/nusb/usbd/usbd.c
+++ b/sys/src/cmd/nusb/usbd/usbd.c
@@ -559,7 +559,11 @@
} ARGEND;
quotefmtinstall();
+
+ /* for usbdebug */
fmtinstall('U', Ufmt);
+ fmtinstall('H', encodefmt);
+
initevent();
hubs = nil;
--
⑨