bebob_hwdep.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * bebob_hwdep.c - a part of driver for BeBoB based devices
  3. *
  4. * Copyright (c) 2013-2014 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 infomation
  12. * 2.get notification about starting/stopping stream
  13. * 3.lock/unlock stream
  14. */
  15. #include "bebob.h"
  16. static long
  17. hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
  18. loff_t *offset)
  19. {
  20. struct snd_bebob *bebob = hwdep->private_data;
  21. DEFINE_WAIT(wait);
  22. union snd_firewire_event event;
  23. spin_lock_irq(&bebob->lock);
  24. while (!bebob->dev_lock_changed) {
  25. prepare_to_wait(&bebob->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  26. spin_unlock_irq(&bebob->lock);
  27. schedule();
  28. finish_wait(&bebob->hwdep_wait, &wait);
  29. if (signal_pending(current))
  30. return -ERESTARTSYS;
  31. spin_lock_irq(&bebob->lock);
  32. }
  33. memset(&event, 0, sizeof(event));
  34. if (bebob->dev_lock_changed) {
  35. event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
  36. event.lock_status.status = (bebob->dev_lock_count > 0);
  37. bebob->dev_lock_changed = false;
  38. count = min_t(long, count, sizeof(event.lock_status));
  39. }
  40. spin_unlock_irq(&bebob->lock);
  41. if (copy_to_user(buf, &event, count))
  42. return -EFAULT;
  43. return count;
  44. }
  45. static unsigned int
  46. hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
  47. {
  48. struct snd_bebob *bebob = hwdep->private_data;
  49. unsigned int events;
  50. poll_wait(file, &bebob->hwdep_wait, wait);
  51. spin_lock_irq(&bebob->lock);
  52. if (bebob->dev_lock_changed)
  53. events = POLLIN | POLLRDNORM;
  54. else
  55. events = 0;
  56. spin_unlock_irq(&bebob->lock);
  57. return events;
  58. }
  59. static int
  60. hwdep_get_info(struct snd_bebob *bebob, void __user *arg)
  61. {
  62. struct fw_device *dev = fw_parent_device(bebob->unit);
  63. struct snd_firewire_get_info info;
  64. memset(&info, 0, sizeof(info));
  65. info.type = SNDRV_FIREWIRE_TYPE_BEBOB;
  66. info.card = dev->card->index;
  67. *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
  68. *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
  69. strlcpy(info.device_name, dev_name(&dev->device),
  70. sizeof(info.device_name));
  71. if (copy_to_user(arg, &info, sizeof(info)))
  72. return -EFAULT;
  73. return 0;
  74. }
  75. static int
  76. hwdep_lock(struct snd_bebob *bebob)
  77. {
  78. int err;
  79. spin_lock_irq(&bebob->lock);
  80. if (bebob->dev_lock_count == 0) {
  81. bebob->dev_lock_count = -1;
  82. err = 0;
  83. } else {
  84. err = -EBUSY;
  85. }
  86. spin_unlock_irq(&bebob->lock);
  87. return err;
  88. }
  89. static int
  90. hwdep_unlock(struct snd_bebob *bebob)
  91. {
  92. int err;
  93. spin_lock_irq(&bebob->lock);
  94. if (bebob->dev_lock_count == -1) {
  95. bebob->dev_lock_count = 0;
  96. err = 0;
  97. } else {
  98. err = -EBADFD;
  99. }
  100. spin_unlock_irq(&bebob->lock);
  101. return err;
  102. }
  103. static int
  104. hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  105. {
  106. struct snd_bebob *bebob = hwdep->private_data;
  107. spin_lock_irq(&bebob->lock);
  108. if (bebob->dev_lock_count == -1)
  109. bebob->dev_lock_count = 0;
  110. spin_unlock_irq(&bebob->lock);
  111. return 0;
  112. }
  113. static int
  114. hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  115. unsigned int cmd, unsigned long arg)
  116. {
  117. struct snd_bebob *bebob = hwdep->private_data;
  118. switch (cmd) {
  119. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  120. return hwdep_get_info(bebob, (void __user *)arg);
  121. case SNDRV_FIREWIRE_IOCTL_LOCK:
  122. return hwdep_lock(bebob);
  123. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  124. return hwdep_unlock(bebob);
  125. default:
  126. return -ENOIOCTLCMD;
  127. }
  128. }
  129. #ifdef CONFIG_COMPAT
  130. static int
  131. 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_bebob_create_hwdep_device(struct snd_bebob *bebob)
  148. {
  149. struct snd_hwdep *hwdep;
  150. int err;
  151. err = snd_hwdep_new(bebob->card, "BeBoB", 0, &hwdep);
  152. if (err < 0)
  153. goto end;
  154. strcpy(hwdep->name, "BeBoB");
  155. hwdep->iface = SNDRV_HWDEP_IFACE_FW_BEBOB;
  156. hwdep->ops = hwdep_ops;
  157. hwdep->private_data = bebob;
  158. hwdep->exclusive = true;
  159. end:
  160. return err;
  161. }