oxfw-hwdep.c 4.0 KB

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