aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/brcm2708/patches-4.19/950-0729-media-bcm2835-unicam-Rework-to-not-cache-the-list-of.patch
blob: 3a638710f74b0a6052aadce864b326b0e9c1263f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
From 2c51b8e533a8b43bde18072c9dbbd0fc5084bbe7 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.org>
Date: Wed, 2 Oct 2019 17:40:38 +0100
Subject: [PATCH] media: bcm2835-unicam: Rework to not cache the list
 of active fmts

Some sensors will change Bayer order based on H & V flips,
therefore collecting the list of formats at async_bound has
problems.

Enumerate the formats from the sensor every time.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
---
 .../media/platform/bcm2835/bcm2835-unicam.c   | 246 ++++++++++--------
 1 file changed, 136 insertions(+), 110 deletions(-)

--- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
+++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
@@ -363,8 +363,6 @@ struct unicam_device {
 	/* Used to store current mbus frame format */
 	struct v4l2_mbus_framefmt m_fmt;
 
-	struct unicam_fmt active_fmts[MAX_POSSIBLE_PIX_FMTS];
-	int num_active_fmt;
 	unsigned int virtual_channel;
 	enum v4l2_mbus_type bus_type;
 	/*
@@ -455,48 +453,30 @@ static int find_mbus_depth_by_code(u32 c
 	return 0;
 }
 
-static const struct unicam_fmt *find_format_by_code(struct unicam_device *dev,
-						    u32 code)
+static const struct unicam_fmt *find_format_by_code(u32 code)
 {
-	const struct unicam_fmt *fmt;
 	unsigned int k;
 
-	for (k = 0; k < dev->num_active_fmt; k++) {
-		fmt = &dev->active_fmts[k];
-		if (fmt->code == code)
-			return fmt;
+	for (k = 0; k < ARRAY_SIZE(formats); k++) {
+		if (formats[k].code == code)
+			return &formats[k];
 	}
 
 	return NULL;
 }
 
-static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
-						   u32 pixelformat)
+static const struct unicam_fmt *find_format_by_pix(u32 pixelformat)
 {
-	const struct unicam_fmt *fmt;
 	unsigned int k;
 
-	for (k = 0; k < dev->num_active_fmt; k++) {
-		fmt = &dev->active_fmts[k];
-		if (fmt->fourcc == pixelformat)
-			return fmt;
+	for (k = 0; k < ARRAY_SIZE(formats); k++) {
+		if (formats[k].fourcc == pixelformat)
+			return &formats[k];
 	}
 
 	return NULL;
 }
 
-static void dump_active_formats(struct unicam_device *dev)
-{
-	int i;
-
-	for (i = 0; i < dev->num_active_fmt; i++) {
-		unicam_dbg(3, dev, "active_fmt[%d] (%p) is code %04x, fourcc " V4L2_FOURCC_CONV ", depth %d\n",
-			   i, &dev->active_fmts[i], dev->active_fmts[i].code,
-			   V4L2_FOURCC_CONV_ARGS(dev->active_fmts[i].fourcc),
-			   dev->active_fmts[i].depth);
-	}
-}
-
 static inline unsigned int bytes_per_line(u32 width,
 					  const struct unicam_fmt *fmt)
 {
@@ -726,14 +706,40 @@ static int unicam_enum_fmt_vid_cap(struc
 				   struct v4l2_fmtdesc *f)
 {
 	struct unicam_device *dev = video_drvdata(file);
+	struct v4l2_subdev_mbus_code_enum mbus_code;
 	const struct unicam_fmt *fmt = NULL;
+	int index = 0;
+	int ret = 0;
+	int i;
 
-	if (f->index >= dev->num_active_fmt)
-		return -EINVAL;
+	/* Loop whilst the sensor driver says it has more formats, but add a
+	 * failsafe against a dodgy driver at 128 (more than any sensor will
+	 * ever sensibly advertise)
+	 */
+	for (i = 0; !ret && i < 128 ; i++) {
+		memset(&mbus_code, 0, sizeof(mbus_code));
+		mbus_code.index = i;
 
-	fmt = &dev->active_fmts[f->index];
+		ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
+				       NULL, &mbus_code);
+		if (ret < 0) {
+			unicam_dbg(2, dev,
+				   "subdev->enum_mbus_code idx %d returned %d - index invalid\n",
+				   i, ret);
+			return -EINVAL;
+		}
 
-	f->pixelformat = fmt->fourcc;
+		fmt = find_format_by_code(mbus_code.code);
+		if (fmt) {
+			if (fmt->fourcc) {
+				if (index == f->index) {
+					f->pixelformat = fmt->fourcc;
+					break;
+				}
+				index++;
+			}
+		}
+	}
 
 	return 0;
 }
