1 // SPDX-License-Identifier: GPL-2.0-only
3 * ff-hwdep.c - a part of driver for RME Fireface series
5 * Copyright (c) 2015-2017 Takashi Sakamoto
9 * This codes give three functionality.
11 * 1.get firewire node information
12 * 2.get notification about starting/stopping stream
13 * 3.lock/unlock stream
18 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
21 struct snd_ff *ff = hwdep->private_data;
23 union snd_firewire_event event;
25 spin_lock_irq(&ff->lock);
27 while (!ff->dev_lock_changed) {
28 prepare_to_wait(&ff->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
29 spin_unlock_irq(&ff->lock);
31 finish_wait(&ff->hwdep_wait, &wait);
32 if (signal_pending(current))
34 spin_lock_irq(&ff->lock);
37 memset(&event, 0, sizeof(event));
38 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
39 event.lock_status.status = (ff->dev_lock_count > 0);
40 ff->dev_lock_changed = false;
42 count = min_t(long, count, sizeof(event.lock_status));
44 spin_unlock_irq(&ff->lock);
46 if (copy_to_user(buf, &event, count))
52 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
55 struct snd_ff *ff = hwdep->private_data;
58 poll_wait(file, &ff->hwdep_wait, wait);
60 spin_lock_irq(&ff->lock);
61 if (ff->dev_lock_changed)
62 events = EPOLLIN | EPOLLRDNORM;
65 spin_unlock_irq(&ff->lock);
70 static int hwdep_get_info(struct snd_ff *ff, void __user *arg)
72 struct fw_device *dev = fw_parent_device(ff->unit);
73 struct snd_firewire_get_info info;
75 memset(&info, 0, sizeof(info));
76 info.type = SNDRV_FIREWIRE_TYPE_FIREFACE;
77 info.card = dev->card->index;
78 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
79 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
80 strscpy(info.device_name, dev_name(&dev->device),
81 sizeof(info.device_name));
83 if (copy_to_user(arg, &info, sizeof(info)))
89 static int hwdep_lock(struct snd_ff *ff)
93 spin_lock_irq(&ff->lock);
95 if (ff->dev_lock_count == 0) {
96 ff->dev_lock_count = -1;
102 spin_unlock_irq(&ff->lock);
107 static int hwdep_unlock(struct snd_ff *ff)
111 spin_lock_irq(&ff->lock);
113 if (ff->dev_lock_count == -1) {
114 ff->dev_lock_count = 0;
120 spin_unlock_irq(&ff->lock);
125 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
127 struct snd_ff *ff = hwdep->private_data;
129 spin_lock_irq(&ff->lock);
130 if (ff->dev_lock_count == -1)
131 ff->dev_lock_count = 0;
132 spin_unlock_irq(&ff->lock);
137 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
138 unsigned int cmd, unsigned long arg)
140 struct snd_ff *ff = hwdep->private_data;
143 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
144 return hwdep_get_info(ff, (void __user *)arg);
145 case SNDRV_FIREWIRE_IOCTL_LOCK:
146 return hwdep_lock(ff);
147 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
148 return hwdep_unlock(ff);
155 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
156 unsigned int cmd, unsigned long arg)
158 return hwdep_ioctl(hwdep, file, cmd,
159 (unsigned long)compat_ptr(arg));
162 #define hwdep_compat_ioctl NULL
165 int snd_ff_create_hwdep_devices(struct snd_ff *ff)
167 static const struct snd_hwdep_ops hwdep_ops = {
169 .release = hwdep_release,
171 .ioctl = hwdep_ioctl,
172 .ioctl_compat = hwdep_compat_ioctl,
174 struct snd_hwdep *hwdep;
177 err = snd_hwdep_new(ff->card, ff->card->driver, 0, &hwdep);
181 strcpy(hwdep->name, ff->card->driver);
182 hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREFACE;
183 hwdep->ops = hwdep_ops;
184 hwdep->private_data = ff;
185 hwdep->exclusive = true;