2 * bebob_hwdep.c - a part of driver for BeBoB based devices
4 * Copyright (c) 2013-2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
10 * This codes give three functionality.
12 * 1.get firewire node infomation
13 * 2.get notification about starting/stopping stream
14 * 3.lock/unlock stream
20 hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
23 struct snd_bebob *bebob = hwdep->private_data;
25 union snd_firewire_event event;
27 spin_lock_irq(&bebob->lock);
29 while (!bebob->dev_lock_changed) {
30 prepare_to_wait(&bebob->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
31 spin_unlock_irq(&bebob->lock);
33 finish_wait(&bebob->hwdep_wait, &wait);
34 if (signal_pending(current))
36 spin_lock_irq(&bebob->lock);
39 memset(&event, 0, sizeof(event));
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;
45 count = min_t(long, count, sizeof(event.lock_status));
48 spin_unlock_irq(&bebob->lock);
50 if (copy_to_user(buf, &event, count))
57 hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
59 struct snd_bebob *bebob = hwdep->private_data;
62 poll_wait(file, &bebob->hwdep_wait, wait);
64 spin_lock_irq(&bebob->lock);
65 if (bebob->dev_lock_changed)
66 events = POLLIN | POLLRDNORM;
69 spin_unlock_irq(&bebob->lock);
75 hwdep_get_info(struct snd_bebob *bebob, void __user *arg)
77 struct fw_device *dev = fw_parent_device(bebob->unit);
78 struct snd_firewire_get_info info;
80 memset(&info, 0, sizeof(info));
81 info.type = SNDRV_FIREWIRE_TYPE_BEBOB;
82 info.card = dev->card->index;
83 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
84 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
85 strlcpy(info.device_name, dev_name(&dev->device),
86 sizeof(info.device_name));
88 if (copy_to_user(arg, &info, sizeof(info)))
95 hwdep_lock(struct snd_bebob *bebob)
99 spin_lock_irq(&bebob->lock);
101 if (bebob->dev_lock_count == 0) {
102 bebob->dev_lock_count = -1;
108 spin_unlock_irq(&bebob->lock);
114 hwdep_unlock(struct snd_bebob *bebob)
118 spin_lock_irq(&bebob->lock);
120 if (bebob->dev_lock_count == -1) {
121 bebob->dev_lock_count = 0;
127 spin_unlock_irq(&bebob->lock);
133 hwdep_release(struct snd_hwdep *hwdep, struct file *file)
135 struct snd_bebob *bebob = hwdep->private_data;
137 spin_lock_irq(&bebob->lock);
138 if (bebob->dev_lock_count == -1)
139 bebob->dev_lock_count = 0;
140 spin_unlock_irq(&bebob->lock);
146 hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
147 unsigned int cmd, unsigned long arg)
149 struct snd_bebob *bebob = hwdep->private_data;
152 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
153 return hwdep_get_info(bebob, (void __user *)arg);
154 case SNDRV_FIREWIRE_IOCTL_LOCK:
155 return hwdep_lock(bebob);
156 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
157 return hwdep_unlock(bebob);
165 hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
166 unsigned int cmd, unsigned long arg)
168 return hwdep_ioctl(hwdep, file, cmd,
169 (unsigned long)compat_ptr(arg));
172 #define hwdep_compat_ioctl NULL
175 static const struct snd_hwdep_ops hwdep_ops = {
177 .release = hwdep_release,
179 .ioctl = hwdep_ioctl,
180 .ioctl_compat = hwdep_compat_ioctl,
183 int snd_bebob_create_hwdep_device(struct snd_bebob *bebob)
185 struct snd_hwdep *hwdep;
188 err = snd_hwdep_new(bebob->card, "BeBoB", 0, &hwdep);
191 strcpy(hwdep->name, "BeBoB");
192 hwdep->iface = SNDRV_HWDEP_IFACE_FW_BEBOB;
193 hwdep->ops = hwdep_ops;
194 hwdep->private_data = bebob;
195 hwdep->exclusive = true;