mixer.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. /*
  2. * (Tentative) USB Audio Driver for ALSA
  3. *
  4. * Mixer control part
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * Many codes borrowed from audio.c by
  9. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  10. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  11. *
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. /*
  29. * TODOs, for both the mixer and the streaming interfaces:
  30. *
  31. * - support for UAC2 effect units
  32. * - support for graphical equalizers
  33. * - RANGE and MEM set commands (UAC2)
  34. * - RANGE and MEM interrupt dispatchers (UAC2)
  35. * - audio channel clustering (UAC2)
  36. * - audio sample rate converter units (UAC2)
  37. * - proper handling of clock multipliers (UAC2)
  38. * - dispatch clock change notifications (UAC2)
  39. * - stop PCM streams which use a clock that became invalid
  40. * - stop PCM streams which use a clock selector that has changed
  41. * - parse available sample rates again when clock sources changed
  42. */
  43. #include <linux/bitops.h>
  44. #include <linux/init.h>
  45. #include <linux/list.h>
  46. #include <linux/slab.h>
  47. #include <linux/string.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/audio.h>
  50. #include <linux/usb/audio-v2.h>
  51. #include <sound/core.h>
  52. #include <sound/control.h>
  53. #include <sound/hwdep.h>
  54. #include <sound/info.h>
  55. #include <sound/tlv.h>
  56. #include "usbaudio.h"
  57. #include "mixer.h"
  58. #include "helper.h"
  59. #include "mixer_quirks.h"
  60. #define MAX_ID_ELEMS 256
  61. struct usb_audio_term {
  62. int id;
  63. int type;
  64. int channels;
  65. unsigned int chconfig;
  66. int name;
  67. };
  68. struct usbmix_name_map;
  69. struct mixer_build {
  70. struct snd_usb_audio *chip;
  71. struct usb_mixer_interface *mixer;
  72. unsigned char *buffer;
  73. unsigned int buflen;
  74. DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
  75. struct usb_audio_term oterm;
  76. const struct usbmix_name_map *map;
  77. const struct usbmix_selector_map *selector_map;
  78. };
  79. enum {
  80. USB_MIXER_BOOLEAN,
  81. USB_MIXER_INV_BOOLEAN,
  82. USB_MIXER_S8,
  83. USB_MIXER_U8,
  84. USB_MIXER_S16,
  85. USB_MIXER_U16,
  86. };
  87. /*E-mu 0202(0404) eXtension Unit(XU) control*/
  88. enum {
  89. USB_XU_CLOCK_RATE = 0xe301,
  90. USB_XU_CLOCK_SOURCE = 0xe302,
  91. USB_XU_DIGITAL_IO_STATUS = 0xe303,
  92. USB_XU_DEVICE_OPTIONS = 0xe304,
  93. USB_XU_DIRECT_MONITORING = 0xe305,
  94. USB_XU_METERING = 0xe306
  95. };
  96. enum {
  97. USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
  98. USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
  99. USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
  100. USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
  101. };
  102. /*
  103. * manual mapping of mixer names
  104. * if the mixer topology is too complicated and the parsed names are
  105. * ambiguous, add the entries in usbmixer_maps.c.
  106. */
  107. #include "mixer_maps.c"
  108. static const struct usbmix_name_map *
  109. find_map(struct mixer_build *state, int unitid, int control)
  110. {
  111. const struct usbmix_name_map *p = state->map;
  112. if (!p)
  113. return NULL;
  114. for (p = state->map; p->id; p++) {
  115. if (p->id == unitid &&
  116. (!control || !p->control || control == p->control))
  117. return p;
  118. }
  119. return NULL;
  120. }
  121. /* get the mapped name if the unit matches */
  122. static int
  123. check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
  124. {
  125. if (!p || !p->name)
  126. return 0;
  127. buflen--;
  128. return strlcpy(buf, p->name, buflen);
  129. }
  130. /* check whether the control should be ignored */
  131. static inline int
  132. check_ignored_ctl(const struct usbmix_name_map *p)
  133. {
  134. if (!p || p->name || p->dB)
  135. return 0;
  136. return 1;
  137. }
  138. /* dB mapping */
  139. static inline void check_mapped_dB(const struct usbmix_name_map *p,
  140. struct usb_mixer_elem_info *cval)
  141. {
  142. if (p && p->dB) {
  143. cval->dBmin = p->dB->min;
  144. cval->dBmax = p->dB->max;
  145. }
  146. }
  147. /* get the mapped selector source name */
  148. static int check_mapped_selector_name(struct mixer_build *state, int unitid,
  149. int index, char *buf, int buflen)
  150. {
  151. const struct usbmix_selector_map *p;
  152. if (! state->selector_map)
  153. return 0;
  154. for (p = state->selector_map; p->id; p++) {
  155. if (p->id == unitid && index < p->count)
  156. return strlcpy(buf, p->names[index], buflen);
  157. }
  158. return 0;
  159. }
  160. /*
  161. * find an audio control unit with the given unit id
  162. */
  163. static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
  164. {
  165. /* we just parse the header */
  166. struct uac_feature_unit_descriptor *hdr = NULL;
  167. while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
  168. USB_DT_CS_INTERFACE)) != NULL) {
  169. if (hdr->bLength >= 4 &&
  170. hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
  171. hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER &&
  172. hdr->bUnitID == unit)
  173. return hdr;
  174. }
  175. return NULL;
  176. }
  177. /*
  178. * copy a string with the given id
  179. */
  180. static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
  181. {
  182. int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
  183. buf[len] = 0;
  184. return len;
  185. }
  186. /*
  187. * convert from the byte/word on usb descriptor to the zero-based integer
  188. */
  189. static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
  190. {
  191. switch (cval->val_type) {
  192. case USB_MIXER_BOOLEAN:
  193. return !!val;
  194. case USB_MIXER_INV_BOOLEAN:
  195. return !val;
  196. case USB_MIXER_U8:
  197. val &= 0xff;
  198. break;
  199. case USB_MIXER_S8:
  200. val &= 0xff;
  201. if (val >= 0x80)
  202. val -= 0x100;
  203. break;
  204. case USB_MIXER_U16:
  205. val &= 0xffff;
  206. break;
  207. case USB_MIXER_S16:
  208. val &= 0xffff;
  209. if (val >= 0x8000)
  210. val -= 0x10000;
  211. break;
  212. }
  213. return val;
  214. }
  215. /*
  216. * convert from the zero-based int to the byte/word for usb descriptor
  217. */
  218. static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
  219. {
  220. switch (cval->val_type) {
  221. case USB_MIXER_BOOLEAN:
  222. return !!val;
  223. case USB_MIXER_INV_BOOLEAN:
  224. return !val;
  225. case USB_MIXER_S8:
  226. case USB_MIXER_U8:
  227. return val & 0xff;
  228. case USB_MIXER_S16:
  229. case USB_MIXER_U16:
  230. return val & 0xffff;
  231. }
  232. return 0; /* not reached */
  233. }
  234. static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
  235. {
  236. if (! cval->res)
  237. cval->res = 1;
  238. if (val < cval->min)
  239. return 0;
  240. else if (val >= cval->max)
  241. return (cval->max - cval->min + cval->res - 1) / cval->res;
  242. else
  243. return (val - cval->min) / cval->res;
  244. }
  245. static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
  246. {
  247. if (val < 0)
  248. return cval->min;
  249. if (! cval->res)
  250. cval->res = 1;
  251. val *= cval->res;
  252. val += cval->min;
  253. if (val > cval->max)
  254. return cval->max;
  255. return val;
  256. }
  257. /*
  258. * retrieve a mixer value
  259. */
  260. static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
  261. {
  262. struct snd_usb_audio *chip = cval->mixer->chip;
  263. unsigned char buf[2];
  264. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  265. int timeout = 10;
  266. while (timeout-- > 0) {
  267. if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
  268. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  269. validx, snd_usb_ctrl_intf(chip) | (cval->id << 8),
  270. buf, val_len, 100) >= val_len) {
  271. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
  272. return 0;
  273. }
  274. }
  275. snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  276. request, validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), cval->val_type);
  277. return -EINVAL;
  278. }
  279. static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
  280. {
  281. struct snd_usb_audio *chip = cval->mixer->chip;
  282. unsigned char buf[2 + 3*sizeof(__u16)]; /* enough space for one range */
  283. unsigned char *val;
  284. int ret, size;
  285. __u8 bRequest;
  286. if (request == UAC_GET_CUR) {
  287. bRequest = UAC2_CS_CUR;
  288. size = sizeof(__u16);
  289. } else {
  290. bRequest = UAC2_CS_RANGE;
  291. size = sizeof(buf);
  292. }
  293. memset(buf, 0, sizeof(buf));
  294. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
  295. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  296. validx, snd_usb_ctrl_intf(chip) | (cval->id << 8),
  297. buf, size, 1000);
  298. if (ret < 0) {
  299. snd_printk(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  300. request, validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), cval->val_type);
  301. return ret;
  302. }
  303. /* FIXME: how should we handle multiple triplets here? */
  304. switch (request) {
  305. case UAC_GET_CUR:
  306. val = buf;
  307. break;
  308. case UAC_GET_MIN:
  309. val = buf + sizeof(__u16);
  310. break;
  311. case UAC_GET_MAX:
  312. val = buf + sizeof(__u16) * 2;
  313. break;
  314. case UAC_GET_RES:
  315. val = buf + sizeof(__u16) * 3;
  316. break;
  317. default:
  318. return -EINVAL;
  319. }
  320. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16)));
  321. return 0;
  322. }
  323. static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
  324. {
  325. return (cval->mixer->protocol == UAC_VERSION_1) ?
  326. get_ctl_value_v1(cval, request, validx, value_ret) :
  327. get_ctl_value_v2(cval, request, validx, value_ret);
  328. }
  329. static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
  330. {
  331. return get_ctl_value(cval, UAC_GET_CUR, validx, value);
  332. }
  333. /* channel = 0: master, 1 = first channel */
  334. static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
  335. int channel, int *value)
  336. {
  337. return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value);
  338. }
  339. static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
  340. int channel, int index, int *value)
  341. {
  342. int err;
  343. if (cval->cached & (1 << channel)) {
  344. *value = cval->cache_val[index];
  345. return 0;
  346. }
  347. err = get_cur_mix_raw(cval, channel, value);
  348. if (err < 0) {
  349. if (!cval->mixer->ignore_ctl_error)
  350. snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n",
  351. cval->control, channel, err);
  352. return err;
  353. }
  354. cval->cached |= 1 << channel;
  355. cval->cache_val[index] = *value;
  356. return 0;
  357. }
  358. /*
  359. * set a mixer value
  360. */
  361. int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
  362. int request, int validx, int value_set)
  363. {
  364. struct snd_usb_audio *chip = cval->mixer->chip;
  365. unsigned char buf[2];
  366. int val_len, timeout = 10;
  367. if (cval->mixer->protocol == UAC_VERSION_1) {
  368. val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  369. } else { /* UAC_VERSION_2 */
  370. /* audio class v2 controls are always 2 bytes in size */
  371. val_len = sizeof(__u16);
  372. /* FIXME */
  373. if (request != UAC_SET_CUR) {
  374. snd_printdd(KERN_WARNING "RANGE setting not yet supported\n");
  375. return -EINVAL;
  376. }
  377. request = UAC2_CS_CUR;
  378. }
  379. value_set = convert_bytes_value(cval, value_set);
  380. buf[0] = value_set & 0xff;
  381. buf[1] = (value_set >> 8) & 0xff;
  382. while (timeout-- > 0)
  383. if (snd_usb_ctl_msg(chip->dev,
  384. usb_sndctrlpipe(chip->dev, 0), request,
  385. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  386. validx, snd_usb_ctrl_intf(chip) | (cval->id << 8),
  387. buf, val_len, 100) >= 0)
  388. return 0;
  389. snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
  390. request, validx, snd_usb_ctrl_intf(chip) | (cval->id << 8), cval->val_type, buf[0], buf[1]);
  391. return -EINVAL;
  392. }
  393. static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
  394. {
  395. return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
  396. }
  397. static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
  398. int index, int value)
  399. {
  400. int err;
  401. unsigned int read_only = (channel == 0) ?
  402. cval->master_readonly :
  403. cval->ch_readonly & (1 << (channel - 1));
  404. if (read_only) {
  405. snd_printdd(KERN_INFO "%s(): channel %d of control %d is read_only\n",
  406. __func__, channel, cval->control);
  407. return 0;
  408. }
  409. err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,
  410. value);
  411. if (err < 0)
  412. return err;
  413. cval->cached |= 1 << channel;
  414. cval->cache_val[index] = value;
  415. return 0;
  416. }
  417. /*
  418. * TLV callback for mixer volume controls
  419. */
  420. static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  421. unsigned int size, unsigned int __user *_tlv)
  422. {
  423. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  424. DECLARE_TLV_DB_MINMAX(scale, 0, 0);
  425. if (size < sizeof(scale))
  426. return -ENOMEM;
  427. scale[2] = cval->dBmin;
  428. scale[3] = cval->dBmax;
  429. if (copy_to_user(_tlv, scale, sizeof(scale)))
  430. return -EFAULT;
  431. return 0;
  432. }
  433. /*
  434. * parser routines begin here...
  435. */
  436. static int parse_audio_unit(struct mixer_build *state, int unitid);
  437. /*
  438. * check if the input/output channel routing is enabled on the given bitmap.
  439. * used for mixer unit parser
  440. */
  441. static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
  442. {
  443. int idx = ich * num_outs + och;
  444. return bmap[idx >> 3] & (0x80 >> (idx & 7));
  445. }
  446. /*
  447. * add an alsa control element
  448. * search and increment the index until an empty slot is found.
  449. *
  450. * if failed, give up and free the control instance.
  451. */
  452. static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
  453. {
  454. struct usb_mixer_elem_info *cval = kctl->private_data;
  455. int err;
  456. while (snd_ctl_find_id(state->chip->card, &kctl->id))
  457. kctl->id.index++;
  458. if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
  459. snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
  460. return err;
  461. }
  462. cval->elem_id = &kctl->id;
  463. cval->next_id_elem = state->mixer->id_elems[cval->id];
  464. state->mixer->id_elems[cval->id] = cval;
  465. return 0;
  466. }
  467. /*
  468. * get a terminal name string
  469. */
  470. static struct iterm_name_combo {
  471. int type;
  472. char *name;
  473. } iterm_names[] = {
  474. { 0x0300, "Output" },
  475. { 0x0301, "Speaker" },
  476. { 0x0302, "Headphone" },
  477. { 0x0303, "HMD Audio" },
  478. { 0x0304, "Desktop Speaker" },
  479. { 0x0305, "Room Speaker" },
  480. { 0x0306, "Com Speaker" },
  481. { 0x0307, "LFE" },
  482. { 0x0600, "External In" },
  483. { 0x0601, "Analog In" },
  484. { 0x0602, "Digital In" },
  485. { 0x0603, "Line" },
  486. { 0x0604, "Legacy In" },
  487. { 0x0605, "IEC958 In" },
  488. { 0x0606, "1394 DA Stream" },
  489. { 0x0607, "1394 DV Stream" },
  490. { 0x0700, "Embedded" },
  491. { 0x0701, "Noise Source" },
  492. { 0x0702, "Equalization Noise" },
  493. { 0x0703, "CD" },
  494. { 0x0704, "DAT" },
  495. { 0x0705, "DCC" },
  496. { 0x0706, "MiniDisk" },
  497. { 0x0707, "Analog Tape" },
  498. { 0x0708, "Phonograph" },
  499. { 0x0709, "VCR Audio" },
  500. { 0x070a, "Video Disk Audio" },
  501. { 0x070b, "DVD Audio" },
  502. { 0x070c, "TV Tuner Audio" },
  503. { 0x070d, "Satellite Rec Audio" },
  504. { 0x070e, "Cable Tuner Audio" },
  505. { 0x070f, "DSS Audio" },
  506. { 0x0710, "Radio Receiver" },
  507. { 0x0711, "Radio Transmitter" },
  508. { 0x0712, "Multi-Track Recorder" },
  509. { 0x0713, "Synthesizer" },
  510. { 0 },
  511. };
  512. static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
  513. unsigned char *name, int maxlen, int term_only)
  514. {
  515. struct iterm_name_combo *names;
  516. if (iterm->name)
  517. return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
  518. /* virtual type - not a real terminal */
  519. if (iterm->type >> 16) {
  520. if (term_only)
  521. return 0;
  522. switch (iterm->type >> 16) {
  523. case UAC_SELECTOR_UNIT:
  524. strcpy(name, "Selector"); return 8;
  525. case UAC1_PROCESSING_UNIT:
  526. strcpy(name, "Process Unit"); return 12;
  527. case UAC1_EXTENSION_UNIT:
  528. strcpy(name, "Ext Unit"); return 8;
  529. case UAC_MIXER_UNIT:
  530. strcpy(name, "Mixer"); return 5;
  531. default:
  532. return sprintf(name, "Unit %d", iterm->id);
  533. }
  534. }
  535. switch (iterm->type & 0xff00) {
  536. case 0x0100:
  537. strcpy(name, "PCM"); return 3;
  538. case 0x0200:
  539. strcpy(name, "Mic"); return 3;
  540. case 0x0400:
  541. strcpy(name, "Headset"); return 7;
  542. case 0x0500:
  543. strcpy(name, "Phone"); return 5;
  544. }
  545. for (names = iterm_names; names->type; names++)
  546. if (names->type == iterm->type) {
  547. strcpy(name, names->name);
  548. return strlen(names->name);
  549. }
  550. return 0;
  551. }
  552. /*
  553. * parse the source unit recursively until it reaches to a terminal
  554. * or a branched unit.
  555. */
  556. static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
  557. {
  558. int err;
  559. void *p1;
  560. memset(term, 0, sizeof(*term));
  561. while ((p1 = find_audio_control_unit(state, id)) != NULL) {
  562. unsigned char *hdr = p1;
  563. term->id = id;
  564. switch (hdr[2]) {
  565. case UAC_INPUT_TERMINAL:
  566. if (state->mixer->protocol == UAC_VERSION_1) {
  567. struct uac_input_terminal_descriptor *d = p1;
  568. term->type = le16_to_cpu(d->wTerminalType);
  569. term->channels = d->bNrChannels;
  570. term->chconfig = le16_to_cpu(d->wChannelConfig);
  571. term->name = d->iTerminal;
  572. } else { /* UAC_VERSION_2 */
  573. struct uac2_input_terminal_descriptor *d = p1;
  574. term->type = le16_to_cpu(d->wTerminalType);
  575. term->channels = d->bNrChannels;
  576. term->chconfig = le32_to_cpu(d->bmChannelConfig);
  577. term->name = d->iTerminal;
  578. /* call recursively to get the clock selectors */
  579. err = check_input_term(state, d->bCSourceID, term);
  580. if (err < 0)
  581. return err;
  582. }
  583. return 0;
  584. case UAC_FEATURE_UNIT: {
  585. /* the header is the same for v1 and v2 */
  586. struct uac_feature_unit_descriptor *d = p1;
  587. id = d->bSourceID;
  588. break; /* continue to parse */
  589. }
  590. case UAC_MIXER_UNIT: {
  591. struct uac_mixer_unit_descriptor *d = p1;
  592. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  593. term->channels = uac_mixer_unit_bNrChannels(d);
  594. term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
  595. term->name = uac_mixer_unit_iMixer(d);
  596. return 0;
  597. }
  598. case UAC_SELECTOR_UNIT:
  599. case UAC2_CLOCK_SELECTOR: {
  600. struct uac_selector_unit_descriptor *d = p1;
  601. /* call recursively to retrieve the channel info */
  602. if (check_input_term(state, d->baSourceID[0], term) < 0)
  603. return -ENODEV;
  604. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  605. term->id = id;
  606. term->name = uac_selector_unit_iSelector(d);
  607. return 0;
  608. }
  609. case UAC1_PROCESSING_UNIT:
  610. case UAC1_EXTENSION_UNIT: {
  611. struct uac_processing_unit_descriptor *d = p1;
  612. if (d->bNrInPins) {
  613. id = d->baSourceID[0];
  614. break; /* continue to parse */
  615. }
  616. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  617. term->channels = uac_processing_unit_bNrChannels(d);
  618. term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol);
  619. term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol);
  620. return 0;
  621. }
  622. case UAC2_CLOCK_SOURCE: {
  623. struct uac_clock_source_descriptor *d = p1;
  624. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  625. term->id = id;
  626. term->name = d->iClockSource;
  627. return 0;
  628. }
  629. default:
  630. return -ENODEV;
  631. }
  632. }
  633. return -ENODEV;
  634. }
  635. /*
  636. * Feature Unit
  637. */
  638. /* feature unit control information */
  639. struct usb_feature_control_info {
  640. const char *name;
  641. unsigned int type; /* control type (mute, volume, etc.) */
  642. };
  643. static struct usb_feature_control_info audio_feature_info[] = {
  644. { "Mute", USB_MIXER_INV_BOOLEAN },
  645. { "Volume", USB_MIXER_S16 },
  646. { "Tone Control - Bass", USB_MIXER_S8 },
  647. { "Tone Control - Mid", USB_MIXER_S8 },
  648. { "Tone Control - Treble", USB_MIXER_S8 },
  649. { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
  650. { "Auto Gain Control", USB_MIXER_BOOLEAN },
  651. { "Delay Control", USB_MIXER_U16 },
  652. { "Bass Boost", USB_MIXER_BOOLEAN },
  653. { "Loudness", USB_MIXER_BOOLEAN },
  654. /* UAC2 specific */
  655. { "Input Gain Control", USB_MIXER_U16 },
  656. { "Input Gain Pad Control", USB_MIXER_BOOLEAN },
  657. { "Phase Inverter Control", USB_MIXER_BOOLEAN },
  658. };
  659. /* private_free callback */
  660. static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
  661. {
  662. kfree(kctl->private_data);
  663. kctl->private_data = NULL;
  664. }
  665. /*
  666. * interface to ALSA control for feature/mixer units
  667. */
  668. /*
  669. * retrieve the minimum and maximum values for the specified control
  670. */
  671. static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
  672. {
  673. struct snd_usb_audio *chip = cval->mixer->chip;
  674. /* for failsafe */
  675. cval->min = default_min;
  676. cval->max = cval->min + 1;
  677. cval->res = 1;
  678. cval->dBmin = cval->dBmax = 0;
  679. if (cval->val_type == USB_MIXER_BOOLEAN ||
  680. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  681. cval->initialized = 1;
  682. } else {
  683. int minchn = 0;
  684. if (cval->cmask) {
  685. int i;
  686. for (i = 0; i < MAX_CHANNELS; i++)
  687. if (cval->cmask & (1 << i)) {
  688. minchn = i + 1;
  689. break;
  690. }
  691. }
  692. if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
  693. get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
  694. snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
  695. cval->id, snd_usb_ctrl_intf(chip), cval->control, cval->id);
  696. return -EINVAL;
  697. }
  698. if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
  699. cval->res = 1;
  700. } else {
  701. int last_valid_res = cval->res;
  702. while (cval->res > 1) {
  703. if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
  704. (cval->control << 8) | minchn, cval->res / 2) < 0)
  705. break;
  706. cval->res /= 2;
  707. }
  708. if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
  709. cval->res = last_valid_res;
  710. }
  711. if (cval->res == 0)
  712. cval->res = 1;
  713. /* Additional checks for the proper resolution
  714. *
  715. * Some devices report smaller resolutions than actually
  716. * reacting. They don't return errors but simply clip
  717. * to the lower aligned value.
  718. */
  719. if (cval->min + cval->res < cval->max) {
  720. int last_valid_res = cval->res;
  721. int saved, test, check;
  722. get_cur_mix_raw(cval, minchn, &saved);
  723. for (;;) {
  724. test = saved;
  725. if (test < cval->max)
  726. test += cval->res;
  727. else
  728. test -= cval->res;
  729. if (test < cval->min || test > cval->max ||
  730. set_cur_mix_value(cval, minchn, 0, test) ||
  731. get_cur_mix_raw(cval, minchn, &check)) {
  732. cval->res = last_valid_res;
  733. break;
  734. }
  735. if (test == check)
  736. break;
  737. cval->res *= 2;
  738. }
  739. set_cur_mix_value(cval, minchn, 0, saved);
  740. }
  741. cval->initialized = 1;
  742. }
  743. /* USB descriptions contain the dB scale in 1/256 dB unit
  744. * while ALSA TLV contains in 1/100 dB unit
  745. */
  746. cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
  747. cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
  748. if (cval->dBmin > cval->dBmax) {
  749. /* something is wrong; assume it's either from/to 0dB */
  750. if (cval->dBmin < 0)
  751. cval->dBmax = 0;
  752. else if (cval->dBmin > 0)
  753. cval->dBmin = 0;
  754. if (cval->dBmin > cval->dBmax) {
  755. /* totally crap, return an error */
  756. return -EINVAL;
  757. }
  758. }
  759. return 0;
  760. }
  761. /* get a feature/mixer unit info */
  762. static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  763. {
  764. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  765. if (cval->val_type == USB_MIXER_BOOLEAN ||
  766. cval->val_type == USB_MIXER_INV_BOOLEAN)
  767. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  768. else
  769. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  770. uinfo->count = cval->channels;
  771. if (cval->val_type == USB_MIXER_BOOLEAN ||
  772. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  773. uinfo->value.integer.min = 0;
  774. uinfo->value.integer.max = 1;
  775. } else {
  776. if (! cval->initialized)
  777. get_min_max(cval, 0);
  778. uinfo->value.integer.min = 0;
  779. uinfo->value.integer.max =
  780. (cval->max - cval->min + cval->res - 1) / cval->res;
  781. }
  782. return 0;
  783. }
  784. /* get the current value from feature/mixer unit */
  785. static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  786. {
  787. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  788. int c, cnt, val, err;
  789. ucontrol->value.integer.value[0] = cval->min;
  790. if (cval->cmask) {
  791. cnt = 0;
  792. for (c = 0; c < MAX_CHANNELS; c++) {
  793. if (!(cval->cmask & (1 << c)))
  794. continue;
  795. err = get_cur_mix_value(cval, c + 1, cnt, &val);
  796. if (err < 0)
  797. return cval->mixer->ignore_ctl_error ? 0 : err;
  798. val = get_relative_value(cval, val);
  799. ucontrol->value.integer.value[cnt] = val;
  800. cnt++;
  801. }
  802. return 0;
  803. } else {
  804. /* master channel */
  805. err = get_cur_mix_value(cval, 0, 0, &val);
  806. if (err < 0)
  807. return cval->mixer->ignore_ctl_error ? 0 : err;
  808. val = get_relative_value(cval, val);
  809. ucontrol->value.integer.value[0] = val;
  810. }
  811. return 0;
  812. }
  813. /* put the current value to feature/mixer unit */
  814. static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  815. {
  816. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  817. int c, cnt, val, oval, err;
  818. int changed = 0;
  819. if (cval->cmask) {
  820. cnt = 0;
  821. for (c = 0; c < MAX_CHANNELS; c++) {
  822. if (!(cval->cmask & (1 << c)))
  823. continue;
  824. err = get_cur_mix_value(cval, c + 1, cnt, &oval);
  825. if (err < 0)
  826. return cval->mixer->ignore_ctl_error ? 0 : err;
  827. val = ucontrol->value.integer.value[cnt];
  828. val = get_abs_value(cval, val);
  829. if (oval != val) {
  830. set_cur_mix_value(cval, c + 1, cnt, val);
  831. changed = 1;
  832. }
  833. cnt++;
  834. }
  835. } else {
  836. /* master channel */
  837. err = get_cur_mix_value(cval, 0, 0, &oval);
  838. if (err < 0)
  839. return cval->mixer->ignore_ctl_error ? 0 : err;
  840. val = ucontrol->value.integer.value[0];
  841. val = get_abs_value(cval, val);
  842. if (val != oval) {
  843. set_cur_mix_value(cval, 0, 0, val);
  844. changed = 1;
  845. }
  846. }
  847. return changed;
  848. }
  849. static struct snd_kcontrol_new usb_feature_unit_ctl = {
  850. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  851. .name = "", /* will be filled later manually */
  852. .info = mixer_ctl_feature_info,
  853. .get = mixer_ctl_feature_get,
  854. .put = mixer_ctl_feature_put,
  855. };
  856. /* the read-only variant */
  857. static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
  858. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  859. .name = "", /* will be filled later manually */
  860. .info = mixer_ctl_feature_info,
  861. .get = mixer_ctl_feature_get,
  862. .put = NULL,
  863. };
  864. /*
  865. * build a feature control
  866. */
  867. static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
  868. {
  869. return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
  870. }
  871. static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
  872. unsigned int ctl_mask, int control,
  873. struct usb_audio_term *iterm, int unitid,
  874. int readonly_mask)
  875. {
  876. struct uac_feature_unit_descriptor *desc = raw_desc;
  877. unsigned int len = 0;
  878. int mapped_name = 0;
  879. int nameid = uac_feature_unit_iFeature(desc);
  880. struct snd_kcontrol *kctl;
  881. struct usb_mixer_elem_info *cval;
  882. const struct usbmix_name_map *map;
  883. control++; /* change from zero-based to 1-based value */
  884. if (control == UAC_FU_GRAPHIC_EQUALIZER) {
  885. /* FIXME: not supported yet */
  886. return;
  887. }
  888. map = find_map(state, unitid, control);
  889. if (check_ignored_ctl(map))
  890. return;
  891. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  892. if (! cval) {
  893. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  894. return;
  895. }
  896. cval->mixer = state->mixer;
  897. cval->id = unitid;
  898. cval->control = control;
  899. cval->cmask = ctl_mask;
  900. cval->val_type = audio_feature_info[control-1].type;
  901. if (ctl_mask == 0) {
  902. cval->channels = 1; /* master channel */
  903. cval->master_readonly = readonly_mask;
  904. } else {
  905. int i, c = 0;
  906. for (i = 0; i < 16; i++)
  907. if (ctl_mask & (1 << i))
  908. c++;
  909. cval->channels = c;
  910. cval->ch_readonly = readonly_mask;
  911. }
  912. /* get min/max values */
  913. get_min_max(cval, 0);
  914. /* if all channels in the mask are marked read-only, make the control
  915. * read-only. set_cur_mix_value() will check the mask again and won't
  916. * issue write commands to read-only channels. */
  917. if (cval->channels == readonly_mask)
  918. kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
  919. else
  920. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  921. if (! kctl) {
  922. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  923. kfree(cval);
  924. return;
  925. }
  926. kctl->private_free = usb_mixer_elem_free;
  927. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  928. mapped_name = len != 0;
  929. if (! len && nameid)
  930. len = snd_usb_copy_string_desc(state, nameid,
  931. kctl->id.name, sizeof(kctl->id.name));
  932. switch (control) {
  933. case UAC_FU_MUTE:
  934. case UAC_FU_VOLUME:
  935. /* determine the control name. the rule is:
  936. * - if a name id is given in descriptor, use it.
  937. * - if the connected input can be determined, then use the name
  938. * of terminal type.
  939. * - if the connected output can be determined, use it.
  940. * - otherwise, anonymous name.
  941. */
  942. if (! len) {
  943. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
  944. if (! len)
  945. len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
  946. if (! len)
  947. len = snprintf(kctl->id.name, sizeof(kctl->id.name),
  948. "Feature %d", unitid);
  949. }
  950. /* determine the stream direction:
  951. * if the connected output is USB stream, then it's likely a
  952. * capture stream. otherwise it should be playback (hopefully :)
  953. */
  954. if (! mapped_name && ! (state->oterm.type >> 16)) {
  955. if ((state->oterm.type & 0xff00) == 0x0100) {
  956. len = append_ctl_name(kctl, " Capture");
  957. } else {
  958. len = append_ctl_name(kctl, " Playback");
  959. }
  960. }
  961. append_ctl_name(kctl, control == UAC_FU_MUTE ?
  962. " Switch" : " Volume");
  963. if (control == UAC_FU_VOLUME) {
  964. kctl->tlv.c = mixer_vol_tlv;
  965. kctl->vd[0].access |=
  966. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  967. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  968. check_mapped_dB(map, cval);
  969. }
  970. break;
  971. default:
  972. if (! len)
  973. strlcpy(kctl->id.name, audio_feature_info[control-1].name,
  974. sizeof(kctl->id.name));
  975. break;
  976. }
  977. /* volume control quirks */
  978. switch (state->chip->usb_id) {
  979. case USB_ID(0x0471, 0x0101):
  980. case USB_ID(0x0471, 0x0104):
  981. case USB_ID(0x0471, 0x0105):
  982. case USB_ID(0x0672, 0x1041):
  983. /* quirk for UDA1321/N101.
  984. * note that detection between firmware 2.1.1.7 (N101)
  985. * and later 2.1.1.21 is not very clear from datasheets.
  986. * I hope that the min value is -15360 for newer firmware --jk
  987. */
  988. if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
  989. cval->min == -15616) {
  990. snd_printk(KERN_INFO
  991. "set volume quirk for UDA1321/N101 chip\n");
  992. cval->max = -256;
  993. }
  994. break;
  995. case USB_ID(0x046d, 0x09a4):
  996. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  997. snd_printk(KERN_INFO
  998. "set volume quirk for QuickCam E3500\n");
  999. cval->min = 6080;
  1000. cval->max = 8768;
  1001. cval->res = 192;
  1002. }
  1003. break;
  1004. case USB_ID(0x046d, 0x0809):
  1005. case USB_ID(0x046d, 0x0991):
  1006. /* Most audio usb devices lie about volume resolution.
  1007. * Most Logitech webcams have res = 384.
  1008. * Proboly there is some logitech magic behind this number --fishor
  1009. */
  1010. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  1011. snd_printk(KERN_INFO
  1012. "set resolution quirk: cval->res = 384\n");
  1013. cval->res = 384;
  1014. }
  1015. break;
  1016. }
  1017. snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
  1018. cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
  1019. add_control_to_empty(state, kctl);
  1020. }
  1021. /*
  1022. * parse a feature unit
  1023. *
  1024. * most of controlls are defined here.
  1025. */
  1026. static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
  1027. {
  1028. int channels, i, j;
  1029. struct usb_audio_term iterm;
  1030. unsigned int master_bits, first_ch_bits;
  1031. int err, csize;
  1032. struct uac_feature_unit_descriptor *hdr = _ftr;
  1033. __u8 *bmaControls;
  1034. if (state->mixer->protocol == UAC_VERSION_1) {
  1035. csize = hdr->bControlSize;
  1036. channels = (hdr->bLength - 7) / csize - 1;
  1037. bmaControls = hdr->bmaControls;
  1038. } else {
  1039. struct uac2_feature_unit_descriptor *ftr = _ftr;
  1040. csize = 4;
  1041. channels = (hdr->bLength - 6) / 4 - 1;
  1042. bmaControls = ftr->bmaControls;
  1043. }
  1044. if (hdr->bLength < 7 || !csize || hdr->bLength < 7 + csize) {
  1045. snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid);
  1046. return -EINVAL;
  1047. }
  1048. /* parse the source unit */
  1049. if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
  1050. return err;
  1051. /* determine the input source type and name */
  1052. if (check_input_term(state, hdr->bSourceID, &iterm) < 0)
  1053. return -EINVAL;
  1054. master_bits = snd_usb_combine_bytes(bmaControls, csize);
  1055. /* master configuration quirks */
  1056. switch (state->chip->usb_id) {
  1057. case USB_ID(0x08bb, 0x2702):
  1058. snd_printk(KERN_INFO
  1059. "usbmixer: master volume quirk for PCM2702 chip\n");
  1060. /* disable non-functional volume control */
  1061. master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
  1062. break;
  1063. }
  1064. if (channels > 0)
  1065. first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
  1066. else
  1067. first_ch_bits = 0;
  1068. if (state->mixer->protocol == UAC_VERSION_1) {
  1069. /* check all control types */
  1070. for (i = 0; i < 10; i++) {
  1071. unsigned int ch_bits = 0;
  1072. for (j = 0; j < channels; j++) {
  1073. unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
  1074. if (mask & (1 << i))
  1075. ch_bits |= (1 << j);
  1076. }
  1077. /* audio class v1 controls are never read-only */
  1078. if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
  1079. build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, 0);
  1080. if (master_bits & (1 << i))
  1081. build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0);
  1082. }
  1083. } else { /* UAC_VERSION_2 */
  1084. for (i = 0; i < 30/2; i++) {
  1085. unsigned int ch_bits = 0;
  1086. unsigned int ch_read_only = 0;
  1087. for (j = 0; j < channels; j++) {
  1088. unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
  1089. if (uac2_control_is_readable(mask, i)) {
  1090. ch_bits |= (1 << j);
  1091. if (!uac2_control_is_writeable(mask, i))
  1092. ch_read_only |= (1 << j);
  1093. }
  1094. }
  1095. /* NOTE: build_feature_ctl() will mark the control read-only if all channels
  1096. * are marked read-only in the descriptors. Otherwise, the control will be
  1097. * reported as writeable, but the driver will not actually issue a write
  1098. * command for read-only channels */
  1099. if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
  1100. build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, ch_read_only);
  1101. if (uac2_control_is_readable(master_bits, i))
  1102. build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
  1103. !uac2_control_is_writeable(master_bits, i));
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. /*
  1109. * Mixer Unit
  1110. */
  1111. /*
  1112. * build a mixer unit control
  1113. *
  1114. * the callbacks are identical with feature unit.
  1115. * input channel number (zero based) is given in control field instead.
  1116. */
  1117. static void build_mixer_unit_ctl(struct mixer_build *state,
  1118. struct uac_mixer_unit_descriptor *desc,
  1119. int in_pin, int in_ch, int unitid,
  1120. struct usb_audio_term *iterm)
  1121. {
  1122. struct usb_mixer_elem_info *cval;
  1123. unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
  1124. unsigned int i, len;
  1125. struct snd_kcontrol *kctl;
  1126. const struct usbmix_name_map *map;
  1127. map = find_map(state, unitid, 0);
  1128. if (check_ignored_ctl(map))
  1129. return;
  1130. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1131. if (! cval)
  1132. return;
  1133. cval->mixer = state->mixer;
  1134. cval->id = unitid;
  1135. cval->control = in_ch + 1; /* based on 1 */
  1136. cval->val_type = USB_MIXER_S16;
  1137. for (i = 0; i < num_outs; i++) {
  1138. if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), in_ch, i, num_outs)) {
  1139. cval->cmask |= (1 << i);
  1140. cval->channels++;
  1141. }
  1142. }
  1143. /* get min/max values */
  1144. get_min_max(cval, 0);
  1145. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1146. if (! kctl) {
  1147. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1148. kfree(cval);
  1149. return;
  1150. }
  1151. kctl->private_free = usb_mixer_elem_free;
  1152. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1153. if (! len)
  1154. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
  1155. if (! len)
  1156. len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
  1157. append_ctl_name(kctl, " Volume");
  1158. snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
  1159. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1160. add_control_to_empty(state, kctl);
  1161. }
  1162. /*
  1163. * parse a mixer unit
  1164. */
  1165. static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1166. {
  1167. struct uac_mixer_unit_descriptor *desc = raw_desc;
  1168. struct usb_audio_term iterm;
  1169. int input_pins, num_ins, num_outs;
  1170. int pin, ich, err;
  1171. if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) || ! (num_outs = uac_mixer_unit_bNrChannels(desc))) {
  1172. snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
  1173. return -EINVAL;
  1174. }
  1175. /* no bmControls field (e.g. Maya44) -> ignore */
  1176. if (desc->bLength <= 10 + input_pins) {
  1177. snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
  1178. return 0;
  1179. }
  1180. num_ins = 0;
  1181. ich = 0;
  1182. for (pin = 0; pin < input_pins; pin++) {
  1183. err = parse_audio_unit(state, desc->baSourceID[pin]);
  1184. if (err < 0)
  1185. return err;
  1186. err = check_input_term(state, desc->baSourceID[pin], &iterm);
  1187. if (err < 0)
  1188. return err;
  1189. num_ins += iterm.channels;
  1190. for (; ich < num_ins; ++ich) {
  1191. int och, ich_has_controls = 0;
  1192. for (och = 0; och < num_outs; ++och) {
  1193. if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol),
  1194. ich, och, num_outs)) {
  1195. ich_has_controls = 1;
  1196. break;
  1197. }
  1198. }
  1199. if (ich_has_controls)
  1200. build_mixer_unit_ctl(state, desc, pin, ich,
  1201. unitid, &iterm);
  1202. }
  1203. }
  1204. return 0;
  1205. }
  1206. /*
  1207. * Processing Unit / Extension Unit
  1208. */
  1209. /* get callback for processing/extension unit */
  1210. static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1211. {
  1212. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1213. int err, val;
  1214. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1215. if (err < 0 && cval->mixer->ignore_ctl_error) {
  1216. ucontrol->value.integer.value[0] = cval->min;
  1217. return 0;
  1218. }
  1219. if (err < 0)
  1220. return err;
  1221. val = get_relative_value(cval, val);
  1222. ucontrol->value.integer.value[0] = val;
  1223. return 0;
  1224. }
  1225. /* put callback for processing/extension unit */
  1226. static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1227. {
  1228. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1229. int val, oval, err;
  1230. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1231. if (err < 0) {
  1232. if (cval->mixer->ignore_ctl_error)
  1233. return 0;
  1234. return err;
  1235. }
  1236. val = ucontrol->value.integer.value[0];
  1237. val = get_abs_value(cval, val);
  1238. if (val != oval) {
  1239. set_cur_ctl_value(cval, cval->control << 8, val);
  1240. return 1;
  1241. }
  1242. return 0;
  1243. }
  1244. /* alsa control interface for processing/extension unit */
  1245. static struct snd_kcontrol_new mixer_procunit_ctl = {
  1246. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1247. .name = "", /* will be filled later */
  1248. .info = mixer_ctl_feature_info,
  1249. .get = mixer_ctl_procunit_get,
  1250. .put = mixer_ctl_procunit_put,
  1251. };
  1252. /*
  1253. * predefined data for processing units
  1254. */
  1255. struct procunit_value_info {
  1256. int control;
  1257. char *suffix;
  1258. int val_type;
  1259. int min_value;
  1260. };
  1261. struct procunit_info {
  1262. int type;
  1263. char *name;
  1264. struct procunit_value_info *values;
  1265. };
  1266. static struct procunit_value_info updown_proc_info[] = {
  1267. { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1268. { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1269. { 0 }
  1270. };
  1271. static struct procunit_value_info prologic_proc_info[] = {
  1272. { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1273. { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1274. { 0 }
  1275. };
  1276. static struct procunit_value_info threed_enh_proc_info[] = {
  1277. { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1278. { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
  1279. { 0 }
  1280. };
  1281. static struct procunit_value_info reverb_proc_info[] = {
  1282. { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1283. { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
  1284. { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
  1285. { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
  1286. { 0 }
  1287. };
  1288. static struct procunit_value_info chorus_proc_info[] = {
  1289. { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1290. { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
  1291. { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
  1292. { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
  1293. { 0 }
  1294. };
  1295. static struct procunit_value_info dcr_proc_info[] = {
  1296. { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1297. { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
  1298. { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
  1299. { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
  1300. { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
  1301. { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
  1302. { 0 }
  1303. };
  1304. static struct procunit_info procunits[] = {
  1305. { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
  1306. { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
  1307. { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
  1308. { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
  1309. { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
  1310. { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
  1311. { 0 },
  1312. };
  1313. /*
  1314. * predefined data for extension units
  1315. */
  1316. static struct procunit_value_info clock_rate_xu_info[] = {
  1317. { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
  1318. { 0 }
  1319. };
  1320. static struct procunit_value_info clock_source_xu_info[] = {
  1321. { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
  1322. { 0 }
  1323. };
  1324. static struct procunit_value_info spdif_format_xu_info[] = {
  1325. { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
  1326. { 0 }
  1327. };
  1328. static struct procunit_value_info soft_limit_xu_info[] = {
  1329. { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
  1330. { 0 }
  1331. };
  1332. static struct procunit_info extunits[] = {
  1333. { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
  1334. { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
  1335. { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
  1336. { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
  1337. { 0 }
  1338. };
  1339. /*
  1340. * build a processing/extension unit
  1341. */
  1342. static int build_audio_procunit(struct mixer_build *state, int unitid, void *raw_desc, struct procunit_info *list, char *name)
  1343. {
  1344. struct uac_processing_unit_descriptor *desc = raw_desc;
  1345. int num_ins = desc->bNrInPins;
  1346. struct usb_mixer_elem_info *cval;
  1347. struct snd_kcontrol *kctl;
  1348. int i, err, nameid, type, len;
  1349. struct procunit_info *info;
  1350. struct procunit_value_info *valinfo;
  1351. const struct usbmix_name_map *map;
  1352. static struct procunit_value_info default_value_info[] = {
  1353. { 0x01, "Switch", USB_MIXER_BOOLEAN },
  1354. { 0 }
  1355. };
  1356. static struct procunit_info default_info = {
  1357. 0, NULL, default_value_info
  1358. };
  1359. if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
  1360. desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
  1361. snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
  1362. return -EINVAL;
  1363. }
  1364. for (i = 0; i < num_ins; i++) {
  1365. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1366. return err;
  1367. }
  1368. type = le16_to_cpu(desc->wProcessType);
  1369. for (info = list; info && info->type; info++)
  1370. if (info->type == type)
  1371. break;
  1372. if (! info || ! info->type)
  1373. info = &default_info;
  1374. for (valinfo = info->values; valinfo->control; valinfo++) {
  1375. __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
  1376. if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
  1377. continue;
  1378. map = find_map(state, unitid, valinfo->control);
  1379. if (check_ignored_ctl(map))
  1380. continue;
  1381. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1382. if (! cval) {
  1383. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1384. return -ENOMEM;
  1385. }
  1386. cval->mixer = state->mixer;
  1387. cval->id = unitid;
  1388. cval->control = valinfo->control;
  1389. cval->val_type = valinfo->val_type;
  1390. cval->channels = 1;
  1391. /* get min/max values */
  1392. if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
  1393. __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
  1394. /* FIXME: hard-coded */
  1395. cval->min = 1;
  1396. cval->max = control_spec[0];
  1397. cval->res = 1;
  1398. cval->initialized = 1;
  1399. } else {
  1400. if (type == USB_XU_CLOCK_RATE) {
  1401. /* E-Mu USB 0404/0202/TrackerPre
  1402. * samplerate control quirk
  1403. */
  1404. cval->min = 0;
  1405. cval->max = 5;
  1406. cval->res = 1;
  1407. cval->initialized = 1;
  1408. } else
  1409. get_min_max(cval, valinfo->min_value);
  1410. }
  1411. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  1412. if (! kctl) {
  1413. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1414. kfree(cval);
  1415. return -ENOMEM;
  1416. }
  1417. kctl->private_free = usb_mixer_elem_free;
  1418. if (check_mapped_name(map, kctl->id.name,
  1419. sizeof(kctl->id.name)))
  1420. /* nothing */ ;
  1421. else if (info->name)
  1422. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  1423. else {
  1424. nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
  1425. len = 0;
  1426. if (nameid)
  1427. len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1428. if (! len)
  1429. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  1430. }
  1431. append_ctl_name(kctl, " ");
  1432. append_ctl_name(kctl, valinfo->suffix);
  1433. snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
  1434. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1435. if ((err = add_control_to_empty(state, kctl)) < 0)
  1436. return err;
  1437. }
  1438. return 0;
  1439. }
  1440. static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1441. {
  1442. return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit");
  1443. }
  1444. static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1445. {
  1446. /* Note that we parse extension units with processing unit descriptors.
  1447. * That's ok as the layout is the same */
  1448. return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit");
  1449. }
  1450. /*
  1451. * Selector Unit
  1452. */
  1453. /* info callback for selector unit
  1454. * use an enumerator type for routing
  1455. */
  1456. static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1457. {
  1458. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1459. char **itemlist = (char **)kcontrol->private_value;
  1460. if (snd_BUG_ON(!itemlist))
  1461. return -EINVAL;
  1462. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1463. uinfo->count = 1;
  1464. uinfo->value.enumerated.items = cval->max;
  1465. if ((int)uinfo->value.enumerated.item >= cval->max)
  1466. uinfo->value.enumerated.item = cval->max - 1;
  1467. strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
  1468. return 0;
  1469. }
  1470. /* get callback for selector unit */
  1471. static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1472. {
  1473. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1474. int val, err;
  1475. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1476. if (err < 0) {
  1477. if (cval->mixer->ignore_ctl_error) {
  1478. ucontrol->value.enumerated.item[0] = 0;
  1479. return 0;
  1480. }
  1481. return err;
  1482. }
  1483. val = get_relative_value(cval, val);
  1484. ucontrol->value.enumerated.item[0] = val;
  1485. return 0;
  1486. }
  1487. /* put callback for selector unit */
  1488. static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1489. {
  1490. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1491. int val, oval, err;
  1492. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1493. if (err < 0) {
  1494. if (cval->mixer->ignore_ctl_error)
  1495. return 0;
  1496. return err;
  1497. }
  1498. val = ucontrol->value.enumerated.item[0];
  1499. val = get_abs_value(cval, val);
  1500. if (val != oval) {
  1501. set_cur_ctl_value(cval, cval->control << 8, val);
  1502. return 1;
  1503. }
  1504. return 0;
  1505. }
  1506. /* alsa control interface for selector unit */
  1507. static struct snd_kcontrol_new mixer_selectunit_ctl = {
  1508. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1509. .name = "", /* will be filled later */
  1510. .info = mixer_ctl_selector_info,
  1511. .get = mixer_ctl_selector_get,
  1512. .put = mixer_ctl_selector_put,
  1513. };
  1514. /* private free callback.
  1515. * free both private_data and private_value
  1516. */
  1517. static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
  1518. {
  1519. int i, num_ins = 0;
  1520. if (kctl->private_data) {
  1521. struct usb_mixer_elem_info *cval = kctl->private_data;
  1522. num_ins = cval->max;
  1523. kfree(cval);
  1524. kctl->private_data = NULL;
  1525. }
  1526. if (kctl->private_value) {
  1527. char **itemlist = (char **)kctl->private_value;
  1528. for (i = 0; i < num_ins; i++)
  1529. kfree(itemlist[i]);
  1530. kfree(itemlist);
  1531. kctl->private_value = 0;
  1532. }
  1533. }
  1534. /*
  1535. * parse a selector unit
  1536. */
  1537. static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1538. {
  1539. struct uac_selector_unit_descriptor *desc = raw_desc;
  1540. unsigned int i, nameid, len;
  1541. int err;
  1542. struct usb_mixer_elem_info *cval;
  1543. struct snd_kcontrol *kctl;
  1544. const struct usbmix_name_map *map;
  1545. char **namelist;
  1546. if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {
  1547. snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
  1548. return -EINVAL;
  1549. }
  1550. for (i = 0; i < desc->bNrInPins; i++) {
  1551. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1552. return err;
  1553. }
  1554. if (desc->bNrInPins == 1) /* only one ? nonsense! */
  1555. return 0;
  1556. map = find_map(state, unitid, 0);
  1557. if (check_ignored_ctl(map))
  1558. return 0;
  1559. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1560. if (! cval) {
  1561. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1562. return -ENOMEM;
  1563. }
  1564. cval->mixer = state->mixer;
  1565. cval->id = unitid;
  1566. cval->val_type = USB_MIXER_U8;
  1567. cval->channels = 1;
  1568. cval->min = 1;
  1569. cval->max = desc->bNrInPins;
  1570. cval->res = 1;
  1571. cval->initialized = 1;
  1572. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  1573. cval->control = UAC2_CX_CLOCK_SELECTOR;
  1574. else
  1575. cval->control = 0;
  1576. namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
  1577. if (! namelist) {
  1578. snd_printk(KERN_ERR "cannot malloc\n");
  1579. kfree(cval);
  1580. return -ENOMEM;
  1581. }
  1582. #define MAX_ITEM_NAME_LEN 64
  1583. for (i = 0; i < desc->bNrInPins; i++) {
  1584. struct usb_audio_term iterm;
  1585. len = 0;
  1586. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  1587. if (! namelist[i]) {
  1588. snd_printk(KERN_ERR "cannot malloc\n");
  1589. while (i--)
  1590. kfree(namelist[i]);
  1591. kfree(namelist);
  1592. kfree(cval);
  1593. return -ENOMEM;
  1594. }
  1595. len = check_mapped_selector_name(state, unitid, i, namelist[i],
  1596. MAX_ITEM_NAME_LEN);
  1597. if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
  1598. len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
  1599. if (! len)
  1600. sprintf(namelist[i], "Input %d", i);
  1601. }
  1602. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  1603. if (! kctl) {
  1604. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1605. kfree(namelist);
  1606. kfree(cval);
  1607. return -ENOMEM;
  1608. }
  1609. kctl->private_value = (unsigned long)namelist;
  1610. kctl->private_free = usb_mixer_selector_elem_free;
  1611. nameid = uac_selector_unit_iSelector(desc);
  1612. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1613. if (len)
  1614. ;
  1615. else if (nameid)
  1616. snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1617. else {
  1618. len = get_term_name(state, &state->oterm,
  1619. kctl->id.name, sizeof(kctl->id.name), 0);
  1620. if (! len)
  1621. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  1622. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  1623. append_ctl_name(kctl, " Clock Source");
  1624. else if ((state->oterm.type & 0xff00) == 0x0100)
  1625. append_ctl_name(kctl, " Capture Source");
  1626. else
  1627. append_ctl_name(kctl, " Playback Source");
  1628. }
  1629. snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
  1630. cval->id, kctl->id.name, desc->bNrInPins);
  1631. if ((err = add_control_to_empty(state, kctl)) < 0)
  1632. return err;
  1633. return 0;
  1634. }
  1635. /*
  1636. * parse an audio unit recursively
  1637. */
  1638. static int parse_audio_unit(struct mixer_build *state, int unitid)
  1639. {
  1640. unsigned char *p1;
  1641. if (test_and_set_bit(unitid, state->unitbitmap))
  1642. return 0; /* the unit already visited */
  1643. p1 = find_audio_control_unit(state, unitid);
  1644. if (!p1) {
  1645. snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
  1646. return -EINVAL;
  1647. }
  1648. switch (p1[2]) {
  1649. case UAC_INPUT_TERMINAL:
  1650. case UAC2_CLOCK_SOURCE:
  1651. return 0; /* NOP */
  1652. case UAC_MIXER_UNIT:
  1653. return parse_audio_mixer_unit(state, unitid, p1);
  1654. case UAC_SELECTOR_UNIT:
  1655. case UAC2_CLOCK_SELECTOR:
  1656. return parse_audio_selector_unit(state, unitid, p1);
  1657. case UAC_FEATURE_UNIT:
  1658. return parse_audio_feature_unit(state, unitid, p1);
  1659. case UAC1_PROCESSING_UNIT:
  1660. /* UAC2_EFFECT_UNIT has the same value */
  1661. if (state->mixer->protocol == UAC_VERSION_1)
  1662. return parse_audio_processing_unit(state, unitid, p1);
  1663. else
  1664. return 0; /* FIXME - effect units not implemented yet */
  1665. case UAC1_EXTENSION_UNIT:
  1666. /* UAC2_PROCESSING_UNIT_V2 has the same value */
  1667. if (state->mixer->protocol == UAC_VERSION_1)
  1668. return parse_audio_extension_unit(state, unitid, p1);
  1669. else /* UAC_VERSION_2 */
  1670. return parse_audio_processing_unit(state, unitid, p1);
  1671. default:
  1672. snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  1673. return -EINVAL;
  1674. }
  1675. }
  1676. static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
  1677. {
  1678. kfree(mixer->id_elems);
  1679. if (mixer->urb) {
  1680. kfree(mixer->urb->transfer_buffer);
  1681. usb_free_urb(mixer->urb);
  1682. }
  1683. usb_free_urb(mixer->rc_urb);
  1684. kfree(mixer->rc_setup_packet);
  1685. kfree(mixer);
  1686. }
  1687. static int snd_usb_mixer_dev_free(struct snd_device *device)
  1688. {
  1689. struct usb_mixer_interface *mixer = device->device_data;
  1690. snd_usb_mixer_free(mixer);
  1691. return 0;
  1692. }
  1693. /*
  1694. * create mixer controls
  1695. *
  1696. * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
  1697. */
  1698. static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
  1699. {
  1700. struct mixer_build state;
  1701. int err;
  1702. const struct usbmix_ctl_map *map;
  1703. struct usb_host_interface *hostif;
  1704. void *p;
  1705. hostif = mixer->chip->ctrl_intf;
  1706. memset(&state, 0, sizeof(state));
  1707. state.chip = mixer->chip;
  1708. state.mixer = mixer;
  1709. state.buffer = hostif->extra;
  1710. state.buflen = hostif->extralen;
  1711. /* check the mapping table */
  1712. for (map = usbmix_ctl_maps; map->id; map++) {
  1713. if (map->id == state.chip->usb_id) {
  1714. state.map = map->map;
  1715. state.selector_map = map->selector_map;
  1716. mixer->ignore_ctl_error = map->ignore_ctl_error;
  1717. break;
  1718. }
  1719. }
  1720. p = NULL;
  1721. while ((p = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, p, UAC_OUTPUT_TERMINAL)) != NULL) {
  1722. if (mixer->protocol == UAC_VERSION_1) {
  1723. struct uac1_output_terminal_descriptor *desc = p;
  1724. if (desc->bLength < sizeof(*desc))
  1725. continue; /* invalid descriptor? */
  1726. set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
  1727. state.oterm.id = desc->bTerminalID;
  1728. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  1729. state.oterm.name = desc->iTerminal;
  1730. err = parse_audio_unit(&state, desc->bSourceID);
  1731. if (err < 0)
  1732. return err;
  1733. } else { /* UAC_VERSION_2 */
  1734. struct uac2_output_terminal_descriptor *desc = p;
  1735. if (desc->bLength < sizeof(*desc))
  1736. continue; /* invalid descriptor? */
  1737. set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
  1738. state.oterm.id = desc->bTerminalID;
  1739. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  1740. state.oterm.name = desc->iTerminal;
  1741. err = parse_audio_unit(&state, desc->bSourceID);
  1742. if (err < 0)
  1743. return err;
  1744. /* for UAC2, use the same approach to also add the clock selectors */
  1745. err = parse_audio_unit(&state, desc->bCSourceID);
  1746. if (err < 0)
  1747. return err;
  1748. }
  1749. }
  1750. return 0;
  1751. }
  1752. void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
  1753. {
  1754. struct usb_mixer_elem_info *info;
  1755. for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
  1756. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1757. info->elem_id);
  1758. }
  1759. static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
  1760. int unitid,
  1761. struct usb_mixer_elem_info *cval)
  1762. {
  1763. static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
  1764. "S8", "U8", "S16", "U16"};
  1765. snd_iprintf(buffer, " Unit: %i\n", unitid);
  1766. if (cval->elem_id)
  1767. snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
  1768. cval->elem_id->name, cval->elem_id->index);
  1769. snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
  1770. "channels=%i, type=\"%s\"\n", cval->id,
  1771. cval->control, cval->cmask, cval->channels,
  1772. val_types[cval->val_type]);
  1773. snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
  1774. cval->min, cval->max, cval->dBmin, cval->dBmax);
  1775. }
  1776. static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
  1777. struct snd_info_buffer *buffer)
  1778. {
  1779. struct snd_usb_audio *chip = entry->private_data;
  1780. struct usb_mixer_interface *mixer;
  1781. struct usb_mixer_elem_info *cval;
  1782. int unitid;
  1783. list_for_each_entry(mixer, &chip->mixer_list, list) {
  1784. snd_iprintf(buffer,
  1785. "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
  1786. chip->usb_id, snd_usb_ctrl_intf(chip),
  1787. mixer->ignore_ctl_error);
  1788. snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
  1789. for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
  1790. for (cval = mixer->id_elems[unitid]; cval;
  1791. cval = cval->next_id_elem)
  1792. snd_usb_mixer_dump_cval(buffer, unitid, cval);
  1793. }
  1794. }
  1795. }
  1796. static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
  1797. int attribute, int value, int index)
  1798. {
  1799. struct usb_mixer_elem_info *info;
  1800. __u8 unitid = (index >> 8) & 0xff;
  1801. __u8 control = (value >> 8) & 0xff;
  1802. __u8 channel = value & 0xff;
  1803. if (channel >= MAX_CHANNELS) {
  1804. snd_printk(KERN_DEBUG "%s(): bogus channel number %d\n",
  1805. __func__, channel);
  1806. return;
  1807. }
  1808. for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) {
  1809. if (info->control != control)
  1810. continue;
  1811. switch (attribute) {
  1812. case UAC2_CS_CUR:
  1813. /* invalidate cache, so the value is read from the device */
  1814. if (channel)
  1815. info->cached &= ~(1 << channel);
  1816. else /* master channel */
  1817. info->cached = 0;
  1818. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1819. info->elem_id);
  1820. break;
  1821. case UAC2_CS_RANGE:
  1822. /* TODO */
  1823. break;
  1824. case UAC2_CS_MEM:
  1825. /* TODO */
  1826. break;
  1827. default:
  1828. snd_printk(KERN_DEBUG "unknown attribute %d in interrupt\n",
  1829. attribute);
  1830. break;
  1831. } /* switch */
  1832. }
  1833. }
  1834. static void snd_usb_mixer_interrupt(struct urb *urb)
  1835. {
  1836. struct usb_mixer_interface *mixer = urb->context;
  1837. int len = urb->actual_length;
  1838. if (urb->status != 0)
  1839. goto requeue;
  1840. if (mixer->protocol == UAC_VERSION_1) {
  1841. struct uac1_status_word *status;
  1842. for (status = urb->transfer_buffer;
  1843. len >= sizeof(*status);
  1844. len -= sizeof(*status), status++) {
  1845. snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
  1846. status->bStatusType,
  1847. status->bOriginator);
  1848. /* ignore any notifications not from the control interface */
  1849. if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
  1850. UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
  1851. continue;
  1852. if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
  1853. snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
  1854. else
  1855. snd_usb_mixer_notify_id(mixer, status->bOriginator);
  1856. }
  1857. } else { /* UAC_VERSION_2 */
  1858. struct uac2_interrupt_data_msg *msg;
  1859. for (msg = urb->transfer_buffer;
  1860. len >= sizeof(*msg);
  1861. len -= sizeof(*msg), msg++) {
  1862. /* drop vendor specific and endpoint requests */
  1863. if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
  1864. (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
  1865. continue;
  1866. snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
  1867. le16_to_cpu(msg->wValue),
  1868. le16_to_cpu(msg->wIndex));
  1869. }
  1870. }
  1871. requeue:
  1872. if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
  1873. urb->dev = mixer->chip->dev;
  1874. usb_submit_urb(urb, GFP_ATOMIC);
  1875. }
  1876. }
  1877. /* create the handler for the optional status interrupt endpoint */
  1878. static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
  1879. {
  1880. struct usb_host_interface *hostif;
  1881. struct usb_endpoint_descriptor *ep;
  1882. void *transfer_buffer;
  1883. int buffer_length;
  1884. unsigned int epnum;
  1885. hostif = mixer->chip->ctrl_intf;
  1886. /* we need one interrupt input endpoint */
  1887. if (get_iface_desc(hostif)->bNumEndpoints < 1)
  1888. return 0;
  1889. ep = get_endpoint(hostif, 0);
  1890. if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
  1891. return 0;
  1892. epnum = usb_endpoint_num(ep);
  1893. buffer_length = le16_to_cpu(ep->wMaxPacketSize);
  1894. transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
  1895. if (!transfer_buffer)
  1896. return -ENOMEM;
  1897. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  1898. if (!mixer->urb) {
  1899. kfree(transfer_buffer);
  1900. return -ENOMEM;
  1901. }
  1902. usb_fill_int_urb(mixer->urb, mixer->chip->dev,
  1903. usb_rcvintpipe(mixer->chip->dev, epnum),
  1904. transfer_buffer, buffer_length,
  1905. snd_usb_mixer_interrupt, mixer, ep->bInterval);
  1906. usb_submit_urb(mixer->urb, GFP_KERNEL);
  1907. return 0;
  1908. }
  1909. int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
  1910. int ignore_error)
  1911. {
  1912. static struct snd_device_ops dev_ops = {
  1913. .dev_free = snd_usb_mixer_dev_free
  1914. };
  1915. struct usb_mixer_interface *mixer;
  1916. struct snd_info_entry *entry;
  1917. struct usb_host_interface *host_iface;
  1918. int err;
  1919. strcpy(chip->card->mixername, "USB Mixer");
  1920. mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
  1921. if (!mixer)
  1922. return -ENOMEM;
  1923. mixer->chip = chip;
  1924. mixer->ignore_ctl_error = ignore_error;
  1925. mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
  1926. GFP_KERNEL);
  1927. if (!mixer->id_elems) {
  1928. kfree(mixer);
  1929. return -ENOMEM;
  1930. }
  1931. host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
  1932. switch (get_iface_desc(host_iface)->bInterfaceProtocol) {
  1933. case UAC_VERSION_1:
  1934. default:
  1935. mixer->protocol = UAC_VERSION_1;
  1936. break;
  1937. case UAC_VERSION_2:
  1938. mixer->protocol = UAC_VERSION_2;
  1939. break;
  1940. }
  1941. if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
  1942. (err = snd_usb_mixer_status_create(mixer)) < 0)
  1943. goto _error;
  1944. snd_usb_mixer_apply_create_quirk(mixer);
  1945. err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
  1946. if (err < 0)
  1947. goto _error;
  1948. if (list_empty(&chip->mixer_list) &&
  1949. !snd_card_proc_new(chip->card, "usbmixer", &entry))
  1950. snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
  1951. list_add(&mixer->list, &chip->mixer_list);
  1952. return 0;
  1953. _error:
  1954. snd_usb_mixer_free(mixer);
  1955. return err;
  1956. }
  1957. void snd_usb_mixer_disconnect(struct list_head *p)
  1958. {
  1959. struct usb_mixer_interface *mixer;
  1960. mixer = list_entry(p, struct usb_mixer_interface, list);
  1961. usb_kill_urb(mixer->urb);
  1962. usb_kill_urb(mixer->rc_urb);
  1963. }