emu8000_pcm.c 17 KB

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