1 // SPDX-License-Identifier: GPL-2.0-only
3 * motu-hwdep.c - a part of driver for MOTU FireWire series
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
9 * This codes have five functionalities.
11 * 1.get information about firewire node
12 * 2.get notification about starting/stopping stream
13 * 3.lock/unlock streaming
19 static bool has_dsp_event(struct snd_motu *motu)
21 if (motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP)
22 return (snd_motu_register_dsp_message_parser_count_event(motu) > 0);
27 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
30 struct snd_motu *motu = hwdep->private_data;
32 union snd_firewire_event event;
34 spin_lock_irq(&motu->lock);
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);
40 finish_wait(&motu->hwdep_wait, &wait);
41 if (signal_pending(current))
43 spin_lock_irq(&motu->lock);
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);
53 count = min_t(long, count, sizeof(event));
54 if (copy_to_user(buf, &event, count))
56 } else if (motu->msg > 0) {
57 event.motu_notification.type = SNDRV_FIREWIRE_EVENT_MOTU_NOTIFICATION;
58 event.motu_notification.message = motu->msg;
60 spin_unlock_irq(&motu->lock);
62 count = min_t(long, count, sizeof(event));
63 if (copy_to_user(buf, &event, count))
65 } else if (has_dsp_event(motu)) {
70 spin_unlock_irq(&motu->lock);
72 // Header is filled later.
73 consumed += sizeof(event.motu_register_dsp_change);
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))
80 consumed += sizeof(ev);
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)))
95 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
98 struct snd_motu *motu = hwdep->private_data;
101 poll_wait(file, &motu->hwdep_wait, wait);
103 spin_lock_irq(&motu->lock);
104 if (motu->dev_lock_changed || motu->msg || has_dsp_event(motu))
105 events = EPOLLIN | EPOLLRDNORM;
108 spin_unlock_irq(&motu->lock);
110 return events | EPOLLOUT;
113 static int hwdep_get_info(struct snd_motu *motu, void __user *arg)
115 struct fw_device *dev = fw_parent_device(motu->unit);
116 struct snd_firewire_get_info info;
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));
126 if (copy_to_user(arg, &info, sizeof(info)))
132 static int hwdep_lock(struct snd_motu *motu)
136 spin_lock_irq(&motu->lock);
138 if (motu->dev_lock_count == 0) {
139 motu->dev_lock_count = -1;
145 spin_unlock_irq(&motu->lock);
150 static int hwdep_unlock(struct snd_motu *motu)
154 spin_lock_irq(&motu->lock);
156 if (motu->dev_lock_count == -1) {
157 motu->dev_lock_count = 0;
163 spin_unlock_irq(&motu->lock);
168 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
170 struct snd_motu *motu = hwdep->private_data;
172 spin_lock_irq(&motu->lock);
173 if (motu->dev_lock_count == -1)
174 motu->dev_lock_count = 0;
175 spin_unlock_irq(&motu->lock);
180 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
181 unsigned int cmd, unsigned long arg)
183 struct snd_motu *motu = hwdep->private_data;
186 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
187 return hwdep_get_info(motu, (void __user *)arg);
188 case SNDRV_FIREWIRE_IOCTL_LOCK:
189 return hwdep_lock(motu);
190 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
191 return hwdep_unlock(motu);
192 case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_METER:
194 struct snd_firewire_motu_register_dsp_meter *meter;
197 if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP))
200 meter = kzalloc(sizeof(*meter), GFP_KERNEL);
204 snd_motu_register_dsp_message_parser_copy_meter(motu, meter);
206 err = copy_to_user((void __user *)arg, meter, sizeof(*meter));
214 case SNDRV_FIREWIRE_IOCTL_MOTU_COMMAND_DSP_METER:
216 struct snd_firewire_motu_command_dsp_meter *meter;
219 if (!(motu->spec->flags & SND_MOTU_SPEC_COMMAND_DSP))
222 meter = kzalloc(sizeof(*meter), GFP_KERNEL);
226 snd_motu_command_dsp_message_parser_copy_meter(motu, meter);
228 err = copy_to_user((void __user *)arg, meter, sizeof(*meter));
236 case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_PARAMETER:
238 struct snd_firewire_motu_register_dsp_parameter *param;
241 if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP))
244 param = kzalloc(sizeof(*param), GFP_KERNEL);
248 snd_motu_register_dsp_message_parser_copy_parameter(motu, param);
250 err = copy_to_user((void __user *)arg, param, sizeof(*param));
263 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
264 unsigned int cmd, unsigned long arg)
266 return hwdep_ioctl(hwdep, file, cmd,
267 (unsigned long)compat_ptr(arg));
270 #define hwdep_compat_ioctl NULL
273 int snd_motu_create_hwdep_device(struct snd_motu *motu)
275 static const struct snd_hwdep_ops ops = {
277 .release = hwdep_release,
279 .ioctl = hwdep_ioctl,
280 .ioctl_compat = hwdep_compat_ioctl,
282 struct snd_hwdep *hwdep;
285 err = snd_hwdep_new(motu->card, motu->card->driver, 0, &hwdep);
289 strcpy(hwdep->name, "MOTU");
290 hwdep->iface = SNDRV_HWDEP_IFACE_FW_MOTU;
292 hwdep->private_data = motu;
293 hwdep->exclusive = true;