Source

oss4-netbsd / kernel / framework / sndstat / oss_sndstat.c

Full commit
  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
/*
 * Purpose: /dev/sndstat driver
 *
 * Description:
 */
#define COPYING Copyright (C) Hannu Savolainen and Dev Mazumdar 2005. All rights reserved.

#include <oss_config.h>
#include <midi_core.h>

static char *sndstat_buf = NULL;
static int sndstat_len, sndstat_ptr;
static volatile int sndstat_busy = 0;
int riptide_notice = 0;		/* The Riptide driver will set this to 1 */

/*
 * All kind of status messages
 */
#define MAX_MESSAGE 50
static char *messages[MAX_MESSAGE];
static int nmessages;		/* # of display messages (/dev/sndstat) */

static int
put_status (const char *s)
{
  int l = strlen (s);

  if (sndstat_len + l >= 4000)
    return 0;

  memcpy (&sndstat_buf[sndstat_len], s, l);
  sndstat_len += l;

  return 1;
}

static int
put_status_int (unsigned int val, int radix)
{
  char buf[11], *rx = "%d";

  if (!val)
    return put_status ("0");

  if (radix == 16)
    rx = "%x";
  sprintf (buf, rx, val);

  return put_status (buf);
}

static void
init_status (void)
{
  /*
   * Write the status information to the sndstat_buf and update sndstat_len.
   * There is a limit of 4000 bytes for the data.
   */

  int i, p, missing_devs = 0;
  int notify = 0;
  extern char *oss_license_string;

  sndstat_ptr = 0;

  put_status ("OSS " OSS_VERSION_STRING);
  put_status (oss_license_string);
  put_status (" (C) 4Front Technologies 1996-2011\n");

  if (riptide_notice)
    put_status ("RipTide Driver (C) 2000, Conexant Systems, Inc.\n");

#ifdef LICENSED_VERSION
  oss_print_license (put_status, put_status_int);
#endif

#ifdef OSS_CONFIG_OPTIONS
  put_status ("\nSource configration options: ");
  put_status (OSS_CONFIG_OPTIONS);
  put_status ("\n");
#endif

#ifdef OSS_HG_INFO
  put_status ("\nHg revision: ");
  put_status (OSS_HG_INFO);
  put_status ("\n");
#endif

  if (nmessages > 0)
    {
      put_status ("\n");

      for (i = 0; i < nmessages; i++)
	{
	  if (!put_status (messages[i]))
	    return;
	}

      put_status ("\n");
    }

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  {
#if defined(__FreeBSD__)
    extern char version[];
#endif

    put_status ("Kernel: ");
    put_status (version);
    put_status ("\n");
  }
#endif

#if 0
/*
 * This code is obsolete and not functional at this moment.
 */
  if (!put_status ("\nDevice objects:\n"))
    return;

  for (i = 0; i < oss_num_cards; i++)
    {
      char tmp[256];

      oss_get_cardinfo (i, tmp, sizeof (tmp) - 1);
      if (!put_status (tmp))
	return;
      if (!put_status ("\n"))
	return;
    }

#endif

  if (!put_status ("\nAudio devices:\n"))
    return;

  missing_devs = 0;

  if (audio_devfiles != NULL)
    for (i = 0; i < MAX_AUDIO_DEVFILES +100 && missing_devs < 40; i++)
      {
	int j, d;
#if 0
	if (i < num_audio_devfiles)
	if (audio_devfiles[i]->card_number != cardno)
	  {
	    put_status (" \n");
	    cardno = audio_devfiles[i]->card_number;
	  }
#endif
/*
 * New device numbering scheme may have /dev/dsp# devices in different
 * order than the order of devices in audio_devices[]. Find the right device
 * based on the adev->real_dev number. Note that device numbering may have
 * holes if devices have been removed after running ossdevlinks -f last time.
 */
	d = -1;
	for (j = 0; j < num_audio_devfiles; j++)
	  {
	    if (audio_devfiles[j]->real_dev == i)
	      {
		if (j != i)
		  notify = 1;
		d = audio_devfiles[j]->audio_devfile;
		break;
	      }
	  }

	if (d == -1)
	  {
	    missing_devs++;
	    continue;
	  }

	if (missing_devs > 0)	/* There is a hole in numbering */
	  for (j = i - missing_devs; j < i; j++)
	    {
	      notify = 1;
	      if (!put_status_int (j, 10))
		return;
	      if (!put_status (": (Undefined or removed device)\n"))
		return;
	    }
	missing_devs = 0;

	if (!put_status_int (i, 10))
	  return;
	if (!put_status (": "))
	  return;
	if (!audio_devfiles[d]->enabled || audio_devfiles[d]->unloaded)
	  put_status ("(");

	if (!put_status (audio_devfiles[d]->name))
	  return;
	if (!audio_devfiles[d]->enabled || audio_devfiles[d]->unloaded)
	  put_status (")");

	if ((audio_devfiles[d]->flags & ADEV_DUPLEX)
	    || (audio_devfiles[d]->flags & ADEV_NOINPUT)
	    || (audio_devfiles[d]->flags & ADEV_NOOUTPUT))
	  {
	    int nn = 0;

	    if (!put_status (" ("))
	      return;

	    if (audio_devfiles[d]->flags & ADEV_NOINPUT)
	      {
		if (nn++)
		  put_status (",");
		if (!put_status ("OUTPUT"))
		  return;
	      }

	    if (audio_devfiles[d]->flags & ADEV_NOOUTPUT)
	      {
		if (nn++)
		  put_status (",");
		if (!put_status ("INPUT"))
		  return;
	      }

	    if (audio_devfiles[d]->flags & ADEV_DUPLEX)
	      {
		if (nn++)
		  put_status (",");
		if (!put_status ("DUPLEX"))
		  return;
	      }
	    if (!put_status (")"))
	      return;
	  }

	if (!put_status ("\n"))
	  return;

	{
	  adev_t *adev = audio_devfiles[d];
	  int n = 0, single = 0;
	  if (adev->next_out == NULL)
	    single = 1;

	  while (adev != NULL)
	    {
	      if (adev->open_mode != 0)
		{
		  if (i < 10)
		    {
		      if (!put_status ("   "))
			return;
		    }
		  else
		    {
		      if (!put_status ("    "))
			return;
		    }

		  if (single)
		    {
		      put_status ("Opened ");
		      if (adev->open_mode & OPEN_READ)
			put_status ("IN");
		      if (adev->open_mode & OPEN_WRITE)
			put_status ("OUT");
		      put_status (" by ");
		    }
		  else
		    {
		      put_status ("Engine ");
		      put_status_int (n + 1, 10);
		      put_status (" opened ");
		      if (adev->open_mode & OPEN_READ)
			put_status ("IN");
		      if (adev->open_mode & OPEN_WRITE)
			put_status ("OUT");
		      put_status (" by ");
		    }

		  if (adev->pid != -1 || *adev->label != 0)
		    {
		      if (*adev->label != 0)
			{
			  if (!put_status (adev->label))
			    return;
			  if (!put_status ("/"))
			    return;
			}

		      put_status_int (adev->pid, 10);
		    }
		  else
		    {
		      if (!put_status ("unknown application"))
			return;
		    }

		  if (!put_status (" @ "))
		    return;
		  if (!put_status_int (adev->user_parms.rate, 10))
		    return;
		  if (!put_status ("/"))
		    return;
		  if (!put_status_int (adev->hw_parms.rate, 10))
		    return;
		  if (!put_status (" Hz"))
		    return;

#if 1
		  if (!put_status (" Fragment: "))
		    return;

		  if (!put_status (audio_show_latency (adev->engine_num)))
		    return;
#endif

		  if (!put_status ("\n"))
		    return;

		  if (*adev->song_name != 0)
		    {
		      if (i < 10)
			{
			  if (!put_status ("   "))
			    return;
			}
		      else
			{
			  if (!put_status ("    "))
			    return;
			}

		      if (!put_status ("Song name: "))
			return;
		      if (!put_status (adev->song_name))
			return;
		      if (!put_status ("\n"))
			return;
		    }
		}

	      adev = adev->next_out;
	      n++;
	    }
	}
      }

#ifdef CONFIG_OSS_MIDI
  if (!put_status ("\nMIDI devices:\n"))
    return;

  missing_devs = 0;
  for (i = 0; i < MAX_MIDI_DEV * 2 && missing_devs < 16; i++)
    {
      int j, d = -1;

      for (j = 0; j < num_mididevs; j++)
	if (midi_devs[j]->real_dev == i)
	  {
	    d = j;
	    if (j != i)
	      notify = 1;
	  }

      if (d == -1)
	{
	  missing_devs++;
	  continue;
	}

      for (j = i - missing_devs; j < i; j++)
	{
	  notify = 1;
	  if (!put_status_int (j, 10))
	    return;
	  if (!put_status (": (Unknown or removed device)\n"))
	    return;
	}
      missing_devs = 0;

      if (!put_status_int (i, 10))
	return;
      if (!put_status (": "))
	return;
      if (!midi_devs[d]->enabled || midi_devs[d]->unloaded)
	put_status ("(");
      if (!put_status (midi_devs[d]->name))
	return;
      if (!midi_devs[d]->enabled || midi_devs[d]->unloaded)
	put_status (")");
      if (!put_status ("\n"))
	return;

      if (midi_devs[d]->pid != -1)
	{
	  if (i < 10)
	    {
	      if (!put_status ("   "))
		return;
	    }
	  else
	    {
	      if (!put_status ("    "))
		return;
	    }
	  if (!put_status ("Open by "))
	    return;
	  if (!put_status_int (midi_devs[d]->pid, 10))
	    return;
	  if (*midi_devs[d]->cmd != 0)
	    {
	      if (!put_status ("/"))
		return;
	      if (!put_status (midi_devs[d]->cmd))
		return;
	    }
	  if (!put_status ("\n"))
	    return;
	  if (midi_devs[d]->d->ioctl)
	    {
	      oss_longname_t song_name;

	      if (midi_devs[d]->d->ioctl (d, SNDCTL_GETSONG,
					  (ioctl_arg) song_name) >= 0
		  && *song_name != 0)
		{
		  if (!put_status ("   Song name: "))
		    return;
		  if (!put_status (song_name))
		    return;
		  if (!put_status ("\n"))
		    return;
		}
	    }
	}
    }
#endif

#if 0
  /* TODO: No timer available at this moment */
  if (!put_status ("\nTimers:\n"))
    return;

  for (i = 0; i < oss_num_timers; i++)
    {
      if (!put_status_int (i, 10))
	return;
      if (!put_status (": "))
	return;
      if (!put_status (oss_timer_devs[i]->info.name))
	return;
      if (!put_status ("\n"))
	return;
    }
#endif

  if (!put_status ("\nMixers:\n"))
    return;

  missing_devs = 0;

  for (i = 0; i < MAX_MIXER_DEV * 2 && missing_devs < 10; i++)
    {
      int j, d = -1;

      for (j = 0; j < num_mixers; j++)
	if (mixer_devs[j]->real_dev == i)
	  {
	    if (j != i)
	      notify = 1;
	    d = j;
	  }

      if (d == -1)
	{
	  missing_devs++;
	  continue;
	}

      for (j = i - missing_devs; j < i; j++)
	{
	  notify = 1;
	  if (!put_status_int (j, 10))
	    return;
	  if (!put_status (": (Uninstalled or removed device\n"))
	    return;
	}
      missing_devs = 0;

      if (!put_status_int (i, 10))
	return;
      if (!put_status (": "))
	return;

      if (!mixer_devs[d]->enabled || mixer_devs[d]->unloaded)
	if (!put_status ("("))
	  return;

      if (!put_status (mixer_devs[d]->name))
	return;

      if (!mixer_devs[d]->enabled || mixer_devs[d]->unloaded)
	if (!put_status (")"))
	  return;

      if (!put_status ("\n"))
	return;
    }

#if 1
  p = 0;
  for (i = 0; i < OSS_HISTORY_SIZE; i++)
    if (*oss_history[i] != 0)
      p++;

  if (p > 0)
#ifdef GET_PROCESS_UID
    if (GET_PROCESS_UID () == 0)
#endif
      {
	if (!put_status ("\nHistory:\n"))
	  return;

	p = oss_history_p;

	for (i = 0; i < OSS_HISTORY_SIZE; i++)
	  {
	    int ix = (p + i) % OSS_HISTORY_SIZE;

	    if (*oss_history[ix] == 0)
	      continue;

	    if (!put_status (oss_history[ix]))
	      return;
	    if (!put_status ("\n"))
	      return;
	  }
      }
#endif
#if 0
  if (!put_status
      ("\n\nNOTICE! This /dev/sndstat file is obsolete - use the ossinfo command instead\n"))
    return;
#endif
  if (notify)
    {
      if (!put_status
	  ("\n\nWARNING! Legacy device numbering in /dev/sndstat is different from actual device numbering\n"))
	return;
    }

  put_status ("\n\nNOTICE! Device numbers shown above may be wrong.\n");
  put_status ("        Use the ossinfo command to find out the correct device names.\n");

  sndstat_buf[sndstat_len] = 0;
}

