cpufreq: AMD: Ignore the check for ProcFeedback in ST/CZ
[linux-2.6/btrfs-unstable.git] / sound / firewire / amdtp-am824.c
blob4210e5c6262eb9f19e4d08f6d195bca793f4c680
1 /*
2 * AM824 format in Audio and Music Data Transmission Protocol (IEC 61883-6)
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
10 #include <linux/slab.h>
12 #include "amdtp-am824.h"
14 #define CIP_FMT_AM 0x10
16 /* "Clock-based rate control mode" is just supported. */
17 #define AMDTP_FDF_AM824 0x00
20 * Nominally 3125 bytes/second, but the MIDI port's clock might be
21 * 1% too slow, and the bus clock 100 ppm too fast.
23 #define MIDI_BYTES_PER_SECOND 3093
26 * Several devices look only at the first eight data blocks.
27 * In any case, this is more than enough for the MIDI data rate.
29 #define MAX_MIDI_RX_BLOCKS 8
31 struct amdtp_am824 {
32 struct snd_rawmidi_substream *midi[AM824_MAX_CHANNELS_FOR_MIDI * 8];
33 int midi_fifo_limit;
34 int midi_fifo_used[AM824_MAX_CHANNELS_FOR_MIDI * 8];
35 unsigned int pcm_channels;
36 unsigned int midi_ports;
38 u8 pcm_positions[AM824_MAX_CHANNELS_FOR_PCM];
39 u8 midi_position;
41 unsigned int frame_multiplier;
44 /**
45 * amdtp_am824_set_parameters - set stream parameters
46 * @s: the AMDTP stream to configure
47 * @rate: the sample rate
48 * @pcm_channels: the number of PCM samples in each data block, to be encoded
49 * as AM824 multi-bit linear audio
50 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
51 * @double_pcm_frames: one data block transfers two PCM frames
53 * The parameters must be set before the stream is started, and must not be
54 * changed while the stream is running.
56 int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate,
57 unsigned int pcm_channels,
58 unsigned int midi_ports,
59 bool double_pcm_frames)
61 struct amdtp_am824 *p = s->protocol;
62 unsigned int midi_channels;
63 unsigned int i;
64 int err;
66 if (amdtp_stream_running(s))
67 return -EINVAL;
69 if (pcm_channels > AM824_MAX_CHANNELS_FOR_PCM)
70 return -EINVAL;
72 midi_channels = DIV_ROUND_UP(midi_ports, 8);
73 if (midi_channels > AM824_MAX_CHANNELS_FOR_MIDI)
74 return -EINVAL;
76 if (WARN_ON(amdtp_stream_running(s)) ||
77 WARN_ON(pcm_channels > AM824_MAX_CHANNELS_FOR_PCM) ||
78 WARN_ON(midi_channels > AM824_MAX_CHANNELS_FOR_MIDI))
79 return -EINVAL;
81 err = amdtp_stream_set_parameters(s, rate,
82 pcm_channels + midi_channels);
83 if (err < 0)
84 return err;
86 s->fdf = AMDTP_FDF_AM824 | s->sfc;
88 p->pcm_channels = pcm_channels;
89 p->midi_ports = midi_ports;
92 * In IEC 61883-6, one data block represents one event. In ALSA, one
93 * event equals to one PCM frame. But Dice has a quirk at higher
94 * sampling rate to transfer two PCM frames in one data block.
96 if (double_pcm_frames)
97 p->frame_multiplier = 2;
98 else
99 p->frame_multiplier = 1;
101 /* init the position map for PCM and MIDI channels */
102 for (i = 0; i < pcm_channels; i++)
103 p->pcm_positions[i] = i;
104 p->midi_position = p->pcm_channels;
107 * We do not know the actual MIDI FIFO size of most devices. Just
108 * assume two bytes, i.e., one byte can be received over the bus while
109 * the previous one is transmitted over MIDI.
110 * (The value here is adjusted for midi_ratelimit_per_packet().)
112 p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
114 return 0;
116 EXPORT_SYMBOL_GPL(amdtp_am824_set_parameters);
119 * amdtp_am824_set_pcm_position - set an index of data channel for a channel
120 * of PCM frame
121 * @s: the AMDTP stream
122 * @index: the index of data channel in an data block
123 * @position: the channel of PCM frame
125 void amdtp_am824_set_pcm_position(struct amdtp_stream *s, unsigned int index,
126 unsigned int position)
128 struct amdtp_am824 *p = s->protocol;
130 if (index < p->pcm_channels)
131 p->pcm_positions[index] = position;
133 EXPORT_SYMBOL_GPL(amdtp_am824_set_pcm_position);
136 * amdtp_am824_set_midi_position - set a index of data channel for MIDI
137 * conformant data channel
138 * @s: the AMDTP stream
139 * @position: the index of data channel in an data block
141 void amdtp_am824_set_midi_position(struct amdtp_stream *s,
142 unsigned int position)
144 struct amdtp_am824 *p = s->protocol;
146 p->midi_position = position;
148 EXPORT_SYMBOL_GPL(amdtp_am824_set_midi_position);
150 static void write_pcm_s32(struct amdtp_stream *s,
151 struct snd_pcm_substream *pcm,
152 __be32 *buffer, unsigned int frames)
154 struct amdtp_am824 *p = s->protocol;
155 struct snd_pcm_runtime *runtime = pcm->runtime;
156 unsigned int channels, remaining_frames, i, c;
157 const u32 *src;
159 channels = p->pcm_channels;
160 src = (void *)runtime->dma_area +
161 frames_to_bytes(runtime, s->pcm_buffer_pointer);
162 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
164 for (i = 0; i < frames; ++i) {
165 for (c = 0; c < channels; ++c) {
166 buffer[p->pcm_positions[c]] =
167 cpu_to_be32((*src >> 8) | 0x40000000);
168 src++;
170 buffer += s->data_block_quadlets;
171 if (--remaining_frames == 0)
172 src = (void *)runtime->dma_area;
176 static void read_pcm_s32(struct amdtp_stream *s,
177 struct snd_pcm_substream *pcm,
178 __be32 *buffer, unsigned int frames)
180 struct amdtp_am824 *p = s->protocol;
181 struct snd_pcm_runtime *runtime = pcm->runtime;
182 unsigned int channels, remaining_frames, i, c;
183 u32 *dst;
185 channels = p->pcm_channels;
186 dst = (void *)runtime->dma_area +
187 frames_to_bytes(runtime, s->pcm_buffer_pointer);
188 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
190 for (i = 0; i < frames; ++i) {
191 for (c = 0; c < channels; ++c) {
192 *dst = be32_to_cpu(buffer[p->pcm_positions[c]]) << 8;
193 dst++;
195 buffer += s->data_block_quadlets;
196 if (--remaining_frames == 0)
197 dst = (void *)runtime->dma_area;
201 static void write_pcm_silence(struct amdtp_stream *s,
202 __be32 *buffer, unsigned int frames)
204 struct amdtp_am824 *p = s->protocol;
205 unsigned int i, c, channels = p->pcm_channels;
207 for (i = 0; i < frames; ++i) {
208 for (c = 0; c < channels; ++c)
209 buffer[p->pcm_positions[c]] = cpu_to_be32(0x40000000);
210 buffer += s->data_block_quadlets;
215 * amdtp_am824_add_pcm_hw_constraints - add hw constraints for PCM substream
216 * @s: the AMDTP stream for AM824 data block, must be initialized.
217 * @runtime: the PCM substream runtime
220 int amdtp_am824_add_pcm_hw_constraints(struct amdtp_stream *s,
221 struct snd_pcm_runtime *runtime)
223 int err;
225 err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
226 if (err < 0)
227 return err;
229 /* AM824 in IEC 61883-6 can deliver 24bit data. */
230 return snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
232 EXPORT_SYMBOL_GPL(amdtp_am824_add_pcm_hw_constraints);
235 * amdtp_am824_midi_trigger - start/stop playback/capture with a MIDI device
236 * @s: the AMDTP stream
237 * @port: index of MIDI port
238 * @midi: the MIDI device to be started, or %NULL to stop the current device
240 * Call this function on a running isochronous stream to enable the actual
241 * transmission of MIDI data. This function should be called from the MIDI
242 * device's .trigger callback.
244 void amdtp_am824_midi_trigger(struct amdtp_stream *s, unsigned int port,
245 struct snd_rawmidi_substream *midi)
247 struct amdtp_am824 *p = s->protocol;
249 if (port < p->midi_ports)
250 WRITE_ONCE(p->midi[port], midi);
252 EXPORT_SYMBOL_GPL(amdtp_am824_midi_trigger);
255 * To avoid sending MIDI bytes at too high a rate, assume that the receiving
256 * device has a FIFO, and track how much it is filled. This values increases
257 * by one whenever we send one byte in a packet, but the FIFO empties at
258 * a constant rate independent of our packet rate. One packet has syt_interval
259 * samples, so the number of bytes that empty out of the FIFO, per packet(!),
260 * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
261 * fractional values, the values in midi_fifo_used[] are measured in bytes
262 * multiplied by the sample rate.
264 static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
266 struct amdtp_am824 *p = s->protocol;
267 int used;
269 used = p->midi_fifo_used[port];
270 if (used == 0) /* common shortcut */
271 return true;
273 used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
274 used = max(used, 0);
275 p->midi_fifo_used[port] = used;
277 return used < p->midi_fifo_limit;
280 static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
282 struct amdtp_am824 *p = s->protocol;
284 p->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
287 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
288 unsigned int frames)
290 struct amdtp_am824 *p = s->protocol;
291 unsigned int f, port;
292 u8 *b;
294 for (f = 0; f < frames; f++) {
295 b = (u8 *)&buffer[p->midi_position];
297 port = (s->data_block_counter + f) % 8;
298 if (f < MAX_MIDI_RX_BLOCKS &&
299 midi_ratelimit_per_packet(s, port) &&
300 p->midi[port] != NULL &&
301 snd_rawmidi_transmit(p->midi[port], &b[1], 1) == 1) {
302 midi_rate_use_one_byte(s, port);
303 b[0] = 0x81;
304 } else {
305 b[0] = 0x80;
306 b[1] = 0;
308 b[2] = 0;
309 b[3] = 0;
311 buffer += s->data_block_quadlets;
315 static void read_midi_messages(struct amdtp_stream *s,
316 __be32 *buffer, unsigned int frames)
318 struct amdtp_am824 *p = s->protocol;
319 unsigned int f, port;
320 int len;
321 u8 *b;
323 for (f = 0; f < frames; f++) {
324 port = (s->data_block_counter + f) % 8;
325 b = (u8 *)&buffer[p->midi_position];
327 len = b[0] - 0x80;
328 if ((1 <= len) && (len <= 3) && (p->midi[port]))
329 snd_rawmidi_receive(p->midi[port], b + 1, len);
331 buffer += s->data_block_quadlets;
335 static unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
336 unsigned int data_blocks, unsigned int *syt)
338 struct amdtp_am824 *p = s->protocol;
339 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
340 unsigned int pcm_frames;
342 if (pcm) {
343 write_pcm_s32(s, pcm, buffer, data_blocks);
344 pcm_frames = data_blocks * p->frame_multiplier;
345 } else {
346 write_pcm_silence(s, buffer, data_blocks);
347 pcm_frames = 0;
350 if (p->midi_ports)
351 write_midi_messages(s, buffer, data_blocks);
353 return pcm_frames;
356 static unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
357 unsigned int data_blocks, unsigned int *syt)
359 struct amdtp_am824 *p = s->protocol;
360 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
361 unsigned int pcm_frames;
363 if (pcm) {
364 read_pcm_s32(s, pcm, buffer, data_blocks);
365 pcm_frames = data_blocks * p->frame_multiplier;
366 } else {
367 pcm_frames = 0;
370 if (p->midi_ports)
371 read_midi_messages(s, buffer, data_blocks);
373 return pcm_frames;
377 * amdtp_am824_init - initialize an AMDTP stream structure to handle AM824
378 * data block
379 * @s: the AMDTP stream to initialize
380 * @unit: the target of the stream
381 * @dir: the direction of stream
382 * @flags: the packet transmission method to use
384 int amdtp_am824_init(struct amdtp_stream *s, struct fw_unit *unit,
385 enum amdtp_stream_direction dir, enum cip_flags flags)
387 amdtp_stream_process_data_blocks_t process_data_blocks;
389 if (dir == AMDTP_IN_STREAM)
390 process_data_blocks = process_tx_data_blocks;
391 else
392 process_data_blocks = process_rx_data_blocks;
394 return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
395 process_data_blocks,
396 sizeof(struct amdtp_am824));
398 EXPORT_SYMBOL_GPL(amdtp_am824_init);