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