Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * dice_hwdep.c - a part of driver for DICE based devices
0004  *
0005  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
0006  * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
0007  */
0008 
0009 #include "dice.h"
0010 
0011 static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
0012                 long count, loff_t *offset)
0013 {
0014     struct snd_dice *dice = hwdep->private_data;
0015     DEFINE_WAIT(wait);
0016     union snd_firewire_event event;
0017 
0018     spin_lock_irq(&dice->lock);
0019 
0020     while (!dice->dev_lock_changed && dice->notification_bits == 0) {
0021         prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
0022         spin_unlock_irq(&dice->lock);
0023         schedule();
0024         finish_wait(&dice->hwdep_wait, &wait);
0025         if (signal_pending(current))
0026             return -ERESTARTSYS;
0027         spin_lock_irq(&dice->lock);
0028     }
0029 
0030     memset(&event, 0, sizeof(event));
0031     if (dice->dev_lock_changed) {
0032         event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
0033         event.lock_status.status = dice->dev_lock_count > 0;
0034         dice->dev_lock_changed = false;
0035 
0036         count = min_t(long, count, sizeof(event.lock_status));
0037     } else {
0038         event.dice_notification.type =
0039                     SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
0040         event.dice_notification.notification = dice->notification_bits;
0041         dice->notification_bits = 0;
0042 
0043         count = min_t(long, count, sizeof(event.dice_notification));
0044     }
0045 
0046     spin_unlock_irq(&dice->lock);
0047 
0048     if (copy_to_user(buf, &event, count))
0049         return -EFAULT;
0050 
0051     return count;
0052 }
0053 
0054 static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
0055                    poll_table *wait)
0056 {
0057     struct snd_dice *dice = hwdep->private_data;
0058     __poll_t events;
0059 
0060     poll_wait(file, &dice->hwdep_wait, wait);
0061 
0062     spin_lock_irq(&dice->lock);
0063     if (dice->dev_lock_changed || dice->notification_bits != 0)
0064         events = EPOLLIN | EPOLLRDNORM;
0065     else
0066         events = 0;
0067     spin_unlock_irq(&dice->lock);
0068 
0069     return events;
0070 }
0071 
0072 static int hwdep_get_info(struct snd_dice *dice, void __user *arg)
0073 {
0074     struct fw_device *dev = fw_parent_device(dice->unit);
0075     struct snd_firewire_get_info info;
0076 
0077     memset(&info, 0, sizeof(info));
0078     info.type = SNDRV_FIREWIRE_TYPE_DICE;
0079     info.card = dev->card->index;
0080     *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
0081     *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
0082     strscpy(info.device_name, dev_name(&dev->device),
0083         sizeof(info.device_name));
0084 
0085     if (copy_to_user(arg, &info, sizeof(info)))
0086         return -EFAULT;
0087 
0088     return 0;
0089 }
0090 
0091 static int hwdep_lock(struct snd_dice *dice)
0092 {
0093     int err;
0094 
0095     spin_lock_irq(&dice->lock);
0096 
0097     if (dice->dev_lock_count == 0) {
0098         dice->dev_lock_count = -1;
0099         err = 0;
0100     } else {
0101         err = -EBUSY;
0102     }
0103 
0104     spin_unlock_irq(&dice->lock);
0105 
0106     return err;
0107 }
0108 
0109 static int hwdep_unlock(struct snd_dice *dice)
0110 {
0111     int err;
0112 
0113     spin_lock_irq(&dice->lock);
0114 
0115     if (dice->dev_lock_count == -1) {
0116         dice->dev_lock_count = 0;
0117         err = 0;
0118     } else {
0119         err = -EBADFD;
0120     }
0121 
0122     spin_unlock_irq(&dice->lock);
0123 
0124     return err;
0125 }
0126 
0127 static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
0128 {
0129     struct snd_dice *dice = hwdep->private_data;
0130 
0131     spin_lock_irq(&dice->lock);
0132     if (dice->dev_lock_count == -1)
0133         dice->dev_lock_count = 0;
0134     spin_unlock_irq(&dice->lock);
0135 
0136     return 0;
0137 }
0138 
0139 static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
0140                unsigned int cmd, unsigned long arg)
0141 {
0142     struct snd_dice *dice = hwdep->private_data;
0143 
0144     switch (cmd) {
0145     case SNDRV_FIREWIRE_IOCTL_GET_INFO:
0146         return hwdep_get_info(dice, (void __user *)arg);
0147     case SNDRV_FIREWIRE_IOCTL_LOCK:
0148         return hwdep_lock(dice);
0149     case SNDRV_FIREWIRE_IOCTL_UNLOCK:
0150         return hwdep_unlock(dice);
0151     default:
0152         return -ENOIOCTLCMD;
0153     }
0154 }
0155 
0156 #ifdef CONFIG_COMPAT
0157 static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
0158                   unsigned int cmd, unsigned long arg)
0159 {
0160     return hwdep_ioctl(hwdep, file, cmd,
0161                (unsigned long)compat_ptr(arg));
0162 }
0163 #else
0164 #define hwdep_compat_ioctl NULL
0165 #endif
0166 
0167 int snd_dice_create_hwdep(struct snd_dice *dice)
0168 {
0169     static const struct snd_hwdep_ops ops = {
0170         .read         = hwdep_read,
0171         .release      = hwdep_release,
0172         .poll         = hwdep_poll,
0173         .ioctl        = hwdep_ioctl,
0174         .ioctl_compat = hwdep_compat_ioctl,
0175     };
0176     struct snd_hwdep *hwdep;
0177     int err;
0178 
0179     err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
0180     if (err < 0)
0181         return err;
0182     strcpy(hwdep->name, "DICE");
0183     hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
0184     hwdep->ops = ops;
0185     hwdep->private_data = dice;
0186     hwdep->exclusive = true;
0187 
0188     return 0;
0189 }