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