ff-hwdep.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ff-hwdep.c - a part of driver for RME Fireface series
  4. *
  5. * Copyright (c) 2015-2017 Takashi Sakamoto
  6. */
  7. /*
  8. * This codes give three functionality.
  9. *
  10. * 1.get firewire node information
  11. * 2.get notification about starting/stopping stream
  12. * 3.lock/unlock stream
  13. */
  14. #include "ff.h"
  15. static bool has_msg(struct snd_ff *ff)
  16. {
  17. if (ff->spec->protocol->has_msg)
  18. return ff->spec->protocol->has_msg(ff);
  19. else
  20. return 0;
  21. }
  22. static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
  23. loff_t *offset)
  24. {
  25. struct snd_ff *ff = hwdep->private_data;
  26. DEFINE_WAIT(wait);
  27. spin_lock_irq(&ff->lock);
  28. while (!ff->dev_lock_changed && !has_msg(ff)) {
  29. prepare_to_wait(&ff->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  30. spin_unlock_irq(&ff->lock);
  31. schedule();
  32. finish_wait(&ff->hwdep_wait, &wait);
  33. if (signal_pending(current))
  34. return -ERESTARTSYS;
  35. spin_lock_irq(&ff->lock);
  36. }
  37. if (ff->dev_lock_changed && count >= sizeof(struct snd_firewire_event_lock_status)) {
  38. struct snd_firewire_event_lock_status ev = {
  39. .type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
  40. .status = (ff->dev_lock_count > 0),
  41. };
  42. ff->dev_lock_changed = false;
  43. spin_unlock_irq(&ff->lock);
  44. if (copy_to_user(buf, &ev, sizeof(ev)))
  45. return -EFAULT;
  46. count = sizeof(ev);
  47. } else if (has_msg(ff)) {
  48. // NOTE: Acquired spin lock should be released before accessing to user space in the
  49. // callback since the access can cause page fault.
  50. count = ff->spec->protocol->copy_msg_to_user(ff, buf, count);
  51. spin_unlock_irq(&ff->lock);
  52. } else {
  53. spin_unlock_irq(&ff->lock);
  54. count = 0;
  55. }
  56. return count;
  57. }
  58. static __poll_t hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
  59. poll_table *wait)
  60. {
  61. struct snd_ff *ff = hwdep->private_data;
  62. __poll_t events;
  63. poll_wait(file, &ff->hwdep_wait, wait);
  64. spin_lock_irq(&ff->lock);
  65. if (ff->dev_lock_changed || has_msg(ff))
  66. events = EPOLLIN | EPOLLRDNORM;
  67. else
  68. events = 0;
  69. spin_unlock_irq(&ff->lock);
  70. return events;
  71. }
  72. static int hwdep_get_info(struct snd_ff *ff, void __user *arg)
  73. {
  74. struct fw_device *dev = fw_parent_device(ff->unit);
  75. struct snd_firewire_get_info info;
  76. memset(&info, 0, sizeof(info));
  77. info.type = SNDRV_FIREWIRE_TYPE_FIREFACE;
  78. info.card = dev->card->index;
  79. *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
  80. *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
  81. strscpy(info.device_name, dev_name(&dev->device),
  82. sizeof(info.device_name));
  83. if (copy_to_user(arg, &info, sizeof(info)))
  84. return -EFAULT;
  85. return 0;
  86. }
  87. static int hwdep_lock(struct snd_ff *ff)
  88. {
  89. int err;
  90. spin_lock_irq(&ff->lock);
  91. if (ff->dev_lock_count == 0) {
  92. ff->dev_lock_count = -1;
  93. err = 0;
  94. } else {
  95. err = -EBUSY;
  96. }
  97. spin_unlock_irq(&ff->lock);
  98. return err;
  99. }
  100. static int hwdep_unlock(struct snd_ff *ff)
  101. {
  102. int err;
  103. spin_lock_irq(&ff->lock);
  104. if (ff->dev_lock_count == -1) {
  105. ff->dev_lock_count = 0;
  106. err = 0;
  107. } else {
  108. err = -EBADFD;
  109. }
  110. spin_unlock_irq(&ff->lock);
  111. return err;
  112. }
  113. static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  114. {
  115. struct snd_ff *ff = hwdep->private_data;
  116. spin_lock_irq(&ff->lock);
  117. if (ff->dev_lock_count == -1)
  118. ff->dev_lock_count = 0;
  119. spin_unlock_irq(&ff->lock);
  120. return 0;
  121. }
  122. static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  123. unsigned int cmd, unsigned long arg)
  124. {
  125. struct snd_ff *ff = hwdep->private_data;
  126. switch (cmd) {
  127. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  128. return hwdep_get_info(ff, (void __user *)arg);
  129. case SNDRV_FIREWIRE_IOCTL_LOCK:
  130. return hwdep_lock(ff);
  131. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  132. return hwdep_unlock(ff);
  133. default:
  134. return -ENOIOCTLCMD;
  135. }
  136. }
  137. #ifdef CONFIG_COMPAT
  138. static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
  139. unsigned int cmd, unsigned long arg)
  140. {
  141. return hwdep_ioctl(hwdep, file, cmd,
  142. (unsigned long)compat_ptr(arg));
  143. }
  144. #else
  145. #define hwdep_compat_ioctl NULL
  146. #endif
  147. int snd_ff_create_hwdep_devices(struct snd_ff *ff)
  148. {
  149. static const struct snd_hwdep_ops hwdep_ops = {
  150. .read = hwdep_read,
  151. .release = hwdep_release,
  152. .poll = hwdep_poll,
  153. .ioctl = hwdep_ioctl,
  154. .ioctl_compat = hwdep_compat_ioctl,
  155. };
  156. struct snd_hwdep *hwdep;
  157. int err;
  158. err = snd_hwdep_new(ff->card, ff->card->driver, 0, &hwdep);
  159. if (err < 0)
  160. return err;
  161. strcpy(hwdep->name, ff->card->driver);
  162. hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREFACE;
  163. hwdep->ops = hwdep_ops;
  164. hwdep->private_data = ff;
  165. hwdep->exclusive = true;
  166. return 0;
  167. }