sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / sound / firewire / fireworks / fireworks_hwdep.c
blob2e1d9a23920c0c3ebf4b6edda5019b672dab4e82
1 /*
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.
7 */
9 /*
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"
22 static long
23 hwdep_read_resp_buf(struct snd_efw *efw, char __user *buf, long remained,
24 loff_t *offset)
26 unsigned int length, till_end, type;
27 struct snd_efw_transaction *t;
28 u8 *pull_ptr;
29 long count = 0;
31 if (remained < sizeof(type) + sizeof(struct snd_efw_transaction))
32 return -ENOSPC;
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)))
37 return -EFAULT;
38 remained -= sizeof(type);
39 buf += sizeof(type);
41 /* write into buffer as many responses as possible */
42 spin_lock_irq(&efw->lock);
45 * When another task reaches here during this task's access to user
46 * space, it picks up current position in buffer and can read the same
47 * series of responses.
49 pull_ptr = efw->pull_ptr;
51 while (efw->push_ptr != pull_ptr) {
52 t = (struct snd_efw_transaction *)(pull_ptr);
53 length = be32_to_cpu(t->length) * sizeof(__be32);
55 /* confirm enough space for this response */
56 if (remained < length)
57 break;
59 /* copy from ring buffer to user buffer */
60 while (length > 0) {
61 till_end = snd_efw_resp_buf_size -
62 (unsigned int)(pull_ptr - efw->resp_buf);
63 till_end = min_t(unsigned int, length, till_end);
65 spin_unlock_irq(&efw->lock);
67 if (copy_to_user(buf, pull_ptr, till_end))
68 return -EFAULT;
70 spin_lock_irq(&efw->lock);
72 pull_ptr += till_end;
73 if (pull_ptr >= efw->resp_buf + snd_efw_resp_buf_size)
74 pull_ptr -= snd_efw_resp_buf_size;
76 length -= till_end;
77 buf += till_end;
78 count += till_end;
79 remained -= till_end;
84 * All of tasks can read from the buffer nearly simultaneously, but the
85 * last position for each task is different depending on the length of
86 * given buffer. Here, for simplicity, a position of buffer is set by
87 * the latest task. It's better for a listening application to allow one
88 * thread to read from the buffer. Unless, each task can read different
89 * sequence of responses depending on variation of buffer length.
91 efw->pull_ptr = pull_ptr;
93 spin_unlock_irq(&efw->lock);
95 return count;
98 static long
99 hwdep_read_locked(struct snd_efw *efw, char __user *buf, long count,
100 loff_t *offset)
102 union snd_firewire_event event = {
103 .lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
106 spin_lock_irq(&efw->lock);
108 event.lock_status.status = (efw->dev_lock_count > 0);
109 efw->dev_lock_changed = false;
111 spin_unlock_irq(&efw->lock);
113 count = min_t(long, count, sizeof(event.lock_status));
115 if (copy_to_user(buf, &event, count))
116 return -EFAULT;
118 return count;
121 static long
122 hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
123 loff_t *offset)
125 struct snd_efw *efw = hwdep->private_data;
126 DEFINE_WAIT(wait);
127 bool dev_lock_changed;
128 bool queued;
130 spin_lock_irq(&efw->lock);
132 dev_lock_changed = efw->dev_lock_changed;
133 queued = efw->push_ptr != efw->pull_ptr;
135 while (!dev_lock_changed && !queued) {
136 prepare_to_wait(&efw->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
137 spin_unlock_irq(&efw->lock);
138 schedule();
139 finish_wait(&efw->hwdep_wait, &wait);
140 if (signal_pending(current))
141 return -ERESTARTSYS;
142 spin_lock_irq(&efw->lock);
143 dev_lock_changed = efw->dev_lock_changed;
144 queued = efw->push_ptr != efw->pull_ptr;
147 spin_unlock_irq(&efw->lock);
149 if (dev_lock_changed)
150 count = hwdep_read_locked(efw, buf, count, offset);
151 else if (queued)
152 count = hwdep_read_resp_buf(efw, buf, count, offset);
154 return count;
157 static long
158 hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
159 loff_t *offset)
161 struct snd_efw *efw = hwdep->private_data;
162 u32 seqnum;
163 u8 *buf;
165 if (count < sizeof(struct snd_efw_transaction) ||
166 SND_EFW_RESPONSE_MAXIMUM_BYTES < count)
167 return -EINVAL;
169 buf = memdup_user(data, count);
170 if (IS_ERR(buf))
171 return PTR_ERR(buf);
173 /* check seqnum is not for kernel-land */
174 seqnum = be32_to_cpu(((struct snd_efw_transaction *)buf)->seqnum);
175 if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX) {
176 count = -EINVAL;
177 goto end;
180 if (snd_efw_transaction_cmd(efw->unit, buf, count) < 0)
181 count = -EIO;
182 end:
183 kfree(buf);
184 return count;
187 static unsigned int
188 hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
190 struct snd_efw *efw = hwdep->private_data;
191 unsigned int events;
193 poll_wait(file, &efw->hwdep_wait, wait);
195 spin_lock_irq(&efw->lock);
196 if (efw->dev_lock_changed || efw->pull_ptr != efw->push_ptr)
197 events = POLLIN | POLLRDNORM;
198 else
199 events = 0;
200 spin_unlock_irq(&efw->lock);
202 return events | POLLOUT;
205 static int
206 hwdep_get_info(struct snd_efw *efw, void __user *arg)
208 struct fw_device *dev = fw_parent_device(efw->unit);
209 struct snd_firewire_get_info info;
211 memset(&info, 0, sizeof(info));
212 info.type = SNDRV_FIREWIRE_TYPE_FIREWORKS;
213 info.card = dev->card->index;
214 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
215 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
216 strlcpy(info.device_name, dev_name(&dev->device),
217 sizeof(info.device_name));
219 if (copy_to_user(arg, &info, sizeof(info)))
220 return -EFAULT;
222 return 0;
225 static int
226 hwdep_lock(struct snd_efw *efw)
228 int err;
230 spin_lock_irq(&efw->lock);
232 if (efw->dev_lock_count == 0) {
233 efw->dev_lock_count = -1;
234 err = 0;
235 } else {
236 err = -EBUSY;
239 spin_unlock_irq(&efw->lock);
241 return err;
244 static int
245 hwdep_unlock(struct snd_efw *efw)
247 int err;
249 spin_lock_irq(&efw->lock);
251 if (efw->dev_lock_count == -1) {
252 efw->dev_lock_count = 0;
253 err = 0;
254 } else {
255 err = -EBADFD;
258 spin_unlock_irq(&efw->lock);
260 return err;
263 static int
264 hwdep_release(struct snd_hwdep *hwdep, struct file *file)
266 struct snd_efw *efw = hwdep->private_data;
268 spin_lock_irq(&efw->lock);
269 if (efw->dev_lock_count == -1)
270 efw->dev_lock_count = 0;
271 spin_unlock_irq(&efw->lock);
273 return 0;
276 static int
277 hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
278 unsigned int cmd, unsigned long arg)
280 struct snd_efw *efw = hwdep->private_data;
282 switch (cmd) {
283 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
284 return hwdep_get_info(efw, (void __user *)arg);
285 case SNDRV_FIREWIRE_IOCTL_LOCK:
286 return hwdep_lock(efw);
287 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
288 return hwdep_unlock(efw);
289 default:
290 return -ENOIOCTLCMD;
294 #ifdef CONFIG_COMPAT
295 static int
296 hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
297 unsigned int cmd, unsigned long arg)
299 return hwdep_ioctl(hwdep, file, cmd,
300 (unsigned long)compat_ptr(arg));
302 #else
303 #define hwdep_compat_ioctl NULL
304 #endif
306 static const struct snd_hwdep_ops hwdep_ops = {
307 .read = hwdep_read,
308 .write = hwdep_write,
309 .release = hwdep_release,
310 .poll = hwdep_poll,
311 .ioctl = hwdep_ioctl,
312 .ioctl_compat = hwdep_compat_ioctl,
315 int snd_efw_create_hwdep_device(struct snd_efw *efw)
317 struct snd_hwdep *hwdep;
318 int err;
320 err = snd_hwdep_new(efw->card, "Fireworks", 0, &hwdep);
321 if (err < 0)
322 goto end;
323 strcpy(hwdep->name, "Fireworks");
324 hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREWORKS;
325 hwdep->ops = hwdep_ops;
326 hwdep->private_data = efw;
327 hwdep->exclusive = true;
328 end:
329 return err;