summaryrefslogtreecommitdiff
path: root/setup/FreeBSD/oss/build/osscore.c
blob: 5264118ab6fdcbbf0f17201fb9c9fc73fb942971 (plain)
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
 * Purpose: OSS core functions that need to be compiled in the target system
 *
 * Some parts of the FreeBSD operating system interface of OSS are sensitive
 * to changes in internal structures of FreeBSD. For this reason these
 * files have to be compiled in the target system when OSS is installed.
 * In this way the same OSS binary package can be used with several FreeBSD
 * versions.
 */
#include <machine/stdarg.h>
#include <sys/param.h>		/* defines used in kernel.h */
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kernel.h>		/* types used in module initialization */
#include <sys/conf.h>		/* cdevsw struct */
#include <sys/uio.h>		/* uio struct */
#include <sys/malloc.h>

#include <sys/bus.h>		/* structs, prototypes for pci bus stuff */
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/param.h>		/* defines used in kernel.h */
#include <dev/pci/pcivar.h>	/* For pci_get macros! */
#include <dev/pci/pcireg.h>
#include <machine/intr_machdep.h>

#include <vm/vm.h>
#include <vm/pmap.h>
#include <sys/proc.h>

typedef struct _oss_device_t oss_device_t;
#include "bsddefs.h"

/* The PCIBIOS_* defines must match oss_pci.h */
#define PCIBIOS_SUCCESSFUL		0x00
#define PCIBIOS_FAILED			-1

extern int soundcard_attach (void);
extern int soundcard_detach (void);

void *
memset (void *t, int val, int l)
{
  char *c = t;
  while (l-- > 0)
    *c++ = val;

  return t;
}

void
cmn_err (int level, char *s, ...)
{
  char tmp[1024], *a[6];
  va_list ap;
  int i, n = 0;

  va_start (ap, s);

  for (i = 0; i < strlen (s); i++)
    if (s[i] == '%')
      n++;

  for (i = 0; i < n && i < 6; i++)
    a[i] = va_arg (ap, char *);

  for (i = n; i < 6; i++)
    a[i] = NULL;

  strcpy (tmp, "osscore: ");
  sprintf (tmp + strlen (tmp), s, a[0], a[1], a[2], a[3], a[4], a[5], NULL,
	   NULL, NULL, NULL);
  if (level == CE_PANIC)
    panic (tmp);
  printf ("%s", tmp);
#if 0
  /* This may cause a crash under SMP */
  if (sound_started)
    store_msg (tmp);
#endif

  va_end (ap);
}

void
oss_udelay (unsigned long t)
{
  DELAY (t);
}

typedef struct
{
  int irq;
  oss_device_t *osdev;
  oss_tophalf_handler_t top;
  oss_bottomhalf_handler_t bottom;
  struct resource *irqres;
  int irqid;
  void *cookie;
} osscore_intr_t;

#define MAX_INTRS	32

static osscore_intr_t intrs[MAX_INTRS] = { {0} };
static int nintrs = 0;

static void
ossintr (void *arg)
{
  osscore_intr_t *intr = arg;
  int serviced = 0;

  if (intr->top)
    serviced = intr->top (intr->osdev);
  if (intr->bottom)
    intr->bottom (intr->osdev);
  oss_inc_intrcount (intr->osdev, serviced);
}

