tascam-hwdep.c 4.0 KB

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