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