Subversion Repositories Kolibri OS

Rev

Rev 4539 | Rev 5271 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31.  
  32. #include <linux/kernel.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/slab.h>
  35. #include <linux/fb.h>
  36. #include <linux/module.h>
  37. #include <drm/drmP.h>
  38. #include <drm/drm_crtc.h>
  39. #include <drm/drm_fb_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41.  
  42. static LIST_HEAD(kernel_fb_helper_list);
  43.  
  44. /**
  45.  * DOC: fbdev helpers
  46.  *
  47.  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  48.  * mode setting driver. They can be used mostly independantely from the crtc
  49.  * helper functions used by many drivers to implement the kernel mode setting
  50.  * interfaces.
  51.  *
  52.  * Initialization is done as a three-step process with drm_fb_helper_init(),
  53.  * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config().
  54.  * Drivers with fancier requirements than the default beheviour can override the
  55.  * second step with their own code.  Teardown is done with drm_fb_helper_fini().
  56.  *
  57.  * At runtime drivers should restore the fbdev console by calling
  58.  * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
  59.  * should also notify the fb helper code from updates to the output
  60.  * configuration by calling drm_fb_helper_hotplug_event(). For easier
  61.  * integration with the output polling code in drm_crtc_helper.c the modeset
  62.  * code proves a ->output_poll_changed callback.
  63.  *
  64.  * All other functions exported by the fb helper library can be used to
  65.  * implement the fbdev driver interface by the driver.
  66.  */
  67.  
  68. /**
  69.  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
  70.  *                                             emulation helper
  71.  * @fb_helper: fbdev initialized with drm_fb_helper_init
  72.  *
  73.  * This functions adds all the available connectors for use with the given
  74.  * fb_helper. This is a separate step to allow drivers to freely assign
  75.  * connectors to the fbdev, e.g. if some are reserved for special purposes or
  76.  * not adequate to be used for the fbcon.
  77.  *
  78.  * Since this is part of the initial setup before the fbdev is published, no
  79.  * locking is required.
  80.  */
  81. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  82. {
  83.         struct drm_device *dev = fb_helper->dev;
  84.         struct drm_connector *connector;
  85.         int i;
  86.  
  87.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  88.                 struct drm_fb_helper_connector *fb_helper_connector;
  89.  
  90.                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  91.                 if (!fb_helper_connector)
  92.                         goto fail;
  93.  
  94.                 fb_helper_connector->connector = connector;
  95.                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  96.         }
  97.         return 0;
  98. fail:
  99.         for (i = 0; i < fb_helper->connector_count; i++) {
  100.                 kfree(fb_helper->connector_info[i]);
  101.                 fb_helper->connector_info[i] = NULL;
  102.         }
  103.         fb_helper->connector_count = 0;
  104.     return -ENOMEM;
  105. }
  106. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  107.  
  108. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  109. {
  110.         uint16_t *r_base, *g_base, *b_base;
  111.         int i;
  112.  
  113.         if (helper->funcs->gamma_get == NULL)
  114.                 return;
  115.  
  116.         r_base = crtc->gamma_store;
  117.         g_base = r_base + crtc->gamma_size;
  118.         b_base = g_base + crtc->gamma_size;
  119.  
  120.         for (i = 0; i < crtc->gamma_size; i++)
  121.                 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  122. }
  123.  
  124. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  125. {
  126.         uint16_t *r_base, *g_base, *b_base;
  127.  
  128.         if (crtc->funcs->gamma_set == NULL)
  129.                 return;
  130.  
  131.         r_base = crtc->gamma_store;
  132.         g_base = r_base + crtc->gamma_size;
  133.         b_base = g_base + crtc->gamma_size;
  134.  
  135.         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  136. }
  137.  
  138.  
  139. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  140. {
  141.         struct drm_device *dev = fb_helper->dev;
  142.         struct drm_crtc *crtc;
  143.         int bound = 0, crtcs_bound = 0;
  144.  
  145.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  146.                 if (crtc->fb)
  147.                         crtcs_bound++;
  148.                 if (crtc->fb == fb_helper->fb)
  149.                         bound++;
  150.         }
  151.  
  152.         if (bound < crtcs_bound)
  153.                 return false;
  154.  
  155.         return true;
  156. }
  157.  
  158. #ifdef CONFIG_MAGIC_SYSRQ
  159. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  160. {
  161.         bool ret;
  162.         ret = drm_fb_helper_force_kernel_mode();
  163.         if (ret == true)
  164.                 DRM_ERROR("Failed to restore crtc configuration\n");
  165. }
  166. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  167.  
  168. static void drm_fb_helper_sysrq(int dummy1)
  169. {
  170.         schedule_work(&drm_fb_helper_restore_work);
  171. }
  172.  
  173. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  174.         .handler = drm_fb_helper_sysrq,
  175.         .help_msg = "force-fb(V)",
  176.         .action_msg = "Restore framebuffer console",
  177. };
  178. #else
  179.  
  180. #endif
  181.  
  182. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  183. {
  184.         struct drm_fb_helper *fb_helper = info->par;
  185.         struct drm_device *dev = fb_helper->dev;
  186.         struct drm_crtc *crtc;
  187.         struct drm_connector *connector;
  188.         int i, j;
  189.  
  190.         /*
  191.          * fbdev->blank can be called from irq context in case of a panic.
  192.          * Since we already have our own special panic handler which will
  193.          * restore the fbdev console mode completely, just bail out early.
  194.          */
  195.  
  196.         /*
  197.          * For each CRTC in this fb, turn the connectors on/off.
  198.          */
  199.         drm_modeset_lock_all(dev);
  200.         if (!drm_fb_helper_is_bound(fb_helper)) {
  201.                 drm_modeset_unlock_all(dev);
  202.                 return;
  203.         }
  204.  
  205.         for (i = 0; i < fb_helper->crtc_count; i++) {
  206.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  207.  
  208.                 if (!crtc->enabled)
  209.                                 continue;
  210.  
  211.                 /* Walk the connectors & encoders on this fb turning them on/off */
  212.                 for (j = 0; j < fb_helper->connector_count; j++) {
  213.                         connector = fb_helper->connector_info[j]->connector;
  214.                         connector->funcs->dpms(connector, dpms_mode);
  215.                         drm_object_property_set_value(&connector->base,
  216.                                 dev->mode_config.dpms_property, dpms_mode);
  217.                 }
  218.         }
  219.         drm_modeset_unlock_all(dev);
  220. }
  221.  
  222. /**
  223.  * drm_fb_helper_blank - implementation for ->fb_blank
  224.  * @blank: desired blanking state
  225.  * @info: fbdev registered by the helper
  226.  */
  227. int drm_fb_helper_blank(int blank, struct fb_info *info)
  228. {
  229.         switch (blank) {
  230.         /* Display: On; HSync: On, VSync: On */
  231.         case FB_BLANK_UNBLANK:
  232.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  233.                 break;
  234.         /* Display: Off; HSync: On, VSync: On */
  235.         case FB_BLANK_NORMAL:
  236.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  237.                 break;
  238.         /* Display: Off; HSync: Off, VSync: On */
  239.         case FB_BLANK_HSYNC_SUSPEND:
  240.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  241.                 break;
  242.         /* Display: Off; HSync: On, VSync: Off */
  243.         case FB_BLANK_VSYNC_SUSPEND:
  244.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  245.                 break;
  246.         /* Display: Off; HSync: Off, VSync: Off */
  247.         case FB_BLANK_POWERDOWN:
  248.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  249.                 break;
  250.         }
  251.         return 0;
  252. }
  253. EXPORT_SYMBOL(drm_fb_helper_blank);
  254.  
  255. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  256. {
  257.         int i;
  258.  
  259.         for (i = 0; i < helper->connector_count; i++)
  260.                 kfree(helper->connector_info[i]);
  261.         kfree(helper->connector_info);
  262.         for (i = 0; i < helper->crtc_count; i++) {
  263.                 kfree(helper->crtc_info[i].mode_set.connectors);
  264.                 if (helper->crtc_info[i].mode_set.mode)
  265.                         drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
  266.         }
  267.         kfree(helper->crtc_info);
  268. }
  269.  
  270. /**
  271.  * drm_fb_helper_init - initialize a drm_fb_helper structure
  272.  * @dev: drm device
  273.  * @fb_helper: driver-allocated fbdev helper structure to initialize
  274.  * @crtc_count: maximum number of crtcs to support in this fbdev emulation
  275.  * @max_conn_count: max connector count
  276.  *
  277.  * This allocates the structures for the fbdev helper with the given limits.
  278.  * Note that this won't yet touch the hardware (through the driver interfaces)
  279.  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  280.  * to allow driver writes more control over the exact init sequence.
  281.  *
  282.  * Drivers must set fb_helper->funcs before calling
  283.  * drm_fb_helper_initial_config().
  284.  *
  285.  * RETURNS:
  286.  * Zero if everything went ok, nonzero otherwise.
  287.  */
  288. int drm_fb_helper_init(struct drm_device *dev,
  289.                        struct drm_fb_helper *fb_helper,
  290.                        int crtc_count, int max_conn_count)
  291. {
  292.         struct drm_crtc *crtc;
  293.         int i;
  294.  
  295.         fb_helper->dev = dev;
  296.  
  297.         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
  298.  
  299.         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  300.         if (!fb_helper->crtc_info)
  301.                 return -ENOMEM;
  302.  
  303.         fb_helper->crtc_count = crtc_count;
  304.         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  305.         if (!fb_helper->connector_info) {
  306.                 kfree(fb_helper->crtc_info);
  307.                 return -ENOMEM;
  308.         }
  309.         fb_helper->connector_count = 0;
  310.  
  311.         for (i = 0; i < crtc_count; i++) {
  312.                 fb_helper->crtc_info[i].mode_set.connectors =
  313.                         kcalloc(max_conn_count,
  314.                                 sizeof(struct drm_connector *),
  315.                                 GFP_KERNEL);
  316.  
  317.                 if (!fb_helper->crtc_info[i].mode_set.connectors)
  318.                         goto out_free;
  319.                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  320.         }
  321.  
  322.         i = 0;
  323.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  324.                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
  325.                 i++;
  326.         }
  327.  
  328.         return 0;
  329. out_free:
  330.         drm_fb_helper_crtc_free(fb_helper);
  331.         return -ENOMEM;
  332. }
  333. EXPORT_SYMBOL(drm_fb_helper_init);
  334.  
  335. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  336.                      u16 blue, u16 regno, struct fb_info *info)
  337. {
  338.         struct drm_fb_helper *fb_helper = info->par;
  339.         struct drm_framebuffer *fb = fb_helper->fb;
  340.         int pindex;
  341.  
  342.         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  343.                 u32 *palette;
  344.                 u32 value;
  345.                 /* place color in psuedopalette */
  346.                 if (regno > 16)
  347.                         return -EINVAL;
  348.                 palette = (u32 *)info->pseudo_palette;
  349.                 red >>= (16 - info->var.red.length);
  350.                 green >>= (16 - info->var.green.length);
  351.                 blue >>= (16 - info->var.blue.length);
  352.                 value = (red << info->var.red.offset) |
  353.                         (green << info->var.green.offset) |
  354.                         (blue << info->var.blue.offset);
  355.                 if (info->var.transp.length > 0) {
  356.                         u32 mask = (1 << info->var.transp.length) - 1;
  357.                         mask <<= info->var.transp.offset;
  358.                         value |= mask;
  359.                 }
  360.                 palette[regno] = value;
  361.                 return 0;
  362.         }
  363.  
  364.         /*
  365.          * The driver really shouldn't advertise pseudo/directcolor
  366.          * visuals if it can't deal with the palette.
  367.          */
  368.         if (WARN_ON(!fb_helper->funcs->gamma_set ||
  369.                     !fb_helper->funcs->gamma_get))
  370.                 return -EINVAL;
  371.  
  372.         pindex = regno;
  373.  
  374.         if (fb->bits_per_pixel == 16) {
  375.                 pindex = regno << 3;
  376.  
  377.                 if (fb->depth == 16 && regno > 63)
  378.                         return -EINVAL;
  379.                 if (fb->depth == 15 && regno > 31)
  380.                         return -EINVAL;
  381.  
  382.                 if (fb->depth == 16) {
  383.                         u16 r, g, b;
  384.                         int i;
  385.                         if (regno < 32) {
  386.                                 for (i = 0; i < 8; i++)
  387.                                         fb_helper->funcs->gamma_set(crtc, red,
  388.                                                 green, blue, pindex + i);
  389.                         }
  390.  
  391.                         fb_helper->funcs->gamma_get(crtc, &r,
  392.                                                     &g, &b,
  393.                                                     pindex >> 1);
  394.  
  395.                         for (i = 0; i < 4; i++)
  396.                                 fb_helper->funcs->gamma_set(crtc, r,
  397.                                                             green, b,
  398.                                                             (pindex >> 1) + i);
  399.                 }
  400.         }
  401.  
  402.         if (fb->depth != 16)
  403.                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  404.         return 0;
  405. }
  406.  
  407. /**
  408.  * drm_fb_helper_setcmap - implementation for ->fb_setcmap
  409.  * @cmap: cmap to set
  410.  * @info: fbdev registered by the helper
  411.  */
  412. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  413. {
  414.         struct drm_fb_helper *fb_helper = info->par;
  415.         struct drm_device *dev = fb_helper->dev;
  416.         struct drm_crtc_helper_funcs *crtc_funcs;
  417.         u16 *red, *green, *blue, *transp;
  418.         struct drm_crtc *crtc;
  419.         int i, j, rc = 0;
  420.         int start;
  421.  
  422.         drm_modeset_lock_all(dev);
  423.         if (!drm_fb_helper_is_bound(fb_helper)) {
  424.                 drm_modeset_unlock_all(dev);
  425.                 return -EBUSY;
  426.         }
  427.  
  428.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  429.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  430.                 crtc_funcs = crtc->helper_private;
  431.  
  432.                 red = cmap->red;
  433.                 green = cmap->green;
  434.                 blue = cmap->blue;
  435.                 transp = cmap->transp;
  436.                 start = cmap->start;
  437.  
  438.                 for (j = 0; j < cmap->len; j++) {
  439.                         u16 hred, hgreen, hblue, htransp = 0xffff;
  440.  
  441.                         hred = *red++;
  442.                         hgreen = *green++;
  443.                         hblue = *blue++;
  444.  
  445.                         if (transp)
  446.                                 htransp = *transp++;
  447.  
  448.                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  449.                         if (rc)
  450.                                 goto out;
  451.                 }
  452.                 if (crtc_funcs->load_lut)
  453.                 crtc_funcs->load_lut(crtc);
  454.         }
  455.  out:
  456.         drm_modeset_unlock_all(dev);
  457.         return rc;
  458. }
  459. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  460.  
  461. /**
  462.  * drm_fb_helper_check_var - implementation for ->fb_check_var
  463.  * @var: screeninfo to check
  464.  * @info: fbdev registered by the helper
  465.  */
  466. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  467.                             struct fb_info *info)
  468. {
  469.         struct drm_fb_helper *fb_helper = info->par;
  470.         struct drm_framebuffer *fb = fb_helper->fb;
  471.         int depth;
  472.  
  473.         if (var->pixclock != 0 || in_dbg_master())
  474.                 return -EINVAL;
  475.  
  476.         /* Need to resize the fb object !!! */
  477.         if (var->bits_per_pixel > fb->bits_per_pixel ||
  478.             var->xres > fb->width || var->yres > fb->height ||
  479.             var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  480.                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  481.                           "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  482.                           var->xres, var->yres, var->bits_per_pixel,
  483.                           var->xres_virtual, var->yres_virtual,
  484.                           fb->width, fb->height, fb->bits_per_pixel);
  485.                 return -EINVAL;
  486.         }
  487.  
  488.         switch (var->bits_per_pixel) {
  489.         case 16:
  490.                 depth = (var->green.length == 6) ? 16 : 15;
  491.                 break;
  492.         case 32:
  493.                 depth = (var->transp.length > 0) ? 32 : 24;
  494.                 break;
  495.         default:
  496.                 depth = var->bits_per_pixel;
  497.                 break;
  498.         }
  499.  
  500.         switch (depth) {
  501.         case 8:
  502.                 var->red.offset = 0;
  503.                 var->green.offset = 0;
  504.                 var->blue.offset = 0;
  505.                 var->red.length = 8;
  506.                 var->green.length = 8;
  507.                 var->blue.length = 8;
  508.                 var->transp.length = 0;
  509.                 var->transp.offset = 0;
  510.                 break;
  511.         case 15:
  512.                 var->red.offset = 10;
  513.                 var->green.offset = 5;
  514.                 var->blue.offset = 0;
  515.                 var->red.length = 5;
  516.                 var->green.length = 5;
  517.                 var->blue.length = 5;
  518.                 var->transp.length = 1;
  519.                 var->transp.offset = 15;
  520.                 break;
  521.         case 16:
  522.                 var->red.offset = 11;
  523.                 var->green.offset = 5;
  524.                 var->blue.offset = 0;
  525.                 var->red.length = 5;
  526.                 var->green.length = 6;
  527.                 var->blue.length = 5;
  528.                 var->transp.length = 0;
  529.                 var->transp.offset = 0;
  530.                 break;
  531.         case 24:
  532.                 var->red.offset = 16;
  533.                 var->green.offset = 8;
  534.                 var->blue.offset = 0;
  535.                 var->red.length = 8;
  536.                 var->green.length = 8;
  537.                 var->blue.length = 8;
  538.                 var->transp.length = 0;
  539.                 var->transp.offset = 0;
  540.                 break;
  541.         case 32:
  542.                 var->red.offset = 16;
  543.                 var->green.offset = 8;
  544.                 var->blue.offset = 0;
  545.                 var->red.length = 8;
  546.                 var->green.length = 8;
  547.                 var->blue.length = 8;
  548.                 var->transp.length = 8;
  549.                 var->transp.offset = 24;
  550.                 break;
  551.         default:
  552.                 return -EINVAL;
  553.         }
  554.         return 0;
  555. }
  556. EXPORT_SYMBOL(drm_fb_helper_check_var);
  557.  
  558. /**
  559.  * drm_fb_helper_set_par - implementation for ->fb_set_par
  560.  * @info: fbdev registered by the helper
  561.  *
  562.  * This will let fbcon do the mode init and is called at initialization time by
  563.  * the fbdev core when registering the driver, and later on through the hotplug
  564.  * callback.
  565.  */
  566. int drm_fb_helper_set_par(struct fb_info *info)
  567. {
  568.         struct drm_fb_helper *fb_helper = info->par;
  569.         struct drm_device *dev = fb_helper->dev;
  570.         struct fb_var_screeninfo *var = &info->var;
  571.         int ret;
  572.         int i;
  573.  
  574.         if (var->pixclock != 0) {
  575.                 DRM_ERROR("PIXEL CLOCK SET\n");
  576.                 return -EINVAL;
  577.         }
  578.  
  579.         drm_modeset_lock_all(dev);
  580.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  581.                 ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
  582.                 if (ret) {
  583.                         drm_modeset_unlock_all(dev);
  584.                                 return ret;
  585.                 }
  586.         }
  587.         drm_modeset_unlock_all(dev);
  588.  
  589.         if (fb_helper->delayed_hotplug) {
  590.                 fb_helper->delayed_hotplug = false;
  591.                 drm_fb_helper_hotplug_event(fb_helper);
  592.         }
  593.         return 0;
  594. }
  595. EXPORT_SYMBOL(drm_fb_helper_set_par);
  596.  
  597. /**
  598.  * drm_fb_helper_pan_display - implementation for ->fb_pan_display
  599.  * @var: updated screen information
  600.  * @info: fbdev registered by the helper
  601.  */
  602. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  603.                               struct fb_info *info)
  604. {
  605.         struct drm_fb_helper *fb_helper = info->par;
  606.         struct drm_device *dev = fb_helper->dev;
  607.         struct drm_mode_set *modeset;
  608.         int ret = 0;
  609.         int i;
  610.  
  611.         drm_modeset_lock_all(dev);
  612.         if (!drm_fb_helper_is_bound(fb_helper)) {
  613.                 drm_modeset_unlock_all(dev);
  614.                 return -EBUSY;
  615.         }
  616.  
  617.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  618.                 modeset = &fb_helper->crtc_info[i].mode_set;
  619.  
  620.                 modeset->x = var->xoffset;
  621.                 modeset->y = var->yoffset;
  622.  
  623.                 if (modeset->num_connectors) {
  624.                         ret = drm_mode_set_config_internal(modeset);
  625.                         if (!ret) {
  626.                                 info->var.xoffset = var->xoffset;
  627.                                 info->var.yoffset = var->yoffset;
  628.                         }
  629.                 }
  630.         }
  631.         drm_modeset_unlock_all(dev);
  632.         return ret;
  633. }
  634. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  635.  
  636. /*
  637.  * Allocates the backing storage and sets up the fbdev info structure through
  638.  * the ->fb_probe callback and then registers the fbdev and sets up the panic
  639.  * notifier.
  640.  */
  641. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  642.                                   int preferred_bpp)
  643. {
  644.         int ret = 0;
  645.         int crtc_count = 0;
  646.         int i;
  647.         struct fb_info *info;
  648.         struct drm_fb_helper_surface_size sizes;
  649.         int gamma_size = 0;
  650.  
  651.         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  652.         sizes.surface_depth = 24;
  653.         sizes.surface_bpp = 32;
  654.         sizes.fb_width = (unsigned)-1;
  655.         sizes.fb_height = (unsigned)-1;
  656.  
  657.         /* if driver picks 8 or 16 by default use that
  658.            for both depth/bpp */
  659.         if (preferred_bpp != sizes.surface_bpp)
  660.                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  661.  
  662.         /* first up get a count of crtcs now in use and new min/maxes width/heights */
  663.         for (i = 0; i < fb_helper->connector_count; i++) {
  664.                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  665.                 struct drm_cmdline_mode *cmdline_mode;
  666.  
  667.                 cmdline_mode = &fb_helper_conn->cmdline_mode;
  668.  
  669.                 if (cmdline_mode->bpp_specified) {
  670.                         switch (cmdline_mode->bpp) {
  671.                         case 8:
  672.                                 sizes.surface_depth = sizes.surface_bpp = 8;
  673.                                 break;
  674.                         case 15:
  675.                                 sizes.surface_depth = 15;
  676.                                 sizes.surface_bpp = 16;
  677.                                 break;
  678.                         case 16:
  679.                                 sizes.surface_depth = sizes.surface_bpp = 16;
  680.                                 break;
  681.                         case 24:
  682.                                 sizes.surface_depth = sizes.surface_bpp = 24;
  683.                                 break;
  684.                         case 32:
  685.     sizes.surface_depth = 24;
  686.     sizes.surface_bpp = 32;
  687.                                 break;
  688.                         }
  689.                         break;
  690.                 }
  691.         }
  692.  
  693.         crtc_count = 0;
  694.         for (i = 0; i < fb_helper->crtc_count; i++) {
  695.                 struct drm_display_mode *desired_mode;
  696.                 desired_mode = fb_helper->crtc_info[i].desired_mode;
  697.  
  698.                 if (desired_mode) {
  699.                         if (gamma_size == 0)
  700.                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  701.                         if (desired_mode->hdisplay < sizes.fb_width)
  702.                                 sizes.fb_width = desired_mode->hdisplay;
  703.                         if (desired_mode->vdisplay < sizes.fb_height)
  704.                                 sizes.fb_height = desired_mode->vdisplay;
  705.                         if (desired_mode->hdisplay > sizes.surface_width)
  706.                                 sizes.surface_width = desired_mode->hdisplay;
  707.                         if (desired_mode->vdisplay > sizes.surface_height)
  708.                                 sizes.surface_height = desired_mode->vdisplay;
  709.                         crtc_count++;
  710.        }
  711.         }
  712.  
  713.         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  714.                 /* hmm everyone went away - assume VGA cable just fell out
  715.                    and will come back later. */
  716.                 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  717.                 sizes.fb_width = sizes.surface_width = 1024;
  718.                 sizes.fb_height = sizes.surface_height = 768;
  719.         }
  720.  
  721.         /* push down into drivers */
  722.         ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  723.         if (ret < 0)
  724.                 return ret;
  725.  
  726.         info = fb_helper->fbdev;
  727.  
  728.         /*
  729.          * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
  730.          * events, but at init time drm_setup_crtcs needs to be called before
  731.          * the fb is allocated (since we need to figure out the desired size of
  732.          * the fb before we can allocate it ...). Hence we need to fix things up
  733.          * here again.
  734.          */
  735.         for (i = 0; i < fb_helper->crtc_count; i++)
  736.                 if (fb_helper->crtc_info[i].mode_set.num_connectors)
  737.                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  738.  
  739.  
  740.                 info->var.pixclock = 0;
  741.  
  742.         list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  743.  
  744.         return 0;
  745. }
  746.  
  747. /**
  748.  * drm_fb_helper_fill_fix - initializes fixed fbdev information
  749.  * @info: fbdev registered by the helper
  750.  * @pitch: desired pitch
  751.  * @depth: desired depth
  752.  *
  753.  * Helper to fill in the fixed fbdev information useful for a non-accelerated
  754.  * fbdev emulations. Drivers which support acceleration methods which impose
  755.  * additional constraints need to set up their own limits.
  756.  *
  757.  * Drivers should call this (or their equivalent setup code) from their
  758.  * ->fb_probe callback.
  759.  */
  760. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  761.                             uint32_t depth)
  762. {
  763.         info->fix.type = FB_TYPE_PACKED_PIXELS;
  764.         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  765.                 FB_VISUAL_TRUECOLOR;
  766.         info->fix.mmio_start = 0;
  767.         info->fix.mmio_len = 0;
  768.         info->fix.type_aux = 0;
  769.         info->fix.xpanstep = 1; /* doing it in hw */
  770.         info->fix.ypanstep = 1; /* doing it in hw */
  771.         info->fix.ywrapstep = 0;
  772.         info->fix.accel = FB_ACCEL_NONE;
  773.         info->fix.type_aux = 0;
  774.  
  775.         info->fix.line_length = pitch;
  776.         return;
  777. }
  778. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  779.  
  780. /**
  781.  * drm_fb_helper_fill_var - initalizes variable fbdev information
  782.  * @info: fbdev instance to set up
  783.  * @fb_helper: fb helper instance to use as template
  784.  * @fb_width: desired fb width
  785.  * @fb_height: desired fb height
  786.  *
  787.  * Sets up the variable fbdev metainformation from the given fb helper instance
  788.  * and the drm framebuffer allocated in fb_helper->fb.
  789.  *
  790.  * Drivers should call this (or their equivalent setup code) from their
  791.  * ->fb_probe callback after having allocated the fbdev backing
  792.  * storage framebuffer.
  793.  */
  794. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  795.                             uint32_t fb_width, uint32_t fb_height)
  796. {
  797.         struct drm_framebuffer *fb = fb_helper->fb;
  798.         info->pseudo_palette = fb_helper->pseudo_palette;
  799.         info->var.xres_virtual = fb->width;
  800.         info->var.yres_virtual = fb->height;
  801.         info->var.bits_per_pixel = fb->bits_per_pixel;
  802.         info->var.accel_flags = FB_ACCELF_TEXT;
  803.         info->var.xoffset = 0;
  804.         info->var.yoffset = 0;
  805.         info->var.activate = FB_ACTIVATE_NOW;
  806.         info->var.height = -1;
  807.         info->var.width = -1;
  808.  
  809.         switch (fb->depth) {
  810.         case 8:
  811.                 info->var.red.offset = 0;
  812.                 info->var.green.offset = 0;
  813.                 info->var.blue.offset = 0;
  814.                 info->var.red.length = 8; /* 8bit DAC */
  815.                 info->var.green.length = 8;
  816.                 info->var.blue.length = 8;
  817.                 info->var.transp.offset = 0;
  818.                 info->var.transp.length = 0;
  819.                 break;
  820.         case 15:
  821.                 info->var.red.offset = 10;
  822.                 info->var.green.offset = 5;
  823.                 info->var.blue.offset = 0;
  824.                 info->var.red.length = 5;
  825.                 info->var.green.length = 5;
  826.                 info->var.blue.length = 5;
  827.                 info->var.transp.offset = 15;
  828.                 info->var.transp.length = 1;
  829.                 break;
  830.         case 16:
  831.                 info->var.red.offset = 11;
  832.                 info->var.green.offset = 5;
  833.                 info->var.blue.offset = 0;
  834.                 info->var.red.length = 5;
  835.                 info->var.green.length = 6;
  836.                 info->var.blue.length = 5;
  837.                 info->var.transp.offset = 0;
  838.                 break;
  839.         case 24:
  840.                 info->var.red.offset = 16;
  841.                 info->var.green.offset = 8;
  842.                 info->var.blue.offset = 0;
  843.                 info->var.red.length = 8;
  844.                 info->var.green.length = 8;
  845.                 info->var.blue.length = 8;
  846.                 info->var.transp.offset = 0;
  847.                 info->var.transp.length = 0;
  848.                 break;
  849.         case 32:
  850.                 info->var.red.offset = 16;
  851.                 info->var.green.offset = 8;
  852.                 info->var.blue.offset = 0;
  853.                 info->var.red.length = 8;
  854.                 info->var.green.length = 8;
  855.                 info->var.blue.length = 8;
  856.                 info->var.transp.offset = 24;
  857.                 info->var.transp.length = 8;
  858.                 break;
  859.         default:
  860.                 break;
  861.         }
  862.  
  863.         info->var.xres = fb_width;
  864.         info->var.yres = fb_height;
  865. }
  866. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  867.  
  868. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  869.                                                uint32_t maxX,
  870.                                                uint32_t maxY)
  871. {
  872.         struct drm_connector *connector;
  873.         int count = 0;
  874.         int i;
  875.  
  876.         for (i = 0; i < fb_helper->connector_count; i++) {
  877.                 connector = fb_helper->connector_info[i]->connector;
  878.                 count += connector->funcs->fill_modes(connector, maxX, maxY);
  879.         }
  880.  
  881.         return count;
  882. }
  883.  
  884. static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  885. {
  886.         struct drm_display_mode *mode;
  887.  
  888.         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  889.                 if (drm_mode_width(mode) > width ||
  890.                     drm_mode_height(mode) > height)
  891.                         continue;
  892.                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
  893.                         return mode;
  894.         }
  895.         return NULL;
  896. }
  897.  
  898. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  899. {
  900.         struct drm_cmdline_mode *cmdline_mode;
  901.         cmdline_mode = &fb_connector->cmdline_mode;
  902.         return cmdline_mode->specified;
  903. }
  904.  
  905. static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  906.                                                       int width, int height)
  907. {
  908.         struct drm_cmdline_mode *cmdline_mode;
  909.         struct drm_display_mode *mode = NULL;
  910.  
  911.         return NULL;
  912.  
  913.         cmdline_mode = &fb_helper_conn->cmdline_mode;
  914.         if (cmdline_mode->specified == false)
  915.                 return mode;
  916.  
  917.         /* attempt to find a matching mode in the list of modes
  918.          *  we have gotten so far, if not add a CVT mode that conforms
  919.          */
  920.         if (cmdline_mode->rb || cmdline_mode->margins)
  921.                 goto create_mode;
  922.  
  923.         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  924.                 /* check width/height */
  925.                 if (mode->hdisplay != cmdline_mode->xres ||
  926.                     mode->vdisplay != cmdline_mode->yres)
  927.                         continue;
  928.  
  929.                 if (cmdline_mode->refresh_specified) {
  930.                         if (mode->vrefresh != cmdline_mode->refresh)
  931.                                 continue;
  932.                 }
  933.  
  934.                 if (cmdline_mode->interlace) {
  935.                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  936.                                 continue;
  937.                 }
  938.                 return mode;
  939.         }
  940.  
  941. create_mode:
  942.         mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  943.                                                  cmdline_mode);
  944.         list_add(&mode->head, &fb_helper_conn->connector->modes);
  945.         return mode;
  946. }
  947.  
  948. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  949. {
  950.         bool enable;
  951.  
  952.         if (strict)
  953.                 enable = connector->status == connector_status_connected;
  954.         else
  955.                 enable = connector->status != connector_status_disconnected;
  956.  
  957.         return enable;
  958. }
  959.  
  960. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  961.                                   bool *enabled)
  962. {
  963.         bool any_enabled = false;
  964.         struct drm_connector *connector;
  965.         int i = 0;
  966.  
  967.         for (i = 0; i < fb_helper->connector_count; i++) {
  968.                 connector = fb_helper->connector_info[i]->connector;
  969.                 enabled[i] = drm_connector_enabled(connector, true);
  970.                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  971.                           enabled[i] ? "yes" : "no");
  972.                 any_enabled |= enabled[i];
  973.         }
  974.  
  975.         if (any_enabled)
  976.                 return;
  977.  
  978.         for (i = 0; i < fb_helper->connector_count; i++) {
  979.                 connector = fb_helper->connector_info[i]->connector;
  980.                 enabled[i] = drm_connector_enabled(connector, false);
  981.         }
  982. }
  983.  
  984. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  985.                               struct drm_display_mode **modes,
  986.                               bool *enabled, int width, int height)
  987. {
  988.         int count, i, j;
  989.         bool can_clone = false;
  990.         struct drm_fb_helper_connector *fb_helper_conn;
  991.         struct drm_display_mode *dmt_mode, *mode;
  992.  
  993.         /* only contemplate cloning in the single crtc case */
  994.         if (fb_helper->crtc_count > 1)
  995.                 return false;
  996.  
  997.         count = 0;
  998.         for (i = 0; i < fb_helper->connector_count; i++) {
  999.                 if (enabled[i])
  1000.                         count++;
  1001.         }
  1002.  
  1003.         /* only contemplate cloning if more than one connector is enabled */
  1004.         if (count <= 1)
  1005.                 return false;
  1006.  
  1007.         /* check the command line or if nothing common pick 1024x768 */
  1008.         can_clone = true;
  1009.         for (i = 0; i < fb_helper->connector_count; i++) {
  1010.                 if (!enabled[i])
  1011.                         continue;
  1012.                 fb_helper_conn = fb_helper->connector_info[i];
  1013.                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1014.                 if (!modes[i]) {
  1015.                         can_clone = false;
  1016.                         break;
  1017.                 }
  1018.                 for (j = 0; j < i; j++) {
  1019.                         if (!enabled[j])
  1020.                                 continue;
  1021.                         if (!drm_mode_equal(modes[j], modes[i]))
  1022.                                 can_clone = false;
  1023.                 }
  1024.         }
  1025.  
  1026.         if (can_clone) {
  1027.                 DRM_DEBUG_KMS("can clone using command line\n");
  1028.                 return true;
  1029.         }
  1030.  
  1031.         /* try and find a 1024x768 mode on each connector */
  1032.         can_clone = true;
  1033.         dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1034.  
  1035.         for (i = 0; i < fb_helper->connector_count; i++) {
  1036.  
  1037.                 if (!enabled[i])
  1038.                         continue;
  1039.  
  1040.                 fb_helper_conn = fb_helper->connector_info[i];
  1041.                 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1042.                         if (drm_mode_equal(mode, dmt_mode))
  1043.                                 modes[i] = mode;
  1044.                 }
  1045.                 if (!modes[i])
  1046.                         can_clone = false;
  1047.         }
  1048.  
  1049.         if (can_clone) {
  1050.                 DRM_DEBUG_KMS("can clone using 1024x768\n");
  1051.                 return true;
  1052.         }
  1053.         DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1054.         return false;
  1055. }
  1056.  
  1057. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1058.                                  struct drm_display_mode **modes,
  1059.                                  bool *enabled, int width, int height)
  1060. {
  1061.         struct drm_fb_helper_connector *fb_helper_conn;
  1062.         int i;
  1063.  
  1064.         for (i = 0; i < fb_helper->connector_count; i++) {
  1065.                 fb_helper_conn = fb_helper->connector_info[i];
  1066.  
  1067.                 if (enabled[i] == false)
  1068.                         continue;
  1069.  
  1070.                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1071.                               fb_helper_conn->connector->base.id);
  1072.  
  1073.                 /* got for command line mode first */
  1074.                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1075.                 if (!modes[i]) {
  1076.                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  1077.                                       fb_helper_conn->connector->base.id);
  1078.                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1079.                 }
  1080.                 /* No preferred modes, pick one off the list */
  1081.                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1082.                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1083.                                 break;
  1084.                 }
  1085.                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1086.                           "none");
  1087.         }
  1088.         return true;
  1089. }
  1090.  
  1091. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1092.                           struct drm_fb_helper_crtc **best_crtcs,
  1093.                           struct drm_display_mode **modes,
  1094.                           int n, int width, int height)
  1095. {
  1096.         int c, o;
  1097.         struct drm_device *dev = fb_helper->dev;
  1098.         struct drm_connector *connector;
  1099.         struct drm_connector_helper_funcs *connector_funcs;
  1100.         struct drm_encoder *encoder;
  1101.         int my_score, best_score, score;
  1102.         struct drm_fb_helper_crtc **crtcs, *crtc;
  1103.         struct drm_fb_helper_connector *fb_helper_conn;
  1104.  
  1105.         if (n == fb_helper->connector_count)
  1106.                 return 0;
  1107.  
  1108.         fb_helper_conn = fb_helper->connector_info[n];
  1109.         connector = fb_helper_conn->connector;
  1110.  
  1111.         best_crtcs[n] = NULL;
  1112.         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1113.         if (modes[n] == NULL)
  1114.                 return best_score;
  1115.  
  1116.         crtcs = kzalloc(dev->mode_config.num_connector *
  1117.                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1118.         if (!crtcs)
  1119.                 return best_score;
  1120.  
  1121.         my_score = 1;
  1122.         if (connector->status == connector_status_connected)
  1123.                 my_score++;
  1124.         if (drm_has_cmdline_mode(fb_helper_conn))
  1125.                 my_score++;
  1126.         if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1127.                 my_score++;
  1128.  
  1129.         connector_funcs = connector->helper_private;
  1130.         encoder = connector_funcs->best_encoder(connector);
  1131.         if (!encoder)
  1132.                 goto out;
  1133.  
  1134.         /* select a crtc for this connector and then attempt to configure
  1135.            remaining connectors */
  1136.         for (c = 0; c < fb_helper->crtc_count; c++) {
  1137.                 crtc = &fb_helper->crtc_info[c];
  1138.  
  1139.                 if ((encoder->possible_crtcs & (1 << c)) == 0)
  1140.                         continue;
  1141.  
  1142.                 for (o = 0; o < n; o++)
  1143.                         if (best_crtcs[o] == crtc)
  1144.                                 break;
  1145.  
  1146.                 if (o < n) {
  1147.                         /* ignore cloning unless only a single crtc */
  1148.                         if (fb_helper->crtc_count > 1)
  1149.                                 continue;
  1150.  
  1151.                         if (!drm_mode_equal(modes[o], modes[n]))
  1152.                                 continue;
  1153.                 }
  1154.  
  1155.                 crtcs[n] = crtc;
  1156.                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1157.                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1158.                                                   width, height);
  1159.                 if (score > best_score) {
  1160.                         best_score = score;
  1161.                         memcpy(best_crtcs, crtcs,
  1162.                                dev->mode_config.num_connector *
  1163.                                sizeof(struct drm_fb_helper_crtc *));
  1164.                 }
  1165.         }
  1166. out:
  1167.         kfree(crtcs);
  1168.         return best_score;
  1169. }
  1170.  
  1171. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1172. {
  1173.         struct drm_device *dev = fb_helper->dev;
  1174.         struct drm_fb_helper_crtc **crtcs;
  1175.         struct drm_display_mode **modes;
  1176.         struct drm_mode_set *modeset;
  1177.         bool *enabled;
  1178.         int width, height;
  1179.         int i;
  1180.  
  1181.         DRM_DEBUG_KMS("\n");
  1182.  
  1183.         width = dev->mode_config.max_width;
  1184.         height = dev->mode_config.max_height;
  1185.  
  1186.         crtcs = kcalloc(dev->mode_config.num_connector,
  1187.                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1188.         modes = kcalloc(dev->mode_config.num_connector,
  1189.                         sizeof(struct drm_display_mode *), GFP_KERNEL);
  1190.         enabled = kcalloc(dev->mode_config.num_connector,
  1191.                           sizeof(bool), GFP_KERNEL);
  1192.         if (!crtcs || !modes || !enabled) {
  1193.                 DRM_ERROR("Memory allocation failed\n");
  1194.                 goto out;
  1195.         }
  1196.  
  1197.  
  1198.         drm_enable_connectors(fb_helper, enabled);
  1199.  
  1200.         if (!(fb_helper->funcs->initial_config &&
  1201.               fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  1202.                                                enabled, width, height))) {
  1203.                 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
  1204.                 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
  1205.  
  1206.                 if (!drm_target_cloned(fb_helper,
  1207.                                        modes, enabled, width, height) &&
  1208.                     !drm_target_preferred(fb_helper,
  1209.                                           modes, enabled, width, height))
  1210.                         DRM_ERROR("Unable to find initial modes\n");
  1211.  
  1212.                 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  1213.                               width, height);
  1214.  
  1215.         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1216.         }
  1217.  
  1218.         /* need to set the modesets up here for use later */
  1219.         /* fill out the connector<->crtc mappings into the modesets */
  1220.         for (i = 0; i < fb_helper->crtc_count; i++) {
  1221.                 modeset = &fb_helper->crtc_info[i].mode_set;
  1222.                 modeset->num_connectors = 0;
  1223.                 modeset->fb = NULL;
  1224.         }
  1225.  
  1226.         for (i = 0; i < fb_helper->connector_count; i++) {
  1227.                 struct drm_display_mode *mode = modes[i];
  1228.                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1229.                 modeset = &fb_crtc->mode_set;
  1230.  
  1231.                 if (mode && fb_crtc) {
  1232.                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1233.                                       mode->name, fb_crtc->mode_set.crtc->base.id);
  1234.                         fb_crtc->desired_mode = mode;
  1235.                         if (modeset->mode)
  1236.                                 drm_mode_destroy(dev, modeset->mode);
  1237.                         modeset->mode = drm_mode_duplicate(dev,
  1238.                                                            fb_crtc->desired_mode);
  1239.                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1240.                         modeset->fb = fb_helper->fb;
  1241.                 }
  1242.         }
  1243.  
  1244.         /* Clear out any old modes if there are no more connected outputs. */
  1245.         for (i = 0; i < fb_helper->crtc_count; i++) {
  1246.                 modeset = &fb_helper->crtc_info[i].mode_set;
  1247.                 if (modeset->num_connectors == 0) {
  1248.                         BUG_ON(modeset->fb);
  1249.                         BUG_ON(modeset->num_connectors);
  1250.                         if (modeset->mode)
  1251.                                 drm_mode_destroy(dev, modeset->mode);
  1252.                         modeset->mode = NULL;
  1253.                 }
  1254.         }
  1255. out:
  1256.         kfree(crtcs);
  1257.         kfree(modes);
  1258.         kfree(enabled);
  1259. }
  1260.  
  1261. /**
  1262.  * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1263.  * @fb_helper: fb_helper device struct
  1264.  * @bpp_sel: bpp value to use for the framebuffer configuration
  1265.  *
  1266.  * Scans the CRTCs and connectors and tries to put together an initial setup.
  1267.  * At the moment, this is a cloned configuration across all heads with
  1268.  * a new framebuffer object as the backing store.
  1269.  *
  1270.  * Note that this also registers the fbdev and so allows userspace to call into
  1271.  * the driver through the fbdev interfaces.
  1272.  *
  1273.  * This function will call down into the ->fb_probe callback to let
  1274.  * the driver allocate and initialize the fbdev info structure and the drm
  1275.  * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  1276.  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  1277.  * values for the fbdev info structure.
  1278.  *
  1279.  * RETURNS:
  1280.  * Zero if everything went ok, nonzero otherwise.
  1281.  */
  1282. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1283. {
  1284.         struct drm_device *dev = fb_helper->dev;
  1285.         int count = 0;
  1286.  
  1287. //   drm_fb_helper_parse_command_line(fb_helper);
  1288.  
  1289.         count = drm_fb_helper_probe_connector_modes(fb_helper,
  1290.                                                     dev->mode_config.max_width,
  1291.                                                     dev->mode_config.max_height);
  1292.         /*
  1293.          * we shouldn't end up with no modes here.
  1294.          */
  1295.         if (count == 0)
  1296.                 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
  1297.  
  1298.         drm_setup_crtcs(fb_helper);
  1299.  
  1300.         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1301. }
  1302. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1303.  
  1304. /**
  1305.  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1306.  *                               probing all the outputs attached to the fb
  1307.  * @fb_helper: the drm_fb_helper
  1308.  *
  1309.  * Scan the connectors attached to the fb_helper and try to put together a
  1310.  * setup after *notification of a change in output configuration.
  1311.  *
  1312.  * Called at runtime, takes the mode config locks to be able to check/change the
  1313.  * modeset configuration. Must be run from process context (which usually means
  1314.  * either the output polling work or a work item launched from the driver's
  1315.  * hotplug interrupt).
  1316.  *
  1317.  * Note that the driver must ensure that this is only called _after_ the fb has
  1318.  * been fully set up, i.e. after the call to drm_fb_helper_initial_config.
  1319.  *
  1320.  * RETURNS:
  1321.  * 0 on success and a non-zero error code otherwise.
  1322.  */
  1323. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1324. {
  1325.         struct drm_device *dev = fb_helper->dev;
  1326.         u32 max_width, max_height;
  1327.  
  1328.         if (!fb_helper->fb)
  1329.                 return 0;
  1330.  
  1331.         mutex_lock(&fb_helper->dev->mode_config.mutex);
  1332.         if (!drm_fb_helper_is_bound(fb_helper)) {
  1333.                 fb_helper->delayed_hotplug = true;
  1334.                 mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1335.                 return 0;
  1336.         }
  1337.         DRM_DEBUG_KMS("\n");
  1338.  
  1339.         max_width = fb_helper->fb->width;
  1340.         max_height = fb_helper->fb->height;
  1341.  
  1342.         drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
  1343.         mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1344.  
  1345.         drm_modeset_lock_all(dev);
  1346.         drm_setup_crtcs(fb_helper);
  1347.         drm_modeset_unlock_all(dev);
  1348.         drm_fb_helper_set_par(fb_helper->fbdev);
  1349.  
  1350.         return 0;
  1351. }
  1352. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1353.  
  1354. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1355.  * but the module doesn't depend on any fb console symbols.  At least
  1356.  * attempt to load fbcon to avoid leaving the system without a usable console.
  1357.  */
  1358. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  1359. static int __init drm_fb_helper_modinit(void)
  1360. {
  1361.         const char *name = "fbcon";
  1362.         struct module *fbcon;
  1363.  
  1364.         mutex_lock(&module_mutex);
  1365.         fbcon = find_module(name);
  1366.         mutex_unlock(&module_mutex);
  1367.  
  1368.         if (!fbcon)
  1369.                 request_module_nowait(name);
  1370.         return 0;
  1371. }
  1372.  
  1373. module_init(drm_fb_helper_modinit);
  1374. #endif
  1375.