motu-hwdep.c (6907B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * motu-hwdep.c - a part of driver for MOTU FireWire series 4 * 5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> 6 */ 7 8/* 9 * This codes have five functionalities. 10 * 11 * 1.get information about firewire node 12 * 2.get notification about starting/stopping stream 13 * 3.lock/unlock streaming 14 * 15 */ 16 17#include "motu.h" 18 19static bool has_dsp_event(struct snd_motu *motu) 20{ 21 if (motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP) 22 return (snd_motu_register_dsp_message_parser_count_event(motu) > 0); 23 else 24 return false; 25} 26 27static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count, 28 loff_t *offset) 29{ 30 struct snd_motu *motu = hwdep->private_data; 31 DEFINE_WAIT(wait); 32 union snd_firewire_event event; 33 34 spin_lock_irq(&motu->lock); 35 36 while (!motu->dev_lock_changed && motu->msg == 0 && !has_dsp_event(motu)) { 37 prepare_to_wait(&motu->hwdep_wait, &wait, TASK_INTERRUPTIBLE); 38 spin_unlock_irq(&motu->lock); 39 schedule(); 40 finish_wait(&motu->hwdep_wait, &wait); 41 if (signal_pending(current)) 42 return -ERESTARTSYS; 43 spin_lock_irq(&motu->lock); 44 } 45 46 memset(&event, 0, sizeof(event)); 47 if (motu->dev_lock_changed) { 48 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS; 49 event.lock_status.status = (motu->dev_lock_count > 0); 50 motu->dev_lock_changed = false; 51 spin_unlock_irq(&motu->lock); 52 53 count = min_t(long, count, sizeof(event)); 54 if (copy_to_user(buf, &event, count)) 55 return -EFAULT; 56 } else if (motu->msg > 0) { 57 event.motu_notification.type = SNDRV_FIREWIRE_EVENT_MOTU_NOTIFICATION; 58 event.motu_notification.message = motu->msg; 59 motu->msg = 0; 60 spin_unlock_irq(&motu->lock); 61 62 count = min_t(long, count, sizeof(event)); 63 if (copy_to_user(buf, &event, count)) 64 return -EFAULT; 65 } else if (has_dsp_event(motu)) { 66 size_t consumed = 0; 67 u32 __user *ptr; 68 u32 ev; 69 70 spin_unlock_irq(&motu->lock); 71 72 // Header is filled later. 73 consumed += sizeof(event.motu_register_dsp_change); 74 75 while (consumed < count && 76 snd_motu_register_dsp_message_parser_copy_event(motu, &ev)) { 77 ptr = (u32 __user *)(buf + consumed); 78 if (put_user(ev, ptr)) 79 return -EFAULT; 80 consumed += sizeof(ev); 81 } 82 83 event.motu_register_dsp_change.type = SNDRV_FIREWIRE_EVENT_MOTU_REGISTER_DSP_CHANGE; 84 event.motu_register_dsp_change.count = 85 (consumed - sizeof(event.motu_register_dsp_change)) / 4; 86 if (copy_to_user(buf, &event, sizeof(event.motu_register_dsp_change))) 87 return -EFAULT; 88 89 count = consumed; 90 } 91 92 return count; 93} 94 95static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file, 96 poll_table *wait) 97{ 98 struct snd_motu *motu = hwdep->private_data; 99 __poll_t events; 100 101 poll_wait(file, &motu->hwdep_wait, wait); 102 103 spin_lock_irq(&motu->lock); 104 if (motu->dev_lock_changed || motu->msg || has_dsp_event(motu)) 105 events = EPOLLIN | EPOLLRDNORM; 106 else 107 events = 0; 108 spin_unlock_irq(&motu->lock); 109 110 return events | EPOLLOUT; 111} 112 113static int hwdep_get_info(struct snd_motu *motu, void __user *arg) 114{ 115 struct fw_device *dev = fw_parent_device(motu->unit); 116 struct snd_firewire_get_info info; 117 118 memset(&info, 0, sizeof(info)); 119 info.type = SNDRV_FIREWIRE_TYPE_MOTU; 120 info.card = dev->card->index; 121 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]); 122 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]); 123 strscpy(info.device_name, dev_name(&dev->device), 124 sizeof(info.device_name)); 125 126 if (copy_to_user(arg, &info, sizeof(info))) 127 return -EFAULT; 128 129 return 0; 130} 131 132static int hwdep_lock(struct snd_motu *motu) 133{ 134 int err; 135 136 spin_lock_irq(&motu->lock); 137 138 if (motu->dev_lock_count == 0) { 139 motu->dev_lock_count = -1; 140 err = 0; 141 } else { 142 err = -EBUSY; 143 } 144 145 spin_unlock_irq(&motu->lock); 146 147 return err; 148} 149 150static int hwdep_unlock(struct snd_motu *motu) 151{ 152 int err; 153 154 spin_lock_irq(&motu->lock); 155 156 if (motu->dev_lock_count == -1) { 157 motu->dev_lock_count = 0; 158 err = 0; 159 } else { 160 err = -EBADFD; 161 } 162 163 spin_unlock_irq(&motu->lock); 164 165 return err; 166} 167 168static int hwdep_release(struct snd_hwdep *hwdep, struct file *file) 169{ 170 struct snd_motu *motu = hwdep->private_data; 171 172 spin_lock_irq(&motu->lock); 173 if (motu->dev_lock_count == -1) 174 motu->dev_lock_count = 0; 175 spin_unlock_irq(&motu->lock); 176 177 return 0; 178} 179 180static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file, 181 unsigned int cmd, unsigned long arg) 182{ 183 struct snd_motu *motu = hwdep->private_data; 184 185 switch (cmd) { 186 case SNDRV_FIREWIRE_IOCTL_GET_INFO: 187 return hwdep_get_info(motu, (void __user *)arg); 188 case SNDRV_FIREWIRE_IOCTL_LOCK: 189 return hwdep_lock(motu); 190 case SNDRV_FIREWIRE_IOCTL_UNLOCK: 191 return hwdep_unlock(motu); 192 case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_METER: 193 { 194 struct snd_firewire_motu_register_dsp_meter *meter; 195 int err; 196 197 if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP)) 198 return -ENXIO; 199 200 meter = kzalloc(sizeof(*meter), GFP_KERNEL); 201 if (!meter) 202 return -ENOMEM; 203 204 snd_motu_register_dsp_message_parser_copy_meter(motu, meter); 205 206 err = copy_to_user((void __user *)arg, meter, sizeof(*meter)); 207 kfree(meter); 208 209 if (err) 210 return -EFAULT; 211 212 return 0; 213 } 214 case SNDRV_FIREWIRE_IOCTL_MOTU_COMMAND_DSP_METER: 215 { 216 struct snd_firewire_motu_command_dsp_meter *meter; 217 int err; 218 219 if (!(motu->spec->flags & SND_MOTU_SPEC_COMMAND_DSP)) 220 return -ENXIO; 221 222 meter = kzalloc(sizeof(*meter), GFP_KERNEL); 223 if (!meter) 224 return -ENOMEM; 225 226 snd_motu_command_dsp_message_parser_copy_meter(motu, meter); 227 228 err = copy_to_user((void __user *)arg, meter, sizeof(*meter)); 229 kfree(meter); 230 231 if (err) 232 return -EFAULT; 233 234 return 0; 235 } 236 case SNDRV_FIREWIRE_IOCTL_MOTU_REGISTER_DSP_PARAMETER: 237 { 238 struct snd_firewire_motu_register_dsp_parameter *param; 239 int err; 240 241 if (!(motu->spec->flags & SND_MOTU_SPEC_REGISTER_DSP)) 242 return -ENXIO; 243 244 param = kzalloc(sizeof(*param), GFP_KERNEL); 245 if (!param) 246 return -ENOMEM; 247 248 snd_motu_register_dsp_message_parser_copy_parameter(motu, param); 249 250 err = copy_to_user((void __user *)arg, param, sizeof(*param)); 251 kfree(param); 252 if (err) 253 return -EFAULT; 254 255 return 0; 256 } 257 default: 258 return -ENOIOCTLCMD; 259 } 260} 261 262#ifdef CONFIG_COMPAT 263static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file, 264 unsigned int cmd, unsigned long arg) 265{ 266 return hwdep_ioctl(hwdep, file, cmd, 267 (unsigned long)compat_ptr(arg)); 268} 269#else 270#define hwdep_compat_ioctl NULL 271#endif 272 273int snd_motu_create_hwdep_device(struct snd_motu *motu) 274{ 275 static const struct snd_hwdep_ops ops = { 276 .read = hwdep_read, 277 .release = hwdep_release, 278 .poll = hwdep_poll, 279 .ioctl = hwdep_ioctl, 280 .ioctl_compat = hwdep_compat_ioctl, 281 }; 282 struct snd_hwdep *hwdep; 283 int err; 284 285 err = snd_hwdep_new(motu->card, motu->card->driver, 0, &hwdep); 286 if (err < 0) 287 return err; 288 289 strcpy(hwdep->name, "MOTU"); 290 hwdep->iface = SNDRV_HWDEP_IFACE_FW_MOTU; 291 hwdep->ops = ops; 292 hwdep->private_data = motu; 293 hwdep->exclusive = true; 294 295 motu->hwdep = hwdep; 296 297 return 0; 298}