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/info.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
30 #include "iso-resources.h"
32 #include "dice-interface.h"
36 struct snd_card
*card
;
40 unsigned int global_offset
;
41 unsigned int rx_offset
;
42 unsigned int clock_caps
;
43 unsigned int rx_channels
[3];
44 unsigned int rx_midi_ports
[3];
45 struct fw_address_handler notification_handler
;
47 int dev_lock_count
; /* > 0 driver, < 0 userspace */
48 bool dev_lock_changed
;
50 struct completion clock_accepted
;
51 wait_queue_head_t hwdep_wait
;
52 u32 notification_bits
;
53 struct fw_iso_resources resources
;
54 struct amdtp_stream stream
;
57 MODULE_DESCRIPTION("DICE driver");
58 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
59 MODULE_LICENSE("GPL v2");
61 static const unsigned int dice_rates
[] = {
74 static unsigned int rate_to_index(unsigned int rate
)
78 for (i
= 0; i
< ARRAY_SIZE(dice_rates
); ++i
)
79 if (dice_rates
[i
] == rate
)
85 static unsigned int rate_index_to_mode(unsigned int rate_index
)
87 return ((int)rate_index
- 1) / 2;
90 static void dice_lock_changed(struct dice
*dice
)
92 dice
->dev_lock_changed
= true;
93 wake_up(&dice
->hwdep_wait
);
96 static int dice_try_lock(struct dice
*dice
)
100 spin_lock_irq(&dice
->lock
);
102 if (dice
->dev_lock_count
< 0) {
107 if (dice
->dev_lock_count
++ == 0)
108 dice_lock_changed(dice
);
112 spin_unlock_irq(&dice
->lock
);
117 static void dice_unlock(struct dice
*dice
)
119 spin_lock_irq(&dice
->lock
);
121 if (WARN_ON(dice
->dev_lock_count
<= 0))
124 if (--dice
->dev_lock_count
== 0)
125 dice_lock_changed(dice
);
128 spin_unlock_irq(&dice
->lock
);
131 static inline u64
global_address(struct dice
*dice
, unsigned int offset
)
133 return DICE_PRIVATE_SPACE
+ dice
->global_offset
+ offset
;
137 static inline u64
rx_address(struct dice
*dice
, unsigned int offset
)
139 return DICE_PRIVATE_SPACE
+ dice
->rx_offset
+ offset
;
142 static int dice_owner_set(struct dice
*dice
)
144 struct fw_device
*device
= fw_parent_device(dice
->unit
);
148 buffer
= kmalloc(2 * 8, GFP_KERNEL
);
153 buffer
[0] = cpu_to_be64(OWNER_NO_OWNER
);
154 buffer
[1] = cpu_to_be64(
155 ((u64
)device
->card
->node_id
<< OWNER_NODE_SHIFT
) |
156 dice
->notification_handler
.offset
);
158 dice
->owner_generation
= device
->generation
;
159 smp_rmb(); /* node_id vs. generation */
160 err
= snd_fw_transaction(dice
->unit
,
161 TCODE_LOCK_COMPARE_SWAP
,
162 global_address(dice
, GLOBAL_OWNER
),
164 FW_FIXED_GENERATION
|
165 dice
->owner_generation
);
168 if (buffer
[0] != cpu_to_be64(OWNER_NO_OWNER
)) {
169 dev_err(&dice
->unit
->device
,
170 "device is already in use\n");
175 if (err
!= -EAGAIN
|| ++errors
>= 3)
186 static int dice_owner_update(struct dice
*dice
)
188 struct fw_device
*device
= fw_parent_device(dice
->unit
);
192 if (dice
->owner_generation
== -1)
195 buffer
= kmalloc(2 * 8, GFP_KERNEL
);
199 buffer
[0] = cpu_to_be64(OWNER_NO_OWNER
);
200 buffer
[1] = cpu_to_be64(
201 ((u64
)device
->card
->node_id
<< OWNER_NODE_SHIFT
) |
202 dice
->notification_handler
.offset
);
204 dice
->owner_generation
= device
->generation
;
205 smp_rmb(); /* node_id vs. generation */
206 err
= snd_fw_transaction(dice
->unit
, TCODE_LOCK_COMPARE_SWAP
,
207 global_address(dice
, GLOBAL_OWNER
),
209 FW_FIXED_GENERATION
| dice
->owner_generation
);
212 if (buffer
[0] != cpu_to_be64(OWNER_NO_OWNER
)) {
213 dev_err(&dice
->unit
->device
,
214 "device is already in use\n");
217 } else if (err
== -EAGAIN
) {
218 err
= 0; /* try again later */
224 dice
->owner_generation
= -1;
229 static void dice_owner_clear(struct dice
*dice
)
231 struct fw_device
*device
= fw_parent_device(dice
->unit
);
234 buffer
= kmalloc(2 * 8, GFP_KERNEL
);
238 buffer
[0] = cpu_to_be64(
239 ((u64
)device
->card
->node_id
<< OWNER_NODE_SHIFT
) |
240 dice
->notification_handler
.offset
);
241 buffer
[1] = cpu_to_be64(OWNER_NO_OWNER
);
242 snd_fw_transaction(dice
->unit
, TCODE_LOCK_COMPARE_SWAP
,
243 global_address(dice
, GLOBAL_OWNER
),
244 buffer
, 2 * 8, FW_QUIET
|
245 FW_FIXED_GENERATION
| dice
->owner_generation
);
249 dice
->owner_generation
= -1;
252 static int dice_enable_set(struct dice
*dice
)
257 value
= cpu_to_be32(1);
258 err
= snd_fw_transaction(dice
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
259 global_address(dice
, GLOBAL_ENABLE
),
261 FW_FIXED_GENERATION
| dice
->owner_generation
);
265 dice
->global_enabled
= true;
270 static void dice_enable_clear(struct dice
*dice
)
274 if (!dice
->global_enabled
)
278 snd_fw_transaction(dice
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
279 global_address(dice
, GLOBAL_ENABLE
),
280 &value
, 4, FW_QUIET
|
281 FW_FIXED_GENERATION
| dice
->owner_generation
);
283 dice
->global_enabled
= false;
286 static void dice_notification(struct fw_card
*card
, struct fw_request
*request
,
287 int tcode
, int destination
, int source
,
288 int generation
, unsigned long long offset
,
289 void *data
, size_t length
, void *callback_data
)
291 struct dice
*dice
= callback_data
;
295 if (tcode
!= TCODE_WRITE_QUADLET_REQUEST
) {
296 fw_send_response(card
, request
, RCODE_TYPE_ERROR
);
299 if ((offset
& 3) != 0) {
300 fw_send_response(card
, request
, RCODE_ADDRESS_ERROR
);
304 bits
= be32_to_cpup(data
);
306 spin_lock_irqsave(&dice
->lock
, flags
);
307 dice
->notification_bits
|= bits
;
308 spin_unlock_irqrestore(&dice
->lock
, flags
);
310 fw_send_response(card
, request
, RCODE_COMPLETE
);
312 if (bits
& NOTIFY_CLOCK_ACCEPTED
)
313 complete(&dice
->clock_accepted
);
314 wake_up(&dice
->hwdep_wait
);
317 static int dice_rate_constraint(struct snd_pcm_hw_params
*params
,
318 struct snd_pcm_hw_rule
*rule
)
320 struct dice
*dice
= rule
->private;
321 const struct snd_interval
*channels
=
322 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
323 struct snd_interval
*rate
=
324 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
325 struct snd_interval allowed_rates
= {
326 .min
= UINT_MAX
, .max
= 0, .integer
= 1
328 unsigned int i
, mode
;
330 for (i
= 0; i
< ARRAY_SIZE(dice_rates
); ++i
) {
331 mode
= rate_index_to_mode(i
);
332 if ((dice
->clock_caps
& (1 << i
)) &&
333 snd_interval_test(channels
, dice
->rx_channels
[mode
])) {
334 allowed_rates
.min
= min(allowed_rates
.min
,
336 allowed_rates
.max
= max(allowed_rates
.max
,
341 return snd_interval_refine(rate
, &allowed_rates
);
344 static int dice_channels_constraint(struct snd_pcm_hw_params
*params
,
345 struct snd_pcm_hw_rule
*rule
)
347 struct dice
*dice
= rule
->private;
348 const struct snd_interval
*rate
=
349 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_RATE
);
350 struct snd_interval
*channels
=
351 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
352 struct snd_interval allowed_channels
= {
353 .min
= UINT_MAX
, .max
= 0, .integer
= 1
355 unsigned int i
, mode
;
357 for (i
= 0; i
< ARRAY_SIZE(dice_rates
); ++i
)
358 if ((dice
->clock_caps
& (1 << i
)) &&
359 snd_interval_test(rate
, dice_rates
[i
])) {
360 mode
= rate_index_to_mode(i
);
361 allowed_channels
.min
= min(allowed_channels
.min
,
362 dice
->rx_channels
[mode
]);
363 allowed_channels
.max
= max(allowed_channels
.max
,
364 dice
->rx_channels
[mode
]);
367 return snd_interval_refine(channels
, &allowed_channels
);
370 static int dice_open(struct snd_pcm_substream
*substream
)
372 static const struct snd_pcm_hardware hardware
= {
373 .info
= SNDRV_PCM_INFO_MMAP
|
374 SNDRV_PCM_INFO_MMAP_VALID
|
375 SNDRV_PCM_INFO_BATCH
|
376 SNDRV_PCM_INFO_INTERLEAVED
|
377 SNDRV_PCM_INFO_BLOCK_TRANSFER
,
378 .formats
= AMDTP_OUT_PCM_FORMAT_BITS
,
379 .channels_min
= UINT_MAX
,
381 .buffer_bytes_max
= 16 * 1024 * 1024,
382 .period_bytes_min
= 1,
383 .period_bytes_max
= UINT_MAX
,
385 .periods_max
= UINT_MAX
,
387 struct dice
*dice
= substream
->private_data
;
388 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
392 err
= dice_try_lock(dice
);
396 runtime
->hw
= hardware
;
398 for (i
= 0; i
< ARRAY_SIZE(dice_rates
); ++i
)
399 if (dice
->clock_caps
& (1 << i
))
401 snd_pcm_rate_to_rate_bit(dice_rates
[i
]);
402 snd_pcm_limit_hw_rates(runtime
);
404 for (i
= 0; i
< 3; ++i
)
405 if (dice
->rx_channels
[i
]) {
406 runtime
->hw
.channels_min
= min(runtime
->hw
.channels_min
,
407 dice
->rx_channels
[i
]);
408 runtime
->hw
.channels_max
= max(runtime
->hw
.channels_max
,
409 dice
->rx_channels
[i
]);
412 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
413 dice_rate_constraint
, dice
,
414 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
417 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
418 dice_channels_constraint
, dice
,
419 SNDRV_PCM_HW_PARAM_RATE
, -1);
423 err
= amdtp_stream_add_pcm_hw_constraints(&dice
->stream
, runtime
);
435 static int dice_close(struct snd_pcm_substream
*substream
)
437 struct dice
*dice
= substream
->private_data
;
444 static int dice_stream_start_packets(struct dice
*dice
)
448 if (amdtp_stream_running(&dice
->stream
))
451 err
= amdtp_stream_start(&dice
->stream
, dice
->resources
.channel
,
452 fw_parent_device(dice
->unit
)->max_speed
);
456 err
= dice_enable_set(dice
);
458 amdtp_stream_stop(&dice
->stream
);
465 static int dice_stream_start(struct dice
*dice
)
470 if (!dice
->resources
.allocated
) {
471 err
= fw_iso_resources_allocate(&dice
->resources
,
472 amdtp_stream_get_max_payload(&dice
->stream
),
473 fw_parent_device(dice
->unit
)->max_speed
);
477 channel
= cpu_to_be32(dice
->resources
.channel
);
478 err
= snd_fw_transaction(dice
->unit
,
479 TCODE_WRITE_QUADLET_REQUEST
,
480 rx_address(dice
, RX_ISOCHRONOUS
),
486 err
= dice_stream_start_packets(dice
);
493 channel
= cpu_to_be32((u32
)-1);
494 snd_fw_transaction(dice
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
495 rx_address(dice
, RX_ISOCHRONOUS
), &channel
, 4, 0);
497 fw_iso_resources_free(&dice
->resources
);
502 static void dice_stream_stop_packets(struct dice
*dice
)
504 if (amdtp_stream_running(&dice
->stream
)) {
505 dice_enable_clear(dice
);
506 amdtp_stream_stop(&dice
->stream
);
510 static void dice_stream_stop(struct dice
*dice
)
514 dice_stream_stop_packets(dice
);
516 if (!dice
->resources
.allocated
)
519 channel
= cpu_to_be32((u32
)-1);
520 snd_fw_transaction(dice
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
521 rx_address(dice
, RX_ISOCHRONOUS
), &channel
, 4, 0);
523 fw_iso_resources_free(&dice
->resources
);
526 static int dice_change_rate(struct dice
*dice
, unsigned int clock_rate
)
531 reinit_completion(&dice
->clock_accepted
);
533 value
= cpu_to_be32(clock_rate
| CLOCK_SOURCE_ARX1
);
534 err
= snd_fw_transaction(dice
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
535 global_address(dice
, GLOBAL_CLOCK_SELECT
),
540 if (!wait_for_completion_timeout(&dice
->clock_accepted
,
541 msecs_to_jiffies(100)))
542 dev_warn(&dice
->unit
->device
, "clock change timed out\n");
547 static int dice_hw_params(struct snd_pcm_substream
*substream
,
548 struct snd_pcm_hw_params
*hw_params
)
550 struct dice
*dice
= substream
->private_data
;
551 unsigned int rate_index
, mode
, rate
, channels
, i
;
554 mutex_lock(&dice
->mutex
);
555 dice_stream_stop(dice
);
556 mutex_unlock(&dice
->mutex
);
558 err
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
559 params_buffer_bytes(hw_params
));
563 rate
= params_rate(hw_params
);
564 rate_index
= rate_to_index(rate
);
565 err
= dice_change_rate(dice
, rate_index
<< CLOCK_RATE_SHIFT
);
570 * At rates above 96 kHz, pretend that the stream runs at half the
571 * actual sample rate with twice the number of channels; two samples
572 * of a channel are stored consecutively in the packet. Requires
573 * blocking mode and PCM buffer size should be aligned to SYT_INTERVAL.
575 channels
= params_channels(hw_params
);
576 if (rate_index
> 4) {
577 if (channels
> AMDTP_MAX_CHANNELS_FOR_PCM
/ 2) {
582 for (i
= 0; i
< channels
; i
++) {
583 dice
->stream
.pcm_positions
[i
* 2] = i
;
584 dice
->stream
.pcm_positions
[i
* 2 + 1] = i
+ channels
;
591 mode
= rate_index_to_mode(rate_index
);
592 amdtp_stream_set_parameters(&dice
->stream
, rate
, channels
,
593 dice
->rx_midi_ports
[mode
]);
594 amdtp_stream_set_pcm_format(&dice
->stream
,
595 params_format(hw_params
));
600 static int dice_hw_free(struct snd_pcm_substream
*substream
)
602 struct dice
*dice
= substream
->private_data
;
604 mutex_lock(&dice
->mutex
);
605 dice_stream_stop(dice
);
606 mutex_unlock(&dice
->mutex
);
608 return snd_pcm_lib_free_vmalloc_buffer(substream
);
611 static int dice_prepare(struct snd_pcm_substream
*substream
)
613 struct dice
*dice
= substream
->private_data
;
616 mutex_lock(&dice
->mutex
);
618 if (amdtp_streaming_error(&dice
->stream
))
619 dice_stream_stop_packets(dice
);
621 err
= dice_stream_start(dice
);
623 mutex_unlock(&dice
->mutex
);
627 mutex_unlock(&dice
->mutex
);
629 amdtp_stream_pcm_prepare(&dice
->stream
);
634 static int dice_trigger(struct snd_pcm_substream
*substream
, int cmd
)
636 struct dice
*dice
= substream
->private_data
;
637 struct snd_pcm_substream
*pcm
;
640 case SNDRV_PCM_TRIGGER_START
:
643 case SNDRV_PCM_TRIGGER_STOP
:
649 amdtp_stream_pcm_trigger(&dice
->stream
, pcm
);
654 static snd_pcm_uframes_t
dice_pointer(struct snd_pcm_substream
*substream
)
656 struct dice
*dice
= substream
->private_data
;
658 return amdtp_stream_pcm_pointer(&dice
->stream
);
661 static int dice_create_pcm(struct dice
*dice
)
663 static struct snd_pcm_ops ops
= {
666 .ioctl
= snd_pcm_lib_ioctl
,
667 .hw_params
= dice_hw_params
,
668 .hw_free
= dice_hw_free
,
669 .prepare
= dice_prepare
,
670 .trigger
= dice_trigger
,
671 .pointer
= dice_pointer
,
672 .page
= snd_pcm_lib_get_vmalloc_page
,
673 .mmap
= snd_pcm_lib_mmap_vmalloc
,
678 err
= snd_pcm_new(dice
->card
, "DICE", 0, 1, 0, &pcm
);
681 pcm
->private_data
= dice
;
682 strcpy(pcm
->name
, dice
->card
->shortname
);
683 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->ops
= &ops
;
688 static long dice_hwdep_read(struct snd_hwdep
*hwdep
, char __user
*buf
,
689 long count
, loff_t
*offset
)
691 struct dice
*dice
= hwdep
->private_data
;
693 union snd_firewire_event event
;
695 spin_lock_irq(&dice
->lock
);
697 while (!dice
->dev_lock_changed
&& dice
->notification_bits
== 0) {
698 prepare_to_wait(&dice
->hwdep_wait
, &wait
, TASK_INTERRUPTIBLE
);
699 spin_unlock_irq(&dice
->lock
);
701 finish_wait(&dice
->hwdep_wait
, &wait
);
702 if (signal_pending(current
))
704 spin_lock_irq(&dice
->lock
);
707 memset(&event
, 0, sizeof(event
));
708 if (dice
->dev_lock_changed
) {
709 event
.lock_status
.type
= SNDRV_FIREWIRE_EVENT_LOCK_STATUS
;
710 event
.lock_status
.status
= dice
->dev_lock_count
> 0;
711 dice
->dev_lock_changed
= false;
713 count
= min(count
, (long)sizeof(event
.lock_status
));
715 event
.dice_notification
.type
= SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION
;
716 event
.dice_notification
.notification
= dice
->notification_bits
;
717 dice
->notification_bits
= 0;
719 count
= min(count
, (long)sizeof(event
.dice_notification
));
722 spin_unlock_irq(&dice
->lock
);
724 if (copy_to_user(buf
, &event
, count
))
730 static unsigned int dice_hwdep_poll(struct snd_hwdep
*hwdep
, struct file
*file
,
733 struct dice
*dice
= hwdep
->private_data
;
736 poll_wait(file
, &dice
->hwdep_wait
, wait
);
738 spin_lock_irq(&dice
->lock
);
739 if (dice
->dev_lock_changed
|| dice
->notification_bits
!= 0)
740 events
= POLLIN
| POLLRDNORM
;
743 spin_unlock_irq(&dice
->lock
);
748 static int dice_hwdep_get_info(struct dice
*dice
, void __user
*arg
)
750 struct fw_device
*dev
= fw_parent_device(dice
->unit
);
751 struct snd_firewire_get_info info
;
753 memset(&info
, 0, sizeof(info
));
754 info
.type
= SNDRV_FIREWIRE_TYPE_DICE
;
755 info
.card
= dev
->card
->index
;
756 *(__be32
*)&info
.guid
[0] = cpu_to_be32(dev
->config_rom
[3]);
757 *(__be32
*)&info
.guid
[4] = cpu_to_be32(dev
->config_rom
[4]);
758 strlcpy(info
.device_name
, dev_name(&dev
->device
),
759 sizeof(info
.device_name
));
761 if (copy_to_user(arg
, &info
, sizeof(info
)))
767 static int dice_hwdep_lock(struct dice
*dice
)
771 spin_lock_irq(&dice
->lock
);
773 if (dice
->dev_lock_count
== 0) {
774 dice
->dev_lock_count
= -1;
780 spin_unlock_irq(&dice
->lock
);
785 static int dice_hwdep_unlock(struct dice
*dice
)
789 spin_lock_irq(&dice
->lock
);
791 if (dice
->dev_lock_count
== -1) {
792 dice
->dev_lock_count
= 0;
798 spin_unlock_irq(&dice
->lock
);
803 static int dice_hwdep_release(struct snd_hwdep
*hwdep
, struct file
*file
)
805 struct dice
*dice
= hwdep
->private_data
;
807 spin_lock_irq(&dice
->lock
);
808 if (dice
->dev_lock_count
== -1)
809 dice
->dev_lock_count
= 0;
810 spin_unlock_irq(&dice
->lock
);
815 static int dice_hwdep_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
816 unsigned int cmd
, unsigned long arg
)
818 struct dice
*dice
= hwdep
->private_data
;
821 case SNDRV_FIREWIRE_IOCTL_GET_INFO
:
822 return dice_hwdep_get_info(dice
, (void __user
*)arg
);
823 case SNDRV_FIREWIRE_IOCTL_LOCK
:
824 return dice_hwdep_lock(dice
);
825 case SNDRV_FIREWIRE_IOCTL_UNLOCK
:
826 return dice_hwdep_unlock(dice
);
833 static int dice_hwdep_compat_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
834 unsigned int cmd
, unsigned long arg
)
836 return dice_hwdep_ioctl(hwdep
, file
, cmd
,
837 (unsigned long)compat_ptr(arg
));
840 #define dice_hwdep_compat_ioctl NULL
843 static int dice_create_hwdep(struct dice
*dice
)
845 static const struct snd_hwdep_ops ops
= {
846 .read
= dice_hwdep_read
,
847 .release
= dice_hwdep_release
,
848 .poll
= dice_hwdep_poll
,
849 .ioctl
= dice_hwdep_ioctl
,
850 .ioctl_compat
= dice_hwdep_compat_ioctl
,
852 struct snd_hwdep
*hwdep
;
855 err
= snd_hwdep_new(dice
->card
, "DICE", 0, &hwdep
);
858 strcpy(hwdep
->name
, "DICE");
859 hwdep
->iface
= SNDRV_HWDEP_IFACE_FW_DICE
;
861 hwdep
->private_data
= dice
;
862 hwdep
->exclusive
= true;
867 static int dice_proc_read_mem(struct dice
*dice
, void *buffer
,
868 unsigned int offset_q
, unsigned int quadlets
)
873 err
= snd_fw_transaction(dice
->unit
, TCODE_READ_BLOCK_REQUEST
,
874 DICE_PRIVATE_SPACE
+ 4 * offset_q
,
875 buffer
, 4 * quadlets
, 0);
879 for (i
= 0; i
< quadlets
; ++i
)
880 be32_to_cpus(&((u32
*)buffer
)[i
]);
885 static const char *str_from_array(const char *const strs
[], unsigned int count
,
894 static void dice_proc_fixup_string(char *s
, unsigned int size
)
898 for (i
= 0; i
< size
; i
+= 4)
899 cpu_to_le32s((u32
*)(s
+ i
));
901 for (i
= 0; i
< size
- 2; ++i
) {
904 if (s
[i
] == '\\' && s
[i
+ 1] == '\\') {
912 static void dice_proc_read(struct snd_info_entry
*entry
,
913 struct snd_info_buffer
*buffer
)
915 static const char *const section_names
[5] = {
916 "global", "tx", "rx", "ext_sync", "unused2"
918 static const char *const clock_sources
[] = {
919 "aes1", "aes2", "aes3", "aes4", "aes", "adat", "tdif",
920 "wc", "arx1", "arx2", "arx3", "arx4", "internal"
922 static const char *const rates
[] = {
923 "32000", "44100", "48000", "88200", "96000", "176400", "192000",
924 "any low", "any mid", "any high", "none"
926 struct dice
*dice
= entry
->private_data
;
927 u32 sections
[ARRAY_SIZE(section_names
) * 2];
934 u32 owner_hi
, owner_lo
;
936 char nick_name
[NICK_NAME_SIZE
];
944 char clock_source_names
[CLOCK_SOURCE_NAMES_SIZE
];
951 char names
[TX_NAMES_SIZE
];
960 char names
[RX_NAMES_SIZE
];
971 unsigned int quadlets
, stream
, i
;
973 if (dice_proc_read_mem(dice
, sections
, 0, ARRAY_SIZE(sections
)) < 0)
975 snd_iprintf(buffer
, "sections:\n");
976 for (i
= 0; i
< ARRAY_SIZE(section_names
); ++i
)
977 snd_iprintf(buffer
, " %s: offset %u, size %u\n",
979 sections
[i
* 2], sections
[i
* 2 + 1]);
981 quadlets
= min_t(u32
, sections
[1], sizeof(buf
.global
) / 4);
982 if (dice_proc_read_mem(dice
, &buf
.global
, sections
[0], quadlets
) < 0)
984 snd_iprintf(buffer
, "global:\n");
985 snd_iprintf(buffer
, " owner: %04x:%04x%08x\n",
986 buf
.global
.owner_hi
>> 16,
987 buf
.global
.owner_hi
& 0xffff, buf
.global
.owner_lo
);
988 snd_iprintf(buffer
, " notification: %08x\n", buf
.global
.notification
);
989 dice_proc_fixup_string(buf
.global
.nick_name
, NICK_NAME_SIZE
);
990 snd_iprintf(buffer
, " nick name: %s\n", buf
.global
.nick_name
);
991 snd_iprintf(buffer
, " clock select: %s %s\n",
992 str_from_array(clock_sources
, ARRAY_SIZE(clock_sources
),
993 buf
.global
.clock_select
& CLOCK_SOURCE_MASK
),
994 str_from_array(rates
, ARRAY_SIZE(rates
),
995 (buf
.global
.clock_select
& CLOCK_RATE_MASK
)
996 >> CLOCK_RATE_SHIFT
));
997 snd_iprintf(buffer
, " enable: %u\n", buf
.global
.enable
);
998 snd_iprintf(buffer
, " status: %slocked %s\n",
999 buf
.global
.status
& STATUS_SOURCE_LOCKED
? "" : "un",
1000 str_from_array(rates
, ARRAY_SIZE(rates
),
1001 (buf
.global
.status
&
1002 STATUS_NOMINAL_RATE_MASK
)
1003 >> CLOCK_RATE_SHIFT
));
1004 snd_iprintf(buffer
, " ext status: %08x\n", buf
.global
.extended_status
);
1005 snd_iprintf(buffer
, " sample rate: %u\n", buf
.global
.sample_rate
);
1006 snd_iprintf(buffer
, " version: %u.%u.%u.%u\n",
1007 (buf
.global
.version
>> 24) & 0xff,
1008 (buf
.global
.version
>> 16) & 0xff,
1009 (buf
.global
.version
>> 8) & 0xff,
1010 (buf
.global
.version
>> 0) & 0xff);
1011 if (quadlets
>= 90) {
1012 snd_iprintf(buffer
, " clock caps:");
1013 for (i
= 0; i
<= 6; ++i
)
1014 if (buf
.global
.clock_caps
& (1 << i
))
1015 snd_iprintf(buffer
, " %s", rates
[i
]);
1016 for (i
= 0; i
<= 12; ++i
)
1017 if (buf
.global
.clock_caps
& (1 << (16 + i
)))
1018 snd_iprintf(buffer
, " %s", clock_sources
[i
]);
1019 snd_iprintf(buffer
, "\n");
1020 dice_proc_fixup_string(buf
.global
.clock_source_names
,
1021 CLOCK_SOURCE_NAMES_SIZE
);
1022 snd_iprintf(buffer
, " clock source names: %s\n",
1023 buf
.global
.clock_source_names
);
1026 if (dice_proc_read_mem(dice
, &tx_rx_header
, sections
[2], 2) < 0)
1028 quadlets
= min_t(u32
, tx_rx_header
.size
, sizeof(buf
.tx
) / 4);
1029 for (stream
= 0; stream
< tx_rx_header
.number
; ++stream
) {
1030 if (dice_proc_read_mem(dice
, &buf
.tx
, sections
[2] + 2 +
1031 stream
* tx_rx_header
.size
,
1034 snd_iprintf(buffer
, "tx %u:\n", stream
);
1035 snd_iprintf(buffer
, " iso channel: %d\n", (int)buf
.tx
.iso
);
1036 snd_iprintf(buffer
, " audio channels: %u\n",
1037 buf
.tx
.number_audio
);
1038 snd_iprintf(buffer
, " midi ports: %u\n", buf
.tx
.number_midi
);
1039 snd_iprintf(buffer
, " speed: S%u\n", 100u << buf
.tx
.speed
);
1040 if (quadlets
>= 68) {
1041 dice_proc_fixup_string(buf
.tx
.names
, TX_NAMES_SIZE
);
1042 snd_iprintf(buffer
, " names: %s\n", buf
.tx
.names
);
1044 if (quadlets
>= 70) {
1045 snd_iprintf(buffer
, " ac3 caps: %08x\n",
1047 snd_iprintf(buffer
, " ac3 enable: %08x\n",
1052 if (dice_proc_read_mem(dice
, &tx_rx_header
, sections
[4], 2) < 0)
1054 quadlets
= min_t(u32
, tx_rx_header
.size
, sizeof(buf
.rx
) / 4);
1055 for (stream
= 0; stream
< tx_rx_header
.number
; ++stream
) {
1056 if (dice_proc_read_mem(dice
, &buf
.rx
, sections
[4] + 2 +
1057 stream
* tx_rx_header
.size
,
1060 snd_iprintf(buffer
, "rx %u:\n", stream
);
1061 snd_iprintf(buffer
, " iso channel: %d\n", (int)buf
.rx
.iso
);
1062 snd_iprintf(buffer
, " sequence start: %u\n", buf
.rx
.seq_start
);
1063 snd_iprintf(buffer
, " audio channels: %u\n",
1064 buf
.rx
.number_audio
);
1065 snd_iprintf(buffer
, " midi ports: %u\n", buf
.rx
.number_midi
);
1066 if (quadlets
>= 68) {
1067 dice_proc_fixup_string(buf
.rx
.names
, RX_NAMES_SIZE
);
1068 snd_iprintf(buffer
, " names: %s\n", buf
.rx
.names
);
1070 if (quadlets
>= 70) {
1071 snd_iprintf(buffer
, " ac3 caps: %08x\n",
1073 snd_iprintf(buffer
, " ac3 enable: %08x\n",
1078 quadlets
= min_t(u32
, sections
[7], sizeof(buf
.ext_sync
) / 4);
1079 if (quadlets
>= 4) {
1080 if (dice_proc_read_mem(dice
, &buf
.ext_sync
,
1081 sections
[6], 4) < 0)
1083 snd_iprintf(buffer
, "ext status:\n");
1084 snd_iprintf(buffer
, " clock source: %s\n",
1085 str_from_array(clock_sources
,
1086 ARRAY_SIZE(clock_sources
),
1087 buf
.ext_sync
.clock_source
));
1088 snd_iprintf(buffer
, " locked: %u\n", buf
.ext_sync
.locked
);
1089 snd_iprintf(buffer
, " rate: %s\n",
1090 str_from_array(rates
, ARRAY_SIZE(rates
),
1091 buf
.ext_sync
.rate
));
1092 snd_iprintf(buffer
, " adat user data: ");
1093 if (buf
.ext_sync
.adat_user_data
& ADAT_USER_DATA_NO_DATA
)
1094 snd_iprintf(buffer
, "-\n");
1096 snd_iprintf(buffer
, "%x\n",
1097 buf
.ext_sync
.adat_user_data
);
1101 static void dice_create_proc(struct dice
*dice
)
1103 struct snd_info_entry
*entry
;
1105 if (!snd_card_proc_new(dice
->card
, "dice", &entry
))
1106 snd_info_set_text_ops(entry
, dice
, dice_proc_read
);
1109 static void dice_card_free(struct snd_card
*card
)
1111 struct dice
*dice
= card
->private_data
;
1113 amdtp_stream_destroy(&dice
->stream
);
1114 fw_core_remove_address_handler(&dice
->notification_handler
);
1115 mutex_destroy(&dice
->mutex
);
1118 #define OUI_WEISS 0x001c6a
1120 #define DICE_CATEGORY_ID 0x04
1121 #define WEISS_CATEGORY_ID 0x00
1123 static int dice_interface_check(struct fw_unit
*unit
)
1125 static const int min_values
[10] = {
1132 struct fw_device
*device
= fw_parent_device(unit
);
1133 struct fw_csr_iterator it
;
1134 int key
, value
, vendor
= -1, model
= -1, err
;
1135 unsigned int category
, i
;
1136 __be32 pointers
[ARRAY_SIZE(min_values
)];
1141 * Check that GUID and unit directory are constructed according to DICE
1142 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
1143 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
1144 * ID, and a 22-bit serial number.
1146 fw_csr_iterator_init(&it
, unit
->directory
);
1147 while (fw_csr_iterator_next(&it
, &key
, &value
)) {
1149 case CSR_SPECIFIER_ID
:
1157 if (vendor
== OUI_WEISS
)
1158 category
= WEISS_CATEGORY_ID
;
1160 category
= DICE_CATEGORY_ID
;
1161 if (device
->config_rom
[3] != ((vendor
<< 8) | category
) ||
1162 device
->config_rom
[4] >> 22 != model
)
1166 * Check that the sub address spaces exist and are located inside the
1167 * private address space. The minimum values are chosen so that all
1168 * minimally required registers are included.
1170 err
= snd_fw_transaction(unit
, TCODE_READ_BLOCK_REQUEST
,
1172 pointers
, sizeof(pointers
), 0);
1175 for (i
= 0; i
< ARRAY_SIZE(pointers
); ++i
) {
1176 value
= be32_to_cpu(pointers
[i
]);
1177 if (value
< min_values
[i
] || value
>= 0x40000)
1181 /* We support playback only. Let capture devices be handled by FFADO. */
1182 err
= snd_fw_transaction(unit
, TCODE_READ_BLOCK_REQUEST
,
1183 DICE_PRIVATE_SPACE
+
1184 be32_to_cpu(pointers
[2]) * 4,
1185 tx_data
, sizeof(tx_data
), 0);
1186 if (err
< 0 || (tx_data
[0] && tx_data
[3]))
1190 * Check that the implemented DICE driver specification major version
1193 err
= snd_fw_transaction(unit
, TCODE_READ_QUADLET_REQUEST
,
1194 DICE_PRIVATE_SPACE
+
1195 be32_to_cpu(pointers
[0]) * 4 + GLOBAL_VERSION
,
1199 if ((version
& cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
1200 dev_err(&unit
->device
,
1201 "unknown DICE version: 0x%08x\n", be32_to_cpu(version
));
1208 static int highest_supported_mode_rate(struct dice
*dice
, unsigned int mode
)
1212 for (i
= ARRAY_SIZE(dice_rates
) - 1; i
>= 0; --i
)
1213 if ((dice
->clock_caps
& (1 << i
)) &&
1214 rate_index_to_mode(i
) == mode
)
1220 static int dice_read_mode_params(struct dice
*dice
, unsigned int mode
)
1223 int rate_index
, err
;
1225 rate_index
= highest_supported_mode_rate(dice
, mode
);
1226 if (rate_index
< 0) {
1227 dice
->rx_channels
[mode
] = 0;
1228 dice
->rx_midi_ports
[mode
] = 0;
1232 err
= dice_change_rate(dice
, rate_index
<< CLOCK_RATE_SHIFT
);
1236 err
= snd_fw_transaction(dice
->unit
, TCODE_READ_BLOCK_REQUEST
,
1237 rx_address(dice
, RX_NUMBER_AUDIO
),
1242 dice
->rx_channels
[mode
] = be32_to_cpu(values
[0]);
1243 dice
->rx_midi_ports
[mode
] = be32_to_cpu(values
[1]);
1248 static int dice_read_params(struct dice
*dice
)
1254 err
= snd_fw_transaction(dice
->unit
, TCODE_READ_BLOCK_REQUEST
,
1256 pointers
, sizeof(pointers
), 0);
1260 dice
->global_offset
= be32_to_cpu(pointers
[0]) * 4;
1261 dice
->rx_offset
= be32_to_cpu(pointers
[4]) * 4;
1263 /* some very old firmwares don't tell about their clock support */
1264 if (be32_to_cpu(pointers
[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES
+ 4) {
1265 err
= snd_fw_transaction(
1266 dice
->unit
, TCODE_READ_QUADLET_REQUEST
,
1267 global_address(dice
, GLOBAL_CLOCK_CAPABILITIES
),
1271 dice
->clock_caps
= be32_to_cpu(value
);
1273 /* this should be supported by any device */
1274 dice
->clock_caps
= CLOCK_CAP_RATE_44100
|
1275 CLOCK_CAP_RATE_48000
|
1276 CLOCK_CAP_SOURCE_ARX1
|
1277 CLOCK_CAP_SOURCE_INTERNAL
;
1280 for (mode
= 2; mode
>= 0; --mode
) {
1281 err
= dice_read_mode_params(dice
, mode
);
1289 static void dice_card_strings(struct dice
*dice
)
1291 struct snd_card
*card
= dice
->card
;
1292 struct fw_device
*dev
= fw_parent_device(dice
->unit
);
1293 char vendor
[32], model
[32];
1297 strcpy(card
->driver
, "DICE");
1299 strcpy(card
->shortname
, "DICE");
1300 BUILD_BUG_ON(NICK_NAME_SIZE
< sizeof(card
->shortname
));
1301 err
= snd_fw_transaction(dice
->unit
, TCODE_READ_BLOCK_REQUEST
,
1302 global_address(dice
, GLOBAL_NICK_NAME
),
1303 card
->shortname
, sizeof(card
->shortname
), 0);
1305 /* DICE strings are returned in "always-wrong" endianness */
1306 BUILD_BUG_ON(sizeof(card
->shortname
) % 4 != 0);
1307 for (i
= 0; i
< sizeof(card
->shortname
); i
+= 4)
1308 swab32s((u32
*)&card
->shortname
[i
]);
1309 card
->shortname
[sizeof(card
->shortname
) - 1] = '\0';
1312 strcpy(vendor
, "?");
1313 fw_csr_string(dev
->config_rom
+ 5, CSR_VENDOR
, vendor
, sizeof(vendor
));
1315 fw_csr_string(dice
->unit
->directory
, CSR_MODEL
, model
, sizeof(model
));
1316 snprintf(card
->longname
, sizeof(card
->longname
),
1317 "%s %s (serial %u) at %s, S%d",
1318 vendor
, model
, dev
->config_rom
[4] & 0x3fffff,
1319 dev_name(&dice
->unit
->device
), 100 << dev
->max_speed
);
1321 strcpy(card
->mixername
, "DICE");
1324 static int dice_probe(struct fw_unit
*unit
, const struct ieee1394_device_id
*id
)
1326 struct snd_card
*card
;
1331 err
= dice_interface_check(unit
);
1335 err
= snd_card_new(&unit
->device
, -1, NULL
, THIS_MODULE
,
1336 sizeof(*dice
), &card
);
1340 dice
= card
->private_data
;
1342 spin_lock_init(&dice
->lock
);
1343 mutex_init(&dice
->mutex
);
1345 init_completion(&dice
->clock_accepted
);
1346 init_waitqueue_head(&dice
->hwdep_wait
);
1348 dice
->notification_handler
.length
= 4;
1349 dice
->notification_handler
.address_callback
= dice_notification
;
1350 dice
->notification_handler
.callback_data
= dice
;
1351 err
= fw_core_add_address_handler(&dice
->notification_handler
,
1352 &fw_high_memory_region
);
1356 err
= dice_owner_set(dice
);
1358 goto err_notification_handler
;
1360 err
= dice_read_params(dice
);
1364 err
= fw_iso_resources_init(&dice
->resources
, unit
);
1367 dice
->resources
.channels_mask
= 0x00000000ffffffffuLL
;
1369 err
= amdtp_stream_init(&dice
->stream
, unit
, AMDTP_OUT_STREAM
,
1374 card
->private_free
= dice_card_free
;
1376 dice_card_strings(dice
);
1378 err
= snd_fw_transaction(unit
, TCODE_READ_QUADLET_REQUEST
,
1379 global_address(dice
, GLOBAL_CLOCK_SELECT
),
1383 clock_sel
&= cpu_to_be32(~CLOCK_SOURCE_MASK
);
1384 clock_sel
|= cpu_to_be32(CLOCK_SOURCE_ARX1
);
1385 err
= snd_fw_transaction(unit
, TCODE_WRITE_QUADLET_REQUEST
,
1386 global_address(dice
, GLOBAL_CLOCK_SELECT
),
1391 err
= dice_create_pcm(dice
);
1395 err
= dice_create_hwdep(dice
);
1399 dice_create_proc(dice
);
1401 err
= snd_card_register(card
);
1405 dev_set_drvdata(&unit
->device
, dice
);
1410 fw_iso_resources_destroy(&dice
->resources
);
1412 dice_owner_clear(dice
);
1413 err_notification_handler
:
1414 fw_core_remove_address_handler(&dice
->notification_handler
);
1416 mutex_destroy(&dice
->mutex
);
1418 snd_card_free(card
);
1422 static void dice_remove(struct fw_unit
*unit
)
1424 struct dice
*dice
= dev_get_drvdata(&unit
->device
);
1426 amdtp_stream_pcm_abort(&dice
->stream
);
1428 snd_card_disconnect(dice
->card
);
1430 mutex_lock(&dice
->mutex
);
1432 dice_stream_stop(dice
);
1433 dice_owner_clear(dice
);
1435 mutex_unlock(&dice
->mutex
);
1437 snd_card_free_when_closed(dice
->card
);
1440 static void dice_bus_reset(struct fw_unit
*unit
)
1442 struct dice
*dice
= dev_get_drvdata(&unit
->device
);
1445 * On a bus reset, the DICE firmware disables streaming and then goes
1446 * off contemplating its own navel for hundreds of milliseconds before
1447 * it can react to any of our attempts to reenable streaming. This
1448 * means that we lose synchronization anyway, so we force our streams
1449 * to stop so that the application can restart them in an orderly
1452 amdtp_stream_pcm_abort(&dice
->stream
);
1454 mutex_lock(&dice
->mutex
);
1456 dice
->global_enabled
= false;
1457 dice_stream_stop_packets(dice
);
1459 dice_owner_update(dice
);
1461 fw_iso_resources_update(&dice
->resources
);
1463 mutex_unlock(&dice
->mutex
);
1466 #define DICE_INTERFACE 0x000001
1468 static const struct ieee1394_device_id dice_id_table
[] = {
1470 .match_flags
= IEEE1394_MATCH_VERSION
,
1471 .version
= DICE_INTERFACE
,
1475 MODULE_DEVICE_TABLE(ieee1394
, dice_id_table
);
1477 static struct fw_driver dice_driver
= {
1479 .owner
= THIS_MODULE
,
1480 .name
= KBUILD_MODNAME
,
1481 .bus
= &fw_bus_type
,
1483 .probe
= dice_probe
,
1484 .update
= dice_bus_reset
,
1485 .remove
= dice_remove
,
1486 .id_table
= dice_id_table
,
1489 static int __init
alsa_dice_init(void)
1491 return driver_register(&dice_driver
.driver
);
1494 static void __exit
alsa_dice_exit(void)
1496 driver_unregister(&dice_driver
.driver
);
1499 module_init(alsa_dice_init
);
1500 module_exit(alsa_dice_exit
);