mixer.c 68 KB

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