2 * Apple iSight audio driver
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
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>
23 #include "iso-resources.h"
24 #include "packets-buffer.h"
26 #define OUI_APPLE 0x000a27
27 #define MODEL_APPLE_ISIGHT 0x000008
28 #define SW_ISIGHT_AUDIO 0x000010
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
45 #define MAX_FRAMES_PER_PACKET 475
47 #define QUEUE_LENGTH 20
50 struct snd_card *card;
52 struct fw_device *device;
54 struct snd_pcm_substream *pcm;
56 struct iso_packets_buffer buffer;
57 struct fw_iso_resources resources;
58 struct fw_iso_context *context;
64 unsigned int buffer_pointer;
65 unsigned int period_counter;
66 s32 gain_min, gain_max;
67 unsigned int gain_tlv[4];
70 struct audio_payload {
75 __be16 samples[2 * MAX_FRAMES_PER_PACKET];
78 MODULE_DESCRIPTION("iSight audio driver");
79 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
80 MODULE_LICENSE("GPL v2");
82 static struct fw_iso_packet audio_packet = {
83 .payload_length = sizeof(struct audio_payload),
88 static void isight_update_pointers(struct isight *isight, unsigned int count)
90 struct snd_pcm_runtime *runtime = isight->pcm->runtime;
93 smp_wmb(); /* update buffer data before buffer pointer */
95 ptr = isight->buffer_pointer;
97 if (ptr >= runtime->buffer_size)
98 ptr -= runtime->buffer_size;
99 ACCESS_ONCE(isight->buffer_pointer) = ptr;
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);
108 static void isight_samples(struct isight *isight,
109 const __be16 *samples, unsigned int count)
111 struct snd_pcm_runtime *runtime;
114 if (!ACCESS_ONCE(isight->pcm_running))
117 runtime = isight->pcm->runtime;
118 if (isight->buffer_pointer + count <= runtime->buffer_size) {
119 memcpy(runtime->dma_area + isight->buffer_pointer * 4,
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);
129 isight_update_pointers(isight, count);
132 static void isight_pcm_abort(struct isight *isight)
136 if (ACCESS_ONCE(isight->pcm_active)) {
137 snd_pcm_stream_lock_irqsave(isight->pcm, flags);
138 if (snd_pcm_running(isight->pcm))
139 snd_pcm_stop(isight->pcm, SNDRV_PCM_STATE_XRUN);
140 snd_pcm_stream_unlock_irqrestore(isight->pcm, flags);
144 static void isight_dropped_samples(struct isight *isight, unsigned int total)
146 struct snd_pcm_runtime *runtime;
150 if (!ACCESS_ONCE(isight->pcm_running))
153 runtime = isight->pcm->runtime;
154 dropped = total - isight->total_samples;
155 if (dropped < runtime->buffer_size) {
156 if (isight->buffer_pointer + dropped <= runtime->buffer_size) {
157 memset(runtime->dma_area + isight->buffer_pointer * 4,
160 count1 = runtime->buffer_size - isight->buffer_pointer;
161 memset(runtime->dma_area + isight->buffer_pointer * 4,
163 memset(runtime->dma_area, 0, (dropped - count1) * 4);
165 isight_update_pointers(isight, dropped);
167 isight_pcm_abort(isight);
171 static void isight_packet(struct fw_iso_context *context, u32 cycle,
172 size_t header_length, void *header, void *data)
174 struct isight *isight = data;
175 const struct audio_payload *payload;
176 unsigned int index, length, count, total;
179 if (isight->packet_index < 0)
181 index = isight->packet_index;
182 payload = isight->buffer.packets[index].buffer;
183 length = be32_to_cpup(header) >> 16;
185 if (likely(length >= 16 &&
186 payload->signature == cpu_to_be32(0x73676874/*"sght"*/))) {
187 count = be32_to_cpu(payload->sample_count);
188 if (likely(count <= (length - 16) / 4)) {
189 total = be32_to_cpu(payload->sample_total);
190 if (unlikely(total != isight->total_samples)) {
191 if (!isight->first_packet)
192 isight_dropped_samples(isight, total);
193 isight->first_packet = false;
194 isight->total_samples = total;
197 isight_samples(isight, payload->samples, count);
198 isight->total_samples += count;
202 err = fw_iso_context_queue(isight->context, &audio_packet,
203 &isight->buffer.iso_buffer,
204 isight->buffer.packets[index].offset);
206 dev_err(&isight->unit->device, "queueing error: %d\n", err);
207 isight_pcm_abort(isight);
208 isight->packet_index = -1;
211 fw_iso_context_queue_flush(isight->context);
213 if (++index >= QUEUE_LENGTH)
215 isight->packet_index = index;
218 static int isight_connect(struct isight *isight)
223 retry_after_bus_reset:
224 ch = fw_iso_resources_allocate(&isight->resources,
225 sizeof(struct audio_payload),
226 isight->device->max_speed);
232 value = cpu_to_be32(ch | (isight->device->max_speed << SPEED_SHIFT));
233 err = snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
234 isight->audio_base + REG_ISO_TX_CONFIG,
235 &value, 4, FW_FIXED_GENERATION |
236 isight->resources.generation);
237 if (err == -EAGAIN) {
238 fw_iso_resources_free(&isight->resources);
239 goto retry_after_bus_reset;
240 } else if (err < 0) {
247 fw_iso_resources_free(&isight->resources);
252 static int isight_open(struct snd_pcm_substream *substream)
254 static const struct snd_pcm_hardware hardware = {
255 .info = SNDRV_PCM_INFO_MMAP |
256 SNDRV_PCM_INFO_MMAP_VALID |
257 SNDRV_PCM_INFO_BATCH |
258 SNDRV_PCM_INFO_INTERLEAVED |
259 SNDRV_PCM_INFO_BLOCK_TRANSFER,
260 .formats = SNDRV_PCM_FMTBIT_S16_BE,
261 .rates = SNDRV_PCM_RATE_48000,
266 .buffer_bytes_max = 4 * 1024 * 1024,
267 .period_bytes_min = MAX_FRAMES_PER_PACKET * 4,
268 .period_bytes_max = 1024 * 1024,
270 .periods_max = UINT_MAX,
272 struct isight *isight = substream->private_data;
274 substream->runtime->hw = hardware;
276 return iso_packets_buffer_init(&isight->buffer, isight->unit,
278 sizeof(struct audio_payload),
282 static int isight_close(struct snd_pcm_substream *substream)
284 struct isight *isight = substream->private_data;
286 iso_packets_buffer_destroy(&isight->buffer, isight->unit);
291 static int isight_hw_params(struct snd_pcm_substream *substream,
292 struct snd_pcm_hw_params *hw_params)
294 struct isight *isight = substream->private_data;
297 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
298 params_buffer_bytes(hw_params));
302 ACCESS_ONCE(isight->pcm_active) = true;
307 static int reg_read(struct isight *isight, int offset, __be32 *value)
309 return snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
310 isight->audio_base + offset, value, 4, 0);
313 static int reg_write(struct isight *isight, int offset, __be32 value)
315 return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
316 isight->audio_base + offset, &value, 4, 0);
319 static void isight_stop_streaming(struct isight *isight)
323 if (!isight->context)
326 fw_iso_context_stop(isight->context);
327 fw_iso_context_destroy(isight->context);
328 isight->context = NULL;
329 fw_iso_resources_free(&isight->resources);
331 snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
332 isight->audio_base + REG_AUDIO_ENABLE,
333 &value, 4, FW_QUIET);
336 static int isight_hw_free(struct snd_pcm_substream *substream)
338 struct isight *isight = substream->private_data;
340 ACCESS_ONCE(isight->pcm_active) = false;
342 mutex_lock(&isight->mutex);
343 isight_stop_streaming(isight);
344 mutex_unlock(&isight->mutex);
346 return snd_pcm_lib_free_vmalloc_buffer(substream);
349 static int isight_start_streaming(struct isight *isight)
354 if (isight->context) {
355 if (isight->packet_index < 0)
356 isight_stop_streaming(isight);
361 err = reg_write(isight, REG_SAMPLE_RATE, cpu_to_be32(RATE_48000));
365 err = isight_connect(isight);
369 err = reg_write(isight, REG_AUDIO_ENABLE, cpu_to_be32(AUDIO_ENABLE));
373 isight->context = fw_iso_context_create(isight->device->card,
374 FW_ISO_CONTEXT_RECEIVE,
375 isight->resources.channel,
376 isight->device->max_speed,
377 4, isight_packet, isight);
378 if (IS_ERR(isight->context)) {
379 err = PTR_ERR(isight->context);
380 isight->context = NULL;
384 for (i = 0; i < QUEUE_LENGTH; ++i) {
385 err = fw_iso_context_queue(isight->context, &audio_packet,
386 &isight->buffer.iso_buffer,
387 isight->buffer.packets[i].offset);
392 isight->first_packet = true;
393 isight->packet_index = 0;
395 err = fw_iso_context_start(isight->context, -1, 0,
396 FW_ISO_CONTEXT_MATCH_ALL_TAGS/*?*/);
403 fw_iso_context_destroy(isight->context);
404 isight->context = NULL;
406 fw_iso_resources_free(&isight->resources);
407 reg_write(isight, REG_AUDIO_ENABLE, 0);
412 static int isight_prepare(struct snd_pcm_substream *substream)
414 struct isight *isight = substream->private_data;
417 isight->buffer_pointer = 0;
418 isight->period_counter = 0;
420 mutex_lock(&isight->mutex);
421 err = isight_start_streaming(isight);
422 mutex_unlock(&isight->mutex);
427 static int isight_trigger(struct snd_pcm_substream *substream, int cmd)
429 struct isight *isight = substream->private_data;
432 case SNDRV_PCM_TRIGGER_START:
433 ACCESS_ONCE(isight->pcm_running) = true;
435 case SNDRV_PCM_TRIGGER_STOP:
436 ACCESS_ONCE(isight->pcm_running) = false;
444 static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream)
446 struct isight *isight = substream->private_data;
448 return ACCESS_ONCE(isight->buffer_pointer);
451 static int isight_create_pcm(struct isight *isight)
453 static struct snd_pcm_ops ops = {
455 .close = isight_close,
456 .ioctl = snd_pcm_lib_ioctl,
457 .hw_params = isight_hw_params,
458 .hw_free = isight_hw_free,
459 .prepare = isight_prepare,
460 .trigger = isight_trigger,
461 .pointer = isight_pointer,
462 .page = snd_pcm_lib_get_vmalloc_page,
463 .mmap = snd_pcm_lib_mmap_vmalloc,
468 err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm);
471 pcm->private_data = isight;
472 strcpy(pcm->name, "iSight");
473 isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
474 isight->pcm->ops = &ops;
479 static int isight_gain_info(struct snd_kcontrol *ctl,
480 struct snd_ctl_elem_info *info)
482 struct isight *isight = ctl->private_data;
484 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
486 info->value.integer.min = isight->gain_min;
487 info->value.integer.max = isight->gain_max;
492 static int isight_gain_get(struct snd_kcontrol *ctl,
493 struct snd_ctl_elem_value *value)
495 struct isight *isight = ctl->private_data;
499 err = reg_read(isight, REG_GAIN, &gain);
503 value->value.integer.value[0] = (s32)be32_to_cpu(gain);
508 static int isight_gain_put(struct snd_kcontrol *ctl,
509 struct snd_ctl_elem_value *value)
511 struct isight *isight = ctl->private_data;
513 if (value->value.integer.value[0] < isight->gain_min ||
514 value->value.integer.value[0] > isight->gain_max)
517 return reg_write(isight, REG_GAIN,
518 cpu_to_be32(value->value.integer.value[0]));
521 static int isight_mute_get(struct snd_kcontrol *ctl,
522 struct snd_ctl_elem_value *value)
524 struct isight *isight = ctl->private_data;
528 err = reg_read(isight, REG_MUTE, &mute);
532 value->value.integer.value[0] = !mute;
537 static int isight_mute_put(struct snd_kcontrol *ctl,
538 struct snd_ctl_elem_value *value)
540 struct isight *isight = ctl->private_data;
542 return reg_write(isight, REG_MUTE,
543 (__force __be32)!value->value.integer.value[0]);
546 static int isight_create_mixer(struct isight *isight)
548 static const struct snd_kcontrol_new gain_control = {
549 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
550 .name = "Mic Capture Volume",
551 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
552 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
553 .info = isight_gain_info,
554 .get = isight_gain_get,
555 .put = isight_gain_put,
557 static const struct snd_kcontrol_new mute_control = {
558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
559 .name = "Mic Capture Switch",
560 .info = snd_ctl_boolean_mono_info,
561 .get = isight_mute_get,
562 .put = isight_mute_put,
565 struct snd_kcontrol *ctl;
568 err = reg_read(isight, REG_GAIN_RAW_START, &value);
571 isight->gain_min = be32_to_cpu(value);
573 err = reg_read(isight, REG_GAIN_RAW_END, &value);
576 isight->gain_max = be32_to_cpu(value);
578 isight->gain_tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
579 isight->gain_tlv[1] = 2 * sizeof(unsigned int);
581 err = reg_read(isight, REG_GAIN_DB_START, &value);
584 isight->gain_tlv[2] = (s32)be32_to_cpu(value) * 100;
586 err = reg_read(isight, REG_GAIN_DB_END, &value);
589 isight->gain_tlv[3] = (s32)be32_to_cpu(value) * 100;
591 ctl = snd_ctl_new1(&gain_control, isight);
593 ctl->tlv.p = isight->gain_tlv;
594 err = snd_ctl_add(isight->card, ctl);
598 err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight));
605 static void isight_card_free(struct snd_card *card)
607 struct isight *isight = card->private_data;
609 fw_iso_resources_destroy(&isight->resources);
610 fw_unit_put(isight->unit);
611 mutex_destroy(&isight->mutex);
614 static u64 get_unit_base(struct fw_unit *unit)
616 struct fw_csr_iterator i;
619 fw_csr_iterator_init(&i, unit->directory);
620 while (fw_csr_iterator_next(&i, &key, &value))
621 if (key == CSR_OFFSET)
622 return CSR_REGISTER_BASE + value * 4;
626 static int isight_probe(struct fw_unit *unit,
627 const struct ieee1394_device_id *id)
629 struct fw_device *fw_dev = fw_parent_device(unit);
630 struct snd_card *card;
631 struct isight *isight;
634 err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*isight), &card);
637 snd_card_set_dev(card, &unit->device);
639 isight = card->private_data;
641 mutex_init(&isight->mutex);
642 isight->unit = fw_unit_get(unit);
643 isight->device = fw_dev;
644 isight->audio_base = get_unit_base(unit);
645 if (!isight->audio_base) {
646 dev_err(&unit->device, "audio unit base not found\n");
650 fw_iso_resources_init(&isight->resources, unit);
652 card->private_free = isight_card_free;
654 strcpy(card->driver, "iSight");
655 strcpy(card->shortname, "Apple iSight");
656 snprintf(card->longname, sizeof(card->longname),
657 "Apple iSight (GUID %08x%08x) at %s, S%d",
658 fw_dev->config_rom[3], fw_dev->config_rom[4],
659 dev_name(&unit->device), 100 << fw_dev->max_speed);
660 strcpy(card->mixername, "iSight");
662 err = isight_create_pcm(isight);
666 err = isight_create_mixer(isight);
670 err = snd_card_register(card);
674 dev_set_drvdata(&unit->device, isight);
679 fw_unit_put(isight->unit);
680 mutex_destroy(&isight->mutex);
686 static void isight_bus_reset(struct fw_unit *unit)
688 struct isight *isight = dev_get_drvdata(&unit->device);
690 if (fw_iso_resources_update(&isight->resources) < 0) {
691 isight_pcm_abort(isight);
693 mutex_lock(&isight->mutex);
694 isight_stop_streaming(isight);
695 mutex_unlock(&isight->mutex);
699 static void isight_remove(struct fw_unit *unit)
701 struct isight *isight = dev_get_drvdata(&unit->device);
703 isight_pcm_abort(isight);
705 snd_card_disconnect(isight->card);
707 mutex_lock(&isight->mutex);
708 isight_stop_streaming(isight);
709 mutex_unlock(&isight->mutex);
711 snd_card_free_when_closed(isight->card);
714 static const struct ieee1394_device_id isight_id_table[] = {
716 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
717 IEEE1394_MATCH_VERSION,
718 .specifier_id = OUI_APPLE,
719 .version = SW_ISIGHT_AUDIO,
723 MODULE_DEVICE_TABLE(ieee1394, isight_id_table);
725 static struct fw_driver isight_driver = {
727 .owner = THIS_MODULE,
728 .name = KBUILD_MODNAME,
731 .probe = isight_probe,
732 .update = isight_bus_reset,
733 .remove = isight_remove,
734 .id_table = isight_id_table,
737 static int __init alsa_isight_init(void)
739 return driver_register(&isight_driver.driver);
742 static void __exit alsa_isight_exit(void)
744 driver_unregister(&isight_driver.driver);
747 module_init(alsa_isight_init);
748 module_exit(alsa_isight_exit);