int
oss_register_interrupts (oss_device_t * osdev, int intrnum,
			 oss_tophalf_handler_t top,
			 oss_bottomhalf_handler_t bottom)
{

  osscore_intr_t *intr;
  char name[32];

  if (nintrs >= MAX_INTRS)
    {
      cmn_err (CE_CONT,
	       "oss_register_interrupts: Too many interrupt handlers\n");
      return -ENOMEM;
    }

  intr = &intrs[nintrs];

  intr->irq = 0;
  intr->osdev = osdev;
  intr->top = top;
  intr->bottom = bottom;

  sprintf (name, "%s%d", osdev->nick, osdev->instance);

  intr->irqid = 0;
  intr->irqres = bus_alloc_resource (osdev->dip, SYS_RES_IRQ, &(intr->irqid),
				     0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
  if (intr->irqres == NULL)
    {
      cmn_err (CE_CONT,
	       "oss_register_interrupts: bus_alloc_resource failed.\n");
      return -EIO;
    }

  intr->irq = bus_setup_intr (osdev->dip, intr->irqres,
		              INTR_TYPE_AV | INTR_MPSAFE,
#if __FreeBSD_version >= 700031
			      NULL,
#endif
			      ossintr, intr, &(intr->cookie));

  nintrs++;

  return 0;
}

void
oss_unregister_interrupts (oss_device_t * osdev)
{
  int i;

  for (i = 0; i < nintrs; i++)
    if (intrs[i].osdev == osdev)
      {
	osscore_intr_t *intr;

	intr = &intrs[i];
	bus_teardown_intr (osdev->dip, intr->irqres, intr->cookie);
	bus_release_resource (osdev->dip, SYS_RES_IRQ, intr->irqid,
			      intr->irqres);
      }
}

/*
 * PCI config space access
 */
char *
oss_pci_read_devpath (dev_info_t * dip)
{
  return "Unknown PCI path";	// TODO
}

int
pci_read_config_byte (oss_device_t * osdev, offset_t where,
		      unsigned char *val)
{
  *val = pci_read_config (osdev->dip, where, 1);
  return PCIBIOS_SUCCESSFUL;
}

int
pci_read_config_irq (oss_device_t * osdev, offset_t where, unsigned char *val)
{
  *val = pci_read_config (osdev->dip, where, 1);
  return PCIBIOS_SUCCESSFUL;
}

int
pci_read_config_word (oss_device_t * osdev, offset_t where,
		      unsigned short *val)
{
  *val = pci_read_config (osdev->dip, where, 2);
  return PCIBIOS_SUCCESSFUL;
}

int
pci_read_config_dword (oss_device_t * osdev, offset_t where,
		       unsigned int *val)
{
  *val = pci_read_config (osdev->dip, where, 4);
  return PCIBIOS_SUCCESSFUL;
}

int
pci_write_config_byte (oss_device_t * osdev, offset_t where,
		       unsigned char val)
{
  pci_write_config (osdev->dip, where, val, 1);
  return PCIBIOS_FAILED;
}

int
pci_write_config_word (oss_device_t * osdev, offset_t where,
		       unsigned short val)
{
  pci_write_config (osdev->dip, where, val, 2);
  return PCIBIOS_FAILED;
}

int
pci_write_config_dword (oss_device_t * osdev, offset_t where,
			unsigned int val)
{
  pci_write_config (osdev->dip, where, val, 4);
  return PCIBIOS_FAILED;
}

void *
oss_contig_malloc (unsigned long buffsize, unsigned long memlimit,
		   oss_native_word * phaddr)
{
  char *tmpbuf;
  *phaddr = 0;

  tmpbuf =
    (char *) contigmalloc (buffsize, M_DEVBUF, M_WAITOK, 0ul, memlimit,
			   PAGE_SIZE, 0ul);
  if (tmpbuf == NULL)
    {
      cmn_err (CE_CONT, "OSS: Unable to allocate %lu bytes for a DMA buffer\n",
	       buffsize);
      cmn_err (CE_CONT, "run soundoff and run soundon again.\n");
      return NULL;
    }
  *phaddr = vtophys (tmpbuf);
  return tmpbuf;
}

void
oss_contig_free (void *p, unsigned long sz)
{
  if (p)
    contigfree (p, sz, M_DEVBUF);
}

/* 
 * Load handler that deals with the loading and unloading of a KLD.
 */

static int
osscore_loader (struct module *m, int what, void *arg)
{
  int err = 0;

  switch (what)
    {
    case MOD_LOAD:		/* kldload */
      return soundcard_attach ();
      break;
    case MOD_UNLOAD:
      return soundcard_detach ();
      break;
    default:
      err = EINVAL;
      break;
    }
  return (err);
}

/* Declare this module to the rest of the kernel */

static moduledata_t osscore_mod = {
  "osscore",
  osscore_loader,
  NULL
};

#define _FP_SAVE(envbuf)		asm ("fnsave %0":"=m" (*envbuf));
#define _FP_RESTORE(envbuf)		asm ("frstor %0":"=m" (*envbuf));

/* SSE/SSE2 compatible macros */
#define FX_SAVE(envbuf)		asm ("fxsave %0":"=m" (*envbuf));
#define FX_RESTORE(envbuf)		asm ("fxrstor %0":"=m" (*envbuf));

static int old_arch = 0;	/* No SSE/SSE2 instructions */

#define asm __asm__

#if defined(__amd64__)
#define AMD64
#endif

static inline void
cpuid (int op, int *eax, int *ebx, int *ecx, int *edx)
{
__asm__ ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx):"0" (op), "c"
	   (0));
}

