ALSA: isight: fix isight_pcm_abort() crashes
[firewire-audio.git] / sound / firewire / isight.c
blob4043d9bdc7078ed1af1c7206a1ac2874663f6f4d
1 /*
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.
6 */
8 #include <linux/device.h>
9 #include <linux/firewire.h>
10 #include <linux/firewire-constants.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/mutex.h>
14 #include <linux/string.h>
15 #include <sound/control.h>
16 #include <sound/core.h>
17 #include <sound/initval.h>
18 #include <sound/pcm.h>
19 #include <sound/tlv.h>
20 #include "lib.h"
21 #include "iso-resources.h"
22 #include "packets-buffer.h"
24 #define OUI_APPLE 0x000a27
25 #define MODEL_APPLE_ISIGHT 0x000008
26 #define SW_ISIGHT_AUDIO 0x000010
28 #define REG_AUDIO_ENABLE 0x000
29 #define AUDIO_ENABLE 0x80000000
30 #define REG_DEF_AUDIO_GAIN 0x204
31 #define REG_GAIN_RAW_START 0x210
32 #define REG_GAIN_RAW_END 0x214
33 #define REG_GAIN_DB_START 0x218
34 #define REG_GAIN_DB_END 0x21c
35 #define REG_SAMPLE_RATE_INQUIRY 0x280
36 #define REG_ISO_TX_CONFIG 0x300
37 #define SPEED_SHIFT 16
38 #define REG_SAMPLE_RATE 0x400
39 #define RATE_48000 0x80000000
40 #define REG_GAIN 0x500
41 #define REG_MUTE 0x504
43 #define MAX_FRAMES_PER_PACKET 475
45 #define QUEUE_LENGTH 20
47 struct isight {
48 struct snd_card *card;
49 struct fw_unit *unit;
50 struct fw_device *device;
51 u64 audio_base;
52 struct fw_address_handler iris_handler;
53 struct snd_pcm_substream *pcm;
54 struct mutex mutex;
55 struct iso_packets_buffer buffer;
56 struct fw_iso_resources resources;
57 struct fw_iso_context *context;
58 bool pcm_active;
59 bool pcm_running;
60 bool first_packet;
61 int packet_index;
62 u32 total_samples;
63 unsigned int buffer_pointer;
64 unsigned int period_counter;
65 s32 gain_min, gain_max;
66 unsigned int gain_tlv[4];
69 struct audio_payload {
70 __be32 sample_count;
71 __be32 signature;
72 __be32 sample_total;
73 __be32 reserved;
74 __be16 samples[2 * MAX_FRAMES_PER_PACKET];
77 MODULE_DESCRIPTION("iSight audio driver");
78 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
79 MODULE_LICENSE("GPL v2");
81 static struct fw_iso_packet audio_packet = {
82 .payload_length = sizeof(struct audio_payload),
83 .interrupt = 1,
86 static void isight_update_pointers(struct isight *isight, unsigned int count)
88 struct snd_pcm_runtime *runtime = isight->pcm->runtime;
89 unsigned int ptr;
91 smp_wmb(); /* update buffer data before buffer pointer */
93 ptr = isight->buffer_pointer;
94 ptr += count;
95 if (ptr >= runtime->buffer_size)
96 ptr -= runtime->buffer_size;
97 ACCESS_ONCE(isight->buffer_pointer) = ptr;
99 isight->period_counter += count;
100 if (isight->period_counter >= runtime->period_size) {
101 isight->period_counter -= runtime->period_size;
102 snd_pcm_period_elapsed(isight->pcm);
106 static void isight_samples(struct isight *isight,
107 const __be16 *samples, unsigned int count)
109 struct snd_pcm_runtime *runtime;
110 unsigned int count1;
112 if (!ACCESS_ONCE(isight->pcm_running))
113 return;
115 runtime = isight->pcm->runtime;
116 if (isight->buffer_pointer + count <= runtime->buffer_size) {
117 memcpy(runtime->dma_area + isight->buffer_pointer * 4,
118 samples, count * 4);
119 } else {
120 count1 = runtime->buffer_size - isight->buffer_pointer;
121 memcpy(runtime->dma_area + isight->buffer_pointer * 4,
122 samples, count1 * 4);
123 samples += count1 * 2;
124 memcpy(runtime->dma_area, samples, (count - count1) * 4);
127 isight_update_pointers(isight, count);
130 static void isight_pcm_abort(struct isight *isight)
132 unsigned long flags;
134 if (ACCESS_ONCE(isight->pcm_active)) {
135 snd_pcm_stream_lock_irqsave(isight->pcm, flags);
136 if (snd_pcm_running(isight->pcm))
137 snd_pcm_stop(isight->pcm, SNDRV_PCM_STATE_XRUN);
138 snd_pcm_stream_unlock_irqrestore(isight->pcm, flags);
142 static void isight_dropped_samples(struct isight *isight, unsigned int total)
144 struct snd_pcm_runtime *runtime;
145 u32 dropped;
146 unsigned int count1;
148 if (!ACCESS_ONCE(isight->pcm_running))
149 return;
151 runtime = isight->pcm->runtime;
152 dropped = total - isight->total_samples;
153 if (dropped < runtime->buffer_size) {
154 if (isight->buffer_pointer + dropped <= runtime->buffer_size) {
155 memset(runtime->dma_area + isight->buffer_pointer * 4,
156 0, dropped * 4);
157 } else {
158 count1 = runtime->buffer_size - isight->buffer_pointer;
159 memset(runtime->dma_area + isight->buffer_pointer * 4,
160 0, count1 * 4);
161 memset(runtime->dma_area, 0, (dropped - count1) * 4);
163 isight_update_pointers(isight, dropped);
164 } else {
165 isight_pcm_abort(isight);
169 static void isight_packet(struct fw_iso_context *context, u32 cycle,
170 size_t header_length, void *header, void *data)
172 struct isight *isight = data;
173 const struct audio_payload *payload;
174 unsigned int index, length, count, total;
175 int err;
177 if (isight->packet_index < 0)
178 return;
179 index = isight->packet_index;
180 payload = isight->buffer.packets[index].buffer;
181 length = be32_to_cpup(header) >> 16;
183 if (likely(length >= 16 &&
184 payload->signature == cpu_to_be32(0x73676874/*"sght"*/))) {
185 count = be32_to_cpu(payload->sample_count);
186 if (likely(count <= (length - 16) / 4)) {
187 total = be32_to_cpu(payload->sample_total);
188 if (unlikely(total != isight->total_samples)) {
189 if (!isight->first_packet)
190 isight_dropped_samples(isight, total);
191 isight->first_packet = false;
192 isight->total_samples = total;
195 isight_samples(isight, payload->samples, count);
196 isight->total_samples += count;
200 if (++index >= QUEUE_LENGTH)
201 index = 0;
203 err = fw_iso_context_queue(isight->context, &audio_packet,
204 &isight->buffer.iso_buffer,
205 isight->buffer.packets[index].offset);
206 if (err < 0) {
207 dev_err(&isight->unit->device, "queueing error: %d\n", err);
208 isight_pcm_abort(isight);
209 isight->packet_index = -1;
210 return;
213 isight->packet_index = index;
216 static int isight_connect(struct isight *isight)
218 int ch, err, rcode, errors = 0;
219 __be32 value;
221 retry_after_bus_reset:
222 ch = fw_iso_resources_allocate(&isight->resources,
223 sizeof(struct audio_payload),
224 isight->device->max_speed);
225 if (ch < 0) {
226 err = ch;
227 goto error;
230 value = cpu_to_be32(ch | (isight->device->max_speed << SPEED_SHIFT));
231 for (;;) {
232 rcode = fw_run_transaction(
233 isight->device->card,
234 TCODE_WRITE_QUADLET_REQUEST,
235 isight->device->node_id,
236 isight->resources.generation,
237 isight->device->max_speed,
238 isight->audio_base + REG_ISO_TX_CONFIG,
239 &value, 4);
240 if (rcode == RCODE_COMPLETE) {
241 return 0;
242 } else if (rcode == RCODE_GENERATION) {
243 fw_iso_resources_free(&isight->resources);
244 goto retry_after_bus_reset;
245 } else if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
246 err = -EIO;
247 goto err_resources;
249 msleep(5);
252 err_resources:
253 fw_iso_resources_free(&isight->resources);
254 error:
255 return err;
258 static int isight_open(struct snd_pcm_substream *substream)
260 static const struct snd_pcm_hardware hardware = {
261 .info = SNDRV_PCM_INFO_MMAP |
262 SNDRV_PCM_INFO_MMAP_VALID |
263 SNDRV_PCM_INFO_BATCH |
264 SNDRV_PCM_INFO_INTERLEAVED |
265 SNDRV_PCM_INFO_BLOCK_TRANSFER,
266 .formats = SNDRV_PCM_FMTBIT_S16_BE,
267 .rates = SNDRV_PCM_RATE_48000,
268 .rate_min = 48000,
269 .rate_max = 48000,
270 .channels_min = 2,
271 .channels_max = 2,
272 .buffer_bytes_max = 4 * 1024 * 1024,
273 .period_bytes_min = MAX_FRAMES_PER_PACKET * 4,
274 .period_bytes_max = 1024 * 1024,
275 .periods_min = 2,
276 .periods_max = UINT_MAX,
278 struct isight *isight = substream->private_data;
280 substream->runtime->hw = hardware;
282 return iso_packets_buffer_init(&isight->buffer, isight->unit,
283 QUEUE_LENGTH,
284 sizeof(struct audio_payload),
285 DMA_FROM_DEVICE);
288 static int isight_close(struct snd_pcm_substream *substream)
290 struct isight *isight = substream->private_data;
292 iso_packets_buffer_destroy(&isight->buffer, isight->unit);
294 return 0;
297 static int isight_hw_params(struct snd_pcm_substream *substream,
298 struct snd_pcm_hw_params *hw_params)
300 struct isight *isight = substream->private_data;
301 int err;
303 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
304 params_buffer_bytes(hw_params));
305 if (err < 0)
306 return err;
308 ACCESS_ONCE(isight->pcm_active) = true;
310 return 0;
313 static void isight_stop_streaming(struct isight *isight)
315 __be32 value;
317 if (!isight->context)
318 return;
320 fw_iso_context_stop(isight->context);
321 fw_iso_context_destroy(isight->context);
322 isight->context = NULL;
324 value = 0;
325 snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
326 isight->audio_base + REG_AUDIO_ENABLE,
327 &value, 4);
329 fw_iso_resources_free(&isight->resources);
332 static int isight_hw_free(struct snd_pcm_substream *substream)
334 struct isight *isight = substream->private_data;
336 ACCESS_ONCE(isight->pcm_active) = false;
338 mutex_lock(&isight->mutex);
339 isight_stop_streaming(isight);
340 mutex_unlock(&isight->mutex);
342 return snd_pcm_lib_free_vmalloc_buffer(substream);
345 static int isight_start_streaming(struct isight *isight)
347 __be32 sample_rate;
348 unsigned int i;
349 int err;
351 if (isight->context) {
352 if (isight->packet_index < 0)
353 isight_stop_streaming(isight);
354 else
355 return 0;
358 sample_rate = cpu_to_be32(RATE_48000);
359 err = snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
360 isight->audio_base + REG_SAMPLE_RATE,
361 &sample_rate, 4);
362 if (err < 0)
363 return err;
365 err = isight_connect(isight);
366 if (err < 0)
367 goto error;
369 isight->context = fw_iso_context_create(isight->device->card,
370 FW_ISO_CONTEXT_RECEIVE,
371 isight->resources.channel,
372 isight->device->max_speed,
373 4, isight_packet, isight);
374 if (IS_ERR(isight->context)) {
375 err = PTR_ERR(isight->context);
376 isight->context = NULL;
377 goto err_resources;
380 for (i = 0; i < QUEUE_LENGTH; ++i) {
381 err = fw_iso_context_queue(isight->context, &audio_packet,
382 &isight->buffer.iso_buffer,
383 isight->buffer.packets[i].offset);
384 if (err < 0)
385 goto err_context;
388 isight->first_packet = true;
389 isight->packet_index = 0;
391 err = fw_iso_context_start(isight->context, -1, 0,
392 FW_ISO_CONTEXT_MATCH_ALL_TAGS/*?*/);
393 if (err < 0)
394 goto err_context;
396 return 0;
398 err_context:
399 fw_iso_context_destroy(isight->context);
400 isight->context = NULL;
401 err_resources:
402 fw_iso_resources_free(&isight->resources);
403 error:
404 return err;
407 static int isight_prepare(struct snd_pcm_substream *substream)
409 struct isight *isight = substream->private_data;
410 int err;
412 isight->buffer_pointer = 0;
413 isight->period_counter = 0;
415 mutex_lock(&isight->mutex);
416 err = isight_start_streaming(isight);
417 mutex_unlock(&isight->mutex);
419 return err;
422 static int isight_trigger(struct snd_pcm_substream *substream, int cmd)
424 struct isight *isight = substream->private_data;
426 switch (cmd) {
427 case SNDRV_PCM_TRIGGER_START:
428 ACCESS_ONCE(isight->pcm_running) = true;
429 break;
430 case SNDRV_PCM_TRIGGER_STOP:
431 ACCESS_ONCE(isight->pcm_running) = false;
432 break;
433 default:
434 return -EINVAL;
436 return 0;
439 static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream)
441 struct isight *isight = substream->private_data;
443 return ACCESS_ONCE(isight->buffer_pointer);
446 static int isight_create_pcm(struct isight *isight)
448 static struct snd_pcm_ops ops = {
449 .open = isight_open,
450 .close = isight_close,
451 .ioctl = snd_pcm_lib_ioctl,
452 .hw_params = isight_hw_params,
453 .hw_free = isight_hw_free,
454 .prepare = isight_prepare,
455 .trigger = isight_trigger,
456 .pointer = isight_pointer,
457 .page = snd_pcm_lib_get_vmalloc_page,
458 .mmap = snd_pcm_lib_mmap_vmalloc,
460 struct snd_pcm *pcm;
461 int err;
463 err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm);
464 if (err < 0)
465 return err;
466 pcm->private_data = isight;
467 strcpy(pcm->name, "iSight");
468 isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
469 isight->pcm->ops = &ops;
471 return 0;
474 static int isight_gain_info(struct snd_kcontrol *ctl,
475 struct snd_ctl_elem_info *info)
477 struct isight *isight = ctl->private_data;
479 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
480 info->count = 1;
481 info->value.integer.min = isight->gain_min;
482 info->value.integer.max = isight->gain_max;
484 return 0;
487 static int isight_gain_get(struct snd_kcontrol *ctl,
488 struct snd_ctl_elem_value *value)
490 struct isight *isight = ctl->private_data;
491 __be32 gain;
492 int err;
494 err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
495 isight->audio_base + REG_GAIN, &gain, 4);
496 if (err < 0)
497 return err;
499 value->value.integer.value[0] = (s32)be32_to_cpu(gain);
501 return 0;
504 static int isight_gain_put(struct snd_kcontrol *ctl,
505 struct snd_ctl_elem_value *value)
507 struct isight *isight = ctl->private_data;
508 __be32 gain;
510 if (value->value.integer.value[0] < isight->gain_min ||
511 value->value.integer.value[0] > isight->gain_max)
512 return -EINVAL;
514 gain = cpu_to_be32(value->value.integer.value[0]);
515 return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
516 isight->audio_base + REG_GAIN, &gain, 4);
519 static int isight_mute_get(struct snd_kcontrol *ctl,
520 struct snd_ctl_elem_value *value)
522 struct isight *isight = ctl->private_data;
523 __be32 mute;
524 int err;
526 err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
527 isight->audio_base + REG_MUTE, &mute, 4);
528 if (err < 0)
529 return err;
531 value->value.integer.value[0] = !mute;
533 return 0;
536 static int isight_mute_put(struct snd_kcontrol *ctl,
537 struct snd_ctl_elem_value *value)
539 struct isight *isight = ctl->private_data;
540 __be32 mute;
542 mute = (__force __be32)!value->value.integer.value[0];
543 return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
544 isight->audio_base + REG_MUTE, &mute, 4);
547 static int isight_create_mixer(struct isight *isight)
549 static const struct snd_kcontrol_new gain_control = {
550 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
551 .name = "Mic Capture Volume",
552 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
553 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
554 .info = isight_gain_info,
555 .get = isight_gain_get,
556 .put = isight_gain_put,
558 static const struct snd_kcontrol_new mute_control = {
559 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
560 .name = "Mic Capture Switch",
561 .info = snd_ctl_boolean_mono_info,
562 .get = isight_mute_get,
563 .put = isight_mute_put,
565 __be32 value;
566 struct snd_kcontrol *ctl;
567 int err;
569 err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
570 isight->audio_base + REG_GAIN_RAW_START,
571 &value, 4);
572 if (err < 0)
573 return err;
574 isight->gain_min = be32_to_cpu(value);
576 err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
577 isight->audio_base + REG_GAIN_RAW_END,
578 &value, 4);
579 if (err < 0)
580 return err;
581 isight->gain_max = be32_to_cpu(value);
583 isight->gain_tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
584 isight->gain_tlv[1] = 2 * sizeof(unsigned int);
585 err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
586 isight->audio_base + REG_GAIN_DB_START,
587 &value, 4);
588 if (err < 0)
589 return err;
590 isight->gain_tlv[2] = (s32)be32_to_cpu(value) * 100;
591 err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
592 isight->audio_base + REG_GAIN_DB_END,
593 &value, 4);
594 if (err < 0)
595 return err;
596 isight->gain_tlv[3] = (s32)be32_to_cpu(value) * 100;
598 ctl = snd_ctl_new1(&gain_control, isight);
599 if (ctl)
600 ctl->tlv.p = isight->gain_tlv;
601 err = snd_ctl_add(isight->card, ctl);
602 if (err < 0)
603 return err;
605 err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight));
606 if (err < 0)
607 return err;
609 return 0;
612 static void isight_card_free(struct snd_card *card)
614 struct isight *isight = card->private_data;
616 fw_iso_resources_destroy(&isight->resources);
617 fw_unit_put(isight->unit);
618 fw_device_put(isight->device);
619 mutex_destroy(&isight->mutex);
622 static u64 get_unit_base(struct fw_unit *unit)
624 struct fw_csr_iterator i;
625 int key, value;
627 fw_csr_iterator_init(&i, unit->directory);
628 while (fw_csr_iterator_next(&i, &key, &value))
629 if (key == CSR_OFFSET)
630 return CSR_REGISTER_BASE + value * 4;
631 return 0;
634 static int isight_probe(struct device *unit_dev)
636 struct fw_unit *unit = fw_unit(unit_dev);
637 struct fw_device *fw_dev = fw_parent_device(unit);
638 struct snd_card *card;
639 struct isight *isight;
640 int err;
642 err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*isight), &card);
643 if (err < 0)
644 return err;
645 snd_card_set_dev(card, unit_dev);
647 isight = card->private_data;
648 isight->card = card;
649 mutex_init(&isight->mutex);
650 isight->unit = fw_unit_get(unit);
651 isight->device = fw_device_get(fw_dev);
652 isight->audio_base = get_unit_base(unit);
653 if (!isight->audio_base) {
654 dev_err(&unit->device, "audio unit base not found\n");
655 err = -ENXIO;
656 goto err_unit;
658 fw_iso_resources_init(&isight->resources, unit);
660 card->private_free = isight_card_free;
662 strcpy(card->driver, "iSight");
663 strcpy(card->shortname, "Apple iSight");
664 snprintf(card->longname, sizeof(card->longname),
665 "Apple iSight (GUID %08x%08x) at %s, S%d",
666 fw_dev->config_rom[3], fw_dev->config_rom[4],
667 dev_name(&unit->device), 100 << fw_dev->max_speed);
668 strcpy(card->mixername, "iSight");
670 err = isight_create_pcm(isight);
671 if (err < 0)
672 goto error;
674 err = isight_create_mixer(isight);
675 if (err < 0)
676 goto error;
678 err = snd_card_register(card);
679 if (err < 0)
680 goto error;
682 dev_set_drvdata(unit_dev, isight);
684 return 0;
686 err_unit:
687 fw_unit_put(isight->unit);
688 fw_device_put(isight->device);
689 mutex_destroy(&isight->mutex);
690 error:
691 snd_card_free(card);
692 return err;
695 static int isight_remove(struct device *dev)
697 struct isight *isight = dev_get_drvdata(dev);
699 snd_card_disconnect(isight->card);
701 mutex_lock(&isight->mutex);
702 isight_pcm_abort(isight);
703 isight_stop_streaming(isight);
704 mutex_unlock(&isight->mutex);
706 snd_card_free_when_closed(isight->card);
708 return 0;
711 static void isight_bus_reset(struct fw_unit *unit)
713 struct isight *isight = dev_get_drvdata(&unit->device);
715 mutex_lock(&isight->mutex);
716 if (fw_iso_resources_update(&isight->resources) < 0) {
717 isight_pcm_abort(isight);
718 isight_stop_streaming(isight);
720 mutex_unlock(&isight->mutex);
723 static const struct ieee1394_device_id isight_id_table[] = {
725 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
726 IEEE1394_MATCH_VERSION,
727 .specifier_id = OUI_APPLE,
728 .version = SW_ISIGHT_AUDIO,
732 MODULE_DEVICE_TABLE(ieee1394, isight_id_table);
734 static struct fw_driver isight_driver = {
735 .driver = {
736 .owner = THIS_MODULE,
737 .name = KBUILD_MODNAME,
738 .bus = &fw_bus_type,
739 .probe = isight_probe,
740 .remove = isight_remove,
742 .update = isight_bus_reset,
743 .id_table = isight_id_table,
746 static int __init alsa_isight_init(void)
748 return driver_register(&isight_driver.driver);
751 static void __exit alsa_isight_exit(void)
753 driver_unregister(&isight_driver.driver);
756 module_init(alsa_isight_init);
757 module_exit(alsa_isight_exit);