Linux 4.18.10
[linux/fpc-iii.git] / sound / firewire / dice / dice-hwdep.c
blob6498bf6909ba907499cd269f198656c8952a5c5a
1 /*
2 * dice_hwdep.c - a part of driver for DICE based devices
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
10 #include "dice.h"
12 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
13 long count, loff_t *offset)
15 struct snd_dice *dice = hwdep->private_data;
16 DEFINE_WAIT(wait);
17 union snd_firewire_event event;
19 spin_lock_irq(&dice->lock);
21 while (!dice->dev_lock_changed && dice->notification_bits == 0) {
22 prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
23 spin_unlock_irq(&dice->lock);
24 schedule();
25 finish_wait(&dice->hwdep_wait, &wait);
26 if (signal_pending(current))
27 return -ERESTARTSYS;
28 spin_lock_irq(&dice->lock);
31 memset(&event, 0, sizeof(event));
32 if (dice->dev_lock_changed) {
33 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
34 event.lock_status.status = dice->dev_lock_count > 0;
35 dice->dev_lock_changed = false;
37 count = min_t(long, count, sizeof(event.lock_status));
38 } else {
39 event.dice_notification.type =
40 SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
41 event.dice_notification.notification = dice->notification_bits;
42 dice->notification_bits = 0;
44 count = min_t(long, count, sizeof(event.dice_notification));
47 spin_unlock_irq(&dice->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_dice *dice = hwdep->private_data;
59 __poll_t events;
61 poll_wait(file, &dice->hwdep_wait, wait);
63 spin_lock_irq(&dice->lock);
64 if (dice->dev_lock_changed || dice->notification_bits != 0)
65 events = EPOLLIN | EPOLLRDNORM;
66 else
67 events = 0;
68 spin_unlock_irq(&dice->lock);
70 return events;
73 static int hwdep_get_info(struct snd_dice *dice, void __user *arg)
75 struct fw_device *dev = fw_parent_device(dice->unit);
76 struct snd_firewire_get_info info;
78 memset(&info, 0, sizeof(info));
79 info.type = SNDRV_FIREWIRE_TYPE_DICE;
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_dice *dice)
94 int err;
96 spin_lock_irq(&dice->lock);
98 if (dice->dev_lock_count == 0) {
99 dice->dev_lock_count = -1;
100 err = 0;
101 } else {
102 err = -EBUSY;
105 spin_unlock_irq(&dice->lock);
107 return err;
110 static int hwdep_unlock(struct snd_dice *dice)
112 int err;
114 spin_lock_irq(&dice->lock);
116 if (dice->dev_lock_count == -1) {
117 dice->dev_lock_count = 0;
118 err = 0;
119 } else {
120 err = -EBADFD;
123 spin_unlock_irq(&dice->lock);
125 return err;
128 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
130 struct snd_dice *dice = hwdep->private_data;
132 spin_lock_irq(&dice->lock);
133 if (dice->dev_lock_count == -1)
134 dice->dev_lock_count = 0;
135 spin_unlock_irq(&dice->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_dice *dice = hwdep->private_data;
145 switch (cmd) {
146 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
147 return hwdep_get_info(dice, (void __user *)arg);
148 case SNDRV_FIREWIRE_IOCTL_LOCK:
149 return hwdep_lock(dice);
150 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
151 return hwdep_unlock(dice);
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_dice_create_hwdep(struct snd_dice *dice)
170 static const struct snd_hwdep_ops 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(dice->card, "DICE", 0, &hwdep);
181 if (err < 0)
182 return err;
183 strcpy(hwdep->name, "DICE");
184 hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
185 hwdep->ops = ops;
186 hwdep->private_data = dice;
187 hwdep->exclusive = true;
189 return 0;