gus_pcm.c 29 KB

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