dice-hwdep.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * dice_hwdep.c - a part of driver for DICE based devices
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  6. *
  7. * Licensed under the terms of the GNU General Public License, version 2.
  8. */
  9. #include "dice.h"
  10. static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
  11. long count, loff_t *offset)
  12. {
  13. struct snd_dice *dice = hwdep->private_data;
  14. DEFINE_WAIT(wait);
  15. union snd_firewire_event event;
  16. spin_lock_irq(&dice->lock);
  17. while (!dice->dev_lock_changed && dice->notification_bits == 0) {
  18. prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  19. spin_unlock_irq(&dice->lock);
  20. schedule();
  21. finish_wait(&dice->hwdep_wait, &wait);
  22. if (signal_pending(current))
  23. return -ERESTARTSYS;
  24. spin_lock_irq(&dice->lock);
  25. }
  26. memset(&event, 0, sizeof(event));
  27. if (dice->dev_lock_changed) {
  28. event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
  29. event.lock_status.status = dice->dev_lock_count > 0;
  30. dice->dev_lock_changed = false;
  31. count = min_t(long, count, sizeof(event.lock_status));
  32. } else {
  33. event.dice_notification.type =
  34. SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
  35. event.dice_notification.notification = dice->notification_bits;
  36. dice->notification_bits = 0;
  37. count = min_t(long, count, sizeof(event.dice_notification));
  38. }
  39. spin_unlock_irq(&dice->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_dice *dice = hwdep->private_data;
  48. unsigned int events;
  49. poll_wait(file, &dice->hwdep_wait, wait);
  50. spin_lock_irq(&dice->lock);
  51. if (dice->dev_lock_changed || dice->notification_bits != 0)
  52. events = POLLIN | POLLRDNORM;
  53. else
  54. events = 0;
  55. spin_unlock_irq(&dice->lock);
  56. return events;
  57. }
  58. static int hwdep_get_info(struct snd_dice *dice, void __user *arg)
  59. {
  60. struct fw_device *dev = fw_parent_device(dice->unit);
  61. struct snd_firewire_get_info info;
  62. memset(&info, 0, sizeof(info));
  63. info.type = SNDRV_FIREWIRE_TYPE_DICE;
  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_dice *dice)
  74. {
  75. int err;
  76. spin_lock_irq(&dice->lock);
  77. if (dice->dev_lock_count == 0) {
  78. dice->dev_lock_count = -1;
  79. err = 0;
  80. } else {
  81. err = -EBUSY;
  82. }
  83. spin_unlock_irq(&dice->lock);
  84. return err;
  85. }
  86. static int hwdep_unlock(struct snd_dice *dice)
  87. {
  88. int err;
  89. spin_lock_irq(&dice->lock);
  90. if (dice->dev_lock_count == -1) {
  91. dice->dev_lock_count = 0;
  92. err = 0;
  93. } else {
  94. err = -EBADFD;
  95. }
  96. spin_unlock_irq(&dice->lock);
  97. return err;
  98. }
  99. static int hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  100. {
  101. struct snd_dice *dice = hwdep->private_data;
  102. spin_lock_irq(&dice->lock);
  103. if (dice->dev_lock_count == -1)
  104. dice->dev_lock_count = 0;
  105. spin_unlock_irq(&dice->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_dice *dice = hwdep->private_data;
  112. switch (cmd) {
  113. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  114. return hwdep_get_info(dice, (void __user *)arg);
  115. case SNDRV_FIREWIRE_IOCTL_LOCK:
  116. return hwdep_lock(dice);
  117. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  118. return hwdep_unlock(dice);
  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_dice_create_hwdep(struct snd_dice *dice)
  134. {
  135. static const struct snd_hwdep_ops 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(dice->card, "DICE", 0, &hwdep);
  145. if (err < 0)
  146. return err;
  147. strcpy(hwdep->name, "DICE");
  148. hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
  149. hwdep->ops = ops;
  150. hwdep->private_data = dice;
  151. hwdep->exclusive = true;
  152. return 0;
  153. }