2 * TC Applied Technologies Digital Interface Communications Engine 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/compat.h>
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/firewire.h>
13 #include <linux/firewire-constants.h>
14 #include <linux/jiffies.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/wait.h>
21 #include <sound/control.h>
22 #include <sound/core.h>
23 #include <sound/firewire.h>
24 #include <sound/hwdep.h>
25 #include <sound/initval.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
29 #include "iso-resources.h"
31 #include "dice-interface.h"
35 struct snd_card *card;
39 unsigned int global_offset;
40 unsigned int rx_offset;
41 unsigned int clock_caps;
42 unsigned int rx_channels[3];
43 unsigned int rx_midi_ports[3];
44 struct fw_address_handler notification_handler;
46 int dev_lock_count; /* > 0 driver, < 0 userspace */
47 bool dev_lock_changed;
49 struct completion clock_accepted;
50 wait_queue_head_t hwdep_wait;
51 u32 notification_bits;
52 struct fw_iso_resources resources;
53 struct amdtp_out_stream stream;
56 MODULE_DESCRIPTION("DICE driver");
57 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
58 MODULE_LICENSE("GPL v2");
60 static const unsigned int dice_rates[] = {
73 static unsigned int rate_to_index(unsigned int rate)
77 for (i = 0; i < ARRAY_SIZE(dice_rates); ++i)
78 if (dice_rates[i] == rate)
84 static unsigned int rate_index_to_mode(unsigned int rate_index)
86 return ((int)rate_index - 1) / 2;
89 static void dice_lock_changed(struct dice *dice)
91 dice->dev_lock_changed = true;
92 wake_up(&dice->hwdep_wait);
95 static int dice_try_lock(struct dice *dice)
99 spin_lock_irq(&dice->lock);
101 if (dice->dev_lock_count < 0) {
106 if (dice->dev_lock_count++ == 0)
107 dice_lock_changed(dice);
111 spin_unlock_irq(&dice->lock);
116 static void dice_unlock(struct dice *dice)
118 spin_lock_irq(&dice->lock);
120 if (WARN_ON(dice->dev_lock_count <= 0))
123 if (--dice->dev_lock_count == 0)
124 dice_lock_changed(dice);
127 spin_unlock_irq(&dice->lock);
130 static inline u64 global_address(struct dice *dice, unsigned int offset)
132 return DICE_PRIVATE_SPACE + dice->global_offset + offset;
136 static inline u64 rx_address(struct dice *dice, unsigned int offset)
138 return DICE_PRIVATE_SPACE + dice->rx_offset + offset;
141 static int dice_owner_set(struct dice *dice)
143 struct fw_device *device = fw_parent_device(dice->unit);
147 buffer = kmalloc(2 * 8, GFP_KERNEL);
152 buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
153 buffer[1] = cpu_to_be64(
154 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
155 dice->notification_handler.offset);
157 dice->owner_generation = device->generation;
158 smp_rmb(); /* node_id vs. generation */
159 err = snd_fw_transaction(dice->unit,
160 TCODE_LOCK_COMPARE_SWAP,
161 global_address(dice, GLOBAL_OWNER),
163 FW_FIXED_GENERATION |
164 dice->owner_generation);
167 if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
168 dev_err(&dice->unit->device,
169 "device is already in use\n");
174 if (err != -EAGAIN || ++errors >= 3)
185 static int dice_owner_update(struct dice *dice)
187 struct fw_device *device = fw_parent_device(dice->unit);
191 if (dice->owner_generation == -1)
194 buffer = kmalloc(2 * 8, GFP_KERNEL);
198 buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
199 buffer[1] = cpu_to_be64(
200 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
201 dice->notification_handler.offset);
203 dice->owner_generation = device->generation;
204 smp_rmb(); /* node_id vs. generation */
205 err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
206 global_address(dice, GLOBAL_OWNER),
208 FW_FIXED_GENERATION | dice->owner_generation);
211 if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
212 dev_err(&dice->unit->device,
213 "device is already in use\n");
216 } else if (err == -EAGAIN) {
217 err = 0; /* try again later */
223 dice->owner_generation = -1;
228 static void dice_owner_clear(struct dice *dice)
230 struct fw_device *device = fw_parent_device(dice->unit);
233 buffer = kmalloc(2 * 8, GFP_KERNEL);
237 buffer[0] = cpu_to_be64(
238 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
239 dice->notification_handler.offset);
240 buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
241 snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
242 global_address(dice, GLOBAL_OWNER),
243 buffer, 2 * 8, FW_QUIET |
244 FW_FIXED_GENERATION | dice->owner_generation);
248 dice->owner_generation = -1;
251 static int dice_enable_set(struct dice *dice)
256 value = cpu_to_be32(1);
257 err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
258 global_address(dice, GLOBAL_ENABLE),
260 FW_FIXED_GENERATION | dice->owner_generation);
264 dice->global_enabled = true;
269 static void dice_enable_clear(struct dice *dice)
273 if (!dice->global_enabled)
277 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
278 global_address(dice, GLOBAL_ENABLE),
279 &value, 4, FW_QUIET |
280 FW_FIXED_GENERATION | dice->owner_generation);
282 dice->global_enabled = false;
285 static void dice_notification(struct fw_card *card, struct fw_request *request,
286 int tcode, int destination, int source,
287 int generation, unsigned long long offset,
288 void *data, size_t length, void *callback_data)
290 struct dice *dice = callback_data;
294 if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
295 fw_send_response(card, request, RCODE_TYPE_ERROR);
298 if ((offset & 3) != 0) {
299 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
303 bits = be32_to_cpup(data);
305 spin_lock_irqsave(&dice->lock, flags);
306 dice->notification_bits |= bits;
307 spin_unlock_irqrestore(&dice->lock, flags);
309 fw_send_response(card, request, RCODE_COMPLETE);
311 if (bits & NOTIFY_CLOCK_ACCEPTED)
312 complete(&dice->clock_accepted);
313 wake_up(&dice->hwdep_wait);
316 static int dice_rate_constraint(struct snd_pcm_hw_params *params,
317 struct snd_pcm_hw_rule *rule)
319 struct dice *dice = rule->private;
320 const struct snd_interval *channels =
321 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
322 struct snd_interval *rate =
323 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
324 struct snd_interval allowed_rates = {
325 .min = UINT_MAX, .max = 0, .integer = 1
327 unsigned int i, mode;
329 for (i = 0; i < ARRAY_SIZE(dice_rates); ++i) {
330 mode = rate_index_to_mode(i);
331 if ((dice->clock_caps & (1 << i)) &&
332 snd_interval_test(channels, dice->rx_channels[mode])) {
333 allowed_rates.min = min(allowed_rates.min,
335 allowed_rates.max = max(allowed_rates.max,
340 return snd_interval_refine(rate, &allowed_rates);
343 static int dice_channels_constraint(struct snd_pcm_hw_params *params,
344 struct snd_pcm_hw_rule *rule)
346 struct dice *dice = rule->private;
347 const struct snd_interval *rate =
348 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
349 struct snd_interval *channels =
350 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
351 struct snd_interval allowed_channels = {
352 .min = UINT_MAX, .max = 0, .integer = 1
354 unsigned int i, mode;
356 for (i = 0; i < ARRAY_SIZE(dice_rates); ++i)
357 if ((dice->clock_caps & (1 << i)) &&
358 snd_interval_test(rate, dice_rates[i])) {
359 mode = rate_index_to_mode(i);
360 allowed_channels.min = min(allowed_channels.min,
361 dice->rx_channels[mode]);
362 allowed_channels.max = max(allowed_channels.max,
363 dice->rx_channels[mode]);
366 return snd_interval_refine(channels, &allowed_channels);
369 static int dice_open(struct snd_pcm_substream *substream)
371 static const struct snd_pcm_hardware hardware = {
372 .info = SNDRV_PCM_INFO_MMAP |
373 SNDRV_PCM_INFO_MMAP_VALID |
374 SNDRV_PCM_INFO_BATCH |
375 SNDRV_PCM_INFO_INTERLEAVED |
376 SNDRV_PCM_INFO_BLOCK_TRANSFER,
377 .formats = AMDTP_OUT_PCM_FORMAT_BITS,
378 .channels_min = UINT_MAX,
380 .buffer_bytes_max = 16 * 1024 * 1024,
381 .period_bytes_min = 1,
382 .period_bytes_max = UINT_MAX,
384 .periods_max = UINT_MAX,
386 struct dice *dice = substream->private_data;
387 struct snd_pcm_runtime *runtime = substream->runtime;
391 err = dice_try_lock(dice);
395 runtime->hw = hardware;
397 for (i = 0; i < ARRAY_SIZE(dice_rates); ++i)
398 if (dice->clock_caps & (1 << i))
400 snd_pcm_rate_to_rate_bit(dice_rates[i]);
401 snd_pcm_limit_hw_rates(runtime);
403 for (i = 0; i < 3; ++i)
404 if (dice->rx_channels[i]) {
405 runtime->hw.channels_min = min(runtime->hw.channels_min,
406 dice->rx_channels[i]);
407 runtime->hw.channels_max = max(runtime->hw.channels_max,
408 dice->rx_channels[i]);
411 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
412 dice_rate_constraint, dice,
413 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
416 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
417 dice_channels_constraint, dice,
418 SNDRV_PCM_HW_PARAM_RATE, -1);
422 err = snd_pcm_hw_constraint_step(runtime, 0,
423 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
426 err = snd_pcm_hw_constraint_step(runtime, 0,
427 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
431 err = snd_pcm_hw_constraint_minmax(runtime,
432 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
437 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
449 static int dice_close(struct snd_pcm_substream *substream)
451 struct dice *dice = substream->private_data;
458 static int dice_stream_start_packets(struct dice *dice)
462 if (amdtp_out_stream_running(&dice->stream))
465 err = amdtp_out_stream_start(&dice->stream, dice->resources.channel,
466 fw_parent_device(dice->unit)->max_speed);
470 err = dice_enable_set(dice);
472 amdtp_out_stream_stop(&dice->stream);
479 static int dice_stream_start(struct dice *dice)
484 if (!dice->resources.allocated) {
485 err = fw_iso_resources_allocate(&dice->resources,
486 amdtp_out_stream_get_max_payload(&dice->stream),
487 fw_parent_device(dice->unit)->max_speed);
491 channel = cpu_to_be32(dice->resources.channel);
492 err = snd_fw_transaction(dice->unit,
493 TCODE_WRITE_QUADLET_REQUEST,
494 rx_address(dice, RX_ISOCHRONOUS),
500 err = dice_stream_start_packets(dice);
507 channel = cpu_to_be32((u32)-1);
508 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
509 rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
511 fw_iso_resources_free(&dice->resources);
516 static void dice_stream_stop_packets(struct dice *dice)
518 if (amdtp_out_stream_running(&dice->stream)) {
519 dice_enable_clear(dice);
520 amdtp_out_stream_stop(&dice->stream);
524 static void dice_stream_stop(struct dice *dice)
528 dice_stream_stop_packets(dice);
530 if (!dice->resources.allocated)
533 channel = cpu_to_be32((u32)-1);
534 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
535 rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
537 fw_iso_resources_free(&dice->resources);
540 static int dice_change_rate(struct dice *dice, unsigned int clock_rate)
545 INIT_COMPLETION(dice->clock_accepted);
547 value = cpu_to_be32(clock_rate | CLOCK_SOURCE_ARX1);
548 err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
549 global_address(dice, GLOBAL_CLOCK_SELECT),
554 wait_for_completion_timeout(&dice->clock_accepted,
555 msecs_to_jiffies(100));
560 static int dice_hw_params(struct snd_pcm_substream *substream,
561 struct snd_pcm_hw_params *hw_params)
563 struct dice *dice = substream->private_data;
564 unsigned int rate_index, mode;
567 mutex_lock(&dice->mutex);
568 dice_stream_stop(dice);
569 mutex_unlock(&dice->mutex);
571 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
572 params_buffer_bytes(hw_params));
576 rate_index = rate_to_index(params_rate(hw_params));
577 err = dice_change_rate(dice, rate_index << CLOCK_RATE_SHIFT);
581 mode = rate_index_to_mode(rate_index);
582 amdtp_out_stream_set_parameters(&dice->stream,
583 params_rate(hw_params),
584 params_channels(hw_params),
585 dice->rx_midi_ports[mode]);
586 amdtp_out_stream_set_pcm_format(&dice->stream,
587 params_format(hw_params));
592 static int dice_hw_free(struct snd_pcm_substream *substream)
594 struct dice *dice = substream->private_data;
596 mutex_lock(&dice->mutex);
597 dice_stream_stop(dice);
598 mutex_unlock(&dice->mutex);
600 return snd_pcm_lib_free_vmalloc_buffer(substream);
603 static int dice_prepare(struct snd_pcm_substream *substream)
605 struct dice *dice = substream->private_data;
608 mutex_lock(&dice->mutex);
610 if (amdtp_out_streaming_error(&dice->stream))
611 dice_stream_stop_packets(dice);
613 err = dice_stream_start(dice);
615 mutex_unlock(&dice->mutex);
619 mutex_unlock(&dice->mutex);
621 amdtp_out_stream_pcm_prepare(&dice->stream);
626 static int dice_trigger(struct snd_pcm_substream *substream, int cmd)
628 struct dice *dice = substream->private_data;
629 struct snd_pcm_substream *pcm;
632 case SNDRV_PCM_TRIGGER_START:
635 case SNDRV_PCM_TRIGGER_STOP:
641 amdtp_out_stream_pcm_trigger(&dice->stream, pcm);
646 static snd_pcm_uframes_t dice_pointer(struct snd_pcm_substream *substream)
648 struct dice *dice = substream->private_data;
650 return amdtp_out_stream_pcm_pointer(&dice->stream);
653 static int dice_create_pcm(struct dice *dice)
655 static struct snd_pcm_ops ops = {
658 .ioctl = snd_pcm_lib_ioctl,
659 .hw_params = dice_hw_params,
660 .hw_free = dice_hw_free,
661 .prepare = dice_prepare,
662 .trigger = dice_trigger,
663 .pointer = dice_pointer,
664 .page = snd_pcm_lib_get_vmalloc_page,
665 .mmap = snd_pcm_lib_mmap_vmalloc,
670 err = snd_pcm_new(dice->card, "DICE", 0, 1, 0, &pcm);
673 pcm->private_data = dice;
674 strcpy(pcm->name, dice->card->shortname);
675 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->ops = &ops;
680 static long dice_hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
681 long count, loff_t *offset)
683 struct dice *dice = hwdep->private_data;
685 union snd_firewire_event event;
687 spin_lock_irq(&dice->lock);
689 while (!dice->dev_lock_changed && dice->notification_bits == 0) {
690 prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
691 spin_unlock_irq(&dice->lock);
693 finish_wait(&dice->hwdep_wait, &wait);
694 if (signal_pending(current))
696 spin_lock_irq(&dice->lock);
699 memset(&event, 0, sizeof(event));
700 if (dice->dev_lock_changed) {
701 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
702 event.lock_status.status = dice->dev_lock_count > 0;
703 dice->dev_lock_changed = false;
705 count = min(count, (long)sizeof(event.lock_status));
707 event.dice_notification.type = SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
708 event.dice_notification.notification = dice->notification_bits;
709 dice->notification_bits = 0;
711 count = min(count, (long)sizeof(event.dice_notification));
714 spin_unlock_irq(&dice->lock);
716 if (copy_to_user(buf, &event, count))
722 static unsigned int dice_hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
725 struct dice *dice = hwdep->private_data;
728 poll_wait(file, &dice->hwdep_wait, wait);
730 spin_lock_irq(&dice->lock);
731 if (dice->dev_lock_changed || dice->notification_bits != 0)
732 events = POLLIN | POLLRDNORM;
735 spin_unlock_irq(&dice->lock);
740 static int dice_hwdep_get_info(struct dice *dice, void __user *arg)
742 struct fw_device *dev = fw_parent_device(dice->unit);
743 struct snd_firewire_get_info info;
745 memset(&info, 0, sizeof(info));
746 info.type = SNDRV_FIREWIRE_TYPE_DICE;
747 info.card = dev->card->index;
748 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
749 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
750 strlcpy(info.device_name, dev_name(&dev->device),
751 sizeof(info.device_name));
753 if (copy_to_user(arg, &info, sizeof(info)))
759 static int dice_hwdep_lock(struct dice *dice)
763 spin_lock_irq(&dice->lock);
765 if (dice->dev_lock_count == 0) {
766 dice->dev_lock_count = -1;
772 spin_unlock_irq(&dice->lock);
777 static int dice_hwdep_unlock(struct dice *dice)
781 spin_lock_irq(&dice->lock);
783 if (dice->dev_lock_count == -1) {
784 dice->dev_lock_count = 0;
790 spin_unlock_irq(&dice->lock);
795 static int dice_hwdep_release(struct snd_hwdep *hwdep, struct file *file)
797 struct dice *dice = hwdep->private_data;
799 spin_lock_irq(&dice->lock);
800 if (dice->dev_lock_count == -1)
801 dice->dev_lock_count = 0;
802 spin_unlock_irq(&dice->lock);
807 static int dice_hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
808 unsigned int cmd, unsigned long arg)
810 struct dice *dice = hwdep->private_data;
813 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
814 return dice_hwdep_get_info(dice, (void __user *)arg);
815 case SNDRV_FIREWIRE_IOCTL_LOCK:
816 return dice_hwdep_lock(dice);
817 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
818 return dice_hwdep_unlock(dice);
825 static int dice_hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
826 unsigned int cmd, unsigned long arg)
828 return dice_hwdep_ioctl(hwdep, file, cmd,
829 (unsigned long)compat_ptr(arg));
832 #define dice_hwdep_compat_ioctl NULL
835 static int dice_create_hwdep(struct dice *dice)
837 static const struct snd_hwdep_ops ops = {
838 .read = dice_hwdep_read,
839 .release = dice_hwdep_release,
840 .poll = dice_hwdep_poll,
841 .ioctl = dice_hwdep_ioctl,
842 .ioctl_compat = dice_hwdep_compat_ioctl,
844 struct snd_hwdep *hwdep;
847 err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
850 strcpy(hwdep->name, "DICE");
851 hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
853 hwdep->private_data = dice;
854 hwdep->exclusive = true;
859 static void dice_card_free(struct snd_card *card)
861 struct dice *dice = card->private_data;
863 amdtp_out_stream_destroy(&dice->stream);
864 fw_core_remove_address_handler(&dice->notification_handler);
865 mutex_destroy(&dice->mutex);
868 #define DICE_CATEGORY_ID 0x04
870 static int dice_interface_check(struct fw_unit *unit)
872 static const int min_values[10] = {
879 struct fw_device *device = fw_parent_device(unit);
880 struct fw_csr_iterator it;
881 int key, value, vendor = -1, model = -1, err;
883 __be32 pointers[ARRAY_SIZE(min_values)];
887 * Check that GUID and unit directory are constructed according to DICE
888 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
889 * GUID chip ID consists of the 8-bit DICE category ID, the 10-bit
890 * product ID, and a 22-bit serial number.
892 fw_csr_iterator_init(&it, unit->directory);
893 while (fw_csr_iterator_next(&it, &key, &value)) {
895 case CSR_SPECIFIER_ID:
903 if (device->config_rom[3] != ((vendor << 8) | DICE_CATEGORY_ID) ||
904 device->config_rom[4] >> 22 != model)
908 * Check that the sub address spaces exist and are located inside the
909 * private address space. The minimum values are chosen so that all
910 * minimally required registers are included.
912 err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
914 pointers, sizeof(pointers), 0);
917 for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
918 value = be32_to_cpu(pointers[i]);
919 if (value < min_values[i] || value >= 0x40000)
924 * Check that the implemented DICE driver specification major version
927 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
929 be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
933 if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
934 dev_err(&unit->device,
935 "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
942 static int highest_supported_mode_rate(struct dice *dice, unsigned int mode)
946 for (i = ARRAY_SIZE(dice_rates) - 1; i >= 0; --i)
947 if ((dice->clock_caps & (1 << i)) &&
948 rate_index_to_mode(i) == mode)
954 static int dice_read_mode_params(struct dice *dice, unsigned int mode)
959 rate_index = highest_supported_mode_rate(dice, mode);
960 if (rate_index < 0) {
961 dice->rx_channels[mode] = 0;
962 dice->rx_midi_ports[mode] = 0;
966 err = dice_change_rate(dice, rate_index << CLOCK_RATE_SHIFT);
970 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
971 rx_address(dice, RX_NUMBER_AUDIO),
976 dice->rx_channels[mode] = be32_to_cpu(values[0]);
977 dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
982 static int dice_read_params(struct dice *dice)
988 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
990 pointers, sizeof(pointers), 0);
994 dice->global_offset = be32_to_cpu(pointers[0]) * 4;
995 dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
997 /* some very old firmwares don't tell about their clock support */
998 if (be32_to_cpu(pointers[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES + 4) {
999 err = snd_fw_transaction(
1000 dice->unit, TCODE_READ_QUADLET_REQUEST,
1001 global_address(dice, GLOBAL_CLOCK_CAPABILITIES),
1005 dice->clock_caps = be32_to_cpu(value);
1007 /* this should be supported by any device */
1008 dice->clock_caps = CLOCK_CAP_RATE_44100 |
1009 CLOCK_CAP_RATE_48000 |
1010 CLOCK_CAP_SOURCE_ARX1 |
1011 CLOCK_CAP_SOURCE_INTERNAL;
1014 for (mode = 2; mode >= 0; --mode) {
1015 err = dice_read_mode_params(dice, mode);
1023 static void dice_card_strings(struct dice *dice)
1025 struct snd_card *card = dice->card;
1026 struct fw_device *dev = fw_parent_device(dice->unit);
1027 char vendor[32], model[32];
1031 strcpy(card->driver, "DICE");
1033 strcpy(card->shortname, "DICE");
1034 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
1035 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
1036 global_address(dice, GLOBAL_NICK_NAME),
1037 card->shortname, sizeof(card->shortname), 0);
1039 /* DICE strings are returned in "always-wrong" endianness */
1040 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
1041 for (i = 0; i < sizeof(card->shortname); i += 4)
1042 swab32s((u32 *)&card->shortname[i]);
1043 card->shortname[sizeof(card->shortname) - 1] = '\0';
1046 strcpy(vendor, "?");
1047 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
1049 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
1050 snprintf(card->longname, sizeof(card->longname),
1051 "%s %s (serial %u) at %s, S%d",
1052 vendor, model, dev->config_rom[4] & 0x3fffff,
1053 dev_name(&dice->unit->device), 100 << dev->max_speed);
1055 strcpy(card->mixername, "DICE");
1058 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
1060 struct snd_card *card;
1065 err = dice_interface_check(unit);
1069 err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
1072 snd_card_set_dev(card, &unit->device);
1074 dice = card->private_data;
1076 spin_lock_init(&dice->lock);
1077 mutex_init(&dice->mutex);
1079 init_completion(&dice->clock_accepted);
1080 init_waitqueue_head(&dice->hwdep_wait);
1082 dice->notification_handler.length = 4;
1083 dice->notification_handler.address_callback = dice_notification;
1084 dice->notification_handler.callback_data = dice;
1085 err = fw_core_add_address_handler(&dice->notification_handler,
1086 &fw_high_memory_region);
1090 err = dice_owner_set(dice);
1092 goto err_notification_handler;
1094 err = dice_read_params(dice);
1098 err = fw_iso_resources_init(&dice->resources, unit);
1101 dice->resources.channels_mask = 0x00000000ffffffffuLL;
1103 err = amdtp_out_stream_init(&dice->stream, unit,
1104 CIP_BLOCKING | CIP_HI_DUALWIRE);
1108 card->private_free = dice_card_free;
1110 dice_card_strings(dice);
1112 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
1113 global_address(dice, GLOBAL_CLOCK_SELECT),
1117 clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
1118 clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
1119 err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
1120 global_address(dice, GLOBAL_CLOCK_SELECT),
1125 err = dice_create_pcm(dice);
1129 err = dice_create_hwdep(dice);
1133 err = snd_card_register(card);
1137 dev_set_drvdata(&unit->device, dice);
1142 fw_iso_resources_destroy(&dice->resources);
1144 dice_owner_clear(dice);
1145 err_notification_handler:
1146 fw_core_remove_address_handler(&dice->notification_handler);
1148 mutex_destroy(&dice->mutex);
1150 snd_card_free(card);
1154 static void dice_remove(struct fw_unit *unit)
1156 struct dice *dice = dev_get_drvdata(&unit->device);
1158 amdtp_out_stream_pcm_abort(&dice->stream);
1160 snd_card_disconnect(dice->card);
1162 mutex_lock(&dice->mutex);
1164 dice_stream_stop(dice);
1165 dice_owner_clear(dice);
1167 mutex_unlock(&dice->mutex);
1169 snd_card_free_when_closed(dice->card);
1172 static void dice_bus_reset(struct fw_unit *unit)
1174 struct dice *dice = dev_get_drvdata(&unit->device);
1177 * On a bus reset, the DICE firmware disables streaming and then goes
1178 * off contemplating its own navel for hundreds of milliseconds before
1179 * it can react to any of our attempts to reenable streaming. This
1180 * means that we lose synchronization anyway, so we force our streams
1181 * to stop so that the application can restart them in an orderly
1184 amdtp_out_stream_pcm_abort(&dice->stream);
1186 mutex_lock(&dice->mutex);
1188 dice->global_enabled = false;
1189 dice_stream_stop_packets(dice);
1191 dice_owner_update(dice);
1193 fw_iso_resources_update(&dice->resources);
1195 mutex_unlock(&dice->mutex);
1198 #define DICE_INTERFACE 0x000001
1200 static const struct ieee1394_device_id dice_id_table[] = {
1202 .match_flags = IEEE1394_MATCH_VERSION,
1203 .version = DICE_INTERFACE,
1207 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
1209 static struct fw_driver dice_driver = {
1211 .owner = THIS_MODULE,
1212 .name = KBUILD_MODNAME,
1213 .bus = &fw_bus_type,
1215 .probe = dice_probe,
1216 .update = dice_bus_reset,
1217 .remove = dice_remove,
1218 .id_table = dice_id_table,
1221 static int __init alsa_dice_init(void)
1223 return driver_register(&dice_driver.driver);
1226 static void __exit alsa_dice_exit(void)
1228 driver_unregister(&dice_driver.driver);
1231 module_init(alsa_dice_init);
1232 module_exit(alsa_dice_exit);