mixer.c 68 KB

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