#ifdef AMD64
#  define local_save_flags(x)     asm volatile("pushfq ; popq %0":"=g" (x):)
#  define local_restore_flags(x)  asm volatile("pushq %0 ; popfq"::"g" (x):"memory", "cc")
#else
#  define local_save_flags(x)     asm volatile("pushfl ; popl %0":"=g" (x):)
#  define local_restore_flags(x)  asm volatile("pushl %0 ; popfl"::"g" (x):"memory", "cc")
#endif

static inline unsigned long
read_cr0 (void)
{
  unsigned long cr0;
#ifdef AMD64
asm ("movq %%cr0,%0":"=r" (cr0));
#else
asm ("movl %%cr0,%0":"=r" (cr0));
#endif
  return cr0;
}

static inline void
write_cr0 (unsigned long val)
{
#ifdef AMD64
  asm ("movq %0,%%cr0"::"r" (val));
#else
  asm ("movl %0,%%cr0"::"r" (val));
#endif
}

static inline unsigned long
read_cr4 (void)
{
  unsigned long cr4;
#ifdef AMD64
asm ("movq %%cr4,%0":"=r" (cr4));
#else
asm ("movl %%cr4,%0":"=r" (cr4));
#endif
  return cr4;
}

static inline void
write_cr4 (unsigned long val)
{
#ifdef AMD64
  asm ("movq %0,%%cr4"::"r" (val));
#else
  asm ("movl %0,%%cr4"::"r" (val));
#endif
}

static inline unsigned long long
read_mxcsr (void)
{
  unsigned long long mxcsr;
asm ("stmxcsr %0":"=m" (mxcsr));
  return mxcsr;
}

static inline void
write_mxcsr (unsigned long long val)
{
  asm ("ldmxcsr %0"::"m" (val));
}

int
oss_fp_check (void)
{
  int eax, ebx, ecx, edx;
#define FLAGS_ID (1<<21)

  oss_native_word flags_reg;

  local_save_flags (flags_reg);
  flags_reg &= ~FLAGS_ID;
  local_restore_flags (flags_reg);

  local_save_flags (flags_reg);
  if (flags_reg & FLAGS_ID)
    return 0;

  flags_reg |= FLAGS_ID;
  local_restore_flags (flags_reg);

  local_save_flags (flags_reg);
  if (!(flags_reg & FLAGS_ID))
    return 0;

#define OSS_CPUID_FXSR	(1<<24)
#define OSS_CPUID_SSE	(1<<25)
#define OSS_CPUID_SSE2	(1<<26)

  cpuid (1, &eax, &ebx, &ecx, &edx);

  if (!(edx & OSS_CPUID_FXSR))
    return 0;

  /*
   * Older machines require different FP handling than the latest ones. Use the SSE
   * instruction set as an indicator.
   */
  if (!(edx & OSS_CPUID_SSE))
    old_arch = 1;

  return 1;
}

