hda_hwdep.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <linux/ctype.h>
  26. #include <sound/core.h>
  27. #include "hda_codec.h"
  28. #include "hda_local.h"
  29. #include <sound/hda_hwdep.h>
  30. #include <sound/minors.h>
  31. /*
  32. * write/read an out-of-bound verb
  33. */
  34. static int verb_write_ioctl(struct hda_codec *codec,
  35. struct hda_verb_ioctl __user *arg)
  36. {
  37. u32 verb, res;
  38. if (get_user(verb, &arg->verb))
  39. return -EFAULT;
  40. res = snd_hda_codec_read(codec, verb >> 24, 0,
  41. (verb >> 8) & 0xffff, verb & 0xff);
  42. if (put_user(res, &arg->res))
  43. return -EFAULT;
  44. return 0;
  45. }
  46. static int get_wcap_ioctl(struct hda_codec *codec,
  47. struct hda_verb_ioctl __user *arg)
  48. {
  49. u32 verb, res;
  50. if (get_user(verb, &arg->verb))
  51. return -EFAULT;
  52. res = get_wcaps(codec, verb >> 24);
  53. if (put_user(res, &arg->res))
  54. return -EFAULT;
  55. return 0;
  56. }
  57. /*
  58. */
  59. static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  60. unsigned int cmd, unsigned long arg)
  61. {
  62. struct hda_codec *codec = hw->private_data;
  63. void __user *argp = (void __user *)arg;
  64. switch (cmd) {
  65. case HDA_IOCTL_PVERSION:
  66. return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
  67. case HDA_IOCTL_VERB_WRITE:
  68. return verb_write_ioctl(codec, argp);
  69. case HDA_IOCTL_GET_WCAP:
  70. return get_wcap_ioctl(codec, argp);
  71. }
  72. return -ENOIOCTLCMD;
  73. }
  74. #ifdef CONFIG_COMPAT
  75. static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
  76. unsigned int cmd, unsigned long arg)
  77. {
  78. return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
  79. }
  80. #endif
  81. static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
  82. {
  83. #ifndef CONFIG_SND_DEBUG_VERBOSE
  84. if (!capable(CAP_SYS_RAWIO))
  85. return -EACCES;
  86. #endif
  87. return 0;
  88. }
  89. static void clear_hwdep_elements(struct hda_codec *codec)
  90. {
  91. char **head;
  92. int i;
  93. /* clear init verbs */
  94. snd_array_free(&codec->init_verbs);
  95. /* clear hints */
  96. head = codec->hints.list;
  97. for (i = 0; i < codec->hints.used; i++, head++)
  98. kfree(*head);
  99. snd_array_free(&codec->hints);
  100. }
  101. static void hwdep_free(struct snd_hwdep *hwdep)
  102. {
  103. clear_hwdep_elements(hwdep->private_data);
  104. }
  105. int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
  106. {
  107. char hwname[16];
  108. struct snd_hwdep *hwdep;
  109. int err;
  110. sprintf(hwname, "HDA Codec %d", codec->addr);
  111. err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
  112. if (err < 0)
  113. return err;
  114. codec->hwdep = hwdep;
  115. sprintf(hwdep->name, "HDA Codec %d", codec->addr);
  116. hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
  117. hwdep->private_data = codec;
  118. hwdep->private_free = hwdep_free;
  119. hwdep->exclusive = 1;
  120. hwdep->ops.open = hda_hwdep_open;
  121. hwdep->ops.ioctl = hda_hwdep_ioctl;
  122. #ifdef CONFIG_COMPAT
  123. hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
  124. #endif
  125. snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
  126. snd_array_init(&codec->hints, sizeof(char *), 32);
  127. return 0;
  128. }
  129. #ifdef CONFIG_SND_HDA_RECONFIG
  130. /*
  131. * sysfs interface
  132. */
  133. static int clear_codec(struct hda_codec *codec)
  134. {
  135. snd_hda_codec_reset(codec);
  136. clear_hwdep_elements(codec);
  137. return 0;
  138. }
  139. static int reconfig_codec(struct hda_codec *codec)
  140. {
  141. int err;
  142. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  143. snd_hda_codec_reset(codec);
  144. err = snd_hda_codec_configure(codec);
  145. if (err < 0)
  146. return err;
  147. /* rebuild PCMs */
  148. err = snd_hda_codec_build_pcms(codec);
  149. if (err < 0)
  150. return err;
  151. /* rebuild mixers */
  152. err = snd_hda_codec_build_controls(codec);
  153. if (err < 0)
  154. return err;
  155. return 0;
  156. }
  157. /*
  158. * allocate a string at most len chars, and remove the trailing EOL
  159. */
  160. static char *kstrndup_noeol(const char *src, size_t len)
  161. {
  162. char *s = kstrndup(src, len, GFP_KERNEL);
  163. char *p;
  164. if (!s)
  165. return NULL;
  166. p = strchr(s, '\n');
  167. if (p)
  168. *p = 0;
  169. return s;
  170. }
  171. #define CODEC_INFO_SHOW(type) \
  172. static ssize_t type##_show(struct device *dev, \
  173. struct device_attribute *attr, \
  174. char *buf) \
  175. { \
  176. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  177. struct hda_codec *codec = hwdep->private_data; \
  178. return sprintf(buf, "0x%x\n", codec->type); \
  179. }
  180. #define CODEC_INFO_STR_SHOW(type) \
  181. static ssize_t type##_show(struct device *dev, \
  182. struct device_attribute *attr, \
  183. char *buf) \
  184. { \
  185. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  186. struct hda_codec *codec = hwdep->private_data; \
  187. return sprintf(buf, "%s\n", \
  188. codec->type ? codec->type : ""); \
  189. }
  190. CODEC_INFO_SHOW(vendor_id);
  191. CODEC_INFO_SHOW(subsystem_id);
  192. CODEC_INFO_SHOW(revision_id);
  193. CODEC_INFO_SHOW(afg);
  194. CODEC_INFO_SHOW(mfg);
  195. CODEC_INFO_STR_SHOW(name);
  196. CODEC_INFO_STR_SHOW(modelname);
  197. #define CODEC_INFO_STORE(type) \
  198. static ssize_t type##_store(struct device *dev, \
  199. struct device_attribute *attr, \
  200. const char *buf, size_t count) \
  201. { \
  202. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  203. struct hda_codec *codec = hwdep->private_data; \
  204. char *after; \
  205. codec->type = simple_strtoul(buf, &after, 0); \
  206. return count; \
  207. }
  208. #define CODEC_INFO_STR_STORE(type) \
  209. static ssize_t type##_store(struct device *dev, \
  210. struct device_attribute *attr, \
  211. const char *buf, size_t count) \
  212. { \
  213. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  214. struct hda_codec *codec = hwdep->private_data; \
  215. char *s = kstrndup_noeol(buf, 64); \
  216. if (!s) \
  217. return -ENOMEM; \
  218. kfree(codec->type); \
  219. codec->type = s; \
  220. return count; \
  221. }
  222. CODEC_INFO_STORE(vendor_id);
  223. CODEC_INFO_STORE(subsystem_id);
  224. CODEC_INFO_STORE(revision_id);
  225. CODEC_INFO_STR_STORE(name);
  226. CODEC_INFO_STR_STORE(modelname);
  227. #define CODEC_ACTION_STORE(type) \
  228. static ssize_t type##_store(struct device *dev, \
  229. struct device_attribute *attr, \
  230. const char *buf, size_t count) \
  231. { \
  232. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  233. struct hda_codec *codec = hwdep->private_data; \
  234. int err = 0; \
  235. if (*buf) \
  236. err = type##_codec(codec); \
  237. return err < 0 ? err : count; \
  238. }
  239. CODEC_ACTION_STORE(reconfig);
  240. CODEC_ACTION_STORE(clear);
  241. static ssize_t init_verbs_store(struct device *dev,
  242. struct device_attribute *attr,
  243. const char *buf, size_t count)
  244. {
  245. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  246. struct hda_codec *codec = hwdep->private_data;
  247. char *p;
  248. struct hda_verb verb, *v;
  249. verb.nid = simple_strtoul(buf, &p, 0);
  250. verb.verb = simple_strtoul(p, &p, 0);
  251. verb.param = simple_strtoul(p, &p, 0);
  252. if (!verb.nid || !verb.verb || !verb.param)
  253. return -EINVAL;
  254. v = snd_array_new(&codec->init_verbs);
  255. if (!v)
  256. return -ENOMEM;
  257. *v = verb;
  258. return count;
  259. }
  260. static ssize_t hints_store(struct device *dev,
  261. struct device_attribute *attr,
  262. const char *buf, size_t count)
  263. {
  264. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  265. struct hda_codec *codec = hwdep->private_data;
  266. char *p;
  267. char **hint;
  268. if (!*buf || isspace(*buf) || *buf == '#' || *buf == '\n')
  269. return count;
  270. p = kstrndup_noeol(buf, 1024);
  271. if (!p)
  272. return -ENOMEM;
  273. hint = snd_array_new(&codec->hints);
  274. if (!hint) {
  275. kfree(p);
  276. return -ENOMEM;
  277. }
  278. *hint = p;
  279. return count;
  280. }
  281. #define CODEC_ATTR_RW(type) \
  282. __ATTR(type, 0644, type##_show, type##_store)
  283. #define CODEC_ATTR_RO(type) \
  284. __ATTR_RO(type)
  285. #define CODEC_ATTR_WO(type) \
  286. __ATTR(type, 0200, NULL, type##_store)
  287. static struct device_attribute codec_attrs[] = {
  288. CODEC_ATTR_RW(vendor_id),
  289. CODEC_ATTR_RW(subsystem_id),
  290. CODEC_ATTR_RW(revision_id),
  291. CODEC_ATTR_RO(afg),
  292. CODEC_ATTR_RO(mfg),
  293. CODEC_ATTR_RW(name),
  294. CODEC_ATTR_RW(modelname),
  295. CODEC_ATTR_WO(init_verbs),
  296. CODEC_ATTR_WO(hints),
  297. CODEC_ATTR_WO(reconfig),
  298. CODEC_ATTR_WO(clear),
  299. };
  300. /*
  301. * create sysfs files on hwdep directory
  302. */
  303. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  304. {
  305. struct snd_hwdep *hwdep = codec->hwdep;
  306. int i;
  307. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  308. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  309. hwdep->device, &codec_attrs[i]);
  310. return 0;
  311. }
  312. #endif /* CONFIG_SND_HDA_RECONFIG */