usbmixer.c 56 KB

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