24 #include <linux/module.h>
25 #include <linux/slab.h>
27 #include <linux/time.h>
34 #define MAX_USER_CONTROLS 32
35 #define MAX_CONTROL_COUNT 1028
45 static LIST_HEAD(snd_control_compat_ioctls);
69 if (!try_module_get(card->
module)) {
78 INIT_LIST_HEAD(&ctl->
events);
102 static void snd_ctl_empty_read_queue(
struct snd_ctl_file * ctl)
108 while (!list_empty(&ctl->
events)) {
113 spin_unlock_irqrestore(&ctl->
read_lock, flags);
116 static int snd_ctl_release(
struct inode *inode,
struct file *file)
132 for (idx = 0; idx < control->
count; idx++)
136 snd_ctl_empty_read_queue(ctl);
154 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
155 card->mixer_oss_change_count++;
162 if (ev->
id.numid == id->numid) {
177 spin_unlock_irqrestore(&ctl->
read_lock, flags);
213 for (idx = 0; idx < kctl->
count; idx++)
214 kctl->
vd[idx].access = access;
237 memset(&kctl, 0,
sizeof(kctl));
238 kctl.
id.iface = ncontrol->
iface;
241 if (ncontrol->
name) {
245 "Control name '%s' truncated to '%s'\n",
246 ncontrol->
name, kctl.
id.name);
248 kctl.
id.index = ncontrol->
index;
263 return snd_ctl_new(&kctl, access);
287 static bool snd_ctl_remove_numid_conflict(
struct snd_card *card,
302 static int snd_ctl_find_hole(
struct snd_card *card,
unsigned int count)
304 unsigned int iter = 100000;
306 while (snd_ctl_remove_numid_conflict(card, count)) {
352 if (snd_ctl_find_hole(card, kcontrol->
count) < 0) {
362 for (idx = 0; idx < kcontrol->
count; idx++,
id.index++,
id.numid++)
417 if (snd_ctl_find_hole(card, kcontrol->
count) < 0) {
427 for (idx = 0; idx < kcontrol->
count; idx++,
id.index++,
id.numid++)
458 for (idx = 0; idx < kcontrol->
count; idx++,
id.index++,
id.numid++)
504 static int snd_ctl_remove_user_ctl(
struct snd_ctl_file * file,
521 for (idx = 0; idx < kctl->
count; idx++)
522 if (kctl->
vd[idx].owner !=
NULL && kctl->
vd[idx].owner != file) {
551 unsigned int index_offset;
560 index_offset = snd_ctl_get_ioff(kctl, &kctl->
id);
561 vd = &kctl->
vd[index_offset];
631 if (kctl->
id.numid <= numid && kctl->
id.numid + kctl->
count > numid)
661 if (kctl->
id.iface != id->
iface)
669 if (kctl->
id.index > id->
index)
681 unsigned int cmd,
void __user *
arg)
712 unsigned int offset, space, jidx;
716 offset =
list.offset;
735 offset -= kctl->
count;
740 while (space > 0 && plist != &card->
controls) {
742 for (jidx = offset; space > 0 && jidx < kctl->
count; jidx++) {
743 snd_ctl_build_ioff(
id, kctl, jidx);
775 unsigned int index_offset;
784 #ifdef CONFIG_SND_DEBUG
787 result = kctl->
info(kctl, info);
790 index_offset = snd_ctl_get_ioff(kctl, &info->
id);
791 vd = &kctl->
vd[index_offset];
792 snd_ctl_build_ioff(&info->
id, kctl, index_offset);
796 if (vd->
owner == ctl)
807 static int snd_ctl_elem_info_user(
struct snd_ctl_file *ctl,
826 static int snd_ctl_elem_read(
struct snd_card *card,
831 unsigned int index_offset;
839 index_offset = snd_ctl_get_ioff(kctl, &control->
id);
840 vd = &kctl->
vd[index_offset];
843 snd_ctl_build_ioff(&control->
id, kctl, index_offset);
844 result = kctl->
get(kctl, control);
852 static int snd_ctl_elem_read_user(
struct snd_card *card,
860 return PTR_ERR(control);
865 result = snd_ctl_elem_read(card, control);
879 unsigned int index_offset;
887 index_offset = snd_ctl_get_ioff(kctl, &control->
id);
888 vd = &kctl->
vd[index_offset];
894 snd_ctl_build_ioff(&control->
id, kctl, index_offset);
895 result = kctl->
put(kctl, control);
908 static int snd_ctl_elem_write_user(
struct snd_ctl_file *file,
917 return PTR_ERR(control);
923 result = snd_ctl_elem_write(card, file, control);
948 vd = &kctl->
vd[snd_ctl_get_ioff(kctl, &
id)];
960 static int snd_ctl_elem_unlock(
struct snd_ctl_file *file,
976 vd = &kctl->
vd[snd_ctl_get_ioff(kctl, &
id)];
979 else if (vd->
owner != file)
999 static int snd_ctl_elem_user_info(
struct snd_kcontrol *kcontrol,
1008 static int snd_ctl_elem_user_enum_info(
struct snd_kcontrol *kcontrol,
1023 for (; item > 0; --
item)
1024 names +=
strlen(names) + 1;
1030 static int snd_ctl_elem_user_get(
struct snd_kcontrol *kcontrol,
1039 static int snd_ctl_elem_user_put(
struct snd_kcontrol *kcontrol,
1051 static int snd_ctl_elem_user_tlv(
struct snd_kcontrol *kcontrol,
1054 unsigned int __user *
tlv)
1061 if (size > 1024 * 128)
1065 if (IS_ERR(new_data))
1066 return PTR_ERR(new_data);
1084 static int snd_ctl_elem_init_enum_names(
struct user_element *ue)
1089 const uintptr_t user_ptrval = ue->
info.value.enumerated.names_ptr;
1091 if (ue->
info.value.enumerated.names_length > 64 * 1024)
1094 names =
memdup_user((
const void __user *)user_ptrval,
1095 ue->
info.value.enumerated.names_length);
1097 return PTR_ERR(names);
1100 buf_len = ue->
info.value.enumerated.names_length;
1102 for (i = 0; i < ue->
info.value.enumerated.items; ++
i) {
1103 name_len =
strnlen(p, buf_len);
1104 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1109 buf_len -= name_len + 1;
1113 ue->
info.value.enumerated.names_ptr = 0;
1118 static void snd_ctl_elem_user_free(
struct snd_kcontrol *kcontrol)
1139 if (info->
count < 1)
1146 memset(&kctl, 0,
sizeof(kctl));
1166 kctl.
info = snd_ctl_elem_user_enum_info;
1168 kctl.
info = snd_ctl_elem_user_info;
1170 kctl.
get = snd_ctl_elem_user_get;
1172 kctl.
put = snd_ctl_elem_user_put;
1174 kctl.
tlv.
c = snd_ctl_elem_user_tlv;
1177 switch (info->
type) {
1180 private_size =
sizeof(
long);
1181 if (info->
count > 128)
1185 private_size =
sizeof(
long long);
1186 if (info->
count > 64)
1190 private_size =
sizeof(
unsigned int);
1195 private_size =
sizeof(
unsigned char);
1196 if (info->
count > 512)
1201 if (info->
count != 1)
1207 private_size *= info->
count;
1212 ue->
info.access = 0;
1213 ue->
elem_data = (
char *)ue +
sizeof(*ue);
1216 err = snd_ctl_elem_init_enum_names(ue);
1223 _kctl = snd_ctl_new(&kctl, access);
1224 if (_kctl ==
NULL) {
1230 for (idx = 0; idx < _kctl->
count; idx++)
1231 _kctl->
vd[idx].owner = file;
1243 static int snd_ctl_elem_add_user(
struct snd_ctl_file *file,
1249 return snd_ctl_elem_add(file, &info, replace);
1252 static int snd_ctl_elem_remove(
struct snd_ctl_file *file,
1259 return snd_ctl_remove_user_ctl(file, &
id);
1262 static int snd_ctl_subscribe_events(
struct snd_ctl_file *file,
int __user *
ptr)
1267 if (subscribe < 0) {
1277 snd_ctl_empty_read_queue(file);
1283 static int snd_ctl_tlv_ioctl(
struct snd_ctl_file *file,
1296 if (tlv.length <
sizeof(
unsigned int) * 2)
1308 vd = &kctl->
vd[tlv.numid - kctl->
id.numid];
1320 err = kctl->
tlv.
c(kctl, op_flag, tlv.length, _tlv->
tlv);
1331 len = kctl->
tlv.
p[1] + 2 *
sizeof(
unsigned int);
1332 if (tlv.length < len) {
1344 static long snd_ctl_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
1365 return snd_ctl_elem_info_user(ctl, argp);
1367 return snd_ctl_elem_read_user(card, argp);
1369 return snd_ctl_elem_write_user(ctl, argp);
1371 return snd_ctl_elem_lock(ctl, argp);
1373 return snd_ctl_elem_unlock(ctl, argp);
1375 return snd_ctl_elem_add_user(ctl, argp, 0);
1377 return snd_ctl_elem_add_user(ctl, argp, 1);
1379 return snd_ctl_elem_remove(ctl, argp);
1381 return snd_ctl_subscribe_events(ctl, ip);
1383 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1385 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1387 return snd_ctl_tlv_ioctl(ctl, argp, -1);
1399 err = p->
fioctl(card, ctl, cmd, arg);
1410 static ssize_t snd_ctl_read(
struct file *file,
char __user *
buffer,
1411 size_t count, loff_t * offset)
1428 while (list_empty(&ctl->
events)) {
1434 init_waitqueue_entry(&wait,
current);
1440 if (ctl->
card->shutdown)
1448 ev.data.elem.mask = kev->
mask;
1449 ev.data.elem.id = kev->
id;
1465 return result > 0 ? result :
err;
1468 static unsigned int snd_ctl_poll(
struct file *file,
poll_table * wait)
1479 if (!list_empty(&ctl->
events))
1505 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1510 #ifdef CONFIG_COMPAT
1513 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1545 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1550 #ifdef CONFIG_COMPAT
1553 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1559 static int snd_ctl_fasync(
int fd,
struct file * file,
int on)
1570 #ifdef CONFIG_COMPAT
1573 #define snd_ctl_ioctl_compat NULL
1583 .read = snd_ctl_read,
1584 .open = snd_ctl_open,
1585 .release = snd_ctl_release,
1587 .poll = snd_ctl_poll,
1588 .unlocked_ioctl = snd_ctl_ioctl,
1590 .fasync = snd_ctl_fasync,
1607 sprintf(name,
"controlC%i", cardnum);
1609 &snd_ctl_f_ops, card, name)) < 0)
1617 static int snd_ctl_dev_disconnect(
struct snd_device *device)
1645 static int snd_ctl_dev_free(
struct snd_device *device)
1651 while (!list_empty(&card->
controls)) {
1667 .dev_register = snd_ctl_dev_register,
1668 .dev_disconnect = snd_ctl_dev_disconnect,
1715 unsigned int items,
const char *
const names[])