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 bool has_msg(struct snd_ff *ff)
20 if (ff->spec->protocol->has_msg)
21 return ff->spec->protocol->has_msg(ff);
26 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
29 struct snd_ff *ff = hwdep->private_data;
32 spin_lock_irq(&ff->lock);
34 while (!ff->dev_lock_changed && !has_msg(ff)) {
35 prepare_to_wait(&ff->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
36 spin_unlock_irq(&ff->lock);
38 finish_wait(&ff->hwdep_wait, &wait);
39 if (signal_pending(current))
41 spin_lock_irq(&ff->lock);
44 if (ff->dev_lock_changed && count >= sizeof(struct snd_firewire_event_lock_status)) {
45 struct snd_firewire_event_lock_status ev = {
46 .type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
47 .status = (ff->dev_lock_count > 0),
50 ff->dev_lock_changed = false;
52 spin_unlock_irq(&ff->lock);
54 if (copy_to_user(buf, &ev, sizeof(ev)))
57 } else if (has_msg(ff)) {
58 // NOTE: Acquired spin lock should be released before accessing to user space in the
59 // callback since the access can cause page fault.
60 count = ff->spec->protocol->copy_msg_to_user(ff, buf, count);
61 spin_unlock_irq(&ff->lock);
63 spin_unlock_irq(&ff->lock);
71 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
74 struct snd_ff *ff = hwdep->private_data;
77 poll_wait(file, &ff->hwdep_wait, wait);
79 spin_lock_irq(&ff->lock);
80 if (ff->dev_lock_changed || has_msg(ff))
81 events = EPOLLIN | EPOLLRDNORM;
84 spin_unlock_irq(&ff->lock);
89 static int hwdep_get_info(struct snd_ff *ff, void __user *arg)
91 struct fw_device *dev = fw_parent_device(ff->unit);
92 struct snd_firewire_get_info info;
94 memset(&info, 0, sizeof(info));
95 info.type = SNDRV_FIREWIRE_TYPE_FIREFACE;
96 info.card = dev->card->index;
97 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
98 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
99 strscpy(info.device_name, dev_name(&dev->device),
100 sizeof(info.device_name));
102 if (copy_to_user(arg, &info, sizeof(info)))
108 static int hwdep_lock(struct snd_ff *ff)
112 spin_lock_irq(&ff->lock);
114 if (ff->dev_lock_count == 0) {
115 ff->dev_lock_count = -1;
121 spin_unlock_irq(&ff->lock);
126 static int hwdep_unlock(struct snd_ff *ff)
130 spin_lock_irq(&ff->lock);
132 if (ff->dev_lock_count == -1) {
133 ff->dev_lock_count = 0;
139 spin_unlock_irq(&ff->lock);
144 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
146 struct snd_ff *ff = hwdep->private_data;
148 spin_lock_irq(&ff->lock);
149 if (ff->dev_lock_count == -1)
150 ff->dev_lock_count = 0;
151 spin_unlock_irq(&ff->lock);
156 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
157 unsigned int cmd, unsigned long arg)
159 struct snd_ff *ff = hwdep->private_data;
162 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
163 return hwdep_get_info(ff, (void __user *)arg);
164 case SNDRV_FIREWIRE_IOCTL_LOCK:
165 return hwdep_lock(ff);
166 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
167 return hwdep_unlock(ff);
174 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
175 unsigned int cmd, unsigned long arg)
177 return hwdep_ioctl(hwdep, file, cmd,
178 (unsigned long)compat_ptr(arg));
181 #define hwdep_compat_ioctl NULL
184 int snd_ff_create_hwdep_devices(struct snd_ff *ff)
186 static const struct snd_hwdep_ops hwdep_ops = {
188 .release = hwdep_release,
190 .ioctl = hwdep_ioctl,
191 .ioctl_compat = hwdep_compat_ioctl,
193 struct snd_hwdep *hwdep;
196 err = snd_hwdep_new(ff->card, ff->card->driver, 0, &hwdep);
200 strcpy(hwdep->name, ff->card->driver);
201 hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREFACE;
202 hwdep->ops = hwdep_ops;
203 hwdep->private_data = ff;
204 hwdep->exclusive = true;