Thomas Gleixner | da607e1 | 2019-05-29 16:57:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 2 | /* |
| 3 | * oxfw_stream.c - a part of driver for OXFW970/971 based devices |
| 4 | * |
| 5 | * Copyright (c) 2014 Takashi Sakamoto |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "oxfw.h" |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 9 | #include <linux/delay.h> |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 10 | |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 11 | #define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512 |
Takashi Sakamoto | cddcd54 | 2021-10-28 22:03:25 +0900 | [diff] [blame] | 12 | #define READY_TIMEOUT_MS 600 |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 13 | |
| 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 | */ |
| 19 | static 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 | */ |
| 32 | static 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 Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 41 | static 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); |
| 53 | end: |
| 54 | return err; |
| 55 | } |
| 56 | |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 57 | static 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 Carpenter | 5580ba7 | 2014-12-12 22:27:03 +0300 | [diff] [blame] | 63 | unsigned int len; |
| 64 | int i, err; |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 65 | |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 66 | 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 Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 73 | |
| 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 Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 88 | return set_rate(oxfw, rate); |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 89 | |
| 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 Sakamoto | 521b2e1 | 2019-06-12 17:44:15 +0900 | [diff] [blame] | 103 | static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 104 | { |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 105 | struct cmp_connection *conn; |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 106 | int err; |
| 107 | |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 108 | if (stream == &oxfw->rx_stream) |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 109 | conn = &oxfw->in_conn; |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 110 | else |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 111 | conn = &oxfw->out_conn; |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 112 | |
Takashi Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 113 | err = cmp_connection_establish(conn); |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 114 | if (err < 0) |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 115 | return err; |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 116 | |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 117 | err = amdtp_domain_add_stream(&oxfw->domain, stream, |
| 118 | conn->resources.channel, conn->speed); |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 119 | if (err < 0) { |
| 120 | cmp_connection_break(conn); |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 121 | return err; |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 122 | } |
| 123 | |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 124 | return 0; |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 127 | static int check_connection_used_by_others(struct snd_oxfw *oxfw, |
| 128 | struct amdtp_stream *stream) |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 129 | { |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 130 | 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 Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 151 | static int init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 152 | { |
| 153 | struct cmp_connection *conn; |
| 154 | enum cmp_direction c_dir; |
| 155 | enum amdtp_stream_direction s_dir; |
Takashi Sakamoto | 67bb66d | 2021-08-12 11:28:39 +0900 | [diff] [blame] | 156 | unsigned int flags = 0; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 157 | int err; |
| 158 | |
Takashi Sakamoto | 07a35edc | 2021-05-18 17:45:57 +0900 | [diff] [blame] | 159 | if (!(oxfw->quirks & SND_OXFW_QUIRK_BLOCKING_TRANSMISSION)) |
Takashi Sakamoto | 029ffc4 | 2021-05-31 11:51:00 +0900 | [diff] [blame] | 160 | flags |= CIP_NONBLOCKING; |
Takashi Sakamoto | 07a35edc | 2021-05-18 17:45:57 +0900 | [diff] [blame] | 161 | else |
Takashi Sakamoto | 029ffc4 | 2021-05-31 11:51:00 +0900 | [diff] [blame] | 162 | flags |= CIP_BLOCKING; |
Takashi Sakamoto | 07a35edc | 2021-05-18 17:45:57 +0900 | [diff] [blame] | 163 | |
Takashi Sakamoto | 67bb66d | 2021-08-12 11:28:39 +0900 | [diff] [blame] | 164 | // 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 Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 171 | if (stream == &oxfw->tx_stream) { |
| 172 | conn = &oxfw->out_conn; |
| 173 | c_dir = CMP_OUTPUT; |
| 174 | s_dir = AMDTP_IN_STREAM; |
Takashi Sakamoto | a092f00 | 2021-05-18 17:45:54 +0900 | [diff] [blame] | 175 | |
| 176 | if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD) |
| 177 | flags |= CIP_JUMBO_PAYLOAD; |
Takashi Sakamoto | a6f9169 | 2021-05-18 17:45:56 +0900 | [diff] [blame] | 178 | if (oxfw->quirks & SND_OXFW_QUIRK_WRONG_DBS) |
Takashi Sakamoto | a092f00 | 2021-05-18 17:45:54 +0900 | [diff] [blame] | 179 | flags |= CIP_WRONG_DBS; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 180 | } 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 Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 188 | return err; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 189 | |
Takashi Sakamoto | a092f00 | 2021-05-18 17:45:54 +0900 | [diff] [blame] | 190 | err = amdtp_am824_init(stream, oxfw->unit, s_dir, flags); |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 191 | if (err < 0) { |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 192 | cmp_connection_destroy(conn); |
Takashi Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 193 | return err; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 194 | } |
| 195 | |
Takashi Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 196 | return 0; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 197 | } |
| 198 | |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 199 | static 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 Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 204 | struct cmp_connection *conn; |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 205 | 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 Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 211 | conn = &oxfw->in_conn; |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 212 | } else { |
| 213 | dir = AVC_GENERAL_PLUG_DIR_OUT; |
| 214 | formats = oxfw->tx_stream_formats; |
Takashi Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 215 | conn = &oxfw->out_conn; |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 216 | } |
| 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 Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 243 | err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm, |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 244 | formation.midi * 8, false); |
Takashi Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 245 | if (err < 0) |
| 246 | return err; |
| 247 | |
| 248 | return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream)); |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 249 | } |
| 250 | |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 251 | int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw, |
| 252 | struct amdtp_stream *stream, |
Takashi Sakamoto | 1d6a722 | 2019-10-07 20:05:19 +0900 | [diff] [blame] | 253 | unsigned int rate, unsigned int pcm_channels, |
Takashi Sakamoto | 3299d2a | 2019-10-18 00:54:16 +0900 | [diff] [blame] | 254 | unsigned int frames_per_period, |
| 255 | unsigned int frames_per_buffer) |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 256 | { |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 257 | struct snd_oxfw_stream_formation formation; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 258 | enum avc_general_plug_dir dir; |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 259 | int err; |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 260 | |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 261 | // 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 Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 270 | } |
| 271 | |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 272 | if (stream == &oxfw->tx_stream) |
| 273 | dir = AVC_GENERAL_PLUG_DIR_OUT; |
| 274 | else |
| 275 | dir = AVC_GENERAL_PLUG_DIR_IN; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 276 | |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 277 | err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 278 | if (err < 0) |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 279 | return err; |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 280 | if (rate == 0) { |
Takashi Sakamoto | 05588d3 | 2014-12-09 00:10:48 +0900 | [diff] [blame] | 281 | rate = formation.rate; |
Takashi Sakamoto | 05588d3 | 2014-12-09 00:10:48 +0900 | [diff] [blame] | 282 | pcm_channels = formation.pcm; |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 283 | } |
| 284 | if (formation.rate != rate || formation.pcm != pcm_channels) { |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 285 | amdtp_domain_stop(&oxfw->domain); |
| 286 | |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 287 | cmp_connection_break(&oxfw->in_conn); |
Takashi Sakamoto | 3f2ce83 | 2019-06-18 22:26:22 +0900 | [diff] [blame] | 288 | cmp_connection_release(&oxfw->in_conn); |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 289 | |
| 290 | if (oxfw->has_output) { |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 291 | cmp_connection_break(&oxfw->out_conn); |
Takashi Sakamoto | 3f2ce83 | 2019-06-18 22:26:22 +0900 | [diff] [blame] | 292 | cmp_connection_release(&oxfw->out_conn); |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 293 | } |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 294 | } |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 295 | |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 296 | if (oxfw->substreams_count == 0 || |
| 297 | formation.rate != rate || formation.pcm != pcm_channels) { |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 298 | err = set_stream_format(oxfw, stream, rate, pcm_channels); |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 299 | if (err < 0) { |
| 300 | dev_err(&oxfw->unit->device, |
| 301 | "fail to set stream format: %d\n", err); |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 302 | return err; |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 303 | } |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 304 | |
| 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 Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 311 | if (err < 0) { |
| 312 | cmp_connection_release(&oxfw->in_conn); |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 313 | return err; |
Takashi Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 314 | } |
Takashi Sakamoto | 0356ce3 | 2019-06-12 17:44:22 +0900 | [diff] [blame] | 315 | } |
Takashi Sakamoto | 1d6a722 | 2019-10-07 20:05:19 +0900 | [diff] [blame] | 316 | |
| 317 | err = amdtp_domain_set_events_per_period(&oxfw->domain, |
Takashi Sakamoto | 3299d2a | 2019-10-18 00:54:16 +0900 | [diff] [blame] | 318 | frames_per_period, frames_per_buffer); |
Takashi Sakamoto | 1d6a722 | 2019-10-07 20:05:19 +0900 | [diff] [blame] | 319 | 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 Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 325 | } |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 326 | |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | int 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 Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 339 | amdtp_domain_stop(&oxfw->domain); |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 340 | |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 341 | cmp_connection_break(&oxfw->in_conn); |
| 342 | if (oxfw->has_output) |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 343 | cmp_connection_break(&oxfw->out_conn); |
Takashi Sakamoto | 4f380d0 | 2019-06-12 17:44:21 +0900 | [diff] [blame] | 344 | } |
| 345 | |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 346 | if (!amdtp_stream_running(&oxfw->rx_stream)) { |
Takashi Sakamoto | 029ffc4 | 2021-05-31 11:51:00 +0900 | [diff] [blame] | 347 | unsigned int tx_init_skip_cycles = 0; |
| 348 | bool replay_seq = false; |
| 349 | |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 350 | err = start_stream(oxfw, &oxfw->rx_stream); |
| 351 | if (err < 0) { |
| 352 | dev_err(&oxfw->unit->device, |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 353 | "fail to prepare rx stream: %d\n", err); |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 354 | goto error; |
| 355 | } |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 356 | |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 357 | if (oxfw->has_output && |
| 358 | !amdtp_stream_running(&oxfw->tx_stream)) { |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 359 | err = start_stream(oxfw, &oxfw->tx_stream); |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 360 | if (err < 0) { |
| 361 | dev_err(&oxfw->unit->device, |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 362 | "fail to prepare tx stream: %d\n", err); |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 363 | goto error; |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 364 | } |
Takashi Sakamoto | 029ffc4 | 2021-05-31 11:51:00 +0900 | [diff] [blame] | 365 | |
| 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 Sakamoto | cddcd54 | 2021-10-28 22:03:25 +0900 | [diff] [blame] | 370 | } 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 Sakamoto | 029ffc4 | 2021-05-31 11:51:00 +0900 | [diff] [blame] | 375 | } else { |
| 376 | replay_seq = true; |
| 377 | } |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 378 | } |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 379 | |
Takashi Sakamoto | 029ffc4 | 2021-05-31 11:51:00 +0900 | [diff] [blame] | 380 | // 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 Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 384 | if (err < 0) |
| 385 | goto error; |
| 386 | |
Takashi Sakamoto | bdaedca | 2021-05-20 13:01:54 +0900 | [diff] [blame] | 387 | if (!amdtp_domain_wait_ready(&oxfw->domain, READY_TIMEOUT_MS)) { |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 388 | err = -ETIMEDOUT; |
| 389 | goto error; |
| 390 | } |
Takashi Sakamoto | f3699e2 | 2014-12-09 00:10:44 +0900 | [diff] [blame] | 391 | } |
| 392 | |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 393 | return 0; |
| 394 | error: |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 395 | amdtp_domain_stop(&oxfw->domain); |
| 396 | |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 397 | cmp_connection_break(&oxfw->in_conn); |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 398 | if (oxfw->has_output) |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 399 | cmp_connection_break(&oxfw->out_conn); |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 400 | |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 401 | return err; |
| 402 | } |
| 403 | |
Takashi Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 404 | void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw) |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 405 | { |
Takashi Sakamoto | 4a0a047 | 2019-06-12 17:44:20 +0900 | [diff] [blame] | 406 | if (oxfw->substreams_count == 0) { |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 407 | amdtp_domain_stop(&oxfw->domain); |
| 408 | |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 409 | cmp_connection_break(&oxfw->in_conn); |
Takashi Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 410 | cmp_connection_release(&oxfw->in_conn); |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 411 | |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 412 | if (oxfw->has_output) { |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 413 | cmp_connection_break(&oxfw->out_conn); |
Takashi Sakamoto | 7bc9382 | 2019-06-15 18:11:01 +0900 | [diff] [blame] | 414 | cmp_connection_release(&oxfw->out_conn); |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 415 | } |
Takashi Sakamoto | 20358d4 | 2019-06-12 17:44:16 +0900 | [diff] [blame] | 416 | } |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 417 | } |
| 418 | |
Takashi Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 419 | static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 420 | { |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 421 | struct cmp_connection *conn; |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 422 | |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 423 | if (stream == &oxfw->tx_stream) |
| 424 | conn = &oxfw->out_conn; |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 425 | else |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 426 | conn = &oxfw->in_conn; |
| 427 | |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 428 | amdtp_stream_destroy(stream); |
| 429 | cmp_connection_destroy(conn); |
| 430 | } |
| 431 | |
Takashi Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 432 | int 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 Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 448 | 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 Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // This function should be called before starting the stream or after stopping |
| 459 | // the streams. |
| 460 | void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw) |
| 461 | { |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 462 | amdtp_domain_destroy(&oxfw->domain); |
| 463 | |
Takashi Sakamoto | 779f0db | 2019-06-12 17:44:19 +0900 | [diff] [blame] | 464 | destroy_stream(oxfw, &oxfw->rx_stream); |
| 465 | |
| 466 | if (oxfw->has_output) |
| 467 | destroy_stream(oxfw, &oxfw->tx_stream); |
| 468 | } |
| 469 | |
| 470 | void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw) |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 471 | { |
Takashi Sakamoto | ac5d778 | 2019-08-04 15:21:32 +0900 | [diff] [blame] | 472 | amdtp_domain_stop(&oxfw->domain); |
| 473 | |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 474 | cmp_connection_break(&oxfw->in_conn); |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 475 | |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 476 | amdtp_stream_pcm_abort(&oxfw->rx_stream); |
| 477 | |
| 478 | if (oxfw->has_output) { |
Takashi Sakamoto | e34244d | 2019-06-12 17:44:18 +0900 | [diff] [blame] | 479 | cmp_connection_break(&oxfw->out_conn); |
| 480 | |
| 481 | amdtp_stream_pcm_abort(&oxfw->tx_stream); |
| 482 | } |
Takashi Sakamoto | e2786ca | 2014-11-29 00:59:27 +0900 | [diff] [blame] | 483 | } |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 484 | |
Takashi Sakamoto | 3c96101 | 2014-12-09 00:10:43 +0900 | [diff] [blame] | 485 | int 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); |
| 507 | end: |
| 508 | kfree(format); |
| 509 | return err; |
| 510 | } |
| 511 | |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 512 | /* |
| 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 | */ |
| 518 | int 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 Sakamoto | 03be63b | 2020-01-13 16:34:16 +0900 | [diff] [blame] | 531 | return -ENXIO; |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 532 | |
| 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 Sakamoto | 03be63b | 2020-01-13 16:34:16 +0900 | [diff] [blame] | 539 | return -ENXIO; |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 540 | |
| 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 Sakamoto | 03be63b | 2020-01-13 16:34:16 +0900 | [diff] [blame] | 583 | return -ENXIO; /* not supported */ |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Takashi Sakamoto | 49c7b3f | 2015-09-19 11:22:01 +0900 | [diff] [blame] | 587 | if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM || |
| 588 | formation->midi > AM824_MAX_CHANNELS_FOR_MIDI) |
Takashi Sakamoto | 03be63b | 2020-01-13 16:34:16 +0900 | [diff] [blame] | 589 | return -ENXIO; |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int |
| 595 | assume_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 Sakamoto | cd3b711 | 2018-10-03 08:21:54 +0900 | [diff] [blame] | 619 | formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len, |
| 620 | GFP_KERNEL); |
| 621 | if (!formats[eid]) { |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 622 | err = -ENOMEM; |
| 623 | goto end; |
| 624 | } |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 625 | |
| 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 Sakamoto | cd3b711 | 2018-10-03 08:21:54 +0900 | [diff] [blame] | 638 | formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len, |
| 639 | GFP_KERNEL); |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 640 | if (formats[eid] == NULL) { |
| 641 | err = -ENOMEM; |
| 642 | goto end; |
| 643 | } |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 644 | formats[eid][2] = avc_stream_rate_table[i]; |
| 645 | } |
| 646 | |
| 647 | err = 0; |
| 648 | oxfw->assumed = true; |
| 649 | end: |
| 650 | return err; |
| 651 | } |
| 652 | |
| 653 | static 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 Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 666 | if (dir == AVC_GENERAL_PLUG_DIR_OUT) |
| 667 | formats = oxfw->tx_stream_formats; |
| 668 | else |
| 669 | formats = oxfw->rx_stream_formats; |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 670 | |
| 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 Sakamoto | 03be63b | 2020-01-13 16:34:16 +0900 | [diff] [blame] | 674 | if (err == -ENXIO) { |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 675 | /* 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 Sakamoto | cd3b711 | 2018-10-03 08:21:54 +0900 | [diff] [blame] | 701 | formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len, |
| 702 | GFP_KERNEL); |
| 703 | if (!formats[eid]) { |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 704 | err = -ENOMEM; |
| 705 | break; |
| 706 | } |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 707 | |
| 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 | } |
| 725 | end: |
| 726 | kfree(buf); |
| 727 | return err; |
| 728 | } |
| 729 | |
| 730 | int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) |
| 731 | { |
| 732 | u8 plugs[AVC_PLUG_INFO_BUF_BYTES]; |
Takashi Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 733 | struct snd_oxfw_stream_formation formation; |
| 734 | u8 *format; |
| 735 | unsigned int i; |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 736 | 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 Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 745 | } else if ((plugs[0] == 0) && (plugs[1] == 0)) { |
Takashi Sakamoto | 03be63b | 2020-01-13 16:34:16 +0900 | [diff] [blame] | 746 | err = -ENXIO; |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 747 | goto end; |
| 748 | } |
| 749 | |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 750 | /* use oPCR[0] if exists */ |
| 751 | if (plugs[1] > 0) { |
| 752 | err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0); |
Takashi Sakamoto | 41dbc79 | 2020-01-13 16:34:18 +0900 | [diff] [blame] | 753 | if (err < 0) { |
| 754 | if (err != -ENXIO) |
| 755 | return err; |
Takashi Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 756 | |
Takashi Sakamoto | 41dbc79 | 2020-01-13 16:34:18 +0900 | [diff] [blame] | 757 | // 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 Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 768 | |
Takashi Sakamoto | 41dbc79 | 2020-01-13 16:34:18 +0900 | [diff] [blame] | 769 | /* Add one MIDI port. */ |
| 770 | if (formation.midi > 0) |
| 771 | oxfw->midi_input_ports = 1; |
| 772 | } |
| 773 | |
| 774 | oxfw->has_output = true; |
Takashi Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 775 | } |
Takashi Sakamoto | b0ac000 | 2014-12-09 00:10:46 +0900 | [diff] [blame] | 776 | } |
| 777 | |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 778 | /* use iPCR[0] if exists */ |
Takashi Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 779 | if (plugs[0] > 0) { |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 780 | err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0); |
Takashi Sakamoto | 41dbc79 | 2020-01-13 16:34:18 +0900 | [diff] [blame] | 781 | if (err < 0) { |
| 782 | if (err != -ENXIO) |
| 783 | return err; |
Takashi Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 784 | |
Takashi Sakamoto | 41dbc79 | 2020-01-13 16:34:18 +0900 | [diff] [blame] | 785 | // 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 Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 796 | |
Takashi Sakamoto | 41dbc79 | 2020-01-13 16:34:18 +0900 | [diff] [blame] | 797 | /* Add one MIDI port. */ |
| 798 | if (formation.midi > 0) |
| 799 | oxfw->midi_output_ports = 1; |
| 800 | } |
| 801 | |
| 802 | oxfw->has_input = true; |
Takashi Sakamoto | 3205604 | 2015-10-18 17:09:38 +0900 | [diff] [blame] | 803 | } |
| 804 | } |
Takashi Sakamoto | 5cd1d3f | 2014-12-09 00:10:42 +0900 | [diff] [blame] | 805 | end: |
| 806 | return err; |
| 807 | } |
Takashi Sakamoto | 8985f4a | 2014-12-09 00:10:49 +0900 | [diff] [blame] | 808 | |
| 809 | void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw) |
| 810 | { |
| 811 | oxfw->dev_lock_changed = true; |
| 812 | wake_up(&oxfw->hwdep_wait); |
| 813 | } |
| 814 | |
| 815 | int 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; |
| 831 | end: |
| 832 | spin_unlock_irq(&oxfw->lock); |
| 833 | return err; |
| 834 | } |
| 835 | |
| 836 | void 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); |
| 844 | end: |
| 845 | spin_unlock_irq(&oxfw->lock); |
| 846 | } |