hda_hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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_hda_power_up(codec);
  155. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  156. err = snd_hda_codec_reset(codec);
  157. if (err < 0) {
  158. snd_printk(KERN_ERR
  159. "The codec is being used, can't reconfigure.\n");
  160. goto error;
  161. }
  162. err = snd_hda_codec_configure(codec);
  163. if (err < 0)
  164. goto error;
  165. /* rebuild PCMs */
  166. err = snd_hda_codec_build_pcms(codec);
  167. if (err < 0)
  168. goto error;
  169. /* rebuild mixers */
  170. err = snd_hda_codec_build_controls(codec);
  171. if (err < 0)
  172. goto error;
  173. err = snd_card_register(codec->bus->card);
  174. error:
  175. snd_hda_power_down(codec);
  176. return err;
  177. }
  178. /*
  179. * allocate a string at most len chars, and remove the trailing EOL
  180. */
  181. static char *kstrndup_noeol(const char *src, size_t len)
  182. {
  183. char *s = kstrndup(src, len, GFP_KERNEL);
  184. char *p;
  185. if (!s)
  186. return NULL;
  187. p = strchr(s, '\n');
  188. if (p)
  189. *p = 0;
  190. return s;
  191. }
  192. #define CODEC_INFO_SHOW(type) \
  193. static ssize_t type##_show(struct device *dev, \
  194. struct device_attribute *attr, \
  195. char *buf) \
  196. { \
  197. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  198. struct hda_codec *codec = hwdep->private_data; \
  199. return sprintf(buf, "0x%x\n", codec->type); \
  200. }
  201. #define CODEC_INFO_STR_SHOW(type) \
  202. static ssize_t type##_show(struct device *dev, \
  203. struct device_attribute *attr, \
  204. char *buf) \
  205. { \
  206. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  207. struct hda_codec *codec = hwdep->private_data; \
  208. return sprintf(buf, "%s\n", \
  209. codec->type ? codec->type : ""); \
  210. }
  211. CODEC_INFO_SHOW(vendor_id);
  212. CODEC_INFO_SHOW(subsystem_id);
  213. CODEC_INFO_SHOW(revision_id);
  214. CODEC_INFO_SHOW(afg);
  215. CODEC_INFO_SHOW(mfg);
  216. CODEC_INFO_STR_SHOW(name);
  217. CODEC_INFO_STR_SHOW(modelname);
  218. #define CODEC_INFO_STORE(type) \
  219. static ssize_t type##_store(struct device *dev, \
  220. struct device_attribute *attr, \
  221. const char *buf, size_t count) \
  222. { \
  223. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  224. struct hda_codec *codec = hwdep->private_data; \
  225. char *after; \
  226. codec->type = simple_strtoul(buf, &after, 0); \
  227. return count; \
  228. }
  229. #define CODEC_INFO_STR_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. char *s = kstrndup_noeol(buf, 64); \
  237. if (!s) \
  238. return -ENOMEM; \
  239. kfree(codec->type); \
  240. codec->type = s; \
  241. return count; \
  242. }
  243. CODEC_INFO_STORE(vendor_id);
  244. CODEC_INFO_STORE(subsystem_id);
  245. CODEC_INFO_STORE(revision_id);
  246. CODEC_INFO_STR_STORE(name);
  247. CODEC_INFO_STR_STORE(modelname);
  248. #define CODEC_ACTION_STORE(type) \
  249. static ssize_t type##_store(struct device *dev, \
  250. struct device_attribute *attr, \
  251. const char *buf, size_t count) \
  252. { \
  253. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  254. struct hda_codec *codec = hwdep->private_data; \
  255. int err = 0; \
  256. if (*buf) \
  257. err = type##_codec(codec); \
  258. return err < 0 ? err : count; \
  259. }
  260. CODEC_ACTION_STORE(reconfig);
  261. CODEC_ACTION_STORE(clear);
  262. static ssize_t init_verbs_show(struct device *dev,
  263. struct device_attribute *attr,
  264. char *buf)
  265. {
  266. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  267. struct hda_codec *codec = hwdep->private_data;
  268. int i, len = 0;
  269. for (i = 0; i < codec->init_verbs.used; i++) {
  270. struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
  271. len += snprintf(buf + len, PAGE_SIZE - len,
  272. "0x%02x 0x%03x 0x%04x\n",
  273. v->nid, v->verb, v->param);
  274. }
  275. return len;
  276. }
  277. static ssize_t init_verbs_store(struct device *dev,
  278. struct device_attribute *attr,
  279. const char *buf, size_t count)
  280. {
  281. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  282. struct hda_codec *codec = hwdep->private_data;
  283. struct hda_verb *v;
  284. int nid, verb, param;
  285. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  286. return -EINVAL;
  287. if (!nid || !verb)
  288. return -EINVAL;
  289. v = snd_array_new(&codec->init_verbs);
  290. if (!v)
  291. return -ENOMEM;
  292. v->nid = nid;
  293. v->verb = verb;
  294. v->param = param;
  295. return count;
  296. }
  297. static ssize_t hints_show(struct device *dev,
  298. struct device_attribute *attr,
  299. char *buf)
  300. {
  301. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  302. struct hda_codec *codec = hwdep->private_data;
  303. int i, len = 0;
  304. for (i = 0; i < codec->hints.used; i++) {
  305. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  306. len += snprintf(buf + len, PAGE_SIZE - len,
  307. "%s = %s\n", hint->key, hint->val);
  308. }
  309. return len;
  310. }
  311. static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
  312. {
  313. int i;
  314. for (i = 0; i < codec->hints.used; i++) {
  315. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  316. if (!strcmp(hint->key, key))
  317. return hint;
  318. }
  319. return NULL;
  320. }
  321. static void remove_trail_spaces(char *str)
  322. {
  323. char *p;
  324. if (!*str)
  325. return;
  326. p = str + strlen(str) - 1;
  327. for (; isspace(*p); p--) {
  328. *p = 0;
  329. if (p == str)
  330. return;
  331. }
  332. }
  333. #define MAX_HINTS 1024
  334. static ssize_t hints_store(struct device *dev,
  335. struct device_attribute *attr,
  336. const char *buf, size_t count)
  337. {
  338. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  339. struct hda_codec *codec = hwdep->private_data;
  340. char *key, *val;
  341. struct hda_hint *hint;
  342. while (isspace(*buf))
  343. buf++;
  344. if (!*buf || *buf == '#' || *buf == '\n')
  345. return count;
  346. if (*buf == '=')
  347. return -EINVAL;
  348. key = kstrndup_noeol(buf, 1024);
  349. if (!key)
  350. return -ENOMEM;
  351. /* extract key and val */
  352. val = strchr(key, '=');
  353. if (!val) {
  354. kfree(key);
  355. return -EINVAL;
  356. }
  357. *val++ = 0;
  358. while (isspace(*val))
  359. val++;
  360. remove_trail_spaces(key);
  361. remove_trail_spaces(val);
  362. hint = get_hint(codec, key);
  363. if (hint) {
  364. /* replace */
  365. kfree(hint->key);
  366. hint->key = key;
  367. hint->val = val;
  368. return count;
  369. }
  370. /* allocate a new hint entry */
  371. if (codec->hints.used >= MAX_HINTS)
  372. hint = NULL;
  373. else
  374. hint = snd_array_new(&codec->hints);
  375. if (!hint) {
  376. kfree(key);
  377. return -ENOMEM;
  378. }
  379. hint->key = key;
  380. hint->val = val;
  381. return count;
  382. }
  383. static ssize_t pin_configs_show(struct hda_codec *codec,
  384. struct snd_array *list,
  385. char *buf)
  386. {
  387. int i, len = 0;
  388. for (i = 0; i < list->used; i++) {
  389. struct hda_pincfg *pin = snd_array_elem(list, i);
  390. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  391. pin->nid, pin->cfg);
  392. }
  393. return len;
  394. }
  395. static ssize_t init_pin_configs_show(struct device *dev,
  396. struct device_attribute *attr,
  397. char *buf)
  398. {
  399. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  400. struct hda_codec *codec = hwdep->private_data;
  401. return pin_configs_show(codec, &codec->init_pins, buf);
  402. }
  403. static ssize_t user_pin_configs_show(struct device *dev,
  404. struct device_attribute *attr,
  405. char *buf)
  406. {
  407. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  408. struct hda_codec *codec = hwdep->private_data;
  409. return pin_configs_show(codec, &codec->user_pins, buf);
  410. }
  411. static ssize_t driver_pin_configs_show(struct device *dev,
  412. struct device_attribute *attr,
  413. char *buf)
  414. {
  415. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  416. struct hda_codec *codec = hwdep->private_data;
  417. return pin_configs_show(codec, &codec->driver_pins, buf);
  418. }
  419. #define MAX_PIN_CONFIGS 32
  420. static ssize_t user_pin_configs_store(struct device *dev,
  421. struct device_attribute *attr,
  422. const char *buf, size_t count)
  423. {
  424. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  425. struct hda_codec *codec = hwdep->private_data;
  426. int nid, cfg;
  427. int err;
  428. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  429. return -EINVAL;
  430. if (!nid)
  431. return -EINVAL;
  432. err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  433. if (err < 0)
  434. return err;
  435. return count;
  436. }
  437. #define CODEC_ATTR_RW(type) \
  438. __ATTR(type, 0644, type##_show, type##_store)
  439. #define CODEC_ATTR_RO(type) \
  440. __ATTR_RO(type)
  441. #define CODEC_ATTR_WO(type) \
  442. __ATTR(type, 0200, NULL, type##_store)
  443. static struct device_attribute codec_attrs[] = {
  444. CODEC_ATTR_RW(vendor_id),
  445. CODEC_ATTR_RW(subsystem_id),
  446. CODEC_ATTR_RW(revision_id),
  447. CODEC_ATTR_RO(afg),
  448. CODEC_ATTR_RO(mfg),
  449. CODEC_ATTR_RW(name),
  450. CODEC_ATTR_RW(modelname),
  451. CODEC_ATTR_RW(init_verbs),
  452. CODEC_ATTR_RW(hints),
  453. CODEC_ATTR_RO(init_pin_configs),
  454. CODEC_ATTR_RW(user_pin_configs),
  455. CODEC_ATTR_RO(driver_pin_configs),
  456. CODEC_ATTR_WO(reconfig),
  457. CODEC_ATTR_WO(clear),
  458. };
  459. /*
  460. * create sysfs files on hwdep directory
  461. */
  462. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  463. {
  464. struct snd_hwdep *hwdep = codec->hwdep;
  465. int i;
  466. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  467. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  468. hwdep->device, &codec_attrs[i]);
  469. return 0;
  470. }
  471. /*
  472. * Look for hint string
  473. */
  474. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  475. {
  476. struct hda_hint *hint = get_hint(codec, key);
  477. return hint ? hint->val : NULL;
  478. }
  479. EXPORT_SYMBOL_HDA(snd_hda_get_hint);
  480. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  481. {
  482. const char *p = snd_hda_get_hint(codec, key);
  483. if (!p || !*p)
  484. return -ENOENT;
  485. switch (toupper(*p)) {
  486. case 'T': /* true */
  487. case 'Y': /* yes */
  488. case '1':
  489. return 1;
  490. }
  491. return 0;
  492. }
  493. EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
  494. #endif /* CONFIG_SND_HDA_RECONFIG */