Source

oss4-netbsd / kernel / OS / NetBSD / os_netbsd.h

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#ifndef _OS_H_
#define _OS_H_

/*
 * Purpose: OS specific definitions for NetBSD
 *
 */
#define COPYING9 Copyright (C) Hannu Savolainen and Dev Mazumdar 2000-2007. All rights reserved.
#define OS_VERSION "6"
#define __inline__ inline
#define __inline inline
#define EXTERN_C extern "C"

/*
 * Debugging and misc settings
 */
#undef  MUTEX_CHECKS
#undef  MEMDEBUG
#undef PCI_IO_DEBUG

#if (!defined(__i386__) && !defined(__x86_64__)) || defined(CONFIG_OSS_FIXDEPOINT)
// Floating point is not supported or it's disabled
#undef CONFIG_OSS_VMIX_FLOAT
#endif

/*
 * Disable support for per-application features such as /dev/dsp device
 * selection based on command name. Requires working GET_PROCESS_NAME
 * macro implementation.
 */
#undef  APPLIST_SUPPORT
#define USE_DEVICE_SUBDIRS

#include <stdarg.h>
#include <sys/types.h>
#ifdef _KERNEL
#include <sys/systm.h>
#endif
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/fcntl.h>
#include <sys/poll.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/kernel.h>
#include <sys/select.h>
#include <sys/lwp.h>
#include <machine/pio.h>
#include <machine/vmparam.h>

#ifdef _KERNEL
#include <sys/bus.h>
#include <dev/pci/pcivar.h>
#endif
// XXX avoid conflict: I hope it wont break anything!
#undef PCI_INTERRUPT_LINE
#endif

#include <oss_errno.h>

// XXX PORT
#define MINOR(x) minor(x)

#undef timeout
#define timeout oss_timeout
#undef untimeout
#define untimeout oss_untimeout
typedef int timeout_id_t;
extern timeout_id_t oss_timeout (void (*func) (void *), void *arg,
				 unsigned long long ticks);
extern void oss_untimeout (timeout_id_t id);

#define uiomove oss_uiomove
typedef struct uio uio_t;
extern int oss_uiomove (void *address, size_t nbytes, enum uio_rw rwflag,
			uio_t * uio_p);

#undef HZ
#define OSS_HZ hz

typedef struct device dev_info_t;
typedef int64_t oss_int64_t;		/* Signed 64 bit integer */
typedef uint64_t oss_uint64_t;		/* Unsigned 64 bit integer */
typedef off_t offset_t;

/*
 * Some integer types
 */
#if defined(__amd64__)
typedef unsigned long long oss_native_word;	/* Same as the address and status register size */
#else
typedef unsigned long oss_native_word;	/* Same as the address and status register size */
#endif

/* The soundcard.h could be in a nonstandard place so include it here. */
#include "soundcard.h"

typedef struct udi_usb_devc udi_usb_devc;

#define ALLOW_SELECT
#define ALLOW_BUFFER_MAPPING

/*
 * Sleep/wakeup
 */

#ifdef _KERNEL
struct oss_wait_queue
{
  kcondvar_t cv;
  short pollevents;
  //  unsigned long flags;
  struct selinfo *sip;
};
#endif

/* Busy wait routine */

/* System wall timer access */
extern unsigned long oss_get_time (void);
#define GET_JIFFIES()	oss_get_time()

/*
 * Mutexes
 */

#ifdef MUTEX_CHECKS
/* Debugging version */
struct _oss_mutex_t
{
  kmutex_t mu;
  char *file;
  int line;
  int busy_flags;
#define CNTX_INTR		1
#define CNTX_USER		2
};

typedef struct _oss_mutex_t oss_mutex_t;
extern void debug_mutex_init (oss_mutex_t * mutex, void *dummy, int typ,
			      void *arg, char *file, int line);
extern void debug_mutex_destroy (oss_mutex_t * mutex, char *file, int line);
extern void debug_mutex_enter (oss_mutex_t * mutex, char *file, int line);
extern void debug_mutex_exit (oss_mutex_t * mutex, char *file, int line);

#define MUTEX_INIT(osdev, mutex, hier) \
  debug_mutex_init(&mutex, __FILE__, __LINE__)
#define MUTEX_CLEANUP(mutex) \
  debug_mutex_destroy(&mutex, __FILE__, __LINE__)
