hostaudio_kern.c 7.8 KB

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