2 * fireworks_hwdep.c - a part of driver for Fireworks 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 have five functionalities.
12 * 1.get information about firewire node
13 * 2.get notification about starting/stopping stream
14 * 3.lock/unlock streaming
15 * 4.transmit command of EFW transaction
16 * 5.receive response of EFW transaction
20 #include "fireworks.h"
23 hwdep_read_resp_buf(struct snd_efw *efw, char __user *buf, long remained,
26 unsigned int length, till_end, type;
27 struct snd_efw_transaction *t;
31 if (remained < sizeof(type) + sizeof(struct snd_efw_transaction))
34 /* data type is SNDRV_FIREWIRE_EVENT_EFW_RESPONSE */
35 type = SNDRV_FIREWIRE_EVENT_EFW_RESPONSE;
36 if (copy_to_user(buf, &type, sizeof(type)))
38 count += sizeof(type);
39 remained -= sizeof(type);
42 /* write into buffer as many responses as possible */
43 spin_lock_irq(&efw->lock);
46 * When another task reaches here during this task's access to user
47 * space, it picks up current position in buffer and can read the same
48 * series of responses.
50 pull_ptr = efw->pull_ptr;
52 while (efw->push_ptr != pull_ptr) {
53 t = (struct snd_efw_transaction *)(pull_ptr);
54 length = be32_to_cpu(t->length) * sizeof(__be32);
56 /* confirm enough space for this response */
57 if (remained < length)
60 /* copy from ring buffer to user buffer */
62 till_end = snd_efw_resp_buf_size -
63 (unsigned int)(pull_ptr - efw->resp_buf);
64 till_end = min_t(unsigned int, length, till_end);
66 spin_unlock_irq(&efw->lock);
68 if (copy_to_user(buf, pull_ptr, till_end))
71 spin_lock_irq(&efw->lock);
74 if (pull_ptr >= efw->resp_buf + snd_efw_resp_buf_size)
75 pull_ptr -= snd_efw_resp_buf_size;
85 * All of tasks can read from the buffer nearly simultaneously, but the
86 * last position for each task is different depending on the length of
87 * given buffer. Here, for simplicity, a position of buffer is set by
88 * the latest task. It's better for a listening application to allow one
89 * thread to read from the buffer. Unless, each task can read different
90 * sequence of responses depending on variation of buffer length.
92 efw->pull_ptr = pull_ptr;
94 spin_unlock_irq(&efw->lock);
100 hwdep_read_locked(struct snd_efw *efw, char __user *buf, long count,
103 union snd_firewire_event event = {
104 .lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
107 spin_lock_irq(&efw->lock);
109 event.lock_status.status = (efw->dev_lock_count > 0);
110 efw->dev_lock_changed = false;
112 spin_unlock_irq(&efw->lock);
114 count = min_t(long, count, sizeof(event.lock_status));
116 if (copy_to_user(buf, &event, count))
123 hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
126 struct snd_efw *efw = hwdep->private_data;
128 bool dev_lock_changed;
131 spin_lock_irq(&efw->lock);
133 dev_lock_changed = efw->dev_lock_changed;
134 queued = efw->push_ptr != efw->pull_ptr;
136 while (!dev_lock_changed && !queued) {
137 prepare_to_wait(&efw->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
138 spin_unlock_irq(&efw->lock);
140 finish_wait(&efw->hwdep_wait, &wait);
141 if (signal_pending(current))
143 spin_lock_irq(&efw->lock);
144 dev_lock_changed = efw->dev_lock_changed;
145 queued = efw->push_ptr != efw->pull_ptr;
148 spin_unlock_irq(&efw->lock);
150 if (dev_lock_changed)
151 count = hwdep_read_locked(efw, buf, count, offset);
153 count = hwdep_read_resp_buf(efw, buf, count, offset);
159 hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
162 struct snd_efw *efw = hwdep->private_data;
166 if (count < sizeof(struct snd_efw_transaction) ||
167 SND_EFW_RESPONSE_MAXIMUM_BYTES < count)
170 buf = memdup_user(data, count);
174 /* check seqnum is not for kernel-land */
175 seqnum = be32_to_cpu(((struct snd_efw_transaction *)buf)->seqnum);
176 if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX) {
181 if (snd_efw_transaction_cmd(efw->unit, buf, count) < 0)
189 hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
191 struct snd_efw *efw = hwdep->private_data;
194 poll_wait(file, &efw->hwdep_wait, wait);
196 spin_lock_irq(&efw->lock);
197 if (efw->dev_lock_changed || efw->pull_ptr != efw->push_ptr)
198 events = EPOLLIN | EPOLLRDNORM;
201 spin_unlock_irq(&efw->lock);
203 return events | EPOLLOUT;
207 hwdep_get_info(struct snd_efw *efw, void __user *arg)
209 struct fw_device *dev = fw_parent_device(efw->unit);
210 struct snd_firewire_get_info info;
212 memset(&info, 0, sizeof(info));
213 info.type = SNDRV_FIREWIRE_TYPE_FIREWORKS;
214 info.card = dev->card->index;
215 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
216 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
217 strlcpy(info.device_name, dev_name(&dev->device),
218 sizeof(info.device_name));
220 if (copy_to_user(arg, &info, sizeof(info)))
227 hwdep_lock(struct snd_efw *efw)
231 spin_lock_irq(&efw->lock);
233 if (efw->dev_lock_count == 0) {
234 efw->dev_lock_count = -1;
240 spin_unlock_irq(&efw->lock);
246 hwdep_unlock(struct snd_efw *efw)
250 spin_lock_irq(&efw->lock);
252 if (efw->dev_lock_count == -1) {
253 efw->dev_lock_count = 0;
259 spin_unlock_irq(&efw->lock);
265 hwdep_release(struct snd_hwdep *hwdep, struct file *file)
267 struct snd_efw *efw = hwdep->private_data;
269 spin_lock_irq(&efw->lock);
270 if (efw->dev_lock_count == -1)
271 efw->dev_lock_count = 0;
272 spin_unlock_irq(&efw->lock);
278 hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
279 unsigned int cmd, unsigned long arg)
281 struct snd_efw *efw = hwdep->private_data;
284 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
285 return hwdep_get_info(efw, (void __user *)arg);
286 case SNDRV_FIREWIRE_IOCTL_LOCK:
287 return hwdep_lock(efw);
288 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
289 return hwdep_unlock(efw);
297 hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
298 unsigned int cmd, unsigned long arg)
300 return hwdep_ioctl(hwdep, file, cmd,
301 (unsigned long)compat_ptr(arg));
304 #define hwdep_compat_ioctl NULL
307 int snd_efw_create_hwdep_device(struct snd_efw *efw)
309 static const struct snd_hwdep_ops ops = {
311 .write = hwdep_write,
312 .release = hwdep_release,
314 .ioctl = hwdep_ioctl,
315 .ioctl_compat = hwdep_compat_ioctl,
317 struct snd_hwdep *hwdep;
320 err = snd_hwdep_new(efw->card, "Fireworks", 0, &hwdep);
323 strcpy(hwdep->name, "Fireworks");
324 hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREWORKS;
326 hwdep->private_data = efw;
327 hwdep->exclusive = true;