Use __getdents instead of __getdirentries in sparc __get_clockfreq_via_proc_openprom.
[glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc64 / get_clockfreq.c
blobabd0ebfb9f7b795432e32b195fdb8af5ccb8dbd2
1 /* Get frequency of the system processor. sparc64 version.
2 Copyright (C) 2001, 2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <ctype.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <dirent.h>
24 #include <stdlib.h>
25 #include <sys/ioctl.h>
26 #include <libc-internal.h>
27 #include <asm/openpromio.h>
29 static hp_timing_t
30 __get_clockfreq_via_cpuinfo (void)
32 hp_timing_t result;
33 int fd;
35 result = 0;
37 fd = __open ("/proc/cpuinfo", O_RDONLY);
38 if (fd != -1)
40 char buf[8192];
41 ssize_t n;
43 n = __read (fd, buf, sizeof buf);
44 if (n > 0)
46 char *mhz = memmem (buf, n, "Cpu0ClkTck", 7);
48 if (mhz != NULL)
50 char *endp = buf + n;
52 /* Search for the beginning of the string. */
53 while (mhz < endp
54 && (*mhz < '0' || *mhz > '9')
55 && (*mhz < 'a' || *mhz > 'f')
56 && *mhz != '\n')
57 ++mhz;
59 while (mhz < endp && *mhz != '\n')
61 if ((*mhz >= '0' && *mhz <= '9') ||
62 (*mhz >= 'a' && *mhz <= 'f'))
64 result <<= 4;
65 if (*mhz >= '0' && *mhz <= '9')
66 result += *mhz - '0';
67 else
68 result += (*mhz - 'a') + 10;
70 ++mhz;
75 __close (fd);
78 return result;
81 static hp_timing_t
82 __get_clockfreq_via_proc_openprom (void)
84 hp_timing_t result;
85 int obp_fd;
87 result = 0;
89 obp_fd = __open ("/proc/openprom", O_RDONLY);
90 if (obp_fd != -1)
92 unsigned long int buf[4096 / sizeof (unsigned long int)];
93 struct dirent *dirp = (struct dirent *) buf;
94 ssize_t len;
96 while ((len = __getdents (obp_fd, (char *) dirp, sizeof (buf))) > 0)
98 struct dirent *this_dirp = dirp;
100 while (len > 0)
102 char node[strlen ("/proc/openprom/")
103 + _D_ALLOC_NAMLEN (this_dirp)
104 + strlen ("/clock-frequency")];
105 char *prop;
106 int fd;
108 /* Note that
109 strlen("/clock-frequency") > strlen("/device_type")
111 __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
112 this_dirp->d_name),
113 "/device_type");
114 fd = __open (node, O_RDONLY);
115 if (fd != -1)
117 char type_string[128];
118 int ret;
120 ret = __read (fd, type_string, sizeof (type_string));
121 if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
123 int clkfreq_fd;
125 __stpcpy (prop, "/clock-frequency");
126 clkfreq_fd = open (node, O_RDONLY);
127 if (clkfreq_fd != -1)
129 if (read (clkfreq_fd, type_string,
130 sizeof (type_string)) > 0)
131 result = (hp_timing_t)
132 strtoull (type_string, NULL, 16);
133 close (clkfreq_fd);
136 __close (fd);
139 if (result != 0)
140 break;
142 len -= this_dirp->d_reclen;
143 this_dirp = (struct dirent *)
144 ((char *) this_dirp + this_dirp->d_reclen);
146 if (result != 0)
147 break;
149 __close (obp_fd);
152 return result;
155 static void set_obp_int (struct openpromio *op, int val)
157 char *cp = op->oprom_array;
158 int *ip = (int *) cp;
160 *ip = val;
163 static int get_obp_int (struct openpromio *op)
165 char *cp = op->oprom_array;
166 int *ip = (int *) cp;
168 return *ip;
171 static hp_timing_t
172 __get_clockfreq_via_dev_openprom (void)
174 hp_timing_t result;
175 int obp_dev_fd;
177 result = 0;
179 obp_dev_fd = __open ("/dev/openprom", O_RDONLY);
180 if (obp_dev_fd != -1)
182 char obp_buf[8192];
183 struct openpromio *obp_cmd = (struct openpromio *)obp_buf;
184 int ret;
186 obp_cmd->oprom_size =
187 sizeof (obp_buf) - sizeof (unsigned int);
188 set_obp_int (obp_cmd, 0);
189 ret = __ioctl (obp_dev_fd, OPROMCHILD, (char *) obp_cmd);
190 if (ret == 0)
192 int cur_node = get_obp_int (obp_cmd);
194 while (cur_node != 0 && cur_node != -1)
196 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
197 strcpy (obp_cmd->oprom_array, "device_type");
198 ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
199 if (ret == 0
200 && strncmp (obp_cmd->oprom_array, "cpu", 3) == 0)
202 obp_cmd->oprom_size = (sizeof (obp_buf)
203 - sizeof (unsigned int));
204 strcpy (obp_cmd->oprom_array, "clock-frequency");
205 ret = __ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
206 if (ret == 0)
207 result = (hp_timing_t) get_obp_int (obp_cmd);
209 obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
210 set_obp_int (obp_cmd, cur_node);
211 ret = __ioctl (obp_dev_fd, OPROMNEXT, (char *) obp_cmd);
212 if (ret < 0)
213 break;
214 cur_node = get_obp_int (obp_cmd);
219 return result;
222 hp_timing_t
223 __get_clockfreq (void)
225 static hp_timing_t result;
227 /* If this function was called before, we know the result. */
228 if (result != 0)
229 return result;
231 /* We first read the information from the /proc/cpuinfo file.
232 It contains at least one line like
233 Cpu0ClkTick : 000000002cb41780
234 We search for this line and convert the number in an integer. */
235 result = __get_clockfreq_via_cpuinfo ();
236 if (result != 0)
237 return result;
239 /* If that did not work, try to find an OpenPROM node
240 with device_type equal to 'cpu' using /dev/openprom
241 and fetch the clock-frequency property from there. */
242 result = __get_clockfreq_via_dev_openprom ();
243 if (result != 0)
244 return result;
246 /* Finally, try the same lookup as above but using /proc/openprom. */
247 result = __get_clockfreq_via_proc_openprom ();
249 return result;