2 * bebob_pcm.c - a part of driver for BeBoB based devices
4 * Copyright (c) 2013-2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
12 hw_rule_rate(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
14 struct snd_bebob_stream_formation
*formations
= rule
->private;
15 struct snd_interval
*r
=
16 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
17 const struct snd_interval
*c
=
18 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
19 struct snd_interval t
= {
20 .min
= UINT_MAX
, .max
= 0, .integer
= 1
24 for (i
= 0; i
< SND_BEBOB_STRM_FMT_ENTRIES
; i
++) {
25 /* entry is invalid */
26 if (formations
[i
].pcm
== 0)
29 if (!snd_interval_test(c
, formations
[i
].pcm
))
32 t
.min
= min(t
.min
, snd_bebob_rate_table
[i
]);
33 t
.max
= max(t
.max
, snd_bebob_rate_table
[i
]);
36 return snd_interval_refine(r
, &t
);
40 hw_rule_channels(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
42 struct snd_bebob_stream_formation
*formations
= rule
->private;
43 struct snd_interval
*c
=
44 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
45 const struct snd_interval
*r
=
46 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_RATE
);
47 struct snd_interval t
= {
48 .min
= UINT_MAX
, .max
= 0, .integer
= 1
53 for (i
= 0; i
< SND_BEBOB_STRM_FMT_ENTRIES
; i
++) {
54 /* entry is invalid */
55 if (formations
[i
].pcm
== 0)
58 if (!snd_interval_test(r
, snd_bebob_rate_table
[i
]))
61 t
.min
= min(t
.min
, formations
[i
].pcm
);
62 t
.max
= max(t
.max
, formations
[i
].pcm
);
65 return snd_interval_refine(c
, &t
);
69 limit_channels_and_rates(struct snd_pcm_hardware
*hw
,
70 struct snd_bebob_stream_formation
*formations
)
74 hw
->channels_min
= UINT_MAX
;
77 hw
->rate_min
= UINT_MAX
;
81 for (i
= 0; i
< SND_BEBOB_STRM_FMT_ENTRIES
; i
++) {
82 /* entry has no PCM channels */
83 if (formations
[i
].pcm
== 0)
86 hw
->channels_min
= min(hw
->channels_min
, formations
[i
].pcm
);
87 hw
->channels_max
= max(hw
->channels_max
, formations
[i
].pcm
);
89 hw
->rate_min
= min(hw
->rate_min
, snd_bebob_rate_table
[i
]);
90 hw
->rate_max
= max(hw
->rate_max
, snd_bebob_rate_table
[i
]);
91 hw
->rates
|= snd_pcm_rate_to_rate_bit(snd_bebob_rate_table
[i
]);
96 pcm_init_hw_params(struct snd_bebob
*bebob
,
97 struct snd_pcm_substream
*substream
)
99 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
100 struct amdtp_stream
*s
;
101 struct snd_bebob_stream_formation
*formations
;
104 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
105 runtime
->hw
.formats
= AM824_IN_PCM_FORMAT_BITS
;
106 s
= &bebob
->tx_stream
;
107 formations
= bebob
->tx_stream_formations
;
109 runtime
->hw
.formats
= AM824_OUT_PCM_FORMAT_BITS
;
110 s
= &bebob
->rx_stream
;
111 formations
= bebob
->rx_stream_formations
;
114 limit_channels_and_rates(&runtime
->hw
, formations
);
116 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
117 hw_rule_channels
, formations
,
118 SNDRV_PCM_HW_PARAM_RATE
, -1);
122 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
123 hw_rule_rate
, formations
,
124 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
128 err
= amdtp_am824_add_pcm_hw_constraints(s
, runtime
);
134 pcm_open(struct snd_pcm_substream
*substream
)
136 struct snd_bebob
*bebob
= substream
->private_data
;
137 const struct snd_bebob_rate_spec
*spec
= bebob
->spec
->rate
;
138 unsigned int sampling_rate
;
139 enum snd_bebob_clock_type src
;
142 err
= snd_bebob_stream_lock_try(bebob
);
146 err
= pcm_init_hw_params(bebob
, substream
);
150 err
= snd_bebob_stream_get_clock_src(bebob
, &src
);
155 * When source of clock is internal or any PCM stream are running,
156 * the available sampling rate is limited at current sampling rate.
158 if (src
== SND_BEBOB_CLOCK_TYPE_EXTERNAL
||
159 amdtp_stream_pcm_running(&bebob
->tx_stream
) ||
160 amdtp_stream_pcm_running(&bebob
->rx_stream
)) {
161 err
= spec
->get(bebob
, &sampling_rate
);
163 dev_err(&bebob
->unit
->device
,
164 "fail to get sampling rate: %d\n", err
);
168 substream
->runtime
->hw
.rate_min
= sampling_rate
;
169 substream
->runtime
->hw
.rate_max
= sampling_rate
;
172 snd_pcm_set_sync(substream
);
176 snd_bebob_stream_lock_release(bebob
);
181 pcm_close(struct snd_pcm_substream
*substream
)
183 struct snd_bebob
*bebob
= substream
->private_data
;
184 snd_bebob_stream_lock_release(bebob
);
189 pcm_capture_hw_params(struct snd_pcm_substream
*substream
,
190 struct snd_pcm_hw_params
*hw_params
)
192 struct snd_bebob
*bebob
= substream
->private_data
;
195 err
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
196 params_buffer_bytes(hw_params
));
200 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
201 mutex_lock(&bebob
->mutex
);
202 bebob
->substreams_counter
++;
203 mutex_unlock(&bebob
->mutex
);
209 pcm_playback_hw_params(struct snd_pcm_substream
*substream
,
210 struct snd_pcm_hw_params
*hw_params
)
212 struct snd_bebob
*bebob
= substream
->private_data
;
215 err
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
216 params_buffer_bytes(hw_params
));
220 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
221 mutex_lock(&bebob
->mutex
);
222 bebob
->substreams_counter
++;
223 mutex_unlock(&bebob
->mutex
);
230 pcm_capture_hw_free(struct snd_pcm_substream
*substream
)
232 struct snd_bebob
*bebob
= substream
->private_data
;
234 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
) {
235 mutex_lock(&bebob
->mutex
);
236 bebob
->substreams_counter
--;
237 mutex_unlock(&bebob
->mutex
);
240 snd_bebob_stream_stop_duplex(bebob
);
242 return snd_pcm_lib_free_vmalloc_buffer(substream
);
245 pcm_playback_hw_free(struct snd_pcm_substream
*substream
)
247 struct snd_bebob
*bebob
= substream
->private_data
;
249 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
) {
250 mutex_lock(&bebob
->mutex
);
251 bebob
->substreams_counter
--;
252 mutex_unlock(&bebob
->mutex
);
255 snd_bebob_stream_stop_duplex(bebob
);
257 return snd_pcm_lib_free_vmalloc_buffer(substream
);
261 pcm_capture_prepare(struct snd_pcm_substream
*substream
)
263 struct snd_bebob
*bebob
= substream
->private_data
;
264 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
267 err
= snd_bebob_stream_start_duplex(bebob
, runtime
->rate
);
269 amdtp_stream_pcm_prepare(&bebob
->tx_stream
);
274 pcm_playback_prepare(struct snd_pcm_substream
*substream
)
276 struct snd_bebob
*bebob
= substream
->private_data
;
277 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
280 err
= snd_bebob_stream_start_duplex(bebob
, runtime
->rate
);
282 amdtp_stream_pcm_prepare(&bebob
->rx_stream
);
288 pcm_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
290 struct snd_bebob
*bebob
= substream
->private_data
;
293 case SNDRV_PCM_TRIGGER_START
:
294 amdtp_stream_pcm_trigger(&bebob
->tx_stream
, substream
);
296 case SNDRV_PCM_TRIGGER_STOP
:
297 amdtp_stream_pcm_trigger(&bebob
->tx_stream
, NULL
);
306 pcm_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
308 struct snd_bebob
*bebob
= substream
->private_data
;
311 case SNDRV_PCM_TRIGGER_START
:
312 amdtp_stream_pcm_trigger(&bebob
->rx_stream
, substream
);
314 case SNDRV_PCM_TRIGGER_STOP
:
315 amdtp_stream_pcm_trigger(&bebob
->rx_stream
, NULL
);
324 static snd_pcm_uframes_t
325 pcm_capture_pointer(struct snd_pcm_substream
*sbstrm
)
327 struct snd_bebob
*bebob
= sbstrm
->private_data
;
328 return amdtp_stream_pcm_pointer(&bebob
->tx_stream
);
330 static snd_pcm_uframes_t
331 pcm_playback_pointer(struct snd_pcm_substream
*sbstrm
)
333 struct snd_bebob
*bebob
= sbstrm
->private_data
;
334 return amdtp_stream_pcm_pointer(&bebob
->rx_stream
);
337 static int pcm_capture_ack(struct snd_pcm_substream
*substream
)
339 struct snd_bebob
*bebob
= substream
->private_data
;
341 return amdtp_stream_pcm_ack(&bebob
->tx_stream
);
344 static int pcm_playback_ack(struct snd_pcm_substream
*substream
)
346 struct snd_bebob
*bebob
= substream
->private_data
;
348 return amdtp_stream_pcm_ack(&bebob
->rx_stream
);
351 int snd_bebob_create_pcm_devices(struct snd_bebob
*bebob
)
353 static const struct snd_pcm_ops capture_ops
= {
356 .ioctl
= snd_pcm_lib_ioctl
,
357 .hw_params
= pcm_capture_hw_params
,
358 .hw_free
= pcm_capture_hw_free
,
359 .prepare
= pcm_capture_prepare
,
360 .trigger
= pcm_capture_trigger
,
361 .pointer
= pcm_capture_pointer
,
362 .ack
= pcm_capture_ack
,
363 .page
= snd_pcm_lib_get_vmalloc_page
,
365 static const struct snd_pcm_ops playback_ops
= {
368 .ioctl
= snd_pcm_lib_ioctl
,
369 .hw_params
= pcm_playback_hw_params
,
370 .hw_free
= pcm_playback_hw_free
,
371 .prepare
= pcm_playback_prepare
,
372 .trigger
= pcm_playback_trigger
,
373 .pointer
= pcm_playback_pointer
,
374 .ack
= pcm_playback_ack
,
375 .page
= snd_pcm_lib_get_vmalloc_page
,
376 .mmap
= snd_pcm_lib_mmap_vmalloc
,
381 err
= snd_pcm_new(bebob
->card
, bebob
->card
->driver
, 0, 1, 1, &pcm
);
385 pcm
->private_data
= bebob
;
386 snprintf(pcm
->name
, sizeof(pcm
->name
),
387 "%s PCM", bebob
->card
->shortname
);
388 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &playback_ops
);
389 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &capture_ops
);