hostaudio_kern.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2002 Steve Schmidtke
  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 <linux/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 = memdup_user(buffer, count);
  86. if (IS_ERR(kbuf))
  87. return PTR_ERR(kbuf);
  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 __poll_t hostaudio_poll(struct file *file,
  97. struct poll_table_struct *wait)
  98. {
  99. #ifdef DEBUG
  100. printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
  101. #endif
  102. return 0;
  103. }
  104. static long hostaudio_ioctl(struct file *file,
  105. unsigned int cmd, unsigned long arg)
  106. {
  107. struct hostaudio_state *state = file->private_data;
  108. unsigned long data = 0;
  109. int err;
  110. #ifdef DEBUG
  111. printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
  112. #endif
  113. switch(cmd){
  114. case SNDCTL_DSP_SPEED:
  115. case SNDCTL_DSP_STEREO:
  116. case SNDCTL_DSP_GETBLKSIZE:
  117. case SNDCTL_DSP_CHANNELS:
  118. case SNDCTL_DSP_SUBDIVIDE:
  119. case SNDCTL_DSP_SETFRAGMENT:
  120. if (get_user(data, (int __user *) arg))
  121. return -EFAULT;
  122. break;
  123. default:
  124. break;
  125. }
  126. err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
  127. switch(cmd){
  128. case SNDCTL_DSP_SPEED:
  129. case SNDCTL_DSP_STEREO:
  130. case SNDCTL_DSP_GETBLKSIZE:
  131. case SNDCTL_DSP_CHANNELS:
  132. case SNDCTL_DSP_SUBDIVIDE:
  133. case SNDCTL_DSP_SETFRAGMENT:
  134. if (put_user(data, (int __user *) arg))
  135. return -EFAULT;
  136. break;
  137. default:
  138. break;
  139. }
  140. return err;
  141. }
  142. static int hostaudio_open(struct inode *inode, struct file *file)
  143. {
  144. struct hostaudio_state *state;
  145. int r = 0, w = 0;
  146. int ret;
  147. #ifdef DEBUG
  148. kernel_param_lock(THIS_MODULE);
  149. printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
  150. kernel_param_unlock(THIS_MODULE);
  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)
  156. r = 1;
  157. if (file->f_mode & FMODE_WRITE)
  158. w = 1;
  159. kernel_param_lock(THIS_MODULE);
  160. mutex_lock(&hostaudio_mutex);
  161. ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
  162. mutex_unlock(&hostaudio_mutex);
  163. kernel_param_unlock(THIS_MODULE);
  164. if (ret < 0) {
  165. kfree(state);
  166. return ret;
  167. }
  168. state->fd = ret;
  169. file->private_data = state;
  170. return 0;
  171. }
  172. static int hostaudio_release(struct inode *inode, struct file *file)
  173. {
  174. struct hostaudio_state *state = file->private_data;
  175. #ifdef DEBUG
  176. printk(KERN_DEBUG "hostaudio: release called\n");
  177. #endif
  178. os_close_file(state->fd);
  179. kfree(state);
  180. return 0;
  181. }
  182. /* /dev/mixer file operations */
  183. static long hostmixer_ioctl_mixdev(struct file *file,
  184. unsigned int cmd, unsigned long arg)
  185. {
  186. struct hostmixer_state *state = file->private_data;
  187. #ifdef DEBUG
  188. printk(KERN_DEBUG "hostmixer: ioctl called\n");
  189. #endif
  190. return os_ioctl_generic(state->fd, cmd, arg);
  191. }
  192. static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
  193. {
  194. struct hostmixer_state *state;
  195. int r = 0, w = 0;
  196. int ret;
  197. #ifdef DEBUG
  198. printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
  199. #endif
  200. state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
  201. if (state == NULL)
  202. return -ENOMEM;
  203. if (file->f_mode & FMODE_READ)
  204. r = 1;
  205. if (file->f_mode & FMODE_WRITE)
  206. w = 1;
  207. kernel_param_lock(THIS_MODULE);
  208. mutex_lock(&hostaudio_mutex);
  209. ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
  210. mutex_unlock(&hostaudio_mutex);
  211. kernel_param_unlock(THIS_MODULE);
  212. if (ret < 0) {
  213. kernel_param_lock(THIS_MODULE);
  214. printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
  215. "err = %d\n", dsp, -ret);
  216. kernel_param_unlock(THIS_MODULE);
  217. kfree(state);
  218. return ret;
  219. }
  220. file->private_data = state;
  221. return 0;
  222. }
  223. static int hostmixer_release(struct inode *inode, struct file *file)
  224. {
  225. struct hostmixer_state *state = file->private_data;
  226. #ifdef DEBUG
  227. printk(KERN_DEBUG "hostmixer: release called\n");
  228. #endif
  229. os_close_file(state->fd);
  230. kfree(state);
  231. return 0;
  232. }
  233. /* kernel module operations */
  234. static const struct file_operations hostaudio_fops = {
  235. .owner = THIS_MODULE,
  236. .llseek = no_llseek,
  237. .read = hostaudio_read,
  238. .write = hostaudio_write,
  239. .poll = hostaudio_poll,
  240. .unlocked_ioctl = hostaudio_ioctl,
  241. .compat_ioctl = compat_ptr_ioctl,
  242. .mmap = NULL,
  243. .open = hostaudio_open,
  244. .release = hostaudio_release,
  245. };
  246. static const struct file_operations hostmixer_fops = {
  247. .owner = THIS_MODULE,
  248. .llseek = no_llseek,
  249. .unlocked_ioctl = hostmixer_ioctl_mixdev,
  250. .open = hostmixer_open_mixdev,
  251. .release = hostmixer_release,
  252. };
  253. struct {
  254. int dev_audio;
  255. int dev_mixer;
  256. } module_data;
  257. MODULE_AUTHOR("Steve Schmidtke");
  258. MODULE_DESCRIPTION("UML Audio Relay");
  259. MODULE_LICENSE("GPL");
  260. static int __init hostaudio_init_module(void)
  261. {
  262. kernel_param_lock(THIS_MODULE);
  263. printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
  264. dsp, mixer);
  265. kernel_param_unlock(THIS_MODULE);
  266. module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
  267. if (module_data.dev_audio < 0) {
  268. printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
  269. return -ENODEV;
  270. }
  271. module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
  272. if (module_data.dev_mixer < 0) {
  273. printk(KERN_ERR "hostmixer: couldn't register mixer "
  274. "device!\n");
  275. unregister_sound_dsp(module_data.dev_audio);
  276. return -ENODEV;
  277. }
  278. return 0;
  279. }
  280. static void __exit hostaudio_cleanup_module (void)
  281. {
  282. unregister_sound_mixer(module_data.dev_mixer);
  283. unregister_sound_dsp(module_data.dev_audio);
  284. }
  285. module_init(hostaudio_init_module);
  286. module_exit(hostaudio_cleanup_module);