hostaudio_kern.c 7.2 KB

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