^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) * bebob_hwdep.c - a part of driver for BeBoB based devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2013-2014 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 infomation
^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) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "bebob.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) 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_bebob *bebob = 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(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) while (!bebob->dev_lock_changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) prepare_to_wait(&bebob->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) spin_unlock_irq(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) finish_wait(&bebob->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(&bebob->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) count = min_t(long, count, sizeof(event.lock_status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (bebob->dev_lock_changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) event.lock_status.status = (bebob->dev_lock_count > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) bebob->dev_lock_changed = false;
^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(&bebob->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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) hwdep_poll(struct snd_hwdep *hwdep, struct file *file, 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_bebob *bebob = 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, &bebob->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(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (bebob->dev_lock_changed)
^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(&bebob->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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) hwdep_get_info(struct snd_bebob *bebob, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct fw_device *dev = fw_parent_device(bebob->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct snd_firewire_get_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) memset(&info, 0, sizeof(info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) info.type = SNDRV_FIREWIRE_TYPE_BEBOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) info.card = dev->card->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) strlcpy(info.device_name, dev_name(&dev->device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) sizeof(info.device_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (copy_to_user(arg, &info, sizeof(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) hwdep_lock(struct snd_bebob *bebob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) spin_lock_irq(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (bebob->dev_lock_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) bebob->dev_lock_count = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) spin_unlock_irq(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return err;
^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) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) hwdep_unlock(struct snd_bebob *bebob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) spin_lock_irq(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (bebob->dev_lock_count == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) bebob->dev_lock_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) err = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) spin_unlock_irq(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return err;
^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) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) hwdep_release(struct snd_hwdep *hwdep, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct snd_bebob *bebob = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) spin_lock_irq(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (bebob->dev_lock_count == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) bebob->dev_lock_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) spin_unlock_irq(&bebob->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct snd_bebob *bebob = hwdep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) case SNDRV_FIREWIRE_IOCTL_GET_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return hwdep_get_info(bebob, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) case SNDRV_FIREWIRE_IOCTL_LOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return hwdep_lock(bebob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case SNDRV_FIREWIRE_IOCTL_UNLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return hwdep_unlock(bebob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return hwdep_ioctl(hwdep, file, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define hwdep_compat_ioctl NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int snd_bebob_create_hwdep_device(struct snd_bebob *bebob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static const struct snd_hwdep_ops ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .read = hwdep_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .release = hwdep_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .poll = hwdep_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .ioctl = hwdep_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .ioctl_compat = hwdep_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct snd_hwdep *hwdep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err = snd_hwdep_new(bebob->card, "BeBoB", 0, &hwdep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) strcpy(hwdep->name, "BeBoB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) hwdep->iface = SNDRV_HWDEP_IFACE_FW_BEBOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) hwdep->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) hwdep->private_data = bebob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) hwdep->exclusive = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)