hda_hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. /* hint string pair */
  32. struct hda_hint {
  33. const char *key;
  34. const char *val; /* contained in the same alloc as key */
  35. };
  36. /*
  37. * write/read an out-of-bound verb
  38. */
  39. static int verb_write_ioctl(struct hda_codec *codec,
  40. struct hda_verb_ioctl __user *arg)
  41. {
  42. u32 verb, res;
  43. if (get_user(verb, &arg->verb))
  44. return -EFAULT;
  45. res = snd_hda_codec_read(codec, verb >> 24, 0,
  46. (verb >> 8) & 0xffff, verb & 0xff);
  47. if (put_user(res, &arg->res))
  48. return -EFAULT;
  49. return 0;
  50. }
  51. static int get_wcap_ioctl(struct hda_codec *codec,
  52. struct hda_verb_ioctl __user *arg)
  53. {
  54. u32 verb, res;
  55. if (get_user(verb, &arg->verb))
  56. return -EFAULT;
  57. res = get_wcaps(codec, verb >> 24);
  58. if (put_user(res, &arg->res))
  59. return -EFAULT;
  60. return 0;
  61. }
  62. /*
  63. */
  64. static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  65. unsigned int cmd, unsigned long arg)
  66. {
  67. struct hda_codec *codec = hw->private_data;
  68. void __user *argp = (void __user *)arg;
  69. switch (cmd) {
  70. case HDA_IOCTL_PVERSION:
  71. return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
  72. case HDA_IOCTL_VERB_WRITE:
  73. return verb_write_ioctl(codec, argp);
  74. case HDA_IOCTL_GET_WCAP:
  75. return get_wcap_ioctl(codec, argp);
  76. }
  77. return -ENOIOCTLCMD;
  78. }
  79. #ifdef CONFIG_COMPAT
  80. static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
  81. unsigned int cmd, unsigned long arg)
  82. {
  83. return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
  84. }
  85. #endif
  86. static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
  87. {
  88. #ifndef CONFIG_SND_DEBUG_VERBOSE
  89. if (!capable(CAP_SYS_RAWIO))
  90. return -EACCES;
  91. #endif
  92. return 0;
  93. }
  94. static void clear_hwdep_elements(struct hda_codec *codec)
  95. {
  96. int i;
  97. /* clear init verbs */
  98. snd_array_free(&codec->init_verbs);
  99. /* clear hints */
  100. for (i = 0; i < codec->hints.used; i++) {
  101. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  102. kfree(hint->key); /* we don't need to free hint->val */
  103. }
  104. snd_array_free(&codec->hints);
  105. snd_array_free(&codec->user_pins);
  106. }
  107. static void hwdep_free(struct snd_hwdep *hwdep)
  108. {
  109. clear_hwdep_elements(hwdep->private_data);
  110. }
  111. int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
  112. {
  113. char hwname[16];
  114. struct snd_hwdep *hwdep;
  115. int err;
  116. sprintf(hwname, "HDA Codec %d", codec->addr);
  117. err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
  118. if (err < 0)
  119. return err;
  120. codec->hwdep = hwdep;
  121. sprintf(hwdep->name, "HDA Codec %d", codec->addr);
  122. hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
  123. hwdep->private_data = codec;
  124. hwdep->private_free = hwdep_free;
  125. hwdep->exclusive = 1;
  126. hwdep->ops.open = hda_hwdep_open;
  127. hwdep->ops.ioctl = hda_hwdep_ioctl;
  128. #ifdef CONFIG_COMPAT
  129. hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
  130. #endif
  131. snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
  132. snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
  133. snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
  134. return 0;
  135. }
  136. #ifdef CONFIG_SND_HDA_RECONFIG
  137. /*
  138. * sysfs interface
  139. */
  140. static int clear_codec(struct hda_codec *codec)
  141. {
  142. int err;
  143. err = snd_hda_codec_reset(codec);
  144. if (err < 0) {
  145. snd_printk(KERN_ERR "The codec is being used, can't free.\n");
  146. return err;
  147. }
  148. clear_hwdep_elements(codec);
  149. return 0;
  150. }
  151. static int reconfig_codec(struct hda_codec *codec)
  152. {
  153. int err;
  154. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  155. err = snd_hda_codec_reset(codec);
  156. if (err < 0) {
  157. snd_printk(KERN_ERR
  158. "The codec is being used, can't reconfigure.\n");
  159. return err;
  160. }
  161. err = snd_hda_codec_configure(codec);
  162. if (err < 0)
  163. return err;
  164. /* rebuild PCMs */
  165. err = snd_hda_codec_build_pcms(codec);
  166. if (err < 0)
  167. return err;
  168. /* rebuild mixers */
  169. err = snd_hda_codec_build_controls(codec);
  170. if (err < 0)
  171. return err;
  172. return snd_card_register(codec->bus->card);
  173. }
  174. /*
  175. * allocate a string at most len chars, and remove the trailing EOL
  176. */
  177. static char *kstrndup_noeol(const char *src, size_t len)
  178. {
  179. char *s = kstrndup(src, len, GFP_KERNEL);
  180. char *p;
  181. if (!s)
  182. return NULL;
  183. p = strchr(s, '\n');
  184. if (p)
  185. *p = 0;
  186. return s;
  187. }
  188. #define CODEC_INFO_SHOW(type) \
  189. static ssize_t type##_show(struct device *dev, \
  190. struct device_attribute *attr, \
  191. char *buf) \
  192. { \
  193. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  194. struct hda_codec *codec = hwdep->private_data; \
  195. return sprintf(buf, "0x%x\n", codec->type); \
  196. }
  197. #define CODEC_INFO_STR_SHOW(type) \
  198. static ssize_t type##_show(struct device *dev, \
  199. struct device_attribute *attr, \
  200. char *buf) \
  201. { \
  202. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  203. struct hda_codec *codec = hwdep->private_data; \
  204. return sprintf(buf, "%s\n", \
  205. codec->type ? codec->type : ""); \
  206. }
  207. CODEC_INFO_SHOW(vendor_id);
  208. CODEC_INFO_SHOW(subsystem_id);
  209. CODEC_INFO_SHOW(revision_id);
  210. CODEC_INFO_SHOW(afg);
  211. CODEC_INFO_SHOW(mfg);
  212. CODEC_INFO_STR_SHOW(name);
  213. CODEC_INFO_STR_SHOW(modelname);
  214. #define CODEC_INFO_STORE(type) \
  215. static ssize_t type##_store(struct device *dev, \
  216. struct device_attribute *attr, \
  217. const char *buf, size_t count) \
  218. { \
  219. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  220. struct hda_codec *codec = hwdep->private_data; \
  221. char *after; \
  222. codec->type = simple_strtoul(buf, &after, 0); \
  223. return count; \
  224. }
  225. #define CODEC_INFO_STR_STORE(type) \
  226. static ssize_t type##_store(struct device *dev, \
  227. struct device_attribute *attr, \
  228. const char *buf, size_t count) \
  229. { \
  230. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  231. struct hda_codec *codec = hwdep->private_data; \
  232. char *s = kstrndup_noeol(buf, 64); \
  233. if (!s) \
  234. return -ENOMEM; \
  235. kfree(codec->type); \
  236. codec->type = s; \
  237. return count; \
  238. }
  239. CODEC_INFO_STORE(vendor_id);
  240. CODEC_INFO_STORE(subsystem_id);
  241. CODEC_INFO_STORE(revision_id);
  242. CODEC_INFO_STR_STORE(name);
  243. CODEC_INFO_STR_STORE(modelname);
  244. #define CODEC_ACTION_STORE(type) \
  245. static ssize_t type##_store(struct device *dev, \
  246. struct device_attribute *attr, \
  247. const char *buf, size_t count) \
  248. { \
  249. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  250. struct hda_codec *codec = hwdep->private_data; \
  251. int err = 0; \
  252. if (*buf) \
  253. err = type##_codec(codec); \
  254. return err < 0 ? err : count; \
  255. }
  256. CODEC_ACTION_STORE(reconfig);
  257. CODEC_ACTION_STORE(clear);
  258. static ssize_t init_verbs_show(struct device *dev,
  259. struct device_attribute *attr,
  260. char *buf)
  261. {
  262. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  263. struct hda_codec *codec = hwdep->private_data;
  264. int i, len = 0;
  265. for (i = 0; i < codec->init_verbs.used; i++) {
  266. struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
  267. len += snprintf(buf + len, PAGE_SIZE - len,
  268. "0x%02x 0x%03x 0x%04x\n",
  269. v->nid, v->verb, v->param);
  270. }
  271. return len;
  272. }
  273. static ssize_t init_verbs_store(struct device *dev,
  274. struct device_attribute *attr,
  275. const char *buf, size_t count)
  276. {
  277. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  278. struct hda_codec *codec = hwdep->private_data;
  279. struct hda_verb *v;
  280. int nid, verb, param;
  281. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  282. return -EINVAL;
  283. if (!nid || !verb)
  284. return -EINVAL;
  285. v = snd_array_new(&codec->init_verbs);
  286. if (!v)
  287. return -ENOMEM;
  288. v->nid = nid;
  289. v->verb = verb;
  290. v->param = param;
  291. return count;
  292. }
  293. static ssize_t hints_show(struct device *dev,
  294. struct device_attribute *attr,
  295. char *buf)
  296. {
  297. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  298. struct hda_codec *codec = hwdep->private_data;
  299. int i, len = 0;
  300. for (i = 0; i < codec->hints.used; i++) {
  301. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  302. len += snprintf(buf + len, PAGE_SIZE - len,
  303. "%s = %s\n", hint->key, hint->val);
  304. }
  305. return len;
  306. }
  307. static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
  308. {
  309. int i;
  310. for (i = 0; i < codec->hints.used; i++) {
  311. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  312. if (!strcmp(hint->key, key))
  313. return hint;
  314. }
  315. return NULL;
  316. }
  317. static void remove_trail_spaces(char *str)
  318. {
  319. char *p;
  320. if (!*str)
  321. return;
  322. p = str + strlen(str) - 1;
  323. for (; isspace(*p); p--) {
  324. *p = 0;
  325. if (p == str)
  326. return;
  327. }
  328. }
  329. #define MAX_HINTS 1024
  330. static ssize_t hints_store(struct device *dev,
  331. struct device_attribute *attr,
  332. const char *buf, size_t count)
  333. {
  334. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  335. struct hda_codec *codec = hwdep->private_data;
  336. char *key, *val;
  337. struct hda_hint *hint;
  338. while (isspace(*buf))
  339. buf++;
  340. if (!*buf || *buf == '#' || *buf == '\n')
  341. return count;
  342. if (*buf == '=')
  343. return -EINVAL;
  344. key = kstrndup_noeol(buf, 1024);
  345. if (!key)
  346. return -ENOMEM;
  347. /* extract key and val */
  348. val = strchr(key, '=');
  349. if (!val) {
  350. kfree(key);
  351. return -EINVAL;
  352. }
  353. *val++ = 0;
  354. while (isspace(*val))
  355. val++;
  356. remove_trail_spaces(key);
  357. remove_trail_spaces(val);
  358. hint = get_hint(codec, key);
  359. if (hint) {
  360. /* replace */
  361. kfree(hint->key);
  362. hint->key = key;
  363. hint->val = val;
  364. return count;
  365. }
  366. /* allocate a new hint entry */
  367. if (codec->hints.used >= MAX_HINTS)
  368. hint = NULL;
  369. else
  370. hint = snd_array_new(&codec->hints);
  371. if (!hint) {
  372. kfree(key);
  373. return -ENOMEM;
  374. }
  375. hint->key = key;
  376. hint->val = val;
  377. return count;
  378. }
  379. static ssize_t pin_configs_show(struct hda_codec *codec,
  380. struct snd_array *list,
  381. char *buf)
  382. {
  383. int i, len = 0;
  384. for (i = 0; i < list->used; i++) {
  385. struct hda_pincfg *pin = snd_array_elem(list, i);
  386. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  387. pin->nid, pin->cfg);
  388. }
  389. return len;
  390. }
  391. static ssize_t init_pin_configs_show(struct device *dev,
  392. struct device_attribute *attr,
  393. char *buf)
  394. {
  395. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  396. struct hda_codec *codec = hwdep->private_data;
  397. return pin_configs_show(codec, &codec->init_pins, buf);
  398. }
  399. static ssize_t user_pin_configs_show(struct device *dev,
  400. struct device_attribute *attr,
  401. char *buf)
  402. {
  403. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  404. struct hda_codec *codec = hwdep->private_data;
  405. return pin_configs_show(codec, &codec->user_pins, buf);
  406. }
  407. static ssize_t driver_pin_configs_show(struct device *dev,
  408. struct device_attribute *attr,
  409. char *buf)
  410. {
  411. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  412. struct hda_codec *codec = hwdep->private_data;
  413. return pin_configs_show(codec, &codec->driver_pins, buf);
  414. }
  415. #define MAX_PIN_CONFIGS 32
  416. static ssize_t user_pin_configs_store(struct device *dev,
  417. struct device_attribute *attr,
  418. const char *buf, size_t count)
  419. {
  420. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  421. struct hda_codec *codec = hwdep->private_data;
  422. int nid, cfg;
  423. int err;
  424. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  425. return -EINVAL;
  426. if (!nid)
  427. return -EINVAL;
  428. err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  429. if (err < 0)
  430. return err;
  431. return count;
  432. }
  433. #define CODEC_ATTR_RW(type) \
  434. __ATTR(type, 0644, type##_show, type##_store)
  435. #define CODEC_ATTR_RO(type) \
  436. __ATTR_RO(type)
  437. #define CODEC_ATTR_WO(type) \
  438. __ATTR(type, 0200, NULL, type##_store)
  439. static struct device_attribute codec_attrs[] = {
  440. CODEC_ATTR_RW(vendor_id),
  441. CODEC_ATTR_RW(subsystem_id),
  442. CODEC_ATTR_RW(revision_id),
  443. CODEC_ATTR_RO(afg),
  444. CODEC_ATTR_RO(mfg),
  445. CODEC_ATTR_RW(name),
  446. CODEC_ATTR_RW(modelname),
  447. CODEC_ATTR_RW(init_verbs),
  448. CODEC_ATTR_RW(hints),
  449. CODEC_ATTR_RO(init_pin_configs),
  450. CODEC_ATTR_RW(user_pin_configs),
  451. CODEC_ATTR_RO(driver_pin_configs),
  452. CODEC_ATTR_WO(reconfig),
  453. CODEC_ATTR_WO(clear),
  454. };
  455. /*
  456. * create sysfs files on hwdep directory
  457. */
  458. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  459. {
  460. struct snd_hwdep *hwdep = codec->hwdep;
  461. int i;
  462. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  463. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  464. hwdep->device, &codec_attrs[i]);
  465. return 0;
  466. }
  467. /*
  468. * Look for hint string
  469. */
  470. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  471. {
  472. struct hda_hint *hint = get_hint(codec, key);
  473. return hint ? hint->val : NULL;
  474. }
  475. EXPORT_SYMBOL_HDA(snd_hda_get_hint);
  476. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  477. {
  478. const char *p = snd_hda_get_hint(codec, key);
  479. if (!p || !*p)
  480. return -ENOENT;
  481. switch (toupper(*p)) {
  482. case 'T': /* true */
  483. case 'Y': /* yes */
  484. case '1':
  485. return 1;
  486. }
  487. return 0;
  488. }
  489. EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
  490. #endif /* CONFIG_SND_HDA_RECONFIG */