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> |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 14 | #include <linux/sched.h> |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 15 | #include <sound/pcm.h> |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 16 | #include <sound/pcm_params.h> |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 17 | #include <sound/rawmidi.h> |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 18 | #include "amdtp.h" |
| 19 | |
| 20 | #define TICKS_PER_CYCLE 3072 |
| 21 | #define CYCLES_PER_SECOND 8000 |
| 22 | #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) |
| 23 | |
Clemens Ladisch | 5c697e5 | 2014-11-25 22:52:24 +0100 | [diff] [blame^] | 24 | /* |
| 25 | * Several devices look only at the first eight data blocks. |
| 26 | * In any case, this is more than enough for the MIDI data rate. |
| 27 | */ |
| 28 | #define MAX_MIDI_RX_BLOCKS 8 |
| 29 | |
| 30 | #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 31 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 32 | /* isochronous header parameters */ |
| 33 | #define ISO_DATA_LENGTH_SHIFT 16 |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 34 | #define TAG_CIP 1 |
| 35 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 36 | /* common isochronous packet header parameters */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 37 | #define CIP_EOH (1u << 31) |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 38 | #define CIP_EOH_MASK 0x80000000 |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 39 | #define CIP_FMT_AM (0x10 << 24) |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 40 | #define CIP_FMT_MASK 0x3f000000 |
| 41 | #define CIP_SYT_MASK 0x0000ffff |
| 42 | #define CIP_SYT_NO_INFO 0xffff |
| 43 | #define CIP_FDF_MASK 0x00ff0000 |
| 44 | #define CIP_FDF_SFC_SHIFT 16 |
| 45 | |
| 46 | /* |
| 47 | * Audio and Music transfer protocol specific parameters |
| 48 | * only "Clock-based rate control mode" is supported |
| 49 | */ |
| 50 | #define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3)) |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 51 | #define AMDTP_FDF_NO_DATA 0xff |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 52 | #define AMDTP_DBS_MASK 0x00ff0000 |
| 53 | #define AMDTP_DBS_SHIFT 16 |
| 54 | #define AMDTP_DBC_MASK 0x000000ff |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 55 | |
| 56 | /* TODO: make these configurable */ |
| 57 | #define INTERRUPT_INTERVAL 16 |
| 58 | #define QUEUE_LENGTH 48 |
| 59 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 60 | #define IN_PACKET_HEADER_SIZE 4 |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 61 | #define OUT_PACKET_HEADER_SIZE 0 |
| 62 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 63 | static void pcm_period_tasklet(unsigned long data); |
| 64 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 65 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 66 | * amdtp_stream_init - initialize an AMDTP stream structure |
| 67 | * @s: the AMDTP stream to initialize |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 68 | * @unit: the target of the stream |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 69 | * @dir: the direction of stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 70 | * @flags: the packet transmission method to use |
| 71 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 72 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 73 | enum amdtp_stream_direction dir, enum cip_flags flags) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 74 | { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 75 | s->unit = fw_unit_get(unit); |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 76 | s->direction = dir; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 77 | s->flags = flags; |
| 78 | s->context = ERR_PTR(-1); |
| 79 | mutex_init(&s->mutex); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 80 | tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 81 | s->packet_index = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 82 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 83 | init_waitqueue_head(&s->callback_wait); |
| 84 | s->callbacked = false; |
| 85 | s->sync_slave = NULL; |
| 86 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 87 | return 0; |
| 88 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 89 | EXPORT_SYMBOL(amdtp_stream_init); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 90 | |
| 91 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 92 | * amdtp_stream_destroy - free stream resources |
| 93 | * @s: the AMDTP stream to destroy |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 94 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 95 | void amdtp_stream_destroy(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 96 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 97 | WARN_ON(amdtp_stream_running(s)); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 98 | mutex_destroy(&s->mutex); |
| 99 | fw_unit_put(s->unit); |
| 100 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 101 | EXPORT_SYMBOL(amdtp_stream_destroy); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 102 | |
Clemens Ladisch | c5280e9 | 2011-10-16 21:39:00 +0200 | [diff] [blame] | 103 | const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 104 | [CIP_SFC_32000] = 8, |
| 105 | [CIP_SFC_44100] = 8, |
| 106 | [CIP_SFC_48000] = 8, |
| 107 | [CIP_SFC_88200] = 16, |
| 108 | [CIP_SFC_96000] = 16, |
| 109 | [CIP_SFC_176400] = 32, |
| 110 | [CIP_SFC_192000] = 32, |
| 111 | }; |
| 112 | EXPORT_SYMBOL(amdtp_syt_intervals); |
| 113 | |
Takashi Sakamoto | f9503a6 | 2014-05-28 00:14:36 +0900 | [diff] [blame] | 114 | const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { |
Takashi Sakamoto | 1017abe | 2014-04-25 22:44:59 +0900 | [diff] [blame] | 115 | [CIP_SFC_32000] = 32000, |
| 116 | [CIP_SFC_44100] = 44100, |
| 117 | [CIP_SFC_48000] = 48000, |
| 118 | [CIP_SFC_88200] = 88200, |
| 119 | [CIP_SFC_96000] = 96000, |
| 120 | [CIP_SFC_176400] = 176400, |
| 121 | [CIP_SFC_192000] = 192000, |
| 122 | }; |
| 123 | EXPORT_SYMBOL(amdtp_rate_table); |
| 124 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 125 | /** |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 126 | * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream |
| 127 | * @s: the AMDTP stream, which must be initialized. |
| 128 | * @runtime: the PCM substream runtime |
| 129 | */ |
| 130 | int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, |
| 131 | struct snd_pcm_runtime *runtime) |
| 132 | { |
| 133 | int err; |
| 134 | |
| 135 | /* AM824 in IEC 61883-6 can deliver 24bit data */ |
| 136 | err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24); |
| 137 | if (err < 0) |
| 138 | goto end; |
| 139 | |
| 140 | /* |
| 141 | * Currently firewire-lib processes 16 packets in one software |
| 142 | * interrupt callback. This equals to 2msec but actually the |
| 143 | * interval of the interrupts has a jitter. |
| 144 | * Additionally, even if adding a constraint to fit period size to |
| 145 | * 2msec, actual calculated frames per period doesn't equal to 2msec, |
| 146 | * depending on sampling rate. |
| 147 | * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec. |
| 148 | * Here let us use 5msec for safe period interrupt. |
| 149 | */ |
| 150 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 151 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 152 | 5000, UINT_MAX); |
| 153 | if (err < 0) |
| 154 | goto end; |
| 155 | |
| 156 | /* Non-Blocking stream has no more constraints */ |
| 157 | if (!(s->flags & CIP_BLOCKING)) |
| 158 | goto end; |
| 159 | |
| 160 | /* |
| 161 | * One AMDTP packet can include some frames. In blocking mode, the |
| 162 | * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, |
| 163 | * depending on its sampling rate. For accurate period interrupt, it's |
| 164 | * preferrable to aligh period/buffer sizes to current SYT_INTERVAL. |
| 165 | * |
| 166 | * TODO: These constraints can be improved with propper rules. |
| 167 | * Currently apply LCM of SYT_INTEVALs. |
| 168 | */ |
| 169 | err = snd_pcm_hw_constraint_step(runtime, 0, |
| 170 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32); |
| 171 | if (err < 0) |
| 172 | goto end; |
| 173 | err = snd_pcm_hw_constraint_step(runtime, 0, |
| 174 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32); |
| 175 | end: |
| 176 | return err; |
| 177 | } |
| 178 | EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); |
| 179 | |
| 180 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 181 | * amdtp_stream_set_parameters - set stream parameters |
| 182 | * @s: the AMDTP stream to configure |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 183 | * @rate: the sample rate |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 184 | * @pcm_channels: the number of PCM samples in each data block, to be encoded |
| 185 | * as AM824 multi-bit linear audio |
| 186 | * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 187 | * |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 188 | * 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] | 189 | * changed while the stream is running. |
| 190 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 191 | void amdtp_stream_set_parameters(struct amdtp_stream *s, |
| 192 | unsigned int rate, |
| 193 | unsigned int pcm_channels, |
| 194 | unsigned int midi_ports) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 195 | { |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 196 | unsigned int i, sfc, midi_channels; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 197 | |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 198 | midi_channels = DIV_ROUND_UP(midi_ports, 8); |
| 199 | |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 200 | if (WARN_ON(amdtp_stream_running(s)) | |
| 201 | WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) | |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 202 | WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI)) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 203 | return; |
| 204 | |
Takashi Sakamoto | f9503a6 | 2014-05-28 00:14:36 +0900 | [diff] [blame] | 205 | for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) |
Takashi Sakamoto | 1017abe | 2014-04-25 22:44:59 +0900 | [diff] [blame] | 206 | if (amdtp_rate_table[sfc] == rate) |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 207 | goto sfc_found; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 208 | WARN_ON(1); |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 209 | return; |
| 210 | |
| 211 | sfc_found: |
Takashi Sakamoto | 10550be | 2014-04-25 22:44:51 +0900 | [diff] [blame] | 212 | s->pcm_channels = pcm_channels; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 213 | s->sfc = sfc; |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 214 | s->data_block_quadlets = s->pcm_channels + midi_channels; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 215 | s->midi_ports = midi_ports; |
| 216 | |
| 217 | s->syt_interval = amdtp_syt_intervals[sfc]; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 218 | |
| 219 | /* default buffering in the device */ |
| 220 | s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; |
| 221 | if (s->flags & CIP_BLOCKING) |
| 222 | /* additional buffering needed to adjust for no-data packets */ |
| 223 | s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 224 | |
| 225 | /* init the position map for PCM and MIDI channels */ |
| 226 | for (i = 0; i < pcm_channels; i++) |
| 227 | s->pcm_positions[i] = i; |
| 228 | s->midi_position = s->pcm_channels; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 229 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 230 | EXPORT_SYMBOL(amdtp_stream_set_parameters); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 231 | |
| 232 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 233 | * amdtp_stream_get_max_payload - get the stream's packet size |
| 234 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 235 | * |
| 236 | * This function must not be called before the stream has been configured |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 237 | * with amdtp_stream_set_parameters(). |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 238 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 239 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 240 | { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 241 | return 8 + s->syt_interval * s->data_block_quadlets * 4; |
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 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 245 | static void amdtp_write_s16(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 246 | struct snd_pcm_substream *pcm, |
| 247 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 248 | static void amdtp_write_s32(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 249 | struct snd_pcm_substream *pcm, |
| 250 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 251 | static void amdtp_read_s32(struct amdtp_stream *s, |
| 252 | struct snd_pcm_substream *pcm, |
| 253 | __be32 *buffer, unsigned int frames); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 254 | |
| 255 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 256 | * amdtp_stream_set_pcm_format - set the PCM format |
| 257 | * @s: the AMDTP stream to configure |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 258 | * @format: the format of the ALSA PCM device |
| 259 | * |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 260 | * The sample format must be set after the other paramters (rate/PCM channels/ |
| 261 | * MIDI) and before the stream is started, and must not be changed while the |
| 262 | * stream is running. |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 263 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 264 | void amdtp_stream_set_pcm_format(struct amdtp_stream *s, |
| 265 | snd_pcm_format_t format) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 266 | { |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 267 | if (WARN_ON(amdtp_stream_pcm_running(s))) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 268 | return; |
| 269 | |
| 270 | switch (format) { |
| 271 | default: |
| 272 | WARN_ON(1); |
| 273 | /* fall through */ |
| 274 | case SNDRV_PCM_FORMAT_S16: |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 275 | if (s->direction == AMDTP_OUT_STREAM) { |
Takashi Sakamoto | 10550be | 2014-04-25 22:44:51 +0900 | [diff] [blame] | 276 | s->transfer_samples = amdtp_write_s16; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | WARN_ON(1); |
| 280 | /* fall through */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 281 | case SNDRV_PCM_FORMAT_S32: |
Takashi Sakamoto | 10550be | 2014-04-25 22:44:51 +0900 | [diff] [blame] | 282 | if (s->direction == AMDTP_OUT_STREAM) |
| 283 | s->transfer_samples = amdtp_write_s32; |
| 284 | else |
| 285 | s->transfer_samples = amdtp_read_s32; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 286 | break; |
| 287 | } |
| 288 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 289 | EXPORT_SYMBOL(amdtp_stream_set_pcm_format); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 290 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 291 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 292 | * amdtp_stream_pcm_prepare - prepare PCM device for running |
| 293 | * @s: the AMDTP stream |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 294 | * |
| 295 | * This function should be called from the PCM device's .prepare callback. |
| 296 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 297 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s) |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 298 | { |
| 299 | tasklet_kill(&s->period_tasklet); |
| 300 | s->pcm_buffer_pointer = 0; |
| 301 | s->pcm_period_pointer = 0; |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 302 | s->pointer_flush = true; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 303 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 304 | EXPORT_SYMBOL(amdtp_stream_pcm_prepare); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 305 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 306 | static unsigned int calculate_data_blocks(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 307 | { |
| 308 | unsigned int phase, data_blocks; |
| 309 | |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 310 | if (s->flags & CIP_BLOCKING) |
| 311 | data_blocks = s->syt_interval; |
| 312 | else if (!cip_sfc_is_base_44100(s->sfc)) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 313 | /* Sample_rate / 8000 is an integer, and precomputed. */ |
| 314 | data_blocks = s->data_block_state; |
| 315 | } else { |
| 316 | phase = s->data_block_state; |
| 317 | |
| 318 | /* |
| 319 | * This calculates the number of data blocks per packet so that |
| 320 | * 1) the overall rate is correct and exactly synchronized to |
| 321 | * the bus clock, and |
| 322 | * 2) packets with a rounded-up number of blocks occur as early |
| 323 | * as possible in the sequence (to prevent underruns of the |
| 324 | * device's buffer). |
| 325 | */ |
| 326 | if (s->sfc == CIP_SFC_44100) |
| 327 | /* 6 6 5 6 5 6 5 ... */ |
| 328 | data_blocks = 5 + ((phase & 1) ^ |
| 329 | (phase == 0 || phase >= 40)); |
| 330 | else |
| 331 | /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ |
| 332 | data_blocks = 11 * (s->sfc >> 1) + (phase == 0); |
| 333 | if (++phase >= (80 >> (s->sfc >> 1))) |
| 334 | phase = 0; |
| 335 | s->data_block_state = phase; |
| 336 | } |
| 337 | |
| 338 | return data_blocks; |
| 339 | } |
| 340 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 341 | static unsigned int calculate_syt(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 342 | unsigned int cycle) |
| 343 | { |
| 344 | unsigned int syt_offset, phase, index, syt; |
| 345 | |
| 346 | if (s->last_syt_offset < TICKS_PER_CYCLE) { |
| 347 | if (!cip_sfc_is_base_44100(s->sfc)) |
| 348 | syt_offset = s->last_syt_offset + s->syt_offset_state; |
| 349 | else { |
| 350 | /* |
| 351 | * The time, in ticks, of the n'th SYT_INTERVAL sample is: |
| 352 | * n * SYT_INTERVAL * 24576000 / sample_rate |
| 353 | * Modulo TICKS_PER_CYCLE, the difference between successive |
| 354 | * elements is about 1386.23. Rounding the results of this |
| 355 | * formula to the SYT precision results in a sequence of |
| 356 | * differences that begins with: |
| 357 | * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... |
| 358 | * This code generates _exactly_ the same sequence. |
| 359 | */ |
| 360 | phase = s->syt_offset_state; |
| 361 | index = phase % 13; |
| 362 | syt_offset = s->last_syt_offset; |
| 363 | syt_offset += 1386 + ((index && !(index & 3)) || |
| 364 | phase == 146); |
| 365 | if (++phase >= 147) |
| 366 | phase = 0; |
| 367 | s->syt_offset_state = phase; |
| 368 | } |
| 369 | } else |
| 370 | syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; |
| 371 | s->last_syt_offset = syt_offset; |
| 372 | |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 373 | if (syt_offset < TICKS_PER_CYCLE) { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 374 | syt_offset += s->transfer_delay; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 375 | syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; |
| 376 | syt += syt_offset % TICKS_PER_CYCLE; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 377 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 378 | return syt & CIP_SYT_MASK; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 379 | } else { |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 380 | return CIP_SYT_NO_INFO; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 381 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 384 | static void amdtp_write_s32(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 385 | struct snd_pcm_substream *pcm, |
| 386 | __be32 *buffer, unsigned int frames) |
| 387 | { |
| 388 | struct snd_pcm_runtime *runtime = pcm->runtime; |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 389 | unsigned int channels, remaining_frames, i, c; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 390 | const u32 *src; |
| 391 | |
| 392 | channels = s->pcm_channels; |
| 393 | src = (void *)runtime->dma_area + |
Takashi Sakamoto | e84841f | 2013-09-14 00:35:47 +0900 | [diff] [blame] | 394 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 395 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 396 | |
| 397 | for (i = 0; i < frames; ++i) { |
| 398 | for (c = 0; c < channels; ++c) { |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 399 | buffer[s->pcm_positions[c]] = |
| 400 | cpu_to_be32((*src >> 8) | 0x40000000); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 401 | src++; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 402 | } |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 403 | buffer += s->data_block_quadlets; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 404 | if (--remaining_frames == 0) |
| 405 | src = (void *)runtime->dma_area; |
| 406 | } |
| 407 | } |
| 408 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 409 | static void amdtp_write_s16(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 410 | struct snd_pcm_substream *pcm, |
| 411 | __be32 *buffer, unsigned int frames) |
| 412 | { |
| 413 | struct snd_pcm_runtime *runtime = pcm->runtime; |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 414 | unsigned int channels, remaining_frames, i, c; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 415 | const u16 *src; |
| 416 | |
| 417 | channels = s->pcm_channels; |
| 418 | src = (void *)runtime->dma_area + |
Takashi Sakamoto | e84841f | 2013-09-14 00:35:47 +0900 | [diff] [blame] | 419 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 420 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 421 | |
| 422 | for (i = 0; i < frames; ++i) { |
| 423 | for (c = 0; c < channels; ++c) { |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 424 | buffer[s->pcm_positions[c]] = |
Takashi Sakamoto | a6975f2 | 2014-06-02 01:50:16 +0900 | [diff] [blame] | 425 | cpu_to_be32((*src << 8) | 0x42000000); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 426 | src++; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 427 | } |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 428 | buffer += s->data_block_quadlets; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 429 | if (--remaining_frames == 0) |
| 430 | src = (void *)runtime->dma_area; |
| 431 | } |
| 432 | } |
| 433 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 434 | static void amdtp_read_s32(struct amdtp_stream *s, |
| 435 | struct snd_pcm_substream *pcm, |
| 436 | __be32 *buffer, unsigned int frames) |
| 437 | { |
| 438 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 439 | unsigned int channels, remaining_frames, i, c; |
| 440 | u32 *dst; |
| 441 | |
| 442 | channels = s->pcm_channels; |
| 443 | dst = (void *)runtime->dma_area + |
| 444 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
| 445 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
| 446 | |
| 447 | for (i = 0; i < frames; ++i) { |
| 448 | for (c = 0; c < channels; ++c) { |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 449 | *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 450 | dst++; |
| 451 | } |
| 452 | buffer += s->data_block_quadlets; |
| 453 | if (--remaining_frames == 0) |
| 454 | dst = (void *)runtime->dma_area; |
| 455 | } |
| 456 | } |
| 457 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 458 | static void amdtp_fill_pcm_silence(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 459 | __be32 *buffer, unsigned int frames) |
| 460 | { |
| 461 | unsigned int i, c; |
| 462 | |
| 463 | for (i = 0; i < frames; ++i) { |
| 464 | for (c = 0; c < s->pcm_channels; ++c) |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 465 | buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 466 | buffer += s->data_block_quadlets; |
| 467 | } |
| 468 | } |
| 469 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 470 | static void amdtp_fill_midi(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 471 | __be32 *buffer, unsigned int frames) |
| 472 | { |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 473 | unsigned int f, port; |
| 474 | u8 *b; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 475 | |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 476 | for (f = 0; f < frames; f++) { |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 477 | buffer[s->midi_position] = 0; |
| 478 | b = (u8 *)&buffer[s->midi_position]; |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 479 | |
| 480 | port = (s->data_block_counter + f) % 8; |
Clemens Ladisch | 5c697e5 | 2014-11-25 22:52:24 +0100 | [diff] [blame^] | 481 | if ((f >= MAX_MIDI_RX_BLOCKS) || |
Takashi Sakamoto | 53111cd | 2014-04-25 22:45:10 +0900 | [diff] [blame] | 482 | (s->midi[port] == NULL) || |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 483 | (snd_rawmidi_transmit(s->midi[port], b + 1, 1) <= 0)) |
| 484 | b[0] = 0x80; |
| 485 | else |
| 486 | b[0] = 0x81; |
| 487 | |
| 488 | buffer += s->data_block_quadlets; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static void amdtp_pull_midi(struct amdtp_stream *s, |
| 493 | __be32 *buffer, unsigned int frames) |
| 494 | { |
| 495 | unsigned int f, port; |
| 496 | int len; |
| 497 | u8 *b; |
| 498 | |
| 499 | for (f = 0; f < frames; f++) { |
| 500 | port = (s->data_block_counter + f) % 8; |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 501 | b = (u8 *)&buffer[s->midi_position]; |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 502 | |
| 503 | len = b[0] - 0x80; |
| 504 | if ((1 <= len) && (len <= 3) && (s->midi[port])) |
| 505 | snd_rawmidi_receive(s->midi[port], b + 1, len); |
| 506 | |
| 507 | buffer += s->data_block_quadlets; |
| 508 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 511 | static void update_pcm_pointers(struct amdtp_stream *s, |
| 512 | struct snd_pcm_substream *pcm, |
| 513 | unsigned int frames) |
Takashi Sakamoto | 65845f2 | 2014-08-29 13:40:45 +0900 | [diff] [blame] | 514 | { |
| 515 | unsigned int ptr; |
| 516 | |
| 517 | /* |
| 518 | * In IEC 61883-6, one data block represents one event. In ALSA, one |
| 519 | * event equals to one PCM frame. But Dice has a quirk to transfer |
| 520 | * two PCM frames in one data block. |
| 521 | */ |
| 522 | if (s->double_pcm_frames) |
| 523 | frames *= 2; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 524 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 525 | ptr = s->pcm_buffer_pointer + frames; |
| 526 | if (ptr >= pcm->runtime->buffer_size) |
| 527 | ptr -= pcm->runtime->buffer_size; |
| 528 | ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; |
| 529 | |
| 530 | s->pcm_period_pointer += frames; |
| 531 | if (s->pcm_period_pointer >= pcm->runtime->period_size) { |
| 532 | s->pcm_period_pointer -= pcm->runtime->period_size; |
| 533 | s->pointer_flush = false; |
| 534 | tasklet_hi_schedule(&s->period_tasklet); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | static void pcm_period_tasklet(unsigned long data) |
| 539 | { |
| 540 | struct amdtp_stream *s = (void *)data; |
| 541 | struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); |
| 542 | |
| 543 | if (pcm) |
| 544 | snd_pcm_period_elapsed(pcm); |
| 545 | } |
| 546 | |
| 547 | static int queue_packet(struct amdtp_stream *s, |
| 548 | unsigned int header_length, |
| 549 | unsigned int payload_length, bool skip) |
| 550 | { |
| 551 | struct fw_iso_packet p = {0}; |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 552 | int err = 0; |
| 553 | |
| 554 | if (IS_ERR(s->context)) |
| 555 | goto end; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 556 | |
| 557 | p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL); |
| 558 | p.tag = TAG_CIP; |
| 559 | p.header_length = header_length; |
| 560 | p.payload_length = (!skip) ? payload_length : 0; |
| 561 | p.skip = skip; |
| 562 | err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer, |
| 563 | s->buffer.packets[s->packet_index].offset); |
| 564 | if (err < 0) { |
| 565 | dev_err(&s->unit->device, "queueing error: %d\n", err); |
| 566 | goto end; |
| 567 | } |
| 568 | |
| 569 | if (++s->packet_index >= QUEUE_LENGTH) |
| 570 | s->packet_index = 0; |
| 571 | end: |
| 572 | return err; |
| 573 | } |
| 574 | |
| 575 | static inline int queue_out_packet(struct amdtp_stream *s, |
| 576 | unsigned int payload_length, bool skip) |
| 577 | { |
| 578 | return queue_packet(s, OUT_PACKET_HEADER_SIZE, |
| 579 | payload_length, skip); |
| 580 | } |
| 581 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 582 | static inline int queue_in_packet(struct amdtp_stream *s) |
| 583 | { |
| 584 | return queue_packet(s, IN_PACKET_HEADER_SIZE, |
| 585 | amdtp_stream_get_max_payload(s), false); |
| 586 | } |
| 587 | |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 588 | static void handle_out_packet(struct amdtp_stream *s, unsigned int syt) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 589 | { |
| 590 | __be32 *buffer; |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 591 | unsigned int data_blocks, payload_length; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 592 | struct snd_pcm_substream *pcm; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 593 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 594 | if (s->packet_index < 0) |
| 595 | return; |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 596 | |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 597 | /* this module generate empty packet for 'no data' */ |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 598 | if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO)) |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 599 | data_blocks = calculate_data_blocks(s); |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 600 | else |
| 601 | data_blocks = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 602 | |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 603 | buffer = s->buffer.packets[s->packet_index].buffer; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 604 | buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 605 | (s->data_block_quadlets << AMDTP_DBS_SHIFT) | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 606 | s->data_block_counter); |
| 607 | buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 608 | (s->sfc << CIP_FDF_SFC_SHIFT) | syt); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 609 | buffer += 2; |
| 610 | |
| 611 | pcm = ACCESS_ONCE(s->pcm); |
| 612 | if (pcm) |
| 613 | s->transfer_samples(s, pcm, buffer, data_blocks); |
| 614 | else |
| 615 | amdtp_fill_pcm_silence(s, buffer, data_blocks); |
| 616 | if (s->midi_ports) |
| 617 | amdtp_fill_midi(s, buffer, data_blocks); |
| 618 | |
| 619 | s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; |
| 620 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 621 | payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; |
| 622 | if (queue_out_packet(s, payload_length, false) < 0) { |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 623 | s->packet_index = -1; |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 624 | amdtp_stream_pcm_abort(s); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 625 | return; |
| 626 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 627 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 628 | if (pcm) |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 629 | update_pcm_pointers(s, pcm, data_blocks); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 632 | static void handle_in_packet(struct amdtp_stream *s, |
| 633 | unsigned int payload_quadlets, |
| 634 | __be32 *buffer) |
| 635 | { |
| 636 | u32 cip_header[2]; |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 637 | unsigned int data_blocks, data_block_quadlets, data_block_counter, |
| 638 | dbc_interval; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 639 | struct snd_pcm_substream *pcm = NULL; |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 640 | bool lost; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 641 | |
| 642 | cip_header[0] = be32_to_cpu(buffer[0]); |
| 643 | cip_header[1] = be32_to_cpu(buffer[1]); |
| 644 | |
| 645 | /* |
| 646 | * This module supports 'Two-quadlet CIP header with SYT field'. |
Takashi Sakamoto | 77d2a8a | 2014-04-25 22:44:50 +0900 | [diff] [blame] | 647 | * For convenience, also check FMT field is AM824 or not. |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 648 | */ |
| 649 | if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || |
| 650 | ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) || |
| 651 | ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) { |
| 652 | dev_info_ratelimited(&s->unit->device, |
| 653 | "Invalid CIP header for AMDTP: %08X:%08X\n", |
| 654 | cip_header[0], cip_header[1]); |
| 655 | goto end; |
| 656 | } |
| 657 | |
| 658 | /* Calculate data blocks */ |
| 659 | if (payload_quadlets < 3 || |
| 660 | ((cip_header[1] & CIP_FDF_MASK) == |
| 661 | (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) { |
| 662 | data_blocks = 0; |
| 663 | } else { |
| 664 | data_block_quadlets = |
| 665 | (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT; |
| 666 | /* avoid division by zero */ |
| 667 | if (data_block_quadlets == 0) { |
| 668 | dev_info_ratelimited(&s->unit->device, |
| 669 | "Detect invalid value in dbs field: %08X\n", |
| 670 | cip_header[0]); |
| 671 | goto err; |
| 672 | } |
Takashi Sakamoto | 6970223 | 2014-04-25 22:45:05 +0900 | [diff] [blame] | 673 | if (s->flags & CIP_WRONG_DBS) |
| 674 | data_block_quadlets = s->data_block_quadlets; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 675 | |
| 676 | data_blocks = (payload_quadlets - 2) / data_block_quadlets; |
| 677 | } |
| 678 | |
| 679 | /* Check data block counter continuity */ |
| 680 | data_block_counter = cip_header[0] & AMDTP_DBC_MASK; |
Takashi Sakamoto | 9d59124 | 2014-04-25 22:45:27 +0900 | [diff] [blame] | 681 | if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && |
| 682 | s->data_block_counter != UINT_MAX) |
| 683 | data_block_counter = s->data_block_counter; |
| 684 | |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 685 | if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) || |
| 686 | (s->data_block_counter == UINT_MAX)) { |
Takashi Sakamoto | b84b1a2 | 2014-04-25 22:45:07 +0900 | [diff] [blame] | 687 | lost = false; |
| 688 | } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 689 | lost = data_block_counter != s->data_block_counter; |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 690 | } else { |
| 691 | if ((data_blocks > 0) && (s->tx_dbc_interval > 0)) |
| 692 | dbc_interval = s->tx_dbc_interval; |
| 693 | else |
| 694 | dbc_interval = data_blocks; |
| 695 | |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 696 | lost = data_block_counter != |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 697 | ((s->data_block_counter + dbc_interval) & 0xff); |
| 698 | } |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 699 | |
| 700 | if (lost) { |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 701 | dev_info(&s->unit->device, |
| 702 | "Detect discontinuity of CIP: %02X %02X\n", |
| 703 | s->data_block_counter, data_block_counter); |
| 704 | goto err; |
| 705 | } |
| 706 | |
| 707 | if (data_blocks > 0) { |
| 708 | buffer += 2; |
| 709 | |
| 710 | pcm = ACCESS_ONCE(s->pcm); |
| 711 | if (pcm) |
| 712 | s->transfer_samples(s, pcm, buffer, data_blocks); |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame] | 713 | |
| 714 | if (s->midi_ports) |
| 715 | amdtp_pull_midi(s, buffer, data_blocks); |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 716 | } |
| 717 | |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 718 | if (s->flags & CIP_DBC_IS_END_EVENT) |
| 719 | s->data_block_counter = data_block_counter; |
| 720 | else |
| 721 | s->data_block_counter = |
| 722 | (data_block_counter + data_blocks) & 0xff; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 723 | end: |
| 724 | if (queue_in_packet(s) < 0) |
| 725 | goto err; |
| 726 | |
| 727 | if (pcm) |
| 728 | update_pcm_pointers(s, pcm, data_blocks); |
| 729 | |
| 730 | return; |
| 731 | err: |
| 732 | s->packet_index = -1; |
| 733 | amdtp_stream_pcm_abort(s); |
| 734 | } |
| 735 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 736 | static void out_stream_callback(struct fw_iso_context *context, u32 cycle, |
| 737 | size_t header_length, void *header, |
| 738 | void *private_data) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 739 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 740 | struct amdtp_stream *s = private_data; |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 741 | unsigned int i, syt, packets = header_length / 4; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 742 | |
| 743 | /* |
| 744 | * Compute the cycle of the last queued packet. |
| 745 | * (We need only the four lowest bits for the SYT, so we can ignore |
| 746 | * that bits 0-11 must wrap around at 3072.) |
| 747 | */ |
| 748 | cycle += QUEUE_LENGTH - packets; |
| 749 | |
Takashi Sakamoto | ccccad8 | 2014-04-25 22:44:48 +0900 | [diff] [blame] | 750 | for (i = 0; i < packets; ++i) { |
| 751 | syt = calculate_syt(s, ++cycle); |
| 752 | handle_out_packet(s, syt); |
| 753 | } |
Clemens Ladisch | 13882a8 | 2011-05-02 09:33:56 +0200 | [diff] [blame] | 754 | fw_iso_context_queue_flush(s->context); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 755 | } |
| 756 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 757 | static void in_stream_callback(struct fw_iso_context *context, u32 cycle, |
| 758 | size_t header_length, void *header, |
| 759 | void *private_data) |
| 760 | { |
| 761 | struct amdtp_stream *s = private_data; |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 762 | unsigned int p, syt, packets, payload_quadlets; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 763 | __be32 *buffer, *headers = header; |
| 764 | |
| 765 | /* The number of packets in buffer */ |
| 766 | packets = header_length / IN_PACKET_HEADER_SIZE; |
| 767 | |
| 768 | for (p = 0; p < packets; p++) { |
| 769 | if (s->packet_index < 0) |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 770 | break; |
| 771 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 772 | buffer = s->buffer.packets[s->packet_index].buffer; |
| 773 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 774 | /* Process sync slave stream */ |
| 775 | if (s->sync_slave && s->sync_slave->callbacked) { |
| 776 | syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK; |
| 777 | handle_out_packet(s->sync_slave, syt); |
| 778 | } |
| 779 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 780 | /* The number of quadlets in this packet */ |
| 781 | payload_quadlets = |
| 782 | (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4; |
| 783 | handle_in_packet(s, payload_quadlets, buffer); |
| 784 | } |
| 785 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 786 | /* Queueing error or detecting discontinuity */ |
| 787 | if (s->packet_index < 0) { |
| 788 | /* Abort sync slave. */ |
| 789 | if (s->sync_slave) { |
| 790 | s->sync_slave->packet_index = -1; |
| 791 | amdtp_stream_pcm_abort(s->sync_slave); |
| 792 | } |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | /* when sync to device, flush the packets for slave stream */ |
| 797 | if (s->sync_slave && s->sync_slave->callbacked) |
| 798 | fw_iso_context_queue_flush(s->sync_slave->context); |
| 799 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 800 | fw_iso_context_queue_flush(s->context); |
| 801 | } |
| 802 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 803 | /* processing is done by master callback */ |
| 804 | static void slave_stream_callback(struct fw_iso_context *context, u32 cycle, |
| 805 | size_t header_length, void *header, |
| 806 | void *private_data) |
| 807 | { |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | /* this is executed one time */ |
| 812 | static void amdtp_stream_first_callback(struct fw_iso_context *context, |
| 813 | u32 cycle, size_t header_length, |
| 814 | void *header, void *private_data) |
| 815 | { |
| 816 | struct amdtp_stream *s = private_data; |
| 817 | |
| 818 | /* |
| 819 | * For in-stream, first packet has come. |
| 820 | * For out-stream, prepared to transmit first packet |
| 821 | */ |
| 822 | s->callbacked = true; |
| 823 | wake_up(&s->callback_wait); |
| 824 | |
| 825 | if (s->direction == AMDTP_IN_STREAM) |
| 826 | context->callback.sc = in_stream_callback; |
| 827 | else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE)) |
| 828 | context->callback.sc = slave_stream_callback; |
| 829 | else |
| 830 | context->callback.sc = out_stream_callback; |
| 831 | |
| 832 | context->callback.sc(context, cycle, header_length, header, s); |
| 833 | } |
| 834 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 835 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 836 | * amdtp_stream_start - start transferring packets |
| 837 | * @s: the AMDTP stream to start |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 838 | * @channel: the isochronous channel on the bus |
| 839 | * @speed: firewire speed code |
| 840 | * |
| 841 | * The stream cannot be started until it has been configured with |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 842 | * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI |
| 843 | * device can be started. |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 844 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 845 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 846 | { |
| 847 | static const struct { |
| 848 | unsigned int data_block; |
| 849 | unsigned int syt_offset; |
| 850 | } initial_state[] = { |
| 851 | [CIP_SFC_32000] = { 4, 3072 }, |
| 852 | [CIP_SFC_48000] = { 6, 1024 }, |
| 853 | [CIP_SFC_96000] = { 12, 1024 }, |
| 854 | [CIP_SFC_192000] = { 24, 1024 }, |
| 855 | [CIP_SFC_44100] = { 0, 67 }, |
| 856 | [CIP_SFC_88200] = { 0, 67 }, |
| 857 | [CIP_SFC_176400] = { 0, 67 }, |
| 858 | }; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 859 | unsigned int header_size; |
| 860 | enum dma_data_direction dir; |
Takashi Sakamoto | 7ab5664 | 2014-04-25 22:45:03 +0900 | [diff] [blame] | 861 | int type, tag, err; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 862 | |
| 863 | mutex_lock(&s->mutex); |
| 864 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 865 | if (WARN_ON(amdtp_stream_running(s) || |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 866 | (s->data_block_quadlets < 1))) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 867 | err = -EBADFD; |
| 868 | goto err_unlock; |
| 869 | } |
| 870 | |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 871 | if (s->direction == AMDTP_IN_STREAM && |
| 872 | s->flags & CIP_SKIP_INIT_DBC_CHECK) |
| 873 | s->data_block_counter = UINT_MAX; |
| 874 | else |
| 875 | s->data_block_counter = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 876 | s->data_block_state = initial_state[s->sfc].data_block; |
| 877 | s->syt_offset_state = initial_state[s->sfc].syt_offset; |
| 878 | s->last_syt_offset = TICKS_PER_CYCLE; |
| 879 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 880 | /* initialize packet buffer */ |
| 881 | if (s->direction == AMDTP_IN_STREAM) { |
| 882 | dir = DMA_FROM_DEVICE; |
| 883 | type = FW_ISO_CONTEXT_RECEIVE; |
| 884 | header_size = IN_PACKET_HEADER_SIZE; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 885 | } else { |
| 886 | dir = DMA_TO_DEVICE; |
| 887 | type = FW_ISO_CONTEXT_TRANSMIT; |
| 888 | header_size = OUT_PACKET_HEADER_SIZE; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 889 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 890 | err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 891 | amdtp_stream_get_max_payload(s), dir); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 892 | if (err < 0) |
| 893 | goto err_unlock; |
| 894 | |
| 895 | s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 896 | type, channel, speed, header_size, |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 897 | amdtp_stream_first_callback, s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 898 | if (IS_ERR(s->context)) { |
| 899 | err = PTR_ERR(s->context); |
| 900 | if (err == -EBUSY) |
| 901 | dev_err(&s->unit->device, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 902 | "no free stream on this controller\n"); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 903 | goto err_buffer; |
| 904 | } |
| 905 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 906 | amdtp_stream_update(s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 907 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 908 | s->packet_index = 0; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 909 | do { |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 910 | if (s->direction == AMDTP_IN_STREAM) |
| 911 | err = queue_in_packet(s); |
| 912 | else |
| 913 | err = queue_out_packet(s, 0, true); |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 914 | if (err < 0) |
| 915 | goto err_context; |
| 916 | } while (s->packet_index > 0); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 917 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 918 | /* NOTE: TAG1 matches CIP. This just affects in stream. */ |
Takashi Sakamoto | 7ab5664 | 2014-04-25 22:45:03 +0900 | [diff] [blame] | 919 | tag = FW_ISO_CONTEXT_MATCH_TAG1; |
| 920 | if (s->flags & CIP_EMPTY_WITH_TAG0) |
| 921 | tag |= FW_ISO_CONTEXT_MATCH_TAG0; |
| 922 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 923 | s->callbacked = false; |
Takashi Sakamoto | 7ab5664 | 2014-04-25 22:45:03 +0900 | [diff] [blame] | 924 | err = fw_iso_context_start(s->context, -1, 0, tag); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 925 | if (err < 0) |
| 926 | goto err_context; |
| 927 | |
| 928 | mutex_unlock(&s->mutex); |
| 929 | |
| 930 | return 0; |
| 931 | |
| 932 | err_context: |
| 933 | fw_iso_context_destroy(s->context); |
| 934 | s->context = ERR_PTR(-1); |
| 935 | err_buffer: |
| 936 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 937 | err_unlock: |
| 938 | mutex_unlock(&s->mutex); |
| 939 | |
| 940 | return err; |
| 941 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 942 | EXPORT_SYMBOL(amdtp_stream_start); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 943 | |
| 944 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 945 | * amdtp_stream_pcm_pointer - get the PCM buffer position |
| 946 | * @s: the AMDTP stream that transports the PCM data |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 947 | * |
| 948 | * Returns the current buffer position, in frames. |
| 949 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 950 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 951 | { |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 952 | /* this optimization is allowed to be racy */ |
Takashi Sakamoto | c8de6db | 2014-04-25 22:44:53 +0900 | [diff] [blame] | 953 | if (s->pointer_flush && amdtp_stream_running(s)) |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 954 | fw_iso_context_flush_completions(s->context); |
| 955 | else |
| 956 | s->pointer_flush = true; |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 957 | |
| 958 | return ACCESS_ONCE(s->pcm_buffer_pointer); |
| 959 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 960 | EXPORT_SYMBOL(amdtp_stream_pcm_pointer); |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 961 | |
| 962 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 963 | * amdtp_stream_update - update the stream after a bus reset |
| 964 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 965 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 966 | void amdtp_stream_update(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 967 | { |
| 968 | ACCESS_ONCE(s->source_node_id_field) = |
| 969 | (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24; |
| 970 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 971 | EXPORT_SYMBOL(amdtp_stream_update); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 972 | |
| 973 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 974 | * amdtp_stream_stop - stop sending packets |
| 975 | * @s: the AMDTP stream to stop |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 976 | * |
| 977 | * All PCM and MIDI devices of the stream must be stopped before the stream |
| 978 | * itself can be stopped. |
| 979 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 980 | void amdtp_stream_stop(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 981 | { |
| 982 | mutex_lock(&s->mutex); |
| 983 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 984 | if (!amdtp_stream_running(s)) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 985 | mutex_unlock(&s->mutex); |
| 986 | return; |
| 987 | } |
| 988 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 989 | tasklet_kill(&s->period_tasklet); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 990 | fw_iso_context_stop(s->context); |
| 991 | fw_iso_context_destroy(s->context); |
| 992 | s->context = ERR_PTR(-1); |
| 993 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 994 | |
Takashi Sakamoto | 7b3b0d8 | 2014-04-25 22:44:49 +0900 | [diff] [blame] | 995 | s->callbacked = false; |
| 996 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 997 | mutex_unlock(&s->mutex); |
| 998 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 999 | EXPORT_SYMBOL(amdtp_stream_stop); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 1000 | |
| 1001 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 1002 | * amdtp_stream_pcm_abort - abort the running PCM device |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 1003 | * @s: the AMDTP stream about to be stopped |
| 1004 | * |
| 1005 | * If the isochronous stream needs to be stopped asynchronously, call this |
| 1006 | * function first to stop the PCM device. |
| 1007 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 1008 | void amdtp_stream_pcm_abort(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 1009 | { |
| 1010 | struct snd_pcm_substream *pcm; |
| 1011 | |
| 1012 | pcm = ACCESS_ONCE(s->pcm); |
Takashi Iwai | 1fb8510 | 2014-11-07 17:08:28 +0100 | [diff] [blame] | 1013 | if (pcm) |
| 1014 | snd_pcm_stop_xrun(pcm); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 1015 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 1016 | EXPORT_SYMBOL(amdtp_stream_pcm_abort); |