ALSA: firewire-lib: taskletize the snd_pcm_period_elapsed() call
[deliverable/linux.git] / sound / firewire / amdtp.c
CommitLineData
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
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/* TODO: make these configurable */
31#define INTERRUPT_INTERVAL 16
32#define QUEUE_LENGTH 48
33
76fb8789
CL
34static void pcm_period_tasklet(unsigned long data);
35
31ef9134
CL
36/**
37 * amdtp_out_stream_init - initialize an AMDTP output stream structure
38 * @s: the AMDTP output stream to initialize
39 * @unit: the target of the stream
40 * @flags: the packet transmission method to use
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);
76fb8789 52 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
ec00f5e4 53 s->packet_index = 0;
31ef9134
CL
54
55 return 0;
56}
57EXPORT_SYMBOL(amdtp_out_stream_init);
58
59/**
60 * amdtp_out_stream_destroy - free stream resources
61 * @s: the AMDTP output stream to destroy
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 * amdtp_out_stream_set_rate - set the sample rate
73 * @s: the AMDTP output stream to configure
74 * @rate: the sample rate
75 *
76 * The sample rate must be set before the stream is started, and must not be
77 * changed while the stream is running.
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 * amdtp_out_stream_get_max_payload - get the stream's packet size
110 * @s: the AMDTP output stream
111 *
112 * This function must not be called before the stream has been configured
113 * with amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
114 * amdtp_out_stream_set_midi().
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 * amdtp_out_stream_set_pcm_format - set the PCM format
144 * @s: the AMDTP output stream to configure
145 * @format: the format of the ALSA PCM device
146 *
147 * The sample format must be set before the stream is started, and must not be
148 * changed while the stream is running.
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 /* fall through */
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
76fb8789
CL
170/**
171 * amdtp_out_stream_pcm_prepare - prepare PCM device for running
172 * @s: the AMDTP output stream
173 *
174 * This function should be called from the PCM device's .prepare callback.
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}
182EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare);
183
31ef9134
CL
184static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
185{
186 unsigned int phase, data_blocks;
187
188 if (!cip_sfc_is_base_44100(s->sfc)) {
189 /* Sample_rate / 8000 is an integer, and precomputed. */
190 data_blocks = s->data_block_state;
191 } else {
192 phase = s->data_block_state;
193
194 /*
195 * This calculates the number of data blocks per packet so that
196 * 1) the overall rate is correct and exactly synchronized to
197 * the bus clock, and
198 * 2) packets with a rounded-up number of blocks occur as early
199 * as possible in the sequence (to prevent underruns of the
200 * device's buffer).
201 */
202 if (s->sfc == CIP_SFC_44100)
203 /* 6 6 5 6 5 6 5 ... */
204 data_blocks = 5 + ((phase & 1) ^
205 (phase == 0 || phase >= 40));
206 else
207 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
208 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
209 if (++phase >= (80 >> (s->sfc >> 1)))
210 phase = 0;
211 s->data_block_state = phase;
212 }
213
214 return data_blocks;
215}
216
217static unsigned int calculate_syt(struct amdtp_out_stream *s,
218 unsigned int cycle)
219{
220 unsigned int syt_offset, phase, index, syt;
221
222 if (s->last_syt_offset < TICKS_PER_CYCLE) {
223 if (!cip_sfc_is_base_44100(s->sfc))
224 syt_offset = s->last_syt_offset + s->syt_offset_state;
225 else {
226 /*
227 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
228 * n * SYT_INTERVAL * 24576000 / sample_rate
229 * Modulo TICKS_PER_CYCLE, the difference between successive
230 * elements is about 1386.23. Rounding the results of this
231 * formula to the SYT precision results in a sequence of
232 * differences that begins with:
233 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
234 * This code generates _exactly_ the same sequence.
235 */
236 phase = s->syt_offset_state;
237 index = phase % 13;
238 syt_offset = s->last_syt_offset;
239 syt_offset += 1386 + ((index && !(index & 3)) ||
240 phase == 146);
241 if (++phase >= 147)
242 phase = 0;
243 s->syt_offset_state = phase;
244 }
245 } else
246 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
247 s->last_syt_offset = syt_offset;
248
be454366
CL
249 if (syt_offset < TICKS_PER_CYCLE) {
250 syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
251 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
252 syt += syt_offset % TICKS_PER_CYCLE;
31ef9134 253
be454366
CL
254 return syt & 0xffff;
255 } else {
256 return 0xffff; /* no info */
257 }
31ef9134
CL
258}
259
260static void amdtp_write_s32(struct amdtp_out_stream *s,
261 struct snd_pcm_substream *pcm,
262 __be32 *buffer, unsigned int frames)
263{
264 struct snd_pcm_runtime *runtime = pcm->runtime;
265 unsigned int channels, remaining_frames, frame_step, i, c;
266 const u32 *src;
267
268 channels = s->pcm_channels;
269 src = (void *)runtime->dma_area +
270 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
271 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
272 frame_step = s->data_block_quadlets - channels;
273
274 for (i = 0; i < frames; ++i) {
275 for (c = 0; c < channels; ++c) {
276 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
277 src++;
278 buffer++;
279 }
280 buffer += frame_step;
281 if (--remaining_frames == 0)
282 src = (void *)runtime->dma_area;
283 }
284}
285
286static void amdtp_write_s16(struct amdtp_out_stream *s,
287 struct snd_pcm_substream *pcm,
288 __be32 *buffer, unsigned int frames)
289{
290 struct snd_pcm_runtime *runtime = pcm->runtime;
291 unsigned int channels, remaining_frames, frame_step, i, c;
292 const u16 *src;
293
294 channels = s->pcm_channels;
295 src = (void *)runtime->dma_area +
296 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
297 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
298 frame_step = s->data_block_quadlets - channels;
299
300 for (i = 0; i < frames; ++i) {
301 for (c = 0; c < channels; ++c) {
302 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
303 src++;
304 buffer++;
305 }
306 buffer += frame_step;
307 if (--remaining_frames == 0)
308 src = (void *)runtime->dma_area;
309 }
310}
311
312static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
313 __be32 *buffer, unsigned int frames)
314{
315 unsigned int i, c;
316
317 for (i = 0; i < frames; ++i) {
318 for (c = 0; c < s->pcm_channels; ++c)
319 buffer[c] = cpu_to_be32(0x40000000);
320 buffer += s->data_block_quadlets;
321 }
322}
323
324static void amdtp_fill_midi(struct amdtp_out_stream *s,
325 __be32 *buffer, unsigned int frames)
326{
327 unsigned int i;
328
329 for (i = 0; i < frames; ++i)
330 buffer[s->pcm_channels + i * s->data_block_quadlets] =
331 cpu_to_be32(0x80000000);
332}
333
334static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
335{
336 __be32 *buffer;
ec00f5e4 337 unsigned int index, data_blocks, syt, ptr;
31ef9134
CL
338 struct snd_pcm_substream *pcm;
339 struct fw_iso_packet packet;
340 int err;
341
ec00f5e4
CL
342 if (s->packet_index < 0)
343 return;
344 index = s->packet_index;
345
31ef9134
CL
346 data_blocks = calculate_data_blocks(s);
347 syt = calculate_syt(s, cycle);
348
ec00f5e4 349 buffer = s->buffer.packets[index].buffer;
31ef9134
CL
350 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
351 (s->data_block_quadlets << 16) |
352 s->data_block_counter);
353 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
354 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
355 buffer += 2;
356
357 pcm = ACCESS_ONCE(s->pcm);
358 if (pcm)
359 s->transfer_samples(s, pcm, buffer, data_blocks);
360 else
361 amdtp_fill_pcm_silence(s, buffer, data_blocks);
362 if (s->midi_ports)
363 amdtp_fill_midi(s, buffer, data_blocks);
364
365 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
366
367 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
ec00f5e4 368 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
31ef9134
CL
369 packet.skip = 0;
370 packet.tag = TAG_CIP;
371 packet.sy = 0;
372 packet.header_length = 0;
373
374 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
ec00f5e4
CL
375 s->buffer.packets[index].offset);
376 if (err < 0) {
31ef9134 377 dev_err(&s->unit->device, "queueing error: %d\n", err);
ec00f5e4
CL
378 s->packet_index = -1;
379 amdtp_out_stream_pcm_abort(s);
380 return;
381 }
31ef9134 382
ec00f5e4
CL
383 if (++index >= QUEUE_LENGTH)
384 index = 0;
385 s->packet_index = index;
31ef9134
CL
386
387 if (pcm) {
388 ptr = s->pcm_buffer_pointer + data_blocks;
389 if (ptr >= pcm->runtime->buffer_size)
390 ptr -= pcm->runtime->buffer_size;
391 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
392
393 s->pcm_period_pointer += data_blocks;
394 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
395 s->pcm_period_pointer -= pcm->runtime->period_size;
76fb8789 396 tasklet_hi_schedule(&s->period_tasklet);
31ef9134
CL
397 }
398 }
399}
400
76fb8789
CL
401static void pcm_period_tasklet(unsigned long data)
402{
403 struct amdtp_out_stream *s = (void *)data;
404 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
405
406 if (pcm)
407 snd_pcm_period_elapsed(pcm);
408}
409
31ef9134
CL
410static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
411 size_t header_length, void *header, void *data)
412{
413 struct amdtp_out_stream *s = data;
414 unsigned int i, packets = header_length / 4;
415
416 /*
417 * Compute the cycle of the last queued packet.
418 * (We need only the four lowest bits for the SYT, so we can ignore
419 * that bits 0-11 must wrap around at 3072.)
420 */
421 cycle += QUEUE_LENGTH - packets;
422
423 for (i = 0; i < packets; ++i)
424 queue_out_packet(s, ++cycle);
13882a82 425 fw_iso_context_queue_flush(s->context);
31ef9134
CL
426}
427
428static int queue_initial_skip_packets(struct amdtp_out_stream *s)
429{
430 struct fw_iso_packet skip_packet = {
431 .skip = 1,
432 };
433 unsigned int i;
434 int err;
435
436 for (i = 0; i < QUEUE_LENGTH; ++i) {
ec00f5e4 437 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
31ef9134
CL
438 INTERRUPT_INTERVAL);
439 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
440 if (err < 0)
441 return err;
ec00f5e4
CL
442 if (++s->packet_index >= QUEUE_LENGTH)
443 s->packet_index = 0;
31ef9134
CL
444 }
445
446 return 0;
447}
448
449/**
450 * amdtp_out_stream_start - start sending packets
451 * @s: the AMDTP output stream to start
452 * @channel: the isochronous channel on the bus
453 * @speed: firewire speed code
454 *
455 * The stream cannot be started until it has been configured with
456 * amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
457 * amdtp_out_stream_set_midi(); and it must be started before any
458 * PCM or MIDI device can be started.
459 */
460int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
461{
462 static const struct {
463 unsigned int data_block;
464 unsigned int syt_offset;
465 } initial_state[] = {
466 [CIP_SFC_32000] = { 4, 3072 },
467 [CIP_SFC_48000] = { 6, 1024 },
468 [CIP_SFC_96000] = { 12, 1024 },
469 [CIP_SFC_192000] = { 24, 1024 },
470 [CIP_SFC_44100] = { 0, 67 },
471 [CIP_SFC_88200] = { 0, 67 },
472 [CIP_SFC_176400] = { 0, 67 },
473 };
474 int err;
475
476 mutex_lock(&s->mutex);
477
478 if (WARN_ON(!IS_ERR(s->context) ||
479 (!s->pcm_channels && !s->midi_ports))) {
480 err = -EBADFD;
481 goto err_unlock;
482 }
483
484 s->data_block_state = initial_state[s->sfc].data_block;
485 s->syt_offset_state = initial_state[s->sfc].syt_offset;
486 s->last_syt_offset = TICKS_PER_CYCLE;
487
488 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
489 amdtp_out_stream_get_max_payload(s),
490 DMA_TO_DEVICE);
491 if (err < 0)
492 goto err_unlock;
493
494 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
495 FW_ISO_CONTEXT_TRANSMIT,
496 channel, speed, 0,
497 out_packet_callback, s);
498 if (IS_ERR(s->context)) {
499 err = PTR_ERR(s->context);
500 if (err == -EBUSY)
501 dev_err(&s->unit->device,
502 "no free output stream on this controller\n");
503 goto err_buffer;
504 }
505
506 amdtp_out_stream_update(s);
507
ec00f5e4 508 s->packet_index = 0;
31ef9134
CL
509 s->data_block_counter = 0;
510 err = queue_initial_skip_packets(s);
511 if (err < 0)
512 goto err_context;
513
514 err = fw_iso_context_start(s->context, -1, 0, 0);
515 if (err < 0)
516 goto err_context;
517
518 mutex_unlock(&s->mutex);
519
520 return 0;
521
522err_context:
523 fw_iso_context_destroy(s->context);
524 s->context = ERR_PTR(-1);
525err_buffer:
526 iso_packets_buffer_destroy(&s->buffer, s->unit);
527err_unlock:
528 mutex_unlock(&s->mutex);
529
530 return err;
531}
532EXPORT_SYMBOL(amdtp_out_stream_start);
533
534/**
535 * amdtp_out_stream_update - update the stream after a bus reset
536 * @s: the AMDTP output stream
537 */
538void amdtp_out_stream_update(struct amdtp_out_stream *s)
539{
540 ACCESS_ONCE(s->source_node_id_field) =
541 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
542}
543EXPORT_SYMBOL(amdtp_out_stream_update);
544
545/**
546 * amdtp_out_stream_stop - stop sending packets
547 * @s: the AMDTP output stream to stop
548 *
549 * All PCM and MIDI devices of the stream must be stopped before the stream
550 * itself can be stopped.
551 */
552void amdtp_out_stream_stop(struct amdtp_out_stream *s)
553{
554 mutex_lock(&s->mutex);
555
556 if (IS_ERR(s->context)) {
557 mutex_unlock(&s->mutex);
558 return;
559 }
560
76fb8789 561 tasklet_kill(&s->period_tasklet);
31ef9134
CL
562 fw_iso_context_stop(s->context);
563 fw_iso_context_destroy(s->context);
564 s->context = ERR_PTR(-1);
565 iso_packets_buffer_destroy(&s->buffer, s->unit);
566
567 mutex_unlock(&s->mutex);
568}
569EXPORT_SYMBOL(amdtp_out_stream_stop);
570
571/**
572 * amdtp_out_stream_pcm_abort - abort the running PCM device
573 * @s: the AMDTP stream about to be stopped
574 *
575 * If the isochronous stream needs to be stopped asynchronously, call this
576 * function first to stop the PCM device.
577 */
578void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
579{
580 struct snd_pcm_substream *pcm;
581
582 pcm = ACCESS_ONCE(s->pcm);
583 if (pcm) {
584 snd_pcm_stream_lock_irq(pcm);
585 if (snd_pcm_running(pcm))
586 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
587 snd_pcm_stream_unlock_irq(pcm);
588 }
589}
590EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);
This page took 0.087376 seconds and 5 git commands to generate.