hda_hwdep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. snd_array_free(&codec->user_pins);
  101. }
  102. static void hwdep_free(struct snd_hwdep *hwdep)
  103. {
  104. clear_hwdep_elements(hwdep->private_data);
  105. }
  106. int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
  107. {
  108. char hwname[16];
  109. struct snd_hwdep *hwdep;
  110. int err;
  111. sprintf(hwname, "HDA Codec %d", codec->addr);
  112. err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
  113. if (err < 0)
  114. return err;
  115. codec->hwdep = hwdep;
  116. sprintf(hwdep->name, "HDA Codec %d", codec->addr);
  117. hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
  118. hwdep->private_data = codec;
  119. hwdep->private_free = hwdep_free;
  120. hwdep->exclusive = 1;
  121. hwdep->ops.open = hda_hwdep_open;
  122. hwdep->ops.ioctl = hda_hwdep_ioctl;
  123. #ifdef CONFIG_COMPAT
  124. hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
  125. #endif
  126. snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
  127. snd_array_init(&codec->hints, sizeof(char *), 32);
  128. snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
  129. return 0;
  130. }
  131. #ifdef CONFIG_SND_HDA_RECONFIG
  132. /*
  133. * sysfs interface
  134. */
  135. static int clear_codec(struct hda_codec *codec)
  136. {
  137. snd_hda_codec_reset(codec);
  138. clear_hwdep_elements(codec);
  139. return 0;
  140. }
  141. static int reconfig_codec(struct hda_codec *codec)
  142. {
  143. int err;
  144. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  145. snd_hda_codec_reset(codec);
  146. err = snd_hda_codec_configure(codec);
  147. if (err < 0)
  148. return err;
  149. /* rebuild PCMs */
  150. err = snd_hda_codec_build_pcms(codec);
  151. if (err < 0)
  152. return err;
  153. /* rebuild mixers */
  154. err = snd_hda_codec_build_controls(codec);
  155. if (err < 0)
  156. return err;
  157. return snd_card_register(codec->bus->card);
  158. }
  159. /*
  160. * allocate a string at most len chars, and remove the trailing EOL
  161. */
  162. static char *kstrndup_noeol(const char *src, size_t len)
  163. {
  164. char *s = kstrndup(src, len, GFP_KERNEL);
  165. char *p;
  166. if (!s)
  167. return NULL;
  168. p = strchr(s, '\n');
  169. if (p)
  170. *p = 0;
  171. return s;
  172. }
  173. #define CODEC_INFO_SHOW(type) \
  174. static ssize_t type##_show(struct device *dev, \
  175. struct device_attribute *attr, \
  176. char *buf) \
  177. { \
  178. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  179. struct hda_codec *codec = hwdep->private_data; \
  180. return sprintf(buf, "0x%x\n", codec->type); \
  181. }
  182. #define CODEC_INFO_STR_SHOW(type) \
  183. static ssize_t type##_show(struct device *dev, \
  184. struct device_attribute *attr, \
  185. char *buf) \
  186. { \
  187. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  188. struct hda_codec *codec = hwdep->private_data; \
  189. return sprintf(buf, "%s\n", \
  190. codec->type ? codec->type : ""); \
  191. }
  192. CODEC_INFO_SHOW(vendor_id);
  193. CODEC_INFO_SHOW(subsystem_id);
  194. CODEC_INFO_SHOW(revision_id);
  195. CODEC_INFO_SHOW(afg);
  196. CODEC_INFO_SHOW(mfg);
  197. CODEC_INFO_STR_SHOW(name);
  198. CODEC_INFO_STR_SHOW(modelname);
  199. #define CODEC_INFO_STORE(type) \
  200. static ssize_t type##_store(struct device *dev, \
  201. struct device_attribute *attr, \
  202. const char *buf, size_t count) \
  203. { \
  204. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  205. struct hda_codec *codec = hwdep->private_data; \
  206. char *after; \
  207. codec->type = simple_strtoul(buf, &after, 0); \
  208. return count; \
  209. }
  210. #define CODEC_INFO_STR_STORE(type) \
  211. static ssize_t type##_store(struct device *dev, \
  212. struct device_attribute *attr, \
  213. const char *buf, size_t count) \
  214. { \
  215. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  216. struct hda_codec *codec = hwdep->private_data; \
  217. char *s = kstrndup_noeol(buf, 64); \
  218. if (!s) \
  219. return -ENOMEM; \
  220. kfree(codec->type); \
  221. codec->type = s; \
  222. return count; \
  223. }
  224. CODEC_INFO_STORE(vendor_id);
  225. CODEC_INFO_STORE(subsystem_id);
  226. CODEC_INFO_STORE(revision_id);
  227. CODEC_INFO_STR_STORE(name);
  228. CODEC_INFO_STR_STORE(modelname);
  229. #define CODEC_ACTION_STORE(type) \
  230. static ssize_t type##_store(struct device *dev, \
  231. struct device_attribute *attr, \
  232. const char *buf, size_t count) \
  233. { \
  234. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  235. struct hda_codec *codec = hwdep->private_data; \
  236. int err = 0; \
  237. if (*buf) \
  238. err = type##_codec(codec); \
  239. return err < 0 ? err : count; \
  240. }
  241. CODEC_ACTION_STORE(reconfig);
  242. CODEC_ACTION_STORE(clear);
  243. static ssize_t init_verbs_store(struct device *dev,
  244. struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  248. struct hda_codec *codec = hwdep->private_data;
  249. struct hda_verb *v;
  250. int nid, verb, param;
  251. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  252. return -EINVAL;
  253. if (!nid || !verb)
  254. return -EINVAL;
  255. v = snd_array_new(&codec->init_verbs);
  256. if (!v)
  257. return -ENOMEM;
  258. v->nid = nid;
  259. v->verb = verb;
  260. v->param = param;
  261. return count;
  262. }
  263. static ssize_t hints_store(struct device *dev,
  264. struct device_attribute *attr,
  265. const char *buf, size_t count)
  266. {
  267. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  268. struct hda_codec *codec = hwdep->private_data;
  269. char *p;
  270. char **hint;
  271. if (!*buf || isspace(*buf) || *buf == '#' || *buf == '\n')
  272. return count;
  273. p = kstrndup_noeol(buf, 1024);
  274. if (!p)
  275. return -ENOMEM;
  276. hint = snd_array_new(&codec->hints);
  277. if (!hint) {
  278. kfree(p);
  279. return -ENOMEM;
  280. }
  281. *hint = p;
  282. return count;
  283. }
  284. static ssize_t pin_configs_show(struct hda_codec *codec,
  285. struct snd_array *list,
  286. char *buf)
  287. {
  288. int i, len = 0;
  289. for (i = 0; i < list->used; i++) {
  290. struct hda_pincfg *pin = snd_array_elem(list, i);
  291. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  292. pin->nid, pin->cfg);
  293. }
  294. return len;
  295. }
  296. static ssize_t init_pin_configs_show(struct device *dev,
  297. struct device_attribute *attr,
  298. char *buf)
  299. {
  300. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  301. struct hda_codec *codec = hwdep->private_data;
  302. return pin_configs_show(codec, &codec->init_pins, buf);
  303. }
  304. static ssize_t user_pin_configs_show(struct device *dev,
  305. struct device_attribute *attr,
  306. char *buf)
  307. {
  308. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  309. struct hda_codec *codec = hwdep->private_data;
  310. return pin_configs_show(codec, &codec->user_pins, buf);
  311. }
  312. static ssize_t driver_pin_configs_show(struct device *dev,
  313. struct device_attribute *attr,
  314. char *buf)
  315. {
  316. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  317. struct hda_codec *codec = hwdep->private_data;
  318. return pin_configs_show(codec, &codec->driver_pins, buf);
  319. }
  320. #define MAX_PIN_CONFIGS 32
  321. static ssize_t user_pin_configs_store(struct device *dev,
  322. struct device_attribute *attr,
  323. const char *buf, size_t count)
  324. {
  325. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  326. struct hda_codec *codec = hwdep->private_data;
  327. int nid, cfg;
  328. int err;
  329. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  330. return -EINVAL;
  331. if (!nid)
  332. return -EINVAL;
  333. err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  334. if (err < 0)
  335. return err;
  336. return count;
  337. }
  338. #define CODEC_ATTR_RW(type) \
  339. __ATTR(type, 0644, type##_show, type##_store)
  340. #define CODEC_ATTR_RO(type) \
  341. __ATTR_RO(type)
  342. #define CODEC_ATTR_WO(type) \
  343. __ATTR(type, 0200, NULL, type##_store)
  344. static struct device_attribute codec_attrs[] = {
  345. CODEC_ATTR_RW(vendor_id),
  346. CODEC_ATTR_RW(subsystem_id),
  347. CODEC_ATTR_RW(revision_id),
  348. CODEC_ATTR_RO(afg),
  349. CODEC_ATTR_RO(mfg),
  350. CODEC_ATTR_RW(name),
  351. CODEC_ATTR_RW(modelname),
  352. CODEC_ATTR_WO(init_verbs),
  353. CODEC_ATTR_WO(hints),
  354. CODEC_ATTR_RO(init_pin_configs),
  355. CODEC_ATTR_RW(user_pin_configs),
  356. CODEC_ATTR_RO(driver_pin_configs),
  357. CODEC_ATTR_WO(reconfig),
  358. CODEC_ATTR_WO(clear),
  359. };
  360. /*
  361. * create sysfs files on hwdep directory
  362. */
  363. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  364. {
  365. struct snd_hwdep *hwdep = codec->hwdep;
  366. int i;
  367. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  368. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  369. hwdep->device, &codec_attrs[i]);
  370. return 0;
  371. }
  372. #endif /* CONFIG_SND_HDA_RECONFIG */