/[pkgs]/rpms/kernel/F-9/drm-next.patch
ViewVC logotype

Contents of /rpms/kernel/F-9/drm-next.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download) (as text)
Mon Jan 12 19:54:17 2009 UTC (10 months, 1 week ago) by kyle
Branch: MAIN
CVS Tags: kernel-2_6_28-1_fc9, kernel-2_6_28-2_fc9, HEAD
File MIME type: text/x-patch
* Mon Jan 12 2009 Kyle McMartin <kyle@redhat.com>
- Rebase for Fedora 9.
- Turn off CONFIG_MAXSMP on x86_64.
1 diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
2 index a8b33c2..5130b72 100644
3 --- a/drivers/gpu/drm/Kconfig
4 +++ b/drivers/gpu/drm/Kconfig
5 @@ -7,6 +7,8 @@
6 menuconfig DRM
7 tristate "Direct Rendering Manager (XFree86 4.1.0 and higher DRI support)"
8 depends on (AGP || AGP=n) && PCI && !EMULATED_CMPXCHG && MMU
9 + select I2C
10 + select I2C_ALGOBIT
11 help
12 Kernel-level support for the Direct Rendering Infrastructure (DRI)
13 introduced in XFree86 4.0. If you say Y here, you need to select
14 @@ -65,6 +67,10 @@ config DRM_I830
15 will load the correct one.
16
17 config DRM_I915
18 + select FB_CFB_FILLRECT
19 + select FB_CFB_COPYAREA
20 + select FB_CFB_IMAGEBLIT
21 + depends on FB
22 tristate "i915 driver"
23 help
24 Choose this option if you have a system that has Intel 830M, 845G,
25 @@ -76,6 +82,17 @@ config DRM_I915
26
27 endchoice
28
29 +config DRM_I915_KMS
30 + bool "Enable modesetting on intel by default"
31 + depends on DRM_I915
32 + help
33 + Choose this option if you want kernel modesetting enabled by default,
34 + and you have a new enough userspace to support this. Running old
35 + userspaces with this enabled will cause pain. Note that this causes
36 + the driver to bind to PCI devices, which precludes loading things
37 + like intelfb.
38 +
39 +
40 config DRM_MGA
41 tristate "Matrox g200/g400"
42 depends on DRM
43 diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
44 index 74da994..30022c4 100644
45 --- a/drivers/gpu/drm/Makefile
46 +++ b/drivers/gpu/drm/Makefile
47 @@ -9,7 +9,8 @@ drm-y := drm_auth.o drm_bufs.o drm_cache.o \
48 drm_drv.o drm_fops.o drm_gem.o drm_ioctl.o drm_irq.o \
49 drm_lock.o drm_memory.o drm_proc.o drm_stub.o drm_vm.o \
50 drm_agpsupport.o drm_scatter.o ati_pcigart.o drm_pci.o \
51 - drm_sysfs.o drm_hashtab.o drm_sman.o drm_mm.o
52 + drm_sysfs.o drm_hashtab.o drm_sman.o drm_mm.o \
53 + drm_crtc.o drm_crtc_helper.o drm_modes.o drm_edid.o
54
55 drm-$(CONFIG_COMPAT) += drm_ioc32.o
56
57 diff --git a/drivers/gpu/drm/drm_auth.c b/drivers/gpu/drm/drm_auth.c
58 index a734627..ca7a9ef 100644
59 --- a/drivers/gpu/drm/drm_auth.c
60 +++ b/drivers/gpu/drm/drm_auth.c
61 @@ -45,14 +45,15 @@
62 * the one with matching magic number, while holding the drm_device::struct_mutex
63 * lock.
64 */
65 -static struct drm_file *drm_find_file(struct drm_device * dev, drm_magic_t magic)
66 +static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
67 {
68 struct drm_file *retval = NULL;
69 struct drm_magic_entry *pt;
70 struct drm_hash_item *hash;
71 + struct drm_device *dev = master->minor->dev;
72
73 mutex_lock(&dev->struct_mutex);
74 - if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
75 + if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
76 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
77 retval = pt->priv;
78 }
79 @@ -71,11 +72,11 @@ static struct drm_file *drm_find_file(struct drm_device * dev, drm_magic_t magic
80 * associated the magic number hash key in drm_device::magiclist, while holding
81 * the drm_device::struct_mutex lock.
82 */
83 -static int drm_add_magic(struct drm_device * dev, struct drm_file * priv,
84 +static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
85 drm_magic_t magic)
86 {
87 struct drm_magic_entry *entry;
88 -
89 + struct drm_device *dev = master->minor->dev;
90 DRM_DEBUG("%d\n", magic);
91
92 entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
93 @@ -83,11 +84,10 @@ static int drm_add_magic(struct drm_device * dev, struct drm_file * priv,
94 return -ENOMEM;
95 memset(entry, 0, sizeof(*entry));
96 entry->priv = priv;
97 -
98 entry->hash_item.key = (unsigned long)magic;
99 mutex_lock(&dev->struct_mutex);
100 - drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
101 - list_add_tail(&entry->head, &dev->magicfree);
102 + drm_ht_insert_item(&master->magiclist, &entry->hash_item);
103 + list_add_tail(&entry->head, &master->magicfree);
104 mutex_unlock(&dev->struct_mutex);
105
106 return 0;
107 @@ -102,20 +102,21 @@ static int drm_add_magic(struct drm_device * dev, struct drm_file * priv,
108 * Searches and unlinks the entry in drm_device::magiclist with the magic
109 * number hash key, while holding the drm_device::struct_mutex lock.
110 */
111 -static int drm_remove_magic(struct drm_device * dev, drm_magic_t magic)
112 +static int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
113 {
114 struct drm_magic_entry *pt;
115 struct drm_hash_item *hash;
116 + struct drm_device *dev = master->minor->dev;
117
118 DRM_DEBUG("%d\n", magic);
119
120 mutex_lock(&dev->struct_mutex);
121 - if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
122 + if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
123 mutex_unlock(&dev->struct_mutex);
124 return -EINVAL;
125 }
126 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
127 - drm_ht_remove_item(&dev->magiclist, hash);
128 + drm_ht_remove_item(&master->magiclist, hash);
129 list_del(&pt->head);
130 mutex_unlock(&dev->struct_mutex);
131
132 @@ -153,9 +154,9 @@ int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
133 ++sequence; /* reserve 0 */
134 auth->magic = sequence++;
135 spin_unlock(&lock);
136 - } while (drm_find_file(dev, auth->magic));
137 + } while (drm_find_file(file_priv->master, auth->magic));
138 file_priv->magic = auth->magic;
139 - drm_add_magic(dev, file_priv, auth->magic);
140 + drm_add_magic(file_priv->master, file_priv, auth->magic);
141 }
142
143 DRM_DEBUG("%u\n", auth->magic);
144 @@ -181,9 +182,9 @@ int drm_authmagic(struct drm_device *dev, void *data,
145 struct drm_file *file;
146
147 DRM_DEBUG("%u\n", auth->magic);
148 - if ((file = drm_find_file(dev, auth->magic))) {
149 + if ((file = drm_find_file(file_priv->master, auth->magic))) {
150 file->authenticated = 1;
151 - drm_remove_magic(dev, auth->magic);
152 + drm_remove_magic(file_priv->master, auth->magic);
153 return 0;
154 }
155 return -EINVAL;
156 diff --git a/drivers/gpu/drm/drm_bufs.c b/drivers/gpu/drm/drm_bufs.c
157 index bde64b8..72c667f 100644
158 --- a/drivers/gpu/drm/drm_bufs.c
159 +++ b/drivers/gpu/drm/drm_bufs.c
160 @@ -54,9 +54,9 @@ static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
161 {
162 struct drm_map_list *entry;
163 list_for_each_entry(entry, &dev->maplist, head) {
164 - if (entry->map && map->type == entry->map->type &&
165 + if (entry->map && (entry->master == dev->primary->master) && (map->type == entry->map->type) &&
166 ((entry->map->offset == map->offset) ||
167 - (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) {
168 + ((map->type == _DRM_SHM) && (map->flags&_DRM_CONTAINS_LOCK)))) {
169 return entry;
170 }
171 }
172 @@ -210,12 +210,12 @@ static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
173 map->offset = (unsigned long)map->handle;
174 if (map->flags & _DRM_CONTAINS_LOCK) {
175 /* Prevent a 2nd X Server from creating a 2nd lock */
176 - if (dev->lock.hw_lock != NULL) {
177 + if (dev->primary->master->lock.hw_lock != NULL) {
178 vfree(map->handle);
179 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
180 return -EBUSY;
181 }
182 - dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
183 + dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
184 }
185 break;
186 case _DRM_AGP: {
187 @@ -262,6 +262,9 @@ static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
188 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
189
190 break;
191 + case _DRM_GEM:
192 + DRM_ERROR("tried to rmmap GEM object\n");
193 + break;
194 }
195 case _DRM_SCATTER_GATHER:
196 if (!dev->sg) {
197 @@ -319,6 +322,7 @@ static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
198 list->user_token = list->hash.key << PAGE_SHIFT;
199 mutex_unlock(&dev->struct_mutex);
200
201 + list->master = dev->primary->master;
202 *maplist = list;
203 return 0;
204 }
205 @@ -345,7 +349,7 @@ int drm_addmap_ioctl(struct drm_device *dev, void *data,
206 struct drm_map_list *maplist;
207 int err;
208
209 - if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP))
210 + if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
211 return -EPERM;
212
213 err = drm_addmap_core(dev, map->offset, map->size, map->type,
214 @@ -380,10 +384,12 @@ int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
215 struct drm_map_list *r_list = NULL, *list_t;
216 drm_dma_handle_t dmah;
217 int found = 0;
218 + struct drm_master *master;
219
220 /* Find the list entry for the map and remove it */
221 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
222 if (r_list->map == map) {
223 + master = r_list->master;
224 list_del(&r_list->head);
225 drm_ht_remove_key(&dev->map_hash,
226 r_list->user_token >> PAGE_SHIFT);
227 @@ -409,6 +415,13 @@ int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
228 break;
229 case _DRM_SHM:
230 vfree(map->handle);
231 + if (master) {
232 + if (dev->sigdata.lock == master->lock.hw_lock)
233 + dev->sigdata.lock = NULL;
234 + master->lock.hw_lock = NULL; /* SHM removed */
235 + master->lock.file_priv = NULL;
236 + wake_up_interruptible(&master->lock.lock_queue);
237 + }
238 break;
239 case _DRM_AGP:
240 case _DRM_SCATTER_GATHER:
241 @@ -419,11 +432,15 @@ int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
242 dmah.size = map->size;
243 __drm_pci_free(dev, &dmah);
244 break;
245 + case _DRM_GEM:
246 + DRM_ERROR("tried to rmmap GEM object\n");
247 + break;
248 }
249 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
250
251 return 0;
252 }
253 +EXPORT_SYMBOL(drm_rmmap_locked);
254
255 int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
256 {
257 diff --git a/drivers/gpu/drm/drm_context.c b/drivers/gpu/drm/drm_context.c
258 index d505f69..809ec0f 100644
259 --- a/drivers/gpu/drm/drm_context.c
260 +++ b/drivers/gpu/drm/drm_context.c
261 @@ -256,12 +256,13 @@ static int drm_context_switch(struct drm_device * dev, int old, int new)
262 * hardware lock is held, clears the drm_device::context_flag and wakes up
263 * drm_device::context_wait.
264 */
265 -static int drm_context_switch_complete(struct drm_device * dev, int new)
266 +static int drm_context_switch_complete(struct drm_device *dev,
267 + struct drm_file *file_priv, int new)
268 {
269 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
270 dev->last_switch = jiffies;
271
272 - if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
273 + if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
274 DRM_ERROR("Lock isn't held after context switch\n");
275 }
276
277 @@ -420,7 +421,7 @@ int drm_newctx(struct drm_device *dev, void *data,
278 struct drm_ctx *ctx = data;
279
280 DRM_DEBUG("%d\n", ctx->handle);
281 - drm_context_switch_complete(dev, ctx->handle);
282 + drm_context_switch_complete(dev, file_priv, ctx->handle);
283
284 return 0;
285 }
286 @@ -442,9 +443,6 @@ int drm_rmctx(struct drm_device *dev, void *data,
287 struct drm_ctx *ctx = data;
288
289 DRM_DEBUG("%d\n", ctx->handle);
290 - if (ctx->handle == DRM_KERNEL_CONTEXT + 1) {
291 - file_priv->remove_auth_on_close = 1;
292 - }
293 if (ctx->handle != DRM_KERNEL_CONTEXT) {
294 if (dev->driver->context_dtor)
295 dev->driver->context_dtor(dev, ctx->handle);
296 diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
297 new file mode 100644
298 index 0000000..53c8725
299 --- /dev/null
300 +++ b/drivers/gpu/drm/drm_crtc.c
301 @@ -0,0 +1,2446 @@
302 +/*
303 + * Copyright (c) 2006-2008 Intel Corporation
304 + * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
305 + * Copyright (c) 2008 Red Hat Inc.
306 + *
307 + * DRM core CRTC related functions
308 + *
309 + * Permission to use, copy, modify, distribute, and sell this software and its
310 + * documentation for any purpose is hereby granted without fee, provided that
311 + * the above copyright notice appear in all copies and that both that copyright
312 + * notice and this permission notice appear in supporting documentation, and
313 + * that the name of the copyright holders not be used in advertising or
314 + * publicity pertaining to distribution of the software without specific,
315 + * written prior permission. The copyright holders make no representations
316 + * about the suitability of this software for any purpose. It is provided "as
317 + * is" without express or implied warranty.
318 + *
319 + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
320 + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
321 + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
322 + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
323 + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
324 + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
325 + * OF THIS SOFTWARE.
326 + *
327 + * Authors:
328 + * Keith Packard
329 + * Eric Anholt <eric@anholt.net>
330 + * Dave Airlie <airlied@linux.ie>
331 + * Jesse Barnes <jesse.barnes@intel.com>
332 + */
333 +#include <linux/list.h>
334 +#include "drm.h"
335 +#include "drmP.h"
336 +#include "drm_crtc.h"
337 +
338 +struct drm_prop_enum_list {
339 + int type;
340 + char *name;
341 +};
342 +
343 +/* Avoid boilerplate. I'm tired of typing. */
344 +#define DRM_ENUM_NAME_FN(fnname, list) \
345 + char *fnname(int val) \
346 + { \
347 + int i; \
348 + for (i = 0; i < ARRAY_SIZE(list); i++) { \
349 + if (list[i].type == val) \
350 + return list[i].name; \
351 + } \
352 + return "(unknown)"; \
353 + }
354 +
355 +/*
356 + * Global properties
357 + */
358 +static struct drm_prop_enum_list drm_dpms_enum_list[] =
359 +{ { DRM_MODE_DPMS_ON, "On" },
360 + { DRM_MODE_DPMS_STANDBY, "Standby" },
361 + { DRM_MODE_DPMS_SUSPEND, "Suspend" },
362 + { DRM_MODE_DPMS_OFF, "Off" }
363 +};
364 +
365 +DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
366 +
367 +/*
368 + * Optional properties
369 + */
370 +static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
371 +{
372 + { DRM_MODE_SCALE_NON_GPU, "Non-GPU" },
373 + { DRM_MODE_SCALE_FULLSCREEN, "Fullscreen" },
374 + { DRM_MODE_SCALE_NO_SCALE, "No scale" },
375 + { DRM_MODE_SCALE_ASPECT, "Aspect" },
376 +};
377 +
378 +static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
379 +{
380 + { DRM_MODE_DITHERING_OFF, "Off" },
381 + { DRM_MODE_DITHERING_ON, "On" },
382 +};
383 +
384 +/*
385 + * Non-global properties, but "required" for certain connectors.
386 + */
387 +static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
388 +{
389 + { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
390 + { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
391 + { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
392 +};
393 +
394 +DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
395 +
396 +static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
397 +{
398 + { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
399 + { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
400 + { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
401 +};
402 +
403 +DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
404 + drm_dvi_i_subconnector_enum_list)
405 +
406 +static struct drm_prop_enum_list drm_tv_select_enum_list[] =
407 +{
408 + { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
409 + { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
410 + { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
411 + { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
412 +};
413 +
414 +DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
415 +
416 +static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
417 +{
418 + { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
419 + { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
420 + { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
421 + { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
422 +};
423 +
424 +DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
425 + drm_tv_subconnector_enum_list)
426 +
427 +struct drm_conn_prop_enum_list {
428 + int type;
429 + char *name;
430 + int count;
431 +};
432 +
433 +/*
434 + * Connector and encoder types.
435 + */
436 +static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
437 +{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
438 + { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
439 + { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
440 + { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
441 + { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
442 + { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
443 + { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
444 + { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
445 + { DRM_MODE_CONNECTOR_Component, "Component", 0 },
446 + { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
447 + { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
448 + { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
449 + { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
450 +};
451 +
452 +static struct drm_prop_enum_list drm_encoder_enum_list[] =
453 +{ { DRM_MODE_ENCODER_NONE, "None" },
454 + { DRM_MODE_ENCODER_DAC, "DAC" },
455 + { DRM_MODE_ENCODER_TMDS, "TMDS" },
456 + { DRM_MODE_ENCODER_LVDS, "LVDS" },
457 + { DRM_MODE_ENCODER_TVDAC, "TV" },
458 +};
459 +
460 +char *drm_get_encoder_name(struct drm_encoder *encoder)
461 +{
462 + static char buf[32];
463 +
464 + snprintf(buf, 32, "%s-%d",
465 + drm_encoder_enum_list[encoder->encoder_type].name,
466 + encoder->base.id);
467 + return buf;
468 +}
469 +
470 +char *drm_get_connector_name(struct drm_connector *connector)
471 +{
472 + static char buf[32];
473 +
474 + snprintf(buf, 32, "%s-%d",
475 + drm_connector_enum_list[connector->connector_type].name,
476 + connector->connector_type_id);
477 + return buf;
478 +}
479 +EXPORT_SYMBOL(drm_get_connector_name);
480 +
481 +char *drm_get_connector_status_name(enum drm_connector_status status)
482 +{
483 + if (status == connector_status_connected)
484 + return "connected";
485 + else if (status == connector_status_disconnected)
486 + return "disconnected";
487 + else
488 + return "unknown";
489 +}
490 +
491 +/**
492 + * drm_mode_object_get - allocate a new identifier
493 + * @dev: DRM device
494 + * @ptr: object pointer, used to generate unique ID
495 + * @type: object type
496 + *
497 + * LOCKING:
498 + * Caller must hold DRM mode_config lock.
499 + *
500 + * Create a unique identifier based on @ptr in @dev's identifier space. Used
501 + * for tracking modes, CRTCs and connectors.
502 + *
503 + * RETURNS:
504 + * New unique (relative to other objects in @dev) integer identifier for the
505 + * object.
506 + */
507 +static int drm_mode_object_get(struct drm_device *dev,
508 + struct drm_mode_object *obj, uint32_t obj_type)
509 +{
510 + int new_id = 0;
511 + int ret;
512 +
513 + WARN(!mutex_is_locked(&dev->mode_config.mutex),
514 + "%s called w/o mode_config lock\n", __FUNCTION__);
515 +again:
516 + if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
517 + DRM_ERROR("Ran out memory getting a mode number\n");
518 + return -EINVAL;
519 + }
520 +
521 + ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
522 + if (ret == -EAGAIN)
523 + goto again;
524 +
525 + obj->id = new_id;
526 + obj->type = obj_type;
527 + return 0;
528 +}
529 +
530 +/**
531 + * drm_mode_object_put - free an identifer
532 + * @dev: DRM device
533 + * @id: ID to free
534 + *
535 + * LOCKING:
536 + * Caller must hold DRM mode_config lock.
537 + *
538 + * Free @id from @dev's unique identifier pool.
539 + */
540 +static void drm_mode_object_put(struct drm_device *dev,
541 + struct drm_mode_object *object)
542 +{
543 + idr_remove(&dev->mode_config.crtc_idr, object->id);
544 +}
545 +
546 +void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
547 +{
548 + struct drm_mode_object *obj;
549 +
550 + obj = idr_find(&dev->mode_config.crtc_idr, id);
551 + if (!obj || (obj->type != type) || (obj->id != id))
552 + return NULL;
553 +
554 + return obj;
555 +}
556 +EXPORT_SYMBOL(drm_mode_object_find);
557 +
558 +/**
559 + * drm_crtc_from_fb - find the CRTC structure associated with an fb
560 + * @dev: DRM device
561 + * @fb: framebuffer in question
562 + *
563 + * LOCKING:
564 + * Caller must hold mode_config lock.
565 + *
566 + * Find CRTC in the mode_config structure that matches @fb.
567 + *
568 + * RETURNS:
569 + * Pointer to the CRTC or NULL if it wasn't found.
570 + */
571 +struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
572 + struct drm_framebuffer *fb)
573 +{
574 + struct drm_crtc *crtc;
575 +
576 + list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
577 + if (crtc->fb == fb)
578 + return crtc;
579 + }
580 + return NULL;
581 +}
582 +
583 +/**
584 + * drm_framebuffer_init - initialize a framebuffer
585 + * @dev: DRM device
586 + *
587 + * LOCKING:
588 + * Caller must hold mode config lock.
589 + *
590 + * Allocates an ID for the framebuffer's parent mode object, sets its mode
591 + * functions & device file and adds it to the master fd list.
592 + *
593 + * RETURNS:
594 + * Zero on success, error code on falure.
595 + */
596 +int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
597 + const struct drm_framebuffer_funcs *funcs)
598 +{
599 + int ret;
600 +
601 + ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
602 + if (ret) {
603 + return ret;
604 + }
605 +
606 + fb->dev = dev;
607 + fb->funcs = funcs;
608 + dev->mode_config.num_fb++;
609 + list_add(&fb->head, &dev->mode_config.fb_list);
610 +
611 + return 0;
612 +}
613 +EXPORT_SYMBOL(drm_framebuffer_init);
614 +
615 +/**
616 + * drm_framebuffer_cleanup - remove a framebuffer object
617 + * @fb: framebuffer to remove
618 + *
619 + * LOCKING:
620 + * Caller must hold mode config lock.
621 + *
622 + * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
623 + * it, setting it to NULL.
624 + */
625 +void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
626 +{
627 + struct drm_device *dev = fb->dev;
628 + struct drm_crtc *crtc;
629 +
630 + /* remove from any CRTC */
631 + list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
632 + if (crtc->fb == fb)
633 + crtc->fb = NULL;
634 + }
635 +
636 + drm_mode_object_put(dev, &fb->base);
637 + list_del(&fb->head);
638 + dev->mode_config.num_fb--;
639 +}
640 +EXPORT_SYMBOL(drm_framebuffer_cleanup);
641 +
642 +/**
643 + * drm_crtc_init - Initialise a new CRTC object
644 + * @dev: DRM device
645 + * @crtc: CRTC object to init
646 + * @funcs: callbacks for the new CRTC
647 + *
648 + * LOCKING:
649 + * Caller must hold mode config lock.
650 + *
651 + * Inits a new object created as base part of an driver crtc object.
652 + */
653 +void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
654 + const struct drm_crtc_funcs *funcs)
655 +{
656 + crtc->dev = dev;
657 + crtc->funcs = funcs;
658 +
659 + mutex_lock(&dev->mode_config.mutex);
660 + drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
661 +
662 + list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
663 + dev->mode_config.num_crtc++;
664 + mutex_unlock(&dev->mode_config.mutex);
665 +}
666 +EXPORT_SYMBOL(drm_crtc_init);
667 +
668 +/**
669 + * drm_crtc_cleanup - Cleans up the core crtc usage.
670 + * @crtc: CRTC to cleanup
671 + *
672 + * LOCKING:
673 + * Caller must hold mode config lock.
674 + *
675 + * Cleanup @crtc. Removes from drm modesetting space
676 + * does NOT free object, caller does that.
677 + */
678 +void drm_crtc_cleanup(struct drm_crtc *crtc)
679 +{
680 + struct drm_device *dev = crtc->dev;
681 +
682 + if (crtc->gamma_store) {
683 + kfree(crtc->gamma_store);
684 + crtc->gamma_store = NULL;
685 + }
686 +
687 + drm_mode_object_put(dev, &crtc->base);
688 + list_del(&crtc->head);
689 + dev->mode_config.num_crtc--;
690 +}
691 +EXPORT_SYMBOL(drm_crtc_cleanup);
692 +
693 +/**
694 + * drm_mode_probed_add - add a mode to a connector's probed mode list
695 + * @connector: connector the new mode
696 + * @mode: mode data
697 + *
698 + * LOCKING:
699 + * Caller must hold mode config lock.
700 + *
701 + * Add @mode to @connector's mode list for later use.
702 + */
703 +void drm_mode_probed_add(struct drm_connector *connector,
704 + struct drm_display_mode *mode)
705 +{
706 + list_add(&mode->head, &connector->probed_modes);
707 +}
708 +EXPORT_SYMBOL(drm_mode_probed_add);
709 +
710 +/**
711 + * drm_mode_remove - remove and free a mode
712 + * @connector: connector list to modify
713 + * @mode: mode to remove
714 + *
715 + * LOCKING:
716 + * Caller must hold mode config lock.
717 + *
718 + * Remove @mode from @connector's mode list, then free it.
719 + */
720 +void drm_mode_remove(struct drm_connector *connector,
721 + struct drm_display_mode *mode)
722 +{
723 + list_del(&mode->head);
724 + kfree(mode);
725 +}
726 +EXPORT_SYMBOL(drm_mode_remove);
727 +
728 +/**
729 + * drm_connector_init - Init a preallocated connector
730 + * @dev: DRM device
731 + * @connector: the connector to init
732 + * @funcs: callbacks for this connector
733 + * @name: user visible name of the connector
734 + *
735 + * LOCKING:
736 + * Caller must hold @dev's mode_config lock.
737 + *
738 + * Initialises a preallocated connector. Connectors should be
739 + * subclassed as part of driver connector objects.
740 + */
741 +void drm_connector_init(struct drm_device *dev,
742 + struct drm_connector *connector,
743 + const struct drm_connector_funcs *funcs,
744 + int connector_type)
745 +{
746 + mutex_lock(&dev->mode_config.mutex);
747 +
748 + connector->dev = dev;
749 + connector->funcs = funcs;
750 + drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
751 + connector->connector_type = connector_type;
752 + connector->connector_type_id =
753 + ++drm_connector_enum_list[connector_type].count; /* TODO */
754 + INIT_LIST_HEAD(&connector->user_modes);
755 + INIT_LIST_HEAD(&connector->probed_modes);
756 + INIT_LIST_HEAD(&connector->modes);
757 + connector->edid_blob_ptr = NULL;
758 +
759 + list_add_tail(&connector->head, &dev->mode_config.connector_list);
760 + dev->mode_config.num_connector++;
761 +
762 + drm_connector_attach_property(connector,
763 + dev->mode_config.edid_property, 0);
764 +
765 + drm_connector_attach_property(connector,
766 + dev->mode_config.dpms_property, 0);
767 +
768 + mutex_unlock(&dev->mode_config.mutex);
769 +}
770 +EXPORT_SYMBOL(drm_connector_init);
771 +
772 +/**
773 + * drm_connector_cleanup - cleans up an initialised connector
774 + * @connector: connector to cleanup
775 + *
776 + * LOCKING:
777 + * Caller must hold @dev's mode_config lock.
778 + *
779 + * Cleans up the connector but doesn't free the object.
780 + */
781 +void drm_connector_cleanup(struct drm_connector *connector)
782 +{
783 + struct drm_device *dev = connector->dev;
784 + struct drm_display_mode *mode, *t;
785 +
786 + list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
787 + drm_mode_remove(connector, mode);
788 +
789 + list_for_each_entry_safe(mode, t, &connector->modes, head)
790 + drm_mode_remove(connector, mode);
791 +
792 + list_for_each_entry_safe(mode, t, &connector->user_modes, head)
793 + drm_mode_remove(connector, mode);
794 +
795 + mutex_lock(&dev->mode_config.mutex);
796 + drm_mode_object_put(dev, &connector->base);
797 + list_del(&connector->head);
798 + mutex_unlock(&dev->mode_config.mutex);
799 +}
800 +EXPORT_SYMBOL(drm_connector_cleanup);
801 +
802 +void drm_encoder_init(struct drm_device *dev,
803 + struct drm_encoder *encoder,
804 + const struct drm_encoder_funcs *funcs,
805 + int encoder_type)
806 +{
807 + mutex_lock(&dev->mode_config.mutex);
808 +
809 + encoder->dev = dev;
810 +
811 + drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
812 + encoder->encoder_type = encoder_type;
813 + encoder->funcs = funcs;
814 +
815 + list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
816 + dev->mode_config.num_encoder++;
817 +
818 + mutex_unlock(&dev->mode_config.mutex);
819 +}
820 +EXPORT_SYMBOL(drm_encoder_init);
821 +
822 +void drm_encoder_cleanup(struct drm_encoder *encoder)
823 +{
824 + struct drm_device *dev = encoder->dev;
825 + mutex_lock(&dev->mode_config.mutex);
826 + drm_mode_object_put(dev, &encoder->base);
827 + list_del(&encoder->head);
828 + mutex_unlock(&dev->mode_config.mutex);
829 +}
830 +EXPORT_SYMBOL(drm_encoder_cleanup);
831 +
832 +/**
833 + * drm_mode_create - create a new display mode
834 + * @dev: DRM device
835 + *
836 + * LOCKING:
837 + * Caller must hold DRM mode_config lock.
838 + *
839 + * Create a new drm_display_mode, give it an ID, and return it.
840 + *
841 + * RETURNS:
842 + * Pointer to new mode on success, NULL on error.
843 + */
844 +struct drm_display_mode *drm_mode_create(struct drm_device *dev)
845 +{
846 + struct drm_display_mode *nmode;
847 +
848 + nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
849 + if (!nmode)
850 + return NULL;
851 +
852 + drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
853 + return nmode;
854 +}
855 +EXPORT_SYMBOL(drm_mode_create);
856 +
857 +/**
858 + * drm_mode_destroy - remove a mode
859 + * @dev: DRM device
860 + * @mode: mode to remove
861 + *
862 + * LOCKING:
863 + * Caller must hold mode config lock.
864 + *
865 + * Free @mode's unique identifier, then free it.
866 + */
867 +void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
868 +{
869 + drm_mode_object_put(dev, &mode->base);
870 +
871 + kfree(mode);
872 +}
873 +EXPORT_SYMBOL(drm_mode_destroy);
874 +
875 +static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
876 +{
877 + struct drm_property *edid;
878 + struct drm_property *dpms;
879 + int i;
880 +
881 + /*
882 + * Standard properties (apply to all connectors)
883 + */
884 + edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
885 + DRM_MODE_PROP_IMMUTABLE,
886 + "EDID", 0);
887 + dev->mode_config.edid_property = edid;
888 +
889 + dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
890 + "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
891 + for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
892 + drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
893 + drm_dpms_enum_list[i].name);
894 + dev->mode_config.dpms_property = dpms;
895 +
896 + return 0;
897 +}
898 +
899 +/**
900 + * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
901 + * @dev: DRM device
902 + *
903 + * Called by a driver the first time a DVI-I connector is made.
904 + */
905 +int drm_mode_create_dvi_i_properties(struct drm_device *dev)
906 +{
907 + struct drm_property *dvi_i_selector;
908 + struct drm_property *dvi_i_subconnector;
909 + int i;
910 +
911 + if (dev->mode_config.dvi_i_select_subconnector_property)
912 + return 0;
913 +
914 + dvi_i_selector =
915 + drm_property_create(dev, DRM_MODE_PROP_ENUM,
916 + "select subconnector",
917 + ARRAY_SIZE(drm_dvi_i_select_enum_list));
918 + for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
919 + drm_property_add_enum(dvi_i_selector, i,
920 + drm_dvi_i_select_enum_list[i].type,
921 + drm_dvi_i_select_enum_list[i].name);
922 + dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
923 +
924 + dvi_i_subconnector =
925 + drm_property_create(dev, DRM_MODE_PROP_ENUM |
926 + DRM_MODE_PROP_IMMUTABLE,
927 + "subconnector",
928 + ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
929 + for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
930 + drm_property_add_enum(dvi_i_subconnector, i,
931 + drm_dvi_i_subconnector_enum_list[i].type,
932 + drm_dvi_i_subconnector_enum_list[i].name);
933 + dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
934 +
935 + return 0;
936 +}
937 +EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
938 +
939 +/**
940 + * drm_create_tv_properties - create TV specific connector properties
941 + * @dev: DRM device
942 + * @num_modes: number of different TV formats (modes) supported
943 + * @modes: array of pointers to strings containing name of each format
944 + *
945 + * Called by a driver's TV initialization routine, this function creates
946 + * the TV specific connector properties for a given device. Caller is
947 + * responsible for allocating a list of format names and passing them to
948 + * this routine.
949 + */
950 +int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
951 + char *modes[])
952 +{
953 + struct drm_property *tv_selector;
954 + struct drm_property *tv_subconnector;
955 + int i;
956 +
957 + if (dev->mode_config.tv_select_subconnector_property)
958 + return 0;
959 +
960 + /*
961 + * Basic connector properties
962 + */
963 + tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
964 + "select subconnector",
965 + ARRAY_SIZE(drm_tv_select_enum_list));
966 + for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
967 + drm_property_add_enum(tv_selector, i,
968 + drm_tv_select_enum_list[i].type,
969 + drm_tv_select_enum_list[i].name);
970 + dev->mode_config.tv_select_subconnector_property = tv_selector;
971 +
972 + tv_subconnector =
973 + drm_property_create(dev, DRM_MODE_PROP_ENUM |
974 + DRM_MODE_PROP_IMMUTABLE, "subconnector",
975 + ARRAY_SIZE(drm_tv_subconnector_enum_list));
976 + for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
977 + drm_property_add_enum(tv_subconnector, i,
978 + drm_tv_subconnector_enum_list[i].type,
979 + drm_tv_subconnector_enum_list[i].name);
980 + dev->mode_config.tv_subconnector_property = tv_subconnector;
981 +
982 + /*
983 + * Other, TV specific properties: margins & TV modes.
984 + */
985 + dev->mode_config.tv_left_margin_property =
986 + drm_property_create(dev, DRM_MODE_PROP_RANGE,
987 + "left margin", 2);
988 + dev->mode_config.tv_left_margin_property->values[0] = 0;
989 + dev->mode_config.tv_left_margin_property->values[1] = 100;
990 +
991 + dev->mode_config.tv_right_margin_property =
992 + drm_property_create(dev, DRM_MODE_PROP_RANGE,
993 + "right margin", 2);
994 + dev->mode_config.tv_right_margin_property->values[0] = 0;
995 + dev->mode_config.tv_right_margin_property->values[1] = 100;
996 +
997 + dev->mode_config.tv_top_margin_property =
998 + drm_property_create(dev, DRM_MODE_PROP_RANGE,
999 + "top margin", 2);
1000 + dev->mode_config.tv_top_margin_property->values[0] = 0;
1001 + dev->mode_config.tv_top_margin_property->values[1] = 100;
1002 +
1003 + dev->mode_config.tv_bottom_margin_property =
1004 + drm_property_create(dev, DRM_MODE_PROP_RANGE,
1005 + "bottom margin", 2);
1006 + dev->mode_config.tv_bottom_margin_property->values[0] = 0;
1007 + dev->mode_config.tv_bottom_margin_property->values[1] = 100;
1008 +
1009 + dev->mode_config.tv_mode_property =
1010 + drm_property_create(dev, DRM_MODE_PROP_ENUM,
1011 + "mode", num_modes);
1012 + for (i = 0; i < num_modes; i++)
1013 + drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1014 + i, modes[i]);
1015 +
1016 + return 0;
1017 +}
1018 +EXPORT_SYMBOL(drm_mode_create_tv_properties);
1019 +
1020 +/**
1021 + * drm_mode_create_scaling_mode_property - create scaling mode property
1022 + * @dev: DRM device
1023 + *
1024 + * Called by a driver the first time it's needed, must be attached to desired
1025 + * connectors.
1026 + */
1027 +int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1028 +{
1029 + struct drm_property *scaling_mode;
1030 + int i;
1031 +
1032 + if (dev->mode_config.scaling_mode_property)
1033 + return 0;
1034 +
1035 + scaling_mode =
1036 + drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1037 + ARRAY_SIZE(drm_scaling_mode_enum_list));
1038 + for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
1039 + drm_property_add_enum(scaling_mode, i,
1040 + drm_scaling_mode_enum_list[i].type,
1041 + drm_scaling_mode_enum_list[i].name);
1042 +
1043 + dev->mode_config.scaling_mode_property = scaling_mode;
1044 +
1045 + return 0;
1046 +}
1047 +EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1048 +
1049 +/**
1050 + * drm_mode_create_dithering_property - create dithering property
1051 + * @dev: DRM device
1052 + *
1053 + * Called by a driver the first time it's needed, must be attached to desired
1054 + * connectors.
1055 + */
1056 +int drm_mode_create_dithering_property(struct drm_device *dev)
1057 +{
1058 + struct drm_property *dithering_mode;
1059 + int i;
1060 +
1061 + if (dev->mode_config.dithering_mode_property)
1062 + return 0;
1063 +
1064 + dithering_mode =
1065 + drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
1066 + ARRAY_SIZE(drm_dithering_mode_enum_list));
1067 + for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
1068 + drm_property_add_enum(dithering_mode, i,
1069 + drm_dithering_mode_enum_list[i].type,
1070 + drm_dithering_mode_enum_list[i].name);
1071 + dev->mode_config.dithering_mode_property = dithering_mode;
1072 +
1073 + return 0;
1074 +}
1075 +EXPORT_SYMBOL(drm_mode_create_dithering_property);
1076 +
1077 +/**
1078 + * drm_mode_config_init - initialize DRM mode_configuration structure
1079 + * @dev: DRM device
1080 + *
1081 + * LOCKING:
1082 + * None, should happen single threaded at init time.
1083 + *
1084 + * Initialize @dev's mode_config structure, used for tracking the graphics
1085 + * configuration of @dev.
1086 + */
1087 +void drm_mode_config_init(struct drm_device *dev)
1088 +{
1089 + mutex_init(&dev->mode_config.mutex);
1090 + INIT_LIST_HEAD(&dev->mode_config.fb_list);
1091 + INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
1092 + INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1093 + INIT_LIST_HEAD(&dev->mode_config.connector_list);
1094 + INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1095 + INIT_LIST_HEAD(&dev->mode_config.property_list);
1096 + INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1097 + idr_init(&dev->mode_config.crtc_idr);
1098 +
1099 + mutex_lock(&dev->mode_config.mutex);
1100 + drm_mode_create_standard_connector_properties(dev);
1101 + mutex_unlock(&dev->mode_config.mutex);
1102 +
1103 + /* Just to be sure */
1104 + dev->mode_config.num_fb = 0;
1105 + dev->mode_config.num_connector = 0;
1106 + dev->mode_config.num_crtc = 0;
1107 + dev->mode_config.num_encoder = 0;
1108 +}
1109 +EXPORT_SYMBOL(drm_mode_config_init);
1110 +
1111 +int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1112 +{
1113 + uint32_t total_objects = 0;
1114 +
1115 + total_objects += dev->mode_config.num_crtc;
1116 + total_objects += dev->mode_config.num_connector;
1117 + total_objects += dev->mode_config.num_encoder;
1118 +
1119 + if (total_objects == 0)
1120 + return -EINVAL;
1121 +
1122 + group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1123 + if (!group->id_list)
1124 + return -ENOMEM;
1125 +
1126 + group->num_crtcs = 0;
1127 + group->num_connectors = 0;
1128 + group->num_encoders = 0;
1129 + return 0;
1130 +}
1131 +
1132 +int drm_mode_group_init_legacy_group(struct drm_device *dev,
1133 + struct drm_mode_group *group)
1134 +{
1135 + struct drm_crtc *crtc;
1136 + struct drm_encoder *encoder;
1137 + struct drm_connector *connector;
1138 + int ret;
1139 +
1140 + if ((ret = drm_mode_group_init(dev, group)))
1141 + return ret;
1142 +
1143 + list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1144 + group->id_list[group->num_crtcs++] = crtc->base.id;
1145 +
1146 + list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1147 + group->id_list[group->num_crtcs + group->num_encoders++] =
1148 + encoder->base.id;
1149 +
1150 + list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1151 + group->id_list[group->num_crtcs + group->num_encoders +
1152 + group->num_connectors++] = connector->base.id;
1153 +
1154 + return 0;
1155 +}
1156 +
1157 +/**
1158 + * drm_mode_config_cleanup - free up DRM mode_config info
1159 + * @dev: DRM device
1160 + *
1161 + * LOCKING:
1162 + * Caller must hold mode config lock.
1163 + *
1164 + * Free up all the connectors and CRTCs associated with this DRM device, then
1165 + * free up the framebuffers and associated buffer objects.
1166 + *
1167 + * FIXME: cleanup any dangling user buffer objects too
1168 + */
1169 +void drm_mode_config_cleanup(struct drm_device *dev)
1170 +{
1171 + struct drm_connector *connector, *ot;
1172 + struct drm_crtc *crtc, *ct;
1173 + struct drm_encoder *encoder, *enct;
1174 + struct drm_framebuffer *fb, *fbt;
1175 + struct drm_property *property, *pt;
1176 +
1177 + list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1178 + head) {
1179 + encoder->funcs->destroy(encoder);
1180 + }
1181 +
1182 + list_for_each_entry_safe(connector, ot,
1183 + &dev->mode_config.connector_list, head) {
1184 + connector->funcs->destroy(connector);
1185 + }
1186 +
1187 + list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1188 + head) {
1189 + drm_property_destroy(dev, property);
1190 + }
1191 +
1192 + list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1193 + fb->funcs->destroy(fb);
1194 + }
1195 +
1196 + list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1197 + crtc->funcs->destroy(crtc);
1198 + }
1199 +
1200 +}
1201 +EXPORT_SYMBOL(drm_mode_config_cleanup);
1202 +
1203 +/**
1204 + * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1205 + * @out: drm_mode_modeinfo struct to return to the user
1206 + * @in: drm_display_mode to use
1207 + *
1208 + * LOCKING:
1209 + * None.
1210 + *
1211 + * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1212 + * the user.
1213 + */
1214 +void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1215 + struct drm_display_mode *in)
1216 +{
1217 + out->clock = in->clock;
1218 + out->hdisplay = in->hdisplay;
1219 + out->hsync_start = in->hsync_start;
1220 + out->hsync_end = in->hsync_end;
1221 + out->htotal = in->htotal;
1222 + out->hskew = in->hskew;
1223 + out->vdisplay = in->vdisplay;
1224 + out->vsync_start = in->vsync_start;
1225 + out->vsync_end = in->vsync_end;
1226 + out->vtotal = in->vtotal;
1227 + out->vscan = in->vscan;
1228 + out->vrefresh = in->vrefresh;
1229 + out->flags = in->flags;
1230 + out->type = in->type;
1231 + strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1232 + out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1233 +}
1234 +
1235 +/**
1236 + * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1237 + * @out: drm_display_mode to return to the user
1238 + * @in: drm_mode_modeinfo to use
1239 + *
1240 + * LOCKING:
1241 + * None.
1242 + *
1243 + * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1244 + * the caller.
1245 + */
1246 +void drm_crtc_convert_umode(struct drm_display_mode *out,
1247 + struct drm_mode_modeinfo *in)
1248 +{
1249 + out->clock = in->clock;
1250 + out->hdisplay = in->hdisplay;
1251 + out->hsync_start = in->hsync_start;
1252 + out->hsync_end = in->hsync_end;
1253 + out->htotal = in->htotal;
1254 + out->hskew = in->hskew;
1255 + out->vdisplay = in->vdisplay;
1256 + out->vsync_start = in->vsync_start;
1257 + out->vsync_end = in->vsync_end;
1258 + out->vtotal = in->vtotal;
1259 + out->vscan = in->vscan;
1260 + out->vrefresh = in->vrefresh;
1261 + out->flags = in->flags;
1262 + out->type = in->type;
1263 + strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1264 + out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1265 +}
1266 +
1267 +/**
1268 + * drm_mode_getresources - get graphics configuration
1269 + * @inode: inode from the ioctl
1270 + * @filp: file * from the ioctl
1271 + * @cmd: cmd from ioctl
1272 + * @arg: arg from ioctl
1273 + *
1274 + * LOCKING:
1275 + * Takes mode config lock.
1276 + *
1277 + * Construct a set of configuration description structures and return
1278 + * them to the user, including CRTC, connector and framebuffer configuration.
1279 + *
1280 + * Called by the user via ioctl.
1281 + *
1282 + * RETURNS:
1283 + * Zero on success, errno on failure.
1284 + */
1285 +int drm_mode_getresources(struct drm_device *dev, void *data,
1286 + struct drm_file *file_priv)
1287 +{
1288 + struct drm_mode_card_res *card_res = data;
1289 + struct list_head *lh;
1290 + struct drm_framebuffer *fb;
1291 + struct drm_connector *connector;
1292 + struct drm_crtc *crtc;
1293 + struct drm_encoder *encoder;
1294 + int ret = 0;
1295 + int connector_count = 0;
1296 + int crtc_count = 0;
1297 + int fb_count = 0;
1298 + int encoder_count = 0;
1299 + int copied = 0, i;
1300 + uint32_t __user *fb_id;
1301 + uint32_t __user *crtc_id;
1302 + uint32_t __user *connector_id;
1303 + uint32_t __user *encoder_id;
1304 + struct drm_mode_group *mode_group;
1305 +
1306 + mutex_lock(&dev->mode_config.mutex);
1307 +
1308 + /*
1309 + * For the non-control nodes we need to limit the list of resources
1310 + * by IDs in the group list for this node
1311 + */
1312 + list_for_each(lh, &file_priv->fbs)
1313 + fb_count++;
1314 +
1315 + mode_group = &file_priv->master->minor->mode_group;
1316 + if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1317 +
1318 + list_for_each(lh, &dev->mode_config.crtc_list)
1319 + crtc_count++;
1320 +
1321 + list_for_each(lh, &dev->mode_config.connector_list)
1322 + connector_count++;
1323 +
1324 + list_for_each(lh, &dev->mode_config.encoder_list)
1325 + encoder_count++;
1326 + } else {
1327 +
1328 + crtc_count = mode_group->num_crtcs;
1329 + connector_count = mode_group->num_connectors;
1330 + encoder_count = mode_group->num_encoders;
1331 + }
1332 +
1333 + card_res->max_height = dev->mode_config.max_height;
1334 + card_res->min_height = dev->mode_config.min_height;
1335 + card_res->max_width = dev->mode_config.max_width;
1336 + card_res->min_width = dev->mode_config.min_width;
1337 +
1338 + /* handle this in 4 parts */
1339 + /* FBs */
1340 + if (card_res->count_fbs >= fb_count) {
1341 + copied = 0;
1342 + fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1343 + list_for_each_entry(fb, &file_priv->fbs, head) {
1344 + if (put_user(fb->base.id, fb_id + copied)) {
1345 + ret = -EFAULT;
1346 + goto out;
1347 + }
1348 + copied++;
1349 + }
1350 + }
1351 + card_res->count_fbs = fb_count;
1352 +
1353 + /* CRTCs */
1354 + if (card_res->count_crtcs >= crtc_count) {
1355 + copied = 0;
1356 + crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1357 + if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1358 + list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1359 + head) {
1360 + DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
1361 + if (put_user(crtc->base.id, crtc_id + copied)) {
1362 + ret = -EFAULT;
1363 + goto out;
1364 + }
1365 + copied++;
1366 + }
1367 + } else {
1368 + for (i = 0; i < mode_group->num_crtcs; i++) {
1369 + if (put_user(mode_group->id_list[i],
1370 + crtc_id + copied)) {
1371 + ret = -EFAULT;
1372 + goto out;
1373 + }
1374 + copied++;
1375 + }
1376 + }
1377 + }
1378 + card_res->count_crtcs = crtc_count;
1379 +
1380 + /* Encoders */
1381 + if (card_res->count_encoders >= encoder_count) {
1382 + copied = 0;
1383 + encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1384 + if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1385 + list_for_each_entry(encoder,
1386 + &dev->mode_config.encoder_list,
1387 + head) {
1388 + DRM_DEBUG("ENCODER ID is %d\n",
1389 + encoder->base.id);
1390 + if (put_user(encoder->base.id, encoder_id +
1391 + copied)) {
1392 + ret = -EFAULT;
1393 + goto out;
1394 + }
1395 + copied++;
1396 + }
1397 + } else {
1398 + for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1399 + if (put_user(mode_group->id_list[i],
1400 + encoder_id + copied)) {
1401 + ret = -EFAULT;
1402 + goto out;
1403 + }
1404 + copied++;
1405 + }
1406 +
1407 + }
1408 + }
1409 + card_res->count_encoders = encoder_count;
1410 +
1411 + /* Connectors */
1412 + if (card_res->count_connectors >= connector_count) {
1413 + copied = 0;
1414 + connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1415 + if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1416 + list_for_each_entry(connector,
1417 + &dev->mode_config.connector_list,
1418 + head) {
1419 + DRM_DEBUG("CONNECTOR ID is %d\n",
1420 + connector->base.id);
1421 + if (put_user(connector->base.id,
1422 + connector_id + copied)) {
1423 + ret = -EFAULT;
1424 + goto out;
1425 + }
1426 + copied++;
1427 + }
1428 + } else {
1429 + int start = mode_group->num_crtcs +
1430 + mode_group->num_encoders;
1431 + for (i = start; i < start + mode_group->num_connectors; i++) {
1432 + if (put_user(mode_group->id_list[i],
1433 + connector_id + copied)) {
1434 + ret = -EFAULT;
1435 + goto out;
1436 + }
1437 + copied++;
1438 + }
1439 + }
1440 + }
1441 + card_res->count_connectors = connector_count;
1442 +
1443 + DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1444 + card_res->count_connectors, card_res->count_encoders);
1445 +
1446 +out:
1447 + mutex_unlock(&dev->mode_config.mutex);
1448 + return ret;
1449 +}
1450 +
1451 +/**
1452 + * drm_mode_getcrtc - get CRTC configuration
1453 + * @inode: inode from the ioctl
1454 + * @filp: file * from the ioctl
1455 + * @cmd: cmd from ioctl
1456 + * @arg: arg from ioctl
1457 + *
1458 + * LOCKING:
1459 + * Caller? (FIXME)
1460 + *
1461 + * Construct a CRTC configuration structure to return to the user.
1462 + *
1463 + * Called by the user via ioctl.
1464 + *
1465 + * RETURNS:
1466 + * Zero on success, errno on failure.
1467 + */
1468 +int drm_mode_getcrtc(struct drm_device *dev,
1469 + void *data, struct drm_file *file_priv)
1470 +{
1471 + struct drm_mode_crtc *crtc_resp = data;
1472 + struct drm_crtc *crtc;
1473 + struct drm_mode_object *obj;
1474 + int ret = 0;
1475 +
1476 + mutex_lock(&dev->mode_config.mutex);
1477 +
1478 + obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1479 + DRM_MODE_OBJECT_CRTC);
1480 + if (!obj) {
1481 + ret = -EINVAL;
1482 + goto out;
1483 + }
1484 + crtc = obj_to_crtc(obj);
1485 +
1486 + crtc_resp->x = crtc->x;
1487 + crtc_resp->y = crtc->y;
1488 + crtc_resp->gamma_size = crtc->gamma_size;
1489 + if (crtc->fb)
1490 + crtc_resp->fb_id = crtc->fb->base.id;
1491 + else
1492 + crtc_resp->fb_id = 0;
1493 +
1494 + if (crtc->enabled) {
1495 +
1496 + drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1497 + crtc_resp->mode_valid = 1;
1498 +
1499 + } else {
1500 + crtc_resp->mode_valid = 0;
1501 + }
1502 +
1503 +out:
1504 + mutex_unlock(&dev->mode_config.mutex);
1505 + return ret;
1506 +}
1507 +
1508 +/**
1509 + * drm_mode_getconnector - get connector configuration
1510 + * @inode: inode from the ioctl
1511 + * @filp: file * from the ioctl
1512 + * @cmd: cmd from ioctl
1513 + * @arg: arg from ioctl
1514 + *
1515 + * LOCKING:
1516 + * Caller? (FIXME)
1517 + *
1518 + * Construct a connector configuration structure to return to the user.
1519 + *
1520 + * Called by the user via ioctl.
1521 + *
1522 + * RETURNS:
1523 + * Zero on success, errno on failure.
1524 + */
1525 +int drm_mode_getconnector(struct drm_device *dev, void *data,
1526 + struct drm_file *file_priv)
1527 +{
1528 + struct drm_mode_get_connector *out_resp = data;
1529 + struct drm_mode_object *obj;
1530 + struct drm_connector *connector;
1531 + struct drm_display_mode *mode;
1532 + int mode_count = 0;
1533 + int props_count = 0;
1534 + int encoders_count = 0;
1535 + int ret = 0;
1536 + int copied = 0;
1537 + int i;
1538 + struct drm_mode_modeinfo u_mode;
1539 + struct drm_mode_modeinfo __user *mode_ptr;
1540 + uint32_t __user *prop_ptr;
1541 + uint64_t __user *prop_values;
1542 + uint32_t __user *encoder_ptr;
1543 +
1544 + memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1545 +
1546 + DRM_DEBUG("connector id %d:\n", out_resp->connector_id);
1547 +
1548 + mutex_lock(&dev->mode_config.mutex);
1549 +
1550 + obj = drm_mode_object_find(dev, out_resp->connector_id,
1551 + DRM_MODE_OBJECT_CONNECTOR);
1552 + if (!obj) {
1553 + ret = -EINVAL;
1554 + goto out;
1555 + }
1556 + connector = obj_to_connector(obj);
1557 +
1558 + for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1559 + if (connector->property_ids[i] != 0) {
1560 + props_count++;
1561 + }
1562 + }
1563 +
1564 + for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1565 + if (connector->encoder_ids[i] != 0) {
1566 + encoders_count++;
1567 + }
1568 + }
1569 +
1570 + if (out_resp->count_modes == 0) {
1571 + connector->funcs->fill_modes(connector,
1572 + dev->mode_config.max_width,
1573 + dev->mode_config.max_height);
1574 + }
1575 +
1576 + /* delayed so we get modes regardless of pre-fill_modes state */
1577 + list_for_each_entry(mode, &connector->modes, head)
1578 + mode_count++;
1579 +
1580 + out_resp->connector_id = connector->base.id;
1581 + out_resp->connector_type = connector->connector_type;
1582 + out_resp->connector_type_id = connector->connector_type_id;
1583 + out_resp->mm_width = connector->display_info.width_mm;
1584 + out_resp->mm_height = connector->display_info.height_mm;
1585 + out_resp->subpixel = connector->display_info.subpixel_order;
1586 + out_resp->connection = connector->status;
1587 + if (connector->encoder)
1588 + out_resp->encoder_id = connector->encoder->base.id;
1589 + else
1590 + out_resp->encoder_id = 0;
1591 +
1592 + /*
1593 + * This ioctl is called twice, once to determine how much space is
1594 + * needed, and the 2nd time to fill it.
1595 + */
1596 + if ((out_resp->count_modes >= mode_count) && mode_count) {
1597 + copied = 0;
1598 + mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1599 + list_for_each_entry(mode, &connector->modes, head) {
1600 + drm_crtc_convert_to_umode(&u_mode, mode);
1601 + if (copy_to_user(mode_ptr + copied,
1602 + &u_mode, sizeof(u_mode))) {
1603 + ret = -EFAULT;
1604 + goto out;
1605 + }
1606 + copied++;
1607 + }
1608 + }
1609 + out_resp->count_modes = mode_count;
1610 +
1611 + if ((out_resp->count_props >= props_count) && props_count) {
1612 + copied = 0;
1613 + prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1614 + prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1615 + for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1616 + if (connector->property_ids[i] != 0) {
1617 + if (put_user(connector->property_ids[i],
1618 + prop_ptr + copied)) {
1619 + ret = -EFAULT;
1620 + goto out;
1621 + }
1622 +
1623 + if (put_user(connector->property_values[i],
1624 + prop_values + copied)) {
1625 + ret = -EFAULT;
1626 + goto out;
1627 + }
1628 + copied++;
1629 + }
1630 + }
1631 + }
1632 + out_resp->count_props = props_count;
1633 +
1634 + if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1635 + copied = 0;
1636 + encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1637 + for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1638 + if (connector->encoder_ids[i] != 0) {
1639 + if (put_user(connector->encoder_ids[i],
1640 + encoder_ptr + copied)) {
1641 + ret = -EFAULT;
1642 + goto out;
1643 + }
1644 + copied++;
1645 + }
1646 + }
1647 + }
1648 + out_resp->count_encoders = encoders_count;
1649 +
1650 +out:
1651 + mutex_unlock(&dev->mode_config.mutex);
1652 + return ret;
1653 +}
1654 +
1655 +int drm_mode_getencoder(struct drm_device *dev, void *data,
1656 + struct drm_file *file_priv)
1657 +{
1658 + struct drm_mode_get_encoder *enc_resp = data;
1659 + struct drm_mode_object *obj;
1660 + struct drm_encoder *encoder;
1661 + int ret = 0;
1662 +
1663 + mutex_lock(&dev->mode_config.mutex);
1664 + obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1665 + DRM_MODE_OBJECT_ENCODER);
1666 + if (!obj) {
1667 + ret = -EINVAL;
1668 + goto out;
1669 + }
1670 + encoder = obj_to_encoder(obj);
1671 +
1672 + if (encoder->crtc)
1673 + enc_resp->crtc_id = encoder->crtc->base.id;
1674 + else
1675 + enc_resp->crtc_id = 0;
1676 + enc_resp->encoder_type = encoder->encoder_type;
1677 + enc_resp->encoder_id = encoder->base.id;
1678 + enc_resp->possible_crtcs = encoder->possible_crtcs;
1679 + enc_resp->possible_clones = encoder->possible_clones;
1680 +
1681 +out:
1682 + mutex_unlock(&dev->mode_config.mutex);
1683 + return ret;
1684 +}
1685 +
1686 +/**
1687 + * drm_mode_setcrtc - set CRTC configuration
1688 + * @inode: inode from the ioctl
1689 + * @filp: file * from the ioctl
1690 + * @cmd: cmd from ioctl
1691 + * @arg: arg from ioctl
1692 + *
1693 + * LOCKING:
1694 + * Caller? (FIXME)
1695 + *
1696 + * Build a new CRTC configuration based on user request.
1697 + *
1698 + * Called by the user via ioctl.
1699 + *
1700 + * RETURNS:
1701 + * Zero on success, errno on failure.
1702 + */
1703 +int drm_mode_setcrtc(struct drm_device *dev, void *data,
1704 + struct drm_file *file_priv)
1705 +{
1706 + struct drm_mode_config *config = &dev->mode_config;
1707 + struct drm_mode_crtc *crtc_req = data;
1708 + struct drm_mode_object *obj;
1709 + struct drm_crtc *crtc, *crtcfb;
1710 + struct drm_connector **connector_set = NULL, *connector;
1711 + struct drm_framebuffer *fb = NULL;
1712 + struct drm_display_mode *mode = NULL;
1713 + struct drm_mode_set set;
1714 + uint32_t __user *set_connectors_ptr;
1715 + int ret = 0;
1716 + int i;
1717 +
1718 + mutex_lock(&dev->mode_config.mutex);
1719 + obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1720 + DRM_MODE_OBJECT_CRTC);
1721 + if (!obj) {
1722 + DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1723 + ret = -EINVAL;
1724 + goto out;
1725 + }
1726 + crtc = obj_to_crtc(obj);
1727 +
1728 + if (crtc_req->mode_valid) {
1729 + /* If we have a mode we need a framebuffer. */
1730 + /* If we pass -1, set the mode with the currently bound fb */
1731 + if (crtc_req->fb_id == -1) {
1732 + list_for_each_entry(crtcfb,
1733 + &dev->mode_config.crtc_list, head) {
1734 + if (crtcfb == crtc) {
1735 + DRM_DEBUG("Using current fb for setmode\n");
1736 + fb = crtc->fb;
1737 + }
1738 + }
1739 + } else {
1740 + obj = drm_mode_object_find(dev, crtc_req->fb_id,
1741 + DRM_MODE_OBJECT_FB);
1742 + if (!obj) {
1743 + DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1744 + ret = -EINVAL;
1745 + goto out;
1746 + }
1747 + fb = obj_to_fb(obj);
1748 + }
1749 +
1750 + mode = drm_mode_create(dev);
1751 + drm_crtc_convert_umode(mode, &crtc_req->mode);
1752 + drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1753 + }
1754 +
1755 + if (crtc_req->count_connectors == 0 && mode) {
1756 + DRM_DEBUG("Count connectors is 0 but mode set\n");
1757 + ret = -EINVAL;
1758 + goto out;
1759 + }
1760 +
1761 + if (crtc_req->count_connectors > 0 && !mode && !fb) {
1762 + DRM_DEBUG("Count connectors is %d but no mode or fb set\n",
1763 + crtc_req->count_connectors);
1764 + ret = -EINVAL;
1765 + goto out;
1766 + }
1767 +
1768 + if (crtc_req->count_connectors > 0) {
1769 + u32 out_id;
1770 +
1771 + /* Avoid unbounded kernel memory allocation */
1772 + if (crtc_req->count_connectors > config->num_connector) {
1773 + ret = -EINVAL;
1774 + goto out;
1775 + }
1776 +
1777 + connector_set = kmalloc(crtc_req->count_connectors *
1778 + sizeof(struct drm_connector *),
1779 + GFP_KERNEL);
1780 + if (!connector_set) {
1781 + ret = -ENOMEM;
1782 + goto out;
1783 + }
1784 +
1785 + for (i = 0; i < crtc_req->count_connectors; i++) {
1786 + set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1787 + if (get_user(out_id, &set_connectors_ptr[i])) {
1788 + ret = -EFAULT;
1789 + goto out;
1790 + }
1791 +
1792 + obj = drm_mode_object_find(dev, out_id,
1793 + DRM_MODE_OBJECT_CONNECTOR);
1794 + if (!obj) {
1795 + DRM_DEBUG("Connector id %d unknown\n", out_id);
1796 + ret = -EINVAL;
1797 + goto out;
1798 + }
1799 + connector = obj_to_connector(obj);
1800 +
1801 + connector_set[i] = connector;
1802 + }
1803 + }
1804 +
1805 + set.crtc = crtc;
1806 + set.x = crtc_req->x;
1807 + set.y = crtc_req->y;
1808 + set.mode = mode;
1809 + set.connectors = connector_set;
1810 + set.num_connectors = crtc_req->count_connectors;
1811 + set.fb =fb;
1812 + ret = crtc->funcs->set_config(&set);
1813 +
1814 +out:
1815 + kfree(connector_set);
1816 + mutex_unlock(&dev->mode_config.mutex);
1817 + return ret;
1818 +}
1819 +
1820 +int drm_mode_cursor_ioctl(struct drm_device *dev,
1821 + void *data, struct drm_file *file_priv)
1822 +{
1823 + struct drm_mode_cursor *req = data;
1824 + struct drm_mode_object *obj;
1825 + struct drm_crtc *crtc;
1826 + int ret = 0;
1827 +
1828 + DRM_DEBUG("\n");
1829 +
1830 + if (!req->flags) {
1831 + DRM_ERROR("no operation set\n");
1832 + return -EINVAL;
1833 + }
1834 +
1835 + mutex_lock(&dev->mode_config.mutex);
1836 + obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1837 + if (!obj) {
1838 + DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc_id);
1839 + ret = -EINVAL;
1840 + goto out;
1841 + }
1842 + crtc = obj_to_crtc(obj);
1843 +
1844 + if (req->flags & DRM_MODE_CURSOR_BO) {
1845 + if (!crtc->funcs->cursor_set) {
1846 + DRM_ERROR("crtc does not support cursor\n");
1847 + ret = -ENXIO;
1848 + goto out;
1849 + }
1850 + /* Turns off the cursor if handle is 0 */
1851 + ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1852 + req->width, req->height);
1853 + }
1854 +
1855 + if (req->flags & DRM_MODE_CURSOR_MOVE) {
1856 + if (crtc->funcs->cursor_move) {
1857 + ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1858 + } else {
1859 + DRM_ERROR("crtc does not support cursor\n");
1860 + ret = -EFAULT;
1861 + goto out;
1862 + }
1863 + }
1864 +out:
1865 + mutex_unlock(&dev->mode_config.mutex);
1866 + return ret;
1867 +}
1868 +
1869 +/**
1870 + * drm_mode_addfb - add an FB to the graphics configuration
1871 + * @inode: inode from the ioctl
1872 + * @filp: file * from the ioctl
1873 + * @cmd: cmd from ioctl
1874 + * @arg: arg from ioctl
1875 + *
1876 + * LOCKING:
1877 + * Takes mode config lock.
1878 + *
1879 + * Add a new FB to the specified CRTC, given a user request.
1880 + *
1881 + * Called by the user via ioctl.
1882 + *
1883 + * RETURNS:
1884 + * Zero on success, errno on failure.
1885 + */
1886 +int drm_mode_addfb(struct drm_device *dev,
1887 + void *data, struct drm_file *file_priv)
1888 +{
1889 + struct drm_mode_fb_cmd *r = data;
1890 + struct drm_mode_config *config = &dev->mode_config;
1891 + struct drm_framebuffer *fb;
1892 + int ret = 0;
1893 +
1894 + if ((config->min_width > r->width) || (r->width > config->max_width)) {
1895 + DRM_ERROR("mode new framebuffer width not within limits\n");
1896 + return -EINVAL;
1897 + }
1898 + if ((config->min_height > r->height) || (r->height > config->max_height)) {
1899 + DRM_ERROR("mode new framebuffer height not within limits\n");
1900 + return -EINVAL;
1901 + }
1902 +
1903 + mutex_lock(&dev->mode_config.mutex);
1904 +
1905 + /* TODO check buffer is sufficently large */
1906 + /* TODO setup destructor callback */
1907 +
1908 + fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1909 + if (!fb) {
1910 + DRM_ERROR("could not create framebuffer\n");
1911 + ret = -EINVAL;
1912 + goto out;
1913 + }
1914 +
1915 + r->fb_id = fb->base.id;
1916 + list_add(&fb->filp_head, &file_priv->fbs);
1917 +
1918 +out:
1919 + mutex_unlock(&dev->mode_config.mutex);
1920 + return ret;
1921 +}
1922 +
1923 +/**
1924 + * drm_mode_rmfb - remove an FB from the configuration
1925 + * @inode: inode from the ioctl
1926 + * @filp: file * from the ioctl
1927 + * @cmd: cmd from ioctl
1928 + * @arg: arg from ioctl
1929 + *
1930 + * LOCKING:
1931 + * Takes mode config lock.
1932 + *
1933 + * Remove the FB specified by the user.
1934 + *
1935 + * Called by the user via ioctl.
1936 + *
1937 + * RETURNS:
1938 + * Zero on success, errno on failure.
1939 + */
1940 +int drm_mode_rmfb(struct drm_device *dev,
1941 + void *data, struct drm_file *file_priv)
1942 +{
1943 + struct drm_mode_object *obj;
1944 + struct drm_framebuffer *fb = NULL;
1945 + struct drm_framebuffer *fbl = NULL;
1946 + uint32_t *id = data;
1947 + int ret = 0;
1948 + int found = 0;
1949 +
1950 + mutex_lock(&dev->mode_config.mutex);
1951 + obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1952 + /* TODO check that we realy get a framebuffer back. */
1953 + if (!obj) {
1954 + DRM_ERROR("mode invalid framebuffer id\n");
1955 + ret = -EINVAL;
1956 + goto out;
1957 + }
1958 + fb = obj_to_fb(obj);
1959 +
1960 + list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1961 + if (fb == fbl)
1962 + found = 1;
1963 +
1964 + if (!found) {
1965 + DRM_ERROR("tried to remove a fb that we didn't own\n");
1966 + ret = -EINVAL;
1967 + goto out;
1968 + }
1969 +
1970 + /* TODO release all crtc connected to the framebuffer */
1971 + /* TODO unhock the destructor from the buffer object */
1972 +
1973 + list_del(&fb->filp_head);
1974 + fb->funcs->destroy(fb);
1975 +
1976 +out:
1977 + mutex_unlock(&dev->mode_config.mutex);
1978 + return ret;
1979 +}
1980 +
1981 +/**
1982 + * drm_mode_getfb - get FB info
1983 + * @inode: inode from the ioctl
1984 + * @filp: file * from the ioctl
1985 + * @cmd: cmd from ioctl
1986 + * @arg: arg from ioctl
1987 + *
1988 + * LOCKING:
1989 + * Caller? (FIXME)
1990 + *
1991 + * Lookup the FB given its ID and return info about it.
1992 + *
1993 + * Called by the user via ioctl.
1994 + *
1995 + * RETURNS:
1996 + * Zero on success, errno on failure.
1997 + */
1998 +int drm_mode_getfb(struct drm_device *dev,
1999 + void *data, struct drm_file *file_priv)
2000 +{
2001 + struct drm_mode_fb_cmd *r = data;
2002 + struct drm_mode_object *obj;
2003 + struct drm_framebuffer *fb;
2004 + int ret = 0;
2005 +
2006 + mutex_lock(&dev->mode_config.mutex);
2007 + obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2008 + if (!obj) {
2009 + DRM_ERROR("invalid framebuffer id\n");
2010 + ret = -EINVAL;
2011 + goto out;
2012 + }
2013 + fb = obj_to_fb(obj);
2014 +
2015 + r->height = fb->height;
2016 + r->width = fb->width;
2017 + r->depth = fb->depth;
2018 + r->bpp = fb->bits_per_pixel;
2019 + r->pitch = fb->pitch;
2020 + fb->funcs->create_handle(fb, file_priv, &r->handle);
2021 +
2022 +out:
2023 + mutex_unlock(&dev->mode_config.mutex);
2024 + return ret;
2025 +}
2026 +
2027 +/**
2028 + * drm_fb_release - remove and free the FBs on this file
2029 + * @filp: file * from the ioctl
2030 + *
2031 + * LOCKING:
2032 + * Takes mode config lock.
2033 + *
2034 + * Destroy all the FBs associated with @filp.
2035 + *
2036 + * Called by the user via ioctl.
2037 + *
2038 + * RETURNS:
2039 + * Zero on success, errno on failure.
2040 + */
2041 +void drm_fb_release(struct file *filp)
2042 +{
2043 + struct drm_file *priv = filp->private_data;
2044 + struct drm_device *dev = priv->minor->dev;
2045 + struct drm_framebuffer *fb, *tfb;
2046 +