gus_pcm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Routines for control of GF1 chip (PCM things)
  4. *
  5. * InterWave chips supports interleaved DMA, but this feature isn't used in
  6. * this code.
  7. *
  8. * This code emulates autoinit DMA transfer for playback, recording by GF1
  9. * chip doesn't support autoinit DMA.
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <sound/driver.h>
  28. #include <asm/dma.h>
  29. #include <linux/slab.h>
  30. #include <sound/core.h>
  31. #include <sound/control.h>
  32. #include <sound/gus.h>
  33. #include <sound/pcm_params.h>
  34. #include "gus_tables.h"
  35. /* maximum rate */
  36. #define SNDRV_GF1_PCM_RATE 48000
  37. #define SNDRV_GF1_PCM_PFLG_NONE 0
  38. #define SNDRV_GF1_PCM_PFLG_ACTIVE (1<<0)
  39. #define SNDRV_GF1_PCM_PFLG_NEUTRAL (2<<0)
  40. struct gus_pcm_private {
  41. struct snd_gus_card * gus;
  42. struct snd_pcm_substream *substream;
  43. spinlock_t lock;
  44. unsigned int voices;
  45. struct snd_gus_voice *pvoices[2];
  46. unsigned int memory;
  47. unsigned short flags;
  48. unsigned char voice_ctrl, ramp_ctrl;
  49. unsigned int bpos;
  50. unsigned int blocks;
  51. unsigned int block_size;
  52. unsigned int dma_size;
  53. wait_queue_head_t sleep;
  54. atomic_t dma_count;
  55. int final_volume;
  56. };
  57. static int snd_gf1_pcm_use_dma = 1;
  58. static void snd_gf1_pcm_block_change_ack(struct snd_gus_card * gus, void *private_data)
  59. {
  60. struct gus_pcm_private *pcmp = private_data;
  61. if (pcmp) {
  62. atomic_dec(&pcmp->dma_count);
  63. wake_up(&pcmp->sleep);
  64. }
  65. }
  66. static int snd_gf1_pcm_block_change(struct snd_pcm_substream *substream,
  67. unsigned int offset,
  68. unsigned int addr,
  69. unsigned int count)
  70. {
  71. struct snd_gf1_dma_block block;
  72. struct snd_pcm_runtime *runtime = substream->runtime;
  73. struct gus_pcm_private *pcmp = runtime->private_data;
  74. count += offset & 31;
  75. offset &= ~31;
  76. // snd_printk("block change - offset = 0x%x, count = 0x%x\n", offset, count);
  77. memset(&block, 0, sizeof(block));
  78. block.cmd = SNDRV_GF1_DMA_IRQ;
  79. if (snd_pcm_format_unsigned(runtime->format))
  80. block.cmd |= SNDRV_GF1_DMA_UNSIGNED;
  81. if (snd_pcm_format_width(runtime->format) == 16)
  82. block.cmd |= SNDRV_GF1_DMA_16BIT;
  83. block.addr = addr & ~31;
  84. block.buffer = runtime->dma_area + offset;
  85. block.buf_addr = runtime->dma_addr + offset;
  86. block.count = count;
  87. block.private_data = pcmp;
  88. block.ack = snd_gf1_pcm_block_change_ack;
  89. if (!snd_gf1_dma_transfer_block(pcmp->gus, &block, 0, 0))
  90. atomic_inc(&pcmp->dma_count);
  91. return 0;
  92. }
  93. static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream *substream)
  94. {
  95. struct snd_pcm_runtime *runtime = substream->runtime;
  96. struct gus_pcm_private *pcmp = runtime->private_data;
  97. struct snd_gus_card * gus = pcmp->gus;
  98. unsigned long flags;
  99. unsigned char voice_ctrl, ramp_ctrl;
  100. unsigned short rate;
  101. unsigned int curr, begin, end;
  102. unsigned short vol;
  103. unsigned char pan;
  104. unsigned int voice;
  105. spin_lock_irqsave(&pcmp->lock, flags);
  106. if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
  107. spin_unlock_irqrestore(&pcmp->lock, flags);
  108. return;
  109. }
  110. pcmp->flags |= SNDRV_GF1_PCM_PFLG_ACTIVE;
  111. pcmp->final_volume = 0;
  112. spin_unlock_irqrestore(&pcmp->lock, flags);
  113. rate = snd_gf1_translate_freq(gus, runtime->rate << 4);
  114. /* enable WAVE IRQ */
  115. voice_ctrl = snd_pcm_format_width(runtime->format) == 16 ? 0x24 : 0x20;
  116. /* enable RAMP IRQ + rollover */
  117. ramp_ctrl = 0x24;
  118. if (pcmp->blocks == 1) {
  119. voice_ctrl |= 0x08; /* loop enable */
  120. ramp_ctrl &= ~0x04; /* disable rollover */
  121. }
  122. for (voice = 0; voice < pcmp->voices; voice++) {
  123. begin = pcmp->memory + voice * (pcmp->dma_size / runtime->channels);
  124. curr = begin + (pcmp->bpos * pcmp->block_size) / runtime->channels;
  125. end = curr + (pcmp->block_size / runtime->channels);
  126. end -= snd_pcm_format_width(runtime->format) == 16 ? 2 : 1;
  127. // snd_printk("init: curr=0x%x, begin=0x%x, end=0x%x, ctrl=0x%x, ramp=0x%x, rate=0x%x\n", curr, begin, end, voice_ctrl, ramp_ctrl, rate);
  128. pan = runtime->channels == 2 ? (!voice ? 1 : 14) : 8;
  129. vol = !voice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
  130. spin_lock_irqsave(&gus->reg_lock, flags);
  131. snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
  132. snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, pan);
  133. snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, rate);
  134. snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, begin << 4, voice_ctrl & 4);
  135. snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
  136. snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, curr << 4, voice_ctrl & 4);
  137. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, SNDRV_GF1_MIN_VOLUME << 4);
  138. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0x2f);
  139. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET);
  140. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, vol >> 8);
  141. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  142. if (!gus->gf1.enh_mode) {
  143. snd_gf1_delay(gus);
  144. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  145. }
  146. spin_unlock_irqrestore(&gus->reg_lock, flags);
  147. }
  148. spin_lock_irqsave(&gus->reg_lock, flags);
  149. for (voice = 0; voice < pcmp->voices; voice++) {
  150. snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
  151. if (gus->gf1.enh_mode)
  152. snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, 0x00); /* deactivate voice */
  153. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  154. voice_ctrl &= ~0x20;
  155. }
  156. voice_ctrl |= 0x20;
  157. if (!gus->gf1.enh_mode) {
  158. snd_gf1_delay(gus);
  159. for (voice = 0; voice < pcmp->voices; voice++) {
  160. snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
  161. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  162. voice_ctrl &= ~0x20; /* disable IRQ for next voice */
  163. }
  164. }
  165. spin_unlock_irqrestore(&gus->reg_lock, flags);
  166. }
  167. static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card * gus,
  168. struct snd_gus_voice *pvoice)
  169. {
  170. struct gus_pcm_private * pcmp;
  171. struct snd_pcm_runtime *runtime;
  172. unsigned char voice_ctrl, ramp_ctrl;
  173. unsigned int idx;
  174. unsigned int end, step;
  175. if (!pvoice->private_data) {
  176. snd_printd("snd_gf1_pcm: unknown wave irq?\n");
  177. snd_gf1_smart_stop_voice(gus, pvoice->number);
  178. return;
  179. }
  180. pcmp = pvoice->private_data;
  181. if (pcmp == NULL) {
  182. snd_printd("snd_gf1_pcm: unknown wave irq?\n");
  183. snd_gf1_smart_stop_voice(gus, pvoice->number);
  184. return;
  185. }
  186. gus = pcmp->gus;
  187. runtime = pcmp->substream->runtime;
  188. spin_lock(&gus->reg_lock);
  189. snd_gf1_select_voice(gus, pvoice->number);
  190. voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & ~0x8b;
  191. ramp_ctrl = (snd_gf1_read8(gus, SNDRV_GF1_VB_VOLUME_CONTROL) & ~0xa4) | 0x03;
  192. #if 0
  193. snd_gf1_select_voice(gus, pvoice->number);
  194. printk("position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
  195. snd_gf1_select_voice(gus, pcmp->pvoices[1]->number);
  196. printk("position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
  197. snd_gf1_select_voice(gus, pvoice->number);
  198. #endif
  199. pcmp->bpos++;
  200. pcmp->bpos %= pcmp->blocks;
  201. if (pcmp->bpos + 1 >= pcmp->blocks) { /* last block? */
  202. voice_ctrl |= 0x08; /* enable loop */
  203. } else {
  204. ramp_ctrl |= 0x04; /* enable rollover */
  205. }
  206. end = pcmp->memory + (((pcmp->bpos + 1) * pcmp->block_size) / runtime->channels);
  207. end -= voice_ctrl & 4 ? 2 : 1;
  208. step = pcmp->dma_size / runtime->channels;
  209. voice_ctrl |= 0x20;
  210. if (!pcmp->final_volume) {
  211. ramp_ctrl |= 0x20;
  212. ramp_ctrl &= ~0x03;
  213. }
  214. for (idx = 0; idx < pcmp->voices; idx++, end += step) {
  215. snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
  216. snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
  217. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  218. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  219. voice_ctrl &= ~0x20;
  220. }
  221. if (!gus->gf1.enh_mode) {
  222. snd_gf1_delay(gus);
  223. voice_ctrl |= 0x20;
  224. for (idx = 0; idx < pcmp->voices; idx++) {
  225. snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
  226. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  227. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  228. voice_ctrl &= ~0x20;
  229. }
  230. }
  231. spin_unlock(&gus->reg_lock);
  232. snd_pcm_period_elapsed(pcmp->substream);
  233. #if 0
  234. if ((runtime->flags & SNDRV_PCM_FLG_MMAP) &&
  235. *runtime->state == SNDRV_PCM_STATE_RUNNING) {
  236. end = pcmp->bpos * pcmp->block_size;
  237. if (runtime->channels > 1) {
  238. snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + (end / 2), pcmp->block_size / 2);
  239. snd_gf1_pcm_block_change(pcmp->substream, end + (pcmp->block_size / 2), pcmp->memory + (pcmp->dma_size / 2) + (end / 2), pcmp->block_size / 2);
  240. } else {
  241. snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + end, pcmp->block_size);
  242. }
  243. }
  244. #endif
  245. }
  246. static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card * gus,
  247. struct snd_gus_voice * pvoice)
  248. {
  249. unsigned short vol;
  250. int cvoice;
  251. struct gus_pcm_private *pcmp = pvoice->private_data;
  252. /* stop ramp, but leave rollover bit untouched */
  253. spin_lock(&gus->reg_lock);
  254. snd_gf1_select_voice(gus, pvoice->number);
  255. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
  256. spin_unlock(&gus->reg_lock);
  257. if (pcmp == NULL)
  258. return;
  259. /* are we active? */
  260. if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
  261. return;
  262. /* load real volume - better precision */
  263. cvoice = pcmp->pvoices[0] == pvoice ? 0 : 1;
  264. if (pcmp->substream == NULL)
  265. return;
  266. vol = !cvoice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
  267. spin_lock(&gus->reg_lock);
  268. snd_gf1_select_voice(gus, pvoice->number);
  269. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
  270. pcmp->final_volume = 1;
  271. spin_unlock(&gus->reg_lock);
  272. }
  273. static void snd_gf1_pcm_volume_change(struct snd_gus_card * gus)
  274. {
  275. }
  276. static int snd_gf1_pcm_poke_block(struct snd_gus_card *gus, unsigned char *buf,
  277. unsigned int pos, unsigned int count,
  278. int w16, int invert)
  279. {
  280. unsigned int len;
  281. unsigned long flags;
  282. // printk("poke block; buf = 0x%x, pos = %i, count = %i, port = 0x%x\n", (int)buf, pos, count, gus->gf1.port);
  283. while (count > 0) {
  284. len = count;
  285. if (len > 512) /* limit, to allow IRQ */
  286. len = 512;
  287. count -= len;
  288. if (gus->interwave) {
  289. spin_lock_irqsave(&gus->reg_lock, flags);
  290. snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01 | (invert ? 0x08 : 0x00));
  291. snd_gf1_dram_addr(gus, pos);
  292. if (w16) {
  293. outb(SNDRV_GF1_GW_DRAM_IO16, GUSP(gus, GF1REGSEL));
  294. outsw(GUSP(gus, GF1DATALOW), buf, len >> 1);
  295. } else {
  296. outsb(GUSP(gus, DRAM), buf, len);
  297. }
  298. spin_unlock_irqrestore(&gus->reg_lock, flags);
  299. buf += 512;
  300. pos += 512;
  301. } else {
  302. invert = invert ? 0x80 : 0x00;
  303. if (w16) {
  304. len >>= 1;
  305. while (len--) {
  306. snd_gf1_poke(gus, pos++, *buf++);
  307. snd_gf1_poke(gus, pos++, *buf++ ^ invert);
  308. }
  309. } else {
  310. while (len--)
  311. snd_gf1_poke(gus, pos++, *buf++ ^ invert);
  312. }
  313. }
  314. if (count > 0 && !in_interrupt()) {
  315. schedule_timeout_interruptible(1);
  316. if (signal_pending(current))
  317. return -EAGAIN;
  318. }
  319. }
  320. return 0;
  321. }
  322. static int snd_gf1_pcm_playback_copy(struct snd_pcm_substream *substream,
  323. int voice,
  324. snd_pcm_uframes_t pos,
  325. void __user *src,
  326. snd_pcm_uframes_t count)
  327. {
  328. struct snd_pcm_runtime *runtime = substream->runtime;
  329. struct gus_pcm_private *pcmp = runtime->private_data;
  330. unsigned int bpos, len;
  331. bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
  332. len = samples_to_bytes(runtime, count);
  333. snd_assert(bpos <= pcmp->dma_size, return -EIO);
  334. snd_assert(bpos + len <= pcmp->dma_size, return -EIO);
  335. if (copy_from_user(runtime->dma_area + bpos, src, len))
  336. return -EFAULT;
  337. if (snd_gf1_pcm_use_dma && len > 32) {
  338. return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
  339. } else {
  340. struct snd_gus_card *gus = pcmp->gus;
  341. int err, w16, invert;
  342. w16 = (snd_pcm_format_width(runtime->format) == 16);
  343. invert = snd_pcm_format_unsigned(runtime->format);
  344. if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
  345. return err;
  346. }
  347. return 0;
  348. }
  349. static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream *substream,
  350. int voice,
  351. snd_pcm_uframes_t pos,
  352. snd_pcm_uframes_t count)
  353. {
  354. struct snd_pcm_runtime *runtime = substream->runtime;
  355. struct gus_pcm_private *pcmp = runtime->private_data;
  356. unsigned int bpos, len;
  357. bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
  358. len = samples_to_bytes(runtime, count);
  359. snd_assert(bpos <= pcmp->dma_size, return -EIO);
  360. snd_assert(bpos + len <= pcmp->dma_size, return -EIO);
  361. snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos, count);
  362. if (snd_gf1_pcm_use_dma && len > 32) {
  363. return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
  364. } else {
  365. struct snd_gus_card *gus = pcmp->gus;
  366. int err, w16, invert;
  367. w16 = (snd_pcm_format_width(runtime->format) == 16);
  368. invert = snd_pcm_format_unsigned(runtime->format);
  369. if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
  370. return err;
  371. }
  372. return 0;
  373. }
  374. static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream *substream,
  375. struct snd_pcm_hw_params *hw_params)
  376. {
  377. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  378. struct snd_pcm_runtime *runtime = substream->runtime;
  379. struct gus_pcm_private *pcmp = runtime->private_data;
  380. int err;
  381. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  382. return err;
  383. if (err > 0) { /* change */
  384. struct snd_gf1_mem_block *block;
  385. if (pcmp->memory > 0) {
  386. snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory);
  387. pcmp->memory = 0;
  388. }
  389. if ((block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc,
  390. SNDRV_GF1_MEM_OWNER_DRIVER,
  391. "GF1 PCM",
  392. runtime->dma_bytes, 1, 32,
  393. NULL)) == NULL)
  394. return -ENOMEM;
  395. pcmp->memory = block->ptr;
  396. }
  397. pcmp->voices = params_channels(hw_params);
  398. if (pcmp->pvoices[0] == NULL) {
  399. if ((pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
  400. return -ENOMEM;
  401. pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave;
  402. pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume;
  403. pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change;
  404. pcmp->pvoices[0]->private_data = pcmp;
  405. }
  406. if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) {
  407. if ((pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
  408. return -ENOMEM;
  409. pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave;
  410. pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume;
  411. pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change;
  412. pcmp->pvoices[1]->private_data = pcmp;
  413. } else if (pcmp->voices == 1) {
  414. if (pcmp->pvoices[1]) {
  415. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
  416. pcmp->pvoices[1] = NULL;
  417. }
  418. }
  419. return 0;
  420. }
  421. static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream *substream)
  422. {
  423. struct snd_pcm_runtime *runtime = substream->runtime;
  424. struct gus_pcm_private *pcmp = runtime->private_data;
  425. snd_pcm_lib_free_pages(substream);
  426. if (pcmp->pvoices[0]) {
  427. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]);
  428. pcmp->pvoices[0] = NULL;
  429. }
  430. if (pcmp->pvoices[1]) {
  431. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
  432. pcmp->pvoices[1] = NULL;
  433. }
  434. if (pcmp->memory > 0) {
  435. snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory);
  436. pcmp->memory = 0;
  437. }
  438. return 0;
  439. }
  440. static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream *substream)
  441. {
  442. struct snd_pcm_runtime *runtime = substream->runtime;
  443. struct gus_pcm_private *pcmp = runtime->private_data;
  444. pcmp->bpos = 0;
  445. pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream);
  446. pcmp->block_size = snd_pcm_lib_period_bytes(substream);
  447. pcmp->blocks = pcmp->dma_size / pcmp->block_size;
  448. return 0;
  449. }
  450. static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream *substream,
  451. int cmd)
  452. {
  453. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  454. struct snd_pcm_runtime *runtime = substream->runtime;
  455. struct gus_pcm_private *pcmp = runtime->private_data;
  456. int voice;
  457. if (cmd == SNDRV_PCM_TRIGGER_START) {
  458. snd_gf1_pcm_trigger_up(substream);
  459. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  460. spin_lock(&pcmp->lock);
  461. pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE;
  462. spin_unlock(&pcmp->lock);
  463. voice = pcmp->pvoices[0]->number;
  464. snd_gf1_stop_voices(gus, voice, voice);
  465. if (pcmp->pvoices[1]) {
  466. voice = pcmp->pvoices[1]->number;
  467. snd_gf1_stop_voices(gus, voice, voice);
  468. }
  469. } else {
  470. return -EINVAL;
  471. }
  472. return 0;
  473. }
  474. static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(struct snd_pcm_substream *substream)
  475. {
  476. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  477. struct snd_pcm_runtime *runtime = substream->runtime;
  478. struct gus_pcm_private *pcmp = runtime->private_data;
  479. unsigned int pos;
  480. unsigned char voice_ctrl;
  481. pos = 0;
  482. spin_lock(&gus->reg_lock);
  483. if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
  484. snd_gf1_select_voice(gus, pcmp->pvoices[0]->number);
  485. voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
  486. pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory;
  487. if (substream->runtime->channels > 1)
  488. pos <<= 1;
  489. pos = bytes_to_frames(runtime, pos);
  490. }
  491. spin_unlock(&gus->reg_lock);
  492. return pos;
  493. }
  494. static struct snd_ratnum clock = {
  495. .num = 9878400/16,
  496. .den_min = 2,
  497. .den_max = 257,
  498. .den_step = 1,
  499. };
  500. static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
  501. .nrats = 1,
  502. .rats = &clock,
  503. };
  504. static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream *substream,
  505. struct snd_pcm_hw_params *hw_params)
  506. {
  507. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  508. gus->c_dma_size = params_buffer_bytes(hw_params);
  509. gus->c_period_size = params_period_bytes(hw_params);
  510. gus->c_pos = 0;
  511. gus->gf1.pcm_rcntrl_reg = 0x21; /* IRQ at end, enable & start */
  512. if (params_channels(hw_params) > 1)
  513. gus->gf1.pcm_rcntrl_reg |= 2;
  514. if (gus->gf1.dma2 > 3)
  515. gus->gf1.pcm_rcntrl_reg |= 4;
  516. if (snd_pcm_format_unsigned(params_format(hw_params)))
  517. gus->gf1.pcm_rcntrl_reg |= 0x80;
  518. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  519. }
  520. static int snd_gf1_pcm_capture_hw_free(struct snd_pcm_substream *substream)
  521. {
  522. return snd_pcm_lib_free_pages(substream);
  523. }
  524. static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream *substream)
  525. {
  526. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  527. struct snd_pcm_runtime *runtime = substream->runtime;
  528. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2);
  529. snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
  530. snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
  531. snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ);
  532. return 0;
  533. }
  534. static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream *substream,
  535. int cmd)
  536. {
  537. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  538. int val;
  539. if (cmd == SNDRV_PCM_TRIGGER_START) {
  540. val = gus->gf1.pcm_rcntrl_reg;
  541. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  542. val = 0;
  543. } else {
  544. return -EINVAL;
  545. }
  546. spin_lock(&gus->reg_lock);
  547. snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val);
  548. snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);
  549. spin_unlock(&gus->reg_lock);
  550. return 0;
  551. }
  552. static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(struct snd_pcm_substream *substream)
  553. {
  554. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  555. int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size);
  556. pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size);
  557. return pos;
  558. }
  559. static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card * gus)
  560. {
  561. snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
  562. snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
  563. if (gus->pcm_cap_substream != NULL) {
  564. snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream);
  565. snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START);
  566. gus->c_pos += gus->c_period_size;
  567. snd_pcm_period_elapsed(gus->pcm_cap_substream);
  568. }
  569. }
  570. static struct snd_pcm_hardware snd_gf1_pcm_playback =
  571. {
  572. .info = SNDRV_PCM_INFO_NONINTERLEAVED,
  573. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  574. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  575. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  576. .rate_min = 5510,
  577. .rate_max = 48000,
  578. .channels_min = 1,
  579. .channels_max = 2,
  580. .buffer_bytes_max = (128*1024),
  581. .period_bytes_min = 64,
  582. .period_bytes_max = (128*1024),
  583. .periods_min = 1,
  584. .periods_max = 1024,
  585. .fifo_size = 0,
  586. };
  587. static struct snd_pcm_hardware snd_gf1_pcm_capture =
  588. {
  589. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  590. SNDRV_PCM_INFO_MMAP_VALID),
  591. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8,
  592. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
  593. .rate_min = 5510,
  594. .rate_max = 44100,
  595. .channels_min = 1,
  596. .channels_max = 2,
  597. .buffer_bytes_max = (128*1024),
  598. .period_bytes_min = 64,
  599. .period_bytes_max = (128*1024),
  600. .periods_min = 1,
  601. .periods_max = 1024,
  602. .fifo_size = 0,
  603. };
  604. static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime *runtime)
  605. {
  606. kfree(runtime->private_data);
  607. }
  608. static int snd_gf1_pcm_playback_open(struct snd_pcm_substream *substream)
  609. {
  610. struct gus_pcm_private *pcmp;
  611. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  612. struct snd_pcm_runtime *runtime = substream->runtime;
  613. int err;
  614. pcmp = kzalloc(sizeof(*pcmp), GFP_KERNEL);
  615. if (pcmp == NULL)
  616. return -ENOMEM;
  617. pcmp->gus = gus;
  618. spin_lock_init(&pcmp->lock);
  619. init_waitqueue_head(&pcmp->sleep);
  620. atomic_set(&pcmp->dma_count, 0);
  621. runtime->private_data = pcmp;
  622. runtime->private_free = snd_gf1_pcm_playback_free;
  623. #if 0
  624. printk("playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n", (long) pcm->playback.buffer, (long) gus->gf1.pcm_buffer);
  625. #endif
  626. if ((err = snd_gf1_dma_init(gus)) < 0)
  627. return err;
  628. pcmp->flags = SNDRV_GF1_PCM_PFLG_NONE;
  629. pcmp->substream = substream;
  630. runtime->hw = snd_gf1_pcm_playback;
  631. snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.buffer_bytes_max);
  632. snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.period_bytes_max);
  633. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
  634. return 0;
  635. }
  636. static int snd_gf1_pcm_playback_close(struct snd_pcm_substream *substream)
  637. {
  638. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  639. struct snd_pcm_runtime *runtime = substream->runtime;
  640. struct gus_pcm_private *pcmp = runtime->private_data;
  641. if (!wait_event_timeout(pcmp->sleep, (atomic_read(&pcmp->dma_count) <= 0), 2*HZ))
  642. snd_printk(KERN_ERR "gf1 pcm - serious DMA problem\n");
  643. snd_gf1_dma_done(gus);
  644. return 0;
  645. }
  646. static int snd_gf1_pcm_capture_open(struct snd_pcm_substream *substream)
  647. {
  648. struct snd_pcm_runtime *runtime = substream->runtime;
  649. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  650. gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read;
  651. gus->pcm_cap_substream = substream;
  652. substream->runtime->hw = snd_gf1_pcm_capture;
  653. snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max);
  654. snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max);
  655. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  656. &hw_constraints_clocks);
  657. return 0;
  658. }
  659. static int snd_gf1_pcm_capture_close(struct snd_pcm_substream *substream)
  660. {
  661. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  662. gus->pcm_cap_substream = NULL;
  663. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ);
  664. return 0;
  665. }
  666. static int snd_gf1_pcm_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  667. {
  668. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  669. uinfo->count = 2;
  670. uinfo->value.integer.min = 0;
  671. uinfo->value.integer.max = 127;
  672. return 0;
  673. }
  674. static int snd_gf1_pcm_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  675. {
  676. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  677. unsigned long flags;
  678. spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
  679. ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1;
  680. ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1;
  681. spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
  682. return 0;
  683. }
  684. static int snd_gf1_pcm_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  685. {
  686. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  687. unsigned long flags;
  688. int change;
  689. unsigned int idx;
  690. unsigned short val1, val2, vol;
  691. struct gus_pcm_private *pcmp;
  692. struct snd_gus_voice *pvoice;
  693. val1 = ucontrol->value.integer.value[0] & 127;
  694. val2 = ucontrol->value.integer.value[1] & 127;
  695. spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
  696. change = val1 != gus->gf1.pcm_volume_level_left1 ||
  697. val2 != gus->gf1.pcm_volume_level_right1;
  698. gus->gf1.pcm_volume_level_left1 = val1;
  699. gus->gf1.pcm_volume_level_right1 = val2;
  700. gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4;
  701. gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4;
  702. spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
  703. /* are we active? */
  704. spin_lock_irqsave(&gus->voice_alloc, flags);
  705. for (idx = 0; idx < 32; idx++) {
  706. pvoice = &gus->gf1.voices[idx];
  707. if (!pvoice->pcm)
  708. continue;
  709. pcmp = pvoice->private_data;
  710. if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
  711. continue;
  712. /* load real volume - better precision */
  713. spin_lock_irqsave(&gus->reg_lock, flags);
  714. snd_gf1_select_voice(gus, pvoice->number);
  715. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
  716. vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
  717. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
  718. pcmp->final_volume = 1;
  719. spin_unlock_irqrestore(&gus->reg_lock, flags);
  720. }
  721. spin_unlock_irqrestore(&gus->voice_alloc, flags);
  722. return change;
  723. }
  724. static struct snd_kcontrol_new snd_gf1_pcm_volume_control =
  725. {
  726. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  727. .name = "PCM Playback Volume",
  728. .info = snd_gf1_pcm_volume_info,
  729. .get = snd_gf1_pcm_volume_get,
  730. .put = snd_gf1_pcm_volume_put
  731. };
  732. static struct snd_kcontrol_new snd_gf1_pcm_volume_control1 =
  733. {
  734. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  735. .name = "GPCM Playback Volume",
  736. .info = snd_gf1_pcm_volume_info,
  737. .get = snd_gf1_pcm_volume_get,
  738. .put = snd_gf1_pcm_volume_put
  739. };
  740. static struct snd_pcm_ops snd_gf1_pcm_playback_ops = {
  741. .open = snd_gf1_pcm_playback_open,
  742. .close = snd_gf1_pcm_playback_close,
  743. .ioctl = snd_pcm_lib_ioctl,
  744. .hw_params = snd_gf1_pcm_playback_hw_params,
  745. .hw_free = snd_gf1_pcm_playback_hw_free,
  746. .prepare = snd_gf1_pcm_playback_prepare,
  747. .trigger = snd_gf1_pcm_playback_trigger,
  748. .pointer = snd_gf1_pcm_playback_pointer,
  749. .copy = snd_gf1_pcm_playback_copy,
  750. .silence = snd_gf1_pcm_playback_silence,
  751. };
  752. static struct snd_pcm_ops snd_gf1_pcm_capture_ops = {
  753. .open = snd_gf1_pcm_capture_open,
  754. .close = snd_gf1_pcm_capture_close,
  755. .ioctl = snd_pcm_lib_ioctl,
  756. .hw_params = snd_gf1_pcm_capture_hw_params,
  757. .hw_free = snd_gf1_pcm_capture_hw_free,
  758. .prepare = snd_gf1_pcm_capture_prepare,
  759. .trigger = snd_gf1_pcm_capture_trigger,
  760. .pointer = snd_gf1_pcm_capture_pointer,
  761. };
  762. int snd_gf1_pcm_new(struct snd_gus_card * gus, int pcm_dev, int control_index, struct snd_pcm ** rpcm)
  763. {
  764. struct snd_card *card;
  765. struct snd_kcontrol *kctl;
  766. struct snd_pcm *pcm;
  767. struct snd_pcm_substream *substream;
  768. int capture, err;
  769. if (rpcm)
  770. *rpcm = NULL;
  771. card = gus->card;
  772. capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0;
  773. err = snd_pcm_new(card,
  774. gus->interwave ? "AMD InterWave" : "GF1",
  775. pcm_dev,
  776. gus->gf1.pcm_channels / 2,
  777. capture,
  778. &pcm);
  779. if (err < 0)
  780. return err;
  781. pcm->private_data = gus;
  782. /* playback setup */
  783. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops);
  784. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  785. snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
  786. snd_dma_isa_data(),
  787. 64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024);
  788. pcm->info_flags = 0;
  789. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  790. if (capture) {
  791. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops);
  792. if (gus->gf1.dma2 == gus->gf1.dma1)
  793. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  794. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  795. SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
  796. 64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024);
  797. }
  798. strcpy(pcm->name, pcm->id);
  799. if (gus->interwave) {
  800. sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
  801. }
  802. strcat(pcm->name, " (synth)");
  803. gus->pcm = pcm;
  804. if (gus->codec_flag)
  805. kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
  806. else
  807. kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
  808. if ((err = snd_ctl_add(card, kctl)) < 0)
  809. return err;
  810. kctl->id.index = control_index;
  811. if (rpcm)
  812. *rpcm = pcm;
  813. return 0;
  814. }