xref: /linux/include/sound/core.h (revision f0780cfa6acf72576464dc3b40431db389f46ee8)
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 
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 
67 struct snd_device {
68 	struct list_head list;		/* list of registered devices */
69 	struct snd_card *card;		/* card which holds this device */
70 	enum snd_device_state state;	/* state of the device */
71 	enum snd_device_type type;	/* device type */
72 	void *device_data;		/* device structure */
73 	const struct snd_device_ops *ops;	/* operations */
74 };
75 
76 #define snd_device(n) list_entry(n, struct snd_device, list)
77 
78 /*
79  * A simple reference counter with a wait queue;
80  * typically used for usage counts, and you can synchronize at finishing
81  * via snd_refcount_sync(), which is woken up when the refcount reaches to
82  * zero again.
83  */
84 struct snd_refcount {
85 	atomic_t count;
86 	wait_queue_head_t waiter;
87 	spinlock_t lock;
88 };
89 
90 void snd_refcount_init(struct snd_refcount *ref);
91 
92 static inline void snd_refcount_get(struct snd_refcount *ref)
93 {
94 	atomic_inc(&ref->count);
95 }
96 
97 void snd_refcount_put(struct snd_refcount *ref);
98 void snd_refcount_sync(struct snd_refcount *ref);
99 
100 /* main structure for soundcard */
101 
102 struct snd_card {
103 	int number;			/* number of soundcard (index to
104 								snd_cards) */
105 
106 	char id[16];			/* id string of this card */
107 	char driver[16];		/* driver name */
108 	char shortname[32];		/* short name of this soundcard */
109 	char longname[80];		/* name of this soundcard */
110 	char irq_descr[32];		/* Interrupt description */
111 	char mixername[80];		/* mixer name */
112 	char *components;		/* card components, space-delimited */
113 	unsigned int components_alloc_size;	/* current allocation size of components */
114 	struct module *module;		/* top-level module */
115 
116 	void *private_data;		/* private data for soundcard */
117 	void (*private_free) (struct snd_card *card); /* callback for freeing of
118 								private data */
119 	struct list_head devices;	/* devices */
120 
121 	struct device *ctl_dev;		/* control device */
122 	unsigned int last_numid;	/* last used numeric ID */
123 	struct rw_semaphore controls_rwsem;	/* controls lock (list and values) */
124 	rwlock_t controls_rwlock;	/* lock for lookup and ctl_files list */
125 	int controls_count;		/* count of all controls */
126 	size_t user_ctl_alloc_size;	// current memory allocation by user controls.
127 	struct list_head controls;	/* all controls for this card */
128 	struct list_head ctl_files;	/* active control files */
129 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
130 	struct xarray ctl_numids;	/* hash table for numids */
131 	struct xarray ctl_hash;		/* hash table for ctl id matching */
132 	bool ctl_hash_collision;	/* ctl_hash collision seen? */
133 #endif
134 
135 	struct snd_info_entry *proc_root;	/* root for soundcard specific files */
136 	struct proc_dir_entry *proc_root_link;	/* number link to real id */
137 
138 	struct list_head files_list;	/* all files associated to this card */
139 	struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
140 								state */
141 	spinlock_t files_lock;		/* lock the files for this card */
142 	int shutdown;			/* this card is going down */
143 	struct completion *release_completion;
144 	struct device *dev;		/* device assigned to this card */
145 	struct device card_dev;		/* cardX object for sysfs */
146 	const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
147 	bool registered;		/* card_dev is registered? */
148 	bool managed;			/* managed via devres */
149 	bool releasing;			/* during card free process */
150 	int sync_irq;			/* assigned irq, used for PCM sync */
151 	wait_queue_head_t remove_sleep;
152 
153 	size_t total_pcm_alloc_bytes;	/* total amount of allocated buffers */
154 	struct mutex memory_mutex;	/* protection for the above */
155 #ifdef CONFIG_SND_DEBUG
156 	struct dentry *debugfs_root;    /* debugfs root for card */
157 #endif
158 #ifdef CONFIG_SND_CTL_DEBUG
159 	struct snd_ctl_elem_value *value_buf; /* buffer for kctl->put() verification */
160 #endif
161 
162 #ifdef CONFIG_PM
163 	unsigned int power_state;	/* power state */
164 	wait_queue_head_t power_sleep;
165 	struct snd_refcount power_ref;
166 #endif
167 
168 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
169 	struct snd_mixer_oss *mixer_oss;
170 	int mixer_oss_change_count;
171 #endif
172 
173 	unsigned char private_data_area[] __aligned(__alignof__(unsigned long long));
174 };
175 
176 #define dev_to_snd_card(p)	container_of(p, struct snd_card, card_dev)
177 
178 #ifdef CONFIG_PM
179 static inline unsigned int snd_power_get_state(struct snd_card *card)
180 {
181 	return READ_ONCE(card->power_state);
182 }
183 
184 static inline void snd_power_change_state(struct snd_card *card, unsigned int state)
185 {
186 	WRITE_ONCE(card->power_state, state);
187 	wake_up(&card->power_sleep);
188 }
189 
190 /**
191  * snd_power_ref - Take the reference count for power control
192  * @card: sound card object
193  *
194  * The power_ref reference of the card is used for managing to block
195  * the snd_power_sync_ref() operation.  This function increments the reference.
196  * The counterpart snd_power_unref() has to be called appropriately later.
197  */
198 static inline void snd_power_ref(struct snd_card *card)
199 {
200 	snd_refcount_get(&card->power_ref);
201 }
202 
203 /**
204  * snd_power_unref - Release the reference count for power control
205  * @card: sound card object
206  */
207 static inline void snd_power_unref(struct snd_card *card)
208 {
209 	snd_refcount_put(&card->power_ref);
210 }
211 
212 /**
213  * snd_power_sync_ref - wait until the card power_ref is freed
214  * @card: sound card object
215  *
216  * This function is used to synchronize with the pending power_ref being
217  * released.
218  */
219 static inline void snd_power_sync_ref(struct snd_card *card)
220 {
221 	snd_refcount_sync(&card->power_ref);
222 }
223 
224 /* init.c */
225 int snd_power_wait(struct snd_card *card);
226 int snd_power_ref_and_wait(struct snd_card *card);
227 
228 #else /* ! CONFIG_PM */
229 
230 static inline int snd_power_wait(struct snd_card *card) { return 0; }
231 static inline void snd_power_ref(struct snd_card *card) {}
232 static inline void snd_power_unref(struct snd_card *card) {}
233 static inline int snd_power_ref_and_wait(struct snd_card *card) { return 0; }
234 static inline void snd_power_sync_ref(struct snd_card *card) {}
235 #define snd_power_get_state(card)	({ (void)(card); SNDRV_CTL_POWER_D0; })
236 #define snd_power_change_state(card, state)	do { (void)(card); } while (0)
237 
238 #endif /* CONFIG_PM */
239 
240 struct snd_minor {
241 	int type;			/* SNDRV_DEVICE_TYPE_XXX */
242 	int card;			/* card number */
243 	int device;			/* device number */
244 	const struct file_operations *f_ops;	/* file operations */
245 	void *private_data;		/* private data for f_ops->open */
246 	struct device *dev;		/* device for sysfs */
247 	struct snd_card *card_ptr;	/* assigned card instance */
248 };
249 
250 /* return a device pointer linked to each sound device as a parent */
251 static inline struct device *snd_card_get_device_link(struct snd_card *card)
252 {
253 	return card ? &card->card_dev : NULL;
254 }
255 
256 /* sound.c */
257 
258 extern int snd_major;
259 extern int snd_ecards_limit;
260 extern const struct class sound_class;
261 #ifdef CONFIG_SND_DEBUG
262 extern struct dentry *sound_debugfs_root;
263 #endif
264 
265 void snd_request_card(int card);
266 
267 int snd_device_alloc(struct device **dev_p, struct snd_card *card);
268 
269 int snd_register_device(int type, struct snd_card *card, int dev,
270 			const struct file_operations *f_ops,
271 			void *private_data, struct device *device);
272 int snd_unregister_device(struct device *dev);
273 void *snd_lookup_minor_data(unsigned int minor, int type);
274 
275 #ifdef CONFIG_SND_OSSEMUL
276 int snd_register_oss_device(int type, struct snd_card *card, int dev,
277 			    const struct file_operations *f_ops, void *private_data);
278 int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
279 void *snd_lookup_oss_minor_data(unsigned int minor, int type);
280 #endif
281 
282 int snd_minor_info_init(void);
283 
284 /* sound_oss.c */
285 
286 #ifdef CONFIG_SND_OSSEMUL
287 int snd_minor_info_oss_init(void);
288 #else
289 static inline int snd_minor_info_oss_init(void) { return 0; }
290 #endif
291 
292 /* memory.c */
293 
294 int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
295 int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
296 
297 /* init.c */
298 
299 int snd_card_locked(int card);
300 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
301 #define SND_MIXER_OSS_NOTIFY_REGISTER	0
302 #define SND_MIXER_OSS_NOTIFY_DISCONNECT	1
303 #define SND_MIXER_OSS_NOTIFY_FREE	2
304 void snd_mixer_oss_set_notify(int (*callback)(struct snd_card *card, int cmd));
305 void snd_mixer_oss_notify(struct snd_card *card, int cmd);
306 #endif
307 
308 int snd_card_new(struct device *parent, int idx, const char *xid,
309 		 struct module *module, int extra_size,
310 		 struct snd_card **card_ret);
311 int snd_devm_card_new(struct device *parent, int idx, const char *xid,
312 		      struct module *module, size_t extra_size,
313 		      struct snd_card **card_ret);
314 
315 void snd_card_disconnect(struct snd_card *card);
316 void snd_card_disconnect_sync(struct snd_card *card);
317 void snd_card_free(struct snd_card *card);
318 void snd_card_free_when_closed(struct snd_card *card);
319 void snd_card_set_id(struct snd_card *card, const char *id);
320 int snd_card_register(struct snd_card *card);
321 int snd_card_info_init(void);
322 int snd_card_add_dev_attr(struct snd_card *card,
323 			  const struct attribute_group *group);
324 int snd_component_add(struct snd_card *card, const char *component);
325 int snd_card_file_add(struct snd_card *card, struct file *file);
326 int snd_card_file_remove(struct snd_card *card, struct file *file);
327 
328 struct snd_card *snd_card_ref(int card);
329 
330 /**
331  * snd_card_unref - Unreference the card object
332  * @card: the card object to unreference
333  *
334  * Call this function for the card object that was obtained via snd_card_ref()
335  * or snd_lookup_minor_data().
336  */
337 static inline void snd_card_unref(struct snd_card *card)
338 {
339 	put_device(&card->card_dev);
340 }
341 
342 DEFINE_FREE(snd_card_unref, struct snd_card *, if (_T) snd_card_unref(_T))
343 
344 /*
345  * For automatic error handling at probe time, assign the card like:
346  *	struct snd_card *card __free(snd_card_free) = NULL;
347  * then create and process as usual.
348  * But, don't forget to clear to NULL at successful return for avoiding
349  * the unexpected cleanup!
350  */
351 DEFINE_FREE(snd_card_free, struct snd_card *, if (_T) snd_card_free(_T))
352 
353 #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
354 
355 /* device.c */
356 
357 int snd_device_new(struct snd_card *card, enum snd_device_type type,
358 		   void *device_data, const struct snd_device_ops *ops);
359 int snd_device_register(struct snd_card *card, void *device_data);
360 int snd_device_register_all(struct snd_card *card);
361 void snd_device_disconnect(struct snd_card *card, void *device_data);
362 void snd_device_disconnect_all(struct snd_card *card);
363 void snd_device_free(struct snd_card *card, void *device_data);
364 void snd_device_free_all(struct snd_card *card);
365 
366 /* isadma.c */
367 
368 #ifdef CONFIG_ISA_DMA_API
369 #define DMA_MODE_NO_ENABLE	0x0100
370 
371 void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
372 void snd_dma_disable(unsigned long dma);
373 unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
374 int snd_devm_request_dma(struct device *dev, int dma, const char *name);
375 #endif
376 
377 /* misc.c */
378 struct resource;
379 void release_and_free_resource(struct resource *res);
380 
381 /* --- */
382 
383 #ifdef CONFIG_SND_DEBUG
384 /**
385  * snd_BUG - give a BUG warning message and stack trace
386  *
387  * Calls WARN() if CONFIG_SND_DEBUG is set.
388  * Ignored when CONFIG_SND_DEBUG is not set.
389  */
390 #define snd_BUG()		WARN(1, "BUG?\n")
391 
392 /**
393  * snd_BUG_ON - debugging check macro
394  * @cond: condition to evaluate
395  *
396  * Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set,
397  * otherwise just evaluates the conditional and returns the value.
398  */
399 #define snd_BUG_ON(cond)	WARN_ON((cond))
400 
401 #else /* !CONFIG_SND_DEBUG */
402 
403 #define snd_BUG()			do { } while (0)
404 
405 #define snd_BUG_ON(condition) ({ \
406 	int __ret_warn_on = !!(condition); \
407 	unlikely(__ret_warn_on); \
408 })
409 
410 #endif /* CONFIG_SND_DEBUG */
411 
412 #define SNDRV_OSS_VERSION         ((3<<16)|(8<<8)|(1<<4)|(0))	/* 3.8.1a */
413 
414 /* for easier backward-porting */
415 #if IS_ENABLED(CONFIG_GAMEPORT)
416 #define gameport_set_dev_parent(gp,xdev) ((gp)->dev.parent = (xdev))
417 #define gameport_set_port_data(gp,r) ((gp)->port_data = (r))
418 #define gameport_get_port_data(gp) (gp)->port_data
419 #endif
420 
421 /* PCI quirk list helper */
422 struct snd_pci_quirk {
423 	unsigned short subvendor;	/* PCI subvendor ID */
424 	unsigned short subdevice;	/* PCI subdevice ID */
425 	unsigned short subdevice_mask;	/* bitmask to match */
426 	int value;			/* value */
427 #ifdef CONFIG_SND_DEBUG_VERBOSE
428 	const char *name;		/* name of the device (optional) */
429 #endif
430 };
431 
432 #define _SND_PCI_QUIRK_ID_MASK(vend, mask, dev)	\
433 	.subvendor = (vend), .subdevice = (dev), .subdevice_mask = (mask)
434 #define _SND_PCI_QUIRK_ID(vend, dev) \
435 	_SND_PCI_QUIRK_ID_MASK(vend, 0xffff, dev)
436 #define SND_PCI_QUIRK_ID(vend,dev) {_SND_PCI_QUIRK_ID(vend, dev)}
437 #ifdef CONFIG_SND_DEBUG_VERBOSE
438 #define SND_PCI_QUIRK(vend,dev,xname,val) \
439 	{_SND_PCI_QUIRK_ID(vend, dev), .value = (val), .name = (xname)}
440 #define SND_PCI_QUIRK_VENDOR(vend, xname, val)			\
441 	{_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val), .name = (xname)}
442 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val)			\
443 	{_SND_PCI_QUIRK_ID_MASK(vend, mask, dev),			\
444 			.value = (val), .name = (xname)}
445 #define snd_pci_quirk_name(q)	((q)->name)
446 #else
447 #define SND_PCI_QUIRK(vend,dev,xname,val) \
448 	{_SND_PCI_QUIRK_ID(vend, dev), .value = (val)}
449 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val)			\
450 	{_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), .value = (val)}
451 #define SND_PCI_QUIRK_VENDOR(vend, xname, val)			\
452 	{_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val)}
453 #define snd_pci_quirk_name(q)	""
454 #endif
455 
456 #ifdef CONFIG_PCI
457 const struct snd_pci_quirk *
458 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list);
459 
460 const struct snd_pci_quirk *
461 snd_pci_quirk_lookup_id(u16 vendor, u16 device,
462 			const struct snd_pci_quirk *list);
463 #else
464 static inline const struct snd_pci_quirk *
465 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
466 {
467 	return NULL;
468 }
469 
470 static inline const struct snd_pci_quirk *
471 snd_pci_quirk_lookup_id(u16 vendor, u16 device,
472 			const struct snd_pci_quirk *list)
473 {
474 	return NULL;
475 }
476 #endif
477 
478 /* async signal helpers */
479 struct snd_fasync;
480 
481 int snd_fasync_helper(int fd, struct file *file, int on,
482 		      struct snd_fasync **fasyncp);
483 void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll);
484 void snd_fasync_free(struct snd_fasync *fasync);
485 
486 #endif /* __SOUND_CORE_H */
487