hostaudio_kern.c 7.5 KB
Newer Older
J
Jeff Dike 已提交
1 2
/*
 * Copyright (C) 2002 Steve Schmidtke
L
Linus Torvalds 已提交
3 4 5
 * Licensed under the GPL
 */

J
Jeff Dike 已提交
6
#include "linux/fs.h"
L
Linus Torvalds 已提交
7 8 9 10
#include "linux/module.h"
#include "linux/slab.h"
#include "linux/sound.h"
#include "linux/soundcard.h"
11
#include "linux/smp_lock.h"
L
Linus Torvalds 已提交
12 13 14 15 16
#include "asm/uaccess.h"
#include "init.h"
#include "os.h"

struct hostaudio_state {
J
Jeff Dike 已提交
17
	int fd;
L
Linus Torvalds 已提交
18 19 20
};

struct hostmixer_state {
J
Jeff Dike 已提交
21
	int fd;
L
Linus Torvalds 已提交
22 23 24 25 26
};

#define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
#define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"

J
Jeff Dike 已提交
27 28
/*
 * Changed either at boot time or module load time.  At boot, this is
J
Jeff Dike 已提交
29 30 31 32 33
 * single-threaded; at module load, multiple modules would each have
 * their own copy of these variables.
 */
static char *dsp = HOSTAUDIO_DEV_DSP;
static char *mixer = HOSTAUDIO_DEV_MIXER;
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46

#define DSP_HELP \
"    This is used to specify the host dsp device to the hostaudio driver.\n" \
"    The default is \"" HOSTAUDIO_DEV_DSP "\".\n\n"

#define MIXER_HELP \
"    This is used to specify the host mixer device to the hostaudio driver.\n"\
"    The default is \"" HOSTAUDIO_DEV_MIXER "\".\n\n"

#ifndef MODULE
static int set_dsp(char *name, int *add)
{
	dsp = name;
J
Jeff Dike 已提交
47
	return 0;
L
Linus Torvalds 已提交
48 49 50 51 52 53 54
}

__uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);

static int set_mixer(char *name, int *add)
{
	mixer = name;
J
Jeff Dike 已提交
55
	return 0;
L
Linus Torvalds 已提交
56 57 58 59 60 61
}

__uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);

#else /*MODULE*/

62
module_param(dsp, charp, 0644);
L
Linus Torvalds 已提交
63 64
MODULE_PARM_DESC(dsp, DSP_HELP);

65
module_param(mixer, charp, 0644);
L
Linus Torvalds 已提交
66 67 68 69 70 71
MODULE_PARM_DESC(mixer, MIXER_HELP);

#endif

/* /dev/dsp file operations */

