hda_hwdep.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/compat.h>
  23. #include <linux/nospec.h>
  24. #include <sound/core.h>
  25. #include "hda_codec.h"
  26. #include "hda_local.h"
  27. #include <sound/hda_hwdep.h>
  28. #include <sound/minors.h>
  29. /*
  30. * write/read an out-of-bound verb
  31. */
  32. static int verb_write_ioctl(struct hda_codec *codec,
  33. struct hda_verb_ioctl __user *arg)
  34. {
  35. u32 verb, res;
  36. if (get_user(verb, &arg->verb))
  37. return -EFAULT;
  38. res = snd_hda_codec_read(codec, verb >> 24, 0,
  39. (verb >> 8) & 0xffff, verb & 0xff);
  40. if (put_user(res, &arg->res))
  41. return -EFAULT;
  42. return 0;
  43. }
  44. static int get_wcap_ioctl(struct hda_codec *codec,
  45. struct hda_verb_ioctl __user *arg)
  46. {
  47. u32 verb, res;
  48. if (get_user(verb, &arg->verb))
  49. return -EFAULT;
  50. /* open-code get_wcaps(verb>>24) with nospec */
  51. verb >>= 24;
  52. if (verb < codec->core.start_nid ||
  53. verb >= codec->core.start_nid + codec->core.num_nodes) {
  54. res = 0;
  55. } else {
  56. verb -= codec->core.start_nid;
  57. verb = array_index_nospec(verb, codec->core.num_nodes);
  58. res = codec->wcaps[verb];
  59. }
  60. if (put_user(res, &arg->res))
  61. return -EFAULT;
  62. return 0;
  63. }
  64. /*
  65. */
  66. static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  67. unsigned int cmd, unsigned long arg)
  68. {
  69. struct hda_codec *codec = hw->private_data;
  70. void __user *argp = (void __user *)arg;
  71. switch (cmd) {
  72. case HDA_IOCTL_PVERSION:
  73. return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
  74. case HDA_IOCTL_VERB_WRITE:
  75. return verb_write_ioctl(codec, argp);
  76. case HDA_IOCTL_GET_WCAP:
  77. return get_wcap_ioctl(codec, argp);
  78. }
  79. return -ENOIOCTLCMD;
  80. }
  81. #ifdef CONFIG_COMPAT
  82. static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
  83. unsigned int cmd, unsigned long arg)
  84. {
  85. return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
  86. }
  87. #endif
  88. static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
  89. {
  90. #ifndef CONFIG_SND_DEBUG_VERBOSE
  91. if (!capable(CAP_SYS_RAWIO))
  92. return -EACCES;
  93. #endif
  94. return 0;
  95. }
  96. int snd_hda_create_hwdep(struct hda_codec *codec)
  97. {
  98. char hwname[16];
  99. struct snd_hwdep *hwdep;
  100. int err;
  101. sprintf(hwname, "HDA Codec %d", codec->addr);
  102. err = snd_hwdep_new(codec->card, hwname, codec->addr, &hwdep);
  103. if (err < 0)
  104. return err;
  105. codec->hwdep = hwdep;
  106. sprintf(hwdep->name, "HDA Codec %d", codec->addr);
  107. hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
  108. hwdep->private_data = codec;
  109. hwdep->exclusive = 1;
  110. hwdep->ops.open = hda_hwdep_open;
  111. hwdep->ops.ioctl = hda_hwdep_ioctl;
  112. #ifdef CONFIG_COMPAT
  113. hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
  114. #endif
  115. /* for sysfs */
  116. hwdep->dev.groups = snd_hda_dev_attr_groups;
  117. dev_set_drvdata(&hwdep->dev, codec);
  118. return 0;
  119. }