#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define MODULE_NAME "vicam"
#define HEADER_SIZE 64
#include <linux/workqueue.h>
#include <linux/slab.h>
#include <linux/firmware.h>
#include <linux/ihex.h>
#include "gspca.h"
#define VICAM_FIRMWARE "vicam/firmware.fw"
MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
MODULE_LICENSE("GPL");
MODULE_FIRMWARE(VICAM_FIRMWARE);
struct sd {
struct gspca_dev gspca_dev;
struct work_struct work_struct;
};
static struct v4l2_pix_format vicam_mode[] = {
{ 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
.bytesperline = 256,
.sizeimage = 256 * 122,
.colorspace = V4L2_COLORSPACE_SRGB,},
{ 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
.bytesperline = 256,
.sizeimage = 256 * 200,
.colorspace = V4L2_COLORSPACE_SRGB,},
{ 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
.bytesperline = 256,
.sizeimage = 256 * 240,
.colorspace = V4L2_COLORSPACE_SRGB,},
#if 0 /* This mode has extremely non square pixels, testing use only */
{ 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
.bytesperline = 512,
.sizeimage = 512 * 122,
.colorspace = V4L2_COLORSPACE_SRGB,},
#endif
{ 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
.bytesperline = 512,
.sizeimage = 512 * 244,
.colorspace = V4L2_COLORSPACE_SRGB,},
};
static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
u16 value, u16 index, u8 *data, u16 len)
{
int ret;
ret = usb_control_msg(gspca_dev->dev,
usb_sndctrlpipe(gspca_dev->dev, 0),
request,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
value, index, data, len, 1000);
if (ret < 0)
pr_err("control msg req %02X error %d\n", request, ret);
return ret;
}
static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
{
int ret;
ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
if (ret < 0)
return ret;
if (state)
ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
return ret;
}
static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
{
int ret, unscaled_height, act_len = 0;
u8 *req_data = gspca_dev->usb_buf;
s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
memset(req_data, 0, 16);
req_data[0] = gain;
if (gspca_dev->pixfmt.width == 256)
req_data[1] |= 0x01;
if (gspca_dev->pixfmt.height <= 122) {
req_data[1] |= 0x10;
unscaled_height = gspca_dev->pixfmt.height * 2;
} else
unscaled_height = gspca_dev->pixfmt.height;
req_data[2] = 0x90;
if (unscaled_height <= 200)
req_data[3] = 0x06;
else if (unscaled_height <= 242)
req_data[3] = 0x07;
else
req_data[3] = 0x08;
if (expo < 256) {
req_data[4] = 255 - expo;
req_data[5] = 0x00;
req_data[6] = 0x00;
req_data[7] = 0x01;
} else {
req_data[4] = 0x00;
req_data[5] = 0x00;
req_data[6] = expo & 0xFF;
req_data[7] = expo >> 8;
}
req_data[8] = ((244 - unscaled_height) / 2) & ~0x01;
mutex_lock(&gspca_dev->usb_lock);
ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
mutex_unlock(&gspca_dev->usb_lock);
if (ret < 0)
return ret;
ret = usb_bulk_msg(gspca_dev->dev,
usb_rcvbulkpipe(gspca_dev->dev, 0x81),
data, size, &act_len, 10000);
if (ret < 0 || act_len != size) {
pr_err("bulk read fail (%d) len %d/%d\n",
ret, act_len, size);
return -EIO;
}
return 0;
}
static void vicam_dostream(struct work_struct *work)
{
struct sd *sd = container_of(work, struct sd, work_struct);
struct gspca_dev *gspca_dev = &sd->gspca_dev;
int ret, frame_sz;
u8 *buffer;
frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
HEADER_SIZE;
buffer = kmalloc(frame_sz, GFP_KERNEL);
if (!buffer) {
pr_err("Couldn't allocate USB buffer\n");
goto exit;
}
while (gspca_dev->present && gspca_dev->streaming) {
#ifdef CONFIG_PM
if (gspca_dev->frozen)
break;
#endif
ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
if (ret < 0)
break;
gspca_frame_add(gspca_dev, FIRST_PACKET,
buffer + HEADER_SIZE,
frame_sz - HEADER_SIZE);
gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
}
exit:
kfree(buffer);
}
static int sd_config(struct gspca_dev *gspca_dev,
const struct usb_device_id *id)
{
struct cam *cam = &gspca_dev->cam;
struct sd *sd = (struct sd *)gspca_dev;
cam->bulk = 1;
cam->bulk_size = 64;
cam->cam_mode = vicam_mode;
cam->nmodes = ARRAY_SIZE(vicam_mode);
INIT_WORK(&sd->work_struct, vicam_dostream);
return 0;
}
static int sd_init(struct gspca_dev *gspca_dev)
{
int ret;
const struct ihex_binrec *rec;
const struct firmware *fw;
u8 *firmware_buf;
ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
&gspca_dev->dev->dev);
if (ret) {
pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
return ret;
}
firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
if (!firmware_buf) {
ret = -ENOMEM;
goto exit;
}
for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
be16_to_cpu(rec->len));
if (ret < 0)
break;
}
kfree(firmware_buf);
exit:
release_firmware(fw);
return ret;
}
static int sd_start(struct gspca_dev *gspca_dev)
{
struct sd *sd = (struct sd *)gspca_dev;
int ret;
ret = vicam_set_camera_power(gspca_dev, 1);
if (ret < 0)
return ret;
schedule_work(&sd->work_struct);
return 0;
}
static void sd_stop0(struct gspca_dev *gspca_dev)
{
struct sd *dev = (struct sd *)gspca_dev;
mutex_unlock(&gspca_dev->usb_lock);
flush_work(&dev->work_struct);
mutex_lock(&gspca_dev->usb_lock);
if (gspca_dev->present)
vicam_set_camera_power(gspca_dev, 0);
}
static int sd_init_controls(struct gspca_dev *gspca_dev)
{
struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
gspca_dev->vdev.ctrl_handler = hdl;
v4l2_ctrl_handler_init(hdl, 2);
gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_GAIN, 0, 255, 1, 200);
if (hdl->error) {
pr_err("Could not initialize controls\n");
return hdl->error;
}
return 0;
}
static const struct usb_device_id device_table[] = {
{USB_DEVICE(0x04c1, 0x009d)},
{USB_DEVICE(0x0602, 0x1001)},
{}
};
MODULE_DEVICE_TABLE(usb, device_table);
static const struct sd_desc sd_desc = {
.name = MODULE_NAME,
.config = sd_config,
.init = sd_init,
.init_controls = sd_init_controls,
.start = sd_start,
.stop0 = sd_stop0,
};
static int sd_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
return gspca_dev_probe(intf, id,
&sd_desc,
sizeof(struct sd),
THIS_MODULE);
}
static struct usb_driver sd_driver = {
.name = MODULE_NAME,
.id_table = device_table,
.probe = sd_probe,
.disconnect = gspca_disconnect,
#ifdef CONFIG_PM
.suspend = gspca_suspend,
.resume = gspca_resume,
.reset_resume = gspca_resume,
#endif
};
module_usb_driver