summaryrefslogtreecommitdiff
path: root/src/wfdport.c
blob: ab0f70e12e22b04310f4e46f1aa124d6ae8f6754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
 * Copyright (c) 2011 Benjamin Franzke
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>

#include "wfhandle.h"
#include "wfdport.h"
#include "wfddevice.h"
#include "wfdpipeline.h"

#include <xf86drmMode.h>

struct wfd_port {
	drmModeConnectorPtr connector;

	int modes_count;
	WFDPortMode *modes;

	uint32_t possible_crtcs;
	drmModeModeInfoPtr mode;

	struct wfd_pipeline *pipeline;
	int pipeline_updated;

	uint32_t choosen_pipeline;

	uint32_t dpms_enum_id;
	WFDint power_mode;
};

WFDint
wfd_enumerate_ports(struct wfd_device *device,
		    WFDint *port_ids,
		    WFDint port_id_count,
		    WFDint *filter_list)
{
	drmModeResPtr resources = wfd_device_get_resources(device);
	int count = 0;
	int i;

	if (port_ids == NULL)
		return resources->count_connectors;	

	for (i = 0; i < resources->count_connectors && count < port_id_count; ++i) {
		port_ids[count] = resources->connectors[i];
		count++;
	}

	return count;
}

static void
port_modes_destroy(struct wfd_device *device,
		   struct wfd_port *port)
{
	int i;

	for (i = 0; i < port->modes_count; ++i)
		wf_handle_destroy(port->modes[i]);
	free(port->modes);
	port->modes = NULL;
}

void
wfd_port_destroy(struct wfd_device *device,
		 struct wfd_port *port)
{
	if (port->choosen_pipeline) {
		uint32_t *crtcs = wfd_device_get_choosen_crtcs(device);
		*crtcs &= (1 << port->choosen_pipeline);
	}

	port_modes_destroy(device, port);
	drmModeFreeConnector(port->connector);
	free(port);
}

static int
port_update_modes(struct wfd_device *device,
		  struct wfd_port *port)
{
	int i;

	if (port->connector->connection != DRM_MODE_CONNECTED)
		return 0;

	if (port->modes)
		port_modes_destroy(device, port);

	port->modes = calloc(port->connector->count_modes,
			     sizeof(WFDPortMode));
	if (port->modes == NULL)
		return -1;

	port->modes_count = port->connector->count_modes;

	for (i = 0; i < port->modes_count; ++i) {
		port->modes[i] =
			wf_handle_create(&port->connector->modes[i],
					 MODE_HANDLE);

	}

	return 0;
}

struct wfd_port *
wfd_port_create(struct wfd_device *device,
		WFDint port_id,
		const WFDint *attrib_list)
{
	struct wfd_port *port;
	int drm_fd = wfd_device_get_fd(device);
	drmModeEncoderPtr encoder;
	drmModePropertyPtr prop;
	int i;

	port = calloc(1, sizeof *port);
	if (port == NULL)
		return NULL;

	port->power_mode = WFD_POWER_MODE_OFF;

	port->connector = drmModeGetConnector(drm_fd, port_id);
	if (port->connector == NULL) {
		free(port);
		return NULL;
	}

	if (port_update_modes(device, port) < 0) {
		free(port->connector);
		free(port);
		return NULL;
	}

	/* intersect encoder's possible_crtc to be sure the crtc we choose
	 * is compatible with all encoders the kernel might choose.
	 * Radeon DDX does this as well, all other KMS based DDX
	 * use the first encoder's possible_crtcs
	 * */
	port->possible_crtcs = ~0;
	for (i = 0; i < port->connector->count_encoders; ++i) {
		encoder = drmModeGetEncoder(drm_fd, port->connector->encoders[i]);
		port->possible_crtcs &= encoder->possible_crtcs;
		drmModeFreeEncoder(encoder);
	}

	port->dpms_enum_id = 0;
	for (i = 0; i < port->connector->count_props; ++i) {
		prop = drmModeGetProperty(drm_fd, port->connector->props[i]);
		if (!prop)
			continue;

		if (prop->flags && DRM_MODE_PROP_ENUM &&
		    strcmp(prop->name, "DPMS") == 0) {
			port->dpms_enum_id = port->connector->props[i];
			drmModeFreeProperty(prop);
			break;
		}

		drmModeFreeProperty(prop);
	}

	port->mode = NULL;

	return port;
}

