hostaudio_kern.c 7.5 KB

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