Commit | Line | Data |
---|---|---|
31ef9134 CL |
1 | /* |
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> | |
7b2d99fa | 15 | #include <sound/pcm_params.h> |
83d8d72d | 16 | #include <sound/rawmidi.h> |
d67c46b9 | 17 | #include "amdtp-stream.h" |
31ef9134 CL |
18 | |
19 | #define TICKS_PER_CYCLE 3072 | |
20 | #define CYCLES_PER_SECOND 8000 | |
21 | #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) | |
22 | ||
25ca917c CL |
23 | /* |
24 | * 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 | ||
5c697e5b CL |
29 | /* |
30 | * 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 | ||
ca5b5050 | 35 | #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */ |
31ef9134 | 36 | |
b445db44 TS |
37 | /* isochronous header parameters */ |
38 | #define ISO_DATA_LENGTH_SHIFT 16 | |
31ef9134 CL |
39 | #define TAG_CIP 1 |
40 | ||
b445db44 | 41 | /* common isochronous packet header parameters */ |
9a2820c1 TS |
42 | #define CIP_EOH_SHIFT 31 |
43 | #define CIP_EOH (1u << CIP_EOH_SHIFT) | |
b445db44 | 44 | #define CIP_EOH_MASK 0x80000000 |
9a2820c1 TS |
45 | #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 | |
b445db44 | 51 | #define CIP_FMT_MASK 0x3f000000 |
9a2820c1 TS |
52 | #define CIP_FDF_MASK 0x00ff0000 |
53 | #define CIP_FDF_SHIFT 16 | |
b445db44 TS |
54 | #define CIP_SYT_MASK 0x0000ffff |
55 | #define CIP_SYT_NO_INFO 0xffff | |
b445db44 TS |
56 | |
57 | /* | |
58 | * Audio and Music transfer protocol specific parameters | |
59 | * only "Clock-based rate control mode" is supported | |
60 | */ | |
414ba022 TS |
61 | #define CIP_FMT_AM 0x10 |
62 | #define AMDTP_FDF_AM824 0x00 | |
2b3fc456 | 63 | #define AMDTP_FDF_NO_DATA 0xff |
31ef9134 CL |
64 | |
65 | /* TODO: make these configurable */ | |
66 | #define INTERRUPT_INTERVAL 16 | |
67 | #define QUEUE_LENGTH 48 | |
68 | ||
2b3fc456 | 69 | #define IN_PACKET_HEADER_SIZE 4 |
4b7da117 TS |
70 | #define OUT_PACKET_HEADER_SIZE 0 |
71 | ||
76fb8789 CL |
72 | static void pcm_period_tasklet(unsigned long data); |
73 | ||
31ef9134 | 74 | /** |
be4a2894 TS |
75 | * amdtp_stream_init - initialize an AMDTP stream structure |
76 | * @s: the AMDTP stream to initialize | |
31ef9134 | 77 | * @unit: the target of the stream |
3ff7e8f0 | 78 | * @dir: the direction of stream |
31ef9134 | 79 | * @flags: the packet transmission method to use |
5955815e | 80 | * @fmt: the value of fmt field in CIP header |
31ef9134 | 81 | */ |
be4a2894 | 82 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
5955815e TS |
83 | enum amdtp_stream_direction dir, enum cip_flags flags, |
84 | unsigned int fmt) | |
31ef9134 | 85 | { |
c6f224dc | 86 | s->unit = unit; |
3ff7e8f0 | 87 | s->direction = dir; |
31ef9134 CL |
88 | s->flags = flags; |
89 | s->context = ERR_PTR(-1); | |
90 | mutex_init(&s->mutex); | |
76fb8789 | 91 | tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); |
ec00f5e4 | 92 | s->packet_index = 0; |
31ef9134 | 93 | |
7b3b0d85 TS |
94 | init_waitqueue_head(&s->callback_wait); |
95 | s->callbacked = false; | |
96 | s->sync_slave = NULL; | |
97 | ||
5955815e | 98 | s->fmt = fmt; |
414ba022 | 99 | |
31ef9134 CL |
100 | return 0; |
101 | } | |
be4a2894 | 102 | EXPORT_SYMBOL(amdtp_stream_init); |
31ef9134 CL |
103 | |
104 | /** | |
be4a2894 TS |
105 | * amdtp_stream_destroy - free stream resources |
106 | * @s: the AMDTP stream to destroy | |
31ef9134 | 107 | */ |
be4a2894 | 108 | void amdtp_stream_destroy(struct amdtp_stream *s) |
31ef9134 | 109 | { |
be4a2894 | 110 | WARN_ON(amdtp_stream_running(s)); |
31ef9134 | 111 | mutex_destroy(&s->mutex); |
31ef9134 | 112 | } |
be4a2894 | 113 | EXPORT_SYMBOL(amdtp_stream_destroy); |
31ef9134 | 114 | |
c5280e99 | 115 | const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { |
a7304e3b CL |
116 | [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 | }; | |
124 | EXPORT_SYMBOL(amdtp_syt_intervals); | |
125 | ||
f9503a68 | 126 | const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { |
1017abed TS |
127 | [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 | }; | |
135 | EXPORT_SYMBOL(amdtp_rate_table); | |
136 | ||
7b2d99fa TS |
137 | /** |
138 | * 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 | */ | |
142 | int 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 | |
ce991981 | 176 | * preferrable to align period/buffer sizes to current SYT_INTERVAL. |
7b2d99fa | 177 | * |
ce991981 YG |
178 | * TODO: These constraints can be improved with proper rules. |
179 | * Currently apply LCM of SYT_INTERVALs. | |
7b2d99fa TS |
180 | */ |
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); | |
187 | end: | |
188 | return err; | |
189 | } | |
190 | EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); | |
191 | ||
31ef9134 | 192 | /** |
be4a2894 TS |
193 | * amdtp_stream_set_parameters - set stream parameters |
194 | * @s: the AMDTP stream to configure | |
31ef9134 | 195 | * @rate: the sample rate |
a7304e3b CL |
196 | * @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) | |
27ec83b5 | 199 | * @double_pcm_frames: one data block transfers two PCM frames |
31ef9134 | 200 | * |
a7304e3b | 201 | * The parameters must be set before the stream is started, and must not be |
31ef9134 CL |
202 | * changed while the stream is running. |
203 | */ | |
547e631c TS |
204 | int amdtp_stream_set_parameters(struct amdtp_stream *s, |
205 | unsigned int rate, | |
206 | unsigned int pcm_channels, | |
27ec83b5 TS |
207 | unsigned int midi_ports, |
208 | bool double_pcm_frames) | |
31ef9134 | 209 | { |
77d2a8a4 | 210 | unsigned int i, sfc, midi_channels; |
31ef9134 | 211 | |
83d8d72d TS |
212 | midi_channels = DIV_ROUND_UP(midi_ports, 8); |
213 | ||
d67c46b9 TS |
214 | if (WARN_ON(amdtp_stream_running(s)) || |
215 | WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) || | |
83d8d72d | 216 | WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI)) |
547e631c | 217 | return -EINVAL; |
31ef9134 | 218 | |
547e631c | 219 | for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { |
1017abed | 220 | if (amdtp_rate_table[sfc] == rate) |
547e631c TS |
221 | break; |
222 | } | |
223 | if (sfc == ARRAY_SIZE(amdtp_rate_table)) | |
224 | return -EINVAL; | |
e84d15f6 | 225 | |
10550bea | 226 | s->pcm_channels = pcm_channels; |
e84d15f6 | 227 | s->sfc = sfc; |
77d2a8a4 | 228 | s->data_block_quadlets = s->pcm_channels + midi_channels; |
a7304e3b CL |
229 | s->midi_ports = midi_ports; |
230 | ||
414ba022 TS |
231 | s->fdf = AMDTP_FDF_AM824 | s->sfc; |
232 | ||
6a4e89ff TS |
233 | /* |
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 | ||
a7304e3b | 243 | s->syt_interval = amdtp_syt_intervals[sfc]; |
e84d15f6 CL |
244 | |
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; | |
77d2a8a4 TS |
250 | |
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; | |
25ca917c CL |
255 | |
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; | |
547e631c TS |
263 | |
264 | return 0; | |
31ef9134 | 265 | } |
be4a2894 | 266 | EXPORT_SYMBOL(amdtp_stream_set_parameters); |
31ef9134 CL |
267 | |
268 | /** | |
be4a2894 TS |
269 | * amdtp_stream_get_max_payload - get the stream's packet size |
270 | * @s: the AMDTP stream | |
31ef9134 CL |
271 | * |
272 | * This function must not be called before the stream has been configured | |
be4a2894 | 273 | * with amdtp_stream_set_parameters(). |
31ef9134 | 274 | */ |
be4a2894 | 275 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) |
31ef9134 | 276 | { |
a2064710 TS |
277 | 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; | |
31ef9134 | 283 | } |
be4a2894 | 284 | EXPORT_SYMBOL(amdtp_stream_get_max_payload); |
31ef9134 | 285 | |
29bcae20 TS |
286 | static void write_pcm_s16(struct amdtp_stream *s, |
287 | struct snd_pcm_substream *pcm, | |
288 | __be32 *buffer, unsigned int frames); | |
289 | static void write_pcm_s32(struct amdtp_stream *s, | |
290 | struct snd_pcm_substream *pcm, | |
291 | __be32 *buffer, unsigned int frames); | |
292 | static void read_pcm_s32(struct amdtp_stream *s, | |
293 | struct snd_pcm_substream *pcm, | |
294 | __be32 *buffer, unsigned int frames); | |
31ef9134 CL |
295 | |
296 | /** | |
be4a2894 TS |
297 | * amdtp_stream_set_pcm_format - set the PCM format |
298 | * @s: the AMDTP stream to configure | |
31ef9134 CL |
299 | * @format: the format of the ALSA PCM device |
300 | * | |
ce991981 | 301 | * The sample format must be set after the other parameters (rate/PCM channels/ |
a7304e3b CL |
302 | * MIDI) and before the stream is started, and must not be changed while the |
303 | * stream is running. | |
31ef9134 | 304 | */ |
be4a2894 TS |
305 | void amdtp_stream_set_pcm_format(struct amdtp_stream *s, |
306 | snd_pcm_format_t format) | |
31ef9134 | 307 | { |
83d8d72d | 308 | if (WARN_ON(amdtp_stream_pcm_running(s))) |
31ef9134 CL |
309 | return; |
310 | ||
311 | switch (format) { | |
312 | default: | |
313 | WARN_ON(1); | |
314 | /* fall through */ | |
315 | case SNDRV_PCM_FORMAT_S16: | |
2b3fc456 | 316 | if (s->direction == AMDTP_OUT_STREAM) { |
29bcae20 | 317 | s->transfer_samples = write_pcm_s16; |
2b3fc456 TS |
318 | break; |
319 | } | |
320 | WARN_ON(1); | |
321 | /* fall through */ | |
31ef9134 | 322 | case SNDRV_PCM_FORMAT_S32: |
10550bea | 323 | if (s->direction == AMDTP_OUT_STREAM) |
29bcae20 | 324 | s->transfer_samples = write_pcm_s32; |
10550bea | 325 | else |
29bcae20 | 326 | s->transfer_samples = read_pcm_s32; |
31ef9134 CL |
327 | break; |
328 | } | |
329 | } | |
be4a2894 | 330 | EXPORT_SYMBOL(amdtp_stream_set_pcm_format); |
31ef9134 | 331 | |
76fb8789 | 332 | /** |
be4a2894 TS |
333 | * amdtp_stream_pcm_prepare - prepare PCM device for running |
334 | * @s: the AMDTP stream | |
76fb8789 CL |
335 | * |
336 | * This function should be called from the PCM device's .prepare callback. | |
337 | */ | |
be4a2894 | 338 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s) |
76fb8789 CL |
339 | { |
340 | tasklet_kill(&s->period_tasklet); | |
341 | s->pcm_buffer_pointer = 0; | |
342 | s->pcm_period_pointer = 0; | |
92b862c7 | 343 | s->pointer_flush = true; |
76fb8789 | 344 | } |
be4a2894 | 345 | EXPORT_SYMBOL(amdtp_stream_pcm_prepare); |
76fb8789 | 346 | |
875be091 TS |
347 | static unsigned int calculate_data_blocks(struct amdtp_stream *s, |
348 | unsigned int syt) | |
31ef9134 CL |
349 | { |
350 | unsigned int phase, data_blocks; | |
351 | ||
875be091 TS |
352 | /* 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. */ | |
31ef9134 | 360 | } else { |
875be091 TS |
361 | 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; | |
31ef9134 CL |
366 | |
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 | */ | |
875be091 TS |
375 | 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 | } | |
31ef9134 CL |
386 | } |
387 | ||
388 | return data_blocks; | |
389 | } | |
390 | ||
be4a2894 | 391 | static unsigned int calculate_syt(struct amdtp_stream *s, |
31ef9134 CL |
392 | 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 | ||
be454366 | 423 | if (syt_offset < TICKS_PER_CYCLE) { |
e84d15f6 | 424 | syt_offset += s->transfer_delay; |
be454366 CL |
425 | syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; |
426 | syt += syt_offset % TICKS_PER_CYCLE; | |
31ef9134 | 427 | |
b445db44 | 428 | return syt & CIP_SYT_MASK; |
be454366 | 429 | } else { |
b445db44 | 430 | return CIP_SYT_NO_INFO; |
be454366 | 431 | } |
31ef9134 CL |
432 | } |
433 | ||
29bcae20 TS |
434 | static void write_pcm_s32(struct amdtp_stream *s, |
435 | struct snd_pcm_substream *pcm, | |
436 | __be32 *buffer, unsigned int frames) | |
31ef9134 CL |
437 | { |
438 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
77d2a8a4 | 439 | unsigned int channels, remaining_frames, i, c; |
31ef9134 CL |
440 | const u32 *src; |
441 | ||
442 | channels = s->pcm_channels; | |
443 | src = (void *)runtime->dma_area + | |
e84841f9 | 444 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
31ef9134 | 445 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
31ef9134 CL |
446 | |
447 | for (i = 0; i < frames; ++i) { | |
448 | for (c = 0; c < channels; ++c) { | |
77d2a8a4 TS |
449 | buffer[s->pcm_positions[c]] = |
450 | cpu_to_be32((*src >> 8) | 0x40000000); | |
31ef9134 | 451 | src++; |
31ef9134 | 452 | } |
77d2a8a4 | 453 | buffer += s->data_block_quadlets; |
31ef9134 CL |
454 | if (--remaining_frames == 0) |
455 | src = (void *)runtime->dma_area; | |
456 | } | |
457 | } | |
458 | ||
29bcae20 TS |
459 | static void write_pcm_s16(struct amdtp_stream *s, |
460 | struct snd_pcm_substream *pcm, | |
461 | __be32 *buffer, unsigned int frames) | |
31ef9134 CL |
462 | { |
463 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
77d2a8a4 | 464 | unsigned int channels, remaining_frames, i, c; |
31ef9134 CL |
465 | const u16 *src; |
466 | ||
467 | channels = s->pcm_channels; | |
468 | src = (void *)runtime->dma_area + | |
e84841f9 | 469 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
31ef9134 | 470 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
31ef9134 CL |
471 | |
472 | for (i = 0; i < frames; ++i) { | |
473 | for (c = 0; c < channels; ++c) { | |
77d2a8a4 | 474 | buffer[s->pcm_positions[c]] = |
a6975f2a | 475 | cpu_to_be32((*src << 8) | 0x42000000); |
31ef9134 | 476 | src++; |
31ef9134 | 477 | } |
77d2a8a4 | 478 | buffer += s->data_block_quadlets; |
31ef9134 CL |
479 | if (--remaining_frames == 0) |
480 | src = (void *)runtime->dma_area; | |
481 | } | |
482 | } | |
483 | ||
29bcae20 TS |
484 | static void read_pcm_s32(struct amdtp_stream *s, |
485 | struct snd_pcm_substream *pcm, | |
486 | __be32 *buffer, unsigned int frames) | |
2b3fc456 TS |
487 | { |
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) { | |
77d2a8a4 | 499 | *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8; |
2b3fc456 TS |
500 | dst++; |
501 | } | |
502 | buffer += s->data_block_quadlets; | |
503 | if (--remaining_frames == 0) | |
504 | dst = (void *)runtime->dma_area; | |
505 | } | |
506 | } | |
507 | ||
29bcae20 TS |
508 | static void write_pcm_silence(struct amdtp_stream *s, |
509 | __be32 *buffer, unsigned int frames) | |
31ef9134 CL |
510 | { |
511 | unsigned int i, c; | |
512 | ||
513 | for (i = 0; i < frames; ++i) { | |
514 | for (c = 0; c < s->pcm_channels; ++c) | |
77d2a8a4 | 515 | buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000); |
31ef9134 CL |
516 | buffer += s->data_block_quadlets; |
517 | } | |
518 | } | |
519 | ||
25ca917c CL |
520 | /* |
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 | */ | |
530 | static 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 | ||
545 | static 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 | ||
29bcae20 TS |
550 | static void write_midi_messages(struct amdtp_stream *s, |
551 | __be32 *buffer, unsigned int frames) | |
31ef9134 | 552 | { |
83d8d72d TS |
553 | unsigned int f, port; |
554 | u8 *b; | |
555 | ||
556 | for (f = 0; f < frames; f++) { | |
77d2a8a4 | 557 | b = (u8 *)&buffer[s->midi_position]; |
83d8d72d TS |
558 | |
559 | port = (s->data_block_counter + f) % 8; | |
25ca917c CL |
560 | 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); | |
83d8d72d | 565 | b[0] = 0x81; |
25ca917c CL |
566 | } else { |
567 | b[0] = 0x80; | |
568 | b[1] = 0; | |
569 | } | |
570 | b[2] = 0; | |
571 | b[3] = 0; | |
83d8d72d TS |
572 | |
573 | buffer += s->data_block_quadlets; | |
574 | } | |
575 | } | |
576 | ||
29bcae20 TS |
577 | static void read_midi_messages(struct amdtp_stream *s, |
578 | __be32 *buffer, unsigned int frames) | |
83d8d72d TS |
579 | { |
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; | |
77d2a8a4 | 586 | b = (u8 *)&buffer[s->midi_position]; |
31ef9134 | 587 | |
83d8d72d TS |
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 | } | |
31ef9134 CL |
594 | } |
595 | ||
4b7da117 TS |
596 | static void update_pcm_pointers(struct amdtp_stream *s, |
597 | struct snd_pcm_substream *pcm, | |
598 | unsigned int frames) | |
65845f29 TS |
599 | { |
600 | unsigned int ptr; | |
601 | ||
4b7da117 TS |
602 | 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 | ||
615 | static 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 | ||
624 | static 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}; | |
7b3b0d85 TS |
629 | int err = 0; |
630 | ||
631 | if (IS_ERR(s->context)) | |
632 | goto end; | |
4b7da117 TS |
633 | |
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; | |
648 | end: | |
649 | return err; | |
650 | } | |
651 | ||
652 | static 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 | ||
2b3fc456 TS |
659 | static 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 | ||
20e44577 TS |
665 | unsigned 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 | ||
a4103bd7 TS |
685 | static int handle_out_packet(struct amdtp_stream *s, unsigned int data_blocks, |
686 | unsigned int syt) | |
31ef9134 CL |
687 | { |
688 | __be32 *buffer; | |
6fc6b9ce | 689 | unsigned int payload_length; |
20e44577 | 690 | unsigned int pcm_frames; |
31ef9134 | 691 | struct snd_pcm_substream *pcm; |
31ef9134 | 692 | |
ccccad86 | 693 | buffer = s->buffer.packets[s->packet_index].buffer; |
20e44577 TS |
694 | pcm_frames = process_rx_data_blocks(s, buffer + 2, data_blocks, &syt); |
695 | ||
31ef9134 | 696 | buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | |
9a2820c1 | 697 | (s->data_block_quadlets << CIP_DBS_SHIFT) | |
31ef9134 | 698 | s->data_block_counter); |
414ba022 TS |
699 | 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)); | |
31ef9134 CL |
703 | |
704 | s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; | |
705 | ||
4b7da117 | 706 | payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; |
a4103bd7 TS |
707 | if (queue_out_packet(s, payload_length, false) < 0) |
708 | return -EIO; | |
31ef9134 | 709 | |
20e44577 TS |
710 | pcm = ACCESS_ONCE(s->pcm); |
711 | if (pcm && pcm_frames > 0) | |
712 | update_pcm_pointers(s, pcm, pcm_frames); | |
a4103bd7 TS |
713 | |
714 | /* No need to return the number of handled data blocks. */ | |
715 | return 0; | |
76fb8789 CL |
716 | } |
717 | ||
20e44577 TS |
718 | unsigned 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 | ||
6fc6b9ce | 737 | static int handle_in_packet(struct amdtp_stream *s, |
31ea49ba | 738 | unsigned int payload_quadlets, __be32 *buffer, |
20e44577 | 739 | unsigned int *data_blocks, unsigned int syt) |
2b3fc456 TS |
740 | { |
741 | u32 cip_header[2]; | |
414ba022 | 742 | unsigned int fmt, fdf; |
6fc6b9ce | 743 | unsigned int data_block_quadlets, data_block_counter, dbc_interval; |
20e44577 TS |
744 | struct snd_pcm_substream *pcm; |
745 | unsigned int pcm_frames; | |
c8bdf49b | 746 | bool lost; |
2b3fc456 TS |
747 | |
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'. | |
77d2a8a4 | 753 | * For convenience, also check FMT field is AM824 or not. |
2b3fc456 TS |
754 | */ |
755 | if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || | |
414ba022 | 756 | ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) { |
2b3fc456 TS |
757 | dev_info_ratelimited(&s->unit->device, |
758 | "Invalid CIP header for AMDTP: %08X:%08X\n", | |
759 | cip_header[0], cip_header[1]); | |
31ea49ba | 760 | *data_blocks = 0; |
20e44577 | 761 | pcm_frames = 0; |
2b3fc456 TS |
762 | goto end; |
763 | } | |
764 | ||
414ba022 TS |
765 | /* 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 | ||
2b3fc456 | 774 | /* Calculate data blocks */ |
414ba022 | 775 | fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT; |
2b3fc456 | 776 | if (payload_quadlets < 3 || |
414ba022 | 777 | (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) { |
31ea49ba | 778 | *data_blocks = 0; |
2b3fc456 TS |
779 | } else { |
780 | data_block_quadlets = | |
9a2820c1 | 781 | (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; |
2b3fc456 TS |
782 | /* avoid division by zero */ |
783 | if (data_block_quadlets == 0) { | |
12e0f438 | 784 | dev_err(&s->unit->device, |
2b3fc456 TS |
785 | "Detect invalid value in dbs field: %08X\n", |
786 | cip_header[0]); | |
a9007054 | 787 | return -EPROTO; |
2b3fc456 | 788 | } |
69702239 TS |
789 | if (s->flags & CIP_WRONG_DBS) |
790 | data_block_quadlets = s->data_block_quadlets; | |
2b3fc456 | 791 | |
31ea49ba | 792 | *data_blocks = (payload_quadlets - 2) / data_block_quadlets; |
2b3fc456 TS |
793 | } |
794 | ||
795 | /* Check data block counter continuity */ | |
9a2820c1 | 796 | data_block_counter = cip_header[0] & CIP_DBC_MASK; |
31ea49ba | 797 | if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && |
9d59124c TS |
798 | s->data_block_counter != UINT_MAX) |
799 | data_block_counter = s->data_block_counter; | |
800 | ||
18f5ed36 TS |
801 | if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && |
802 | data_block_counter == s->tx_first_dbc) || | |
803 | s->data_block_counter == UINT_MAX) { | |
b84b1a27 TS |
804 | lost = false; |
805 | } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { | |
c8bdf49b | 806 | lost = data_block_counter != s->data_block_counter; |
d9cd0065 | 807 | } else { |
31ea49ba | 808 | if ((*data_blocks > 0) && (s->tx_dbc_interval > 0)) |
d9cd0065 TS |
809 | dbc_interval = s->tx_dbc_interval; |
810 | else | |
31ea49ba | 811 | dbc_interval = *data_blocks; |
d9cd0065 | 812 | |
c8bdf49b | 813 | lost = data_block_counter != |
d9cd0065 TS |
814 | ((s->data_block_counter + dbc_interval) & 0xff); |
815 | } | |
c8bdf49b TS |
816 | |
817 | if (lost) { | |
12e0f438 TS |
818 | dev_err(&s->unit->device, |
819 | "Detect discontinuity of CIP: %02X %02X\n", | |
820 | s->data_block_counter, data_block_counter); | |
6fc6b9ce | 821 | return -EIO; |
2b3fc456 TS |
822 | } |
823 | ||
20e44577 | 824 | pcm_frames = process_tx_data_blocks(s, buffer + 2, *data_blocks, &syt); |
2b3fc456 | 825 | |
c8bdf49b TS |
826 | if (s->flags & CIP_DBC_IS_END_EVENT) |
827 | s->data_block_counter = data_block_counter; | |
828 | else | |
829 | s->data_block_counter = | |
31ea49ba | 830 | (data_block_counter + *data_blocks) & 0xff; |
2b3fc456 TS |
831 | end: |
832 | if (queue_in_packet(s) < 0) | |
6fc6b9ce | 833 | return -EIO; |
2b3fc456 | 834 | |
20e44577 TS |
835 | pcm = ACCESS_ONCE(s->pcm); |
836 | if (pcm && pcm_frames > 0) | |
837 | update_pcm_pointers(s, pcm, pcm_frames); | |
2b3fc456 | 838 | |
31ea49ba | 839 | return 0; |
2b3fc456 TS |
840 | } |
841 | ||
4b7da117 TS |
842 | static void out_stream_callback(struct fw_iso_context *context, u32 cycle, |
843 | size_t header_length, void *header, | |
844 | void *private_data) | |
31ef9134 | 845 | { |
be4a2894 | 846 | struct amdtp_stream *s = private_data; |
ccccad86 | 847 | unsigned int i, syt, packets = header_length / 4; |
6fc6b9ce | 848 | unsigned int data_blocks; |
31ef9134 | 849 | |
a4103bd7 TS |
850 | if (s->packet_index < 0) |
851 | return; | |
852 | ||
31ef9134 CL |
853 | /* |
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 | ||
ccccad86 TS |
860 | for (i = 0; i < packets; ++i) { |
861 | syt = calculate_syt(s, ++cycle); | |
6fc6b9ce TS |
862 | data_blocks = calculate_data_blocks(s, syt); |
863 | ||
a4103bd7 TS |
864 | if (handle_out_packet(s, data_blocks, syt) < 0) { |
865 | s->packet_index = -1; | |
866 | amdtp_stream_pcm_abort(s); | |
867 | return; | |
868 | } | |
ccccad86 | 869 | } |
a4103bd7 | 870 | |
13882a82 | 871 | fw_iso_context_queue_flush(s->context); |
31ef9134 CL |
872 | } |
873 | ||
2b3fc456 TS |
874 | static 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; | |
a2064710 TS |
879 | unsigned int p, syt, packets; |
880 | unsigned int payload_quadlets, max_payload_quadlets; | |
6fc6b9ce | 881 | unsigned int data_blocks; |
2b3fc456 TS |
882 | __be32 *buffer, *headers = header; |
883 | ||
a4103bd7 TS |
884 | if (s->packet_index < 0) |
885 | return; | |
886 | ||
2b3fc456 TS |
887 | /* The number of packets in buffer */ |
888 | packets = header_length / IN_PACKET_HEADER_SIZE; | |
889 | ||
a2064710 TS |
890 | /* For buffer-over-run prevention. */ |
891 | max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4; | |
892 | ||
2b3fc456 | 893 | for (p = 0; p < packets; p++) { |
2b3fc456 TS |
894 | 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; | |
a2064710 TS |
899 | 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 | ||
20e44577 | 907 | syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK; |
31ea49ba | 908 | if (handle_in_packet(s, payload_quadlets, buffer, |
20e44577 | 909 | &data_blocks, syt) < 0) { |
6fc6b9ce TS |
910 | s->packet_index = -1; |
911 | break; | |
912 | } | |
913 | ||
914 | /* Process sync slave stream */ | |
915 | if (s->sync_slave && s->sync_slave->callbacked) { | |
a4103bd7 TS |
916 | if (handle_out_packet(s->sync_slave, |
917 | data_blocks, syt) < 0) { | |
918 | s->packet_index = -1; | |
919 | break; | |
920 | } | |
6fc6b9ce | 921 | } |
2b3fc456 TS |
922 | } |
923 | ||
7b3b0d85 TS |
924 | /* Queueing error or detecting discontinuity */ |
925 | if (s->packet_index < 0) { | |
6fc6b9ce TS |
926 | amdtp_stream_pcm_abort(s); |
927 | ||
7b3b0d85 TS |
928 | /* 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 | ||
2b3fc456 TS |
940 | fw_iso_context_queue_flush(s->context); |
941 | } | |
942 | ||
7b3b0d85 TS |
943 | /* processing is done by master callback */ |
944 | static 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 */ | |
952 | static 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; | |
727d3a0b | 967 | else if (s->flags & CIP_SYNC_TO_DEVICE) |
7b3b0d85 TS |
968 | 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 | ||
31ef9134 | 975 | /** |
be4a2894 TS |
976 | * amdtp_stream_start - start transferring packets |
977 | * @s: the AMDTP stream to start | |
31ef9134 CL |
978 | * @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 | |
be4a2894 TS |
982 | * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI |
983 | * device can be started. | |
31ef9134 | 984 | */ |
be4a2894 | 985 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) |
31ef9134 CL |
986 | { |
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 | }; | |
2b3fc456 TS |
999 | unsigned int header_size; |
1000 | enum dma_data_direction dir; | |
7ab56645 | 1001 | int type, tag, err; |
31ef9134 CL |
1002 | |
1003 | mutex_lock(&s->mutex); | |
1004 | ||
be4a2894 | 1005 | if (WARN_ON(amdtp_stream_running(s) || |
4b7da117 | 1006 | (s->data_block_quadlets < 1))) { |
31ef9134 CL |
1007 | err = -EBADFD; |
1008 | goto err_unlock; | |
1009 | } | |
1010 | ||
b6bc8123 TS |
1011 | 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; | |
31ef9134 CL |
1016 | 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 | ||
2b3fc456 TS |
1020 | /* 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; | |
2b3fc456 TS |
1025 | } else { |
1026 | dir = DMA_TO_DEVICE; | |
1027 | type = FW_ISO_CONTEXT_TRANSMIT; | |
1028 | header_size = OUT_PACKET_HEADER_SIZE; | |
2b3fc456 | 1029 | } |
31ef9134 | 1030 | err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, |
2b3fc456 | 1031 | amdtp_stream_get_max_payload(s), dir); |
31ef9134 CL |
1032 | if (err < 0) |
1033 | goto err_unlock; | |
1034 | ||
1035 | s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, | |
2b3fc456 | 1036 | type, channel, speed, header_size, |
7b3b0d85 | 1037 | amdtp_stream_first_callback, s); |
31ef9134 CL |
1038 | if (IS_ERR(s->context)) { |
1039 | err = PTR_ERR(s->context); | |
1040 | if (err == -EBUSY) | |
1041 | dev_err(&s->unit->device, | |
be4a2894 | 1042 | "no free stream on this controller\n"); |
31ef9134 CL |
1043 | goto err_buffer; |
1044 | } | |
1045 | ||
be4a2894 | 1046 | amdtp_stream_update(s); |
31ef9134 | 1047 | |
ec00f5e4 | 1048 | s->packet_index = 0; |
4b7da117 | 1049 | do { |
2b3fc456 TS |
1050 | if (s->direction == AMDTP_IN_STREAM) |
1051 | err = queue_in_packet(s); | |
1052 | else | |
1053 | err = queue_out_packet(s, 0, true); | |
4b7da117 TS |
1054 | if (err < 0) |
1055 | goto err_context; | |
1056 | } while (s->packet_index > 0); | |
31ef9134 | 1057 | |
2b3fc456 | 1058 | /* NOTE: TAG1 matches CIP. This just affects in stream. */ |
7ab56645 TS |
1059 | tag = FW_ISO_CONTEXT_MATCH_TAG1; |
1060 | if (s->flags & CIP_EMPTY_WITH_TAG0) | |
1061 | tag |= FW_ISO_CONTEXT_MATCH_TAG0; | |
1062 | ||
7b3b0d85 | 1063 | s->callbacked = false; |
7ab56645 | 1064 | err = fw_iso_context_start(s->context, -1, 0, tag); |
31ef9134 CL |
1065 | if (err < 0) |
1066 | goto err_context; | |
1067 | ||
1068 | mutex_unlock(&s->mutex); | |
1069 | ||
1070 | return 0; | |
1071 | ||
1072 | err_context: | |
1073 | fw_iso_context_destroy(s->context); | |
1074 | s->context = ERR_PTR(-1); | |
1075 | err_buffer: | |
1076 | iso_packets_buffer_destroy(&s->buffer, s->unit); | |
1077 | err_unlock: | |
1078 | mutex_unlock(&s->mutex); | |
1079 | ||
1080 | return err; | |
1081 | } | |
be4a2894 | 1082 | EXPORT_SYMBOL(amdtp_stream_start); |
31ef9134 | 1083 | |
e9148ddd | 1084 | /** |
be4a2894 TS |
1085 | * amdtp_stream_pcm_pointer - get the PCM buffer position |
1086 | * @s: the AMDTP stream that transports the PCM data | |
e9148ddd CL |
1087 | * |
1088 | * Returns the current buffer position, in frames. | |
1089 | */ | |
be4a2894 | 1090 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) |
e9148ddd | 1091 | { |
92b862c7 | 1092 | /* this optimization is allowed to be racy */ |
c8de6dbb | 1093 | if (s->pointer_flush && amdtp_stream_running(s)) |
92b862c7 CL |
1094 | fw_iso_context_flush_completions(s->context); |
1095 | else | |
1096 | s->pointer_flush = true; | |
e9148ddd CL |
1097 | |
1098 | return ACCESS_ONCE(s->pcm_buffer_pointer); | |
1099 | } | |
be4a2894 | 1100 | EXPORT_SYMBOL(amdtp_stream_pcm_pointer); |
e9148ddd | 1101 | |
31ef9134 | 1102 | /** |
be4a2894 TS |
1103 | * amdtp_stream_update - update the stream after a bus reset |
1104 | * @s: the AMDTP stream | |
31ef9134 | 1105 | */ |
be4a2894 | 1106 | void amdtp_stream_update(struct amdtp_stream *s) |
31ef9134 | 1107 | { |
9a2820c1 | 1108 | /* Precomputing. */ |
31ef9134 | 1109 | ACCESS_ONCE(s->source_node_id_field) = |
9a2820c1 TS |
1110 | (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & |
1111 | CIP_SID_MASK; | |
31ef9134 | 1112 | } |
be4a2894 | 1113 | EXPORT_SYMBOL(amdtp_stream_update); |
31ef9134 CL |
1114 | |
1115 | /** | |
be4a2894 TS |
1116 | * amdtp_stream_stop - stop sending packets |
1117 | * @s: the AMDTP stream to stop | |
31ef9134 CL |
1118 | * |
1119 | * All PCM and MIDI devices of the stream must be stopped before the stream | |
1120 | * itself can be stopped. | |
1121 | */ | |
be4a2894 | 1122 | void amdtp_stream_stop(struct amdtp_stream *s) |
31ef9134 CL |
1123 | { |
1124 | mutex_lock(&s->mutex); | |
1125 | ||
be4a2894 | 1126 | if (!amdtp_stream_running(s)) { |
31ef9134 CL |
1127 | mutex_unlock(&s->mutex); |
1128 | return; | |
1129 | } | |
1130 | ||
76fb8789 | 1131 | tasklet_kill(&s->period_tasklet); |
31ef9134 CL |
1132 | 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 | ||
7b3b0d85 TS |
1137 | s->callbacked = false; |
1138 | ||
31ef9134 CL |
1139 | mutex_unlock(&s->mutex); |
1140 | } | |
be4a2894 | 1141 | EXPORT_SYMBOL(amdtp_stream_stop); |
31ef9134 CL |
1142 | |
1143 | /** | |
be4a2894 | 1144 | * amdtp_stream_pcm_abort - abort the running PCM device |
31ef9134 CL |
1145 | * @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 | */ | |
be4a2894 | 1150 | void amdtp_stream_pcm_abort(struct amdtp_stream *s) |
31ef9134 CL |
1151 | { |
1152 | struct snd_pcm_substream *pcm; | |
1153 | ||
1154 | pcm = ACCESS_ONCE(s->pcm); | |
1fb8510c TI |
1155 | if (pcm) |
1156 | snd_pcm_stop_xrun(pcm); | |
31ef9134 | 1157 | } |
be4a2894 | 1158 | EXPORT_SYMBOL(amdtp_stream_pcm_abort); |