core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_CONTROL,
  48. SNDRV_DEV_INFO,
  49. SNDRV_DEV_BUS,
  50. SNDRV_DEV_CODEC,
  51. SNDRV_DEV_PCM,
  52. SNDRV_DEV_COMPRESS,
  53. SNDRV_DEV_RAWMIDI,
  54. SNDRV_DEV_TIMER,
  55. SNDRV_DEV_SEQUENCER,
  56. SNDRV_DEV_HWDEP,
  57. SNDRV_DEV_JACK,
  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. #ifdef CONFIG_PM
  118. unsigned int power_state; /* power state */
  119. wait_queue_head_t power_sleep;
  120. #endif
  121. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  122. struct snd_mixer_oss *mixer_oss;
  123. int mixer_oss_change_count;
  124. #endif
  125. };
  126. #define dev_to_snd_card(p) container_of(p, struct snd_card, card_dev)
  127. #ifdef CONFIG_PM
  128. static inline unsigned int snd_power_get_state(struct snd_card *card)
  129. {
  130. return card->power_state;
  131. }
  132. static inline void snd_power_change_state(struct snd_card *card, unsigned int state)
  133. {
  134. card->power_state = state;
  135. wake_up(&card->power_sleep);
  136. }
  137. /* init.c */
  138. int snd_power_wait(struct snd_card *card, unsigned int power_state);
  139. #else /* ! CONFIG_PM */
  140. static inline int snd_power_wait(struct snd_card *card, unsigned int state) { return 0; }
  141. #define snd_power_get_state(card) ({ (void)(card); SNDRV_CTL_POWER_D0; })
  142. #define snd_power_change_state(card, state) do { (void)(card); } while (0)
  143. #endif /* CONFIG_PM */
  144. struct snd_minor {
  145. int type; /* SNDRV_DEVICE_TYPE_XXX */
  146. int card; /* card number */
  147. int device; /* device number */
  148. const struct file_operations *f_ops; /* file operations */
  149. void *private_data; /* private data for f_ops->open */
  150. struct device *dev; /* device for sysfs */
  151. struct snd_card *card_ptr; /* assigned card instance */
  152. };
  153. /* return a device pointer linked to each sound device as a parent */
  154. static inline struct device *snd_card_get_device_link(struct snd_card *card)
  155. {
  156. return card ? &card->card_dev : NULL;
  157. }
  158. /* sound.c */
  159. extern int snd_major;
  160. extern int snd_ecards_limit;
  161. extern struct class *sound_class;
  162. void snd_request_card(int card);
  163. void snd_device_initialize(struct device *dev, struct snd_card *card);
  164. int snd_register_device(int type, struct snd_card *card, int dev,
  165. const struct file_operations *f_ops,
  166. void *private_data, struct device *device);
  167. int snd_unregister_device(struct device *dev);
  168. void *snd_lookup_minor_data(unsigned int minor, int type);
  169. #ifdef CONFIG_SND_OSSEMUL
  170. int snd_register_oss_device(int type, struct snd_card *card, int dev,
  171. const struct file_operations *f_ops, void *private_data);
  172. int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
  173. void *snd_lookup_oss_minor_data(unsigned int minor, int type);
  174. #endif
  175. int snd_minor_info_init(void);
  176. /* sound_oss.c */
  177. #ifdef CONFIG_SND_OSSEMUL
  178. int snd_minor_info_oss_init(void);
  179. #else
  180. static inline int snd_minor_info_oss_init(void) { return 0; }
  181. #endif
  182. /* memory.c */
  183. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
  184. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
  185. /* init.c */
  186. extern struct snd_card *snd_cards[SNDRV_CARDS];
  187. int snd_card_locked(int card);
  188. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  189. #define SND_MIXER_OSS_NOTIFY_REGISTER 0
  190. #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1
  191. #define SND_MIXER_OSS_NOTIFY_FREE 2
  192. extern int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int cmd);
  193. #endif
  194. int snd_card_new(struct device *parent, int idx, const char *xid,
  195. struct module *module, int extra_size,
  196. struct snd_card **card_ret);
  197. int snd_card_disconnect(struct snd_card *card);
  198. int snd_card_free(struct snd_card *card);
  199. int snd_card_free_when_closed(struct snd_card *card);
  200. void snd_card_set_id(struct snd_card *card, const char *id);
  201. int snd_card_register(struct snd_card *card);
  202. int snd_card_info_init(void);
  203. int snd_card_add_dev_attr(struct snd_card *card,
  204. const struct attribute_group *group);
  205. int snd_component_add(struct snd_card *card, const char *component);
  206. int snd_card_file_add(struct snd_card *card, struct file *file);
  207. int snd_card_file_remove(struct snd_card *card, struct file *file);
  208. #define snd_card_unref(card) put_device(&(card)->card_dev)
  209. #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
  210. /* device.c */
  211. int snd_device_new(struct snd_card *card, enum snd_device_type type,
  212. void *device_data, struct snd_device_ops *ops);
  213. int snd_device_register(struct snd_card *card, void *device_data);
  214. int snd_device_register_all(struct snd_card *card);
  215. void snd_device_disconnect(struct snd_card *card, void *device_data);
  216. void snd_device_disconnect_all(struct snd_card *card);
  217. void snd_device_free(struct snd_card *card, void *device_data);
  218. void snd_device_free_all(struct snd_card *card);
  219. /* isadma.c */
  220. #ifdef CONFIG_ISA_DMA_API
  221. #define DMA_MODE_NO_ENABLE 0x0100
  222. void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
  223. void snd_dma_disable(unsigned long dma);
  224. unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
  225. #endif
  226. /* misc.c */
  227. struct resource;
  228. void release_and_free_resource(struct resource *res);
  229. /* --- */
  230. /* sound printk debug levels */
  231. enum {
  232. SND_PR_ALWAYS,
  233. SND_PR_DEBUG,
  234. SND_PR_VERBOSE,
  235. };
  236. #if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
  237. __printf(4, 5)
  238. void __snd_printk(unsigned int level, const char *file, int line,
  239. const char *format, ...);
  240. #else
  241. #define __snd_printk(level, file, line, format, ...) \
  242. printk(format, ##__VA_ARGS__)
  243. #endif
  244. /**
  245. * snd_printk - printk wrapper
  246. * @fmt: format string
  247. *
  248. * Works like printk() but prints the file and the line of the caller
  249. * when configured with CONFIG_SND_VERBOSE_PRINTK.
  250. */
  251. #define snd_printk(fmt, ...) \
  252. __snd_printk(0, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  253. #ifdef CONFIG_SND_DEBUG
  254. /**
  255. * snd_printd - debug printk
  256. * @fmt: format string
  257. *
  258. * Works like snd_printk() for debugging purposes.
  259. * Ignored when CONFIG_SND_DEBUG is not set.
  260. */
  261. #define snd_printd(fmt, ...) \
  262. __snd_printk(1, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  263. #define _snd_printd(level, fmt, ...) \
  264. __snd_printk(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  265. /**
  266. * snd_BUG - give a BUG warning message and stack trace
  267. *
  268. * Calls WARN() if CONFIG_SND_DEBUG is set.
  269. * Ignored when CONFIG_SND_DEBUG is not set.
  270. */
  271. #define snd_BUG() WARN(1, "BUG?\n")
  272. /**
  273. * Suppress high rates of output when CONFIG_SND_DEBUG is enabled.
  274. */
  275. #define snd_printd_ratelimit() printk_ratelimit()
  276. /**
  277. * snd_BUG_ON - debugging check macro
  278. * @cond: condition to evaluate
  279. *
  280. * Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set,
  281. * otherwise just evaluates the conditional and returns the value.
  282. */
  283. #define snd_BUG_ON(cond) WARN_ON((cond))
  284. #else /* !CONFIG_SND_DEBUG */
  285. __printf(1, 2)
  286. static inline void snd_printd(const char *format, ...) {}
  287. __printf(2, 3)
  288. static inline void _snd_printd(int level, const char *format, ...) {}
  289. #define snd_BUG() do { } while (0)
  290. #define snd_BUG_ON(condition) ({ \
  291. int __ret_warn_on = !!(condition); \
  292. unlikely(__ret_warn_on); \
  293. })
  294. static inline bool snd_printd_ratelimit(void) { return false; }
  295. #endif /* CONFIG_SND_DEBUG */
  296. #ifdef CONFIG_SND_DEBUG_VERBOSE
  297. /**
  298. * snd_printdd - debug printk
  299. * @format: format string
  300. *
  301. * Works like snd_printk() for debugging purposes.
  302. * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
  303. */
  304. #define snd_printdd(format, ...) \
  305. __snd_printk(2, __FILE__, __LINE__, format, ##__VA_ARGS__)
  306. #else
  307. __printf(1, 2)
  308. static inline void snd_printdd(const char *format, ...) {}
  309. #endif
  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. #endif /* __SOUND_CORE_H */