hostaudio_kern.c 7.8 KB

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