hda_hwdep.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /*
  130. * sysfs interface
  131. */
  132. static int clear_codec(struct hda_codec *codec)
  133. {
  134. snd_hda_codec_reset(codec);
  135. clear_hwdep_elements(codec);
  136. return 0;
  137. }
  138. static int reconfig_codec(struct hda_codec *codec)
  139. {
  140. int err;
  141. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  142. snd_hda_codec_reset(codec);
  143. err = snd_hda_codec_configure(codec);
  144. if (err < 0)
  145. return err;
  146. /* rebuild PCMs */
  147. err = snd_hda_build_pcms(codec->bus);
  148. if (err < 0)
  149. return err;
  150. /* rebuild mixers */
  151. err = snd_hda_codec_build_controls(codec);
  152. if (err < 0)
  153. return err;
  154. return 0;
  155. }
  156. /*
  157. * allocate a string at most len chars, and remove the trailing EOL
  158. */
  159. static char *kstrndup_noeol(const char *src, size_t len)
  160. {
  161. char *s = kstrndup(src, len, GFP_KERNEL);
  162. char *p;
  163. if (!s)
  164. return NULL;
  165. p = strchr(s, '\n');
  166. if (p)
  167. *p = 0;
  168. return s;
  169. }
  170. #define CODEC_INFO_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, "0x%x\n", codec->type); \
  178. }
  179. #define CODEC_INFO_STR_SHOW(type) \
  180. static ssize_t type##_show(struct device *dev, \
  181. struct device_attribute *attr, \
  182. char *buf) \
  183. { \
  184. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  185. struct hda_codec *codec = hwdep->private_data; \
  186. return sprintf(buf, "%s\n", \
  187. codec->type ? codec->type : ""); \
  188. }
  189. CODEC_INFO_SHOW(vendor_id);
  190. CODEC_INFO_SHOW(subsystem_id);
  191. CODEC_INFO_SHOW(revision_id);
  192. CODEC_INFO_SHOW(afg);
  193. CODEC_INFO_SHOW(mfg);
  194. CODEC_INFO_STR_SHOW(name);
  195. CODEC_INFO_STR_SHOW(modelname);
  196. #define CODEC_INFO_STORE(type) \
  197. static ssize_t type##_store(struct device *dev, \
  198. struct device_attribute *attr, \
  199. const char *buf, size_t count) \
  200. { \
  201. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  202. struct hda_codec *codec = hwdep->private_data; \
  203. char *after; \
  204. codec->type = simple_strtoul(buf, &after, 0); \
  205. return count; \
  206. }
  207. #define CODEC_INFO_STR_STORE(type) \
  208. static ssize_t type##_store(struct device *dev, \
  209. struct device_attribute *attr, \
  210. const char *buf, size_t count) \
  211. { \
  212. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  213. struct hda_codec *codec = hwdep->private_data; \
  214. char *s = kstrndup_noeol(buf, 64); \
  215. if (!s) \
  216. return -ENOMEM; \
  217. kfree(codec->type); \
  218. codec->type = s; \
  219. return count; \
  220. }
  221. CODEC_INFO_STORE(vendor_id);
  222. CODEC_INFO_STORE(subsystem_id);
  223. CODEC_INFO_STORE(revision_id);
  224. CODEC_INFO_STR_STORE(name);
  225. CODEC_INFO_STR_STORE(modelname);
  226. #define CODEC_ACTION_STORE(type) \
  227. static ssize_t type##_store(struct device *dev, \
  228. struct device_attribute *attr, \
  229. const char *buf, size_t count) \
  230. { \
  231. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  232. struct hda_codec *codec = hwdep->private_data; \
  233. int err = 0; \
  234. if (*buf) \
  235. err = type##_codec(codec); \
  236. return err < 0 ? err : count; \
  237. }
  238. CODEC_ACTION_STORE(reconfig);
  239. CODEC_ACTION_STORE(clear);
  240. static ssize_t init_verbs_store(struct device *dev,
  241. struct device_attribute *attr,
  242. const char *buf, size_t count)
  243. {
  244. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  245. struct hda_codec *codec = hwdep->private_data;
  246. char *p;
  247. struct hda_verb verb, *v;
  248. verb.nid = simple_strtoul(buf, &p, 0);
  249. verb.verb = simple_strtoul(p, &p, 0);
  250. verb.param = simple_strtoul(p, &p, 0);
  251. if (!verb.nid || !verb.verb || !verb.param)
  252. return -EINVAL;
  253. v = snd_array_new(&codec->init_verbs);
  254. if (!v)
  255. return -ENOMEM;
  256. *v = verb;
  257. return count;
  258. }
  259. static ssize_t hints_store(struct device *dev,
  260. struct device_attribute *attr,
  261. const char *buf, size_t count)
  262. {
  263. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  264. struct hda_codec *codec = hwdep->private_data;
  265. char *p;
  266. char **hint;
  267. if (!*buf || isspace(*buf) || *buf == '#' || *buf == '\n')
  268. return count;
  269. p = kstrndup_noeol(buf, 1024);
  270. if (!p)
  271. return -ENOMEM;
  272. hint = snd_array_new(&codec->hints);
  273. if (!hint) {
  274. kfree(p);
  275. return -ENOMEM;
  276. }
  277. *hint = p;
  278. return count;
  279. }
  280. #define CODEC_ATTR_RW(type) \
  281. __ATTR(type, 0644, type##_show, type##_store)
  282. #define CODEC_ATTR_RO(type) \
  283. __ATTR_RO(type)
  284. #define CODEC_ATTR_WO(type) \
  285. __ATTR(type, 0200, NULL, type##_store)
  286. static struct device_attribute codec_attrs[] = {
  287. CODEC_ATTR_RW(vendor_id),
  288. CODEC_ATTR_RW(subsystem_id),
  289. CODEC_ATTR_RW(revision_id),
  290. CODEC_ATTR_RO(afg),
  291. CODEC_ATTR_RO(mfg),
  292. CODEC_ATTR_RW(name),
  293. CODEC_ATTR_RW(modelname),
  294. CODEC_ATTR_WO(init_verbs),
  295. CODEC_ATTR_WO(hints),
  296. CODEC_ATTR_WO(reconfig),
  297. CODEC_ATTR_WO(clear),
  298. };
  299. /*
  300. * create sysfs files on hwdep directory
  301. */
  302. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  303. {
  304. struct snd_hwdep *hwdep = codec->hwdep;
  305. int i;
  306. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  307. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  308. hwdep->device, &codec_attrs[i]);
  309. return 0;
  310. }