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 { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 18 | int fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | struct hostmixer_state { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 22 | int fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | #define HOSTAUDIO_DEV_DSP "/dev/sound/dsp" |
| 26 | #define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer" |
| 27 | |
Jeff Dike | b612e47 | 2007-02-10 01:43:59 -0800 | [diff] [blame] | 28 | /* Changed either at boot time or module load time. At boot, this is |
| 29 | * single-threaded; at module load, multiple modules would each have |
| 30 | * their own copy of these variables. |
| 31 | */ |
| 32 | static char *dsp = HOSTAUDIO_DEV_DSP; |
| 33 | static char *mixer = HOSTAUDIO_DEV_MIXER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 43 | #ifndef MODULE |
| 44 | static int set_dsp(char *name, int *add) |
| 45 | { |
| 46 | dsp = name; |
| 47 | return(0); |
| 48 | } |
| 49 | |
| 50 | __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP); |
| 51 | |
| 52 | static int set_mixer(char *name, int *add) |
| 53 | { |
| 54 | mixer = name; |
| 55 | return(0); |
| 56 | } |
| 57 | |
| 58 | __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP); |
| 59 | |
| 60 | #else /*MODULE*/ |
| 61 | |
Dominik Hackl | 20a64f1 | 2005-07-27 11:43:32 -0700 | [diff] [blame] | 62 | module_param(dsp, charp, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | MODULE_PARM_DESC(dsp, DSP_HELP); |
| 64 | |
Dominik Hackl | 20a64f1 | 2005-07-27 11:43:32 -0700 | [diff] [blame] | 65 | module_param(mixer, charp, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | MODULE_PARM_DESC(mixer, MIXER_HELP); |
| 67 | |
| 68 | #endif |
| 69 | |
| 70 | /* /dev/dsp file operations */ |
| 71 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 72 | static ssize_t hostaudio_read(struct file *file, char __user *buffer, |
| 73 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 75 | struct hostaudio_state *state = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | void *kbuf; |
| 77 | int err; |
| 78 | |
| 79 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 80 | printk("hostaudio: read called, count = %d\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | kbuf = kmalloc(count, GFP_KERNEL); |
| 84 | if(kbuf == NULL) |
| 85 | return(-ENOMEM); |
| 86 | |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 87 | err = os_read_file(state->fd, kbuf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if(err < 0) |
| 89 | goto out; |
| 90 | |
| 91 | if(copy_to_user(buffer, kbuf, err)) |
| 92 | err = -EFAULT; |
| 93 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 94 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | kfree(kbuf); |
| 96 | return(err); |
| 97 | } |
| 98 | |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 99 | static ssize_t hostaudio_write(struct file *file, const char __user *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | size_t count, loff_t *ppos) |
| 101 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 102 | struct hostaudio_state *state = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | void *kbuf; |
| 104 | int err; |
| 105 | |
| 106 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 107 | printk("hostaudio: write called, count = %d\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | kbuf = kmalloc(count, GFP_KERNEL); |
| 111 | if(kbuf == NULL) |
| 112 | return(-ENOMEM); |
| 113 | |
| 114 | err = -EFAULT; |
| 115 | if(copy_from_user(kbuf, buffer, count)) |
| 116 | goto out; |
| 117 | |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 118 | err = os_write_file(state->fd, kbuf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if(err < 0) |
| 120 | goto out; |
| 121 | *ppos += err; |
| 122 | |
| 123 | out: |
| 124 | kfree(kbuf); |
| 125 | return(err); |
| 126 | } |
| 127 | |
| 128 | static unsigned int hostaudio_poll(struct file *file, |
| 129 | struct poll_table_struct *wait) |
| 130 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 131 | unsigned int mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 134 | printk("hostaudio: poll called (unimplemented)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | #endif |
| 136 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 137 | return(mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int hostaudio_ioctl(struct inode *inode, struct file *file, |
| 141 | unsigned int cmd, unsigned long arg) |
| 142 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 143 | struct hostaudio_state *state = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | unsigned long data = 0; |
| 145 | int err; |
| 146 | |
| 147 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 148 | printk("hostaudio: ioctl called, cmd = %u\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | #endif |
| 150 | switch(cmd){ |
| 151 | case SNDCTL_DSP_SPEED: |
| 152 | case SNDCTL_DSP_STEREO: |
| 153 | case SNDCTL_DSP_GETBLKSIZE: |
| 154 | case SNDCTL_DSP_CHANNELS: |
| 155 | case SNDCTL_DSP_SUBDIVIDE: |
| 156 | case SNDCTL_DSP_SETFRAGMENT: |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 157 | if(get_user(data, (int __user *) arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return(-EFAULT); |
| 159 | break; |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data); |
| 165 | |
| 166 | switch(cmd){ |
| 167 | case SNDCTL_DSP_SPEED: |
| 168 | case SNDCTL_DSP_STEREO: |
| 169 | case SNDCTL_DSP_GETBLKSIZE: |
| 170 | case SNDCTL_DSP_CHANNELS: |
| 171 | case SNDCTL_DSP_SUBDIVIDE: |
| 172 | case SNDCTL_DSP_SETFRAGMENT: |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 173 | if(put_user(data, (int __user *) arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return(-EFAULT); |
| 175 | break; |
| 176 | default: |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | return(err); |
| 181 | } |
| 182 | |
| 183 | static int hostaudio_open(struct inode *inode, struct file *file) |
| 184 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 185 | struct hostaudio_state *state; |
| 186 | int r = 0, w = 0; |
| 187 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 190 | printk("hostaudio: open called (host: %s)\n", dsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | #endif |
| 192 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 193 | state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL); |
| 194 | if(state == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return(-ENOMEM); |
| 196 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 197 | if(file->f_mode & FMODE_READ) r = 1; |
| 198 | if(file->f_mode & FMODE_WRITE) w = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0); |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 201 | if(ret < 0){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | kfree(state); |
| 203 | return(ret); |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | state->fd = ret; |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 206 | file->private_data = state; |
| 207 | return(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static int hostaudio_release(struct inode *inode, struct file *file) |
| 211 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 212 | struct hostaudio_state *state = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 215 | printk("hostaudio: release called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | #endif |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 217 | os_close_file(state->fd); |
| 218 | kfree(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | return(0); |
| 221 | } |
| 222 | |
| 223 | /* /dev/mixer file operations */ |
| 224 | |
| 225 | static int hostmixer_ioctl_mixdev(struct inode *inode, struct file *file, |
| 226 | unsigned int cmd, unsigned long arg) |
| 227 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 228 | struct hostmixer_state *state = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 231 | printk("hostmixer: ioctl called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | #endif |
| 233 | |
| 234 | return(os_ioctl_generic(state->fd, cmd, arg)); |
| 235 | } |
| 236 | |
| 237 | static int hostmixer_open_mixdev(struct inode *inode, struct file *file) |
| 238 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 239 | struct hostmixer_state *state; |
| 240 | int r = 0, w = 0; |
| 241 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 244 | printk("hostmixer: open called (host: %s)\n", mixer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | #endif |
| 246 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 247 | state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL); |
| 248 | if(state == NULL) return(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 250 | if(file->f_mode & FMODE_READ) r = 1; |
| 251 | if(file->f_mode & FMODE_WRITE) w = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0); |
| 254 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 255 | if(ret < 0){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | printk("hostaudio_open_mixdev failed to open '%s', err = %d\n", |
| 257 | dsp, -ret); |
| 258 | kfree(state); |
| 259 | return(ret); |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 262 | file->private_data = state; |
| 263 | return(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static int hostmixer_release(struct inode *inode, struct file *file) |
| 267 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 268 | struct hostmixer_state *state = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | #ifdef DEBUG |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 271 | printk("hostmixer: release called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | #endif |
| 273 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 274 | os_close_file(state->fd); |
| 275 | kfree(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | return(0); |
| 278 | } |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 = { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | }; |
| 293 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 294 | static const struct file_operations hostmixer_fops = { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 295 | .owner = THIS_MODULE, |
| 296 | .llseek = no_llseek, |
| 297 | .ioctl = hostmixer_ioctl_mixdev, |
| 298 | .open = hostmixer_open_mixdev, |
| 299 | .release = hostmixer_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 313 | printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | dsp, mixer); |
| 315 | |
| 316 | module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1); |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 317 | if(module_data.dev_audio < 0){ |
| 318 | printk(KERN_ERR "hostaudio: couldn't register DSP device!\n"); |
| 319 | return -ENODEV; |
| 320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1); |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 323 | if(module_data.dev_mixer < 0){ |
| 324 | printk(KERN_ERR "hostmixer: couldn't register mixer " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | "device!\n"); |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 326 | unregister_sound_dsp(module_data.dev_audio); |
| 327 | return -ENODEV; |
| 328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 330 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static void __exit hostaudio_cleanup_module (void) |
| 334 | { |
Jeff Dike | d471c0f | 2007-02-10 01:44:00 -0800 | [diff] [blame] | 335 | unregister_sound_mixer(module_data.dev_mixer); |
| 336 | unregister_sound_dsp(module_data.dev_audio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | module_init(hostaudio_init_module); |
| 340 | module_exit(hostaudio_cleanup_module); |