usbmixer.c 54 KB

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