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)) |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 42 | #define AMDTP_FDF_NO_DATA 0xff |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 43 | #define AMDTP_DBS_MASK 0x00ff0000 |
| 44 | #define AMDTP_DBS_SHIFT 16 |
| 45 | #define AMDTP_DBC_MASK 0x000000ff |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 46 | |
| 47 | /* TODO: make these configurable */ |
| 48 | #define INTERRUPT_INTERVAL 16 |
| 49 | #define QUEUE_LENGTH 48 |
| 50 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 51 | #define IN_PACKET_HEADER_SIZE 4 |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 52 | #define OUT_PACKET_HEADER_SIZE 0 |
| 53 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 54 | static void pcm_period_tasklet(unsigned long data); |
| 55 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 56 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 57 | * amdtp_stream_init - initialize an AMDTP stream structure |
| 58 | * @s: the AMDTP stream to initialize |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 59 | * @unit: the target of the stream |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 60 | * @dir: the direction of stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 61 | * @flags: the packet transmission method to use |
| 62 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 63 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 64 | enum amdtp_stream_direction dir, enum cip_flags flags) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 65 | { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 66 | s->unit = fw_unit_get(unit); |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 67 | s->direction = dir; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 68 | s->flags = flags; |
| 69 | s->context = ERR_PTR(-1); |
| 70 | mutex_init(&s->mutex); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 71 | tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 72 | s->packet_index = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 76 | EXPORT_SYMBOL(amdtp_stream_init); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 77 | |
| 78 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 79 | * amdtp_stream_destroy - free stream resources |
| 80 | * @s: the AMDTP stream to destroy |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 81 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 82 | void amdtp_stream_destroy(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 83 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 84 | WARN_ON(amdtp_stream_running(s)); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 85 | mutex_destroy(&s->mutex); |
| 86 | fw_unit_put(s->unit); |
| 87 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 88 | EXPORT_SYMBOL(amdtp_stream_destroy); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 89 | |
Clemens Ladisch | c5280e9 | 2011-10-16 21:39:00 +0200 | [diff] [blame] | 90 | const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 91 | [CIP_SFC_32000] = 8, |
| 92 | [CIP_SFC_44100] = 8, |
| 93 | [CIP_SFC_48000] = 8, |
| 94 | [CIP_SFC_88200] = 16, |
| 95 | [CIP_SFC_96000] = 16, |
| 96 | [CIP_SFC_176400] = 32, |
| 97 | [CIP_SFC_192000] = 32, |
| 98 | }; |
| 99 | EXPORT_SYMBOL(amdtp_syt_intervals); |
| 100 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 101 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 102 | * amdtp_stream_set_parameters - set stream parameters |
| 103 | * @s: the AMDTP stream to configure |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 104 | * @rate: the sample rate |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 105 | * @pcm_channels: the number of PCM samples in each data block, to be encoded |
| 106 | * as AM824 multi-bit linear audio |
| 107 | * @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] | 108 | * |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 109 | * 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] | 110 | * changed while the stream is running. |
| 111 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 112 | void amdtp_stream_set_parameters(struct amdtp_stream *s, |
| 113 | unsigned int rate, |
| 114 | unsigned int pcm_channels, |
| 115 | unsigned int midi_ports) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 116 | { |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 117 | static const unsigned int rates[] = { |
| 118 | [CIP_SFC_32000] = 32000, |
| 119 | [CIP_SFC_44100] = 44100, |
| 120 | [CIP_SFC_48000] = 48000, |
| 121 | [CIP_SFC_88200] = 88200, |
| 122 | [CIP_SFC_96000] = 96000, |
| 123 | [CIP_SFC_176400] = 176400, |
| 124 | [CIP_SFC_192000] = 192000, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 125 | }; |
| 126 | unsigned int sfc; |
| 127 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 128 | if (WARN_ON(amdtp_stream_running(s))) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 129 | return; |
| 130 | |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 131 | for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc) |
| 132 | if (rates[sfc] == rate) |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 133 | goto sfc_found; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 134 | WARN_ON(1); |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | sfc_found: |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 138 | s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000; |
| 139 | if (s->dual_wire) { |
| 140 | sfc -= 2; |
| 141 | rate /= 2; |
| 142 | pcm_channels *= 2; |
| 143 | } |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 144 | s->sfc = sfc; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 145 | s->data_block_quadlets = pcm_channels + DIV_ROUND_UP(midi_ports, 8); |
| 146 | s->pcm_channels = pcm_channels; |
| 147 | s->midi_ports = midi_ports; |
| 148 | |
| 149 | s->syt_interval = amdtp_syt_intervals[sfc]; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 150 | |
| 151 | /* default buffering in the device */ |
| 152 | s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; |
| 153 | if (s->flags & CIP_BLOCKING) |
| 154 | /* additional buffering needed to adjust for no-data packets */ |
| 155 | s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 156 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 157 | EXPORT_SYMBOL(amdtp_stream_set_parameters); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 158 | |
| 159 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 160 | * amdtp_stream_get_max_payload - get the stream's packet size |
| 161 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 162 | * |
| 163 | * This function must not be called before the stream has been configured |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 164 | * with amdtp_stream_set_parameters(). |
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 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 167 | { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 168 | return 8 + s->syt_interval * s->data_block_quadlets * 4; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 169 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 170 | EXPORT_SYMBOL(amdtp_stream_get_max_payload); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 171 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 172 | static void amdtp_write_s16(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [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(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 176 | struct snd_pcm_substream *pcm, |
| 177 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 178 | static void amdtp_write_s16_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 179 | struct snd_pcm_substream *pcm, |
| 180 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 181 | static void amdtp_write_s32_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 182 | struct snd_pcm_substream *pcm, |
| 183 | __be32 *buffer, unsigned int frames); |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 184 | static void amdtp_read_s32(struct amdtp_stream *s, |
| 185 | struct snd_pcm_substream *pcm, |
| 186 | __be32 *buffer, unsigned int frames); |
| 187 | static void amdtp_read_s32_dualwire(struct amdtp_stream *s, |
| 188 | struct snd_pcm_substream *pcm, |
| 189 | __be32 *buffer, unsigned int frames); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 190 | |
| 191 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 192 | * amdtp_stream_set_pcm_format - set the PCM format |
| 193 | * @s: the AMDTP stream to configure |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 194 | * @format: the format of the ALSA PCM device |
| 195 | * |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 196 | * The sample format must be set after the other paramters (rate/PCM channels/ |
| 197 | * MIDI) and before the stream is started, and must not be changed while the |
| 198 | * stream is running. |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 199 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 200 | void amdtp_stream_set_pcm_format(struct amdtp_stream *s, |
| 201 | snd_pcm_format_t format) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 202 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 203 | if (WARN_ON(amdtp_stream_running(s))) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 204 | return; |
| 205 | |
| 206 | switch (format) { |
| 207 | default: |
| 208 | WARN_ON(1); |
| 209 | /* fall through */ |
| 210 | case SNDRV_PCM_FORMAT_S16: |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 211 | if (s->direction == AMDTP_OUT_STREAM) { |
| 212 | if (s->dual_wire) |
| 213 | s->transfer_samples = amdtp_write_s16_dualwire; |
| 214 | else |
| 215 | s->transfer_samples = amdtp_write_s16; |
| 216 | break; |
| 217 | } |
| 218 | WARN_ON(1); |
| 219 | /* fall through */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 220 | case SNDRV_PCM_FORMAT_S32: |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 221 | if (s->direction == AMDTP_OUT_STREAM) { |
| 222 | if (s->dual_wire) |
| 223 | s->transfer_samples = amdtp_write_s32_dualwire; |
| 224 | else |
| 225 | s->transfer_samples = amdtp_write_s32; |
| 226 | } else { |
| 227 | if (s->dual_wire) |
| 228 | s->transfer_samples = amdtp_read_s32_dualwire; |
| 229 | else |
| 230 | s->transfer_samples = amdtp_read_s32; |
| 231 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 235 | EXPORT_SYMBOL(amdtp_stream_set_pcm_format); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 236 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 237 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 238 | * amdtp_stream_pcm_prepare - prepare PCM device for running |
| 239 | * @s: the AMDTP stream |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 240 | * |
| 241 | * This function should be called from the PCM device's .prepare callback. |
| 242 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 243 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s) |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 244 | { |
| 245 | tasklet_kill(&s->period_tasklet); |
| 246 | s->pcm_buffer_pointer = 0; |
| 247 | s->pcm_period_pointer = 0; |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 248 | s->pointer_flush = true; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 249 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 250 | EXPORT_SYMBOL(amdtp_stream_pcm_prepare); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 251 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 252 | static unsigned int calculate_data_blocks(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 253 | { |
| 254 | unsigned int phase, data_blocks; |
| 255 | |
| 256 | if (!cip_sfc_is_base_44100(s->sfc)) { |
| 257 | /* Sample_rate / 8000 is an integer, and precomputed. */ |
| 258 | data_blocks = s->data_block_state; |
| 259 | } else { |
| 260 | phase = s->data_block_state; |
| 261 | |
| 262 | /* |
| 263 | * This calculates the number of data blocks per packet so that |
| 264 | * 1) the overall rate is correct and exactly synchronized to |
| 265 | * the bus clock, and |
| 266 | * 2) packets with a rounded-up number of blocks occur as early |
| 267 | * as possible in the sequence (to prevent underruns of the |
| 268 | * device's buffer). |
| 269 | */ |
| 270 | if (s->sfc == CIP_SFC_44100) |
| 271 | /* 6 6 5 6 5 6 5 ... */ |
| 272 | data_blocks = 5 + ((phase & 1) ^ |
| 273 | (phase == 0 || phase >= 40)); |
| 274 | else |
| 275 | /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ |
| 276 | data_blocks = 11 * (s->sfc >> 1) + (phase == 0); |
| 277 | if (++phase >= (80 >> (s->sfc >> 1))) |
| 278 | phase = 0; |
| 279 | s->data_block_state = phase; |
| 280 | } |
| 281 | |
| 282 | return data_blocks; |
| 283 | } |
| 284 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 285 | static unsigned int calculate_syt(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 286 | unsigned int cycle) |
| 287 | { |
| 288 | unsigned int syt_offset, phase, index, syt; |
| 289 | |
| 290 | if (s->last_syt_offset < TICKS_PER_CYCLE) { |
| 291 | if (!cip_sfc_is_base_44100(s->sfc)) |
| 292 | syt_offset = s->last_syt_offset + s->syt_offset_state; |
| 293 | else { |
| 294 | /* |
| 295 | * The time, in ticks, of the n'th SYT_INTERVAL sample is: |
| 296 | * n * SYT_INTERVAL * 24576000 / sample_rate |
| 297 | * Modulo TICKS_PER_CYCLE, the difference between successive |
| 298 | * elements is about 1386.23. Rounding the results of this |
| 299 | * formula to the SYT precision results in a sequence of |
| 300 | * differences that begins with: |
| 301 | * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... |
| 302 | * This code generates _exactly_ the same sequence. |
| 303 | */ |
| 304 | phase = s->syt_offset_state; |
| 305 | index = phase % 13; |
| 306 | syt_offset = s->last_syt_offset; |
| 307 | syt_offset += 1386 + ((index && !(index & 3)) || |
| 308 | phase == 146); |
| 309 | if (++phase >= 147) |
| 310 | phase = 0; |
| 311 | s->syt_offset_state = phase; |
| 312 | } |
| 313 | } else |
| 314 | syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; |
| 315 | s->last_syt_offset = syt_offset; |
| 316 | |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 317 | if (syt_offset < TICKS_PER_CYCLE) { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 318 | syt_offset += s->transfer_delay; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 319 | syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; |
| 320 | syt += syt_offset % TICKS_PER_CYCLE; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 321 | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 322 | return syt & CIP_SYT_MASK; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 323 | } else { |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 324 | return CIP_SYT_NO_INFO; |
Clemens Ladisch | be45436 | 2011-03-15 07:55:02 +0100 | [diff] [blame] | 325 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 328 | static void amdtp_write_s32(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 329 | struct snd_pcm_substream *pcm, |
| 330 | __be32 *buffer, unsigned int frames) |
| 331 | { |
| 332 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 333 | unsigned int channels, remaining_frames, frame_step, i, c; |
| 334 | const u32 *src; |
| 335 | |
| 336 | channels = s->pcm_channels; |
| 337 | src = (void *)runtime->dma_area + |
Takashi Sakamoto | e84841f | 2013-09-14 00:35:47 +0900 | [diff] [blame] | 338 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 339 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
| 340 | frame_step = s->data_block_quadlets - channels; |
| 341 | |
| 342 | for (i = 0; i < frames; ++i) { |
| 343 | for (c = 0; c < channels; ++c) { |
| 344 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); |
| 345 | src++; |
| 346 | buffer++; |
| 347 | } |
| 348 | buffer += frame_step; |
| 349 | if (--remaining_frames == 0) |
| 350 | src = (void *)runtime->dma_area; |
| 351 | } |
| 352 | } |
| 353 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 354 | static void amdtp_write_s16(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 355 | struct snd_pcm_substream *pcm, |
| 356 | __be32 *buffer, unsigned int frames) |
| 357 | { |
| 358 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 359 | unsigned int channels, remaining_frames, frame_step, i, c; |
| 360 | const u16 *src; |
| 361 | |
| 362 | channels = s->pcm_channels; |
| 363 | src = (void *)runtime->dma_area + |
Takashi Sakamoto | e84841f | 2013-09-14 00:35:47 +0900 | [diff] [blame] | 364 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 365 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
| 366 | frame_step = s->data_block_quadlets - channels; |
| 367 | |
| 368 | for (i = 0; i < frames; ++i) { |
| 369 | for (c = 0; c < channels; ++c) { |
| 370 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); |
| 371 | src++; |
| 372 | buffer++; |
| 373 | } |
| 374 | buffer += frame_step; |
| 375 | if (--remaining_frames == 0) |
| 376 | src = (void *)runtime->dma_area; |
| 377 | } |
| 378 | } |
| 379 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 380 | static void amdtp_write_s32_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 381 | struct snd_pcm_substream *pcm, |
| 382 | __be32 *buffer, unsigned int frames) |
| 383 | { |
| 384 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 385 | unsigned int channels, frame_adjust_1, frame_adjust_2, i, c; |
| 386 | const u32 *src; |
| 387 | |
| 388 | channels = s->pcm_channels; |
| 389 | src = (void *)runtime->dma_area + |
| 390 | s->pcm_buffer_pointer * (runtime->frame_bits / 8); |
| 391 | frame_adjust_1 = channels - 1; |
| 392 | frame_adjust_2 = 1 - (s->data_block_quadlets - channels); |
| 393 | |
| 394 | channels /= 2; |
| 395 | for (i = 0; i < frames; ++i) { |
| 396 | for (c = 0; c < channels; ++c) { |
| 397 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); |
| 398 | src++; |
| 399 | buffer += 2; |
| 400 | } |
| 401 | buffer -= frame_adjust_1; |
| 402 | for (c = 0; c < channels; ++c) { |
| 403 | *buffer = cpu_to_be32((*src >> 8) | 0x40000000); |
| 404 | src++; |
| 405 | buffer += 2; |
| 406 | } |
| 407 | buffer -= frame_adjust_2; |
| 408 | } |
| 409 | } |
| 410 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 411 | static void amdtp_write_s16_dualwire(struct amdtp_stream *s, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 412 | struct snd_pcm_substream *pcm, |
| 413 | __be32 *buffer, unsigned int frames) |
| 414 | { |
| 415 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 416 | unsigned int channels, frame_adjust_1, frame_adjust_2, i, c; |
| 417 | const u16 *src; |
| 418 | |
| 419 | channels = s->pcm_channels; |
| 420 | src = (void *)runtime->dma_area + |
| 421 | s->pcm_buffer_pointer * (runtime->frame_bits / 8); |
| 422 | frame_adjust_1 = channels - 1; |
| 423 | frame_adjust_2 = 1 - (s->data_block_quadlets - channels); |
| 424 | |
| 425 | channels /= 2; |
| 426 | for (i = 0; i < frames; ++i) { |
| 427 | for (c = 0; c < channels; ++c) { |
| 428 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); |
| 429 | src++; |
| 430 | buffer += 2; |
| 431 | } |
| 432 | buffer -= frame_adjust_1; |
| 433 | for (c = 0; c < channels; ++c) { |
| 434 | *buffer = cpu_to_be32((*src << 8) | 0x40000000); |
| 435 | src++; |
| 436 | buffer += 2; |
| 437 | } |
| 438 | buffer -= frame_adjust_2; |
| 439 | } |
| 440 | } |
| 441 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 442 | static void amdtp_read_s32(struct amdtp_stream *s, |
| 443 | struct snd_pcm_substream *pcm, |
| 444 | __be32 *buffer, unsigned int frames) |
| 445 | { |
| 446 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 447 | unsigned int channels, remaining_frames, i, c; |
| 448 | u32 *dst; |
| 449 | |
| 450 | channels = s->pcm_channels; |
| 451 | dst = (void *)runtime->dma_area + |
| 452 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
| 453 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
| 454 | |
| 455 | for (i = 0; i < frames; ++i) { |
| 456 | for (c = 0; c < channels; ++c) { |
| 457 | *dst = be32_to_cpu(buffer[c]) << 8; |
| 458 | dst++; |
| 459 | } |
| 460 | buffer += s->data_block_quadlets; |
| 461 | if (--remaining_frames == 0) |
| 462 | dst = (void *)runtime->dma_area; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void amdtp_read_s32_dualwire(struct amdtp_stream *s, |
| 467 | struct snd_pcm_substream *pcm, |
| 468 | __be32 *buffer, unsigned int frames) |
| 469 | { |
| 470 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 471 | unsigned int channels, remaining_frames, i, c; |
| 472 | u32 *dst; |
| 473 | |
| 474 | dst = (void *)runtime->dma_area + |
| 475 | frames_to_bytes(runtime, s->pcm_buffer_pointer); |
| 476 | remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
| 477 | channels = s->pcm_channels / 2; |
| 478 | |
| 479 | for (i = 0; i < frames; ++i) { |
| 480 | for (c = 0; c < channels; ++c) { |
| 481 | *dst = be32_to_cpu(buffer[c * 2]) << 8; |
| 482 | dst++; |
| 483 | } |
| 484 | buffer += 1; |
| 485 | for (c = 0; c < channels; ++c) { |
| 486 | *dst = be32_to_cpu(buffer[c * 2]) << 8; |
| 487 | dst++; |
| 488 | } |
| 489 | buffer += s->data_block_quadlets - 1; |
| 490 | if (--remaining_frames == 0) |
| 491 | dst = (void *)runtime->dma_area; |
| 492 | } |
| 493 | } |
| 494 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 495 | static void amdtp_fill_pcm_silence(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 496 | __be32 *buffer, unsigned int frames) |
| 497 | { |
| 498 | unsigned int i, c; |
| 499 | |
| 500 | for (i = 0; i < frames; ++i) { |
| 501 | for (c = 0; c < s->pcm_channels; ++c) |
| 502 | buffer[c] = cpu_to_be32(0x40000000); |
| 503 | buffer += s->data_block_quadlets; |
| 504 | } |
| 505 | } |
| 506 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 507 | static void amdtp_fill_midi(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 508 | __be32 *buffer, unsigned int frames) |
| 509 | { |
| 510 | unsigned int i; |
| 511 | |
| 512 | for (i = 0; i < frames; ++i) |
| 513 | buffer[s->pcm_channels + i * s->data_block_quadlets] = |
| 514 | cpu_to_be32(0x80000000); |
| 515 | } |
| 516 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 517 | static void update_pcm_pointers(struct amdtp_stream *s, |
| 518 | struct snd_pcm_substream *pcm, |
| 519 | unsigned int frames) |
| 520 | { unsigned int ptr; |
| 521 | |
| 522 | if (s->dual_wire) |
| 523 | frames *= 2; |
| 524 | |
| 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}; |
| 552 | int err; |
| 553 | |
| 554 | p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL); |
| 555 | p.tag = TAG_CIP; |
| 556 | p.header_length = header_length; |
| 557 | p.payload_length = (!skip) ? payload_length : 0; |
| 558 | p.skip = skip; |
| 559 | err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer, |
| 560 | s->buffer.packets[s->packet_index].offset); |
| 561 | if (err < 0) { |
| 562 | dev_err(&s->unit->device, "queueing error: %d\n", err); |
| 563 | goto end; |
| 564 | } |
| 565 | |
| 566 | if (++s->packet_index >= QUEUE_LENGTH) |
| 567 | s->packet_index = 0; |
| 568 | end: |
| 569 | return err; |
| 570 | } |
| 571 | |
| 572 | static inline int queue_out_packet(struct amdtp_stream *s, |
| 573 | unsigned int payload_length, bool skip) |
| 574 | { |
| 575 | return queue_packet(s, OUT_PACKET_HEADER_SIZE, |
| 576 | payload_length, skip); |
| 577 | } |
| 578 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 579 | static inline int queue_in_packet(struct amdtp_stream *s) |
| 580 | { |
| 581 | return queue_packet(s, IN_PACKET_HEADER_SIZE, |
| 582 | amdtp_stream_get_max_payload(s), false); |
| 583 | } |
| 584 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 585 | static void handle_out_packet(struct amdtp_stream *s, unsigned int cycle) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 586 | { |
| 587 | __be32 *buffer; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 588 | unsigned int index, data_blocks, syt, payload_length; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 589 | struct snd_pcm_substream *pcm; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 590 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 591 | if (s->packet_index < 0) |
| 592 | return; |
| 593 | index = s->packet_index; |
| 594 | |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 595 | /* this module generate empty packet for 'no data' */ |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 596 | syt = calculate_syt(s, cycle); |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 597 | if (!(s->flags & CIP_BLOCKING)) |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 598 | data_blocks = calculate_data_blocks(s); |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 599 | else if (syt != CIP_SYT_NO_INFO) |
Takashi Sakamoto | 82755ab | 2013-11-21 14:21:06 +0900 | [diff] [blame] | 600 | data_blocks = s->syt_interval; |
| 601 | else |
| 602 | data_blocks = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 603 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 604 | buffer = s->buffer.packets[index].buffer; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 605 | buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | |
Takashi Sakamoto | b445db4 | 2014-04-25 22:44:43 +0900 | [diff] [blame] | 606 | (s->data_block_quadlets << AMDTP_DBS_SHIFT) | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 607 | s->data_block_counter); |
| 608 | 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] | 609 | (s->sfc << CIP_FDF_SFC_SHIFT) | syt); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 610 | buffer += 2; |
| 611 | |
| 612 | pcm = ACCESS_ONCE(s->pcm); |
| 613 | if (pcm) |
| 614 | s->transfer_samples(s, pcm, buffer, data_blocks); |
| 615 | else |
| 616 | amdtp_fill_pcm_silence(s, buffer, data_blocks); |
| 617 | if (s->midi_ports) |
| 618 | amdtp_fill_midi(s, buffer, data_blocks); |
| 619 | |
| 620 | s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; |
| 621 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 622 | payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; |
| 623 | if (queue_out_packet(s, payload_length, false) < 0) { |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 624 | s->packet_index = -1; |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 625 | amdtp_stream_pcm_abort(s); |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 626 | return; |
| 627 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 628 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 629 | if (pcm) |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 630 | update_pcm_pointers(s, pcm, data_blocks); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 633 | static void handle_in_packet(struct amdtp_stream *s, |
| 634 | unsigned int payload_quadlets, |
| 635 | __be32 *buffer) |
| 636 | { |
| 637 | u32 cip_header[2]; |
| 638 | unsigned int data_blocks, data_block_quadlets, data_block_counter; |
| 639 | struct snd_pcm_substream *pcm = NULL; |
| 640 | |
| 641 | cip_header[0] = be32_to_cpu(buffer[0]); |
| 642 | cip_header[1] = be32_to_cpu(buffer[1]); |
| 643 | |
| 644 | /* |
| 645 | * This module supports 'Two-quadlet CIP header with SYT field'. |
| 646 | * For convinience, also check FMT field is AM824 or not. |
| 647 | */ |
| 648 | if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || |
| 649 | ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) || |
| 650 | ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) { |
| 651 | dev_info_ratelimited(&s->unit->device, |
| 652 | "Invalid CIP header for AMDTP: %08X:%08X\n", |
| 653 | cip_header[0], cip_header[1]); |
| 654 | goto end; |
| 655 | } |
| 656 | |
| 657 | /* Calculate data blocks */ |
| 658 | if (payload_quadlets < 3 || |
| 659 | ((cip_header[1] & CIP_FDF_MASK) == |
| 660 | (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) { |
| 661 | data_blocks = 0; |
| 662 | } else { |
| 663 | data_block_quadlets = |
| 664 | (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT; |
| 665 | /* avoid division by zero */ |
| 666 | if (data_block_quadlets == 0) { |
| 667 | dev_info_ratelimited(&s->unit->device, |
| 668 | "Detect invalid value in dbs field: %08X\n", |
| 669 | cip_header[0]); |
| 670 | goto err; |
| 671 | } |
| 672 | |
| 673 | data_blocks = (payload_quadlets - 2) / data_block_quadlets; |
| 674 | } |
| 675 | |
| 676 | /* Check data block counter continuity */ |
| 677 | data_block_counter = cip_header[0] & AMDTP_DBC_MASK; |
| 678 | if (data_block_counter != s->data_block_counter) { |
| 679 | dev_info(&s->unit->device, |
| 680 | "Detect discontinuity of CIP: %02X %02X\n", |
| 681 | s->data_block_counter, data_block_counter); |
| 682 | goto err; |
| 683 | } |
| 684 | |
| 685 | if (data_blocks > 0) { |
| 686 | buffer += 2; |
| 687 | |
| 688 | pcm = ACCESS_ONCE(s->pcm); |
| 689 | if (pcm) |
| 690 | s->transfer_samples(s, pcm, buffer, data_blocks); |
| 691 | } |
| 692 | |
| 693 | s->data_block_counter = (data_block_counter + data_blocks) & 0xff; |
| 694 | end: |
| 695 | if (queue_in_packet(s) < 0) |
| 696 | goto err; |
| 697 | |
| 698 | if (pcm) |
| 699 | update_pcm_pointers(s, pcm, data_blocks); |
| 700 | |
| 701 | return; |
| 702 | err: |
| 703 | s->packet_index = -1; |
| 704 | amdtp_stream_pcm_abort(s); |
| 705 | } |
| 706 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 707 | static void out_stream_callback(struct fw_iso_context *context, u32 cycle, |
| 708 | size_t header_length, void *header, |
| 709 | void *private_data) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 710 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 711 | struct amdtp_stream *s = private_data; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 712 | unsigned int i, packets = header_length / 4; |
| 713 | |
| 714 | /* |
| 715 | * Compute the cycle of the last queued packet. |
| 716 | * (We need only the four lowest bits for the SYT, so we can ignore |
| 717 | * that bits 0-11 must wrap around at 3072.) |
| 718 | */ |
| 719 | cycle += QUEUE_LENGTH - packets; |
| 720 | |
| 721 | for (i = 0; i < packets; ++i) |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 722 | handle_out_packet(s, ++cycle); |
Clemens Ladisch | 13882a8 | 2011-05-02 09:33:56 +0200 | [diff] [blame] | 723 | fw_iso_context_queue_flush(s->context); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 726 | static void in_stream_callback(struct fw_iso_context *context, u32 cycle, |
| 727 | size_t header_length, void *header, |
| 728 | void *private_data) |
| 729 | { |
| 730 | struct amdtp_stream *s = private_data; |
| 731 | unsigned int p, packets, payload_quadlets; |
| 732 | __be32 *buffer, *headers = header; |
| 733 | |
| 734 | /* The number of packets in buffer */ |
| 735 | packets = header_length / IN_PACKET_HEADER_SIZE; |
| 736 | |
| 737 | for (p = 0; p < packets; p++) { |
| 738 | if (s->packet_index < 0) |
| 739 | return; |
| 740 | buffer = s->buffer.packets[s->packet_index].buffer; |
| 741 | |
| 742 | /* The number of quadlets in this packet */ |
| 743 | payload_quadlets = |
| 744 | (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4; |
| 745 | handle_in_packet(s, payload_quadlets, buffer); |
| 746 | } |
| 747 | |
| 748 | fw_iso_context_queue_flush(s->context); |
| 749 | } |
| 750 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 751 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 752 | * amdtp_stream_start - start transferring packets |
| 753 | * @s: the AMDTP stream to start |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 754 | * @channel: the isochronous channel on the bus |
| 755 | * @speed: firewire speed code |
| 756 | * |
| 757 | * The stream cannot be started until it has been configured with |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 758 | * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI |
| 759 | * device can be started. |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 760 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 761 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 762 | { |
| 763 | static const struct { |
| 764 | unsigned int data_block; |
| 765 | unsigned int syt_offset; |
| 766 | } initial_state[] = { |
| 767 | [CIP_SFC_32000] = { 4, 3072 }, |
| 768 | [CIP_SFC_48000] = { 6, 1024 }, |
| 769 | [CIP_SFC_96000] = { 12, 1024 }, |
| 770 | [CIP_SFC_192000] = { 24, 1024 }, |
| 771 | [CIP_SFC_44100] = { 0, 67 }, |
| 772 | [CIP_SFC_88200] = { 0, 67 }, |
| 773 | [CIP_SFC_176400] = { 0, 67 }, |
| 774 | }; |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 775 | unsigned int header_size; |
| 776 | enum dma_data_direction dir; |
| 777 | fw_iso_callback_t cb; |
| 778 | int type, err; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 779 | |
| 780 | mutex_lock(&s->mutex); |
| 781 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 782 | if (WARN_ON(amdtp_stream_running(s) || |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 783 | (s->data_block_quadlets < 1))) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 784 | err = -EBADFD; |
| 785 | goto err_unlock; |
| 786 | } |
| 787 | |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 788 | s->data_block_counter = 0; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 789 | s->data_block_state = initial_state[s->sfc].data_block; |
| 790 | s->syt_offset_state = initial_state[s->sfc].syt_offset; |
| 791 | s->last_syt_offset = TICKS_PER_CYCLE; |
| 792 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 793 | /* initialize packet buffer */ |
| 794 | if (s->direction == AMDTP_IN_STREAM) { |
| 795 | dir = DMA_FROM_DEVICE; |
| 796 | type = FW_ISO_CONTEXT_RECEIVE; |
| 797 | header_size = IN_PACKET_HEADER_SIZE; |
| 798 | cb = in_stream_callback; |
| 799 | } else { |
| 800 | dir = DMA_TO_DEVICE; |
| 801 | type = FW_ISO_CONTEXT_TRANSMIT; |
| 802 | header_size = OUT_PACKET_HEADER_SIZE; |
| 803 | cb = out_stream_callback; |
| 804 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 805 | err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 806 | amdtp_stream_get_max_payload(s), dir); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 807 | if (err < 0) |
| 808 | goto err_unlock; |
| 809 | |
| 810 | s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 811 | type, channel, speed, header_size, |
| 812 | cb, s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 813 | if (IS_ERR(s->context)) { |
| 814 | err = PTR_ERR(s->context); |
| 815 | if (err == -EBUSY) |
| 816 | dev_err(&s->unit->device, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 817 | "no free stream on this controller\n"); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 818 | goto err_buffer; |
| 819 | } |
| 820 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 821 | amdtp_stream_update(s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 822 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 823 | s->packet_index = 0; |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 824 | do { |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 825 | if (s->direction == AMDTP_IN_STREAM) |
| 826 | err = queue_in_packet(s); |
| 827 | else |
| 828 | err = queue_out_packet(s, 0, true); |
Takashi Sakamoto | 4b7da11 | 2014-04-25 22:44:45 +0900 | [diff] [blame] | 829 | if (err < 0) |
| 830 | goto err_context; |
| 831 | } while (s->packet_index > 0); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 832 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame^] | 833 | /* NOTE: TAG1 matches CIP. This just affects in stream. */ |
| 834 | err = fw_iso_context_start(s->context, -1, 0, |
| 835 | FW_ISO_CONTEXT_MATCH_TAG1); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 836 | if (err < 0) |
| 837 | goto err_context; |
| 838 | |
| 839 | mutex_unlock(&s->mutex); |
| 840 | |
| 841 | return 0; |
| 842 | |
| 843 | err_context: |
| 844 | fw_iso_context_destroy(s->context); |
| 845 | s->context = ERR_PTR(-1); |
| 846 | err_buffer: |
| 847 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 848 | err_unlock: |
| 849 | mutex_unlock(&s->mutex); |
| 850 | |
| 851 | return err; |
| 852 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 853 | EXPORT_SYMBOL(amdtp_stream_start); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 854 | |
| 855 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 856 | * amdtp_stream_pcm_pointer - get the PCM buffer position |
| 857 | * @s: the AMDTP stream that transports the PCM data |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 858 | * |
| 859 | * Returns the current buffer position, in frames. |
| 860 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 861 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 862 | { |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 863 | /* this optimization is allowed to be racy */ |
| 864 | if (s->pointer_flush) |
| 865 | fw_iso_context_flush_completions(s->context); |
| 866 | else |
| 867 | s->pointer_flush = true; |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 868 | |
| 869 | return ACCESS_ONCE(s->pcm_buffer_pointer); |
| 870 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 871 | EXPORT_SYMBOL(amdtp_stream_pcm_pointer); |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 872 | |
| 873 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 874 | * amdtp_stream_update - update the stream after a bus reset |
| 875 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 876 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 877 | void amdtp_stream_update(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 878 | { |
| 879 | ACCESS_ONCE(s->source_node_id_field) = |
| 880 | (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24; |
| 881 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 882 | EXPORT_SYMBOL(amdtp_stream_update); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 883 | |
| 884 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 885 | * amdtp_stream_stop - stop sending packets |
| 886 | * @s: the AMDTP stream to stop |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 887 | * |
| 888 | * All PCM and MIDI devices of the stream must be stopped before the stream |
| 889 | * itself can be stopped. |
| 890 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 891 | void amdtp_stream_stop(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 892 | { |
| 893 | mutex_lock(&s->mutex); |
| 894 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 895 | if (!amdtp_stream_running(s)) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 896 | mutex_unlock(&s->mutex); |
| 897 | return; |
| 898 | } |
| 899 | |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 900 | tasklet_kill(&s->period_tasklet); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 901 | fw_iso_context_stop(s->context); |
| 902 | fw_iso_context_destroy(s->context); |
| 903 | s->context = ERR_PTR(-1); |
| 904 | iso_packets_buffer_destroy(&s->buffer, s->unit); |
| 905 | |
| 906 | mutex_unlock(&s->mutex); |
| 907 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 908 | EXPORT_SYMBOL(amdtp_stream_stop); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 909 | |
| 910 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 911 | * amdtp_stream_pcm_abort - abort the running PCM device |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 912 | * @s: the AMDTP stream about to be stopped |
| 913 | * |
| 914 | * If the isochronous stream needs to be stopped asynchronously, call this |
| 915 | * function first to stop the PCM device. |
| 916 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 917 | void amdtp_stream_pcm_abort(struct amdtp_stream *s) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 918 | { |
| 919 | struct snd_pcm_substream *pcm; |
| 920 | |
| 921 | pcm = ACCESS_ONCE(s->pcm); |
| 922 | if (pcm) { |
| 923 | snd_pcm_stream_lock_irq(pcm); |
| 924 | if (snd_pcm_running(pcm)) |
| 925 | snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN); |
| 926 | snd_pcm_stream_unlock_irq(pcm); |
| 927 | } |
| 928 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 929 | EXPORT_SYMBOL(amdtp_stream_pcm_abort); |