^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) * motu-hwdep.c - a part of driver for MOTU FireWire series
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
^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 have five functionalities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * 1.get information about firewire node
^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 streaming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^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 "motu.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_motu *motu = 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(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) while (!motu->dev_lock_changed && motu->msg == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) prepare_to_wait(&motu->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) spin_unlock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) finish_wait(&motu->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(&motu->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 (motu->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 = (motu->dev_lock_count > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) motu->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.motu_notification.type = SNDRV_FIREWIRE_EVENT_MOTU_NOTIFICATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) event.motu_notification.message = motu->msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) motu->msg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) count = min_t(long, count, sizeof(event.motu_notification));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) spin_unlock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (copy_to_user(buf, &event, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct snd_motu *motu = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) __poll_t events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) poll_wait(file, &motu->hwdep_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) spin_lock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (motu->dev_lock_changed || motu->msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) events = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) spin_unlock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return events | EPOLLOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int hwdep_get_info(struct snd_motu *motu, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct fw_device *dev = fw_parent_device(motu->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct snd_firewire_get_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) memset(&info, 0, sizeof(info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) info.type = SNDRV_FIREWIRE_TYPE_MOTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) info.card = dev->card->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) strlcpy(info.device_name, dev_name(&dev->device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sizeof(info.device_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (copy_to_user(arg, &info, sizeof(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int hwdep_lock(struct snd_motu *motu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) spin_lock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (motu->dev_lock_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) motu->dev_lock_count = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) spin_unlock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int hwdep_unlock(struct snd_motu *motu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) spin_lock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (motu->dev_lock_count == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) motu->dev_lock_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) spin_unlock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct snd_motu *motu = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) spin_lock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (motu->dev_lock_count == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) motu->dev_lock_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) spin_unlock_irq(&motu->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct snd_motu *motu = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) case SNDRV_FIREWIRE_IOCTL_GET_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return hwdep_get_info(motu, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case SNDRV_FIREWIRE_IOCTL_LOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return hwdep_lock(motu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) case SNDRV_FIREWIRE_IOCTL_UNLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return hwdep_unlock(motu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^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) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return hwdep_ioctl(hwdep, file, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #define hwdep_compat_ioctl NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int snd_motu_create_hwdep_device(struct snd_motu *motu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static const struct snd_hwdep_ops ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .read = hwdep_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .release = hwdep_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .poll = hwdep_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .ioctl = hwdep_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .ioctl_compat = hwdep_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct snd_hwdep *hwdep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) err = snd_hwdep_new(motu->card, motu->card->driver, 0, &hwdep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) strcpy(hwdep->name, "MOTU");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) hwdep->iface = SNDRV_HWDEP_IFACE_FW_MOTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) hwdep->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) hwdep->private_data = motu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) hwdep->exclusive = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }