1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Apple iSight audio driver 4 * 5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 6 */ 7 8 #include <asm/byteorder.h> 9 #include <linux/delay.h> 10 #include <linux/device.h> 11 #include <linux/firewire.h> 12 #include <linux/firewire-constants.h> 13 #include <linux/module.h> 14 #include <linux/mod_devicetable.h> 15 #include <linux/mutex.h> 16 #include <linux/string.h> 17 #include <sound/control.h> 18 #include <sound/core.h> 19 #include <sound/initval.h> 20 #include <sound/pcm.h> 21 #include <sound/tlv.h> 22 #include "lib.h" 23 #include "iso-resources.h" 24 #include "packets-buffer.h" 25 26 #define OUI_APPLE 0x000a27 27 #define MODEL_APPLE_ISIGHT 0x000008 28 #define SW_ISIGHT_AUDIO 0x000010 29 30 #define REG_AUDIO_ENABLE 0x000 31 #define AUDIO_ENABLE 0x80000000 32 #define REG_DEF_AUDIO_GAIN 0x204 33 #define REG_GAIN_RAW_START 0x210 34 #define REG_GAIN_RAW_END 0x214 35 #define REG_GAIN_DB_START 0x218 36 #define REG_GAIN_DB_END 0x21c 37 #define REG_SAMPLE_RATE_INQUIRY 0x280 38 #define REG_ISO_TX_CONFIG 0x300 39 #define SPEED_SHIFT 16 40 #define REG_SAMPLE_RATE 0x400 41 #define RATE_48000 0x80000000 42 #define REG_GAIN 0x500 43 #define REG_MUTE 0x504 44 45 #define MAX_FRAMES_PER_PACKET 475 46 47 #define QUEUE_LENGTH 20 48 49 struct isight { 50 struct snd_card *card; 51 struct fw_unit *unit; 52 struct fw_device *device; 53 u64 audio_base; 54 struct snd_pcm_substream *pcm; 55 struct mutex mutex; 56 struct iso_packets_buffer buffer; 57 struct fw_iso_resources resources; 58 struct fw_iso_context *context; 59 bool pcm_active; 60 bool pcm_running; 61 bool first_packet; 62 int packet_index; 63 u32 total_samples; 64 unsigned int buffer_pointer; 65 unsigned int period_counter; 66 s32 gain_min, gain_max; 67 unsigned int gain_tlv[4]; 68 }; 69 70 struct audio_payload { 71 __be32 sample_count; 72 __be32 signature; 73 __be32 sample_total; 74 __be32 reserved; 75 __be16 samples[2 * MAX_FRAMES_PER_PACKET]; 76 }; 77 78 MODULE_DESCRIPTION("iSight audio driver"); 79 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 80 MODULE_LICENSE("GPL"); 81 82 static struct fw_iso_packet audio_packet = { 83 .payload_length = sizeof(struct audio_payload), 84 .interrupt = 1, 85 .header_length = 4, 86 }; 87 88 static void isight_update_pointers(struct isight *isight, unsigned int count) 89 { 90 struct snd_pcm_runtime *runtime = isight->pcm->runtime; 91 unsigned int ptr; 92 93 smp_wmb(); /* update buffer data before buffer pointer */ 94 95 ptr = isight->buffer_pointer; 96 ptr += count; 97 if (ptr >= runtime->buffer_size) 98 ptr -= runtime->buffer_size; 99 WRITE_ONCE(isight->buffer_pointer, ptr); 100 101 isight->period_counter += count; 102 if (isight->period_counter >= runtime->period_size) { 103 isight->period_counter -= runtime->period_size; 104 snd_pcm_period_elapsed(isight->pcm); 105 } 106 } 107 108 static void isight_samples(struct isight *isight, 109 const __be16 *samples, unsigned int count) 110 { 111 struct snd_pcm_runtime *runtime; 112 unsigned int count1; 113 114 if (!READ_ONCE(isight->pcm_running)) 115 return; 116 117 runtime = isight->pcm->runtime; 118 if (isight->buffer_pointer + count <= runtime->buffer_size) { 119 memcpy(runtime->dma_area + isight->buffer_pointer * 4, 120 samples, count * 4); 121 } else { 122 count1 = runtime->buffer_size - isight->buffer_pointer; 123 memcpy(runtime->dma_area + isight->buffer_pointer * 4, 124 samples, count1 * 4); 125 samples += count1 * 2; 126 memcpy(runtime->dma_area, samples, (count - count1) * 4); 127 } 128 129 isight_update_pointers(isight, count); 130 } 131 132 static void isight_pcm_abort(struct isight *isight) 133 { 134 if (READ_ONCE(isight->pcm_active)) 135 snd_pcm_stop_xrun(isight->pcm); 136 } 137 138 static void isight_dropped_samples(struct isight *isight, unsigned int total) 139 { 140 struct snd_pcm_runtime *runtime; 141 u32 dropped; 142 unsigned int count1; 143 144 if (!READ_ONCE(isight->pcm_running)) 145 return; 146 147 runtime = isight->pcm->runtime; 148 dropped = total - isight->total_samples; 149 if (dropped < runtime->buffer_size) { 150 if (isight->buffer_pointer + dropped <= runtime->buffer_size) { 151 memset(runtime->dma_area + isight->buffer_pointer * 4, 152 0, dropped * 4); 153 } else { 154 count1 = runtime->buffer_size - isight->buffer_pointer; 155 memset(runtime->dma_area + isight->buffer_pointer * 4, 156 0, count1 * 4); 157 memset(runtime->dma_area, 0, (dropped - count1) * 4); 158 } 159 isight_update_pointers(isight, dropped); 160 } else { 161 isight_pcm_abort(isight); 162 } 163 } 164 165 static void isight_packet(struct fw_iso_context *context, u32 cycle, 166 size_t header_length, void *header, void *data) 167 { 168 struct isight *isight = data; 169 const struct audio_payload *payload; 170 unsigned int index, length, count, total; 171 int err; 172 173 if (isight->packet_index < 0) 174 return; 175 index = isight->packet_index; 176 payload = isight->buffer.packets[index].buffer; 177 length = be32_to_cpup(header) >> 16; 178 179 if (likely(length >= 16 && 180 payload->signature == cpu_to_be32(0x73676874/*"sght"*/))) { 181 count = be32_to_cpu(payload->sample_count); 182 if (likely(count <= (length - 16) / 4 && 183 count <= MAX_FRAMES_PER_PACKET)) { 184 total = be32_to_cpu(payload->sample_total); 185 if (unlikely(total != isight->total_samples)) { 186 if (!isight->first_packet) 187 isight_dropped_samples(isight, total); 188 isight->first_packet = false; 189 isight->total_samples = total; 190 } 191 192 isight_samples(isight, payload->samples, count); 193 isight->total_samples += count; 194 } 195 } 196 197 err = fw_iso_context_queue(isight->context, &audio_packet, 198 &isight->buffer.iso_buffer, 199 isight->buffer.packets[index].offset); 200 if (err < 0) { 201 dev_err(&isight->unit->device, "queueing error: %d\n", err); 202 isight_pcm_abort(isight); 203 isight->packet_index = -1; 204 return; 205 } 206 fw_iso_context_queue_flush(isight->context); 207 208 if (++index >= QUEUE_LENGTH) 209 index = 0; 210 isight->packet_index = index; 211 } 212 213 static int isight_connect(struct isight *isight) 214 { 215 int ch, err; 216 __be32 value; 217 218 retry_after_bus_reset: 219 ch = fw_iso_resources_allocate(&isight->resources, 220 sizeof(struct audio_payload), 221 isight->device->max_speed); 222 if (ch < 0) { 223 err = ch; 224 goto error; 225 } 226 227 value = cpu_to_be32(ch | (isight->device->max_speed << SPEED_SHIFT)); 228 err = snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST, 229 isight->audio_base + REG_ISO_TX_CONFIG, 230 &value, 4, FW_FIXED_GENERATION | 231 isight->resources.generation); 232 if (err == -EAGAIN) { 233 fw_iso_resources_free(&isight->resources); 234 goto retry_after_bus_reset; 235 } else if (err < 0) { 236 goto err_resources; 237 } 238 239 return 0; 240 241 err_resources: 242 fw_iso_resources_free(&isight->resources); 243 error: 244 return err; 245 } 246 247 static int isight_open(struct snd_pcm_substream *substream) 248 { 249 static const struct snd_pcm_hardware hardware = { 250 .info = SNDRV_PCM_INFO_MMAP | 251 SNDRV_PCM_INFO_MMAP_VALID | 252 SNDRV_PCM_INFO_BATCH | 253 SNDRV_PCM_INFO_INTERLEAVED | 254 SNDRV_PCM_INFO_BLOCK_TRANSFER, 255 .formats = SNDRV_PCM_FMTBIT_S16_BE, 256 .rates = SNDRV_PCM_RATE_48000, 257 .rate_min = 48000, 258 .rate_max = 48000, 259 .channels_min = 2, 260 .channels_max = 2, 261 .buffer_bytes_max = 4 * 1024 * 1024, 262 .period_bytes_min = MAX_FRAMES_PER_PACKET * 4, 263 .period_bytes_max = 1024 * 1024, 264 .periods_min = 2, 265 .periods_max = UINT_MAX, 266 }; 267 struct isight *isight = substream->private_data; 268 269 substream->runtime->hw = hardware; 270 271 return iso_packets_buffer_init(&isight->buffer, isight->unit, 272 QUEUE_LENGTH, 273 sizeof(struct audio_payload), 274 DMA_FROM_DEVICE); 275 } 276 277 static int isight_close(struct snd_pcm_substream *substream) 278 { 279 struct isight *isight = substream->private_data; 280 281 iso_packets_buffer_destroy(&isight->buffer, isight->unit); 282 283 return 0; 284 } 285 286 static int isight_hw_params(struct snd_pcm_substream *substream, 287 struct snd_pcm_hw_params *hw_params) 288 { 289 struct isight *isight = substream->private_data; 290 291 WRITE_ONCE(isight->pcm_active, true); 292 293 return 0; 294 } 295 296 static int reg_read(struct isight *isight, int offset, __be32 *value) 297 { 298 return snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST, 299 isight->audio_base + offset, value, 4, 0); 300 } 301 302 static int reg_write(struct isight *isight, int offset, __be32 value) 303 { 304 return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST, 305 isight->audio_base + offset, &value, 4, 0); 306 } 307 308 static void isight_stop_streaming(struct isight *isight) 309 { 310 __be32 value; 311 312 if (!isight->context) 313 return; 314 315 fw_iso_context_stop(isight->context); 316 fw_iso_context_destroy(isight->context); 317 isight->context = NULL; 318 fw_iso_resources_free(&isight->resources); 319 value = 0; 320 snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST, 321 isight->audio_base + REG_AUDIO_ENABLE, 322 &value, 4, FW_QUIET); 323 } 324 325 static int isight_hw_free(struct snd_pcm_substream *substream) 326 { 327 struct isight *isight = substream->private_data; 328 329 WRITE_ONCE(isight->pcm_active, false); 330 331 guard(mutex)(&isight->mutex); 332 isight_stop_streaming(isight); 333 334 return 0; 335 } 336 337 static int isight_start_streaming(struct isight *isight) 338 { 339 unsigned int i; 340 int err; 341 342 if (isight->context) { 343 if (isight->packet_index < 0) 344 isight_stop_streaming(isight); 345 else 346 return 0; 347 } 348 349 err = reg_write(isight, REG_SAMPLE_RATE, cpu_to_be32(RATE_48000)); 350 if (err < 0) 351 goto error; 352 353 err = isight_connect(isight); 354 if (err < 0) 355 goto error; 356 357 err = reg_write(isight, REG_AUDIO_ENABLE, cpu_to_be32(AUDIO_ENABLE)); 358 if (err < 0) 359 goto err_resources; 360 361 isight->context = fw_iso_context_create(isight->device->card, 362 FW_ISO_CONTEXT_RECEIVE, 363 isight->resources.channel, 364 isight->device->max_speed, 365 4, isight_packet, isight); 366 if (IS_ERR(isight->context)) { 367 err = PTR_ERR(isight->context); 368 isight->context = NULL; 369 goto err_resources; 370 } 371 372 for (i = 0; i < QUEUE_LENGTH; ++i) { 373 err = fw_iso_context_queue(isight->context, &audio_packet, 374 &isight->buffer.iso_buffer, 375 isight->buffer.packets[i].offset); 376 if (err < 0) 377 goto err_context; 378 } 379 380 isight->first_packet = true; 381 isight->packet_index = 0; 382 383 err = fw_iso_context_start(isight->context, -1, 0, 384 FW_ISO_CONTEXT_MATCH_ALL_TAGS/*?*/); 385 if (err < 0) 386 goto err_context; 387 388 return 0; 389 390 err_context: 391 fw_iso_context_destroy(isight->context); 392 isight->context = NULL; 393 err_resources: 394 fw_iso_resources_free(&isight->resources); 395 reg_write(isight, REG_AUDIO_ENABLE, 0); 396 error: 397 return err; 398 } 399 400 static int isight_prepare(struct snd_pcm_substream *substream) 401 { 402 struct isight *isight = substream->private_data; 403 404 isight->buffer_pointer = 0; 405 isight->period_counter = 0; 406 407 guard(mutex)(&isight->mutex); 408 return isight_start_streaming(isight); 409 } 410 411 static int isight_trigger(struct snd_pcm_substream *substream, int cmd) 412 { 413 struct isight *isight = substream->private_data; 414 415 switch (cmd) { 416 case SNDRV_PCM_TRIGGER_START: 417 WRITE_ONCE(isight->pcm_running, true); 418 break; 419 case SNDRV_PCM_TRIGGER_STOP: 420 WRITE_ONCE(isight->pcm_running, false); 421 break; 422 default: 423 return -EINVAL; 424 } 425 return 0; 426 } 427 428 static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream) 429 { 430 struct isight *isight = substream->private_data; 431 432 return READ_ONCE(isight->buffer_pointer); 433 } 434 435 static int isight_create_pcm(struct isight *isight) 436 { 437 static const struct snd_pcm_ops ops = { 438 .open = isight_open, 439 .close = isight_close, 440 .hw_params = isight_hw_params, 441 .hw_free = isight_hw_free, 442 .prepare = isight_prepare, 443 .trigger = isight_trigger, 444 .pointer = isight_pointer, 445 }; 446 struct snd_pcm *pcm; 447 int err; 448 449 err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm); 450 if (err < 0) 451 return err; 452 pcm->private_data = isight; 453 pcm->nonatomic = true; 454 strscpy(pcm->name, "iSight"); 455 isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 456 isight->pcm->ops = &ops; 457 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); 458 459 return 0; 460 } 461 462 static int isight_gain_info(struct snd_kcontrol *ctl, 463 struct snd_ctl_elem_info *info) 464 { 465 struct isight *isight = ctl->private_data; 466 467 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 468 info->count = 1; 469 info->value.integer.min = isight->gain_min; 470 info->value.integer.max = isight->gain_max; 471 472 return 0; 473 } 474 475 static int isight_gain_get(struct snd_kcontrol *ctl, 476 struct snd_ctl_elem_value *value) 477 { 478 struct isight *isight = ctl->private_data; 479 __be32 gain; 480 int err; 481 482 err = reg_read(isight, REG_GAIN, &gain); 483 if (err < 0) 484 return err; 485 486 value->value.integer.value[0] = (s32)be32_to_cpu(gain); 487 488 return 0; 489 } 490 491 static int isight_gain_put(struct snd_kcontrol *ctl, 492 struct snd_ctl_elem_value *value) 493 { 494 struct isight *isight = ctl->private_data; 495 496 if (value->value.integer.value[0] < isight->gain_min || 497 value->value.integer.value[0] > isight->gain_max) 498 return -EINVAL; 499 500 return reg_write(isight, REG_GAIN, 501 cpu_to_be32(value->value.integer.value[0])); 502 } 503 504 static int isight_mute_get(struct snd_kcontrol *ctl, 505 struct snd_ctl_elem_value *value) 506 { 507 struct isight *isight = ctl->private_data; 508 __be32 mute; 509 int err; 510 511 err = reg_read(isight, REG_MUTE, &mute); 512 if (err < 0) 513 return err; 514 515 value->value.integer.value[0] = !mute; 516 517 return 0; 518 } 519 520 static int isight_mute_put(struct snd_kcontrol *ctl, 521 struct snd_ctl_elem_value *value) 522 { 523 struct isight *isight = ctl->private_data; 524 525 return reg_write(isight, REG_MUTE, 526 (__force __be32)!value->value.integer.value[0]); 527 } 528 529 static int isight_create_mixer(struct isight *isight) 530 { 531 static const struct snd_kcontrol_new gain_control = { 532 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 533 .name = "Mic Capture Volume", 534 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 535 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 536 .info = isight_gain_info, 537 .get = isight_gain_get, 538 .put = isight_gain_put, 539 }; 540 static const struct snd_kcontrol_new mute_control = { 541 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 542 .name = "Mic Capture Switch", 543 .info = snd_ctl_boolean_mono_info, 544 .get = isight_mute_get, 545 .put = isight_mute_put, 546 }; 547 __be32 value; 548 struct snd_kcontrol *ctl; 549 int err; 550 551 err = reg_read(isight, REG_GAIN_RAW_START, &value); 552 if (err < 0) 553 return err; 554 isight->gain_min = be32_to_cpu(value); 555 556 err = reg_read(isight, REG_GAIN_RAW_END, &value); 557 if (err < 0) 558 return err; 559 isight->gain_max = be32_to_cpu(value); 560 561 isight->gain_tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_MINMAX; 562 isight->gain_tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int); 563 564 err = reg_read(isight, REG_GAIN_DB_START, &value); 565 if (err < 0) 566 return err; 567 isight->gain_tlv[SNDRV_CTL_TLVO_DB_MINMAX_MIN] = 568 (s32)be32_to_cpu(value) * 100; 569 570 err = reg_read(isight, REG_GAIN_DB_END, &value); 571 if (err < 0) 572 return err; 573 isight->gain_tlv[SNDRV_CTL_TLVO_DB_MINMAX_MAX] = 574 (s32)be32_to_cpu(value) * 100; 575 576 ctl = snd_ctl_new1(&gain_control, isight); 577 if (ctl) 578 ctl->tlv.p = isight->gain_tlv; 579 err = snd_ctl_add(isight->card, ctl); 580 if (err < 0) 581 return err; 582 583 err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight)); 584 if (err < 0) 585 return err; 586 587 return 0; 588 } 589 590 static void isight_card_free(struct snd_card *card) 591 { 592 struct isight *isight = card->private_data; 593 594 fw_iso_resources_destroy(&isight->resources); 595 } 596 597 static u64 get_unit_base(struct fw_unit *unit) 598 { 599 struct fw_csr_iterator i; 600 int key, value; 601 602 fw_csr_iterator_init(&i, unit->directory); 603 while (fw_csr_iterator_next(&i, &key, &value)) 604 if (key == CSR_OFFSET) 605 return CSR_REGISTER_BASE + value * 4; 606 return 0; 607 } 608 609 static int isight_probe(struct fw_unit *unit, 610 const struct ieee1394_device_id *id) 611 { 612 struct fw_device *fw_dev = fw_parent_device(unit); 613 struct snd_card *card; 614 struct isight *isight; 615 int err; 616 617 err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, 618 sizeof(*isight), &card); 619 if (err < 0) 620 return err; 621 622 isight = card->private_data; 623 isight->card = card; 624 mutex_init(&isight->mutex); 625 isight->unit = fw_unit_get(unit); 626 isight->device = fw_dev; 627 isight->audio_base = get_unit_base(unit); 628 if (!isight->audio_base) { 629 dev_err(&unit->device, "audio unit base not found\n"); 630 err = -ENXIO; 631 goto error; 632 } 633 fw_iso_resources_init(&isight->resources, unit); 634 635 card->private_free = isight_card_free; 636 637 strscpy(card->driver, "iSight"); 638 strscpy(card->shortname, "Apple iSight"); 639 snprintf(card->longname, sizeof(card->longname), 640 "Apple iSight (GUID %08x%08x) at %s, S%d", 641 fw_dev->config_rom[3], fw_dev->config_rom[4], 642 dev_name(&unit->device), 100 << fw_dev->max_speed); 643 strscpy(card->mixername, "iSight"); 644 645 err = isight_create_pcm(isight); 646 if (err < 0) 647 goto error; 648 649 err = isight_create_mixer(isight); 650 if (err < 0) 651 goto error; 652 653 err = snd_card_register(card); 654 if (err < 0) 655 goto error; 656 657 dev_set_drvdata(&unit->device, isight); 658 659 return 0; 660 error: 661 snd_card_free(card); 662 663 mutex_destroy(&isight->mutex); 664 fw_unit_put(isight->unit); 665 666 return err; 667 } 668 669 static void isight_bus_reset(struct fw_unit *unit) 670 { 671 struct isight *isight = dev_get_drvdata(&unit->device); 672 673 if (fw_iso_resources_update(&isight->resources) < 0) { 674 isight_pcm_abort(isight); 675 676 guard(mutex)(&isight->mutex); 677 isight_stop_streaming(isight); 678 } 679 } 680 681 static void isight_remove(struct fw_unit *unit) 682 { 683 struct isight *isight = dev_get_drvdata(&unit->device); 684 685 isight_pcm_abort(isight); 686 687 snd_card_disconnect(isight->card); 688 689 scoped_guard(mutex, &isight->mutex) { 690 isight_stop_streaming(isight); 691 } 692 693 // Block till all of ALSA character devices are released. 694 snd_card_free(isight->card); 695 696 mutex_destroy(&isight->mutex); 697 fw_unit_put(isight->unit); 698 } 699 700 static const struct ieee1394_device_id isight_id_table[] = { 701 { 702 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | 703 IEEE1394_MATCH_VERSION, 704 .specifier_id = OUI_APPLE, 705 .version = SW_ISIGHT_AUDIO, 706 }, 707 { } 708 }; 709 MODULE_DEVICE_TABLE(ieee1394, isight_id_table); 710 711 static struct fw_driver isight_driver = { 712 .driver = { 713 .owner = THIS_MODULE, 714 .name = KBUILD_MODNAME, 715 .bus = &fw_bus_type, 716 }, 717 .probe = isight_probe, 718 .update = isight_bus_reset, 719 .remove = isight_remove, 720 .id_table = isight_id_table, 721 }; 722 723 static int __init alsa_isight_init(void) 724 { 725 return driver_register(&isight_driver.driver); 726 } 727 728 static void __exit alsa_isight_exit(void) 729 { 730 driver_unregister(&isight_driver.driver); 731 } 732 733 module_init(alsa_isight_init); 734 module_exit(alsa_isight_exit); 735