Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Audio and Music Data Transmission Protocol (IEC 61883-6) streams |
| 3 | * with Common Isochronous Packet (IEC 61883-1) headers |
| 4 | * |
| 5 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> |
| 6 | * Licensed under the terms of the GNU General Public License, version 2. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/firewire.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <sound/pcm.h> |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 15 | #include <sound/pcm_params.h> |
Takashi Sakamoto | d67c46b | 2015-09-19 11:21:54 +0900 | [diff] [blame] | 16 | #include "amdtp-stream.h" |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 17 | |
| 18 | #define TICKS_PER_CYCLE 3072 |
| 19 | #define CYCLES_PER_SECOND 8000 |
| 20 | #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) |
| 21 | |
Takashi Sakamoto | 0c95c1d | 2016-05-09 21:12:46 +0900 | [diff] [blame] | 22 | /* Always support Linux tracing subsystem. */ |
| 23 | #define CREATE_TRACE_POINTS |
| 24 | #include "amdtp-stream-trace.h" |
| 25 | |
Takashi Sakamoto | ca5b505 | 2015-02-21 11:50:17 +0900 | [diff] [blame] | 26 | #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 27 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 28 | /* isochronous header parameters */ |
| 29 | #define ISO_DATA_LENGTH_SHIFT 16 |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 30 | #define TAG_CIP 1 |
| 31 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 32 | /* common isochronous packet header parameters */ |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 33 | #define CIP_EOH_SHIFT 31 |
| 34 | #define CIP_EOH (1u << CIP_EOH_SHIFT) |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 35 | #define CIP_EOH_MASK 0x80000000 |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 36 | #define CIP_SID_SHIFT 24 |
| 37 | #define CIP_SID_MASK 0x3f000000 |
| 38 | #define CIP_DBS_MASK 0x00ff0000 |
| 39 | #define CIP_DBS_SHIFT 16 |
Takashi Sakamoto | 9863874 | 2017-03-22 21:30:16 +0900 | [diff] [blame] | 40 | #define CIP_SPH_MASK 0x00000400 |
| 41 | #define CIP_SPH_SHIFT 10 |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 42 | #define CIP_DBC_MASK 0x000000ff |
| 43 | #define CIP_FMT_SHIFT 24 |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 44 | #define CIP_FMT_MASK 0x3f000000 |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 45 | #define CIP_FDF_MASK 0x00ff0000 |
| 46 | #define CIP_FDF_SHIFT 16 |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 47 | #define CIP_SYT_MASK 0x0000ffff |
| 48 | #define CIP_SYT_NO_INFO 0xffff |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 49 | |
Takashi Sakamoto | 51c29fd | 2015-09-19 11:21:56 +0900 | [diff] [blame] | 50 | /* Audio and Music transfer protocol specific parameters */ |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 51 | #define CIP_FMT_AM 0x10 |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 52 | #define AMDTP_FDF_NO_DATA 0xff |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 53 | |
| 54 | /* TODO: make these configurable */ |
| 55 | #define INTERRUPT_INTERVAL 16 |
| 56 | #define QUEUE_LENGTH 48 |
| 57 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 58 | #define IN_PACKET_HEADER_SIZE 4 |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 59 | #define OUT_PACKET_HEADER_SIZE 0 |
| 60 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 61 | static void pcm_period_tasklet(unsigned long data); |
| 62 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 63 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 64 | * amdtp_stream_init - initialize an AMDTP stream structure |
| 65 | * @s: the AMDTP stream to initialize |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 66 | * @unit: the target of the stream |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 67 | * @dir: the direction of stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 68 | * @flags: the packet transmission method to use |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 69 | * @fmt: the value of fmt field in CIP header |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 70 | * @process_data_blocks: callback handler to process data blocks |
| 71 | * @protocol_size: the size to allocate newly for protocol |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 72 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 73 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 74 | enum amdtp_stream_direction dir, enum cip_flags flags, |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 75 | unsigned int fmt, |
| 76 | amdtp_stream_process_data_blocks_t process_data_blocks, |
| 77 | unsigned int protocol_size) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 78 | { |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 79 | if (process_data_blocks == NULL) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | s->protocol = kzalloc(protocol_size, GFP_KERNEL); |
| 83 | if (!s->protocol) |
| 84 | return -ENOMEM; |
| 85 | |
Takashi Sakamoto | c6f224d | 2015-02-21 23:54:58 +0900 | [diff] [blame] | 86 | s->unit = unit; |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 87 | s->direction = dir; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 88 | s->flags = flags; |
| 89 | s->context = ERR_PTR(-1); |
| 90 | mutex_init(&s->mutex); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 91 | tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 92 | s->packet_index = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 93 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 94 | init_waitqueue_head(&s->callback_wait); |
| 95 | s->callbacked = false; |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 96 | |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 97 | s->fmt = fmt; |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 98 | s->process_data_blocks = process_data_blocks; |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 99 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 100 | return 0; |
| 101 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 102 | EXPORT_SYMBOL(amdtp_stream_init); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 103 | |
| 104 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 105 | * amdtp_stream_destroy - free stream resources |
| 106 | * @s: the AMDTP stream to destroy |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 107 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 108 | void amdtp_stream_destroy(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 109 | { |
Takashi Sakamoto | 44c376b | 2016-03-31 08:47:02 +0900 | [diff] [blame] | 110 | /* Not initialized. */ |
| 111 | if (s->protocol == NULL) |
| 112 | return; |
| 113 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 114 | WARN_ON(amdtp_stream_running(s)); |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 115 | kfree(s->protocol); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 116 | mutex_destroy(&s->mutex); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 117 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 118 | EXPORT_SYMBOL(amdtp_stream_destroy); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 119 | |
Clemens Ladisch | c5280e9 | 2011-10-16 21:39:00 +0200 | [diff] [blame] | 120 | const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 121 | [CIP_SFC_32000] = 8, |
| 122 | [CIP_SFC_44100] = 8, |
| 123 | [CIP_SFC_48000] = 8, |
| 124 | [CIP_SFC_88200] = 16, |
| 125 | [CIP_SFC_96000] = 16, |
| 126 | [CIP_SFC_176400] = 32, |
| 127 | [CIP_SFC_192000] = 32, |
| 128 | }; |
| 129 | EXPORT_SYMBOL(amdtp_syt_intervals); |
| 130 | |
Takashi Sakamoto | f9503a6 | 2014-05-28 00:14:36 +0900 | [diff] [blame] | 131 | const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { |
Takashi Sakamoto | 1017abe | 2014-04-25 22:44:59 +0900 | [diff] [blame] | 132 | [CIP_SFC_32000] = 32000, |
| 133 | [CIP_SFC_44100] = 44100, |
| 134 | [CIP_SFC_48000] = 48000, |
| 135 | [CIP_SFC_88200] = 88200, |
| 136 | [CIP_SFC_96000] = 96000, |
| 137 | [CIP_SFC_176400] = 176400, |
| 138 | [CIP_SFC_192000] = 192000, |
| 139 | }; |
| 140 | EXPORT_SYMBOL(amdtp_rate_table); |
| 141 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 142 | /** |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 143 | * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream |
| 144 | * @s: the AMDTP stream, which must be initialized. |
| 145 | * @runtime: the PCM substream runtime |
| 146 | */ |
| 147 | int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, |
| 148 | struct snd_pcm_runtime *runtime) |
| 149 | { |
| 150 | int err; |
| 151 | |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 152 | /* |
| 153 | * Currently firewire-lib processes 16 packets in one software |
| 154 | * interrupt callback. This equals to 2msec but actually the |
| 155 | * interval of the interrupts has a jitter. |
| 156 | * Additionally, even if adding a constraint to fit period size to |
| 157 | * 2msec, actual calculated frames per period doesn't equal to 2msec, |
| 158 | * depending on sampling rate. |
| 159 | * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec. |
| 160 | * Here let us use 5msec for safe period interrupt. |
| 161 | */ |
| 162 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 163 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 164 | 5000, UINT_MAX); |
| 165 | if (err < 0) |
| 166 | goto end; |
| 167 | |
| 168 | /* Non-Blocking stream has no more constraints */ |
| 169 | if (!(s->flags & CIP_BLOCKING)) |
| 170 | goto end; |
| 171 | |
| 172 | /* |
| 173 | * One AMDTP packet can include some frames. In blocking mode, the |
| 174 | * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, |
| 175 | * depending on its sampling rate. For accurate period interrupt, it's |
Yannick Guerrini | ce99198 | 2015-03-09 22:13:03 +0100 | [diff] [blame] | 176 | * preferrable to align period/buffer sizes to current SYT_INTERVAL. |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 177 | * |
Yannick Guerrini | ce99198 | 2015-03-09 22:13:03 +0100 | [diff] [blame] | 178 | * TODO: These constraints can be improved with proper rules. |
| 179 | * Currently apply LCM of SYT_INTERVALs. |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 180 | */ |
| 181 | err = snd_pcm_hw_constraint_step(runtime, 0, |
| 182 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32); |
| 183 | if (err < 0) |
| 184 | goto end; |
| 185 | err = snd_pcm_hw_constraint_step(runtime, 0, |
| 186 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32); |
| 187 | end: |
| 188 | return err; |
| 189 | } |
| 190 | EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); |
| 191 | |
| 192 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 193 | * amdtp_stream_set_parameters - set stream parameters |
| 194 | * @s: the AMDTP stream to configure |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 195 | * @rate: the sample rate |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 196 | * @data_block_quadlets: the size of a data block in quadlet unit |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 197 | * |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 198 | * The parameters must be set before the stream is started, and must not be |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 199 | * changed while the stream is running. |
| 200 | */ |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 201 | int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, |
| 202 | unsigned int data_block_quadlets) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 203 | { |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 204 | unsigned int sfc; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 205 | |
Takashi Sakamoto | 547e631 | 2015-09-19 11:21:49 +0900 | [diff] [blame] | 206 | for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { |
Takashi Sakamoto | 1017abe | 2014-04-25 22:44:59 +0900 | [diff] [blame] | 207 | if (amdtp_rate_table[sfc] == rate) |
Takashi Sakamoto | 547e631 | 2015-09-19 11:21:49 +0900 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | if (sfc == ARRAY_SIZE(amdtp_rate_table)) |
| 211 | return -EINVAL; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 212 | |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 213 | s->sfc = sfc; |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 214 | s->data_block_quadlets = data_block_quadlets; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 215 | s->syt_interval = amdtp_syt_intervals[sfc]; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 216 | |
| 217 | /* default buffering in the device */ |
| 218 | s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; |
| 219 | if (s->flags & CIP_BLOCKING) |
| 220 | /* additional buffering needed to adjust for no-data packets */ |
| 221 | s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 222 | |
Takashi Sakamoto | 547e631 | 2015-09-19 11:21:49 +0900 | [diff] [blame] | 223 | return 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 224 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 225 | EXPORT_SYMBOL(amdtp_stream_set_parameters); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 226 | |
| 227 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 228 | * amdtp_stream_get_max_payload - get the stream's packet size |
| 229 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 230 | * |
| 231 | * This function must not be called before the stream has been configured |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 232 | * with amdtp_stream_set_parameters(). |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 233 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 234 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 235 | { |
Takashi Sakamoto | a206471 | 2015-05-22 23:00:50 +0900 | [diff] [blame] | 236 | unsigned int multiplier = 1; |
| 237 | |
| 238 | if (s->flags & CIP_JUMBO_PAYLOAD) |
| 239 | multiplier = 5; |
| 240 | |
| 241 | return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 242 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 243 | EXPORT_SYMBOL(amdtp_stream_get_max_payload); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 244 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 245 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 246 | * amdtp_stream_pcm_prepare - prepare PCM device for running |
| 247 | * @s: the AMDTP stream |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 248 | * |
| 249 | * This function should be called from the PCM device's .prepare callback. |
| 250 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 251 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s) |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 252 | { |
| 253 | tasklet_kill(&s->period_tasklet); |
| 254 | s->pcm_buffer_pointer = 0; |
| 255 | s->pcm_period_pointer = 0; |
| 256 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 257 | EXPORT_SYMBOL(amdtp_stream_pcm_prepare); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 258 | |
Takashi Sakamoto | 875be09 | 2015-05-22 23:00:51 +0900 | [diff] [blame] | 259 | static unsigned int calculate_data_blocks(struct amdtp_stream *s, |
| 260 | unsigned int syt) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 261 | { |
| 262 | unsigned int phase, data_blocks; |
| 263 | |
Takashi Sakamoto | 875be09 | 2015-05-22 23:00:51 +0900 | [diff] [blame] | 264 | /* Blocking mode. */ |
| 265 | if (s->flags & CIP_BLOCKING) { |
| 266 | /* This module generate empty packet for 'no data'. */ |
| 267 | if (syt == CIP_SYT_NO_INFO) |
| 268 | data_blocks = 0; |
| 269 | else |
| 270 | data_blocks = s->syt_interval; |
| 271 | /* Non-blocking mode. */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 272 | } else { |
Takashi Sakamoto | 875be09 | 2015-05-22 23:00:51 +0900 | [diff] [blame] | 273 | if (!cip_sfc_is_base_44100(s->sfc)) { |
| 274 | /* Sample_rate / 8000 is an integer, and precomputed. */ |
| 275 | data_blocks = s->data_block_state; |
| 276 | } else { |
| 277 | phase = s->data_block_state; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * This calculates the number of data blocks per packet so that |
| 281 | * 1) the overall rate is correct and exactly synchronized to |
| 282 | * the bus clock, and |
| 283 | * 2) packets with a rounded-up number of blocks occur as early |
| 284 | * as possible in the sequence (to prevent underruns of the |
| 285 | * device's buffer). |
| 286 | */ |
Takashi Sakamoto | 875be09 | 2015-05-22 23:00:51 +0900 | [diff] [blame] | 287 | if (s->sfc == CIP_SFC_44100) |
| 288 | /* 6 6 5 6 5 6 5 ... */ |
| 289 | data_blocks = 5 + ((phase & 1) ^ |
| 290 | (phase == 0 || phase >= 40)); |
| 291 | else |
| 292 | /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ |
| 293 | data_blocks = 11 * (s->sfc >> 1) + (phase == 0); |
| 294 | if (++phase >= (80 >> (s->sfc >> 1))) |
| 295 | phase = 0; |
| 296 | s->data_block_state = phase; |
| 297 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return data_blocks; |
| 301 | } |
| 302 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 303 | static unsigned int calculate_syt(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 304 | unsigned int cycle) |
| 305 | { |
| 306 | unsigned int syt_offset, phase, index, syt; |
| 307 | |
| 308 | if (s->last_syt_offset < TICKS_PER_CYCLE) { |
| 309 | if (!cip_sfc_is_base_44100(s->sfc)) |
| 310 | syt_offset = s->last_syt_offset + s->syt_offset_state; |
| 311 | else { |
| 312 | /* |
| 313 | * The time, in ticks, of the n'th SYT_INTERVAL sample is: |
| 314 | * n * SYT_INTERVAL * 24576000 / sample_rate |
| 315 | * Modulo TICKS_PER_CYCLE, the difference between successive |
| 316 | * elements is about 1386.23. Rounding the results of this |
| 317 | * formula to the SYT precision results in a sequence of |
| 318 | * differences that begins with: |
| 319 | * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... |
| 320 | * This code generates _exactly_ the same sequence. |
| 321 | */ |
| 322 | phase = s->syt_offset_state; |
| 323 | index = phase % 13; |
| 324 | syt_offset = s->last_syt_offset; |
| 325 | syt_offset += 1386 + ((index && !(index & 3)) || |
| 326 | phase == 146); |
| 327 | if (++phase >= 147) |
| 328 | phase = 0; |
| 329 | s->syt_offset_state = phase; |
| 330 | } |
| 331 | } else |
| 332 | syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; |
| 333 | s->last_syt_offset = syt_offset; |
| 334 | |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 335 | if (syt_offset < TICKS_PER_CYCLE) { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 336 | syt_offset += s->transfer_delay; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 337 | syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; |
| 338 | syt += syt_offset % TICKS_PER_CYCLE; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 339 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 340 | return syt & CIP_SYT_MASK; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 341 | } else { |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 342 | return CIP_SYT_NO_INFO; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 343 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 346 | static void update_pcm_pointers(struct amdtp_stream *s, |
| 347 | struct snd_pcm_substream *pcm, |
| 348 | unsigned int frames) |
Takashi Sakamoto | 65845f2 | 2014-08-29 13:40:45 +0900 | [diff] [blame] | 349 | { |
| 350 | unsigned int ptr; |
| 351 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 352 | ptr = s->pcm_buffer_pointer + frames; |
| 353 | if (ptr >= pcm->runtime->buffer_size) |
| 354 | ptr -= pcm->runtime->buffer_size; |
| 355 | ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; |
| 356 | |
| 357 | s->pcm_period_pointer += frames; |
| 358 | if (s->pcm_period_pointer >= pcm->runtime->period_size) { |
| 359 | s->pcm_period_pointer -= pcm->runtime->period_size; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 360 | tasklet_hi_schedule(&s->period_tasklet); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static void pcm_period_tasklet(unsigned long data) |
| 365 | { |
| 366 | struct amdtp_stream *s = (void *)data; |
| 367 | struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); |
| 368 | |
| 369 | if (pcm) |
| 370 | snd_pcm_period_elapsed(pcm); |
| 371 | } |
| 372 | |
Takashi Sakamoto | ff38e0c | 2016-05-11 07:35:09 +0900 | [diff] [blame] | 373 | static int queue_packet(struct amdtp_stream *s, unsigned int header_length, |
| 374 | unsigned int payload_length) |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 375 | { |
| 376 | struct fw_iso_packet p = {0}; |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 377 | int err = 0; |
| 378 | |
| 379 | if (IS_ERR(s->context)) |
| 380 | goto end; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 381 | |
| 382 | p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL); |
| 383 | p.tag = TAG_CIP; |
| 384 | p.header_length = header_length; |
Takashi Sakamoto | ff38e0c | 2016-05-11 07:35:09 +0900 | [diff] [blame] | 385 | if (payload_length > 0) |
| 386 | p.payload_length = payload_length; |
| 387 | else |
| 388 | p.skip = true; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 389 | err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer, |
| 390 | s->buffer.packets[s->packet_index].offset); |
| 391 | if (err < 0) { |
| 392 | dev_err(&s->unit->device, "queueing error: %d\n", err); |
| 393 | goto end; |
| 394 | } |
| 395 | |
| 396 | if (++s->packet_index >= QUEUE_LENGTH) |
| 397 | s->packet_index = 0; |
| 398 | end: |
| 399 | return err; |
| 400 | } |
| 401 | |
| 402 | static inline int queue_out_packet(struct amdtp_stream *s, |
Takashi Sakamoto | ff38e0c | 2016-05-11 07:35:09 +0900 | [diff] [blame] | 403 | unsigned int payload_length) |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 404 | { |
Takashi Sakamoto | ff38e0c | 2016-05-11 07:35:09 +0900 | [diff] [blame] | 405 | return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length); |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 406 | } |
| 407 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 408 | static inline int queue_in_packet(struct amdtp_stream *s) |
| 409 | { |
| 410 | return queue_packet(s, IN_PACKET_HEADER_SIZE, |
Takashi Sakamoto | ff38e0c | 2016-05-11 07:35:09 +0900 | [diff] [blame] | 411 | amdtp_stream_get_max_payload(s)); |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 412 | } |
| 413 | |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 414 | static int handle_out_packet(struct amdtp_stream *s, |
| 415 | unsigned int payload_length, unsigned int cycle, |
Takashi Sakamoto | a9c4284 | 2016-05-11 07:33:27 +0900 | [diff] [blame] | 416 | unsigned int index) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 417 | { |
| 418 | __be32 *buffer; |
Takashi Sakamoto | 390a151 | 2016-05-09 23:15:55 +0900 | [diff] [blame] | 419 | unsigned int syt; |
| 420 | unsigned int data_blocks; |
Takashi Sakamoto | 20e4457 | 2015-09-19 11:21:52 +0900 | [diff] [blame] | 421 | unsigned int pcm_frames; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 422 | struct snd_pcm_substream *pcm; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 423 | |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 424 | buffer = s->buffer.packets[s->packet_index].buffer; |
Takashi Sakamoto | 390a151 | 2016-05-09 23:15:55 +0900 | [diff] [blame] | 425 | syt = calculate_syt(s, cycle); |
| 426 | data_blocks = calculate_data_blocks(s, syt); |
Takashi Sakamoto | df075fe | 2015-09-19 11:22:02 +0900 | [diff] [blame] | 427 | pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt); |
Takashi Sakamoto | 20e4457 | 2015-09-19 11:21:52 +0900 | [diff] [blame] | 428 | |
Takashi Sakamoto | 9dae017 | 2017-03-22 21:30:17 +0900 | [diff] [blame] | 429 | if (s->flags & CIP_DBC_IS_END_EVENT) |
| 430 | s->data_block_counter = |
| 431 | (s->data_block_counter + data_blocks) & 0xff; |
| 432 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 433 | buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 434 | (s->data_block_quadlets << CIP_DBS_SHIFT) | |
Takashi Sakamoto | 9863874 | 2017-03-22 21:30:16 +0900 | [diff] [blame] | 435 | ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 436 | s->data_block_counter); |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 437 | buffer[1] = cpu_to_be32(CIP_EOH | |
| 438 | ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | |
| 439 | ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | |
| 440 | (syt & CIP_SYT_MASK)); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 441 | |
Takashi Sakamoto | 9dae017 | 2017-03-22 21:30:17 +0900 | [diff] [blame] | 442 | if (!(s->flags & CIP_DBC_IS_END_EVENT)) |
| 443 | s->data_block_counter = |
| 444 | (s->data_block_counter + data_blocks) & 0xff; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 445 | payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; |
Takashi Sakamoto | 0c95c1d | 2016-05-09 21:12:46 +0900 | [diff] [blame] | 446 | |
Takashi Sakamoto | a9c4284 | 2016-05-11 07:33:27 +0900 | [diff] [blame] | 447 | trace_out_packet(s, cycle, buffer, payload_length, index); |
Takashi Sakamoto | 0c95c1d | 2016-05-09 21:12:46 +0900 | [diff] [blame] | 448 | |
Takashi Sakamoto | ff38e0c | 2016-05-11 07:35:09 +0900 | [diff] [blame] | 449 | if (queue_out_packet(s, payload_length) < 0) |
Takashi Sakamoto | a4103bd | 2015-05-22 23:00:53 +0900 | [diff] [blame] | 450 | return -EIO; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 451 | |
Takashi Sakamoto | 20e4457 | 2015-09-19 11:21:52 +0900 | [diff] [blame] | 452 | pcm = ACCESS_ONCE(s->pcm); |
| 453 | if (pcm && pcm_frames > 0) |
| 454 | update_pcm_pointers(s, pcm, pcm_frames); |
Takashi Sakamoto | a4103bd | 2015-05-22 23:00:53 +0900 | [diff] [blame] | 455 | |
| 456 | /* No need to return the number of handled data blocks. */ |
| 457 | return 0; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Takashi Sakamoto | 6fc6b9c | 2015-05-22 23:00:52 +0900 | [diff] [blame] | 460 | static int handle_in_packet(struct amdtp_stream *s, |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 461 | unsigned int payload_length, unsigned int cycle, |
Takashi Sakamoto | a9c4284 | 2016-05-11 07:33:27 +0900 | [diff] [blame] | 462 | unsigned int index) |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 463 | { |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 464 | __be32 *buffer; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 465 | u32 cip_header[2]; |
Takashi Sakamoto | 9863874 | 2017-03-22 21:30:16 +0900 | [diff] [blame] | 466 | unsigned int sph, fmt, fdf, syt; |
Takashi Sakamoto | 6fc6b9c | 2015-05-22 23:00:52 +0900 | [diff] [blame] | 467 | unsigned int data_block_quadlets, data_block_counter, dbc_interval; |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 468 | unsigned int data_blocks; |
Takashi Sakamoto | 20e4457 | 2015-09-19 11:21:52 +0900 | [diff] [blame] | 469 | struct snd_pcm_substream *pcm; |
| 470 | unsigned int pcm_frames; |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 471 | bool lost; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 472 | |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 473 | buffer = s->buffer.packets[s->packet_index].buffer; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 474 | cip_header[0] = be32_to_cpu(buffer[0]); |
| 475 | cip_header[1] = be32_to_cpu(buffer[1]); |
| 476 | |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 477 | trace_in_packet(s, cycle, cip_header, payload_length, index); |
Takashi Sakamoto | 0c95c1d | 2016-05-09 21:12:46 +0900 | [diff] [blame] | 478 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 479 | /* |
| 480 | * This module supports 'Two-quadlet CIP header with SYT field'. |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 481 | * For convenience, also check FMT field is AM824 or not. |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 482 | */ |
Takashi Sakamoto | 2128f78 | 2017-03-22 21:30:27 +0900 | [diff] [blame] | 483 | if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || |
| 484 | ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) && |
| 485 | (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 486 | dev_info_ratelimited(&s->unit->device, |
| 487 | "Invalid CIP header for AMDTP: %08X:%08X\n", |
| 488 | cip_header[0], cip_header[1]); |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 489 | data_blocks = 0; |
Takashi Sakamoto | 20e4457 | 2015-09-19 11:21:52 +0900 | [diff] [blame] | 490 | pcm_frames = 0; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 491 | goto end; |
| 492 | } |
| 493 | |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 494 | /* Check valid protocol or not. */ |
Takashi Sakamoto | 9863874 | 2017-03-22 21:30:16 +0900 | [diff] [blame] | 495 | sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT; |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 496 | fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT; |
Takashi Sakamoto | 9863874 | 2017-03-22 21:30:16 +0900 | [diff] [blame] | 497 | if (sph != s->sph || fmt != s->fmt) { |
Takashi Sakamoto | 2a7e171 | 2015-10-11 22:33:50 +0900 | [diff] [blame] | 498 | dev_info_ratelimited(&s->unit->device, |
| 499 | "Detect unexpected protocol: %08x %08x\n", |
| 500 | cip_header[0], cip_header[1]); |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 501 | data_blocks = 0; |
Takashi Sakamoto | 2a7e171 | 2015-10-11 22:33:50 +0900 | [diff] [blame] | 502 | pcm_frames = 0; |
| 503 | goto end; |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 504 | } |
| 505 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 506 | /* Calculate data blocks */ |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 507 | fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT; |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 508 | if (payload_length < 12 || |
Takashi Sakamoto | 414ba02 | 2015-09-19 11:21:53 +0900 | [diff] [blame] | 509 | (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) { |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 510 | data_blocks = 0; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 511 | } else { |
| 512 | data_block_quadlets = |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 513 | (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 514 | /* avoid division by zero */ |
| 515 | if (data_block_quadlets == 0) { |
Takashi Sakamoto | 12e0f43 | 2015-05-22 23:21:13 +0900 | [diff] [blame] | 516 | dev_err(&s->unit->device, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 517 | "Detect invalid value in dbs field: %08X\n", |
| 518 | cip_header[0]); |
Takashi Sakamoto | a900705 | 2015-05-22 23:21:14 +0900 | [diff] [blame] | 519 | return -EPROTO; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 520 | } |
Takashi Sakamoto | 6970223 | 2014-04-25 22:45:05 +0900 | [diff] [blame] | 521 | if (s->flags & CIP_WRONG_DBS) |
| 522 | data_block_quadlets = s->data_block_quadlets; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 523 | |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 524 | data_blocks = (payload_length / 4 - 2) / |
| 525 | data_block_quadlets; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* Check data block counter continuity */ |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 529 | data_block_counter = cip_header[0] & CIP_DBC_MASK; |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 530 | if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && |
Takashi Sakamoto | 9d59124 | 2014-04-25 22:45:27 +0900 | [diff] [blame] | 531 | s->data_block_counter != UINT_MAX) |
| 532 | data_block_counter = s->data_block_counter; |
| 533 | |
Takashi Sakamoto | 18f5ed3 | 2015-08-05 09:21:05 +0900 | [diff] [blame] | 534 | if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && |
| 535 | data_block_counter == s->tx_first_dbc) || |
| 536 | s->data_block_counter == UINT_MAX) { |
Takashi Sakamoto | b84b1a2 | 2014-04-25 22:45:07 +0900 | [diff] [blame] | 537 | lost = false; |
| 538 | } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 539 | lost = data_block_counter != s->data_block_counter; |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 540 | } else { |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 541 | if (data_blocks > 0 && s->tx_dbc_interval > 0) |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 542 | dbc_interval = s->tx_dbc_interval; |
| 543 | else |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 544 | dbc_interval = data_blocks; |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 545 | |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 546 | lost = data_block_counter != |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 547 | ((s->data_block_counter + dbc_interval) & 0xff); |
| 548 | } |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 549 | |
| 550 | if (lost) { |
Takashi Sakamoto | 12e0f43 | 2015-05-22 23:21:13 +0900 | [diff] [blame] | 551 | dev_err(&s->unit->device, |
| 552 | "Detect discontinuity of CIP: %02X %02X\n", |
| 553 | s->data_block_counter, data_block_counter); |
Takashi Sakamoto | 6fc6b9c | 2015-05-22 23:00:52 +0900 | [diff] [blame] | 554 | return -EIO; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 555 | } |
| 556 | |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 557 | syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK; |
| 558 | pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt); |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 559 | |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 560 | if (s->flags & CIP_DBC_IS_END_EVENT) |
| 561 | s->data_block_counter = data_block_counter; |
| 562 | else |
| 563 | s->data_block_counter = |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 564 | (data_block_counter + data_blocks) & 0xff; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 565 | end: |
| 566 | if (queue_in_packet(s) < 0) |
Takashi Sakamoto | 6fc6b9c | 2015-05-22 23:00:52 +0900 | [diff] [blame] | 567 | return -EIO; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 568 | |
Takashi Sakamoto | 20e4457 | 2015-09-19 11:21:52 +0900 | [diff] [blame] | 569 | pcm = ACCESS_ONCE(s->pcm); |
| 570 | if (pcm && pcm_frames > 0) |
| 571 | update_pcm_pointers(s, pcm, pcm_frames); |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 572 | |
Takashi Sakamoto | 31ea49b | 2015-05-28 00:02:59 +0900 | [diff] [blame] | 573 | return 0; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 574 | } |
| 575 | |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 576 | /* |
| 577 | * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On |
| 578 | * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent |
| 579 | * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second. |
| 580 | */ |
| 581 | static inline u32 compute_cycle_count(u32 tstamp) |
| 582 | { |
| 583 | return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff); |
| 584 | } |
| 585 | |
| 586 | static inline u32 increment_cycle_count(u32 cycle, unsigned int addend) |
| 587 | { |
| 588 | cycle += addend; |
| 589 | if (cycle >= 8 * CYCLES_PER_SECOND) |
| 590 | cycle -= 8 * CYCLES_PER_SECOND; |
| 591 | return cycle; |
| 592 | } |
| 593 | |
Takashi Sakamoto | f90e2de | 2016-05-09 21:12:45 +0900 | [diff] [blame] | 594 | static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend) |
| 595 | { |
| 596 | if (cycle < subtrahend) |
| 597 | cycle += 8 * CYCLES_PER_SECOND; |
| 598 | return cycle - subtrahend; |
| 599 | } |
| 600 | |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 601 | static void out_stream_callback(struct fw_iso_context *context, u32 tstamp, |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 602 | size_t header_length, void *header, |
| 603 | void *private_data) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 604 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 605 | struct amdtp_stream *s = private_data; |
Takashi Sakamoto | 390a151 | 2016-05-09 23:15:55 +0900 | [diff] [blame] | 606 | unsigned int i, packets = header_length / 4; |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 607 | u32 cycle; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 608 | |
Takashi Sakamoto | a4103bd | 2015-05-22 23:00:53 +0900 | [diff] [blame] | 609 | if (s->packet_index < 0) |
| 610 | return; |
| 611 | |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 612 | cycle = compute_cycle_count(tstamp); |
| 613 | |
| 614 | /* Align to actual cycle count for the last packet. */ |
| 615 | cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 616 | |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 617 | for (i = 0; i < packets; ++i) { |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 618 | cycle = increment_cycle_count(cycle, 1); |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 619 | if (handle_out_packet(s, 0, cycle, i) < 0) { |
Takashi Sakamoto | a4103bd | 2015-05-22 23:00:53 +0900 | [diff] [blame] | 620 | s->packet_index = -1; |
| 621 | amdtp_stream_pcm_abort(s); |
| 622 | return; |
| 623 | } |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 624 | } |
Takashi Sakamoto | a4103bd | 2015-05-22 23:00:53 +0900 | [diff] [blame] | 625 | |
Clemens Ladisch | 13882a8 | 2011-05-02 09:33:56 +0200 | [diff] [blame] | 626 | fw_iso_context_queue_flush(s->context); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 629 | static void in_stream_callback(struct fw_iso_context *context, u32 tstamp, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 630 | size_t header_length, void *header, |
| 631 | void *private_data) |
| 632 | { |
| 633 | struct amdtp_stream *s = private_data; |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 634 | unsigned int i, packets; |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 635 | unsigned int payload_length, max_payload_length; |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 636 | __be32 *headers = header; |
Takashi Sakamoto | f90e2de | 2016-05-09 21:12:45 +0900 | [diff] [blame] | 637 | u32 cycle; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 638 | |
Takashi Sakamoto | a4103bd | 2015-05-22 23:00:53 +0900 | [diff] [blame] | 639 | if (s->packet_index < 0) |
| 640 | return; |
| 641 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 642 | /* The number of packets in buffer */ |
| 643 | packets = header_length / IN_PACKET_HEADER_SIZE; |
| 644 | |
Takashi Sakamoto | f90e2de | 2016-05-09 21:12:45 +0900 | [diff] [blame] | 645 | cycle = compute_cycle_count(tstamp); |
| 646 | |
| 647 | /* Align to actual cycle count for the last packet. */ |
| 648 | cycle = decrement_cycle_count(cycle, packets); |
| 649 | |
Takashi Sakamoto | a206471 | 2015-05-22 23:00:50 +0900 | [diff] [blame] | 650 | /* For buffer-over-run prevention. */ |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 651 | max_payload_length = amdtp_stream_get_max_payload(s); |
Takashi Sakamoto | a206471 | 2015-05-22 23:00:50 +0900 | [diff] [blame] | 652 | |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 653 | for (i = 0; i < packets; i++) { |
Takashi Sakamoto | f90e2de | 2016-05-09 21:12:45 +0900 | [diff] [blame] | 654 | cycle = increment_cycle_count(cycle, 1); |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 655 | |
| 656 | /* The number of quadlets in this packet */ |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 657 | payload_length = |
| 658 | (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT); |
| 659 | if (payload_length > max_payload_length) { |
Takashi Sakamoto | a206471 | 2015-05-22 23:00:50 +0900 | [diff] [blame] | 660 | dev_err(&s->unit->device, |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 661 | "Detect jumbo payload: %04x %04x\n", |
| 662 | payload_length, max_payload_length); |
Takashi Sakamoto | a206471 | 2015-05-22 23:00:50 +0900 | [diff] [blame] | 663 | break; |
| 664 | } |
| 665 | |
Takashi Sakamoto | ff0fb5a | 2017-03-31 22:06:06 +0900 | [diff] [blame^] | 666 | if (handle_in_packet(s, payload_length, cycle, i) < 0) |
Takashi Sakamoto | 6fc6b9c | 2015-05-22 23:00:52 +0900 | [diff] [blame] | 667 | break; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 668 | } |
| 669 | |
Takashi Sakamoto | dec63cc | 2016-05-09 23:15:53 +0900 | [diff] [blame] | 670 | /* Queueing error or detecting invalid payload. */ |
Takashi Sakamoto | d9a16fc | 2016-05-09 23:15:54 +0900 | [diff] [blame] | 671 | if (i < packets) { |
Takashi Sakamoto | dec63cc | 2016-05-09 23:15:53 +0900 | [diff] [blame] | 672 | s->packet_index = -1; |
Takashi Sakamoto | 6fc6b9c | 2015-05-22 23:00:52 +0900 | [diff] [blame] | 673 | amdtp_stream_pcm_abort(s); |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 677 | fw_iso_context_queue_flush(s->context); |
| 678 | } |
| 679 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 680 | /* this is executed one time */ |
| 681 | static void amdtp_stream_first_callback(struct fw_iso_context *context, |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 682 | u32 tstamp, size_t header_length, |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 683 | void *header, void *private_data) |
| 684 | { |
| 685 | struct amdtp_stream *s = private_data; |
Takashi Sakamoto | a04513f | 2017-03-22 21:30:15 +0900 | [diff] [blame] | 686 | u32 cycle; |
| 687 | unsigned int packets; |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 688 | |
| 689 | /* |
| 690 | * For in-stream, first packet has come. |
| 691 | * For out-stream, prepared to transmit first packet |
| 692 | */ |
| 693 | s->callbacked = true; |
| 694 | wake_up(&s->callback_wait); |
| 695 | |
Takashi Sakamoto | a04513f | 2017-03-22 21:30:15 +0900 | [diff] [blame] | 696 | cycle = compute_cycle_count(tstamp); |
| 697 | |
| 698 | if (s->direction == AMDTP_IN_STREAM) { |
| 699 | packets = header_length / IN_PACKET_HEADER_SIZE; |
| 700 | cycle = decrement_cycle_count(cycle, packets); |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 701 | context->callback.sc = in_stream_callback; |
Takashi Sakamoto | a04513f | 2017-03-22 21:30:15 +0900 | [diff] [blame] | 702 | } else { |
| 703 | packets = header_length / 4; |
| 704 | cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets); |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 705 | context->callback.sc = out_stream_callback; |
Takashi Sakamoto | a04513f | 2017-03-22 21:30:15 +0900 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | s->start_cycle = cycle; |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 709 | |
Takashi Sakamoto | 73fc7f0 | 2016-05-09 21:12:44 +0900 | [diff] [blame] | 710 | context->callback.sc(context, tstamp, header_length, header, s); |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 711 | } |
| 712 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 713 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 714 | * amdtp_stream_start - start transferring packets |
| 715 | * @s: the AMDTP stream to start |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 716 | * @channel: the isochronous channel on the bus |
| 717 | * @speed: firewire speed code |
| 718 | * |
| 719 | * The stream cannot be started until it has been configured with |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 720 | * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI |
| 721 | * device can be started. |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 722 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 723 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 724 | { |
| 725 | static const struct { |
| 726 | unsigned int data_block; |
| 727 | unsigned int syt_offset; |
| 728 | } initial_state[] = { |
| 729 | [CIP_SFC_32000] = { 4, 3072 }, |
| 730 | [CIP_SFC_48000] = { 6, 1024 }, |
| 731 | [CIP_SFC_96000] = { 12, 1024 }, |
| 732 | [CIP_SFC_192000] = { 24, 1024 }, |
| 733 | [CIP_SFC_44100] = { 0, 67 }, |
| 734 | [CIP_SFC_88200] = { 0, 67 }, |
| 735 | [CIP_SFC_176400] = { 0, 67 }, |
| 736 | }; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 737 | unsigned int header_size; |
| 738 | enum dma_data_direction dir; |
Takashi Sakamoto | 7ab5664 | 2014-04-25 22:45:03 +0900 | [diff] [blame] | 739 | int type, tag, err; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 740 | |
| 741 | mutex_lock(&s->mutex); |
| 742 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 743 | if (WARN_ON(amdtp_stream_running(s) || |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 744 | (s->data_block_quadlets < 1))) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 745 | err = -EBADFD; |
| 746 | goto err_unlock; |
| 747 | } |
| 748 | |
Takashi Sakamoto | 62f00e4 | 2016-05-09 23:15:56 +0900 | [diff] [blame] | 749 | if (s->direction == AMDTP_IN_STREAM) |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 750 | s->data_block_counter = UINT_MAX; |
| 751 | else |
| 752 | s->data_block_counter = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 753 | s->data_block_state = initial_state[s->sfc].data_block; |
| 754 | s->syt_offset_state = initial_state[s->sfc].syt_offset; |
| 755 | s->last_syt_offset = TICKS_PER_CYCLE; |
| 756 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 757 | /* initialize packet buffer */ |
| 758 | if (s->direction == AMDTP_IN_STREAM) { |
| 759 | dir = DMA_FROM_DEVICE; |
| 760 | type = FW_ISO_CONTEXT_RECEIVE; |
| 761 | header_size = IN_PACKET_HEADER_SIZE; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 762 | } else { |
| 763 | dir = DMA_TO_DEVICE; |
| 764 | type = FW_ISO_CONTEXT_TRANSMIT; |
| 765 | header_size = OUT_PACKET_HEADER_SIZE; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 766 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 767 | err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 768 | amdtp_stream_get_max_payload(s), dir); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 769 | if (err < 0) |
| 770 | goto err_unlock; |
| 771 | |
| 772 | s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 773 | type, channel, speed, header_size, |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 774 | amdtp_stream_first_callback, s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 775 | if (IS_ERR(s->context)) { |
| 776 | err = PTR_ERR(s->context); |
| 777 | if (err == -EBUSY) |
| 778 | dev_err(&s->unit->device, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 779 | "no free stream on this controller\n"); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 780 | goto err_buffer; |
| 781 | } |
| 782 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 783 | amdtp_stream_update(s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 784 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 785 | s->packet_index = 0; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 786 | do { |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 787 | if (s->direction == AMDTP_IN_STREAM) |
| 788 | err = queue_in_packet(s); |
| 789 | else |
Takashi Sakamoto | ff38e0c | 2016-05-11 07:35:09 +0900 | [diff] [blame] | 790 | err = queue_out_packet(s, 0); |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 791 | if (err < 0) |
| 792 | goto err_context; |
| 793 | } while (s->packet_index > 0); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 794 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 795 | /* NOTE: TAG1 matches CIP. This just affects in stream. */ |
Takashi Sakamoto | 7ab5664 | 2014-04-25 22:45:03 +0900 | [diff] [blame] | 796 | tag = FW_ISO_CONTEXT_MATCH_TAG1; |
| 797 | if (s->flags & CIP_EMPTY_WITH_TAG0) |
| 798 | tag |= FW_ISO_CONTEXT_MATCH_TAG0; |
| 799 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 800 | s->callbacked = false; |
Takashi Sakamoto | 7ab5664 | 2014-04-25 22:45:03 +0900 | [diff] [blame] | 801 | err = fw_iso_context_start(s->context, -1, 0, tag); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 802 | if (err < 0) |
| 803 | goto err_context; |
| 804 | |
| 805 | mutex_unlock(&s->mutex); |
| 806 | |
| 807 | return 0; |
| 808 | |
| 809 | err_context: |
| 810 | fw_iso_context_destroy(s->context); |
| 811 | s->context = ERR_PTR(-1); |
| 812 | err_buffer: |
| 813 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 814 | err_unlock: |
| 815 | mutex_unlock(&s->mutex); |
| 816 | |
| 817 | return err; |
| 818 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 819 | EXPORT_SYMBOL(amdtp_stream_start); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 820 | |
| 821 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 822 | * amdtp_stream_pcm_pointer - get the PCM buffer position |
| 823 | * @s: the AMDTP stream that transports the PCM data |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 824 | * |
| 825 | * Returns the current buffer position, in frames. |
| 826 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 827 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 828 | { |
Takashi Sakamoto | 1dba9db | 2016-05-12 02:17:39 +0900 | [diff] [blame] | 829 | /* |
| 830 | * This function is called in software IRQ context of period_tasklet or |
| 831 | * process context. |
| 832 | * |
| 833 | * When the software IRQ context was scheduled by software IRQ context |
| 834 | * of IR/IT contexts, queued packets were already handled. Therefore, |
| 835 | * no need to flush the queue in buffer anymore. |
| 836 | * |
| 837 | * When the process context reach here, some packets will be already |
| 838 | * queued in the buffer. These packets should be handled immediately |
| 839 | * to keep better granularity of PCM pointer. |
| 840 | * |
| 841 | * Later, the process context will sometimes schedules software IRQ |
| 842 | * context of the period_tasklet. Then, no need to flush the queue by |
| 843 | * the same reason as described for IR/IT contexts. |
| 844 | */ |
| 845 | if (!in_interrupt() && amdtp_stream_running(s)) |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 846 | fw_iso_context_flush_completions(s->context); |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 847 | |
| 848 | return ACCESS_ONCE(s->pcm_buffer_pointer); |
| 849 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 850 | EXPORT_SYMBOL(amdtp_stream_pcm_pointer); |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 851 | |
| 852 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 853 | * amdtp_stream_update - update the stream after a bus reset |
| 854 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 855 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 856 | void amdtp_stream_update(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 857 | { |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 858 | /* Precomputing. */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 859 | ACCESS_ONCE(s->source_node_id_field) = |
Takashi Sakamoto | 9a2820c | 2015-05-22 23:21:12 +0900 | [diff] [blame] | 860 | (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & |
| 861 | CIP_SID_MASK; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 862 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 863 | EXPORT_SYMBOL(amdtp_stream_update); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 864 | |
| 865 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 866 | * amdtp_stream_stop - stop sending packets |
| 867 | * @s: the AMDTP stream to stop |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 868 | * |
| 869 | * All PCM and MIDI devices of the stream must be stopped before the stream |
| 870 | * itself can be stopped. |
| 871 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 872 | void amdtp_stream_stop(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 873 | { |
| 874 | mutex_lock(&s->mutex); |
| 875 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 876 | if (!amdtp_stream_running(s)) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 877 | mutex_unlock(&s->mutex); |
| 878 | return; |
| 879 | } |
| 880 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 881 | tasklet_kill(&s->period_tasklet); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 882 | fw_iso_context_stop(s->context); |
| 883 | fw_iso_context_destroy(s->context); |
| 884 | s->context = ERR_PTR(-1); |
| 885 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 886 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 887 | s->callbacked = false; |
| 888 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 889 | mutex_unlock(&s->mutex); |
| 890 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 891 | EXPORT_SYMBOL(amdtp_stream_stop); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 892 | |
| 893 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 894 | * amdtp_stream_pcm_abort - abort the running PCM device |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 895 | * @s: the AMDTP stream about to be stopped |
| 896 | * |
| 897 | * If the isochronous stream needs to be stopped asynchronously, call this |
| 898 | * function first to stop the PCM device. |
| 899 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 900 | void amdtp_stream_pcm_abort(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 901 | { |
| 902 | struct snd_pcm_substream *pcm; |
| 903 | |
| 904 | pcm = ACCESS_ONCE(s->pcm); |
Takashi Iwai | 1fb8510 | 2014-11-07 17:08:28 +0100 | [diff] [blame] | 905 | if (pcm) |
| 906 | snd_pcm_stop_xrun(pcm); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 907 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 908 | EXPORT_SYMBOL(amdtp_stream_pcm_abort); |