1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Interface for hwdep device 4 * 5 * Copyright (C) 2004 Takashi Iwai <tiwai@suse.de> 6 */ 7 8 #include <sound/core.h> 9 #include <sound/hwdep.h> 10 #include <linux/uaccess.h> 11 #include <linux/nospec.h> 12 #include "emux_voice.h" 13 14 #define TMP_CLIENT_ID 0x1001 15 16 /* 17 * load patch 18 */ 19 static int 20 snd_emux_hwdep_load_patch(struct snd_emux *emu, void __user *arg) 21 { 22 int err; 23 struct soundfont_patch_info patch; 24 25 if (copy_from_user(&patch, arg, sizeof(patch))) 26 return -EFAULT; 27 28 if (patch.key == GUS_PATCH) 29 return snd_soundfont_load_guspatch(emu->sflist, arg, 30 patch.len + sizeof(patch)); 31 32 if (patch.type >= SNDRV_SFNT_LOAD_INFO && 33 patch.type <= SNDRV_SFNT_PROBE_DATA) { 34 err = snd_soundfont_load(emu->sflist, arg, patch.len + sizeof(patch), TMP_CLIENT_ID); 35 if (err < 0) 36 return err; 37 } else { 38 if (emu->ops.load_fx) 39 return emu->ops.load_fx(emu, patch.type, patch.optarg, arg, patch.len + sizeof(patch)); 40 else 41 return -EINVAL; 42 } 43 return 0; 44 } 45 46 /* 47 * set misc mode 48 */ 49 static int 50 snd_emux_hwdep_misc_mode(struct snd_emux *emu, void __user *arg) 51 { 52 struct snd_emux_misc_mode info; 53 int i; 54 55 if (copy_from_user(&info, arg, sizeof(info))) 56 return -EFAULT; 57 if (info.mode < 0 || info.mode >= EMUX_MD_END) 58 return -EINVAL; 59 info.mode = array_index_nospec(info.mode, EMUX_MD_END); 60 61 if (info.port < 0) { 62 for (i = 0; i < emu->num_ports; i++) 63 emu->portptrs[i]->ctrls[info.mode] = info.value; 64 } else { 65 if (info.port < emu->num_ports) { 66 info.port = array_index_nospec(info.port, emu->num_ports); 67 emu->portptrs[info.port]->ctrls[info.mode] = info.value; 68 } 69 } 70 return 0; 71 } 72 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 83 switch (cmd) { 84 case SNDRV_EMUX_IOCTL_VERSION: 85 return put_user(SNDRV_EMUX_VERSION, (unsigned int __user *)arg); 86 case SNDRV_EMUX_IOCTL_LOAD_PATCH: 87 return snd_emux_hwdep_load_patch(emu, (void __user *)arg); 88 case SNDRV_EMUX_IOCTL_RESET_SAMPLES: 89 snd_soundfont_remove_samples(emu->sflist); 90 break; 91 case SNDRV_EMUX_IOCTL_REMOVE_LAST_SAMPLES: 92 snd_soundfont_remove_unlocked(emu->sflist); 93 break; 94 case SNDRV_EMUX_IOCTL_MEM_AVAIL: 95 if (emu->memhdr) { 96 int size = snd_util_mem_avail(emu->memhdr); 97 return put_user(size, (unsigned int __user *)arg); 98 } 99 break; 100 case SNDRV_EMUX_IOCTL_MISC_MODE: 101 return snd_emux_hwdep_misc_mode(emu, (void __user *)arg); 102 } 103 104 return 0; 105 } 106 107 108 /* 109 * register hwdep device 110 */ 111 112 int 113 snd_emux_init_hwdep(struct snd_emux *emu) 114 { 115 struct snd_hwdep *hw; 116 int err; 117 118 err = snd_hwdep_new(emu->card, SNDRV_EMUX_HWDEP_NAME, emu->hwdep_idx, &hw); 119 if (err < 0) 120 return err; 121 emu->hwdep = hw; 122 strcpy(hw->name, SNDRV_EMUX_HWDEP_NAME); 123 hw->iface = SNDRV_HWDEP_IFACE_EMUX_WAVETABLE; 124 hw->ops.ioctl = snd_emux_hwdep_ioctl; 125 /* The ioctl parameter types are compatible between 32- and 126 * 64-bit architectures, so use the same function. */ 127 hw->ops.ioctl_compat = snd_emux_hwdep_ioctl; 128 hw->exclusive = 1; 129 hw->private_data = emu; 130 err = snd_card_register(emu->card); 131 if (err < 0) 132 return err; 133 134 return 0; 135 } 136 137 138 /* 139 * unregister 140 */ 141 void 142 snd_emux_delete_hwdep(struct snd_emux *emu) 143 { 144 if (emu->hwdep) { 145 snd_device_free(emu->card, emu->hwdep); 146 emu->hwdep = NULL; 147 } 148 } 149