fireworks_hwdep.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * fireworks_hwdep.c - a part of driver for Fireworks 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 have five functionalities.
  10. *
  11. * 1.get information about firewire node
  12. * 2.get notification about starting/stopping stream
  13. * 3.lock/unlock streaming
  14. * 4.transmit command of EFW transaction
  15. * 5.receive response of EFW transaction
  16. *
  17. */
  18. #include "fireworks.h"
  19. static long
  20. hwdep_read_resp_buf(struct snd_efw *efw, char __user *buf, long remained,
  21. loff_t *offset)
  22. {
  23. unsigned int length, till_end, type;
  24. struct snd_efw_transaction *t;
  25. u8 *pull_ptr;
  26. long count = 0;
  27. if (remained < sizeof(type) + sizeof(struct snd_efw_transaction))
  28. return -ENOSPC;
  29. /* data type is SNDRV_FIREWIRE_EVENT_EFW_RESPONSE */
  30. type = SNDRV_FIREWIRE_EVENT_EFW_RESPONSE;
  31. if (copy_to_user(buf, &type, sizeof(type)))
  32. return -EFAULT;
  33. remained -= sizeof(type);
  34. buf += sizeof(type);
  35. /* write into buffer as many responses as possible */
  36. spin_lock_irq(&efw->lock);
  37. /*
  38. * When another task reaches here during this task's access to user
  39. * space, it picks up current position in buffer and can read the same
  40. * series of responses.
  41. */
  42. pull_ptr = efw->pull_ptr;
  43. while (efw->push_ptr != pull_ptr) {
  44. t = (struct snd_efw_transaction *)(pull_ptr);
  45. length = be32_to_cpu(t->length) * sizeof(__be32);
  46. /* confirm enough space for this response */
  47. if (remained < length)
  48. break;
  49. /* copy from ring buffer to user buffer */
  50. while (length > 0) {
  51. till_end = snd_efw_resp_buf_size -
  52. (unsigned int)(pull_ptr - efw->resp_buf);
  53. till_end = min_t(unsigned int, length, till_end);
  54. spin_unlock_irq(&efw->lock);
  55. if (copy_to_user(buf, pull_ptr, till_end))
  56. return -EFAULT;
  57. spin_lock_irq(&efw->lock);
  58. pull_ptr += till_end;
  59. if (pull_ptr >= efw->resp_buf + snd_efw_resp_buf_size)
  60. pull_ptr -= snd_efw_resp_buf_size;
  61. length -= till_end;
  62. buf += till_end;
  63. count += till_end;
  64. remained -= till_end;
  65. }
  66. }
  67. /*
  68. * All of tasks can read from the buffer nearly simultaneously, but the
  69. * last position for each task is different depending on the length of
  70. * given buffer. Here, for simplicity, a position of buffer is set by
  71. * the latest task. It's better for a listening application to allow one
  72. * thread to read from the buffer. Unless, each task can read different
  73. * sequence of responses depending on variation of buffer length.
  74. */
  75. efw->pull_ptr = pull_ptr;
  76. spin_unlock_irq(&efw->lock);
  77. return count;
  78. }
  79. static long
  80. hwdep_read_locked(struct snd_efw *efw, char __user *buf, long count,
  81. loff_t *offset)
  82. {
  83. union snd_firewire_event event = {
  84. .lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS,
  85. };
  86. spin_lock_irq(&efw->lock);
  87. event.lock_status.status = (efw->dev_lock_count > 0);
  88. efw->dev_lock_changed = false;
  89. spin_unlock_irq(&efw->lock);
  90. count = min_t(long, count, sizeof(event.lock_status));
  91. if (copy_to_user(buf, &event, count))
  92. return -EFAULT;
  93. return count;
  94. }
  95. static long
  96. hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
  97. loff_t *offset)
  98. {
  99. struct snd_efw *efw = hwdep->private_data;
  100. DEFINE_WAIT(wait);
  101. bool dev_lock_changed;
  102. bool queued;
  103. spin_lock_irq(&efw->lock);
  104. dev_lock_changed = efw->dev_lock_changed;
  105. queued = efw->push_ptr != efw->pull_ptr;
  106. while (!dev_lock_changed && !queued) {
  107. prepare_to_wait(&efw->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  108. spin_unlock_irq(&efw->lock);
  109. schedule();
  110. finish_wait(&efw->hwdep_wait, &wait);
  111. if (signal_pending(current))
  112. return -ERESTARTSYS;
  113. spin_lock_irq(&efw->lock);
  114. dev_lock_changed = efw->dev_lock_changed;
  115. queued = efw->push_ptr != efw->pull_ptr;
  116. }
  117. spin_unlock_irq(&efw->lock);
  118. if (dev_lock_changed)
  119. count = hwdep_read_locked(efw, buf, count, offset);
  120. else if (queued)
  121. count = hwdep_read_resp_buf(efw, buf, count, offset);
  122. return count;
  123. }
  124. static long
  125. hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
  126. loff_t *offset)
  127. {
  128. struct snd_efw *efw = hwdep->private_data;
  129. u32 seqnum;
  130. u8 *buf;
  131. if (count < sizeof(struct snd_efw_transaction) ||
  132. SND_EFW_RESPONSE_MAXIMUM_BYTES < count)
  133. return -EINVAL;
  134. buf = memdup_user(data, count);
  135. if (IS_ERR(buf))
  136. return PTR_ERR(buf);
  137. /* check seqnum is not for kernel-land */
  138. seqnum = be32_to_cpu(((struct snd_efw_transaction *)buf)->seqnum);
  139. if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX) {
  140. count = -EINVAL;
  141. goto end;
  142. }
  143. if (snd_efw_transaction_cmd(efw->unit, buf, count) < 0)
  144. count = -EIO;
  145. end:
  146. kfree(buf);
  147. return count;
  148. }
  149. static unsigned int
  150. hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
  151. {
  152. struct snd_efw *efw = hwdep->private_data;
  153. unsigned int events;
  154. poll_wait(file, &efw->hwdep_wait, wait);
  155. spin_lock_irq(&efw->lock);
  156. if (efw->dev_lock_changed || efw->pull_ptr != efw->push_ptr)
  157. events = POLLIN | POLLRDNORM;
  158. else
  159. events = 0;
  160. spin_unlock_irq(&efw->lock);
  161. return events | POLLOUT;
  162. }
  163. static int
  164. hwdep_get_info(struct snd_efw *efw, void __user *arg)
  165. {
  166. struct fw_device *dev = fw_parent_device(efw->unit);
  167. struct snd_firewire_get_info info;
  168. memset(&info, 0, sizeof(info));
  169. info.type = SNDRV_FIREWIRE_TYPE_FIREWORKS;
  170. info.card = dev->card->index;
  171. *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
  172. *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
  173. strlcpy(info.device_name, dev_name(&dev->device),
  174. sizeof(info.device_name));
  175. if (copy_to_user(arg, &info, sizeof(info)))
  176. return -EFAULT;
  177. return 0;
  178. }
  179. static int
  180. hwdep_lock(struct snd_efw *efw)
  181. {
  182. int err;
  183. spin_lock_irq(&efw->lock);
  184. if (efw->dev_lock_count == 0) {
  185. efw->dev_lock_count = -1;
  186. err = 0;
  187. } else {
  188. err = -EBUSY;
  189. }
  190. spin_unlock_irq(&efw->lock);
  191. return err;
  192. }
  193. static int
  194. hwdep_unlock(struct snd_efw *efw)
  195. {
  196. int err;
  197. spin_lock_irq(&efw->lock);
  198. if (efw->dev_lock_count == -1) {
  199. efw->dev_lock_count = 0;
  200. err = 0;
  201. } else {
  202. err = -EBADFD;
  203. }
  204. spin_unlock_irq(&efw->lock);
  205. return err;
  206. }
  207. static int
  208. hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  209. {
  210. struct snd_efw *efw = hwdep->private_data;
  211. spin_lock_irq(&efw->lock);
  212. if (efw->dev_lock_count == -1)
  213. efw->dev_lock_count = 0;
  214. spin_unlock_irq(&efw->lock);
  215. return 0;
  216. }
  217. static int
  218. hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  219. unsigned int cmd, unsigned long arg)
  220. {
  221. struct snd_efw *efw = hwdep->private_data;
  222. switch (cmd) {
  223. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  224. return hwdep_get_info(efw, (void __user *)arg);
  225. case SNDRV_FIREWIRE_IOCTL_LOCK:
  226. return hwdep_lock(efw);
  227. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  228. return hwdep_unlock(efw);
  229. default:
  230. return -ENOIOCTLCMD;
  231. }
  232. }
  233. #ifdef CONFIG_COMPAT
  234. static int
  235. hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
  236. unsigned int cmd, unsigned long arg)
  237. {
  238. return hwdep_ioctl(hwdep, file, cmd,
  239. (unsigned long)compat_ptr(arg));
  240. }
  241. #else
  242. #define hwdep_compat_ioctl NULL
  243. #endif
  244. static const struct snd_hwdep_ops hwdep_ops = {
  245. .read = hwdep_read,
  246. .write = hwdep_write,
  247. .release = hwdep_release,
  248. .poll = hwdep_poll,
  249. .ioctl = hwdep_ioctl,
  250. .ioctl_compat = hwdep_compat_ioctl,
  251. };
  252. int snd_efw_create_hwdep_device(struct snd_efw *efw)
  253. {
  254. struct snd_hwdep *hwdep;
  255. int err;
  256. err = snd_hwdep_new(efw->card, "Fireworks", 0, &hwdep);
  257. if (err < 0)
  258. goto end;
  259. strcpy(hwdep->name, "Fireworks");
  260. hwdep->iface = SNDRV_HWDEP_IFACE_FW_FIREWORKS;
  261. hwdep->ops = hwdep_ops;
  262. hwdep->private_data = efw;
  263. hwdep->exclusive = true;
  264. end:
  265. return err;
  266. }