static uint32_t
wfd_port_choose_pipeline(struct wfd_device *device,
			 struct wfd_port *port)
{
	drmModeResPtr resources = wfd_device_get_resources(device);
	int i;
	uint32_t *crtcs;

	if (port->choosen_pipeline)
		return port->choosen_pipeline;

	crtcs = wfd_device_get_choosen_crtcs(device);

	for (i = 0; i < resources->count_crtcs; ++i) {
		if (port->possible_crtcs & (1 << i) &&
		    !(*crtcs & (1 << resources->crtcs[i]))) {
			*crtcs |= 1 << resources->crtcs[i];
			port->choosen_pipeline = resources->crtcs[i];
			break;
		}
	}

	return port->choosen_pipeline;
}

static WFDint
wfd_port_get_type(struct wfd_device *device,
		  struct wfd_port *port)
{
	switch (port->connector->connector_type){
	case DRM_MODE_CONNECTOR_DVII:
	case DRM_MODE_CONNECTOR_DVID:
	case DRM_MODE_CONNECTOR_DVIA:
		return WFD_PORT_TYPE_DVI;
	case DRM_MODE_CONNECTOR_Composite:
		return WFD_PORT_TYPE_COMPOSITE;
	/* FIXME: request subconnector info for TV */
	case DRM_MODE_CONNECTOR_TV:
	case DRM_MODE_CONNECTOR_SVIDEO:
		return WFD_PORT_TYPE_SVIDEO;
	case DRM_MODE_CONNECTOR_Component:
		return WFD_PORT_TYPE_COMPONENT_YPbPr;
	case DRM_MODE_CONNECTOR_DisplayPort:
		return WFD_PORT_TYPE_DISPLAYPORT;
	case DRM_MODE_CONNECTOR_HDMIA:
	case DRM_MODE_CONNECTOR_HDMIB:
		return WFD_PORT_TYPE_HDMI;
	case DRM_MODE_CONNECTOR_LVDS:
	case DRM_MODE_CONNECTOR_eDP:
		return WFD_PORT_TYPE_INTERNAL;
	/* FIXME: find appriate WFD_PORT types for vga and 9PinDIN */
	case DRM_MODE_CONNECTOR_Unknown:
	case DRM_MODE_CONNECTOR_9PinDIN:
	case DRM_MODE_CONNECTOR_VGA:
	default:
		return WFD_PORT_TYPE_OTHER;
	};
}

WFDint
wfd_port_get_attribi(struct wfd_device *device,
		     struct wfd_port *port,
		     WFDPortConfigAttrib attribute)
{
	switch (attribute) {
	case WFD_PORT_ID:
		return port->connector->connector_id;
	case WFD_PORT_TYPE:
		return wfd_port_get_type(device, port);
	case WFD_PORT_DETACHABLE:
		switch (port->connector->connector_type) {
		case DRM_MODE_CONNECTOR_LVDS:
		case DRM_MODE_CONNECTOR_eDP:
			return WFD_FALSE;
		default:
			return WFD_TRUE;
		}
	case WFD_PORT_ATTACHED:
		return port->connector->connection == DRM_MODE_CONNECTED;
	case WFD_PORT_FILL_PORT_AREA:
		return WFD_TRUE;
	/* no effect as FILL_PORT_AREA = true */
	case WFD_PORT_BACKGROUND_COLOR:
		return 0;
	case WFD_PORT_FLIP:
	case WFD_PORT_MIRROR:
		return WFD_FALSE;
	case WFD_PORT_ROTATION:
		return 0;
	case WFD_PORT_PIPELINE_ID_COUNT:
		return wfd_port_choose_pipeline(device, port) != 0 ? 1 : 0;
	default:
		break;
	}
	
	return 0;
}

static void
wfd_port_get_bindable_pipelines(struct wfd_device *device,
				struct wfd_port *port,
				int count,
				int *value)
{
	uint32_t pipeline;

	if (count != 1)
		return;

	pipeline = wfd_port_choose_pipeline(device, port);

	if (pipeline != 0)
		*value = pipeline;
}

