]>
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> | |
15 | #include "amdtp.h" | |
16 | ||
17 | #define TICKS_PER_CYCLE 3072 | |
18 | #define CYCLES_PER_SECOND 8000 | |
19 | #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) | |
20 | ||
21 | #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */ | |
22 | ||
b445db44 TS |
23 | /* isochronous header parameters */ |
24 | #define ISO_DATA_LENGTH_SHIFT 16 | |
31ef9134 CL |
25 | #define TAG_CIP 1 |
26 | ||
b445db44 | 27 | /* common isochronous packet header parameters */ |
31ef9134 | 28 | #define CIP_EOH (1u << 31) |
b445db44 | 29 | #define CIP_EOH_MASK 0x80000000 |
31ef9134 | 30 | #define CIP_FMT_AM (0x10 << 24) |
b445db44 TS |
31 | #define CIP_FMT_MASK 0x3f000000 |
32 | #define CIP_SYT_MASK 0x0000ffff | |
33 | #define CIP_SYT_NO_INFO 0xffff | |
34 | #define CIP_FDF_MASK 0x00ff0000 | |
35 | #define CIP_FDF_SFC_SHIFT 16 | |
36 | ||
37 | /* | |
38 | * Audio and Music transfer protocol specific parameters | |
39 | * only "Clock-based rate control mode" is supported | |
40 | */ | |
41 | #define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3)) | |
42 | #define AMDTP_DBS_MASK 0x00ff0000 | |
43 | #define AMDTP_DBS_SHIFT 16 | |
44 | #define AMDTP_DBC_MASK 0x000000ff | |
31ef9134 CL |
45 | |
46 | /* TODO: make these configurable */ | |
47 | #define INTERRUPT_INTERVAL 16 | |
48 | #define QUEUE_LENGTH 48 | |
49 | ||
76fb8789 CL |
50 | static void pcm_period_tasklet(unsigned long data); |
51 | ||
31ef9134 | 52 | /** |
be4a2894 TS |
53 | * amdtp_stream_init - initialize an AMDTP stream structure |
54 | * @s: the AMDTP stream to initialize | |
31ef9134 | 55 | * @unit: the target of the stream |
3ff7e8f0 | 56 | * @dir: the direction of stream |
31ef9134 CL |
57 | * @flags: the packet transmission method to use |
58 | */ | |
be4a2894 | 59 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
3ff7e8f0 | 60 | enum amdtp_stream_direction dir, enum cip_flags flags) |
31ef9134 | 61 | { |
31ef9134 | 62 | s->unit = fw_unit_get(unit); |
3ff7e8f0 | 63 | s->direction = dir; |
31ef9134 CL |
64 | s->flags = flags; |
65 | s->context = ERR_PTR(-1); | |
66 | mutex_init(&s->mutex); | |
76fb8789 | 67 | tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); |
ec00f5e4 | 68 | s->packet_index = 0; |
31ef9134 CL |
69 | |
70 | return 0; | |
71 | } | |
be4a2894 | 72 | EXPORT_SYMBOL(amdtp_stream_init); |
31ef9134 CL |
73 | |
74 | /** | |
be4a2894 TS |
75 | * amdtp_stream_destroy - free stream resources |
76 | * @s: the AMDTP stream to destroy | |
31ef9134 | 77 | */ |
be4a2894 | 78 | void amdtp_stream_destroy(struct amdtp_stream *s) |
31ef9134 | 79 | { |
be4a2894 | 80 | WARN_ON(amdtp_stream_running(s)); |
31ef9134 CL |
81 | mutex_destroy(&s->mutex); |
82 | fw_unit_put(s->unit); | |
83 | } | |
be4a2894 | 84 | EXPORT_SYMBOL(amdtp_stream_destroy); |
31ef9134 | 85 | |
c5280e99 | 86 | const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { |
a7304e3b CL |
87 | [CIP_SFC_32000] = 8, |
88 | [CIP_SFC_44100] = 8, | |
89 | [CIP_SFC_48000] = 8, | |
90 | [CIP_SFC_88200] = 16, | |
91 | [CIP_SFC_96000] = 16, | |
92 | [CIP_SFC_176400] = 32, | |
93 | [CIP_SFC_192000] = 32, | |
94 | }; | |
95 | EXPORT_SYMBOL(amdtp_syt_intervals); | |
96 | ||
31ef9134 | 97 | /** |
be4a2894 TS |
98 | * amdtp_stream_set_parameters - set stream parameters |
99 | * @s: the AMDTP stream to configure | |
31ef9134 | 100 | * @rate: the sample rate |
a7304e3b CL |
101 | * @pcm_channels: the number of PCM samples in each data block, to be encoded |
102 | * as AM824 multi-bit linear audio | |
103 | * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels) | |
31ef9134 | 104 | * |
a7304e3b | 105 | * The parameters must be set before the stream is started, and must not be |
31ef9134 CL |
106 | * changed while the stream is running. |
107 | */ | |
be4a2894 TS |
108 | void amdtp_stream_set_parameters(struct amdtp_stream *s, |
109 | unsigned int rate, | |
110 | unsigned int pcm_channels, | |
111 | unsigned int midi_ports) | |
31ef9134 | 112 | { |
a7304e3b CL |
113 | static const unsigned int rates[] = { |
114 | [CIP_SFC_32000] = 32000, | |
115 | [CIP_SFC_44100] = 44100, | |
116 | [CIP_SFC_48000] = 48000, | |
117 | [CIP_SFC_88200] = 88200, | |
118 | [CIP_SFC_96000] = 96000, | |
119 | [CIP_SFC_176400] = 176400, | |
120 | [CIP_SFC_192000] = 192000, | |
31ef9134 CL |
121 | }; |
122 | unsigned int sfc; | |
123 | ||
be4a2894 | 124 | if (WARN_ON(amdtp_stream_running(s))) |
31ef9134 CL |
125 | return; |
126 | ||
a7304e3b CL |
127 | for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc) |
128 | if (rates[sfc] == rate) | |
e84d15f6 | 129 | goto sfc_found; |
31ef9134 | 130 | WARN_ON(1); |
e84d15f6 CL |
131 | return; |
132 | ||
133 | sfc_found: | |
a7304e3b CL |
134 | s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000; |
135 | if (s->dual_wire) { | |
136 | sfc -= 2; | |
137 | rate /= 2; | |
138 | pcm_channels *= 2; | |
139 | } | |
e84d15f6 | 140 | s->sfc = sfc; |
a7304e3b CL |
141 | s->data_block_quadlets = pcm_channels + DIV_ROUND_UP(midi_ports, 8); |
142 | s->pcm_channels = pcm_channels; | |
143 | s->midi_ports = midi_ports; | |
144 | ||
145 | s->syt_interval = amdtp_syt_intervals[sfc]; | |
e84d15f6 CL |
146 | |
147 | /* default buffering in the device */ | |
148 | s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; | |
149 | if (s->flags & CIP_BLOCKING) | |
150 | /* additional buffering needed to adjust for no-data packets */ | |
151 | s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; | |
31ef9134 | 152 | } |
be4a2894 | 153 | EXPORT_SYMBOL(amdtp_stream_set_parameters); |
31ef9134 CL |
154 | |
155 | /** | |
be4a2894 TS |
156 | * amdtp_stream_get_max_payload - get the stream's packet size |
157 | * @s: the AMDTP stream | |
31ef9134 CL |
158 | * |
159 | * This function must not be called before the stream has been configured | |
be4a2894 | 160 | * with amdtp_stream_set_parameters(). |
31ef9134 | 161 | */ |
be4a2894 | 162 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) |
31ef9134 | 163 | { |
e84d15f6 | 164 | return 8 + s->syt_interval * s->data_block_quadlets * 4; |
31ef9134 | 165 | } |
be4a2894 | 166 | EXPORT_SYMBOL(amdtp_stream_get_max_payload); |
31ef9134 | 167 | |
be4a2894 | 168 | static void amdtp_write_s16(struct amdtp_stream *s, |
31ef9134 CL |
169 | struct snd_pcm_substream *pcm, |
170 | __be32 *buffer, unsigned int frames); | |
be4a2894 | 171 | static void amdtp_write_s32(struct amdtp_stream *s, |
31ef9134 CL |
172 | struct snd_pcm_substream *pcm, |
173 | __be32 *buffer, unsigned int frames); | |
be4a2894 | 174 | static void amdtp_write_s16_dualwire(struct amdtp_stream *s, |
a7304e3b CL |
175 | struct snd_pcm_substream *pcm, |
176 | __be32 *buffer, unsigned int frames); | |
be4a2894 | 177 | static void amdtp_write_s32_dualwire(struct amdtp_stream *s, |
a7304e3b CL |
178 | struct snd_pcm_substream *pcm, |
179 | __be32 *buffer, unsigned int frames); | |
31ef9134 CL |
180 | |
181 | /** | |
be4a2894 TS |
182 | * amdtp_stream_set_pcm_format - set the PCM format |
183 | * @s: the AMDTP stream to configure | |
31ef9134 CL |
184 | * @format: the format of the ALSA PCM device |
185 | * | |
a7304e3b CL |
186 | * The sample format must be set after the other paramters (rate/PCM channels/ |
187 | * MIDI) and before the stream is started, and must not be changed while the | |
188 | * stream is running. | |
31ef9134 | 189 | */ |
be4a2894 TS |
190 | void amdtp_stream_set_pcm_format(struct amdtp_stream *s, |
191 | snd_pcm_format_t format) | |
31ef9134 | 192 | { |
be4a2894 | 193 | if (WARN_ON(amdtp_stream_running(s))) |
31ef9134 CL |
194 | return; |
195 | ||
196 | switch (format) { | |
197 | default: | |
198 | WARN_ON(1); | |
199 | /* fall through */ | |
200 | case SNDRV_PCM_FORMAT_S16: | |
a7304e3b CL |
201 | if (s->dual_wire) |
202 | s->transfer_samples = amdtp_write_s16_dualwire; | |
203 | else | |
204 | s->transfer_samples = amdtp_write_s16; | |
31ef9134 CL |
205 | break; |
206 | case SNDRV_PCM_FORMAT_S32: | |
a7304e3b CL |
207 | if (s->dual_wire) |
208 | s->transfer_samples = amdtp_write_s32_dualwire; | |
209 | else | |
210 | s->transfer_samples = amdtp_write_s32; | |
31ef9134 CL |
211 | break; |
212 | } | |
213 | } | |
be4a2894 | 214 | EXPORT_SYMBOL(amdtp_stream_set_pcm_format); |
31ef9134 | 215 | |
76fb8789 | 216 | /** |
be4a2894 TS |
217 | * amdtp_stream_pcm_prepare - prepare PCM device for running |
218 | * @s: the AMDTP stream | |
76fb8789 CL |
219 | * |
220 | * This function should be called from the PCM device's .prepare callback. | |
221 | */ | |
be4a2894 | 222 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s) |
76fb8789 CL |
223 | { |
224 | tasklet_kill(&s->period_tasklet); | |
225 | s->pcm_buffer_pointer = 0; | |
226 | s->pcm_period_pointer = 0; | |
92b862c7 | 227 | s->pointer_flush = true; |
76fb8789 | 228 | } |
be4a2894 | 229 | EXPORT_SYMBOL(amdtp_stream_pcm_prepare); |
76fb8789 | 230 | |
be4a2894 | 231 | static unsigned int calculate_data_blocks(struct amdtp_stream *s) |
31ef9134 CL |
232 | { |
233 | unsigned int phase, data_blocks; | |
234 | ||
235 | if (!cip_sfc_is_base_44100(s->sfc)) { | |
236 | /* Sample_rate / 8000 is an integer, and precomputed. */ | |
237 | data_blocks = s->data_block_state; | |
238 | } else { | |
239 | phase = s->data_block_state; | |
240 | ||
241 | /* | |
242 | * This calculates the number of data blocks per packet so that | |
243 | * 1) the overall rate is correct and exactly synchronized to | |
244 | * the bus clock, and | |
245 | * 2) packets with a rounded-up number of blocks occur as early | |
246 | * as possible in the sequence (to prevent underruns of the | |
247 | * device's buffer). | |
248 | */ | |
249 | if (s->sfc == CIP_SFC_44100) | |
250 | /* 6 6 5 6 5 6 5 ... */ | |
251 | data_blocks = 5 + ((phase & 1) ^ | |
252 | (phase == 0 || phase >= 40)); | |
253 | else | |
254 | /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ | |
255 | data_blocks = 11 * (s->sfc >> 1) + (phase == 0); | |
256 | if (++phase >= (80 >> (s->sfc >> 1))) | |
257 | phase = 0; | |
258 | s->data_block_state = phase; | |
259 | } | |
260 | ||
261 | return data_blocks; | |
262 | } | |
263 | ||
be4a2894 | 264 | static unsigned int calculate_syt(struct amdtp_stream *s, |
31ef9134 CL |
265 | unsigned int cycle) |
266 | { | |
267 | unsigned int syt_offset, phase, index, syt; | |
268 | ||
269 | if (s->last_syt_offset < TICKS_PER_CYCLE) { | |
270 | if (!cip_sfc_is_base_44100(s->sfc)) | |
271 | syt_offset = s->last_syt_offset + s->syt_offset_state; | |
272 | else { | |
273 | /* | |
274 | * The time, in ticks, of the n'th SYT_INTERVAL sample is: | |
275 | * n * SYT_INTERVAL * 24576000 / sample_rate | |
276 | * Modulo TICKS_PER_CYCLE, the difference between successive | |
277 | * elements is about 1386.23. Rounding the results of this | |
278 | * formula to the SYT precision results in a sequence of | |
279 | * differences that begins with: | |
280 | * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... | |
281 | * This code generates _exactly_ the same sequence. | |
282 | */ | |
283 | phase = s->syt_offset_state; | |
284 | index = phase % 13; | |
285 | syt_offset = s->last_syt_offset; | |
286 | syt_offset += 1386 + ((index && !(index & 3)) || | |
287 | phase == 146); | |
288 | if (++phase >= 147) | |
289 | phase = 0; | |
290 | s->syt_offset_state = phase; | |
291 | } | |
292 | } else | |
293 | syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; | |
294 | s->last_syt_offset = syt_offset; | |
295 | ||
be454366 | 296 | if (syt_offset < TICKS_PER_CYCLE) { |
e84d15f6 | 297 | syt_offset += s->transfer_delay; |
be454366 CL |
298 | syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; |
299 | syt += syt_offset % TICKS_PER_CYCLE; | |
31ef9134 | 300 | |
b445db44 | 301 | return syt & CIP_SYT_MASK; |
be454366 | 302 | } else { |
b445db44 | 303 | return CIP_SYT_NO_INFO; |
be454366 | 304 | } |
31ef9134 CL |
305 | } |
306 | ||
be4a2894 | 307 | static void amdtp_write_s32(struct amdtp_stream *s, |
31ef9134 CL |
308 | struct snd_pcm_substream *pcm, |
309 | __be32 *buffer, unsigned int frames) | |
310 | { | |
311 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
312 | unsigned int channels, remaining_frames, frame_step, i, c; | |
313 | const u32 *src; | |
314 | ||
315 | channels = s->pcm_channels; | |
316 | src = (void *)runtime->dma_area + | |
e84841f9 | 317 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
31ef9134 CL |
318 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
319 | frame_step = s->data_block_quadlets - channels; | |
320 | ||
321 | for (i = 0; i < frames; ++i) { | |
322 | for (c = 0; c < channels; ++c) { | |
323 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); | |
324 | src++; | |
325 | buffer++; | |
326 | } | |
327 | buffer += frame_step; | |
328 | if (--remaining_frames == 0) | |
329 | src = (void *)runtime->dma_area; | |
330 | } | |
331 | } | |
332 | ||
be4a2894 | 333 | static void amdtp_write_s16(struct amdtp_stream *s, |
31ef9134 CL |
334 | struct snd_pcm_substream *pcm, |
335 | __be32 *buffer, unsigned int frames) | |
336 | { | |
337 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
338 | unsigned int channels, remaining_frames, frame_step, i, c; | |
339 | const u16 *src; | |
340 | ||
341 | channels = s->pcm_channels; | |
342 | src = (void *)runtime->dma_area + | |
e84841f9 | 343 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
31ef9134 CL |
344 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
345 | frame_step = s->data_block_quadlets - channels; | |
346 | ||
347 | for (i = 0; i < frames; ++i) { | |
348 | for (c = 0; c < channels; ++c) { | |
349 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); | |
350 | src++; | |
351 | buffer++; | |
352 | } | |
353 | buffer += frame_step; | |
354 | if (--remaining_frames == 0) | |
355 | src = (void *)runtime->dma_area; | |
356 | } | |
357 | } | |
358 | ||
be4a2894 | 359 | static void amdtp_write_s32_dualwire(struct amdtp_stream *s, |
a7304e3b CL |
360 | struct snd_pcm_substream *pcm, |
361 | __be32 *buffer, unsigned int frames) | |
362 | { | |
363 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
364 | unsigned int channels, frame_adjust_1, frame_adjust_2, i, c; | |
365 | const u32 *src; | |
366 | ||
367 | channels = s->pcm_channels; | |
368 | src = (void *)runtime->dma_area + | |
369 | s->pcm_buffer_pointer * (runtime->frame_bits / 8); | |
370 | frame_adjust_1 = channels - 1; | |
371 | frame_adjust_2 = 1 - (s->data_block_quadlets - channels); | |
372 | ||
373 | channels /= 2; | |
374 | for (i = 0; i < frames; ++i) { | |
375 | for (c = 0; c < channels; ++c) { | |
376 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); | |
377 | src++; | |
378 | buffer += 2; | |
379 | } | |
380 | buffer -= frame_adjust_1; | |
381 | for (c = 0; c < channels; ++c) { | |
382 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); | |
383 | src++; | |
384 | buffer += 2; | |
385 | } | |
386 | buffer -= frame_adjust_2; | |
387 | } | |
388 | } | |
389 | ||
be4a2894 | 390 | static void amdtp_write_s16_dualwire(struct amdtp_stream *s, |
a7304e3b CL |
391 | struct snd_pcm_substream *pcm, |
392 | __be32 *buffer, unsigned int frames) | |
393 | { | |
394 | struct snd_pcm_runtime *runtime = pcm->runtime; | |
395 | unsigned int channels, frame_adjust_1, frame_adjust_2, i, c; | |
396 | const u16 *src; | |
397 | ||
398 | channels = s->pcm_channels; | |
399 | src = (void *)runtime->dma_area + | |
400 | s->pcm_buffer_pointer * (runtime->frame_bits / 8); | |
401 | frame_adjust_1 = channels - 1; | |
402 | frame_adjust_2 = 1 - (s->data_block_quadlets - channels); | |
403 | ||
404 | channels /= 2; | |
405 | for (i = 0; i < frames; ++i) { | |
406 | for (c = 0; c < channels; ++c) { | |
407 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); | |
408 | src++; | |
409 | buffer += 2; | |
410 | } | |
411 | buffer -= frame_adjust_1; | |
412 | for (c = 0; c < channels; ++c) { | |
413 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); | |
414 | src++; | |
415 | buffer += 2; | |
416 | } | |
417 | buffer -= frame_adjust_2; | |
418 | } | |
419 | } | |
420 | ||
be4a2894 | 421 | static void amdtp_fill_pcm_silence(struct amdtp_stream *s, |
31ef9134 CL |
422 | __be32 *buffer, unsigned int frames) |
423 | { | |
424 | unsigned int i, c; | |
425 | ||
426 | for (i = 0; i < frames; ++i) { | |
427 | for (c = 0; c < s->pcm_channels; ++c) | |
428 | buffer[c] = cpu_to_be32(0x40000000); | |
429 | buffer += s->data_block_quadlets; | |
430 | } | |
431 | } | |
432 | ||
be4a2894 | 433 | static void amdtp_fill_midi(struct amdtp_stream *s, |
31ef9134 CL |
434 | __be32 *buffer, unsigned int frames) |
435 | { | |
436 | unsigned int i; | |
437 | ||
438 | for (i = 0; i < frames; ++i) | |
439 | buffer[s->pcm_channels + i * s->data_block_quadlets] = | |
440 | cpu_to_be32(0x80000000); | |
441 | } | |
442 | ||
be4a2894 | 443 | static void queue_out_packet(struct amdtp_stream *s, unsigned int cycle) |
31ef9134 CL |
444 | { |
445 | __be32 *buffer; | |
ec00f5e4 | 446 | unsigned int index, data_blocks, syt, ptr; |
31ef9134 CL |
447 | struct snd_pcm_substream *pcm; |
448 | struct fw_iso_packet packet; | |
449 | int err; | |
450 | ||
ec00f5e4 CL |
451 | if (s->packet_index < 0) |
452 | return; | |
453 | index = s->packet_index; | |
454 | ||
82755abf | 455 | /* this module generate empty packet for 'no data' */ |
31ef9134 | 456 | syt = calculate_syt(s, cycle); |
82755abf | 457 | if (!(s->flags & CIP_BLOCKING)) |
e84d15f6 | 458 | data_blocks = calculate_data_blocks(s); |
b445db44 | 459 | else if (syt != CIP_SYT_NO_INFO) |
82755abf TS |
460 | data_blocks = s->syt_interval; |
461 | else | |
462 | data_blocks = 0; | |
31ef9134 | 463 | |
ec00f5e4 | 464 | buffer = s->buffer.packets[index].buffer; |
31ef9134 | 465 | buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | |
b445db44 | 466 | (s->data_block_quadlets << AMDTP_DBS_SHIFT) | |
31ef9134 CL |
467 | s->data_block_counter); |
468 | buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 | | |
b445db44 | 469 | (s->sfc << CIP_FDF_SFC_SHIFT) | syt); |
31ef9134 CL |
470 | buffer += 2; |
471 | ||
472 | pcm = ACCESS_ONCE(s->pcm); | |
473 | if (pcm) | |
474 | s->transfer_samples(s, pcm, buffer, data_blocks); | |
475 | else | |
476 | amdtp_fill_pcm_silence(s, buffer, data_blocks); | |
477 | if (s->midi_ports) | |
478 | amdtp_fill_midi(s, buffer, data_blocks); | |
479 | ||
480 | s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; | |
481 | ||
482 | packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; | |
ec00f5e4 | 483 | packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL); |
31ef9134 CL |
484 | packet.skip = 0; |
485 | packet.tag = TAG_CIP; | |
486 | packet.sy = 0; | |
487 | packet.header_length = 0; | |
488 | ||
489 | err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer, | |
ec00f5e4 CL |
490 | s->buffer.packets[index].offset); |
491 | if (err < 0) { | |
31ef9134 | 492 | dev_err(&s->unit->device, "queueing error: %d\n", err); |
ec00f5e4 | 493 | s->packet_index = -1; |
be4a2894 | 494 | amdtp_stream_pcm_abort(s); |
ec00f5e4 CL |
495 | return; |
496 | } | |
31ef9134 | 497 | |
ec00f5e4 CL |
498 | if (++index >= QUEUE_LENGTH) |
499 | index = 0; | |
500 | s->packet_index = index; | |
31ef9134 CL |
501 | |
502 | if (pcm) { | |
a7304e3b CL |
503 | if (s->dual_wire) |
504 | data_blocks *= 2; | |
505 | ||
31ef9134 CL |
506 | ptr = s->pcm_buffer_pointer + data_blocks; |
507 | if (ptr >= pcm->runtime->buffer_size) | |
508 | ptr -= pcm->runtime->buffer_size; | |
509 | ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; | |
510 | ||
511 | s->pcm_period_pointer += data_blocks; | |
512 | if (s->pcm_period_pointer >= pcm->runtime->period_size) { | |
513 | s->pcm_period_pointer -= pcm->runtime->period_size; | |
92b862c7 | 514 | s->pointer_flush = false; |
76fb8789 | 515 | tasklet_hi_schedule(&s->period_tasklet); |
31ef9134 CL |
516 | } |
517 | } | |
518 | } | |
519 | ||
76fb8789 CL |
520 | static void pcm_period_tasklet(unsigned long data) |
521 | { | |
be4a2894 | 522 | struct amdtp_stream *s = (void *)data; |
76fb8789 CL |
523 | struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); |
524 | ||
525 | if (pcm) | |
526 | snd_pcm_period_elapsed(pcm); | |
527 | } | |
528 | ||
31ef9134 | 529 | static void out_packet_callback(struct fw_iso_context *context, u32 cycle, |
be4a2894 | 530 | size_t header_length, void *header, void *private_data) |
31ef9134 | 531 | { |
be4a2894 | 532 | struct amdtp_stream *s = private_data; |
31ef9134 CL |
533 | unsigned int i, packets = header_length / 4; |
534 | ||
535 | /* | |
536 | * Compute the cycle of the last queued packet. | |
537 | * (We need only the four lowest bits for the SYT, so we can ignore | |
538 | * that bits 0-11 must wrap around at 3072.) | |
539 | */ | |
540 | cycle += QUEUE_LENGTH - packets; | |
541 | ||
542 | for (i = 0; i < packets; ++i) | |
543 | queue_out_packet(s, ++cycle); | |
13882a82 | 544 | fw_iso_context_queue_flush(s->context); |
31ef9134 CL |
545 | } |
546 | ||
be4a2894 | 547 | static int queue_initial_skip_packets(struct amdtp_stream *s) |
31ef9134 CL |
548 | { |
549 | struct fw_iso_packet skip_packet = { | |
550 | .skip = 1, | |
551 | }; | |
552 | unsigned int i; | |
553 | int err; | |
554 | ||
555 | for (i = 0; i < QUEUE_LENGTH; ++i) { | |
ec00f5e4 | 556 | skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1, |
31ef9134 CL |
557 | INTERRUPT_INTERVAL); |
558 | err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0); | |
559 | if (err < 0) | |
560 | return err; | |
ec00f5e4 CL |
561 | if (++s->packet_index >= QUEUE_LENGTH) |
562 | s->packet_index = 0; | |
31ef9134 CL |
563 | } |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
568 | /** | |
be4a2894 TS |
569 | * amdtp_stream_start - start transferring packets |
570 | * @s: the AMDTP stream to start | |
31ef9134 CL |
571 | * @channel: the isochronous channel on the bus |
572 | * @speed: firewire speed code | |
573 | * | |
574 | * The stream cannot be started until it has been configured with | |
be4a2894 TS |
575 | * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI |
576 | * device can be started. | |
31ef9134 | 577 | */ |
be4a2894 | 578 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) |
31ef9134 CL |
579 | { |
580 | static const struct { | |
581 | unsigned int data_block; | |
582 | unsigned int syt_offset; | |
583 | } initial_state[] = { | |
584 | [CIP_SFC_32000] = { 4, 3072 }, | |
585 | [CIP_SFC_48000] = { 6, 1024 }, | |
586 | [CIP_SFC_96000] = { 12, 1024 }, | |
587 | [CIP_SFC_192000] = { 24, 1024 }, | |
588 | [CIP_SFC_44100] = { 0, 67 }, | |
589 | [CIP_SFC_88200] = { 0, 67 }, | |
590 | [CIP_SFC_176400] = { 0, 67 }, | |
591 | }; | |
592 | int err; | |
593 | ||
594 | mutex_lock(&s->mutex); | |
595 | ||
be4a2894 | 596 | if (WARN_ON(amdtp_stream_running(s) || |
31ef9134 CL |
597 | (!s->pcm_channels && !s->midi_ports))) { |
598 | err = -EBADFD; | |
599 | goto err_unlock; | |
600 | } | |
601 | ||
602 | s->data_block_state = initial_state[s->sfc].data_block; | |
603 | s->syt_offset_state = initial_state[s->sfc].syt_offset; | |
604 | s->last_syt_offset = TICKS_PER_CYCLE; | |
605 | ||
606 | err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, | |
be4a2894 | 607 | amdtp_stream_get_max_payload(s), |
31ef9134 CL |
608 | DMA_TO_DEVICE); |
609 | if (err < 0) | |
610 | goto err_unlock; | |
611 | ||
612 | s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, | |
613 | FW_ISO_CONTEXT_TRANSMIT, | |
614 | channel, speed, 0, | |
615 | out_packet_callback, s); | |
616 | if (IS_ERR(s->context)) { | |
617 | err = PTR_ERR(s->context); | |
618 | if (err == -EBUSY) | |
619 | dev_err(&s->unit->device, | |
be4a2894 | 620 | "no free stream on this controller\n"); |
31ef9134 CL |
621 | goto err_buffer; |
622 | } | |
623 | ||
be4a2894 | 624 | amdtp_stream_update(s); |
31ef9134 | 625 | |
ec00f5e4 | 626 | s->packet_index = 0; |
31ef9134 CL |
627 | s->data_block_counter = 0; |
628 | err = queue_initial_skip_packets(s); | |
629 | if (err < 0) | |
630 | goto err_context; | |
631 | ||
632 | err = fw_iso_context_start(s->context, -1, 0, 0); | |
633 | if (err < 0) | |
634 | goto err_context; | |
635 | ||
636 | mutex_unlock(&s->mutex); | |
637 | ||
638 | return 0; | |
639 | ||
640 | err_context: | |
641 | fw_iso_context_destroy(s->context); | |
642 | s->context = ERR_PTR(-1); | |
643 | err_buffer: | |
644 | iso_packets_buffer_destroy(&s->buffer, s->unit); | |
645 | err_unlock: | |
646 | mutex_unlock(&s->mutex); | |
647 | ||
648 | return err; | |
649 | } | |
be4a2894 | 650 | EXPORT_SYMBOL(amdtp_stream_start); |
31ef9134 | 651 | |
e9148ddd | 652 | /** |
be4a2894 TS |
653 | * amdtp_stream_pcm_pointer - get the PCM buffer position |
654 | * @s: the AMDTP stream that transports the PCM data | |
e9148ddd CL |
655 | * |
656 | * Returns the current buffer position, in frames. | |
657 | */ | |
be4a2894 | 658 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) |
e9148ddd | 659 | { |
92b862c7 CL |
660 | /* this optimization is allowed to be racy */ |
661 | if (s->pointer_flush) | |
662 | fw_iso_context_flush_completions(s->context); | |
663 | else | |
664 | s->pointer_flush = true; | |
e9148ddd CL |
665 | |
666 | return ACCESS_ONCE(s->pcm_buffer_pointer); | |
667 | } | |
be4a2894 | 668 | EXPORT_SYMBOL(amdtp_stream_pcm_pointer); |
e9148ddd | 669 | |
31ef9134 | 670 | /** |
be4a2894 TS |
671 | * amdtp_stream_update - update the stream after a bus reset |
672 | * @s: the AMDTP stream | |
31ef9134 | 673 | */ |
be4a2894 | 674 | void amdtp_stream_update(struct amdtp_stream *s) |
31ef9134 CL |
675 | { |
676 | ACCESS_ONCE(s->source_node_id_field) = | |
677 | (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24; | |
678 | } | |
be4a2894 | 679 | EXPORT_SYMBOL(amdtp_stream_update); |
31ef9134 CL |
680 | |
681 | /** | |
be4a2894 TS |
682 | * amdtp_stream_stop - stop sending packets |
683 | * @s: the AMDTP stream to stop | |
31ef9134 CL |
684 | * |
685 | * All PCM and MIDI devices of the stream must be stopped before the stream | |
686 | * itself can be stopped. | |
687 | */ | |
be4a2894 | 688 | void amdtp_stream_stop(struct amdtp_stream *s) |
31ef9134 CL |
689 | { |
690 | mutex_lock(&s->mutex); | |
691 | ||
be4a2894 | 692 | if (!amdtp_stream_running(s)) { |
31ef9134 CL |
693 | mutex_unlock(&s->mutex); |
694 | return; | |
695 | } | |
696 | ||
76fb8789 | 697 | tasklet_kill(&s->period_tasklet); |
31ef9134 CL |
698 | fw_iso_context_stop(s->context); |
699 | fw_iso_context_destroy(s->context); | |
700 | s->context = ERR_PTR(-1); | |
701 | iso_packets_buffer_destroy(&s->buffer, s->unit); | |
702 | ||
703 | mutex_unlock(&s->mutex); | |
704 | } | |
be4a2894 | 705 | EXPORT_SYMBOL(amdtp_stream_stop); |
31ef9134 CL |
706 | |
707 | /** | |
be4a2894 | 708 | * amdtp_stream_pcm_abort - abort the running PCM device |
31ef9134 CL |
709 | * @s: the AMDTP stream about to be stopped |
710 | * | |
711 | * If the isochronous stream needs to be stopped asynchronously, call this | |
712 | * function first to stop the PCM device. | |
713 | */ | |
be4a2894 | 714 | void amdtp_stream_pcm_abort(struct amdtp_stream *s) |
31ef9134 CL |
715 | { |
716 | struct snd_pcm_substream *pcm; | |
717 | ||
718 | pcm = ACCESS_ONCE(s->pcm); | |
719 | if (pcm) { | |
720 | snd_pcm_stream_lock_irq(pcm); | |
721 | if (snd_pcm_running(pcm)) | |
722 | snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN); | |
723 | snd_pcm_stream_unlock_irq(pcm); | |
724 | } | |
725 | } | |
be4a2894 | 726 | EXPORT_SYMBOL(amdtp_stream_pcm_abort); |