hda_hwdep.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * HWDEP Interface for HD-audio codec
  3. *
  4. * Copyright (c) 2007 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This driver is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This driver is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/pci.h>
  23. #include <linux/compat.h>
  24. #include <linux/mutex.h>
  25. #include <sound/core.h>
  26. #include "hda_codec.h"
  27. #include "hda_local.h"
  28. #include <sound/hda_hwdep.h>
  29. #include <sound/minors.h>
  30. /*
  31. * write/read an out-of-bound verb
  32. */
  33. static int verb_write_ioctl(struct hda_codec *codec,
  34. struct hda_verb_ioctl __user *arg)
  35. {
  36. u32 verb, res;
  37. if (get_user(verb, &arg->verb))
  38. return -EFAULT;
  39. res = snd_hda_codec_read(codec, verb >> 24, 0,
  40. (verb >> 8) & 0xffff, verb & 0xff);
  41. if (put_user(res, &arg->res))
  42. return -EFAULT;
  43. return 0;
  44. }
  45. static int get_wcap_ioctl(struct hda_codec *codec,
  46. struct hda_verb_ioctl __user *arg)
  47. {
  48. u32 verb, res;
  49. if (get_user(verb, &arg->verb))
  50. return -EFAULT;
  51. res = get_wcaps(codec, verb >> 24);
  52. if (put_user(res, &arg->res))
  53. return -EFAULT;
  54. return 0;
  55. }
  56. /*
  57. */
  58. static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  59. unsigned int cmd, unsigned long arg)
  60. {
  61. struct hda_codec *codec = hw->private_data;
  62. void __user *argp = (void __user *)arg;
  63. switch (cmd) {
  64. case HDA_IOCTL_PVERSION:
  65. return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
  66. case HDA_IOCTL_VERB_WRITE:
  67. return verb_write_ioctl(codec, argp);
  68. case HDA_IOCTL_GET_WCAP:
  69. return get_wcap_ioctl(codec, argp);
  70. }
  71. return -ENOIOCTLCMD;
  72. }
  73. #ifdef CONFIG_COMPAT
  74. static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
  75. unsigned int cmd, unsigned long arg)
  76. {
  77. return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
  78. }
  79. #endif
  80. static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
  81. {
  82. #ifndef CONFIG_SND_DEBUG_VERBOSE
  83. if (!capable(CAP_SYS_RAWIO))
  84. return -EACCES;
  85. #endif
  86. return 0;
  87. }
  88. int __devinit snd_hda_create_hwdep(struct hda_codec *codec)
  89. {
  90. char hwname[16];
  91. struct snd_hwdep *hwdep;
  92. int err;
  93. sprintf(hwname, "HDA Codec %d", codec->addr);
  94. err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
  95. if (err < 0)
  96. return err;
  97. codec->hwdep = hwdep;
  98. sprintf(hwdep->name, "HDA Codec %d", codec->addr);
  99. hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
  100. hwdep->private_data = codec;
  101. hwdep->exclusive = 1;
  102. hwdep->ops.open = hda_hwdep_open;
  103. hwdep->ops.ioctl = hda_hwdep_ioctl;
  104. #ifdef CONFIG_COMPAT
  105. hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
  106. #endif
  107. return 0;
  108. }
  109. /*
  110. * sysfs interface
  111. */
  112. static int clear_codec(struct hda_codec *codec)
  113. {
  114. snd_hda_codec_reset(codec);
  115. return 0;
  116. }
  117. static int reconfig_codec(struct hda_codec *codec)
  118. {
  119. int err;
  120. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  121. snd_hda_codec_reset(codec);
  122. err = snd_hda_codec_configure(codec);
  123. if (err < 0)
  124. return err;
  125. /* rebuild PCMs */
  126. err = snd_hda_build_pcms(codec->bus);
  127. if (err < 0)
  128. return err;
  129. /* rebuild mixers */
  130. err = snd_hda_codec_build_controls(codec);
  131. if (err < 0)
  132. return err;
  133. return 0;
  134. }
  135. /*
  136. * allocate a string at most len chars, and remove the trailing EOL
  137. */
  138. static char *kstrndup_noeol(const char *src, size_t len)
  139. {
  140. char *s = kstrndup(src, len, GFP_KERNEL);
  141. char *p;
  142. if (!s)
  143. return NULL;
  144. p = strchr(s, '\n');
  145. if (p)
  146. *p = 0;
  147. return s;
  148. }
  149. #define CODEC_INFO_SHOW(type) \
  150. static ssize_t type##_show(struct device *dev, \
  151. struct device_attribute *attr, \
  152. char *buf) \
  153. { \
  154. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  155. struct hda_codec *codec = hwdep->private_data; \
  156. return sprintf(buf, "0x%x\n", codec->type); \
  157. }
  158. #define CODEC_INFO_STR_SHOW(type) \
  159. static ssize_t type##_show(struct device *dev, \
  160. struct device_attribute *attr, \
  161. char *buf) \
  162. { \
  163. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  164. struct hda_codec *codec = hwdep->private_data; \
  165. return sprintf(buf, "%s\n", \
  166. codec->type ? codec->type : ""); \
  167. }
  168. CODEC_INFO_SHOW(vendor_id);
  169. CODEC_INFO_SHOW(subsystem_id);
  170. CODEC_INFO_SHOW(revision_id);
  171. CODEC_INFO_SHOW(afg);
  172. CODEC_INFO_SHOW(mfg);
  173. CODEC_INFO_STR_SHOW(name);
  174. CODEC_INFO_STR_SHOW(modelname);
  175. #define CODEC_INFO_STORE(type) \
  176. static ssize_t type##_store(struct device *dev, \
  177. struct device_attribute *attr, \
  178. const char *buf, size_t count) \
  179. { \
  180. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  181. struct hda_codec *codec = hwdep->private_data; \
  182. char *after; \
  183. codec->type = simple_strtoul(buf, &after, 0); \
  184. return count; \
  185. }
  186. #define CODEC_INFO_STR_STORE(type) \
  187. static ssize_t type##_store(struct device *dev, \
  188. struct device_attribute *attr, \
  189. const char *buf, size_t count) \
  190. { \
  191. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  192. struct hda_codec *codec = hwdep->private_data; \
  193. char *s = kstrndup_noeol(buf, 64); \
  194. if (!s) \
  195. return -ENOMEM; \
  196. kfree(codec->type); \
  197. codec->type = s; \
  198. return count; \
  199. }
  200. CODEC_INFO_STORE(vendor_id);
  201. CODEC_INFO_STORE(subsystem_id);
  202. CODEC_INFO_STORE(revision_id);
  203. CODEC_INFO_STR_STORE(name);
  204. CODEC_INFO_STR_STORE(modelname);
  205. #define CODEC_ACTION_STORE(type) \
  206. static ssize_t type##_store(struct device *dev, \
  207. struct device_attribute *attr, \
  208. const char *buf, size_t count) \
  209. { \
  210. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  211. struct hda_codec *codec = hwdep->private_data; \
  212. int err = 0; \
  213. if (*buf) \
  214. err = type##_codec(codec); \
  215. return err < 0 ? err : count; \
  216. }
  217. CODEC_ACTION_STORE(reconfig);
  218. CODEC_ACTION_STORE(clear);
  219. #define CODEC_ATTR_RW(type) \
  220. __ATTR(type, 0644, type##_show, type##_store)
  221. #define CODEC_ATTR_RO(type) \
  222. __ATTR_RO(type)
  223. #define CODEC_ATTR_WO(type) \
  224. __ATTR(type, 0200, NULL, type##_store)
  225. static struct device_attribute codec_attrs[] = {
  226. CODEC_ATTR_RW(vendor_id),
  227. CODEC_ATTR_RW(subsystem_id),
  228. CODEC_ATTR_RW(revision_id),
  229. CODEC_ATTR_RO(afg),
  230. CODEC_ATTR_RO(mfg),
  231. CODEC_ATTR_RW(name),
  232. CODEC_ATTR_RW(modelname),
  233. CODEC_ATTR_WO(reconfig),
  234. CODEC_ATTR_WO(clear),
  235. };
  236. /*
  237. * create sysfs files on hwdep directory
  238. */
  239. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  240. {
  241. struct snd_hwdep *hwdep = codec->hwdep;
  242. int i;
  243. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  244. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  245. hwdep->device, &codec_attrs[i]);
  246. return 0;
  247. }