Merge master.kernel.org:/pub/scm/linux/kernel/git/sam/kbuild
[deliverable/linux.git] / sound / core / hwdep.c
1 /*
2 * Hardware dependent layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
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>
33
34 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
35 MODULE_DESCRIPTION("Hardware dependent layer");
36 MODULE_LICENSE("GPL");
37
38 static snd_hwdep_t *snd_hwdep_devices[SNDRV_CARDS * SNDRV_MINOR_HWDEPS];
39
40 static DECLARE_MUTEX(register_mutex);
41
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);
46
47 /*
48
49 */
50
51 static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
52 {
53 snd_hwdep_t *hw = file->private_data;
54 if (hw->ops.llseek)
55 return hw->ops.llseek(hw, file, offset, orig);
56 return -ENXIO;
57 }
58
59 static ssize_t snd_hwdep_read(struct file * file, char __user *buf, size_t count, loff_t *offset)
60 {
61 snd_hwdep_t *hw = file->private_data;
62 if (hw->ops.read)
63 return hw->ops.read(hw, buf, count, offset);
64 return -ENXIO;
65 }
66
67 static ssize_t snd_hwdep_write(struct file * file, const char __user *buf, size_t count, loff_t *offset)
68 {
69 snd_hwdep_t *hw = file->private_data;
70 if (hw->ops.write)
71 return hw->ops.write(hw, buf, count, offset);
72 return -ENXIO;
73 }
74
75 static int snd_hwdep_open(struct inode *inode, struct file * file)
76 {
77 int major = imajor(inode);
78 int cardnum;
79 int device;
80 snd_hwdep_t *hw;
81 int err;
82 wait_queue_t wait;
83
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));
90 device = 0;
91 #endif
92 } else
93 return -ENXIO;
94 cardnum %= SNDRV_CARDS;
95 device %= SNDRV_MINOR_HWDEPS;
96 hw = snd_hwdep_devices[(cardnum * SNDRV_MINOR_HWDEPS) + device];
97 if (hw == NULL)
98 return -ENODEV;
99
100 if (!hw->ops.open)
101 return -ENXIO;
102 #ifdef CONFIG_SND_OSSEMUL
103 if (major == SOUND_MAJOR && hw->oss_type < 0)
104 return -ENXIO;
105 #endif
106
107 if (!try_module_get(hw->card->module))
108 return -EFAULT;
109
110 init_waitqueue_entry(&wait, current);
111 add_wait_queue(&hw->open_wait, &wait);
112 down(&hw->open_mutex);
113 while (1) {
114 if (hw->exclusive && hw->used > 0) {
115 err = -EBUSY;
116 break;
117 }
118 err = hw->ops.open(hw, file);
119 if (err >= 0)
120 break;
121 if (err == -EAGAIN) {
122 if (file->f_flags & O_NONBLOCK) {
123 err = -EBUSY;
124 break;
125 }
126 } else
127 break;
128 set_current_state(TASK_INTERRUPTIBLE);
129 up(&hw->open_mutex);
130 schedule();
131 down(&hw->open_mutex);
132 if (signal_pending(current)) {
133 err = -ERESTARTSYS;
134 break;
135 }
136 }
137 remove_wait_queue(&hw->open_wait, &wait);
138 if (err >= 0) {
139 err = snd_card_file_add(hw->card, file);
140 if (err >= 0) {
141 file->private_data = hw;
142 hw->used++;
143 } else {
144 if (hw->ops.release)
145 hw->ops.release(hw, file);
146 }
147 }
148 up(&hw->open_mutex);
149 if (err < 0)
150 module_put(hw->card->module);
151 return err;
152 }
153
154 static int snd_hwdep_release(struct inode *inode, struct file * file)
155 {
156 int err = -ENXIO;
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);
162 }
163 if (hw->used > 0)
164 hw->used--;
165 snd_card_file_remove(hw->card, file);
166 up(&hw->open_mutex);
167 module_put(hw->card->module);
168 return err;
169 }
170
171 static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
172 {
173 snd_hwdep_t *hw = file->private_data;
174 if (hw->ops.poll)
175 return hw->ops.poll(hw, file, wait);
176 return 0;
177 }
178
179 static int snd_hwdep_info(snd_hwdep_t *hw, snd_hwdep_info_t __user *_info)
180 {
181 snd_hwdep_info_t info;
182
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)))
189 return -EFAULT;
190 return 0;
191 }
192
193 static int snd_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t __user *_info)
194 {
195 snd_hwdep_dsp_status_t info;
196 int err;
197
198 if (! hw->ops.dsp_status)
199 return -ENXIO;
200 memset(&info, 0, sizeof(info));
201 info.dsp_loaded = hw->dsp_loaded;
202 if ((err = hw->ops.dsp_status(hw, &info)) < 0)
203 return err;
204 if (copy_to_user(_info, &info, sizeof(info)))
205 return -EFAULT;
206 return 0;
207 }
208
209 static int snd_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t __user *_info)
210 {
211 snd_hwdep_dsp_image_t info;
212 int err;
213
214 if (! hw->ops.dsp_load)
215 return -ENXIO;
216 memset(&info, 0, sizeof(info));
217 if (copy_from_user(&info, _info, sizeof(info)))
218 return -EFAULT;
219 /* check whether the dsp was already loaded */
220 if (hw->dsp_loaded & (1 << info.index))
221 return -EBUSY;
222 if (!access_ok(VERIFY_READ, info.image, info.length))
223 return -EFAULT;
224 err = hw->ops.dsp_load(hw, &info);
225 if (err < 0)
226 return err;
227 hw->dsp_loaded |= (1 << info.index);
228 return 0;
229 }
230
231 static long snd_hwdep_ioctl(struct file * file, unsigned int cmd, unsigned long arg)
232 {
233 snd_hwdep_t *hw = file->private_data;
234 void __user *argp = (void __user *)arg;
235 switch (cmd) {
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);
244 }
245 if (hw->ops.ioctl)
246 return hw->ops.ioctl(hw, file, cmd, arg);
247 return -ENOTTY;
248 }
249
250 static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
251 {
252 snd_hwdep_t *hw = file->private_data;
253 if (hw->ops.mmap)
254 return hw->ops.mmap(hw, file, vma);
255 return -ENXIO;
256 }
257
258 static int snd_hwdep_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
259 unsigned int cmd, unsigned long arg)
260 {
261 unsigned int tmp;
262
263 tmp = card->number * SNDRV_MINOR_HWDEPS;
264 switch (cmd) {
265 case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
266 {
267 int device;
268
269 if (get_user(device, (int __user *)arg))
270 return -EFAULT;
271 device = device < 0 ? 0 : device + 1;
272 while (device < SNDRV_MINOR_HWDEPS) {
273 if (snd_hwdep_devices[tmp + device])
274 break;
275 device++;
276 }
277 if (device >= SNDRV_MINOR_HWDEPS)
278 device = -1;
279 if (put_user(device, (int __user *)arg))
280 return -EFAULT;
281 return 0;
282 }
283 case SNDRV_CTL_IOCTL_HWDEP_INFO:
284 {
285 snd_hwdep_info_t __user *info = (snd_hwdep_info_t __user *)arg;
286 int device;
287 snd_hwdep_t *hwdep;
288
289 if (get_user(device, &info->device))
290 return -EFAULT;
291 if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
292 return -ENXIO;
293 hwdep = snd_hwdep_devices[tmp + device];
294 if (hwdep == NULL)
295 return -ENXIO;
296 return snd_hwdep_info(hwdep, info);
297 }
298 }
299 return -ENOIOCTLCMD;
300 }
301
302 #ifdef CONFIG_COMPAT
303 #include "hwdep_compat.c"
304 #else
305 #define snd_hwdep_ioctl_compat NULL
306 #endif
307
308 /*
309
310 */
311
312 static struct file_operations snd_hwdep_f_ops =
313 {
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,
324 };
325
326 static snd_minor_t snd_hwdep_reg =
327 {
328 .comment = "hardware dependent",
329 .f_ops = &snd_hwdep_f_ops,
330 };
331
332 /**
333 * snd_hwdep_new - create a new hwdep instance
334 * @card: the card instance
335 * @id: the id string
336 * @device: the device index (zero-based)
337 * @rhwdep: the pointer to store the new hwdep instance
338 *
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.
342 *
343 * Returns zero if successful, or a negative error code on failure.
344 */
345 int snd_hwdep_new(snd_card_t * card, char *id, int device, snd_hwdep_t ** rhwdep)
346 {
347 snd_hwdep_t *hwdep;
348 int err;
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
353 };
354
355 snd_assert(rhwdep != NULL, return -EINVAL);
356 *rhwdep = NULL;
357 snd_assert(card != NULL, return -ENXIO);
358 hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
359 if (hwdep == NULL)
360 return -ENOMEM;
361 hwdep->card = card;
362 hwdep->device = device;
363 if (id) {
364 strlcpy(hwdep->id, id, sizeof(hwdep->id));
365 }
366 #ifdef CONFIG_SND_OSSEMUL
367 hwdep->oss_type = -1;
368 #endif
369 if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
370 snd_hwdep_free(hwdep);
371 return err;
372 }
373 init_waitqueue_head(&hwdep->open_wait);
374 init_MUTEX(&hwdep->open_mutex);
375 *rhwdep = hwdep;
376 return 0;
377 }
378
379 static int snd_hwdep_free(snd_hwdep_t *hwdep)
380 {
381 snd_assert(hwdep != NULL, return -ENXIO);
382 if (hwdep->private_free)
383 hwdep->private_free(hwdep);
384 kfree(hwdep);
385 return 0;
386 }
387
388 static int snd_hwdep_dev_free(snd_device_t *device)
389 {
390 snd_hwdep_t *hwdep = device->device_data;
391 return snd_hwdep_free(hwdep);
392 }
393
394 static int snd_hwdep_dev_register(snd_device_t *device)
395 {
396 snd_hwdep_t *hwdep = device->device_data;
397 int idx, err;
398 char name[32];
399
400 down(&register_mutex);
401 idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
402 if (snd_hwdep_devices[idx]) {
403 up(&register_mutex);
404 return -EBUSY;
405 }
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;
414 up(&register_mutex);
415 return err;
416 }
417 #ifdef CONFIG_SND_OSSEMUL
418 hwdep->ossreg = 0;
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");
422 } else {
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);
428 } else
429 hwdep->ossreg = 1;
430 }
431 }
432 #endif
433 up(&register_mutex);
434 return 0;
435 }
436
437 static int snd_hwdep_dev_unregister(snd_device_t *device)
438 {
439 snd_hwdep_t *hwdep = device->device_data;
440 int idx;
441
442 snd_assert(hwdep != NULL, return -ENXIO);
443 down(&register_mutex);
444 idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
445 if (snd_hwdep_devices[idx] != hwdep) {
446 up(&register_mutex);
447 return -EINVAL;
448 }
449 #ifdef CONFIG_SND_OSSEMUL
450 if (hwdep->ossreg)
451 snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
452 #endif
453 snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
454 snd_hwdep_devices[idx] = NULL;
455 up(&register_mutex);
456 return snd_hwdep_free(hwdep);
457 }
458
459 /*
460 * Info interface
461 */
462
463 static void snd_hwdep_proc_read(snd_info_entry_t *entry,
464 snd_info_buffer_t * buffer)
465 {
466 int idx;
467 snd_hwdep_t *hwdep;
468
469 down(&register_mutex);
470 for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
471 hwdep = snd_hwdep_devices[idx];
472 if (hwdep == NULL)
473 continue;
474 snd_iprintf(buffer, "%02i-%02i: %s\n",
475 idx / SNDRV_MINOR_HWDEPS,
476 idx % SNDRV_MINOR_HWDEPS,
477 hwdep->name);
478 }
479 up(&register_mutex);
480 }
481
482 /*
483 * ENTRY functions
484 */
485
486 static snd_info_entry_t *snd_hwdep_proc_entry = NULL;
487
488 static int __init alsa_hwdep_init(void)
489 {
490 snd_info_entry_t *entry;
491
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);
498 entry = NULL;
499 }
500 }
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);
504 return 0;
505 }
506
507 static void __exit alsa_hwdep_exit(void)
508 {
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;
514 }
515 }
516
517 module_init(alsa_hwdep_init)
518 module_exit(alsa_hwdep_exit)
519
520 EXPORT_SYMBOL(snd_hwdep_new);
This page took 0.042082 seconds and 6 git commands to generate.