summaryrefslogtreecommitdiffstats
path: root/lib/sylixos-device.c
blob: 07195d562a6f0b092ceb7c7206fae91c92254095 (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
/*
 *  The PCI Library -- Direct Configuration access via SylixOS Ports
 *
 *  Copyright (c) 2018 YuJian.Gong <gongyujian@acoinfo.com>
 *
 *  Can be freely distributed and used under the terms of the GNU GPL.
 */

#define _GNU_SOURCE
#define  __SYLIXOS_KERNEL
#define  __SYLIXOS_PCI_DRV
#include <SylixOS.h>
#include <stdlib.h>
#include <string.h>

#include "internal.h"

static void
sylixos_scan(struct pci_access *a)
{
  u8 busmap[256];
  int bus;

  memset(busmap, 0, sizeof(busmap));

  for (bus = 0; bus < PCI_MAX_BUS; bus++)
    if (!busmap[bus])
      pci_generic_scan_bus(a, busmap, 0, bus);
}

static void
sylixos_config(struct pci_access *a)
{
  pci_define_param(a, "sylixos.path", PCI_PATH_SYLIXOS_DEVICE, "Path to the SylixOS PCI device");
}

static int
sylixos_detect(struct pci_access *a)
{
  char *name = pci_get_param(a, "sylixos.path");

  if (access(name, R_OK))
    {
      a->warning("Cannot open %s", name);
      return 0;
    }

  a->debug("...using %s", name);
  return 1;
}

static void
sylixos_init(struct pci_access *a UNUSED)
{
}

static void
sylixos_cleanup(struct pci_access *a UNUSED)
{
}

static int
sylixos_read(struct pci_dev *d, int pos, byte *buf, int len)
{
  int ret = -1;
  u8  data_byte = -1;
  u16 data_word = -1;
  u32 data_dword = -1;

  if (!(len == 1 || len == 2 || len == 4))
    return pci_generic_block_read(d, pos, buf, len);

  if (pos >= 256)
    return 0;

  switch (len)
    {
    case 1:
      ret = pciConfigInByte(d->bus, d->dev, d->func, pos, &data_byte);
      if (ret != ERROR_NONE)
	return 0;
      buf[0] = (u8)data_byte;
      break;

    case 2:
      ret = pciConfigInWord(d->bus, d->dev, d->func, pos, &data_word);
      if (ret != ERROR_NONE)
	return 0;
      ((u16 *) buf)[0] = cpu_to_le16(data_word);
      break;

    case 4:
      ret = pciConfigInDword(d->bus, d->dev, d->func, pos, &data_dword);
      if (ret != ERROR_NONE)
	return 0;
      ((u32 *) buf)[0] = cpu_to_le32(data_dword);
      break;
    }

  return 1;
}

static int
sylixos_write(struct pci_dev *d, int pos, byte *buf, int len)
{
  int ret = PX_ERROR;
  u8 data_byte;
  u16 data_word;
  u32 data_dword;

  if (!(len == 1 || len == 2 || len == 4))
    return pci_generic_block_write(d, pos, buf, len);

  if (pos >= 256)
    return 0;

  switch (len)
    {
    case 1:
      data_byte = buf[0];
      ret = pciConfigOutByte(d->bus, d->dev, d->func, pos, data_byte);
      if (ret != ERROR_NONE)
	return 0;
      break;

    case 2:
      data_word = le16_to_cpu(((u16 *) buf)[0]);
      ret = pciConfigOutWord(d->bus, d->dev, d->func, pos, data_word);
      if (ret != ERROR_NONE)
	return 0;
      break;

    case 4:
      data_dword = le32_to_cpu(((u32 *) buf)[0]);
      ret = pciConfigOutDword(d->bus, d->dev, d->func, pos, data_dword);
      if (ret != ERROR_NONE)
	return 0;
      break;
    }

  return 1;
}

struct pci_methods pm_sylixos_device = {
  "sylixos-device",
  "SylixOS /proc/pci device",
  sylixos_config,
  sylixos_detect,
  sylixos_init,
  sylixos_cleanup,
  sylixos_scan,
  pci_generic_fill_info,
  sylixos_read,
  sylixos_write,
  NULL,			// no read_vpd
  NULL,			// no init_dev
  NULL,			// no cleanup_dev
};