fs/proc/proc_sysctl.c: fix NULL pointer dereference in put_links
[muen/linux.git] / drivers / gpu / drm / udl / udl_connector.c
1 /*
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>
7  *
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
10  * more details.
11  */
12
13 #include <drm/drmP.h>
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"
19 #include "udl_drv.h"
20
21 static bool udl_get_edid_block(struct udl_device *udl, int block_idx,
22                                                            u8 *buff)
23 {
24         int ret, i;
25         u8 *read_buff;
26
27         read_buff = kmalloc(2, GFP_KERNEL);
28         if (!read_buff)
29                 return false;
30
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);
37                 if (ret < 1) {
38                         DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret);
39                         kfree(read_buff);
40                         return false;
41                 }
42                 buff[i] = read_buff[1];
43         }
44
45         kfree(read_buff);
46         return true;
47 }
48
49 static bool udl_get_edid(struct udl_device *udl, u8 **result_buff,
50                          int *result_buff_size)
51 {
52         int i, extensions;
53         u8 *block_buff = NULL, *buff_ptr;
54
55         block_buff = kmalloc(EDID_LENGTH, GFP_KERNEL);
56         if (block_buff == NULL)
57                 return false;
58
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;
62                 if (extensions > 0) {
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) {
68                                 kfree(block_buff);
69                                 return false;
70                         }
71                         memcpy(buff_ptr, block_buff, EDID_LENGTH);
72                         kfree(block_buff);
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;
77                                 } else {
78                                         kfree(*result_buff);
79                                         *result_buff = NULL;
80                                         return false;
81                                 }
82                         }
83                         return true;
84                 }
85                 /* we have only base edid block */
86                 *result_buff = block_buff;
87                 *result_buff_size = EDID_LENGTH;
88                 return true;
89         }
90
91         kfree(block_buff);
92
93         return false;
94 }
95
96 static int udl_get_modes(struct drm_connector *connector)
97 {
98         struct udl_drm_connector *udl_connector =
99                                         container_of(connector,
100                                         struct udl_drm_connector,
101                                         connector);
102
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);
106         return 0;
107 }
108
109 static enum drm_mode_status udl_mode_valid(struct drm_connector *connector,
110                           struct drm_display_mode *mode)
111 {
112         struct udl_device *udl = connector->dev->dev_private;
113         if (!udl->sku_pixel_limit)
114                 return 0;
115
116         if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
117                 return MODE_VIRTUAL_Y;
118
119         return 0;
120 }
121
122 static enum drm_connector_status
123 udl_detect(struct drm_connector *connector, bool force)
124 {
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,
131                                         connector);
132
133         /* cleanup previous edid */
134         if (udl_connector->edid != NULL) {
135                 kfree(udl_connector->edid);
136                 udl_connector->edid = NULL;
137         }
138
139
140         if (!udl_get_edid(udl, &edid_buff, &edid_buff_size))
141                 return connector_status_disconnected;
142
143         udl_connector->edid = (struct edid *)edid_buff;
144         
145         return connector_status_connected;
146 }
147
148 static struct drm_encoder*
149 udl_best_single_encoder(struct drm_connector *connector)
150 {
151         int enc_id = connector->encoder_ids[0];
152         return drm_encoder_find(connector->dev, NULL, enc_id);
153 }
154
155 static int udl_connector_set_property(struct drm_connector *connector,
156                                       struct drm_property *property,
157                                       uint64_t val)
158 {
159         return 0;
160 }
161
162 static void udl_connector_destroy(struct drm_connector *connector)
163 {
164         struct udl_drm_connector *udl_connector =
165                                         container_of(connector,
166                                         struct udl_drm_connector,
167                                         connector);
168
169         drm_connector_unregister(connector);
170         drm_connector_cleanup(connector);
171         kfree(udl_connector->edid);
172         kfree(connector);
173 }
174
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,
179 };
180
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,
187 };
188
189 int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
190 {
191         struct udl_drm_connector *udl_connector;
192         struct drm_connector *connector;
193
194         udl_connector = kzalloc(sizeof(struct udl_drm_connector), GFP_KERNEL);
195         if (!udl_connector)
196                 return -ENOMEM;
197
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);
202
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;
207
208         return 0;
209 }