2 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3 * with Common Isochronous Packet (IEC 61883-1) headers
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Licensed under the terms of the GNU General Public License, version 2.
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"
18 #define TICKS_PER_CYCLE 3072
19 #define CYCLES_PER_SECOND 8000
20 #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22 /* Always support Linux tracing subsystem. */
23 #define CREATE_TRACE_POINTS
24 #include "amdtp-stream-trace.h"
26 #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
28 /* isochronous header parameters */
29 #define ISO_DATA_LENGTH_SHIFT 16
32 /* common isochronous packet header parameters */
33 #define CIP_EOH_SHIFT 31
34 #define CIP_EOH (1u << CIP_EOH_SHIFT)
35 #define CIP_EOH_MASK 0x80000000
36 #define CIP_SID_SHIFT 24
37 #define CIP_SID_MASK 0x3f000000
38 #define CIP_DBS_MASK 0x00ff0000
39 #define CIP_DBS_SHIFT 16
40 #define CIP_DBC_MASK 0x000000ff
41 #define CIP_FMT_SHIFT 24
42 #define CIP_FMT_MASK 0x3f000000
43 #define CIP_FDF_MASK 0x00ff0000
44 #define CIP_FDF_SHIFT 16
45 #define CIP_SYT_MASK 0x0000ffff
46 #define CIP_SYT_NO_INFO 0xffff
48 /* Audio and Music transfer protocol specific parameters */
49 #define CIP_FMT_AM 0x10
50 #define AMDTP_FDF_NO_DATA 0xff
52 /* TODO: make these configurable */
53 #define INTERRUPT_INTERVAL 16
54 #define QUEUE_LENGTH 48
56 #define IN_PACKET_HEADER_SIZE 4
57 #define OUT_PACKET_HEADER_SIZE 0
59 static void pcm_period_tasklet(unsigned long data);
62 * amdtp_stream_init - initialize an AMDTP stream structure
63 * @s: the AMDTP stream to initialize
64 * @unit: the target of the stream
65 * @dir: the direction of stream
66 * @flags: the packet transmission method to use
67 * @fmt: the value of fmt field in CIP header
68 * @process_data_blocks: callback handler to process data blocks
69 * @protocol_size: the size to allocate newly for protocol
71 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
72 enum amdtp_stream_direction dir, enum cip_flags flags,
74 amdtp_stream_process_data_blocks_t process_data_blocks,
75 unsigned int protocol_size)
77 if (process_data_blocks == NULL)
80 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
87 s->context = ERR_PTR(-1);
88 mutex_init(&s->mutex);
89 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
92 init_waitqueue_head(&s->callback_wait);
93 s->callbacked = false;
96 s->process_data_blocks = process_data_blocks;
100 EXPORT_SYMBOL(amdtp_stream_init);
103 * amdtp_stream_destroy - free stream resources
104 * @s: the AMDTP stream to destroy
106 void amdtp_stream_destroy(struct amdtp_stream *s)
108 /* Not initialized. */
109 if (s->protocol == NULL)
112 WARN_ON(amdtp_stream_running(s));
114 mutex_destroy(&s->mutex);
116 EXPORT_SYMBOL(amdtp_stream_destroy);
118 const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
122 [CIP_SFC_88200] = 16,
123 [CIP_SFC_96000] = 16,
124 [CIP_SFC_176400] = 32,
125 [CIP_SFC_192000] = 32,
127 EXPORT_SYMBOL(amdtp_syt_intervals);
129 const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
130 [CIP_SFC_32000] = 32000,
131 [CIP_SFC_44100] = 44100,
132 [CIP_SFC_48000] = 48000,
133 [CIP_SFC_88200] = 88200,
134 [CIP_SFC_96000] = 96000,
135 [CIP_SFC_176400] = 176400,
136 [CIP_SFC_192000] = 192000,
138 EXPORT_SYMBOL(amdtp_rate_table);
141 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
142 * @s: the AMDTP stream, which must be initialized.
143 * @runtime: the PCM substream runtime
145 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
146 struct snd_pcm_runtime *runtime)
151 * Currently firewire-lib processes 16 packets in one software
152 * interrupt callback. This equals to 2msec but actually the
153 * interval of the interrupts has a jitter.
154 * Additionally, even if adding a constraint to fit period size to
155 * 2msec, actual calculated frames per period doesn't equal to 2msec,
156 * depending on sampling rate.
157 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
158 * Here let us use 5msec for safe period interrupt.
160 err = snd_pcm_hw_constraint_minmax(runtime,
161 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
166 /* Non-Blocking stream has no more constraints */
167 if (!(s->flags & CIP_BLOCKING))
171 * One AMDTP packet can include some frames. In blocking mode, the
172 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
173 * depending on its sampling rate. For accurate period interrupt, it's
174 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
176 * TODO: These constraints can be improved with proper rules.
177 * Currently apply LCM of SYT_INTERVALs.
179 err = snd_pcm_hw_constraint_step(runtime, 0,
180 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
183 err = snd_pcm_hw_constraint_step(runtime, 0,
184 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
188 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
191 * amdtp_stream_set_parameters - set stream parameters
192 * @s: the AMDTP stream to configure
193 * @rate: the sample rate
194 * @data_block_quadlets: the size of a data block in quadlet unit
196 * The parameters must be set before the stream is started, and must not be
197 * changed while the stream is running.
199 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
200 unsigned int data_block_quadlets)
204 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
205 if (amdtp_rate_table[sfc] == rate)
208 if (sfc == ARRAY_SIZE(amdtp_rate_table))
212 s->data_block_quadlets = data_block_quadlets;
213 s->syt_interval = amdtp_syt_intervals[sfc];
215 /* default buffering in the device */
216 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
217 if (s->flags & CIP_BLOCKING)
218 /* additional buffering needed to adjust for no-data packets */
219 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
223 EXPORT_SYMBOL(amdtp_stream_set_parameters);
226 * amdtp_stream_get_max_payload - get the stream's packet size
227 * @s: the AMDTP stream
229 * This function must not be called before the stream has been configured
230 * with amdtp_stream_set_parameters().
232 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
234 unsigned int multiplier = 1;
236 if (s->flags & CIP_JUMBO_PAYLOAD)
239 return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
241 EXPORT_SYMBOL(amdtp_stream_get_max_payload);
244 * amdtp_stream_pcm_prepare - prepare PCM device for running
245 * @s: the AMDTP stream
247 * This function should be called from the PCM device's .prepare callback.
249 void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
251 tasklet_kill(&s->period_tasklet);
252 s->pcm_buffer_pointer = 0;
253 s->pcm_period_pointer = 0;
255 EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
257 static unsigned int calculate_data_blocks(struct amdtp_stream *s,
260 unsigned int phase, data_blocks;
263 if (s->flags & CIP_BLOCKING) {
264 /* This module generate empty packet for 'no data'. */
265 if (syt == CIP_SYT_NO_INFO)
268 data_blocks = s->syt_interval;
269 /* Non-blocking mode. */
271 if (!cip_sfc_is_base_44100(s->sfc)) {
272 /* Sample_rate / 8000 is an integer, and precomputed. */
273 data_blocks = s->data_block_state;
275 phase = s->data_block_state;
278 * This calculates the number of data blocks per packet so that
279 * 1) the overall rate is correct and exactly synchronized to
281 * 2) packets with a rounded-up number of blocks occur as early
282 * as possible in the sequence (to prevent underruns of the
285 if (s->sfc == CIP_SFC_44100)
286 /* 6 6 5 6 5 6 5 ... */
287 data_blocks = 5 + ((phase & 1) ^
288 (phase == 0 || phase >= 40));
290 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
291 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
292 if (++phase >= (80 >> (s->sfc >> 1)))
294 s->data_block_state = phase;
301 static unsigned int calculate_syt(struct amdtp_stream *s,
304 unsigned int syt_offset, phase, index, syt;
306 if (s->last_syt_offset < TICKS_PER_CYCLE) {
307 if (!cip_sfc_is_base_44100(s->sfc))
308 syt_offset = s->last_syt_offset + s->syt_offset_state;
311 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
312 * n * SYT_INTERVAL * 24576000 / sample_rate
313 * Modulo TICKS_PER_CYCLE, the difference between successive
314 * elements is about 1386.23. Rounding the results of this
315 * formula to the SYT precision results in a sequence of
316 * differences that begins with:
317 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
318 * This code generates _exactly_ the same sequence.
320 phase = s->syt_offset_state;
322 syt_offset = s->last_syt_offset;
323 syt_offset += 1386 + ((index && !(index & 3)) ||
327 s->syt_offset_state = phase;
330 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
331 s->last_syt_offset = syt_offset;
333 if (syt_offset < TICKS_PER_CYCLE) {
334 syt_offset += s->transfer_delay;
335 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
336 syt += syt_offset % TICKS_PER_CYCLE;
338 return syt & CIP_SYT_MASK;
340 return CIP_SYT_NO_INFO;
344 static void update_pcm_pointers(struct amdtp_stream *s,
345 struct snd_pcm_substream *pcm,
350 ptr = s->pcm_buffer_pointer + frames;
351 if (ptr >= pcm->runtime->buffer_size)
352 ptr -= pcm->runtime->buffer_size;
353 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
355 s->pcm_period_pointer += frames;
356 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
357 s->pcm_period_pointer -= pcm->runtime->period_size;
358 tasklet_hi_schedule(&s->period_tasklet);
362 static void pcm_period_tasklet(unsigned long data)
364 struct amdtp_stream *s = (void *)data;
365 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
368 snd_pcm_period_elapsed(pcm);
371 static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
372 unsigned int payload_length)
374 struct fw_iso_packet p = {0};
377 if (IS_ERR(s->context))
380 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
382 p.header_length = header_length;
383 if (payload_length > 0)
384 p.payload_length = payload_length;
387 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
388 s->buffer.packets[s->packet_index].offset);
390 dev_err(&s->unit->device, "queueing error: %d\n", err);
394 if (++s->packet_index >= QUEUE_LENGTH)
400 static inline int queue_out_packet(struct amdtp_stream *s,
401 unsigned int payload_length)
403 return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
406 static inline int queue_in_packet(struct amdtp_stream *s)
408 return queue_packet(s, IN_PACKET_HEADER_SIZE,
409 amdtp_stream_get_max_payload(s));
412 static int handle_out_packet(struct amdtp_stream *s, unsigned int cycle,
417 unsigned int data_blocks;
418 unsigned int payload_length;
419 unsigned int pcm_frames;
420 struct snd_pcm_substream *pcm;
422 buffer = s->buffer.packets[s->packet_index].buffer;
423 syt = calculate_syt(s, cycle);
424 data_blocks = calculate_data_blocks(s, syt);
425 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
427 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
428 (s->data_block_quadlets << CIP_DBS_SHIFT) |
429 s->data_block_counter);
430 buffer[1] = cpu_to_be32(CIP_EOH |
431 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
432 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
433 (syt & CIP_SYT_MASK));
435 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
436 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
438 trace_out_packet(s, cycle, buffer, payload_length, index);
440 if (queue_out_packet(s, payload_length) < 0)
443 pcm = ACCESS_ONCE(s->pcm);
444 if (pcm && pcm_frames > 0)
445 update_pcm_pointers(s, pcm, pcm_frames);
447 /* No need to return the number of handled data blocks. */
451 static int handle_in_packet(struct amdtp_stream *s,
452 unsigned int payload_quadlets, unsigned int cycle,
457 unsigned int fmt, fdf, syt;
458 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
459 unsigned int data_blocks;
460 struct snd_pcm_substream *pcm;
461 unsigned int pcm_frames;
464 buffer = s->buffer.packets[s->packet_index].buffer;
465 cip_header[0] = be32_to_cpu(buffer[0]);
466 cip_header[1] = be32_to_cpu(buffer[1]);
468 trace_in_packet(s, cycle, cip_header, payload_quadlets, index);
471 * This module supports 'Two-quadlet CIP header with SYT field'.
472 * For convenience, also check FMT field is AM824 or not.
474 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
475 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
476 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
477 dev_info_ratelimited(&s->unit->device,
478 "Invalid CIP header for AMDTP: %08X:%08X\n",
479 cip_header[0], cip_header[1]);
485 /* Check valid protocol or not. */
486 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
488 dev_info_ratelimited(&s->unit->device,
489 "Detect unexpected protocol: %08x %08x\n",
490 cip_header[0], cip_header[1]);
496 /* Calculate data blocks */
497 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
498 if (payload_quadlets < 3 ||
499 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
502 data_block_quadlets =
503 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
504 /* avoid division by zero */
505 if (data_block_quadlets == 0) {
506 dev_err(&s->unit->device,
507 "Detect invalid value in dbs field: %08X\n",
511 if (s->flags & CIP_WRONG_DBS)
512 data_block_quadlets = s->data_block_quadlets;
514 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
517 /* Check data block counter continuity */
518 data_block_counter = cip_header[0] & CIP_DBC_MASK;
519 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
520 s->data_block_counter != UINT_MAX)
521 data_block_counter = s->data_block_counter;
523 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
524 data_block_counter == s->tx_first_dbc) ||
525 s->data_block_counter == UINT_MAX) {
527 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
528 lost = data_block_counter != s->data_block_counter;
530 if (data_blocks > 0 && s->tx_dbc_interval > 0)
531 dbc_interval = s->tx_dbc_interval;
533 dbc_interval = data_blocks;
535 lost = data_block_counter !=
536 ((s->data_block_counter + dbc_interval) & 0xff);
540 dev_err(&s->unit->device,
541 "Detect discontinuity of CIP: %02X %02X\n",
542 s->data_block_counter, data_block_counter);
546 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
547 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
549 if (s->flags & CIP_DBC_IS_END_EVENT)
550 s->data_block_counter = data_block_counter;
552 s->data_block_counter =
553 (data_block_counter + data_blocks) & 0xff;
555 if (queue_in_packet(s) < 0)
558 pcm = ACCESS_ONCE(s->pcm);
559 if (pcm && pcm_frames > 0)
560 update_pcm_pointers(s, pcm, pcm_frames);
566 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
567 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
568 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
570 static inline u32 compute_cycle_count(u32 tstamp)
572 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
575 static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
578 if (cycle >= 8 * CYCLES_PER_SECOND)
579 cycle -= 8 * CYCLES_PER_SECOND;
583 static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
585 if (cycle < subtrahend)
586 cycle += 8 * CYCLES_PER_SECOND;
587 return cycle - subtrahend;
590 static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
591 size_t header_length, void *header,
594 struct amdtp_stream *s = private_data;
595 unsigned int i, packets = header_length / 4;
598 if (s->packet_index < 0)
601 cycle = compute_cycle_count(tstamp);
603 /* Align to actual cycle count for the last packet. */
604 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
606 for (i = 0; i < packets; ++i) {
607 cycle = increment_cycle_count(cycle, 1);
608 if (handle_out_packet(s, cycle, i) < 0) {
609 s->packet_index = -1;
611 amdtp_stream_pcm_abort(s);
612 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
617 fw_iso_context_queue_flush(s->context);
620 static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
621 size_t header_length, void *header,
624 struct amdtp_stream *s = private_data;
625 unsigned int i, packets;
626 unsigned int payload_quadlets, max_payload_quadlets;
627 __be32 *headers = header;
630 if (s->packet_index < 0)
633 /* The number of packets in buffer */
634 packets = header_length / IN_PACKET_HEADER_SIZE;
636 cycle = compute_cycle_count(tstamp);
638 /* Align to actual cycle count for the last packet. */
639 cycle = decrement_cycle_count(cycle, packets);
641 /* For buffer-over-run prevention. */
642 max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4;
644 for (i = 0; i < packets; i++) {
645 cycle = increment_cycle_count(cycle, 1);
647 /* The number of quadlets in this packet */
649 (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT) / 4;
650 if (payload_quadlets > max_payload_quadlets) {
651 dev_err(&s->unit->device,
652 "Detect jumbo payload: %02x %02x\n",
653 payload_quadlets, max_payload_quadlets);
657 if (handle_in_packet(s, payload_quadlets, cycle, i) < 0)
661 /* Queueing error or detecting invalid payload. */
663 s->packet_index = -1;
665 amdtp_stream_pcm_abort(s);
666 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
670 fw_iso_context_queue_flush(s->context);
673 /* this is executed one time */
674 static void amdtp_stream_first_callback(struct fw_iso_context *context,
675 u32 tstamp, size_t header_length,
676 void *header, void *private_data)
678 struct amdtp_stream *s = private_data;
681 * For in-stream, first packet has come.
682 * For out-stream, prepared to transmit first packet
684 s->callbacked = true;
685 wake_up(&s->callback_wait);
687 if (s->direction == AMDTP_IN_STREAM)
688 context->callback.sc = in_stream_callback;
690 context->callback.sc = out_stream_callback;
692 context->callback.sc(context, tstamp, header_length, header, s);
696 * amdtp_stream_start - start transferring packets
697 * @s: the AMDTP stream to start
698 * @channel: the isochronous channel on the bus
699 * @speed: firewire speed code
701 * The stream cannot be started until it has been configured with
702 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
703 * device can be started.
705 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
707 static const struct {
708 unsigned int data_block;
709 unsigned int syt_offset;
710 } initial_state[] = {
711 [CIP_SFC_32000] = { 4, 3072 },
712 [CIP_SFC_48000] = { 6, 1024 },
713 [CIP_SFC_96000] = { 12, 1024 },
714 [CIP_SFC_192000] = { 24, 1024 },
715 [CIP_SFC_44100] = { 0, 67 },
716 [CIP_SFC_88200] = { 0, 67 },
717 [CIP_SFC_176400] = { 0, 67 },
719 unsigned int header_size;
720 enum dma_data_direction dir;
723 mutex_lock(&s->mutex);
725 if (WARN_ON(amdtp_stream_running(s) ||
726 (s->data_block_quadlets < 1))) {
731 if (s->direction == AMDTP_IN_STREAM)
732 s->data_block_counter = UINT_MAX;
734 s->data_block_counter = 0;
735 s->data_block_state = initial_state[s->sfc].data_block;
736 s->syt_offset_state = initial_state[s->sfc].syt_offset;
737 s->last_syt_offset = TICKS_PER_CYCLE;
739 /* initialize packet buffer */
740 if (s->direction == AMDTP_IN_STREAM) {
741 dir = DMA_FROM_DEVICE;
742 type = FW_ISO_CONTEXT_RECEIVE;
743 header_size = IN_PACKET_HEADER_SIZE;
746 type = FW_ISO_CONTEXT_TRANSMIT;
747 header_size = OUT_PACKET_HEADER_SIZE;
749 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
750 amdtp_stream_get_max_payload(s), dir);
754 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
755 type, channel, speed, header_size,
756 amdtp_stream_first_callback, s);
757 if (IS_ERR(s->context)) {
758 err = PTR_ERR(s->context);
760 dev_err(&s->unit->device,
761 "no free stream on this controller\n");
765 amdtp_stream_update(s);
769 if (s->direction == AMDTP_IN_STREAM)
770 err = queue_in_packet(s);
772 err = queue_out_packet(s, 0);
775 } while (s->packet_index > 0);
777 /* NOTE: TAG1 matches CIP. This just affects in stream. */
778 tag = FW_ISO_CONTEXT_MATCH_TAG1;
779 if (s->flags & CIP_EMPTY_WITH_TAG0)
780 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
782 s->callbacked = false;
783 err = fw_iso_context_start(s->context, -1, 0, tag);
787 mutex_unlock(&s->mutex);
792 fw_iso_context_destroy(s->context);
793 s->context = ERR_PTR(-1);
795 iso_packets_buffer_destroy(&s->buffer, s->unit);
797 mutex_unlock(&s->mutex);
801 EXPORT_SYMBOL(amdtp_stream_start);
804 * amdtp_stream_pcm_pointer - get the PCM buffer position
805 * @s: the AMDTP stream that transports the PCM data
807 * Returns the current buffer position, in frames.
809 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
812 * This function is called in software IRQ context of period_tasklet or
815 * When the software IRQ context was scheduled by software IRQ context
816 * of IR/IT contexts, queued packets were already handled. Therefore,
817 * no need to flush the queue in buffer anymore.
819 * When the process context reach here, some packets will be already
820 * queued in the buffer. These packets should be handled immediately
821 * to keep better granularity of PCM pointer.
823 * Later, the process context will sometimes schedules software IRQ
824 * context of the period_tasklet. Then, no need to flush the queue by
825 * the same reason as described for IR/IT contexts.
827 if (!in_interrupt() && amdtp_stream_running(s))
828 fw_iso_context_flush_completions(s->context);
830 return ACCESS_ONCE(s->pcm_buffer_pointer);
832 EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
835 * amdtp_stream_update - update the stream after a bus reset
836 * @s: the AMDTP stream
838 void amdtp_stream_update(struct amdtp_stream *s)
841 ACCESS_ONCE(s->source_node_id_field) =
842 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
845 EXPORT_SYMBOL(amdtp_stream_update);
848 * amdtp_stream_stop - stop sending packets
849 * @s: the AMDTP stream to stop
851 * All PCM and MIDI devices of the stream must be stopped before the stream
852 * itself can be stopped.
854 void amdtp_stream_stop(struct amdtp_stream *s)
856 mutex_lock(&s->mutex);
858 if (!amdtp_stream_running(s)) {
859 mutex_unlock(&s->mutex);
863 tasklet_kill(&s->period_tasklet);
864 fw_iso_context_stop(s->context);
865 fw_iso_context_destroy(s->context);
866 s->context = ERR_PTR(-1);
867 iso_packets_buffer_destroy(&s->buffer, s->unit);
869 s->callbacked = false;
871 mutex_unlock(&s->mutex);
873 EXPORT_SYMBOL(amdtp_stream_stop);
876 * amdtp_stream_pcm_abort - abort the running PCM device
877 * @s: the AMDTP stream about to be stopped
879 * If the isochronous stream needs to be stopped asynchronously, call this
880 * function first to stop the PCM device.
882 void amdtp_stream_pcm_abort(struct amdtp_stream *s)
884 struct snd_pcm_substream *pcm;
886 pcm = ACCESS_ONCE(s->pcm);
888 snd_pcm_stop_xrun(pcm);
890 EXPORT_SYMBOL(amdtp_stream_pcm_abort);