blob: f4a702def3979437faf2684a77351bb2a07c7600 [file] [log] [blame]
Thomas Gleixnerda607e12019-05-29 16:57:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +09002/*
3 * oxfw_stream.c - a part of driver for OXFW970/971 based devices
4 *
5 * Copyright (c) 2014 Takashi Sakamoto
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +09006 */
7
8#include "oxfw.h"
Takashi Sakamotof3699e22014-12-09 00:10:44 +09009#include <linux/delay.h>
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +090010
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090011#define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512
Takashi Sakamotocddcd542021-10-28 22:03:25 +090012#define READY_TIMEOUT_MS 600
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090013
14/*
15 * According to datasheet of Oxford Semiconductor:
16 * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
17 * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
18 */
19static const unsigned int oxfw_rate_table[] = {
20 [0] = 32000,
21 [1] = 44100,
22 [2] = 48000,
23 [3] = 88200,
24 [4] = 96000,
25 [5] = 192000,
26};
27
28/*
29 * See Table 5.7 – Sampling frequency for Multi-bit Audio
30 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
31 */
32static const unsigned int avc_stream_rate_table[] = {
33 [0] = 0x02,
34 [1] = 0x03,
35 [2] = 0x04,
36 [3] = 0x0a,
37 [4] = 0x05,
38 [5] = 0x07,
39};
40
Takashi Sakamotob0ac0002014-12-09 00:10:46 +090041static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
42{
43 int err;
44
45 err = avc_general_set_sig_fmt(oxfw->unit, rate,
46 AVC_GENERAL_PLUG_DIR_IN, 0);
47 if (err < 0)
48 goto end;
49
50 if (oxfw->has_output)
51 err = avc_general_set_sig_fmt(oxfw->unit, rate,
52 AVC_GENERAL_PLUG_DIR_OUT, 0);
53end:
54 return err;
55}
56
Takashi Sakamotof3699e22014-12-09 00:10:44 +090057static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
58 unsigned int rate, unsigned int pcm_channels)
59{
60 u8 **formats;
61 struct snd_oxfw_stream_formation formation;
62 enum avc_general_plug_dir dir;
Dan Carpenter5580ba72014-12-12 22:27:03 +030063 unsigned int len;
64 int i, err;
Takashi Sakamotof3699e22014-12-09 00:10:44 +090065
Takashi Sakamotob0ac0002014-12-09 00:10:46 +090066 if (s == &oxfw->tx_stream) {
67 formats = oxfw->tx_stream_formats;
68 dir = AVC_GENERAL_PLUG_DIR_OUT;
69 } else {
70 formats = oxfw->rx_stream_formats;
71 dir = AVC_GENERAL_PLUG_DIR_IN;
72 }
Takashi Sakamotof3699e22014-12-09 00:10:44 +090073
74 /* Seek stream format for requirements. */
75 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
76 err = snd_oxfw_stream_parse_format(formats[i], &formation);
77 if (err < 0)
78 return err;
79
80 if ((formation.rate == rate) && (formation.pcm == pcm_channels))
81 break;
82 }
83 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
84 return -EINVAL;
85
86 /* If assumed, just change rate. */
87 if (oxfw->assumed)
Takashi Sakamotob0ac0002014-12-09 00:10:46 +090088 return set_rate(oxfw, rate);
Takashi Sakamotof3699e22014-12-09 00:10:44 +090089
90 /* Calculate format length. */
91 len = 5 + formats[i][4] * 2;
92
93 err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
94 if (err < 0)
95 return err;
96
97 /* Some requests just after changing format causes freezing. */
98 msleep(100);
99
100 return 0;
101}
102
Takashi Sakamoto521b2e12019-06-12 17:44:15 +0900103static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900104{
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900105 struct cmp_connection *conn;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900106 int err;
107
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900108 if (stream == &oxfw->rx_stream)
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900109 conn = &oxfw->in_conn;
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900110 else
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900111 conn = &oxfw->out_conn;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900112
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900113 err = cmp_connection_establish(conn);
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900114 if (err < 0)
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900115 return err;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900116
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900117 err = amdtp_domain_add_stream(&oxfw->domain, stream,
118 conn->resources.channel, conn->speed);
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900119 if (err < 0) {
120 cmp_connection_break(conn);
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900121 return err;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900122 }
123
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900124 return 0;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900125}
126
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900127static int check_connection_used_by_others(struct snd_oxfw *oxfw,
128 struct amdtp_stream *stream)
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900129{
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900130 struct cmp_connection *conn;
131 bool used;
132 int err;
133
134 if (stream == &oxfw->tx_stream)
135 conn = &oxfw->out_conn;
136 else
137 conn = &oxfw->in_conn;
138
139 err = cmp_connection_check_used(conn, &used);
140 if ((err >= 0) && used && !amdtp_stream_running(stream)) {
141 dev_err(&oxfw->unit->device,
142 "Connection established by others: %cPCR[%d]\n",
143 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
144 conn->pcr_index);
145 err = -EBUSY;
146 }
147
148 return err;
149}
150
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900151static int init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900152{
153 struct cmp_connection *conn;
154 enum cmp_direction c_dir;
155 enum amdtp_stream_direction s_dir;
Takashi Sakamoto67bb66d2021-08-12 11:28:39 +0900156 unsigned int flags = 0;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900157 int err;
158
Takashi Sakamoto07a35edc2021-05-18 17:45:57 +0900159 if (!(oxfw->quirks & SND_OXFW_QUIRK_BLOCKING_TRANSMISSION))
Takashi Sakamoto029ffc42021-05-31 11:51:00 +0900160 flags |= CIP_NONBLOCKING;
Takashi Sakamoto07a35edc2021-05-18 17:45:57 +0900161 else
Takashi Sakamoto029ffc42021-05-31 11:51:00 +0900162 flags |= CIP_BLOCKING;
Takashi Sakamoto07a35edc2021-05-18 17:45:57 +0900163
Takashi Sakamoto67bb66d2021-08-12 11:28:39 +0900164 // OXFW 970/971 has no function to generate playback timing according to the sequence
165 // of value in syt field, thus the packet should include NO_INFO value in the field.
166 // However, some models just ignore data blocks in packet with NO_INFO for audio data
167 // processing.
168 if (!(oxfw->quirks & SND_OXFW_QUIRK_IGNORE_NO_INFO_PACKET))
169 flags |= CIP_UNAWARE_SYT;
170
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900171 if (stream == &oxfw->tx_stream) {
172 conn = &oxfw->out_conn;
173 c_dir = CMP_OUTPUT;
174 s_dir = AMDTP_IN_STREAM;
Takashi Sakamotoa092f002021-05-18 17:45:54 +0900175
176 if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD)
177 flags |= CIP_JUMBO_PAYLOAD;
Takashi Sakamotoa6f91692021-05-18 17:45:56 +0900178 if (oxfw->quirks & SND_OXFW_QUIRK_WRONG_DBS)
Takashi Sakamotoa092f002021-05-18 17:45:54 +0900179 flags |= CIP_WRONG_DBS;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900180 } else {
181 conn = &oxfw->in_conn;
182 c_dir = CMP_INPUT;
183 s_dir = AMDTP_OUT_STREAM;
184 }
185
186 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
187 if (err < 0)
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900188 return err;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900189
Takashi Sakamotoa092f002021-05-18 17:45:54 +0900190 err = amdtp_am824_init(stream, oxfw->unit, s_dir, flags);
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900191 if (err < 0) {
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900192 cmp_connection_destroy(conn);
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900193 return err;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900194 }
195
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900196 return 0;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900197}
198
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900199static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
200{
201 enum avc_general_plug_dir dir;
202 u8 **formats;
203 struct snd_oxfw_stream_formation formation;
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900204 struct cmp_connection *conn;
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900205 int i;
206 int err;
207
208 if (stream == &oxfw->rx_stream) {
209 dir = AVC_GENERAL_PLUG_DIR_IN;
210 formats = oxfw->rx_stream_formats;
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900211 conn = &oxfw->in_conn;
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900212 } else {
213 dir = AVC_GENERAL_PLUG_DIR_OUT;
214 formats = oxfw->tx_stream_formats;
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900215 conn = &oxfw->out_conn;
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900216 }
217
218 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
219 if (err < 0)
220 return err;
221
222 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
223 struct snd_oxfw_stream_formation fmt;
224
225 if (formats[i] == NULL)
226 break;
227
228 err = snd_oxfw_stream_parse_format(formats[i], &fmt);
229 if (err < 0)
230 return err;
231
232 if (fmt.rate == formation.rate && fmt.pcm == formation.pcm &&
233 fmt.midi == formation.midi)
234 break;
235 }
236 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
237 return -EINVAL;
238
239 // The stream should have one pcm channels at least.
240 if (formation.pcm == 0)
241 return -EINVAL;
242
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900243 err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm,
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900244 formation.midi * 8, false);
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900245 if (err < 0)
246 return err;
247
248 return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900249}
250
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900251int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
252 struct amdtp_stream *stream,
Takashi Sakamoto1d6a7222019-10-07 20:05:19 +0900253 unsigned int rate, unsigned int pcm_channels,
Takashi Sakamoto3299d2a2019-10-18 00:54:16 +0900254 unsigned int frames_per_period,
255 unsigned int frames_per_buffer)
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900256{
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900257 struct snd_oxfw_stream_formation formation;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900258 enum avc_general_plug_dir dir;
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900259 int err;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900260
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900261 // Considering JACK/FFADO streaming:
262 // TODO: This can be removed hwdep functionality becomes popular.
263 err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
264 if (err < 0)
265 return err;
266 if (oxfw->has_output) {
267 err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
268 if (err < 0)
269 return err;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900270 }
271
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900272 if (stream == &oxfw->tx_stream)
273 dir = AVC_GENERAL_PLUG_DIR_OUT;
274 else
275 dir = AVC_GENERAL_PLUG_DIR_IN;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900276
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900277 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900278 if (err < 0)
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900279 return err;
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900280 if (rate == 0) {
Takashi Sakamoto05588d32014-12-09 00:10:48 +0900281 rate = formation.rate;
Takashi Sakamoto05588d32014-12-09 00:10:48 +0900282 pcm_channels = formation.pcm;
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900283 }
284 if (formation.rate != rate || formation.pcm != pcm_channels) {
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900285 amdtp_domain_stop(&oxfw->domain);
286
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900287 cmp_connection_break(&oxfw->in_conn);
Takashi Sakamoto3f2ce832019-06-18 22:26:22 +0900288 cmp_connection_release(&oxfw->in_conn);
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900289
290 if (oxfw->has_output) {
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900291 cmp_connection_break(&oxfw->out_conn);
Takashi Sakamoto3f2ce832019-06-18 22:26:22 +0900292 cmp_connection_release(&oxfw->out_conn);
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900293 }
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900294 }
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900295
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900296 if (oxfw->substreams_count == 0 ||
297 formation.rate != rate || formation.pcm != pcm_channels) {
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900298 err = set_stream_format(oxfw, stream, rate, pcm_channels);
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900299 if (err < 0) {
300 dev_err(&oxfw->unit->device,
301 "fail to set stream format: %d\n", err);
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900302 return err;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900303 }
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900304
305 err = keep_resources(oxfw, &oxfw->rx_stream);
306 if (err < 0)
307 return err;
308
309 if (oxfw->has_output) {
310 err = keep_resources(oxfw, &oxfw->tx_stream);
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900311 if (err < 0) {
312 cmp_connection_release(&oxfw->in_conn);
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900313 return err;
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900314 }
Takashi Sakamoto0356ce32019-06-12 17:44:22 +0900315 }
Takashi Sakamoto1d6a7222019-10-07 20:05:19 +0900316
317 err = amdtp_domain_set_events_per_period(&oxfw->domain,
Takashi Sakamoto3299d2a2019-10-18 00:54:16 +0900318 frames_per_period, frames_per_buffer);
Takashi Sakamoto1d6a7222019-10-07 20:05:19 +0900319 if (err < 0) {
320 cmp_connection_release(&oxfw->in_conn);
321 if (oxfw->has_output)
322 cmp_connection_release(&oxfw->out_conn);
323 return err;
324 }
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900325 }
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900326
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900327 return 0;
328}
329
330int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
331{
332 int err;
333
334 if (oxfw->substreams_count == 0)
335 return -EIO;
336
337 if (amdtp_streaming_error(&oxfw->rx_stream) ||
338 amdtp_streaming_error(&oxfw->tx_stream)) {
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900339 amdtp_domain_stop(&oxfw->domain);
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900340
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900341 cmp_connection_break(&oxfw->in_conn);
342 if (oxfw->has_output)
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900343 cmp_connection_break(&oxfw->out_conn);
Takashi Sakamoto4f380d02019-06-12 17:44:21 +0900344 }
345
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900346 if (!amdtp_stream_running(&oxfw->rx_stream)) {
Takashi Sakamoto029ffc42021-05-31 11:51:00 +0900347 unsigned int tx_init_skip_cycles = 0;
348 bool replay_seq = false;
349
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900350 err = start_stream(oxfw, &oxfw->rx_stream);
351 if (err < 0) {
352 dev_err(&oxfw->unit->device,
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900353 "fail to prepare rx stream: %d\n", err);
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900354 goto error;
355 }
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900356
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900357 if (oxfw->has_output &&
358 !amdtp_stream_running(&oxfw->tx_stream)) {
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900359 err = start_stream(oxfw, &oxfw->tx_stream);
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900360 if (err < 0) {
361 dev_err(&oxfw->unit->device,
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900362 "fail to prepare tx stream: %d\n", err);
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900363 goto error;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900364 }
Takashi Sakamoto029ffc42021-05-31 11:51:00 +0900365
366 if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD) {
367 // Just after changing sampling transfer frequency, many cycles are
368 // skipped for packet transmission.
369 tx_init_skip_cycles = 400;
Takashi Sakamotocddcd542021-10-28 22:03:25 +0900370 } else if (oxfw->quirks & SND_OXFW_QUIRK_VOLUNTARY_RECOVERY) {
371 // It takes a bit time for target device to adjust event frequency
372 // according to nominal event frequency in isochronous packets from
373 // ALSA oxfw driver.
374 tx_init_skip_cycles = 4000;
Takashi Sakamoto029ffc42021-05-31 11:51:00 +0900375 } else {
376 replay_seq = true;
377 }
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900378 }
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900379
Takashi Sakamoto029ffc42021-05-31 11:51:00 +0900380 // NOTE: The device ignores presentation time expressed by the value of syt field
381 // of CIP header in received packets. The sequence of the number of data blocks per
382 // packet is important for media clock recovery.
383 err = amdtp_domain_start(&oxfw->domain, tx_init_skip_cycles, replay_seq, false);
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900384 if (err < 0)
385 goto error;
386
Takashi Sakamotobdaedca2021-05-20 13:01:54 +0900387 if (!amdtp_domain_wait_ready(&oxfw->domain, READY_TIMEOUT_MS)) {
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900388 err = -ETIMEDOUT;
389 goto error;
390 }
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900391 }
392
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900393 return 0;
394error:
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900395 amdtp_domain_stop(&oxfw->domain);
396
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900397 cmp_connection_break(&oxfw->in_conn);
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900398 if (oxfw->has_output)
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900399 cmp_connection_break(&oxfw->out_conn);
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900400
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900401 return err;
402}
403
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900404void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900405{
Takashi Sakamoto4a0a0472019-06-12 17:44:20 +0900406 if (oxfw->substreams_count == 0) {
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900407 amdtp_domain_stop(&oxfw->domain);
408
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900409 cmp_connection_break(&oxfw->in_conn);
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900410 cmp_connection_release(&oxfw->in_conn);
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900411
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900412 if (oxfw->has_output) {
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900413 cmp_connection_break(&oxfw->out_conn);
Takashi Sakamoto7bc93822019-06-15 18:11:01 +0900414 cmp_connection_release(&oxfw->out_conn);
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900415 }
Takashi Sakamoto20358d42019-06-12 17:44:16 +0900416 }
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900417}
418
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900419static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900420{
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900421 struct cmp_connection *conn;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900422
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900423 if (stream == &oxfw->tx_stream)
424 conn = &oxfw->out_conn;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900425 else
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900426 conn = &oxfw->in_conn;
427
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900428 amdtp_stream_destroy(stream);
429 cmp_connection_destroy(conn);
430}
431
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900432int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
433{
434 int err;
435
436 err = init_stream(oxfw, &oxfw->rx_stream);
437 if (err < 0)
438 return err;
439
440 if (oxfw->has_output) {
441 err = init_stream(oxfw, &oxfw->tx_stream);
442 if (err < 0) {
443 destroy_stream(oxfw, &oxfw->rx_stream);
444 return err;
445 }
446 }
447
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900448 err = amdtp_domain_init(&oxfw->domain);
449 if (err < 0) {
450 destroy_stream(oxfw, &oxfw->rx_stream);
451 if (oxfw->has_output)
452 destroy_stream(oxfw, &oxfw->tx_stream);
453 }
454
455 return err;
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900456}
457
458// This function should be called before starting the stream or after stopping
459// the streams.
460void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
461{
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900462 amdtp_domain_destroy(&oxfw->domain);
463
Takashi Sakamoto779f0db2019-06-12 17:44:19 +0900464 destroy_stream(oxfw, &oxfw->rx_stream);
465
466 if (oxfw->has_output)
467 destroy_stream(oxfw, &oxfw->tx_stream);
468}
469
470void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900471{
Takashi Sakamotoac5d7782019-08-04 15:21:32 +0900472 amdtp_domain_stop(&oxfw->domain);
473
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900474 cmp_connection_break(&oxfw->in_conn);
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900475
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900476 amdtp_stream_pcm_abort(&oxfw->rx_stream);
477
478 if (oxfw->has_output) {
Takashi Sakamotoe34244d2019-06-12 17:44:18 +0900479 cmp_connection_break(&oxfw->out_conn);
480
481 amdtp_stream_pcm_abort(&oxfw->tx_stream);
482 }
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900483}
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900484
Takashi Sakamoto3c961012014-12-09 00:10:43 +0900485int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
486 enum avc_general_plug_dir dir,
487 struct snd_oxfw_stream_formation *formation)
488{
489 u8 *format;
490 unsigned int len;
491 int err;
492
493 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
494 format = kmalloc(len, GFP_KERNEL);
495 if (format == NULL)
496 return -ENOMEM;
497
498 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
499 if (err < 0)
500 goto end;
501 if (len < 3) {
502 err = -EIO;
503 goto end;
504 }
505
506 err = snd_oxfw_stream_parse_format(format, formation);
507end:
508 kfree(format);
509 return err;
510}
511
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900512/*
513 * See Table 6.16 - AM824 Stream Format
514 * Figure 6.19 - format_information field for AM824 Compound
515 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
516 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
517 */
518int snd_oxfw_stream_parse_format(u8 *format,
519 struct snd_oxfw_stream_formation *formation)
520{
521 unsigned int i, e, channels, type;
522
523 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
524
525 /*
526 * this module can support a hierarchy combination that:
527 * Root: Audio and Music (0x90)
528 * Level 1: AM824 Compound (0x40)
529 */
530 if ((format[0] != 0x90) || (format[1] != 0x40))
Takashi Sakamoto03be63b2020-01-13 16:34:16 +0900531 return -ENXIO;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900532
533 /* check the sampling rate */
534 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
535 if (format[2] == avc_stream_rate_table[i])
536 break;
537 }
538 if (i == ARRAY_SIZE(avc_stream_rate_table))
Takashi Sakamoto03be63b2020-01-13 16:34:16 +0900539 return -ENXIO;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900540
541 formation->rate = oxfw_rate_table[i];
542
543 for (e = 0; e < format[4]; e++) {
544 channels = format[5 + e * 2];
545 type = format[6 + e * 2];
546
547 switch (type) {
548 /* IEC 60958 Conformant, currently handled as MBLA */
549 case 0x00:
550 /* Multi Bit Linear Audio (Raw) */
551 case 0x06:
552 formation->pcm += channels;
553 break;
554 /* MIDI Conformant */
555 case 0x0d:
556 formation->midi = channels;
557 break;
558 /* IEC 61937-3 to 7 */
559 case 0x01:
560 case 0x02:
561 case 0x03:
562 case 0x04:
563 case 0x05:
564 /* Multi Bit Linear Audio */
565 case 0x07: /* DVD-Audio */
566 case 0x0c: /* High Precision */
567 /* One Bit Audio */
568 case 0x08: /* (Plain) Raw */
569 case 0x09: /* (Plain) SACD */
570 case 0x0a: /* (Encoded) Raw */
571 case 0x0b: /* (Encoded) SACD */
572 /* SMPTE Time-Code conformant */
573 case 0x0e:
574 /* Sample Count */
575 case 0x0f:
576 /* Anciliary Data */
577 case 0x10:
578 /* Synchronization Stream (Stereo Raw audio) */
579 case 0x40:
580 /* Don't care */
581 case 0xff:
582 default:
Takashi Sakamoto03be63b2020-01-13 16:34:16 +0900583 return -ENXIO; /* not supported */
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900584 }
585 }
586
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900587 if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM ||
588 formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
Takashi Sakamoto03be63b2020-01-13 16:34:16 +0900589 return -ENXIO;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900590
591 return 0;
592}
593
594static int
595assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
596 unsigned int pid, u8 *buf, unsigned int *len,
597 u8 **formats)
598{
599 struct snd_oxfw_stream_formation formation;
600 unsigned int i, eid;
601 int err;
602
603 /* get format at current sampling rate */
604 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
605 if (err < 0) {
606 dev_err(&oxfw->unit->device,
607 "fail to get current stream format for isoc %s plug %d:%d\n",
608 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
609 pid, err);
610 goto end;
611 }
612
613 /* parse and set stream format */
614 eid = 0;
615 err = snd_oxfw_stream_parse_format(buf, &formation);
616 if (err < 0)
617 goto end;
618
Takashi Sakamotocd3b7112018-10-03 08:21:54 +0900619 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
620 GFP_KERNEL);
621 if (!formats[eid]) {
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900622 err = -ENOMEM;
623 goto end;
624 }
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900625
626 /* apply the format for each available sampling rate */
627 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
628 if (formation.rate == oxfw_rate_table[i])
629 continue;
630
631 err = avc_general_inquiry_sig_fmt(oxfw->unit,
632 oxfw_rate_table[i],
633 dir, pid);
634 if (err < 0)
635 continue;
636
637 eid++;
Takashi Sakamotocd3b7112018-10-03 08:21:54 +0900638 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
639 GFP_KERNEL);
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900640 if (formats[eid] == NULL) {
641 err = -ENOMEM;
642 goto end;
643 }
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900644 formats[eid][2] = avc_stream_rate_table[i];
645 }
646
647 err = 0;
648 oxfw->assumed = true;
649end:
650 return err;
651}
652
653static int fill_stream_formats(struct snd_oxfw *oxfw,
654 enum avc_general_plug_dir dir,
655 unsigned short pid)
656{
657 u8 *buf, **formats;
658 unsigned int len, eid = 0;
659 struct snd_oxfw_stream_formation dummy;
660 int err;
661
662 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
663 if (buf == NULL)
664 return -ENOMEM;
665
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900666 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
667 formats = oxfw->tx_stream_formats;
668 else
669 formats = oxfw->rx_stream_formats;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900670
671 /* get first entry */
672 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
673 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
Takashi Sakamoto03be63b2020-01-13 16:34:16 +0900674 if (err == -ENXIO) {
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900675 /* LIST subfunction is not implemented */
676 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
677 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
678 formats);
679 goto end;
680 } else if (err < 0) {
681 dev_err(&oxfw->unit->device,
682 "fail to get stream format %d for isoc %s plug %d:%d\n",
683 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
684 pid, err);
685 goto end;
686 }
687
688 /* LIST subfunction is implemented */
689 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
690 /* The format is too short. */
691 if (len < 3) {
692 err = -EIO;
693 break;
694 }
695
696 /* parse and set stream format */
697 err = snd_oxfw_stream_parse_format(buf, &dummy);
698 if (err < 0)
699 break;
700
Takashi Sakamotocd3b7112018-10-03 08:21:54 +0900701 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
702 GFP_KERNEL);
703 if (!formats[eid]) {
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900704 err = -ENOMEM;
705 break;
706 }
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900707
708 /* get next entry */
709 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
710 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
711 buf, &len, ++eid);
712 /* No entries remained. */
713 if (err == -EINVAL) {
714 err = 0;
715 break;
716 } else if (err < 0) {
717 dev_err(&oxfw->unit->device,
718 "fail to get stream format %d for isoc %s plug %d:%d\n",
719 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
720 "out",
721 pid, err);
722 break;
723 }
724 }
725end:
726 kfree(buf);
727 return err;
728}
729
730int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
731{
732 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
Takashi Sakamoto32056042015-10-18 17:09:38 +0900733 struct snd_oxfw_stream_formation formation;
734 u8 *format;
735 unsigned int i;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900736 int err;
737
738 /* the number of plugs for isoc in/out, ext in/out */
739 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
740 if (err < 0) {
741 dev_err(&oxfw->unit->device,
742 "fail to get info for isoc/external in/out plugs: %d\n",
743 err);
744 goto end;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900745 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
Takashi Sakamoto03be63b2020-01-13 16:34:16 +0900746 err = -ENXIO;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900747 goto end;
748 }
749
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900750 /* use oPCR[0] if exists */
751 if (plugs[1] > 0) {
752 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
Takashi Sakamoto41dbc792020-01-13 16:34:18 +0900753 if (err < 0) {
754 if (err != -ENXIO)
755 return err;
Takashi Sakamoto32056042015-10-18 17:09:38 +0900756
Takashi Sakamoto41dbc792020-01-13 16:34:18 +0900757 // The oPCR is not available for isoc communication.
758 err = 0;
759 } else {
760 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
761 format = oxfw->tx_stream_formats[i];
762 if (format == NULL)
763 continue;
764 err = snd_oxfw_stream_parse_format(format,
765 &formation);
766 if (err < 0)
767 continue;
Takashi Sakamoto32056042015-10-18 17:09:38 +0900768
Takashi Sakamoto41dbc792020-01-13 16:34:18 +0900769 /* Add one MIDI port. */
770 if (formation.midi > 0)
771 oxfw->midi_input_ports = 1;
772 }
773
774 oxfw->has_output = true;
Takashi Sakamoto32056042015-10-18 17:09:38 +0900775 }
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900776 }
777
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900778 /* use iPCR[0] if exists */
Takashi Sakamoto32056042015-10-18 17:09:38 +0900779 if (plugs[0] > 0) {
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900780 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
Takashi Sakamoto41dbc792020-01-13 16:34:18 +0900781 if (err < 0) {
782 if (err != -ENXIO)
783 return err;
Takashi Sakamoto32056042015-10-18 17:09:38 +0900784
Takashi Sakamoto41dbc792020-01-13 16:34:18 +0900785 // The iPCR is not available for isoc communication.
786 err = 0;
787 } else {
788 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
789 format = oxfw->rx_stream_formats[i];
790 if (format == NULL)
791 continue;
792 err = snd_oxfw_stream_parse_format(format,
793 &formation);
794 if (err < 0)
795 continue;
Takashi Sakamoto32056042015-10-18 17:09:38 +0900796
Takashi Sakamoto41dbc792020-01-13 16:34:18 +0900797 /* Add one MIDI port. */
798 if (formation.midi > 0)
799 oxfw->midi_output_ports = 1;
800 }
801
802 oxfw->has_input = true;
Takashi Sakamoto32056042015-10-18 17:09:38 +0900803 }
804 }
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900805end:
806 return err;
807}
Takashi Sakamoto8985f4a2014-12-09 00:10:49 +0900808
809void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
810{
811 oxfw->dev_lock_changed = true;
812 wake_up(&oxfw->hwdep_wait);
813}
814
815int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
816{
817 int err;
818
819 spin_lock_irq(&oxfw->lock);
820
821 /* user land lock this */
822 if (oxfw->dev_lock_count < 0) {
823 err = -EBUSY;
824 goto end;
825 }
826
827 /* this is the first time */
828 if (oxfw->dev_lock_count++ == 0)
829 snd_oxfw_stream_lock_changed(oxfw);
830 err = 0;
831end:
832 spin_unlock_irq(&oxfw->lock);
833 return err;
834}
835
836void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
837{
838 spin_lock_irq(&oxfw->lock);
839
840 if (WARN_ON(oxfw->dev_lock_count <= 0))
841 goto end;
842 if (--oxfw->dev_lock_count == 0)
843 snd_oxfw_stream_lock_changed(oxfw);
844end:
845 spin_unlock_irq(&oxfw->lock);
846}