2 * Hardware dependent layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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
22 #include <sound/driver.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/smp_lock.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/minors.h>
31 #include <sound/hwdep.h>
32 #include <sound/info.h>
34 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
35 MODULE_DESCRIPTION("Hardware dependent layer");
36 MODULE_LICENSE("GPL");
38 static snd_hwdep_t
*snd_hwdep_devices
[SNDRV_CARDS
* SNDRV_MINOR_HWDEPS
];
40 static DECLARE_MUTEX(register_mutex
);
42 static int snd_hwdep_free(snd_hwdep_t
*hwdep
);
43 static int snd_hwdep_dev_free(snd_device_t
*device
);
44 static int snd_hwdep_dev_register(snd_device_t
*device
);
45 static int snd_hwdep_dev_unregister(snd_device_t
*device
);
51 static loff_t
snd_hwdep_llseek(struct file
* file
, loff_t offset
, int orig
)
53 snd_hwdep_t
*hw
= file
->private_data
;
55 return hw
->ops
.llseek(hw
, file
, offset
, orig
);
59 static ssize_t
snd_hwdep_read(struct file
* file
, char __user
*buf
, size_t count
, loff_t
*offset
)
61 snd_hwdep_t
*hw
= file
->private_data
;
63 return hw
->ops
.read(hw
, buf
, count
, offset
);
67 static ssize_t
snd_hwdep_write(struct file
* file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
69 snd_hwdep_t
*hw
= file
->private_data
;
71 return hw
->ops
.write(hw
, buf
, count
, offset
);
75 static int snd_hwdep_open(struct inode
*inode
, struct file
* file
)
77 int major
= imajor(inode
);
84 if (major
== snd_major
) {
85 cardnum
= SNDRV_MINOR_CARD(iminor(inode
));
86 device
= SNDRV_MINOR_DEVICE(iminor(inode
)) - SNDRV_MINOR_HWDEP
;
87 #ifdef CONFIG_SND_OSSEMUL
88 } else if (major
== SOUND_MAJOR
) {
89 cardnum
= SNDRV_MINOR_OSS_CARD(iminor(inode
));
94 cardnum
%= SNDRV_CARDS
;
95 device
%= SNDRV_MINOR_HWDEPS
;
96 hw
= snd_hwdep_devices
[(cardnum
* SNDRV_MINOR_HWDEPS
) + device
];
102 #ifdef CONFIG_SND_OSSEMUL
103 if (major
== SOUND_MAJOR
&& hw
->oss_type
< 0)
107 if (!try_module_get(hw
->card
->module
))
110 init_waitqueue_entry(&wait
, current
);
111 add_wait_queue(&hw
->open_wait
, &wait
);
112 down(&hw
->open_mutex
);
114 if (hw
->exclusive
&& hw
->used
> 0) {
118 err
= hw
->ops
.open(hw
, file
);
121 if (err
== -EAGAIN
) {
122 if (file
->f_flags
& O_NONBLOCK
) {
128 set_current_state(TASK_INTERRUPTIBLE
);
131 down(&hw
->open_mutex
);
132 if (signal_pending(current
)) {
137 remove_wait_queue(&hw
->open_wait
, &wait
);
139 err
= snd_card_file_add(hw
->card
, file
);
141 file
->private_data
= hw
;
145 hw
->ops
.release(hw
, file
);
150 module_put(hw
->card
->module
);
154 static int snd_hwdep_release(struct inode
*inode
, struct file
* file
)
157 snd_hwdep_t
*hw
= file
->private_data
;
158 down(&hw
->open_mutex
);
159 if (hw
->ops
.release
) {
160 err
= hw
->ops
.release(hw
, file
);
161 wake_up(&hw
->open_wait
);
165 snd_card_file_remove(hw
->card
, file
);
167 module_put(hw
->card
->module
);
171 static unsigned int snd_hwdep_poll(struct file
* file
, poll_table
* wait
)
173 snd_hwdep_t
*hw
= file
->private_data
;
175 return hw
->ops
.poll(hw
, file
, wait
);
179 static int snd_hwdep_info(snd_hwdep_t
*hw
, snd_hwdep_info_t __user
*_info
)
181 snd_hwdep_info_t info
;
183 memset(&info
, 0, sizeof(info
));
184 info
.card
= hw
->card
->number
;
185 strlcpy(info
.id
, hw
->id
, sizeof(info
.id
));
186 strlcpy(info
.name
, hw
->name
, sizeof(info
.name
));
187 info
.iface
= hw
->iface
;
188 if (copy_to_user(_info
, &info
, sizeof(info
)))
193 static int snd_hwdep_dsp_status(snd_hwdep_t
*hw
, snd_hwdep_dsp_status_t __user
*_info
)
195 snd_hwdep_dsp_status_t info
;
198 if (! hw
->ops
.dsp_status
)
200 memset(&info
, 0, sizeof(info
));
201 info
.dsp_loaded
= hw
->dsp_loaded
;
202 if ((err
= hw
->ops
.dsp_status(hw
, &info
)) < 0)
204 if (copy_to_user(_info
, &info
, sizeof(info
)))
209 static int snd_hwdep_dsp_load(snd_hwdep_t
*hw
, snd_hwdep_dsp_image_t __user
*_info
)
211 snd_hwdep_dsp_image_t info
;
214 if (! hw
->ops
.dsp_load
)
216 memset(&info
, 0, sizeof(info
));
217 if (copy_from_user(&info
, _info
, sizeof(info
)))
219 /* check whether the dsp was already loaded */
220 if (hw
->dsp_loaded
& (1 << info
.index
))
222 if (!access_ok(VERIFY_READ
, info
.image
, info
.length
))
224 err
= hw
->ops
.dsp_load(hw
, &info
);
227 hw
->dsp_loaded
|= (1 << info
.index
);
231 static long snd_hwdep_ioctl(struct file
* file
, unsigned int cmd
, unsigned long arg
)
233 snd_hwdep_t
*hw
= file
->private_data
;
234 void __user
*argp
= (void __user
*)arg
;
236 case SNDRV_HWDEP_IOCTL_PVERSION
:
237 return put_user(SNDRV_HWDEP_VERSION
, (int __user
*)argp
);
238 case SNDRV_HWDEP_IOCTL_INFO
:
239 return snd_hwdep_info(hw
, argp
);
240 case SNDRV_HWDEP_IOCTL_DSP_STATUS
:
241 return snd_hwdep_dsp_status(hw
, argp
);
242 case SNDRV_HWDEP_IOCTL_DSP_LOAD
:
243 return snd_hwdep_dsp_load(hw
, argp
);
246 return hw
->ops
.ioctl(hw
, file
, cmd
, arg
);
250 static int snd_hwdep_mmap(struct file
* file
, struct vm_area_struct
* vma
)
252 snd_hwdep_t
*hw
= file
->private_data
;
254 return hw
->ops
.mmap(hw
, file
, vma
);
258 static int snd_hwdep_control_ioctl(snd_card_t
* card
, snd_ctl_file_t
* control
,
259 unsigned int cmd
, unsigned long arg
)
263 tmp
= card
->number
* SNDRV_MINOR_HWDEPS
;
265 case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE
:
269 if (get_user(device
, (int __user
*)arg
))
271 device
= device
< 0 ? 0 : device
+ 1;
272 while (device
< SNDRV_MINOR_HWDEPS
) {
273 if (snd_hwdep_devices
[tmp
+ device
])
277 if (device
>= SNDRV_MINOR_HWDEPS
)
279 if (put_user(device
, (int __user
*)arg
))
283 case SNDRV_CTL_IOCTL_HWDEP_INFO
:
285 snd_hwdep_info_t __user
*info
= (snd_hwdep_info_t __user
*)arg
;
289 if (get_user(device
, &info
->device
))
291 if (device
< 0 || device
>= SNDRV_MINOR_HWDEPS
)
293 hwdep
= snd_hwdep_devices
[tmp
+ device
];
296 return snd_hwdep_info(hwdep
, info
);
303 #include "hwdep_compat.c"
305 #define snd_hwdep_ioctl_compat NULL
312 static struct file_operations snd_hwdep_f_ops
=
314 .owner
= THIS_MODULE
,
315 .llseek
= snd_hwdep_llseek
,
316 .read
= snd_hwdep_read
,
317 .write
= snd_hwdep_write
,
318 .open
= snd_hwdep_open
,
319 .release
= snd_hwdep_release
,
320 .poll
= snd_hwdep_poll
,
321 .unlocked_ioctl
= snd_hwdep_ioctl
,
322 .compat_ioctl
= snd_hwdep_ioctl_compat
,
323 .mmap
= snd_hwdep_mmap
,
326 static snd_minor_t snd_hwdep_reg
=
328 .comment
= "hardware dependent",
329 .f_ops
= &snd_hwdep_f_ops
,
333 * snd_hwdep_new - create a new hwdep instance
334 * @card: the card instance
336 * @device: the device index (zero-based)
337 * @rhwdep: the pointer to store the new hwdep instance
339 * Creates a new hwdep instance with the given index on the card.
340 * The callbacks (hwdep->ops) must be set on the returned instance
341 * after this call manually by the caller.
343 * Returns zero if successful, or a negative error code on failure.
345 int snd_hwdep_new(snd_card_t
* card
, char *id
, int device
, snd_hwdep_t
** rhwdep
)
349 static snd_device_ops_t ops
= {
350 .dev_free
= snd_hwdep_dev_free
,
351 .dev_register
= snd_hwdep_dev_register
,
352 .dev_unregister
= snd_hwdep_dev_unregister
355 snd_assert(rhwdep
!= NULL
, return -EINVAL
);
357 snd_assert(card
!= NULL
, return -ENXIO
);
358 hwdep
= kzalloc(sizeof(*hwdep
), GFP_KERNEL
);
362 hwdep
->device
= device
;
364 strlcpy(hwdep
->id
, id
, sizeof(hwdep
->id
));
366 #ifdef CONFIG_SND_OSSEMUL
367 hwdep
->oss_type
= -1;
369 if ((err
= snd_device_new(card
, SNDRV_DEV_HWDEP
, hwdep
, &ops
)) < 0) {
370 snd_hwdep_free(hwdep
);
373 init_waitqueue_head(&hwdep
->open_wait
);
374 init_MUTEX(&hwdep
->open_mutex
);
379 static int snd_hwdep_free(snd_hwdep_t
*hwdep
)
381 snd_assert(hwdep
!= NULL
, return -ENXIO
);
382 if (hwdep
->private_free
)
383 hwdep
->private_free(hwdep
);
388 static int snd_hwdep_dev_free(snd_device_t
*device
)
390 snd_hwdep_t
*hwdep
= device
->device_data
;
391 return snd_hwdep_free(hwdep
);
394 static int snd_hwdep_dev_register(snd_device_t
*device
)
396 snd_hwdep_t
*hwdep
= device
->device_data
;
400 down(®ister_mutex
);
401 idx
= (hwdep
->card
->number
* SNDRV_MINOR_HWDEPS
) + hwdep
->device
;
402 if (snd_hwdep_devices
[idx
]) {
406 snd_hwdep_devices
[idx
] = hwdep
;
407 sprintf(name
, "hwC%iD%i", hwdep
->card
->number
, hwdep
->device
);
408 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_HWDEP
,
409 hwdep
->card
, hwdep
->device
,
410 &snd_hwdep_reg
, name
)) < 0) {
411 snd_printk(KERN_ERR
"unable to register hardware dependent device %i:%i\n",
412 hwdep
->card
->number
, hwdep
->device
);
413 snd_hwdep_devices
[idx
] = NULL
;
417 #ifdef CONFIG_SND_OSSEMUL
419 if (hwdep
->oss_type
>= 0) {
420 if ((hwdep
->oss_type
== SNDRV_OSS_DEVICE_TYPE_DMFM
) && (hwdep
->device
!= 0)) {
421 snd_printk (KERN_WARNING
"only hwdep device 0 can be registered as OSS direct FM device!\n");
423 if (snd_register_oss_device(hwdep
->oss_type
,
424 hwdep
->card
, hwdep
->device
,
425 &snd_hwdep_reg
, hwdep
->oss_dev
) < 0) {
426 snd_printk(KERN_ERR
"unable to register OSS compatibility device %i:%i\n",
427 hwdep
->card
->number
, hwdep
->device
);
437 static int snd_hwdep_dev_unregister(snd_device_t
*device
)
439 snd_hwdep_t
*hwdep
= device
->device_data
;
442 snd_assert(hwdep
!= NULL
, return -ENXIO
);
443 down(®ister_mutex
);
444 idx
= (hwdep
->card
->number
* SNDRV_MINOR_HWDEPS
) + hwdep
->device
;
445 if (snd_hwdep_devices
[idx
] != hwdep
) {
449 #ifdef CONFIG_SND_OSSEMUL
451 snd_unregister_oss_device(hwdep
->oss_type
, hwdep
->card
, hwdep
->device
);
453 snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP
, hwdep
->card
, hwdep
->device
);
454 snd_hwdep_devices
[idx
] = NULL
;
456 return snd_hwdep_free(hwdep
);
463 static void snd_hwdep_proc_read(snd_info_entry_t
*entry
,
464 snd_info_buffer_t
* buffer
)
469 down(®ister_mutex
);
470 for (idx
= 0; idx
< SNDRV_CARDS
* SNDRV_MINOR_HWDEPS
; idx
++) {
471 hwdep
= snd_hwdep_devices
[idx
];
474 snd_iprintf(buffer
, "%02i-%02i: %s\n",
475 idx
/ SNDRV_MINOR_HWDEPS
,
476 idx
% SNDRV_MINOR_HWDEPS
,
486 static snd_info_entry_t
*snd_hwdep_proc_entry
= NULL
;
488 static int __init
alsa_hwdep_init(void)
490 snd_info_entry_t
*entry
;
492 memset(snd_hwdep_devices
, 0, sizeof(snd_hwdep_devices
));
493 if ((entry
= snd_info_create_module_entry(THIS_MODULE
, "hwdep", NULL
)) != NULL
) {
494 entry
->c
.text
.read_size
= 512;
495 entry
->c
.text
.read
= snd_hwdep_proc_read
;
496 if (snd_info_register(entry
) < 0) {
497 snd_info_free_entry(entry
);
501 snd_hwdep_proc_entry
= entry
;
502 snd_ctl_register_ioctl(snd_hwdep_control_ioctl
);
503 snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl
);
507 static void __exit
alsa_hwdep_exit(void)
509 snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl
);
510 snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl
);
511 if (snd_hwdep_proc_entry
) {
512 snd_info_unregister(snd_hwdep_proc_entry
);
513 snd_hwdep_proc_entry
= NULL
;
517 module_init(alsa_hwdep_init
)
518 module_exit(alsa_hwdep_exit
)
520 EXPORT_SYMBOL(snd_hwdep_new
);