hda_hwdep.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. static void clear_hwdep_elements(struct hda_codec *codec)
  89. {
  90. /* clear init verbs */
  91. snd_array_free(&codec->init_verbs);
  92. }
  93. static void hwdep_free(struct snd_hwdep *hwdep)
  94. {
  95. clear_hwdep_elements(hwdep->private_data);
  96. }
  97. int __devinit snd_hda_create_hwdep(struct hda_codec *codec)
  98. {
  99. char hwname[16];
  100. struct snd_hwdep *hwdep;
  101. int err;
  102. sprintf(hwname, "HDA Codec %d", codec->addr);
  103. err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
  104. if (err < 0)
  105. return err;
  106. codec->hwdep = hwdep;
  107. sprintf(hwdep->name, "HDA Codec %d", codec->addr);
  108. hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
  109. hwdep->private_data = codec;
  110. hwdep->private_free = hwdep_free;
  111. hwdep->exclusive = 1;
  112. hwdep->ops.open = hda_hwdep_open;
  113. hwdep->ops.ioctl = hda_hwdep_ioctl;
  114. #ifdef CONFIG_COMPAT
  115. hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
  116. #endif
  117. snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
  118. return 0;
  119. }
  120. /*
  121. * sysfs interface
  122. */
  123. static int clear_codec(struct hda_codec *codec)
  124. {
  125. snd_hda_codec_reset(codec);
  126. clear_hwdep_elements(codec);
  127. return 0;
  128. }
  129. static int reconfig_codec(struct hda_codec *codec)
  130. {
  131. int err;
  132. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  133. snd_hda_codec_reset(codec);
  134. err = snd_hda_codec_configure(codec);
  135. if (err < 0)
  136. return err;
  137. /* rebuild PCMs */
  138. err = snd_hda_build_pcms(codec->bus);
  139. if (err < 0)
  140. return err;
  141. /* rebuild mixers */
  142. err = snd_hda_codec_build_controls(codec);
  143. if (err < 0)
  144. return err;
  145. return 0;
  146. }
  147. /*
  148. * allocate a string at most len chars, and remove the trailing EOL
  149. */
  150. static char *kstrndup_noeol(const char *src, size_t len)
  151. {
  152. char *s = kstrndup(src, len, GFP_KERNEL);
  153. char *p;
  154. if (!s)
  155. return NULL;
  156. p = strchr(s, '\n');
  157. if (p)
  158. *p = 0;
  159. return s;
  160. }
  161. #define CODEC_INFO_SHOW(type) \
  162. static ssize_t type##_show(struct device *dev, \
  163. struct device_attribute *attr, \
  164. char *buf) \
  165. { \
  166. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  167. struct hda_codec *codec = hwdep->private_data; \
  168. return sprintf(buf, "0x%x\n", codec->type); \
  169. }
  170. #define CODEC_INFO_STR_SHOW(type) \
  171. static ssize_t type##_show(struct device *dev, \
  172. struct device_attribute *attr, \
  173. char *buf) \
  174. { \
  175. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  176. struct hda_codec *codec = hwdep->private_data; \
  177. return sprintf(buf, "%s\n", \
  178. codec->type ? codec->type : ""); \
  179. }
  180. CODEC_INFO_SHOW(vendor_id);
  181. CODEC_INFO_SHOW(subsystem_id);
  182. CODEC_INFO_SHOW(revision_id);
  183. CODEC_INFO_SHOW(afg);
  184. CODEC_INFO_SHOW(mfg);
  185. CODEC_INFO_STR_SHOW(name);
  186. CODEC_INFO_STR_SHOW(modelname);
  187. #define CODEC_INFO_STORE(type) \
  188. static ssize_t type##_store(struct device *dev, \
  189. struct device_attribute *attr, \
  190. const char *buf, size_t count) \
  191. { \
  192. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  193. struct hda_codec *codec = hwdep->private_data; \
  194. char *after; \
  195. codec->type = simple_strtoul(buf, &after, 0); \
  196. return count; \
  197. }
  198. #define CODEC_INFO_STR_STORE(type) \
  199. static ssize_t type##_store(struct device *dev, \
  200. struct device_attribute *attr, \
  201. const char *buf, size_t count) \
  202. { \
  203. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  204. struct hda_codec *codec = hwdep->private_data; \
  205. char *s = kstrndup_noeol(buf, 64); \
  206. if (!s) \
  207. return -ENOMEM; \
  208. kfree(codec->type); \
  209. codec->type = s; \
  210. return count; \
  211. }
  212. CODEC_INFO_STORE(vendor_id);
  213. CODEC_INFO_STORE(subsystem_id);
  214. CODEC_INFO_STORE(revision_id);
  215. CODEC_INFO_STR_STORE(name);
  216. CODEC_INFO_STR_STORE(modelname);
  217. #define CODEC_ACTION_STORE(type) \
  218. static ssize_t type##_store(struct device *dev, \
  219. struct device_attribute *attr, \
  220. const char *buf, size_t count) \
  221. { \
  222. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  223. struct hda_codec *codec = hwdep->private_data; \
  224. int err = 0; \
  225. if (*buf) \
  226. err = type##_codec(codec); \
  227. return err < 0 ? err : count; \
  228. }
  229. CODEC_ACTION_STORE(reconfig);
  230. CODEC_ACTION_STORE(clear);
  231. static ssize_t init_verbs_store(struct device *dev,
  232. struct device_attribute *attr,
  233. const char *buf, size_t count)
  234. {
  235. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  236. struct hda_codec *codec = hwdep->private_data;
  237. char *p;
  238. struct hda_verb verb, *v;
  239. verb.nid = simple_strtoul(buf, &p, 0);
  240. verb.verb = simple_strtoul(p, &p, 0);
  241. verb.param = simple_strtoul(p, &p, 0);
  242. if (!verb.nid || !verb.verb || !verb.param)
  243. return -EINVAL;
  244. v = snd_array_new(&codec->init_verbs);
  245. if (!v)
  246. return -ENOMEM;
  247. *v = verb;
  248. return count;
  249. }
  250. #define CODEC_ATTR_RW(type) \
  251. __ATTR(type, 0644, type##_show, type##_store)
  252. #define CODEC_ATTR_RO(type) \
  253. __ATTR_RO(type)
  254. #define CODEC_ATTR_WO(type) \
  255. __ATTR(type, 0200, NULL, type##_store)
  256. static struct device_attribute codec_attrs[] = {
  257. CODEC_ATTR_RW(vendor_id),
  258. CODEC_ATTR_RW(subsystem_id),
  259. CODEC_ATTR_RW(revision_id),
  260. CODEC_ATTR_RO(afg),
  261. CODEC_ATTR_RO(mfg),
  262. CODEC_ATTR_RW(name),
  263. CODEC_ATTR_RW(modelname),
  264. CODEC_ATTR_WO(init_verbs),
  265. CODEC_ATTR_WO(reconfig),
  266. CODEC_ATTR_WO(clear),
  267. };
  268. /*
  269. * create sysfs files on hwdep directory
  270. */
  271. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  272. {
  273. struct snd_hwdep *hwdep = codec->hwdep;
  274. int i;
  275. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  276. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  277. hwdep->device, &codec_attrs[i]);
  278. return 0;
  279. }