Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * fireworks_stream.c - a part of driver for Fireworks based devices |
| 3 | * |
| 4 | * Copyright (c) 2013-2014 Takashi Sakamoto |
| 5 | * |
| 6 | * Licensed under the terms of the GNU General Public License, version 2. |
| 7 | */ |
| 8 | #include "./fireworks.h" |
| 9 | |
| 10 | #define CALLBACK_TIMEOUT 100 |
| 11 | |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 12 | static int |
| 13 | init_stream(struct snd_efw *efw, struct amdtp_stream *stream) |
| 14 | { |
| 15 | struct cmp_connection *conn; |
| 16 | enum cmp_direction c_dir; |
| 17 | enum amdtp_stream_direction s_dir; |
| 18 | int err; |
| 19 | |
| 20 | if (stream == &efw->tx_stream) { |
| 21 | conn = &efw->out_conn; |
| 22 | c_dir = CMP_OUTPUT; |
| 23 | s_dir = AMDTP_IN_STREAM; |
| 24 | } else { |
| 25 | conn = &efw->in_conn; |
| 26 | c_dir = CMP_INPUT; |
| 27 | s_dir = AMDTP_OUT_STREAM; |
| 28 | } |
| 29 | |
| 30 | err = cmp_connection_init(conn, efw->unit, c_dir, 0); |
| 31 | if (err < 0) |
| 32 | goto end; |
| 33 | |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 34 | err = amdtp_am824_init(stream, efw->unit, s_dir, CIP_BLOCKING); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 35 | if (err < 0) { |
| 36 | amdtp_stream_destroy(stream); |
| 37 | cmp_connection_destroy(conn); |
| 38 | } |
| 39 | end: |
| 40 | return err; |
| 41 | } |
| 42 | |
| 43 | static void |
| 44 | stop_stream(struct snd_efw *efw, struct amdtp_stream *stream) |
| 45 | { |
| 46 | amdtp_stream_pcm_abort(stream); |
| 47 | amdtp_stream_stop(stream); |
| 48 | |
| 49 | if (stream == &efw->tx_stream) |
| 50 | cmp_connection_break(&efw->out_conn); |
| 51 | else |
| 52 | cmp_connection_break(&efw->in_conn); |
| 53 | } |
| 54 | |
| 55 | static int |
| 56 | start_stream(struct snd_efw *efw, struct amdtp_stream *stream, |
| 57 | unsigned int sampling_rate) |
| 58 | { |
| 59 | struct cmp_connection *conn; |
| 60 | unsigned int mode, pcm_channels, midi_ports; |
| 61 | int err; |
| 62 | |
| 63 | err = snd_efw_get_multiplier_mode(sampling_rate, &mode); |
| 64 | if (err < 0) |
| 65 | goto end; |
| 66 | if (stream == &efw->tx_stream) { |
| 67 | conn = &efw->out_conn; |
| 68 | pcm_channels = efw->pcm_capture_channels[mode]; |
| 69 | midi_ports = efw->midi_out_ports; |
| 70 | } else { |
| 71 | conn = &efw->in_conn; |
| 72 | pcm_channels = efw->pcm_playback_channels[mode]; |
| 73 | midi_ports = efw->midi_in_ports; |
| 74 | } |
| 75 | |
Takashi Sakamoto | 51c29fd | 2015-09-19 11:21:56 +0900 | [diff] [blame] | 76 | err = amdtp_am824_set_parameters(stream, sampling_rate, |
| 77 | pcm_channels, midi_ports, false); |
Takashi Sakamoto | 547e631 | 2015-09-19 11:21:49 +0900 | [diff] [blame] | 78 | if (err < 0) |
| 79 | goto end; |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 80 | |
| 81 | /* establish connection via CMP */ |
| 82 | err = cmp_connection_establish(conn, |
| 83 | amdtp_stream_get_max_payload(stream)); |
| 84 | if (err < 0) |
| 85 | goto end; |
| 86 | |
| 87 | /* start amdtp stream */ |
| 88 | err = amdtp_stream_start(stream, |
| 89 | conn->resources.channel, |
| 90 | conn->speed); |
| 91 | if (err < 0) { |
| 92 | stop_stream(efw, stream); |
| 93 | goto end; |
| 94 | } |
| 95 | |
| 96 | /* wait first callback */ |
| 97 | if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) { |
| 98 | stop_stream(efw, stream); |
| 99 | err = -ETIMEDOUT; |
| 100 | } |
| 101 | end: |
| 102 | return err; |
| 103 | } |
| 104 | |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 105 | /* |
| 106 | * This function should be called before starting the stream or after stopping |
| 107 | * the streams. |
| 108 | */ |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 109 | static void |
| 110 | destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream) |
| 111 | { |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 112 | struct cmp_connection *conn; |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 113 | |
| 114 | if (stream == &efw->tx_stream) |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 115 | conn = &efw->out_conn; |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 116 | else |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 117 | conn = &efw->in_conn; |
| 118 | |
| 119 | amdtp_stream_destroy(stream); |
| 120 | cmp_connection_destroy(&efw->out_conn); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 124 | check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s) |
| 125 | { |
| 126 | struct cmp_connection *conn; |
| 127 | bool used; |
| 128 | int err; |
| 129 | |
| 130 | if (s == &efw->tx_stream) |
| 131 | conn = &efw->out_conn; |
| 132 | else |
| 133 | conn = &efw->in_conn; |
| 134 | |
| 135 | err = cmp_connection_check_used(conn, &used); |
| 136 | if ((err >= 0) && used && !amdtp_stream_running(s)) { |
| 137 | dev_err(&efw->unit->device, |
| 138 | "Connection established by others: %cPCR[%d]\n", |
| 139 | (conn->direction == CMP_OUTPUT) ? 'o' : 'i', |
| 140 | conn->pcr_index); |
| 141 | err = -EBUSY; |
| 142 | } |
| 143 | |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | int snd_efw_stream_init_duplex(struct snd_efw *efw) |
| 148 | { |
| 149 | int err; |
| 150 | |
| 151 | err = init_stream(efw, &efw->tx_stream); |
| 152 | if (err < 0) |
| 153 | goto end; |
Takashi Sakamoto | 7ab5664 | 2014-04-25 22:45:03 +0900 | [diff] [blame] | 154 | /* Fireworks transmits NODATA packets with TAG0. */ |
| 155 | efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0; |
Takashi Sakamoto | c8bdf49 | 2014-04-25 22:45:04 +0900 | [diff] [blame] | 156 | /* Fireworks has its own meaning for dbc. */ |
| 157 | efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT; |
Takashi Sakamoto | b84b1a2 | 2014-04-25 22:45:07 +0900 | [diff] [blame] | 158 | /* Fireworks reset dbc at bus reset. */ |
| 159 | efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK; |
Takashi Sakamoto | 18f5ed3 | 2015-08-05 09:21:05 +0900 | [diff] [blame] | 160 | /* |
| 161 | * But Recent firmwares starts packets with non-zero dbc. |
| 162 | * Driver version 5.7.6 installs firmware version 5.7.3. |
| 163 | */ |
| 164 | if (efw->is_fireworks3 && |
| 165 | (efw->firmware_version == 0x5070000 || |
| 166 | efw->firmware_version == 0x5070300 || |
| 167 | efw->firmware_version == 0x5080000)) |
| 168 | efw->tx_stream.tx_first_dbc = 0x02; |
Takashi Sakamoto | 6970223 | 2014-04-25 22:45:05 +0900 | [diff] [blame] | 169 | /* AudioFire9 always reports wrong dbs. */ |
| 170 | if (efw->is_af9) |
| 171 | efw->tx_stream.flags |= CIP_WRONG_DBS; |
Takashi Sakamoto | d9cd006 | 2014-04-25 22:45:06 +0900 | [diff] [blame] | 172 | /* Firmware version 5.5 reports fixed interval for dbc. */ |
| 173 | if (efw->firmware_version == 0x5050000) |
| 174 | efw->tx_stream.tx_dbc_interval = 8; |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 175 | |
| 176 | err = init_stream(efw, &efw->rx_stream); |
| 177 | if (err < 0) { |
| 178 | destroy_stream(efw, &efw->tx_stream); |
| 179 | goto end; |
| 180 | } |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 181 | |
| 182 | /* set IEC61883 compliant mode (actually not fully compliant...) */ |
| 183 | err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883); |
| 184 | if (err < 0) { |
| 185 | destroy_stream(efw, &efw->tx_stream); |
| 186 | destroy_stream(efw, &efw->rx_stream); |
| 187 | } |
| 188 | end: |
| 189 | return err; |
| 190 | } |
| 191 | |
Takashi Sakamoto | 4a286d5 | 2014-05-28 00:14:40 +0900 | [diff] [blame] | 192 | int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate) |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 193 | { |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 194 | unsigned int curr_rate; |
| 195 | int err = 0; |
| 196 | |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 197 | /* Need no substreams */ |
Takashi Sakamoto | 4d2c50a | 2015-11-14 16:54:51 +0900 | [diff] [blame] | 198 | if (efw->playback_substreams == 0 && efw->capture_substreams == 0) |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 199 | goto end; |
| 200 | |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 201 | /* |
| 202 | * Considering JACK/FFADO streaming: |
| 203 | * TODO: This can be removed hwdep functionality becomes popular. |
| 204 | */ |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 205 | err = check_connection_used_by_others(efw, &efw->rx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 206 | if (err < 0) |
| 207 | goto end; |
| 208 | |
| 209 | /* packet queueing error */ |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 210 | if (amdtp_streaming_error(&efw->tx_stream)) |
| 211 | stop_stream(efw, &efw->tx_stream); |
| 212 | if (amdtp_streaming_error(&efw->rx_stream)) |
| 213 | stop_stream(efw, &efw->rx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 214 | |
| 215 | /* stop streams if rate is different */ |
| 216 | err = snd_efw_command_get_sampling_rate(efw, &curr_rate); |
| 217 | if (err < 0) |
| 218 | goto end; |
| 219 | if (rate == 0) |
| 220 | rate = curr_rate; |
| 221 | if (rate != curr_rate) { |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 222 | stop_stream(efw, &efw->tx_stream); |
| 223 | stop_stream(efw, &efw->rx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* master should be always running */ |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 227 | if (!amdtp_stream_running(&efw->rx_stream)) { |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 228 | err = snd_efw_command_set_sampling_rate(efw, rate); |
| 229 | if (err < 0) |
| 230 | goto end; |
| 231 | |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 232 | err = start_stream(efw, &efw->rx_stream, rate); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 233 | if (err < 0) { |
| 234 | dev_err(&efw->unit->device, |
| 235 | "fail to start AMDTP master stream:%d\n", err); |
| 236 | goto end; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* start slave if needed */ |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 241 | if (efw->capture_substreams > 0 && |
| 242 | !amdtp_stream_running(&efw->tx_stream)) { |
| 243 | err = start_stream(efw, &efw->tx_stream, rate); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 244 | if (err < 0) { |
| 245 | dev_err(&efw->unit->device, |
| 246 | "fail to start AMDTP slave stream:%d\n", err); |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 247 | stop_stream(efw, &efw->rx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | end: |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 251 | return err; |
| 252 | } |
| 253 | |
| 254 | void snd_efw_stream_stop_duplex(struct snd_efw *efw) |
| 255 | { |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 256 | if (efw->capture_substreams == 0) { |
| 257 | stop_stream(efw, &efw->tx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 258 | |
Takashi Sakamoto | eb4a378 | 2016-05-09 23:15:51 +0900 | [diff] [blame] | 259 | if (efw->playback_substreams == 0) |
| 260 | stop_stream(efw, &efw->rx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 261 | } |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void snd_efw_stream_update_duplex(struct snd_efw *efw) |
| 265 | { |
Takashi Sakamoto | 99d7355 | 2016-02-20 16:41:01 +0900 | [diff] [blame] | 266 | if (cmp_connection_update(&efw->out_conn) < 0 || |
| 267 | cmp_connection_update(&efw->in_conn) < 0) { |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 268 | stop_stream(efw, &efw->rx_stream); |
| 269 | stop_stream(efw, &efw->tx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 270 | } else { |
| 271 | amdtp_stream_update(&efw->rx_stream); |
| 272 | amdtp_stream_update(&efw->tx_stream); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void snd_efw_stream_destroy_duplex(struct snd_efw *efw) |
| 277 | { |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 278 | destroy_stream(efw, &efw->rx_stream); |
| 279 | destroy_stream(efw, &efw->tx_stream); |
Takashi Sakamoto | 315fd41 | 2014-04-25 22:45:02 +0900 | [diff] [blame] | 280 | } |
Takashi Sakamoto | 594ddce | 2014-04-25 22:45:12 +0900 | [diff] [blame] | 281 | |
| 282 | void snd_efw_stream_lock_changed(struct snd_efw *efw) |
| 283 | { |
| 284 | efw->dev_lock_changed = true; |
| 285 | wake_up(&efw->hwdep_wait); |
| 286 | } |
| 287 | |
| 288 | int snd_efw_stream_lock_try(struct snd_efw *efw) |
| 289 | { |
| 290 | int err; |
| 291 | |
| 292 | spin_lock_irq(&efw->lock); |
| 293 | |
| 294 | /* user land lock this */ |
| 295 | if (efw->dev_lock_count < 0) { |
| 296 | err = -EBUSY; |
| 297 | goto end; |
| 298 | } |
| 299 | |
| 300 | /* this is the first time */ |
| 301 | if (efw->dev_lock_count++ == 0) |
| 302 | snd_efw_stream_lock_changed(efw); |
| 303 | err = 0; |
| 304 | end: |
| 305 | spin_unlock_irq(&efw->lock); |
| 306 | return err; |
| 307 | } |
| 308 | |
| 309 | void snd_efw_stream_lock_release(struct snd_efw *efw) |
| 310 | { |
| 311 | spin_lock_irq(&efw->lock); |
| 312 | |
| 313 | if (WARN_ON(efw->dev_lock_count <= 0)) |
| 314 | goto end; |
| 315 | if (--efw->dev_lock_count == 0) |
| 316 | snd_efw_stream_lock_changed(efw); |
| 317 | end: |
| 318 | spin_unlock_irq(&efw->lock); |
| 319 | } |