gus_pcm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. /*
  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 snd_gf1_pcm_playback_copy(struct snd_pcm_substream *substream,
  335. int voice,
  336. snd_pcm_uframes_t pos,
  337. void __user *src,
  338. snd_pcm_uframes_t count)
  339. {
  340. struct snd_pcm_runtime *runtime = substream->runtime;
  341. struct gus_pcm_private *pcmp = runtime->private_data;
  342. unsigned int bpos, len;
  343. bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
  344. len = samples_to_bytes(runtime, count);
  345. if (snd_BUG_ON(bpos > pcmp->dma_size))
  346. return -EIO;
  347. if (snd_BUG_ON(bpos + len > pcmp->dma_size))
  348. return -EIO;
  349. if (copy_from_user(runtime->dma_area + bpos, src, len))
  350. return -EFAULT;
  351. if (snd_gf1_pcm_use_dma && len > 32) {
  352. return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
  353. } else {
  354. struct snd_gus_card *gus = pcmp->gus;
  355. int err, w16, invert;
  356. w16 = (snd_pcm_format_width(runtime->format) == 16);
  357. invert = snd_pcm_format_unsigned(runtime->format);
  358. if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
  359. return err;
  360. }
  361. return 0;
  362. }
  363. static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream *substream,
  364. int voice,
  365. snd_pcm_uframes_t pos,
  366. snd_pcm_uframes_t count)
  367. {
  368. struct snd_pcm_runtime *runtime = substream->runtime;
  369. struct gus_pcm_private *pcmp = runtime->private_data;
  370. unsigned int bpos, len;
  371. bpos = samples_to_bytes(runtime, pos) + (voice * (pcmp->dma_size / 2));
  372. len = samples_to_bytes(runtime, count);
  373. if (snd_BUG_ON(bpos > pcmp->dma_size))
  374. return -EIO;
  375. if (snd_BUG_ON(bpos + len > pcmp->dma_size))
  376. return -EIO;
  377. snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos, count);
  378. if (snd_gf1_pcm_use_dma && len > 32) {
  379. return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len);
  380. } else {
  381. struct snd_gus_card *gus = pcmp->gus;
  382. int err, w16, invert;
  383. w16 = (snd_pcm_format_width(runtime->format) == 16);
  384. invert = snd_pcm_format_unsigned(runtime->format);
  385. if ((err = snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert)) < 0)
  386. return err;
  387. }
  388. return 0;
  389. }
  390. static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream *substream,
  391. struct snd_pcm_hw_params *hw_params)
  392. {
  393. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  394. struct snd_pcm_runtime *runtime = substream->runtime;
  395. struct gus_pcm_private *pcmp = runtime->private_data;
  396. int err;
  397. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  398. return err;
  399. if (err > 0) { /* change */
  400. struct snd_gf1_mem_block *block;
  401. if (pcmp->memory > 0) {
  402. snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory);
  403. pcmp->memory = 0;
  404. }
  405. if ((block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc,
  406. SNDRV_GF1_MEM_OWNER_DRIVER,
  407. "GF1 PCM",
  408. runtime->dma_bytes, 1, 32,
  409. NULL)) == NULL)
  410. return -ENOMEM;
  411. pcmp->memory = block->ptr;
  412. }
  413. pcmp->voices = params_channels(hw_params);
  414. if (pcmp->pvoices[0] == NULL) {
  415. if ((pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
  416. return -ENOMEM;
  417. pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave;
  418. pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume;
  419. pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change;
  420. pcmp->pvoices[0]->private_data = pcmp;
  421. }
  422. if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) {
  423. if ((pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0)) == NULL)
  424. return -ENOMEM;
  425. pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave;
  426. pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume;
  427. pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change;
  428. pcmp->pvoices[1]->private_data = pcmp;
  429. } else if (pcmp->voices == 1) {
  430. if (pcmp->pvoices[1]) {
  431. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
  432. pcmp->pvoices[1] = NULL;
  433. }
  434. }
  435. return 0;
  436. }
  437. static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream *substream)
  438. {
  439. struct snd_pcm_runtime *runtime = substream->runtime;
  440. struct gus_pcm_private *pcmp = runtime->private_data;
  441. snd_pcm_lib_free_pages(substream);
  442. if (pcmp->pvoices[0]) {
  443. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]);
  444. pcmp->pvoices[0] = NULL;
  445. }
  446. if (pcmp->pvoices[1]) {
  447. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
  448. pcmp->pvoices[1] = NULL;
  449. }
  450. if (pcmp->memory > 0) {
  451. snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory);
  452. pcmp->memory = 0;
  453. }
  454. return 0;
  455. }
  456. static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream *substream)
  457. {
  458. struct snd_pcm_runtime *runtime = substream->runtime;
  459. struct gus_pcm_private *pcmp = runtime->private_data;
  460. pcmp->bpos = 0;
  461. pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream);
  462. pcmp->block_size = snd_pcm_lib_period_bytes(substream);
  463. pcmp->blocks = pcmp->dma_size / pcmp->block_size;
  464. return 0;
  465. }
  466. static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream *substream,
  467. int cmd)
  468. {
  469. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  470. struct snd_pcm_runtime *runtime = substream->runtime;
  471. struct gus_pcm_private *pcmp = runtime->private_data;
  472. int voice;
  473. if (cmd == SNDRV_PCM_TRIGGER_START) {
  474. snd_gf1_pcm_trigger_up(substream);
  475. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  476. spin_lock(&pcmp->lock);
  477. pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE;
  478. spin_unlock(&pcmp->lock);
  479. voice = pcmp->pvoices[0]->number;
  480. snd_gf1_stop_voices(gus, voice, voice);
  481. if (pcmp->pvoices[1]) {
  482. voice = pcmp->pvoices[1]->number;
  483. snd_gf1_stop_voices(gus, voice, voice);
  484. }
  485. } else {
  486. return -EINVAL;
  487. }
  488. return 0;
  489. }
  490. static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(struct snd_pcm_substream *substream)
  491. {
  492. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  493. struct snd_pcm_runtime *runtime = substream->runtime;
  494. struct gus_pcm_private *pcmp = runtime->private_data;
  495. unsigned int pos;
  496. unsigned char voice_ctrl;
  497. pos = 0;
  498. spin_lock(&gus->reg_lock);
  499. if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
  500. snd_gf1_select_voice(gus, pcmp->pvoices[0]->number);
  501. voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
  502. pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory;
  503. if (substream->runtime->channels > 1)
  504. pos <<= 1;
  505. pos = bytes_to_frames(runtime, pos);
  506. }
  507. spin_unlock(&gus->reg_lock);
  508. return pos;
  509. }
  510. static struct snd_ratnum clock = {
  511. .num = 9878400/16,
  512. .den_min = 2,
  513. .den_max = 257,
  514. .den_step = 1,
  515. };
  516. static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
  517. .nrats = 1,
  518. .rats = &clock,
  519. };
  520. static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream *substream,
  521. struct snd_pcm_hw_params *hw_params)
  522. {
  523. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  524. gus->c_dma_size = params_buffer_bytes(hw_params);
  525. gus->c_period_size = params_period_bytes(hw_params);
  526. gus->c_pos = 0;
  527. gus->gf1.pcm_rcntrl_reg = 0x21; /* IRQ at end, enable & start */
  528. if (params_channels(hw_params) > 1)
  529. gus->gf1.pcm_rcntrl_reg |= 2;
  530. if (gus->gf1.dma2 > 3)
  531. gus->gf1.pcm_rcntrl_reg |= 4;
  532. if (snd_pcm_format_unsigned(params_format(hw_params)))
  533. gus->gf1.pcm_rcntrl_reg |= 0x80;
  534. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  535. }
  536. static int snd_gf1_pcm_capture_hw_free(struct snd_pcm_substream *substream)
  537. {
  538. return snd_pcm_lib_free_pages(substream);
  539. }
  540. static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream *substream)
  541. {
  542. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  543. struct snd_pcm_runtime *runtime = substream->runtime;
  544. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2);
  545. snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
  546. snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
  547. snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ);
  548. return 0;
  549. }
  550. static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream *substream,
  551. int cmd)
  552. {
  553. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  554. int val;
  555. if (cmd == SNDRV_PCM_TRIGGER_START) {
  556. val = gus->gf1.pcm_rcntrl_reg;
  557. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  558. val = 0;
  559. } else {
  560. return -EINVAL;
  561. }
  562. spin_lock(&gus->reg_lock);
  563. snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val);
  564. snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);
  565. spin_unlock(&gus->reg_lock);
  566. return 0;
  567. }
  568. static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(struct snd_pcm_substream *substream)
  569. {
  570. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  571. int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size);
  572. pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size);
  573. return pos;
  574. }
  575. static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card * gus)
  576. {
  577. snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
  578. snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
  579. if (gus->pcm_cap_substream != NULL) {
  580. snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream);
  581. snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START);
  582. gus->c_pos += gus->c_period_size;
  583. snd_pcm_period_elapsed(gus->pcm_cap_substream);
  584. }
  585. }
  586. static struct snd_pcm_hardware snd_gf1_pcm_playback =
  587. {
  588. .info = SNDRV_PCM_INFO_NONINTERLEAVED,
  589. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  590. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  591. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  592. .rate_min = 5510,
  593. .rate_max = 48000,
  594. .channels_min = 1,
  595. .channels_max = 2,
  596. .buffer_bytes_max = (128*1024),
  597. .period_bytes_min = 64,
  598. .period_bytes_max = (128*1024),
  599. .periods_min = 1,
  600. .periods_max = 1024,
  601. .fifo_size = 0,
  602. };
  603. static struct snd_pcm_hardware snd_gf1_pcm_capture =
  604. {
  605. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  606. SNDRV_PCM_INFO_MMAP_VALID),
  607. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8,
  608. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
  609. .rate_min = 5510,
  610. .rate_max = 44100,
  611. .channels_min = 1,
  612. .channels_max = 2,
  613. .buffer_bytes_max = (128*1024),
  614. .period_bytes_min = 64,
  615. .period_bytes_max = (128*1024),
  616. .periods_min = 1,
  617. .periods_max = 1024,
  618. .fifo_size = 0,
  619. };
  620. static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime *runtime)
  621. {
  622. kfree(runtime->private_data);
  623. }
  624. static int snd_gf1_pcm_playback_open(struct snd_pcm_substream *substream)
  625. {
  626. struct gus_pcm_private *pcmp;
  627. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  628. struct snd_pcm_runtime *runtime = substream->runtime;
  629. int err;
  630. pcmp = kzalloc(sizeof(*pcmp), GFP_KERNEL);
  631. if (pcmp == NULL)
  632. return -ENOMEM;
  633. pcmp->gus = gus;
  634. spin_lock_init(&pcmp->lock);
  635. init_waitqueue_head(&pcmp->sleep);
  636. atomic_set(&pcmp->dma_count, 0);
  637. runtime->private_data = pcmp;
  638. runtime->private_free = snd_gf1_pcm_playback_free;
  639. #if 0
  640. printk(KERN_DEBUG "playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n",
  641. (long) pcm->playback.buffer, (long) gus->gf1.pcm_buffer);
  642. #endif
  643. if ((err = snd_gf1_dma_init(gus)) < 0)
  644. return err;
  645. pcmp->flags = SNDRV_GF1_PCM_PFLG_NONE;
  646. pcmp->substream = substream;
  647. runtime->hw = snd_gf1_pcm_playback;
  648. snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.buffer_bytes_max);
  649. snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.period_bytes_max);
  650. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
  651. return 0;
  652. }
  653. static int snd_gf1_pcm_playback_close(struct snd_pcm_substream *substream)
  654. {
  655. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  656. struct snd_pcm_runtime *runtime = substream->runtime;
  657. struct gus_pcm_private *pcmp = runtime->private_data;
  658. if (!wait_event_timeout(pcmp->sleep, (atomic_read(&pcmp->dma_count) <= 0), 2*HZ))
  659. snd_printk(KERN_ERR "gf1 pcm - serious DMA problem\n");
  660. snd_gf1_dma_done(gus);
  661. return 0;
  662. }
  663. static int snd_gf1_pcm_capture_open(struct snd_pcm_substream *substream)
  664. {
  665. struct snd_pcm_runtime *runtime = substream->runtime;
  666. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  667. gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read;
  668. gus->pcm_cap_substream = substream;
  669. substream->runtime->hw = snd_gf1_pcm_capture;
  670. snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max);
  671. snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max);
  672. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  673. &hw_constraints_clocks);
  674. return 0;
  675. }
  676. static int snd_gf1_pcm_capture_close(struct snd_pcm_substream *substream)
  677. {
  678. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  679. gus->pcm_cap_substream = NULL;
  680. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ);
  681. return 0;
  682. }
  683. static int snd_gf1_pcm_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  684. {
  685. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  686. uinfo->count = 2;
  687. uinfo->value.integer.min = 0;
  688. uinfo->value.integer.max = 127;
  689. return 0;
  690. }
  691. static int snd_gf1_pcm_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  692. {
  693. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  694. unsigned long flags;
  695. spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
  696. ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1;
  697. ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1;
  698. spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
  699. return 0;
  700. }
  701. static int snd_gf1_pcm_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  702. {
  703. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  704. unsigned long flags;
  705. int change;
  706. unsigned int idx;
  707. unsigned short val1, val2, vol;
  708. struct gus_pcm_private *pcmp;
  709. struct snd_gus_voice *pvoice;
  710. val1 = ucontrol->value.integer.value[0] & 127;
  711. val2 = ucontrol->value.integer.value[1] & 127;
  712. spin_lock_irqsave(&gus->pcm_volume_level_lock, flags);
  713. change = val1 != gus->gf1.pcm_volume_level_left1 ||
  714. val2 != gus->gf1.pcm_volume_level_right1;
  715. gus->gf1.pcm_volume_level_left1 = val1;
  716. gus->gf1.pcm_volume_level_right1 = val2;
  717. gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4;
  718. gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4;
  719. spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags);
  720. /* are we active? */
  721. spin_lock_irqsave(&gus->voice_alloc, flags);
  722. for (idx = 0; idx < 32; idx++) {
  723. pvoice = &gus->gf1.voices[idx];
  724. if (!pvoice->pcm)
  725. continue;
  726. pcmp = pvoice->private_data;
  727. if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
  728. continue;
  729. /* load real volume - better precision */
  730. spin_lock(&gus->reg_lock);
  731. snd_gf1_select_voice(gus, pvoice->number);
  732. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
  733. vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
  734. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
  735. pcmp->final_volume = 1;
  736. spin_unlock(&gus->reg_lock);
  737. }
  738. spin_unlock_irqrestore(&gus->voice_alloc, flags);
  739. return change;
  740. }
  741. static struct snd_kcontrol_new snd_gf1_pcm_volume_control =
  742. {
  743. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  744. .name = "PCM Playback Volume",
  745. .info = snd_gf1_pcm_volume_info,
  746. .get = snd_gf1_pcm_volume_get,
  747. .put = snd_gf1_pcm_volume_put
  748. };
  749. static struct snd_kcontrol_new snd_gf1_pcm_volume_control1 =
  750. {
  751. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  752. .name = "GPCM Playback Volume",
  753. .info = snd_gf1_pcm_volume_info,
  754. .get = snd_gf1_pcm_volume_get,
  755. .put = snd_gf1_pcm_volume_put
  756. };
  757. static struct snd_pcm_ops snd_gf1_pcm_playback_ops = {
  758. .open = snd_gf1_pcm_playback_open,
  759. .close = snd_gf1_pcm_playback_close,
  760. .ioctl = snd_pcm_lib_ioctl,
  761. .hw_params = snd_gf1_pcm_playback_hw_params,
  762. .hw_free = snd_gf1_pcm_playback_hw_free,
  763. .prepare = snd_gf1_pcm_playback_prepare,
  764. .trigger = snd_gf1_pcm_playback_trigger,
  765. .pointer = snd_gf1_pcm_playback_pointer,
  766. .copy = snd_gf1_pcm_playback_copy,
  767. .silence = snd_gf1_pcm_playback_silence,
  768. };
  769. static struct snd_pcm_ops snd_gf1_pcm_capture_ops = {
  770. .open = snd_gf1_pcm_capture_open,
  771. .close = snd_gf1_pcm_capture_close,
  772. .ioctl = snd_pcm_lib_ioctl,
  773. .hw_params = snd_gf1_pcm_capture_hw_params,
  774. .hw_free = snd_gf1_pcm_capture_hw_free,
  775. .prepare = snd_gf1_pcm_capture_prepare,
  776. .trigger = snd_gf1_pcm_capture_trigger,
  777. .pointer = snd_gf1_pcm_capture_pointer,
  778. };
  779. int snd_gf1_pcm_new(struct snd_gus_card *gus, int pcm_dev, int control_index)
  780. {
  781. struct snd_card *card;
  782. struct snd_kcontrol *kctl;
  783. struct snd_pcm *pcm;
  784. struct snd_pcm_substream *substream;
  785. int capture, err;
  786. card = gus->card;
  787. capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0;
  788. err = snd_pcm_new(card,
  789. gus->interwave ? "AMD InterWave" : "GF1",
  790. pcm_dev,
  791. gus->gf1.pcm_channels / 2,
  792. capture,
  793. &pcm);
  794. if (err < 0)
  795. return err;
  796. pcm->private_data = gus;
  797. /* playback setup */
  798. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops);
  799. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  800. snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
  801. snd_dma_isa_data(),
  802. 64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024);
  803. pcm->info_flags = 0;
  804. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  805. if (capture) {
  806. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops);
  807. if (gus->gf1.dma2 == gus->gf1.dma1)
  808. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  809. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  810. SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
  811. 64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024);
  812. }
  813. strcpy(pcm->name, pcm->id);
  814. if (gus->interwave) {
  815. sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
  816. }
  817. strcat(pcm->name, " (synth)");
  818. gus->pcm = pcm;
  819. if (gus->codec_flag)
  820. kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
  821. else
  822. kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
  823. if ((err = snd_ctl_add(card, kctl)) < 0)
  824. return err;
  825. kctl->id.index = control_index;
  826. return 0;
  827. }