Commit | Line | Data |
---|---|---|
c50fb91f TS |
1 | /* |
2 | * dice_pcm.c - a part of driver for DICE based devices | |
3 | * | |
4 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> | |
5 | * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp> | |
6 | * | |
7 | * Licensed under the terms of the GNU General Public License, version 2. | |
8 | */ | |
9 | ||
10 | #include "dice.h" | |
11 | ||
0d5ee195 TS |
12 | static int limit_channels_and_rates(struct snd_dice *dice, |
13 | struct snd_pcm_runtime *runtime, | |
4bdc495c TS |
14 | enum amdtp_stream_direction dir, |
15 | unsigned int index, unsigned int size) | |
c50fb91f | 16 | { |
2c2416c8 | 17 | struct snd_pcm_hardware *hw = &runtime->hw; |
4bdc495c | 18 | struct amdtp_stream *stream; |
0d5ee195 | 19 | unsigned int rate; |
4bdc495c | 20 | __be32 reg; |
0d5ee195 | 21 | int err; |
c50fb91f | 22 | |
0d5ee195 TS |
23 | /* |
24 | * Retrieve current Multi Bit Linear Audio data channel and limit to | |
25 | * it. | |
26 | */ | |
4bdc495c TS |
27 | if (dir == AMDTP_IN_STREAM) { |
28 | stream = &dice->tx_stream[index]; | |
29 | err = snd_dice_transaction_read_tx(dice, | |
30 | size * index + TX_NUMBER_AUDIO, | |
31 | ®, sizeof(reg)); | |
0d5ee195 | 32 | } else { |
4bdc495c TS |
33 | stream = &dice->rx_stream[index]; |
34 | err = snd_dice_transaction_read_rx(dice, | |
35 | size * index + RX_NUMBER_AUDIO, | |
36 | ®, sizeof(reg)); | |
0d5ee195 TS |
37 | } |
38 | if (err < 0) | |
39 | return err; | |
c50fb91f | 40 | |
4bdc495c | 41 | hw->channels_min = hw->channels_max = be32_to_cpu(reg); |
2c2416c8 | 42 | |
0d5ee195 TS |
43 | /* Retrieve current sampling transfer frequency and limit to it. */ |
44 | err = snd_dice_transaction_get_rate(dice, &rate); | |
45 | if (err < 0) | |
46 | return err; | |
2c2416c8 | 47 | |
0d5ee195 | 48 | hw->rates = snd_pcm_rate_to_rate_bit(rate); |
c50fb91f | 49 | snd_pcm_limit_hw_rates(runtime); |
0d5ee195 TS |
50 | |
51 | return 0; | |
2c2416c8 | 52 | } |
c50fb91f | 53 | |
2c2416c8 TS |
54 | static void limit_period_and_buffer(struct snd_pcm_hardware *hw) |
55 | { | |
56 | hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */ | |
57 | hw->periods_max = UINT_MAX; | |
58 | ||
59 | hw->period_bytes_min = 4 * hw->channels_max; /* byte for a frame */ | |
60 | ||
61 | /* Just to prevent from allocating much pages. */ | |
62 | hw->period_bytes_max = hw->period_bytes_min * 2048; | |
63 | hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min; | |
64 | } | |
65 | ||
66 | static int init_hw_info(struct snd_dice *dice, | |
67 | struct snd_pcm_substream *substream) | |
68 | { | |
69 | struct snd_pcm_runtime *runtime = substream->runtime; | |
70 | struct snd_pcm_hardware *hw = &runtime->hw; | |
4bdc495c | 71 | enum amdtp_stream_direction dir; |
69dcf3e4 | 72 | struct amdtp_stream *stream; |
4bdc495c TS |
73 | __be32 reg[2]; |
74 | unsigned int count, size; | |
2c2416c8 TS |
75 | int err; |
76 | ||
77 | hw->info = SNDRV_PCM_INFO_MMAP | | |
78 | SNDRV_PCM_INFO_MMAP_VALID | | |
79 | SNDRV_PCM_INFO_BATCH | | |
80 | SNDRV_PCM_INFO_INTERLEAVED | | |
69dcf3e4 | 81 | SNDRV_PCM_INFO_JOINT_DUPLEX | |
2c2416c8 | 82 | SNDRV_PCM_INFO_BLOCK_TRANSFER; |
2c2416c8 | 83 | |
69dcf3e4 | 84 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
49c7b3fc | 85 | hw->formats = AM824_IN_PCM_FORMAT_BITS; |
4bdc495c TS |
86 | dir = AMDTP_IN_STREAM; |
87 | stream = &dice->tx_stream[substream->pcm->device]; | |
88 | err = snd_dice_transaction_read_tx(dice, TX_NUMBER, reg, | |
89 | sizeof(reg)); | |
69dcf3e4 | 90 | } else { |
49c7b3fc | 91 | hw->formats = AM824_OUT_PCM_FORMAT_BITS; |
4bdc495c TS |
92 | dir = AMDTP_OUT_STREAM; |
93 | stream = &dice->rx_stream[substream->pcm->device]; | |
94 | err = snd_dice_transaction_read_rx(dice, RX_NUMBER, reg, | |
95 | sizeof(reg)); | |
69dcf3e4 TS |
96 | } |
97 | ||
4bdc495c TS |
98 | if (err < 0) |
99 | return err; | |
100 | ||
101 | count = min_t(unsigned int, be32_to_cpu(reg[0]), MAX_STREAMS); | |
102 | if (substream->pcm->device >= count) | |
103 | return -ENXIO; | |
104 | ||
105 | size = be32_to_cpu(reg[1]) * 4; | |
106 | err = limit_channels_and_rates(dice, substream->runtime, dir, | |
107 | substream->pcm->device, size); | |
c50fb91f | 108 | if (err < 0) |
0d5ee195 TS |
109 | return err; |
110 | limit_period_and_buffer(hw); | |
c50fb91f | 111 | |
0d5ee195 | 112 | return amdtp_am824_add_pcm_hw_constraints(stream, runtime); |
2c2416c8 | 113 | } |
c50fb91f | 114 | |
2c2416c8 TS |
115 | static int pcm_open(struct snd_pcm_substream *substream) |
116 | { | |
117 | struct snd_dice *dice = substream->private_data; | |
118 | int err; | |
c50fb91f | 119 | |
2c2416c8 TS |
120 | err = snd_dice_stream_lock_try(dice); |
121 | if (err < 0) | |
122 | goto end; | |
123 | ||
124 | err = init_hw_info(dice, substream); | |
125 | if (err < 0) | |
126 | goto err_locked; | |
8fc01fc0 | 127 | |
8fc01fc0 | 128 | snd_pcm_set_sync(substream); |
2c2416c8 TS |
129 | end: |
130 | return err; | |
131 | err_locked: | |
c50fb91f | 132 | snd_dice_stream_lock_release(dice); |
c50fb91f TS |
133 | return err; |
134 | } | |
135 | ||
136 | static int pcm_close(struct snd_pcm_substream *substream) | |
137 | { | |
138 | struct snd_dice *dice = substream->private_data; | |
139 | ||
140 | snd_dice_stream_lock_release(dice); | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
69dcf3e4 TS |
145 | static int capture_hw_params(struct snd_pcm_substream *substream, |
146 | struct snd_pcm_hw_params *hw_params) | |
147 | { | |
148 | struct snd_dice *dice = substream->private_data; | |
22c103cd TS |
149 | int err; |
150 | ||
151 | err = snd_pcm_lib_alloc_vmalloc_buffer(substream, | |
152 | params_buffer_bytes(hw_params)); | |
153 | if (err < 0) | |
154 | return err; | |
69dcf3e4 TS |
155 | |
156 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { | |
157 | mutex_lock(&dice->mutex); | |
158 | dice->substreams_counter++; | |
159 | mutex_unlock(&dice->mutex); | |
160 | } | |
161 | ||
22c103cd | 162 | return 0; |
69dcf3e4 | 163 | } |
c50fb91f TS |
164 | static int playback_hw_params(struct snd_pcm_substream *substream, |
165 | struct snd_pcm_hw_params *hw_params) | |
166 | { | |
167 | struct snd_dice *dice = substream->private_data; | |
22c103cd TS |
168 | int err; |
169 | ||
170 | err = snd_pcm_lib_alloc_vmalloc_buffer(substream, | |
171 | params_buffer_bytes(hw_params)); | |
172 | if (err < 0) | |
173 | return err; | |
69dcf3e4 TS |
174 | |
175 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { | |
176 | mutex_lock(&dice->mutex); | |
177 | dice->substreams_counter++; | |
178 | mutex_unlock(&dice->mutex); | |
179 | } | |
180 | ||
22c103cd | 181 | return 0; |
c50fb91f TS |
182 | } |
183 | ||
69dcf3e4 TS |
184 | static int capture_hw_free(struct snd_pcm_substream *substream) |
185 | { | |
186 | struct snd_dice *dice = substream->private_data; | |
187 | ||
188 | mutex_lock(&dice->mutex); | |
189 | ||
190 | if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) | |
191 | dice->substreams_counter--; | |
192 | ||
193 | snd_dice_stream_stop_duplex(dice); | |
194 | ||
195 | mutex_unlock(&dice->mutex); | |
196 | ||
197 | return snd_pcm_lib_free_vmalloc_buffer(substream); | |
198 | } | |
199 | ||
c50fb91f TS |
200 | static int playback_hw_free(struct snd_pcm_substream *substream) |
201 | { | |
202 | struct snd_dice *dice = substream->private_data; | |
203 | ||
204 | mutex_lock(&dice->mutex); | |
69dcf3e4 TS |
205 | |
206 | if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) | |
207 | dice->substreams_counter--; | |
208 | ||
9a02843c | 209 | snd_dice_stream_stop_duplex(dice); |
69dcf3e4 | 210 | |
c50fb91f TS |
211 | mutex_unlock(&dice->mutex); |
212 | ||
213 | return snd_pcm_lib_free_vmalloc_buffer(substream); | |
214 | } | |
215 | ||
69dcf3e4 TS |
216 | static int capture_prepare(struct snd_pcm_substream *substream) |
217 | { | |
218 | struct snd_dice *dice = substream->private_data; | |
4bdc495c | 219 | struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; |
69dcf3e4 TS |
220 | int err; |
221 | ||
222 | mutex_lock(&dice->mutex); | |
223 | err = snd_dice_stream_start_duplex(dice, substream->runtime->rate); | |
224 | mutex_unlock(&dice->mutex); | |
225 | if (err >= 0) | |
8ae25b76 | 226 | amdtp_stream_pcm_prepare(stream); |
69dcf3e4 TS |
227 | |
228 | return 0; | |
229 | } | |
c50fb91f TS |
230 | static int playback_prepare(struct snd_pcm_substream *substream) |
231 | { | |
232 | struct snd_dice *dice = substream->private_data; | |
4bdc495c | 233 | struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; |
c50fb91f TS |
234 | int err; |
235 | ||
236 | mutex_lock(&dice->mutex); | |
9a02843c | 237 | err = snd_dice_stream_start_duplex(dice, substream->runtime->rate); |
c50fb91f | 238 | mutex_unlock(&dice->mutex); |
288a8d0c | 239 | if (err >= 0) |
8ae25b76 | 240 | amdtp_stream_pcm_prepare(stream); |
c50fb91f | 241 | |
288a8d0c | 242 | return err; |
c50fb91f TS |
243 | } |
244 | ||
69dcf3e4 TS |
245 | static int capture_trigger(struct snd_pcm_substream *substream, int cmd) |
246 | { | |
247 | struct snd_dice *dice = substream->private_data; | |
4bdc495c | 248 | struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; |
69dcf3e4 TS |
249 | |
250 | switch (cmd) { | |
251 | case SNDRV_PCM_TRIGGER_START: | |
8ae25b76 | 252 | amdtp_stream_pcm_trigger(stream, substream); |
69dcf3e4 TS |
253 | break; |
254 | case SNDRV_PCM_TRIGGER_STOP: | |
8ae25b76 | 255 | amdtp_stream_pcm_trigger(stream, NULL); |
69dcf3e4 TS |
256 | break; |
257 | default: | |
258 | return -EINVAL; | |
259 | } | |
260 | ||
261 | return 0; | |
262 | } | |
c50fb91f TS |
263 | static int playback_trigger(struct snd_pcm_substream *substream, int cmd) |
264 | { | |
265 | struct snd_dice *dice = substream->private_data; | |
4bdc495c | 266 | struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; |
c50fb91f TS |
267 | |
268 | switch (cmd) { | |
269 | case SNDRV_PCM_TRIGGER_START: | |
8ae25b76 | 270 | amdtp_stream_pcm_trigger(stream, substream); |
c50fb91f TS |
271 | break; |
272 | case SNDRV_PCM_TRIGGER_STOP: | |
8ae25b76 | 273 | amdtp_stream_pcm_trigger(stream, NULL); |
c50fb91f TS |
274 | break; |
275 | default: | |
276 | return -EINVAL; | |
277 | } | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
69dcf3e4 TS |
282 | static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream) |
283 | { | |
284 | struct snd_dice *dice = substream->private_data; | |
4bdc495c | 285 | struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; |
69dcf3e4 | 286 | |
8ae25b76 | 287 | return amdtp_stream_pcm_pointer(stream); |
69dcf3e4 | 288 | } |
c50fb91f TS |
289 | static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream) |
290 | { | |
291 | struct snd_dice *dice = substream->private_data; | |
4bdc495c | 292 | struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; |
c50fb91f | 293 | |
8ae25b76 | 294 | return amdtp_stream_pcm_pointer(stream); |
c50fb91f TS |
295 | } |
296 | ||
297 | int snd_dice_create_pcm(struct snd_dice *dice) | |
298 | { | |
5116ffc3 | 299 | static const struct snd_pcm_ops capture_ops = { |
69dcf3e4 TS |
300 | .open = pcm_open, |
301 | .close = pcm_close, | |
302 | .ioctl = snd_pcm_lib_ioctl, | |
303 | .hw_params = capture_hw_params, | |
304 | .hw_free = capture_hw_free, | |
305 | .prepare = capture_prepare, | |
306 | .trigger = capture_trigger, | |
307 | .pointer = capture_pointer, | |
308 | .page = snd_pcm_lib_get_vmalloc_page, | |
309 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
310 | }; | |
5116ffc3 | 311 | static const struct snd_pcm_ops playback_ops = { |
c50fb91f TS |
312 | .open = pcm_open, |
313 | .close = pcm_close, | |
314 | .ioctl = snd_pcm_lib_ioctl, | |
315 | .hw_params = playback_hw_params, | |
316 | .hw_free = playback_hw_free, | |
317 | .prepare = playback_prepare, | |
318 | .trigger = playback_trigger, | |
319 | .pointer = playback_pointer, | |
320 | .page = snd_pcm_lib_get_vmalloc_page, | |
321 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
322 | }; | |
c3007656 | 323 | __be32 reg; |
c50fb91f | 324 | struct snd_pcm *pcm; |
4bdc495c | 325 | unsigned int i, max_capture, max_playback, capture, playback; |
c50fb91f TS |
326 | int err; |
327 | ||
4bdc495c | 328 | /* Check whether PCM substreams are required. */ |
7cafc65b TS |
329 | if (dice->force_two_pcms) { |
330 | max_capture = max_playback = 2; | |
331 | } else { | |
332 | max_capture = max_playback = 0; | |
333 | err = snd_dice_transaction_read_tx(dice, TX_NUMBER, ®, | |
334 | sizeof(reg)); | |
335 | if (err < 0) | |
336 | return err; | |
337 | max_capture = min_t(unsigned int, be32_to_cpu(reg), MAX_STREAMS); | |
69dcf3e4 | 338 | |
7cafc65b TS |
339 | err = snd_dice_transaction_read_rx(dice, RX_NUMBER, ®, |
340 | sizeof(reg)); | |
341 | if (err < 0) | |
342 | return err; | |
343 | max_playback = min_t(unsigned int, be32_to_cpu(reg), MAX_STREAMS); | |
344 | } | |
4bdc495c TS |
345 | |
346 | for (i = 0; i < MAX_STREAMS; i++) { | |
347 | capture = playback = 0; | |
348 | if (i < max_capture) | |
349 | capture = 1; | |
350 | if (i < max_playback) | |
351 | playback = 1; | |
352 | if (capture == 0 && playback == 0) | |
353 | break; | |
354 | ||
355 | err = snd_pcm_new(dice->card, "DICE", i, playback, capture, | |
356 | &pcm); | |
357 | if (err < 0) | |
358 | return err; | |
359 | pcm->private_data = dice; | |
360 | strcpy(pcm->name, dice->card->shortname); | |
361 | ||
362 | if (capture > 0) | |
363 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, | |
364 | &capture_ops); | |
365 | ||
366 | if (playback > 0) | |
367 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, | |
368 | &playback_ops); | |
369 | } | |
c50fb91f TS |
370 | |
371 | return 0; | |
372 | } |