1 /* 2 * oxfw_stream.c - a part of driver for OXFW970/971 based devices 3 * 4 * Copyright (c) 2014 Takashi Sakamoto 5 * 6 * Licensed under the terms of the GNU General Public License, version 2. 7 */ 8 9 #include "oxfw.h" 10 #include <linux/delay.h> 11 12 #define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512 13 #define CALLBACK_TIMEOUT 200 14 15 /* 16 * According to datasheet of Oxford Semiconductor: 17 * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O 18 * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O 19 */ 20 static const unsigned int oxfw_rate_table[] = { 21 [0] = 32000, 22 [1] = 44100, 23 [2] = 48000, 24 [3] = 88200, 25 [4] = 96000, 26 [5] = 192000, 27 }; 28 29 /* 30 * See Table 5.7 – Sampling frequency for Multi-bit Audio 31 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA) 32 */ 33 static const unsigned int avc_stream_rate_table[] = { 34 [0] = 0x02, 35 [1] = 0x03, 36 [2] = 0x04, 37 [3] = 0x0a, 38 [4] = 0x05, 39 [5] = 0x07, 40 }; 41 42 static int set_rate(struct snd_oxfw *oxfw, unsigned int rate) 43 { 44 int err; 45 46 err = avc_general_set_sig_fmt(oxfw->unit, rate, 47 AVC_GENERAL_PLUG_DIR_IN, 0); 48 if (err < 0) 49 goto end; 50 51 if (oxfw->has_output) 52 err = avc_general_set_sig_fmt(oxfw->unit, rate, 53 AVC_GENERAL_PLUG_DIR_OUT, 0); 54 end: 55 return err; 56 } 57 58 static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, 59 unsigned int rate, unsigned int pcm_channels) 60 { 61 u8 **formats; 62 struct snd_oxfw_stream_formation formation; 63 enum avc_general_plug_dir dir; 64 unsigned int len; 65 int i, err; 66 67 if (s == &oxfw->tx_stream) { 68 formats = oxfw->tx_stream_formats; 69 dir = AVC_GENERAL_PLUG_DIR_OUT; 70 } else { 71 formats = oxfw->rx_stream_formats; 72 dir = AVC_GENERAL_PLUG_DIR_IN; 73 } 74 75 /* Seek stream format for requirements. */ 76 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { 77 err = snd_oxfw_stream_parse_format(formats[i], &formation); 78 if (err < 0) 79 return err; 80 81 if ((formation.rate == rate) && (formation.pcm == pcm_channels)) 82 break; 83 } 84 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) 85 return -EINVAL; 86 87 /* If assumed, just change rate. */ 88 if (oxfw->assumed) 89 return set_rate(oxfw, rate); 90 91 /* Calculate format length. */ 92 len = 5 + formats[i][4] * 2; 93 94 err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len); 95 if (err < 0) 96 return err; 97 98 /* Some requests just after changing format causes freezing. */ 99 msleep(100); 100 101 return 0; 102 } 103 104 static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) 105 { 106 amdtp_stream_pcm_abort(stream); 107 amdtp_stream_stop(stream); 108 109 if (stream == &oxfw->tx_stream) 110 cmp_connection_break(&oxfw->out_conn); 111 else 112 cmp_connection_break(&oxfw->in_conn); 113 } 114 115 static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) 116 { 117 u8 **formats; 118 enum avc_general_plug_dir dir; 119 struct cmp_connection *conn; 120 struct snd_oxfw_stream_formation formation; 121 int i; 122 int err; 123 124 if (stream == &oxfw->rx_stream) { 125 dir = AVC_GENERAL_PLUG_DIR_IN; 126 formats = oxfw->rx_stream_formats; 127 conn = &oxfw->in_conn; 128 } else { 129 dir = AVC_GENERAL_PLUG_DIR_OUT; 130 formats = oxfw->tx_stream_formats; 131 conn = &oxfw->out_conn; 132 } 133 134 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); 135 if (err < 0) 136 return err; 137 138 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { 139 struct snd_oxfw_stream_formation fmt; 140 141 if (formats[i] == NULL) 142 break; 143 144 err = snd_oxfw_stream_parse_format(formats[i], &fmt); 145 if (err < 0) 146 return err; 147 if (fmt.rate == formation.rate && fmt.pcm == formation.pcm && 148 fmt.midi == formation.midi) 149 break; 150 } 151 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) 152 return -EINVAL; 153 154 // The stream should have one pcm channels at least. 155 if (formation.pcm == 0) 156 return -EINVAL; 157 158 err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm, 159 formation.midi * 8, false); 160 if (err < 0) 161 goto end; 162 163 err = cmp_connection_establish(conn, 164 amdtp_stream_get_max_payload(stream)); 165 if (err < 0) 166 goto end; 167 168 err = amdtp_stream_start(stream, 169 conn->resources.channel, 170 conn->speed); 171 if (err < 0) { 172 cmp_connection_break(conn); 173 goto end; 174 } 175 176 /* Wait first packet */ 177 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) { 178 stop_stream(oxfw, stream); 179 err = -ETIMEDOUT; 180 } 181 end: 182 return err; 183 } 184 185 static int check_connection_used_by_others(struct snd_oxfw *oxfw, 186 struct amdtp_stream *stream) 187 { 188 struct cmp_connection *conn; 189 bool used; 190 int err; 191 192 if (stream == &oxfw->tx_stream) 193 conn = &oxfw->out_conn; 194 else 195 conn = &oxfw->in_conn; 196 197 err = cmp_connection_check_used(conn, &used); 198 if ((err >= 0) && used && !amdtp_stream_running(stream)) { 199 dev_err(&oxfw->unit->device, 200 "Connection established by others: %cPCR[%d]\n", 201 (conn->direction == CMP_OUTPUT) ? 'o' : 'i', 202 conn->pcr_index); 203 err = -EBUSY; 204 } 205 206 return err; 207 } 208 209 int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw, 210 struct amdtp_stream *stream) 211 { 212 struct cmp_connection *conn; 213 enum cmp_direction c_dir; 214 enum amdtp_stream_direction s_dir; 215 int err; 216 217 if (stream == &oxfw->tx_stream) { 218 conn = &oxfw->out_conn; 219 c_dir = CMP_OUTPUT; 220 s_dir = AMDTP_IN_STREAM; 221 } else { 222 conn = &oxfw->in_conn; 223 c_dir = CMP_INPUT; 224 s_dir = AMDTP_OUT_STREAM; 225 } 226 227 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0); 228 if (err < 0) 229 goto end; 230 231 err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING); 232 if (err < 0) { 233 amdtp_stream_destroy(stream); 234 cmp_connection_destroy(conn); 235 goto end; 236 } 237 238 /* 239 * OXFW starts to transmit packets with non-zero dbc. 240 * OXFW postpone transferring packets till handling any asynchronous 241 * packets. As a result, next isochronous packet includes more data 242 * blocks than IEC 61883-6 defines. 243 */ 244 if (stream == &oxfw->tx_stream) { 245 oxfw->tx_stream.flags |= CIP_JUMBO_PAYLOAD; 246 if (oxfw->wrong_dbs) 247 oxfw->tx_stream.flags |= CIP_WRONG_DBS; 248 } 249 end: 250 return err; 251 } 252 253 int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, 254 struct amdtp_stream *stream, 255 unsigned int rate, unsigned int pcm_channels) 256 { 257 struct snd_oxfw_stream_formation formation; 258 enum avc_general_plug_dir dir; 259 int err = 0; 260 261 if (oxfw->capture_substreams == 0 && oxfw->playback_substreams == 0) 262 return -EIO; 263 264 // Considering JACK/FFADO streaming: 265 // TODO: This can be removed hwdep functionality becomes popular. 266 err = check_connection_used_by_others(oxfw, &oxfw->rx_stream); 267 if (err < 0) 268 return err; 269 if (oxfw->has_output) { 270 err = check_connection_used_by_others(oxfw, &oxfw->tx_stream); 271 if (err < 0) 272 return err; 273 } 274 275 if (stream == &oxfw->tx_stream) 276 dir = AVC_GENERAL_PLUG_DIR_OUT; 277 else 278 dir = AVC_GENERAL_PLUG_DIR_IN; 279 280 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); 281 if (err < 0) 282 return err; 283 if (rate == 0) 284 rate = formation.rate; 285 if (pcm_channels == 0) 286 pcm_channels = formation.pcm; 287 288 if (formation.rate != rate || formation.pcm != pcm_channels || 289 amdtp_streaming_error(&oxfw->rx_stream) || 290 amdtp_streaming_error(&oxfw->tx_stream)) { 291 stop_stream(oxfw, &oxfw->rx_stream); 292 if (oxfw->has_output) 293 stop_stream(oxfw, &oxfw->tx_stream); 294 295 err = set_stream_format(oxfw, stream, rate, pcm_channels); 296 if (err < 0) { 297 dev_err(&oxfw->unit->device, 298 "fail to set stream format: %d\n", err); 299 return err; 300 } 301 } 302 303 if (!amdtp_stream_running(&oxfw->rx_stream)) { 304 err = start_stream(oxfw, &oxfw->rx_stream); 305 if (err < 0) { 306 dev_err(&oxfw->unit->device, 307 "fail to start rx stream: %d\n", err); 308 goto error; 309 } 310 } 311 312 if (oxfw->has_output) { 313 if (!amdtp_stream_running(&oxfw->tx_stream)) { 314 err = start_stream(oxfw, &oxfw->tx_stream); 315 if (err < 0) { 316 dev_err(&oxfw->unit->device, 317 "fail to start tx stream: %d\n", err); 318 goto error; 319 } 320 } 321 } 322 323 return 0; 324 error: 325 stop_stream(oxfw, &oxfw->rx_stream); 326 cmp_connection_break(&oxfw->in_conn); 327 if (oxfw->has_output) { 328 stop_stream(oxfw, &oxfw->tx_stream); 329 cmp_connection_break(&oxfw->out_conn); 330 } 331 return err; 332 } 333 334 void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw, 335 struct amdtp_stream *stream) 336 { 337 if (oxfw->capture_substreams == 0 && oxfw->playback_substreams == 0) { 338 stop_stream(oxfw, &oxfw->rx_stream); 339 340 if (oxfw->has_output) 341 stop_stream(oxfw, &oxfw->tx_stream); 342 } 343 } 344 345 /* 346 * This function should be called before starting the stream or after stopping 347 * the streams. 348 */ 349 void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw, 350 struct amdtp_stream *stream) 351 { 352 struct cmp_connection *conn; 353 354 if (stream == &oxfw->tx_stream) 355 conn = &oxfw->out_conn; 356 else 357 conn = &oxfw->in_conn; 358 359 amdtp_stream_destroy(stream); 360 cmp_connection_destroy(conn); 361 } 362 363 void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw, 364 struct amdtp_stream *stream) 365 { 366 stop_stream(oxfw, &oxfw->rx_stream); 367 368 if (oxfw->has_output) 369 stop_stream(oxfw, &oxfw->tx_stream); 370 } 371 372 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, 373 enum avc_general_plug_dir dir, 374 struct snd_oxfw_stream_formation *formation) 375 { 376 u8 *format; 377 unsigned int len; 378 int err; 379 380 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; 381 format = kmalloc(len, GFP_KERNEL); 382 if (format == NULL) 383 return -ENOMEM; 384 385 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len); 386 if (err < 0) 387 goto end; 388 if (len < 3) { 389 err = -EIO; 390 goto end; 391 } 392 393 err = snd_oxfw_stream_parse_format(format, formation); 394 end: 395 kfree(format); 396 return err; 397 } 398 399 /* 400 * See Table 6.16 - AM824 Stream Format 401 * Figure 6.19 - format_information field for AM824 Compound 402 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA) 403 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005 404 */ 405 int snd_oxfw_stream_parse_format(u8 *format, 406 struct snd_oxfw_stream_formation *formation) 407 { 408 unsigned int i, e, channels, type; 409 410 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation)); 411 412 /* 413 * this module can support a hierarchy combination that: 414 * Root: Audio and Music (0x90) 415 * Level 1: AM824 Compound (0x40) 416 */ 417 if ((format[0] != 0x90) || (format[1] != 0x40)) 418 return -ENOSYS; 419 420 /* check the sampling rate */ 421 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) { 422 if (format[2] == avc_stream_rate_table[i]) 423 break; 424 } 425 if (i == ARRAY_SIZE(avc_stream_rate_table)) 426 return -ENOSYS; 427 428 formation->rate = oxfw_rate_table[i]; 429 430 for (e = 0; e < format[4]; e++) { 431 channels = format[5 + e * 2]; 432 type = format[6 + e * 2]; 433 434 switch (type) { 435 /* IEC 60958 Conformant, currently handled as MBLA */ 436 case 0x00: 437 /* Multi Bit Linear Audio (Raw) */ 438 case 0x06: 439 formation->pcm += channels; 440 break; 441 /* MIDI Conformant */ 442 case 0x0d: 443 formation->midi = channels; 444 break; 445 /* IEC 61937-3 to 7 */ 446 case 0x01: 447 case 0x02: 448 case 0x03: 449 case 0x04: 450 case 0x05: 451 /* Multi Bit Linear Audio */ 452 case 0x07: /* DVD-Audio */ 453 case 0x0c: /* High Precision */ 454 /* One Bit Audio */ 455 case 0x08: /* (Plain) Raw */ 456 case 0x09: /* (Plain) SACD */ 457 case 0x0a: /* (Encoded) Raw */ 458 case 0x0b: /* (Encoded) SACD */ 459 /* SMPTE Time-Code conformant */ 460 case 0x0e: 461 /* Sample Count */ 462 case 0x0f: 463 /* Anciliary Data */ 464 case 0x10: 465 /* Synchronization Stream (Stereo Raw audio) */ 466 case 0x40: 467 /* Don't care */ 468 case 0xff: 469 default: 470 return -ENOSYS; /* not supported */ 471 } 472 } 473 474 if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM || 475 formation->midi > AM824_MAX_CHANNELS_FOR_MIDI) 476 return -ENOSYS; 477 478 return 0; 479 } 480 481 static int 482 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, 483 unsigned int pid, u8 *buf, unsigned int *len, 484 u8 **formats) 485 { 486 struct snd_oxfw_stream_formation formation; 487 unsigned int i, eid; 488 int err; 489 490 /* get format at current sampling rate */ 491 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len); 492 if (err < 0) { 493 dev_err(&oxfw->unit->device, 494 "fail to get current stream format for isoc %s plug %d:%d\n", 495 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out", 496 pid, err); 497 goto end; 498 } 499 500 /* parse and set stream format */ 501 eid = 0; 502 err = snd_oxfw_stream_parse_format(buf, &formation); 503 if (err < 0) 504 goto end; 505 506 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len, 507 GFP_KERNEL); 508 if (!formats[eid]) { 509 err = -ENOMEM; 510 goto end; 511 } 512 513 /* apply the format for each available sampling rate */ 514 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) { 515 if (formation.rate == oxfw_rate_table[i]) 516 continue; 517 518 err = avc_general_inquiry_sig_fmt(oxfw->unit, 519 oxfw_rate_table[i], 520 dir, pid); 521 if (err < 0) 522 continue; 523 524 eid++; 525 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len, 526 GFP_KERNEL); 527 if (formats[eid] == NULL) { 528 err = -ENOMEM; 529 goto end; 530 } 531 formats[eid][2] = avc_stream_rate_table[i]; 532 } 533 534 err = 0; 535 oxfw->assumed = true; 536 end: 537 return err; 538 } 539 540 static int fill_stream_formats(struct snd_oxfw *oxfw, 541 enum avc_general_plug_dir dir, 542 unsigned short pid) 543 { 544 u8 *buf, **formats; 545 unsigned int len, eid = 0; 546 struct snd_oxfw_stream_formation dummy; 547 int err; 548 549 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL); 550 if (buf == NULL) 551 return -ENOMEM; 552 553 if (dir == AVC_GENERAL_PLUG_DIR_OUT) 554 formats = oxfw->tx_stream_formats; 555 else 556 formats = oxfw->rx_stream_formats; 557 558 /* get first entry */ 559 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; 560 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0); 561 if (err == -ENOSYS) { 562 /* LIST subfunction is not implemented */ 563 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; 564 err = assume_stream_formats(oxfw, dir, pid, buf, &len, 565 formats); 566 goto end; 567 } else if (err < 0) { 568 dev_err(&oxfw->unit->device, 569 "fail to get stream format %d for isoc %s plug %d:%d\n", 570 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out", 571 pid, err); 572 goto end; 573 } 574 575 /* LIST subfunction is implemented */ 576 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) { 577 /* The format is too short. */ 578 if (len < 3) { 579 err = -EIO; 580 break; 581 } 582 583 /* parse and set stream format */ 584 err = snd_oxfw_stream_parse_format(buf, &dummy); 585 if (err < 0) 586 break; 587 588 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len, 589 GFP_KERNEL); 590 if (!formats[eid]) { 591 err = -ENOMEM; 592 break; 593 } 594 595 /* get next entry */ 596 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; 597 err = avc_stream_get_format_list(oxfw->unit, dir, 0, 598 buf, &len, ++eid); 599 /* No entries remained. */ 600 if (err == -EINVAL) { 601 err = 0; 602 break; 603 } else if (err < 0) { 604 dev_err(&oxfw->unit->device, 605 "fail to get stream format %d for isoc %s plug %d:%d\n", 606 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : 607 "out", 608 pid, err); 609 break; 610 } 611 } 612 end: 613 kfree(buf); 614 return err; 615 } 616 617 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) 618 { 619 u8 plugs[AVC_PLUG_INFO_BUF_BYTES]; 620 struct snd_oxfw_stream_formation formation; 621 u8 *format; 622 unsigned int i; 623 int err; 624 625 /* the number of plugs for isoc in/out, ext in/out */ 626 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs); 627 if (err < 0) { 628 dev_err(&oxfw->unit->device, 629 "fail to get info for isoc/external in/out plugs: %d\n", 630 err); 631 goto end; 632 } else if ((plugs[0] == 0) && (plugs[1] == 0)) { 633 err = -ENOSYS; 634 goto end; 635 } 636 637 /* use oPCR[0] if exists */ 638 if (plugs[1] > 0) { 639 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0); 640 if (err < 0) 641 goto end; 642 643 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { 644 format = oxfw->tx_stream_formats[i]; 645 if (format == NULL) 646 continue; 647 err = snd_oxfw_stream_parse_format(format, &formation); 648 if (err < 0) 649 continue; 650 651 /* Add one MIDI port. */ 652 if (formation.midi > 0) 653 oxfw->midi_input_ports = 1; 654 } 655 656 oxfw->has_output = true; 657 } 658 659 /* use iPCR[0] if exists */ 660 if (plugs[0] > 0) { 661 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0); 662 if (err < 0) 663 goto end; 664 665 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { 666 format = oxfw->rx_stream_formats[i]; 667 if (format == NULL) 668 continue; 669 err = snd_oxfw_stream_parse_format(format, &formation); 670 if (err < 0) 671 continue; 672 673 /* Add one MIDI port. */ 674 if (formation.midi > 0) 675 oxfw->midi_output_ports = 1; 676 } 677 } 678 end: 679 return err; 680 } 681 682 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw) 683 { 684 oxfw->dev_lock_changed = true; 685 wake_up(&oxfw->hwdep_wait); 686 } 687 688 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw) 689 { 690 int err; 691 692 spin_lock_irq(&oxfw->lock); 693 694 /* user land lock this */ 695 if (oxfw->dev_lock_count < 0) { 696 err = -EBUSY; 697 goto end; 698 } 699 700 /* this is the first time */ 701 if (oxfw->dev_lock_count++ == 0) 702 snd_oxfw_stream_lock_changed(oxfw); 703 err = 0; 704 end: 705 spin_unlock_irq(&oxfw->lock); 706 return err; 707 } 708 709 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw) 710 { 711 spin_lock_irq(&oxfw->lock); 712 713 if (WARN_ON(oxfw->dev_lock_count <= 0)) 714 goto end; 715 if (--oxfw->dev_lock_count == 0) 716 snd_oxfw_stream_lock_changed(oxfw); 717 end: 718 spin_unlock_irq(&oxfw->lock); 719 } 720