1 // SPDX-License-Identifier: GPL-2.0-only
3 * tascam-hwdep.c - a part of driver for TASCAM FireWire series
5 * Copyright (c) 2015 Takashi Sakamoto
9 * This codes give three functionality.
11 * 1.get firewire node information
12 * 2.get notification about starting/stopping stream
13 * 3.lock/unlock stream
18 static long tscm_hwdep_read_locked(struct snd_tscm *tscm, char __user *buf,
19 long count, loff_t *offset)
21 struct snd_firewire_event_lock_status event = {
22 .type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
25 event.status = (tscm->dev_lock_count > 0);
26 tscm->dev_lock_changed = false;
27 count = min_t(long, count, sizeof(event));
29 spin_unlock_irq(&tscm->lock);
31 if (copy_to_user(buf, &event, count))
37 static long tscm_hwdep_read_queue(struct snd_tscm *tscm, char __user *buf,
38 long remained, loff_t *offset)
40 char __user *pos = buf;
41 unsigned int type = SNDRV_FIREWIRE_EVENT_TASCAM_CONTROL;
42 struct snd_firewire_tascam_change *entries = tscm->queue;
45 // At least, one control event can be copied.
46 if (remained < sizeof(type) + sizeof(*entries)) {
47 spin_unlock_irq(&tscm->lock);
51 // Copy the type field later.
53 remained -= sizeof(type);
57 unsigned int head_pos;
58 unsigned int tail_pos;
61 if (tscm->pull_pos == tscm->push_pos)
63 else if (tscm->pull_pos < tscm->push_pos)
64 tail_pos = tscm->push_pos;
66 tail_pos = SND_TSCM_QUEUE_COUNT;
67 head_pos = tscm->pull_pos;
69 length = (tail_pos - head_pos) * sizeof(*entries);
70 if (remained < length)
71 length = rounddown(remained, sizeof(*entries));
75 spin_unlock_irq(&tscm->lock);
76 if (copy_to_user(pos, &entries[head_pos], length))
79 spin_lock_irq(&tscm->lock);
81 tscm->pull_pos = tail_pos % SND_TSCM_QUEUE_COUNT;
88 spin_unlock_irq(&tscm->lock);
90 if (copy_to_user(buf, &type, sizeof(type)))
96 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
99 struct snd_tscm *tscm = hwdep->private_data;
102 spin_lock_irq(&tscm->lock);
104 while (!tscm->dev_lock_changed && tscm->push_pos == tscm->pull_pos) {
105 prepare_to_wait(&tscm->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
106 spin_unlock_irq(&tscm->lock);
108 finish_wait(&tscm->hwdep_wait, &wait);
109 if (signal_pending(current))
111 spin_lock_irq(&tscm->lock);
114 // NOTE: The acquired lock should be released in callee side.
115 if (tscm->dev_lock_changed) {
116 count = tscm_hwdep_read_locked(tscm, buf, count, offset);
117 } else if (tscm->push_pos != tscm->pull_pos) {
118 count = tscm_hwdep_read_queue(tscm, buf, count, offset);
120 spin_unlock_irq(&tscm->lock);
127 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
130 struct snd_tscm *tscm = hwdep->private_data;
133 poll_wait(file, &tscm->hwdep_wait, wait);
135 spin_lock_irq(&tscm->lock);
136 if (tscm->dev_lock_changed || tscm->push_pos != tscm->pull_pos)
137 events = EPOLLIN | EPOLLRDNORM;
140 spin_unlock_irq(&tscm->lock);
145 static int hwdep_get_info(struct snd_tscm *tscm, void __user *arg)
147 struct fw_device *dev = fw_parent_device(tscm->unit);
148 struct snd_firewire_get_info info;
150 memset(&info, 0, sizeof(info));
151 info.type = SNDRV_FIREWIRE_TYPE_TASCAM;
152 info.card = dev->card->index;
153 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
154 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
155 strlcpy(info.device_name, dev_name(&dev->device),
156 sizeof(info.device_name));
158 if (copy_to_user(arg, &info, sizeof(info)))
164 static int hwdep_lock(struct snd_tscm *tscm)
168 spin_lock_irq(&tscm->lock);
170 if (tscm->dev_lock_count == 0) {
171 tscm->dev_lock_count = -1;
177 spin_unlock_irq(&tscm->lock);
182 static int hwdep_unlock(struct snd_tscm *tscm)
186 spin_lock_irq(&tscm->lock);
188 if (tscm->dev_lock_count == -1) {
189 tscm->dev_lock_count = 0;
195 spin_unlock_irq(&tscm->lock);
200 static int tscm_hwdep_state(struct snd_tscm *tscm, void __user *arg)
202 if (copy_to_user(arg, tscm->state, sizeof(tscm->state)))
208 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
210 struct snd_tscm *tscm = hwdep->private_data;
212 spin_lock_irq(&tscm->lock);
213 if (tscm->dev_lock_count == -1)
214 tscm->dev_lock_count = 0;
215 spin_unlock_irq(&tscm->lock);
220 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
221 unsigned int cmd, unsigned long arg)
223 struct snd_tscm *tscm = hwdep->private_data;
226 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
227 return hwdep_get_info(tscm, (void __user *)arg);
228 case SNDRV_FIREWIRE_IOCTL_LOCK:
229 return hwdep_lock(tscm);
230 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
231 return hwdep_unlock(tscm);
232 case SNDRV_FIREWIRE_IOCTL_TASCAM_STATE:
233 return tscm_hwdep_state(tscm, (void __user *)arg);
240 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
241 unsigned int cmd, unsigned long arg)
243 return hwdep_ioctl(hwdep, file, cmd,
244 (unsigned long)compat_ptr(arg));
247 #define hwdep_compat_ioctl NULL
250 int snd_tscm_create_hwdep_device(struct snd_tscm *tscm)
252 static const struct snd_hwdep_ops ops = {
254 .release = hwdep_release,
256 .ioctl = hwdep_ioctl,
257 .ioctl_compat = hwdep_compat_ioctl,
259 struct snd_hwdep *hwdep;
262 err = snd_hwdep_new(tscm->card, "Tascam", 0, &hwdep);
266 strcpy(hwdep->name, "Tascam");
267 hwdep->iface = SNDRV_HWDEP_IFACE_FW_TASCAM;
269 hwdep->private_data = tscm;
270 hwdep->exclusive = true;