usbmixer.c 61 KB

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