]> mj.ucw.cz Git - pciutils.git/blob - lib/sylixos-device.c
update-pciids: Re-compress pci.ids if needed
[pciutils.git] / lib / sylixos-device.c
1 /*
2  *      The PCI Library -- Direct Configuration access via SylixOS Ports
3  *
4  *      Copyright (c) 2018 YuJian.Gong <gongyujian@acoinfo.com>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL v2+.
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #define _GNU_SOURCE
12 #define  __SYLIXOS_KERNEL
13 #define  __SYLIXOS_PCI_DRV
14 #include <SylixOS.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "internal.h"
19
20 static void
21 sylixos_scan(struct pci_access *a)
22 {
23   u8 busmap[256];
24   int bus;
25
26   memset(busmap, 0, sizeof(busmap));
27
28   for (bus = 0; bus < PCI_MAX_BUS; bus++)
29     if (!busmap[bus])
30       pci_generic_scan_bus(a, busmap, 0, bus);
31 }
32
33 static void
34 sylixos_config(struct pci_access *a)
35 {
36   pci_define_param(a, "sylixos.path", PCI_PATH_SYLIXOS_DEVICE, "Path to the SylixOS PCI device");
37 }
38
39 static int
40 sylixos_detect(struct pci_access *a)
41 {
42   char *name = pci_get_param(a, "sylixos.path");
43
44   if (access(name, R_OK))
45     {
46       a->warning("Cannot open %s", name);
47       return 0;
48     }
49
50   a->debug("...using %s", name);
51   return 1;
52 }
53
54 static void
55 sylixos_init(struct pci_access *a UNUSED)
56 {
57 }
58
59 static void
60 sylixos_cleanup(struct pci_access *a UNUSED)
61 {
62 }
63
64 static int
65 sylixos_read(struct pci_dev *d, int pos, byte *buf, int len)
66 {
67   int ret = -1;
68   u8  data_byte = -1;
69   u16 data_word = -1;
70   u32 data_dword = -1;
71
72   if (!(len == 1 || len == 2 || len == 4))
73     return pci_generic_block_read(d, pos, buf, len);
74
75   if (pos >= 256)
76     return 0;
77
78   switch (len)
79     {
80     case 1:
81       ret = pciConfigInByte(d->bus, d->dev, d->func, pos, &data_byte);
82       if (ret != ERROR_NONE)
83         return 0;
84       buf[0] = (u8)data_byte;
85       break;
86
87     case 2:
88       ret = pciConfigInWord(d->bus, d->dev, d->func, pos, &data_word);
89       if (ret != ERROR_NONE)
90         return 0;
91       ((u16 *) buf)[0] = cpu_to_le16(data_word);
92       break;
93
94     case 4:
95       ret = pciConfigInDword(d->bus, d->dev, d->func, pos, &data_dword);
96       if (ret != ERROR_NONE)
97         return 0;
98       ((u32 *) buf)[0] = cpu_to_le32(data_dword);
99       break;
100     }
101
102   return 1;
103 }
104
105 static int
106 sylixos_write(struct pci_dev *d, int pos, byte *buf, int len)
107 {
108   int ret = PX_ERROR;
109   u8 data_byte;
110   u16 data_word;
111   u32 data_dword;
112
113   if (!(len == 1 || len == 2 || len == 4))
114     return pci_generic_block_write(d, pos, buf, len);
115
116   if (pos >= 256)
117     return 0;
118
119   switch (len)
120     {
121     case 1:
122       data_byte = buf[0];
123       ret = pciConfigOutByte(d->bus, d->dev, d->func, pos, data_byte);
124       if (ret != ERROR_NONE)
125         return 0;
126       break;
127
128     case 2:
129       data_word = le16_to_cpu(((u16 *) buf)[0]);
130       ret = pciConfigOutWord(d->bus, d->dev, d->func, pos, data_word);
131       if (ret != ERROR_NONE)
132         return 0;
133       break;
134
135     case 4:
136       data_dword = le32_to_cpu(((u32 *) buf)[0]);
137       ret = pciConfigOutDword(d->bus, d->dev, d->func, pos, data_dword);
138       if (ret != ERROR_NONE)
139         return 0;
140       break;
141     }
142
143   return 1;
144 }
145
146 struct pci_methods pm_sylixos_device = {
147   "sylixos-device",
148   "SylixOS /proc/pci device",
149   sylixos_config,
150   sylixos_detect,
151   sylixos_init,
152   sylixos_cleanup,
153   sylixos_scan,
154   pci_generic_fill_info,
155   sylixos_read,
156   sylixos_write,
157   NULL,                 // no read_vpd
158   NULL,                 // no init_dev
159   NULL,                 // no cleanup_dev
160 };