Initialize Tizen 2.3
[framework/system/pciutils.git] / lib / proc.c
1 /*
2  *      The PCI Library -- Configuration Access via /proc/bus/pci
3  *
4  *      Copyright (c) 1997--2003 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #define _GNU_SOURCE
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <sys/types.h>
17
18 #include "internal.h"
19 #include "pread.h"
20
21 static void
22 proc_config(struct pci_access *a)
23 {
24   pci_define_param(a, "proc.path", PCI_PATH_PROC_BUS_PCI, "Path to the procfs bus tree");
25 }
26
27 static int
28 proc_detect(struct pci_access *a)
29 {
30   char *name = pci_get_param(a, "proc.path");
31
32   if (access(name, R_OK))
33     {
34       a->warning("Cannot open %s", name);
35       return 0;
36     }
37   a->debug("...using %s", name);
38   return 1;
39 }
40
41 static void
42 proc_init(struct pci_access *a)
43 {
44   a->fd = -1;
45 }
46
47 static void
48 proc_cleanup(struct pci_access *a)
49 {
50   if (a->fd >= 0)
51     {
52       close(a->fd);
53       a->fd = -1;
54     }
55 }
56
57 static void
58 proc_scan(struct pci_access *a)
59 {
60   FILE *f;
61   char buf[512];
62
63   if (snprintf(buf, sizeof(buf), "%s/devices", pci_get_param(a, "proc.path")) == sizeof(buf))
64     a->error("File name too long");
65   f = fopen(buf, "r");
66   if (!f)
67     a->error("Cannot open %s", buf);
68   while (fgets(buf, sizeof(buf)-1, f))
69     {
70       struct pci_dev *d = pci_alloc_dev(a);
71       unsigned int dfn, vend, cnt, known;
72
73 #define F " " PCIADDR_T_FMT
74       cnt = sscanf(buf, "%x %x %x" F F F F F F F F F F F F F F,
75              &dfn,
76              &vend,
77              &d->irq,
78              &d->base_addr[0],
79              &d->base_addr[1],
80              &d->base_addr[2],
81              &d->base_addr[3],
82              &d->base_addr[4],
83              &d->base_addr[5],
84              &d->rom_base_addr,
85              &d->size[0],
86              &d->size[1],
87              &d->size[2],
88              &d->size[3],
89              &d->size[4],
90              &d->size[5],
91              &d->rom_size);
92 #undef F
93       if (cnt != 9 && cnt != 10 && cnt != 17)
94         a->error("proc: parse error (read only %d items)", cnt);
95       d->bus = dfn >> 8U;
96       d->dev = PCI_SLOT(dfn & 0xff);
97       d->func = PCI_FUNC(dfn & 0xff);
98       d->vendor_id = vend >> 16U;
99       d->device_id = vend & 0xffff;
100       known = PCI_FILL_IDENT;
101       if (!a->buscentric)
102         {
103           known |= PCI_FILL_IRQ | PCI_FILL_BASES;
104           if (cnt >= 10)
105             known |= PCI_FILL_ROM_BASE;
106           if (cnt >= 17)
107             known |= PCI_FILL_SIZES;
108         }
109       d->known_fields = known;
110       pci_link_dev(a, d);
111     }
112   fclose(f);
113 }
114
115 static int
116 proc_setup(struct pci_dev *d, int rw)
117 {
118   struct pci_access *a = d->access;
119
120   if (a->cached_dev != d || a->fd_rw < rw)
121     {
122       char buf[1024];
123       int e;
124       if (a->fd >= 0)
125         close(a->fd);
126       e = snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d",
127                    pci_get_param(a, "proc.path"),
128                    d->bus, d->dev, d->func);
129       if (e < 0 || e >= (int) sizeof(buf))
130         a->error("File name too long");
131       a->fd_rw = a->writeable || rw;
132       a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
133       if (a->fd < 0)
134         a->warning("Cannot open %s", buf);
135       a->cached_dev = d;
136       a->fd_pos = 0;
137     }
138   return a->fd;
139 }
140
141 static int
142 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
143 {
144   int fd = proc_setup(d, 0);
145   int res;
146
147   if (fd < 0)
148     return 0;
149   res = do_read(d, fd, buf, len, pos);
150   if (res < 0)
151     {
152       d->access->warning("proc_read: read failed: %s", strerror(errno));
153       return 0;
154     }
155   else if (res != len)
156     return 0;
157   return 1;
158 }
159
160 static int
161 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
162 {
163   int fd = proc_setup(d, 1);
164   int res;
165
166   if (fd < 0)
167     return 0;
168   res = do_write(d, fd, buf, len, pos);
169   if (res < 0)
170     {
171       d->access->warning("proc_write: write failed: %s", strerror(errno));
172       return 0;
173     }
174   else if (res != len)
175     {
176       d->access->warning("proc_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
177       return 0;
178     }
179   return 1;
180 }
181
182 static void
183 proc_cleanup_dev(struct pci_dev *d)
184 {
185   if (d->access->cached_dev == d)
186     d->access->cached_dev = NULL;
187 }
188
189 struct pci_methods pm_linux_proc = {
190   "linux-proc",
191   "The proc file system on Linux",
192   proc_config,
193   proc_detect,
194   proc_init,
195   proc_cleanup,
196   proc_scan,
197   pci_generic_fill_info,
198   proc_read,
199   proc_write,
200   NULL,                                 /* init_dev */
201   proc_cleanup_dev
202 };