blob: 2254eec4521b998716249dae6c22d65a58cbde5e [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001/*
2 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3 * with Common Isochronous Packet (IEC 61883-1) headers
4 *
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/firewire.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <sound/pcm.h>
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +090015#include <sound/pcm_params.h>
Takashi Sakamoto83d8d722014-04-25 22:44:47 +090016#include <sound/rawmidi.h>
Takashi Sakamotod67c46b2015-09-19 11:21:54 +090017#include "amdtp-stream.h"
Clemens Ladisch31ef9132011-03-15 07:53:21 +010018
19#define TICKS_PER_CYCLE 3072
20#define CYCLES_PER_SECOND 8000
21#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22
Clemens Ladisch5c697e52014-11-25 22:52:24 +010023/*
Clemens Ladisch25ca9172014-11-25 22:54:10 +010024 * Nominally 3125 bytes/second, but the MIDI port's clock might be
25 * 1% too slow, and the bus clock 100 ppm too fast.
26 */
27#define MIDI_BYTES_PER_SECOND 3093
28
29/*
Clemens Ladisch5c697e52014-11-25 22:52:24 +010030 * Several devices look only at the first eight data blocks.
31 * In any case, this is more than enough for the MIDI data rate.
32 */
33#define MAX_MIDI_RX_BLOCKS 8
34
Takashi Sakamotoca5b5052015-02-21 11:50:17 +090035#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010036
Takashi Sakamotob445db42014-04-25 22:44:43 +090037/* isochronous header parameters */
38#define ISO_DATA_LENGTH_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010039#define TAG_CIP 1
40
Takashi Sakamotob445db42014-04-25 22:44:43 +090041/* common isochronous packet header parameters */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090042#define CIP_EOH_SHIFT 31
43#define CIP_EOH (1u << CIP_EOH_SHIFT)
Takashi Sakamotob445db42014-04-25 22:44:43 +090044#define CIP_EOH_MASK 0x80000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090045#define CIP_SID_SHIFT 24
46#define CIP_SID_MASK 0x3f000000
47#define CIP_DBS_MASK 0x00ff0000
48#define CIP_DBS_SHIFT 16
49#define CIP_DBC_MASK 0x000000ff
50#define CIP_FMT_SHIFT 24
Takashi Sakamotob445db42014-04-25 22:44:43 +090051#define CIP_FMT_MASK 0x3f000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090052#define CIP_FDF_MASK 0x00ff0000
53#define CIP_FDF_SHIFT 16
Takashi Sakamotob445db42014-04-25 22:44:43 +090054#define CIP_SYT_MASK 0x0000ffff
55#define CIP_SYT_NO_INFO 0xffff
Takashi Sakamotob445db42014-04-25 22:44:43 +090056
57/*
58 * Audio and Music transfer protocol specific parameters
59 * only "Clock-based rate control mode" is supported
60 */
Takashi Sakamoto414ba022015-09-19 11:21:53 +090061#define CIP_FMT_AM 0x10
62#define AMDTP_FDF_AM824 0x00
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090063#define AMDTP_FDF_NO_DATA 0xff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010064
65/* TODO: make these configurable */
66#define INTERRUPT_INTERVAL 16
67#define QUEUE_LENGTH 48
68
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090069#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090070#define OUT_PACKET_HEADER_SIZE 0
71
Clemens Ladisch76fb8782012-05-13 22:03:09 +020072static void pcm_period_tasklet(unsigned long data);
73
Clemens Ladisch31ef9132011-03-15 07:53:21 +010074/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090075 * amdtp_stream_init - initialize an AMDTP stream structure
76 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010077 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090078 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010079 * @flags: the packet transmission method to use
Takashi Sakamoto59558152015-09-19 11:21:55 +090080 * @fmt: the value of fmt field in CIP header
Clemens Ladisch31ef9132011-03-15 07:53:21 +010081 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090082int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto59558152015-09-19 11:21:55 +090083 enum amdtp_stream_direction dir, enum cip_flags flags,
84 unsigned int fmt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010085{
Takashi Sakamotoc6f224d2015-02-21 23:54:58 +090086 s->unit = unit;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090087 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010088 s->flags = flags;
89 s->context = ERR_PTR(-1);
90 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020091 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010092 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010093
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090094 init_waitqueue_head(&s->callback_wait);
95 s->callbacked = false;
96 s->sync_slave = NULL;
97
Takashi Sakamoto59558152015-09-19 11:21:55 +090098 s->fmt = fmt;
Takashi Sakamoto414ba022015-09-19 11:21:53 +090099
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100100 return 0;
101}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900102EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100103
104/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900105 * amdtp_stream_destroy - free stream resources
106 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100107 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900108void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100109{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900110 WARN_ON(amdtp_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100111 mutex_destroy(&s->mutex);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100112}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900113EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100114
Clemens Ladischc5280e92011-10-16 21:39:00 +0200115const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200116 [CIP_SFC_32000] = 8,
117 [CIP_SFC_44100] = 8,
118 [CIP_SFC_48000] = 8,
119 [CIP_SFC_88200] = 16,
120 [CIP_SFC_96000] = 16,
121 [CIP_SFC_176400] = 32,
122 [CIP_SFC_192000] = 32,
123};
124EXPORT_SYMBOL(amdtp_syt_intervals);
125
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900126const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900127 [CIP_SFC_32000] = 32000,
128 [CIP_SFC_44100] = 44100,
129 [CIP_SFC_48000] = 48000,
130 [CIP_SFC_88200] = 88200,
131 [CIP_SFC_96000] = 96000,
132 [CIP_SFC_176400] = 176400,
133 [CIP_SFC_192000] = 192000,
134};
135EXPORT_SYMBOL(amdtp_rate_table);
136
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100137/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900138 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
139 * @s: the AMDTP stream, which must be initialized.
140 * @runtime: the PCM substream runtime
141 */
142int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
143 struct snd_pcm_runtime *runtime)
144{
145 int err;
146
147 /* AM824 in IEC 61883-6 can deliver 24bit data */
148 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
149 if (err < 0)
150 goto end;
151
152 /*
153 * Currently firewire-lib processes 16 packets in one software
154 * interrupt callback. This equals to 2msec but actually the
155 * interval of the interrupts has a jitter.
156 * Additionally, even if adding a constraint to fit period size to
157 * 2msec, actual calculated frames per period doesn't equal to 2msec,
158 * depending on sampling rate.
159 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
160 * Here let us use 5msec for safe period interrupt.
161 */
162 err = snd_pcm_hw_constraint_minmax(runtime,
163 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
164 5000, UINT_MAX);
165 if (err < 0)
166 goto end;
167
168 /* Non-Blocking stream has no more constraints */
169 if (!(s->flags & CIP_BLOCKING))
170 goto end;
171
172 /*
173 * One AMDTP packet can include some frames. In blocking mode, the
174 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
175 * depending on its sampling rate. For accurate period interrupt, it's
Yannick Guerrinice991982015-03-09 22:13:03 +0100176 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900177 *
Yannick Guerrinice991982015-03-09 22:13:03 +0100178 * TODO: These constraints can be improved with proper rules.
179 * Currently apply LCM of SYT_INTERVALs.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900180 */
181 err = snd_pcm_hw_constraint_step(runtime, 0,
182 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
183 if (err < 0)
184 goto end;
185 err = snd_pcm_hw_constraint_step(runtime, 0,
186 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
187end:
188 return err;
189}
190EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
191
192/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900193 * amdtp_stream_set_parameters - set stream parameters
194 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100195 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200196 * @pcm_channels: the number of PCM samples in each data block, to be encoded
197 * as AM824 multi-bit linear audio
198 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900199 * @double_pcm_frames: one data block transfers two PCM frames
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100200 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200201 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100202 * changed while the stream is running.
203 */
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900204int amdtp_stream_set_parameters(struct amdtp_stream *s,
205 unsigned int rate,
206 unsigned int pcm_channels,
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900207 unsigned int midi_ports,
208 bool double_pcm_frames)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100209{
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900210 unsigned int i, sfc, midi_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100211
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900212 midi_channels = DIV_ROUND_UP(midi_ports, 8);
213
Takashi Sakamotod67c46b2015-09-19 11:21:54 +0900214 if (WARN_ON(amdtp_stream_running(s)) ||
215 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) ||
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900216 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900217 return -EINVAL;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100218
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900219 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900220 if (amdtp_rate_table[sfc] == rate)
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900221 break;
222 }
223 if (sfc == ARRAY_SIZE(amdtp_rate_table))
224 return -EINVAL;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200225
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900226 s->pcm_channels = pcm_channels;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200227 s->sfc = sfc;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900228 s->data_block_quadlets = s->pcm_channels + midi_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200229 s->midi_ports = midi_ports;
230
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900231 s->fdf = AMDTP_FDF_AM824 | s->sfc;
232
Takashi Sakamoto6a4e89f2015-09-19 11:21:51 +0900233 /*
234 * In IEC 61883-6, one data block represents one event. In ALSA, one
235 * event equals to one PCM frame. But Dice has a quirk at higher
236 * sampling rate to transfer two PCM frames in one data block.
237 */
238 if (double_pcm_frames)
239 s->frame_multiplier = 2;
240 else
241 s->frame_multiplier = 1;
242
Clemens Ladischa7304e32011-09-04 22:16:10 +0200243 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200244
245 /* default buffering in the device */
246 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
247 if (s->flags & CIP_BLOCKING)
248 /* additional buffering needed to adjust for no-data packets */
249 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900250
251 /* init the position map for PCM and MIDI channels */
252 for (i = 0; i < pcm_channels; i++)
253 s->pcm_positions[i] = i;
254 s->midi_position = s->pcm_channels;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100255
256 /*
257 * We do not know the actual MIDI FIFO size of most devices. Just
258 * assume two bytes, i.e., one byte can be received over the bus while
259 * the previous one is transmitted over MIDI.
260 * (The value here is adjusted for midi_ratelimit_per_packet().)
261 */
262 s->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900263
264 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100265}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900266EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100267
268/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900269 * amdtp_stream_get_max_payload - get the stream's packet size
270 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100271 *
272 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900273 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100274 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900275unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100276{
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900277 unsigned int multiplier = 1;
278
279 if (s->flags & CIP_JUMBO_PAYLOAD)
280 multiplier = 5;
281
282 return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100283}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900284EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100285
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900286static void write_pcm_s16(struct amdtp_stream *s,
287 struct snd_pcm_substream *pcm,
288 __be32 *buffer, unsigned int frames);
289static void write_pcm_s32(struct amdtp_stream *s,
290 struct snd_pcm_substream *pcm,
291 __be32 *buffer, unsigned int frames);
292static void read_pcm_s32(struct amdtp_stream *s,
293 struct snd_pcm_substream *pcm,
294 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100295
296/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900297 * amdtp_stream_set_pcm_format - set the PCM format
298 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100299 * @format: the format of the ALSA PCM device
300 *
Yannick Guerrinice991982015-03-09 22:13:03 +0100301 * The sample format must be set after the other parameters (rate/PCM channels/
Clemens Ladischa7304e32011-09-04 22:16:10 +0200302 * MIDI) and before the stream is started, and must not be changed while the
303 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100304 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900305void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
306 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100307{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900308 if (WARN_ON(amdtp_stream_pcm_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100309 return;
310
311 switch (format) {
312 default:
313 WARN_ON(1);
314 /* fall through */
315 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900316 if (s->direction == AMDTP_OUT_STREAM) {
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900317 s->transfer_samples = write_pcm_s16;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900318 break;
319 }
320 WARN_ON(1);
321 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100322 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900323 if (s->direction == AMDTP_OUT_STREAM)
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900324 s->transfer_samples = write_pcm_s32;
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900325 else
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900326 s->transfer_samples = read_pcm_s32;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100327 break;
328 }
329}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900330EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100331
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200332/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900333 * amdtp_stream_pcm_prepare - prepare PCM device for running
334 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200335 *
336 * This function should be called from the PCM device's .prepare callback.
337 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900338void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200339{
340 tasklet_kill(&s->period_tasklet);
341 s->pcm_buffer_pointer = 0;
342 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200343 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200344}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900345EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200346
Takashi Sakamoto875be092015-05-22 23:00:51 +0900347static unsigned int calculate_data_blocks(struct amdtp_stream *s,
348 unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100349{
350 unsigned int phase, data_blocks;
351
Takashi Sakamoto875be092015-05-22 23:00:51 +0900352 /* Blocking mode. */
353 if (s->flags & CIP_BLOCKING) {
354 /* This module generate empty packet for 'no data'. */
355 if (syt == CIP_SYT_NO_INFO)
356 data_blocks = 0;
357 else
358 data_blocks = s->syt_interval;
359 /* Non-blocking mode. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100360 } else {
Takashi Sakamoto875be092015-05-22 23:00:51 +0900361 if (!cip_sfc_is_base_44100(s->sfc)) {
362 /* Sample_rate / 8000 is an integer, and precomputed. */
363 data_blocks = s->data_block_state;
364 } else {
365 phase = s->data_block_state;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100366
367 /*
368 * This calculates the number of data blocks per packet so that
369 * 1) the overall rate is correct and exactly synchronized to
370 * the bus clock, and
371 * 2) packets with a rounded-up number of blocks occur as early
372 * as possible in the sequence (to prevent underruns of the
373 * device's buffer).
374 */
Takashi Sakamoto875be092015-05-22 23:00:51 +0900375 if (s->sfc == CIP_SFC_44100)
376 /* 6 6 5 6 5 6 5 ... */
377 data_blocks = 5 + ((phase & 1) ^
378 (phase == 0 || phase >= 40));
379 else
380 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
381 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
382 if (++phase >= (80 >> (s->sfc >> 1)))
383 phase = 0;
384 s->data_block_state = phase;
385 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100386 }
387
388 return data_blocks;
389}
390
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900391static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100392 unsigned int cycle)
393{
394 unsigned int syt_offset, phase, index, syt;
395
396 if (s->last_syt_offset < TICKS_PER_CYCLE) {
397 if (!cip_sfc_is_base_44100(s->sfc))
398 syt_offset = s->last_syt_offset + s->syt_offset_state;
399 else {
400 /*
401 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
402 * n * SYT_INTERVAL * 24576000 / sample_rate
403 * Modulo TICKS_PER_CYCLE, the difference between successive
404 * elements is about 1386.23. Rounding the results of this
405 * formula to the SYT precision results in a sequence of
406 * differences that begins with:
407 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
408 * This code generates _exactly_ the same sequence.
409 */
410 phase = s->syt_offset_state;
411 index = phase % 13;
412 syt_offset = s->last_syt_offset;
413 syt_offset += 1386 + ((index && !(index & 3)) ||
414 phase == 146);
415 if (++phase >= 147)
416 phase = 0;
417 s->syt_offset_state = phase;
418 }
419 } else
420 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
421 s->last_syt_offset = syt_offset;
422
Clemens Ladischbe454362011-03-15 07:55:02 +0100423 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200424 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100425 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
426 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100427
Takashi Sakamotob445db42014-04-25 22:44:43 +0900428 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100429 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900430 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100431 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100432}
433
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900434static void write_pcm_s32(struct amdtp_stream *s,
435 struct snd_pcm_substream *pcm,
436 __be32 *buffer, unsigned int frames)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100437{
438 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900439 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100440 const u32 *src;
441
442 channels = s->pcm_channels;
443 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900444 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100445 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100446
447 for (i = 0; i < frames; ++i) {
448 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900449 buffer[s->pcm_positions[c]] =
450 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100451 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100452 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900453 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100454 if (--remaining_frames == 0)
455 src = (void *)runtime->dma_area;
456 }
457}
458
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900459static void write_pcm_s16(struct amdtp_stream *s,
460 struct snd_pcm_substream *pcm,
461 __be32 *buffer, unsigned int frames)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100462{
463 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900464 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100465 const u16 *src;
466
467 channels = s->pcm_channels;
468 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900469 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100470 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100471
472 for (i = 0; i < frames; ++i) {
473 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900474 buffer[s->pcm_positions[c]] =
Takashi Sakamotoa6975f22014-06-02 01:50:16 +0900475 cpu_to_be32((*src << 8) | 0x42000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100476 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100477 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900478 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100479 if (--remaining_frames == 0)
480 src = (void *)runtime->dma_area;
481 }
482}
483
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900484static void read_pcm_s32(struct amdtp_stream *s,
485 struct snd_pcm_substream *pcm,
486 __be32 *buffer, unsigned int frames)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900487{
488 struct snd_pcm_runtime *runtime = pcm->runtime;
489 unsigned int channels, remaining_frames, i, c;
490 u32 *dst;
491
492 channels = s->pcm_channels;
493 dst = (void *)runtime->dma_area +
494 frames_to_bytes(runtime, s->pcm_buffer_pointer);
495 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
496
497 for (i = 0; i < frames; ++i) {
498 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900499 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900500 dst++;
501 }
502 buffer += s->data_block_quadlets;
503 if (--remaining_frames == 0)
504 dst = (void *)runtime->dma_area;
505 }
506}
507
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900508static void write_pcm_silence(struct amdtp_stream *s,
509 __be32 *buffer, unsigned int frames)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100510{
511 unsigned int i, c;
512
513 for (i = 0; i < frames; ++i) {
514 for (c = 0; c < s->pcm_channels; ++c)
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900515 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100516 buffer += s->data_block_quadlets;
517 }
518}
519
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100520/*
521 * To avoid sending MIDI bytes at too high a rate, assume that the receiving
522 * device has a FIFO, and track how much it is filled. This values increases
523 * by one whenever we send one byte in a packet, but the FIFO empties at
524 * a constant rate independent of our packet rate. One packet has syt_interval
525 * samples, so the number of bytes that empty out of the FIFO, per packet(!),
526 * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
527 * fractional values, the values in midi_fifo_used[] are measured in bytes
528 * multiplied by the sample rate.
529 */
530static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
531{
532 int used;
533
534 used = s->midi_fifo_used[port];
535 if (used == 0) /* common shortcut */
536 return true;
537
538 used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
539 used = max(used, 0);
540 s->midi_fifo_used[port] = used;
541
542 return used < s->midi_fifo_limit;
543}
544
545static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
546{
547 s->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
548}
549
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900550static void write_midi_messages(struct amdtp_stream *s,
551 __be32 *buffer, unsigned int frames)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100552{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900553 unsigned int f, port;
554 u8 *b;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100555
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900556 for (f = 0; f < frames; f++) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900557 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900558
559 port = (s->data_block_counter + f) % 8;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100560 if (f < MAX_MIDI_RX_BLOCKS &&
561 midi_ratelimit_per_packet(s, port) &&
562 s->midi[port] != NULL &&
563 snd_rawmidi_transmit(s->midi[port], &b[1], 1) == 1) {
564 midi_rate_use_one_byte(s, port);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900565 b[0] = 0x81;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100566 } else {
567 b[0] = 0x80;
568 b[1] = 0;
569 }
570 b[2] = 0;
571 b[3] = 0;
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900572
573 buffer += s->data_block_quadlets;
574 }
575}
576
Takashi Sakamoto29bcae22015-05-22 23:21:11 +0900577static void read_midi_messages(struct amdtp_stream *s,
578 __be32 *buffer, unsigned int frames)
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900579{
580 unsigned int f, port;
581 int len;
582 u8 *b;
583
584 for (f = 0; f < frames; f++) {
585 port = (s->data_block_counter + f) % 8;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900586 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900587
588 len = b[0] - 0x80;
589 if ((1 <= len) && (len <= 3) && (s->midi[port]))
590 snd_rawmidi_receive(s->midi[port], b + 1, len);
591
592 buffer += s->data_block_quadlets;
593 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100594}
595
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900596static void update_pcm_pointers(struct amdtp_stream *s,
597 struct snd_pcm_substream *pcm,
598 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900599{
600 unsigned int ptr;
601
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900602 ptr = s->pcm_buffer_pointer + frames;
603 if (ptr >= pcm->runtime->buffer_size)
604 ptr -= pcm->runtime->buffer_size;
605 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
606
607 s->pcm_period_pointer += frames;
608 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
609 s->pcm_period_pointer -= pcm->runtime->period_size;
610 s->pointer_flush = false;
611 tasklet_hi_schedule(&s->period_tasklet);
612 }
613}
614
615static void pcm_period_tasklet(unsigned long data)
616{
617 struct amdtp_stream *s = (void *)data;
618 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
619
620 if (pcm)
621 snd_pcm_period_elapsed(pcm);
622}
623
624static int queue_packet(struct amdtp_stream *s,
625 unsigned int header_length,
626 unsigned int payload_length, bool skip)
627{
628 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900629 int err = 0;
630
631 if (IS_ERR(s->context))
632 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900633
634 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
635 p.tag = TAG_CIP;
636 p.header_length = header_length;
637 p.payload_length = (!skip) ? payload_length : 0;
638 p.skip = skip;
639 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
640 s->buffer.packets[s->packet_index].offset);
641 if (err < 0) {
642 dev_err(&s->unit->device, "queueing error: %d\n", err);
643 goto end;
644 }
645
646 if (++s->packet_index >= QUEUE_LENGTH)
647 s->packet_index = 0;
648end:
649 return err;
650}
651
652static inline int queue_out_packet(struct amdtp_stream *s,
653 unsigned int payload_length, bool skip)
654{
655 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
656 payload_length, skip);
657}
658
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900659static inline int queue_in_packet(struct amdtp_stream *s)
660{
661 return queue_packet(s, IN_PACKET_HEADER_SIZE,
662 amdtp_stream_get_max_payload(s), false);
663}
664
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900665unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
666 unsigned int data_blocks, unsigned int *syt)
667{
668 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
669 unsigned int pcm_frames;
670
671 if (pcm) {
672 s->transfer_samples(s, pcm, buffer, data_blocks);
673 pcm_frames = data_blocks * s->frame_multiplier;
674 } else {
675 write_pcm_silence(s, buffer, data_blocks);
676 pcm_frames = 0;
677 }
678
679 if (s->midi_ports)
680 write_midi_messages(s, buffer, data_blocks);
681
682 return pcm_frames;
683}
684
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900685static int handle_out_packet(struct amdtp_stream *s, unsigned int data_blocks,
686 unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100687{
688 __be32 *buffer;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900689 unsigned int payload_length;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900690 unsigned int pcm_frames;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100691 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100692
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900693 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900694 pcm_frames = process_rx_data_blocks(s, buffer + 2, data_blocks, &syt);
695
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100696 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900697 (s->data_block_quadlets << CIP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100698 s->data_block_counter);
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900699 buffer[1] = cpu_to_be32(CIP_EOH |
700 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
701 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
702 (syt & CIP_SYT_MASK));
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100703
704 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
705
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900706 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900707 if (queue_out_packet(s, payload_length, false) < 0)
708 return -EIO;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100709
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900710 pcm = ACCESS_ONCE(s->pcm);
711 if (pcm && pcm_frames > 0)
712 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900713
714 /* No need to return the number of handled data blocks. */
715 return 0;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200716}
717
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900718unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
719 unsigned int data_blocks, unsigned int *syt)
720{
721 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
722 unsigned int pcm_frames;
723
724 if (pcm) {
725 s->transfer_samples(s, pcm, buffer, data_blocks);
726 pcm_frames = data_blocks * s->frame_multiplier;
727 } else {
728 pcm_frames = 0;
729 }
730
731 if (s->midi_ports)
732 read_midi_messages(s, buffer, data_blocks);
733
734 return pcm_frames;
735}
736
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900737static int handle_in_packet(struct amdtp_stream *s,
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900738 unsigned int payload_quadlets, __be32 *buffer,
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900739 unsigned int *data_blocks, unsigned int syt)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900740{
741 u32 cip_header[2];
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900742 unsigned int fmt, fdf;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900743 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900744 struct snd_pcm_substream *pcm;
745 unsigned int pcm_frames;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900746 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900747
748 cip_header[0] = be32_to_cpu(buffer[0]);
749 cip_header[1] = be32_to_cpu(buffer[1]);
750
751 /*
752 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900753 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900754 */
755 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900756 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900757 dev_info_ratelimited(&s->unit->device,
758 "Invalid CIP header for AMDTP: %08X:%08X\n",
759 cip_header[0], cip_header[1]);
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900760 *data_blocks = 0;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900761 pcm_frames = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900762 goto end;
763 }
764
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900765 /* Check valid protocol or not. */
766 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
767 if (fmt != s->fmt) {
768 dev_err(&s->unit->device,
769 "Detect unexpected protocol: %08x %08x\n",
770 cip_header[0], cip_header[1]);
771 return -EIO;
772 }
773
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900774 /* Calculate data blocks */
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900775 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900776 if (payload_quadlets < 3 ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900777 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900778 *data_blocks = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900779 } else {
780 data_block_quadlets =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900781 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900782 /* avoid division by zero */
783 if (data_block_quadlets == 0) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900784 dev_err(&s->unit->device,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900785 "Detect invalid value in dbs field: %08X\n",
786 cip_header[0]);
Takashi Sakamotoa9007052015-05-22 23:21:14 +0900787 return -EPROTO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900788 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900789 if (s->flags & CIP_WRONG_DBS)
790 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900791
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900792 *data_blocks = (payload_quadlets - 2) / data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900793 }
794
795 /* Check data block counter continuity */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900796 data_block_counter = cip_header[0] & CIP_DBC_MASK;
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900797 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900798 s->data_block_counter != UINT_MAX)
799 data_block_counter = s->data_block_counter;
800
Takashi Sakamoto18f5ed32015-08-05 09:21:05 +0900801 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
802 data_block_counter == s->tx_first_dbc) ||
803 s->data_block_counter == UINT_MAX) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900804 lost = false;
805 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900806 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900807 } else {
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900808 if ((*data_blocks > 0) && (s->tx_dbc_interval > 0))
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900809 dbc_interval = s->tx_dbc_interval;
810 else
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900811 dbc_interval = *data_blocks;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900812
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900813 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900814 ((s->data_block_counter + dbc_interval) & 0xff);
815 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900816
817 if (lost) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900818 dev_err(&s->unit->device,
819 "Detect discontinuity of CIP: %02X %02X\n",
820 s->data_block_counter, data_block_counter);
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900821 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900822 }
823
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900824 pcm_frames = process_tx_data_blocks(s, buffer + 2, *data_blocks, &syt);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900825
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900826 if (s->flags & CIP_DBC_IS_END_EVENT)
827 s->data_block_counter = data_block_counter;
828 else
829 s->data_block_counter =
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900830 (data_block_counter + *data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900831end:
832 if (queue_in_packet(s) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900833 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900834
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900835 pcm = ACCESS_ONCE(s->pcm);
836 if (pcm && pcm_frames > 0)
837 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900838
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900839 return 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900840}
841
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900842static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
843 size_t header_length, void *header,
844 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100845{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900846 struct amdtp_stream *s = private_data;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900847 unsigned int i, syt, packets = header_length / 4;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900848 unsigned int data_blocks;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100849
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900850 if (s->packet_index < 0)
851 return;
852
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100853 /*
854 * Compute the cycle of the last queued packet.
855 * (We need only the four lowest bits for the SYT, so we can ignore
856 * that bits 0-11 must wrap around at 3072.)
857 */
858 cycle += QUEUE_LENGTH - packets;
859
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900860 for (i = 0; i < packets; ++i) {
861 syt = calculate_syt(s, ++cycle);
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900862 data_blocks = calculate_data_blocks(s, syt);
863
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900864 if (handle_out_packet(s, data_blocks, syt) < 0) {
865 s->packet_index = -1;
866 amdtp_stream_pcm_abort(s);
867 return;
868 }
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900869 }
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900870
Clemens Ladisch13882a82011-05-02 09:33:56 +0200871 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100872}
873
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900874static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
875 size_t header_length, void *header,
876 void *private_data)
877{
878 struct amdtp_stream *s = private_data;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900879 unsigned int p, syt, packets;
880 unsigned int payload_quadlets, max_payload_quadlets;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900881 unsigned int data_blocks;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900882 __be32 *buffer, *headers = header;
883
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900884 if (s->packet_index < 0)
885 return;
886
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900887 /* The number of packets in buffer */
888 packets = header_length / IN_PACKET_HEADER_SIZE;
889
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900890 /* For buffer-over-run prevention. */
891 max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4;
892
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900893 for (p = 0; p < packets; p++) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900894 buffer = s->buffer.packets[s->packet_index].buffer;
895
896 /* The number of quadlets in this packet */
897 payload_quadlets =
898 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900899 if (payload_quadlets > max_payload_quadlets) {
900 dev_err(&s->unit->device,
901 "Detect jumbo payload: %02x %02x\n",
902 payload_quadlets, max_payload_quadlets);
903 s->packet_index = -1;
904 break;
905 }
906
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900907 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900908 if (handle_in_packet(s, payload_quadlets, buffer,
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900909 &data_blocks, syt) < 0) {
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900910 s->packet_index = -1;
911 break;
912 }
913
914 /* Process sync slave stream */
915 if (s->sync_slave && s->sync_slave->callbacked) {
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900916 if (handle_out_packet(s->sync_slave,
917 data_blocks, syt) < 0) {
918 s->packet_index = -1;
919 break;
920 }
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900921 }
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900922 }
923
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900924 /* Queueing error or detecting discontinuity */
925 if (s->packet_index < 0) {
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900926 amdtp_stream_pcm_abort(s);
927
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900928 /* Abort sync slave. */
929 if (s->sync_slave) {
930 s->sync_slave->packet_index = -1;
931 amdtp_stream_pcm_abort(s->sync_slave);
932 }
933 return;
934 }
935
936 /* when sync to device, flush the packets for slave stream */
937 if (s->sync_slave && s->sync_slave->callbacked)
938 fw_iso_context_queue_flush(s->sync_slave->context);
939
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900940 fw_iso_context_queue_flush(s->context);
941}
942
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900943/* processing is done by master callback */
944static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
945 size_t header_length, void *header,
946 void *private_data)
947{
948 return;
949}
950
951/* this is executed one time */
952static void amdtp_stream_first_callback(struct fw_iso_context *context,
953 u32 cycle, size_t header_length,
954 void *header, void *private_data)
955{
956 struct amdtp_stream *s = private_data;
957
958 /*
959 * For in-stream, first packet has come.
960 * For out-stream, prepared to transmit first packet
961 */
962 s->callbacked = true;
963 wake_up(&s->callback_wait);
964
965 if (s->direction == AMDTP_IN_STREAM)
966 context->callback.sc = in_stream_callback;
Takashi Sakamoto727d3a02015-05-22 23:00:54 +0900967 else if (s->flags & CIP_SYNC_TO_DEVICE)
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900968 context->callback.sc = slave_stream_callback;
969 else
970 context->callback.sc = out_stream_callback;
971
972 context->callback.sc(context, cycle, header_length, header, s);
973}
974
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100975/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900976 * amdtp_stream_start - start transferring packets
977 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100978 * @channel: the isochronous channel on the bus
979 * @speed: firewire speed code
980 *
981 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900982 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
983 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100984 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900985int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100986{
987 static const struct {
988 unsigned int data_block;
989 unsigned int syt_offset;
990 } initial_state[] = {
991 [CIP_SFC_32000] = { 4, 3072 },
992 [CIP_SFC_48000] = { 6, 1024 },
993 [CIP_SFC_96000] = { 12, 1024 },
994 [CIP_SFC_192000] = { 24, 1024 },
995 [CIP_SFC_44100] = { 0, 67 },
996 [CIP_SFC_88200] = { 0, 67 },
997 [CIP_SFC_176400] = { 0, 67 },
998 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900999 unsigned int header_size;
1000 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +09001001 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001002
1003 mutex_lock(&s->mutex);
1004
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001005 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +09001006 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001007 err = -EBADFD;
1008 goto err_unlock;
1009 }
1010
Takashi Sakamotob6bc8122014-04-25 22:45:16 +09001011 if (s->direction == AMDTP_IN_STREAM &&
1012 s->flags & CIP_SKIP_INIT_DBC_CHECK)
1013 s->data_block_counter = UINT_MAX;
1014 else
1015 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001016 s->data_block_state = initial_state[s->sfc].data_block;
1017 s->syt_offset_state = initial_state[s->sfc].syt_offset;
1018 s->last_syt_offset = TICKS_PER_CYCLE;
1019
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001020 /* initialize packet buffer */
1021 if (s->direction == AMDTP_IN_STREAM) {
1022 dir = DMA_FROM_DEVICE;
1023 type = FW_ISO_CONTEXT_RECEIVE;
1024 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001025 } else {
1026 dir = DMA_TO_DEVICE;
1027 type = FW_ISO_CONTEXT_TRANSMIT;
1028 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001029 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001030 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001031 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001032 if (err < 0)
1033 goto err_unlock;
1034
1035 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001036 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001037 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001038 if (IS_ERR(s->context)) {
1039 err = PTR_ERR(s->context);
1040 if (err == -EBUSY)
1041 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001042 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001043 goto err_buffer;
1044 }
1045
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001046 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001047
Clemens Ladischec00f5e2011-03-15 07:57:24 +01001048 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +09001049 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001050 if (s->direction == AMDTP_IN_STREAM)
1051 err = queue_in_packet(s);
1052 else
1053 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +09001054 if (err < 0)
1055 goto err_context;
1056 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001057
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001058 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +09001059 tag = FW_ISO_CONTEXT_MATCH_TAG1;
1060 if (s->flags & CIP_EMPTY_WITH_TAG0)
1061 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1062
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001063 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +09001064 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001065 if (err < 0)
1066 goto err_context;
1067
1068 mutex_unlock(&s->mutex);
1069
1070 return 0;
1071
1072err_context:
1073 fw_iso_context_destroy(s->context);
1074 s->context = ERR_PTR(-1);
1075err_buffer:
1076 iso_packets_buffer_destroy(&s->buffer, s->unit);
1077err_unlock:
1078 mutex_unlock(&s->mutex);
1079
1080 return err;
1081}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001082EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001083
1084/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001085 * amdtp_stream_pcm_pointer - get the PCM buffer position
1086 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +02001087 *
1088 * Returns the current buffer position, in frames.
1089 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001090unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +02001091{
Clemens Ladisch92b862c2012-05-13 19:07:22 +02001092 /* this optimization is allowed to be racy */
Takashi Sakamotoc8de6db2014-04-25 22:44:53 +09001093 if (s->pointer_flush && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +02001094 fw_iso_context_flush_completions(s->context);
1095 else
1096 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +02001097
1098 return ACCESS_ONCE(s->pcm_buffer_pointer);
1099}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001100EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +02001101
1102/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001103 * amdtp_stream_update - update the stream after a bus reset
1104 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001105 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001106void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001107{
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +09001108 /* Precomputing. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001109 ACCESS_ONCE(s->source_node_id_field) =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +09001110 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
1111 CIP_SID_MASK;
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001112}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001113EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001114
1115/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001116 * amdtp_stream_stop - stop sending packets
1117 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001118 *
1119 * All PCM and MIDI devices of the stream must be stopped before the stream
1120 * itself can be stopped.
1121 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001122void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001123{
1124 mutex_lock(&s->mutex);
1125
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001126 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001127 mutex_unlock(&s->mutex);
1128 return;
1129 }
1130
Clemens Ladisch76fb8782012-05-13 22:03:09 +02001131 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001132 fw_iso_context_stop(s->context);
1133 fw_iso_context_destroy(s->context);
1134 s->context = ERR_PTR(-1);
1135 iso_packets_buffer_destroy(&s->buffer, s->unit);
1136
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001137 s->callbacked = false;
1138
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001139 mutex_unlock(&s->mutex);
1140}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001141EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001142
1143/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001144 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001145 * @s: the AMDTP stream about to be stopped
1146 *
1147 * If the isochronous stream needs to be stopped asynchronously, call this
1148 * function first to stop the PCM device.
1149 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001150void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001151{
1152 struct snd_pcm_substream *pcm;
1153
1154 pcm = ACCESS_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +01001155 if (pcm)
1156 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001157}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001158EXPORT_SYMBOL(amdtp_stream_pcm_abort);