#define MUTEX_ENTER_IRQDISABLE(mutex, flags) \
  flags=0;debug_mutex_enter(&mutex, __FILE__, __LINE__)
#define MUTEX_ENTER(mutex, flags) \
  flags=0;debug_mutex_enter(&mutex, __FILE__, __LINE__)
#define MUTEX_EXIT_IRQRESTORE(mutex, flags) \
  debug_mutex_exit(&mutex, __FILE__, __LINE__);(flags)++
#define MUTEX_EXIT(mutex, flags) \
  debug_mutex_exit(&mutex, __FILE__, __LINE__);(flags)++

#else
typedef kmutex_t oss_mutex_t;

#define MUTEX_INIT(osdev, mutex, hier) \
  mutex_init(&mutex, MUTEX_DRIVER, IPL_NONE)
#define MUTEX_CLEANUP(mutex) \
  mutex_destroy(&mutex)
#define MUTEX_ENTER_IRQDISABLE(mutex, flags) \
  flags=0;mutex_enter(&mutex)
#define MUTEX_ENTER(mutex, flags) \
  flags=0;mutex_enter(&mutex)
#define MUTEX_EXIT_IRQRESTORE(mutex, flags) \
  mutex_exit(&mutex);(flags)++
#define MUTEX_EXIT(mutex, flags) \
  mutex_exit(&mutex);(flags)++

#endif	/* MUTEX_CHECKS */

/*
 * INB() and OUTB() should be obvious. NOTE! The order of
 * paratemeters of OUTB() is different than on some other
 * operating systems.
 */

/* Memory Mapped devices */
#ifdef PCI_IO_DEBUG

void oss_outb(oss_device_t *osdev, unsigned int addr, unsigned char data);
void oss_outw(oss_device_t *osdev, unsigned int addr, unsigned short data);
void oss_outl(oss_device_t *osdev, unsigned int addr, unsigned int data);
unsigned char oss_inb (oss_device_t *osdev, unsigned int addr);
unsigned short oss_inw (oss_device_t *osdev, unsigned int addr);
unsigned int oss_inl (oss_device_t *osdev, unsigned int addr);

unsigned int oss_pci_readl (oss_device_t * osdev, void *addr);
unsigned short oss_pci_readw (oss_device_t * osdev, void *addr);
unsigned char oss_pci_readb (oss_device_t *osdev, void *addr);
unsigned int oss_pci_writel(oss_device_t *osdev, void *addr, unsigned int data);
unsigned short oss_pci_writew(oss_device_t *osdev, void *addr, unsigned short data);
unsigned char oss_pci_writeb(oss_device_t *osdev, void *addr, unsigned char data);

/* I/O Mapped devices */
#define INB(o, p)	oss_inb(o, p)
#define INW(o, p)	oss_inw(o, p)
#define INL(o, p)	oss_inl(o, p)

#define OUTB(o, v, p)	oss_outb(o, p, v)
#define OUTW(o, v, p)	oss_outw(o, p, v)
#define OUTL(o, v, p)	oss_outl(o, p, v)

#define PCI_READL(osdev, p) \
  oss_pci_readl(osdev, p)
#define PCI_WRITEL(osdev, addr, data) \
  oss_pci_writel(osdev, addr, data)
#define PCI_READW(osdev, p) \
  oss_pci_readw(osdev, p)
#define PCI_WRITEW(osdev, addr, data)	\
  oss_pci_writew(osdev, addr, data)  
#define PCI_READB(osdev, p)			\
  oss_pci_readb(osdev, p)
#define PCI_WRITEB(osdev, addr, data) \
  oss_pci_writeb(osdev, addr, data)  

#else

/* I/O Mapped devices */
#define INB(o, p)	inb(p)
#define INW(o, p)	inw(p)
#define INL(o, p)	inl(p)

#define OUTB(o, v, p)	outb(p,v)
#define OUTW(o, v, p)	outw(p,v)
#define OUTL(o, v, p)	outl(p,v)

#define PCI_READL(osdev, p) \
  (*(volatile unsigned int *) (p))
#define PCI_WRITEL(osdev, addr, data) \
  (*(volatile unsigned int *) (addr) = (data))
#define PCI_READW(osdev, p) \
  (*(volatile unsigned short *) (p))
