hostaudio_kern.c 7.2 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 11 12 13 14 15
#include "linux/module.h"
#include "linux/slab.h"
#include "linux/sound.h"
#include "linux/soundcard.h"
#include "asm/uaccess.h"
#include "init.h"
#include "os.h"

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

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

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

J
Jeff Dike 已提交
26 27
/*
 * Changed either at boot time or module load time.  At boot, this is
J
Jeff Dike 已提交
28 29 30 31 32
 * 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 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45

#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 已提交
46
	return 0;
L
Linus Torvalds 已提交
47 48 49 50 51 52 53
}

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

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

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

#else /*MODULE*/

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

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

#endif

/* /dev/dsp file operations */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
J
Jeff Dike 已提交
202
	if (ret < 0) {
L
Linus Torvalds 已提交
203
		kfree(state);
J
Jeff Dike 已提交
204
		return ret;
J
Jeff Dike 已提交
205
	}
L
Linus Torvalds 已提交
206
	state->fd = ret;
J
Jeff Dike 已提交
207
	file->private_data = state;
J
Jeff Dike 已提交
208
	return 0;
L
Linus Torvalds 已提交
209 210 211 212
}

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

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

J
Jeff Dike 已提交
221
	return 0;
L
Linus Torvalds 已提交
222 223 224 225
}

/* /dev/mixer file operations */

J
Jeff Dike 已提交
226
static int hostmixer_ioctl_mixdev(struct inode *inode, struct file *file,
L
Linus Torvalds 已提交
227 228
				  unsigned int cmd, unsigned long arg)
{
J
Jeff Dike 已提交
229
	struct hostmixer_state *state = file->private_data;
L
Linus Torvalds 已提交
230 231

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

J
Jeff Dike 已提交
235
	return os_ioctl_generic(state->fd, cmd, arg);
L
Linus Torvalds 已提交
236 237 238 239
}

static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
{
J
Jeff Dike 已提交
240 241 242
	struct hostmixer_state *state;
	int r = 0, w = 0;
	int ret;
L
Linus Torvalds 已提交
243 244

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

J
Jeff Dike 已提交
248
	state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
J
Jeff Dike 已提交
249 250
	if (state == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
251

J
Jeff Dike 已提交
252 253 254 255
	if (file->f_mode & FMODE_READ)
		r = 1;
	if (file->f_mode & FMODE_WRITE)
		w = 1;
L
Linus Torvalds 已提交
256 257

	ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
J
Jeff Dike 已提交
258 259 260 261

	if (ret < 0) {
		printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
		       "err = %d\n", dsp, -ret);
L
Linus Torvalds 已提交
262
		kfree(state);
J
Jeff Dike 已提交
263
		return ret;
J
Jeff Dike 已提交
264
	}
L
Linus Torvalds 已提交
265

J
Jeff Dike 已提交
266
	file->private_data = state;
J
Jeff Dike 已提交
267
	return 0;
L
Linus Torvalds 已提交
268 269 270 271
}

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

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

J
Jeff Dike 已提交
278 279
	os_close_file(state->fd);
	kfree(state);
L
Linus Torvalds 已提交
280

J
Jeff Dike 已提交
281
	return 0;
L
Linus Torvalds 已提交
282 283 284 285
}

/* kernel module operations */

J
Jeff Dike 已提交
286
static const struct file_operations hostaudio_fops = {
J
Jeff Dike 已提交
287 288 289 290 291 292 293 294 295
	.owner          = THIS_MODULE,
	.llseek         = no_llseek,
	.read           = hostaudio_read,
	.write          = hostaudio_write,
	.poll           = hostaudio_poll,
	.ioctl          = hostaudio_ioctl,
	.mmap           = NULL,
	.open           = hostaudio_open,
	.release        = hostaudio_release,
L
Linus Torvalds 已提交
296 297
};

J
Jeff Dike 已提交
298
static const struct file_operations hostmixer_fops = {
J
Jeff Dike 已提交
299 300 301 302 303
	.owner          = THIS_MODULE,
	.llseek         = no_llseek,
	.ioctl          = hostmixer_ioctl_mixdev,
	.open           = hostmixer_open_mixdev,
	.release        = hostmixer_release,
L
Linus Torvalds 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316
};

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)
{
J
Jeff Dike 已提交
317
	printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
L
Linus Torvalds 已提交
318 319 320
	       dsp, mixer);

	module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
J
Jeff Dike 已提交
321
	if (module_data.dev_audio < 0) {
J
Jeff Dike 已提交
322 323 324
		printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
		return -ENODEV;
	}
L
Linus Torvalds 已提交
325 326

	module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
J
Jeff Dike 已提交
327
	if (module_data.dev_mixer < 0) {
J
Jeff Dike 已提交
328
		printk(KERN_ERR "hostmixer: couldn't register mixer "
L
Linus Torvalds 已提交
329
		       "device!\n");
J
Jeff Dike 已提交
330 331 332
		unregister_sound_dsp(module_data.dev_audio);
		return -ENODEV;
	}
L
Linus Torvalds 已提交
333

J
Jeff Dike 已提交
334
	return 0;
L
Linus Torvalds 已提交
335 336 337 338
}

static void __exit hostaudio_cleanup_module (void)
{
J
Jeff Dike 已提交
339 340
	unregister_sound_mixer(module_data.dev_mixer);
	unregister_sound_dsp(module_data.dev_audio);
L
Linus Torvalds 已提交
341 342 343 344
}

module_init(hostaudio_init_module);
module_exit(hostaudio_cleanup_module);