1 /*
2  * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include "sysdeps.h"
26 #include <xf86drm.h>
27 #include "va_drm.h"
28 #include "va_backend.h"
29 #include "va_drmcommon.h"
30 #include "va_drm_auth.h"
31 #include "va_drm_utils.h"
32 
33 static int
va_DisplayContextIsValid(VADisplayContextP pDisplayContext)34 va_DisplayContextIsValid(VADisplayContextP pDisplayContext)
35 {
36     VADriverContextP const pDriverContext = pDisplayContext->pDriverContext;
37 
38     return (pDriverContext &&
39             pDriverContext->display_type == VA_DISPLAY_DRM);
40 }
41 
42 static void
va_DisplayContextDestroy(VADisplayContextP pDisplayContext)43 va_DisplayContextDestroy(VADisplayContextP pDisplayContext)
44 {
45     if (!pDisplayContext)
46         return;
47 
48     free(pDisplayContext->pDriverContext->drm_state);
49     free(pDisplayContext->pDriverContext);
50     free(pDisplayContext);
51 }
52 
53 static VAStatus
va_DisplayContextGetDriverName(VADisplayContextP pDisplayContext,char ** driver_name_ptr)54 va_DisplayContextGetDriverName(
55     VADisplayContextP pDisplayContext,
56     char            **driver_name_ptr
57 )
58 {
59 
60     VADriverContextP const ctx = pDisplayContext->pDriverContext;
61     struct drm_state * const drm_state = ctx->drm_state;
62     drm_magic_t magic;
63     VAStatus status;
64     int ret;
65 
66     status = VA_DRM_GetDriverName(ctx, driver_name_ptr);
67     if (status != VA_STATUS_SUCCESS)
68         return status;
69 
70     ret = drmGetMagic(drm_state->fd, &magic);
71     if (ret < 0)
72         return VA_STATUS_ERROR_OPERATION_FAILED;
73 
74     if (!va_drm_is_authenticated(drm_state->fd)) {
75         if (!va_drm_authenticate(drm_state->fd, magic))
76             return VA_STATUS_ERROR_OPERATION_FAILED;
77         if (!va_drm_is_authenticated(drm_state->fd))
78             return VA_STATUS_ERROR_OPERATION_FAILED;
79     }
80 
81     drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
82 
83     return VA_STATUS_SUCCESS;
84 }
85 
86 VADisplay
vaGetDisplayDRM(int fd)87 vaGetDisplayDRM(int fd)
88 {
89     VADisplayContextP pDisplayContext = NULL;
90     VADriverContextP  pDriverContext  = NULL;
91     struct drm_state *drm_state       = NULL;
92 
93     if (fd < 0)
94         return NULL;
95 
96     /* Create new entry */
97     /* XXX: handle cache? */
98     drm_state = calloc(1, sizeof(*drm_state));
99     if (!drm_state)
100         goto error;
101     drm_state->fd = fd;
102 
103     pDriverContext = calloc(1, sizeof(*pDriverContext));
104     if (!pDriverContext)
105         goto error;
106     pDriverContext->native_dpy   = NULL;
107     pDriverContext->display_type = VA_DISPLAY_DRM;
108     pDriverContext->drm_state    = drm_state;
109 
110     pDisplayContext = calloc(1, sizeof(*pDisplayContext));
111     if (!pDisplayContext)
112         goto error;
113 
114     pDisplayContext->vadpy_magic     = VA_DISPLAY_MAGIC;
115     pDisplayContext->pDriverContext  = pDriverContext;
116     pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
117     pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
118     pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
119     return pDisplayContext;
120 
121 error:
122     free(pDisplayContext);
123     free(pDriverContext);
124     free(drm_state);
125     return NULL;
126 }
127