hostaudio_kern.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2002 Steve Schmidtke
  3. * Licensed under the GPL
  4. */
  5. #include "linux/module.h"
  6. #include "linux/init.h"
  7. #include "linux/slab.h"
  8. #include "linux/fs.h"
  9. #include "linux/sound.h"
  10. #include "linux/soundcard.h"
  11. #include "asm/uaccess.h"
  12. #include "kern_util.h"
  13. #include "init.h"
  14. #include "os.h"
  15. struct hostaudio_state {
  16. int fd;
  17. };
  18. struct hostmixer_state {
  19. int fd;
  20. };
  21. #define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
  22. #define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
  23. /* Changed either at boot time or module load time. At boot, this is
  24. * single-threaded; at module load, multiple modules would each have
  25. * their own copy of these variables.
  26. */
  27. static char *dsp = HOSTAUDIO_DEV_DSP;
  28. static char *mixer = HOSTAUDIO_DEV_MIXER;
  29. #define DSP_HELP \
  30. " This is used to specify the host dsp device to the hostaudio driver.\n" \
  31. " The default is \"" HOSTAUDIO_DEV_DSP "\".\n\n"
  32. #define MIXER_HELP \
  33. " This is used to specify the host mixer device to the hostaudio driver.\n"\
  34. " The default is \"" HOSTAUDIO_DEV_MIXER "\".\n\n"
  35. #ifndef MODULE
  36. static int set_dsp(char *name, int *add)
  37. {
  38. dsp = name;
  39. return(0);
  40. }
  41. __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
  42. static int set_mixer(char *name, int *add)
  43. {
  44. mixer = name;
  45. return(0);
  46. }
  47. __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
  48. #else /*MODULE*/
  49. module_param(dsp, charp, 0644);
  50. MODULE_PARM_DESC(dsp, DSP_HELP);
  51. module_param(mixer, charp, 0644);
  52. MODULE_PARM_DESC(mixer, MIXER_HELP);
  53. #endif
  54. /* /dev/dsp file operations */
  55. static ssize_t hostaudio_read(struct file *file, char __user *buffer,
  56. size_t count, loff_t *ppos)
  57. {
  58. struct hostaudio_state *state = file->private_data;
  59. void *kbuf;
  60. int err;
  61. #ifdef DEBUG
  62. printk("hostaudio: read called, count = %d\n", count);
  63. #endif
  64. kbuf = kmalloc(count, GFP_KERNEL);
  65. if(kbuf == NULL)
  66. return(-ENOMEM);
  67. err = os_read_file(state->fd, kbuf, count);
  68. if(err < 0)
  69. goto out;
  70. if(copy_to_user(buffer, kbuf, err))
  71. err = -EFAULT;
  72. out:
  73. kfree(kbuf);
  74. return(err);
  75. }
  76. static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
  77. size_t count, loff_t *ppos)
  78. {
  79. struct hostaudio_state *state = file->private_data;
  80. void *kbuf;
  81. int err;
  82. #ifdef DEBUG
  83. printk("hostaudio: write called, count = %d\n", count);
  84. #endif
  85. kbuf = kmalloc(count, GFP_KERNEL);
  86. if(kbuf == NULL)
  87. return(-ENOMEM);
  88. err = -EFAULT;
  89. if(copy_from_user(kbuf, buffer, count))
  90. goto out;
  91. err = os_write_file(state->fd, kbuf, count);
  92. if(err < 0)
  93. goto out;
  94. *ppos += err;
  95. out:
  96. kfree(kbuf);
  97. return(err);
  98. }
  99. static unsigned int hostaudio_poll(struct file *file,
  100. struct poll_table_struct *wait)
  101. {
  102. unsigned int mask = 0;
  103. #ifdef DEBUG
  104. printk("hostaudio: poll called (unimplemented)\n");
  105. #endif
  106. return(mask);
  107. }
  108. static int hostaudio_ioctl(struct inode *inode, struct file *file,
  109. unsigned int cmd, unsigned long arg)
  110. {
  111. struct hostaudio_state *state = file->private_data;
  112. unsigned long data = 0;
  113. int err;
  114. #ifdef DEBUG
  115. printk("hostaudio: ioctl called, cmd = %u\n", cmd);
  116. #endif
  117. switch(cmd){
  118. case SNDCTL_DSP_SPEED:
  119. case SNDCTL_DSP_STEREO:
  120. case SNDCTL_DSP_GETBLKSIZE:
  121. case SNDCTL_DSP_CHANNELS:
  122. case SNDCTL_DSP_SUBDIVIDE:
  123. case SNDCTL_DSP_SETFRAGMENT:
  124. if(get_user(data, (int __user *) arg))
  125. return(-EFAULT);
  126. break;
  127. default:
  128. break;
  129. }
  130. err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
  131. switch(cmd){
  132. case SNDCTL_DSP_SPEED:
  133. case SNDCTL_DSP_STEREO:
  134. case SNDCTL_DSP_GETBLKSIZE:
  135. case SNDCTL_DSP_CHANNELS:
  136. case SNDCTL_DSP_SUBDIVIDE:
  137. case SNDCTL_DSP_SETFRAGMENT:
  138. if(put_user(data, (int __user *) arg))
  139. return(-EFAULT);
  140. break;
  141. default:
  142. break;
  143. }
  144. return(err);
  145. }
  146. static int hostaudio_open(struct inode *inode, struct file *file)
  147. {
  148. struct hostaudio_state *state;
  149. int r = 0, w = 0;
  150. int ret;
  151. #ifdef DEBUG
  152. printk("hostaudio: open called (host: %s)\n", dsp);
  153. #endif
  154. state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
  155. if(state == NULL)
  156. return(-ENOMEM);
  157. if(file->f_mode & FMODE_READ) r = 1;
  158. if(file->f_mode & FMODE_WRITE) w = 1;
  159. ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
  160. if(ret < 0){
  161. kfree(state);
  162. return(ret);
  163. }
  164. state->fd = ret;
  165. file->private_data = state;
  166. return(0);
  167. }
  168. static int hostaudio_release(struct inode *inode, struct file *file)
  169. {
  170. struct hostaudio_state *state = file->private_data;
  171. #ifdef DEBUG
  172. printk("hostaudio: release called\n");
  173. #endif
  174. os_close_file(state->fd);
  175. kfree(state);
  176. return(0);
  177. }
  178. /* /dev/mixer file operations */
  179. static int hostmixer_ioctl_mixdev(struct inode *inode, struct file *file,
  180. unsigned int cmd, unsigned long arg)
  181. {
  182. struct hostmixer_state *state = file->private_data;
  183. #ifdef DEBUG
  184. printk("hostmixer: ioctl called\n");
  185. #endif
  186. return(os_ioctl_generic(state->fd, cmd, arg));
  187. }
  188. static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
  189. {
  190. struct hostmixer_state *state;
  191. int r = 0, w = 0;
  192. int ret;
  193. #ifdef DEBUG
  194. printk("hostmixer: open called (host: %s)\n", mixer);
  195. #endif
  196. state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
  197. if(state == NULL) return(-ENOMEM);
  198. if(file->f_mode & FMODE_READ) r = 1;
  199. if(file->f_mode & FMODE_WRITE) w = 1;
  200. ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
  201. if(ret < 0){
  202. printk("hostaudio_open_mixdev failed to open '%s', err = %d\n",
  203. dsp, -ret);
  204. kfree(state);
  205. return(ret);
  206. }
  207. file->private_data = state;
  208. return(0);
  209. }
  210. static int hostmixer_release(struct inode *inode, struct file *file)
  211. {
  212. struct hostmixer_state *state = file->private_data;
  213. #ifdef DEBUG
  214. printk("hostmixer: release called\n");
  215. #endif
  216. os_close_file(state->fd);
  217. kfree(state);
  218. return(0);
  219. }
  220. /* kernel module operations */
  221. static const struct file_operations hostaudio_fops = {
  222. .owner = THIS_MODULE,
  223. .llseek = no_llseek,
  224. .read = hostaudio_read,
  225. .write = hostaudio_write,
  226. .poll = hostaudio_poll,
  227. .ioctl = hostaudio_ioctl,
  228. .mmap = NULL,
  229. .open = hostaudio_open,
  230. .release = hostaudio_release,
  231. };
  232. static const struct file_operations hostmixer_fops = {
  233. .owner = THIS_MODULE,
  234. .llseek = no_llseek,
  235. .ioctl = hostmixer_ioctl_mixdev,
  236. .open = hostmixer_open_mixdev,
  237. .release = hostmixer_release,
  238. };
  239. struct {
  240. int dev_audio;
  241. int dev_mixer;
  242. } module_data;
  243. MODULE_AUTHOR("Steve Schmidtke");
  244. MODULE_DESCRIPTION("UML Audio Relay");
  245. MODULE_LICENSE("GPL");
  246. static int __init hostaudio_init_module(void)
  247. {
  248. printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
  249. dsp, mixer);
  250. module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
  251. if(module_data.dev_audio < 0){
  252. printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
  253. return -ENODEV;
  254. }
  255. module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
  256. if(module_data.dev_mixer < 0){
  257. printk(KERN_ERR "hostmixer: couldn't register mixer "
  258. "device!\n");
  259. unregister_sound_dsp(module_data.dev_audio);
  260. return -ENODEV;
  261. }
  262. return 0;
  263. }
  264. static void __exit hostaudio_cleanup_module (void)
  265. {
  266. unregister_sound_mixer(module_data.dev_mixer);
  267. unregister_sound_dsp(module_data.dev_audio);
  268. }
  269. module_init(hostaudio_init_module);
  270. module_exit(hostaudio_cleanup_module);