1
2
3
4
5
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
49struct 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
70struct 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
78MODULE_DESCRIPTION("iSight audio driver");
79MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
80MODULE_LICENSE("GPL v2");
81
82static struct fw_iso_packet audio_packet = {
83 .payload_length = sizeof(struct audio_payload),
84 .interrupt = 1,
85 .header_length = 4,
86};
87
88static 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();
94
95 ptr = isight->buffer_pointer;
96 ptr += count;
97 if (ptr >= runtime->buffer_size)
98 ptr -= runtime->buffer_size;
99 ACCESS_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
108static 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 (!ACCESS_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
132static void isight_pcm_abort(struct isight *isight)
133{
134 if (ACCESS_ONCE(isight->pcm_active))
135 snd_pcm_stop_xrun(isight->pcm);
136}
137
138static 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 (!ACCESS_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
165static 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))) {
181 count = be32_to_cpu(payload->sample_count);
182 if (likely(count <= (length - 16) / 4)) {
183 total = be32_to_cpu(payload->sample_total);
184 if (unlikely(total != isight->total_samples)) {
185 if (!isight->first_packet)
186 isight_dropped_samples(isight, total);
187 isight->first_packet = false;
188 isight->total_samples = total;
189 }
190
191 isight_samples(isight, payload->samples, count);
192 isight->total_samples += count;
193 }
194 }
195
196 err = fw_iso_context_queue(isight->context, &audio_packet,
197 &isight->buffer.iso_buffer,
198 isight->buffer.packets[index].offset);
199 if (err < 0) {
200 dev_err(&isight->unit->device, "queueing error: %d\n", err);
201 isight_pcm_abort(isight);
202 isight->packet_index = -1;
203 return;
204 }
205 fw_iso_context_queue_flush(isight->context);
206
207 if (++index >= QUEUE_LENGTH)
208 index = 0;
209 isight->packet_index = index;
210}
211
212static int isight_connect(struct isight *isight)
213{
214 int ch, err, rcode, errors = 0;
215 __be32 value;
216
217retry_after_bus_reset:
218 ch = fw_iso_resources_allocate(&isight->resources,
219 sizeof(struct audio_payload),
220 isight->device->max_speed);
221 if (ch < 0) {
222 err = ch;
223 goto error;
224 }
225
226 value = cpu_to_be32(ch | (isight->device->max_speed << SPEED_SHIFT));
227 for (;;) {
228 rcode = fw_run_transaction(
229 isight->device->card,
230 TCODE_WRITE_QUADLET_REQUEST,
231 isight->device->node_id,
232 isight->resources.generation,
233 isight->device->max_speed,
234 isight->audio_base + REG_ISO_TX_CONFIG,
235 &value, 4);
236 if (rcode == RCODE_COMPLETE) {
237 return 0;
238 } else if (rcode == RCODE_GENERATION) {
239 fw_iso_resources_free(&isight->resources);
240 goto retry_after_bus_reset;
241 } else if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
242 err = -EIO;
243 goto err_resources;
244 }
245 msleep(5);
246 }
247
248err_resources:
249 fw_iso_resources_free(&isight->resources);
250error:
251 return err;
252}
253
254static int isight_open(struct snd_pcm_substream *substream)
255{
256 static const struct snd_pcm_hardware hardware = {
257 .info = SNDRV_PCM_INFO_MMAP |
258 SNDRV_PCM_INFO_MMAP_VALID |
259 SNDRV_PCM_INFO_BATCH |
260 SNDRV_PCM_INFO_INTERLEAVED |
261 SNDRV_PCM_INFO_BLOCK_TRANSFER,
262 .formats = SNDRV_PCM_FMTBIT_S16_BE,
263 .rates = SNDRV_PCM_RATE_48000,
264 .rate_min = 48000,
265 .rate_max = 48000,
266 .channels_min = 2,
267 .channels_max = 2,
268 .buffer_bytes_max = 4 * 1024 * 1024,
269 .period_bytes_min = MAX_FRAMES_PER_PACKET * 4,
270 .period_bytes_max = 1024 * 1024,
271 .periods_min = 2,
272 .periods_max = UINT_MAX,
273 };
274 struct isight *isight = substream->private_data;
275
276 substream->runtime->hw = hardware;
277
278 return iso_packets_buffer_init(&isight->buffer, isight->unit,
279 QUEUE_LENGTH,
280 sizeof(struct audio_payload),
281 DMA_FROM_DEVICE);
282}
283
284static int isight_close(struct snd_pcm_substream *substream)
285{
286 struct isight *isight = substream->private_data;
287
288 iso_packets_buffer_destroy(&isight->buffer, isight->unit);
289
290 return 0;
291}
292
293static int isight_hw_params(struct snd_pcm_substream *substream,
294 struct snd_pcm_hw_params *hw_params)
295{
296 struct isight *isight = substream->private_data;
297 int err;
298
299 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
300 params_buffer_bytes(hw_params));
301 if (err < 0)
302 return err;
303
304 ACCESS_ONCE(isight->pcm_active) = true;
305
306 return 0;
307}
308
309static int reg_read(struct isight *isight, int offset, __be32 *value)
310{
311 return snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
312 isight->audio_base + offset, value, 4);
313}
314
315static int reg_write(struct isight *isight, int offset, __be32 value)
316{
317 return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
318 isight->audio_base + offset, &value, 4);
319}
320
321static void isight_stop_streaming(struct isight *isight)
322{
323 if (!isight->context)
324 return;
325
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);
330 reg_write(isight, REG_AUDIO_ENABLE, 0);
331}
332
333static int isight_hw_free(struct snd_pcm_substream *substream)
334{
335 struct isight *isight = substream->private_data;
336
337 ACCESS_ONCE(isight->pcm_active) = false;
338
339 mutex_lock(&isight->mutex);
340 isight_stop_streaming(isight);
341 mutex_unlock(&isight->mutex);
342
343 return snd_pcm_lib_free_vmalloc_buffer(substream);
344}
345
346static int isight_start_streaming(struct isight *isight)
347{
348 unsigned int i;
349 int err;
350
351 if (isight->context) {
352 if (isight->packet_index < 0)
353 isight_stop_streaming(isight);
354 else
355 return 0;
356 }
357
358 err = reg_write(isight, REG_SAMPLE_RATE, cpu_to_be32(RATE_48000));
359 if (err < 0)
360 goto error;
361
362 err = isight_connect(isight);
363 if (err < 0)
364 goto error;
365
366 err = reg_write(isight, REG_AUDIO_ENABLE, cpu_to_be32(AUDIO_ENABLE));
367 if (err < 0)
368 goto err_resources;
369
370 isight->context = fw_iso_context_create(isight->device->card,
371 FW_ISO_CONTEXT_RECEIVE,
372 isight->resources.channel,
373 isight->device->max_speed,
374 4, isight_packet, isight);
375 if (IS_ERR(isight->context)) {
376 err = PTR_ERR(isight->context);
377 isight->context = NULL;
378 goto err_resources;
379 }
380
381 for (i = 0; i < QUEUE_LENGTH; ++i) {
382 err = fw_iso_context_queue(isight->context, &audio_packet,
383 &isight->buffer.iso_buffer,
384 isight->buffer.packets[i].offset);
385 if (err < 0)
386 goto err_context;
387 }
388
389 isight->first_packet = true;
390 isight->packet_index = 0;
391
392 err = fw_iso_context_start(isight->context, -1, 0,
393 FW_ISO_CONTEXT_MATCH_ALL_TAGS);
394 if (err < 0)
395 goto err_context;
396
397 return 0;
398
399err_context:
400 fw_iso_context_destroy(isight->context);
401 isight->context = NULL;
402err_resources:
403 fw_iso_resources_free(&isight->resources);
404 reg_write(isight, REG_AUDIO_ENABLE, 0);
405error:
406 return err;
407}
408
409static int isight_prepare(struct snd_pcm_substream *substream)
410{
411 struct isight *isight = substream->private_data;
412 int err;
413
414 isight->buffer_pointer = 0;
415 isight->period_counter = 0;
416
417 mutex_lock(&isight->mutex);
418 err = isight_start_streaming(isight);
419 mutex_unlock(&isight->mutex);
420
421 return err;
422}
423
424static int isight_trigger(struct snd_pcm_substream *substream, int cmd)
425{
426 struct isight *isight = substream->private_data;
427
428 switch (cmd) {
429 case SNDRV_PCM_TRIGGER_START:
430 ACCESS_ONCE(isight->pcm_running) = true;
431 break;
432 case SNDRV_PCM_TRIGGER_STOP:
433 ACCESS_ONCE(isight->pcm_running) = false;
434 break;
435 default:
436 return -EINVAL;
437 }
438 return 0;
439}
440
441static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream)
442{
443 struct isight *isight = substream->private_data;
444
445 return ACCESS_ONCE(isight->buffer_pointer);
446}
447
448static int isight_create_pcm(struct isight *isight)
449{
450 static struct snd_pcm_ops ops = {
451 .open = isight_open,
452 .close = isight_close,
453 .ioctl = snd_pcm_lib_ioctl,
454 .hw_params = isight_hw_params,
455 .hw_free = isight_hw_free,
456 .prepare = isight_prepare,
457 .trigger = isight_trigger,
458 .pointer = isight_pointer,
459 .page = snd_pcm_lib_get_vmalloc_page,
460 .mmap = snd_pcm_lib_mmap_vmalloc,
461 };
462 struct snd_pcm *pcm;
463 int err;
464
465 err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm);
466 if (err < 0)
467 return err;
468 pcm->private_data = isight;
469 strcpy(pcm->name, "iSight");
470 isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
471 isight->pcm->ops = &ops;
472
473 return 0;
474}
475
476static int isight_gain_info(struct snd_kcontrol *ctl,
477 struct snd_ctl_elem_info *info)
478{
479 struct isight *isight = ctl->private_data;
480
481 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
482 info->count = 1;
483 info->value.integer.min = isight->gain_min;
484 info->value.integer.max = isight->gain_max;
485
486 return 0;
487}
488
489static int isight_gain_get(struct snd_kcontrol *ctl,
490 struct snd_ctl_elem_value *value)
491{
492 struct isight *isight = ctl->private_data;
493 __be32 gain;
494 int err;
495
496 err = reg_read(isight, REG_GAIN, &gain);
497 if (err < 0)
498 return err;
499
500 value->value.integer.value[0] = (s32)be32_to_cpu(gain);
501
502 return 0;
503}
504
505static int isight_gain_put(struct snd_kcontrol *ctl,
506 struct snd_ctl_elem_value *value)
507{
508 struct isight *isight = ctl->private_data;
509
510 if (value->value.integer.value[0] < isight->gain_min ||
511 value->value.integer.value[0] > isight->gain_max)
512 return -EINVAL;
513
514 return reg_write(isight, REG_GAIN,
515 cpu_to_be32(value->value.integer.value[0]));
516}
517
518static int isight_mute_get(struct snd_kcontrol *ctl,
519 struct snd_ctl_elem_value *value)
520{
521 struct isight *isight = ctl->private_data;
522 __be32 mute;
523 int err;
524
525 err = reg_read(isight, REG_MUTE, &mute);
526 if (err < 0)
527 return err;
528
529 value->value.integer.value[0] = !mute;
530
531 return 0;
532}
533
534static int isight_mute_put(struct snd_kcontrol *ctl,
535 struct snd_ctl_elem_value *value)
536{
537 struct isight *isight = ctl->private_data;
538
539 return reg_write(isight, REG_MUTE,
540 (__force __be32)!value->value.integer.value[0]);
541}
542
543static int isight_create_mixer(struct isight *isight)
544{
545 static const struct snd_kcontrol_new gain_control = {
546 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
547 .name = "Mic Capture Volume",
548 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
549 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
550 .info = isight_gain_info,
551 .get = isight_gain_get,
552 .put = isight_gain_put,
553 };
554 static const struct snd_kcontrol_new mute_control = {
555 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
556 .name = "Mic Capture Switch",
557 .info = snd_ctl_boolean_mono_info,
558 .get = isight_mute_get,
559 .put = isight_mute_put,
560 };
561 __be32 value;
562 struct snd_kcontrol *ctl;
563 int err;
564
565 err = reg_read(isight, REG_GAIN_RAW_START, &value);
566 if (err < 0)
567 return err;
568 isight->gain_min = be32_to_cpu(value);
569
570 err = reg_read(isight, REG_GAIN_RAW_END, &value);
571 if (err < 0)
572 return err;
573 isight->gain_max = be32_to_cpu(value);
574
575 isight->gain_tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
576 isight->gain_tlv[1] = 2 * sizeof(unsigned int);
577
578 err = reg_read(isight, REG_GAIN_DB_START, &value);
579 if (err < 0)
580 return err;
581 isight->gain_tlv[2] = (s32)be32_to_cpu(value) * 100;
582
583 err = reg_read(isight, REG_GAIN_DB_END, &value);
584 if (err < 0)
585 return err;
586 isight->gain_tlv[3] = (s32)be32_to_cpu(value) * 100;
587
588 ctl = snd_ctl_new1(&gain_control, isight);
589 if (ctl)
590 ctl->tlv.p = isight->gain_tlv;
591 err = snd_ctl_add(isight->card, ctl);
592 if (err < 0)
593 return err;
594
595 err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight));
596 if (err < 0)
597 return err;
598
599 return 0;
600}
601
602static void isight_card_free(struct snd_card *card)
603{
604 struct isight *isight = card->private_data;
605
606 fw_iso_resources_destroy(&isight->resources);
607 fw_unit_put(isight->unit);
608 mutex_destroy(&isight->mutex);
609}
610
611static u64 get_unit_base(struct fw_unit *unit)
612{
613 struct fw_csr_iterator i;
614 int key, value;
615
616 fw_csr_iterator_init(&i, unit->directory);
617 while (fw_csr_iterator_next(&i, &key, &value))
618 if (key == CSR_OFFSET)
619 return CSR_REGISTER_BASE + value * 4;
620 return 0;
621}
622
623static int isight_probe(struct device *unit_dev)
624{
625 struct fw_unit *unit = fw_unit(unit_dev);
626 struct fw_device *fw_dev = fw_parent_device(unit);
627 struct snd_card *card;
628 struct isight *isight;
629 int err;
630
631 err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
632 sizeof(*isight), &card);
633 if (err < 0)
634 return err;
635
636 isight = card->private_data;
637 isight->card = card;
638 mutex_init(&isight->mutex);
639 isight->unit = fw_unit_get(unit);
640 isight->device = fw_dev;
641 isight->audio_base = get_unit_base(unit);
642 if (!isight->audio_base) {
643 dev_err(&unit->device, "audio unit base not found\n");
644 err = -ENXIO;
645 goto err_unit;
646 }
647 fw_iso_resources_init(&isight->resources, unit);
648
649 card->private_free = isight_card_free;
650
651 strcpy(card->driver, "iSight");
652 strcpy(card->shortname, "Apple iSight");
653 snprintf(card->longname, sizeof(card->longname),
654 "Apple iSight (GUID %08x%08x) at %s, S%d",
655 fw_dev->config_rom[3], fw_dev->config_rom[4],
656 dev_name(&unit->device), 100 << fw_dev->max_speed);
657 strcpy(card->mixername, "iSight");
658
659 err = isight_create_pcm(isight);
660 if (err < 0)
661 goto error;
662
663 err = isight_create_mixer(isight);
664 if (err < 0)
665 goto error;
666
667 err = snd_card_register(card);
668 if (err < 0)
669 goto error;
670
671 dev_set_drvdata(unit_dev, isight);
672
673 return 0;
674
675err_unit:
676 fw_unit_put(isight->unit);
677 mutex_destroy(&isight->mutex);
678error:
679 snd_card_free(card);
680 return err;
681}
682
683static int isight_remove(struct device *dev)
684{
685 struct isight *isight = dev_get_drvdata(dev);
686
687 isight_pcm_abort(isight);
688
689 snd_card_disconnect(isight->card);
690
691 mutex_lock(&isight->mutex);
692 isight_stop_streaming(isight);
693 mutex_unlock(&isight->mutex);
694
695 snd_card_free_when_closed(isight->card);
696
697 return 0;
698}
699
700static void isight_bus_reset(struct fw_unit *unit)
701{
702 struct isight *isight = dev_get_drvdata(&unit->device);
703
704 if (fw_iso_resources_update(&isight->resources) < 0) {
705 isight_pcm_abort(isight);
706
707 mutex_lock(&isight->mutex);
708 isight_stop_streaming(isight);
709 mutex_unlock(&isight->mutex);
710 }
711}
712
713static const struct ieee1394_device_id isight_id_table[] = {
714 {
715 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
716 IEEE1394_MATCH_VERSION,
717 .specifier_id = OUI_APPLE,
718 .version = SW_ISIGHT_AUDIO,
719 },
720 { }
721};
722MODULE_DEVICE_TABLE(ieee1394, isight_id_table);
723
724static struct fw_driver isight_driver = {
725 .driver = {
726 .owner = THIS_MODULE,
727 .name = KBUILD_MODNAME,
728 .bus = &fw_bus_type,
729 .probe = isight_probe,
730 .remove = isight_remove,
731 },
732 .update = isight_bus_reset,
733 .id_table = isight_id_table,
734};
735
736static int __init alsa_isight_init(void)
737{
738 return driver_register(&isight_driver.driver);
739}
740
741static void __exit alsa_isight_exit(void)
742{
743 driver_unregister(&isight_driver.driver);
744}
745
746module_init(alsa_isight_init);
747module_exit(alsa_isight_exit);
748