1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef __SOUND_CORE_H
3 #define __SOUND_CORE_H
4
5 /*
6 * Main header file for the ALSA driver
7 * Copyright (c) 1994-2001 by Jaroslav Kysela <perex@perex.cz>
8 */
9
10 #include <linux/device.h>
11 #include <linux/sched.h> /* wake_up() */
12 #include <linux/mutex.h> /* struct mutex */
13 #include <linux/rwsem.h> /* struct rw_semaphore */
14 #include <linux/pm.h> /* pm_message_t */
15 #include <linux/stringify.h>
16 #include <linux/printk.h>
17 #include <linux/xarray.h>
18 #include <linux/android_kabi.h>
19
20 /* number of supported soundcards */
21 #ifdef CONFIG_SND_DYNAMIC_MINORS
22 #define SNDRV_CARDS CONFIG_SND_MAX_CARDS
23 #else
24 #define SNDRV_CARDS 8 /* don't change - minor numbers */
25 #endif
26
27 #define CONFIG_SND_MAJOR 116 /* standard configuration */
28
29 /* forward declarations */
30 struct pci_dev;
31 struct module;
32 struct completion;
33
34 /* device allocation stuff */
35
36 /* type of the object used in snd_device_*()
37 * this also defines the calling order
38 */
39 enum snd_device_type {
40 SNDRV_DEV_LOWLEVEL,
41 SNDRV_DEV_INFO,
42 SNDRV_DEV_BUS,
43 SNDRV_DEV_CODEC,
44 SNDRV_DEV_PCM,
45 SNDRV_DEV_COMPRESS,
46 SNDRV_DEV_RAWMIDI,
47 SNDRV_DEV_TIMER,
48 SNDRV_DEV_SEQUENCER,
49 SNDRV_DEV_HWDEP,
50 SNDRV_DEV_JACK,
51 SNDRV_DEV_CONTROL, /* NOTE: this must be the last one */
52 };
53
54 enum snd_device_state {
55 SNDRV_DEV_BUILD,
56 SNDRV_DEV_REGISTERED,
57 SNDRV_DEV_DISCONNECTED,
58 };
59
60 struct snd_device;
61
62 struct snd_device_ops {
63 int (*dev_free)(struct snd_device *dev);
64 int (*dev_register)(struct snd_device *dev);
65 int (*dev_disconnect)(struct snd_device *dev);
66
67 ANDROID_KABI_RESERVE(1);
68 };
69
70 struct snd_device {
71 struct list_head list; /* list of registered devices */
72 struct snd_card *card; /* card which holds this device */
73 enum snd_device_state state; /* state of the device */
74 enum snd_device_type type; /* device type */
75 void *device_data; /* device structure */
76 const struct snd_device_ops *ops; /* operations */
77
78 ANDROID_KABI_RESERVE(1);
79 };
80
81 #define snd_device(n) list_entry(n, struct snd_device, list)
82
83 /* main structure for soundcard */
84
85 struct snd_card {
86 int number; /* number of soundcard (index to
87 snd_cards) */
88
89 char id[16]; /* id string of this card */
90 char driver[16]; /* driver name */
91 char shortname[32]; /* short name of this soundcard */
92 char longname[80]; /* name of this soundcard */
93 char irq_descr[32]; /* Interrupt description */
94 char mixername[80]; /* mixer name */
95 char components[128]; /* card components delimited with
96 space */
97 struct module *module; /* top-level module */
98
99 void *private_data; /* private data for soundcard */
100 void (*private_free) (struct snd_card *card); /* callback for freeing of
101 private data */
102 struct list_head devices; /* devices */
103
104 struct device *ctl_dev; /* control device */
105 unsigned int last_numid; /* last used numeric ID */
106 struct rw_semaphore controls_rwsem; /* controls lock (list and values) */
107 rwlock_t ctl_files_rwlock; /* ctl_files list lock */
108 int controls_count; /* count of all controls */
109 size_t user_ctl_alloc_size; // current memory allocation by user controls.
110 struct list_head controls; /* all controls for this card */
111 struct list_head ctl_files; /* active control files */
112 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
113 struct xarray ctl_numids; /* hash table for numids */
114 struct xarray ctl_hash; /* hash table for ctl id matching */
115 bool ctl_hash_collision; /* ctl_hash collision seen? */
116 #endif
117
118 struct snd_info_entry *proc_root; /* root for soundcard specific files */
119 struct proc_dir_entry *proc_root_link; /* number link to real id */
120
121 struct list_head files_list; /* all files associated to this card */
122 struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
123 state */
124 spinlock_t files_lock; /* lock the files for this card */
125 int shutdown; /* this card is going down */
126 struct completion *release_completion;
127 struct device *dev; /* device assigned to this card */
128 struct device card_dev; /* cardX object for sysfs */
129 const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
130 bool registered; /* card_dev is registered? */
131 bool managed; /* managed via devres */
132 bool releasing; /* during card free process */
133 int sync_irq; /* assigned irq, used for PCM sync */
134 wait_queue_head_t remove_sleep;
135
136 size_t total_pcm_alloc_bytes; /* total amount of allocated buffers */
137 struct mutex memory_mutex; /* protection for the above */
138 #ifdef CONFIG_SND_DEBUG
139 struct dentry *debugfs_root; /* debugfs root for card */
140 #endif
141
142 #ifdef CONFIG_PM
143 unsigned int power_state; /* power state */
144 atomic_t power_ref;
145 wait_queue_head_t power_sleep;
146 wait_queue_head_t power_ref_sleep;
147 #endif
148
149 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
150 struct snd_mixer_oss *mixer_oss;
151 int mixer_oss_change_count;
152 #endif
153
154 ANDROID_KABI_RESERVE(1);
155 ANDROID_KABI_RESERVE(2);
156 };
157
158 #define dev_to_snd_card(p) container_of(p, struct snd_card, card_dev)
159
160 #ifdef CONFIG_PM
snd_power_get_state(struct snd_card * card)161 static inline unsigned int snd_power_get_state(struct snd_card *card)
162 {
163 return READ_ONCE(card->power_state);
164 }
165
snd_power_change_state(struct snd_card * card,unsigned int state)166 static inline void snd_power_change_state(struct snd_card *card, unsigned int state)
167 {
168 WRITE_ONCE(card->power_state, state);
169 wake_up(&card->power_sleep);
170 }
171
172 /**
173 * snd_power_ref - Take the reference count for power control
174 * @card: sound card object
175 *
176 * The power_ref reference of the card is used for managing to block
177 * the snd_power_sync_ref() operation. This function increments the reference.
178 * The counterpart snd_power_unref() has to be called appropriately later.
179 */
snd_power_ref(struct snd_card * card)180 static inline void snd_power_ref(struct snd_card *card)
181 {
182 atomic_inc(&card->power_ref);
183 }
184
185 /**
186 * snd_power_unref - Release the reference count for power control
187 * @card: sound card object
188 */
snd_power_unref(struct snd_card * card)189 static inline void snd_power_unref(struct snd_card *card)
190 {
191 if (atomic_dec_and_test(&card->power_ref))
192 wake_up(&card->power_ref_sleep);
193 }
194
195 /**
196 * snd_power_sync_ref - wait until the card power_ref is freed
197 * @card: sound card object
198 *
199 * This function is used to synchronize with the pending power_ref being
200 * released.
201 */
snd_power_sync_ref(struct snd_card * card)202 static inline void snd_power_sync_ref(struct snd_card *card)
203 {
204 wait_event(card->power_ref_sleep, !atomic_read(&card->power_ref));
205 }
206
207 /* init.c */
208 int snd_power_wait(struct snd_card *card);
209 int snd_power_ref_and_wait(struct snd_card *card);
210
211 #else /* ! CONFIG_PM */
212
snd_power_wait(struct snd_card * card)213 static inline int snd_power_wait(struct snd_card *card) { return 0; }
snd_power_ref(struct snd_card * card)214 static inline void snd_power_ref(struct snd_card *card) {}
snd_power_unref(struct snd_card * card)215 static inline void snd_power_unref(struct snd_card *card) {}
snd_power_ref_and_wait(struct snd_card * card)216 static inline int snd_power_ref_and_wait(struct snd_card *card) { return 0; }
snd_power_sync_ref(struct snd_card * card)217 static inline void snd_power_sync_ref(struct snd_card *card) {}
218 #define snd_power_get_state(card) ({ (void)(card); SNDRV_CTL_POWER_D0; })
219 #define snd_power_change_state(card, state) do { (void)(card); } while (0)
220
221 #endif /* CONFIG_PM */
222
223 struct snd_minor {
224 int type; /* SNDRV_DEVICE_TYPE_XXX */
225 int card; /* card number */
226 int device; /* device number */
227 const struct file_operations *f_ops; /* file operations */
228 void *private_data; /* private data for f_ops->open */
229 struct device *dev; /* device for sysfs */
230 struct snd_card *card_ptr; /* assigned card instance */
231
232 ANDROID_KABI_RESERVE(1);
233 };
234
235 /* return a device pointer linked to each sound device as a parent */
snd_card_get_device_link(struct snd_card * card)236 static inline struct device *snd_card_get_device_link(struct snd_card *card)
237 {
238 return card ? &card->card_dev : NULL;
239 }
240
241 /* sound.c */
242
243 extern int snd_major;
244 extern int snd_ecards_limit;
245 extern const struct class sound_class;
246 #ifdef CONFIG_SND_DEBUG
247 extern struct dentry *sound_debugfs_root;
248 #endif
249
250 void snd_request_card(int card);
251
252 int snd_device_alloc(struct device **dev_p, struct snd_card *card);
253
254 int snd_register_device(int type, struct snd_card *card, int dev,
255 const struct file_operations *f_ops,
256 void *private_data, struct device *device);
257 int snd_unregister_device(struct device *dev);
258 void *snd_lookup_minor_data(unsigned int minor, int type);
259
260 #ifdef CONFIG_SND_OSSEMUL
261 int snd_register_oss_device(int type, struct snd_card *card, int dev,
262 const struct file_operations *f_ops, void *private_data);
263 int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
264 void *snd_lookup_oss_minor_data(unsigned int minor, int type);
265 #endif
266
267 int snd_minor_info_init(void);
268
269 /* sound_oss.c */
270
271 #ifdef CONFIG_SND_OSSEMUL
272 int snd_minor_info_oss_init(void);
273 #else
snd_minor_info_oss_init(void)274 static inline int snd_minor_info_oss_init(void) { return 0; }
275 #endif
276
277 /* memory.c */
278
279 int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
280 int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
281
282 /* init.c */
283
284 int snd_card_locked(int card);
285 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
286 #define SND_MIXER_OSS_NOTIFY_REGISTER 0
287 #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1
288 #define SND_MIXER_OSS_NOTIFY_FREE 2
289 extern int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int cmd);
290 #endif
291
292 int snd_card_new(struct device *parent, int idx, const char *xid,
293 struct module *module, int extra_size,
294 struct snd_card **card_ret);
295 int snd_devm_card_new(struct device *parent, int idx, const char *xid,
296 struct module *module, size_t extra_size,
297 struct snd_card **card_ret);
298
299 void snd_card_disconnect(struct snd_card *card);
300 void snd_card_disconnect_sync(struct snd_card *card);
301 void snd_card_free(struct snd_card *card);
302 void snd_card_free_when_closed(struct snd_card *card);
303 int snd_card_free_on_error(struct device *dev, int ret);
304 void snd_card_set_id(struct snd_card *card, const char *id);
305 int snd_card_register(struct snd_card *card);
306 int snd_card_info_init(void);
307 int snd_card_add_dev_attr(struct snd_card *card,
308 const struct attribute_group *group);
309 int snd_component_add(struct snd_card *card, const char *component);
310 int snd_card_file_add(struct snd_card *card, struct file *file);
311 int snd_card_file_remove(struct snd_card *card, struct file *file);
312
313 struct snd_card *snd_card_ref(int card);
314
315 /**
316 * snd_card_unref - Unreference the card object
317 * @card: the card object to unreference
318 *
319 * Call this function for the card object that was obtained via snd_card_ref()
320 * or snd_lookup_minor_data().
321 */
snd_card_unref(struct snd_card * card)322 static inline void snd_card_unref(struct snd_card *card)
323 {
324 put_device(&card->card_dev);
325 }
326
327 #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
328
329 /* device.c */
330
331 int snd_device_new(struct snd_card *card, enum snd_device_type type,
332 void *device_data, const struct snd_device_ops *ops);
333 int snd_device_register(struct snd_card *card, void *device_data);
334 int snd_device_register_all(struct snd_card *card);
335 void snd_device_disconnect(struct snd_card *card, void *device_data);
336 void snd_device_disconnect_all(struct snd_card *card);
337 void snd_device_free(struct snd_card *card, void *device_data);
338 void snd_device_free_all(struct snd_card *card);
339 int snd_device_get_state(struct snd_card *card, void *device_data);
340
341 /* isadma.c */
342
343 #ifdef CONFIG_ISA_DMA_API
344 #define DMA_MODE_NO_ENABLE 0x0100
345
346 void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
347 void snd_dma_disable(unsigned long dma);
348 unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
349 int snd_devm_request_dma(struct device *dev, int dma, const char *name);
350 #endif
351
352 /* misc.c */
353 struct resource;
354 void release_and_free_resource(struct resource *res);
355
356 /* --- */
357
358 /* sound printk debug levels */
359 enum {
360 SND_PR_ALWAYS,
361 SND_PR_DEBUG,
362 SND_PR_VERBOSE,
363 };
364
365 #if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
366 __printf(4, 5)
367 void __snd_printk(unsigned int level, const char *file, int line,
368 const char *format, ...);
369 #else
370 #define __snd_printk(level, file, line, format, ...) \
371 printk(format, ##__VA_ARGS__)
372 #endif
373
374 /**
375 * snd_printk - printk wrapper
376 * @fmt: format string
377 *
378 * Works like printk() but prints the file and the line of the caller
379 * when configured with CONFIG_SND_VERBOSE_PRINTK.
380 */
381 #define snd_printk(fmt, ...) \
382 __snd_printk(0, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
383
384 #ifdef CONFIG_SND_DEBUG
385 /**
386 * snd_printd - debug printk
387 * @fmt: format string
388 *
389 * Works like snd_printk() for debugging purposes.
390 * Ignored when CONFIG_SND_DEBUG is not set.
391 */
392 #define snd_printd(fmt, ...) \
393 __snd_printk(1, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
394 #define _snd_printd(level, fmt, ...) \
395 __snd_printk(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
396
397 /**
398 * snd_BUG - give a BUG warning message and stack trace
399 *
400 * Calls WARN() if CONFIG_SND_DEBUG is set.
401 * Ignored when CONFIG_SND_DEBUG is not set.
402 */
403 #define snd_BUG() WARN(1, "BUG?\n")
404
405 /**
406 * snd_printd_ratelimit - Suppress high rates of output when
407 * CONFIG_SND_DEBUG is enabled.
408 */
409 #define snd_printd_ratelimit() printk_ratelimit()
410
411 /**
412 * snd_BUG_ON - debugging check macro
413 * @cond: condition to evaluate
414 *
415 * Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set,
416 * otherwise just evaluates the conditional and returns the value.
417 */
418 #define snd_BUG_ON(cond) WARN_ON((cond))
419
420 #else /* !CONFIG_SND_DEBUG */
421
422 __printf(1, 2)
snd_printd(const char * format,...)423 static inline void snd_printd(const char *format, ...) {}
424 __printf(2, 3)
_snd_printd(int level,const char * format,...)425 static inline void _snd_printd(int level, const char *format, ...) {}
426
427 #define snd_BUG() do { } while (0)
428
429 #define snd_BUG_ON(condition) ({ \
430 int __ret_warn_on = !!(condition); \
431 unlikely(__ret_warn_on); \
432 })
433
snd_printd_ratelimit(void)434 static inline bool snd_printd_ratelimit(void) { return false; }
435
436 #endif /* CONFIG_SND_DEBUG */
437
438 #ifdef CONFIG_SND_DEBUG_VERBOSE
439 /**
440 * snd_printdd - debug printk
441 * @format: format string
442 *
443 * Works like snd_printk() for debugging purposes.
444 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
445 */
446 #define snd_printdd(format, ...) \
447 __snd_printk(2, __FILE__, __LINE__, format, ##__VA_ARGS__)
448 #else
449 __printf(1, 2)
snd_printdd(const char * format,...)450 static inline void snd_printdd(const char *format, ...) {}
451 #endif
452
453
454 #define SNDRV_OSS_VERSION ((3<<16)|(8<<8)|(1<<4)|(0)) /* 3.8.1a */
455
456 /* for easier backward-porting */
457 #if IS_ENABLED(CONFIG_GAMEPORT)
458 #define gameport_set_dev_parent(gp,xdev) ((gp)->dev.parent = (xdev))
459 #define gameport_set_port_data(gp,r) ((gp)->port_data = (r))
460 #define gameport_get_port_data(gp) (gp)->port_data
461 #endif
462
463 /* PCI quirk list helper */
464 struct snd_pci_quirk {
465 unsigned short subvendor; /* PCI subvendor ID */
466 unsigned short subdevice; /* PCI subdevice ID */
467 unsigned short subdevice_mask; /* bitmask to match */
468 int value; /* value */
469 #ifdef CONFIG_SND_DEBUG_VERBOSE
470 const char *name; /* name of the device (optional) */
471 #endif
472 };
473
474 #define _SND_PCI_QUIRK_ID_MASK(vend, mask, dev) \
475 .subvendor = (vend), .subdevice = (dev), .subdevice_mask = (mask)
476 #define _SND_PCI_QUIRK_ID(vend, dev) \
477 _SND_PCI_QUIRK_ID_MASK(vend, 0xffff, dev)
478 #define SND_PCI_QUIRK_ID(vend,dev) {_SND_PCI_QUIRK_ID(vend, dev)}
479 #ifdef CONFIG_SND_DEBUG_VERBOSE
480 #define SND_PCI_QUIRK(vend,dev,xname,val) \
481 {_SND_PCI_QUIRK_ID(vend, dev), .value = (val), .name = (xname)}
482 #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
483 {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val), .name = (xname)}
484 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
485 {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), \
486 .value = (val), .name = (xname)}
487 #define snd_pci_quirk_name(q) ((q)->name)
488 #else
489 #define SND_PCI_QUIRK(vend,dev,xname,val) \
490 {_SND_PCI_QUIRK_ID(vend, dev), .value = (val)}
491 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
492 {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), .value = (val)}
493 #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
494 {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val)}
495 #define snd_pci_quirk_name(q) ""
496 #endif
497
498 #ifdef CONFIG_PCI
499 const struct snd_pci_quirk *
500 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list);
501
502 const struct snd_pci_quirk *
503 snd_pci_quirk_lookup_id(u16 vendor, u16 device,
504 const struct snd_pci_quirk *list);
505 #else
506 static inline const struct snd_pci_quirk *
snd_pci_quirk_lookup(struct pci_dev * pci,const struct snd_pci_quirk * list)507 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
508 {
509 return NULL;
510 }
511
512 static inline const struct snd_pci_quirk *
snd_pci_quirk_lookup_id(u16 vendor,u16 device,const struct snd_pci_quirk * list)513 snd_pci_quirk_lookup_id(u16 vendor, u16 device,
514 const struct snd_pci_quirk *list)
515 {
516 return NULL;
517 }
518 #endif
519
520 /* async signal helpers */
521 struct snd_fasync;
522
523 int snd_fasync_helper(int fd, struct file *file, int on,
524 struct snd_fasync **fasyncp);
525 void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll);
526 void snd_fasync_free(struct snd_fasync *fasync);
527
528 #endif /* __SOUND_CORE_H */
529