summaryrefslogtreecommitdiff
path: root/kernel/audio.c
blob: 0b1c4af2d79c40d2f11080b04dc81096b7ce6752 (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
// TODO: This should support multiple audio sources.
#include <assert.h>
#include <audio.h>
#include <ctype.h>
#include <drivers/ac97.h>
#include <errno.h>
#include <fs/devfs.h>
#include <lib/sv.h>
#include <math.h>

int audio_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
  (void)offset;
  (void)fd;
  int rc = ac97_add_pcm(buffer, len);
  if (0 == rc) {
    return -EWOULDBLOCK;
  }
  return rc;
}

int audio_can_write(vfs_inode_t *inode) {
  (void)inode;
  return ac97_can_write();
}

int volume_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
  (void)offset;
  (void)fd;
  struct sv string_view = sv_init(buffer, len);
  struct sv rest;
  int got_num;
  u64 volume = sv_parse_unsigned_number(string_view, &rest, &got_num);
  int i = sv_length(string_view) - sv_length(rest);
  if (!got_num) {
    return 0;
  }

  if (volume > 100) {
    volume = 0;
  }

  ac97_set_volume(volume);
  return i;
}

int volume_read(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
  if (offset > 0) {
    return 0;
  }
  (void)fd;
  int volume = ac97_get_volume();
  return min(len, (u64)kbnprintf(buffer, len, "%d", volume));
}

static int add_files(void) {
  devfs_add_file("/audio", NULL, audio_write, NULL, NULL, audio_can_write,
                 FS_TYPE_CHAR_DEVICE);
  devfs_add_file("/volume", volume_read, volume_write, NULL, always_has_data,
                 always_can_write, FS_TYPE_BLOCK_DEVICE);
  return 1;
}

void audio_init(void) {
  ac97_init();
  add_files();
}