Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vimc-capture.c Virtual Media Controller Driver |
| 3 | * |
| 4 | * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 18 | #include <linux/component.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 21 | #include <media/v4l2-ioctl.h> |
| 22 | #include <media/videobuf2-core.h> |
| 23 | #include <media/videobuf2-vmalloc.h> |
| 24 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 25 | #include "vimc-common.h" |
| 26 | |
| 27 | #define VIMC_CAP_DRV_NAME "vimc-capture" |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 28 | |
| 29 | struct vimc_cap_device { |
| 30 | struct vimc_ent_device ved; |
| 31 | struct video_device vdev; |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 32 | struct device *dev; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 33 | struct v4l2_pix_format format; |
| 34 | struct vb2_queue queue; |
| 35 | struct list_head buf_list; |
| 36 | /* |
| 37 | * NOTE: in a real driver, a spin lock must be used to access the |
| 38 | * queue because the frames are generated from a hardware interruption |
| 39 | * and the isr is not allowed to sleep. |
| 40 | * Even if it is not necessary a spinlock in the vimc driver, we |
| 41 | * use it here as a code reference |
| 42 | */ |
| 43 | spinlock_t qlock; |
| 44 | struct mutex lock; |
| 45 | u32 sequence; |
| 46 | struct media_pipeline pipe; |
| 47 | }; |
| 48 | |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 49 | static const struct v4l2_pix_format fmt_default = { |
| 50 | .width = 640, |
| 51 | .height = 480, |
| 52 | .pixelformat = V4L2_PIX_FMT_RGB24, |
| 53 | .field = V4L2_FIELD_NONE, |
| 54 | .colorspace = V4L2_COLORSPACE_DEFAULT, |
| 55 | }; |
| 56 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 57 | struct vimc_cap_buffer { |
| 58 | /* |
| 59 | * struct vb2_v4l2_buffer must be the first element |
| 60 | * the videobuf2 framework will allocate this struct based on |
| 61 | * buf_struct_size and use the first sizeof(struct vb2_buffer) bytes of |
| 62 | * memory as a vb2_buffer |
| 63 | */ |
| 64 | struct vb2_v4l2_buffer vb2; |
| 65 | struct list_head list; |
| 66 | }; |
| 67 | |
| 68 | static int vimc_cap_querycap(struct file *file, void *priv, |
| 69 | struct v4l2_capability *cap) |
| 70 | { |
| 71 | struct vimc_cap_device *vcap = video_drvdata(file); |
| 72 | |
| 73 | strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); |
| 74 | strlcpy(cap->card, KBUILD_MODNAME, sizeof(cap->card)); |
| 75 | snprintf(cap->bus_info, sizeof(cap->bus_info), |
| 76 | "platform:%s", vcap->vdev.v4l2_dev->name); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 81 | static void vimc_cap_get_format(struct vimc_ent_device *ved, |
| 82 | struct v4l2_pix_format *fmt) |
| 83 | { |
| 84 | struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device, |
| 85 | ved); |
| 86 | |
| 87 | *fmt = vcap->format; |
| 88 | } |
| 89 | |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 90 | static int vimc_cap_g_fmt_vid_cap(struct file *file, void *priv, |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 91 | struct v4l2_format *f) |
| 92 | { |
| 93 | struct vimc_cap_device *vcap = video_drvdata(file); |
| 94 | |
| 95 | f->fmt.pix = vcap->format; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 100 | static int vimc_cap_try_fmt_vid_cap(struct file *file, void *priv, |
| 101 | struct v4l2_format *f) |
| 102 | { |
| 103 | struct v4l2_pix_format *format = &f->fmt.pix; |
| 104 | const struct vimc_pix_map *vpix; |
| 105 | |
| 106 | format->width = clamp_t(u32, format->width, VIMC_FRAME_MIN_WIDTH, |
| 107 | VIMC_FRAME_MAX_WIDTH) & ~1; |
| 108 | format->height = clamp_t(u32, format->height, VIMC_FRAME_MIN_HEIGHT, |
| 109 | VIMC_FRAME_MAX_HEIGHT) & ~1; |
| 110 | |
| 111 | /* Don't accept a pixelformat that is not on the table */ |
| 112 | vpix = vimc_pix_map_by_pixelformat(format->pixelformat); |
| 113 | if (!vpix) { |
| 114 | format->pixelformat = fmt_default.pixelformat; |
| 115 | vpix = vimc_pix_map_by_pixelformat(format->pixelformat); |
| 116 | } |
| 117 | /* TODO: Add support for custom bytesperline values */ |
| 118 | format->bytesperline = format->width * vpix->bpp; |
| 119 | format->sizeimage = format->bytesperline * format->height; |
| 120 | |
| 121 | if (format->field == V4L2_FIELD_ANY) |
| 122 | format->field = fmt_default.field; |
| 123 | |
| 124 | vimc_colorimetry_clamp(format); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int vimc_cap_s_fmt_vid_cap(struct file *file, void *priv, |
| 130 | struct v4l2_format *f) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 131 | { |
| 132 | struct vimc_cap_device *vcap = video_drvdata(file); |
| 133 | |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 134 | /* Do not change the format while stream is on */ |
| 135 | if (vb2_is_busy(&vcap->queue)) |
| 136 | return -EBUSY; |
| 137 | |
| 138 | vimc_cap_try_fmt_vid_cap(file, priv, f); |
| 139 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 140 | dev_dbg(vcap->dev, "%s: format update: " |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 141 | "old:%dx%d (0x%x, %d, %d, %d, %d) " |
| 142 | "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vcap->vdev.name, |
| 143 | /* old */ |
| 144 | vcap->format.width, vcap->format.height, |
| 145 | vcap->format.pixelformat, vcap->format.colorspace, |
| 146 | vcap->format.quantization, vcap->format.xfer_func, |
| 147 | vcap->format.ycbcr_enc, |
| 148 | /* new */ |
| 149 | f->fmt.pix.width, f->fmt.pix.height, |
| 150 | f->fmt.pix.pixelformat, f->fmt.pix.colorspace, |
| 151 | f->fmt.pix.quantization, f->fmt.pix.xfer_func, |
| 152 | f->fmt.pix.ycbcr_enc); |
| 153 | |
| 154 | vcap->format = f->fmt.pix; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int vimc_cap_enum_fmt_vid_cap(struct file *file, void *priv, |
| 160 | struct v4l2_fmtdesc *f) |
| 161 | { |
| 162 | const struct vimc_pix_map *vpix = vimc_pix_map_by_index(f->index); |
| 163 | |
| 164 | if (!vpix) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 167 | f->pixelformat = vpix->pixelformat; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int vimc_cap_enum_framesizes(struct file *file, void *fh, |
| 173 | struct v4l2_frmsizeenum *fsize) |
| 174 | { |
| 175 | const struct vimc_pix_map *vpix; |
| 176 | |
| 177 | if (fsize->index) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | /* Only accept code in the pix map table */ |
| 181 | vpix = vimc_pix_map_by_code(fsize->pixel_format); |
| 182 | if (!vpix) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; |
| 186 | fsize->stepwise.min_width = VIMC_FRAME_MIN_WIDTH; |
| 187 | fsize->stepwise.max_width = VIMC_FRAME_MAX_WIDTH; |
| 188 | fsize->stepwise.min_height = VIMC_FRAME_MIN_HEIGHT; |
| 189 | fsize->stepwise.max_height = VIMC_FRAME_MAX_HEIGHT; |
| 190 | fsize->stepwise.step_width = 2; |
| 191 | fsize->stepwise.step_height = 2; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static const struct v4l2_file_operations vimc_cap_fops = { |
| 197 | .owner = THIS_MODULE, |
| 198 | .open = v4l2_fh_open, |
| 199 | .release = vb2_fop_release, |
| 200 | .read = vb2_fop_read, |
| 201 | .poll = vb2_fop_poll, |
| 202 | .unlocked_ioctl = video_ioctl2, |
| 203 | .mmap = vb2_fop_mmap, |
| 204 | }; |
| 205 | |
| 206 | static const struct v4l2_ioctl_ops vimc_cap_ioctl_ops = { |
| 207 | .vidioc_querycap = vimc_cap_querycap, |
| 208 | |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 209 | .vidioc_g_fmt_vid_cap = vimc_cap_g_fmt_vid_cap, |
| 210 | .vidioc_s_fmt_vid_cap = vimc_cap_s_fmt_vid_cap, |
| 211 | .vidioc_try_fmt_vid_cap = vimc_cap_try_fmt_vid_cap, |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 212 | .vidioc_enum_fmt_vid_cap = vimc_cap_enum_fmt_vid_cap, |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 213 | .vidioc_enum_framesizes = vimc_cap_enum_framesizes, |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 214 | |
| 215 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
| 216 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
| 217 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, |
| 218 | .vidioc_querybuf = vb2_ioctl_querybuf, |
| 219 | .vidioc_qbuf = vb2_ioctl_qbuf, |
| 220 | .vidioc_dqbuf = vb2_ioctl_dqbuf, |
| 221 | .vidioc_expbuf = vb2_ioctl_expbuf, |
| 222 | .vidioc_streamon = vb2_ioctl_streamon, |
| 223 | .vidioc_streamoff = vb2_ioctl_streamoff, |
| 224 | }; |
| 225 | |
| 226 | static void vimc_cap_return_all_buffers(struct vimc_cap_device *vcap, |
| 227 | enum vb2_buffer_state state) |
| 228 | { |
| 229 | struct vimc_cap_buffer *vbuf, *node; |
| 230 | |
| 231 | spin_lock(&vcap->qlock); |
| 232 | |
| 233 | list_for_each_entry_safe(vbuf, node, &vcap->buf_list, list) { |
| 234 | list_del(&vbuf->list); |
| 235 | vb2_buffer_done(&vbuf->vb2.vb2_buf, state); |
| 236 | } |
| 237 | |
| 238 | spin_unlock(&vcap->qlock); |
| 239 | } |
| 240 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 241 | static int vimc_cap_start_streaming(struct vb2_queue *vq, unsigned int count) |
| 242 | { |
| 243 | struct vimc_cap_device *vcap = vb2_get_drv_priv(vq); |
| 244 | struct media_entity *entity = &vcap->vdev.entity; |
| 245 | int ret; |
| 246 | |
| 247 | vcap->sequence = 0; |
| 248 | |
| 249 | /* Start the media pipeline */ |
| 250 | ret = media_pipeline_start(entity, &vcap->pipe); |
| 251 | if (ret) { |
| 252 | vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | /* Enable streaming from the pipe */ |
Helen Fornazier | bf5fb95 | 2017-06-19 14:00:13 -0300 | [diff] [blame] | 257 | ret = vimc_pipeline_s_stream(&vcap->vdev.entity, 1); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 258 | if (ret) { |
| 259 | media_pipeline_stop(entity); |
| 260 | vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Stop the stream engine. Any remaining buffers in the stream queue are |
| 269 | * dequeued and passed on to the vb2 framework marked as STATE_ERROR. |
| 270 | */ |
| 271 | static void vimc_cap_stop_streaming(struct vb2_queue *vq) |
| 272 | { |
| 273 | struct vimc_cap_device *vcap = vb2_get_drv_priv(vq); |
| 274 | |
| 275 | /* Disable streaming from the pipe */ |
Helen Fornazier | bf5fb95 | 2017-06-19 14:00:13 -0300 | [diff] [blame] | 276 | vimc_pipeline_s_stream(&vcap->vdev.entity, 0); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 277 | |
| 278 | /* Stop the media pipeline */ |
| 279 | media_pipeline_stop(&vcap->vdev.entity); |
| 280 | |
| 281 | /* Release all active buffers */ |
| 282 | vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_ERROR); |
| 283 | } |
| 284 | |
| 285 | static void vimc_cap_buf_queue(struct vb2_buffer *vb2_buf) |
| 286 | { |
| 287 | struct vimc_cap_device *vcap = vb2_get_drv_priv(vb2_buf->vb2_queue); |
| 288 | struct vimc_cap_buffer *buf = container_of(vb2_buf, |
| 289 | struct vimc_cap_buffer, |
| 290 | vb2.vb2_buf); |
| 291 | |
| 292 | spin_lock(&vcap->qlock); |
| 293 | list_add_tail(&buf->list, &vcap->buf_list); |
| 294 | spin_unlock(&vcap->qlock); |
| 295 | } |
| 296 | |
| 297 | static int vimc_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, |
| 298 | unsigned int *nplanes, unsigned int sizes[], |
| 299 | struct device *alloc_devs[]) |
| 300 | { |
| 301 | struct vimc_cap_device *vcap = vb2_get_drv_priv(vq); |
| 302 | |
| 303 | if (*nplanes) |
| 304 | return sizes[0] < vcap->format.sizeimage ? -EINVAL : 0; |
| 305 | /* We don't support multiplanes for now */ |
| 306 | *nplanes = 1; |
| 307 | sizes[0] = vcap->format.sizeimage; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int vimc_cap_buffer_prepare(struct vb2_buffer *vb) |
| 313 | { |
| 314 | struct vimc_cap_device *vcap = vb2_get_drv_priv(vb->vb2_queue); |
| 315 | unsigned long size = vcap->format.sizeimage; |
| 316 | |
| 317 | if (vb2_plane_size(vb, 0) < size) { |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 318 | dev_err(vcap->dev, "%s: buffer too small (%lu < %lu)\n", |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 319 | vcap->vdev.name, vb2_plane_size(vb, 0), size); |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static const struct vb2_ops vimc_cap_qops = { |
| 326 | .start_streaming = vimc_cap_start_streaming, |
| 327 | .stop_streaming = vimc_cap_stop_streaming, |
| 328 | .buf_queue = vimc_cap_buf_queue, |
| 329 | .queue_setup = vimc_cap_queue_setup, |
| 330 | .buf_prepare = vimc_cap_buffer_prepare, |
| 331 | /* |
| 332 | * Since q->lock is set we can use the standard |
| 333 | * vb2_ops_wait_prepare/finish helper functions. |
| 334 | */ |
| 335 | .wait_prepare = vb2_ops_wait_prepare, |
| 336 | .wait_finish = vb2_ops_wait_finish, |
| 337 | }; |
| 338 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 339 | static const struct media_entity_operations vimc_cap_mops = { |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 340 | .link_validate = vimc_link_validate, |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 341 | }; |
| 342 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 343 | static void vimc_cap_comp_unbind(struct device *comp, struct device *master, |
| 344 | void *master_data) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 345 | { |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 346 | struct vimc_ent_device *ved = dev_get_drvdata(comp); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 347 | struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device, |
| 348 | ved); |
| 349 | |
| 350 | vb2_queue_release(&vcap->queue); |
| 351 | media_entity_cleanup(ved->ent); |
| 352 | video_unregister_device(&vcap->vdev); |
| 353 | vimc_pads_cleanup(vcap->ved.pads); |
| 354 | kfree(vcap); |
| 355 | } |
| 356 | |
| 357 | static void vimc_cap_process_frame(struct vimc_ent_device *ved, |
| 358 | struct media_pad *sink, const void *frame) |
| 359 | { |
| 360 | struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device, |
| 361 | ved); |
| 362 | struct vimc_cap_buffer *vimc_buf; |
| 363 | void *vbuf; |
| 364 | |
| 365 | spin_lock(&vcap->qlock); |
| 366 | |
| 367 | /* Get the first entry of the list */ |
| 368 | vimc_buf = list_first_entry_or_null(&vcap->buf_list, |
| 369 | typeof(*vimc_buf), list); |
| 370 | if (!vimc_buf) { |
| 371 | spin_unlock(&vcap->qlock); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | /* Remove this entry from the list */ |
| 376 | list_del(&vimc_buf->list); |
| 377 | |
| 378 | spin_unlock(&vcap->qlock); |
| 379 | |
| 380 | /* Fill the buffer */ |
| 381 | vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns(); |
| 382 | vimc_buf->vb2.sequence = vcap->sequence++; |
| 383 | vimc_buf->vb2.field = vcap->format.field; |
| 384 | |
| 385 | vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0); |
| 386 | |
| 387 | memcpy(vbuf, frame, vcap->format.sizeimage); |
| 388 | |
| 389 | /* Set it as ready */ |
| 390 | vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0, |
| 391 | vcap->format.sizeimage); |
| 392 | vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE); |
| 393 | } |
| 394 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 395 | static int vimc_cap_comp_bind(struct device *comp, struct device *master, |
| 396 | void *master_data) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 397 | { |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 398 | struct v4l2_device *v4l2_dev = master_data; |
| 399 | struct vimc_platform_data *pdata = comp->platform_data; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 400 | const struct vimc_pix_map *vpix; |
| 401 | struct vimc_cap_device *vcap; |
| 402 | struct video_device *vdev; |
| 403 | struct vb2_queue *q; |
| 404 | int ret; |
| 405 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 406 | /* Allocate the vimc_cap_device struct */ |
| 407 | vcap = kzalloc(sizeof(*vcap), GFP_KERNEL); |
| 408 | if (!vcap) |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 409 | return -ENOMEM; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 410 | |
| 411 | /* Allocate the pads */ |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 412 | vcap->ved.pads = |
| 413 | vimc_pads_init(1, (const unsigned long[1]) {MEDIA_PAD_FL_SINK}); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 414 | if (IS_ERR(vcap->ved.pads)) { |
| 415 | ret = PTR_ERR(vcap->ved.pads); |
| 416 | goto err_free_vcap; |
| 417 | } |
| 418 | |
| 419 | /* Initialize the media entity */ |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 420 | vcap->vdev.entity.name = pdata->entity_name; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 421 | vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L; |
| 422 | ret = media_entity_pads_init(&vcap->vdev.entity, |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 423 | 1, vcap->ved.pads); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 424 | if (ret) |
| 425 | goto err_clean_pads; |
| 426 | |
| 427 | /* Initialize the lock */ |
| 428 | mutex_init(&vcap->lock); |
| 429 | |
| 430 | /* Initialize the vb2 queue */ |
| 431 | q = &vcap->queue; |
| 432 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 433 | q->io_modes = VB2_MMAP | VB2_DMABUF; |
| 434 | q->drv_priv = vcap; |
| 435 | q->buf_struct_size = sizeof(struct vimc_cap_buffer); |
| 436 | q->ops = &vimc_cap_qops; |
| 437 | q->mem_ops = &vb2_vmalloc_memops; |
| 438 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
| 439 | q->min_buffers_needed = 2; |
| 440 | q->lock = &vcap->lock; |
| 441 | |
| 442 | ret = vb2_queue_init(q); |
| 443 | if (ret) { |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 444 | dev_err(comp, "%s: vb2 queue init failed (err=%d)\n", |
| 445 | pdata->entity_name, ret); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 446 | goto err_clean_m_ent; |
| 447 | } |
| 448 | |
| 449 | /* Initialize buffer list and its lock */ |
| 450 | INIT_LIST_HEAD(&vcap->buf_list); |
| 451 | spin_lock_init(&vcap->qlock); |
| 452 | |
Helen Fornazier | 535d296 | 2017-06-19 14:00:17 -0300 | [diff] [blame] | 453 | /* Set default frame format */ |
| 454 | vcap->format = fmt_default; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 455 | vpix = vimc_pix_map_by_pixelformat(vcap->format.pixelformat); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 456 | vcap->format.bytesperline = vcap->format.width * vpix->bpp; |
| 457 | vcap->format.sizeimage = vcap->format.bytesperline * |
| 458 | vcap->format.height; |
| 459 | |
| 460 | /* Fill the vimc_ent_device struct */ |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 461 | vcap->ved.ent = &vcap->vdev.entity; |
| 462 | vcap->ved.process_frame = vimc_cap_process_frame; |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 463 | vcap->ved.vdev_get_format = vimc_cap_get_format; |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 464 | dev_set_drvdata(comp, &vcap->ved); |
| 465 | vcap->dev = comp; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 466 | |
| 467 | /* Initialize the video_device struct */ |
| 468 | vdev = &vcap->vdev; |
| 469 | vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 470 | vdev->entity.ops = &vimc_cap_mops; |
| 471 | vdev->release = video_device_release_empty; |
| 472 | vdev->fops = &vimc_cap_fops; |
| 473 | vdev->ioctl_ops = &vimc_cap_ioctl_ops; |
| 474 | vdev->lock = &vcap->lock; |
| 475 | vdev->queue = q; |
| 476 | vdev->v4l2_dev = v4l2_dev; |
| 477 | vdev->vfl_dir = VFL_DIR_RX; |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 478 | strlcpy(vdev->name, pdata->entity_name, sizeof(vdev->name)); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 479 | video_set_drvdata(vdev, &vcap->ved); |
| 480 | |
| 481 | /* Register the video_device with the v4l2 and the media framework */ |
| 482 | ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); |
| 483 | if (ret) { |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 484 | dev_err(comp, "%s: video register failed (err=%d)\n", |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 485 | vcap->vdev.name, ret); |
| 486 | goto err_release_queue; |
| 487 | } |
| 488 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 489 | return 0; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 490 | |
| 491 | err_release_queue: |
| 492 | vb2_queue_release(q); |
| 493 | err_clean_m_ent: |
| 494 | media_entity_cleanup(&vcap->vdev.entity); |
| 495 | err_clean_pads: |
| 496 | vimc_pads_cleanup(vcap->ved.pads); |
| 497 | err_free_vcap: |
| 498 | kfree(vcap); |
| 499 | |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 500 | return ret; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 501 | } |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 502 | |
| 503 | static const struct component_ops vimc_cap_comp_ops = { |
| 504 | .bind = vimc_cap_comp_bind, |
| 505 | .unbind = vimc_cap_comp_unbind, |
| 506 | }; |
| 507 | |
| 508 | static int vimc_cap_probe(struct platform_device *pdev) |
| 509 | { |
| 510 | return component_add(&pdev->dev, &vimc_cap_comp_ops); |
| 511 | } |
| 512 | |
| 513 | static int vimc_cap_remove(struct platform_device *pdev) |
| 514 | { |
| 515 | component_del(&pdev->dev, &vimc_cap_comp_ops); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static struct platform_driver vimc_cap_pdrv = { |
| 521 | .probe = vimc_cap_probe, |
| 522 | .remove = vimc_cap_remove, |
| 523 | .driver = { |
| 524 | .name = VIMC_CAP_DRV_NAME, |
| 525 | }, |
| 526 | }; |
| 527 | |
| 528 | static const struct platform_device_id vimc_cap_driver_ids[] = { |
| 529 | { |
| 530 | .name = VIMC_CAP_DRV_NAME, |
| 531 | }, |
| 532 | { } |
| 533 | }; |
| 534 | |
| 535 | module_platform_driver(vimc_cap_pdrv); |
| 536 | |
| 537 | MODULE_DEVICE_TABLE(platform, vimc_cap_driver_ids); |
| 538 | |
| 539 | MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Capture"); |
| 540 | MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>"); |
| 541 | MODULE_LICENSE("GPL"); |