xref: /rockchip-linux_mpp/osal/driver/mpp_vcodec_client.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3  * Copyright (c) 2024 Rockchip Electronics Co., Ltd.
4  */
5 
6 #define MODULE_TAG "mpp_vcodec"
7 
8 #include <sys/ioctl.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <string.h>
12 
13 #include "mpp_log.h"
14 #include "mpp_env.h"
15 #include "mpp_common.h"
16 #include "mpp_vcodec_client.h"
17 
18 #define VOCDEC_IOC_MAGIC            'V'
19 #define VOCDEC_IOC_CFG              _IOW(VOCDEC_IOC_MAGIC, 1, unsigned int)
20 
21 typedef struct vcodec_req_t {
22     RK_U32 cmd;
23     RK_U32 ctrl_cmd;
24     RK_U32 size;
25     RK_U64 data;
26 } vcodec_req;
27 
28 #if __SIZEOF_POINTER__ == 4
29 #define REQ_DATA_PTR(ptr) ((RK_U32)ptr)
30 #elif __SIZEOF_POINTER__ == 8
31 #define REQ_DATA_PTR(ptr) ((RK_U64)ptr)
32 #endif
33 
mpp_vcodec_open(void)34 RK_S32 mpp_vcodec_open(void)
35 {
36     RK_S32 fd = -1;
37 
38     fd = open("/dev/vcodec", O_RDWR | O_CLOEXEC);
39     if (fd < 0) {
40         mpp_err("open /dev/vcodec failed errno %d %s\n", errno, strerror(errno));
41         return -1;
42     }
43 
44     return fd;
45 }
46 
mpp_vcodec_ioctl(RK_S32 fd,RK_U32 cmd,RK_U32 ctrl_cmd,RK_U32 size,void * param)47 MPP_RET mpp_vcodec_ioctl(RK_S32 fd, RK_U32 cmd, RK_U32 ctrl_cmd, RK_U32 size, void *param)
48 {
49     vcodec_req req;
50     RK_S32 ret = 0;
51 
52     memset(&req, 0, sizeof(req));
53     req.cmd = cmd;
54     req.ctrl_cmd = ctrl_cmd;
55     req.size = size;
56     req.data = REQ_DATA_PTR(param);
57 
58     ret = (RK_S32)ioctl(fd, VOCDEC_IOC_CFG, &req);
59     if (ret) {
60         mpp_err("ioctl fd %d failed ret %d errno %d %s\n",
61                 fd, ret, errno, strerror(errno));
62         return MPP_NOK;
63     }
64 
65     return MPP_OK;
66 }
67 
mpp_vcodec_close(RK_S32 fd)68 MPP_RET mpp_vcodec_close(RK_S32 fd)
69 {
70     if (fd)
71         close(fd);
72 
73     return MPP_OK;
74 }
75