Merge tag 'pm+acpi-4.2-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-block.git] / arch / um / drivers / hostaudio_kern.c
CommitLineData
cb8fa61c
JD
1/*
2 * Copyright (C) 2002 Steve Schmidtke
1da177e4
LT
3 * Licensed under the GPL
4 */
5
37185b33
AV
6#include <linux/fs.h>
7#include <linux/module.h>
8#include <linux/slab.h>
9#include <linux/sound.h>
10#include <linux/soundcard.h>
11#include <linux/mutex.h>
12#include <asm/uaccess.h>
13#include <init.h>
14#include <os.h>
1da177e4
LT
15
16struct hostaudio_state {
d471c0fc 17 int fd;
1da177e4
LT
18};
19
20struct hostmixer_state {
d471c0fc 21 int fd;
1da177e4
LT
22};
23
24#define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
25#define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
26
cb8fa61c
JD
27/*
28 * Changed either at boot time or module load time. At boot, this is
b612e475
JD
29 * single-threaded; at module load, multiple modules would each have
30 * their own copy of these variables.
31 */
32static char *dsp = HOSTAUDIO_DEV_DSP;
33static char *mixer = HOSTAUDIO_DEV_MIXER;
1da177e4
LT
34
35#define DSP_HELP \
36" This is used to specify the host dsp device to the hostaudio driver.\n" \
37" The default is \"" HOSTAUDIO_DEV_DSP "\".\n\n"
38
39#define MIXER_HELP \
40" This is used to specify the host mixer device to the hostaudio driver.\n"\
41" The default is \"" HOSTAUDIO_DEV_MIXER "\".\n\n"
42
e3c6cf61
FT
43module_param(dsp, charp, 0644);
44MODULE_PARM_DESC(dsp, DSP_HELP);
45module_param(mixer, charp, 0644);
46MODULE_PARM_DESC(mixer, MIXER_HELP);
47
1da177e4
LT
48#ifndef MODULE
49static int set_dsp(char *name, int *add)
50{
51 dsp = name;
cb8fa61c 52 return 0;
1da177e4
LT
53}
54
55__uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
56
57static int set_mixer(char *name, int *add)
58{
59 mixer = name;
cb8fa61c 60 return 0;
1da177e4
LT
61}
62
63__uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
1da177e4
LT
64#endif
65
9a181c58
AB
66static DEFINE_MUTEX(hostaudio_mutex);
67
1da177e4
LT
68/* /dev/dsp file operations */
69
4d338e1a
AV
70static ssize_t hostaudio_read(struct file *file, char __user *buffer,
71 size_t count, loff_t *ppos)
1da177e4 72{
d471c0fc 73 struct hostaudio_state *state = file->private_data;
1da177e4
LT
74 void *kbuf;
75 int err;
76
77#ifdef DEBUG
cb8fa61c 78 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
1da177e4
LT
79#endif
80
81 kbuf = kmalloc(count, GFP_KERNEL);
cb8fa61c
JD
82 if (kbuf == NULL)
83 return -ENOMEM;
1da177e4 84
a6ea4cce 85 err = os_read_file(state->fd, kbuf, count);
cb8fa61c 86 if (err < 0)
1da177e4
LT
87 goto out;
88
cb8fa61c 89 if (copy_to_user(buffer, kbuf, err))
1da177e4
LT
90 err = -EFAULT;
91
d471c0fc 92out:
1da177e4 93 kfree(kbuf);
cb8fa61c 94 return err;
1da177e4
LT
95}
96
4d338e1a 97static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
1da177e4
LT
98 size_t count, loff_t *ppos)
99{
d471c0fc 100 struct hostaudio_state *state = file->private_data;
1da177e4
LT
101 void *kbuf;
102 int err;
103
104#ifdef DEBUG
cb8fa61c 105 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
1da177e4
LT
106#endif
107
108 kbuf = kmalloc(count, GFP_KERNEL);
cb8fa61c
JD
109 if (kbuf == NULL)
110 return -ENOMEM;
1da177e4
LT
111
112 err = -EFAULT;
cb8fa61c 113 if (copy_from_user(kbuf, buffer, count))
1da177e4
LT
114 goto out;
115
a6ea4cce 116 err = os_write_file(state->fd, kbuf, count);
cb8fa61c 117 if (err < 0)
1da177e4
LT
118 goto out;
119 *ppos += err;
120
121 out:
122 kfree(kbuf);
cb8fa61c 123 return err;
1da177e4
LT
124}
125
cb8fa61c 126static unsigned int hostaudio_poll(struct file *file,
1da177e4
LT
127 struct poll_table_struct *wait)
128{
d471c0fc 129 unsigned int mask = 0;
1da177e4
LT
130
131#ifdef DEBUG
cb8fa61c 132 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
1da177e4
LT
133#endif
134
cb8fa61c 135 return mask;
1da177e4
LT
136}
137
d6c89d9a 138static long hostaudio_ioctl(struct file *file,
1da177e4
LT
139 unsigned int cmd, unsigned long arg)
140{
d471c0fc 141 struct hostaudio_state *state = file->private_data;
1da177e4
LT
142 unsigned long data = 0;
143 int err;
144
145#ifdef DEBUG
cb8fa61c 146 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
1da177e4
LT
147#endif
148 switch(cmd){
149 case SNDCTL_DSP_SPEED:
150 case SNDCTL_DSP_STEREO:
151 case SNDCTL_DSP_GETBLKSIZE:
152 case SNDCTL_DSP_CHANNELS:
153 case SNDCTL_DSP_SUBDIVIDE:
154 case SNDCTL_DSP_SETFRAGMENT:
cb8fa61c 155 if (get_user(data, (int __user *) arg))
484f1e2c 156 return -EFAULT;
1da177e4
LT
157 break;
158 default:
159 break;
160 }
161
162 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
163
164 switch(cmd){
165 case SNDCTL_DSP_SPEED:
166 case SNDCTL_DSP_STEREO:
167 case SNDCTL_DSP_GETBLKSIZE:
168 case SNDCTL_DSP_CHANNELS:
169 case SNDCTL_DSP_SUBDIVIDE:
170 case SNDCTL_DSP_SETFRAGMENT:
cb8fa61c
JD
171 if (put_user(data, (int __user *) arg))
172 return -EFAULT;
1da177e4
LT
173 break;
174 default:
175 break;
176 }
177
cb8fa61c 178 return err;
1da177e4
LT
179}
180
181static int hostaudio_open(struct inode *inode, struct file *file)
182{
d471c0fc
JD
183 struct hostaudio_state *state;
184 int r = 0, w = 0;
185 int ret;
1da177e4
LT
186
187#ifdef DEBUG
b51d23e4 188 kernel_param_lock(THIS_MODULE);
cb8fa61c 189 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
b51d23e4 190 kernel_param_unlock(THIS_MODULE);
1da177e4
LT
191#endif
192
d471c0fc 193 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
cb8fa61c
JD
194 if (state == NULL)
195 return -ENOMEM;
1da177e4 196
cb8fa61c
JD
197 if (file->f_mode & FMODE_READ)
198 r = 1;
199 if (file->f_mode & FMODE_WRITE)
200 w = 1;
1da177e4 201
b51d23e4 202 kernel_param_lock(THIS_MODULE);
9a181c58 203 mutex_lock(&hostaudio_mutex);
1da177e4 204 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
9a181c58 205 mutex_unlock(&hostaudio_mutex);
b51d23e4 206 kernel_param_unlock(THIS_MODULE);
90dc763f 207
cb8fa61c 208 if (ret < 0) {
1da177e4 209 kfree(state);
cb8fa61c 210 return ret;
d471c0fc 211 }
1da177e4 212 state->fd = ret;
d471c0fc 213 file->private_data = state;
cb8fa61c 214 return 0;
1da177e4
LT
215}
216
217static int hostaudio_release(struct inode *inode, struct file *file)
218{
d471c0fc 219 struct hostaudio_state *state = file->private_data;
1da177e4
LT
220
221#ifdef DEBUG
cb8fa61c 222 printk(KERN_DEBUG "hostaudio: release called\n");
1da177e4 223#endif
d471c0fc
JD
224 os_close_file(state->fd);
225 kfree(state);
1da177e4 226
cb8fa61c 227 return 0;
1da177e4
LT
228}
229
230/* /dev/mixer file operations */
231
d6c89d9a 232static long hostmixer_ioctl_mixdev(struct file *file,
1da177e4
LT
233 unsigned int cmd, unsigned long arg)
234{
d471c0fc 235 struct hostmixer_state *state = file->private_data;
1da177e4
LT
236
237#ifdef DEBUG
cb8fa61c 238 printk(KERN_DEBUG "hostmixer: ioctl called\n");
1da177e4
LT
239#endif
240
cb8fa61c 241 return os_ioctl_generic(state->fd, cmd, arg);
1da177e4
LT
242}
243
244static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
245{
d471c0fc
JD
246 struct hostmixer_state *state;
247 int r = 0, w = 0;
248 int ret;
1da177e4
LT
249
250#ifdef DEBUG
cb8fa61c 251 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
1da177e4
LT
252#endif
253
d471c0fc 254 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
cb8fa61c
JD
255 if (state == NULL)
256 return -ENOMEM;
1da177e4 257
cb8fa61c
JD
258 if (file->f_mode & FMODE_READ)
259 r = 1;
260 if (file->f_mode & FMODE_WRITE)
261 w = 1;
1da177e4 262
b51d23e4 263 kernel_param_lock(THIS_MODULE);
9a181c58 264 mutex_lock(&hostaudio_mutex);
1da177e4 265 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
9a181c58 266 mutex_unlock(&hostaudio_mutex);
b51d23e4 267 kernel_param_unlock(THIS_MODULE);
cb8fa61c
JD
268
269 if (ret < 0) {
b51d23e4 270 kernel_param_lock(THIS_MODULE);
cb8fa61c
JD
271 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
272 "err = %d\n", dsp, -ret);
b51d23e4 273 kernel_param_unlock(THIS_MODULE);
1da177e4 274 kfree(state);
cb8fa61c 275 return ret;
d471c0fc 276 }
1da177e4 277
d471c0fc 278 file->private_data = state;
cb8fa61c 279 return 0;
1da177e4
LT
280}
281
282static int hostmixer_release(struct inode *inode, struct file *file)
283{
d471c0fc 284 struct hostmixer_state *state = file->private_data;
1da177e4
LT
285
286#ifdef DEBUG
cb8fa61c 287 printk(KERN_DEBUG "hostmixer: release called\n");
1da177e4
LT
288#endif
289
d471c0fc
JD
290 os_close_file(state->fd);
291 kfree(state);
1da177e4 292
cb8fa61c 293 return 0;
1da177e4
LT
294}
295
1da177e4
LT
296/* kernel module operations */
297
5e7672ec 298static const struct file_operations hostaudio_fops = {
d471c0fc
JD
299 .owner = THIS_MODULE,
300 .llseek = no_llseek,
301 .read = hostaudio_read,
302 .write = hostaudio_write,
303 .poll = hostaudio_poll,
d6c89d9a 304 .unlocked_ioctl = hostaudio_ioctl,
d471c0fc
JD
305 .mmap = NULL,
306 .open = hostaudio_open,
307 .release = hostaudio_release,
1da177e4
LT
308};
309
5e7672ec 310static const struct file_operations hostmixer_fops = {
d471c0fc
JD
311 .owner = THIS_MODULE,
312 .llseek = no_llseek,
d6c89d9a 313 .unlocked_ioctl = hostmixer_ioctl_mixdev,
d471c0fc
JD
314 .open = hostmixer_open_mixdev,
315 .release = hostmixer_release,
1da177e4
LT
316};
317
318struct {
319 int dev_audio;
320 int dev_mixer;
321} module_data;
322
323MODULE_AUTHOR("Steve Schmidtke");
324MODULE_DESCRIPTION("UML Audio Relay");
325MODULE_LICENSE("GPL");
326
327static int __init hostaudio_init_module(void)
328{
b51d23e4 329 kernel_param_lock(THIS_MODULE);
d471c0fc 330 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
1da177e4 331 dsp, mixer);
b51d23e4 332 kernel_param_unlock(THIS_MODULE);
1da177e4
LT
333
334 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
cb8fa61c 335 if (module_data.dev_audio < 0) {
d471c0fc
JD
336 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
337 return -ENODEV;
338 }
1da177e4
LT
339
340 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
cb8fa61c 341 if (module_data.dev_mixer < 0) {
d471c0fc 342 printk(KERN_ERR "hostmixer: couldn't register mixer "
1da177e4 343 "device!\n");
d471c0fc
JD
344 unregister_sound_dsp(module_data.dev_audio);
345 return -ENODEV;
346 }
1da177e4 347
d471c0fc 348 return 0;
1da177e4
LT
349}
350
351static void __exit hostaudio_cleanup_module (void)
352{
d471c0fc
JD
353 unregister_sound_mixer(module_data.dev_mixer);
354 unregister_sound_dsp(module_data.dev_audio);
1da177e4
LT
355}
356
357module_init(hostaudio_init_module);
358module_exit(hostaudio_cleanup_module);