// SPDX-License-Identifier: GPL-2.0-or-later
/*
* SN9C2028 library
*
* Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define MODULE_NAME "sn9c2028"
#include "gspca.h"
MODULE_AUTHOR("Theodore Kilgore");
MODULE_DESCRIPTION("Sonix SN9C2028 USB Camera Driver");
MODULE_LICENSE("GPL");
/* specific webcam descriptor */
struct sd {
struct gspca_dev gspca_dev; /* !! must be the first item */
u8 sof_read;
u16 model;
#define MIN_AVG_LUM 8500
#define MAX_AVG_LUM 10000
int avg_lum;
u8 avg_lum_l;
struct { /* autogain and gain control cluster */
struct v4l2_ctrl *autogain;
struct v4l2_ctrl *gain;
};
};
struct init_command {
unsigned char instruction[6];
unsigned char to_read; /* length to read. 0 means no reply requested */
};
/* How to change the resolution of any of the VGA cams is unknown */
static const struct v4l2_pix_format vga_mode[] = {
{640, 480, V4L2_PIX_FMT_SN9C2028, V4L2_FIELD_NONE,
.bytesperline = 640,
.sizeimage = 640 * 480 * 3 / 4,
.colorspace = V4L2_COLORSPACE_SRGB,
.priv = 0},
};
/* No way to change the resolution of the CIF cams is known */
static const struct v4l2_pix_format cif_mode[] = {
{352, 288, V4L2_PIX_FMT_SN9C2028, V4L2_FIELD_NONE,
.bytesperline = 352,
.sizeimage = 352 * 288 * 3 / 4,
.colorspace = V4L2_COLORSPACE_SRGB,
.priv = 0},
};
/* the bytes to write are in gspca_dev->usb_buf */
static int sn9c2028_command(struct gspca_dev *gspca_dev, u8 *command)
{
int rc;
gspca_dbg(gspca_dev, D_USBO, "sending command %02x%02x%02x%02x%02x%02x\n",
command[0], command[1], command[2],
command[3], command[4], command[5]);
memcpy(gspca_dev->usb_buf, command, 6);
rc = usb_control_msg(gspca_dev->dev,
usb_sndctrlpipe(gspca_dev->dev, 0),
USB_REQ_GET_CONFIGURATION,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
2, 0, gspca_dev->usb_buf, 6, 500);
if (rc < 0) {
pr_err("command write [%02x] error %d\n",
gspca_dev->usb_buf[0], rc);
return rc;
}
return 0;
}
static int sn9c2028_read1(struct gspca_dev *gspca_dev)
{
int rc;
rc = usb_control_msg(gspca_dev->dev,
usb_rcvctrlpipe(gspca_dev->dev, 0),
USB_REQ_GET_STATUS,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1, 0, gspca_dev->usb_buf, 1, 500);
if (rc != 1) {
pr_err("read1 error %d\n", rc);
return (rc < 0) ? rc : -EIO;
}
gspca_dbg(gspca_dev, D_USBI, "read1 response %02x\n",
gspca_dev->usb_buf[0]);
return gspca_dev->usb_buf[0];
}
static int sn9c2028_read4(struct gspca_dev *gspca_dev, u8 *reading)
{
int rc;
rc = usb_control_msg(gspca_dev->dev,
usb_rcvctrlpipe(gspca_dev->dev, 0),
USB_REQ_GET_STATUS,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
4, 0, gspca_dev->usb_buf, 4, 500);
if (rc != 4) {
pr_err("read4 error %d\n", rc);
return (rc < 0) ? rc : -EIO;
}
memcpy(reading, gspca_dev->usb_buf, 4);
gspca_dbg(gspca_dev, D_USBI, "read4 response %02x%02x%02x%02x\n",
reading[0], reading[1], reading[2], reading[3]);
return rc;
}
static int sn9c2028_long_command(struct gspca_dev *gspca_dev, u8 *command)
{
int i, status;
__u8 reading[4];
status = sn9c2028_command(gspca_dev, command);
if (status < 0)
return status;
status = -1;
for (i = 0; i < 256 && status < 2; i++)
status = sn9c2028_read1(gspca_dev);
if (status < 0) {
pr_err("long command status read error %d\n", status);
return status;
}
memset(reading, 0, 4);
status = sn9c2028_read4(gspca_dev, reading);
if (status < 0)
return status;
/* in general, the first byte of the response is the first byte of
* the command, or'ed with 8 */
status = sn9c2028_read1(gspca_dev);
if (status < 0)
return status;
return 0;
}
static int sn9c2028_short_command(struct gspca_dev *gspca_dev, u8 *command)
{
int err_code;
err_code = sn9c2028_command(gspca_dev, command);
if (err_code < 0)
return err_code;
err_code = sn9c2028_read1(gspca_dev);
if (err_code < 0)
return err_code;
return 0;
}
/* this function is called at probe time */
static int sd_config(struct gspca_dev *gspca_dev,
const struct usb_device_id *id)
{
struct sd *sd = (struct sd *) gspca_dev;
struct cam *cam = &gspca_dev->cam;
gspca_dbg(gspca_dev, D_PROBE, "SN9C2028 camera detected (vid/pid 0x%04X:0x%04X)\n",
id->idVendor, id->idProduct);
sd->model = id->idProduct;
switch (sd->model) {
case 0x7005:
gspca_dbg(gspca_dev, D_PROBE, "Genius Smart 300 camera\n");
break;
case 0x7003:
gspca_dbg(gspca_dev, D_PROBE, "Genius Videocam Live v2\n");
break;
case 0x8000:
gspca_dbg(gspca_dev, D_PROBE, "DC31VC\n");
break