control.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #ifndef __SOUND_CONTROL_H
  2. #define __SOUND_CONTROL_H
  3. /*
  4. * Header file for control interface
  5. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/wait.h>
  24. #include <linux/nospec.h>
  25. #include <sound/asound.h>
  26. #define snd_kcontrol_chip(kcontrol) ((kcontrol)->private_data)
  27. struct snd_kcontrol;
  28. typedef int (snd_kcontrol_info_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_info * uinfo);
  29. typedef int (snd_kcontrol_get_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
  30. typedef int (snd_kcontrol_put_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
  31. typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,
  32. int op_flag, /* SNDRV_CTL_TLV_OP_XXX */
  33. unsigned int size,
  34. unsigned int __user *tlv);
  35. enum {
  36. SNDRV_CTL_TLV_OP_READ = 0,
  37. SNDRV_CTL_TLV_OP_WRITE = 1,
  38. SNDRV_CTL_TLV_OP_CMD = -1,
  39. };
  40. struct snd_kcontrol_new {
  41. snd_ctl_elem_iface_t iface; /* interface identifier */
  42. unsigned int device; /* device/client number */
  43. unsigned int subdevice; /* subdevice (substream) number */
  44. const unsigned char *name; /* ASCII name of item */
  45. unsigned int index; /* index of item */
  46. unsigned int access; /* access rights */
  47. unsigned int count; /* count of same elements */
  48. snd_kcontrol_info_t *info;
  49. snd_kcontrol_get_t *get;
  50. snd_kcontrol_put_t *put;
  51. union {
  52. snd_kcontrol_tlv_rw_t *c;
  53. const unsigned int *p;
  54. } tlv;
  55. unsigned long private_value;
  56. };
  57. struct snd_kcontrol_volatile {
  58. struct snd_ctl_file *owner; /* locked */
  59. unsigned int access; /* access rights */
  60. };
  61. struct snd_kcontrol {
  62. struct list_head list; /* list of controls */
  63. struct snd_ctl_elem_id id;
  64. unsigned int count; /* count of same elements */
  65. snd_kcontrol_info_t *info;
  66. snd_kcontrol_get_t *get;
  67. snd_kcontrol_put_t *put;
  68. union {
  69. snd_kcontrol_tlv_rw_t *c;
  70. const unsigned int *p;
  71. } tlv;
  72. unsigned long private_value;
  73. void *private_data;
  74. void (*private_free)(struct snd_kcontrol *kcontrol);
  75. struct snd_kcontrol_volatile vd[0]; /* volatile data */
  76. };
  77. #define snd_kcontrol(n) list_entry(n, struct snd_kcontrol, list)
  78. struct snd_kctl_event {
  79. struct list_head list; /* list of events */
  80. struct snd_ctl_elem_id id;
  81. unsigned int mask;
  82. };
  83. #define snd_kctl_event(n) list_entry(n, struct snd_kctl_event, list)
  84. struct pid;
  85. enum {
  86. SND_CTL_SUBDEV_PCM,
  87. SND_CTL_SUBDEV_RAWMIDI,
  88. SND_CTL_SUBDEV_ITEMS,
  89. };
  90. struct snd_ctl_file {
  91. struct list_head list; /* list of all control files */
  92. struct snd_card *card;
  93. struct pid *pid;
  94. int preferred_subdevice[SND_CTL_SUBDEV_ITEMS];
  95. wait_queue_head_t change_sleep;
  96. spinlock_t read_lock;
  97. struct fasync_struct *fasync;
  98. int subscribed; /* read interface is activated */
  99. struct list_head events; /* waiting events for read */
  100. };
  101. #define snd_ctl_file(n) list_entry(n, struct snd_ctl_file, list)
  102. typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
  103. struct snd_ctl_file * control,
  104. unsigned int cmd, unsigned long arg);
  105. void snd_ctl_notify(struct snd_card * card, unsigned int mask, struct snd_ctl_elem_id * id);
  106. struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new * kcontrolnew, void * private_data);
  107. void snd_ctl_free_one(struct snd_kcontrol * kcontrol);
  108. int snd_ctl_add(struct snd_card * card, struct snd_kcontrol * kcontrol);
  109. int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);
  110. int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, bool add_on_replace);
  111. int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
  112. int snd_ctl_rename_id(struct snd_card * card, struct snd_ctl_elem_id *src_id, struct snd_ctl_elem_id *dst_id);
  113. int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
  114. int active);
  115. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card * card, unsigned int numid);
  116. struct snd_kcontrol *snd_ctl_find_id(struct snd_card * card, struct snd_ctl_elem_id *id);
  117. int snd_ctl_create(struct snd_card *card);
  118. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn);
  119. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn);
  120. #ifdef CONFIG_COMPAT
  121. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn);
  122. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
  123. #else
  124. #define snd_ctl_register_ioctl_compat(fcn)
  125. #define snd_ctl_unregister_ioctl_compat(fcn)
  126. #endif
  127. int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
  128. static inline unsigned int snd_ctl_get_ioffnum(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
  129. {
  130. unsigned int ioff = id->numid - kctl->id.numid;
  131. return array_index_nospec(ioff, kctl->count);
  132. }
  133. static inline unsigned int snd_ctl_get_ioffidx(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
  134. {
  135. unsigned int ioff = id->index - kctl->id.index;
  136. return array_index_nospec(ioff, kctl->count);
  137. }
  138. static inline unsigned int snd_ctl_get_ioff(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
  139. {
  140. if (id->numid) {
  141. return snd_ctl_get_ioffnum(kctl, id);
  142. } else {
  143. return snd_ctl_get_ioffidx(kctl, id);
  144. }
  145. }
  146. static inline struct snd_ctl_elem_id *snd_ctl_build_ioff(struct snd_ctl_elem_id *dst_id,
  147. struct snd_kcontrol *src_kctl,
  148. unsigned int offset)
  149. {
  150. *dst_id = src_kctl->id;
  151. dst_id->index += offset;
  152. dst_id->numid += offset;
  153. return dst_id;
  154. }
  155. /*
  156. * Frequently used control callbacks/helpers
  157. */
  158. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_info *uinfo);
  160. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  161. struct snd_ctl_elem_info *uinfo);
  162. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  163. unsigned int items, const char *const names[]);
  164. /*
  165. * virtual master control
  166. */
  167. struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
  168. const unsigned int *tlv);
  169. int _snd_ctl_add_slave(struct snd_kcontrol *master, struct snd_kcontrol *slave,
  170. unsigned int flags);
  171. /* optional flags for slave */
  172. #define SND_CTL_SLAVE_NEED_UPDATE (1 << 0)
  173. /**
  174. * snd_ctl_add_slave - Add a virtual slave control
  175. * @master: vmaster element
  176. * @slave: slave element to add
  177. *
  178. * Add a virtual slave control to the given master element created via
  179. * snd_ctl_create_virtual_master() beforehand.
  180. *
  181. * All slaves must be the same type (returning the same information
  182. * via info callback). The function doesn't check it, so it's your
  183. * responsibility.
  184. *
  185. * Also, some additional limitations:
  186. * at most two channels,
  187. * logarithmic volume control (dB level) thus no linear volume,
  188. * master can only attenuate the volume without gain
  189. *
  190. * Return: Zero if successful or a negative error code.
  191. */
  192. static inline int
  193. snd_ctl_add_slave(struct snd_kcontrol *master, struct snd_kcontrol *slave)
  194. {
  195. return _snd_ctl_add_slave(master, slave, 0);
  196. }
  197. /**
  198. * snd_ctl_add_slave_uncached - Add a virtual slave control
  199. * @master: vmaster element
  200. * @slave: slave element to add
  201. *
  202. * Add a virtual slave control to the given master.
  203. * Unlike snd_ctl_add_slave(), the element added via this function
  204. * is supposed to have volatile values, and get callback is called
  205. * at each time queried from the master.
  206. *
  207. * When the control peeks the hardware values directly and the value
  208. * can be changed by other means than the put callback of the element,
  209. * this function should be used to keep the value always up-to-date.
  210. *
  211. * Return: Zero if successful or a negative error code.
  212. */
  213. static inline int
  214. snd_ctl_add_slave_uncached(struct snd_kcontrol *master,
  215. struct snd_kcontrol *slave)
  216. {
  217. return _snd_ctl_add_slave(master, slave, SND_CTL_SLAVE_NEED_UPDATE);
  218. }
  219. int snd_ctl_add_vmaster_hook(struct snd_kcontrol *kctl,
  220. void (*hook)(void *private_data, int),
  221. void *private_data);
  222. void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only);
  223. #define snd_ctl_sync_vmaster_hook(kctl) snd_ctl_sync_vmaster(kctl, true)
  224. int snd_ctl_apply_vmaster_slaves(struct snd_kcontrol *kctl,
  225. int (*func)(struct snd_kcontrol *vslave,
  226. struct snd_kcontrol *slave,
  227. void *arg),
  228. void *arg);
  229. /*
  230. * Helper functions for jack-detection controls
  231. */
  232. struct snd_kcontrol *
  233. snd_kctl_jack_new(const char *name, struct snd_card *card);
  234. void snd_kctl_jack_report(struct snd_card *card,
  235. struct snd_kcontrol *kctl, bool status);
  236. #endif /* __SOUND_CONTROL_H */