Linux 4.16.11
[linux/fpc-iii.git] / sound / firewire / fireface / ff-hwdep.c
blob336c0076ec42150c9b936dd66d326e8a1430a089
1 /*
2 * ff-hwdep.c - a part of driver for RME Fireface series
4 * Copyright (c) 2015-2017 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
9 /*
10 * This codes give three functionality.
12 * 1.get firewire node information
13 * 2.get notification about starting/stopping stream
14 * 3.lock/unlock stream
17 #include "ff.h"
19 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
20 loff_t *offset)
22 struct snd_ff *ff = hwdep->private_data;
23 DEFINE_WAIT(wait);
24 union snd_firewire_event event;
26 spin_lock_irq(&ff->lock);
28 while (!ff->dev_lock_changed) {
29 prepare_to_wait(&ff->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
30 spin_unlock_irq(&ff->lock);
31 schedule();
32 finish_wait(&ff->hwdep_wait, &wait);
33 if (signal_pending(current))
34 return -ERESTARTSYS;
35 spin_lock_irq(&ff->lock);
38 memset(&event, 0, sizeof(event));
39 if (ff->dev_lock_changed) {
40 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
41 event.lock_status.status = (ff->dev_lock_count > 0);
42 ff->dev_lock_changed = false;
44 count = min_t(long, count, sizeof(event.lock_status));
47 spin_unlock_irq(&ff->lock);
49 if (copy_to_user(buf, &event, count))
50 return -EFAULT;
52 return count;
55 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
56 poll_table *wait)
58 struct snd_ff *ff = hwdep->private_data;
59 __poll_t events;
61 poll_wait(file, &ff->hwdep_wait, wait);
63 spin_lock_irq(&ff->lock);
64 if (ff->dev_lock_changed)
65 events = EPOLLIN | EPOLLRDNORM;
66 else
67 events = 0;
68 spin_unlock_irq(&ff->lock);
70 return events;
73 static int hwdep_get_info(struct snd_ff *ff, void __user *arg)
75 struct fw_device *dev = fw_parent_device(ff->unit);
76 struct snd_firewire_get_info info;
78 memset(&info, 0, sizeof(info));
79 info.type = SNDRV_FIREWIRE_TYPE_FIREFACE;
80 info.card = dev->card->index;
81 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
82 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
83 strlcpy(info.device_name, dev_name(&dev->device),
84 sizeof(info.device_name));
86 if (copy_to_user(arg, &info, sizeof(info)))
87 return -EFAULT;
89 return 0;
92 static int hwdep_lock(struct snd_ff *ff)
94 int err;
96 spin_lock_irq(&ff->lock);
98 if (ff->dev_lock_count == 0) {
99 ff->dev_lock_count = -1;
100 err = 0;
101 } else {
102 err = -EBUSY;
105 spin_unlock_irq(&ff->lock);
107 return err;
110 static int hwdep_unlock(struct snd_ff *ff)
112 int err;
114 spin_lock_irq(&ff->lock);
116 if (ff->dev_lock_count == -1) {
117 ff->dev_lock_count = 0;
118 err = 0;
119 } else {
120 err = -EBADFD;
123 spin_unlock_irq(&ff->lock);
125 return err;
128 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
130 struct snd_ff *ff = hwdep->private_data;
132 spin_lock_irq(&ff->lock);
133 if (ff->dev_lock_count == -1)
134 ff->dev_lock_count = 0;
135 spin_unlock_irq(&ff->lock);
137 return 0;
140 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
141 unsigned int cmd, unsigned long arg)
143 struct snd_ff *ff = hwdep->private_data;
145 switch (cmd) {
146 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
147 return hwdep_get_info(ff, (void __user *)arg);
148 case SNDRV_FIREWIRE_IOCTL_LOCK:
149 return hwdep_lock(ff);
150 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
151 return hwdep_unlock(ff);
152 default:
153 return -ENOIOCTLCMD;
157 #ifdef CONFIG_COMPAT
158 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
159 unsigned int cmd, unsigned long arg)
161 return hwdep_ioctl(hwdep, file, cmd,
162 (unsigned long)compat_ptr(arg));
164 #else
165 #define hwdep_compat_ioctl NULL
166 #endif
168 int snd_ff_create_hwdep_devices(struct snd_ff *ff)
170 static const struct snd_hwdep_ops hwdep_ops = {
171 .read = hwdep_read,
172 .release = hwdep_release,
173 .poll = hwdep_poll,
174 .ioctl = hwdep_ioctl,
175 .ioctl_compat = hwdep_compat_ioctl,
177 struct snd_hwdep *hwdep;
178 int err;
180 err = snd_hwdep_new(ff->card, ff->card->driver, 0, &hwdep);
181 if (err < 0)
182 return err;
184 strcpy(hwdep->name, ff->card->driver);
185 hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREFACE;
186 hwdep->ops = hwdep_ops;
187 hwdep->private_data = ff;
188 hwdep->exclusive = true;
190 return 0;