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> |
| 15 | #include "amdtp.h" |
| 16 | |
| 17 | #define TICKS_PER_CYCLE 3072 |
| 18 | #define CYCLES_PER_SECOND 8000 |
| 19 | #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) |
| 20 | |
| 21 | #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */ |
| 22 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 23 | /* isochronous header parameters */ |
| 24 | #define ISO_DATA_LENGTH_SHIFT 16 |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 25 | #define TAG_CIP 1 |
| 26 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 27 | /* common isochronous packet header parameters */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 28 | #define CIP_EOH (1u << 31) |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 29 | #define CIP_EOH_MASK 0x80000000 |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 30 | #define CIP_FMT_AM (0x10 << 24) |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 31 | #define CIP_FMT_MASK 0x3f000000 |
| 32 | #define CIP_SYT_MASK 0x0000ffff |
| 33 | #define CIP_SYT_NO_INFO 0xffff |
| 34 | #define CIP_FDF_MASK 0x00ff0000 |
| 35 | #define CIP_FDF_SFC_SHIFT 16 |
| 36 | |
| 37 | /* |
| 38 | * Audio and Music transfer protocol specific parameters |
| 39 | * only "Clock-based rate control mode" is supported |
| 40 | */ |
| 41 | #define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3)) |
| 42 | #define AMDTP_DBS_MASK 0x00ff0000 |
| 43 | #define AMDTP_DBS_SHIFT 16 |
| 44 | #define AMDTP_DBC_MASK 0x000000ff |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 45 | |
| 46 | /* TODO: make these configurable */ |
| 47 | #define INTERRUPT_INTERVAL 16 |
| 48 | #define QUEUE_LENGTH 48 |
| 49 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 50 | static void pcm_period_tasklet(unsigned long data); |
| 51 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 52 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 53 | * amdtp_stream_init - initialize an AMDTP stream structure |
| 54 | * @s: the AMDTP stream to initialize |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 55 | * @unit: the target of the stream |
| 56 | * @flags: the packet transmission method to use |
| 57 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 58 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
| 59 | enum cip_flags flags) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 60 | { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 61 | s->unit = fw_unit_get(unit); |
| 62 | s->flags = flags; |
| 63 | s->context = ERR_PTR(-1); |
| 64 | mutex_init(&s->mutex); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 65 | tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 66 | s->packet_index = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 70 | EXPORT_SYMBOL(amdtp_stream_init); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 73 | * amdtp_stream_destroy - free stream resources |
| 74 | * @s: the AMDTP stream to destroy |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 75 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 76 | void amdtp_stream_destroy(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 77 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 78 | WARN_ON(amdtp_stream_running(s)); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 79 | mutex_destroy(&s->mutex); |
| 80 | fw_unit_put(s->unit); |
| 81 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 82 | EXPORT_SYMBOL(amdtp_stream_destroy); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 83 | |
Clemens Ladisch | c5280e9 | 2011-10-16 21:39:00 +0200 | [diff] [blame] | 84 | const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 85 | [CIP_SFC_32000] = 8, |
| 86 | [CIP_SFC_44100] = 8, |
| 87 | [CIP_SFC_48000] = 8, |
| 88 | [CIP_SFC_88200] = 16, |
| 89 | [CIP_SFC_96000] = 16, |
| 90 | [CIP_SFC_176400] = 32, |
| 91 | [CIP_SFC_192000] = 32, |
| 92 | }; |
| 93 | EXPORT_SYMBOL(amdtp_syt_intervals); |
| 94 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 95 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 96 | * amdtp_stream_set_parameters - set stream parameters |
| 97 | * @s: the AMDTP stream to configure |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 98 | * @rate: the sample rate |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 99 | * @pcm_channels: the number of PCM samples in each data block, to be encoded |
| 100 | * as AM824 multi-bit linear audio |
| 101 | * @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] | 102 | * |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 103 | * 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] | 104 | * changed while the stream is running. |
| 105 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 106 | void amdtp_stream_set_parameters(struct amdtp_stream *s, |
| 107 | unsigned int rate, |
| 108 | unsigned int pcm_channels, |
| 109 | unsigned int midi_ports) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 110 | { |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 111 | static const unsigned int rates[] = { |
| 112 | [CIP_SFC_32000] = 32000, |
| 113 | [CIP_SFC_44100] = 44100, |
| 114 | [CIP_SFC_48000] = 48000, |
| 115 | [CIP_SFC_88200] = 88200, |
| 116 | [CIP_SFC_96000] = 96000, |
| 117 | [CIP_SFC_176400] = 176400, |
| 118 | [CIP_SFC_192000] = 192000, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 119 | }; |
| 120 | unsigned int sfc; |
| 121 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 122 | if (WARN_ON(amdtp_stream_running(s))) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 123 | return; |
| 124 | |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 125 | for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc) |
| 126 | if (rates[sfc] == rate) |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 127 | goto sfc_found; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 128 | WARN_ON(1); |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 129 | return; |
| 130 | |
| 131 | sfc_found: |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 132 | s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000; |
| 133 | if (s->dual_wire) { |
| 134 | sfc -= 2; |
| 135 | rate /= 2; |
| 136 | pcm_channels *= 2; |
| 137 | } |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 138 | s->sfc = sfc; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 139 | s->data_block_quadlets = pcm_channels + DIV_ROUND_UP(midi_ports, 8); |
| 140 | s->pcm_channels = pcm_channels; |
| 141 | s->midi_ports = midi_ports; |
| 142 | |
| 143 | s->syt_interval = amdtp_syt_intervals[sfc]; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 144 | |
| 145 | /* default buffering in the device */ |
| 146 | s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; |
| 147 | if (s->flags & CIP_BLOCKING) |
| 148 | /* additional buffering needed to adjust for no-data packets */ |
| 149 | s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 150 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 151 | EXPORT_SYMBOL(amdtp_stream_set_parameters); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 152 | |
| 153 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 154 | * amdtp_stream_get_max_payload - get the stream's packet size |
| 155 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 156 | * |
| 157 | * This function must not be called before the stream has been configured |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 158 | * with amdtp_stream_set_parameters(). |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 159 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 160 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 161 | { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 162 | return 8 + s->syt_interval * s->data_block_quadlets * 4; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 163 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 164 | EXPORT_SYMBOL(amdtp_stream_get_max_payload); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 165 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 166 | static void amdtp_write_s16(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 167 | struct snd_pcm_substream *pcm, |
| 168 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 169 | static void amdtp_write_s32(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 170 | struct snd_pcm_substream *pcm, |
| 171 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 172 | static void amdtp_write_s16_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 173 | struct snd_pcm_substream *pcm, |
| 174 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 175 | static void amdtp_write_s32_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 176 | struct snd_pcm_substream *pcm, |
| 177 | __be32 *buffer, unsigned int frames); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 178 | |
| 179 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 180 | * amdtp_stream_set_pcm_format - set the PCM format |
| 181 | * @s: the AMDTP stream to configure |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 182 | * @format: the format of the ALSA PCM device |
| 183 | * |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 184 | * The sample format must be set after the other paramters (rate/PCM channels/ |
| 185 | * MIDI) and before the stream is started, and must not be changed while the |
| 186 | * stream is running. |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 187 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 188 | void amdtp_stream_set_pcm_format(struct amdtp_stream *s, |
| 189 | snd_pcm_format_t format) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 190 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 191 | if (WARN_ON(amdtp_stream_running(s))) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 192 | return; |
| 193 | |
| 194 | switch (format) { |
| 195 | default: |
| 196 | WARN_ON(1); |
| 197 | /* fall through */ |
| 198 | case SNDRV_PCM_FORMAT_S16: |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 199 | if (s->dual_wire) |
| 200 | s->transfer_samples = amdtp_write_s16_dualwire; |
| 201 | else |
| 202 | s->transfer_samples = amdtp_write_s16; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 203 | break; |
| 204 | case SNDRV_PCM_FORMAT_S32: |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 205 | if (s->dual_wire) |
| 206 | s->transfer_samples = amdtp_write_s32_dualwire; |
| 207 | else |
| 208 | s->transfer_samples = amdtp_write_s32; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 212 | EXPORT_SYMBOL(amdtp_stream_set_pcm_format); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 213 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 214 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 215 | * amdtp_stream_pcm_prepare - prepare PCM device for running |
| 216 | * @s: the AMDTP stream |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 217 | * |
| 218 | * This function should be called from the PCM device's .prepare callback. |
| 219 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 220 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s) |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 221 | { |
| 222 | tasklet_kill(&s->period_tasklet); |
| 223 | s->pcm_buffer_pointer = 0; |
| 224 | s->pcm_period_pointer = 0; |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 225 | s->pointer_flush = true; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 226 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 227 | EXPORT_SYMBOL(amdtp_stream_pcm_prepare); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 228 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 229 | static unsigned int calculate_data_blocks(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 230 | { |
| 231 | unsigned int phase, data_blocks; |
| 232 | |
| 233 | if (!cip_sfc_is_base_44100(s->sfc)) { |
| 234 | /* Sample_rate / 8000 is an integer, and precomputed. */ |
| 235 | data_blocks = s->data_block_state; |
| 236 | } else { |
| 237 | phase = s->data_block_state; |
| 238 | |
| 239 | /* |
| 240 | * This calculates the number of data blocks per packet so that |
| 241 | * 1) the overall rate is correct and exactly synchronized to |
| 242 | * the bus clock, and |
| 243 | * 2) packets with a rounded-up number of blocks occur as early |
| 244 | * as possible in the sequence (to prevent underruns of the |
| 245 | * device's buffer). |
| 246 | */ |
| 247 | if (s->sfc == CIP_SFC_44100) |
| 248 | /* 6 6 5 6 5 6 5 ... */ |
| 249 | data_blocks = 5 + ((phase & 1) ^ |
| 250 | (phase == 0 || phase >= 40)); |
| 251 | else |
| 252 | /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ |
| 253 | data_blocks = 11 * (s->sfc >> 1) + (phase == 0); |
| 254 | if (++phase >= (80 >> (s->sfc >> 1))) |
| 255 | phase = 0; |
| 256 | s->data_block_state = phase; |
| 257 | } |
| 258 | |
| 259 | return data_blocks; |
| 260 | } |
| 261 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 262 | static unsigned int calculate_syt(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 263 | unsigned int cycle) |
| 264 | { |
| 265 | unsigned int syt_offset, phase, index, syt; |
| 266 | |
| 267 | if (s->last_syt_offset < TICKS_PER_CYCLE) { |
| 268 | if (!cip_sfc_is_base_44100(s->sfc)) |
| 269 | syt_offset = s->last_syt_offset + s->syt_offset_state; |
| 270 | else { |
| 271 | /* |
| 272 | * The time, in ticks, of the n'th SYT_INTERVAL sample is: |
| 273 | * n * SYT_INTERVAL * 24576000 / sample_rate |
| 274 | * Modulo TICKS_PER_CYCLE, the difference between successive |
| 275 | * elements is about 1386.23. Rounding the results of this |
| 276 | * formula to the SYT precision results in a sequence of |
| 277 | * differences that begins with: |
| 278 | * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... |
| 279 | * This code generates _exactly_ the same sequence. |
| 280 | */ |
| 281 | phase = s->syt_offset_state; |
| 282 | index = phase % 13; |
| 283 | syt_offset = s->last_syt_offset; |
| 284 | syt_offset += 1386 + ((index && !(index & 3)) || |
| 285 | phase == 146); |
| 286 | if (++phase >= 147) |
| 287 | phase = 0; |
| 288 | s->syt_offset_state = phase; |
| 289 | } |
| 290 | } else |
| 291 | syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; |
| 292 | s->last_syt_offset = syt_offset; |
| 293 | |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 294 | if (syt_offset < TICKS_PER_CYCLE) { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 295 | syt_offset += s->transfer_delay; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 296 | syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; |
| 297 | syt += syt_offset % TICKS_PER_CYCLE; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 298 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 299 | return syt & CIP_SYT_MASK; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 300 | } else { |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 301 | return CIP_SYT_NO_INFO; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 302 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 305 | static void amdtp_write_s32(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 306 | struct snd_pcm_substream *pcm, |
| 307 | __be32 *buffer, unsigned int frames) |
| 308 | { |
| 309 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 310 | unsigned int channels, remaining_frames, frame_step, i, c; |
| 311 | const u32 *src; |
| 312 | |
| 313 | channels = s->pcm_channels; |
| 314 | src = (void *)runtime->dma_area + |
Takashi Sakamoto | e84841f | 2013-09-14 00:35:47 +0900 | [diff] [blame] | 315 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 316 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
| 317 | frame_step = s->data_block_quadlets - channels; |
| 318 | |
| 319 | for (i = 0; i < frames; ++i) { |
| 320 | for (c = 0; c < channels; ++c) { |
| 321 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); |
| 322 | src++; |
| 323 | buffer++; |
| 324 | } |
| 325 | buffer += frame_step; |
| 326 | if (--remaining_frames == 0) |
| 327 | src = (void *)runtime->dma_area; |
| 328 | } |
| 329 | } |
| 330 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 331 | static void amdtp_write_s16(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 332 | struct snd_pcm_substream *pcm, |
| 333 | __be32 *buffer, unsigned int frames) |
| 334 | { |
| 335 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 336 | unsigned int channels, remaining_frames, frame_step, i, c; |
| 337 | const u16 *src; |
| 338 | |
| 339 | channels = s->pcm_channels; |
| 340 | src = (void *)runtime->dma_area + |
Takashi Sakamoto | e84841f | 2013-09-14 00:35:47 +0900 | [diff] [blame] | 341 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 342 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
| 343 | frame_step = s->data_block_quadlets - channels; |
| 344 | |
| 345 | for (i = 0; i < frames; ++i) { |
| 346 | for (c = 0; c < channels; ++c) { |
| 347 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); |
| 348 | src++; |
| 349 | buffer++; |
| 350 | } |
| 351 | buffer += frame_step; |
| 352 | if (--remaining_frames == 0) |
| 353 | src = (void *)runtime->dma_area; |
| 354 | } |
| 355 | } |
| 356 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 357 | static void amdtp_write_s32_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 358 | struct snd_pcm_substream *pcm, |
| 359 | __be32 *buffer, unsigned int frames) |
| 360 | { |
| 361 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 362 | unsigned int channels, frame_adjust_1, frame_adjust_2, i, c; |
| 363 | const u32 *src; |
| 364 | |
| 365 | channels = s->pcm_channels; |
| 366 | src = (void *)runtime->dma_area + |
| 367 | s->pcm_buffer_pointer * (runtime->frame_bits / 8); |
| 368 | frame_adjust_1 = channels - 1; |
| 369 | frame_adjust_2 = 1 - (s->data_block_quadlets - channels); |
| 370 | |
| 371 | channels /= 2; |
| 372 | for (i = 0; i < frames; ++i) { |
| 373 | for (c = 0; c < channels; ++c) { |
| 374 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); |
| 375 | src++; |
| 376 | buffer += 2; |
| 377 | } |
| 378 | buffer -= frame_adjust_1; |
| 379 | for (c = 0; c < channels; ++c) { |
| 380 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); |
| 381 | src++; |
| 382 | buffer += 2; |
| 383 | } |
| 384 | buffer -= frame_adjust_2; |
| 385 | } |
| 386 | } |
| 387 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 388 | static void amdtp_write_s16_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 389 | struct snd_pcm_substream *pcm, |
| 390 | __be32 *buffer, unsigned int frames) |
| 391 | { |
| 392 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 393 | unsigned int channels, frame_adjust_1, frame_adjust_2, i, c; |
| 394 | const u16 *src; |
| 395 | |
| 396 | channels = s->pcm_channels; |
| 397 | src = (void *)runtime->dma_area + |
| 398 | s->pcm_buffer_pointer * (runtime->frame_bits / 8); |
| 399 | frame_adjust_1 = channels - 1; |
| 400 | frame_adjust_2 = 1 - (s->data_block_quadlets - channels); |
| 401 | |
| 402 | channels /= 2; |
| 403 | for (i = 0; i < frames; ++i) { |
| 404 | for (c = 0; c < channels; ++c) { |
| 405 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); |
| 406 | src++; |
| 407 | buffer += 2; |
| 408 | } |
| 409 | buffer -= frame_adjust_1; |
| 410 | for (c = 0; c < channels; ++c) { |
| 411 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); |
| 412 | src++; |
| 413 | buffer += 2; |
| 414 | } |
| 415 | buffer -= frame_adjust_2; |
| 416 | } |
| 417 | } |
| 418 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 419 | static void amdtp_fill_pcm_silence(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 420 | __be32 *buffer, unsigned int frames) |
| 421 | { |
| 422 | unsigned int i, c; |
| 423 | |
| 424 | for (i = 0; i < frames; ++i) { |
| 425 | for (c = 0; c < s->pcm_channels; ++c) |
| 426 | buffer[c] = cpu_to_be32(0x40000000); |
| 427 | buffer += s->data_block_quadlets; |
| 428 | } |
| 429 | } |
| 430 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 431 | static void amdtp_fill_midi(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 432 | __be32 *buffer, unsigned int frames) |
| 433 | { |
| 434 | unsigned int i; |
| 435 | |
| 436 | for (i = 0; i < frames; ++i) |
| 437 | buffer[s->pcm_channels + i * s->data_block_quadlets] = |
| 438 | cpu_to_be32(0x80000000); |
| 439 | } |
| 440 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 441 | static void queue_out_packet(struct amdtp_stream *s, unsigned int cycle) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 442 | { |
| 443 | __be32 *buffer; |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 444 | unsigned int index, data_blocks, syt, ptr; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 445 | struct snd_pcm_substream *pcm; |
| 446 | struct fw_iso_packet packet; |
| 447 | int err; |
| 448 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 449 | if (s->packet_index < 0) |
| 450 | return; |
| 451 | index = s->packet_index; |
| 452 | |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 453 | /* this module generate empty packet for 'no data' */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 454 | syt = calculate_syt(s, cycle); |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 455 | if (!(s->flags & CIP_BLOCKING)) |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 456 | data_blocks = calculate_data_blocks(s); |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 457 | else if (syt != CIP_SYT_NO_INFO) |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 458 | data_blocks = s->syt_interval; |
| 459 | else |
| 460 | data_blocks = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 461 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 462 | buffer = s->buffer.packets[index].buffer; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 463 | buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame^] | 464 | (s->data_block_quadlets << AMDTP_DBS_SHIFT) | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 465 | s->data_block_counter); |
| 466 | 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^] | 467 | (s->sfc << CIP_FDF_SFC_SHIFT) | syt); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 468 | buffer += 2; |
| 469 | |
| 470 | pcm = ACCESS_ONCE(s->pcm); |
| 471 | if (pcm) |
| 472 | s->transfer_samples(s, pcm, buffer, data_blocks); |
| 473 | else |
| 474 | amdtp_fill_pcm_silence(s, buffer, data_blocks); |
| 475 | if (s->midi_ports) |
| 476 | amdtp_fill_midi(s, buffer, data_blocks); |
| 477 | |
| 478 | s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; |
| 479 | |
| 480 | packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 481 | packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 482 | packet.skip = 0; |
| 483 | packet.tag = TAG_CIP; |
| 484 | packet.sy = 0; |
| 485 | packet.header_length = 0; |
| 486 | |
| 487 | err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer, |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 488 | s->buffer.packets[index].offset); |
| 489 | if (err < 0) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 490 | dev_err(&s->unit->device, "queueing error: %d\n", err); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 491 | s->packet_index = -1; |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 492 | amdtp_stream_pcm_abort(s); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 493 | return; |
| 494 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 495 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 496 | if (++index >= QUEUE_LENGTH) |
| 497 | index = 0; |
| 498 | s->packet_index = index; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 499 | |
| 500 | if (pcm) { |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 501 | if (s->dual_wire) |
| 502 | data_blocks *= 2; |
| 503 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 504 | ptr = s->pcm_buffer_pointer + data_blocks; |
| 505 | if (ptr >= pcm->runtime->buffer_size) |
| 506 | ptr -= pcm->runtime->buffer_size; |
| 507 | ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; |
| 508 | |
| 509 | s->pcm_period_pointer += data_blocks; |
| 510 | if (s->pcm_period_pointer >= pcm->runtime->period_size) { |
| 511 | s->pcm_period_pointer -= pcm->runtime->period_size; |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 512 | s->pointer_flush = false; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 513 | tasklet_hi_schedule(&s->period_tasklet); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 518 | static void pcm_period_tasklet(unsigned long data) |
| 519 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 520 | struct amdtp_stream *s = (void *)data; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 521 | struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); |
| 522 | |
| 523 | if (pcm) |
| 524 | snd_pcm_period_elapsed(pcm); |
| 525 | } |
| 526 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 527 | static void out_packet_callback(struct fw_iso_context *context, u32 cycle, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 528 | size_t header_length, void *header, void *private_data) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 529 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 530 | struct amdtp_stream *s = private_data; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 531 | unsigned int i, packets = header_length / 4; |
| 532 | |
| 533 | /* |
| 534 | * Compute the cycle of the last queued packet. |
| 535 | * (We need only the four lowest bits for the SYT, so we can ignore |
| 536 | * that bits 0-11 must wrap around at 3072.) |
| 537 | */ |
| 538 | cycle += QUEUE_LENGTH - packets; |
| 539 | |
| 540 | for (i = 0; i < packets; ++i) |
| 541 | queue_out_packet(s, ++cycle); |
Clemens Ladisch | 13882a8 | 2011-05-02 09:33:56 +0200 | [diff] [blame] | 542 | fw_iso_context_queue_flush(s->context); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 543 | } |
| 544 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 545 | static int queue_initial_skip_packets(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 546 | { |
| 547 | struct fw_iso_packet skip_packet = { |
| 548 | .skip = 1, |
| 549 | }; |
| 550 | unsigned int i; |
| 551 | int err; |
| 552 | |
| 553 | for (i = 0; i < QUEUE_LENGTH; ++i) { |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 554 | skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 555 | INTERRUPT_INTERVAL); |
| 556 | err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0); |
| 557 | if (err < 0) |
| 558 | return err; |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 559 | if (++s->packet_index >= QUEUE_LENGTH) |
| 560 | s->packet_index = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 567 | * amdtp_stream_start - start transferring packets |
| 568 | * @s: the AMDTP stream to start |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 569 | * @channel: the isochronous channel on the bus |
| 570 | * @speed: firewire speed code |
| 571 | * |
| 572 | * The stream cannot be started until it has been configured with |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 573 | * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI |
| 574 | * device can be started. |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 575 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 576 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 577 | { |
| 578 | static const struct { |
| 579 | unsigned int data_block; |
| 580 | unsigned int syt_offset; |
| 581 | } initial_state[] = { |
| 582 | [CIP_SFC_32000] = { 4, 3072 }, |
| 583 | [CIP_SFC_48000] = { 6, 1024 }, |
| 584 | [CIP_SFC_96000] = { 12, 1024 }, |
| 585 | [CIP_SFC_192000] = { 24, 1024 }, |
| 586 | [CIP_SFC_44100] = { 0, 67 }, |
| 587 | [CIP_SFC_88200] = { 0, 67 }, |
| 588 | [CIP_SFC_176400] = { 0, 67 }, |
| 589 | }; |
| 590 | int err; |
| 591 | |
| 592 | mutex_lock(&s->mutex); |
| 593 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 594 | if (WARN_ON(amdtp_stream_running(s) || |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 595 | (!s->pcm_channels && !s->midi_ports))) { |
| 596 | err = -EBADFD; |
| 597 | goto err_unlock; |
| 598 | } |
| 599 | |
| 600 | s->data_block_state = initial_state[s->sfc].data_block; |
| 601 | s->syt_offset_state = initial_state[s->sfc].syt_offset; |
| 602 | s->last_syt_offset = TICKS_PER_CYCLE; |
| 603 | |
| 604 | err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 605 | amdtp_stream_get_max_payload(s), |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 606 | DMA_TO_DEVICE); |
| 607 | if (err < 0) |
| 608 | goto err_unlock; |
| 609 | |
| 610 | s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, |
| 611 | FW_ISO_CONTEXT_TRANSMIT, |
| 612 | channel, speed, 0, |
| 613 | out_packet_callback, s); |
| 614 | if (IS_ERR(s->context)) { |
| 615 | err = PTR_ERR(s->context); |
| 616 | if (err == -EBUSY) |
| 617 | dev_err(&s->unit->device, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 618 | "no free stream on this controller\n"); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 619 | goto err_buffer; |
| 620 | } |
| 621 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 622 | amdtp_stream_update(s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 623 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 624 | s->packet_index = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 625 | s->data_block_counter = 0; |
| 626 | err = queue_initial_skip_packets(s); |
| 627 | if (err < 0) |
| 628 | goto err_context; |
| 629 | |
| 630 | err = fw_iso_context_start(s->context, -1, 0, 0); |
| 631 | if (err < 0) |
| 632 | goto err_context; |
| 633 | |
| 634 | mutex_unlock(&s->mutex); |
| 635 | |
| 636 | return 0; |
| 637 | |
| 638 | err_context: |
| 639 | fw_iso_context_destroy(s->context); |
| 640 | s->context = ERR_PTR(-1); |
| 641 | err_buffer: |
| 642 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 643 | err_unlock: |
| 644 | mutex_unlock(&s->mutex); |
| 645 | |
| 646 | return err; |
| 647 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 648 | EXPORT_SYMBOL(amdtp_stream_start); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 649 | |
| 650 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 651 | * amdtp_stream_pcm_pointer - get the PCM buffer position |
| 652 | * @s: the AMDTP stream that transports the PCM data |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 653 | * |
| 654 | * Returns the current buffer position, in frames. |
| 655 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 656 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 657 | { |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 658 | /* this optimization is allowed to be racy */ |
| 659 | if (s->pointer_flush) |
| 660 | fw_iso_context_flush_completions(s->context); |
| 661 | else |
| 662 | s->pointer_flush = true; |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 663 | |
| 664 | return ACCESS_ONCE(s->pcm_buffer_pointer); |
| 665 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 666 | EXPORT_SYMBOL(amdtp_stream_pcm_pointer); |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 667 | |
| 668 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 669 | * amdtp_stream_update - update the stream after a bus reset |
| 670 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 671 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 672 | void amdtp_stream_update(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 673 | { |
| 674 | ACCESS_ONCE(s->source_node_id_field) = |
| 675 | (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24; |
| 676 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 677 | EXPORT_SYMBOL(amdtp_stream_update); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 678 | |
| 679 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 680 | * amdtp_stream_stop - stop sending packets |
| 681 | * @s: the AMDTP stream to stop |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 682 | * |
| 683 | * All PCM and MIDI devices of the stream must be stopped before the stream |
| 684 | * itself can be stopped. |
| 685 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 686 | void amdtp_stream_stop(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 687 | { |
| 688 | mutex_lock(&s->mutex); |
| 689 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 690 | if (!amdtp_stream_running(s)) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 691 | mutex_unlock(&s->mutex); |
| 692 | return; |
| 693 | } |
| 694 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 695 | tasklet_kill(&s->period_tasklet); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 696 | fw_iso_context_stop(s->context); |
| 697 | fw_iso_context_destroy(s->context); |
| 698 | s->context = ERR_PTR(-1); |
| 699 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 700 | |
| 701 | mutex_unlock(&s->mutex); |
| 702 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 703 | EXPORT_SYMBOL(amdtp_stream_stop); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 704 | |
| 705 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 706 | * amdtp_stream_pcm_abort - abort the running PCM device |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 707 | * @s: the AMDTP stream about to be stopped |
| 708 | * |
| 709 | * If the isochronous stream needs to be stopped asynchronously, call this |
| 710 | * function first to stop the PCM device. |
| 711 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 712 | void amdtp_stream_pcm_abort(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 713 | { |
| 714 | struct snd_pcm_substream *pcm; |
| 715 | |
| 716 | pcm = ACCESS_ONCE(s->pcm); |
| 717 | if (pcm) { |
| 718 | snd_pcm_stream_lock_irq(pcm); |
| 719 | if (snd_pcm_running(pcm)) |
| 720 | snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN); |
| 721 | snd_pcm_stream_unlock_irq(pcm); |
| 722 | } |
| 723 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 724 | EXPORT_SYMBOL(amdtp_stream_pcm_abort); |