2 * OXFW970-based speakers driver
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
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/slab.h>
15 #include <sound/control.h>
16 #include <sound/core.h>
17 #include <sound/initval.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
25 #define OXFORD_FIRMWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x50000)
26 /* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */
28 #define OXFORD_HARDWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x90020)
29 #define OXFORD_HARDWARE_ID_OXFW970 0x39443841
30 #define OXFORD_HARDWARE_ID_OXFW971 0x39373100
32 #define VENDOR_GRIFFIN 0x001292
33 #define VENDOR_LACIE 0x00d04b
35 #define SPECIFIER_1394TA 0x00a02d
36 #define VERSION_AVC 0x010001
39 const char *driver_name;
40 const char *short_name;
41 const char *long_name;
42 int (*pcm_constraints)(struct snd_pcm_runtime *runtime);
43 unsigned int mixer_channels;
49 struct snd_card *card;
51 const struct device_info *device_info;
52 struct snd_pcm_substream *pcm;
54 struct cmp_connection connection;
55 struct amdtp_out_stream stream;
63 MODULE_DESCRIPTION("FireWire speakers driver");
64 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
65 MODULE_LICENSE("GPL v2");
67 static int firewave_rate_constraint(struct snd_pcm_hw_params *params,
68 struct snd_pcm_hw_rule *rule)
70 static unsigned int stereo_rates[] = { 48000, 96000 };
71 struct snd_interval *channels =
72 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
73 struct snd_interval *rate =
74 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
76 /* two channels work only at 48/96 kHz */
77 if (snd_interval_max(channels) < 6)
78 return snd_interval_list(rate, 2, stereo_rates, 0);
82 static int firewave_channels_constraint(struct snd_pcm_hw_params *params,
83 struct snd_pcm_hw_rule *rule)
85 static const struct snd_interval all_channels = { .min = 6, .max = 6 };
86 struct snd_interval *rate =
87 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
88 struct snd_interval *channels =
89 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
91 /* 32/44.1 kHz work only with all six channels */
92 if (snd_interval_max(rate) < 48000)
93 return snd_interval_refine(channels, &all_channels);
97 static int firewave_constraints(struct snd_pcm_runtime *runtime)
99 static unsigned int channels_list[] = { 2, 6 };
100 static struct snd_pcm_hw_constraint_list channels_list_constraint = {
102 .list = channels_list,
106 runtime->hw.rates = SNDRV_PCM_RATE_32000 |
107 SNDRV_PCM_RATE_44100 |
108 SNDRV_PCM_RATE_48000 |
109 SNDRV_PCM_RATE_96000;
110 runtime->hw.channels_max = 6;
112 err = snd_pcm_hw_constraint_list(runtime, 0,
113 SNDRV_PCM_HW_PARAM_CHANNELS,
114 &channels_list_constraint);
117 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
118 firewave_rate_constraint, NULL,
119 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
122 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
123 firewave_channels_constraint, NULL,
124 SNDRV_PCM_HW_PARAM_RATE, -1);
131 static int lacie_speakers_constraints(struct snd_pcm_runtime *runtime)
133 runtime->hw.rates = SNDRV_PCM_RATE_32000 |
134 SNDRV_PCM_RATE_44100 |
135 SNDRV_PCM_RATE_48000 |
136 SNDRV_PCM_RATE_88200 |
137 SNDRV_PCM_RATE_96000;
142 static int fwspk_open(struct snd_pcm_substream *substream)
144 static const struct snd_pcm_hardware hardware = {
145 .info = SNDRV_PCM_INFO_MMAP |
146 SNDRV_PCM_INFO_MMAP_VALID |
147 SNDRV_PCM_INFO_BATCH |
148 SNDRV_PCM_INFO_INTERLEAVED |
149 SNDRV_PCM_INFO_BLOCK_TRANSFER,
150 .formats = AMDTP_OUT_PCM_FORMAT_BITS,
153 .buffer_bytes_max = 4 * 1024 * 1024,
154 .period_bytes_min = 1,
155 .period_bytes_max = UINT_MAX,
157 .periods_max = UINT_MAX,
159 struct fwspk *fwspk = substream->private_data;
160 struct snd_pcm_runtime *runtime = substream->runtime;
163 runtime->hw = hardware;
165 err = fwspk->device_info->pcm_constraints(runtime);
168 err = snd_pcm_limit_hw_rates(runtime);
172 err = snd_pcm_hw_constraint_minmax(runtime,
173 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
178 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
185 static int fwspk_close(struct snd_pcm_substream *substream)
190 static void fwspk_stop_stream(struct fwspk *fwspk)
192 if (fwspk->stream_running) {
193 amdtp_out_stream_stop(&fwspk->stream);
194 cmp_connection_break(&fwspk->connection);
195 fwspk->stream_running = false;
199 static int fwspk_set_rate(struct fwspk *fwspk, unsigned int sfc)
204 buf = kmalloc(8, GFP_KERNEL);
208 buf[0] = 0x00; /* AV/C, CONTROL */
209 buf[1] = 0xff; /* unit */
210 buf[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
211 buf[3] = 0x00; /* plug 0 */
212 buf[4] = 0x90; /* format: audio */
213 buf[5] = 0x00 | sfc; /* AM824, frequency */
214 buf[6] = 0xff; /* SYT (not used) */
217 err = fcp_avc_transaction(fwspk->unit, buf, 8, buf, 8,
218 BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
221 if (err < 6 || buf[0] != 0x09 /* ACCEPTED */) {
222 dev_err(&fwspk->unit->device, "failed to set sample rate\n");
235 static int fwspk_hw_params(struct snd_pcm_substream *substream,
236 struct snd_pcm_hw_params *hw_params)
238 struct fwspk *fwspk = substream->private_data;
241 mutex_lock(&fwspk->mutex);
242 fwspk_stop_stream(fwspk);
243 mutex_unlock(&fwspk->mutex);
245 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
246 params_buffer_bytes(hw_params));
250 amdtp_out_stream_set_rate(&fwspk->stream, params_rate(hw_params));
251 amdtp_out_stream_set_pcm(&fwspk->stream, params_channels(hw_params));
253 amdtp_out_stream_set_pcm_format(&fwspk->stream,
254 params_format(hw_params));
256 err = fwspk_set_rate(fwspk, fwspk->stream.sfc);
263 snd_pcm_lib_free_vmalloc_buffer(substream);
268 static int fwspk_hw_free(struct snd_pcm_substream *substream)
270 struct fwspk *fwspk = substream->private_data;
272 mutex_lock(&fwspk->mutex);
273 fwspk_stop_stream(fwspk);
274 mutex_unlock(&fwspk->mutex);
276 return snd_pcm_lib_free_vmalloc_buffer(substream);
279 static int fwspk_prepare(struct snd_pcm_substream *substream)
281 struct fwspk *fwspk = substream->private_data;
284 mutex_lock(&fwspk->mutex);
286 if (!fwspk->stream_running) {
287 err = cmp_connection_establish(&fwspk->connection,
288 amdtp_out_stream_get_max_payload(&fwspk->stream));
292 err = amdtp_out_stream_start(&fwspk->stream,
293 fwspk->connection.resources.channel,
294 fwspk->connection.speed);
298 fwspk->stream_running = true;
301 mutex_unlock(&fwspk->mutex);
303 amdtp_out_stream_pcm_prepare(&fwspk->stream);
308 cmp_connection_break(&fwspk->connection);
310 mutex_unlock(&fwspk->mutex);
315 static int fwspk_trigger(struct snd_pcm_substream *substream, int cmd)
317 struct fwspk *fwspk = substream->private_data;
318 struct snd_pcm_substream *pcm;
321 case SNDRV_PCM_TRIGGER_START:
324 case SNDRV_PCM_TRIGGER_STOP:
330 amdtp_out_stream_pcm_trigger(&fwspk->stream, pcm);
334 static snd_pcm_uframes_t fwspk_pointer(struct snd_pcm_substream *substream)
336 struct fwspk *fwspk = substream->private_data;
338 return amdtp_out_stream_pcm_pointer(&fwspk->stream);
341 static int fwspk_create_pcm(struct fwspk *fwspk)
343 static struct snd_pcm_ops ops = {
345 .close = fwspk_close,
346 .ioctl = snd_pcm_lib_ioctl,
347 .hw_params = fwspk_hw_params,
348 .hw_free = fwspk_hw_free,
349 .prepare = fwspk_prepare,
350 .trigger = fwspk_trigger,
351 .pointer = fwspk_pointer,
352 .page = snd_pcm_lib_get_vmalloc_page,
353 .mmap = snd_pcm_lib_mmap_vmalloc,
358 err = snd_pcm_new(fwspk->card, "OXFW970", 0, 1, 0, &pcm);
361 pcm->private_data = fwspk;
362 strcpy(pcm->name, fwspk->device_info->short_name);
363 fwspk->pcm = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
364 fwspk->pcm->ops = &ops;
368 enum control_action { CTL_READ, CTL_WRITE };
369 enum control_attribute {
375 static int fwspk_mute_command(struct fwspk *fwspk, bool *value,
376 enum control_action action)
382 buf = kmalloc(11, GFP_KERNEL);
386 if (action == CTL_READ) {
387 buf[0] = 0x01; /* AV/C, STATUS */
388 response_ok = 0x0c; /* STABLE */
390 buf[0] = 0x00; /* AV/C, CONTROL */
391 response_ok = 0x09; /* ACCEPTED */
393 buf[1] = 0x08; /* audio unit 0 */
394 buf[2] = 0xb8; /* FUNCTION BLOCK */
395 buf[3] = 0x81; /* function block type: feature */
396 buf[4] = fwspk->device_info->mute_fb_id; /* function block ID */
397 buf[5] = 0x10; /* control attribute: current */
398 buf[6] = 0x02; /* selector length */
399 buf[7] = 0x00; /* audio channel number */
400 buf[8] = 0x01; /* control selector: mute */
401 buf[9] = 0x01; /* control data length */
402 if (action == CTL_READ)
405 buf[10] = *value ? 0x70 : 0x60;
407 err = fcp_avc_transaction(fwspk->unit, buf, 11, buf, 11, 0x3fe);
411 dev_err(&fwspk->unit->device, "short FCP response\n");
415 if (buf[0] != response_ok) {
416 dev_err(&fwspk->unit->device, "mute command failed\n");
420 if (action == CTL_READ)
421 *value = buf[10] == 0x70;
431 static int fwspk_volume_command(struct fwspk *fwspk, s16 *value,
432 unsigned int channel,
433 enum control_attribute attribute,
434 enum control_action action)
440 buf = kmalloc(12, GFP_KERNEL);
444 if (action == CTL_READ) {
445 buf[0] = 0x01; /* AV/C, STATUS */
446 response_ok = 0x0c; /* STABLE */
448 buf[0] = 0x00; /* AV/C, CONTROL */
449 response_ok = 0x09; /* ACCEPTED */
451 buf[1] = 0x08; /* audio unit 0 */
452 buf[2] = 0xb8; /* FUNCTION BLOCK */
453 buf[3] = 0x81; /* function block type: feature */
454 buf[4] = fwspk->device_info->volume_fb_id; /* function block ID */
455 buf[5] = attribute; /* control attribute */
456 buf[6] = 0x02; /* selector length */
457 buf[7] = channel; /* audio channel number */
458 buf[8] = 0x02; /* control selector: volume */
459 buf[9] = 0x02; /* control data length */
460 if (action == CTL_READ) {
464 buf[10] = *value >> 8;
468 err = fcp_avc_transaction(fwspk->unit, buf, 12, buf, 12, 0x3fe);
472 dev_err(&fwspk->unit->device, "short FCP response\n");
476 if (buf[0] != response_ok) {
477 dev_err(&fwspk->unit->device, "volume command failed\n");
481 if (action == CTL_READ)
482 *value = (buf[10] << 8) | buf[11];
492 static int fwspk_mute_get(struct snd_kcontrol *control,
493 struct snd_ctl_elem_value *value)
495 struct fwspk *fwspk = control->private_data;
497 value->value.integer.value[0] = !fwspk->mute;
502 static int fwspk_mute_put(struct snd_kcontrol *control,
503 struct snd_ctl_elem_value *value)
505 struct fwspk *fwspk = control->private_data;
509 mute = !value->value.integer.value[0];
511 if (mute == fwspk->mute)
514 err = fwspk_mute_command(fwspk, &mute, CTL_WRITE);
522 static int fwspk_volume_info(struct snd_kcontrol *control,
523 struct snd_ctl_elem_info *info)
525 struct fwspk *fwspk = control->private_data;
527 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
528 info->count = fwspk->device_info->mixer_channels;
529 info->value.integer.min = fwspk->volume_min;
530 info->value.integer.max = fwspk->volume_max;
535 static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 };
537 static int fwspk_volume_get(struct snd_kcontrol *control,
538 struct snd_ctl_elem_value *value)
540 struct fwspk *fwspk = control->private_data;
543 for (i = 0; i < fwspk->device_info->mixer_channels; ++i)
544 value->value.integer.value[channel_map[i]] = fwspk->volume[i];
549 static int fwspk_volume_put(struct snd_kcontrol *control,
550 struct snd_ctl_elem_value *value)
552 struct fwspk *fwspk = control->private_data;
553 unsigned int i, changed_channels;
554 bool equal_values = true;
558 for (i = 0; i < fwspk->device_info->mixer_channels; ++i) {
559 if (value->value.integer.value[i] < fwspk->volume_min ||
560 value->value.integer.value[i] > fwspk->volume_max)
562 if (value->value.integer.value[i] !=
563 value->value.integer.value[0])
564 equal_values = false;
567 changed_channels = 0;
568 for (i = 0; i < fwspk->device_info->mixer_channels; ++i)
569 if (value->value.integer.value[channel_map[i]] !=
571 changed_channels |= 1 << (i + 1);
573 if (equal_values && changed_channels != 0)
574 changed_channels = 1 << 0;
576 for (i = 0; i <= fwspk->device_info->mixer_channels; ++i) {
577 volume = value->value.integer.value[channel_map[i ? i - 1 : 0]];
578 if (changed_channels & (1 << i)) {
579 err = fwspk_volume_command(fwspk, &volume, i,
580 CTL_CURRENT, CTL_WRITE);
585 fwspk->volume[i - 1] = volume;
588 return changed_channels != 0;
591 static int fwspk_create_mixer(struct fwspk *fwspk)
593 static const struct snd_kcontrol_new controls[] = {
595 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
596 .name = "PCM Playback Switch",
597 .info = snd_ctl_boolean_mono_info,
598 .get = fwspk_mute_get,
599 .put = fwspk_mute_put,
602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
603 .name = "PCM Playback Volume",
604 .info = fwspk_volume_info,
605 .get = fwspk_volume_get,
606 .put = fwspk_volume_put,
609 unsigned int i, first_ch;
612 err = fwspk_volume_command(fwspk, &fwspk->volume_min,
613 0, CTL_MIN, CTL_READ);
616 err = fwspk_volume_command(fwspk, &fwspk->volume_max,
617 0, CTL_MAX, CTL_READ);
621 err = fwspk_mute_command(fwspk, &fwspk->mute, CTL_READ);
625 first_ch = fwspk->device_info->mixer_channels == 1 ? 0 : 1;
626 for (i = 0; i < fwspk->device_info->mixer_channels; ++i) {
627 err = fwspk_volume_command(fwspk, &fwspk->volume[i],
628 first_ch + i, CTL_CURRENT, CTL_READ);
633 for (i = 0; i < ARRAY_SIZE(controls); ++i) {
634 err = snd_ctl_add(fwspk->card,
635 snd_ctl_new1(&controls[i], fwspk));
643 static u32 fwspk_read_firmware_version(struct fw_unit *unit)
648 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
649 OXFORD_FIRMWARE_ID_ADDRESS, &data, 4);
650 return err >= 0 ? be32_to_cpu(data) : 0;
653 static void fwspk_card_free(struct snd_card *card)
655 struct fwspk *fwspk = card->private_data;
656 struct fw_device *dev = fw_parent_device(fwspk->unit);
658 amdtp_out_stream_destroy(&fwspk->stream);
659 cmp_connection_destroy(&fwspk->connection);
660 fw_unit_put(fwspk->unit);
662 mutex_destroy(&fwspk->mutex);
665 static const struct device_info *__devinit fwspk_detect(struct fw_device *dev)
667 static const struct device_info griffin_firewave = {
668 .driver_name = "FireWave",
669 .short_name = "FireWave",
670 .long_name = "Griffin FireWave Surround",
671 .pcm_constraints = firewave_constraints,
674 .volume_fb_id = 0x02,
676 static const struct device_info lacie_speakers = {
677 .driver_name = "FWSpeakers",
678 .short_name = "FireWire Speakers",
679 .long_name = "LaCie FireWire Speakers",
680 .pcm_constraints = lacie_speakers_constraints,
683 .volume_fb_id = 0x01,
685 struct fw_csr_iterator i;
688 fw_csr_iterator_init(&i, dev->config_rom);
689 while (fw_csr_iterator_next(&i, &key, &value))
690 if (key == CSR_VENDOR)
693 return &griffin_firewave;
695 return &lacie_speakers;
701 static int __devinit fwspk_probe(struct device *unit_dev)
703 struct fw_unit *unit = fw_unit(unit_dev);
704 struct fw_device *fw_dev = fw_parent_device(unit);
705 struct snd_card *card;
710 err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*fwspk), &card);
713 snd_card_set_dev(card, unit_dev);
715 fwspk = card->private_data;
717 mutex_init(&fwspk->mutex);
718 fw_device_get(fw_dev);
719 fwspk->unit = fw_unit_get(unit);
720 fwspk->device_info = fwspk_detect(fw_dev);
721 if (!fwspk->device_info) {
726 err = cmp_connection_init(&fwspk->connection, unit, 0);
730 err = amdtp_out_stream_init(&fwspk->stream, unit, CIP_NONBLOCKING);
734 card->private_free = fwspk_card_free;
736 strcpy(card->driver, fwspk->device_info->driver_name);
737 strcpy(card->shortname, fwspk->device_info->short_name);
738 firmware = fwspk_read_firmware_version(unit);
739 snprintf(card->longname, sizeof(card->longname),
740 "%s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
741 fwspk->device_info->long_name,
742 firmware >> 20, firmware & 0xffff,
743 fw_dev->config_rom[3], fw_dev->config_rom[4],
744 dev_name(&unit->device), 100 << fw_dev->max_speed);
745 strcpy(card->mixername, "OXFW970");
747 err = fwspk_create_pcm(fwspk);
751 err = fwspk_create_mixer(fwspk);
755 err = snd_card_register(card);
759 dev_set_drvdata(unit_dev, fwspk);
764 cmp_connection_destroy(&fwspk->connection);
766 fw_unit_put(fwspk->unit);
767 fw_device_put(fw_dev);
768 mutex_destroy(&fwspk->mutex);
774 static int __devexit fwspk_remove(struct device *dev)
776 struct fwspk *fwspk = dev_get_drvdata(dev);
778 snd_card_disconnect(fwspk->card);
780 mutex_lock(&fwspk->mutex);
781 amdtp_out_stream_pcm_abort(&fwspk->stream);
782 fwspk_stop_stream(fwspk);
783 mutex_unlock(&fwspk->mutex);
785 snd_card_free_when_closed(fwspk->card);
790 static void fwspk_bus_reset(struct fw_unit *unit)
792 struct fwspk *fwspk = dev_get_drvdata(&unit->device);
794 fcp_bus_reset(fwspk->unit);
796 if (cmp_connection_update(&fwspk->connection) < 0) {
797 mutex_lock(&fwspk->mutex);
798 amdtp_out_stream_pcm_abort(&fwspk->stream);
799 fwspk_stop_stream(fwspk);
800 mutex_unlock(&fwspk->mutex);
804 amdtp_out_stream_update(&fwspk->stream);
807 static const struct ieee1394_device_id fwspk_id_table[] = {
809 .match_flags = IEEE1394_MATCH_VENDOR_ID |
810 IEEE1394_MATCH_MODEL_ID |
811 IEEE1394_MATCH_SPECIFIER_ID |
812 IEEE1394_MATCH_VERSION,
813 .vendor_id = VENDOR_GRIFFIN,
814 .model_id = 0x00f970,
815 .specifier_id = SPECIFIER_1394TA,
816 .version = VERSION_AVC,
819 .match_flags = IEEE1394_MATCH_VENDOR_ID |
820 IEEE1394_MATCH_MODEL_ID |
821 IEEE1394_MATCH_SPECIFIER_ID |
822 IEEE1394_MATCH_VERSION,
823 .vendor_id = VENDOR_LACIE,
824 .model_id = 0x00f970,
825 .specifier_id = SPECIFIER_1394TA,
826 .version = VERSION_AVC,
830 MODULE_DEVICE_TABLE(ieee1394, fwspk_id_table);
832 static struct fw_driver fwspk_driver = {
834 .owner = THIS_MODULE,
835 .name = KBUILD_MODNAME,
837 .probe = fwspk_probe,
838 .remove = __devexit_p(fwspk_remove),
840 .update = fwspk_bus_reset,
841 .id_table = fwspk_id_table,
844 static int __init alsa_fwspk_init(void)
846 return driver_register(&fwspk_driver.driver);
849 static void __exit alsa_fwspk_exit(void)
851 driver_unregister(&fwspk_driver.driver);
854 module_init(alsa_fwspk_init);
855 module_exit(alsa_fwspk_exit);