hostaudio_kern.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #ifndef MODULE
  36. static int set_dsp(char *name, int *add)
  37. {
  38. dsp = name;
  39. return 0;
  40. }
  41. __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
  42. static int set_mixer(char *name, int *add)
  43. {
  44. mixer = name;
  45. return 0;
  46. }
  47. __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
  48. #else /*MODULE*/
  49. module_param(dsp, charp, 0644);
  50. MODULE_PARM_DESC(dsp, DSP_HELP);
  51. module_param(mixer, charp, 0644);
  52. MODULE_PARM_DESC(mixer, MIXER_HELP);
  53. #endif
  54. static DEFINE_MUTEX(hostaudio_mutex);
  55. /* /dev/dsp file operations */
  56. static ssize_t hostaudio_read(struct file *file, char __user *buffer,
  57. size_t count, loff_t *ppos)
  58. {
  59. struct hostaudio_state *state = file->private_data;
  60. void *kbuf;
  61. int err;
  62. #ifdef DEBUG
  63. printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
  64. #endif
  65. kbuf = kmalloc(count, GFP_KERNEL);
  66. if (kbuf == NULL)
  67. return -ENOMEM;
  68. err = os_read_file(state->fd, kbuf, count);
  69. if (err < 0)
  70. goto out;
  71. if (copy_to_user(buffer, kbuf, err))
  72. err = -EFAULT;
  73. out:
  74. kfree(kbuf);
  75. return err;
  76. }
  77. static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
  78. size_t count, loff_t *ppos)
  79. {
  80. struct hostaudio_state *state = file->private_data;
  81. void *kbuf;
  82. int err;
  83. #ifdef DEBUG
  84. printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
  85. #endif
  86. kbuf = kmalloc(count, GFP_KERNEL);
  87. if (kbuf == NULL)
  88. return -ENOMEM;
  89. err = -EFAULT;
  90. if (copy_from_user(kbuf, buffer, count))
  91. goto out;
  92. err = os_write_file(state->fd, kbuf, count);
  93. if (err < 0)
  94. goto out;
  95. *ppos += err;
  96. out:
  97. kfree(kbuf);
  98. return err;
  99. }
  100. static unsigned int hostaudio_poll(struct file *file,
  101. struct poll_table_struct *wait)
  102. {
  103. unsigned int mask = 0;
  104. #ifdef DEBUG
  105. printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
  106. #endif
  107. return mask;
  108. }
  109. static long hostaudio_ioctl(struct file *file,
  110. unsigned int cmd, unsigned long arg)
  111. {
  112. struct hostaudio_state *state = file->private_data;
  113. unsigned long data = 0;
  114. int err;
  115. #ifdef DEBUG
  116. printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
  117. #endif
  118. switch(cmd){
  119. case SNDCTL_DSP_SPEED:
  120. case SNDCTL_DSP_STEREO:
  121. case SNDCTL_DSP_GETBLKSIZE:
  122. case SNDCTL_DSP_CHANNELS:
  123. case SNDCTL_DSP_SUBDIVIDE:
  124. case SNDCTL_DSP_SETFRAGMENT:
  125. if (get_user(data, (int __user *) arg))
  126. return -EFAULT;
  127. break;
  128. default:
  129. break;
  130. }
  131. err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
  132. switch(cmd){
  133. case SNDCTL_DSP_SPEED:
  134. case SNDCTL_DSP_STEREO:
  135. case SNDCTL_DSP_GETBLKSIZE:
  136. case SNDCTL_DSP_CHANNELS:
  137. case SNDCTL_DSP_SUBDIVIDE:
  138. case SNDCTL_DSP_SETFRAGMENT:
  139. if (put_user(data, (int __user *) arg))
  140. return -EFAULT;
  141. break;
  142. default:
  143. break;
  144. }
  145. return err;
  146. }
  147. static int hostaudio_open(struct inode *inode, struct file *file)
  148. {
  149. struct hostaudio_state *state;
  150. int r = 0, w = 0;
  151. int ret;
  152. #ifdef DEBUG
  153. kparam_block_sysfs_write(dsp);
  154. printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
  155. kparam_unblock_sysfs_write(dsp);
  156. #endif
  157. state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
  158. if (state == NULL)
  159. return -ENOMEM;
  160. if (file->f_mode & FMODE_READ)
  161. r = 1;
  162. if (file->f_mode & FMODE_WRITE)
  163. w = 1;
  164. kparam_block_sysfs_write(dsp);
  165. mutex_lock(&hostaudio_mutex);
  166. ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
  167. mutex_unlock(&hostaudio_mutex);
  168. kparam_unblock_sysfs_write(dsp);
  169. if (ret < 0) {
  170. kfree(state);
  171. return ret;
  172. }
  173. state->fd = ret;
  174. file->private_data = state;
  175. return 0;
  176. }
  177. static int hostaudio_release(struct inode *inode, struct file *file)
  178. {
  179. struct hostaudio_state *state = file->private_data;
  180. #ifdef DEBUG
  181. printk(KERN_DEBUG "hostaudio: release called\n");
  182. #endif
  183. os_close_file(state->fd);
  184. kfree(state);
  185. return 0;
  186. }
  187. /* /dev/mixer file operations */
  188. static long hostmixer_ioctl_mixdev(struct file *file,
  189. unsigned int cmd, unsigned long arg)
  190. {
  191. struct hostmixer_state *state = file->private_data;
  192. #ifdef DEBUG
  193. printk(KERN_DEBUG "hostmixer: ioctl called\n");
  194. #endif
  195. return os_ioctl_generic(state->fd, cmd, arg);
  196. }
  197. static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
  198. {
  199. struct hostmixer_state *state;
  200. int r = 0, w = 0;
  201. int ret;
  202. #ifdef DEBUG
  203. printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
  204. #endif
  205. state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
  206. if (state == NULL)
  207. return -ENOMEM;
  208. if (file->f_mode & FMODE_READ)
  209. r = 1;
  210. if (file->f_mode & FMODE_WRITE)
  211. w = 1;
  212. kparam_block_sysfs_write(mixer);
  213. mutex_lock(&hostaudio_mutex);
  214. ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
  215. mutex_unlock(&hostaudio_mutex);
  216. kparam_unblock_sysfs_write(mixer);
  217. if (ret < 0) {
  218. kparam_block_sysfs_write(dsp);
  219. printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
  220. "err = %d\n", dsp, -ret);
  221. kparam_unblock_sysfs_write(dsp);
  222. kfree(state);
  223. return ret;
  224. }
  225. file->private_data = state;
  226. return 0;
  227. }
  228. static int hostmixer_release(struct inode *inode, struct file *file)
  229. {
  230. struct hostmixer_state *state = file->private_data;
  231. #ifdef DEBUG
  232. printk(KERN_DEBUG "hostmixer: release called\n");
  233. #endif
  234. os_close_file(state->fd);
  235. kfree(state);
  236. return 0;
  237. }
  238. /* kernel module operations */
  239. static const struct file_operations hostaudio_fops = {
  240. .owner = THIS_MODULE,
  241. .llseek = no_llseek,
  242. .read = hostaudio_read,
  243. .write = hostaudio_write,
  244. .poll = hostaudio_poll,
  245. .unlocked_ioctl = hostaudio_ioctl,
  246. .mmap = NULL,
  247. .open = hostaudio_open,
  248. .release = hostaudio_release,
  249. };
  250. static const struct file_operations hostmixer_fops = {
  251. .owner = THIS_MODULE,
  252. .llseek = no_llseek,
  253. .unlocked_ioctl = hostmixer_ioctl_mixdev,
  254. .open = hostmixer_open_mixdev,
  255. .release = hostmixer_release,
  256. };
  257. struct {
  258. int dev_audio;
  259. int dev_mixer;
  260. } module_data;
  261. MODULE_AUTHOR("Steve Schmidtke");
  262. MODULE_DESCRIPTION("UML Audio Relay");
  263. MODULE_LICENSE("GPL");
  264. static int __init hostaudio_init_module(void)
  265. {
  266. __kernel_param_lock();
  267. printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
  268. dsp, mixer);
  269. __kernel_param_unlock();
  270. module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
  271. if (module_data.dev_audio < 0) {
  272. printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
  273. return -ENODEV;
  274. }
  275. module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
  276. if (module_data.dev_mixer < 0) {
  277. printk(KERN_ERR "hostmixer: couldn't register mixer "
  278. "device!\n");
  279. unregister_sound_dsp(module_data.dev_audio);
  280. return -ENODEV;
  281. }
  282. return 0;
  283. }
  284. static void __exit hostaudio_cleanup_module (void)
  285. {
  286. unregister_sound_mixer(module_data.dev_mixer);
  287. unregister_sound_dsp(module_data.dev_audio);
  288. }
  289. module_init(hostaudio_init_module);
  290. module_exit(hostaudio_cleanup_module);