summaryrefslogtreecommitdiff
path: root/acpi/acpi-ops.c
blob: d9795e87f5491f310f27cea1a7ada3321b353d09 (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
/*
   Copyright (C) 2021 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Implementation of ACPI operations */

#include <acpi_S.h>

#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/io.h>

#include "libacpica/acpi_init.h"
#include "acpifs.h"

static error_t
check_permissions (struct protid *master, int flags)
{
  struct node *node;
  struct acpifs_dirent *e;

  node = master->po->np;
  e = node->nn->ln;

  /* Check whether the user has permissions to access this node */
  return entry_check_perms (master->user, e, flags);
}

static void
acpi_enable(struct acpi_table *facp)
{
  int i;
  uint8_t *ptr = (uint8_t *)&facp->data;
  
  /* Grab value to write to SMI_CMD to enable ACPI */
  uint8_t acpi_en = ptr[SMI_EN_OFFSET];
  uint16_t smi_cmd = (uint16_t)ptr[SMI_CMD_OFFSET] | ((uint16_t)ptr[SMI_CMD_OFFSET+1] << 8);
  uint16_t pm1a_ctl = (uint16_t)ptr[PM1A_CTL_OFFSET] | ((uint16_t)ptr[PM1A_CTL_OFFSET+1] << 8);

  /* Get I/O permissions */
  if (ioperm(smi_cmd, 2, 1))
    {
      fprintf(stderr, "EPERM on ioperm(smi_cmd)\n");
      return;
    }

  if (ioperm(pm1a_ctl, 2, 1))
    {
      fprintf(stderr, "EPERM on ioperm(pm1a_ctl)\n");
      return;
    }

  /* Enable ACPI */
  outb(acpi_en, smi_cmd);
  for (i = 0; i < 300; i++)
    {
      if ((inw(pm1a_ctl) & SCI_EN) == SCI_EN)
        break;
    }

  /* Drop I/O permissions */
  ioperm(smi_cmd, 2, 0);
  ioperm(pm1a_ctl, 2, 0);
}

error_t
S_acpi_sleep (struct protid *master, int sleep_state)
{
  error_t err;
  struct acpifs_dirent *e;

  if (!master)
    return EOPNOTSUPP;

  e = master->po->np->nn->ln;
  if (strncmp (e->name, "FACP", 4))
    /* This operation may only be addressed to the FADT table */
    return EINVAL;

  err = check_permissions (master, O_READ);
  if (err)
    return err;

  /* Enable ACPI */
  acpi_enable(e->acpitable);

  /* Perform sleep */
  acpi_enter_sleep(sleep_state);

  /* Never reached */
  return err;
}

error_t
S_acpi_enable (struct protid *master)
{
  error_t err;
  struct acpifs_dirent *e;

  if (!master)
    return EOPNOTSUPP;

  e = master->po->np->nn->ln;
  if (strncmp (e->name, "FACP", 4))
    /* This operation may only be addressed to the FADT table */
    return EINVAL;

  err = check_permissions (master, O_READ);
  if (err)
    return err;

  acpi_enable(e->acpitable);
  return err;
}

error_t
S_acpi_get_irq (struct protid *master, int bus, int dev, int func, int *irq)
{
  error_t err;

  if (!master)
    return EOPNOTSUPP;

  err = check_permissions (master, O_READ);
  if (err)
    return err;

  *irq = acpi_get_irq_number(bus, dev, func);
  return err;
}