hostaudio_kern.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. __poll_t mask = 0;
  100. #ifdef DEBUG
  101. printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
  102. #endif
  103. return mask;
  104. }
  105. static long hostaudio_ioctl(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(KERN_DEBUG "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. kernel_param_lock(THIS_MODULE);
  150. printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
  151. kernel_param_unlock(THIS_MODULE);
  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. kernel_param_lock(THIS_MODULE);
  161. mutex_lock(&hostaudio_mutex);
  162. ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
  163. mutex_unlock(&hostaudio_mutex);
  164. kernel_param_unlock(THIS_MODULE);
  165. if (ret < 0) {
  166. kfree(state);
  167. return ret;
  168. }
  169. state->fd = ret;
  170. file->private_data = state;
  171. return 0;
  172. }
  173. static int hostaudio_release(struct inode *inode, struct file *file)
  174. {
  175. struct hostaudio_state *state = file->private_data;
  176. #ifdef DEBUG
  177. printk(KERN_DEBUG "hostaudio: release called\n");
  178. #endif
  179. os_close_file(state->fd);
  180. kfree(state);
  181. return 0;
  182. }
  183. /* /dev/mixer file operations */
  184. static long hostmixer_ioctl_mixdev(struct file *file,
  185. unsigned int cmd, unsigned long arg)
  186. {
  187. struct hostmixer_state *state = file->private_data;
  188. #ifdef DEBUG
  189. printk(KERN_DEBUG "hostmixer: ioctl called\n");
  190. #endif
  191. return os_ioctl_generic(state->fd, cmd, arg);
  192. }
  193. static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
  194. {
  195. struct hostmixer_state *state;
  196. int r = 0, w = 0;
  197. int ret;
  198. #ifdef DEBUG
  199. printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
  200. #endif
  201. state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
  202. if (state == NULL)
  203. return -ENOMEM;
  204. if (file->f_mode & FMODE_READ)
  205. r = 1;
  206. if (file->f_mode & FMODE_WRITE)
  207. w = 1;
  208. kernel_param_lock(THIS_MODULE);
  209. mutex_lock(&hostaudio_mutex);
  210. ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
  211. mutex_unlock(&hostaudio_mutex);
  212. kernel_param_unlock(THIS_MODULE);
  213. if (ret < 0) {
  214. kernel_param_lock(THIS_MODULE);
  215. printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
  216. "err = %d\n", dsp, -ret);
  217. kernel_param_unlock(THIS_MODULE);
  218. kfree(state);
  219. return ret;
  220. }
  221. file->private_data = state;
  222. return 0;
  223. }
  224. static int hostmixer_release(struct inode *inode, struct file *file)
  225. {
  226. struct hostmixer_state *state = file->private_data;
  227. #ifdef DEBUG
  228. printk(KERN_DEBUG "hostmixer: release called\n");
  229. #endif
  230. os_close_file(state->fd);
  231. kfree(state);
  232. return 0;
  233. }
  234. /* kernel module operations */
  235. static const struct file_operations hostaudio_fops = {
  236. .owner = THIS_MODULE,
  237. .llseek = no_llseek,
  238. .read = hostaudio_read,
  239. .write = hostaudio_write,
  240. .poll = hostaudio_poll,
  241. .unlocked_ioctl = hostaudio_ioctl,
  242. .compat_ioctl = compat_ptr_ioctl,
  243. .mmap = NULL,
  244. .open = hostaudio_open,
  245. .release = hostaudio_release,
  246. };
  247. static const struct file_operations hostmixer_fops = {
  248. .owner = THIS_MODULE,
  249. .llseek = no_llseek,
  250. .unlocked_ioctl = hostmixer_ioctl_mixdev,
  251. .open = hostmixer_open_mixdev,
  252. .release = hostmixer_release,
  253. };
  254. struct {
  255. int dev_audio;
  256. int dev_mixer;
  257. } module_data;
  258. MODULE_AUTHOR("Steve Schmidtke");
  259. MODULE_DESCRIPTION("UML Audio Relay");
  260. MODULE_LICENSE("GPL");
  261. static int __init hostaudio_init_module(void)
  262. {
  263. kernel_param_lock(THIS_MODULE);
  264. printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
  265. dsp, mixer);
  266. kernel_param_unlock(THIS_MODULE);
  267. module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
  268. if (module_data.dev_audio < 0) {
  269. printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
  270. return -ENODEV;
  271. }
  272. module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
  273. if (module_data.dev_mixer < 0) {
  274. printk(KERN_ERR "hostmixer: couldn't register mixer "
  275. "device!\n");
  276. unregister_sound_dsp(module_data.dev_audio);
  277. return -ENODEV;
  278. }
  279. return 0;
  280. }
  281. static void __exit hostaudio_cleanup_module (void)
  282. {
  283. unregister_sound_mixer(module_data.dev_mixer);
  284. unregister_sound_dsp(module_data.dev_audio);
  285. }
  286. module_init(hostaudio_init_module);
  287. module_exit(hostaudio_cleanup_module);