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 count = min_t(long, count, sizeof(event.lock_status));
41 if (bebob->dev_lock_changed) {
42 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
43 event.lock_status.status = (bebob->dev_lock_count > 0);
44 bebob->dev_lock_changed = false;
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 = POLLIN | POLLRDNORM;
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 static const struct snd_hwdep_ops hwdep_ops = {
176 .release = hwdep_release,
178 .ioctl = hwdep_ioctl,
179 .ioctl_compat = hwdep_compat_ioctl,
182 int snd_bebob_create_hwdep_device(struct snd_bebob *bebob)
184 struct snd_hwdep *hwdep;
187 err = snd_hwdep_new(bebob->card, "BeBoB", 0, &hwdep);
190 strcpy(hwdep->name, "BeBoB");
191 hwdep->iface = SNDRV_HWDEP_IFACE_FW_BEBOB;
192 hwdep->ops = hwdep_ops;
193 hwdep->private_data = bebob;
194 hwdep->exclusive = true;