#define PCI_WRITEW(osdev, addr, data) \
  (*(volatile unsigned short *) (addr) = (data))
#define PCI_READB(osdev, p) \
  (*(volatile unsigned char *) (p))
#define PCI_WRITEB(osdev, addr, data) \
  (*(volatile unsigned char *) (addr) = (data))

#endif

/* 
   KERNEL_MALLOC() allocates requested number of memory  and 
   KERNEL_FREE is used to free it. 
   These macros are never called from interrupt, in addition the
   nbytes will never be more than 4096 bytes. Generally the driver
   will allocate memory in blocks of 4k. If the kernel has just a
   page level memory allocation, 4K can be safely used as the size
   (the nbytes parameter can be ignored).
*/

#define CONTIG_MALLOC(osdev, sz, memlimit, phaddr, handle) \
  oss_contig_malloc(osdev, sz, memlimit, phaddr, &handle)

#define CONTIG_FREE(osdev, p, sz, handle) \
  oss_contig_free(osdev, handle)

#ifndef SOLARIS_STYLE_ALLOCATOR

#define	KERNEL_MALLOC(nbytes) \
  malloc(nbytes, M_DEVBUF, M_NOWAIT|M_ZERO)

#define	KERNEL_FREE(addr) \
  { if (addr)free(addr, M_DEVBUF); addr = NULL; }

#else

#ifdef MEMDEBUG
extern void *oss_kmem_alloc (size_t size, int flags, char *file, int line);
extern void oss_kmem_free (void *addr);

#define KERNEL_MALLOC(nbytes) \
  oss_kmem_alloc(nbytes, KM_SLEEP, __FILE__, __LINE__)
#define KERNEL_FREE(addr) \
  oss_kmem_free(addr)

#else

extern void *oss_kmem_alloc (size_t size, int flags);
extern void oss_kmem_free (void *addr);

#define KERNEL_MALLOC(nbytes) \
  oss_kmem_alloc(nbytes, KM_SLEEP)
#define KERNEL_FREE(addr) \
  oss_kmem_free(addr)

#endif	/* MEMDEBUG */

#endif	/* SOLARIS_STYLE_ALLOCATOR */

/*
 * Timer macros
 *
 * These macros are obsolete and should not be used in any new code.
 * Use the timeout mechanism (see the timeout(9F) Solaris man page).
 */
#define DEFINE_TIMER(name, proc) \
  static timeout_id_t name = 0
#define REMOVE_TIMER(name, proc) \
  {if (name != 0) oss_untimeout(name);}
#define INIT_TIMER(name,proc) \
  typedef void (*timeout_func_t) (void *);
#define ACTIVATE_TIMER(name, proc, time) \
  name = oss_timeout((timeout_func_t)proc, (void*)&name, time)

struct fileinfo
{
  int mode;			/* Open mode */
  int acc_flags;
  int pid;
  int dev;
  char *cmd;
};
#define ISSET_FILE_FLAG(fileinfo, flag)  (fileinfo->acc_flags & (flag) ? 1:0)

#define OSS_OS "NetBSD"
#define OSS_OS_LONGNAME "NetBSD " OS_VERSION

typedef void (*softintr_func_t) (int);

struct oss_softintr
{
  int id;
  softintr_func_t func;
  volatile int armed, running;
};

struct _oss_poll_event_t
{
  short events, revents;
  struct lwp *p;
  dev_t bsd_dev;
};
typedef struct _oss_poll_event_t oss_poll_event_t;

extern int detect_trace;
#define DDB(x) if (detect_trace) x

extern void *oss_map_pci_mem (oss_device_t * osdev, int nr, int paddr,
			      int psize);
extern void oss_unmap_pci_mem (oss_device_t * osdev, int nr, int psize);

#define MAP_PCI_IOADDR(osdev, nr, io) \
  (oss_native_word)(io)
#define MAP_PCI_MEM(osdev, nr, phaddr, size) \
  oss_map_pci_mem(osdev, nr, phaddr, size)
#define UNMAP_PCI_MEM(osdev, nr, ph, virt, size) \
  oss_unmap_pci_mem(osdev, nr, size)
#define UNMAP_PCI_IOADDR(osdev, nr) \
  {}

#define GET_PROCESS_PID(f)	f->pid
#define GET_PROCESS_UID(x)	oss_get_uid()
#define GET_PROCESS_NAME(f)	curlwp->l_name

