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

#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"

F
FUJITA Tomonori 已提交
43 44 45 46 47
module_param(dsp, charp, 0644);
MODULE_PARM_DESC(dsp, DSP_HELP);
module_param(mixer, charp, 0644);
MODULE_PARM_DESC(mixer, MIXER_HELP);

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

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

static int set_mixer(char *name, int *add)
{
	mixer = name;
J
Jeff Dike 已提交
60
	return 0;
L
Linus Torvalds 已提交
61 62 63 64 65 66 67
}

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

/* /dev/dsp file operations */

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

#ifdef DEBUG
J
Jeff Dike 已提交
76
	printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
L
Linus Torvalds 已提交
77 78 79
#endif

	kbuf = kmalloc(count, GFP_KERNEL);
J
Jeff Dike 已提交
80 81
	if (kbuf == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
82

83
	err = os_read_file(state->fd, kbuf, count);
J
Jeff Dike 已提交
84
	if (err < 0)
L
Linus Torvalds 已提交
85 86
		goto out;

J
Jeff Dike 已提交
87
	if (copy_to_user(buffer, kbuf, err))
L
Linus Torvalds 已提交
88 89
		err = -EFAULT;

J
Jeff Dike 已提交
90
out:
L
Linus Torvalds 已提交
91
	kfree(kbuf);
J
Jeff Dike 已提交
92
	return err;
L
Linus Torvalds 已提交
93 94
}

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

#ifdef DEBUG
J
Jeff Dike 已提交
103
	printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
L
Linus Torvalds 已提交
104 105 106
#endif

	kbuf = kmalloc(count, GFP_KERNEL);
J
Jeff Dike 已提交
107 108
	if (kbuf == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
109 110

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

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

 out:
	kfree(kbuf);
J
Jeff Dike 已提交
121
	return err;
L
Linus Torvalds 已提交
122 123
}

J
Jeff Dike 已提交
124
static unsigned int hostaudio_poll(struct file *file,
L
Linus Torvalds 已提交
125 126
				   struct poll_table_struct *wait)
{
J
Jeff Dike 已提交
127
	unsigned int mask = 0;
L
Linus Torvalds 已提交
128 129

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

J
Jeff Dike 已提交
133
	return mask;
L
Linus Torvalds 已提交
134 135
}

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

#ifdef DEBUG
J
Jeff Dike 已提交
144
	printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152
#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 已提交
153
		if (get_user(data, (int __user *) arg))
J
Johann Felix Soden 已提交
154
			return -EFAULT;
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168
		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 已提交
169 170
		if (put_user(data, (int __user *) arg))
			return -EFAULT;
L
Linus Torvalds 已提交
171 172 173 174 175
		break;
	default:
		break;
	}

J
Jeff Dike 已提交
176
	return err;
L
Linus Torvalds 已提交
177 178 179 180
}

static int hostaudio_open(struct inode *inode, struct file *file)
{
J
Jeff Dike 已提交
181 182 183
	struct hostaudio_state *state;
	int r = 0, w = 0;
	int ret;
L
Linus Torvalds 已提交
184 185

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

J
Jeff Dike 已提交
191
	state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
J
Jeff Dike 已提交
192 193
	if (state == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
194

J
Jeff Dike 已提交
195 196 197 198
	if (file->f_mode & FMODE_READ)
		r = 1;
	if (file->f_mode & FMODE_WRITE)
		w = 1;
L
Linus Torvalds 已提交
199

200
	kparam_block_sysfs_write(dsp);
201
	lock_kernel();
L
Linus Torvalds 已提交
202
	ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
203
	unlock_kernel();
204
	kparam_unblock_sysfs_write(dsp);
205

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

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

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

J
Jeff Dike 已提交
225
	return 0;
L
Linus Torvalds 已提交
226 227 228 229
}

/* /dev/mixer file operations */

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

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

J
Jeff Dike 已提交
239
	return os_ioctl_generic(state->fd, cmd, arg);
L
Linus Torvalds 已提交
240 241 242 243
}

static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
{
J
Jeff Dike 已提交
244 245 246
	struct hostmixer_state *state;
	int r = 0, w = 0;
	int ret;
L
Linus Torvalds 已提交
247 248

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

J
Jeff Dike 已提交
252
	state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
J
Jeff Dike 已提交
253 254
	if (state == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
255

J
Jeff Dike 已提交
256 257 258 259
	if (file->f_mode & FMODE_READ)
		r = 1;
	if (file->f_mode & FMODE_WRITE)
		w = 1;
L
Linus Torvalds 已提交
260

261
	kparam_block_sysfs_write(mixer);
262
	lock_kernel();
L
Linus Torvalds 已提交
263
	ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
264
	unlock_kernel();
265
	kparam_unblock_sysfs_write(mixer);
J
Jeff Dike 已提交
266 267

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

J
Jeff Dike 已提交
276
	file->private_data = state;
J
Jeff Dike 已提交
277
	return 0;
L
Linus Torvalds 已提交
278 279 280 281
}

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

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

J
Jeff Dike 已提交
288 289
	os_close_file(state->fd);
	kfree(state);
L
Linus Torvalds 已提交
290

J
Jeff Dike 已提交
291
	return 0;
L
Linus Torvalds 已提交
292 293 294 295
}

/* kernel module operations */

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

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

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)
{
327
	__kernel_param_lock();
J
Jeff Dike 已提交
328
	printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
L
Linus Torvalds 已提交
329
	       dsp, mixer);
330
	__kernel_param_unlock();
L
Linus Torvalds 已提交
331 332

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

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

J
Jeff Dike 已提交
346
	return 0;
L
Linus Torvalds 已提交
347 348 349 350
}

static void __exit hostaudio_cleanup_module (void)
{
J
Jeff Dike 已提交
351 352
	unregister_sound_mixer(module_data.dev_mixer);
	unregister_sound_dsp(module_data.dev_audio);
L
Linus Torvalds 已提交
353 354 355 356
}

module_init(hostaudio_init_module);
module_exit(hostaudio_cleanup_module);