Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/firewire/motu/motu-hwdep.c
29266 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
* motu-hwdep.c - a part of driver for MOTU FireWire series
4
*
5
* Copyright (c) 2015-2017 Takashi Sakamoto <[email protected]>
6
*/
7
8
/*
9
* This codes have five functionalities.
10
*
11
* 1.get information about firewire node
12
* 2.get notification about starting/stopping stream
13
* 3.lock/unlock streaming
14
*
15
*/
16
17
#include "motu.h"
18
19
static bool has_dsp_event(struct snd_motu *motu)
20
{
21
if (motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP)
22
return (snd_motu_register_dsp_message_parser_count_event(motu) > 0);
23
else
24
return false;
25
}
26
27
static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
28
loff_t *offset)
29
{
30
struct snd_motu *motu = hwdep->private_data;
31
DEFINE_WAIT(wait);
32
union snd_firewire_event event;
33
34
spin_lock_irq(&motu->lock);
35
36
while (!motu->dev_lock_changed && motu->msg == 0 && !has_dsp_event(motu)) {
37
prepare_to_wait(&motu->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
38
spin_unlock_irq(&motu->lock);
39
schedule();
40
finish_wait(&motu->hwdep_wait, &wait);
41
if (signal_pending(current))
42
return -ERESTARTSYS;
43
spin_lock_irq(&motu->lock);
44
}
45
46
memset(&event, 0, sizeof(event));
47
if (motu->dev_lock_changed) {
48
event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
49
event.lock_status.status = (motu->dev_lock_count > 0);
50
motu->dev_lock_changed = false;
51
spin_unlock_irq(&motu->lock);
52
53
count = min_t(long, count, sizeof(event));
54
if (copy_to_user(buf, &event, count))
55
return -EFAULT;
56
} else if (motu->msg > 0) {
57
event.motu_notification.type = SNDRV_FIREWIRE_EVENT_MOTU_NOTIFICATION;
58
event.motu_notification.message = motu->msg;
59
motu->msg = 0;
60
spin_unlock_irq(&motu->lock);
61
62
count = min_t(long, count, sizeof(event));
63
if (copy_to_user(buf, &event, count))
64
return -EFAULT;
65
} else if (has_dsp_event(motu)) {
66
size_t consumed = 0;
67
u32 __user *ptr;
68
u32 ev;
69
70
spin_unlock_irq(&motu->lock);
71
72
// Header is filled later.
73
consumed += sizeof(event.motu_register_dsp_change);
74
75
while (consumed < count &&
76
snd_motu_register_dsp_message_parser_copy_event(motu, &ev)) {
77
ptr = (u32 __user *)(buf + consumed);
78
if (put_user(ev, ptr))
79
return -EFAULT;
80
consumed += sizeof(ev);
81
}
82
83
event.motu_register_dsp_change.type = SNDRV_FIREWIRE_EVENT_MOTU_REGISTER_DSP_CHANGE;
84
event.motu_register_dsp_change.count =
85
(consumed - sizeof(event.motu_register_dsp_change)) / 4;
86
if (copy_to_user(buf, &event, sizeof(event.motu_register_dsp_change)))
87
return -EFAULT;
88
89
count = consumed;
90
} else {
91
spin_unlock_irq(&motu->lock);
92
93
count = 0;
94
}
95
96
return count;
97
}
98
99
static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
100
poll_table *wait)
101
{
102
struct snd_motu *motu = hwdep->private_data;
103
104
poll_wait(file, &motu->hwdep_wait, wait);
105
106
guard(spinlock_irq)(&motu->lock);
107
if (motu->dev_lock_changed || motu->msg || has_dsp_event(motu))
108
return EPOLLIN | EPOLLRDNORM;
109
else
110
return 0;
111
}
112
113
static int hwdep_get_info(struct snd_motu *motu, void __user *arg)
114
{
115
struct fw_device *dev = fw_parent_device(motu->unit);
116
struct snd_firewire_get_info info;
117
118
memset(&info, 0, sizeof(info));
119
info.type = SNDRV_FIREWIRE_TYPE_MOTU;
120
info.card = dev->card->index;
121
*(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
122
*(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
123
strscpy(info.device_name, dev_name(&dev->device),
124
sizeof(info.device_name));
125
126
if (copy_to_user(arg, &info, sizeof(info)))
127
return -EFAULT;
128
129
return 0;
130
}
131
132
static int hwdep_lock(struct snd_motu *motu)
133
{
134
guard(spinlock_irq)(&motu->lock);
135
136
if (motu->dev_lock_count == 0) {
137
motu->dev_lock_count = -1;
138
return 0;
139
} else {
140
return -EBUSY;
141
}
142
}
143
144
static int hwdep_unlock(struct snd_motu *motu)
145
{
146
guard(spinlock_irq)(&motu->lock);
147
148
if (motu->dev_lock_count == -1) {
149
motu->dev_lock_count = 0;
150
return 0;
151
} else {
152
return -EBADFD;
153
}
154
}
155
156
static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
157
{
158
struct snd_motu *motu = hwdep->private_data;
159
160
guard(spinlock_irq)(&motu->lock);
161
if (motu->dev_lock_count == -1)
162
motu->dev_lock_count = 0;
163
164
return 0;
165
}
166
167
static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
168
unsigned int cmd, unsigned long arg)
169
{
170
struct snd_motu *motu = hwdep->private_data;
171
172
switch (cmd) {
173
case SNDRV_FIREWIRE_IOCTL_GET_INFO:
174
return hwdep_get_info(motu, (void __user *)arg);
175
case SNDRV_FIREWIRE_IOCTL_LOCK:
176
return hwdep_lock(motu);
177
case SNDRV_FIREWIRE_IOCTL_UNLOCK:
178
return hwdep_unlock(motu);
179
case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_METER:
180
{
181
struct snd_firewire_motu_register_dsp_meter *meter;
182
int err;
183
184
if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP))
185
return -ENXIO;
186
187
meter = kzalloc(sizeof(*meter), GFP_KERNEL);
188
if (!meter)
189
return -ENOMEM;
190
191
snd_motu_register_dsp_message_parser_copy_meter(motu, meter);
192
193
err = copy_to_user((void __user *)arg, meter, sizeof(*meter));
194
kfree(meter);
195
196
if (err)
197
return -EFAULT;
198
199
return 0;
200
}
201
case SNDRV_FIREWIRE_IOCTL_MOTU_COMMAND_DSP_METER:
202
{
203
struct snd_firewire_motu_command_dsp_meter *meter;
204
int err;
205
206
if (!(motu->spec->flags & SND_MOTU_SPEC_COMMAND_DSP))
207
return -ENXIO;
208
209
meter = kzalloc(sizeof(*meter), GFP_KERNEL);
210
if (!meter)
211
return -ENOMEM;
212
213
snd_motu_command_dsp_message_parser_copy_meter(motu, meter);
214
215
err = copy_to_user((void __user *)arg, meter, sizeof(*meter));
216
kfree(meter);
217
218
if (err)
219
return -EFAULT;
220
221
return 0;
222
}
223
case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_PARAMETER:
224
{
225
struct snd_firewire_motu_register_dsp_parameter *param;
226
int err;
227
228
if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP))
229
return -ENXIO;
230
231
param = kzalloc(sizeof(*param), GFP_KERNEL);
232
if (!param)
233
return -ENOMEM;
234
235
snd_motu_register_dsp_message_parser_copy_parameter(motu, param);
236
237
err = copy_to_user((void __user *)arg, param, sizeof(*param));
238
kfree(param);
239
if (err)
240
return -EFAULT;
241
242
return 0;
243
}
244
default:
245
return -ENOIOCTLCMD;
246
}
247
}
248
249
#ifdef CONFIG_COMPAT
250
static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
251
unsigned int cmd, unsigned long arg)
252
{
253
return hwdep_ioctl(hwdep, file, cmd,
254
(unsigned long)compat_ptr(arg));
255
}
256
#else
257
#define hwdep_compat_ioctl NULL
258
#endif
259
260
int snd_motu_create_hwdep_device(struct snd_motu *motu)
261
{
262
static const struct snd_hwdep_ops ops = {
263
.read = hwdep_read,
264
.release = hwdep_release,
265
.poll = hwdep_poll,
266
.ioctl = hwdep_ioctl,
267
.ioctl_compat = hwdep_compat_ioctl,
268
};
269
struct snd_hwdep *hwdep;
270
int err;
271
272
err = snd_hwdep_new(motu->card, motu->card->driver, 0, &hwdep);
273
if (err < 0)
274
return err;
275
276
strscpy(hwdep->name, "MOTU");
277
hwdep->iface = SNDRV_HWDEP_IFACE_FW_MOTU;
278
hwdep->ops = ops;
279
hwdep->private_data = motu;
280
hwdep->exclusive = true;
281
282
motu->hwdep = hwdep;
283
284
return 0;
285
}
286
287