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