controls.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <thread.h>
  4. #include "usb.h"
  5. #include "usbproto.h"
  6. #include "dat.h"
  7. #include "fns.h"
  8. #include "audioclass.h"
  9. int buttonendpt = -1;
  10. Nexus *nexus[2];
  11. char *controlname[Ncontrol] =
  12. {
  13. [Speed_control] "speed",
  14. [Mute_control] "mute",
  15. [Volume_control] "volume",
  16. [Bass_control] "bass",
  17. [Mid_control] "mid",
  18. [Treble_control] "treble",
  19. [Equalizer_control] "equalizer",
  20. [Agc_control] "agc",
  21. [Delay_control] "delay",
  22. [Bassboost_control] "bassboost",
  23. [Loudness_control] "loudness",
  24. [Mixer_control] "mixer",
  25. [Channel_control] "channels",
  26. [Resolution_control] "resolution",
  27. };
  28. Nexus *
  29. findnexus(char *name)
  30. {
  31. if(strcmp(name, "playback") == 0)
  32. return nexus[Play];
  33. else if(strcmp(name, "record") == 0)
  34. return nexus[Record];
  35. else
  36. return nil;
  37. }
  38. int
  39. controlnum(char *name)
  40. {
  41. int i;
  42. for(i = 0; i < Ncontrol; i++)
  43. if(strcmp(controlname[i], name) == 0)
  44. return i;
  45. return Undef;
  46. }
  47. Audiocontrol *
  48. findcontrol(Nexus *nx, char *name)
  49. {
  50. int i;
  51. i = controlnum(name);
  52. if(i == Undef)
  53. return nil;
  54. return &nx->control[i];
  55. }
  56. static void
  57. resetcontrol(Nexus *nx, int i)
  58. {
  59. Audiocontrol *c;
  60. c = &nx->control[i];
  61. c->min = 1000000;
  62. c->max = 0;
  63. c->step = Undef;
  64. }
  65. static void
  66. controlval(Nexus *nx, int i, int val)
  67. {
  68. Audiocontrol *c;
  69. c = &nx->control[i];
  70. c->value[0] = val;
  71. if(val < c->min)
  72. c->min = val;
  73. if(val > c->max)
  74. c->max = val;
  75. c->settable = (c->min < c->max);
  76. c->readable = 1;
  77. }
  78. void
  79. calcbounds(Nexus *nx)
  80. {
  81. int i;
  82. Stream *s;
  83. Streamalt *sa;
  84. s = nx->s;
  85. if(s == nil)
  86. return;
  87. resetcontrol(nx, Channel_control);
  88. resetcontrol(nx, Resolution_control);
  89. resetcontrol(nx, Speed_control);
  90. for(sa = s->salt; sa != nil; sa = sa->next) {
  91. if(sa->nchan == 0)
  92. continue;
  93. controlval(nx, Channel_control, sa->nchan);
  94. controlval(nx, Resolution_control, sa->res);
  95. if(sa->nf == 0) {
  96. controlval(nx, Speed_control, sa->minf);
  97. controlval(nx, Speed_control, sa->maxf);
  98. }
  99. else {
  100. for(i = 0; i < sa->nf; i++)
  101. controlval(nx, Speed_control, sa->f[i]);
  102. }
  103. }
  104. }
  105. static Streamalt *
  106. findalt(Nexus *nx, int nchan, int res, int speed, int *newspeed, int *newi)
  107. {
  108. Stream *s;
  109. Streamalt *sa, *bestalt;
  110. int i, guess, err, besti, bestguess, besterr;
  111. s = nx->s;
  112. if(s == nil)
  113. return nil;
  114. bestalt = nil;
  115. besti = -1;
  116. bestguess = 0;
  117. besterr = 0x7fffffff;
  118. for(sa = s->salt; sa != nil; sa = sa->next) {
  119. if(sa->nchan != nchan || sa->res != res)
  120. continue;
  121. if(sa->nf == 0) {
  122. if(speed < sa->minf)
  123. guess = sa->minf;
  124. else if(speed > sa->maxf)
  125. guess = sa->maxf;
  126. else
  127. guess = speed;
  128. err = abs(speed - guess);
  129. if(err < besterr) {
  130. bestguess = guess;
  131. besti = -1;
  132. bestalt = sa;
  133. besterr = err;
  134. }
  135. }
  136. else {
  137. for(i = 0; i < sa->nf; i++) {
  138. err = abs(speed - sa->f[i]);
  139. if(err < besterr) {
  140. bestguess = sa->f[i];
  141. besti = i;
  142. bestalt = sa;
  143. besterr = err;
  144. }
  145. }
  146. }
  147. }
  148. *newspeed = bestguess;
  149. *newi = besti;
  150. return bestalt;
  151. }
  152. static int
  153. altctl(Nexus *nx, int ctl, int val)
  154. {
  155. Endpt *ep;
  156. Dalt *dalt;
  157. Device *d;
  158. uchar buf[3];
  159. Streamalt *sa;
  160. char cmdbuf[32];
  161. int ps, speed, newspeed, ind, nchan, res;
  162. nchan = nx->control[Channel_control].value[0];
  163. res = nx->control[Resolution_control].value[0];
  164. speed = nx->control[Speed_control].value[0];
  165. switch(ctl) {
  166. case Channel_control:
  167. nchan = val;
  168. break;
  169. case Resolution_control:
  170. res = val;
  171. break;
  172. case Speed_control:
  173. speed = val;
  174. break;
  175. }
  176. sa = findalt(nx, nchan, res, speed, &newspeed, &ind);
  177. if(sa == nil) {
  178. fprint(2, "cannot find alt: nchan %d res %d speed %d\n", nchan, res, speed);
  179. return Undef;
  180. }
  181. dalt = sa->dalt;
  182. if(dalt->npt == 0) {
  183. fprint(2, "no endpoint!\n");
  184. return Undef;
  185. }
  186. d = dalt->intf->d;
  187. if(setupreq(d, RH2D, SET_INTERFACE, dalt->alt, dalt->intf->x, nil, 0) < 0)
  188. return -1;
  189. ep = &dalt->ep[0];
  190. if(sa->attr & Asampfreq) {
  191. if(debug & Dbgcontrol)
  192. fprint(2, "Setting speed to %d Hz;", speed);
  193. if(ind >= 0) {
  194. buf[0] = ind;
  195. buf[1] = 0;
  196. buf[2] = 0;
  197. }
  198. else {
  199. buf[0] = newspeed;
  200. buf[1] = newspeed >> 8;
  201. buf[2] = newspeed >> 16;
  202. }
  203. if(setupreq(d, RH2D|Rclass|Rendpt, SET_CUR, sampling_freq_control<<8, ep->addr, buf, 3) < 0)
  204. return Undef;
  205. if(setupreq(d, RD2H|Rclass|Rendpt, GET_CUR, sampling_freq_control<<8, ep->addr, buf, 3) != 3)
  206. return Undef;
  207. if(buf[2]) {
  208. if (debug & Dbgcontrol)
  209. fprint(2, "Speed out of bounds %d (0x%x)\n",
  210. buf[0] | buf[1] << 8 | buf[2] << 16,
  211. buf[0] | buf[1] << 8 | buf[2] << 16);
  212. }
  213. else
  214. speed = buf[0] | buf[1] << 8 | buf[2] << 16;
  215. if (debug & Dbgcontrol)
  216. fprint(2, " speed now %d Hz;", speed);
  217. }
  218. else
  219. speed = newspeed;
  220. ps = ((speed * ep->pollms + 999) / 1000) * nchan * res/8;
  221. if(ps > ep->maxpkt){
  222. fprint(2, "packet size %d > maximum packet size %d", ps, ep->maxpkt);
  223. return Undef;
  224. }
  225. if(debug & Dbgcontrol)
  226. fprint(2, "Configuring %s endpoint for %d Hz\n", nx->name, speed);
  227. sprint(cmdbuf, "ep %d %d %c %d %d", ep->addr, ep->pollms, ep->dir == Ein ? 'r' : 'w',
  228. nchan*res/8, speed);
  229. if(write(d->ctl, cmdbuf, strlen(cmdbuf)) != strlen(cmdbuf)){
  230. fprint(2, "writing %s to #U/usb%d/%d/ctl: %r\n", cmdbuf, d->ctlrno, d->id);
  231. return Undef;
  232. }
  233. if (debug & Dbgcontrol) fprint(2, "sent `%s' to /dev/usb%d/%d/ctl\n", cmdbuf, d->ctlrno, d->id);
  234. if(ctl == Speed_control)
  235. return speed;
  236. return val;
  237. }
  238. static int
  239. set1(Nexus *nx, int ctl, int req, int i, int val)
  240. {
  241. Device *d;
  242. byte buf[2];
  243. int type, count, id;
  244. if(nx->s == nil)
  245. return Undef;
  246. d = nx->s->intf->d;
  247. if(ctl == Mixer_control){
  248. if (nx->mixer == nil)
  249. return Undef;
  250. id = nx->mixer->id<<8;
  251. }else{
  252. if (nx->feat == nil)
  253. return Undef;
  254. id = nx->feat->id<<8;
  255. }
  256. type = RH2D|Rclass|Rinterface;
  257. switch(ctl) {
  258. case Speed_control:
  259. case Channel_control:
  260. case Resolution_control:
  261. return Undef;
  262. case Mixer_control:
  263. ctl = 1; /* hack */
  264. case Volume_control:
  265. case Delay_control:
  266. count = 2;
  267. break;
  268. default:
  269. count = 1;
  270. break;
  271. }
  272. buf[0] = val;
  273. buf[1] = val>>8;
  274. if(setupreq(d, type, req, (ctl<<8) | i, id, buf, count) < 0)
  275. return Undef;
  276. return 0;
  277. }
  278. static int
  279. set2(Nexus *nx, int ctl, int req, int i, int val)
  280. {
  281. Device *d;
  282. byte buf[2];
  283. int type, id;
  284. if(nx->mixer == nil || nx->s == nil)
  285. return Undef;
  286. d = nx->s->intf->d;
  287. id = nx->mixer->id<<8;
  288. type = RH2D|Rclass|Rinterface;
  289. if (ctl != Mixer_control)
  290. return Undef;
  291. buf[0] = val;
  292. buf[1] = val>>8;
  293. if(setupreq(d, type, req, (1<<8) | i, id, buf, 2) < 0)
  294. return Undef;
  295. return 0;
  296. }
  297. int
  298. setcontrol(Nexus *nx, char *name, long *value)
  299. {
  300. int i, ctl, m, req;
  301. Audiocontrol *c;
  302. ctl = controlnum(name);
  303. if (ctl == Undef){
  304. if (debug & Dbgcontrol) fprint(2, "setcontrol: control not found\n");
  305. return -1;
  306. }
  307. c = &nx->control[ctl];
  308. if(c->settable == 0) {
  309. if(debug & Dbgcontrol)
  310. fprint(2, "setcontrol: control %s.%d not settable\n", nx->name, ctl);
  311. if(c->chans) {
  312. for(i = 0; i < 8; i++)
  313. if((c->chans & 1 << i) && c->value[i] != value[i])
  314. return -1;
  315. return 0;
  316. }
  317. if(c->value[0] != value[0])
  318. return -1;
  319. return 0;
  320. }
  321. if(c->chans) {
  322. value[0] = 0; // set to average
  323. m = 0;
  324. for(i = 1; i < 8; i++)
  325. if(c->chans & (1 << i)) {
  326. if(c->min != Undef && value[i] < c->min)
  327. value[i] = c->min;
  328. if(c->max != Undef && value[i] > c->max)
  329. value[i] = c->max;
  330. value[0] += value[i];
  331. m++;
  332. }
  333. else
  334. value[i] = Undef;
  335. if(m)
  336. value[0] /= m;
  337. }
  338. else {
  339. if(c->min != Undef && value[0] < c->min)
  340. value[0] = c->min;
  341. if(c->max != Undef && value[0] > c->max)
  342. value[0] = c->max;
  343. }
  344. req = SET_CUR;
  345. switch(ctl) {
  346. default:
  347. if(debug & Dbgcontrol)
  348. fprint(2, "setcontrol: can't happen\n");
  349. return -1;
  350. case Speed_control:
  351. case Resolution_control:
  352. case Channel_control:
  353. if((value[0] = altctl(nx, ctl, value[0])) < 0)
  354. return -1;
  355. c->value[0] = value[0];
  356. return 0;
  357. case Equalizer_control:
  358. /* not implemented */
  359. return -1;
  360. case Volume_control:
  361. case Mixer_control:
  362. case Delay_control:
  363. case Mute_control:
  364. case Bass_control:
  365. case Mid_control:
  366. case Treble_control:
  367. case Agc_control:
  368. case Bassboost_control:
  369. case Loudness_control:
  370. break;
  371. }
  372. if(c->chans) {
  373. for(i = 1; i < 8; i++)
  374. if(c->chans & 1 << i) {
  375. if(set1(nx, ctl, req, i, value[i]) < 0) {
  376. if(debug & Dbgcontrol)
  377. fprint(2, "setcontrol: setupcmd %s failed\n",
  378. controlname[ctl]);
  379. return -1;
  380. }
  381. c->value[i] = value[i];
  382. }
  383. }
  384. else {
  385. if(set1(nx, ctl, req, 0, value[0]) < 0) {
  386. if(debug & Dbgcontrol)
  387. fprint(2, "setcontrol: setupcmd %s failed\n",
  388. controlname[ctl]);
  389. return -1;
  390. }
  391. }
  392. c->value[0] = value[0];
  393. return 0;
  394. }
  395. static int
  396. get1(Nexus *nx, int ctl, int req, int i)
  397. {
  398. Device *d;
  399. byte buf[2];
  400. int type, count, id;
  401. if(nx->s == nil)
  402. return Undef;
  403. d = nx->s->intf->d;
  404. if(ctl == Mixer_control){
  405. if (nx->mixer == nil)
  406. return Undef;
  407. id = nx->mixer->id<<8;
  408. }else{
  409. if (nx->feat == nil)
  410. return Undef;
  411. id = nx->feat->id<<8;
  412. }
  413. type = RD2H|Rclass|Rinterface;
  414. switch(ctl) {
  415. default:
  416. count = 1;
  417. break;
  418. case Mixer_control:
  419. ctl = 1; /* see set1() */
  420. /* fall through */
  421. case Volume_control:
  422. case Delay_control:
  423. count = 2;
  424. break;
  425. case Speed_control:
  426. case Channel_control:
  427. case Resolution_control:
  428. return Undef;
  429. }
  430. if(setupreq(d, type, req, (ctl<<8) | i, id, buf, count) != count){
  431. return Undef;
  432. }
  433. switch(count) {
  434. case 1:
  435. return buf[0];
  436. case 2:
  437. return (short) (buf[0] | (buf[1]<<8));
  438. }
  439. }
  440. static int
  441. getspecialcontrol(Nexus *nx, int ctl, int req, long *value)
  442. {
  443. int m, i, val;
  444. Audiocontrol *c;
  445. c = &nx->control[ctl];
  446. switch(ctl) {
  447. default:
  448. return Undef;
  449. case Speed_control:
  450. case Channel_control:
  451. case Resolution_control:
  452. if(req == GET_MIN)
  453. value[0] = c->min;
  454. else if(req == GET_MAX)
  455. value[0] = c->max;
  456. else if(req == GET_RES)
  457. value[0] = c->step;
  458. else if(req == GET_CUR)
  459. value[0] = c->value[0];
  460. else
  461. value[0] = Undef;
  462. return 0;
  463. case Mixer_control:
  464. case Volume_control:
  465. case Delay_control:
  466. case Mute_control:
  467. case Bass_control:
  468. case Mid_control:
  469. case Treble_control:
  470. case Equalizer_control:
  471. case Agc_control:
  472. case Bassboost_control:
  473. case Loudness_control:
  474. break;
  475. }
  476. if(c->chans) {
  477. m = 0;
  478. value[0] = 0; // set to average
  479. for (i = 1; i < 8; i++){
  480. value[i] = Undef;
  481. if(c->chans & (1 << i)) {
  482. val = get1(nx, ctl, req, i);
  483. if(val == Undef)
  484. return Undef;
  485. if(req == GET_CUR) {
  486. value[i] = val;
  487. value[0] += val;
  488. m++;
  489. }
  490. else
  491. value[0] = val;
  492. }
  493. }
  494. if(m != 0)
  495. value[0] /= m;
  496. return 0;
  497. }
  498. value[0] = Undef;
  499. val = get1(nx, ctl, req, 0);
  500. if(val == Undef)
  501. return Undef;
  502. value[0] = val;
  503. return 0;
  504. }
  505. int
  506. getcontrol(Nexus *nx, char *name, long *value)
  507. {
  508. int i;
  509. Audiocontrol *c;
  510. i = controlnum(name);
  511. if(i == Undef)
  512. return -1;
  513. c = &nx->control[i];
  514. if(!c->readable)
  515. return -1;
  516. if(getspecialcontrol(nx, i, GET_CUR, value) < 0)
  517. return -1;
  518. memmove(c->value, value, sizeof c->value);
  519. return 0;
  520. }
  521. void
  522. getcontrols(Nexus *nx)
  523. {
  524. int ctl, i;
  525. Audiocontrol *c;
  526. long v[8];
  527. for(ctl = 0; ctl < Ncontrol; ctl++) {
  528. c = &nx->control[ctl];
  529. if(c->readable) {
  530. if(verbose)
  531. fprint(2, "%s %s control", nx->name, controlname[ctl]);
  532. c->min = (getspecialcontrol(nx, ctl, GET_MIN, v) < 0) ? Undef : v[0];
  533. if(verbose && c->min != Undef)
  534. fprint(2, ", min %ld", c->min);
  535. c->max = (getspecialcontrol(nx, ctl, GET_MAX, v) < 0) ? Undef : v[0];
  536. if(verbose && c->max != Undef)
  537. fprint(2, ", max %ld", c->max);
  538. c->step = (getspecialcontrol(nx, ctl, GET_RES, v) < 0) ? Undef : v[0];
  539. if(verbose && c->step != Undef)
  540. fprint(2, ", step %ld", c->step);
  541. if(getspecialcontrol(nx, ctl, GET_CUR, c->value) == 0) {
  542. if(verbose) {
  543. if(c->chans) {
  544. fprint(2, ", values");
  545. for(i = 1; i < 8; i++)
  546. if(c->chans & (1 << i))
  547. fprint(2, "[%d] %ld ", i, c->value[i]);
  548. }
  549. else
  550. fprint(2, ", value %ld", c->value[0]);
  551. }
  552. }
  553. if(verbose)
  554. fprint(2, "\n");
  555. }
  556. else {
  557. c->min = Undef;
  558. c->max = Undef;
  559. c->step = Undef;
  560. c->value[0] = Undef;
  561. if (debug & Dbgcontrol)
  562. fprint(2, "%s %s control not settable\n", nx->name, controlname[ctl]);
  563. }
  564. }
  565. }
  566. int
  567. ctlparse(char *s, Audiocontrol *c, long *v)
  568. {
  569. int i, j, nf, m;
  570. char *vals[9];
  571. char *p;
  572. long val;
  573. nf = tokenize(s, vals, nelem(vals));
  574. if (nf <= 0)
  575. return -1;
  576. if (c->chans){
  577. j = 0;
  578. m = 0;
  579. SET(val);
  580. v[0] = 0; // will compute average of v[i]
  581. for (i = 1; i < 8; i++)
  582. if (c->chans & 1 << i) {
  583. if (j < nf){
  584. val = strtol(vals[j], &p, 0);
  585. if (val == 0 && *p != '\0' && *p != '%')
  586. return -1;
  587. if (*p == '%' && c->min != Undef)
  588. val = (val*c->max + (100-val)*c->min)/100;
  589. j++;
  590. }
  591. v[i] = val;
  592. v[0] += val;
  593. m++;
  594. } else
  595. v[i] = Undef;
  596. if (m) v[0] /= m;
  597. } else {
  598. val = strtol(vals[0], &p, 0);
  599. if (*p == '%' && c->min != Undef)
  600. val = (val*c->max + (100-val)*c->min)/100;
  601. v[0] = val;
  602. }
  603. return 0;
  604. }
  605. int
  606. Aconv(Fmt *fp)
  607. {
  608. char str[256];
  609. Audiocontrol *c;
  610. int fst, i;
  611. char *p;
  612. c = va_arg(fp->args, Audiocontrol*);
  613. p = str;
  614. if (c->chans) {
  615. fst = 1;
  616. for (i = 1; i < 8; i++)
  617. if (c->chans & 1 << i){
  618. p = seprint(p, str+sizeof str, "%s%ld", fst?"'":" ", c->value[i]);
  619. fst = 0;
  620. }
  621. seprint(p, str+sizeof str, "'");
  622. } else
  623. seprint(p, str+sizeof str, "%ld", c->value[0]);
  624. return fmtstrcpy(fp, str);
  625. }