usbmixer.c 54 KB

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