usbmixer.c 52 KB

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