/*
* SN9C2028 library
*
* Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#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++