ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / sound / core.h
1 #ifndef __SOUND_CORE_H
2 #define __SOUND_CORE_H
3
4 /*
5  *  Main header file for the ALSA driver
6  *  Copyright (c) 1994-2001 by Jaroslav Kysela <perex@suse.cz>
7  *
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */
24
25 #include <linux/sched.h>                /* wake_up() */
26 #include <asm/semaphore.h>              /* struct semaphore */
27 #include <linux/rwsem.h>                /* struct rw_semaphore */
28 #include <linux/workqueue.h>            /* struct workqueue_struct */
29
30 /* Typedef's */
31 typedef struct timespec snd_timestamp_t;
32 typedef struct sndrv_interval snd_interval_t;
33 typedef enum sndrv_card_type snd_card_type;
34 typedef struct sndrv_xferi snd_xferi_t;
35 typedef struct sndrv_xfern snd_xfern_t;
36 typedef struct sndrv_xferv snd_xferv_t;
37
38 /* forward declarations */
39 #ifdef CONFIG_PCI
40 struct pci_dev;
41 #endif
42 #ifdef CONFIG_SBUS
43 struct sbus_dev;
44 #endif
45
46 /* device allocation stuff */
47
48 #define SNDRV_DEV_TYPE_RANGE_SIZE               0x1000
49
50 typedef enum {
51         SNDRV_DEV_TOPLEVEL =            (0*SNDRV_DEV_TYPE_RANGE_SIZE),
52         SNDRV_DEV_LOWLEVEL_PRE,
53         SNDRV_DEV_LOWLEVEL_NORMAL =     (1*SNDRV_DEV_TYPE_RANGE_SIZE),
54         SNDRV_DEV_PCM,
55         SNDRV_DEV_RAWMIDI,
56         SNDRV_DEV_TIMER,
57         SNDRV_DEV_SEQUENCER,
58         SNDRV_DEV_HWDEP,
59         SNDRV_DEV_INFO,
60         SNDRV_DEV_LOWLEVEL =            (2*SNDRV_DEV_TYPE_RANGE_SIZE)
61 } snd_device_type_t;
62
63 typedef enum {
64         SNDRV_DEV_BUILD,
65         SNDRV_DEV_REGISTERED,
66         SNDRV_DEV_DISCONNECTED
67 } snd_device_state_t;
68
69 typedef enum {
70         SNDRV_DEV_CMD_PRE = 0,
71         SNDRV_DEV_CMD_NORMAL = 1,
72         SNDRV_DEV_CMD_POST = 2
73 } snd_device_cmd_t;
74
75 typedef struct _snd_card snd_card_t;
76 typedef struct _snd_device snd_device_t;
77
78 typedef int (snd_dev_free_t)(snd_device_t *device);
79 typedef int (snd_dev_register_t)(snd_device_t *device);
80 typedef int (snd_dev_disconnect_t)(snd_device_t *device);
81 typedef int (snd_dev_unregister_t)(snd_device_t *device);
82
83 typedef struct {
84         snd_dev_free_t *dev_free;
85         snd_dev_register_t *dev_register;
86         snd_dev_disconnect_t *dev_disconnect;
87         snd_dev_unregister_t *dev_unregister;
88 } snd_device_ops_t;
89
90 struct _snd_device {
91         struct list_head list;          /* list of registered devices */
92         snd_card_t *card;               /* card which holds this device */
93         snd_device_state_t state;       /* state of the device */
94         snd_device_type_t type;         /* device type */
95         void *device_data;              /* device structure */
96         snd_device_ops_t *ops;          /* operations */
97 };
98
99 #define snd_device(n) list_entry(n, snd_device_t, list)
100
101 /* various typedefs */
102
103 typedef struct snd_info_entry snd_info_entry_t;
104 typedef struct _snd_pcm snd_pcm_t;
105 typedef struct _snd_pcm_str snd_pcm_str_t;
106 typedef struct _snd_pcm_substream snd_pcm_substream_t;
107 typedef struct _snd_mixer snd_kmixer_t;
108 typedef struct _snd_rawmidi snd_rawmidi_t;
109 typedef struct _snd_ctl_file snd_ctl_file_t;
110 typedef struct _snd_kcontrol snd_kcontrol_t;
111 typedef struct _snd_timer snd_timer_t;
112 typedef struct _snd_timer_instance snd_timer_instance_t;
113 typedef struct _snd_hwdep snd_hwdep_t;
114 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
115 typedef struct _snd_oss_mixer snd_mixer_oss_t;
116 #endif
117
118 /* monitor files for graceful shutdown (hotplug) */
119
120 struct snd_monitor_file {
121         struct file *file;
122         struct snd_monitor_file *next;
123 };
124
125 struct snd_shutdown_f_ops;      /* define it later */
126
127 /* main structure for soundcard */
128
129 struct _snd_card {
130         int number;                     /* number of soundcard (index to snd_cards) */
131
132         char id[16];                    /* id string of this card */
133         char driver[16];                /* driver name */
134         char shortname[32];             /* short name of this soundcard */
135         char longname[80];              /* name of this soundcard */
136         char mixername[80];             /* mixer name */
137         char components[80];            /* card components delimited with space */
138
139         struct module *module;          /* top-level module */
140
141         void *private_data;             /* private data for soundcard */
142         void (*private_free) (snd_card_t *card); /* callback for freeing of private data */
143
144         struct list_head devices;       /* devices */
145
146         unsigned int last_numid;        /* last used numeric ID */
147         struct rw_semaphore controls_rwsem;     /* controls list lock */
148         rwlock_t ctl_files_rwlock;      /* ctl_files list lock */
149         int controls_count;             /* count of all controls */
150         struct list_head controls;      /* all controls for this card */
151         struct list_head ctl_files;     /* active control files */
152
153         snd_info_entry_t *proc_root;    /* root for soundcard specific files */
154         snd_info_entry_t *proc_id;      /* the card id */
155         struct proc_dir_entry *proc_root_link;  /* number link to real id */
156
157         struct snd_monitor_file *files; /* all files associated to this card */
158         struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown state */
159         spinlock_t files_lock;          /* lock the files for this card */
160         int shutdown;                   /* this card is going down */
161         wait_queue_head_t shutdown_sleep;
162         struct work_struct free_workq;  /* for free in workqueue */
163         struct device *dev;
164
165 #ifdef CONFIG_PM
166         int (*set_power_state) (snd_card_t *card, unsigned int state);
167         void *power_state_private_data;
168         unsigned int power_state;       /* power state */
169         struct semaphore power_lock;    /* power lock */
170         wait_queue_head_t power_sleep;
171 #endif
172
173 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
174         snd_mixer_oss_t *mixer_oss;
175         int mixer_oss_change_count;
176 #endif
177 };
178
179 #ifdef CONFIG_PM
180 static inline void snd_power_lock(snd_card_t *card)
181 {
182         down(&card->power_lock);
183 }
184
185 static inline void snd_power_unlock(snd_card_t *card)
186 {
187         up(&card->power_lock);
188 }
189
190 int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file);
191
192 static inline unsigned int snd_power_get_state(snd_card_t *card)
193 {
194         return card->power_state;
195 }
196
197 static inline void snd_power_change_state(snd_card_t *card, unsigned int state)
198 {
199         card->power_state = state;
200         wake_up(&card->power_sleep);
201 }
202 #else
203 #define snd_power_lock(card)            do { (void)(card); } while (0)
204 #define snd_power_unlock(card)          do { (void)(card); } while (0)
205 static inline int snd_power_wait(snd_card_t *card, unsigned int state, struct file *file) { return 0; }
206 #define snd_power_get_state(card)       SNDRV_CTL_POWER_D0
207 #define snd_power_change_state(card, state)     do { (void)(card); } while (0)
208 #endif
209
210 /* device.c */
211
212 struct _snd_minor {
213         struct list_head list;          /* list of all minors per card */
214         int number;                     /* minor number */
215         int device;                     /* device number */
216         const char *comment;            /* for /proc/asound/devices */
217         struct file_operations *f_ops;  /* file operations */
218         char name[0];                   /* device name (keep at the end of structure) */
219 };
220
221 typedef struct _snd_minor snd_minor_t;
222
223 /* sound.c */
224
225 extern int snd_ecards_limit;
226
227 void snd_request_card(int card);
228
229 int snd_register_device(int type, snd_card_t *card, int dev, snd_minor_t *reg, const char *name);
230 int snd_unregister_device(int type, snd_card_t *card, int dev);
231
232 #ifdef CONFIG_SND_OSSEMUL
233 int snd_register_oss_device(int type, snd_card_t *card, int dev, snd_minor_t *reg, const char *name);
234 int snd_unregister_oss_device(int type, snd_card_t *card, int dev);
235 #endif
236
237 int snd_minor_info_init(void);
238 int snd_minor_info_done(void);
239
240 /* sound_oss.c */
241
242 #ifdef CONFIG_SND_OSSEMUL
243 int snd_minor_info_oss_init(void);
244 int snd_minor_info_oss_done(void);
245 int snd_oss_init_module(void);
246 #else
247 #define snd_minor_info_oss_init() /*NOP*/
248 #define snd_minor_info_oss_done() /*NOP*/
249 #define snd_oss_init_module() /*NOP*/
250 #endif
251
252 /* memory.c */
253
254 #ifdef CONFIG_SND_DEBUG_MEMORY
255 void snd_memory_init(void);
256 void snd_memory_done(void);
257 int snd_memory_info_init(void);
258 int snd_memory_info_done(void);
259 void *snd_hidden_kmalloc(size_t size, int flags);
260 void snd_hidden_kfree(const void *obj);
261 void *snd_hidden_vmalloc(unsigned long size);
262 void snd_hidden_vfree(void *obj);
263 #define kmalloc(size, flags) snd_hidden_kmalloc(size, flags)
264 #define kfree(obj) snd_hidden_kfree(obj)
265 #define vmalloc(size) snd_hidden_vmalloc(size)
266 #define vfree(obj) snd_hidden_vfree(obj)
267 #define kmalloc_nocheck(size, flags) snd_wrapper_kmalloc(size, flags)
268 #define vmalloc_nocheck(size) snd_wrapper_vmalloc(size)
269 #define kfree_nocheck(obj) snd_wrapper_kfree(obj)
270 #define vfree_nocheck(obj) snd_wrapper_vfree(obj)
271 #else
272 #define snd_memory_init() /*NOP*/
273 #define snd_memory_done() /*NOP*/
274 #define snd_memory_info_init() /*NOP*/
275 #define snd_memory_info_done() /*NOP*/
276 #define kmalloc_nocheck(size, flags) kmalloc(size, flags)
277 #define vmalloc_nocheck(size) vmalloc(size)
278 #define kfree_nocheck(obj) kfree(obj)
279 #define vfree_nocheck(obj) vfree(obj)
280 #endif
281 void *snd_kcalloc(size_t size, int flags);
282 char *snd_kmalloc_strdup(const char *string, int flags);
283 int copy_to_user_fromio(void *dst, unsigned long src, size_t count);
284 int copy_from_user_toio(unsigned long dst, const void *src, size_t count);
285
286 /* init.c */
287
288 extern int snd_cards_count;
289 extern unsigned int snd_cards_lock;
290 extern snd_card_t *snd_cards[SNDRV_CARDS];
291 extern rwlock_t snd_card_rwlock;
292 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
293 #define SND_MIXER_OSS_NOTIFY_REGISTER   0
294 #define SND_MIXER_OSS_NOTIFY_DISCONNECT 1
295 #define SND_MIXER_OSS_NOTIFY_FREE       2
296 extern int (*snd_mixer_oss_notify_callback)(snd_card_t *card, int cmd);
297 #endif
298
299 snd_card_t *snd_card_new(int idx, const char *id,
300                          struct module *module, int extra_size);
301 int snd_card_disconnect(snd_card_t *card);
302 int snd_card_free(snd_card_t *card);
303 int snd_card_free_in_thread(snd_card_t *card);
304 int snd_card_register(snd_card_t *card);
305 int snd_card_info_init(void);
306 int snd_card_info_done(void);
307 int snd_component_add(snd_card_t *card, const char *component);
308 int snd_card_file_add(snd_card_t *card, struct file *file);
309 int snd_card_file_remove(snd_card_t *card, struct file *file);
310
311 #ifndef snd_card_set_dev
312 #define snd_card_set_dev(card,devptr) ((card)->dev = (devptr))
313 #endif
314
315 /* device.c */
316
317 int snd_device_new(snd_card_t *card, snd_device_type_t type,
318                    void *device_data, snd_device_ops_t *ops);
319 int snd_device_register(snd_card_t *card, void *device_data);
320 int snd_device_register_all(snd_card_t *card);
321 int snd_device_disconnect(snd_card_t *card, void *device_data);
322 int snd_device_disconnect_all(snd_card_t *card);
323 int snd_device_free(snd_card_t *card, void *device_data);
324 int snd_device_free_all(snd_card_t *card, snd_device_cmd_t cmd);
325
326 /* isadma.c */
327
328 #define DMA_MODE_NO_ENABLE      0x0100
329
330 void snd_dma_program(unsigned long dma, unsigned long addr, unsigned int size, unsigned short mode);
331 void snd_dma_disable(unsigned long dma);
332 unsigned int snd_dma_pointer(unsigned long dma, unsigned int size);
333
334 /* misc.c */
335
336 int snd_task_name(struct task_struct *task, char *name, size_t size);
337 #ifdef CONFIG_SND_VERBOSE_PRINTK
338 void snd_verbose_printk(const char *file, int line, const char *format, ...)
339      __attribute__ ((format (printf, 3, 4)));
340 #endif
341 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_VERBOSE_PRINTK)
342 void snd_verbose_printd(const char *file, int line, const char *format, ...)
343      __attribute__ ((format (printf, 3, 4)));
344 #endif
345
346 /* --- */
347
348 #ifdef CONFIG_SND_VERBOSE_PRINTK
349 /**
350  * snd_printk - printk wrapper
351  * @fmt: format string
352  *
353  * Works like print() but prints the file and the line of the caller
354  * when configured with CONFIG_SND_VERBOSE_PRINTK.
355  */
356 #define snd_printk(fmt, args...) \
357         snd_verbose_printk(__FILE__, __LINE__, fmt ,##args)
358 #else
359 #define snd_printk(fmt, args...) \
360         printk(fmt ,##args)
361 #endif
362
363 #ifdef CONFIG_SND_DEBUG
364
365 #define __ASTRING__(x) #x
366
367 #ifdef CONFIG_SND_VERBOSE_PRINTK
368 /**
369  * snd_printd - debug printk
370  * @format: format string
371  *
372  * Compiled only when Works like snd_printk() for debugging purpose.
373  * Ignored when CONFIG_SND_DEBUG is not set.
374  */
375 #define snd_printd(fmt, args...) \
376         snd_verbose_printd(__FILE__, __LINE__, fmt ,##args)
377 #else
378 #define snd_printd(fmt, args...) \
379         printk(fmt ,##args)
380 #endif
381 /**
382  * snd_assert - run-time assersion macro
383  * @expr: expression
384  * @args...: the action
385  *
386  * This macro checks the expression in run-time and invokes the commands
387  * given in the rest arguments if the assertion is failed.
388  * When CONFIG_SND_DEBUG is not set, the expression is executed but
389  * not checked.
390  */
391 #define snd_assert(expr, args...) do {\
392         if (!(expr)) {\
393                 snd_printk("BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
394                 args;\
395         }\
396 } while (0)
397 /**
398  * snd_runtime_check - run-time assersion macro
399  * @expr: expression
400  * @args...: the action
401  *
402  * This macro checks the expression in run-time and invokes the commands
403  * given in the rest arguments if the assertion is failed.
404  * Unlike snd_assert(), the action commands are executed even if
405  * CONFIG_SND_DEBUG is not set but without any error messages.
406  */
407 #define snd_runtime_check(expr, args...) do {\
408         if (!(expr)) {\
409                 snd_printk("ERROR (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
410                 args;\
411         }\
412 } while (0)
413
414 #else /* !CONFIG_SND_DEBUG */
415
416 #define snd_printd(fmt, args...)        /* nothing */
417 #define snd_assert(expr, args...)       (void)(expr)
418 #define snd_runtime_check(expr, args...) do { if (!(expr)) { args; } } while (0)
419
420 #endif /* CONFIG_SND_DEBUG */
421
422 #ifdef CONFIG_SND_DEBUG_DETECT
423 /**
424  * snd_printdd - debug printk
425  * @format: format string
426  *
427  * Compiled only when Works like snd_printk() for debugging purpose.
428  * Ignored when CONFIG_SND_DEBUG_DETECT is not set.
429  */
430 #define snd_printdd(format, args...) snd_printk(format, ##args)
431 #else
432 #define snd_printdd(format, args...) /* nothing */
433 #endif
434
435 #define snd_BUG() snd_assert(0, )
436
437
438 static inline void snd_timestamp_now(struct timespec *tstamp, int timespec)
439 {
440         struct timeval val;
441         /* FIXME: use a linear time source */
442         do_gettimeofday(&val);
443         tstamp->tv_sec = val.tv_sec;
444         tstamp->tv_nsec = val.tv_usec;
445         if (timespec)
446                 tstamp->tv_nsec *= 1000L;
447 }
448
449 static inline void snd_timestamp_zero(struct timespec *tstamp)
450 {
451         tstamp->tv_sec = 0;
452         tstamp->tv_nsec = 0;
453 }
454
455 static inline int snd_timestamp_null(struct timespec *tstamp)
456 {
457         return tstamp->tv_sec == 0 && tstamp->tv_nsec == 0;
458 }
459
460 #define SNDRV_OSS_VERSION         ((3<<16)|(8<<8)|(1<<4)|(0))   /* 3.8.1a */
461
462 #endif /* __SOUND_CORE_H */