oss4-netbsd / os_cmd / SunOS / ossdetect / ossdetect.c

  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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
 * Purpose: OSS device autodetection utility for Solaris
 */
#define COPYING Copyright (C) Hannu Savolainen and Dev Mazumdar 2006. All rights reserved.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libdevinfo.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <ctype.h>
#include <sys/systeminfo.h>

static int use_force = 0;
static char arch[32] = "";
static int install_imux = 0;
static int install_userdev = 0;
static char *safe_mode="";

/* List of all modules installed in the system (OSS and non-oss) */
#define MAX_MODS 512
static char *installed_modules[MAX_MODS];
static int nmods = 0;


FILE *drivers_f = NULL;

static const char *ossdevs[] = {
  "sndstat",
  "mixer",
  "dsp",
/*      "audio", */
  "midi",
  "sequencer",
  "music",
  NULL
};

static void
scan_devdir (char *path)
{
  int i, ok;
  DIR *dir;
  struct dirent *de;
  struct stat st;

  char fullname[256], *devpart, devclass[256], *s;
  char devname[256];

  if ((dir = opendir (path)) == NULL)
    {
      perror (path);
      exit (-1);
    }

  while ((de = readdir (dir)) != NULL)
    {
      if (de->d_name[0] == '.')
	continue;

      sprintf (fullname, "%s/%s", path, de->d_name);

      if (stat (fullname, &st) == -1)
	{
	  perror (fullname);
	  continue;
	}

      if (S_ISDIR (st.st_mode))
	{
	  scan_devdir (fullname);
	  continue;
	}

#if 1
      if (!S_ISCHR (st.st_mode))
	continue;
#endif

/*
 * Find the device name part (after ":").
 */
      devpart = fullname;
      while (*devpart)
	{
	  if (*devpart == ':')
	    {
	      devpart++;
	      break;
	    }

	  devpart++;
	}

      strcpy (devclass, devpart);
      s = devclass;
      while (*s)
	{
	  if (isdigit (*s))
	    *s = 0;
	  else
	    s++;
	}

/*
 * Check if this kind of devices are known by OSS
 */
      ok = 0;

      for (i = 0; !ok && ossdevs[i] != NULL; i++)
	if (strcmp (devclass, ossdevs[i]) == 0)
	  ok = 1;

      if (!ok)
	continue;

      sprintf (devname, "/dev/%s", devpart);
      unlink (devname);

      if (symlink (fullname, devname) == -1)
	{
	  perror (devname);
	}
    }

  closedir (dir);
}

typedef struct
{
  char *driver;
  char *name;
  char idlist[128];
  int nids;
  char *ids[32];
  int pass;
} module_def_t;

typedef struct
{
  char *key, *driver, *name;
  int is_3rdparty, reload;
} driver_def_t;

#define MAX_DRIVERS	1000
static driver_def_t drivers[MAX_DRIVERS];
static int ndrivers = 0;

#define MAX_MODULES	32
static module_def_t modules[MAX_MODULES];
static int nmodules = 0;

static int verbose = 0;

static void
install_driver (driver_def_t * drv, char *drv_id)
{
  int i, j;

  if (verbose > 0)
    printf ("Install module '%s' (%s)\n", drv->driver, drv_id);

  for (i = 0; i < nmodules; i++)
    if (strcmp (drv->driver, modules[i].driver) == 0)
      {
	for (j = 0; j < modules[i].nids; j++)
	  {
	    if (strcmp (drv_id, modules[i].ids[j]) == 0)
	      return;
	  }

	strcat (modules[i].idlist, " \"");
	strcat (modules[i].idlist, drv_id);
	strcat (modules[i].idlist, "\"");

	if (modules[i].nids < 32)
	  {
	    int n = modules[i].nids++;
	    modules[i].ids[n] = strdup (drv_id);
	  }
	return;
      }

  if (nmodules >= MAX_MODULES)
    {
      fprintf (stderr, "Too many OSS modules\n");
      exit (-1);
    }

  modules[nmodules].name = drv->name;
  modules[nmodules].driver = drv->driver;
  sprintf (modules[nmodules].idlist, "\"%s\"", drv_id);
  modules[nmodules].nids = 1;
  modules[nmodules].ids[0] = strdup (drv_id);
  modules[nmodules].pass = 0;

/*
 * Hot-pluggable modules should be loaded after other modules.
 */
  if (strcmp (drv->driver, "oss_usb") == 0)
    modules[nmodules].pass = 1;

  nmodules++;
}

