gus_pcm.c 29 KB

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