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