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