core.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #ifndef __SOUND_CORE_H
  2. #define __SOUND_CORE_H
  3. /*
  4. * Main header file for the ALSA driver
  5. * Copyright (c) 1994-2001 by Jaroslav Kysela <perex@perex.cz>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/device.h>
  24. #include <linux/sched.h> /* wake_up() */
  25. #include <linux/mutex.h> /* struct mutex */
  26. #include <linux/rwsem.h> /* struct rw_semaphore */
  27. #include <linux/pm.h> /* pm_message_t */
  28. #include <linux/stringify.h>
  29. #include <linux/printk.h>
  30. /* number of supported soundcards */
  31. #ifdef CONFIG_SND_DYNAMIC_MINORS
  32. #define SNDRV_CARDS CONFIG_SND_MAX_CARDS
  33. #else
  34. #define SNDRV_CARDS 8 /* don't change - minor numbers */
  35. #endif
  36. #define CONFIG_SND_MAJOR 116 /* standard configuration */
  37. /* forward declarations */
  38. struct pci_dev;
  39. struct module;
  40. struct completion;
  41. /* device allocation stuff */
  42. /* type of the object used in snd_device_*()
  43. * this also defines the calling order
  44. */
  45. enum snd_device_type {
  46. SNDRV_DEV_LOWLEVEL,
  47. SNDRV_DEV_INFO,
  48. SNDRV_DEV_BUS,
  49. SNDRV_DEV_CODEC,
  50. SNDRV_DEV_PCM,
  51. SNDRV_DEV_COMPRESS,
  52. SNDRV_DEV_RAWMIDI,
  53. SNDRV_DEV_TIMER,
  54. SNDRV_DEV_SEQUENCER,
  55. SNDRV_DEV_HWDEP,
  56. SNDRV_DEV_JACK,
  57. SNDRV_DEV_CONTROL, /* NOTE: this must be the last one */
  58. };
  59. enum snd_device_state {
  60. SNDRV_DEV_BUILD,
  61. SNDRV_DEV_REGISTERED,
  62. SNDRV_DEV_DISCONNECTED,
  63. };
  64. struct snd_device;
  65. struct snd_device_ops {
  66. int (*dev_free)(struct snd_device *dev);
  67. int (*dev_register)(struct snd_device *dev);
  68. int (*dev_disconnect)(struct snd_device *dev);
  69. };
  70. struct snd_device {
  71. struct list_head list; /* list of registered devices */
  72. struct snd_card *card; /* card which holds this device */
  73. enum snd_device_state state; /* state of the device */
  74. enum snd_device_type type; /* device type */
  75. void *device_data; /* device structure */
  76. struct snd_device_ops *ops; /* operations */
  77. };
  78. #define snd_device(n) list_entry(n, struct snd_device, list)
  79. /* main structure for soundcard */
  80. struct snd_card {
  81. int number; /* number of soundcard (index to
  82. snd_cards) */
  83. char id[16]; /* id string of this card */
  84. char driver[16]; /* driver name */
  85. char shortname[32]; /* short name of this soundcard */
  86. char longname[80]; /* name of this soundcard */
  87. char irq_descr[32]; /* Interrupt description */
  88. char mixername[80]; /* mixer name */
  89. char components[128]; /* card components delimited with
  90. space */
  91. struct module *module; /* top-level module */
  92. void *private_data; /* private data for soundcard */
  93. void (*private_free) (struct snd_card *card); /* callback for freeing of
  94. private data */
  95. struct list_head devices; /* devices */
  96. struct device ctl_dev; /* control device */
  97. unsigned int last_numid; /* last used numeric ID */
  98. struct rw_semaphore controls_rwsem; /* controls list lock */
  99. rwlock_t ctl_files_rwlock; /* ctl_files list lock */
  100. int controls_count; /* count of all controls */
  101. int user_ctl_count; /* count of all user controls */
  102. struct list_head controls; /* all controls for this card */
  103. struct list_head ctl_files; /* active control files */
  104. struct snd_info_entry *proc_root; /* root for soundcard specific files */
  105. struct snd_info_entry *proc_id; /* the card id */
  106. struct proc_dir_entry *proc_root_link; /* number link to real id */
  107. struct list_head files_list; /* all files associated to this card */
  108. struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
  109. state */
  110. spinlock_t files_lock; /* lock the files for this card */
  111. int shutdown; /* this card is going down */
  112. struct completion *release_completion;
  113. struct device *dev; /* device assigned to this card */
  114. struct device card_dev; /* cardX object for sysfs */
  115. const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
  116. bool registered; /* card_dev is registered? */
  117. wait_queue_head_t remove_sleep;
  118. #ifdef CONFIG_PM
  119. unsigned int power_state; /* power state */
  120. wait_queue_head_t power_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 card->power_state;
  132. }
  133. static inline void snd_power_change_state(struct snd_card *card, unsigned int state)
  134. {
  135. card->power_state = state;
  136. wake_up(&card->power_sleep);
  137. }
  138. /* init.c */
  139. int snd_power_wait(struct snd_card *card, unsigned int power_state);
  140. #else /* ! CONFIG_PM */
  141. static inline int snd_power_wait(struct snd_card *card, unsigned int state) { return 0; }
  142. #define snd_power_get_state(card) ({ (void)(card); SNDRV_CTL_POWER_D0; })
  143. #define snd_power_change_state(card, state) do { (void)(card); } while (0)
  144. #endif /* CONFIG_PM */
  145. struct snd_minor {
  146. int type; /* SNDRV_DEVICE_TYPE_XXX */
  147. int card; /* card number */
  148. int device; /* device number */
  149. const struct file_operations *f_ops; /* file operations */
  150. void *private_data; /* private data for f_ops->open */
  151. struct device *dev; /* device for sysfs */
  152. struct snd_card *card_ptr; /* assigned card instance */
  153. };
  154. /* return a device pointer linked to each sound device as a parent */
  155. static inline struct device *snd_card_get_device_link(struct snd_card *card)
  156. {
  157. return card ? &card->card_dev : NULL;
  158. }
  159. /* sound.c */
  160. extern int snd_major;
  161. extern int snd_ecards_limit;
  162. extern struct class *sound_class;
  163. void snd_request_card(int card);
  164. void snd_device_initialize(struct device *dev, struct snd_card *card);
  165. int snd_register_device(int type, struct snd_card *card, int dev,
  166. const struct file_operations *f_ops,
  167. void *private_data, struct device *device);
  168. int snd_unregister_device(struct device *dev);
  169. void *snd_lookup_minor_data(unsigned int minor, int type);
  170. #ifdef CONFIG_SND_OSSEMUL
  171. int snd_register_oss_device(int type, struct snd_card *card, int dev,
  172. const struct file_operations *f_ops, void *private_data);
  173. int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
  174. void *snd_lookup_oss_minor_data(unsigned int minor, int type);
  175. #endif
  176. int snd_minor_info_init(void);
  177. /* sound_oss.c */
  178. #ifdef CONFIG_SND_OSSEMUL
  179. int snd_minor_info_oss_init(void);
  180. #else
  181. static inline int snd_minor_info_oss_init(void) { return 0; }
  182. #endif
  183. /* memory.c */
  184. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
  185. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
  186. /* init.c */
  187. extern struct snd_card *snd_cards[SNDRV_CARDS];
  188. int snd_card_locked(int card);
  189. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  190. #define SND_MIXER_OSS_NOTIFY_REGISTER 0
  191. #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1
  192. #define SND_MIXER_OSS_NOTIFY_FREE 2
  193. extern int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int cmd);
  194. #endif
  195. int snd_card_new(struct device *parent, int idx, const char *xid,
  196. struct module *module, int extra_size,
  197. struct snd_card **card_ret);
  198. int snd_card_disconnect(struct snd_card *card);
  199. void snd_card_disconnect_sync(struct snd_card *card);
  200. int snd_card_free(struct snd_card *card);
  201. int snd_card_free_when_closed(struct snd_card *card);
  202. void snd_card_set_id(struct snd_card *card, const char *id);
  203. int snd_card_register(struct snd_card *card);
  204. int snd_card_info_init(void);
  205. int snd_card_add_dev_attr(struct snd_card *card,
  206. const struct attribute_group *group);
  207. int snd_component_add(struct snd_card *card, const char *component);
  208. int snd_card_file_add(struct snd_card *card, struct file *file);
  209. int snd_card_file_remove(struct snd_card *card, struct file *file);
  210. #define snd_card_unref(card) put_device(&(card)->card_dev)
  211. #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
  212. /* device.c */
  213. int snd_device_new(struct snd_card *card, enum snd_device_type type,
  214. void *device_data, struct snd_device_ops *ops);
  215. int snd_device_register(struct snd_card *card, void *device_data);
  216. int snd_device_register_all(struct snd_card *card);
  217. void snd_device_disconnect(struct snd_card *card, void *device_data);
  218. void snd_device_disconnect_all(struct snd_card *card);
  219. void snd_device_free(struct snd_card *card, void *device_data);
  220. void snd_device_free_all(struct snd_card *card);
  221. /* isadma.c */
  222. #ifdef CONFIG_ISA_DMA_API
  223. #define DMA_MODE_NO_ENABLE 0x0100
  224. void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
  225. void snd_dma_disable(unsigned long dma);
  226. unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
  227. #endif
  228. /* misc.c */
  229. struct resource;
  230. void release_and_free_resource(struct resource *res);
  231. /* --- */
  232. /* sound printk debug levels */
  233. enum {
  234. SND_PR_ALWAYS,
  235. SND_PR_DEBUG,
  236. SND_PR_VERBOSE,
  237. };
  238. #if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
  239. __printf(4, 5)
  240. void __snd_printk(unsigned int level, const char *file, int line,
  241. const char *format, ...);
  242. #else
  243. #define __snd_printk(level, file, line, format, ...) \
  244. printk(format, ##__VA_ARGS__)
  245. #endif
  246. /**
  247. * snd_printk - printk wrapper
  248. * @fmt: format string
  249. *
  250. * Works like printk() but prints the file and the line of the caller
  251. * when configured with CONFIG_SND_VERBOSE_PRINTK.
  252. */
  253. #define snd_printk(fmt, ...) \
  254. __snd_printk(0, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  255. #ifdef CONFIG_SND_DEBUG
  256. /**
  257. * snd_printd - debug printk
  258. * @fmt: format string
  259. *
  260. * Works like snd_printk() for debugging purposes.
  261. * Ignored when CONFIG_SND_DEBUG is not set.
  262. */
  263. #define snd_printd(fmt, ...) \
  264. __snd_printk(1, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  265. #define _snd_printd(level, fmt, ...) \
  266. __snd_printk(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  267. /**
  268. * snd_BUG - give a BUG warning message and stack trace
  269. *
  270. * Calls WARN() if CONFIG_SND_DEBUG is set.
  271. * Ignored when CONFIG_SND_DEBUG is not set.
  272. */
  273. #define snd_BUG() WARN(1, "BUG?\n")
  274. /**
  275. * Suppress high rates of output when CONFIG_SND_DEBUG is enabled.
  276. */
  277. #define snd_printd_ratelimit() printk_ratelimit()
  278. /**
  279. * snd_BUG_ON - debugging check macro
  280. * @cond: condition to evaluate
  281. *
  282. * Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set,
  283. * otherwise just evaluates the conditional and returns the value.
  284. */
  285. #define snd_BUG_ON(cond) WARN_ON((cond))
  286. #else /* !CONFIG_SND_DEBUG */
  287. __printf(1, 2)
  288. static inline void snd_printd(const char *format, ...) {}
  289. __printf(2, 3)
  290. static inline void _snd_printd(int level, const char *format, ...) {}
  291. #define snd_BUG() do { } while (0)
  292. #define snd_BUG_ON(condition) ({ \
  293. int __ret_warn_on = !!(condition); \
  294. unlikely(__ret_warn_on); \
  295. })
  296. static inline bool snd_printd_ratelimit(void) { return false; }
  297. #endif /* CONFIG_SND_DEBUG */
  298. #ifdef CONFIG_SND_DEBUG_VERBOSE
  299. /**
  300. * snd_printdd - debug printk
  301. * @format: format string
  302. *
  303. * Works like snd_printk() for debugging purposes.
  304. * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
  305. */
  306. #define snd_printdd(format, ...) \
  307. __snd_printk(2, __FILE__, __LINE__, format, ##__VA_ARGS__)
  308. #else
  309. __printf(1, 2)
  310. static inline void snd_printdd(const char *format, ...) {}
  311. #endif
  312. #define SNDRV_OSS_VERSION ((3<<16)|(8<<8)|(1<<4)|(0)) /* 3.8.1a */
  313. /* for easier backward-porting */
  314. #if IS_ENABLED(CONFIG_GAMEPORT)
  315. #define gameport_set_dev_parent(gp,xdev) ((gp)->dev.parent = (xdev))
  316. #define gameport_set_port_data(gp,r) ((gp)->port_data = (r))
  317. #define gameport_get_port_data(gp) (gp)->port_data
  318. #endif
  319. /* PCI quirk list helper */
  320. struct snd_pci_quirk {
  321. unsigned short subvendor; /* PCI subvendor ID */
  322. unsigned short subdevice; /* PCI subdevice ID */
  323. unsigned short subdevice_mask; /* bitmask to match */
  324. int value; /* value */
  325. #ifdef CONFIG_SND_DEBUG_VERBOSE
  326. const char *name; /* name of the device (optional) */
  327. #endif
  328. };
  329. #define _SND_PCI_QUIRK_ID_MASK(vend, mask, dev) \
  330. .subvendor = (vend), .subdevice = (dev), .subdevice_mask = (mask)
  331. #define _SND_PCI_QUIRK_ID(vend, dev) \
  332. _SND_PCI_QUIRK_ID_MASK(vend, 0xffff, dev)
  333. #define SND_PCI_QUIRK_ID(vend,dev) {_SND_PCI_QUIRK_ID(vend, dev)}
  334. #ifdef CONFIG_SND_DEBUG_VERBOSE
  335. #define SND_PCI_QUIRK(vend,dev,xname,val) \
  336. {_SND_PCI_QUIRK_ID(vend, dev), .value = (val), .name = (xname)}
  337. #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
  338. {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val), .name = (xname)}
  339. #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
  340. {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), \
  341. .value = (val), .name = (xname)}
  342. #define snd_pci_quirk_name(q) ((q)->name)
  343. #else
  344. #define SND_PCI_QUIRK(vend,dev,xname,val) \
  345. {_SND_PCI_QUIRK_ID(vend, dev), .value = (val)}
  346. #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
  347. {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), .value = (val)}
  348. #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
  349. {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val)}
  350. #define snd_pci_quirk_name(q) ""
  351. #endif
  352. #ifdef CONFIG_PCI
  353. const struct snd_pci_quirk *
  354. snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list);
  355. const struct snd_pci_quirk *
  356. snd_pci_quirk_lookup_id(u16 vendor, u16 device,
  357. const struct snd_pci_quirk *list);
  358. #else
  359. static inline const struct snd_pci_quirk *
  360. snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
  361. {
  362. return NULL;
  363. }
  364. static inline const struct snd_pci_quirk *
  365. snd_pci_quirk_lookup_id(u16 vendor, u16 device,
  366. const struct snd_pci_quirk *list)
  367. {
  368. return NULL;
  369. }
  370. #endif
  371. #endif /* __SOUND_CORE_H */