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 if (bebob->dev_lock_changed) {
41 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
42 event.lock_status.status = (bebob->dev_lock_count > 0);
43 bebob->dev_lock_changed = false;
46 spin_unlock_irq(&bebob->lock);
48 if (copy_to_user(buf, &event, count))
55 hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
57 struct snd_bebob *bebob = hwdep->private_data;
60 poll_wait(file, &bebob->hwdep_wait, wait);
62 spin_lock_irq(&bebob->lock);
63 if (bebob->dev_lock_changed)
64 events = EPOLLIN | EPOLLRDNORM;
67 spin_unlock_irq(&bebob->lock);
73 hwdep_get_info(struct snd_bebob *bebob, void __user *arg)
75 struct fw_device *dev = fw_parent_device(bebob->unit);
76 struct snd_firewire_get_info info;
78 memset(&info, 0, sizeof(info));
79 info.type = SNDRV_FIREWIRE_TYPE_BEBOB;
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)))
93 hwdep_lock(struct snd_bebob *bebob)
97 spin_lock_irq(&bebob->lock);
99 if (bebob->dev_lock_count == 0) {
100 bebob->dev_lock_count = -1;
106 spin_unlock_irq(&bebob->lock);
112 hwdep_unlock(struct snd_bebob *bebob)
116 spin_lock_irq(&bebob->lock);
118 if (bebob->dev_lock_count == -1) {
119 bebob->dev_lock_count = 0;
125 spin_unlock_irq(&bebob->lock);
131 hwdep_release(struct snd_hwdep *hwdep, struct file *file)
133 struct snd_bebob *bebob = hwdep->private_data;
135 spin_lock_irq(&bebob->lock);
136 if (bebob->dev_lock_count == -1)
137 bebob->dev_lock_count = 0;
138 spin_unlock_irq(&bebob->lock);
144 hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
145 unsigned int cmd, unsigned long arg)
147 struct snd_bebob *bebob = hwdep->private_data;
150 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
151 return hwdep_get_info(bebob, (void __user *)arg);
152 case SNDRV_FIREWIRE_IOCTL_LOCK:
153 return hwdep_lock(bebob);
154 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
155 return hwdep_unlock(bebob);
163 hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
164 unsigned int cmd, unsigned long arg)
166 return hwdep_ioctl(hwdep, file, cmd,
167 (unsigned long)compat_ptr(arg));
170 #define hwdep_compat_ioctl NULL
173 int snd_bebob_create_hwdep_device(struct snd_bebob *bebob)
175 static const struct snd_hwdep_ops ops = {
177 .release = hwdep_release,
179 .ioctl = hwdep_ioctl,
180 .ioctl_compat = hwdep_compat_ioctl,
182 struct snd_hwdep *hwdep;
185 err = snd_hwdep_new(bebob->card, "BeBoB", 0, &hwdep);
188 strcpy(hwdep->name, "BeBoB");
189 hwdep->iface = SNDRV_HWDEP_IFACE_FW_BEBOB;
191 hwdep->private_data = bebob;
192 hwdep->exclusive = true;