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)))
91 spin_unlock_irq(&motu->lock);
99 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
102 struct snd_motu *motu = hwdep->private_data;
105 poll_wait(file, &motu->hwdep_wait, wait);
107 spin_lock_irq(&motu->lock);
108 if (motu->dev_lock_changed || motu->msg || has_dsp_event(motu))
109 events = EPOLLIN | EPOLLRDNORM;
112 spin_unlock_irq(&motu->lock);
114 return events | EPOLLOUT;
117 static int hwdep_get_info(struct snd_motu *motu, void __user *arg)
119 struct fw_device *dev = fw_parent_device(motu->unit);
120 struct snd_firewire_get_info info;
122 memset(&info, 0, sizeof(info));
123 info.type = SNDRV_FIREWIRE_TYPE_MOTU;
124 info.card = dev->card->index;
125 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
126 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
127 strscpy(info.device_name, dev_name(&dev->device),
128 sizeof(info.device_name));
130 if (copy_to_user(arg, &info, sizeof(info)))
136 static int hwdep_lock(struct snd_motu *motu)
140 spin_lock_irq(&motu->lock);
142 if (motu->dev_lock_count == 0) {
143 motu->dev_lock_count = -1;
149 spin_unlock_irq(&motu->lock);
154 static int hwdep_unlock(struct snd_motu *motu)
158 spin_lock_irq(&motu->lock);
160 if (motu->dev_lock_count == -1) {
161 motu->dev_lock_count = 0;
167 spin_unlock_irq(&motu->lock);
172 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
174 struct snd_motu *motu = hwdep->private_data;
176 spin_lock_irq(&motu->lock);
177 if (motu->dev_lock_count == -1)
178 motu->dev_lock_count = 0;
179 spin_unlock_irq(&motu->lock);
184 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
185 unsigned int cmd, unsigned long arg)
187 struct snd_motu *motu = hwdep->private_data;
190 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
191 return hwdep_get_info(motu, (void __user *)arg);
192 case SNDRV_FIREWIRE_IOCTL_LOCK:
193 return hwdep_lock(motu);
194 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
195 return hwdep_unlock(motu);
196 case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_METER:
198 struct snd_firewire_motu_register_dsp_meter *meter;
201 if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP))
204 meter = kzalloc(sizeof(*meter), GFP_KERNEL);
208 snd_motu_register_dsp_message_parser_copy_meter(motu, meter);
210 err = copy_to_user((void __user *)arg, meter, sizeof(*meter));
218 case SNDRV_FIREWIRE_IOCTL_MOTU_COMMAND_DSP_METER:
220 struct snd_firewire_motu_command_dsp_meter *meter;
223 if (!(motu->spec->flags & SND_MOTU_SPEC_COMMAND_DSP))
226 meter = kzalloc(sizeof(*meter), GFP_KERNEL);
230 snd_motu_command_dsp_message_parser_copy_meter(motu, meter);
232 err = copy_to_user((void __user *)arg, meter, sizeof(*meter));
240 case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_PARAMETER:
242 struct snd_firewire_motu_register_dsp_parameter *param;
245 if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP))
248 param = kzalloc(sizeof(*param), GFP_KERNEL);
252 snd_motu_register_dsp_message_parser_copy_parameter(motu, param);
254 err = copy_to_user((void __user *)arg, param, sizeof(*param));
267 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
268 unsigned int cmd, unsigned long arg)
270 return hwdep_ioctl(hwdep, file, cmd,
271 (unsigned long)compat_ptr(arg));
274 #define hwdep_compat_ioctl NULL
277 int snd_motu_create_hwdep_device(struct snd_motu *motu)
279 static const struct snd_hwdep_ops ops = {
281 .release = hwdep_release,
283 .ioctl = hwdep_ioctl,
284 .ioctl_compat = hwdep_compat_ioctl,
286 struct snd_hwdep *hwdep;
289 err = snd_hwdep_new(motu->card, motu->card->driver, 0, &hwdep);
293 strcpy(hwdep->name, "MOTU");
294 hwdep->iface = SNDRV_HWDEP_IFACE_FW_MOTU;
296 hwdep->private_data = motu;
297 hwdep->exclusive = true;