drm/kms/fb: use slow work mechanism for normal hotplug also.
[safe/jmp/linux-2.6] / drivers / gpu / drm / drm_fb_helper.c
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5  *
6  * DRM framebuffer helper 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  *      Dave Airlie <airlied@linux.ie>
28  *      Jesse Barnes <jesse.barnes@intel.com>
29  */
30 #include <linux/kernel.h>
31 #include <linux/sysrq.h>
32 #include <linux/fb.h>
33 #include "drmP.h"
34 #include "drm_crtc.h"
35 #include "drm_fb_helper.h"
36 #include "drm_crtc_helper.h"
37
38 MODULE_AUTHOR("David Airlie, Jesse Barnes");
39 MODULE_DESCRIPTION("DRM KMS helper");
40 MODULE_LICENSE("GPL and additional rights");
41
42 static LIST_HEAD(kernel_fb_helper_list);
43
44 static struct slow_work_ops output_status_change_ops;
45
46 /* simple single crtc case helper function */
47 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
48 {
49         struct drm_device *dev = fb_helper->dev;
50         struct drm_connector *connector;
51         int i;
52
53         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
54                 struct drm_fb_helper_connector *fb_helper_connector;
55
56                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
57                 if (!fb_helper_connector)
58                         goto fail;
59
60                 fb_helper_connector->connector = connector;
61                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
62         }
63         return 0;
64 fail:
65         for (i = 0; i < fb_helper->connector_count; i++) {
66                 kfree(fb_helper->connector_info[i]);
67                 fb_helper->connector_info[i] = NULL;
68         }
69         fb_helper->connector_count = 0;
70         return -ENOMEM;
71 }
72 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
73
74 /**
75  * drm_fb_helper_connector_parse_command_line - parse command line for connector
76  * @connector - connector to parse line for
77  * @mode_option - per connector mode option
78  *
79  * This parses the connector specific then generic command lines for
80  * modes and options to configure the connector.
81  *
82  * This uses the same parameters as the fb modedb.c, except for extra
83  *      <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
84  *
85  * enable/enable Digital/disable bit at the end
86  */
87 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
88                                                        const char *mode_option)
89 {
90         const char *name;
91         unsigned int namelen;
92         int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
93         unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
94         int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
95         int i;
96         enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
97         struct drm_fb_helper_cmdline_mode *cmdline_mode;
98         struct drm_connector *connector = fb_helper_conn->connector;
99
100         if (!fb_helper_conn)
101                 return false;
102
103         cmdline_mode = &fb_helper_conn->cmdline_mode;
104         if (!mode_option)
105                 mode_option = fb_mode_option;
106
107         if (!mode_option) {
108                 cmdline_mode->specified = false;
109                 return false;
110         }
111
112         name = mode_option;
113         namelen = strlen(name);
114         for (i = namelen-1; i >= 0; i--) {
115                 switch (name[i]) {
116                 case '@':
117                         namelen = i;
118                         if (!refresh_specified && !bpp_specified &&
119                             !yres_specified) {
120                                 refresh = simple_strtol(&name[i+1], NULL, 10);
121                                 refresh_specified = 1;
122                                 if (cvt || rb)
123                                         cvt = 0;
124                         } else
125                                 goto done;
126                         break;
127                 case '-':
128                         namelen = i;
129                         if (!bpp_specified && !yres_specified) {
130                                 bpp = simple_strtol(&name[i+1], NULL, 10);
131                                 bpp_specified = 1;
132                                 if (cvt || rb)
133                                         cvt = 0;
134                         } else
135                                 goto done;
136                         break;
137                 case 'x':
138                         if (!yres_specified) {
139                                 yres = simple_strtol(&name[i+1], NULL, 10);
140                                 yres_specified = 1;
141                         } else
142                                 goto done;
143                 case '0' ... '9':
144                         break;
145                 case 'M':
146                         if (!yres_specified)
147                                 cvt = 1;
148                         break;
149                 case 'R':
150                         if (!cvt)
151                                 rb = 1;
152                         break;
153                 case 'm':
154                         if (!cvt)
155                                 margins = 1;
156                         break;
157                 case 'i':
158                         if (!cvt)
159                                 interlace = 1;
160                         break;
161                 case 'e':
162                         force = DRM_FORCE_ON;
163                         break;
164                 case 'D':
165                         if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
166                             (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
167                                 force = DRM_FORCE_ON;
168                         else
169                                 force = DRM_FORCE_ON_DIGITAL;
170                         break;
171                 case 'd':
172                         force = DRM_FORCE_OFF;
173                         break;
174                 default:
175                         goto done;
176                 }
177         }
178         if (i < 0 && yres_specified) {
179                 xres = simple_strtol(name, NULL, 10);
180                 res_specified = 1;
181         }
182 done:
183
184         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
185                 drm_get_connector_name(connector), xres, yres,
186                 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
187                 "", (margins) ? " with margins" : "", (interlace) ?
188                 " interlaced" : "");
189
190         if (force) {
191                 const char *s;
192                 switch (force) {
193                 case DRM_FORCE_OFF: s = "OFF"; break;
194                 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
195                 default:
196                 case DRM_FORCE_ON: s = "ON"; break;
197                 }
198
199                 DRM_INFO("forcing %s connector %s\n",
200                          drm_get_connector_name(connector), s);
201                 connector->force = force;
202         }
203
204         if (res_specified) {
205                 cmdline_mode->specified = true;
206                 cmdline_mode->xres = xres;
207                 cmdline_mode->yres = yres;
208         }
209
210         if (refresh_specified) {
211                 cmdline_mode->refresh_specified = true;
212                 cmdline_mode->refresh = refresh;
213         }
214
215         if (bpp_specified) {
216                 cmdline_mode->bpp_specified = true;
217                 cmdline_mode->bpp = bpp;
218         }
219         cmdline_mode->rb = rb ? true : false;
220         cmdline_mode->cvt = cvt  ? true : false;
221         cmdline_mode->interlace = interlace ? true : false;
222
223         return true;
224 }
225
226 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
227 {
228         struct drm_fb_helper_connector *fb_helper_conn;
229         int i;
230
231         for (i = 0; i < fb_helper->connector_count; i++) {
232                 char *option = NULL;
233
234                 fb_helper_conn = fb_helper->connector_info[i];
235
236                 /* do something on return - turn off connector maybe */
237                 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
238                         continue;
239
240                 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
241         }
242         return 0;
243 }
244
245 bool drm_fb_helper_force_kernel_mode(void)
246 {
247         int i = 0;
248         bool ret, error = false;
249         struct drm_fb_helper *helper;
250
251         if (list_empty(&kernel_fb_helper_list))
252                 return false;
253
254         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
255                 for (i = 0; i < helper->crtc_count; i++) {
256                         struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
257                         ret = drm_crtc_helper_set_config(mode_set);
258                         if (ret)
259                                 error = true;
260                 }
261         }
262         return error;
263 }
264
265 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
266                         void *panic_str)
267 {
268         DRM_ERROR("panic occurred, switching back to text console\n");
269         return drm_fb_helper_force_kernel_mode();
270         return 0;
271 }
272 EXPORT_SYMBOL(drm_fb_helper_panic);
273
274 static struct notifier_block paniced = {
275         .notifier_call = drm_fb_helper_panic,
276 };
277
278 /**
279  * drm_fb_helper_restore - restore the framebuffer console (kernel) config
280  *
281  * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
282  */
283 void drm_fb_helper_restore(void)
284 {
285         bool ret;
286         ret = drm_fb_helper_force_kernel_mode();
287         if (ret == true)
288                 DRM_ERROR("Failed to restore crtc configuration\n");
289 }
290 EXPORT_SYMBOL(drm_fb_helper_restore);
291
292 #ifdef CONFIG_MAGIC_SYSRQ
293 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
294 {
295         drm_fb_helper_restore();
296 }
297 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
298
299 static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
300 {
301         schedule_work(&drm_fb_helper_restore_work);
302 }
303
304 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
305         .handler = drm_fb_helper_sysrq,
306         .help_msg = "force-fb(V)",
307         .action_msg = "Restore framebuffer console",
308 };
309 #else
310 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
311 #endif
312
313 static void drm_fb_helper_on(struct fb_info *info)
314 {
315         struct drm_fb_helper *fb_helper = info->par;
316         struct drm_device *dev = fb_helper->dev;
317         struct drm_crtc *crtc;
318         struct drm_crtc_helper_funcs *crtc_funcs;
319         struct drm_encoder *encoder;
320         int i;
321
322         /*
323          * For each CRTC in this fb, turn the crtc on then,
324          * find all associated encoders and turn them on.
325          */
326         mutex_lock(&dev->mode_config.mutex);
327         for (i = 0; i < fb_helper->crtc_count; i++) {
328                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
329                 crtc_funcs = crtc->helper_private;
330
331                 if (!crtc->enabled)
332                         continue;
333
334                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
335
336
337                 /* Found a CRTC on this fb, now find encoders */
338                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
339                         if (encoder->crtc == crtc) {
340                                 struct drm_encoder_helper_funcs *encoder_funcs;
341
342                                 encoder_funcs = encoder->helper_private;
343                                 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
344                         }
345                 }
346         }
347         mutex_unlock(&dev->mode_config.mutex);
348 }
349
350 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
351 {
352         struct drm_fb_helper *fb_helper = info->par;
353         struct drm_device *dev = fb_helper->dev;
354         struct drm_crtc *crtc;
355         struct drm_crtc_helper_funcs *crtc_funcs;
356         struct drm_encoder *encoder;
357         int i;
358
359         /*
360          * For each CRTC in this fb, find all associated encoders
361          * and turn them off, then turn off the CRTC.
362          */
363         mutex_lock(&dev->mode_config.mutex);
364         for (i = 0; i < fb_helper->crtc_count; i++) {
365                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
366                 crtc_funcs = crtc->helper_private;
367
368                 if (!crtc->enabled)
369                         continue;
370
371                 /* Found a CRTC on this fb, now find encoders */
372                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
373                         if (encoder->crtc == crtc) {
374                                 struct drm_encoder_helper_funcs *encoder_funcs;
375
376                                 encoder_funcs = encoder->helper_private;
377                                 encoder_funcs->dpms(encoder, dpms_mode);
378                         }
379                 }
380                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
381         }
382         mutex_unlock(&dev->mode_config.mutex);
383 }
384
385 int drm_fb_helper_blank(int blank, struct fb_info *info)
386 {
387         switch (blank) {
388         /* Display: On; HSync: On, VSync: On */
389         case FB_BLANK_UNBLANK:
390                 drm_fb_helper_on(info);
391                 break;
392         /* Display: Off; HSync: On, VSync: On */
393         case FB_BLANK_NORMAL:
394                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
395                 break;
396         /* Display: Off; HSync: Off, VSync: On */
397         case FB_BLANK_HSYNC_SUSPEND:
398                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
399                 break;
400         /* Display: Off; HSync: On, VSync: Off */
401         case FB_BLANK_VSYNC_SUSPEND:
402                 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
403                 break;
404         /* Display: Off; HSync: Off, VSync: Off */
405         case FB_BLANK_POWERDOWN:
406                 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
407                 break;
408         }
409         return 0;
410 }
411 EXPORT_SYMBOL(drm_fb_helper_blank);
412
413 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
414 {
415         int i;
416
417         for (i = 0; i < helper->connector_count; i++)
418                 kfree(helper->connector_info[i]);
419         kfree(helper->connector_info);
420         for (i = 0; i < helper->crtc_count; i++)
421                 kfree(helper->crtc_info[i].mode_set.connectors);
422         kfree(helper->crtc_info);
423 }
424
425 int drm_fb_helper_init(struct drm_device *dev,
426                        struct drm_fb_helper *fb_helper,
427                        int crtc_count, int max_conn_count,
428                        bool polled)
429 {
430         struct drm_crtc *crtc;
431         int ret = 0;
432         int i;
433
434         fb_helper->dev = dev;
435         fb_helper->poll_enabled = polled;
436
437         slow_work_register_user(THIS_MODULE);
438         delayed_slow_work_init(&fb_helper->output_status_change_slow_work,
439                                &output_status_change_ops);
440
441         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
442
443         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
444         if (!fb_helper->crtc_info)
445                 return -ENOMEM;
446
447         fb_helper->crtc_count = crtc_count;
448         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
449         if (!fb_helper->connector_info) {
450                 kfree(fb_helper->crtc_info);
451                 return -ENOMEM;
452         }
453         fb_helper->connector_count = 0;
454
455         for (i = 0; i < crtc_count; i++) {
456                 fb_helper->crtc_info[i].mode_set.connectors =
457                         kcalloc(max_conn_count,
458                                 sizeof(struct drm_connector *),
459                                 GFP_KERNEL);
460
461                 if (!fb_helper->crtc_info[i].mode_set.connectors) {
462                         ret = -ENOMEM;
463                         goto out_free;
464                 }
465                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
466         }
467
468         i = 0;
469         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
470                 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
471                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
472                 i++;
473         }
474         fb_helper->conn_limit = max_conn_count;
475         return 0;
476 out_free:
477         drm_fb_helper_crtc_free(fb_helper);
478         return -ENOMEM;
479 }
480 EXPORT_SYMBOL(drm_fb_helper_init);
481
482 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
483 {
484         if (!list_empty(&fb_helper->kernel_fb_list)) {
485                 list_del(&fb_helper->kernel_fb_list);
486                 if (list_empty(&kernel_fb_helper_list)) {
487                         printk(KERN_INFO "unregistered panic notifier\n");
488                         atomic_notifier_chain_unregister(&panic_notifier_list,
489                                                          &paniced);
490                         unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
491                 }
492         }
493
494         drm_fb_helper_crtc_free(fb_helper);
495
496         delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
497         slow_work_unregister_user(THIS_MODULE);
498 }
499 EXPORT_SYMBOL(drm_fb_helper_fini);
500
501 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
502                      u16 blue, u16 regno, struct fb_info *info)
503 {
504         struct drm_fb_helper *fb_helper = info->par;
505         struct drm_framebuffer *fb = fb_helper->fb;
506         int pindex;
507
508         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
509                 u32 *palette;
510                 u32 value;
511                 /* place color in psuedopalette */
512                 if (regno > 16)
513                         return -EINVAL;
514                 palette = (u32 *)info->pseudo_palette;
515                 red >>= (16 - info->var.red.length);
516                 green >>= (16 - info->var.green.length);
517                 blue >>= (16 - info->var.blue.length);
518                 value = (red << info->var.red.offset) |
519                         (green << info->var.green.offset) |
520                         (blue << info->var.blue.offset);
521                 palette[regno] = value;
522                 return 0;
523         }
524
525         pindex = regno;
526
527         if (fb->bits_per_pixel == 16) {
528                 pindex = regno << 3;
529
530                 if (fb->depth == 16 && regno > 63)
531                         return -EINVAL;
532                 if (fb->depth == 15 && regno > 31)
533                         return -EINVAL;
534
535                 if (fb->depth == 16) {
536                         u16 r, g, b;
537                         int i;
538                         if (regno < 32) {
539                                 for (i = 0; i < 8; i++)
540                                         fb_helper->funcs->gamma_set(crtc, red,
541                                                 green, blue, pindex + i);
542                         }
543
544                         fb_helper->funcs->gamma_get(crtc, &r,
545                                                     &g, &b,
546                                                     pindex >> 1);
547
548                         for (i = 0; i < 4; i++)
549                                 fb_helper->funcs->gamma_set(crtc, r,
550                                                             green, b,
551                                                             (pindex >> 1) + i);
552                 }
553         }
554
555         if (fb->depth != 16)
556                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
557         return 0;
558 }
559
560 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
561 {
562         struct drm_fb_helper *fb_helper = info->par;
563         struct drm_crtc_helper_funcs *crtc_funcs;
564         u16 *red, *green, *blue, *transp;
565         struct drm_crtc *crtc;
566         int i, rc = 0;
567         int start;
568
569         for (i = 0; i < fb_helper->crtc_count; i++) {
570                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
571                 crtc_funcs = crtc->helper_private;
572
573                 red = cmap->red;
574                 green = cmap->green;
575                 blue = cmap->blue;
576                 transp = cmap->transp;
577                 start = cmap->start;
578
579                 for (i = 0; i < cmap->len; i++) {
580                         u16 hred, hgreen, hblue, htransp = 0xffff;
581
582                         hred = *red++;
583                         hgreen = *green++;
584                         hblue = *blue++;
585
586                         if (transp)
587                                 htransp = *transp++;
588
589                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
590                         if (rc)
591                                 return rc;
592                 }
593                 crtc_funcs->load_lut(crtc);
594         }
595         return rc;
596 }
597 EXPORT_SYMBOL(drm_fb_helper_setcmap);
598
599 int drm_fb_helper_setcolreg(unsigned regno,
600                             unsigned red,
601                             unsigned green,
602                             unsigned blue,
603                             unsigned transp,
604                             struct fb_info *info)
605 {
606         struct drm_fb_helper *fb_helper = info->par;
607         struct drm_crtc *crtc;
608         struct drm_crtc_helper_funcs *crtc_funcs;
609         int i;
610         int ret;
611
612         if (regno > 255)
613                 return 1;
614
615         for (i = 0; i < fb_helper->crtc_count; i++) {
616                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
617                 crtc_funcs = crtc->helper_private;
618
619                 ret = setcolreg(crtc, red, green, blue, regno, info);
620                 if (ret)
621                         return ret;
622
623                 crtc_funcs->load_lut(crtc);
624         }
625         return 0;
626 }
627 EXPORT_SYMBOL(drm_fb_helper_setcolreg);
628
629 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
630                             struct fb_info *info)
631 {
632         struct drm_fb_helper *fb_helper = info->par;
633         struct drm_framebuffer *fb = fb_helper->fb;
634         int depth;
635
636         if (var->pixclock != 0)
637                 return -EINVAL;
638
639         /* Need to resize the fb object !!! */
640         if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
641                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
642                           "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
643                           fb->width, fb->height, fb->bits_per_pixel);
644                 return -EINVAL;
645         }
646
647         switch (var->bits_per_pixel) {
648         case 16:
649                 depth = (var->green.length == 6) ? 16 : 15;
650                 break;
651         case 32:
652                 depth = (var->transp.length > 0) ? 32 : 24;
653                 break;
654         default:
655                 depth = var->bits_per_pixel;
656                 break;
657         }
658
659         switch (depth) {
660         case 8:
661                 var->red.offset = 0;
662                 var->green.offset = 0;
663                 var->blue.offset = 0;
664                 var->red.length = 8;
665                 var->green.length = 8;
666                 var->blue.length = 8;
667                 var->transp.length = 0;
668                 var->transp.offset = 0;
669                 break;
670         case 15:
671                 var->red.offset = 10;
672                 var->green.offset = 5;
673                 var->blue.offset = 0;
674                 var->red.length = 5;
675                 var->green.length = 5;
676                 var->blue.length = 5;
677                 var->transp.length = 1;
678                 var->transp.offset = 15;
679                 break;
680         case 16:
681                 var->red.offset = 11;
682                 var->green.offset = 5;
683                 var->blue.offset = 0;
684                 var->red.length = 5;
685                 var->green.length = 6;
686                 var->blue.length = 5;
687                 var->transp.length = 0;
688                 var->transp.offset = 0;
689                 break;
690         case 24:
691                 var->red.offset = 16;
692                 var->green.offset = 8;
693                 var->blue.offset = 0;
694                 var->red.length = 8;
695                 var->green.length = 8;
696                 var->blue.length = 8;
697                 var->transp.length = 0;
698                 var->transp.offset = 0;
699                 break;
700         case 32:
701                 var->red.offset = 16;
702                 var->green.offset = 8;
703                 var->blue.offset = 0;
704                 var->red.length = 8;
705                 var->green.length = 8;
706                 var->blue.length = 8;
707                 var->transp.length = 8;
708                 var->transp.offset = 24;
709                 break;
710         default:
711                 return -EINVAL;
712         }
713         return 0;
714 }
715 EXPORT_SYMBOL(drm_fb_helper_check_var);
716
717 /* this will let fbcon do the mode init */
718 int drm_fb_helper_set_par(struct fb_info *info)
719 {
720         struct drm_fb_helper *fb_helper = info->par;
721         struct drm_device *dev = fb_helper->dev;
722         struct fb_var_screeninfo *var = &info->var;
723         struct drm_crtc *crtc;
724         int ret;
725         int i;
726
727         if (var->pixclock != 0) {
728                 DRM_ERROR("PIXEL CLOCK SET\n");
729                 return -EINVAL;
730         }
731
732         mutex_lock(&dev->mode_config.mutex);
733         for (i = 0; i < fb_helper->crtc_count; i++) {
734                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
735                 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
736                 if (ret) {
737                         mutex_unlock(&dev->mode_config.mutex);
738                         return ret;
739                 }
740         }
741         mutex_unlock(&dev->mode_config.mutex);
742
743         if (fb_helper->delayed_hotplug) {
744                 fb_helper->delayed_hotplug = false;
745                 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
746         }
747         return 0;
748 }
749 EXPORT_SYMBOL(drm_fb_helper_set_par);
750
751 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
752                               struct fb_info *info)
753 {
754         struct drm_fb_helper *fb_helper = info->par;
755         struct drm_device *dev = fb_helper->dev;
756         struct drm_mode_set *modeset;
757         struct drm_crtc *crtc;
758         int ret = 0;
759         int i;
760
761         mutex_lock(&dev->mode_config.mutex);
762         for (i = 0; i < fb_helper->crtc_count; i++) {
763                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
764
765                 modeset = &fb_helper->crtc_info[i].mode_set;
766
767                 modeset->x = var->xoffset;
768                 modeset->y = var->yoffset;
769
770                 if (modeset->num_connectors) {
771                         ret = crtc->funcs->set_config(modeset);
772                         if (!ret) {
773                                 info->var.xoffset = var->xoffset;
774                                 info->var.yoffset = var->yoffset;
775                         }
776                 }
777         }
778         mutex_unlock(&dev->mode_config.mutex);
779         return ret;
780 }
781 EXPORT_SYMBOL(drm_fb_helper_pan_display);
782
783 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
784                                   int preferred_bpp)
785 {
786         int new_fb = 0;
787         int crtc_count = 0;
788         int i;
789         struct fb_info *info;
790         struct drm_fb_helper_surface_size sizes;
791         int gamma_size = 0;
792
793         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
794         sizes.surface_depth = 24;
795         sizes.surface_bpp = 32;
796         sizes.fb_width = (unsigned)-1;
797         sizes.fb_height = (unsigned)-1;
798
799         /* if driver picks 8 or 16 by default use that
800            for both depth/bpp */
801         if (preferred_bpp != sizes.surface_bpp) {
802                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
803         }
804         /* first up get a count of crtcs now in use and new min/maxes width/heights */
805         for (i = 0; i < fb_helper->connector_count; i++) {
806                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
807                 struct drm_fb_helper_cmdline_mode *cmdline_mode;
808
809                 cmdline_mode = &fb_helper_conn->cmdline_mode;
810
811                 if (cmdline_mode->bpp_specified) {
812                         switch (cmdline_mode->bpp) {
813                         case 8:
814                                 sizes.surface_depth = sizes.surface_bpp = 8;
815                                 break;
816                         case 15:
817                                 sizes.surface_depth = 15;
818                                 sizes.surface_bpp = 16;
819                                 break;
820                         case 16:
821                                 sizes.surface_depth = sizes.surface_bpp = 16;
822                                 break;
823                         case 24:
824                                 sizes.surface_depth = sizes.surface_bpp = 24;
825                                 break;
826                         case 32:
827                                 sizes.surface_depth = 24;
828                                 sizes.surface_bpp = 32;
829                                 break;
830                         }
831                         break;
832                 }
833         }
834
835         crtc_count = 0;
836         for (i = 0; i < fb_helper->crtc_count; i++) {
837                 struct drm_display_mode *desired_mode;
838                 desired_mode = fb_helper->crtc_info[i].desired_mode;
839
840                 if (desired_mode) {
841                         if (gamma_size == 0)
842                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
843                         if (desired_mode->hdisplay < sizes.fb_width)
844                                 sizes.fb_width = desired_mode->hdisplay;
845                         if (desired_mode->vdisplay < sizes.fb_height)
846                                 sizes.fb_height = desired_mode->vdisplay;
847                         if (desired_mode->hdisplay > sizes.surface_width)
848                                 sizes.surface_width = desired_mode->hdisplay;
849                         if (desired_mode->vdisplay > sizes.surface_height)
850                                 sizes.surface_height = desired_mode->vdisplay;
851                         crtc_count++;
852                 }
853         }
854
855         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
856                 /* hmm everyone went away - assume VGA cable just fell out
857                    and will come back later. */
858                 DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n");
859                 sizes.fb_width = sizes.surface_width = 1024;
860                 sizes.fb_height = sizes.surface_height = 768;
861         }
862
863         /* push down into drivers */
864         new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
865         if (new_fb < 0)
866                 return new_fb;
867
868         info = fb_helper->fbdev;
869
870         /* set the fb pointer */
871         for (i = 0; i < fb_helper->crtc_count; i++) {
872                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
873         }
874
875         if (new_fb) {
876                 info->var.pixclock = 0;
877                 if (register_framebuffer(info) < 0) {
878                         return -EINVAL;
879                 }
880
881                 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
882                        info->fix.id);
883
884         } else {
885                 drm_fb_helper_set_par(info);
886         }
887
888         /* Switch back to kernel console on panic */
889         /* multi card linked list maybe */
890         if (list_empty(&kernel_fb_helper_list)) {
891                 printk(KERN_INFO "registered panic notifier\n");
892                 atomic_notifier_chain_register(&panic_notifier_list,
893                                                &paniced);
894                 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
895         }
896         if (new_fb)
897                 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
898
899         return 0;
900 }
901 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
902
903 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
904                             uint32_t depth)
905 {
906         info->fix.type = FB_TYPE_PACKED_PIXELS;
907         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
908                 FB_VISUAL_TRUECOLOR;
909         info->fix.type_aux = 0;
910         info->fix.xpanstep = 1; /* doing it in hw */
911         info->fix.ypanstep = 1; /* doing it in hw */
912         info->fix.ywrapstep = 0;
913         info->fix.accel = FB_ACCEL_NONE;
914         info->fix.type_aux = 0;
915
916         info->fix.line_length = pitch;
917         return;
918 }
919 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
920
921 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
922                             uint32_t fb_width, uint32_t fb_height)
923 {
924         struct drm_framebuffer *fb = fb_helper->fb;
925         info->pseudo_palette = fb_helper->pseudo_palette;
926         info->var.xres_virtual = fb->width;
927         info->var.yres_virtual = fb->height;
928         info->var.bits_per_pixel = fb->bits_per_pixel;
929         info->var.xoffset = 0;
930         info->var.yoffset = 0;
931         info->var.activate = FB_ACTIVATE_NOW;
932         info->var.height = -1;
933         info->var.width = -1;
934
935         switch (fb->depth) {
936         case 8:
937                 info->var.red.offset = 0;
938                 info->var.green.offset = 0;
939                 info->var.blue.offset = 0;
940                 info->var.red.length = 8; /* 8bit DAC */
941                 info->var.green.length = 8;
942                 info->var.blue.length = 8;
943                 info->var.transp.offset = 0;
944                 info->var.transp.length = 0;
945                 break;
946         case 15:
947                 info->var.red.offset = 10;
948                 info->var.green.offset = 5;
949                 info->var.blue.offset = 0;
950                 info->var.red.length = 5;
951                 info->var.green.length = 5;
952                 info->var.blue.length = 5;
953                 info->var.transp.offset = 15;
954                 info->var.transp.length = 1;
955                 break;
956         case 16:
957                 info->var.red.offset = 11;
958                 info->var.green.offset = 5;
959                 info->var.blue.offset = 0;
960                 info->var.red.length = 5;
961                 info->var.green.length = 6;
962                 info->var.blue.length = 5;
963                 info->var.transp.offset = 0;
964                 break;
965         case 24:
966                 info->var.red.offset = 16;
967                 info->var.green.offset = 8;
968                 info->var.blue.offset = 0;
969                 info->var.red.length = 8;
970                 info->var.green.length = 8;
971                 info->var.blue.length = 8;
972                 info->var.transp.offset = 0;
973                 info->var.transp.length = 0;
974                 break;
975         case 32:
976                 info->var.red.offset = 16;
977                 info->var.green.offset = 8;
978                 info->var.blue.offset = 0;
979                 info->var.red.length = 8;
980                 info->var.green.length = 8;
981                 info->var.blue.length = 8;
982                 info->var.transp.offset = 24;
983                 info->var.transp.length = 8;
984                 break;
985         default:
986                 break;
987         }
988
989         info->var.xres = fb_width;
990         info->var.yres = fb_height;
991 }
992 EXPORT_SYMBOL(drm_fb_helper_fill_var);
993
994 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
995                                                uint32_t maxX,
996                                                uint32_t maxY)
997 {
998         struct drm_connector *connector;
999         int count = 0;
1000         int i;
1001
1002         for (i = 0; i < fb_helper->connector_count; i++) {
1003                 connector = fb_helper->connector_info[i]->connector;
1004                 count += connector->funcs->fill_modes(connector, maxX, maxY);
1005         }
1006
1007         return count;
1008 }
1009
1010 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1011 {
1012         struct drm_display_mode *mode;
1013
1014         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1015                 if (drm_mode_width(mode) > width ||
1016                     drm_mode_height(mode) > height)
1017                         continue;
1018                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1019                         return mode;
1020         }
1021         return NULL;
1022 }
1023
1024 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1025 {
1026         struct drm_fb_helper_cmdline_mode *cmdline_mode;
1027         cmdline_mode = &fb_connector->cmdline_mode;
1028         return cmdline_mode->specified;
1029 }
1030
1031 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1032                                                       int width, int height)
1033 {
1034         struct drm_fb_helper_cmdline_mode *cmdline_mode;
1035         struct drm_display_mode *mode = NULL;
1036
1037         cmdline_mode = &fb_helper_conn->cmdline_mode;
1038         if (cmdline_mode->specified == false)
1039                 return mode;
1040
1041         /* attempt to find a matching mode in the list of modes
1042          *  we have gotten so far, if not add a CVT mode that conforms
1043          */
1044         if (cmdline_mode->rb || cmdline_mode->margins)
1045                 goto create_mode;
1046
1047         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1048                 /* check width/height */
1049                 if (mode->hdisplay != cmdline_mode->xres ||
1050                     mode->vdisplay != cmdline_mode->yres)
1051                         continue;
1052
1053                 if (cmdline_mode->refresh_specified) {
1054                         if (mode->vrefresh != cmdline_mode->refresh)
1055                                 continue;
1056                 }
1057
1058                 if (cmdline_mode->interlace) {
1059                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1060                                 continue;
1061                 }
1062                 return mode;
1063         }
1064
1065 create_mode:
1066         mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
1067                             cmdline_mode->yres,
1068                             cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1069                             cmdline_mode->rb, cmdline_mode->interlace,
1070                             cmdline_mode->margins);
1071         drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1072         list_add(&mode->head, &fb_helper_conn->connector->modes);
1073         return mode;
1074 }
1075
1076 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1077 {
1078         bool enable;
1079
1080         if (strict) {
1081                 enable = connector->status == connector_status_connected;
1082         } else {
1083                 enable = connector->status != connector_status_disconnected;
1084         }
1085         return enable;
1086 }
1087
1088 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1089                                   bool *enabled)
1090 {
1091         bool any_enabled = false;
1092         struct drm_connector *connector;
1093         int i = 0;
1094
1095         for (i = 0; i < fb_helper->connector_count; i++) {
1096                 connector = fb_helper->connector_info[i]->connector;
1097                 enabled[i] = drm_connector_enabled(connector, true);
1098                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1099                           enabled[i] ? "yes" : "no");
1100                 any_enabled |= enabled[i];
1101         }
1102
1103         if (any_enabled)
1104                 return;
1105
1106         for (i = 0; i < fb_helper->connector_count; i++) {
1107                 connector = fb_helper->connector_info[i]->connector;
1108                 enabled[i] = drm_connector_enabled(connector, false);
1109         }
1110 }
1111
1112 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1113                                  struct drm_display_mode **modes,
1114                                  bool *enabled, int width, int height)
1115 {
1116         struct drm_fb_helper_connector *fb_helper_conn;
1117         int i;
1118
1119         for (i = 0; i < fb_helper->connector_count; i++) {
1120                 fb_helper_conn = fb_helper->connector_info[i];
1121
1122                 if (enabled[i] == false)
1123                         continue;
1124
1125                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1126                               fb_helper_conn->connector->base.id);
1127
1128                 /* got for command line mode first */
1129                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1130                 if (!modes[i]) {
1131                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1132                                       fb_helper_conn->connector->base.id);
1133                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1134                 }
1135                 /* No preferred modes, pick one off the list */
1136                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1137                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1138                                 break;
1139                 }
1140                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1141                           "none");
1142         }
1143         return true;
1144 }
1145
1146 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1147                           struct drm_fb_helper_crtc **best_crtcs,
1148                           struct drm_display_mode **modes,
1149                           int n, int width, int height)
1150 {
1151         int c, o;
1152         struct drm_device *dev = fb_helper->dev;
1153         struct drm_connector *connector;
1154         struct drm_connector_helper_funcs *connector_funcs;
1155         struct drm_encoder *encoder;
1156         struct drm_fb_helper_crtc *best_crtc;
1157         int my_score, best_score, score;
1158         struct drm_fb_helper_crtc **crtcs, *crtc;
1159         struct drm_fb_helper_connector *fb_helper_conn;
1160
1161         if (n == fb_helper->connector_count)
1162                 return 0;
1163
1164         fb_helper_conn = fb_helper->connector_info[n];
1165         connector = fb_helper_conn->connector;
1166
1167         best_crtcs[n] = NULL;
1168         best_crtc = NULL;
1169         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1170         if (modes[n] == NULL)
1171                 return best_score;
1172
1173         crtcs = kzalloc(dev->mode_config.num_connector *
1174                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1175         if (!crtcs)
1176                 return best_score;
1177
1178         my_score = 1;
1179         if (connector->status == connector_status_connected)
1180                 my_score++;
1181         if (drm_has_cmdline_mode(fb_helper_conn))
1182                 my_score++;
1183         if (drm_has_preferred_mode(fb_helper_conn, width, height))
1184                 my_score++;
1185
1186         connector_funcs = connector->helper_private;
1187         encoder = connector_funcs->best_encoder(connector);
1188         if (!encoder)
1189                 goto out;
1190
1191         /* select a crtc for this connector and then attempt to configure
1192            remaining connectors */
1193         for (c = 0; c < fb_helper->crtc_count; c++) {
1194                 crtc = &fb_helper->crtc_info[c];
1195
1196                 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1197                         continue;
1198                 }
1199
1200                 for (o = 0; o < n; o++)
1201                         if (best_crtcs[o] == crtc)
1202                                 break;
1203
1204                 if (o < n) {
1205                         /* ignore cloning for now */
1206                         continue;
1207                 }
1208
1209                 crtcs[n] = crtc;
1210                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1211                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1212                                                   width, height);
1213                 if (score > best_score) {
1214                         best_crtc = crtc;
1215                         best_score = score;
1216                         memcpy(best_crtcs, crtcs,
1217                                dev->mode_config.num_connector *
1218                                sizeof(struct drm_fb_helper_crtc *));
1219                 }
1220         }
1221 out:
1222         kfree(crtcs);
1223         return best_score;
1224 }
1225
1226 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1227 {
1228         struct drm_device *dev = fb_helper->dev;
1229         struct drm_fb_helper_crtc **crtcs;
1230         struct drm_display_mode **modes;
1231         struct drm_encoder *encoder;
1232         struct drm_mode_set *modeset;
1233         bool *enabled;
1234         int width, height;
1235         int i, ret;
1236
1237         DRM_DEBUG_KMS("\n");
1238
1239         width = dev->mode_config.max_width;
1240         height = dev->mode_config.max_height;
1241
1242         /* clean out all the encoder/crtc combos */
1243         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1244                 encoder->crtc = NULL;
1245         }
1246
1247         crtcs = kcalloc(dev->mode_config.num_connector,
1248                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1249         modes = kcalloc(dev->mode_config.num_connector,
1250                         sizeof(struct drm_display_mode *), GFP_KERNEL);
1251         enabled = kcalloc(dev->mode_config.num_connector,
1252                           sizeof(bool), GFP_KERNEL);
1253
1254         drm_enable_connectors(fb_helper, enabled);
1255
1256         ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1257         if (!ret)
1258                 DRM_ERROR("Unable to find initial modes\n");
1259
1260         DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1261
1262         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1263
1264         /* need to set the modesets up here for use later */
1265         /* fill out the connector<->crtc mappings into the modesets */
1266         for (i = 0; i < fb_helper->crtc_count; i++) {
1267                 modeset = &fb_helper->crtc_info[i].mode_set;
1268                 modeset->num_connectors = 0;
1269         }
1270
1271         for (i = 0; i < fb_helper->connector_count; i++) {
1272                 struct drm_display_mode *mode = modes[i];
1273                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1274                 modeset = &fb_crtc->mode_set;
1275
1276                 if (mode && fb_crtc) {
1277                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1278                                       mode->name, fb_crtc->mode_set.crtc->base.id);
1279                         fb_crtc->desired_mode = mode;
1280                         if (modeset->mode)
1281                                 drm_mode_destroy(dev, modeset->mode);
1282                         modeset->mode = drm_mode_duplicate(dev,
1283                                                            fb_crtc->desired_mode);
1284                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1285                 }
1286         }
1287
1288         kfree(crtcs);
1289         kfree(modes);
1290         kfree(enabled);
1291 }
1292
1293 /**
1294  * drm_helper_initial_config - setup a sane initial connector configuration
1295  * @dev: DRM device
1296  *
1297  * LOCKING:
1298  * Called at init time, must take mode config lock.
1299  *
1300  * Scan the CRTCs and connectors and try to put together an initial setup.
1301  * At the moment, this is a cloned configuration across all heads with
1302  * a new framebuffer object as the backing store.
1303  *
1304  * RETURNS:
1305  * Zero if everything went ok, nonzero otherwise.
1306  */
1307 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1308 {
1309         struct drm_device *dev = fb_helper->dev;
1310         int count = 0;
1311
1312         /* disable all the possible outputs/crtcs before entering KMS mode */
1313         drm_helper_disable_unused_functions(fb_helper->dev);
1314
1315         drm_fb_helper_parse_command_line(fb_helper);
1316
1317         count = drm_fb_helper_probe_connector_modes(fb_helper,
1318                                                     dev->mode_config.max_width,
1319                                                     dev->mode_config.max_height);
1320         /*
1321          * we shouldn't end up with no modes here.
1322          */
1323         if (count == 0) {
1324                 if (fb_helper->poll_enabled) {
1325                         delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work,
1326                                                   5*HZ);
1327                         printk(KERN_INFO "No connectors reported connected with modes - started polling\n");
1328                 } else
1329                         printk(KERN_INFO "No connectors reported connected with modes\n");
1330         }
1331         drm_setup_crtcs(fb_helper);
1332
1333         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1334 }
1335 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1336
1337 /* we got a hotplug irq - need to update fbcon */
1338 void drm_helper_fb_hpd_irq_event(struct drm_fb_helper *fb_helper)
1339 {
1340         /* if we don't have the fbdev registered yet do nothing */
1341         if (!fb_helper->fbdev)
1342                 return;
1343
1344         /* schedule a slow work asap */
1345         delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
1346 }
1347 EXPORT_SYMBOL(drm_helper_fb_hpd_irq_event);
1348
1349 bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper, bool polled)
1350 {
1351         int count = 0;
1352         int ret;
1353         u32 max_width, max_height, bpp_sel;
1354
1355         if (!fb_helper->fb)
1356                 return false;
1357         DRM_DEBUG_KMS("\n");
1358
1359         max_width = fb_helper->fb->width;
1360         max_height = fb_helper->fb->height;
1361         bpp_sel = fb_helper->fb->bits_per_pixel;
1362
1363         count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1364                                                     max_height);
1365         if (fb_helper->poll_enabled && !polled) {
1366                 if (count) {
1367                         delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
1368                 } else {
1369                         ret = delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 5*HZ);
1370                 }
1371         }
1372         drm_setup_crtcs(fb_helper);
1373
1374         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1375 }
1376 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);
1377
1378 /*
1379  * delayed work queue execution function
1380  * - check if fbdev is actually in use on the gpu
1381  *   - if not set delayed flag and repoll if necessary
1382  * - check for connector status change
1383  * - repoll if 0 modes found
1384  *- call driver output status changed notifier
1385  */
1386 static void output_status_change_execute(struct slow_work *work)
1387 {
1388         struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
1389         struct drm_fb_helper *fb_helper = container_of(delayed_work, struct drm_fb_helper, output_status_change_slow_work);
1390         struct drm_connector *connector;
1391         enum drm_connector_status old_status, status;
1392         bool repoll, changed = false;
1393         int ret;
1394         int i;
1395         bool bound = false, crtcs_bound = false;
1396         struct drm_crtc *crtc;
1397
1398         repoll = fb_helper->poll_enabled;
1399
1400         /* first of all check the fbcon framebuffer is actually bound to any crtc */
1401         /* take into account that no crtc at all maybe bound */
1402         list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1403                 if (crtc->fb)
1404                         crtcs_bound = true;
1405                 if (crtc->fb == fb_helper->fb)
1406                         bound = true;
1407         }
1408
1409         if (bound == false && crtcs_bound) {
1410                 fb_helper->delayed_hotplug = true;
1411                 goto requeue;
1412         }
1413
1414         for (i = 0; i < fb_helper->connector_count; i++) {
1415                 connector = fb_helper->connector_info[i]->connector;
1416                 old_status = connector->status;
1417                 status = connector->funcs->detect(connector);
1418                 if (old_status != status) {
1419                         changed = true;
1420                 }
1421                 if (status == connector_status_connected && repoll) {
1422                         DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector));
1423                         repoll = false;
1424                 }
1425         }
1426
1427         if (changed) {
1428                 if (fb_helper->funcs->fb_output_status_changed)
1429                         fb_helper->funcs->fb_output_status_changed(fb_helper);
1430         }
1431
1432 requeue:
1433         if (repoll) {
1434                 ret = delayed_slow_work_enqueue(delayed_work, 5*HZ);
1435                 if (ret)
1436                         DRM_ERROR("delayed enqueue failed %d\n", ret);
1437         }
1438 }
1439
1440 static struct slow_work_ops output_status_change_ops = {
1441         .execute = output_status_change_execute,
1442 };
1443