gus_pcm.c 29 KB

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