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
);
180 err
= kctl
->info(kctl
, &info
);
181 up_read(&card
->controls_rwsem
);
184 *countp
= info
.count
;
189 static int get_elem_size(int type
, int count
)
192 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
193 return sizeof(s64
) * count
;
194 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
195 return sizeof(int) * count
;
196 case SNDRV_CTL_ELEM_TYPE_BYTES
:
198 case SNDRV_CTL_ELEM_TYPE_IEC958
:
199 return sizeof(struct snd_aes_iec958
);
205 static int copy_ctl_value_from_user(struct snd_card
*card
,
206 struct snd_ctl_elem_value
*data
,
207 struct snd_ctl_elem_value32 __user
*data32
,
208 int *typep
, int *countp
)
210 int i
, type
, count
, size
;
211 unsigned int indirect
;
213 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)))
215 if (get_user(indirect
, &data32
->indirect
))
219 type
= get_ctl_type(card
, &data
->id
, &count
);
223 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
224 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
225 for (i
= 0; i
< count
; i
++) {
227 if (get_user(val
, &data32
->value
.integer
[i
]))
229 data
->value
.integer
.value
[i
] = val
;
232 size
= get_elem_size(type
, count
);
234 printk(KERN_ERR
"snd_ioctl32_ctl_elem_value: unknown type %d\n", type
);
237 if (copy_from_user(data
->value
.bytes
.data
,
238 data32
->value
.data
, size
))
247 /* restore the value to 32bit */
248 static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user
*data32
,
249 struct snd_ctl_elem_value
*data
,
254 if (type
== SNDRV_CTL_ELEM_TYPE_BOOLEAN
||
255 type
== SNDRV_CTL_ELEM_TYPE_INTEGER
) {
256 for (i
= 0; i
< count
; i
++) {
258 val
= data
->value
.integer
.value
[i
];
259 if (put_user(val
, &data32
->value
.integer
[i
]))
263 size
= get_elem_size(type
, count
);
264 if (copy_to_user(data32
->value
.data
,
265 data
->value
.bytes
.data
, size
))
271 static int snd_ctl_elem_read_user_compat(struct snd_card
*card
,
272 struct snd_ctl_elem_value32 __user
*data32
)
274 struct snd_ctl_elem_value
*data
;
275 int err
, type
, count
;
277 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
281 if ((err
= copy_ctl_value_from_user(card
, data
, data32
, &type
, &count
)) < 0)
283 if ((err
= snd_ctl_elem_read(card
, data
)) < 0)
285 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
291 static int snd_ctl_elem_write_user_compat(struct snd_ctl_file
*file
,
292 struct snd_ctl_elem_value32 __user
*data32
)
294 struct snd_ctl_elem_value
*data
;
295 int err
, type
, count
;
297 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
301 if ((err
= copy_ctl_value_from_user(file
->card
, data
, data32
, &type
, &count
)) < 0)
303 if ((err
= snd_ctl_elem_write(file
->card
, file
, data
)) < 0)
305 err
= copy_ctl_value_to_user(data32
, data
, type
, count
);
311 /* add or replace a user control */
312 static int snd_ctl_elem_add_compat(struct snd_ctl_file
*file
,
313 struct snd_ctl_elem_info32 __user
*data32
,
316 struct snd_ctl_elem_info
*data
;
319 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
324 /* id, type, access, count */ \
325 if (copy_from_user(&data
->id
, &data32
->id
, sizeof(data
->id
)) ||
326 copy_from_user(&data
->type
, &data32
->type
, 3 * sizeof(u32
)))
328 if (get_user(data
->owner
, &data32
->owner
) ||
329 get_user(data
->type
, &data32
->type
))
331 switch (data
->type
) {
332 case SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
333 case SNDRV_CTL_ELEM_TYPE_INTEGER
:
334 if (get_user(data
->value
.integer
.min
, &data32
->value
.integer
.min
) ||
335 get_user(data
->value
.integer
.max
, &data32
->value
.integer
.max
) ||
336 get_user(data
->value
.integer
.step
, &data32
->value
.integer
.step
))
339 case SNDRV_CTL_ELEM_TYPE_INTEGER64
:
340 if (copy_from_user(&data
->value
.integer64
,
341 &data32
->value
.integer64
,
342 sizeof(data
->value
.integer64
)))
345 case SNDRV_CTL_ELEM_TYPE_ENUMERATED
:
346 if (copy_from_user(&data
->value
.enumerated
,
347 &data32
->value
.enumerated
,
348 sizeof(data
->value
.enumerated
)))
354 err
= snd_ctl_elem_add(file
, data
, replace
);
361 SNDRV_CTL_IOCTL_ELEM_LIST32
= _IOWR('U', 0x10, struct snd_ctl_elem_list32
),
362 SNDRV_CTL_IOCTL_ELEM_INFO32
= _IOWR('U', 0x11, struct snd_ctl_elem_info32
),
363 SNDRV_CTL_IOCTL_ELEM_READ32
= _IOWR('U', 0x12, struct snd_ctl_elem_value32
),
364 SNDRV_CTL_IOCTL_ELEM_WRITE32
= _IOWR('U', 0x13, struct snd_ctl_elem_value32
),
365 SNDRV_CTL_IOCTL_ELEM_ADD32
= _IOWR('U', 0x17, struct snd_ctl_elem_info32
),
366 SNDRV_CTL_IOCTL_ELEM_REPLACE32
= _IOWR('U', 0x18, struct snd_ctl_elem_info32
),
369 static inline long snd_ctl_ioctl_compat(struct file
*file
, unsigned int cmd
, unsigned long arg
)
371 struct snd_ctl_file
*ctl
;
372 struct list_head
*list
;
373 void __user
*argp
= compat_ptr(arg
);
376 ctl
= file
->private_data
;
377 snd_assert(ctl
&& ctl
->card
, return -ENXIO
);
380 case SNDRV_CTL_IOCTL_PVERSION
:
381 case SNDRV_CTL_IOCTL_CARD_INFO
:
382 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS
:
383 case SNDRV_CTL_IOCTL_POWER
:
384 case SNDRV_CTL_IOCTL_POWER_STATE
:
385 case SNDRV_CTL_IOCTL_ELEM_LOCK
:
386 case SNDRV_CTL_IOCTL_ELEM_UNLOCK
:
387 return snd_ctl_ioctl(file
, cmd
, (unsigned long)argp
);
388 case SNDRV_CTL_IOCTL_ELEM_LIST32
:
389 return snd_ctl_elem_list_compat(ctl
->card
, argp
);
390 case SNDRV_CTL_IOCTL_ELEM_INFO32
:
391 return snd_ctl_elem_info_compat(ctl
, argp
);
392 case SNDRV_CTL_IOCTL_ELEM_READ32
:
393 return snd_ctl_elem_read_user_compat(ctl
->card
, argp
);
394 case SNDRV_CTL_IOCTL_ELEM_WRITE32
:
395 return snd_ctl_elem_write_user_compat(ctl
, argp
);
396 case SNDRV_CTL_IOCTL_ELEM_ADD32
:
397 return snd_ctl_elem_add_compat(ctl
, argp
, 0);
398 case SNDRV_CTL_IOCTL_ELEM_REPLACE32
:
399 return snd_ctl_elem_add_compat(ctl
, argp
, 1);
402 down_read(&snd_ioctl_rwsem
);
403 list_for_each(list
, &snd_control_compat_ioctls
) {
404 struct snd_kctl_ioctl
*p
= list_entry(list
, struct snd_kctl_ioctl
, list
);
406 err
= p
->fioctl(ctl
->card
, ctl
, cmd
, arg
);
407 if (err
!= -ENOIOCTLCMD
) {
408 up_read(&snd_ioctl_rwsem
);
413 up_read(&snd_ioctl_rwsem
);