2 * motu-hwdep.c - a part of driver for MOTU FireWire series
4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 * Licensed under the terms of the GNU General Public License, version 2.
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
20 static long hwdep_read(struct snd_hwdep
*hwdep
, char __user
*buf
, long count
,
23 struct snd_motu
*motu
= hwdep
->private_data
;
25 union snd_firewire_event event
;
27 spin_lock_irq(&motu
->lock
);
29 while (!motu
->dev_lock_changed
&& motu
->msg
== 0) {
30 prepare_to_wait(&motu
->hwdep_wait
, &wait
, TASK_INTERRUPTIBLE
);
31 spin_unlock_irq(&motu
->lock
);
33 finish_wait(&motu
->hwdep_wait
, &wait
);
34 if (signal_pending(current
))
36 spin_lock_irq(&motu
->lock
);
39 memset(&event
, 0, sizeof(event
));
40 if (motu
->dev_lock_changed
) {
41 event
.lock_status
.type
= SNDRV_FIREWIRE_EVENT_LOCK_STATUS
;
42 event
.lock_status
.status
= (motu
->dev_lock_count
> 0);
43 motu
->dev_lock_changed
= false;
45 count
= min_t(long, count
, sizeof(event
.lock_status
));
47 event
.motu_notification
.type
= SNDRV_FIREWIRE_EVENT_MOTU_NOTIFICATION
;
48 event
.motu_notification
.message
= motu
->msg
;
51 count
= min_t(long, count
, sizeof(event
.motu_notification
));
54 spin_unlock_irq(&motu
->lock
);
56 if (copy_to_user(buf
, &event
, count
))
62 static unsigned int hwdep_poll(struct snd_hwdep
*hwdep
, struct file
*file
,
65 struct snd_motu
*motu
= hwdep
->private_data
;
68 poll_wait(file
, &motu
->hwdep_wait
, wait
);
70 spin_lock_irq(&motu
->lock
);
71 if (motu
->dev_lock_changed
|| motu
->msg
)
72 events
= POLLIN
| POLLRDNORM
;
75 spin_unlock_irq(&motu
->lock
);
77 return events
| POLLOUT
;
80 static int hwdep_get_info(struct snd_motu
*motu
, void __user
*arg
)
82 struct fw_device
*dev
= fw_parent_device(motu
->unit
);
83 struct snd_firewire_get_info info
;
85 memset(&info
, 0, sizeof(info
));
86 info
.type
= SNDRV_FIREWIRE_TYPE_MOTU
;
87 info
.card
= dev
->card
->index
;
88 *(__be32
*)&info
.guid
[0] = cpu_to_be32(dev
->config_rom
[3]);
89 *(__be32
*)&info
.guid
[4] = cpu_to_be32(dev
->config_rom
[4]);
90 strlcpy(info
.device_name
, dev_name(&dev
->device
),
91 sizeof(info
.device_name
));
93 if (copy_to_user(arg
, &info
, sizeof(info
)))
99 static int hwdep_lock(struct snd_motu
*motu
)
103 spin_lock_irq(&motu
->lock
);
105 if (motu
->dev_lock_count
== 0) {
106 motu
->dev_lock_count
= -1;
112 spin_unlock_irq(&motu
->lock
);
117 static int hwdep_unlock(struct snd_motu
*motu
)
121 spin_lock_irq(&motu
->lock
);
123 if (motu
->dev_lock_count
== -1) {
124 motu
->dev_lock_count
= 0;
130 spin_unlock_irq(&motu
->lock
);
135 static int hwdep_release(struct snd_hwdep
*hwdep
, struct file
*file
)
137 struct snd_motu
*motu
= hwdep
->private_data
;
139 spin_lock_irq(&motu
->lock
);
140 if (motu
->dev_lock_count
== -1)
141 motu
->dev_lock_count
= 0;
142 spin_unlock_irq(&motu
->lock
);
147 static int hwdep_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
148 unsigned int cmd
, unsigned long arg
)
150 struct snd_motu
*motu
= hwdep
->private_data
;
153 case SNDRV_FIREWIRE_IOCTL_GET_INFO
:
154 return hwdep_get_info(motu
, (void __user
*)arg
);
155 case SNDRV_FIREWIRE_IOCTL_LOCK
:
156 return hwdep_lock(motu
);
157 case SNDRV_FIREWIRE_IOCTL_UNLOCK
:
158 return hwdep_unlock(motu
);
165 static int hwdep_compat_ioctl(struct snd_hwdep
*hwdep
, struct file
*file
,
166 unsigned int cmd
, unsigned long arg
)
168 return hwdep_ioctl(hwdep
, file
, cmd
,
169 (unsigned long)compat_ptr(arg
));
172 #define hwdep_compat_ioctl NULL
175 int snd_motu_create_hwdep_device(struct snd_motu
*motu
)
177 static const struct snd_hwdep_ops ops
= {
179 .release
= hwdep_release
,
181 .ioctl
= hwdep_ioctl
,
182 .ioctl_compat
= hwdep_compat_ioctl
,
184 struct snd_hwdep
*hwdep
;
187 err
= snd_hwdep_new(motu
->card
, motu
->card
->driver
, 0, &hwdep
);
191 strcpy(hwdep
->name
, "MOTU");
192 hwdep
->iface
= SNDRV_HWDEP_IFACE_FW_MOTU
;
194 hwdep
->private_data
= motu
;
195 hwdep
->exclusive
= true;