Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / staging / media / rpivid / rpivid.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Raspberry Pi HEVC driver
4  *
5  * Copyright (C) 2020 Raspberry Pi (Trading) Ltd
6  *
7  * Based on the Cedrus VPU driver, that is:
8  *
9  * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
10  * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
11  * Copyright (C) 2018 Bootlin
12  */
13
14 #include <linux/platform_device.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-ioctl.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-mem2mem.h>
22
23 #include "rpivid.h"
24 #include "rpivid_video.h"
25 #include "rpivid_hw.h"
26 #include "rpivid_dec.h"
27
28 /*
29  * Default /dev/videoN node number.
30  * Deliberately avoid the very low numbers as these are often taken by webcams
31  * etc, and simple apps tend to only go for /dev/video0.
32  */
33 static int video_nr = 19;
34 module_param(video_nr, int, 0644);
35 MODULE_PARM_DESC(video_nr, "decoder video device number");
36
37 static const struct rpivid_control rpivid_ctrls[] = {
38         {
39                 .cfg = {
40                         .id     = V4L2_CID_MPEG_VIDEO_HEVC_SPS,
41                         .ops    = &rpivid_hevc_sps_ctrl_ops,
42                 },
43                 .required       = true,
44         },
45         {
46                 .cfg = {
47                         .id     = V4L2_CID_MPEG_VIDEO_HEVC_PPS,
48                         .ops    = &rpivid_hevc_pps_ctrl_ops,
49                 },
50                 .required       = true,
51         },
52         {
53                 .cfg = {
54                         .id = V4L2_CID_MPEG_VIDEO_HEVC_SCALING_MATRIX,
55                 },
56                 .required       = false,
57         },
58         {
59                 .cfg = {
60                         .id     = V4L2_CID_MPEG_VIDEO_HEVC_DECODE_PARAMS,
61                 },
62                 .required       = true,
63         },
64         {
65                 .cfg = {
66                         .name   = "Slice param array",
67                         .id     = V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS,
68                         .type   = V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS,
69                         .flags  = V4L2_CTRL_FLAG_DYNAMIC_ARRAY,
70                         .dims   = { 0x1000 },
71                 },
72                 .required       = true,
73         },
74         {
75                 .cfg = {
76                         .id     = V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE,
77                         .max    = V4L2_MPEG_VIDEO_HEVC_DECODE_MODE_SLICE_BASED,
78                         .def    = V4L2_MPEG_VIDEO_HEVC_DECODE_MODE_SLICE_BASED,
79                 },
80                 .required       = false,
81         },
82         {
83                 .cfg = {
84                         .id     = V4L2_CID_MPEG_VIDEO_HEVC_START_CODE,
85                         .max    = V4L2_MPEG_VIDEO_HEVC_START_CODE_NONE,
86                         .def    = V4L2_MPEG_VIDEO_HEVC_START_CODE_NONE,
87                 },
88                 .required       = false,
89         },
90 };
91
92 #define rpivid_ctrls_COUNT      ARRAY_SIZE(rpivid_ctrls)
93
94 struct v4l2_ctrl *rpivid_find_ctrl(struct rpivid_ctx *ctx, u32 id)
95 {
96         unsigned int i;
97
98         for (i = 0; ctx->ctrls[i]; i++)
99                 if (ctx->ctrls[i]->id == id)
100                         return ctx->ctrls[i];
101
102         return NULL;
103 }
104
105 void *rpivid_find_control_data(struct rpivid_ctx *ctx, u32 id)
106 {
107         struct v4l2_ctrl *const ctrl = rpivid_find_ctrl(ctx, id);
108
109         return !ctrl ? NULL : ctrl->p_cur.p;
110 }
111
112 static int rpivid_init_ctrls(struct rpivid_dev *dev, struct rpivid_ctx *ctx)
113 {
114         struct v4l2_ctrl_handler *hdl = &ctx->hdl;
115         struct v4l2_ctrl *ctrl;
116         unsigned int ctrl_size;
117         unsigned int i;
118
119         v4l2_ctrl_handler_init(hdl, rpivid_ctrls_COUNT);
120         if (hdl->error) {
121                 v4l2_err(&dev->v4l2_dev,
122                          "Failed to initialize control handler\n");
123                 return hdl->error;
124         }
125
126         ctrl_size = sizeof(ctrl) * rpivid_ctrls_COUNT + 1;
127
128         ctx->ctrls = kzalloc(ctrl_size, GFP_KERNEL);
129         if (!ctx->ctrls)
130                 return -ENOMEM;
131
132         for (i = 0; i < rpivid_ctrls_COUNT; i++) {
133                 ctrl = v4l2_ctrl_new_custom(hdl, &rpivid_ctrls[i].cfg,
134                                             ctx);
135                 if (hdl->error) {
136                         v4l2_err(&dev->v4l2_dev,
137                                  "Failed to create new custom control id=%#x\n",
138                                  rpivid_ctrls[i].cfg.id);
139
140                         v4l2_ctrl_handler_free(hdl);
141                         kfree(ctx->ctrls);
142                         return hdl->error;
143                 }
144
145                 ctx->ctrls[i] = ctrl;
146         }
147
148         ctx->fh.ctrl_handler = hdl;
149         v4l2_ctrl_handler_setup(hdl);
150
151         return 0;
152 }
153
154 static int rpivid_request_validate(struct media_request *req)
155 {
156         struct media_request_object *obj;
157         struct v4l2_ctrl_handler *parent_hdl, *hdl;
158         struct rpivid_ctx *ctx = NULL;
159         struct v4l2_ctrl *ctrl_test;
160         unsigned int count;
161         unsigned int i;
162
163         list_for_each_entry(obj, &req->objects, list) {
164                 struct vb2_buffer *vb;
165
166                 if (vb2_request_object_is_buffer(obj)) {
167                         vb = container_of(obj, struct vb2_buffer, req_obj);
168                         ctx = vb2_get_drv_priv(vb->vb2_queue);
169
170                         break;
171                 }
172         }
173
174         if (!ctx)
175                 return -ENOENT;
176
177         count = vb2_request_buffer_cnt(req);
178         if (!count) {
179                 v4l2_info(&ctx->dev->v4l2_dev,
180                           "No buffer was provided with the request\n");
181                 return -ENOENT;
182         } else if (count > 1) {
183                 v4l2_info(&ctx->dev->v4l2_dev,
184                           "More than one buffer was provided with the request\n");
185                 return -EINVAL;
186         }
187
188         parent_hdl = &ctx->hdl;
189
190         hdl = v4l2_ctrl_request_hdl_find(req, parent_hdl);
191         if (!hdl) {
192                 v4l2_info(&ctx->dev->v4l2_dev, "Missing codec control(s)\n");
193                 return -ENOENT;
194         }
195
196         for (i = 0; i < rpivid_ctrls_COUNT; i++) {
197                 if (!rpivid_ctrls[i].required)
198                         continue;
199
200                 ctrl_test =
201                         v4l2_ctrl_request_hdl_ctrl_find(hdl,
202                                                         rpivid_ctrls[i].cfg.id);
203                 if (!ctrl_test) {
204                         v4l2_info(&ctx->dev->v4l2_dev,
205                                   "Missing required codec control\n");
206                         v4l2_ctrl_request_hdl_put(hdl);
207                         return -ENOENT;
208                 }
209         }
210
211         v4l2_ctrl_request_hdl_put(hdl);
212
213         return vb2_request_validate(req);
214 }
215
216 static int rpivid_open(struct file *file)
217 {
218         struct rpivid_dev *dev = video_drvdata(file);
219         struct rpivid_ctx *ctx = NULL;
220         int ret;
221
222         if (mutex_lock_interruptible(&dev->dev_mutex))
223                 return -ERESTARTSYS;
224
225         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
226         if (!ctx) {
227                 mutex_unlock(&dev->dev_mutex);
228                 ret = -ENOMEM;
229                 goto err_unlock;
230         }
231
232         mutex_init(&ctx->ctx_mutex);
233
234         v4l2_fh_init(&ctx->fh, video_devdata(file));
235         file->private_data = &ctx->fh;
236         ctx->dev = dev;
237
238         ret = rpivid_init_ctrls(dev, ctx);
239         if (ret)
240                 goto err_free;
241
242         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
243                                             &rpivid_queue_init);
244         if (IS_ERR(ctx->fh.m2m_ctx)) {
245                 ret = PTR_ERR(ctx->fh.m2m_ctx);
246                 goto err_ctrls;
247         }
248
249         /* The only bit of format info that we can guess now is H265 src
250          * Everything else we need more info for
251          */
252         rpivid_prepare_src_format(&ctx->src_fmt);
253
254         v4l2_fh_add(&ctx->fh);
255
256         mutex_unlock(&dev->dev_mutex);
257
258         return 0;
259
260 err_ctrls:
261         v4l2_ctrl_handler_free(&ctx->hdl);
262 err_free:
263         mutex_destroy(&ctx->ctx_mutex);
264         kfree(ctx);
265 err_unlock:
266         mutex_unlock(&dev->dev_mutex);
267
268         return ret;
269 }
270
271 static int rpivid_release(struct file *file)
272 {
273         struct rpivid_dev *dev = video_drvdata(file);
274         struct rpivid_ctx *ctx = container_of(file->private_data,
275                                               struct rpivid_ctx, fh);
276
277         mutex_lock(&dev->dev_mutex);
278
279         v4l2_fh_del(&ctx->fh);
280         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
281
282         v4l2_ctrl_handler_free(&ctx->hdl);
283         kfree(ctx->ctrls);
284
285         v4l2_fh_exit(&ctx->fh);
286         mutex_destroy(&ctx->ctx_mutex);
287
288         kfree(ctx);
289
290         mutex_unlock(&dev->dev_mutex);
291
292         return 0;
293 }
294
295 static const struct v4l2_file_operations rpivid_fops = {
296         .owner          = THIS_MODULE,
297         .open           = rpivid_open,
298         .release        = rpivid_release,
299         .poll           = v4l2_m2m_fop_poll,
300         .unlocked_ioctl = video_ioctl2,
301         .mmap           = v4l2_m2m_fop_mmap,
302 };
303
304 static const struct video_device rpivid_video_device = {
305         .name           = RPIVID_NAME,
306         .vfl_dir        = VFL_DIR_M2M,
307         .fops           = &rpivid_fops,
308         .ioctl_ops      = &rpivid_ioctl_ops,
309         .minor          = -1,
310         .release        = video_device_release_empty,
311         .device_caps    = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING,
312 };
313
314 static const struct v4l2_m2m_ops rpivid_m2m_ops = {
315         .device_run     = rpivid_device_run,
316 };
317
318 static const struct media_device_ops rpivid_m2m_media_ops = {
319         .req_validate   = rpivid_request_validate,
320         .req_queue      = v4l2_m2m_request_queue,
321 };
322
323 static int rpivid_probe(struct platform_device *pdev)
324 {
325         struct rpivid_dev *dev;
326         struct video_device *vfd;
327         int ret;
328
329         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
330         if (!dev)
331                 return -ENOMEM;
332
333         dev->vfd = rpivid_video_device;
334         dev->dev = &pdev->dev;
335         dev->pdev = pdev;
336
337         ret = 0;
338         ret = rpivid_hw_probe(dev);
339         if (ret) {
340                 dev_err(&pdev->dev, "Failed to probe hardware\n");
341                 return ret;
342         }
343
344         dev->dec_ops = &rpivid_dec_ops_h265;
345
346         mutex_init(&dev->dev_mutex);
347
348         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
349         if (ret) {
350                 dev_err(&pdev->dev, "Failed to register V4L2 device\n");
351                 return ret;
352         }
353
354         vfd = &dev->vfd;
355         vfd->lock = &dev->dev_mutex;
356         vfd->v4l2_dev = &dev->v4l2_dev;
357
358         snprintf(vfd->name, sizeof(vfd->name), "%s", rpivid_video_device.name);
359         video_set_drvdata(vfd, dev);
360
361         dev->m2m_dev = v4l2_m2m_init(&rpivid_m2m_ops);
362         if (IS_ERR(dev->m2m_dev)) {
363                 v4l2_err(&dev->v4l2_dev,
364                          "Failed to initialize V4L2 M2M device\n");
365                 ret = PTR_ERR(dev->m2m_dev);
366
367                 goto err_v4l2;
368         }
369
370         dev->mdev.dev = &pdev->dev;
371         strscpy(dev->mdev.model, RPIVID_NAME, sizeof(dev->mdev.model));
372         strscpy(dev->mdev.bus_info, "platform:" RPIVID_NAME,
373                 sizeof(dev->mdev.bus_info));
374
375         media_device_init(&dev->mdev);
376         dev->mdev.ops = &rpivid_m2m_media_ops;
377         dev->v4l2_dev.mdev = &dev->mdev;
378
379         ret = video_register_device(vfd, VFL_TYPE_VIDEO, video_nr);
380         if (ret) {
381                 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
382                 goto err_m2m;
383         }
384
385         v4l2_info(&dev->v4l2_dev,
386                   "Device registered as /dev/video%d\n", vfd->num);
387
388         ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
389                                                  MEDIA_ENT_F_PROC_VIDEO_DECODER);
390         if (ret) {
391                 v4l2_err(&dev->v4l2_dev,
392                          "Failed to initialize V4L2 M2M media controller\n");
393                 goto err_video;
394         }
395
396         ret = media_device_register(&dev->mdev);
397         if (ret) {
398                 v4l2_err(&dev->v4l2_dev, "Failed to register media device\n");
399                 goto err_m2m_mc;
400         }
401
402         platform_set_drvdata(pdev, dev);
403
404         return 0;
405
406 err_m2m_mc:
407         v4l2_m2m_unregister_media_controller(dev->m2m_dev);
408 err_video:
409         video_unregister_device(&dev->vfd);
410 err_m2m:
411         v4l2_m2m_release(dev->m2m_dev);
412 err_v4l2:
413         v4l2_device_unregister(&dev->v4l2_dev);
414
415         return ret;
416 }
417
418 static int rpivid_remove(struct platform_device *pdev)
419 {
420         struct rpivid_dev *dev = platform_get_drvdata(pdev);
421
422         if (media_devnode_is_registered(dev->mdev.devnode)) {
423                 media_device_unregister(&dev->mdev);
424                 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
425                 media_device_cleanup(&dev->mdev);
426         }
427
428         v4l2_m2m_release(dev->m2m_dev);
429         video_unregister_device(&dev->vfd);
430         v4l2_device_unregister(&dev->v4l2_dev);
431
432         rpivid_hw_remove(dev);
433
434         return 0;
435 }
436
437 static const struct of_device_id rpivid_dt_match[] = {
438         {
439                 .compatible = "raspberrypi,rpivid-vid-decoder",
440         },
441         { /* sentinel */ }
442 };
443 MODULE_DEVICE_TABLE(of, rpivid_dt_match);
444
445 static struct platform_driver rpivid_driver = {
446         .probe          = rpivid_probe,
447         .remove         = rpivid_remove,
448         .driver         = {
449                 .name = RPIVID_NAME,
450                 .of_match_table = of_match_ptr(rpivid_dt_match),
451         },
452 };
453 module_platform_driver(rpivid_driver);
454
455 MODULE_LICENSE("GPL v2");
456 MODULE_AUTHOR("John Cox <jc@kynesim.co.uk>");
457 MODULE_DESCRIPTION("Raspberry Pi HEVC V4L2 driver");