hda_hwdep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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_store(struct device *dev,
  259. struct device_attribute *attr,
  260. const char *buf, size_t count)
  261. {
  262. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  263. struct hda_codec *codec = hwdep->private_data;
  264. struct hda_verb *v;
  265. int nid, verb, param;
  266. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  267. return -EINVAL;
  268. if (!nid || !verb)
  269. return -EINVAL;
  270. v = snd_array_new(&codec->init_verbs);
  271. if (!v)
  272. return -ENOMEM;
  273. v->nid = nid;
  274. v->verb = verb;
  275. v->param = param;
  276. return count;
  277. }
  278. static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
  279. {
  280. int i;
  281. for (i = 0; i < codec->hints.used; i++) {
  282. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  283. if (!strcmp(hint->key, key))
  284. return hint;
  285. }
  286. return NULL;
  287. }
  288. static void remove_trail_spaces(char *str)
  289. {
  290. char *p;
  291. if (!*str)
  292. return;
  293. p = str + strlen(str) - 1;
  294. for (; isspace(*p); p--) {
  295. *p = 0;
  296. if (p == str)
  297. return;
  298. }
  299. }
  300. #define MAX_HINTS 1024
  301. static ssize_t hints_store(struct device *dev,
  302. struct device_attribute *attr,
  303. const char *buf, size_t count)
  304. {
  305. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  306. struct hda_codec *codec = hwdep->private_data;
  307. char *key, *val;
  308. struct hda_hint *hint;
  309. while (isspace(*buf))
  310. buf++;
  311. if (!*buf || *buf == '#' || *buf == '\n')
  312. return count;
  313. if (*buf == '=')
  314. return -EINVAL;
  315. key = kstrndup_noeol(buf, 1024);
  316. if (!key)
  317. return -ENOMEM;
  318. /* extract key and val */
  319. val = strchr(key, '=');
  320. if (!val) {
  321. kfree(key);
  322. return -EINVAL;
  323. }
  324. *val++ = 0;
  325. while (isspace(*val))
  326. val++;
  327. remove_trail_spaces(key);
  328. remove_trail_spaces(val);
  329. hint = get_hint(codec, key);
  330. if (hint) {
  331. /* replace */
  332. kfree(hint->key);
  333. hint->key = key;
  334. hint->val = val;
  335. return count;
  336. }
  337. /* allocate a new hint entry */
  338. if (codec->hints.used >= MAX_HINTS)
  339. hint = NULL;
  340. else
  341. hint = snd_array_new(&codec->hints);
  342. if (!hint) {
  343. kfree(key);
  344. return -ENOMEM;
  345. }
  346. hint->key = key;
  347. hint->val = val;
  348. return count;
  349. }
  350. static ssize_t pin_configs_show(struct hda_codec *codec,
  351. struct snd_array *list,
  352. char *buf)
  353. {
  354. int i, len = 0;
  355. for (i = 0; i < list->used; i++) {
  356. struct hda_pincfg *pin = snd_array_elem(list, i);
  357. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  358. pin->nid, pin->cfg);
  359. }
  360. return len;
  361. }
  362. static ssize_t init_pin_configs_show(struct device *dev,
  363. struct device_attribute *attr,
  364. char *buf)
  365. {
  366. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  367. struct hda_codec *codec = hwdep->private_data;
  368. return pin_configs_show(codec, &codec->init_pins, buf);
  369. }
  370. static ssize_t user_pin_configs_show(struct device *dev,
  371. struct device_attribute *attr,
  372. char *buf)
  373. {
  374. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  375. struct hda_codec *codec = hwdep->private_data;
  376. return pin_configs_show(codec, &codec->user_pins, buf);
  377. }
  378. static ssize_t driver_pin_configs_show(struct device *dev,
  379. struct device_attribute *attr,
  380. char *buf)
  381. {
  382. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  383. struct hda_codec *codec = hwdep->private_data;
  384. return pin_configs_show(codec, &codec->driver_pins, buf);
  385. }
  386. #define MAX_PIN_CONFIGS 32
  387. static ssize_t user_pin_configs_store(struct device *dev,
  388. struct device_attribute *attr,
  389. const char *buf, size_t count)
  390. {
  391. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  392. struct hda_codec *codec = hwdep->private_data;
  393. int nid, cfg;
  394. int err;
  395. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  396. return -EINVAL;
  397. if (!nid)
  398. return -EINVAL;
  399. err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  400. if (err < 0)
  401. return err;
  402. return count;
  403. }
  404. #define CODEC_ATTR_RW(type) \
  405. __ATTR(type, 0644, type##_show, type##_store)
  406. #define CODEC_ATTR_RO(type) \
  407. __ATTR_RO(type)
  408. #define CODEC_ATTR_WO(type) \
  409. __ATTR(type, 0200, NULL, type##_store)
  410. static struct device_attribute codec_attrs[] = {
  411. CODEC_ATTR_RW(vendor_id),
  412. CODEC_ATTR_RW(subsystem_id),
  413. CODEC_ATTR_RW(revision_id),
  414. CODEC_ATTR_RO(afg),
  415. CODEC_ATTR_RO(mfg),
  416. CODEC_ATTR_RW(name),
  417. CODEC_ATTR_RW(modelname),
  418. CODEC_ATTR_WO(init_verbs),
  419. CODEC_ATTR_WO(hints),
  420. CODEC_ATTR_RO(init_pin_configs),
  421. CODEC_ATTR_RW(user_pin_configs),
  422. CODEC_ATTR_RO(driver_pin_configs),
  423. CODEC_ATTR_WO(reconfig),
  424. CODEC_ATTR_WO(clear),
  425. };
  426. /*
  427. * create sysfs files on hwdep directory
  428. */
  429. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  430. {
  431. struct snd_hwdep *hwdep = codec->hwdep;
  432. int i;
  433. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  434. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  435. hwdep->device, &codec_attrs[i]);
  436. return 0;
  437. }
  438. /*
  439. * Look for hint string
  440. */
  441. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  442. {
  443. struct hda_hint *hint = get_hint(codec, key);
  444. return hint ? hint->val : NULL;
  445. }
  446. EXPORT_SYMBOL_HDA(snd_hda_get_hint);
  447. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  448. {
  449. const char *p = snd_hda_get_hint(codec, key);
  450. if (!p || !*p)
  451. return -ENOENT;
  452. switch (toupper(*p)) {
  453. case 'T': /* true */
  454. case 'Y': /* yes */
  455. case '1':
  456. return 1;
  457. }
  458. return 0;
  459. }
  460. EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
  461. #endif /* CONFIG_SND_HDA_RECONFIG */