1
2
3
4
5
6
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
22
23#define TAG_CIP 1
24
25#define CIP_EOH (1u << 31)
26#define CIP_FMT_AM (0x10 << 24)
27#define AMDTP_FDF_AM824 (0 << 19)
28#define AMDTP_FDF_SFC_SHIFT 16
29
30
31#define INTERRUPT_INTERVAL 16
32#define QUEUE_LENGTH 48
33
34static void pcm_period_tasklet(unsigned long data);
35
36
37
38
39
40
41
42int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
43 enum cip_out_flags flags)
44{
45 if (flags != CIP_NONBLOCKING)
46 return -EINVAL;
47
48 s->unit = fw_unit_get(unit);
49 s->flags = flags;
50 s->context = ERR_PTR(-1);
51 mutex_init(&s->mutex);
52 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
53 s->packet_index = 0;
54
55 return 0;
56}
57EXPORT_SYMBOL(amdtp_out_stream_init);
58
59
60
61
62
63void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
64{
65 WARN_ON(!IS_ERR(s->context));
66 mutex_destroy(&s->mutex);
67 fw_unit_put(s->unit);
68}
69EXPORT_SYMBOL(amdtp_out_stream_destroy);
70
71
72
73
74
75
76
77
78
79void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
80{
81 static const struct {
82 unsigned int rate;
83 unsigned int syt_interval;
84 } rate_info[] = {
85 [CIP_SFC_32000] = { 32000, 8, },
86 [CIP_SFC_44100] = { 44100, 8, },
87 [CIP_SFC_48000] = { 48000, 8, },
88 [CIP_SFC_88200] = { 88200, 16, },
89 [CIP_SFC_96000] = { 96000, 16, },
90 [CIP_SFC_176400] = { 176400, 32, },
91 [CIP_SFC_192000] = { 192000, 32, },
92 };
93 unsigned int sfc;
94
95 if (WARN_ON(!IS_ERR(s->context)))
96 return;
97
98 for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
99 if (rate_info[sfc].rate == rate) {
100 s->sfc = sfc;
101 s->syt_interval = rate_info[sfc].syt_interval;
102 return;
103 }
104 WARN_ON(1);
105}
106EXPORT_SYMBOL(amdtp_out_stream_set_rate);
107
108
109
110
111
112
113
114
115
116unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
117{
118 static const unsigned int max_data_blocks[] = {
119 [CIP_SFC_32000] = 4,
120 [CIP_SFC_44100] = 6,
121 [CIP_SFC_48000] = 6,
122 [CIP_SFC_88200] = 12,
123 [CIP_SFC_96000] = 12,
124 [CIP_SFC_176400] = 23,
125 [CIP_SFC_192000] = 24,
126 };
127
128 s->data_block_quadlets = s->pcm_channels;
129 s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
130
131 return 8 + max_data_blocks[s->sfc] * 4 * s->data_block_quadlets;
132}
133EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
134
135static void amdtp_write_s16(struct amdtp_out_stream *s,
136 struct snd_pcm_substream *pcm,
137 __be32 *buffer, unsigned int frames);
138static void amdtp_write_s32(struct amdtp_out_stream *s,
139 struct snd_pcm_substream *pcm,
140 __be32 *buffer, unsigned int frames);
141
142
143
144
145
146
147
148
149
150void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
151 snd_pcm_format_t format)
152{
153 if (WARN_ON(!IS_ERR(s->context)))
154 return;
155
156 switch (format) {
157 default:
158 WARN_ON(1);
159
160 case SNDRV_PCM_FORMAT_S16:
161 s->transfer_samples = amdtp_write_s16;
162 break;
163 case SNDRV_PCM_FORMAT_S32:
164 s->transfer_samples = amdtp_write_s32;
165 break;
166 }
167}
168EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
169
170
171
172
173
174
175
176void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)
177{
178 tasklet_kill(&s->period_tasklet);
179 s->pcm_buffer_pointer = 0;
180 s->pcm_period_pointer = 0;
181 s->pointer_flush = true;
182}
183EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare);
184
185static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
186{
187 unsigned int phase, data_blocks;
188
189 if (!cip_sfc_is_base_44100(s->sfc)) {
190
191 data_blocks = s->data_block_state;
192 } else {
193 phase = s->data_block_state;
194
195
196
197
198
199
200
201
202
203 if (s->sfc == CIP_SFC_44100)
204
205 data_blocks = 5 + ((phase & 1) ^
206 (phase == 0 || phase >= 40));
207 else
208
209 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
210 if (++phase >= (80 >> (s->sfc >> 1)))
211 phase = 0;
212 s->data_block_state = phase;
213 }
214
215 return data_blocks;
216}
217
218static unsigned int calculate_syt(struct amdtp_out_stream *s,
219 unsigned int cycle)
220{
221 unsigned int syt_offset, phase, index, syt;
222
223 if (s->last_syt_offset < TICKS_PER_CYCLE) {
224 if (!cip_sfc_is_base_44100(s->sfc))
225 syt_offset = s->last_syt_offset + s->syt_offset_state;
226 else {
227
228
229
230
231
232
233
234
235
236
237 phase = s->syt_offset_state;
238 index = phase % 13;
239 syt_offset = s->last_syt_offset;
240 syt_offset += 1386 + ((index && !(index & 3)) ||
241 phase == 146);
242 if (++phase >= 147)
243 phase = 0;
244 s->syt_offset_state = phase;
245 }
246 } else
247 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
248 s->last_syt_offset = syt_offset;
249
250 if (syt_offset < TICKS_PER_CYCLE) {
251 syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
252 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
253 syt += syt_offset % TICKS_PER_CYCLE;
254
255 return syt & 0xffff;
256 } else {
257 return 0xffff;
258 }
259}
260
261static void amdtp_write_s32(struct amdtp_out_stream *s,
262 struct snd_pcm_substream *pcm,
263 __be32 *buffer, unsigned int frames)
264{
265 struct snd_pcm_runtime *runtime = pcm->runtime;
266 unsigned int channels, remaining_frames, frame_step, i, c;
267 const u32 *src;
268
269 channels = s->pcm_channels;
270 src = (void *)runtime->dma_area +
271 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
272 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
273 frame_step = s->data_block_quadlets - channels;
274
275 for (i = 0; i < frames; ++i) {
276 for (c = 0; c < channels; ++c) {
277 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
278 src++;
279 buffer++;
280 }
281 buffer += frame_step;
282 if (--remaining_frames == 0)
283 src = (void *)runtime->dma_area;
284 }
285}
286
287static void amdtp_write_s16(struct amdtp_out_stream *s,
288 struct snd_pcm_substream *pcm,
289 __be32 *buffer, unsigned int frames)
290{
291 struct snd_pcm_runtime *runtime = pcm->runtime;
292 unsigned int channels, remaining_frames, frame_step, i, c;
293 const u16 *src;
294
295 channels = s->pcm_channels;
296 src = (void *)runtime->dma_area +
297 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
298 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
299 frame_step = s->data_block_quadlets - channels;
300
301 for (i = 0; i < frames; ++i) {
302 for (c = 0; c < channels; ++c) {
303 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
304 src++;
305 buffer++;
306 }
307 buffer += frame_step;
308 if (--remaining_frames == 0)
309 src = (void *)runtime->dma_area;
310 }
311}
312
313static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
314 __be32 *buffer, unsigned int frames)
315{
316 unsigned int i, c;
317
318 for (i = 0; i < frames; ++i) {
319 for (c = 0; c < s->pcm_channels; ++c)
320 buffer[c] = cpu_to_be32(0x40000000);
321 buffer += s->data_block_quadlets;
322 }
323}
324
325static void amdtp_fill_midi(struct amdtp_out_stream *s,
326 __be32 *buffer, unsigned int frames)
327{
328 unsigned int i;
329
330 for (i = 0; i < frames; ++i)
331 buffer[s->pcm_channels + i * s->data_block_quadlets] =
332 cpu_to_be32(0x80000000);
333}
334
335static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
336{
337 __be32 *buffer;
338 unsigned int index, data_blocks, syt, ptr;
339 struct snd_pcm_substream *pcm;
340 struct fw_iso_packet packet;
341 int err;
342
343 if (s->packet_index < 0)
344 return;
345 index = s->packet_index;
346
347 data_blocks = calculate_data_blocks(s);
348 syt = calculate_syt(s, cycle);
349
350 buffer = s->buffer.packets[index].buffer;
351 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
352 (s->data_block_quadlets << 16) |
353 s->data_block_counter);
354 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
355 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
356 buffer += 2;
357
358 pcm = ACCESS_ONCE(s->pcm);
359 if (pcm)
360 s->transfer_samples(s, pcm, buffer, data_blocks);
361 else
362 amdtp_fill_pcm_silence(s, buffer, data_blocks);
363 if (s->midi_ports)
364 amdtp_fill_midi(s, buffer, data_blocks);
365
366 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
367
368 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
369 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
370 packet.skip = 0;
371 packet.tag = TAG_CIP;
372 packet.sy = 0;
373 packet.header_length = 0;
374
375 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
376 s->buffer.packets[index].offset);
377 if (err < 0) {
378 dev_err(&s->unit->device, "queueing error: %d\n", err);
379 s->packet_index = -1;
380 amdtp_out_stream_pcm_abort(s);
381 return;
382 }
383
384 if (++index >= QUEUE_LENGTH)
385 index = 0;
386 s->packet_index = index;
387
388 if (pcm) {
389 ptr = s->pcm_buffer_pointer + data_blocks;
390 if (ptr >= pcm->runtime->buffer_size)
391 ptr -= pcm->runtime->buffer_size;
392 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
393
394 s->pcm_period_pointer += data_blocks;
395 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
396 s->pcm_period_pointer -= pcm->runtime->period_size;
397 s->pointer_flush = false;
398 tasklet_hi_schedule(&s->period_tasklet);
399 }
400 }
401}
402
403static void pcm_period_tasklet(unsigned long data)
404{
405 struct amdtp_out_stream *s = (void *)data;
406 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
407
408 if (pcm)
409 snd_pcm_period_elapsed(pcm);
410}
411
412static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
413 size_t header_length, void *header, void *data)
414{
415 struct amdtp_out_stream *s = data;
416 unsigned int i, packets = header_length / 4;
417
418
419
420
421
422
423 cycle += QUEUE_LENGTH - packets;
424
425 for (i = 0; i < packets; ++i)
426 queue_out_packet(s, ++cycle);
427 fw_iso_context_queue_flush(s->context);
428}
429
430static int queue_initial_skip_packets(struct amdtp_out_stream *s)
431{
432 struct fw_iso_packet skip_packet = {
433 .skip = 1,
434 };
435 unsigned int i;
436 int err;
437
438 for (i = 0; i < QUEUE_LENGTH; ++i) {
439 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
440 INTERRUPT_INTERVAL);
441 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
442 if (err < 0)
443 return err;
444 if (++s->packet_index >= QUEUE_LENGTH)
445 s->packet_index = 0;
446 }
447
448 return 0;
449}
450
451
452
453
454
455
456
457
458
459
460
461
462int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
463{
464 static const struct {
465 unsigned int data_block;
466 unsigned int syt_offset;
467 } initial_state[] = {
468 [CIP_SFC_32000] = { 4, 3072 },
469 [CIP_SFC_48000] = { 6, 1024 },
470 [CIP_SFC_96000] = { 12, 1024 },
471 [CIP_SFC_192000] = { 24, 1024 },
472 [CIP_SFC_44100] = { 0, 67 },
473 [CIP_SFC_88200] = { 0, 67 },
474 [CIP_SFC_176400] = { 0, 67 },
475 };
476 int err;
477
478 mutex_lock(&s->mutex);
479
480 if (WARN_ON(!IS_ERR(s->context) ||
481 (!s->pcm_channels && !s->midi_ports))) {
482 err = -EBADFD;
483 goto err_unlock;
484 }
485
486 s->data_block_state = initial_state[s->sfc].data_block;
487 s->syt_offset_state = initial_state[s->sfc].syt_offset;
488 s->last_syt_offset = TICKS_PER_CYCLE;
489
490 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
491 amdtp_out_stream_get_max_payload(s),
492 DMA_TO_DEVICE);
493 if (err < 0)
494 goto err_unlock;
495
496 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
497 FW_ISO_CONTEXT_TRANSMIT,
498 channel, speed, 0,
499 out_packet_callback, s);
500 if (IS_ERR(s->context)) {
501 err = PTR_ERR(s->context);
502 if (err == -EBUSY)
503 dev_err(&s->unit->device,
504 "no free output stream on this controller\n");
505 goto err_buffer;
506 }
507
508 amdtp_out_stream_update(s);
509
510 s->packet_index = 0;
511 s->data_block_counter = 0;
512 err = queue_initial_skip_packets(s);
513 if (err < 0)
514 goto err_context;
515
516 err = fw_iso_context_start(s->context, -1, 0, 0);
517 if (err < 0)
518 goto err_context;
519
520 mutex_unlock(&s->mutex);
521
522 return 0;
523
524err_context:
525 fw_iso_context_destroy(s->context);
526 s->context = ERR_PTR(-1);
527err_buffer:
528 iso_packets_buffer_destroy(&s->buffer, s->unit);
529err_unlock:
530 mutex_unlock(&s->mutex);
531
532 return err;
533}
534EXPORT_SYMBOL(amdtp_out_stream_start);
535
536
537
538
539
540
541
542unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)
543{
544
545 if (s->pointer_flush)
546 fw_iso_context_flush_completions(s->context);
547 else
548 s->pointer_flush = true;
549
550 return ACCESS_ONCE(s->pcm_buffer_pointer);
551}
552EXPORT_SYMBOL(amdtp_out_stream_pcm_pointer);
553
554
555
556
557
558void amdtp_out_stream_update(struct amdtp_out_stream *s)
559{
560 ACCESS_ONCE(s->source_node_id_field) =
561 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
562}
563EXPORT_SYMBOL(amdtp_out_stream_update);
564
565
566
567
568
569
570
571
572void amdtp_out_stream_stop(struct amdtp_out_stream *s)
573{
574 mutex_lock(&s->mutex);
575
576 if (IS_ERR(s->context)) {
577 mutex_unlock(&s->mutex);
578 return;
579 }
580
581 tasklet_kill(&s->period_tasklet);
582 fw_iso_context_stop(s->context);
583 fw_iso_context_destroy(s->context);
584 s->context = ERR_PTR(-1);
585 iso_packets_buffer_destroy(&s->buffer, s->unit);
586
587 mutex_unlock(&s->mutex);
588}
589EXPORT_SYMBOL(amdtp_out_stream_stop);
590
591
592
593
594
595
596
597
598void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
599{
600 struct snd_pcm_substream *pcm;
601
602 pcm = ACCESS_ONCE(s->pcm);
603 if (pcm) {
604 snd_pcm_stream_lock_irq(pcm);
605 if (snd_pcm_running(pcm))
606 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
607 snd_pcm_stream_unlock_irq(pcm);
608 }
609}
610EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);
611