digi00x-hwdep.c 4.4 KB

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