Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * digi00x-stream.c - a part of driver for Digidesign Digi 002/003 family |
| 3 | * |
| 4 | * Copyright (c) 2014-2015 Takashi Sakamoto |
| 5 | * |
| 6 | * Licensed under the terms of the GNU General Public License, version 2. |
| 7 | */ |
| 8 | |
| 9 | #include "digi00x.h" |
| 10 | |
| 11 | #define CALLBACK_TIMEOUT 500 |
| 12 | |
| 13 | const unsigned int snd_dg00x_stream_rates[SND_DG00X_RATE_COUNT] = { |
| 14 | [SND_DG00X_RATE_44100] = 44100, |
| 15 | [SND_DG00X_RATE_48000] = 48000, |
| 16 | [SND_DG00X_RATE_88200] = 88200, |
| 17 | [SND_DG00X_RATE_96000] = 96000, |
| 18 | }; |
| 19 | |
| 20 | /* Multi Bit Linear Audio data channels for each sampling transfer frequency. */ |
| 21 | const unsigned int |
| 22 | snd_dg00x_stream_pcm_channels[SND_DG00X_RATE_COUNT] = { |
| 23 | /* Analog/ADAT/SPDIF */ |
| 24 | [SND_DG00X_RATE_44100] = (8 + 8 + 2), |
| 25 | [SND_DG00X_RATE_48000] = (8 + 8 + 2), |
| 26 | /* Analog/SPDIF */ |
| 27 | [SND_DG00X_RATE_88200] = (8 + 2), |
| 28 | [SND_DG00X_RATE_96000] = (8 + 2), |
| 29 | }; |
| 30 | |
| 31 | int snd_dg00x_stream_get_local_rate(struct snd_dg00x *dg00x, unsigned int *rate) |
| 32 | { |
| 33 | u32 data; |
| 34 | __be32 reg; |
| 35 | int err; |
| 36 | |
| 37 | err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST, |
| 38 | DG00X_ADDR_BASE + DG00X_OFFSET_LOCAL_RATE, |
| 39 | ®, sizeof(reg), 0); |
| 40 | if (err < 0) |
| 41 | return err; |
| 42 | |
| 43 | data = be32_to_cpu(reg) & 0x0f; |
| 44 | if (data < ARRAY_SIZE(snd_dg00x_stream_rates)) |
| 45 | *rate = snd_dg00x_stream_rates[data]; |
| 46 | else |
| 47 | err = -EIO; |
| 48 | |
| 49 | return err; |
| 50 | } |
| 51 | |
| 52 | int snd_dg00x_stream_set_local_rate(struct snd_dg00x *dg00x, unsigned int rate) |
| 53 | { |
| 54 | __be32 reg; |
| 55 | unsigned int i; |
| 56 | |
| 57 | for (i = 0; i < ARRAY_SIZE(snd_dg00x_stream_rates); i++) { |
| 58 | if (rate == snd_dg00x_stream_rates[i]) |
| 59 | break; |
| 60 | } |
| 61 | if (i == ARRAY_SIZE(snd_dg00x_stream_rates)) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | reg = cpu_to_be32(i); |
| 65 | return snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 66 | DG00X_ADDR_BASE + DG00X_OFFSET_LOCAL_RATE, |
| 67 | ®, sizeof(reg), 0); |
| 68 | } |
| 69 | |
| 70 | int snd_dg00x_stream_get_clock(struct snd_dg00x *dg00x, |
| 71 | enum snd_dg00x_clock *clock) |
| 72 | { |
| 73 | __be32 reg; |
| 74 | int err; |
| 75 | |
| 76 | err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST, |
| 77 | DG00X_ADDR_BASE + DG00X_OFFSET_CLOCK_SOURCE, |
| 78 | ®, sizeof(reg), 0); |
| 79 | if (err < 0) |
| 80 | return err; |
| 81 | |
| 82 | *clock = be32_to_cpu(reg) & 0x0f; |
| 83 | if (*clock >= SND_DG00X_CLOCK_COUNT) |
| 84 | err = -EIO; |
| 85 | |
| 86 | return err; |
| 87 | } |
| 88 | |
| 89 | int snd_dg00x_stream_check_external_clock(struct snd_dg00x *dg00x, bool *detect) |
| 90 | { |
| 91 | __be32 reg; |
| 92 | int err; |
| 93 | |
| 94 | err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST, |
| 95 | DG00X_ADDR_BASE + DG00X_OFFSET_DETECT_EXTERNAL, |
| 96 | ®, sizeof(reg), 0); |
| 97 | if (err >= 0) |
| 98 | *detect = be32_to_cpu(reg) > 0; |
| 99 | |
| 100 | return err; |
| 101 | } |
| 102 | |
| 103 | int snd_dg00x_stream_get_external_rate(struct snd_dg00x *dg00x, |
| 104 | unsigned int *rate) |
| 105 | { |
| 106 | u32 data; |
| 107 | __be32 reg; |
| 108 | int err; |
| 109 | |
| 110 | err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST, |
| 111 | DG00X_ADDR_BASE + DG00X_OFFSET_EXTERNAL_RATE, |
| 112 | ®, sizeof(reg), 0); |
| 113 | if (err < 0) |
| 114 | return err; |
| 115 | |
| 116 | data = be32_to_cpu(reg) & 0x0f; |
| 117 | if (data < ARRAY_SIZE(snd_dg00x_stream_rates)) |
| 118 | *rate = snd_dg00x_stream_rates[data]; |
| 119 | /* This means desync. */ |
| 120 | else |
| 121 | err = -EBUSY; |
| 122 | |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | static void finish_session(struct snd_dg00x *dg00x) |
| 127 | { |
Takashi Sakamoto | d18b0a6 | 2019-06-11 22:21:08 +0900 | [diff] [blame] | 128 | __be32 data; |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 129 | |
Takashi Sakamoto | d18b0a6 | 2019-06-11 22:21:08 +0900 | [diff] [blame] | 130 | amdtp_stream_stop(&dg00x->tx_stream); |
| 131 | amdtp_stream_stop(&dg00x->rx_stream); |
| 132 | |
| 133 | data = cpu_to_be32(0x00000003); |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 134 | snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 135 | DG00X_ADDR_BASE + DG00X_OFFSET_STREAMING_SET, |
| 136 | &data, sizeof(data), 0); |
Takashi Sakamoto | 6bc9322 | 2019-06-11 22:21:07 +0900 | [diff] [blame] | 137 | |
| 138 | // Unregister isochronous channels for both direction. |
| 139 | data = 0; |
| 140 | snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 141 | DG00X_ADDR_BASE + DG00X_OFFSET_ISOC_CHANNELS, |
| 142 | &data, sizeof(data), 0); |
Takashi Sakamoto | d18b0a6 | 2019-06-11 22:21:08 +0900 | [diff] [blame] | 143 | |
| 144 | // Just after finishing the session, the device may lost transmitting |
| 145 | // functionality for a short time. |
| 146 | msleep(50); |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int begin_session(struct snd_dg00x *dg00x) |
| 150 | { |
| 151 | __be32 data; |
| 152 | u32 curr; |
| 153 | int err; |
| 154 | |
Takashi Sakamoto | 6bc9322 | 2019-06-11 22:21:07 +0900 | [diff] [blame] | 155 | // Register isochronous channels for both direction. |
| 156 | data = cpu_to_be32((dg00x->tx_resources.channel << 16) | |
| 157 | dg00x->rx_resources.channel); |
| 158 | err = snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST, |
| 159 | DG00X_ADDR_BASE + DG00X_OFFSET_ISOC_CHANNELS, |
| 160 | &data, sizeof(data), 0); |
| 161 | if (err < 0) |
Takashi Sakamoto | 638e19f | 2019-06-11 22:21:09 +0900 | [diff] [blame^] | 162 | return err; |
Takashi Sakamoto | 6bc9322 | 2019-06-11 22:21:07 +0900 | [diff] [blame] | 163 | |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 164 | err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST, |
| 165 | DG00X_ADDR_BASE + DG00X_OFFSET_STREAMING_STATE, |
| 166 | &data, sizeof(data), 0); |
| 167 | if (err < 0) |
Takashi Sakamoto | 638e19f | 2019-06-11 22:21:09 +0900 | [diff] [blame^] | 168 | return err; |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 169 | curr = be32_to_cpu(data); |
| 170 | |
| 171 | if (curr == 0) |
| 172 | curr = 2; |
| 173 | |
| 174 | curr--; |
| 175 | while (curr > 0) { |
| 176 | data = cpu_to_be32(curr); |
| 177 | err = snd_fw_transaction(dg00x->unit, |
| 178 | TCODE_WRITE_QUADLET_REQUEST, |
| 179 | DG00X_ADDR_BASE + |
| 180 | DG00X_OFFSET_STREAMING_SET, |
| 181 | &data, sizeof(data), 0); |
| 182 | if (err < 0) |
Takashi Sakamoto | 638e19f | 2019-06-11 22:21:09 +0900 | [diff] [blame^] | 183 | break; |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 184 | |
| 185 | msleep(20); |
| 186 | curr--; |
| 187 | } |
| 188 | |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 189 | return err; |
| 190 | } |
| 191 | |
| 192 | static void release_resources(struct snd_dg00x *dg00x) |
| 193 | { |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 194 | /* Release isochronous resources. */ |
| 195 | fw_iso_resources_free(&dg00x->tx_resources); |
| 196 | fw_iso_resources_free(&dg00x->rx_resources); |
| 197 | } |
| 198 | |
| 199 | static int keep_resources(struct snd_dg00x *dg00x, unsigned int rate) |
| 200 | { |
| 201 | unsigned int i; |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 202 | int err; |
| 203 | |
| 204 | /* Check sampling rate. */ |
| 205 | for (i = 0; i < SND_DG00X_RATE_COUNT; i++) { |
| 206 | if (snd_dg00x_stream_rates[i] == rate) |
| 207 | break; |
| 208 | } |
| 209 | if (i == SND_DG00X_RATE_COUNT) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | /* Keep resources for out-stream. */ |
| 213 | err = amdtp_dot_set_parameters(&dg00x->rx_stream, rate, |
Takashi Sakamoto | 9dc5d31 | 2015-10-11 12:30:15 +0900 | [diff] [blame] | 214 | snd_dg00x_stream_pcm_channels[i]); |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 215 | if (err < 0) |
| 216 | return err; |
| 217 | err = fw_iso_resources_allocate(&dg00x->rx_resources, |
| 218 | amdtp_stream_get_max_payload(&dg00x->rx_stream), |
| 219 | fw_parent_device(dg00x->unit)->max_speed); |
| 220 | if (err < 0) |
| 221 | return err; |
| 222 | |
| 223 | /* Keep resources for in-stream. */ |
| 224 | err = amdtp_dot_set_parameters(&dg00x->tx_stream, rate, |
Takashi Sakamoto | 9dc5d31 | 2015-10-11 12:30:15 +0900 | [diff] [blame] | 225 | snd_dg00x_stream_pcm_channels[i]); |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 226 | if (err < 0) |
| 227 | return err; |
| 228 | err = fw_iso_resources_allocate(&dg00x->tx_resources, |
| 229 | amdtp_stream_get_max_payload(&dg00x->tx_stream), |
| 230 | fw_parent_device(dg00x->unit)->max_speed); |
Takashi Sakamoto | 6bc9322 | 2019-06-11 22:21:07 +0900 | [diff] [blame] | 231 | if (err < 0) { |
| 232 | fw_iso_resources_free(&dg00x->rx_resources); |
| 233 | return err; |
| 234 | } |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 235 | |
| 236 | return 0; |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | int snd_dg00x_stream_init_duplex(struct snd_dg00x *dg00x) |
| 240 | { |
| 241 | int err; |
| 242 | |
| 243 | /* For out-stream. */ |
| 244 | err = fw_iso_resources_init(&dg00x->rx_resources, dg00x->unit); |
| 245 | if (err < 0) |
| 246 | goto error; |
| 247 | err = amdtp_dot_init(&dg00x->rx_stream, dg00x->unit, AMDTP_OUT_STREAM); |
| 248 | if (err < 0) |
| 249 | goto error; |
| 250 | |
| 251 | /* For in-stream. */ |
| 252 | err = fw_iso_resources_init(&dg00x->tx_resources, dg00x->unit); |
| 253 | if (err < 0) |
| 254 | goto error; |
| 255 | err = amdtp_dot_init(&dg00x->tx_stream, dg00x->unit, AMDTP_IN_STREAM); |
| 256 | if (err < 0) |
| 257 | goto error; |
| 258 | |
| 259 | return 0; |
| 260 | error: |
| 261 | snd_dg00x_stream_destroy_duplex(dg00x); |
| 262 | return err; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * This function should be called before starting streams or after stopping |
| 267 | * streams. |
| 268 | */ |
| 269 | void snd_dg00x_stream_destroy_duplex(struct snd_dg00x *dg00x) |
| 270 | { |
| 271 | amdtp_stream_destroy(&dg00x->rx_stream); |
| 272 | fw_iso_resources_destroy(&dg00x->rx_resources); |
| 273 | |
| 274 | amdtp_stream_destroy(&dg00x->tx_stream); |
| 275 | fw_iso_resources_destroy(&dg00x->tx_resources); |
| 276 | } |
| 277 | |
| 278 | int snd_dg00x_stream_start_duplex(struct snd_dg00x *dg00x, unsigned int rate) |
| 279 | { |
| 280 | unsigned int curr_rate; |
| 281 | int err = 0; |
| 282 | |
| 283 | if (dg00x->substreams_counter == 0) |
| 284 | goto end; |
| 285 | |
| 286 | /* Check current sampling rate. */ |
| 287 | err = snd_dg00x_stream_get_local_rate(dg00x, &curr_rate); |
| 288 | if (err < 0) |
| 289 | goto error; |
Takashi Sakamoto | 9fbfd38 | 2015-10-11 12:30:16 +0900 | [diff] [blame] | 290 | if (rate == 0) |
| 291 | rate = curr_rate; |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 292 | if (curr_rate != rate || |
| 293 | amdtp_streaming_error(&dg00x->tx_stream) || |
| 294 | amdtp_streaming_error(&dg00x->rx_stream)) { |
| 295 | finish_session(dg00x); |
| 296 | |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 297 | release_resources(dg00x); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * No packets are transmitted without receiving packets, reagardless of |
| 302 | * which source of clock is used. |
| 303 | */ |
| 304 | if (!amdtp_stream_running(&dg00x->rx_stream)) { |
| 305 | err = snd_dg00x_stream_set_local_rate(dg00x, rate); |
| 306 | if (err < 0) |
| 307 | goto error; |
| 308 | |
| 309 | err = keep_resources(dg00x, rate); |
| 310 | if (err < 0) |
| 311 | goto error; |
| 312 | |
| 313 | err = begin_session(dg00x); |
| 314 | if (err < 0) |
| 315 | goto error; |
| 316 | |
| 317 | err = amdtp_stream_start(&dg00x->rx_stream, |
| 318 | dg00x->rx_resources.channel, |
| 319 | fw_parent_device(dg00x->unit)->max_speed); |
| 320 | if (err < 0) |
| 321 | goto error; |
| 322 | |
| 323 | if (!amdtp_stream_wait_callback(&dg00x->rx_stream, |
| 324 | CALLBACK_TIMEOUT)) { |
| 325 | err = -ETIMEDOUT; |
| 326 | goto error; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * The value of SYT field in transmitted packets is always 0x0000. Thus, |
| 332 | * duplex streams with timestamp synchronization cannot be built. |
| 333 | */ |
| 334 | if (!amdtp_stream_running(&dg00x->tx_stream)) { |
| 335 | err = amdtp_stream_start(&dg00x->tx_stream, |
| 336 | dg00x->tx_resources.channel, |
| 337 | fw_parent_device(dg00x->unit)->max_speed); |
| 338 | if (err < 0) |
| 339 | goto error; |
| 340 | |
| 341 | if (!amdtp_stream_wait_callback(&dg00x->tx_stream, |
| 342 | CALLBACK_TIMEOUT)) { |
| 343 | err = -ETIMEDOUT; |
| 344 | goto error; |
| 345 | } |
| 346 | } |
| 347 | end: |
| 348 | return err; |
| 349 | error: |
| 350 | finish_session(dg00x); |
| 351 | |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 352 | release_resources(dg00x); |
| 353 | |
| 354 | return err; |
| 355 | } |
| 356 | |
| 357 | void snd_dg00x_stream_stop_duplex(struct snd_dg00x *dg00x) |
| 358 | { |
| 359 | if (dg00x->substreams_counter > 0) |
| 360 | return; |
| 361 | |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 362 | finish_session(dg00x); |
| 363 | release_resources(dg00x); |
Takashi Sakamoto | 3a2a179 | 2015-09-30 09:39:18 +0900 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void snd_dg00x_stream_update_duplex(struct snd_dg00x *dg00x) |
| 367 | { |
| 368 | fw_iso_resources_update(&dg00x->tx_resources); |
| 369 | fw_iso_resources_update(&dg00x->rx_resources); |
| 370 | |
| 371 | amdtp_stream_update(&dg00x->tx_stream); |
| 372 | amdtp_stream_update(&dg00x->rx_stream); |
| 373 | } |
Takashi Sakamoto | 660dd3d | 2015-09-30 09:39:21 +0900 | [diff] [blame] | 374 | |
| 375 | void snd_dg00x_stream_lock_changed(struct snd_dg00x *dg00x) |
| 376 | { |
| 377 | dg00x->dev_lock_changed = true; |
| 378 | wake_up(&dg00x->hwdep_wait); |
| 379 | } |
| 380 | |
| 381 | int snd_dg00x_stream_lock_try(struct snd_dg00x *dg00x) |
| 382 | { |
| 383 | int err; |
| 384 | |
| 385 | spin_lock_irq(&dg00x->lock); |
| 386 | |
| 387 | /* user land lock this */ |
| 388 | if (dg00x->dev_lock_count < 0) { |
| 389 | err = -EBUSY; |
| 390 | goto end; |
| 391 | } |
| 392 | |
| 393 | /* this is the first time */ |
| 394 | if (dg00x->dev_lock_count++ == 0) |
| 395 | snd_dg00x_stream_lock_changed(dg00x); |
| 396 | err = 0; |
| 397 | end: |
| 398 | spin_unlock_irq(&dg00x->lock); |
| 399 | return err; |
| 400 | } |
| 401 | |
| 402 | void snd_dg00x_stream_lock_release(struct snd_dg00x *dg00x) |
| 403 | { |
| 404 | spin_lock_irq(&dg00x->lock); |
| 405 | |
| 406 | if (WARN_ON(dg00x->dev_lock_count <= 0)) |
| 407 | goto end; |
| 408 | if (--dg00x->dev_lock_count == 0) |
| 409 | snd_dg00x_stream_lock_changed(dg00x); |
| 410 | end: |
| 411 | spin_unlock_irq(&dg00x->lock); |
| 412 | } |