Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * gspca ViCam subdriver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Based on the usbvideo vicam driver, which is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *                    Chris Cheney (chris.cheney@gmail.com),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *                    Pavel Machek (pavel@ucw.cz),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *                    John Tyner (jtyner@cs.ucr.edu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *                    Monroe Williams (monroe@pobox.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define MODULE_NAME "vicam"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define HEADER_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/ihex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "gspca.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define VICAM_FIRMWARE "vicam/firmware.fw"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) MODULE_FIRMWARE(VICAM_FIRMWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) struct sd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct gspca_dev gspca_dev;	/* !! must be the first item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct work_struct work_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* The vicam sensor has a resolution of 512 x 244, with I believe square
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)    pixels, but this is forced to a 4:3 ratio by optics. So it has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)    non square pixels :( */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static struct v4l2_pix_format vicam_mode[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	{ 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		.bytesperline = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		.sizeimage = 256 * 122,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		.colorspace = V4L2_COLORSPACE_SRGB,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/* 2 modes with somewhat more square pixels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	{ 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		.bytesperline = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		.sizeimage = 256 * 200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		.colorspace = V4L2_COLORSPACE_SRGB,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	{ 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		.bytesperline = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		.sizeimage = 256 * 240,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		.colorspace = V4L2_COLORSPACE_SRGB,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #if 0   /* This mode has extremely non square pixels, testing use only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	{ 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		.bytesperline = 512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		.sizeimage = 512 * 122,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		.colorspace = V4L2_COLORSPACE_SRGB,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	{ 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		.bytesperline = 512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		.sizeimage = 512 * 244,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		.colorspace = V4L2_COLORSPACE_SRGB,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	u16 value, u16 index, u8 *data, u16 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	ret = usb_control_msg(gspca_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			      usb_sndctrlpipe(gspca_dev->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			      request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			      value, index, data, len, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		pr_err("control msg req %02X error %d\n", request, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  *  request and read a block of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int ret, unscaled_height, act_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	u8 *req_data = gspca_dev->usb_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	memset(req_data, 0, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	req_data[0] = gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (gspca_dev->pixfmt.width == 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		req_data[1] |= 0x01; /* low nibble x-scale */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (gspca_dev->pixfmt.height <= 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		req_data[1] |= 0x10; /* high nibble y-scale */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		unscaled_height = gspca_dev->pixfmt.height * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		unscaled_height = gspca_dev->pixfmt.height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	req_data[2] = 0x90; /* unknown, does not seem to do anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (unscaled_height <= 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		req_data[3] = 0x06; /* vend? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	else if (unscaled_height <= 242) /* Yes 242 not 240 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		req_data[3] = 0x07; /* vend? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	else /* Up to 244 lines with req_data[3] == 0x08 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		req_data[3] = 0x08; /* vend? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (expo < 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		/* Frame rate maxed out, use partial frame expo time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		req_data[4] = 255 - expo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		req_data[5] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		req_data[6] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		req_data[7] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		/* Modify frame rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		req_data[4] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		req_data[5] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		req_data[6] = expo & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		req_data[7] = expo >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/* bytes 9-15 do not seem to affect exposure or image quality */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	mutex_lock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	mutex_unlock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	ret = usb_bulk_msg(gspca_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			   usb_rcvbulkpipe(gspca_dev->dev, 0x81),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			   data, size, &act_len, 10000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/* successful, it returns 0, otherwise  negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (ret < 0 || act_len != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		pr_err("bulk read fail (%d) len %d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		       ret, act_len, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * This function is called as a workqueue function and runs whenever the camera
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * is streaming data. Because it is a workqueue function it is allowed to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * so we can use synchronous USB calls. To avoid possible collisions with other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * performing USB operations using it. In practice we don't really need this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * as the cameras controls are only written from the workqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void vicam_dostream(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct sd *sd = container_of(work, struct sd, work_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct gspca_dev *gspca_dev = &sd->gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int ret, frame_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		   HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	buffer = kmalloc(frame_sz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		pr_err("Couldn't allocate USB buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	while (gspca_dev->present && gspca_dev->streaming) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (gspca_dev->frozen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		/* Note the frame header contents seem to be completely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		   constant, they do not change with either image, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		   settings. So we simply discard it. The frames have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		   a very similar 64 byte footer, which we don't even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		   bother reading from the cam */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		gspca_frame_add(gspca_dev, FIRST_PACKET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				buffer + HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				frame_sz - HEADER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* This function is called at probe time just before sd_init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int sd_config(struct gspca_dev *gspca_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct cam *cam = &gspca_dev->cam;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct sd *sd = (struct sd *)gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	/* We don't use the buffer gspca allocates so make it small. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	cam->bulk = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	cam->bulk_size = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	cam->cam_mode = vicam_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	cam->nmodes = ARRAY_SIZE(vicam_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	INIT_WORK(&sd->work_struct, vicam_dostream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* this function is called at probe and resume time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int sd_init(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	const struct ihex_binrec *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	const struct firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	u8 *firmware_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				    &gspca_dev->dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (!firmware_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 					be16_to_cpu(rec->len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	kfree(firmware_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Set up for getting frames. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int sd_start(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct sd *sd = (struct sd *)gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	ret = vicam_set_camera_power(gspca_dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	schedule_work(&sd->work_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* called on streamoff with alt==0 and on disconnect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* the usb_lock is held at entry - restore on exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static void sd_stop0(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct sd *dev = (struct sd *)gspca_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	/* wait for the work queue to terminate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	mutex_unlock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/* This waits for vicam_dostream to finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	flush_work(&dev->work_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	mutex_lock(&gspca_dev->usb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (gspca_dev->present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		vicam_set_camera_power(gspca_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static int sd_init_controls(struct gspca_dev *gspca_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	gspca_dev->vdev.ctrl_handler = hdl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	v4l2_ctrl_handler_init(hdl, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			V4L2_CID_GAIN, 0, 255, 1, 200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (hdl->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		pr_err("Could not initialize controls\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		return hdl->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Table of supported USB devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static const struct usb_device_id device_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	{USB_DEVICE(0x04c1, 0x009d)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	{USB_DEVICE(0x0602, 0x1001)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) MODULE_DEVICE_TABLE(usb, device_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* sub-driver description */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static const struct sd_desc sd_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	.name   = MODULE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	.config = sd_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	.init   = sd_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	.init_controls = sd_init_controls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	.start  = sd_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	.stop0  = sd_stop0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* -- device connect -- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int sd_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	return gspca_dev_probe(intf, id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			&sd_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			sizeof(struct sd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static struct usb_driver sd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	.name       = MODULE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	.id_table   = device_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	.probe      = sd_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	.disconnect = gspca_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	.suspend = gspca_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	.resume  = gspca_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	.reset_resume = gspca_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) module_usb_driver(sd_driver);