gus_pcm.c 28 KB

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