Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 #ifndef __SOUND_CORE_H
0003 #define __SOUND_CORE_H
0004 
0005 /*
0006  *  Main header file for the ALSA driver
0007  *  Copyright (c) 1994-2001 by Jaroslav Kysela <perex@perex.cz>
0008  */
0009 
0010 #include <linux/device.h>
0011 #include <linux/sched.h>        /* wake_up() */
0012 #include <linux/mutex.h>        /* struct mutex */
0013 #include <linux/rwsem.h>        /* struct rw_semaphore */
0014 #include <linux/pm.h>           /* pm_message_t */
0015 #include <linux/stringify.h>
0016 #include <linux/printk.h>
0017 #include <linux/xarray.h>
0018 
0019 /* number of supported soundcards */
0020 #ifdef CONFIG_SND_DYNAMIC_MINORS
0021 #define SNDRV_CARDS CONFIG_SND_MAX_CARDS
0022 #else
0023 #define SNDRV_CARDS 8       /* don't change - minor numbers */
0024 #endif
0025 
0026 #define CONFIG_SND_MAJOR    116 /* standard configuration */
0027 
0028 /* forward declarations */
0029 struct pci_dev;
0030 struct module;
0031 struct completion;
0032 
0033 /* device allocation stuff */
0034 
0035 /* type of the object used in snd_device_*()
0036  * this also defines the calling order
0037  */
0038 enum snd_device_type {
0039     SNDRV_DEV_LOWLEVEL,
0040     SNDRV_DEV_INFO,
0041     SNDRV_DEV_BUS,
0042     SNDRV_DEV_CODEC,
0043     SNDRV_DEV_PCM,
0044     SNDRV_DEV_COMPRESS,
0045     SNDRV_DEV_RAWMIDI,
0046     SNDRV_DEV_TIMER,
0047     SNDRV_DEV_SEQUENCER,
0048     SNDRV_DEV_HWDEP,
0049     SNDRV_DEV_JACK,
0050     SNDRV_DEV_CONTROL,  /* NOTE: this must be the last one */
0051 };
0052 
0053 enum snd_device_state {
0054     SNDRV_DEV_BUILD,
0055     SNDRV_DEV_REGISTERED,
0056     SNDRV_DEV_DISCONNECTED,
0057 };
0058 
0059 struct snd_device;
0060 
0061 struct snd_device_ops {
0062     int (*dev_free)(struct snd_device *dev);
0063     int (*dev_register)(struct snd_device *dev);
0064     int (*dev_disconnect)(struct snd_device *dev);
0065 };
0066 
0067 struct snd_device {
0068     struct list_head list;      /* list of registered devices */
0069     struct snd_card *card;      /* card which holds this device */
0070     enum snd_device_state state;    /* state of the device */
0071     enum snd_device_type type;  /* device type */
0072     void *device_data;      /* device structure */
0073     const struct snd_device_ops *ops;   /* operations */
0074 };
0075 
0076 #define snd_device(n) list_entry(n, struct snd_device, list)
0077 
0078 /* main structure for soundcard */
0079 
0080 struct snd_card {
0081     int number;         /* number of soundcard (index to
0082                                 snd_cards) */
0083 
0084     char id[16];            /* id string of this card */
0085     char driver[16];        /* driver name */
0086     char shortname[32];     /* short name of this soundcard */
0087     char longname[80];      /* name of this soundcard */
0088     char irq_descr[32];     /* Interrupt description */
0089     char mixername[80];     /* mixer name */
0090     char components[128];       /* card components delimited with
0091                                 space */
0092     struct module *module;      /* top-level module */
0093 
0094     void *private_data;     /* private data for soundcard */
0095     void (*private_free) (struct snd_card *card); /* callback for freeing of
0096                                 private data */
0097     struct list_head devices;   /* devices */
0098 
0099     struct device ctl_dev;      /* control device */
0100     unsigned int last_numid;    /* last used numeric ID */
0101     struct rw_semaphore controls_rwsem; /* controls list lock */
0102     rwlock_t ctl_files_rwlock;  /* ctl_files list lock */
0103     int controls_count;     /* count of all controls */
0104     size_t user_ctl_alloc_size; // current memory allocation by user controls.
0105     struct list_head controls;  /* all controls for this card */
0106     struct list_head ctl_files; /* active control files */
0107 #ifdef CONFIG_SND_CTL_FAST_LOOKUP
0108     struct xarray ctl_numids;   /* hash table for numids */
0109     struct xarray ctl_hash;     /* hash table for ctl id matching */
0110     bool ctl_hash_collision;    /* ctl_hash collision seen? */
0111 #endif
0112 
0113     struct snd_info_entry *proc_root;   /* root for soundcard specific files */
0114     struct proc_dir_entry *proc_root_link;  /* number link to real id */
0115 
0116     struct list_head files_list;    /* all files associated to this card */
0117     struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
0118                                 state */
0119     spinlock_t files_lock;      /* lock the files for this card */
0120     int shutdown;           /* this card is going down */
0121     struct completion *release_completion;
0122     struct device *dev;     /* device assigned to this card */
0123     struct device card_dev;     /* cardX object for sysfs */
0124     const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
0125     bool registered;        /* card_dev is registered? */
0126     bool managed;           /* managed via devres */
0127     bool releasing;         /* during card free process */
0128     int sync_irq;           /* assigned irq, used for PCM sync */
0129     wait_queue_head_t remove_sleep;
0130 
0131     size_t total_pcm_alloc_bytes;   /* total amount of allocated buffers */
0132     struct mutex memory_mutex;  /* protection for the above */
0133 #ifdef CONFIG_SND_DEBUG
0134     struct dentry *debugfs_root;    /* debugfs root for card */
0135 #endif
0136 
0137 #ifdef CONFIG_PM
0138     unsigned int power_state;   /* power state */
0139     atomic_t power_ref;
0140     wait_queue_head_t power_sleep;
0141     wait_queue_head_t power_ref_sleep;
0142 #endif
0143 
0144 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
0145     struct snd_mixer_oss *mixer_oss;
0146     int mixer_oss_change_count;
0147 #endif
0148 };
0149 
0150 #define dev_to_snd_card(p)  container_of(p, struct snd_card, card_dev)
0151 
0152 #ifdef CONFIG_PM
0153 static inline unsigned int snd_power_get_state(struct snd_card *card)
0154 {
0155     return READ_ONCE(card->power_state);
0156 }
0157 
0158 static inline void snd_power_change_state(struct snd_card *card, unsigned int state)
0159 {
0160     WRITE_ONCE(card->power_state, state);
0161     wake_up(&card->power_sleep);
0162 }
0163 
0164 /**
0165  * snd_power_ref - Take the reference count for power control
0166  * @card: sound card object
0167  *
0168  * The power_ref reference of the card is used for managing to block
0169  * the snd_power_sync_ref() operation.  This function increments the reference.
0170  * The counterpart snd_power_unref() has to be called appropriately later.
0171  */
0172 static inline void snd_power_ref(struct snd_card *card)
0173 {
0174     atomic_inc(&card->power_ref);
0175 }
0176 
0177 /**
0178  * snd_power_unref - Release the reference count for power control
0179  * @card: sound card object
0180  */
0181 static inline void snd_power_unref(struct snd_card *card)
0182 {
0183     if (atomic_dec_and_test(&card->power_ref))
0184         wake_up(&card->power_ref_sleep);
0185 }
0186 
0187 /**
0188  * snd_power_sync_ref - wait until the card power_ref is freed
0189  * @card: sound card object
0190  *
0191  * This function is used to synchronize with the pending power_ref being
0192  * released.
0193  */
0194 static inline void snd_power_sync_ref(struct snd_card *card)
0195 {
0196     wait_event(card->power_ref_sleep, !atomic_read(&card->power_ref));
0197 }
0198 
0199 /* init.c */
0200 int snd_power_wait(struct snd_card *card);
0201 int snd_power_ref_and_wait(struct snd_card *card);
0202 
0203 #else /* ! CONFIG_PM */
0204 
0205 static inline int snd_power_wait(struct snd_card *card) { return 0; }
0206 static inline void snd_power_ref(struct snd_card *card) {}
0207 static inline void snd_power_unref(struct snd_card *card) {}
0208 static inline int snd_power_ref_and_wait(struct snd_card *card) { return 0; }
0209 static inline void snd_power_sync_ref(struct snd_card *card) {}
0210 #define snd_power_get_state(card)   ({ (void)(card); SNDRV_CTL_POWER_D0; })
0211 #define snd_power_change_state(card, state) do { (void)(card); } while (0)
0212 
0213 #endif /* CONFIG_PM */
0214 
0215 struct snd_minor {
0216     int type;           /* SNDRV_DEVICE_TYPE_XXX */
0217     int card;           /* card number */
0218     int device;         /* device number */
0219     const struct file_operations *f_ops;    /* file operations */
0220     void *private_data;     /* private data for f_ops->open */
0221     struct device *dev;     /* device for sysfs */
0222     struct snd_card *card_ptr;  /* assigned card instance */
0223 };
0224 
0225 /* return a device pointer linked to each sound device as a parent */
0226 static inline struct device *snd_card_get_device_link(struct snd_card *card)
0227 {
0228     return card ? &card->card_dev : NULL;
0229 }
0230 
0231 /* sound.c */
0232 
0233 extern int snd_major;
0234 extern int snd_ecards_limit;
0235 extern struct class *sound_class;
0236 #ifdef CONFIG_SND_DEBUG
0237 extern struct dentry *sound_debugfs_root;
0238 #endif
0239 
0240 void snd_request_card(int card);
0241 
0242 void snd_device_initialize(struct device *dev, struct snd_card *card);
0243 
0244 int snd_register_device(int type, struct snd_card *card, int dev,
0245             const struct file_operations *f_ops,
0246             void *private_data, struct device *device);
0247 int snd_unregister_device(struct device *dev);
0248 void *snd_lookup_minor_data(unsigned int minor, int type);
0249 
0250 #ifdef CONFIG_SND_OSSEMUL
0251 int snd_register_oss_device(int type, struct snd_card *card, int dev,
0252                 const struct file_operations *f_ops, void *private_data);
0253 int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
0254 void *snd_lookup_oss_minor_data(unsigned int minor, int type);
0255 #endif
0256 
0257 int snd_minor_info_init(void);
0258 
0259 /* sound_oss.c */
0260 
0261 #ifdef CONFIG_SND_OSSEMUL
0262 int snd_minor_info_oss_init(void);
0263 #else
0264 static inline int snd_minor_info_oss_init(void) { return 0; }
0265 #endif
0266 
0267 /* memory.c */
0268 
0269 int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
0270 int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
0271 
0272 /* init.c */
0273 
0274 int snd_card_locked(int card);
0275 #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
0276 #define SND_MIXER_OSS_NOTIFY_REGISTER   0
0277 #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1
0278 #define SND_MIXER_OSS_NOTIFY_FREE   2
0279 extern int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int cmd);
0280 #endif
0281 
0282 int snd_card_new(struct device *parent, int idx, const char *xid,
0283          struct module *module, int extra_size,
0284          struct snd_card **card_ret);
0285 int snd_devm_card_new(struct device *parent, int idx, const char *xid,
0286               struct module *module, size_t extra_size,
0287               struct snd_card **card_ret);
0288 
0289 int snd_card_disconnect(struct snd_card *card);
0290 void snd_card_disconnect_sync(struct snd_card *card);
0291 int snd_card_free(struct snd_card *card);
0292 int snd_card_free_when_closed(struct snd_card *card);
0293 int snd_card_free_on_error(struct device *dev, int ret);
0294 void snd_card_set_id(struct snd_card *card, const char *id);
0295 int snd_card_register(struct snd_card *card);
0296 int snd_card_info_init(void);
0297 int snd_card_add_dev_attr(struct snd_card *card,
0298               const struct attribute_group *group);
0299 int snd_component_add(struct snd_card *card, const char *component);
0300 int snd_card_file_add(struct snd_card *card, struct file *file);
0301 int snd_card_file_remove(struct snd_card *card, struct file *file);
0302 
0303 struct snd_card *snd_card_ref(int card);
0304 
0305 /**
0306  * snd_card_unref - Unreference the card object
0307  * @card: the card object to unreference
0308  *
0309  * Call this function for the card object that was obtained via snd_card_ref()
0310  * or snd_lookup_minor_data().
0311  */
0312 static inline void snd_card_unref(struct snd_card *card)
0313 {
0314     put_device(&card->card_dev);
0315 }
0316 
0317 #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
0318 
0319 /* device.c */
0320 
0321 int snd_device_new(struct snd_card *card, enum snd_device_type type,
0322            void *device_data, const struct snd_device_ops *ops);
0323 int snd_device_register(struct snd_card *card, void *device_data);
0324 int snd_device_register_all(struct snd_card *card);
0325 void snd_device_disconnect(struct snd_card *card, void *device_data);
0326 void snd_device_disconnect_all(struct snd_card *card);
0327 void snd_device_free(struct snd_card *card, void *device_data);
0328 void snd_device_free_all(struct snd_card *card);
0329 int snd_device_get_state(struct snd_card *card, void *device_data);
0330 
0331 /* isadma.c */
0332 
0333 #ifdef CONFIG_ISA_DMA_API
0334 #define DMA_MODE_NO_ENABLE  0x0100
0335 
0336 void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
0337 void snd_dma_disable(unsigned long dma);
0338 unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
0339 int snd_devm_request_dma(struct device *dev, int dma, const char *name);
0340 #endif
0341 
0342 /* misc.c */
0343 struct resource;
0344 void release_and_free_resource(struct resource *res);
0345 
0346 /* --- */
0347 
0348 /* sound printk debug levels */
0349 enum {
0350     SND_PR_ALWAYS,
0351     SND_PR_DEBUG,
0352     SND_PR_VERBOSE,
0353 };
0354 
0355 #if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
0356 __printf(4, 5)
0357 void __snd_printk(unsigned int level, const char *file, int line,
0358           const char *format, ...);
0359 #else
0360 #define __snd_printk(level, file, line, format, ...) \
0361     printk(format, ##__VA_ARGS__)
0362 #endif
0363 
0364 /**
0365  * snd_printk - printk wrapper
0366  * @fmt: format string
0367  *
0368  * Works like printk() but prints the file and the line of the caller
0369  * when configured with CONFIG_SND_VERBOSE_PRINTK.
0370  */
0371 #define snd_printk(fmt, ...) \
0372     __snd_printk(0, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
0373 
0374 #ifdef CONFIG_SND_DEBUG
0375 /**
0376  * snd_printd - debug printk
0377  * @fmt: format string
0378  *
0379  * Works like snd_printk() for debugging purposes.
0380  * Ignored when CONFIG_SND_DEBUG is not set.
0381  */
0382 #define snd_printd(fmt, ...) \
0383     __snd_printk(1, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
0384 #define _snd_printd(level, fmt, ...) \
0385     __snd_printk(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
0386 
0387 /**
0388  * snd_BUG - give a BUG warning message and stack trace
0389  *
0390  * Calls WARN() if CONFIG_SND_DEBUG is set.
0391  * Ignored when CONFIG_SND_DEBUG is not set.
0392  */
0393 #define snd_BUG()       WARN(1, "BUG?\n")
0394 
0395 /**
0396  * snd_printd_ratelimit - Suppress high rates of output when
0397  *            CONFIG_SND_DEBUG is enabled.
0398  */
0399 #define snd_printd_ratelimit() printk_ratelimit()
0400 
0401 /**
0402  * snd_BUG_ON - debugging check macro
0403  * @cond: condition to evaluate
0404  *
0405  * Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set,
0406  * otherwise just evaluates the conditional and returns the value.
0407  */
0408 #define snd_BUG_ON(cond)    WARN_ON((cond))
0409 
0410 #else /* !CONFIG_SND_DEBUG */
0411 
0412 __printf(1, 2)
0413 static inline void snd_printd(const char *format, ...) {}
0414 __printf(2, 3)
0415 static inline void _snd_printd(int level, const char *format, ...) {}
0416 
0417 #define snd_BUG()           do { } while (0)
0418 
0419 #define snd_BUG_ON(condition) ({ \
0420     int __ret_warn_on = !!(condition); \
0421     unlikely(__ret_warn_on); \
0422 })
0423 
0424 static inline bool snd_printd_ratelimit(void) { return false; }
0425 
0426 #endif /* CONFIG_SND_DEBUG */
0427 
0428 #ifdef CONFIG_SND_DEBUG_VERBOSE
0429 /**
0430  * snd_printdd - debug printk
0431  * @format: format string
0432  *
0433  * Works like snd_printk() for debugging purposes.
0434  * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
0435  */
0436 #define snd_printdd(format, ...) \
0437     __snd_printk(2, __FILE__, __LINE__, format, ##__VA_ARGS__)
0438 #else
0439 __printf(1, 2)
0440 static inline void snd_printdd(const char *format, ...) {}
0441 #endif
0442 
0443 
0444 #define SNDRV_OSS_VERSION         ((3<<16)|(8<<8)|(1<<4)|(0))   /* 3.8.1a */
0445 
0446 /* for easier backward-porting */
0447 #if IS_ENABLED(CONFIG_GAMEPORT)
0448 #define gameport_set_dev_parent(gp,xdev) ((gp)->dev.parent = (xdev))
0449 #define gameport_set_port_data(gp,r) ((gp)->port_data = (r))
0450 #define gameport_get_port_data(gp) (gp)->port_data
0451 #endif
0452 
0453 /* PCI quirk list helper */
0454 struct snd_pci_quirk {
0455     unsigned short subvendor;   /* PCI subvendor ID */
0456     unsigned short subdevice;   /* PCI subdevice ID */
0457     unsigned short subdevice_mask;  /* bitmask to match */
0458     int value;          /* value */
0459 #ifdef CONFIG_SND_DEBUG_VERBOSE
0460     const char *name;       /* name of the device (optional) */
0461 #endif
0462 };
0463 
0464 #define _SND_PCI_QUIRK_ID_MASK(vend, mask, dev) \
0465     .subvendor = (vend), .subdevice = (dev), .subdevice_mask = (mask)
0466 #define _SND_PCI_QUIRK_ID(vend, dev) \
0467     _SND_PCI_QUIRK_ID_MASK(vend, 0xffff, dev)
0468 #define SND_PCI_QUIRK_ID(vend,dev) {_SND_PCI_QUIRK_ID(vend, dev)}
0469 #ifdef CONFIG_SND_DEBUG_VERBOSE
0470 #define SND_PCI_QUIRK(vend,dev,xname,val) \
0471     {_SND_PCI_QUIRK_ID(vend, dev), .value = (val), .name = (xname)}
0472 #define SND_PCI_QUIRK_VENDOR(vend, xname, val)          \
0473     {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val), .name = (xname)}
0474 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val)         \
0475     {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev),           \
0476             .value = (val), .name = (xname)}
0477 #define snd_pci_quirk_name(q)   ((q)->name)
0478 #else
0479 #define SND_PCI_QUIRK(vend,dev,xname,val) \
0480     {_SND_PCI_QUIRK_ID(vend, dev), .value = (val)}
0481 #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val)         \
0482     {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), .value = (val)}
0483 #define SND_PCI_QUIRK_VENDOR(vend, xname, val)          \
0484     {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val)}
0485 #define snd_pci_quirk_name(q)   ""
0486 #endif
0487 
0488 #ifdef CONFIG_PCI
0489 const struct snd_pci_quirk *
0490 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list);
0491 
0492 const struct snd_pci_quirk *
0493 snd_pci_quirk_lookup_id(u16 vendor, u16 device,
0494             const struct snd_pci_quirk *list);
0495 #else
0496 static inline const struct snd_pci_quirk *
0497 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
0498 {
0499     return NULL;
0500 }
0501 
0502 static inline const struct snd_pci_quirk *
0503 snd_pci_quirk_lookup_id(u16 vendor, u16 device,
0504             const struct snd_pci_quirk *list)
0505 {
0506     return NULL;
0507 }
0508 #endif
0509 
0510 /* async signal helpers */
0511 struct snd_fasync;
0512 
0513 int snd_fasync_helper(int fd, struct file *file, int on,
0514               struct snd_fasync **fasyncp);
0515 void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll);
0516 void snd_fasync_free(struct snd_fasync *fasync);
0517 
0518 #endif /* __SOUND_CORE_H */