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
)
20 __releases(&tscm
->lock
)
22 struct snd_firewire_event_lock_status event
= {
23 .type
= SNDRV_FIREWIRE_EVENT_LOCK_STATUS
,
26 event
.status
= (tscm
->dev_lock_count
> 0);
27 tscm
->dev_lock_changed
= false;
28 count
= min_t(long, count
, sizeof(event
));
30 spin_unlock_irq(&tscm
->lock
);
32 if (copy_to_user(buf
, &event
, count
))
38 static long tscm_hwdep_read_queue(struct snd_tscm
*tscm
, char __user
*buf
,
39 long remained
, loff_t
*offset
)
40 __releases(&tscm
->lock
)
42 char __user
*pos
= buf
;
43 unsigned int type
= SNDRV_FIREWIRE_EVENT_TASCAM_CONTROL
;
44 struct snd_firewire_tascam_change
*entries
= tscm
->queue
;
47 // At least, one control event can be copied.
48 if (remained
< sizeof(type
) + sizeof(*entries
)) {
49 spin_unlock_irq(&tscm
->lock
);
53 // Copy the type field later.
55 remained
-= sizeof(type
);
59 unsigned int head_pos
;
60 unsigned int tail_pos
;
63 if (tscm
->pull_pos
== tscm
->push_pos
)
65 else if (tscm
->pull_pos
< tscm
->push_pos
)
66 tail_pos
= tscm
->push_pos
;
68 tail_pos
= SND_TSCM_QUEUE_COUNT
;
69 head_pos
= tscm
->pull_pos
;
71 length
= (tail_pos
- head_pos
) * sizeof(*entries
);
72 if (remained
< length
)
73 length
= rounddown(remained
, sizeof(*entries
));
77 spin_unlock_irq(&tscm
->lock
);
78 if (copy_to_user(pos
, &entries
[head_pos
], length
))
81 spin_lock_irq(&tscm
->lock
);
83 tscm
->pull_pos
= tail_pos
% SND_TSCM_QUEUE_COUNT
;
90 spin_unlock_irq(&tscm
->lock
);
92 if (copy_to_user(buf
, &type
, sizeof(type
)))
98 static long hwdep_read(struct snd_hwdep
*hwdep
, char __user
*buf
, long count
,
101 struct snd_tscm
*tscm
= hwdep
->private_data
;
104 spin_lock_irq(&tscm
->lock
);
106 while (!tscm
->dev_lock_changed
&& tscm
->push_pos
== tscm
->pull_pos
) {
107 prepare_to_wait(&tscm
->hwdep_wait
, &wait
, TASK_INTERRUPTIBLE
);
108 spin_unlock_irq(&tscm
->lock
);
110 finish_wait(&tscm
->hwdep_wait
, &wait
);
111 if (signal_pending(current
))
113 spin_lock_irq(&tscm
->lock
);
116 // NOTE: The acquired lock should be released in callee side.
117 if (tscm
->dev_lock_changed
) {
118 count
= tscm_hwdep_read_locked(tscm
, buf
, count
, offset
);
119 } else if (tscm
->push_pos
!= tscm
->pull_pos
) {
120 count
= tscm_hwdep_read_queue(tscm
, buf
, count
, offset
);
122 spin_unlock_irq(&tscm
->lock
);
129 static __poll_t
hwdep_poll(struct snd_hwdep
*hwdep
, struct file
*file
,
132 struct snd_tscm
*tscm
= hwdep
->private_data
;
135 poll_wait(file
, &tscm
->hwdep_wait
, wait
);
137 spin_lock_irq(&tscm
->lock
);
138 if (tscm
->dev_lock_changed
|| tscm
->push_pos
!= tscm
->pull_pos
)
139 events
= EPOLLIN
| EPOLLRDNORM
;
142 spin_unlock_irq(&tscm
->lock
);
147 static int hwdep_get_info(struct snd_tscm
*tscm
, void __user
*arg
)
149 struct fw_device
*dev
= fw_parent_device(tscm
->unit
);
150 struct snd_firewire_get_info info
;
152 memset(&info
, 0, sizeof(info
));
153 info
.type
= SNDRV_FIREWIRE_TYPE_TASCAM
;
154 info
.card
= dev
->card
->index
;
155 *(__be32
*)&info
.guid
[0] = cpu_to_be32(dev
->config_rom
[3]);
156 *(__be32
*)&info
.guid
[4] = cpu_to_be32(dev
->config_rom
[4]);
157 strlcpy(info
.device_name
, dev_name(&dev
->device
),
158 sizeof(info
.device_name
));
160 if (copy_to_user(arg
, &info
, sizeof(info
)))
166 static int hwdep_lock(struct snd_tscm
*tscm
)
170 spin_lock_irq(&tscm
->lock
);
172 if (tscm
->dev_lock_count
== 0) {
173 tscm
->dev_lock_count
= -1;
179 spin_unlock_irq(&tscm
->lock
);
184 static int hwdep_unlock(struct snd_tscm
*tscm
)
188 spin_lock_irq(&tscm
->lock
);
190 if (tscm
->dev_lock_count
== -1) {
191 tscm
->dev_lock_count
= 0;
197 spin_unlock_irq(&tscm
->lock
);
202 static int tscm_hwdep_state(struct snd_tscm
*tscm
, void __user
*arg
)
204 if (copy_to_user(arg
, tscm
->state
, sizeof(tscm
->state
)))
210 static int hwdep_release(struct snd_hwdep
*hwdep
, struct file
*file
)
212 struct snd_tscm
*tscm
= hwdep
->private_data
;
214 spin_lock_irq(&tscm
->lock
);
215 if (tscm
->dev_lock_count
== -1)
216 tscm
->dev_lock_count
= 0;
217 spin_unlock_irq(&tscm
->lock
);
222 static int hwdep_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
223 unsigned int cmd
, unsigned long arg
)
225 struct snd_tscm
*tscm
= hwdep
->private_data
;
228 case SNDRV_FIREWIRE_IOCTL_GET_INFO
:
229 return hwdep_get_info(tscm
, (void __user
*)arg
);
230 case SNDRV_FIREWIRE_IOCTL_LOCK
:
231 return hwdep_lock(tscm
);
232 case SNDRV_FIREWIRE_IOCTL_UNLOCK
:
233 return hwdep_unlock(tscm
);
234 case SNDRV_FIREWIRE_IOCTL_TASCAM_STATE
:
235 return tscm_hwdep_state(tscm
, (void __user
*)arg
);
242 static int hwdep_compat_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
243 unsigned int cmd
, unsigned long arg
)
245 return hwdep_ioctl(hwdep
, file
, cmd
,
246 (unsigned long)compat_ptr(arg
));
249 #define hwdep_compat_ioctl NULL
252 int snd_tscm_create_hwdep_device(struct snd_tscm
*tscm
)
254 static const struct snd_hwdep_ops ops
= {
256 .release
= hwdep_release
,
258 .ioctl
= hwdep_ioctl
,
259 .ioctl_compat
= hwdep_compat_ioctl
,
261 struct snd_hwdep
*hwdep
;
264 err
= snd_hwdep_new(tscm
->card
, "Tascam", 0, &hwdep
);
268 strcpy(hwdep
->name
, "Tascam");
269 hwdep
->iface
= SNDRV_HWDEP_IFACE_FW_TASCAM
;
271 hwdep
->private_data
= tscm
;
272 hwdep
->exclusive
= true;