core.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __SOUND_CORE_H
  3. #define __SOUND_CORE_H
  4. /*
  5. * Main header file for the ALSA driver
  6. * Copyright (c) 1994-2001 by Jaroslav Kysela <perex@perex.cz>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/sched.h> /* wake_up() */
  10. #include <linux/mutex.h> /* struct mutex */
  11. #include <linux/rwsem.h> /* struct rw_semaphore */
  12. #include <linux/pm.h> /* pm_message_t */
  13. #include <linux/stringify.h>
  14. #include <linux/printk.h>
  15. #include <linux/xarray.h>
  16. /* number of supported soundcards */
  17. #ifdef CONFIG_SND_DYNAMIC_MINORS
  18. #define SNDRV_CARDS CONFIG_SND_MAX_CARDS
  19. #else
  20. #define SNDRV_CARDS 8 /* don't change - minor numbers */
  21. #endif
  22. #define CONFIG_SND_MAJOR 116 /* standard configuration */
  23. /* forward declarations */
  24. struct pci_dev;
  25. struct module;
  26. struct completion;
  27. /* device allocation stuff */
  28. /* type of the object used in snd_device_*()
  29. * this also defines the calling order
  30. */
  31. enum snd_device_type {
  32. SNDRV_DEV_LOWLEVEL,
  33. SNDRV_DEV_INFO,
  34. SNDRV_DEV_BUS,
  35. SNDRV_DEV_CODEC,
  36. SNDRV_DEV_PCM,
  37. SNDRV_DEV_COMPRESS,
  38. SNDRV_DEV_RAWMIDI,
  39. SNDRV_DEV_TIMER,
  40. SNDRV_DEV_SEQUENCER,
  41. SNDRV_DEV_HWDEP,
  42. SNDRV_DEV_JACK,
  43. SNDRV_DEV_CONTROL, /* NOTE: this must be the last one */
  44. };
  45. enum snd_device_state {
  46. SNDRV_DEV_BUILD,
  47. SNDRV_DEV_REGISTERED,
  48. SNDRV_DEV_DISCONNECTED,
  49. };
  50. struct snd_device;
  51. struct snd_device_ops {
  52. int (*dev_free)(struct snd_device *dev);
  53. int (*dev_register)(struct snd_device *dev);
  54. int (*dev_disconnect)(struct snd_device *dev);
  55. };
  56. struct snd_device {
  57. struct list_head list; /* list of registered devices */
  58. struct snd_card *card; /* card which holds this device */
  59. enum snd_device_state state; /* state of the device */
  60. enum snd_device_type type; /* device type */
  61. void *device_data; /* device structure */
  62. const struct snd_device_ops *ops; /* operations */
  63. };
  64. #define snd_device(n) list_entry(n, struct snd_device, list)
  65. /* main structure for soundcard */
  66. struct snd_card {
  67. int number; /* number of soundcard (index to
  68. snd_cards) */
  69. char id[16]; /* id string of this card */
  70. char driver[16]; /* driver name */
  71. char shortname[32]; /* short name of this soundcard */
  72. char longname[80]; /* name of this soundcard */
  73. char irq_descr[32]; /* Interrupt description */
  74. char mixername[80]; /* mixer name */
  75. char components[128]; /* card components delimited with
  76. space */
  77. struct module *module; /* top-level module */
  78. void *private_data; /* private data for soundcard */
  79. void (*private_free) (struct snd_card *card); /* callback for freeing of
  80. private data */
  81. struct list_head devices; /* devices */
  82. struct device *ctl_dev; /* control device */
  83. unsigned int last_numid; /* last used numeric ID */
  84. struct rw_semaphore controls_rwsem; /* controls lock (list and values) */
  85. rwlock_t controls_rwlock; /* lock for lookup and ctl_files list */
  86. int controls_count; /* count of all controls */
  87. size_t user_ctl_alloc_size; // current memory allocation by user controls.
  88. struct list_head controls; /* all controls for this card */
  89. struct list_head ctl_files; /* active control files */
  90. #ifdef CONFIG_SND_CTL_FAST_LOOKUP
  91. struct xarray ctl_numids; /* hash table for numids */
  92. struct xarray ctl_hash; /* hash table for ctl id matching */
  93. bool ctl_hash_collision; /* ctl_hash collision seen? */
  94. #endif
  95. struct snd_info_entry *proc_root; /* root for soundcard specific files */
  96. struct proc_dir_entry *proc_root_link; /* number link to real id */
  97. struct list_head files_list; /* all files associated to this card */
  98. struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
  99. state */
  100. spinlock_t files_lock; /* lock the files for this card */
  101. int shutdown; /* this card is going down */
  102. struct completion *release_completion;
  103. struct device *dev; /* device assigned to this card */
  104. struct device card_dev; /* cardX object for sysfs */
  105. const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
  106. bool registered; /* card_dev is registered? */
  107. bool managed; /* managed via devres */
  108. bool releasing; /* during card free process */
  109. int sync_irq; /* assigned irq, used for PCM sync */
  110. wait_queue_head_t remove_sleep;
  111. size_t total_pcm_alloc_bytes; /* total amount of allocated buffers */
  112. struct mutex memory_mutex; /* protection for the above */
  113. #ifdef CONFIG_SND_DEBUG
  114. struct dentry *debugfs_root; /* debugfs root for card */
  115. #endif
  116. #ifdef CONFIG_PM
  117. unsigned int power_state; /* power state */
  118. atomic_t power_ref;
  119. wait_queue_head_t power_sleep;
  120. wait_queue_head_t power_ref_sleep;
  121. #endif
  122. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  123. struct snd_mixer_oss *mixer_oss;
  124. int mixer_oss_change_count;
  125. #endif
  126. };
  127. #define dev_to_snd_card(p) container_of(p, struct snd_card, card_dev)
  128. #ifdef CONFIG_PM
  129. static inline unsigned int snd_power_get_state(struct snd_card *card)
  130. {
  131. return READ_ONCE(card->power_state);
  132. }
  133. static inline void snd_power_change_state(struct snd_card *card, unsigned int state)
  134. {
  135. WRITE_ONCE(card->power_state, state);
  136. wake_up(&card->power_sleep);
  137. }
  138. /**
  139. * snd_power_ref - Take the reference count for power control
  140. * @card: sound card object
  141. *
  142. * The power_ref reference of the card is used for managing to block
  143. * the snd_power_sync_ref() operation. This function increments the reference.
  144. * The counterpart snd_power_unref() has to be called appropriately later.
  145. */
  146. static inline void snd_power_ref(struct snd_card *card)
  147. {
  148. atomic_inc(&card->power_ref);
  149. }
  150. /**
  151. * snd_power_unref - Release the reference count for power control
  152. * @card: sound card object
  153. */
  154. static inline void snd_power_unref(struct snd_card *card)
  155. {
  156. if (atomic_dec_and_test(&card->power_ref))
  157. wake_up(&card->power_ref_sleep);
  158. }
  159. /**
  160. * snd_power_sync_ref - wait until the card power_ref is freed
  161. * @card: sound card object
  162. *
  163. * This function is used to synchronize with the pending power_ref being
  164. * released.
  165. */
  166. static inline void snd_power_sync_ref(struct snd_card *card)
  167. {
  168. wait_event(card->power_ref_sleep, !atomic_read(&card->power_ref));
  169. }
  170. /* init.c */
  171. int snd_power_wait(struct snd_card *card);
  172. int snd_power_ref_and_wait(struct snd_card *card);
  173. #else /* ! CONFIG_PM */
  174. static inline int snd_power_wait(struct snd_card *card) { return 0; }
  175. static inline void snd_power_ref(struct snd_card *card) {}
  176. static inline void snd_power_unref(struct snd_card *card) {}
  177. static inline int snd_power_ref_and_wait(struct snd_card *card) { return 0; }
  178. static inline void snd_power_sync_ref(struct snd_card *card) {}
  179. #define snd_power_get_state(card) ({ (void)(card); SNDRV_CTL_POWER_D0; })
  180. #define snd_power_change_state(card, state) do { (void)(card); } while (0)
  181. #endif /* CONFIG_PM */
  182. struct snd_minor {
  183. int type; /* SNDRV_DEVICE_TYPE_XXX */
  184. int card; /* card number */
  185. int device; /* device number */
  186. const struct file_operations *f_ops; /* file operations */
  187. void *private_data; /* private data for f_ops->open */
  188. struct device *dev; /* device for sysfs */
  189. struct snd_card *card_ptr; /* assigned card instance */
  190. };
  191. /* return a device pointer linked to each sound device as a parent */
  192. static inline struct device *snd_card_get_device_link(struct snd_card *card)
  193. {
  194. return card ? &card->card_dev : NULL;
  195. }
  196. /* sound.c */
  197. extern int snd_major;
  198. extern int snd_ecards_limit;
  199. extern const struct class sound_class;
  200. #ifdef CONFIG_SND_DEBUG
  201. extern struct dentry *sound_debugfs_root;
  202. #endif
  203. void snd_request_card(int card);
  204. int snd_device_alloc(struct device **dev_p, struct snd_card *card);
  205. int snd_register_device(int type, struct snd_card *card, int dev,
  206. const struct file_operations *f_ops,
  207. void *private_data, struct device *device);
  208. int snd_unregister_device(struct device *dev);
  209. void *snd_lookup_minor_data(unsigned int minor, int type);
  210. #ifdef CONFIG_SND_OSSEMUL
  211. int snd_register_oss_device(int type, struct snd_card *card, int dev,
  212. const struct file_operations *f_ops, void *private_data);
  213. int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
  214. void *snd_lookup_oss_minor_data(unsigned int minor, int type);
  215. #endif
  216. int snd_minor_info_init(void);
  217. /* sound_oss.c */
  218. #ifdef CONFIG_SND_OSSEMUL
  219. int snd_minor_info_oss_init(void);
  220. #else
  221. static inline int snd_minor_info_oss_init(void) { return 0; }
  222. #endif
  223. /* memory.c */
  224. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
  225. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
  226. /* init.c */
  227. int snd_card_locked(int card);
  228. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  229. #define SND_MIXER_OSS_NOTIFY_REGISTER 0
  230. #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1
  231. #define SND_MIXER_OSS_NOTIFY_FREE 2
  232. extern int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int cmd);
  233. #endif
  234. int snd_card_new(struct device *parent, int idx, const char *xid,
  235. struct module *module, int extra_size,
  236. struct snd_card **card_ret);
  237. int snd_devm_card_new(struct device *parent, int idx, const char *xid,
  238. struct module *module, size_t extra_size,
  239. struct snd_card **card_ret);
  240. void snd_card_disconnect(struct snd_card *card);
  241. void snd_card_disconnect_sync(struct snd_card *card);
  242. void snd_card_free(struct snd_card *card);
  243. void snd_card_free_when_closed(struct snd_card *card);
  244. int snd_card_free_on_error(struct device *dev, int ret);
  245. void snd_card_set_id(struct snd_card *card, const char *id);
  246. int snd_card_register(struct snd_card *card);
  247. int snd_card_info_init(void);
  248. int snd_card_add_dev_attr(struct snd_card *card,
  249. const struct attribute_group *group);
  250. int snd_component_add(struct snd_card *card, const char *component);
  251. int snd_card_file_add(struct snd_card *card, struct file *file);
  252. int snd_card_file_remove(struct snd_card *card, struct file *file);
  253. struct snd_card *snd_card_ref(int card);
  254. /**
  255. * snd_card_unref - Unreference the card object
  256. * @card: the card object to unreference
  257. *
  258. * Call this function for the card object that was obtained via snd_card_ref()
  259. * or snd_lookup_minor_data().
  260. */
  261. static inline void snd_card_unref(struct snd_card *card)
  262. {
  263. put_device(&card->card_dev);
  264. }
  265. #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
  266. /* device.c */
  267. int snd_device_new(struct snd_card *card, enum snd_device_type type,
  268. void *device_data, const struct snd_device_ops *ops);
  269. int snd_device_register(struct snd_card *card, void *device_data);
  270. int snd_device_register_all(struct snd_card *card);
  271. void snd_device_disconnect(struct snd_card *card, void *device_data);
  272. void snd_device_disconnect_all(struct snd_card *card);
  273. void snd_device_free(struct snd_card *card, void *device_data);
  274. void snd_device_free_all(struct snd_card *card);
  275. /* isadma.c */
  276. #ifdef CONFIG_ISA_DMA_API
  277. #define DMA_MODE_NO_ENABLE 0x0100
  278. void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
  279. void snd_dma_disable(unsigned long dma);
  280. unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
  281. int snd_devm_request_dma(struct device *dev, int dma, const char *name);
  282. #endif
  283. /* misc.c */
  284. struct resource;
  285. void release_and_free_resource(struct resource *res);
  286. /* --- */
  287. #ifdef CONFIG_SND_DEBUG
  288. /**
  289. * snd_BUG - give a BUG warning message and stack trace
  290. *
  291. * Calls WARN() if CONFIG_SND_DEBUG is set.
  292. * Ignored when CONFIG_SND_DEBUG is not set.
  293. */
  294. #define snd_BUG() WARN(1, "BUG?\n")
  295. /**
  296. * snd_BUG_ON - debugging check macro
  297. * @cond: condition to evaluate
  298. *
  299. * Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set,
  300. * otherwise just evaluates the conditional and returns the value.
  301. */
  302. #define snd_BUG_ON(cond) WARN_ON((cond))
  303. #else /* !CONFIG_SND_DEBUG */
  304. #define snd_BUG() do { } while (0)
  305. #define snd_BUG_ON(condition) ({ \
  306. int __ret_warn_on = !!(condition); \
  307. unlikely(__ret_warn_on); \
  308. })
  309. #endif /* CONFIG_SND_DEBUG */
  310. #define SNDRV_OSS_VERSION ((3<<16)|(8<<8)|(1<<4)|(0)) /* 3.8.1a */
  311. /* for easier backward-porting */
  312. #if IS_ENABLED(CONFIG_GAMEPORT)
  313. #define gameport_set_dev_parent(gp,xdev) ((gp)->dev.parent = (xdev))
  314. #define gameport_set_port_data(gp,r) ((gp)->port_data = (r))
  315. #define gameport_get_port_data(gp) (gp)->port_data
  316. #endif
  317. /* PCI quirk list helper */
  318. struct snd_pci_quirk {
  319. unsigned short subvendor; /* PCI subvendor ID */
  320. unsigned short subdevice; /* PCI subdevice ID */
  321. unsigned short subdevice_mask; /* bitmask to match */
  322. int value; /* value */
  323. #ifdef CONFIG_SND_DEBUG_VERBOSE
  324. const char *name; /* name of the device (optional) */
  325. #endif
  326. };
  327. #define _SND_PCI_QUIRK_ID_MASK(vend, mask, dev) \
  328. .subvendor = (vend), .subdevice = (dev), .subdevice_mask = (mask)
  329. #define _SND_PCI_QUIRK_ID(vend, dev) \
  330. _SND_PCI_QUIRK_ID_MASK(vend, 0xffff, dev)
  331. #define SND_PCI_QUIRK_ID(vend,dev) {_SND_PCI_QUIRK_ID(vend, dev)}
  332. #ifdef CONFIG_SND_DEBUG_VERBOSE
  333. #define SND_PCI_QUIRK(vend,dev,xname,val) \
  334. {_SND_PCI_QUIRK_ID(vend, dev), .value = (val), .name = (xname)}
  335. #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
  336. {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val), .name = (xname)}
  337. #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
  338. {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), \
  339. .value = (val), .name = (xname)}
  340. #define snd_pci_quirk_name(q) ((q)->name)
  341. #else
  342. #define SND_PCI_QUIRK(vend,dev,xname,val) \
  343. {_SND_PCI_QUIRK_ID(vend, dev), .value = (val)}
  344. #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
  345. {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), .value = (val)}
  346. #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
  347. {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val)}
  348. #define snd_pci_quirk_name(q) ""
  349. #endif
  350. #ifdef CONFIG_PCI
  351. const struct snd_pci_quirk *
  352. snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list);
  353. const struct snd_pci_quirk *
  354. snd_pci_quirk_lookup_id(u16 vendor, u16 device,
  355. const struct snd_pci_quirk *list);
  356. #else
  357. static inline const struct snd_pci_quirk *
  358. snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
  359. {
  360. return NULL;
  361. }
  362. static inline const struct snd_pci_quirk *
  363. snd_pci_quirk_lookup_id(u16 vendor, u16 device,
  364. const struct snd_pci_quirk *list)
  365. {
  366. return NULL;
  367. }
  368. #endif
  369. /* async signal helpers */
  370. struct snd_fasync;
  371. int snd_fasync_helper(int fd, struct file *file, int on,
  372. struct snd_fasync **fasyncp);
  373. void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll);
  374. void snd_fasync_free(struct snd_fasync *fasync);
  375. #endif /* __SOUND_CORE_H */