#define abs(x)                  ((x) >= 0 ? (x) : -(x))

extern int oss_get_uid (void);

extern void cmn_err (int level, char *format, ...);
#define CE_CONT		0
#define CE_NOTE		1
#define CE_WARN		2
#define CE_PANIC	3

/*
 * PCI config space access (in os.c)
 */
extern char *oss_pci_read_devpath (dev_info_t * dip);

#ifdef _KERNEL
struct _oss_device_t
{
  dev_info_t *dip;
  void *osid;
  void *devc;
  char *name;
  char *hw_info;
  int cardnum;
  int dev_type;
  int instance;
  int available;
  int major;
  char nick[16];
  char modname[16];
  char handle[32];
  int num_audio_engines;
  int num_audioplay, num_audiorec, num_audioduplex;
  int num_mididevs;
  int num_mixerdevs;
  int num_loopdevs;
  int first_mixer;	/* This must be set to -1 by osdev_create() */

  int intrcount;
  int ackcount;
  volatile int refcount; /* Nonzero means that the device is needed by some other (virtual) driver. */
  bus_space_handle_t bus_space_handles[6]; /* I hope that will be enough */
  struct pci_attach_args pa;
};

typedef struct _netbsd_dma_handle {
  void *kva;
  bus_dma_segment_t seg;
  bus_dmamap_t map;
  size_t size;
  int nseg;
} *oss_dma_handle_t;

typedef int ddi_iblock_cookie_t;

extern void oss_udelay (unsigned long t);

extern oss_device_t *osdev_create (dev_info_t * dip, int dev_type,
				   int instance, const char *nick,
				   const char *handle);
extern void osdev_delete (oss_device_t * osdev);

extern char *oss_pci_read_devpath (dev_info_t * dip);
extern int pci_read_config_byte (oss_device_t * osdev, offset_t where,
				 unsigned char *val);
extern int pci_read_config_irq (oss_device_t * osdev, offset_t where,
				unsigned char *val);
extern int pci_read_config_word (oss_device_t * osdev, offset_t where,
				 unsigned short *val);
extern int pci_read_config_dword (oss_device_t * osdev, offset_t where,
				  unsigned int *val);
extern int pci_write_config_byte (oss_device_t * osdev, offset_t where,
				  unsigned char val);
extern int pci_write_config_word (oss_device_t * osdev, offset_t where,
				  unsigned short val);
extern int pci_write_config_dword (oss_device_t * osdev, offset_t where,
				   unsigned int val);
#ifndef OSS_CONFIG_H
/* These definitions must match with oss_config.h */
typedef int (*oss_tophalf_handler_t) (struct _oss_device_t * osdev);
typedef void (*oss_bottomhalf_handler_t) (struct _oss_device_t * osdev);
#endif

extern int oss_register_interrupts (oss_device_t * osdev, int intrnum,
				    oss_tophalf_handler_t top,
				    oss_bottomhalf_handler_t bottom);
extern void oss_unregister_interrupts (oss_device_t * osdev);

void *oss_contig_malloc (oss_device_t * osdev, int size, oss_uint64_t memlimit,
			 oss_native_word * phaddr, oss_dma_handle_t * handlep);
void oss_contig_free (oss_device_t * osdev, oss_dma_handle_t handle);

extern void oss_register_module (char *name);
extern void oss_unregister_module (char *name);
extern void *oss_find_minor_info (int dev_class, int instance);
extern int oss_find_minor (int dev_class, int instance);
extern void oss_inc_intrcount (oss_device_t * osdev, int claimed);
#else
/* define as incomplete type for audio_core.h */
struct _netbsd_dma_handle;
typedef struct _netbsd_dma_handle *oss_dma_handle_t;
#endif	/* _KERNEL */

#define FP_SUPPORT

#ifdef FP_SUPPORT
typedef short fp_env_t[512];
typedef unsigned int fp_flags_t[4];
extern int oss_fp_check (void);
extern void oss_fp_save (short *envbuf, fp_flags_t flags);
extern void oss_fp_restore (short *envbuf, fp_flags_t flags);
#   define FP_SAVE(envbuf, flags)		oss_fp_save(envbuf, flags)
#   define FP_RESTORE(envbuf, flags)		oss_fp_restore(envbuf, flags)
#endif