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>
25 struct snd_ctl_elem_list32
{
31 unsigned char reserved
[50];
32 } /* don't set packed attribute here */;
34 static int snd_ctl_elem_list_compat(struct snd_card
*card
,
35 struct snd_ctl_elem_list32 __user
*data32
)
37 struct snd_ctl_elem_list __user
*data
;
41 data
= compat_alloc_user_space(sizeof(*data
));
43 /* offset, space, used, count */
44 if (copy_in_user(data
, data32
, 4 * sizeof(u32
)))
47 if (get_user(ptr
, &data32
->pids
) ||
48 put_user(compat_ptr(ptr
), &data
->pids
))
50 err
= snd_ctl_elem_list(card
, data
);
54 if (copy_in_user(data32
, data
, 4 * sizeof(u32
)))
60 * control element info
61 * it uses union, so the things are not easy..
64 struct snd_ctl_elem_info32
{
65 struct snd_ctl_elem_id id
; // the size of struct is same
86 unsigned char reserved
[128];
88 unsigned char reserved
[64];
89 } __attribute__((packed
));
91 static int snd_ctl_elem_info_compat(struct snd_ctl_file
*ctl
,
92 struct snd_ctl_elem_info32 __user
*data32
)
94 struct snd_ctl_elem_info
*data
;
97 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
103 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
105 /* we need to copy the item index.
106 * hope this doesn't break anything..
108 if (get_user(data
->value
.enumerated
.item
, &data32
->value
.enumerated
.item
))
110 err
= snd_ctl_elem_info(ctl
, data
);
113 /* restore info to 32bit */
115 /* id, type, access, count */
116 if (copy_to_user(&data32
->id
, &data
->id
, sizeof(data
->id
)) ||
117 copy_to_user(&data32
->type
, &data
->type
, 3 * sizeof(u32
)))
119 if (put_user(data
->owner
, &data32
->owner
))
121 switch (data
->type
) {
122 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
123 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
124 if (put_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
125 put_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
126 put_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
129 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
130 if (copy_to_user(&data32
->value
.integer64
,
131 &data
->value
.integer64
,
132 sizeof(data
->value
.integer64
)))
135 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
136 if (copy_to_user(&data32
->value
.enumerated
,
137 &data
->value
.enumerated
,
138 sizeof(data
->value
.enumerated
)))
151 struct snd_ctl_elem_value32
{
152 struct snd_ctl_elem_id id
;
153 unsigned int indirect
; /* bit-field causes misalignment */
156 unsigned char data
[512];
157 #ifndef CONFIG_X86_64
161 unsigned char reserved
[128];
165 /* get the value type and count of the control */
166 static int get_ctl_type(struct snd_card
*card
, struct snd_ctl_elem_id
*id
,
169 struct snd_kcontrol
*kctl
;
170 struct snd_ctl_elem_info
*info
;
173 down_read(&card
->controls_rwsem
);
174 kctl
= snd_ctl_find_id(card
, id
);
176 up_read(&card
->controls_rwsem
);
179 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
181 up_read(&card
->controls_rwsem
);
185 err
= kctl
->info(kctl
, info
);
186 up_read(&card
->controls_rwsem
);
189 *countp
= info
->count
;
195 static int get_elem_size(int type
, int count
)
198 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
199 return sizeof(s64
) * count
;
200 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
201 return sizeof(int) * count
;
202 case SNDRV_CTL_ELEM_TYPE_BYTES
:
204 case SNDRV_CTL_ELEM_TYPE_IEC958
:
205 return sizeof(struct snd_aes_iec958
);
211 static int copy_ctl_value_from_user(struct snd_card
*card
,
212 struct snd_ctl_elem_value
*data
,
213 struct snd_ctl_elem_value32 __user
*data32
,
214 int *typep
, int *countp
)
216 int i
, type
, count
, size
;
217 unsigned int indirect
;
219 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
221 if (get_user(indirect
, &data32
->indirect
))
225 type
= get_ctl_type(card
, &data
->id
, &count
);
229 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
230 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
231 for (i
= 0; i
< count
; i
++) {
233 if (get_user(val
, &data32
->value
.integer
[i
]))
235 data
->value
.integer
.value
[i
] = val
;
238 size
= get_elem_size(type
, count
);
240 printk(KERN_ERR
"snd_ioctl32_ctl_elem_value: unknown type %d\n", type
);
243 if (copy_from_user(data
->value
.bytes
.data
,
244 data32
->value
.data
, size
))
253 /* restore the value to 32bit */
254 static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user
*data32
,
255 struct snd_ctl_elem_value
*data
,
260 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
261 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
262 for (i
= 0; i
< count
; i
++) {
264 val
= data
->value
.integer
.value
[i
];
265 if (put_user(val
, &data32
->value
.integer
[i
]))
269 size
= get_elem_size(type
, count
);
270 if (copy_to_user(data32
->value
.data
,
271 data
->value
.bytes
.data
, size
))
277 static int snd_ctl_elem_read_user_compat(struct snd_card
*card
,
278 struct snd_ctl_elem_value32 __user
*data32
)
280 struct snd_ctl_elem_value
*data
;
281 int err
, type
, count
;
283 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
287 if ((err
= copy_ctl_value_from_user(card
, data
, data32
, &type
, &count
)) < 0)
289 if ((err
= snd_ctl_elem_read(card
, data
)) < 0)
291 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
297 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file
*file
,
298 struct snd_ctl_elem_value32 __user
*data32
)
300 struct snd_ctl_elem_value
*data
;
301 int err
, type
, count
;
303 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
307 if ((err
= copy_ctl_value_from_user(file
->card
, data
, data32
, &type
, &count
)) < 0)
309 if ((err
= snd_ctl_elem_write(file
->card
, file
, data
)) < 0)
311 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
317 /* add or replace a user control */
318 static int snd_ctl_elem_add_compat(struct snd_ctl_file
*file
,
319 struct snd_ctl_elem_info32 __user
*data32
,
322 struct snd_ctl_elem_info
*data
;
325 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
330 /* id, type, access, count */ \
331 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)) ||
332 copy_from_user(&data
->type
, &data32
->type
, 3 * sizeof(u32
)))
334 if (get_user(data
->owner
, &data32
->owner
) ||
335 get_user(data
->type
, &data32
->type
))
337 switch (data
->type
) {
338 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
339 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
340 if (get_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
341 get_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
342 get_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
345 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
346 if (copy_from_user(&data
->value
.integer64
,
347 &data32
->value
.integer64
,
348 sizeof(data
->value
.integer64
)))
351 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
352 if (copy_from_user(&data
->value
.enumerated
,
353 &data32
->value
.enumerated
,
354 sizeof(data
->value
.enumerated
)))
360 err
= snd_ctl_elem_add(file
, data
, replace
);
367 SNDRV_CTL_IOCTL_ELEM_LIST32
= _IOWR('U', 0x10, struct snd_ctl_elem_list32
),
368 SNDRV_CTL_IOCTL_ELEM_INFO32
= _IOWR('U', 0x11, struct snd_ctl_elem_info32
),
369 SNDRV_CTL_IOCTL_ELEM_READ32
= _IOWR('U', 0x12, struct snd_ctl_elem_value32
),
370 SNDRV_CTL_IOCTL_ELEM_WRITE32
= _IOWR('U', 0x13, struct snd_ctl_elem_value32
),
371 SNDRV_CTL_IOCTL_ELEM_ADD32
= _IOWR('U', 0x17, struct snd_ctl_elem_info32
),
372 SNDRV_CTL_IOCTL_ELEM_REPLACE32
= _IOWR('U', 0x18, struct snd_ctl_elem_info32
),
375 static inline long snd_ctl_ioctl_compat(struct file
*file
, unsigned int cmd
, unsigned long arg
)
377 struct snd_ctl_file
*ctl
;
378 struct list_head
*list
;
379 void __user
*argp
= compat_ptr(arg
);
382 ctl
= file
->private_data
;
383 snd_assert(ctl
&& ctl
->card
, return -ENXIO
);
386 case SNDRV_CTL_IOCTL_PVERSION
:
387 case SNDRV_CTL_IOCTL_CARD_INFO
:
388 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
389 case SNDRV_CTL_IOCTL_POWER
:
390 case SNDRV_CTL_IOCTL_POWER_STATE
:
391 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
392 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
393 return snd_ctl_ioctl(file
, cmd
, (unsigned long)argp
);
394 case SNDRV_CTL_IOCTL_ELEM_LIST32
:
395 return snd_ctl_elem_list_compat(ctl
->card
, argp
);
396 case SNDRV_CTL_IOCTL_ELEM_INFO32
:
397 return snd_ctl_elem_info_compat(ctl
, argp
);
398 case SNDRV_CTL_IOCTL_ELEM_READ32
:
399 return snd_ctl_elem_read_user_compat(ctl
->card
, argp
);
400 case SNDRV_CTL_IOCTL_ELEM_WRITE32
:
401 return snd_ctl_elem_write_user_compat(ctl
, argp
);
402 case SNDRV_CTL_IOCTL_ELEM_ADD32
:
403 return snd_ctl_elem_add_compat(ctl
, argp
, 0);
404 case SNDRV_CTL_IOCTL_ELEM_REPLACE32
:
405 return snd_ctl_elem_add_compat(ctl
, argp
, 1);
408 down_read(&snd_ioctl_rwsem
);
409 list_for_each(list
, &snd_control_compat_ioctls
) {
410 struct snd_kctl_ioctl
*p
= list_entry(list
, struct snd_kctl_ioctl
, list
);
412 err
= p
->fioctl(ctl
->card
, ctl
, cmd
, arg
);
413 if (err
!= -ENOIOCTLCMD
) {
414 up_read(&snd_ioctl_rwsem
);
419 up_read(&snd_ioctl_rwsem
);