Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * fireworks_hwdep.c - a part of driver for Fireworks based devices
0004  *
0005  * Copyright (c) 2013-2014 Takashi Sakamoto
0006  */
0007 
0008 /*
0009  * This codes have five functionalities.
0010  *
0011  * 1.get information about firewire node
0012  * 2.get notification about starting/stopping stream
0013  * 3.lock/unlock streaming
0014  * 4.transmit command of EFW transaction
0015  * 5.receive response of EFW transaction
0016  *
0017  */
0018 
0019 #include "fireworks.h"
0020 
0021 static long
0022 hwdep_read_resp_buf(struct snd_efw *efw, char __user *buf, long remained,
0023             loff_t *offset)
0024 {
0025     unsigned int length, till_end, type;
0026     struct snd_efw_transaction *t;
0027     u8 *pull_ptr;
0028     long count = 0;
0029 
0030     if (remained < sizeof(type) + sizeof(struct snd_efw_transaction))
0031         return -ENOSPC;
0032 
0033     /* data type is SNDRV_FIREWIRE_EVENT_EFW_RESPONSE */
0034     type = SNDRV_FIREWIRE_EVENT_EFW_RESPONSE;
0035     if (copy_to_user(buf, &type, sizeof(type)))
0036         return -EFAULT;
0037     count += sizeof(type);
0038     remained -= sizeof(type);
0039     buf += sizeof(type);
0040 
0041     /* write into buffer as many responses as possible */
0042     spin_lock_irq(&efw->lock);
0043 
0044     /*
0045      * When another task reaches here during this task's access to user
0046      * space, it picks up current position in buffer and can read the same
0047      * series of responses.
0048      */
0049     pull_ptr = efw->pull_ptr;
0050 
0051     while (efw->push_ptr != pull_ptr) {
0052         t = (struct snd_efw_transaction *)(pull_ptr);
0053         length = be32_to_cpu(t->length) * sizeof(__be32);
0054 
0055         /* confirm enough space for this response */
0056         if (remained < length)
0057             break;
0058 
0059         /* copy from ring buffer to user buffer */
0060         while (length > 0) {
0061             till_end = snd_efw_resp_buf_size -
0062                 (unsigned int)(pull_ptr - efw->resp_buf);
0063             till_end = min_t(unsigned int, length, till_end);
0064 
0065             spin_unlock_irq(&efw->lock);
0066 
0067             if (copy_to_user(buf, pull_ptr, till_end))
0068                 return -EFAULT;
0069 
0070             spin_lock_irq(&efw->lock);
0071 
0072             pull_ptr += till_end;
0073             if (pull_ptr >= efw->resp_buf + snd_efw_resp_buf_size)
0074                 pull_ptr -= snd_efw_resp_buf_size;
0075 
0076             length -= till_end;
0077             buf += till_end;
0078             count += till_end;
0079             remained -= till_end;
0080         }
0081     }
0082 
0083     /*
0084      * All of tasks can read from the buffer nearly simultaneously, but the
0085      * last position for each task is different depending on the length of
0086      * given buffer. Here, for simplicity, a position of buffer is set by
0087      * the latest task. It's better for a listening application to allow one
0088      * thread to read from the buffer. Unless, each task can read different
0089      * sequence of responses depending on variation of buffer length.
0090      */
0091     efw->pull_ptr = pull_ptr;
0092 
0093     spin_unlock_irq(&efw->lock);
0094 
0095     return count;
0096 }
0097 
0098 static long
0099 hwdep_read_locked(struct snd_efw *efw, char __user *buf, long count,
0100           loff_t *offset)
0101 {
0102     union snd_firewire_event event = {
0103         .lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
0104     };
0105 
0106     spin_lock_irq(&efw->lock);
0107 
0108     event.lock_status.status = (efw->dev_lock_count > 0);
0109     efw->dev_lock_changed = false;
0110 
0111     spin_unlock_irq(&efw->lock);
0112 
0113     count = min_t(long, count, sizeof(event.lock_status));
0114 
0115     if (copy_to_user(buf, &event, count))
0116         return -EFAULT;
0117 
0118     return count;
0119 }
0120 
0121 static long
0122 hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
0123        loff_t *offset)
0124 {
0125     struct snd_efw *efw = hwdep->private_data;
0126     DEFINE_WAIT(wait);
0127     bool dev_lock_changed;
0128     bool queued;
0129 
0130     spin_lock_irq(&efw->lock);
0131 
0132     dev_lock_changed = efw->dev_lock_changed;
0133     queued = efw->push_ptr != efw->pull_ptr;
0134 
0135     while (!dev_lock_changed && !queued) {
0136         prepare_to_wait(&efw->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
0137         spin_unlock_irq(&efw->lock);
0138         schedule();
0139         finish_wait(&efw->hwdep_wait, &wait);
0140         if (signal_pending(current))
0141             return -ERESTARTSYS;
0142         spin_lock_irq(&efw->lock);
0143         dev_lock_changed = efw->dev_lock_changed;
0144         queued = efw->push_ptr != efw->pull_ptr;
0145     }
0146 
0147     spin_unlock_irq(&efw->lock);
0148 
0149     if (dev_lock_changed)
0150         count = hwdep_read_locked(efw, buf, count, offset);
0151     else if (queued)
0152         count = hwdep_read_resp_buf(efw, buf, count, offset);
0153 
0154     return count;
0155 }
0156 
0157 static long
0158 hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
0159         loff_t *offset)
0160 {
0161     struct snd_efw *efw = hwdep->private_data;
0162     u32 seqnum;
0163     u8 *buf;
0164 
0165     if (count < sizeof(struct snd_efw_transaction) ||
0166         SND_EFW_RESPONSE_MAXIMUM_BYTES < count)
0167         return -EINVAL;
0168 
0169     buf = memdup_user(data, count);
0170     if (IS_ERR(buf))
0171         return PTR_ERR(buf);
0172 
0173     /* check seqnum is not for kernel-land */
0174     seqnum = be32_to_cpu(((struct snd_efw_transaction *)buf)->seqnum);
0175     if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX) {
0176         count = -EINVAL;
0177         goto end;
0178     }
0179 
0180     if (snd_efw_transaction_cmd(efw->unit, buf, count) < 0)
0181         count = -EIO;
0182 end:
0183     kfree(buf);
0184     return count;
0185 }
0186 
0187 static __poll_t
0188 hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
0189 {
0190     struct snd_efw *efw = hwdep->private_data;
0191     __poll_t events;
0192 
0193     poll_wait(file, &efw->hwdep_wait, wait);
0194 
0195     spin_lock_irq(&efw->lock);
0196     if (efw->dev_lock_changed || efw->pull_ptr != efw->push_ptr)
0197         events = EPOLLIN | EPOLLRDNORM;
0198     else
0199         events = 0;
0200     spin_unlock_irq(&efw->lock);
0201 
0202     return events | EPOLLOUT;
0203 }
0204 
0205 static int
0206 hwdep_get_info(struct snd_efw *efw, void __user *arg)
0207 {
0208     struct fw_device *dev = fw_parent_device(efw->unit);
0209     struct snd_firewire_get_info info;
0210 
0211     memset(&info, 0, sizeof(info));
0212     info.type = SNDRV_FIREWIRE_TYPE_FIREWORKS;
0213     info.card = dev->card->index;
0214     *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
0215     *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
0216     strscpy(info.device_name, dev_name(&dev->device),
0217         sizeof(info.device_name));
0218 
0219     if (copy_to_user(arg, &info, sizeof(info)))
0220         return -EFAULT;
0221 
0222     return 0;
0223 }
0224 
0225 static int
0226 hwdep_lock(struct snd_efw *efw)
0227 {
0228     int err;
0229 
0230     spin_lock_irq(&efw->lock);
0231 
0232     if (efw->dev_lock_count == 0) {
0233         efw->dev_lock_count = -1;
0234         err = 0;
0235     } else {
0236         err = -EBUSY;
0237     }
0238 
0239     spin_unlock_irq(&efw->lock);
0240 
0241     return err;
0242 }
0243 
0244 static int
0245 hwdep_unlock(struct snd_efw *efw)
0246 {
0247     int err;
0248 
0249     spin_lock_irq(&efw->lock);
0250 
0251     if (efw->dev_lock_count == -1) {
0252         efw->dev_lock_count = 0;
0253         err = 0;
0254     } else {
0255         err = -EBADFD;
0256     }
0257 
0258     spin_unlock_irq(&efw->lock);
0259 
0260     return err;
0261 }
0262 
0263 static int
0264 hwdep_release(struct snd_hwdep *hwdep, struct file *file)
0265 {
0266     struct snd_efw *efw = hwdep->private_data;
0267 
0268     spin_lock_irq(&efw->lock);
0269     if (efw->dev_lock_count == -1)
0270         efw->dev_lock_count = 0;
0271     spin_unlock_irq(&efw->lock);
0272 
0273     return 0;
0274 }
0275 
0276 static int
0277 hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
0278         unsigned int cmd, unsigned long arg)
0279 {
0280     struct snd_efw *efw = hwdep->private_data;
0281 
0282     switch (cmd) {
0283     case SNDRV_FIREWIRE_IOCTL_GET_INFO:
0284         return hwdep_get_info(efw, (void __user *)arg);
0285     case SNDRV_FIREWIRE_IOCTL_LOCK:
0286         return hwdep_lock(efw);
0287     case SNDRV_FIREWIRE_IOCTL_UNLOCK:
0288         return hwdep_unlock(efw);
0289     default:
0290         return -ENOIOCTLCMD;
0291     }
0292 }
0293 
0294 #ifdef CONFIG_COMPAT
0295 static int
0296 hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
0297            unsigned int cmd, unsigned long arg)
0298 {
0299     return hwdep_ioctl(hwdep, file, cmd,
0300                (unsigned long)compat_ptr(arg));
0301 }
0302 #else
0303 #define hwdep_compat_ioctl NULL
0304 #endif
0305 
0306 int snd_efw_create_hwdep_device(struct snd_efw *efw)
0307 {
0308     static const struct snd_hwdep_ops ops = {
0309         .read       = hwdep_read,
0310         .write      = hwdep_write,
0311         .release    = hwdep_release,
0312         .poll       = hwdep_poll,
0313         .ioctl      = hwdep_ioctl,
0314         .ioctl_compat   = hwdep_compat_ioctl,
0315     };
0316     struct snd_hwdep *hwdep;
0317     int err;
0318 
0319     err = snd_hwdep_new(efw->card, "Fireworks", 0, &hwdep);
0320     if (err < 0)
0321         goto end;
0322     strcpy(hwdep->name, "Fireworks");
0323     hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREWORKS;
0324     hwdep->ops = ops;
0325     hwdep->private_data = efw;
0326     hwdep->exclusive = true;
0327 end:
0328     return err;
0329 }
0330