echoaudio.c 63 KB

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