void
wfd_port_get_attribiv(struct wfd_device *device,
		      struct wfd_port *port,
		      WFDPortConfigAttrib attribute,
		      WFDint count,
		      WFDint *value)
{
	switch (attribute) {
	case WFD_PORT_NATIVE_RESOLUTION:
		if (count != 2)
			return;
		if (port->connector->count_modes > 0) {
			value[0] = port->connector->modes[0].hdisplay;
			value[1] = port->connector->modes[0].vdisplay;
		} else {
			value[0] = value[1] = 0;
		}
		break;
	case WFD_PORT_BACKGROUND_COLOR:
		if (count != 3)
			return;
		value[0] = 0;
		break;
	case WFD_PORT_BINDABLE_PIPELINE_IDS:
		wfd_port_get_bindable_pipelines(device, port, count, value);
		break;
	default:
		break;
	}
}

void
wfd_port_get_attribfv(struct wfd_device *device,
		      struct wfd_port *port,
		      WFDPortConfigAttrib attribute,
		      WFDint count,
		      WFDfloat *value)
{
	switch (attribute) {
	case WFD_PORT_PHYSICAL_SIZE:
		if (count != 2)
			return;
		value[0] = (WFDfloat) port->connector->mmWidth;
		value[1] = (WFDfloat) port->connector->mmHeight;
		break;
	case WFD_PORT_BACKGROUND_COLOR:
		if (count != 3)
			return;
		value[0] = value[1] = value[2] = 0.0;
		break;
	default:
		break;
	}
}

void
wfd_port_set_attribi(struct wfd_device *device,
		     struct wfd_port *port,
		     WFDPortConfigAttrib attribute,
		     WFDint value)
{
	switch (attribute) {
	case WFD_PORT_POWER_MODE:
		switch (value) {
		case WFD_POWER_MODE_ON:
		case WFD_POWER_MODE_LIMITED_USE:
		case WFD_POWER_MODE_SUSPEND:
		case WFD_POWER_MODE_OFF:
			break;
		default:
			return;
		}
		port->power_mode = value;
		break;
	/* ignored by WFD_PORT_MODE_{FLIP_MIRROR,ROTATION}_SUPPORT */
	case WFD_PORT_FLIP:
	case WFD_PORT_MIRROR:
	case WFD_PORT_ROTATION:
	/* ignored as FILL_PORT_AREA = true */
	case WFD_PORT_BACKGROUND_COLOR:
	default:
		break;
	}
}

void
wfd_port_set_attribiv(struct wfd_device *device,
		      struct wfd_port *port,
		      WFDPortConfigAttrib attribute,
		      WFDint count,
		      const WFDint *value)
{
	switch (attribute) {
	/* ignored as FILL_PORT_AREA = true */
	case WFD_PORT_BACKGROUND_COLOR:
	default:
		break;
	}
}

void
wfd_port_set_attribfv(struct wfd_device *device,
		      struct wfd_port *port,
		      WFDPortConfigAttrib attribute,
		      WFDint count,
		      const WFDfloat *value)
{
	switch (attribute) {
	/* ignored as FILL_PORT_AREA = true */
	case WFD_PORT_BACKGROUND_COLOR:
	default:
		break;
	}
}

WFDint
wfd_port_get_modes(struct wfd_device *device,
		   struct wfd_port *port,
		   WFDPortMode *modes,
		   WFDint modes_count)
{
	int i;
	int count_modes = port->modes_count;
	int count;

	if (port->connector->connection != DRM_MODE_CONNECTED)
		return 0;

	if (modes == NULL)
		return count_modes;

	for (i = 0, count = 0; i < count_modes && count < modes_count; ++i) {
		modes[count++] = port->modes[i];
	}

	return count;
}

void
wfd_port_set_mode(struct wfd_device *device,
		  struct wfd_port *port,
		  WFDPortMode mode_handle)
{
	drmModeModeInfoPtr mode;
	
	mode = wf_handle_get_object(mode_handle, MODE_HANDLE);
	if (mode == NULL)
		return;

	port->mode = mode;
}

int
wfd_port_mode_get_attribi(struct wfd_device *device,
			  struct wfd_port *port,
			  WFDPortMode mode_handle,
			  WFDPortModeAttrib attrib)
{
	drmModeModeInfoPtr mode;
	
	mode = wf_handle_get_object(mode_handle, MODE_HANDLE);
	if (mode == NULL)
		return 0;

	switch (attrib) {
	case WFD_PORT_MODE_WIDTH:
		return mode->hdisplay;
	case WFD_PORT_MODE_HEIGHT:
		return mode->vdisplay;
	case WFD_PORT_MODE_FLIP_MIRROR_SUPPORT:
		return WFD_FALSE;
	case WFD_PORT_MODE_ROTATION_SUPPORT:
		return WFD_ROTATION_SUPPORT_NONE;
	case WFD_PORT_MODE_INTERLACED:
		return !!(mode->flags & DRM_MODE_FLAG_INTERLACE);

	default:
		break;
	}

	return 0;
}

