emux_hwdep.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Interface for hwdep device
  3. *
  4. * Copyright (C) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/core.h>
  22. #include <sound/hwdep.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/nospec.h>
  25. #include "emux_voice.h"
  26. #define TMP_CLIENT_ID 0x1001
  27. /*
  28. * load patch
  29. */
  30. static int
  31. snd_emux_hwdep_load_patch(struct snd_emux *emu, void __user *arg)
  32. {
  33. int err;
  34. struct soundfont_patch_info patch;
  35. if (copy_from_user(&patch, arg, sizeof(patch)))
  36. return -EFAULT;
  37. if (patch.type >= SNDRV_SFNT_LOAD_INFO &&
  38. patch.type <= SNDRV_SFNT_PROBE_DATA) {
  39. err = snd_soundfont_load(emu->sflist, arg, patch.len + sizeof(patch), TMP_CLIENT_ID);
  40. if (err < 0)
  41. return err;
  42. } else {
  43. if (emu->ops.load_fx)
  44. return emu->ops.load_fx(emu, patch.type, patch.optarg, arg, patch.len + sizeof(patch));
  45. else
  46. return -EINVAL;
  47. }
  48. return 0;
  49. }
  50. /*
  51. * set misc mode
  52. */
  53. static int
  54. snd_emux_hwdep_misc_mode(struct snd_emux *emu, void __user *arg)
  55. {
  56. struct snd_emux_misc_mode info;
  57. int i;
  58. if (copy_from_user(&info, arg, sizeof(info)))
  59. return -EFAULT;
  60. if (info.mode < 0 || info.mode >= EMUX_MD_END)
  61. return -EINVAL;
  62. info.mode = array_index_nospec(info.mode, EMUX_MD_END);
  63. if (info.port < 0) {
  64. for (i = 0; i < emu->num_ports; i++)
  65. emu->portptrs[i]->ctrls[info.mode] = info.value;
  66. } else {
  67. if (info.port < emu->num_ports) {
  68. info.port = array_index_nospec(info.port, emu->num_ports);
  69. emu->portptrs[info.port]->ctrls[info.mode] = info.value;
  70. }
  71. }
  72. return 0;
  73. }
  74. /*
  75. * ioctl
  76. */
  77. static int
  78. snd_emux_hwdep_ioctl(struct snd_hwdep * hw, struct file *file,
  79. unsigned int cmd, unsigned long arg)
  80. {
  81. struct snd_emux *emu = hw->private_data;
  82. switch (cmd) {
  83. case SNDRV_EMUX_IOCTL_VERSION:
  84. return put_user(SNDRV_EMUX_VERSION, (unsigned int __user *)arg);
  85. case SNDRV_EMUX_IOCTL_LOAD_PATCH:
  86. return snd_emux_hwdep_load_patch(emu, (void __user *)arg);
  87. case SNDRV_EMUX_IOCTL_RESET_SAMPLES:
  88. snd_soundfont_remove_samples(emu->sflist);
  89. break;
  90. case SNDRV_EMUX_IOCTL_REMOVE_LAST_SAMPLES:
  91. snd_soundfont_remove_unlocked(emu->sflist);
  92. break;
  93. case SNDRV_EMUX_IOCTL_MEM_AVAIL:
  94. if (emu->memhdr) {
  95. int size = snd_util_mem_avail(emu->memhdr);
  96. return put_user(size, (unsigned int __user *)arg);
  97. }
  98. break;
  99. case SNDRV_EMUX_IOCTL_MISC_MODE:
  100. return snd_emux_hwdep_misc_mode(emu, (void __user *)arg);
  101. }
  102. return 0;
  103. }
  104. /*
  105. * register hwdep device
  106. */
  107. int
  108. snd_emux_init_hwdep(struct snd_emux *emu)
  109. {
  110. struct snd_hwdep *hw;
  111. int err;
  112. if ((err = snd_hwdep_new(emu->card, SNDRV_EMUX_HWDEP_NAME, emu->hwdep_idx, &hw)) < 0)
  113. return err;
  114. emu->hwdep = hw;
  115. strcpy(hw->name, SNDRV_EMUX_HWDEP_NAME);
  116. hw->iface = SNDRV_HWDEP_IFACE_EMUX_WAVETABLE;
  117. hw->ops.ioctl = snd_emux_hwdep_ioctl;
  118. /* The ioctl parameter types are compatible between 32- and
  119. * 64-bit architectures, so use the same function. */
  120. hw->ops.ioctl_compat = snd_emux_hwdep_ioctl;
  121. hw->exclusive = 1;
  122. hw->private_data = emu;
  123. if ((err = snd_card_register(emu->card)) < 0)
  124. return err;
  125. return 0;
  126. }
  127. /*
  128. * unregister
  129. */
  130. void
  131. snd_emux_delete_hwdep(struct snd_emux *emu)
  132. {
  133. if (emu->hwdep) {
  134. snd_device_free(emu->card, emu->hwdep);
  135. emu->hwdep = NULL;
  136. }
  137. }