2 * Copyright (C) 2012 Red Hat
3 * based in parts on udlfb.c:
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
6 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License v2. See the file COPYING in the main directory of this archive for
14 #include <drm/drm_crtc.h>
15 #include <drm/drm_edid.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_probe_helper.h>
18 #include "udl_connector.h"
21 static bool udl_get_edid_block(struct udl_device *udl, int block_idx,
27 read_buff = kmalloc(2, GFP_KERNEL);
31 for (i = 0; i < EDID_LENGTH; i++) {
32 int bval = (i + block_idx * EDID_LENGTH) << 8;
33 ret = usb_control_msg(udl->udev,
34 usb_rcvctrlpipe(udl->udev, 0),
35 (0x02), (0x80 | (0x02 << 5)), bval,
36 0xA1, read_buff, 2, HZ);
38 DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret);
42 buff[i] = read_buff[1];
49 static bool udl_get_edid(struct udl_device *udl, u8 **result_buff,
50 int *result_buff_size)
53 u8 *block_buff = NULL, *buff_ptr;
55 block_buff = kmalloc(EDID_LENGTH, GFP_KERNEL);
56 if (block_buff == NULL)
59 if (udl_get_edid_block(udl, 0, block_buff) &&
60 memchr_inv(block_buff, 0, EDID_LENGTH)) {
61 extensions = ((struct edid *)block_buff)->extensions;
63 /* we have to read all extensions one by one */
64 *result_buff_size = EDID_LENGTH * (extensions + 1);
65 *result_buff = kmalloc(*result_buff_size, GFP_KERNEL);
66 buff_ptr = *result_buff;
67 if (buff_ptr == NULL) {
71 memcpy(buff_ptr, block_buff, EDID_LENGTH);
73 buff_ptr += EDID_LENGTH;
74 for (i = 1; i < extensions; ++i) {
75 if (udl_get_edid_block(udl, i, buff_ptr)) {
76 buff_ptr += EDID_LENGTH;
85 /* we have only base edid block */
86 *result_buff = block_buff;
87 *result_buff_size = EDID_LENGTH;
96 static int udl_get_modes(struct drm_connector *connector)
98 struct udl_drm_connector *udl_connector =
99 container_of(connector,
100 struct udl_drm_connector,
103 drm_connector_update_edid_property(connector, udl_connector->edid);
104 if (udl_connector->edid)
105 return drm_add_edid_modes(connector, udl_connector->edid);
109 static enum drm_mode_status udl_mode_valid(struct drm_connector *connector,
110 struct drm_display_mode *mode)
112 struct udl_device *udl = connector->dev->dev_private;
113 if (!udl->sku_pixel_limit)
116 if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
117 return MODE_VIRTUAL_Y;
122 static enum drm_connector_status
123 udl_detect(struct drm_connector *connector, bool force)
125 u8 *edid_buff = NULL;
126 int edid_buff_size = 0;
127 struct udl_device *udl = connector->dev->dev_private;
128 struct udl_drm_connector *udl_connector =
129 container_of(connector,
130 struct udl_drm_connector,
133 /* cleanup previous edid */
134 if (udl_connector->edid != NULL) {
135 kfree(udl_connector->edid);
136 udl_connector->edid = NULL;
140 if (!udl_get_edid(udl, &edid_buff, &edid_buff_size))
141 return connector_status_disconnected;
143 udl_connector->edid = (struct edid *)edid_buff;
145 return connector_status_connected;
148 static struct drm_encoder*
149 udl_best_single_encoder(struct drm_connector *connector)
151 int enc_id = connector->encoder_ids[0];
152 return drm_encoder_find(connector->dev, NULL, enc_id);
155 static int udl_connector_set_property(struct drm_connector *connector,
156 struct drm_property *property,
162 static void udl_connector_destroy(struct drm_connector *connector)
164 struct udl_drm_connector *udl_connector =
165 container_of(connector,
166 struct udl_drm_connector,
169 drm_connector_unregister(connector);
170 drm_connector_cleanup(connector);
171 kfree(udl_connector->edid);
175 static const struct drm_connector_helper_funcs udl_connector_helper_funcs = {
176 .get_modes = udl_get_modes,
177 .mode_valid = udl_mode_valid,
178 .best_encoder = udl_best_single_encoder,
181 static const struct drm_connector_funcs udl_connector_funcs = {
182 .dpms = drm_helper_connector_dpms,
183 .detect = udl_detect,
184 .fill_modes = drm_helper_probe_single_connector_modes,
185 .destroy = udl_connector_destroy,
186 .set_property = udl_connector_set_property,
189 int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
191 struct udl_drm_connector *udl_connector;
192 struct drm_connector *connector;
194 udl_connector = kzalloc(sizeof(struct udl_drm_connector), GFP_KERNEL);
198 connector = &udl_connector->connector;
199 drm_connector_init(dev, connector, &udl_connector_funcs,
200 DRM_MODE_CONNECTOR_DVII);
201 drm_connector_helper_add(connector, &udl_connector_helper_funcs);
203 drm_connector_register(connector);
204 drm_connector_attach_encoder(connector, encoder);
205 connector->polled = DRM_CONNECTOR_POLL_HPD |
206 DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;