Thomas Gleixner | d0fa117 | 2019-05-20 19:07:57 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 2 | /* |
| 3 | * HWDEP Interface for HD-audio codec |
| 4 | * |
| 5 | * Copyright (c) 2007 Takashi Iwai <tiwai@suse.de> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 10 | #include <linux/compat.h> |
Takashi Iwai | 69fa6f1 | 2018-04-24 07:50:50 +0200 | [diff] [blame] | 11 | #include <linux/nospec.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 12 | #include <sound/core.h> |
Pierre-Louis Bossart | be57bff | 2018-08-22 15:24:57 -0500 | [diff] [blame] | 13 | #include <sound/hda_codec.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 14 | #include "hda_local.h" |
| 15 | #include <sound/hda_hwdep.h> |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 16 | #include <sound/minors.h> |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * write/read an out-of-bound verb |
| 20 | */ |
| 21 | static int verb_write_ioctl(struct hda_codec *codec, |
| 22 | struct hda_verb_ioctl __user *arg) |
| 23 | { |
| 24 | u32 verb, res; |
| 25 | |
| 26 | if (get_user(verb, &arg->verb)) |
| 27 | return -EFAULT; |
| 28 | res = snd_hda_codec_read(codec, verb >> 24, 0, |
| 29 | (verb >> 8) & 0xffff, verb & 0xff); |
| 30 | if (put_user(res, &arg->res)) |
| 31 | return -EFAULT; |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int get_wcap_ioctl(struct hda_codec *codec, |
| 36 | struct hda_verb_ioctl __user *arg) |
| 37 | { |
| 38 | u32 verb, res; |
| 39 | |
| 40 | if (get_user(verb, &arg->verb)) |
| 41 | return -EFAULT; |
Takashi Iwai | 69fa6f1 | 2018-04-24 07:50:50 +0200 | [diff] [blame] | 42 | /* open-code get_wcaps(verb>>24) with nospec */ |
| 43 | verb >>= 24; |
| 44 | if (verb < codec->core.start_nid || |
| 45 | verb >= codec->core.start_nid + codec->core.num_nodes) { |
| 46 | res = 0; |
| 47 | } else { |
| 48 | verb -= codec->core.start_nid; |
| 49 | verb = array_index_nospec(verb, codec->core.num_nodes); |
| 50 | res = codec->wcaps[verb]; |
| 51 | } |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 52 | if (put_user(res, &arg->res)) |
| 53 | return -EFAULT; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | */ |
| 60 | static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, |
| 61 | unsigned int cmd, unsigned long arg) |
| 62 | { |
| 63 | struct hda_codec *codec = hw->private_data; |
| 64 | void __user *argp = (void __user *)arg; |
| 65 | |
| 66 | switch (cmd) { |
| 67 | case HDA_IOCTL_PVERSION: |
| 68 | return put_user(HDA_HWDEP_VERSION, (int __user *)argp); |
| 69 | case HDA_IOCTL_VERB_WRITE: |
| 70 | return verb_write_ioctl(codec, argp); |
| 71 | case HDA_IOCTL_GET_WCAP: |
| 72 | return get_wcap_ioctl(codec, argp); |
| 73 | } |
| 74 | return -ENOIOCTLCMD; |
| 75 | } |
| 76 | |
| 77 | #ifdef CONFIG_COMPAT |
| 78 | static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file, |
| 79 | unsigned int cmd, unsigned long arg) |
| 80 | { |
Takashi Iwai | 312d045 | 2007-07-31 11:08:10 +0200 | [diff] [blame] | 81 | return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg)); |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 82 | } |
| 83 | #endif |
| 84 | |
| 85 | static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file) |
| 86 | { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 87 | #ifndef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 88 | if (!capable(CAP_SYS_RAWIO)) |
| 89 | return -EACCES; |
| 90 | #endif |
| 91 | return 0; |
| 92 | } |
| 93 | |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 94 | int snd_hda_create_hwdep(struct hda_codec *codec) |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 95 | { |
| 96 | char hwname[16]; |
| 97 | struct snd_hwdep *hwdep; |
| 98 | int err; |
| 99 | |
| 100 | sprintf(hwname, "HDA Codec %d", codec->addr); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 101 | err = snd_hwdep_new(codec->card, hwname, codec->addr, &hwdep); |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 102 | if (err < 0) |
| 103 | return err; |
| 104 | codec->hwdep = hwdep; |
| 105 | sprintf(hwdep->name, "HDA Codec %d", codec->addr); |
| 106 | hwdep->iface = SNDRV_HWDEP_IFACE_HDA; |
| 107 | hwdep->private_data = codec; |
| 108 | hwdep->exclusive = 1; |
| 109 | |
| 110 | hwdep->ops.open = hda_hwdep_open; |
| 111 | hwdep->ops.ioctl = hda_hwdep_ioctl; |
| 112 | #ifdef CONFIG_COMPAT |
| 113 | hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat; |
| 114 | #endif |
| 115 | |
Takashi Iwai | 7b46160 | 2015-01-29 17:13:32 +0100 | [diff] [blame] | 116 | /* for sysfs */ |
| 117 | hwdep->dev.groups = snd_hda_dev_attr_groups; |
| 118 | dev_set_drvdata(&hwdep->dev, codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 119 | |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 120 | return 0; |
| 121 | } |