1 /**************************************************************************
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
32 #define WL_HIDE_DEPRECATED
44 #include "tbm_bufmgr_int.h"
46 #include "wayland-tbm-drm-auth-server-protocol.h"
48 struct wayland_tbm_drm_auth_server {
49 struct wl_display *display;
50 struct wl_global *wl_tbm_drm_auth_global;
57 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
59 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv;
63 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
64 struct wl_resource *resource)
67 uint32_t capabilities;
68 char *device_name = NULL;
69 drm_magic_t magic = 0;
71 fd = open(tbm_drm_auth_srv->device_name, O_RDWR | O_CLOEXEC);
72 if (fd == -1 && errno == EINVAL) {
73 fd = open(tbm_drm_auth_srv->device_name, O_RDWR);
75 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
79 TBM_LOG_E("failed to open drm : device_name, %s\n", tbm_drm_auth_srv->device_name);
81 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
82 "authenicate failed::open_drm");
86 if (drmGetMagic(fd, &magic) < 0) {
87 if (errno != EACCES) {
88 TBM_LOG_E("failed to get magic\n");
90 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
91 "authenicate failed::get_magic");
96 if (drmAuthMagic(tbm_drm_auth_srv->fd, magic) < 0) {
97 TBM_LOG_E("failed to authenticate magic\n");
99 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
100 "authenicate failed::auth_magic");
104 capabilities = tbm_drm_auth_srv->flags;
105 device_name = tbm_drm_auth_srv->device_name;
108 wl_tbm_drm_auth_send_authentication_info(resource, device_name, capabilities, fd);
114 if (device_name && device_name != tbm_drm_auth_srv->device_name)
120 _wayland_tbm_drm_auth_server_impl_get_authentication_info(struct wl_client *client,
121 struct wl_resource *resource)
123 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv = wl_resource_get_user_data(resource);
125 /* if display server is the client of the host display server, for embedded server */
126 _send_server_auth_info(tbm_drm_auth_srv, resource);
130 static const struct wl_tbm_drm_auth_interface _wayland_tbm_drm_auth_server_implementation = {
131 _wayland_tbm_drm_auth_server_impl_get_authentication_info,
135 _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
139 struct wl_resource *resource;
141 resource = wl_resource_create(client, &wl_tbm_drm_auth_interface, MIN(version, 1), id);
143 wl_client_post_no_memory(client);
147 wl_resource_set_implementation(resource,
148 &_wayland_tbm_drm_auth_server_implementation,
154 tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *device_name, uint32_t flags)
156 if (!tbm_drm_auth_srv) {
157 TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0);
159 tbm_drm_auth_srv = calloc(1, sizeof(struct wayland_tbm_drm_auth_server));
160 TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0);
162 tbm_drm_auth_srv->display = (struct wl_display *)wl_display;
163 tbm_drm_auth_srv->device_name = strdup(device_name);
164 tbm_drm_auth_srv->fd = fd;
165 tbm_drm_auth_srv->flags = flags;
167 if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
168 TBM_LOG_E("[TBM_DRM] fail to add socket\n");
170 if (tbm_drm_auth_srv->device_name)
171 free(tbm_drm_auth_srv->device_name);
173 free(tbm_drm_auth_srv);
174 tbm_drm_auth_srv = NULL;
179 /* init the client resource list */
180 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
181 tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
188 tbm_drm_helper_wl_auth_server_deinit(void)
190 if (tbm_drm_auth_srv) {
191 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
193 if (tbm_drm_auth_srv->device_name)
194 free(tbm_drm_auth_srv->device_name);
196 free(tbm_drm_auth_srv);
197 tbm_drm_auth_srv = NULL;
202 tbm_drm_helper_get_master_fd(void)
205 int ret, flags, fd = -1;
208 value = (const char*)getenv("TDM_DRM_MASTER_FD");
212 ret = sscanf(value, "%d", &fd);
216 TBM_LOG_I("TDM_DRM_MASTER_FD: %d\n", fd);
218 flags = fcntl(fd, F_GETFD);
220 TBM_LOG_E("fcntl failed: %m");
226 TBM_LOG_E("dup failed: %m");
230 fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC);
232 TBM_LOG_I("Return MASTER_FD: %d\n", new_fd);
238 tbm_drm_helper_set_tbm_master_fd(int fd)
243 snprintf(buf, sizeof(buf), "%d", fd);
245 ret = setenv("TBM_DRM_MASTER_FD", (const char*)buf, 1);
247 TBM_LOG_E("failed to set TIZEN_DRM_MASTER_FD to %d", fd);
251 TBM_LOG_I("TBM_DRM_MASTER_FD: %d\n", fd);
255 tbm_drm_helper_unset_tbm_master_fd(void)
259 ret = unsetenv("TBM_DRM_MASTER_FD");
261 TBM_LOG_E("failed to unset TBM_DRM_MASTER_FD");