1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HWDEP Interface for HD-audio codec
5 * Copyright (c) 2007 Takashi Iwai <tiwai@suse.de>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/compat.h>
11 #include <linux/nospec.h>
12 #include <sound/core.h>
13 #include <sound/hda_codec.h>
14 #include "hda_local.h"
15 #include <sound/hda_hwdep.h>
16 #include <sound/minors.h>
19 * write/read an out-of-bound verb
21 static int verb_write_ioctl(struct hda_codec
*codec
,
22 struct hda_verb_ioctl __user
*arg
)
26 if (get_user(verb
, &arg
->verb
))
28 res
= snd_hda_codec_read(codec
, verb
>> 24, 0,
29 (verb
>> 8) & 0xffff, verb
& 0xff);
30 if (put_user(res
, &arg
->res
))
35 static int get_wcap_ioctl(struct hda_codec
*codec
,
36 struct hda_verb_ioctl __user
*arg
)
40 if (get_user(verb
, &arg
->verb
))
42 /* open-code get_wcaps(verb>>24) with nospec */
44 if (verb
< codec
->core
.start_nid
||
45 verb
>= codec
->core
.start_nid
+ codec
->core
.num_nodes
) {
48 verb
-= codec
->core
.start_nid
;
49 verb
= array_index_nospec(verb
, codec
->core
.num_nodes
);
50 res
= codec
->wcaps
[verb
];
52 if (put_user(res
, &arg
->res
))
60 static int hda_hwdep_ioctl(struct snd_hwdep
*hw
, struct file
*file
,
61 unsigned int cmd
, unsigned long arg
)
63 struct hda_codec
*codec
= hw
->private_data
;
64 void __user
*argp
= (void __user
*)arg
;
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
);
78 static int hda_hwdep_ioctl_compat(struct snd_hwdep
*hw
, struct file
*file
,
79 unsigned int cmd
, unsigned long arg
)
81 return hda_hwdep_ioctl(hw
, file
, cmd
, (unsigned long)compat_ptr(arg
));
85 static int hda_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
87 #ifndef CONFIG_SND_DEBUG_VERBOSE
88 if (!capable(CAP_SYS_RAWIO
))
94 int snd_hda_create_hwdep(struct hda_codec
*codec
)
97 struct snd_hwdep
*hwdep
;
100 sprintf(hwname
, "HDA Codec %d", codec
->addr
);
101 err
= snd_hwdep_new(codec
->card
, hwname
, codec
->addr
, &hwdep
);
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;
110 hwdep
->ops
.open
= hda_hwdep_open
;
111 hwdep
->ops
.ioctl
= hda_hwdep_ioctl
;
113 hwdep
->ops
.ioctl_compat
= hda_hwdep_ioctl_compat
;
117 hwdep
->dev
->groups
= snd_hda_dev_attr_groups
;
118 dev_set_drvdata(hwdep
->dev
, codec
);