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> | |
7b3b0d85 | 14 | #include <linux/sched.h> |
31ef9134 | 15 | #include <sound/pcm.h> |
7b2d99fa | 16 | #include <sound/pcm_params.h> |
83d8d72d | 17 | #include <sound/rawmidi.h> |
31ef9134 CL |
18 | #include "amdtp.h" |
19 | ||
20 | #define TICKS_PER_CYCLE 3072 | |
21 | #define CYCLES_PER_SECOND 8000 | |
22 | #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) | |
23 | ||
25ca917c CL |
24 | /* |
25 | * Nominally 3125 bytes/second, but the MIDI port's clock might be | |
26 | * 1% too slow, and the bus clock 100 ppm too fast. | |
27 | */ | |
28 | #define MIDI_BYTES_PER_SECOND 3093 | |
29 | ||
5c697e5b CL |
30 | /* |
31 | * Several devices look only at the first eight data blocks. | |
32 | * In any case, this is more than enough for the MIDI data rate. | |
33 | */ | |
34 | #define MAX_MIDI_RX_BLOCKS 8 | |
35 | ||
ca5b5050 | 36 | #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */ |
31ef9134 | 37 | |
b445db44 TS |
38 | /* isochronous header parameters */ |
39 | #define ISO_DATA_LENGTH_SHIFT 16 | |
31ef9134 CL |
40 | #define TAG_CIP 1 |
41 | ||
b445db44 | 42 | /* common isochronous packet header parameters */ |
9a2820c1 TS |
43 | #define CIP_EOH_SHIFT 31 |
44 | #define CIP_EOH (1u << CIP_EOH_SHIFT) | |
b445db44 | 45 | #define CIP_EOH_MASK 0x80000000 |
9a2820c1 TS |
46 | #define CIP_SID_SHIFT 24 |
47 | #define CIP_SID_MASK 0x3f000000 | |
48 | #define CIP_DBS_MASK 0x00ff0000 | |
49 | #define CIP_DBS_SHIFT 16 | |
50 | #define CIP_DBC_MASK 0x000000ff | |
51 | #define CIP_FMT_SHIFT 24 | |
b445db44 | 52 | #define CIP_FMT_MASK 0x3f000000 |
9a2820c1 TS |
53 | #define CIP_FDF_MASK 0x00ff0000 |
54 | #define CIP_FDF_SHIFT 16 | |
b445db44 TS |
55 | #define CIP_SYT_MASK 0x0000ffff |
56 | #define CIP_SYT_NO_INFO 0xffff | |
b445db44 TS |
57 | |
58 | /* | |
59 | * Audio and Music transfer protocol specific parameters | |
60 | * only "Clock-based rate control mode" is supported | |
61 | */ | |
9a2820c1 TS |
62 | #define CIP_FMT_AM (0x10 << CIP_FMT_SHIFT) |
63 | #define AMDTP_FDF_AM824 (0 << (CIP_FDF_SHIFT + 3)) | |
2b3fc456 | 64 | #define AMDTP_FDF_NO_DATA 0xff |
31ef9134 CL |
65 | |
66 | /* TODO: make these configurable */ | |
67 | #define INTERRUPT_INTERVAL 16 | |
68 | #define QUEUE_LENGTH 48 | |
69 | ||
2b3fc456 | 70 | #define IN_PACKET_HEADER_SIZE 4 |
4b7da117 TS |
71 | #define OUT_PACKET_HEADER_SIZE 0 |
72 | ||
76fb8789 CL |
73 | static void pcm_period_tasklet(unsigned long data); |
74 | ||
31ef9134 | 75 | /** |
be4a2894 TS |
76 | * amdtp_stream_init - initialize an AMDTP stream structure |
77 | * @s: the AMDTP stream to initialize | |
31ef9134 | 78 | * @unit: the target of the stream |
3ff7e8f0 | 79 | * @dir: the direction of stream |
31ef9134 CL |
80 | * @flags: the packet transmission method to use |
81 | */ | |
be4a2894 | 82 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
3ff7e8f0 | 83 | enum amdtp_stream_direction dir, enum cip_flags flags) |
31ef9134 | 84 | { |
c6f224dc | 85 | s->unit = unit; |
3ff7e8f0 | 86 | s->direction = dir; |
31ef9134 CL |
87 | s->flags = flags; |
88 | s->context = ERR_PTR(-1); | |
89 | mutex_init(&s->mutex); | |
76fb8789 | 90 | tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); |
ec00f5e4 | 91 | s->packet_index = 0; |
31ef9134 | 92 | |
7b3b0d85 TS |
93 | init_waitqueue_head(&s->callback_wait); |
94 | s->callbacked = false; | |
95 | s->sync_slave = NULL; | |
96 | ||
31ef9134 CL |
97 | return 0; |
98 | } | |
be4a2894 | 99 | EXPORT_SYMBOL(amdtp_stream_init); |
31ef9134 CL |
100 | |
101 | /** | |
be4a2894 TS |
102 | * amdtp_stream_destroy - free stream resources |
103 | * @s: the AMDTP stream to destroy | |
31ef9134 | 104 | */ |
be4a2894 | 105 | void amdtp_stream_destroy(struct amdtp_stream *s) |
31ef9134 | 106 | { |
be4a2894 | 107 | WARN_ON(amdtp_stream_running(s)); |
31ef9134 | 108 | mutex_destroy(&s->mutex); |
31ef9134 | 109 | } |
be4a2894 | 110 | EXPORT_SYMBOL(amdtp_stream_destroy); |
31ef9134 | 111 | |
c5280e99 | 112 | const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { |
a7304e3b CL |
113 | [CIP_SFC_32000] = 8, |
114 | [CIP_SFC_44100] = 8, | |
115 | [CIP_SFC_48000] = 8, | |
116 | [CIP_SFC_88200] = 16, | |
117 | [CIP_SFC_96000] = 16, | |
118 | [CIP_SFC_176400] = 32, | |
119 | [CIP_SFC_192000] = 32, | |
120 | }; | |
121 | EXPORT_SYMBOL(amdtp_syt_intervals); | |
122 | ||
f9503a68 | 123 | const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { |
1017abed TS |
124 | [CIP_SFC_32000] = 32000, |
125 | [CIP_SFC_44100] = 44100, | |
126 | [CIP_SFC_48000] = 48000, | |
127 | [CIP_SFC_88200] = 88200, | |
128 | [CIP_SFC_96000] = 96000, | |
129 | [CIP_SFC_176400] = 176400, | |
130 | [CIP_SFC_192000] = 192000, | |
131 | }; | |
132 | EXPORT_SYMBOL(amdtp_rate_table); | |
133 | ||
7b2d99fa TS |
134 | /** |
135 | * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream | |
136 | * @s: the AMDTP stream, which must be initialized. | |
137 | * @runtime: the PCM substream runtime | |
138 | */ | |
139 | int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, | |
140 | struct snd_pcm_runtime *runtime) | |
141 | { | |
142 | int err; | |
143 | ||
144 | /* AM824 in IEC 61883-6 can deliver 24bit data */ | |
145 | err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24); | |
146 | if (err < 0) | |
147 | goto end; | |
148 | ||
149 | /* | |
150 | * Currently firewire-lib processes 16 packets in one software | |
151 | * interrupt callback. This equals to 2msec but actually the | |
152 | * interval of the interrupts has a jitter. | |
153 | * Additionally, even if adding a constraint to fit period size to | |
154 | * 2msec, actual calculated frames per period doesn't equal to 2msec, | |
155 | * depending on sampling rate. | |
156 | * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec. | |
157 | * Here let us use 5msec for safe period interrupt. | |
158 | */ | |
159 | err = snd_pcm_hw_constraint_minmax(runtime, | |
160 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
161 | 5000, UINT_MAX); | |
162 | if (err < 0) | |
163 | goto end; | |
164 | ||
165 | /* Non-Blocking stream has no more constraints */ | |
166 | if (!(s->flags & CIP_BLOCKING)) | |
167 | goto end; | |
168 | ||
169 | /* | |
170 | * One AMDTP packet can include some frames. In blocking mode, the | |
171 | * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, | |
172 | * depending on its sampling rate. For accurate period interrupt, it's | |
ce991981 | 173 | * preferrable to align period/buffer sizes to current SYT_INTERVAL. |
7b2d99fa | 174 | * |
ce991981 YG |
175 | * TODO: These constraints can be improved with proper rules. |
176 | * Currently apply LCM of SYT_INTERVALs. | |
7b2d99fa TS |
177 | */ |
178 | err = snd_pcm_hw_constraint_step(runtime, 0, | |
179 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32); | |
180 | if (err < 0) | |
181 | goto end; | |
182 | err = snd_pcm_hw_constraint_step(runtime, 0, | |
183 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32); | |
184 | end: | |
185 | return err; | |
186 | } | |
187 | EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); | |
188 | ||
31ef9134 | 189 | /** |
be4a2894 TS |
190 | * amdtp_stream_set_parameters - set stream parameters |
191 | * @s: the AMDTP stream to configure | |
31ef9134 | 192 | * @rate: the sample rate |
a7304e3b CL |
193 | * @pcm_channels: the number of PCM samples in each data block, to be encoded |
194 | * as AM824 multi-bit linear audio | |
195 | * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels) | |
27ec83b5 | 196 | * @double_pcm_frames: one data block transfers two PCM frames |
31ef9134 | 197 | * |
a7304e3b | 198 | * The parameters must be set before the stream is started, and must not be |
31ef9134 CL |
199 | * changed while the stream is running. |
200 | */ | |
547e631c TS |
201 | int amdtp_stream_set_parameters(struct amdtp_stream *s, |
202 | unsigned int rate, | |
203 | unsigned int pcm_channels, | |
27ec83b5 TS |
204 | unsigned int midi_ports, |
205 | bool double_pcm_frames) | |
31ef9134 | 206 | { |
77d2a8a4 | 207 | unsigned int i, sfc, midi_channels; |
31ef9134 | 208 | |
83d8d72d TS |
209 | midi_channels = DIV_ROUND_UP(midi_ports, 8); |
210 | ||
77d2a8a4 TS |
211 | if (WARN_ON(amdtp_stream_running(s)) | |
212 | WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) | | |
83d8d72d | 213 | WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI)) |
547e631c | 214 | return -EINVAL; |
31ef9134 | 215 | |
547e631c | 216 | for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { |
1017abed | 217 | if (amdtp_rate_table[sfc] == rate) |
547e631c TS |
218 | break; |
219 | } | |
220 | if (sfc == ARRAY_SIZE(amdtp_rate_table)) | |
221 | return -EINVAL; | |
e84d15f6 | 222 | |
10550bea | 223 | s->pcm_channels = pcm_channels; |
e84d15f6 | 224 | s->sfc = sfc; |
77d2a8a4 | 225 | s->data_block_quadlets = s->pcm_channels + midi_channels; |
a7304e3b CL |
226 | s->midi_ports = midi_ports; |
227 | ||
6a4e89ff TS |
228 | /* |
229 | * In IEC 61883-6, one data block represents one event. In ALSA, one | |
230 | * event equals to one PCM frame. But Dice has a quirk at higher | |
231 | * sampling rate to transfer two PCM frames in one data block. | |
232 | */ | |
233 | if (double_pcm_frames) | |
234 | s->frame_multiplier = 2; | |
235 | else | |
236 | s->frame_multiplier = 1; | |
237 | ||
a7304e3b | 238 | s->syt_interval = amdtp_syt_intervals[sfc]; |
e84d15f6 CL |
239 | |
240 | /* default buffering in the device */ | |
241 | s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; | |
242 | if (s->flags & CIP_BLOCKING) | |
243 | /* additional buffering needed to adjust for no-data packets */ | |
244 | s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; | |
77d2a8a4 TS |
245 | |
246 | /* init the position map for PCM and MIDI channels */ | |
247 | for (i = 0; i < pcm_channels; i++) | |
248 | s->pcm_positions[i] = i; | |
249 | s->midi_position = s->pcm_channels; | |
25ca917c CL |
250 | |
251 | /* | |
252 | * We do not know the actual MIDI FIFO size of most devices. Just | |
253 | * assume two bytes, i.e., one byte can be received over the bus while | |
254 | * the previous one is transmitted over MIDI. | |
255 | * (The value here is adjusted for midi_ratelimit_per_packet().) | |
256 | */ | |
257 | s->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1; | |
547e631c TS |
258 | |
259 | return 0; | |
31ef9134 | 260 | } |
be4a2894 | 261 | EXPORT_SYMBOL(amdtp_stream_set_parameters); |
31ef9134 CL |
262 | |
263 | /** | |
be4a2894 TS |
264 | * amdtp_stream_get_max_payload - get the stream's packet size |
265 | * @s: the AMDTP stream | |
31ef9134 CL |
266 | * |
267 | * This function must not be called before the stream has been configured | |
be4a2894 | 268 | * with amdtp_stream_set_parameters(). |
31ef9134 | 269 | */ |
be4a2894 | 270 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) |
31ef9134 | 271 | { |
a2064710 TS |
272 | unsigned int multiplier = 1; |
273 | ||
274 | if (s->flags & CIP_JUMBO_PAYLOAD) | |
275 | multiplier = 5; | |
276 | ||
277 | return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier; | |
31ef9134 | 278 | } |
be4a2894 | 279 | EXPORT_SYMBOL(amdtp_stream_get_max_payload); |
31ef9134 | 280 | |
29bcae20 TS |
281 | static void write_pcm_s16(struct amdtp_stream *s, |
282 | struct snd_pcm_substream *pcm, | |
283 | __be32 *buffer, unsigned int frames); | |
284 | static void write_pcm_s32(struct amdtp_stream *s, | |
285 | struct snd_pcm_substream *pcm, | |
286 | __be32 *buffer, unsigned int frames); | |
287 | static void read_pcm_s32(struct amdtp_stream *s, | |
288 | struct snd_pcm_substream *pcm, | |
289 | __be32 *buffer, unsigned int frames); | |
31ef9134 CL |
290 | |
291 | /** | |
be4a2894 TS |
292 | * amdtp_stream_set_pcm_format - set the PCM format |
293 | * @s: the AMDTP stream to configure | |
31ef9134 CL |
294 | * @format: the format of the ALSA PCM device |
295 | * | |
ce991981 | 296 | * The sample format must be set after the other parameters (rate/PCM channels/ |
a7304e3b CL |
297 | * MIDI) and before the stream is started, and must not be changed while the |
298 | * stream is running. | |
31ef9134 | 299 | */ |
be4a2894 TS |
300 | void amdtp_stream_set_pcm_format(struct amdtp_stream *s, |
301 | snd_pcm_format_t format) | |
31ef9134 | 302 | { |
83d8d72d | 303 | if (WARN_ON(amdtp_stream_pcm_running(s))) |
31ef9134 CL |
304 | return; |
305 | ||
306 | switch (format) { | |
307 | default: | |
308 | WARN_ON(1); | |
309 | /* fall through */ | |
310 | case SNDRV_PCM_FORMAT_S16: | |
2b3fc456 | 311 | if (s->direction == AMDTP_OUT_STREAM) { |
29bcae20 | 312 | s->transfer_samples = write_pcm_s16; |
2b3fc456 TS |
313 | break; |
314 | } | |
315 | WARN_ON(1); | |
316 | /* fall through */ | |
31ef9134 | 317 | case SNDRV_PCM_FORMAT_S32: |
10550bea | 318 | if (s->direction == AMDTP_OUT_STREAM) |
29bcae20 | 319 | s->transfer_samples = write_pcm_s32; |
10550bea | 320 | else |
29bcae20 | 321 | s->transfer_samples = read_pcm_s32; |
31ef9134 CL |
322 | break; |
323 | } | |
324 | } | |
be4a2894 | 325 | EXPORT_SYMBOL(amdtp_stream_set_pcm_format); |
31ef9134 | 326 | |
76fb8789 | 327 | /** |
be4a2894 TS |
328 | * amdtp_stream_pcm_prepare - prepare PCM device for running |
329 | * @s: the AMDTP stream | |
76fb8789 CL |
330 | * |
331 | * This function should be called from the PCM device's .prepare callback. | |
332 | */ | |
be4a2894 | 333 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s) |
76fb8789 CL |
334 | { |
335 | tasklet_kill(&s->period_tasklet); | |
336 | s->pcm_buffer_pointer = 0; | |
337 | s->pcm_period_pointer = 0; | |
92b862c7 | 338 | s->pointer_flush = true; |
76fb8789 | 339 | } |
be4a2894 | 340 | EXPORT_SYMBOL(amdtp_stream_pcm_prepare); |
76fb8789 | 341 | |
875be091 TS |
342 | static unsigned int calculate_data_blocks(struct amdtp_stream *s, |
343 | unsigned int syt) | |
31ef9134 CL |
344 | { |
345 | unsigned int phase, data_blocks; | |
346 | ||
875be091 TS |
347 | /* Blocking mode. */ |
348 | if (s->flags & CIP_BLOCKING) { | |
349 | /* This module generate empty packet for 'no data'. */ | |
350 | if (syt == CIP_SYT_NO_INFO) | |
351 | data_blocks = 0; | |
352 | else | |
353 | data_blocks = s->syt_interval; | |
354 | /* Non-blocking mode. */ | |
31ef9134 | 355 | } else { |
875be091 TS |
356 | if (!cip_sfc_is_base_44100(s->sfc)) { |
357 | /* Sample_rate / 8000 is an integer, and precomputed. */ | |
358 | data_blocks = s->data_block_state; | |
359 | } else { | |
360 | phase = s->data_block_state; | |
31ef9134 CL |
361 | |
362 | /* | |
363 | * This calculates the number of data blocks per packet so that | |
364 | * 1) the overall rate is correct and exactly synchronized to | |
365 | * the bus clock, and | |
366 | * 2) packets with a rounded-up number of blocks occur as early | |
367 | * as possible in the sequence (to prevent underruns of the | |
368 | * device's buffer). | |
369 | */ | |
875be091 TS |
370 | if (s->sfc == CIP_SFC_44100) |
371 | /* 6 6 5 6 5 6 5 ... */ | |
372 | data_blocks = 5 + ((phase & 1) ^ | |
373 | (phase == 0 || phase >= 40)); | |
374 | else | |
375 | /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ | |
376 | data_blocks = 11 * (s->sfc >> 1) + (phase == 0); | |
377 | if (++phase >= (80 >> (s->sfc >> 1))) | |
378 | phase = 0; | |
379 | s->data_block_state = phase; | |
380 | } | |
31ef9134 CL |
381 | } |
382 | ||
383 | return data_blocks; | |
384 | } | |
385 | ||
be4a2894 | 386 | static unsigned int calculate_syt(struct amdtp_stream *s, |
31ef9134 CL |
387 | unsigned int cycle) |
388 | { | |
389 | unsigned int syt_offset, phase, index, syt; | |
390 | ||
391 | if (s->last_syt_offset < TICKS_PER_CYCLE) { | |
392 | if (!cip_sfc_is_base_44100(s->sfc)) | |
393 | syt_offset = s->last_syt_offset + s->syt_offset_state; | |
394 | else { | |
395 | /* | |
396 | * The time, in ticks, of the n'th SYT_INTERVAL sample is: | |
397 | * n * SYT_INTERVAL * 24576000 / sample_rate | |
398 | * Modulo TICKS_PER_CYCLE, the difference between successive | |
399 | * elements is about 1386.23. Rounding the results of this | |
400 | * formula to the SYT precision results in a sequence of | |
401 | * differences that begins with: | |
402 | * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... | |
403 | * This code generates _exactly_ the same sequence. | |
404 | */ | |
405 | phase = s->syt_offset_state; | |
406 | index = phase % 13; | |
407 | syt_offset = s->last_syt_offset; | |
408 | syt_offset += 1386 + ((index && !(index & 3)) || | |
409 | phase == 146); | |
410 | if (++phase >= 147) | |
411 | phase = 0; | |
412 | s->syt_offset_state = phase; | |
413 | } | |
414 | } else | |
415 | syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; | |
416 | s->last_syt_offset = syt_offset; | |
417 | ||
be454366 | 418 | if (syt_offset < TICKS_PER_CYCLE) { |
e84d15f6 | 419 | syt_offset += s->transfer_delay; |
be454366 CL |
420 | syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; |
421 | syt += syt_offset % TICKS_PER_CYCLE; | |
31ef9134 | 422 | |
b445db44 | 423 | return syt & CIP_SYT_MASK; |
be454366 | 424 | } else { |
b445db44 | 425 | return CIP_SYT_NO_INFO; |
be454366 | 426 | } |
31ef9134 CL |
427 | } |
428 | ||
29bcae20 TS |
429 | static void write_pcm_s32(struct amdtp_stream *s, |
430 | struct snd_pcm_substream *pcm, | |
431 | __be32 *buffer, unsigned int frames) | |
31ef9134 CL |
432 | { |
433 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
77d2a8a4 | 434 | unsigned int channels, remaining_frames, i, c; |
31ef9134 CL |
435 | const u32 *src; |
436 | ||
437 | channels = s->pcm_channels; | |
438 | src = (void *)runtime->dma_area + | |
e84841f9 | 439 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
31ef9134 | 440 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
31ef9134 CL |
441 | |
442 | for (i = 0; i < frames; ++i) { | |
443 | for (c = 0; c < channels; ++c) { | |
77d2a8a4 TS |
444 | buffer[s->pcm_positions[c]] = |
445 | cpu_to_be32((*src >> 8) | 0x40000000); | |
31ef9134 | 446 | src++; |
31ef9134 | 447 | } |
77d2a8a4 | 448 | buffer += s->data_block_quadlets; |
31ef9134 CL |
449 | if (--remaining_frames == 0) |
450 | src = (void *)runtime->dma_area; | |
451 | } | |
452 | } | |
453 | ||
29bcae20 TS |
454 | static void write_pcm_s16(struct amdtp_stream *s, |
455 | struct snd_pcm_substream *pcm, | |
456 | __be32 *buffer, unsigned int frames) | |
31ef9134 CL |
457 | { |
458 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
77d2a8a4 | 459 | unsigned int channels, remaining_frames, i, c; |
31ef9134 CL |
460 | const u16 *src; |
461 | ||
462 | channels = s->pcm_channels; | |
463 | src = (void *)runtime->dma_area + | |
e84841f9 | 464 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
31ef9134 | 465 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
31ef9134 CL |
466 | |
467 | for (i = 0; i < frames; ++i) { | |
468 | for (c = 0; c < channels; ++c) { | |
77d2a8a4 | 469 | buffer[s->pcm_positions[c]] = |
a6975f2a | 470 | cpu_to_be32((*src << 8) | 0x42000000); |
31ef9134 | 471 | src++; |
31ef9134 | 472 | } |
77d2a8a4 | 473 | buffer += s->data_block_quadlets; |
31ef9134 CL |
474 | if (--remaining_frames == 0) |
475 | src = (void *)runtime->dma_area; | |
476 | } | |
477 | } | |
478 | ||
29bcae20 TS |
479 | static void read_pcm_s32(struct amdtp_stream *s, |
480 | struct snd_pcm_substream *pcm, | |
481 | __be32 *buffer, unsigned int frames) | |
2b3fc456 TS |
482 | { |
483 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
484 | unsigned int channels, remaining_frames, i, c; | |
485 | u32 *dst; | |
486 | ||
487 | channels = s->pcm_channels; | |
488 | dst = (void *)runtime->dma_area + | |
489 | frames_to_bytes(runtime, s->pcm_buffer_pointer); | |
490 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; | |
491 | ||
492 | for (i = 0; i < frames; ++i) { | |
493 | for (c = 0; c < channels; ++c) { | |
77d2a8a4 | 494 | *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8; |
2b3fc456 TS |
495 | dst++; |
496 | } | |
497 | buffer += s->data_block_quadlets; | |
498 | if (--remaining_frames == 0) | |
499 | dst = (void *)runtime->dma_area; | |
500 | } | |
501 | } | |
502 | ||
29bcae20 TS |
503 | static void write_pcm_silence(struct amdtp_stream *s, |
504 | __be32 *buffer, unsigned int frames) | |
31ef9134 CL |
505 | { |
506 | unsigned int i, c; | |
507 | ||
508 | for (i = 0; i < frames; ++i) { | |
509 | for (c = 0; c < s->pcm_channels; ++c) | |
77d2a8a4 | 510 | buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000); |
31ef9134 CL |
511 | buffer += s->data_block_quadlets; |
512 | } | |
513 | } | |
514 | ||
25ca917c CL |
515 | /* |
516 | * To avoid sending MIDI bytes at too high a rate, assume that the receiving | |
517 | * device has a FIFO, and track how much it is filled. This values increases | |
518 | * by one whenever we send one byte in a packet, but the FIFO empties at | |
519 | * a constant rate independent of our packet rate. One packet has syt_interval | |
520 | * samples, so the number of bytes that empty out of the FIFO, per packet(!), | |
521 | * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing | |
522 | * fractional values, the values in midi_fifo_used[] are measured in bytes | |
523 | * multiplied by the sample rate. | |
524 | */ | |
525 | static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port) | |
526 | { | |
527 | int used; | |
528 | ||
529 | used = s->midi_fifo_used[port]; | |
530 | if (used == 0) /* common shortcut */ | |
531 | return true; | |
532 | ||
533 | used -= MIDI_BYTES_PER_SECOND * s->syt_interval; | |
534 | used = max(used, 0); | |
535 | s->midi_fifo_used[port] = used; | |
536 | ||
537 | return used < s->midi_fifo_limit; | |
538 | } | |
539 | ||
540 | static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port) | |
541 | { | |
542 | s->midi_fifo_used[port] += amdtp_rate_table[s->sfc]; | |
543 | } | |
544 | ||
29bcae20 TS |
545 | static void write_midi_messages(struct amdtp_stream *s, |
546 | __be32 *buffer, unsigned int frames) | |
31ef9134 | 547 | { |
83d8d72d TS |
548 | unsigned int f, port; |
549 | u8 *b; | |
550 | ||
551 | for (f = 0; f < frames; f++) { | |
77d2a8a4 | 552 | b = (u8 *)&buffer[s->midi_position]; |
83d8d72d TS |
553 | |
554 | port = (s->data_block_counter + f) % 8; | |
25ca917c CL |
555 | if (f < MAX_MIDI_RX_BLOCKS && |
556 | midi_ratelimit_per_packet(s, port) && | |
557 | s->midi[port] != NULL && | |
558 | snd_rawmidi_transmit(s->midi[port], &b[1], 1) == 1) { | |
559 | midi_rate_use_one_byte(s, port); | |
83d8d72d | 560 | b[0] = 0x81; |
25ca917c CL |
561 | } else { |
562 | b[0] = 0x80; | |
563 | b[1] = 0; | |
564 | } | |
565 | b[2] = 0; | |
566 | b[3] = 0; | |
83d8d72d TS |
567 | |
568 | buffer += s->data_block_quadlets; | |
569 | } | |
570 | } | |
571 | ||
29bcae20 TS |
572 | static void read_midi_messages(struct amdtp_stream *s, |
573 | __be32 *buffer, unsigned int frames) | |
83d8d72d TS |
574 | { |
575 | unsigned int f, port; | |
576 | int len; | |
577 | u8 *b; | |
578 | ||
579 | for (f = 0; f < frames; f++) { | |
580 | port = (s->data_block_counter + f) % 8; | |
77d2a8a4 | 581 | b = (u8 *)&buffer[s->midi_position]; |
31ef9134 | 582 | |
83d8d72d TS |
583 | len = b[0] - 0x80; |
584 | if ((1 <= len) && (len <= 3) && (s->midi[port])) | |
585 | snd_rawmidi_receive(s->midi[port], b + 1, len); | |
586 | ||
587 | buffer += s->data_block_quadlets; | |
588 | } | |
31ef9134 CL |
589 | } |
590 | ||
4b7da117 TS |
591 | static void update_pcm_pointers(struct amdtp_stream *s, |
592 | struct snd_pcm_substream *pcm, | |
593 | unsigned int frames) | |
65845f29 TS |
594 | { |
595 | unsigned int ptr; | |
596 | ||
4b7da117 TS |
597 | ptr = s->pcm_buffer_pointer + frames; |
598 | if (ptr >= pcm->runtime->buffer_size) | |
599 | ptr -= pcm->runtime->buffer_size; | |
600 | ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; | |
601 | ||
602 | s->pcm_period_pointer += frames; | |
603 | if (s->pcm_period_pointer >= pcm->runtime->period_size) { | |
604 | s->pcm_period_pointer -= pcm->runtime->period_size; | |
605 | s->pointer_flush = false; | |
606 | tasklet_hi_schedule(&s->period_tasklet); | |
607 | } | |
608 | } | |
609 | ||
610 | static void pcm_period_tasklet(unsigned long data) | |
611 | { | |
612 | struct amdtp_stream *s = (void *)data; | |
613 | struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); | |
614 | ||
615 | if (pcm) | |
616 | snd_pcm_period_elapsed(pcm); | |
617 | } | |
618 | ||
619 | static int queue_packet(struct amdtp_stream *s, | |
620 | unsigned int header_length, | |
621 | unsigned int payload_length, bool skip) | |
622 | { | |
623 | struct fw_iso_packet p = {0}; | |
7b3b0d85 TS |
624 | int err = 0; |
625 | ||
626 | if (IS_ERR(s->context)) | |
627 | goto end; | |
4b7da117 TS |
628 | |
629 | p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL); | |
630 | p.tag = TAG_CIP; | |
631 | p.header_length = header_length; | |
632 | p.payload_length = (!skip) ? payload_length : 0; | |
633 | p.skip = skip; | |
634 | err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer, | |
635 | s->buffer.packets[s->packet_index].offset); | |
636 | if (err < 0) { | |
637 | dev_err(&s->unit->device, "queueing error: %d\n", err); | |
638 | goto end; | |
639 | } | |
640 | ||
641 | if (++s->packet_index >= QUEUE_LENGTH) | |
642 | s->packet_index = 0; | |
643 | end: | |
644 | return err; | |
645 | } | |
646 | ||
647 | static inline int queue_out_packet(struct amdtp_stream *s, | |
648 | unsigned int payload_length, bool skip) | |
649 | { | |
650 | return queue_packet(s, OUT_PACKET_HEADER_SIZE, | |
651 | payload_length, skip); | |
652 | } | |
653 | ||
2b3fc456 TS |
654 | static inline int queue_in_packet(struct amdtp_stream *s) |
655 | { | |
656 | return queue_packet(s, IN_PACKET_HEADER_SIZE, | |
657 | amdtp_stream_get_max_payload(s), false); | |
658 | } | |
659 | ||
20e44577 TS |
660 | unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer, |
661 | unsigned int data_blocks, unsigned int *syt) | |
662 | { | |
663 | struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); | |
664 | unsigned int pcm_frames; | |
665 | ||
666 | if (pcm) { | |
667 | s->transfer_samples(s, pcm, buffer, data_blocks); | |
668 | pcm_frames = data_blocks * s->frame_multiplier; | |
669 | } else { | |
670 | write_pcm_silence(s, buffer, data_blocks); | |
671 | pcm_frames = 0; | |
672 | } | |
673 | ||
674 | if (s->midi_ports) | |
675 | write_midi_messages(s, buffer, data_blocks); | |
676 | ||
677 | return pcm_frames; | |
678 | } | |
679 | ||
a4103bd7 TS |
680 | static int handle_out_packet(struct amdtp_stream *s, unsigned int data_blocks, |
681 | unsigned int syt) | |
31ef9134 CL |
682 | { |
683 | __be32 *buffer; | |
6fc6b9ce | 684 | unsigned int payload_length; |
20e44577 | 685 | unsigned int pcm_frames; |
31ef9134 | 686 | struct snd_pcm_substream *pcm; |
31ef9134 | 687 | |
ccccad86 | 688 | buffer = s->buffer.packets[s->packet_index].buffer; |
20e44577 TS |
689 | pcm_frames = process_rx_data_blocks(s, buffer + 2, data_blocks, &syt); |
690 | ||
31ef9134 | 691 | buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | |
9a2820c1 | 692 | (s->data_block_quadlets << CIP_DBS_SHIFT) | |
31ef9134 CL |
693 | s->data_block_counter); |
694 | buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 | | |
9a2820c1 | 695 | (s->sfc << CIP_FDF_SHIFT) | syt); |
31ef9134 CL |
696 | |
697 | s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; | |
698 | ||
4b7da117 | 699 | payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; |
a4103bd7 TS |
700 | if (queue_out_packet(s, payload_length, false) < 0) |
701 | return -EIO; | |
31ef9134 | 702 | |
20e44577 TS |
703 | pcm = ACCESS_ONCE(s->pcm); |
704 | if (pcm && pcm_frames > 0) | |
705 | update_pcm_pointers(s, pcm, pcm_frames); | |
a4103bd7 TS |
706 | |
707 | /* No need to return the number of handled data blocks. */ | |
708 | return 0; | |
76fb8789 CL |
709 | } |
710 | ||
20e44577 TS |
711 | unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer, |
712 | unsigned int data_blocks, unsigned int *syt) | |
713 | { | |
714 | struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); | |
715 | unsigned int pcm_frames; | |
716 | ||
717 | if (pcm) { | |
718 | s->transfer_samples(s, pcm, buffer, data_blocks); | |
719 | pcm_frames = data_blocks * s->frame_multiplier; | |
720 | } else { | |
721 | pcm_frames = 0; | |
722 | } | |
723 | ||
724 | if (s->midi_ports) | |
725 | read_midi_messages(s, buffer, data_blocks); | |
726 | ||
727 | return pcm_frames; | |
728 | } | |
729 | ||
6fc6b9ce | 730 | static int handle_in_packet(struct amdtp_stream *s, |
31ea49ba | 731 | unsigned int payload_quadlets, __be32 *buffer, |
20e44577 | 732 | unsigned int *data_blocks, unsigned int syt) |
2b3fc456 TS |
733 | { |
734 | u32 cip_header[2]; | |
6fc6b9ce | 735 | unsigned int data_block_quadlets, data_block_counter, dbc_interval; |
20e44577 TS |
736 | struct snd_pcm_substream *pcm; |
737 | unsigned int pcm_frames; | |
c8bdf49b | 738 | bool lost; |
2b3fc456 TS |
739 | |
740 | cip_header[0] = be32_to_cpu(buffer[0]); | |
741 | cip_header[1] = be32_to_cpu(buffer[1]); | |
742 | ||
743 | /* | |
744 | * This module supports 'Two-quadlet CIP header with SYT field'. | |
77d2a8a4 | 745 | * For convenience, also check FMT field is AM824 or not. |
2b3fc456 TS |
746 | */ |
747 | if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || | |
748 | ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) || | |
749 | ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) { | |
750 | dev_info_ratelimited(&s->unit->device, | |
751 | "Invalid CIP header for AMDTP: %08X:%08X\n", | |
752 | cip_header[0], cip_header[1]); | |
31ea49ba | 753 | *data_blocks = 0; |
20e44577 | 754 | pcm_frames = 0; |
2b3fc456 TS |
755 | goto end; |
756 | } | |
757 | ||
758 | /* Calculate data blocks */ | |
759 | if (payload_quadlets < 3 || | |
760 | ((cip_header[1] & CIP_FDF_MASK) == | |
9a2820c1 | 761 | (AMDTP_FDF_NO_DATA << CIP_FDF_SHIFT))) { |
31ea49ba | 762 | *data_blocks = 0; |
2b3fc456 TS |
763 | } else { |
764 | data_block_quadlets = | |
9a2820c1 | 765 | (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; |
2b3fc456 TS |
766 | /* avoid division by zero */ |
767 | if (data_block_quadlets == 0) { | |
12e0f438 | 768 | dev_err(&s->unit->device, |
2b3fc456 TS |
769 | "Detect invalid value in dbs field: %08X\n", |
770 | cip_header[0]); | |
a9007054 | 771 | return -EPROTO; |
2b3fc456 | 772 | } |
69702239 TS |
773 | if (s->flags & CIP_WRONG_DBS) |
774 | data_block_quadlets = s->data_block_quadlets; | |
2b3fc456 | 775 | |
31ea49ba | 776 | *data_blocks = (payload_quadlets - 2) / data_block_quadlets; |
2b3fc456 TS |
777 | } |
778 | ||
779 | /* Check data block counter continuity */ | |
9a2820c1 | 780 | data_block_counter = cip_header[0] & CIP_DBC_MASK; |
31ea49ba | 781 | if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && |
9d59124c TS |
782 | s->data_block_counter != UINT_MAX) |
783 | data_block_counter = s->data_block_counter; | |
784 | ||
18f5ed36 TS |
785 | if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && |
786 | data_block_counter == s->tx_first_dbc) || | |
787 | s->data_block_counter == UINT_MAX) { | |
b84b1a27 TS |
788 | lost = false; |
789 | } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { | |
c8bdf49b | 790 | lost = data_block_counter != s->data_block_counter; |
d9cd0065 | 791 | } else { |
31ea49ba | 792 | if ((*data_blocks > 0) && (s->tx_dbc_interval > 0)) |
d9cd0065 TS |
793 | dbc_interval = s->tx_dbc_interval; |
794 | else | |
31ea49ba | 795 | dbc_interval = *data_blocks; |
d9cd0065 | 796 | |
c8bdf49b | 797 | lost = data_block_counter != |
d9cd0065 TS |
798 | ((s->data_block_counter + dbc_interval) & 0xff); |
799 | } | |
c8bdf49b TS |
800 | |
801 | if (lost) { | |
12e0f438 TS |
802 | dev_err(&s->unit->device, |
803 | "Detect discontinuity of CIP: %02X %02X\n", | |
804 | s->data_block_counter, data_block_counter); | |
6fc6b9ce | 805 | return -EIO; |
2b3fc456 TS |
806 | } |
807 | ||
20e44577 | 808 | pcm_frames = process_tx_data_blocks(s, buffer + 2, *data_blocks, &syt); |
2b3fc456 | 809 | |
c8bdf49b TS |
810 | if (s->flags & CIP_DBC_IS_END_EVENT) |
811 | s->data_block_counter = data_block_counter; | |
812 | else | |
813 | s->data_block_counter = | |
31ea49ba | 814 | (data_block_counter + *data_blocks) & 0xff; |
2b3fc456 TS |
815 | end: |
816 | if (queue_in_packet(s) < 0) | |
6fc6b9ce | 817 | return -EIO; |
2b3fc456 | 818 | |
20e44577 TS |
819 | pcm = ACCESS_ONCE(s->pcm); |
820 | if (pcm && pcm_frames > 0) | |
821 | update_pcm_pointers(s, pcm, pcm_frames); | |
2b3fc456 | 822 | |
31ea49ba | 823 | return 0; |
2b3fc456 TS |
824 | } |
825 | ||
4b7da117 TS |
826 | static void out_stream_callback(struct fw_iso_context *context, u32 cycle, |
827 | size_t header_length, void *header, | |
828 | void *private_data) | |
31ef9134 | 829 | { |
be4a2894 | 830 | struct amdtp_stream *s = private_data; |
ccccad86 | 831 | unsigned int i, syt, packets = header_length / 4; |
6fc6b9ce | 832 | unsigned int data_blocks; |
31ef9134 | 833 | |
a4103bd7 TS |
834 | if (s->packet_index < 0) |
835 | return; | |
836 | ||
31ef9134 CL |
837 | /* |
838 | * Compute the cycle of the last queued packet. | |
839 | * (We need only the four lowest bits for the SYT, so we can ignore | |
840 | * that bits 0-11 must wrap around at 3072.) | |
841 | */ | |
842 | cycle += QUEUE_LENGTH - packets; | |
843 | ||
ccccad86 TS |
844 | for (i = 0; i < packets; ++i) { |
845 | syt = calculate_syt(s, ++cycle); | |
6fc6b9ce TS |
846 | data_blocks = calculate_data_blocks(s, syt); |
847 | ||
a4103bd7 TS |
848 | if (handle_out_packet(s, data_blocks, syt) < 0) { |
849 | s->packet_index = -1; | |
850 | amdtp_stream_pcm_abort(s); | |
851 | return; | |
852 | } | |
ccccad86 | 853 | } |
a4103bd7 | 854 | |
13882a82 | 855 | fw_iso_context_queue_flush(s->context); |
31ef9134 CL |
856 | } |
857 | ||
2b3fc456 TS |
858 | static void in_stream_callback(struct fw_iso_context *context, u32 cycle, |
859 | size_t header_length, void *header, | |
860 | void *private_data) | |
861 | { | |
862 | struct amdtp_stream *s = private_data; | |
a2064710 TS |
863 | unsigned int p, syt, packets; |
864 | unsigned int payload_quadlets, max_payload_quadlets; | |
6fc6b9ce | 865 | unsigned int data_blocks; |
2b3fc456 TS |
866 | __be32 *buffer, *headers = header; |
867 | ||
a4103bd7 TS |
868 | if (s->packet_index < 0) |
869 | return; | |
870 | ||
2b3fc456 TS |
871 | /* The number of packets in buffer */ |
872 | packets = header_length / IN_PACKET_HEADER_SIZE; | |
873 | ||
a2064710 TS |
874 | /* For buffer-over-run prevention. */ |
875 | max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4; | |
876 | ||
2b3fc456 | 877 | for (p = 0; p < packets; p++) { |
2b3fc456 TS |
878 | buffer = s->buffer.packets[s->packet_index].buffer; |
879 | ||
880 | /* The number of quadlets in this packet */ | |
881 | payload_quadlets = | |
882 | (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4; | |
a2064710 TS |
883 | if (payload_quadlets > max_payload_quadlets) { |
884 | dev_err(&s->unit->device, | |
885 | "Detect jumbo payload: %02x %02x\n", | |
886 | payload_quadlets, max_payload_quadlets); | |
887 | s->packet_index = -1; | |
888 | break; | |
889 | } | |
890 | ||
20e44577 | 891 | syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK; |
31ea49ba | 892 | if (handle_in_packet(s, payload_quadlets, buffer, |
20e44577 | 893 | &data_blocks, syt) < 0) { |
6fc6b9ce TS |
894 | s->packet_index = -1; |
895 | break; | |
896 | } | |
897 | ||
898 | /* Process sync slave stream */ | |
899 | if (s->sync_slave && s->sync_slave->callbacked) { | |
a4103bd7 TS |
900 | if (handle_out_packet(s->sync_slave, |
901 | data_blocks, syt) < 0) { | |
902 | s->packet_index = -1; | |
903 | break; | |
904 | } | |
6fc6b9ce | 905 | } |
2b3fc456 TS |
906 | } |
907 | ||
7b3b0d85 TS |
908 | /* Queueing error or detecting discontinuity */ |
909 | if (s->packet_index < 0) { | |
6fc6b9ce TS |
910 | amdtp_stream_pcm_abort(s); |
911 | ||
7b3b0d85 TS |
912 | /* Abort sync slave. */ |
913 | if (s->sync_slave) { | |
914 | s->sync_slave->packet_index = -1; | |
915 | amdtp_stream_pcm_abort(s->sync_slave); | |
916 | } | |
917 | return; | |
918 | } | |
919 | ||
920 | /* when sync to device, flush the packets for slave stream */ | |
921 | if (s->sync_slave && s->sync_slave->callbacked) | |
922 | fw_iso_context_queue_flush(s->sync_slave->context); | |
923 | ||
2b3fc456 TS |
924 | fw_iso_context_queue_flush(s->context); |
925 | } | |
926 | ||
7b3b0d85 TS |
927 | /* processing is done by master callback */ |
928 | static void slave_stream_callback(struct fw_iso_context *context, u32 cycle, | |
929 | size_t header_length, void *header, | |
930 | void *private_data) | |
931 | { | |
932 | return; | |
933 | } | |
934 | ||
935 | /* this is executed one time */ | |
936 | static void amdtp_stream_first_callback(struct fw_iso_context *context, | |
937 | u32 cycle, size_t header_length, | |
938 | void *header, void *private_data) | |
939 | { | |
940 | struct amdtp_stream *s = private_data; | |
941 | ||
942 | /* | |
943 | * For in-stream, first packet has come. | |
944 | * For out-stream, prepared to transmit first packet | |
945 | */ | |
946 | s->callbacked = true; | |
947 | wake_up(&s->callback_wait); | |
948 | ||
949 | if (s->direction == AMDTP_IN_STREAM) | |
950 | context->callback.sc = in_stream_callback; | |
727d3a0b | 951 | else if (s->flags & CIP_SYNC_TO_DEVICE) |
7b3b0d85 TS |
952 | context->callback.sc = slave_stream_callback; |
953 | else | |
954 | context->callback.sc = out_stream_callback; | |
955 | ||
956 | context->callback.sc(context, cycle, header_length, header, s); | |
957 | } | |
958 | ||
31ef9134 | 959 | /** |
be4a2894 TS |
960 | * amdtp_stream_start - start transferring packets |
961 | * @s: the AMDTP stream to start | |
31ef9134 CL |
962 | * @channel: the isochronous channel on the bus |
963 | * @speed: firewire speed code | |
964 | * | |
965 | * The stream cannot be started until it has been configured with | |
be4a2894 TS |
966 | * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI |
967 | * device can be started. | |
31ef9134 | 968 | */ |
be4a2894 | 969 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) |
31ef9134 CL |
970 | { |
971 | static const struct { | |
972 | unsigned int data_block; | |
973 | unsigned int syt_offset; | |
974 | } initial_state[] = { | |
975 | [CIP_SFC_32000] = { 4, 3072 }, | |
976 | [CIP_SFC_48000] = { 6, 1024 }, | |
977 | [CIP_SFC_96000] = { 12, 1024 }, | |
978 | [CIP_SFC_192000] = { 24, 1024 }, | |
979 | [CIP_SFC_44100] = { 0, 67 }, | |
980 | [CIP_SFC_88200] = { 0, 67 }, | |
981 | [CIP_SFC_176400] = { 0, 67 }, | |
982 | }; | |
2b3fc456 TS |
983 | unsigned int header_size; |
984 | enum dma_data_direction dir; | |
7ab56645 | 985 | int type, tag, err; |
31ef9134 CL |
986 | |
987 | mutex_lock(&s->mutex); | |
988 | ||
be4a2894 | 989 | if (WARN_ON(amdtp_stream_running(s) || |
4b7da117 | 990 | (s->data_block_quadlets < 1))) { |
31ef9134 CL |
991 | err = -EBADFD; |
992 | goto err_unlock; | |
993 | } | |
994 | ||
b6bc8123 TS |
995 | if (s->direction == AMDTP_IN_STREAM && |
996 | s->flags & CIP_SKIP_INIT_DBC_CHECK) | |
997 | s->data_block_counter = UINT_MAX; | |
998 | else | |
999 | s->data_block_counter = 0; | |
31ef9134 CL |
1000 | s->data_block_state = initial_state[s->sfc].data_block; |
1001 | s->syt_offset_state = initial_state[s->sfc].syt_offset; | |
1002 | s->last_syt_offset = TICKS_PER_CYCLE; | |
1003 | ||
2b3fc456 TS |
1004 | /* initialize packet buffer */ |
1005 | if (s->direction == AMDTP_IN_STREAM) { | |
1006 | dir = DMA_FROM_DEVICE; | |
1007 | type = FW_ISO_CONTEXT_RECEIVE; | |
1008 | header_size = IN_PACKET_HEADER_SIZE; | |
2b3fc456 TS |
1009 | } else { |
1010 | dir = DMA_TO_DEVICE; | |
1011 | type = FW_ISO_CONTEXT_TRANSMIT; | |
1012 | header_size = OUT_PACKET_HEADER_SIZE; | |
2b3fc456 | 1013 | } |
31ef9134 | 1014 | err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, |
2b3fc456 | 1015 | amdtp_stream_get_max_payload(s), dir); |
31ef9134 CL |
1016 | if (err < 0) |
1017 | goto err_unlock; | |
1018 | ||
1019 | s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, | |
2b3fc456 | 1020 | type, channel, speed, header_size, |
7b3b0d85 | 1021 | amdtp_stream_first_callback, s); |
31ef9134 CL |
1022 | if (IS_ERR(s->context)) { |
1023 | err = PTR_ERR(s->context); | |
1024 | if (err == -EBUSY) | |
1025 | dev_err(&s->unit->device, | |
be4a2894 | 1026 | "no free stream on this controller\n"); |
31ef9134 CL |
1027 | goto err_buffer; |
1028 | } | |
1029 | ||
be4a2894 | 1030 | amdtp_stream_update(s); |
31ef9134 | 1031 | |
ec00f5e4 | 1032 | s->packet_index = 0; |
4b7da117 | 1033 | do { |
2b3fc456 TS |
1034 | if (s->direction == AMDTP_IN_STREAM) |
1035 | err = queue_in_packet(s); | |
1036 | else | |
1037 | err = queue_out_packet(s, 0, true); | |
4b7da117 TS |
1038 | if (err < 0) |
1039 | goto err_context; | |
1040 | } while (s->packet_index > 0); | |
31ef9134 | 1041 | |
2b3fc456 | 1042 | /* NOTE: TAG1 matches CIP. This just affects in stream. */ |
7ab56645 TS |
1043 | tag = FW_ISO_CONTEXT_MATCH_TAG1; |
1044 | if (s->flags & CIP_EMPTY_WITH_TAG0) | |
1045 | tag |= FW_ISO_CONTEXT_MATCH_TAG0; | |
1046 | ||
7b3b0d85 | 1047 | s->callbacked = false; |
7ab56645 | 1048 | err = fw_iso_context_start(s->context, -1, 0, tag); |
31ef9134 CL |
1049 | if (err < 0) |
1050 | goto err_context; | |
1051 | ||
1052 | mutex_unlock(&s->mutex); | |
1053 | ||
1054 | return 0; | |
1055 | ||
1056 | err_context: | |
1057 | fw_iso_context_destroy(s->context); | |
1058 | s->context = ERR_PTR(-1); | |
1059 | err_buffer: | |
1060 | iso_packets_buffer_destroy(&s->buffer, s->unit); | |
1061 | err_unlock: | |
1062 | mutex_unlock(&s->mutex); | |
1063 | ||
1064 | return err; | |
1065 | } | |
be4a2894 | 1066 | EXPORT_SYMBOL(amdtp_stream_start); |
31ef9134 | 1067 | |
e9148ddd | 1068 | /** |
be4a2894 TS |
1069 | * amdtp_stream_pcm_pointer - get the PCM buffer position |
1070 | * @s: the AMDTP stream that transports the PCM data | |
e9148ddd CL |
1071 | * |
1072 | * Returns the current buffer position, in frames. | |
1073 | */ | |
be4a2894 | 1074 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) |
e9148ddd | 1075 | { |
92b862c7 | 1076 | /* this optimization is allowed to be racy */ |
c8de6dbb | 1077 | if (s->pointer_flush && amdtp_stream_running(s)) |
92b862c7 CL |
1078 | fw_iso_context_flush_completions(s->context); |
1079 | else | |
1080 | s->pointer_flush = true; | |
e9148ddd CL |
1081 | |
1082 | return ACCESS_ONCE(s->pcm_buffer_pointer); | |
1083 | } | |
be4a2894 | 1084 | EXPORT_SYMBOL(amdtp_stream_pcm_pointer); |
e9148ddd | 1085 | |
31ef9134 | 1086 | /** |
be4a2894 TS |
1087 | * amdtp_stream_update - update the stream after a bus reset |
1088 | * @s: the AMDTP stream | |
31ef9134 | 1089 | */ |
be4a2894 | 1090 | void amdtp_stream_update(struct amdtp_stream *s) |
31ef9134 | 1091 | { |
9a2820c1 | 1092 | /* Precomputing. */ |
31ef9134 | 1093 | ACCESS_ONCE(s->source_node_id_field) = |
9a2820c1 TS |
1094 | (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & |
1095 | CIP_SID_MASK; | |
31ef9134 | 1096 | } |
be4a2894 | 1097 | EXPORT_SYMBOL(amdtp_stream_update); |
31ef9134 CL |
1098 | |
1099 | /** | |
be4a2894 TS |
1100 | * amdtp_stream_stop - stop sending packets |
1101 | * @s: the AMDTP stream to stop | |
31ef9134 CL |
1102 | * |
1103 | * All PCM and MIDI devices of the stream must be stopped before the stream | |
1104 | * itself can be stopped. | |
1105 | */ | |
be4a2894 | 1106 | void amdtp_stream_stop(struct amdtp_stream *s) |
31ef9134 CL |
1107 | { |
1108 | mutex_lock(&s->mutex); | |
1109 | ||
be4a2894 | 1110 | if (!amdtp_stream_running(s)) { |
31ef9134 CL |
1111 | mutex_unlock(&s->mutex); |
1112 | return; | |
1113 | } | |
1114 | ||
76fb8789 | 1115 | tasklet_kill(&s->period_tasklet); |
31ef9134 CL |
1116 | fw_iso_context_stop(s->context); |
1117 | fw_iso_context_destroy(s->context); | |
1118 | s->context = ERR_PTR(-1); | |
1119 | iso_packets_buffer_destroy(&s->buffer, s->unit); | |
1120 | ||
7b3b0d85 TS |
1121 | s->callbacked = false; |
1122 | ||
31ef9134 CL |
1123 | mutex_unlock(&s->mutex); |
1124 | } | |
be4a2894 | 1125 | EXPORT_SYMBOL(amdtp_stream_stop); |
31ef9134 CL |
1126 | |
1127 | /** | |
be4a2894 | 1128 | * amdtp_stream_pcm_abort - abort the running PCM device |
31ef9134 CL |
1129 | * @s: the AMDTP stream about to be stopped |
1130 | * | |
1131 | * If the isochronous stream needs to be stopped asynchronously, call this | |
1132 | * function first to stop the PCM device. | |
1133 | */ | |
be4a2894 | 1134 | void amdtp_stream_pcm_abort(struct amdtp_stream *s) |
31ef9134 CL |
1135 | { |
1136 | struct snd_pcm_substream *pcm; | |
1137 | ||
1138 | pcm = ACCESS_ONCE(s->pcm); | |
1fb8510c TI |
1139 | if (pcm) |
1140 | snd_pcm_stop_xrun(pcm); | |
31ef9134 | 1141 | } |
be4a2894 | 1142 | EXPORT_SYMBOL(amdtp_stream_pcm_abort); |