gus_pcm.c 29 KB

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