1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * tascam-hwdep.c - a part of driver for TASCAM FireWire series
4 *
5 * Copyright (c) 2015 Takashi Sakamoto
6 */
7
8 /*
9 * This codes give three functionality.
10 *
11 * 1.get firewire node information
12 * 2.get notification about starting/stopping stream
13 * 3.lock/unlock stream
14 */
15
16 #include "tascam.h"
17
tscm_hwdep_read_locked(struct snd_tscm * tscm,char __user * buf,long count,loff_t * offset)18 static long tscm_hwdep_read_locked(struct snd_tscm *tscm, char __user *buf,
19 long count, loff_t *offset)
20 __releases(&tscm->lock)
21 {
22 struct snd_firewire_event_lock_status event = {
23 .type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
24 };
25
26 event.status = (tscm->dev_lock_count > 0);
27 tscm->dev_lock_changed = false;
28 count = min_t(long, count, sizeof(event));
29
30 spin_unlock_irq(&tscm->lock);
31
32 if (copy_to_user(buf, &event, count))
33 return -EFAULT;
34
35 return count;
36 }
37
tscm_hwdep_read_queue(struct snd_tscm * tscm,char __user * buf,long remained,loff_t * offset)38 static long tscm_hwdep_read_queue(struct snd_tscm *tscm, char __user *buf,
39 long remained, loff_t *offset)
40 __releases(&tscm->lock)
41 {
42 char __user *pos = buf;
43 unsigned int type = SNDRV_FIREWIRE_EVENT_TASCAM_CONTROL;
44 struct snd_firewire_tascam_change *entries = tscm->queue;
45 long count;
46
47 // At least, one control event can be copied.
48 if (remained < sizeof(type) + sizeof(*entries)) {
49 spin_unlock_irq(&tscm->lock);
50 return -EINVAL;
51 }
52
53 // Copy the type field later.
54 count = sizeof(type);
55 remained -= sizeof(type);
56 pos += sizeof(type);
57
58 while (true) {
59 unsigned int head_pos;
60 unsigned int tail_pos;
61 unsigned int length;
62
63 if (tscm->pull_pos == tscm->push_pos)
64 break;
65 else if (tscm->pull_pos < tscm->push_pos)
66 tail_pos = tscm->push_pos;
67 else
68 tail_pos = SND_TSCM_QUEUE_COUNT;
69 head_pos = tscm->pull_pos;
70
71 length = (tail_pos - head_pos) * sizeof(*entries);
72 if (remained < length)
73 length = rounddown(remained, sizeof(*entries));
74 if (length == 0)
75 break;
76 tail_pos = head_pos + length / sizeof(*entries);
77
78 spin_unlock_irq(&tscm->lock);
79 if (copy_to_user(pos, &entries[head_pos], length))
80 return -EFAULT;
81
82 spin_lock_irq(&tscm->lock);
83
84 tscm->pull_pos = tail_pos % SND_TSCM_QUEUE_COUNT;
85
86 count += length;
87 remained -= length;
88 pos += length;
89 }
90
91 spin_unlock_irq(&tscm->lock);
92
93 if (copy_to_user(buf, &type, sizeof(type)))
94 return -EFAULT;
95
96 return count;
97 }
98
hwdep_read(struct snd_hwdep * hwdep,char __user * buf,long count,loff_t * offset)99 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
100 loff_t *offset)
101 {
102 struct snd_tscm *tscm = hwdep->private_data;
103 DEFINE_WAIT(wait);
104
105 spin_lock_irq(&tscm->lock);
106
107 while (!tscm->dev_lock_changed && tscm->push_pos == tscm->pull_pos) {
108 prepare_to_wait(&tscm->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
109 spin_unlock_irq(&tscm->lock);
110 schedule();
111 finish_wait(&tscm->hwdep_wait, &wait);
112 if (signal_pending(current))
113 return -ERESTARTSYS;
114 spin_lock_irq(&tscm->lock);
115 }
116
117 // NOTE: The acquired lock should be released in callee side.
118 if (tscm->dev_lock_changed) {
119 count = tscm_hwdep_read_locked(tscm, buf, count, offset);
120 } else if (tscm->push_pos != tscm->pull_pos) {
121 count = tscm_hwdep_read_queue(tscm, buf, count, offset);
122 } else {
123 spin_unlock_irq(&tscm->lock);
124 count = 0;
125 }
126
127 return count;
128 }
129
hwdep_poll(struct snd_hwdep * hwdep,struct file * file,poll_table * wait)130 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
131 poll_table *wait)
132 {
133 struct snd_tscm *tscm = hwdep->private_data;
134
135 poll_wait(file, &tscm->hwdep_wait, wait);
136
137 guard(spinlock_irq)(&tscm->lock);
138 if (tscm->dev_lock_changed || tscm->push_pos != tscm->pull_pos)
139 return EPOLLIN | EPOLLRDNORM;
140 else
141 return 0;
142 }
143
hwdep_get_info(struct snd_tscm * tscm,void __user * arg)144 static int hwdep_get_info(struct snd_tscm *tscm, void __user *arg)
145 {
146 struct fw_device *dev = fw_parent_device(tscm->unit);
147 struct snd_firewire_get_info info;
148
149 memset(&info, 0, sizeof(info));
150 info.type = SNDRV_FIREWIRE_TYPE_TASCAM;
151 info.card = dev->card->index;
152 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
153 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
154 strscpy(info.device_name, dev_name(&dev->device),
155 sizeof(info.device_name));
156
157 if (copy_to_user(arg, &info, sizeof(info)))
158 return -EFAULT;
159
160 return 0;
161 }
162
hwdep_lock(struct snd_tscm * tscm)163 static int hwdep_lock(struct snd_tscm *tscm)
164 {
165 guard(spinlock_irq)(&tscm->lock);
166
167 if (tscm->dev_lock_count == 0) {
168 tscm->dev_lock_count = -1;
169 return 0;
170 } else {
171 return -EBUSY;
172 }
173 }
174
hwdep_unlock(struct snd_tscm * tscm)175 static int hwdep_unlock(struct snd_tscm *tscm)
176 {
177 guard(spinlock_irq)(&tscm->lock);
178
179 if (tscm->dev_lock_count == -1) {
180 tscm->dev_lock_count = 0;
181 return 0;
182 } else {
183 return -EBADFD;
184 }
185 }
186
tscm_hwdep_state(struct snd_tscm * tscm,void __user * arg)187 static int tscm_hwdep_state(struct snd_tscm *tscm, void __user *arg)
188 {
189 if (copy_to_user(arg, tscm->state, sizeof(tscm->state)))
190 return -EFAULT;
191
192 return 0;
193 }
194
hwdep_release(struct snd_hwdep * hwdep,struct file * file)195 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
196 {
197 struct snd_tscm *tscm = hwdep->private_data;
198
199 guard(spinlock_irq)(&tscm->lock);
200 if (tscm->dev_lock_count == -1)
201 tscm->dev_lock_count = 0;
202
203 return 0;
204 }
205
hwdep_ioctl(struct snd_hwdep * hwdep,struct file * file,unsigned int cmd,unsigned long arg)206 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
207 unsigned int cmd, unsigned long arg)
208 {
209 struct snd_tscm *tscm = hwdep->private_data;
210
211 switch (cmd) {
212 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
213 return hwdep_get_info(tscm, (void __user *)arg);
214 case SNDRV_FIREWIRE_IOCTL_LOCK:
215 return hwdep_lock(tscm);
216 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
217 return hwdep_unlock(tscm);
218 case SNDRV_FIREWIRE_IOCTL_TASCAM_STATE:
219 return tscm_hwdep_state(tscm, (void __user *)arg);
220 default:
221 return -ENOIOCTLCMD;
222 }
223 }
224
225 #ifdef CONFIG_COMPAT
hwdep_compat_ioctl(struct snd_hwdep * hwdep,struct file * file,unsigned int cmd,unsigned long arg)226 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
227 unsigned int cmd, unsigned long arg)
228 {
229 return hwdep_ioctl(hwdep, file, cmd,
230 (unsigned long)compat_ptr(arg));
231 }
232 #else
233 #define hwdep_compat_ioctl NULL
234 #endif
235
snd_tscm_create_hwdep_device(struct snd_tscm * tscm)236 int snd_tscm_create_hwdep_device(struct snd_tscm *tscm)
237 {
238 static const struct snd_hwdep_ops ops = {
239 .read = hwdep_read,
240 .release = hwdep_release,
241 .poll = hwdep_poll,
242 .ioctl = hwdep_ioctl,
243 .ioctl_compat = hwdep_compat_ioctl,
244 };
245 struct snd_hwdep *hwdep;
246 int err;
247
248 err = snd_hwdep_new(tscm->card, "Tascam", 0, &hwdep);
249 if (err < 0)
250 return err;
251
252 strscpy(hwdep->name, "Tascam");
253 hwdep->iface = SNDRV_HWDEP_IFACE_FW_TASCAM;
254 hwdep->ops = ops;
255 hwdep->private_data = tscm;
256 hwdep->exclusive = true;
257
258 tscm->hwdep = hwdep;
259
260 return err;
261 }
262