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