control.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __SOUND_CONTROL_H
  3. #define __SOUND_CONTROL_H
  4. /*
  5. * Header file for control interface
  6. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  7. */
  8. #include <linux/wait.h>
  9. #include <linux/nospec.h>
  10. #include <sound/asound.h>
  11. #define snd_kcontrol_chip(kcontrol) ((kcontrol)->private_data)
  12. struct snd_kcontrol;
  13. typedef int (snd_kcontrol_info_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_info * uinfo);
  14. typedef int (snd_kcontrol_get_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
  15. typedef int (snd_kcontrol_put_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);
  16. typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,
  17. int op_flag, /* SNDRV_CTL_TLV_OP_XXX */
  18. unsigned int size,
  19. unsigned int __user *tlv);
  20. /* internal flag for skipping validations */
  21. #ifdef CONFIG_SND_CTL_DEBUG
  22. #define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK (1 << 24)
  23. #define snd_ctl_skip_validation(info) \
  24. ((info)->access & SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK)
  25. #else
  26. #define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK 0
  27. #define snd_ctl_skip_validation(info) true
  28. #endif
  29. /* kernel only - LED bits */
  30. #define SNDRV_CTL_ELEM_ACCESS_LED_SHIFT 25
  31. #define SNDRV_CTL_ELEM_ACCESS_LED_MASK (7<<25) /* kernel three bits - LED group */
  32. #define SNDRV_CTL_ELEM_ACCESS_SPK_LED (1<<25) /* kernel speaker (output) LED flag */
  33. #define SNDRV_CTL_ELEM_ACCESS_MIC_LED (2<<25) /* kernel microphone (input) LED flag */
  34. enum {
  35. SNDRV_CTL_TLV_OP_READ = 0,
  36. SNDRV_CTL_TLV_OP_WRITE = 1,
  37. SNDRV_CTL_TLV_OP_CMD = -1,
  38. };
  39. struct snd_kcontrol_new {
  40. snd_ctl_elem_iface_t iface; /* interface identifier */
  41. unsigned int device; /* device/client number */
  42. unsigned int subdevice; /* subdevice (substream) number */
  43. const char *name; /* ASCII name of item */
  44. unsigned int index; /* index of item */
  45. unsigned int access; /* access rights */
  46. unsigned int count; /* count of same elements */
  47. snd_kcontrol_info_t *info;
  48. snd_kcontrol_get_t *get;
  49. snd_kcontrol_put_t *put;
  50. union {
  51. snd_kcontrol_tlv_rw_t *c;
  52. const unsigned int *p;
  53. } tlv;
  54. unsigned long private_value;
  55. };
  56. struct snd_kcontrol_volatile {
  57. struct snd_ctl_file *owner; /* locked */
  58. unsigned int access; /* access rights */
  59. };
  60. struct snd_kcontrol {
  61. struct list_head list; /* list of controls */
  62. struct snd_ctl_elem_id id;
  63. unsigned int count; /* count of same elements */
  64. snd_kcontrol_info_t *info;
  65. snd_kcontrol_get_t *get;
  66. snd_kcontrol_put_t *put;
  67. union {
  68. snd_kcontrol_tlv_rw_t *c;
  69. const unsigned int *p;
  70. } tlv;
  71. unsigned long private_value;
  72. void *private_data;
  73. void (*private_free)(struct snd_kcontrol *kcontrol);
  74. struct snd_kcontrol_volatile vd[] __counted_by(count); /* volatile data */
  75. };
  76. #define snd_kcontrol(n) list_entry(n, struct snd_kcontrol, list)
  77. struct snd_kctl_event {
  78. struct list_head list; /* list of events */
  79. struct snd_ctl_elem_id id;
  80. unsigned int mask;
  81. };
  82. #define snd_kctl_event(n) list_entry(n, struct snd_kctl_event, list)
  83. struct pid;
  84. enum {
  85. SND_CTL_SUBDEV_PCM,
  86. SND_CTL_SUBDEV_RAWMIDI,
  87. SND_CTL_SUBDEV_ITEMS,
  88. };
  89. struct snd_ctl_file {
  90. struct list_head list; /* list of all control files */
  91. struct snd_card *card;
  92. struct pid *pid;
  93. int preferred_subdevice[SND_CTL_SUBDEV_ITEMS];
  94. wait_queue_head_t change_sleep;
  95. spinlock_t read_lock;
  96. struct snd_fasync *fasync;
  97. int subscribed; /* read interface is activated */
  98. struct list_head events; /* waiting events for read */
  99. };
  100. struct snd_ctl_layer_ops {
  101. struct snd_ctl_layer_ops *next;
  102. const char *module_name;
  103. void (*lregister)(struct snd_card *card);
  104. void (*ldisconnect)(struct snd_card *card);
  105. void (*lnotify)(struct snd_card *card, unsigned int mask, struct snd_kcontrol *kctl, unsigned int ioff);
  106. };
  107. #define snd_ctl_file(n) list_entry(n, struct snd_ctl_file, list)
  108. typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
  109. struct snd_ctl_file * control,
  110. unsigned int cmd, unsigned long arg);
  111. void snd_ctl_notify(struct snd_card * card, unsigned int mask, struct snd_ctl_elem_id * id);
  112. void snd_ctl_notify_one(struct snd_card * card, unsigned int mask, struct snd_kcontrol * kctl, unsigned int ioff);
  113. struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new * kcontrolnew, void * private_data);
  114. void snd_ctl_free_one(struct snd_kcontrol * kcontrol);
  115. int snd_ctl_add(struct snd_card * card, struct snd_kcontrol * kcontrol);
  116. int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);
  117. int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, bool add_on_replace);
  118. int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
  119. int snd_ctl_rename_id(struct snd_card * card, struct snd_ctl_elem_id *src_id, struct snd_ctl_elem_id *dst_id);
  120. void snd_ctl_rename(struct snd_card *card, struct snd_kcontrol *kctl, const char *name);
  121. int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id, int active);
  122. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid);
  123. struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card, const struct snd_ctl_elem_id *id);
  124. /**
  125. * snd_ctl_find_id_mixer - find the control instance with the given name string
  126. * @card: the card instance
  127. * @name: the name string
  128. *
  129. * Finds the control instance with the given name and
  130. * @SNDRV_CTL_ELEM_IFACE_MIXER. Other fields are set to zero.
  131. *
  132. * This is merely a wrapper to snd_ctl_find_id().
  133. *
  134. * Return: The pointer of the instance if found, or %NULL if not.
  135. */
  136. static inline struct snd_kcontrol *
  137. snd_ctl_find_id_mixer(struct snd_card *card, const char *name)
  138. {
  139. struct snd_ctl_elem_id id = {};
  140. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  141. strscpy(id.name, name, sizeof(id.name));
  142. return snd_ctl_find_id(card, &id);
  143. }
  144. int snd_ctl_create(struct snd_card *card);
  145. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn);
  146. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn);
  147. #ifdef CONFIG_COMPAT
  148. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn);
  149. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
  150. #else
  151. #define snd_ctl_register_ioctl_compat(fcn)
  152. #define snd_ctl_unregister_ioctl_compat(fcn)
  153. #endif
  154. int snd_ctl_request_layer(const char *module_name);
  155. void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops);
  156. void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops);
  157. int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
  158. static inline unsigned int snd_ctl_get_ioffnum(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
  159. {
  160. unsigned int ioff = id->numid - kctl->id.numid;
  161. return array_index_nospec(ioff, kctl->count);
  162. }
  163. static inline unsigned int snd_ctl_get_ioffidx(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
  164. {
  165. unsigned int ioff = id->index - kctl->id.index;
  166. return array_index_nospec(ioff, kctl->count);
  167. }
  168. static inline unsigned int snd_ctl_get_ioff(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
  169. {
  170. if (id->numid) {
  171. return snd_ctl_get_ioffnum(kctl, id);
  172. } else {
  173. return snd_ctl_get_ioffidx(kctl, id);
  174. }
  175. }
  176. static inline struct snd_ctl_elem_id *snd_ctl_build_ioff(struct snd_ctl_elem_id *dst_id,
  177. struct snd_kcontrol *src_kctl,
  178. unsigned int offset)
  179. {
  180. *dst_id = src_kctl->id;
  181. dst_id->index += offset;
  182. dst_id->numid += offset;
  183. return dst_id;
  184. }
  185. /*
  186. * Frequently used control callbacks/helpers
  187. */
  188. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  189. struct snd_ctl_elem_info *uinfo);
  190. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  191. struct snd_ctl_elem_info *uinfo);
  192. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  193. unsigned int items, const char *const names[]);
  194. /*
  195. * virtual master control
  196. */
  197. struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
  198. const unsigned int *tlv);
  199. int _snd_ctl_add_follower(struct snd_kcontrol *master,
  200. struct snd_kcontrol *follower,
  201. unsigned int flags);
  202. /* optional flags for follower */
  203. #define SND_CTL_FOLLOWER_NEED_UPDATE (1 << 0)
  204. /**
  205. * snd_ctl_add_follower - Add a virtual follower control
  206. * @master: vmaster element
  207. * @follower: follower element to add
  208. *
  209. * Add a virtual follower control to the given master element created via
  210. * snd_ctl_create_virtual_master() beforehand.
  211. *
  212. * All followers must be the same type (returning the same information
  213. * via info callback). The function doesn't check it, so it's your
  214. * responsibility.
  215. *
  216. * Also, some additional limitations:
  217. * at most two channels,
  218. * logarithmic volume control (dB level) thus no linear volume,
  219. * master can only attenuate the volume without gain
  220. *
  221. * Return: Zero if successful or a negative error code.
  222. */
  223. static inline int
  224. snd_ctl_add_follower(struct snd_kcontrol *master, struct snd_kcontrol *follower)
  225. {
  226. return _snd_ctl_add_follower(master, follower, 0);
  227. }
  228. int snd_ctl_add_followers(struct snd_card *card, struct snd_kcontrol *master,
  229. const char * const *list);
  230. /**
  231. * snd_ctl_add_follower_uncached - Add a virtual follower control
  232. * @master: vmaster element
  233. * @follower: follower element to add
  234. *
  235. * Add a virtual follower control to the given master.
  236. * Unlike snd_ctl_add_follower(), the element added via this function
  237. * is supposed to have volatile values, and get callback is called
  238. * at each time queried from the master.
  239. *
  240. * When the control peeks the hardware values directly and the value
  241. * can be changed by other means than the put callback of the element,
  242. * this function should be used to keep the value always up-to-date.
  243. *
  244. * Return: Zero if successful or a negative error code.
  245. */
  246. static inline int
  247. snd_ctl_add_follower_uncached(struct snd_kcontrol *master,
  248. struct snd_kcontrol *follower)
  249. {
  250. return _snd_ctl_add_follower(master, follower, SND_CTL_FOLLOWER_NEED_UPDATE);
  251. }
  252. int snd_ctl_add_vmaster_hook(struct snd_kcontrol *kctl,
  253. void (*hook)(void *private_data, int),
  254. void *private_data);
  255. void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only);
  256. #define snd_ctl_sync_vmaster_hook(kctl) snd_ctl_sync_vmaster(kctl, true)
  257. int snd_ctl_apply_vmaster_followers(struct snd_kcontrol *kctl,
  258. int (*func)(struct snd_kcontrol *vfollower,
  259. struct snd_kcontrol *follower,
  260. void *arg),
  261. void *arg);
  262. /*
  263. * Control LED trigger layer
  264. */
  265. #define SND_CTL_LAYER_MODULE_LED "snd-ctl-led"
  266. #if IS_MODULE(CONFIG_SND_CTL_LED)
  267. static inline int snd_ctl_led_request(void) { return snd_ctl_request_layer(SND_CTL_LAYER_MODULE_LED); }
  268. #else
  269. static inline int snd_ctl_led_request(void) { return 0; }
  270. #endif
  271. /*
  272. * Helper functions for jack-detection controls
  273. */
  274. struct snd_kcontrol *
  275. snd_kctl_jack_new(const char *name, struct snd_card *card);
  276. void snd_kctl_jack_report(struct snd_card *card,
  277. struct snd_kcontrol *kctl, bool status);
  278. #endif /* __SOUND_CONTROL_H */