^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * dice_hwdep.c - a part of driver for DICE based devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "dice.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) long count, loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct snd_dice *dice = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) union snd_firewire_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) spin_lock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) while (!dice->dev_lock_changed && dice->notification_bits == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) spin_unlock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) finish_wait(&dice->hwdep_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) spin_lock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) memset(&event, 0, sizeof(event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (dice->dev_lock_changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) event.lock_status.status = dice->dev_lock_count > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) dice->dev_lock_changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) count = min_t(long, count, sizeof(event.lock_status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) event.dice_notification.type =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) event.dice_notification.notification = dice->notification_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) dice->notification_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) count = min_t(long, count, sizeof(event.dice_notification));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) spin_unlock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (copy_to_user(buf, &event, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct snd_dice *dice = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) __poll_t events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) poll_wait(file, &dice->hwdep_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) spin_lock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (dice->dev_lock_changed || dice->notification_bits != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) events = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) spin_unlock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int hwdep_get_info(struct snd_dice *dice, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct fw_device *dev = fw_parent_device(dice->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct snd_firewire_get_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) memset(&info, 0, sizeof(info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) info.type = SNDRV_FIREWIRE_TYPE_DICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) info.card = dev->card->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) strlcpy(info.device_name, dev_name(&dev->device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) sizeof(info.device_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (copy_to_user(arg, &info, sizeof(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static int hwdep_lock(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) spin_lock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (dice->dev_lock_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) dice->dev_lock_count = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) spin_unlock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int hwdep_unlock(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) spin_lock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (dice->dev_lock_count == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dice->dev_lock_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) err = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) spin_unlock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct snd_dice *dice = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) spin_lock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (dice->dev_lock_count == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) dice->dev_lock_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) spin_unlock_irq(&dice->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct snd_dice *dice = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) case SNDRV_FIREWIRE_IOCTL_GET_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return hwdep_get_info(dice, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) case SNDRV_FIREWIRE_IOCTL_LOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return hwdep_lock(dice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) case SNDRV_FIREWIRE_IOCTL_UNLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return hwdep_unlock(dice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return hwdep_ioctl(hwdep, file, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define hwdep_compat_ioctl NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int snd_dice_create_hwdep(struct snd_dice *dice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static const struct snd_hwdep_ops ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .read = hwdep_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .release = hwdep_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .poll = hwdep_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .ioctl = hwdep_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .ioctl_compat = hwdep_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct snd_hwdep *hwdep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) strcpy(hwdep->name, "DICE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) hwdep->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) hwdep->private_data = dice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) hwdep->exclusive = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }