Subversion Repositories Kolibri OS

Rev

Rev 1125 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1123 serge 1
/*
2
 * Copyright (c) 2006-2008 Intel Corporation
3
 * Copyright (c) 2007 Dave Airlie 
4
 * Copyright (c) 2008 Red Hat Inc.
5
 *
6
 * DRM core CRTC related functions
7
 *
8
 * Permission to use, copy, modify, distribute, and sell this software and its
9
 * documentation for any purpose is hereby granted without fee, provided that
10
 * the above copyright notice appear in all copies and that both that copyright
11
 * notice and this permission notice appear in supporting documentation, and
12
 * that the name of the copyright holders not be used in advertising or
13
 * publicity pertaining to distribution of the software without specific,
14
 * written prior permission.  The copyright holders make no representations
15
 * about the suitability of this software for any purpose.  It is provided "as
16
 * is" without express or implied warranty.
17
 *
18
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24
 * OF THIS SOFTWARE.
25
 *
26
 * Authors:
27
 *      Keith Packard
28
 *	Eric Anholt 
29
 *      Dave Airlie 
30
 *      Jesse Barnes 
31
 */
32
#include 
33
#include "drm.h"
34
#include "drmP.h"
35
#include "drm_crtc.h"
36
 
37
struct drm_prop_enum_list {
38
	int type;
39
	char *name;
40
};
41
 
42
/* Avoid boilerplate.  I'm tired of typing. */
43
#define DRM_ENUM_NAME_FN(fnname, list)				\
44
	char *fnname(int val)					\
45
	{							\
46
		int i;						\
47
		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
48
			if (list[i].type == val)		\
49
				return list[i].name;		\
50
		}						\
51
		return "(unknown)";				\
52
	}
53
 
54
/*
55
 * Global properties
56
 */
57
static struct drm_prop_enum_list drm_dpms_enum_list[] =
58
{	{ DRM_MODE_DPMS_ON, "On" },
59
	{ DRM_MODE_DPMS_STANDBY, "Standby" },
60
	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
61
	{ DRM_MODE_DPMS_OFF, "Off" }
62
};
63
 
64
DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
65
 
66
/*
67
 * Optional properties
68
 */
69
static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
70
{
71
	{ DRM_MODE_SCALE_NON_GPU, "Non-GPU" },
72
	{ DRM_MODE_SCALE_FULLSCREEN, "Fullscreen" },
73
	{ DRM_MODE_SCALE_NO_SCALE, "No scale" },
74
	{ DRM_MODE_SCALE_ASPECT, "Aspect" },
75
};
76
 
77
static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
78
{
79
	{ DRM_MODE_DITHERING_OFF, "Off" },
80
	{ DRM_MODE_DITHERING_ON, "On" },
81
};
82
 
83
/*
84
 * Non-global properties, but "required" for certain connectors.
85
 */
86
static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87
{
88
	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89
	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
90
	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
91
};
92
 
93
DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
 
95
static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96
{
97
	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
98
	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
99
	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
100
};
101
 
102
DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103
		 drm_dvi_i_subconnector_enum_list)
104
 
105
static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106
{
107
	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108
	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109
	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
110
	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
111
};
112
 
113
DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
114
 
115
static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
116
{
117
	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
118
	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119
	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
120
	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
121
};
122
 
123
DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
124
		 drm_tv_subconnector_enum_list)
125
 
126
struct drm_conn_prop_enum_list {
127
	int type;
128
	char *name;
129
	int count;
130
};
131
 
132
/*
133
 * Connector and encoder types.
134
 */
