2 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
4 * This source file is released under GPL v2 license (no other versions).
5 * See the COPYING file included in the main directory of this source
6 * distribution for the license terms and conditions.
11 * This file contains the definition of the pcm device functions.
19 #include <sound/pcm.h>
21 /* Hardware descriptions for playback */
22 static struct snd_pcm_hardware ct_pcm_playback_hw = {
23 .info = (SNDRV_PCM_INFO_MMAP |
24 SNDRV_PCM_INFO_INTERLEAVED |
25 SNDRV_PCM_INFO_BLOCK_TRANSFER |
26 SNDRV_PCM_INFO_MMAP_VALID |
27 SNDRV_PCM_INFO_PAUSE),
28 .formats = (SNDRV_PCM_FMTBIT_U8 |
30 SNDRV_PCM_FMTBIT_S16_LE |
31 SNDRV_PCM_FMTBIT_U16_LE |
32 SNDRV_PCM_FMTBIT_S24_3LE |
33 SNDRV_PCM_FMTBIT_S24_LE |
34 SNDRV_PCM_FMTBIT_S32_LE),
35 .rates = (SNDRV_PCM_RATE_CONTINUOUS |
36 SNDRV_PCM_RATE_8000_192000),
41 .buffer_bytes_max = (128*1024),
42 .period_bytes_min = (64),
43 .period_bytes_max = (128*1024),
49 static struct snd_pcm_hardware ct_spdif_passthru_playback_hw = {
50 .info = (SNDRV_PCM_INFO_MMAP |
51 SNDRV_PCM_INFO_INTERLEAVED |
52 SNDRV_PCM_INFO_BLOCK_TRANSFER |
53 SNDRV_PCM_INFO_MMAP_VALID |
54 SNDRV_PCM_INFO_PAUSE),
55 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
56 SNDRV_PCM_FMTBIT_U16_LE),
57 .rates = (SNDRV_PCM_RATE_48000 |
58 SNDRV_PCM_RATE_44100 |
59 SNDRV_PCM_RATE_32000),
64 .buffer_bytes_max = (128*1024),
65 .period_bytes_min = (64),
66 .period_bytes_max = (128*1024),
72 /* Hardware descriptions for capture */
73 static struct snd_pcm_hardware ct_pcm_capture_hw = {
74 .info = (SNDRV_PCM_INFO_MMAP |
75 SNDRV_PCM_INFO_INTERLEAVED |
76 SNDRV_PCM_INFO_BLOCK_TRANSFER |
77 SNDRV_PCM_INFO_PAUSE |
78 SNDRV_PCM_INFO_MMAP_VALID),
79 .formats = (SNDRV_PCM_FMTBIT_U8 |
81 SNDRV_PCM_FMTBIT_S16_LE |
82 SNDRV_PCM_FMTBIT_U16_LE |
83 SNDRV_PCM_FMTBIT_S24_3LE |
84 SNDRV_PCM_FMTBIT_S24_LE |
85 SNDRV_PCM_FMTBIT_S32_LE),
86 .rates = (SNDRV_PCM_RATE_CONTINUOUS |
87 SNDRV_PCM_RATE_8000_96000),
92 .buffer_bytes_max = (128*1024),
93 .period_bytes_min = (384),
94 .period_bytes_max = (64*1024),
100 static void ct_atc_pcm_interrupt(struct ct_atc_pcm *atc_pcm)
102 struct ct_atc_pcm *apcm = atc_pcm;
104 if (NULL == apcm->substream)
107 snd_pcm_period_elapsed(apcm->substream);
110 static void ct_atc_pcm_free_substream(struct snd_pcm_runtime *runtime)
112 struct ct_atc_pcm *apcm = runtime->private_data;
113 struct ct_atc *atc = snd_pcm_substream_chip(apcm->substream);
115 atc->pcm_release_resources(atc, apcm);
117 runtime->private_data = NULL;
120 /* pcm playback operations */
121 static int ct_pcm_playback_open(struct snd_pcm_substream *substream)
123 struct ct_atc *atc = snd_pcm_substream_chip(substream);
124 struct snd_pcm_runtime *runtime = substream->runtime;
125 struct ct_atc_pcm *apcm;
128 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
132 spin_lock_init(&apcm->timer_lock);
133 apcm->stop_timer = 0;
134 apcm->substream = substream;
135 apcm->interrupt = ct_atc_pcm_interrupt;
136 runtime->private_data = apcm;
137 runtime->private_free = ct_atc_pcm_free_substream;
138 if (IEC958 == substream->pcm->device) {
139 runtime->hw = ct_spdif_passthru_playback_hw;
140 atc->spdif_out_passthru(atc, 1);
142 runtime->hw = ct_pcm_playback_hw;
143 if (FRONT == substream->pcm->device)
144 runtime->hw.channels_max = 8;
147 err = snd_pcm_hw_constraint_integer(runtime,
148 SNDRV_PCM_HW_PARAM_PERIODS);
153 err = snd_pcm_hw_constraint_minmax(runtime,
154 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
164 static int ct_pcm_playback_close(struct snd_pcm_substream *substream)
166 struct ct_atc *atc = snd_pcm_substream_chip(substream);
168 /* TODO: Notify mixer inactive. */
169 if (IEC958 == substream->pcm->device)
170 atc->spdif_out_passthru(atc, 0);
172 /* The ct_atc_pcm object will be freed by runtime->private_free */
177 static int ct_pcm_hw_params(struct snd_pcm_substream *substream,
178 struct snd_pcm_hw_params *hw_params)
180 return snd_pcm_lib_malloc_pages(substream,
181 params_buffer_bytes(hw_params));
184 static int ct_pcm_hw_free(struct snd_pcm_substream *substream)
186 /* Free snd-allocated pages */
187 return snd_pcm_lib_free_pages(substream);
190 static void ct_pcm_timer_callback(unsigned long data)
192 struct ct_atc_pcm *apcm = (struct ct_atc_pcm *)data;
193 struct snd_pcm_substream *substream = apcm->substream;
194 struct snd_pcm_runtime *runtime = substream->runtime;
195 unsigned int period_size = runtime->period_size;
196 unsigned int buffer_size = runtime->buffer_size;
198 unsigned int position = 0, dist = 0, interval = 0;
200 position = substream->ops->pointer(substream);
201 dist = (position + buffer_size - apcm->position) % buffer_size;
202 if ((dist >= period_size) ||
203 (position/period_size != apcm->position/period_size)) {
204 apcm->interrupt(apcm);
205 apcm->position = position;
207 /* Add extra HZ*5/1000 to avoid overrun issue when recording
208 * at 8kHz in 8-bit format or at 88kHz in 24-bit format. */
209 interval = ((period_size - (position % period_size))
210 * HZ + (runtime->rate - 1)) / runtime->rate + HZ * 5 / 1000;
211 spin_lock_irqsave(&apcm->timer_lock, flags);
212 apcm->timer.expires = jiffies + interval;
213 if (!apcm->stop_timer)
214 add_timer(&apcm->timer);
216 spin_unlock_irqrestore(&apcm->timer_lock, flags);
219 static int ct_pcm_timer_prepare(struct ct_atc_pcm *apcm)
223 spin_lock_irqsave(&apcm->timer_lock, flags);
224 if (timer_pending(&apcm->timer)) {
225 /* The timer has already been started. */
226 spin_unlock_irqrestore(&apcm->timer_lock, flags);
230 init_timer(&apcm->timer);
231 apcm->timer.data = (unsigned long)apcm;
232 apcm->timer.function = ct_pcm_timer_callback;
233 spin_unlock_irqrestore(&apcm->timer_lock, flags);
239 static int ct_pcm_timer_start(struct ct_atc_pcm *apcm)
241 struct snd_pcm_runtime *runtime = apcm->substream->runtime;
244 spin_lock_irqsave(&apcm->timer_lock, flags);
245 if (timer_pending(&apcm->timer)) {
246 /* The timer has already been started. */
247 spin_unlock_irqrestore(&apcm->timer_lock, flags);
251 apcm->timer.expires = jiffies + (runtime->period_size * HZ +
252 (runtime->rate - 1)) / runtime->rate;
253 apcm->stop_timer = 0;
254 add_timer(&apcm->timer);
255 spin_unlock_irqrestore(&apcm->timer_lock, flags);
260 static int ct_pcm_timer_stop(struct ct_atc_pcm *apcm)
264 spin_lock_irqsave(&apcm->timer_lock, flags);
265 apcm->stop_timer = 1;
266 del_timer(&apcm->timer);
267 spin_unlock_irqrestore(&apcm->timer_lock, flags);
269 try_to_del_timer_sync(&apcm->timer);
274 static int ct_pcm_playback_prepare(struct snd_pcm_substream *substream)
277 struct ct_atc *atc = snd_pcm_substream_chip(substream);
278 struct snd_pcm_runtime *runtime = substream->runtime;
279 struct ct_atc_pcm *apcm = runtime->private_data;
281 if (IEC958 == substream->pcm->device)
282 err = atc->spdif_passthru_playback_prepare(atc, apcm);
284 err = atc->pcm_playback_prepare(atc, apcm);
287 printk(KERN_ERR "ctxfi: Preparing pcm playback failed!!!\n");
291 ct_pcm_timer_prepare(apcm);
297 ct_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
299 struct ct_atc *atc = snd_pcm_substream_chip(substream);
300 struct snd_pcm_runtime *runtime = substream->runtime;
301 struct ct_atc_pcm *apcm = runtime->private_data;
304 case SNDRV_PCM_TRIGGER_START:
305 case SNDRV_PCM_TRIGGER_RESUME:
306 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
307 atc->pcm_playback_start(atc, apcm);
308 ct_pcm_timer_start(apcm);
310 case SNDRV_PCM_TRIGGER_STOP:
311 case SNDRV_PCM_TRIGGER_SUSPEND:
312 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
313 ct_pcm_timer_stop(apcm);
314 atc->pcm_playback_stop(atc, apcm);
323 static snd_pcm_uframes_t
324 ct_pcm_playback_pointer(struct snd_pcm_substream *substream)
326 unsigned long position;
327 struct ct_atc *atc = snd_pcm_substream_chip(substream);
328 struct snd_pcm_runtime *runtime = substream->runtime;
329 struct ct_atc_pcm *apcm = runtime->private_data;
331 /* Read out playback position */
332 position = atc->pcm_playback_position(atc, apcm);
333 position = bytes_to_frames(runtime, position);
337 /* pcm capture operations */
338 static int ct_pcm_capture_open(struct snd_pcm_substream *substream)
340 struct ct_atc *atc = snd_pcm_substream_chip(substream);
341 struct snd_pcm_runtime *runtime = substream->runtime;
342 struct ct_atc_pcm *apcm;
345 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
349 spin_lock_init(&apcm->timer_lock);
351 apcm->stop_timer = 0;
352 apcm->substream = substream;
353 apcm->interrupt = ct_atc_pcm_interrupt;
354 runtime->private_data = apcm;
355 runtime->private_free = ct_atc_pcm_free_substream;
356 runtime->hw = ct_pcm_capture_hw;
357 runtime->hw.rate_max = atc->rsr * atc->msr;
359 err = snd_pcm_hw_constraint_integer(runtime,
360 SNDRV_PCM_HW_PARAM_PERIODS);
365 err = snd_pcm_hw_constraint_minmax(runtime,
366 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
376 static int ct_pcm_capture_close(struct snd_pcm_substream *substream)
378 /* The ct_atc_pcm object will be freed by runtime->private_free */
379 /* TODO: Notify mixer inactive. */
383 static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream)
386 struct ct_atc *atc = snd_pcm_substream_chip(substream);
387 struct snd_pcm_runtime *runtime = substream->runtime;
388 struct ct_atc_pcm *apcm = runtime->private_data;
390 err = atc->pcm_capture_prepare(atc, apcm);
392 printk(KERN_ERR "ctxfi: Preparing pcm capture failed!!!\n");
396 ct_pcm_timer_prepare(apcm);
402 ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
404 struct ct_atc *atc = snd_pcm_substream_chip(substream);
405 struct snd_pcm_runtime *runtime = substream->runtime;
406 struct ct_atc_pcm *apcm = runtime->private_data;
409 case SNDRV_PCM_TRIGGER_START:
410 atc->pcm_capture_start(atc, apcm);
411 ct_pcm_timer_start(apcm);
413 case SNDRV_PCM_TRIGGER_STOP:
414 ct_pcm_timer_stop(apcm);
415 atc->pcm_capture_stop(atc, apcm);
418 ct_pcm_timer_stop(apcm);
419 atc->pcm_capture_stop(atc, apcm);
426 static snd_pcm_uframes_t
427 ct_pcm_capture_pointer(struct snd_pcm_substream *substream)
429 unsigned long position;
430 struct ct_atc *atc = snd_pcm_substream_chip(substream);
431 struct snd_pcm_runtime *runtime = substream->runtime;
432 struct ct_atc_pcm *apcm = runtime->private_data;
434 /* Read out playback position */
435 position = atc->pcm_capture_position(atc, apcm);
436 position = bytes_to_frames(runtime, position);
440 /* PCM operators for playback */
441 static struct snd_pcm_ops ct_pcm_playback_ops = {
442 .open = ct_pcm_playback_open,
443 .close = ct_pcm_playback_close,
444 .ioctl = snd_pcm_lib_ioctl,
445 .hw_params = ct_pcm_hw_params,
446 .hw_free = ct_pcm_hw_free,
447 .prepare = ct_pcm_playback_prepare,
448 .trigger = ct_pcm_playback_trigger,
449 .pointer = ct_pcm_playback_pointer,
452 /* PCM operators for capture */
453 static struct snd_pcm_ops ct_pcm_capture_ops = {
454 .open = ct_pcm_capture_open,
455 .close = ct_pcm_capture_close,
456 .ioctl = snd_pcm_lib_ioctl,
457 .hw_params = ct_pcm_hw_params,
458 .hw_free = ct_pcm_hw_free,
459 .prepare = ct_pcm_capture_prepare,
460 .trigger = ct_pcm_capture_trigger,
461 .pointer = ct_pcm_capture_pointer,
464 /* Create ALSA pcm device */
465 int ct_alsa_pcm_create(struct ct_atc *atc,
466 enum CTALSADEVS device,
467 const char *device_name)
471 int playback_count, capture_count;
474 strncpy(name, device_name, sizeof(name));
475 playback_count = (IEC958 == device) ? 1 : 8;
476 capture_count = (FRONT == device) ? 1 : 0;
477 err = snd_pcm_new(atc->card, name, device,
478 playback_count, capture_count, &pcm);
480 printk(KERN_ERR "ctxfi: snd_pcm_new failed!! Err=%d\n", err);
484 pcm->private_data = atc;
486 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
487 strcpy(pcm->name, device_name);
489 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops);
493 SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops);
495 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
496 snd_dma_pci_data(atc->pci), 128*1024, 128*1024);