PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/pci/hda/hda_hwdep.c

https://github.com/concer/linux-2.6
C | 555 lines | 453 code | 58 blank | 44 comment | 53 complexity | 63466a909b15fb04b49b0398200930f5 MD5 | raw file
  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(vendor_name);
  217. CODEC_INFO_STR_SHOW(chip_name);
  218. CODEC_INFO_STR_SHOW(modelname);
  219. #define CODEC_INFO_STORE(type) \
  220. static ssize_t type##_store(struct device *dev, \
  221. struct device_attribute *attr, \
  222. const char *buf, size_t count) \
  223. { \
  224. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  225. struct hda_codec *codec = hwdep->private_data; \
  226. char *after; \
  227. codec->type = simple_strtoul(buf, &after, 0); \
  228. return count; \
  229. }
  230. #define CODEC_INFO_STR_STORE(type) \
  231. static ssize_t type##_store(struct device *dev, \
  232. struct device_attribute *attr, \
  233. const char *buf, size_t count) \
  234. { \
  235. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  236. struct hda_codec *codec = hwdep->private_data; \
  237. char *s = kstrndup_noeol(buf, 64); \
  238. if (!s) \
  239. return -ENOMEM; \
  240. kfree(codec->type); \
  241. codec->type = s; \
  242. return count; \
  243. }
  244. CODEC_INFO_STORE(vendor_id);
  245. CODEC_INFO_STORE(subsystem_id);
  246. CODEC_INFO_STORE(revision_id);
  247. CODEC_INFO_STR_STORE(vendor_name);
  248. CODEC_INFO_STR_STORE(chip_name);
  249. CODEC_INFO_STR_STORE(modelname);
  250. #define CODEC_ACTION_STORE(type) \
  251. static ssize_t type##_store(struct device *dev, \
  252. struct device_attribute *attr, \
  253. const char *buf, size_t count) \
  254. { \
  255. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  256. struct hda_codec *codec = hwdep->private_data; \
  257. int err = 0; \
  258. if (*buf) \
  259. err = type##_codec(codec); \
  260. return err < 0 ? err : count; \
  261. }
  262. CODEC_ACTION_STORE(reconfig);
  263. CODEC_ACTION_STORE(clear);
  264. static ssize_t init_verbs_show(struct device *dev,
  265. struct device_attribute *attr,
  266. char *buf)
  267. {
  268. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  269. struct hda_codec *codec = hwdep->private_data;
  270. int i, len = 0;
  271. for (i = 0; i < codec->init_verbs.used; i++) {
  272. struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
  273. len += snprintf(buf + len, PAGE_SIZE - len,
  274. "0x%02x 0x%03x 0x%04x\n",
  275. v->nid, v->verb, v->param);
  276. }
  277. return len;
  278. }
  279. static ssize_t init_verbs_store(struct device *dev,
  280. struct device_attribute *attr,
  281. const char *buf, size_t count)
  282. {
  283. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  284. struct hda_codec *codec = hwdep->private_data;
  285. struct hda_verb *v;
  286. int nid, verb, param;
  287. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  288. return -EINVAL;
  289. if (!nid || !verb)
  290. return -EINVAL;
  291. v = snd_array_new(&codec->init_verbs);
  292. if (!v)
  293. return -ENOMEM;
  294. v->nid = nid;
  295. v->verb = verb;
  296. v->param = param;
  297. return count;
  298. }
  299. static ssize_t hints_show(struct device *dev,
  300. struct device_attribute *attr,
  301. char *buf)
  302. {
  303. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  304. struct hda_codec *codec = hwdep->private_data;
  305. int i, len = 0;
  306. for (i = 0; i < codec->hints.used; i++) {
  307. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  308. len += snprintf(buf + len, PAGE_SIZE - len,
  309. "%s = %s\n", hint->key, hint->val);
  310. }
  311. return len;
  312. }
  313. static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
  314. {
  315. int i;
  316. for (i = 0; i < codec->hints.used; i++) {
  317. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  318. if (!strcmp(hint->key, key))
  319. return hint;
  320. }
  321. return NULL;
  322. }
  323. static void remove_trail_spaces(char *str)
  324. {
  325. char *p;
  326. if (!*str)
  327. return;
  328. p = str + strlen(str) - 1;
  329. for (; isspace(*p); p--) {
  330. *p = 0;
  331. if (p == str)
  332. return;
  333. }
  334. }
  335. #define MAX_HINTS 1024
  336. static ssize_t hints_store(struct device *dev,
  337. struct device_attribute *attr,
  338. const char *buf, size_t count)
  339. {
  340. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  341. struct hda_codec *codec = hwdep->private_data;
  342. char *key, *val;
  343. struct hda_hint *hint;
  344. while (isspace(*buf))
  345. buf++;
  346. if (!*buf || *buf == '#' || *buf == '\n')
  347. return count;
  348. if (*buf == '=')
  349. return -EINVAL;
  350. key = kstrndup_noeol(buf, 1024);
  351. if (!key)
  352. return -ENOMEM;
  353. /* extract key and val */
  354. val = strchr(key, '=');
  355. if (!val) {
  356. kfree(key);
  357. return -EINVAL;
  358. }
  359. *val++ = 0;
  360. while (isspace(*val))
  361. val++;
  362. remove_trail_spaces(key);
  363. remove_trail_spaces(val);
  364. hint = get_hint(codec, key);
  365. if (hint) {
  366. /* replace */
  367. kfree(hint->key);
  368. hint->key = key;
  369. hint->val = val;
  370. return count;
  371. }
  372. /* allocate a new hint entry */
  373. if (codec->hints.used >= MAX_HINTS)
  374. hint = NULL;
  375. else
  376. hint = snd_array_new(&codec->hints);
  377. if (!hint) {
  378. kfree(key);
  379. return -ENOMEM;
  380. }
  381. hint->key = key;
  382. hint->val = val;
  383. return count;
  384. }
  385. static ssize_t pin_configs_show(struct hda_codec *codec,
  386. struct snd_array *list,
  387. char *buf)
  388. {
  389. int i, len = 0;
  390. for (i = 0; i < list->used; i++) {
  391. struct hda_pincfg *pin = snd_array_elem(list, i);
  392. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  393. pin->nid, pin->cfg);
  394. }
  395. return len;
  396. }
  397. static ssize_t init_pin_configs_show(struct device *dev,
  398. struct device_attribute *attr,
  399. char *buf)
  400. {
  401. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  402. struct hda_codec *codec = hwdep->private_data;
  403. return pin_configs_show(codec, &codec->init_pins, buf);
  404. }
  405. static ssize_t user_pin_configs_show(struct device *dev,
  406. struct device_attribute *attr,
  407. char *buf)
  408. {
  409. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  410. struct hda_codec *codec = hwdep->private_data;
  411. return pin_configs_show(codec, &codec->user_pins, buf);
  412. }
  413. static ssize_t driver_pin_configs_show(struct device *dev,
  414. struct device_attribute *attr,
  415. char *buf)
  416. {
  417. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  418. struct hda_codec *codec = hwdep->private_data;
  419. return pin_configs_show(codec, &codec->driver_pins, buf);
  420. }
  421. #define MAX_PIN_CONFIGS 32
  422. static ssize_t user_pin_configs_store(struct device *dev,
  423. struct device_attribute *attr,
  424. const char *buf, size_t count)
  425. {
  426. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  427. struct hda_codec *codec = hwdep->private_data;
  428. int nid, cfg;
  429. int err;
  430. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  431. return -EINVAL;
  432. if (!nid)
  433. return -EINVAL;
  434. err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  435. if (err < 0)
  436. return err;
  437. return count;
  438. }
  439. #define CODEC_ATTR_RW(type) \
  440. __ATTR(type, 0644, type##_show, type##_store)
  441. #define CODEC_ATTR_RO(type) \
  442. __ATTR_RO(type)
  443. #define CODEC_ATTR_WO(type) \
  444. __ATTR(type, 0200, NULL, type##_store)
  445. static struct device_attribute codec_attrs[] = {
  446. CODEC_ATTR_RW(vendor_id),
  447. CODEC_ATTR_RW(subsystem_id),
  448. CODEC_ATTR_RW(revision_id),
  449. CODEC_ATTR_RO(afg),
  450. CODEC_ATTR_RO(mfg),
  451. CODEC_ATTR_RW(vendor_name),
  452. CODEC_ATTR_RW(chip_name),
  453. CODEC_ATTR_RW(modelname),
  454. CODEC_ATTR_RW(init_verbs),
  455. CODEC_ATTR_RW(hints),
  456. CODEC_ATTR_RO(init_pin_configs),
  457. CODEC_ATTR_RW(user_pin_configs),
  458. CODEC_ATTR_RO(driver_pin_configs),
  459. CODEC_ATTR_WO(reconfig),
  460. CODEC_ATTR_WO(clear),
  461. };
  462. /*
  463. * create sysfs files on hwdep directory
  464. */
  465. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  466. {
  467. struct snd_hwdep *hwdep = codec->hwdep;
  468. int i;
  469. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  470. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  471. hwdep->device, &codec_attrs[i]);
  472. return 0;
  473. }
  474. /*
  475. * Look for hint string
  476. */
  477. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  478. {
  479. struct hda_hint *hint = get_hint(codec, key);
  480. return hint ? hint->val : NULL;
  481. }
  482. EXPORT_SYMBOL_HDA(snd_hda_get_hint);
  483. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  484. {
  485. const char *p = snd_hda_get_hint(codec, key);
  486. if (!p || !*p)
  487. return -ENOENT;
  488. switch (toupper(*p)) {
  489. case 'T': /* true */
  490. case 'Y': /* yes */
  491. case '1':
  492. return 1;
  493. }
  494. return 0;
  495. }
  496. EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
  497. #endif /* CONFIG_SND_HDA_RECONFIG */