echoaudio.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA driver for Echoaudio soundcards.
  4. * Copyright (C) 2003-2004 Giuliano Pochini <[email protected]>
  5. * Copyright (C) 2020 Mark Hills <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. MODULE_AUTHOR("Giuliano Pochini <[email protected]>");
  9. MODULE_LICENSE("GPL v2");
  10. MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
  11. MODULE_DEVICE_TABLE(pci, snd_echo_ids);
  12. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  13. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  14. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  15. module_param_array(index, int, NULL, 0444);
  16. MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
  17. module_param_array(id, charp, NULL, 0444);
  18. MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
  19. module_param_array(enable, bool, NULL, 0444);
  20. MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
  21. static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
  22. static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
  23. static int get_firmware(const struct firmware **fw_entry,
  24. struct echoaudio *chip, const short fw_index)
  25. {
  26. int err;
  27. char name[30];
  28. #ifdef CONFIG_PM_SLEEP
  29. if (chip->fw_cache[fw_index]) {
  30. dev_dbg(chip->card->dev,
  31. "firmware requested: %s is cached\n",
  32. card_fw[fw_index].data);
  33. *fw_entry = chip->fw_cache[fw_index];
  34. return 0;
  35. }
  36. #endif
  37. dev_dbg(chip->card->dev,
  38. "firmware requested: %s\n", card_fw[fw_index].data);
  39. snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
  40. err = request_firmware(fw_entry, name, &chip->pci->dev);
  41. if (err < 0)
  42. dev_err(chip->card->dev,
  43. "get_firmware(): Firmware not available (%d)\n", err);
  44. #ifdef CONFIG_PM_SLEEP
  45. else
  46. chip->fw_cache[fw_index] = *fw_entry;
  47. #endif
  48. return err;
  49. }
  50. static void free_firmware(const struct firmware *fw_entry,
  51. struct echoaudio *chip)
  52. {
  53. #ifdef CONFIG_PM_SLEEP
  54. dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
  55. #else
  56. release_firmware(fw_entry);
  57. #endif
  58. }
  59. static void free_firmware_cache(struct echoaudio *chip)
  60. {
  61. #ifdef CONFIG_PM_SLEEP
  62. int i;
  63. for (i = 0; i < 8 ; i++)
  64. if (chip->fw_cache[i]) {
  65. release_firmware(chip->fw_cache[i]);
  66. dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
  67. }
  68. #endif
  69. }
  70. /******************************************************************************
  71. PCM interface
  72. ******************************************************************************/
  73. static void audiopipe_free(struct snd_pcm_runtime *runtime)
  74. {
  75. struct audiopipe *pipe = runtime->private_data;
  76. if (pipe->sgpage.area)
  77. snd_dma_free_pages(&pipe->sgpage);
  78. kfree(pipe);
  79. }
  80. static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
  81. struct snd_pcm_hw_rule *rule)
  82. {
  83. struct snd_interval *c = hw_param_interval(params,
  84. SNDRV_PCM_HW_PARAM_CHANNELS);
  85. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  86. struct snd_mask fmt;
  87. snd_mask_any(&fmt);
  88. #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
  89. /* >=2 channels cannot be S32_BE */
  90. if (c->min == 2) {
  91. fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
  92. return snd_mask_refine(f, &fmt);
  93. }
  94. #endif
  95. /* > 2 channels cannot be U8 and S32_BE */
  96. if (c->min > 2) {
  97. fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
  98. return snd_mask_refine(f, &fmt);
  99. }
  100. /* Mono is ok with any format */
  101. return 0;
  102. }
  103. static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
  104. struct snd_pcm_hw_rule *rule)
  105. {
  106. struct snd_interval *c = hw_param_interval(params,
  107. SNDRV_PCM_HW_PARAM_CHANNELS);
  108. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  109. struct snd_interval ch;
  110. snd_interval_any(&ch);
  111. /* S32_BE is mono (and stereo) only */
  112. if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
  113. ch.min = 1;
  114. #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
  115. ch.max = 2;
  116. #else
  117. ch.max = 1;
  118. #endif
  119. ch.integer = 1;
  120. return snd_interval_refine(c, &ch);
  121. }
  122. /* U8 can be only mono or stereo */
  123. if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
  124. ch.min = 1;
  125. ch.max = 2;
  126. ch.integer = 1;
  127. return snd_interval_refine(c, &ch);
  128. }
  129. /* S16_LE, S24_3LE and S32_LE support any number of channels. */
  130. return 0;
  131. }
  132. static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
  133. struct snd_pcm_hw_rule *rule)
  134. {
  135. struct snd_interval *c = hw_param_interval(params,
  136. SNDRV_PCM_HW_PARAM_CHANNELS);
  137. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  138. struct snd_mask fmt;
  139. u64 fmask;
  140. snd_mask_any(&fmt);
  141. fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
  142. /* >2 channels must be S16_LE, S24_3LE or S32_LE */
  143. if (c->min > 2) {
  144. fmask &= SNDRV_PCM_FMTBIT_S16_LE |
  145. SNDRV_PCM_FMTBIT_S24_3LE |
  146. SNDRV_PCM_FMTBIT_S32_LE;
  147. /* 1 channel must be S32_BE or S32_LE */
  148. } else if (c->max == 1)
  149. fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
  150. #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
  151. /* 2 channels cannot be S32_BE */
  152. else if (c->min == 2 && c->max == 2)
  153. fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
  154. #endif
  155. else
  156. return 0;
  157. fmt.bits[0] &= (u32)fmask;
  158. fmt.bits[1] &= (u32)(fmask >> 32);
  159. return snd_mask_refine(f, &fmt);
  160. }
  161. static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
  162. struct snd_pcm_hw_rule *rule)
  163. {
  164. struct snd_interval *c = hw_param_interval(params,
  165. SNDRV_PCM_HW_PARAM_CHANNELS);
  166. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  167. struct snd_interval ch;
  168. u64 fmask;
  169. snd_interval_any(&ch);
  170. ch.integer = 1;
  171. fmask = f->bits[0] + ((u64)f->bits[1] << 32);
  172. /* S32_BE is mono (and stereo) only */
  173. if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
  174. ch.min = 1;
  175. #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
  176. ch.max = 2;
  177. #else
  178. ch.max = 1;
  179. #endif
  180. /* U8 is stereo only */
  181. } else if (fmask == SNDRV_PCM_FMTBIT_U8)
  182. ch.min = ch.max = 2;
  183. /* S16_LE and S24_3LE must be at least stereo */
  184. else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
  185. SNDRV_PCM_FMTBIT_S24_3LE)))
  186. ch.min = 2;
  187. else
  188. return 0;
  189. return snd_interval_refine(c, &ch);
  190. }
  191. /* Since the sample rate is a global setting, do allow the user to change the
  192. sample rate only if there is only one pcm device open. */
  193. static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
  194. struct snd_pcm_hw_rule *rule)
  195. {
  196. struct snd_interval *rate = hw_param_interval(params,
  197. SNDRV_PCM_HW_PARAM_RATE);
  198. struct echoaudio *chip = rule->private;
  199. struct snd_interval fixed;
  200. int err;
  201. mutex_lock(&chip->mode_mutex);
  202. if (chip->can_set_rate) {
  203. err = 0;
  204. } else {
  205. snd_interval_any(&fixed);
  206. fixed.min = fixed.max = chip->sample_rate;
  207. err = snd_interval_refine(rate, &fixed);
  208. }
  209. mutex_unlock(&chip->mode_mutex);
  210. return err;
  211. }
  212. static int pcm_open(struct snd_pcm_substream *substream,
  213. signed char max_channels)
  214. {
  215. struct echoaudio *chip;
  216. struct snd_pcm_runtime *runtime;
  217. struct audiopipe *pipe;
  218. int err, i;
  219. if (max_channels <= 0)
  220. return -EAGAIN;
  221. chip = snd_pcm_substream_chip(substream);
  222. runtime = substream->runtime;
  223. pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
  224. if (!pipe)
  225. return -ENOMEM;
  226. pipe->index = -1; /* Not configured yet */
  227. /* Set up hw capabilities and contraints */
  228. memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
  229. dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
  230. pipe->constr.list = channels_list;
  231. pipe->constr.mask = 0;
  232. for (i = 0; channels_list[i] <= max_channels; i++);
  233. pipe->constr.count = i;
  234. if (pipe->hw.channels_max > max_channels)
  235. pipe->hw.channels_max = max_channels;
  236. if (chip->digital_mode == DIGITAL_MODE_ADAT) {
  237. pipe->hw.rate_max = 48000;
  238. pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
  239. }
  240. runtime->hw = pipe->hw;
  241. runtime->private_data = pipe;
  242. runtime->private_free = audiopipe_free;
  243. snd_pcm_set_sync(substream);
  244. /* Only mono and any even number of channels are allowed */
  245. err = snd_pcm_hw_constraint_list(runtime, 0,
  246. SNDRV_PCM_HW_PARAM_CHANNELS,
  247. &pipe->constr);
  248. if (err < 0)
  249. return err;
  250. /* All periods should have the same size */
  251. err = snd_pcm_hw_constraint_integer(runtime,
  252. SNDRV_PCM_HW_PARAM_PERIODS);
  253. if (err < 0)
  254. return err;
  255. /* The hw accesses memory in chunks 32 frames long and they should be
  256. 32-bytes-aligned. It's not a requirement, but it seems that IRQs are
  257. generated with a resolution of 32 frames. Thus we need the following */
  258. err = snd_pcm_hw_constraint_step(runtime, 0,
  259. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
  260. if (err < 0)
  261. return err;
  262. err = snd_pcm_hw_constraint_step(runtime, 0,
  263. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
  264. if (err < 0)
  265. return err;
  266. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  267. SNDRV_PCM_HW_PARAM_RATE,
  268. hw_rule_sample_rate, chip,
  269. SNDRV_PCM_HW_PARAM_RATE, -1);
  270. if (err < 0)
  271. return err;
  272. /* Allocate a page for the scatter-gather list */
  273. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  274. &chip->pci->dev,
  275. PAGE_SIZE, &pipe->sgpage);
  276. if (err < 0) {
  277. dev_err(chip->card->dev, "s-g list allocation failed\n");
  278. return err;
  279. }
  280. /*
  281. * Sole ownership required to set the rate
  282. */
  283. dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
  284. chip->opencount, chip->can_set_rate, chip->rate_set);
  285. chip->opencount++;
  286. if (chip->opencount > 1 && chip->rate_set)
  287. chip->can_set_rate = 0;
  288. return 0;
  289. }
  290. static int pcm_analog_in_open(struct snd_pcm_substream *substream)
  291. {
  292. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  293. int err;
  294. err = pcm_open(substream,
  295. num_analog_busses_in(chip) - substream->number);
  296. if (err < 0)
  297. return err;
  298. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  299. SNDRV_PCM_HW_PARAM_CHANNELS,
  300. hw_rule_capture_channels_by_format, NULL,
  301. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  302. if (err < 0)
  303. return err;
  304. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  305. SNDRV_PCM_HW_PARAM_FORMAT,
  306. hw_rule_capture_format_by_channels, NULL,
  307. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  308. if (err < 0)
  309. return err;
  310. return 0;
  311. }
  312. static int pcm_analog_out_open(struct snd_pcm_substream *substream)
  313. {
  314. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  315. int max_channels, err;
  316. #ifdef ECHOCARD_HAS_VMIXER
  317. max_channels = num_pipes_out(chip);
  318. #else
  319. max_channels = num_analog_busses_out(chip);
  320. #endif
  321. err = pcm_open(substream, max_channels - substream->number);
  322. if (err < 0)
  323. return err;
  324. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  325. SNDRV_PCM_HW_PARAM_CHANNELS,
  326. hw_rule_playback_channels_by_format,
  327. NULL,
  328. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  329. if (err < 0)
  330. return err;
  331. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  332. SNDRV_PCM_HW_PARAM_FORMAT,
  333. hw_rule_playback_format_by_channels,
  334. NULL,
  335. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  336. if (err < 0)
  337. return err;
  338. return 0;
  339. }
  340. #ifdef ECHOCARD_HAS_DIGITAL_IO
  341. static int pcm_digital_in_open(struct snd_pcm_substream *substream)
  342. {
  343. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  344. int err, max_channels;
  345. max_channels = num_digital_busses_in(chip) - substream->number;
  346. mutex_lock(&chip->mode_mutex);
  347. if (chip->digital_mode == DIGITAL_MODE_ADAT)
  348. err = pcm_open(substream, max_channels);
  349. else /* If the card has ADAT, subtract the 6 channels
  350. * that S/PDIF doesn't have
  351. */
  352. err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
  353. if (err < 0)
  354. goto din_exit;
  355. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  356. SNDRV_PCM_HW_PARAM_CHANNELS,
  357. hw_rule_capture_channels_by_format, NULL,
  358. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  359. if (err < 0)
  360. goto din_exit;
  361. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  362. SNDRV_PCM_HW_PARAM_FORMAT,
  363. hw_rule_capture_format_by_channels, NULL,
  364. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  365. if (err < 0)
  366. goto din_exit;
  367. din_exit:
  368. mutex_unlock(&chip->mode_mutex);
  369. return err;
  370. }
  371. #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
  372. static int pcm_digital_out_open(struct snd_pcm_substream *substream)
  373. {
  374. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  375. int err, max_channels;
  376. max_channels = num_digital_busses_out(chip) - substream->number;
  377. mutex_lock(&chip->mode_mutex);
  378. if (chip->digital_mode == DIGITAL_MODE_ADAT)
  379. err = pcm_open(substream, max_channels);
  380. else /* If the card has ADAT, subtract the 6 channels
  381. * that S/PDIF doesn't have
  382. */
  383. err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
  384. if (err < 0)
  385. goto dout_exit;
  386. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  387. SNDRV_PCM_HW_PARAM_CHANNELS,
  388. hw_rule_playback_channels_by_format,
  389. NULL, SNDRV_PCM_HW_PARAM_FORMAT,
  390. -1);
  391. if (err < 0)
  392. goto dout_exit;
  393. err = snd_pcm_hw_rule_add(substream->runtime, 0,
  394. SNDRV_PCM_HW_PARAM_FORMAT,
  395. hw_rule_playback_format_by_channels,
  396. NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
  397. -1);
  398. if (err < 0)
  399. goto dout_exit;
  400. dout_exit:
  401. mutex_unlock(&chip->mode_mutex);
  402. return err;
  403. }
  404. #endif /* !ECHOCARD_HAS_VMIXER */
  405. #endif /* ECHOCARD_HAS_DIGITAL_IO */
  406. static int pcm_close(struct snd_pcm_substream *substream)
  407. {
  408. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  409. /* Nothing to do here. Audio is already off and pipe will be
  410. * freed by its callback
  411. */
  412. mutex_lock(&chip->mode_mutex);
  413. dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
  414. chip->opencount, chip->can_set_rate, chip->rate_set);
  415. chip->opencount--;
  416. switch (chip->opencount) {
  417. case 1:
  418. chip->can_set_rate = 1;
  419. break;
  420. case 0:
  421. chip->rate_set = 0;
  422. break;
  423. }
  424. mutex_unlock(&chip->mode_mutex);
  425. return 0;
  426. }
  427. /* Channel allocation and scatter-gather list setup */
  428. static int init_engine(struct snd_pcm_substream *substream,
  429. struct snd_pcm_hw_params *hw_params,
  430. int pipe_index, int interleave)
  431. {
  432. struct echoaudio *chip;
  433. int err, per, rest, page, edge, offs;
  434. struct audiopipe *pipe;
  435. chip = snd_pcm_substream_chip(substream);
  436. pipe = (struct audiopipe *) substream->runtime->private_data;
  437. /* Sets up che hardware. If it's already initialized, reset and
  438. * redo with the new parameters
  439. */
  440. spin_lock_irq(&chip->lock);
  441. if (pipe->index >= 0) {
  442. dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
  443. err = free_pipes(chip, pipe);
  444. snd_BUG_ON(err);
  445. chip->substream[pipe->index] = NULL;
  446. }
  447. err = allocate_pipes(chip, pipe, pipe_index, interleave);
  448. if (err < 0) {
  449. spin_unlock_irq(&chip->lock);
  450. dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
  451. pipe_index, err);
  452. return err;
  453. }
  454. spin_unlock_irq(&chip->lock);
  455. dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
  456. dev_dbg(chip->card->dev,
  457. "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
  458. params_buffer_bytes(hw_params), params_periods(hw_params),
  459. params_period_bytes(hw_params));
  460. sglist_init(chip, pipe);
  461. edge = PAGE_SIZE;
  462. for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
  463. per++) {
  464. rest = params_period_bytes(hw_params);
  465. if (offs + rest > params_buffer_bytes(hw_params))
  466. rest = params_buffer_bytes(hw_params) - offs;
  467. while (rest) {
  468. dma_addr_t addr;
  469. addr = snd_pcm_sgbuf_get_addr(substream, offs);
  470. if (rest <= edge - offs) {
  471. sglist_add_mapping(chip, pipe, addr, rest);
  472. sglist_add_irq(chip, pipe);
  473. offs += rest;
  474. rest = 0;
  475. } else {
  476. sglist_add_mapping(chip, pipe, addr,
  477. edge - offs);
  478. rest -= edge - offs;
  479. offs = edge;
  480. }
  481. if (offs == edge) {
  482. edge += PAGE_SIZE;
  483. page++;
  484. }
  485. }
  486. }
  487. /* Close the ring buffer */
  488. sglist_wrap(chip, pipe);
  489. /* This stuff is used by the irq handler, so it must be
  490. * initialized before chip->substream
  491. */
  492. pipe->last_period = 0;
  493. pipe->last_counter = 0;
  494. pipe->position = 0;
  495. smp_wmb();
  496. chip->substream[pipe_index] = substream;
  497. chip->rate_set = 1;
  498. spin_lock_irq(&chip->lock);
  499. set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
  500. spin_unlock_irq(&chip->lock);
  501. return 0;
  502. }
  503. static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
  504. struct snd_pcm_hw_params *hw_params)
  505. {
  506. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  507. return init_engine(substream, hw_params, px_analog_in(chip) +
  508. substream->number, params_channels(hw_params));
  509. }
  510. static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
  511. struct snd_pcm_hw_params *hw_params)
  512. {
  513. return init_engine(substream, hw_params, substream->number,
  514. params_channels(hw_params));
  515. }
  516. #ifdef ECHOCARD_HAS_DIGITAL_IO
  517. static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
  518. struct snd_pcm_hw_params *hw_params)
  519. {
  520. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  521. return init_engine(substream, hw_params, px_digital_in(chip) +
  522. substream->number, params_channels(hw_params));
  523. }
  524. #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
  525. static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
  526. struct snd_pcm_hw_params *hw_params)
  527. {
  528. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  529. return init_engine(substream, hw_params, px_digital_out(chip) +
  530. substream->number, params_channels(hw_params));
  531. }
  532. #endif /* !ECHOCARD_HAS_VMIXER */
  533. #endif /* ECHOCARD_HAS_DIGITAL_IO */
  534. static int pcm_hw_free(struct snd_pcm_substream *substream)
  535. {
  536. struct echoaudio *chip;
  537. struct audiopipe *pipe;
  538. chip = snd_pcm_substream_chip(substream);
  539. pipe = (struct audiopipe *) substream->runtime->private_data;
  540. spin_lock_irq(&chip->lock);
  541. if (pipe->index >= 0) {
  542. dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
  543. free_pipes(chip, pipe);
  544. chip->substream[pipe->index] = NULL;
  545. pipe->index = -1;
  546. }
  547. spin_unlock_irq(&chip->lock);
  548. return 0;
  549. }
  550. static int pcm_prepare(struct snd_pcm_substream *substream)
  551. {
  552. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  553. struct snd_pcm_runtime *runtime = substream->runtime;
  554. struct audioformat format;
  555. int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
  556. dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
  557. runtime->rate, runtime->format, runtime->channels);
  558. format.interleave = runtime->channels;
  559. format.data_are_bigendian = 0;
  560. format.mono_to_stereo = 0;
  561. switch (runtime->format) {
  562. case SNDRV_PCM_FORMAT_U8:
  563. format.bits_per_sample = 8;
  564. break;
  565. case SNDRV_PCM_FORMAT_S16_LE:
  566. format.bits_per_sample = 16;
  567. break;
  568. case SNDRV_PCM_FORMAT_S24_3LE:
  569. format.bits_per_sample = 24;
  570. break;
  571. case SNDRV_PCM_FORMAT_S32_BE:
  572. format.data_are_bigendian = 1;
  573. fallthrough;
  574. case SNDRV_PCM_FORMAT_S32_LE:
  575. format.bits_per_sample = 32;
  576. break;
  577. default:
  578. dev_err(chip->card->dev,
  579. "Prepare error: unsupported format %d\n",
  580. runtime->format);
  581. return -EINVAL;
  582. }
  583. if (snd_BUG_ON(pipe_index >= px_num(chip)))
  584. return -EINVAL;
  585. /*
  586. * We passed checks we can do independently; now take
  587. * exclusive control
  588. */
  589. spin_lock_irq(&chip->lock);
  590. if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
  591. spin_unlock_irq(&chip->lock);
  592. return -EINVAL;
  593. }
  594. set_audio_format(chip, pipe_index, &format);
  595. spin_unlock_irq(&chip->lock);
  596. return 0;
  597. }
  598. static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  599. {
  600. struct echoaudio *chip = snd_pcm_substream_chip(substream);
  601. struct audiopipe *pipe;
  602. int i, err;
  603. u32 channelmask = 0;
  604. struct snd_pcm_substream *s;
  605. snd_pcm_group_for_each_entry(s, substream) {
  606. for (i = 0; i < DSP_MAXPIPES; i++) {
  607. if (s == chip->substream[i]) {
  608. channelmask |= 1 << i;
  609. snd_pcm_trigger_done(s, substream);
  610. }
  611. }
  612. }
  613. spin_lock(&chip->lock);
  614. switch (cmd) {
  615. case SNDRV_PCM_TRIGGER_RESUME:
  616. case SNDRV_PCM_TRIGGER_START:
  617. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  618. for (i = 0; i < DSP_MAXPIPES; i++) {
  619. if (channelmask & (1 << i)) {
  620. pipe = chip->substream[i]->runtime->private_data;
  621. switch (pipe->state) {
  622. case PIPE_STATE_STOPPED:
  623. pipe->last_period = 0;
  624. pipe->last_counter = 0;
  625. pipe->position = 0;
  626. *pipe->dma_counter = 0;
  627. fallthrough;
  628. case PIPE_STATE_PAUSED:
  629. pipe->state = PIPE_STATE_STARTED;
  630. break;
  631. case PIPE_STATE_STARTED:
  632. break;
  633. }
  634. }
  635. }
  636. err = start_transport(chip, channelmask,
  637. chip->pipe_cyclic_mask);
  638. break;
  639. case SNDRV_PCM_TRIGGER_SUSPEND:
  640. case SNDRV_PCM_TRIGGER_STOP:
  641. for (i = 0; i < DSP_MAXPIPES; i++) {
  642. if (channelmask & (1 << i)) {
  643. pipe = chip->substream[i]->runtime->private_data;
  644. pipe->state = PIPE_STATE_STOPPED;
  645. }
  646. }
  647. err = stop_transport(chip, channelmask);
  648. break;
  649. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  650. for (i = 0; i < DSP_MAXPIPES; i++) {
  651. if (channelmask & (1 << i)) {
  652. pipe = chip->substream[i]->runtime->private_data;
  653. pipe->state = PIPE_STATE_PAUSED;
  654. }
  655. }
  656. err = pause_transport(chip, channelmask);
  657. break;
  658. default:
  659. err = -EINVAL;
  660. }
  661. spin_unlock(&chip->lock);
  662. return err;
  663. }
  664. static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
  665. {
  666. struct snd_pcm_runtime *runtime = substream->runtime;
  667. struct audiopipe *pipe = runtime->private_data;
  668. u32 counter, step;
  669. /*
  670. * IRQ handling runs concurrently. Do not share tracking of
  671. * counter with it, which would race or require locking
  672. */
  673. counter = le32_to_cpu(*pipe->dma_counter); /* presumed atomic */
  674. step = counter - pipe->last_counter; /* handles wrapping */
  675. pipe->last_counter = counter;
  676. /* counter doesn't neccessarily wrap on a multiple of
  677. * buffer_size, so can't derive the position; must
  678. * accumulate */
  679. pipe->position += step;
  680. pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
  681. return bytes_to_frames(runtime, pipe->position);
  682. }
  683. /* pcm *_ops structures */
  684. static const struct snd_pcm_ops analog_playback_ops = {
  685. .open = pcm_analog_out_open,
  686. .close = pcm_close,
  687. .hw_params = pcm_analog_out_hw_params,
  688. .hw_free = pcm_hw_free,
  689. .prepare = pcm_prepare,
  690. .trigger = pcm_trigger,
  691. .pointer = pcm_pointer,
  692. };
  693. static const struct snd_pcm_ops analog_capture_ops = {
  694. .open = pcm_analog_in_open,
  695. .close = pcm_close,
  696. .hw_params = pcm_analog_in_hw_params,
  697. .hw_free = pcm_hw_free,
  698. .prepare = pcm_prepare,
  699. .trigger = pcm_trigger,
  700. .pointer = pcm_pointer,
  701. };
  702. #ifdef ECHOCARD_HAS_DIGITAL_IO
  703. #ifndef ECHOCARD_HAS_VMIXER
  704. static const struct snd_pcm_ops digital_playback_ops = {
  705. .open = pcm_digital_out_open,
  706. .close = pcm_close,
  707. .hw_params = pcm_digital_out_hw_params,
  708. .hw_free = pcm_hw_free,
  709. .prepare = pcm_prepare,
  710. .trigger = pcm_trigger,
  711. .pointer = pcm_pointer,
  712. };
  713. #endif /* !ECHOCARD_HAS_VMIXER */
  714. static const struct snd_pcm_ops digital_capture_ops = {
  715. .open = pcm_digital_in_open,
  716. .close = pcm_close,
  717. .hw_params = pcm_digital_in_hw_params,
  718. .hw_free = pcm_hw_free,
  719. .prepare = pcm_prepare,
  720. .trigger = pcm_trigger,
  721. .pointer = pcm_pointer,
  722. };
  723. #endif /* ECHOCARD_HAS_DIGITAL_IO */
  724. /* Preallocate memory only for the first substream because it's the most
  725. * used one
  726. */
  727. static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
  728. {
  729. struct snd_pcm_substream *ss;
  730. int stream;
  731. for (stream = 0; stream < 2; stream++)
  732. for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
  733. snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
  734. dev,
  735. ss->number ? 0 : 128<<10,
  736. 256<<10);
  737. }
  738. /*<--snd_echo_probe() */
  739. static int snd_echo_new_pcm(struct echoaudio *chip)
  740. {
  741. struct snd_pcm *pcm;
  742. int err;
  743. #ifdef ECHOCARD_HAS_VMIXER
  744. /* This card has a Vmixer, that is there is no direct mapping from PCM
  745. streams to physical outputs. The user can mix the streams as he wishes
  746. via control interface and it's possible to send any stream to any
  747. output, thus it makes no sense to keep analog and digital outputs
  748. separated */
  749. /* PCM#0 Virtual outputs and analog inputs */
  750. err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
  751. num_analog_busses_in(chip), &pcm);
  752. if (err < 0)
  753. return err;
  754. pcm->private_data = chip;
  755. chip->analog_pcm = pcm;
  756. strcpy(pcm->name, chip->card->shortname);
  757. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
  758. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
  759. snd_echo_preallocate_pages(pcm, &chip->pci->dev);
  760. #ifdef ECHOCARD_HAS_DIGITAL_IO
  761. /* PCM#1 Digital inputs, no outputs */
  762. err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
  763. num_digital_busses_in(chip), &pcm);
  764. if (err < 0)
  765. return err;
  766. pcm->private_data = chip;
  767. chip->digital_pcm = pcm;
  768. strcpy(pcm->name, chip->card->shortname);
  769. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
  770. snd_echo_preallocate_pages(pcm, &chip->pci->dev);
  771. #endif /* ECHOCARD_HAS_DIGITAL_IO */
  772. #else /* ECHOCARD_HAS_VMIXER */
  773. /* The card can manage substreams formed by analog and digital channels
  774. at the same time, but I prefer to keep analog and digital channels
  775. separated, because that mixed thing is confusing and useless. So we
  776. register two PCM devices: */
  777. /* PCM#0 Analog i/o */
  778. err = snd_pcm_new(chip->card, "Analog PCM", 0,
  779. num_analog_busses_out(chip),
  780. num_analog_busses_in(chip), &pcm);
  781. if (err < 0)
  782. return err;
  783. pcm->private_data = chip;
  784. chip->analog_pcm = pcm;
  785. strcpy(pcm->name, chip->card->shortname);
  786. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
  787. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
  788. snd_echo_preallocate_pages(pcm, &chip->pci->dev);
  789. #ifdef ECHOCARD_HAS_DIGITAL_IO
  790. /* PCM#1 Digital i/o */
  791. err = snd_pcm_new(chip->card, "Digital PCM", 1,
  792. num_digital_busses_out(chip),
  793. num_digital_busses_in(chip), &pcm);
  794. if (err < 0)
  795. return err;
  796. pcm->private_data = chip;
  797. chip->digital_pcm = pcm;
  798. strcpy(pcm->name, chip->card->shortname);
  799. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
  800. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
  801. snd_echo_preallocate_pages(pcm, &chip->pci->dev);
  802. #endif /* ECHOCARD_HAS_DIGITAL_IO */
  803. #endif /* ECHOCARD_HAS_VMIXER */
  804. return 0;
  805. }
  806. /******************************************************************************
  807. Control interface
  808. ******************************************************************************/
  809. #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
  810. /******************* PCM output volume *******************/
  811. static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
  812. struct snd_ctl_elem_info *uinfo)
  813. {
  814. struct echoaudio *chip;
  815. chip = snd_kcontrol_chip(kcontrol);
  816. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  817. uinfo->count = num_busses_out(chip);
  818. uinfo->value.integer.min = ECHOGAIN_MINOUT;
  819. uinfo->value.integer.max = ECHOGAIN_MAXOUT;
  820. return 0;
  821. }
  822. static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
  823. struct snd_ctl_elem_value *ucontrol)
  824. {
  825. struct echoaudio *chip;
  826. int c;
  827. chip = snd_kcontrol_chip(kcontrol);
  828. for (c = 0; c < num_busses_out(chip); c++)
  829. ucontrol->value.integer.value[c] = chip->output_gain[c];
  830. return 0;
  831. }
  832. static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
  833. struct snd_ctl_elem_value *ucontrol)
  834. {
  835. struct echoaudio *chip;
  836. int c, changed, gain;
  837. changed = 0;
  838. chip = snd_kcontrol_chip(kcontrol);
  839. spin_lock_irq(&chip->lock);
  840. for (c = 0; c < num_busses_out(chip); c++) {
  841. gain = ucontrol->value.integer.value[c];
  842. /* Ignore out of range values */
  843. if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
  844. continue;
  845. if (chip->output_gain[c] != gain) {
  846. set_output_gain(chip, c, gain);
  847. changed = 1;
  848. }
  849. }
  850. if (changed)
  851. update_output_line_level(chip);
  852. spin_unlock_irq(&chip->lock);
  853. return changed;
  854. }
  855. #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
  856. /* On the Mia this one controls the line-out volume */
  857. static const struct snd_kcontrol_new snd_echo_line_output_gain = {
  858. .name = "Line Playback Volume",
  859. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  860. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  861. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  862. .info = snd_echo_output_gain_info,
  863. .get = snd_echo_output_gain_get,
  864. .put = snd_echo_output_gain_put,
  865. .tlv = {.p = db_scale_output_gain},
  866. };
  867. #else
  868. static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
  869. .name = "PCM Playback Volume",
  870. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  871. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  872. .info = snd_echo_output_gain_info,
  873. .get = snd_echo_output_gain_get,
  874. .put = snd_echo_output_gain_put,
  875. .tlv = {.p = db_scale_output_gain},
  876. };
  877. #endif
  878. #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
  879. #ifdef ECHOCARD_HAS_INPUT_GAIN
  880. /******************* Analog input volume *******************/
  881. static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
  882. struct snd_ctl_elem_info *uinfo)
  883. {
  884. struct echoaudio *chip;
  885. chip = snd_kcontrol_chip(kcontrol);
  886. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  887. uinfo->count = num_analog_busses_in(chip);
  888. uinfo->value.integer.min = ECHOGAIN_MININP;
  889. uinfo->value.integer.max = ECHOGAIN_MAXINP;
  890. return 0;
  891. }
  892. static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
  893. struct snd_ctl_elem_value *ucontrol)
  894. {
  895. struct echoaudio *chip;
  896. int c;
  897. chip = snd_kcontrol_chip(kcontrol);
  898. for (c = 0; c < num_analog_busses_in(chip); c++)
  899. ucontrol->value.integer.value[c] = chip->input_gain[c];
  900. return 0;
  901. }
  902. static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
  903. struct snd_ctl_elem_value *ucontrol)
  904. {
  905. struct echoaudio *chip;
  906. int c, gain, changed;
  907. changed = 0;
  908. chip = snd_kcontrol_chip(kcontrol);
  909. spin_lock_irq(&chip->lock);
  910. for (c = 0; c < num_analog_busses_in(chip); c++) {
  911. gain = ucontrol->value.integer.value[c];
  912. /* Ignore out of range values */
  913. if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
  914. continue;
  915. if (chip->input_gain[c] != gain) {
  916. set_input_gain(chip, c, gain);
  917. changed = 1;
  918. }
  919. }
  920. if (changed)
  921. update_input_line_level(chip);
  922. spin_unlock_irq(&chip->lock);
  923. return changed;
  924. }
  925. static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
  926. static const struct snd_kcontrol_new snd_echo_line_input_gain = {
  927. .name = "Line Capture Volume",
  928. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  929. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  930. .info = snd_echo_input_gain_info,
  931. .get = snd_echo_input_gain_get,
  932. .put = snd_echo_input_gain_put,
  933. .tlv = {.p = db_scale_input_gain},
  934. };
  935. #endif /* ECHOCARD_HAS_INPUT_GAIN */
  936. #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
  937. /************ Analog output nominal level (+4dBu / -10dBV) ***************/
  938. static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
  939. struct snd_ctl_elem_info *uinfo)
  940. {
  941. struct echoaudio *chip;
  942. chip = snd_kcontrol_chip(kcontrol);
  943. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  944. uinfo->count = num_analog_busses_out(chip);
  945. uinfo->value.integer.min = 0;
  946. uinfo->value.integer.max = 1;
  947. return 0;
  948. }
  949. static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
  950. struct snd_ctl_elem_value *ucontrol)
  951. {
  952. struct echoaudio *chip;
  953. int c;
  954. chip = snd_kcontrol_chip(kcontrol);
  955. for (c = 0; c < num_analog_busses_out(chip); c++)
  956. ucontrol->value.integer.value[c] = chip->nominal_level[c];
  957. return 0;
  958. }
  959. static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
  960. struct snd_ctl_elem_value *ucontrol)
  961. {
  962. struct echoaudio *chip;
  963. int c, changed;
  964. changed = 0;
  965. chip = snd_kcontrol_chip(kcontrol);
  966. spin_lock_irq(&chip->lock);
  967. for (c = 0; c < num_analog_busses_out(chip); c++) {
  968. if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
  969. set_nominal_level(chip, c,
  970. ucontrol->value.integer.value[c]);
  971. changed = 1;
  972. }
  973. }
  974. if (changed)
  975. update_output_line_level(chip);
  976. spin_unlock_irq(&chip->lock);
  977. return changed;
  978. }
  979. static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
  980. .name = "Line Playback Switch (-10dBV)",
  981. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  982. .info = snd_echo_output_nominal_info,
  983. .get = snd_echo_output_nominal_get,
  984. .put = snd_echo_output_nominal_put,
  985. };
  986. #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
  987. #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
  988. /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
  989. static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
  990. struct snd_ctl_elem_info *uinfo)
  991. {
  992. struct echoaudio *chip;
  993. chip = snd_kcontrol_chip(kcontrol);
  994. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  995. uinfo->count = num_analog_busses_in(chip);
  996. uinfo->value.integer.min = 0;
  997. uinfo->value.integer.max = 1;
  998. return 0;
  999. }
  1000. static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
  1001. struct snd_ctl_elem_value *ucontrol)
  1002. {
  1003. struct echoaudio *chip;
  1004. int c;
  1005. chip = snd_kcontrol_chip(kcontrol);
  1006. for (c = 0; c < num_analog_busses_in(chip); c++)
  1007. ucontrol->value.integer.value[c] =
  1008. chip->nominal_level[bx_analog_in(chip) + c];
  1009. return 0;
  1010. }
  1011. static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
  1012. struct snd_ctl_elem_value *ucontrol)
  1013. {
  1014. struct echoaudio *chip;
  1015. int c, changed;
  1016. changed = 0;
  1017. chip = snd_kcontrol_chip(kcontrol);
  1018. spin_lock_irq(&chip->lock);
  1019. for (c = 0; c < num_analog_busses_in(chip); c++) {
  1020. if (chip->nominal_level[bx_analog_in(chip) + c] !=
  1021. ucontrol->value.integer.value[c]) {
  1022. set_nominal_level(chip, bx_analog_in(chip) + c,
  1023. ucontrol->value.integer.value[c]);
  1024. changed = 1;
  1025. }
  1026. }
  1027. if (changed)
  1028. update_output_line_level(chip); /* "Output" is not a mistake
  1029. * here.
  1030. */
  1031. spin_unlock_irq(&chip->lock);
  1032. return changed;
  1033. }
  1034. static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
  1035. .name = "Line Capture Switch (-10dBV)",
  1036. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1037. .info = snd_echo_input_nominal_info,
  1038. .get = snd_echo_input_nominal_get,
  1039. .put = snd_echo_input_nominal_put,
  1040. };
  1041. #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
  1042. #ifdef ECHOCARD_HAS_MONITOR
  1043. /******************* Monitor mixer *******************/
  1044. static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
  1045. struct snd_ctl_elem_info *uinfo)
  1046. {
  1047. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1048. uinfo->count = 1;
  1049. uinfo->value.integer.min = ECHOGAIN_MINOUT;
  1050. uinfo->value.integer.max = ECHOGAIN_MAXOUT;
  1051. return 0;
  1052. }
  1053. static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
  1054. struct snd_ctl_elem_value *ucontrol)
  1055. {
  1056. struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
  1057. unsigned int out = ucontrol->id.index / num_busses_in(chip);
  1058. unsigned int in = ucontrol->id.index % num_busses_in(chip);
  1059. if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
  1060. return -EINVAL;
  1061. ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
  1062. return 0;
  1063. }
  1064. static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
  1065. struct snd_ctl_elem_value *ucontrol)
  1066. {
  1067. struct echoaudio *chip;
  1068. int changed, gain;
  1069. unsigned int out, in;
  1070. changed = 0;
  1071. chip = snd_kcontrol_chip(kcontrol);
  1072. out = ucontrol->id.index / num_busses_in(chip);
  1073. in = ucontrol->id.index % num_busses_in(chip);
  1074. if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
  1075. return -EINVAL;
  1076. gain = ucontrol->value.integer.value[0];
  1077. if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
  1078. return -EINVAL;
  1079. if (chip->monitor_gain[out][in] != gain) {
  1080. spin_lock_irq(&chip->lock);
  1081. set_monitor_gain(chip, out, in, gain);
  1082. update_output_line_level(chip);
  1083. spin_unlock_irq(&chip->lock);
  1084. changed = 1;
  1085. }
  1086. return changed;
  1087. }
  1088. static struct snd_kcontrol_new snd_echo_monitor_mixer = {
  1089. .name = "Monitor Mixer Volume",
  1090. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1091. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  1092. .info = snd_echo_mixer_info,
  1093. .get = snd_echo_mixer_get,
  1094. .put = snd_echo_mixer_put,
  1095. .tlv = {.p = db_scale_output_gain},
  1096. };
  1097. #endif /* ECHOCARD_HAS_MONITOR */
  1098. #ifdef ECHOCARD_HAS_VMIXER
  1099. /******************* Vmixer *******************/
  1100. static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
  1101. struct snd_ctl_elem_info *uinfo)
  1102. {
  1103. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1104. uinfo->count = 1;
  1105. uinfo->value.integer.min = ECHOGAIN_MINOUT;
  1106. uinfo->value.integer.max = ECHOGAIN_MAXOUT;
  1107. return 0;
  1108. }
  1109. static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
  1110. struct snd_ctl_elem_value *ucontrol)
  1111. {
  1112. struct echoaudio *chip;
  1113. chip = snd_kcontrol_chip(kcontrol);
  1114. ucontrol->value.integer.value[0] =
  1115. chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
  1116. [ucontrol->id.index % num_pipes_out(chip)];
  1117. return 0;
  1118. }
  1119. static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
  1120. struct snd_ctl_elem_value *ucontrol)
  1121. {
  1122. struct echoaudio *chip;
  1123. int gain, changed;
  1124. short vch, out;
  1125. changed = 0;
  1126. chip = snd_kcontrol_chip(kcontrol);
  1127. out = ucontrol->id.index / num_pipes_out(chip);
  1128. vch = ucontrol->id.index % num_pipes_out(chip);
  1129. gain = ucontrol->value.integer.value[0];
  1130. if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
  1131. return -EINVAL;
  1132. if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
  1133. spin_lock_irq(&chip->lock);
  1134. set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
  1135. update_vmixer_level(chip);
  1136. spin_unlock_irq(&chip->lock);
  1137. changed = 1;
  1138. }
  1139. return changed;
  1140. }
  1141. static struct snd_kcontrol_new snd_echo_vmixer = {
  1142. .name = "VMixer Volume",
  1143. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1144. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  1145. .info = snd_echo_vmixer_info,
  1146. .get = snd_echo_vmixer_get,
  1147. .put = snd_echo_vmixer_put,
  1148. .tlv = {.p = db_scale_output_gain},
  1149. };
  1150. #endif /* ECHOCARD_HAS_VMIXER */
  1151. #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
  1152. /******************* Digital mode switch *******************/
  1153. static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
  1154. struct snd_ctl_elem_info *uinfo)
  1155. {
  1156. static const char * const names[4] = {
  1157. "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
  1158. "S/PDIF Cdrom"
  1159. };
  1160. struct echoaudio *chip;
  1161. chip = snd_kcontrol_chip(kcontrol);
  1162. return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
  1163. }
  1164. static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
  1165. struct snd_ctl_elem_value *ucontrol)
  1166. {
  1167. struct echoaudio *chip;
  1168. int i, mode;
  1169. chip = snd_kcontrol_chip(kcontrol);
  1170. mode = chip->digital_mode;
  1171. for (i = chip->num_digital_modes - 1; i >= 0; i--)
  1172. if (mode == chip->digital_mode_list[i]) {
  1173. ucontrol->value.enumerated.item[0] = i;
  1174. break;
  1175. }
  1176. return 0;
  1177. }
  1178. static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
  1179. struct snd_ctl_elem_value *ucontrol)
  1180. {
  1181. struct echoaudio *chip;
  1182. int changed;
  1183. unsigned short emode, dmode;
  1184. changed = 0;
  1185. chip = snd_kcontrol_chip(kcontrol);
  1186. emode = ucontrol->value.enumerated.item[0];
  1187. if (emode >= chip->num_digital_modes)
  1188. return -EINVAL;
  1189. dmode = chip->digital_mode_list[emode];
  1190. if (dmode != chip->digital_mode) {
  1191. /* mode_mutex is required to make this operation atomic wrt
  1192. pcm_digital_*_open() and set_input_clock() functions. */
  1193. mutex_lock(&chip->mode_mutex);
  1194. /* Do not allow the user to change the digital mode when a pcm
  1195. device is open because it also changes the number of channels
  1196. and the allowed sample rates */
  1197. if (chip->opencount) {
  1198. changed = -EAGAIN;
  1199. } else {
  1200. changed = set_digital_mode(chip, dmode);
  1201. /* If we had to change the clock source, report it */
  1202. if (changed > 0 && chip->clock_src_ctl) {
  1203. snd_ctl_notify(chip->card,
  1204. SNDRV_CTL_EVENT_MASK_VALUE,
  1205. &chip->clock_src_ctl->id);
  1206. dev_dbg(chip->card->dev,
  1207. "SDM() =%d\n", changed);
  1208. }
  1209. if (changed >= 0)
  1210. changed = 1; /* No errors */
  1211. }
  1212. mutex_unlock(&chip->mode_mutex);
  1213. }
  1214. return changed;
  1215. }
  1216. static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
  1217. .name = "Digital mode Switch",
  1218. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1219. .info = snd_echo_digital_mode_info,
  1220. .get = snd_echo_digital_mode_get,
  1221. .put = snd_echo_digital_mode_put,
  1222. };
  1223. #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
  1224. #ifdef ECHOCARD_HAS_DIGITAL_IO
  1225. /******************* S/PDIF mode switch *******************/
  1226. static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
  1227. struct snd_ctl_elem_info *uinfo)
  1228. {
  1229. static const char * const names[2] = {"Consumer", "Professional"};
  1230. return snd_ctl_enum_info(uinfo, 1, 2, names);
  1231. }
  1232. static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
  1233. struct snd_ctl_elem_value *ucontrol)
  1234. {
  1235. struct echoaudio *chip;
  1236. chip = snd_kcontrol_chip(kcontrol);
  1237. ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
  1238. return 0;
  1239. }
  1240. static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
  1241. struct snd_ctl_elem_value *ucontrol)
  1242. {
  1243. struct echoaudio *chip;
  1244. int mode;
  1245. chip = snd_kcontrol_chip(kcontrol);
  1246. mode = !!ucontrol->value.enumerated.item[0];
  1247. if (mode != chip->professional_spdif) {
  1248. spin_lock_irq(&chip->lock);
  1249. set_professional_spdif(chip, mode);
  1250. spin_unlock_irq(&chip->lock);
  1251. return 1;
  1252. }
  1253. return 0;
  1254. }
  1255. static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
  1256. .name = "S/PDIF mode Switch",
  1257. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1258. .info = snd_echo_spdif_mode_info,
  1259. .get = snd_echo_spdif_mode_get,
  1260. .put = snd_echo_spdif_mode_put,
  1261. };
  1262. #endif /* ECHOCARD_HAS_DIGITAL_IO */
  1263. #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
  1264. /******************* Select input clock source *******************/
  1265. static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
  1266. struct snd_ctl_elem_info *uinfo)
  1267. {
  1268. static const char * const names[8] = {
  1269. "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
  1270. "ESync96", "MTC"
  1271. };
  1272. struct echoaudio *chip;
  1273. chip = snd_kcontrol_chip(kcontrol);
  1274. return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
  1275. }
  1276. static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
  1277. struct snd_ctl_elem_value *ucontrol)
  1278. {
  1279. struct echoaudio *chip;
  1280. int i, clock;
  1281. chip = snd_kcontrol_chip(kcontrol);
  1282. clock = chip->input_clock;
  1283. for (i = 0; i < chip->num_clock_sources; i++)
  1284. if (clock == chip->clock_source_list[i])
  1285. ucontrol->value.enumerated.item[0] = i;
  1286. return 0;
  1287. }
  1288. static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
  1289. struct snd_ctl_elem_value *ucontrol)
  1290. {
  1291. struct echoaudio *chip;
  1292. int changed;
  1293. unsigned int eclock, dclock;
  1294. changed = 0;
  1295. chip = snd_kcontrol_chip(kcontrol);
  1296. eclock = ucontrol->value.enumerated.item[0];
  1297. if (eclock >= chip->input_clock_types)
  1298. return -EINVAL;
  1299. dclock = chip->clock_source_list[eclock];
  1300. if (chip->input_clock != dclock) {
  1301. mutex_lock(&chip->mode_mutex);
  1302. spin_lock_irq(&chip->lock);
  1303. changed = set_input_clock(chip, dclock);
  1304. if (!changed)
  1305. changed = 1; /* no errors */
  1306. spin_unlock_irq(&chip->lock);
  1307. mutex_unlock(&chip->mode_mutex);
  1308. }
  1309. if (changed < 0)
  1310. dev_dbg(chip->card->dev,
  1311. "seticlk val%d err 0x%x\n", dclock, changed);
  1312. return changed;
  1313. }
  1314. static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
  1315. .name = "Sample Clock Source",
  1316. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1317. .info = snd_echo_clock_source_info,
  1318. .get = snd_echo_clock_source_get,
  1319. .put = snd_echo_clock_source_put,
  1320. };
  1321. #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
  1322. #ifdef ECHOCARD_HAS_PHANTOM_POWER
  1323. /******************* Phantom power switch *******************/
  1324. #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
  1325. static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
  1326. struct snd_ctl_elem_value *ucontrol)
  1327. {
  1328. struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
  1329. ucontrol->value.integer.value[0] = chip->phantom_power;
  1330. return 0;
  1331. }
  1332. static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
  1333. struct snd_ctl_elem_value *ucontrol)
  1334. {
  1335. struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
  1336. int power, changed = 0;
  1337. power = !!ucontrol->value.integer.value[0];
  1338. if (chip->phantom_power != power) {
  1339. spin_lock_irq(&chip->lock);
  1340. changed = set_phantom_power(chip, power);
  1341. spin_unlock_irq(&chip->lock);
  1342. if (changed == 0)
  1343. changed = 1; /* no errors */
  1344. }
  1345. return changed;
  1346. }
  1347. static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
  1348. .name = "Phantom power Switch",
  1349. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1350. .info = snd_echo_phantom_power_info,
  1351. .get = snd_echo_phantom_power_get,
  1352. .put = snd_echo_phantom_power_put,
  1353. };
  1354. #endif /* ECHOCARD_HAS_PHANTOM_POWER */
  1355. #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
  1356. /******************* Digital input automute switch *******************/
  1357. #define snd_echo_automute_info snd_ctl_boolean_mono_info
  1358. static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
  1359. struct snd_ctl_elem_value *ucontrol)
  1360. {
  1361. struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
  1362. ucontrol->value.integer.value[0] = chip->digital_in_automute;
  1363. return 0;
  1364. }
  1365. static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
  1366. struct snd_ctl_elem_value *ucontrol)
  1367. {
  1368. struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
  1369. int automute, changed = 0;
  1370. automute = !!ucontrol->value.integer.value[0];
  1371. if (chip->digital_in_automute != automute) {
  1372. spin_lock_irq(&chip->lock);
  1373. changed = set_input_auto_mute(chip, automute);
  1374. spin_unlock_irq(&chip->lock);
  1375. if (changed == 0)
  1376. changed = 1; /* no errors */
  1377. }
  1378. return changed;
  1379. }
  1380. static const struct snd_kcontrol_new snd_echo_automute_switch = {
  1381. .name = "Digital Capture Switch (automute)",
  1382. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1383. .info = snd_echo_automute_info,
  1384. .get = snd_echo_automute_get,
  1385. .put = snd_echo_automute_put,
  1386. };
  1387. #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
  1388. /******************* VU-meters switch *******************/
  1389. #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
  1390. static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
  1391. struct snd_ctl_elem_value *ucontrol)
  1392. {
  1393. struct echoaudio *chip;
  1394. chip = snd_kcontrol_chip(kcontrol);
  1395. spin_lock_irq(&chip->lock);
  1396. set_meters_on(chip, ucontrol->value.integer.value[0]);
  1397. spin_unlock_irq(&chip->lock);
  1398. return 1;
  1399. }
  1400. static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
  1401. .name = "VU-meters Switch",
  1402. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1403. .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
  1404. .info = snd_echo_vumeters_switch_info,
  1405. .put = snd_echo_vumeters_switch_put,
  1406. };
  1407. /***** Read VU-meters (input, output, analog and digital together) *****/
  1408. static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
  1409. struct snd_ctl_elem_info *uinfo)
  1410. {
  1411. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1412. uinfo->count = 96;
  1413. uinfo->value.integer.min = ECHOGAIN_MINOUT;
  1414. uinfo->value.integer.max = 0;
  1415. return 0;
  1416. }
  1417. static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
  1418. struct snd_ctl_elem_value *ucontrol)
  1419. {
  1420. struct echoaudio *chip;
  1421. chip = snd_kcontrol_chip(kcontrol);
  1422. get_audio_meters(chip, ucontrol->value.integer.value);
  1423. return 0;
  1424. }
  1425. static const struct snd_kcontrol_new snd_echo_vumeters = {
  1426. .name = "VU-meters",
  1427. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1428. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  1429. SNDRV_CTL_ELEM_ACCESS_VOLATILE |
  1430. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  1431. .info = snd_echo_vumeters_info,
  1432. .get = snd_echo_vumeters_get,
  1433. .tlv = {.p = db_scale_output_gain},
  1434. };
  1435. /*** Channels info - it exports informations about the number of channels ***/
  1436. static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
  1437. struct snd_ctl_elem_info *uinfo)
  1438. {
  1439. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1440. uinfo->count = 6;
  1441. uinfo->value.integer.min = 0;
  1442. uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
  1443. return 0;
  1444. }
  1445. static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
  1446. struct snd_ctl_elem_value *ucontrol)
  1447. {
  1448. struct echoaudio *chip;
  1449. int detected, clocks, bit, src;
  1450. chip = snd_kcontrol_chip(kcontrol);
  1451. ucontrol->value.integer.value[0] = num_busses_in(chip);
  1452. ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
  1453. ucontrol->value.integer.value[2] = num_busses_out(chip);
  1454. ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
  1455. ucontrol->value.integer.value[4] = num_pipes_out(chip);
  1456. /* Compute the bitmask of the currently valid input clocks */
  1457. detected = detect_input_clocks(chip);
  1458. clocks = 0;
  1459. src = chip->num_clock_sources - 1;
  1460. for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
  1461. if (detected & (1 << bit))
  1462. for (; src >= 0; src--)
  1463. if (bit == chip->clock_source_list[src]) {
  1464. clocks |= 1 << src;
  1465. break;
  1466. }
  1467. ucontrol->value.integer.value[5] = clocks;
  1468. return 0;
  1469. }
  1470. static const struct snd_kcontrol_new snd_echo_channels_info = {
  1471. .name = "Channels info",
  1472. .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
  1473. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  1474. .info = snd_echo_channels_info_info,
  1475. .get = snd_echo_channels_info_get,
  1476. };
  1477. /******************************************************************************
  1478. IRQ Handling
  1479. ******************************************************************************/
  1480. /* Check if a period has elapsed since last interrupt
  1481. *
  1482. * Don't make any updates to state; PCM core handles this with the
  1483. * correct locks.
  1484. *
  1485. * \return true if a period has elapsed, otherwise false
  1486. */
  1487. static bool period_has_elapsed(struct snd_pcm_substream *substream)
  1488. {
  1489. struct snd_pcm_runtime *runtime = substream->runtime;
  1490. struct audiopipe *pipe = runtime->private_data;
  1491. u32 counter, step;
  1492. size_t period_bytes;
  1493. if (pipe->state != PIPE_STATE_STARTED)
  1494. return false;
  1495. period_bytes = frames_to_bytes(runtime, runtime->period_size);
  1496. counter = le32_to_cpu(*pipe->dma_counter); /* presumed atomic */
  1497. step = counter - pipe->last_period; /* handles wrapping */
  1498. step -= step % period_bytes; /* acknowledge whole periods only */
  1499. if (step == 0)
  1500. return false; /* haven't advanced a whole period yet */
  1501. pipe->last_period += step; /* used exclusively by us */
  1502. return true;
  1503. }
  1504. static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
  1505. {
  1506. struct echoaudio *chip = dev_id;
  1507. int ss, st;
  1508. spin_lock(&chip->lock);
  1509. st = service_irq(chip);
  1510. if (st < 0) {
  1511. spin_unlock(&chip->lock);
  1512. return IRQ_NONE;
  1513. }
  1514. /* The hardware doesn't tell us which substream caused the irq,
  1515. thus we have to check all running substreams. */
  1516. for (ss = 0; ss < DSP_MAXPIPES; ss++) {
  1517. struct snd_pcm_substream *substream;
  1518. substream = chip->substream[ss];
  1519. if (substream && period_has_elapsed(substream)) {
  1520. spin_unlock(&chip->lock);
  1521. snd_pcm_period_elapsed(substream);
  1522. spin_lock(&chip->lock);
  1523. }
  1524. }
  1525. spin_unlock(&chip->lock);
  1526. #ifdef ECHOCARD_HAS_MIDI
  1527. if (st > 0 && chip->midi_in) {
  1528. snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
  1529. dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
  1530. }
  1531. #endif
  1532. return IRQ_HANDLED;
  1533. }
  1534. /******************************************************************************
  1535. Module construction / destruction
  1536. ******************************************************************************/
  1537. static void snd_echo_free(struct snd_card *card)
  1538. {
  1539. struct echoaudio *chip = card->private_data;
  1540. if (chip->comm_page)
  1541. rest_in_peace(chip);
  1542. if (chip->irq >= 0)
  1543. free_irq(chip->irq, chip);
  1544. /* release chip data */
  1545. free_firmware_cache(chip);
  1546. }
  1547. /* <--snd_echo_probe() */
  1548. static int snd_echo_create(struct snd_card *card,
  1549. struct pci_dev *pci)
  1550. {
  1551. struct echoaudio *chip = card->private_data;
  1552. int err;
  1553. size_t sz;
  1554. pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
  1555. err = pcim_enable_device(pci);
  1556. if (err < 0)
  1557. return err;
  1558. pci_set_master(pci);
  1559. /* Allocate chip if needed */
  1560. spin_lock_init(&chip->lock);
  1561. chip->card = card;
  1562. chip->pci = pci;
  1563. chip->irq = -1;
  1564. chip->opencount = 0;
  1565. mutex_init(&chip->mode_mutex);
  1566. chip->can_set_rate = 1;
  1567. /* PCI resource allocation */
  1568. err = pci_request_regions(pci, ECHOCARD_NAME);
  1569. if (err < 0)
  1570. return err;
  1571. chip->dsp_registers_phys = pci_resource_start(pci, 0);
  1572. sz = pci_resource_len(pci, 0);
  1573. if (sz > PAGE_SIZE)
  1574. sz = PAGE_SIZE; /* We map only the required part */
  1575. chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz);
  1576. if (!chip->dsp_registers) {
  1577. dev_err(chip->card->dev, "ioremap failed\n");
  1578. return -ENOMEM;
  1579. }
  1580. if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
  1581. KBUILD_MODNAME, chip)) {
  1582. dev_err(chip->card->dev, "cannot grab irq\n");
  1583. return -EBUSY;
  1584. }
  1585. chip->irq = pci->irq;
  1586. card->sync_irq = chip->irq;
  1587. dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
  1588. chip->pci, chip->irq, chip->pci->subsystem_device);
  1589. card->private_free = snd_echo_free;
  1590. /* Create the DSP comm page - this is the area of memory used for most
  1591. of the communication with the DSP, which accesses it via bus mastering */
  1592. chip->commpage_dma_buf =
  1593. snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
  1594. sizeof(struct comm_page));
  1595. if (!chip->commpage_dma_buf)
  1596. return -ENOMEM;
  1597. chip->comm_page_phys = chip->commpage_dma_buf->addr;
  1598. chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
  1599. err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
  1600. if (err >= 0)
  1601. err = set_mixer_defaults(chip);
  1602. if (err < 0) {
  1603. dev_err(card->dev, "init_hw err=%d\n", err);
  1604. return err;
  1605. }
  1606. return 0;
  1607. }
  1608. /* constructor */
  1609. static int __snd_echo_probe(struct pci_dev *pci,
  1610. const struct pci_device_id *pci_id)
  1611. {
  1612. static int dev;
  1613. struct snd_card *card;
  1614. struct echoaudio *chip;
  1615. char *dsp;
  1616. __maybe_unused int i;
  1617. int err;
  1618. if (dev >= SNDRV_CARDS)
  1619. return -ENODEV;
  1620. if (!enable[dev]) {
  1621. dev++;
  1622. return -ENOENT;
  1623. }
  1624. i = 0;
  1625. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  1626. sizeof(*chip), &card);
  1627. if (err < 0)
  1628. return err;
  1629. chip = card->private_data;
  1630. err = snd_echo_create(card, pci);
  1631. if (err < 0)
  1632. return err;
  1633. strcpy(card->driver, "Echo_" ECHOCARD_NAME);
  1634. strcpy(card->shortname, chip->card_name);
  1635. dsp = "56301";
  1636. if (pci_id->device == 0x3410)
  1637. dsp = "56361";
  1638. sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
  1639. card->shortname, pci_id->subdevice & 0x000f, dsp,
  1640. chip->dsp_registers_phys, chip->irq);
  1641. err = snd_echo_new_pcm(chip);
  1642. if (err < 0) {
  1643. dev_err(chip->card->dev, "new pcm error %d\n", err);
  1644. return err;
  1645. }
  1646. #ifdef ECHOCARD_HAS_MIDI
  1647. if (chip->has_midi) { /* Some Mia's do not have midi */
  1648. err = snd_echo_midi_create(card, chip);
  1649. if (err < 0) {
  1650. dev_err(chip->card->dev, "new midi error %d\n", err);
  1651. return err;
  1652. }
  1653. }
  1654. #endif
  1655. #ifdef ECHOCARD_HAS_VMIXER
  1656. snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
  1657. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip));
  1658. if (err < 0)
  1659. return err;
  1660. #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
  1661. err = snd_ctl_add(chip->card,
  1662. snd_ctl_new1(&snd_echo_line_output_gain, chip));
  1663. if (err < 0)
  1664. return err;
  1665. #endif
  1666. #else /* ECHOCARD_HAS_VMIXER */
  1667. err = snd_ctl_add(chip->card,
  1668. snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
  1669. if (err < 0)
  1670. return err;
  1671. #endif /* ECHOCARD_HAS_VMIXER */
  1672. #ifdef ECHOCARD_HAS_INPUT_GAIN
  1673. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
  1674. if (err < 0)
  1675. return err;
  1676. #endif
  1677. #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
  1678. if (!chip->hasnt_input_nominal_level) {
  1679. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip));
  1680. if (err < 0)
  1681. return err;
  1682. }
  1683. #endif
  1684. #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
  1685. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
  1686. if (err < 0)
  1687. return err;
  1688. #endif
  1689. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
  1690. if (err < 0)
  1691. return err;
  1692. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
  1693. if (err < 0)
  1694. return err;
  1695. #ifdef ECHOCARD_HAS_MONITOR
  1696. snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
  1697. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip));
  1698. if (err < 0)
  1699. return err;
  1700. #endif
  1701. #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
  1702. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
  1703. if (err < 0)
  1704. return err;
  1705. #endif
  1706. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
  1707. if (err < 0)
  1708. return err;
  1709. #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
  1710. /* Creates a list of available digital modes */
  1711. chip->num_digital_modes = 0;
  1712. for (i = 0; i < 6; i++)
  1713. if (chip->digital_modes & (1 << i))
  1714. chip->digital_mode_list[chip->num_digital_modes++] = i;
  1715. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
  1716. if (err < 0)
  1717. return err;
  1718. #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
  1719. #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
  1720. /* Creates a list of available clock sources */
  1721. chip->num_clock_sources = 0;
  1722. for (i = 0; i < 10; i++)
  1723. if (chip->input_clock_types & (1 << i))
  1724. chip->clock_source_list[chip->num_clock_sources++] = i;
  1725. if (chip->num_clock_sources > 1) {
  1726. chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
  1727. err = snd_ctl_add(chip->card, chip->clock_src_ctl);
  1728. if (err < 0)
  1729. return err;
  1730. }
  1731. #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
  1732. #ifdef ECHOCARD_HAS_DIGITAL_IO
  1733. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
  1734. if (err < 0)
  1735. return err;
  1736. #endif
  1737. #ifdef ECHOCARD_HAS_PHANTOM_POWER
  1738. if (chip->has_phantom_power) {
  1739. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip));
  1740. if (err < 0)
  1741. return err;
  1742. }
  1743. #endif
  1744. err = snd_card_register(card);
  1745. if (err < 0)
  1746. return err;
  1747. dev_info(card->dev, "Card registered: %s\n", card->longname);
  1748. pci_set_drvdata(pci, chip);
  1749. dev++;
  1750. return 0;
  1751. }
  1752. static int snd_echo_probe(struct pci_dev *pci,
  1753. const struct pci_device_id *pci_id)
  1754. {
  1755. return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
  1756. }
  1757. #if defined(CONFIG_PM_SLEEP)
  1758. static int snd_echo_suspend(struct device *dev)
  1759. {
  1760. struct echoaudio *chip = dev_get_drvdata(dev);
  1761. #ifdef ECHOCARD_HAS_MIDI
  1762. /* This call can sleep */
  1763. if (chip->midi_out)
  1764. snd_echo_midi_output_trigger(chip->midi_out, 0);
  1765. #endif
  1766. spin_lock_irq(&chip->lock);
  1767. if (wait_handshake(chip)) {
  1768. spin_unlock_irq(&chip->lock);
  1769. return -EIO;
  1770. }
  1771. clear_handshake(chip);
  1772. if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
  1773. spin_unlock_irq(&chip->lock);
  1774. return -EIO;
  1775. }
  1776. spin_unlock_irq(&chip->lock);
  1777. chip->dsp_code = NULL;
  1778. free_irq(chip->irq, chip);
  1779. chip->irq = -1;
  1780. chip->card->sync_irq = -1;
  1781. return 0;
  1782. }
  1783. static int snd_echo_resume(struct device *dev)
  1784. {
  1785. struct pci_dev *pci = to_pci_dev(dev);
  1786. struct echoaudio *chip = dev_get_drvdata(dev);
  1787. struct comm_page *commpage, *commpage_bak;
  1788. u32 pipe_alloc_mask;
  1789. int err;
  1790. commpage = chip->comm_page;
  1791. commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
  1792. if (commpage_bak == NULL)
  1793. return -ENOMEM;
  1794. err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
  1795. if (err < 0) {
  1796. kfree(commpage_bak);
  1797. dev_err(dev, "resume init_hw err=%d\n", err);
  1798. return err;
  1799. }
  1800. /* Temporarily set chip->pipe_alloc_mask=0 otherwise
  1801. * restore_dsp_settings() fails.
  1802. */
  1803. pipe_alloc_mask = chip->pipe_alloc_mask;
  1804. chip->pipe_alloc_mask = 0;
  1805. err = restore_dsp_rettings(chip);
  1806. chip->pipe_alloc_mask = pipe_alloc_mask;
  1807. if (err < 0) {
  1808. kfree(commpage_bak);
  1809. return err;
  1810. }
  1811. memcpy(&commpage->audio_format, &commpage_bak->audio_format,
  1812. sizeof(commpage->audio_format));
  1813. memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
  1814. sizeof(commpage->sglist_addr));
  1815. memcpy(&commpage->midi_output, &commpage_bak->midi_output,
  1816. sizeof(commpage->midi_output));
  1817. kfree(commpage_bak);
  1818. if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
  1819. KBUILD_MODNAME, chip)) {
  1820. dev_err(chip->card->dev, "cannot grab irq\n");
  1821. return -EBUSY;
  1822. }
  1823. chip->irq = pci->irq;
  1824. chip->card->sync_irq = chip->irq;
  1825. dev_dbg(dev, "resume irq=%d\n", chip->irq);
  1826. #ifdef ECHOCARD_HAS_MIDI
  1827. if (chip->midi_input_enabled)
  1828. enable_midi_input(chip, true);
  1829. if (chip->midi_out)
  1830. snd_echo_midi_output_trigger(chip->midi_out, 1);
  1831. #endif
  1832. return 0;
  1833. }
  1834. static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
  1835. #define SND_ECHO_PM_OPS &snd_echo_pm
  1836. #else
  1837. #define SND_ECHO_PM_OPS NULL
  1838. #endif /* CONFIG_PM_SLEEP */
  1839. /******************************************************************************
  1840. Everything starts and ends here
  1841. ******************************************************************************/
  1842. /* pci_driver definition */
  1843. static struct pci_driver echo_driver = {
  1844. .name = KBUILD_MODNAME,
  1845. .id_table = snd_echo_ids,
  1846. .probe = snd_echo_probe,
  1847. .driver = {
  1848. .pm = SND_ECHO_PM_OPS,
  1849. },
  1850. };
  1851. module_pci_driver(echo_driver);