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