1 // SPDX-License-Identifier: GPL-2.0-only
3 * oxfw_hwdep.c - a part of driver for OXFW970/971 based devices
5 * Copyright (c) 2014 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 hwdep_read(struct snd_hwdep
*hwdep
, char __user
*buf
, long count
,
21 struct snd_oxfw
*oxfw
= hwdep
->private_data
;
23 union snd_firewire_event event
;
25 spin_lock_irq(&oxfw
->lock
);
27 while (!oxfw
->dev_lock_changed
) {
28 prepare_to_wait(&oxfw
->hwdep_wait
, &wait
, TASK_INTERRUPTIBLE
);
29 spin_unlock_irq(&oxfw
->lock
);
31 finish_wait(&oxfw
->hwdep_wait
, &wait
);
32 if (signal_pending(current
))
34 spin_lock_irq(&oxfw
->lock
);
37 memset(&event
, 0, sizeof(event
));
38 if (oxfw
->dev_lock_changed
) {
39 event
.lock_status
.type
= SNDRV_FIREWIRE_EVENT_LOCK_STATUS
;
40 event
.lock_status
.status
= (oxfw
->dev_lock_count
> 0);
41 oxfw
->dev_lock_changed
= false;
43 count
= min_t(long, count
, sizeof(event
.lock_status
));
46 spin_unlock_irq(&oxfw
->lock
);
48 if (copy_to_user(buf
, &event
, count
))
54 static __poll_t
hwdep_poll(struct snd_hwdep
*hwdep
, struct file
*file
,
57 struct snd_oxfw
*oxfw
= hwdep
->private_data
;
60 poll_wait(file
, &oxfw
->hwdep_wait
, wait
);
62 spin_lock_irq(&oxfw
->lock
);
63 if (oxfw
->dev_lock_changed
)
64 events
= EPOLLIN
| EPOLLRDNORM
;
67 spin_unlock_irq(&oxfw
->lock
);
72 static int hwdep_get_info(struct snd_oxfw
*oxfw
, void __user
*arg
)
74 struct fw_device
*dev
= fw_parent_device(oxfw
->unit
);
75 struct snd_firewire_get_info info
;
77 memset(&info
, 0, sizeof(info
));
78 info
.type
= SNDRV_FIREWIRE_TYPE_OXFW
;
79 info
.card
= dev
->card
->index
;
80 *(__be32
*)&info
.guid
[0] = cpu_to_be32(dev
->config_rom
[3]);
81 *(__be32
*)&info
.guid
[4] = cpu_to_be32(dev
->config_rom
[4]);
82 strlcpy(info
.device_name
, dev_name(&dev
->device
),
83 sizeof(info
.device_name
));
85 if (copy_to_user(arg
, &info
, sizeof(info
)))
91 static int hwdep_lock(struct snd_oxfw
*oxfw
)
95 spin_lock_irq(&oxfw
->lock
);
97 if (oxfw
->dev_lock_count
== 0) {
98 oxfw
->dev_lock_count
= -1;
104 spin_unlock_irq(&oxfw
->lock
);
109 static int hwdep_unlock(struct snd_oxfw
*oxfw
)
113 spin_lock_irq(&oxfw
->lock
);
115 if (oxfw
->dev_lock_count
== -1) {
116 oxfw
->dev_lock_count
= 0;
122 spin_unlock_irq(&oxfw
->lock
);
127 static int hwdep_release(struct snd_hwdep
*hwdep
, struct file
*file
)
129 struct snd_oxfw
*oxfw
= hwdep
->private_data
;
131 spin_lock_irq(&oxfw
->lock
);
132 if (oxfw
->dev_lock_count
== -1)
133 oxfw
->dev_lock_count
= 0;
134 spin_unlock_irq(&oxfw
->lock
);
139 static int hwdep_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
140 unsigned int cmd
, unsigned long arg
)
142 struct snd_oxfw
*oxfw
= hwdep
->private_data
;
145 case SNDRV_FIREWIRE_IOCTL_GET_INFO
:
146 return hwdep_get_info(oxfw
, (void __user
*)arg
);
147 case SNDRV_FIREWIRE_IOCTL_LOCK
:
148 return hwdep_lock(oxfw
);
149 case SNDRV_FIREWIRE_IOCTL_UNLOCK
:
150 return hwdep_unlock(oxfw
);
157 static int hwdep_compat_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
158 unsigned int cmd
, unsigned long arg
)
160 return hwdep_ioctl(hwdep
, file
, cmd
,
161 (unsigned long)compat_ptr(arg
));
164 #define hwdep_compat_ioctl NULL
167 int snd_oxfw_create_hwdep(struct snd_oxfw
*oxfw
)
169 static const struct snd_hwdep_ops hwdep_ops
= {
171 .release
= hwdep_release
,
173 .ioctl
= hwdep_ioctl
,
174 .ioctl_compat
= hwdep_compat_ioctl
,
176 struct snd_hwdep
*hwdep
;
179 err
= snd_hwdep_new(oxfw
->card
, oxfw
->card
->driver
, 0, &hwdep
);
182 strcpy(hwdep
->name
, oxfw
->card
->driver
);
183 hwdep
->iface
= SNDRV_HWDEP_IFACE_FW_OXFW
;
184 hwdep
->ops
= hwdep_ops
;
185 hwdep
->private_data
= oxfw
;
186 hwdep
->exclusive
= true;