static void
find_and_install_driver (char *key)
{

  int i;

  for (i = 0; i < ndrivers; i++)
    if (strcmp (drivers[i].key, key) == 0)
      {
	if (verbose > 0)
	  printf ("\nDetected %s: %s (driver=%s)\n",
		  key, drivers[i].name, drivers[i].driver);
	install_driver (&drivers[i], key);
	return;
      }
}

static int
check_name (char *key, char *realname)
{
  int i;

  if (realname == NULL)
    realname = key;

  for (i = 0; i < ndrivers; i++)
    if (strcmp (drivers[i].key, key) == 0)
      {
	if (verbose > 0)
	  printf ("\nDetected %s: %s (driver=%s)\n",
		  realname, drivers[i].name, drivers[i].driver);
	/*      install_driver(&drivers[i], realname); */
	install_driver (&drivers[i], key);
	return 1;
      }
  return 0;
}

static int
check_node (di_node_t node, int level, char *realname)
{
  int j, ok = 0;

  while (node != DI_NODE_NIL)
    {
      char *name = di_node_name (node), *p;
      char *cnames;
      int n, i;

      cnames = "?";

      if (verbose > 1)
	{
	  for (j = 1; j < level; j++)
	    printf ("\t");
	  printf ("%s ", name);
	}

      /* if (realname==NULL) */
      {
	realname = name;
      }

      if (check_name (name, realname))
	{
	  if (verbose > 1)
	    {
	      char *drv = di_driver_name (node);
	      char *bnd = di_binding_name (node);
	      if (drv == NULL)
		drv = "";
	      if (bnd == NULL)
		bnd = "";
	      printf ("*match (driver=%s, binding=%s, instance=%d)*", drv,
		      bnd, di_instance (node));
	    }
	  ok = 1;
	}

      if (verbose > 1)
	printf ("\n");

      n = di_compatible_names (node, &cnames);
      p = cnames;
      for (i = 0; i < n; i++)
	if (!ok)
	  {
	    if (verbose > 1)
	      {
		for (j = 1; j < level; j++)
		  printf ("\t");
		printf (" = %s ", p);
	      }

	    if (check_name (p, realname))
	      {
		if (verbose > 1)
		  {
		    char *drv = di_driver_name (node);
		    char *bnd = di_binding_name (node);
		    if (drv == NULL)
		      drv = "";
		    if (bnd == NULL)
		      bnd = "";
		    printf ("*match (driver=%s, binding=%s, instance=%d)*",
			    drv, bnd, di_instance (node));
		  }
		ok = 1;
	      }

	    if (verbose > 1)
	      printf ("\n");
	    p = p + strlen (p) + 1;
	  }

      if (ok)
	{			/* Handle properties */
	  di_prop_t prop = DI_PROP_NIL;

	  while ((prop = di_prop_next (node, prop)) != DI_PROP_NIL)
	    {
	      printf ("\tProp '%s'}n", di_prop_name (prop));
	    }
	}

      /* if (!ok) */
      check_node (di_child_node (node), level + 1, realname);

      node = di_sibling_node (node);
    }

  return ok;
}

