]> asedeno.scripts.mit.edu Git - linux.git/blob - sound/firewire/amdtp-stream.c
Merge tag 'clk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
[linux.git] / sound / firewire / amdtp-stream.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 <sound/pcm_params.h>
16 #include "amdtp-stream.h"
17
18 #define TICKS_PER_CYCLE         3072
19 #define CYCLES_PER_SECOND       8000
20 #define TICKS_PER_SECOND        (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21
22 /* Always support Linux tracing subsystem. */
23 #define CREATE_TRACE_POINTS
24 #include "amdtp-stream-trace.h"
25
26 #define TRANSFER_DELAY_TICKS    0x2e00 /* 479.17 microseconds */
27
28 /* isochronous header parameters */
29 #define ISO_DATA_LENGTH_SHIFT   16
30 #define TAG_NO_CIP_HEADER       0
31 #define TAG_CIP                 1
32
33 /* common isochronous packet header parameters */
34 #define CIP_EOH_SHIFT           31
35 #define CIP_EOH                 (1u << CIP_EOH_SHIFT)
36 #define CIP_EOH_MASK            0x80000000
37 #define CIP_SID_SHIFT           24
38 #define CIP_SID_MASK            0x3f000000
39 #define CIP_DBS_MASK            0x00ff0000
40 #define CIP_DBS_SHIFT           16
41 #define CIP_SPH_MASK            0x00000400
42 #define CIP_SPH_SHIFT           10
43 #define CIP_DBC_MASK            0x000000ff
44 #define CIP_FMT_SHIFT           24
45 #define CIP_FMT_MASK            0x3f000000
46 #define CIP_FDF_MASK            0x00ff0000
47 #define CIP_FDF_SHIFT           16
48 #define CIP_SYT_MASK            0x0000ffff
49 #define CIP_SYT_NO_INFO         0xffff
50
51 /* Audio and Music transfer protocol specific parameters */
52 #define CIP_FMT_AM              0x10
53 #define AMDTP_FDF_NO_DATA       0xff
54
55 /* TODO: make these configurable */
56 #define INTERRUPT_INTERVAL      16
57 #define QUEUE_LENGTH            48
58
59 #define IN_PACKET_HEADER_SIZE   4
60 #define OUT_PACKET_HEADER_SIZE  0
61
62 static void pcm_period_tasklet(unsigned long data);
63
64 /**
65  * amdtp_stream_init - initialize an AMDTP stream structure
66  * @s: the AMDTP stream to initialize
67  * @unit: the target of the stream
68  * @dir: the direction of stream
69  * @flags: the packet transmission method to use
70  * @fmt: the value of fmt field in CIP header
71  * @process_data_blocks: callback handler to process data blocks
72  * @protocol_size: the size to allocate newly for protocol
73  */
74 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
75                       enum amdtp_stream_direction dir, enum cip_flags flags,
76                       unsigned int fmt,
77                       amdtp_stream_process_data_blocks_t process_data_blocks,
78                       unsigned int protocol_size)
79 {
80         if (process_data_blocks == NULL)
81                 return -EINVAL;
82
83         s->protocol = kzalloc(protocol_size, GFP_KERNEL);
84         if (!s->protocol)
85                 return -ENOMEM;
86
87         s->unit = unit;
88         s->direction = dir;
89         s->flags = flags;
90         s->context = ERR_PTR(-1);
91         mutex_init(&s->mutex);
92         tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
93         s->packet_index = 0;
94
95         init_waitqueue_head(&s->callback_wait);
96         s->callbacked = false;
97
98         s->fmt = fmt;
99         s->process_data_blocks = process_data_blocks;
100
101         return 0;
102 }
103 EXPORT_SYMBOL(amdtp_stream_init);
104
105 /**
106  * amdtp_stream_destroy - free stream resources
107  * @s: the AMDTP stream to destroy
108  */
109 void amdtp_stream_destroy(struct amdtp_stream *s)
110 {
111         /* Not initialized. */
112         if (s->protocol == NULL)
113                 return;
114
115         WARN_ON(amdtp_stream_running(s));
116         kfree(s->protocol);
117         mutex_destroy(&s->mutex);
118 }
119 EXPORT_SYMBOL(amdtp_stream_destroy);
120
121 const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
122         [CIP_SFC_32000]  =  8,
123         [CIP_SFC_44100]  =  8,
124         [CIP_SFC_48000]  =  8,
125         [CIP_SFC_88200]  = 16,
126         [CIP_SFC_96000]  = 16,
127         [CIP_SFC_176400] = 32,
128         [CIP_SFC_192000] = 32,
129 };
130 EXPORT_SYMBOL(amdtp_syt_intervals);
131
132 const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
133         [CIP_SFC_32000]  =  32000,
134         [CIP_SFC_44100]  =  44100,
135         [CIP_SFC_48000]  =  48000,
136         [CIP_SFC_88200]  =  88200,
137         [CIP_SFC_96000]  =  96000,
138         [CIP_SFC_176400] = 176400,
139         [CIP_SFC_192000] = 192000,
140 };
141 EXPORT_SYMBOL(amdtp_rate_table);
142
143 static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
144                                     struct snd_pcm_hw_rule *rule)
145 {
146         struct snd_interval *s = hw_param_interval(params, rule->var);
147         const struct snd_interval *r =
148                 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
149         struct snd_interval t = {
150                 .min = s->min, .max = s->max, .integer = 1,
151         };
152         int i;
153
154         for (i = 0; i < CIP_SFC_COUNT; ++i) {
155                 unsigned int rate = amdtp_rate_table[i];
156                 unsigned int step = amdtp_syt_intervals[i];
157
158                 if (!snd_interval_test(r, rate))
159                         continue;
160
161                 t.min = roundup(t.min, step);
162                 t.max = rounddown(t.max, step);
163         }
164
165         if (snd_interval_checkempty(&t))
166                 return -EINVAL;
167
168         return snd_interval_refine(s, &t);
169 }
170
171 static int apply_constraint_to_rate(struct snd_pcm_hw_params *params,
172                                     struct snd_pcm_hw_rule *rule)
173 {
174         struct snd_interval *r =
175                         hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
176         const struct snd_interval *s = hw_param_interval_c(params, rule->deps[0]);
177         struct snd_interval t = {
178                 .min = UINT_MAX, .max = 0, .integer = 1,
179         };
180         int i;
181
182         for (i = 0; i < CIP_SFC_COUNT; ++i) {
183                 unsigned int step = amdtp_syt_intervals[i];
184                 unsigned int rate = amdtp_rate_table[i];
185
186                 if (s->min % step || s->max % step)
187                         continue;
188
189                 t.min = min(t.min, rate);
190                 t.max = max(t.max, rate);
191         }
192
193         return snd_interval_refine(r, &t);
194 }
195
196 /**
197  * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
198  * @s:          the AMDTP stream, which must be initialized.
199  * @runtime:    the PCM substream runtime
200  */
201 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
202                                         struct snd_pcm_runtime *runtime)
203 {
204         struct snd_pcm_hardware *hw = &runtime->hw;
205         int err;
206
207         hw->info = SNDRV_PCM_INFO_BATCH |
208                    SNDRV_PCM_INFO_BLOCK_TRANSFER |
209                    SNDRV_PCM_INFO_INTERLEAVED |
210                    SNDRV_PCM_INFO_JOINT_DUPLEX |
211                    SNDRV_PCM_INFO_MMAP |
212                    SNDRV_PCM_INFO_MMAP_VALID;
213
214         /* SNDRV_PCM_INFO_BATCH */
215         hw->periods_min = 2;
216         hw->periods_max = UINT_MAX;
217
218         /* bytes for a frame */
219         hw->period_bytes_min = 4 * hw->channels_max;
220
221         /* Just to prevent from allocating much pages. */
222         hw->period_bytes_max = hw->period_bytes_min * 2048;
223         hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
224
225         /*
226          * Currently firewire-lib processes 16 packets in one software
227          * interrupt callback. This equals to 2msec but actually the
228          * interval of the interrupts has a jitter.
229          * Additionally, even if adding a constraint to fit period size to
230          * 2msec, actual calculated frames per period doesn't equal to 2msec,
231          * depending on sampling rate.
232          * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
233          * Here let us use 5msec for safe period interrupt.
234          */
235         err = snd_pcm_hw_constraint_minmax(runtime,
236                                            SNDRV_PCM_HW_PARAM_PERIOD_TIME,
237                                            5000, UINT_MAX);
238         if (err < 0)
239                 goto end;
240
241         /* Non-Blocking stream has no more constraints */
242         if (!(s->flags & CIP_BLOCKING))
243                 goto end;
244
245         /*
246          * One AMDTP packet can include some frames. In blocking mode, the
247          * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
248          * depending on its sampling rate. For accurate period interrupt, it's
249          * preferrable to align period/buffer sizes to current SYT_INTERVAL.
250          */
251         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
252                                   apply_constraint_to_size, NULL,
253                                   SNDRV_PCM_HW_PARAM_RATE, -1);
254         if (err < 0)
255                 goto end;
256         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
257                                   apply_constraint_to_rate, NULL,
258                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
259         if (err < 0)
260                 goto end;
261         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
262                                   apply_constraint_to_size, NULL,
263                                   SNDRV_PCM_HW_PARAM_RATE, -1);
264         if (err < 0)
265                 goto end;
266         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
267                                   apply_constraint_to_rate, NULL,
268                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
269         if (err < 0)
270                 goto end;
271 end:
272         return err;
273 }
274 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
275
276 /**
277  * amdtp_stream_set_parameters - set stream parameters
278  * @s: the AMDTP stream to configure
279  * @rate: the sample rate
280  * @data_block_quadlets: the size of a data block in quadlet unit
281  *
282  * The parameters must be set before the stream is started, and must not be
283  * changed while the stream is running.
284  */
285 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
286                                 unsigned int data_block_quadlets)
287 {
288         unsigned int sfc;
289
290         for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
291                 if (amdtp_rate_table[sfc] == rate)
292                         break;
293         }
294         if (sfc == ARRAY_SIZE(amdtp_rate_table))
295                 return -EINVAL;
296
297         s->sfc = sfc;
298         s->data_block_quadlets = data_block_quadlets;
299         s->syt_interval = amdtp_syt_intervals[sfc];
300
301         /* default buffering in the device */
302         s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
303         if (s->flags & CIP_BLOCKING)
304                 /* additional buffering needed to adjust for no-data packets */
305                 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
306
307         return 0;
308 }
309 EXPORT_SYMBOL(amdtp_stream_set_parameters);
310
311 /**
312  * amdtp_stream_get_max_payload - get the stream's packet size
313  * @s: the AMDTP stream
314  *
315  * This function must not be called before the stream has been configured
316  * with amdtp_stream_set_parameters().
317  */
318 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
319 {
320         unsigned int multiplier = 1;
321         unsigned int header_size = 0;
322
323         if (s->flags & CIP_JUMBO_PAYLOAD)
324                 multiplier = 5;
325         if (!(s->flags & CIP_NO_HEADER))
326                 header_size = 8;
327
328         return header_size +
329                 s->syt_interval * s->data_block_quadlets * 4 * multiplier;
330 }
331 EXPORT_SYMBOL(amdtp_stream_get_max_payload);
332
333 /**
334  * amdtp_stream_pcm_prepare - prepare PCM device for running
335  * @s: the AMDTP stream
336  *
337  * This function should be called from the PCM device's .prepare callback.
338  */
339 void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
340 {
341         tasklet_kill(&s->period_tasklet);
342         s->pcm_buffer_pointer = 0;
343         s->pcm_period_pointer = 0;
344 }
345 EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
346
347 static unsigned int calculate_data_blocks(struct amdtp_stream *s,
348                                           unsigned int syt)
349 {
350         unsigned int phase, data_blocks;
351
352         /* Blocking mode. */
353         if (s->flags & CIP_BLOCKING) {
354                 /* This module generate empty packet for 'no data'. */
355                 if (syt == CIP_SYT_NO_INFO)
356                         data_blocks = 0;
357                 else
358                         data_blocks = s->syt_interval;
359         /* Non-blocking mode. */
360         } else {
361                 if (!cip_sfc_is_base_44100(s->sfc)) {
362                         /* Sample_rate / 8000 is an integer, and precomputed. */
363                         data_blocks = s->data_block_state;
364                 } else {
365                         phase = s->data_block_state;
366
367                 /*
368                  * This calculates the number of data blocks per packet so that
369                  * 1) the overall rate is correct and exactly synchronized to
370                  *    the bus clock, and
371                  * 2) packets with a rounded-up number of blocks occur as early
372                  *    as possible in the sequence (to prevent underruns of the
373                  *    device's buffer).
374                  */
375                         if (s->sfc == CIP_SFC_44100)
376                                 /* 6 6 5 6 5 6 5 ... */
377                                 data_blocks = 5 + ((phase & 1) ^
378                                                    (phase == 0 || phase >= 40));
379                         else
380                                 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
381                                 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
382                         if (++phase >= (80 >> (s->sfc >> 1)))
383                                 phase = 0;
384                         s->data_block_state = phase;
385                 }
386         }
387
388         return data_blocks;
389 }
390
391 static unsigned int calculate_syt(struct amdtp_stream *s,
392                                   unsigned int cycle)
393 {
394         unsigned int syt_offset, phase, index, syt;
395
396         if (s->last_syt_offset < TICKS_PER_CYCLE) {
397                 if (!cip_sfc_is_base_44100(s->sfc))
398                         syt_offset = s->last_syt_offset + s->syt_offset_state;
399                 else {
400                 /*
401                  * The time, in ticks, of the n'th SYT_INTERVAL sample is:
402                  *   n * SYT_INTERVAL * 24576000 / sample_rate
403                  * Modulo TICKS_PER_CYCLE, the difference between successive
404                  * elements is about 1386.23.  Rounding the results of this
405                  * formula to the SYT precision results in a sequence of
406                  * differences that begins with:
407                  *   1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
408                  * This code generates _exactly_ the same sequence.
409                  */
410                         phase = s->syt_offset_state;
411                         index = phase % 13;
412                         syt_offset = s->last_syt_offset;
413                         syt_offset += 1386 + ((index && !(index & 3)) ||
414                                               phase == 146);
415                         if (++phase >= 147)
416                                 phase = 0;
417                         s->syt_offset_state = phase;
418                 }
419         } else
420                 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
421         s->last_syt_offset = syt_offset;
422
423         if (syt_offset < TICKS_PER_CYCLE) {
424                 syt_offset += s->transfer_delay;
425                 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
426                 syt += syt_offset % TICKS_PER_CYCLE;
427
428                 return syt & CIP_SYT_MASK;
429         } else {
430                 return CIP_SYT_NO_INFO;
431         }
432 }
433
434 static void update_pcm_pointers(struct amdtp_stream *s,
435                                 struct snd_pcm_substream *pcm,
436                                 unsigned int frames)
437 {
438         unsigned int ptr;
439
440         ptr = s->pcm_buffer_pointer + frames;
441         if (ptr >= pcm->runtime->buffer_size)
442                 ptr -= pcm->runtime->buffer_size;
443         WRITE_ONCE(s->pcm_buffer_pointer, ptr);
444
445         s->pcm_period_pointer += frames;
446         if (s->pcm_period_pointer >= pcm->runtime->period_size) {
447                 s->pcm_period_pointer -= pcm->runtime->period_size;
448                 tasklet_hi_schedule(&s->period_tasklet);
449         }
450 }
451
452 static void pcm_period_tasklet(unsigned long data)
453 {
454         struct amdtp_stream *s = (void *)data;
455         struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
456
457         if (pcm)
458                 snd_pcm_period_elapsed(pcm);
459 }
460
461 static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
462                         unsigned int payload_length)
463 {
464         struct fw_iso_packet p = {0};
465         int err = 0;
466
467         if (IS_ERR(s->context))
468                 goto end;
469
470         p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
471         p.tag = s->tag;
472         p.header_length = header_length;
473         if (payload_length > 0)
474                 p.payload_length = payload_length;
475         else
476                 p.skip = true;
477         err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
478                                    s->buffer.packets[s->packet_index].offset);
479         if (err < 0) {
480                 dev_err(&s->unit->device, "queueing error: %d\n", err);
481                 goto end;
482         }
483
484         if (++s->packet_index >= QUEUE_LENGTH)
485                 s->packet_index = 0;
486 end:
487         return err;
488 }
489
490 static inline int queue_out_packet(struct amdtp_stream *s,
491                                    unsigned int payload_length)
492 {
493         return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
494 }
495
496 static inline int queue_in_packet(struct amdtp_stream *s)
497 {
498         return queue_packet(s, IN_PACKET_HEADER_SIZE, s->max_payload_length);
499 }
500
501 static int handle_out_packet(struct amdtp_stream *s,
502                              unsigned int payload_length, unsigned int cycle,
503                              unsigned int index)
504 {
505         __be32 *buffer;
506         unsigned int syt;
507         unsigned int data_blocks;
508         unsigned int pcm_frames;
509         struct snd_pcm_substream *pcm;
510
511         buffer = s->buffer.packets[s->packet_index].buffer;
512         syt = calculate_syt(s, cycle);
513         data_blocks = calculate_data_blocks(s, syt);
514         pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
515
516         if (s->flags & CIP_DBC_IS_END_EVENT)
517                 s->data_block_counter =
518                                 (s->data_block_counter + data_blocks) & 0xff;
519
520         buffer[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
521                                 (s->data_block_quadlets << CIP_DBS_SHIFT) |
522                                 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
523                                 s->data_block_counter);
524         buffer[1] = cpu_to_be32(CIP_EOH |
525                                 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
526                                 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
527                                 (syt & CIP_SYT_MASK));
528
529         if (!(s->flags & CIP_DBC_IS_END_EVENT))
530                 s->data_block_counter =
531                                 (s->data_block_counter + data_blocks) & 0xff;
532         payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
533
534         trace_out_packet(s, cycle, buffer, payload_length, index);
535
536         if (queue_out_packet(s, payload_length) < 0)
537                 return -EIO;
538
539         pcm = READ_ONCE(s->pcm);
540         if (pcm && pcm_frames > 0)
541                 update_pcm_pointers(s, pcm, pcm_frames);
542
543         /* No need to return the number of handled data blocks. */
544         return 0;
545 }
546
547 static int handle_out_packet_without_header(struct amdtp_stream *s,
548                         unsigned int payload_length, unsigned int cycle,
549                         unsigned int index)
550 {
551         __be32 *buffer;
552         unsigned int syt;
553         unsigned int data_blocks;
554         unsigned int pcm_frames;
555         struct snd_pcm_substream *pcm;
556
557         buffer = s->buffer.packets[s->packet_index].buffer;
558         syt = calculate_syt(s, cycle);
559         data_blocks = calculate_data_blocks(s, syt);
560         pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt);
561         s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
562
563         payload_length = data_blocks * 4 * s->data_block_quadlets;
564
565         trace_out_packet_without_header(s, cycle, payload_length, data_blocks,
566                                         index);
567
568         if (queue_out_packet(s, payload_length) < 0)
569                 return -EIO;
570
571         pcm = READ_ONCE(s->pcm);
572         if (pcm && pcm_frames > 0)
573                 update_pcm_pointers(s, pcm, pcm_frames);
574
575         /* No need to return the number of handled data blocks. */
576         return 0;
577 }
578
579 static int handle_in_packet(struct amdtp_stream *s,
580                             unsigned int payload_length, unsigned int cycle,
581                             unsigned int index)
582 {
583         __be32 *buffer;
584         u32 cip_header[2];
585         unsigned int sph, fmt, fdf, syt;
586         unsigned int data_block_quadlets, data_block_counter, dbc_interval;
587         unsigned int data_blocks;
588         struct snd_pcm_substream *pcm;
589         unsigned int pcm_frames;
590         bool lost;
591
592         buffer = s->buffer.packets[s->packet_index].buffer;
593         cip_header[0] = be32_to_cpu(buffer[0]);
594         cip_header[1] = be32_to_cpu(buffer[1]);
595
596         trace_in_packet(s, cycle, cip_header, payload_length, index);
597
598         /*
599          * This module supports 'Two-quadlet CIP header with SYT field'.
600          * For convenience, also check FMT field is AM824 or not.
601          */
602         if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
603              ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
604             (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
605                 dev_info_ratelimited(&s->unit->device,
606                                 "Invalid CIP header for AMDTP: %08X:%08X\n",
607                                 cip_header[0], cip_header[1]);
608                 data_blocks = 0;
609                 pcm_frames = 0;
610                 goto end;
611         }
612
613         /* Check valid protocol or not. */
614         sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
615         fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
616         if (sph != s->sph || fmt != s->fmt) {
617                 dev_info_ratelimited(&s->unit->device,
618                                      "Detect unexpected protocol: %08x %08x\n",
619                                      cip_header[0], cip_header[1]);
620                 data_blocks = 0;
621                 pcm_frames = 0;
622                 goto end;
623         }
624
625         /* Calculate data blocks */
626         fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
627         if (payload_length < 12 ||
628             (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
629                 data_blocks = 0;
630         } else {
631                 data_block_quadlets =
632                         (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
633                 /* avoid division by zero */
634                 if (data_block_quadlets == 0) {
635                         dev_err(&s->unit->device,
636                                 "Detect invalid value in dbs field: %08X\n",
637                                 cip_header[0]);
638                         return -EPROTO;
639                 }
640                 if (s->flags & CIP_WRONG_DBS)
641                         data_block_quadlets = s->data_block_quadlets;
642
643                 data_blocks = (payload_length / 4 - 2) /
644                                                         data_block_quadlets;
645         }
646
647         /* Check data block counter continuity */
648         data_block_counter = cip_header[0] & CIP_DBC_MASK;
649         if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
650             s->data_block_counter != UINT_MAX)
651                 data_block_counter = s->data_block_counter;
652
653         if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
654              data_block_counter == s->tx_first_dbc) ||
655             s->data_block_counter == UINT_MAX) {
656                 lost = false;
657         } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
658                 lost = data_block_counter != s->data_block_counter;
659         } else {
660                 if (data_blocks > 0 && s->tx_dbc_interval > 0)
661                         dbc_interval = s->tx_dbc_interval;
662                 else
663                         dbc_interval = data_blocks;
664
665                 lost = data_block_counter !=
666                        ((s->data_block_counter + dbc_interval) & 0xff);
667         }
668
669         if (lost) {
670                 dev_err(&s->unit->device,
671                         "Detect discontinuity of CIP: %02X %02X\n",
672                         s->data_block_counter, data_block_counter);
673                 return -EIO;
674         }
675
676         syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
677         pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
678
679         if (s->flags & CIP_DBC_IS_END_EVENT)
680                 s->data_block_counter = data_block_counter;
681         else
682                 s->data_block_counter =
683                                 (data_block_counter + data_blocks) & 0xff;
684 end:
685         if (queue_in_packet(s) < 0)
686                 return -EIO;
687
688         pcm = READ_ONCE(s->pcm);
689         if (pcm && pcm_frames > 0)
690                 update_pcm_pointers(s, pcm, pcm_frames);
691
692         return 0;
693 }
694
695 static int handle_in_packet_without_header(struct amdtp_stream *s,
696                         unsigned int payload_quadlets, unsigned int cycle,
697                         unsigned int index)
698 {
699         __be32 *buffer;
700         unsigned int data_blocks;
701         struct snd_pcm_substream *pcm;
702         unsigned int pcm_frames;
703
704         buffer = s->buffer.packets[s->packet_index].buffer;
705         data_blocks = payload_quadlets / s->data_block_quadlets;
706
707         trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks,
708                                        index);
709
710         pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL);
711         s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
712
713         if (queue_in_packet(s) < 0)
714                 return -EIO;
715
716         pcm = READ_ONCE(s->pcm);
717         if (pcm && pcm_frames > 0)
718                 update_pcm_pointers(s, pcm, pcm_frames);
719
720         return 0;
721 }
722
723 /*
724  * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
725  * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
726  * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
727  */
728 static inline u32 compute_cycle_count(u32 tstamp)
729 {
730         return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
731 }
732
733 static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
734 {
735         cycle += addend;
736         if (cycle >= 8 * CYCLES_PER_SECOND)
737                 cycle -= 8 * CYCLES_PER_SECOND;
738         return cycle;
739 }
740
741 static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
742 {
743         if (cycle < subtrahend)
744                 cycle += 8 * CYCLES_PER_SECOND;
745         return cycle - subtrahend;
746 }
747
748 static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
749                                 size_t header_length, void *header,
750                                 void *private_data)
751 {
752         struct amdtp_stream *s = private_data;
753         unsigned int i, packets = header_length / 4;
754         u32 cycle;
755
756         if (s->packet_index < 0)
757                 return;
758
759         cycle = compute_cycle_count(tstamp);
760
761         /* Align to actual cycle count for the last packet. */
762         cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
763
764         for (i = 0; i < packets; ++i) {
765                 cycle = increment_cycle_count(cycle, 1);
766                 if (s->handle_packet(s, 0, cycle, i) < 0) {
767                         s->packet_index = -1;
768                         if (in_interrupt())
769                                 amdtp_stream_pcm_abort(s);
770                         WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
771                         return;
772                 }
773         }
774
775         fw_iso_context_queue_flush(s->context);
776 }
777
778 static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
779                                size_t header_length, void *header,
780                                void *private_data)
781 {
782         struct amdtp_stream *s = private_data;
783         unsigned int i, packets;
784         unsigned int payload_length, max_payload_length;
785         __be32 *headers = header;
786         u32 cycle;
787
788         if (s->packet_index < 0)
789                 return;
790
791         /* The number of packets in buffer */
792         packets = header_length / IN_PACKET_HEADER_SIZE;
793
794         cycle = compute_cycle_count(tstamp);
795
796         /* Align to actual cycle count for the last packet. */
797         cycle = decrement_cycle_count(cycle, packets);
798
799         /* For buffer-over-run prevention. */
800         max_payload_length = s->max_payload_length;
801
802         for (i = 0; i < packets; i++) {
803                 cycle = increment_cycle_count(cycle, 1);
804
805                 /* The number of bytes in this packet */
806                 payload_length =
807                         (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT);
808                 if (payload_length > max_payload_length) {
809                         dev_err(&s->unit->device,
810                                 "Detect jumbo payload: %04x %04x\n",
811                                 payload_length, max_payload_length);
812                         break;
813                 }
814
815                 if (s->handle_packet(s, payload_length, cycle, i) < 0)
816                         break;
817         }
818
819         /* Queueing error or detecting invalid payload. */
820         if (i < packets) {
821                 s->packet_index = -1;
822                 if (in_interrupt())
823                         amdtp_stream_pcm_abort(s);
824                 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
825                 return;
826         }
827
828         fw_iso_context_queue_flush(s->context);
829 }
830
831 /* this is executed one time */
832 static void amdtp_stream_first_callback(struct fw_iso_context *context,
833                                         u32 tstamp, size_t header_length,
834                                         void *header, void *private_data)
835 {
836         struct amdtp_stream *s = private_data;
837         u32 cycle;
838         unsigned int packets;
839
840         /*
841          * For in-stream, first packet has come.
842          * For out-stream, prepared to transmit first packet
843          */
844         s->callbacked = true;
845         wake_up(&s->callback_wait);
846
847         cycle = compute_cycle_count(tstamp);
848
849         if (s->direction == AMDTP_IN_STREAM) {
850                 packets = header_length / IN_PACKET_HEADER_SIZE;
851                 cycle = decrement_cycle_count(cycle, packets);
852                 context->callback.sc = in_stream_callback;
853                 if (s->flags & CIP_NO_HEADER)
854                         s->handle_packet = handle_in_packet_without_header;
855                 else
856                         s->handle_packet = handle_in_packet;
857         } else {
858                 packets = header_length / 4;
859                 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
860                 context->callback.sc = out_stream_callback;
861                 if (s->flags & CIP_NO_HEADER)
862                         s->handle_packet = handle_out_packet_without_header;
863                 else
864                         s->handle_packet = handle_out_packet;
865         }
866
867         s->start_cycle = cycle;
868
869         context->callback.sc(context, tstamp, header_length, header, s);
870 }
871
872 /**
873  * amdtp_stream_start - start transferring packets
874  * @s: the AMDTP stream to start
875  * @channel: the isochronous channel on the bus
876  * @speed: firewire speed code
877  *
878  * The stream cannot be started until it has been configured with
879  * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
880  * device can be started.
881  */
882 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
883 {
884         static const struct {
885                 unsigned int data_block;
886                 unsigned int syt_offset;
887         } initial_state[] = {
888                 [CIP_SFC_32000]  = {  4, 3072 },
889                 [CIP_SFC_48000]  = {  6, 1024 },
890                 [CIP_SFC_96000]  = { 12, 1024 },
891                 [CIP_SFC_192000] = { 24, 1024 },
892                 [CIP_SFC_44100]  = {  0,   67 },
893                 [CIP_SFC_88200]  = {  0,   67 },
894                 [CIP_SFC_176400] = {  0,   67 },
895         };
896         unsigned int header_size;
897         enum dma_data_direction dir;
898         int type, tag, err;
899
900         mutex_lock(&s->mutex);
901
902         if (WARN_ON(amdtp_stream_running(s) ||
903                     (s->data_block_quadlets < 1))) {
904                 err = -EBADFD;
905                 goto err_unlock;
906         }
907
908         if (s->direction == AMDTP_IN_STREAM)
909                 s->data_block_counter = UINT_MAX;
910         else
911                 s->data_block_counter = 0;
912         s->data_block_state = initial_state[s->sfc].data_block;
913         s->syt_offset_state = initial_state[s->sfc].syt_offset;
914         s->last_syt_offset = TICKS_PER_CYCLE;
915
916         /* initialize packet buffer */
917         if (s->direction == AMDTP_IN_STREAM) {
918                 dir = DMA_FROM_DEVICE;
919                 type = FW_ISO_CONTEXT_RECEIVE;
920                 header_size = IN_PACKET_HEADER_SIZE;
921         } else {
922                 dir = DMA_TO_DEVICE;
923                 type = FW_ISO_CONTEXT_TRANSMIT;
924                 header_size = OUT_PACKET_HEADER_SIZE;
925         }
926         err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
927                                       amdtp_stream_get_max_payload(s), dir);
928         if (err < 0)
929                 goto err_unlock;
930
931         s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
932                                            type, channel, speed, header_size,
933                                            amdtp_stream_first_callback, s);
934         if (IS_ERR(s->context)) {
935                 err = PTR_ERR(s->context);
936                 if (err == -EBUSY)
937                         dev_err(&s->unit->device,
938                                 "no free stream on this controller\n");
939                 goto err_buffer;
940         }
941
942         amdtp_stream_update(s);
943
944         if (s->direction == AMDTP_IN_STREAM)
945                 s->max_payload_length = amdtp_stream_get_max_payload(s);
946
947         if (s->flags & CIP_NO_HEADER)
948                 s->tag = TAG_NO_CIP_HEADER;
949         else
950                 s->tag = TAG_CIP;
951
952         s->packet_index = 0;
953         do {
954                 if (s->direction == AMDTP_IN_STREAM)
955                         err = queue_in_packet(s);
956                 else
957                         err = queue_out_packet(s, 0);
958                 if (err < 0)
959                         goto err_context;
960         } while (s->packet_index > 0);
961
962         /* NOTE: TAG1 matches CIP. This just affects in stream. */
963         tag = FW_ISO_CONTEXT_MATCH_TAG1;
964         if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
965                 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
966
967         s->callbacked = false;
968         err = fw_iso_context_start(s->context, -1, 0, tag);
969         if (err < 0)
970                 goto err_context;
971
972         mutex_unlock(&s->mutex);
973
974         return 0;
975
976 err_context:
977         fw_iso_context_destroy(s->context);
978         s->context = ERR_PTR(-1);
979 err_buffer:
980         iso_packets_buffer_destroy(&s->buffer, s->unit);
981 err_unlock:
982         mutex_unlock(&s->mutex);
983
984         return err;
985 }
986 EXPORT_SYMBOL(amdtp_stream_start);
987
988 /**
989  * amdtp_stream_pcm_pointer - get the PCM buffer position
990  * @s: the AMDTP stream that transports the PCM data
991  *
992  * Returns the current buffer position, in frames.
993  */
994 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
995 {
996         /*
997          * This function is called in software IRQ context of period_tasklet or
998          * process context.
999          *
1000          * When the software IRQ context was scheduled by software IRQ context
1001          * of IR/IT contexts, queued packets were already handled. Therefore,
1002          * no need to flush the queue in buffer anymore.
1003          *
1004          * When the process context reach here, some packets will be already
1005          * queued in the buffer. These packets should be handled immediately
1006          * to keep better granularity of PCM pointer.
1007          *
1008          * Later, the process context will sometimes schedules software IRQ
1009          * context of the period_tasklet. Then, no need to flush the queue by
1010          * the same reason as described for IR/IT contexts.
1011          */
1012         if (!in_interrupt() && amdtp_stream_running(s))
1013                 fw_iso_context_flush_completions(s->context);
1014
1015         return READ_ONCE(s->pcm_buffer_pointer);
1016 }
1017 EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
1018
1019 /**
1020  * amdtp_stream_pcm_ack - acknowledge queued PCM frames
1021  * @s: the AMDTP stream that transfers the PCM frames
1022  *
1023  * Returns zero always.
1024  */
1025 int amdtp_stream_pcm_ack(struct amdtp_stream *s)
1026 {
1027         /*
1028          * Process isochronous packets for recent isochronous cycle to handle
1029          * queued PCM frames.
1030          */
1031         if (amdtp_stream_running(s))
1032                 fw_iso_context_flush_completions(s->context);
1033
1034         return 0;
1035 }
1036 EXPORT_SYMBOL(amdtp_stream_pcm_ack);
1037
1038 /**
1039  * amdtp_stream_update - update the stream after a bus reset
1040  * @s: the AMDTP stream
1041  */
1042 void amdtp_stream_update(struct amdtp_stream *s)
1043 {
1044         /* Precomputing. */
1045         WRITE_ONCE(s->source_node_id_field,
1046                    (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
1047 }
1048 EXPORT_SYMBOL(amdtp_stream_update);
1049
1050 /**
1051  * amdtp_stream_stop - stop sending packets
1052  * @s: the AMDTP stream to stop
1053  *
1054  * All PCM and MIDI devices of the stream must be stopped before the stream
1055  * itself can be stopped.
1056  */
1057 void amdtp_stream_stop(struct amdtp_stream *s)
1058 {
1059         mutex_lock(&s->mutex);
1060
1061         if (!amdtp_stream_running(s)) {
1062                 mutex_unlock(&s->mutex);
1063                 return;
1064         }
1065
1066         tasklet_kill(&s->period_tasklet);
1067         fw_iso_context_stop(s->context);
1068         fw_iso_context_destroy(s->context);
1069         s->context = ERR_PTR(-1);
1070         iso_packets_buffer_destroy(&s->buffer, s->unit);
1071
1072         s->callbacked = false;
1073
1074         mutex_unlock(&s->mutex);
1075 }
1076 EXPORT_SYMBOL(amdtp_stream_stop);
1077
1078 /**
1079  * amdtp_stream_pcm_abort - abort the running PCM device
1080  * @s: the AMDTP stream about to be stopped
1081  *
1082  * If the isochronous stream needs to be stopped asynchronously, call this
1083  * function first to stop the PCM device.
1084  */
1085 void amdtp_stream_pcm_abort(struct amdtp_stream *s)
1086 {
1087         struct snd_pcm_substream *pcm;
1088
1089         pcm = READ_ONCE(s->pcm);
1090         if (pcm)
1091                 snd_pcm_stop_xrun(pcm);
1092 }
1093 EXPORT_SYMBOL(amdtp_stream_pcm_abort);