core.h 16 KB

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