gus_pcm.c 27 KB

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