core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/sched.h> /* wake_up() */
  24. #include <linux/mutex.h> /* struct mutex */
  25. #include <linux/rwsem.h> /* struct rw_semaphore */
  26. #include <linux/pm.h> /* pm_message_t */
  27. #include <linux/stringify.h>
  28. /* number of supported soundcards */
  29. #ifdef CONFIG_SND_DYNAMIC_MINORS
  30. #define SNDRV_CARDS 32
  31. #else
  32. #define SNDRV_CARDS 8 /* don't change - minor numbers */
  33. #endif
  34. #define CONFIG_SND_MAJOR 116 /* standard configuration */
  35. /* forward declarations */
  36. struct pci_dev;
  37. struct module;
  38. struct device;
  39. struct device_attribute;
  40. /* device allocation stuff */
  41. #define SNDRV_DEV_TYPE_RANGE_SIZE 0x1000
  42. typedef int __bitwise snd_device_type_t;
  43. #define SNDRV_DEV_TOPLEVEL ((__force snd_device_type_t) 0)
  44. #define SNDRV_DEV_CONTROL ((__force snd_device_type_t) 1)
  45. #define SNDRV_DEV_LOWLEVEL_PRE ((__force snd_device_type_t) 2)
  46. #define SNDRV_DEV_LOWLEVEL_NORMAL ((__force snd_device_type_t) 0x1000)
  47. #define SNDRV_DEV_PCM ((__force snd_device_type_t) 0x1001)
  48. #define SNDRV_DEV_RAWMIDI ((__force snd_device_type_t) 0x1002)
  49. #define SNDRV_DEV_TIMER ((__force snd_device_type_t) 0x1003)
  50. #define SNDRV_DEV_SEQUENCER ((__force snd_device_type_t) 0x1004)
  51. #define SNDRV_DEV_HWDEP ((__force snd_device_type_t) 0x1005)
  52. #define SNDRV_DEV_INFO ((__force snd_device_type_t) 0x1006)
  53. #define SNDRV_DEV_BUS ((__force snd_device_type_t) 0x1007)
  54. #define SNDRV_DEV_CODEC ((__force snd_device_type_t) 0x1008)
  55. #define SNDRV_DEV_JACK ((__force snd_device_type_t) 0x1009)
  56. #define SNDRV_DEV_COMPRESS ((__force snd_device_type_t) 0x100A)
  57. #define SNDRV_DEV_LOWLEVEL ((__force snd_device_type_t) 0x2000)
  58. typedef int __bitwise snd_device_state_t;
  59. #define SNDRV_DEV_BUILD ((__force snd_device_state_t) 0)
  60. #define SNDRV_DEV_REGISTERED ((__force snd_device_state_t) 1)
  61. #define SNDRV_DEV_DISCONNECTED ((__force snd_device_state_t) 2)
  62. typedef int __bitwise snd_device_cmd_t;
  63. #define SNDRV_DEV_CMD_PRE ((__force snd_device_cmd_t) 0)
  64. #define SNDRV_DEV_CMD_NORMAL ((__force snd_device_cmd_t) 1)
  65. #define SNDRV_DEV_CMD_POST ((__force snd_device_cmd_t) 2)
  66. struct snd_device;
  67. struct snd_device_ops {
  68. int (*dev_free)(struct snd_device *dev);
  69. int (*dev_register)(struct snd_device *dev);
  70. int (*dev_disconnect)(struct snd_device *dev);
  71. };
  72. struct snd_device {
  73. struct list_head list; /* list of registered devices */
  74. struct snd_card *card; /* card which holds this device */
  75. snd_device_state_t state; /* state of the device */
  76. snd_device_type_t type; /* device type */
  77. void *device_data; /* device structure */
  78. struct snd_device_ops *ops; /* operations */
  79. };
  80. #define snd_device(n) list_entry(n, struct snd_device, list)
  81. /* main structure for soundcard */
  82. struct snd_card {
  83. int number; /* number of soundcard (index to
  84. snd_cards) */
  85. char id[16]; /* id string of this card */
  86. char driver[16]; /* driver name */
  87. char shortname[32]; /* short name of this soundcard */
  88. char longname[80]; /* name of this soundcard */
  89. char mixername[80]; /* mixer name */
  90. char components[128]; /* card components delimited with
  91. space */
  92. struct module *module; /* top-level module */
  93. void *private_data; /* private data for soundcard */
  94. void (*private_free) (struct snd_card *card); /* callback for freeing of
  95. private data */
  96. struct list_head devices; /* devices */
  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 mutex user_ctl_lock; /* protects user controls against
  105. concurrent access */
  106. struct snd_info_entry *proc_root; /* root for soundcard specific files */
  107. struct snd_info_entry *proc_id; /* the card id */
  108. struct proc_dir_entry *proc_root_link; /* number link to real id */
  109. struct list_head files_list; /* all files associated to this card */
  110. struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
  111. state */
  112. spinlock_t files_lock; /* lock the files for this card */
  113. int shutdown; /* this card is going down */
  114. int free_on_last_close; /* free in context of file_release */
  115. wait_queue_head_t shutdown_sleep;
  116. atomic_t refcount; /* refcount for disconnection */
  117. struct device *dev; /* device assigned to this card */
  118. struct device *card_dev; /* cardX object for sysfs */
  119. int offline; /* if this sound card is offline */
  120. unsigned long offline_change;
  121. wait_queue_head_t offline_poll_wait;
  122. #ifdef CONFIG_PM
  123. unsigned int power_state; /* power state */
  124. struct mutex power_lock; /* power lock */
  125. wait_queue_head_t power_sleep;
  126. #endif
  127. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  128. struct snd_mixer_oss *mixer_oss;
  129. int mixer_oss_change_count;
  130. #endif
  131. };
  132. #ifdef CONFIG_PM
  133. static inline void snd_power_lock(struct snd_card *card)
  134. {
  135. mutex_lock(&card->power_lock);
  136. }
  137. static inline void snd_power_unlock(struct snd_card *card)
  138. {
  139. mutex_unlock(&card->power_lock);
  140. }
  141. static inline unsigned int snd_power_get_state(struct snd_card *card)
  142. {
  143. return card->power_state;
  144. }
  145. static inline void snd_power_change_state(struct snd_card *card, unsigned int state)
  146. {
  147. card->power_state = state;
  148. wake_up(&card->power_sleep);
  149. }
  150. /* init.c */
  151. int snd_power_wait(struct snd_card *card, unsigned int power_state);
  152. #else /* ! CONFIG_PM */
  153. #define snd_power_lock(card) do { (void)(card); } while (0)
  154. #define snd_power_unlock(card) do { (void)(card); } while (0)
  155. static inline int snd_power_wait(struct snd_card *card, unsigned int state) { return 0; }
  156. #define snd_power_get_state(card) ({ (void)(card); SNDRV_CTL_POWER_D0; })
  157. #define snd_power_change_state(card, state) do { (void)(card); } while (0)
  158. #endif /* CONFIG_PM */
  159. struct snd_minor {
  160. int type; /* SNDRV_DEVICE_TYPE_XXX */
  161. int card; /* card number */
  162. int device; /* device number */
  163. const struct file_operations *f_ops; /* file operations */
  164. void *private_data; /* private data for f_ops->open */
  165. struct device *dev; /* device for sysfs */
  166. struct snd_card *card_ptr; /* assigned card instance */
  167. };
  168. /* return a device pointer linked to each sound device as a parent */
  169. static inline struct device *snd_card_get_device_link(struct snd_card *card)
  170. {
  171. return card ? card->card_dev : NULL;
  172. }
  173. /* sound.c */
  174. extern int snd_major;
  175. extern int snd_ecards_limit;
  176. extern struct class *sound_class;
  177. void snd_request_card(int card);
  178. int snd_register_device_for_dev(int type, struct snd_card *card,
  179. int dev,
  180. const struct file_operations *f_ops,
  181. void *private_data,
  182. const char *name,
  183. struct device *device);
  184. /**
  185. * snd_register_device - Register the ALSA device file for the card
  186. * @type: the device type, SNDRV_DEVICE_TYPE_XXX
  187. * @card: the card instance
  188. * @dev: the device index
  189. * @f_ops: the file operations
  190. * @private_data: user pointer for f_ops->open()
  191. * @name: the device file name
  192. *
  193. * Registers an ALSA device file for the given card.
  194. * The operators have to be set in reg parameter.
  195. *
  196. * This function uses the card's device pointer to link to the
  197. * correct &struct device.
  198. *
  199. * Returns zero if successful, or a negative error code on failure.
  200. */
  201. static inline int snd_register_device(int type, struct snd_card *card, int dev,
  202. const struct file_operations *f_ops,
  203. void *private_data,
  204. const char *name)
  205. {
  206. return snd_register_device_for_dev(type, card, dev, f_ops,
  207. private_data, name,
  208. snd_card_get_device_link(card));
  209. }
  210. int snd_unregister_device(int type, struct snd_card *card, int dev);
  211. void *snd_lookup_minor_data(unsigned int minor, int type);
  212. int snd_add_device_sysfs_file(int type, struct snd_card *card, int dev,
  213. struct device_attribute *attr);
  214. #ifdef CONFIG_SND_OSSEMUL
  215. int snd_register_oss_device(int type, struct snd_card *card, int dev,
  216. const struct file_operations *f_ops, void *private_data,
  217. const char *name);
  218. int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
  219. void *snd_lookup_oss_minor_data(unsigned int minor, int type);
  220. #endif
  221. int snd_minor_info_init(void);
  222. int snd_minor_info_done(void);
  223. /* sound_oss.c */
  224. #ifdef CONFIG_SND_OSSEMUL
  225. int snd_minor_info_oss_init(void);
  226. int snd_minor_info_oss_done(void);
  227. #else
  228. static inline int snd_minor_info_oss_init(void) { return 0; }
  229. static inline int snd_minor_info_oss_done(void) { return 0; }
  230. #endif
  231. /* memory.c */
  232. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
  233. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
  234. /* init.c */
  235. extern struct snd_card *snd_cards[SNDRV_CARDS];
  236. int snd_card_locked(int card);
  237. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  238. #define SND_MIXER_OSS_NOTIFY_REGISTER 0
  239. #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1
  240. #define SND_MIXER_OSS_NOTIFY_FREE 2
  241. extern int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int cmd);
  242. #endif
  243. int snd_card_create(int idx, const char *id,
  244. struct module *module, int extra_size,
  245. struct snd_card **card_ret);
  246. int snd_card_disconnect(struct snd_card *card);
  247. int snd_card_free(struct snd_card *card);
  248. int snd_card_free_when_closed(struct snd_card *card);
  249. void snd_card_set_id(struct snd_card *card, const char *id);
  250. int snd_card_register(struct snd_card *card);
  251. int snd_card_info_init(void);
  252. int snd_card_info_done(void);
  253. int snd_component_add(struct snd_card *card, const char *component);
  254. int snd_card_file_add(struct snd_card *card, struct file *file);
  255. int snd_card_file_remove(struct snd_card *card, struct file *file);
  256. void snd_card_change_online_state(struct snd_card *card, int online);
  257. bool snd_card_is_online_state(struct snd_card *card);
  258. void snd_card_unref(struct snd_card *card);
  259. #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
  260. /* device.c */
  261. int snd_device_new(struct snd_card *card, snd_device_type_t type,
  262. void *device_data, struct snd_device_ops *ops);
  263. int snd_device_register(struct snd_card *card, void *device_data);
  264. int snd_device_register_all(struct snd_card *card);
  265. int snd_device_disconnect(struct snd_card *card, void *device_data);
  266. int snd_device_disconnect_all(struct snd_card *card);
  267. int snd_device_free(struct snd_card *card, void *device_data);
  268. int snd_device_free_all(struct snd_card *card, snd_device_cmd_t cmd);
  269. /* isadma.c */
  270. #ifdef CONFIG_ISA_DMA_API
  271. #define DMA_MODE_NO_ENABLE 0x0100
  272. void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
  273. void snd_dma_disable(unsigned long dma);
  274. unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
  275. #endif
  276. /* misc.c */
  277. struct resource;
  278. void release_and_free_resource(struct resource *res);
  279. /* --- */
  280. /* sound printk debug levels */
  281. enum {
  282. SND_PR_ALWAYS,
  283. SND_PR_DEBUG,
  284. SND_PR_VERBOSE,
  285. };
  286. #if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
  287. __printf(4, 5)
  288. void __snd_printk(unsigned int level, const char *file, int line,
  289. const char *format, ...);
  290. #else
  291. #define __snd_printk(level, file, line, format, args...) \
  292. printk(format, ##args)
  293. #endif
  294. /**
  295. * snd_printk - printk wrapper
  296. * @fmt: format string
  297. *
  298. * Works like printk() but prints the file and the line of the caller
  299. * when configured with CONFIG_SND_VERBOSE_PRINTK.
  300. */
  301. #define snd_printk(fmt, args...) \
  302. __snd_printk(0, __FILE__, __LINE__, fmt, ##args)
  303. #ifdef CONFIG_SND_DEBUG
  304. /**
  305. * snd_printd - debug printk
  306. * @fmt: format string
  307. *
  308. * Works like snd_printk() for debugging purposes.
  309. * Ignored when CONFIG_SND_DEBUG is not set.
  310. */
  311. #define snd_printd(fmt, args...) \
  312. __snd_printk(1, __FILE__, __LINE__, fmt, ##args)
  313. #define _snd_printd(level, fmt, args...) \
  314. __snd_printk(level, __FILE__, __LINE__, fmt, ##args)
  315. /**
  316. * snd_BUG - give a BUG warning message and stack trace
  317. *
  318. * Calls WARN() if CONFIG_SND_DEBUG is set.
  319. * Ignored when CONFIG_SND_DEBUG is not set.
  320. */
  321. #define snd_BUG() WARN(1, "BUG?\n")
  322. /**
  323. * snd_BUG_ON - debugging check macro
  324. * @cond: condition to evaluate
  325. *
  326. * When CONFIG_SND_DEBUG is set, this macro evaluates the given condition,
  327. * and call WARN() and returns the value if it's non-zero.
  328. *
  329. * When CONFIG_SND_DEBUG is not set, this just returns zero, and the given
  330. * condition is ignored.
  331. *
  332. * NOTE: the argument won't be evaluated at all when CONFIG_SND_DEBUG=n.
  333. * Thus, don't put any statement that influences on the code behavior,
  334. * such as pre/post increment, to the argument of this macro.
  335. * If you want to evaluate and give a warning, use standard WARN_ON().
  336. */
  337. #define snd_BUG_ON(cond) WARN((cond), "BUG? (%s)\n", __stringify(cond))
  338. #else /* !CONFIG_SND_DEBUG */
  339. #define snd_printd(fmt, args...) do { } while (0)
  340. #define _snd_printd(level, fmt, args...) do { } while (0)
  341. #define snd_BUG() do { } while (0)
  342. static inline int __snd_bug_on(int cond)
  343. {
  344. return 0;
  345. }
  346. #define snd_BUG_ON(cond) __snd_bug_on(0 && (cond)) /* always false */
  347. #endif /* CONFIG_SND_DEBUG */
  348. #ifdef CONFIG_SND_DEBUG_VERBOSE
  349. /**
  350. * snd_printdd - debug printk
  351. * @format: format string
  352. *
  353. * Works like snd_printk() for debugging purposes.
  354. * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
  355. */
  356. #define snd_printdd(format, args...) \
  357. __snd_printk(2, __FILE__, __LINE__, format, ##args)
  358. #else
  359. #define snd_printdd(format, args...) do { } while (0)
  360. #endif
  361. #define SNDRV_OSS_VERSION ((3<<16)|(8<<8)|(1<<4)|(0)) /* 3.8.1a */
  362. /* for easier backward-porting */
  363. #if defined(CONFIG_GAMEPORT) || defined(CONFIG_GAMEPORT_MODULE)
  364. #define gameport_set_dev_parent(gp,xdev) ((gp)->dev.parent = (xdev))
  365. #define gameport_set_port_data(gp,r) ((gp)->port_data = (r))
  366. #define gameport_get_port_data(gp) (gp)->port_data
  367. #endif
  368. #ifdef CONFIG_PCI
  369. /* PCI quirk list helper */
  370. struct snd_pci_quirk {
  371. unsigned short subvendor; /* PCI subvendor ID */
  372. unsigned short subdevice; /* PCI subdevice ID */
  373. unsigned short subdevice_mask; /* bitmask to match */
  374. int value; /* value */
  375. #ifdef CONFIG_SND_DEBUG_VERBOSE
  376. const char *name; /* name of the device (optional) */
  377. #endif
  378. };
  379. #define _SND_PCI_QUIRK_ID_MASK(vend, mask, dev) \
  380. .subvendor = (vend), .subdevice = (dev), .subdevice_mask = (mask)
  381. #define _SND_PCI_QUIRK_ID(vend, dev) \
  382. _SND_PCI_QUIRK_ID_MASK(vend, 0xffff, dev)
  383. #define SND_PCI_QUIRK_ID(vend,dev) {_SND_PCI_QUIRK_ID(vend, dev)}
  384. #ifdef CONFIG_SND_DEBUG_VERBOSE
  385. #define SND_PCI_QUIRK(vend,dev,xname,val) \
  386. {_SND_PCI_QUIRK_ID(vend, dev), .value = (val), .name = (xname)}
  387. #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
  388. {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val), .name = (xname)}
  389. #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
  390. {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), \
  391. .value = (val), .name = (xname)}
  392. #else
  393. #define SND_PCI_QUIRK(vend,dev,xname,val) \
  394. {_SND_PCI_QUIRK_ID(vend, dev), .value = (val)}
  395. #define SND_PCI_QUIRK_MASK(vend, mask, dev, xname, val) \
  396. {_SND_PCI_QUIRK_ID_MASK(vend, mask, dev), .value = (val)}
  397. #define SND_PCI_QUIRK_VENDOR(vend, xname, val) \
  398. {_SND_PCI_QUIRK_ID_MASK(vend, 0, 0), .value = (val)}
  399. #endif
  400. const struct snd_pci_quirk *
  401. snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list);
  402. const struct snd_pci_quirk *
  403. snd_pci_quirk_lookup_id(u16 vendor, u16 device,
  404. const struct snd_pci_quirk *list);
  405. #endif
  406. #endif /* __SOUND_CORE_H */