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