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