1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include <sys/stat.h>
26 #include <sys/mount.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "debugfs.h"
32
33 char debugfs_path[128];
34 char debugfs_dri_path[128];
35
debugfs_init(void)36 int debugfs_init(void)
37 {
38 const char *path = "/sys/kernel/debug";
39 struct stat st;
40 int n;
41
42 if (stat("/debug/dri", &st) == 0) {
43 path = "/debug/dri";
44 goto find_minor;
45 }
46
47 if (stat("/sys/kernel/debug/dri", &st) == 0)
48 goto find_minor;
49
50 if (stat("/sys/kernel/debug", &st))
51 return errno;
52
53 if (mount("debug", "/sys/kernel/debug", "debugfs", 0, 0))
54 return errno;
55
56 find_minor:
57 strcpy(debugfs_path, path);
58 for (n = 0; n < 16; n++) {
59 int len = sprintf(debugfs_dri_path, "%s/dri/%d", path, n);
60 sprintf(debugfs_dri_path + len, "/i915_error_state");
61 if (stat(debugfs_dri_path, &st) == 0) {
62 debugfs_dri_path[len] = '\0';
63 return 0;
64 }
65 }
66
67 debugfs_dri_path[0] = '\0';
68 return ENOENT;
69 }
70