hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2019 Rockchip Electronics Co., Ltd.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL), available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <unistd.h>
 
#include <libdrm/drm.h>
#include <libdrm/drm_mode.h>
#include "drm.h"
#include "uvc_log.h"
 
#define DRM_DEVICE "/dev/dri/card0"
 
int drm_open(void)
{
    int fd;
    fd = open(DRM_DEVICE, O_RDWR);
    if (fd < 0)
    {
        LOG_ERROR("open %s failed!\n", DRM_DEVICE);
        return -1;
    }
    return fd;
}
 
void drm_close(int fd)
{
    if (fd >= 0)
        close(fd);
}
 
static int drm_ioctl(int fd, int req, void *arg)
{
    int ret;
 
    do
    {
        ret = ioctl(fd, req, arg);
    }
    while (ret == -1 && (errno == EINTR || errno == EAGAIN));
 
    return ret;
}
 
int drm_alloc(int fd, size_t len, size_t align, unsigned int *handle, unsigned int flags)
{
    int ret;
    struct drm_mode_create_dumb dmcb;
 
    memset(&dmcb, 0, sizeof(struct drm_mode_create_dumb));
    dmcb.bpp = 8;
    dmcb.width = (len + align - 1) & (~(align - 1));
    dmcb.height = 1;
    dmcb.flags = flags;
 
    if (handle == NULL)
        return -EINVAL;
 
    ret = drm_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &dmcb);
    if (ret < 0)
        return ret;
 
    *handle = dmcb.handle;
 
    return ret;
}
 
int drm_free(int fd, unsigned int handle)
{
    struct drm_mode_destroy_dumb data =
    {
        .handle = handle,
    };
    return drm_ioctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &data);
}
 
void *drm_map_buffer(int fd, unsigned int handle, size_t len)
{
    struct drm_mode_map_dumb dmmd;
    void *buf = NULL;
    int ret;
 
    memset(&dmmd, 0, sizeof(dmmd));
    dmmd.handle = handle;
 
    ret = drm_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &dmmd);
    if (ret)
    {
        LOG_ERROR("map_dumb failed: %s\n", strerror(ret));
        return NULL;
    }
 
    buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, dmmd.offset);
    if (buf == MAP_FAILED)
    {
        LOG_ERROR("mmap failed: %s\n", strerror(errno));
        return NULL;
    }
 
    return buf;
}
 
void drm_unmap_buffer(void *buf, size_t len)
{
    if (buf)
        munmap(buf, len);
}
 
int drm_handle_to_fd(int fd, unsigned int handle, int *map_fd, unsigned int flags)
{
    int ret;
    struct drm_prime_handle dph;
 
    memset(&dph, 0, sizeof(struct drm_prime_handle));
    dph.handle = handle;
    dph.fd = -1;
    dph.flags = flags;
 
    if (map_fd == NULL)
        return -EINVAL;
 
    ret = drm_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &dph);
    if (ret < 0)
        return ret;
 
    *map_fd = dph.fd;
 
    if (*map_fd < 0)
    {
        LOG_ERROR("map ioctl returned negative fd\n");
        return -EINVAL;
    }
 
    return ret;
}
 
 
int drm_get_info_from_name(
    int   fd,
    unsigned int   name,
    unsigned int  *handle,
    int  *size)
{
    int  ret = 0;
    struct drm_gem_open req;
 
    req.name = name;
    ret = drm_ioctl(fd, DRM_IOCTL_GEM_OPEN, &req);
    if (ret < 0)
    {
        return ret;
    }
 
    *handle = req.handle;
    *size   = (int)req.size;
 
    return ret;
}