mixer_us16x08.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * Tascam US-16x08 ALSA driver
  3. *
  4. * Copyright (c) 2016 by Detlef Urban (onkel@paraair.de)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/audio-v2.h>
  20. #include <sound/core.h>
  21. #include <sound/control.h>
  22. #include "usbaudio.h"
  23. #include "mixer.h"
  24. #include "helper.h"
  25. #include "mixer_us16x08.h"
  26. /* USB control message templates */
  27. static const char route_msg[] = {
  28. 0x61,
  29. 0x02,
  30. 0x03, /* input from master (0x02) or input from computer bus (0x03) */
  31. 0x62,
  32. 0x02,
  33. 0x01, /* input index (0x01/0x02 eq. left/right) or bus (0x01-0x08) */
  34. 0x41,
  35. 0x01,
  36. 0x61,
  37. 0x02,
  38. 0x01,
  39. 0x62,
  40. 0x02,
  41. 0x01, /* output index (0x01-0x08) */
  42. 0x42,
  43. 0x01,
  44. 0x43,
  45. 0x01,
  46. 0x00,
  47. 0x00
  48. };
  49. static const char mix_init_msg1[] = {
  50. 0x71, 0x01, 0x00, 0x00
  51. };
  52. static const char mix_init_msg2[] = {
  53. 0x62, 0x02, 0x00, 0x61, 0x02, 0x04, 0xb1, 0x01, 0x00, 0x00
  54. };
  55. static const char mix_msg_in[] = {
  56. /* default message head, equal to all mixers */
  57. 0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
  58. 0x81, /* 0x06: Controller ID */
  59. 0x02, /* 0x07: */
  60. 0x00, /* 0x08: Value of common mixer */
  61. 0x00,
  62. 0x00
  63. };
  64. static const char mix_msg_out[] = {
  65. /* default message head, equal to all mixers */
  66. 0x61, 0x02, 0x02, 0x62, 0x02, 0x01,
  67. 0x81, /* 0x06: Controller ID */
  68. 0x02, /* 0x07: */
  69. 0x00, /* 0x08: Value of common mixer */
  70. 0x00,
  71. 0x00
  72. };
  73. static const char bypass_msg_out[] = {
  74. 0x45,
  75. 0x02,
  76. 0x01, /* on/off flag */
  77. 0x00,
  78. 0x00
  79. };
  80. static const char bus_msg_out[] = {
  81. 0x44,
  82. 0x02,
  83. 0x01, /* on/off flag */
  84. 0x00,
  85. 0x00
  86. };
  87. static const char comp_msg[] = {
  88. /* default message head, equal to all mixers */
  89. 0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
  90. 0x91,
  91. 0x02,
  92. 0xf0, /* 0x08: Threshold db (8) (e0 ... 00) (+-0dB -- -32dB) x-32 */
  93. 0x92,
  94. 0x02,
  95. 0x0a, /* 0x0b: Ratio (0a,0b,0d,0f,11,14,19,1e,23,28,32,3c,50,a0,ff) */
  96. 0x93,
  97. 0x02,
  98. 0x02, /* 0x0e: Attack (0x02 ... 0xc0) (2ms ... 200ms) */
  99. 0x94,
  100. 0x02,
  101. 0x01, /* 0x11: Release (0x01 ... 0x64) (10ms ... 1000ms) x*10 */
  102. 0x95,
  103. 0x02,
  104. 0x03, /* 0x14: gain (0 ... 20) (0dB .. 20dB) */
  105. 0x96,
  106. 0x02,
  107. 0x01,
  108. 0x97,
  109. 0x02,
  110. 0x01, /* 0x1a: main Comp switch (0 ... 1) (off ... on)) */
  111. 0x00,
  112. 0x00
  113. };
  114. static const char eqs_msq[] = {
  115. /* default message head, equal to all mixers */
  116. 0x61, 0x02, 0x04, 0x62, 0x02, 0x01,
  117. 0x51, /* 0x06: Controller ID */
  118. 0x02,
  119. 0x04, /* 0x08: EQ set num (0x01..0x04) (LOW, LOWMID, HIGHMID, HIGH)) */
  120. 0x52,
  121. 0x02,
  122. 0x0c, /* 0x0b: value dB (0 ... 12) (-12db .. +12db) x-6 */
  123. 0x53,
  124. 0x02,
  125. 0x0f, /* 0x0e: value freq (32-47) (1.7kHz..18kHz) */
  126. 0x54,
  127. 0x02,
  128. 0x02, /* 0x11: band width (0-6) (Q16-Q0.25) 2^x/4 (EQ xxMID only) */
  129. 0x55,
  130. 0x02,
  131. 0x01, /* 0x14: main EQ switch (0 ... 1) (off ... on)) */
  132. 0x00,
  133. 0x00
  134. };
  135. /* compressor ratio map */
  136. static const char ratio_map[] = {
  137. 0x0a, 0x0b, 0x0d, 0x0f, 0x11, 0x14, 0x19, 0x1e,
  138. 0x23, 0x28, 0x32, 0x3c, 0x50, 0xa0, 0xff
  139. };
  140. /* route enumeration names */
  141. static const char *const route_names[] = {
  142. "Master Left", "Master Right", "Output 1", "Output 2", "Output 3",
  143. "Output 4", "Output 5", "Output 6", "Output 7", "Output 8",
  144. };
  145. static int snd_us16x08_recv_urb(struct snd_usb_audio *chip,
  146. unsigned char *buf, int size)
  147. {
  148. mutex_lock(&chip->mutex);
  149. snd_usb_ctl_msg(chip->dev,
  150. usb_rcvctrlpipe(chip->dev, 0),
  151. SND_US16X08_URB_METER_REQUEST,
  152. SND_US16X08_URB_METER_REQUESTTYPE, 0, 0, buf, size);
  153. mutex_unlock(&chip->mutex);
  154. return 0;
  155. }
  156. /* wrapper function to send prepared URB buffer to usb device. Return an error
  157. * code if something went wrong
  158. */
  159. static int snd_us16x08_send_urb(struct snd_usb_audio *chip, char *buf, int size)
  160. {
  161. return snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
  162. SND_US16X08_URB_REQUEST, SND_US16X08_URB_REQUESTTYPE,
  163. 0, 0, buf, size);
  164. }
  165. static int snd_us16x08_route_info(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_info *uinfo)
  167. {
  168. return snd_ctl_enum_info(uinfo, 1, 10, route_names);
  169. }
  170. static int snd_us16x08_route_get(struct snd_kcontrol *kcontrol,
  171. struct snd_ctl_elem_value *ucontrol)
  172. {
  173. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  174. int index = ucontrol->id.index;
  175. /* route has no bias */
  176. ucontrol->value.enumerated.item[0] = elem->cache_val[index];
  177. return 0;
  178. }
  179. static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,
  180. struct snd_ctl_elem_value *ucontrol)
  181. {
  182. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  183. struct snd_usb_audio *chip = elem->head.mixer->chip;
  184. int index = ucontrol->id.index;
  185. char buf[sizeof(route_msg)];
  186. int val, val_org, err;
  187. /* get the new value (no bias for routes) */
  188. val = ucontrol->value.enumerated.item[0];
  189. /* sanity check */
  190. if (val < 0 || val > 9)
  191. return -EINVAL;
  192. /* prepare the message buffer from template */
  193. memcpy(buf, route_msg, sizeof(route_msg));
  194. if (val < 2) {
  195. /* input comes from a master channel */
  196. val_org = val;
  197. buf[2] = 0x02;
  198. } else {
  199. /* input comes from a computer channel */
  200. buf[2] = 0x03;
  201. val_org = val - 2;
  202. }
  203. /* place new route selection in URB message */
  204. buf[5] = (unsigned char) (val_org & 0x0f) + 1;
  205. /* place route selector in URB message */
  206. buf[13] = index + 1;
  207. err = snd_us16x08_send_urb(chip, buf, sizeof(route_msg));
  208. if (err > 0) {
  209. elem->cached |= 1 << index;
  210. elem->cache_val[index] = val;
  211. } else {
  212. usb_audio_dbg(chip, "Failed to set routing, err:%d\n", err);
  213. }
  214. return err > 0 ? 1 : 0;
  215. }
  216. static int snd_us16x08_master_info(struct snd_kcontrol *kcontrol,
  217. struct snd_ctl_elem_info *uinfo)
  218. {
  219. uinfo->count = 1;
  220. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  221. uinfo->value.integer.max = SND_US16X08_KCMAX(kcontrol);
  222. uinfo->value.integer.min = SND_US16X08_KCMIN(kcontrol);
  223. uinfo->value.integer.step = SND_US16X08_KCSTEP(kcontrol);
  224. return 0;
  225. }
  226. static int snd_us16x08_master_get(struct snd_kcontrol *kcontrol,
  227. struct snd_ctl_elem_value *ucontrol)
  228. {
  229. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  230. int index = ucontrol->id.index;
  231. ucontrol->value.integer.value[0] = elem->cache_val[index];
  232. return 0;
  233. }
  234. static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,
  235. struct snd_ctl_elem_value *ucontrol)
  236. {
  237. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  238. struct snd_usb_audio *chip = elem->head.mixer->chip;
  239. char buf[sizeof(mix_msg_out)];
  240. int val, err;
  241. int index = ucontrol->id.index;
  242. /* new control value incl. bias*/
  243. val = ucontrol->value.integer.value[0];
  244. /* sanity check */
  245. if (val < SND_US16X08_KCMIN(kcontrol)
  246. || val > SND_US16X08_KCMAX(kcontrol))
  247. return -EINVAL;
  248. /* prepare the message buffer from template */
  249. memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
  250. buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
  251. buf[6] = elem->head.id;
  252. /* place channel selector in URB message */
  253. buf[5] = index + 1;
  254. err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_out));
  255. if (err > 0) {
  256. elem->cached |= 1 << index;
  257. elem->cache_val[index] = val;
  258. } else {
  259. usb_audio_dbg(chip, "Failed to set master, err:%d\n", err);
  260. }
  261. return err > 0 ? 1 : 0;
  262. }
  263. static int snd_us16x08_bus_put(struct snd_kcontrol *kcontrol,
  264. struct snd_ctl_elem_value *ucontrol)
  265. {
  266. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  267. struct snd_usb_audio *chip = elem->head.mixer->chip;
  268. char buf[sizeof(mix_msg_out)];
  269. int val, err = 0;
  270. val = ucontrol->value.integer.value[0];
  271. /* prepare the message buffer from template */
  272. switch (elem->head.id) {
  273. case SND_US16X08_ID_BYPASS:
  274. memcpy(buf, bypass_msg_out, sizeof(bypass_msg_out));
  275. buf[2] = val;
  276. err = snd_us16x08_send_urb(chip, buf, sizeof(bypass_msg_out));
  277. break;
  278. case SND_US16X08_ID_BUSS_OUT:
  279. memcpy(buf, bus_msg_out, sizeof(bus_msg_out));
  280. buf[2] = val;
  281. err = snd_us16x08_send_urb(chip, buf, sizeof(bus_msg_out));
  282. break;
  283. case SND_US16X08_ID_MUTE:
  284. memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
  285. buf[8] = val;
  286. buf[6] = elem->head.id;
  287. buf[5] = 1;
  288. err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_out));
  289. break;
  290. }
  291. if (err > 0) {
  292. elem->cached |= 1;
  293. elem->cache_val[0] = val;
  294. } else {
  295. usb_audio_dbg(chip, "Failed to set buss param, err:%d\n", err);
  296. }
  297. return err > 0 ? 1 : 0;
  298. }
  299. static int snd_us16x08_bus_get(struct snd_kcontrol *kcontrol,
  300. struct snd_ctl_elem_value *ucontrol)
  301. {
  302. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  303. switch (elem->head.id) {
  304. case SND_US16X08_ID_BUSS_OUT:
  305. ucontrol->value.integer.value[0] = elem->cache_val[0];
  306. break;
  307. case SND_US16X08_ID_BYPASS:
  308. ucontrol->value.integer.value[0] = elem->cache_val[0];
  309. break;
  310. case SND_US16X08_ID_MUTE:
  311. ucontrol->value.integer.value[0] = elem->cache_val[0];
  312. break;
  313. }
  314. return 0;
  315. }
  316. /* gets a current mixer value from common store */
  317. static int snd_us16x08_channel_get(struct snd_kcontrol *kcontrol,
  318. struct snd_ctl_elem_value *ucontrol)
  319. {
  320. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  321. int index = ucontrol->id.index;
  322. ucontrol->value.integer.value[0] = elem->cache_val[index];
  323. return 0;
  324. }
  325. static int snd_us16x08_channel_put(struct snd_kcontrol *kcontrol,
  326. struct snd_ctl_elem_value *ucontrol)
  327. {
  328. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  329. struct snd_usb_audio *chip = elem->head.mixer->chip;
  330. char buf[sizeof(mix_msg_in)];
  331. int val, err;
  332. int index = ucontrol->id.index;
  333. val = ucontrol->value.integer.value[0];
  334. /* sanity check */
  335. if (val < SND_US16X08_KCMIN(kcontrol)
  336. || val > SND_US16X08_KCMAX(kcontrol))
  337. return -EINVAL;
  338. /* prepare URB message from template */
  339. memcpy(buf, mix_msg_in, sizeof(mix_msg_in));
  340. /* add the bias to the new value */
  341. buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
  342. buf[6] = elem->head.id;
  343. buf[5] = index + 1;
  344. err = snd_us16x08_send_urb(chip, buf, sizeof(mix_msg_in));
  345. if (err > 0) {
  346. elem->cached |= 1 << index;
  347. elem->cache_val[index] = val;
  348. } else {
  349. usb_audio_dbg(chip, "Failed to set channel, err:%d\n", err);
  350. }
  351. return err > 0 ? 1 : 0;
  352. }
  353. static int snd_us16x08_mix_info(struct snd_kcontrol *kcontrol,
  354. struct snd_ctl_elem_info *uinfo)
  355. {
  356. uinfo->count = 1;
  357. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  358. uinfo->value.integer.max = SND_US16X08_KCMAX(kcontrol);
  359. uinfo->value.integer.min = SND_US16X08_KCMIN(kcontrol);
  360. uinfo->value.integer.step = SND_US16X08_KCSTEP(kcontrol);
  361. return 0;
  362. }
  363. static int snd_us16x08_comp_get(struct snd_kcontrol *kcontrol,
  364. struct snd_ctl_elem_value *ucontrol)
  365. {
  366. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  367. struct snd_us16x08_comp_store *store = elem->private_data;
  368. int index = ucontrol->id.index;
  369. int val_idx = COMP_STORE_IDX(elem->head.id);
  370. ucontrol->value.integer.value[0] = store->val[val_idx][index];
  371. return 0;
  372. }
  373. static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
  374. struct snd_ctl_elem_value *ucontrol)
  375. {
  376. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  377. struct snd_usb_audio *chip = elem->head.mixer->chip;
  378. struct snd_us16x08_comp_store *store = elem->private_data;
  379. int index = ucontrol->id.index;
  380. char buf[sizeof(comp_msg)];
  381. int val_idx, val;
  382. int err;
  383. val = ucontrol->value.integer.value[0];
  384. /* sanity check */
  385. if (val < SND_US16X08_KCMIN(kcontrol)
  386. || val > SND_US16X08_KCMAX(kcontrol))
  387. return -EINVAL;
  388. /* new control value incl. bias*/
  389. val_idx = elem->head.id - SND_US16X08_ID_COMP_BASE;
  390. store->val[val_idx][index] = ucontrol->value.integer.value[0];
  391. /* prepare compressor URB message from template */
  392. memcpy(buf, comp_msg, sizeof(comp_msg));
  393. /* place comp values in message buffer watch bias! */
  394. buf[8] = store->val[
  395. COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][index]
  396. - SND_US16X08_COMP_THRESHOLD_BIAS;
  397. buf[11] = ratio_map[store->val[
  398. COMP_STORE_IDX(SND_US16X08_ID_COMP_RATIO)][index]];
  399. buf[14] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_ATTACK)][index]
  400. + SND_US16X08_COMP_ATTACK_BIAS;
  401. buf[17] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RELEASE)][index]
  402. + SND_US16X08_COMP_RELEASE_BIAS;
  403. buf[20] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_GAIN)][index];
  404. buf[26] = store->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)][index];
  405. /* place channel selector in message buffer */
  406. buf[5] = index + 1;
  407. err = snd_us16x08_send_urb(chip, buf, sizeof(comp_msg));
  408. if (err > 0) {
  409. elem->cached |= 1 << index;
  410. elem->cache_val[index] = val;
  411. } else {
  412. usb_audio_dbg(chip, "Failed to set compressor, err:%d\n", err);
  413. }
  414. return 1;
  415. }
  416. static int snd_us16x08_eqswitch_get(struct snd_kcontrol *kcontrol,
  417. struct snd_ctl_elem_value *ucontrol)
  418. {
  419. int val;
  420. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  421. struct snd_us16x08_eq_store *store = elem->private_data;
  422. int index = ucontrol->id.index;
  423. /* get low switch from cache is enough, cause all bands are together */
  424. val = store->val[EQ_STORE_BAND_IDX(elem->head.id)]
  425. [EQ_STORE_PARAM_IDX(elem->head.id)][index];
  426. ucontrol->value.integer.value[0] = val;
  427. return 0;
  428. }
  429. static int snd_us16x08_eqswitch_put(struct snd_kcontrol *kcontrol,
  430. struct snd_ctl_elem_value *ucontrol)
  431. {
  432. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  433. struct snd_usb_audio *chip = elem->head.mixer->chip;
  434. struct snd_us16x08_eq_store *store = elem->private_data;
  435. int index = ucontrol->id.index;
  436. char buf[sizeof(eqs_msq)];
  437. int val, err = 0;
  438. int b_idx;
  439. /* new control value incl. bias*/
  440. val = ucontrol->value.integer.value[0] + SND_US16X08_KCBIAS(kcontrol);
  441. /* prepare URB message from EQ template */
  442. memcpy(buf, eqs_msq, sizeof(eqs_msq));
  443. /* place channel index in URB message */
  444. buf[5] = index + 1;
  445. for (b_idx = 0; b_idx < SND_US16X08_ID_EQ_BAND_COUNT; b_idx++) {
  446. /* all four EQ bands have to be enabled/disabled in once */
  447. buf[20] = val;
  448. buf[17] = store->val[b_idx][2][index];
  449. buf[14] = store->val[b_idx][1][index];
  450. buf[11] = store->val[b_idx][0][index];
  451. buf[8] = b_idx + 1;
  452. err = snd_us16x08_send_urb(chip, buf, sizeof(eqs_msq));
  453. if (err < 0)
  454. break;
  455. store->val[b_idx][3][index] = val;
  456. msleep(15);
  457. }
  458. if (err > 0) {
  459. elem->cached |= 1 << index;
  460. elem->cache_val[index] = val;
  461. } else {
  462. usb_audio_dbg(chip, "Failed to set eq switch, err:%d\n", err);
  463. }
  464. return 1;
  465. }
  466. static int snd_us16x08_eq_get(struct snd_kcontrol *kcontrol,
  467. struct snd_ctl_elem_value *ucontrol)
  468. {
  469. int val;
  470. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  471. struct snd_us16x08_eq_store *store = elem->private_data;
  472. int index = ucontrol->id.index;
  473. int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
  474. int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
  475. val = store->val[b_idx][p_idx][index];
  476. ucontrol->value.integer.value[0] = val;
  477. return 0;
  478. }
  479. static int snd_us16x08_eq_put(struct snd_kcontrol *kcontrol,
  480. struct snd_ctl_elem_value *ucontrol)
  481. {
  482. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  483. struct snd_usb_audio *chip = elem->head.mixer->chip;
  484. struct snd_us16x08_eq_store *store = elem->private_data;
  485. int index = ucontrol->id.index;
  486. char buf[sizeof(eqs_msq)];
  487. int val, err;
  488. int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
  489. int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
  490. val = ucontrol->value.integer.value[0];
  491. /* sanity check */
  492. if (val < SND_US16X08_KCMIN(kcontrol)
  493. || val > SND_US16X08_KCMAX(kcontrol))
  494. return -EINVAL;
  495. /* copy URB buffer from EQ template */
  496. memcpy(buf, eqs_msq, sizeof(eqs_msq));
  497. store->val[b_idx][p_idx][index] = val;
  498. buf[20] = store->val[b_idx][3][index];
  499. buf[17] = store->val[b_idx][2][index];
  500. buf[14] = store->val[b_idx][1][index];
  501. buf[11] = store->val[b_idx][0][index];
  502. /* place channel index in URB buffer */
  503. buf[5] = index + 1;
  504. /* place EQ band in URB buffer */
  505. buf[8] = b_idx + 1;
  506. err = snd_us16x08_send_urb(chip, buf, sizeof(eqs_msq));
  507. if (err > 0) {
  508. /* store new value in EQ band cache */
  509. elem->cached |= 1 << index;
  510. elem->cache_val[index] = val;
  511. } else {
  512. usb_audio_dbg(chip, "Failed to set eq param, err:%d\n", err);
  513. }
  514. return 1;
  515. }
  516. static int snd_us16x08_meter_info(struct snd_kcontrol *kcontrol,
  517. struct snd_ctl_elem_info *uinfo)
  518. {
  519. uinfo->count = 34;
  520. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  521. uinfo->value.integer.max = 0x7FFF;
  522. uinfo->value.integer.min = 0;
  523. return 0;
  524. }
  525. /* calculate compressor index for reduction level request */
  526. static int snd_get_meter_comp_index(struct snd_us16x08_meter_store *store)
  527. {
  528. int ret;
  529. /* any channel active */
  530. if (store->comp_active_index) {
  531. /* check for stereo link */
  532. if (store->comp_active_index & 0x20) {
  533. /* reset comp_index to left channel*/
  534. if (store->comp_index -
  535. store->comp_active_index > 1)
  536. store->comp_index =
  537. store->comp_active_index;
  538. ret = store->comp_index++ & 0x1F;
  539. } else {
  540. /* no stereo link */
  541. ret = store->comp_active_index;
  542. }
  543. } else {
  544. /* skip channels with no compressor active */
  545. while (!store->comp_store->val[
  546. COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)]
  547. [store->comp_index - 1]
  548. && store->comp_index <= SND_US16X08_MAX_CHANNELS) {
  549. store->comp_index++;
  550. }
  551. ret = store->comp_index++;
  552. if (store->comp_index > SND_US16X08_MAX_CHANNELS)
  553. store->comp_index = 1;
  554. }
  555. return ret;
  556. }
  557. /* retrieve the meter level values from URB message */
  558. static void get_meter_levels_from_urb(int s,
  559. struct snd_us16x08_meter_store *store,
  560. u8 *meter_urb)
  561. {
  562. int val = MUC2(meter_urb, s) + (MUC3(meter_urb, s) << 8);
  563. if (MUA0(meter_urb, s) == 0x61 && MUA1(meter_urb, s) == 0x02 &&
  564. MUA2(meter_urb, s) == 0x04 && MUB0(meter_urb, s) == 0x62) {
  565. if (MUC0(meter_urb, s) == 0x72)
  566. store->meter_level[MUB2(meter_urb, s) - 1] = val;
  567. if (MUC0(meter_urb, s) == 0xb2)
  568. store->comp_level[MUB2(meter_urb, s) - 1] = val;
  569. }
  570. if (MUA0(meter_urb, s) == 0x61 && MUA1(meter_urb, s) == 0x02 &&
  571. MUA2(meter_urb, s) == 0x02 && MUB0(meter_urb, s) == 0x62)
  572. store->master_level[MUB2(meter_urb, s) - 1] = val;
  573. }
  574. /* Function to retrieve current meter values from the device.
  575. *
  576. * The device needs to be polled for meter values with an initial
  577. * requests. It will return with a sequence of different meter value
  578. * packages. The first request (case 0:) initiate this meter response sequence.
  579. * After the third response, an additional request can be placed,
  580. * to retrieve compressor reduction level value for given channel. This round
  581. * trip channel selector will skip all inactive compressors.
  582. * A mixer can interrupt this round-trip by selecting one ore two (stereo-link)
  583. * specific channels.
  584. */
  585. static int snd_us16x08_meter_get(struct snd_kcontrol *kcontrol,
  586. struct snd_ctl_elem_value *ucontrol)
  587. {
  588. int i, set;
  589. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  590. struct snd_usb_audio *chip = elem->head.mixer->chip;
  591. struct snd_us16x08_meter_store *store = elem->private_data;
  592. u8 meter_urb[64];
  593. switch (kcontrol->private_value) {
  594. case 0: {
  595. char tmp[sizeof(mix_init_msg1)];
  596. memcpy(tmp, mix_init_msg1, sizeof(mix_init_msg1));
  597. snd_us16x08_send_urb(chip, tmp, 4);
  598. snd_us16x08_recv_urb(chip, meter_urb,
  599. sizeof(meter_urb));
  600. kcontrol->private_value++;
  601. break;
  602. }
  603. case 1:
  604. snd_us16x08_recv_urb(chip, meter_urb,
  605. sizeof(meter_urb));
  606. kcontrol->private_value++;
  607. break;
  608. case 2:
  609. snd_us16x08_recv_urb(chip, meter_urb,
  610. sizeof(meter_urb));
  611. kcontrol->private_value++;
  612. break;
  613. case 3: {
  614. char tmp[sizeof(mix_init_msg2)];
  615. memcpy(tmp, mix_init_msg2, sizeof(mix_init_msg2));
  616. tmp[2] = snd_get_meter_comp_index(store);
  617. snd_us16x08_send_urb(chip, tmp, 10);
  618. snd_us16x08_recv_urb(chip, meter_urb,
  619. sizeof(meter_urb));
  620. kcontrol->private_value = 0;
  621. break;
  622. }
  623. }
  624. for (set = 0; set < 6; set++)
  625. get_meter_levels_from_urb(set, store, meter_urb);
  626. for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
  627. ucontrol->value.integer.value[i] =
  628. store ? store->meter_level[i] : 0;
  629. }
  630. ucontrol->value.integer.value[i++] = store ? store->master_level[0] : 0;
  631. ucontrol->value.integer.value[i++] = store ? store->master_level[1] : 0;
  632. for (i = 2; i < SND_US16X08_MAX_CHANNELS + 2; i++)
  633. ucontrol->value.integer.value[i + SND_US16X08_MAX_CHANNELS] =
  634. store ? store->comp_level[i - 2] : 0;
  635. return 1;
  636. }
  637. static int snd_us16x08_meter_put(struct snd_kcontrol *kcontrol,
  638. struct snd_ctl_elem_value *ucontrol)
  639. {
  640. struct usb_mixer_elem_info *elem = kcontrol->private_data;
  641. struct snd_us16x08_meter_store *store = elem->private_data;
  642. int val;
  643. val = ucontrol->value.integer.value[0];
  644. /* sanity check */
  645. if (val < 0 || val >= SND_US16X08_MAX_CHANNELS)
  646. return -EINVAL;
  647. store->comp_active_index = val;
  648. store->comp_index = val;
  649. return 1;
  650. }
  651. static struct snd_kcontrol_new snd_us16x08_ch_boolean_ctl = {
  652. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  653. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  654. .count = 16,
  655. .info = snd_us16x08_switch_info,
  656. .get = snd_us16x08_channel_get,
  657. .put = snd_us16x08_channel_put,
  658. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  659. };
  660. static struct snd_kcontrol_new snd_us16x08_ch_int_ctl = {
  661. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  662. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  663. .count = 16,
  664. .info = snd_us16x08_mix_info,
  665. .get = snd_us16x08_channel_get,
  666. .put = snd_us16x08_channel_put,
  667. .private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 133)
  668. };
  669. static struct snd_kcontrol_new snd_us16x08_pan_int_ctl = {
  670. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  671. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  672. .count = 16,
  673. .info = snd_us16x08_mix_info,
  674. .get = snd_us16x08_channel_get,
  675. .put = snd_us16x08_channel_put,
  676. .private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 255)
  677. };
  678. static struct snd_kcontrol_new snd_us16x08_master_ctl = {
  679. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  680. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  681. .count = 1,
  682. .info = snd_us16x08_master_info,
  683. .get = snd_us16x08_master_get,
  684. .put = snd_us16x08_master_put,
  685. .private_value = SND_US16X08_KCSET(SND_US16X08_FADER_BIAS, 1, 0, 133)
  686. };
  687. static struct snd_kcontrol_new snd_us16x08_route_ctl = {
  688. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  689. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  690. .count = 8,
  691. .info = snd_us16x08_route_info,
  692. .get = snd_us16x08_route_get,
  693. .put = snd_us16x08_route_put,
  694. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 9)
  695. };
  696. static struct snd_kcontrol_new snd_us16x08_bus_ctl = {
  697. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  698. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  699. .count = 1,
  700. .info = snd_us16x08_switch_info,
  701. .get = snd_us16x08_bus_get,
  702. .put = snd_us16x08_bus_put,
  703. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  704. };
  705. static struct snd_kcontrol_new snd_us16x08_compswitch_ctl = {
  706. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  707. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  708. .count = 16,
  709. .info = snd_us16x08_switch_info,
  710. .get = snd_us16x08_comp_get,
  711. .put = snd_us16x08_comp_put,
  712. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  713. };
  714. static struct snd_kcontrol_new snd_us16x08_comp_threshold_ctl = {
  715. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  716. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  717. .count = 16,
  718. .info = snd_us16x08_mix_info,
  719. .get = snd_us16x08_comp_get,
  720. .put = snd_us16x08_comp_put,
  721. .private_value = SND_US16X08_KCSET(SND_US16X08_COMP_THRESHOLD_BIAS, 1,
  722. 0, 0x20)
  723. };
  724. static struct snd_kcontrol_new snd_us16x08_comp_ratio_ctl = {
  725. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  726. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  727. .count = 16,
  728. .info = snd_us16x08_mix_info,
  729. .get = snd_us16x08_comp_get,
  730. .put = snd_us16x08_comp_put,
  731. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0,
  732. sizeof(ratio_map) - 1), /*max*/
  733. };
  734. static struct snd_kcontrol_new snd_us16x08_comp_gain_ctl = {
  735. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  736. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  737. .count = 16,
  738. .info = snd_us16x08_mix_info,
  739. .get = snd_us16x08_comp_get,
  740. .put = snd_us16x08_comp_put,
  741. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x14)
  742. };
  743. static struct snd_kcontrol_new snd_us16x08_comp_attack_ctl = {
  744. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  745. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  746. .count = 16,
  747. .info = snd_us16x08_mix_info,
  748. .get = snd_us16x08_comp_get,
  749. .put = snd_us16x08_comp_put,
  750. .private_value =
  751. SND_US16X08_KCSET(SND_US16X08_COMP_ATTACK_BIAS, 1, 0, 0xc6),
  752. };
  753. static struct snd_kcontrol_new snd_us16x08_comp_release_ctl = {
  754. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  755. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  756. .count = 16,
  757. .info = snd_us16x08_mix_info,
  758. .get = snd_us16x08_comp_get,
  759. .put = snd_us16x08_comp_put,
  760. .private_value =
  761. SND_US16X08_KCSET(SND_US16X08_COMP_RELEASE_BIAS, 1, 0, 0x63),
  762. };
  763. static struct snd_kcontrol_new snd_us16x08_eq_gain_ctl = {
  764. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  765. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  766. .count = 16,
  767. .info = snd_us16x08_mix_info,
  768. .get = snd_us16x08_eq_get,
  769. .put = snd_us16x08_eq_put,
  770. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 24),
  771. };
  772. static struct snd_kcontrol_new snd_us16x08_eq_low_freq_ctl = {
  773. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  774. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  775. .count = 16,
  776. .info = snd_us16x08_mix_info,
  777. .get = snd_us16x08_eq_get,
  778. .put = snd_us16x08_eq_put,
  779. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x1F),
  780. };
  781. static struct snd_kcontrol_new snd_us16x08_eq_mid_freq_ctl = {
  782. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  783. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  784. .count = 16,
  785. .info = snd_us16x08_mix_info,
  786. .get = snd_us16x08_eq_get,
  787. .put = snd_us16x08_eq_put,
  788. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x3F)
  789. };
  790. static struct snd_kcontrol_new snd_us16x08_eq_mid_width_ctl = {
  791. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  792. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  793. .count = 16,
  794. .info = snd_us16x08_mix_info,
  795. .get = snd_us16x08_eq_get,
  796. .put = snd_us16x08_eq_put,
  797. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 0x06)
  798. };
  799. static struct snd_kcontrol_new snd_us16x08_eq_high_freq_ctl = {
  800. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  801. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  802. .count = 16,
  803. .info = snd_us16x08_mix_info,
  804. .get = snd_us16x08_eq_get,
  805. .put = snd_us16x08_eq_put,
  806. .private_value =
  807. SND_US16X08_KCSET(SND_US16X08_EQ_HIGHFREQ_BIAS, 1, 0, 0x1F)
  808. };
  809. static struct snd_kcontrol_new snd_us16x08_eq_switch_ctl = {
  810. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  811. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  812. .count = 16,
  813. .info = snd_us16x08_switch_info,
  814. .get = snd_us16x08_eqswitch_get,
  815. .put = snd_us16x08_eqswitch_put,
  816. .private_value = SND_US16X08_KCSET(SND_US16X08_NO_BIAS, 1, 0, 1)
  817. };
  818. static struct snd_kcontrol_new snd_us16x08_meter_ctl = {
  819. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  820. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  821. .count = 1,
  822. .info = snd_us16x08_meter_info,
  823. .get = snd_us16x08_meter_get,
  824. .put = snd_us16x08_meter_put
  825. };
  826. /* control store preparation */
  827. /* setup compressor store and assign default value */
  828. static struct snd_us16x08_comp_store *snd_us16x08_create_comp_store(void)
  829. {
  830. int i;
  831. struct snd_us16x08_comp_store *tmp;
  832. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  833. if (!tmp)
  834. return NULL;
  835. for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
  836. tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][i]
  837. = 0x20;
  838. tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RATIO)][i] = 0x00;
  839. tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_GAIN)][i] = 0x00;
  840. tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_SWITCH)][i] = 0x00;
  841. tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_ATTACK)][i] = 0x00;
  842. tmp->val[COMP_STORE_IDX(SND_US16X08_ID_COMP_RELEASE)][i] = 0x00;
  843. }
  844. return tmp;
  845. }
  846. /* setup EQ store and assign default values */
  847. static struct snd_us16x08_eq_store *snd_us16x08_create_eq_store(void)
  848. {
  849. int i, b_idx;
  850. struct snd_us16x08_eq_store *tmp;
  851. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  852. if (!tmp)
  853. return NULL;
  854. for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
  855. for (b_idx = 0; b_idx < SND_US16X08_ID_EQ_BAND_COUNT; b_idx++) {
  856. tmp->val[b_idx][0][i] = 0x0c;
  857. tmp->val[b_idx][3][i] = 0x00;
  858. switch (b_idx) {
  859. case 0: /* EQ Low */
  860. tmp->val[b_idx][1][i] = 0x05;
  861. tmp->val[b_idx][2][i] = 0xff;
  862. break;
  863. case 1: /* EQ Mid low */
  864. tmp->val[b_idx][1][i] = 0x0e;
  865. tmp->val[b_idx][2][i] = 0x02;
  866. break;
  867. case 2: /* EQ Mid High */
  868. tmp->val[b_idx][1][i] = 0x1b;
  869. tmp->val[b_idx][2][i] = 0x02;
  870. break;
  871. case 3: /* EQ High */
  872. tmp->val[b_idx][1][i] = 0x2f
  873. - SND_US16X08_EQ_HIGHFREQ_BIAS;
  874. tmp->val[b_idx][2][i] = 0xff;
  875. break;
  876. }
  877. }
  878. }
  879. return tmp;
  880. }
  881. static struct snd_us16x08_meter_store *snd_us16x08_create_meter_store(void)
  882. {
  883. struct snd_us16x08_meter_store *tmp;
  884. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  885. if (!tmp)
  886. return NULL;
  887. tmp->comp_index = 1;
  888. tmp->comp_active_index = 0;
  889. return tmp;
  890. }
  891. /* release elem->private_free as well; called only once for each *_store */
  892. static void elem_private_free(struct snd_kcontrol *kctl)
  893. {
  894. struct usb_mixer_elem_info *elem = kctl->private_data;
  895. if (elem)
  896. kfree(elem->private_data);
  897. kfree(elem);
  898. kctl->private_data = NULL;
  899. }
  900. static int add_new_ctl(struct usb_mixer_interface *mixer,
  901. const struct snd_kcontrol_new *ncontrol,
  902. int index, int val_type, int channels,
  903. const char *name, void *opt,
  904. bool do_private_free,
  905. struct usb_mixer_elem_info **elem_ret)
  906. {
  907. struct snd_kcontrol *kctl;
  908. struct usb_mixer_elem_info *elem;
  909. int err;
  910. usb_audio_dbg(mixer->chip, "us16x08 add mixer %s\n", name);
  911. elem = kzalloc(sizeof(*elem), GFP_KERNEL);
  912. if (!elem)
  913. return -ENOMEM;
  914. elem->head.mixer = mixer;
  915. elem->head.resume = NULL;
  916. elem->control = 0;
  917. elem->idx_off = 0;
  918. elem->head.id = index;
  919. elem->val_type = val_type;
  920. elem->channels = channels;
  921. elem->private_data = opt;
  922. kctl = snd_ctl_new1(ncontrol, elem);
  923. if (!kctl) {
  924. kfree(elem);
  925. return -ENOMEM;
  926. }
  927. if (do_private_free)
  928. kctl->private_free = elem_private_free;
  929. else
  930. kctl->private_free = snd_usb_mixer_elem_free;
  931. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  932. err = snd_usb_mixer_add_control(&elem->head, kctl);
  933. if (err < 0)
  934. return err;
  935. if (elem_ret)
  936. *elem_ret = elem;
  937. return 0;
  938. }
  939. /* table of EQ controls */
  940. static const struct snd_us16x08_control_params eq_controls[] = {
  941. { /* EQ switch */
  942. .kcontrol_new = &snd_us16x08_eq_switch_ctl,
  943. .control_id = SND_US16X08_ID_EQENABLE,
  944. .type = USB_MIXER_BOOLEAN,
  945. .num_channels = 16,
  946. .name = "EQ Switch",
  947. },
  948. { /* EQ low gain */
  949. .kcontrol_new = &snd_us16x08_eq_gain_ctl,
  950. .control_id = SND_US16X08_ID_EQLOWLEVEL,
  951. .type = USB_MIXER_U8,
  952. .num_channels = 16,
  953. .name = "EQ Low Volume",
  954. },
  955. { /* EQ low freq */
  956. .kcontrol_new = &snd_us16x08_eq_low_freq_ctl,
  957. .control_id = SND_US16X08_ID_EQLOWFREQ,
  958. .type = USB_MIXER_U8,
  959. .num_channels = 16,
  960. .name = "EQ Low Frequence",
  961. },
  962. { /* EQ mid low gain */
  963. .kcontrol_new = &snd_us16x08_eq_gain_ctl,
  964. .control_id = SND_US16X08_ID_EQLOWMIDLEVEL,
  965. .type = USB_MIXER_U8,
  966. .num_channels = 16,
  967. .name = "EQ MidLow Volume",
  968. },
  969. { /* EQ mid low freq */
  970. .kcontrol_new = &snd_us16x08_eq_mid_freq_ctl,
  971. .control_id = SND_US16X08_ID_EQLOWMIDFREQ,
  972. .type = USB_MIXER_U8,
  973. .num_channels = 16,
  974. .name = "EQ MidLow Frequence",
  975. },
  976. { /* EQ mid low Q */
  977. .kcontrol_new = &snd_us16x08_eq_mid_width_ctl,
  978. .control_id = SND_US16X08_ID_EQLOWMIDWIDTH,
  979. .type = USB_MIXER_U8,
  980. .num_channels = 16,
  981. .name = "EQ MidLow Q",
  982. },
  983. { /* EQ mid high gain */
  984. .kcontrol_new = &snd_us16x08_eq_gain_ctl,
  985. .control_id = SND_US16X08_ID_EQHIGHMIDLEVEL,
  986. .type = USB_MIXER_U8,
  987. .num_channels = 16,
  988. .name = "EQ MidHigh Volume",
  989. },
  990. { /* EQ mid high freq */
  991. .kcontrol_new = &snd_us16x08_eq_mid_freq_ctl,
  992. .control_id = SND_US16X08_ID_EQHIGHMIDFREQ,
  993. .type = USB_MIXER_U8,
  994. .num_channels = 16,
  995. .name = "EQ MidHigh Frequence",
  996. },
  997. { /* EQ mid high Q */
  998. .kcontrol_new = &snd_us16x08_eq_mid_width_ctl,
  999. .control_id = SND_US16X08_ID_EQHIGHMIDWIDTH,
  1000. .type = USB_MIXER_U8,
  1001. .num_channels = 16,
  1002. .name = "EQ MidHigh Q",
  1003. },
  1004. { /* EQ high gain */
  1005. .kcontrol_new = &snd_us16x08_eq_gain_ctl,
  1006. .control_id = SND_US16X08_ID_EQHIGHLEVEL,
  1007. .type = USB_MIXER_U8,
  1008. .num_channels = 16,
  1009. .name = "EQ High Volume",
  1010. },
  1011. { /* EQ low freq */
  1012. .kcontrol_new = &snd_us16x08_eq_high_freq_ctl,
  1013. .control_id = SND_US16X08_ID_EQHIGHFREQ,
  1014. .type = USB_MIXER_U8,
  1015. .num_channels = 16,
  1016. .name = "EQ High Frequence",
  1017. },
  1018. };
  1019. /* table of compressor controls */
  1020. static const struct snd_us16x08_control_params comp_controls[] = {
  1021. { /* Comp enable */
  1022. .kcontrol_new = &snd_us16x08_compswitch_ctl,
  1023. .control_id = SND_US16X08_ID_COMP_SWITCH,
  1024. .type = USB_MIXER_BOOLEAN,
  1025. .num_channels = 16,
  1026. .name = "Compressor Switch",
  1027. },
  1028. { /* Comp threshold */
  1029. .kcontrol_new = &snd_us16x08_comp_threshold_ctl,
  1030. .control_id = SND_US16X08_ID_COMP_THRESHOLD,
  1031. .type = USB_MIXER_U8,
  1032. .num_channels = 16,
  1033. .name = "Compressor Threshold Volume",
  1034. },
  1035. { /* Comp ratio */
  1036. .kcontrol_new = &snd_us16x08_comp_ratio_ctl,
  1037. .control_id = SND_US16X08_ID_COMP_RATIO,
  1038. .type = USB_MIXER_U8,
  1039. .num_channels = 16,
  1040. .name = "Compressor Ratio",
  1041. },
  1042. { /* Comp attack */
  1043. .kcontrol_new = &snd_us16x08_comp_attack_ctl,
  1044. .control_id = SND_US16X08_ID_COMP_ATTACK,
  1045. .type = USB_MIXER_U8,
  1046. .num_channels = 16,
  1047. .name = "Compressor Attack",
  1048. },
  1049. { /* Comp release */
  1050. .kcontrol_new = &snd_us16x08_comp_release_ctl,
  1051. .control_id = SND_US16X08_ID_COMP_RELEASE,
  1052. .type = USB_MIXER_U8,
  1053. .num_channels = 16,
  1054. .name = "Compressor Release",
  1055. },
  1056. { /* Comp gain */
  1057. .kcontrol_new = &snd_us16x08_comp_gain_ctl,
  1058. .control_id = SND_US16X08_ID_COMP_GAIN,
  1059. .type = USB_MIXER_U8,
  1060. .num_channels = 16,
  1061. .name = "Compressor Volume",
  1062. },
  1063. };
  1064. /* table of channel controls */
  1065. static const struct snd_us16x08_control_params channel_controls[] = {
  1066. { /* Phase */
  1067. .kcontrol_new = &snd_us16x08_ch_boolean_ctl,
  1068. .control_id = SND_US16X08_ID_PHASE,
  1069. .type = USB_MIXER_BOOLEAN,
  1070. .num_channels = 16,
  1071. .name = "Phase Switch",
  1072. .default_val = 0
  1073. },
  1074. { /* Fader */
  1075. .kcontrol_new = &snd_us16x08_ch_int_ctl,
  1076. .control_id = SND_US16X08_ID_FADER,
  1077. .type = USB_MIXER_U8,
  1078. .num_channels = 16,
  1079. .name = "Line Volume",
  1080. .default_val = 127
  1081. },
  1082. { /* Mute */
  1083. .kcontrol_new = &snd_us16x08_ch_boolean_ctl,
  1084. .control_id = SND_US16X08_ID_MUTE,
  1085. .type = USB_MIXER_BOOLEAN,
  1086. .num_channels = 16,
  1087. .name = "Mute Switch",
  1088. .default_val = 0
  1089. },
  1090. { /* Pan */
  1091. .kcontrol_new = &snd_us16x08_pan_int_ctl,
  1092. .control_id = SND_US16X08_ID_PAN,
  1093. .type = USB_MIXER_U16,
  1094. .num_channels = 16,
  1095. .name = "Pan Left-Right Volume",
  1096. .default_val = 127
  1097. },
  1098. };
  1099. /* table of master controls */
  1100. static const struct snd_us16x08_control_params master_controls[] = {
  1101. { /* Master */
  1102. .kcontrol_new = &snd_us16x08_master_ctl,
  1103. .control_id = SND_US16X08_ID_FADER,
  1104. .type = USB_MIXER_U8,
  1105. .num_channels = 16,
  1106. .name = "Master Volume",
  1107. .default_val = 127
  1108. },
  1109. { /* Bypass */
  1110. .kcontrol_new = &snd_us16x08_bus_ctl,
  1111. .control_id = SND_US16X08_ID_BYPASS,
  1112. .type = USB_MIXER_BOOLEAN,
  1113. .num_channels = 16,
  1114. .name = "DSP Bypass Switch",
  1115. .default_val = 0
  1116. },
  1117. { /* Buss out */
  1118. .kcontrol_new = &snd_us16x08_bus_ctl,
  1119. .control_id = SND_US16X08_ID_BUSS_OUT,
  1120. .type = USB_MIXER_BOOLEAN,
  1121. .num_channels = 16,
  1122. .name = "Buss Out Switch",
  1123. .default_val = 0
  1124. },
  1125. { /* Master mute */
  1126. .kcontrol_new = &snd_us16x08_bus_ctl,
  1127. .control_id = SND_US16X08_ID_MUTE,
  1128. .type = USB_MIXER_BOOLEAN,
  1129. .num_channels = 16,
  1130. .name = "Master Mute Switch",
  1131. .default_val = 0
  1132. },
  1133. };
  1134. int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
  1135. {
  1136. int i, j;
  1137. int err;
  1138. struct usb_mixer_elem_info *elem;
  1139. struct snd_us16x08_comp_store *comp_store;
  1140. struct snd_us16x08_meter_store *meter_store;
  1141. struct snd_us16x08_eq_store *eq_store;
  1142. /* just check for non-MIDI interface */
  1143. if (mixer->hostif->desc.bInterfaceNumber == 3) {
  1144. /* add routing control */
  1145. err = add_new_ctl(mixer, &snd_us16x08_route_ctl,
  1146. SND_US16X08_ID_ROUTE, USB_MIXER_U8, 8, "Line Out Route",
  1147. NULL, false, &elem);
  1148. if (err < 0) {
  1149. usb_audio_dbg(mixer->chip,
  1150. "Failed to create route control, err:%d\n",
  1151. err);
  1152. return err;
  1153. }
  1154. for (i = 0; i < 8; i++)
  1155. elem->cache_val[i] = i < 2 ? i : i + 2;
  1156. elem->cached = 0xff;
  1157. /* create compressor mixer elements */
  1158. comp_store = snd_us16x08_create_comp_store();
  1159. if (!comp_store)
  1160. return -ENOMEM;
  1161. /* add master controls */
  1162. for (i = 0; i < ARRAY_SIZE(master_controls); i++) {
  1163. err = add_new_ctl(mixer,
  1164. master_controls[i].kcontrol_new,
  1165. master_controls[i].control_id,
  1166. master_controls[i].type,
  1167. master_controls[i].num_channels,
  1168. master_controls[i].name,
  1169. comp_store,
  1170. i == 0, /* release comp_store only once */
  1171. &elem);
  1172. if (err < 0)
  1173. return err;
  1174. elem->cache_val[0] = master_controls[i].default_val;
  1175. elem->cached = 1;
  1176. }
  1177. /* add channel controls */
  1178. for (i = 0; i < ARRAY_SIZE(channel_controls); i++) {
  1179. err = add_new_ctl(mixer,
  1180. channel_controls[i].kcontrol_new,
  1181. channel_controls[i].control_id,
  1182. channel_controls[i].type,
  1183. channel_controls[i].num_channels,
  1184. channel_controls[i].name,
  1185. comp_store,
  1186. false, &elem);
  1187. if (err < 0)
  1188. return err;
  1189. for (j = 0; j < SND_US16X08_MAX_CHANNELS; j++) {
  1190. elem->cache_val[j] =
  1191. channel_controls[i].default_val;
  1192. }
  1193. elem->cached = 0xffff;
  1194. }
  1195. /* create eq store */
  1196. eq_store = snd_us16x08_create_eq_store();
  1197. if (!eq_store)
  1198. return -ENOMEM;
  1199. /* add EQ controls */
  1200. for (i = 0; i < ARRAY_SIZE(eq_controls); i++) {
  1201. err = add_new_ctl(mixer,
  1202. eq_controls[i].kcontrol_new,
  1203. eq_controls[i].control_id,
  1204. eq_controls[i].type,
  1205. eq_controls[i].num_channels,
  1206. eq_controls[i].name,
  1207. eq_store,
  1208. i == 0, /* release eq_store only once */
  1209. NULL);
  1210. if (err < 0)
  1211. return err;
  1212. }
  1213. /* add compressor controls */
  1214. for (i = 0; i < ARRAY_SIZE(comp_controls); i++) {
  1215. err = add_new_ctl(mixer,
  1216. comp_controls[i].kcontrol_new,
  1217. comp_controls[i].control_id,
  1218. comp_controls[i].type,
  1219. comp_controls[i].num_channels,
  1220. comp_controls[i].name,
  1221. comp_store,
  1222. false, NULL);
  1223. if (err < 0)
  1224. return err;
  1225. }
  1226. /* create meters store */
  1227. meter_store = snd_us16x08_create_meter_store();
  1228. if (!meter_store)
  1229. return -ENOMEM;
  1230. /* meter function 'get' must access to compressor store
  1231. * so place a reference here
  1232. */
  1233. meter_store->comp_store = comp_store;
  1234. err = add_new_ctl(mixer, &snd_us16x08_meter_ctl,
  1235. SND_US16X08_ID_METER, USB_MIXER_U16, 0, "Level Meter",
  1236. meter_store, true, NULL);
  1237. if (err < 0)
  1238. return err;
  1239. }
  1240. return 0;
  1241. }