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
87 unsigned char reserved
[128];
89 unsigned char reserved
[64];
90 } __attribute__((packed
));
92 static int snd_ctl_elem_info_compat(struct snd_ctl_file
*ctl
,
93 struct snd_ctl_elem_info32 __user
*data32
)
95 struct snd_ctl_elem_info
*data
;
98 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
104 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
106 /* we need to copy the item index.
107 * hope this doesn't break anything..
109 if (get_user(data
->value
.enumerated
.item
, &data32
->value
.enumerated
.item
))
112 snd_power_lock(ctl
->card
);
113 err
= snd_power_wait(ctl
->card
, SNDRV_CTL_POWER_D0
);
115 err
= snd_ctl_elem_info(ctl
, data
);
116 snd_power_unlock(ctl
->card
);
120 /* restore info to 32bit */
122 /* id, type, access, count */
123 if (copy_to_user(&data32
->id
, &data
->id
, sizeof(data
->id
)) ||
124 copy_to_user(&data32
->type
, &data
->type
, 3 * sizeof(u32
)))
126 if (put_user(data
->owner
, &data32
->owner
))
128 switch (data
->type
) {
129 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
130 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
131 if (put_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
132 put_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
133 put_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
136 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
137 if (copy_to_user(&data32
->value
.integer64
,
138 &data
->value
.integer64
,
139 sizeof(data
->value
.integer64
)))
142 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
143 if (copy_to_user(&data32
->value
.enumerated
,
144 &data
->value
.enumerated
,
145 sizeof(data
->value
.enumerated
)))
158 struct snd_ctl_elem_value32
{
159 struct snd_ctl_elem_id id
;
160 unsigned int indirect
; /* bit-field causes misalignment */
163 unsigned char data
[512];
164 #ifndef CONFIG_X86_64
168 unsigned char reserved
[128];
172 /* get the value type and count of the control */
173 static int get_ctl_type(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
176 struct snd_kcontrol
*kctl
;
177 struct snd_ctl_elem_info
*info
;
180 down_read(&card
->controls_rwsem
);
181 kctl
= snd_ctl_find_id(card
, id
);
183 up_read(&card
->controls_rwsem
);
186 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
188 up_read(&card
->controls_rwsem
);
192 err
= kctl
->info(kctl
, info
);
193 up_read(&card
->controls_rwsem
);
196 *countp
= info
->count
;
202 static int get_elem_size(int type
, int count
)
205 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
206 return sizeof(s64
) * count
;
207 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
208 return sizeof(int) * count
;
209 case SNDRV_CTL_ELEM_TYPE_BYTES
:
211 case SNDRV_CTL_ELEM_TYPE_IEC958
:
212 return sizeof(struct snd_aes_iec958
);
218 static int copy_ctl_value_from_user(struct snd_card
*card
,
219 struct snd_ctl_elem_value
*data
,
220 struct snd_ctl_elem_value32 __user
*data32
,
221 int *typep
, int *countp
)
224 int uninitialized_var(count
);
225 unsigned int indirect
;
227 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
229 if (get_user(indirect
, &data32
->indirect
))
233 type
= get_ctl_type(card
, &data
->id
, &count
);
237 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
238 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
239 for (i
= 0; i
< count
; i
++) {
241 if (get_user(val
, &data32
->value
.integer
[i
]))
243 data
->value
.integer
.value
[i
] = val
;
246 size
= get_elem_size(type
, count
);
248 printk(KERN_ERR
"snd_ioctl32_ctl_elem_value: unknown type %d\n", type
);
251 if (copy_from_user(data
->value
.bytes
.data
,
252 data32
->value
.data
, size
))
261 /* restore the value to 32bit */
262 static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user
*data32
,
263 struct snd_ctl_elem_value
*data
,
268 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
269 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
270 for (i
= 0; i
< count
; i
++) {
272 val
= data
->value
.integer
.value
[i
];
273 if (put_user(val
, &data32
->value
.integer
[i
]))
277 size
= get_elem_size(type
, count
);
278 if (copy_to_user(data32
->value
.data
,
279 data
->value
.bytes
.data
, size
))
285 static int snd_ctl_elem_read_user_compat(struct snd_card
*card
,
286 struct snd_ctl_elem_value32 __user
*data32
)
288 struct snd_ctl_elem_value
*data
;
289 int err
, type
, count
;
291 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
295 if ((err
= copy_ctl_value_from_user(card
, data
, data32
, &type
, &count
)) < 0)
298 snd_power_lock(card
);
299 err
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
301 err
= snd_ctl_elem_read(card
, data
);
302 snd_power_unlock(card
);
304 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
310 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file
*file
,
311 struct snd_ctl_elem_value32 __user
*data32
)
313 struct snd_ctl_elem_value
*data
;
314 struct snd_card
*card
= file
->card
;
315 int err
, type
, count
;
317 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
321 if ((err
= copy_ctl_value_from_user(card
, data
, data32
, &type
, &count
)) < 0)
324 snd_power_lock(card
);
325 err
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
327 err
= snd_ctl_elem_write(card
, file
, data
);
328 snd_power_unlock(card
);
330 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
336 /* add or replace a user control */
337 static int snd_ctl_elem_add_compat(struct snd_ctl_file
*file
,
338 struct snd_ctl_elem_info32 __user
*data32
,
341 struct snd_ctl_elem_info
*data
;
344 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
349 /* id, type, access, count */ \
350 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)) ||
351 copy_from_user(&data
->type
, &data32
->type
, 3 * sizeof(u32
)))
353 if (get_user(data
->owner
, &data32
->owner
) ||
354 get_user(data
->type
, &data32
->type
))
356 switch (data
->type
) {
357 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
358 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
359 if (get_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
360 get_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
361 get_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
364 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
365 if (copy_from_user(&data
->value
.integer64
,
366 &data32
->value
.integer64
,
367 sizeof(data
->value
.integer64
)))
370 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
371 if (copy_from_user(&data
->value
.enumerated
,
372 &data32
->value
.enumerated
,
373 sizeof(data
->value
.enumerated
)))
379 err
= snd_ctl_elem_add(file
, data
, replace
);
386 SNDRV_CTL_IOCTL_ELEM_LIST32
= _IOWR('U', 0x10, struct snd_ctl_elem_list32
),
387 SNDRV_CTL_IOCTL_ELEM_INFO32
= _IOWR('U', 0x11, struct snd_ctl_elem_info32
),
388 SNDRV_CTL_IOCTL_ELEM_READ32
= _IOWR('U', 0x12, struct snd_ctl_elem_value32
),
389 SNDRV_CTL_IOCTL_ELEM_WRITE32
= _IOWR('U', 0x13, struct snd_ctl_elem_value32
),
390 SNDRV_CTL_IOCTL_ELEM_ADD32
= _IOWR('U', 0x17, struct snd_ctl_elem_info32
),
391 SNDRV_CTL_IOCTL_ELEM_REPLACE32
= _IOWR('U', 0x18, struct snd_ctl_elem_info32
),
394 static inline long snd_ctl_ioctl_compat(struct file
*file
, unsigned int cmd
, unsigned long arg
)
396 struct snd_ctl_file
*ctl
;
397 struct snd_kctl_ioctl
*p
;
398 void __user
*argp
= compat_ptr(arg
);
401 ctl
= file
->private_data
;
402 if (snd_BUG_ON(!ctl
|| !ctl
->card
))
406 case SNDRV_CTL_IOCTL_PVERSION
:
407 case SNDRV_CTL_IOCTL_CARD_INFO
:
408 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
409 case SNDRV_CTL_IOCTL_POWER
:
410 case SNDRV_CTL_IOCTL_POWER_STATE
:
411 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
412 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
413 case SNDRV_CTL_IOCTL_ELEM_REMOVE
:
414 case SNDRV_CTL_IOCTL_TLV_READ
:
415 case SNDRV_CTL_IOCTL_TLV_WRITE
:
416 case SNDRV_CTL_IOCTL_TLV_COMMAND
:
417 return snd_ctl_ioctl(file
, cmd
, (unsigned long)argp
);
418 case SNDRV_CTL_IOCTL_ELEM_LIST32
:
419 return snd_ctl_elem_list_compat(ctl
->card
, argp
);
420 case SNDRV_CTL_IOCTL_ELEM_INFO32
:
421 return snd_ctl_elem_info_compat(ctl
, argp
);
422 case SNDRV_CTL_IOCTL_ELEM_READ32
:
423 return snd_ctl_elem_read_user_compat(ctl
->card
, argp
);
424 case SNDRV_CTL_IOCTL_ELEM_WRITE32
:
425 return snd_ctl_elem_write_user_compat(ctl
, argp
);
426 case SNDRV_CTL_IOCTL_ELEM_ADD32
:
427 return snd_ctl_elem_add_compat(ctl
, argp
, 0);
428 case SNDRV_CTL_IOCTL_ELEM_REPLACE32
:
429 return snd_ctl_elem_add_compat(ctl
, argp
, 1);
432 down_read(&snd_ioctl_rwsem
);
433 list_for_each_entry(p
, &snd_control_compat_ioctls
, list
) {
435 err
= p
->fioctl(ctl
->card
, ctl
, cmd
, arg
);
436 if (err
!= -ENOIOCTLCMD
) {
437 up_read(&snd_ioctl_rwsem
);
442 up_read(&snd_ioctl_rwsem
);