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