135
static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
136
{	{ DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
137
	{ DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
138
	{ DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
139
	{ DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
140
	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
141
	{ DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
142
	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
143
	{ DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
144
	{ DRM_MODE_CONNECTOR_Component, "Component", 0 },
145
	{ DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
146
	{ DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
147
	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
148
	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
149
};
150
 
151
static struct drm_prop_enum_list drm_encoder_enum_list[] =
152
{	{ DRM_MODE_ENCODER_NONE, "None" },
153
	{ DRM_MODE_ENCODER_DAC, "DAC" },
154
	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
155
	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
156
	{ DRM_MODE_ENCODER_TVDAC, "TV" },
157
};
158
 
159
char *drm_get_encoder_name(struct drm_encoder *encoder)
160
{
161
	static char buf[32];
162
 
163
	snprintf(buf, 32, "%s-%d",
164
		 drm_encoder_enum_list[encoder->encoder_type].name,
165
		 encoder->base.id);
166
	return buf;
167
}
168
 
169
char *drm_get_connector_name(struct drm_connector *connector)
170
{
171
	static char buf[32];
172
 
173
	snprintf(buf, 32, "%s-%d",
174
		 drm_connector_enum_list[connector->connector_type].name,
175
		 connector->connector_type_id);
176
	return buf;
177
}
178
EXPORT_SYMBOL(drm_get_connector_name);
179
 
180
char *drm_get_connector_status_name(enum drm_connector_status status)
181
{
182
	if (status == connector_status_connected)
183
		return "connected";
184
	else if (status == connector_status_disconnected)
185
		return "disconnected";
186
	else
187
		return "unknown";
188
}
189
 
190
/**
191
 * drm_mode_object_get - allocate a new identifier
192
 * @dev: DRM device
193
 * @ptr: object pointer, used to generate unique ID
194
 * @type: object type
195
 *
196
 * LOCKING:
197
 *
198
 * Create a unique identifier based on @ptr in @dev's identifier space.  Used
199
 * for tracking modes, CRTCs and connectors.
200
 *
201
 * RETURNS:
202
 * New unique (relative to other objects in @dev) integer identifier for the
203
 * object.
204
 */
205
static int drm_mode_object_get(struct drm_device *dev,
206
			       struct drm_mode_object *obj, uint32_t obj_type)
207
{
208
	int new_id = 0;
209
	int ret;
210
 
211
again:
212
	if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
213
		DRM_ERROR("Ran out memory getting a mode number\n");
214
		return -EINVAL;
215
	}
216
 
217
//   mutex_lock(&dev->mode_config.idr_mutex);
218
	ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
219
//   mutex_unlock(&dev->mode_config.idr_mutex);
220
	if (ret == -EAGAIN)
221
		goto again;
222
 
223
	obj->id = new_id;
224
	obj->type = obj_type;
225
	return 0;
226
}
227
 
228
/**
229
 * drm_mode_object_put - free an identifer
230
 * @dev: DRM device
231
 * @id: ID to free
232
 *
233
 * LOCKING:
234
 * Caller must hold DRM mode_config lock.
235
 *
236
 * Free @id from @dev's unique identifier pool.
237
 */
238
static void drm_mode_object_put(struct drm_device *dev,
239
				struct drm_mode_object *object)
240
{
241
//   mutex_lock(&dev->mode_config.idr_mutex);
242
	idr_remove(&dev->mode_config.crtc_idr, object->id);
243
//   mutex_unlock(&dev->mode_config.idr_mutex);
244
}
245
 
246
void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
247
{
248
	struct drm_mode_object *obj = NULL;
249
 
250
//   mutex_lock(&dev->mode_config.idr_mutex);
251
	obj = idr_find(&dev->mode_config.crtc_idr, id);
252
	if (!obj || (obj->type != type) || (obj->id != id))
253
		obj = NULL;
254
//   mutex_unlock(&dev->mode_config.idr_mutex);
255
 
256
	return obj;
257
}
258
EXPORT_SYMBOL(drm_mode_object_find);
259
 
260
 
261
#if 0
262
 
263
/**
264
 * drm_crtc_from_fb - find the CRTC structure associated with an fb
265
 * @dev: DRM device
266
 * @fb: framebuffer in question
267
 *
268
 * LOCKING:
269
 * Caller must hold mode_config lock.
270
 *
271
 * Find CRTC in the mode_config structure that matches @fb.
272
 *
273
 * RETURNS:
274
 * Pointer to the CRTC or NULL if it wasn't found.
275
 */
276
struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
277
				  struct drm_framebuffer *fb)
278
{
279
	struct drm_crtc *crtc;
280
 
281
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
282
		if (crtc->fb == fb)
283
			return crtc;
284
	}
285
	return NULL;
286
}
287
 
288
/**
289
 * drm_framebuffer_init - initialize a framebuffer
290
 * @dev: DRM device
291
 *
292
 * LOCKING:
293
 * Caller must hold mode config lock.
294
 *
295
 * Allocates an ID for the framebuffer's parent mode object, sets its mode
296
 * functions & device file and adds it to the master fd list.
297
 *
298
 * RETURNS:
299
 * Zero on success, error code on falure.
300
 */
301
int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
302
			 const struct drm_framebuffer_funcs *funcs)
303
{
304
	int ret;
305
 
306
	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
307
	if (ret) {
308
		return ret;
309
	}
310
 
311
	fb->dev = dev;
312
	fb->funcs = funcs;
313
	dev->mode_config.num_fb++;
314
	list_add(&fb->head, &dev->mode_config.fb_list);
315
 
316
	return 0;
317
}
318
EXPORT_SYMBOL(drm_framebuffer_init);
319
 
320
/**
321
 * drm_framebuffer_cleanup - remove a framebuffer object
322
 * @fb: framebuffer to remove
323
 *
324
 * LOCKING:
325
 * Caller must hold mode config lock.
326
 *
327
 * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
328
 * it, setting it to NULL.
329
 */
330
void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
331
{
332
	struct drm_device *dev = fb->dev;
333
	struct drm_crtc *crtc;
334
 
335
	/* remove from any CRTC */
336
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
337
		if (crtc->fb == fb)
338
			crtc->fb = NULL;
339
	}
340
 
341
	drm_mode_object_put(dev, &fb->base);
342
	list_del(&fb->head);
343
	dev->mode_config.num_fb--;
344
}
345
EXPORT_SYMBOL(drm_framebuffer_cleanup);
346
 
347
 
348
#endif
349
 
350
/**
351
 * drm_crtc_init - Initialise a new CRTC object
352
 * @dev: DRM device
353
 * @crtc: CRTC object to init
354
 * @funcs: callbacks for the new CRTC
355
 *
356
 * LOCKING:
357
 * Caller must hold mode config lock.
358
 *
359
 * Inits a new object created as base part of an driver crtc object.
360
 */
361
void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
362
		   const struct drm_crtc_funcs *funcs)
363
{
364
	crtc->dev = dev;
365
	crtc->funcs = funcs;
366
 
367
//   mutex_lock(&dev->mode_config.mutex);
368
	drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
369
 
370
	list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
371
	dev->mode_config.num_crtc++;
372
//   mutex_unlock(&dev->mode_config.mutex);
373
}
374
EXPORT_SYMBOL(drm_crtc_init);
375
 
376
/**
377
 * drm_crtc_cleanup - Cleans up the core crtc usage.
378
 * @crtc: CRTC to cleanup
379
 *
380
 * LOCKING:
381
 * Caller must hold mode config lock.
382
 *
383
 * Cleanup @crtc. Removes from drm modesetting space
384
 * does NOT free object, caller does that.
385
 */
386
void drm_crtc_cleanup(struct drm_crtc *crtc)
387
{
388
	struct drm_device *dev = crtc->dev;
389
 
390
	if (crtc->gamma_store) {
391
		kfree(crtc->gamma_store);
392
		crtc->gamma_store = NULL;
393
	}
394
 
395
	drm_mode_object_put(dev, &crtc->base);
396
	list_del(&crtc->head);
397
	dev->mode_config.num_crtc--;
398
}
399
EXPORT_SYMBOL(drm_crtc_cleanup);
400
 
401
/**
402
 * drm_mode_probed_add - add a mode to a connector's probed mode list
403
 * @connector: connector the new mode
404
 * @mode: mode data
405
 *
406
 * LOCKING:
407
 * Caller must hold mode config lock.
408
 *
409
 * Add @mode to @connector's mode list for later use.
410
 */
411
void drm_mode_probed_add(struct drm_connector *connector,
412
			 struct drm_display_mode *mode)
413
{
414
	list_add(&mode->head, &connector->probed_modes);
415
}
416
EXPORT_SYMBOL(drm_mode_probed_add);
417
 
418
/**
419
 * drm_mode_remove - remove and free a mode
420
 * @connector: connector list to modify
421
 * @mode: mode to remove
422
 *
423
 * LOCKING:
424
 * Caller must hold mode config lock.
425
 *
426
 * Remove @mode from @connector's mode list, then free it.
427
 */
428
void drm_mode_remove(struct drm_connector *connector,
429
		     struct drm_display_mode *mode)
430
{
431
	list_del(&mode->head);
432
	kfree(mode);
433
}
434
EXPORT_SYMBOL(drm_mode_remove);
435
 
436
/**
437
 * drm_connector_init - Init a preallocated connector
438
 * @dev: DRM device
439
 * @connector: the connector to init
440
 * @funcs: callbacks for this connector
441
 * @name: user visible name of the connector
442
 *
443
 * LOCKING:
444
 * Caller must hold @dev's mode_config lock.
445
 *
446
 * Initialises a preallocated connector. Connectors should be
447
 * subclassed as part of driver connector objects.
448
 */
449
void drm_connector_init(struct drm_device *dev,
450
		     struct drm_connector *connector,
451
		     const struct drm_connector_funcs *funcs,
452
		     int connector_type)
453
{
454
//   mutex_lock(&dev->mode_config.mutex);
455
 
456
	connector->dev = dev;
457
	connector->funcs = funcs;
458
	drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
459
	connector->connector_type = connector_type;
460
	connector->connector_type_id =
461
		++drm_connector_enum_list[connector_type].count; /* TODO */
462
	INIT_LIST_HEAD(&connector->user_modes);
463
	INIT_LIST_HEAD(&connector->probed_modes);
464
	INIT_LIST_HEAD(&connector->modes);
465
	connector->edid_blob_ptr = NULL;
466
 
467
	list_add_tail(&connector->head, &dev->mode_config.connector_list);
468
	dev->mode_config.num_connector++;
469
 
470
	drm_connector_attach_property(connector,
471
				      dev->mode_config.edid_property, 0);
472
 
473
	drm_connector_attach_property(connector,
474
				      dev->mode_config.dpms_property, 0);
475
 
476
//   mutex_unlock(&dev->mode_config.mutex);
477
}
478
EXPORT_SYMBOL(drm_connector_init);
479
 
480
/**
481
 * drm_connector_cleanup - cleans up an initialised connector
482
 * @connector: connector to cleanup
483
 *
484
 * LOCKING:
485
 * Caller must hold @dev's mode_config lock.
486
 *
487
 * Cleans up the connector but doesn't free the object.
488
 */
489
void drm_connector_cleanup(struct drm_connector *connector)
490
{
491
	struct drm_device *dev = connector->dev;
492
	struct drm_display_mode *mode, *t;
493
 
494
	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
495
		drm_mode_remove(connector, mode);
496
 
497
	list_for_each_entry_safe(mode, t, &connector->modes, head)
498
		drm_mode_remove(connector, mode);
499
 
500
	list_for_each_entry_safe(mode, t, &connector->user_modes, head)
501
		drm_mode_remove(connector, mode);
502
 
503
//   mutex_lock(&dev->mode_config.mutex);
504
	drm_mode_object_put(dev, &connector->base);
505
	list_del(&connector->head);
506
//   mutex_unlock(&dev->mode_config.mutex);
507
}
508
EXPORT_SYMBOL(drm_connector_cleanup);
509
 
510
void drm_encoder_init(struct drm_device *dev,
511
		      struct drm_encoder *encoder,
512
		      const struct drm_encoder_funcs *funcs,
513
		      int encoder_type)
514
{
515
//   mutex_lock(&dev->mode_config.mutex);
516
 
517
	encoder->dev = dev;
518
 
519
	drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
520
	encoder->encoder_type = encoder_type;
521
	encoder->funcs = funcs;
522
 
523
	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
524
	dev->mode_config.num_encoder++;
525
 
526
//   mutex_unlock(&dev->mode_config.mutex);
527
}
528
EXPORT_SYMBOL(drm_encoder_init);
529
 
530
void drm_encoder_cleanup(struct drm_encoder *encoder)
531
{
532
	struct drm_device *dev = encoder->dev;
533
//   mutex_lock(&dev->mode_config.mutex);
534
	drm_mode_object_put(dev, &encoder->base);
535
	list_del(&encoder->head);
536
//   mutex_unlock(&dev->mode_config.mutex);
537
}
538
EXPORT_SYMBOL(drm_encoder_cleanup);
539
 
540
/**
541
 * drm_mode_create - create a new display mode
542
 * @dev: DRM device
543
 *
544
 * LOCKING:
545
 * Caller must hold DRM mode_config lock.
546
 *
547
 * Create a new drm_display_mode, give it an ID, and return it.
548
 *
549
 * RETURNS:
550
 * Pointer to new mode on success, NULL on error.
551
 */
552
struct drm_display_mode *drm_mode_create(struct drm_device *dev)
553
{
554
	struct drm_display_mode *nmode;
555
 
556
	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
557
	if (!nmode)
558
		return NULL;
559
 
560
	drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
561
	return nmode;
562
}
563
EXPORT_SYMBOL(drm_mode_create);
564
 
565
/**
566
 * drm_mode_destroy - remove a mode
567
 * @dev: DRM device
568
 * @mode: mode to remove
569
 *
570
 * LOCKING:
571
 * Caller must hold mode config lock.
572
 *
573
 * Free @mode's unique identifier, then free it.
574
 */
575
void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
576
{
577
	drm_mode_object_put(dev, &mode->base);
578
 
579
	kfree(mode);
580
}
581
EXPORT_SYMBOL(drm_mode_destroy);
582
 
583
static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
584
{
585
	struct drm_property *edid;
586
	struct drm_property *dpms;
587
	int i;
588
 
589
	/*
590
	 * Standard properties (apply to all connectors)
591
	 */
592
	edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
593
				   DRM_MODE_PROP_IMMUTABLE,
594
				   "EDID", 0);
595
	dev->mode_config.edid_property = edid;
596
 
597
	dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
598
				   "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
599
	for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
600
		drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
601
				      drm_dpms_enum_list[i].name);
602
	dev->mode_config.dpms_property = dpms;
603
 
604
	return 0;
605
}
606
 
607
/**
608
 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
609
 * @dev: DRM device
610
 *
611
 * Called by a driver the first time a DVI-I connector is made.
612
 */
613
int drm_mode_create_dvi_i_properties(struct drm_device *dev)
614
{
615
	struct drm_property *dvi_i_selector;
616
	struct drm_property *dvi_i_subconnector;
617
	int i;
618
 
619
	if (dev->mode_config.dvi_i_select_subconnector_property)
620
		return 0;
621
 
622
	dvi_i_selector =
623
		drm_property_create(dev, DRM_MODE_PROP_ENUM,
624
				    "select subconnector",
625
				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
626
	for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
627
		drm_property_add_enum(dvi_i_selector, i,
628
				      drm_dvi_i_select_enum_list[i].type,
629
				      drm_dvi_i_select_enum_list[i].name);
630
	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
631
 
632
	dvi_i_subconnector =
633
		drm_property_create(dev, DRM_MODE_PROP_ENUM |
634
				    DRM_MODE_PROP_IMMUTABLE,
635
				    "subconnector",
636
				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
637
	for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
638
		drm_property_add_enum(dvi_i_subconnector, i,
639
				      drm_dvi_i_subconnector_enum_list[i].type,
640
				      drm_dvi_i_subconnector_enum_list[i].name);
641
	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
642
 
643
	return 0;
644
}
645
EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
646
 
647
/**
648
 * drm_create_tv_properties - create TV specific connector properties
649
 * @dev: DRM device
650
 * @num_modes: number of different TV formats (modes) supported
651
 * @modes: array of pointers to strings containing name of each format
652
 *
653
 * Called by a driver's TV initialization routine, this function creates
654
 * the TV specific connector properties for a given device.  Caller is
655
 * responsible for allocating a list of format names and passing them to
656
 * this routine.
657
 */
658
int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
659
				  char *modes[])
660
{
661
	struct drm_property *tv_selector;
662
	struct drm_property *tv_subconnector;
663
	int i;
664
 
665
	if (dev->mode_config.tv_select_subconnector_property)
666
		return 0;
667
 
668
	/*
669
	 * Basic connector properties
670
	 */
671
	tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
672
					  "select subconnector",
673
					  ARRAY_SIZE(drm_tv_select_enum_list));
674
	for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
675
		drm_property_add_enum(tv_selector, i,
676
				      drm_tv_select_enum_list[i].type,
677
				      drm_tv_select_enum_list[i].name);
678
	dev->mode_config.tv_select_subconnector_property = tv_selector;
679
 
680
	tv_subconnector =
681
		drm_property_create(dev, DRM_MODE_PROP_ENUM |
682
				    DRM_MODE_PROP_IMMUTABLE, "subconnector",
683
				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
684
	for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
685
		drm_property_add_enum(tv_subconnector, i,
686
				      drm_tv_subconnector_enum_list[i].type,
687
				      drm_tv_subconnector_enum_list[i].name);
688
	dev->mode_config.tv_subconnector_property = tv_subconnector;
689
 
690
	/*
691
	 * Other, TV specific properties: margins & TV modes.
692
	 */
693
	dev->mode_config.tv_left_margin_property =
694
		drm_property_create(dev, DRM_MODE_PROP_RANGE,
695
				    "left margin", 2);
696
	dev->mode_config.tv_left_margin_property->values[0] = 0;
697
	dev->mode_config.tv_left_margin_property->values[1] = 100;
698
 
699
	dev->mode_config.tv_right_margin_property =
700
		drm_property_create(dev, DRM_MODE_PROP_RANGE,
701
				    "right margin", 2);
702
	dev->mode_config.tv_right_margin_property->values[0] = 0;
703
	dev->mode_config.tv_right_margin_property->values[1] = 100;
704
 
705
	dev->mode_config.tv_top_margin_property =
706
		drm_property_create(dev, DRM_MODE_PROP_RANGE,
707
				    "top margin", 2);
708
	dev->mode_config.tv_top_margin_property->values[0] = 0;
709
	dev->mode_config.tv_top_margin_property->values[1] = 100;
710
 
711
	dev->mode_config.tv_bottom_margin_property =
712
		drm_property_create(dev, DRM_MODE_PROP_RANGE,
713
				    "bottom margin", 2);
714
	dev->mode_config.tv_bottom_margin_property->values[0] = 0;
715
	dev->mode_config.tv_bottom_margin_property->values[1] = 100;
716
 
717
	dev->mode_config.tv_mode_property =
718
		drm_property_create(dev, DRM_MODE_PROP_ENUM,
719
				    "mode", num_modes);
720
	for (i = 0; i < num_modes; i++)
721
		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
722
				      i, modes[i]);
723
 
724
	return 0;
725
}
726
EXPORT_SYMBOL(drm_mode_create_tv_properties);
727
 
728
/**
729
 * drm_mode_create_scaling_mode_property - create scaling mode property
730
 * @dev: DRM device
731
 *
732
 * Called by a driver the first time it's needed, must be attached to desired
733
 * connectors.
734
 */
735
int drm_mode_create_scaling_mode_property(struct drm_device *dev)
736
{
737
	struct drm_property *scaling_mode;
738
	int i;
739
 
740
	if (dev->mode_config.scaling_mode_property)
741
		return 0;
742
 
743
	scaling_mode =
744
		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
745
				    ARRAY_SIZE(drm_scaling_mode_enum_list));
746
	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
747
		drm_property_add_enum(scaling_mode, i,
748
				      drm_scaling_mode_enum_list[i].type,
749
				      drm_scaling_mode_enum_list[i].name);
750
 
751
	dev->mode_config.scaling_mode_property = scaling_mode;
752
 
753
	return 0;
754
}
755
EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
756
 
757
/**
758
 * drm_mode_create_dithering_property - create dithering property
759
 * @dev: DRM device
760
 *
761
 * Called by a driver the first time it's needed, must be attached to desired
762
 * connectors.
763
 */
764
int drm_mode_create_dithering_property(struct drm_device *dev)
765
{
766
	struct drm_property *dithering_mode;
767
	int i;
768
 
769
	if (dev->mode_config.dithering_mode_property)
770
		return 0;
771
 
772
	dithering_mode =
773
		drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
774
				    ARRAY_SIZE(drm_dithering_mode_enum_list));
775
	for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
776
		drm_property_add_enum(dithering_mode, i,
777
				      drm_dithering_mode_enum_list[i].type,
778
				      drm_dithering_mode_enum_list[i].name);
779
	dev->mode_config.dithering_mode_property = dithering_mode;
780
 
781
	return 0;
782
}
783
EXPORT_SYMBOL(drm_mode_create_dithering_property);
784
 
785
/**
786
 * drm_mode_config_init - initialize DRM mode_configuration structure
787
 * @dev: DRM device
788
 *
789
 * LOCKING:
790
 * None, should happen single threaded at init time.
791
 *
792
 * Initialize @dev's mode_config structure, used for tracking the graphics
793
 * configuration of @dev.
794
 */
795
void drm_mode_config_init(struct drm_device *dev)
796
{
797
//   mutex_init(&dev->mode_config.mutex);
798
//   mutex_init(&dev->mode_config.idr_mutex);
799
	INIT_LIST_HEAD(&dev->mode_config.fb_list);
800
	INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
801
	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
802
	INIT_LIST_HEAD(&dev->mode_config.connector_list);
803
	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
804
	INIT_LIST_HEAD(&dev->mode_config.property_list);
805
	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
806
	idr_init(&dev->mode_config.crtc_idr);
807
 
808
//   mutex_lock(&dev->mode_config.mutex);
809
	drm_mode_create_standard_connector_properties(dev);
810
//   mutex_unlock(&dev->mode_config.mutex);
811
 
812
	/* Just to be sure */
813
	dev->mode_config.num_fb = 0;
814
	dev->mode_config.num_connector = 0;
815
	dev->mode_config.num_crtc = 0;
816
	dev->mode_config.num_encoder = 0;
817
}
818
EXPORT_SYMBOL(drm_mode_config_init);
819
 
820
int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
821
{
822
	uint32_t total_objects = 0;
823
 
824
	total_objects += dev->mode_config.num_crtc;
825
	total_objects += dev->mode_config.num_connector;
826
	total_objects += dev->mode_config.num_encoder;
827
 
828
	if (total_objects == 0)
829
		return -EINVAL;
830
 
831
	group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
832
	if (!group->id_list)
833
		return -ENOMEM;
834
 
835
	group->num_crtcs = 0;
836
	group->num_connectors = 0;
837
	group->num_encoders = 0;
838
	return 0;
839
}
840
 
841
int drm_mode_group_init_legacy_group(struct drm_device *dev,
842
				     struct drm_mode_group *group)
843
{
844
	struct drm_crtc *crtc;
845
	struct drm_encoder *encoder;
846
	struct drm_connector *connector;
847
	int ret;
848
 
849
	if ((ret = drm_mode_group_init(dev, group)))
850
		return ret;
851
 
852
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
853
		group->id_list[group->num_crtcs++] = crtc->base.id;
854
 
855
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
856
		group->id_list[group->num_crtcs + group->num_encoders++] =
857
		encoder->base.id;
858
 
859
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
860
		group->id_list[group->num_crtcs + group->num_encoders +
861
			       group->num_connectors++] = connector->base.id;
862
 
863
	return 0;
864
}
865
 
866
/**
867
 * drm_mode_config_cleanup - free up DRM mode_config info
868
 * @dev: DRM device
869
 *
870
 * LOCKING:
871
 * Caller must hold mode config lock.
872
 *
873
 * Free up all the connectors and CRTCs associated with this DRM device, then
874
 * free up the framebuffers and associated buffer objects.
875
 *
876
 * FIXME: cleanup any dangling user buffer objects too
877
 */
878
void drm_mode_config_cleanup(struct drm_device *dev)
879
{
880
	struct drm_connector *connector, *ot;
881
	struct drm_crtc *crtc, *ct;
882
	struct drm_encoder *encoder, *enct;
883
	struct drm_framebuffer *fb, *fbt;
884
	struct drm_property *property, *pt;
885
 
886
	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
887
				 head) {
888
		encoder->funcs->destroy(encoder);
889
	}
890
 
891
	list_for_each_entry_safe(connector, ot,
892
				 &dev->mode_config.connector_list, head) {
893
		connector->funcs->destroy(connector);
894
	}
895
 
896
	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
897
				 head) {
898
		drm_property_destroy(dev, property);
899
	}
900
 
901
	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
902
		fb->funcs->destroy(fb);
903
	}
904
 
905
	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
906
		crtc->funcs->destroy(crtc);
907
	}
908
 
909
}
910
EXPORT_SYMBOL(drm_mode_config_cleanup);
911
 
912
/**
913
 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
914
 * @out: drm_mode_modeinfo struct to return to the user
915
 * @in: drm_display_mode to use
916
 *
917
 * LOCKING:
918
 * None.
919
 *
920
 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
921
 * the user.
922
 */
923
void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
924
			       struct drm_display_mode *in)
925
{
926
	out->clock = in->clock;
927
	out->hdisplay = in->hdisplay;
928
	out->hsync_start = in->hsync_start;
929
	out->hsync_end = in->hsync_end;
930
	out->htotal = in->htotal;
931
	out->hskew = in->hskew;
932
	out->vdisplay = in->vdisplay;
933
	out->vsync_start = in->vsync_start;
934
	out->vsync_end = in->vsync_end;
935
	out->vtotal = in->vtotal;
936
	out->vscan = in->vscan;
937
	out->vrefresh = in->vrefresh;
938
	out->flags = in->flags;
939
	out->type = in->type;
940
	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
941
	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
942
}
943
 
944
/**
945
 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
946
 * @out: drm_display_mode to return to the user
947
 * @in: drm_mode_modeinfo to use
948
 *
949
 * LOCKING:
950
 * None.
951
 *
952
 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
953
 * the caller.
954
 */
955
void drm_crtc_convert_umode(struct drm_display_mode *out,
956
			    struct drm_mode_modeinfo *in)
957
{
958
	out->clock = in->clock;
959
	out->hdisplay = in->hdisplay;
960
	out->hsync_start = in->hsync_start;
961
	out->hsync_end = in->hsync_end;
962
	out->htotal = in->htotal;
963
	out->hskew = in->hskew;
964
	out->vdisplay = in->vdisplay;
965
	out->vsync_start = in->vsync_start;
966
	out->vsync_end = in->vsync_end;
967
	out->vtotal = in->vtotal;
968
	out->vscan = in->vscan;
969
	out->vrefresh = in->vrefresh;
970
	out->flags = in->flags;
971
	out->type = in->type;
972
	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
973
	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
974
}
975
 
976
 
977
#if 0
978
/**
979
 * drm_mode_getresources - get graphics configuration
980
 * @inode: inode from the ioctl
981
 * @filp: file * from the ioctl
982
 * @cmd: cmd from ioctl
983
 * @arg: arg from ioctl
984
 *
985
 * LOCKING:
986
 * Takes mode config lock.
987
 *
988
 * Construct a set of configuration description structures and return
989
 * them to the user, including CRTC, connector and framebuffer configuration.
990
 *
991
 * Called by the user via ioctl.
992
 *
993
 * RETURNS:
994
 * Zero on success, errno on failure.
995
 */
996
int drm_mode_getresources(struct drm_device *dev, void *data,
997
			  struct drm_file *file_priv)
998
{
999
	struct drm_mode_card_res *card_res = data;
1000
	struct list_head *lh;
1001
	struct drm_framebuffer *fb;
1002
	struct drm_connector *connector;
1003
	struct drm_crtc *crtc;
1004
	struct drm_encoder *encoder;
1005
	int ret = 0;
1006
	int connector_count = 0;
1007
	int crtc_count = 0;
1008
	int fb_count = 0;
1009
	int encoder_count = 0;
1010
	int copied = 0, i;
1011
	uint32_t __user *fb_id;
1012
	uint32_t __user *crtc_id;
1013
	uint32_t __user *connector_id;
1014
	uint32_t __user *encoder_id;
1015
	struct drm_mode_group *mode_group;
1016
 
1017
	mutex_lock(&dev->mode_config.mutex);
1018
 
1019
	/*
1020
	 * For the non-control nodes we need to limit the list of resources
1021
	 * by IDs in the group list for this node
1022
	 */
1023
	list_for_each(lh, &file_priv->fbs)
1024
		fb_count++;
1025
 
1026
	mode_group = &file_priv->master->minor->mode_group;
1027
	if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1028
 
1029
		list_for_each(lh, &dev->mode_config.crtc_list)
1030
			crtc_count++;
1031
 
1032
		list_for_each(lh, &dev->mode_config.connector_list)
1033
			connector_count++;
1034
 
1035
		list_for_each(lh, &dev->mode_config.encoder_list)
1036
			encoder_count++;
1037
	} else {
1038
 
1039
		crtc_count = mode_group->num_crtcs;
1040
		connector_count = mode_group->num_connectors;
1041
		encoder_count = mode_group->num_encoders;
1042
	}
1043
 
1044
	card_res->max_height = dev->mode_config.max_height;
1045
	card_res->min_height = dev->mode_config.min_height;
1046
	card_res->max_width = dev->mode_config.max_width;
1047
	card_res->min_width = dev->mode_config.min_width;
1048
 
1049
	/* handle this in 4 parts */
1050
	/* FBs */
1051
	if (card_res->count_fbs >= fb_count) {
1052
		copied = 0;
1053
		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1054
		list_for_each_entry(fb, &file_priv->fbs, head) {
1055
			if (put_user(fb->base.id, fb_id + copied)) {
1056
				ret = -EFAULT;
1057
				goto out;
1058
			}
1059
			copied++;
1060
		}
1061
	}
1062
	card_res->count_fbs = fb_count;
1063
 
1064
	/* CRTCs */
1065
	if (card_res->count_crtcs >= crtc_count) {
1066
		copied = 0;
1067
		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1068
		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1069
			list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1070
					    head) {
1071
				DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
1072
				if (put_user(crtc->base.id, crtc_id + copied)) {
1073
					ret = -EFAULT;
1074
					goto out;
1075
				}
1076
				copied++;
1077
			}
1078
		} else {
1079
			for (i = 0; i < mode_group->num_crtcs; i++) {
1080
				if (put_user(mode_group->id_list[i],
1081
					     crtc_id + copied)) {
1082
					ret = -EFAULT;
1083
					goto out;
1084
				}
1085
				copied++;
1086
			}
1087
		}
1088
	}
1089
	card_res->count_crtcs = crtc_count;
1090
 
1091
	/* Encoders */
1092
	if (card_res->count_encoders >= encoder_count) {
1093
		copied = 0;
1094
		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1095
		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1096
			list_for_each_entry(encoder,
1097
					    &dev->mode_config.encoder_list,
1098
					    head) {
1099
				DRM_DEBUG("ENCODER ID is %d\n",
1100
					  encoder->base.id);
1101
				if (put_user(encoder->base.id, encoder_id +
1102
					     copied)) {
1103
					ret = -EFAULT;
1104
					goto out;
1105
				}
1106
				copied++;
1107
			}
1108
		} else {
1109
			for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1110
				if (put_user(mode_group->id_list[i],
1111
					     encoder_id + copied)) {
1112
					ret = -EFAULT;
1113
					goto out;
1114
				}
1115
				copied++;
1116
			}
1117
 
1118
		}
1119
	}
1120
	card_res->count_encoders = encoder_count;
1121
 
1122
	/* Connectors */
1123
	if (card_res->count_connectors >= connector_count) {
1124
		copied = 0;
1125
		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1126
		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1127
			list_for_each_entry(connector,
1128
					    &dev->mode_config.connector_list,
1129
					    head) {
1130
				DRM_DEBUG("CONNECTOR ID is %d\n",
1131
					  connector->base.id);
1132
				if (put_user(connector->base.id,
1133
					     connector_id + copied)) {
1134
					ret = -EFAULT;
1135
					goto out;
1136
				}
1137
				copied++;
1138
			}
1139
		} else {
1140
			int start = mode_group->num_crtcs +
1141
				mode_group->num_encoders;
1142
			for (i = start; i < start + mode_group->num_connectors; i++) {
1143
				if (put_user(mode_group->id_list[i],
1144
					     connector_id + copied)) {
1145
					ret = -EFAULT;
1146
					goto out;
1147
				}
1148
				copied++;
1149
			}
1150
		}
1151
	}
1152
	card_res->count_connectors = connector_count;
1153
 
1154
	DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1155
		  card_res->count_connectors, card_res->count_encoders);
1156
 
1157
out:
1158
	mutex_unlock(&dev->mode_config.mutex);
1159
	return ret;
1160
}
1161
 
1162
 
1163
/**
1164
 * drm_mode_getcrtc - get CRTC configuration
1165
 * @inode: inode from the ioctl
1166
 * @filp: file * from the ioctl
1167
 * @cmd: cmd from ioctl
1168
 * @arg: arg from ioctl
1169
 *
1170
 * LOCKING:
1171
 * Caller? (FIXME)
1172
 *
1173
 * Construct a CRTC configuration structure to return to the user.
1174
 *
1175
 * Called by the user via ioctl.
1176
 *
1177
 * RETURNS:
1178
 * Zero on success, errno on failure.
1179
 */
1180
int drm_mode_getcrtc(struct drm_device *dev,
1181
		     void *data, struct drm_file *file_priv)
1182
{
1183
	struct drm_mode_crtc *crtc_resp = data;
1184
	struct drm_crtc *crtc;
1185
	struct drm_mode_object *obj;
1186
	int ret = 0;
1187
 
1188
	mutex_lock(&dev->mode_config.mutex);
1189
 
1190
	obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1191
				   DRM_MODE_OBJECT_CRTC);
1192
	if (!obj) {
1193
		ret = -EINVAL;
1194
		goto out;
1195
	}
1196
	crtc = obj_to_crtc(obj);
1197
 
1198
	crtc_resp->x = crtc->x;
1199
	crtc_resp->y = crtc->y;
1200
	crtc_resp->gamma_size = crtc->gamma_size;
1201
	if (crtc->fb)
1202
		crtc_resp->fb_id = crtc->fb->base.id;
1203
	else
1204
		crtc_resp->fb_id = 0;
1205
 
1206
	if (crtc->enabled) {
1207
 
1208
		drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1209
		crtc_resp->mode_valid = 1;
1210
 
1211
	} else {
1212
		crtc_resp->mode_valid = 0;
1213
	}
1214
 
1215
out:
1216
	mutex_unlock(&dev->mode_config.mutex);
1217
	return ret;
1218
}
1219
 
1220
/**
1221
 * drm_mode_getconnector - get connector configuration
1222
 * @inode: inode from the ioctl
1223
 * @filp: file * from the ioctl
1224
 * @cmd: cmd from ioctl
1225
 * @arg: arg from ioctl
1226
 *
1227
 * LOCKING:
1228
 * Caller? (FIXME)
1229
 *
1230
 * Construct a connector configuration structure to return to the user.
1231
 *
1232
 * Called by the user via ioctl.
1233
 *
1234
 * RETURNS:
1235
 * Zero on success, errno on failure.
1236
 */
1237
int drm_mode_getconnector(struct drm_device *dev, void *data,
1238
			  struct drm_file *file_priv)
1239
{
1240
	struct drm_mode_get_connector *out_resp = data;
1241
	struct drm_mode_object *obj;
1242
	struct drm_connector *connector;
1243
	struct drm_display_mode *mode;
1244
	int mode_count = 0;
1245
	int props_count = 0;
1246
	int encoders_count = 0;
1247
	int ret = 0;
1248
	int copied = 0;
1249
	int i;
1250
	struct drm_mode_modeinfo u_mode;
1251
	struct drm_mode_modeinfo __user *mode_ptr;
1252
	uint32_t __user *prop_ptr;
1253
	uint64_t __user *prop_values;
1254
	uint32_t __user *encoder_ptr;
1255
 
1256
	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1257
 
1258
	DRM_DEBUG("connector id %d:\n", out_resp->connector_id);
1259
 
1260
	mutex_lock(&dev->mode_config.mutex);
1261
 
1262
	obj = drm_mode_object_find(dev, out_resp->connector_id,
1263
				   DRM_MODE_OBJECT_CONNECTOR);
1264
	if (!obj) {
1265
		ret = -EINVAL;
1266
		goto out;
1267
	}
1268
	connector = obj_to_connector(obj);
1269
 
1270
	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1271
		if (connector->property_ids[i] != 0) {
1272
			props_count++;
1273
		}
1274
	}
1275
 
1276
	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1277
		if (connector->encoder_ids[i] != 0) {
1278
			encoders_count++;
1279
		}
1280
	}
1281
 
1282
	if (out_resp->count_modes == 0) {
1283
		connector->funcs->fill_modes(connector,
1284
					     dev->mode_config.max_width,
1285
					     dev->mode_config.max_height);
1286
	}
1287
 
1288
	/* delayed so we get modes regardless of pre-fill_modes state */
1289
	list_for_each_entry(mode, &connector->modes, head)
1290
		mode_count++;
1291
 
1292
	out_resp->connector_id = connector->base.id;
1293
	out_resp->connector_type = connector->connector_type;
1294
	out_resp->connector_type_id = connector->connector_type_id;
1295
	out_resp->mm_width = connector->display_info.width_mm;
1296
	out_resp->mm_height = connector->display_info.height_mm;
1297
	out_resp->subpixel = connector->display_info.subpixel_order;
1298
	out_resp->connection = connector->status;
1299
	if (connector->encoder)
1300
		out_resp->encoder_id = connector->encoder->base.id;
1301
	else
1302
		out_resp->encoder_id = 0;
1303
 
1304
	/*
1305
	 * This ioctl is called twice, once to determine how much space is
1306
	 * needed, and the 2nd time to fill it.
1307
	 */
1308
	if ((out_resp->count_modes >= mode_count) && mode_count) {
1309
		copied = 0;
1310
		mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1311
		list_for_each_entry(mode, &connector->modes, head) {
1312
			drm_crtc_convert_to_umode(&u_mode, mode);
1313
			if (copy_to_user(mode_ptr + copied,
1314
					 &u_mode, sizeof(u_mode))) {
1315
				ret = -EFAULT;
1316
				goto out;
1317
			}
1318
			copied++;
1319
		}
1320
	}
1321
	out_resp->count_modes = mode_count;
1322
 
1323
	if ((out_resp->count_props >= props_count) && props_count) {
1324
		copied = 0;
1325
		prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1326
		prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1327
		for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1328
			if (connector->property_ids[i] != 0) {
1329
				if (put_user(connector->property_ids[i],
1330
					     prop_ptr + copied)) {
1331
					ret = -EFAULT;
1332
					goto out;
1333
				}
1334
 
1335
				if (put_user(connector->property_values[i],
1336
					     prop_values + copied)) {
1337
					ret = -EFAULT;
1338
					goto out;
1339
				}
1340
				copied++;
1341
			}
1342
		}
1343
	}
1344
	out_resp->count_props = props_count;
1345
 
1346
	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1347
		copied = 0;
1348
		encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1349
		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1350
			if (connector->encoder_ids[i] != 0) {
1351
				if (put_user(connector->encoder_ids[i],
1352
					     encoder_ptr + copied)) {
1353
					ret = -EFAULT;
1354
					goto out;
1355
				}
1356
				copied++;
1357
			}
1358
		}
1359
	}
1360
	out_resp->count_encoders = encoders_count;
1361
 
1362
out:
1363
	mutex_unlock(&dev->mode_config.mutex);
1364
	return ret;
1365
}
1366
 
1367
int drm_mode_getencoder(struct drm_device *dev, void *data,
1368
			struct drm_file *file_priv)
1369
{
1370
	struct drm_mode_get_encoder *enc_resp = data;
1371
	struct drm_mode_object *obj;
1372
	struct drm_encoder *encoder;
1373
	int ret = 0;
1374
 
1375
	mutex_lock(&dev->mode_config.mutex);
1376
	obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1377
				   DRM_MODE_OBJECT_ENCODER);
1378
	if (!obj) {
1379
		ret = -EINVAL;
1380
		goto out;
1381
	}
1382
	encoder = obj_to_encoder(obj);
1383
 
1384
	if (encoder->crtc)
1385
		enc_resp->crtc_id = encoder->crtc->base.id;
1386
	else
1387
		enc_resp->crtc_id = 0;
1388
	enc_resp->encoder_type = encoder->encoder_type;
1389
	enc_resp->encoder_id = encoder->base.id;
1390
	enc_resp->possible_crtcs = encoder->possible_crtcs;
1391
	enc_resp->possible_clones = encoder->possible_clones;
1392
 
1393
out:
1394
	mutex_unlock(&dev->mode_config.mutex);
1395
	return ret;
1396
}
1397
 
1398
/**
1399
 * drm_mode_setcrtc - set CRTC configuration
1400
 * @inode: inode from the ioctl
1401
 * @filp: file * from the ioctl
1402
 * @cmd: cmd from ioctl
1403
 * @arg: arg from ioctl
1404
 *
1405
 * LOCKING:
1406
 * Caller? (FIXME)
1407
 *
1408
 * Build a new CRTC configuration based on user request.
1409
 *
1410
 * Called by the user via ioctl.
1411
 *
1412
 * RETURNS:
1413
 * Zero on success, errno on failure.
1414
 */
1415
int drm_mode_setcrtc(struct drm_device *dev, void *data,
1416
		     struct drm_file *file_priv)
1417
{
1418
	struct drm_mode_config *config = &dev->mode_config;
1419
	struct drm_mode_crtc *crtc_req = data;
1420
	struct drm_mode_object *obj;
1421
	struct drm_crtc *crtc, *crtcfb;
1422
	struct drm_connector **connector_set = NULL, *connector;
1423
	struct drm_framebuffer *fb = NULL;
1424
	struct drm_display_mode *mode = NULL;
1425
	struct drm_mode_set set;
1426
	uint32_t __user *set_connectors_ptr;
1427
	int ret = 0;
1428
	int i;
1429
 
1430
	mutex_lock(&dev->mode_config.mutex);
1431
	obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1432
				   DRM_MODE_OBJECT_CRTC);
1433
	if (!obj) {
1434
		DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1435
		ret = -EINVAL;
1436
		goto out;
1437
	}
1438
	crtc = obj_to_crtc(obj);
1439
 
1440
	if (crtc_req->mode_valid) {
1441
		/* If we have a mode we need a framebuffer. */
1442
		/* If we pass -1, set the mode with the currently bound fb */
1443
		if (crtc_req->fb_id == -1) {
1444
			list_for_each_entry(crtcfb,
1445
					    &dev->mode_config.crtc_list, head) {
1446
				if (crtcfb == crtc) {
1447
					DRM_DEBUG("Using current fb for setmode\n");
1448
					fb = crtc->fb;
1449
				}
1450
			}
1451
		} else {
1452
			obj = drm_mode_object_find(dev, crtc_req->fb_id,
1453
						   DRM_MODE_OBJECT_FB);
1454
			if (!obj) {
1455
				DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1456
				ret = -EINVAL;
1457
				goto out;
1458
			}
1459
			fb = obj_to_fb(obj);
1460
		}
1461
 
1462
		mode = drm_mode_create(dev);
1463
		drm_crtc_convert_umode(mode, &crtc_req->mode);
1464
		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1465
	}
1466
 
1467
	if (crtc_req->count_connectors == 0 && mode) {
1468
		DRM_DEBUG("Count connectors is 0 but mode set\n");
1469
		ret = -EINVAL;
1470
		goto out;
1471
	}
1472
 
1473
	if (crtc_req->count_connectors > 0 && !mode && !fb) {
1474
		DRM_DEBUG("Count connectors is %d but no mode or fb set\n",
1475
			  crtc_req->count_connectors);
1476
		ret = -EINVAL;
1477
		goto out;
1478
	}
1479
 
1480
	if (crtc_req->count_connectors > 0) {
1481
		u32 out_id;
1482
 
1483
		/* Avoid unbounded kernel memory allocation */
1484
		if (crtc_req->count_connectors > config->num_connector) {
1485
			ret = -EINVAL;
1486
			goto out;
1487
		}
1488
 
1489
		connector_set = kmalloc(crtc_req->count_connectors *
1490
					sizeof(struct drm_connector *),
1491
					GFP_KERNEL);
1492
		if (!connector_set) {
1493
			ret = -ENOMEM;
1494
			goto out;
1495
		}
1496
 
1497
		for (i = 0; i < crtc_req->count_connectors; i++) {
1498
			set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1499
			if (get_user(out_id, &set_connectors_ptr[i])) {
1500
				ret = -EFAULT;
1501
				goto out;
1502
			}
1503
 
1504
			obj = drm_mode_object_find(dev, out_id,
1505
						   DRM_MODE_OBJECT_CONNECTOR);
1506
			if (!obj) {
1507
				DRM_DEBUG("Connector id %d unknown\n", out_id);
1508
				ret = -EINVAL;
1509
				goto out;
1510
			}
1511
			connector = obj_to_connector(obj);
1512
 
1513
			connector_set[i] = connector;
1514
		}
1515
	}