void
store_msg (char *msg)
{
  char *s;

  if (strlen (msg) > 100 || nmessages >= MAX_MESSAGE)
    return;

  s = PMALLOC (NULL, strlen (msg) + 1);
  if (s == NULL)
    {
      return;
    }
  strcpy (s, msg);
  messages[nmessages++] = s;
}

static int
read_status (uio_t * buf, int count)
{
  /*
   * Return at most 'count' bytes from the sndstat_buf.
   */
  int l, c;

  l = count;
  c = sndstat_len - sndstat_ptr;

  if (l > c)
    l = c;
  if (l <= 0)
    return 0;

  if (uiomove (&sndstat_buf[sndstat_ptr], l, UIO_READ, buf) != 0)
    return OSS_EFAULT;
  sndstat_ptr += l;

  return l;
}

/*ARGSUSED*/
static int
sndstat_open (int dev, int dev_class, struct fileinfo *file,
	      int recursive, int open_flags, int *redirect)
{
  /* TODO: Concurrency control */
  if (sndstat_busy)
    return OSS_EBUSY;
  sndstat_busy = 1;

  if ((sndstat_buf = KERNEL_MALLOC (4096)) == NULL)
    {
      sndstat_busy = 0;
      return OSS_ENOMEM;
    }

  sndstat_len = 0;
  init_status ();
  sndstat_ptr = 0;

  return 0;
}