@@ -748,6 +754,39 @@ static int unicam_g_fmt_vid_cap(struct f
 	return 0;
 }
 
+static
+const struct unicam_fmt *get_first_supported_format(struct unicam_device *dev)
+{
+	struct v4l2_subdev_mbus_code_enum mbus_code;
+	const struct unicam_fmt *fmt = NULL;
+	int ret;
+	int j;
+
+	for (j = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++j) {
+		memset(&mbus_code, 0, sizeof(mbus_code));
+		mbus_code.index = j;
+		ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
+				       &mbus_code);
+		if (ret < 0) {
+			unicam_dbg(2, dev,
+				   "subdev->enum_mbus_code idx %d returned %d - continue\n",
+				   j, ret);
+			continue;
+		}
+
+		unicam_dbg(2, dev, "subdev %s: code: %04x idx: %d\n",
+			   dev->sensor->name, mbus_code.code, j);
+
+		fmt = find_format_by_code(mbus_code.code);
+		unicam_dbg(2, dev, "fmt %04x returned as %p, V4L2 FOURCC %04x, csi_dt %02X\n",
+			   mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
+			   fmt ? fmt->csi_dt : 0);
+		if (fmt)
+			return fmt;
+	}
+
+	return NULL;
+}
 static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
 				  struct v4l2_format *f)
 {
@@ -759,13 +798,15 @@ static int unicam_try_fmt_vid_cap(struct
 	struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
 	int ret;
 
-	fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
+	fmt = find_format_by_pix(f->fmt.pix.pixelformat);
 	if (!fmt) {
-		unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use default of %08X\n",
-			   f->fmt.pix.pixelformat, dev->active_fmts[0].fourcc);
+		/* Pixel format not supported by unicam. Choose the first
+		 * supported format, and let the sensor choose something else.
+		 */
+		unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use first format.\n",
+			   f->fmt.pix.pixelformat);
 
-		/* Just get the first one enumerated */
-		fmt = &dev->active_fmts[0];
+		fmt = &formats[0];
 		f->fmt.pix.pixelformat = fmt->fourcc;
 	}
 
@@ -785,6 +826,40 @@ static int unicam_try_fmt_vid_cap(struct
 		unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
 
 	v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
+	if (mbus_fmt->code != fmt->code) {
+		/* Sensor has returned an alternate format */
+		fmt = find_format_by_code(mbus_fmt->code);
+		if (!fmt) {
+			/* The alternate format is one unicam can't support.
+			 * Find the first format that is supported by both, and
+			 * then set that.
+			 */
+			fmt = get_first_supported_format(dev);
+			mbus_fmt->code = fmt->code;
+
+			ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
+					       dev->sensor_config, &sd_fmt);
+			if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
+				return ret;
+
+			if (mbus_fmt->field != V4L2_FIELD_NONE)
+				unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
+
+			v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
+
+			if (mbus_fmt->code != fmt->code) {
+				/* We've set a format that the sensor reports
+				 * as being supported, but it refuses to set it.
+				 * Not much else we can do.
+				 * Assume that the sensor driver may accept the
+				 * format when it is set (rather than tried).
+				 */
+				unicam_err(dev, "Sensor won't accept default format, and Unicam can't support sensor default\n");
+			}
+		}
+
+		f->fmt.pix.pixelformat = fmt->fourcc;
+	}
 
 	return unicam_calc_format_size_bpl(dev, fmt, f);
 }
