hostaudio_kern.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <linux/mutex.h>
  11. #include <asm/uaccess.h>
  12. #include <init.h>
  13. #include <os.h>
  14. struct hostaudio_state {
  15. int fd;
  16. };
  17. struct hostmixer_state {
  18. int fd;
  19. };
  20. #define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
  21. #define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
  22. /*
  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. module_param(dsp, charp, 0644);
  36. MODULE_PARM_DESC(dsp, DSP_HELP);
  37. module_param(mixer, charp, 0644);
  38. MODULE_PARM_DESC(mixer, MIXER_HELP);
  39. #ifndef MODULE
  40. static int set_dsp(char *name, int *add)
  41. {
  42. dsp = name;
  43. return 0;
  44. }
  45. __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
  46. static int set_mixer(char *name, int *add)
  47. {
  48. mixer = name;
  49. return 0;
  50. }
  51. __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
  52. #endif
  53. static DEFINE_MUTEX(hostaudio_mutex);
  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(KERN_DEBUG "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(KERN_DEBUG "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(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
  105. #endif
  106. return mask;
  107. }
  108. static long hostaudio_ioctl(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(KERN_DEBUG "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. kernel_param_lock(THIS_MODULE);
  153. printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
  154. kernel_param_unlock(THIS_MODULE);
  155. #endif
  156. state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
  157. if (state == NULL)
  158. return -ENOMEM;
  159. if (file->f_mode & FMODE_READ)
  160. r = 1;
  161. if (file->f_mode & FMODE_WRITE)
  162. w = 1;
  163. kernel_param_lock(THIS_MODULE);
  164. mutex_lock(&hostaudio_mutex);
  165. ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
  166. mutex_unlock(&hostaudio_mutex);
  167. kernel_param_unlock(THIS_MODULE);
  168. if (ret < 0) {
  169. kfree(state);
  170. return ret;
  171. }
  172. state->fd = ret;
  173. file->private_data = state;
  174. return 0;
  175. }
  176. static int hostaudio_release(struct inode *inode, struct file *file)
  177. {
  178. struct hostaudio_state *state = file->private_data;
  179. #ifdef DEBUG
  180. printk(KERN_DEBUG "hostaudio: release called\n");
  181. #endif
  182. os_close_file(state->fd);
  183. kfree(state);
  184. return 0;
  185. }
  186. /* /dev/mixer file operations */
  187. static long hostmixer_ioctl_mixdev(struct file *file,
  188. unsigned int cmd, unsigned long arg)
  189. {
  190. struct hostmixer_state *state = file->private_data;
  191. #ifdef DEBUG
  192. printk(KERN_DEBUG "hostmixer: ioctl called\n");
  193. #endif
  194. return os_ioctl_generic(state->fd, cmd, arg);
  195. }
  196. static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
  197. {
  198. struct hostmixer_state *state;
  199. int r = 0, w = 0;
  200. int ret;
  201. #ifdef DEBUG
  202. printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
  203. #endif
  204. state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
  205. if (state == NULL)
  206. return -ENOMEM;
  207. if (file->f_mode & FMODE_READ)
  208. r = 1;
  209. if (file->f_mode & FMODE_WRITE)
  210. w = 1;
  211. kernel_param_lock(THIS_MODULE);
  212. mutex_lock(&hostaudio_mutex);
  213. ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
  214. mutex_unlock(&hostaudio_mutex);
  215. kernel_param_unlock(THIS_MODULE);
  216. if (ret < 0) {
  217. kernel_param_lock(THIS_MODULE);
  218. printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
  219. "err = %d\n", dsp, -ret);
  220. kernel_param_unlock(THIS_MODULE);
  221. kfree(state);
  222. return ret;
  223. }
  224. file->private_data = state;
  225. return 0;
  226. }
  227. static int hostmixer_release(struct inode *inode, struct file *file)
  228. {
  229. struct hostmixer_state *state = file->private_data;
  230. #ifdef DEBUG
  231. printk(KERN_DEBUG "hostmixer: release called\n");
  232. #endif
  233. os_close_file(state->fd);
  234. kfree(state);
  235. return 0;
  236. }
  237. /* kernel module operations */
  238. static const struct file_operations hostaudio_fops = {
  239. .owner = THIS_MODULE,
  240. .llseek = no_llseek,
  241. .read = hostaudio_read,
  242. .write = hostaudio_write,
  243. .poll = hostaudio_poll,
  244. .unlocked_ioctl = hostaudio_ioctl,
  245. .mmap = NULL,
  246. .open = hostaudio_open,
  247. .release = hostaudio_release,
  248. };
  249. static const struct file_operations hostmixer_fops = {
  250. .owner = THIS_MODULE,
  251. .llseek = no_llseek,
  252. .unlocked_ioctl = hostmixer_ioctl_mixdev,
  253. .open = hostmixer_open_mixdev,
  254. .release = hostmixer_release,
  255. };
  256. struct {
  257. int dev_audio;
  258. int dev_mixer;
  259. } module_data;
  260. MODULE_AUTHOR("Steve Schmidtke");
  261. MODULE_DESCRIPTION("UML Audio Relay");
  262. MODULE_LICENSE("GPL");
  263. static int __init hostaudio_init_module(void)
  264. {
  265. kernel_param_lock(THIS_MODULE);
  266. printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
  267. dsp, mixer);
  268. kernel_param_unlock(THIS_MODULE);
  269. module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
  270. if (module_data.dev_audio < 0) {
  271. printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
  272. return -ENODEV;
  273. }
  274. module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
  275. if (module_data.dev_mixer < 0) {
  276. printk(KERN_ERR "hostmixer: couldn't register mixer "
  277. "device!\n");
  278. unregister_sound_dsp(module_data.dev_audio);
  279. return -ENODEV;
  280. }
  281. return 0;
  282. }
  283. static void __exit hostaudio_cleanup_module (void)
  284. {
  285. unregister_sound_mixer(module_data.dev_mixer);
  286. unregister_sound_dsp(module_data.dev_audio);
  287. }
  288. module_init(hostaudio_init_module);
  289. module_exit(hostaudio_cleanup_module);