1516
 
1517
	set.crtc = crtc;
1518
	set.x = crtc_req->x;
1519
	set.y = crtc_req->y;
1520
	set.mode = mode;
1521
	set.connectors = connector_set;
1522
	set.num_connectors = crtc_req->count_connectors;
1523
	set.fb =fb;
1524
	ret = crtc->funcs->set_config(&set);
1525
 
1526
out:
1527
	kfree(connector_set);
1528
	mutex_unlock(&dev->mode_config.mutex);
1529
	return ret;
1530
}
1531
 
1532
int drm_mode_cursor_ioctl(struct drm_device *dev,
1533
			void *data, struct drm_file *file_priv)
1534
{
1535
	struct drm_mode_cursor *req = data;
1536
	struct drm_mode_object *obj;
1537
	struct drm_crtc *crtc;
1538
	int ret = 0;
1539
 
1540
	DRM_DEBUG("\n");
1541
 
1542
	if (!req->flags) {
1543
		DRM_ERROR("no operation set\n");
1544
		return -EINVAL;
1545
	}
1546
 
1547
	mutex_lock(&dev->mode_config.mutex);
1548
	obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1549
	if (!obj) {
1550
		DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc_id);
1551
		ret = -EINVAL;
1552
		goto out;
1553
	}
1554
	crtc = obj_to_crtc(obj);
1555
 
1556
	if (req->flags & DRM_MODE_CURSOR_BO) {
1557
		if (!crtc->funcs->cursor_set) {
1558
			DRM_ERROR("crtc does not support cursor\n");
1559
			ret = -ENXIO;
1560
			goto out;
1561
		}
1562
		/* Turns off the cursor if handle is 0 */
1563
		ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1564
					      req->width, req->height);
1565
	}
1566
 
1567
	if (req->flags & DRM_MODE_CURSOR_MOVE) {
1568
		if (crtc->funcs->cursor_move) {
1569
			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1570
		} else {
1571
			DRM_ERROR("crtc does not support cursor\n");
1572
			ret = -EFAULT;
1573
			goto out;
1574
		}
1575
	}
1576
out:
1577
	mutex_unlock(&dev->mode_config.mutex);
1578
	return ret;
1579
}
1580
 
1581
/**
1582
 * drm_mode_addfb - add an FB to the graphics configuration
1583
 * @inode: inode from the ioctl
1584
 * @filp: file * from the ioctl
1585
 * @cmd: cmd from ioctl
1586
 * @arg: arg from ioctl
1587
 *
1588
 * LOCKING:
1589
 * Takes mode config lock.
1590
 *
1591
 * Add a new FB to the specified CRTC, given a user request.
1592
 *
1593
 * Called by the user via ioctl.
1594
 *
1595
 * RETURNS:
1596
 * Zero on success, errno on failure.
1597
 */
1598
int drm_mode_addfb(struct drm_device *dev,
1599
		   void *data, struct drm_file *file_priv)
1600
{
1601
	struct drm_mode_fb_cmd *r = data;
1602
	struct drm_mode_config *config = &dev->mode_config;
1603
	struct drm_framebuffer *fb;
1604
	int ret = 0;
1605
 
1606
	if ((config->min_width > r->width) || (r->width > config->max_width)) {
1607
		DRM_ERROR("mode new framebuffer width not within limits\n");
1608
		return -EINVAL;
1609
	}
1610
	if ((config->min_height > r->height) || (r->height > config->max_height)) {
1611
		DRM_ERROR("mode new framebuffer height not within limits\n");
1612
		return -EINVAL;
1613
	}
1614
 
1615
	mutex_lock(&dev->mode_config.mutex);
1616
 
1617
	/* TODO check buffer is sufficently large */
1618
	/* TODO setup destructor callback */
1619
 
1620
	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1621
	if (!fb) {
1622
		DRM_ERROR("could not create framebuffer\n");
1623
		ret = -EINVAL;
1624
		goto out;
1625
	}
1626
 
1627
	r->fb_id = fb->base.id;
1628
	list_add(&fb->filp_head, &file_priv->fbs);
1629
 
1630
out:
1631
	mutex_unlock(&dev->mode_config.mutex);
1632
	return ret;
1633
}
1634
 
1635
/**
1636
 * drm_mode_rmfb - remove an FB from the configuration
1637
 * @inode: inode from the ioctl
1638
 * @filp: file * from the ioctl
1639
 * @cmd: cmd from ioctl
1640
 * @arg: arg from ioctl
1641
 *
1642
 * LOCKING:
1643
 * Takes mode config lock.
1644
 *
1645
 * Remove the FB specified by the user.
1646
 *
1647
 * Called by the user via ioctl.
1648
 *
1649
 * RETURNS:
1650
 * Zero on success, errno on failure.
1651
 */
1652
int drm_mode_rmfb(struct drm_device *dev,
1653
		   void *data, struct drm_file *file_priv)
1654
{
1655
	struct drm_mode_object *obj;
1656
	struct drm_framebuffer *fb = NULL;
1657
	struct drm_framebuffer *fbl = NULL;
1658
	uint32_t *id = data;
1659
	int ret = 0;
1660
	int found = 0;
1661
 
1662
	mutex_lock(&dev->mode_config.mutex);
1663
	obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1664
	/* TODO check that we realy get a framebuffer back. */
1665
	if (!obj) {
1666
		DRM_ERROR("mode invalid framebuffer id\n");
1667
		ret = -EINVAL;
1668
		goto out;
1669
	}
1670
	fb = obj_to_fb(obj);
1671
 
1672
	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1673
		if (fb == fbl)
1674
			found = 1;
1675
 
1676
	if (!found) {
1677
		DRM_ERROR("tried to remove a fb that we didn't own\n");
1678
		ret = -EINVAL;
1679
		goto out;
1680
	}
1681
 
1682
	/* TODO release all crtc connected to the framebuffer */
1683
	/* TODO unhock the destructor from the buffer object */
1684
 
1685
	list_del(&fb->filp_head);
1686
	fb->funcs->destroy(fb);
1687
 
1688
out:
1689
	mutex_unlock(&dev->mode_config.mutex);
1690
	return ret;
1691
}
1692
 
1693
/**
1694
 * drm_mode_getfb - get FB info
1695
 * @inode: inode from the ioctl
1696
 * @filp: file * from the ioctl
1697
 * @cmd: cmd from ioctl
1698
 * @arg: arg from ioctl
1699
 *
1700
 * LOCKING:
1701
 * Caller? (FIXME)
1702
 *
1703
 * Lookup the FB given its ID and return info about it.
1704
 *
1705
 * Called by the user via ioctl.
1706
 *
1707
 * RETURNS:
1708
 * Zero on success, errno on failure.
1709
 */
1710
int drm_mode_getfb(struct drm_device *dev,
1711
		   void *data, struct drm_file *file_priv)
1712
{
1713
	struct drm_mode_fb_cmd *r = data;
1714
	struct drm_mode_object *obj;
1715
	struct drm_framebuffer *fb;
1716
	int ret = 0;
1717
 
1718
	mutex_lock(&dev->mode_config.mutex);
1719
	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1720
	if (!obj) {
1721
		DRM_ERROR("invalid framebuffer id\n");
1722
		ret = -EINVAL;
1723
		goto out;
1724
	}
1725
	fb = obj_to_fb(obj);
1726
 
1727
	r->height = fb->height;
1728
	r->width = fb->width;
1729
	r->depth = fb->depth;
1730
	r->bpp = fb->bits_per_pixel;
1731
	r->pitch = fb->pitch;
1732
	fb->funcs->create_handle(fb, file_priv, &r->handle);
1733
 
1734
out:
1735
	mutex_unlock(&dev->mode_config.mutex);
1736
	return ret;
1737
}
1738
 
1739
/**
1740
 * drm_fb_release - remove and free the FBs on this file
1741
 * @filp: file * from the ioctl
1742
 *
1743
 * LOCKING:
1744
 * Takes mode config lock.
1745
 *
1746
 * Destroy all the FBs associated with @filp.
1747
 *
1748
 * Called by the user via ioctl.
1749
 *
1750
 * RETURNS:
1751
 * Zero on success, errno on failure.
1752
 */
1753
void drm_fb_release(struct drm_file *priv)
1754
{
1755
	struct drm_device *dev = priv->minor->dev;
1756
	struct drm_framebuffer *fb, *tfb;
1757
 
1758
	mutex_lock(&dev->mode_config.mutex);
1759
	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1760
		list_del(&fb->filp_head);
1761
		fb->funcs->destroy(fb);
1762
	}
1763
	mutex_unlock(&dev->mode_config.mutex);
1764
}
1765
#endif
1766
 
1767
 
1768
/**
1769
 * drm_mode_attachmode - add a mode to the user mode list
1770
 * @dev: DRM device
1771
 * @connector: connector to add the mode to
1772
 * @mode: mode to add
1773
 *
1774
 * Add @mode to @connector's user mode list.
1775
 */
1776
static int drm_mode_attachmode(struct drm_device *dev,
1777
			       struct drm_connector *connector,
1778
			       struct drm_display_mode *mode)
1779
{
1780
	int ret = 0;
1781
 
1782
	list_add_tail(&mode->head, &connector->user_modes);
1783
	return ret;
1784
}
1785
 
1786
int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1787
			     struct drm_display_mode *mode)
