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