void
oss_fp_save (short *envbuf, unsigned int flags[])
{
  flags[0] = read_cr0 ();
  write_cr0 (flags[0] & ~0x0e);	/* Clear CR0.TS/MP/EM */

  if (old_arch)
    {
      _FP_SAVE (envbuf);
    }
  else
    {
      flags[1] = read_cr4 ();
      write_cr4 (flags[1] | 0x600);	/* Set OSFXSR & OSXMMEXCEPT */
      FX_SAVE (envbuf);
      asm ("fninit");
      asm ("fwait");
      write_mxcsr (0x1f80);
    }
  flags[2] = read_cr0 ();
}

void
oss_fp_restore (short *envbuf, unsigned int flags[])
{
  asm ("fwait");
  if (old_arch)
    {
      _FP_RESTORE (envbuf);
    }
  else
    {
      FX_RESTORE (envbuf);
      write_cr4 (flags[1]);	/* Restore cr4 */
    }
  write_cr0 (flags[0]);		/* Restore cr0 */
}

#ifdef VDEV_SUPPORT
static void
oss_file_free_private (void *v)
{
  free (v, M_DEVBUF);
}

int
oss_file_get_private (void **v)
{
  int error;

  error = devfs_get_cdevpriv (v);
  if (error)
    {
      cmn_err (CE_CONT, "Couldn't retrieve private data from file handle!\n");
      return error;
    }
  return 0;
}

int
oss_file_set_private (struct thread *t, void *v, size_t l)
{
  int error;
  void * p;

  p = malloc (l, M_DEVBUF, M_WAITOK);
  if (p == NULL)
    {
      cmn_err (CE_CONT, "Couldn't allocate memory!\n");
      return -1;
    }
  memcpy (p, v, l);
  error = devfs_set_cdevpriv (p, oss_file_free_private);
  if (error)
    {
      cmn_err (CE_CONT, "Couldn't attach private data to file handle!\n");
      oss_file_free_private (p);
      return error;
    }
  return 0;
}
#endif

int
oss_get_uid(void)
{
  return curthread->td_ucred->cr_uid;
}

extern int max_intrate;
extern int detect_trace;
extern int src_quality;
extern int flat_device_model;
extern int vmix_disabled;
extern int vmix_loopdevs;
extern int vmix_no_autoattach;
extern int ac97_amplifier;
extern int ac97_recselect;
extern int cooked_enable;
extern int dma_buffsize;
extern int excl_policy;
extern int mixer_muted;
TUNABLE_INT("osscore.max_intrate", &max_intrate);
TUNABLE_INT("osscore.detect_trace", &detect_trace);
TUNABLE_INT("osscore.src_quality", &src_quality);
TUNABLE_INT("osscore.flat_device_model", &flat_device_model);
TUNABLE_INT("osscore.vmix_disabled", &vmix_disabled);
TUNABLE_INT("osscore.vmix_loopdevs", &vmix_loopdevs);
TUNABLE_INT("osscore.vmix_no_autoattach", &vmix_no_autoattach);
TUNABLE_INT("osscore.ac97_amplifier", &ac97_amplifier);
TUNABLE_INT("osscore.ac97_recselect", &ac97_recselect);
TUNABLE_INT("osscore.cooked_enable", &cooked_enable);
TUNABLE_INT("osscore.dma_buffsize", &dma_buffsize);
TUNABLE_INT("osscore.excl_policy", &excl_policy);
TUNABLE_INT("osscore.mixer_muted", &mixer_muted);

DECLARE_MODULE (osscore, osscore_mod, SI_SUB_KLD, SI_ORDER_ANY);
MODULE_VERSION (osscore, 4);