forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/media/usb/zr364xx/zr364xx.c
....@@ -1,8 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Zoran 364xx based USB webcam module version 0.73
34 *
45 * Allows you to use your USB webcam with V4L2 applications
5
- * This is still in heavy developpement !
6
+ * This is still in heavy development !
67 *
78 * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
89 * http://royale.zerezo.com/zr364xx/
....@@ -11,16 +12,6 @@
1112 * V4L2 version inspired by meye.c driver
1213 *
1314 * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
14
- *
15
- * This program is free software; you can redistribute it and/or modify
16
- * it under the terms of the GNU General Public License as published by
17
- * the Free Software Foundation; either version 2 of the License, or
18
- * (at your option) any later version.
19
- *
20
- * This program is distributed in the hope that it will be useful,
21
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
- * GNU General Public License for more details.
2415 */
2516
2617
....@@ -29,7 +20,6 @@
2920 #include <linux/usb.h>
3021 #include <linux/vmalloc.h>
3122 #include <linux/slab.h>
32
-#include <linux/proc_fs.h>
3323 #include <linux/highmem.h>
3424 #include <media/v4l2-common.h>
3525 #include <media/v4l2-ioctl.h>
....@@ -150,7 +140,6 @@
150140 };
151141
152142 struct zr364xx_fmt {
153
- char *name;
154143 u32 fourcc;
155144 int depth;
156145 };
....@@ -158,7 +147,6 @@
158147 /* image formats. */
159148 static const struct zr364xx_fmt formats[] = {
160149 {
161
- .name = "JPG",
162150 .fourcc = V4L2_PIX_FMT_JPEG,
163151 .depth = 24
164152 }
....@@ -208,11 +196,9 @@
208196 {
209197 int status;
210198
211
- unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
199
+ unsigned char *transfer_buffer = kmemdup(cp, size, GFP_KERNEL);
212200 if (!transfer_buffer)
213201 return -ENOMEM;
214
-
215
- memcpy(transfer_buffer, cp, size);
216202
217203 status = usb_control_msg(udev,
218204 usb_sndctrlpipe(udev, 0),
....@@ -385,8 +371,7 @@
385371 vb);
386372 int rc;
387373
388
- DBG("%s, field=%d, fmt name = %s\n", __func__, field,
389
- cam->fmt ? cam->fmt->name : "");
374
+ DBG("%s, field=%d\n", __func__, field);
390375 if (!cam->fmt)
391376 return -EINVAL;
392377
....@@ -521,7 +506,7 @@
521506 /* tell v4l buffer was filled */
522507
523508 buf->vb.field_count = cam->frame_count * 2;
524
- v4l2_get_timestamp(&buf->vb.ts);
509
+ buf->vb.ts = ktime_get_ns();
525510 buf->vb.state = VIDEOBUF_DONE;
526511 }
527512
....@@ -549,7 +534,7 @@
549534 goto unlock;
550535 }
551536 list_del(&buf->vb.queue);
552
- v4l2_get_timestamp(&buf->vb.ts);
537
+ buf->vb.ts = ktime_get_ns();
553538 DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
554539 zr364xx_fillbuff(cam, buf, jpgsize);
555540 wake_up(&buf->vb.done);
....@@ -570,14 +555,12 @@
570555 {
571556 unsigned char *pdest;
572557 unsigned char *psrc;
573
- s32 idx = -1;
574
- struct zr364xx_framei *frm;
558
+ s32 idx = cam->cur_frame;
559
+ struct zr364xx_framei *frm = &cam->buffer.frame[idx];
575560 int i = 0;
576561 unsigned char *ptr = NULL;
577562
578563 _DBG("buffer to user\n");
579
- idx = cam->cur_frame;
580
- frm = &cam->buffer.frame[idx];
581564
582565 /* swap bytes if camera needs it */
583566 if (cam->method == METHOD0) {
....@@ -702,16 +685,11 @@
702685 {
703686 struct zr364xx_camera *cam = video_drvdata(file);
704687
705
- strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
688
+ strscpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
706689 if (cam->udev->product)
707
- strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
708
- strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
690
+ strscpy(cap->card, cam->udev->product, sizeof(cap->card));
691
+ strscpy(cap->bus_info, dev_name(&cam->udev->dev),
709692 sizeof(cap->bus_info));
710
- cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
711
- V4L2_CAP_READWRITE |
712
- V4L2_CAP_STREAMING;
713
- cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
714
-
715693 return 0;
716694 }
717695
....@@ -720,7 +698,7 @@
720698 {
721699 if (i->index != 0)
722700 return -EINVAL;
723
- strcpy(i->name, DRIVER_DESC " Camera");
701
+ strscpy(i->name, DRIVER_DESC " Camera", sizeof(i->name));
724702 i->type = V4L2_INPUT_TYPE_CAMERA;
725703 return 0;
726704 }
....@@ -765,8 +743,6 @@
765743 {
766744 if (f->index > 0)
767745 return -EINVAL;
768
- f->flags = V4L2_FMT_FLAG_COMPRESSED;
769
- strcpy(f->description, formats[0].name);
770746 f->pixelformat = formats[0].fourcc;
771747 return 0;
772748 }
....@@ -1208,15 +1184,11 @@
12081184 return err;
12091185 }
12101186
1211
-static void zr364xx_release(struct v4l2_device *v4l2_dev)
1187
+static void zr364xx_board_uninit(struct zr364xx_camera *cam)
12121188 {
1213
- struct zr364xx_camera *cam =
1214
- container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
12151189 unsigned long i;
12161190
1217
- v4l2_device_unregister(&cam->v4l2_dev);
1218
-
1219
- videobuf_mmap_free(&cam->vb_vidq);
1191
+ zr364xx_stop_readpipe(cam);
12201192
12211193 /* release sys buffers */
12221194 for (i = 0; i < FRAMES; i++) {
....@@ -1227,9 +1199,19 @@
12271199 cam->buffer.frame[i].lpvbits = NULL;
12281200 }
12291201
1230
- v4l2_ctrl_handler_free(&cam->ctrl_handler);
12311202 /* release transfer buffer */
12321203 kfree(cam->pipe->transfer_buffer);
1204
+}
1205
+
1206
+static void zr364xx_release(struct v4l2_device *v4l2_dev)
1207
+{
1208
+ struct zr364xx_camera *cam =
1209
+ container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
1210
+
1211
+ videobuf_mmap_free(&cam->vb_vidq);
1212
+ v4l2_ctrl_handler_free(&cam->ctrl_handler);
1213
+ zr364xx_board_uninit(cam);
1214
+ v4l2_device_unregister(&cam->v4l2_dev);
12331215 kfree(cam);
12341216 }
12351217
....@@ -1339,6 +1321,8 @@
13391321 .fops = &zr364xx_fops,
13401322 .ioctl_ops = &zr364xx_ioctl_ops,
13411323 .release = video_device_release_empty,
1324
+ .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1325
+ V4L2_CAP_STREAMING,
13421326 };
13431327
13441328
....@@ -1350,6 +1334,7 @@
13501334 {
13511335 struct zr364xx_pipeinfo *pipe = cam->pipe;
13521336 unsigned long i;
1337
+ int err;
13531338
13541339 DBG("board init: %p\n", cam);
13551340 memset(pipe, 0, sizeof(*pipe));
....@@ -1382,9 +1367,8 @@
13821367
13831368 if (i == 0) {
13841369 printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
1385
- kfree(cam->pipe->transfer_buffer);
1386
- cam->pipe->transfer_buffer = NULL;
1387
- return -ENOMEM;
1370
+ err = -ENOMEM;
1371
+ goto err_free;
13881372 } else
13891373 cam->buffer.dwFrames = i;
13901374
....@@ -1399,9 +1383,20 @@
13991383 /*** end create system buffers ***/
14001384
14011385 /* start read pipe */
1402
- zr364xx_start_readpipe(cam);
1386
+ err = zr364xx_start_readpipe(cam);
1387
+ if (err)
1388
+ goto err_free_frames;
1389
+
14031390 DBG(": board initialized\n");
14041391 return 0;
1392
+
1393
+err_free_frames:
1394
+ for (i = 0; i < FRAMES; i++)
1395
+ vfree(cam->buffer.frame[i].lpvbits);
1396
+err_free:
1397
+ kfree(cam->pipe->transfer_buffer);
1398
+ cam->pipe->transfer_buffer = NULL;
1399
+ return err;
14051400 }
14061401
14071402 static int zr364xx_probe(struct usb_interface *intf,
....@@ -1426,12 +1421,10 @@
14261421 if (!cam)
14271422 return -ENOMEM;
14281423
1429
- cam->v4l2_dev.release = zr364xx_release;
14301424 err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
14311425 if (err < 0) {
14321426 dev_err(&udev->dev, "couldn't register v4l2_device\n");
1433
- kfree(cam);
1434
- return err;
1427
+ goto free_cam;
14351428 }
14361429 hdl = &cam->ctrl_handler;
14371430 v4l2_ctrl_handler_init(hdl, 1);
....@@ -1440,7 +1433,7 @@
14401433 if (hdl->error) {
14411434 err = hdl->error;
14421435 dev_err(&udev->dev, "couldn't register control\n");
1443
- goto fail;
1436
+ goto free_hdlr_and_unreg_dev;
14441437 }
14451438 /* save the init method used by this camera */
14461439 cam->method = id->driver_info;
....@@ -1513,7 +1506,7 @@
15131506 if (!cam->read_endpoint) {
15141507 err = -ENOMEM;
15151508 dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
1516
- goto fail;
1509
+ goto free_hdlr_and_unreg_dev;
15171510 }
15181511
15191512 /* v4l */
....@@ -1524,10 +1517,11 @@
15241517
15251518 /* load zr364xx board specific */
15261519 err = zr364xx_board_init(cam);
1527
- if (!err)
1528
- err = v4l2_ctrl_handler_setup(hdl);
15291520 if (err)
1530
- goto fail;
1521
+ goto free_hdlr_and_unreg_dev;
1522
+ err = v4l2_ctrl_handler_setup(hdl);
1523
+ if (err)
1524
+ goto board_uninit;
15311525
15321526 spin_lock_init(&cam->slock);
15331527
....@@ -1539,19 +1533,23 @@
15391533 V4L2_FIELD_NONE,
15401534 sizeof(struct zr364xx_buffer), cam, &cam->lock);
15411535
1542
- err = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1536
+ err = video_register_device(&cam->vdev, VFL_TYPE_VIDEO, -1);
15431537 if (err) {
15441538 dev_err(&udev->dev, "video_register_device failed\n");
1545
- goto fail;
1539
+ goto board_uninit;
15461540 }
1541
+ cam->v4l2_dev.release = zr364xx_release;
15471542
15481543 dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
15491544 video_device_node_name(&cam->vdev));
15501545 return 0;
15511546
1552
-fail:
1547
+board_uninit:
1548
+ zr364xx_board_uninit(cam);
1549
+free_hdlr_and_unreg_dev:
15531550 v4l2_ctrl_handler_free(hdl);
15541551 v4l2_device_unregister(&cam->v4l2_dev);
1552
+free_cam:
15551553 kfree(cam);
15561554 return err;
15571555 }
....@@ -1598,10 +1596,19 @@
15981596 if (!cam->was_streaming)
15991597 return 0;
16001598
1601
- zr364xx_start_readpipe(cam);
1599
+ res = zr364xx_start_readpipe(cam);
1600
+ if (res)
1601
+ return res;
1602
+
16021603 res = zr364xx_prepare(cam);
1603
- if (!res)
1604
- zr364xx_start_acquire(cam);
1604
+ if (res)
1605
+ goto err_prepare;
1606
+
1607
+ zr364xx_start_acquire(cam);
1608
+ return 0;
1609
+
1610
+err_prepare:
1611
+ zr364xx_stop_readpipe(cam);
16051612 return res;
16061613 }
16071614 #endif