1788
{
1789
	struct drm_connector *connector;
1790
	int ret = 0;
1791
	struct drm_display_mode *dup_mode;
1792
	int need_dup = 0;
1793
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1794
		if (!connector->encoder)
1795
			break;
1796
		if (connector->encoder->crtc == crtc) {
1797
			if (need_dup)
1798
				dup_mode = drm_mode_duplicate(dev, mode);
1799
			else
1800
				dup_mode = mode;
1801
			ret = drm_mode_attachmode(dev, connector, dup_mode);
1802
			if (ret)
1803
				return ret;
1804
			need_dup = 1;
1805
		}
1806
	}
1807
	return 0;
1808
}
1809
EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1810
 
1811
static int drm_mode_detachmode(struct drm_device *dev,
1812
			       struct drm_connector *connector,
1813
			       struct drm_display_mode *mode)
1814
{
1815
	int found = 0;
1816
	int ret = 0;
1817
	struct drm_display_mode *match_mode, *t;
1818
 
1819
	list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1820
		if (drm_mode_equal(match_mode, mode)) {
1821
			list_del(&match_mode->head);
1822
			drm_mode_destroy(dev, match_mode);
1823
			found = 1;
1824
			break;
1825
		}
1826
	}
1827
 
1828
	if (!found)
1829
		ret = -EINVAL;
1830
 
1831
	return ret;
1832
}
1833
 
1834
int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1835
{
1836
	struct drm_connector *connector;
1837
 
1838
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1839
		drm_mode_detachmode(dev, connector, mode);
1840
	}
1841
	return 0;
1842
}
1843
EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1844
 
1845
#if 0
1846
 
1847
/**
1848
 * drm_fb_attachmode - Attach a user mode to an connector
1849
 * @inode: inode from the ioctl
1850
 * @filp: file * from the ioctl
1851
 * @cmd: cmd from ioctl
1852
 * @arg: arg from ioctl
1853
 *
1854
 * This attaches a user specified mode to an connector.
1855
 * Called by the user via ioctl.
1856
 *
1857
 * RETURNS:
1858
 * Zero on success, errno on failure.
1859
 */
1860
int drm_mode_attachmode_ioctl(struct drm_device *dev,
1861
			      void *data, struct drm_file *file_priv)
1862
{
1863
	struct drm_mode_mode_cmd *mode_cmd = data;
1864
	struct drm_connector *connector;
1865
	struct drm_display_mode *mode;
1866
	struct drm_mode_object *obj;
1867
	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1868
	int ret = 0;
1869
 
1870
	mutex_lock(&dev->mode_config.mutex);
1871
 
1872
	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1873
	if (!obj) {
1874
		ret = -EINVAL;
1875
		goto out;
1876
	}
1877
	connector = obj_to_connector(obj);
1878
 
1879
	mode = drm_mode_create(dev);
1880
	if (!mode) {
1881
		ret = -ENOMEM;
1882
		goto out;
1883
	}
1884
 
1885
	drm_crtc_convert_umode(mode, umode);
1886
 
1887
	ret = drm_mode_attachmode(dev, connector, mode);
1888
out:
1889
	mutex_unlock(&dev->mode_config.mutex);
1890
	return ret;
1891
}
1892
 
1893
 
1894
/**
1895
 * drm_fb_detachmode - Detach a user specified mode from an connector
1896
 * @inode: inode from the ioctl
1897
 * @filp: file * from the ioctl
1898
 * @cmd: cmd from ioctl
1899
 * @arg: arg from ioctl
1900
 *
1901
 * Called by the user via ioctl.
1902
 *
1903
 * RETURNS:
1904
 * Zero on success, errno on failure.
1905
 */
1906
int drm_mode_detachmode_ioctl(struct drm_device *dev,
1907
			      void *data, struct drm_file *file_priv)
1908
{
1909
	struct drm_mode_object *obj;
1910
	struct drm_mode_mode_cmd *mode_cmd = data;
1911
	struct drm_connector *connector;
1912
	struct drm_display_mode mode;
1913
	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1914
	int ret = 0;
1915
 
1916
	mutex_lock(&dev->mode_config.mutex);
1917
 
1918
	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1919
	if (!obj) {
1920
		ret = -EINVAL;
1921
		goto out;
1922
	}
1923
	connector = obj_to_connector(obj);
1924
 
1925
	drm_crtc_convert_umode(&mode, umode);
1926
	ret = drm_mode_detachmode(dev, connector, &mode);
1927
out:
1928
	mutex_unlock(&dev->mode_config.mutex);
1929
	return ret;
1930
}
1931
#endif
1932
 
1933
struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1934
					 const char *name, int num_values)
1935
{
1936
	struct drm_property *property = NULL;
1937
 
1938
	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1939
	if (!property)
1940
		return NULL;
1941
 
1942
	if (num_values) {
1943
		property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1944
		if (!property->values)
1945
			goto fail;
1946
	}
1947
 
1948
	drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1949
	property->flags = flags;
1950
	property->num_values = num_values;
1951
	INIT_LIST_HEAD(&property->enum_blob_list);
1952
 
1953
	if (name)
1954
		strncpy(property->name, name, DRM_PROP_NAME_LEN);
1955
 
1956
	list_add_tail(&property->head, &dev->mode_config.property_list);
1957
	return property;
1958
fail:
1959
	kfree(property);
1960
	return NULL;
1961
}
1962
EXPORT_SYMBOL(drm_property_create);
1963
 
1964
int drm_property_add_enum(struct drm_property *property, int index,
1965
			  uint64_t value, const char *name)
1966
{
1967
	struct drm_property_enum *prop_enum;
1968
 
1969
	if (!(property->flags & DRM_MODE_PROP_ENUM))
1970
		return -EINVAL;
1971
 
1972
	if (!list_empty(&property->enum_blob_list)) {
1973
		list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1974
			if (prop_enum->value == value) {
1975
				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1976
				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1977
				return 0;
1978
			}
1979
		}
1980
	}
1981
 
1982
	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1983
	if (!prop_enum)
1984
		return -ENOMEM;
1985
 
1986
	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1987
	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1988
	prop_enum->value = value;
1989
 
1990
	property->values[index] = value;
1991
	list_add_tail(&prop_enum->head, &property->enum_blob_list);
1992
	return 0;
1993
}
1994
EXPORT_SYMBOL(drm_property_add_enum);
1995
 
1996
void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1997
{
1998
	struct drm_property_enum *prop_enum, *pt;
1999
 
2000
	list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2001
		list_del(&prop_enum->head);
2002
		kfree(prop_enum);
2003
	}
2004
 
2005
	if (property->num_values)
2006
		kfree(property->values);
2007
	drm_mode_object_put(dev, &property->base);
2008
	list_del(&property->head);
2009
	kfree(property);
2010
}
2011
EXPORT_SYMBOL(drm_property_destroy);
2012
 
2013
int drm_connector_attach_property(struct drm_connector *connector,
2014
			       struct drm_property *property, uint64_t init_val)
2015
{
2016
	int i;
2017
 
2018
	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2019
		if (connector->property_ids[i] == 0) {
2020
			connector->property_ids[i] = property->base.id;
2021
			connector->property_values[i] = init_val;
2022
			break;
2023
		}
2024
	}
2025
 
2026
	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2027
		return -EINVAL;
2028
	return 0;
2029
}
2030
EXPORT_SYMBOL(drm_connector_attach_property);
2031
 
2032
int drm_connector_property_set_value(struct drm_connector *connector,
2033
				  struct drm_property *property, uint64_t value)
2034
{
2035
	int i;
2036
 
2037
	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2038
		if (connector->property_ids[i] == property->base.id) {
2039
			connector->property_values[i] = value;
2040
			break;
2041
		}
2042
	}
2043
 
2044
	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2045
		return -EINVAL;
2046
	return 0;
2047
}
2048
EXPORT_SYMBOL(drm_connector_property_set_value);
2049
 
2050
int drm_connector_property_get_value(struct drm_connector *connector,
2051
				  struct drm_property *property, uint64_t *val)
2052
{
2053
	int i;
2054
 
2055
	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2056
		if (connector->property_ids[i] == property->base.id) {
2057
			*val = connector->property_values[i];
2058
			break;
2059
		}
2060
	}
2061
 
2062
	if (i == DRM_CONNECTOR_MAX_PROPERTY)
2063
		return -EINVAL;
2064
	return 0;
2065
}
2066
EXPORT_SYMBOL(drm_connector_property_get_value);
2067
 
2068
#if 0
2069
int drm_mode_getproperty_ioctl(struct drm_device *dev,
2070
			       void *data, struct drm_file *file_priv)
2071
{
2072
	struct drm_mode_object *obj;
2073
	struct drm_mode_get_property *out_resp = data;
2074
	struct drm_property *property;
2075
	int enum_count = 0;
2076
	int blob_count = 0;
2077
	int value_count = 0;
2078
	int ret = 0, i;
2079
	int copied;
2080
	struct drm_property_enum *prop_enum;
2081
	struct drm_mode_property_enum __user *enum_ptr;
2082
	struct drm_property_blob *prop_blob;
2083
	uint32_t *blob_id_ptr;
2084
	uint64_t __user *values_ptr;
2085
	uint32_t __user *blob_length_ptr;
2086
 
2087
//   mutex_lock(&dev->mode_config.mutex);
2088
	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2089
	if (!obj) {
2090
		ret = -EINVAL;
2091
		goto done;
2092
	}
2093
	property = obj_to_property(obj);
2094
 
2095
	if (property->flags & DRM_MODE_PROP_ENUM) {
2096
		list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2097
			enum_count++;
2098
	} else if (property->flags & DRM_MODE_PROP_BLOB) {
2099
		list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2100
			blob_count++;
2101
	}
2102
 
2103
	value_count = property->num_values;
2104
 
2105
	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2106
	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2107
	out_resp->flags = property->flags;
2108
 
2109
	if ((out_resp->count_values >= value_count) && value_count) {
2110
		values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2111
		for (i = 0; i < value_count; i++) {
2112
			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2113
				ret = -EFAULT;
2114
				goto done;
2115
			}
2116
		}
2117
	}
2118
	out_resp->count_values = value_count;
2119
 
