oxfw-hwdep.c 4.1 KB

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