2 * motu-pcm.c - a part of driver for MOTU FireWire series
4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 * Licensed under the terms of the GNU General Public License, version 2.
9 #include <sound/pcm_params.h>
12 static int motu_rate_constraint(struct snd_pcm_hw_params
*params
,
13 struct snd_pcm_hw_rule
*rule
)
15 struct snd_motu_packet_format
*formats
= rule
->private;
17 const struct snd_interval
*c
=
18 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
19 struct snd_interval
*r
=
20 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
21 struct snd_interval rates
= {
22 .min
= UINT_MAX
, .max
= 0, .integer
= 1
24 unsigned int i
, pcm_channels
, rate
, mode
;
26 for (i
= 0; i
< ARRAY_SIZE(snd_motu_clock_rates
); ++i
) {
27 rate
= snd_motu_clock_rates
[i
];
30 pcm_channels
= formats
->fixed_part_pcm_chunks
[mode
] +
31 formats
->differed_part_pcm_chunks
[mode
];
32 if (!snd_interval_test(c
, pcm_channels
))
35 rates
.min
= min(rates
.min
, rate
);
36 rates
.max
= max(rates
.max
, rate
);
39 return snd_interval_refine(r
, &rates
);
42 static int motu_channels_constraint(struct snd_pcm_hw_params
*params
,
43 struct snd_pcm_hw_rule
*rule
)
45 struct snd_motu_packet_format
*formats
= rule
->private;
47 const struct snd_interval
*r
=
48 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_RATE
);
49 struct snd_interval
*c
=
50 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
51 struct snd_interval channels
= {
52 .min
= UINT_MAX
, .max
= 0, .integer
= 1
54 unsigned int i
, pcm_channels
, rate
, mode
;
56 for (i
= 0; i
< ARRAY_SIZE(snd_motu_clock_rates
); ++i
) {
57 rate
= snd_motu_clock_rates
[i
];
60 if (!snd_interval_test(r
, rate
))
63 pcm_channels
= formats
->fixed_part_pcm_chunks
[mode
] +
64 formats
->differed_part_pcm_chunks
[mode
];
65 channels
.min
= min(channels
.min
, pcm_channels
);
66 channels
.max
= max(channels
.max
, pcm_channels
);
69 return snd_interval_refine(c
, &channels
);
72 static void limit_channels_and_rates(struct snd_motu
*motu
,
73 struct snd_pcm_runtime
*runtime
,
74 struct snd_motu_packet_format
*formats
)
76 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
77 unsigned int i
, pcm_channels
, rate
, mode
;
79 hw
->channels_min
= UINT_MAX
;
82 for (i
= 0; i
< ARRAY_SIZE(snd_motu_clock_rates
); ++i
) {
83 rate
= snd_motu_clock_rates
[i
];
86 pcm_channels
= formats
->fixed_part_pcm_chunks
[mode
] +
87 formats
->differed_part_pcm_chunks
[mode
];
88 if (pcm_channels
== 0)
91 hw
->rates
|= snd_pcm_rate_to_rate_bit(rate
);
92 hw
->channels_min
= min(hw
->channels_min
, pcm_channels
);
93 hw
->channels_max
= max(hw
->channels_max
, pcm_channels
);
96 snd_pcm_limit_hw_rates(runtime
);
99 static int init_hw_info(struct snd_motu
*motu
,
100 struct snd_pcm_substream
*substream
)
102 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
103 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
104 struct amdtp_stream
*stream
;
105 struct snd_motu_packet_format
*formats
;
108 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
109 hw
->formats
= SNDRV_PCM_FMTBIT_S32
;
110 stream
= &motu
->tx_stream
;
111 formats
= &motu
->tx_packet_formats
;
113 hw
->formats
= SNDRV_PCM_FMTBIT_S32
;
114 stream
= &motu
->rx_stream
;
115 formats
= &motu
->rx_packet_formats
;
118 limit_channels_and_rates(motu
, runtime
, formats
);
120 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
121 motu_rate_constraint
, formats
,
122 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
125 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
126 motu_channels_constraint
, formats
,
127 SNDRV_PCM_HW_PARAM_RATE
, -1);
131 return amdtp_motu_add_pcm_hw_constraints(stream
, runtime
);
134 static int pcm_open(struct snd_pcm_substream
*substream
)
136 struct snd_motu
*motu
= substream
->private_data
;
137 const struct snd_motu_protocol
*const protocol
= motu
->spec
->protocol
;
138 enum snd_motu_clock_source src
;
142 err
= snd_motu_stream_lock_try(motu
);
146 mutex_lock(&motu
->mutex
);
148 err
= snd_motu_stream_cache_packet_formats(motu
);
152 err
= init_hw_info(motu
, substream
);
157 * When source of clock is not internal or any PCM streams are running,
158 * available sampling rate is limited at current sampling rate.
160 err
= protocol
->get_clock_source(motu
, &src
);
163 if (src
!= SND_MOTU_CLOCK_SOURCE_INTERNAL
||
164 amdtp_stream_pcm_running(&motu
->tx_stream
) ||
165 amdtp_stream_pcm_running(&motu
->rx_stream
)) {
166 err
= protocol
->get_clock_rate(motu
, &rate
);
169 substream
->runtime
->hw
.rate_min
= rate
;
170 substream
->runtime
->hw
.rate_max
= rate
;
173 snd_pcm_set_sync(substream
);
175 mutex_unlock(&motu
->mutex
);
179 mutex_unlock(&motu
->mutex
);
180 snd_motu_stream_lock_release(motu
);
184 static int pcm_close(struct snd_pcm_substream
*substream
)
186 struct snd_motu
*motu
= substream
->private_data
;
188 snd_motu_stream_lock_release(motu
);
193 static int capture_hw_params(struct snd_pcm_substream
*substream
,
194 struct snd_pcm_hw_params
*hw_params
)
196 struct snd_motu
*motu
= substream
->private_data
;
199 err
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
200 params_buffer_bytes(hw_params
));
204 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
205 mutex_lock(&motu
->mutex
);
206 motu
->capture_substreams
++;
207 mutex_unlock(&motu
->mutex
);
212 static int playback_hw_params(struct snd_pcm_substream
*substream
,
213 struct snd_pcm_hw_params
*hw_params
)
215 struct snd_motu
*motu
= substream
->private_data
;
218 err
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
219 params_buffer_bytes(hw_params
));
223 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
224 mutex_lock(&motu
->mutex
);
225 motu
->playback_substreams
++;
226 mutex_unlock(&motu
->mutex
);
232 static int capture_hw_free(struct snd_pcm_substream
*substream
)
234 struct snd_motu
*motu
= substream
->private_data
;
236 mutex_lock(&motu
->mutex
);
238 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
239 motu
->capture_substreams
--;
241 snd_motu_stream_stop_duplex(motu
);
243 mutex_unlock(&motu
->mutex
);
245 return snd_pcm_lib_free_vmalloc_buffer(substream
);
248 static int playback_hw_free(struct snd_pcm_substream
*substream
)
250 struct snd_motu
*motu
= substream
->private_data
;
252 mutex_lock(&motu
->mutex
);
254 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
255 motu
->playback_substreams
--;
257 snd_motu_stream_stop_duplex(motu
);
259 mutex_unlock(&motu
->mutex
);
261 return snd_pcm_lib_free_vmalloc_buffer(substream
);
264 static int capture_prepare(struct snd_pcm_substream
*substream
)
266 struct snd_motu
*motu
= substream
->private_data
;
269 mutex_lock(&motu
->mutex
);
270 err
= snd_motu_stream_start_duplex(motu
, substream
->runtime
->rate
);
271 mutex_unlock(&motu
->mutex
);
273 amdtp_stream_pcm_prepare(&motu
->tx_stream
);
277 static int playback_prepare(struct snd_pcm_substream
*substream
)
279 struct snd_motu
*motu
= substream
->private_data
;
282 mutex_lock(&motu
->mutex
);
283 err
= snd_motu_stream_start_duplex(motu
, substream
->runtime
->rate
);
284 mutex_unlock(&motu
->mutex
);
286 amdtp_stream_pcm_prepare(&motu
->rx_stream
);
291 static int capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
293 struct snd_motu
*motu
= substream
->private_data
;
296 case SNDRV_PCM_TRIGGER_START
:
297 amdtp_stream_pcm_trigger(&motu
->tx_stream
, substream
);
299 case SNDRV_PCM_TRIGGER_STOP
:
300 amdtp_stream_pcm_trigger(&motu
->tx_stream
, NULL
);
308 static int playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
310 struct snd_motu
*motu
= substream
->private_data
;
313 case SNDRV_PCM_TRIGGER_START
:
314 amdtp_stream_pcm_trigger(&motu
->rx_stream
, substream
);
316 case SNDRV_PCM_TRIGGER_STOP
:
317 amdtp_stream_pcm_trigger(&motu
->rx_stream
, NULL
);
326 static snd_pcm_uframes_t
capture_pointer(struct snd_pcm_substream
*substream
)
328 struct snd_motu
*motu
= substream
->private_data
;
330 return amdtp_stream_pcm_pointer(&motu
->tx_stream
);
332 static snd_pcm_uframes_t
playback_pointer(struct snd_pcm_substream
*substream
)
334 struct snd_motu
*motu
= substream
->private_data
;
336 return amdtp_stream_pcm_pointer(&motu
->rx_stream
);
339 static int capture_ack(struct snd_pcm_substream
*substream
)
341 struct snd_motu
*motu
= substream
->private_data
;
343 return amdtp_stream_pcm_ack(&motu
->tx_stream
);
346 static int playback_ack(struct snd_pcm_substream
*substream
)
348 struct snd_motu
*motu
= substream
->private_data
;
350 return amdtp_stream_pcm_ack(&motu
->rx_stream
);
353 int snd_motu_create_pcm_devices(struct snd_motu
*motu
)
355 static const struct snd_pcm_ops capture_ops
= {
358 .ioctl
= snd_pcm_lib_ioctl
,
359 .hw_params
= capture_hw_params
,
360 .hw_free
= capture_hw_free
,
361 .prepare
= capture_prepare
,
362 .trigger
= capture_trigger
,
363 .pointer
= capture_pointer
,
365 .page
= snd_pcm_lib_get_vmalloc_page
,
366 .mmap
= snd_pcm_lib_mmap_vmalloc
,
368 static const struct snd_pcm_ops playback_ops
= {
371 .ioctl
= snd_pcm_lib_ioctl
,
372 .hw_params
= playback_hw_params
,
373 .hw_free
= playback_hw_free
,
374 .prepare
= playback_prepare
,
375 .trigger
= playback_trigger
,
376 .pointer
= playback_pointer
,
378 .page
= snd_pcm_lib_get_vmalloc_page
,
379 .mmap
= snd_pcm_lib_mmap_vmalloc
,
384 err
= snd_pcm_new(motu
->card
, motu
->card
->driver
, 0, 1, 1, &pcm
);
387 pcm
->private_data
= motu
;
388 strcpy(pcm
->name
, motu
->card
->shortname
);
390 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &capture_ops
);
391 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &playback_ops
);