usbmixer.c 59 KB

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