mixer.c 60 KB

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