nusb: export a volume file and allow setting speed
This commit is contained in:
parent
6f3039351a
commit
3a076c7a95
1 changed files with 129 additions and 49 deletions
|
@ -1,17 +1,32 @@
|
||||||
#include <u.h>
|
#include <u.h>
|
||||||
#include <libc.h>
|
#include <libc.h>
|
||||||
|
#include <fcall.h>
|
||||||
#include <thread.h>
|
#include <thread.h>
|
||||||
|
#include <9p.h>
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
|
|
||||||
|
typedef struct Audio Audio;
|
||||||
|
struct Audio
|
||||||
|
{
|
||||||
|
Audio *next;
|
||||||
|
|
||||||
|
int minfreq;
|
||||||
|
int maxfreq;
|
||||||
|
};
|
||||||
|
|
||||||
int audiofreq = 44100;
|
int audiofreq = 44100;
|
||||||
int audiochan = 2;
|
int audiochan = 2;
|
||||||
int audiores = 16;
|
int audiores = 16;
|
||||||
|
|
||||||
char validaltc[] = "";
|
char user[] = "audio";
|
||||||
|
|
||||||
|
Dev *audiodev = nil;
|
||||||
|
Ep *audioep = nil;
|
||||||
|
|
||||||
void
|
void
|
||||||
parsedescr(Desc *dd)
|
parsedescr(Desc *dd)
|
||||||
{
|
{
|
||||||
|
Audio *a, **ap;
|
||||||
uchar *b;
|
uchar *b;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -24,27 +39,110 @@ parsedescr(Desc *dd)
|
||||||
return;
|
return;
|
||||||
if(b[6] != audiores)
|
if(b[6] != audiores)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
ap = (Audio**)&dd->altc->aux;
|
||||||
if(b[7] == 0){
|
if(b[7] == 0){
|
||||||
int minfreq, maxfreq;
|
a = mallocz(sizeof(*a), 1);
|
||||||
|
a->minfreq = b[8] | b[9]<<8 | b[10]<<16;
|
||||||
minfreq = b[8] | b[9]<<8 | b[10]<<16;
|
a->maxfreq = b[11] | b[12]<<8 | b[13]<<16;
|
||||||
maxfreq = b[11] | b[12]<<8 | b[13]<<16;
|
a->next = *ap;
|
||||||
if(minfreq > audiofreq || maxfreq < audiofreq)
|
*ap = a;
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
int freq;
|
|
||||||
|
|
||||||
for(i=0; i<b[7]; i++){
|
for(i=0; i<b[7]; i++){
|
||||||
freq = b[8+3*i] | b[9+3*i]<<8 | b[10+3*i]<<16;
|
a = mallocz(sizeof(*a), 1);
|
||||||
if(freq == audiofreq)
|
a->minfreq = b[8+3*i] | b[9+3*i]<<8 | b[10+3*i]<<16;
|
||||||
break;
|
a->maxfreq = a->minfreq;
|
||||||
|
a->next = *ap;
|
||||||
|
*ap = a;
|
||||||
}
|
}
|
||||||
if(i == b[7])
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
dd->altc->aux = validaltc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dev*
|
||||||
|
setupep(Dev *d, Ep *e, int speed)
|
||||||
|
{
|
||||||
|
uchar b[4];
|
||||||
|
Audio *x;
|
||||||
|
Altc *a;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i = 0; i < nelem(e->iface->altc); i++)
|
||||||
|
if(a = e->iface->altc[i])
|
||||||
|
for(x = a->aux; x; x = x->next)
|
||||||
|
if(speed >= x->minfreq && speed <= x->maxfreq)
|
||||||
|
goto Foundaltc;
|
||||||
|
|
||||||
|
werrstr("no altc found");
|
||||||
|
return nil;
|
||||||
|
|
||||||
|
Foundaltc:
|
||||||
|
if(usbcmd(d, Rh2d|Rstd|Riface, Rsetiface, i, e->iface->id, nil, 0) < 0){
|
||||||
|
werrstr("set altc: %r");
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
b[0] = speed;
|
||||||
|
b[1] = speed >> 8;
|
||||||
|
b[2] = speed >> 16;
|
||||||
|
if(usbcmd(d, Rh2d|Rclass|Rep, Rsetcur, 0x100, e->addr, b, 3) < 0)
|
||||||
|
fprint(2, "warning: set freq: %r");
|
||||||
|
|
||||||
|
if((d = openep(d, e->id)) == nil){
|
||||||
|
werrstr("openep: %r");
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
devctl(d, "pollival %d", 1);
|
||||||
|
devctl(d, "samplesz %d", audiochan*audiores/8);
|
||||||
|
devctl(d, "hz %d", speed);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
fsread(Req *r)
|
||||||
|
{
|
||||||
|
char *msg;
|
||||||
|
|
||||||
|
msg = smprint("master 100 100\nspeed %d\n", audiofreq);
|
||||||
|
readstr(r, msg);
|
||||||
|
respond(r, nil);
|
||||||
|
free(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
fswrite(Req *r)
|
||||||
|
{
|
||||||
|
char msg[256], *f[4];
|
||||||
|
int nf;
|
||||||
|
|
||||||
|
snprint(msg, sizeof(msg), "%.*s", r->ifcall.count, r->ifcall.data);
|
||||||
|
nf = tokenize(msg, f, nelem(f));
|
||||||
|
if(nf < 2){
|
||||||
|
respond(r, "invalid ctl message");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(strcmp(f[0], "speed") == 0){
|
||||||
|
Dev *d;
|
||||||
|
int speed;
|
||||||
|
|
||||||
|
speed = atoi(f[1]);
|
||||||
|
if((d = setupep(audiodev, audioep, speed)) == nil){
|
||||||
|
responderror(r);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
devctl(d, "name audio");
|
||||||
|
closedev(d);
|
||||||
|
|
||||||
|
audiofreq = speed;
|
||||||
|
}
|
||||||
|
r->ofcall.count = r->ifcall.count;
|
||||||
|
respond(r, nil);
|
||||||
|
}
|
||||||
|
|
||||||
|
Srv fs = {
|
||||||
|
.read = fsread,
|
||||||
|
.write = fswrite,
|
||||||
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
|
@ -55,10 +153,8 @@ usage(void)
|
||||||
void
|
void
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Dev *d, *ed;
|
char buf[32];
|
||||||
Usbdev *ud;
|
Dev *d;
|
||||||
uchar b[4];
|
|
||||||
Altc *a;
|
|
||||||
Ep *e;
|
Ep *e;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -72,15 +168,13 @@ main(int argc, char *argv[])
|
||||||
sysfatal("getdev: %r");
|
sysfatal("getdev: %r");
|
||||||
if(configdev(d) < 0)
|
if(configdev(d) < 0)
|
||||||
sysfatal("configdev: %r");
|
sysfatal("configdev: %r");
|
||||||
|
audiodev = d;
|
||||||
ud = d->usb;
|
|
||||||
|
|
||||||
/* parse descriptors, mark valid altc */
|
/* parse descriptors, mark valid altc */
|
||||||
for(i = 0; i < nelem(ud->ddesc); i++)
|
for(i = 0; i < nelem(d->usb->ddesc); i++)
|
||||||
parsedescr(ud->ddesc[i]);
|
parsedescr(d->usb->ddesc[i]);
|
||||||
|
for(i = 0; i < nelem(d->usb->ep); i++){
|
||||||
for(i = 0; i < nelem(ud->ep); i++){
|
e = d->usb->ep[i];
|
||||||
e = ud->ep[i];
|
|
||||||
if(e && e->iface && e->iface->csp == CSP(Claudio, 2, 0) && e->dir == Eout)
|
if(e && e->iface && e->iface->csp == CSP(Claudio, 2, 0) && e->dir == Eout)
|
||||||
goto Foundendp;
|
goto Foundendp;
|
||||||
}
|
}
|
||||||
|
@ -88,31 +182,17 @@ main(int argc, char *argv[])
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Foundendp:
|
Foundendp:
|
||||||
for(i = 0; i < nelem(e->iface->altc); i++){
|
audioep = e;
|
||||||
a = e->iface->altc[i];
|
if((d = setupep(audiodev, audioep, audiofreq)) == nil)
|
||||||
if(a && a->aux == validaltc)
|
sysfatal("setupep: %r");
|
||||||
goto Foundaltc;
|
devctl(d, "name audio");
|
||||||
}
|
closedev(d);
|
||||||
sysfatal("no altc found");
|
|
||||||
|
|
||||||
Foundaltc:
|
fs.tree = alloctree(user, "usb", DMDIR|0555, nil);
|
||||||
if(usbcmd(d, Rh2d|Rstd|Riface, Rsetiface, i, e->iface->id, nil, 0) < 0)
|
createfile(fs.tree->root, "volume", user, 0666, nil);
|
||||||
sysfatal("usbcmd: set altc: %r");
|
|
||||||
|
|
||||||
b[0] = audiofreq;
|
snprint(buf, sizeof buf, "%d.audio", audiodev->id);
|
||||||
b[1] = audiofreq >> 8;
|
postsharesrv(&fs, nil, "usb", buf);
|
||||||
b[2] = audiofreq >> 16;
|
|
||||||
if(usbcmd(d, Rh2d|Rclass|Rep, Rsetcur, 0x100, e->addr, b, 3) < 0)
|
|
||||||
fprint(2, "warning: set freq: %r");
|
|
||||||
|
|
||||||
if((ed = openep(d, e->id)) == nil)
|
|
||||||
sysfatal("openep: %r");
|
|
||||||
devctl(ed, "pollival %d", 1);
|
|
||||||
devctl(ed, "samplesz %d", audiochan*audiores/8);
|
|
||||||
devctl(ed, "hz %d", audiofreq);
|
|
||||||
|
|
||||||
/* rename endpoint to #u/audio */
|
|
||||||
devctl(ed, "name audio");
|
|
||||||
|
|
||||||
exits(0);
|
exits(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue