xref: /OK3568_Linux_fs/kernel/sound/firewire/motu/motu-hwdep.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * motu-hwdep.c - a part of driver for MOTU FireWire series
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * This codes have five functionalities.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * 1.get information about firewire node
12*4882a593Smuzhiyun  * 2.get notification about starting/stopping stream
13*4882a593Smuzhiyun  * 3.lock/unlock streaming
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "motu.h"
18*4882a593Smuzhiyun 
hwdep_read(struct snd_hwdep * hwdep,char __user * buf,long count,loff_t * offset)19*4882a593Smuzhiyun static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
20*4882a593Smuzhiyun 		       loff_t *offset)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	struct snd_motu *motu = hwdep->private_data;
23*4882a593Smuzhiyun 	DEFINE_WAIT(wait);
24*4882a593Smuzhiyun 	union snd_firewire_event event;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	spin_lock_irq(&motu->lock);
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	while (!motu->dev_lock_changed && motu->msg == 0) {
29*4882a593Smuzhiyun 		prepare_to_wait(&motu->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
30*4882a593Smuzhiyun 		spin_unlock_irq(&motu->lock);
31*4882a593Smuzhiyun 		schedule();
32*4882a593Smuzhiyun 		finish_wait(&motu->hwdep_wait, &wait);
33*4882a593Smuzhiyun 		if (signal_pending(current))
34*4882a593Smuzhiyun 			return -ERESTARTSYS;
35*4882a593Smuzhiyun 		spin_lock_irq(&motu->lock);
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	memset(&event, 0, sizeof(event));
39*4882a593Smuzhiyun 	if (motu->dev_lock_changed) {
40*4882a593Smuzhiyun 		event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
41*4882a593Smuzhiyun 		event.lock_status.status = (motu->dev_lock_count > 0);
42*4882a593Smuzhiyun 		motu->dev_lock_changed = false;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 		count = min_t(long, count, sizeof(event.lock_status));
45*4882a593Smuzhiyun 	} else {
46*4882a593Smuzhiyun 		event.motu_notification.type = SNDRV_FIREWIRE_EVENT_MOTU_NOTIFICATION;
47*4882a593Smuzhiyun 		event.motu_notification.message = motu->msg;
48*4882a593Smuzhiyun 		motu->msg = 0;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 		count = min_t(long, count, sizeof(event.motu_notification));
51*4882a593Smuzhiyun 	}
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	spin_unlock_irq(&motu->lock);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	if (copy_to_user(buf, &event, count))
56*4882a593Smuzhiyun 		return -EFAULT;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	return count;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
hwdep_poll(struct snd_hwdep * hwdep,struct file * file,poll_table * wait)61*4882a593Smuzhiyun static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
62*4882a593Smuzhiyun 			       poll_table *wait)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	struct snd_motu *motu = hwdep->private_data;
65*4882a593Smuzhiyun 	__poll_t events;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	poll_wait(file, &motu->hwdep_wait, wait);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	spin_lock_irq(&motu->lock);
70*4882a593Smuzhiyun 	if (motu->dev_lock_changed || motu->msg)
71*4882a593Smuzhiyun 		events = EPOLLIN | EPOLLRDNORM;
72*4882a593Smuzhiyun 	else
73*4882a593Smuzhiyun 		events = 0;
74*4882a593Smuzhiyun 	spin_unlock_irq(&motu->lock);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return events | EPOLLOUT;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
hwdep_get_info(struct snd_motu * motu,void __user * arg)79*4882a593Smuzhiyun static int hwdep_get_info(struct snd_motu *motu, void __user *arg)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	struct fw_device *dev = fw_parent_device(motu->unit);
82*4882a593Smuzhiyun 	struct snd_firewire_get_info info;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	memset(&info, 0, sizeof(info));
85*4882a593Smuzhiyun 	info.type = SNDRV_FIREWIRE_TYPE_MOTU;
86*4882a593Smuzhiyun 	info.card = dev->card->index;
87*4882a593Smuzhiyun 	*(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
88*4882a593Smuzhiyun 	*(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
89*4882a593Smuzhiyun 	strlcpy(info.device_name, dev_name(&dev->device),
90*4882a593Smuzhiyun 		sizeof(info.device_name));
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (copy_to_user(arg, &info, sizeof(info)))
93*4882a593Smuzhiyun 		return -EFAULT;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
hwdep_lock(struct snd_motu * motu)98*4882a593Smuzhiyun static int hwdep_lock(struct snd_motu *motu)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	int err;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	spin_lock_irq(&motu->lock);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (motu->dev_lock_count == 0) {
105*4882a593Smuzhiyun 		motu->dev_lock_count = -1;
106*4882a593Smuzhiyun 		err = 0;
107*4882a593Smuzhiyun 	} else {
108*4882a593Smuzhiyun 		err = -EBUSY;
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	spin_unlock_irq(&motu->lock);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	return err;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
hwdep_unlock(struct snd_motu * motu)116*4882a593Smuzhiyun static int hwdep_unlock(struct snd_motu *motu)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	int err;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	spin_lock_irq(&motu->lock);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	if (motu->dev_lock_count == -1) {
123*4882a593Smuzhiyun 		motu->dev_lock_count = 0;
124*4882a593Smuzhiyun 		err = 0;
125*4882a593Smuzhiyun 	} else {
126*4882a593Smuzhiyun 		err = -EBADFD;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	spin_unlock_irq(&motu->lock);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	return err;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
hwdep_release(struct snd_hwdep * hwdep,struct file * file)134*4882a593Smuzhiyun static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct snd_motu *motu = hwdep->private_data;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	spin_lock_irq(&motu->lock);
139*4882a593Smuzhiyun 	if (motu->dev_lock_count == -1)
140*4882a593Smuzhiyun 		motu->dev_lock_count = 0;
141*4882a593Smuzhiyun 	spin_unlock_irq(&motu->lock);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	return 0;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
hwdep_ioctl(struct snd_hwdep * hwdep,struct file * file,unsigned int cmd,unsigned long arg)146*4882a593Smuzhiyun static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
147*4882a593Smuzhiyun 	    unsigned int cmd, unsigned long arg)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	struct snd_motu *motu = hwdep->private_data;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	switch (cmd) {
152*4882a593Smuzhiyun 	case SNDRV_FIREWIRE_IOCTL_GET_INFO:
153*4882a593Smuzhiyun 		return hwdep_get_info(motu, (void __user *)arg);
154*4882a593Smuzhiyun 	case SNDRV_FIREWIRE_IOCTL_LOCK:
155*4882a593Smuzhiyun 		return hwdep_lock(motu);
156*4882a593Smuzhiyun 	case SNDRV_FIREWIRE_IOCTL_UNLOCK:
157*4882a593Smuzhiyun 		return hwdep_unlock(motu);
158*4882a593Smuzhiyun 	default:
159*4882a593Smuzhiyun 		return -ENOIOCTLCMD;
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
hwdep_compat_ioctl(struct snd_hwdep * hwdep,struct file * file,unsigned int cmd,unsigned long arg)164*4882a593Smuzhiyun static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
165*4882a593Smuzhiyun 			      unsigned int cmd, unsigned long arg)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	return hwdep_ioctl(hwdep, file, cmd,
168*4882a593Smuzhiyun 			   (unsigned long)compat_ptr(arg));
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun #else
171*4882a593Smuzhiyun #define hwdep_compat_ioctl NULL
172*4882a593Smuzhiyun #endif
173*4882a593Smuzhiyun 
snd_motu_create_hwdep_device(struct snd_motu * motu)174*4882a593Smuzhiyun int snd_motu_create_hwdep_device(struct snd_motu *motu)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	static const struct snd_hwdep_ops ops = {
177*4882a593Smuzhiyun 		.read		= hwdep_read,
178*4882a593Smuzhiyun 		.release	= hwdep_release,
179*4882a593Smuzhiyun 		.poll		= hwdep_poll,
180*4882a593Smuzhiyun 		.ioctl		= hwdep_ioctl,
181*4882a593Smuzhiyun 		.ioctl_compat	= hwdep_compat_ioctl,
182*4882a593Smuzhiyun 	};
183*4882a593Smuzhiyun 	struct snd_hwdep *hwdep;
184*4882a593Smuzhiyun 	int err;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	err = snd_hwdep_new(motu->card, motu->card->driver, 0, &hwdep);
187*4882a593Smuzhiyun 	if (err < 0)
188*4882a593Smuzhiyun 		return err;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	strcpy(hwdep->name, "MOTU");
191*4882a593Smuzhiyun 	hwdep->iface = SNDRV_HWDEP_IFACE_FW_MOTU;
192*4882a593Smuzhiyun 	hwdep->ops = ops;
193*4882a593Smuzhiyun 	hwdep->private_data = motu;
194*4882a593Smuzhiyun 	hwdep->exclusive = true;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	return 0;
197*4882a593Smuzhiyun }
198