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