A
Al Viro 已提交
72 73
static ssize_t hostaudio_read(struct file *file, char __user *buffer,
			      size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
74
{
J
Jeff Dike 已提交
75
	struct hostaudio_state *state = file->private_data;
L
Linus Torvalds 已提交
76 77 78 79
	void *kbuf;
	int err;

#ifdef DEBUG
J
Jeff Dike 已提交
80
	printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
L
Linus Torvalds 已提交
81 82 83
#endif

	kbuf = kmalloc(count, GFP_KERNEL);
J
Jeff Dike 已提交
84 85
	if (kbuf == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
86

87
	err = os_read_file(state->fd, kbuf, count);
J
Jeff Dike 已提交
88
	if (err < 0)
L
Linus Torvalds 已提交
89 90
		goto out;

J
Jeff Dike 已提交
91
	if (copy_to_user(buffer, kbuf, err))
L
Linus Torvalds 已提交
92 93
		err = -EFAULT;

J
Jeff Dike 已提交
94
out:
L
Linus Torvalds 已提交
95
	kfree(kbuf);
J
Jeff Dike 已提交
96
	return err;
L
Linus Torvalds 已提交
97 98
}

A
Al Viro 已提交
99
static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
L
Linus Torvalds 已提交
100 101
			       size_t count, loff_t *ppos)
{
J
Jeff Dike 已提交
102
	struct hostaudio_state *state = file->private_data;
L
Linus Torvalds 已提交
103 104 105 106
	void *kbuf;
	int err;

#ifdef DEBUG
J
Jeff Dike 已提交
107
	printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
L
Linus Torvalds 已提交
108 109 110
#endif

	kbuf = kmalloc(count, GFP_KERNEL);
J
Jeff Dike 已提交
111 112
	if (kbuf == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
113 114

	err = -EFAULT;
J
Jeff Dike 已提交
115
	if (copy_from_user(kbuf, buffer, count))
L
Linus Torvalds 已提交
116 117
		goto out;

118
	err = os_write_file(state->fd, kbuf, count);
J
Jeff Dike 已提交
119
	if (err < 0)
L
Linus Torvalds 已提交
120 121 122 123 124
		goto out;
	*ppos += err;

 out:
	kfree(kbuf);
J
Jeff Dike 已提交
125
	return err;
L
Linus Torvalds 已提交
126 127
}

J
Jeff Dike 已提交
128
static unsigned int hostaudio_poll(struct file *file,
L
Linus Torvalds 已提交
129 130
				   struct poll_table_struct *wait)
{
J
Jeff Dike 已提交
131
	unsigned int mask = 0;
L
Linus Torvalds 已提交
132 133

#ifdef DEBUG
J
Jeff Dike 已提交
134
	printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
L
Linus Torvalds 已提交
135 136
#endif

J
Jeff Dike 已提交
137
	return mask;
L
Linus Torvalds 已提交
138 139
}

140
static long hostaudio_ioctl(struct file *file,
L
Linus Torvalds 已提交
141 142
			   unsigned int cmd, unsigned long arg)
{
J
Jeff Dike 已提交
143
	struct hostaudio_state *state = file->private_data;
L
Linus Torvalds 已提交
144 145 146 147
	unsigned long data = 0;
	int err;

#ifdef DEBUG
J
Jeff Dike 已提交
148
	printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156
#endif
	switch(cmd){
	case SNDCTL_DSP_SPEED:
	case SNDCTL_DSP_STEREO:
	case SNDCTL_DSP_GETBLKSIZE:
	case SNDCTL_DSP_CHANNELS:
	case SNDCTL_DSP_SUBDIVIDE:
	case SNDCTL_DSP_SETFRAGMENT:
J
Jeff Dike 已提交
157
		if (get_user(data, (int __user *) arg))
J
Johann Felix Soden 已提交
158
			return -EFAULT;
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172
		break;
	default:
		break;
	}

	err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);

	switch(cmd){
	case SNDCTL_DSP_SPEED:
	case SNDCTL_DSP_STEREO:
	case SNDCTL_DSP_GETBLKSIZE:
	case SNDCTL_DSP_CHANNELS:
	case SNDCTL_DSP_SUBDIVIDE:
	case SNDCTL_DSP_SETFRAGMENT:
J
Jeff Dike 已提交
173 174
		if (put_user(data, (int __user *) arg))
			return -EFAULT;
L
Linus Torvalds 已提交
175 176 177 178 179
		break;
	default:
		break;
	}

J
Jeff Dike 已提交
180
	return err;
L
Linus Torvalds 已提交
181 182 183 184
}

static int hostaudio_open(struct inode *inode, struct file *file)
{
J
Jeff Dike 已提交
185 186 187
	struct hostaudio_state *state;
	int r = 0, w = 0;
	int ret;
L
Linus Torvalds 已提交
188 189

#ifdef DEBUG
190
	kparam_block_sysfs_write(dsp);
J
Jeff Dike 已提交
191
	printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
192
	kparam_unblock_sysfs_write(dsp);
L
Linus Torvalds 已提交
193 194
#endif

J
Jeff Dike 已提交
195
	state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
J
Jeff Dike 已提交
196 197
	if (state == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
198

J
Jeff Dike 已提交
199 200 201 202
	if (file->f_mode & FMODE_READ)
		r = 1;
	if (file->f_mode & FMODE_WRITE)
		w = 1;
L
Linus Torvalds 已提交
203

204
	kparam_block_sysfs_write(dsp);
205
	lock_kernel();
L
Linus Torvalds 已提交
206
	ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
207
	unlock_kernel();
208
	kparam_unblock_sysfs_write(dsp);
209

J
Jeff Dike 已提交
210
	if (ret < 0) {
L
Linus Torvalds 已提交
211
		kfree(state);
J
Jeff Dike 已提交
212
		return ret;
J
Jeff Dike 已提交
213
	}
L
Linus Torvalds 已提交
214
	state->fd = ret;
J
Jeff Dike 已提交
215
	file->private_data = state;
J
Jeff Dike 已提交
216
	return 0;
L
Linus Torvalds 已提交
217 218 219 220
}

static int hostaudio_release(struct inode *inode, struct file *file)
{
J
Jeff Dike 已提交
221
	struct hostaudio_state *state = file->private_data;
L
Linus Torvalds 已提交
222 223

#ifdef DEBUG
J
Jeff Dike 已提交
224
	printk(KERN_DEBUG "hostaudio: release called\n");
L
Linus Torvalds 已提交
225
#endif
J
Jeff Dike 已提交
226 227
	os_close_file(state->fd);
	kfree(state);
L
Linus Torvalds 已提交
228

J
Jeff Dike 已提交
229
	return 0;
L
Linus Torvalds 已提交
230 231 232 233
}

/* /dev/mixer file operations */

234
static long hostmixer_ioctl_mixdev(struct file *file,
L
Linus Torvalds 已提交
235 236
				  unsigned int cmd, unsigned long arg)
{
J
Jeff Dike 已提交
237
	struct hostmixer_state *state = file->private_data;
L
Linus Torvalds 已提交
238 239

#ifdef DEBUG
J
Jeff Dike 已提交
240
	printk(KERN_DEBUG "hostmixer: ioctl called\n");
L
Linus Torvalds 已提交
241 242
#endif

J
Jeff Dike 已提交
243
	return os_ioctl_generic(state->fd, cmd, arg);
L
Linus Torvalds 已提交
244 245 246 247
}

static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
{
J
Jeff Dike 已提交
248 249 250
	struct hostmixer_state *state;
	int r = 0, w = 0;
	int ret;
L
Linus Torvalds 已提交
251 252

#ifdef DEBUG
J
Jeff Dike 已提交
253
	printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
L
Linus Torvalds 已提交
254 255
#endif

J
Jeff Dike 已提交
256
	state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
J
Jeff Dike 已提交
257 258
	if (state == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
259

J
Jeff Dike 已提交
260 261 262 263
	if (file->f_mode & FMODE_READ)
		r = 1;
	if (file->f_mode & FMODE_WRITE)
		w = 1;
L
Linus Torvalds 已提交
264

265
	kparam_block_sysfs_write(mixer);
266
	lock_kernel();
L
Linus Torvalds 已提交
267
	ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
268
	unlock_kernel();
269
	kparam_unblock_sysfs_write(mixer);
J
Jeff Dike 已提交
270 271

	if (ret < 0) {
272
		kparam_block_sysfs_write(dsp);
J
Jeff Dike 已提交
273 274
		printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
		       "err = %d\n", dsp, -ret);
275
		kparam_unblock_sysfs_write(dsp);
L
Linus Torvalds 已提交
276
		kfree(state);
J
Jeff Dike 已提交
277
		return ret;
J
Jeff Dike 已提交
278
	}
L
Linus Torvalds 已提交
279

J
Jeff Dike 已提交
280
	file->private_data = state;
J
Jeff Dike 已提交
281
	return 0;
L
Linus Torvalds 已提交
282 283 284 285
}

static int hostmixer_release(struct inode *inode, struct file *file)
{
J
Jeff Dike 已提交
286
	struct hostmixer_state *state = file->private_data;
L
Linus Torvalds 已提交
287 288

#ifdef DEBUG
J
Jeff Dike 已提交
289
	printk(KERN_DEBUG "hostmixer: release called\n");
L
Linus Torvalds 已提交
290 291
#endif

J
Jeff Dike 已提交
292 293
	os_close_file(state->fd);
	kfree(state);
L
Linus Torvalds 已提交
294

J
Jeff Dike 已提交
295
	return 0;
L
Linus Torvalds 已提交
296 297 298 299
}

/* kernel module operations */

J
Jeff Dike 已提交
300
static const struct file_operations hostaudio_fops = {
J
Jeff Dike 已提交
301 302 303 304 305
	.owner          = THIS_MODULE,
	.llseek         = no_llseek,
	.read           = hostaudio_read,
	.write          = hostaudio_write,
	.poll           = hostaudio_poll,
306
	.unlocked_ioctl	= hostaudio_ioctl,
J
Jeff Dike 已提交
307 308 309
	.mmap           = NULL,
	.open           = hostaudio_open,
	.release        = hostaudio_release,
L
Linus Torvalds 已提交
310 311
};

J
Jeff Dike 已提交
312
static const struct file_operations hostmixer_fops = {
J
Jeff Dike 已提交
313 314
	.owner          = THIS_MODULE,
	.llseek         = no_llseek,
315
	.unlocked_ioctl	= hostmixer_ioctl_mixdev,
J
Jeff Dike 已提交
316 317
	.open           = hostmixer_open_mixdev,
	.release        = hostmixer_release,
L
Linus Torvalds 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330
};

struct {
	int dev_audio;
	int dev_mixer;
} module_data;

MODULE_AUTHOR("Steve Schmidtke");
MODULE_DESCRIPTION("UML Audio Relay");
MODULE_LICENSE("GPL");

static int __init hostaudio_init_module(void)
{
331
	__kernel_param_lock();
J
Jeff Dike 已提交
332
	printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
L
Linus Torvalds 已提交
333
	       dsp, mixer);
334
	__kernel_param_unlock();
L
Linus Torvalds 已提交
335 336

	module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
J
Jeff Dike 已提交
337
	if (module_data.dev_audio < 0) {
J
Jeff Dike 已提交
338 339 340
		printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
		return -ENODEV;
	}
L
Linus Torvalds 已提交
341 342

	module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
J
Jeff Dike 已提交
343
	if (module_data.dev_mixer < 0) {
J
Jeff Dike 已提交
344
		printk(KERN_ERR "hostmixer: couldn't register mixer "
L
Linus Torvalds 已提交
345
		       "device!\n");
J
Jeff Dike 已提交
346 347 348
		unregister_sound_dsp(module_data.dev_audio);
		return -ENODEV;
	}
L
Linus Torvalds 已提交
349

J
Jeff Dike 已提交
350
	return 0;
L
Linus Torvalds 已提交
351 352 353 354
}

static void __exit hostaudio_cleanup_module (void)
{
J
Jeff Dike 已提交
355 356
	unregister_sound_mixer(module_data.dev_mixer);
	unregister_sound_dsp(module_data.dev_audio);
L
Linus Torvalds 已提交
357 358 359 360
}

module_init(hostaudio_init_module);
module_exit(hostaudio_cleanup_module);