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
38 #include "tbm_bufmgr_int.h"
40 #include "wayland-tbm-drm-auth-server-protocol.h"
42 struct wayland_tbm_drm_auth_server {
43 struct wl_display *display;
44 struct wl_global *wl_tbm_drm_auth_global;
51 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
53 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv;
57 _send_server_auth_info(struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv,
58 struct wl_resource *resource)
61 uint32_t capabilities;
62 char *device_name = NULL;
63 drm_magic_t magic = 0;
65 fd = open(tbm_drm_auth_srv->device_name, O_RDWR | O_CLOEXEC);
66 if (fd == -1 && errno == EINVAL) {
67 fd = open(tbm_drm_auth_srv->device_name, O_RDWR);
69 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
73 TBM_LOG_E("failed to open drm : device_name, %s\n", tbm_drm_auth_srv->device_name);
75 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
76 "authenicate failed::open_drm");
80 if (drmGetMagic(fd, &magic) < 0) {
81 if (errno != EACCES) {
82 TBM_LOG_E("failed to get magic\n");
84 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
85 "authenicate failed::get_magic");
90 if (drmAuthMagic(tbm_drm_auth_srv->fd, magic) < 0) {
91 TBM_LOG_E("failed to authenticate magic\n");
93 wl_resource_post_error(resource, WL_TBM_DRM_AUTH_ERROR_AUTHENTICATE_FAIL,
94 "authenicate failed::auth_magic");
98 capabilities = tbm_drm_auth_srv->flags;
99 device_name = tbm_drm_auth_srv->device_name;
102 wl_tbm_drm_auth_send_authentication_info(resource, device_name, capabilities, fd);
108 if (device_name && device_name != tbm_drm_auth_srv->device_name)
114 _wayland_tbm_drm_auth_server_impl_get_authentication_info(struct wl_client *client,
115 struct wl_resource *resource)
117 struct wayland_tbm_drm_auth_server *tbm_drm_auth_srv = wl_resource_get_user_data(resource);
119 /* if display server is the client of the host display server, for embedded server */
120 _send_server_auth_info(tbm_drm_auth_srv, resource);
124 static const struct wl_tbm_drm_auth_interface _wayland_tbm_drm_auth_server_implementation = {
125 _wayland_tbm_drm_auth_server_impl_get_authentication_info,
129 _wayland_tbm_drm_auth_server_bind_cb(struct wl_client *client, void *data,
133 struct wl_resource *resource;
135 resource = wl_resource_create(client, &wl_tbm_drm_auth_interface, MIN(version, 1), id);
137 wl_client_post_no_memory(client);
141 wl_resource_set_implementation(resource,
142 &_wayland_tbm_drm_auth_server_implementation,
148 tbm_drm_helper_wl_auth_server_init(void *wl_display, int fd, const char *device_name, uint32_t flags)
150 if (!tbm_drm_auth_srv) {
151 TBM_RETURN_VAL_IF_FAIL(wl_display != NULL, 0);
153 tbm_drm_auth_srv = calloc(1, sizeof(struct wayland_tbm_drm_auth_server));
154 TBM_RETURN_VAL_IF_FAIL(tbm_drm_auth_srv != NULL, 0);
156 tbm_drm_auth_srv->display = (struct wl_display *)wl_display;
157 tbm_drm_auth_srv->device_name = strdup(device_name);
158 tbm_drm_auth_srv->fd = fd;
159 tbm_drm_auth_srv->flags = flags;
161 if (wl_display_add_socket(tbm_drm_auth_srv->display, "tbm-drm-auth")) {
162 TBM_LOG_E("[TBM_DRM] fail to add socket\n");
164 if (tbm_drm_auth_srv->device_name)
165 free(tbm_drm_auth_srv->device_name);
167 free(tbm_drm_auth_srv);
168 tbm_drm_auth_srv = NULL;
173 /* init the client resource list */
174 tbm_drm_auth_srv->wl_tbm_drm_auth_global = wl_global_create(tbm_drm_auth_srv->display, &wl_tbm_drm_auth_interface, 1,
175 tbm_drm_auth_srv, _wayland_tbm_drm_auth_server_bind_cb);
182 tbm_drm_helper_wl_auth_server_deinit(void)
184 if (tbm_drm_auth_srv) {
185 wl_global_destroy(tbm_drm_auth_srv->wl_tbm_drm_auth_global);
187 if (tbm_drm_auth_srv->device_name)
188 free(tbm_drm_auth_srv->device_name);
190 free(tbm_drm_auth_srv);
191 tbm_drm_auth_srv = NULL;
196 tbm_drm_helper_get_master_fd(void)
199 int ret, flags, fd = -1;
202 value = (const char*)getenv("TDM_DRM_MASTER_FD");
206 ret = sscanf(value, "%d", &fd);
210 TBM_LOG_I("TDM_DRM_MASTER_FD: %d\n", fd);
212 flags = fcntl(fd, F_GETFD);
214 TBM_LOG_E("fcntl failed: %m");
220 TBM_LOG_E("dup failed: %m");
224 fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC);
226 TBM_LOG_I("Return MASTER_FD: %d\n", new_fd);
232 tbm_drm_helper_set_tbm_master_fd(int fd)
237 snprintf(buf, sizeof(buf), "%d", fd);
239 ret = setenv("TBM_DRM_MASTER_FD", (const char*)buf, 1);
241 TBM_LOG_E("failed to set TIZEN_DRM_MASTER_FD to %d", fd);
245 TBM_LOG_I("TBM_DRM_MASTER_FD: %d\n", fd);
249 tbm_drm_helper_unset_tbm_master_fd(void)
253 ret = unsetenv("TBM_DRM_MASTER_FD");
255 TBM_LOG_E("failed to unset TBM_DRM_MASTER_FD");