/*ARGSUSED*/
static void
sndstat_close (int dev, struct fileinfo *file)
{
  KERNEL_FREE (sndstat_buf);
  sndstat_buf = NULL;
  sndstat_busy = 0;
}

/*ARGSUSED*/
static int
sndstat_read (int dev, struct fileinfo *file, uio_t * buf, int count)
{
  int l;
  l = read_status (buf, count);
  return l;
}

/*ARGSUSED*/
static int
sndstat_write (int dev, struct fileinfo *file, uio_t * buf, int count)
{
/*
 * This dummy write routine will be used for some internal management purposes
 * in the future. At this moment it just tells the osscore module that it 
 * should permit detaching itself.
 */
#ifdef sun
  extern int oss_detach_enabled;
  oss_detach_enabled = 1;
  return count;
#else
  return OSS_EIO;
#endif
}

/*ARGSUSED*/
static int
sndstat_ioctl (int dev, struct fileinfo *bogus,
	       unsigned int cmd, ioctl_arg arg)
{
  if (cmd == OSS_GETVERSION)
    return *arg = OSS_VERSION;

  return OSS_EINVAL;
}

static oss_cdev_drv_t sndstat_cdev_drv = {
  sndstat_open,
  sndstat_close,
  sndstat_read,
  sndstat_write,
  sndstat_ioctl
};

void
install_sndstat (oss_device_t * osdev)
{
  //static int already_installed=0;

  // if (!already_installed++) // TODO: Is it necessaary to prevent loading sndstat multiple times?
  oss_install_chrdev (osdev, "sndstat", OSS_DEV_STATUS, 0, &sndstat_cdev_drv,
		      0);
}