usbaudioctl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. n = endpt[rec];
  193. if (rec)
  194. n |= 0x80;
  195. if(setupcmd(ad->ep[0], RH2D|Rclass|Rendpt, SET_CUR, sampling_freq_control<<8, n, buf, 3) < 0){
  196. fprint(2, "Error in setupcmd\n");
  197. return Undef;
  198. }
  199. if (setupreq(ad->ep[0], RD2H|Rclass|Rendpt, GET_CUR, sampling_freq_control<<8, n, 3) < 0){
  200. fprint(2, "Error in setupreq\n");
  201. return Undef;
  202. }
  203. n = setupreply(ad->ep[0], buf, 3);
  204. if (n != 3)
  205. fprint(2, "Error in setupreply: %d\n", n);
  206. else{
  207. n = buf[0] | buf[1] << 8 | buf[2] << 16;
  208. if (buf[2] || n == 0){
  209. if (debug & Dbgcontrol)
  210. fprint(2, "Speed out of bounds %d (0x%x)\n", n, n);
  211. }else if (n != speed && ad->vid == 0x077d && (ad->did == 0x0223 || ad->did == 0x07af)){
  212. /* Griffin iMic responds incorrectly to sample rate inquiry */
  213. if (debug & Dbgcontrol)
  214. fprint(2, " reported as %d (iMic bug?);", n);
  215. }else
  216. speed = n;
  217. }
  218. if (debug & Dbgcontrol)
  219. fprint(2, " speed now %d Hz;", speed);
  220. }
  221. ps = ((speed * da->interval + 999) / 1000)
  222. * controls[rec][Channel_control].value[0]
  223. * controls[rec][Resolution_control].value[0]/8;
  224. if(ps > ep->maxpkt){
  225. fprint(2, "packet size %d > maximum packet size %d\n",
  226. ps, ep->maxpkt);
  227. return Undef;
  228. }
  229. if (debug & Dbgcontrol)
  230. fprint(2, "Configuring %s endpoint for %d Hz\n",
  231. rec?"record":"playback", speed);
  232. sprint(cmdbuf, "ep %d %d %c %ld %d", endpt[rec], da->interval, rec?'r':'w',
  233. controls[rec][Channel_control].value[0]*controls[rec][Resolution_control].value[0]/8,
  234. speed);
  235. if (write(ad->ctl, cmdbuf, strlen(cmdbuf)) != strlen(cmdbuf)){
  236. fprint(2, "writing %s to #U/usb/%d/ctl: %r\n", cmdbuf, id);
  237. return Undef;
  238. }
  239. if (debug & Dbgcontrol) fprint(2, "sent `%s' to /dev/usb/%d/ctl\n", cmdbuf, id);
  240. return speed;
  241. }
  242. long
  243. getspeed(int rec, int which)
  244. {
  245. int i, n;
  246. Audioalt *a;
  247. Dalt *da;
  248. Endpt *ep;
  249. uchar buf[3];
  250. if (curalt[rec] < 0){
  251. fprint(2, "Must set channels and resolution before getspeed\n");
  252. return Undef;
  253. }
  254. if (endpt[rec] < 0)
  255. sysfatal("endpt[%s] not set\n", rec?"Record":"Playback");
  256. if(debug & Dbgcontrol)
  257. fprint(2, "getspeed: endpt[%d] == %d\n", rec, endpt[rec]);
  258. ep = ad->ep[endpt[rec]];
  259. if (ep->iface == nil)
  260. sysfatal("no interface");
  261. if (curalt[rec] < 0)
  262. sysfatal("curalt[%s] not set\n", rec?"Record":"Playback");
  263. da = ep->iface->dalt[curalt[rec]];
  264. a = da->devspec;
  265. if (a->caps & onefreq){
  266. if(debug & Dbgcontrol)
  267. fprint(2, "getspeed: onefreq\n");
  268. if (which == GET_RES)
  269. return Undef;
  270. return a->freqs[0]; /* speed not settable */
  271. }
  272. if (a->caps & has_setspeed){
  273. if(debug & Dbgcontrol)
  274. fprint(2, "getspeed: has_setspeed, ask\n");
  275. n = endpt[rec];
  276. if (rec)
  277. n |= 0x80;
  278. if (setupreq(ad->ep[0], RD2H|Rclass|Rendpt, which, sampling_freq_control<<8, n, 3) < 0)
  279. return Undef;
  280. n = setupreply(ad->ep[0], buf, 3);
  281. if(n == 3){
  282. if(buf[2]){
  283. if (debug & Dbgcontrol)
  284. fprint(2, "Speed out of bounds\n");
  285. if ((a->caps & has_discfreq) && (buf[0] | buf[1] << 8) < 8)
  286. return a->freqs[buf[0] | buf[1] << 8];
  287. }
  288. return buf[0] | buf[1] << 8 | buf[2] << 16;
  289. }
  290. if(debug & Dbgcontrol)
  291. fprint(2, "getspeed: n = %d\n", n);
  292. }
  293. if (a->caps & has_contfreq){
  294. if(debug & Dbgcontrol)
  295. fprint(2, "getspeed: has_contfreq\n");
  296. if (which == GET_CUR)
  297. return controls[rec][Speed_control].value[0];
  298. if (which == GET_MIN)
  299. return a->minfreq;
  300. if (which == GET_MAX)
  301. return a->maxfreq;
  302. if (which == GET_RES)
  303. return 1;
  304. }
  305. if (a->caps & has_discfreq){
  306. if(debug & Dbgcontrol)
  307. fprint(2, "getspeed: has_discfreq\n");
  308. if (which == GET_CUR)
  309. return controls[rec][Speed_control].value[0];
  310. if (which == GET_MIN)
  311. return a->freqs[0];
  312. for (i = 0; i < 8 && a->freqs[i] > 0; i++)
  313. ;
  314. if (which == GET_MAX)
  315. return a->freqs[i-1];
  316. if (which == GET_RES)
  317. return Undef;
  318. }
  319. if (debug & Dbgcontrol)
  320. fprint(2, "can't happen\n?");
  321. return Undef;
  322. }
  323. int
  324. setcontrol(int rec, char *name, long *value)
  325. {
  326. int i, ctl, m;
  327. byte buf[3];
  328. int type, req, control, index, count;
  329. Audiocontrol *c;
  330. c = nil;
  331. for (ctl = 0; ctl < Ncontrol; ctl++){
  332. c = &controls[rec][ctl];
  333. if (strcmp(name, c->name) == 0)
  334. break;
  335. }
  336. if (ctl == Ncontrol){
  337. if (debug & Dbgcontrol) fprint(2, "setcontrol: control not found\n");
  338. return -1;
  339. }
  340. if (c->settable == 0) {
  341. if (debug & Dbgcontrol) fprint(2, "setcontrol: control %d.%d not settable\n", rec, ctl);
  342. if (c->chans){
  343. for (i = 0; i < 8; i++)
  344. if ((c->chans & 1 << i) && c->value[i] != value[i])
  345. return -1;
  346. return 0;
  347. }
  348. if (c->value[0] != value[0])
  349. return -1;
  350. return 0;
  351. }
  352. if (c->chans){
  353. value[0] = 0; // set to average
  354. m = 0;
  355. for (i = 1; i < 8; i++)
  356. if (c->chans & 1 << i){
  357. if (c->min != Undef && value[i] < c->min)
  358. value[i] = c->min;
  359. if (c->max != Undef && value[i] > c->max)
  360. value[i] = c->max;
  361. value[0] += value[i];
  362. m++;
  363. } else
  364. value[i] = Undef;
  365. if (m) value[0] /= m;
  366. }else{
  367. if (c->min != Undef && value[0] < c->min)
  368. value[0] = c->min;
  369. if (c->max != Undef && value[0] > c->max)
  370. value[0] = c->max;
  371. }
  372. req = SET_CUR;
  373. count = 1;
  374. switch(ctl){
  375. default:
  376. if (debug & Dbgcontrol) fprint(2, "setcontrol: can't happen\n");
  377. return -1;
  378. case Speed_control:
  379. if ((value[0] = setspeed(rec, value[0])) < 0)
  380. return -1;
  381. c->value[0] = value[0];
  382. return 0;
  383. case Equalizer_control:
  384. /* not implemented */
  385. return -1;
  386. case Resolution_control:
  387. control = findalt(rec, controls[rec][Channel_control].value[0], value[0], defaultspeed[rec]);
  388. if(control < 0 || setaudioalt(rec, c, control) < 0){
  389. if (debug & Dbgcontrol) fprint(2, "setcontrol: can't find setting for %s\n",
  390. c->name);
  391. return -1;
  392. }
  393. c->value[0] = value[0];
  394. controls[rec][Speed_control].value[0] = defaultspeed[rec];
  395. return 0;
  396. case Volume_control:
  397. case Delay_control:
  398. count = 2;
  399. /* fall through */
  400. case Mute_control:
  401. case Bass_control:
  402. case Mid_control:
  403. case Treble_control:
  404. case Agc_control:
  405. case Bassboost_control:
  406. case Loudness_control:
  407. type = RH2D|Rclass|Rinterface;
  408. control = ctl<<8;
  409. index = featureid[rec]<<8;
  410. break;
  411. case Selector_control:
  412. type = RH2D|Rclass|Rinterface;
  413. control = 0;
  414. index = selectorid[rec]<<8;
  415. break;
  416. case Channel_control:
  417. control = findalt(rec, value[0], controls[rec][Resolution_control].value[0], defaultspeed[rec]);
  418. if(control < 0 || setaudioalt(rec, c, control) < 0){
  419. if (debug & Dbgcontrol) fprint(2, "setcontrol: can't find setting for %s\n",
  420. c->name);
  421. return -1;
  422. }
  423. c->value[0] = value[0];
  424. controls[rec][Speed_control].value[0] = defaultspeed[rec];
  425. return 0;
  426. }
  427. if(c->chans){
  428. for (i = 1; i < 8; i++)
  429. if (c->chans & 1 << i){
  430. switch(count){
  431. case 2:
  432. buf[1] = value[i] >> 8;
  433. case 1:
  434. buf[0] = value[i];
  435. }
  436. if (setupcmd(ad->ep[0], type, req, control | i, index, buf, count) < 0){
  437. if (debug & Dbgcontrol) fprint(2, "setcontrol: setupcmd %s failed\n",
  438. controls[rec][ctl].name);
  439. return -1;
  440. }
  441. c->value[i] = value[i];
  442. }
  443. }else{
  444. switch(count){
  445. case 2:
  446. buf[1] = value[0] >> 8;
  447. case 1:
  448. buf[0] = value[0];
  449. }
  450. if (setupcmd(ad->ep[0], type, req, control, index, buf, count) < 0){
  451. if (debug & Dbgcontrol) fprint(2, "setcontrol: setupcmd %s failed\n",
  452. c->name);
  453. return -1;
  454. }
  455. }
  456. c->value[0] = value[0];
  457. return 0;
  458. }
  459. int
  460. getspecialcontrol(int rec, int ctl, int req, long *value)
  461. {
  462. byte buf[3];
  463. int m, n, i;
  464. int type, control, index, count, signedbyte;
  465. short svalue;
  466. count = 1;
  467. signedbyte = 0;
  468. switch(ctl){
  469. default:
  470. return Undef;
  471. case Speed_control:
  472. value[0] = getspeed(rec, req);
  473. return 0;
  474. case Channel_control:
  475. case Resolution_control:
  476. if (req == GET_MIN)
  477. value[0] = controls[rec][ctl].min;
  478. if (req == GET_MAX)
  479. value[0] = controls[rec][ctl].max;
  480. if (req == GET_RES)
  481. value[0] = controls[rec][ctl].step;
  482. if (req == GET_CUR)
  483. value[0] = controls[rec][ctl].value[0];
  484. return 0;
  485. case Volume_control:
  486. case Delay_control:
  487. count = 2;
  488. /* fall through */
  489. case Bass_control:
  490. case Mid_control:
  491. case Treble_control:
  492. case Equalizer_control:
  493. signedbyte = 1;
  494. type = RD2H|Rclass|Rinterface;
  495. control = ctl<<8;
  496. index = featureid[rec]<<8;
  497. break;
  498. case Selector_control:
  499. type = RD2H|Rclass|Rinterface;
  500. control = 0;
  501. index = selectorid[rec]<<8;
  502. break;
  503. case Mute_control:
  504. case Agc_control:
  505. case Bassboost_control:
  506. case Loudness_control:
  507. if (req != GET_CUR)
  508. return Undef;
  509. type = RD2H|Rclass|Rinterface;
  510. control = ctl<<8;
  511. index = featureid[rec]<<8;
  512. break;
  513. }
  514. if (controls[rec][ctl].chans){
  515. m = 0;
  516. value[0] = 0; // set to average
  517. for (i = 1; i < 8; i++){
  518. value[i] = Undef;
  519. if (controls[rec][ctl].chans & 1 << i){
  520. if (setupreq(ad->ep[0], type, req, control | i, index, count) < 0)
  521. return Undef;
  522. n = setupreply(ad->ep[0], buf, count);
  523. if (n != count)
  524. return -1;
  525. switch (count) {
  526. case 2:
  527. svalue = buf[1] << 8 | buf[0];
  528. if (req == GET_CUR){
  529. value[i] = svalue;
  530. value[0] += svalue;
  531. m++;
  532. }else
  533. value[0] = svalue;
  534. break;
  535. case 1:
  536. svalue = buf[0];
  537. if (signedbyte && (svalue&0x80))
  538. svalue |= 0xFF00;
  539. if (req == GET_CUR){
  540. value[i] = svalue;
  541. value[0] += svalue;
  542. m++;
  543. }else
  544. value[0] = svalue;
  545. }
  546. }
  547. }
  548. if (m) value[0] /= m;
  549. return 0;
  550. }
  551. value[0] = Undef;
  552. if (setupreq(ad->ep[0], type, req, control, index, count) < 0)
  553. return -1;
  554. n = setupreply(ad->ep[0], buf, count);
  555. if (n != count)
  556. return -1;
  557. switch (count) {
  558. case 2:
  559. svalue = buf[1] << 8 | buf[0];
  560. value[0] = svalue;
  561. break;
  562. case 1:
  563. svalue = buf[0];
  564. if (signedbyte && (svalue&0x80))
  565. svalue |= 0xFF00;
  566. value[0] = svalue;
  567. }
  568. return 0;
  569. }
  570. int
  571. getcontrol(int rec, char *name, long *value)
  572. {
  573. int i;
  574. for (i = 0; i < Ncontrol; i++){
  575. if (strcmp(name, controls[rec][i].name) == 0)
  576. break;
  577. }
  578. if (i == Ncontrol)
  579. return -1;
  580. if (controls[rec][i].readable == 0)
  581. return -1;
  582. if(getspecialcontrol(rec, i, GET_CUR, value) < 0)
  583. return -1;
  584. memmove(controls[rec][i].value, value, sizeof controls[rec][i].value);
  585. return 0;
  586. }
  587. void
  588. getcontrols(void)
  589. {
  590. int rec, ctl, i;
  591. Audiocontrol *c;
  592. long v[8];
  593. for (rec = 0; rec < 2; rec++)
  594. for (ctl = 0; ctl < Ncontrol; ctl++){
  595. c = &controls[rec][ctl];
  596. if (c->readable){
  597. if (verbose)
  598. fprint(2, "%s %s control",
  599. rec?"Record":"Playback", controls[rec][ctl].name);
  600. c->min = (getspecialcontrol(rec, ctl, GET_MIN, v) < 0) ? Undef : v[0];
  601. if (verbose && c->min != Undef)
  602. fprint(2, ", min %ld", c->min);
  603. c->max = (getspecialcontrol(rec, ctl, GET_MAX, v) < 0) ? Undef : v[0];
  604. if (verbose && c->max != Undef)
  605. fprint(2, ", max %ld", c->max);
  606. c->step = (getspecialcontrol(rec, ctl, GET_RES, v) < 0) ? Undef : v[0];
  607. if (verbose && c->step != Undef)
  608. fprint(2, ", step %ld", c->step);
  609. if (getspecialcontrol(rec, ctl, GET_CUR, c->value) == 0){
  610. if (verbose) {
  611. if (c->chans){
  612. fprint(2, ", values");
  613. for (i = 1; i < 8; i++)
  614. if (c->chans & 1 << i)
  615. fprint(2, "[%d] %ld ", i, c->value[i]);
  616. }else
  617. fprint(2, ", value %ld", c->value[0]);
  618. }
  619. }
  620. if (verbose)
  621. fprint(2, "\n");
  622. } else {
  623. c->min = Undef;
  624. c->max = Undef;
  625. c->step = Undef;
  626. c->value[0] = Undef;
  627. if (debug & Dbgcontrol)
  628. fprint(2, "%s %s control not settable\n",
  629. rec?"Playback":"Record", controls[rec][ctl].name);
  630. }
  631. }
  632. }
  633. int
  634. ctlparse(char *s, Audiocontrol *c, long *v)
  635. {
  636. int i, j, nf, m;
  637. char *vals[9];
  638. char *p;
  639. long val;
  640. nf = tokenize(s, vals, nelem(vals));
  641. if (nf <= 0)
  642. return -1;
  643. if (c->chans){
  644. j = 0;
  645. m = 0;
  646. SET(val);
  647. v[0] = 0; // will compute average of v[i]
  648. for (i = 1; i < 8; i++)
  649. if (c->chans & 1 << i) {
  650. if (j < nf){
  651. val = strtol(vals[j], &p, 0);
  652. if (val == 0 && *p != '\0' && *p != '%')
  653. return -1;
  654. if (*p == '%' && c->min != Undef)
  655. val = (val*c->max + (100-val)*c->min)/100;
  656. j++;
  657. }
  658. v[i] = val;
  659. v[0] += val;
  660. m++;
  661. } else
  662. v[i] = Undef;
  663. if (m) v[0] /= m;
  664. } else {
  665. val = strtol(vals[0], &p, 0);
  666. if (*p == '%' && c->min != Undef)
  667. val = (val*c->max + (100-val)*c->min)/100;
  668. v[0] = val;
  669. }
  670. return 0;
  671. }
  672. int
  673. Aconv(Fmt *fp)
  674. {
  675. char str[256];
  676. Audiocontrol *c;
  677. int fst, i;
  678. char *p;
  679. c = va_arg(fp->args, Audiocontrol*);
  680. p = str;
  681. if (c->chans) {
  682. fst = 1;
  683. for (i = 1; i < 8; i++)
  684. if (c->chans & 1 << i){
  685. p = seprint(p, str+sizeof str, "%s%ld", fst?"'":" ", c->value[i]);
  686. fst = 0;
  687. }
  688. seprint(p, str+sizeof str, "'");
  689. } else
  690. seprint(p, str+sizeof str, "%ld", c->value[0]);
  691. return fmtstrcpy(fp, str);
  692. }