static void
load_devlist (const char *fname, int is_3rdparty)
{
  FILE *f;
  char line[256], *p;
  char *driver, *key, *name;

  if ((f = fopen (fname, "r")) == NULL)
    {
      perror (fname);
      exit (-1);
    }

  while (fgets (line, sizeof (line) - 1, f) != NULL)
    {
      p = line;
      while (*p)
	{
	  if (*p == '#' || *p == '\n')
	    *p = 0;
	  p++;
	}

      /* Drivers with upper case names are unsupported ones */
      if ((*line >= 'A' && *line <= 'Z') || (*line == '\0'))
	continue;

      driver = line;
      p = line;

      while (*p && *p != '\t' && *p != ' ')
	p++;
      if (*p)
	*p++ = 0;
      key = p;

/*
 * PCI subvendor ID's are marked as pcsNN,MM in devices.list. Convert them
 * to pciNN,MM for Solaris
 */
      if (strncmp (key, "pcs", 3) == 0)
	key[2] = 'i';

      while (*p && *p != '\t')
	p++;
      if (*p)
	*p++ = 0;
      name = p;

      if (verbose > 1)
	printf ("device=%s, name=%s, driver=%s\n", key, name, driver);

      if (ndrivers >= MAX_DRIVERS)
	{
	  printf ("Too many drivers defined in drivers.list\n");
	  exit (-1);
	}

      drivers[ndrivers].key = strdup (key);
      drivers[ndrivers].driver = strdup (driver);
      drivers[ndrivers].name = strdup (name);
      drivers[ndrivers].is_3rdparty = is_3rdparty;
      drivers[ndrivers].reload = 0;

      ndrivers++;
    }

  fclose (f);
}

static void
check_conf (char *modname, int is_virtual, char *options)
{
  char fname[256], drivername[256];
  struct stat st;
  FILE *f;

#ifdef sparc
  sprintf (fname, "/kernel/drv/%s.conf", modname);
  sprintf (drivername, "/kernel/drv/%s%s", arch, modname);
#else
  sprintf (fname, "/kernel/drv/%s.conf", modname);
  sprintf (drivername, "/kernel/drv/%s%s", arch, modname);
#endif

  /* fprintf(start_script, "/usr/sbin/modload %s\n", drivername); */

  if (!use_force)
    if (stat (fname, &st) != -1)	/* File already exists */
      return;
    else
      fprintf (stderr, "\n\nWarning! Config file %s was missing!\n\n", fname);

  if ((f = fopen (fname, "w")) == NULL)
    {
      perror (fname);
      exit (-1);
    }
  fprintf (f, "# Open Sound System configuration file\n");

  if (is_virtual)
    fprintf (f, "name=\"%s\" parent=\"pseudo\" instance=0%s;\n", modname,
	     options);
  else
    fprintf (f, "interrupt-priorities=9%s;\n", options);

  fclose (f);
}

static void
load_name_to_major (void)
{
  FILE *f;
  char line[256], *p;

  if ((f = fopen ("/etc/name_to_major", "r")) == NULL)
    return;

  while (fgets (line, sizeof (line), f) != NULL)
    {
      p = line;

      while (*p && *p != ' ')
	p++;
      *p = '\0';

      if (*line == '\0') continue;

      if (nmods < MAX_MODS)
	{
	  installed_modules[nmods++] = strdup (line);
	}

/*
 * Force reinstall of imux if it is currently installed in the system
 */
      if (strcmp (line, "oss_imux") == 0)
	install_imux = 1;
      if (strcmp (line, "oss_userdev") == 0)
	install_userdev = 1;
    }

  fclose (f);
}

static int
add_drv (char *name, char *drv, char *parms)
{
  char tmp[512];
  int ret;

  printf ("add_drv %s%s %s\n", safe_mode, parms, drv);
  sprintf (tmp, "add_drv %s%s %s", safe_mode, parms, drv);

  ret = system (tmp);

  if (ret != 0)
    fprintf (stderr, "%s\n", tmp);

  /* save the driver and name info in installed_drivers file except osscore */
  if (name != NULL)
    fprintf (drivers_f, "%s #%s\n", drv, name);

  return ret;
}

