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 if (bebob->dev_lock_changed) {
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 count = min_t(long, count, sizeof(event.lock_status));
47 spin_unlock_irq(&bebob->lock);
49 if (copy_to_user(buf, &event, count))
56 hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
58 struct snd_bebob *bebob = hwdep->private_data;
61 poll_wait(file, &bebob->hwdep_wait, wait);
63 spin_lock_irq(&bebob->lock);
64 if (bebob->dev_lock_changed)
65 events = EPOLLIN | EPOLLRDNORM;
68 spin_unlock_irq(&bebob->lock);
74 hwdep_get_info(struct snd_bebob *bebob, void __user *arg)
76 struct fw_device *dev = fw_parent_device(bebob->unit);
77 struct snd_firewire_get_info info;
79 memset(&info, 0, sizeof(info));
80 info.type = SNDRV_FIREWIRE_TYPE_BEBOB;
81 info.card = dev->card->index;
82 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
83 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
84 strlcpy(info.device_name, dev_name(&dev->device),
85 sizeof(info.device_name));
87 if (copy_to_user(arg, &info, sizeof(info)))
94 hwdep_lock(struct snd_bebob *bebob)
98 spin_lock_irq(&bebob->lock);
100 if (bebob->dev_lock_count == 0) {
101 bebob->dev_lock_count = -1;
107 spin_unlock_irq(&bebob->lock);
113 hwdep_unlock(struct snd_bebob *bebob)
117 spin_lock_irq(&bebob->lock);
119 if (bebob->dev_lock_count == -1) {
120 bebob->dev_lock_count = 0;
126 spin_unlock_irq(&bebob->lock);
132 hwdep_release(struct snd_hwdep *hwdep, struct file *file)
134 struct snd_bebob *bebob = hwdep->private_data;
136 spin_lock_irq(&bebob->lock);
137 if (bebob->dev_lock_count == -1)
138 bebob->dev_lock_count = 0;
139 spin_unlock_irq(&bebob->lock);
145 hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
146 unsigned int cmd, unsigned long arg)
148 struct snd_bebob *bebob = hwdep->private_data;
151 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
152 return hwdep_get_info(bebob, (void __user *)arg);
153 case SNDRV_FIREWIRE_IOCTL_LOCK:
154 return hwdep_lock(bebob);
155 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
156 return hwdep_unlock(bebob);
164 hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
165 unsigned int cmd, unsigned long arg)
167 return hwdep_ioctl(hwdep, file, cmd,
168 (unsigned long)compat_ptr(arg));
171 #define hwdep_compat_ioctl NULL
174 int snd_bebob_create_hwdep_device(struct snd_bebob *bebob)
176 static const struct snd_hwdep_ops ops = {
178 .release = hwdep_release,
180 .ioctl = hwdep_ioctl,
181 .ioctl_compat = hwdep_compat_ioctl,
183 struct snd_hwdep *hwdep;
186 err = snd_hwdep_new(bebob->card, "BeBoB", 0, &hwdep);
189 strcpy(hwdep->name, "BeBoB");
190 hwdep->iface = SNDRV_HWDEP_IFACE_FW_BEBOB;
192 hwdep->private_data = bebob;
193 hwdep->exclusive = true;