diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-06-28 18:09:43 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-06-28 18:09:43 +0200 |
commit | 4275c49e72544d5b92512e41ddecbd6af5bee6c3 (patch) | |
tree | 22d66cc1e3286e69f50cc676e3c8e7fbd4b0636a /sys/src/cmd/nusb/lib/dev.c | |
parent | d8c75e45de9483881f782be0b5e7113e625db571 (diff) |
nusb: implement aijus stable uniqueue device names
instead of naming devices by ther dynamically assigned device address,
we hash device uniqueue fields from the device descriptor and produce
a 5 digit hex string that will identify the device across machines.
when there is a collision (less than 1% chance with 100 devices),
usbd will append the device address to the name to make it uniqueue
for this machine.
the hname is passed to drivers in the devid argument, which now has
the form addr:hname, where the colon and hname can be omited (for backwards
compatibility).
when the new behaviour isnt desired, nousbhname= environment variable
can be defined giving the old behaviour.
Diffstat (limited to 'sys/src/cmd/nusb/lib/dev.c')
-rw-r--r-- | sys/src/cmd/nusb/lib/dev.c | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/sys/src/cmd/nusb/lib/dev.c b/sys/src/cmd/nusb/lib/dev.c index 60d327e87..2eed9490e 100644 --- a/sys/src/cmd/nusb/lib/dev.c +++ b/sys/src/cmd/nusb/lib/dev.c @@ -121,6 +121,7 @@ opendev(char *fn) free(d); return nil; } + d->hname = nil; dprint(2, "%s: opendev %#p %s\n", argv0, d, fn); return d; } @@ -323,6 +324,8 @@ closedev(Dev *d) d->cfd = d->dfd = -1; free(d->dir); d->dir = nil; + free(d->hname); + d->hname = nil; ud = d->usb; d->usb = nil; if(ud != nil){ @@ -509,12 +512,26 @@ devctl(Dev *dev, char *fmt, ...) } Dev * -getdev(int id) +getdev(char *devid) { + char buf[40], *p; Dev *d; - char buf[40]; - - snprint(buf, sizeof buf, "/dev/usb/ep%d.0", id); + + if(devid[0] == '/' || devid[0] == '#'){ + snprint(buf, sizeof buf, "%s", devid); + p = strrchr(buf, '/'); + if(p != nil){ + p = strrchr(buf, ':'); + if(p != nil) + *p = 0; + } + } else { + p = nil; + snprint(buf, sizeof buf, "/dev/usb/ep%ld.0", strtol(devid, &p, 10)); + if(*p != ':') + p = nil; + } + d = opendev(buf); if(d == nil) return nil; @@ -522,5 +539,12 @@ getdev(int id) closedev(d); return nil; } + + if(p == nil){ + snprint(buf, sizeof buf, ":%d", d->id); + p = buf; + } + d->hname = strdup(p+1); + return d; } |