2120
	if (property->flags & DRM_MODE_PROP_ENUM) {
2121
		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2122
			copied = 0;
2123
			enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2124
			list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2125
 
2126
				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2127
					ret = -EFAULT;
2128
					goto done;
2129
				}
2130
 
2131
				if (copy_to_user(&enum_ptr[copied].name,
2132
						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2133
					ret = -EFAULT;
2134
					goto done;
2135
				}
2136
				copied++;
2137
			}
2138
		}
2139
		out_resp->count_enum_blobs = enum_count;
2140
	}
2141
 
2142
	if (property->flags & DRM_MODE_PROP_BLOB) {
2143
		if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2144
			copied = 0;
2145
			blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2146
			blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2147
 
2148
			list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2149
				if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2150
					ret = -EFAULT;
2151
					goto done;
2152
				}
2153
 
2154
				if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2155
					ret = -EFAULT;
2156
					goto done;
2157
				}
2158
 
2159
				copied++;
2160
			}
2161
		}
2162
		out_resp->count_enum_blobs = blob_count;
2163
	}
2164
done:
2165
//   mutex_unlock(&dev->mode_config.mutex);
2166
	return ret;
2167
}
2168
#endif
2169
 
2170
static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2171
							  void *data)
2172
{
2173
	struct drm_property_blob *blob;
2174
 
2175
	if (!length || !data)
2176
		return NULL;
2177
 
2178
	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2179
	if (!blob)
2180
		return NULL;
2181
 
2182
	blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2183
	blob->length = length;
2184
 
2185
	memcpy(blob->data, data, length);
2186
 
2187
	drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2188
 
2189
	list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2190
	return blob;
2191
}
2192
 
2193
static void drm_property_destroy_blob(struct drm_device *dev,
2194
			       struct drm_property_blob *blob)
2195
{
2196
	drm_mode_object_put(dev, &blob->base);
2197
	list_del(&blob->head);
2198
	kfree(blob);
2199
}
2200
 
2201
#if 0
2202
int drm_mode_getblob_ioctl(struct drm_device *dev,
2203
			   void *data, struct drm_file *file_priv)
2204
{
2205
	struct drm_mode_object *obj;
2206
	struct drm_mode_get_blob *out_resp = data;
2207
	struct drm_property_blob *blob;
2208
	int ret = 0;
2209
	void *blob_ptr;
2210
 
2211
	mutex_lock(&dev->mode_config.mutex);
2212
	obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2213
	if (!obj) {
2214
		ret = -EINVAL;
2215
		goto done;
2216
	}
2217
	blob = obj_to_blob(obj);
2218
 
2219
	if (out_resp->length == blob->length) {
2220
		blob_ptr = (void *)(unsigned long)out_resp->data;
2221
		if (copy_to_user(blob_ptr, blob->data, blob->length)){
2222
			ret = -EFAULT;
2223
			goto done;
2224
		}
2225
	}
2226
	out_resp->length = blob->length;
2227
 
2228
done:
2229
	mutex_unlock(&dev->mode_config.mutex);
2230
	return ret;
2231
}
2232
#endif
2233
 
2234
int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2235
					    struct edid *edid)
2236
{
2237
	struct drm_device *dev = connector->dev;
2238
	int ret = 0;
2239
 
2240
	if (connector->edid_blob_ptr)
2241
		drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2242
 
2243
	/* Delete edid, when there is none. */
2244
	if (!edid) {
2245
		connector->edid_blob_ptr = NULL;
2246
		ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2247
		return ret;
2248
	}
2249
 
2250
	connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2251
 
2252
	ret = drm_connector_property_set_value(connector,
2253
					       dev->mode_config.edid_property,
2254
					       connector->edid_blob_ptr->base.id);
2255
 
2256
	return ret;
2257
}
2258
EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2259
 
2260
#if 0
2261
int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2262
				       void *data, struct drm_file *file_priv)
2263
{
2264
	struct drm_mode_connector_set_property *out_resp = data;
2265
	struct drm_mode_object *obj;
2266
	struct drm_property *property;
2267
	struct drm_connector *connector;
2268
	int ret = -EINVAL;
2269
	int i;
2270
 
2271
//   mutex_lock(&dev->mode_config.mutex);
2272
 
2273
	obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2274
	if (!obj) {
2275
		goto out;
2276
	}
2277
	connector = obj_to_connector(obj);
2278
 
2279
	for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2280
		if (connector->property_ids[i] == out_resp->prop_id)
2281
			break;
2282
	}
2283
 
2284
	if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2285
		goto out;
2286
	}
2287
 
2288
	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2289
	if (!obj) {
2290
		goto out;
2291
	}
2292
	property = obj_to_property(obj);
2293
 
2294
	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2295
		goto out;
2296
 
2297
	if (property->flags & DRM_MODE_PROP_RANGE) {
2298
		if (out_resp->value < property->values[0])
2299
			goto out;
2300
 
2301
		if (out_resp->value > property->values[1])
2302
			goto out;
2303
	} else {
2304
		int found = 0;
2305
		for (i = 0; i < property->num_values; i++) {
2306
			if (property->values[i] == out_resp->value) {
2307
				found = 1;
2308
				break;
2309
			}
2310
		}
2311
		if (!found) {
2312
			goto out;
2313
		}
2314
	}
2315
 
2316
	/* Do DPMS ourselves */
2317
	if (property == connector->dev->mode_config.dpms_property) {
2318
		if (connector->funcs->dpms)
2319
			(*connector->funcs->dpms)(connector, (int) out_resp->value);
2320
		ret = 0;
2321
	} else if (connector->funcs->set_property)
2322
		ret = connector->funcs->set_property(connector, property, out_resp->value);
2323
 
2324
	/* store the property value if succesful */
2325
	if (!ret)
2326
		drm_connector_property_set_value(connector, property, out_resp->value);
2327
out:
2328
//   mutex_unlock(&dev->mode_config.mutex);
2329
	return ret;
2330
}
2331
#endif
2332
 
2333
int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2334
				      struct drm_encoder *encoder)
2335
{
2336
	int i;
2337
 
2338
	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2339
		if (connector->encoder_ids[i] == 0) {
2340
			connector->encoder_ids[i] = encoder->base.id;
2341
			return 0;
2342
		}
2343
	}
2344
	return -ENOMEM;
2345
}
2346
EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2347
 
2348
void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2349
				    struct drm_encoder *encoder)
2350
{
2351
	int i;
2352
	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2353
		if (connector->encoder_ids[i] == encoder->base.id) {
2354
			connector->encoder_ids[i] = 0;
2355
			if (connector->encoder == encoder)
2356
				connector->encoder = NULL;
2357
			break;
2358
		}
2359
	}
2360
}
2361
EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2362
 
2363
bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2364
				  int gamma_size)
2365
{
2366
	crtc->gamma_size = gamma_size;
2367
 
2368
	crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2369
	if (!crtc->gamma_store) {
2370
		crtc->gamma_size = 0;
2371
		return false;
2372
	}
2373
 
2374
	return true;
2375
}
2376
EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2377
 
2378
#if 0
2379
int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2380
			     void *data, struct drm_file *file_priv)
2381
{
2382
	struct drm_mode_crtc_lut *crtc_lut = data;
2383
	struct drm_mode_object *obj;
2384
	struct drm_crtc *crtc;
2385
	void *r_base, *g_base, *b_base;
2386
	int size;
2387
	int ret = 0;
2388
 
2389
//   mutex_lock(&dev->mode_config.mutex);
2390
	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2391
	if (!obj) {
2392
		ret = -EINVAL;
2393
		goto out;
2394
	}
2395
	crtc = obj_to_crtc(obj);
2396
 
2397
	/* memcpy into gamma store */
2398
	if (crtc_lut->gamma_size != crtc->gamma_size) {
2399
		ret = -EINVAL;
2400
		goto out;
2401
	}
2402
 
2403
	size = crtc_lut->gamma_size * (sizeof(uint16_t));
2404
	r_base = crtc->gamma_store;
2405
	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2406
		ret = -EFAULT;
2407
		goto out;
2408
	}
2409
 
2410
	g_base = r_base + size;
2411
	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2412
		ret = -EFAULT;
2413
		goto out;
2414
	}
2415
 
2416
	b_base = g_base + size;
2417
	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2418
		ret = -EFAULT;
2419
		goto out;
2420
	}
2421
 
2422
	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2423
 
2424
out:
2425
//   mutex_unlock(&dev->mode_config.mutex);
2426
	return ret;
2427
 
2428
}
2429
 
2430
int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2431
			     void *data, struct drm_file *file_priv)
2432
{
2433
	struct drm_mode_crtc_lut *crtc_lut = data;
2434
	struct drm_mode_object *obj;
2435
	struct drm_crtc *crtc;
2436
	void *r_base, *g_base, *b_base;
2437
	int size;
2438
	int ret = 0;
2439
 
2440
//   mutex_lock(&dev->mode_config.mutex);
2441
	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2442
	if (!obj) {
2443
		ret = -EINVAL;
2444
		goto out;
2445
	}
2446
	crtc = obj_to_crtc(obj);
2447
 
2448
	/* memcpy into gamma store */
2449
	if (crtc_lut->gamma_size != crtc->gamma_size) {
2450
		ret = -EINVAL;
2451
		goto out;
2452
	}
2453
 
2454
	size = crtc_lut->gamma_size * (sizeof(uint16_t));
2455
	r_base = crtc->gamma_store;
2456
	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2457
		ret = -EFAULT;
2458
		goto out;
2459
	}
2460
 
2461
	g_base = r_base + size;
2462
	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2463
		ret = -EFAULT;
2464
		goto out;
2465
	}
2466
 
2467
	b_base = g_base + size;
2468
	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2469
		ret = -EFAULT;
2470
		goto out;
2471
	}
2472
out:
2473
//   mutex_unlock(&dev->mode_config.mutex);
2474
	return ret;
2475
}
2476
 
2477
#endif