brcm2708: rename target to bcm27xx
[openwrt/staging/wigyori.git] / target / linux / bcm27xx / patches-4.19 / 950-0639-drm-modes-Rewrite-the-command-line-parser.patch
1 From 3508a8548f13be68b6d098ad99a7bc1fc1810f76 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime.ripard@bootlin.com>
3 Date: Wed, 19 Jun 2019 12:17:49 +0200
4 Subject: [PATCH] drm/modes: Rewrite the command line parser
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 commit e08ab74bd4c7a5fe311bc05f32dbb4f1e7fa3428 upstream.
10
11 Rewrite the command line parser in order to get away from the state machine
12 parsing the video mode lines.
13
14 Hopefully, this will allow to extend it more easily to support named modes
15 and / or properties set directly on the command line.
16
17 Reviewed-by: Noralf Trønnes <noralf@tronnes.org>
18 Signed-off-by: Maxime Ripard <maxime.ripard@bootlin.com>
19 Link: https://patchwork.freedesktop.org/patch/msgid/e32cd4009153b184103554009135c7bf7c9975d7.1560783090.git-series.maxime.ripard@bootlin.com
20 ---
21 drivers/gpu/drm/drm_modes.c | 325 +++++++++++++++++++++++-------------
22 1 file changed, 210 insertions(+), 115 deletions(-)
23
24 --- a/drivers/gpu/drm/drm_modes.c
25 +++ b/drivers/gpu/drm/drm_modes.c
26 @@ -30,6 +30,7 @@
27 * authorization from the copyright holder(s) and author(s).
28 */
29
30 +#include <linux/ctype.h>
31 #include <linux/list.h>
32 #include <linux/list_sort.h>
33 #include <linux/export.h>
34 @@ -1414,6 +1415,151 @@ void drm_connector_list_update(struct dr
35 }
36 EXPORT_SYMBOL(drm_connector_list_update);
37
38 +static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr,
39 + struct drm_cmdline_mode *mode)
40 +{
41 + unsigned int bpp;
42 +
43 + if (str[0] != '-')
44 + return -EINVAL;
45 +
46 + str++;
47 + bpp = simple_strtol(str, end_ptr, 10);
48 + if (*end_ptr == str)
49 + return -EINVAL;
50 +
51 + mode->bpp = bpp;
52 + mode->bpp_specified = true;
53 +
54 + return 0;
55 +}
56 +
57 +static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr,
58 + struct drm_cmdline_mode *mode)
59 +{
60 + unsigned int refresh;
61 +
62 + if (str[0] != '@')
63 + return -EINVAL;
64 +
65 + str++;
66 + refresh = simple_strtol(str, end_ptr, 10);
67 + if (*end_ptr == str)
68 + return -EINVAL;
69 +
70 + mode->refresh = refresh;
71 + mode->refresh_specified = true;
72 +
73 + return 0;
74 +}
75 +
76 +static int drm_mode_parse_cmdline_extra(const char *str, int length,
77 + struct drm_connector *connector,
78 + struct drm_cmdline_mode *mode)
79 +{
80 + int i;
81 +
82 + for (i = 0; i < length; i++) {
83 + switch (str[i]) {
84 + case 'i':
85 + mode->interlace = true;
86 + break;
87 + case 'm':
88 + mode->margins = true;
89 + break;
90 + case 'D':
91 + if (mode->force != DRM_FORCE_UNSPECIFIED)
92 + return -EINVAL;
93 +
94 + if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
95 + (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
96 + mode->force = DRM_FORCE_ON;
97 + else
98 + mode->force = DRM_FORCE_ON_DIGITAL;
99 + break;
100 + case 'd':
101 + if (mode->force != DRM_FORCE_UNSPECIFIED)
102 + return -EINVAL;
103 +
104 + mode->force = DRM_FORCE_OFF;
105 + break;
106 + case 'e':
107 + if (mode->force != DRM_FORCE_UNSPECIFIED)
108 + return -EINVAL;
109 +
110 + mode->force = DRM_FORCE_ON;
111 + break;
112 + default:
113 + return -EINVAL;
114 + }
115 + }
116 +
117 + return 0;
118 +}
119 +
120 +static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length,
121 + bool extras,
122 + struct drm_connector *connector,
123 + struct drm_cmdline_mode *mode)
124 +{
125 + const char *str_start = str;
126 + bool rb = false, cvt = false;
127 + int xres = 0, yres = 0;
128 + int remaining, i;
129 + char *end_ptr;
130 +
131 + xres = simple_strtol(str, &end_ptr, 10);
132 + if (end_ptr == str)
133 + return -EINVAL;
134 +
135 + if (end_ptr[0] != 'x')
136 + return -EINVAL;
137 + end_ptr++;
138 +
139 + str = end_ptr;
140 + yres = simple_strtol(str, &end_ptr, 10);
141 + if (end_ptr == str)
142 + return -EINVAL;
143 +
144 + remaining = length - (end_ptr - str_start);
145 + if (remaining < 0)
146 + return -EINVAL;
147 +
148 + for (i = 0; i < remaining; i++) {
149 + switch (end_ptr[i]) {
150 + case 'M':
151 + cvt = true;
152 + break;
153 + case 'R':
154 + rb = true;
155 + break;
156 + default:
157 + /*
158 + * Try to pass that to our extras parsing
159 + * function to handle the case where the
160 + * extras are directly after the resolution
161 + */
162 + if (extras) {
163 + int ret = drm_mode_parse_cmdline_extra(end_ptr + i,
164 + 1,
165 + connector,
166 + mode);
167 + if (ret)
168 + return ret;
169 + } else {
170 + return -EINVAL;
171 + }
172 + }
173 + }
174 +
175 + mode->xres = xres;
176 + mode->yres = yres;
177 + mode->cvt = cvt;
178 + mode->rb = rb;
179 +
180 + return 0;
181 +}
182 +
183 /**
184 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
185 * @mode_option: optional per connector mode option
186 @@ -1440,13 +1586,12 @@ bool drm_mode_parse_command_line_for_con
187 struct drm_cmdline_mode *mode)
188 {
189 const char *name;
190 - unsigned int namelen;
191 - bool res_specified = false, bpp_specified = false, refresh_specified = false;
192 - unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
193 - bool yres_specified = false, cvt = false, rb = false;
194 - bool interlace = false, margins = false, was_digit = false;
195 - int i;
196 - enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
197 + bool parse_extras = false;
198 + unsigned int bpp_off = 0, refresh_off = 0;
199 + unsigned int mode_end = 0;
200 + char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL;
201 + char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL;
202 + int ret;
203
204 #ifdef CONFIG_FB
205 if (!mode_option)
206 @@ -1459,127 +1604,77 @@ bool drm_mode_parse_command_line_for_con
207 }
208
209 name = mode_option;
210 - namelen = strlen(name);
211 - for (i = namelen-1; i >= 0; i--) {
212 - switch (name[i]) {
213 - case '@':
214 - if (!refresh_specified && !bpp_specified &&
215 - !yres_specified && !cvt && !rb && was_digit) {
216 - refresh = simple_strtol(&name[i+1], NULL, 10);
217 - refresh_specified = true;
218 - was_digit = false;
219 - } else
220 - goto done;
221 - break;
222 - case '-':
223 - if (!bpp_specified && !yres_specified && !cvt &&
224 - !rb && was_digit) {
225 - bpp = simple_strtol(&name[i+1], NULL, 10);
226 - bpp_specified = true;
227 - was_digit = false;
228 - } else
229 - goto done;
230 - break;
231 - case 'x':
232 - if (!yres_specified && was_digit) {
233 - yres = simple_strtol(&name[i+1], NULL, 10);
234 - yres_specified = true;
235 - was_digit = false;
236 - } else
237 - goto done;
238 - break;
239 - case '0' ... '9':
240 - was_digit = true;
241 - break;
242 - case 'M':
243 - if (yres_specified || cvt || was_digit)
244 - goto done;
245 - cvt = true;
246 - break;
247 - case 'R':
248 - if (yres_specified || cvt || rb || was_digit)
249 - goto done;
250 - rb = true;
251 - break;
252 - case 'm':
253 - if (cvt || yres_specified || was_digit)
254 - goto done;
255 - margins = true;
256 - break;
257 - case 'i':
258 - if (cvt || yres_specified || was_digit)
259 - goto done;
260 - interlace = true;
261 - break;
262 - case 'e':
263 - if (yres_specified || bpp_specified || refresh_specified ||
264 - was_digit || (force != DRM_FORCE_UNSPECIFIED))
265 - goto done;
266
267 - force = DRM_FORCE_ON;
268 - break;
269 - case 'D':
270 - if (yres_specified || bpp_specified || refresh_specified ||
271 - was_digit || (force != DRM_FORCE_UNSPECIFIED))
272 - goto done;
273 + if (!isdigit(name[0]))
274 + return false;
275
276 - if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
277 - (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
278 - force = DRM_FORCE_ON;
279 - else
280 - force = DRM_FORCE_ON_DIGITAL;
281 - break;
282 - case 'd':
283 - if (yres_specified || bpp_specified || refresh_specified ||
284 - was_digit || (force != DRM_FORCE_UNSPECIFIED))
285 - goto done;
286 + /* Try to locate the bpp and refresh specifiers, if any */
287 + bpp_ptr = strchr(name, '-');
288 + if (bpp_ptr) {
289 + bpp_off = bpp_ptr - name;
290 + mode->bpp_specified = true;
291 + }
292
293 - force = DRM_FORCE_OFF;
294 - break;
295 - default:
296 - goto done;
297 - }
298 + refresh_ptr = strchr(name, '@');
299 + if (refresh_ptr) {
300 + refresh_off = refresh_ptr - name;
301 + mode->refresh_specified = true;
302 }
303
304 - if (i < 0 && yres_specified) {
305 - char *ch;
306 - xres = simple_strtol(name, &ch, 10);
307 - if ((ch != NULL) && (*ch == 'x'))
308 - res_specified = true;
309 - else
310 - i = ch - name;
311 - } else if (!yres_specified && was_digit) {
312 - /* catch mode that begins with digits but has no 'x' */
313 - i = 0;
314 - }
315 -done:
316 - if (i >= 0) {
317 - pr_warn("[drm] parse error at position %i in video mode '%s'\n",
318 - i, name);
319 - mode->specified = false;
320 - return false;
321 + /* Locate the end of the name / resolution, and parse it */
322 + if (bpp_ptr && refresh_ptr) {
323 + mode_end = min(bpp_off, refresh_off);
324 + } else if (bpp_ptr) {
325 + mode_end = bpp_off;
326 + } else if (refresh_ptr) {
327 + mode_end = refresh_off;
328 + } else {
329 + mode_end = strlen(name);
330 + parse_extras = true;
331 }
332
333 - if (res_specified) {
334 - mode->specified = true;
335 - mode->xres = xres;
336 - mode->yres = yres;
337 + ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
338 + parse_extras,
339 + connector,
340 + mode);
341 + if (ret)
342 + return false;
343 + mode->specified = true;
344 +
345 + if (bpp_ptr) {
346 + ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode);
347 + if (ret)
348 + return false;
349 }
350
351 - if (refresh_specified) {
352 - mode->refresh_specified = true;
353 - mode->refresh = refresh;
354 + if (refresh_ptr) {
355 + ret = drm_mode_parse_cmdline_refresh(refresh_ptr,
356 + &refresh_end_ptr, mode);
357 + if (ret)
358 + return false;
359 }
360
361 - if (bpp_specified) {
362 - mode->bpp_specified = true;
363 - mode->bpp = bpp;
364 + /*
365 + * Locate the end of the bpp / refresh, and parse the extras
366 + * if relevant
367 + */
368 + if (bpp_ptr && refresh_ptr)
369 + extra_ptr = max(bpp_end_ptr, refresh_end_ptr);
370 + else if (bpp_ptr)
371 + extra_ptr = bpp_end_ptr;
372 + else if (refresh_ptr)
373 + extra_ptr = refresh_end_ptr;
374 +
375 + if (extra_ptr) {
376 + int remaining = strlen(name) - (extra_ptr - name);
377 +
378 + /*
379 + * We still have characters to process, while
380 + * we shouldn't have any
381 + */
382 + if (remaining > 0)
383 + return false;
384 }
385 - mode->rb = rb;
386 - mode->cvt = cvt;
387 - mode->interlace = interlace;
388 - mode->margins = margins;
389 - mode->force = force;
390
391 return true;
392 }