static void
load_license (char *fname)
{
  struct stat st;
  char cmd[256];

  if (stat (fname, &st) == -1)
    return;			/* Doesn't exist */

  if (stat ("/usr/sbin/osslic", &st) == -1)
    return;			/* No osslic utility in the system. No need to install license. */

  sprintf (cmd, "/usr/sbin/osslic -q %s", fname);
  system (cmd);
}

static void
forceload_drivers (char *fname)
{
  FILE *f;
  char line[256], *p;

  if ((f = fopen (fname, "r")) == NULL)
    return;

  while (fgets (line, sizeof (line), f) != NULL)
    {
      p = strchr (line, '\n');
      if (p) *p = '\0';
      find_and_install_driver (line);
    }
}

int
main (int argc, char *argv[])
{
  int i, pass, c;
  di_node_t root_node;
  char tmp[256];
  struct stat st;
  FILE *ptr;
  int pid;
  char *cmd = "/usr/sbin/modinfo | grep osscommon";

#if 0
  if (sysinfo (SI_ARCHITECTURE_K, tmp, sizeof (tmp)) == -1)
    {
      perror ("sysinfo SI_ARCHITECTURE_K");
      exit (-1);
    }
#else
  if (sysinfo (SI_ARCHITECTURE, tmp, sizeof (tmp)) == -1)
    {
      perror ("sysinfo SI_ARCHITECTURE");
      exit (-1);
    }
#endif

  load_name_to_major ();

#ifdef sparc
  if (strcmp (tmp, "sparc") == 0)
    sprintf (arch, "sparcv9/", tmp);
#else
  if (strcmp (tmp, "amd64") == 0)
    sprintf (arch, "%s/", tmp);
#endif

  while ((c = getopt (argc, argv, "vfiudlVS")) != EOF)
      switch (c)
	{
	case 'v':
	  verbose++;
	  break;
	case 'f':
	  use_force = 1;
	  break;
	case 'S': /* Safe mode */
	  safe_mode="-n ";
	  break;
	case 'i':
	  install_imux = 1;
	  break;
	case 'u':
	  install_userdev = 1;
	  break;
	case 'd': /* Obolete under Solaris. */
	  exit (0);
	  break;
	case 'l':
	  load_license ("/etc/oss/license.asc");
	  exit (0);
	  break;
	}

  load_license ("/etc/oss/license.asc");

  load_devlist ("/etc/oss/devices.list", 0);

  if ((drivers_f = fopen ("/etc/oss/installed_drivers", "w")) == NULL)
    {
      perror ("/etc/oss/installed_drivers");
      exit (-1);
    }

  if (stat ("/etc/oss_3rdparty", &st) != -1)
    load_devlist ("/etc/oss_3rdparty", 1);

  if ((root_node = di_init ("/", DINFOSUBTREE)) == DI_NODE_NIL)
    {
      printf ("di_init() failed\n");
    }

  check_node (root_node, 0, NULL);

  di_fini (root_node);

  /*
   * Force unconditional loading of the USB driver and few others to make
   * hotplugging possible.
   */
  forceload_drivers ("/etc/oss/forceload.conf");

  if (verbose > 3)
    {
      for (i = 0; i < nmodules; i++)
	{
	  printf ("Would add %s -i '%s'\n", modules[i].driver,
		  modules[i].idlist);
	}
      printf ("Skipping actual device installation\n");
      exit (0);
    }

  sync ();

/*
 * Unload drivers that appear to be loaded
 */
  for (i = 0; i < nmods; i++)
    {
      int j, ok = 0;

      for (j = 0; j < ndrivers && !ok; j++)
	{
	  if (strcmp (installed_modules[i], drivers[j].driver) == 0)
	    {
	      ok = 1;
	      if (strcmp (drivers[j].key, "$PSEUDO") == 0 ||
		  drivers[j].is_3rdparty)
		{
		  if (drivers[j].is_3rdparty)
		    printf ("Removed 3rd party driver %s\n",
			    drivers[j].driver);
		  drivers[j].reload = 1;
		}
	    }
	}

      if (!ok)
	continue;

      sprintf (tmp, "rem_drv %s\n", installed_modules[i]);
      printf ("rem_drv %s\n", installed_modules[i]);
      system (tmp);
      sync ();
    }

#if 0
/*
 * Tell the osscore module to go away by writing something to /dev/sndstat
 * (at this moment it doesn't matter what is written).
 */

  system ("rem_drv osscore");
  sync ();
#endif

  /* Find the osscommon module's PID and pass it to moduload to unload it */
  if ((ptr = popen (cmd, "r")) != NULL)
    {
      if (fscanf (ptr, "%d", &pid) == 1 && pid > 0)
	{
	  sprintf (tmp, "modunload -i %d", pid);
	  fprintf (stderr, "unloaded osscommon\n");
	  system (tmp);
	}
      pclose (ptr);
    }


/* Now start loading all the modules */
  check_conf ("osscore", 1, "");

  if (add_drv ("OSS Core Devices", "osscore", "-m '* 0666 root sys'") != 0)
    {
      fprintf (stderr, "Installing OSS (osscore) failed\n");
      exit (256);
    }

  for (pass = 0; pass < 2; pass++)
    for (i = 0; i < nmodules; i++)
      if (modules[i].pass == pass)
	{
	  check_conf (modules[i].driver, 0, "");
	  sprintf (tmp, "-m '* 0666 root sys' -i '%s'", modules[i].idlist);
	  if (add_drv (modules[i].name, modules[i].driver, tmp) != 0)
	    {
	      fprintf (stderr, "Installing OSS (%s) failed\n",
		       modules[i].driver);
	      exit (256);
	    }
	}

  if (install_imux)
    {
      check_conf ("oss_imux", 1, "");
      add_drv ("OSS Input Multiplexer", "oss_imux", "-m '* 0666 root sys'");
    }

  if (install_userdev)
    {
      check_conf ("oss_userdev", 1, "");
      add_drv ("OSS User space device driver", "oss_userdev", "-m '* 0666 root sys'");
    }

#ifdef sparc
  if (stat("/kernel/drv/sparcv9/oss_sadasupport", &st) != -1)
    if (stat("/kernel/misc/sparcv9/audiosup", &st) != -1)
#else
  if (stat("/kernel/drv/oss_sadasupport", &st) != -1)
    if (stat("/kernel/misc/audiosup", &st) != -1)
#endif
     {
  	check_conf ("oss_sadasupport", 1, "");
  	add_drv ("SADA emulation layer", "oss_sadasupport", "-m '* 0666 root sys'");
     }

  for (i = 0; i < ndrivers; i++)
    if (drivers[i].reload)
      if (!drivers[i].is_3rdparty)
	{
	  check_conf (drivers[i].driver, 1, "");
	  add_drv (drivers[i].name, drivers[i].driver,
		   "-m '* 0666 root sys'");
	}
      else
	{
	  char parms[1024];

	  if (strcmp (drivers[i].key, "$PSEUDO") == 0)
	    {
	      add_drv (drivers[i].name, drivers[i].driver,
		       "-m '* 0666 root sys'");
	      continue;
	    }

	  sprintf (parms, "-i '%s' -m '* 0666 root sys'", drivers[i].key);
	  printf ("Attempting to reload %s\n", drivers[i].driver);
	  add_drv (drivers[i].name, drivers[i].driver, parms);
	}

/*
 * Reload the previous default settings if they were ever saved.
 */
  if (stat ("/etc/oss/mixer.save", &st) != -1 ||
      stat ("/etc/oss/dspdevs.map", &st) != -1)
    system ("savemixer -L");

  fclose (drivers_f);
#if 0
  sync ();
  printf ("Restarting OSS - Please wait\r");
  fflush (stdout);
  system ("soundoff");
  system ("soundon");
  printf ("\n");
#endif
  return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.