]>
Commit | Line | Data |
---|---|---|
fc96d58c SA |
1 | /* |
2 | * TI OMAP4 ISS V4L2 Driver - Generic video node | |
3 | * | |
4 | * Copyright (C) 2012 Texas Instruments, Inc. | |
5 | * | |
6 | * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | */ | |
13 | ||
14 | #include <asm/cacheflush.h> | |
15 | #include <linux/clk.h> | |
16 | #include <linux/mm.h> | |
17 | #include <linux/pagemap.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/vmalloc.h> | |
21 | #include <linux/module.h> | |
22 | #include <media/v4l2-dev.h> | |
23 | #include <media/v4l2-ioctl.h> | |
24 | ||
25 | #include "iss_video.h" | |
26 | #include "iss.h" | |
27 | ||
28 | ||
29 | /* ----------------------------------------------------------------------------- | |
30 | * Helper functions | |
31 | */ | |
32 | ||
33 | static struct iss_format_info formats[] = { | |
34 | { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8, | |
35 | V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8, | |
a1d4eab0 | 36 | V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", }, |
fc96d58c SA |
37 | { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10, |
38 | V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8, | |
a1d4eab0 | 39 | V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", }, |
fc96d58c SA |
40 | { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10, |
41 | V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8, | |
a1d4eab0 | 42 | V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", }, |
fc96d58c SA |
43 | { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8, |
44 | V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8, | |
a1d4eab0 | 45 | V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", }, |
fc96d58c SA |
46 | { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8, |
47 | V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8, | |
a1d4eab0 | 48 | V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", }, |
fc96d58c SA |
49 | { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8, |
50 | V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8, | |
a1d4eab0 | 51 | V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", }, |
fc96d58c SA |
52 | { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8, |
53 | V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8, | |
a1d4eab0 | 54 | V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", }, |
fc96d58c SA |
55 | { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, |
56 | V4L2_MBUS_FMT_SGRBG10_1X10, 0, | |
a1d4eab0 | 57 | V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8", }, |
fc96d58c SA |
58 | { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10, |
59 | V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8, | |
a1d4eab0 | 60 | V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", }, |
fc96d58c SA |
61 | { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10, |
62 | V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8, | |
a1d4eab0 | 63 | V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", }, |
fc96d58c SA |
64 | { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10, |
65 | V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8, | |
a1d4eab0 | 66 | V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", }, |
fc96d58c SA |
67 | { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10, |
68 | V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8, | |
a1d4eab0 | 69 | V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", }, |
fc96d58c SA |
70 | { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10, |
71 | V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8, | |
a1d4eab0 | 72 | V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", }, |
fc96d58c SA |
73 | { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10, |
74 | V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8, | |
a1d4eab0 | 75 | V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", }, |
fc96d58c SA |
76 | { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10, |
77 | V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8, | |
a1d4eab0 | 78 | V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", }, |
fc96d58c SA |
79 | { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10, |
80 | V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8, | |
a1d4eab0 | 81 | V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", }, |
fc96d58c SA |
82 | { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16, |
83 | V4L2_MBUS_FMT_UYVY8_1X16, 0, | |
a1d4eab0 | 84 | V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", }, |
fc96d58c SA |
85 | { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16, |
86 | V4L2_MBUS_FMT_YUYV8_1X16, 0, | |
a1d4eab0 | 87 | V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", }, |
fc96d58c SA |
88 | { V4L2_MBUS_FMT_YUYV8_1_5X8, V4L2_MBUS_FMT_YUYV8_1_5X8, |
89 | V4L2_MBUS_FMT_YUYV8_1_5X8, 0, | |
a1d4eab0 | 90 | V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", }, |
fc96d58c SA |
91 | }; |
92 | ||
93 | const struct iss_format_info * | |
94 | omap4iss_video_format_info(enum v4l2_mbus_pixelcode code) | |
95 | { | |
96 | unsigned int i; | |
97 | ||
98 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { | |
99 | if (formats[i].code == code) | |
100 | return &formats[i]; | |
101 | } | |
102 | ||
103 | return NULL; | |
104 | } | |
105 | ||
106 | /* | |
107 | * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format | |
108 | * @video: ISS video instance | |
109 | * @mbus: v4l2_mbus_framefmt format (input) | |
110 | * @pix: v4l2_pix_format format (output) | |
111 | * | |
112 | * Fill the output pix structure with information from the input mbus format. | |
113 | * The bytesperline and sizeimage fields are computed from the requested bytes | |
114 | * per line value in the pix format and information from the video instance. | |
115 | * | |
116 | * Return the number of padding bytes at end of line. | |
117 | */ | |
118 | static unsigned int iss_video_mbus_to_pix(const struct iss_video *video, | |
119 | const struct v4l2_mbus_framefmt *mbus, | |
120 | struct v4l2_pix_format *pix) | |
121 | { | |
122 | unsigned int bpl = pix->bytesperline; | |
123 | unsigned int min_bpl; | |
124 | unsigned int i; | |
125 | ||
126 | memset(pix, 0, sizeof(*pix)); | |
127 | pix->width = mbus->width; | |
128 | pix->height = mbus->height; | |
129 | ||
130 | /* Skip the last format in the loop so that it will be selected if no | |
131 | * match is found. | |
132 | */ | |
133 | for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) { | |
134 | if (formats[i].code == mbus->code) | |
135 | break; | |
136 | } | |
137 | ||
138 | min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8; | |
139 | ||
140 | /* Clamp the requested bytes per line value. If the maximum bytes per | |
141 | * line value is zero, the module doesn't support user configurable line | |
142 | * sizes. Override the requested value with the minimum in that case. | |
143 | */ | |
144 | if (video->bpl_max) | |
145 | bpl = clamp(bpl, min_bpl, video->bpl_max); | |
146 | else | |
147 | bpl = min_bpl; | |
148 | ||
149 | if (!video->bpl_zero_padding || bpl != min_bpl) | |
150 | bpl = ALIGN(bpl, video->bpl_alignment); | |
151 | ||
152 | pix->pixelformat = formats[i].pixelformat; | |
153 | pix->bytesperline = bpl; | |
154 | pix->sizeimage = pix->bytesperline * pix->height; | |
155 | pix->colorspace = mbus->colorspace; | |
156 | pix->field = mbus->field; | |
157 | ||
158 | /* FIXME: Special case for NV12! We should make this nicer... */ | |
159 | if (pix->pixelformat == V4L2_PIX_FMT_NV12) | |
160 | pix->sizeimage += (pix->bytesperline * pix->height) / 2; | |
161 | ||
162 | return bpl - min_bpl; | |
163 | } | |
164 | ||
165 | static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix, | |
166 | struct v4l2_mbus_framefmt *mbus) | |
167 | { | |
168 | unsigned int i; | |
169 | ||
170 | memset(mbus, 0, sizeof(*mbus)); | |
171 | mbus->width = pix->width; | |
172 | mbus->height = pix->height; | |
173 | ||
174 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { | |
175 | if (formats[i].pixelformat == pix->pixelformat) | |
176 | break; | |
177 | } | |
178 | ||
179 | if (WARN_ON(i == ARRAY_SIZE(formats))) | |
180 | return; | |
181 | ||
182 | mbus->code = formats[i].code; | |
183 | mbus->colorspace = pix->colorspace; | |
184 | mbus->field = pix->field; | |
185 | } | |
186 | ||
187 | static struct v4l2_subdev * | |
188 | iss_video_remote_subdev(struct iss_video *video, u32 *pad) | |
189 | { | |
190 | struct media_pad *remote; | |
191 | ||
192 | remote = media_entity_remote_pad(&video->pad); | |
193 | ||
194 | if (remote == NULL || | |
195 | media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
196 | return NULL; | |
197 | ||
198 | if (pad) | |
199 | *pad = remote->index; | |
200 | ||
201 | return media_entity_to_v4l2_subdev(remote->entity); | |
202 | } | |
203 | ||
204 | /* Return a pointer to the ISS video instance at the far end of the pipeline. */ | |
205 | static struct iss_video * | |
206 | iss_video_far_end(struct iss_video *video) | |
207 | { | |
208 | struct media_entity_graph graph; | |
209 | struct media_entity *entity = &video->video.entity; | |
210 | struct media_device *mdev = entity->parent; | |
211 | struct iss_video *far_end = NULL; | |
212 | ||
213 | mutex_lock(&mdev->graph_mutex); | |
214 | media_entity_graph_walk_start(&graph, entity); | |
215 | ||
216 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
217 | if (entity == &video->video.entity) | |
218 | continue; | |
219 | ||
220 | if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE) | |
221 | continue; | |
222 | ||
223 | far_end = to_iss_video(media_entity_to_video_device(entity)); | |
224 | if (far_end->type != video->type) | |
225 | break; | |
226 | ||
227 | far_end = NULL; | |
228 | } | |
229 | ||
230 | mutex_unlock(&mdev->graph_mutex); | |
231 | return far_end; | |
232 | } | |
233 | ||
234 | static int | |
cc3c2ac2 LP |
235 | __iss_video_get_format(struct iss_video *video, |
236 | struct v4l2_mbus_framefmt *format) | |
fc96d58c SA |
237 | { |
238 | struct v4l2_subdev_format fmt; | |
239 | struct v4l2_subdev *subdev; | |
240 | u32 pad; | |
241 | int ret; | |
242 | ||
243 | subdev = iss_video_remote_subdev(video, &pad); | |
244 | if (subdev == NULL) | |
245 | return -EINVAL; | |
246 | ||
cc3c2ac2 | 247 | memset(&fmt, 0, sizeof(fmt)); |
fc96d58c SA |
248 | fmt.pad = pad; |
249 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
fc96d58c | 250 | |
c3dbc70d LP |
251 | mutex_lock(&video->mutex); |
252 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); | |
fc96d58c SA |
253 | mutex_unlock(&video->mutex); |
254 | ||
255 | if (ret) | |
256 | return ret; | |
257 | ||
cc3c2ac2 LP |
258 | *format = fmt.format; |
259 | return 0; | |
fc96d58c SA |
260 | } |
261 | ||
262 | static int | |
263 | iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh) | |
264 | { | |
cc3c2ac2 LP |
265 | struct v4l2_mbus_framefmt format; |
266 | struct v4l2_pix_format pixfmt; | |
fc96d58c SA |
267 | int ret; |
268 | ||
fc96d58c SA |
269 | ret = __iss_video_get_format(video, &format); |
270 | if (ret < 0) | |
271 | return ret; | |
272 | ||
cc3c2ac2 LP |
273 | pixfmt.bytesperline = 0; |
274 | ret = iss_video_mbus_to_pix(video, &format, &pixfmt); | |
275 | ||
276 | if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat || | |
277 | vfh->format.fmt.pix.height != pixfmt.height || | |
278 | vfh->format.fmt.pix.width != pixfmt.width || | |
279 | vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline || | |
280 | vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage) | |
fc96d58c SA |
281 | return -EINVAL; |
282 | ||
283 | return ret; | |
284 | } | |
285 | ||
286 | /* ----------------------------------------------------------------------------- | |
287 | * Video queue operations | |
288 | */ | |
289 | ||
a0fe029c LP |
290 | static int iss_video_queue_setup(struct vb2_queue *vq, |
291 | const struct v4l2_format *fmt, | |
fc96d58c SA |
292 | unsigned int *count, unsigned int *num_planes, |
293 | unsigned int sizes[], void *alloc_ctxs[]) | |
294 | { | |
295 | struct iss_video_fh *vfh = vb2_get_drv_priv(vq); | |
296 | struct iss_video *video = vfh->video; | |
297 | ||
298 | /* Revisit multi-planar support for NV12 */ | |
299 | *num_planes = 1; | |
300 | ||
301 | sizes[0] = vfh->format.fmt.pix.sizeimage; | |
302 | if (sizes[0] == 0) | |
303 | return -EINVAL; | |
304 | ||
305 | alloc_ctxs[0] = video->alloc_ctx; | |
306 | ||
a0fe029c | 307 | *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0])); |
fc96d58c SA |
308 | |
309 | return 0; | |
310 | } | |
311 | ||
312 | static void iss_video_buf_cleanup(struct vb2_buffer *vb) | |
313 | { | |
314 | struct iss_buffer *buffer = container_of(vb, struct iss_buffer, vb); | |
315 | ||
316 | if (buffer->iss_addr) | |
317 | buffer->iss_addr = 0; | |
318 | } | |
319 | ||
320 | static int iss_video_buf_prepare(struct vb2_buffer *vb) | |
321 | { | |
322 | struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue); | |
323 | struct iss_buffer *buffer = container_of(vb, struct iss_buffer, vb); | |
324 | struct iss_video *video = vfh->video; | |
325 | unsigned long size = vfh->format.fmt.pix.sizeimage; | |
326 | dma_addr_t addr; | |
327 | ||
328 | if (vb2_plane_size(vb, 0) < size) | |
329 | return -ENOBUFS; | |
330 | ||
112da085 LP |
331 | /* Refuse to prepare the buffer is the video node has registered an |
332 | * error. We don't need to take any lock here as the operation is | |
333 | * inherently racy. The authoritative check will be performed in the | |
334 | * queue handler, which can't return an error, this check is just a best | |
335 | * effort to notify userspace as early as possible. | |
336 | */ | |
337 | if (unlikely(video->error)) | |
338 | return -EIO; | |
339 | ||
fc96d58c SA |
340 | addr = vb2_dma_contig_plane_dma_addr(vb, 0); |
341 | if (!IS_ALIGNED(addr, 32)) { | |
499226fb LP |
342 | dev_dbg(video->iss->dev, |
343 | "Buffer address must be aligned to 32 bytes boundary.\n"); | |
fc96d58c SA |
344 | return -EINVAL; |
345 | } | |
346 | ||
347 | vb2_set_plane_payload(vb, 0, size); | |
348 | buffer->iss_addr = addr; | |
349 | return 0; | |
350 | } | |
351 | ||
352 | static void iss_video_buf_queue(struct vb2_buffer *vb) | |
353 | { | |
354 | struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue); | |
355 | struct iss_video *video = vfh->video; | |
356 | struct iss_buffer *buffer = container_of(vb, struct iss_buffer, vb); | |
357 | struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity); | |
fc96d58c | 358 | unsigned long flags; |
112da085 | 359 | bool empty; |
fc96d58c SA |
360 | |
361 | spin_lock_irqsave(&video->qlock, flags); | |
112da085 LP |
362 | |
363 | if (unlikely(video->error)) { | |
364 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
365 | spin_unlock_irqrestore(&video->qlock, flags); | |
366 | return; | |
367 | } | |
368 | ||
fc96d58c SA |
369 | empty = list_empty(&video->dmaqueue); |
370 | list_add_tail(&buffer->list, &video->dmaqueue); | |
112da085 | 371 | |
fc96d58c SA |
372 | spin_unlock_irqrestore(&video->qlock, flags); |
373 | ||
374 | if (empty) { | |
375 | enum iss_pipeline_state state; | |
376 | unsigned int start; | |
377 | ||
378 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
379 | state = ISS_PIPELINE_QUEUE_OUTPUT; | |
380 | else | |
381 | state = ISS_PIPELINE_QUEUE_INPUT; | |
382 | ||
383 | spin_lock_irqsave(&pipe->lock, flags); | |
384 | pipe->state |= state; | |
385 | video->ops->queue(video, buffer); | |
386 | video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED; | |
387 | ||
388 | start = iss_pipeline_ready(pipe); | |
389 | if (start) | |
390 | pipe->state |= ISS_PIPELINE_STREAM; | |
391 | spin_unlock_irqrestore(&pipe->lock, flags); | |
392 | ||
393 | if (start) | |
394 | omap4iss_pipeline_set_stream(pipe, | |
395 | ISS_PIPELINE_STREAM_SINGLESHOT); | |
396 | } | |
397 | } | |
398 | ||
399 | static struct vb2_ops iss_video_vb2ops = { | |
400 | .queue_setup = iss_video_queue_setup, | |
401 | .buf_prepare = iss_video_buf_prepare, | |
402 | .buf_queue = iss_video_buf_queue, | |
403 | .buf_cleanup = iss_video_buf_cleanup, | |
404 | }; | |
405 | ||
406 | /* | |
407 | * omap4iss_video_buffer_next - Complete the current buffer and return the next | |
408 | * @video: ISS video object | |
409 | * | |
410 | * Remove the current video buffer from the DMA queue and fill its timestamp, | |
411 | * field count and state fields before waking up its completion handler. | |
412 | * | |
413 | * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no | |
414 | * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise. | |
415 | * | |
416 | * The DMA queue is expected to contain at least one buffer. | |
417 | * | |
418 | * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is | |
419 | * empty. | |
420 | */ | |
421 | struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video) | |
422 | { | |
423 | struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity); | |
424 | enum iss_pipeline_state state; | |
425 | struct iss_buffer *buf; | |
426 | unsigned long flags; | |
427 | struct timespec ts; | |
428 | ||
429 | spin_lock_irqsave(&video->qlock, flags); | |
430 | if (WARN_ON(list_empty(&video->dmaqueue))) { | |
431 | spin_unlock_irqrestore(&video->qlock, flags); | |
432 | return NULL; | |
433 | } | |
434 | ||
435 | buf = list_first_entry(&video->dmaqueue, struct iss_buffer, | |
436 | list); | |
437 | list_del(&buf->list); | |
438 | spin_unlock_irqrestore(&video->qlock, flags); | |
439 | ||
440 | ktime_get_ts(&ts); | |
441 | buf->vb.v4l2_buf.timestamp.tv_sec = ts.tv_sec; | |
442 | buf->vb.v4l2_buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC; | |
443 | ||
444 | /* Do frame number propagation only if this is the output video node. | |
445 | * Frame number either comes from the CSI receivers or it gets | |
446 | * incremented here if H3A is not active. | |
447 | * Note: There is no guarantee that the output buffer will finish | |
448 | * first, so the input number might lag behind by 1 in some cases. | |
449 | */ | |
450 | if (video == pipe->output && !pipe->do_propagation) | |
a0fe029c LP |
451 | buf->vb.v4l2_buf.sequence = |
452 | atomic_inc_return(&pipe->frame_number); | |
fc96d58c SA |
453 | else |
454 | buf->vb.v4l2_buf.sequence = atomic_read(&pipe->frame_number); | |
455 | ||
a0fe029c LP |
456 | vb2_buffer_done(&buf->vb, pipe->error ? |
457 | VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); | |
fc96d58c SA |
458 | pipe->error = false; |
459 | ||
460 | spin_lock_irqsave(&video->qlock, flags); | |
461 | if (list_empty(&video->dmaqueue)) { | |
462 | spin_unlock_irqrestore(&video->qlock, flags); | |
463 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
464 | state = ISS_PIPELINE_QUEUE_OUTPUT | |
465 | | ISS_PIPELINE_STREAM; | |
466 | else | |
467 | state = ISS_PIPELINE_QUEUE_INPUT | |
468 | | ISS_PIPELINE_STREAM; | |
469 | ||
470 | spin_lock_irqsave(&pipe->lock, flags); | |
471 | pipe->state &= ~state; | |
472 | if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS) | |
473 | video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN; | |
474 | spin_unlock_irqrestore(&pipe->lock, flags); | |
475 | return NULL; | |
476 | } | |
477 | ||
478 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) { | |
f7d40eea | 479 | spin_lock(&pipe->lock); |
fc96d58c | 480 | pipe->state &= ~ISS_PIPELINE_STREAM; |
f7d40eea | 481 | spin_unlock(&pipe->lock); |
fc96d58c SA |
482 | } |
483 | ||
484 | buf = list_first_entry(&video->dmaqueue, struct iss_buffer, | |
485 | list); | |
486 | spin_unlock_irqrestore(&video->qlock, flags); | |
487 | buf->vb.state = VB2_BUF_STATE_ACTIVE; | |
488 | return buf; | |
489 | } | |
490 | ||
112da085 LP |
491 | /* |
492 | * omap4iss_video_cancel_stream - Cancel stream on a video node | |
493 | * @video: ISS video object | |
494 | * | |
495 | * Cancelling a stream mark all buffers on the video node as erroneous and makes | |
496 | * sure no new buffer can be queued. | |
497 | */ | |
498 | void omap4iss_video_cancel_stream(struct iss_video *video) | |
499 | { | |
500 | unsigned long flags; | |
501 | ||
502 | spin_lock_irqsave(&video->qlock, flags); | |
503 | ||
504 | while (!list_empty(&video->dmaqueue)) { | |
505 | struct iss_buffer *buf; | |
506 | ||
507 | buf = list_first_entry(&video->dmaqueue, struct iss_buffer, | |
508 | list); | |
509 | list_del(&buf->list); | |
510 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | |
511 | } | |
512 | ||
513 | video->error = true; | |
514 | ||
515 | spin_unlock_irqrestore(&video->qlock, flags); | |
516 | } | |
517 | ||
fc96d58c SA |
518 | /* ----------------------------------------------------------------------------- |
519 | * V4L2 ioctls | |
520 | */ | |
521 | ||
522 | static int | |
523 | iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap) | |
524 | { | |
525 | struct iss_video *video = video_drvdata(file); | |
526 | ||
527 | strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver)); | |
528 | strlcpy(cap->card, video->video.name, sizeof(cap->card)); | |
529 | strlcpy(cap->bus_info, "media", sizeof(cap->bus_info)); | |
530 | ||
531 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
35c71be8 | 532 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
fc96d58c | 533 | else |
35c71be8 LP |
534 | cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; |
535 | ||
536 | cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING | |
537 | | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT; | |
fc96d58c SA |
538 | |
539 | return 0; | |
540 | } | |
541 | ||
0b1d4249 LP |
542 | static int |
543 | iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f) | |
544 | { | |
545 | struct iss_video *video = video_drvdata(file); | |
546 | struct v4l2_mbus_framefmt format; | |
547 | unsigned int index = f->index; | |
548 | unsigned int i; | |
549 | int ret; | |
550 | ||
551 | if (f->type != video->type) | |
552 | return -EINVAL; | |
553 | ||
554 | ret = __iss_video_get_format(video, &format); | |
555 | if (ret < 0) | |
556 | return ret; | |
557 | ||
558 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { | |
559 | const struct iss_format_info *info = &formats[i]; | |
560 | ||
561 | if (format.code != info->code) | |
562 | continue; | |
563 | ||
564 | if (index == 0) { | |
565 | f->pixelformat = info->pixelformat; | |
566 | strlcpy(f->description, info->description, | |
567 | sizeof(f->description)); | |
568 | return 0; | |
569 | } | |
570 | ||
571 | index--; | |
572 | } | |
573 | ||
574 | return -EINVAL; | |
575 | } | |
576 | ||
fc96d58c SA |
577 | static int |
578 | iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format) | |
579 | { | |
580 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
581 | struct iss_video *video = video_drvdata(file); | |
582 | ||
583 | if (format->type != video->type) | |
584 | return -EINVAL; | |
585 | ||
586 | mutex_lock(&video->mutex); | |
587 | *format = vfh->format; | |
588 | mutex_unlock(&video->mutex); | |
589 | ||
590 | return 0; | |
591 | } | |
592 | ||
593 | static int | |
594 | iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format) | |
595 | { | |
596 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
597 | struct iss_video *video = video_drvdata(file); | |
598 | struct v4l2_mbus_framefmt fmt; | |
599 | ||
600 | if (format->type != video->type) | |
601 | return -EINVAL; | |
602 | ||
603 | mutex_lock(&video->mutex); | |
604 | ||
605 | /* Fill the bytesperline and sizeimage fields by converting to media bus | |
606 | * format and back to pixel format. | |
607 | */ | |
608 | iss_video_pix_to_mbus(&format->fmt.pix, &fmt); | |
609 | iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix); | |
610 | ||
611 | vfh->format = *format; | |
612 | ||
613 | mutex_unlock(&video->mutex); | |
614 | return 0; | |
615 | } | |
616 | ||
617 | static int | |
618 | iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format) | |
619 | { | |
620 | struct iss_video *video = video_drvdata(file); | |
621 | struct v4l2_subdev_format fmt; | |
622 | struct v4l2_subdev *subdev; | |
623 | u32 pad; | |
624 | int ret; | |
625 | ||
626 | if (format->type != video->type) | |
627 | return -EINVAL; | |
628 | ||
629 | subdev = iss_video_remote_subdev(video, &pad); | |
630 | if (subdev == NULL) | |
631 | return -EINVAL; | |
632 | ||
633 | iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format); | |
634 | ||
635 | fmt.pad = pad; | |
636 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
637 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); | |
638 | if (ret) | |
e43484e4 | 639 | return ret; |
fc96d58c SA |
640 | |
641 | iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix); | |
642 | return 0; | |
643 | } | |
644 | ||
645 | static int | |
646 | iss_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap) | |
647 | { | |
648 | struct iss_video *video = video_drvdata(file); | |
649 | struct v4l2_subdev *subdev; | |
650 | int ret; | |
651 | ||
652 | subdev = iss_video_remote_subdev(video, NULL); | |
653 | if (subdev == NULL) | |
654 | return -EINVAL; | |
655 | ||
656 | mutex_lock(&video->mutex); | |
657 | ret = v4l2_subdev_call(subdev, video, cropcap, cropcap); | |
658 | mutex_unlock(&video->mutex); | |
659 | ||
4a62361f | 660 | return ret == -ENOIOCTLCMD ? -ENOTTY : ret; |
fc96d58c SA |
661 | } |
662 | ||
663 | static int | |
664 | iss_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop) | |
665 | { | |
666 | struct iss_video *video = video_drvdata(file); | |
667 | struct v4l2_subdev_format format; | |
668 | struct v4l2_subdev *subdev; | |
669 | u32 pad; | |
670 | int ret; | |
671 | ||
672 | subdev = iss_video_remote_subdev(video, &pad); | |
673 | if (subdev == NULL) | |
674 | return -EINVAL; | |
675 | ||
676 | /* Try the get crop operation first and fallback to get format if not | |
677 | * implemented. | |
678 | */ | |
679 | ret = v4l2_subdev_call(subdev, video, g_crop, crop); | |
680 | if (ret != -ENOIOCTLCMD) | |
681 | return ret; | |
682 | ||
683 | format.pad = pad; | |
684 | format.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
685 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format); | |
686 | if (ret < 0) | |
4a62361f | 687 | return ret == -ENOIOCTLCMD ? -ENOTTY : ret; |
fc96d58c SA |
688 | |
689 | crop->c.left = 0; | |
690 | crop->c.top = 0; | |
691 | crop->c.width = format.format.width; | |
692 | crop->c.height = format.format.height; | |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
697 | static int | |
698 | iss_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop) | |
699 | { | |
700 | struct iss_video *video = video_drvdata(file); | |
701 | struct v4l2_subdev *subdev; | |
702 | int ret; | |
703 | ||
704 | subdev = iss_video_remote_subdev(video, NULL); | |
705 | if (subdev == NULL) | |
706 | return -EINVAL; | |
707 | ||
708 | mutex_lock(&video->mutex); | |
709 | ret = v4l2_subdev_call(subdev, video, s_crop, crop); | |
710 | mutex_unlock(&video->mutex); | |
711 | ||
4a62361f | 712 | return ret == -ENOIOCTLCMD ? -ENOTTY : ret; |
fc96d58c SA |
713 | } |
714 | ||
715 | static int | |
716 | iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a) | |
717 | { | |
718 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
719 | struct iss_video *video = video_drvdata(file); | |
720 | ||
721 | if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || | |
722 | video->type != a->type) | |
723 | return -EINVAL; | |
724 | ||
725 | memset(a, 0, sizeof(*a)); | |
726 | a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
727 | a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; | |
728 | a->parm.output.timeperframe = vfh->timeperframe; | |
729 | ||
730 | return 0; | |
731 | } | |
732 | ||
733 | static int | |
734 | iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a) | |
735 | { | |
736 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
737 | struct iss_video *video = video_drvdata(file); | |
738 | ||
739 | if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || | |
740 | video->type != a->type) | |
741 | return -EINVAL; | |
742 | ||
743 | if (a->parm.output.timeperframe.denominator == 0) | |
744 | a->parm.output.timeperframe.denominator = 1; | |
745 | ||
746 | vfh->timeperframe = a->parm.output.timeperframe; | |
747 | ||
748 | return 0; | |
749 | } | |
750 | ||
751 | static int | |
752 | iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb) | |
753 | { | |
754 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
755 | ||
756 | return vb2_reqbufs(&vfh->queue, rb); | |
757 | } | |
758 | ||
759 | static int | |
760 | iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b) | |
761 | { | |
762 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
763 | ||
764 | return vb2_querybuf(&vfh->queue, b); | |
765 | } | |
766 | ||
767 | static int | |
768 | iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b) | |
769 | { | |
770 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
771 | ||
772 | return vb2_qbuf(&vfh->queue, b); | |
773 | } | |
774 | ||
775 | static int | |
776 | iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b) | |
777 | { | |
778 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
779 | ||
780 | return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK); | |
781 | } | |
782 | ||
783 | /* | |
784 | * Stream management | |
785 | * | |
786 | * Every ISS pipeline has a single input and a single output. The input can be | |
787 | * either a sensor or a video node. The output is always a video node. | |
788 | * | |
789 | * As every pipeline has an output video node, the ISS video objects at the | |
790 | * pipeline output stores the pipeline state. It tracks the streaming state of | |
791 | * both the input and output, as well as the availability of buffers. | |
792 | * | |
793 | * In sensor-to-memory mode, frames are always available at the pipeline input. | |
794 | * Starting the sensor usually requires I2C transfers and must be done in | |
795 | * interruptible context. The pipeline is started and stopped synchronously | |
796 | * to the stream on/off commands. All modules in the pipeline will get their | |
797 | * subdev set stream handler called. The module at the end of the pipeline must | |
798 | * delay starting the hardware until buffers are available at its output. | |
799 | * | |
800 | * In memory-to-memory mode, starting/stopping the stream requires | |
801 | * synchronization between the input and output. ISS modules can't be stopped | |
802 | * in the middle of a frame, and at least some of the modules seem to become | |
803 | * busy as soon as they're started, even if they don't receive a frame start | |
804 | * event. For that reason frames need to be processed in single-shot mode. The | |
805 | * driver needs to wait until a frame is completely processed and written to | |
806 | * memory before restarting the pipeline for the next frame. Pipelined | |
807 | * processing might be possible but requires more testing. | |
808 | * | |
809 | * Stream start must be delayed until buffers are available at both the input | |
810 | * and output. The pipeline must be started in the videobuf queue callback with | |
811 | * the buffers queue spinlock held. The modules subdev set stream operation must | |
812 | * not sleep. | |
813 | */ | |
814 | static int | |
815 | iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type) | |
816 | { | |
817 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
818 | struct iss_video *video = video_drvdata(file); | |
f3632ba8 LP |
819 | struct media_entity_graph graph; |
820 | struct media_entity *entity; | |
fc96d58c SA |
821 | enum iss_pipeline_state state; |
822 | struct iss_pipeline *pipe; | |
823 | struct iss_video *far_end; | |
824 | unsigned long flags; | |
825 | int ret; | |
826 | ||
827 | if (type != video->type) | |
828 | return -EINVAL; | |
829 | ||
830 | mutex_lock(&video->stream_lock); | |
831 | ||
fc96d58c SA |
832 | /* Start streaming on the pipeline. No link touching an entity in the |
833 | * pipeline can be activated or deactivated once streaming is started. | |
834 | */ | |
835 | pipe = video->video.entity.pipe | |
836 | ? to_iss_pipeline(&video->video.entity) : &video->pipe; | |
837 | pipe->external = NULL; | |
838 | pipe->external_rate = 0; | |
839 | pipe->external_bpp = 0; | |
f3632ba8 | 840 | pipe->entities = 0; |
fc96d58c SA |
841 | |
842 | if (video->iss->pdata->set_constraints) | |
843 | video->iss->pdata->set_constraints(video->iss, true); | |
844 | ||
845 | ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe); | |
846 | if (ret < 0) | |
847 | goto err_media_entity_pipeline_start; | |
848 | ||
f3632ba8 LP |
849 | entity = &video->video.entity; |
850 | media_entity_graph_walk_start(&graph, entity); | |
851 | while ((entity = media_entity_graph_walk_next(&graph))) | |
852 | pipe->entities |= 1 << entity->id; | |
853 | ||
fc96d58c SA |
854 | /* Verify that the currently configured format matches the output of |
855 | * the connected subdev. | |
856 | */ | |
857 | ret = iss_video_check_format(video, vfh); | |
858 | if (ret < 0) | |
859 | goto err_iss_video_check_format; | |
860 | ||
861 | video->bpl_padding = ret; | |
862 | video->bpl_value = vfh->format.fmt.pix.bytesperline; | |
863 | ||
864 | /* Find the ISS video node connected at the far end of the pipeline and | |
865 | * update the pipeline. | |
866 | */ | |
867 | far_end = iss_video_far_end(video); | |
868 | ||
869 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { | |
870 | state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT; | |
871 | pipe->input = far_end; | |
872 | pipe->output = video; | |
873 | } else { | |
874 | if (far_end == NULL) { | |
875 | ret = -EPIPE; | |
876 | goto err_iss_video_check_format; | |
877 | } | |
878 | ||
879 | state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT; | |
880 | pipe->input = video; | |
881 | pipe->output = far_end; | |
882 | } | |
883 | ||
884 | spin_lock_irqsave(&pipe->lock, flags); | |
885 | pipe->state &= ~ISS_PIPELINE_STREAM; | |
886 | pipe->state |= state; | |
887 | spin_unlock_irqrestore(&pipe->lock, flags); | |
888 | ||
889 | /* Set the maximum time per frame as the value requested by userspace. | |
890 | * This is a soft limit that can be overridden if the hardware doesn't | |
891 | * support the request limit. | |
892 | */ | |
893 | if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) | |
894 | pipe->max_timeperframe = vfh->timeperframe; | |
895 | ||
896 | video->queue = &vfh->queue; | |
897 | INIT_LIST_HEAD(&video->dmaqueue); | |
898 | spin_lock_init(&video->qlock); | |
112da085 | 899 | video->error = false; |
fc96d58c SA |
900 | atomic_set(&pipe->frame_number, -1); |
901 | ||
902 | ret = vb2_streamon(&vfh->queue, type); | |
903 | if (ret < 0) | |
904 | goto err_iss_video_check_format; | |
905 | ||
906 | /* In sensor-to-memory mode, the stream can be started synchronously | |
907 | * to the stream on command. In memory-to-memory mode, it will be | |
908 | * started when buffers are queued on both the input and output. | |
909 | */ | |
910 | if (pipe->input == NULL) { | |
911 | unsigned long flags; | |
912 | ret = omap4iss_pipeline_set_stream(pipe, | |
913 | ISS_PIPELINE_STREAM_CONTINUOUS); | |
914 | if (ret < 0) | |
915 | goto err_omap4iss_set_stream; | |
916 | spin_lock_irqsave(&video->qlock, flags); | |
917 | if (list_empty(&video->dmaqueue)) | |
918 | video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN; | |
919 | spin_unlock_irqrestore(&video->qlock, flags); | |
920 | } | |
921 | ||
2b16b44a LP |
922 | mutex_unlock(&video->stream_lock); |
923 | return 0; | |
924 | ||
fc96d58c | 925 | err_omap4iss_set_stream: |
2b16b44a | 926 | vb2_streamoff(&vfh->queue, type); |
fc96d58c | 927 | err_iss_video_check_format: |
2b16b44a | 928 | media_entity_pipeline_stop(&video->video.entity); |
fc96d58c | 929 | err_media_entity_pipeline_start: |
2b16b44a LP |
930 | if (video->iss->pdata->set_constraints) |
931 | video->iss->pdata->set_constraints(video->iss, false); | |
932 | video->queue = NULL; | |
fc96d58c | 933 | |
fc96d58c SA |
934 | mutex_unlock(&video->stream_lock); |
935 | return ret; | |
936 | } | |
937 | ||
938 | static int | |
939 | iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) | |
940 | { | |
941 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
942 | struct iss_video *video = video_drvdata(file); | |
943 | struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity); | |
944 | enum iss_pipeline_state state; | |
945 | unsigned long flags; | |
946 | ||
947 | if (type != video->type) | |
948 | return -EINVAL; | |
949 | ||
950 | mutex_lock(&video->stream_lock); | |
951 | ||
952 | if (!vb2_is_streaming(&vfh->queue)) | |
953 | goto done; | |
954 | ||
955 | /* Update the pipeline state. */ | |
956 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
957 | state = ISS_PIPELINE_STREAM_OUTPUT | |
958 | | ISS_PIPELINE_QUEUE_OUTPUT; | |
959 | else | |
960 | state = ISS_PIPELINE_STREAM_INPUT | |
961 | | ISS_PIPELINE_QUEUE_INPUT; | |
962 | ||
963 | spin_lock_irqsave(&pipe->lock, flags); | |
964 | pipe->state &= ~state; | |
965 | spin_unlock_irqrestore(&pipe->lock, flags); | |
966 | ||
967 | /* Stop the stream. */ | |
968 | omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED); | |
969 | vb2_streamoff(&vfh->queue, type); | |
970 | video->queue = NULL; | |
fc96d58c SA |
971 | |
972 | if (video->iss->pdata->set_constraints) | |
973 | video->iss->pdata->set_constraints(video->iss, false); | |
974 | media_entity_pipeline_stop(&video->video.entity); | |
975 | ||
976 | done: | |
977 | mutex_unlock(&video->stream_lock); | |
978 | return 0; | |
979 | } | |
980 | ||
981 | static int | |
982 | iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input) | |
983 | { | |
984 | if (input->index > 0) | |
985 | return -EINVAL; | |
986 | ||
987 | strlcpy(input->name, "camera", sizeof(input->name)); | |
988 | input->type = V4L2_INPUT_TYPE_CAMERA; | |
989 | ||
990 | return 0; | |
991 | } | |
992 | ||
993 | static int | |
994 | iss_video_g_input(struct file *file, void *fh, unsigned int *input) | |
995 | { | |
996 | *input = 0; | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
16422f55 LP |
1001 | static int |
1002 | iss_video_s_input(struct file *file, void *fh, unsigned int input) | |
1003 | { | |
1004 | return input == 0 ? 0 : -EINVAL; | |
1005 | } | |
1006 | ||
fc96d58c SA |
1007 | static const struct v4l2_ioctl_ops iss_video_ioctl_ops = { |
1008 | .vidioc_querycap = iss_video_querycap, | |
0b1d4249 | 1009 | .vidioc_enum_fmt_vid_cap = iss_video_enum_format, |
fc96d58c SA |
1010 | .vidioc_g_fmt_vid_cap = iss_video_get_format, |
1011 | .vidioc_s_fmt_vid_cap = iss_video_set_format, | |
1012 | .vidioc_try_fmt_vid_cap = iss_video_try_format, | |
1013 | .vidioc_g_fmt_vid_out = iss_video_get_format, | |
1014 | .vidioc_s_fmt_vid_out = iss_video_set_format, | |
1015 | .vidioc_try_fmt_vid_out = iss_video_try_format, | |
1016 | .vidioc_cropcap = iss_video_cropcap, | |
1017 | .vidioc_g_crop = iss_video_get_crop, | |
1018 | .vidioc_s_crop = iss_video_set_crop, | |
1019 | .vidioc_g_parm = iss_video_get_param, | |
1020 | .vidioc_s_parm = iss_video_set_param, | |
1021 | .vidioc_reqbufs = iss_video_reqbufs, | |
1022 | .vidioc_querybuf = iss_video_querybuf, | |
1023 | .vidioc_qbuf = iss_video_qbuf, | |
1024 | .vidioc_dqbuf = iss_video_dqbuf, | |
1025 | .vidioc_streamon = iss_video_streamon, | |
1026 | .vidioc_streamoff = iss_video_streamoff, | |
1027 | .vidioc_enum_input = iss_video_enum_input, | |
1028 | .vidioc_g_input = iss_video_g_input, | |
16422f55 | 1029 | .vidioc_s_input = iss_video_s_input, |
fc96d58c SA |
1030 | }; |
1031 | ||
1032 | /* ----------------------------------------------------------------------------- | |
1033 | * V4L2 file operations | |
1034 | */ | |
1035 | ||
1036 | static int iss_video_open(struct file *file) | |
1037 | { | |
1038 | struct iss_video *video = video_drvdata(file); | |
1039 | struct iss_video_fh *handle; | |
1040 | struct vb2_queue *q; | |
1041 | int ret = 0; | |
1042 | ||
1043 | handle = kzalloc(sizeof(*handle), GFP_KERNEL); | |
1044 | if (handle == NULL) | |
1045 | return -ENOMEM; | |
1046 | ||
1047 | v4l2_fh_init(&handle->vfh, &video->video); | |
1048 | v4l2_fh_add(&handle->vfh); | |
1049 | ||
1050 | /* If this is the first user, initialise the pipeline. */ | |
1051 | if (omap4iss_get(video->iss) == NULL) { | |
1052 | ret = -EBUSY; | |
1053 | goto done; | |
1054 | } | |
1055 | ||
1056 | ret = omap4iss_pipeline_pm_use(&video->video.entity, 1); | |
1057 | if (ret < 0) { | |
1058 | omap4iss_put(video->iss); | |
1059 | goto done; | |
1060 | } | |
1061 | ||
1062 | video->alloc_ctx = vb2_dma_contig_init_ctx(video->iss->dev); | |
1063 | if (IS_ERR(video->alloc_ctx)) { | |
1064 | ret = PTR_ERR(video->alloc_ctx); | |
1065 | omap4iss_put(video->iss); | |
1066 | goto done; | |
1067 | } | |
1068 | ||
1069 | q = &handle->queue; | |
1070 | ||
1071 | q->type = video->type; | |
1072 | q->io_modes = VB2_MMAP; | |
1073 | q->drv_priv = handle; | |
1074 | q->ops = &iss_video_vb2ops; | |
1075 | q->mem_ops = &vb2_dma_contig_memops; | |
1076 | q->buf_struct_size = sizeof(struct iss_buffer); | |
ade48681 | 1077 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
fc96d58c SA |
1078 | |
1079 | ret = vb2_queue_init(q); | |
1080 | if (ret) { | |
1081 | omap4iss_put(video->iss); | |
1082 | goto done; | |
1083 | } | |
1084 | ||
1085 | memset(&handle->format, 0, sizeof(handle->format)); | |
1086 | handle->format.type = video->type; | |
1087 | handle->timeperframe.denominator = 1; | |
1088 | ||
1089 | handle->video = video; | |
1090 | file->private_data = &handle->vfh; | |
1091 | ||
1092 | done: | |
1093 | if (ret < 0) { | |
1094 | v4l2_fh_del(&handle->vfh); | |
1095 | kfree(handle); | |
1096 | } | |
1097 | ||
1098 | return ret; | |
1099 | } | |
1100 | ||
1101 | static int iss_video_release(struct file *file) | |
1102 | { | |
1103 | struct iss_video *video = video_drvdata(file); | |
1104 | struct v4l2_fh *vfh = file->private_data; | |
1105 | struct iss_video_fh *handle = to_iss_video_fh(vfh); | |
1106 | ||
1107 | /* Disable streaming and free the buffers queue resources. */ | |
1108 | iss_video_streamoff(file, vfh, video->type); | |
1109 | ||
1110 | omap4iss_pipeline_pm_use(&video->video.entity, 0); | |
1111 | ||
1112 | /* Release the videobuf2 queue */ | |
1113 | vb2_queue_release(&handle->queue); | |
1114 | ||
1115 | /* Release the file handle. */ | |
1116 | v4l2_fh_del(vfh); | |
1117 | kfree(handle); | |
1118 | file->private_data = NULL; | |
1119 | ||
1120 | omap4iss_put(video->iss); | |
1121 | ||
1122 | return 0; | |
1123 | } | |
1124 | ||
1125 | static unsigned int iss_video_poll(struct file *file, poll_table *wait) | |
1126 | { | |
1127 | struct iss_video_fh *vfh = to_iss_video_fh(file->private_data); | |
1128 | ||
1129 | return vb2_poll(&vfh->queue, file, wait); | |
1130 | } | |
1131 | ||
1132 | static int iss_video_mmap(struct file *file, struct vm_area_struct *vma) | |
1133 | { | |
1134 | struct iss_video_fh *vfh = to_iss_video_fh(file->private_data); | |
1135 | ||
29ee6261 | 1136 | return vb2_mmap(&vfh->queue, vma); |
fc96d58c SA |
1137 | } |
1138 | ||
1139 | static struct v4l2_file_operations iss_video_fops = { | |
1140 | .owner = THIS_MODULE, | |
1141 | .unlocked_ioctl = video_ioctl2, | |
1142 | .open = iss_video_open, | |
1143 | .release = iss_video_release, | |
1144 | .poll = iss_video_poll, | |
1145 | .mmap = iss_video_mmap, | |
1146 | }; | |
1147 | ||
1148 | /* ----------------------------------------------------------------------------- | |
1149 | * ISS video core | |
1150 | */ | |
1151 | ||
1152 | static const struct iss_video_operations iss_video_dummy_ops = { | |
1153 | }; | |
1154 | ||
1155 | int omap4iss_video_init(struct iss_video *video, const char *name) | |
1156 | { | |
1157 | const char *direction; | |
1158 | int ret; | |
1159 | ||
1160 | switch (video->type) { | |
1161 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | |
1162 | direction = "output"; | |
1163 | video->pad.flags = MEDIA_PAD_FL_SINK; | |
1164 | break; | |
1165 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: | |
1166 | direction = "input"; | |
1167 | video->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1168 | break; | |
1169 | ||
1170 | default: | |
1171 | return -EINVAL; | |
1172 | } | |
1173 | ||
1174 | ret = media_entity_init(&video->video.entity, 1, &video->pad, 0); | |
1175 | if (ret < 0) | |
1176 | return ret; | |
1177 | ||
1178 | mutex_init(&video->mutex); | |
1179 | atomic_set(&video->active, 0); | |
1180 | ||
1181 | spin_lock_init(&video->pipe.lock); | |
1182 | mutex_init(&video->stream_lock); | |
1183 | ||
1184 | /* Initialize the video device. */ | |
1185 | if (video->ops == NULL) | |
1186 | video->ops = &iss_video_dummy_ops; | |
1187 | ||
1188 | video->video.fops = &iss_video_fops; | |
1189 | snprintf(video->video.name, sizeof(video->video.name), | |
1190 | "OMAP4 ISS %s %s", name, direction); | |
1191 | video->video.vfl_type = VFL_TYPE_GRABBER; | |
1192 | video->video.release = video_device_release_empty; | |
1193 | video->video.ioctl_ops = &iss_video_ioctl_ops; | |
1194 | video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED; | |
1195 | ||
1196 | video_set_drvdata(&video->video, video); | |
1197 | ||
1198 | return 0; | |
1199 | } | |
1200 | ||
1201 | void omap4iss_video_cleanup(struct iss_video *video) | |
1202 | { | |
1203 | media_entity_cleanup(&video->video.entity); | |
1204 | mutex_destroy(&video->stream_lock); | |
1205 | mutex_destroy(&video->mutex); | |
1206 | } | |
1207 | ||
1208 | int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev) | |
1209 | { | |
1210 | int ret; | |
1211 | ||
1212 | video->video.v4l2_dev = vdev; | |
1213 | ||
1214 | ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1); | |
1215 | if (ret < 0) | |
4cd89e91 LP |
1216 | dev_err(video->iss->dev, |
1217 | "%s: could not register video device (%d)\n", | |
fc96d58c SA |
1218 | __func__, ret); |
1219 | ||
1220 | return ret; | |
1221 | } | |
1222 | ||
1223 | void omap4iss_video_unregister(struct iss_video *video) | |
1224 | { | |
2b7f0b64 | 1225 | video_unregister_device(&video->video); |
fc96d58c | 1226 | } |