mixer.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  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. static int 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. err = check_input_term(state, d->baSourceID[0], term);
  634. if (err < 0)
  635. return err;
  636. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  637. term->id = id;
  638. term->name = uac_selector_unit_iSelector(d);
  639. return 0;
  640. }
  641. case UAC1_PROCESSING_UNIT:
  642. case UAC1_EXTENSION_UNIT:
  643. /* UAC2_PROCESSING_UNIT_V2 */
  644. /* UAC2_EFFECT_UNIT */
  645. case UAC2_EXTENSION_UNIT_V2: {
  646. struct uac_processing_unit_descriptor *d = p1;
  647. if (state->mixer->protocol == UAC_VERSION_2 &&
  648. hdr[2] == UAC2_EFFECT_UNIT) {
  649. /* UAC2/UAC1 unit IDs overlap here in an
  650. * uncompatible way. Ignore this unit for now.
  651. */
  652. return 0;
  653. }
  654. if (d->bNrInPins) {
  655. id = d->baSourceID[0];
  656. break; /* continue to parse */
  657. }
  658. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  659. term->channels = uac_processing_unit_bNrChannels(d);
  660. term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol);
  661. term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol);
  662. return 0;
  663. }
  664. case UAC2_CLOCK_SOURCE: {
  665. struct uac_clock_source_descriptor *d = p1;
  666. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  667. term->id = id;
  668. term->name = d->iClockSource;
  669. return 0;
  670. }
  671. default:
  672. return -ENODEV;
  673. }
  674. }
  675. return -ENODEV;
  676. }
  677. /*
  678. * Feature Unit
  679. */
  680. /* feature unit control information */
  681. struct usb_feature_control_info {
  682. const char *name;
  683. unsigned int type; /* control type (mute, volume, etc.) */
  684. };
  685. static struct usb_feature_control_info audio_feature_info[] = {
  686. { "Mute", USB_MIXER_INV_BOOLEAN },
  687. { "Volume", USB_MIXER_S16 },
  688. { "Tone Control - Bass", USB_MIXER_S8 },
  689. { "Tone Control - Mid", USB_MIXER_S8 },
  690. { "Tone Control - Treble", USB_MIXER_S8 },
  691. { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
  692. { "Auto Gain Control", USB_MIXER_BOOLEAN },
  693. { "Delay Control", USB_MIXER_U16 },
  694. { "Bass Boost", USB_MIXER_BOOLEAN },
  695. { "Loudness", USB_MIXER_BOOLEAN },
  696. /* UAC2 specific */
  697. { "Input Gain Control", USB_MIXER_U16 },
  698. { "Input Gain Pad Control", USB_MIXER_BOOLEAN },
  699. { "Phase Inverter Control", USB_MIXER_BOOLEAN },
  700. };
  701. /* private_free callback */
  702. static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
  703. {
  704. kfree(kctl->private_data);
  705. kctl->private_data = NULL;
  706. }
  707. /*
  708. * interface to ALSA control for feature/mixer units
  709. */
  710. /* volume control quirks */
  711. static void volume_control_quirks(struct usb_mixer_elem_info *cval,
  712. struct snd_kcontrol *kctl)
  713. {
  714. switch (cval->mixer->chip->usb_id) {
  715. case USB_ID(0x0471, 0x0101):
  716. case USB_ID(0x0471, 0x0104):
  717. case USB_ID(0x0471, 0x0105):
  718. case USB_ID(0x0672, 0x1041):
  719. /* quirk for UDA1321/N101.
  720. * note that detection between firmware 2.1.1.7 (N101)
  721. * and later 2.1.1.21 is not very clear from datasheets.
  722. * I hope that the min value is -15360 for newer firmware --jk
  723. */
  724. if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
  725. cval->min == -15616) {
  726. snd_printk(KERN_INFO
  727. "set volume quirk for UDA1321/N101 chip\n");
  728. cval->max = -256;
  729. }
  730. break;
  731. case USB_ID(0x046d, 0x09a4):
  732. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  733. snd_printk(KERN_INFO
  734. "set volume quirk for QuickCam E3500\n");
  735. cval->min = 6080;
  736. cval->max = 8768;
  737. cval->res = 192;
  738. }
  739. break;
  740. case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
  741. case USB_ID(0x046d, 0x0808):
  742. case USB_ID(0x046d, 0x0809):
  743. case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
  744. case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
  745. case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
  746. case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
  747. case USB_ID(0x046d, 0x0991):
  748. /* Most audio usb devices lie about volume resolution.
  749. * Most Logitech webcams have res = 384.
  750. * Proboly there is some logitech magic behind this number --fishor
  751. */
  752. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  753. snd_printk(KERN_INFO
  754. "set resolution quirk: cval->res = 384\n");
  755. cval->res = 384;
  756. }
  757. break;
  758. }
  759. }
  760. /*
  761. * retrieve the minimum and maximum values for the specified control
  762. */
  763. static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
  764. int default_min, struct snd_kcontrol *kctl)
  765. {
  766. /* for failsafe */
  767. cval->min = default_min;
  768. cval->max = cval->min + 1;
  769. cval->res = 1;
  770. cval->dBmin = cval->dBmax = 0;
  771. if (cval->val_type == USB_MIXER_BOOLEAN ||
  772. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  773. cval->initialized = 1;
  774. } else {
  775. int minchn = 0;
  776. if (cval->cmask) {
  777. int i;
  778. for (i = 0; i < MAX_CHANNELS; i++)
  779. if (cval->cmask & (1 << i)) {
  780. minchn = i + 1;
  781. break;
  782. }
  783. }
  784. if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
  785. get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
  786. snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
  787. cval->id, snd_usb_ctrl_intf(cval->mixer->chip), cval->control, cval->id);
  788. return -EINVAL;
  789. }
  790. if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
  791. cval->res = 1;
  792. } else {
  793. int last_valid_res = cval->res;
  794. while (cval->res > 1) {
  795. if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
  796. (cval->control << 8) | minchn, cval->res / 2) < 0)
  797. break;
  798. cval->res /= 2;
  799. }
  800. if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
  801. cval->res = last_valid_res;
  802. }
  803. if (cval->res == 0)
  804. cval->res = 1;
  805. /* Additional checks for the proper resolution
  806. *
  807. * Some devices report smaller resolutions than actually
  808. * reacting. They don't return errors but simply clip
  809. * to the lower aligned value.
  810. */
  811. if (cval->min + cval->res < cval->max) {
  812. int last_valid_res = cval->res;
  813. int saved, test, check;
  814. get_cur_mix_raw(cval, minchn, &saved);
  815. for (;;) {
  816. test = saved;
  817. if (test < cval->max)
  818. test += cval->res;
  819. else
  820. test -= cval->res;
  821. if (test < cval->min || test > cval->max ||
  822. set_cur_mix_value(cval, minchn, 0, test) ||
  823. get_cur_mix_raw(cval, minchn, &check)) {
  824. cval->res = last_valid_res;
  825. break;
  826. }
  827. if (test == check)
  828. break;
  829. cval->res *= 2;
  830. }
  831. set_cur_mix_value(cval, minchn, 0, saved);
  832. }
  833. cval->initialized = 1;
  834. }
  835. if (kctl)
  836. volume_control_quirks(cval, kctl);
  837. /* USB descriptions contain the dB scale in 1/256 dB unit
  838. * while ALSA TLV contains in 1/100 dB unit
  839. */
  840. cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
  841. cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
  842. if (cval->dBmin > cval->dBmax) {
  843. /* something is wrong; assume it's either from/to 0dB */
  844. if (cval->dBmin < 0)
  845. cval->dBmax = 0;
  846. else if (cval->dBmin > 0)
  847. cval->dBmin = 0;
  848. if (cval->dBmin > cval->dBmax) {
  849. /* totally crap, return an error */
  850. return -EINVAL;
  851. }
  852. }
  853. return 0;
  854. }
  855. #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
  856. /* get a feature/mixer unit info */
  857. static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  858. {
  859. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  860. if (cval->val_type == USB_MIXER_BOOLEAN ||
  861. cval->val_type == USB_MIXER_INV_BOOLEAN)
  862. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  863. else
  864. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  865. uinfo->count = cval->channels;
  866. if (cval->val_type == USB_MIXER_BOOLEAN ||
  867. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  868. uinfo->value.integer.min = 0;
  869. uinfo->value.integer.max = 1;
  870. } else {
  871. if (!cval->initialized) {
  872. get_min_max_with_quirks(cval, 0, kcontrol);
  873. if (cval->initialized && cval->dBmin >= cval->dBmax) {
  874. kcontrol->vd[0].access &=
  875. ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  876. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
  877. snd_ctl_notify(cval->mixer->chip->card,
  878. SNDRV_CTL_EVENT_MASK_INFO,
  879. &kcontrol->id);
  880. }
  881. }
  882. uinfo->value.integer.min = 0;
  883. uinfo->value.integer.max =
  884. (cval->max - cval->min + cval->res - 1) / cval->res;
  885. }
  886. return 0;
  887. }
  888. /* get the current value from feature/mixer unit */
  889. static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  890. {
  891. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  892. int c, cnt, val, err;
  893. ucontrol->value.integer.value[0] = cval->min;
  894. if (cval->cmask) {
  895. cnt = 0;
  896. for (c = 0; c < MAX_CHANNELS; c++) {
  897. if (!(cval->cmask & (1 << c)))
  898. continue;
  899. err = get_cur_mix_value(cval, c + 1, cnt, &val);
  900. if (err < 0)
  901. return cval->mixer->ignore_ctl_error ? 0 : err;
  902. val = get_relative_value(cval, val);
  903. ucontrol->value.integer.value[cnt] = val;
  904. cnt++;
  905. }
  906. return 0;
  907. } else {
  908. /* master channel */
  909. err = get_cur_mix_value(cval, 0, 0, &val);
  910. if (err < 0)
  911. return cval->mixer->ignore_ctl_error ? 0 : err;
  912. val = get_relative_value(cval, val);
  913. ucontrol->value.integer.value[0] = val;
  914. }
  915. return 0;
  916. }
  917. /* put the current value to feature/mixer unit */
  918. static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  919. {
  920. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  921. int c, cnt, val, oval, err;
  922. int changed = 0;
  923. if (cval->cmask) {
  924. cnt = 0;
  925. for (c = 0; c < MAX_CHANNELS; c++) {
  926. if (!(cval->cmask & (1 << c)))
  927. continue;
  928. err = get_cur_mix_value(cval, c + 1, cnt, &oval);
  929. if (err < 0)
  930. return cval->mixer->ignore_ctl_error ? 0 : err;
  931. val = ucontrol->value.integer.value[cnt];
  932. val = get_abs_value(cval, val);
  933. if (oval != val) {
  934. set_cur_mix_value(cval, c + 1, cnt, val);
  935. changed = 1;
  936. }
  937. cnt++;
  938. }
  939. } else {
  940. /* master channel */
  941. err = get_cur_mix_value(cval, 0, 0, &oval);
  942. if (err < 0)
  943. return cval->mixer->ignore_ctl_error ? 0 : err;
  944. val = ucontrol->value.integer.value[0];
  945. val = get_abs_value(cval, val);
  946. if (val != oval) {
  947. set_cur_mix_value(cval, 0, 0, val);
  948. changed = 1;
  949. }
  950. }
  951. return changed;
  952. }
  953. static struct snd_kcontrol_new usb_feature_unit_ctl = {
  954. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  955. .name = "", /* will be filled later manually */
  956. .info = mixer_ctl_feature_info,
  957. .get = mixer_ctl_feature_get,
  958. .put = mixer_ctl_feature_put,
  959. };
  960. /* the read-only variant */
  961. static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
  962. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  963. .name = "", /* will be filled later manually */
  964. .info = mixer_ctl_feature_info,
  965. .get = mixer_ctl_feature_get,
  966. .put = NULL,
  967. };
  968. /* This symbol is exported in order to allow the mixer quirks to
  969. * hook up to the standard feature unit control mechanism */
  970. struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
  971. /*
  972. * build a feature control
  973. */
  974. static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
  975. {
  976. return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
  977. }
  978. static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
  979. unsigned int ctl_mask, int control,
  980. struct usb_audio_term *iterm, int unitid,
  981. int readonly_mask)
  982. {
  983. struct uac_feature_unit_descriptor *desc = raw_desc;
  984. unsigned int len = 0;
  985. int mapped_name = 0;
  986. int nameid = uac_feature_unit_iFeature(desc);
  987. struct snd_kcontrol *kctl;
  988. struct usb_mixer_elem_info *cval;
  989. const struct usbmix_name_map *map;
  990. unsigned int range;
  991. control++; /* change from zero-based to 1-based value */
  992. if (control == UAC_FU_GRAPHIC_EQUALIZER) {
  993. /* FIXME: not supported yet */
  994. return;
  995. }
  996. map = find_map(state, unitid, control);
  997. if (check_ignored_ctl(map))
  998. return;
  999. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1000. if (! cval) {
  1001. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1002. return;
  1003. }
  1004. cval->mixer = state->mixer;
  1005. cval->id = unitid;
  1006. cval->control = control;
  1007. cval->cmask = ctl_mask;
  1008. cval->val_type = audio_feature_info[control-1].type;
  1009. if (ctl_mask == 0) {
  1010. cval->channels = 1; /* master channel */
  1011. cval->master_readonly = readonly_mask;
  1012. } else {
  1013. int i, c = 0;
  1014. for (i = 0; i < 16; i++)
  1015. if (ctl_mask & (1 << i))
  1016. c++;
  1017. cval->channels = c;
  1018. cval->ch_readonly = readonly_mask;
  1019. }
  1020. /* if all channels in the mask are marked read-only, make the control
  1021. * read-only. set_cur_mix_value() will check the mask again and won't
  1022. * issue write commands to read-only channels. */
  1023. if (cval->channels == readonly_mask)
  1024. kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
  1025. else
  1026. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1027. if (! kctl) {
  1028. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1029. kfree(cval);
  1030. return;
  1031. }
  1032. kctl->private_free = usb_mixer_elem_free;
  1033. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1034. mapped_name = len != 0;
  1035. if (! len && nameid)
  1036. len = snd_usb_copy_string_desc(state, nameid,
  1037. kctl->id.name, sizeof(kctl->id.name));
  1038. /* get min/max values */
  1039. get_min_max_with_quirks(cval, 0, kctl);
  1040. switch (control) {
  1041. case UAC_FU_MUTE:
  1042. case UAC_FU_VOLUME:
  1043. /* determine the control name. the rule is:
  1044. * - if a name id is given in descriptor, use it.
  1045. * - if the connected input can be determined, then use the name
  1046. * of terminal type.
  1047. * - if the connected output can be determined, use it.
  1048. * - otherwise, anonymous name.
  1049. */
  1050. if (! len) {
  1051. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
  1052. if (! len)
  1053. len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
  1054. if (! len)
  1055. len = snprintf(kctl->id.name, sizeof(kctl->id.name),
  1056. "Feature %d", unitid);
  1057. }
  1058. /* determine the stream direction:
  1059. * if the connected output is USB stream, then it's likely a
  1060. * capture stream. otherwise it should be playback (hopefully :)
  1061. */
  1062. if (! mapped_name && ! (state->oterm.type >> 16)) {
  1063. if ((state->oterm.type & 0xff00) == 0x0100) {
  1064. len = append_ctl_name(kctl, " Capture");
  1065. } else {
  1066. len = append_ctl_name(kctl, " Playback");
  1067. }
  1068. }
  1069. append_ctl_name(kctl, control == UAC_FU_MUTE ?
  1070. " Switch" : " Volume");
  1071. if (control == UAC_FU_VOLUME) {
  1072. check_mapped_dB(map, cval);
  1073. if (cval->dBmin < cval->dBmax || !cval->initialized) {
  1074. kctl->tlv.c = mixer_vol_tlv;
  1075. kctl->vd[0].access |=
  1076. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1077. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1078. }
  1079. }
  1080. break;
  1081. default:
  1082. if (! len)
  1083. strlcpy(kctl->id.name, audio_feature_info[control-1].name,
  1084. sizeof(kctl->id.name));
  1085. break;
  1086. }
  1087. snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
  1088. range = (cval->max - cval->min) / cval->res;
  1089. /* Are there devices with volume range more than 255? I use a bit more
  1090. * to be sure. 384 is a resolution magic number found on Logitech
  1091. * devices. It will definitively catch all buggy Logitech devices.
  1092. */
  1093. if (range > 384) {
  1094. snd_printk(KERN_WARNING "usb_audio: Warning! Unlikely big "
  1095. "volume range (=%u), cval->res is probably wrong.",
  1096. range);
  1097. snd_printk(KERN_WARNING "usb_audio: [%d] FU [%s] ch = %d, "
  1098. "val = %d/%d/%d", cval->id,
  1099. kctl->id.name, cval->channels,
  1100. cval->min, cval->max, cval->res);
  1101. }
  1102. snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
  1103. cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
  1104. snd_usb_mixer_add_control(state->mixer, kctl);
  1105. }
  1106. /*
  1107. * parse a feature unit
  1108. *
  1109. * most of controls are defined here.
  1110. */
  1111. static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
  1112. {
  1113. int channels, i, j;
  1114. struct usb_audio_term iterm;
  1115. unsigned int master_bits, first_ch_bits;
  1116. int err, csize;
  1117. struct uac_feature_unit_descriptor *hdr = _ftr;
  1118. __u8 *bmaControls;
  1119. if (state->mixer->protocol == UAC_VERSION_1) {
  1120. csize = hdr->bControlSize;
  1121. if (!csize) {
  1122. snd_printdd(KERN_ERR "usbaudio: unit %u: "
  1123. "invalid bControlSize == 0\n", unitid);
  1124. return -EINVAL;
  1125. }
  1126. channels = (hdr->bLength - 7) / csize - 1;
  1127. bmaControls = hdr->bmaControls;
  1128. if (hdr->bLength < 7 + csize) {
  1129. snd_printk(KERN_ERR "usbaudio: unit %u: "
  1130. "invalid UAC_FEATURE_UNIT descriptor\n",
  1131. unitid);
  1132. return -EINVAL;
  1133. }
  1134. } else {
  1135. struct uac2_feature_unit_descriptor *ftr = _ftr;
  1136. csize = 4;
  1137. channels = (hdr->bLength - 6) / 4 - 1;
  1138. bmaControls = ftr->bmaControls;
  1139. if (hdr->bLength < 6 + csize) {
  1140. snd_printk(KERN_ERR "usbaudio: unit %u: "
  1141. "invalid UAC_FEATURE_UNIT descriptor\n",
  1142. unitid);
  1143. return -EINVAL;
  1144. }
  1145. }
  1146. /* parse the source unit */
  1147. if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
  1148. return err;
  1149. /* determine the input source type and name */
  1150. err = check_input_term(state, hdr->bSourceID, &iterm);
  1151. if (err < 0)
  1152. return err;
  1153. master_bits = snd_usb_combine_bytes(bmaControls, csize);
  1154. /* master configuration quirks */
  1155. switch (state->chip->usb_id) {
  1156. case USB_ID(0x08bb, 0x2702):
  1157. snd_printk(KERN_INFO
  1158. "usbmixer: master volume quirk for PCM2702 chip\n");
  1159. /* disable non-functional volume control */
  1160. master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
  1161. break;
  1162. case USB_ID(0x1130, 0xf211):
  1163. snd_printk(KERN_INFO
  1164. "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
  1165. /* disable non-functional volume control */
  1166. channels = 0;
  1167. break;
  1168. }
  1169. if (channels > 0)
  1170. first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
  1171. else
  1172. first_ch_bits = 0;
  1173. if (state->mixer->protocol == UAC_VERSION_1) {
  1174. /* check all control types */
  1175. for (i = 0; i < 10; i++) {
  1176. unsigned int ch_bits = 0;
  1177. for (j = 0; j < channels; j++) {
  1178. unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
  1179. if (mask & (1 << i))
  1180. ch_bits |= (1 << j);
  1181. }
  1182. /* audio class v1 controls are never read-only */
  1183. if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
  1184. build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, 0);
  1185. if (master_bits & (1 << i))
  1186. build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0);
  1187. }
  1188. } else { /* UAC_VERSION_2 */
  1189. for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
  1190. unsigned int ch_bits = 0;
  1191. unsigned int ch_read_only = 0;
  1192. for (j = 0; j < channels; j++) {
  1193. unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
  1194. if (uac2_control_is_readable(mask, i)) {
  1195. ch_bits |= (1 << j);
  1196. if (!uac2_control_is_writeable(mask, i))
  1197. ch_read_only |= (1 << j);
  1198. }
  1199. }
  1200. /* NOTE: build_feature_ctl() will mark the control read-only if all channels
  1201. * are marked read-only in the descriptors. Otherwise, the control will be
  1202. * reported as writeable, but the driver will not actually issue a write
  1203. * command for read-only channels */
  1204. if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
  1205. build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, ch_read_only);
  1206. if (uac2_control_is_readable(master_bits, i))
  1207. build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
  1208. !uac2_control_is_writeable(master_bits, i));
  1209. }
  1210. }
  1211. return 0;
  1212. }
  1213. /*
  1214. * Mixer Unit
  1215. */
  1216. /*
  1217. * build a mixer unit control
  1218. *
  1219. * the callbacks are identical with feature unit.
  1220. * input channel number (zero based) is given in control field instead.
  1221. */
  1222. static void build_mixer_unit_ctl(struct mixer_build *state,
  1223. struct uac_mixer_unit_descriptor *desc,
  1224. int in_pin, int in_ch, int unitid,
  1225. struct usb_audio_term *iterm)
  1226. {
  1227. struct usb_mixer_elem_info *cval;
  1228. unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
  1229. unsigned int i, len;
  1230. struct snd_kcontrol *kctl;
  1231. const struct usbmix_name_map *map;
  1232. map = find_map(state, unitid, 0);
  1233. if (check_ignored_ctl(map))
  1234. return;
  1235. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1236. if (! cval)
  1237. return;
  1238. cval->mixer = state->mixer;
  1239. cval->id = unitid;
  1240. cval->control = in_ch + 1; /* based on 1 */
  1241. cval->val_type = USB_MIXER_S16;
  1242. for (i = 0; i < num_outs; i++) {
  1243. if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), in_ch, i, num_outs)) {
  1244. cval->cmask |= (1 << i);
  1245. cval->channels++;
  1246. }
  1247. }
  1248. /* get min/max values */
  1249. get_min_max(cval, 0);
  1250. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1251. if (! kctl) {
  1252. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1253. kfree(cval);
  1254. return;
  1255. }
  1256. kctl->private_free = usb_mixer_elem_free;
  1257. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1258. if (! len)
  1259. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
  1260. if (! len)
  1261. len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
  1262. append_ctl_name(kctl, " Volume");
  1263. snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
  1264. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1265. snd_usb_mixer_add_control(state->mixer, kctl);
  1266. }
  1267. /*
  1268. * parse a mixer unit
  1269. */
  1270. static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1271. {
  1272. struct uac_mixer_unit_descriptor *desc = raw_desc;
  1273. struct usb_audio_term iterm;
  1274. int input_pins, num_ins, num_outs;
  1275. int pin, ich, err;
  1276. if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) ||
  1277. desc->bLength < sizeof(*desc) + desc->bNrInPins ||
  1278. !(num_outs = uac_mixer_unit_bNrChannels(desc))) {
  1279. snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
  1280. return -EINVAL;
  1281. }
  1282. /* no bmControls field (e.g. Maya44) -> ignore */
  1283. if (desc->bLength <= 10 + input_pins) {
  1284. snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
  1285. return 0;
  1286. }
  1287. num_ins = 0;
  1288. ich = 0;
  1289. for (pin = 0; pin < input_pins; pin++) {
  1290. err = parse_audio_unit(state, desc->baSourceID[pin]);
  1291. if (err < 0)
  1292. return err;
  1293. err = check_input_term(state, desc->baSourceID[pin], &iterm);
  1294. if (err < 0)
  1295. return err;
  1296. num_ins += iterm.channels;
  1297. for (; ich < num_ins; ++ich) {
  1298. int och, ich_has_controls = 0;
  1299. for (och = 0; och < num_outs; ++och) {
  1300. if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol),
  1301. ich, och, num_outs)) {
  1302. ich_has_controls = 1;
  1303. break;
  1304. }
  1305. }
  1306. if (ich_has_controls)
  1307. build_mixer_unit_ctl(state, desc, pin, ich,
  1308. unitid, &iterm);
  1309. }
  1310. }
  1311. return 0;
  1312. }
  1313. /*
  1314. * Processing Unit / Extension Unit
  1315. */
  1316. /* get callback for processing/extension unit */
  1317. static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1318. {
  1319. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1320. int err, val;
  1321. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1322. if (err < 0 && cval->mixer->ignore_ctl_error) {
  1323. ucontrol->value.integer.value[0] = cval->min;
  1324. return 0;
  1325. }
  1326. if (err < 0)
  1327. return err;
  1328. val = get_relative_value(cval, val);
  1329. ucontrol->value.integer.value[0] = val;
  1330. return 0;
  1331. }
  1332. /* put callback for processing/extension unit */
  1333. static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1334. {
  1335. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1336. int val, oval, err;
  1337. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1338. if (err < 0) {
  1339. if (cval->mixer->ignore_ctl_error)
  1340. return 0;
  1341. return err;
  1342. }
  1343. val = ucontrol->value.integer.value[0];
  1344. val = get_abs_value(cval, val);
  1345. if (val != oval) {
  1346. set_cur_ctl_value(cval, cval->control << 8, val);
  1347. return 1;
  1348. }
  1349. return 0;
  1350. }
  1351. /* alsa control interface for processing/extension unit */
  1352. static struct snd_kcontrol_new mixer_procunit_ctl = {
  1353. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1354. .name = "", /* will be filled later */
  1355. .info = mixer_ctl_feature_info,
  1356. .get = mixer_ctl_procunit_get,
  1357. .put = mixer_ctl_procunit_put,
  1358. };
  1359. /*
  1360. * predefined data for processing units
  1361. */
  1362. struct procunit_value_info {
  1363. int control;
  1364. char *suffix;
  1365. int val_type;
  1366. int min_value;
  1367. };
  1368. struct procunit_info {
  1369. int type;
  1370. char *name;
  1371. struct procunit_value_info *values;
  1372. };
  1373. static struct procunit_value_info updown_proc_info[] = {
  1374. { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1375. { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1376. { 0 }
  1377. };
  1378. static struct procunit_value_info prologic_proc_info[] = {
  1379. { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1380. { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1381. { 0 }
  1382. };
  1383. static struct procunit_value_info threed_enh_proc_info[] = {
  1384. { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1385. { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
  1386. { 0 }
  1387. };
  1388. static struct procunit_value_info reverb_proc_info[] = {
  1389. { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1390. { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
  1391. { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
  1392. { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
  1393. { 0 }
  1394. };
  1395. static struct procunit_value_info chorus_proc_info[] = {
  1396. { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1397. { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
  1398. { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
  1399. { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
  1400. { 0 }
  1401. };
  1402. static struct procunit_value_info dcr_proc_info[] = {
  1403. { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1404. { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
  1405. { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
  1406. { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
  1407. { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
  1408. { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
  1409. { 0 }
  1410. };
  1411. static struct procunit_info procunits[] = {
  1412. { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
  1413. { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
  1414. { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
  1415. { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
  1416. { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
  1417. { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
  1418. { 0 },
  1419. };
  1420. /*
  1421. * predefined data for extension units
  1422. */
  1423. static struct procunit_value_info clock_rate_xu_info[] = {
  1424. { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
  1425. { 0 }
  1426. };
  1427. static struct procunit_value_info clock_source_xu_info[] = {
  1428. { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
  1429. { 0 }
  1430. };
  1431. static struct procunit_value_info spdif_format_xu_info[] = {
  1432. { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
  1433. { 0 }
  1434. };
  1435. static struct procunit_value_info soft_limit_xu_info[] = {
  1436. { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
  1437. { 0 }
  1438. };
  1439. static struct procunit_info extunits[] = {
  1440. { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
  1441. { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
  1442. { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
  1443. { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
  1444. { 0 }
  1445. };
  1446. /*
  1447. * build a processing/extension unit
  1448. */
  1449. static int build_audio_procunit(struct mixer_build *state, int unitid, void *raw_desc, struct procunit_info *list, char *name)
  1450. {
  1451. struct uac_processing_unit_descriptor *desc = raw_desc;
  1452. int num_ins = desc->bNrInPins;
  1453. struct usb_mixer_elem_info *cval;
  1454. struct snd_kcontrol *kctl;
  1455. int i, err, nameid, type, len;
  1456. struct procunit_info *info;
  1457. struct procunit_value_info *valinfo;
  1458. const struct usbmix_name_map *map;
  1459. static struct procunit_value_info default_value_info[] = {
  1460. { 0x01, "Switch", USB_MIXER_BOOLEAN },
  1461. { 0 }
  1462. };
  1463. static struct procunit_info default_info = {
  1464. 0, NULL, default_value_info
  1465. };
  1466. if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
  1467. desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
  1468. snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
  1469. return -EINVAL;
  1470. }
  1471. for (i = 0; i < num_ins; i++) {
  1472. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1473. return err;
  1474. }
  1475. type = le16_to_cpu(desc->wProcessType);
  1476. for (info = list; info && info->type; info++)
  1477. if (info->type == type)
  1478. break;
  1479. if (! info || ! info->type)
  1480. info = &default_info;
  1481. for (valinfo = info->values; valinfo->control; valinfo++) {
  1482. __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
  1483. if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
  1484. continue;
  1485. map = find_map(state, unitid, valinfo->control);
  1486. if (check_ignored_ctl(map))
  1487. continue;
  1488. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1489. if (! cval) {
  1490. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1491. return -ENOMEM;
  1492. }
  1493. cval->mixer = state->mixer;
  1494. cval->id = unitid;
  1495. cval->control = valinfo->control;
  1496. cval->val_type = valinfo->val_type;
  1497. cval->channels = 1;
  1498. /* get min/max values */
  1499. if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
  1500. __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
  1501. /* FIXME: hard-coded */
  1502. cval->min = 1;
  1503. cval->max = control_spec[0];
  1504. cval->res = 1;
  1505. cval->initialized = 1;
  1506. } else {
  1507. if (type == USB_XU_CLOCK_RATE) {
  1508. /* E-Mu USB 0404/0202/TrackerPre/0204
  1509. * samplerate control quirk
  1510. */
  1511. cval->min = 0;
  1512. cval->max = 5;
  1513. cval->res = 1;
  1514. cval->initialized = 1;
  1515. } else
  1516. get_min_max(cval, valinfo->min_value);
  1517. }
  1518. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  1519. if (! kctl) {
  1520. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1521. kfree(cval);
  1522. return -ENOMEM;
  1523. }
  1524. kctl->private_free = usb_mixer_elem_free;
  1525. if (check_mapped_name(map, kctl->id.name,
  1526. sizeof(kctl->id.name)))
  1527. /* nothing */ ;
  1528. else if (info->name)
  1529. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  1530. else {
  1531. nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
  1532. len = 0;
  1533. if (nameid)
  1534. len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1535. if (! len)
  1536. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  1537. }
  1538. append_ctl_name(kctl, " ");
  1539. append_ctl_name(kctl, valinfo->suffix);
  1540. snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
  1541. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1542. if ((err = snd_usb_mixer_add_control(state->mixer, kctl)) < 0)
  1543. return err;
  1544. }
  1545. return 0;
  1546. }
  1547. static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1548. {
  1549. return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit");
  1550. }
  1551. static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1552. {
  1553. /* Note that we parse extension units with processing unit descriptors.
  1554. * That's ok as the layout is the same */
  1555. return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit");
  1556. }
  1557. /*
  1558. * Selector Unit
  1559. */
  1560. /* info callback for selector unit
  1561. * use an enumerator type for routing
  1562. */
  1563. static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1564. {
  1565. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1566. const char **itemlist = (const char **)kcontrol->private_value;
  1567. if (snd_BUG_ON(!itemlist))
  1568. return -EINVAL;
  1569. return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
  1570. }
  1571. /* get callback for selector unit */
  1572. static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1573. {
  1574. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1575. int val, err;
  1576. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1577. if (err < 0) {
  1578. if (cval->mixer->ignore_ctl_error) {
  1579. ucontrol->value.enumerated.item[0] = 0;
  1580. return 0;
  1581. }
  1582. return err;
  1583. }
  1584. val = get_relative_value(cval, val);
  1585. ucontrol->value.enumerated.item[0] = val;
  1586. return 0;
  1587. }
  1588. /* put callback for selector unit */
  1589. static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1590. {
  1591. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1592. int val, oval, err;
  1593. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1594. if (err < 0) {
  1595. if (cval->mixer->ignore_ctl_error)
  1596. return 0;
  1597. return err;
  1598. }
  1599. val = ucontrol->value.enumerated.item[0];
  1600. val = get_abs_value(cval, val);
  1601. if (val != oval) {
  1602. set_cur_ctl_value(cval, cval->control << 8, val);
  1603. return 1;
  1604. }
  1605. return 0;
  1606. }
  1607. /* alsa control interface for selector unit */
  1608. static struct snd_kcontrol_new mixer_selectunit_ctl = {
  1609. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1610. .name = "", /* will be filled later */
  1611. .info = mixer_ctl_selector_info,
  1612. .get = mixer_ctl_selector_get,
  1613. .put = mixer_ctl_selector_put,
  1614. };
  1615. /* private free callback.
  1616. * free both private_data and private_value
  1617. */
  1618. static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
  1619. {
  1620. int i, num_ins = 0;
  1621. if (kctl->private_data) {
  1622. struct usb_mixer_elem_info *cval = kctl->private_data;
  1623. num_ins = cval->max;
  1624. kfree(cval);
  1625. kctl->private_data = NULL;
  1626. }
  1627. if (kctl->private_value) {
  1628. char **itemlist = (char **)kctl->private_value;
  1629. for (i = 0; i < num_ins; i++)
  1630. kfree(itemlist[i]);
  1631. kfree(itemlist);
  1632. kctl->private_value = 0;
  1633. }
  1634. }
  1635. /*
  1636. * parse a selector unit
  1637. */
  1638. static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1639. {
  1640. struct uac_selector_unit_descriptor *desc = raw_desc;
  1641. unsigned int i, nameid, len;
  1642. int err;
  1643. struct usb_mixer_elem_info *cval;
  1644. struct snd_kcontrol *kctl;
  1645. const struct usbmix_name_map *map;
  1646. char **namelist;
  1647. if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {
  1648. snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
  1649. return -EINVAL;
  1650. }
  1651. for (i = 0; i < desc->bNrInPins; i++) {
  1652. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1653. return err;
  1654. }
  1655. if (desc->bNrInPins == 1) /* only one ? nonsense! */
  1656. return 0;
  1657. map = find_map(state, unitid, 0);
  1658. if (check_ignored_ctl(map))
  1659. return 0;
  1660. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1661. if (! cval) {
  1662. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1663. return -ENOMEM;
  1664. }
  1665. cval->mixer = state->mixer;
  1666. cval->id = unitid;
  1667. cval->val_type = USB_MIXER_U8;
  1668. cval->channels = 1;
  1669. cval->min = 1;
  1670. cval->max = desc->bNrInPins;
  1671. cval->res = 1;
  1672. cval->initialized = 1;
  1673. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  1674. cval->control = UAC2_CX_CLOCK_SELECTOR;
  1675. else
  1676. cval->control = 0;
  1677. namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
  1678. if (! namelist) {
  1679. snd_printk(KERN_ERR "cannot malloc\n");
  1680. kfree(cval);
  1681. return -ENOMEM;
  1682. }
  1683. #define MAX_ITEM_NAME_LEN 64
  1684. for (i = 0; i < desc->bNrInPins; i++) {
  1685. struct usb_audio_term iterm;
  1686. len = 0;
  1687. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  1688. if (! namelist[i]) {
  1689. snd_printk(KERN_ERR "cannot malloc\n");
  1690. while (i--)
  1691. kfree(namelist[i]);
  1692. kfree(namelist);
  1693. kfree(cval);
  1694. return -ENOMEM;
  1695. }
  1696. len = check_mapped_selector_name(state, unitid, i, namelist[i],
  1697. MAX_ITEM_NAME_LEN);
  1698. if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
  1699. len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
  1700. if (! len)
  1701. sprintf(namelist[i], "Input %d", i);
  1702. }
  1703. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  1704. if (! kctl) {
  1705. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1706. kfree(namelist);
  1707. kfree(cval);
  1708. return -ENOMEM;
  1709. }
  1710. kctl->private_value = (unsigned long)namelist;
  1711. kctl->private_free = usb_mixer_selector_elem_free;
  1712. nameid = uac_selector_unit_iSelector(desc);
  1713. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1714. if (len)
  1715. ;
  1716. else if (nameid)
  1717. snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1718. else {
  1719. len = get_term_name(state, &state->oterm,
  1720. kctl->id.name, sizeof(kctl->id.name), 0);
  1721. if (! len)
  1722. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  1723. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  1724. append_ctl_name(kctl, " Clock Source");
  1725. else if ((state->oterm.type & 0xff00) == 0x0100)
  1726. append_ctl_name(kctl, " Capture Source");
  1727. else
  1728. append_ctl_name(kctl, " Playback Source");
  1729. }
  1730. snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
  1731. cval->id, kctl->id.name, desc->bNrInPins);
  1732. if ((err = snd_usb_mixer_add_control(state->mixer, kctl)) < 0)
  1733. return err;
  1734. return 0;
  1735. }
  1736. /*
  1737. * parse an audio unit recursively
  1738. */
  1739. static int parse_audio_unit(struct mixer_build *state, int unitid)
  1740. {
  1741. unsigned char *p1;
  1742. if (test_and_set_bit(unitid, state->unitbitmap))
  1743. return 0; /* the unit already visited */
  1744. p1 = find_audio_control_unit(state, unitid);
  1745. if (!p1) {
  1746. snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
  1747. return -EINVAL;
  1748. }
  1749. switch (p1[2]) {
  1750. case UAC_INPUT_TERMINAL:
  1751. case UAC2_CLOCK_SOURCE:
  1752. return 0; /* NOP */
  1753. case UAC_MIXER_UNIT:
  1754. return parse_audio_mixer_unit(state, unitid, p1);
  1755. case UAC_SELECTOR_UNIT:
  1756. case UAC2_CLOCK_SELECTOR:
  1757. return parse_audio_selector_unit(state, unitid, p1);
  1758. case UAC_FEATURE_UNIT:
  1759. return parse_audio_feature_unit(state, unitid, p1);
  1760. case UAC1_PROCESSING_UNIT:
  1761. /* UAC2_EFFECT_UNIT has the same value */
  1762. if (state->mixer->protocol == UAC_VERSION_1)
  1763. return parse_audio_processing_unit(state, unitid, p1);
  1764. else
  1765. return 0; /* FIXME - effect units not implemented yet */
  1766. case UAC1_EXTENSION_UNIT:
  1767. /* UAC2_PROCESSING_UNIT_V2 has the same value */
  1768. if (state->mixer->protocol == UAC_VERSION_1)
  1769. return parse_audio_extension_unit(state, unitid, p1);
  1770. else /* UAC_VERSION_2 */
  1771. return parse_audio_processing_unit(state, unitid, p1);
  1772. case UAC2_EXTENSION_UNIT_V2:
  1773. return parse_audio_extension_unit(state, unitid, p1);
  1774. default:
  1775. snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  1776. return -EINVAL;
  1777. }
  1778. }
  1779. static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
  1780. {
  1781. /* kill pending URBs */
  1782. snd_usb_mixer_disconnect(&mixer->list);
  1783. kfree(mixer->id_elems);
  1784. if (mixer->urb) {
  1785. kfree(mixer->urb->transfer_buffer);
  1786. usb_free_urb(mixer->urb);
  1787. }
  1788. usb_free_urb(mixer->rc_urb);
  1789. kfree(mixer->rc_setup_packet);
  1790. kfree(mixer);
  1791. }
  1792. static int snd_usb_mixer_dev_free(struct snd_device *device)
  1793. {
  1794. struct usb_mixer_interface *mixer = device->device_data;
  1795. snd_usb_mixer_free(mixer);
  1796. return 0;
  1797. }
  1798. /*
  1799. * create mixer controls
  1800. *
  1801. * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
  1802. */
  1803. static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
  1804. {
  1805. struct mixer_build state;
  1806. int err;
  1807. const struct usbmix_ctl_map *map;
  1808. void *p;
  1809. memset(&state, 0, sizeof(state));
  1810. state.chip = mixer->chip;
  1811. state.mixer = mixer;
  1812. state.buffer = mixer->hostif->extra;
  1813. state.buflen = mixer->hostif->extralen;
  1814. /* check the mapping table */
  1815. for (map = usbmix_ctl_maps; map->id; map++) {
  1816. if (map->id == state.chip->usb_id) {
  1817. state.map = map->map;
  1818. state.selector_map = map->selector_map;
  1819. mixer->ignore_ctl_error = map->ignore_ctl_error;
  1820. break;
  1821. }
  1822. }
  1823. p = NULL;
  1824. while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, mixer->hostif->extralen,
  1825. p, UAC_OUTPUT_TERMINAL)) != NULL) {
  1826. if (mixer->protocol == UAC_VERSION_1) {
  1827. struct uac1_output_terminal_descriptor *desc = p;
  1828. if (desc->bLength < sizeof(*desc))
  1829. continue; /* invalid descriptor? */
  1830. set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
  1831. state.oterm.id = desc->bTerminalID;
  1832. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  1833. state.oterm.name = desc->iTerminal;
  1834. err = parse_audio_unit(&state, desc->bSourceID);
  1835. if (err < 0 && err != -EINVAL)
  1836. return err;
  1837. } else { /* UAC_VERSION_2 */
  1838. struct uac2_output_terminal_descriptor *desc = p;
  1839. if (desc->bLength < sizeof(*desc))
  1840. continue; /* invalid descriptor? */
  1841. set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
  1842. state.oterm.id = desc->bTerminalID;
  1843. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  1844. state.oterm.name = desc->iTerminal;
  1845. err = parse_audio_unit(&state, desc->bSourceID);
  1846. if (err < 0 && err != -EINVAL)
  1847. return err;
  1848. /* for UAC2, use the same approach to also add the clock selectors */
  1849. err = parse_audio_unit(&state, desc->bCSourceID);
  1850. if (err < 0 && err != -EINVAL)
  1851. return err;
  1852. }
  1853. }
  1854. return 0;
  1855. }
  1856. void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
  1857. {
  1858. struct usb_mixer_elem_info *info;
  1859. for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
  1860. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1861. info->elem_id);
  1862. }
  1863. static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
  1864. int unitid,
  1865. struct usb_mixer_elem_info *cval)
  1866. {
  1867. static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
  1868. "S8", "U8", "S16", "U16"};
  1869. snd_iprintf(buffer, " Unit: %i\n", unitid);
  1870. if (cval->elem_id)
  1871. snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
  1872. cval->elem_id->name, cval->elem_id->index);
  1873. snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
  1874. "channels=%i, type=\"%s\"\n", cval->id,
  1875. cval->control, cval->cmask, cval->channels,
  1876. val_types[cval->val_type]);
  1877. snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
  1878. cval->min, cval->max, cval->dBmin, cval->dBmax);
  1879. }
  1880. static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
  1881. struct snd_info_buffer *buffer)
  1882. {
  1883. struct snd_usb_audio *chip = entry->private_data;
  1884. struct usb_mixer_interface *mixer;
  1885. struct usb_mixer_elem_info *cval;
  1886. int unitid;
  1887. list_for_each_entry(mixer, &chip->mixer_list, list) {
  1888. snd_iprintf(buffer,
  1889. "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
  1890. chip->usb_id, snd_usb_ctrl_intf(chip),
  1891. mixer->ignore_ctl_error);
  1892. snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
  1893. for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
  1894. for (cval = mixer->id_elems[unitid]; cval;
  1895. cval = cval->next_id_elem)
  1896. snd_usb_mixer_dump_cval(buffer, unitid, cval);
  1897. }
  1898. }
  1899. }
  1900. static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
  1901. int attribute, int value, int index)
  1902. {
  1903. struct usb_mixer_elem_info *info;
  1904. __u8 unitid = (index >> 8) & 0xff;
  1905. __u8 control = (value >> 8) & 0xff;
  1906. __u8 channel = value & 0xff;
  1907. if (channel >= MAX_CHANNELS) {
  1908. snd_printk(KERN_DEBUG "%s(): bogus channel number %d\n",
  1909. __func__, channel);
  1910. return;
  1911. }
  1912. for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) {
  1913. if (info->control != control)
  1914. continue;
  1915. switch (attribute) {
  1916. case UAC2_CS_CUR:
  1917. /* invalidate cache, so the value is read from the device */
  1918. if (channel)
  1919. info->cached &= ~(1 << channel);
  1920. else /* master channel */
  1921. info->cached = 0;
  1922. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1923. info->elem_id);
  1924. break;
  1925. case UAC2_CS_RANGE:
  1926. /* TODO */
  1927. break;
  1928. case UAC2_CS_MEM:
  1929. /* TODO */
  1930. break;
  1931. default:
  1932. snd_printk(KERN_DEBUG "unknown attribute %d in interrupt\n",
  1933. attribute);
  1934. break;
  1935. } /* switch */
  1936. }
  1937. }
  1938. static void snd_usb_mixer_interrupt(struct urb *urb)
  1939. {
  1940. struct usb_mixer_interface *mixer = urb->context;
  1941. int len = urb->actual_length;
  1942. int ustatus = urb->status;
  1943. if (ustatus != 0)
  1944. goto requeue;
  1945. if (mixer->protocol == UAC_VERSION_1) {
  1946. struct uac1_status_word *status;
  1947. for (status = urb->transfer_buffer;
  1948. len >= sizeof(*status);
  1949. len -= sizeof(*status), status++) {
  1950. snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
  1951. status->bStatusType,
  1952. status->bOriginator);
  1953. /* ignore any notifications not from the control interface */
  1954. if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
  1955. UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
  1956. continue;
  1957. if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
  1958. snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
  1959. else
  1960. snd_usb_mixer_notify_id(mixer, status->bOriginator);
  1961. }
  1962. } else { /* UAC_VERSION_2 */
  1963. struct uac2_interrupt_data_msg *msg;
  1964. for (msg = urb->transfer_buffer;
  1965. len >= sizeof(*msg);
  1966. len -= sizeof(*msg), msg++) {
  1967. /* drop vendor specific and endpoint requests */
  1968. if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
  1969. (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
  1970. continue;
  1971. snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
  1972. le16_to_cpu(msg->wValue),
  1973. le16_to_cpu(msg->wIndex));
  1974. }
  1975. }
  1976. requeue:
  1977. if (ustatus != -ENOENT && ustatus != -ECONNRESET && ustatus != -ESHUTDOWN) {
  1978. urb->dev = mixer->chip->dev;
  1979. usb_submit_urb(urb, GFP_ATOMIC);
  1980. }
  1981. }
  1982. /* stop any bus activity of a mixer */
  1983. void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
  1984. {
  1985. usb_kill_urb(mixer->urb);
  1986. usb_kill_urb(mixer->rc_urb);
  1987. }
  1988. int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
  1989. {
  1990. int err;
  1991. if (mixer->urb) {
  1992. err = usb_submit_urb(mixer->urb, GFP_NOIO);
  1993. if (err < 0)
  1994. return err;
  1995. }
  1996. return 0;
  1997. }
  1998. /* create the handler for the optional status interrupt endpoint */
  1999. static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
  2000. {
  2001. struct usb_endpoint_descriptor *ep;
  2002. void *transfer_buffer;
  2003. int buffer_length;
  2004. unsigned int epnum;
  2005. /* we need one interrupt input endpoint */
  2006. if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
  2007. return 0;
  2008. ep = get_endpoint(mixer->hostif, 0);
  2009. if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
  2010. return 0;
  2011. epnum = usb_endpoint_num(ep);
  2012. buffer_length = le16_to_cpu(ep->wMaxPacketSize);
  2013. transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
  2014. if (!transfer_buffer)
  2015. return -ENOMEM;
  2016. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  2017. if (!mixer->urb) {
  2018. kfree(transfer_buffer);
  2019. return -ENOMEM;
  2020. }
  2021. usb_fill_int_urb(mixer->urb, mixer->chip->dev,
  2022. usb_rcvintpipe(mixer->chip->dev, epnum),
  2023. transfer_buffer, buffer_length,
  2024. snd_usb_mixer_interrupt, mixer, ep->bInterval);
  2025. usb_submit_urb(mixer->urb, GFP_KERNEL);
  2026. return 0;
  2027. }
  2028. int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
  2029. int ignore_error)
  2030. {
  2031. static struct snd_device_ops dev_ops = {
  2032. .dev_free = snd_usb_mixer_dev_free
  2033. };
  2034. struct usb_mixer_interface *mixer;
  2035. struct snd_info_entry *entry;
  2036. int err;
  2037. strcpy(chip->card->mixername, "USB Mixer");
  2038. mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
  2039. if (!mixer)
  2040. return -ENOMEM;
  2041. mixer->chip = chip;
  2042. mixer->ignore_ctl_error = ignore_error;
  2043. mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
  2044. GFP_KERNEL);
  2045. if (!mixer->id_elems) {
  2046. kfree(mixer);
  2047. return -ENOMEM;
  2048. }
  2049. mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
  2050. switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
  2051. case UAC_VERSION_1:
  2052. default:
  2053. mixer->protocol = UAC_VERSION_1;
  2054. break;
  2055. case UAC_VERSION_2:
  2056. mixer->protocol = UAC_VERSION_2;
  2057. break;
  2058. }
  2059. if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
  2060. (err = snd_usb_mixer_status_create(mixer)) < 0)
  2061. goto _error;
  2062. snd_usb_mixer_apply_create_quirk(mixer);
  2063. err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
  2064. if (err < 0)
  2065. goto _error;
  2066. if (list_empty(&chip->mixer_list) &&
  2067. !snd_card_proc_new(chip->card, "usbmixer", &entry))
  2068. snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
  2069. list_add(&mixer->list, &chip->mixer_list);
  2070. return 0;
  2071. _error:
  2072. snd_usb_mixer_free(mixer);
  2073. return err;
  2074. }
  2075. void snd_usb_mixer_disconnect(struct list_head *p)
  2076. {
  2077. struct usb_mixer_interface *mixer;
  2078. mixer = list_entry(p, struct usb_mixer_interface, list);
  2079. if (mixer->disconnected)
  2080. return;
  2081. if (mixer->urb)
  2082. usb_kill_urb(mixer->urb);
  2083. if (mixer->rc_urb)
  2084. usb_kill_urb(mixer->rc_urb);
  2085. mixer->disconnected = true;
  2086. }