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