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