motu-hwdep.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * motu-hwdep.c - a part of driver for MOTU FireWire series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  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. #include "motu.h"
  17. static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
  18. loff_t *offset)
  19. {
  20. struct snd_motu *motu = hwdep->private_data;
  21. DEFINE_WAIT(wait);
  22. union snd_firewire_event event;
  23. spin_lock_irq(&motu->lock);
  24. while (!motu->dev_lock_changed && motu->msg == 0) {
  25. prepare_to_wait(&motu->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  26. spin_unlock_irq(&motu->lock);
  27. schedule();
  28. finish_wait(&motu->hwdep_wait, &wait);
  29. if (signal_pending(current))
  30. return -ERESTARTSYS;
  31. spin_lock_irq(&motu->lock);
  32. }
  33. memset(&event, 0, sizeof(event));
  34. if (motu->dev_lock_changed) {
  35. event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
  36. event.lock_status.status = (motu->dev_lock_count > 0);
  37. motu->dev_lock_changed = false;
  38. count = min_t(long, count, sizeof(event.lock_status));
  39. } else {
  40. event.motu_notification.type = SNDRV_FIREWIRE_EVENT_MOTU_NOTIFICATION;
  41. event.motu_notification.message = motu->msg;
  42. motu->msg = 0;
  43. count = min_t(long, count, sizeof(event.motu_notification));
  44. }
  45. spin_unlock_irq(&motu->lock);
  46. if (copy_to_user(buf, &event, count))
  47. return -EFAULT;
  48. return count;
  49. }
  50. static unsigned int hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
  51. poll_table *wait)
  52. {
  53. struct snd_motu *motu = hwdep->private_data;
  54. unsigned int events;
  55. poll_wait(file, &motu->hwdep_wait, wait);
  56. spin_lock_irq(&motu->lock);
  57. if (motu->dev_lock_changed || motu->msg)
  58. events = POLLIN | POLLRDNORM;
  59. else
  60. events = 0;
  61. spin_unlock_irq(&motu->lock);
  62. return events | POLLOUT;
  63. }
  64. static int hwdep_get_info(struct snd_motu *motu, void __user *arg)
  65. {
  66. struct fw_device *dev = fw_parent_device(motu->unit);
  67. struct snd_firewire_get_info info;
  68. memset(&info, 0, sizeof(info));
  69. info.type = SNDRV_FIREWIRE_TYPE_MOTU;
  70. info.card = dev->card->index;
  71. *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
  72. *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
  73. strlcpy(info.device_name, dev_name(&dev->device),
  74. sizeof(info.device_name));
  75. if (copy_to_user(arg, &info, sizeof(info)))
  76. return -EFAULT;
  77. return 0;
  78. }
  79. static int hwdep_lock(struct snd_motu *motu)
  80. {
  81. int err;
  82. spin_lock_irq(&motu->lock);
  83. if (motu->dev_lock_count == 0) {
  84. motu->dev_lock_count = -1;
  85. err = 0;
  86. } else {
  87. err = -EBUSY;
  88. }
  89. spin_unlock_irq(&motu->lock);
  90. return err;
  91. }
  92. static int hwdep_unlock(struct snd_motu *motu)
  93. {
  94. int err;
  95. spin_lock_irq(&motu->lock);
  96. if (motu->dev_lock_count == -1) {
  97. motu->dev_lock_count = 0;
  98. err = 0;
  99. } else {
  100. err = -EBADFD;
  101. }
  102. spin_unlock_irq(&motu->lock);
  103. return err;
  104. }
  105. static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  106. {
  107. struct snd_motu *motu = hwdep->private_data;
  108. spin_lock_irq(&motu->lock);
  109. if (motu->dev_lock_count == -1)
  110. motu->dev_lock_count = 0;
  111. spin_unlock_irq(&motu->lock);
  112. return 0;
  113. }
  114. static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  115. unsigned int cmd, unsigned long arg)
  116. {
  117. struct snd_motu *motu = hwdep->private_data;
  118. switch (cmd) {
  119. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  120. return hwdep_get_info(motu, (void __user *)arg);
  121. case SNDRV_FIREWIRE_IOCTL_LOCK:
  122. return hwdep_lock(motu);
  123. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  124. return hwdep_unlock(motu);
  125. default:
  126. return -ENOIOCTLCMD;
  127. }
  128. }
  129. #ifdef CONFIG_COMPAT
  130. static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
  131. unsigned int cmd, unsigned long arg)
  132. {
  133. return hwdep_ioctl(hwdep, file, cmd,
  134. (unsigned long)compat_ptr(arg));
  135. }
  136. #else
  137. #define hwdep_compat_ioctl NULL
  138. #endif
  139. int snd_motu_create_hwdep_device(struct snd_motu *motu)
  140. {
  141. static const struct snd_hwdep_ops ops = {
  142. .read = hwdep_read,
  143. .release = hwdep_release,
  144. .poll = hwdep_poll,
  145. .ioctl = hwdep_ioctl,
  146. .ioctl_compat = hwdep_compat_ioctl,
  147. };
  148. struct snd_hwdep *hwdep;
  149. int err;
  150. err = snd_hwdep_new(motu->card, motu->card->driver, 0, &hwdep);
  151. if (err < 0)
  152. return err;
  153. strcpy(hwdep->name, "MOTU");
  154. hwdep->iface = SNDRV_HWDEP_IFACE_FW_MOTU;
  155. hwdep->ops = ops;
  156. hwdep->private_data = motu;
  157. hwdep->exclusive = true;
  158. return 0;
  159. }