18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * digi00x-hwdep.c - a part of driver for Digidesign Digi 002/003 family
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2014-2015 Takashi Sakamoto
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * This codes give three functionality.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * 1.get firewire node information
128c2ecf20Sopenharmony_ci * 2.get notification about starting/stopping stream
138c2ecf20Sopenharmony_ci * 3.lock/unlock stream
148c2ecf20Sopenharmony_ci * 4.get asynchronous messaging
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "digi00x.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic long hwdep_read(struct snd_hwdep *hwdep, char __user *buf,  long count,
208c2ecf20Sopenharmony_ci		       loff_t *offset)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct snd_dg00x *dg00x = hwdep->private_data;
238c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
248c2ecf20Sopenharmony_ci	union snd_firewire_event event;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	spin_lock_irq(&dg00x->lock);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	while (!dg00x->dev_lock_changed && dg00x->msg == 0) {
298c2ecf20Sopenharmony_ci		prepare_to_wait(&dg00x->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
308c2ecf20Sopenharmony_ci		spin_unlock_irq(&dg00x->lock);
318c2ecf20Sopenharmony_ci		schedule();
328c2ecf20Sopenharmony_ci		finish_wait(&dg00x->hwdep_wait, &wait);
338c2ecf20Sopenharmony_ci		if (signal_pending(current))
348c2ecf20Sopenharmony_ci			return -ERESTARTSYS;
358c2ecf20Sopenharmony_ci		spin_lock_irq(&dg00x->lock);
368c2ecf20Sopenharmony_ci	}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	memset(&event, 0, sizeof(event));
398c2ecf20Sopenharmony_ci	if (dg00x->dev_lock_changed) {
408c2ecf20Sopenharmony_ci		event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
418c2ecf20Sopenharmony_ci		event.lock_status.status = (dg00x->dev_lock_count > 0);
428c2ecf20Sopenharmony_ci		dg00x->dev_lock_changed = false;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci		count = min_t(long, count, sizeof(event.lock_status));
458c2ecf20Sopenharmony_ci	} else {
468c2ecf20Sopenharmony_ci		event.digi00x_message.type =
478c2ecf20Sopenharmony_ci					SNDRV_FIREWIRE_EVENT_DIGI00X_MESSAGE;
488c2ecf20Sopenharmony_ci		event.digi00x_message.message = dg00x->msg;
498c2ecf20Sopenharmony_ci		dg00x->msg = 0;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci		count = min_t(long, count, sizeof(event.digi00x_message));
528c2ecf20Sopenharmony_ci	}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	spin_unlock_irq(&dg00x->lock);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	if (copy_to_user(buf, &event, count))
578c2ecf20Sopenharmony_ci		return -EFAULT;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	return count;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
638c2ecf20Sopenharmony_ci			       poll_table *wait)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct snd_dg00x *dg00x = hwdep->private_data;
668c2ecf20Sopenharmony_ci	__poll_t events;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	poll_wait(file, &dg00x->hwdep_wait, wait);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	spin_lock_irq(&dg00x->lock);
718c2ecf20Sopenharmony_ci	if (dg00x->dev_lock_changed || dg00x->msg)
728c2ecf20Sopenharmony_ci		events = EPOLLIN | EPOLLRDNORM;
738c2ecf20Sopenharmony_ci	else
748c2ecf20Sopenharmony_ci		events = 0;
758c2ecf20Sopenharmony_ci	spin_unlock_irq(&dg00x->lock);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return events;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic int hwdep_get_info(struct snd_dg00x *dg00x, void __user *arg)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct fw_device *dev = fw_parent_device(dg00x->unit);
838c2ecf20Sopenharmony_ci	struct snd_firewire_get_info info;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	memset(&info, 0, sizeof(info));
868c2ecf20Sopenharmony_ci	info.type = SNDRV_FIREWIRE_TYPE_DIGI00X;
878c2ecf20Sopenharmony_ci	info.card = dev->card->index;
888c2ecf20Sopenharmony_ci	*(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
898c2ecf20Sopenharmony_ci	*(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
908c2ecf20Sopenharmony_ci	strlcpy(info.device_name, dev_name(&dev->device),
918c2ecf20Sopenharmony_ci		sizeof(info.device_name));
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (copy_to_user(arg, &info, sizeof(info)))
948c2ecf20Sopenharmony_ci		return -EFAULT;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	return 0;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic int hwdep_lock(struct snd_dg00x *dg00x)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	int err;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	spin_lock_irq(&dg00x->lock);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (dg00x->dev_lock_count == 0) {
1068c2ecf20Sopenharmony_ci		dg00x->dev_lock_count = -1;
1078c2ecf20Sopenharmony_ci		err = 0;
1088c2ecf20Sopenharmony_ci	} else {
1098c2ecf20Sopenharmony_ci		err = -EBUSY;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	spin_unlock_irq(&dg00x->lock);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return err;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int hwdep_unlock(struct snd_dg00x *dg00x)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	int err;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	spin_lock_irq(&dg00x->lock);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (dg00x->dev_lock_count == -1) {
1248c2ecf20Sopenharmony_ci		dg00x->dev_lock_count = 0;
1258c2ecf20Sopenharmony_ci		err = 0;
1268c2ecf20Sopenharmony_ci	} else {
1278c2ecf20Sopenharmony_ci		err = -EBADFD;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	spin_unlock_irq(&dg00x->lock);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	return err;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	struct snd_dg00x *dg00x = hwdep->private_data;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	spin_lock_irq(&dg00x->lock);
1408c2ecf20Sopenharmony_ci	if (dg00x->dev_lock_count == -1)
1418c2ecf20Sopenharmony_ci		dg00x->dev_lock_count = 0;
1428c2ecf20Sopenharmony_ci	spin_unlock_irq(&dg00x->lock);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return 0;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
1488c2ecf20Sopenharmony_ci	    unsigned int cmd, unsigned long arg)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	struct snd_dg00x *dg00x = hwdep->private_data;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	switch (cmd) {
1538c2ecf20Sopenharmony_ci	case SNDRV_FIREWIRE_IOCTL_GET_INFO:
1548c2ecf20Sopenharmony_ci		return hwdep_get_info(dg00x, (void __user *)arg);
1558c2ecf20Sopenharmony_ci	case SNDRV_FIREWIRE_IOCTL_LOCK:
1568c2ecf20Sopenharmony_ci		return hwdep_lock(dg00x);
1578c2ecf20Sopenharmony_ci	case SNDRV_FIREWIRE_IOCTL_UNLOCK:
1588c2ecf20Sopenharmony_ci		return hwdep_unlock(dg00x);
1598c2ecf20Sopenharmony_ci	default:
1608c2ecf20Sopenharmony_ci		return -ENOIOCTLCMD;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
1658c2ecf20Sopenharmony_cistatic int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
1668c2ecf20Sopenharmony_ci			      unsigned int cmd, unsigned long arg)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	return hwdep_ioctl(hwdep, file, cmd,
1698c2ecf20Sopenharmony_ci			   (unsigned long)compat_ptr(arg));
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci#else
1728c2ecf20Sopenharmony_ci#define hwdep_compat_ioctl NULL
1738c2ecf20Sopenharmony_ci#endif
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciint snd_dg00x_create_hwdep_device(struct snd_dg00x *dg00x)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	static const struct snd_hwdep_ops ops = {
1788c2ecf20Sopenharmony_ci		.read		= hwdep_read,
1798c2ecf20Sopenharmony_ci		.release	= hwdep_release,
1808c2ecf20Sopenharmony_ci		.poll		= hwdep_poll,
1818c2ecf20Sopenharmony_ci		.ioctl		= hwdep_ioctl,
1828c2ecf20Sopenharmony_ci		.ioctl_compat	= hwdep_compat_ioctl,
1838c2ecf20Sopenharmony_ci	};
1848c2ecf20Sopenharmony_ci	struct snd_hwdep *hwdep;
1858c2ecf20Sopenharmony_ci	int err;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	err = snd_hwdep_new(dg00x->card, "Digi00x", 0, &hwdep);
1888c2ecf20Sopenharmony_ci	if (err < 0)
1898c2ecf20Sopenharmony_ci		return err;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	strcpy(hwdep->name, "Digi00x");
1928c2ecf20Sopenharmony_ci	hwdep->iface = SNDRV_HWDEP_IFACE_FW_DIGI00X;
1938c2ecf20Sopenharmony_ci	hwdep->ops = ops;
1948c2ecf20Sopenharmony_ci	hwdep->private_data = dg00x;
1958c2ecf20Sopenharmony_ci	hwdep->exclusive = true;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return err;
1988c2ecf20Sopenharmony_ci}
199