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