1 // SPDX-License-Identifier: GPL-2.0 2 // ff-protocol-former.c - a part of driver for RME Fireface series 3 // 4 // Copyright (c) 2019 Takashi Sakamoto 5 // 6 // Licensed under the terms of the GNU General Public License, version 2. 7 8 #include <linux/delay.h> 9 10 #include "ff.h" 11 12 #define FORMER_REG_SYNC_STATUS 0x0000801c0000ull 13 /* For block write request. */ 14 #define FORMER_REG_FETCH_PCM_FRAMES 0x0000801c0000ull 15 16 static int former_switch_fetching_mode(struct snd_ff *ff, bool enable) 17 { 18 unsigned int count; 19 __le32 *reg; 20 int i; 21 int err; 22 23 count = 0; 24 for (i = 0; i < SND_FF_STREAM_MODE_COUNT; ++i) 25 count = max(count, ff->spec->pcm_playback_channels[i]); 26 27 reg = kcalloc(count, sizeof(__le32), GFP_KERNEL); 28 if (!reg) 29 return -ENOMEM; 30 31 if (!enable) { 32 /* 33 * Each quadlet is corresponding to data channels in a data 34 * blocks in reverse order. Precisely, quadlets for available 35 * data channels should be enabled. Here, I take second best 36 * to fetch PCM frames from all of data channels regardless of 37 * stf. 38 */ 39 for (i = 0; i < count; ++i) 40 reg[i] = cpu_to_le32(0x00000001); 41 } 42 43 err = snd_fw_transaction(ff->unit, TCODE_WRITE_BLOCK_REQUEST, 44 FORMER_REG_FETCH_PCM_FRAMES, reg, 45 sizeof(__le32) * count, 0); 46 kfree(reg); 47 return err; 48 } 49 50 static void dump_clock_config(struct snd_ff *ff, struct snd_info_buffer *buffer) 51 { 52 __le32 reg; 53 u32 data; 54 unsigned int rate; 55 const char *src; 56 int err; 57 58 err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST, 59 SND_FF_REG_CLOCK_CONFIG, ®, sizeof(reg), 0); 60 if (err < 0) 61 return; 62 63 data = le32_to_cpu(reg); 64 65 snd_iprintf(buffer, "Output S/PDIF format: %s (Emphasis: %s)\n", 66 (data & 0x20) ? "Professional" : "Consumer", 67 (data & 0x40) ? "on" : "off"); 68 69 snd_iprintf(buffer, "Optical output interface format: %s\n", 70 ((data >> 8) & 0x01) ? "S/PDIF" : "ADAT"); 71 72 snd_iprintf(buffer, "Word output single speed: %s\n", 73 ((data >> 8) & 0x20) ? "on" : "off"); 74 75 snd_iprintf(buffer, "S/PDIF input interface: %s\n", 76 ((data >> 8) & 0x02) ? "Optical" : "Coaxial"); 77 78 switch ((data >> 1) & 0x03) { 79 case 0x01: 80 rate = 32000; 81 break; 82 case 0x00: 83 rate = 44100; 84 break; 85 case 0x03: 86 rate = 48000; 87 break; 88 case 0x02: 89 default: 90 return; 91 } 92 93 if (data & 0x08) 94 rate *= 2; 95 else if (data & 0x10) 96 rate *= 4; 97 98 snd_iprintf(buffer, "Sampling rate: %d\n", rate); 99 100 if (data & 0x01) { 101 src = "Internal"; 102 } else { 103 switch ((data >> 10) & 0x07) { 104 case 0x00: 105 src = "ADAT1"; 106 break; 107 case 0x01: 108 src = "ADAT2"; 109 break; 110 case 0x03: 111 src = "S/PDIF"; 112 break; 113 case 0x04: 114 src = "Word"; 115 break; 116 case 0x05: 117 src = "LTC"; 118 break; 119 default: 120 return; 121 } 122 } 123 124 snd_iprintf(buffer, "Sync to clock source: %s\n", src); 125 } 126 127 static void dump_sync_status(struct snd_ff *ff, struct snd_info_buffer *buffer) 128 { 129 __le32 reg; 130 u32 data; 131 int err; 132 133 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST, 134 FORMER_REG_SYNC_STATUS, ®, sizeof(reg), 0); 135 if (err < 0) 136 return; 137 138 data = le32_to_cpu(reg); 139 140 snd_iprintf(buffer, "External source detection:\n"); 141 142 snd_iprintf(buffer, "Word Clock:"); 143 if ((data >> 24) & 0x20) { 144 if ((data >> 24) & 0x40) 145 snd_iprintf(buffer, "sync\n"); 146 else 147 snd_iprintf(buffer, "lock\n"); 148 } else { 149 snd_iprintf(buffer, "none\n"); 150 } 151 152 snd_iprintf(buffer, "S/PDIF:"); 153 if ((data >> 16) & 0x10) { 154 if ((data >> 16) & 0x04) 155 snd_iprintf(buffer, "sync\n"); 156 else 157 snd_iprintf(buffer, "lock\n"); 158 } else { 159 snd_iprintf(buffer, "none\n"); 160 } 161 162 snd_iprintf(buffer, "ADAT1:"); 163 if ((data >> 8) & 0x04) { 164 if ((data >> 8) & 0x10) 165 snd_iprintf(buffer, "sync\n"); 166 else 167 snd_iprintf(buffer, "lock\n"); 168 } else { 169 snd_iprintf(buffer, "none\n"); 170 } 171 172 snd_iprintf(buffer, "ADAT2:"); 173 if ((data >> 8) & 0x08) { 174 if ((data >> 8) & 0x20) 175 snd_iprintf(buffer, "sync\n"); 176 else 177 snd_iprintf(buffer, "lock\n"); 178 } else { 179 snd_iprintf(buffer, "none\n"); 180 } 181 182 snd_iprintf(buffer, "\nUsed external source:\n"); 183 184 if (((data >> 22) & 0x07) == 0x07) { 185 snd_iprintf(buffer, "None\n"); 186 } else { 187 switch ((data >> 22) & 0x07) { 188 case 0x00: 189 snd_iprintf(buffer, "ADAT1:"); 190 break; 191 case 0x01: 192 snd_iprintf(buffer, "ADAT2:"); 193 break; 194 case 0x03: 195 snd_iprintf(buffer, "S/PDIF:"); 196 break; 197 case 0x04: 198 snd_iprintf(buffer, "Word:"); 199 break; 200 case 0x07: 201 snd_iprintf(buffer, "Nothing:"); 202 break; 203 case 0x02: 204 case 0x05: 205 case 0x06: 206 default: 207 snd_iprintf(buffer, "unknown:"); 208 break; 209 } 210 211 if ((data >> 25) & 0x07) { 212 switch ((data >> 25) & 0x07) { 213 case 0x01: 214 snd_iprintf(buffer, "32000\n"); 215 break; 216 case 0x02: 217 snd_iprintf(buffer, "44100\n"); 218 break; 219 case 0x03: 220 snd_iprintf(buffer, "48000\n"); 221 break; 222 case 0x04: 223 snd_iprintf(buffer, "64000\n"); 224 break; 225 case 0x05: 226 snd_iprintf(buffer, "88200\n"); 227 break; 228 case 0x06: 229 snd_iprintf(buffer, "96000\n"); 230 break; 231 case 0x07: 232 snd_iprintf(buffer, "128000\n"); 233 break; 234 case 0x08: 235 snd_iprintf(buffer, "176400\n"); 236 break; 237 case 0x09: 238 snd_iprintf(buffer, "192000\n"); 239 break; 240 case 0x00: 241 snd_iprintf(buffer, "unknown\n"); 242 break; 243 } 244 } 245 } 246 247 snd_iprintf(buffer, "Multiplied:"); 248 snd_iprintf(buffer, "%d\n", (data & 0x3ff) * 250); 249 } 250 251 static void former_dump_status(struct snd_ff *ff, 252 struct snd_info_buffer *buffer) 253 { 254 dump_clock_config(ff, buffer); 255 dump_sync_status(ff, buffer); 256 } 257 258 #define FF800_STF 0x0000fc88f000 259 #define FF800_RX_PACKET_FORMAT 0x0000fc88f004 260 #define FF800_ALLOC_TX_STREAM 0x0000fc88f008 261 #define FF800_ISOC_COMM_START 0x0000fc88f00c 262 #define FF800_TX_S800_FLAG 0x00000800 263 #define FF800_ISOC_COMM_STOP 0x0000fc88f010 264 265 #define FF800_TX_PACKET_ISOC_CH 0x0000801c0008 266 267 static int allocate_rx_resources(struct snd_ff *ff) 268 { 269 u32 data; 270 __le32 reg; 271 int err; 272 273 // Controllers should allocate isochronous resources for rx stream. 274 err = fw_iso_resources_allocate(&ff->rx_resources, 275 amdtp_stream_get_max_payload(&ff->rx_stream), 276 fw_parent_device(ff->unit)->max_speed); 277 if (err < 0) 278 return err; 279 280 // Set isochronous channel and the number of quadlets of rx packets. 281 data = ff->rx_stream.data_block_quadlets << 3; 282 data = (data << 8) | ff->rx_resources.channel; 283 reg = cpu_to_le32(data); 284 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 285 FF800_RX_PACKET_FORMAT, ®, sizeof(reg), 0); 286 } 287 288 static int allocate_tx_resources(struct snd_ff *ff) 289 { 290 __le32 reg; 291 unsigned int count; 292 unsigned int tx_isoc_channel; 293 int err; 294 295 reg = cpu_to_le32(ff->tx_stream.data_block_quadlets); 296 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 297 FF800_ALLOC_TX_STREAM, ®, sizeof(reg), 0); 298 if (err < 0) 299 return err; 300 301 // Wait till the format of tx packet is available. 302 count = 0; 303 while (count++ < 10) { 304 u32 data; 305 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST, 306 FF800_TX_PACKET_ISOC_CH, ®, sizeof(reg), 0); 307 if (err < 0) 308 return err; 309 310 data = le32_to_cpu(reg); 311 if (data != 0xffffffff) { 312 tx_isoc_channel = data; 313 break; 314 } 315 316 msleep(50); 317 } 318 if (count >= 10) 319 return -ETIMEDOUT; 320 321 // NOTE: this is a makeshift to start OHCI 1394 IR context in the 322 // channel. On the other hand, 'struct fw_iso_resources.allocated' is 323 // not true and it's not deallocated at stop. 324 ff->tx_resources.channel = tx_isoc_channel; 325 326 return 0; 327 } 328 329 static int ff800_begin_session(struct snd_ff *ff, unsigned int rate) 330 { 331 __le32 reg; 332 int err; 333 334 reg = cpu_to_le32(rate); 335 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 336 FF800_STF, ®, sizeof(reg), 0); 337 if (err < 0) 338 return err; 339 340 // If starting isochronous communication immediately, change of STF has 341 // no effect. In this case, the communication runs based on former STF. 342 // Let's sleep for a bit. 343 msleep(100); 344 345 err = allocate_rx_resources(ff); 346 if (err < 0) 347 return err; 348 349 err = allocate_tx_resources(ff); 350 if (err < 0) 351 return err; 352 353 reg = cpu_to_le32(0x80000000); 354 reg |= cpu_to_le32(ff->tx_stream.data_block_quadlets); 355 if (fw_parent_device(ff->unit)->max_speed == SCODE_800) 356 reg |= cpu_to_le32(FF800_TX_S800_FLAG); 357 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 358 FF800_ISOC_COMM_START, ®, sizeof(reg), 0); 359 } 360 361 static void ff800_finish_session(struct snd_ff *ff) 362 { 363 __le32 reg; 364 365 reg = cpu_to_le32(0x80000000); 366 snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 367 FF800_ISOC_COMM_STOP, ®, sizeof(reg), 0); 368 } 369 370 static void ff800_handle_midi_msg(struct snd_ff *ff, __le32 *buf, size_t length) 371 { 372 int i; 373 374 for (i = 0; i < length / 4; i++) { 375 u8 byte = le32_to_cpu(buf[i]) & 0xff; 376 struct snd_rawmidi_substream *substream; 377 378 substream = READ_ONCE(ff->tx_midi_substreams[0]); 379 if (substream) 380 snd_rawmidi_receive(substream, &byte, 1); 381 } 382 } 383 384 const struct snd_ff_protocol snd_ff_protocol_ff800 = { 385 .handle_midi_msg = ff800_handle_midi_msg, 386 .switch_fetching_mode = former_switch_fetching_mode, 387 .begin_session = ff800_begin_session, 388 .finish_session = ff800_finish_session, 389 .dump_status = former_dump_status, 390 }; 391 392 #define FF400_STF 0x000080100500ull 393 #define FF400_RX_PACKET_FORMAT 0x000080100504ull 394 #define FF400_ISOC_COMM_START 0x000080100508ull 395 #define FF400_TX_PACKET_FORMAT 0x00008010050cull 396 #define FF400_ISOC_COMM_STOP 0x000080100510ull 397 398 /* 399 * Fireface 400 manages isochronous channel number in 3 bit field. Therefore, 400 * we can allocate between 0 and 7 channel. 401 */ 402 static int keep_resources(struct snd_ff *ff, unsigned int rate) 403 { 404 enum snd_ff_stream_mode mode; 405 int i; 406 int err; 407 408 // Check whether the given value is supported or not. 409 for (i = 0; i < CIP_SFC_COUNT; i++) { 410 if (amdtp_rate_table[i] == rate) 411 break; 412 } 413 if (i >= CIP_SFC_COUNT) 414 return -EINVAL; 415 416 err = snd_ff_stream_get_multiplier_mode(i, &mode); 417 if (err < 0) 418 return err; 419 420 /* Keep resources for in-stream. */ 421 ff->tx_resources.channels_mask = 0x00000000000000ffuLL; 422 err = fw_iso_resources_allocate(&ff->tx_resources, 423 amdtp_stream_get_max_payload(&ff->tx_stream), 424 fw_parent_device(ff->unit)->max_speed); 425 if (err < 0) 426 return err; 427 428 /* Keep resources for out-stream. */ 429 ff->rx_resources.channels_mask = 0x00000000000000ffuLL; 430 err = fw_iso_resources_allocate(&ff->rx_resources, 431 amdtp_stream_get_max_payload(&ff->rx_stream), 432 fw_parent_device(ff->unit)->max_speed); 433 if (err < 0) 434 fw_iso_resources_free(&ff->tx_resources); 435 436 return err; 437 } 438 439 static int ff400_begin_session(struct snd_ff *ff, unsigned int rate) 440 { 441 __le32 reg; 442 int err; 443 444 err = keep_resources(ff, rate); 445 if (err < 0) 446 return err; 447 448 /* Set the number of data blocks transferred in a second. */ 449 reg = cpu_to_le32(rate); 450 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 451 FF400_STF, ®, sizeof(reg), 0); 452 if (err < 0) 453 return err; 454 455 msleep(100); 456 457 /* 458 * Set isochronous channel and the number of quadlets of received 459 * packets. 460 */ 461 reg = cpu_to_le32(((ff->rx_stream.data_block_quadlets << 3) << 8) | 462 ff->rx_resources.channel); 463 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 464 FF400_RX_PACKET_FORMAT, ®, sizeof(reg), 0); 465 if (err < 0) 466 return err; 467 468 /* 469 * Set isochronous channel and the number of quadlets of transmitted 470 * packet. 471 */ 472 /* TODO: investigate the purpose of this 0x80. */ 473 reg = cpu_to_le32((0x80 << 24) | 474 (ff->tx_resources.channel << 5) | 475 (ff->tx_stream.data_block_quadlets)); 476 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 477 FF400_TX_PACKET_FORMAT, ®, sizeof(reg), 0); 478 if (err < 0) 479 return err; 480 481 /* Allow to transmit packets. */ 482 reg = cpu_to_le32(0x00000001); 483 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 484 FF400_ISOC_COMM_START, ®, sizeof(reg), 0); 485 } 486 487 static void ff400_finish_session(struct snd_ff *ff) 488 { 489 __le32 reg; 490 491 reg = cpu_to_le32(0x80000000); 492 snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST, 493 FF400_ISOC_COMM_STOP, ®, sizeof(reg), 0); 494 } 495 496 static void ff400_handle_midi_msg(struct snd_ff *ff, __le32 *buf, size_t length) 497 { 498 int i; 499 500 for (i = 0; i < length / 4; i++) { 501 u32 quad = le32_to_cpu(buf[i]); 502 u8 byte; 503 unsigned int index; 504 struct snd_rawmidi_substream *substream; 505 506 /* Message in first port. */ 507 /* 508 * This value may represent the index of this unit when the same 509 * units are on the same IEEE 1394 bus. This driver doesn't use 510 * it. 511 */ 512 index = (quad >> 8) & 0xff; 513 if (index > 0) { 514 substream = READ_ONCE(ff->tx_midi_substreams[0]); 515 if (substream != NULL) { 516 byte = quad & 0xff; 517 snd_rawmidi_receive(substream, &byte, 1); 518 } 519 } 520 521 /* Message in second port. */ 522 index = (quad >> 24) & 0xff; 523 if (index > 0) { 524 substream = READ_ONCE(ff->tx_midi_substreams[1]); 525 if (substream != NULL) { 526 byte = (quad >> 16) & 0xff; 527 snd_rawmidi_receive(substream, &byte, 1); 528 } 529 } 530 } 531 } 532 533 const struct snd_ff_protocol snd_ff_protocol_ff400 = { 534 .handle_midi_msg = ff400_handle_midi_msg, 535 .switch_fetching_mode = former_switch_fetching_mode, 536 .begin_session = ff400_begin_session, 537 .finish_session = ff400_finish_session, 538 .dump_status = former_dump_status, 539 }; 540