echoaudio.c 60 KB

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