0f1984b37de2d5beef2cb9aed4bfb334a5c10e75
[platform/adaptation/nexell/libtdm-nexell.git] / src / tdm_nexell.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #if HAVE_UDEV
6 #include <libudev.h>
7 #endif
8
9 #include "tdm_nexell.h"
10 #include <tdm_helper.h>
11 #include <tbm_drm_helper.h>
12
13 #define ENABLE_PP
14
15 #define TDM_NEXELL_NAME "vigs"
16
17 static tdm_nexell_data *nexell_data;
18
19 #ifdef HAVE_UDEV
20 static struct udev_device *
21 _tdm_find_primary_gpu(void)
22 {
23         struct udev *udev;
24         struct udev_enumerate *e;
25         struct udev_list_entry *entry;
26         const char *path, *id;
27         struct udev_device *device, *drm_device, *pci;
28
29         udev = udev_new();
30         if (!udev) {
31                 TDM_ERR("fail to initialize udev context\n");
32                 return NULL;
33         }
34
35         e = udev_enumerate_new(udev);
36         udev_enumerate_add_match_subsystem(e, "drm");
37         udev_enumerate_add_match_sysname(e, "card[0-9]*");
38
39         udev_enumerate_scan_devices(e);
40         drm_device = NULL;
41         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
42                 path = udev_list_entry_get_name(entry);
43                 device = udev_device_new_from_syspath(udev, path);
44                 if (!device)
45                         continue;
46
47                 pci = udev_device_get_parent_with_subsystem_devtype(device,
48                                 "pci", NULL);
49                 if (pci) {
50                         id = udev_device_get_sysattr_value(pci, "boot_vga");
51                         if (id && !strcmp(id, "1")) {
52                                 if (drm_device)
53                                         udev_device_unref(drm_device);
54                                 drm_device = device;
55                                 break;
56                         }
57                 }
58
59                 if (!drm_device)
60                         drm_device = device;
61                 else
62                         udev_device_unref(device);
63         }
64
65         udev_enumerate_unref(e);
66         return drm_device;
67 }
68
69 static tdm_error
70 _tdm_nexell_udev_fd_handler(int fd, tdm_event_loop_mask mask, void *user_data)
71 {
72         tdm_nexell_data *edata = (tdm_nexell_data*)user_data;
73         struct udev_device *dev;
74         const char *hotplug;
75         struct stat s;
76         dev_t udev_devnum;
77         int ret;
78
79         dev = udev_monitor_receive_device(edata->uevent_monitor);
80         if (!dev) {
81                 TDM_ERR("couldn't receive device");
82                 return TDM_ERROR_OPERATION_FAILED;
83         }
84
85         udev_devnum = udev_device_get_devnum(dev);
86
87         ret = fstat(edata->drm_fd, &s);
88         if (ret == -1) {
89                 TDM_ERR("fstat failed");
90                 return TDM_ERROR_OPERATION_FAILED;
91         }
92
93         hotplug = udev_device_get_property_value(dev, "HOTPLUG");
94
95         if (memcmp(&s.st_rdev, &udev_devnum, sizeof (dev_t)) == 0 &&
96                 hotplug && atoi(hotplug) == 1)
97         {
98                 TDM_INFO("HotPlug");
99                 tdm_nexell_display_update_output_status(edata);
100         }
101
102         udev_device_unref(dev);
103
104         return TDM_ERROR_NONE;
105 }
106
107 static void
108 _tdm_nexell_udev_init(tdm_nexell_data *edata)
109 {
110         struct udev *u = NULL;
111         struct udev_monitor *mon = NULL;
112
113         u = udev_new();
114         if (!u) {
115                 TDM_ERR("couldn't create udev");
116                 goto failed;
117         }
118
119         mon = udev_monitor_new_from_netlink(u, "udev");
120         if (!mon) {
121                 TDM_ERR("couldn't create udev monitor");
122                 goto failed;
123         }
124
125         if (udev_monitor_filter_add_match_subsystem_devtype(mon, "drm", "drm_minor") > 0 ||
126             udev_monitor_enable_receiving(mon) < 0) {
127                 TDM_ERR("add match subsystem failed");
128                 goto failed;
129         }
130
131         edata->uevent_source =
132                 tdm_event_loop_add_fd_handler(edata->dpy, udev_monitor_get_fd(mon),
133                                               TDM_EVENT_LOOP_READABLE,
134                                               _tdm_nexell_udev_fd_handler,
135                                               edata, NULL);
136         if (!edata->uevent_source) {
137                 TDM_ERR("couldn't create udev event source");
138                 goto failed;
139         }
140
141         edata->uevent_monitor = mon;
142
143         TDM_INFO("hotplug monitor created");
144
145         return;
146 failed:
147         if (mon)
148                 udev_monitor_unref(mon);
149         if (u)
150                 udev_unref(u);
151 }
152
153 static void
154 _tdm_nexell_udev_deinit(tdm_nexell_data *edata)
155 {
156         if (edata->uevent_source) {
157                 tdm_event_loop_source_remove(edata->uevent_source);
158                 edata->uevent_source = NULL;
159         }
160
161         if (edata->uevent_monitor) {
162                 struct udev *u = udev_monitor_get_udev(edata->uevent_monitor);
163                 udev_monitor_unref(edata->uevent_monitor);
164                 udev_unref(u);
165                 edata->uevent_monitor = NULL;
166                 TDM_INFO("hotplug monitor destroyed");
167         }
168 }
169 #endif
170
171 static int
172 _tdm_nexell_open_drm(void)
173 {
174         int fd = -1;
175
176         fd = drmOpen(TDM_NEXELL_NAME, NULL);
177         if (fd < 0) {
178                 TDM_WRN("Cannot open '%s' drm", TDM_NEXELL_NAME);
179         }
180
181 #ifdef HAVE_UDEV
182         if (fd < 0) {
183                 struct udev_device *drm_device = NULL;
184                 const char *filename;
185                 TDM_WRN("Cannot open drm device.. search by udev");
186
187                 drm_device = _tdm_find_primary_gpu();
188                 if (drm_device == NULL) {
189                         TDM_ERR("fail to find drm device\n");
190                         goto close_l;
191                 }
192
193                 filename = udev_device_get_devnode(drm_device);
194
195                 fd = open(filename, O_RDWR | O_CLOEXEC);
196                 if (fd < 0)
197                         TDM_ERR("Cannot open drm device(%s)\n", filename);
198
199                 TDM_DBG("open drm device (name:%s, fd:%d)", filename, fd);
200
201                 udev_device_unref(drm_device);
202         }
203 close_l:
204 #endif
205         return fd;
206 }
207
208 void
209 tdm_nexell_deinit(tdm_backend_data *bdata)
210 {
211         if (nexell_data != bdata)
212                 return;
213
214         TDM_INFO("deinit");
215
216 #ifdef HAVE_UDEV
217         _tdm_nexell_udev_deinit(nexell_data);
218 #endif
219
220         tdm_nexell_display_destroy_output_list(nexell_data);
221
222         if (nexell_data->plane_res)
223                 drmModeFreePlaneResources(nexell_data->plane_res);
224         if (nexell_data->mode_res)
225                 drmModeFreeResources(nexell_data->mode_res);
226         if (nexell_data->drm_fd >= 0)
227                 close(nexell_data->drm_fd);
228
229         free(nexell_data);
230         nexell_data = NULL;
231 }
232
233 tdm_backend_data *
234 tdm_nexell_init(tdm_display *dpy, tdm_error *error)
235 {
236         tdm_func_display nexell_func_display;
237         tdm_func_output nexell_func_output;
238         tdm_func_layer nexell_func_layer;
239 #ifdef ENABLE_PP
240         tdm_func_pp nexell_func_pp;
241 #endif
242         tdm_error ret;
243
244         if (!dpy) {
245                 TDM_ERR("display is null");
246                 if (error)
247                         *error = TDM_ERROR_INVALID_PARAMETER;
248                 return NULL;
249         }
250
251         if (nexell_data) {
252                 TDM_ERR("failed: init twice");
253                 if (error)
254                         *error = TDM_ERROR_BAD_REQUEST;
255                 return NULL;
256         }
257
258         nexell_data = calloc(1, sizeof(tdm_nexell_data));
259         if (!nexell_data) {
260                 TDM_ERR("alloc failed");
261                 if (error)
262                         *error = TDM_ERROR_OUT_OF_MEMORY;
263                 return NULL;
264         }
265
266         LIST_INITHEAD(&nexell_data->output_list);
267         LIST_INITHEAD(&nexell_data->buffer_list);
268
269         memset(&nexell_func_display, 0, sizeof(nexell_func_display));
270         nexell_func_display.display_get_capability = nexell_display_get_capability;
271         nexell_func_display.display_get_pp_capability = nexell_display_get_pp_capability;
272         nexell_func_display.display_get_outputs = nexell_display_get_outputs;
273         nexell_func_display.display_get_fd = nexell_display_get_fd;
274         nexell_func_display.display_handle_events = nexell_display_handle_events;
275         nexell_func_display.display_create_pp = nexell_display_create_pp;
276
277         memset(&nexell_func_output, 0, sizeof(nexell_func_output));
278         nexell_func_output.output_get_capability = nexell_output_get_capability;
279         nexell_func_output.output_get_layers = nexell_output_get_layers;
280         nexell_func_output.output_set_property = nexell_output_set_property;
281         nexell_func_output.output_get_property = nexell_output_get_property;
282         nexell_func_output.output_wait_vblank = nexell_output_wait_vblank;
283         nexell_func_output.output_set_vblank_handler = nexell_output_set_vblank_handler;
284         nexell_func_output.output_commit = nexell_output_commit;
285         nexell_func_output.output_set_commit_handler = nexell_output_set_commit_handler;
286         nexell_func_output.output_set_dpms = nexell_output_set_dpms;
287         nexell_func_output.output_get_dpms = nexell_output_get_dpms;
288         nexell_func_output.output_set_mode = nexell_output_set_mode;
289         nexell_func_output.output_get_mode = nexell_output_get_mode;
290 #ifdef HAVE_UDEV
291         nexell_func_output.output_set_status_handler = nexell_output_set_status_handler;
292 #endif
293
294         memset(&nexell_func_layer, 0, sizeof(nexell_func_layer));
295         nexell_func_layer.layer_get_capability = nexell_layer_get_capability;
296         nexell_func_layer.layer_set_property = nexell_layer_set_property;
297         nexell_func_layer.layer_get_property = nexell_layer_get_property;
298         nexell_func_layer.layer_set_info = nexell_layer_set_info;
299         nexell_func_layer.layer_get_info = nexell_layer_get_info;
300         nexell_func_layer.layer_set_buffer = nexell_layer_set_buffer;
301         nexell_func_layer.layer_unset_buffer = nexell_layer_unset_buffer;
302
303 #ifdef ENABLE_PP
304         memset(&nexell_func_pp, 0, sizeof(nexell_func_pp));
305         nexell_func_pp.pp_destroy = nexell_pp_destroy;
306         nexell_func_pp.pp_set_info = nexell_pp_set_info;
307         nexell_func_pp.pp_attach = nexell_pp_attach;
308         nexell_func_pp.pp_commit = nexell_pp_commit;
309         nexell_func_pp.pp_set_done_handler = nexell_pp_set_done_handler;
310 #endif
311
312         ret = tdm_backend_register_func_display(dpy, &nexell_func_display);
313         if (ret != TDM_ERROR_NONE)
314                 goto failed;
315
316         ret = tdm_backend_register_func_output(dpy, &nexell_func_output);
317         if (ret != TDM_ERROR_NONE)
318                 goto failed;
319
320         ret = tdm_backend_register_func_layer(dpy, &nexell_func_layer);
321         if (ret != TDM_ERROR_NONE)
322                 goto failed;
323
324 #ifdef ENABLE_PP
325         ret = tdm_backend_register_func_pp(dpy, &nexell_func_pp);
326         if (ret != TDM_ERROR_NONE)
327                 goto failed;
328 #endif
329
330         nexell_data->dpy = dpy;
331
332         /* The drm master fd can be opened by a tbm backend module in
333          * tbm_bufmgr_init() time. In this case, we just get it from tbm.
334          */
335         nexell_data->drm_fd = tbm_drm_helper_get_master_fd();
336         if (nexell_data->drm_fd < 0) {
337                 nexell_data->drm_fd = _tdm_nexell_open_drm();
338
339                 if (nexell_data->drm_fd < 0) {
340                         ret = TDM_ERROR_OPERATION_FAILED;
341                         goto failed;
342                 }
343
344                 tbm_drm_helper_set_tbm_master_fd(nexell_data->drm_fd);
345         }
346
347         TDM_INFO("master fd(%d)", nexell_data->drm_fd);
348
349 #ifdef HAVE_UDEV
350         _tdm_nexell_udev_init(nexell_data);
351 #endif
352
353 #if LIBDRM_MAJOR_VERSION >= 2 && LIBDRM_MINOR_VERSION >= 4  && LIBDRM_MICRO_VERSION >= 47
354         if (drmSetClientCap(nexell_data->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) < 0) {
355                 TDM_WRN("Set DRM_CLIENT_CAP_UNIVERSAL_PLANES failed");
356         } else {
357                 TDM_INFO("has universal planes");
358                 nexell_data->has_universal_plane = 1;
359         }
360 #endif
361
362         nexell_data->mode_res = drmModeGetResources(nexell_data->drm_fd);
363         if (!nexell_data->mode_res) {
364                 TDM_ERR("no drm resource: %m");
365                 ret = TDM_ERROR_OPERATION_FAILED;
366                 goto failed;
367         }
368
369         nexell_data->plane_res = drmModeGetPlaneResources(nexell_data->drm_fd);
370         if (!nexell_data->plane_res) {
371                 TDM_ERR("no drm plane resource: %m");
372                 ret = TDM_ERROR_OPERATION_FAILED;
373                 goto failed;
374         }
375
376         if (nexell_data->plane_res->count_planes <= 0) {
377                 TDM_ERR("no drm plane resource");
378                 ret = TDM_ERROR_OPERATION_FAILED;
379                 goto failed;
380         }
381
382         ret = tdm_nexell_display_create_output_list(nexell_data);
383         if (ret != TDM_ERROR_NONE)
384                 goto failed;
385
386         ret = tdm_nexell_display_create_layer_list(nexell_data);
387         if (ret != TDM_ERROR_NONE)
388                 goto failed;
389
390         if (error)
391                 *error = TDM_ERROR_NONE;
392
393         TDM_INFO("init success!");
394
395         return (tdm_backend_data *)nexell_data;
396 failed:
397         if (error)
398                 *error = ret;
399
400         tdm_nexell_deinit(nexell_data);
401
402         TDM_ERR("init failed!");
403         return NULL;
404 }
405
406 tdm_backend_module tdm_backend_module_data = {
407         "drm",
408         "Samsung",
409         TDM_BACKEND_SET_ABI_VERSION(1, 1),
410         tdm_nexell_init,
411         tdm_nexell_deinit
412 };