mixer.c 70 KB

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