cea26d5eff1a5898499b12a05f30a0b49234334b
[linux-2.6-block.git] / sound / firewire / tascam / tascam-pcm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tascam-pcm.c - a part of driver for TASCAM FireWire series
4  *
5  * Copyright (c) 2015 Takashi Sakamoto
6  */
7
8 #include "tascam.h"
9
10 static int pcm_init_hw_params(struct snd_tscm *tscm,
11                               struct snd_pcm_substream *substream)
12 {
13         struct snd_pcm_runtime *runtime = substream->runtime;
14         struct snd_pcm_hardware *hw = &runtime->hw;
15         struct amdtp_stream *stream;
16         unsigned int pcm_channels;
17
18         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
19                 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
20                 stream = &tscm->tx_stream;
21                 pcm_channels = tscm->spec->pcm_capture_analog_channels;
22         } else {
23                 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
24                 stream = &tscm->rx_stream;
25                 pcm_channels = tscm->spec->pcm_playback_analog_channels;
26         }
27
28         if (tscm->spec->has_adat)
29                 pcm_channels += 8;
30         if (tscm->spec->has_spdif)
31                 pcm_channels += 2;
32         runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels;
33
34         hw->rates = SNDRV_PCM_RATE_44100 |
35                     SNDRV_PCM_RATE_48000 |
36                     SNDRV_PCM_RATE_88200 |
37                     SNDRV_PCM_RATE_96000;
38         snd_pcm_limit_hw_rates(runtime);
39
40         return amdtp_tscm_add_pcm_hw_constraints(stream, runtime);
41 }
42
43 static int pcm_open(struct snd_pcm_substream *substream)
44 {
45         struct snd_tscm *tscm = substream->private_data;
46         enum snd_tscm_clock clock;
47         unsigned int rate;
48         int err;
49
50         err = snd_tscm_stream_lock_try(tscm);
51         if (err < 0)
52                 goto end;
53
54         err = pcm_init_hw_params(tscm, substream);
55         if (err < 0)
56                 goto err_locked;
57
58         err = snd_tscm_stream_get_clock(tscm, &clock);
59         if (err < 0)
60                 goto err_locked;
61
62         if (clock != SND_TSCM_CLOCK_INTERNAL ||
63             amdtp_stream_pcm_running(&tscm->rx_stream) ||
64             amdtp_stream_pcm_running(&tscm->tx_stream)) {
65                 err = snd_tscm_stream_get_rate(tscm, &rate);
66                 if (err < 0)
67                         goto err_locked;
68                 substream->runtime->hw.rate_min = rate;
69                 substream->runtime->hw.rate_max = rate;
70         }
71
72         snd_pcm_set_sync(substream);
73 end:
74         return err;
75 err_locked:
76         snd_tscm_stream_lock_release(tscm);
77         return err;
78 }
79
80 static int pcm_close(struct snd_pcm_substream *substream)
81 {
82         struct snd_tscm *tscm = substream->private_data;
83
84         snd_tscm_stream_lock_release(tscm);
85
86         return 0;
87 }
88
89 static int pcm_hw_params(struct snd_pcm_substream *substream,
90                          struct snd_pcm_hw_params *hw_params)
91 {
92         struct snd_tscm *tscm = substream->private_data;
93         int err;
94
95         err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
96                                                params_buffer_bytes(hw_params));
97         if (err < 0)
98                 return err;
99
100         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
101                 unsigned int rate = params_rate(hw_params);
102                 unsigned int frames_per_period = params_period_size(hw_params);
103
104                 mutex_lock(&tscm->mutex);
105                 err = snd_tscm_stream_reserve_duplex(tscm, rate,
106                                                      frames_per_period);
107                 if (err >= 0)
108                         ++tscm->substreams_counter;
109                 mutex_unlock(&tscm->mutex);
110         }
111
112         return err;
113 }
114
115 static int pcm_hw_free(struct snd_pcm_substream *substream)
116 {
117         struct snd_tscm *tscm = substream->private_data;
118
119         mutex_lock(&tscm->mutex);
120
121         if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
122                 --tscm->substreams_counter;
123
124         snd_tscm_stream_stop_duplex(tscm);
125
126         mutex_unlock(&tscm->mutex);
127
128         return snd_pcm_lib_free_vmalloc_buffer(substream);
129 }
130
131 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
132 {
133         struct snd_tscm *tscm = substream->private_data;
134         struct snd_pcm_runtime *runtime = substream->runtime;
135         int err;
136
137         mutex_lock(&tscm->mutex);
138
139         err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
140         if (err >= 0)
141                 amdtp_stream_pcm_prepare(&tscm->tx_stream);
142
143         mutex_unlock(&tscm->mutex);
144
145         return err;
146 }
147
148 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
149 {
150         struct snd_tscm *tscm = substream->private_data;
151         struct snd_pcm_runtime *runtime = substream->runtime;
152         int err;
153
154         mutex_lock(&tscm->mutex);
155
156         err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
157         if (err >= 0)
158                 amdtp_stream_pcm_prepare(&tscm->rx_stream);
159
160         mutex_unlock(&tscm->mutex);
161
162         return err;
163 }
164
165 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
166 {
167         struct snd_tscm *tscm = substream->private_data;
168
169         switch (cmd) {
170         case SNDRV_PCM_TRIGGER_START:
171                 amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
172                 break;
173         case SNDRV_PCM_TRIGGER_STOP:
174                 amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
175                 break;
176         default:
177                 return -EINVAL;
178         }
179
180         return 0;
181 }
182
183 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
184 {
185         struct snd_tscm *tscm = substream->private_data;
186
187         switch (cmd) {
188         case SNDRV_PCM_TRIGGER_START:
189                 amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
190                 break;
191         case SNDRV_PCM_TRIGGER_STOP:
192                 amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
193                 break;
194         default:
195                 return -EINVAL;
196         }
197
198         return 0;
199 }
200
201 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
202 {
203         struct snd_tscm *tscm = sbstrm->private_data;
204
205         return amdtp_stream_pcm_pointer(&tscm->tx_stream);
206 }
207
208 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
209 {
210         struct snd_tscm *tscm = sbstrm->private_data;
211
212         return amdtp_stream_pcm_pointer(&tscm->rx_stream);
213 }
214
215 static int pcm_capture_ack(struct snd_pcm_substream *substream)
216 {
217         struct snd_tscm *tscm = substream->private_data;
218
219         return amdtp_stream_pcm_ack(&tscm->tx_stream);
220 }
221
222 static int pcm_playback_ack(struct snd_pcm_substream *substream)
223 {
224         struct snd_tscm *tscm = substream->private_data;
225
226         return amdtp_stream_pcm_ack(&tscm->rx_stream);
227 }
228
229 int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
230 {
231         static const struct snd_pcm_ops capture_ops = {
232                 .open           = pcm_open,
233                 .close          = pcm_close,
234                 .ioctl          = snd_pcm_lib_ioctl,
235                 .hw_params      = pcm_hw_params,
236                 .hw_free        = pcm_hw_free,
237                 .prepare        = pcm_capture_prepare,
238                 .trigger        = pcm_capture_trigger,
239                 .pointer        = pcm_capture_pointer,
240                 .ack            = pcm_capture_ack,
241                 .page           = snd_pcm_lib_get_vmalloc_page,
242         };
243         static const struct snd_pcm_ops playback_ops = {
244                 .open           = pcm_open,
245                 .close          = pcm_close,
246                 .ioctl          = snd_pcm_lib_ioctl,
247                 .hw_params      = pcm_hw_params,
248                 .hw_free        = pcm_hw_free,
249                 .prepare        = pcm_playback_prepare,
250                 .trigger        = pcm_playback_trigger,
251                 .pointer        = pcm_playback_pointer,
252                 .ack            = pcm_playback_ack,
253                 .page           = snd_pcm_lib_get_vmalloc_page,
254         };
255         struct snd_pcm *pcm;
256         int err;
257
258         err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
259         if (err < 0)
260                 return err;
261
262         pcm->private_data = tscm;
263         snprintf(pcm->name, sizeof(pcm->name),
264                  "%s PCM", tscm->card->shortname);
265         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
266         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
267
268         return 0;
269 }