Commit | Line | Data |
---|---|---|
da607e19 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
fbbebd2c TS |
2 | /* |
3 | * bebob_pcm.c - a part of driver for BeBoB based devices | |
4 | * | |
5 | * Copyright (c) 2013-2014 Takashi Sakamoto | |
fbbebd2c TS |
6 | */ |
7 | ||
8 | #include "./bebob.h" | |
9 | ||
10 | static int | |
11 | hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) | |
12 | { | |
13 | struct snd_bebob_stream_formation *formations = rule->private; | |
14 | struct snd_interval *r = | |
15 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
16 | const struct snd_interval *c = | |
17 | hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
18 | struct snd_interval t = { | |
19 | .min = UINT_MAX, .max = 0, .integer = 1 | |
20 | }; | |
21 | unsigned int i; | |
22 | ||
23 | for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) { | |
24 | /* entry is invalid */ | |
25 | if (formations[i].pcm == 0) | |
26 | continue; | |
27 | ||
28 | if (!snd_interval_test(c, formations[i].pcm)) | |
29 | continue; | |
30 | ||
31 | t.min = min(t.min, snd_bebob_rate_table[i]); | |
32 | t.max = max(t.max, snd_bebob_rate_table[i]); | |
33 | ||
34 | } | |
35 | return snd_interval_refine(r, &t); | |
36 | } | |
37 | ||
38 | static int | |
39 | hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) | |
40 | { | |
41 | struct snd_bebob_stream_formation *formations = rule->private; | |
42 | struct snd_interval *c = | |
43 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
44 | const struct snd_interval *r = | |
45 | hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); | |
46 | struct snd_interval t = { | |
47 | .min = UINT_MAX, .max = 0, .integer = 1 | |
48 | }; | |
49 | ||
50 | unsigned int i; | |
51 | ||
52 | for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) { | |
53 | /* entry is invalid */ | |
54 | if (formations[i].pcm == 0) | |
55 | continue; | |
56 | ||
57 | if (!snd_interval_test(r, snd_bebob_rate_table[i])) | |
58 | continue; | |
59 | ||
60 | t.min = min(t.min, formations[i].pcm); | |
61 | t.max = max(t.max, formations[i].pcm); | |
62 | } | |
63 | ||
64 | return snd_interval_refine(c, &t); | |
65 | } | |
66 | ||
67 | static void | |
68 | limit_channels_and_rates(struct snd_pcm_hardware *hw, | |
69 | struct snd_bebob_stream_formation *formations) | |
70 | { | |
71 | unsigned int i; | |
72 | ||
73 | hw->channels_min = UINT_MAX; | |
74 | hw->channels_max = 0; | |
75 | ||
76 | hw->rate_min = UINT_MAX; | |
77 | hw->rate_max = 0; | |
78 | hw->rates = 0; | |
79 | ||
80 | for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) { | |
81 | /* entry has no PCM channels */ | |
82 | if (formations[i].pcm == 0) | |
83 | continue; | |
84 | ||
85 | hw->channels_min = min(hw->channels_min, formations[i].pcm); | |
86 | hw->channels_max = max(hw->channels_max, formations[i].pcm); | |
87 | ||
88 | hw->rate_min = min(hw->rate_min, snd_bebob_rate_table[i]); | |
89 | hw->rate_max = max(hw->rate_max, snd_bebob_rate_table[i]); | |
90 | hw->rates |= snd_pcm_rate_to_rate_bit(snd_bebob_rate_table[i]); | |
91 | } | |
92 | } | |
93 | ||
fbbebd2c TS |
94 | static int |
95 | pcm_init_hw_params(struct snd_bebob *bebob, | |
96 | struct snd_pcm_substream *substream) | |
97 | { | |
98 | struct snd_pcm_runtime *runtime = substream->runtime; | |
99 | struct amdtp_stream *s; | |
100 | struct snd_bebob_stream_formation *formations; | |
101 | int err; | |
102 | ||
fbbebd2c | 103 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
49c7b3fc | 104 | runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS; |
fbbebd2c TS |
105 | s = &bebob->tx_stream; |
106 | formations = bebob->tx_stream_formations; | |
107 | } else { | |
49c7b3fc | 108 | runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS; |
fbbebd2c TS |
109 | s = &bebob->rx_stream; |
110 | formations = bebob->rx_stream_formations; | |
111 | } | |
112 | ||
113 | limit_channels_and_rates(&runtime->hw, formations); | |
fbbebd2c TS |
114 | |
115 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, | |
116 | hw_rule_channels, formations, | |
117 | SNDRV_PCM_HW_PARAM_RATE, -1); | |
118 | if (err < 0) | |
119 | goto end; | |
120 | ||
121 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
122 | hw_rule_rate, formations, | |
123 | SNDRV_PCM_HW_PARAM_CHANNELS, -1); | |
124 | if (err < 0) | |
125 | goto end; | |
126 | ||
bc8500da | 127 | err = amdtp_am824_add_pcm_hw_constraints(s, runtime); |
fbbebd2c TS |
128 | end: |
129 | return err; | |
130 | } | |
131 | ||
a80b29a4 | 132 | static int pcm_open(struct snd_pcm_substream *substream) |
fbbebd2c TS |
133 | { |
134 | struct snd_bebob *bebob = substream->private_data; | |
6b9866c8 | 135 | const struct snd_bebob_rate_spec *spec = bebob->spec->rate; |
a80b29a4 | 136 | struct amdtp_domain *d = &bebob->domain; |
3e254b16 | 137 | enum snd_bebob_clock_type src; |
fbbebd2c TS |
138 | int err; |
139 | ||
618eabea | 140 | err = snd_bebob_stream_lock_try(bebob); |
fbbebd2c | 141 | if (err < 0) |
a80b29a4 | 142 | return err; |
fbbebd2c | 143 | |
618eabea TS |
144 | err = pcm_init_hw_params(bebob, substream); |
145 | if (err < 0) | |
146 | goto err_locked; | |
147 | ||
3e254b16 | 148 | err = snd_bebob_stream_get_clock_src(bebob, &src); |
fbbebd2c | 149 | if (err < 0) |
618eabea | 150 | goto err_locked; |
fbbebd2c | 151 | |
a80b29a4 TS |
152 | mutex_lock(&bebob->mutex); |
153 | ||
154 | // When source of clock is not internal or any stream is reserved for | |
155 | // transmission of PCM frames, the available sampling rate is limited | |
156 | // at current one. | |
3e254b16 | 157 | if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL || |
a80b29a4 TS |
158 | (bebob->substreams_counter > 0 && d->events_per_period > 0)) { |
159 | unsigned int frames_per_period = d->events_per_period; | |
1fde7a44 | 160 | unsigned int frames_per_buffer = d->events_per_buffer; |
a80b29a4 TS |
161 | unsigned int sampling_rate; |
162 | ||
1fc9522a | 163 | err = spec->get(bebob, &sampling_rate); |
fbbebd2c | 164 | if (err < 0) { |
a80b29a4 | 165 | mutex_unlock(&bebob->mutex); |
fbbebd2c TS |
166 | dev_err(&bebob->unit->device, |
167 | "fail to get sampling rate: %d\n", err); | |
618eabea | 168 | goto err_locked; |
fbbebd2c TS |
169 | } |
170 | ||
171 | substream->runtime->hw.rate_min = sampling_rate; | |
172 | substream->runtime->hw.rate_max = sampling_rate; | |
a80b29a4 TS |
173 | |
174 | if (frames_per_period > 0) { | |
175 | err = snd_pcm_hw_constraint_minmax(substream->runtime, | |
176 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, | |
177 | frames_per_period, frames_per_period); | |
178 | if (err < 0) { | |
179 | mutex_unlock(&bebob->mutex); | |
180 | goto err_locked; | |
181 | } | |
1fde7a44 TS |
182 | |
183 | err = snd_pcm_hw_constraint_minmax(substream->runtime, | |
184 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
185 | frames_per_buffer, frames_per_buffer); | |
186 | if (err < 0) { | |
187 | mutex_unlock(&bebob->mutex); | |
188 | goto err_locked; | |
189 | } | |
a80b29a4 | 190 | } |
fbbebd2c TS |
191 | } |
192 | ||
a80b29a4 TS |
193 | mutex_unlock(&bebob->mutex); |
194 | ||
fbbebd2c | 195 | snd_pcm_set_sync(substream); |
a80b29a4 TS |
196 | |
197 | return 0; | |
618eabea TS |
198 | err_locked: |
199 | snd_bebob_stream_lock_release(bebob); | |
200 | return err; | |
fbbebd2c TS |
201 | } |
202 | ||
203 | static int | |
204 | pcm_close(struct snd_pcm_substream *substream) | |
205 | { | |
618eabea TS |
206 | struct snd_bebob *bebob = substream->private_data; |
207 | snd_bebob_stream_lock_release(bebob); | |
fbbebd2c TS |
208 | return 0; |
209 | } | |
210 | ||
f69fc179 TS |
211 | static int pcm_hw_params(struct snd_pcm_substream *substream, |
212 | struct snd_pcm_hw_params *hw_params) | |
fbbebd2c TS |
213 | { |
214 | struct snd_bebob *bebob = substream->private_data; | |
22c103cd TS |
215 | int err; |
216 | ||
217 | err = snd_pcm_lib_alloc_vmalloc_buffer(substream, | |
218 | params_buffer_bytes(hw_params)); | |
219 | if (err < 0) | |
220 | return err; | |
fbbebd2c | 221 | |
2a71e701 | 222 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
ac2888b9 | 223 | unsigned int rate = params_rate(hw_params); |
8737209f | 224 | unsigned int frames_per_period = params_period_size(hw_params); |
1fde7a44 | 225 | unsigned int frames_per_buffer = params_buffer_size(hw_params); |
ac2888b9 | 226 | |
2a71e701 | 227 | mutex_lock(&bebob->mutex); |
8737209f | 228 | err = snd_bebob_stream_reserve_duplex(bebob, rate, |
1fde7a44 | 229 | frames_per_period, frames_per_buffer); |
ac2888b9 TS |
230 | if (err >= 0) |
231 | ++bebob->substreams_counter; | |
2a71e701 TS |
232 | mutex_unlock(&bebob->mutex); |
233 | } | |
85130cb4 | 234 | |
ac2888b9 | 235 | return err; |
fbbebd2c TS |
236 | } |
237 | ||
f69fc179 | 238 | static int pcm_hw_free(struct snd_pcm_substream *substream) |
fbbebd2c TS |
239 | { |
240 | struct snd_bebob *bebob = substream->private_data; | |
241 | ||
5e818ac2 TS |
242 | mutex_lock(&bebob->mutex); |
243 | ||
244 | if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) | |
4fd6c6c7 | 245 | bebob->substreams_counter--; |
fbbebd2c TS |
246 | |
247 | snd_bebob_stream_stop_duplex(bebob); | |
248 | ||
5e818ac2 TS |
249 | mutex_unlock(&bebob->mutex); |
250 | ||
fbbebd2c TS |
251 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
252 | } | |
253 | ||
254 | static int | |
255 | pcm_capture_prepare(struct snd_pcm_substream *substream) | |
256 | { | |
257 | struct snd_bebob *bebob = substream->private_data; | |
fbbebd2c TS |
258 | int err; |
259 | ||
ac2888b9 | 260 | err = snd_bebob_stream_start_duplex(bebob); |
fbbebd2c TS |
261 | if (err >= 0) |
262 | amdtp_stream_pcm_prepare(&bebob->tx_stream); | |
263 | ||
264 | return err; | |
265 | } | |
266 | static int | |
267 | pcm_playback_prepare(struct snd_pcm_substream *substream) | |
268 | { | |
269 | struct snd_bebob *bebob = substream->private_data; | |
fbbebd2c TS |
270 | int err; |
271 | ||
ac2888b9 | 272 | err = snd_bebob_stream_start_duplex(bebob); |
fbbebd2c TS |
273 | if (err >= 0) |
274 | amdtp_stream_pcm_prepare(&bebob->rx_stream); | |
275 | ||
276 | return err; | |
277 | } | |
278 | ||
279 | static int | |
280 | pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd) | |
281 | { | |
282 | struct snd_bebob *bebob = substream->private_data; | |
283 | ||
284 | switch (cmd) { | |
285 | case SNDRV_PCM_TRIGGER_START: | |
286 | amdtp_stream_pcm_trigger(&bebob->tx_stream, substream); | |
287 | break; | |
288 | case SNDRV_PCM_TRIGGER_STOP: | |
289 | amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL); | |
290 | break; | |
291 | default: | |
292 | return -EINVAL; | |
293 | } | |
294 | ||
295 | return 0; | |
296 | } | |
297 | static int | |
298 | pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd) | |
299 | { | |
300 | struct snd_bebob *bebob = substream->private_data; | |
301 | ||
302 | switch (cmd) { | |
303 | case SNDRV_PCM_TRIGGER_START: | |
304 | amdtp_stream_pcm_trigger(&bebob->rx_stream, substream); | |
305 | break; | |
306 | case SNDRV_PCM_TRIGGER_STOP: | |
307 | amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL); | |
308 | break; | |
309 | default: | |
310 | return -EINVAL; | |
311 | } | |
312 | ||
313 | return 0; | |
314 | } | |
315 | ||
316 | static snd_pcm_uframes_t | |
317 | pcm_capture_pointer(struct snd_pcm_substream *sbstrm) | |
318 | { | |
319 | struct snd_bebob *bebob = sbstrm->private_data; | |
320 | return amdtp_stream_pcm_pointer(&bebob->tx_stream); | |
321 | } | |
322 | static snd_pcm_uframes_t | |
323 | pcm_playback_pointer(struct snd_pcm_substream *sbstrm) | |
324 | { | |
325 | struct snd_bebob *bebob = sbstrm->private_data; | |
326 | return amdtp_stream_pcm_pointer(&bebob->rx_stream); | |
327 | } | |
328 | ||
875becf8 TS |
329 | static int pcm_capture_ack(struct snd_pcm_substream *substream) |
330 | { | |
331 | struct snd_bebob *bebob = substream->private_data; | |
332 | ||
333 | return amdtp_stream_pcm_ack(&bebob->tx_stream); | |
334 | } | |
335 | ||
336 | static int pcm_playback_ack(struct snd_pcm_substream *substream) | |
337 | { | |
338 | struct snd_bebob *bebob = substream->private_data; | |
339 | ||
340 | return amdtp_stream_pcm_ack(&bebob->rx_stream); | |
341 | } | |
342 | ||
fbbebd2c TS |
343 | int snd_bebob_create_pcm_devices(struct snd_bebob *bebob) |
344 | { | |
4780f774 TS |
345 | static const struct snd_pcm_ops capture_ops = { |
346 | .open = pcm_open, | |
347 | .close = pcm_close, | |
348 | .ioctl = snd_pcm_lib_ioctl, | |
f69fc179 TS |
349 | .hw_params = pcm_hw_params, |
350 | .hw_free = pcm_hw_free, | |
4780f774 TS |
351 | .prepare = pcm_capture_prepare, |
352 | .trigger = pcm_capture_trigger, | |
353 | .pointer = pcm_capture_pointer, | |
875becf8 | 354 | .ack = pcm_capture_ack, |
4780f774 TS |
355 | .page = snd_pcm_lib_get_vmalloc_page, |
356 | }; | |
357 | static const struct snd_pcm_ops playback_ops = { | |
358 | .open = pcm_open, | |
359 | .close = pcm_close, | |
360 | .ioctl = snd_pcm_lib_ioctl, | |
f69fc179 TS |
361 | .hw_params = pcm_hw_params, |
362 | .hw_free = pcm_hw_free, | |
4780f774 TS |
363 | .prepare = pcm_playback_prepare, |
364 | .trigger = pcm_playback_trigger, | |
365 | .pointer = pcm_playback_pointer, | |
875becf8 | 366 | .ack = pcm_playback_ack, |
4780f774 | 367 | .page = snd_pcm_lib_get_vmalloc_page, |
4780f774 | 368 | }; |
fbbebd2c TS |
369 | struct snd_pcm *pcm; |
370 | int err; | |
371 | ||
372 | err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm); | |
373 | if (err < 0) | |
374 | goto end; | |
375 | ||
376 | pcm->private_data = bebob; | |
377 | snprintf(pcm->name, sizeof(pcm->name), | |
378 | "%s PCM", bebob->card->shortname); | |
4780f774 TS |
379 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); |
380 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops); | |
fbbebd2c TS |
381 | end: |
382 | return err; | |
383 | } |