blob: f8586f75441db690134f1913cf4413c97c446e6f [file] [log] [blame]
Thomas Gleixnerda607e12019-05-29 16:57:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Clemens Ladisch31ef9132011-03-15 07:53:21 +01002/*
3 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
4 * with Common Isochronous Packet (IEC 61883-1) headers
5 *
6 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
Clemens Ladisch31ef9132011-03-15 07:53:21 +01007 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/firewire.h>
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +090012#include <linux/firewire-constants.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +010013#include <linux/module.h>
14#include <linux/slab.h>
15#include <sound/pcm.h>
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +090016#include <sound/pcm_params.h>
Takashi Sakamotod67c46b2015-09-19 11:21:54 +090017#include "amdtp-stream.h"
Clemens Ladisch31ef9132011-03-15 07:53:21 +010018
19#define TICKS_PER_CYCLE 3072
20#define CYCLES_PER_SECOND 8000
21#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22
Takashi Sakamoto10aa8e42020-05-08 13:36:27 +090023#define OHCI_MAX_SECOND 8
24
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +090025/* Always support Linux tracing subsystem. */
26#define CREATE_TRACE_POINTS
27#include "amdtp-stream-trace.h"
28
Takashi Sakamotoca5b5052015-02-21 11:50:17 +090029#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010030
Takashi Sakamotob445db42014-04-25 22:44:43 +090031/* isochronous header parameters */
32#define ISO_DATA_LENGTH_SHIFT 16
Takashi Sakamoto3b196c392017-03-31 22:06:07 +090033#define TAG_NO_CIP_HEADER 0
Clemens Ladisch31ef9132011-03-15 07:53:21 +010034#define TAG_CIP 1
35
Takashi Sakamotob445db42014-04-25 22:44:43 +090036/* common isochronous packet header parameters */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090037#define CIP_EOH_SHIFT 31
38#define CIP_EOH (1u << CIP_EOH_SHIFT)
Takashi Sakamotob445db42014-04-25 22:44:43 +090039#define CIP_EOH_MASK 0x80000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090040#define CIP_SID_SHIFT 24
41#define CIP_SID_MASK 0x3f000000
42#define CIP_DBS_MASK 0x00ff0000
43#define CIP_DBS_SHIFT 16
Takashi Sakamoto98638742017-03-22 21:30:16 +090044#define CIP_SPH_MASK 0x00000400
45#define CIP_SPH_SHIFT 10
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090046#define CIP_DBC_MASK 0x000000ff
47#define CIP_FMT_SHIFT 24
Takashi Sakamotob445db42014-04-25 22:44:43 +090048#define CIP_FMT_MASK 0x3f000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090049#define CIP_FDF_MASK 0x00ff0000
50#define CIP_FDF_SHIFT 16
Takashi Sakamotob445db42014-04-25 22:44:43 +090051#define CIP_SYT_MASK 0x0000ffff
52#define CIP_SYT_NO_INFO 0xffff
Takashi Sakamotob445db42014-04-25 22:44:43 +090053
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +090054/* Audio and Music transfer protocol specific parameters */
Takashi Sakamoto414ba022015-09-19 11:21:53 +090055#define CIP_FMT_AM 0x10
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090056#define AMDTP_FDF_NO_DATA 0xff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010057
Takashi Sakamotof11453c2019-05-22 23:17:07 +090058// For iso header, tstamp and 2 CIP header.
59#define IR_CTX_HEADER_SIZE_CIP 16
60// For iso header and tstamp.
61#define IR_CTX_HEADER_SIZE_NO_CIP 8
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +090062#define HEADER_TSTAMP_MASK 0x0000ffff
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090063
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +090064#define IT_PKT_HEADER_SIZE_CIP 8 // For 2 CIP header.
65#define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing.
66
Clemens Ladisch76fb8782012-05-13 22:03:09 +020067static void pcm_period_tasklet(unsigned long data);
68
Clemens Ladisch31ef9132011-03-15 07:53:21 +010069/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090070 * amdtp_stream_init - initialize an AMDTP stream structure
71 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010072 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090073 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010074 * @flags: the packet transmission method to use
Takashi Sakamoto59558152015-09-19 11:21:55 +090075 * @fmt: the value of fmt field in CIP header
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +090076 * @process_ctx_payloads: callback handler to process payloads of isoc context
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090077 * @protocol_size: the size to allocate newly for protocol
Clemens Ladisch31ef9132011-03-15 07:53:21 +010078 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090079int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto59558152015-09-19 11:21:55 +090080 enum amdtp_stream_direction dir, enum cip_flags flags,
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090081 unsigned int fmt,
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +090082 amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090083 unsigned int protocol_size)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010084{
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +090085 if (process_ctx_payloads == NULL)
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090086 return -EINVAL;
87
88 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
89 if (!s->protocol)
90 return -ENOMEM;
91
Takashi Sakamotoc6f224d2015-02-21 23:54:58 +090092 s->unit = unit;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090093 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010094 s->flags = flags;
95 s->context = ERR_PTR(-1);
96 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020097 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010098 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010099
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900100 init_waitqueue_head(&s->callback_wait);
101 s->callbacked = false;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900102
Takashi Sakamoto59558152015-09-19 11:21:55 +0900103 s->fmt = fmt;
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900104 s->process_ctx_payloads = process_ctx_payloads;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900105
Takashi Sakamoto3baf3052019-07-22 12:36:56 +0900106 if (dir == AMDTP_OUT_STREAM)
107 s->ctx_data.rx.syt_override = -1;
108
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100109 return 0;
110}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900111EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100112
113/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900114 * amdtp_stream_destroy - free stream resources
115 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100116 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900117void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100118{
Takashi Sakamoto44c376b2016-03-31 08:47:02 +0900119 /* Not initialized. */
120 if (s->protocol == NULL)
121 return;
122
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900123 WARN_ON(amdtp_stream_running(s));
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900124 kfree(s->protocol);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100125 mutex_destroy(&s->mutex);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100126}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900127EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100128
Clemens Ladischc5280e92011-10-16 21:39:00 +0200129const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200130 [CIP_SFC_32000] = 8,
131 [CIP_SFC_44100] = 8,
132 [CIP_SFC_48000] = 8,
133 [CIP_SFC_88200] = 16,
134 [CIP_SFC_96000] = 16,
135 [CIP_SFC_176400] = 32,
136 [CIP_SFC_192000] = 32,
137};
138EXPORT_SYMBOL(amdtp_syt_intervals);
139
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900140const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900141 [CIP_SFC_32000] = 32000,
142 [CIP_SFC_44100] = 44100,
143 [CIP_SFC_48000] = 48000,
144 [CIP_SFC_88200] = 88200,
145 [CIP_SFC_96000] = 96000,
146 [CIP_SFC_176400] = 176400,
147 [CIP_SFC_192000] = 192000,
148};
149EXPORT_SYMBOL(amdtp_rate_table);
150
Takashi Sakamoto59502292018-10-01 04:11:49 +0900151static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
152 struct snd_pcm_hw_rule *rule)
153{
154 struct snd_interval *s = hw_param_interval(params, rule->var);
155 const struct snd_interval *r =
156 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900157 struct snd_interval t = {0};
158 unsigned int step = 0;
Takashi Sakamoto59502292018-10-01 04:11:49 +0900159 int i;
160
161 for (i = 0; i < CIP_SFC_COUNT; ++i) {
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900162 if (snd_interval_test(r, amdtp_rate_table[i]))
163 step = max(step, amdtp_syt_intervals[i]);
Takashi Sakamoto59502292018-10-01 04:11:49 +0900164 }
165
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900166 t.min = roundup(s->min, step);
167 t.max = rounddown(s->max, step);
168 t.integer = 1;
Takashi Sakamoto59502292018-10-01 04:11:49 +0900169
170 return snd_interval_refine(s, &t);
171}
172
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100173/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900174 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
175 * @s: the AMDTP stream, which must be initialized.
176 * @runtime: the PCM substream runtime
177 */
178int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
179 struct snd_pcm_runtime *runtime)
180{
Takashi Sakamoto55799c52017-06-08 09:11:03 +0900181 struct snd_pcm_hardware *hw = &runtime->hw;
Takashi Sakamoto99921ec2019-10-18 00:54:23 +0900182 unsigned int ctx_header_size;
183 unsigned int maximum_usec_per_period;
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900184 int err;
185
Takashi Sakamoto55799c52017-06-08 09:11:03 +0900186 hw->info = SNDRV_PCM_INFO_BATCH |
187 SNDRV_PCM_INFO_BLOCK_TRANSFER |
188 SNDRV_PCM_INFO_INTERLEAVED |
189 SNDRV_PCM_INFO_JOINT_DUPLEX |
190 SNDRV_PCM_INFO_MMAP |
191 SNDRV_PCM_INFO_MMAP_VALID;
192
193 /* SNDRV_PCM_INFO_BATCH */
194 hw->periods_min = 2;
195 hw->periods_max = UINT_MAX;
196
197 /* bytes for a frame */
198 hw->period_bytes_min = 4 * hw->channels_max;
199
200 /* Just to prevent from allocating much pages. */
201 hw->period_bytes_max = hw->period_bytes_min * 2048;
202 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
203
Takashi Sakamoto99921ec2019-10-18 00:54:23 +0900204 // Linux driver for 1394 OHCI controller voluntarily flushes isoc
205 // context when total size of accumulated context header reaches
206 // PAGE_SIZE. This kicks tasklet for the isoc context and brings
207 // callback in the middle of scheduled interrupts.
208 // Although AMDTP streams in the same domain use the same events per
209 // IRQ, use the largest size of context header between IT/IR contexts.
210 // Here, use the value of context header in IR context is for both
211 // contexts.
212 if (!(s->flags & CIP_NO_HEADER))
213 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
214 else
215 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
216 maximum_usec_per_period = USEC_PER_SEC * PAGE_SIZE /
217 CYCLES_PER_SECOND / ctx_header_size;
218
Takashi Sakamotof706df42019-10-18 00:54:24 +0900219 // In IEC 61883-6, one isoc packet can transfer events up to the value
220 // of syt interval. This comes from the interval of isoc cycle. As 1394
221 // OHCI controller can generate hardware IRQ per isoc packet, the
222 // interval is 125 usec.
223 // However, there are two ways of transmission in IEC 61883-6; blocking
224 // and non-blocking modes. In blocking mode, the sequence of isoc packet
225 // includes 'empty' or 'NODATA' packets which include no event. In
226 // non-blocking mode, the number of events per packet is variable up to
227 // the syt interval.
228 // Due to the above protocol design, the minimum PCM frames per
229 // interrupt should be double of the value of syt interval, thus it is
230 // 250 usec.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900231 err = snd_pcm_hw_constraint_minmax(runtime,
232 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
Takashi Sakamotof706df42019-10-18 00:54:24 +0900233 250, maximum_usec_per_period);
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900234 if (err < 0)
235 goto end;
236
237 /* Non-Blocking stream has no more constraints */
238 if (!(s->flags & CIP_BLOCKING))
239 goto end;
240
241 /*
242 * One AMDTP packet can include some frames. In blocking mode, the
243 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
244 * depending on its sampling rate. For accurate period interrupt, it's
Yannick Guerrinice991982015-03-09 22:13:03 +0100245 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900246 */
Takashi Sakamoto59502292018-10-01 04:11:49 +0900247 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
248 apply_constraint_to_size, NULL,
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900249 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Takashi Sakamoto59502292018-10-01 04:11:49 +0900250 SNDRV_PCM_HW_PARAM_RATE, -1);
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900251 if (err < 0)
252 goto end;
Takashi Sakamoto59502292018-10-01 04:11:49 +0900253 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
254 apply_constraint_to_size, NULL,
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900255 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
Takashi Sakamoto59502292018-10-01 04:11:49 +0900256 SNDRV_PCM_HW_PARAM_RATE, -1);
257 if (err < 0)
258 goto end;
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900259end:
260 return err;
261}
262EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
263
264/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900265 * amdtp_stream_set_parameters - set stream parameters
266 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100267 * @rate: the sample rate
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900268 * @data_block_quadlets: the size of a data block in quadlet unit
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100269 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200270 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100271 * changed while the stream is running.
272 */
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900273int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
274 unsigned int data_block_quadlets)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100275{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900276 unsigned int sfc;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100277
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900278 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900279 if (amdtp_rate_table[sfc] == rate)
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900280 break;
281 }
282 if (sfc == ARRAY_SIZE(amdtp_rate_table))
283 return -EINVAL;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200284
Clemens Ladische84d15f2011-09-04 22:12:48 +0200285 s->sfc = sfc;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900286 s->data_block_quadlets = data_block_quadlets;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200287 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200288
Takashi Sakamotod3d10a42019-05-21 23:57:34 +0900289 // default buffering in the device.
290 if (s->direction == AMDTP_OUT_STREAM) {
291 s->ctx_data.rx.transfer_delay =
292 TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
293
294 if (s->flags & CIP_BLOCKING) {
295 // additional buffering needed to adjust for no-data
296 // packets.
297 s->ctx_data.rx.transfer_delay +=
298 TICKS_PER_SECOND * s->syt_interval / rate;
299 }
300 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900301
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900302 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100303}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900304EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100305
306/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900307 * amdtp_stream_get_max_payload - get the stream's packet size
308 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100309 *
310 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900311 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100312 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900313unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100314{
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900315 unsigned int multiplier = 1;
Takashi Sakamoto07ea2382019-05-22 23:17:03 +0900316 unsigned int cip_header_size = 0;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900317
318 if (s->flags & CIP_JUMBO_PAYLOAD)
319 multiplier = 5;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900320 if (!(s->flags & CIP_NO_HEADER))
Takashi Sakamoto07ea2382019-05-22 23:17:03 +0900321 cip_header_size = sizeof(__be32) * 2;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900322
Takashi Sakamoto07ea2382019-05-22 23:17:03 +0900323 return cip_header_size +
324 s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100325}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900326EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100327
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200328/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900329 * amdtp_stream_pcm_prepare - prepare PCM device for running
330 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200331 *
332 * This function should be called from the PCM device's .prepare callback.
333 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900334void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200335{
336 tasklet_kill(&s->period_tasklet);
337 s->pcm_buffer_pointer = 0;
338 s->pcm_period_pointer = 0;
339}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900340EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200341
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900342static unsigned int calculate_data_blocks(unsigned int *data_block_state,
343 bool is_blocking, bool is_no_info,
344 unsigned int syt_interval, enum cip_sfc sfc)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100345{
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900346 unsigned int data_blocks;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100347
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900348 /* Blocking mode. */
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900349 if (is_blocking) {
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900350 /* This module generate empty packet for 'no data'. */
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900351 if (is_no_info)
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900352 data_blocks = 0;
353 else
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900354 data_blocks = syt_interval;
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900355 /* Non-blocking mode. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100356 } else {
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900357 if (!cip_sfc_is_base_44100(sfc)) {
Takashi Sakamotod3d10a42019-05-21 23:57:34 +0900358 // Sample_rate / 8000 is an integer, and precomputed.
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900359 data_blocks = *data_block_state;
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900360 } else {
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900361 unsigned int phase = *data_block_state;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100362
363 /*
364 * This calculates the number of data blocks per packet so that
365 * 1) the overall rate is correct and exactly synchronized to
366 * the bus clock, and
367 * 2) packets with a rounded-up number of blocks occur as early
368 * as possible in the sequence (to prevent underruns of the
369 * device's buffer).
370 */
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900371 if (sfc == CIP_SFC_44100)
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900372 /* 6 6 5 6 5 6 5 ... */
373 data_blocks = 5 + ((phase & 1) ^
374 (phase == 0 || phase >= 40));
375 else
376 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900377 data_blocks = 11 * (sfc >> 1) + (phase == 0);
378 if (++phase >= (80 >> (sfc >> 1)))
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900379 phase = 0;
Takashi Sakamoto274fc352020-05-08 13:36:32 +0900380 *data_block_state = phase;
Takashi Sakamoto875be0912015-05-22 23:00:51 +0900381 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100382 }
383
384 return data_blocks;
385}
386
Takashi Sakamoto816d8482020-05-08 13:36:31 +0900387static unsigned int calculate_syt_offset(unsigned int *last_syt_offset,
388 unsigned int *syt_offset_state, enum cip_sfc sfc)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100389{
Takashi Sakamoto816d8482020-05-08 13:36:31 +0900390 unsigned int syt_offset;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100391
Takashi Sakamoto816d8482020-05-08 13:36:31 +0900392 if (*last_syt_offset < TICKS_PER_CYCLE) {
393 if (!cip_sfc_is_base_44100(sfc))
394 syt_offset = *last_syt_offset + *syt_offset_state;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100395 else {
396 /*
397 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
398 * n * SYT_INTERVAL * 24576000 / sample_rate
399 * Modulo TICKS_PER_CYCLE, the difference between successive
400 * elements is about 1386.23. Rounding the results of this
401 * formula to the SYT precision results in a sequence of
402 * differences that begins with:
403 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
404 * This code generates _exactly_ the same sequence.
405 */
Takashi Sakamoto816d8482020-05-08 13:36:31 +0900406 unsigned int phase = *syt_offset_state;
407 unsigned int index = phase % 13;
408
409 syt_offset = *last_syt_offset;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100410 syt_offset += 1386 + ((index && !(index & 3)) ||
411 phase == 146);
412 if (++phase >= 147)
413 phase = 0;
Takashi Sakamoto816d8482020-05-08 13:36:31 +0900414 *syt_offset_state = phase;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100415 }
416 } else
Takashi Sakamoto816d8482020-05-08 13:36:31 +0900417 syt_offset = *last_syt_offset - TICKS_PER_CYCLE;
418 *last_syt_offset = syt_offset;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100419
Takashi Sakamoto83cfb5c2020-05-08 13:36:30 +0900420 if (syt_offset >= TICKS_PER_CYCLE)
421 syt_offset = CIP_SYT_NO_INFO;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100422
Takashi Sakamoto83cfb5c2020-05-08 13:36:30 +0900423 return syt_offset;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100424}
425
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900426static void update_pcm_pointers(struct amdtp_stream *s,
427 struct snd_pcm_substream *pcm,
428 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900429{
430 unsigned int ptr;
431
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900432 ptr = s->pcm_buffer_pointer + frames;
433 if (ptr >= pcm->runtime->buffer_size)
434 ptr -= pcm->runtime->buffer_size;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700435 WRITE_ONCE(s->pcm_buffer_pointer, ptr);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900436
437 s->pcm_period_pointer += frames;
438 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
439 s->pcm_period_pointer -= pcm->runtime->period_size;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900440 tasklet_hi_schedule(&s->period_tasklet);
441 }
442}
443
444static void pcm_period_tasklet(unsigned long data)
445{
446 struct amdtp_stream *s = (void *)data;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700447 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900448
449 if (pcm)
450 snd_pcm_period_elapsed(pcm);
451}
452
Takashi Sakamotoe2298532019-10-18 00:54:22 +0900453static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params,
454 bool sched_irq)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900455{
Takashi Sakamoto6007bf52019-05-24 00:14:39 +0900456 int err;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900457
Takashi Sakamotoe2298532019-10-18 00:54:22 +0900458 params->interrupt = sched_irq;
Takashi Sakamoto6007bf52019-05-24 00:14:39 +0900459 params->tag = s->tag;
460 params->sy = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900461
Takashi Sakamoto6007bf52019-05-24 00:14:39 +0900462 err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer,
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900463 s->buffer.packets[s->packet_index].offset);
464 if (err < 0) {
465 dev_err(&s->unit->device, "queueing error: %d\n", err);
466 goto end;
467 }
468
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900469 if (++s->packet_index >= s->queue_size)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900470 s->packet_index = 0;
471end:
472 return err;
473}
474
475static inline int queue_out_packet(struct amdtp_stream *s,
Takashi Sakamotoe2298532019-10-18 00:54:22 +0900476 struct fw_iso_packet *params, bool sched_irq)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900477{
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +0900478 params->skip =
479 !!(params->header_length == 0 && params->payload_length == 0);
Takashi Sakamotoe2298532019-10-18 00:54:22 +0900480 return queue_packet(s, params, sched_irq);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900481}
482
Takashi Sakamoto6007bf52019-05-24 00:14:39 +0900483static inline int queue_in_packet(struct amdtp_stream *s,
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900484 struct fw_iso_packet *params)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900485{
Takashi Sakamoto6007bf52019-05-24 00:14:39 +0900486 // Queue one packet for IR context.
487 params->header_length = s->ctx_data.tx.ctx_header_size;
488 params->payload_length = s->ctx_data.tx.max_ctx_payload_length;
489 params->skip = false;
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900490 return queue_packet(s, params, false);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900491}
492
Takashi Sakamoto252219c2019-05-24 00:14:37 +0900493static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2],
Takashi Sakamoto860d7982019-07-22 12:36:54 +0900494 unsigned int data_block_counter, unsigned int syt)
Takashi Sakamoto252219c2019-05-24 00:14:37 +0900495{
496 cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
497 (s->data_block_quadlets << CIP_DBS_SHIFT) |
498 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
Takashi Sakamoto860d7982019-07-22 12:36:54 +0900499 data_block_counter);
Takashi Sakamoto252219c2019-05-24 00:14:37 +0900500 cip_header[1] = cpu_to_be32(CIP_EOH |
501 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
502 ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
503 (syt & CIP_SYT_MASK));
504}
505
Takashi Sakamoto6bc1a262019-05-24 18:03:41 +0900506static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle,
507 struct fw_iso_packet *params,
Takashi Sakamoto860d7982019-07-22 12:36:54 +0900508 unsigned int data_blocks,
509 unsigned int data_block_counter,
510 unsigned int syt, unsigned int index)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100511{
Takashi Sakamoto0ebf3ce2019-07-07 21:07:53 +0900512 unsigned int payload_length;
Takashi Sakamoto16be4582019-05-24 00:14:38 +0900513 __be32 *cip_header;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100514
Takashi Sakamoto0ebf3ce2019-07-07 21:07:53 +0900515 payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets;
516 params->payload_length = payload_length;
517
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +0900518 if (!(s->flags & CIP_NO_HEADER)) {
Takashi Sakamoto6bc1a262019-05-24 18:03:41 +0900519 cip_header = (__be32 *)params->header;
Takashi Sakamoto860d7982019-07-22 12:36:54 +0900520 generate_cip_header(s, cip_header, data_block_counter, syt);
Takashi Sakamoto6bc1a262019-05-24 18:03:41 +0900521 params->header_length = 2 * sizeof(__be32);
Takashi Sakamoto0ebf3ce2019-07-07 21:07:53 +0900522 payload_length += params->header_length;
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +0900523 } else {
524 cip_header = NULL;
525 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100526
Takashi Sakamoto213fa982019-07-07 21:07:56 +0900527 trace_amdtp_packet(s, cycle, cip_header, payload_length, data_blocks,
Takashi Sakamoto860d7982019-07-22 12:36:54 +0900528 data_block_counter, index);
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900529}
530
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900531static int check_cip_header(struct amdtp_stream *s, const __be32 *buf,
532 unsigned int payload_length,
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900533 unsigned int *data_blocks,
534 unsigned int *data_block_counter, unsigned int *syt)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900535{
536 u32 cip_header[2];
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900537 unsigned int sph;
538 unsigned int fmt;
539 unsigned int fdf;
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900540 unsigned int dbc;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900541 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900542
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900543 cip_header[0] = be32_to_cpu(buf[0]);
544 cip_header[1] = be32_to_cpu(buf[1]);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900545
546 /*
547 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900548 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900549 */
Takashi Sakamoto2128f782017-03-22 21:30:27 +0900550 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
551 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
552 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900553 dev_info_ratelimited(&s->unit->device,
554 "Invalid CIP header for AMDTP: %08X:%08X\n",
555 cip_header[0], cip_header[1]);
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900556 return -EAGAIN;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900557 }
558
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900559 /* Check valid protocol or not. */
Takashi Sakamoto98638742017-03-22 21:30:16 +0900560 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900561 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
Takashi Sakamoto98638742017-03-22 21:30:16 +0900562 if (sph != s->sph || fmt != s->fmt) {
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900563 dev_info_ratelimited(&s->unit->device,
564 "Detect unexpected protocol: %08x %08x\n",
565 cip_header[0], cip_header[1]);
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900566 return -EAGAIN;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900567 }
568
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900569 /* Calculate data blocks */
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900570 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900571 if (payload_length < sizeof(__be32) * 2 ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900572 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900573 *data_blocks = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900574 } else {
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900575 unsigned int data_block_quadlets =
576 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900577 /* avoid division by zero */
578 if (data_block_quadlets == 0) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900579 dev_err(&s->unit->device,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900580 "Detect invalid value in dbs field: %08X\n",
581 cip_header[0]);
Takashi Sakamotoa9007052015-05-22 23:21:14 +0900582 return -EPROTO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900583 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900584 if (s->flags & CIP_WRONG_DBS)
585 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900586
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900587 *data_blocks = (payload_length / sizeof(__be32) - 2) /
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900588 data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900589 }
590
591 /* Check data block counter continuity */
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900592 dbc = cip_header[0] & CIP_DBC_MASK;
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900593 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900594 *data_block_counter != UINT_MAX)
595 dbc = *data_block_counter;
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900596
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900597 if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) ||
598 *data_block_counter == UINT_MAX) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900599 lost = false;
600 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900601 lost = dbc != *data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900602 } else {
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900603 unsigned int dbc_interval;
604
605 if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0)
Takashi Sakamotod3d10a42019-05-21 23:57:34 +0900606 dbc_interval = s->ctx_data.tx.dbc_interval;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900607 else
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900608 dbc_interval = *data_blocks;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900609
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900610 lost = dbc != ((*data_block_counter + dbc_interval) & 0xff);
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900611 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900612
613 if (lost) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900614 dev_err(&s->unit->device,
615 "Detect discontinuity of CIP: %02X %02X\n",
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900616 *data_block_counter, dbc);
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900617 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900618 }
619
Takashi Sakamoto753e7172019-07-22 12:37:00 +0900620 *data_block_counter = dbc;
621
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900622 *syt = cip_header[1] & CIP_SYT_MASK;
623
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900624 return 0;
625}
626
Takashi Sakamoto98e3e432019-05-24 18:03:42 +0900627static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle,
628 const __be32 *ctx_header,
629 unsigned int *payload_length,
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900630 unsigned int *data_blocks,
631 unsigned int *data_block_counter,
632 unsigned int *syt, unsigned int index)
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900633{
Takashi Sakamotof11453c2019-05-22 23:17:07 +0900634 const __be32 *cip_header;
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900635 int err;
636
Takashi Sakamoto98e3e432019-05-24 18:03:42 +0900637 *payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT;
638 if (*payload_length > s->ctx_data.tx.ctx_header_size +
Takashi Sakamotof11453c2019-05-22 23:17:07 +0900639 s->ctx_data.tx.max_ctx_payload_length) {
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900640 dev_err(&s->unit->device,
641 "Detect jumbo payload: %04x %04x\n",
Takashi Sakamoto98e3e432019-05-24 18:03:42 +0900642 *payload_length, s->ctx_data.tx.max_ctx_payload_length);
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900643 return -EIO;
644 }
645
Takashi Sakamoto947b4372019-05-22 23:17:08 +0900646 if (!(s->flags & CIP_NO_HEADER)) {
Takashi Sakamoto98e3e432019-05-24 18:03:42 +0900647 cip_header = ctx_header + 2;
648 err = check_cip_header(s, cip_header, *payload_length,
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900649 data_blocks, data_block_counter, syt);
Takashi Sakamotob8b0e242019-07-07 21:07:57 +0900650 if (err < 0)
651 return err;
Takashi Sakamoto947b4372019-05-22 23:17:08 +0900652 } else {
653 cip_header = NULL;
Takashi Sakamoto76864862019-06-28 14:53:30 +0900654 err = 0;
Takashi Sakamoto98e3e432019-05-24 18:03:42 +0900655 *data_blocks = *payload_length / sizeof(__be32) /
656 s->data_block_quadlets;
657 *syt = 0;
Takashi Sakamoto7fbf9092019-07-07 21:07:54 +0900658
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900659 if (*data_block_counter == UINT_MAX)
660 *data_block_counter = 0;
Takashi Sakamotoe3354252019-05-22 23:17:06 +0900661 }
662
Takashi Sakamoto98e3e432019-05-24 18:03:42 +0900663 trace_amdtp_packet(s, cycle, cip_header, *payload_length, *data_blocks,
Takashi Sakamotoa35463d2019-07-22 12:36:55 +0900664 *data_block_counter, index);
Takashi Sakamoto64d0bf42019-05-19 19:01:06 +0900665
Takashi Sakamoto76864862019-06-28 14:53:30 +0900666 return err;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900667}
668
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +0900669// In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
670// the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
671// it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
672static inline u32 compute_cycle_count(__be32 ctx_header_tstamp)
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900673{
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +0900674 u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK;
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900675 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
676}
677
678static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
679{
680 cycle += addend;
Takashi Sakamoto10aa8e42020-05-08 13:36:27 +0900681 if (cycle >= OHCI_MAX_SECOND * CYCLES_PER_SECOND)
682 cycle -= OHCI_MAX_SECOND * CYCLES_PER_SECOND;
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900683 return cycle;
684}
685
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +0900686// Align to actual cycle count for the packet which is going to be scheduled.
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900687// This module queued the same number of isochronous cycle as the size of queue
688// to kip isochronous cycle, therefore it's OK to just increment the cycle by
689// the size of queue for scheduled cycle.
690static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp,
691 unsigned int queue_size)
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +0900692{
693 u32 cycle = compute_cycle_count(ctx_header_tstamp);
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900694 return increment_cycle_count(cycle, queue_size);
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +0900695}
696
Takashi Sakamoto753e7172019-07-22 12:37:00 +0900697static int generate_device_pkt_descs(struct amdtp_stream *s,
698 struct pkt_desc *descs,
699 const __be32 *ctx_header,
700 unsigned int packets)
701{
702 unsigned int dbc = s->data_block_counter;
703 int i;
704 int err;
705
706 for (i = 0; i < packets; ++i) {
707 struct pkt_desc *desc = descs + i;
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900708 unsigned int index = (s->packet_index + i) % s->queue_size;
Takashi Sakamoto753e7172019-07-22 12:37:00 +0900709 unsigned int cycle;
710 unsigned int payload_length;
711 unsigned int data_blocks;
712 unsigned int syt;
713
714 cycle = compute_cycle_count(ctx_header[1]);
715
716 err = parse_ir_ctx_header(s, cycle, ctx_header, &payload_length,
717 &data_blocks, &dbc, &syt, i);
718 if (err < 0)
719 return err;
720
721 desc->cycle = cycle;
722 desc->syt = syt;
723 desc->data_blocks = data_blocks;
724 desc->data_block_counter = dbc;
725 desc->ctx_payload = s->buffer.packets[index].buffer;
726
727 if (!(s->flags & CIP_DBC_IS_END_EVENT))
728 dbc = (dbc + desc->data_blocks) & 0xff;
729
730 ctx_header +=
731 s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
732 }
733
734 s->data_block_counter = dbc;
735
736 return 0;
737}
738
Takashi Sakamoto83cfb5c2020-05-08 13:36:30 +0900739static unsigned int compute_syt(unsigned int syt_offset, unsigned int cycle,
740 unsigned int transfer_delay)
741{
742 unsigned int syt;
743
744 syt_offset += transfer_delay;
745 syt = ((cycle + syt_offset / TICKS_PER_CYCLE) << 12) |
746 (syt_offset % TICKS_PER_CYCLE);
747 return syt & CIP_SYT_MASK;
748}
749
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900750static void generate_pkt_descs(struct amdtp_stream *s, struct pkt_desc *descs,
751 const __be32 *ctx_header, unsigned int packets,
752 const struct seq_desc *seq_descs,
753 unsigned int seq_size)
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900754{
755 unsigned int dbc = s->data_block_counter;
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900756 unsigned int seq_index = s->ctx_data.rx.seq_index;
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900757 int i;
758
759 for (i = 0; i < packets; ++i) {
760 struct pkt_desc *desc = descs + i;
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900761 unsigned int index = (s->packet_index + i) % s->queue_size;
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900762 const struct seq_desc *seq = seq_descs + seq_index;
763 unsigned int syt;
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900764
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900765 desc->cycle = compute_it_cycle(*ctx_header, s->queue_size);
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900766
767 syt = seq->syt_offset;
768 if (syt != CIP_SYT_NO_INFO) {
769 syt = compute_syt(syt, desc->cycle,
770 s->ctx_data.rx.transfer_delay);
Takashi Sakamoto83cfb5c2020-05-08 13:36:30 +0900771 }
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900772 desc->syt = syt;
773 desc->data_blocks = seq->data_blocks;
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900774
775 if (s->flags & CIP_DBC_IS_END_EVENT)
776 dbc = (dbc + desc->data_blocks) & 0xff;
777
778 desc->data_block_counter = dbc;
779
780 if (!(s->flags & CIP_DBC_IS_END_EVENT))
781 dbc = (dbc + desc->data_blocks) & 0xff;
782
783 desc->ctx_payload = s->buffer.packets[index].buffer;
784
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900785 seq_index = (seq_index + 1) % seq_size;
786
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900787 ++ctx_header;
788 }
789
790 s->data_block_counter = dbc;
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900791 s->ctx_data.rx.seq_index = seq_index;
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900792}
793
Takashi Sakamotofce9b012019-05-21 23:57:35 +0900794static inline void cancel_stream(struct amdtp_stream *s)
795{
796 s->packet_index = -1;
797 if (in_interrupt())
798 amdtp_stream_pcm_abort(s);
799 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
800}
801
Takashi Sakamoto0f5cfcb2019-07-22 12:37:02 +0900802static void process_ctx_payloads(struct amdtp_stream *s,
803 const struct pkt_desc *descs,
804 unsigned int packets)
805{
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900806 struct snd_pcm_substream *pcm;
807 unsigned int pcm_frames;
Takashi Sakamoto0f5cfcb2019-07-22 12:37:02 +0900808
Takashi Sakamoto9a738ad2019-07-22 12:37:09 +0900809 pcm = READ_ONCE(s->pcm);
810 pcm_frames = s->process_ctx_payloads(s, descs, packets, pcm);
811 if (pcm)
812 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamoto0f5cfcb2019-07-22 12:37:02 +0900813}
814
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900815static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900816 size_t header_length, void *header,
817 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100818{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900819 struct amdtp_stream *s = private_data;
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900820 const struct amdtp_domain *d = s->domain;
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +0900821 const __be32 *ctx_header = header;
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900822 unsigned int events_per_period = s->ctx_data.rx.events_per_period;
823 unsigned int event_count = s->ctx_data.rx.event_count;
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900824 unsigned int packets;
Takashi Sakamoto0dcb4ef2019-07-07 21:07:59 +0900825 int i;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100826
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900827 if (s->packet_index < 0)
828 return;
829
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900830 // Calculate the number of packets in buffer and check XRUN.
831 packets = header_length / sizeof(*ctx_header);
832
Takashi Sakamoto69efd5c2020-05-08 13:36:35 +0900833 generate_pkt_descs(s, s->pkt_descs, ctx_header, packets, d->seq_descs,
834 d->seq_size);
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900835
Takashi Sakamoto0f5cfcb2019-07-22 12:37:02 +0900836 process_ctx_payloads(s, s->pkt_descs, packets);
Takashi Sakamoto5e2ece02019-07-22 12:37:01 +0900837
838 for (i = 0; i < packets; ++i) {
839 const struct pkt_desc *desc = s->pkt_descs + i;
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900840 unsigned int syt;
Takashi Sakamoto6bc1a262019-05-24 18:03:41 +0900841 struct {
842 struct fw_iso_packet params;
843 __be32 header[IT_PKT_HEADER_SIZE_CIP / sizeof(__be32)];
844 } template = { {0}, {0} };
Takashi Sakamotoe2298532019-10-18 00:54:22 +0900845 bool sched_irq = false;
Takashi Sakamoto860d7982019-07-22 12:36:54 +0900846
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900847 if (s->ctx_data.rx.syt_override < 0)
848 syt = desc->syt;
849 else
Takashi Sakamoto3baf3052019-07-22 12:36:56 +0900850 syt = s->ctx_data.rx.syt_override;
851
Takashi Sakamotof4f6ae72019-07-22 12:36:59 +0900852 build_it_pkt_header(s, desc->cycle, &template.params,
853 desc->data_blocks, desc->data_block_counter,
854 syt, i);
Takashi Sakamoto6bc1a262019-05-24 18:03:41 +0900855
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +0900856 if (s == s->domain->irq_target) {
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900857 event_count += desc->data_blocks;
858 if (event_count >= events_per_period) {
859 event_count -= events_per_period;
860 sched_irq = true;
861 }
Takashi Sakamotoe2298532019-10-18 00:54:22 +0900862 }
863
864 if (queue_out_packet(s, &template.params, sched_irq) < 0) {
Takashi Sakamotofce9b012019-05-21 23:57:35 +0900865 cancel_stream(s);
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900866 return;
867 }
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900868 }
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900869
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900870 s->ctx_data.rx.event_count = event_count;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100871}
872
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900873static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900874 size_t header_length, void *header,
875 void *private_data)
876{
877 struct amdtp_stream *s = private_data;
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900878 __be32 *ctx_header = header;
Takashi Sakamotoe2298532019-10-18 00:54:22 +0900879 unsigned int packets;
Takashi Sakamoto753e7172019-07-22 12:37:00 +0900880 int i;
881 int err;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900882
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900883 if (s->packet_index < 0)
884 return;
885
Takashi Sakamotoa0e02332019-10-18 00:54:13 +0900886 // Calculate the number of packets in buffer and check XRUN.
Takashi Sakamotod3d10a42019-05-21 23:57:34 +0900887 packets = header_length / s->ctx_data.tx.ctx_header_size;
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900888
Takashi Sakamoto753e7172019-07-22 12:37:00 +0900889 err = generate_device_pkt_descs(s, s->pkt_descs, ctx_header, packets);
890 if (err < 0) {
891 if (err != -EAGAIN) {
892 cancel_stream(s);
893 return;
894 }
Takashi Sakamoto5e2ece02019-07-22 12:37:01 +0900895 } else {
Takashi Sakamoto0f5cfcb2019-07-22 12:37:02 +0900896 process_ctx_payloads(s, s->pkt_descs, packets);
Takashi Sakamoto5e2ece02019-07-22 12:37:01 +0900897 }
898
899 for (i = 0; i < packets; ++i) {
900 struct fw_iso_packet params = {0};
Takashi Sakamoto98e3e432019-05-24 18:03:42 +0900901
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900902 if (queue_in_packet(s, &params) < 0) {
Takashi Sakamoto753e7172019-07-22 12:37:00 +0900903 cancel_stream(s);
904 return;
905 }
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900906 }
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900907}
908
Takashi Sakamoto1a4be182020-05-08 13:36:34 +0900909static void pool_ideal_seq_descs(struct amdtp_domain *d, unsigned int packets)
910{
911 struct amdtp_stream *irq_target = d->irq_target;
912 unsigned int seq_tail = d->seq_tail;
913 unsigned int seq_size = d->seq_size;
914 unsigned int min_avail;
915 struct amdtp_stream *s;
916
917 min_avail = d->seq_size;
918 list_for_each_entry(s, &d->streams, list) {
919 unsigned int seq_index;
920 unsigned int avail;
921
922 if (s->direction == AMDTP_IN_STREAM)
923 continue;
924
925 seq_index = s->ctx_data.rx.seq_index;
926 avail = d->seq_tail;
927 if (seq_index > avail)
928 avail += d->seq_size;
929 avail -= seq_index;
930
931 if (avail < min_avail)
932 min_avail = avail;
933 }
934
935 while (min_avail < packets) {
936 struct seq_desc *desc = d->seq_descs + seq_tail;
937
938 desc->syt_offset = calculate_syt_offset(&d->last_syt_offset,
939 &d->syt_offset_state, irq_target->sfc);
940 desc->data_blocks = calculate_data_blocks(&d->data_block_state,
941 !!(irq_target->flags & CIP_BLOCKING),
942 desc->syt_offset == CIP_SYT_NO_INFO,
943 irq_target->syt_interval, irq_target->sfc);
944
945 ++seq_tail;
946 seq_tail %= seq_size;
947
948 ++min_avail;
949 }
950
951 d->seq_tail = seq_tail;
952}
953
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +0900954static void irq_target_callback(struct fw_iso_context *context, u32 tstamp,
955 size_t header_length, void *header,
956 void *private_data)
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900957{
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +0900958 struct amdtp_stream *irq_target = private_data;
959 struct amdtp_domain *d = irq_target->domain;
Takashi Sakamoto1a4be182020-05-08 13:36:34 +0900960 unsigned int packets = header_length / sizeof(__be32);
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900961 struct amdtp_stream *s;
962
Takashi Sakamoto1a4be182020-05-08 13:36:34 +0900963 // Record enough entries with extra 3 cycles at least.
964 pool_ideal_seq_descs(d, packets + 3);
965
Takashi Sakamoto60dd4922019-10-18 15:19:10 +0900966 out_stream_callback(context, tstamp, header_length, header, irq_target);
967 if (amdtp_streaming_error(irq_target))
968 goto error;
969
970 list_for_each_entry(s, &d->streams, list) {
971 if (s != irq_target && amdtp_stream_running(s)) {
972 fw_iso_context_flush_completions(s->context);
973 if (amdtp_streaming_error(s))
974 goto error;
975 }
976 }
977
978 return;
979error:
980 if (amdtp_stream_running(irq_target))
981 cancel_stream(irq_target);
982
983 list_for_each_entry(s, &d->streams, list) {
984 if (amdtp_stream_running(s))
985 cancel_stream(s);
986 }
987}
988
989// this is executed one time.
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900990static void amdtp_stream_first_callback(struct fw_iso_context *context,
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900991 u32 tstamp, size_t header_length,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900992 void *header, void *private_data)
993{
994 struct amdtp_stream *s = private_data;
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +0900995 const __be32 *ctx_header = header;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900996 u32 cycle;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900997
998 /*
999 * For in-stream, first packet has come.
1000 * For out-stream, prepared to transmit first packet
1001 */
1002 s->callbacked = true;
1003 wake_up(&s->callback_wait);
1004
Takashi Sakamotoa04513f2017-03-22 21:30:15 +09001005 if (s->direction == AMDTP_IN_STREAM) {
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +09001006 cycle = compute_cycle_count(ctx_header[1]);
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +09001007
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001008 context->callback.sc = in_stream_callback;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +09001009 } else {
Takashi Sakamotoa0e02332019-10-18 00:54:13 +09001010 cycle = compute_it_cycle(*ctx_header, s->queue_size);
Takashi Sakamoto26cd1e52019-05-21 23:57:37 +09001011
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +09001012 if (s == s->domain->irq_target)
1013 context->callback.sc = irq_target_callback;
1014 else
1015 context->callback.sc = out_stream_callback;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +09001016 }
1017
1018 s->start_cycle = cycle;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001019
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +09001020 context->callback.sc(context, tstamp, header_length, header, s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001021}
1022
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001023/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001024 * amdtp_stream_start - start transferring packets
1025 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001026 * @channel: the isochronous channel on the bus
1027 * @speed: firewire speed code
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001028 * @start_cycle: the isochronous cycle to start the context. Start immediately
1029 * if negative value is given.
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001030 * @queue_size: The number of packets in the queue.
1031 * @idle_irq_interval: the interval to queue packet during initial state.
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001032 *
1033 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001034 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
1035 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001036 */
Takashi Sakamotoa0e02332019-10-18 00:54:13 +09001037static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001038 int start_cycle, unsigned int queue_size,
1039 unsigned int idle_irq_interval)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001040{
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +09001041 bool is_irq_target = (s == s->domain->irq_target);
Takashi Sakamotod3d10a42019-05-21 23:57:34 +09001042 unsigned int ctx_header_size;
Takashi Sakamotof11453c2019-05-22 23:17:07 +09001043 unsigned int max_ctx_payload_size;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001044 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +09001045 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001046
1047 mutex_lock(&s->mutex);
1048
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001049 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +09001050 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001051 err = -EBADFD;
1052 goto err_unlock;
1053 }
1054
Takashi Sakamotod3d10a42019-05-21 23:57:34 +09001055 if (s->direction == AMDTP_IN_STREAM) {
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001056 // NOTE: IT context should be used for constant IRQ.
1057 if (is_irq_target) {
1058 err = -EINVAL;
1059 goto err_unlock;
1060 }
1061
Takashi Sakamotob6bc8122014-04-25 22:45:16 +09001062 s->data_block_counter = UINT_MAX;
Takashi Sakamotod3d10a42019-05-21 23:57:34 +09001063 } else {
Takashi Sakamotob6bc8122014-04-25 22:45:16 +09001064 s->data_block_counter = 0;
Takashi Sakamotod3d10a42019-05-21 23:57:34 +09001065 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001066
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001067 /* initialize packet buffer */
1068 if (s->direction == AMDTP_IN_STREAM) {
1069 dir = DMA_FROM_DEVICE;
1070 type = FW_ISO_CONTEXT_RECEIVE;
Takashi Sakamotof11453c2019-05-22 23:17:07 +09001071 if (!(s->flags & CIP_NO_HEADER))
1072 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
1073 else
1074 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +09001075
1076 max_ctx_payload_size = amdtp_stream_get_max_payload(s) -
1077 ctx_header_size;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001078 } else {
1079 dir = DMA_TO_DEVICE;
1080 type = FW_ISO_CONTEXT_TRANSMIT;
Takashi Sakamotodf9160b2019-05-21 23:57:36 +09001081 ctx_header_size = 0; // No effect for IT context.
Takashi Sakamotof11453c2019-05-22 23:17:07 +09001082
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +09001083 max_ctx_payload_size = amdtp_stream_get_max_payload(s);
1084 if (!(s->flags & CIP_NO_HEADER))
1085 max_ctx_payload_size -= IT_PKT_HEADER_SIZE_CIP;
1086 }
Takashi Sakamotof11453c2019-05-22 23:17:07 +09001087
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001088 err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size,
Takashi Sakamotof11453c2019-05-22 23:17:07 +09001089 max_ctx_payload_size, dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001090 if (err < 0)
1091 goto err_unlock;
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001092 s->queue_size = queue_size;
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001093
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001094 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamotod3d10a42019-05-21 23:57:34 +09001095 type, channel, speed, ctx_header_size,
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +09001096 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001097 if (IS_ERR(s->context)) {
1098 err = PTR_ERR(s->context);
1099 if (err == -EBUSY)
1100 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001101 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001102 goto err_buffer;
1103 }
1104
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001105 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001106
Takashi Sakamotod3d10a42019-05-21 23:57:34 +09001107 if (s->direction == AMDTP_IN_STREAM) {
Takashi Sakamotof11453c2019-05-22 23:17:07 +09001108 s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
Takashi Sakamotod3d10a42019-05-21 23:57:34 +09001109 s->ctx_data.tx.ctx_header_size = ctx_header_size;
1110 }
Takashi Sakamoto52759c092018-04-29 15:01:46 +09001111
Takashi Sakamoto3b196c392017-03-31 22:06:07 +09001112 if (s->flags & CIP_NO_HEADER)
1113 s->tag = TAG_NO_CIP_HEADER;
1114 else
1115 s->tag = TAG_CIP;
1116
Takashi Sakamotoa0e02332019-10-18 00:54:13 +09001117 s->pkt_descs = kcalloc(s->queue_size, sizeof(*s->pkt_descs),
Takashi Sakamoto04130cf2019-07-22 12:36:58 +09001118 GFP_KERNEL);
1119 if (!s->pkt_descs) {
1120 err = -ENOMEM;
1121 goto err_context;
1122 }
1123
Clemens Ladischec00f5e2011-03-15 07:57:24 +01001124 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +09001125 do {
Takashi Sakamoto6007bf52019-05-24 00:14:39 +09001126 struct fw_iso_packet params;
Takashi Sakamotoe2298532019-10-18 00:54:22 +09001127
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +09001128 if (s->direction == AMDTP_IN_STREAM) {
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001129 err = queue_in_packet(s, &params);
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +09001130 } else {
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001131 bool sched_irq = false;
1132
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +09001133 params.header_length = 0;
1134 params.payload_length = 0;
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001135
1136 if (is_irq_target) {
1137 sched_irq = !((s->packet_index + 1) %
1138 idle_irq_interval);
1139 }
1140
Takashi Sakamotoe2298532019-10-18 00:54:22 +09001141 err = queue_out_packet(s, &params, sched_irq);
Takashi Sakamotob18f0cf2019-05-24 00:14:40 +09001142 }
Takashi Sakamoto4b7da112014-04-25 22:44:45 +09001143 if (err < 0)
Takashi Sakamoto04130cf2019-07-22 12:36:58 +09001144 goto err_pkt_descs;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +09001145 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001146
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +09001147 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +09001148 tag = FW_ISO_CONTEXT_MATCH_TAG1;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +09001149 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
Takashi Sakamoto7ab56642014-04-25 22:45:03 +09001150 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1151
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001152 s->callbacked = false;
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001153 err = fw_iso_context_start(s->context, start_cycle, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001154 if (err < 0)
Takashi Sakamoto04130cf2019-07-22 12:36:58 +09001155 goto err_pkt_descs;
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001156
1157 mutex_unlock(&s->mutex);
1158
1159 return 0;
Takashi Sakamoto04130cf2019-07-22 12:36:58 +09001160err_pkt_descs:
1161 kfree(s->pkt_descs);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001162err_context:
1163 fw_iso_context_destroy(s->context);
1164 s->context = ERR_PTR(-1);
1165err_buffer:
1166 iso_packets_buffer_destroy(&s->buffer, s->unit);
1167err_unlock:
1168 mutex_unlock(&s->mutex);
1169
1170 return err;
1171}
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001172
1173/**
Takashi Sakamotof890f9a2019-10-18 15:19:07 +09001174 * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1175 * @d: the AMDTP domain.
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001176 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +02001177 *
1178 * Returns the current buffer position, in frames.
1179 */
Takashi Sakamotof890f9a2019-10-18 15:19:07 +09001180unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
1181 struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +02001182{
Takashi Sakamotof890f9a2019-10-18 15:19:07 +09001183 struct amdtp_stream *irq_target = d->irq_target;
1184
1185 if (irq_target && amdtp_stream_running(irq_target)) {
1186 // This function is called in software IRQ context of
1187 // period_tasklet or process context.
1188 //
1189 // When the software IRQ context was scheduled by software IRQ
1190 // context of IT contexts, queued packets were already handled.
1191 // Therefore, no need to flush the queue in buffer furthermore.
1192 //
1193 // When the process context reach here, some packets will be
1194 // already queued in the buffer. These packets should be handled
1195 // immediately to keep better granularity of PCM pointer.
1196 //
1197 // Later, the process context will sometimes schedules software
1198 // IRQ context of the period_tasklet. Then, no need to flush the
1199 // queue by the same reason as described in the above
1200 if (!in_interrupt()) {
1201 // Queued packet should be processed without any kernel
1202 // preemption to keep latency against bus cycle.
1203 preempt_disable();
1204 fw_iso_context_flush_completions(irq_target->context);
1205 preempt_enable();
1206 }
1207 }
Clemens Ladische9148dd2012-05-13 18:49:14 +02001208
Mark Rutland6aa7de02017-10-23 14:07:29 -07001209 return READ_ONCE(s->pcm_buffer_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +02001210}
Takashi Sakamotof890f9a2019-10-18 15:19:07 +09001211EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +02001212
1213/**
Takashi Sakamotoe6dcc922019-10-18 15:19:08 +09001214 * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1215 * @d: the AMDTP domain.
Takashi Sakamoto875becf2017-06-07 09:38:05 +09001216 * @s: the AMDTP stream that transfers the PCM frames
1217 *
1218 * Returns zero always.
1219 */
Takashi Sakamotoe6dcc922019-10-18 15:19:08 +09001220int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s)
Takashi Sakamoto875becf2017-06-07 09:38:05 +09001221{
Takashi Sakamotoe6dcc922019-10-18 15:19:08 +09001222 struct amdtp_stream *irq_target = d->irq_target;
1223
1224 // Process isochronous packets for recent isochronous cycle to handle
1225 // queued PCM frames.
1226 if (irq_target && amdtp_stream_running(irq_target)) {
1227 // Queued packet should be processed without any kernel
1228 // preemption to keep latency against bus cycle.
1229 preempt_disable();
1230 fw_iso_context_flush_completions(irq_target->context);
1231 preempt_enable();
1232 }
Takashi Sakamoto875becf2017-06-07 09:38:05 +09001233
1234 return 0;
1235}
Takashi Sakamotoe6dcc922019-10-18 15:19:08 +09001236EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_ack);
Takashi Sakamoto875becf2017-06-07 09:38:05 +09001237
1238/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001239 * amdtp_stream_update - update the stream after a bus reset
1240 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001241 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001242void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001243{
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +09001244 /* Precomputing. */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001245 WRITE_ONCE(s->source_node_id_field,
1246 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001247}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001248EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001249
1250/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001251 * amdtp_stream_stop - stop sending packets
1252 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001253 *
1254 * All PCM and MIDI devices of the stream must be stopped before the stream
1255 * itself can be stopped.
1256 */
Takashi Sakamoto74f94e42019-08-04 15:21:38 +09001257static void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001258{
1259 mutex_lock(&s->mutex);
1260
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001261 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001262 mutex_unlock(&s->mutex);
1263 return;
1264 }
1265
Clemens Ladisch76fb8782012-05-13 22:03:09 +02001266 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001267 fw_iso_context_stop(s->context);
1268 fw_iso_context_destroy(s->context);
1269 s->context = ERR_PTR(-1);
1270 iso_packets_buffer_destroy(&s->buffer, s->unit);
Takashi Sakamoto04130cf2019-07-22 12:36:58 +09001271 kfree(s->pkt_descs);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001272
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001273 s->callbacked = false;
1274
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001275 mutex_unlock(&s->mutex);
1276}
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001277
1278/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001279 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001280 * @s: the AMDTP stream about to be stopped
1281 *
1282 * If the isochronous stream needs to be stopped asynchronously, call this
1283 * function first to stop the PCM device.
1284 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001285void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001286{
1287 struct snd_pcm_substream *pcm;
1288
Mark Rutland6aa7de02017-10-23 14:07:29 -07001289 pcm = READ_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +01001290 if (pcm)
1291 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001292}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001293EXPORT_SYMBOL(amdtp_stream_pcm_abort);
Takashi Sakamoto3ec3d7a2019-08-04 15:21:20 +09001294
1295/**
1296 * amdtp_domain_init - initialize an AMDTP domain structure
1297 * @d: the AMDTP domain to initialize.
1298 */
1299int amdtp_domain_init(struct amdtp_domain *d)
1300{
1301 INIT_LIST_HEAD(&d->streams);
1302
Takashi Sakamotod68c3122019-10-07 20:05:16 +09001303 d->events_per_period = 0;
1304
Takashi Sakamoto25babf22020-05-08 13:36:33 +09001305 d->seq_descs = NULL;
1306
Takashi Sakamoto3ec3d7a2019-08-04 15:21:20 +09001307 return 0;
1308}
1309EXPORT_SYMBOL_GPL(amdtp_domain_init);
1310
1311/**
1312 * amdtp_domain_destroy - destroy an AMDTP domain structure
1313 * @d: the AMDTP domain to destroy.
1314 */
1315void amdtp_domain_destroy(struct amdtp_domain *d)
1316{
Takashi Sakamoto8d0d5c32019-09-06 22:14:14 +09001317 // At present nothing to do.
1318 return;
Takashi Sakamoto3ec3d7a2019-08-04 15:21:20 +09001319}
1320EXPORT_SYMBOL_GPL(amdtp_domain_destroy);
Takashi Sakamoto6261f902019-08-04 15:21:21 +09001321
1322/**
Takashi Sakamoto157a53e2019-08-04 15:21:22 +09001323 * amdtp_domain_add_stream - register isoc context into the domain.
1324 * @d: the AMDTP domain.
1325 * @s: the AMDTP stream.
1326 * @channel: the isochronous channel on the bus.
1327 * @speed: firewire speed code.
1328 */
1329int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
1330 int channel, int speed)
1331{
1332 struct amdtp_stream *tmp;
1333
1334 list_for_each_entry(tmp, &d->streams, list) {
1335 if (s == tmp)
1336 return -EBUSY;
1337 }
1338
1339 list_add(&s->list, &d->streams);
1340
1341 s->channel = channel;
1342 s->speed = speed;
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +09001343 s->domain = d;
Takashi Sakamoto157a53e2019-08-04 15:21:22 +09001344
1345 return 0;
1346}
1347EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
1348
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001349static int get_current_cycle_time(struct fw_card *fw_card, int *cur_cycle)
1350{
1351 int generation;
1352 int rcode;
1353 __be32 reg;
1354 u32 data;
1355
1356 // This is a request to local 1394 OHCI controller and expected to
1357 // complete without any event waiting.
1358 generation = fw_card->generation;
1359 smp_rmb(); // node_id vs. generation.
1360 rcode = fw_run_transaction(fw_card, TCODE_READ_QUADLET_REQUEST,
1361 fw_card->node_id, generation, SCODE_100,
1362 CSR_REGISTER_BASE + CSR_CYCLE_TIME,
1363 &reg, sizeof(reg));
1364 if (rcode != RCODE_COMPLETE)
1365 return -EIO;
1366
1367 data = be32_to_cpu(reg);
1368 *cur_cycle = data >> 12;
1369
1370 return 0;
1371}
1372
Takashi Sakamoto157a53e2019-08-04 15:21:22 +09001373/**
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001374 * amdtp_domain_start - start sending packets for isoc context in the domain.
1375 * @d: the AMDTP domain.
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001376 * @ir_delay_cycle: the cycle delay to start all IR contexts.
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001377 */
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001378int amdtp_domain_start(struct amdtp_domain *d, unsigned int ir_delay_cycle)
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001379{
Takashi Sakamoto1a4be182020-05-08 13:36:34 +09001380 static const struct {
1381 unsigned int data_block;
1382 unsigned int syt_offset;
1383 } *entry, initial_state[] = {
1384 [CIP_SFC_32000] = { 4, 3072 },
1385 [CIP_SFC_48000] = { 6, 1024 },
1386 [CIP_SFC_96000] = { 12, 1024 },
1387 [CIP_SFC_192000] = { 24, 1024 },
1388 [CIP_SFC_44100] = { 0, 67 },
1389 [CIP_SFC_88200] = { 0, 67 },
1390 [CIP_SFC_176400] = { 0, 67 },
1391 };
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001392 unsigned int events_per_buffer = d->events_per_buffer;
1393 unsigned int events_per_period = d->events_per_period;
1394 unsigned int idle_irq_interval;
1395 unsigned int queue_size;
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001396 struct amdtp_stream *s;
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001397 int cycle;
1398 int err;
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001399
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001400 // Select an IT context as IRQ target.
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001401 list_for_each_entry(s, &d->streams, list) {
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001402 if (s->direction == AMDTP_OUT_STREAM)
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001403 break;
1404 }
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001405 if (!s)
1406 return -ENXIO;
1407 d->irq_target = s;
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001408
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001409 // This is a case that AMDTP streams in domain run just for MIDI
1410 // substream. Use the number of events equivalent to 10 msec as
1411 // interval of hardware IRQ.
1412 if (events_per_period == 0)
1413 events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100;
1414 if (events_per_buffer == 0)
1415 events_per_buffer = events_per_period * 3;
1416
1417 queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
1418 amdtp_rate_table[d->irq_target->sfc]);
1419
Takashi Sakamoto25babf22020-05-08 13:36:33 +09001420 d->seq_descs = kcalloc(queue_size, sizeof(*d->seq_descs), GFP_KERNEL);
1421 if (!d->seq_descs)
1422 return -ENOMEM;
1423 d->seq_size = queue_size;
1424 d->seq_tail = 0;
1425
Takashi Sakamoto1a4be182020-05-08 13:36:34 +09001426 entry = &initial_state[s->sfc];
1427 d->data_block_state = entry->data_block;
1428 d->syt_offset_state = entry->syt_offset;
1429 d->last_syt_offset = TICKS_PER_CYCLE;
1430
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001431 if (ir_delay_cycle > 0) {
1432 struct fw_card *fw_card = fw_parent_device(s->unit)->card;
1433
1434 err = get_current_cycle_time(fw_card, &cycle);
1435 if (err < 0)
Takashi Sakamoto25babf22020-05-08 13:36:33 +09001436 goto error;
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001437
1438 // No need to care overflow in cycle field because of enough
1439 // width.
1440 cycle += ir_delay_cycle;
1441
1442 // Round up to sec field.
1443 if ((cycle & 0x00001fff) >= CYCLES_PER_SECOND) {
1444 unsigned int sec;
1445
1446 // The sec field can overflow.
1447 sec = (cycle & 0xffffe000) >> 13;
1448 cycle = (++sec << 13) |
1449 ((cycle & 0x00001fff) / CYCLES_PER_SECOND);
1450 }
1451
1452 // In OHCI 1394 specification, lower 2 bits are available for
1453 // sec field.
1454 cycle &= 0x00007fff;
1455 } else {
1456 cycle = -1;
1457 }
1458
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001459 list_for_each_entry(s, &d->streams, list) {
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001460 int cycle_match;
1461
1462 if (s->direction == AMDTP_IN_STREAM) {
1463 cycle_match = cycle;
1464 } else {
1465 // IT context starts immediately.
1466 cycle_match = -1;
Takashi Sakamoto1a4be182020-05-08 13:36:34 +09001467 s->ctx_data.rx.seq_index = 0;
Takashi Sakamotoacfedcb2019-10-18 15:19:11 +09001468 }
1469
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001470 if (s != d->irq_target) {
Takashi Sakamoto2472cfb2020-05-08 13:36:28 +09001471 err = amdtp_stream_start(s, s->channel, s->speed,
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001472 cycle_match, queue_size, 0);
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001473 if (err < 0)
1474 goto error;
1475 }
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001476 }
1477
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001478 s = d->irq_target;
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001479 s->ctx_data.rx.events_per_period = events_per_period;
1480 s->ctx_data.rx.event_count = 0;
Takashi Sakamoto1a4be182020-05-08 13:36:34 +09001481 s->ctx_data.rx.seq_index = 0;
Takashi Sakamotoaf86b0b2020-05-08 13:36:29 +09001482
1483 idle_irq_interval = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period,
1484 amdtp_rate_table[d->irq_target->sfc]);
1485 err = amdtp_stream_start(s, s->channel, s->speed, -1, queue_size,
1486 idle_irq_interval);
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001487 if (err < 0)
1488 goto error;
1489
1490 return 0;
1491error:
1492 list_for_each_entry(s, &d->streams, list)
1493 amdtp_stream_stop(s);
Takashi Sakamoto25babf22020-05-08 13:36:33 +09001494 kfree(d->seq_descs);
1495 d->seq_descs = NULL;
Takashi Sakamoto9b4702b2019-08-04 15:21:23 +09001496 return err;
1497}
1498EXPORT_SYMBOL_GPL(amdtp_domain_start);
1499
1500/**
Takashi Sakamoto6261f902019-08-04 15:21:21 +09001501 * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
1502 * @d: the AMDTP domain to which the isoc contexts belong.
1503 */
1504void amdtp_domain_stop(struct amdtp_domain *d)
1505{
1506 struct amdtp_stream *s, *next;
1507
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001508 if (d->irq_target)
1509 amdtp_stream_stop(d->irq_target);
1510
Takashi Sakamoto6261f902019-08-04 15:21:21 +09001511 list_for_each_entry_safe(s, next, &d->streams, list) {
1512 list_del(&s->list);
1513
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001514 if (s != d->irq_target)
1515 amdtp_stream_stop(s);
Takashi Sakamoto6261f902019-08-04 15:21:21 +09001516 }
Takashi Sakamotod68c3122019-10-07 20:05:16 +09001517
1518 d->events_per_period = 0;
Takashi Sakamoto60dd4922019-10-18 15:19:10 +09001519 d->irq_target = NULL;
Takashi Sakamoto25babf22020-05-08 13:36:33 +09001520
1521 kfree(d->seq_descs);
1522 d->seq_descs = NULL;
Takashi Sakamoto6261f902019-08-04 15:21:21 +09001523}
1524EXPORT_SYMBOL_GPL(amdtp_domain_stop);