usbmixer.c 57 KB

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