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