usbaudioctl.c 18 KB

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