int
wfd_port_commit(struct wfd_device *device,
		struct wfd_port *port)
{
	int fd = wfd_device_get_fd(device);
	int ret;
	int dest_rect[4];

	wfd_pipeline_commit(device, port->pipeline);

	if (port->pipeline_updated == 0)
		return 0;

	assert(port->mode != NULL);

	/* FIXME: implicit image scale needed if dest_rect width and height
	 * != source_rect width and height? */
	wfd_pipeline_get_attribiv(device, port->pipeline,
				  WFD_PIPELINE_DESTINATION_RECTANGLE,
				  4, dest_rect);

	ret = drmModeSetCrtc(fd,
			     wfd_pipeline_get_crtc_id(port->pipeline),
			     wfd_pipeline_get_fb_id(port->pipeline),
			     dest_rect[0], dest_rect[1],
			     &port->connector->connector_id, 1,
			     port->mode);

	if (ret) {
		fprintf(stderr, "drmModeSetCrtc: %d %m\n", ret);
		return ret;
	}

	uint64_t power_mode;
	switch (port->power_mode) {
	case WFD_POWER_MODE_ON:
		power_mode = DRM_MODE_DPMS_ON;
		break;
	case WFD_POWER_MODE_LIMITED_USE:
		power_mode = DRM_MODE_DPMS_STANDBY;
		break;
	case WFD_POWER_MODE_SUSPEND:
		power_mode = DRM_MODE_DPMS_SUSPEND;
		break;
	case WFD_POWER_MODE_OFF:
		power_mode = DRM_MODE_DPMS_OFF;
		break;
	}

	ret = drmModeConnectorSetProperty(fd, port->connector->connector_id,
					  port->dpms_enum_id, power_mode);
	if (ret) {
		fprintf(stderr, "drmModeconnectorSetProperty: %d %m\n", ret);
		return ret;
	}

	return ret;
}

int
wfd_port_bind_pipeline(struct wfd_device *device,
		       struct wfd_port *port,
		       struct wfd_pipeline *pipeline)
{
	port->pipeline = pipeline;
	port->pipeline_updated = 1;

	return 0;
}

static inline void
pointer_swap(void **p1, void **p2)
{
	void *tmp = *p1;
	
	*p1 = *p2;
	*p2 = tmp;
}

struct id_search {
	uint32_t connector_id;
	void *object;
};

static void
port_search(void *object, void *user_data)
{
	struct wfd_port *port = object;
	struct id_search *search = user_data;

	if (search->connector_id == port->connector->connector_id)
		search->object = object;
}

static struct wfd_port *
id_get_port(int connector_id)
{
	struct id_search search = { connector_id, NULL };

	wf_handle_foreach(PORT_HANDLE,
			  port_search,
			  &search);
	
	return search.object;
}

WFDEventType
wfd_ports_find_attach_detach(struct wfd_device *device,
			     WFDint *port_id,
			     WFDboolean *state)
{
	drmModeResPtr resources = wfd_device_get_resources(device);
	struct wfd_port *port;
	int drm_fd = wfd_device_get_fd(device);
	drmModeConnectorPtr connector;
	int i;
	WFDEventType event_type = WFD_EVENT_NONE;

	for (i = 0; i < resources->count_connectors &&
	     event_type == WFD_EVENT_NONE; ++i) {
		connector = drmModeGetConnector(drm_fd, resources->connectors[i]);
		port = id_get_port(connector->connector_id);

		/* new one? */
		if (port == NULL && connector->connection == DRM_MODE_CONNECTED) {
			*state = WFD_TRUE;
			*port_id = connector->connector_id;
			event_type = WFD_EVENT_PORT_ATTACH_DETACH;
		} else if (port != NULL &&
			   connector->connection == DRM_MODE_DISCONNECTED) {
			*state = WFD_FALSE;
			*port_id = connector->connector_id;
			event_type = WFD_EVENT_PORT_ATTACH_DETACH;

			pointer_swap((void **) &port->connector,
				     (void **) &connector);
		}

		drmModeFreeConnector(connector);
	}

	return event_type;
}