Forward DRM authentication to the upstream server.
[platform/adaptation/renesas_rcar/wayland-kms.git] / wayland-kms.c
1 /*
2  * Copyright © 2013 Renesas Solutions Corp.
3  * Copyright © 2011 Kristian Høgsberg
4  * Copyright © 2011 Benjamin Franzke
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  * Authors:
27  *    Takanari Hayama <taki@igel.co.jp>
28  *
29  * Based on wayland-drm.c by the following authors:
30  *    Kristian Høgsberg <krh@bitplanet.net>
31  *    Benjamin Franzke <benjaminfranzke@googlemail.com>
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stddef.h>
38 #include <unistd.h>
39 #include <errno.h>
40
41 #include <xf86drm.h>
42 #include <wayland-server.h>
43 #include "wayland-kms.h"
44 #include "wayland-kms-auth.h"
45 #include "wayland-kms-server-protocol.h"
46
47 #if defined(DEBUG)
48 #       define WLKMS_DEBUG(s, x...) { printf(s, ##x); }
49 #else
50 #       define WLKMS_DEBUG(s, x...) { }
51 #endif
52
53 struct wl_kms {
54         struct wl_display *display;
55         int fd;                         /* FD for DRM */
56         char *device_name;
57
58         struct kms_auth *auth;          /* for nested authentication */
59 };
60
61 /*
62  * wl_kms server
63  */
64
65 static void destroy_buffer(struct wl_resource *resource)
66 {
67         struct wl_kms_buffer *buffer = resource->data;
68         free(buffer);
69 }
70
71 static void
72 buffer_destroy(struct wl_client *client, struct wl_resource *resource)
73 {
74         wl_resource_destroy(resource);
75 }
76
77 const static struct wl_buffer_interface kms_buffer_interface = {
78         .destroy = buffer_destroy
79 };
80
81 static void
82 kms_authenticate(struct wl_client *client, struct wl_resource *resource,
83                  uint32_t magic)
84 {
85         struct wl_kms *kms = resource->data;
86         int err;
87
88         WLKMS_DEBUG("%s: %s: magic=%lu\n", __FILE__, __func__, magic);
89
90         if (kms->auth) {
91                 err = kms_auth_request(kms->auth, magic);
92         } else {
93                 err = drmAuthMagic(kms->fd, magic);
94         }
95
96         if (err < 0) {
97                 wl_resource_post_error(resource, WL_KMS_ERROR_AUTHENTICATION_FAILED,
98                                        "authentication failed");
99                 WLKMS_DEBUG("%s: %s: authentication failed.\n", __FILE__, __func__);
100         } else {
101                 wl_resource_post_event(resource, WL_KMS_AUTHENTICATED);
102                 WLKMS_DEBUG("%s: %s: authentication succeeded.\n", __FILE__, __func__);
103         }
104 }
105
106 static void
107 kms_create_buffer(struct wl_client *client, struct wl_resource *resource,
108                   uint32_t id, int32_t prime_fd, int32_t width, int32_t height,
109                   uint32_t stride, uint32_t format, uint32_t handle)
110 {
111         struct wl_kms *kms = resource->data;
112         struct wl_kms_buffer *buffer;
113         int err;
114
115         switch (format) {
116         case WL_KMS_FORMAT_ARGB8888:
117         case WL_KMS_FORMAT_XRGB8888:
118                 break;
119         default:
120                 wl_resource_post_error(resource,
121                                        WL_KMS_ERROR_INVALID_FORMAT,
122                                        "invalid format");
123                 return;
124         }
125
126         buffer = calloc(1, sizeof *buffer);
127         if (buffer == NULL) {
128                 wl_resource_post_no_memory(resource);
129                 return;
130         }
131
132         buffer->kms = kms;
133         buffer->width = width;
134         buffer->height = height;
135         buffer->format = format;
136         buffer->stride = stride;
137         buffer->fd = prime_fd;
138
139         WLKMS_DEBUG("%s: %s: prime_fd=%d\n", __FILE__, __func__, prime_fd);
140 #if 0
141         if ((err = drmPrimeFDToHandle(kms->fd, prime_fd, &buffer->handle))) {
142                 WLKMS_DEBUG("%s: %s: drmPrimeFDToHandle() failed...%d (%s)\n", __FILE__, __func__, err, strerror(errno));
143                 wl_resource_post_error(resource,
144                                        WL_KMS_ERROR_INVALID_FD,
145                                        "invalid prime FD");
146                 return;
147         }
148 #else
149         {
150                 struct drm_gem_open op;
151                 int ret;
152
153                 op.name   = prime_fd;
154                 op.handle = 0;
155
156                 ret = drmIoctl(kms->fd, DRM_IOCTL_GEM_OPEN, &op);
157                 if (ret) {
158                         WLKMS_DEBUG("%s: %s: DRM_IOCTL_GEM_OPEN failed...(%s)\n", __FILE__, __func__, strerror(errno));
159                         wl_resource_post_error(resource, WL_KMS_ERROR_INVALID_FD, "invalid prime FD");
160                         return;
161                 }
162                 buffer->handle = op.handle;
163         }
164 #endif
165
166         // We create a wl_buffer
167         buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
168         if (!buffer->resource) {
169                 wl_resource_post_no_memory(resource);
170                 free(buffer);
171                 return;
172         }
173
174         wl_resource_set_implementation(buffer->resource,
175                                        (void (**)(void))&kms_buffer_interface,
176                                        buffer, destroy_buffer);
177 }
178
179 const static struct wl_kms_interface kms_interface = {
180         .authenticate = kms_authenticate,
181         .create_buffer = kms_create_buffer,
182 };
183
184 static void
185 bind_kms(struct wl_client *client, void *data, uint32_t version, uint32_t id)
186 {
187         struct wl_kms *kms = data;
188         struct wl_resource *resource;
189         uint32_t capabilities;
190
191         resource = wl_resource_create(client, &wl_kms_interface, version, id);
192         if (!resource) {
193                 wl_client_post_no_memory(client);
194                 return;
195         }
196
197         wl_resource_set_implementation(resource, &kms_interface, data, NULL);
198
199         wl_resource_post_event(resource, WL_KMS_DEVICE, kms->device_name);
200         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_ARGB8888);
201         wl_resource_post_event(resource, WL_KMS_FORMAT, WL_KMS_FORMAT_XRGB8888);
202 }
203
204 struct wl_kms_buffer *wayland_kms_buffer_get(struct wl_resource *resource)
205 {
206         if (resource == NULL)
207                 return NULL;
208
209         if (wl_resource_instance_of(resource, &wl_buffer_interface,
210                                     &kms_buffer_interface))
211                 return wl_resource_get_user_data(resource);
212         else
213                 return NULL;
214 }
215
216 struct wl_kms *wayland_kms_init(struct wl_display *display,
217                                 struct wl_display *server, char *device_name, int fd)
218 {
219         struct wl_kms *kms;
220
221         if (!(kms = calloc(1, sizeof(struct wl_kms))))
222                 return NULL;
223
224         kms->display = display;
225         kms->device_name = strdup(device_name);
226         kms->fd = fd;
227
228         wl_global_create(display, &wl_kms_interface, 1, kms, bind_kms);
229
230         /*
231          * we're the server in the middle. we should forward the auth
232          * request to our server.
233          */
234         if (server) {
235                 if (!(kms->auth = kms_auth_init(server))) {
236                         free(kms);
237                         kms = NULL;
238                 }
239         }
240
241         return kms;
242 }
243
244 void wayland_kms_uninit(struct wl_kms *kms)
245 {
246         free(kms->device_name);
247
248         /* FIXME: need wl_display_del_{object,global} */
249
250         free(kms);
251 }
252
253 uint32_t wayland_kms_buffer_get_format(struct wl_kms_buffer *buffer)
254 {
255         return buffer->format;
256 }
257
258 int wayland_kms_query_buffer(struct wl_kms *kms, struct wl_resource *resource,
259                                 enum wl_kms_attribute attr, int *value)
260 {
261         struct wl_kms_buffer *buffer = wayland_kms_buffer_get(resource);
262         if (!buffer)
263                 return -1;
264
265         switch(attr) {
266         case WL_KMS_WIDTH:
267                 *value = buffer->width;
268                 return 0;
269
270         case WL_KMS_HEIGHT:
271                 *value = buffer->height;
272                 return 0;
273         
274         case WL_KMS_TEXTURE_FORMAT:
275                 *value = WL_KMS_FORMAT_ARGB8888;
276                 return 0;
277         }
278
279         return -1;
280 }