audiosub.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. Namelist terminal_types[] = {
  8. { 0x100, "USB Terminal, undefined type"},
  9. { 0x101, "USB Streaming"},
  10. { 0x201, "Microphone"},
  11. { 0x301, "Speaker"},
  12. { 0x302, "Headphones"},
  13. { 0x401, "Handset"}, /* bi-directional */
  14. { 0x501, "Phone Line"}, /* analog telephone line jack */
  15. { 0x601, "Analog connector"},
  16. { 0x602, "Digital audio interface"},
  17. { 0x603, "Line connector"}, /* 3.5mm jack */
  18. { 0x604, "Legacy audio connector"}, /* input, connected to audio OUT */
  19. { 0x605, "S/PDIF"},
  20. { 0x606, "1394 DA stream"},
  21. { 0x607, "1394 DV stream soundtrack"},
  22. { 0x703, "CD player"},
  23. { 0x704, "DAT"},
  24. { 0x706, "Minidisk"},
  25. { 0x70b, "DVD Audio"},
  26. { 0x713, "Synthesizer"},
  27. { 0, nil }
  28. };
  29. /* units[8][16] stores rec and play and other unit's id, K.Okamoto
  30. 8: one of Play, Record, masterRecAGC, masterRecMute, LRRecVol,
  31. LRPlayVol, masterPlayMute, masterPlayVol
  32. 8: unit's ID
  33. 1: unit attribute of readable, settable, chans, value[9] and min, max, step
  34. */
  35. FeatureAttr units[8][16];
  36. int nunits[8]; /* number in use for individual Unit's type K.Okamoto */
  37. int nf; /* current total number of featurelist K.Okamoto */
  38. static int
  39. findunit(int nr)
  40. {
  41. int rec, i;
  42. if (nr == 12) /* bSourceID == Mixer */
  43. return Play; /* K.Okamoto */
  44. for (rec = 0; rec < 2; rec++)
  45. for (i = 0; i < nunits[rec]; i++)
  46. if (units[rec][i].id == nr)
  47. return rec;
  48. return -1;
  49. }
  50. void
  51. audio_interface(Device *d, int n, ulong csp, void *bb, int nb) {
  52. byte *b = bb;
  53. int ctl, ch, u, x, nbchan, bmc;
  54. byte *p;
  55. int class, subclass, ifc, dalt;
  56. Audioalt *aa;
  57. if ((dalt = (csp >> 24) & 0xff) == 0xff) dalt = -1;
  58. if ((ifc = (csp >> 16) & 0xff) == 0xff) ifc = -1;
  59. class = Class(csp);
  60. subclass = Subclass(csp);
  61. if (debug & Dbginfo) fprint(2, "%d.%d: ", class, subclass);
  62. switch (subclass) {
  63. case 1: // control
  64. switch (b[2]) {
  65. case 0x01: /* class-specific AC interface header */
  66. if (debug & Dbginfo){
  67. fprint(2, "Class-Specific AC Interface Header Descriptor\n");
  68. fprint(2, "\tAudioDeviceClass release (bcd)%c%c%c%c, TotalLength %d, InCollection %d aInterfaceNr %d %d",
  69. '0'+((b[4]>>4)&0xf), '0'+(b[4]&0xf),
  70. '0'+((b[3]>>4)&0xf), '0'+(b[3]&0xf),
  71. b[5]|(b[6]<<8), b[7], 1, b[8]);
  72. if(b[7]-1) /* K.Okamoto */
  73. for(ctl=1;ctl<b[7];ctl++)
  74. fprint(2,", aInterfaceNr%d %d", ctl+1, b[8+ctl]);
  75. fprint(2, "\n");
  76. }
  77. break;
  78. case 0x02: // input
  79. if (debug & Dbginfo){
  80. fprint(2, "Audio Input Terminal Descriptor\n");
  81. fprint(2, "\tbTerminalId %d, wTerminalType 0x%x (%s), bAssocTerminal %d bNrChannels %d, wChannelConfig 0x%x, iChannelNames %d iTerminal %d\n",
  82. b[3], b[4]|(b[5]<<8),
  83. namefor(terminal_types, b[4]|(b[5]<<8)),
  84. b[6], b[7], b[8]|(b[9]<<8), b[10], b[11]);
  85. }
  86. if ((b[4]|b[5]<<8) == 0x101){ /* b[4]|b[5]<<8 = wTerminalType */
  87. if (verbose)
  88. fprint(2, "Audio output unit %d\n", b[3]);
  89. /* USB streaming input: play interface */
  90. units[Play][nunits[Play]++].id = b[3]; /* b[3] = bTerminalId */
  91. }else{
  92. if (verbose)
  93. fprint(2, "Device can record from %s\n",
  94. namefor(terminal_types, b[4]|(b[5]<<8)));
  95. /* Non-USB input: record interface */
  96. units[Record][nunits[Record]++].id = b[3];
  97. }
  98. break;
  99. case 0x03: // output
  100. if(debug & Dbginfo){
  101. fprint(2, "Audio Output Terminal Descriptor\n");
  102. fprint(2, "\tbTerminalId %d, wTerminalType 0x%x (%s), bAssocTerminal %d bSourceId %d, iTerminal %d\n",
  103. b[3], b[4]|(b[5]<<8),
  104. namefor(terminal_types, b[4]|(b[5]<<8)),
  105. b[6], b[7], b[8]);
  106. }
  107. if((b[4]|b[5]<<8) == 0x101){
  108. if (verbose)
  109. fprint(2, "Audio input unit %d\n", b[3]);
  110. /* USB streaming output: record interface */
  111. units[Record][nunits[Record]++].id = b[3];
  112. }else{
  113. if (verbose)
  114. fprint(2, "Device can play to %s\n",
  115. namefor(terminal_types, b[4]|(b[5]<<8)));
  116. /* Non-USB output: play interface */
  117. units[Play][nunits[Play]++].id = b[3];
  118. }
  119. break;
  120. case 0x04: /* mixer */
  121. if (debug & Dbginfo){ /* K.Okamoto */
  122. fprint(2, "Audio Mixer Unit %d\n", b[3]);
  123. fprint(2, "\tbNrInPins %d, ", b[4]);
  124. for(ctl = 1;ctl <= b[4]; ctl++)
  125. fprint(2, "Input Pin %d's UnitID %d, ", ctl, b[4+ctl]);
  126. ctl--;
  127. nbchan = b[5+ctl];
  128. fprint(2, "bNrChannels %d, ", nbchan);
  129. fprint(2, "wChannelConfig 0x%x, ", b[6+ctl]|b[7+ctl]<<8);
  130. fprint(2, "iChannelNames %d, ", b[8+ctl]);
  131. bmc = 9 + ctl;
  132. fprint(2, "bmControls 0x%x", b[bmc++]);
  133. nbchan = b[0]-bmc-1;
  134. if(nbchan > 1)
  135. while(nbchan--) fprint(2, "%x", b[bmc++]);
  136. fprint(2, ", iMixer %d\n", b[bmc]);
  137. }
  138. if (verbose)
  139. fprint(2, "Audio Mixer Unit %d\n", b[3]);
  140. mixerid = b[3];
  141. break;
  142. case 0x05: /* selector */
  143. if(verbose)
  144. fprint(2, "Audio Selector Unit %d\n", b[3]);
  145. if(debug & Dbginfo)
  146. fprint(2, "\tbUnitId %d, bNrInPins %d", b[3], b[4]);
  147. selector = b[3]; /* K.Okamoto */
  148. if(b[4]){ /* number of Pins >=1, K.Okamoto */
  149. if (debug & Dbginfo) fprint(2, ", baSourceIDs: [%d", b[5]);
  150. u = findunit(b[5]);
  151. for (ctl = 1; ctl < b[4]; ctl++){
  152. if (u < 0)
  153. u = findunit(b[5+ctl]);
  154. else if ((x = findunit(b[5+ctl])) >= 0 && u != x && verbose)
  155. fprint(2, "\tSelector %d for output AND input\n", b[3]);
  156. if (debug & Dbginfo) fprint(2, ", %d", b[5+ctl]);
  157. }
  158. if (debug & Dbginfo) fprint(2, "]\n");
  159. if (u >= 0){
  160. if (selectorid[u] >= 0)
  161. fprint(2, "Second selector (%d, %d) on %s\n", selectorid[u], b[3], u?"record":"playback");
  162. selectorid[u] = b[3];
  163. }
  164. }
  165. break;
  166. case 0x06:
  167. /* feature, b[6]: master channel, b[7]: logical channel 1, 2, 3, .. -->
  168. * b[5]: byte size of individual element data size
  169. * b[3] id number, b[4]: source ID,
  170. */
  171. if (verbose) fprint(2, "Audio Feature Unit %d", b[3]);
  172. if (debug & Dbginfo)
  173. fprint(2, "\tbUnitId %d, bSourceId %d, bControlSize %d\n",
  174. b[3], b[4], b[5]);
  175. u = findunit(b[4]);
  176. if (u >= 0){
  177. if (verbose) fprint(2, " for %s\n", u?"Record":"Playback");
  178. // units[u][nunits[u]++].id = b[3];
  179. if (featureid[u] >= 0)
  180. if (verbose) fprint(2, "Second feature unit (%d, %d) on %s\n", featureid[u], b[3], u?"record":"playback");
  181. featureid[u] = b[3];
  182. }else
  183. if (verbose) fprint(2, ", not known what for\n");
  184. p = b + 6; /* p: each logical channel data */
  185. /* check first for master channel */
  186. for(ctl = 1; ctl < 0x0b; ctl++){
  187. if((1<<(ctl-1)) & (b[6] | ((b[5]>1)?(b[7]<<8):0))){
  188. /* b[6]: master channel control,
  189. * D0: Mute, D1: Volume, D2: Bass, D3: Mid,
  190. * D4: Treble, D5: Equalizer, D6: Agc, D7: Delay,
  191. * D8: Bass Boost, D9: Loudness ...
  192. */
  193. if (ctl == 1 && u == Record){
  194. units[masterRecMute][nunits[masterRecMute]].id = b[3];
  195. units[masterRecMute][nunits[masterRecMute]].readable =1;
  196. units[masterRecMute][nunits[masterRecMute]].settable = 1;
  197. units[masterRecMute][nunits[masterRecMute]++].chans = 0;
  198. controls[u][ctl].readable = 1;
  199. controls[u][ctl].settable = 1;
  200. controls[u][ctl].chans = 0;
  201. } else if (ctl == 1 && u == Play) {
  202. units[masterPlayMute][nunits[masterPlayMute]].id = b[3];
  203. units[masterPlayMute][nunits[masterPlayMute]].readable = 1;
  204. units[masterPlayMute][nunits[masterPlayMute]].settable = 1;
  205. units[masterPlayMute][nunits[masterPlayMute]++].chans = 0;
  206. controls[u][ctl].readable = 1;
  207. controls[u][ctl].settable = 1;
  208. controls[u][ctl].chans = 0;
  209. } else if (ctl == 2 && u == Play) {
  210. units[masterPlayVol][nunits[masterPlayVol]].id = b[3];
  211. units[masterPlayVol][nunits[masterPlayVol]].readable = 1;
  212. units[masterPlayVol][nunits[masterPlayVol]].settable = 1;
  213. units[masterPlayVol][nunits[masterPlayVol]++].chans = 0;
  214. controls[u][ctl].readable = 1;
  215. controls[u][ctl].settable = 1;
  216. controls[u][ctl].chans = 0;
  217. } else if (ctl == 7 && u == Record){ /* K.Okamoto for CM106/F */
  218. units[masterRecAGC][nunits[masterRecAGC]].id = b[3];
  219. units[masterRecAGC][nunits[masterRecAGC]].readable = 1;
  220. units[masterRecAGC][nunits[masterRecAGC]].settable = 1;
  221. units[masterRecAGC][nunits[masterRecAGC]++].chans = 0;
  222. controls[u][ctl].readable = 1;
  223. controls[u][ctl].settable = 1;
  224. controls[u][ctl].chans = 0;
  225. }
  226. if (verbose)
  227. fprint(2, "\t%s control on master channel\n",
  228. controls[0][ctl].name);
  229. }
  230. }
  231. /* Now going to check each logical channel */
  232. p += (b[5]>1)?2:1;
  233. for (ch = 0; ch < (nb - 8)/b[5]; ch++) {
  234. for (ctl = 1; ctl < 0x0b; ctl++) {
  235. /* p[0]: each channel control,
  236. * D0: Mute, D1: Volume, D2: Bass, D3: Mid,
  237. * D4: Treble, D5: Equalizer, D6: Agc, D7: Delay,
  238. * D8: Bass Boost, D9: Loudness ...
  239. */
  240. if ((1<<(ctl-1)) & (p[0] | ((b[5]>1)?(p[1]<<8):0))) {
  241. if (ctl == 2 && u == Play ) {
  242. if (!ch) {
  243. units[LRPlayVol][nunits[LRPlayVol]].id = b[3];
  244. units[LRPlayVol][nunits[LRPlayVol]].readable = 1;
  245. units[LRPlayVol][nunits[LRPlayVol]].settable = 1;
  246. }
  247. units[LRPlayVol][nunits[LRPlayVol]].chans |= 1<<ch;
  248. }
  249. else if (ctl == 2 && u == Record) {
  250. if (!ch) {
  251. units[LRRecVol][nunits[LRRecVol]].id = b[3];
  252. units[LRRecVol][nunits[LRRecVol]].readable = 1;
  253. units[LRRecVol][nunits[LRRecVol]].settable = 1;
  254. }
  255. units[LRRecVol][nunits[LRRecVol]].chans |= 1<<ch;
  256. }
  257. /* Why we have to set these flags? K.Okamoto */
  258. controls[u][ctl].readable = 1;
  259. controls[u][ctl].settable = 1;
  260. controls[u][ctl].chans |= 1<<ch;
  261. /**/
  262. if (verbose)
  263. fprint(2, "\t%s control on channel %d\n",
  264. controls[0][ctl].name, ch+1);
  265. }
  266. }
  267. p += (b[5]>1)?2:1;
  268. }
  269. if (units[LRRecVol][nunits[LRRecVol]].id) nunits[LRRecVol]++;
  270. if (units[LRPlayVol][nunits[LRPlayVol]].id) nunits[LRPlayVol]++;
  271. if (debug & Dbginfo) { /* K.Okamoto */
  272. fprint(2, "Number of Play Units = %d, and consists of ", nunits[Play]);
  273. for(x=0; x<nunits[Play]; x++) fprint(2, "%d, ", units[Play][x].id);
  274. fprint(2, "\nNumber of Record Units = %d, and consists of ", nunits[Record]);
  275. for(x=0; x<nunits[Record]; x++) fprint(2, "%d, ", units[Record][x].id);
  276. fprint(2, "\nNumber of masterRecAGC Units = %d, and consists of ", nunits[masterRecAGC]);
  277. for(x=0; x<nunits[masterRecAGC]; x++)
  278. if (units[masterRecAGC][x].id) fprint(2, "%d, ", units[masterRecAGC][x].id);
  279. fprint(2, "\nNumber of masterRecMute Units = %d, and consists of ", nunits[masterRecMute]);
  280. for(x=0; x<nunits[masterRecMute]; x++)
  281. if (units[masterRecMute][x].id) fprint(2, "%d, ", units[masterRecMute][x].id);
  282. fprint(2, "\nNumber of LRRecVol Units = %d, and consists of ", nunits[LRRecVol]);
  283. for(x=0; x<nunits[LRRecVol]; x++)
  284. if (units[LRRecVol][x].id) fprint(2, "%d, ", units[LRRecVol][x].id);
  285. fprint(2, "\nNumber of masterPlayMute Units = %d, and consists of ", nunits[masterPlayMute]);
  286. for(x=0; x<nunits[masterPlayMute]; x++)
  287. if (units[masterPlayMute][x].id) fprint(2, "%d, ", units[masterPlayMute][x].id);
  288. fprint(2, "\nNumber of masterPlayVol Units = %d, and consists of ", nunits[masterPlayVol]);
  289. for(x=0; x<nunits[masterPlayVol]; x++)
  290. if (units[masterPlayVol][x].id) fprint(2, "%d, ", units[masterPlayVol][x].id);
  291. fprint(2, "\nNumber of LRPlayVol Units = %d, and consists of ", nunits[LRPlayVol]);
  292. for(x=0; x<nunits[LRPlayVol]; x++)
  293. if (units[LRPlayVol][x].id) fprint(2, "%d, ", units[LRPlayVol][x].id);
  294. fprint(2, "\n");
  295. }
  296. break;
  297. default:
  298. pcs_raw("audio control unknown", bb, nb);
  299. }
  300. break;
  301. case 2: // stream
  302. switch (b[2]) {
  303. case 0x01:
  304. if (debug & Dbginfo)
  305. fprint(2, "Audio stream for TerminalID %d, delay %d, format_tag %#ux\n",
  306. b[3], b[4], b[5] | (b[6]<<8));
  307. break;
  308. case 0x02:
  309. if (d->config[n]->iface[ifc]->dalt[dalt] == nil)
  310. d->config[n]->iface[ifc]->dalt[dalt] = mallocz(sizeof(Dalt),1);
  311. aa = (Audioalt *)d->config[n]->iface[ifc]->dalt[dalt]->devspec;
  312. if (aa == nil) {
  313. aa = mallocz(sizeof(Audioalt), 1);
  314. d->config[n]->iface[ifc]->dalt[dalt]->devspec = aa;
  315. }
  316. if (verbose){
  317. if (b[4] <= 2)
  318. fprint(2, "Interface %d, alt %d: %s, %d bits, ",
  319. ifc, dalt, (b[4] == 1)?"mono":"stereo", b[6]);
  320. else
  321. fprint(2, "Interface %d, alt %d: %d channels, %d bits, ",
  322. ifc, dalt, b[4], b[6]);
  323. }
  324. if(b[7] == 0){
  325. if (verbose)
  326. fprint(2, "frequency variable between %d and %d\n",
  327. b[8] | b[9]<<8 | b[10]<<16, b[11] | b[12]<<8 | b[13]<<16);
  328. aa->minfreq = b[8] | b[9]<<8 | b[10]<<16;
  329. aa->maxfreq = b[11] | b[12]<<8 | b[13]<<16;
  330. aa->caps |= has_contfreq;
  331. }else{
  332. if (verbose)
  333. fprint(2, "discrete frequencies are:");
  334. for (ch = 0; ch < b[7] && ch < 8; ch++){
  335. aa->freqs[ch] = b[8+3*ch] | b[9+3*ch]<<8 | b[10+3*ch]<<16;
  336. if (verbose)
  337. fprint(2, " %d", b[8+3*ch] | b[9+3*ch]<<8 | b[10+3*ch]<<16);
  338. }
  339. if (ch < 8)
  340. aa->freqs[ch] = -1;
  341. if (verbose)
  342. fprint(2, "\n");
  343. if (ch > 1)
  344. aa->caps |= has_discfreq; /* more than one frequency */
  345. else
  346. aa->caps |= onefreq; /* only one frequency */
  347. }
  348. aa->nchan = b[4];
  349. aa->res = b[6];
  350. aa->subframesize = b[5];
  351. break;
  352. default:
  353. if (debug & Dbginfo)
  354. pcs_raw("audio stream unknown", bb, nb);
  355. }
  356. break;
  357. case 3: // midi
  358. default:
  359. if (debug & Dbginfo){
  360. fprint(2, "Unknown audio stream type: ");
  361. pcs_raw("CS_INTERFACE", bb, nb);
  362. }
  363. }
  364. }