mixer.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  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. /*
  29. * TODOs, for both the mixer and the streaming interfaces:
  30. *
  31. * - support for UAC2 effect units
  32. * - support for graphical equalizers
  33. * - RANGE and MEM set commands (UAC2)
  34. * - RANGE and MEM interrupt dispatchers (UAC2)
  35. * - audio channel clustering (UAC2)
  36. * - audio sample rate converter units (UAC2)
  37. * - proper handling of clock multipliers (UAC2)
  38. * - dispatch clock change notifications (UAC2)
  39. * - stop PCM streams which use a clock that became invalid
  40. * - stop PCM streams which use a clock selector that has changed
  41. * - parse available sample rates again when clock sources changed
  42. */
  43. #include <linux/bitops.h>
  44. #include <linux/init.h>
  45. #include <linux/list.h>
  46. #include <linux/slab.h>
  47. #include <linux/string.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/audio.h>
  50. #include <linux/usb/audio-v2.h>
  51. #include <sound/core.h>
  52. #include <sound/control.h>
  53. #include <sound/hwdep.h>
  54. #include <sound/info.h>
  55. #include <sound/tlv.h>
  56. #include "usbaudio.h"
  57. #include "mixer.h"
  58. #include "helper.h"
  59. #include "mixer_quirks.h"
  60. #include "power.h"
  61. #define MAX_ID_ELEMS 256
  62. struct usb_audio_term {
  63. int id;
  64. int type;
  65. int channels;
  66. unsigned int chconfig;
  67. int name;
  68. };
  69. struct usbmix_name_map;
  70. struct mixer_build {
  71. struct snd_usb_audio *chip;
  72. struct usb_mixer_interface *mixer;
  73. unsigned char *buffer;
  74. unsigned int buflen;
  75. DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
  76. struct usb_audio_term oterm;
  77. const struct usbmix_name_map *map;
  78. const struct usbmix_selector_map *selector_map;
  79. };
  80. /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
  81. enum {
  82. USB_XU_CLOCK_RATE = 0xe301,
  83. USB_XU_CLOCK_SOURCE = 0xe302,
  84. USB_XU_DIGITAL_IO_STATUS = 0xe303,
  85. USB_XU_DEVICE_OPTIONS = 0xe304,
  86. USB_XU_DIRECT_MONITORING = 0xe305,
  87. USB_XU_METERING = 0xe306
  88. };
  89. enum {
  90. USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
  91. USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
  92. USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
  93. USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
  94. };
  95. /*
  96. * manual mapping of mixer names
  97. * if the mixer topology is too complicated and the parsed names are
  98. * ambiguous, add the entries in usbmixer_maps.c.
  99. */
  100. #include "mixer_maps.c"
  101. static const struct usbmix_name_map *
  102. find_map(struct mixer_build *state, int unitid, int control)
  103. {
  104. const struct usbmix_name_map *p = state->map;
  105. if (!p)
  106. return NULL;
  107. for (p = state->map; p->id; p++) {
  108. if (p->id == unitid &&
  109. (!control || !p->control || control == p->control))
  110. return p;
  111. }
  112. return NULL;
  113. }
  114. /* get the mapped name if the unit matches */
  115. static int
  116. check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
  117. {
  118. if (!p || !p->name)
  119. return 0;
  120. buflen--;
  121. return strlcpy(buf, p->name, buflen);
  122. }
  123. /* check whether the control should be ignored */
  124. static inline int
  125. check_ignored_ctl(const struct usbmix_name_map *p)
  126. {
  127. if (!p || p->name || p->dB)
  128. return 0;
  129. return 1;
  130. }
  131. /* dB mapping */
  132. static inline void check_mapped_dB(const struct usbmix_name_map *p,
  133. struct usb_mixer_elem_info *cval)
  134. {
  135. if (p && p->dB) {
  136. cval->dBmin = p->dB->min;
  137. cval->dBmax = p->dB->max;
  138. cval->initialized = 1;
  139. }
  140. }
  141. /* get the mapped selector source name */
  142. static int check_mapped_selector_name(struct mixer_build *state, int unitid,
  143. int index, char *buf, int buflen)
  144. {
  145. const struct usbmix_selector_map *p;
  146. if (! state->selector_map)
  147. return 0;
  148. for (p = state->selector_map; p->id; p++) {
  149. if (p->id == unitid && index < p->count)
  150. return strlcpy(buf, p->names[index], buflen);
  151. }
  152. return 0;
  153. }
  154. /*
  155. * find an audio control unit with the given unit id
  156. */
  157. static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
  158. {
  159. /* we just parse the header */
  160. struct uac_feature_unit_descriptor *hdr = NULL;
  161. while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
  162. USB_DT_CS_INTERFACE)) != NULL) {
  163. if (hdr->bLength >= 4 &&
  164. hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
  165. hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER &&
  166. hdr->bUnitID == unit)
  167. return hdr;
  168. }
  169. return NULL;
  170. }
  171. /*
  172. * copy a string with the given id
  173. */
  174. static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
  175. {
  176. int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
  177. buf[len] = 0;
  178. return len;
  179. }
  180. /*
  181. * convert from the byte/word on usb descriptor to the zero-based integer
  182. */
  183. static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
  184. {
  185. switch (cval->val_type) {
  186. case USB_MIXER_BOOLEAN:
  187. return !!val;
  188. case USB_MIXER_INV_BOOLEAN:
  189. return !val;
  190. case USB_MIXER_U8:
  191. val &= 0xff;
  192. break;
  193. case USB_MIXER_S8:
  194. val &= 0xff;
  195. if (val >= 0x80)
  196. val -= 0x100;
  197. break;
  198. case USB_MIXER_U16:
  199. val &= 0xffff;
  200. break;
  201. case USB_MIXER_S16:
  202. val &= 0xffff;
  203. if (val >= 0x8000)
  204. val -= 0x10000;
  205. break;
  206. }
  207. return val;
  208. }
  209. /*
  210. * convert from the zero-based int to the byte/word for usb descriptor
  211. */
  212. static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
  213. {
  214. switch (cval->val_type) {
  215. case USB_MIXER_BOOLEAN:
  216. return !!val;
  217. case USB_MIXER_INV_BOOLEAN:
  218. return !val;
  219. case USB_MIXER_S8:
  220. case USB_MIXER_U8:
  221. return val & 0xff;
  222. case USB_MIXER_S16:
  223. case USB_MIXER_U16:
  224. return val & 0xffff;
  225. }
  226. return 0; /* not reached */
  227. }
  228. static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
  229. {
  230. if (! cval->res)
  231. cval->res = 1;
  232. if (val < cval->min)
  233. return 0;
  234. else if (val >= cval->max)
  235. return (cval->max - cval->min + cval->res - 1) / cval->res;
  236. else
  237. return (val - cval->min) / cval->res;
  238. }
  239. static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
  240. {
  241. if (val < 0)
  242. return cval->min;
  243. if (! cval->res)
  244. cval->res = 1;
  245. val *= cval->res;
  246. val += cval->min;
  247. if (val > cval->max)
  248. return cval->max;
  249. return val;
  250. }
  251. /*
  252. * retrieve a mixer value
  253. */
  254. static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
  255. {
  256. struct snd_usb_audio *chip = cval->mixer->chip;
  257. unsigned char buf[2];
  258. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  259. int timeout = 10;
  260. int idx = 0, err;
  261. err = snd_usb_autoresume(cval->mixer->chip);
  262. if (err < 0)
  263. return -EIO;
  264. down_read(&chip->shutdown_rwsem);
  265. while (timeout-- > 0) {
  266. if (chip->shutdown)
  267. break;
  268. idx = snd_usb_ctrl_intf(chip) | (cval->id << 8);
  269. if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
  270. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  271. validx, idx, buf, val_len) >= val_len) {
  272. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
  273. err = 0;
  274. goto out;
  275. }
  276. }
  277. snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  278. request, validx, idx, cval->val_type);
  279. err = -EINVAL;
  280. out:
  281. up_read(&chip->shutdown_rwsem);
  282. snd_usb_autosuspend(cval->mixer->chip);
  283. return err;
  284. }
  285. static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
  286. {
  287. struct snd_usb_audio *chip = cval->mixer->chip;
  288. unsigned char buf[2 + 3*sizeof(__u16)]; /* enough space for one range */
  289. unsigned char *val;
  290. int idx = 0, ret, size;
  291. __u8 bRequest;
  292. if (request == UAC_GET_CUR) {
  293. bRequest = UAC2_CS_CUR;
  294. size = sizeof(__u16);
  295. } else {
  296. bRequest = UAC2_CS_RANGE;
  297. size = sizeof(buf);
  298. }
  299. memset(buf, 0, sizeof(buf));
  300. ret = snd_usb_autoresume(chip) ? -EIO : 0;
  301. if (ret)
  302. goto error;
  303. down_read(&chip->shutdown_rwsem);
  304. if (chip->shutdown)
  305. ret = -ENODEV;
  306. else {
  307. idx = snd_usb_ctrl_intf(chip) | (cval->id << 8);
  308. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
  309. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  310. validx, idx, buf, size);
  311. }
  312. up_read(&chip->shutdown_rwsem);
  313. snd_usb_autosuspend(chip);
  314. if (ret < 0) {
  315. error:
  316. snd_printk(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  317. request, validx, idx, cval->val_type);
  318. return ret;
  319. }
  320. /* FIXME: how should we handle multiple triplets here? */
  321. switch (request) {
  322. case UAC_GET_CUR:
  323. val = buf;
  324. break;
  325. case UAC_GET_MIN:
  326. val = buf + sizeof(__u16);
  327. break;
  328. case UAC_GET_MAX:
  329. val = buf + sizeof(__u16) * 2;
  330. break;
  331. case UAC_GET_RES:
  332. val = buf + sizeof(__u16) * 3;
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16)));
  338. return 0;
  339. }
  340. static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
  341. {
  342. validx += cval->idx_off;
  343. return (cval->mixer->protocol == UAC_VERSION_1) ?
  344. get_ctl_value_v1(cval, request, validx, value_ret) :
  345. get_ctl_value_v2(cval, request, validx, value_ret);
  346. }
  347. static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
  348. {
  349. return get_ctl_value(cval, UAC_GET_CUR, validx, value);
  350. }
  351. /* channel = 0: master, 1 = first channel */
  352. static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
  353. int channel, int *value)
  354. {
  355. return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value);
  356. }
  357. static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
  358. int channel, int index, int *value)
  359. {
  360. int err;
  361. if (cval->cached & (1 << channel)) {
  362. *value = cval->cache_val[index];
  363. return 0;
  364. }
  365. err = get_cur_mix_raw(cval, channel, value);
  366. if (err < 0) {
  367. if (!cval->mixer->ignore_ctl_error)
  368. snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n",
  369. cval->control, channel, err);
  370. return err;
  371. }
  372. cval->cached |= 1 << channel;
  373. cval->cache_val[index] = *value;
  374. return 0;
  375. }
  376. /*
  377. * set a mixer value
  378. */
  379. int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
  380. int request, int validx, int value_set)
  381. {
  382. struct snd_usb_audio *chip = cval->mixer->chip;
  383. unsigned char buf[2];
  384. int idx = 0, val_len, err, timeout = 10;
  385. validx += cval->idx_off;
  386. if (cval->mixer->protocol == UAC_VERSION_1) {
  387. val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  388. } else { /* UAC_VERSION_2 */
  389. /* audio class v2 controls are always 2 bytes in size */
  390. val_len = sizeof(__u16);
  391. /* FIXME */
  392. if (request != UAC_SET_CUR) {
  393. snd_printdd(KERN_WARNING "RANGE setting not yet supported\n");
  394. return -EINVAL;
  395. }
  396. request = UAC2_CS_CUR;
  397. }
  398. value_set = convert_bytes_value(cval, value_set);
  399. buf[0] = value_set & 0xff;
  400. buf[1] = (value_set >> 8) & 0xff;
  401. err = snd_usb_autoresume(chip);
  402. if (err < 0)
  403. return -EIO;
  404. down_read(&chip->shutdown_rwsem);
  405. while (timeout-- > 0) {
  406. if (chip->shutdown)
  407. break;
  408. idx = snd_usb_ctrl_intf(chip) | (cval->id << 8);
  409. if (snd_usb_ctl_msg(chip->dev,
  410. usb_sndctrlpipe(chip->dev, 0), request,
  411. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  412. validx, idx, buf, val_len) >= 0) {
  413. err = 0;
  414. goto out;
  415. }
  416. }
  417. snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
  418. request, validx, idx, cval->val_type, buf[0], buf[1]);
  419. err = -EINVAL;
  420. out:
  421. up_read(&chip->shutdown_rwsem);
  422. snd_usb_autosuspend(chip);
  423. return err;
  424. }
  425. static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
  426. {
  427. return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
  428. }
  429. static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
  430. int index, int value)
  431. {
  432. int err;
  433. unsigned int read_only = (channel == 0) ?
  434. cval->master_readonly :
  435. cval->ch_readonly & (1 << (channel - 1));
  436. if (read_only) {
  437. snd_printdd(KERN_INFO "%s(): channel %d of control %d is read_only\n",
  438. __func__, channel, cval->control);
  439. return 0;
  440. }
  441. err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,
  442. value);
  443. if (err < 0)
  444. return err;
  445. cval->cached |= 1 << channel;
  446. cval->cache_val[index] = value;
  447. return 0;
  448. }
  449. /*
  450. * TLV callback for mixer volume controls
  451. */
  452. int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  453. unsigned int size, unsigned int __user *_tlv)
  454. {
  455. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  456. DECLARE_TLV_DB_MINMAX(scale, 0, 0);
  457. if (size < sizeof(scale))
  458. return -ENOMEM;
  459. scale[2] = cval->dBmin;
  460. scale[3] = cval->dBmax;
  461. if (copy_to_user(_tlv, scale, sizeof(scale)))
  462. return -EFAULT;
  463. return 0;
  464. }
  465. /*
  466. * parser routines begin here...
  467. */
  468. static int parse_audio_unit(struct mixer_build *state, int unitid);
  469. /*
  470. * check if the input/output channel routing is enabled on the given bitmap.
  471. * used for mixer unit parser
  472. */
  473. static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
  474. {
  475. int idx = ich * num_outs + och;
  476. return bmap[idx >> 3] & (0x80 >> (idx & 7));
  477. }
  478. /*
  479. * add an alsa control element
  480. * search and increment the index until an empty slot is found.
  481. *
  482. * if failed, give up and free the control instance.
  483. */
  484. int snd_usb_mixer_add_control(struct usb_mixer_interface *mixer,
  485. struct snd_kcontrol *kctl)
  486. {
  487. struct usb_mixer_elem_info *cval = kctl->private_data;
  488. int err;
  489. while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
  490. kctl->id.index++;
  491. if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
  492. snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
  493. return err;
  494. }
  495. cval->elem_id = &kctl->id;
  496. cval->next_id_elem = mixer->id_elems[cval->id];
  497. mixer->id_elems[cval->id] = cval;
  498. return 0;
  499. }
  500. /*
  501. * get a terminal name string
  502. */
  503. static struct iterm_name_combo {
  504. int type;
  505. char *name;
  506. } iterm_names[] = {
  507. { 0x0300, "Output" },
  508. { 0x0301, "Speaker" },
  509. { 0x0302, "Headphone" },
  510. { 0x0303, "HMD Audio" },
  511. { 0x0304, "Desktop Speaker" },
  512. { 0x0305, "Room Speaker" },
  513. { 0x0306, "Com Speaker" },
  514. { 0x0307, "LFE" },
  515. { 0x0600, "External In" },
  516. { 0x0601, "Analog In" },
  517. { 0x0602, "Digital In" },
  518. { 0x0603, "Line" },
  519. { 0x0604, "Legacy In" },
  520. { 0x0605, "IEC958 In" },
  521. { 0x0606, "1394 DA Stream" },
  522. { 0x0607, "1394 DV Stream" },
  523. { 0x0700, "Embedded" },
  524. { 0x0701, "Noise Source" },
  525. { 0x0702, "Equalization Noise" },
  526. { 0x0703, "CD" },
  527. { 0x0704, "DAT" },
  528. { 0x0705, "DCC" },
  529. { 0x0706, "MiniDisk" },
  530. { 0x0707, "Analog Tape" },
  531. { 0x0708, "Phonograph" },
  532. { 0x0709, "VCR Audio" },
  533. { 0x070a, "Video Disk Audio" },
  534. { 0x070b, "DVD Audio" },
  535. { 0x070c, "TV Tuner Audio" },
  536. { 0x070d, "Satellite Rec Audio" },
  537. { 0x070e, "Cable Tuner Audio" },
  538. { 0x070f, "DSS Audio" },
  539. { 0x0710, "Radio Receiver" },
  540. { 0x0711, "Radio Transmitter" },
  541. { 0x0712, "Multi-Track Recorder" },
  542. { 0x0713, "Synthesizer" },
  543. { 0 },
  544. };
  545. static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
  546. unsigned char *name, int maxlen, int term_only)
  547. {
  548. struct iterm_name_combo *names;
  549. if (iterm->name)
  550. return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
  551. /* virtual type - not a real terminal */
  552. if (iterm->type >> 16) {
  553. if (term_only)
  554. return 0;
  555. switch (iterm->type >> 16) {
  556. case UAC_SELECTOR_UNIT:
  557. strcpy(name, "Selector"); return 8;
  558. case UAC1_PROCESSING_UNIT:
  559. strcpy(name, "Process Unit"); return 12;
  560. case UAC1_EXTENSION_UNIT:
  561. strcpy(name, "Ext Unit"); return 8;
  562. case UAC_MIXER_UNIT:
  563. strcpy(name, "Mixer"); return 5;
  564. default:
  565. return sprintf(name, "Unit %d", iterm->id);
  566. }
  567. }
  568. switch (iterm->type & 0xff00) {
  569. case 0x0100:
  570. strcpy(name, "PCM"); return 3;
  571. case 0x0200:
  572. strcpy(name, "Mic"); return 3;
  573. case 0x0400:
  574. strcpy(name, "Headset"); return 7;
  575. case 0x0500:
  576. strcpy(name, "Phone"); return 5;
  577. }
  578. for (names = iterm_names; names->type; names++)
  579. if (names->type == iterm->type) {
  580. strcpy(name, names->name);
  581. return strlen(names->name);
  582. }
  583. return 0;
  584. }
  585. /*
  586. * parse the source unit recursively until it reaches to a terminal
  587. * or a branched unit.
  588. */
  589. static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
  590. {
  591. int err;
  592. void *p1;
  593. memset(term, 0, sizeof(*term));
  594. while ((p1 = find_audio_control_unit(state, id)) != NULL) {
  595. unsigned char *hdr = p1;
  596. term->id = id;
  597. switch (hdr[2]) {
  598. case UAC_INPUT_TERMINAL:
  599. if (state->mixer->protocol == UAC_VERSION_1) {
  600. struct uac_input_terminal_descriptor *d = p1;
  601. term->type = le16_to_cpu(d->wTerminalType);
  602. term->channels = d->bNrChannels;
  603. term->chconfig = le16_to_cpu(d->wChannelConfig);
  604. term->name = d->iTerminal;
  605. } else { /* UAC_VERSION_2 */
  606. struct uac2_input_terminal_descriptor *d = p1;
  607. term->type = le16_to_cpu(d->wTerminalType);
  608. term->channels = d->bNrChannels;
  609. term->chconfig = le32_to_cpu(d->bmChannelConfig);
  610. term->name = d->iTerminal;
  611. /* call recursively to get the clock selectors */
  612. err = check_input_term(state, d->bCSourceID, term);
  613. if (err < 0)
  614. return err;
  615. }
  616. return 0;
  617. case UAC_FEATURE_UNIT: {
  618. /* the header is the same for v1 and v2 */
  619. struct uac_feature_unit_descriptor *d = p1;
  620. id = d->bSourceID;
  621. break; /* continue to parse */
  622. }
  623. case UAC_MIXER_UNIT: {
  624. struct uac_mixer_unit_descriptor *d = p1;
  625. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  626. term->channels = uac_mixer_unit_bNrChannels(d);
  627. term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
  628. term->name = uac_mixer_unit_iMixer(d);
  629. return 0;
  630. }
  631. case UAC_SELECTOR_UNIT:
  632. case UAC2_CLOCK_SELECTOR: {
  633. struct uac_selector_unit_descriptor *d = p1;
  634. /* call recursively to retrieve the channel info */
  635. if (check_input_term(state, d->baSourceID[0], term) < 0)
  636. return -ENODEV;
  637. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  638. term->id = id;
  639. term->name = uac_selector_unit_iSelector(d);
  640. return 0;
  641. }
  642. case UAC1_PROCESSING_UNIT:
  643. case UAC1_EXTENSION_UNIT:
  644. /* UAC2_PROCESSING_UNIT_V2 */
  645. /* UAC2_EFFECT_UNIT */ {
  646. struct uac_processing_unit_descriptor *d = p1;
  647. if (state->mixer->protocol == UAC_VERSION_2 &&
  648. hdr[2] == UAC2_EFFECT_UNIT) {
  649. /* UAC2/UAC1 unit IDs overlap here in an
  650. * uncompatible way. Ignore this unit for now.
  651. */
  652. return 0;
  653. }
  654. if (d->bNrInPins) {
  655. id = d->baSourceID[0];
  656. break; /* continue to parse */
  657. }
  658. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  659. term->channels = uac_processing_unit_bNrChannels(d);
  660. term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol);
  661. term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol);
  662. return 0;
  663. }
  664. case UAC2_CLOCK_SOURCE: {
  665. struct uac_clock_source_descriptor *d = p1;
  666. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  667. term->id = id;
  668. term->name = d->iClockSource;
  669. return 0;
  670. }
  671. default:
  672. return -ENODEV;
  673. }
  674. }
  675. return -ENODEV;
  676. }
  677. /*
  678. * Feature Unit
  679. */
  680. /* feature unit control information */
  681. struct usb_feature_control_info {
  682. const char *name;
  683. unsigned int type; /* control type (mute, volume, etc.) */
  684. };
  685. static struct usb_feature_control_info audio_feature_info[] = {
  686. { "Mute", USB_MIXER_INV_BOOLEAN },
  687. { "Volume", USB_MIXER_S16 },
  688. { "Tone Control - Bass", USB_MIXER_S8 },
  689. { "Tone Control - Mid", USB_MIXER_S8 },
  690. { "Tone Control - Treble", USB_MIXER_S8 },
  691. { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
  692. { "Auto Gain Control", USB_MIXER_BOOLEAN },
  693. { "Delay Control", USB_MIXER_U16 },
  694. { "Bass Boost", USB_MIXER_BOOLEAN },
  695. { "Loudness", USB_MIXER_BOOLEAN },
  696. /* UAC2 specific */
  697. { "Input Gain Control", USB_MIXER_U16 },
  698. { "Input Gain Pad Control", USB_MIXER_BOOLEAN },
  699. { "Phase Inverter Control", USB_MIXER_BOOLEAN },
  700. };
  701. /* private_free callback */
  702. static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
  703. {
  704. kfree(kctl->private_data);
  705. kctl->private_data = NULL;
  706. }
  707. /*
  708. * interface to ALSA control for feature/mixer units
  709. */
  710. /* volume control quirks */
  711. static void volume_control_quirks(struct usb_mixer_elem_info *cval,
  712. struct snd_kcontrol *kctl)
  713. {
  714. switch (cval->mixer->chip->usb_id) {
  715. case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
  716. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  717. cval->min = 0x0000;
  718. cval->max = 0xffff;
  719. cval->res = 0x00e6;
  720. break;
  721. }
  722. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  723. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  724. cval->min = 0x00;
  725. cval->max = 0xff;
  726. break;
  727. }
  728. if (strstr(kctl->id.name, "Effect Return") != NULL) {
  729. cval->min = 0xb706;
  730. cval->max = 0xff7b;
  731. cval->res = 0x0073;
  732. break;
  733. }
  734. if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
  735. (strstr(kctl->id.name, "Effect Send") != NULL)) {
  736. cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
  737. cval->max = 0xfcfe;
  738. cval->res = 0x0073;
  739. }
  740. break;
  741. case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
  742. case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
  743. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  744. snd_printk(KERN_INFO
  745. "usb-audio: set quirk for FTU Effect Duration\n");
  746. cval->min = 0x0000;
  747. cval->max = 0x7f00;
  748. cval->res = 0x0100;
  749. break;
  750. }
  751. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  752. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  753. snd_printk(KERN_INFO
  754. "usb-audio: set quirks for FTU Effect Feedback/Volume\n");
  755. cval->min = 0x00;
  756. cval->max = 0x7f;
  757. break;
  758. }
  759. break;
  760. case USB_ID(0x0471, 0x0101):
  761. case USB_ID(0x0471, 0x0104):
  762. case USB_ID(0x0471, 0x0105):
  763. case USB_ID(0x0672, 0x1041):
  764. /* quirk for UDA1321/N101.
  765. * note that detection between firmware 2.1.1.7 (N101)
  766. * and later 2.1.1.21 is not very clear from datasheets.
  767. * I hope that the min value is -15360 for newer firmware --jk
  768. */
  769. if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
  770. cval->min == -15616) {
  771. snd_printk(KERN_INFO
  772. "set volume quirk for UDA1321/N101 chip\n");
  773. cval->max = -256;
  774. }
  775. break;
  776. case USB_ID(0x046d, 0x09a4):
  777. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  778. snd_printk(KERN_INFO
  779. "set volume quirk for QuickCam E3500\n");
  780. cval->min = 6080;
  781. cval->max = 8768;
  782. cval->res = 192;
  783. }
  784. break;
  785. case USB_ID(0x046d, 0x0808):
  786. case USB_ID(0x046d, 0x0809):
  787. case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
  788. case USB_ID(0x046d, 0x0991):
  789. /* Most audio usb devices lie about volume resolution.
  790. * Most Logitech webcams have res = 384.
  791. * Proboly there is some logitech magic behind this number --fishor
  792. */
  793. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  794. snd_printk(KERN_INFO
  795. "set resolution quirk: cval->res = 384\n");
  796. cval->res = 384;
  797. }
  798. break;
  799. }
  800. }
  801. /*
  802. * retrieve the minimum and maximum values for the specified control
  803. */
  804. static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
  805. int default_min, struct snd_kcontrol *kctl)
  806. {
  807. /* for failsafe */
  808. cval->min = default_min;
  809. cval->max = cval->min + 1;
  810. cval->res = 1;
  811. cval->dBmin = cval->dBmax = 0;
  812. if (cval->val_type == USB_MIXER_BOOLEAN ||
  813. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  814. cval->initialized = 1;
  815. } else {
  816. int minchn = 0;
  817. if (cval->cmask) {
  818. int i;
  819. for (i = 0; i < MAX_CHANNELS; i++)
  820. if (cval->cmask & (1 << i)) {
  821. minchn = i + 1;
  822. break;
  823. }
  824. }
  825. if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
  826. get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
  827. snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
  828. cval->id, snd_usb_ctrl_intf(cval->mixer->chip), cval->control, cval->id);
  829. return -EINVAL;
  830. }
  831. if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
  832. cval->res = 1;
  833. } else {
  834. int last_valid_res = cval->res;
  835. while (cval->res > 1) {
  836. if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
  837. (cval->control << 8) | minchn, cval->res / 2) < 0)
  838. break;
  839. cval->res /= 2;
  840. }
  841. if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
  842. cval->res = last_valid_res;
  843. }
  844. if (cval->res == 0)
  845. cval->res = 1;
  846. /* Additional checks for the proper resolution
  847. *
  848. * Some devices report smaller resolutions than actually
  849. * reacting. They don't return errors but simply clip
  850. * to the lower aligned value.
  851. */
  852. if (cval->min + cval->res < cval->max) {
  853. int last_valid_res = cval->res;
  854. int saved, test, check;
  855. get_cur_mix_raw(cval, minchn, &saved);
  856. for (;;) {
  857. test = saved;
  858. if (test < cval->max)
  859. test += cval->res;
  860. else
  861. test -= cval->res;
  862. if (test < cval->min || test > cval->max ||
  863. set_cur_mix_value(cval, minchn, 0, test) ||
  864. get_cur_mix_raw(cval, minchn, &check)) {
  865. cval->res = last_valid_res;
  866. break;
  867. }
  868. if (test == check)
  869. break;
  870. cval->res *= 2;
  871. }
  872. set_cur_mix_value(cval, minchn, 0, saved);
  873. }
  874. cval->initialized = 1;
  875. }
  876. if (kctl)
  877. volume_control_quirks(cval, kctl);
  878. /* USB descriptions contain the dB scale in 1/256 dB unit
  879. * while ALSA TLV contains in 1/100 dB unit
  880. */
  881. cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
  882. cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
  883. if (cval->dBmin > cval->dBmax) {
  884. /* something is wrong; assume it's either from/to 0dB */
  885. if (cval->dBmin < 0)
  886. cval->dBmax = 0;
  887. else if (cval->dBmin > 0)
  888. cval->dBmin = 0;
  889. if (cval->dBmin > cval->dBmax) {
  890. /* totally crap, return an error */
  891. return -EINVAL;
  892. }
  893. }
  894. return 0;
  895. }
  896. #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
  897. /* get a feature/mixer unit info */
  898. static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  899. {
  900. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  901. if (cval->val_type == USB_MIXER_BOOLEAN ||
  902. cval->val_type == USB_MIXER_INV_BOOLEAN)
  903. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  904. else
  905. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  906. uinfo->count = cval->channels;
  907. if (cval->val_type == USB_MIXER_BOOLEAN ||
  908. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  909. uinfo->value.integer.min = 0;
  910. uinfo->value.integer.max = 1;
  911. } else {
  912. if (!cval->initialized) {
  913. get_min_max_with_quirks(cval, 0, kcontrol);
  914. if (cval->initialized && cval->dBmin >= cval->dBmax) {
  915. kcontrol->vd[0].access &=
  916. ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  917. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
  918. snd_ctl_notify(cval->mixer->chip->card,
  919. SNDRV_CTL_EVENT_MASK_INFO,
  920. &kcontrol->id);
  921. }
  922. }
  923. uinfo->value.integer.min = 0;
  924. uinfo->value.integer.max =
  925. (cval->max - cval->min + cval->res - 1) / cval->res;
  926. }
  927. return 0;
  928. }
  929. /* get the current value from feature/mixer unit */
  930. static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  931. {
  932. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  933. int c, cnt, val, err;
  934. ucontrol->value.integer.value[0] = cval->min;
  935. if (cval->cmask) {
  936. cnt = 0;
  937. for (c = 0; c < MAX_CHANNELS; c++) {
  938. if (!(cval->cmask & (1 << c)))
  939. continue;
  940. err = get_cur_mix_value(cval, c + 1, cnt, &val);
  941. if (err < 0)
  942. return cval->mixer->ignore_ctl_error ? 0 : err;
  943. val = get_relative_value(cval, val);
  944. ucontrol->value.integer.value[cnt] = val;
  945. cnt++;
  946. }
  947. return 0;
  948. } else {
  949. /* master channel */
  950. err = get_cur_mix_value(cval, 0, 0, &val);
  951. if (err < 0)
  952. return cval->mixer->ignore_ctl_error ? 0 : err;
  953. val = get_relative_value(cval, val);
  954. ucontrol->value.integer.value[0] = val;
  955. }
  956. return 0;
  957. }
  958. /* put the current value to feature/mixer unit */
  959. static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  960. {
  961. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  962. int c, cnt, val, oval, err;
  963. int changed = 0;
  964. if (cval->cmask) {
  965. cnt = 0;
  966. for (c = 0; c < MAX_CHANNELS; c++) {
  967. if (!(cval->cmask & (1 << c)))
  968. continue;
  969. err = get_cur_mix_value(cval, c + 1, cnt, &oval);
  970. if (err < 0)
  971. return cval->mixer->ignore_ctl_error ? 0 : err;
  972. val = ucontrol->value.integer.value[cnt];
  973. val = get_abs_value(cval, val);
  974. if (oval != val) {
  975. set_cur_mix_value(cval, c + 1, cnt, val);
  976. changed = 1;
  977. }
  978. cnt++;
  979. }
  980. } else {
  981. /* master channel */
  982. err = get_cur_mix_value(cval, 0, 0, &oval);
  983. if (err < 0)
  984. return cval->mixer->ignore_ctl_error ? 0 : err;
  985. val = ucontrol->value.integer.value[0];
  986. val = get_abs_value(cval, val);
  987. if (val != oval) {
  988. set_cur_mix_value(cval, 0, 0, val);
  989. changed = 1;
  990. }
  991. }
  992. return changed;
  993. }
  994. static struct snd_kcontrol_new usb_feature_unit_ctl = {
  995. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  996. .name = "", /* will be filled later manually */
  997. .info = mixer_ctl_feature_info,
  998. .get = mixer_ctl_feature_get,
  999. .put = mixer_ctl_feature_put,
  1000. };
  1001. /* the read-only variant */
  1002. static struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
  1003. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1004. .name = "", /* will be filled later manually */
  1005. .info = mixer_ctl_feature_info,
  1006. .get = mixer_ctl_feature_get,
  1007. .put = NULL,
  1008. };
  1009. /* This symbol is exported in order to allow the mixer quirks to
  1010. * hook up to the standard feature unit control mechanism */
  1011. struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
  1012. /*
  1013. * build a feature control
  1014. */
  1015. static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
  1016. {
  1017. return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
  1018. }
  1019. /* A lot of headsets/headphones have a "Speaker" mixer. Make sure we
  1020. rename it to "Headphone". We determine if something is a headphone
  1021. similar to how udev determines form factor. */
  1022. static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
  1023. struct snd_card *card)
  1024. {
  1025. const char *names_to_check[] = {
  1026. "Headset", "headset", "Headphone", "headphone", NULL};
  1027. const char **s;
  1028. bool found = 0;
  1029. if (strcmp("Speaker", kctl->id.name))
  1030. return;
  1031. for (s = names_to_check; *s; s++)
  1032. if (strstr(card->shortname, *s)) {
  1033. found = 1;
  1034. break;
  1035. }
  1036. if (!found)
  1037. return;
  1038. strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
  1039. }
  1040. static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
  1041. unsigned int ctl_mask, int control,
  1042. struct usb_audio_term *iterm, int unitid,
  1043. int readonly_mask)
  1044. {
  1045. struct uac_feature_unit_descriptor *desc = raw_desc;
  1046. unsigned int len = 0;
  1047. int mapped_name = 0;
  1048. int nameid = uac_feature_unit_iFeature(desc);
  1049. struct snd_kcontrol *kctl;
  1050. struct usb_mixer_elem_info *cval;
  1051. const struct usbmix_name_map *map;
  1052. unsigned int range;
  1053. control++; /* change from zero-based to 1-based value */
  1054. if (control == UAC_FU_GRAPHIC_EQUALIZER) {
  1055. /* FIXME: not supported yet */
  1056. return;
  1057. }
  1058. map = find_map(state, unitid, control);
  1059. if (check_ignored_ctl(map))
  1060. return;
  1061. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1062. if (! cval) {
  1063. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1064. return;
  1065. }
  1066. cval->mixer = state->mixer;
  1067. cval->id = unitid;
  1068. cval->control = control;
  1069. cval->cmask = ctl_mask;
  1070. cval->val_type = audio_feature_info[control-1].type;
  1071. if (ctl_mask == 0) {
  1072. cval->channels = 1; /* master channel */
  1073. cval->master_readonly = readonly_mask;
  1074. } else {
  1075. int i, c = 0;
  1076. for (i = 0; i < 16; i++)
  1077. if (ctl_mask & (1 << i))
  1078. c++;
  1079. cval->channels = c;
  1080. cval->ch_readonly = readonly_mask;
  1081. }
  1082. /* if all channels in the mask are marked read-only, make the control
  1083. * read-only. set_cur_mix_value() will check the mask again and won't
  1084. * issue write commands to read-only channels. */
  1085. if (cval->channels == readonly_mask)
  1086. kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
  1087. else
  1088. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1089. if (! kctl) {
  1090. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1091. kfree(cval);
  1092. return;
  1093. }
  1094. kctl->private_free = usb_mixer_elem_free;
  1095. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1096. mapped_name = len != 0;
  1097. if (! len && nameid)
  1098. len = snd_usb_copy_string_desc(state, nameid,
  1099. kctl->id.name, sizeof(kctl->id.name));
  1100. switch (control) {
  1101. case UAC_FU_MUTE:
  1102. case UAC_FU_VOLUME:
  1103. /* determine the control name. the rule is:
  1104. * - if a name id is given in descriptor, use it.
  1105. * - if the connected input can be determined, then use the name
  1106. * of terminal type.
  1107. * - if the connected output can be determined, use it.
  1108. * - otherwise, anonymous name.
  1109. */
  1110. if (! len) {
  1111. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
  1112. if (! len)
  1113. len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
  1114. if (! len)
  1115. len = snprintf(kctl->id.name, sizeof(kctl->id.name),
  1116. "Feature %d", unitid);
  1117. }
  1118. if (!mapped_name)
  1119. check_no_speaker_on_headset(kctl, state->mixer->chip->card);
  1120. /* determine the stream direction:
  1121. * if the connected output is USB stream, then it's likely a
  1122. * capture stream. otherwise it should be playback (hopefully :)
  1123. */
  1124. if (! mapped_name && ! (state->oterm.type >> 16)) {
  1125. if ((state->oterm.type & 0xff00) == 0x0100) {
  1126. len = append_ctl_name(kctl, " Capture");
  1127. } else {
  1128. len = append_ctl_name(kctl, " Playback");
  1129. }
  1130. }
  1131. append_ctl_name(kctl, control == UAC_FU_MUTE ?
  1132. " Switch" : " Volume");
  1133. break;
  1134. default:
  1135. if (! len)
  1136. strlcpy(kctl->id.name, audio_feature_info[control-1].name,
  1137. sizeof(kctl->id.name));
  1138. break;
  1139. }
  1140. /* get min/max values */
  1141. get_min_max_with_quirks(cval, 0, kctl);
  1142. if (control == UAC_FU_VOLUME) {
  1143. check_mapped_dB(map, cval);
  1144. if (cval->dBmin < cval->dBmax || !cval->initialized) {
  1145. kctl->tlv.c = snd_usb_mixer_vol_tlv;
  1146. kctl->vd[0].access |=
  1147. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1148. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1149. }
  1150. }
  1151. range = (cval->max - cval->min) / cval->res;
  1152. /* Are there devices with volume range more than 255? I use a bit more
  1153. * to be sure. 384 is a resolution magic number found on Logitech
  1154. * devices. It will definitively catch all buggy Logitech devices.
  1155. */
  1156. if (range > 384) {
  1157. snd_printk(KERN_WARNING "usb_audio: Warning! Unlikely big "
  1158. "volume range (=%u), cval->res is probably wrong.",
  1159. range);
  1160. snd_printk(KERN_WARNING "usb_audio: [%d] FU [%s] ch = %d, "
  1161. "val = %d/%d/%d", cval->id,
  1162. kctl->id.name, cval->channels,
  1163. cval->min, cval->max, cval->res);
  1164. }
  1165. snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
  1166. cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
  1167. snd_usb_mixer_add_control(state->mixer, kctl);
  1168. }
  1169. /*
  1170. * parse a feature unit
  1171. *
  1172. * most of controls are defined here.
  1173. */
  1174. static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
  1175. {
  1176. int channels, i, j;
  1177. struct usb_audio_term iterm;
  1178. unsigned int master_bits, first_ch_bits;
  1179. int err, csize;
  1180. struct uac_feature_unit_descriptor *hdr = _ftr;
  1181. __u8 *bmaControls;
  1182. if (state->mixer->protocol == UAC_VERSION_1) {
  1183. csize = hdr->bControlSize;
  1184. if (!csize) {
  1185. snd_printdd(KERN_ERR "usbaudio: unit %u: "
  1186. "invalid bControlSize == 0\n", unitid);
  1187. return -EINVAL;
  1188. }
  1189. channels = (hdr->bLength - 7) / csize - 1;
  1190. bmaControls = hdr->bmaControls;
  1191. if (hdr->bLength < 7 + csize) {
  1192. snd_printk(KERN_ERR "usbaudio: unit %u: "
  1193. "invalid UAC_FEATURE_UNIT descriptor\n",
  1194. unitid);
  1195. return -EINVAL;
  1196. }
  1197. } else {
  1198. struct uac2_feature_unit_descriptor *ftr = _ftr;
  1199. csize = 4;
  1200. channels = (hdr->bLength - 6) / 4 - 1;
  1201. bmaControls = ftr->bmaControls;
  1202. if (hdr->bLength < 6 + csize) {
  1203. snd_printk(KERN_ERR "usbaudio: unit %u: "
  1204. "invalid UAC_FEATURE_UNIT descriptor\n",
  1205. unitid);
  1206. return -EINVAL;
  1207. }
  1208. }
  1209. /* parse the source unit */
  1210. if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
  1211. return err;
  1212. /* determine the input source type and name */
  1213. if (check_input_term(state, hdr->bSourceID, &iterm) < 0)
  1214. return -EINVAL;
  1215. master_bits = snd_usb_combine_bytes(bmaControls, csize);
  1216. /* master configuration quirks */
  1217. switch (state->chip->usb_id) {
  1218. case USB_ID(0x08bb, 0x2702):
  1219. snd_printk(KERN_INFO
  1220. "usbmixer: master volume quirk for PCM2702 chip\n");
  1221. /* disable non-functional volume control */
  1222. master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
  1223. break;
  1224. case USB_ID(0x1130, 0xf211):
  1225. snd_printk(KERN_INFO
  1226. "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
  1227. /* disable non-functional volume control */
  1228. channels = 0;
  1229. break;
  1230. }
  1231. if (channels > 0)
  1232. first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
  1233. else
  1234. first_ch_bits = 0;
  1235. if (state->mixer->protocol == UAC_VERSION_1) {
  1236. /* check all control types */
  1237. for (i = 0; i < 10; i++) {
  1238. unsigned int ch_bits = 0;
  1239. for (j = 0; j < channels; j++) {
  1240. unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
  1241. if (mask & (1 << i))
  1242. ch_bits |= (1 << j);
  1243. }
  1244. /* audio class v1 controls are never read-only */
  1245. if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
  1246. build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, 0);
  1247. if (master_bits & (1 << i))
  1248. build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0);
  1249. }
  1250. } else { /* UAC_VERSION_2 */
  1251. for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
  1252. unsigned int ch_bits = 0;
  1253. unsigned int ch_read_only = 0;
  1254. for (j = 0; j < channels; j++) {
  1255. unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize);
  1256. if (uac2_control_is_readable(mask, i)) {
  1257. ch_bits |= (1 << j);
  1258. if (!uac2_control_is_writeable(mask, i))
  1259. ch_read_only |= (1 << j);
  1260. }
  1261. }
  1262. /* NOTE: build_feature_ctl() will mark the control read-only if all channels
  1263. * are marked read-only in the descriptors. Otherwise, the control will be
  1264. * reported as writeable, but the driver will not actually issue a write
  1265. * command for read-only channels */
  1266. if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
  1267. build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, ch_read_only);
  1268. if (uac2_control_is_readable(master_bits, i))
  1269. build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
  1270. !uac2_control_is_writeable(master_bits, i));
  1271. }
  1272. }
  1273. return 0;
  1274. }
  1275. /*
  1276. * Mixer Unit
  1277. */
  1278. /*
  1279. * build a mixer unit control
  1280. *
  1281. * the callbacks are identical with feature unit.
  1282. * input channel number (zero based) is given in control field instead.
  1283. */
  1284. static void build_mixer_unit_ctl(struct mixer_build *state,
  1285. struct uac_mixer_unit_descriptor *desc,
  1286. int in_pin, int in_ch, int unitid,
  1287. struct usb_audio_term *iterm)
  1288. {
  1289. struct usb_mixer_elem_info *cval;
  1290. unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
  1291. unsigned int i, len;
  1292. struct snd_kcontrol *kctl;
  1293. const struct usbmix_name_map *map;
  1294. map = find_map(state, unitid, 0);
  1295. if (check_ignored_ctl(map))
  1296. return;
  1297. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1298. if (! cval)
  1299. return;
  1300. cval->mixer = state->mixer;
  1301. cval->id = unitid;
  1302. cval->control = in_ch + 1; /* based on 1 */
  1303. cval->val_type = USB_MIXER_S16;
  1304. for (i = 0; i < num_outs; i++) {
  1305. if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), in_ch, i, num_outs)) {
  1306. cval->cmask |= (1 << i);
  1307. cval->channels++;
  1308. }
  1309. }
  1310. /* get min/max values */
  1311. get_min_max(cval, 0);
  1312. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1313. if (! kctl) {
  1314. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1315. kfree(cval);
  1316. return;
  1317. }
  1318. kctl->private_free = usb_mixer_elem_free;
  1319. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1320. if (! len)
  1321. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
  1322. if (! len)
  1323. len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
  1324. append_ctl_name(kctl, " Volume");
  1325. snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
  1326. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1327. snd_usb_mixer_add_control(state->mixer, kctl);
  1328. }
  1329. /*
  1330. * parse a mixer unit
  1331. */
  1332. static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1333. {
  1334. struct uac_mixer_unit_descriptor *desc = raw_desc;
  1335. struct usb_audio_term iterm;
  1336. int input_pins, num_ins, num_outs;
  1337. int pin, ich, err;
  1338. if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) || ! (num_outs = uac_mixer_unit_bNrChannels(desc))) {
  1339. snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
  1340. return -EINVAL;
  1341. }
  1342. /* no bmControls field (e.g. Maya44) -> ignore */
  1343. if (desc->bLength <= 10 + input_pins) {
  1344. snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
  1345. return 0;
  1346. }
  1347. num_ins = 0;
  1348. ich = 0;
  1349. for (pin = 0; pin < input_pins; pin++) {
  1350. err = parse_audio_unit(state, desc->baSourceID[pin]);
  1351. if (err < 0)
  1352. continue;
  1353. err = check_input_term(state, desc->baSourceID[pin], &iterm);
  1354. if (err < 0)
  1355. return err;
  1356. num_ins += iterm.channels;
  1357. for (; ich < num_ins; ++ich) {
  1358. int och, ich_has_controls = 0;
  1359. for (och = 0; och < num_outs; ++och) {
  1360. if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol),
  1361. ich, och, num_outs)) {
  1362. ich_has_controls = 1;
  1363. break;
  1364. }
  1365. }
  1366. if (ich_has_controls)
  1367. build_mixer_unit_ctl(state, desc, pin, ich,
  1368. unitid, &iterm);
  1369. }
  1370. }
  1371. return 0;
  1372. }
  1373. /*
  1374. * Processing Unit / Extension Unit
  1375. */
  1376. /* get callback for processing/extension unit */
  1377. static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1378. {
  1379. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1380. int err, val;
  1381. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1382. if (err < 0 && cval->mixer->ignore_ctl_error) {
  1383. ucontrol->value.integer.value[0] = cval->min;
  1384. return 0;
  1385. }
  1386. if (err < 0)
  1387. return err;
  1388. val = get_relative_value(cval, val);
  1389. ucontrol->value.integer.value[0] = val;
  1390. return 0;
  1391. }
  1392. /* put callback for processing/extension unit */
  1393. static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1394. {
  1395. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1396. int val, oval, err;
  1397. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1398. if (err < 0) {
  1399. if (cval->mixer->ignore_ctl_error)
  1400. return 0;
  1401. return err;
  1402. }
  1403. val = ucontrol->value.integer.value[0];
  1404. val = get_abs_value(cval, val);
  1405. if (val != oval) {
  1406. set_cur_ctl_value(cval, cval->control << 8, val);
  1407. return 1;
  1408. }
  1409. return 0;
  1410. }
  1411. /* alsa control interface for processing/extension unit */
  1412. static struct snd_kcontrol_new mixer_procunit_ctl = {
  1413. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1414. .name = "", /* will be filled later */
  1415. .info = mixer_ctl_feature_info,
  1416. .get = mixer_ctl_procunit_get,
  1417. .put = mixer_ctl_procunit_put,
  1418. };
  1419. /*
  1420. * predefined data for processing units
  1421. */
  1422. struct procunit_value_info {
  1423. int control;
  1424. char *suffix;
  1425. int val_type;
  1426. int min_value;
  1427. };
  1428. struct procunit_info {
  1429. int type;
  1430. char *name;
  1431. struct procunit_value_info *values;
  1432. };
  1433. static struct procunit_value_info updown_proc_info[] = {
  1434. { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1435. { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1436. { 0 }
  1437. };
  1438. static struct procunit_value_info prologic_proc_info[] = {
  1439. { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1440. { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1441. { 0 }
  1442. };
  1443. static struct procunit_value_info threed_enh_proc_info[] = {
  1444. { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1445. { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
  1446. { 0 }
  1447. };
  1448. static struct procunit_value_info reverb_proc_info[] = {
  1449. { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1450. { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
  1451. { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
  1452. { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
  1453. { 0 }
  1454. };
  1455. static struct procunit_value_info chorus_proc_info[] = {
  1456. { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1457. { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
  1458. { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
  1459. { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
  1460. { 0 }
  1461. };
  1462. static struct procunit_value_info dcr_proc_info[] = {
  1463. { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1464. { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
  1465. { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
  1466. { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
  1467. { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
  1468. { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
  1469. { 0 }
  1470. };
  1471. static struct procunit_info procunits[] = {
  1472. { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
  1473. { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
  1474. { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
  1475. { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
  1476. { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
  1477. { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
  1478. { 0 },
  1479. };
  1480. /*
  1481. * predefined data for extension units
  1482. */
  1483. static struct procunit_value_info clock_rate_xu_info[] = {
  1484. { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
  1485. { 0 }
  1486. };
  1487. static struct procunit_value_info clock_source_xu_info[] = {
  1488. { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
  1489. { 0 }
  1490. };
  1491. static struct procunit_value_info spdif_format_xu_info[] = {
  1492. { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
  1493. { 0 }
  1494. };
  1495. static struct procunit_value_info soft_limit_xu_info[] = {
  1496. { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
  1497. { 0 }
  1498. };
  1499. static struct procunit_info extunits[] = {
  1500. { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
  1501. { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
  1502. { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
  1503. { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
  1504. { 0 }
  1505. };
  1506. /*
  1507. * build a processing/extension unit
  1508. */
  1509. static int build_audio_procunit(struct mixer_build *state, int unitid, void *raw_desc, struct procunit_info *list, char *name)
  1510. {
  1511. struct uac_processing_unit_descriptor *desc = raw_desc;
  1512. int num_ins = desc->bNrInPins;
  1513. struct usb_mixer_elem_info *cval;
  1514. struct snd_kcontrol *kctl;
  1515. int i, err, nameid, type, len;
  1516. struct procunit_info *info;
  1517. struct procunit_value_info *valinfo;
  1518. const struct usbmix_name_map *map;
  1519. static struct procunit_value_info default_value_info[] = {
  1520. { 0x01, "Switch", USB_MIXER_BOOLEAN },
  1521. { 0 }
  1522. };
  1523. static struct procunit_info default_info = {
  1524. 0, NULL, default_value_info
  1525. };
  1526. if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
  1527. desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
  1528. snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
  1529. return -EINVAL;
  1530. }
  1531. for (i = 0; i < num_ins; i++) {
  1532. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1533. return err;
  1534. }
  1535. type = le16_to_cpu(desc->wProcessType);
  1536. for (info = list; info && info->type; info++)
  1537. if (info->type == type)
  1538. break;
  1539. if (! info || ! info->type)
  1540. info = &default_info;
  1541. for (valinfo = info->values; valinfo->control; valinfo++) {
  1542. __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
  1543. if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
  1544. continue;
  1545. map = find_map(state, unitid, valinfo->control);
  1546. if (check_ignored_ctl(map))
  1547. continue;
  1548. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1549. if (! cval) {
  1550. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1551. return -ENOMEM;
  1552. }
  1553. cval->mixer = state->mixer;
  1554. cval->id = unitid;
  1555. cval->control = valinfo->control;
  1556. cval->val_type = valinfo->val_type;
  1557. cval->channels = 1;
  1558. /* get min/max values */
  1559. if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
  1560. __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
  1561. /* FIXME: hard-coded */
  1562. cval->min = 1;
  1563. cval->max = control_spec[0];
  1564. cval->res = 1;
  1565. cval->initialized = 1;
  1566. } else {
  1567. if (type == USB_XU_CLOCK_RATE) {
  1568. /* E-Mu USB 0404/0202/TrackerPre/0204
  1569. * samplerate control quirk
  1570. */
  1571. cval->min = 0;
  1572. cval->max = 5;
  1573. cval->res = 1;
  1574. cval->initialized = 1;
  1575. } else
  1576. get_min_max(cval, valinfo->min_value);
  1577. }
  1578. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  1579. if (! kctl) {
  1580. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1581. kfree(cval);
  1582. return -ENOMEM;
  1583. }
  1584. kctl->private_free = usb_mixer_elem_free;
  1585. if (check_mapped_name(map, kctl->id.name,
  1586. sizeof(kctl->id.name)))
  1587. /* nothing */ ;
  1588. else if (info->name)
  1589. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  1590. else {
  1591. nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
  1592. len = 0;
  1593. if (nameid)
  1594. len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1595. if (! len)
  1596. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  1597. }
  1598. append_ctl_name(kctl, " ");
  1599. append_ctl_name(kctl, valinfo->suffix);
  1600. snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
  1601. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1602. if ((err = snd_usb_mixer_add_control(state->mixer, kctl)) < 0)
  1603. return err;
  1604. }
  1605. return 0;
  1606. }
  1607. static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1608. {
  1609. return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit");
  1610. }
  1611. static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1612. {
  1613. /* Note that we parse extension units with processing unit descriptors.
  1614. * That's ok as the layout is the same */
  1615. return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit");
  1616. }
  1617. /*
  1618. * Selector Unit
  1619. */
  1620. /* info callback for selector unit
  1621. * use an enumerator type for routing
  1622. */
  1623. static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1624. {
  1625. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1626. const char **itemlist = (const char **)kcontrol->private_value;
  1627. if (snd_BUG_ON(!itemlist))
  1628. return -EINVAL;
  1629. return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
  1630. }
  1631. /* get callback for selector unit */
  1632. static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1633. {
  1634. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1635. int val, err;
  1636. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1637. if (err < 0) {
  1638. if (cval->mixer->ignore_ctl_error) {
  1639. ucontrol->value.enumerated.item[0] = 0;
  1640. return 0;
  1641. }
  1642. return err;
  1643. }
  1644. val = get_relative_value(cval, val);
  1645. ucontrol->value.enumerated.item[0] = val;
  1646. return 0;
  1647. }
  1648. /* put callback for selector unit */
  1649. static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1650. {
  1651. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1652. int val, oval, err;
  1653. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1654. if (err < 0) {
  1655. if (cval->mixer->ignore_ctl_error)
  1656. return 0;
  1657. return err;
  1658. }
  1659. val = ucontrol->value.enumerated.item[0];
  1660. val = get_abs_value(cval, val);
  1661. if (val != oval) {
  1662. set_cur_ctl_value(cval, cval->control << 8, val);
  1663. return 1;
  1664. }
  1665. return 0;
  1666. }
  1667. /* alsa control interface for selector unit */
  1668. static struct snd_kcontrol_new mixer_selectunit_ctl = {
  1669. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1670. .name = "", /* will be filled later */
  1671. .info = mixer_ctl_selector_info,
  1672. .get = mixer_ctl_selector_get,
  1673. .put = mixer_ctl_selector_put,
  1674. };
  1675. /* private free callback.
  1676. * free both private_data and private_value
  1677. */
  1678. static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
  1679. {
  1680. int i, num_ins = 0;
  1681. if (kctl->private_data) {
  1682. struct usb_mixer_elem_info *cval = kctl->private_data;
  1683. num_ins = cval->max;
  1684. kfree(cval);
  1685. kctl->private_data = NULL;
  1686. }
  1687. if (kctl->private_value) {
  1688. char **itemlist = (char **)kctl->private_value;
  1689. for (i = 0; i < num_ins; i++)
  1690. kfree(itemlist[i]);
  1691. kfree(itemlist);
  1692. kctl->private_value = 0;
  1693. }
  1694. }
  1695. /*
  1696. * parse a selector unit
  1697. */
  1698. static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc)
  1699. {
  1700. struct uac_selector_unit_descriptor *desc = raw_desc;
  1701. unsigned int i, nameid, len;
  1702. int err;
  1703. struct usb_mixer_elem_info *cval;
  1704. struct snd_kcontrol *kctl;
  1705. const struct usbmix_name_map *map;
  1706. char **namelist;
  1707. if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) {
  1708. snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
  1709. return -EINVAL;
  1710. }
  1711. for (i = 0; i < desc->bNrInPins; i++) {
  1712. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1713. return err;
  1714. }
  1715. if (desc->bNrInPins == 1) /* only one ? nonsense! */
  1716. return 0;
  1717. map = find_map(state, unitid, 0);
  1718. if (check_ignored_ctl(map))
  1719. return 0;
  1720. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1721. if (! cval) {
  1722. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1723. return -ENOMEM;
  1724. }
  1725. cval->mixer = state->mixer;
  1726. cval->id = unitid;
  1727. cval->val_type = USB_MIXER_U8;
  1728. cval->channels = 1;
  1729. cval->min = 1;
  1730. cval->max = desc->bNrInPins;
  1731. cval->res = 1;
  1732. cval->initialized = 1;
  1733. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  1734. cval->control = UAC2_CX_CLOCK_SELECTOR;
  1735. else
  1736. cval->control = 0;
  1737. namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
  1738. if (! namelist) {
  1739. snd_printk(KERN_ERR "cannot malloc\n");
  1740. kfree(cval);
  1741. return -ENOMEM;
  1742. }
  1743. #define MAX_ITEM_NAME_LEN 64
  1744. for (i = 0; i < desc->bNrInPins; i++) {
  1745. struct usb_audio_term iterm;
  1746. len = 0;
  1747. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  1748. if (! namelist[i]) {
  1749. snd_printk(KERN_ERR "cannot malloc\n");
  1750. while (i--)
  1751. kfree(namelist[i]);
  1752. kfree(namelist);
  1753. kfree(cval);
  1754. return -ENOMEM;
  1755. }
  1756. len = check_mapped_selector_name(state, unitid, i, namelist[i],
  1757. MAX_ITEM_NAME_LEN);
  1758. if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
  1759. len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
  1760. if (! len)
  1761. sprintf(namelist[i], "Input %d", i);
  1762. }
  1763. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  1764. if (! kctl) {
  1765. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1766. kfree(namelist);
  1767. kfree(cval);
  1768. return -ENOMEM;
  1769. }
  1770. kctl->private_value = (unsigned long)namelist;
  1771. kctl->private_free = usb_mixer_selector_elem_free;
  1772. nameid = uac_selector_unit_iSelector(desc);
  1773. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1774. if (len)
  1775. ;
  1776. else if (nameid)
  1777. snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1778. else {
  1779. len = get_term_name(state, &state->oterm,
  1780. kctl->id.name, sizeof(kctl->id.name), 0);
  1781. if (! len)
  1782. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  1783. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  1784. append_ctl_name(kctl, " Clock Source");
  1785. else if ((state->oterm.type & 0xff00) == 0x0100)
  1786. append_ctl_name(kctl, " Capture Source");
  1787. else
  1788. append_ctl_name(kctl, " Playback Source");
  1789. }
  1790. snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
  1791. cval->id, kctl->id.name, desc->bNrInPins);
  1792. if ((err = snd_usb_mixer_add_control(state->mixer, kctl)) < 0)
  1793. return err;
  1794. return 0;
  1795. }
  1796. /*
  1797. * parse an audio unit recursively
  1798. */
  1799. static int parse_audio_unit(struct mixer_build *state, int unitid)
  1800. {
  1801. unsigned char *p1;
  1802. if (test_and_set_bit(unitid, state->unitbitmap))
  1803. return 0; /* the unit already visited */
  1804. p1 = find_audio_control_unit(state, unitid);
  1805. if (!p1) {
  1806. snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
  1807. return -EINVAL;
  1808. }
  1809. switch (p1[2]) {
  1810. case UAC_INPUT_TERMINAL:
  1811. case UAC2_CLOCK_SOURCE:
  1812. return 0; /* NOP */
  1813. case UAC_MIXER_UNIT:
  1814. return parse_audio_mixer_unit(state, unitid, p1);
  1815. case UAC_SELECTOR_UNIT:
  1816. case UAC2_CLOCK_SELECTOR:
  1817. return parse_audio_selector_unit(state, unitid, p1);
  1818. case UAC_FEATURE_UNIT:
  1819. return parse_audio_feature_unit(state, unitid, p1);
  1820. case UAC1_PROCESSING_UNIT:
  1821. /* UAC2_EFFECT_UNIT has the same value */
  1822. if (state->mixer->protocol == UAC_VERSION_1)
  1823. return parse_audio_processing_unit(state, unitid, p1);
  1824. else
  1825. return 0; /* FIXME - effect units not implemented yet */
  1826. case UAC1_EXTENSION_UNIT:
  1827. /* UAC2_PROCESSING_UNIT_V2 has the same value */
  1828. if (state->mixer->protocol == UAC_VERSION_1)
  1829. return parse_audio_extension_unit(state, unitid, p1);
  1830. else /* UAC_VERSION_2 */
  1831. return parse_audio_processing_unit(state, unitid, p1);
  1832. default:
  1833. snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  1834. return -EINVAL;
  1835. }
  1836. }
  1837. static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
  1838. {
  1839. kfree(mixer->id_elems);
  1840. if (mixer->urb) {
  1841. kfree(mixer->urb->transfer_buffer);
  1842. usb_free_urb(mixer->urb);
  1843. }
  1844. usb_free_urb(mixer->rc_urb);
  1845. kfree(mixer->rc_setup_packet);
  1846. kfree(mixer);
  1847. }
  1848. static int snd_usb_mixer_dev_free(struct snd_device *device)
  1849. {
  1850. struct usb_mixer_interface *mixer = device->device_data;
  1851. snd_usb_mixer_free(mixer);
  1852. return 0;
  1853. }
  1854. /*
  1855. * create mixer controls
  1856. *
  1857. * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
  1858. */
  1859. static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
  1860. {
  1861. struct mixer_build state;
  1862. int err;
  1863. const struct usbmix_ctl_map *map;
  1864. void *p;
  1865. memset(&state, 0, sizeof(state));
  1866. state.chip = mixer->chip;
  1867. state.mixer = mixer;
  1868. state.buffer = mixer->hostif->extra;
  1869. state.buflen = mixer->hostif->extralen;
  1870. /* check the mapping table */
  1871. for (map = usbmix_ctl_maps; map->id; map++) {
  1872. if (map->id == state.chip->usb_id) {
  1873. state.map = map->map;
  1874. state.selector_map = map->selector_map;
  1875. mixer->ignore_ctl_error = map->ignore_ctl_error;
  1876. break;
  1877. }
  1878. }
  1879. p = NULL;
  1880. while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, mixer->hostif->extralen,
  1881. p, UAC_OUTPUT_TERMINAL)) != NULL) {
  1882. if (mixer->protocol == UAC_VERSION_1) {
  1883. struct uac1_output_terminal_descriptor *desc = p;
  1884. if (desc->bLength < sizeof(*desc))
  1885. continue; /* invalid descriptor? */
  1886. set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
  1887. state.oterm.id = desc->bTerminalID;
  1888. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  1889. state.oterm.name = desc->iTerminal;
  1890. err = parse_audio_unit(&state, desc->bSourceID);
  1891. if (err < 0)
  1892. return err;
  1893. } else { /* UAC_VERSION_2 */
  1894. struct uac2_output_terminal_descriptor *desc = p;
  1895. if (desc->bLength < sizeof(*desc))
  1896. continue; /* invalid descriptor? */
  1897. set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
  1898. state.oterm.id = desc->bTerminalID;
  1899. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  1900. state.oterm.name = desc->iTerminal;
  1901. err = parse_audio_unit(&state, desc->bSourceID);
  1902. if (err < 0)
  1903. return err;
  1904. /* for UAC2, use the same approach to also add the clock selectors */
  1905. err = parse_audio_unit(&state, desc->bCSourceID);
  1906. if (err < 0)
  1907. return err;
  1908. }
  1909. }
  1910. return 0;
  1911. }
  1912. void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
  1913. {
  1914. struct usb_mixer_elem_info *info;
  1915. for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
  1916. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1917. info->elem_id);
  1918. }
  1919. static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
  1920. int unitid,
  1921. struct usb_mixer_elem_info *cval)
  1922. {
  1923. static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
  1924. "S8", "U8", "S16", "U16"};
  1925. snd_iprintf(buffer, " Unit: %i\n", unitid);
  1926. if (cval->elem_id)
  1927. snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
  1928. cval->elem_id->name, cval->elem_id->index);
  1929. snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
  1930. "channels=%i, type=\"%s\"\n", cval->id,
  1931. cval->control, cval->cmask, cval->channels,
  1932. val_types[cval->val_type]);
  1933. snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
  1934. cval->min, cval->max, cval->dBmin, cval->dBmax);
  1935. }
  1936. static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
  1937. struct snd_info_buffer *buffer)
  1938. {
  1939. struct snd_usb_audio *chip = entry->private_data;
  1940. struct usb_mixer_interface *mixer;
  1941. struct usb_mixer_elem_info *cval;
  1942. int unitid;
  1943. list_for_each_entry(mixer, &chip->mixer_list, list) {
  1944. snd_iprintf(buffer,
  1945. "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
  1946. chip->usb_id, snd_usb_ctrl_intf(chip),
  1947. mixer->ignore_ctl_error);
  1948. snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
  1949. for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
  1950. for (cval = mixer->id_elems[unitid]; cval;
  1951. cval = cval->next_id_elem)
  1952. snd_usb_mixer_dump_cval(buffer, unitid, cval);
  1953. }
  1954. }
  1955. }
  1956. static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
  1957. int attribute, int value, int index)
  1958. {
  1959. struct usb_mixer_elem_info *info;
  1960. __u8 unitid = (index >> 8) & 0xff;
  1961. __u8 control = (value >> 8) & 0xff;
  1962. __u8 channel = value & 0xff;
  1963. if (channel >= MAX_CHANNELS) {
  1964. snd_printk(KERN_DEBUG "%s(): bogus channel number %d\n",
  1965. __func__, channel);
  1966. return;
  1967. }
  1968. for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) {
  1969. if (info->control != control)
  1970. continue;
  1971. switch (attribute) {
  1972. case UAC2_CS_CUR:
  1973. /* invalidate cache, so the value is read from the device */
  1974. if (channel)
  1975. info->cached &= ~(1 << channel);
  1976. else /* master channel */
  1977. info->cached = 0;
  1978. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1979. info->elem_id);
  1980. break;
  1981. case UAC2_CS_RANGE:
  1982. /* TODO */
  1983. break;
  1984. case UAC2_CS_MEM:
  1985. /* TODO */
  1986. break;
  1987. default:
  1988. snd_printk(KERN_DEBUG "unknown attribute %d in interrupt\n",
  1989. attribute);
  1990. break;
  1991. } /* switch */
  1992. }
  1993. }
  1994. static void snd_usb_mixer_interrupt(struct urb *urb)
  1995. {
  1996. struct usb_mixer_interface *mixer = urb->context;
  1997. int len = urb->actual_length;
  1998. int ustatus = urb->status;
  1999. if (ustatus != 0)
  2000. goto requeue;
  2001. if (mixer->protocol == UAC_VERSION_1) {
  2002. struct uac1_status_word *status;
  2003. for (status = urb->transfer_buffer;
  2004. len >= sizeof(*status);
  2005. len -= sizeof(*status), status++) {
  2006. snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
  2007. status->bStatusType,
  2008. status->bOriginator);
  2009. /* ignore any notifications not from the control interface */
  2010. if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
  2011. UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
  2012. continue;
  2013. if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
  2014. snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
  2015. else
  2016. snd_usb_mixer_notify_id(mixer, status->bOriginator);
  2017. }
  2018. } else { /* UAC_VERSION_2 */
  2019. struct uac2_interrupt_data_msg *msg;
  2020. for (msg = urb->transfer_buffer;
  2021. len >= sizeof(*msg);
  2022. len -= sizeof(*msg), msg++) {
  2023. /* drop vendor specific and endpoint requests */
  2024. if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
  2025. (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
  2026. continue;
  2027. snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
  2028. le16_to_cpu(msg->wValue),
  2029. le16_to_cpu(msg->wIndex));
  2030. }
  2031. }
  2032. requeue:
  2033. if (ustatus != -ENOENT && ustatus != -ECONNRESET && ustatus != -ESHUTDOWN) {
  2034. urb->dev = mixer->chip->dev;
  2035. usb_submit_urb(urb, GFP_ATOMIC);
  2036. }
  2037. }
  2038. /* stop any bus activity of a mixer */
  2039. void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
  2040. {
  2041. usb_kill_urb(mixer->urb);
  2042. usb_kill_urb(mixer->rc_urb);
  2043. }
  2044. int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
  2045. {
  2046. int err;
  2047. if (mixer->urb) {
  2048. err = usb_submit_urb(mixer->urb, GFP_NOIO);
  2049. if (err < 0)
  2050. return err;
  2051. }
  2052. return 0;
  2053. }
  2054. /* create the handler for the optional status interrupt endpoint */
  2055. static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
  2056. {
  2057. struct usb_endpoint_descriptor *ep;
  2058. void *transfer_buffer;
  2059. int buffer_length;
  2060. unsigned int epnum;
  2061. /* we need one interrupt input endpoint */
  2062. if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
  2063. return 0;
  2064. ep = get_endpoint(mixer->hostif, 0);
  2065. if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
  2066. return 0;
  2067. epnum = usb_endpoint_num(ep);
  2068. buffer_length = le16_to_cpu(ep->wMaxPacketSize);
  2069. transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
  2070. if (!transfer_buffer)
  2071. return -ENOMEM;
  2072. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  2073. if (!mixer->urb) {
  2074. kfree(transfer_buffer);
  2075. return -ENOMEM;
  2076. }
  2077. usb_fill_int_urb(mixer->urb, mixer->chip->dev,
  2078. usb_rcvintpipe(mixer->chip->dev, epnum),
  2079. transfer_buffer, buffer_length,
  2080. snd_usb_mixer_interrupt, mixer, ep->bInterval);
  2081. usb_submit_urb(mixer->urb, GFP_KERNEL);
  2082. return 0;
  2083. }
  2084. int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
  2085. int ignore_error)
  2086. {
  2087. static struct snd_device_ops dev_ops = {
  2088. .dev_free = snd_usb_mixer_dev_free
  2089. };
  2090. struct usb_mixer_interface *mixer;
  2091. struct snd_info_entry *entry;
  2092. int err;
  2093. strcpy(chip->card->mixername, "USB Mixer");
  2094. mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
  2095. if (!mixer)
  2096. return -ENOMEM;
  2097. mixer->chip = chip;
  2098. mixer->ignore_ctl_error = ignore_error;
  2099. mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
  2100. GFP_KERNEL);
  2101. if (!mixer->id_elems) {
  2102. kfree(mixer);
  2103. return -ENOMEM;
  2104. }
  2105. mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
  2106. switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
  2107. case UAC_VERSION_1:
  2108. default:
  2109. mixer->protocol = UAC_VERSION_1;
  2110. break;
  2111. case UAC_VERSION_2:
  2112. mixer->protocol = UAC_VERSION_2;
  2113. break;
  2114. }
  2115. if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
  2116. (err = snd_usb_mixer_status_create(mixer)) < 0)
  2117. goto _error;
  2118. snd_usb_mixer_apply_create_quirk(mixer);
  2119. err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
  2120. if (err < 0)
  2121. goto _error;
  2122. if (list_empty(&chip->mixer_list) &&
  2123. !snd_card_proc_new(chip->card, "usbmixer", &entry))
  2124. snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
  2125. list_add(&mixer->list, &chip->mixer_list);
  2126. return 0;
  2127. _error:
  2128. snd_usb_mixer_free(mixer);
  2129. return err;
  2130. }
  2131. void snd_usb_mixer_disconnect(struct list_head *p)
  2132. {
  2133. struct usb_mixer_interface *mixer;
  2134. mixer = list_entry(p, struct usb_mixer_interface, list);
  2135. usb_kill_urb(mixer->urb);
  2136. usb_kill_urb(mixer->rc_urb);
  2137. }