emu8000_pcm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * pcm emulation on emu8000 wavetable
  3. *
  4. * Copyright (C) 2002 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "emu8000_local.h"
  21. #include <linux/sched/signal.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <sound/initval.h>
  25. #include <sound/pcm.h>
  26. /*
  27. * define the following if you want to use this pcm with non-interleaved mode
  28. */
  29. /* #define USE_NONINTERLEAVE */
  30. /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
  31. * mmap_emulation flag to 1 in your .asoundrc, such like
  32. *
  33. * pcm.emu8k {
  34. * type plug
  35. * slave.pcm {
  36. * type hw
  37. * card 0
  38. * device 1
  39. * mmap_emulation 1
  40. * }
  41. * }
  42. *
  43. * besides, for the time being, the non-interleaved mode doesn't work well on
  44. * alsa-lib...
  45. */
  46. struct snd_emu8k_pcm {
  47. struct snd_emu8000 *emu;
  48. struct snd_pcm_substream *substream;
  49. unsigned int allocated_bytes;
  50. struct snd_util_memblk *block;
  51. unsigned int offset;
  52. unsigned int buf_size;
  53. unsigned int period_size;
  54. unsigned int loop_start[2];
  55. unsigned int pitch;
  56. int panning[2];
  57. int last_ptr;
  58. int period_pos;
  59. int voices;
  60. unsigned int dram_opened: 1;
  61. unsigned int running: 1;
  62. unsigned int timer_running: 1;
  63. struct timer_list timer;
  64. spinlock_t timer_lock;
  65. };
  66. #define LOOP_BLANK_SIZE 8
  67. /*
  68. * open up channels for the simultaneous data transfer and playback
  69. */
  70. static int
  71. emu8k_open_dram_for_pcm(struct snd_emu8000 *emu, int channels)
  72. {
  73. int i;
  74. /* reserve up to 2 voices for playback */
  75. snd_emux_lock_voice(emu->emu, 0);
  76. if (channels > 1)
  77. snd_emux_lock_voice(emu->emu, 1);
  78. /* reserve 28 voices for loading */
  79. for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) {
  80. unsigned int mode = EMU8000_RAM_WRITE;
  81. snd_emux_lock_voice(emu->emu, i);
  82. #ifndef USE_NONINTERLEAVE
  83. if (channels > 1 && (i & 1) != 0)
  84. mode |= EMU8000_RAM_RIGHT;
  85. #endif
  86. snd_emu8000_dma_chan(emu, i, mode);
  87. }
  88. /* assign voice 31 and 32 to ROM */
  89. EMU8000_VTFT_WRITE(emu, 30, 0);
  90. EMU8000_PSST_WRITE(emu, 30, 0x1d8);
  91. EMU8000_CSL_WRITE(emu, 30, 0x1e0);
  92. EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
  93. EMU8000_VTFT_WRITE(emu, 31, 0);
  94. EMU8000_PSST_WRITE(emu, 31, 0x1d8);
  95. EMU8000_CSL_WRITE(emu, 31, 0x1e0);
  96. EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
  97. return 0;
  98. }
  99. /*
  100. */
  101. static void
  102. snd_emu8000_write_wait(struct snd_emu8000 *emu, int can_schedule)
  103. {
  104. while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
  105. if (can_schedule) {
  106. schedule_timeout_interruptible(1);
  107. if (signal_pending(current))
  108. break;
  109. }
  110. }
  111. }
  112. /*
  113. * close all channels
  114. */
  115. static void
  116. emu8k_close_dram(struct snd_emu8000 *emu)
  117. {
  118. int i;
  119. for (i = 0; i < 2; i++)
  120. snd_emux_unlock_voice(emu->emu, i);
  121. for (; i < EMU8000_DRAM_VOICES; i++) {
  122. snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE);
  123. snd_emux_unlock_voice(emu->emu, i);
  124. }
  125. }
  126. /*
  127. * convert Hz to AWE32 rate offset (see emux/soundfont.c)
  128. */
  129. #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */
  130. #define SAMPLERATE_RATIO 4096
  131. static int calc_rate_offset(int hz)
  132. {
  133. return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO);
  134. }
  135. /*
  136. */
  137. static const struct snd_pcm_hardware emu8k_pcm_hw = {
  138. #ifdef USE_NONINTERLEAVE
  139. .info = SNDRV_PCM_INFO_NONINTERLEAVED,
  140. #else
  141. .info = SNDRV_PCM_INFO_INTERLEAVED,
  142. #endif
  143. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  144. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  145. .rate_min = 4000,
  146. .rate_max = 48000,
  147. .channels_min = 1,
  148. .channels_max = 2,
  149. .buffer_bytes_max = (128*1024),
  150. .period_bytes_min = 1024,
  151. .period_bytes_max = (128*1024),
  152. .periods_min = 2,
  153. .periods_max = 1024,
  154. .fifo_size = 0,
  155. };
  156. /*
  157. * get the current position at the given channel from CCCA register
  158. */
  159. static inline int emu8k_get_curpos(struct snd_emu8k_pcm *rec, int ch)
  160. {
  161. int val = EMU8000_CCCA_READ(rec->emu, ch) & 0xfffffff;
  162. val -= rec->loop_start[ch] - 1;
  163. return val;
  164. }
  165. /*
  166. * timer interrupt handler
  167. * check the current position and update the period if necessary.
  168. */
  169. static void emu8k_pcm_timer_func(struct timer_list *t)
  170. {
  171. struct snd_emu8k_pcm *rec = from_timer(rec, t, timer);
  172. int ptr, delta;
  173. spin_lock(&rec->timer_lock);
  174. /* update the current pointer */
  175. ptr = emu8k_get_curpos(rec, 0);
  176. if (ptr < rec->last_ptr)
  177. delta = ptr + rec->buf_size - rec->last_ptr;
  178. else
  179. delta = ptr - rec->last_ptr;
  180. rec->period_pos += delta;
  181. rec->last_ptr = ptr;
  182. /* reprogram timer */
  183. mod_timer(&rec->timer, jiffies + 1);
  184. /* update period */
  185. if (rec->period_pos >= (int)rec->period_size) {
  186. rec->period_pos %= rec->period_size;
  187. spin_unlock(&rec->timer_lock);
  188. snd_pcm_period_elapsed(rec->substream);
  189. return;
  190. }
  191. spin_unlock(&rec->timer_lock);
  192. }
  193. /*
  194. * open pcm
  195. * creating an instance here
  196. */
  197. static int emu8k_pcm_open(struct snd_pcm_substream *subs)
  198. {
  199. struct snd_emu8000 *emu = snd_pcm_substream_chip(subs);
  200. struct snd_emu8k_pcm *rec;
  201. struct snd_pcm_runtime *runtime = subs->runtime;
  202. rec = kzalloc(sizeof(*rec), GFP_KERNEL);
  203. if (! rec)
  204. return -ENOMEM;
  205. rec->emu = emu;
  206. rec->substream = subs;
  207. runtime->private_data = rec;
  208. spin_lock_init(&rec->timer_lock);
  209. timer_setup(&rec->timer, emu8k_pcm_timer_func, 0);
  210. runtime->hw = emu8k_pcm_hw;
  211. runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3;
  212. runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2;
  213. /* use timer to update periods.. (specified in msec) */
  214. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  215. (1000000 + HZ - 1) / HZ, UINT_MAX);
  216. return 0;
  217. }
  218. static int emu8k_pcm_close(struct snd_pcm_substream *subs)
  219. {
  220. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  221. kfree(rec);
  222. subs->runtime->private_data = NULL;
  223. return 0;
  224. }
  225. /*
  226. * calculate pitch target
  227. */
  228. static int calc_pitch_target(int pitch)
  229. {
  230. int ptarget = 1 << (pitch >> 12);
  231. if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710;
  232. if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710;
  233. if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710;
  234. ptarget += (ptarget >> 1);
  235. if (ptarget > 0xffff) ptarget = 0xffff;
  236. return ptarget;
  237. }
  238. /*
  239. * set up the voice
  240. */
  241. static void setup_voice(struct snd_emu8k_pcm *rec, int ch)
  242. {
  243. struct snd_emu8000 *hw = rec->emu;
  244. unsigned int temp;
  245. /* channel to be silent and idle */
  246. EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
  247. EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
  248. EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
  249. EMU8000_PTRX_WRITE(hw, ch, 0);
  250. EMU8000_CPF_WRITE(hw, ch, 0);
  251. /* pitch offset */
  252. EMU8000_IP_WRITE(hw, ch, rec->pitch);
  253. /* set envelope parameters */
  254. EMU8000_ENVVAL_WRITE(hw, ch, 0x8000);
  255. EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f);
  256. EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f);
  257. EMU8000_ENVVOL_WRITE(hw, ch, 0x8000);
  258. EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f);
  259. /* decay/sustain parameter for volume envelope is used
  260. for triggerg the voice */
  261. /* modulation envelope heights */
  262. EMU8000_PEFE_WRITE(hw, ch, 0x0);
  263. /* lfo1/2 delay */
  264. EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
  265. EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
  266. /* lfo1 pitch & cutoff shift */
  267. EMU8000_FMMOD_WRITE(hw, ch, 0);
  268. /* lfo1 volume & freq */
  269. EMU8000_TREMFRQ_WRITE(hw, ch, 0);
  270. /* lfo2 pitch & freq */
  271. EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
  272. /* pan & loop start */
  273. temp = rec->panning[ch];
  274. temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
  275. EMU8000_PSST_WRITE(hw, ch, temp);
  276. /* chorus & loop end (chorus 8bit, MSB) */
  277. temp = 0; // chorus
  278. temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
  279. EMU8000_CSL_WRITE(hw, ch, temp);
  280. /* Q & current address (Q 4bit value, MSB) */
  281. temp = 0; // filterQ
  282. temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
  283. EMU8000_CCCA_WRITE(hw, ch, temp);
  284. /* clear unknown registers */
  285. EMU8000_00A0_WRITE(hw, ch, 0);
  286. EMU8000_0080_WRITE(hw, ch, 0);
  287. }
  288. /*
  289. * trigger the voice
  290. */
  291. static void start_voice(struct snd_emu8k_pcm *rec, int ch)
  292. {
  293. unsigned long flags;
  294. struct snd_emu8000 *hw = rec->emu;
  295. unsigned int temp, aux;
  296. int pt = calc_pitch_target(rec->pitch);
  297. /* cutoff and volume */
  298. EMU8000_IFATN_WRITE(hw, ch, 0xff00);
  299. EMU8000_VTFT_WRITE(hw, ch, 0xffff);
  300. EMU8000_CVCF_WRITE(hw, ch, 0xffff);
  301. /* trigger envelope */
  302. EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
  303. /* set reverb and pitch target */
  304. temp = 0; // reverb
  305. if (rec->panning[ch] == 0)
  306. aux = 0xff;
  307. else
  308. aux = (-rec->panning[ch]) & 0xff;
  309. temp = (temp << 8) | (pt << 16) | aux;
  310. EMU8000_PTRX_WRITE(hw, ch, temp);
  311. EMU8000_CPF_WRITE(hw, ch, pt << 16);
  312. /* start timer */
  313. spin_lock_irqsave(&rec->timer_lock, flags);
  314. if (! rec->timer_running) {
  315. mod_timer(&rec->timer, jiffies + 1);
  316. rec->timer_running = 1;
  317. }
  318. spin_unlock_irqrestore(&rec->timer_lock, flags);
  319. }
  320. /*
  321. * stop the voice immediately
  322. */
  323. static void stop_voice(struct snd_emu8k_pcm *rec, int ch)
  324. {
  325. unsigned long flags;
  326. struct snd_emu8000 *hw = rec->emu;
  327. EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
  328. /* stop timer */
  329. spin_lock_irqsave(&rec->timer_lock, flags);
  330. if (rec->timer_running) {
  331. del_timer(&rec->timer);
  332. rec->timer_running = 0;
  333. }
  334. spin_unlock_irqrestore(&rec->timer_lock, flags);
  335. }
  336. static int emu8k_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
  337. {
  338. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  339. int ch;
  340. switch (cmd) {
  341. case SNDRV_PCM_TRIGGER_START:
  342. for (ch = 0; ch < rec->voices; ch++)
  343. start_voice(rec, ch);
  344. rec->running = 1;
  345. break;
  346. case SNDRV_PCM_TRIGGER_STOP:
  347. rec->running = 0;
  348. for (ch = 0; ch < rec->voices; ch++)
  349. stop_voice(rec, ch);
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. return 0;
  355. }
  356. /*
  357. * copy / silence ops
  358. */
  359. /*
  360. * this macro should be inserted in the copy/silence loops
  361. * to reduce the latency. without this, the system will hang up
  362. * during the whole loop.
  363. */
  364. #define CHECK_SCHEDULER() \
  365. do { \
  366. cond_resched();\
  367. if (signal_pending(current))\
  368. return -EAGAIN;\
  369. } while (0)
  370. enum {
  371. COPY_USER, COPY_KERNEL, FILL_SILENCE,
  372. };
  373. #define GET_VAL(sval, buf, mode) \
  374. do { \
  375. switch (mode) { \
  376. case FILL_SILENCE: \
  377. sval = 0; \
  378. break; \
  379. case COPY_KERNEL: \
  380. sval = *buf++; \
  381. break; \
  382. default: \
  383. if (get_user(sval, (unsigned short __user *)buf)) \
  384. return -EFAULT; \
  385. buf++; \
  386. break; \
  387. } \
  388. } while (0)
  389. #ifdef USE_NONINTERLEAVE
  390. #define LOOP_WRITE(rec, offset, _buf, count, mode) \
  391. do { \
  392. struct snd_emu8000 *emu = (rec)->emu; \
  393. unsigned short *buf = (unsigned short *)(_buf); \
  394. snd_emu8000_write_wait(emu, 1); \
  395. EMU8000_SMALW_WRITE(emu, offset); \
  396. while (count > 0) { \
  397. unsigned short sval; \
  398. CHECK_SCHEDULER(); \
  399. GET_VAL(sval, buf, mode); \
  400. EMU8000_SMLD_WRITE(emu, sval); \
  401. count--; \
  402. } \
  403. } while (0)
  404. /* copy one channel block */
  405. static int emu8k_pcm_copy(struct snd_pcm_substream *subs,
  406. int voice, unsigned long pos,
  407. void __user *src, unsigned long count)
  408. {
  409. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  410. /* convert to word unit */
  411. pos = (pos << 1) + rec->loop_start[voice];
  412. count <<= 1;
  413. LOOP_WRITE(rec, pos, src, count, COPY_USER);
  414. return 0;
  415. }
  416. static int emu8k_pcm_copy_kernel(struct snd_pcm_substream *subs,
  417. int voice, unsigned long pos,
  418. void *src, unsigned long count)
  419. {
  420. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  421. /* convert to word unit */
  422. pos = (pos << 1) + rec->loop_start[voice];
  423. count <<= 1;
  424. LOOP_WRITE(rec, pos, src, count, COPY_KERNEL);
  425. return 0;
  426. }
  427. /* make a channel block silence */
  428. static int emu8k_pcm_silence(struct snd_pcm_substream *subs,
  429. int voice, unsigned long pos, unsigned long count)
  430. {
  431. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  432. /* convert to word unit */
  433. pos = (pos << 1) + rec->loop_start[voice];
  434. count <<= 1;
  435. LOOP_WRITE(rec, pos, NULL, count, FILL_SILENCE);
  436. return 0;
  437. }
  438. #else /* interleave */
  439. #define LOOP_WRITE(rec, pos, _buf, count, mode) \
  440. do { \
  441. struct snd_emu8000 *emu = rec->emu; \
  442. unsigned short *buf = (unsigned short *)(_buf); \
  443. snd_emu8000_write_wait(emu, 1); \
  444. EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]); \
  445. if (rec->voices > 1) \
  446. EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]); \
  447. while (count > 0) { \
  448. unsigned short sval; \
  449. CHECK_SCHEDULER(); \
  450. GET_VAL(sval, buf, mode); \
  451. EMU8000_SMLD_WRITE(emu, sval); \
  452. if (rec->voices > 1) { \
  453. CHECK_SCHEDULER(); \
  454. GET_VAL(sval, buf, mode); \
  455. EMU8000_SMRD_WRITE(emu, sval); \
  456. } \
  457. count--; \
  458. } \
  459. } while (0)
  460. /*
  461. * copy the interleaved data can be done easily by using
  462. * DMA "left" and "right" channels on emu8k engine.
  463. */
  464. static int emu8k_pcm_copy(struct snd_pcm_substream *subs,
  465. int voice, unsigned long pos,
  466. void __user *src, unsigned long count)
  467. {
  468. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  469. /* convert to frames */
  470. pos = bytes_to_frames(subs->runtime, pos);
  471. count = bytes_to_frames(subs->runtime, count);
  472. LOOP_WRITE(rec, pos, src, count, COPY_USER);
  473. return 0;
  474. }
  475. static int emu8k_pcm_copy_kernel(struct snd_pcm_substream *subs,
  476. int voice, unsigned long pos,
  477. void *src, unsigned long count)
  478. {
  479. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  480. /* convert to frames */
  481. pos = bytes_to_frames(subs->runtime, pos);
  482. count = bytes_to_frames(subs->runtime, count);
  483. LOOP_WRITE(rec, pos, src, count, COPY_KERNEL);
  484. return 0;
  485. }
  486. static int emu8k_pcm_silence(struct snd_pcm_substream *subs,
  487. int voice, unsigned long pos, unsigned long count)
  488. {
  489. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  490. /* convert to frames */
  491. pos = bytes_to_frames(subs->runtime, pos);
  492. count = bytes_to_frames(subs->runtime, count);
  493. LOOP_WRITE(rec, pos, NULL, count, FILL_SILENCE);
  494. return 0;
  495. }
  496. #endif
  497. /*
  498. * allocate a memory block
  499. */
  500. static int emu8k_pcm_hw_params(struct snd_pcm_substream *subs,
  501. struct snd_pcm_hw_params *hw_params)
  502. {
  503. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  504. if (rec->block) {
  505. /* reallocation - release the old block */
  506. snd_util_mem_free(rec->emu->memhdr, rec->block);
  507. rec->block = NULL;
  508. }
  509. rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
  510. rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
  511. if (! rec->block)
  512. return -ENOMEM;
  513. rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */
  514. /* at least dma_bytes must be set for non-interleaved mode */
  515. subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
  516. return 0;
  517. }
  518. /*
  519. * free the memory block
  520. */
  521. static int emu8k_pcm_hw_free(struct snd_pcm_substream *subs)
  522. {
  523. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  524. if (rec->block) {
  525. int ch;
  526. for (ch = 0; ch < rec->voices; ch++)
  527. stop_voice(rec, ch); // to be sure
  528. if (rec->dram_opened)
  529. emu8k_close_dram(rec->emu);
  530. snd_util_mem_free(rec->emu->memhdr, rec->block);
  531. rec->block = NULL;
  532. }
  533. return 0;
  534. }
  535. /*
  536. */
  537. static int emu8k_pcm_prepare(struct snd_pcm_substream *subs)
  538. {
  539. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  540. rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
  541. rec->last_ptr = 0;
  542. rec->period_pos = 0;
  543. rec->buf_size = subs->runtime->buffer_size;
  544. rec->period_size = subs->runtime->period_size;
  545. rec->voices = subs->runtime->channels;
  546. rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
  547. if (rec->voices > 1)
  548. rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
  549. if (rec->voices > 1) {
  550. rec->panning[0] = 0xff;
  551. rec->panning[1] = 0x00;
  552. } else
  553. rec->panning[0] = 0x80;
  554. if (! rec->dram_opened) {
  555. int err, i, ch;
  556. snd_emux_terminate_all(rec->emu->emu);
  557. if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
  558. return err;
  559. rec->dram_opened = 1;
  560. /* clear loop blanks */
  561. snd_emu8000_write_wait(rec->emu, 0);
  562. EMU8000_SMALW_WRITE(rec->emu, rec->offset);
  563. for (i = 0; i < LOOP_BLANK_SIZE; i++)
  564. EMU8000_SMLD_WRITE(rec->emu, 0);
  565. for (ch = 0; ch < rec->voices; ch++) {
  566. EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
  567. for (i = 0; i < LOOP_BLANK_SIZE; i++)
  568. EMU8000_SMLD_WRITE(rec->emu, 0);
  569. }
  570. }
  571. setup_voice(rec, 0);
  572. if (rec->voices > 1)
  573. setup_voice(rec, 1);
  574. return 0;
  575. }
  576. static snd_pcm_uframes_t emu8k_pcm_pointer(struct snd_pcm_substream *subs)
  577. {
  578. struct snd_emu8k_pcm *rec = subs->runtime->private_data;
  579. if (rec->running)
  580. return emu8k_get_curpos(rec, 0);
  581. return 0;
  582. }
  583. static const struct snd_pcm_ops emu8k_pcm_ops = {
  584. .open = emu8k_pcm_open,
  585. .close = emu8k_pcm_close,
  586. .ioctl = snd_pcm_lib_ioctl,
  587. .hw_params = emu8k_pcm_hw_params,
  588. .hw_free = emu8k_pcm_hw_free,
  589. .prepare = emu8k_pcm_prepare,
  590. .trigger = emu8k_pcm_trigger,
  591. .pointer = emu8k_pcm_pointer,
  592. .copy_user = emu8k_pcm_copy,
  593. .copy_kernel = emu8k_pcm_copy_kernel,
  594. .fill_silence = emu8k_pcm_silence,
  595. };
  596. static void snd_emu8000_pcm_free(struct snd_pcm *pcm)
  597. {
  598. struct snd_emu8000 *emu = pcm->private_data;
  599. emu->pcm = NULL;
  600. }
  601. int snd_emu8000_pcm_new(struct snd_card *card, struct snd_emu8000 *emu, int index)
  602. {
  603. struct snd_pcm *pcm;
  604. int err;
  605. if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
  606. return err;
  607. pcm->private_data = emu;
  608. pcm->private_free = snd_emu8000_pcm_free;
  609. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
  610. emu->pcm = pcm;
  611. snd_device_register(card, pcm);
  612. return 0;
  613. }