@@ -805,10 +880,18 @@ static int unicam_s_fmt_vid_cap(struct f
 	if (ret < 0)
 		return ret;
 
-	fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
+	fmt = find_format_by_pix(f->fmt.pix.pixelformat);
 	if (!fmt) {
-		/* Unknown pixel format - adopt a default */
-		fmt = &dev->active_fmts[0];
+		/* Unknown pixel format - adopt a default.
+		 * This shouldn't happen as try_fmt should have resolved any
+		 * issues first.
+		 */
+		fmt = get_first_supported_format(dev);
+		if (!fmt)
+			/* It shouldn't be possible to get here with no
+			 * supported formats
+			 */
+			return -EINVAL;
 		f->fmt.pix.pixelformat = fmt->fourcc;
 		return -EINVAL;
 	}
@@ -944,6 +1027,7 @@ static void unicam_set_packing_config(st
 			unpack = UNICAM_PUM_NONE;
 			break;
 		}
+
 		switch (v4l2_depth) {
 		case 8:
 			pack = UNICAM_PPM_PACK8;
@@ -1439,7 +1523,7 @@ static int unicam_enum_framesizes(struct
 	int ret;
 
 	/* check for valid format */
-	fmt = find_format_by_pix(dev, fsize->pixel_format);
+	fmt = find_format_by_pix(fsize->pixel_format);
 	if (!fmt) {
 		unicam_dbg(3, dev, "Invalid pixel code: %x\n",
 			   fsize->pixel_format);
@@ -1478,7 +1562,7 @@ static int unicam_enum_frameintervals(st
 	};
 	int ret;
 
-	fmt = find_format_by_pix(dev, fival->pixel_format);
+	fmt = find_format_by_pix(fival->pixel_format);
 	if (!fmt)
 		return -EINVAL;
 
@@ -1742,27 +1826,6 @@ static const struct v4l2_ioctl_ops unica
 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
 };
 
-/*
- * Adds an entry to the active_fmts array
- * Returns non-zero if attempting to write off the end of the array.
- */
-static int unicam_add_active_format(struct unicam_device *unicam,
-				    const struct unicam_fmt *fmt)
-{
-	//Ensure we don't run off the end of the array.
-	if (unicam->num_active_fmt >= MAX_POSSIBLE_PIX_FMTS)
-		return 1;
-
-	unicam->active_fmts[unicam->num_active_fmt] = *fmt;
-	unicam_dbg(2, unicam,
-		   "matched fourcc: " V4L2_FOURCC_CONV ": code: %04x idx: %d\n",
-		   V4L2_FOURCC_CONV_ARGS(fmt->fourcc),
-		   fmt->code, unicam->num_active_fmt);
-	unicam->num_active_fmt++;
-
-	return 0;
-}
-
 static int
 unicam_async_bound(struct v4l2_async_notifier *notifier,
 		   struct v4l2_subdev *subdev,
@@ -1770,9 +1833,6 @@ unicam_async_bound(struct v4l2_async_not
 {
 	struct unicam_device *unicam = container_of(notifier->v4l2_dev,
 					       struct unicam_device, v4l2_dev);
-	struct v4l2_subdev_mbus_code_enum mbus_code;
-	int ret = 0;
-	int j;
 
 	if (unicam->sensor) {
 		unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
@@ -1783,47 +1843,6 @@ unicam_async_bound(struct v4l2_async_not
 	unicam->sensor = subdev;
 	unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
 
-	/* Enumerate sub device formats and enable all matching local formats */
-	unicam->num_active_fmt = 0;
-	unicam_dbg(2, unicam, "Get supported formats...\n");
-	for (j = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++j) {
-		const struct unicam_fmt *fmt = NULL;
-		int k;
-
-		memset(&mbus_code, 0, sizeof(mbus_code));
-		mbus_code.index = j;
-		ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
-				       NULL, &mbus_code);
-		if (ret < 0) {
-			unicam_dbg(2, unicam,
-				   "subdev->enum_mbus_code idx %d returned %d - continue\n",
-				   j, ret);
-			continue;
-		}
-
-		unicam_dbg(2, unicam, "subdev %s: code: %04x idx: %d\n",
-			   subdev->name, mbus_code.code, j);
-
-		for (k = 0; k < ARRAY_SIZE(formats); k++) {
-			if (mbus_code.code == formats[k].code) {
-				fmt = &formats[k];
-				break;
-			}
-		}
-		unicam_dbg(2, unicam, "fmt %04x returned as %p, V4L2 FOURCC %04x, csi_dt %02X\n",
-			   mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
-			   fmt ? fmt->csi_dt : 0);
-		if (fmt) {
-			if (unicam_add_active_format(unicam, fmt)) {
-				unicam_dbg(1, unicam, "Active fmt list truncated\n");
-				break;
-			}
-		}
-	}
-	unicam_dbg(2, unicam,
-		   "Done all formats\n");
-	dump_active_formats(unicam);
-
 	return 0;
 }
 
@@ -1849,10 +1868,17 @@ static int unicam_probe_complete(struct
 		return ret;
 	}
 
-	fmt = find_format_by_code(unicam, mbus_fmt.code);
+	fmt = find_format_by_code(mbus_fmt.code);
 	if (!fmt) {
-		/* Default image format not valid. Choose first active fmt. */
-		fmt = &unicam->active_fmts[0];
+		/* Find the first format that the sensor and unicam both
+		 * support
+		 */
+		fmt = get_first_supported_format(unicam);
+
+		if (!fmt)
+			/* No compatible formats */
+			return -EINVAL;
+
 		mbus_fmt.code = fmt->code;
 		ret = __subdev_set_format(unicam, &mbus_fmt);
 		if (ret)