2 * compat ioctls for control API
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
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.
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.
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
21 /* this file included from control.c */
23 #include <linux/compat.h>
24 #include <linux/slab.h>
26 struct snd_ctl_elem_list32
{
32 unsigned char reserved
[50];
33 } /* don't set packed attribute here */;
35 static int snd_ctl_elem_list_compat(struct snd_card
*card
,
36 struct snd_ctl_elem_list32 __user
*data32
)
38 struct snd_ctl_elem_list __user
*data
;
42 data
= compat_alloc_user_space(sizeof(*data
));
44 /* offset, space, used, count */
45 if (copy_in_user(data
, data32
, 4 * sizeof(u32
)))
48 if (get_user(ptr
, &data32
->pids
) ||
49 put_user(compat_ptr(ptr
), &data
->pids
))
51 err
= snd_ctl_elem_list(card
, data
);
55 if (copy_in_user(data32
, data
, 4 * sizeof(u32
)))
61 * control element info
62 * it uses union, so the things are not easy..
65 struct snd_ctl_elem_info32
{
66 struct snd_ctl_elem_id id
; // the size of struct is same
89 unsigned char reserved
[128];
91 unsigned char reserved
[64];
92 } __attribute__((packed
));
94 static int snd_ctl_elem_info_compat(struct snd_ctl_file
*ctl
,
95 struct snd_ctl_elem_info32 __user
*data32
)
97 struct snd_ctl_elem_info
*data
;
100 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
106 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
108 /* we need to copy the item index.
109 * hope this doesn't break anything..
111 if (get_user(data
->value
.enumerated
.item
, &data32
->value
.enumerated
.item
))
114 snd_power_lock(ctl
->card
);
115 err
= snd_power_wait(ctl
->card
, SNDRV_CTL_POWER_D0
);
117 err
= snd_ctl_elem_info(ctl
, data
);
118 snd_power_unlock(ctl
->card
);
122 /* restore info to 32bit */
124 /* id, type, access, count */
125 if (copy_to_user(&data32
->id
, &data
->id
, sizeof(data
->id
)) ||
126 copy_to_user(&data32
->type
, &data
->type
, 3 * sizeof(u32
)))
128 if (put_user(data
->owner
, &data32
->owner
))
130 switch (data
->type
) {
131 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
132 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
133 if (put_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
134 put_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
135 put_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
138 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
139 if (copy_to_user(&data32
->value
.integer64
,
140 &data
->value
.integer64
,
141 sizeof(data
->value
.integer64
)))
144 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
145 if (copy_to_user(&data32
->value
.enumerated
,
146 &data
->value
.enumerated
,
147 sizeof(data
->value
.enumerated
)))
160 struct snd_ctl_elem_value32
{
161 struct snd_ctl_elem_id id
;
162 unsigned int indirect
; /* bit-field causes misalignment */
165 unsigned char data
[512];
166 #ifndef CONFIG_X86_64
170 unsigned char reserved
[128];
174 /* get the value type and count of the control */
175 static int get_ctl_type(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
178 struct snd_kcontrol
*kctl
;
179 struct snd_ctl_elem_info
*info
;
182 down_read(&card
->controls_rwsem
);
183 kctl
= snd_ctl_find_id(card
, id
);
185 up_read(&card
->controls_rwsem
);
188 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
190 up_read(&card
->controls_rwsem
);
194 err
= kctl
->info(kctl
, info
);
195 up_read(&card
->controls_rwsem
);
198 *countp
= info
->count
;
204 static int get_elem_size(int type
, int count
)
207 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
208 return sizeof(s64
) * count
;
209 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
210 return sizeof(int) * count
;
211 case SNDRV_CTL_ELEM_TYPE_BYTES
:
213 case SNDRV_CTL_ELEM_TYPE_IEC958
:
214 return sizeof(struct snd_aes_iec958
);
220 static int copy_ctl_value_from_user(struct snd_card
*card
,
221 struct snd_ctl_elem_value
*data
,
222 struct snd_ctl_elem_value32 __user
*data32
,
223 int *typep
, int *countp
)
226 int uninitialized_var(count
);
227 unsigned int indirect
;
229 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
231 if (get_user(indirect
, &data32
->indirect
))
235 type
= get_ctl_type(card
, &data
->id
, &count
);
239 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
240 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
241 for (i
= 0; i
< count
; i
++) {
243 if (get_user(val
, &data32
->value
.integer
[i
]))
245 data
->value
.integer
.value
[i
] = val
;
248 size
= get_elem_size(type
, count
);
250 dev_err(card
->dev
, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type
);
253 if (copy_from_user(data
->value
.bytes
.data
,
254 data32
->value
.data
, size
))
263 /* restore the value to 32bit */
264 static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user
*data32
,
265 struct snd_ctl_elem_value
*data
,
270 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
271 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
272 for (i
= 0; i
< count
; i
++) {
274 val
= data
->value
.integer
.value
[i
];
275 if (put_user(val
, &data32
->value
.integer
[i
]))
279 size
= get_elem_size(type
, count
);
280 if (copy_to_user(data32
->value
.data
,
281 data
->value
.bytes
.data
, size
))
287 static int snd_ctl_elem_read_user_compat(struct snd_card
*card
,
288 struct snd_ctl_elem_value32 __user
*data32
)
290 struct snd_ctl_elem_value
*data
;
291 int err
, type
, count
;
293 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
297 if ((err
= copy_ctl_value_from_user(card
, data
, data32
, &type
, &count
)) < 0)
300 snd_power_lock(card
);
301 err
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
303 err
= snd_ctl_elem_read(card
, data
);
304 snd_power_unlock(card
);
306 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
312 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file
*file
,
313 struct snd_ctl_elem_value32 __user
*data32
)
315 struct snd_ctl_elem_value
*data
;
316 struct snd_card
*card
= file
->card
;
317 int err
, type
, count
;
319 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
323 if ((err
= copy_ctl_value_from_user(card
, data
, data32
, &type
, &count
)) < 0)
326 snd_power_lock(card
);
327 err
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
329 err
= snd_ctl_elem_write(card
, file
, data
);
330 snd_power_unlock(card
);
332 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
338 /* add or replace a user control */
339 static int snd_ctl_elem_add_compat(struct snd_ctl_file
*file
,
340 struct snd_ctl_elem_info32 __user
*data32
,
343 struct snd_ctl_elem_info
*data
;
346 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
351 /* id, type, access, count */ \
352 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)) ||
353 copy_from_user(&data
->type
, &data32
->type
, 3 * sizeof(u32
)))
355 if (get_user(data
->owner
, &data32
->owner
) ||
356 get_user(data
->type
, &data32
->type
))
358 switch (data
->type
) {
359 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
360 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
361 if (get_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
362 get_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
363 get_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
366 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
367 if (copy_from_user(&data
->value
.integer64
,
368 &data32
->value
.integer64
,
369 sizeof(data
->value
.integer64
)))
372 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
373 if (copy_from_user(&data
->value
.enumerated
,
374 &data32
->value
.enumerated
,
375 sizeof(data
->value
.enumerated
)))
377 data
->value
.enumerated
.names_ptr
=
378 (uintptr_t)compat_ptr(data
->value
.enumerated
.names_ptr
);
383 err
= snd_ctl_elem_add(file
, data
, replace
);
390 SNDRV_CTL_IOCTL_ELEM_LIST32
= _IOWR('U', 0x10, struct snd_ctl_elem_list32
),
391 SNDRV_CTL_IOCTL_ELEM_INFO32
= _IOWR('U', 0x11, struct snd_ctl_elem_info32
),
392 SNDRV_CTL_IOCTL_ELEM_READ32
= _IOWR('U', 0x12, struct snd_ctl_elem_value32
),
393 SNDRV_CTL_IOCTL_ELEM_WRITE32
= _IOWR('U', 0x13, struct snd_ctl_elem_value32
),
394 SNDRV_CTL_IOCTL_ELEM_ADD32
= _IOWR('U', 0x17, struct snd_ctl_elem_info32
),
395 SNDRV_CTL_IOCTL_ELEM_REPLACE32
= _IOWR('U', 0x18, struct snd_ctl_elem_info32
),
398 static inline long snd_ctl_ioctl_compat(struct file
*file
, unsigned int cmd
, unsigned long arg
)
400 struct snd_ctl_file
*ctl
;
401 struct snd_kctl_ioctl
*p
;
402 void __user
*argp
= compat_ptr(arg
);
405 ctl
= file
->private_data
;
406 if (snd_BUG_ON(!ctl
|| !ctl
->card
))
410 case SNDRV_CTL_IOCTL_PVERSION
:
411 case SNDRV_CTL_IOCTL_CARD_INFO
:
412 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
413 case SNDRV_CTL_IOCTL_POWER
:
414 case SNDRV_CTL_IOCTL_POWER_STATE
:
415 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
416 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
417 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
418 case SNDRV_CTL_IOCTL_TLV_READ
:
419 case SNDRV_CTL_IOCTL_TLV_WRITE
:
420 case SNDRV_CTL_IOCTL_TLV_COMMAND
:
421 return snd_ctl_ioctl(file
, cmd
, (unsigned long)argp
);
422 case SNDRV_CTL_IOCTL_ELEM_LIST32
:
423 return snd_ctl_elem_list_compat(ctl
->card
, argp
);
424 case SNDRV_CTL_IOCTL_ELEM_INFO32
:
425 return snd_ctl_elem_info_compat(ctl
, argp
);
426 case SNDRV_CTL_IOCTL_ELEM_READ32
:
427 return snd_ctl_elem_read_user_compat(ctl
->card
, argp
);
428 case SNDRV_CTL_IOCTL_ELEM_WRITE32
:
429 return snd_ctl_elem_write_user_compat(ctl
, argp
);
430 case SNDRV_CTL_IOCTL_ELEM_ADD32
:
431 return snd_ctl_elem_add_compat(ctl
, argp
, 0);
432 case SNDRV_CTL_IOCTL_ELEM_REPLACE32
:
433 return snd_ctl_elem_add_compat(ctl
, argp
, 1);
436 down_read(&snd_ioctl_rwsem
);
437 list_for_each_entry(p
, &snd_control_compat_ioctls
, list
) {
439 err
= p
->fioctl(ctl
->card
, ctl
, cmd
, arg
);
440 if (err
!= -ENOIOCTLCMD
) {
441 up_read(&snd_ioctl_rwsem
);
446 up_read(&snd_ioctl_rwsem
);