mixer.c 59 KB

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