echoaudio.c 63 KB

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