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 153 #define dev_to_snd_card(p) container_of(p, struct snd_card, card_dev) 154 155 #ifdef CONFIG_PM 156 static inline unsigned int snd_power_get_state(struct snd_card *card) 157 { 158 return READ_ONCE(card->power_state); 159 } 160 161 static inline void snd_power_change_state(struct snd_card *card, unsigned int state) 162 { 163 WRITE_ONCE(card->power_state, state); 164 wake_up(&card->power_sleep); 165 } 166 167 /** 168 * snd_power_ref - Take the reference count for power control 169 * @card: sound card object 170 * 171 * The power_ref reference of the card is used for managing to block 172 * the snd_power_sync_ref() operation. This function increments the reference. 173 * The counterpart snd_power_unref() has to be called appropriately later. 174 */ 175 static inline void snd_power_ref(struct snd_card *card) 176 { 177 atomic_inc(&card->power_ref); 178 } 179 180 /** 181 * snd_power_unref - Release the reference count for power control 182 * @card: sound card object 183 */ 184 static inline void snd_power_unref(struct snd_card *card) 185 { 186 if (atomic_dec_and_test(&card->power_ref)) 187 wake_up(&card->power_ref_sleep); 188 } 189 190 /** 191 * snd_power_sync_ref - wait until the card power_ref is freed 192 * @card: sound card object 193 * 194 * This function is used to synchronize with the pending power_ref being 195 * released. 196 */ 197 static inline void snd_power_sync_ref(struct snd_card *card) 198 { 199 wait_event(card->power_ref_sleep, !atomic_read(&card->power_ref)); 200 } 201 202 /* init.c */ 203 int snd_power_wait(struct snd_card *card); 204 int snd_power_ref_and_wait(struct snd_card *card); 205 206 #else /* ! CONFIG_PM */ 207 208 static inline int snd_power_wait(struct snd_card *card) { return 0; } 209 static inline void snd_power_ref(struct snd_card *card) {} 210 static inline void snd_power_unref(struct snd_card *card) {} 211 static inline int snd_power_ref_and_wait(struct snd_card *card) { return 0; } 212 static inline void snd_power_sync_ref(struct snd_card *card) {} 213 #define snd_power_get_state(card) ({ (void)(card); SNDRV_CTL_POWER_D0; }) 214 #define snd_power_change_state(card, state) do { (void)(card); } while (0) 215 216 #endif /* CONFIG_PM */ 217 218 struct snd_minor { 219 int type; /* SNDRV_DEVICE_TYPE_XXX */ 220 int card; /* card number */ 221 int device; /* device number */ 222 const struct file_operations *f_ops; /* file operations */ 223 void *private_data; /* private data for f_ops->open */ 224 struct device *dev; /* device for sysfs */ 225 struct snd_card *card_ptr; /* assigned card instance */ 226 }; 227 228 /* return a device pointer linked to each sound device as a parent */ 229 static inline struct device *snd_card_get_device_link(struct snd_card *card) 230 { 231 return card ? &card->card_dev : NULL; 232 } 233 234 /* sound.c */ 235 236 extern int snd_major; 237 extern int snd_ecards_limit; 238 extern const struct class sound_class; 239 #ifdef CONFIG_SND_DEBUG 240 extern struct dentry *sound_debugfs_root; 241 #endif 242 243 void snd_request_card(int card); 244 245 int snd_device_alloc(struct device **dev_p, struct snd_card *card); 246 247 int snd_register_device(int type, struct snd_card *card, int dev, 248 const struct file_operations *f_ops, 249 void *private_data, struct device *device); 250 int snd_unregister_device(struct device *dev); 251 void *snd_lookup_minor_data(unsigned int minor, int type); 252 253 #ifdef CONFIG_SND_OSSEMUL 254 int snd_register_oss_device(int type, struct snd_card *card, int dev, 255 const struct file_operations *f_ops, void *private_data); 256 int snd_unregister_oss_device(int type, struct snd_card *card, int dev); 257 void *snd_lookup_oss_minor_data(unsigned int minor, int type); 258 #endif 259 260 int snd_minor_info_init(void); 261 262 /* sound_oss.c */ 263 264 #ifdef CONFIG_SND_OSSEMUL 265 int snd_minor_info_oss_init(void); 266 #else 267 static inline int snd_minor_info_oss_init(void) { return 0; } 268 #endif 269 270 /* memory.c */ 271 272 int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count); 273 int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count); 274 275 /* init.c */ 276 277 int snd_card_locked(int card); 278 #if IS_ENABLED(CONFIG_SND_MIXER_OSS) 279 #define SND_MIXER_OSS_NOTIFY_REGISTER 0 280 #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1 281 #define SND_MIXER_OSS_NOTIFY_FREE 2 282 extern int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int cmd); 283 #endif 284 285 int snd_card_new(struct device *parent, int idx, const char *xid, 286 struct module *module, int extra_size, 287 struct snd_card **card_ret); 288 int snd_devm_card_new(struct device *parent, int idx, const char *xid, 289 struct module *module, size_t extra_size, 290 struct snd_card **card_ret); 291 292 void snd_card_disconnect(struct snd_card *card); 293 void snd_card_disconnect_sync(struct snd_card *card); 294 void snd_card_free(struct snd_card *card); 295 void snd_card_free_when_closed(struct snd_card *card); 296 int snd_card_free_on_error(struct device *dev, int ret); 297 void snd_card_set_id(struct snd_card *card, const char *id); 298 int snd_card_register(struct snd_card *card); 299 int snd_card_info_init(void); 300 int snd_card_add_dev_attr(struct snd_card *card, 301 const struct attribute_group *group); 302 int snd_component_add(struct snd_card *card, const char *component); 303 int snd_card_file_add(struct snd_card *card, struct file *file); 304 int snd_card_file_remove(struct snd_card *card, struct file *file); 305 306 struct snd_card *snd_card_ref(int card); 307 308 /** 309 * snd_card_unref - Unreference the card object 310 * @card: the card object to unreference 311 * 312 * Call this function for the card object that was obtained via snd_card_ref() 313 * or snd_lookup_minor_data(). 314 */ 315 static inline void snd_card_unref(struct snd_card *card) 316 { 317 put_device(&card->card_dev); 318 } 319 320 #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr)) 321 322 /* device.c */ 323 324 int snd_device_new(struct snd_card *card, enum snd_device_type type, 325 void *device_data, const struct snd_device_ops *ops); 326 int snd_device_register(struct snd_card *card, void *device_data); 327 int snd_device_register_all(struct snd_card *card); 328 void snd_device_disconnect(struct snd_card *card, void *device_data); 329 void snd_device_disconnect_all(struct snd_card *card); 330 void snd_device_free(struct snd_card *card, void *device_data); 331 void snd_device_free_all(struct snd_card *card); 332 333 /* isadma.c */ 334 335 #ifdef CONFIG_ISA_DMA_API 336 #define DMA_MODE_NO_ENABLE 0x0100 337 338 void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode); 339 void snd_dma_disable(unsigned long dma); 340 unsigned int snd_dma_pointer(unsigned long dma, unsigned int size); 341 int snd_devm_request_dma(struct device *dev, int dma, const char *name); 342 #endif 343 344 /* misc.c */ 345 struct resource; 346 void release_and_free_resource(struct resource *res); 347 348 /* --- */ 349 350 #ifdef CONFIG_SND_DEBUG 351 /** 352 * snd_BUG - give a BUG warning message and stack trace 353 * 354 * Calls WARN() if CONFIG_SND_DEBUG is set. 355 * Ignored when CONFIG_SND_DEBUG is not set. 356 */ 357 #define snd_BUG() WARN(1, "BUG?\n") 358 359 /** 360 * snd_BUG_ON - debugging check macro 361 * @cond: condition to evaluate 362 * 363 * Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set, 364 * otherwise just evaluates the conditional and returns the value. 365 */ 366 #define snd_BUG_ON(cond) WARN_ON((cond)) 367 368 #else /* !CONFIG_SND_DEBUG */ 369 370 #define snd_BUG() do { } while (0) 371 372 #define snd_BUG_ON(condition) ({ \ 373 int __ret_warn_on = !!(condition); \ 374 unlikely(__ret_warn_on); \ 375 }) 376 377 #endif /* CONFIG_SND_DEBUG */ 378 379 #define SNDRV_OSS_VERSION ((3<<16)|(8<<8)|(1<<4)|(0)) /* 3.8.1a */ 380 381 /* for easier backward-porting */ 382 #if IS_ENABLED(CONFIG_GAMEPORT) 383 #define gameport_set_dev_parent(gp,xdev) ((gp)->dev.parent = (xdev)) 384 #define gameport_set_port_data(gp,r) ((gp)->port_data = (r)) 385 #define gameport_get_port_data(gp) (gp)->port_data 386 #endif 387 388 /* PCI quirk list helper */ 389 struct snd_pci_quirk { 390 unsigned short subvendor; /* PCI subvendor ID */ 391 unsigned short subdevice; /* PCI subdevice ID */ 392 unsigned short subdevice_mask; /* bitmask to match */ 393 int value; /* value */ 394 #ifdef CONFIG_SND_DEBUG_VERBOSE 395 const char *name; /* name of the device (optional) */ 396 #endif 397 }; 398 399 #define _SND_PCI_QUIRK_ID_MASK(vend, mask, dev) \ 400 .subvendor = (vend), .subdevice = (dev), .subdevice_mask = (mask) 401 #define _SND_PCI_QUIRK_ID(vend, dev) \ 402 _SND_PCI_QUIRK_ID_MASK(vend, 0xffff, dev) 403 #define SND_PCI_QUIRK_ID(vend,dev) {_SND_PCI_QUIRK_ID(vend, dev)} 404 #ifdef CONFIG_SND_DEBUG_VERBOSE 405 #define SND_PCI_QUIRK(vend,dev,xname,val) \ 406 {_SND_PCI_QUIRK_ID(vend, dev), .value = (val), .name = (xname)} 407 #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \ 408 {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val), .name = (xname)} 409 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \ 410 {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), \ 411 .value = (val), .name = (xname)} 412 #define snd_pci_quirk_name(q) ((q)->name) 413 #else 414 #define SND_PCI_QUIRK(vend,dev,xname,val) \ 415 {_SND_PCI_QUIRK_ID(vend, dev), .value = (val)} 416 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \ 417 {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), .value = (val)} 418 #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \ 419 {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val)} 420 #define snd_pci_quirk_name(q) "" 421 #endif 422 423 #ifdef CONFIG_PCI 424 const struct snd_pci_quirk * 425 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list); 426 427 const struct snd_pci_quirk * 428 snd_pci_quirk_lookup_id(u16 vendor, u16 device, 429 const struct snd_pci_quirk *list); 430 #else 431 static inline const struct snd_pci_quirk * 432 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list) 433 { 434 return NULL; 435 } 436 437 static inline const struct snd_pci_quirk * 438 snd_pci_quirk_lookup_id(u16 vendor, u16 device, 439 const struct snd_pci_quirk *list) 440 { 441 return NULL; 442 } 443 #endif 444 445 /* async signal helpers */ 446 struct snd_fasync; 447 448 int snd_fasync_helper(int fd, struct file *file, int on, 449 struct snd_fasync **fasyncp); 450 void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll); 451 void snd_fasync_free(struct snd_fasync *fasync); 452 453 #endif /* __SOUND_CORE_H */ 454