mixer.c 63 KB

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