hostaudio_kern.c 7.5 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. *add = 0;
  43. dsp = name;
  44. return 0;
  45. }
  46. __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
  47. static int set_mixer(char *name, int *add)
  48. {
  49. *add = 0;
  50. mixer = name;
  51. return 0;
  52. }
  53. __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
  54. #endif
  55. static DEFINE_MUTEX(hostaudio_mutex);
  56. /* /dev/dsp file operations */
  57. static ssize_t hostaudio_read(struct file *file, char __user *buffer,
  58. size_t count, loff_t *ppos)
  59. {
  60. struct hostaudio_state *state = file->private_data;
  61. void *kbuf;
  62. int err;
  63. #ifdef DEBUG
  64. printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
  65. #endif
  66. kbuf = kmalloc(count, GFP_KERNEL);
  67. if (kbuf == NULL)
  68. return -ENOMEM;
  69. err = os_read_file(state->fd, kbuf, count);
  70. if (err < 0)
  71. goto out;
  72. if (copy_to_user(buffer, kbuf, err))
  73. err = -EFAULT;
  74. out:
  75. kfree(kbuf);
  76. return err;
  77. }
  78. static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
  79. size_t count, loff_t *ppos)
  80. {
  81. struct hostaudio_state *state = file->private_data;
  82. void *kbuf;
  83. int err;
  84. #ifdef DEBUG
  85. printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
  86. #endif
  87. kbuf = memdup_user(buffer, count);
  88. if (IS_ERR(kbuf))
  89. return PTR_ERR(kbuf);
  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 __poll_t hostaudio_poll(struct file *file,
  99. struct poll_table_struct *wait)
  100. {
  101. #ifdef DEBUG
  102. printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
  103. #endif
  104. return 0;
  105. }
  106. static long hostaudio_ioctl(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(KERN_DEBUG "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 __user *) 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 __user *) 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. kernel_param_lock(THIS_MODULE);
  151. printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
  152. kernel_param_unlock(THIS_MODULE);
  153. #endif
  154. state = kmalloc_obj(struct hostaudio_state);
  155. if (state == NULL)
  156. return -ENOMEM;
  157. if (file->f_mode & FMODE_READ)
  158. r = 1;
  159. if (file->f_mode & FMODE_WRITE)
  160. w = 1;
  161. kernel_param_lock(THIS_MODULE);
  162. mutex_lock(&hostaudio_mutex);
  163. ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
  164. mutex_unlock(&hostaudio_mutex);
  165. kernel_param_unlock(THIS_MODULE);
  166. if (ret < 0) {
  167. kfree(state);
  168. return ret;
  169. }
  170. state->fd = ret;
  171. file->private_data = state;
  172. return 0;
  173. }
  174. static int hostaudio_release(struct inode *inode, struct file *file)
  175. {
  176. struct hostaudio_state *state = file->private_data;
  177. #ifdef DEBUG
  178. printk(KERN_DEBUG "hostaudio: release called\n");
  179. #endif
  180. os_close_file(state->fd);
  181. kfree(state);
  182. return 0;
  183. }
  184. /* /dev/mixer file operations */
  185. static long hostmixer_ioctl_mixdev(struct file *file,
  186. unsigned int cmd, unsigned long arg)
  187. {
  188. struct hostmixer_state *state = file->private_data;
  189. #ifdef DEBUG
  190. printk(KERN_DEBUG "hostmixer: ioctl called\n");
  191. #endif
  192. return os_ioctl_generic(state->fd, cmd, arg);
  193. }
  194. static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
  195. {
  196. struct hostmixer_state *state;
  197. int r = 0, w = 0;
  198. int ret;
  199. #ifdef DEBUG
  200. printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
  201. #endif
  202. state = kmalloc_obj(struct hostmixer_state);
  203. if (state == NULL)
  204. return -ENOMEM;
  205. if (file->f_mode & FMODE_READ)
  206. r = 1;
  207. if (file->f_mode & FMODE_WRITE)
  208. w = 1;
  209. kernel_param_lock(THIS_MODULE);
  210. mutex_lock(&hostaudio_mutex);
  211. ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
  212. mutex_unlock(&hostaudio_mutex);
  213. kernel_param_unlock(THIS_MODULE);
  214. if (ret < 0) {
  215. kernel_param_lock(THIS_MODULE);
  216. printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
  217. "err = %d\n", dsp, -ret);
  218. kernel_param_unlock(THIS_MODULE);
  219. kfree(state);
  220. return ret;
  221. }
  222. file->private_data = state;
  223. return 0;
  224. }
  225. static int hostmixer_release(struct inode *inode, struct file *file)
  226. {
  227. struct hostmixer_state *state = file->private_data;
  228. #ifdef DEBUG
  229. printk(KERN_DEBUG "hostmixer: release called\n");
  230. #endif
  231. os_close_file(state->fd);
  232. kfree(state);
  233. return 0;
  234. }
  235. /* kernel module operations */
  236. static const struct file_operations hostaudio_fops = {
  237. .owner = THIS_MODULE,
  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. .unlocked_ioctl = hostmixer_ioctl_mixdev,
  250. .open = hostmixer_open_mixdev,
  251. .release = hostmixer_release,
  252. };
  253. static 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);