1 // SPDX-License-Identifier: GPL-2.0-only
3 * bebob_hwdep.c - a part of driver for BeBoB based devices
5 * Copyright (c) 2013-2014 Takashi Sakamoto
9 * This codes give three functionality.
11 * 1.get firewire node infomation
12 * 2.get notification about starting/stopping stream
13 * 3.lock/unlock stream
19 hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
22 struct snd_bebob *bebob = hwdep->private_data;
24 union snd_firewire_event event;
26 spin_lock_irq(&bebob->lock);
28 while (!bebob->dev_lock_changed) {
29 prepare_to_wait(&bebob->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
30 spin_unlock_irq(&bebob->lock);
32 finish_wait(&bebob->hwdep_wait, &wait);
33 if (signal_pending(current))
35 spin_lock_irq(&bebob->lock);
38 memset(&event, 0, sizeof(event));
39 count = min_t(long, count, sizeof(event.lock_status));
40 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
41 event.lock_status.status = (bebob->dev_lock_count > 0);
42 bebob->dev_lock_changed = false;
44 spin_unlock_irq(&bebob->lock);
46 if (copy_to_user(buf, &event, count))
53 hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
55 struct snd_bebob *bebob = hwdep->private_data;
58 poll_wait(file, &bebob->hwdep_wait, wait);
60 spin_lock_irq(&bebob->lock);
61 if (bebob->dev_lock_changed)
62 events = EPOLLIN | EPOLLRDNORM;
65 spin_unlock_irq(&bebob->lock);
71 hwdep_get_info(struct snd_bebob *bebob, void __user *arg)
73 struct fw_device *dev = fw_parent_device(bebob->unit);
74 struct snd_firewire_get_info info;
76 memset(&info, 0, sizeof(info));
77 info.type = SNDRV_FIREWIRE_TYPE_BEBOB;
78 info.card = dev->card->index;
79 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
80 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
81 strscpy(info.device_name, dev_name(&dev->device),
82 sizeof(info.device_name));
84 if (copy_to_user(arg, &info, sizeof(info)))
91 hwdep_lock(struct snd_bebob *bebob)
95 spin_lock_irq(&bebob->lock);
97 if (bebob->dev_lock_count == 0) {
98 bebob->dev_lock_count = -1;
104 spin_unlock_irq(&bebob->lock);
110 hwdep_unlock(struct snd_bebob *bebob)
114 spin_lock_irq(&bebob->lock);
116 if (bebob->dev_lock_count == -1) {
117 bebob->dev_lock_count = 0;
123 spin_unlock_irq(&bebob->lock);
129 hwdep_release(struct snd_hwdep *hwdep, struct file *file)
131 struct snd_bebob *bebob = hwdep->private_data;
133 spin_lock_irq(&bebob->lock);
134 if (bebob->dev_lock_count == -1)
135 bebob->dev_lock_count = 0;
136 spin_unlock_irq(&bebob->lock);
142 hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
143 unsigned int cmd, unsigned long arg)
145 struct snd_bebob *bebob = hwdep->private_data;
148 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
149 return hwdep_get_info(bebob, (void __user *)arg);
150 case SNDRV_FIREWIRE_IOCTL_LOCK:
151 return hwdep_lock(bebob);
152 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
153 return hwdep_unlock(bebob);
161 hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
162 unsigned int cmd, unsigned long arg)
164 return hwdep_ioctl(hwdep, file, cmd,
165 (unsigned long)compat_ptr(arg));
168 #define hwdep_compat_ioctl NULL
171 int snd_bebob_create_hwdep_device(struct snd_bebob *bebob)
173 static const struct snd_hwdep_ops ops = {
175 .release = hwdep_release,
177 .ioctl = hwdep_ioctl,
178 .ioctl_compat = hwdep_compat_ioctl,
180 struct snd_hwdep *hwdep;
183 err = snd_hwdep_new(bebob->card, "BeBoB", 0, &hwdep);
186 strcpy(hwdep->name, "BeBoB");
187 hwdep->iface = SNDRV_HWDEP_IFACE_FW_BEBOB;
189 hwdep->private_data = bebob;
190 hwdep->exclusive = true;